2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / builtins.c
blobaeec1708759c4737542ba3c0d1d409a8881f9988
1 /* Expand builtin functions.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "predict.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "alias.h"
29 #include "fold-const.h"
30 #include "stringpool.h"
31 #include "stor-layout.h"
32 #include "calls.h"
33 #include "varasm.h"
34 #include "tree-object-size.h"
35 #include "realmpfr.h"
36 #include "cfgrtl.h"
37 #include "internal-fn.h"
38 #include "flags.h"
39 #include "regs.h"
40 #include "except.h"
41 #include "insn-config.h"
42 #include "expmed.h"
43 #include "dojump.h"
44 #include "explow.h"
45 #include "emit-rtl.h"
46 #include "stmt.h"
47 #include "expr.h"
48 #include "insn-codes.h"
49 #include "optabs.h"
50 #include "libfuncs.h"
51 #include "recog.h"
52 #include "output.h"
53 #include "typeclass.h"
54 #include "tm_p.h"
55 #include "target.h"
56 #include "langhooks.h"
57 #include "tree-ssanames.h"
58 #include "tree-dfa.h"
59 #include "value-prof.h"
60 #include "diagnostic-core.h"
61 #include "builtins.h"
62 #include "asan.h"
63 #include "cilk.h"
64 #include "cgraph.h"
65 #include "tree-chkp.h"
66 #include "rtl-chkp.h"
67 #include "gomp-constants.h"
70 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
72 struct target_builtins default_target_builtins;
73 #if SWITCHABLE_TARGET
74 struct target_builtins *this_target_builtins = &default_target_builtins;
75 #endif
77 /* Define the names of the builtin function types and codes. */
78 const char *const built_in_class_names[BUILT_IN_LAST]
79 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
81 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
82 const char * built_in_names[(int) END_BUILTINS] =
84 #include "builtins.def"
86 #undef DEF_BUILTIN
88 /* Setup an array of builtin_info_type, make sure each element decl is
89 initialized to NULL_TREE. */
90 builtin_info_type builtin_info[(int)END_BUILTINS];
92 /* Non-zero if __builtin_constant_p should be folded right away. */
93 bool force_folding_builtin_constant_p;
95 static rtx c_readstr (const char *, machine_mode);
96 static int target_char_cast (tree, char *);
97 static rtx get_memory_rtx (tree, tree);
98 static int apply_args_size (void);
99 static int apply_result_size (void);
100 static rtx result_vector (int, rtx);
101 static void expand_builtin_prefetch (tree);
102 static rtx expand_builtin_apply_args (void);
103 static rtx expand_builtin_apply_args_1 (void);
104 static rtx expand_builtin_apply (rtx, rtx, rtx);
105 static void expand_builtin_return (rtx);
106 static enum type_class type_to_class (tree);
107 static rtx expand_builtin_classify_type (tree);
108 static void expand_errno_check (tree, rtx);
109 static rtx expand_builtin_mathfn (tree, rtx, rtx);
110 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
111 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
112 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
113 static rtx expand_builtin_interclass_mathfn (tree, rtx);
114 static rtx expand_builtin_sincos (tree);
115 static rtx expand_builtin_cexpi (tree, rtx);
116 static rtx expand_builtin_int_roundingfn (tree, rtx);
117 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
118 static rtx expand_builtin_next_arg (void);
119 static rtx expand_builtin_va_start (tree);
120 static rtx expand_builtin_va_end (tree);
121 static rtx expand_builtin_va_copy (tree);
122 static rtx expand_builtin_strcmp (tree, rtx);
123 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
124 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, machine_mode);
125 static rtx expand_builtin_memcpy (tree, rtx);
126 static rtx expand_builtin_memcpy_with_bounds (tree, rtx);
127 static rtx expand_builtin_memcpy_args (tree, tree, tree, rtx, tree);
128 static rtx expand_builtin_mempcpy (tree, rtx, machine_mode);
129 static rtx expand_builtin_mempcpy_with_bounds (tree, rtx, machine_mode);
130 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
131 machine_mode, int, tree);
132 static rtx expand_builtin_strcpy (tree, rtx);
133 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
134 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
135 static rtx expand_builtin_strncpy (tree, rtx);
136 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, machine_mode);
137 static rtx expand_builtin_memset (tree, rtx, machine_mode);
138 static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode);
139 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
140 static rtx expand_builtin_bzero (tree);
141 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
142 static rtx expand_builtin_alloca (tree, bool);
143 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
144 static rtx expand_builtin_frame_address (tree, tree);
145 static tree stabilize_va_list_loc (location_t, tree, int);
146 static rtx expand_builtin_expect (tree, rtx);
147 static tree fold_builtin_constant_p (tree);
148 static tree fold_builtin_classify_type (tree);
149 static tree fold_builtin_strlen (location_t, tree, tree);
150 static tree fold_builtin_inf (location_t, tree, int);
151 static tree fold_builtin_nan (tree, tree, int);
152 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
153 static bool validate_arg (const_tree, enum tree_code code);
154 static bool integer_valued_real_p (tree);
155 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
156 static rtx expand_builtin_fabs (tree, rtx, rtx);
157 static rtx expand_builtin_signbit (tree, rtx);
158 static tree fold_builtin_sqrt (location_t, tree, tree);
159 static tree fold_builtin_cbrt (location_t, tree, tree);
160 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
161 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
162 static tree fold_builtin_cos (location_t, tree, tree, tree);
163 static tree fold_builtin_cosh (location_t, tree, tree, tree);
164 static tree fold_builtin_tan (tree, tree);
165 static tree fold_builtin_trunc (location_t, tree, tree);
166 static tree fold_builtin_floor (location_t, tree, tree);
167 static tree fold_builtin_ceil (location_t, tree, tree);
168 static tree fold_builtin_round (location_t, tree, tree);
169 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
170 static tree fold_builtin_bitop (tree, tree);
171 static tree fold_builtin_strchr (location_t, tree, tree, tree);
172 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
173 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
174 static tree fold_builtin_strcmp (location_t, tree, tree);
175 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
176 static tree fold_builtin_signbit (location_t, tree, tree);
177 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
178 static tree fold_builtin_isascii (location_t, tree);
179 static tree fold_builtin_toascii (location_t, tree);
180 static tree fold_builtin_isdigit (location_t, tree);
181 static tree fold_builtin_fabs (location_t, tree, tree);
182 static tree fold_builtin_abs (location_t, tree, tree);
183 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
184 enum tree_code);
185 static tree fold_builtin_0 (location_t, tree);
186 static tree fold_builtin_1 (location_t, tree, tree);
187 static tree fold_builtin_2 (location_t, tree, tree, tree);
188 static tree fold_builtin_3 (location_t, tree, tree, tree, tree);
189 static tree fold_builtin_varargs (location_t, tree, tree*, int);
191 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
192 static tree fold_builtin_strstr (location_t, tree, tree, tree);
193 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
194 static tree fold_builtin_strspn (location_t, tree, tree);
195 static tree fold_builtin_strcspn (location_t, tree, tree);
197 static rtx expand_builtin_object_size (tree);
198 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
199 enum built_in_function);
200 static void maybe_emit_chk_warning (tree, enum built_in_function);
201 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
202 static void maybe_emit_free_warning (tree);
203 static tree fold_builtin_object_size (tree, tree);
205 unsigned HOST_WIDE_INT target_newline;
206 unsigned HOST_WIDE_INT target_percent;
207 static unsigned HOST_WIDE_INT target_c;
208 static unsigned HOST_WIDE_INT target_s;
209 char target_percent_c[3];
210 char target_percent_s[3];
211 char target_percent_s_newline[4];
212 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
213 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
214 static tree do_mpfr_arg2 (tree, tree, tree,
215 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
216 static tree do_mpfr_arg3 (tree, tree, tree, tree,
217 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
218 static tree do_mpfr_sincos (tree, tree, tree);
219 static tree do_mpfr_bessel_n (tree, tree, tree,
220 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
221 const REAL_VALUE_TYPE *, bool);
222 static tree do_mpfr_remquo (tree, tree, tree);
223 static tree do_mpfr_lgamma_r (tree, tree, tree);
224 static void expand_builtin_sync_synchronize (void);
226 /* Return true if NAME starts with __builtin_ or __sync_. */
228 static bool
229 is_builtin_name (const char *name)
231 if (strncmp (name, "__builtin_", 10) == 0)
232 return true;
233 if (strncmp (name, "__sync_", 7) == 0)
234 return true;
235 if (strncmp (name, "__atomic_", 9) == 0)
236 return true;
237 if (flag_cilkplus
238 && (!strcmp (name, "__cilkrts_detach")
239 || !strcmp (name, "__cilkrts_pop_frame")))
240 return true;
241 return false;
245 /* Return true if DECL is a function symbol representing a built-in. */
247 bool
248 is_builtin_fn (tree decl)
250 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
253 /* Return true if NODE should be considered for inline expansion regardless
254 of the optimization level. This means whenever a function is invoked with
255 its "internal" name, which normally contains the prefix "__builtin". */
257 static bool
258 called_as_built_in (tree node)
260 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
261 we want the name used to call the function, not the name it
262 will have. */
263 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
264 return is_builtin_name (name);
267 /* Compute values M and N such that M divides (address of EXP - N) and such
268 that N < M. If these numbers can be determined, store M in alignp and N in
269 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
270 *alignp and any bit-offset to *bitposp.
272 Note that the address (and thus the alignment) computed here is based
273 on the address to which a symbol resolves, whereas DECL_ALIGN is based
274 on the address at which an object is actually located. These two
275 addresses are not always the same. For example, on ARM targets,
276 the address &foo of a Thumb function foo() has the lowest bit set,
277 whereas foo() itself starts on an even address.
279 If ADDR_P is true we are taking the address of the memory reference EXP
280 and thus cannot rely on the access taking place. */
282 static bool
283 get_object_alignment_2 (tree exp, unsigned int *alignp,
284 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
286 HOST_WIDE_INT bitsize, bitpos;
287 tree offset;
288 machine_mode mode;
289 int unsignedp, volatilep;
290 unsigned int align = BITS_PER_UNIT;
291 bool known_alignment = false;
293 /* Get the innermost object and the constant (bitpos) and possibly
294 variable (offset) offset of the access. */
295 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
296 &mode, &unsignedp, &volatilep, true);
298 /* Extract alignment information from the innermost object and
299 possibly adjust bitpos and offset. */
300 if (TREE_CODE (exp) == FUNCTION_DECL)
302 /* Function addresses can encode extra information besides their
303 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
304 allows the low bit to be used as a virtual bit, we know
305 that the address itself must be at least 2-byte aligned. */
306 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
307 align = 2 * BITS_PER_UNIT;
309 else if (TREE_CODE (exp) == LABEL_DECL)
311 else if (TREE_CODE (exp) == CONST_DECL)
313 /* The alignment of a CONST_DECL is determined by its initializer. */
314 exp = DECL_INITIAL (exp);
315 align = TYPE_ALIGN (TREE_TYPE (exp));
316 if (CONSTANT_CLASS_P (exp))
317 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
319 known_alignment = true;
321 else if (DECL_P (exp))
323 align = DECL_ALIGN (exp);
324 known_alignment = true;
326 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
328 align = TYPE_ALIGN (TREE_TYPE (exp));
330 else if (TREE_CODE (exp) == INDIRECT_REF
331 || TREE_CODE (exp) == MEM_REF
332 || TREE_CODE (exp) == TARGET_MEM_REF)
334 tree addr = TREE_OPERAND (exp, 0);
335 unsigned ptr_align;
336 unsigned HOST_WIDE_INT ptr_bitpos;
337 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
339 /* If the address is explicitely aligned, handle that. */
340 if (TREE_CODE (addr) == BIT_AND_EXPR
341 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
343 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
344 ptr_bitmask *= BITS_PER_UNIT;
345 align = ptr_bitmask & -ptr_bitmask;
346 addr = TREE_OPERAND (addr, 0);
349 known_alignment
350 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
351 align = MAX (ptr_align, align);
353 /* Re-apply explicit alignment to the bitpos. */
354 ptr_bitpos &= ptr_bitmask;
356 /* The alignment of the pointer operand in a TARGET_MEM_REF
357 has to take the variable offset parts into account. */
358 if (TREE_CODE (exp) == TARGET_MEM_REF)
360 if (TMR_INDEX (exp))
362 unsigned HOST_WIDE_INT step = 1;
363 if (TMR_STEP (exp))
364 step = TREE_INT_CST_LOW (TMR_STEP (exp));
365 align = MIN (align, (step & -step) * BITS_PER_UNIT);
367 if (TMR_INDEX2 (exp))
368 align = BITS_PER_UNIT;
369 known_alignment = false;
372 /* When EXP is an actual memory reference then we can use
373 TYPE_ALIGN of a pointer indirection to derive alignment.
374 Do so only if get_pointer_alignment_1 did not reveal absolute
375 alignment knowledge and if using that alignment would
376 improve the situation. */
377 if (!addr_p && !known_alignment
378 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
379 align = TYPE_ALIGN (TREE_TYPE (exp));
380 else
382 /* Else adjust bitpos accordingly. */
383 bitpos += ptr_bitpos;
384 if (TREE_CODE (exp) == MEM_REF
385 || TREE_CODE (exp) == TARGET_MEM_REF)
386 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
389 else if (TREE_CODE (exp) == STRING_CST)
391 /* STRING_CST are the only constant objects we allow to be not
392 wrapped inside a CONST_DECL. */
393 align = TYPE_ALIGN (TREE_TYPE (exp));
394 if (CONSTANT_CLASS_P (exp))
395 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
397 known_alignment = true;
400 /* If there is a non-constant offset part extract the maximum
401 alignment that can prevail. */
402 if (offset)
404 unsigned int trailing_zeros = tree_ctz (offset);
405 if (trailing_zeros < HOST_BITS_PER_INT)
407 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
408 if (inner)
409 align = MIN (align, inner);
413 *alignp = align;
414 *bitposp = bitpos & (*alignp - 1);
415 return known_alignment;
418 /* For a memory reference expression EXP compute values M and N such that M
419 divides (&EXP - N) and such that N < M. If these numbers can be determined,
420 store M in alignp and N in *BITPOSP and return true. Otherwise return false
421 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
423 bool
424 get_object_alignment_1 (tree exp, unsigned int *alignp,
425 unsigned HOST_WIDE_INT *bitposp)
427 return get_object_alignment_2 (exp, alignp, bitposp, false);
430 /* Return the alignment in bits of EXP, an object. */
432 unsigned int
433 get_object_alignment (tree exp)
435 unsigned HOST_WIDE_INT bitpos = 0;
436 unsigned int align;
438 get_object_alignment_1 (exp, &align, &bitpos);
440 /* align and bitpos now specify known low bits of the pointer.
441 ptr & (align - 1) == bitpos. */
443 if (bitpos != 0)
444 align = (bitpos & -bitpos);
445 return align;
448 /* For a pointer valued expression EXP compute values M and N such that M
449 divides (EXP - N) and such that N < M. If these numbers can be determined,
450 store M in alignp and N in *BITPOSP and return true. Return false if
451 the results are just a conservative approximation.
453 If EXP is not a pointer, false is returned too. */
455 bool
456 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
457 unsigned HOST_WIDE_INT *bitposp)
459 STRIP_NOPS (exp);
461 if (TREE_CODE (exp) == ADDR_EXPR)
462 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
463 alignp, bitposp, true);
464 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
466 unsigned int align;
467 unsigned HOST_WIDE_INT bitpos;
468 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
469 &align, &bitpos);
470 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
471 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
472 else
474 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
475 if (trailing_zeros < HOST_BITS_PER_INT)
477 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
478 if (inner)
479 align = MIN (align, inner);
482 *alignp = align;
483 *bitposp = bitpos & (align - 1);
484 return res;
486 else if (TREE_CODE (exp) == SSA_NAME
487 && POINTER_TYPE_P (TREE_TYPE (exp)))
489 unsigned int ptr_align, ptr_misalign;
490 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
492 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
494 *bitposp = ptr_misalign * BITS_PER_UNIT;
495 *alignp = ptr_align * BITS_PER_UNIT;
496 /* We cannot really tell whether this result is an approximation. */
497 return true;
499 else
501 *bitposp = 0;
502 *alignp = BITS_PER_UNIT;
503 return false;
506 else if (TREE_CODE (exp) == INTEGER_CST)
508 *alignp = BIGGEST_ALIGNMENT;
509 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
510 & (BIGGEST_ALIGNMENT - 1));
511 return true;
514 *bitposp = 0;
515 *alignp = BITS_PER_UNIT;
516 return false;
519 /* Return the alignment in bits of EXP, a pointer valued expression.
520 The alignment returned is, by default, the alignment of the thing that
521 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
523 Otherwise, look at the expression to see if we can do better, i.e., if the
524 expression is actually pointing at an object whose alignment is tighter. */
526 unsigned int
527 get_pointer_alignment (tree exp)
529 unsigned HOST_WIDE_INT bitpos = 0;
530 unsigned int align;
532 get_pointer_alignment_1 (exp, &align, &bitpos);
534 /* align and bitpos now specify known low bits of the pointer.
535 ptr & (align - 1) == bitpos. */
537 if (bitpos != 0)
538 align = (bitpos & -bitpos);
540 return align;
543 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
544 way, because it could contain a zero byte in the middle.
545 TREE_STRING_LENGTH is the size of the character array, not the string.
547 ONLY_VALUE should be nonzero if the result is not going to be emitted
548 into the instruction stream and zero if it is going to be expanded.
549 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
550 is returned, otherwise NULL, since
551 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
552 evaluate the side-effects.
554 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
555 accesses. Note that this implies the result is not going to be emitted
556 into the instruction stream.
558 The value returned is of type `ssizetype'.
560 Unfortunately, string_constant can't access the values of const char
561 arrays with initializers, so neither can we do so here. */
563 tree
564 c_strlen (tree src, int only_value)
566 tree offset_node;
567 HOST_WIDE_INT offset;
568 int max;
569 const char *ptr;
570 location_t loc;
572 STRIP_NOPS (src);
573 if (TREE_CODE (src) == COND_EXPR
574 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
576 tree len1, len2;
578 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
579 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
580 if (tree_int_cst_equal (len1, len2))
581 return len1;
584 if (TREE_CODE (src) == COMPOUND_EXPR
585 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
586 return c_strlen (TREE_OPERAND (src, 1), only_value);
588 loc = EXPR_LOC_OR_LOC (src, input_location);
590 src = string_constant (src, &offset_node);
591 if (src == 0)
592 return NULL_TREE;
594 max = TREE_STRING_LENGTH (src) - 1;
595 ptr = TREE_STRING_POINTER (src);
597 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
599 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
600 compute the offset to the following null if we don't know where to
601 start searching for it. */
602 int i;
604 for (i = 0; i < max; i++)
605 if (ptr[i] == 0)
606 return NULL_TREE;
608 /* We don't know the starting offset, but we do know that the string
609 has no internal zero bytes. We can assume that the offset falls
610 within the bounds of the string; otherwise, the programmer deserves
611 what he gets. Subtract the offset from the length of the string,
612 and return that. This would perhaps not be valid if we were dealing
613 with named arrays in addition to literal string constants. */
615 return size_diffop_loc (loc, size_int (max), offset_node);
618 /* We have a known offset into the string. Start searching there for
619 a null character if we can represent it as a single HOST_WIDE_INT. */
620 if (offset_node == 0)
621 offset = 0;
622 else if (! tree_fits_shwi_p (offset_node))
623 offset = -1;
624 else
625 offset = tree_to_shwi (offset_node);
627 /* If the offset is known to be out of bounds, warn, and call strlen at
628 runtime. */
629 if (offset < 0 || offset > max)
631 /* Suppress multiple warnings for propagated constant strings. */
632 if (only_value != 2
633 && !TREE_NO_WARNING (src))
635 warning_at (loc, 0, "offset outside bounds of constant string");
636 TREE_NO_WARNING (src) = 1;
638 return NULL_TREE;
641 /* Use strlen to search for the first zero byte. Since any strings
642 constructed with build_string will have nulls appended, we win even
643 if we get handed something like (char[4])"abcd".
645 Since OFFSET is our starting index into the string, no further
646 calculation is needed. */
647 return ssize_int (strlen (ptr + offset));
650 /* Return a char pointer for a C string if it is a string constant
651 or sum of string constant and integer constant. */
653 const char *
654 c_getstr (tree src)
656 tree offset_node;
658 src = string_constant (src, &offset_node);
659 if (src == 0)
660 return 0;
662 if (offset_node == 0)
663 return TREE_STRING_POINTER (src);
664 else if (!tree_fits_uhwi_p (offset_node)
665 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
666 return 0;
668 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
671 /* Return a constant integer corresponding to target reading
672 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
674 static rtx
675 c_readstr (const char *str, machine_mode mode)
677 HOST_WIDE_INT ch;
678 unsigned int i, j;
679 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
681 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
682 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
683 / HOST_BITS_PER_WIDE_INT;
685 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
686 for (i = 0; i < len; i++)
687 tmp[i] = 0;
689 ch = 1;
690 for (i = 0; i < GET_MODE_SIZE (mode); i++)
692 j = i;
693 if (WORDS_BIG_ENDIAN)
694 j = GET_MODE_SIZE (mode) - i - 1;
695 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
696 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
697 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
698 j *= BITS_PER_UNIT;
700 if (ch)
701 ch = (unsigned char) str[i];
702 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
705 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
706 return immed_wide_int_const (c, mode);
709 /* Cast a target constant CST to target CHAR and if that value fits into
710 host char type, return zero and put that value into variable pointed to by
711 P. */
713 static int
714 target_char_cast (tree cst, char *p)
716 unsigned HOST_WIDE_INT val, hostval;
718 if (TREE_CODE (cst) != INTEGER_CST
719 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
720 return 1;
722 /* Do not care if it fits or not right here. */
723 val = TREE_INT_CST_LOW (cst);
725 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
726 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
728 hostval = val;
729 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
730 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
732 if (val != hostval)
733 return 1;
735 *p = hostval;
736 return 0;
739 /* Similar to save_expr, but assumes that arbitrary code is not executed
740 in between the multiple evaluations. In particular, we assume that a
741 non-addressable local variable will not be modified. */
743 static tree
744 builtin_save_expr (tree exp)
746 if (TREE_CODE (exp) == SSA_NAME
747 || (TREE_ADDRESSABLE (exp) == 0
748 && (TREE_CODE (exp) == PARM_DECL
749 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
750 return exp;
752 return save_expr (exp);
755 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
756 times to get the address of either a higher stack frame, or a return
757 address located within it (depending on FNDECL_CODE). */
759 static rtx
760 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
762 int i;
764 #ifdef INITIAL_FRAME_ADDRESS_RTX
765 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
766 #else
767 rtx tem;
769 /* For a zero count with __builtin_return_address, we don't care what
770 frame address we return, because target-specific definitions will
771 override us. Therefore frame pointer elimination is OK, and using
772 the soft frame pointer is OK.
774 For a nonzero count, or a zero count with __builtin_frame_address,
775 we require a stable offset from the current frame pointer to the
776 previous one, so we must use the hard frame pointer, and
777 we must disable frame pointer elimination. */
778 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
779 tem = frame_pointer_rtx;
780 else
782 tem = hard_frame_pointer_rtx;
784 /* Tell reload not to eliminate the frame pointer. */
785 crtl->accesses_prior_frames = 1;
787 #endif
789 /* Some machines need special handling before we can access
790 arbitrary frames. For example, on the SPARC, we must first flush
791 all register windows to the stack. */
792 #ifdef SETUP_FRAME_ADDRESSES
793 if (count > 0)
794 SETUP_FRAME_ADDRESSES ();
795 #endif
797 /* On the SPARC, the return address is not in the frame, it is in a
798 register. There is no way to access it off of the current frame
799 pointer, but it can be accessed off the previous frame pointer by
800 reading the value from the register window save area. */
801 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
802 count--;
804 /* Scan back COUNT frames to the specified frame. */
805 for (i = 0; i < count; i++)
807 /* Assume the dynamic chain pointer is in the word that the
808 frame address points to, unless otherwise specified. */
809 #ifdef DYNAMIC_CHAIN_ADDRESS
810 tem = DYNAMIC_CHAIN_ADDRESS (tem);
811 #endif
812 tem = memory_address (Pmode, tem);
813 tem = gen_frame_mem (Pmode, tem);
814 tem = copy_to_reg (tem);
817 /* For __builtin_frame_address, return what we've got. But, on
818 the SPARC for example, we may have to add a bias. */
819 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
820 #ifdef FRAME_ADDR_RTX
821 return FRAME_ADDR_RTX (tem);
822 #else
823 return tem;
824 #endif
826 /* For __builtin_return_address, get the return address from that frame. */
827 #ifdef RETURN_ADDR_RTX
828 tem = RETURN_ADDR_RTX (count, tem);
829 #else
830 tem = memory_address (Pmode,
831 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
832 tem = gen_frame_mem (Pmode, tem);
833 #endif
834 return tem;
837 /* Alias set used for setjmp buffer. */
838 static alias_set_type setjmp_alias_set = -1;
840 /* Construct the leading half of a __builtin_setjmp call. Control will
841 return to RECEIVER_LABEL. This is also called directly by the SJLJ
842 exception handling code. */
844 void
845 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
847 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
848 rtx stack_save;
849 rtx mem;
851 if (setjmp_alias_set == -1)
852 setjmp_alias_set = new_alias_set ();
854 buf_addr = convert_memory_address (Pmode, buf_addr);
856 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
858 /* We store the frame pointer and the address of receiver_label in
859 the buffer and use the rest of it for the stack save area, which
860 is machine-dependent. */
862 mem = gen_rtx_MEM (Pmode, buf_addr);
863 set_mem_alias_set (mem, setjmp_alias_set);
864 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
866 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
867 GET_MODE_SIZE (Pmode))),
868 set_mem_alias_set (mem, setjmp_alias_set);
870 emit_move_insn (validize_mem (mem),
871 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
873 stack_save = gen_rtx_MEM (sa_mode,
874 plus_constant (Pmode, buf_addr,
875 2 * GET_MODE_SIZE (Pmode)));
876 set_mem_alias_set (stack_save, setjmp_alias_set);
877 emit_stack_save (SAVE_NONLOCAL, &stack_save);
879 /* If there is further processing to do, do it. */
880 if (targetm.have_builtin_setjmp_setup ())
881 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
883 /* We have a nonlocal label. */
884 cfun->has_nonlocal_label = 1;
887 /* Construct the trailing part of a __builtin_setjmp call. This is
888 also called directly by the SJLJ exception handling code.
889 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
891 void
892 expand_builtin_setjmp_receiver (rtx receiver_label)
894 rtx chain;
896 /* Mark the FP as used when we get here, so we have to make sure it's
897 marked as used by this function. */
898 emit_use (hard_frame_pointer_rtx);
900 /* Mark the static chain as clobbered here so life information
901 doesn't get messed up for it. */
902 chain = targetm.calls.static_chain (current_function_decl, true);
903 if (chain && REG_P (chain))
904 emit_clobber (chain);
906 /* Now put in the code to restore the frame pointer, and argument
907 pointer, if needed. */
908 if (! targetm.have_nonlocal_goto ())
910 /* First adjust our frame pointer to its actual value. It was
911 previously set to the start of the virtual area corresponding to
912 the stacked variables when we branched here and now needs to be
913 adjusted to the actual hardware fp value.
915 Assignments to virtual registers are converted by
916 instantiate_virtual_regs into the corresponding assignment
917 to the underlying register (fp in this case) that makes
918 the original assignment true.
919 So the following insn will actually be decrementing fp by
920 STARTING_FRAME_OFFSET. */
921 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
923 /* Restoring the frame pointer also modifies the hard frame pointer.
924 Mark it used (so that the previous assignment remains live once
925 the frame pointer is eliminated) and clobbered (to represent the
926 implicit update from the assignment). */
927 emit_use (hard_frame_pointer_rtx);
928 emit_clobber (hard_frame_pointer_rtx);
931 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
932 if (fixed_regs[ARG_POINTER_REGNUM])
934 #ifdef ELIMINABLE_REGS
935 /* If the argument pointer can be eliminated in favor of the
936 frame pointer, we don't need to restore it. We assume here
937 that if such an elimination is present, it can always be used.
938 This is the case on all known machines; if we don't make this
939 assumption, we do unnecessary saving on many machines. */
940 size_t i;
941 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
943 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
944 if (elim_regs[i].from == ARG_POINTER_REGNUM
945 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
946 break;
948 if (i == ARRAY_SIZE (elim_regs))
949 #endif
951 /* Now restore our arg pointer from the address at which it
952 was saved in our stack frame. */
953 emit_move_insn (crtl->args.internal_arg_pointer,
954 copy_to_reg (get_arg_pointer_save_area ()));
957 #endif
959 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
960 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
961 else if (targetm.have_nonlocal_goto_receiver ())
962 emit_insn (targetm.gen_nonlocal_goto_receiver ());
963 else
964 { /* Nothing */ }
966 /* We must not allow the code we just generated to be reordered by
967 scheduling. Specifically, the update of the frame pointer must
968 happen immediately, not later. */
969 emit_insn (gen_blockage ());
972 /* __builtin_longjmp is passed a pointer to an array of five words (not
973 all will be used on all machines). It operates similarly to the C
974 library function of the same name, but is more efficient. Much of
975 the code below is copied from the handling of non-local gotos. */
977 static void
978 expand_builtin_longjmp (rtx buf_addr, rtx value)
980 rtx fp, lab, stack;
981 rtx_insn *insn, *last;
982 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
984 /* DRAP is needed for stack realign if longjmp is expanded to current
985 function */
986 if (SUPPORTS_STACK_ALIGNMENT)
987 crtl->need_drap = true;
989 if (setjmp_alias_set == -1)
990 setjmp_alias_set = new_alias_set ();
992 buf_addr = convert_memory_address (Pmode, buf_addr);
994 buf_addr = force_reg (Pmode, buf_addr);
996 /* We require that the user must pass a second argument of 1, because
997 that is what builtin_setjmp will return. */
998 gcc_assert (value == const1_rtx);
1000 last = get_last_insn ();
1001 if (targetm.have_builtin_longjmp ())
1002 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
1003 else
1005 fp = gen_rtx_MEM (Pmode, buf_addr);
1006 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1007 GET_MODE_SIZE (Pmode)));
1009 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1010 2 * GET_MODE_SIZE (Pmode)));
1011 set_mem_alias_set (fp, setjmp_alias_set);
1012 set_mem_alias_set (lab, setjmp_alias_set);
1013 set_mem_alias_set (stack, setjmp_alias_set);
1015 /* Pick up FP, label, and SP from the block and jump. This code is
1016 from expand_goto in stmt.c; see there for detailed comments. */
1017 if (targetm.have_nonlocal_goto ())
1018 /* We have to pass a value to the nonlocal_goto pattern that will
1019 get copied into the static_chain pointer, but it does not matter
1020 what that value is, because builtin_setjmp does not use it. */
1021 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1022 else
1024 lab = copy_to_reg (lab);
1026 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1027 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1029 emit_move_insn (hard_frame_pointer_rtx, fp);
1030 emit_stack_restore (SAVE_NONLOCAL, stack);
1032 emit_use (hard_frame_pointer_rtx);
1033 emit_use (stack_pointer_rtx);
1034 emit_indirect_jump (lab);
1038 /* Search backwards and mark the jump insn as a non-local goto.
1039 Note that this precludes the use of __builtin_longjmp to a
1040 __builtin_setjmp target in the same function. However, we've
1041 already cautioned the user that these functions are for
1042 internal exception handling use only. */
1043 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1045 gcc_assert (insn != last);
1047 if (JUMP_P (insn))
1049 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1050 break;
1052 else if (CALL_P (insn))
1053 break;
1057 static inline bool
1058 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1060 return (iter->i < iter->n);
1063 /* This function validates the types of a function call argument list
1064 against a specified list of tree_codes. If the last specifier is a 0,
1065 that represents an ellipses, otherwise the last specifier must be a
1066 VOID_TYPE. */
1068 static bool
1069 validate_arglist (const_tree callexpr, ...)
1071 enum tree_code code;
1072 bool res = 0;
1073 va_list ap;
1074 const_call_expr_arg_iterator iter;
1075 const_tree arg;
1077 va_start (ap, callexpr);
1078 init_const_call_expr_arg_iterator (callexpr, &iter);
1082 code = (enum tree_code) va_arg (ap, int);
1083 switch (code)
1085 case 0:
1086 /* This signifies an ellipses, any further arguments are all ok. */
1087 res = true;
1088 goto end;
1089 case VOID_TYPE:
1090 /* This signifies an endlink, if no arguments remain, return
1091 true, otherwise return false. */
1092 res = !more_const_call_expr_args_p (&iter);
1093 goto end;
1094 default:
1095 /* If no parameters remain or the parameter's code does not
1096 match the specified code, return false. Otherwise continue
1097 checking any remaining arguments. */
1098 arg = next_const_call_expr_arg (&iter);
1099 if (!validate_arg (arg, code))
1100 goto end;
1101 break;
1104 while (1);
1106 /* We need gotos here since we can only have one VA_CLOSE in a
1107 function. */
1108 end: ;
1109 va_end (ap);
1111 return res;
1114 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1115 and the address of the save area. */
1117 static rtx
1118 expand_builtin_nonlocal_goto (tree exp)
1120 tree t_label, t_save_area;
1121 rtx r_label, r_save_area, r_fp, r_sp;
1122 rtx_insn *insn;
1124 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1125 return NULL_RTX;
1127 t_label = CALL_EXPR_ARG (exp, 0);
1128 t_save_area = CALL_EXPR_ARG (exp, 1);
1130 r_label = expand_normal (t_label);
1131 r_label = convert_memory_address (Pmode, r_label);
1132 r_save_area = expand_normal (t_save_area);
1133 r_save_area = convert_memory_address (Pmode, r_save_area);
1134 /* Copy the address of the save location to a register just in case it was
1135 based on the frame pointer. */
1136 r_save_area = copy_to_reg (r_save_area);
1137 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1138 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1139 plus_constant (Pmode, r_save_area,
1140 GET_MODE_SIZE (Pmode)));
1142 crtl->has_nonlocal_goto = 1;
1144 /* ??? We no longer need to pass the static chain value, afaik. */
1145 if (targetm.have_nonlocal_goto ())
1146 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1147 else
1149 r_label = copy_to_reg (r_label);
1151 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1152 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1154 /* Restore frame pointer for containing function. */
1155 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1156 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1158 /* USE of hard_frame_pointer_rtx added for consistency;
1159 not clear if really needed. */
1160 emit_use (hard_frame_pointer_rtx);
1161 emit_use (stack_pointer_rtx);
1163 /* If the architecture is using a GP register, we must
1164 conservatively assume that the target function makes use of it.
1165 The prologue of functions with nonlocal gotos must therefore
1166 initialize the GP register to the appropriate value, and we
1167 must then make sure that this value is live at the point
1168 of the jump. (Note that this doesn't necessarily apply
1169 to targets with a nonlocal_goto pattern; they are free
1170 to implement it in their own way. Note also that this is
1171 a no-op if the GP register is a global invariant.) */
1172 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1173 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1174 emit_use (pic_offset_table_rtx);
1176 emit_indirect_jump (r_label);
1179 /* Search backwards to the jump insn and mark it as a
1180 non-local goto. */
1181 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1183 if (JUMP_P (insn))
1185 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1186 break;
1188 else if (CALL_P (insn))
1189 break;
1192 return const0_rtx;
1195 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1196 (not all will be used on all machines) that was passed to __builtin_setjmp.
1197 It updates the stack pointer in that block to the current value. This is
1198 also called directly by the SJLJ exception handling code. */
1200 void
1201 expand_builtin_update_setjmp_buf (rtx buf_addr)
1203 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1204 rtx stack_save
1205 = gen_rtx_MEM (sa_mode,
1206 memory_address
1207 (sa_mode,
1208 plus_constant (Pmode, buf_addr,
1209 2 * GET_MODE_SIZE (Pmode))));
1211 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1214 /* Expand a call to __builtin_prefetch. For a target that does not support
1215 data prefetch, evaluate the memory address argument in case it has side
1216 effects. */
1218 static void
1219 expand_builtin_prefetch (tree exp)
1221 tree arg0, arg1, arg2;
1222 int nargs;
1223 rtx op0, op1, op2;
1225 if (!validate_arglist (exp, POINTER_TYPE, 0))
1226 return;
1228 arg0 = CALL_EXPR_ARG (exp, 0);
1230 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1231 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1232 locality). */
1233 nargs = call_expr_nargs (exp);
1234 if (nargs > 1)
1235 arg1 = CALL_EXPR_ARG (exp, 1);
1236 else
1237 arg1 = integer_zero_node;
1238 if (nargs > 2)
1239 arg2 = CALL_EXPR_ARG (exp, 2);
1240 else
1241 arg2 = integer_three_node;
1243 /* Argument 0 is an address. */
1244 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1246 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1247 if (TREE_CODE (arg1) != INTEGER_CST)
1249 error ("second argument to %<__builtin_prefetch%> must be a constant");
1250 arg1 = integer_zero_node;
1252 op1 = expand_normal (arg1);
1253 /* Argument 1 must be either zero or one. */
1254 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1256 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1257 " using zero");
1258 op1 = const0_rtx;
1261 /* Argument 2 (locality) must be a compile-time constant int. */
1262 if (TREE_CODE (arg2) != INTEGER_CST)
1264 error ("third argument to %<__builtin_prefetch%> must be a constant");
1265 arg2 = integer_zero_node;
1267 op2 = expand_normal (arg2);
1268 /* Argument 2 must be 0, 1, 2, or 3. */
1269 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1271 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1272 op2 = const0_rtx;
1275 if (targetm.have_prefetch ())
1277 struct expand_operand ops[3];
1279 create_address_operand (&ops[0], op0);
1280 create_integer_operand (&ops[1], INTVAL (op1));
1281 create_integer_operand (&ops[2], INTVAL (op2));
1282 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1283 return;
1286 /* Don't do anything with direct references to volatile memory, but
1287 generate code to handle other side effects. */
1288 if (!MEM_P (op0) && side_effects_p (op0))
1289 emit_insn (op0);
1292 /* Get a MEM rtx for expression EXP which is the address of an operand
1293 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1294 the maximum length of the block of memory that might be accessed or
1295 NULL if unknown. */
1297 static rtx
1298 get_memory_rtx (tree exp, tree len)
1300 tree orig_exp = exp;
1301 rtx addr, mem;
1303 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1304 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1305 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1306 exp = TREE_OPERAND (exp, 0);
1308 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1309 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1311 /* Get an expression we can use to find the attributes to assign to MEM.
1312 First remove any nops. */
1313 while (CONVERT_EXPR_P (exp)
1314 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1315 exp = TREE_OPERAND (exp, 0);
1317 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1318 (as builtin stringops may alias with anything). */
1319 exp = fold_build2 (MEM_REF,
1320 build_array_type (char_type_node,
1321 build_range_type (sizetype,
1322 size_one_node, len)),
1323 exp, build_int_cst (ptr_type_node, 0));
1325 /* If the MEM_REF has no acceptable address, try to get the base object
1326 from the original address we got, and build an all-aliasing
1327 unknown-sized access to that one. */
1328 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1329 set_mem_attributes (mem, exp, 0);
1330 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1331 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1332 0))))
1334 exp = build_fold_addr_expr (exp);
1335 exp = fold_build2 (MEM_REF,
1336 build_array_type (char_type_node,
1337 build_range_type (sizetype,
1338 size_zero_node,
1339 NULL)),
1340 exp, build_int_cst (ptr_type_node, 0));
1341 set_mem_attributes (mem, exp, 0);
1343 set_mem_alias_set (mem, 0);
1344 return mem;
1347 /* Built-in functions to perform an untyped call and return. */
1349 #define apply_args_mode \
1350 (this_target_builtins->x_apply_args_mode)
1351 #define apply_result_mode \
1352 (this_target_builtins->x_apply_result_mode)
1354 /* Return the size required for the block returned by __builtin_apply_args,
1355 and initialize apply_args_mode. */
1357 static int
1358 apply_args_size (void)
1360 static int size = -1;
1361 int align;
1362 unsigned int regno;
1363 machine_mode mode;
1365 /* The values computed by this function never change. */
1366 if (size < 0)
1368 /* The first value is the incoming arg-pointer. */
1369 size = GET_MODE_SIZE (Pmode);
1371 /* The second value is the structure value address unless this is
1372 passed as an "invisible" first argument. */
1373 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1374 size += GET_MODE_SIZE (Pmode);
1376 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1377 if (FUNCTION_ARG_REGNO_P (regno))
1379 mode = targetm.calls.get_raw_arg_mode (regno);
1381 gcc_assert (mode != VOIDmode);
1383 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1384 if (size % align != 0)
1385 size = CEIL (size, align) * align;
1386 size += GET_MODE_SIZE (mode);
1387 apply_args_mode[regno] = mode;
1389 else
1391 apply_args_mode[regno] = VOIDmode;
1394 return size;
1397 /* Return the size required for the block returned by __builtin_apply,
1398 and initialize apply_result_mode. */
1400 static int
1401 apply_result_size (void)
1403 static int size = -1;
1404 int align, regno;
1405 machine_mode mode;
1407 /* The values computed by this function never change. */
1408 if (size < 0)
1410 size = 0;
1412 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1413 if (targetm.calls.function_value_regno_p (regno))
1415 mode = targetm.calls.get_raw_result_mode (regno);
1417 gcc_assert (mode != VOIDmode);
1419 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1420 if (size % align != 0)
1421 size = CEIL (size, align) * align;
1422 size += GET_MODE_SIZE (mode);
1423 apply_result_mode[regno] = mode;
1425 else
1426 apply_result_mode[regno] = VOIDmode;
1428 /* Allow targets that use untyped_call and untyped_return to override
1429 the size so that machine-specific information can be stored here. */
1430 #ifdef APPLY_RESULT_SIZE
1431 size = APPLY_RESULT_SIZE;
1432 #endif
1434 return size;
1437 /* Create a vector describing the result block RESULT. If SAVEP is true,
1438 the result block is used to save the values; otherwise it is used to
1439 restore the values. */
1441 static rtx
1442 result_vector (int savep, rtx result)
1444 int regno, size, align, nelts;
1445 machine_mode mode;
1446 rtx reg, mem;
1447 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1449 size = nelts = 0;
1450 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1451 if ((mode = apply_result_mode[regno]) != VOIDmode)
1453 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1454 if (size % align != 0)
1455 size = CEIL (size, align) * align;
1456 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1457 mem = adjust_address (result, mode, size);
1458 savevec[nelts++] = (savep
1459 ? gen_rtx_SET (mem, reg)
1460 : gen_rtx_SET (reg, mem));
1461 size += GET_MODE_SIZE (mode);
1463 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1466 /* Save the state required to perform an untyped call with the same
1467 arguments as were passed to the current function. */
1469 static rtx
1470 expand_builtin_apply_args_1 (void)
1472 rtx registers, tem;
1473 int size, align, regno;
1474 machine_mode mode;
1475 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1477 /* Create a block where the arg-pointer, structure value address,
1478 and argument registers can be saved. */
1479 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1481 /* Walk past the arg-pointer and structure value address. */
1482 size = GET_MODE_SIZE (Pmode);
1483 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1484 size += GET_MODE_SIZE (Pmode);
1486 /* Save each register used in calling a function to the block. */
1487 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1488 if ((mode = apply_args_mode[regno]) != VOIDmode)
1490 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1491 if (size % align != 0)
1492 size = CEIL (size, align) * align;
1494 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1496 emit_move_insn (adjust_address (registers, mode, size), tem);
1497 size += GET_MODE_SIZE (mode);
1500 /* Save the arg pointer to the block. */
1501 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1502 /* We need the pointer as the caller actually passed them to us, not
1503 as we might have pretended they were passed. Make sure it's a valid
1504 operand, as emit_move_insn isn't expected to handle a PLUS. */
1505 if (STACK_GROWS_DOWNWARD)
1507 = force_operand (plus_constant (Pmode, tem,
1508 crtl->args.pretend_args_size),
1509 NULL_RTX);
1510 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1512 size = GET_MODE_SIZE (Pmode);
1514 /* Save the structure value address unless this is passed as an
1515 "invisible" first argument. */
1516 if (struct_incoming_value)
1518 emit_move_insn (adjust_address (registers, Pmode, size),
1519 copy_to_reg (struct_incoming_value));
1520 size += GET_MODE_SIZE (Pmode);
1523 /* Return the address of the block. */
1524 return copy_addr_to_reg (XEXP (registers, 0));
1527 /* __builtin_apply_args returns block of memory allocated on
1528 the stack into which is stored the arg pointer, structure
1529 value address, static chain, and all the registers that might
1530 possibly be used in performing a function call. The code is
1531 moved to the start of the function so the incoming values are
1532 saved. */
1534 static rtx
1535 expand_builtin_apply_args (void)
1537 /* Don't do __builtin_apply_args more than once in a function.
1538 Save the result of the first call and reuse it. */
1539 if (apply_args_value != 0)
1540 return apply_args_value;
1542 /* When this function is called, it means that registers must be
1543 saved on entry to this function. So we migrate the
1544 call to the first insn of this function. */
1545 rtx temp;
1547 start_sequence ();
1548 temp = expand_builtin_apply_args_1 ();
1549 rtx_insn *seq = get_insns ();
1550 end_sequence ();
1552 apply_args_value = temp;
1554 /* Put the insns after the NOTE that starts the function.
1555 If this is inside a start_sequence, make the outer-level insn
1556 chain current, so the code is placed at the start of the
1557 function. If internal_arg_pointer is a non-virtual pseudo,
1558 it needs to be placed after the function that initializes
1559 that pseudo. */
1560 push_topmost_sequence ();
1561 if (REG_P (crtl->args.internal_arg_pointer)
1562 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1563 emit_insn_before (seq, parm_birth_insn);
1564 else
1565 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1566 pop_topmost_sequence ();
1567 return temp;
1571 /* Perform an untyped call and save the state required to perform an
1572 untyped return of whatever value was returned by the given function. */
1574 static rtx
1575 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1577 int size, align, regno;
1578 machine_mode mode;
1579 rtx incoming_args, result, reg, dest, src;
1580 rtx_call_insn *call_insn;
1581 rtx old_stack_level = 0;
1582 rtx call_fusage = 0;
1583 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1585 arguments = convert_memory_address (Pmode, arguments);
1587 /* Create a block where the return registers can be saved. */
1588 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1590 /* Fetch the arg pointer from the ARGUMENTS block. */
1591 incoming_args = gen_reg_rtx (Pmode);
1592 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1593 if (!STACK_GROWS_DOWNWARD)
1594 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1595 incoming_args, 0, OPTAB_LIB_WIDEN);
1597 /* Push a new argument block and copy the arguments. Do not allow
1598 the (potential) memcpy call below to interfere with our stack
1599 manipulations. */
1600 do_pending_stack_adjust ();
1601 NO_DEFER_POP;
1603 /* Save the stack with nonlocal if available. */
1604 if (targetm.have_save_stack_nonlocal ())
1605 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1606 else
1607 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1609 /* Allocate a block of memory onto the stack and copy the memory
1610 arguments to the outgoing arguments address. We can pass TRUE
1611 as the 4th argument because we just saved the stack pointer
1612 and will restore it right after the call. */
1613 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1615 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1616 may have already set current_function_calls_alloca to true.
1617 current_function_calls_alloca won't be set if argsize is zero,
1618 so we have to guarantee need_drap is true here. */
1619 if (SUPPORTS_STACK_ALIGNMENT)
1620 crtl->need_drap = true;
1622 dest = virtual_outgoing_args_rtx;
1623 if (!STACK_GROWS_DOWNWARD)
1625 if (CONST_INT_P (argsize))
1626 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1627 else
1628 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1630 dest = gen_rtx_MEM (BLKmode, dest);
1631 set_mem_align (dest, PARM_BOUNDARY);
1632 src = gen_rtx_MEM (BLKmode, incoming_args);
1633 set_mem_align (src, PARM_BOUNDARY);
1634 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1636 /* Refer to the argument block. */
1637 apply_args_size ();
1638 arguments = gen_rtx_MEM (BLKmode, arguments);
1639 set_mem_align (arguments, PARM_BOUNDARY);
1641 /* Walk past the arg-pointer and structure value address. */
1642 size = GET_MODE_SIZE (Pmode);
1643 if (struct_value)
1644 size += GET_MODE_SIZE (Pmode);
1646 /* Restore each of the registers previously saved. Make USE insns
1647 for each of these registers for use in making the call. */
1648 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1649 if ((mode = apply_args_mode[regno]) != VOIDmode)
1651 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1652 if (size % align != 0)
1653 size = CEIL (size, align) * align;
1654 reg = gen_rtx_REG (mode, regno);
1655 emit_move_insn (reg, adjust_address (arguments, mode, size));
1656 use_reg (&call_fusage, reg);
1657 size += GET_MODE_SIZE (mode);
1660 /* Restore the structure value address unless this is passed as an
1661 "invisible" first argument. */
1662 size = GET_MODE_SIZE (Pmode);
1663 if (struct_value)
1665 rtx value = gen_reg_rtx (Pmode);
1666 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1667 emit_move_insn (struct_value, value);
1668 if (REG_P (struct_value))
1669 use_reg (&call_fusage, struct_value);
1670 size += GET_MODE_SIZE (Pmode);
1673 /* All arguments and registers used for the call are set up by now! */
1674 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1676 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1677 and we don't want to load it into a register as an optimization,
1678 because prepare_call_address already did it if it should be done. */
1679 if (GET_CODE (function) != SYMBOL_REF)
1680 function = memory_address (FUNCTION_MODE, function);
1682 /* Generate the actual call instruction and save the return value. */
1683 if (targetm.have_untyped_call ())
1685 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1686 emit_call_insn (targetm.gen_untyped_call (mem, result,
1687 result_vector (1, result)));
1689 else if (targetm.have_call_value ())
1691 rtx valreg = 0;
1693 /* Locate the unique return register. It is not possible to
1694 express a call that sets more than one return register using
1695 call_value; use untyped_call for that. In fact, untyped_call
1696 only needs to save the return registers in the given block. */
1697 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1698 if ((mode = apply_result_mode[regno]) != VOIDmode)
1700 gcc_assert (!valreg); /* have_untyped_call required. */
1702 valreg = gen_rtx_REG (mode, regno);
1705 emit_insn (targetm.gen_call_value (valreg,
1706 gen_rtx_MEM (FUNCTION_MODE, function),
1707 const0_rtx, NULL_RTX, const0_rtx));
1709 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1711 else
1712 gcc_unreachable ();
1714 /* Find the CALL insn we just emitted, and attach the register usage
1715 information. */
1716 call_insn = last_call_insn ();
1717 add_function_usage_to (call_insn, call_fusage);
1719 /* Restore the stack. */
1720 if (targetm.have_save_stack_nonlocal ())
1721 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1722 else
1723 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1724 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1726 OK_DEFER_POP;
1728 /* Return the address of the result block. */
1729 result = copy_addr_to_reg (XEXP (result, 0));
1730 return convert_memory_address (ptr_mode, result);
1733 /* Perform an untyped return. */
1735 static void
1736 expand_builtin_return (rtx result)
1738 int size, align, regno;
1739 machine_mode mode;
1740 rtx reg;
1741 rtx_insn *call_fusage = 0;
1743 result = convert_memory_address (Pmode, result);
1745 apply_result_size ();
1746 result = gen_rtx_MEM (BLKmode, result);
1748 if (targetm.have_untyped_return ())
1750 rtx vector = result_vector (0, result);
1751 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1752 emit_barrier ();
1753 return;
1756 /* Restore the return value and note that each value is used. */
1757 size = 0;
1758 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1759 if ((mode = apply_result_mode[regno]) != VOIDmode)
1761 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1762 if (size % align != 0)
1763 size = CEIL (size, align) * align;
1764 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1765 emit_move_insn (reg, adjust_address (result, mode, size));
1767 push_to_sequence (call_fusage);
1768 emit_use (reg);
1769 call_fusage = get_insns ();
1770 end_sequence ();
1771 size += GET_MODE_SIZE (mode);
1774 /* Put the USE insns before the return. */
1775 emit_insn (call_fusage);
1777 /* Return whatever values was restored by jumping directly to the end
1778 of the function. */
1779 expand_naked_return ();
1782 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1784 static enum type_class
1785 type_to_class (tree type)
1787 switch (TREE_CODE (type))
1789 case VOID_TYPE: return void_type_class;
1790 case INTEGER_TYPE: return integer_type_class;
1791 case ENUMERAL_TYPE: return enumeral_type_class;
1792 case BOOLEAN_TYPE: return boolean_type_class;
1793 case POINTER_TYPE: return pointer_type_class;
1794 case REFERENCE_TYPE: return reference_type_class;
1795 case OFFSET_TYPE: return offset_type_class;
1796 case REAL_TYPE: return real_type_class;
1797 case COMPLEX_TYPE: return complex_type_class;
1798 case FUNCTION_TYPE: return function_type_class;
1799 case METHOD_TYPE: return method_type_class;
1800 case RECORD_TYPE: return record_type_class;
1801 case UNION_TYPE:
1802 case QUAL_UNION_TYPE: return union_type_class;
1803 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1804 ? string_type_class : array_type_class);
1805 case LANG_TYPE: return lang_type_class;
1806 default: return no_type_class;
1810 /* Expand a call EXP to __builtin_classify_type. */
1812 static rtx
1813 expand_builtin_classify_type (tree exp)
1815 if (call_expr_nargs (exp))
1816 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1817 return GEN_INT (no_type_class);
1820 /* This helper macro, meant to be used in mathfn_built_in below,
1821 determines which among a set of three builtin math functions is
1822 appropriate for a given type mode. The `F' and `L' cases are
1823 automatically generated from the `double' case. */
1824 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1825 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1826 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1827 fcodel = BUILT_IN_MATHFN##L ; break;
1828 /* Similar to above, but appends _R after any F/L suffix. */
1829 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1830 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1831 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1832 fcodel = BUILT_IN_MATHFN##L_R ; break;
1834 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1835 if available. If IMPLICIT is true use the implicit builtin declaration,
1836 otherwise use the explicit declaration. If we can't do the conversion,
1837 return zero. */
1839 static tree
1840 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1842 enum built_in_function fcode, fcodef, fcodel, fcode2;
1844 switch (fn)
1846 CASE_MATHFN (BUILT_IN_ACOS)
1847 CASE_MATHFN (BUILT_IN_ACOSH)
1848 CASE_MATHFN (BUILT_IN_ASIN)
1849 CASE_MATHFN (BUILT_IN_ASINH)
1850 CASE_MATHFN (BUILT_IN_ATAN)
1851 CASE_MATHFN (BUILT_IN_ATAN2)
1852 CASE_MATHFN (BUILT_IN_ATANH)
1853 CASE_MATHFN (BUILT_IN_CBRT)
1854 CASE_MATHFN (BUILT_IN_CEIL)
1855 CASE_MATHFN (BUILT_IN_CEXPI)
1856 CASE_MATHFN (BUILT_IN_COPYSIGN)
1857 CASE_MATHFN (BUILT_IN_COS)
1858 CASE_MATHFN (BUILT_IN_COSH)
1859 CASE_MATHFN (BUILT_IN_DREM)
1860 CASE_MATHFN (BUILT_IN_ERF)
1861 CASE_MATHFN (BUILT_IN_ERFC)
1862 CASE_MATHFN (BUILT_IN_EXP)
1863 CASE_MATHFN (BUILT_IN_EXP10)
1864 CASE_MATHFN (BUILT_IN_EXP2)
1865 CASE_MATHFN (BUILT_IN_EXPM1)
1866 CASE_MATHFN (BUILT_IN_FABS)
1867 CASE_MATHFN (BUILT_IN_FDIM)
1868 CASE_MATHFN (BUILT_IN_FLOOR)
1869 CASE_MATHFN (BUILT_IN_FMA)
1870 CASE_MATHFN (BUILT_IN_FMAX)
1871 CASE_MATHFN (BUILT_IN_FMIN)
1872 CASE_MATHFN (BUILT_IN_FMOD)
1873 CASE_MATHFN (BUILT_IN_FREXP)
1874 CASE_MATHFN (BUILT_IN_GAMMA)
1875 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1876 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1877 CASE_MATHFN (BUILT_IN_HYPOT)
1878 CASE_MATHFN (BUILT_IN_ILOGB)
1879 CASE_MATHFN (BUILT_IN_ICEIL)
1880 CASE_MATHFN (BUILT_IN_IFLOOR)
1881 CASE_MATHFN (BUILT_IN_INF)
1882 CASE_MATHFN (BUILT_IN_IRINT)
1883 CASE_MATHFN (BUILT_IN_IROUND)
1884 CASE_MATHFN (BUILT_IN_ISINF)
1885 CASE_MATHFN (BUILT_IN_J0)
1886 CASE_MATHFN (BUILT_IN_J1)
1887 CASE_MATHFN (BUILT_IN_JN)
1888 CASE_MATHFN (BUILT_IN_LCEIL)
1889 CASE_MATHFN (BUILT_IN_LDEXP)
1890 CASE_MATHFN (BUILT_IN_LFLOOR)
1891 CASE_MATHFN (BUILT_IN_LGAMMA)
1892 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1893 CASE_MATHFN (BUILT_IN_LLCEIL)
1894 CASE_MATHFN (BUILT_IN_LLFLOOR)
1895 CASE_MATHFN (BUILT_IN_LLRINT)
1896 CASE_MATHFN (BUILT_IN_LLROUND)
1897 CASE_MATHFN (BUILT_IN_LOG)
1898 CASE_MATHFN (BUILT_IN_LOG10)
1899 CASE_MATHFN (BUILT_IN_LOG1P)
1900 CASE_MATHFN (BUILT_IN_LOG2)
1901 CASE_MATHFN (BUILT_IN_LOGB)
1902 CASE_MATHFN (BUILT_IN_LRINT)
1903 CASE_MATHFN (BUILT_IN_LROUND)
1904 CASE_MATHFN (BUILT_IN_MODF)
1905 CASE_MATHFN (BUILT_IN_NAN)
1906 CASE_MATHFN (BUILT_IN_NANS)
1907 CASE_MATHFN (BUILT_IN_NEARBYINT)
1908 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1909 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1910 CASE_MATHFN (BUILT_IN_POW)
1911 CASE_MATHFN (BUILT_IN_POWI)
1912 CASE_MATHFN (BUILT_IN_POW10)
1913 CASE_MATHFN (BUILT_IN_REMAINDER)
1914 CASE_MATHFN (BUILT_IN_REMQUO)
1915 CASE_MATHFN (BUILT_IN_RINT)
1916 CASE_MATHFN (BUILT_IN_ROUND)
1917 CASE_MATHFN (BUILT_IN_SCALB)
1918 CASE_MATHFN (BUILT_IN_SCALBLN)
1919 CASE_MATHFN (BUILT_IN_SCALBN)
1920 CASE_MATHFN (BUILT_IN_SIGNBIT)
1921 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1922 CASE_MATHFN (BUILT_IN_SIN)
1923 CASE_MATHFN (BUILT_IN_SINCOS)
1924 CASE_MATHFN (BUILT_IN_SINH)
1925 CASE_MATHFN (BUILT_IN_SQRT)
1926 CASE_MATHFN (BUILT_IN_TAN)
1927 CASE_MATHFN (BUILT_IN_TANH)
1928 CASE_MATHFN (BUILT_IN_TGAMMA)
1929 CASE_MATHFN (BUILT_IN_TRUNC)
1930 CASE_MATHFN (BUILT_IN_Y0)
1931 CASE_MATHFN (BUILT_IN_Y1)
1932 CASE_MATHFN (BUILT_IN_YN)
1934 default:
1935 return NULL_TREE;
1938 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1939 fcode2 = fcode;
1940 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1941 fcode2 = fcodef;
1942 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1943 fcode2 = fcodel;
1944 else
1945 return NULL_TREE;
1947 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1948 return NULL_TREE;
1950 return builtin_decl_explicit (fcode2);
1953 /* Like mathfn_built_in_1(), but always use the implicit array. */
1955 tree
1956 mathfn_built_in (tree type, enum built_in_function fn)
1958 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1961 /* If errno must be maintained, expand the RTL to check if the result,
1962 TARGET, of a built-in function call, EXP, is NaN, and if so set
1963 errno to EDOM. */
1965 static void
1966 expand_errno_check (tree exp, rtx target)
1968 rtx_code_label *lab = gen_label_rtx ();
1970 /* Test the result; if it is NaN, set errno=EDOM because
1971 the argument was not in the domain. */
1972 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1973 NULL_RTX, NULL, lab,
1974 /* The jump is very likely. */
1975 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1977 #ifdef TARGET_EDOM
1978 /* If this built-in doesn't throw an exception, set errno directly. */
1979 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1981 #ifdef GEN_ERRNO_RTX
1982 rtx errno_rtx = GEN_ERRNO_RTX;
1983 #else
1984 rtx errno_rtx
1985 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1986 #endif
1987 emit_move_insn (errno_rtx,
1988 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
1989 emit_label (lab);
1990 return;
1992 #endif
1994 /* Make sure the library call isn't expanded as a tail call. */
1995 CALL_EXPR_TAILCALL (exp) = 0;
1997 /* We can't set errno=EDOM directly; let the library call do it.
1998 Pop the arguments right away in case the call gets deleted. */
1999 NO_DEFER_POP;
2000 expand_call (exp, target, 0);
2001 OK_DEFER_POP;
2002 emit_label (lab);
2005 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2006 Return NULL_RTX if a normal call should be emitted rather than expanding
2007 the function in-line. EXP is the expression that is a call to the builtin
2008 function; if convenient, the result should be placed in TARGET.
2009 SUBTARGET may be used as the target for computing one of EXP's operands. */
2011 static rtx
2012 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2014 optab builtin_optab;
2015 rtx op0;
2016 rtx_insn *insns;
2017 tree fndecl = get_callee_fndecl (exp);
2018 machine_mode mode;
2019 bool errno_set = false;
2020 bool try_widening = false;
2021 tree arg;
2023 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2024 return NULL_RTX;
2026 arg = CALL_EXPR_ARG (exp, 0);
2028 switch (DECL_FUNCTION_CODE (fndecl))
2030 CASE_FLT_FN (BUILT_IN_SQRT):
2031 errno_set = ! tree_expr_nonnegative_p (arg);
2032 try_widening = true;
2033 builtin_optab = sqrt_optab;
2034 break;
2035 CASE_FLT_FN (BUILT_IN_EXP):
2036 errno_set = true; builtin_optab = exp_optab; break;
2037 CASE_FLT_FN (BUILT_IN_EXP10):
2038 CASE_FLT_FN (BUILT_IN_POW10):
2039 errno_set = true; builtin_optab = exp10_optab; break;
2040 CASE_FLT_FN (BUILT_IN_EXP2):
2041 errno_set = true; builtin_optab = exp2_optab; break;
2042 CASE_FLT_FN (BUILT_IN_EXPM1):
2043 errno_set = true; builtin_optab = expm1_optab; break;
2044 CASE_FLT_FN (BUILT_IN_LOGB):
2045 errno_set = true; builtin_optab = logb_optab; break;
2046 CASE_FLT_FN (BUILT_IN_LOG):
2047 errno_set = true; builtin_optab = log_optab; break;
2048 CASE_FLT_FN (BUILT_IN_LOG10):
2049 errno_set = true; builtin_optab = log10_optab; break;
2050 CASE_FLT_FN (BUILT_IN_LOG2):
2051 errno_set = true; builtin_optab = log2_optab; break;
2052 CASE_FLT_FN (BUILT_IN_LOG1P):
2053 errno_set = true; builtin_optab = log1p_optab; break;
2054 CASE_FLT_FN (BUILT_IN_ASIN):
2055 builtin_optab = asin_optab; break;
2056 CASE_FLT_FN (BUILT_IN_ACOS):
2057 builtin_optab = acos_optab; break;
2058 CASE_FLT_FN (BUILT_IN_TAN):
2059 builtin_optab = tan_optab; break;
2060 CASE_FLT_FN (BUILT_IN_ATAN):
2061 builtin_optab = atan_optab; break;
2062 CASE_FLT_FN (BUILT_IN_FLOOR):
2063 builtin_optab = floor_optab; break;
2064 CASE_FLT_FN (BUILT_IN_CEIL):
2065 builtin_optab = ceil_optab; break;
2066 CASE_FLT_FN (BUILT_IN_TRUNC):
2067 builtin_optab = btrunc_optab; break;
2068 CASE_FLT_FN (BUILT_IN_ROUND):
2069 builtin_optab = round_optab; break;
2070 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2071 builtin_optab = nearbyint_optab;
2072 if (flag_trapping_math)
2073 break;
2074 /* Else fallthrough and expand as rint. */
2075 CASE_FLT_FN (BUILT_IN_RINT):
2076 builtin_optab = rint_optab; break;
2077 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2078 builtin_optab = significand_optab; break;
2079 default:
2080 gcc_unreachable ();
2083 /* Make a suitable register to place result in. */
2084 mode = TYPE_MODE (TREE_TYPE (exp));
2086 if (! flag_errno_math || ! HONOR_NANS (mode))
2087 errno_set = false;
2089 /* Before working hard, check whether the instruction is available, but try
2090 to widen the mode for specific operations. */
2091 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2092 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2093 && (!errno_set || !optimize_insn_for_size_p ()))
2095 rtx result = gen_reg_rtx (mode);
2097 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2098 need to expand the argument again. This way, we will not perform
2099 side-effects more the once. */
2100 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2102 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2104 start_sequence ();
2106 /* Compute into RESULT.
2107 Set RESULT to wherever the result comes back. */
2108 result = expand_unop (mode, builtin_optab, op0, result, 0);
2110 if (result != 0)
2112 if (errno_set)
2113 expand_errno_check (exp, result);
2115 /* Output the entire sequence. */
2116 insns = get_insns ();
2117 end_sequence ();
2118 emit_insn (insns);
2119 return result;
2122 /* If we were unable to expand via the builtin, stop the sequence
2123 (without outputting the insns) and call to the library function
2124 with the stabilized argument list. */
2125 end_sequence ();
2128 return expand_call (exp, target, target == const0_rtx);
2131 /* Expand a call to the builtin binary math functions (pow and atan2).
2132 Return NULL_RTX if a normal call should be emitted rather than expanding the
2133 function in-line. EXP is the expression that is a call to the builtin
2134 function; if convenient, the result should be placed in TARGET.
2135 SUBTARGET may be used as the target for computing one of EXP's
2136 operands. */
2138 static rtx
2139 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2141 optab builtin_optab;
2142 rtx op0, op1, result;
2143 rtx_insn *insns;
2144 int op1_type = REAL_TYPE;
2145 tree fndecl = get_callee_fndecl (exp);
2146 tree arg0, arg1;
2147 machine_mode mode;
2148 bool errno_set = true;
2150 switch (DECL_FUNCTION_CODE (fndecl))
2152 CASE_FLT_FN (BUILT_IN_SCALBN):
2153 CASE_FLT_FN (BUILT_IN_SCALBLN):
2154 CASE_FLT_FN (BUILT_IN_LDEXP):
2155 op1_type = INTEGER_TYPE;
2156 default:
2157 break;
2160 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2161 return NULL_RTX;
2163 arg0 = CALL_EXPR_ARG (exp, 0);
2164 arg1 = CALL_EXPR_ARG (exp, 1);
2166 switch (DECL_FUNCTION_CODE (fndecl))
2168 CASE_FLT_FN (BUILT_IN_POW):
2169 builtin_optab = pow_optab; break;
2170 CASE_FLT_FN (BUILT_IN_ATAN2):
2171 builtin_optab = atan2_optab; break;
2172 CASE_FLT_FN (BUILT_IN_SCALB):
2173 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2174 return 0;
2175 builtin_optab = scalb_optab; break;
2176 CASE_FLT_FN (BUILT_IN_SCALBN):
2177 CASE_FLT_FN (BUILT_IN_SCALBLN):
2178 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2179 return 0;
2180 /* Fall through... */
2181 CASE_FLT_FN (BUILT_IN_LDEXP):
2182 builtin_optab = ldexp_optab; break;
2183 CASE_FLT_FN (BUILT_IN_FMOD):
2184 builtin_optab = fmod_optab; break;
2185 CASE_FLT_FN (BUILT_IN_REMAINDER):
2186 CASE_FLT_FN (BUILT_IN_DREM):
2187 builtin_optab = remainder_optab; break;
2188 default:
2189 gcc_unreachable ();
2192 /* Make a suitable register to place result in. */
2193 mode = TYPE_MODE (TREE_TYPE (exp));
2195 /* Before working hard, check whether the instruction is available. */
2196 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2197 return NULL_RTX;
2199 result = gen_reg_rtx (mode);
2201 if (! flag_errno_math || ! HONOR_NANS (mode))
2202 errno_set = false;
2204 if (errno_set && optimize_insn_for_size_p ())
2205 return 0;
2207 /* Always stabilize the argument list. */
2208 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2209 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2211 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2212 op1 = expand_normal (arg1);
2214 start_sequence ();
2216 /* Compute into RESULT.
2217 Set RESULT to wherever the result comes back. */
2218 result = expand_binop (mode, builtin_optab, op0, op1,
2219 result, 0, OPTAB_DIRECT);
2221 /* If we were unable to expand via the builtin, stop the sequence
2222 (without outputting the insns) and call to the library function
2223 with the stabilized argument list. */
2224 if (result == 0)
2226 end_sequence ();
2227 return expand_call (exp, target, target == const0_rtx);
2230 if (errno_set)
2231 expand_errno_check (exp, result);
2233 /* Output the entire sequence. */
2234 insns = get_insns ();
2235 end_sequence ();
2236 emit_insn (insns);
2238 return result;
2241 /* Expand a call to the builtin trinary math functions (fma).
2242 Return NULL_RTX if a normal call should be emitted rather than expanding the
2243 function in-line. EXP is the expression that is a call to the builtin
2244 function; if convenient, the result should be placed in TARGET.
2245 SUBTARGET may be used as the target for computing one of EXP's
2246 operands. */
2248 static rtx
2249 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2251 optab builtin_optab;
2252 rtx op0, op1, op2, result;
2253 rtx_insn *insns;
2254 tree fndecl = get_callee_fndecl (exp);
2255 tree arg0, arg1, arg2;
2256 machine_mode mode;
2258 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2259 return NULL_RTX;
2261 arg0 = CALL_EXPR_ARG (exp, 0);
2262 arg1 = CALL_EXPR_ARG (exp, 1);
2263 arg2 = CALL_EXPR_ARG (exp, 2);
2265 switch (DECL_FUNCTION_CODE (fndecl))
2267 CASE_FLT_FN (BUILT_IN_FMA):
2268 builtin_optab = fma_optab; break;
2269 default:
2270 gcc_unreachable ();
2273 /* Make a suitable register to place result in. */
2274 mode = TYPE_MODE (TREE_TYPE (exp));
2276 /* Before working hard, check whether the instruction is available. */
2277 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2278 return NULL_RTX;
2280 result = gen_reg_rtx (mode);
2282 /* Always stabilize the argument list. */
2283 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2284 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2285 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2287 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2288 op1 = expand_normal (arg1);
2289 op2 = expand_normal (arg2);
2291 start_sequence ();
2293 /* Compute into RESULT.
2294 Set RESULT to wherever the result comes back. */
2295 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2296 result, 0);
2298 /* If we were unable to expand via the builtin, stop the sequence
2299 (without outputting the insns) and call to the library function
2300 with the stabilized argument list. */
2301 if (result == 0)
2303 end_sequence ();
2304 return expand_call (exp, target, target == const0_rtx);
2307 /* Output the entire sequence. */
2308 insns = get_insns ();
2309 end_sequence ();
2310 emit_insn (insns);
2312 return result;
2315 /* Expand a call to the builtin sin and cos math functions.
2316 Return NULL_RTX if a normal call should be emitted rather than expanding the
2317 function in-line. EXP is the expression that is a call to the builtin
2318 function; if convenient, the result should be placed in TARGET.
2319 SUBTARGET may be used as the target for computing one of EXP's
2320 operands. */
2322 static rtx
2323 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2325 optab builtin_optab;
2326 rtx op0;
2327 rtx_insn *insns;
2328 tree fndecl = get_callee_fndecl (exp);
2329 machine_mode mode;
2330 tree arg;
2332 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2333 return NULL_RTX;
2335 arg = CALL_EXPR_ARG (exp, 0);
2337 switch (DECL_FUNCTION_CODE (fndecl))
2339 CASE_FLT_FN (BUILT_IN_SIN):
2340 CASE_FLT_FN (BUILT_IN_COS):
2341 builtin_optab = sincos_optab; break;
2342 default:
2343 gcc_unreachable ();
2346 /* Make a suitable register to place result in. */
2347 mode = TYPE_MODE (TREE_TYPE (exp));
2349 /* Check if sincos insn is available, otherwise fallback
2350 to sin or cos insn. */
2351 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2352 switch (DECL_FUNCTION_CODE (fndecl))
2354 CASE_FLT_FN (BUILT_IN_SIN):
2355 builtin_optab = sin_optab; break;
2356 CASE_FLT_FN (BUILT_IN_COS):
2357 builtin_optab = cos_optab; break;
2358 default:
2359 gcc_unreachable ();
2362 /* Before working hard, check whether the instruction is available. */
2363 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2365 rtx result = gen_reg_rtx (mode);
2367 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2368 need to expand the argument again. This way, we will not perform
2369 side-effects more the once. */
2370 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2372 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2374 start_sequence ();
2376 /* Compute into RESULT.
2377 Set RESULT to wherever the result comes back. */
2378 if (builtin_optab == sincos_optab)
2380 int ok;
2382 switch (DECL_FUNCTION_CODE (fndecl))
2384 CASE_FLT_FN (BUILT_IN_SIN):
2385 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2386 break;
2387 CASE_FLT_FN (BUILT_IN_COS):
2388 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2389 break;
2390 default:
2391 gcc_unreachable ();
2393 gcc_assert (ok);
2395 else
2396 result = expand_unop (mode, builtin_optab, op0, result, 0);
2398 if (result != 0)
2400 /* Output the entire sequence. */
2401 insns = get_insns ();
2402 end_sequence ();
2403 emit_insn (insns);
2404 return result;
2407 /* If we were unable to expand via the builtin, stop the sequence
2408 (without outputting the insns) and call to the library function
2409 with the stabilized argument list. */
2410 end_sequence ();
2413 return expand_call (exp, target, target == const0_rtx);
2416 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2417 return an RTL instruction code that implements the functionality.
2418 If that isn't possible or available return CODE_FOR_nothing. */
2420 static enum insn_code
2421 interclass_mathfn_icode (tree arg, tree fndecl)
2423 bool errno_set = false;
2424 optab builtin_optab = unknown_optab;
2425 machine_mode mode;
2427 switch (DECL_FUNCTION_CODE (fndecl))
2429 CASE_FLT_FN (BUILT_IN_ILOGB):
2430 errno_set = true; builtin_optab = ilogb_optab; break;
2431 CASE_FLT_FN (BUILT_IN_ISINF):
2432 builtin_optab = isinf_optab; break;
2433 case BUILT_IN_ISNORMAL:
2434 case BUILT_IN_ISFINITE:
2435 CASE_FLT_FN (BUILT_IN_FINITE):
2436 case BUILT_IN_FINITED32:
2437 case BUILT_IN_FINITED64:
2438 case BUILT_IN_FINITED128:
2439 case BUILT_IN_ISINFD32:
2440 case BUILT_IN_ISINFD64:
2441 case BUILT_IN_ISINFD128:
2442 /* These builtins have no optabs (yet). */
2443 break;
2444 default:
2445 gcc_unreachable ();
2448 /* There's no easy way to detect the case we need to set EDOM. */
2449 if (flag_errno_math && errno_set)
2450 return CODE_FOR_nothing;
2452 /* Optab mode depends on the mode of the input argument. */
2453 mode = TYPE_MODE (TREE_TYPE (arg));
2455 if (builtin_optab)
2456 return optab_handler (builtin_optab, mode);
2457 return CODE_FOR_nothing;
2460 /* Expand a call to one of the builtin math functions that operate on
2461 floating point argument and output an integer result (ilogb, isinf,
2462 isnan, etc).
2463 Return 0 if a normal call should be emitted rather than expanding the
2464 function in-line. EXP is the expression that is a call to the builtin
2465 function; if convenient, the result should be placed in TARGET. */
2467 static rtx
2468 expand_builtin_interclass_mathfn (tree exp, rtx target)
2470 enum insn_code icode = CODE_FOR_nothing;
2471 rtx op0;
2472 tree fndecl = get_callee_fndecl (exp);
2473 machine_mode mode;
2474 tree arg;
2476 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2477 return NULL_RTX;
2479 arg = CALL_EXPR_ARG (exp, 0);
2480 icode = interclass_mathfn_icode (arg, fndecl);
2481 mode = TYPE_MODE (TREE_TYPE (arg));
2483 if (icode != CODE_FOR_nothing)
2485 struct expand_operand ops[1];
2486 rtx_insn *last = get_last_insn ();
2487 tree orig_arg = arg;
2489 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2490 need to expand the argument again. This way, we will not perform
2491 side-effects more the once. */
2492 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2494 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2496 if (mode != GET_MODE (op0))
2497 op0 = convert_to_mode (mode, op0, 0);
2499 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2500 if (maybe_legitimize_operands (icode, 0, 1, ops)
2501 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2502 return ops[0].value;
2504 delete_insns_since (last);
2505 CALL_EXPR_ARG (exp, 0) = orig_arg;
2508 return NULL_RTX;
2511 /* Expand a call to the builtin sincos math function.
2512 Return NULL_RTX if a normal call should be emitted rather than expanding the
2513 function in-line. EXP is the expression that is a call to the builtin
2514 function. */
2516 static rtx
2517 expand_builtin_sincos (tree exp)
2519 rtx op0, op1, op2, target1, target2;
2520 machine_mode mode;
2521 tree arg, sinp, cosp;
2522 int result;
2523 location_t loc = EXPR_LOCATION (exp);
2524 tree alias_type, alias_off;
2526 if (!validate_arglist (exp, REAL_TYPE,
2527 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2528 return NULL_RTX;
2530 arg = CALL_EXPR_ARG (exp, 0);
2531 sinp = CALL_EXPR_ARG (exp, 1);
2532 cosp = CALL_EXPR_ARG (exp, 2);
2534 /* Make a suitable register to place result in. */
2535 mode = TYPE_MODE (TREE_TYPE (arg));
2537 /* Check if sincos insn is available, otherwise emit the call. */
2538 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2539 return NULL_RTX;
2541 target1 = gen_reg_rtx (mode);
2542 target2 = gen_reg_rtx (mode);
2544 op0 = expand_normal (arg);
2545 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2546 alias_off = build_int_cst (alias_type, 0);
2547 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2548 sinp, alias_off));
2549 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2550 cosp, alias_off));
2552 /* Compute into target1 and target2.
2553 Set TARGET to wherever the result comes back. */
2554 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2555 gcc_assert (result);
2557 /* Move target1 and target2 to the memory locations indicated
2558 by op1 and op2. */
2559 emit_move_insn (op1, target1);
2560 emit_move_insn (op2, target2);
2562 return const0_rtx;
2565 /* Expand a call to the internal cexpi builtin to the sincos math function.
2566 EXP is the expression that is a call to the builtin function; if convenient,
2567 the result should be placed in TARGET. */
2569 static rtx
2570 expand_builtin_cexpi (tree exp, rtx target)
2572 tree fndecl = get_callee_fndecl (exp);
2573 tree arg, type;
2574 machine_mode mode;
2575 rtx op0, op1, op2;
2576 location_t loc = EXPR_LOCATION (exp);
2578 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2579 return NULL_RTX;
2581 arg = CALL_EXPR_ARG (exp, 0);
2582 type = TREE_TYPE (arg);
2583 mode = TYPE_MODE (TREE_TYPE (arg));
2585 /* Try expanding via a sincos optab, fall back to emitting a libcall
2586 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2587 is only generated from sincos, cexp or if we have either of them. */
2588 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2590 op1 = gen_reg_rtx (mode);
2591 op2 = gen_reg_rtx (mode);
2593 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2595 /* Compute into op1 and op2. */
2596 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2598 else if (targetm.libc_has_function (function_sincos))
2600 tree call, fn = NULL_TREE;
2601 tree top1, top2;
2602 rtx op1a, op2a;
2604 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2605 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2606 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2607 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2608 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2609 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2610 else
2611 gcc_unreachable ();
2613 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2614 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2615 op1a = copy_addr_to_reg (XEXP (op1, 0));
2616 op2a = copy_addr_to_reg (XEXP (op2, 0));
2617 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2618 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2620 /* Make sure not to fold the sincos call again. */
2621 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2622 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2623 call, 3, arg, top1, top2));
2625 else
2627 tree call, fn = NULL_TREE, narg;
2628 tree ctype = build_complex_type (type);
2630 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2631 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2632 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2633 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2634 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2635 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2636 else
2637 gcc_unreachable ();
2639 /* If we don't have a decl for cexp create one. This is the
2640 friendliest fallback if the user calls __builtin_cexpi
2641 without full target C99 function support. */
2642 if (fn == NULL_TREE)
2644 tree fntype;
2645 const char *name = NULL;
2647 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2648 name = "cexpf";
2649 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2650 name = "cexp";
2651 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2652 name = "cexpl";
2654 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2655 fn = build_fn_decl (name, fntype);
2658 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2659 build_real (type, dconst0), arg);
2661 /* Make sure not to fold the cexp call again. */
2662 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2663 return expand_expr (build_call_nary (ctype, call, 1, narg),
2664 target, VOIDmode, EXPAND_NORMAL);
2667 /* Now build the proper return type. */
2668 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2669 make_tree (TREE_TYPE (arg), op2),
2670 make_tree (TREE_TYPE (arg), op1)),
2671 target, VOIDmode, EXPAND_NORMAL);
2674 /* Conveniently construct a function call expression. FNDECL names the
2675 function to be called, N is the number of arguments, and the "..."
2676 parameters are the argument expressions. Unlike build_call_exr
2677 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2679 static tree
2680 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2682 va_list ap;
2683 tree fntype = TREE_TYPE (fndecl);
2684 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2686 va_start (ap, n);
2687 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2688 va_end (ap);
2689 SET_EXPR_LOCATION (fn, loc);
2690 return fn;
2693 /* Expand a call to one of the builtin rounding functions gcc defines
2694 as an extension (lfloor and lceil). As these are gcc extensions we
2695 do not need to worry about setting errno to EDOM.
2696 If expanding via optab fails, lower expression to (int)(floor(x)).
2697 EXP is the expression that is a call to the builtin function;
2698 if convenient, the result should be placed in TARGET. */
2700 static rtx
2701 expand_builtin_int_roundingfn (tree exp, rtx target)
2703 convert_optab builtin_optab;
2704 rtx op0, tmp;
2705 rtx_insn *insns;
2706 tree fndecl = get_callee_fndecl (exp);
2707 enum built_in_function fallback_fn;
2708 tree fallback_fndecl;
2709 machine_mode mode;
2710 tree arg;
2712 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2713 gcc_unreachable ();
2715 arg = CALL_EXPR_ARG (exp, 0);
2717 switch (DECL_FUNCTION_CODE (fndecl))
2719 CASE_FLT_FN (BUILT_IN_ICEIL):
2720 CASE_FLT_FN (BUILT_IN_LCEIL):
2721 CASE_FLT_FN (BUILT_IN_LLCEIL):
2722 builtin_optab = lceil_optab;
2723 fallback_fn = BUILT_IN_CEIL;
2724 break;
2726 CASE_FLT_FN (BUILT_IN_IFLOOR):
2727 CASE_FLT_FN (BUILT_IN_LFLOOR):
2728 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2729 builtin_optab = lfloor_optab;
2730 fallback_fn = BUILT_IN_FLOOR;
2731 break;
2733 default:
2734 gcc_unreachable ();
2737 /* Make a suitable register to place result in. */
2738 mode = TYPE_MODE (TREE_TYPE (exp));
2740 target = gen_reg_rtx (mode);
2742 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2743 need to expand the argument again. This way, we will not perform
2744 side-effects more the once. */
2745 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2747 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2749 start_sequence ();
2751 /* Compute into TARGET. */
2752 if (expand_sfix_optab (target, op0, builtin_optab))
2754 /* Output the entire sequence. */
2755 insns = get_insns ();
2756 end_sequence ();
2757 emit_insn (insns);
2758 return target;
2761 /* If we were unable to expand via the builtin, stop the sequence
2762 (without outputting the insns). */
2763 end_sequence ();
2765 /* Fall back to floating point rounding optab. */
2766 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2768 /* For non-C99 targets we may end up without a fallback fndecl here
2769 if the user called __builtin_lfloor directly. In this case emit
2770 a call to the floor/ceil variants nevertheless. This should result
2771 in the best user experience for not full C99 targets. */
2772 if (fallback_fndecl == NULL_TREE)
2774 tree fntype;
2775 const char *name = NULL;
2777 switch (DECL_FUNCTION_CODE (fndecl))
2779 case BUILT_IN_ICEIL:
2780 case BUILT_IN_LCEIL:
2781 case BUILT_IN_LLCEIL:
2782 name = "ceil";
2783 break;
2784 case BUILT_IN_ICEILF:
2785 case BUILT_IN_LCEILF:
2786 case BUILT_IN_LLCEILF:
2787 name = "ceilf";
2788 break;
2789 case BUILT_IN_ICEILL:
2790 case BUILT_IN_LCEILL:
2791 case BUILT_IN_LLCEILL:
2792 name = "ceill";
2793 break;
2794 case BUILT_IN_IFLOOR:
2795 case BUILT_IN_LFLOOR:
2796 case BUILT_IN_LLFLOOR:
2797 name = "floor";
2798 break;
2799 case BUILT_IN_IFLOORF:
2800 case BUILT_IN_LFLOORF:
2801 case BUILT_IN_LLFLOORF:
2802 name = "floorf";
2803 break;
2804 case BUILT_IN_IFLOORL:
2805 case BUILT_IN_LFLOORL:
2806 case BUILT_IN_LLFLOORL:
2807 name = "floorl";
2808 break;
2809 default:
2810 gcc_unreachable ();
2813 fntype = build_function_type_list (TREE_TYPE (arg),
2814 TREE_TYPE (arg), NULL_TREE);
2815 fallback_fndecl = build_fn_decl (name, fntype);
2818 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2820 tmp = expand_normal (exp);
2821 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2823 /* Truncate the result of floating point optab to integer
2824 via expand_fix (). */
2825 target = gen_reg_rtx (mode);
2826 expand_fix (target, tmp, 0);
2828 return target;
2831 /* Expand a call to one of the builtin math functions doing integer
2832 conversion (lrint).
2833 Return 0 if a normal call should be emitted rather than expanding the
2834 function in-line. EXP is the expression that is a call to the builtin
2835 function; if convenient, the result should be placed in TARGET. */
2837 static rtx
2838 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2840 convert_optab builtin_optab;
2841 rtx op0;
2842 rtx_insn *insns;
2843 tree fndecl = get_callee_fndecl (exp);
2844 tree arg;
2845 machine_mode mode;
2846 enum built_in_function fallback_fn = BUILT_IN_NONE;
2848 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2849 gcc_unreachable ();
2851 arg = CALL_EXPR_ARG (exp, 0);
2853 switch (DECL_FUNCTION_CODE (fndecl))
2855 CASE_FLT_FN (BUILT_IN_IRINT):
2856 fallback_fn = BUILT_IN_LRINT;
2857 /* FALLTHRU */
2858 CASE_FLT_FN (BUILT_IN_LRINT):
2859 CASE_FLT_FN (BUILT_IN_LLRINT):
2860 builtin_optab = lrint_optab;
2861 break;
2863 CASE_FLT_FN (BUILT_IN_IROUND):
2864 fallback_fn = BUILT_IN_LROUND;
2865 /* FALLTHRU */
2866 CASE_FLT_FN (BUILT_IN_LROUND):
2867 CASE_FLT_FN (BUILT_IN_LLROUND):
2868 builtin_optab = lround_optab;
2869 break;
2871 default:
2872 gcc_unreachable ();
2875 /* There's no easy way to detect the case we need to set EDOM. */
2876 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2877 return NULL_RTX;
2879 /* Make a suitable register to place result in. */
2880 mode = TYPE_MODE (TREE_TYPE (exp));
2882 /* There's no easy way to detect the case we need to set EDOM. */
2883 if (!flag_errno_math)
2885 rtx result = gen_reg_rtx (mode);
2887 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2888 need to expand the argument again. This way, we will not perform
2889 side-effects more the once. */
2890 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2892 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2894 start_sequence ();
2896 if (expand_sfix_optab (result, op0, builtin_optab))
2898 /* Output the entire sequence. */
2899 insns = get_insns ();
2900 end_sequence ();
2901 emit_insn (insns);
2902 return result;
2905 /* If we were unable to expand via the builtin, stop the sequence
2906 (without outputting the insns) and call to the library function
2907 with the stabilized argument list. */
2908 end_sequence ();
2911 if (fallback_fn != BUILT_IN_NONE)
2913 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2914 targets, (int) round (x) should never be transformed into
2915 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2916 a call to lround in the hope that the target provides at least some
2917 C99 functions. This should result in the best user experience for
2918 not full C99 targets. */
2919 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2920 fallback_fn, 0);
2922 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2923 fallback_fndecl, 1, arg);
2925 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2926 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2927 return convert_to_mode (mode, target, 0);
2930 return expand_call (exp, target, target == const0_rtx);
2933 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2934 a normal call should be emitted rather than expanding the function
2935 in-line. EXP is the expression that is a call to the builtin
2936 function; if convenient, the result should be placed in TARGET. */
2938 static rtx
2939 expand_builtin_powi (tree exp, rtx target)
2941 tree arg0, arg1;
2942 rtx op0, op1;
2943 machine_mode mode;
2944 machine_mode mode2;
2946 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2947 return NULL_RTX;
2949 arg0 = CALL_EXPR_ARG (exp, 0);
2950 arg1 = CALL_EXPR_ARG (exp, 1);
2951 mode = TYPE_MODE (TREE_TYPE (exp));
2953 /* Emit a libcall to libgcc. */
2955 /* Mode of the 2nd argument must match that of an int. */
2956 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2958 if (target == NULL_RTX)
2959 target = gen_reg_rtx (mode);
2961 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2962 if (GET_MODE (op0) != mode)
2963 op0 = convert_to_mode (mode, op0, 0);
2964 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2965 if (GET_MODE (op1) != mode2)
2966 op1 = convert_to_mode (mode2, op1, 0);
2968 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2969 target, LCT_CONST, mode, 2,
2970 op0, mode, op1, mode2);
2972 return target;
2975 /* Expand expression EXP which is a call to the strlen builtin. Return
2976 NULL_RTX if we failed the caller should emit a normal call, otherwise
2977 try to get the result in TARGET, if convenient. */
2979 static rtx
2980 expand_builtin_strlen (tree exp, rtx target,
2981 machine_mode target_mode)
2983 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2984 return NULL_RTX;
2985 else
2987 struct expand_operand ops[4];
2988 rtx pat;
2989 tree len;
2990 tree src = CALL_EXPR_ARG (exp, 0);
2991 rtx src_reg;
2992 rtx_insn *before_strlen;
2993 machine_mode insn_mode = target_mode;
2994 enum insn_code icode = CODE_FOR_nothing;
2995 unsigned int align;
2997 /* If the length can be computed at compile-time, return it. */
2998 len = c_strlen (src, 0);
2999 if (len)
3000 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3002 /* If the length can be computed at compile-time and is constant
3003 integer, but there are side-effects in src, evaluate
3004 src for side-effects, then return len.
3005 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3006 can be optimized into: i++; x = 3; */
3007 len = c_strlen (src, 1);
3008 if (len && TREE_CODE (len) == INTEGER_CST)
3010 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3011 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3014 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3016 /* If SRC is not a pointer type, don't do this operation inline. */
3017 if (align == 0)
3018 return NULL_RTX;
3020 /* Bail out if we can't compute strlen in the right mode. */
3021 while (insn_mode != VOIDmode)
3023 icode = optab_handler (strlen_optab, insn_mode);
3024 if (icode != CODE_FOR_nothing)
3025 break;
3027 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3029 if (insn_mode == VOIDmode)
3030 return NULL_RTX;
3032 /* Make a place to hold the source address. We will not expand
3033 the actual source until we are sure that the expansion will
3034 not fail -- there are trees that cannot be expanded twice. */
3035 src_reg = gen_reg_rtx (Pmode);
3037 /* Mark the beginning of the strlen sequence so we can emit the
3038 source operand later. */
3039 before_strlen = get_last_insn ();
3041 create_output_operand (&ops[0], target, insn_mode);
3042 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3043 create_integer_operand (&ops[2], 0);
3044 create_integer_operand (&ops[3], align);
3045 if (!maybe_expand_insn (icode, 4, ops))
3046 return NULL_RTX;
3048 /* Now that we are assured of success, expand the source. */
3049 start_sequence ();
3050 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3051 if (pat != src_reg)
3053 #ifdef POINTERS_EXTEND_UNSIGNED
3054 if (GET_MODE (pat) != Pmode)
3055 pat = convert_to_mode (Pmode, pat,
3056 POINTERS_EXTEND_UNSIGNED);
3057 #endif
3058 emit_move_insn (src_reg, pat);
3060 pat = get_insns ();
3061 end_sequence ();
3063 if (before_strlen)
3064 emit_insn_after (pat, before_strlen);
3065 else
3066 emit_insn_before (pat, get_insns ());
3068 /* Return the value in the proper mode for this function. */
3069 if (GET_MODE (ops[0].value) == target_mode)
3070 target = ops[0].value;
3071 else if (target != 0)
3072 convert_move (target, ops[0].value, 0);
3073 else
3074 target = convert_to_mode (target_mode, ops[0].value, 0);
3076 return target;
3080 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3081 bytes from constant string DATA + OFFSET and return it as target
3082 constant. */
3084 static rtx
3085 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3086 machine_mode mode)
3088 const char *str = (const char *) data;
3090 gcc_assert (offset >= 0
3091 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3092 <= strlen (str) + 1));
3094 return c_readstr (str + offset, mode);
3097 /* LEN specify length of the block of memcpy/memset operation.
3098 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3099 In some cases we can make very likely guess on max size, then we
3100 set it into PROBABLE_MAX_SIZE. */
3102 static void
3103 determine_block_size (tree len, rtx len_rtx,
3104 unsigned HOST_WIDE_INT *min_size,
3105 unsigned HOST_WIDE_INT *max_size,
3106 unsigned HOST_WIDE_INT *probable_max_size)
3108 if (CONST_INT_P (len_rtx))
3110 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3111 return;
3113 else
3115 wide_int min, max;
3116 enum value_range_type range_type = VR_UNDEFINED;
3118 /* Determine bounds from the type. */
3119 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3120 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3121 else
3122 *min_size = 0;
3123 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3124 *probable_max_size = *max_size
3125 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3126 else
3127 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3129 if (TREE_CODE (len) == SSA_NAME)
3130 range_type = get_range_info (len, &min, &max);
3131 if (range_type == VR_RANGE)
3133 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3134 *min_size = min.to_uhwi ();
3135 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3136 *probable_max_size = *max_size = max.to_uhwi ();
3138 else if (range_type == VR_ANTI_RANGE)
3140 /* Anti range 0...N lets us to determine minimal size to N+1. */
3141 if (min == 0)
3143 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
3144 *min_size = max.to_uhwi () + 1;
3146 /* Code like
3148 int n;
3149 if (n < 100)
3150 memcpy (a, b, n)
3152 Produce anti range allowing negative values of N. We still
3153 can use the information and make a guess that N is not negative.
3155 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3156 *probable_max_size = min.to_uhwi () - 1;
3159 gcc_checking_assert (*max_size <=
3160 (unsigned HOST_WIDE_INT)
3161 GET_MODE_MASK (GET_MODE (len_rtx)));
3164 /* Helper function to do the actual work for expand_builtin_memcpy. */
3166 static rtx
3167 expand_builtin_memcpy_args (tree dest, tree src, tree len, rtx target, tree exp)
3169 const char *src_str;
3170 unsigned int src_align = get_pointer_alignment (src);
3171 unsigned int dest_align = get_pointer_alignment (dest);
3172 rtx dest_mem, src_mem, dest_addr, len_rtx;
3173 HOST_WIDE_INT expected_size = -1;
3174 unsigned int expected_align = 0;
3175 unsigned HOST_WIDE_INT min_size;
3176 unsigned HOST_WIDE_INT max_size;
3177 unsigned HOST_WIDE_INT probable_max_size;
3179 /* If DEST is not a pointer type, call the normal function. */
3180 if (dest_align == 0)
3181 return NULL_RTX;
3183 /* If either SRC is not a pointer type, don't do this
3184 operation in-line. */
3185 if (src_align == 0)
3186 return NULL_RTX;
3188 if (currently_expanding_gimple_stmt)
3189 stringop_block_profile (currently_expanding_gimple_stmt,
3190 &expected_align, &expected_size);
3192 if (expected_align < dest_align)
3193 expected_align = dest_align;
3194 dest_mem = get_memory_rtx (dest, len);
3195 set_mem_align (dest_mem, dest_align);
3196 len_rtx = expand_normal (len);
3197 determine_block_size (len, len_rtx, &min_size, &max_size,
3198 &probable_max_size);
3199 src_str = c_getstr (src);
3201 /* If SRC is a string constant and block move would be done
3202 by pieces, we can avoid loading the string from memory
3203 and only stored the computed constants. */
3204 if (src_str
3205 && CONST_INT_P (len_rtx)
3206 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3207 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3208 CONST_CAST (char *, src_str),
3209 dest_align, false))
3211 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3212 builtin_memcpy_read_str,
3213 CONST_CAST (char *, src_str),
3214 dest_align, false, 0);
3215 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3216 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3217 return dest_mem;
3220 src_mem = get_memory_rtx (src, len);
3221 set_mem_align (src_mem, src_align);
3223 /* Copy word part most expediently. */
3224 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3225 CALL_EXPR_TAILCALL (exp)
3226 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3227 expected_align, expected_size,
3228 min_size, max_size, probable_max_size);
3230 if (dest_addr == 0)
3232 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3233 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3236 return dest_addr;
3239 /* Expand a call EXP to the memcpy builtin.
3240 Return NULL_RTX if we failed, the caller should emit a normal call,
3241 otherwise try to get the result in TARGET, if convenient (and in
3242 mode MODE if that's convenient). */
3244 static rtx
3245 expand_builtin_memcpy (tree exp, rtx target)
3247 if (!validate_arglist (exp,
3248 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3249 return NULL_RTX;
3250 else
3252 tree dest = CALL_EXPR_ARG (exp, 0);
3253 tree src = CALL_EXPR_ARG (exp, 1);
3254 tree len = CALL_EXPR_ARG (exp, 2);
3255 return expand_builtin_memcpy_args (dest, src, len, target, exp);
3259 /* Expand an instrumented call EXP to the memcpy builtin.
3260 Return NULL_RTX if we failed, the caller should emit a normal call,
3261 otherwise try to get the result in TARGET, if convenient (and in
3262 mode MODE if that's convenient). */
3264 static rtx
3265 expand_builtin_memcpy_with_bounds (tree exp, rtx target)
3267 if (!validate_arglist (exp,
3268 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3269 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3270 INTEGER_TYPE, VOID_TYPE))
3271 return NULL_RTX;
3272 else
3274 tree dest = CALL_EXPR_ARG (exp, 0);
3275 tree src = CALL_EXPR_ARG (exp, 2);
3276 tree len = CALL_EXPR_ARG (exp, 4);
3277 rtx res = expand_builtin_memcpy_args (dest, src, len, target, exp);
3279 /* Return src bounds with the result. */
3280 if (res)
3282 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3283 expand_normal (CALL_EXPR_ARG (exp, 1)));
3284 res = chkp_join_splitted_slot (res, bnd);
3286 return res;
3290 /* Expand a call EXP to the mempcpy builtin.
3291 Return NULL_RTX if we failed; the caller should emit a normal call,
3292 otherwise try to get the result in TARGET, if convenient (and in
3293 mode MODE if that's convenient). If ENDP is 0 return the
3294 destination pointer, if ENDP is 1 return the end pointer ala
3295 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3296 stpcpy. */
3298 static rtx
3299 expand_builtin_mempcpy (tree exp, rtx target, machine_mode mode)
3301 if (!validate_arglist (exp,
3302 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3303 return NULL_RTX;
3304 else
3306 tree dest = CALL_EXPR_ARG (exp, 0);
3307 tree src = CALL_EXPR_ARG (exp, 1);
3308 tree len = CALL_EXPR_ARG (exp, 2);
3309 return expand_builtin_mempcpy_args (dest, src, len,
3310 target, mode, /*endp=*/ 1,
3311 exp);
3315 /* Expand an instrumented call EXP to the mempcpy builtin.
3316 Return NULL_RTX if we failed, the caller should emit a normal call,
3317 otherwise try to get the result in TARGET, if convenient (and in
3318 mode MODE if that's convenient). */
3320 static rtx
3321 expand_builtin_mempcpy_with_bounds (tree exp, rtx target, machine_mode mode)
3323 if (!validate_arglist (exp,
3324 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3325 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3326 INTEGER_TYPE, VOID_TYPE))
3327 return NULL_RTX;
3328 else
3330 tree dest = CALL_EXPR_ARG (exp, 0);
3331 tree src = CALL_EXPR_ARG (exp, 2);
3332 tree len = CALL_EXPR_ARG (exp, 4);
3333 rtx res = expand_builtin_mempcpy_args (dest, src, len, target,
3334 mode, 1, exp);
3336 /* Return src bounds with the result. */
3337 if (res)
3339 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3340 expand_normal (CALL_EXPR_ARG (exp, 1)));
3341 res = chkp_join_splitted_slot (res, bnd);
3343 return res;
3347 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3348 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3349 so that this can also be called without constructing an actual CALL_EXPR.
3350 The other arguments and return value are the same as for
3351 expand_builtin_mempcpy. */
3353 static rtx
3354 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3355 rtx target, machine_mode mode, int endp,
3356 tree orig_exp)
3358 tree fndecl = get_callee_fndecl (orig_exp);
3360 /* If return value is ignored, transform mempcpy into memcpy. */
3361 if (target == const0_rtx
3362 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
3363 && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP))
3365 tree fn = builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP);
3366 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3367 dest, src, len);
3368 return expand_expr (result, target, mode, EXPAND_NORMAL);
3370 else if (target == const0_rtx
3371 && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3373 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3374 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3375 dest, src, len);
3376 return expand_expr (result, target, mode, EXPAND_NORMAL);
3378 else
3380 const char *src_str;
3381 unsigned int src_align = get_pointer_alignment (src);
3382 unsigned int dest_align = get_pointer_alignment (dest);
3383 rtx dest_mem, src_mem, len_rtx;
3385 /* If either SRC or DEST is not a pointer type, don't do this
3386 operation in-line. */
3387 if (dest_align == 0 || src_align == 0)
3388 return NULL_RTX;
3390 /* If LEN is not constant, call the normal function. */
3391 if (! tree_fits_uhwi_p (len))
3392 return NULL_RTX;
3394 len_rtx = expand_normal (len);
3395 src_str = c_getstr (src);
3397 /* If SRC is a string constant and block move would be done
3398 by pieces, we can avoid loading the string from memory
3399 and only stored the computed constants. */
3400 if (src_str
3401 && CONST_INT_P (len_rtx)
3402 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3403 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3404 CONST_CAST (char *, src_str),
3405 dest_align, false))
3407 dest_mem = get_memory_rtx (dest, len);
3408 set_mem_align (dest_mem, dest_align);
3409 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3410 builtin_memcpy_read_str,
3411 CONST_CAST (char *, src_str),
3412 dest_align, false, endp);
3413 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3414 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3415 return dest_mem;
3418 if (CONST_INT_P (len_rtx)
3419 && can_move_by_pieces (INTVAL (len_rtx),
3420 MIN (dest_align, src_align)))
3422 dest_mem = get_memory_rtx (dest, len);
3423 set_mem_align (dest_mem, dest_align);
3424 src_mem = get_memory_rtx (src, len);
3425 set_mem_align (src_mem, src_align);
3426 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3427 MIN (dest_align, src_align), endp);
3428 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3429 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3430 return dest_mem;
3433 return NULL_RTX;
3437 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3438 we failed, the caller should emit a normal call, otherwise try to
3439 get the result in TARGET, if convenient. If ENDP is 0 return the
3440 destination pointer, if ENDP is 1 return the end pointer ala
3441 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3442 stpcpy. */
3444 static rtx
3445 expand_movstr (tree dest, tree src, rtx target, int endp)
3447 struct expand_operand ops[3];
3448 rtx dest_mem;
3449 rtx src_mem;
3451 if (!targetm.have_movstr ())
3452 return NULL_RTX;
3454 dest_mem = get_memory_rtx (dest, NULL);
3455 src_mem = get_memory_rtx (src, NULL);
3456 if (!endp)
3458 target = force_reg (Pmode, XEXP (dest_mem, 0));
3459 dest_mem = replace_equiv_address (dest_mem, target);
3462 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3463 create_fixed_operand (&ops[1], dest_mem);
3464 create_fixed_operand (&ops[2], src_mem);
3465 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3466 return NULL_RTX;
3468 if (endp && target != const0_rtx)
3470 target = ops[0].value;
3471 /* movstr is supposed to set end to the address of the NUL
3472 terminator. If the caller requested a mempcpy-like return value,
3473 adjust it. */
3474 if (endp == 1)
3476 rtx tem = plus_constant (GET_MODE (target),
3477 gen_lowpart (GET_MODE (target), target), 1);
3478 emit_move_insn (target, force_operand (tem, NULL_RTX));
3481 return target;
3484 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3485 NULL_RTX if we failed the caller should emit a normal call, otherwise
3486 try to get the result in TARGET, if convenient (and in mode MODE if that's
3487 convenient). */
3489 static rtx
3490 expand_builtin_strcpy (tree exp, rtx target)
3492 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3494 tree dest = CALL_EXPR_ARG (exp, 0);
3495 tree src = CALL_EXPR_ARG (exp, 1);
3496 return expand_builtin_strcpy_args (dest, src, target);
3498 return NULL_RTX;
3501 /* Helper function to do the actual work for expand_builtin_strcpy. The
3502 arguments to the builtin_strcpy call DEST and SRC are broken out
3503 so that this can also be called without constructing an actual CALL_EXPR.
3504 The other arguments and return value are the same as for
3505 expand_builtin_strcpy. */
3507 static rtx
3508 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3510 return expand_movstr (dest, src, target, /*endp=*/0);
3513 /* Expand a call EXP to the stpcpy builtin.
3514 Return NULL_RTX if we failed the caller should emit a normal call,
3515 otherwise try to get the result in TARGET, if convenient (and in
3516 mode MODE if that's convenient). */
3518 static rtx
3519 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3521 tree dst, src;
3522 location_t loc = EXPR_LOCATION (exp);
3524 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3525 return NULL_RTX;
3527 dst = CALL_EXPR_ARG (exp, 0);
3528 src = CALL_EXPR_ARG (exp, 1);
3530 /* If return value is ignored, transform stpcpy into strcpy. */
3531 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3533 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3534 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3535 return expand_expr (result, target, mode, EXPAND_NORMAL);
3537 else
3539 tree len, lenp1;
3540 rtx ret;
3542 /* Ensure we get an actual string whose length can be evaluated at
3543 compile-time, not an expression containing a string. This is
3544 because the latter will potentially produce pessimized code
3545 when used to produce the return value. */
3546 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3547 return expand_movstr (dst, src, target, /*endp=*/2);
3549 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3550 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3551 target, mode, /*endp=*/2,
3552 exp);
3554 if (ret)
3555 return ret;
3557 if (TREE_CODE (len) == INTEGER_CST)
3559 rtx len_rtx = expand_normal (len);
3561 if (CONST_INT_P (len_rtx))
3563 ret = expand_builtin_strcpy_args (dst, src, target);
3565 if (ret)
3567 if (! target)
3569 if (mode != VOIDmode)
3570 target = gen_reg_rtx (mode);
3571 else
3572 target = gen_reg_rtx (GET_MODE (ret));
3574 if (GET_MODE (target) != GET_MODE (ret))
3575 ret = gen_lowpart (GET_MODE (target), ret);
3577 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3578 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3579 gcc_assert (ret);
3581 return target;
3586 return expand_movstr (dst, src, target, /*endp=*/2);
3590 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3591 bytes from constant string DATA + OFFSET and return it as target
3592 constant. */
3595 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3596 machine_mode mode)
3598 const char *str = (const char *) data;
3600 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3601 return const0_rtx;
3603 return c_readstr (str + offset, mode);
3606 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3607 NULL_RTX if we failed the caller should emit a normal call. */
3609 static rtx
3610 expand_builtin_strncpy (tree exp, rtx target)
3612 location_t loc = EXPR_LOCATION (exp);
3614 if (validate_arglist (exp,
3615 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3617 tree dest = CALL_EXPR_ARG (exp, 0);
3618 tree src = CALL_EXPR_ARG (exp, 1);
3619 tree len = CALL_EXPR_ARG (exp, 2);
3620 tree slen = c_strlen (src, 1);
3622 /* We must be passed a constant len and src parameter. */
3623 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3624 return NULL_RTX;
3626 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3628 /* We're required to pad with trailing zeros if the requested
3629 len is greater than strlen(s2)+1. In that case try to
3630 use store_by_pieces, if it fails, punt. */
3631 if (tree_int_cst_lt (slen, len))
3633 unsigned int dest_align = get_pointer_alignment (dest);
3634 const char *p = c_getstr (src);
3635 rtx dest_mem;
3637 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3638 || !can_store_by_pieces (tree_to_uhwi (len),
3639 builtin_strncpy_read_str,
3640 CONST_CAST (char *, p),
3641 dest_align, false))
3642 return NULL_RTX;
3644 dest_mem = get_memory_rtx (dest, len);
3645 store_by_pieces (dest_mem, tree_to_uhwi (len),
3646 builtin_strncpy_read_str,
3647 CONST_CAST (char *, p), dest_align, false, 0);
3648 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3649 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3650 return dest_mem;
3653 return NULL_RTX;
3656 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3657 bytes from constant string DATA + OFFSET and return it as target
3658 constant. */
3661 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3662 machine_mode mode)
3664 const char *c = (const char *) data;
3665 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3667 memset (p, *c, GET_MODE_SIZE (mode));
3669 return c_readstr (p, mode);
3672 /* Callback routine for store_by_pieces. Return the RTL of a register
3673 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3674 char value given in the RTL register data. For example, if mode is
3675 4 bytes wide, return the RTL for 0x01010101*data. */
3677 static rtx
3678 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3679 machine_mode mode)
3681 rtx target, coeff;
3682 size_t size;
3683 char *p;
3685 size = GET_MODE_SIZE (mode);
3686 if (size == 1)
3687 return (rtx) data;
3689 p = XALLOCAVEC (char, size);
3690 memset (p, 1, size);
3691 coeff = c_readstr (p, mode);
3693 target = convert_to_mode (mode, (rtx) data, 1);
3694 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3695 return force_reg (mode, target);
3698 /* Expand expression EXP, which is a call to the memset builtin. Return
3699 NULL_RTX if we failed the caller should emit a normal call, otherwise
3700 try to get the result in TARGET, if convenient (and in mode MODE if that's
3701 convenient). */
3703 static rtx
3704 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
3706 if (!validate_arglist (exp,
3707 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3708 return NULL_RTX;
3709 else
3711 tree dest = CALL_EXPR_ARG (exp, 0);
3712 tree val = CALL_EXPR_ARG (exp, 1);
3713 tree len = CALL_EXPR_ARG (exp, 2);
3714 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3718 /* Expand expression EXP, which is an instrumented call to the memset builtin.
3719 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
3720 try to get the result in TARGET, if convenient (and in mode MODE if that's
3721 convenient). */
3723 static rtx
3724 expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode)
3726 if (!validate_arglist (exp,
3727 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3728 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3729 return NULL_RTX;
3730 else
3732 tree dest = CALL_EXPR_ARG (exp, 0);
3733 tree val = CALL_EXPR_ARG (exp, 2);
3734 tree len = CALL_EXPR_ARG (exp, 3);
3735 rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp);
3737 /* Return src bounds with the result. */
3738 if (res)
3740 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3741 expand_normal (CALL_EXPR_ARG (exp, 1)));
3742 res = chkp_join_splitted_slot (res, bnd);
3744 return res;
3748 /* Helper function to do the actual work for expand_builtin_memset. The
3749 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3750 so that this can also be called without constructing an actual CALL_EXPR.
3751 The other arguments and return value are the same as for
3752 expand_builtin_memset. */
3754 static rtx
3755 expand_builtin_memset_args (tree dest, tree val, tree len,
3756 rtx target, machine_mode mode, tree orig_exp)
3758 tree fndecl, fn;
3759 enum built_in_function fcode;
3760 machine_mode val_mode;
3761 char c;
3762 unsigned int dest_align;
3763 rtx dest_mem, dest_addr, len_rtx;
3764 HOST_WIDE_INT expected_size = -1;
3765 unsigned int expected_align = 0;
3766 unsigned HOST_WIDE_INT min_size;
3767 unsigned HOST_WIDE_INT max_size;
3768 unsigned HOST_WIDE_INT probable_max_size;
3770 dest_align = get_pointer_alignment (dest);
3772 /* If DEST is not a pointer type, don't do this operation in-line. */
3773 if (dest_align == 0)
3774 return NULL_RTX;
3776 if (currently_expanding_gimple_stmt)
3777 stringop_block_profile (currently_expanding_gimple_stmt,
3778 &expected_align, &expected_size);
3780 if (expected_align < dest_align)
3781 expected_align = dest_align;
3783 /* If the LEN parameter is zero, return DEST. */
3784 if (integer_zerop (len))
3786 /* Evaluate and ignore VAL in case it has side-effects. */
3787 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3788 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3791 /* Stabilize the arguments in case we fail. */
3792 dest = builtin_save_expr (dest);
3793 val = builtin_save_expr (val);
3794 len = builtin_save_expr (len);
3796 len_rtx = expand_normal (len);
3797 determine_block_size (len, len_rtx, &min_size, &max_size,
3798 &probable_max_size);
3799 dest_mem = get_memory_rtx (dest, len);
3800 val_mode = TYPE_MODE (unsigned_char_type_node);
3802 if (TREE_CODE (val) != INTEGER_CST)
3804 rtx val_rtx;
3806 val_rtx = expand_normal (val);
3807 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3809 /* Assume that we can memset by pieces if we can store
3810 * the coefficients by pieces (in the required modes).
3811 * We can't pass builtin_memset_gen_str as that emits RTL. */
3812 c = 1;
3813 if (tree_fits_uhwi_p (len)
3814 && can_store_by_pieces (tree_to_uhwi (len),
3815 builtin_memset_read_str, &c, dest_align,
3816 true))
3818 val_rtx = force_reg (val_mode, val_rtx);
3819 store_by_pieces (dest_mem, tree_to_uhwi (len),
3820 builtin_memset_gen_str, val_rtx, dest_align,
3821 true, 0);
3823 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3824 dest_align, expected_align,
3825 expected_size, min_size, max_size,
3826 probable_max_size))
3827 goto do_libcall;
3829 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3830 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3831 return dest_mem;
3834 if (target_char_cast (val, &c))
3835 goto do_libcall;
3837 if (c)
3839 if (tree_fits_uhwi_p (len)
3840 && can_store_by_pieces (tree_to_uhwi (len),
3841 builtin_memset_read_str, &c, dest_align,
3842 true))
3843 store_by_pieces (dest_mem, tree_to_uhwi (len),
3844 builtin_memset_read_str, &c, dest_align, true, 0);
3845 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3846 gen_int_mode (c, val_mode),
3847 dest_align, expected_align,
3848 expected_size, min_size, max_size,
3849 probable_max_size))
3850 goto do_libcall;
3852 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3853 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3854 return dest_mem;
3857 set_mem_align (dest_mem, dest_align);
3858 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3859 CALL_EXPR_TAILCALL (orig_exp)
3860 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3861 expected_align, expected_size,
3862 min_size, max_size,
3863 probable_max_size);
3865 if (dest_addr == 0)
3867 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3868 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3871 return dest_addr;
3873 do_libcall:
3874 fndecl = get_callee_fndecl (orig_exp);
3875 fcode = DECL_FUNCTION_CODE (fndecl);
3876 if (fcode == BUILT_IN_MEMSET
3877 || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP)
3878 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3879 dest, val, len);
3880 else if (fcode == BUILT_IN_BZERO)
3881 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3882 dest, len);
3883 else
3884 gcc_unreachable ();
3885 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3886 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3887 return expand_call (fn, target, target == const0_rtx);
3890 /* Expand expression EXP, which is a call to the bzero builtin. Return
3891 NULL_RTX if we failed the caller should emit a normal call. */
3893 static rtx
3894 expand_builtin_bzero (tree exp)
3896 tree dest, size;
3897 location_t loc = EXPR_LOCATION (exp);
3899 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3900 return NULL_RTX;
3902 dest = CALL_EXPR_ARG (exp, 0);
3903 size = CALL_EXPR_ARG (exp, 1);
3905 /* New argument list transforming bzero(ptr x, int y) to
3906 memset(ptr x, int 0, size_t y). This is done this way
3907 so that if it isn't expanded inline, we fallback to
3908 calling bzero instead of memset. */
3910 return expand_builtin_memset_args (dest, integer_zero_node,
3911 fold_convert_loc (loc,
3912 size_type_node, size),
3913 const0_rtx, VOIDmode, exp);
3916 /* Try to expand cmpstr operation ICODE with the given operands.
3917 Return the result rtx on success, otherwise return null. */
3919 static rtx
3920 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
3921 HOST_WIDE_INT align)
3923 machine_mode insn_mode = insn_data[icode].operand[0].mode;
3925 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
3926 target = NULL_RTX;
3928 struct expand_operand ops[4];
3929 create_output_operand (&ops[0], target, insn_mode);
3930 create_fixed_operand (&ops[1], arg1_rtx);
3931 create_fixed_operand (&ops[2], arg2_rtx);
3932 create_integer_operand (&ops[3], align);
3933 if (maybe_expand_insn (icode, 4, ops))
3934 return ops[0].value;
3935 return NULL_RTX;
3938 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
3939 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
3940 otherwise return null. */
3942 static rtx
3943 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
3944 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
3945 HOST_WIDE_INT align)
3947 machine_mode insn_mode = insn_data[icode].operand[0].mode;
3949 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
3950 target = NULL_RTX;
3952 struct expand_operand ops[5];
3953 create_output_operand (&ops[0], target, insn_mode);
3954 create_fixed_operand (&ops[1], arg1_rtx);
3955 create_fixed_operand (&ops[2], arg2_rtx);
3956 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
3957 TYPE_UNSIGNED (arg3_type));
3958 create_integer_operand (&ops[4], align);
3959 if (maybe_expand_insn (icode, 5, ops))
3960 return ops[0].value;
3961 return NULL_RTX;
3964 /* Expand expression EXP, which is a call to the memcmp built-in function.
3965 Return NULL_RTX if we failed and the caller should emit a normal call,
3966 otherwise try to get the result in TARGET, if convenient. */
3968 static rtx
3969 expand_builtin_memcmp (tree exp, rtx target)
3971 if (!validate_arglist (exp,
3972 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3973 return NULL_RTX;
3975 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3976 implementing memcmp because it will stop if it encounters two
3977 zero bytes. */
3978 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
3979 if (icode == CODE_FOR_nothing)
3980 return NULL_RTX;
3982 tree arg1 = CALL_EXPR_ARG (exp, 0);
3983 tree arg2 = CALL_EXPR_ARG (exp, 1);
3984 tree len = CALL_EXPR_ARG (exp, 2);
3986 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3987 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3989 /* If we don't have POINTER_TYPE, call the function. */
3990 if (arg1_align == 0 || arg2_align == 0)
3991 return NULL_RTX;
3993 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3994 location_t loc = EXPR_LOCATION (exp);
3995 rtx arg1_rtx = get_memory_rtx (arg1, len);
3996 rtx arg2_rtx = get_memory_rtx (arg2, len);
3997 rtx arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3999 /* Set MEM_SIZE as appropriate. */
4000 if (CONST_INT_P (arg3_rtx))
4002 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
4003 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
4006 rtx result = expand_cmpstrn_or_cmpmem (icode, target, arg1_rtx, arg2_rtx,
4007 TREE_TYPE (len), arg3_rtx,
4008 MIN (arg1_align, arg2_align));
4009 if (result)
4011 /* Return the value in the proper mode for this function. */
4012 if (GET_MODE (result) == mode)
4013 return result;
4015 if (target != 0)
4017 convert_move (target, result, 0);
4018 return target;
4021 return convert_to_mode (mode, result, 0);
4024 result = target;
4025 if (! (result != 0
4026 && REG_P (result) && GET_MODE (result) == mode
4027 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4028 result = gen_reg_rtx (mode);
4030 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4031 TYPE_MODE (integer_type_node), 3,
4032 XEXP (arg1_rtx, 0), Pmode,
4033 XEXP (arg2_rtx, 0), Pmode,
4034 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4035 TYPE_UNSIGNED (sizetype)),
4036 TYPE_MODE (sizetype));
4037 return result;
4040 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4041 if we failed the caller should emit a normal call, otherwise try to get
4042 the result in TARGET, if convenient. */
4044 static rtx
4045 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4047 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4048 return NULL_RTX;
4050 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4051 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4052 if (cmpstr_icode != CODE_FOR_nothing || cmpstrn_icode != CODE_FOR_nothing)
4054 rtx arg1_rtx, arg2_rtx;
4055 tree fndecl, fn;
4056 tree arg1 = CALL_EXPR_ARG (exp, 0);
4057 tree arg2 = CALL_EXPR_ARG (exp, 1);
4058 rtx result = NULL_RTX;
4060 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4061 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4063 /* If we don't have POINTER_TYPE, call the function. */
4064 if (arg1_align == 0 || arg2_align == 0)
4065 return NULL_RTX;
4067 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4068 arg1 = builtin_save_expr (arg1);
4069 arg2 = builtin_save_expr (arg2);
4071 arg1_rtx = get_memory_rtx (arg1, NULL);
4072 arg2_rtx = get_memory_rtx (arg2, NULL);
4074 /* Try to call cmpstrsi. */
4075 if (cmpstr_icode != CODE_FOR_nothing)
4076 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4077 MIN (arg1_align, arg2_align));
4079 /* Try to determine at least one length and call cmpstrnsi. */
4080 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4082 tree len;
4083 rtx arg3_rtx;
4085 tree len1 = c_strlen (arg1, 1);
4086 tree len2 = c_strlen (arg2, 1);
4088 if (len1)
4089 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4090 if (len2)
4091 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4093 /* If we don't have a constant length for the first, use the length
4094 of the second, if we know it. We don't require a constant for
4095 this case; some cost analysis could be done if both are available
4096 but neither is constant. For now, assume they're equally cheap,
4097 unless one has side effects. If both strings have constant lengths,
4098 use the smaller. */
4100 if (!len1)
4101 len = len2;
4102 else if (!len2)
4103 len = len1;
4104 else if (TREE_SIDE_EFFECTS (len1))
4105 len = len2;
4106 else if (TREE_SIDE_EFFECTS (len2))
4107 len = len1;
4108 else if (TREE_CODE (len1) != INTEGER_CST)
4109 len = len2;
4110 else if (TREE_CODE (len2) != INTEGER_CST)
4111 len = len1;
4112 else if (tree_int_cst_lt (len1, len2))
4113 len = len1;
4114 else
4115 len = len2;
4117 /* If both arguments have side effects, we cannot optimize. */
4118 if (len && !TREE_SIDE_EFFECTS (len))
4120 arg3_rtx = expand_normal (len);
4121 result = expand_cmpstrn_or_cmpmem
4122 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4123 arg3_rtx, MIN (arg1_align, arg2_align));
4127 if (result)
4129 /* Return the value in the proper mode for this function. */
4130 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4131 if (GET_MODE (result) == mode)
4132 return result;
4133 if (target == 0)
4134 return convert_to_mode (mode, result, 0);
4135 convert_move (target, result, 0);
4136 return target;
4139 /* Expand the library call ourselves using a stabilized argument
4140 list to avoid re-evaluating the function's arguments twice. */
4141 fndecl = get_callee_fndecl (exp);
4142 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4143 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4144 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4145 return expand_call (fn, target, target == const0_rtx);
4147 return NULL_RTX;
4150 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4151 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4152 the result in TARGET, if convenient. */
4154 static rtx
4155 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4156 ATTRIBUTE_UNUSED machine_mode mode)
4158 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4160 if (!validate_arglist (exp,
4161 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4162 return NULL_RTX;
4164 /* If c_strlen can determine an expression for one of the string
4165 lengths, and it doesn't have side effects, then emit cmpstrnsi
4166 using length MIN(strlen(string)+1, arg3). */
4167 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4168 if (cmpstrn_icode != CODE_FOR_nothing)
4170 tree len, len1, len2;
4171 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4172 rtx result;
4173 tree fndecl, fn;
4174 tree arg1 = CALL_EXPR_ARG (exp, 0);
4175 tree arg2 = CALL_EXPR_ARG (exp, 1);
4176 tree arg3 = CALL_EXPR_ARG (exp, 2);
4178 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4179 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4181 len1 = c_strlen (arg1, 1);
4182 len2 = c_strlen (arg2, 1);
4184 if (len1)
4185 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4186 if (len2)
4187 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4189 /* If we don't have a constant length for the first, use the length
4190 of the second, if we know it. We don't require a constant for
4191 this case; some cost analysis could be done if both are available
4192 but neither is constant. For now, assume they're equally cheap,
4193 unless one has side effects. If both strings have constant lengths,
4194 use the smaller. */
4196 if (!len1)
4197 len = len2;
4198 else if (!len2)
4199 len = len1;
4200 else if (TREE_SIDE_EFFECTS (len1))
4201 len = len2;
4202 else if (TREE_SIDE_EFFECTS (len2))
4203 len = len1;
4204 else if (TREE_CODE (len1) != INTEGER_CST)
4205 len = len2;
4206 else if (TREE_CODE (len2) != INTEGER_CST)
4207 len = len1;
4208 else if (tree_int_cst_lt (len1, len2))
4209 len = len1;
4210 else
4211 len = len2;
4213 /* If both arguments have side effects, we cannot optimize. */
4214 if (!len || TREE_SIDE_EFFECTS (len))
4215 return NULL_RTX;
4217 /* The actual new length parameter is MIN(len,arg3). */
4218 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4219 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4221 /* If we don't have POINTER_TYPE, call the function. */
4222 if (arg1_align == 0 || arg2_align == 0)
4223 return NULL_RTX;
4225 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4226 arg1 = builtin_save_expr (arg1);
4227 arg2 = builtin_save_expr (arg2);
4228 len = builtin_save_expr (len);
4230 arg1_rtx = get_memory_rtx (arg1, len);
4231 arg2_rtx = get_memory_rtx (arg2, len);
4232 arg3_rtx = expand_normal (len);
4233 result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4234 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4235 MIN (arg1_align, arg2_align));
4236 if (result)
4238 /* Return the value in the proper mode for this function. */
4239 mode = TYPE_MODE (TREE_TYPE (exp));
4240 if (GET_MODE (result) == mode)
4241 return result;
4242 if (target == 0)
4243 return convert_to_mode (mode, result, 0);
4244 convert_move (target, result, 0);
4245 return target;
4248 /* Expand the library call ourselves using a stabilized argument
4249 list to avoid re-evaluating the function's arguments twice. */
4250 fndecl = get_callee_fndecl (exp);
4251 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4252 arg1, arg2, len);
4253 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4254 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4255 return expand_call (fn, target, target == const0_rtx);
4257 return NULL_RTX;
4260 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4261 if that's convenient. */
4264 expand_builtin_saveregs (void)
4266 rtx val;
4267 rtx_insn *seq;
4269 /* Don't do __builtin_saveregs more than once in a function.
4270 Save the result of the first call and reuse it. */
4271 if (saveregs_value != 0)
4272 return saveregs_value;
4274 /* When this function is called, it means that registers must be
4275 saved on entry to this function. So we migrate the call to the
4276 first insn of this function. */
4278 start_sequence ();
4280 /* Do whatever the machine needs done in this case. */
4281 val = targetm.calls.expand_builtin_saveregs ();
4283 seq = get_insns ();
4284 end_sequence ();
4286 saveregs_value = val;
4288 /* Put the insns after the NOTE that starts the function. If this
4289 is inside a start_sequence, make the outer-level insn chain current, so
4290 the code is placed at the start of the function. */
4291 push_topmost_sequence ();
4292 emit_insn_after (seq, entry_of_function ());
4293 pop_topmost_sequence ();
4295 return val;
4298 /* Expand a call to __builtin_next_arg. */
4300 static rtx
4301 expand_builtin_next_arg (void)
4303 /* Checking arguments is already done in fold_builtin_next_arg
4304 that must be called before this function. */
4305 return expand_binop (ptr_mode, add_optab,
4306 crtl->args.internal_arg_pointer,
4307 crtl->args.arg_offset_rtx,
4308 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4311 /* Make it easier for the backends by protecting the valist argument
4312 from multiple evaluations. */
4314 static tree
4315 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4317 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4319 /* The current way of determining the type of valist is completely
4320 bogus. We should have the information on the va builtin instead. */
4321 if (!vatype)
4322 vatype = targetm.fn_abi_va_list (cfun->decl);
4324 if (TREE_CODE (vatype) == ARRAY_TYPE)
4326 if (TREE_SIDE_EFFECTS (valist))
4327 valist = save_expr (valist);
4329 /* For this case, the backends will be expecting a pointer to
4330 vatype, but it's possible we've actually been given an array
4331 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4332 So fix it. */
4333 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4335 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4336 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4339 else
4341 tree pt = build_pointer_type (vatype);
4343 if (! needs_lvalue)
4345 if (! TREE_SIDE_EFFECTS (valist))
4346 return valist;
4348 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4349 TREE_SIDE_EFFECTS (valist) = 1;
4352 if (TREE_SIDE_EFFECTS (valist))
4353 valist = save_expr (valist);
4354 valist = fold_build2_loc (loc, MEM_REF,
4355 vatype, valist, build_int_cst (pt, 0));
4358 return valist;
4361 /* The "standard" definition of va_list is void*. */
4363 tree
4364 std_build_builtin_va_list (void)
4366 return ptr_type_node;
4369 /* The "standard" abi va_list is va_list_type_node. */
4371 tree
4372 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4374 return va_list_type_node;
4377 /* The "standard" type of va_list is va_list_type_node. */
4379 tree
4380 std_canonical_va_list_type (tree type)
4382 tree wtype, htype;
4384 if (INDIRECT_REF_P (type))
4385 type = TREE_TYPE (type);
4386 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4387 type = TREE_TYPE (type);
4388 wtype = va_list_type_node;
4389 htype = type;
4390 /* Treat structure va_list types. */
4391 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4392 htype = TREE_TYPE (htype);
4393 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4395 /* If va_list is an array type, the argument may have decayed
4396 to a pointer type, e.g. by being passed to another function.
4397 In that case, unwrap both types so that we can compare the
4398 underlying records. */
4399 if (TREE_CODE (htype) == ARRAY_TYPE
4400 || POINTER_TYPE_P (htype))
4402 wtype = TREE_TYPE (wtype);
4403 htype = TREE_TYPE (htype);
4406 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4407 return va_list_type_node;
4409 return NULL_TREE;
4412 /* The "standard" implementation of va_start: just assign `nextarg' to
4413 the variable. */
4415 void
4416 std_expand_builtin_va_start (tree valist, rtx nextarg)
4418 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4419 convert_move (va_r, nextarg, 0);
4421 /* We do not have any valid bounds for the pointer, so
4422 just store zero bounds for it. */
4423 if (chkp_function_instrumented_p (current_function_decl))
4424 chkp_expand_bounds_reset_for_mem (valist,
4425 make_tree (TREE_TYPE (valist),
4426 nextarg));
4429 /* Expand EXP, a call to __builtin_va_start. */
4431 static rtx
4432 expand_builtin_va_start (tree exp)
4434 rtx nextarg;
4435 tree valist;
4436 location_t loc = EXPR_LOCATION (exp);
4438 if (call_expr_nargs (exp) < 2)
4440 error_at (loc, "too few arguments to function %<va_start%>");
4441 return const0_rtx;
4444 if (fold_builtin_next_arg (exp, true))
4445 return const0_rtx;
4447 nextarg = expand_builtin_next_arg ();
4448 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4450 if (targetm.expand_builtin_va_start)
4451 targetm.expand_builtin_va_start (valist, nextarg);
4452 else
4453 std_expand_builtin_va_start (valist, nextarg);
4455 return const0_rtx;
4458 /* Expand EXP, a call to __builtin_va_end. */
4460 static rtx
4461 expand_builtin_va_end (tree exp)
4463 tree valist = CALL_EXPR_ARG (exp, 0);
4465 /* Evaluate for side effects, if needed. I hate macros that don't
4466 do that. */
4467 if (TREE_SIDE_EFFECTS (valist))
4468 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4470 return const0_rtx;
4473 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4474 builtin rather than just as an assignment in stdarg.h because of the
4475 nastiness of array-type va_list types. */
4477 static rtx
4478 expand_builtin_va_copy (tree exp)
4480 tree dst, src, t;
4481 location_t loc = EXPR_LOCATION (exp);
4483 dst = CALL_EXPR_ARG (exp, 0);
4484 src = CALL_EXPR_ARG (exp, 1);
4486 dst = stabilize_va_list_loc (loc, dst, 1);
4487 src = stabilize_va_list_loc (loc, src, 0);
4489 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4491 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4493 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4494 TREE_SIDE_EFFECTS (t) = 1;
4495 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4497 else
4499 rtx dstb, srcb, size;
4501 /* Evaluate to pointers. */
4502 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4503 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4504 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4505 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4507 dstb = convert_memory_address (Pmode, dstb);
4508 srcb = convert_memory_address (Pmode, srcb);
4510 /* "Dereference" to BLKmode memories. */
4511 dstb = gen_rtx_MEM (BLKmode, dstb);
4512 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4513 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4514 srcb = gen_rtx_MEM (BLKmode, srcb);
4515 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4516 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4518 /* Copy. */
4519 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4522 return const0_rtx;
4525 /* Expand a call to one of the builtin functions __builtin_frame_address or
4526 __builtin_return_address. */
4528 static rtx
4529 expand_builtin_frame_address (tree fndecl, tree exp)
4531 /* The argument must be a nonnegative integer constant.
4532 It counts the number of frames to scan up the stack.
4533 The value is either the frame pointer value or the return
4534 address saved in that frame. */
4535 if (call_expr_nargs (exp) == 0)
4536 /* Warning about missing arg was already issued. */
4537 return const0_rtx;
4538 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4540 error ("invalid argument to %qD", fndecl);
4541 return const0_rtx;
4543 else
4545 /* Number of frames to scan up the stack. */
4546 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
4548 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
4550 /* Some ports cannot access arbitrary stack frames. */
4551 if (tem == NULL)
4553 warning (0, "unsupported argument to %qD", fndecl);
4554 return const0_rtx;
4557 if (count)
4559 /* Warn since no effort is made to ensure that any frame
4560 beyond the current one exists or can be safely reached. */
4561 warning (OPT_Wframe_address, "calling %qD with "
4562 "a nonzero argument is unsafe", fndecl);
4565 /* For __builtin_frame_address, return what we've got. */
4566 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4567 return tem;
4569 if (!REG_P (tem)
4570 && ! CONSTANT_P (tem))
4571 tem = copy_addr_to_reg (tem);
4572 return tem;
4576 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4577 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4578 is the same as for allocate_dynamic_stack_space. */
4580 static rtx
4581 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4583 rtx op0;
4584 rtx result;
4585 bool valid_arglist;
4586 unsigned int align;
4587 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4588 == BUILT_IN_ALLOCA_WITH_ALIGN);
4590 valid_arglist
4591 = (alloca_with_align
4592 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4593 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4595 if (!valid_arglist)
4596 return NULL_RTX;
4598 /* Compute the argument. */
4599 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4601 /* Compute the alignment. */
4602 align = (alloca_with_align
4603 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4604 : BIGGEST_ALIGNMENT);
4606 /* Allocate the desired space. */
4607 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4608 result = convert_memory_address (ptr_mode, result);
4610 return result;
4613 /* Expand a call to bswap builtin in EXP.
4614 Return NULL_RTX if a normal call should be emitted rather than expanding the
4615 function in-line. If convenient, the result should be placed in TARGET.
4616 SUBTARGET may be used as the target for computing one of EXP's operands. */
4618 static rtx
4619 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
4620 rtx subtarget)
4622 tree arg;
4623 rtx op0;
4625 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4626 return NULL_RTX;
4628 arg = CALL_EXPR_ARG (exp, 0);
4629 op0 = expand_expr (arg,
4630 subtarget && GET_MODE (subtarget) == target_mode
4631 ? subtarget : NULL_RTX,
4632 target_mode, EXPAND_NORMAL);
4633 if (GET_MODE (op0) != target_mode)
4634 op0 = convert_to_mode (target_mode, op0, 1);
4636 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4638 gcc_assert (target);
4640 return convert_to_mode (target_mode, target, 1);
4643 /* Expand a call to a unary builtin in EXP.
4644 Return NULL_RTX if a normal call should be emitted rather than expanding the
4645 function in-line. If convenient, the result should be placed in TARGET.
4646 SUBTARGET may be used as the target for computing one of EXP's operands. */
4648 static rtx
4649 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
4650 rtx subtarget, optab op_optab)
4652 rtx op0;
4654 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4655 return NULL_RTX;
4657 /* Compute the argument. */
4658 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4659 (subtarget
4660 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4661 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4662 VOIDmode, EXPAND_NORMAL);
4663 /* Compute op, into TARGET if possible.
4664 Set TARGET to wherever the result comes back. */
4665 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4666 op_optab, op0, target, op_optab != clrsb_optab);
4667 gcc_assert (target);
4669 return convert_to_mode (target_mode, target, 0);
4672 /* Expand a call to __builtin_expect. We just return our argument
4673 as the builtin_expect semantic should've been already executed by
4674 tree branch prediction pass. */
4676 static rtx
4677 expand_builtin_expect (tree exp, rtx target)
4679 tree arg;
4681 if (call_expr_nargs (exp) < 2)
4682 return const0_rtx;
4683 arg = CALL_EXPR_ARG (exp, 0);
4685 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4686 /* When guessing was done, the hints should be already stripped away. */
4687 gcc_assert (!flag_guess_branch_prob
4688 || optimize == 0 || seen_error ());
4689 return target;
4692 /* Expand a call to __builtin_assume_aligned. We just return our first
4693 argument as the builtin_assume_aligned semantic should've been already
4694 executed by CCP. */
4696 static rtx
4697 expand_builtin_assume_aligned (tree exp, rtx target)
4699 if (call_expr_nargs (exp) < 2)
4700 return const0_rtx;
4701 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4702 EXPAND_NORMAL);
4703 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4704 && (call_expr_nargs (exp) < 3
4705 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4706 return target;
4709 void
4710 expand_builtin_trap (void)
4712 if (targetm.have_trap ())
4714 rtx_insn *insn = emit_insn (targetm.gen_trap ());
4715 /* For trap insns when not accumulating outgoing args force
4716 REG_ARGS_SIZE note to prevent crossjumping of calls with
4717 different args sizes. */
4718 if (!ACCUMULATE_OUTGOING_ARGS)
4719 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4721 else
4722 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4723 emit_barrier ();
4726 /* Expand a call to __builtin_unreachable. We do nothing except emit
4727 a barrier saying that control flow will not pass here.
4729 It is the responsibility of the program being compiled to ensure
4730 that control flow does never reach __builtin_unreachable. */
4731 static void
4732 expand_builtin_unreachable (void)
4734 emit_barrier ();
4737 /* Expand EXP, a call to fabs, fabsf or fabsl.
4738 Return NULL_RTX if a normal call should be emitted rather than expanding
4739 the function inline. If convenient, the result should be placed
4740 in TARGET. SUBTARGET may be used as the target for computing
4741 the operand. */
4743 static rtx
4744 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4746 machine_mode mode;
4747 tree arg;
4748 rtx op0;
4750 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4751 return NULL_RTX;
4753 arg = CALL_EXPR_ARG (exp, 0);
4754 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4755 mode = TYPE_MODE (TREE_TYPE (arg));
4756 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4757 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4760 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4761 Return NULL is a normal call should be emitted rather than expanding the
4762 function inline. If convenient, the result should be placed in TARGET.
4763 SUBTARGET may be used as the target for computing the operand. */
4765 static rtx
4766 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4768 rtx op0, op1;
4769 tree arg;
4771 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4772 return NULL_RTX;
4774 arg = CALL_EXPR_ARG (exp, 0);
4775 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4777 arg = CALL_EXPR_ARG (exp, 1);
4778 op1 = expand_normal (arg);
4780 return expand_copysign (op0, op1, target);
4783 /* Expand a call to __builtin___clear_cache. */
4785 static rtx
4786 expand_builtin___clear_cache (tree exp)
4788 if (!targetm.code_for_clear_cache)
4790 #ifdef CLEAR_INSN_CACHE
4791 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4792 does something. Just do the default expansion to a call to
4793 __clear_cache(). */
4794 return NULL_RTX;
4795 #else
4796 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4797 does nothing. There is no need to call it. Do nothing. */
4798 return const0_rtx;
4799 #endif /* CLEAR_INSN_CACHE */
4802 /* We have a "clear_cache" insn, and it will handle everything. */
4803 tree begin, end;
4804 rtx begin_rtx, end_rtx;
4806 /* We must not expand to a library call. If we did, any
4807 fallback library function in libgcc that might contain a call to
4808 __builtin___clear_cache() would recurse infinitely. */
4809 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4811 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4812 return const0_rtx;
4815 if (targetm.have_clear_cache ())
4817 struct expand_operand ops[2];
4819 begin = CALL_EXPR_ARG (exp, 0);
4820 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4822 end = CALL_EXPR_ARG (exp, 1);
4823 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4825 create_address_operand (&ops[0], begin_rtx);
4826 create_address_operand (&ops[1], end_rtx);
4827 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
4828 return const0_rtx;
4830 return const0_rtx;
4833 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4835 static rtx
4836 round_trampoline_addr (rtx tramp)
4838 rtx temp, addend, mask;
4840 /* If we don't need too much alignment, we'll have been guaranteed
4841 proper alignment by get_trampoline_type. */
4842 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4843 return tramp;
4845 /* Round address up to desired boundary. */
4846 temp = gen_reg_rtx (Pmode);
4847 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4848 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4850 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4851 temp, 0, OPTAB_LIB_WIDEN);
4852 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4853 temp, 0, OPTAB_LIB_WIDEN);
4855 return tramp;
4858 static rtx
4859 expand_builtin_init_trampoline (tree exp, bool onstack)
4861 tree t_tramp, t_func, t_chain;
4862 rtx m_tramp, r_tramp, r_chain, tmp;
4864 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4865 POINTER_TYPE, VOID_TYPE))
4866 return NULL_RTX;
4868 t_tramp = CALL_EXPR_ARG (exp, 0);
4869 t_func = CALL_EXPR_ARG (exp, 1);
4870 t_chain = CALL_EXPR_ARG (exp, 2);
4872 r_tramp = expand_normal (t_tramp);
4873 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4874 MEM_NOTRAP_P (m_tramp) = 1;
4876 /* If ONSTACK, the TRAMP argument should be the address of a field
4877 within the local function's FRAME decl. Either way, let's see if
4878 we can fill in the MEM_ATTRs for this memory. */
4879 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4880 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4882 /* Creator of a heap trampoline is responsible for making sure the
4883 address is aligned to at least STACK_BOUNDARY. Normally malloc
4884 will ensure this anyhow. */
4885 tmp = round_trampoline_addr (r_tramp);
4886 if (tmp != r_tramp)
4888 m_tramp = change_address (m_tramp, BLKmode, tmp);
4889 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4890 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4893 /* The FUNC argument should be the address of the nested function.
4894 Extract the actual function decl to pass to the hook. */
4895 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4896 t_func = TREE_OPERAND (t_func, 0);
4897 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4899 r_chain = expand_normal (t_chain);
4901 /* Generate insns to initialize the trampoline. */
4902 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4904 if (onstack)
4906 trampolines_created = 1;
4908 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4909 "trampoline generated for nested function %qD", t_func);
4912 return const0_rtx;
4915 static rtx
4916 expand_builtin_adjust_trampoline (tree exp)
4918 rtx tramp;
4920 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4921 return NULL_RTX;
4923 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4924 tramp = round_trampoline_addr (tramp);
4925 if (targetm.calls.trampoline_adjust_address)
4926 tramp = targetm.calls.trampoline_adjust_address (tramp);
4928 return tramp;
4931 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4932 function. The function first checks whether the back end provides
4933 an insn to implement signbit for the respective mode. If not, it
4934 checks whether the floating point format of the value is such that
4935 the sign bit can be extracted. If that is not the case, error out.
4936 EXP is the expression that is a call to the builtin function; if
4937 convenient, the result should be placed in TARGET. */
4938 static rtx
4939 expand_builtin_signbit (tree exp, rtx target)
4941 const struct real_format *fmt;
4942 machine_mode fmode, imode, rmode;
4943 tree arg;
4944 int word, bitpos;
4945 enum insn_code icode;
4946 rtx temp;
4947 location_t loc = EXPR_LOCATION (exp);
4949 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4950 return NULL_RTX;
4952 arg = CALL_EXPR_ARG (exp, 0);
4953 fmode = TYPE_MODE (TREE_TYPE (arg));
4954 rmode = TYPE_MODE (TREE_TYPE (exp));
4955 fmt = REAL_MODE_FORMAT (fmode);
4957 arg = builtin_save_expr (arg);
4959 /* Expand the argument yielding a RTX expression. */
4960 temp = expand_normal (arg);
4962 /* Check if the back end provides an insn that handles signbit for the
4963 argument's mode. */
4964 icode = optab_handler (signbit_optab, fmode);
4965 if (icode != CODE_FOR_nothing)
4967 rtx_insn *last = get_last_insn ();
4968 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4969 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4970 return target;
4971 delete_insns_since (last);
4974 /* For floating point formats without a sign bit, implement signbit
4975 as "ARG < 0.0". */
4976 bitpos = fmt->signbit_ro;
4977 if (bitpos < 0)
4979 /* But we can't do this if the format supports signed zero. */
4980 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
4982 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4983 build_real (TREE_TYPE (arg), dconst0));
4984 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4987 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4989 imode = int_mode_for_mode (fmode);
4990 gcc_assert (imode != BLKmode);
4991 temp = gen_lowpart (imode, temp);
4993 else
4995 imode = word_mode;
4996 /* Handle targets with different FP word orders. */
4997 if (FLOAT_WORDS_BIG_ENDIAN)
4998 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4999 else
5000 word = bitpos / BITS_PER_WORD;
5001 temp = operand_subword_force (temp, word, fmode);
5002 bitpos = bitpos % BITS_PER_WORD;
5005 /* Force the intermediate word_mode (or narrower) result into a
5006 register. This avoids attempting to create paradoxical SUBREGs
5007 of floating point modes below. */
5008 temp = force_reg (imode, temp);
5010 /* If the bitpos is within the "result mode" lowpart, the operation
5011 can be implement with a single bitwise AND. Otherwise, we need
5012 a right shift and an AND. */
5014 if (bitpos < GET_MODE_BITSIZE (rmode))
5016 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5018 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5019 temp = gen_lowpart (rmode, temp);
5020 temp = expand_binop (rmode, and_optab, temp,
5021 immed_wide_int_const (mask, rmode),
5022 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5024 else
5026 /* Perform a logical right shift to place the signbit in the least
5027 significant bit, then truncate the result to the desired mode
5028 and mask just this bit. */
5029 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5030 temp = gen_lowpart (rmode, temp);
5031 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5032 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5035 return temp;
5038 /* Expand fork or exec calls. TARGET is the desired target of the
5039 call. EXP is the call. FN is the
5040 identificator of the actual function. IGNORE is nonzero if the
5041 value is to be ignored. */
5043 static rtx
5044 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5046 tree id, decl;
5047 tree call;
5049 /* If we are not profiling, just call the function. */
5050 if (!profile_arc_flag)
5051 return NULL_RTX;
5053 /* Otherwise call the wrapper. This should be equivalent for the rest of
5054 compiler, so the code does not diverge, and the wrapper may run the
5055 code necessary for keeping the profiling sane. */
5057 switch (DECL_FUNCTION_CODE (fn))
5059 case BUILT_IN_FORK:
5060 id = get_identifier ("__gcov_fork");
5061 break;
5063 case BUILT_IN_EXECL:
5064 id = get_identifier ("__gcov_execl");
5065 break;
5067 case BUILT_IN_EXECV:
5068 id = get_identifier ("__gcov_execv");
5069 break;
5071 case BUILT_IN_EXECLP:
5072 id = get_identifier ("__gcov_execlp");
5073 break;
5075 case BUILT_IN_EXECLE:
5076 id = get_identifier ("__gcov_execle");
5077 break;
5079 case BUILT_IN_EXECVP:
5080 id = get_identifier ("__gcov_execvp");
5081 break;
5083 case BUILT_IN_EXECVE:
5084 id = get_identifier ("__gcov_execve");
5085 break;
5087 default:
5088 gcc_unreachable ();
5091 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5092 FUNCTION_DECL, id, TREE_TYPE (fn));
5093 DECL_EXTERNAL (decl) = 1;
5094 TREE_PUBLIC (decl) = 1;
5095 DECL_ARTIFICIAL (decl) = 1;
5096 TREE_NOTHROW (decl) = 1;
5097 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5098 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5099 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5100 return expand_call (call, target, ignore);
5105 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5106 the pointer in these functions is void*, the tree optimizers may remove
5107 casts. The mode computed in expand_builtin isn't reliable either, due
5108 to __sync_bool_compare_and_swap.
5110 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5111 group of builtins. This gives us log2 of the mode size. */
5113 static inline machine_mode
5114 get_builtin_sync_mode (int fcode_diff)
5116 /* The size is not negotiable, so ask not to get BLKmode in return
5117 if the target indicates that a smaller size would be better. */
5118 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5121 /* Expand the memory expression LOC and return the appropriate memory operand
5122 for the builtin_sync operations. */
5124 static rtx
5125 get_builtin_sync_mem (tree loc, machine_mode mode)
5127 rtx addr, mem;
5129 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5130 addr = convert_memory_address (Pmode, addr);
5132 /* Note that we explicitly do not want any alias information for this
5133 memory, so that we kill all other live memories. Otherwise we don't
5134 satisfy the full barrier semantics of the intrinsic. */
5135 mem = validize_mem (gen_rtx_MEM (mode, addr));
5137 /* The alignment needs to be at least according to that of the mode. */
5138 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5139 get_pointer_alignment (loc)));
5140 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5141 MEM_VOLATILE_P (mem) = 1;
5143 return mem;
5146 /* Make sure an argument is in the right mode.
5147 EXP is the tree argument.
5148 MODE is the mode it should be in. */
5150 static rtx
5151 expand_expr_force_mode (tree exp, machine_mode mode)
5153 rtx val;
5154 machine_mode old_mode;
5156 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5157 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5158 of CONST_INTs, where we know the old_mode only from the call argument. */
5160 old_mode = GET_MODE (val);
5161 if (old_mode == VOIDmode)
5162 old_mode = TYPE_MODE (TREE_TYPE (exp));
5163 val = convert_modes (mode, old_mode, val, 1);
5164 return val;
5168 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5169 EXP is the CALL_EXPR. CODE is the rtx code
5170 that corresponds to the arithmetic or logical operation from the name;
5171 an exception here is that NOT actually means NAND. TARGET is an optional
5172 place for us to store the results; AFTER is true if this is the
5173 fetch_and_xxx form. */
5175 static rtx
5176 expand_builtin_sync_operation (machine_mode mode, tree exp,
5177 enum rtx_code code, bool after,
5178 rtx target)
5180 rtx val, mem;
5181 location_t loc = EXPR_LOCATION (exp);
5183 if (code == NOT && warn_sync_nand)
5185 tree fndecl = get_callee_fndecl (exp);
5186 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5188 static bool warned_f_a_n, warned_n_a_f;
5190 switch (fcode)
5192 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5193 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5194 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5195 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5196 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5197 if (warned_f_a_n)
5198 break;
5200 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5201 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5202 warned_f_a_n = true;
5203 break;
5205 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5206 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5207 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5208 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5209 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5210 if (warned_n_a_f)
5211 break;
5213 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5214 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5215 warned_n_a_f = true;
5216 break;
5218 default:
5219 gcc_unreachable ();
5223 /* Expand the operands. */
5224 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5225 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5227 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
5228 after);
5231 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5232 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5233 true if this is the boolean form. TARGET is a place for us to store the
5234 results; this is NOT optional if IS_BOOL is true. */
5236 static rtx
5237 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5238 bool is_bool, rtx target)
5240 rtx old_val, new_val, mem;
5241 rtx *pbool, *poval;
5243 /* Expand the operands. */
5244 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5245 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5246 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5248 pbool = poval = NULL;
5249 if (target != const0_rtx)
5251 if (is_bool)
5252 pbool = &target;
5253 else
5254 poval = &target;
5256 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5257 false, MEMMODEL_SYNC_SEQ_CST,
5258 MEMMODEL_SYNC_SEQ_CST))
5259 return NULL_RTX;
5261 return target;
5264 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5265 general form is actually an atomic exchange, and some targets only
5266 support a reduced form with the second argument being a constant 1.
5267 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5268 the results. */
5270 static rtx
5271 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5272 rtx target)
5274 rtx val, mem;
5276 /* Expand the operands. */
5277 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5278 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5280 return expand_sync_lock_test_and_set (target, mem, val);
5283 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5285 static void
5286 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5288 rtx mem;
5290 /* Expand the operands. */
5291 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5293 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
5296 /* Given an integer representing an ``enum memmodel'', verify its
5297 correctness and return the memory model enum. */
5299 static enum memmodel
5300 get_memmodel (tree exp)
5302 rtx op;
5303 unsigned HOST_WIDE_INT val;
5305 /* If the parameter is not a constant, it's a run time value so we'll just
5306 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5307 if (TREE_CODE (exp) != INTEGER_CST)
5308 return MEMMODEL_SEQ_CST;
5310 op = expand_normal (exp);
5312 val = INTVAL (op);
5313 if (targetm.memmodel_check)
5314 val = targetm.memmodel_check (val);
5315 else if (val & ~MEMMODEL_MASK)
5317 warning (OPT_Winvalid_memory_model,
5318 "Unknown architecture specifier in memory model to builtin.");
5319 return MEMMODEL_SEQ_CST;
5322 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5323 if (memmodel_base (val) >= MEMMODEL_LAST)
5325 warning (OPT_Winvalid_memory_model,
5326 "invalid memory model argument to builtin");
5327 return MEMMODEL_SEQ_CST;
5330 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5331 be conservative and promote consume to acquire. */
5332 if (val == MEMMODEL_CONSUME)
5333 val = MEMMODEL_ACQUIRE;
5335 return (enum memmodel) val;
5338 /* Expand the __atomic_exchange intrinsic:
5339 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5340 EXP is the CALL_EXPR.
5341 TARGET is an optional place for us to store the results. */
5343 static rtx
5344 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5346 rtx val, mem;
5347 enum memmodel model;
5349 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5351 if (!flag_inline_atomics)
5352 return NULL_RTX;
5354 /* Expand the operands. */
5355 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5356 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5358 return expand_atomic_exchange (target, mem, val, model);
5361 /* Expand the __atomic_compare_exchange intrinsic:
5362 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5363 TYPE desired, BOOL weak,
5364 enum memmodel success,
5365 enum memmodel failure)
5366 EXP is the CALL_EXPR.
5367 TARGET is an optional place for us to store the results. */
5369 static rtx
5370 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5371 rtx target)
5373 rtx expect, desired, mem, oldval;
5374 rtx_code_label *label;
5375 enum memmodel success, failure;
5376 tree weak;
5377 bool is_weak;
5379 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5380 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5382 if (failure > success)
5384 warning (OPT_Winvalid_memory_model,
5385 "failure memory model cannot be stronger than success memory "
5386 "model for %<__atomic_compare_exchange%>");
5387 success = MEMMODEL_SEQ_CST;
5390 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5392 warning (OPT_Winvalid_memory_model,
5393 "invalid failure memory model for "
5394 "%<__atomic_compare_exchange%>");
5395 failure = MEMMODEL_SEQ_CST;
5396 success = MEMMODEL_SEQ_CST;
5400 if (!flag_inline_atomics)
5401 return NULL_RTX;
5403 /* Expand the operands. */
5404 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5406 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5407 expect = convert_memory_address (Pmode, expect);
5408 expect = gen_rtx_MEM (mode, expect);
5409 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5411 weak = CALL_EXPR_ARG (exp, 3);
5412 is_weak = false;
5413 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5414 is_weak = true;
5416 if (target == const0_rtx)
5417 target = NULL;
5419 /* Lest the rtl backend create a race condition with an imporoper store
5420 to memory, always create a new pseudo for OLDVAL. */
5421 oldval = NULL;
5423 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5424 is_weak, success, failure))
5425 return NULL_RTX;
5427 /* Conditionally store back to EXPECT, lest we create a race condition
5428 with an improper store to memory. */
5429 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5430 the normal case where EXPECT is totally private, i.e. a register. At
5431 which point the store can be unconditional. */
5432 label = gen_label_rtx ();
5433 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
5434 GET_MODE (target), 1, label);
5435 emit_move_insn (expect, oldval);
5436 emit_label (label);
5438 return target;
5441 /* Expand the __atomic_load intrinsic:
5442 TYPE __atomic_load (TYPE *object, enum memmodel)
5443 EXP is the CALL_EXPR.
5444 TARGET is an optional place for us to store the results. */
5446 static rtx
5447 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
5449 rtx mem;
5450 enum memmodel model;
5452 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5453 if (is_mm_release (model) || is_mm_acq_rel (model))
5455 warning (OPT_Winvalid_memory_model,
5456 "invalid memory model for %<__atomic_load%>");
5457 model = MEMMODEL_SEQ_CST;
5460 if (!flag_inline_atomics)
5461 return NULL_RTX;
5463 /* Expand the operand. */
5464 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5466 return expand_atomic_load (target, mem, model);
5470 /* Expand the __atomic_store intrinsic:
5471 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5472 EXP is the CALL_EXPR.
5473 TARGET is an optional place for us to store the results. */
5475 static rtx
5476 expand_builtin_atomic_store (machine_mode mode, tree exp)
5478 rtx mem, val;
5479 enum memmodel model;
5481 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5482 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
5483 || is_mm_release (model)))
5485 warning (OPT_Winvalid_memory_model,
5486 "invalid memory model for %<__atomic_store%>");
5487 model = MEMMODEL_SEQ_CST;
5490 if (!flag_inline_atomics)
5491 return NULL_RTX;
5493 /* Expand the operands. */
5494 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5495 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5497 return expand_atomic_store (mem, val, model, false);
5500 /* Expand the __atomic_fetch_XXX intrinsic:
5501 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5502 EXP is the CALL_EXPR.
5503 TARGET is an optional place for us to store the results.
5504 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5505 FETCH_AFTER is true if returning the result of the operation.
5506 FETCH_AFTER is false if returning the value before the operation.
5507 IGNORE is true if the result is not used.
5508 EXT_CALL is the correct builtin for an external call if this cannot be
5509 resolved to an instruction sequence. */
5511 static rtx
5512 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
5513 enum rtx_code code, bool fetch_after,
5514 bool ignore, enum built_in_function ext_call)
5516 rtx val, mem, ret;
5517 enum memmodel model;
5518 tree fndecl;
5519 tree addr;
5521 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5523 /* Expand the operands. */
5524 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5525 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5527 /* Only try generating instructions if inlining is turned on. */
5528 if (flag_inline_atomics)
5530 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5531 if (ret)
5532 return ret;
5535 /* Return if a different routine isn't needed for the library call. */
5536 if (ext_call == BUILT_IN_NONE)
5537 return NULL_RTX;
5539 /* Change the call to the specified function. */
5540 fndecl = get_callee_fndecl (exp);
5541 addr = CALL_EXPR_FN (exp);
5542 STRIP_NOPS (addr);
5544 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5545 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5547 /* Expand the call here so we can emit trailing code. */
5548 ret = expand_call (exp, target, ignore);
5550 /* Replace the original function just in case it matters. */
5551 TREE_OPERAND (addr, 0) = fndecl;
5553 /* Then issue the arithmetic correction to return the right result. */
5554 if (!ignore)
5556 if (code == NOT)
5558 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5559 OPTAB_LIB_WIDEN);
5560 ret = expand_simple_unop (mode, NOT, ret, target, true);
5562 else
5563 ret = expand_simple_binop (mode, code, ret, val, target, true,
5564 OPTAB_LIB_WIDEN);
5566 return ret;
5569 /* Expand an atomic clear operation.
5570 void _atomic_clear (BOOL *obj, enum memmodel)
5571 EXP is the call expression. */
5573 static rtx
5574 expand_builtin_atomic_clear (tree exp)
5576 machine_mode mode;
5577 rtx mem, ret;
5578 enum memmodel model;
5580 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5581 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5582 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5584 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
5586 warning (OPT_Winvalid_memory_model,
5587 "invalid memory model for %<__atomic_store%>");
5588 model = MEMMODEL_SEQ_CST;
5591 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5592 Failing that, a store is issued by __atomic_store. The only way this can
5593 fail is if the bool type is larger than a word size. Unlikely, but
5594 handle it anyway for completeness. Assume a single threaded model since
5595 there is no atomic support in this case, and no barriers are required. */
5596 ret = expand_atomic_store (mem, const0_rtx, model, true);
5597 if (!ret)
5598 emit_move_insn (mem, const0_rtx);
5599 return const0_rtx;
5602 /* Expand an atomic test_and_set operation.
5603 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5604 EXP is the call expression. */
5606 static rtx
5607 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5609 rtx mem;
5610 enum memmodel model;
5611 machine_mode mode;
5613 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5614 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5615 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5617 return expand_atomic_test_and_set (target, mem, model);
5621 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5622 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5624 static tree
5625 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5627 int size;
5628 machine_mode mode;
5629 unsigned int mode_align, type_align;
5631 if (TREE_CODE (arg0) != INTEGER_CST)
5632 return NULL_TREE;
5634 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5635 mode = mode_for_size (size, MODE_INT, 0);
5636 mode_align = GET_MODE_ALIGNMENT (mode);
5638 if (TREE_CODE (arg1) == INTEGER_CST)
5640 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
5642 /* Either this argument is null, or it's a fake pointer encoding
5643 the alignment of the object. */
5644 val = val & -val;
5645 val *= BITS_PER_UNIT;
5647 if (val == 0 || mode_align < val)
5648 type_align = mode_align;
5649 else
5650 type_align = val;
5652 else
5654 tree ttype = TREE_TYPE (arg1);
5656 /* This function is usually invoked and folded immediately by the front
5657 end before anything else has a chance to look at it. The pointer
5658 parameter at this point is usually cast to a void *, so check for that
5659 and look past the cast. */
5660 if (CONVERT_EXPR_P (arg1) && POINTER_TYPE_P (ttype)
5661 && VOID_TYPE_P (TREE_TYPE (ttype)))
5662 arg1 = TREE_OPERAND (arg1, 0);
5664 ttype = TREE_TYPE (arg1);
5665 gcc_assert (POINTER_TYPE_P (ttype));
5667 /* Get the underlying type of the object. */
5668 ttype = TREE_TYPE (ttype);
5669 type_align = TYPE_ALIGN (ttype);
5672 /* If the object has smaller alignment, the lock free routines cannot
5673 be used. */
5674 if (type_align < mode_align)
5675 return boolean_false_node;
5677 /* Check if a compare_and_swap pattern exists for the mode which represents
5678 the required size. The pattern is not allowed to fail, so the existence
5679 of the pattern indicates support is present. */
5680 if (can_compare_and_swap_p (mode, true))
5681 return boolean_true_node;
5682 else
5683 return boolean_false_node;
5686 /* Return true if the parameters to call EXP represent an object which will
5687 always generate lock free instructions. The first argument represents the
5688 size of the object, and the second parameter is a pointer to the object
5689 itself. If NULL is passed for the object, then the result is based on
5690 typical alignment for an object of the specified size. Otherwise return
5691 false. */
5693 static rtx
5694 expand_builtin_atomic_always_lock_free (tree exp)
5696 tree size;
5697 tree arg0 = CALL_EXPR_ARG (exp, 0);
5698 tree arg1 = CALL_EXPR_ARG (exp, 1);
5700 if (TREE_CODE (arg0) != INTEGER_CST)
5702 error ("non-constant argument 1 to __atomic_always_lock_free");
5703 return const0_rtx;
5706 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5707 if (size == boolean_true_node)
5708 return const1_rtx;
5709 return const0_rtx;
5712 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5713 is lock free on this architecture. */
5715 static tree
5716 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5718 if (!flag_inline_atomics)
5719 return NULL_TREE;
5721 /* If it isn't always lock free, don't generate a result. */
5722 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5723 return boolean_true_node;
5725 return NULL_TREE;
5728 /* Return true if the parameters to call EXP represent an object which will
5729 always generate lock free instructions. The first argument represents the
5730 size of the object, and the second parameter is a pointer to the object
5731 itself. If NULL is passed for the object, then the result is based on
5732 typical alignment for an object of the specified size. Otherwise return
5733 NULL*/
5735 static rtx
5736 expand_builtin_atomic_is_lock_free (tree exp)
5738 tree size;
5739 tree arg0 = CALL_EXPR_ARG (exp, 0);
5740 tree arg1 = CALL_EXPR_ARG (exp, 1);
5742 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5744 error ("non-integer argument 1 to __atomic_is_lock_free");
5745 return NULL_RTX;
5748 if (!flag_inline_atomics)
5749 return NULL_RTX;
5751 /* If the value is known at compile time, return the RTX for it. */
5752 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5753 if (size == boolean_true_node)
5754 return const1_rtx;
5756 return NULL_RTX;
5759 /* Expand the __atomic_thread_fence intrinsic:
5760 void __atomic_thread_fence (enum memmodel)
5761 EXP is the CALL_EXPR. */
5763 static void
5764 expand_builtin_atomic_thread_fence (tree exp)
5766 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5767 expand_mem_thread_fence (model);
5770 /* Expand the __atomic_signal_fence intrinsic:
5771 void __atomic_signal_fence (enum memmodel)
5772 EXP is the CALL_EXPR. */
5774 static void
5775 expand_builtin_atomic_signal_fence (tree exp)
5777 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5778 expand_mem_signal_fence (model);
5781 /* Expand the __sync_synchronize intrinsic. */
5783 static void
5784 expand_builtin_sync_synchronize (void)
5786 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
5789 static rtx
5790 expand_builtin_thread_pointer (tree exp, rtx target)
5792 enum insn_code icode;
5793 if (!validate_arglist (exp, VOID_TYPE))
5794 return const0_rtx;
5795 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5796 if (icode != CODE_FOR_nothing)
5798 struct expand_operand op;
5799 /* If the target is not sutitable then create a new target. */
5800 if (target == NULL_RTX
5801 || !REG_P (target)
5802 || GET_MODE (target) != Pmode)
5803 target = gen_reg_rtx (Pmode);
5804 create_output_operand (&op, target, Pmode);
5805 expand_insn (icode, 1, &op);
5806 return target;
5808 error ("__builtin_thread_pointer is not supported on this target");
5809 return const0_rtx;
5812 static void
5813 expand_builtin_set_thread_pointer (tree exp)
5815 enum insn_code icode;
5816 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5817 return;
5818 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5819 if (icode != CODE_FOR_nothing)
5821 struct expand_operand op;
5822 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5823 Pmode, EXPAND_NORMAL);
5824 create_input_operand (&op, val, Pmode);
5825 expand_insn (icode, 1, &op);
5826 return;
5828 error ("__builtin_set_thread_pointer is not supported on this target");
5832 /* Emit code to restore the current value of stack. */
5834 static void
5835 expand_stack_restore (tree var)
5837 rtx_insn *prev;
5838 rtx sa = expand_normal (var);
5840 sa = convert_memory_address (Pmode, sa);
5842 prev = get_last_insn ();
5843 emit_stack_restore (SAVE_BLOCK, sa);
5845 record_new_stack_level ();
5847 fixup_args_size_notes (prev, get_last_insn (), 0);
5850 /* Emit code to save the current value of stack. */
5852 static rtx
5853 expand_stack_save (void)
5855 rtx ret = NULL_RTX;
5857 emit_stack_save (SAVE_BLOCK, &ret);
5858 return ret;
5862 /* Expand OpenACC acc_on_device.
5864 This has to happen late (that is, not in early folding; expand_builtin_*,
5865 rather than fold_builtin_*), as we have to act differently for host and
5866 acceleration device (ACCEL_COMPILER conditional). */
5868 static rtx
5869 expand_builtin_acc_on_device (tree exp, rtx target)
5871 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5872 return NULL_RTX;
5874 tree arg = CALL_EXPR_ARG (exp, 0);
5876 /* Return (arg == v1 || arg == v2) ? 1 : 0. */
5877 machine_mode v_mode = TYPE_MODE (TREE_TYPE (arg));
5878 rtx v = expand_normal (arg), v1, v2;
5879 #ifdef ACCEL_COMPILER
5880 v1 = GEN_INT (GOMP_DEVICE_NOT_HOST);
5881 v2 = GEN_INT (ACCEL_COMPILER_acc_device);
5882 #else
5883 v1 = GEN_INT (GOMP_DEVICE_NONE);
5884 v2 = GEN_INT (GOMP_DEVICE_HOST);
5885 #endif
5886 machine_mode target_mode = TYPE_MODE (integer_type_node);
5887 if (!target || !register_operand (target, target_mode))
5888 target = gen_reg_rtx (target_mode);
5889 emit_move_insn (target, const1_rtx);
5890 rtx_code_label *done_label = gen_label_rtx ();
5891 do_compare_rtx_and_jump (v, v1, EQ, false, v_mode, NULL_RTX,
5892 NULL, done_label, PROB_EVEN);
5893 do_compare_rtx_and_jump (v, v2, EQ, false, v_mode, NULL_RTX,
5894 NULL, done_label, PROB_EVEN);
5895 emit_move_insn (target, const0_rtx);
5896 emit_label (done_label);
5898 return target;
5902 /* Expand an expression EXP that calls a built-in function,
5903 with result going to TARGET if that's convenient
5904 (and in mode MODE if that's convenient).
5905 SUBTARGET may be used as the target for computing one of EXP's operands.
5906 IGNORE is nonzero if the value is to be ignored. */
5909 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
5910 int ignore)
5912 tree fndecl = get_callee_fndecl (exp);
5913 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5914 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5915 int flags;
5917 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5918 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5920 /* When ASan is enabled, we don't want to expand some memory/string
5921 builtins and rely on libsanitizer's hooks. This allows us to avoid
5922 redundant checks and be sure, that possible overflow will be detected
5923 by ASan. */
5925 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
5926 return expand_call (exp, target, ignore);
5928 /* When not optimizing, generate calls to library functions for a certain
5929 set of builtins. */
5930 if (!optimize
5931 && !called_as_built_in (fndecl)
5932 && fcode != BUILT_IN_FORK
5933 && fcode != BUILT_IN_EXECL
5934 && fcode != BUILT_IN_EXECV
5935 && fcode != BUILT_IN_EXECLP
5936 && fcode != BUILT_IN_EXECLE
5937 && fcode != BUILT_IN_EXECVP
5938 && fcode != BUILT_IN_EXECVE
5939 && fcode != BUILT_IN_ALLOCA
5940 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5941 && fcode != BUILT_IN_FREE
5942 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
5943 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
5944 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
5945 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
5946 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5947 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
5948 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
5949 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
5950 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
5951 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
5952 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND
5953 && fcode != BUILT_IN_CHKP_BNDRET)
5954 return expand_call (exp, target, ignore);
5956 /* The built-in function expanders test for target == const0_rtx
5957 to determine whether the function's result will be ignored. */
5958 if (ignore)
5959 target = const0_rtx;
5961 /* If the result of a pure or const built-in function is ignored, and
5962 none of its arguments are volatile, we can avoid expanding the
5963 built-in call and just evaluate the arguments for side-effects. */
5964 if (target == const0_rtx
5965 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5966 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5968 bool volatilep = false;
5969 tree arg;
5970 call_expr_arg_iterator iter;
5972 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5973 if (TREE_THIS_VOLATILE (arg))
5975 volatilep = true;
5976 break;
5979 if (! volatilep)
5981 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5982 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5983 return const0_rtx;
5987 /* expand_builtin_with_bounds is supposed to be used for
5988 instrumented builtin calls. */
5989 gcc_assert (!CALL_WITH_BOUNDS_P (exp));
5991 switch (fcode)
5993 CASE_FLT_FN (BUILT_IN_FABS):
5994 case BUILT_IN_FABSD32:
5995 case BUILT_IN_FABSD64:
5996 case BUILT_IN_FABSD128:
5997 target = expand_builtin_fabs (exp, target, subtarget);
5998 if (target)
5999 return target;
6000 break;
6002 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6003 target = expand_builtin_copysign (exp, target, subtarget);
6004 if (target)
6005 return target;
6006 break;
6008 /* Just do a normal library call if we were unable to fold
6009 the values. */
6010 CASE_FLT_FN (BUILT_IN_CABS):
6011 break;
6013 CASE_FLT_FN (BUILT_IN_EXP):
6014 CASE_FLT_FN (BUILT_IN_EXP10):
6015 CASE_FLT_FN (BUILT_IN_POW10):
6016 CASE_FLT_FN (BUILT_IN_EXP2):
6017 CASE_FLT_FN (BUILT_IN_EXPM1):
6018 CASE_FLT_FN (BUILT_IN_LOGB):
6019 CASE_FLT_FN (BUILT_IN_LOG):
6020 CASE_FLT_FN (BUILT_IN_LOG10):
6021 CASE_FLT_FN (BUILT_IN_LOG2):
6022 CASE_FLT_FN (BUILT_IN_LOG1P):
6023 CASE_FLT_FN (BUILT_IN_TAN):
6024 CASE_FLT_FN (BUILT_IN_ASIN):
6025 CASE_FLT_FN (BUILT_IN_ACOS):
6026 CASE_FLT_FN (BUILT_IN_ATAN):
6027 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
6028 /* Treat these like sqrt only if unsafe math optimizations are allowed,
6029 because of possible accuracy problems. */
6030 if (! flag_unsafe_math_optimizations)
6031 break;
6032 CASE_FLT_FN (BUILT_IN_SQRT):
6033 CASE_FLT_FN (BUILT_IN_FLOOR):
6034 CASE_FLT_FN (BUILT_IN_CEIL):
6035 CASE_FLT_FN (BUILT_IN_TRUNC):
6036 CASE_FLT_FN (BUILT_IN_ROUND):
6037 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6038 CASE_FLT_FN (BUILT_IN_RINT):
6039 target = expand_builtin_mathfn (exp, target, subtarget);
6040 if (target)
6041 return target;
6042 break;
6044 CASE_FLT_FN (BUILT_IN_FMA):
6045 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
6046 if (target)
6047 return target;
6048 break;
6050 CASE_FLT_FN (BUILT_IN_ILOGB):
6051 if (! flag_unsafe_math_optimizations)
6052 break;
6053 CASE_FLT_FN (BUILT_IN_ISINF):
6054 CASE_FLT_FN (BUILT_IN_FINITE):
6055 case BUILT_IN_ISFINITE:
6056 case BUILT_IN_ISNORMAL:
6057 target = expand_builtin_interclass_mathfn (exp, target);
6058 if (target)
6059 return target;
6060 break;
6062 CASE_FLT_FN (BUILT_IN_ICEIL):
6063 CASE_FLT_FN (BUILT_IN_LCEIL):
6064 CASE_FLT_FN (BUILT_IN_LLCEIL):
6065 CASE_FLT_FN (BUILT_IN_LFLOOR):
6066 CASE_FLT_FN (BUILT_IN_IFLOOR):
6067 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6068 target = expand_builtin_int_roundingfn (exp, target);
6069 if (target)
6070 return target;
6071 break;
6073 CASE_FLT_FN (BUILT_IN_IRINT):
6074 CASE_FLT_FN (BUILT_IN_LRINT):
6075 CASE_FLT_FN (BUILT_IN_LLRINT):
6076 CASE_FLT_FN (BUILT_IN_IROUND):
6077 CASE_FLT_FN (BUILT_IN_LROUND):
6078 CASE_FLT_FN (BUILT_IN_LLROUND):
6079 target = expand_builtin_int_roundingfn_2 (exp, target);
6080 if (target)
6081 return target;
6082 break;
6084 CASE_FLT_FN (BUILT_IN_POWI):
6085 target = expand_builtin_powi (exp, target);
6086 if (target)
6087 return target;
6088 break;
6090 CASE_FLT_FN (BUILT_IN_ATAN2):
6091 CASE_FLT_FN (BUILT_IN_LDEXP):
6092 CASE_FLT_FN (BUILT_IN_SCALB):
6093 CASE_FLT_FN (BUILT_IN_SCALBN):
6094 CASE_FLT_FN (BUILT_IN_SCALBLN):
6095 if (! flag_unsafe_math_optimizations)
6096 break;
6098 CASE_FLT_FN (BUILT_IN_FMOD):
6099 CASE_FLT_FN (BUILT_IN_REMAINDER):
6100 CASE_FLT_FN (BUILT_IN_DREM):
6101 CASE_FLT_FN (BUILT_IN_POW):
6102 target = expand_builtin_mathfn_2 (exp, target, subtarget);
6103 if (target)
6104 return target;
6105 break;
6107 CASE_FLT_FN (BUILT_IN_CEXPI):
6108 target = expand_builtin_cexpi (exp, target);
6109 gcc_assert (target);
6110 return target;
6112 CASE_FLT_FN (BUILT_IN_SIN):
6113 CASE_FLT_FN (BUILT_IN_COS):
6114 if (! flag_unsafe_math_optimizations)
6115 break;
6116 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6117 if (target)
6118 return target;
6119 break;
6121 CASE_FLT_FN (BUILT_IN_SINCOS):
6122 if (! flag_unsafe_math_optimizations)
6123 break;
6124 target = expand_builtin_sincos (exp);
6125 if (target)
6126 return target;
6127 break;
6129 case BUILT_IN_APPLY_ARGS:
6130 return expand_builtin_apply_args ();
6132 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6133 FUNCTION with a copy of the parameters described by
6134 ARGUMENTS, and ARGSIZE. It returns a block of memory
6135 allocated on the stack into which is stored all the registers
6136 that might possibly be used for returning the result of a
6137 function. ARGUMENTS is the value returned by
6138 __builtin_apply_args. ARGSIZE is the number of bytes of
6139 arguments that must be copied. ??? How should this value be
6140 computed? We'll also need a safe worst case value for varargs
6141 functions. */
6142 case BUILT_IN_APPLY:
6143 if (!validate_arglist (exp, POINTER_TYPE,
6144 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6145 && !validate_arglist (exp, REFERENCE_TYPE,
6146 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6147 return const0_rtx;
6148 else
6150 rtx ops[3];
6152 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6153 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6154 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6156 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6159 /* __builtin_return (RESULT) causes the function to return the
6160 value described by RESULT. RESULT is address of the block of
6161 memory returned by __builtin_apply. */
6162 case BUILT_IN_RETURN:
6163 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6164 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6165 return const0_rtx;
6167 case BUILT_IN_SAVEREGS:
6168 return expand_builtin_saveregs ();
6170 case BUILT_IN_VA_ARG_PACK:
6171 /* All valid uses of __builtin_va_arg_pack () are removed during
6172 inlining. */
6173 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6174 return const0_rtx;
6176 case BUILT_IN_VA_ARG_PACK_LEN:
6177 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6178 inlining. */
6179 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6180 return const0_rtx;
6182 /* Return the address of the first anonymous stack arg. */
6183 case BUILT_IN_NEXT_ARG:
6184 if (fold_builtin_next_arg (exp, false))
6185 return const0_rtx;
6186 return expand_builtin_next_arg ();
6188 case BUILT_IN_CLEAR_CACHE:
6189 target = expand_builtin___clear_cache (exp);
6190 if (target)
6191 return target;
6192 break;
6194 case BUILT_IN_CLASSIFY_TYPE:
6195 return expand_builtin_classify_type (exp);
6197 case BUILT_IN_CONSTANT_P:
6198 return const0_rtx;
6200 case BUILT_IN_FRAME_ADDRESS:
6201 case BUILT_IN_RETURN_ADDRESS:
6202 return expand_builtin_frame_address (fndecl, exp);
6204 /* Returns the address of the area where the structure is returned.
6205 0 otherwise. */
6206 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6207 if (call_expr_nargs (exp) != 0
6208 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6209 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6210 return const0_rtx;
6211 else
6212 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6214 case BUILT_IN_ALLOCA:
6215 case BUILT_IN_ALLOCA_WITH_ALIGN:
6216 /* If the allocation stems from the declaration of a variable-sized
6217 object, it cannot accumulate. */
6218 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6219 if (target)
6220 return target;
6221 break;
6223 case BUILT_IN_STACK_SAVE:
6224 return expand_stack_save ();
6226 case BUILT_IN_STACK_RESTORE:
6227 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6228 return const0_rtx;
6230 case BUILT_IN_BSWAP16:
6231 case BUILT_IN_BSWAP32:
6232 case BUILT_IN_BSWAP64:
6233 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6234 if (target)
6235 return target;
6236 break;
6238 CASE_INT_FN (BUILT_IN_FFS):
6239 target = expand_builtin_unop (target_mode, exp, target,
6240 subtarget, ffs_optab);
6241 if (target)
6242 return target;
6243 break;
6245 CASE_INT_FN (BUILT_IN_CLZ):
6246 target = expand_builtin_unop (target_mode, exp, target,
6247 subtarget, clz_optab);
6248 if (target)
6249 return target;
6250 break;
6252 CASE_INT_FN (BUILT_IN_CTZ):
6253 target = expand_builtin_unop (target_mode, exp, target,
6254 subtarget, ctz_optab);
6255 if (target)
6256 return target;
6257 break;
6259 CASE_INT_FN (BUILT_IN_CLRSB):
6260 target = expand_builtin_unop (target_mode, exp, target,
6261 subtarget, clrsb_optab);
6262 if (target)
6263 return target;
6264 break;
6266 CASE_INT_FN (BUILT_IN_POPCOUNT):
6267 target = expand_builtin_unop (target_mode, exp, target,
6268 subtarget, popcount_optab);
6269 if (target)
6270 return target;
6271 break;
6273 CASE_INT_FN (BUILT_IN_PARITY):
6274 target = expand_builtin_unop (target_mode, exp, target,
6275 subtarget, parity_optab);
6276 if (target)
6277 return target;
6278 break;
6280 case BUILT_IN_STRLEN:
6281 target = expand_builtin_strlen (exp, target, target_mode);
6282 if (target)
6283 return target;
6284 break;
6286 case BUILT_IN_STRCPY:
6287 target = expand_builtin_strcpy (exp, target);
6288 if (target)
6289 return target;
6290 break;
6292 case BUILT_IN_STRNCPY:
6293 target = expand_builtin_strncpy (exp, target);
6294 if (target)
6295 return target;
6296 break;
6298 case BUILT_IN_STPCPY:
6299 target = expand_builtin_stpcpy (exp, target, mode);
6300 if (target)
6301 return target;
6302 break;
6304 case BUILT_IN_MEMCPY:
6305 target = expand_builtin_memcpy (exp, target);
6306 if (target)
6307 return target;
6308 break;
6310 case BUILT_IN_MEMPCPY:
6311 target = expand_builtin_mempcpy (exp, target, mode);
6312 if (target)
6313 return target;
6314 break;
6316 case BUILT_IN_MEMSET:
6317 target = expand_builtin_memset (exp, target, mode);
6318 if (target)
6319 return target;
6320 break;
6322 case BUILT_IN_BZERO:
6323 target = expand_builtin_bzero (exp);
6324 if (target)
6325 return target;
6326 break;
6328 case BUILT_IN_STRCMP:
6329 target = expand_builtin_strcmp (exp, target);
6330 if (target)
6331 return target;
6332 break;
6334 case BUILT_IN_STRNCMP:
6335 target = expand_builtin_strncmp (exp, target, mode);
6336 if (target)
6337 return target;
6338 break;
6340 case BUILT_IN_BCMP:
6341 case BUILT_IN_MEMCMP:
6342 target = expand_builtin_memcmp (exp, target);
6343 if (target)
6344 return target;
6345 break;
6347 case BUILT_IN_SETJMP:
6348 /* This should have been lowered to the builtins below. */
6349 gcc_unreachable ();
6351 case BUILT_IN_SETJMP_SETUP:
6352 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6353 and the receiver label. */
6354 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6356 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6357 VOIDmode, EXPAND_NORMAL);
6358 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6359 rtx_insn *label_r = label_rtx (label);
6361 /* This is copied from the handling of non-local gotos. */
6362 expand_builtin_setjmp_setup (buf_addr, label_r);
6363 nonlocal_goto_handler_labels
6364 = gen_rtx_INSN_LIST (VOIDmode, label_r,
6365 nonlocal_goto_handler_labels);
6366 /* ??? Do not let expand_label treat us as such since we would
6367 not want to be both on the list of non-local labels and on
6368 the list of forced labels. */
6369 FORCED_LABEL (label) = 0;
6370 return const0_rtx;
6372 break;
6374 case BUILT_IN_SETJMP_RECEIVER:
6375 /* __builtin_setjmp_receiver is passed the receiver label. */
6376 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6378 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6379 rtx_insn *label_r = label_rtx (label);
6381 expand_builtin_setjmp_receiver (label_r);
6382 return const0_rtx;
6384 break;
6386 /* __builtin_longjmp is passed a pointer to an array of five words.
6387 It's similar to the C library longjmp function but works with
6388 __builtin_setjmp above. */
6389 case BUILT_IN_LONGJMP:
6390 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6392 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6393 VOIDmode, EXPAND_NORMAL);
6394 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6396 if (value != const1_rtx)
6398 error ("%<__builtin_longjmp%> second argument must be 1");
6399 return const0_rtx;
6402 expand_builtin_longjmp (buf_addr, value);
6403 return const0_rtx;
6405 break;
6407 case BUILT_IN_NONLOCAL_GOTO:
6408 target = expand_builtin_nonlocal_goto (exp);
6409 if (target)
6410 return target;
6411 break;
6413 /* This updates the setjmp buffer that is its argument with the value
6414 of the current stack pointer. */
6415 case BUILT_IN_UPDATE_SETJMP_BUF:
6416 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6418 rtx buf_addr
6419 = expand_normal (CALL_EXPR_ARG (exp, 0));
6421 expand_builtin_update_setjmp_buf (buf_addr);
6422 return const0_rtx;
6424 break;
6426 case BUILT_IN_TRAP:
6427 expand_builtin_trap ();
6428 return const0_rtx;
6430 case BUILT_IN_UNREACHABLE:
6431 expand_builtin_unreachable ();
6432 return const0_rtx;
6434 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6435 case BUILT_IN_SIGNBITD32:
6436 case BUILT_IN_SIGNBITD64:
6437 case BUILT_IN_SIGNBITD128:
6438 target = expand_builtin_signbit (exp, target);
6439 if (target)
6440 return target;
6441 break;
6443 /* Various hooks for the DWARF 2 __throw routine. */
6444 case BUILT_IN_UNWIND_INIT:
6445 expand_builtin_unwind_init ();
6446 return const0_rtx;
6447 case BUILT_IN_DWARF_CFA:
6448 return virtual_cfa_rtx;
6449 #ifdef DWARF2_UNWIND_INFO
6450 case BUILT_IN_DWARF_SP_COLUMN:
6451 return expand_builtin_dwarf_sp_column ();
6452 case BUILT_IN_INIT_DWARF_REG_SIZES:
6453 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6454 return const0_rtx;
6455 #endif
6456 case BUILT_IN_FROB_RETURN_ADDR:
6457 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6458 case BUILT_IN_EXTRACT_RETURN_ADDR:
6459 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6460 case BUILT_IN_EH_RETURN:
6461 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6462 CALL_EXPR_ARG (exp, 1));
6463 return const0_rtx;
6464 case BUILT_IN_EH_RETURN_DATA_REGNO:
6465 return expand_builtin_eh_return_data_regno (exp);
6466 case BUILT_IN_EXTEND_POINTER:
6467 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6468 case BUILT_IN_EH_POINTER:
6469 return expand_builtin_eh_pointer (exp);
6470 case BUILT_IN_EH_FILTER:
6471 return expand_builtin_eh_filter (exp);
6472 case BUILT_IN_EH_COPY_VALUES:
6473 return expand_builtin_eh_copy_values (exp);
6475 case BUILT_IN_VA_START:
6476 return expand_builtin_va_start (exp);
6477 case BUILT_IN_VA_END:
6478 return expand_builtin_va_end (exp);
6479 case BUILT_IN_VA_COPY:
6480 return expand_builtin_va_copy (exp);
6481 case BUILT_IN_EXPECT:
6482 return expand_builtin_expect (exp, target);
6483 case BUILT_IN_ASSUME_ALIGNED:
6484 return expand_builtin_assume_aligned (exp, target);
6485 case BUILT_IN_PREFETCH:
6486 expand_builtin_prefetch (exp);
6487 return const0_rtx;
6489 case BUILT_IN_INIT_TRAMPOLINE:
6490 return expand_builtin_init_trampoline (exp, true);
6491 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6492 return expand_builtin_init_trampoline (exp, false);
6493 case BUILT_IN_ADJUST_TRAMPOLINE:
6494 return expand_builtin_adjust_trampoline (exp);
6496 case BUILT_IN_FORK:
6497 case BUILT_IN_EXECL:
6498 case BUILT_IN_EXECV:
6499 case BUILT_IN_EXECLP:
6500 case BUILT_IN_EXECLE:
6501 case BUILT_IN_EXECVP:
6502 case BUILT_IN_EXECVE:
6503 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6504 if (target)
6505 return target;
6506 break;
6508 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6509 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6510 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6511 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6512 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6513 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6514 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6515 if (target)
6516 return target;
6517 break;
6519 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6520 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6521 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6522 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6523 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6524 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6525 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6526 if (target)
6527 return target;
6528 break;
6530 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6531 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6532 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6533 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6534 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6535 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6536 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6537 if (target)
6538 return target;
6539 break;
6541 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6542 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6543 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6544 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6545 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6546 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6547 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6548 if (target)
6549 return target;
6550 break;
6552 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6553 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6554 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6555 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6556 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6557 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6558 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6559 if (target)
6560 return target;
6561 break;
6563 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6564 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6565 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6566 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6567 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6568 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6569 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6570 if (target)
6571 return target;
6572 break;
6574 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6575 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6576 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6577 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6578 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6579 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6580 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6581 if (target)
6582 return target;
6583 break;
6585 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6586 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6587 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6588 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6589 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6590 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6591 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6592 if (target)
6593 return target;
6594 break;
6596 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6597 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6598 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6599 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6600 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6601 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6602 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6603 if (target)
6604 return target;
6605 break;
6607 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6608 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6609 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6610 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6611 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6612 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6613 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6614 if (target)
6615 return target;
6616 break;
6618 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6619 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6620 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6621 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6622 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6623 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6624 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6625 if (target)
6626 return target;
6627 break;
6629 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6630 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6631 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6632 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6633 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6634 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6635 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6636 if (target)
6637 return target;
6638 break;
6640 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6641 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6642 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6643 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6644 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6645 if (mode == VOIDmode)
6646 mode = TYPE_MODE (boolean_type_node);
6647 if (!target || !register_operand (target, mode))
6648 target = gen_reg_rtx (mode);
6650 mode = get_builtin_sync_mode
6651 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6652 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6653 if (target)
6654 return target;
6655 break;
6657 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6658 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6659 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6660 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6661 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6662 mode = get_builtin_sync_mode
6663 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6664 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6665 if (target)
6666 return target;
6667 break;
6669 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6670 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6671 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6672 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6673 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6674 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6675 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6676 if (target)
6677 return target;
6678 break;
6680 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6681 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6682 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6683 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6684 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6685 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6686 expand_builtin_sync_lock_release (mode, exp);
6687 return const0_rtx;
6689 case BUILT_IN_SYNC_SYNCHRONIZE:
6690 expand_builtin_sync_synchronize ();
6691 return const0_rtx;
6693 case BUILT_IN_ATOMIC_EXCHANGE_1:
6694 case BUILT_IN_ATOMIC_EXCHANGE_2:
6695 case BUILT_IN_ATOMIC_EXCHANGE_4:
6696 case BUILT_IN_ATOMIC_EXCHANGE_8:
6697 case BUILT_IN_ATOMIC_EXCHANGE_16:
6698 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6699 target = expand_builtin_atomic_exchange (mode, exp, target);
6700 if (target)
6701 return target;
6702 break;
6704 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6705 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6706 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6707 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6708 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6710 unsigned int nargs, z;
6711 vec<tree, va_gc> *vec;
6713 mode =
6714 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6715 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6716 if (target)
6717 return target;
6719 /* If this is turned into an external library call, the weak parameter
6720 must be dropped to match the expected parameter list. */
6721 nargs = call_expr_nargs (exp);
6722 vec_alloc (vec, nargs - 1);
6723 for (z = 0; z < 3; z++)
6724 vec->quick_push (CALL_EXPR_ARG (exp, z));
6725 /* Skip the boolean weak parameter. */
6726 for (z = 4; z < 6; z++)
6727 vec->quick_push (CALL_EXPR_ARG (exp, z));
6728 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6729 break;
6732 case BUILT_IN_ATOMIC_LOAD_1:
6733 case BUILT_IN_ATOMIC_LOAD_2:
6734 case BUILT_IN_ATOMIC_LOAD_4:
6735 case BUILT_IN_ATOMIC_LOAD_8:
6736 case BUILT_IN_ATOMIC_LOAD_16:
6737 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6738 target = expand_builtin_atomic_load (mode, exp, target);
6739 if (target)
6740 return target;
6741 break;
6743 case BUILT_IN_ATOMIC_STORE_1:
6744 case BUILT_IN_ATOMIC_STORE_2:
6745 case BUILT_IN_ATOMIC_STORE_4:
6746 case BUILT_IN_ATOMIC_STORE_8:
6747 case BUILT_IN_ATOMIC_STORE_16:
6748 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6749 target = expand_builtin_atomic_store (mode, exp);
6750 if (target)
6751 return const0_rtx;
6752 break;
6754 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6755 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6756 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6757 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6758 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6760 enum built_in_function lib;
6761 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6762 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6763 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6764 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6765 ignore, lib);
6766 if (target)
6767 return target;
6768 break;
6770 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6771 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6772 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6773 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6774 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6776 enum built_in_function lib;
6777 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6778 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6779 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6780 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6781 ignore, lib);
6782 if (target)
6783 return target;
6784 break;
6786 case BUILT_IN_ATOMIC_AND_FETCH_1:
6787 case BUILT_IN_ATOMIC_AND_FETCH_2:
6788 case BUILT_IN_ATOMIC_AND_FETCH_4:
6789 case BUILT_IN_ATOMIC_AND_FETCH_8:
6790 case BUILT_IN_ATOMIC_AND_FETCH_16:
6792 enum built_in_function lib;
6793 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6794 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6795 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6796 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6797 ignore, lib);
6798 if (target)
6799 return target;
6800 break;
6802 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6803 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6804 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6805 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6806 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6808 enum built_in_function lib;
6809 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6810 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6811 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6812 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6813 ignore, lib);
6814 if (target)
6815 return target;
6816 break;
6818 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6819 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6820 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6821 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6822 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6824 enum built_in_function lib;
6825 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6826 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6827 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6828 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6829 ignore, lib);
6830 if (target)
6831 return target;
6832 break;
6834 case BUILT_IN_ATOMIC_OR_FETCH_1:
6835 case BUILT_IN_ATOMIC_OR_FETCH_2:
6836 case BUILT_IN_ATOMIC_OR_FETCH_4:
6837 case BUILT_IN_ATOMIC_OR_FETCH_8:
6838 case BUILT_IN_ATOMIC_OR_FETCH_16:
6840 enum built_in_function lib;
6841 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6842 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6843 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6844 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6845 ignore, lib);
6846 if (target)
6847 return target;
6848 break;
6850 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6851 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6852 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6853 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6854 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6855 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6856 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6857 ignore, BUILT_IN_NONE);
6858 if (target)
6859 return target;
6860 break;
6862 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6863 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6864 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6865 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6866 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6867 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6868 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6869 ignore, BUILT_IN_NONE);
6870 if (target)
6871 return target;
6872 break;
6874 case BUILT_IN_ATOMIC_FETCH_AND_1:
6875 case BUILT_IN_ATOMIC_FETCH_AND_2:
6876 case BUILT_IN_ATOMIC_FETCH_AND_4:
6877 case BUILT_IN_ATOMIC_FETCH_AND_8:
6878 case BUILT_IN_ATOMIC_FETCH_AND_16:
6879 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6880 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6881 ignore, BUILT_IN_NONE);
6882 if (target)
6883 return target;
6884 break;
6886 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6887 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6888 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6889 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6890 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6891 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6892 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6893 ignore, BUILT_IN_NONE);
6894 if (target)
6895 return target;
6896 break;
6898 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6899 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6900 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6901 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6902 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6903 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6904 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6905 ignore, BUILT_IN_NONE);
6906 if (target)
6907 return target;
6908 break;
6910 case BUILT_IN_ATOMIC_FETCH_OR_1:
6911 case BUILT_IN_ATOMIC_FETCH_OR_2:
6912 case BUILT_IN_ATOMIC_FETCH_OR_4:
6913 case BUILT_IN_ATOMIC_FETCH_OR_8:
6914 case BUILT_IN_ATOMIC_FETCH_OR_16:
6915 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6916 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6917 ignore, BUILT_IN_NONE);
6918 if (target)
6919 return target;
6920 break;
6922 case BUILT_IN_ATOMIC_TEST_AND_SET:
6923 return expand_builtin_atomic_test_and_set (exp, target);
6925 case BUILT_IN_ATOMIC_CLEAR:
6926 return expand_builtin_atomic_clear (exp);
6928 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6929 return expand_builtin_atomic_always_lock_free (exp);
6931 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6932 target = expand_builtin_atomic_is_lock_free (exp);
6933 if (target)
6934 return target;
6935 break;
6937 case BUILT_IN_ATOMIC_THREAD_FENCE:
6938 expand_builtin_atomic_thread_fence (exp);
6939 return const0_rtx;
6941 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6942 expand_builtin_atomic_signal_fence (exp);
6943 return const0_rtx;
6945 case BUILT_IN_OBJECT_SIZE:
6946 return expand_builtin_object_size (exp);
6948 case BUILT_IN_MEMCPY_CHK:
6949 case BUILT_IN_MEMPCPY_CHK:
6950 case BUILT_IN_MEMMOVE_CHK:
6951 case BUILT_IN_MEMSET_CHK:
6952 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6953 if (target)
6954 return target;
6955 break;
6957 case BUILT_IN_STRCPY_CHK:
6958 case BUILT_IN_STPCPY_CHK:
6959 case BUILT_IN_STRNCPY_CHK:
6960 case BUILT_IN_STPNCPY_CHK:
6961 case BUILT_IN_STRCAT_CHK:
6962 case BUILT_IN_STRNCAT_CHK:
6963 case BUILT_IN_SNPRINTF_CHK:
6964 case BUILT_IN_VSNPRINTF_CHK:
6965 maybe_emit_chk_warning (exp, fcode);
6966 break;
6968 case BUILT_IN_SPRINTF_CHK:
6969 case BUILT_IN_VSPRINTF_CHK:
6970 maybe_emit_sprintf_chk_warning (exp, fcode);
6971 break;
6973 case BUILT_IN_FREE:
6974 if (warn_free_nonheap_object)
6975 maybe_emit_free_warning (exp);
6976 break;
6978 case BUILT_IN_THREAD_POINTER:
6979 return expand_builtin_thread_pointer (exp, target);
6981 case BUILT_IN_SET_THREAD_POINTER:
6982 expand_builtin_set_thread_pointer (exp);
6983 return const0_rtx;
6985 case BUILT_IN_CILK_DETACH:
6986 expand_builtin_cilk_detach (exp);
6987 return const0_rtx;
6989 case BUILT_IN_CILK_POP_FRAME:
6990 expand_builtin_cilk_pop_frame (exp);
6991 return const0_rtx;
6993 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
6994 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
6995 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
6996 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
6997 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
6998 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
6999 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
7000 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
7001 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
7002 case BUILT_IN_CHKP_GET_PTR_LBOUND:
7003 case BUILT_IN_CHKP_GET_PTR_UBOUND:
7004 /* We allow user CHKP builtins if Pointer Bounds
7005 Checker is off. */
7006 if (!chkp_function_instrumented_p (current_function_decl))
7008 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
7009 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS
7010 || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS
7011 || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS
7012 || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS)
7013 return expand_normal (CALL_EXPR_ARG (exp, 0));
7014 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
7015 return expand_normal (size_zero_node);
7016 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
7017 return expand_normal (size_int (-1));
7018 else
7019 return const0_rtx;
7021 /* FALLTHROUGH */
7023 case BUILT_IN_CHKP_BNDMK:
7024 case BUILT_IN_CHKP_BNDSTX:
7025 case BUILT_IN_CHKP_BNDCL:
7026 case BUILT_IN_CHKP_BNDCU:
7027 case BUILT_IN_CHKP_BNDLDX:
7028 case BUILT_IN_CHKP_BNDRET:
7029 case BUILT_IN_CHKP_INTERSECT:
7030 case BUILT_IN_CHKP_NARROW:
7031 case BUILT_IN_CHKP_EXTRACT_LOWER:
7032 case BUILT_IN_CHKP_EXTRACT_UPPER:
7033 /* Software implementation of Pointer Bounds Checker is NYI.
7034 Target support is required. */
7035 error ("Your target platform does not support -fcheck-pointer-bounds");
7036 break;
7038 case BUILT_IN_ACC_ON_DEVICE:
7039 target = expand_builtin_acc_on_device (exp, target);
7040 if (target)
7041 return target;
7042 break;
7044 default: /* just do library call, if unknown builtin */
7045 break;
7048 /* The switch statement above can drop through to cause the function
7049 to be called normally. */
7050 return expand_call (exp, target, ignore);
7053 /* Similar to expand_builtin but is used for instrumented calls. */
7056 expand_builtin_with_bounds (tree exp, rtx target,
7057 rtx subtarget ATTRIBUTE_UNUSED,
7058 machine_mode mode, int ignore)
7060 tree fndecl = get_callee_fndecl (exp);
7061 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7063 gcc_assert (CALL_WITH_BOUNDS_P (exp));
7065 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7066 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7068 gcc_assert (fcode > BEGIN_CHKP_BUILTINS
7069 && fcode < END_CHKP_BUILTINS);
7071 switch (fcode)
7073 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP:
7074 target = expand_builtin_memcpy_with_bounds (exp, target);
7075 if (target)
7076 return target;
7077 break;
7079 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP:
7080 target = expand_builtin_mempcpy_with_bounds (exp, target, mode);
7081 if (target)
7082 return target;
7083 break;
7085 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP:
7086 target = expand_builtin_memset_with_bounds (exp, target, mode);
7087 if (target)
7088 return target;
7089 break;
7091 default:
7092 break;
7095 /* The switch statement above can drop through to cause the function
7096 to be called normally. */
7097 return expand_call (exp, target, ignore);
7100 /* Determine whether a tree node represents a call to a built-in
7101 function. If the tree T is a call to a built-in function with
7102 the right number of arguments of the appropriate types, return
7103 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7104 Otherwise the return value is END_BUILTINS. */
7106 enum built_in_function
7107 builtin_mathfn_code (const_tree t)
7109 const_tree fndecl, arg, parmlist;
7110 const_tree argtype, parmtype;
7111 const_call_expr_arg_iterator iter;
7113 if (TREE_CODE (t) != CALL_EXPR
7114 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
7115 return END_BUILTINS;
7117 fndecl = get_callee_fndecl (t);
7118 if (fndecl == NULL_TREE
7119 || TREE_CODE (fndecl) != FUNCTION_DECL
7120 || ! DECL_BUILT_IN (fndecl)
7121 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7122 return END_BUILTINS;
7124 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7125 init_const_call_expr_arg_iterator (t, &iter);
7126 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7128 /* If a function doesn't take a variable number of arguments,
7129 the last element in the list will have type `void'. */
7130 parmtype = TREE_VALUE (parmlist);
7131 if (VOID_TYPE_P (parmtype))
7133 if (more_const_call_expr_args_p (&iter))
7134 return END_BUILTINS;
7135 return DECL_FUNCTION_CODE (fndecl);
7138 if (! more_const_call_expr_args_p (&iter))
7139 return END_BUILTINS;
7141 arg = next_const_call_expr_arg (&iter);
7142 argtype = TREE_TYPE (arg);
7144 if (SCALAR_FLOAT_TYPE_P (parmtype))
7146 if (! SCALAR_FLOAT_TYPE_P (argtype))
7147 return END_BUILTINS;
7149 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7151 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7152 return END_BUILTINS;
7154 else if (POINTER_TYPE_P (parmtype))
7156 if (! POINTER_TYPE_P (argtype))
7157 return END_BUILTINS;
7159 else if (INTEGRAL_TYPE_P (parmtype))
7161 if (! INTEGRAL_TYPE_P (argtype))
7162 return END_BUILTINS;
7164 else
7165 return END_BUILTINS;
7168 /* Variable-length argument list. */
7169 return DECL_FUNCTION_CODE (fndecl);
7172 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7173 evaluate to a constant. */
7175 static tree
7176 fold_builtin_constant_p (tree arg)
7178 /* We return 1 for a numeric type that's known to be a constant
7179 value at compile-time or for an aggregate type that's a
7180 literal constant. */
7181 STRIP_NOPS (arg);
7183 /* If we know this is a constant, emit the constant of one. */
7184 if (CONSTANT_CLASS_P (arg)
7185 || (TREE_CODE (arg) == CONSTRUCTOR
7186 && TREE_CONSTANT (arg)))
7187 return integer_one_node;
7188 if (TREE_CODE (arg) == ADDR_EXPR)
7190 tree op = TREE_OPERAND (arg, 0);
7191 if (TREE_CODE (op) == STRING_CST
7192 || (TREE_CODE (op) == ARRAY_REF
7193 && integer_zerop (TREE_OPERAND (op, 1))
7194 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7195 return integer_one_node;
7198 /* If this expression has side effects, show we don't know it to be a
7199 constant. Likewise if it's a pointer or aggregate type since in
7200 those case we only want literals, since those are only optimized
7201 when generating RTL, not later.
7202 And finally, if we are compiling an initializer, not code, we
7203 need to return a definite result now; there's not going to be any
7204 more optimization done. */
7205 if (TREE_SIDE_EFFECTS (arg)
7206 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7207 || POINTER_TYPE_P (TREE_TYPE (arg))
7208 || cfun == 0
7209 || folding_initializer
7210 || force_folding_builtin_constant_p)
7211 return integer_zero_node;
7213 return NULL_TREE;
7216 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7217 return it as a truthvalue. */
7219 static tree
7220 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
7221 tree predictor)
7223 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7225 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
7226 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7227 ret_type = TREE_TYPE (TREE_TYPE (fn));
7228 pred_type = TREE_VALUE (arg_types);
7229 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7231 pred = fold_convert_loc (loc, pred_type, pred);
7232 expected = fold_convert_loc (loc, expected_type, expected);
7233 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
7234 predictor);
7236 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7237 build_int_cst (ret_type, 0));
7240 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7241 NULL_TREE if no simplification is possible. */
7243 tree
7244 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
7246 tree inner, fndecl, inner_arg0;
7247 enum tree_code code;
7249 /* Distribute the expected value over short-circuiting operators.
7250 See through the cast from truthvalue_type_node to long. */
7251 inner_arg0 = arg0;
7252 while (CONVERT_EXPR_P (inner_arg0)
7253 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7254 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7255 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7257 /* If this is a builtin_expect within a builtin_expect keep the
7258 inner one. See through a comparison against a constant. It
7259 might have been added to create a thruthvalue. */
7260 inner = inner_arg0;
7262 if (COMPARISON_CLASS_P (inner)
7263 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7264 inner = TREE_OPERAND (inner, 0);
7266 if (TREE_CODE (inner) == CALL_EXPR
7267 && (fndecl = get_callee_fndecl (inner))
7268 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7269 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7270 return arg0;
7272 inner = inner_arg0;
7273 code = TREE_CODE (inner);
7274 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7276 tree op0 = TREE_OPERAND (inner, 0);
7277 tree op1 = TREE_OPERAND (inner, 1);
7279 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
7280 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
7281 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7283 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7286 /* If the argument isn't invariant then there's nothing else we can do. */
7287 if (!TREE_CONSTANT (inner_arg0))
7288 return NULL_TREE;
7290 /* If we expect that a comparison against the argument will fold to
7291 a constant return the constant. In practice, this means a true
7292 constant or the address of a non-weak symbol. */
7293 inner = inner_arg0;
7294 STRIP_NOPS (inner);
7295 if (TREE_CODE (inner) == ADDR_EXPR)
7299 inner = TREE_OPERAND (inner, 0);
7301 while (TREE_CODE (inner) == COMPONENT_REF
7302 || TREE_CODE (inner) == ARRAY_REF);
7303 if ((TREE_CODE (inner) == VAR_DECL
7304 || TREE_CODE (inner) == FUNCTION_DECL)
7305 && DECL_WEAK (inner))
7306 return NULL_TREE;
7309 /* Otherwise, ARG0 already has the proper type for the return value. */
7310 return arg0;
7313 /* Fold a call to __builtin_classify_type with argument ARG. */
7315 static tree
7316 fold_builtin_classify_type (tree arg)
7318 if (arg == 0)
7319 return build_int_cst (integer_type_node, no_type_class);
7321 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7324 /* Fold a call to __builtin_strlen with argument ARG. */
7326 static tree
7327 fold_builtin_strlen (location_t loc, tree type, tree arg)
7329 if (!validate_arg (arg, POINTER_TYPE))
7330 return NULL_TREE;
7331 else
7333 tree len = c_strlen (arg, 0);
7335 if (len)
7336 return fold_convert_loc (loc, type, len);
7338 return NULL_TREE;
7342 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7344 static tree
7345 fold_builtin_inf (location_t loc, tree type, int warn)
7347 REAL_VALUE_TYPE real;
7349 /* __builtin_inff is intended to be usable to define INFINITY on all
7350 targets. If an infinity is not available, INFINITY expands "to a
7351 positive constant of type float that overflows at translation
7352 time", footnote "In this case, using INFINITY will violate the
7353 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7354 Thus we pedwarn to ensure this constraint violation is
7355 diagnosed. */
7356 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7357 pedwarn (loc, 0, "target format does not support infinity");
7359 real_inf (&real);
7360 return build_real (type, real);
7363 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7365 static tree
7366 fold_builtin_nan (tree arg, tree type, int quiet)
7368 REAL_VALUE_TYPE real;
7369 const char *str;
7371 if (!validate_arg (arg, POINTER_TYPE))
7372 return NULL_TREE;
7373 str = c_getstr (arg);
7374 if (!str)
7375 return NULL_TREE;
7377 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7378 return NULL_TREE;
7380 return build_real (type, real);
7383 /* Return true if the floating point expression T has an integer value.
7384 We also allow +Inf, -Inf and NaN to be considered integer values. */
7386 static bool
7387 integer_valued_real_p (tree t)
7389 switch (TREE_CODE (t))
7391 case FLOAT_EXPR:
7392 return true;
7394 case ABS_EXPR:
7395 case SAVE_EXPR:
7396 return integer_valued_real_p (TREE_OPERAND (t, 0));
7398 case COMPOUND_EXPR:
7399 case MODIFY_EXPR:
7400 case BIND_EXPR:
7401 return integer_valued_real_p (TREE_OPERAND (t, 1));
7403 case PLUS_EXPR:
7404 case MINUS_EXPR:
7405 case MULT_EXPR:
7406 case MIN_EXPR:
7407 case MAX_EXPR:
7408 return integer_valued_real_p (TREE_OPERAND (t, 0))
7409 && integer_valued_real_p (TREE_OPERAND (t, 1));
7411 case COND_EXPR:
7412 return integer_valued_real_p (TREE_OPERAND (t, 1))
7413 && integer_valued_real_p (TREE_OPERAND (t, 2));
7415 case REAL_CST:
7416 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7418 CASE_CONVERT:
7420 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7421 if (TREE_CODE (type) == INTEGER_TYPE)
7422 return true;
7423 if (TREE_CODE (type) == REAL_TYPE)
7424 return integer_valued_real_p (TREE_OPERAND (t, 0));
7425 break;
7428 case CALL_EXPR:
7429 switch (builtin_mathfn_code (t))
7431 CASE_FLT_FN (BUILT_IN_CEIL):
7432 CASE_FLT_FN (BUILT_IN_FLOOR):
7433 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7434 CASE_FLT_FN (BUILT_IN_RINT):
7435 CASE_FLT_FN (BUILT_IN_ROUND):
7436 CASE_FLT_FN (BUILT_IN_TRUNC):
7437 return true;
7439 CASE_FLT_FN (BUILT_IN_FMIN):
7440 CASE_FLT_FN (BUILT_IN_FMAX):
7441 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7442 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7444 default:
7445 break;
7447 break;
7449 default:
7450 break;
7452 return false;
7455 /* FNDECL is assumed to be a builtin where truncation can be propagated
7456 across (for instance floor((double)f) == (double)floorf (f).
7457 Do the transformation for a call with argument ARG. */
7459 static tree
7460 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7462 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7464 if (!validate_arg (arg, REAL_TYPE))
7465 return NULL_TREE;
7467 /* Integer rounding functions are idempotent. */
7468 if (fcode == builtin_mathfn_code (arg))
7469 return arg;
7471 /* If argument is already integer valued, and we don't need to worry
7472 about setting errno, there's no need to perform rounding. */
7473 if (! flag_errno_math && integer_valued_real_p (arg))
7474 return arg;
7476 if (optimize)
7478 tree arg0 = strip_float_extensions (arg);
7479 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7480 tree newtype = TREE_TYPE (arg0);
7481 tree decl;
7483 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7484 && (decl = mathfn_built_in (newtype, fcode)))
7485 return fold_convert_loc (loc, ftype,
7486 build_call_expr_loc (loc, decl, 1,
7487 fold_convert_loc (loc,
7488 newtype,
7489 arg0)));
7491 return NULL_TREE;
7494 /* FNDECL is assumed to be builtin which can narrow the FP type of
7495 the argument, for instance lround((double)f) -> lroundf (f).
7496 Do the transformation for a call with argument ARG. */
7498 static tree
7499 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7501 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7503 if (!validate_arg (arg, REAL_TYPE))
7504 return NULL_TREE;
7506 /* If argument is already integer valued, and we don't need to worry
7507 about setting errno, there's no need to perform rounding. */
7508 if (! flag_errno_math && integer_valued_real_p (arg))
7509 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7510 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7512 if (optimize)
7514 tree ftype = TREE_TYPE (arg);
7515 tree arg0 = strip_float_extensions (arg);
7516 tree newtype = TREE_TYPE (arg0);
7517 tree decl;
7519 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7520 && (decl = mathfn_built_in (newtype, fcode)))
7521 return build_call_expr_loc (loc, decl, 1,
7522 fold_convert_loc (loc, newtype, arg0));
7525 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7526 sizeof (int) == sizeof (long). */
7527 if (TYPE_PRECISION (integer_type_node)
7528 == TYPE_PRECISION (long_integer_type_node))
7530 tree newfn = NULL_TREE;
7531 switch (fcode)
7533 CASE_FLT_FN (BUILT_IN_ICEIL):
7534 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7535 break;
7537 CASE_FLT_FN (BUILT_IN_IFLOOR):
7538 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7539 break;
7541 CASE_FLT_FN (BUILT_IN_IROUND):
7542 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7543 break;
7545 CASE_FLT_FN (BUILT_IN_IRINT):
7546 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7547 break;
7549 default:
7550 break;
7553 if (newfn)
7555 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7556 return fold_convert_loc (loc,
7557 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7561 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7562 sizeof (long long) == sizeof (long). */
7563 if (TYPE_PRECISION (long_long_integer_type_node)
7564 == TYPE_PRECISION (long_integer_type_node))
7566 tree newfn = NULL_TREE;
7567 switch (fcode)
7569 CASE_FLT_FN (BUILT_IN_LLCEIL):
7570 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7571 break;
7573 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7574 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7575 break;
7577 CASE_FLT_FN (BUILT_IN_LLROUND):
7578 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7579 break;
7581 CASE_FLT_FN (BUILT_IN_LLRINT):
7582 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7583 break;
7585 default:
7586 break;
7589 if (newfn)
7591 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7592 return fold_convert_loc (loc,
7593 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7597 return NULL_TREE;
7600 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7601 return type. Return NULL_TREE if no simplification can be made. */
7603 static tree
7604 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7606 tree res;
7608 if (!validate_arg (arg, COMPLEX_TYPE)
7609 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7610 return NULL_TREE;
7612 /* Calculate the result when the argument is a constant. */
7613 if (TREE_CODE (arg) == COMPLEX_CST
7614 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7615 type, mpfr_hypot)))
7616 return res;
7618 if (TREE_CODE (arg) == COMPLEX_EXPR)
7620 tree real = TREE_OPERAND (arg, 0);
7621 tree imag = TREE_OPERAND (arg, 1);
7623 /* If either part is zero, cabs is fabs of the other. */
7624 if (real_zerop (real))
7625 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7626 if (real_zerop (imag))
7627 return fold_build1_loc (loc, ABS_EXPR, type, real);
7629 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7630 if (flag_unsafe_math_optimizations
7631 && operand_equal_p (real, imag, OEP_PURE_SAME))
7633 const REAL_VALUE_TYPE sqrt2_trunc
7634 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7635 STRIP_NOPS (real);
7636 return fold_build2_loc (loc, MULT_EXPR, type,
7637 fold_build1_loc (loc, ABS_EXPR, type, real),
7638 build_real (type, sqrt2_trunc));
7642 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7643 if (TREE_CODE (arg) == NEGATE_EXPR
7644 || TREE_CODE (arg) == CONJ_EXPR)
7645 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7647 /* Don't do this when optimizing for size. */
7648 if (flag_unsafe_math_optimizations
7649 && optimize && optimize_function_for_speed_p (cfun))
7651 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7653 if (sqrtfn != NULL_TREE)
7655 tree rpart, ipart, result;
7657 arg = builtin_save_expr (arg);
7659 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7660 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7662 rpart = builtin_save_expr (rpart);
7663 ipart = builtin_save_expr (ipart);
7665 result = fold_build2_loc (loc, PLUS_EXPR, type,
7666 fold_build2_loc (loc, MULT_EXPR, type,
7667 rpart, rpart),
7668 fold_build2_loc (loc, MULT_EXPR, type,
7669 ipart, ipart));
7671 return build_call_expr_loc (loc, sqrtfn, 1, result);
7675 return NULL_TREE;
7678 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7679 complex tree type of the result. If NEG is true, the imaginary
7680 zero is negative. */
7682 static tree
7683 build_complex_cproj (tree type, bool neg)
7685 REAL_VALUE_TYPE rinf, rzero = dconst0;
7687 real_inf (&rinf);
7688 rzero.sign = neg;
7689 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7690 build_real (TREE_TYPE (type), rzero));
7693 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7694 return type. Return NULL_TREE if no simplification can be made. */
7696 static tree
7697 fold_builtin_cproj (location_t loc, tree arg, tree type)
7699 if (!validate_arg (arg, COMPLEX_TYPE)
7700 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7701 return NULL_TREE;
7703 /* If there are no infinities, return arg. */
7704 if (! HONOR_INFINITIES (type))
7705 return non_lvalue_loc (loc, arg);
7707 /* Calculate the result when the argument is a constant. */
7708 if (TREE_CODE (arg) == COMPLEX_CST)
7710 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7711 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7713 if (real_isinf (real) || real_isinf (imag))
7714 return build_complex_cproj (type, imag->sign);
7715 else
7716 return arg;
7718 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7720 tree real = TREE_OPERAND (arg, 0);
7721 tree imag = TREE_OPERAND (arg, 1);
7723 STRIP_NOPS (real);
7724 STRIP_NOPS (imag);
7726 /* If the real part is inf and the imag part is known to be
7727 nonnegative, return (inf + 0i). Remember side-effects are
7728 possible in the imag part. */
7729 if (TREE_CODE (real) == REAL_CST
7730 && real_isinf (TREE_REAL_CST_PTR (real))
7731 && tree_expr_nonnegative_p (imag))
7732 return omit_one_operand_loc (loc, type,
7733 build_complex_cproj (type, false),
7734 arg);
7736 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7737 Remember side-effects are possible in the real part. */
7738 if (TREE_CODE (imag) == REAL_CST
7739 && real_isinf (TREE_REAL_CST_PTR (imag)))
7740 return
7741 omit_one_operand_loc (loc, type,
7742 build_complex_cproj (type, TREE_REAL_CST_PTR
7743 (imag)->sign), arg);
7746 return NULL_TREE;
7749 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7750 Return NULL_TREE if no simplification can be made. */
7752 static tree
7753 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7756 enum built_in_function fcode;
7757 tree res;
7759 if (!validate_arg (arg, REAL_TYPE))
7760 return NULL_TREE;
7762 /* Calculate the result when the argument is a constant. */
7763 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7764 return res;
7766 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7767 fcode = builtin_mathfn_code (arg);
7768 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7770 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7771 arg = fold_build2_loc (loc, MULT_EXPR, type,
7772 CALL_EXPR_ARG (arg, 0),
7773 build_real (type, dconsthalf));
7774 return build_call_expr_loc (loc, expfn, 1, arg);
7777 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7778 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7780 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7782 if (powfn)
7784 tree arg0 = CALL_EXPR_ARG (arg, 0);
7785 tree tree_root;
7786 /* The inner root was either sqrt or cbrt. */
7787 /* This was a conditional expression but it triggered a bug
7788 in Sun C 5.5. */
7789 REAL_VALUE_TYPE dconstroot;
7790 if (BUILTIN_SQRT_P (fcode))
7791 dconstroot = dconsthalf;
7792 else
7793 dconstroot = dconst_third ();
7795 /* Adjust for the outer root. */
7796 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7797 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7798 tree_root = build_real (type, dconstroot);
7799 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7803 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7804 if (flag_unsafe_math_optimizations
7805 && (fcode == BUILT_IN_POW
7806 || fcode == BUILT_IN_POWF
7807 || fcode == BUILT_IN_POWL))
7809 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7810 tree arg0 = CALL_EXPR_ARG (arg, 0);
7811 tree arg1 = CALL_EXPR_ARG (arg, 1);
7812 tree narg1;
7813 if (!tree_expr_nonnegative_p (arg0))
7814 arg0 = build1 (ABS_EXPR, type, arg0);
7815 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7816 build_real (type, dconsthalf));
7817 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7820 return NULL_TREE;
7823 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7824 Return NULL_TREE if no simplification can be made. */
7826 static tree
7827 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7829 const enum built_in_function fcode = builtin_mathfn_code (arg);
7830 tree res;
7832 if (!validate_arg (arg, REAL_TYPE))
7833 return NULL_TREE;
7835 /* Calculate the result when the argument is a constant. */
7836 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7837 return res;
7839 if (flag_unsafe_math_optimizations)
7841 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7842 if (BUILTIN_EXPONENT_P (fcode))
7844 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7845 const REAL_VALUE_TYPE third_trunc =
7846 real_value_truncate (TYPE_MODE (type), dconst_third ());
7847 arg = fold_build2_loc (loc, MULT_EXPR, type,
7848 CALL_EXPR_ARG (arg, 0),
7849 build_real (type, third_trunc));
7850 return build_call_expr_loc (loc, expfn, 1, arg);
7853 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7854 if (BUILTIN_SQRT_P (fcode))
7856 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7858 if (powfn)
7860 tree arg0 = CALL_EXPR_ARG (arg, 0);
7861 tree tree_root;
7862 REAL_VALUE_TYPE dconstroot = dconst_third ();
7864 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7865 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7866 tree_root = build_real (type, dconstroot);
7867 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7871 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7872 if (BUILTIN_CBRT_P (fcode))
7874 tree arg0 = CALL_EXPR_ARG (arg, 0);
7875 if (tree_expr_nonnegative_p (arg0))
7877 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7879 if (powfn)
7881 tree tree_root;
7882 REAL_VALUE_TYPE dconstroot;
7884 real_arithmetic (&dconstroot, MULT_EXPR,
7885 dconst_third_ptr (), dconst_third_ptr ());
7886 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7887 tree_root = build_real (type, dconstroot);
7888 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7893 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7894 if (fcode == BUILT_IN_POW
7895 || fcode == BUILT_IN_POWF
7896 || fcode == BUILT_IN_POWL)
7898 tree arg00 = CALL_EXPR_ARG (arg, 0);
7899 tree arg01 = CALL_EXPR_ARG (arg, 1);
7900 if (tree_expr_nonnegative_p (arg00))
7902 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7903 const REAL_VALUE_TYPE dconstroot
7904 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7905 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7906 build_real (type, dconstroot));
7907 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7911 return NULL_TREE;
7914 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7915 TYPE is the type of the return value. Return NULL_TREE if no
7916 simplification can be made. */
7918 static tree
7919 fold_builtin_cos (location_t loc,
7920 tree arg, tree type, tree fndecl)
7922 tree res, narg;
7924 if (!validate_arg (arg, REAL_TYPE))
7925 return NULL_TREE;
7927 /* Calculate the result when the argument is a constant. */
7928 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7929 return res;
7931 /* Optimize cos(-x) into cos (x). */
7932 if ((narg = fold_strip_sign_ops (arg)))
7933 return build_call_expr_loc (loc, fndecl, 1, narg);
7935 return NULL_TREE;
7938 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7939 Return NULL_TREE if no simplification can be made. */
7941 static tree
7942 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7944 if (validate_arg (arg, REAL_TYPE))
7946 tree res, narg;
7948 /* Calculate the result when the argument is a constant. */
7949 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7950 return res;
7952 /* Optimize cosh(-x) into cosh (x). */
7953 if ((narg = fold_strip_sign_ops (arg)))
7954 return build_call_expr_loc (loc, fndecl, 1, narg);
7957 return NULL_TREE;
7960 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7961 argument ARG. TYPE is the type of the return value. Return
7962 NULL_TREE if no simplification can be made. */
7964 static tree
7965 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7966 bool hyper)
7968 if (validate_arg (arg, COMPLEX_TYPE)
7969 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7971 tree tmp;
7973 /* Calculate the result when the argument is a constant. */
7974 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7975 return tmp;
7977 /* Optimize fn(-x) into fn(x). */
7978 if ((tmp = fold_strip_sign_ops (arg)))
7979 return build_call_expr_loc (loc, fndecl, 1, tmp);
7982 return NULL_TREE;
7985 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7986 Return NULL_TREE if no simplification can be made. */
7988 static tree
7989 fold_builtin_tan (tree arg, tree type)
7991 enum built_in_function fcode;
7992 tree res;
7994 if (!validate_arg (arg, REAL_TYPE))
7995 return NULL_TREE;
7997 /* Calculate the result when the argument is a constant. */
7998 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7999 return res;
8001 /* Optimize tan(atan(x)) = x. */
8002 fcode = builtin_mathfn_code (arg);
8003 if (flag_unsafe_math_optimizations
8004 && (fcode == BUILT_IN_ATAN
8005 || fcode == BUILT_IN_ATANF
8006 || fcode == BUILT_IN_ATANL))
8007 return CALL_EXPR_ARG (arg, 0);
8009 return NULL_TREE;
8012 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8013 NULL_TREE if no simplification can be made. */
8015 static tree
8016 fold_builtin_sincos (location_t loc,
8017 tree arg0, tree arg1, tree arg2)
8019 tree type;
8020 tree res, fn, call;
8022 if (!validate_arg (arg0, REAL_TYPE)
8023 || !validate_arg (arg1, POINTER_TYPE)
8024 || !validate_arg (arg2, POINTER_TYPE))
8025 return NULL_TREE;
8027 type = TREE_TYPE (arg0);
8029 /* Calculate the result when the argument is a constant. */
8030 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
8031 return res;
8033 /* Canonicalize sincos to cexpi. */
8034 if (!targetm.libc_has_function (function_c99_math_complex))
8035 return NULL_TREE;
8036 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
8037 if (!fn)
8038 return NULL_TREE;
8040 call = build_call_expr_loc (loc, fn, 1, arg0);
8041 call = builtin_save_expr (call);
8043 return build2 (COMPOUND_EXPR, void_type_node,
8044 build2 (MODIFY_EXPR, void_type_node,
8045 build_fold_indirect_ref_loc (loc, arg1),
8046 build1 (IMAGPART_EXPR, type, call)),
8047 build2 (MODIFY_EXPR, void_type_node,
8048 build_fold_indirect_ref_loc (loc, arg2),
8049 build1 (REALPART_EXPR, type, call)));
8052 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
8053 NULL_TREE if no simplification can be made. */
8055 static tree
8056 fold_builtin_cexp (location_t loc, tree arg0, tree type)
8058 tree rtype;
8059 tree realp, imagp, ifn;
8060 tree res;
8062 if (!validate_arg (arg0, COMPLEX_TYPE)
8063 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
8064 return NULL_TREE;
8066 /* Calculate the result when the argument is a constant. */
8067 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
8068 return res;
8070 rtype = TREE_TYPE (TREE_TYPE (arg0));
8072 /* In case we can figure out the real part of arg0 and it is constant zero
8073 fold to cexpi. */
8074 if (!targetm.libc_has_function (function_c99_math_complex))
8075 return NULL_TREE;
8076 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
8077 if (!ifn)
8078 return NULL_TREE;
8080 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
8081 && real_zerop (realp))
8083 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
8084 return build_call_expr_loc (loc, ifn, 1, narg);
8087 /* In case we can easily decompose real and imaginary parts split cexp
8088 to exp (r) * cexpi (i). */
8089 if (flag_unsafe_math_optimizations
8090 && realp)
8092 tree rfn, rcall, icall;
8094 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
8095 if (!rfn)
8096 return NULL_TREE;
8098 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
8099 if (!imagp)
8100 return NULL_TREE;
8102 icall = build_call_expr_loc (loc, ifn, 1, imagp);
8103 icall = builtin_save_expr (icall);
8104 rcall = build_call_expr_loc (loc, rfn, 1, realp);
8105 rcall = builtin_save_expr (rcall);
8106 return fold_build2_loc (loc, COMPLEX_EXPR, type,
8107 fold_build2_loc (loc, MULT_EXPR, rtype,
8108 rcall,
8109 fold_build1_loc (loc, REALPART_EXPR,
8110 rtype, icall)),
8111 fold_build2_loc (loc, MULT_EXPR, rtype,
8112 rcall,
8113 fold_build1_loc (loc, IMAGPART_EXPR,
8114 rtype, icall)));
8117 return NULL_TREE;
8120 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
8121 Return NULL_TREE if no simplification can be made. */
8123 static tree
8124 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
8126 if (!validate_arg (arg, REAL_TYPE))
8127 return NULL_TREE;
8129 /* Optimize trunc of constant value. */
8130 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8132 REAL_VALUE_TYPE r, x;
8133 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8135 x = TREE_REAL_CST (arg);
8136 real_trunc (&r, TYPE_MODE (type), &x);
8137 return build_real (type, r);
8140 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8143 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
8144 Return NULL_TREE if no simplification can be made. */
8146 static tree
8147 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
8149 if (!validate_arg (arg, REAL_TYPE))
8150 return NULL_TREE;
8152 /* Optimize floor of constant value. */
8153 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8155 REAL_VALUE_TYPE x;
8157 x = TREE_REAL_CST (arg);
8158 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8160 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8161 REAL_VALUE_TYPE r;
8163 real_floor (&r, TYPE_MODE (type), &x);
8164 return build_real (type, r);
8168 /* Fold floor (x) where x is nonnegative to trunc (x). */
8169 if (tree_expr_nonnegative_p (arg))
8171 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
8172 if (truncfn)
8173 return build_call_expr_loc (loc, truncfn, 1, arg);
8176 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8179 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
8180 Return NULL_TREE if no simplification can be made. */
8182 static tree
8183 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
8185 if (!validate_arg (arg, REAL_TYPE))
8186 return NULL_TREE;
8188 /* Optimize ceil of constant value. */
8189 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8191 REAL_VALUE_TYPE x;
8193 x = TREE_REAL_CST (arg);
8194 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8196 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8197 REAL_VALUE_TYPE r;
8199 real_ceil (&r, TYPE_MODE (type), &x);
8200 return build_real (type, r);
8204 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8207 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8208 Return NULL_TREE if no simplification can be made. */
8210 static tree
8211 fold_builtin_round (location_t loc, tree fndecl, tree arg)
8213 if (!validate_arg (arg, REAL_TYPE))
8214 return NULL_TREE;
8216 /* Optimize round of constant value. */
8217 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8219 REAL_VALUE_TYPE x;
8221 x = TREE_REAL_CST (arg);
8222 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8224 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8225 REAL_VALUE_TYPE r;
8227 real_round (&r, TYPE_MODE (type), &x);
8228 return build_real (type, r);
8232 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8235 /* Fold function call to builtin lround, lroundf or lroundl (or the
8236 corresponding long long versions) and other rounding functions. ARG
8237 is the argument to the call. Return NULL_TREE if no simplification
8238 can be made. */
8240 static tree
8241 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8243 if (!validate_arg (arg, REAL_TYPE))
8244 return NULL_TREE;
8246 /* Optimize lround of constant value. */
8247 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8249 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8251 if (real_isfinite (&x))
8253 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8254 tree ftype = TREE_TYPE (arg);
8255 REAL_VALUE_TYPE r;
8256 bool fail = false;
8258 switch (DECL_FUNCTION_CODE (fndecl))
8260 CASE_FLT_FN (BUILT_IN_IFLOOR):
8261 CASE_FLT_FN (BUILT_IN_LFLOOR):
8262 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8263 real_floor (&r, TYPE_MODE (ftype), &x);
8264 break;
8266 CASE_FLT_FN (BUILT_IN_ICEIL):
8267 CASE_FLT_FN (BUILT_IN_LCEIL):
8268 CASE_FLT_FN (BUILT_IN_LLCEIL):
8269 real_ceil (&r, TYPE_MODE (ftype), &x);
8270 break;
8272 CASE_FLT_FN (BUILT_IN_IROUND):
8273 CASE_FLT_FN (BUILT_IN_LROUND):
8274 CASE_FLT_FN (BUILT_IN_LLROUND):
8275 real_round (&r, TYPE_MODE (ftype), &x);
8276 break;
8278 default:
8279 gcc_unreachable ();
8282 wide_int val = real_to_integer (&r, &fail, TYPE_PRECISION (itype));
8283 if (!fail)
8284 return wide_int_to_tree (itype, val);
8288 switch (DECL_FUNCTION_CODE (fndecl))
8290 CASE_FLT_FN (BUILT_IN_LFLOOR):
8291 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8292 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8293 if (tree_expr_nonnegative_p (arg))
8294 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8295 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8296 break;
8297 default:;
8300 return fold_fixed_mathfn (loc, fndecl, arg);
8303 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8304 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8305 the argument to the call. Return NULL_TREE if no simplification can
8306 be made. */
8308 static tree
8309 fold_builtin_bitop (tree fndecl, tree arg)
8311 if (!validate_arg (arg, INTEGER_TYPE))
8312 return NULL_TREE;
8314 /* Optimize for constant argument. */
8315 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8317 tree type = TREE_TYPE (arg);
8318 int result;
8320 switch (DECL_FUNCTION_CODE (fndecl))
8322 CASE_INT_FN (BUILT_IN_FFS):
8323 result = wi::ffs (arg);
8324 break;
8326 CASE_INT_FN (BUILT_IN_CLZ):
8327 if (wi::ne_p (arg, 0))
8328 result = wi::clz (arg);
8329 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8330 result = TYPE_PRECISION (type);
8331 break;
8333 CASE_INT_FN (BUILT_IN_CTZ):
8334 if (wi::ne_p (arg, 0))
8335 result = wi::ctz (arg);
8336 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8337 result = TYPE_PRECISION (type);
8338 break;
8340 CASE_INT_FN (BUILT_IN_CLRSB):
8341 result = wi::clrsb (arg);
8342 break;
8344 CASE_INT_FN (BUILT_IN_POPCOUNT):
8345 result = wi::popcount (arg);
8346 break;
8348 CASE_INT_FN (BUILT_IN_PARITY):
8349 result = wi::parity (arg);
8350 break;
8352 default:
8353 gcc_unreachable ();
8356 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8359 return NULL_TREE;
8362 /* Fold function call to builtin_bswap and the short, long and long long
8363 variants. Return NULL_TREE if no simplification can be made. */
8364 static tree
8365 fold_builtin_bswap (tree fndecl, tree arg)
8367 if (! validate_arg (arg, INTEGER_TYPE))
8368 return NULL_TREE;
8370 /* Optimize constant value. */
8371 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8373 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8375 switch (DECL_FUNCTION_CODE (fndecl))
8377 case BUILT_IN_BSWAP16:
8378 case BUILT_IN_BSWAP32:
8379 case BUILT_IN_BSWAP64:
8381 signop sgn = TYPE_SIGN (type);
8382 tree result =
8383 wide_int_to_tree (type,
8384 wide_int::from (arg, TYPE_PRECISION (type),
8385 sgn).bswap ());
8386 return result;
8388 default:
8389 gcc_unreachable ();
8393 return NULL_TREE;
8396 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8397 NULL_TREE if no simplification can be made. */
8399 static tree
8400 fold_builtin_hypot (location_t loc, tree fndecl,
8401 tree arg0, tree arg1, tree type)
8403 tree res, narg0, narg1;
8405 if (!validate_arg (arg0, REAL_TYPE)
8406 || !validate_arg (arg1, REAL_TYPE))
8407 return NULL_TREE;
8409 /* Calculate the result when the argument is a constant. */
8410 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8411 return res;
8413 /* If either argument to hypot has a negate or abs, strip that off.
8414 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8415 narg0 = fold_strip_sign_ops (arg0);
8416 narg1 = fold_strip_sign_ops (arg1);
8417 if (narg0 || narg1)
8419 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8420 narg1 ? narg1 : arg1);
8423 /* If either argument is zero, hypot is fabs of the other. */
8424 if (real_zerop (arg0))
8425 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8426 else if (real_zerop (arg1))
8427 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8429 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8430 if (flag_unsafe_math_optimizations
8431 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8433 const REAL_VALUE_TYPE sqrt2_trunc
8434 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8435 return fold_build2_loc (loc, MULT_EXPR, type,
8436 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8437 build_real (type, sqrt2_trunc));
8440 return NULL_TREE;
8444 /* Fold a builtin function call to pow, powf, or powl. Return
8445 NULL_TREE if no simplification can be made. */
8446 static tree
8447 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8449 tree res;
8451 if (!validate_arg (arg0, REAL_TYPE)
8452 || !validate_arg (arg1, REAL_TYPE))
8453 return NULL_TREE;
8455 /* Calculate the result when the argument is a constant. */
8456 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8457 return res;
8459 /* Optimize pow(1.0,y) = 1.0. */
8460 if (real_onep (arg0))
8461 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8463 if (TREE_CODE (arg1) == REAL_CST
8464 && !TREE_OVERFLOW (arg1))
8466 REAL_VALUE_TYPE cint;
8467 REAL_VALUE_TYPE c;
8468 HOST_WIDE_INT n;
8470 c = TREE_REAL_CST (arg1);
8472 /* Optimize pow(x,0.0) = 1.0. */
8473 if (REAL_VALUES_EQUAL (c, dconst0))
8474 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8475 arg0);
8477 /* Optimize pow(x,1.0) = x. */
8478 if (REAL_VALUES_EQUAL (c, dconst1))
8479 return arg0;
8481 /* Optimize pow(x,-1.0) = 1.0/x. */
8482 if (REAL_VALUES_EQUAL (c, dconstm1))
8483 return fold_build2_loc (loc, RDIV_EXPR, type,
8484 build_real (type, dconst1), arg0);
8486 /* Optimize pow(x,0.5) = sqrt(x). */
8487 if (flag_unsafe_math_optimizations
8488 && REAL_VALUES_EQUAL (c, dconsthalf))
8490 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8492 if (sqrtfn != NULL_TREE)
8493 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8496 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8497 if (flag_unsafe_math_optimizations)
8499 const REAL_VALUE_TYPE dconstroot
8500 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8502 if (REAL_VALUES_EQUAL (c, dconstroot))
8504 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8505 if (cbrtfn != NULL_TREE)
8506 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8510 /* Check for an integer exponent. */
8511 n = real_to_integer (&c);
8512 real_from_integer (&cint, VOIDmode, n, SIGNED);
8513 if (real_identical (&c, &cint))
8515 /* Attempt to evaluate pow at compile-time, unless this should
8516 raise an exception. */
8517 if (TREE_CODE (arg0) == REAL_CST
8518 && !TREE_OVERFLOW (arg0)
8519 && (n > 0
8520 || (!flag_trapping_math && !flag_errno_math)
8521 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8523 REAL_VALUE_TYPE x;
8524 bool inexact;
8526 x = TREE_REAL_CST (arg0);
8527 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8528 if (flag_unsafe_math_optimizations || !inexact)
8529 return build_real (type, x);
8532 /* Strip sign ops from even integer powers. */
8533 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8535 tree narg0 = fold_strip_sign_ops (arg0);
8536 if (narg0)
8537 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8542 if (flag_unsafe_math_optimizations)
8544 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8546 /* Optimize pow(expN(x),y) = expN(x*y). */
8547 if (BUILTIN_EXPONENT_P (fcode))
8549 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8550 tree arg = CALL_EXPR_ARG (arg0, 0);
8551 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8552 return build_call_expr_loc (loc, expfn, 1, arg);
8555 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8556 if (BUILTIN_SQRT_P (fcode))
8558 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8559 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8560 build_real (type, dconsthalf));
8561 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8564 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8565 if (BUILTIN_CBRT_P (fcode))
8567 tree arg = CALL_EXPR_ARG (arg0, 0);
8568 if (tree_expr_nonnegative_p (arg))
8570 const REAL_VALUE_TYPE dconstroot
8571 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8572 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8573 build_real (type, dconstroot));
8574 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8578 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8579 if (fcode == BUILT_IN_POW
8580 || fcode == BUILT_IN_POWF
8581 || fcode == BUILT_IN_POWL)
8583 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8584 if (tree_expr_nonnegative_p (arg00))
8586 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8587 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8588 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8593 return NULL_TREE;
8596 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8597 Return NULL_TREE if no simplification can be made. */
8598 static tree
8599 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8600 tree arg0, tree arg1, tree type)
8602 if (!validate_arg (arg0, REAL_TYPE)
8603 || !validate_arg (arg1, INTEGER_TYPE))
8604 return NULL_TREE;
8606 /* Optimize pow(1.0,y) = 1.0. */
8607 if (real_onep (arg0))
8608 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8610 if (tree_fits_shwi_p (arg1))
8612 HOST_WIDE_INT c = tree_to_shwi (arg1);
8614 /* Evaluate powi at compile-time. */
8615 if (TREE_CODE (arg0) == REAL_CST
8616 && !TREE_OVERFLOW (arg0))
8618 REAL_VALUE_TYPE x;
8619 x = TREE_REAL_CST (arg0);
8620 real_powi (&x, TYPE_MODE (type), &x, c);
8621 return build_real (type, x);
8624 /* Optimize pow(x,0) = 1.0. */
8625 if (c == 0)
8626 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8627 arg0);
8629 /* Optimize pow(x,1) = x. */
8630 if (c == 1)
8631 return arg0;
8633 /* Optimize pow(x,-1) = 1.0/x. */
8634 if (c == -1)
8635 return fold_build2_loc (loc, RDIV_EXPR, type,
8636 build_real (type, dconst1), arg0);
8639 return NULL_TREE;
8642 /* A subroutine of fold_builtin to fold the various exponent
8643 functions. Return NULL_TREE if no simplification can be made.
8644 FUNC is the corresponding MPFR exponent function. */
8646 static tree
8647 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8648 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8650 if (validate_arg (arg, REAL_TYPE))
8652 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8653 tree res;
8655 /* Calculate the result when the argument is a constant. */
8656 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8657 return res;
8659 /* Optimize expN(logN(x)) = x. */
8660 if (flag_unsafe_math_optimizations)
8662 const enum built_in_function fcode = builtin_mathfn_code (arg);
8664 if ((func == mpfr_exp
8665 && (fcode == BUILT_IN_LOG
8666 || fcode == BUILT_IN_LOGF
8667 || fcode == BUILT_IN_LOGL))
8668 || (func == mpfr_exp2
8669 && (fcode == BUILT_IN_LOG2
8670 || fcode == BUILT_IN_LOG2F
8671 || fcode == BUILT_IN_LOG2L))
8672 || (func == mpfr_exp10
8673 && (fcode == BUILT_IN_LOG10
8674 || fcode == BUILT_IN_LOG10F
8675 || fcode == BUILT_IN_LOG10L)))
8676 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8680 return NULL_TREE;
8683 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8684 arguments to the call, and TYPE is its return type.
8685 Return NULL_TREE if no simplification can be made. */
8687 static tree
8688 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8690 if (!validate_arg (arg1, POINTER_TYPE)
8691 || !validate_arg (arg2, INTEGER_TYPE)
8692 || !validate_arg (len, INTEGER_TYPE))
8693 return NULL_TREE;
8694 else
8696 const char *p1;
8698 if (TREE_CODE (arg2) != INTEGER_CST
8699 || !tree_fits_uhwi_p (len))
8700 return NULL_TREE;
8702 p1 = c_getstr (arg1);
8703 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8705 char c;
8706 const char *r;
8707 tree tem;
8709 if (target_char_cast (arg2, &c))
8710 return NULL_TREE;
8712 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
8714 if (r == NULL)
8715 return build_int_cst (TREE_TYPE (arg1), 0);
8717 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
8718 return fold_convert_loc (loc, type, tem);
8720 return NULL_TREE;
8724 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8725 Return NULL_TREE if no simplification can be made. */
8727 static tree
8728 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8730 const char *p1, *p2;
8732 if (!validate_arg (arg1, POINTER_TYPE)
8733 || !validate_arg (arg2, POINTER_TYPE)
8734 || !validate_arg (len, INTEGER_TYPE))
8735 return NULL_TREE;
8737 /* If the LEN parameter is zero, return zero. */
8738 if (integer_zerop (len))
8739 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8740 arg1, arg2);
8742 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8743 if (operand_equal_p (arg1, arg2, 0))
8744 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8746 p1 = c_getstr (arg1);
8747 p2 = c_getstr (arg2);
8749 /* If all arguments are constant, and the value of len is not greater
8750 than the lengths of arg1 and arg2, evaluate at compile-time. */
8751 if (tree_fits_uhwi_p (len) && p1 && p2
8752 && compare_tree_int (len, strlen (p1) + 1) <= 0
8753 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8755 const int r = memcmp (p1, p2, tree_to_uhwi (len));
8757 if (r > 0)
8758 return integer_one_node;
8759 else if (r < 0)
8760 return integer_minus_one_node;
8761 else
8762 return integer_zero_node;
8765 /* If len parameter is one, return an expression corresponding to
8766 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8767 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8769 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8770 tree cst_uchar_ptr_node
8771 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8773 tree ind1
8774 = fold_convert_loc (loc, integer_type_node,
8775 build1 (INDIRECT_REF, cst_uchar_node,
8776 fold_convert_loc (loc,
8777 cst_uchar_ptr_node,
8778 arg1)));
8779 tree ind2
8780 = fold_convert_loc (loc, integer_type_node,
8781 build1 (INDIRECT_REF, cst_uchar_node,
8782 fold_convert_loc (loc,
8783 cst_uchar_ptr_node,
8784 arg2)));
8785 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8788 return NULL_TREE;
8791 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8792 Return NULL_TREE if no simplification can be made. */
8794 static tree
8795 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8797 const char *p1, *p2;
8799 if (!validate_arg (arg1, POINTER_TYPE)
8800 || !validate_arg (arg2, POINTER_TYPE))
8801 return NULL_TREE;
8803 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8804 if (operand_equal_p (arg1, arg2, 0))
8805 return integer_zero_node;
8807 p1 = c_getstr (arg1);
8808 p2 = c_getstr (arg2);
8810 if (p1 && p2)
8812 const int i = strcmp (p1, p2);
8813 if (i < 0)
8814 return integer_minus_one_node;
8815 else if (i > 0)
8816 return integer_one_node;
8817 else
8818 return integer_zero_node;
8821 /* If the second arg is "", return *(const unsigned char*)arg1. */
8822 if (p2 && *p2 == '\0')
8824 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8825 tree cst_uchar_ptr_node
8826 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8828 return fold_convert_loc (loc, integer_type_node,
8829 build1 (INDIRECT_REF, cst_uchar_node,
8830 fold_convert_loc (loc,
8831 cst_uchar_ptr_node,
8832 arg1)));
8835 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8836 if (p1 && *p1 == '\0')
8838 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8839 tree cst_uchar_ptr_node
8840 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8842 tree temp
8843 = fold_convert_loc (loc, integer_type_node,
8844 build1 (INDIRECT_REF, cst_uchar_node,
8845 fold_convert_loc (loc,
8846 cst_uchar_ptr_node,
8847 arg2)));
8848 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8851 return NULL_TREE;
8854 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8855 Return NULL_TREE if no simplification can be made. */
8857 static tree
8858 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8860 const char *p1, *p2;
8862 if (!validate_arg (arg1, POINTER_TYPE)
8863 || !validate_arg (arg2, POINTER_TYPE)
8864 || !validate_arg (len, INTEGER_TYPE))
8865 return NULL_TREE;
8867 /* If the LEN parameter is zero, return zero. */
8868 if (integer_zerop (len))
8869 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8870 arg1, arg2);
8872 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8873 if (operand_equal_p (arg1, arg2, 0))
8874 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8876 p1 = c_getstr (arg1);
8877 p2 = c_getstr (arg2);
8879 if (tree_fits_uhwi_p (len) && p1 && p2)
8881 const int i = strncmp (p1, p2, tree_to_uhwi (len));
8882 if (i > 0)
8883 return integer_one_node;
8884 else if (i < 0)
8885 return integer_minus_one_node;
8886 else
8887 return integer_zero_node;
8890 /* If the second arg is "", and the length is greater than zero,
8891 return *(const unsigned char*)arg1. */
8892 if (p2 && *p2 == '\0'
8893 && TREE_CODE (len) == INTEGER_CST
8894 && tree_int_cst_sgn (len) == 1)
8896 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8897 tree cst_uchar_ptr_node
8898 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8900 return fold_convert_loc (loc, integer_type_node,
8901 build1 (INDIRECT_REF, cst_uchar_node,
8902 fold_convert_loc (loc,
8903 cst_uchar_ptr_node,
8904 arg1)));
8907 /* If the first arg is "", and the length is greater than zero,
8908 return -*(const unsigned char*)arg2. */
8909 if (p1 && *p1 == '\0'
8910 && TREE_CODE (len) == INTEGER_CST
8911 && tree_int_cst_sgn (len) == 1)
8913 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8914 tree cst_uchar_ptr_node
8915 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8917 tree temp = fold_convert_loc (loc, integer_type_node,
8918 build1 (INDIRECT_REF, cst_uchar_node,
8919 fold_convert_loc (loc,
8920 cst_uchar_ptr_node,
8921 arg2)));
8922 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8925 /* If len parameter is one, return an expression corresponding to
8926 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8927 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8929 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8930 tree cst_uchar_ptr_node
8931 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8933 tree ind1 = fold_convert_loc (loc, integer_type_node,
8934 build1 (INDIRECT_REF, cst_uchar_node,
8935 fold_convert_loc (loc,
8936 cst_uchar_ptr_node,
8937 arg1)));
8938 tree ind2 = fold_convert_loc (loc, integer_type_node,
8939 build1 (INDIRECT_REF, cst_uchar_node,
8940 fold_convert_loc (loc,
8941 cst_uchar_ptr_node,
8942 arg2)));
8943 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8946 return NULL_TREE;
8949 /* Fold function call to builtin signbit, signbitf or signbitl with argument
8950 ARG. Return NULL_TREE if no simplification can be made. */
8952 static tree
8953 fold_builtin_signbit (location_t loc, tree arg, tree type)
8955 if (!validate_arg (arg, REAL_TYPE))
8956 return NULL_TREE;
8958 /* If ARG is a compile-time constant, determine the result. */
8959 if (TREE_CODE (arg) == REAL_CST
8960 && !TREE_OVERFLOW (arg))
8962 REAL_VALUE_TYPE c;
8964 c = TREE_REAL_CST (arg);
8965 return (REAL_VALUE_NEGATIVE (c)
8966 ? build_one_cst (type)
8967 : build_zero_cst (type));
8970 /* If ARG is non-negative, the result is always zero. */
8971 if (tree_expr_nonnegative_p (arg))
8972 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8974 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8975 if (!HONOR_SIGNED_ZEROS (arg))
8976 return fold_convert (type,
8977 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
8978 build_real (TREE_TYPE (arg), dconst0)));
8980 return NULL_TREE;
8983 /* Fold function call to builtin copysign, copysignf or copysignl with
8984 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
8985 be made. */
8987 static tree
8988 fold_builtin_copysign (location_t loc, tree fndecl,
8989 tree arg1, tree arg2, tree type)
8991 tree tem;
8993 if (!validate_arg (arg1, REAL_TYPE)
8994 || !validate_arg (arg2, REAL_TYPE))
8995 return NULL_TREE;
8997 /* copysign(X,X) is X. */
8998 if (operand_equal_p (arg1, arg2, 0))
8999 return fold_convert_loc (loc, type, arg1);
9001 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9002 if (TREE_CODE (arg1) == REAL_CST
9003 && TREE_CODE (arg2) == REAL_CST
9004 && !TREE_OVERFLOW (arg1)
9005 && !TREE_OVERFLOW (arg2))
9007 REAL_VALUE_TYPE c1, c2;
9009 c1 = TREE_REAL_CST (arg1);
9010 c2 = TREE_REAL_CST (arg2);
9011 /* c1.sign := c2.sign. */
9012 real_copysign (&c1, &c2);
9013 return build_real (type, c1);
9016 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9017 Remember to evaluate Y for side-effects. */
9018 if (tree_expr_nonnegative_p (arg2))
9019 return omit_one_operand_loc (loc, type,
9020 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9021 arg2);
9023 /* Strip sign changing operations for the first argument. */
9024 tem = fold_strip_sign_ops (arg1);
9025 if (tem)
9026 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9028 return NULL_TREE;
9031 /* Fold a call to builtin isascii with argument ARG. */
9033 static tree
9034 fold_builtin_isascii (location_t loc, tree arg)
9036 if (!validate_arg (arg, INTEGER_TYPE))
9037 return NULL_TREE;
9038 else
9040 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9041 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9042 build_int_cst (integer_type_node,
9043 ~ (unsigned HOST_WIDE_INT) 0x7f));
9044 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9045 arg, integer_zero_node);
9049 /* Fold a call to builtin toascii with argument ARG. */
9051 static tree
9052 fold_builtin_toascii (location_t loc, tree arg)
9054 if (!validate_arg (arg, INTEGER_TYPE))
9055 return NULL_TREE;
9057 /* Transform toascii(c) -> (c & 0x7f). */
9058 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9059 build_int_cst (integer_type_node, 0x7f));
9062 /* Fold a call to builtin isdigit with argument ARG. */
9064 static tree
9065 fold_builtin_isdigit (location_t loc, tree arg)
9067 if (!validate_arg (arg, INTEGER_TYPE))
9068 return NULL_TREE;
9069 else
9071 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9072 /* According to the C standard, isdigit is unaffected by locale.
9073 However, it definitely is affected by the target character set. */
9074 unsigned HOST_WIDE_INT target_digit0
9075 = lang_hooks.to_target_charset ('0');
9077 if (target_digit0 == 0)
9078 return NULL_TREE;
9080 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9081 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9082 build_int_cst (unsigned_type_node, target_digit0));
9083 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9084 build_int_cst (unsigned_type_node, 9));
9088 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9090 static tree
9091 fold_builtin_fabs (location_t loc, tree arg, tree type)
9093 if (!validate_arg (arg, REAL_TYPE))
9094 return NULL_TREE;
9096 arg = fold_convert_loc (loc, type, arg);
9097 if (TREE_CODE (arg) == REAL_CST)
9098 return fold_abs_const (arg, type);
9099 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9102 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9104 static tree
9105 fold_builtin_abs (location_t loc, tree arg, tree type)
9107 if (!validate_arg (arg, INTEGER_TYPE))
9108 return NULL_TREE;
9110 arg = fold_convert_loc (loc, type, arg);
9111 if (TREE_CODE (arg) == INTEGER_CST)
9112 return fold_abs_const (arg, type);
9113 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9116 /* Fold a fma operation with arguments ARG[012]. */
9118 tree
9119 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9120 tree type, tree arg0, tree arg1, tree arg2)
9122 if (TREE_CODE (arg0) == REAL_CST
9123 && TREE_CODE (arg1) == REAL_CST
9124 && TREE_CODE (arg2) == REAL_CST)
9125 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9127 return NULL_TREE;
9130 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9132 static tree
9133 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9135 if (validate_arg (arg0, REAL_TYPE)
9136 && validate_arg (arg1, REAL_TYPE)
9137 && validate_arg (arg2, REAL_TYPE))
9139 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9140 if (tem)
9141 return tem;
9143 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9144 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9145 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9147 return NULL_TREE;
9150 /* Fold a call to builtin fmin or fmax. */
9152 static tree
9153 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9154 tree type, bool max)
9156 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9158 /* Calculate the result when the argument is a constant. */
9159 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9161 if (res)
9162 return res;
9164 /* If either argument is NaN, return the other one. Avoid the
9165 transformation if we get (and honor) a signalling NaN. Using
9166 omit_one_operand() ensures we create a non-lvalue. */
9167 if (TREE_CODE (arg0) == REAL_CST
9168 && real_isnan (&TREE_REAL_CST (arg0))
9169 && (! HONOR_SNANS (arg0)
9170 || ! TREE_REAL_CST (arg0).signalling))
9171 return omit_one_operand_loc (loc, type, arg1, arg0);
9172 if (TREE_CODE (arg1) == REAL_CST
9173 && real_isnan (&TREE_REAL_CST (arg1))
9174 && (! HONOR_SNANS (arg1)
9175 || ! TREE_REAL_CST (arg1).signalling))
9176 return omit_one_operand_loc (loc, type, arg0, arg1);
9178 /* Transform fmin/fmax(x,x) -> x. */
9179 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9180 return omit_one_operand_loc (loc, type, arg0, arg1);
9182 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9183 functions to return the numeric arg if the other one is NaN.
9184 These tree codes don't honor that, so only transform if
9185 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9186 handled, so we don't have to worry about it either. */
9187 if (flag_finite_math_only)
9188 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9189 fold_convert_loc (loc, type, arg0),
9190 fold_convert_loc (loc, type, arg1));
9192 return NULL_TREE;
9195 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9197 static tree
9198 fold_builtin_carg (location_t loc, tree arg, tree type)
9200 if (validate_arg (arg, COMPLEX_TYPE)
9201 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9203 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9205 if (atan2_fn)
9207 tree new_arg = builtin_save_expr (arg);
9208 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9209 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9210 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9214 return NULL_TREE;
9217 /* Fold a call to builtin logb/ilogb. */
9219 static tree
9220 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9222 if (! validate_arg (arg, REAL_TYPE))
9223 return NULL_TREE;
9225 STRIP_NOPS (arg);
9227 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9229 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9231 switch (value->cl)
9233 case rvc_nan:
9234 case rvc_inf:
9235 /* If arg is Inf or NaN and we're logb, return it. */
9236 if (TREE_CODE (rettype) == REAL_TYPE)
9238 /* For logb(-Inf) we have to return +Inf. */
9239 if (real_isinf (value) && real_isneg (value))
9241 REAL_VALUE_TYPE tem;
9242 real_inf (&tem);
9243 return build_real (rettype, tem);
9245 return fold_convert_loc (loc, rettype, arg);
9247 /* Fall through... */
9248 case rvc_zero:
9249 /* Zero may set errno and/or raise an exception for logb, also
9250 for ilogb we don't know FP_ILOGB0. */
9251 return NULL_TREE;
9252 case rvc_normal:
9253 /* For normal numbers, proceed iff radix == 2. In GCC,
9254 normalized significands are in the range [0.5, 1.0). We
9255 want the exponent as if they were [1.0, 2.0) so get the
9256 exponent and subtract 1. */
9257 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9258 return fold_convert_loc (loc, rettype,
9259 build_int_cst (integer_type_node,
9260 REAL_EXP (value)-1));
9261 break;
9265 return NULL_TREE;
9268 /* Fold a call to builtin significand, if radix == 2. */
9270 static tree
9271 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9273 if (! validate_arg (arg, REAL_TYPE))
9274 return NULL_TREE;
9276 STRIP_NOPS (arg);
9278 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9280 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9282 switch (value->cl)
9284 case rvc_zero:
9285 case rvc_nan:
9286 case rvc_inf:
9287 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9288 return fold_convert_loc (loc, rettype, arg);
9289 case rvc_normal:
9290 /* For normal numbers, proceed iff radix == 2. */
9291 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9293 REAL_VALUE_TYPE result = *value;
9294 /* In GCC, normalized significands are in the range [0.5,
9295 1.0). We want them to be [1.0, 2.0) so set the
9296 exponent to 1. */
9297 SET_REAL_EXP (&result, 1);
9298 return build_real (rettype, result);
9300 break;
9304 return NULL_TREE;
9307 /* Fold a call to builtin frexp, we can assume the base is 2. */
9309 static tree
9310 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9312 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9313 return NULL_TREE;
9315 STRIP_NOPS (arg0);
9317 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9318 return NULL_TREE;
9320 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9322 /* Proceed if a valid pointer type was passed in. */
9323 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9325 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9326 tree frac, exp;
9328 switch (value->cl)
9330 case rvc_zero:
9331 /* For +-0, return (*exp = 0, +-0). */
9332 exp = integer_zero_node;
9333 frac = arg0;
9334 break;
9335 case rvc_nan:
9336 case rvc_inf:
9337 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9338 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9339 case rvc_normal:
9341 /* Since the frexp function always expects base 2, and in
9342 GCC normalized significands are already in the range
9343 [0.5, 1.0), we have exactly what frexp wants. */
9344 REAL_VALUE_TYPE frac_rvt = *value;
9345 SET_REAL_EXP (&frac_rvt, 0);
9346 frac = build_real (rettype, frac_rvt);
9347 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9349 break;
9350 default:
9351 gcc_unreachable ();
9354 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9355 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9356 TREE_SIDE_EFFECTS (arg1) = 1;
9357 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9360 return NULL_TREE;
9363 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9364 then we can assume the base is two. If it's false, then we have to
9365 check the mode of the TYPE parameter in certain cases. */
9367 static tree
9368 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9369 tree type, bool ldexp)
9371 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9373 STRIP_NOPS (arg0);
9374 STRIP_NOPS (arg1);
9376 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9377 if (real_zerop (arg0) || integer_zerop (arg1)
9378 || (TREE_CODE (arg0) == REAL_CST
9379 && !real_isfinite (&TREE_REAL_CST (arg0))))
9380 return omit_one_operand_loc (loc, type, arg0, arg1);
9382 /* If both arguments are constant, then try to evaluate it. */
9383 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9384 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9385 && tree_fits_shwi_p (arg1))
9387 /* Bound the maximum adjustment to twice the range of the
9388 mode's valid exponents. Use abs to ensure the range is
9389 positive as a sanity check. */
9390 const long max_exp_adj = 2 *
9391 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9392 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9394 /* Get the user-requested adjustment. */
9395 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9397 /* The requested adjustment must be inside this range. This
9398 is a preliminary cap to avoid things like overflow, we
9399 may still fail to compute the result for other reasons. */
9400 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9402 REAL_VALUE_TYPE initial_result;
9404 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9406 /* Ensure we didn't overflow. */
9407 if (! real_isinf (&initial_result))
9409 const REAL_VALUE_TYPE trunc_result
9410 = real_value_truncate (TYPE_MODE (type), initial_result);
9412 /* Only proceed if the target mode can hold the
9413 resulting value. */
9414 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9415 return build_real (type, trunc_result);
9421 return NULL_TREE;
9424 /* Fold a call to builtin modf. */
9426 static tree
9427 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9429 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9430 return NULL_TREE;
9432 STRIP_NOPS (arg0);
9434 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9435 return NULL_TREE;
9437 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9439 /* Proceed if a valid pointer type was passed in. */
9440 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9442 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9443 REAL_VALUE_TYPE trunc, frac;
9445 switch (value->cl)
9447 case rvc_nan:
9448 case rvc_zero:
9449 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9450 trunc = frac = *value;
9451 break;
9452 case rvc_inf:
9453 /* For +-Inf, return (*arg1 = arg0, +-0). */
9454 frac = dconst0;
9455 frac.sign = value->sign;
9456 trunc = *value;
9457 break;
9458 case rvc_normal:
9459 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9460 real_trunc (&trunc, VOIDmode, value);
9461 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9462 /* If the original number was negative and already
9463 integral, then the fractional part is -0.0. */
9464 if (value->sign && frac.cl == rvc_zero)
9465 frac.sign = value->sign;
9466 break;
9469 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9470 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9471 build_real (rettype, trunc));
9472 TREE_SIDE_EFFECTS (arg1) = 1;
9473 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9474 build_real (rettype, frac));
9477 return NULL_TREE;
9480 /* Given a location LOC, an interclass builtin function decl FNDECL
9481 and its single argument ARG, return an folded expression computing
9482 the same, or NULL_TREE if we either couldn't or didn't want to fold
9483 (the latter happen if there's an RTL instruction available). */
9485 static tree
9486 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9488 machine_mode mode;
9490 if (!validate_arg (arg, REAL_TYPE))
9491 return NULL_TREE;
9493 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9494 return NULL_TREE;
9496 mode = TYPE_MODE (TREE_TYPE (arg));
9498 /* If there is no optab, try generic code. */
9499 switch (DECL_FUNCTION_CODE (fndecl))
9501 tree result;
9503 CASE_FLT_FN (BUILT_IN_ISINF):
9505 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9506 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9507 tree const type = TREE_TYPE (arg);
9508 REAL_VALUE_TYPE r;
9509 char buf[128];
9511 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9512 real_from_string (&r, buf);
9513 result = build_call_expr (isgr_fn, 2,
9514 fold_build1_loc (loc, ABS_EXPR, type, arg),
9515 build_real (type, r));
9516 return result;
9518 CASE_FLT_FN (BUILT_IN_FINITE):
9519 case BUILT_IN_ISFINITE:
9521 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9522 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9523 tree const type = TREE_TYPE (arg);
9524 REAL_VALUE_TYPE r;
9525 char buf[128];
9527 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9528 real_from_string (&r, buf);
9529 result = build_call_expr (isle_fn, 2,
9530 fold_build1_loc (loc, ABS_EXPR, type, arg),
9531 build_real (type, r));
9532 /*result = fold_build2_loc (loc, UNGT_EXPR,
9533 TREE_TYPE (TREE_TYPE (fndecl)),
9534 fold_build1_loc (loc, ABS_EXPR, type, arg),
9535 build_real (type, r));
9536 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9537 TREE_TYPE (TREE_TYPE (fndecl)),
9538 result);*/
9539 return result;
9541 case BUILT_IN_ISNORMAL:
9543 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9544 islessequal(fabs(x),DBL_MAX). */
9545 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9546 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9547 tree const type = TREE_TYPE (arg);
9548 REAL_VALUE_TYPE rmax, rmin;
9549 char buf[128];
9551 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9552 real_from_string (&rmax, buf);
9553 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9554 real_from_string (&rmin, buf);
9555 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9556 result = build_call_expr (isle_fn, 2, arg,
9557 build_real (type, rmax));
9558 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9559 build_call_expr (isge_fn, 2, arg,
9560 build_real (type, rmin)));
9561 return result;
9563 default:
9564 break;
9567 return NULL_TREE;
9570 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9571 ARG is the argument for the call. */
9573 static tree
9574 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9576 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9577 REAL_VALUE_TYPE r;
9579 if (!validate_arg (arg, REAL_TYPE))
9580 return NULL_TREE;
9582 switch (builtin_index)
9584 case BUILT_IN_ISINF:
9585 if (!HONOR_INFINITIES (arg))
9586 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9588 if (TREE_CODE (arg) == REAL_CST)
9590 r = TREE_REAL_CST (arg);
9591 if (real_isinf (&r))
9592 return real_compare (GT_EXPR, &r, &dconst0)
9593 ? integer_one_node : integer_minus_one_node;
9594 else
9595 return integer_zero_node;
9598 return NULL_TREE;
9600 case BUILT_IN_ISINF_SIGN:
9602 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9603 /* In a boolean context, GCC will fold the inner COND_EXPR to
9604 1. So e.g. "if (isinf_sign(x))" would be folded to just
9605 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9606 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9607 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9608 tree tmp = NULL_TREE;
9610 arg = builtin_save_expr (arg);
9612 if (signbit_fn && isinf_fn)
9614 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9615 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9617 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9618 signbit_call, integer_zero_node);
9619 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9620 isinf_call, integer_zero_node);
9622 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9623 integer_minus_one_node, integer_one_node);
9624 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9625 isinf_call, tmp,
9626 integer_zero_node);
9629 return tmp;
9632 case BUILT_IN_ISFINITE:
9633 if (!HONOR_NANS (arg)
9634 && !HONOR_INFINITIES (arg))
9635 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9637 if (TREE_CODE (arg) == REAL_CST)
9639 r = TREE_REAL_CST (arg);
9640 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9643 return NULL_TREE;
9645 case BUILT_IN_ISNAN:
9646 if (!HONOR_NANS (arg))
9647 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9649 if (TREE_CODE (arg) == REAL_CST)
9651 r = TREE_REAL_CST (arg);
9652 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9655 arg = builtin_save_expr (arg);
9656 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9658 default:
9659 gcc_unreachable ();
9663 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9664 This builtin will generate code to return the appropriate floating
9665 point classification depending on the value of the floating point
9666 number passed in. The possible return values must be supplied as
9667 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9668 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9669 one floating point argument which is "type generic". */
9671 static tree
9672 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9674 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9675 arg, type, res, tmp;
9676 machine_mode mode;
9677 REAL_VALUE_TYPE r;
9678 char buf[128];
9680 /* Verify the required arguments in the original call. */
9681 if (nargs != 6
9682 || !validate_arg (args[0], INTEGER_TYPE)
9683 || !validate_arg (args[1], INTEGER_TYPE)
9684 || !validate_arg (args[2], INTEGER_TYPE)
9685 || !validate_arg (args[3], INTEGER_TYPE)
9686 || !validate_arg (args[4], INTEGER_TYPE)
9687 || !validate_arg (args[5], REAL_TYPE))
9688 return NULL_TREE;
9690 fp_nan = args[0];
9691 fp_infinite = args[1];
9692 fp_normal = args[2];
9693 fp_subnormal = args[3];
9694 fp_zero = args[4];
9695 arg = args[5];
9696 type = TREE_TYPE (arg);
9697 mode = TYPE_MODE (type);
9698 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9700 /* fpclassify(x) ->
9701 isnan(x) ? FP_NAN :
9702 (fabs(x) == Inf ? FP_INFINITE :
9703 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9704 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9706 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9707 build_real (type, dconst0));
9708 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9709 tmp, fp_zero, fp_subnormal);
9711 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9712 real_from_string (&r, buf);
9713 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9714 arg, build_real (type, r));
9715 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9717 if (HONOR_INFINITIES (mode))
9719 real_inf (&r);
9720 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9721 build_real (type, r));
9722 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9723 fp_infinite, res);
9726 if (HONOR_NANS (mode))
9728 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9729 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9732 return res;
9735 /* Fold a call to an unordered comparison function such as
9736 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9737 being called and ARG0 and ARG1 are the arguments for the call.
9738 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9739 the opposite of the desired result. UNORDERED_CODE is used
9740 for modes that can hold NaNs and ORDERED_CODE is used for
9741 the rest. */
9743 static tree
9744 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9745 enum tree_code unordered_code,
9746 enum tree_code ordered_code)
9748 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9749 enum tree_code code;
9750 tree type0, type1;
9751 enum tree_code code0, code1;
9752 tree cmp_type = NULL_TREE;
9754 type0 = TREE_TYPE (arg0);
9755 type1 = TREE_TYPE (arg1);
9757 code0 = TREE_CODE (type0);
9758 code1 = TREE_CODE (type1);
9760 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9761 /* Choose the wider of two real types. */
9762 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9763 ? type0 : type1;
9764 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9765 cmp_type = type0;
9766 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9767 cmp_type = type1;
9769 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9770 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9772 if (unordered_code == UNORDERED_EXPR)
9774 if (!HONOR_NANS (arg0))
9775 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9776 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9779 code = HONOR_NANS (arg0) ? unordered_code : ordered_code;
9780 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9781 fold_build2_loc (loc, code, type, arg0, arg1));
9784 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9785 arithmetics if it can never overflow, or into internal functions that
9786 return both result of arithmetics and overflowed boolean flag in
9787 a complex integer result, or some other check for overflow. */
9789 static tree
9790 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
9791 tree arg0, tree arg1, tree arg2)
9793 enum internal_fn ifn = IFN_LAST;
9794 tree type = TREE_TYPE (TREE_TYPE (arg2));
9795 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
9796 switch (fcode)
9798 case BUILT_IN_ADD_OVERFLOW:
9799 case BUILT_IN_SADD_OVERFLOW:
9800 case BUILT_IN_SADDL_OVERFLOW:
9801 case BUILT_IN_SADDLL_OVERFLOW:
9802 case BUILT_IN_UADD_OVERFLOW:
9803 case BUILT_IN_UADDL_OVERFLOW:
9804 case BUILT_IN_UADDLL_OVERFLOW:
9805 ifn = IFN_ADD_OVERFLOW;
9806 break;
9807 case BUILT_IN_SUB_OVERFLOW:
9808 case BUILT_IN_SSUB_OVERFLOW:
9809 case BUILT_IN_SSUBL_OVERFLOW:
9810 case BUILT_IN_SSUBLL_OVERFLOW:
9811 case BUILT_IN_USUB_OVERFLOW:
9812 case BUILT_IN_USUBL_OVERFLOW:
9813 case BUILT_IN_USUBLL_OVERFLOW:
9814 ifn = IFN_SUB_OVERFLOW;
9815 break;
9816 case BUILT_IN_MUL_OVERFLOW:
9817 case BUILT_IN_SMUL_OVERFLOW:
9818 case BUILT_IN_SMULL_OVERFLOW:
9819 case BUILT_IN_SMULLL_OVERFLOW:
9820 case BUILT_IN_UMUL_OVERFLOW:
9821 case BUILT_IN_UMULL_OVERFLOW:
9822 case BUILT_IN_UMULLL_OVERFLOW:
9823 ifn = IFN_MUL_OVERFLOW;
9824 break;
9825 default:
9826 gcc_unreachable ();
9828 tree ctype = build_complex_type (type);
9829 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
9830 2, arg0, arg1);
9831 tree tgt = save_expr (call);
9832 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9833 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9834 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
9835 tree store
9836 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
9837 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
9840 /* Fold a call to built-in function FNDECL with 0 arguments.
9841 This function returns NULL_TREE if no simplification was possible. */
9843 static tree
9844 fold_builtin_0 (location_t loc, tree fndecl)
9846 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9847 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9848 switch (fcode)
9850 CASE_FLT_FN (BUILT_IN_INF):
9851 case BUILT_IN_INFD32:
9852 case BUILT_IN_INFD64:
9853 case BUILT_IN_INFD128:
9854 return fold_builtin_inf (loc, type, true);
9856 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9857 return fold_builtin_inf (loc, type, false);
9859 case BUILT_IN_CLASSIFY_TYPE:
9860 return fold_builtin_classify_type (NULL_TREE);
9862 default:
9863 break;
9865 return NULL_TREE;
9868 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9869 This function returns NULL_TREE if no simplification was possible. */
9871 static tree
9872 fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
9874 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9875 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9876 switch (fcode)
9878 case BUILT_IN_CONSTANT_P:
9880 tree val = fold_builtin_constant_p (arg0);
9882 /* Gimplification will pull the CALL_EXPR for the builtin out of
9883 an if condition. When not optimizing, we'll not CSE it back.
9884 To avoid link error types of regressions, return false now. */
9885 if (!val && !optimize)
9886 val = integer_zero_node;
9888 return val;
9891 case BUILT_IN_CLASSIFY_TYPE:
9892 return fold_builtin_classify_type (arg0);
9894 case BUILT_IN_STRLEN:
9895 return fold_builtin_strlen (loc, type, arg0);
9897 CASE_FLT_FN (BUILT_IN_FABS):
9898 case BUILT_IN_FABSD32:
9899 case BUILT_IN_FABSD64:
9900 case BUILT_IN_FABSD128:
9901 return fold_builtin_fabs (loc, arg0, type);
9903 case BUILT_IN_ABS:
9904 case BUILT_IN_LABS:
9905 case BUILT_IN_LLABS:
9906 case BUILT_IN_IMAXABS:
9907 return fold_builtin_abs (loc, arg0, type);
9909 CASE_FLT_FN (BUILT_IN_CONJ):
9910 if (validate_arg (arg0, COMPLEX_TYPE)
9911 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9912 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9913 break;
9915 CASE_FLT_FN (BUILT_IN_CREAL):
9916 if (validate_arg (arg0, COMPLEX_TYPE)
9917 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9918 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9919 break;
9921 CASE_FLT_FN (BUILT_IN_CIMAG):
9922 if (validate_arg (arg0, COMPLEX_TYPE)
9923 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9924 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9925 break;
9927 CASE_FLT_FN (BUILT_IN_CCOS):
9928 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
9930 CASE_FLT_FN (BUILT_IN_CCOSH):
9931 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
9933 CASE_FLT_FN (BUILT_IN_CPROJ):
9934 return fold_builtin_cproj (loc, arg0, type);
9936 CASE_FLT_FN (BUILT_IN_CSIN):
9937 if (validate_arg (arg0, COMPLEX_TYPE)
9938 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9939 return do_mpc_arg1 (arg0, type, mpc_sin);
9940 break;
9942 CASE_FLT_FN (BUILT_IN_CSINH):
9943 if (validate_arg (arg0, COMPLEX_TYPE)
9944 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9945 return do_mpc_arg1 (arg0, type, mpc_sinh);
9946 break;
9948 CASE_FLT_FN (BUILT_IN_CTAN):
9949 if (validate_arg (arg0, COMPLEX_TYPE)
9950 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9951 return do_mpc_arg1 (arg0, type, mpc_tan);
9952 break;
9954 CASE_FLT_FN (BUILT_IN_CTANH):
9955 if (validate_arg (arg0, COMPLEX_TYPE)
9956 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9957 return do_mpc_arg1 (arg0, type, mpc_tanh);
9958 break;
9960 CASE_FLT_FN (BUILT_IN_CLOG):
9961 if (validate_arg (arg0, COMPLEX_TYPE)
9962 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9963 return do_mpc_arg1 (arg0, type, mpc_log);
9964 break;
9966 CASE_FLT_FN (BUILT_IN_CSQRT):
9967 if (validate_arg (arg0, COMPLEX_TYPE)
9968 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9969 return do_mpc_arg1 (arg0, type, mpc_sqrt);
9970 break;
9972 CASE_FLT_FN (BUILT_IN_CASIN):
9973 if (validate_arg (arg0, COMPLEX_TYPE)
9974 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9975 return do_mpc_arg1 (arg0, type, mpc_asin);
9976 break;
9978 CASE_FLT_FN (BUILT_IN_CACOS):
9979 if (validate_arg (arg0, COMPLEX_TYPE)
9980 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9981 return do_mpc_arg1 (arg0, type, mpc_acos);
9982 break;
9984 CASE_FLT_FN (BUILT_IN_CATAN):
9985 if (validate_arg (arg0, COMPLEX_TYPE)
9986 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9987 return do_mpc_arg1 (arg0, type, mpc_atan);
9988 break;
9990 CASE_FLT_FN (BUILT_IN_CASINH):
9991 if (validate_arg (arg0, COMPLEX_TYPE)
9992 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9993 return do_mpc_arg1 (arg0, type, mpc_asinh);
9994 break;
9996 CASE_FLT_FN (BUILT_IN_CACOSH):
9997 if (validate_arg (arg0, COMPLEX_TYPE)
9998 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9999 return do_mpc_arg1 (arg0, type, mpc_acosh);
10000 break;
10002 CASE_FLT_FN (BUILT_IN_CATANH):
10003 if (validate_arg (arg0, COMPLEX_TYPE)
10004 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10005 return do_mpc_arg1 (arg0, type, mpc_atanh);
10006 break;
10008 CASE_FLT_FN (BUILT_IN_CABS):
10009 return fold_builtin_cabs (loc, arg0, type, fndecl);
10011 CASE_FLT_FN (BUILT_IN_CARG):
10012 return fold_builtin_carg (loc, arg0, type);
10014 CASE_FLT_FN (BUILT_IN_SQRT):
10015 return fold_builtin_sqrt (loc, arg0, type);
10017 CASE_FLT_FN (BUILT_IN_CBRT):
10018 return fold_builtin_cbrt (loc, arg0, type);
10020 CASE_FLT_FN (BUILT_IN_ASIN):
10021 if (validate_arg (arg0, REAL_TYPE))
10022 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10023 &dconstm1, &dconst1, true);
10024 break;
10026 CASE_FLT_FN (BUILT_IN_ACOS):
10027 if (validate_arg (arg0, REAL_TYPE))
10028 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10029 &dconstm1, &dconst1, true);
10030 break;
10032 CASE_FLT_FN (BUILT_IN_ATAN):
10033 if (validate_arg (arg0, REAL_TYPE))
10034 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10035 break;
10037 CASE_FLT_FN (BUILT_IN_ASINH):
10038 if (validate_arg (arg0, REAL_TYPE))
10039 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10040 break;
10042 CASE_FLT_FN (BUILT_IN_ACOSH):
10043 if (validate_arg (arg0, REAL_TYPE))
10044 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10045 &dconst1, NULL, true);
10046 break;
10048 CASE_FLT_FN (BUILT_IN_ATANH):
10049 if (validate_arg (arg0, REAL_TYPE))
10050 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10051 &dconstm1, &dconst1, false);
10052 break;
10054 CASE_FLT_FN (BUILT_IN_SIN):
10055 if (validate_arg (arg0, REAL_TYPE))
10056 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10057 break;
10059 CASE_FLT_FN (BUILT_IN_COS):
10060 return fold_builtin_cos (loc, arg0, type, fndecl);
10062 CASE_FLT_FN (BUILT_IN_TAN):
10063 return fold_builtin_tan (arg0, type);
10065 CASE_FLT_FN (BUILT_IN_CEXP):
10066 return fold_builtin_cexp (loc, arg0, type);
10068 CASE_FLT_FN (BUILT_IN_CEXPI):
10069 if (validate_arg (arg0, REAL_TYPE))
10070 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10071 break;
10073 CASE_FLT_FN (BUILT_IN_SINH):
10074 if (validate_arg (arg0, REAL_TYPE))
10075 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10076 break;
10078 CASE_FLT_FN (BUILT_IN_COSH):
10079 return fold_builtin_cosh (loc, arg0, type, fndecl);
10081 CASE_FLT_FN (BUILT_IN_TANH):
10082 if (validate_arg (arg0, REAL_TYPE))
10083 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10084 break;
10086 CASE_FLT_FN (BUILT_IN_ERF):
10087 if (validate_arg (arg0, REAL_TYPE))
10088 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10089 break;
10091 CASE_FLT_FN (BUILT_IN_ERFC):
10092 if (validate_arg (arg0, REAL_TYPE))
10093 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10094 break;
10096 CASE_FLT_FN (BUILT_IN_TGAMMA):
10097 if (validate_arg (arg0, REAL_TYPE))
10098 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10099 break;
10101 CASE_FLT_FN (BUILT_IN_EXP):
10102 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10104 CASE_FLT_FN (BUILT_IN_EXP2):
10105 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10107 CASE_FLT_FN (BUILT_IN_EXP10):
10108 CASE_FLT_FN (BUILT_IN_POW10):
10109 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10111 CASE_FLT_FN (BUILT_IN_EXPM1):
10112 if (validate_arg (arg0, REAL_TYPE))
10113 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10114 break;
10116 CASE_FLT_FN (BUILT_IN_LOG):
10117 if (validate_arg (arg0, REAL_TYPE))
10118 return do_mpfr_arg1 (arg0, type, mpfr_log, &dconst0, NULL, false);
10119 break;
10121 CASE_FLT_FN (BUILT_IN_LOG2):
10122 if (validate_arg (arg0, REAL_TYPE))
10123 return do_mpfr_arg1 (arg0, type, mpfr_log2, &dconst0, NULL, false);
10124 break;
10126 CASE_FLT_FN (BUILT_IN_LOG10):
10127 if (validate_arg (arg0, REAL_TYPE))
10128 return do_mpfr_arg1 (arg0, type, mpfr_log10, &dconst0, NULL, false);
10129 break;
10131 CASE_FLT_FN (BUILT_IN_LOG1P):
10132 if (validate_arg (arg0, REAL_TYPE))
10133 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10134 &dconstm1, NULL, false);
10135 break;
10137 CASE_FLT_FN (BUILT_IN_J0):
10138 if (validate_arg (arg0, REAL_TYPE))
10139 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10140 NULL, NULL, 0);
10141 break;
10143 CASE_FLT_FN (BUILT_IN_J1):
10144 if (validate_arg (arg0, REAL_TYPE))
10145 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10146 NULL, NULL, 0);
10147 break;
10149 CASE_FLT_FN (BUILT_IN_Y0):
10150 if (validate_arg (arg0, REAL_TYPE))
10151 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10152 &dconst0, NULL, false);
10153 break;
10155 CASE_FLT_FN (BUILT_IN_Y1):
10156 if (validate_arg (arg0, REAL_TYPE))
10157 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10158 &dconst0, NULL, false);
10159 break;
10161 CASE_FLT_FN (BUILT_IN_NAN):
10162 case BUILT_IN_NAND32:
10163 case BUILT_IN_NAND64:
10164 case BUILT_IN_NAND128:
10165 return fold_builtin_nan (arg0, type, true);
10167 CASE_FLT_FN (BUILT_IN_NANS):
10168 return fold_builtin_nan (arg0, type, false);
10170 CASE_FLT_FN (BUILT_IN_FLOOR):
10171 return fold_builtin_floor (loc, fndecl, arg0);
10173 CASE_FLT_FN (BUILT_IN_CEIL):
10174 return fold_builtin_ceil (loc, fndecl, arg0);
10176 CASE_FLT_FN (BUILT_IN_TRUNC):
10177 return fold_builtin_trunc (loc, fndecl, arg0);
10179 CASE_FLT_FN (BUILT_IN_ROUND):
10180 return fold_builtin_round (loc, fndecl, arg0);
10182 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10183 CASE_FLT_FN (BUILT_IN_RINT):
10184 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10186 CASE_FLT_FN (BUILT_IN_ICEIL):
10187 CASE_FLT_FN (BUILT_IN_LCEIL):
10188 CASE_FLT_FN (BUILT_IN_LLCEIL):
10189 CASE_FLT_FN (BUILT_IN_LFLOOR):
10190 CASE_FLT_FN (BUILT_IN_IFLOOR):
10191 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10192 CASE_FLT_FN (BUILT_IN_IROUND):
10193 CASE_FLT_FN (BUILT_IN_LROUND):
10194 CASE_FLT_FN (BUILT_IN_LLROUND):
10195 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10197 CASE_FLT_FN (BUILT_IN_IRINT):
10198 CASE_FLT_FN (BUILT_IN_LRINT):
10199 CASE_FLT_FN (BUILT_IN_LLRINT):
10200 return fold_fixed_mathfn (loc, fndecl, arg0);
10202 case BUILT_IN_BSWAP16:
10203 case BUILT_IN_BSWAP32:
10204 case BUILT_IN_BSWAP64:
10205 return fold_builtin_bswap (fndecl, arg0);
10207 CASE_INT_FN (BUILT_IN_FFS):
10208 CASE_INT_FN (BUILT_IN_CLZ):
10209 CASE_INT_FN (BUILT_IN_CTZ):
10210 CASE_INT_FN (BUILT_IN_CLRSB):
10211 CASE_INT_FN (BUILT_IN_POPCOUNT):
10212 CASE_INT_FN (BUILT_IN_PARITY):
10213 return fold_builtin_bitop (fndecl, arg0);
10215 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10216 return fold_builtin_signbit (loc, arg0, type);
10218 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10219 return fold_builtin_significand (loc, arg0, type);
10221 CASE_FLT_FN (BUILT_IN_ILOGB):
10222 CASE_FLT_FN (BUILT_IN_LOGB):
10223 return fold_builtin_logb (loc, arg0, type);
10225 case BUILT_IN_ISASCII:
10226 return fold_builtin_isascii (loc, arg0);
10228 case BUILT_IN_TOASCII:
10229 return fold_builtin_toascii (loc, arg0);
10231 case BUILT_IN_ISDIGIT:
10232 return fold_builtin_isdigit (loc, arg0);
10234 CASE_FLT_FN (BUILT_IN_FINITE):
10235 case BUILT_IN_FINITED32:
10236 case BUILT_IN_FINITED64:
10237 case BUILT_IN_FINITED128:
10238 case BUILT_IN_ISFINITE:
10240 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10241 if (ret)
10242 return ret;
10243 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10246 CASE_FLT_FN (BUILT_IN_ISINF):
10247 case BUILT_IN_ISINFD32:
10248 case BUILT_IN_ISINFD64:
10249 case BUILT_IN_ISINFD128:
10251 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10252 if (ret)
10253 return ret;
10254 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10257 case BUILT_IN_ISNORMAL:
10258 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10260 case BUILT_IN_ISINF_SIGN:
10261 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10263 CASE_FLT_FN (BUILT_IN_ISNAN):
10264 case BUILT_IN_ISNAND32:
10265 case BUILT_IN_ISNAND64:
10266 case BUILT_IN_ISNAND128:
10267 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10269 case BUILT_IN_FREE:
10270 if (integer_zerop (arg0))
10271 return build_empty_stmt (loc);
10272 break;
10274 default:
10275 break;
10278 return NULL_TREE;
10282 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10283 This function returns NULL_TREE if no simplification was possible. */
10285 static tree
10286 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
10288 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10289 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10291 switch (fcode)
10293 CASE_FLT_FN (BUILT_IN_JN):
10294 if (validate_arg (arg0, INTEGER_TYPE)
10295 && validate_arg (arg1, REAL_TYPE))
10296 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10297 break;
10299 CASE_FLT_FN (BUILT_IN_YN):
10300 if (validate_arg (arg0, INTEGER_TYPE)
10301 && validate_arg (arg1, REAL_TYPE))
10302 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10303 &dconst0, false);
10304 break;
10306 CASE_FLT_FN (BUILT_IN_DREM):
10307 CASE_FLT_FN (BUILT_IN_REMAINDER):
10308 if (validate_arg (arg0, REAL_TYPE)
10309 && validate_arg (arg1, REAL_TYPE))
10310 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10311 break;
10313 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10314 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10315 if (validate_arg (arg0, REAL_TYPE)
10316 && validate_arg (arg1, POINTER_TYPE))
10317 return do_mpfr_lgamma_r (arg0, arg1, type);
10318 break;
10320 CASE_FLT_FN (BUILT_IN_ATAN2):
10321 if (validate_arg (arg0, REAL_TYPE)
10322 && validate_arg (arg1, REAL_TYPE))
10323 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10324 break;
10326 CASE_FLT_FN (BUILT_IN_FDIM):
10327 if (validate_arg (arg0, REAL_TYPE)
10328 && validate_arg (arg1, REAL_TYPE))
10329 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10330 break;
10332 CASE_FLT_FN (BUILT_IN_HYPOT):
10333 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10335 CASE_FLT_FN (BUILT_IN_CPOW):
10336 if (validate_arg (arg0, COMPLEX_TYPE)
10337 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10338 && validate_arg (arg1, COMPLEX_TYPE)
10339 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10340 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10341 break;
10343 CASE_FLT_FN (BUILT_IN_LDEXP):
10344 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10345 CASE_FLT_FN (BUILT_IN_SCALBN):
10346 CASE_FLT_FN (BUILT_IN_SCALBLN):
10347 return fold_builtin_load_exponent (loc, arg0, arg1,
10348 type, /*ldexp=*/false);
10350 CASE_FLT_FN (BUILT_IN_FREXP):
10351 return fold_builtin_frexp (loc, arg0, arg1, type);
10353 CASE_FLT_FN (BUILT_IN_MODF):
10354 return fold_builtin_modf (loc, arg0, arg1, type);
10356 case BUILT_IN_STRSTR:
10357 return fold_builtin_strstr (loc, arg0, arg1, type);
10359 case BUILT_IN_STRSPN:
10360 return fold_builtin_strspn (loc, arg0, arg1);
10362 case BUILT_IN_STRCSPN:
10363 return fold_builtin_strcspn (loc, arg0, arg1);
10365 case BUILT_IN_STRCHR:
10366 case BUILT_IN_INDEX:
10367 return fold_builtin_strchr (loc, arg0, arg1, type);
10369 case BUILT_IN_STRRCHR:
10370 case BUILT_IN_RINDEX:
10371 return fold_builtin_strrchr (loc, arg0, arg1, type);
10373 case BUILT_IN_STRCMP:
10374 return fold_builtin_strcmp (loc, arg0, arg1);
10376 case BUILT_IN_STRPBRK:
10377 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10379 case BUILT_IN_EXPECT:
10380 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
10382 CASE_FLT_FN (BUILT_IN_POW):
10383 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10385 CASE_FLT_FN (BUILT_IN_POWI):
10386 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10388 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10389 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10391 CASE_FLT_FN (BUILT_IN_FMIN):
10392 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10394 CASE_FLT_FN (BUILT_IN_FMAX):
10395 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10397 case BUILT_IN_ISGREATER:
10398 return fold_builtin_unordered_cmp (loc, fndecl,
10399 arg0, arg1, UNLE_EXPR, LE_EXPR);
10400 case BUILT_IN_ISGREATEREQUAL:
10401 return fold_builtin_unordered_cmp (loc, fndecl,
10402 arg0, arg1, UNLT_EXPR, LT_EXPR);
10403 case BUILT_IN_ISLESS:
10404 return fold_builtin_unordered_cmp (loc, fndecl,
10405 arg0, arg1, UNGE_EXPR, GE_EXPR);
10406 case BUILT_IN_ISLESSEQUAL:
10407 return fold_builtin_unordered_cmp (loc, fndecl,
10408 arg0, arg1, UNGT_EXPR, GT_EXPR);
10409 case BUILT_IN_ISLESSGREATER:
10410 return fold_builtin_unordered_cmp (loc, fndecl,
10411 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10412 case BUILT_IN_ISUNORDERED:
10413 return fold_builtin_unordered_cmp (loc, fndecl,
10414 arg0, arg1, UNORDERED_EXPR,
10415 NOP_EXPR);
10417 /* We do the folding for va_start in the expander. */
10418 case BUILT_IN_VA_START:
10419 break;
10421 case BUILT_IN_OBJECT_SIZE:
10422 return fold_builtin_object_size (arg0, arg1);
10424 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10425 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10427 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10428 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10430 default:
10431 break;
10433 return NULL_TREE;
10436 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10437 and ARG2.
10438 This function returns NULL_TREE if no simplification was possible. */
10440 static tree
10441 fold_builtin_3 (location_t loc, tree fndecl,
10442 tree arg0, tree arg1, tree arg2)
10444 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10445 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10446 switch (fcode)
10449 CASE_FLT_FN (BUILT_IN_SINCOS):
10450 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10452 CASE_FLT_FN (BUILT_IN_FMA):
10453 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10454 break;
10456 CASE_FLT_FN (BUILT_IN_REMQUO):
10457 if (validate_arg (arg0, REAL_TYPE)
10458 && validate_arg (arg1, REAL_TYPE)
10459 && validate_arg (arg2, POINTER_TYPE))
10460 return do_mpfr_remquo (arg0, arg1, arg2);
10461 break;
10463 case BUILT_IN_STRNCMP:
10464 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10466 case BUILT_IN_MEMCHR:
10467 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10469 case BUILT_IN_BCMP:
10470 case BUILT_IN_MEMCMP:
10471 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10473 case BUILT_IN_EXPECT:
10474 return fold_builtin_expect (loc, arg0, arg1, arg2);
10476 case BUILT_IN_ADD_OVERFLOW:
10477 case BUILT_IN_SUB_OVERFLOW:
10478 case BUILT_IN_MUL_OVERFLOW:
10479 case BUILT_IN_SADD_OVERFLOW:
10480 case BUILT_IN_SADDL_OVERFLOW:
10481 case BUILT_IN_SADDLL_OVERFLOW:
10482 case BUILT_IN_SSUB_OVERFLOW:
10483 case BUILT_IN_SSUBL_OVERFLOW:
10484 case BUILT_IN_SSUBLL_OVERFLOW:
10485 case BUILT_IN_SMUL_OVERFLOW:
10486 case BUILT_IN_SMULL_OVERFLOW:
10487 case BUILT_IN_SMULLL_OVERFLOW:
10488 case BUILT_IN_UADD_OVERFLOW:
10489 case BUILT_IN_UADDL_OVERFLOW:
10490 case BUILT_IN_UADDLL_OVERFLOW:
10491 case BUILT_IN_USUB_OVERFLOW:
10492 case BUILT_IN_USUBL_OVERFLOW:
10493 case BUILT_IN_USUBLL_OVERFLOW:
10494 case BUILT_IN_UMUL_OVERFLOW:
10495 case BUILT_IN_UMULL_OVERFLOW:
10496 case BUILT_IN_UMULLL_OVERFLOW:
10497 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
10499 default:
10500 break;
10502 return NULL_TREE;
10505 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10506 arguments. IGNORE is true if the result of the
10507 function call is ignored. This function returns NULL_TREE if no
10508 simplification was possible. */
10510 tree
10511 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool)
10513 tree ret = NULL_TREE;
10515 switch (nargs)
10517 case 0:
10518 ret = fold_builtin_0 (loc, fndecl);
10519 break;
10520 case 1:
10521 ret = fold_builtin_1 (loc, fndecl, args[0]);
10522 break;
10523 case 2:
10524 ret = fold_builtin_2 (loc, fndecl, args[0], args[1]);
10525 break;
10526 case 3:
10527 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
10528 break;
10529 default:
10530 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
10531 break;
10533 if (ret)
10535 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10536 SET_EXPR_LOCATION (ret, loc);
10537 TREE_NO_WARNING (ret) = 1;
10538 return ret;
10540 return NULL_TREE;
10543 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10544 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10545 of arguments in ARGS to be omitted. OLDNARGS is the number of
10546 elements in ARGS. */
10548 static tree
10549 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10550 int skip, tree fndecl, int n, va_list newargs)
10552 int nargs = oldnargs - skip + n;
10553 tree *buffer;
10555 if (n > 0)
10557 int i, j;
10559 buffer = XALLOCAVEC (tree, nargs);
10560 for (i = 0; i < n; i++)
10561 buffer[i] = va_arg (newargs, tree);
10562 for (j = skip; j < oldnargs; j++, i++)
10563 buffer[i] = args[j];
10565 else
10566 buffer = args + skip;
10568 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10571 /* Return true if FNDECL shouldn't be folded right now.
10572 If a built-in function has an inline attribute always_inline
10573 wrapper, defer folding it after always_inline functions have
10574 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10575 might not be performed. */
10577 bool
10578 avoid_folding_inline_builtin (tree fndecl)
10580 return (DECL_DECLARED_INLINE_P (fndecl)
10581 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10582 && cfun
10583 && !cfun->always_inline_functions_inlined
10584 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10587 /* A wrapper function for builtin folding that prevents warnings for
10588 "statement without effect" and the like, caused by removing the
10589 call node earlier than the warning is generated. */
10591 tree
10592 fold_call_expr (location_t loc, tree exp, bool ignore)
10594 tree ret = NULL_TREE;
10595 tree fndecl = get_callee_fndecl (exp);
10596 if (fndecl
10597 && TREE_CODE (fndecl) == FUNCTION_DECL
10598 && DECL_BUILT_IN (fndecl)
10599 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10600 yet. Defer folding until we see all the arguments
10601 (after inlining). */
10602 && !CALL_EXPR_VA_ARG_PACK (exp))
10604 int nargs = call_expr_nargs (exp);
10606 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10607 instead last argument is __builtin_va_arg_pack (). Defer folding
10608 even in that case, until arguments are finalized. */
10609 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10611 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10612 if (fndecl2
10613 && TREE_CODE (fndecl2) == FUNCTION_DECL
10614 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10615 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10616 return NULL_TREE;
10619 if (avoid_folding_inline_builtin (fndecl))
10620 return NULL_TREE;
10622 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10623 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10624 CALL_EXPR_ARGP (exp), ignore);
10625 else
10627 tree *args = CALL_EXPR_ARGP (exp);
10628 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10629 if (ret)
10630 return ret;
10633 return NULL_TREE;
10636 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
10637 N arguments are passed in the array ARGARRAY. Return a folded
10638 expression or NULL_TREE if no simplification was possible. */
10640 tree
10641 fold_builtin_call_array (location_t loc, tree,
10642 tree fn,
10643 int n,
10644 tree *argarray)
10646 if (TREE_CODE (fn) != ADDR_EXPR)
10647 return NULL_TREE;
10649 tree fndecl = TREE_OPERAND (fn, 0);
10650 if (TREE_CODE (fndecl) == FUNCTION_DECL
10651 && DECL_BUILT_IN (fndecl))
10653 /* If last argument is __builtin_va_arg_pack (), arguments to this
10654 function are not finalized yet. Defer folding until they are. */
10655 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10657 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10658 if (fndecl2
10659 && TREE_CODE (fndecl2) == FUNCTION_DECL
10660 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10661 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10662 return NULL_TREE;
10664 if (avoid_folding_inline_builtin (fndecl))
10665 return NULL_TREE;
10666 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10667 return targetm.fold_builtin (fndecl, n, argarray, false);
10668 else
10669 return fold_builtin_n (loc, fndecl, argarray, n, false);
10672 return NULL_TREE;
10675 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10676 along with N new arguments specified as the "..." parameters. SKIP
10677 is the number of arguments in EXP to be omitted. This function is used
10678 to do varargs-to-varargs transformations. */
10680 static tree
10681 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10683 va_list ap;
10684 tree t;
10686 va_start (ap, n);
10687 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10688 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10689 va_end (ap);
10691 return t;
10694 /* Validate a single argument ARG against a tree code CODE representing
10695 a type. */
10697 static bool
10698 validate_arg (const_tree arg, enum tree_code code)
10700 if (!arg)
10701 return false;
10702 else if (code == POINTER_TYPE)
10703 return POINTER_TYPE_P (TREE_TYPE (arg));
10704 else if (code == INTEGER_TYPE)
10705 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10706 return code == TREE_CODE (TREE_TYPE (arg));
10709 /* This function validates the types of a function call argument list
10710 against a specified list of tree_codes. If the last specifier is a 0,
10711 that represents an ellipses, otherwise the last specifier must be a
10712 VOID_TYPE.
10714 This is the GIMPLE version of validate_arglist. Eventually we want to
10715 completely convert builtins.c to work from GIMPLEs and the tree based
10716 validate_arglist will then be removed. */
10718 bool
10719 validate_gimple_arglist (const gcall *call, ...)
10721 enum tree_code code;
10722 bool res = 0;
10723 va_list ap;
10724 const_tree arg;
10725 size_t i;
10727 va_start (ap, call);
10728 i = 0;
10732 code = (enum tree_code) va_arg (ap, int);
10733 switch (code)
10735 case 0:
10736 /* This signifies an ellipses, any further arguments are all ok. */
10737 res = true;
10738 goto end;
10739 case VOID_TYPE:
10740 /* This signifies an endlink, if no arguments remain, return
10741 true, otherwise return false. */
10742 res = (i == gimple_call_num_args (call));
10743 goto end;
10744 default:
10745 /* If no parameters remain or the parameter's code does not
10746 match the specified code, return false. Otherwise continue
10747 checking any remaining arguments. */
10748 arg = gimple_call_arg (call, i++);
10749 if (!validate_arg (arg, code))
10750 goto end;
10751 break;
10754 while (1);
10756 /* We need gotos here since we can only have one VA_CLOSE in a
10757 function. */
10758 end: ;
10759 va_end (ap);
10761 return res;
10764 /* Default target-specific builtin expander that does nothing. */
10767 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10768 rtx target ATTRIBUTE_UNUSED,
10769 rtx subtarget ATTRIBUTE_UNUSED,
10770 machine_mode mode ATTRIBUTE_UNUSED,
10771 int ignore ATTRIBUTE_UNUSED)
10773 return NULL_RTX;
10776 /* Returns true is EXP represents data that would potentially reside
10777 in a readonly section. */
10779 bool
10780 readonly_data_expr (tree exp)
10782 STRIP_NOPS (exp);
10784 if (TREE_CODE (exp) != ADDR_EXPR)
10785 return false;
10787 exp = get_base_address (TREE_OPERAND (exp, 0));
10788 if (!exp)
10789 return false;
10791 /* Make sure we call decl_readonly_section only for trees it
10792 can handle (since it returns true for everything it doesn't
10793 understand). */
10794 if (TREE_CODE (exp) == STRING_CST
10795 || TREE_CODE (exp) == CONSTRUCTOR
10796 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
10797 return decl_readonly_section (exp, 0);
10798 else
10799 return false;
10802 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10803 to the call, and TYPE is its return type.
10805 Return NULL_TREE if no simplification was possible, otherwise return the
10806 simplified form of the call as a tree.
10808 The simplified form may be a constant or other expression which
10809 computes the same value, but in a more efficient manner (including
10810 calls to other builtin functions).
10812 The call may contain arguments which need to be evaluated, but
10813 which are not useful to determine the result of the call. In
10814 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10815 COMPOUND_EXPR will be an argument which must be evaluated.
10816 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10817 COMPOUND_EXPR in the chain will contain the tree for the simplified
10818 form of the builtin function call. */
10820 static tree
10821 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
10823 if (!validate_arg (s1, POINTER_TYPE)
10824 || !validate_arg (s2, POINTER_TYPE))
10825 return NULL_TREE;
10826 else
10828 tree fn;
10829 const char *p1, *p2;
10831 p2 = c_getstr (s2);
10832 if (p2 == NULL)
10833 return NULL_TREE;
10835 p1 = c_getstr (s1);
10836 if (p1 != NULL)
10838 const char *r = strstr (p1, p2);
10839 tree tem;
10841 if (r == NULL)
10842 return build_int_cst (TREE_TYPE (s1), 0);
10844 /* Return an offset into the constant string argument. */
10845 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10846 return fold_convert_loc (loc, type, tem);
10849 /* The argument is const char *, and the result is char *, so we need
10850 a type conversion here to avoid a warning. */
10851 if (p2[0] == '\0')
10852 return fold_convert_loc (loc, type, s1);
10854 if (p2[1] != '\0')
10855 return NULL_TREE;
10857 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10858 if (!fn)
10859 return NULL_TREE;
10861 /* New argument list transforming strstr(s1, s2) to
10862 strchr(s1, s2[0]). */
10863 return build_call_expr_loc (loc, fn, 2, s1,
10864 build_int_cst (integer_type_node, p2[0]));
10868 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10869 the call, and TYPE is its return type.
10871 Return NULL_TREE if no simplification was possible, otherwise return the
10872 simplified form of the call as a tree.
10874 The simplified form may be a constant or other expression which
10875 computes the same value, but in a more efficient manner (including
10876 calls to other builtin functions).
10878 The call may contain arguments which need to be evaluated, but
10879 which are not useful to determine the result of the call. In
10880 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10881 COMPOUND_EXPR will be an argument which must be evaluated.
10882 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10883 COMPOUND_EXPR in the chain will contain the tree for the simplified
10884 form of the builtin function call. */
10886 static tree
10887 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
10889 if (!validate_arg (s1, POINTER_TYPE)
10890 || !validate_arg (s2, INTEGER_TYPE))
10891 return NULL_TREE;
10892 else
10894 const char *p1;
10896 if (TREE_CODE (s2) != INTEGER_CST)
10897 return NULL_TREE;
10899 p1 = c_getstr (s1);
10900 if (p1 != NULL)
10902 char c;
10903 const char *r;
10904 tree tem;
10906 if (target_char_cast (s2, &c))
10907 return NULL_TREE;
10909 r = strchr (p1, c);
10911 if (r == NULL)
10912 return build_int_cst (TREE_TYPE (s1), 0);
10914 /* Return an offset into the constant string argument. */
10915 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10916 return fold_convert_loc (loc, type, tem);
10918 return NULL_TREE;
10922 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10923 the call, and TYPE is its return type.
10925 Return NULL_TREE if no simplification was possible, otherwise return the
10926 simplified form of the call as a tree.
10928 The simplified form may be a constant or other expression which
10929 computes the same value, but in a more efficient manner (including
10930 calls to other builtin functions).
10932 The call may contain arguments which need to be evaluated, but
10933 which are not useful to determine the result of the call. In
10934 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10935 COMPOUND_EXPR will be an argument which must be evaluated.
10936 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10937 COMPOUND_EXPR in the chain will contain the tree for the simplified
10938 form of the builtin function call. */
10940 static tree
10941 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
10943 if (!validate_arg (s1, POINTER_TYPE)
10944 || !validate_arg (s2, INTEGER_TYPE))
10945 return NULL_TREE;
10946 else
10948 tree fn;
10949 const char *p1;
10951 if (TREE_CODE (s2) != INTEGER_CST)
10952 return NULL_TREE;
10954 p1 = c_getstr (s1);
10955 if (p1 != NULL)
10957 char c;
10958 const char *r;
10959 tree tem;
10961 if (target_char_cast (s2, &c))
10962 return NULL_TREE;
10964 r = strrchr (p1, c);
10966 if (r == NULL)
10967 return build_int_cst (TREE_TYPE (s1), 0);
10969 /* Return an offset into the constant string argument. */
10970 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10971 return fold_convert_loc (loc, type, tem);
10974 if (! integer_zerop (s2))
10975 return NULL_TREE;
10977 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10978 if (!fn)
10979 return NULL_TREE;
10981 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
10982 return build_call_expr_loc (loc, fn, 2, s1, s2);
10986 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
10987 to the call, and TYPE is its return type.
10989 Return NULL_TREE if no simplification was possible, otherwise return the
10990 simplified form of the call as a tree.
10992 The simplified form may be a constant or other expression which
10993 computes the same value, but in a more efficient manner (including
10994 calls to other builtin functions).
10996 The call may contain arguments which need to be evaluated, but
10997 which are not useful to determine the result of the call. In
10998 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10999 COMPOUND_EXPR will be an argument which must be evaluated.
11000 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11001 COMPOUND_EXPR in the chain will contain the tree for the simplified
11002 form of the builtin function call. */
11004 static tree
11005 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11007 if (!validate_arg (s1, POINTER_TYPE)
11008 || !validate_arg (s2, POINTER_TYPE))
11009 return NULL_TREE;
11010 else
11012 tree fn;
11013 const char *p1, *p2;
11015 p2 = c_getstr (s2);
11016 if (p2 == NULL)
11017 return NULL_TREE;
11019 p1 = c_getstr (s1);
11020 if (p1 != NULL)
11022 const char *r = strpbrk (p1, p2);
11023 tree tem;
11025 if (r == NULL)
11026 return build_int_cst (TREE_TYPE (s1), 0);
11028 /* Return an offset into the constant string argument. */
11029 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11030 return fold_convert_loc (loc, type, tem);
11033 if (p2[0] == '\0')
11034 /* strpbrk(x, "") == NULL.
11035 Evaluate and ignore s1 in case it had side-effects. */
11036 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11038 if (p2[1] != '\0')
11039 return NULL_TREE; /* Really call strpbrk. */
11041 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11042 if (!fn)
11043 return NULL_TREE;
11045 /* New argument list transforming strpbrk(s1, s2) to
11046 strchr(s1, s2[0]). */
11047 return build_call_expr_loc (loc, fn, 2, s1,
11048 build_int_cst (integer_type_node, p2[0]));
11052 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11053 to the call.
11055 Return NULL_TREE if no simplification was possible, otherwise return the
11056 simplified form of the call as a tree.
11058 The simplified form may be a constant or other expression which
11059 computes the same value, but in a more efficient manner (including
11060 calls to other builtin functions).
11062 The call may contain arguments which need to be evaluated, but
11063 which are not useful to determine the result of the call. In
11064 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11065 COMPOUND_EXPR will be an argument which must be evaluated.
11066 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11067 COMPOUND_EXPR in the chain will contain the tree for the simplified
11068 form of the builtin function call. */
11070 static tree
11071 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11073 if (!validate_arg (s1, POINTER_TYPE)
11074 || !validate_arg (s2, POINTER_TYPE))
11075 return NULL_TREE;
11076 else
11078 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11080 /* If both arguments are constants, evaluate at compile-time. */
11081 if (p1 && p2)
11083 const size_t r = strspn (p1, p2);
11084 return build_int_cst (size_type_node, r);
11087 /* If either argument is "", return NULL_TREE. */
11088 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11089 /* Evaluate and ignore both arguments in case either one has
11090 side-effects. */
11091 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11092 s1, s2);
11093 return NULL_TREE;
11097 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11098 to the call.
11100 Return NULL_TREE if no simplification was possible, otherwise return the
11101 simplified form of the call as a tree.
11103 The simplified form may be a constant or other expression which
11104 computes the same value, but in a more efficient manner (including
11105 calls to other builtin functions).
11107 The call may contain arguments which need to be evaluated, but
11108 which are not useful to determine the result of the call. In
11109 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11110 COMPOUND_EXPR will be an argument which must be evaluated.
11111 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11112 COMPOUND_EXPR in the chain will contain the tree for the simplified
11113 form of the builtin function call. */
11115 static tree
11116 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11118 if (!validate_arg (s1, POINTER_TYPE)
11119 || !validate_arg (s2, POINTER_TYPE))
11120 return NULL_TREE;
11121 else
11123 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11125 /* If both arguments are constants, evaluate at compile-time. */
11126 if (p1 && p2)
11128 const size_t r = strcspn (p1, p2);
11129 return build_int_cst (size_type_node, r);
11132 /* If the first argument is "", return NULL_TREE. */
11133 if (p1 && *p1 == '\0')
11135 /* Evaluate and ignore argument s2 in case it has
11136 side-effects. */
11137 return omit_one_operand_loc (loc, size_type_node,
11138 size_zero_node, s2);
11141 /* If the second argument is "", return __builtin_strlen(s1). */
11142 if (p2 && *p2 == '\0')
11144 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11146 /* If the replacement _DECL isn't initialized, don't do the
11147 transformation. */
11148 if (!fn)
11149 return NULL_TREE;
11151 return build_call_expr_loc (loc, fn, 1, s1);
11153 return NULL_TREE;
11157 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11158 produced. False otherwise. This is done so that we don't output the error
11159 or warning twice or three times. */
11161 bool
11162 fold_builtin_next_arg (tree exp, bool va_start_p)
11164 tree fntype = TREE_TYPE (current_function_decl);
11165 int nargs = call_expr_nargs (exp);
11166 tree arg;
11167 /* There is good chance the current input_location points inside the
11168 definition of the va_start macro (perhaps on the token for
11169 builtin) in a system header, so warnings will not be emitted.
11170 Use the location in real source code. */
11171 source_location current_location =
11172 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11173 NULL);
11175 if (!stdarg_p (fntype))
11177 error ("%<va_start%> used in function with fixed args");
11178 return true;
11181 if (va_start_p)
11183 if (va_start_p && (nargs != 2))
11185 error ("wrong number of arguments to function %<va_start%>");
11186 return true;
11188 arg = CALL_EXPR_ARG (exp, 1);
11190 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11191 when we checked the arguments and if needed issued a warning. */
11192 else
11194 if (nargs == 0)
11196 /* Evidently an out of date version of <stdarg.h>; can't validate
11197 va_start's second argument, but can still work as intended. */
11198 warning_at (current_location,
11199 OPT_Wvarargs,
11200 "%<__builtin_next_arg%> called without an argument");
11201 return true;
11203 else if (nargs > 1)
11205 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11206 return true;
11208 arg = CALL_EXPR_ARG (exp, 0);
11211 if (TREE_CODE (arg) == SSA_NAME)
11212 arg = SSA_NAME_VAR (arg);
11214 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11215 or __builtin_next_arg (0) the first time we see it, after checking
11216 the arguments and if needed issuing a warning. */
11217 if (!integer_zerop (arg))
11219 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11221 /* Strip off all nops for the sake of the comparison. This
11222 is not quite the same as STRIP_NOPS. It does more.
11223 We must also strip off INDIRECT_EXPR for C++ reference
11224 parameters. */
11225 while (CONVERT_EXPR_P (arg)
11226 || TREE_CODE (arg) == INDIRECT_REF)
11227 arg = TREE_OPERAND (arg, 0);
11228 if (arg != last_parm)
11230 /* FIXME: Sometimes with the tree optimizers we can get the
11231 not the last argument even though the user used the last
11232 argument. We just warn and set the arg to be the last
11233 argument so that we will get wrong-code because of
11234 it. */
11235 warning_at (current_location,
11236 OPT_Wvarargs,
11237 "second parameter of %<va_start%> not last named argument");
11240 /* Undefined by C99 7.15.1.4p4 (va_start):
11241 "If the parameter parmN is declared with the register storage
11242 class, with a function or array type, or with a type that is
11243 not compatible with the type that results after application of
11244 the default argument promotions, the behavior is undefined."
11246 else if (DECL_REGISTER (arg))
11248 warning_at (current_location,
11249 OPT_Wvarargs,
11250 "undefined behaviour when second parameter of "
11251 "%<va_start%> is declared with %<register%> storage");
11254 /* We want to verify the second parameter just once before the tree
11255 optimizers are run and then avoid keeping it in the tree,
11256 as otherwise we could warn even for correct code like:
11257 void foo (int i, ...)
11258 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11259 if (va_start_p)
11260 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11261 else
11262 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11264 return false;
11268 /* Expand a call EXP to __builtin_object_size. */
11270 static rtx
11271 expand_builtin_object_size (tree exp)
11273 tree ost;
11274 int object_size_type;
11275 tree fndecl = get_callee_fndecl (exp);
11277 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11279 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11280 exp, fndecl);
11281 expand_builtin_trap ();
11282 return const0_rtx;
11285 ost = CALL_EXPR_ARG (exp, 1);
11286 STRIP_NOPS (ost);
11288 if (TREE_CODE (ost) != INTEGER_CST
11289 || tree_int_cst_sgn (ost) < 0
11290 || compare_tree_int (ost, 3) > 0)
11292 error ("%Klast argument of %D is not integer constant between 0 and 3",
11293 exp, fndecl);
11294 expand_builtin_trap ();
11295 return const0_rtx;
11298 object_size_type = tree_to_shwi (ost);
11300 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11303 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11304 FCODE is the BUILT_IN_* to use.
11305 Return NULL_RTX if we failed; the caller should emit a normal call,
11306 otherwise try to get the result in TARGET, if convenient (and in
11307 mode MODE if that's convenient). */
11309 static rtx
11310 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11311 enum built_in_function fcode)
11313 tree dest, src, len, size;
11315 if (!validate_arglist (exp,
11316 POINTER_TYPE,
11317 fcode == BUILT_IN_MEMSET_CHK
11318 ? INTEGER_TYPE : POINTER_TYPE,
11319 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11320 return NULL_RTX;
11322 dest = CALL_EXPR_ARG (exp, 0);
11323 src = CALL_EXPR_ARG (exp, 1);
11324 len = CALL_EXPR_ARG (exp, 2);
11325 size = CALL_EXPR_ARG (exp, 3);
11327 if (! tree_fits_uhwi_p (size))
11328 return NULL_RTX;
11330 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11332 tree fn;
11334 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11336 warning_at (tree_nonartificial_location (exp),
11337 0, "%Kcall to %D will always overflow destination buffer",
11338 exp, get_callee_fndecl (exp));
11339 return NULL_RTX;
11342 fn = NULL_TREE;
11343 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11344 mem{cpy,pcpy,move,set} is available. */
11345 switch (fcode)
11347 case BUILT_IN_MEMCPY_CHK:
11348 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11349 break;
11350 case BUILT_IN_MEMPCPY_CHK:
11351 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11352 break;
11353 case BUILT_IN_MEMMOVE_CHK:
11354 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11355 break;
11356 case BUILT_IN_MEMSET_CHK:
11357 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11358 break;
11359 default:
11360 break;
11363 if (! fn)
11364 return NULL_RTX;
11366 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11367 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11368 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11369 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11371 else if (fcode == BUILT_IN_MEMSET_CHK)
11372 return NULL_RTX;
11373 else
11375 unsigned int dest_align = get_pointer_alignment (dest);
11377 /* If DEST is not a pointer type, call the normal function. */
11378 if (dest_align == 0)
11379 return NULL_RTX;
11381 /* If SRC and DEST are the same (and not volatile), do nothing. */
11382 if (operand_equal_p (src, dest, 0))
11384 tree expr;
11386 if (fcode != BUILT_IN_MEMPCPY_CHK)
11388 /* Evaluate and ignore LEN in case it has side-effects. */
11389 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11390 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11393 expr = fold_build_pointer_plus (dest, len);
11394 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11397 /* __memmove_chk special case. */
11398 if (fcode == BUILT_IN_MEMMOVE_CHK)
11400 unsigned int src_align = get_pointer_alignment (src);
11402 if (src_align == 0)
11403 return NULL_RTX;
11405 /* If src is categorized for a readonly section we can use
11406 normal __memcpy_chk. */
11407 if (readonly_data_expr (src))
11409 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11410 if (!fn)
11411 return NULL_RTX;
11412 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11413 dest, src, len, size);
11414 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11415 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11416 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11419 return NULL_RTX;
11423 /* Emit warning if a buffer overflow is detected at compile time. */
11425 static void
11426 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11428 int is_strlen = 0;
11429 tree len, size;
11430 location_t loc = tree_nonartificial_location (exp);
11432 switch (fcode)
11434 case BUILT_IN_STRCPY_CHK:
11435 case BUILT_IN_STPCPY_CHK:
11436 /* For __strcat_chk the warning will be emitted only if overflowing
11437 by at least strlen (dest) + 1 bytes. */
11438 case BUILT_IN_STRCAT_CHK:
11439 len = CALL_EXPR_ARG (exp, 1);
11440 size = CALL_EXPR_ARG (exp, 2);
11441 is_strlen = 1;
11442 break;
11443 case BUILT_IN_STRNCAT_CHK:
11444 case BUILT_IN_STRNCPY_CHK:
11445 case BUILT_IN_STPNCPY_CHK:
11446 len = CALL_EXPR_ARG (exp, 2);
11447 size = CALL_EXPR_ARG (exp, 3);
11448 break;
11449 case BUILT_IN_SNPRINTF_CHK:
11450 case BUILT_IN_VSNPRINTF_CHK:
11451 len = CALL_EXPR_ARG (exp, 1);
11452 size = CALL_EXPR_ARG (exp, 3);
11453 break;
11454 default:
11455 gcc_unreachable ();
11458 if (!len || !size)
11459 return;
11461 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11462 return;
11464 if (is_strlen)
11466 len = c_strlen (len, 1);
11467 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11468 return;
11470 else if (fcode == BUILT_IN_STRNCAT_CHK)
11472 tree src = CALL_EXPR_ARG (exp, 1);
11473 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11474 return;
11475 src = c_strlen (src, 1);
11476 if (! src || ! tree_fits_uhwi_p (src))
11478 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
11479 exp, get_callee_fndecl (exp));
11480 return;
11482 else if (tree_int_cst_lt (src, size))
11483 return;
11485 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
11486 return;
11488 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
11489 exp, get_callee_fndecl (exp));
11492 /* Emit warning if a buffer overflow is detected at compile time
11493 in __sprintf_chk/__vsprintf_chk calls. */
11495 static void
11496 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11498 tree size, len, fmt;
11499 const char *fmt_str;
11500 int nargs = call_expr_nargs (exp);
11502 /* Verify the required arguments in the original call. */
11504 if (nargs < 4)
11505 return;
11506 size = CALL_EXPR_ARG (exp, 2);
11507 fmt = CALL_EXPR_ARG (exp, 3);
11509 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11510 return;
11512 /* Check whether the format is a literal string constant. */
11513 fmt_str = c_getstr (fmt);
11514 if (fmt_str == NULL)
11515 return;
11517 if (!init_target_chars ())
11518 return;
11520 /* If the format doesn't contain % args or %%, we know its size. */
11521 if (strchr (fmt_str, target_percent) == 0)
11522 len = build_int_cstu (size_type_node, strlen (fmt_str));
11523 /* If the format is "%s" and first ... argument is a string literal,
11524 we know it too. */
11525 else if (fcode == BUILT_IN_SPRINTF_CHK
11526 && strcmp (fmt_str, target_percent_s) == 0)
11528 tree arg;
11530 if (nargs < 5)
11531 return;
11532 arg = CALL_EXPR_ARG (exp, 4);
11533 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11534 return;
11536 len = c_strlen (arg, 1);
11537 if (!len || ! tree_fits_uhwi_p (len))
11538 return;
11540 else
11541 return;
11543 if (! tree_int_cst_lt (len, size))
11544 warning_at (tree_nonartificial_location (exp),
11545 0, "%Kcall to %D will always overflow destination buffer",
11546 exp, get_callee_fndecl (exp));
11549 /* Emit warning if a free is called with address of a variable. */
11551 static void
11552 maybe_emit_free_warning (tree exp)
11554 tree arg = CALL_EXPR_ARG (exp, 0);
11556 STRIP_NOPS (arg);
11557 if (TREE_CODE (arg) != ADDR_EXPR)
11558 return;
11560 arg = get_base_address (TREE_OPERAND (arg, 0));
11561 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
11562 return;
11564 if (SSA_VAR_P (arg))
11565 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11566 "%Kattempt to free a non-heap object %qD", exp, arg);
11567 else
11568 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11569 "%Kattempt to free a non-heap object", exp);
11572 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11573 if possible. */
11575 static tree
11576 fold_builtin_object_size (tree ptr, tree ost)
11578 unsigned HOST_WIDE_INT bytes;
11579 int object_size_type;
11581 if (!validate_arg (ptr, POINTER_TYPE)
11582 || !validate_arg (ost, INTEGER_TYPE))
11583 return NULL_TREE;
11585 STRIP_NOPS (ost);
11587 if (TREE_CODE (ost) != INTEGER_CST
11588 || tree_int_cst_sgn (ost) < 0
11589 || compare_tree_int (ost, 3) > 0)
11590 return NULL_TREE;
11592 object_size_type = tree_to_shwi (ost);
11594 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11595 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11596 and (size_t) 0 for types 2 and 3. */
11597 if (TREE_SIDE_EFFECTS (ptr))
11598 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11600 if (TREE_CODE (ptr) == ADDR_EXPR)
11602 bytes = compute_builtin_object_size (ptr, object_size_type);
11603 if (wi::fits_to_tree_p (bytes, size_type_node))
11604 return build_int_cstu (size_type_node, bytes);
11606 else if (TREE_CODE (ptr) == SSA_NAME)
11608 /* If object size is not known yet, delay folding until
11609 later. Maybe subsequent passes will help determining
11610 it. */
11611 bytes = compute_builtin_object_size (ptr, object_size_type);
11612 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
11613 && wi::fits_to_tree_p (bytes, size_type_node))
11614 return build_int_cstu (size_type_node, bytes);
11617 return NULL_TREE;
11620 /* Builtins with folding operations that operate on "..." arguments
11621 need special handling; we need to store the arguments in a convenient
11622 data structure before attempting any folding. Fortunately there are
11623 only a few builtins that fall into this category. FNDECL is the
11624 function, EXP is the CALL_EXPR for the call. */
11626 static tree
11627 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
11629 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11630 tree ret = NULL_TREE;
11632 switch (fcode)
11634 case BUILT_IN_FPCLASSIFY:
11635 ret = fold_builtin_fpclassify (loc, args, nargs);
11636 break;
11638 default:
11639 break;
11641 if (ret)
11643 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11644 SET_EXPR_LOCATION (ret, loc);
11645 TREE_NO_WARNING (ret) = 1;
11646 return ret;
11648 return NULL_TREE;
11651 /* Initialize format string characters in the target charset. */
11653 bool
11654 init_target_chars (void)
11656 static bool init;
11657 if (!init)
11659 target_newline = lang_hooks.to_target_charset ('\n');
11660 target_percent = lang_hooks.to_target_charset ('%');
11661 target_c = lang_hooks.to_target_charset ('c');
11662 target_s = lang_hooks.to_target_charset ('s');
11663 if (target_newline == 0 || target_percent == 0 || target_c == 0
11664 || target_s == 0)
11665 return false;
11667 target_percent_c[0] = target_percent;
11668 target_percent_c[1] = target_c;
11669 target_percent_c[2] = '\0';
11671 target_percent_s[0] = target_percent;
11672 target_percent_s[1] = target_s;
11673 target_percent_s[2] = '\0';
11675 target_percent_s_newline[0] = target_percent;
11676 target_percent_s_newline[1] = target_s;
11677 target_percent_s_newline[2] = target_newline;
11678 target_percent_s_newline[3] = '\0';
11680 init = true;
11682 return true;
11685 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11686 and no overflow/underflow occurred. INEXACT is true if M was not
11687 exactly calculated. TYPE is the tree type for the result. This
11688 function assumes that you cleared the MPFR flags and then
11689 calculated M to see if anything subsequently set a flag prior to
11690 entering this function. Return NULL_TREE if any checks fail. */
11692 static tree
11693 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
11695 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11696 overflow/underflow occurred. If -frounding-math, proceed iff the
11697 result of calling FUNC was exact. */
11698 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11699 && (!flag_rounding_math || !inexact))
11701 REAL_VALUE_TYPE rr;
11703 real_from_mpfr (&rr, m, type, GMP_RNDN);
11704 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11705 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11706 but the mpft_t is not, then we underflowed in the
11707 conversion. */
11708 if (real_isfinite (&rr)
11709 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11711 REAL_VALUE_TYPE rmode;
11713 real_convert (&rmode, TYPE_MODE (type), &rr);
11714 /* Proceed iff the specified mode can hold the value. */
11715 if (real_identical (&rmode, &rr))
11716 return build_real (type, rmode);
11719 return NULL_TREE;
11722 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
11723 number and no overflow/underflow occurred. INEXACT is true if M
11724 was not exactly calculated. TYPE is the tree type for the result.
11725 This function assumes that you cleared the MPFR flags and then
11726 calculated M to see if anything subsequently set a flag prior to
11727 entering this function. Return NULL_TREE if any checks fail, if
11728 FORCE_CONVERT is true, then bypass the checks. */
11730 static tree
11731 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
11733 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11734 overflow/underflow occurred. If -frounding-math, proceed iff the
11735 result of calling FUNC was exact. */
11736 if (force_convert
11737 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
11738 && !mpfr_overflow_p () && !mpfr_underflow_p ()
11739 && (!flag_rounding_math || !inexact)))
11741 REAL_VALUE_TYPE re, im;
11743 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
11744 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
11745 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11746 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11747 but the mpft_t is not, then we underflowed in the
11748 conversion. */
11749 if (force_convert
11750 || (real_isfinite (&re) && real_isfinite (&im)
11751 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
11752 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
11754 REAL_VALUE_TYPE re_mode, im_mode;
11756 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
11757 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
11758 /* Proceed iff the specified mode can hold the value. */
11759 if (force_convert
11760 || (real_identical (&re_mode, &re)
11761 && real_identical (&im_mode, &im)))
11762 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
11763 build_real (TREE_TYPE (type), im_mode));
11766 return NULL_TREE;
11769 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11770 FUNC on it and return the resulting value as a tree with type TYPE.
11771 If MIN and/or MAX are not NULL, then the supplied ARG must be
11772 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11773 acceptable values, otherwise they are not. The mpfr precision is
11774 set to the precision of TYPE. We assume that function FUNC returns
11775 zero if the result could be calculated exactly within the requested
11776 precision. */
11778 static tree
11779 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11780 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11781 bool inclusive)
11783 tree result = NULL_TREE;
11785 STRIP_NOPS (arg);
11787 /* To proceed, MPFR must exactly represent the target floating point
11788 format, which only happens when the target base equals two. */
11789 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11790 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
11792 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11794 if (real_isfinite (ra)
11795 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
11796 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
11798 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11799 const int prec = fmt->p;
11800 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11801 int inexact;
11802 mpfr_t m;
11804 mpfr_init2 (m, prec);
11805 mpfr_from_real (m, ra, GMP_RNDN);
11806 mpfr_clear_flags ();
11807 inexact = func (m, m, rnd);
11808 result = do_mpfr_ckconv (m, type, inexact);
11809 mpfr_clear (m);
11813 return result;
11816 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11817 FUNC on it and return the resulting value as a tree with type TYPE.
11818 The mpfr precision is set to the precision of TYPE. We assume that
11819 function FUNC returns zero if the result could be calculated
11820 exactly within the requested precision. */
11822 static tree
11823 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
11824 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11826 tree result = NULL_TREE;
11828 STRIP_NOPS (arg1);
11829 STRIP_NOPS (arg2);
11831 /* To proceed, MPFR must exactly represent the target floating point
11832 format, which only happens when the target base equals two. */
11833 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11834 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11835 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
11837 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11838 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11840 if (real_isfinite (ra1) && real_isfinite (ra2))
11842 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11843 const int prec = fmt->p;
11844 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11845 int inexact;
11846 mpfr_t m1, m2;
11848 mpfr_inits2 (prec, m1, m2, NULL);
11849 mpfr_from_real (m1, ra1, GMP_RNDN);
11850 mpfr_from_real (m2, ra2, GMP_RNDN);
11851 mpfr_clear_flags ();
11852 inexact = func (m1, m1, m2, rnd);
11853 result = do_mpfr_ckconv (m1, type, inexact);
11854 mpfr_clears (m1, m2, NULL);
11858 return result;
11861 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
11862 FUNC on it and return the resulting value as a tree with type TYPE.
11863 The mpfr precision is set to the precision of TYPE. We assume that
11864 function FUNC returns zero if the result could be calculated
11865 exactly within the requested precision. */
11867 static tree
11868 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
11869 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11871 tree result = NULL_TREE;
11873 STRIP_NOPS (arg1);
11874 STRIP_NOPS (arg2);
11875 STRIP_NOPS (arg3);
11877 /* To proceed, MPFR must exactly represent the target floating point
11878 format, which only happens when the target base equals two. */
11879 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11880 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11881 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
11882 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
11884 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11885 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11886 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
11888 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
11890 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11891 const int prec = fmt->p;
11892 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11893 int inexact;
11894 mpfr_t m1, m2, m3;
11896 mpfr_inits2 (prec, m1, m2, m3, NULL);
11897 mpfr_from_real (m1, ra1, GMP_RNDN);
11898 mpfr_from_real (m2, ra2, GMP_RNDN);
11899 mpfr_from_real (m3, ra3, GMP_RNDN);
11900 mpfr_clear_flags ();
11901 inexact = func (m1, m1, m2, m3, rnd);
11902 result = do_mpfr_ckconv (m1, type, inexact);
11903 mpfr_clears (m1, m2, m3, NULL);
11907 return result;
11910 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11911 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11912 If ARG_SINP and ARG_COSP are NULL then the result is returned
11913 as a complex value.
11914 The type is taken from the type of ARG and is used for setting the
11915 precision of the calculation and results. */
11917 static tree
11918 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
11920 tree const type = TREE_TYPE (arg);
11921 tree result = NULL_TREE;
11923 STRIP_NOPS (arg);
11925 /* To proceed, MPFR must exactly represent the target floating point
11926 format, which only happens when the target base equals two. */
11927 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11928 && TREE_CODE (arg) == REAL_CST
11929 && !TREE_OVERFLOW (arg))
11931 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11933 if (real_isfinite (ra))
11935 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11936 const int prec = fmt->p;
11937 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11938 tree result_s, result_c;
11939 int inexact;
11940 mpfr_t m, ms, mc;
11942 mpfr_inits2 (prec, m, ms, mc, NULL);
11943 mpfr_from_real (m, ra, GMP_RNDN);
11944 mpfr_clear_flags ();
11945 inexact = mpfr_sin_cos (ms, mc, m, rnd);
11946 result_s = do_mpfr_ckconv (ms, type, inexact);
11947 result_c = do_mpfr_ckconv (mc, type, inexact);
11948 mpfr_clears (m, ms, mc, NULL);
11949 if (result_s && result_c)
11951 /* If we are to return in a complex value do so. */
11952 if (!arg_sinp && !arg_cosp)
11953 return build_complex (build_complex_type (type),
11954 result_c, result_s);
11956 /* Dereference the sin/cos pointer arguments. */
11957 arg_sinp = build_fold_indirect_ref (arg_sinp);
11958 arg_cosp = build_fold_indirect_ref (arg_cosp);
11959 /* Proceed if valid pointer type were passed in. */
11960 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
11961 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
11963 /* Set the values. */
11964 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
11965 result_s);
11966 TREE_SIDE_EFFECTS (result_s) = 1;
11967 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
11968 result_c);
11969 TREE_SIDE_EFFECTS (result_c) = 1;
11970 /* Combine the assignments into a compound expr. */
11971 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11972 result_s, result_c));
11977 return result;
11980 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
11981 two-argument mpfr order N Bessel function FUNC on them and return
11982 the resulting value as a tree with type TYPE. The mpfr precision
11983 is set to the precision of TYPE. We assume that function FUNC
11984 returns zero if the result could be calculated exactly within the
11985 requested precision. */
11986 static tree
11987 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
11988 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
11989 const REAL_VALUE_TYPE *min, bool inclusive)
11991 tree result = NULL_TREE;
11993 STRIP_NOPS (arg1);
11994 STRIP_NOPS (arg2);
11996 /* To proceed, MPFR must exactly represent the target floating point
11997 format, which only happens when the target base equals two. */
11998 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11999 && tree_fits_shwi_p (arg1)
12000 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12002 const HOST_WIDE_INT n = tree_to_shwi (arg1);
12003 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
12005 if (n == (long)n
12006 && real_isfinite (ra)
12007 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
12009 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12010 const int prec = fmt->p;
12011 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12012 int inexact;
12013 mpfr_t m;
12015 mpfr_init2 (m, prec);
12016 mpfr_from_real (m, ra, GMP_RNDN);
12017 mpfr_clear_flags ();
12018 inexact = func (m, n, m, rnd);
12019 result = do_mpfr_ckconv (m, type, inexact);
12020 mpfr_clear (m);
12024 return result;
12027 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12028 the pointer *(ARG_QUO) and return the result. The type is taken
12029 from the type of ARG0 and is used for setting the precision of the
12030 calculation and results. */
12032 static tree
12033 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
12035 tree const type = TREE_TYPE (arg0);
12036 tree result = NULL_TREE;
12038 STRIP_NOPS (arg0);
12039 STRIP_NOPS (arg1);
12041 /* To proceed, MPFR must exactly represent the target floating point
12042 format, which only happens when the target base equals two. */
12043 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12044 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12045 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12047 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12048 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12050 if (real_isfinite (ra0) && real_isfinite (ra1))
12052 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12053 const int prec = fmt->p;
12054 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12055 tree result_rem;
12056 long integer_quo;
12057 mpfr_t m0, m1;
12059 mpfr_inits2 (prec, m0, m1, NULL);
12060 mpfr_from_real (m0, ra0, GMP_RNDN);
12061 mpfr_from_real (m1, ra1, GMP_RNDN);
12062 mpfr_clear_flags ();
12063 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12064 /* Remquo is independent of the rounding mode, so pass
12065 inexact=0 to do_mpfr_ckconv(). */
12066 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12067 mpfr_clears (m0, m1, NULL);
12068 if (result_rem)
12070 /* MPFR calculates quo in the host's long so it may
12071 return more bits in quo than the target int can hold
12072 if sizeof(host long) > sizeof(target int). This can
12073 happen even for native compilers in LP64 mode. In
12074 these cases, modulo the quo value with the largest
12075 number that the target int can hold while leaving one
12076 bit for the sign. */
12077 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12078 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12080 /* Dereference the quo pointer argument. */
12081 arg_quo = build_fold_indirect_ref (arg_quo);
12082 /* Proceed iff a valid pointer type was passed in. */
12083 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12085 /* Set the value. */
12086 tree result_quo
12087 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12088 build_int_cst (TREE_TYPE (arg_quo),
12089 integer_quo));
12090 TREE_SIDE_EFFECTS (result_quo) = 1;
12091 /* Combine the quo assignment with the rem. */
12092 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12093 result_quo, result_rem));
12098 return result;
12101 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12102 resulting value as a tree with type TYPE. The mpfr precision is
12103 set to the precision of TYPE. We assume that this mpfr function
12104 returns zero if the result could be calculated exactly within the
12105 requested precision. In addition, the integer pointer represented
12106 by ARG_SG will be dereferenced and set to the appropriate signgam
12107 (-1,1) value. */
12109 static tree
12110 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12112 tree result = NULL_TREE;
12114 STRIP_NOPS (arg);
12116 /* To proceed, MPFR must exactly represent the target floating point
12117 format, which only happens when the target base equals two. Also
12118 verify ARG is a constant and that ARG_SG is an int pointer. */
12119 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12120 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12121 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12122 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12124 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12126 /* In addition to NaN and Inf, the argument cannot be zero or a
12127 negative integer. */
12128 if (real_isfinite (ra)
12129 && ra->cl != rvc_zero
12130 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12132 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12133 const int prec = fmt->p;
12134 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12135 int inexact, sg;
12136 mpfr_t m;
12137 tree result_lg;
12139 mpfr_init2 (m, prec);
12140 mpfr_from_real (m, ra, GMP_RNDN);
12141 mpfr_clear_flags ();
12142 inexact = mpfr_lgamma (m, &sg, m, rnd);
12143 result_lg = do_mpfr_ckconv (m, type, inexact);
12144 mpfr_clear (m);
12145 if (result_lg)
12147 tree result_sg;
12149 /* Dereference the arg_sg pointer argument. */
12150 arg_sg = build_fold_indirect_ref (arg_sg);
12151 /* Assign the signgam value into *arg_sg. */
12152 result_sg = fold_build2 (MODIFY_EXPR,
12153 TREE_TYPE (arg_sg), arg_sg,
12154 build_int_cst (TREE_TYPE (arg_sg), sg));
12155 TREE_SIDE_EFFECTS (result_sg) = 1;
12156 /* Combine the signgam assignment with the lgamma result. */
12157 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12158 result_sg, result_lg));
12163 return result;
12166 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
12167 function FUNC on it and return the resulting value as a tree with
12168 type TYPE. The mpfr precision is set to the precision of TYPE. We
12169 assume that function FUNC returns zero if the result could be
12170 calculated exactly within the requested precision. */
12172 static tree
12173 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
12175 tree result = NULL_TREE;
12177 STRIP_NOPS (arg);
12179 /* To proceed, MPFR must exactly represent the target floating point
12180 format, which only happens when the target base equals two. */
12181 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
12182 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
12183 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
12185 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
12186 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
12188 if (real_isfinite (re) && real_isfinite (im))
12190 const struct real_format *const fmt =
12191 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12192 const int prec = fmt->p;
12193 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12194 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12195 int inexact;
12196 mpc_t m;
12198 mpc_init2 (m, prec);
12199 mpfr_from_real (mpc_realref (m), re, rnd);
12200 mpfr_from_real (mpc_imagref (m), im, rnd);
12201 mpfr_clear_flags ();
12202 inexact = func (m, m, crnd);
12203 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
12204 mpc_clear (m);
12208 return result;
12211 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12212 mpc function FUNC on it and return the resulting value as a tree
12213 with type TYPE. The mpfr precision is set to the precision of
12214 TYPE. We assume that function FUNC returns zero if the result
12215 could be calculated exactly within the requested precision. If
12216 DO_NONFINITE is true, then fold expressions containing Inf or NaN
12217 in the arguments and/or results. */
12219 tree
12220 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12221 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12223 tree result = NULL_TREE;
12225 STRIP_NOPS (arg0);
12226 STRIP_NOPS (arg1);
12228 /* To proceed, MPFR must exactly represent the target floating point
12229 format, which only happens when the target base equals two. */
12230 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12231 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
12232 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12233 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
12234 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12236 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12237 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12238 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12239 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12241 if (do_nonfinite
12242 || (real_isfinite (re0) && real_isfinite (im0)
12243 && real_isfinite (re1) && real_isfinite (im1)))
12245 const struct real_format *const fmt =
12246 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12247 const int prec = fmt->p;
12248 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12249 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12250 int inexact;
12251 mpc_t m0, m1;
12253 mpc_init2 (m0, prec);
12254 mpc_init2 (m1, prec);
12255 mpfr_from_real (mpc_realref (m0), re0, rnd);
12256 mpfr_from_real (mpc_imagref (m0), im0, rnd);
12257 mpfr_from_real (mpc_realref (m1), re1, rnd);
12258 mpfr_from_real (mpc_imagref (m1), im1, rnd);
12259 mpfr_clear_flags ();
12260 inexact = func (m0, m0, m1, crnd);
12261 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12262 mpc_clear (m0);
12263 mpc_clear (m1);
12267 return result;
12270 /* A wrapper function for builtin folding that prevents warnings for
12271 "statement without effect" and the like, caused by removing the
12272 call node earlier than the warning is generated. */
12274 tree
12275 fold_call_stmt (gcall *stmt, bool ignore)
12277 tree ret = NULL_TREE;
12278 tree fndecl = gimple_call_fndecl (stmt);
12279 location_t loc = gimple_location (stmt);
12280 if (fndecl
12281 && TREE_CODE (fndecl) == FUNCTION_DECL
12282 && DECL_BUILT_IN (fndecl)
12283 && !gimple_call_va_arg_pack_p (stmt))
12285 int nargs = gimple_call_num_args (stmt);
12286 tree *args = (nargs > 0
12287 ? gimple_call_arg_ptr (stmt, 0)
12288 : &error_mark_node);
12290 if (avoid_folding_inline_builtin (fndecl))
12291 return NULL_TREE;
12292 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12294 return targetm.fold_builtin (fndecl, nargs, args, ignore);
12296 else
12298 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
12299 if (ret)
12301 /* Propagate location information from original call to
12302 expansion of builtin. Otherwise things like
12303 maybe_emit_chk_warning, that operate on the expansion
12304 of a builtin, will use the wrong location information. */
12305 if (gimple_has_location (stmt))
12307 tree realret = ret;
12308 if (TREE_CODE (ret) == NOP_EXPR)
12309 realret = TREE_OPERAND (ret, 0);
12310 if (CAN_HAVE_LOCATION_P (realret)
12311 && !EXPR_HAS_LOCATION (realret))
12312 SET_EXPR_LOCATION (realret, loc);
12313 return realret;
12315 return ret;
12319 return NULL_TREE;
12322 /* Look up the function in builtin_decl that corresponds to DECL
12323 and set ASMSPEC as its user assembler name. DECL must be a
12324 function decl that declares a builtin. */
12326 void
12327 set_builtin_user_assembler_name (tree decl, const char *asmspec)
12329 tree builtin;
12330 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
12331 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
12332 && asmspec != 0);
12334 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12335 set_user_assembler_name (builtin, asmspec);
12336 switch (DECL_FUNCTION_CODE (decl))
12338 case BUILT_IN_MEMCPY:
12339 init_block_move_fn (asmspec);
12340 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
12341 break;
12342 case BUILT_IN_MEMSET:
12343 init_block_clear_fn (asmspec);
12344 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
12345 break;
12346 case BUILT_IN_MEMMOVE:
12347 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
12348 break;
12349 case BUILT_IN_MEMCMP:
12350 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
12351 break;
12352 case BUILT_IN_ABORT:
12353 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
12354 break;
12355 case BUILT_IN_FFS:
12356 if (INT_TYPE_SIZE < BITS_PER_WORD)
12358 set_user_assembler_libfunc ("ffs", asmspec);
12359 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
12360 MODE_INT, 0), "ffs");
12362 break;
12363 default:
12364 break;
12368 /* Return true if DECL is a builtin that expands to a constant or similarly
12369 simple code. */
12370 bool
12371 is_simple_builtin (tree decl)
12373 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12374 switch (DECL_FUNCTION_CODE (decl))
12376 /* Builtins that expand to constants. */
12377 case BUILT_IN_CONSTANT_P:
12378 case BUILT_IN_EXPECT:
12379 case BUILT_IN_OBJECT_SIZE:
12380 case BUILT_IN_UNREACHABLE:
12381 /* Simple register moves or loads from stack. */
12382 case BUILT_IN_ASSUME_ALIGNED:
12383 case BUILT_IN_RETURN_ADDRESS:
12384 case BUILT_IN_EXTRACT_RETURN_ADDR:
12385 case BUILT_IN_FROB_RETURN_ADDR:
12386 case BUILT_IN_RETURN:
12387 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12388 case BUILT_IN_FRAME_ADDRESS:
12389 case BUILT_IN_VA_END:
12390 case BUILT_IN_STACK_SAVE:
12391 case BUILT_IN_STACK_RESTORE:
12392 /* Exception state returns or moves registers around. */
12393 case BUILT_IN_EH_FILTER:
12394 case BUILT_IN_EH_POINTER:
12395 case BUILT_IN_EH_COPY_VALUES:
12396 return true;
12398 default:
12399 return false;
12402 return false;
12405 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12406 most probably expanded inline into reasonably simple code. This is a
12407 superset of is_simple_builtin. */
12408 bool
12409 is_inexpensive_builtin (tree decl)
12411 if (!decl)
12412 return false;
12413 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12414 return true;
12415 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12416 switch (DECL_FUNCTION_CODE (decl))
12418 case BUILT_IN_ABS:
12419 case BUILT_IN_ALLOCA:
12420 case BUILT_IN_ALLOCA_WITH_ALIGN:
12421 case BUILT_IN_BSWAP16:
12422 case BUILT_IN_BSWAP32:
12423 case BUILT_IN_BSWAP64:
12424 case BUILT_IN_CLZ:
12425 case BUILT_IN_CLZIMAX:
12426 case BUILT_IN_CLZL:
12427 case BUILT_IN_CLZLL:
12428 case BUILT_IN_CTZ:
12429 case BUILT_IN_CTZIMAX:
12430 case BUILT_IN_CTZL:
12431 case BUILT_IN_CTZLL:
12432 case BUILT_IN_FFS:
12433 case BUILT_IN_FFSIMAX:
12434 case BUILT_IN_FFSL:
12435 case BUILT_IN_FFSLL:
12436 case BUILT_IN_IMAXABS:
12437 case BUILT_IN_FINITE:
12438 case BUILT_IN_FINITEF:
12439 case BUILT_IN_FINITEL:
12440 case BUILT_IN_FINITED32:
12441 case BUILT_IN_FINITED64:
12442 case BUILT_IN_FINITED128:
12443 case BUILT_IN_FPCLASSIFY:
12444 case BUILT_IN_ISFINITE:
12445 case BUILT_IN_ISINF_SIGN:
12446 case BUILT_IN_ISINF:
12447 case BUILT_IN_ISINFF:
12448 case BUILT_IN_ISINFL:
12449 case BUILT_IN_ISINFD32:
12450 case BUILT_IN_ISINFD64:
12451 case BUILT_IN_ISINFD128:
12452 case BUILT_IN_ISNAN:
12453 case BUILT_IN_ISNANF:
12454 case BUILT_IN_ISNANL:
12455 case BUILT_IN_ISNAND32:
12456 case BUILT_IN_ISNAND64:
12457 case BUILT_IN_ISNAND128:
12458 case BUILT_IN_ISNORMAL:
12459 case BUILT_IN_ISGREATER:
12460 case BUILT_IN_ISGREATEREQUAL:
12461 case BUILT_IN_ISLESS:
12462 case BUILT_IN_ISLESSEQUAL:
12463 case BUILT_IN_ISLESSGREATER:
12464 case BUILT_IN_ISUNORDERED:
12465 case BUILT_IN_VA_ARG_PACK:
12466 case BUILT_IN_VA_ARG_PACK_LEN:
12467 case BUILT_IN_VA_COPY:
12468 case BUILT_IN_TRAP:
12469 case BUILT_IN_SAVEREGS:
12470 case BUILT_IN_POPCOUNTL:
12471 case BUILT_IN_POPCOUNTLL:
12472 case BUILT_IN_POPCOUNTIMAX:
12473 case BUILT_IN_POPCOUNT:
12474 case BUILT_IN_PARITYL:
12475 case BUILT_IN_PARITYLL:
12476 case BUILT_IN_PARITYIMAX:
12477 case BUILT_IN_PARITY:
12478 case BUILT_IN_LABS:
12479 case BUILT_IN_LLABS:
12480 case BUILT_IN_PREFETCH:
12481 case BUILT_IN_ACC_ON_DEVICE:
12482 return true;
12484 default:
12485 return is_simple_builtin (decl);
12488 return false;