2014-06-28 Andrew Sutton <andrew.n.sutton@gmail.com>
[official-gcc.git] / gcc / builtins.c
blob2d3d867b0b2d138a8fe9d4f2898746f7507e12aa
1 /* Expand builtin functions.
2 Copyright (C) 1988-2014 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 "tm.h"
24 #include "machmode.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "stor-layout.h"
29 #include "calls.h"
30 #include "varasm.h"
31 #include "tree-object-size.h"
32 #include "realmpfr.h"
33 #include "basic-block.h"
34 #include "tree-ssa-alias.h"
35 #include "internal-fn.h"
36 #include "gimple-expr.h"
37 #include "is-a.h"
38 #include "gimple.h"
39 #include "flags.h"
40 #include "regs.h"
41 #include "hard-reg-set.h"
42 #include "except.h"
43 #include "function.h"
44 #include "insn-config.h"
45 #include "expr.h"
46 #include "optabs.h"
47 #include "libfuncs.h"
48 #include "recog.h"
49 #include "output.h"
50 #include "typeclass.h"
51 #include "predict.h"
52 #include "tm_p.h"
53 #include "target.h"
54 #include "langhooks.h"
55 #include "tree-ssanames.h"
56 #include "tree-dfa.h"
57 #include "value-prof.h"
58 #include "diagnostic-core.h"
59 #include "builtins.h"
60 #include "ubsan.h"
61 #include "cilk.h"
64 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
66 struct target_builtins default_target_builtins;
67 #if SWITCHABLE_TARGET
68 struct target_builtins *this_target_builtins = &default_target_builtins;
69 #endif
71 /* Define the names of the builtin function types and codes. */
72 const char *const built_in_class_names[BUILT_IN_LAST]
73 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
75 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
76 const char * built_in_names[(int) END_BUILTINS] =
78 #include "builtins.def"
80 #undef DEF_BUILTIN
82 /* Setup an array of _DECL trees, make sure each element is
83 initialized to NULL_TREE. */
84 builtin_info_type builtin_info;
86 /* Non-zero if __builtin_constant_p should be folded right away. */
87 bool force_folding_builtin_constant_p;
89 static const char *c_getstr (tree);
90 static rtx c_readstr (const char *, enum machine_mode);
91 static int target_char_cast (tree, char *);
92 static rtx get_memory_rtx (tree, tree);
93 static int apply_args_size (void);
94 static int apply_result_size (void);
95 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
96 static rtx result_vector (int, rtx);
97 #endif
98 static void expand_builtin_update_setjmp_buf (rtx);
99 static void expand_builtin_prefetch (tree);
100 static rtx expand_builtin_apply_args (void);
101 static rtx expand_builtin_apply_args_1 (void);
102 static rtx expand_builtin_apply (rtx, rtx, rtx);
103 static void expand_builtin_return (rtx);
104 static enum type_class type_to_class (tree);
105 static rtx expand_builtin_classify_type (tree);
106 static void expand_errno_check (tree, rtx);
107 static rtx expand_builtin_mathfn (tree, rtx, rtx);
108 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
109 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
110 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
111 static rtx expand_builtin_interclass_mathfn (tree, rtx);
112 static rtx expand_builtin_sincos (tree);
113 static rtx expand_builtin_cexpi (tree, rtx);
114 static rtx expand_builtin_int_roundingfn (tree, rtx);
115 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
116 static rtx expand_builtin_next_arg (void);
117 static rtx expand_builtin_va_start (tree);
118 static rtx expand_builtin_va_end (tree);
119 static rtx expand_builtin_va_copy (tree);
120 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
121 static rtx expand_builtin_strcmp (tree, rtx);
122 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
123 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memcpy (tree, rtx);
125 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
127 enum machine_mode, int);
128 static rtx expand_builtin_strcpy (tree, rtx);
129 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
130 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
131 static rtx expand_builtin_strncpy (tree, rtx);
132 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
133 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
134 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
135 static rtx expand_builtin_bzero (tree);
136 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
137 static rtx expand_builtin_alloca (tree, bool);
138 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
139 static rtx expand_builtin_frame_address (tree, tree);
140 static tree stabilize_va_list_loc (location_t, tree, int);
141 static rtx expand_builtin_expect (tree, rtx);
142 static tree fold_builtin_constant_p (tree);
143 static tree fold_builtin_classify_type (tree);
144 static tree fold_builtin_strlen (location_t, tree, tree);
145 static tree fold_builtin_inf (location_t, tree, int);
146 static tree fold_builtin_nan (tree, tree, int);
147 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
148 static bool validate_arg (const_tree, enum tree_code code);
149 static bool integer_valued_real_p (tree);
150 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
151 static bool readonly_data_expr (tree);
152 static rtx expand_builtin_fabs (tree, rtx, rtx);
153 static rtx expand_builtin_signbit (tree, rtx);
154 static tree fold_builtin_sqrt (location_t, tree, tree);
155 static tree fold_builtin_cbrt (location_t, tree, tree);
156 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
157 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
158 static tree fold_builtin_cos (location_t, tree, tree, tree);
159 static tree fold_builtin_cosh (location_t, tree, tree, tree);
160 static tree fold_builtin_tan (tree, tree);
161 static tree fold_builtin_trunc (location_t, tree, tree);
162 static tree fold_builtin_floor (location_t, tree, tree);
163 static tree fold_builtin_ceil (location_t, tree, tree);
164 static tree fold_builtin_round (location_t, tree, tree);
165 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
166 static tree fold_builtin_bitop (tree, tree);
167 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
168 static tree fold_builtin_strchr (location_t, tree, tree, tree);
169 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
170 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
171 static tree fold_builtin_strcmp (location_t, tree, tree);
172 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
173 static tree fold_builtin_signbit (location_t, tree, tree);
174 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
175 static tree fold_builtin_isascii (location_t, tree);
176 static tree fold_builtin_toascii (location_t, tree);
177 static tree fold_builtin_isdigit (location_t, tree);
178 static tree fold_builtin_fabs (location_t, tree, tree);
179 static tree fold_builtin_abs (location_t, tree, tree);
180 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
181 enum tree_code);
182 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
183 static tree fold_builtin_0 (location_t, tree, bool);
184 static tree fold_builtin_1 (location_t, tree, tree, bool);
185 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
186 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
187 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
188 static tree fold_builtin_varargs (location_t, tree, tree, bool);
190 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
191 static tree fold_builtin_strstr (location_t, tree, tree, tree);
192 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
193 static tree fold_builtin_strncat (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);
196 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
197 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
199 static rtx expand_builtin_object_size (tree);
200 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
201 enum built_in_function);
202 static void maybe_emit_chk_warning (tree, enum built_in_function);
203 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
204 static void maybe_emit_free_warning (tree);
205 static tree fold_builtin_object_size (tree, tree);
206 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
207 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
208 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
209 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
210 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
211 enum built_in_function);
212 static bool init_target_chars (void);
214 static unsigned HOST_WIDE_INT target_newline;
215 static unsigned HOST_WIDE_INT target_percent;
216 static unsigned HOST_WIDE_INT target_c;
217 static unsigned HOST_WIDE_INT target_s;
218 static char target_percent_c[3];
219 static char target_percent_s[3];
220 static char target_percent_s_newline[4];
221 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
222 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
223 static tree do_mpfr_arg2 (tree, tree, tree,
224 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
225 static tree do_mpfr_arg3 (tree, tree, tree, tree,
226 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
227 static tree do_mpfr_sincos (tree, tree, tree);
228 static tree do_mpfr_bessel_n (tree, tree, tree,
229 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
230 const REAL_VALUE_TYPE *, bool);
231 static tree do_mpfr_remquo (tree, tree, tree);
232 static tree do_mpfr_lgamma_r (tree, tree, tree);
233 static void expand_builtin_sync_synchronize (void);
235 /* Return true if NAME starts with __builtin_ or __sync_. */
237 static bool
238 is_builtin_name (const char *name)
240 if (strncmp (name, "__builtin_", 10) == 0)
241 return true;
242 if (strncmp (name, "__sync_", 7) == 0)
243 return true;
244 if (strncmp (name, "__atomic_", 9) == 0)
245 return true;
246 if (flag_cilkplus
247 && (!strcmp (name, "__cilkrts_detach")
248 || !strcmp (name, "__cilkrts_pop_frame")))
249 return true;
250 return false;
254 /* Return true if DECL is a function symbol representing a built-in. */
256 bool
257 is_builtin_fn (tree decl)
259 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
262 /* Return true if NODE should be considered for inline expansion regardless
263 of the optimization level. This means whenever a function is invoked with
264 its "internal" name, which normally contains the prefix "__builtin". */
266 static bool
267 called_as_built_in (tree node)
269 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
270 we want the name used to call the function, not the name it
271 will have. */
272 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
273 return is_builtin_name (name);
276 /* Compute values M and N such that M divides (address of EXP - N) and such
277 that N < M. If these numbers can be determined, store M in alignp and N in
278 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
279 *alignp and any bit-offset to *bitposp.
281 Note that the address (and thus the alignment) computed here is based
282 on the address to which a symbol resolves, whereas DECL_ALIGN is based
283 on the address at which an object is actually located. These two
284 addresses are not always the same. For example, on ARM targets,
285 the address &foo of a Thumb function foo() has the lowest bit set,
286 whereas foo() itself starts on an even address.
288 If ADDR_P is true we are taking the address of the memory reference EXP
289 and thus cannot rely on the access taking place. */
291 static bool
292 get_object_alignment_2 (tree exp, unsigned int *alignp,
293 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
295 HOST_WIDE_INT bitsize, bitpos;
296 tree offset;
297 enum machine_mode mode;
298 int unsignedp, volatilep;
299 unsigned int align = BITS_PER_UNIT;
300 bool known_alignment = false;
302 /* Get the innermost object and the constant (bitpos) and possibly
303 variable (offset) offset of the access. */
304 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
305 &mode, &unsignedp, &volatilep, true);
307 /* Extract alignment information from the innermost object and
308 possibly adjust bitpos and offset. */
309 if (TREE_CODE (exp) == FUNCTION_DECL)
311 /* Function addresses can encode extra information besides their
312 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
313 allows the low bit to be used as a virtual bit, we know
314 that the address itself must be at least 2-byte aligned. */
315 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
316 align = 2 * BITS_PER_UNIT;
318 else if (TREE_CODE (exp) == LABEL_DECL)
320 else if (TREE_CODE (exp) == CONST_DECL)
322 /* The alignment of a CONST_DECL is determined by its initializer. */
323 exp = DECL_INITIAL (exp);
324 align = TYPE_ALIGN (TREE_TYPE (exp));
325 #ifdef CONSTANT_ALIGNMENT
326 if (CONSTANT_CLASS_P (exp))
327 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
328 #endif
329 known_alignment = true;
331 else if (DECL_P (exp))
333 align = DECL_ALIGN (exp);
334 known_alignment = true;
336 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
338 align = TYPE_ALIGN (TREE_TYPE (exp));
340 else if (TREE_CODE (exp) == INDIRECT_REF
341 || TREE_CODE (exp) == MEM_REF
342 || TREE_CODE (exp) == TARGET_MEM_REF)
344 tree addr = TREE_OPERAND (exp, 0);
345 unsigned ptr_align;
346 unsigned HOST_WIDE_INT ptr_bitpos;
348 if (TREE_CODE (addr) == BIT_AND_EXPR
349 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
351 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
352 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
353 align *= BITS_PER_UNIT;
354 addr = TREE_OPERAND (addr, 0);
357 known_alignment
358 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
359 align = MAX (ptr_align, align);
361 /* The alignment of the pointer operand in a TARGET_MEM_REF
362 has to take the variable offset parts into account. */
363 if (TREE_CODE (exp) == TARGET_MEM_REF)
365 if (TMR_INDEX (exp))
367 unsigned HOST_WIDE_INT step = 1;
368 if (TMR_STEP (exp))
369 step = TREE_INT_CST_LOW (TMR_STEP (exp));
370 align = MIN (align, (step & -step) * BITS_PER_UNIT);
372 if (TMR_INDEX2 (exp))
373 align = BITS_PER_UNIT;
374 known_alignment = false;
377 /* When EXP is an actual memory reference then we can use
378 TYPE_ALIGN of a pointer indirection to derive alignment.
379 Do so only if get_pointer_alignment_1 did not reveal absolute
380 alignment knowledge and if using that alignment would
381 improve the situation. */
382 if (!addr_p && !known_alignment
383 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
384 align = TYPE_ALIGN (TREE_TYPE (exp));
385 else
387 /* Else adjust bitpos accordingly. */
388 bitpos += ptr_bitpos;
389 if (TREE_CODE (exp) == MEM_REF
390 || TREE_CODE (exp) == TARGET_MEM_REF)
391 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
394 else if (TREE_CODE (exp) == STRING_CST)
396 /* STRING_CST are the only constant objects we allow to be not
397 wrapped inside a CONST_DECL. */
398 align = TYPE_ALIGN (TREE_TYPE (exp));
399 #ifdef CONSTANT_ALIGNMENT
400 if (CONSTANT_CLASS_P (exp))
401 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
402 #endif
403 known_alignment = true;
406 /* If there is a non-constant offset part extract the maximum
407 alignment that can prevail. */
408 if (offset)
410 unsigned int trailing_zeros = tree_ctz (offset);
411 if (trailing_zeros < HOST_BITS_PER_INT)
413 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
414 if (inner)
415 align = MIN (align, inner);
419 *alignp = align;
420 *bitposp = bitpos & (*alignp - 1);
421 return known_alignment;
424 /* For a memory reference expression EXP compute values M and N such that M
425 divides (&EXP - N) and such that N < M. If these numbers can be determined,
426 store M in alignp and N in *BITPOSP and return true. Otherwise return false
427 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
429 bool
430 get_object_alignment_1 (tree exp, unsigned int *alignp,
431 unsigned HOST_WIDE_INT *bitposp)
433 return get_object_alignment_2 (exp, alignp, bitposp, false);
436 /* Return the alignment in bits of EXP, an object. */
438 unsigned int
439 get_object_alignment (tree exp)
441 unsigned HOST_WIDE_INT bitpos = 0;
442 unsigned int align;
444 get_object_alignment_1 (exp, &align, &bitpos);
446 /* align and bitpos now specify known low bits of the pointer.
447 ptr & (align - 1) == bitpos. */
449 if (bitpos != 0)
450 align = (bitpos & -bitpos);
451 return align;
454 /* For a pointer valued expression EXP compute values M and N such that M
455 divides (EXP - N) and such that N < M. If these numbers can be determined,
456 store M in alignp and N in *BITPOSP and return true. Return false if
457 the results are just a conservative approximation.
459 If EXP is not a pointer, false is returned too. */
461 bool
462 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
463 unsigned HOST_WIDE_INT *bitposp)
465 STRIP_NOPS (exp);
467 if (TREE_CODE (exp) == ADDR_EXPR)
468 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
469 alignp, bitposp, true);
470 else if (TREE_CODE (exp) == SSA_NAME
471 && POINTER_TYPE_P (TREE_TYPE (exp)))
473 unsigned int ptr_align, ptr_misalign;
474 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
476 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
478 *bitposp = ptr_misalign * BITS_PER_UNIT;
479 *alignp = ptr_align * BITS_PER_UNIT;
480 /* We cannot really tell whether this result is an approximation. */
481 return true;
483 else
485 *bitposp = 0;
486 *alignp = BITS_PER_UNIT;
487 return false;
490 else if (TREE_CODE (exp) == INTEGER_CST)
492 *alignp = BIGGEST_ALIGNMENT;
493 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
494 & (BIGGEST_ALIGNMENT - 1));
495 return true;
498 *bitposp = 0;
499 *alignp = BITS_PER_UNIT;
500 return false;
503 /* Return the alignment in bits of EXP, a pointer valued expression.
504 The alignment returned is, by default, the alignment of the thing that
505 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
507 Otherwise, look at the expression to see if we can do better, i.e., if the
508 expression is actually pointing at an object whose alignment is tighter. */
510 unsigned int
511 get_pointer_alignment (tree exp)
513 unsigned HOST_WIDE_INT bitpos = 0;
514 unsigned int align;
516 get_pointer_alignment_1 (exp, &align, &bitpos);
518 /* align and bitpos now specify known low bits of the pointer.
519 ptr & (align - 1) == bitpos. */
521 if (bitpos != 0)
522 align = (bitpos & -bitpos);
524 return align;
527 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
528 way, because it could contain a zero byte in the middle.
529 TREE_STRING_LENGTH is the size of the character array, not the string.
531 ONLY_VALUE should be nonzero if the result is not going to be emitted
532 into the instruction stream and zero if it is going to be expanded.
533 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
534 is returned, otherwise NULL, since
535 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
536 evaluate the side-effects.
538 The value returned is of type `ssizetype'.
540 Unfortunately, string_constant can't access the values of const char
541 arrays with initializers, so neither can we do so here. */
543 tree
544 c_strlen (tree src, int only_value)
546 tree offset_node;
547 HOST_WIDE_INT offset;
548 int max;
549 const char *ptr;
550 location_t loc;
552 STRIP_NOPS (src);
553 if (TREE_CODE (src) == COND_EXPR
554 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
556 tree len1, len2;
558 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
559 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
560 if (tree_int_cst_equal (len1, len2))
561 return len1;
564 if (TREE_CODE (src) == COMPOUND_EXPR
565 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
566 return c_strlen (TREE_OPERAND (src, 1), only_value);
568 loc = EXPR_LOC_OR_LOC (src, input_location);
570 src = string_constant (src, &offset_node);
571 if (src == 0)
572 return NULL_TREE;
574 max = TREE_STRING_LENGTH (src) - 1;
575 ptr = TREE_STRING_POINTER (src);
577 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
579 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
580 compute the offset to the following null if we don't know where to
581 start searching for it. */
582 int i;
584 for (i = 0; i < max; i++)
585 if (ptr[i] == 0)
586 return NULL_TREE;
588 /* We don't know the starting offset, but we do know that the string
589 has no internal zero bytes. We can assume that the offset falls
590 within the bounds of the string; otherwise, the programmer deserves
591 what he gets. Subtract the offset from the length of the string,
592 and return that. This would perhaps not be valid if we were dealing
593 with named arrays in addition to literal string constants. */
595 return size_diffop_loc (loc, size_int (max), offset_node);
598 /* We have a known offset into the string. Start searching there for
599 a null character if we can represent it as a single HOST_WIDE_INT. */
600 if (offset_node == 0)
601 offset = 0;
602 else if (! tree_fits_shwi_p (offset_node))
603 offset = -1;
604 else
605 offset = tree_to_shwi (offset_node);
607 /* If the offset is known to be out of bounds, warn, and call strlen at
608 runtime. */
609 if (offset < 0 || offset > max)
611 /* Suppress multiple warnings for propagated constant strings. */
612 if (! TREE_NO_WARNING (src))
614 warning_at (loc, 0, "offset outside bounds of constant string");
615 TREE_NO_WARNING (src) = 1;
617 return NULL_TREE;
620 /* Use strlen to search for the first zero byte. Since any strings
621 constructed with build_string will have nulls appended, we win even
622 if we get handed something like (char[4])"abcd".
624 Since OFFSET is our starting index into the string, no further
625 calculation is needed. */
626 return ssize_int (strlen (ptr + offset));
629 /* Return a char pointer for a C string if it is a string constant
630 or sum of string constant and integer constant. */
632 static const char *
633 c_getstr (tree src)
635 tree offset_node;
637 src = string_constant (src, &offset_node);
638 if (src == 0)
639 return 0;
641 if (offset_node == 0)
642 return TREE_STRING_POINTER (src);
643 else if (!tree_fits_uhwi_p (offset_node)
644 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
645 return 0;
647 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
650 /* Return a constant integer corresponding to target reading
651 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
653 static rtx
654 c_readstr (const char *str, enum machine_mode mode)
656 HOST_WIDE_INT ch;
657 unsigned int i, j;
658 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
660 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
661 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
662 / HOST_BITS_PER_WIDE_INT;
664 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
665 for (i = 0; i < len; i++)
666 tmp[i] = 0;
668 ch = 1;
669 for (i = 0; i < GET_MODE_SIZE (mode); i++)
671 j = i;
672 if (WORDS_BIG_ENDIAN)
673 j = GET_MODE_SIZE (mode) - i - 1;
674 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
675 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
676 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
677 j *= BITS_PER_UNIT;
679 if (ch)
680 ch = (unsigned char) str[i];
681 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
684 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
685 return immed_wide_int_const (c, mode);
688 /* Cast a target constant CST to target CHAR and if that value fits into
689 host char type, return zero and put that value into variable pointed to by
690 P. */
692 static int
693 target_char_cast (tree cst, char *p)
695 unsigned HOST_WIDE_INT val, hostval;
697 if (TREE_CODE (cst) != INTEGER_CST
698 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
699 return 1;
701 /* Do not care if it fits or not right here. */
702 val = TREE_INT_CST_LOW (cst);
704 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
705 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
707 hostval = val;
708 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
709 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
711 if (val != hostval)
712 return 1;
714 *p = hostval;
715 return 0;
718 /* Similar to save_expr, but assumes that arbitrary code is not executed
719 in between the multiple evaluations. In particular, we assume that a
720 non-addressable local variable will not be modified. */
722 static tree
723 builtin_save_expr (tree exp)
725 if (TREE_CODE (exp) == SSA_NAME
726 || (TREE_ADDRESSABLE (exp) == 0
727 && (TREE_CODE (exp) == PARM_DECL
728 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
729 return exp;
731 return save_expr (exp);
734 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
735 times to get the address of either a higher stack frame, or a return
736 address located within it (depending on FNDECL_CODE). */
738 static rtx
739 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
741 int i;
743 #ifdef INITIAL_FRAME_ADDRESS_RTX
744 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
745 #else
746 rtx tem;
748 /* For a zero count with __builtin_return_address, we don't care what
749 frame address we return, because target-specific definitions will
750 override us. Therefore frame pointer elimination is OK, and using
751 the soft frame pointer is OK.
753 For a nonzero count, or a zero count with __builtin_frame_address,
754 we require a stable offset from the current frame pointer to the
755 previous one, so we must use the hard frame pointer, and
756 we must disable frame pointer elimination. */
757 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
758 tem = frame_pointer_rtx;
759 else
761 tem = hard_frame_pointer_rtx;
763 /* Tell reload not to eliminate the frame pointer. */
764 crtl->accesses_prior_frames = 1;
766 #endif
768 /* Some machines need special handling before we can access
769 arbitrary frames. For example, on the SPARC, we must first flush
770 all register windows to the stack. */
771 #ifdef SETUP_FRAME_ADDRESSES
772 if (count > 0)
773 SETUP_FRAME_ADDRESSES ();
774 #endif
776 /* On the SPARC, the return address is not in the frame, it is in a
777 register. There is no way to access it off of the current frame
778 pointer, but it can be accessed off the previous frame pointer by
779 reading the value from the register window save area. */
780 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
781 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
782 count--;
783 #endif
785 /* Scan back COUNT frames to the specified frame. */
786 for (i = 0; i < count; i++)
788 /* Assume the dynamic chain pointer is in the word that the
789 frame address points to, unless otherwise specified. */
790 #ifdef DYNAMIC_CHAIN_ADDRESS
791 tem = DYNAMIC_CHAIN_ADDRESS (tem);
792 #endif
793 tem = memory_address (Pmode, tem);
794 tem = gen_frame_mem (Pmode, tem);
795 tem = copy_to_reg (tem);
798 /* For __builtin_frame_address, return what we've got. But, on
799 the SPARC for example, we may have to add a bias. */
800 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
801 #ifdef FRAME_ADDR_RTX
802 return FRAME_ADDR_RTX (tem);
803 #else
804 return tem;
805 #endif
807 /* For __builtin_return_address, get the return address from that frame. */
808 #ifdef RETURN_ADDR_RTX
809 tem = RETURN_ADDR_RTX (count, tem);
810 #else
811 tem = memory_address (Pmode,
812 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
813 tem = gen_frame_mem (Pmode, tem);
814 #endif
815 return tem;
818 /* Alias set used for setjmp buffer. */
819 static alias_set_type setjmp_alias_set = -1;
821 /* Construct the leading half of a __builtin_setjmp call. Control will
822 return to RECEIVER_LABEL. This is also called directly by the SJLJ
823 exception handling code. */
825 void
826 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
828 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
829 rtx stack_save;
830 rtx mem;
832 if (setjmp_alias_set == -1)
833 setjmp_alias_set = new_alias_set ();
835 buf_addr = convert_memory_address (Pmode, buf_addr);
837 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
839 /* We store the frame pointer and the address of receiver_label in
840 the buffer and use the rest of it for the stack save area, which
841 is machine-dependent. */
843 mem = gen_rtx_MEM (Pmode, buf_addr);
844 set_mem_alias_set (mem, setjmp_alias_set);
845 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
847 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
848 GET_MODE_SIZE (Pmode))),
849 set_mem_alias_set (mem, setjmp_alias_set);
851 emit_move_insn (validize_mem (mem),
852 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
854 stack_save = gen_rtx_MEM (sa_mode,
855 plus_constant (Pmode, buf_addr,
856 2 * GET_MODE_SIZE (Pmode)));
857 set_mem_alias_set (stack_save, setjmp_alias_set);
858 emit_stack_save (SAVE_NONLOCAL, &stack_save);
860 /* If there is further processing to do, do it. */
861 #ifdef HAVE_builtin_setjmp_setup
862 if (HAVE_builtin_setjmp_setup)
863 emit_insn (gen_builtin_setjmp_setup (buf_addr));
864 #endif
866 /* We have a nonlocal label. */
867 cfun->has_nonlocal_label = 1;
870 /* Construct the trailing part of a __builtin_setjmp call. This is
871 also called directly by the SJLJ exception handling code.
872 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
874 void
875 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
877 rtx chain;
879 /* Mark the FP as used when we get here, so we have to make sure it's
880 marked as used by this function. */
881 emit_use (hard_frame_pointer_rtx);
883 /* Mark the static chain as clobbered here so life information
884 doesn't get messed up for it. */
885 chain = targetm.calls.static_chain (current_function_decl, true);
886 if (chain && REG_P (chain))
887 emit_clobber (chain);
889 /* Now put in the code to restore the frame pointer, and argument
890 pointer, if needed. */
891 #ifdef HAVE_nonlocal_goto
892 if (! HAVE_nonlocal_goto)
893 #endif
895 /* First adjust our frame pointer to its actual value. It was
896 previously set to the start of the virtual area corresponding to
897 the stacked variables when we branched here and now needs to be
898 adjusted to the actual hardware fp value.
900 Assignments to virtual registers are converted by
901 instantiate_virtual_regs into the corresponding assignment
902 to the underlying register (fp in this case) that makes
903 the original assignment true.
904 So the following insn will actually be decrementing fp by
905 STARTING_FRAME_OFFSET. */
906 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
908 /* Restoring the frame pointer also modifies the hard frame pointer.
909 Mark it used (so that the previous assignment remains live once
910 the frame pointer is eliminated) and clobbered (to represent the
911 implicit update from the assignment). */
912 emit_use (hard_frame_pointer_rtx);
913 emit_clobber (hard_frame_pointer_rtx);
916 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
917 if (fixed_regs[ARG_POINTER_REGNUM])
919 #ifdef ELIMINABLE_REGS
920 /* If the argument pointer can be eliminated in favor of the
921 frame pointer, we don't need to restore it. We assume here
922 that if such an elimination is present, it can always be used.
923 This is the case on all known machines; if we don't make this
924 assumption, we do unnecessary saving on many machines. */
925 size_t i;
926 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
928 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
929 if (elim_regs[i].from == ARG_POINTER_REGNUM
930 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
931 break;
933 if (i == ARRAY_SIZE (elim_regs))
934 #endif
936 /* Now restore our arg pointer from the address at which it
937 was saved in our stack frame. */
938 emit_move_insn (crtl->args.internal_arg_pointer,
939 copy_to_reg (get_arg_pointer_save_area ()));
942 #endif
944 #ifdef HAVE_builtin_setjmp_receiver
945 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
946 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
947 else
948 #endif
949 #ifdef HAVE_nonlocal_goto_receiver
950 if (HAVE_nonlocal_goto_receiver)
951 emit_insn (gen_nonlocal_goto_receiver ());
952 else
953 #endif
954 { /* Nothing */ }
956 /* We must not allow the code we just generated to be reordered by
957 scheduling. Specifically, the update of the frame pointer must
958 happen immediately, not later. */
959 emit_insn (gen_blockage ());
962 /* __builtin_longjmp is passed a pointer to an array of five words (not
963 all will be used on all machines). It operates similarly to the C
964 library function of the same name, but is more efficient. Much of
965 the code below is copied from the handling of non-local gotos. */
967 static void
968 expand_builtin_longjmp (rtx buf_addr, rtx value)
970 rtx fp, lab, stack, insn, last;
971 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
973 /* DRAP is needed for stack realign if longjmp is expanded to current
974 function */
975 if (SUPPORTS_STACK_ALIGNMENT)
976 crtl->need_drap = true;
978 if (setjmp_alias_set == -1)
979 setjmp_alias_set = new_alias_set ();
981 buf_addr = convert_memory_address (Pmode, buf_addr);
983 buf_addr = force_reg (Pmode, buf_addr);
985 /* We require that the user must pass a second argument of 1, because
986 that is what builtin_setjmp will return. */
987 gcc_assert (value == const1_rtx);
989 last = get_last_insn ();
990 #ifdef HAVE_builtin_longjmp
991 if (HAVE_builtin_longjmp)
992 emit_insn (gen_builtin_longjmp (buf_addr));
993 else
994 #endif
996 fp = gen_rtx_MEM (Pmode, buf_addr);
997 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
998 GET_MODE_SIZE (Pmode)));
1000 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1001 2 * GET_MODE_SIZE (Pmode)));
1002 set_mem_alias_set (fp, setjmp_alias_set);
1003 set_mem_alias_set (lab, setjmp_alias_set);
1004 set_mem_alias_set (stack, setjmp_alias_set);
1006 /* Pick up FP, label, and SP from the block and jump. This code is
1007 from expand_goto in stmt.c; see there for detailed comments. */
1008 #ifdef HAVE_nonlocal_goto
1009 if (HAVE_nonlocal_goto)
1010 /* We have to pass a value to the nonlocal_goto pattern that will
1011 get copied into the static_chain pointer, but it does not matter
1012 what that value is, because builtin_setjmp does not use it. */
1013 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1014 else
1015 #endif
1017 lab = copy_to_reg (lab);
1019 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1020 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1022 emit_move_insn (hard_frame_pointer_rtx, fp);
1023 emit_stack_restore (SAVE_NONLOCAL, stack);
1025 emit_use (hard_frame_pointer_rtx);
1026 emit_use (stack_pointer_rtx);
1027 emit_indirect_jump (lab);
1031 /* Search backwards and mark the jump insn as a non-local goto.
1032 Note that this precludes the use of __builtin_longjmp to a
1033 __builtin_setjmp target in the same function. However, we've
1034 already cautioned the user that these functions are for
1035 internal exception handling use only. */
1036 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1038 gcc_assert (insn != last);
1040 if (JUMP_P (insn))
1042 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1043 break;
1045 else if (CALL_P (insn))
1046 break;
1050 static inline bool
1051 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1053 return (iter->i < iter->n);
1056 /* This function validates the types of a function call argument list
1057 against a specified list of tree_codes. If the last specifier is a 0,
1058 that represents an ellipses, otherwise the last specifier must be a
1059 VOID_TYPE. */
1061 static bool
1062 validate_arglist (const_tree callexpr, ...)
1064 enum tree_code code;
1065 bool res = 0;
1066 va_list ap;
1067 const_call_expr_arg_iterator iter;
1068 const_tree arg;
1070 va_start (ap, callexpr);
1071 init_const_call_expr_arg_iterator (callexpr, &iter);
1075 code = (enum tree_code) va_arg (ap, int);
1076 switch (code)
1078 case 0:
1079 /* This signifies an ellipses, any further arguments are all ok. */
1080 res = true;
1081 goto end;
1082 case VOID_TYPE:
1083 /* This signifies an endlink, if no arguments remain, return
1084 true, otherwise return false. */
1085 res = !more_const_call_expr_args_p (&iter);
1086 goto end;
1087 default:
1088 /* If no parameters remain or the parameter's code does not
1089 match the specified code, return false. Otherwise continue
1090 checking any remaining arguments. */
1091 arg = next_const_call_expr_arg (&iter);
1092 if (!validate_arg (arg, code))
1093 goto end;
1094 break;
1097 while (1);
1099 /* We need gotos here since we can only have one VA_CLOSE in a
1100 function. */
1101 end: ;
1102 va_end (ap);
1104 return res;
1107 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1108 and the address of the save area. */
1110 static rtx
1111 expand_builtin_nonlocal_goto (tree exp)
1113 tree t_label, t_save_area;
1114 rtx r_label, r_save_area, r_fp, r_sp, insn;
1116 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1117 return NULL_RTX;
1119 t_label = CALL_EXPR_ARG (exp, 0);
1120 t_save_area = CALL_EXPR_ARG (exp, 1);
1122 r_label = expand_normal (t_label);
1123 r_label = convert_memory_address (Pmode, r_label);
1124 r_save_area = expand_normal (t_save_area);
1125 r_save_area = convert_memory_address (Pmode, r_save_area);
1126 /* Copy the address of the save location to a register just in case it was
1127 based on the frame pointer. */
1128 r_save_area = copy_to_reg (r_save_area);
1129 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1130 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1131 plus_constant (Pmode, r_save_area,
1132 GET_MODE_SIZE (Pmode)));
1134 crtl->has_nonlocal_goto = 1;
1136 #ifdef HAVE_nonlocal_goto
1137 /* ??? We no longer need to pass the static chain value, afaik. */
1138 if (HAVE_nonlocal_goto)
1139 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1140 else
1141 #endif
1143 r_label = copy_to_reg (r_label);
1145 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1146 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1148 /* Restore frame pointer for containing function. */
1149 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1150 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1152 /* USE of hard_frame_pointer_rtx added for consistency;
1153 not clear if really needed. */
1154 emit_use (hard_frame_pointer_rtx);
1155 emit_use (stack_pointer_rtx);
1157 /* If the architecture is using a GP register, we must
1158 conservatively assume that the target function makes use of it.
1159 The prologue of functions with nonlocal gotos must therefore
1160 initialize the GP register to the appropriate value, and we
1161 must then make sure that this value is live at the point
1162 of the jump. (Note that this doesn't necessarily apply
1163 to targets with a nonlocal_goto pattern; they are free
1164 to implement it in their own way. Note also that this is
1165 a no-op if the GP register is a global invariant.) */
1166 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1167 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1168 emit_use (pic_offset_table_rtx);
1170 emit_indirect_jump (r_label);
1173 /* Search backwards to the jump insn and mark it as a
1174 non-local goto. */
1175 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1177 if (JUMP_P (insn))
1179 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1180 break;
1182 else if (CALL_P (insn))
1183 break;
1186 return const0_rtx;
1189 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1190 (not all will be used on all machines) that was passed to __builtin_setjmp.
1191 It updates the stack pointer in that block to correspond to the current
1192 stack pointer. */
1194 static void
1195 expand_builtin_update_setjmp_buf (rtx buf_addr)
1197 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1198 rtx stack_save
1199 = gen_rtx_MEM (sa_mode,
1200 memory_address
1201 (sa_mode,
1202 plus_constant (Pmode, buf_addr,
1203 2 * GET_MODE_SIZE (Pmode))));
1205 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1208 /* Expand a call to __builtin_prefetch. For a target that does not support
1209 data prefetch, evaluate the memory address argument in case it has side
1210 effects. */
1212 static void
1213 expand_builtin_prefetch (tree exp)
1215 tree arg0, arg1, arg2;
1216 int nargs;
1217 rtx op0, op1, op2;
1219 if (!validate_arglist (exp, POINTER_TYPE, 0))
1220 return;
1222 arg0 = CALL_EXPR_ARG (exp, 0);
1224 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1225 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1226 locality). */
1227 nargs = call_expr_nargs (exp);
1228 if (nargs > 1)
1229 arg1 = CALL_EXPR_ARG (exp, 1);
1230 else
1231 arg1 = integer_zero_node;
1232 if (nargs > 2)
1233 arg2 = CALL_EXPR_ARG (exp, 2);
1234 else
1235 arg2 = integer_three_node;
1237 /* Argument 0 is an address. */
1238 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1240 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1241 if (TREE_CODE (arg1) != INTEGER_CST)
1243 error ("second argument to %<__builtin_prefetch%> must be a constant");
1244 arg1 = integer_zero_node;
1246 op1 = expand_normal (arg1);
1247 /* Argument 1 must be either zero or one. */
1248 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1250 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1251 " using zero");
1252 op1 = const0_rtx;
1255 /* Argument 2 (locality) must be a compile-time constant int. */
1256 if (TREE_CODE (arg2) != INTEGER_CST)
1258 error ("third argument to %<__builtin_prefetch%> must be a constant");
1259 arg2 = integer_zero_node;
1261 op2 = expand_normal (arg2);
1262 /* Argument 2 must be 0, 1, 2, or 3. */
1263 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1265 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1266 op2 = const0_rtx;
1269 #ifdef HAVE_prefetch
1270 if (HAVE_prefetch)
1272 struct expand_operand ops[3];
1274 create_address_operand (&ops[0], op0);
1275 create_integer_operand (&ops[1], INTVAL (op1));
1276 create_integer_operand (&ops[2], INTVAL (op2));
1277 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1278 return;
1280 #endif
1282 /* Don't do anything with direct references to volatile memory, but
1283 generate code to handle other side effects. */
1284 if (!MEM_P (op0) && side_effects_p (op0))
1285 emit_insn (op0);
1288 /* Get a MEM rtx for expression EXP which is the address of an operand
1289 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1290 the maximum length of the block of memory that might be accessed or
1291 NULL if unknown. */
1293 static rtx
1294 get_memory_rtx (tree exp, tree len)
1296 tree orig_exp = exp;
1297 rtx addr, mem;
1299 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1300 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1301 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1302 exp = TREE_OPERAND (exp, 0);
1304 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1305 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1307 /* Get an expression we can use to find the attributes to assign to MEM.
1308 First remove any nops. */
1309 while (CONVERT_EXPR_P (exp)
1310 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1311 exp = TREE_OPERAND (exp, 0);
1313 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1314 (as builtin stringops may alias with anything). */
1315 exp = fold_build2 (MEM_REF,
1316 build_array_type (char_type_node,
1317 build_range_type (sizetype,
1318 size_one_node, len)),
1319 exp, build_int_cst (ptr_type_node, 0));
1321 /* If the MEM_REF has no acceptable address, try to get the base object
1322 from the original address we got, and build an all-aliasing
1323 unknown-sized access to that one. */
1324 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1325 set_mem_attributes (mem, exp, 0);
1326 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1327 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1328 0))))
1330 exp = build_fold_addr_expr (exp);
1331 exp = fold_build2 (MEM_REF,
1332 build_array_type (char_type_node,
1333 build_range_type (sizetype,
1334 size_zero_node,
1335 NULL)),
1336 exp, build_int_cst (ptr_type_node, 0));
1337 set_mem_attributes (mem, exp, 0);
1339 set_mem_alias_set (mem, 0);
1340 return mem;
1343 /* Built-in functions to perform an untyped call and return. */
1345 #define apply_args_mode \
1346 (this_target_builtins->x_apply_args_mode)
1347 #define apply_result_mode \
1348 (this_target_builtins->x_apply_result_mode)
1350 /* Return the size required for the block returned by __builtin_apply_args,
1351 and initialize apply_args_mode. */
1353 static int
1354 apply_args_size (void)
1356 static int size = -1;
1357 int align;
1358 unsigned int regno;
1359 enum machine_mode mode;
1361 /* The values computed by this function never change. */
1362 if (size < 0)
1364 /* The first value is the incoming arg-pointer. */
1365 size = GET_MODE_SIZE (Pmode);
1367 /* The second value is the structure value address unless this is
1368 passed as an "invisible" first argument. */
1369 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1370 size += GET_MODE_SIZE (Pmode);
1372 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1373 if (FUNCTION_ARG_REGNO_P (regno))
1375 mode = targetm.calls.get_raw_arg_mode (regno);
1377 gcc_assert (mode != VOIDmode);
1379 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1380 if (size % align != 0)
1381 size = CEIL (size, align) * align;
1382 size += GET_MODE_SIZE (mode);
1383 apply_args_mode[regno] = mode;
1385 else
1387 apply_args_mode[regno] = VOIDmode;
1390 return size;
1393 /* Return the size required for the block returned by __builtin_apply,
1394 and initialize apply_result_mode. */
1396 static int
1397 apply_result_size (void)
1399 static int size = -1;
1400 int align, regno;
1401 enum machine_mode mode;
1403 /* The values computed by this function never change. */
1404 if (size < 0)
1406 size = 0;
1408 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1409 if (targetm.calls.function_value_regno_p (regno))
1411 mode = targetm.calls.get_raw_result_mode (regno);
1413 gcc_assert (mode != VOIDmode);
1415 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1416 if (size % align != 0)
1417 size = CEIL (size, align) * align;
1418 size += GET_MODE_SIZE (mode);
1419 apply_result_mode[regno] = mode;
1421 else
1422 apply_result_mode[regno] = VOIDmode;
1424 /* Allow targets that use untyped_call and untyped_return to override
1425 the size so that machine-specific information can be stored here. */
1426 #ifdef APPLY_RESULT_SIZE
1427 size = APPLY_RESULT_SIZE;
1428 #endif
1430 return size;
1433 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1434 /* Create a vector describing the result block RESULT. If SAVEP is true,
1435 the result block is used to save the values; otherwise it is used to
1436 restore the values. */
1438 static rtx
1439 result_vector (int savep, rtx result)
1441 int regno, size, align, nelts;
1442 enum machine_mode mode;
1443 rtx reg, mem;
1444 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1446 size = nelts = 0;
1447 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1448 if ((mode = apply_result_mode[regno]) != VOIDmode)
1450 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1451 if (size % align != 0)
1452 size = CEIL (size, align) * align;
1453 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1454 mem = adjust_address (result, mode, size);
1455 savevec[nelts++] = (savep
1456 ? gen_rtx_SET (VOIDmode, mem, reg)
1457 : gen_rtx_SET (VOIDmode, reg, mem));
1458 size += GET_MODE_SIZE (mode);
1460 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1462 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1464 /* Save the state required to perform an untyped call with the same
1465 arguments as were passed to the current function. */
1467 static rtx
1468 expand_builtin_apply_args_1 (void)
1470 rtx registers, tem;
1471 int size, align, regno;
1472 enum machine_mode mode;
1473 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1475 /* Create a block where the arg-pointer, structure value address,
1476 and argument registers can be saved. */
1477 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1479 /* Walk past the arg-pointer and structure value address. */
1480 size = GET_MODE_SIZE (Pmode);
1481 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1482 size += GET_MODE_SIZE (Pmode);
1484 /* Save each register used in calling a function to the block. */
1485 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1486 if ((mode = apply_args_mode[regno]) != VOIDmode)
1488 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1489 if (size % align != 0)
1490 size = CEIL (size, align) * align;
1492 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1494 emit_move_insn (adjust_address (registers, mode, size), tem);
1495 size += GET_MODE_SIZE (mode);
1498 /* Save the arg pointer to the block. */
1499 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1500 #ifdef STACK_GROWS_DOWNWARD
1501 /* We need the pointer as the caller actually passed them to us, not
1502 as we might have pretended they were passed. Make sure it's a valid
1503 operand, as emit_move_insn isn't expected to handle a PLUS. */
1505 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1506 NULL_RTX);
1507 #endif
1508 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1510 size = GET_MODE_SIZE (Pmode);
1512 /* Save the structure value address unless this is passed as an
1513 "invisible" first argument. */
1514 if (struct_incoming_value)
1516 emit_move_insn (adjust_address (registers, Pmode, size),
1517 copy_to_reg (struct_incoming_value));
1518 size += GET_MODE_SIZE (Pmode);
1521 /* Return the address of the block. */
1522 return copy_addr_to_reg (XEXP (registers, 0));
1525 /* __builtin_apply_args returns block of memory allocated on
1526 the stack into which is stored the arg pointer, structure
1527 value address, static chain, and all the registers that might
1528 possibly be used in performing a function call. The code is
1529 moved to the start of the function so the incoming values are
1530 saved. */
1532 static rtx
1533 expand_builtin_apply_args (void)
1535 /* Don't do __builtin_apply_args more than once in a function.
1536 Save the result of the first call and reuse it. */
1537 if (apply_args_value != 0)
1538 return apply_args_value;
1540 /* When this function is called, it means that registers must be
1541 saved on entry to this function. So we migrate the
1542 call to the first insn of this function. */
1543 rtx temp;
1544 rtx seq;
1546 start_sequence ();
1547 temp = expand_builtin_apply_args_1 ();
1548 seq = get_insns ();
1549 end_sequence ();
1551 apply_args_value = temp;
1553 /* Put the insns after the NOTE that starts the function.
1554 If this is inside a start_sequence, make the outer-level insn
1555 chain current, so the code is placed at the start of the
1556 function. If internal_arg_pointer is a non-virtual pseudo,
1557 it needs to be placed after the function that initializes
1558 that pseudo. */
1559 push_topmost_sequence ();
1560 if (REG_P (crtl->args.internal_arg_pointer)
1561 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1562 emit_insn_before (seq, parm_birth_insn);
1563 else
1564 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1565 pop_topmost_sequence ();
1566 return temp;
1570 /* Perform an untyped call and save the state required to perform an
1571 untyped return of whatever value was returned by the given function. */
1573 static rtx
1574 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1576 int size, align, regno;
1577 enum machine_mode mode;
1578 rtx incoming_args, result, reg, dest, src, call_insn;
1579 rtx old_stack_level = 0;
1580 rtx call_fusage = 0;
1581 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1583 arguments = convert_memory_address (Pmode, arguments);
1585 /* Create a block where the return registers can be saved. */
1586 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1588 /* Fetch the arg pointer from the ARGUMENTS block. */
1589 incoming_args = gen_reg_rtx (Pmode);
1590 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1591 #ifndef STACK_GROWS_DOWNWARD
1592 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1593 incoming_args, 0, OPTAB_LIB_WIDEN);
1594 #endif
1596 /* Push a new argument block and copy the arguments. Do not allow
1597 the (potential) memcpy call below to interfere with our stack
1598 manipulations. */
1599 do_pending_stack_adjust ();
1600 NO_DEFER_POP;
1602 /* Save the stack with nonlocal if available. */
1603 #ifdef HAVE_save_stack_nonlocal
1604 if (HAVE_save_stack_nonlocal)
1605 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1606 else
1607 #endif
1608 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1610 /* Allocate a block of memory onto the stack and copy the memory
1611 arguments to the outgoing arguments address. We can pass TRUE
1612 as the 4th argument because we just saved the stack pointer
1613 and will restore it right after the call. */
1614 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1616 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1617 may have already set current_function_calls_alloca to true.
1618 current_function_calls_alloca won't be set if argsize is zero,
1619 so we have to guarantee need_drap is true here. */
1620 if (SUPPORTS_STACK_ALIGNMENT)
1621 crtl->need_drap = true;
1623 dest = virtual_outgoing_args_rtx;
1624 #ifndef 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));
1629 #endif
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 #ifdef HAVE_untyped_call
1684 if (HAVE_untyped_call)
1685 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1686 result, result_vector (1, result)));
1687 else
1688 #endif
1689 #ifdef HAVE_call_value
1690 if (HAVE_call_value)
1692 rtx valreg = 0;
1694 /* Locate the unique return register. It is not possible to
1695 express a call that sets more than one return register using
1696 call_value; use untyped_call for that. In fact, untyped_call
1697 only needs to save the return registers in the given block. */
1698 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1699 if ((mode = apply_result_mode[regno]) != VOIDmode)
1701 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1703 valreg = gen_rtx_REG (mode, regno);
1706 emit_call_insn (GEN_CALL_VALUE (valreg,
1707 gen_rtx_MEM (FUNCTION_MODE, function),
1708 const0_rtx, NULL_RTX, const0_rtx));
1710 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1712 else
1713 #endif
1714 gcc_unreachable ();
1716 /* Find the CALL insn we just emitted, and attach the register usage
1717 information. */
1718 call_insn = last_call_insn ();
1719 add_function_usage_to (call_insn, call_fusage);
1721 /* Restore the stack. */
1722 #ifdef HAVE_save_stack_nonlocal
1723 if (HAVE_save_stack_nonlocal)
1724 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1725 else
1726 #endif
1727 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1728 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1730 OK_DEFER_POP;
1732 /* Return the address of the result block. */
1733 result = copy_addr_to_reg (XEXP (result, 0));
1734 return convert_memory_address (ptr_mode, result);
1737 /* Perform an untyped return. */
1739 static void
1740 expand_builtin_return (rtx result)
1742 int size, align, regno;
1743 enum machine_mode mode;
1744 rtx reg;
1745 rtx call_fusage = 0;
1747 result = convert_memory_address (Pmode, result);
1749 apply_result_size ();
1750 result = gen_rtx_MEM (BLKmode, result);
1752 #ifdef HAVE_untyped_return
1753 if (HAVE_untyped_return)
1755 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1756 emit_barrier ();
1757 return;
1759 #endif
1761 /* Restore the return value and note that each value is used. */
1762 size = 0;
1763 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1764 if ((mode = apply_result_mode[regno]) != VOIDmode)
1766 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1767 if (size % align != 0)
1768 size = CEIL (size, align) * align;
1769 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1770 emit_move_insn (reg, adjust_address (result, mode, size));
1772 push_to_sequence (call_fusage);
1773 emit_use (reg);
1774 call_fusage = get_insns ();
1775 end_sequence ();
1776 size += GET_MODE_SIZE (mode);
1779 /* Put the USE insns before the return. */
1780 emit_insn (call_fusage);
1782 /* Return whatever values was restored by jumping directly to the end
1783 of the function. */
1784 expand_naked_return ();
1787 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1789 static enum type_class
1790 type_to_class (tree type)
1792 switch (TREE_CODE (type))
1794 case VOID_TYPE: return void_type_class;
1795 case INTEGER_TYPE: return integer_type_class;
1796 case ENUMERAL_TYPE: return enumeral_type_class;
1797 case BOOLEAN_TYPE: return boolean_type_class;
1798 case POINTER_TYPE: return pointer_type_class;
1799 case REFERENCE_TYPE: return reference_type_class;
1800 case OFFSET_TYPE: return offset_type_class;
1801 case REAL_TYPE: return real_type_class;
1802 case COMPLEX_TYPE: return complex_type_class;
1803 case FUNCTION_TYPE: return function_type_class;
1804 case METHOD_TYPE: return method_type_class;
1805 case RECORD_TYPE: return record_type_class;
1806 case UNION_TYPE:
1807 case QUAL_UNION_TYPE: return union_type_class;
1808 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1809 ? string_type_class : array_type_class);
1810 case LANG_TYPE: return lang_type_class;
1811 default: return no_type_class;
1815 /* Expand a call EXP to __builtin_classify_type. */
1817 static rtx
1818 expand_builtin_classify_type (tree exp)
1820 if (call_expr_nargs (exp))
1821 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1822 return GEN_INT (no_type_class);
1825 /* This helper macro, meant to be used in mathfn_built_in below,
1826 determines which among a set of three builtin math functions is
1827 appropriate for a given type mode. The `F' and `L' cases are
1828 automatically generated from the `double' case. */
1829 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1830 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1831 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1832 fcodel = BUILT_IN_MATHFN##L ; break;
1833 /* Similar to above, but appends _R after any F/L suffix. */
1834 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1835 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1836 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1837 fcodel = BUILT_IN_MATHFN##L_R ; break;
1839 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1840 if available. If IMPLICIT is true use the implicit builtin declaration,
1841 otherwise use the explicit declaration. If we can't do the conversion,
1842 return zero. */
1844 static tree
1845 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1847 enum built_in_function fcode, fcodef, fcodel, fcode2;
1849 switch (fn)
1851 CASE_MATHFN (BUILT_IN_ACOS)
1852 CASE_MATHFN (BUILT_IN_ACOSH)
1853 CASE_MATHFN (BUILT_IN_ASIN)
1854 CASE_MATHFN (BUILT_IN_ASINH)
1855 CASE_MATHFN (BUILT_IN_ATAN)
1856 CASE_MATHFN (BUILT_IN_ATAN2)
1857 CASE_MATHFN (BUILT_IN_ATANH)
1858 CASE_MATHFN (BUILT_IN_CBRT)
1859 CASE_MATHFN (BUILT_IN_CEIL)
1860 CASE_MATHFN (BUILT_IN_CEXPI)
1861 CASE_MATHFN (BUILT_IN_COPYSIGN)
1862 CASE_MATHFN (BUILT_IN_COS)
1863 CASE_MATHFN (BUILT_IN_COSH)
1864 CASE_MATHFN (BUILT_IN_DREM)
1865 CASE_MATHFN (BUILT_IN_ERF)
1866 CASE_MATHFN (BUILT_IN_ERFC)
1867 CASE_MATHFN (BUILT_IN_EXP)
1868 CASE_MATHFN (BUILT_IN_EXP10)
1869 CASE_MATHFN (BUILT_IN_EXP2)
1870 CASE_MATHFN (BUILT_IN_EXPM1)
1871 CASE_MATHFN (BUILT_IN_FABS)
1872 CASE_MATHFN (BUILT_IN_FDIM)
1873 CASE_MATHFN (BUILT_IN_FLOOR)
1874 CASE_MATHFN (BUILT_IN_FMA)
1875 CASE_MATHFN (BUILT_IN_FMAX)
1876 CASE_MATHFN (BUILT_IN_FMIN)
1877 CASE_MATHFN (BUILT_IN_FMOD)
1878 CASE_MATHFN (BUILT_IN_FREXP)
1879 CASE_MATHFN (BUILT_IN_GAMMA)
1880 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1881 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1882 CASE_MATHFN (BUILT_IN_HYPOT)
1883 CASE_MATHFN (BUILT_IN_ILOGB)
1884 CASE_MATHFN (BUILT_IN_ICEIL)
1885 CASE_MATHFN (BUILT_IN_IFLOOR)
1886 CASE_MATHFN (BUILT_IN_INF)
1887 CASE_MATHFN (BUILT_IN_IRINT)
1888 CASE_MATHFN (BUILT_IN_IROUND)
1889 CASE_MATHFN (BUILT_IN_ISINF)
1890 CASE_MATHFN (BUILT_IN_J0)
1891 CASE_MATHFN (BUILT_IN_J1)
1892 CASE_MATHFN (BUILT_IN_JN)
1893 CASE_MATHFN (BUILT_IN_LCEIL)
1894 CASE_MATHFN (BUILT_IN_LDEXP)
1895 CASE_MATHFN (BUILT_IN_LFLOOR)
1896 CASE_MATHFN (BUILT_IN_LGAMMA)
1897 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1898 CASE_MATHFN (BUILT_IN_LLCEIL)
1899 CASE_MATHFN (BUILT_IN_LLFLOOR)
1900 CASE_MATHFN (BUILT_IN_LLRINT)
1901 CASE_MATHFN (BUILT_IN_LLROUND)
1902 CASE_MATHFN (BUILT_IN_LOG)
1903 CASE_MATHFN (BUILT_IN_LOG10)
1904 CASE_MATHFN (BUILT_IN_LOG1P)
1905 CASE_MATHFN (BUILT_IN_LOG2)
1906 CASE_MATHFN (BUILT_IN_LOGB)
1907 CASE_MATHFN (BUILT_IN_LRINT)
1908 CASE_MATHFN (BUILT_IN_LROUND)
1909 CASE_MATHFN (BUILT_IN_MODF)
1910 CASE_MATHFN (BUILT_IN_NAN)
1911 CASE_MATHFN (BUILT_IN_NANS)
1912 CASE_MATHFN (BUILT_IN_NEARBYINT)
1913 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1914 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1915 CASE_MATHFN (BUILT_IN_POW)
1916 CASE_MATHFN (BUILT_IN_POWI)
1917 CASE_MATHFN (BUILT_IN_POW10)
1918 CASE_MATHFN (BUILT_IN_REMAINDER)
1919 CASE_MATHFN (BUILT_IN_REMQUO)
1920 CASE_MATHFN (BUILT_IN_RINT)
1921 CASE_MATHFN (BUILT_IN_ROUND)
1922 CASE_MATHFN (BUILT_IN_SCALB)
1923 CASE_MATHFN (BUILT_IN_SCALBLN)
1924 CASE_MATHFN (BUILT_IN_SCALBN)
1925 CASE_MATHFN (BUILT_IN_SIGNBIT)
1926 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1927 CASE_MATHFN (BUILT_IN_SIN)
1928 CASE_MATHFN (BUILT_IN_SINCOS)
1929 CASE_MATHFN (BUILT_IN_SINH)
1930 CASE_MATHFN (BUILT_IN_SQRT)
1931 CASE_MATHFN (BUILT_IN_TAN)
1932 CASE_MATHFN (BUILT_IN_TANH)
1933 CASE_MATHFN (BUILT_IN_TGAMMA)
1934 CASE_MATHFN (BUILT_IN_TRUNC)
1935 CASE_MATHFN (BUILT_IN_Y0)
1936 CASE_MATHFN (BUILT_IN_Y1)
1937 CASE_MATHFN (BUILT_IN_YN)
1939 default:
1940 return NULL_TREE;
1943 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1944 fcode2 = fcode;
1945 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1946 fcode2 = fcodef;
1947 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1948 fcode2 = fcodel;
1949 else
1950 return NULL_TREE;
1952 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1953 return NULL_TREE;
1955 return builtin_decl_explicit (fcode2);
1958 /* Like mathfn_built_in_1(), but always use the implicit array. */
1960 tree
1961 mathfn_built_in (tree type, enum built_in_function fn)
1963 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1966 /* If errno must be maintained, expand the RTL to check if the result,
1967 TARGET, of a built-in function call, EXP, is NaN, and if so set
1968 errno to EDOM. */
1970 static void
1971 expand_errno_check (tree exp, rtx target)
1973 rtx lab = gen_label_rtx ();
1975 /* Test the result; if it is NaN, set errno=EDOM because
1976 the argument was not in the domain. */
1977 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1978 NULL_RTX, NULL_RTX, lab,
1979 /* The jump is very likely. */
1980 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1982 #ifdef TARGET_EDOM
1983 /* If this built-in doesn't throw an exception, set errno directly. */
1984 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1986 #ifdef GEN_ERRNO_RTX
1987 rtx errno_rtx = GEN_ERRNO_RTX;
1988 #else
1989 rtx errno_rtx
1990 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1991 #endif
1992 emit_move_insn (errno_rtx,
1993 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
1994 emit_label (lab);
1995 return;
1997 #endif
1999 /* Make sure the library call isn't expanded as a tail call. */
2000 CALL_EXPR_TAILCALL (exp) = 0;
2002 /* We can't set errno=EDOM directly; let the library call do it.
2003 Pop the arguments right away in case the call gets deleted. */
2004 NO_DEFER_POP;
2005 expand_call (exp, target, 0);
2006 OK_DEFER_POP;
2007 emit_label (lab);
2010 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2011 Return NULL_RTX if a normal call should be emitted rather than expanding
2012 the function in-line. EXP is the expression that is a call to the builtin
2013 function; if convenient, the result should be placed in TARGET.
2014 SUBTARGET may be used as the target for computing one of EXP's operands. */
2016 static rtx
2017 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2019 optab builtin_optab;
2020 rtx op0, insns;
2021 tree fndecl = get_callee_fndecl (exp);
2022 enum machine_mode mode;
2023 bool errno_set = false;
2024 bool try_widening = false;
2025 tree arg;
2027 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2028 return NULL_RTX;
2030 arg = CALL_EXPR_ARG (exp, 0);
2032 switch (DECL_FUNCTION_CODE (fndecl))
2034 CASE_FLT_FN (BUILT_IN_SQRT):
2035 errno_set = ! tree_expr_nonnegative_p (arg);
2036 try_widening = true;
2037 builtin_optab = sqrt_optab;
2038 break;
2039 CASE_FLT_FN (BUILT_IN_EXP):
2040 errno_set = true; builtin_optab = exp_optab; break;
2041 CASE_FLT_FN (BUILT_IN_EXP10):
2042 CASE_FLT_FN (BUILT_IN_POW10):
2043 errno_set = true; builtin_optab = exp10_optab; break;
2044 CASE_FLT_FN (BUILT_IN_EXP2):
2045 errno_set = true; builtin_optab = exp2_optab; break;
2046 CASE_FLT_FN (BUILT_IN_EXPM1):
2047 errno_set = true; builtin_optab = expm1_optab; break;
2048 CASE_FLT_FN (BUILT_IN_LOGB):
2049 errno_set = true; builtin_optab = logb_optab; break;
2050 CASE_FLT_FN (BUILT_IN_LOG):
2051 errno_set = true; builtin_optab = log_optab; break;
2052 CASE_FLT_FN (BUILT_IN_LOG10):
2053 errno_set = true; builtin_optab = log10_optab; break;
2054 CASE_FLT_FN (BUILT_IN_LOG2):
2055 errno_set = true; builtin_optab = log2_optab; break;
2056 CASE_FLT_FN (BUILT_IN_LOG1P):
2057 errno_set = true; builtin_optab = log1p_optab; break;
2058 CASE_FLT_FN (BUILT_IN_ASIN):
2059 builtin_optab = asin_optab; break;
2060 CASE_FLT_FN (BUILT_IN_ACOS):
2061 builtin_optab = acos_optab; break;
2062 CASE_FLT_FN (BUILT_IN_TAN):
2063 builtin_optab = tan_optab; break;
2064 CASE_FLT_FN (BUILT_IN_ATAN):
2065 builtin_optab = atan_optab; break;
2066 CASE_FLT_FN (BUILT_IN_FLOOR):
2067 builtin_optab = floor_optab; break;
2068 CASE_FLT_FN (BUILT_IN_CEIL):
2069 builtin_optab = ceil_optab; break;
2070 CASE_FLT_FN (BUILT_IN_TRUNC):
2071 builtin_optab = btrunc_optab; break;
2072 CASE_FLT_FN (BUILT_IN_ROUND):
2073 builtin_optab = round_optab; break;
2074 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2075 builtin_optab = nearbyint_optab;
2076 if (flag_trapping_math)
2077 break;
2078 /* Else fallthrough and expand as rint. */
2079 CASE_FLT_FN (BUILT_IN_RINT):
2080 builtin_optab = rint_optab; break;
2081 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2082 builtin_optab = significand_optab; break;
2083 default:
2084 gcc_unreachable ();
2087 /* Make a suitable register to place result in. */
2088 mode = TYPE_MODE (TREE_TYPE (exp));
2090 if (! flag_errno_math || ! HONOR_NANS (mode))
2091 errno_set = false;
2093 /* Before working hard, check whether the instruction is available, but try
2094 to widen the mode for specific operations. */
2095 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2096 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2097 && (!errno_set || !optimize_insn_for_size_p ()))
2099 rtx result = gen_reg_rtx (mode);
2101 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2102 need to expand the argument again. This way, we will not perform
2103 side-effects more the once. */
2104 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2106 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2108 start_sequence ();
2110 /* Compute into RESULT.
2111 Set RESULT to wherever the result comes back. */
2112 result = expand_unop (mode, builtin_optab, op0, result, 0);
2114 if (result != 0)
2116 if (errno_set)
2117 expand_errno_check (exp, result);
2119 /* Output the entire sequence. */
2120 insns = get_insns ();
2121 end_sequence ();
2122 emit_insn (insns);
2123 return result;
2126 /* If we were unable to expand via the builtin, stop the sequence
2127 (without outputting the insns) and call to the library function
2128 with the stabilized argument list. */
2129 end_sequence ();
2132 return expand_call (exp, target, target == const0_rtx);
2135 /* Expand a call to the builtin binary math functions (pow and atan2).
2136 Return NULL_RTX if a normal call should be emitted rather than expanding the
2137 function in-line. EXP is the expression that is a call to the builtin
2138 function; if convenient, the result should be placed in TARGET.
2139 SUBTARGET may be used as the target for computing one of EXP's
2140 operands. */
2142 static rtx
2143 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2145 optab builtin_optab;
2146 rtx op0, op1, insns, result;
2147 int op1_type = REAL_TYPE;
2148 tree fndecl = get_callee_fndecl (exp);
2149 tree arg0, arg1;
2150 enum machine_mode mode;
2151 bool errno_set = true;
2153 switch (DECL_FUNCTION_CODE (fndecl))
2155 CASE_FLT_FN (BUILT_IN_SCALBN):
2156 CASE_FLT_FN (BUILT_IN_SCALBLN):
2157 CASE_FLT_FN (BUILT_IN_LDEXP):
2158 op1_type = INTEGER_TYPE;
2159 default:
2160 break;
2163 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2164 return NULL_RTX;
2166 arg0 = CALL_EXPR_ARG (exp, 0);
2167 arg1 = CALL_EXPR_ARG (exp, 1);
2169 switch (DECL_FUNCTION_CODE (fndecl))
2171 CASE_FLT_FN (BUILT_IN_POW):
2172 builtin_optab = pow_optab; break;
2173 CASE_FLT_FN (BUILT_IN_ATAN2):
2174 builtin_optab = atan2_optab; break;
2175 CASE_FLT_FN (BUILT_IN_SCALB):
2176 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2177 return 0;
2178 builtin_optab = scalb_optab; break;
2179 CASE_FLT_FN (BUILT_IN_SCALBN):
2180 CASE_FLT_FN (BUILT_IN_SCALBLN):
2181 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2182 return 0;
2183 /* Fall through... */
2184 CASE_FLT_FN (BUILT_IN_LDEXP):
2185 builtin_optab = ldexp_optab; break;
2186 CASE_FLT_FN (BUILT_IN_FMOD):
2187 builtin_optab = fmod_optab; break;
2188 CASE_FLT_FN (BUILT_IN_REMAINDER):
2189 CASE_FLT_FN (BUILT_IN_DREM):
2190 builtin_optab = remainder_optab; break;
2191 default:
2192 gcc_unreachable ();
2195 /* Make a suitable register to place result in. */
2196 mode = TYPE_MODE (TREE_TYPE (exp));
2198 /* Before working hard, check whether the instruction is available. */
2199 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2200 return NULL_RTX;
2202 result = gen_reg_rtx (mode);
2204 if (! flag_errno_math || ! HONOR_NANS (mode))
2205 errno_set = false;
2207 if (errno_set && optimize_insn_for_size_p ())
2208 return 0;
2210 /* Always stabilize the argument list. */
2211 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2212 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2214 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2215 op1 = expand_normal (arg1);
2217 start_sequence ();
2219 /* Compute into RESULT.
2220 Set RESULT to wherever the result comes back. */
2221 result = expand_binop (mode, builtin_optab, op0, op1,
2222 result, 0, OPTAB_DIRECT);
2224 /* If we were unable to expand via the builtin, stop the sequence
2225 (without outputting the insns) and call to the library function
2226 with the stabilized argument list. */
2227 if (result == 0)
2229 end_sequence ();
2230 return expand_call (exp, target, target == const0_rtx);
2233 if (errno_set)
2234 expand_errno_check (exp, result);
2236 /* Output the entire sequence. */
2237 insns = get_insns ();
2238 end_sequence ();
2239 emit_insn (insns);
2241 return result;
2244 /* Expand a call to the builtin trinary math functions (fma).
2245 Return NULL_RTX if a normal call should be emitted rather than expanding the
2246 function in-line. EXP is the expression that is a call to the builtin
2247 function; if convenient, the result should be placed in TARGET.
2248 SUBTARGET may be used as the target for computing one of EXP's
2249 operands. */
2251 static rtx
2252 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2254 optab builtin_optab;
2255 rtx op0, op1, op2, insns, result;
2256 tree fndecl = get_callee_fndecl (exp);
2257 tree arg0, arg1, arg2;
2258 enum machine_mode mode;
2260 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2261 return NULL_RTX;
2263 arg0 = CALL_EXPR_ARG (exp, 0);
2264 arg1 = CALL_EXPR_ARG (exp, 1);
2265 arg2 = CALL_EXPR_ARG (exp, 2);
2267 switch (DECL_FUNCTION_CODE (fndecl))
2269 CASE_FLT_FN (BUILT_IN_FMA):
2270 builtin_optab = fma_optab; break;
2271 default:
2272 gcc_unreachable ();
2275 /* Make a suitable register to place result in. */
2276 mode = TYPE_MODE (TREE_TYPE (exp));
2278 /* Before working hard, check whether the instruction is available. */
2279 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2280 return NULL_RTX;
2282 result = gen_reg_rtx (mode);
2284 /* Always stabilize the argument list. */
2285 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2286 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2287 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2289 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2290 op1 = expand_normal (arg1);
2291 op2 = expand_normal (arg2);
2293 start_sequence ();
2295 /* Compute into RESULT.
2296 Set RESULT to wherever the result comes back. */
2297 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2298 result, 0);
2300 /* If we were unable to expand via the builtin, stop the sequence
2301 (without outputting the insns) and call to the library function
2302 with the stabilized argument list. */
2303 if (result == 0)
2305 end_sequence ();
2306 return expand_call (exp, target, target == const0_rtx);
2309 /* Output the entire sequence. */
2310 insns = get_insns ();
2311 end_sequence ();
2312 emit_insn (insns);
2314 return result;
2317 /* Expand a call to the builtin sin and cos math functions.
2318 Return NULL_RTX if a normal call should be emitted rather than expanding the
2319 function in-line. EXP is the expression that is a call to the builtin
2320 function; if convenient, the result should be placed in TARGET.
2321 SUBTARGET may be used as the target for computing one of EXP's
2322 operands. */
2324 static rtx
2325 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2327 optab builtin_optab;
2328 rtx op0, insns;
2329 tree fndecl = get_callee_fndecl (exp);
2330 enum machine_mode mode;
2331 tree arg;
2333 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2334 return NULL_RTX;
2336 arg = CALL_EXPR_ARG (exp, 0);
2338 switch (DECL_FUNCTION_CODE (fndecl))
2340 CASE_FLT_FN (BUILT_IN_SIN):
2341 CASE_FLT_FN (BUILT_IN_COS):
2342 builtin_optab = sincos_optab; break;
2343 default:
2344 gcc_unreachable ();
2347 /* Make a suitable register to place result in. */
2348 mode = TYPE_MODE (TREE_TYPE (exp));
2350 /* Check if sincos insn is available, otherwise fallback
2351 to sin or cos insn. */
2352 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2353 switch (DECL_FUNCTION_CODE (fndecl))
2355 CASE_FLT_FN (BUILT_IN_SIN):
2356 builtin_optab = sin_optab; break;
2357 CASE_FLT_FN (BUILT_IN_COS):
2358 builtin_optab = cos_optab; break;
2359 default:
2360 gcc_unreachable ();
2363 /* Before working hard, check whether the instruction is available. */
2364 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2366 rtx result = gen_reg_rtx (mode);
2368 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2369 need to expand the argument again. This way, we will not perform
2370 side-effects more the once. */
2371 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2373 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2375 start_sequence ();
2377 /* Compute into RESULT.
2378 Set RESULT to wherever the result comes back. */
2379 if (builtin_optab == sincos_optab)
2381 int ok;
2383 switch (DECL_FUNCTION_CODE (fndecl))
2385 CASE_FLT_FN (BUILT_IN_SIN):
2386 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2387 break;
2388 CASE_FLT_FN (BUILT_IN_COS):
2389 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2390 break;
2391 default:
2392 gcc_unreachable ();
2394 gcc_assert (ok);
2396 else
2397 result = expand_unop (mode, builtin_optab, op0, result, 0);
2399 if (result != 0)
2401 /* Output the entire sequence. */
2402 insns = get_insns ();
2403 end_sequence ();
2404 emit_insn (insns);
2405 return result;
2408 /* If we were unable to expand via the builtin, stop the sequence
2409 (without outputting the insns) and call to the library function
2410 with the stabilized argument list. */
2411 end_sequence ();
2414 return expand_call (exp, target, target == const0_rtx);
2417 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2418 return an RTL instruction code that implements the functionality.
2419 If that isn't possible or available return CODE_FOR_nothing. */
2421 static enum insn_code
2422 interclass_mathfn_icode (tree arg, tree fndecl)
2424 bool errno_set = false;
2425 optab builtin_optab = unknown_optab;
2426 enum machine_mode mode;
2428 switch (DECL_FUNCTION_CODE (fndecl))
2430 CASE_FLT_FN (BUILT_IN_ILOGB):
2431 errno_set = true; builtin_optab = ilogb_optab; break;
2432 CASE_FLT_FN (BUILT_IN_ISINF):
2433 builtin_optab = isinf_optab; break;
2434 case BUILT_IN_ISNORMAL:
2435 case BUILT_IN_ISFINITE:
2436 CASE_FLT_FN (BUILT_IN_FINITE):
2437 case BUILT_IN_FINITED32:
2438 case BUILT_IN_FINITED64:
2439 case BUILT_IN_FINITED128:
2440 case BUILT_IN_ISINFD32:
2441 case BUILT_IN_ISINFD64:
2442 case BUILT_IN_ISINFD128:
2443 /* These builtins have no optabs (yet). */
2444 break;
2445 default:
2446 gcc_unreachable ();
2449 /* There's no easy way to detect the case we need to set EDOM. */
2450 if (flag_errno_math && errno_set)
2451 return CODE_FOR_nothing;
2453 /* Optab mode depends on the mode of the input argument. */
2454 mode = TYPE_MODE (TREE_TYPE (arg));
2456 if (builtin_optab)
2457 return optab_handler (builtin_optab, mode);
2458 return CODE_FOR_nothing;
2461 /* Expand a call to one of the builtin math functions that operate on
2462 floating point argument and output an integer result (ilogb, isinf,
2463 isnan, etc).
2464 Return 0 if a normal call should be emitted rather than expanding the
2465 function in-line. EXP is the expression that is a call to the builtin
2466 function; if convenient, the result should be placed in TARGET. */
2468 static rtx
2469 expand_builtin_interclass_mathfn (tree exp, rtx target)
2471 enum insn_code icode = CODE_FOR_nothing;
2472 rtx op0;
2473 tree fndecl = get_callee_fndecl (exp);
2474 enum machine_mode mode;
2475 tree arg;
2477 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2478 return NULL_RTX;
2480 arg = CALL_EXPR_ARG (exp, 0);
2481 icode = interclass_mathfn_icode (arg, fndecl);
2482 mode = TYPE_MODE (TREE_TYPE (arg));
2484 if (icode != CODE_FOR_nothing)
2486 struct expand_operand ops[1];
2487 rtx last = get_last_insn ();
2488 tree orig_arg = arg;
2490 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2491 need to expand the argument again. This way, we will not perform
2492 side-effects more the once. */
2493 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2495 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2497 if (mode != GET_MODE (op0))
2498 op0 = convert_to_mode (mode, op0, 0);
2500 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2501 if (maybe_legitimize_operands (icode, 0, 1, ops)
2502 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2503 return ops[0].value;
2505 delete_insns_since (last);
2506 CALL_EXPR_ARG (exp, 0) = orig_arg;
2509 return NULL_RTX;
2512 /* Expand a call to the builtin sincos math function.
2513 Return NULL_RTX if a normal call should be emitted rather than expanding the
2514 function in-line. EXP is the expression that is a call to the builtin
2515 function. */
2517 static rtx
2518 expand_builtin_sincos (tree exp)
2520 rtx op0, op1, op2, target1, target2;
2521 enum machine_mode mode;
2522 tree arg, sinp, cosp;
2523 int result;
2524 location_t loc = EXPR_LOCATION (exp);
2525 tree alias_type, alias_off;
2527 if (!validate_arglist (exp, REAL_TYPE,
2528 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2529 return NULL_RTX;
2531 arg = CALL_EXPR_ARG (exp, 0);
2532 sinp = CALL_EXPR_ARG (exp, 1);
2533 cosp = CALL_EXPR_ARG (exp, 2);
2535 /* Make a suitable register to place result in. */
2536 mode = TYPE_MODE (TREE_TYPE (arg));
2538 /* Check if sincos insn is available, otherwise emit the call. */
2539 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2540 return NULL_RTX;
2542 target1 = gen_reg_rtx (mode);
2543 target2 = gen_reg_rtx (mode);
2545 op0 = expand_normal (arg);
2546 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2547 alias_off = build_int_cst (alias_type, 0);
2548 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2549 sinp, alias_off));
2550 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2551 cosp, alias_off));
2553 /* Compute into target1 and target2.
2554 Set TARGET to wherever the result comes back. */
2555 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2556 gcc_assert (result);
2558 /* Move target1 and target2 to the memory locations indicated
2559 by op1 and op2. */
2560 emit_move_insn (op1, target1);
2561 emit_move_insn (op2, target2);
2563 return const0_rtx;
2566 /* Expand a call to the internal cexpi builtin to the sincos math function.
2567 EXP is the expression that is a call to the builtin function; if convenient,
2568 the result should be placed in TARGET. */
2570 static rtx
2571 expand_builtin_cexpi (tree exp, rtx target)
2573 tree fndecl = get_callee_fndecl (exp);
2574 tree arg, type;
2575 enum machine_mode mode;
2576 rtx op0, op1, op2;
2577 location_t loc = EXPR_LOCATION (exp);
2579 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2580 return NULL_RTX;
2582 arg = CALL_EXPR_ARG (exp, 0);
2583 type = TREE_TYPE (arg);
2584 mode = TYPE_MODE (TREE_TYPE (arg));
2586 /* Try expanding via a sincos optab, fall back to emitting a libcall
2587 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2588 is only generated from sincos, cexp or if we have either of them. */
2589 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2591 op1 = gen_reg_rtx (mode);
2592 op2 = gen_reg_rtx (mode);
2594 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2596 /* Compute into op1 and op2. */
2597 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2599 else if (targetm.libc_has_function (function_sincos))
2601 tree call, fn = NULL_TREE;
2602 tree top1, top2;
2603 rtx op1a, op2a;
2605 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2606 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2607 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2608 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2609 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2610 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2611 else
2612 gcc_unreachable ();
2614 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2615 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2616 op1a = copy_addr_to_reg (XEXP (op1, 0));
2617 op2a = copy_addr_to_reg (XEXP (op2, 0));
2618 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2619 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2621 /* Make sure not to fold the sincos call again. */
2622 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2623 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2624 call, 3, arg, top1, top2));
2626 else
2628 tree call, fn = NULL_TREE, narg;
2629 tree ctype = build_complex_type (type);
2631 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2632 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2633 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2634 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2635 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2636 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2637 else
2638 gcc_unreachable ();
2640 /* If we don't have a decl for cexp create one. This is the
2641 friendliest fallback if the user calls __builtin_cexpi
2642 without full target C99 function support. */
2643 if (fn == NULL_TREE)
2645 tree fntype;
2646 const char *name = NULL;
2648 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2649 name = "cexpf";
2650 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2651 name = "cexp";
2652 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2653 name = "cexpl";
2655 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2656 fn = build_fn_decl (name, fntype);
2659 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2660 build_real (type, dconst0), arg);
2662 /* Make sure not to fold the cexp call again. */
2663 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2664 return expand_expr (build_call_nary (ctype, call, 1, narg),
2665 target, VOIDmode, EXPAND_NORMAL);
2668 /* Now build the proper return type. */
2669 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2670 make_tree (TREE_TYPE (arg), op2),
2671 make_tree (TREE_TYPE (arg), op1)),
2672 target, VOIDmode, EXPAND_NORMAL);
2675 /* Conveniently construct a function call expression. FNDECL names the
2676 function to be called, N is the number of arguments, and the "..."
2677 parameters are the argument expressions. Unlike build_call_exr
2678 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2680 static tree
2681 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2683 va_list ap;
2684 tree fntype = TREE_TYPE (fndecl);
2685 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2687 va_start (ap, n);
2688 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2689 va_end (ap);
2690 SET_EXPR_LOCATION (fn, loc);
2691 return fn;
2694 /* Expand a call to one of the builtin rounding functions gcc defines
2695 as an extension (lfloor and lceil). As these are gcc extensions we
2696 do not need to worry about setting errno to EDOM.
2697 If expanding via optab fails, lower expression to (int)(floor(x)).
2698 EXP is the expression that is a call to the builtin function;
2699 if convenient, the result should be placed in TARGET. */
2701 static rtx
2702 expand_builtin_int_roundingfn (tree exp, rtx target)
2704 convert_optab builtin_optab;
2705 rtx op0, insns, tmp;
2706 tree fndecl = get_callee_fndecl (exp);
2707 enum built_in_function fallback_fn;
2708 tree fallback_fndecl;
2709 enum 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, insns;
2842 tree fndecl = get_callee_fndecl (exp);
2843 tree arg;
2844 enum machine_mode mode;
2845 enum built_in_function fallback_fn = BUILT_IN_NONE;
2847 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2848 gcc_unreachable ();
2850 arg = CALL_EXPR_ARG (exp, 0);
2852 switch (DECL_FUNCTION_CODE (fndecl))
2854 CASE_FLT_FN (BUILT_IN_IRINT):
2855 fallback_fn = BUILT_IN_LRINT;
2856 /* FALLTHRU */
2857 CASE_FLT_FN (BUILT_IN_LRINT):
2858 CASE_FLT_FN (BUILT_IN_LLRINT):
2859 builtin_optab = lrint_optab;
2860 break;
2862 CASE_FLT_FN (BUILT_IN_IROUND):
2863 fallback_fn = BUILT_IN_LROUND;
2864 /* FALLTHRU */
2865 CASE_FLT_FN (BUILT_IN_LROUND):
2866 CASE_FLT_FN (BUILT_IN_LLROUND):
2867 builtin_optab = lround_optab;
2868 break;
2870 default:
2871 gcc_unreachable ();
2874 /* There's no easy way to detect the case we need to set EDOM. */
2875 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2876 return NULL_RTX;
2878 /* Make a suitable register to place result in. */
2879 mode = TYPE_MODE (TREE_TYPE (exp));
2881 /* There's no easy way to detect the case we need to set EDOM. */
2882 if (!flag_errno_math)
2884 rtx result = gen_reg_rtx (mode);
2886 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2887 need to expand the argument again. This way, we will not perform
2888 side-effects more the once. */
2889 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2891 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2893 start_sequence ();
2895 if (expand_sfix_optab (result, op0, builtin_optab))
2897 /* Output the entire sequence. */
2898 insns = get_insns ();
2899 end_sequence ();
2900 emit_insn (insns);
2901 return result;
2904 /* If we were unable to expand via the builtin, stop the sequence
2905 (without outputting the insns) and call to the library function
2906 with the stabilized argument list. */
2907 end_sequence ();
2910 if (fallback_fn != BUILT_IN_NONE)
2912 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2913 targets, (int) round (x) should never be transformed into
2914 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2915 a call to lround in the hope that the target provides at least some
2916 C99 functions. This should result in the best user experience for
2917 not full C99 targets. */
2918 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2919 fallback_fn, 0);
2921 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2922 fallback_fndecl, 1, arg);
2924 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2925 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2926 return convert_to_mode (mode, target, 0);
2929 return expand_call (exp, target, target == const0_rtx);
2932 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2933 a normal call should be emitted rather than expanding the function
2934 in-line. EXP is the expression that is a call to the builtin
2935 function; if convenient, the result should be placed in TARGET. */
2937 static rtx
2938 expand_builtin_powi (tree exp, rtx target)
2940 tree arg0, arg1;
2941 rtx op0, op1;
2942 enum machine_mode mode;
2943 enum machine_mode mode2;
2945 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2946 return NULL_RTX;
2948 arg0 = CALL_EXPR_ARG (exp, 0);
2949 arg1 = CALL_EXPR_ARG (exp, 1);
2950 mode = TYPE_MODE (TREE_TYPE (exp));
2952 /* Emit a libcall to libgcc. */
2954 /* Mode of the 2nd argument must match that of an int. */
2955 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2957 if (target == NULL_RTX)
2958 target = gen_reg_rtx (mode);
2960 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2961 if (GET_MODE (op0) != mode)
2962 op0 = convert_to_mode (mode, op0, 0);
2963 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2964 if (GET_MODE (op1) != mode2)
2965 op1 = convert_to_mode (mode2, op1, 0);
2967 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2968 target, LCT_CONST, mode, 2,
2969 op0, mode, op1, mode2);
2971 return target;
2974 /* Expand expression EXP which is a call to the strlen builtin. Return
2975 NULL_RTX if we failed the caller should emit a normal call, otherwise
2976 try to get the result in TARGET, if convenient. */
2978 static rtx
2979 expand_builtin_strlen (tree exp, rtx target,
2980 enum machine_mode target_mode)
2982 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2983 return NULL_RTX;
2984 else
2986 struct expand_operand ops[4];
2987 rtx pat;
2988 tree len;
2989 tree src = CALL_EXPR_ARG (exp, 0);
2990 rtx src_reg, before_strlen;
2991 enum machine_mode insn_mode = target_mode;
2992 enum insn_code icode = CODE_FOR_nothing;
2993 unsigned int align;
2995 /* If the length can be computed at compile-time, return it. */
2996 len = c_strlen (src, 0);
2997 if (len)
2998 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3000 /* If the length can be computed at compile-time and is constant
3001 integer, but there are side-effects in src, evaluate
3002 src for side-effects, then return len.
3003 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3004 can be optimized into: i++; x = 3; */
3005 len = c_strlen (src, 1);
3006 if (len && TREE_CODE (len) == INTEGER_CST)
3008 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3009 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3012 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3014 /* If SRC is not a pointer type, don't do this operation inline. */
3015 if (align == 0)
3016 return NULL_RTX;
3018 /* Bail out if we can't compute strlen in the right mode. */
3019 while (insn_mode != VOIDmode)
3021 icode = optab_handler (strlen_optab, insn_mode);
3022 if (icode != CODE_FOR_nothing)
3023 break;
3025 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3027 if (insn_mode == VOIDmode)
3028 return NULL_RTX;
3030 /* Make a place to hold the source address. We will not expand
3031 the actual source until we are sure that the expansion will
3032 not fail -- there are trees that cannot be expanded twice. */
3033 src_reg = gen_reg_rtx (Pmode);
3035 /* Mark the beginning of the strlen sequence so we can emit the
3036 source operand later. */
3037 before_strlen = get_last_insn ();
3039 create_output_operand (&ops[0], target, insn_mode);
3040 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3041 create_integer_operand (&ops[2], 0);
3042 create_integer_operand (&ops[3], align);
3043 if (!maybe_expand_insn (icode, 4, ops))
3044 return NULL_RTX;
3046 /* Now that we are assured of success, expand the source. */
3047 start_sequence ();
3048 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3049 if (pat != src_reg)
3051 #ifdef POINTERS_EXTEND_UNSIGNED
3052 if (GET_MODE (pat) != Pmode)
3053 pat = convert_to_mode (Pmode, pat,
3054 POINTERS_EXTEND_UNSIGNED);
3055 #endif
3056 emit_move_insn (src_reg, pat);
3058 pat = get_insns ();
3059 end_sequence ();
3061 if (before_strlen)
3062 emit_insn_after (pat, before_strlen);
3063 else
3064 emit_insn_before (pat, get_insns ());
3066 /* Return the value in the proper mode for this function. */
3067 if (GET_MODE (ops[0].value) == target_mode)
3068 target = ops[0].value;
3069 else if (target != 0)
3070 convert_move (target, ops[0].value, 0);
3071 else
3072 target = convert_to_mode (target_mode, ops[0].value, 0);
3074 return target;
3078 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3079 bytes from constant string DATA + OFFSET and return it as target
3080 constant. */
3082 static rtx
3083 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3084 enum machine_mode mode)
3086 const char *str = (const char *) data;
3088 gcc_assert (offset >= 0
3089 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3090 <= strlen (str) + 1));
3092 return c_readstr (str + offset, mode);
3095 /* LEN specify length of the block of memcpy/memset operation.
3096 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3097 In some cases we can make very likely guess on max size, then we
3098 set it into PROBABLE_MAX_SIZE. */
3100 static void
3101 determine_block_size (tree len, rtx len_rtx,
3102 unsigned HOST_WIDE_INT *min_size,
3103 unsigned HOST_WIDE_INT *max_size,
3104 unsigned HOST_WIDE_INT *probable_max_size)
3106 if (CONST_INT_P (len_rtx))
3108 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3109 return;
3111 else
3113 wide_int min, max;
3114 enum value_range_type range_type = VR_UNDEFINED;
3116 /* Determine bounds from the type. */
3117 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3118 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3119 else
3120 *min_size = 0;
3121 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3122 *probable_max_size = *max_size
3123 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3124 else
3125 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3127 if (TREE_CODE (len) == SSA_NAME)
3128 range_type = get_range_info (len, &min, &max);
3129 if (range_type == VR_RANGE)
3131 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3132 *min_size = min.to_uhwi ();
3133 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3134 *probable_max_size = *max_size = max.to_uhwi ();
3136 else if (range_type == VR_ANTI_RANGE)
3138 /* Anti range 0...N lets us to determine minimal size to N+1. */
3139 if (min == 0)
3141 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
3142 *min_size = max.to_uhwi () + 1;
3144 /* Code like
3146 int n;
3147 if (n < 100)
3148 memcpy (a, b, n)
3150 Produce anti range allowing negative values of N. We still
3151 can use the information and make a guess that N is not negative.
3153 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3154 *probable_max_size = min.to_uhwi () - 1;
3157 gcc_checking_assert (*max_size <=
3158 (unsigned HOST_WIDE_INT)
3159 GET_MODE_MASK (GET_MODE (len_rtx)));
3162 /* Expand a call EXP to the memcpy builtin.
3163 Return NULL_RTX if we failed, the caller should emit a normal call,
3164 otherwise try to get the result in TARGET, if convenient (and in
3165 mode MODE if that's convenient). */
3167 static rtx
3168 expand_builtin_memcpy (tree exp, rtx target)
3170 if (!validate_arglist (exp,
3171 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3172 return NULL_RTX;
3173 else
3175 tree dest = CALL_EXPR_ARG (exp, 0);
3176 tree src = CALL_EXPR_ARG (exp, 1);
3177 tree len = CALL_EXPR_ARG (exp, 2);
3178 const char *src_str;
3179 unsigned int src_align = get_pointer_alignment (src);
3180 unsigned int dest_align = get_pointer_alignment (dest);
3181 rtx dest_mem, src_mem, dest_addr, len_rtx;
3182 HOST_WIDE_INT expected_size = -1;
3183 unsigned int expected_align = 0;
3184 unsigned HOST_WIDE_INT min_size;
3185 unsigned HOST_WIDE_INT max_size;
3186 unsigned HOST_WIDE_INT probable_max_size;
3188 /* If DEST is not a pointer type, call the normal function. */
3189 if (dest_align == 0)
3190 return NULL_RTX;
3192 /* If either SRC is not a pointer type, don't do this
3193 operation in-line. */
3194 if (src_align == 0)
3195 return NULL_RTX;
3197 if (currently_expanding_gimple_stmt)
3198 stringop_block_profile (currently_expanding_gimple_stmt,
3199 &expected_align, &expected_size);
3201 if (expected_align < dest_align)
3202 expected_align = dest_align;
3203 dest_mem = get_memory_rtx (dest, len);
3204 set_mem_align (dest_mem, dest_align);
3205 len_rtx = expand_normal (len);
3206 determine_block_size (len, len_rtx, &min_size, &max_size,
3207 &probable_max_size);
3208 src_str = c_getstr (src);
3210 /* If SRC is a string constant and block move would be done
3211 by pieces, we can avoid loading the string from memory
3212 and only stored the computed constants. */
3213 if (src_str
3214 && CONST_INT_P (len_rtx)
3215 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3216 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3217 CONST_CAST (char *, src_str),
3218 dest_align, false))
3220 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3221 builtin_memcpy_read_str,
3222 CONST_CAST (char *, src_str),
3223 dest_align, false, 0);
3224 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3225 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3226 return dest_mem;
3229 src_mem = get_memory_rtx (src, len);
3230 set_mem_align (src_mem, src_align);
3232 /* Copy word part most expediently. */
3233 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3234 CALL_EXPR_TAILCALL (exp)
3235 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3236 expected_align, expected_size,
3237 min_size, max_size, probable_max_size);
3239 if (dest_addr == 0)
3241 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3242 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3244 return dest_addr;
3248 /* Expand a call EXP to the mempcpy builtin.
3249 Return NULL_RTX if we failed; the caller should emit a normal call,
3250 otherwise try to get the result in TARGET, if convenient (and in
3251 mode MODE if that's convenient). If ENDP is 0 return the
3252 destination pointer, if ENDP is 1 return the end pointer ala
3253 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3254 stpcpy. */
3256 static rtx
3257 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3259 if (!validate_arglist (exp,
3260 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3261 return NULL_RTX;
3262 else
3264 tree dest = CALL_EXPR_ARG (exp, 0);
3265 tree src = CALL_EXPR_ARG (exp, 1);
3266 tree len = CALL_EXPR_ARG (exp, 2);
3267 return expand_builtin_mempcpy_args (dest, src, len,
3268 target, mode, /*endp=*/ 1);
3272 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3273 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3274 so that this can also be called without constructing an actual CALL_EXPR.
3275 The other arguments and return value are the same as for
3276 expand_builtin_mempcpy. */
3278 static rtx
3279 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3280 rtx target, enum machine_mode mode, int endp)
3282 /* If return value is ignored, transform mempcpy into memcpy. */
3283 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3285 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3286 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3287 dest, src, len);
3288 return expand_expr (result, target, mode, EXPAND_NORMAL);
3290 else
3292 const char *src_str;
3293 unsigned int src_align = get_pointer_alignment (src);
3294 unsigned int dest_align = get_pointer_alignment (dest);
3295 rtx dest_mem, src_mem, len_rtx;
3297 /* If either SRC or DEST is not a pointer type, don't do this
3298 operation in-line. */
3299 if (dest_align == 0 || src_align == 0)
3300 return NULL_RTX;
3302 /* If LEN is not constant, call the normal function. */
3303 if (! tree_fits_uhwi_p (len))
3304 return NULL_RTX;
3306 len_rtx = expand_normal (len);
3307 src_str = c_getstr (src);
3309 /* If SRC is a string constant and block move would be done
3310 by pieces, we can avoid loading the string from memory
3311 and only stored the computed constants. */
3312 if (src_str
3313 && CONST_INT_P (len_rtx)
3314 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3315 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3316 CONST_CAST (char *, src_str),
3317 dest_align, false))
3319 dest_mem = get_memory_rtx (dest, len);
3320 set_mem_align (dest_mem, dest_align);
3321 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3322 builtin_memcpy_read_str,
3323 CONST_CAST (char *, src_str),
3324 dest_align, false, endp);
3325 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3326 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3327 return dest_mem;
3330 if (CONST_INT_P (len_rtx)
3331 && can_move_by_pieces (INTVAL (len_rtx),
3332 MIN (dest_align, src_align)))
3334 dest_mem = get_memory_rtx (dest, len);
3335 set_mem_align (dest_mem, dest_align);
3336 src_mem = get_memory_rtx (src, len);
3337 set_mem_align (src_mem, src_align);
3338 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3339 MIN (dest_align, src_align), endp);
3340 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3341 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3342 return dest_mem;
3345 return NULL_RTX;
3349 #ifndef HAVE_movstr
3350 # define HAVE_movstr 0
3351 # define CODE_FOR_movstr CODE_FOR_nothing
3352 #endif
3354 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3355 we failed, the caller should emit a normal call, otherwise try to
3356 get the result in TARGET, if convenient. If ENDP is 0 return the
3357 destination pointer, if ENDP is 1 return the end pointer ala
3358 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3359 stpcpy. */
3361 static rtx
3362 expand_movstr (tree dest, tree src, rtx target, int endp)
3364 struct expand_operand ops[3];
3365 rtx dest_mem;
3366 rtx src_mem;
3368 if (!HAVE_movstr)
3369 return NULL_RTX;
3371 dest_mem = get_memory_rtx (dest, NULL);
3372 src_mem = get_memory_rtx (src, NULL);
3373 if (!endp)
3375 target = force_reg (Pmode, XEXP (dest_mem, 0));
3376 dest_mem = replace_equiv_address (dest_mem, target);
3379 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3380 create_fixed_operand (&ops[1], dest_mem);
3381 create_fixed_operand (&ops[2], src_mem);
3382 if (!maybe_expand_insn (CODE_FOR_movstr, 3, ops))
3383 return NULL_RTX;
3385 if (endp && target != const0_rtx)
3387 target = ops[0].value;
3388 /* movstr is supposed to set end to the address of the NUL
3389 terminator. If the caller requested a mempcpy-like return value,
3390 adjust it. */
3391 if (endp == 1)
3393 rtx tem = plus_constant (GET_MODE (target),
3394 gen_lowpart (GET_MODE (target), target), 1);
3395 emit_move_insn (target, force_operand (tem, NULL_RTX));
3398 return target;
3401 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3402 NULL_RTX if we failed the caller should emit a normal call, otherwise
3403 try to get the result in TARGET, if convenient (and in mode MODE if that's
3404 convenient). */
3406 static rtx
3407 expand_builtin_strcpy (tree exp, rtx target)
3409 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3411 tree dest = CALL_EXPR_ARG (exp, 0);
3412 tree src = CALL_EXPR_ARG (exp, 1);
3413 return expand_builtin_strcpy_args (dest, src, target);
3415 return NULL_RTX;
3418 /* Helper function to do the actual work for expand_builtin_strcpy. The
3419 arguments to the builtin_strcpy call DEST and SRC are broken out
3420 so that this can also be called without constructing an actual CALL_EXPR.
3421 The other arguments and return value are the same as for
3422 expand_builtin_strcpy. */
3424 static rtx
3425 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3427 return expand_movstr (dest, src, target, /*endp=*/0);
3430 /* Expand a call EXP to the stpcpy builtin.
3431 Return NULL_RTX if we failed the caller should emit a normal call,
3432 otherwise try to get the result in TARGET, if convenient (and in
3433 mode MODE if that's convenient). */
3435 static rtx
3436 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3438 tree dst, src;
3439 location_t loc = EXPR_LOCATION (exp);
3441 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3442 return NULL_RTX;
3444 dst = CALL_EXPR_ARG (exp, 0);
3445 src = CALL_EXPR_ARG (exp, 1);
3447 /* If return value is ignored, transform stpcpy into strcpy. */
3448 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3450 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3451 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3452 return expand_expr (result, target, mode, EXPAND_NORMAL);
3454 else
3456 tree len, lenp1;
3457 rtx ret;
3459 /* Ensure we get an actual string whose length can be evaluated at
3460 compile-time, not an expression containing a string. This is
3461 because the latter will potentially produce pessimized code
3462 when used to produce the return value. */
3463 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3464 return expand_movstr (dst, src, target, /*endp=*/2);
3466 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3467 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3468 target, mode, /*endp=*/2);
3470 if (ret)
3471 return ret;
3473 if (TREE_CODE (len) == INTEGER_CST)
3475 rtx len_rtx = expand_normal (len);
3477 if (CONST_INT_P (len_rtx))
3479 ret = expand_builtin_strcpy_args (dst, src, target);
3481 if (ret)
3483 if (! target)
3485 if (mode != VOIDmode)
3486 target = gen_reg_rtx (mode);
3487 else
3488 target = gen_reg_rtx (GET_MODE (ret));
3490 if (GET_MODE (target) != GET_MODE (ret))
3491 ret = gen_lowpart (GET_MODE (target), ret);
3493 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3494 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3495 gcc_assert (ret);
3497 return target;
3502 return expand_movstr (dst, src, target, /*endp=*/2);
3506 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3507 bytes from constant string DATA + OFFSET and return it as target
3508 constant. */
3511 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3512 enum machine_mode mode)
3514 const char *str = (const char *) data;
3516 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3517 return const0_rtx;
3519 return c_readstr (str + offset, mode);
3522 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3523 NULL_RTX if we failed the caller should emit a normal call. */
3525 static rtx
3526 expand_builtin_strncpy (tree exp, rtx target)
3528 location_t loc = EXPR_LOCATION (exp);
3530 if (validate_arglist (exp,
3531 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3533 tree dest = CALL_EXPR_ARG (exp, 0);
3534 tree src = CALL_EXPR_ARG (exp, 1);
3535 tree len = CALL_EXPR_ARG (exp, 2);
3536 tree slen = c_strlen (src, 1);
3538 /* We must be passed a constant len and src parameter. */
3539 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3540 return NULL_RTX;
3542 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3544 /* We're required to pad with trailing zeros if the requested
3545 len is greater than strlen(s2)+1. In that case try to
3546 use store_by_pieces, if it fails, punt. */
3547 if (tree_int_cst_lt (slen, len))
3549 unsigned int dest_align = get_pointer_alignment (dest);
3550 const char *p = c_getstr (src);
3551 rtx dest_mem;
3553 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3554 || !can_store_by_pieces (tree_to_uhwi (len),
3555 builtin_strncpy_read_str,
3556 CONST_CAST (char *, p),
3557 dest_align, false))
3558 return NULL_RTX;
3560 dest_mem = get_memory_rtx (dest, len);
3561 store_by_pieces (dest_mem, tree_to_uhwi (len),
3562 builtin_strncpy_read_str,
3563 CONST_CAST (char *, p), dest_align, false, 0);
3564 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3565 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3566 return dest_mem;
3569 return NULL_RTX;
3572 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3573 bytes from constant string DATA + OFFSET and return it as target
3574 constant. */
3577 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3578 enum machine_mode mode)
3580 const char *c = (const char *) data;
3581 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3583 memset (p, *c, GET_MODE_SIZE (mode));
3585 return c_readstr (p, mode);
3588 /* Callback routine for store_by_pieces. Return the RTL of a register
3589 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3590 char value given in the RTL register data. For example, if mode is
3591 4 bytes wide, return the RTL for 0x01010101*data. */
3593 static rtx
3594 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3595 enum machine_mode mode)
3597 rtx target, coeff;
3598 size_t size;
3599 char *p;
3601 size = GET_MODE_SIZE (mode);
3602 if (size == 1)
3603 return (rtx) data;
3605 p = XALLOCAVEC (char, size);
3606 memset (p, 1, size);
3607 coeff = c_readstr (p, mode);
3609 target = convert_to_mode (mode, (rtx) data, 1);
3610 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3611 return force_reg (mode, target);
3614 /* Expand expression EXP, which is a call to the memset builtin. Return
3615 NULL_RTX if we failed the caller should emit a normal call, otherwise
3616 try to get the result in TARGET, if convenient (and in mode MODE if that's
3617 convenient). */
3619 static rtx
3620 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3622 if (!validate_arglist (exp,
3623 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3624 return NULL_RTX;
3625 else
3627 tree dest = CALL_EXPR_ARG (exp, 0);
3628 tree val = CALL_EXPR_ARG (exp, 1);
3629 tree len = CALL_EXPR_ARG (exp, 2);
3630 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3634 /* Helper function to do the actual work for expand_builtin_memset. The
3635 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3636 so that this can also be called without constructing an actual CALL_EXPR.
3637 The other arguments and return value are the same as for
3638 expand_builtin_memset. */
3640 static rtx
3641 expand_builtin_memset_args (tree dest, tree val, tree len,
3642 rtx target, enum machine_mode mode, tree orig_exp)
3644 tree fndecl, fn;
3645 enum built_in_function fcode;
3646 enum machine_mode val_mode;
3647 char c;
3648 unsigned int dest_align;
3649 rtx dest_mem, dest_addr, len_rtx;
3650 HOST_WIDE_INT expected_size = -1;
3651 unsigned int expected_align = 0;
3652 unsigned HOST_WIDE_INT min_size;
3653 unsigned HOST_WIDE_INT max_size;
3654 unsigned HOST_WIDE_INT probable_max_size;
3656 dest_align = get_pointer_alignment (dest);
3658 /* If DEST is not a pointer type, don't do this operation in-line. */
3659 if (dest_align == 0)
3660 return NULL_RTX;
3662 if (currently_expanding_gimple_stmt)
3663 stringop_block_profile (currently_expanding_gimple_stmt,
3664 &expected_align, &expected_size);
3666 if (expected_align < dest_align)
3667 expected_align = dest_align;
3669 /* If the LEN parameter is zero, return DEST. */
3670 if (integer_zerop (len))
3672 /* Evaluate and ignore VAL in case it has side-effects. */
3673 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3674 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3677 /* Stabilize the arguments in case we fail. */
3678 dest = builtin_save_expr (dest);
3679 val = builtin_save_expr (val);
3680 len = builtin_save_expr (len);
3682 len_rtx = expand_normal (len);
3683 determine_block_size (len, len_rtx, &min_size, &max_size,
3684 &probable_max_size);
3685 dest_mem = get_memory_rtx (dest, len);
3686 val_mode = TYPE_MODE (unsigned_char_type_node);
3688 if (TREE_CODE (val) != INTEGER_CST)
3690 rtx val_rtx;
3692 val_rtx = expand_normal (val);
3693 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3695 /* Assume that we can memset by pieces if we can store
3696 * the coefficients by pieces (in the required modes).
3697 * We can't pass builtin_memset_gen_str as that emits RTL. */
3698 c = 1;
3699 if (tree_fits_uhwi_p (len)
3700 && can_store_by_pieces (tree_to_uhwi (len),
3701 builtin_memset_read_str, &c, dest_align,
3702 true))
3704 val_rtx = force_reg (val_mode, val_rtx);
3705 store_by_pieces (dest_mem, tree_to_uhwi (len),
3706 builtin_memset_gen_str, val_rtx, dest_align,
3707 true, 0);
3709 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3710 dest_align, expected_align,
3711 expected_size, min_size, max_size,
3712 probable_max_size))
3713 goto do_libcall;
3715 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3716 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3717 return dest_mem;
3720 if (target_char_cast (val, &c))
3721 goto do_libcall;
3723 if (c)
3725 if (tree_fits_uhwi_p (len)
3726 && can_store_by_pieces (tree_to_uhwi (len),
3727 builtin_memset_read_str, &c, dest_align,
3728 true))
3729 store_by_pieces (dest_mem, tree_to_uhwi (len),
3730 builtin_memset_read_str, &c, dest_align, true, 0);
3731 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3732 gen_int_mode (c, val_mode),
3733 dest_align, expected_align,
3734 expected_size, min_size, max_size,
3735 probable_max_size))
3736 goto do_libcall;
3738 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3739 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3740 return dest_mem;
3743 set_mem_align (dest_mem, dest_align);
3744 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3745 CALL_EXPR_TAILCALL (orig_exp)
3746 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3747 expected_align, expected_size,
3748 min_size, max_size,
3749 probable_max_size);
3751 if (dest_addr == 0)
3753 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3754 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3757 return dest_addr;
3759 do_libcall:
3760 fndecl = get_callee_fndecl (orig_exp);
3761 fcode = DECL_FUNCTION_CODE (fndecl);
3762 if (fcode == BUILT_IN_MEMSET)
3763 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3764 dest, val, len);
3765 else if (fcode == BUILT_IN_BZERO)
3766 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3767 dest, len);
3768 else
3769 gcc_unreachable ();
3770 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3771 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3772 return expand_call (fn, target, target == const0_rtx);
3775 /* Expand expression EXP, which is a call to the bzero builtin. Return
3776 NULL_RTX if we failed the caller should emit a normal call. */
3778 static rtx
3779 expand_builtin_bzero (tree exp)
3781 tree dest, size;
3782 location_t loc = EXPR_LOCATION (exp);
3784 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3785 return NULL_RTX;
3787 dest = CALL_EXPR_ARG (exp, 0);
3788 size = CALL_EXPR_ARG (exp, 1);
3790 /* New argument list transforming bzero(ptr x, int y) to
3791 memset(ptr x, int 0, size_t y). This is done this way
3792 so that if it isn't expanded inline, we fallback to
3793 calling bzero instead of memset. */
3795 return expand_builtin_memset_args (dest, integer_zero_node,
3796 fold_convert_loc (loc,
3797 size_type_node, size),
3798 const0_rtx, VOIDmode, exp);
3801 /* Expand expression EXP, which is a call to the memcmp built-in function.
3802 Return NULL_RTX if we failed and the caller should emit a normal call,
3803 otherwise try to get the result in TARGET, if convenient (and in mode
3804 MODE, if that's convenient). */
3806 static rtx
3807 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3808 ATTRIBUTE_UNUSED enum machine_mode mode)
3810 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3812 if (!validate_arglist (exp,
3813 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3814 return NULL_RTX;
3816 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3817 implementing memcmp because it will stop if it encounters two
3818 zero bytes. */
3819 #if defined HAVE_cmpmemsi
3821 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3822 rtx result;
3823 rtx insn;
3824 tree arg1 = CALL_EXPR_ARG (exp, 0);
3825 tree arg2 = CALL_EXPR_ARG (exp, 1);
3826 tree len = CALL_EXPR_ARG (exp, 2);
3828 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3829 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3830 enum machine_mode insn_mode;
3832 if (HAVE_cmpmemsi)
3833 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3834 else
3835 return NULL_RTX;
3837 /* If we don't have POINTER_TYPE, call the function. */
3838 if (arg1_align == 0 || arg2_align == 0)
3839 return NULL_RTX;
3841 /* Make a place to write the result of the instruction. */
3842 result = target;
3843 if (! (result != 0
3844 && REG_P (result) && GET_MODE (result) == insn_mode
3845 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3846 result = gen_reg_rtx (insn_mode);
3848 arg1_rtx = get_memory_rtx (arg1, len);
3849 arg2_rtx = get_memory_rtx (arg2, len);
3850 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3852 /* Set MEM_SIZE as appropriate. */
3853 if (CONST_INT_P (arg3_rtx))
3855 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3856 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3859 if (HAVE_cmpmemsi)
3860 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3861 GEN_INT (MIN (arg1_align, arg2_align)));
3862 else
3863 gcc_unreachable ();
3865 if (insn)
3866 emit_insn (insn);
3867 else
3868 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3869 TYPE_MODE (integer_type_node), 3,
3870 XEXP (arg1_rtx, 0), Pmode,
3871 XEXP (arg2_rtx, 0), Pmode,
3872 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3873 TYPE_UNSIGNED (sizetype)),
3874 TYPE_MODE (sizetype));
3876 /* Return the value in the proper mode for this function. */
3877 mode = TYPE_MODE (TREE_TYPE (exp));
3878 if (GET_MODE (result) == mode)
3879 return result;
3880 else if (target != 0)
3882 convert_move (target, result, 0);
3883 return target;
3885 else
3886 return convert_to_mode (mode, result, 0);
3888 #endif /* HAVE_cmpmemsi. */
3890 return NULL_RTX;
3893 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3894 if we failed the caller should emit a normal call, otherwise try to get
3895 the result in TARGET, if convenient. */
3897 static rtx
3898 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3900 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3901 return NULL_RTX;
3903 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3904 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3905 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3907 rtx arg1_rtx, arg2_rtx;
3908 rtx result, insn = NULL_RTX;
3909 tree fndecl, fn;
3910 tree arg1 = CALL_EXPR_ARG (exp, 0);
3911 tree arg2 = CALL_EXPR_ARG (exp, 1);
3913 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3914 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3916 /* If we don't have POINTER_TYPE, call the function. */
3917 if (arg1_align == 0 || arg2_align == 0)
3918 return NULL_RTX;
3920 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3921 arg1 = builtin_save_expr (arg1);
3922 arg2 = builtin_save_expr (arg2);
3924 arg1_rtx = get_memory_rtx (arg1, NULL);
3925 arg2_rtx = get_memory_rtx (arg2, NULL);
3927 #ifdef HAVE_cmpstrsi
3928 /* Try to call cmpstrsi. */
3929 if (HAVE_cmpstrsi)
3931 enum machine_mode insn_mode
3932 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3934 /* Make a place to write the result of the instruction. */
3935 result = target;
3936 if (! (result != 0
3937 && REG_P (result) && GET_MODE (result) == insn_mode
3938 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3939 result = gen_reg_rtx (insn_mode);
3941 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3942 GEN_INT (MIN (arg1_align, arg2_align)));
3944 #endif
3945 #ifdef HAVE_cmpstrnsi
3946 /* Try to determine at least one length and call cmpstrnsi. */
3947 if (!insn && HAVE_cmpstrnsi)
3949 tree len;
3950 rtx arg3_rtx;
3952 enum machine_mode insn_mode
3953 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3954 tree len1 = c_strlen (arg1, 1);
3955 tree len2 = c_strlen (arg2, 1);
3957 if (len1)
3958 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3959 if (len2)
3960 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3962 /* If we don't have a constant length for the first, use the length
3963 of the second, if we know it. We don't require a constant for
3964 this case; some cost analysis could be done if both are available
3965 but neither is constant. For now, assume they're equally cheap,
3966 unless one has side effects. If both strings have constant lengths,
3967 use the smaller. */
3969 if (!len1)
3970 len = len2;
3971 else if (!len2)
3972 len = len1;
3973 else if (TREE_SIDE_EFFECTS (len1))
3974 len = len2;
3975 else if (TREE_SIDE_EFFECTS (len2))
3976 len = len1;
3977 else if (TREE_CODE (len1) != INTEGER_CST)
3978 len = len2;
3979 else if (TREE_CODE (len2) != INTEGER_CST)
3980 len = len1;
3981 else if (tree_int_cst_lt (len1, len2))
3982 len = len1;
3983 else
3984 len = len2;
3986 /* If both arguments have side effects, we cannot optimize. */
3987 if (!len || TREE_SIDE_EFFECTS (len))
3988 goto do_libcall;
3990 arg3_rtx = expand_normal (len);
3992 /* Make a place to write the result of the instruction. */
3993 result = target;
3994 if (! (result != 0
3995 && REG_P (result) && GET_MODE (result) == insn_mode
3996 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3997 result = gen_reg_rtx (insn_mode);
3999 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4000 GEN_INT (MIN (arg1_align, arg2_align)));
4002 #endif
4004 if (insn)
4006 enum machine_mode mode;
4007 emit_insn (insn);
4009 /* Return the value in the proper mode for this function. */
4010 mode = TYPE_MODE (TREE_TYPE (exp));
4011 if (GET_MODE (result) == mode)
4012 return result;
4013 if (target == 0)
4014 return convert_to_mode (mode, result, 0);
4015 convert_move (target, result, 0);
4016 return target;
4019 /* Expand the library call ourselves using a stabilized argument
4020 list to avoid re-evaluating the function's arguments twice. */
4021 #ifdef HAVE_cmpstrnsi
4022 do_libcall:
4023 #endif
4024 fndecl = get_callee_fndecl (exp);
4025 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4026 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4027 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4028 return expand_call (fn, target, target == const0_rtx);
4030 #endif
4031 return NULL_RTX;
4034 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4035 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4036 the result in TARGET, if convenient. */
4038 static rtx
4039 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4040 ATTRIBUTE_UNUSED enum machine_mode mode)
4042 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4044 if (!validate_arglist (exp,
4045 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4046 return NULL_RTX;
4048 /* If c_strlen can determine an expression for one of the string
4049 lengths, and it doesn't have side effects, then emit cmpstrnsi
4050 using length MIN(strlen(string)+1, arg3). */
4051 #ifdef HAVE_cmpstrnsi
4052 if (HAVE_cmpstrnsi)
4054 tree len, len1, len2;
4055 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4056 rtx result, insn;
4057 tree fndecl, fn;
4058 tree arg1 = CALL_EXPR_ARG (exp, 0);
4059 tree arg2 = CALL_EXPR_ARG (exp, 1);
4060 tree arg3 = CALL_EXPR_ARG (exp, 2);
4062 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4063 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4064 enum machine_mode insn_mode
4065 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4067 len1 = c_strlen (arg1, 1);
4068 len2 = c_strlen (arg2, 1);
4070 if (len1)
4071 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4072 if (len2)
4073 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4075 /* If we don't have a constant length for the first, use the length
4076 of the second, if we know it. We don't require a constant for
4077 this case; some cost analysis could be done if both are available
4078 but neither is constant. For now, assume they're equally cheap,
4079 unless one has side effects. If both strings have constant lengths,
4080 use the smaller. */
4082 if (!len1)
4083 len = len2;
4084 else if (!len2)
4085 len = len1;
4086 else if (TREE_SIDE_EFFECTS (len1))
4087 len = len2;
4088 else if (TREE_SIDE_EFFECTS (len2))
4089 len = len1;
4090 else if (TREE_CODE (len1) != INTEGER_CST)
4091 len = len2;
4092 else if (TREE_CODE (len2) != INTEGER_CST)
4093 len = len1;
4094 else if (tree_int_cst_lt (len1, len2))
4095 len = len1;
4096 else
4097 len = len2;
4099 /* If both arguments have side effects, we cannot optimize. */
4100 if (!len || TREE_SIDE_EFFECTS (len))
4101 return NULL_RTX;
4103 /* The actual new length parameter is MIN(len,arg3). */
4104 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4105 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4107 /* If we don't have POINTER_TYPE, call the function. */
4108 if (arg1_align == 0 || arg2_align == 0)
4109 return NULL_RTX;
4111 /* Make a place to write the result of the instruction. */
4112 result = target;
4113 if (! (result != 0
4114 && REG_P (result) && GET_MODE (result) == insn_mode
4115 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4116 result = gen_reg_rtx (insn_mode);
4118 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4119 arg1 = builtin_save_expr (arg1);
4120 arg2 = builtin_save_expr (arg2);
4121 len = builtin_save_expr (len);
4123 arg1_rtx = get_memory_rtx (arg1, len);
4124 arg2_rtx = get_memory_rtx (arg2, len);
4125 arg3_rtx = expand_normal (len);
4126 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4127 GEN_INT (MIN (arg1_align, arg2_align)));
4128 if (insn)
4130 emit_insn (insn);
4132 /* Return the value in the proper mode for this function. */
4133 mode = TYPE_MODE (TREE_TYPE (exp));
4134 if (GET_MODE (result) == mode)
4135 return result;
4136 if (target == 0)
4137 return convert_to_mode (mode, result, 0);
4138 convert_move (target, result, 0);
4139 return target;
4142 /* Expand the library call ourselves using a stabilized argument
4143 list to avoid re-evaluating the function's arguments twice. */
4144 fndecl = get_callee_fndecl (exp);
4145 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4146 arg1, arg2, len);
4147 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4148 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4149 return expand_call (fn, target, target == const0_rtx);
4151 #endif
4152 return NULL_RTX;
4155 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4156 if that's convenient. */
4159 expand_builtin_saveregs (void)
4161 rtx val, seq;
4163 /* Don't do __builtin_saveregs more than once in a function.
4164 Save the result of the first call and reuse it. */
4165 if (saveregs_value != 0)
4166 return saveregs_value;
4168 /* When this function is called, it means that registers must be
4169 saved on entry to this function. So we migrate the call to the
4170 first insn of this function. */
4172 start_sequence ();
4174 /* Do whatever the machine needs done in this case. */
4175 val = targetm.calls.expand_builtin_saveregs ();
4177 seq = get_insns ();
4178 end_sequence ();
4180 saveregs_value = val;
4182 /* Put the insns after the NOTE that starts the function. If this
4183 is inside a start_sequence, make the outer-level insn chain current, so
4184 the code is placed at the start of the function. */
4185 push_topmost_sequence ();
4186 emit_insn_after (seq, entry_of_function ());
4187 pop_topmost_sequence ();
4189 return val;
4192 /* Expand a call to __builtin_next_arg. */
4194 static rtx
4195 expand_builtin_next_arg (void)
4197 /* Checking arguments is already done in fold_builtin_next_arg
4198 that must be called before this function. */
4199 return expand_binop (ptr_mode, add_optab,
4200 crtl->args.internal_arg_pointer,
4201 crtl->args.arg_offset_rtx,
4202 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4205 /* Make it easier for the backends by protecting the valist argument
4206 from multiple evaluations. */
4208 static tree
4209 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4211 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4213 /* The current way of determining the type of valist is completely
4214 bogus. We should have the information on the va builtin instead. */
4215 if (!vatype)
4216 vatype = targetm.fn_abi_va_list (cfun->decl);
4218 if (TREE_CODE (vatype) == ARRAY_TYPE)
4220 if (TREE_SIDE_EFFECTS (valist))
4221 valist = save_expr (valist);
4223 /* For this case, the backends will be expecting a pointer to
4224 vatype, but it's possible we've actually been given an array
4225 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4226 So fix it. */
4227 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4229 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4230 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4233 else
4235 tree pt = build_pointer_type (vatype);
4237 if (! needs_lvalue)
4239 if (! TREE_SIDE_EFFECTS (valist))
4240 return valist;
4242 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4243 TREE_SIDE_EFFECTS (valist) = 1;
4246 if (TREE_SIDE_EFFECTS (valist))
4247 valist = save_expr (valist);
4248 valist = fold_build2_loc (loc, MEM_REF,
4249 vatype, valist, build_int_cst (pt, 0));
4252 return valist;
4255 /* The "standard" definition of va_list is void*. */
4257 tree
4258 std_build_builtin_va_list (void)
4260 return ptr_type_node;
4263 /* The "standard" abi va_list is va_list_type_node. */
4265 tree
4266 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4268 return va_list_type_node;
4271 /* The "standard" type of va_list is va_list_type_node. */
4273 tree
4274 std_canonical_va_list_type (tree type)
4276 tree wtype, htype;
4278 if (INDIRECT_REF_P (type))
4279 type = TREE_TYPE (type);
4280 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4281 type = TREE_TYPE (type);
4282 wtype = va_list_type_node;
4283 htype = type;
4284 /* Treat structure va_list types. */
4285 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4286 htype = TREE_TYPE (htype);
4287 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4289 /* If va_list is an array type, the argument may have decayed
4290 to a pointer type, e.g. by being passed to another function.
4291 In that case, unwrap both types so that we can compare the
4292 underlying records. */
4293 if (TREE_CODE (htype) == ARRAY_TYPE
4294 || POINTER_TYPE_P (htype))
4296 wtype = TREE_TYPE (wtype);
4297 htype = TREE_TYPE (htype);
4300 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4301 return va_list_type_node;
4303 return NULL_TREE;
4306 /* The "standard" implementation of va_start: just assign `nextarg' to
4307 the variable. */
4309 void
4310 std_expand_builtin_va_start (tree valist, rtx nextarg)
4312 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4313 convert_move (va_r, nextarg, 0);
4316 /* Expand EXP, a call to __builtin_va_start. */
4318 static rtx
4319 expand_builtin_va_start (tree exp)
4321 rtx nextarg;
4322 tree valist;
4323 location_t loc = EXPR_LOCATION (exp);
4325 if (call_expr_nargs (exp) < 2)
4327 error_at (loc, "too few arguments to function %<va_start%>");
4328 return const0_rtx;
4331 if (fold_builtin_next_arg (exp, true))
4332 return const0_rtx;
4334 nextarg = expand_builtin_next_arg ();
4335 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4337 if (targetm.expand_builtin_va_start)
4338 targetm.expand_builtin_va_start (valist, nextarg);
4339 else
4340 std_expand_builtin_va_start (valist, nextarg);
4342 return const0_rtx;
4345 /* Expand EXP, a call to __builtin_va_end. */
4347 static rtx
4348 expand_builtin_va_end (tree exp)
4350 tree valist = CALL_EXPR_ARG (exp, 0);
4352 /* Evaluate for side effects, if needed. I hate macros that don't
4353 do that. */
4354 if (TREE_SIDE_EFFECTS (valist))
4355 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4357 return const0_rtx;
4360 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4361 builtin rather than just as an assignment in stdarg.h because of the
4362 nastiness of array-type va_list types. */
4364 static rtx
4365 expand_builtin_va_copy (tree exp)
4367 tree dst, src, t;
4368 location_t loc = EXPR_LOCATION (exp);
4370 dst = CALL_EXPR_ARG (exp, 0);
4371 src = CALL_EXPR_ARG (exp, 1);
4373 dst = stabilize_va_list_loc (loc, dst, 1);
4374 src = stabilize_va_list_loc (loc, src, 0);
4376 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4378 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4380 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4381 TREE_SIDE_EFFECTS (t) = 1;
4382 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4384 else
4386 rtx dstb, srcb, size;
4388 /* Evaluate to pointers. */
4389 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4390 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4391 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4392 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4394 dstb = convert_memory_address (Pmode, dstb);
4395 srcb = convert_memory_address (Pmode, srcb);
4397 /* "Dereference" to BLKmode memories. */
4398 dstb = gen_rtx_MEM (BLKmode, dstb);
4399 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4400 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4401 srcb = gen_rtx_MEM (BLKmode, srcb);
4402 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4403 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4405 /* Copy. */
4406 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4409 return const0_rtx;
4412 /* Expand a call to one of the builtin functions __builtin_frame_address or
4413 __builtin_return_address. */
4415 static rtx
4416 expand_builtin_frame_address (tree fndecl, tree exp)
4418 /* The argument must be a nonnegative integer constant.
4419 It counts the number of frames to scan up the stack.
4420 The value is the return address saved in that frame. */
4421 if (call_expr_nargs (exp) == 0)
4422 /* Warning about missing arg was already issued. */
4423 return const0_rtx;
4424 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4426 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4427 error ("invalid argument to %<__builtin_frame_address%>");
4428 else
4429 error ("invalid argument to %<__builtin_return_address%>");
4430 return const0_rtx;
4432 else
4434 rtx tem
4435 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4436 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4438 /* Some ports cannot access arbitrary stack frames. */
4439 if (tem == NULL)
4441 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4442 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4443 else
4444 warning (0, "unsupported argument to %<__builtin_return_address%>");
4445 return const0_rtx;
4448 /* For __builtin_frame_address, return what we've got. */
4449 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4450 return tem;
4452 if (!REG_P (tem)
4453 && ! CONSTANT_P (tem))
4454 tem = copy_addr_to_reg (tem);
4455 return tem;
4459 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4460 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4461 is the same as for allocate_dynamic_stack_space. */
4463 static rtx
4464 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4466 rtx op0;
4467 rtx result;
4468 bool valid_arglist;
4469 unsigned int align;
4470 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4471 == BUILT_IN_ALLOCA_WITH_ALIGN);
4473 valid_arglist
4474 = (alloca_with_align
4475 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4476 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4478 if (!valid_arglist)
4479 return NULL_RTX;
4481 /* Compute the argument. */
4482 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4484 /* Compute the alignment. */
4485 align = (alloca_with_align
4486 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4487 : BIGGEST_ALIGNMENT);
4489 /* Allocate the desired space. */
4490 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4491 result = convert_memory_address (ptr_mode, result);
4493 return result;
4496 /* Expand a call to bswap builtin in EXP.
4497 Return NULL_RTX if a normal call should be emitted rather than expanding the
4498 function in-line. If convenient, the result should be placed in TARGET.
4499 SUBTARGET may be used as the target for computing one of EXP's operands. */
4501 static rtx
4502 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4503 rtx subtarget)
4505 tree arg;
4506 rtx op0;
4508 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4509 return NULL_RTX;
4511 arg = CALL_EXPR_ARG (exp, 0);
4512 op0 = expand_expr (arg,
4513 subtarget && GET_MODE (subtarget) == target_mode
4514 ? subtarget : NULL_RTX,
4515 target_mode, EXPAND_NORMAL);
4516 if (GET_MODE (op0) != target_mode)
4517 op0 = convert_to_mode (target_mode, op0, 1);
4519 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4521 gcc_assert (target);
4523 return convert_to_mode (target_mode, target, 1);
4526 /* Expand a call to a unary builtin in EXP.
4527 Return NULL_RTX if a normal call should be emitted rather than expanding the
4528 function in-line. If convenient, the result should be placed in TARGET.
4529 SUBTARGET may be used as the target for computing one of EXP's operands. */
4531 static rtx
4532 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4533 rtx subtarget, optab op_optab)
4535 rtx op0;
4537 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4538 return NULL_RTX;
4540 /* Compute the argument. */
4541 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4542 (subtarget
4543 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4544 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4545 VOIDmode, EXPAND_NORMAL);
4546 /* Compute op, into TARGET if possible.
4547 Set TARGET to wherever the result comes back. */
4548 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4549 op_optab, op0, target, op_optab != clrsb_optab);
4550 gcc_assert (target);
4552 return convert_to_mode (target_mode, target, 0);
4555 /* Expand a call to __builtin_expect. We just return our argument
4556 as the builtin_expect semantic should've been already executed by
4557 tree branch prediction pass. */
4559 static rtx
4560 expand_builtin_expect (tree exp, rtx target)
4562 tree arg;
4564 if (call_expr_nargs (exp) < 2)
4565 return const0_rtx;
4566 arg = CALL_EXPR_ARG (exp, 0);
4568 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4569 /* When guessing was done, the hints should be already stripped away. */
4570 gcc_assert (!flag_guess_branch_prob
4571 || optimize == 0 || seen_error ());
4572 return target;
4575 /* Expand a call to __builtin_assume_aligned. We just return our first
4576 argument as the builtin_assume_aligned semantic should've been already
4577 executed by CCP. */
4579 static rtx
4580 expand_builtin_assume_aligned (tree exp, rtx target)
4582 if (call_expr_nargs (exp) < 2)
4583 return const0_rtx;
4584 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4585 EXPAND_NORMAL);
4586 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4587 && (call_expr_nargs (exp) < 3
4588 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4589 return target;
4592 void
4593 expand_builtin_trap (void)
4595 #ifdef HAVE_trap
4596 if (HAVE_trap)
4598 rtx insn = emit_insn (gen_trap ());
4599 /* For trap insns when not accumulating outgoing args force
4600 REG_ARGS_SIZE note to prevent crossjumping of calls with
4601 different args sizes. */
4602 if (!ACCUMULATE_OUTGOING_ARGS)
4603 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4605 else
4606 #endif
4607 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4608 emit_barrier ();
4611 /* Expand a call to __builtin_unreachable. We do nothing except emit
4612 a barrier saying that control flow will not pass here.
4614 It is the responsibility of the program being compiled to ensure
4615 that control flow does never reach __builtin_unreachable. */
4616 static void
4617 expand_builtin_unreachable (void)
4619 emit_barrier ();
4622 /* Expand EXP, a call to fabs, fabsf or fabsl.
4623 Return NULL_RTX if a normal call should be emitted rather than expanding
4624 the function inline. If convenient, the result should be placed
4625 in TARGET. SUBTARGET may be used as the target for computing
4626 the operand. */
4628 static rtx
4629 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4631 enum machine_mode mode;
4632 tree arg;
4633 rtx op0;
4635 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4636 return NULL_RTX;
4638 arg = CALL_EXPR_ARG (exp, 0);
4639 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4640 mode = TYPE_MODE (TREE_TYPE (arg));
4641 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4642 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4645 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4646 Return NULL is a normal call should be emitted rather than expanding the
4647 function inline. If convenient, the result should be placed in TARGET.
4648 SUBTARGET may be used as the target for computing the operand. */
4650 static rtx
4651 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4653 rtx op0, op1;
4654 tree arg;
4656 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4657 return NULL_RTX;
4659 arg = CALL_EXPR_ARG (exp, 0);
4660 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4662 arg = CALL_EXPR_ARG (exp, 1);
4663 op1 = expand_normal (arg);
4665 return expand_copysign (op0, op1, target);
4668 /* Expand a call to __builtin___clear_cache. */
4670 static rtx
4671 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4673 #ifndef HAVE_clear_cache
4674 #ifdef CLEAR_INSN_CACHE
4675 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4676 does something. Just do the default expansion to a call to
4677 __clear_cache(). */
4678 return NULL_RTX;
4679 #else
4680 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4681 does nothing. There is no need to call it. Do nothing. */
4682 return const0_rtx;
4683 #endif /* CLEAR_INSN_CACHE */
4684 #else
4685 /* We have a "clear_cache" insn, and it will handle everything. */
4686 tree begin, end;
4687 rtx begin_rtx, end_rtx;
4689 /* We must not expand to a library call. If we did, any
4690 fallback library function in libgcc that might contain a call to
4691 __builtin___clear_cache() would recurse infinitely. */
4692 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4694 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4695 return const0_rtx;
4698 if (HAVE_clear_cache)
4700 struct expand_operand ops[2];
4702 begin = CALL_EXPR_ARG (exp, 0);
4703 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4705 end = CALL_EXPR_ARG (exp, 1);
4706 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4708 create_address_operand (&ops[0], begin_rtx);
4709 create_address_operand (&ops[1], end_rtx);
4710 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4711 return const0_rtx;
4713 return const0_rtx;
4714 #endif /* HAVE_clear_cache */
4717 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4719 static rtx
4720 round_trampoline_addr (rtx tramp)
4722 rtx temp, addend, mask;
4724 /* If we don't need too much alignment, we'll have been guaranteed
4725 proper alignment by get_trampoline_type. */
4726 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4727 return tramp;
4729 /* Round address up to desired boundary. */
4730 temp = gen_reg_rtx (Pmode);
4731 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4732 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4734 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4735 temp, 0, OPTAB_LIB_WIDEN);
4736 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4737 temp, 0, OPTAB_LIB_WIDEN);
4739 return tramp;
4742 static rtx
4743 expand_builtin_init_trampoline (tree exp, bool onstack)
4745 tree t_tramp, t_func, t_chain;
4746 rtx m_tramp, r_tramp, r_chain, tmp;
4748 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4749 POINTER_TYPE, VOID_TYPE))
4750 return NULL_RTX;
4752 t_tramp = CALL_EXPR_ARG (exp, 0);
4753 t_func = CALL_EXPR_ARG (exp, 1);
4754 t_chain = CALL_EXPR_ARG (exp, 2);
4756 r_tramp = expand_normal (t_tramp);
4757 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4758 MEM_NOTRAP_P (m_tramp) = 1;
4760 /* If ONSTACK, the TRAMP argument should be the address of a field
4761 within the local function's FRAME decl. Either way, let's see if
4762 we can fill in the MEM_ATTRs for this memory. */
4763 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4764 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4766 /* Creator of a heap trampoline is responsible for making sure the
4767 address is aligned to at least STACK_BOUNDARY. Normally malloc
4768 will ensure this anyhow. */
4769 tmp = round_trampoline_addr (r_tramp);
4770 if (tmp != r_tramp)
4772 m_tramp = change_address (m_tramp, BLKmode, tmp);
4773 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4774 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4777 /* The FUNC argument should be the address of the nested function.
4778 Extract the actual function decl to pass to the hook. */
4779 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4780 t_func = TREE_OPERAND (t_func, 0);
4781 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4783 r_chain = expand_normal (t_chain);
4785 /* Generate insns to initialize the trampoline. */
4786 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4788 if (onstack)
4790 trampolines_created = 1;
4792 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4793 "trampoline generated for nested function %qD", t_func);
4796 return const0_rtx;
4799 static rtx
4800 expand_builtin_adjust_trampoline (tree exp)
4802 rtx tramp;
4804 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4805 return NULL_RTX;
4807 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4808 tramp = round_trampoline_addr (tramp);
4809 if (targetm.calls.trampoline_adjust_address)
4810 tramp = targetm.calls.trampoline_adjust_address (tramp);
4812 return tramp;
4815 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4816 function. The function first checks whether the back end provides
4817 an insn to implement signbit for the respective mode. If not, it
4818 checks whether the floating point format of the value is such that
4819 the sign bit can be extracted. If that is not the case, the
4820 function returns NULL_RTX to indicate that a normal call should be
4821 emitted rather than expanding the function in-line. EXP is the
4822 expression that is a call to the builtin function; if convenient,
4823 the result should be placed in TARGET. */
4824 static rtx
4825 expand_builtin_signbit (tree exp, rtx target)
4827 const struct real_format *fmt;
4828 enum machine_mode fmode, imode, rmode;
4829 tree arg;
4830 int word, bitpos;
4831 enum insn_code icode;
4832 rtx temp;
4833 location_t loc = EXPR_LOCATION (exp);
4835 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4836 return NULL_RTX;
4838 arg = CALL_EXPR_ARG (exp, 0);
4839 fmode = TYPE_MODE (TREE_TYPE (arg));
4840 rmode = TYPE_MODE (TREE_TYPE (exp));
4841 fmt = REAL_MODE_FORMAT (fmode);
4843 arg = builtin_save_expr (arg);
4845 /* Expand the argument yielding a RTX expression. */
4846 temp = expand_normal (arg);
4848 /* Check if the back end provides an insn that handles signbit for the
4849 argument's mode. */
4850 icode = optab_handler (signbit_optab, fmode);
4851 if (icode != CODE_FOR_nothing)
4853 rtx last = get_last_insn ();
4854 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4855 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4856 return target;
4857 delete_insns_since (last);
4860 /* For floating point formats without a sign bit, implement signbit
4861 as "ARG < 0.0". */
4862 bitpos = fmt->signbit_ro;
4863 if (bitpos < 0)
4865 /* But we can't do this if the format supports signed zero. */
4866 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4867 return NULL_RTX;
4869 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4870 build_real (TREE_TYPE (arg), dconst0));
4871 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4874 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4876 imode = int_mode_for_mode (fmode);
4877 if (imode == BLKmode)
4878 return NULL_RTX;
4879 temp = gen_lowpart (imode, temp);
4881 else
4883 imode = word_mode;
4884 /* Handle targets with different FP word orders. */
4885 if (FLOAT_WORDS_BIG_ENDIAN)
4886 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4887 else
4888 word = bitpos / BITS_PER_WORD;
4889 temp = operand_subword_force (temp, word, fmode);
4890 bitpos = bitpos % BITS_PER_WORD;
4893 /* Force the intermediate word_mode (or narrower) result into a
4894 register. This avoids attempting to create paradoxical SUBREGs
4895 of floating point modes below. */
4896 temp = force_reg (imode, temp);
4898 /* If the bitpos is within the "result mode" lowpart, the operation
4899 can be implement with a single bitwise AND. Otherwise, we need
4900 a right shift and an AND. */
4902 if (bitpos < GET_MODE_BITSIZE (rmode))
4904 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
4906 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4907 temp = gen_lowpart (rmode, temp);
4908 temp = expand_binop (rmode, and_optab, temp,
4909 immed_wide_int_const (mask, rmode),
4910 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4912 else
4914 /* Perform a logical right shift to place the signbit in the least
4915 significant bit, then truncate the result to the desired mode
4916 and mask just this bit. */
4917 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4918 temp = gen_lowpart (rmode, temp);
4919 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4920 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4923 return temp;
4926 /* Expand fork or exec calls. TARGET is the desired target of the
4927 call. EXP is the call. FN is the
4928 identificator of the actual function. IGNORE is nonzero if the
4929 value is to be ignored. */
4931 static rtx
4932 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4934 tree id, decl;
4935 tree call;
4937 /* If we are not profiling, just call the function. */
4938 if (!profile_arc_flag)
4939 return NULL_RTX;
4941 /* Otherwise call the wrapper. This should be equivalent for the rest of
4942 compiler, so the code does not diverge, and the wrapper may run the
4943 code necessary for keeping the profiling sane. */
4945 switch (DECL_FUNCTION_CODE (fn))
4947 case BUILT_IN_FORK:
4948 id = get_identifier ("__gcov_fork");
4949 break;
4951 case BUILT_IN_EXECL:
4952 id = get_identifier ("__gcov_execl");
4953 break;
4955 case BUILT_IN_EXECV:
4956 id = get_identifier ("__gcov_execv");
4957 break;
4959 case BUILT_IN_EXECLP:
4960 id = get_identifier ("__gcov_execlp");
4961 break;
4963 case BUILT_IN_EXECLE:
4964 id = get_identifier ("__gcov_execle");
4965 break;
4967 case BUILT_IN_EXECVP:
4968 id = get_identifier ("__gcov_execvp");
4969 break;
4971 case BUILT_IN_EXECVE:
4972 id = get_identifier ("__gcov_execve");
4973 break;
4975 default:
4976 gcc_unreachable ();
4979 decl = build_decl (DECL_SOURCE_LOCATION (fn),
4980 FUNCTION_DECL, id, TREE_TYPE (fn));
4981 DECL_EXTERNAL (decl) = 1;
4982 TREE_PUBLIC (decl) = 1;
4983 DECL_ARTIFICIAL (decl) = 1;
4984 TREE_NOTHROW (decl) = 1;
4985 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4986 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4987 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
4988 return expand_call (call, target, ignore);
4993 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
4994 the pointer in these functions is void*, the tree optimizers may remove
4995 casts. The mode computed in expand_builtin isn't reliable either, due
4996 to __sync_bool_compare_and_swap.
4998 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
4999 group of builtins. This gives us log2 of the mode size. */
5001 static inline enum machine_mode
5002 get_builtin_sync_mode (int fcode_diff)
5004 /* The size is not negotiable, so ask not to get BLKmode in return
5005 if the target indicates that a smaller size would be better. */
5006 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5009 /* Expand the memory expression LOC and return the appropriate memory operand
5010 for the builtin_sync operations. */
5012 static rtx
5013 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5015 rtx addr, mem;
5017 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5018 addr = convert_memory_address (Pmode, addr);
5020 /* Note that we explicitly do not want any alias information for this
5021 memory, so that we kill all other live memories. Otherwise we don't
5022 satisfy the full barrier semantics of the intrinsic. */
5023 mem = validize_mem (gen_rtx_MEM (mode, addr));
5025 /* The alignment needs to be at least according to that of the mode. */
5026 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5027 get_pointer_alignment (loc)));
5028 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5029 MEM_VOLATILE_P (mem) = 1;
5031 return mem;
5034 /* Make sure an argument is in the right mode.
5035 EXP is the tree argument.
5036 MODE is the mode it should be in. */
5038 static rtx
5039 expand_expr_force_mode (tree exp, enum machine_mode mode)
5041 rtx val;
5042 enum machine_mode old_mode;
5044 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5045 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5046 of CONST_INTs, where we know the old_mode only from the call argument. */
5048 old_mode = GET_MODE (val);
5049 if (old_mode == VOIDmode)
5050 old_mode = TYPE_MODE (TREE_TYPE (exp));
5051 val = convert_modes (mode, old_mode, val, 1);
5052 return val;
5056 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5057 EXP is the CALL_EXPR. CODE is the rtx code
5058 that corresponds to the arithmetic or logical operation from the name;
5059 an exception here is that NOT actually means NAND. TARGET is an optional
5060 place for us to store the results; AFTER is true if this is the
5061 fetch_and_xxx form. */
5063 static rtx
5064 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5065 enum rtx_code code, bool after,
5066 rtx target)
5068 rtx val, mem;
5069 location_t loc = EXPR_LOCATION (exp);
5071 if (code == NOT && warn_sync_nand)
5073 tree fndecl = get_callee_fndecl (exp);
5074 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5076 static bool warned_f_a_n, warned_n_a_f;
5078 switch (fcode)
5080 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5081 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5082 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5083 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5084 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5085 if (warned_f_a_n)
5086 break;
5088 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5089 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5090 warned_f_a_n = true;
5091 break;
5093 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5094 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5095 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5096 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5097 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5098 if (warned_n_a_f)
5099 break;
5101 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5102 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5103 warned_n_a_f = true;
5104 break;
5106 default:
5107 gcc_unreachable ();
5111 /* Expand the operands. */
5112 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5113 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5115 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5116 after);
5119 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5120 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5121 true if this is the boolean form. TARGET is a place for us to store the
5122 results; this is NOT optional if IS_BOOL is true. */
5124 static rtx
5125 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5126 bool is_bool, rtx target)
5128 rtx old_val, new_val, mem;
5129 rtx *pbool, *poval;
5131 /* Expand the operands. */
5132 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5133 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5134 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5136 pbool = poval = NULL;
5137 if (target != const0_rtx)
5139 if (is_bool)
5140 pbool = &target;
5141 else
5142 poval = &target;
5144 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5145 false, MEMMODEL_SEQ_CST,
5146 MEMMODEL_SEQ_CST))
5147 return NULL_RTX;
5149 return target;
5152 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5153 general form is actually an atomic exchange, and some targets only
5154 support a reduced form with the second argument being a constant 1.
5155 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5156 the results. */
5158 static rtx
5159 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5160 rtx target)
5162 rtx val, mem;
5164 /* Expand the operands. */
5165 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5166 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5168 return expand_sync_lock_test_and_set (target, mem, val);
5171 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5173 static void
5174 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5176 rtx mem;
5178 /* Expand the operands. */
5179 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5181 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5184 /* Given an integer representing an ``enum memmodel'', verify its
5185 correctness and return the memory model enum. */
5187 static enum memmodel
5188 get_memmodel (tree exp)
5190 rtx op;
5191 unsigned HOST_WIDE_INT val;
5193 /* If the parameter is not a constant, it's a run time value so we'll just
5194 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5195 if (TREE_CODE (exp) != INTEGER_CST)
5196 return MEMMODEL_SEQ_CST;
5198 op = expand_normal (exp);
5200 val = INTVAL (op);
5201 if (targetm.memmodel_check)
5202 val = targetm.memmodel_check (val);
5203 else if (val & ~MEMMODEL_MASK)
5205 warning (OPT_Winvalid_memory_model,
5206 "Unknown architecture specifier in memory model to builtin.");
5207 return MEMMODEL_SEQ_CST;
5210 if ((INTVAL (op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5212 warning (OPT_Winvalid_memory_model,
5213 "invalid memory model argument to builtin");
5214 return MEMMODEL_SEQ_CST;
5217 return (enum memmodel) val;
5220 /* Expand the __atomic_exchange intrinsic:
5221 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5222 EXP is the CALL_EXPR.
5223 TARGET is an optional place for us to store the results. */
5225 static rtx
5226 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5228 rtx val, mem;
5229 enum memmodel model;
5231 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5232 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5234 error ("invalid memory model for %<__atomic_exchange%>");
5235 return NULL_RTX;
5238 if (!flag_inline_atomics)
5239 return NULL_RTX;
5241 /* Expand the operands. */
5242 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5243 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5245 return expand_atomic_exchange (target, mem, val, model);
5248 /* Expand the __atomic_compare_exchange intrinsic:
5249 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5250 TYPE desired, BOOL weak,
5251 enum memmodel success,
5252 enum memmodel failure)
5253 EXP is the CALL_EXPR.
5254 TARGET is an optional place for us to store the results. */
5256 static rtx
5257 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5258 rtx target)
5260 rtx expect, desired, mem, oldval, label;
5261 enum memmodel success, failure;
5262 tree weak;
5263 bool is_weak;
5265 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5266 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5268 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5269 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5271 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5272 return NULL_RTX;
5275 if (failure > success)
5277 error ("failure memory model cannot be stronger than success "
5278 "memory model for %<__atomic_compare_exchange%>");
5279 return NULL_RTX;
5282 if (!flag_inline_atomics)
5283 return NULL_RTX;
5285 /* Expand the operands. */
5286 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5288 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5289 expect = convert_memory_address (Pmode, expect);
5290 expect = gen_rtx_MEM (mode, expect);
5291 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5293 weak = CALL_EXPR_ARG (exp, 3);
5294 is_weak = false;
5295 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5296 is_weak = true;
5298 if (target == const0_rtx)
5299 target = NULL;
5301 /* Lest the rtl backend create a race condition with an imporoper store
5302 to memory, always create a new pseudo for OLDVAL. */
5303 oldval = NULL;
5305 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5306 is_weak, success, failure))
5307 return NULL_RTX;
5309 /* Conditionally store back to EXPECT, lest we create a race condition
5310 with an improper store to memory. */
5311 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5312 the normal case where EXPECT is totally private, i.e. a register. At
5313 which point the store can be unconditional. */
5314 label = gen_label_rtx ();
5315 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL, VOIDmode, 1, label);
5316 emit_move_insn (expect, oldval);
5317 emit_label (label);
5319 return target;
5322 /* Expand the __atomic_load intrinsic:
5323 TYPE __atomic_load (TYPE *object, enum memmodel)
5324 EXP is the CALL_EXPR.
5325 TARGET is an optional place for us to store the results. */
5327 static rtx
5328 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5330 rtx mem;
5331 enum memmodel model;
5333 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5334 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5335 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5337 error ("invalid memory model for %<__atomic_load%>");
5338 return NULL_RTX;
5341 if (!flag_inline_atomics)
5342 return NULL_RTX;
5344 /* Expand the operand. */
5345 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5347 return expand_atomic_load (target, mem, model);
5351 /* Expand the __atomic_store intrinsic:
5352 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5353 EXP is the CALL_EXPR.
5354 TARGET is an optional place for us to store the results. */
5356 static rtx
5357 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5359 rtx mem, val;
5360 enum memmodel model;
5362 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5363 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5364 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5365 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5367 error ("invalid memory model for %<__atomic_store%>");
5368 return NULL_RTX;
5371 if (!flag_inline_atomics)
5372 return NULL_RTX;
5374 /* Expand the operands. */
5375 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5376 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5378 return expand_atomic_store (mem, val, model, false);
5381 /* Expand the __atomic_fetch_XXX intrinsic:
5382 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5383 EXP is the CALL_EXPR.
5384 TARGET is an optional place for us to store the results.
5385 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5386 FETCH_AFTER is true if returning the result of the operation.
5387 FETCH_AFTER is false if returning the value before the operation.
5388 IGNORE is true if the result is not used.
5389 EXT_CALL is the correct builtin for an external call if this cannot be
5390 resolved to an instruction sequence. */
5392 static rtx
5393 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5394 enum rtx_code code, bool fetch_after,
5395 bool ignore, enum built_in_function ext_call)
5397 rtx val, mem, ret;
5398 enum memmodel model;
5399 tree fndecl;
5400 tree addr;
5402 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5404 /* Expand the operands. */
5405 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5406 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5408 /* Only try generating instructions if inlining is turned on. */
5409 if (flag_inline_atomics)
5411 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5412 if (ret)
5413 return ret;
5416 /* Return if a different routine isn't needed for the library call. */
5417 if (ext_call == BUILT_IN_NONE)
5418 return NULL_RTX;
5420 /* Change the call to the specified function. */
5421 fndecl = get_callee_fndecl (exp);
5422 addr = CALL_EXPR_FN (exp);
5423 STRIP_NOPS (addr);
5425 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5426 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5428 /* Expand the call here so we can emit trailing code. */
5429 ret = expand_call (exp, target, ignore);
5431 /* Replace the original function just in case it matters. */
5432 TREE_OPERAND (addr, 0) = fndecl;
5434 /* Then issue the arithmetic correction to return the right result. */
5435 if (!ignore)
5437 if (code == NOT)
5439 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5440 OPTAB_LIB_WIDEN);
5441 ret = expand_simple_unop (mode, NOT, ret, target, true);
5443 else
5444 ret = expand_simple_binop (mode, code, ret, val, target, true,
5445 OPTAB_LIB_WIDEN);
5447 return ret;
5451 #ifndef HAVE_atomic_clear
5452 # define HAVE_atomic_clear 0
5453 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5454 #endif
5456 /* Expand an atomic clear operation.
5457 void _atomic_clear (BOOL *obj, enum memmodel)
5458 EXP is the call expression. */
5460 static rtx
5461 expand_builtin_atomic_clear (tree exp)
5463 enum machine_mode mode;
5464 rtx mem, ret;
5465 enum memmodel model;
5467 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5468 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5469 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5471 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5472 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5474 error ("invalid memory model for %<__atomic_store%>");
5475 return const0_rtx;
5478 if (HAVE_atomic_clear)
5480 emit_insn (gen_atomic_clear (mem, model));
5481 return const0_rtx;
5484 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5485 Failing that, a store is issued by __atomic_store. The only way this can
5486 fail is if the bool type is larger than a word size. Unlikely, but
5487 handle it anyway for completeness. Assume a single threaded model since
5488 there is no atomic support in this case, and no barriers are required. */
5489 ret = expand_atomic_store (mem, const0_rtx, model, true);
5490 if (!ret)
5491 emit_move_insn (mem, const0_rtx);
5492 return const0_rtx;
5495 /* Expand an atomic test_and_set operation.
5496 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5497 EXP is the call expression. */
5499 static rtx
5500 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5502 rtx mem;
5503 enum memmodel model;
5504 enum machine_mode mode;
5506 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5507 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5508 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5510 return expand_atomic_test_and_set (target, mem, model);
5514 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5515 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5517 static tree
5518 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5520 int size;
5521 enum machine_mode mode;
5522 unsigned int mode_align, type_align;
5524 if (TREE_CODE (arg0) != INTEGER_CST)
5525 return NULL_TREE;
5527 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5528 mode = mode_for_size (size, MODE_INT, 0);
5529 mode_align = GET_MODE_ALIGNMENT (mode);
5531 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5532 type_align = mode_align;
5533 else
5535 tree ttype = TREE_TYPE (arg1);
5537 /* This function is usually invoked and folded immediately by the front
5538 end before anything else has a chance to look at it. The pointer
5539 parameter at this point is usually cast to a void *, so check for that
5540 and look past the cast. */
5541 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5542 && VOID_TYPE_P (TREE_TYPE (ttype)))
5543 arg1 = TREE_OPERAND (arg1, 0);
5545 ttype = TREE_TYPE (arg1);
5546 gcc_assert (POINTER_TYPE_P (ttype));
5548 /* Get the underlying type of the object. */
5549 ttype = TREE_TYPE (ttype);
5550 type_align = TYPE_ALIGN (ttype);
5553 /* If the object has smaller alignment, the the lock free routines cannot
5554 be used. */
5555 if (type_align < mode_align)
5556 return boolean_false_node;
5558 /* Check if a compare_and_swap pattern exists for the mode which represents
5559 the required size. The pattern is not allowed to fail, so the existence
5560 of the pattern indicates support is present. */
5561 if (can_compare_and_swap_p (mode, true))
5562 return boolean_true_node;
5563 else
5564 return boolean_false_node;
5567 /* Return true if the parameters to call EXP represent an object which will
5568 always generate lock free instructions. The first argument represents the
5569 size of the object, and the second parameter is a pointer to the object
5570 itself. If NULL is passed for the object, then the result is based on
5571 typical alignment for an object of the specified size. Otherwise return
5572 false. */
5574 static rtx
5575 expand_builtin_atomic_always_lock_free (tree exp)
5577 tree size;
5578 tree arg0 = CALL_EXPR_ARG (exp, 0);
5579 tree arg1 = CALL_EXPR_ARG (exp, 1);
5581 if (TREE_CODE (arg0) != INTEGER_CST)
5583 error ("non-constant argument 1 to __atomic_always_lock_free");
5584 return const0_rtx;
5587 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5588 if (size == boolean_true_node)
5589 return const1_rtx;
5590 return const0_rtx;
5593 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5594 is lock free on this architecture. */
5596 static tree
5597 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5599 if (!flag_inline_atomics)
5600 return NULL_TREE;
5602 /* If it isn't always lock free, don't generate a result. */
5603 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5604 return boolean_true_node;
5606 return NULL_TREE;
5609 /* Return true if the parameters to call EXP represent an object which will
5610 always generate lock free instructions. The first argument represents the
5611 size of the object, and the second parameter is a pointer to the object
5612 itself. If NULL is passed for the object, then the result is based on
5613 typical alignment for an object of the specified size. Otherwise return
5614 NULL*/
5616 static rtx
5617 expand_builtin_atomic_is_lock_free (tree exp)
5619 tree size;
5620 tree arg0 = CALL_EXPR_ARG (exp, 0);
5621 tree arg1 = CALL_EXPR_ARG (exp, 1);
5623 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5625 error ("non-integer argument 1 to __atomic_is_lock_free");
5626 return NULL_RTX;
5629 if (!flag_inline_atomics)
5630 return NULL_RTX;
5632 /* If the value is known at compile time, return the RTX for it. */
5633 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5634 if (size == boolean_true_node)
5635 return const1_rtx;
5637 return NULL_RTX;
5640 /* Expand the __atomic_thread_fence intrinsic:
5641 void __atomic_thread_fence (enum memmodel)
5642 EXP is the CALL_EXPR. */
5644 static void
5645 expand_builtin_atomic_thread_fence (tree exp)
5647 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5648 expand_mem_thread_fence (model);
5651 /* Expand the __atomic_signal_fence intrinsic:
5652 void __atomic_signal_fence (enum memmodel)
5653 EXP is the CALL_EXPR. */
5655 static void
5656 expand_builtin_atomic_signal_fence (tree exp)
5658 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5659 expand_mem_signal_fence (model);
5662 /* Expand the __sync_synchronize intrinsic. */
5664 static void
5665 expand_builtin_sync_synchronize (void)
5667 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5670 static rtx
5671 expand_builtin_thread_pointer (tree exp, rtx target)
5673 enum insn_code icode;
5674 if (!validate_arglist (exp, VOID_TYPE))
5675 return const0_rtx;
5676 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5677 if (icode != CODE_FOR_nothing)
5679 struct expand_operand op;
5680 /* If the target is not sutitable then create a new target. */
5681 if (target == NULL_RTX
5682 || !REG_P (target)
5683 || GET_MODE (target) != Pmode)
5684 target = gen_reg_rtx (Pmode);
5685 create_output_operand (&op, target, Pmode);
5686 expand_insn (icode, 1, &op);
5687 return target;
5689 error ("__builtin_thread_pointer is not supported on this target");
5690 return const0_rtx;
5693 static void
5694 expand_builtin_set_thread_pointer (tree exp)
5696 enum insn_code icode;
5697 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5698 return;
5699 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5700 if (icode != CODE_FOR_nothing)
5702 struct expand_operand op;
5703 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5704 Pmode, EXPAND_NORMAL);
5705 create_input_operand (&op, val, Pmode);
5706 expand_insn (icode, 1, &op);
5707 return;
5709 error ("__builtin_set_thread_pointer is not supported on this target");
5713 /* Emit code to restore the current value of stack. */
5715 static void
5716 expand_stack_restore (tree var)
5718 rtx prev, sa = expand_normal (var);
5720 sa = convert_memory_address (Pmode, sa);
5722 prev = get_last_insn ();
5723 emit_stack_restore (SAVE_BLOCK, sa);
5724 fixup_args_size_notes (prev, get_last_insn (), 0);
5728 /* Emit code to save the current value of stack. */
5730 static rtx
5731 expand_stack_save (void)
5733 rtx ret = NULL_RTX;
5735 do_pending_stack_adjust ();
5736 emit_stack_save (SAVE_BLOCK, &ret);
5737 return ret;
5740 /* Expand an expression EXP that calls a built-in function,
5741 with result going to TARGET if that's convenient
5742 (and in mode MODE if that's convenient).
5743 SUBTARGET may be used as the target for computing one of EXP's operands.
5744 IGNORE is nonzero if the value is to be ignored. */
5747 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5748 int ignore)
5750 tree fndecl = get_callee_fndecl (exp);
5751 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5752 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5753 int flags;
5755 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5756 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5758 /* When not optimizing, generate calls to library functions for a certain
5759 set of builtins. */
5760 if (!optimize
5761 && !called_as_built_in (fndecl)
5762 && fcode != BUILT_IN_FORK
5763 && fcode != BUILT_IN_EXECL
5764 && fcode != BUILT_IN_EXECV
5765 && fcode != BUILT_IN_EXECLP
5766 && fcode != BUILT_IN_EXECLE
5767 && fcode != BUILT_IN_EXECVP
5768 && fcode != BUILT_IN_EXECVE
5769 && fcode != BUILT_IN_ALLOCA
5770 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5771 && fcode != BUILT_IN_FREE)
5772 return expand_call (exp, target, ignore);
5774 /* The built-in function expanders test for target == const0_rtx
5775 to determine whether the function's result will be ignored. */
5776 if (ignore)
5777 target = const0_rtx;
5779 /* If the result of a pure or const built-in function is ignored, and
5780 none of its arguments are volatile, we can avoid expanding the
5781 built-in call and just evaluate the arguments for side-effects. */
5782 if (target == const0_rtx
5783 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5784 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5786 bool volatilep = false;
5787 tree arg;
5788 call_expr_arg_iterator iter;
5790 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5791 if (TREE_THIS_VOLATILE (arg))
5793 volatilep = true;
5794 break;
5797 if (! volatilep)
5799 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5800 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5801 return const0_rtx;
5805 switch (fcode)
5807 CASE_FLT_FN (BUILT_IN_FABS):
5808 case BUILT_IN_FABSD32:
5809 case BUILT_IN_FABSD64:
5810 case BUILT_IN_FABSD128:
5811 target = expand_builtin_fabs (exp, target, subtarget);
5812 if (target)
5813 return target;
5814 break;
5816 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5817 target = expand_builtin_copysign (exp, target, subtarget);
5818 if (target)
5819 return target;
5820 break;
5822 /* Just do a normal library call if we were unable to fold
5823 the values. */
5824 CASE_FLT_FN (BUILT_IN_CABS):
5825 break;
5827 CASE_FLT_FN (BUILT_IN_EXP):
5828 CASE_FLT_FN (BUILT_IN_EXP10):
5829 CASE_FLT_FN (BUILT_IN_POW10):
5830 CASE_FLT_FN (BUILT_IN_EXP2):
5831 CASE_FLT_FN (BUILT_IN_EXPM1):
5832 CASE_FLT_FN (BUILT_IN_LOGB):
5833 CASE_FLT_FN (BUILT_IN_LOG):
5834 CASE_FLT_FN (BUILT_IN_LOG10):
5835 CASE_FLT_FN (BUILT_IN_LOG2):
5836 CASE_FLT_FN (BUILT_IN_LOG1P):
5837 CASE_FLT_FN (BUILT_IN_TAN):
5838 CASE_FLT_FN (BUILT_IN_ASIN):
5839 CASE_FLT_FN (BUILT_IN_ACOS):
5840 CASE_FLT_FN (BUILT_IN_ATAN):
5841 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5842 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5843 because of possible accuracy problems. */
5844 if (! flag_unsafe_math_optimizations)
5845 break;
5846 CASE_FLT_FN (BUILT_IN_SQRT):
5847 CASE_FLT_FN (BUILT_IN_FLOOR):
5848 CASE_FLT_FN (BUILT_IN_CEIL):
5849 CASE_FLT_FN (BUILT_IN_TRUNC):
5850 CASE_FLT_FN (BUILT_IN_ROUND):
5851 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5852 CASE_FLT_FN (BUILT_IN_RINT):
5853 target = expand_builtin_mathfn (exp, target, subtarget);
5854 if (target)
5855 return target;
5856 break;
5858 CASE_FLT_FN (BUILT_IN_FMA):
5859 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5860 if (target)
5861 return target;
5862 break;
5864 CASE_FLT_FN (BUILT_IN_ILOGB):
5865 if (! flag_unsafe_math_optimizations)
5866 break;
5867 CASE_FLT_FN (BUILT_IN_ISINF):
5868 CASE_FLT_FN (BUILT_IN_FINITE):
5869 case BUILT_IN_ISFINITE:
5870 case BUILT_IN_ISNORMAL:
5871 target = expand_builtin_interclass_mathfn (exp, target);
5872 if (target)
5873 return target;
5874 break;
5876 CASE_FLT_FN (BUILT_IN_ICEIL):
5877 CASE_FLT_FN (BUILT_IN_LCEIL):
5878 CASE_FLT_FN (BUILT_IN_LLCEIL):
5879 CASE_FLT_FN (BUILT_IN_LFLOOR):
5880 CASE_FLT_FN (BUILT_IN_IFLOOR):
5881 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5882 target = expand_builtin_int_roundingfn (exp, target);
5883 if (target)
5884 return target;
5885 break;
5887 CASE_FLT_FN (BUILT_IN_IRINT):
5888 CASE_FLT_FN (BUILT_IN_LRINT):
5889 CASE_FLT_FN (BUILT_IN_LLRINT):
5890 CASE_FLT_FN (BUILT_IN_IROUND):
5891 CASE_FLT_FN (BUILT_IN_LROUND):
5892 CASE_FLT_FN (BUILT_IN_LLROUND):
5893 target = expand_builtin_int_roundingfn_2 (exp, target);
5894 if (target)
5895 return target;
5896 break;
5898 CASE_FLT_FN (BUILT_IN_POWI):
5899 target = expand_builtin_powi (exp, target);
5900 if (target)
5901 return target;
5902 break;
5904 CASE_FLT_FN (BUILT_IN_ATAN2):
5905 CASE_FLT_FN (BUILT_IN_LDEXP):
5906 CASE_FLT_FN (BUILT_IN_SCALB):
5907 CASE_FLT_FN (BUILT_IN_SCALBN):
5908 CASE_FLT_FN (BUILT_IN_SCALBLN):
5909 if (! flag_unsafe_math_optimizations)
5910 break;
5912 CASE_FLT_FN (BUILT_IN_FMOD):
5913 CASE_FLT_FN (BUILT_IN_REMAINDER):
5914 CASE_FLT_FN (BUILT_IN_DREM):
5915 CASE_FLT_FN (BUILT_IN_POW):
5916 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5917 if (target)
5918 return target;
5919 break;
5921 CASE_FLT_FN (BUILT_IN_CEXPI):
5922 target = expand_builtin_cexpi (exp, target);
5923 gcc_assert (target);
5924 return target;
5926 CASE_FLT_FN (BUILT_IN_SIN):
5927 CASE_FLT_FN (BUILT_IN_COS):
5928 if (! flag_unsafe_math_optimizations)
5929 break;
5930 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5931 if (target)
5932 return target;
5933 break;
5935 CASE_FLT_FN (BUILT_IN_SINCOS):
5936 if (! flag_unsafe_math_optimizations)
5937 break;
5938 target = expand_builtin_sincos (exp);
5939 if (target)
5940 return target;
5941 break;
5943 case BUILT_IN_APPLY_ARGS:
5944 return expand_builtin_apply_args ();
5946 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5947 FUNCTION with a copy of the parameters described by
5948 ARGUMENTS, and ARGSIZE. It returns a block of memory
5949 allocated on the stack into which is stored all the registers
5950 that might possibly be used for returning the result of a
5951 function. ARGUMENTS is the value returned by
5952 __builtin_apply_args. ARGSIZE is the number of bytes of
5953 arguments that must be copied. ??? How should this value be
5954 computed? We'll also need a safe worst case value for varargs
5955 functions. */
5956 case BUILT_IN_APPLY:
5957 if (!validate_arglist (exp, POINTER_TYPE,
5958 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5959 && !validate_arglist (exp, REFERENCE_TYPE,
5960 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5961 return const0_rtx;
5962 else
5964 rtx ops[3];
5966 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5967 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5968 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5970 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5973 /* __builtin_return (RESULT) causes the function to return the
5974 value described by RESULT. RESULT is address of the block of
5975 memory returned by __builtin_apply. */
5976 case BUILT_IN_RETURN:
5977 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5978 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5979 return const0_rtx;
5981 case BUILT_IN_SAVEREGS:
5982 return expand_builtin_saveregs ();
5984 case BUILT_IN_VA_ARG_PACK:
5985 /* All valid uses of __builtin_va_arg_pack () are removed during
5986 inlining. */
5987 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
5988 return const0_rtx;
5990 case BUILT_IN_VA_ARG_PACK_LEN:
5991 /* All valid uses of __builtin_va_arg_pack_len () are removed during
5992 inlining. */
5993 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
5994 return const0_rtx;
5996 /* Return the address of the first anonymous stack arg. */
5997 case BUILT_IN_NEXT_ARG:
5998 if (fold_builtin_next_arg (exp, false))
5999 return const0_rtx;
6000 return expand_builtin_next_arg ();
6002 case BUILT_IN_CLEAR_CACHE:
6003 target = expand_builtin___clear_cache (exp);
6004 if (target)
6005 return target;
6006 break;
6008 case BUILT_IN_CLASSIFY_TYPE:
6009 return expand_builtin_classify_type (exp);
6011 case BUILT_IN_CONSTANT_P:
6012 return const0_rtx;
6014 case BUILT_IN_FRAME_ADDRESS:
6015 case BUILT_IN_RETURN_ADDRESS:
6016 return expand_builtin_frame_address (fndecl, exp);
6018 /* Returns the address of the area where the structure is returned.
6019 0 otherwise. */
6020 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6021 if (call_expr_nargs (exp) != 0
6022 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6023 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6024 return const0_rtx;
6025 else
6026 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6028 case BUILT_IN_ALLOCA:
6029 case BUILT_IN_ALLOCA_WITH_ALIGN:
6030 /* If the allocation stems from the declaration of a variable-sized
6031 object, it cannot accumulate. */
6032 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6033 if (target)
6034 return target;
6035 break;
6037 case BUILT_IN_STACK_SAVE:
6038 return expand_stack_save ();
6040 case BUILT_IN_STACK_RESTORE:
6041 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6042 return const0_rtx;
6044 case BUILT_IN_BSWAP16:
6045 case BUILT_IN_BSWAP32:
6046 case BUILT_IN_BSWAP64:
6047 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6048 if (target)
6049 return target;
6050 break;
6052 CASE_INT_FN (BUILT_IN_FFS):
6053 target = expand_builtin_unop (target_mode, exp, target,
6054 subtarget, ffs_optab);
6055 if (target)
6056 return target;
6057 break;
6059 CASE_INT_FN (BUILT_IN_CLZ):
6060 target = expand_builtin_unop (target_mode, exp, target,
6061 subtarget, clz_optab);
6062 if (target)
6063 return target;
6064 break;
6066 CASE_INT_FN (BUILT_IN_CTZ):
6067 target = expand_builtin_unop (target_mode, exp, target,
6068 subtarget, ctz_optab);
6069 if (target)
6070 return target;
6071 break;
6073 CASE_INT_FN (BUILT_IN_CLRSB):
6074 target = expand_builtin_unop (target_mode, exp, target,
6075 subtarget, clrsb_optab);
6076 if (target)
6077 return target;
6078 break;
6080 CASE_INT_FN (BUILT_IN_POPCOUNT):
6081 target = expand_builtin_unop (target_mode, exp, target,
6082 subtarget, popcount_optab);
6083 if (target)
6084 return target;
6085 break;
6087 CASE_INT_FN (BUILT_IN_PARITY):
6088 target = expand_builtin_unop (target_mode, exp, target,
6089 subtarget, parity_optab);
6090 if (target)
6091 return target;
6092 break;
6094 case BUILT_IN_STRLEN:
6095 target = expand_builtin_strlen (exp, target, target_mode);
6096 if (target)
6097 return target;
6098 break;
6100 case BUILT_IN_STRCPY:
6101 target = expand_builtin_strcpy (exp, target);
6102 if (target)
6103 return target;
6104 break;
6106 case BUILT_IN_STRNCPY:
6107 target = expand_builtin_strncpy (exp, target);
6108 if (target)
6109 return target;
6110 break;
6112 case BUILT_IN_STPCPY:
6113 target = expand_builtin_stpcpy (exp, target, mode);
6114 if (target)
6115 return target;
6116 break;
6118 case BUILT_IN_MEMCPY:
6119 target = expand_builtin_memcpy (exp, target);
6120 if (target)
6121 return target;
6122 break;
6124 case BUILT_IN_MEMPCPY:
6125 target = expand_builtin_mempcpy (exp, target, mode);
6126 if (target)
6127 return target;
6128 break;
6130 case BUILT_IN_MEMSET:
6131 target = expand_builtin_memset (exp, target, mode);
6132 if (target)
6133 return target;
6134 break;
6136 case BUILT_IN_BZERO:
6137 target = expand_builtin_bzero (exp);
6138 if (target)
6139 return target;
6140 break;
6142 case BUILT_IN_STRCMP:
6143 target = expand_builtin_strcmp (exp, target);
6144 if (target)
6145 return target;
6146 break;
6148 case BUILT_IN_STRNCMP:
6149 target = expand_builtin_strncmp (exp, target, mode);
6150 if (target)
6151 return target;
6152 break;
6154 case BUILT_IN_BCMP:
6155 case BUILT_IN_MEMCMP:
6156 target = expand_builtin_memcmp (exp, target, mode);
6157 if (target)
6158 return target;
6159 break;
6161 case BUILT_IN_SETJMP:
6162 /* This should have been lowered to the builtins below. */
6163 gcc_unreachable ();
6165 case BUILT_IN_SETJMP_SETUP:
6166 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6167 and the receiver label. */
6168 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6170 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6171 VOIDmode, EXPAND_NORMAL);
6172 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6173 rtx label_r = label_rtx (label);
6175 /* This is copied from the handling of non-local gotos. */
6176 expand_builtin_setjmp_setup (buf_addr, label_r);
6177 nonlocal_goto_handler_labels
6178 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6179 nonlocal_goto_handler_labels);
6180 /* ??? Do not let expand_label treat us as such since we would
6181 not want to be both on the list of non-local labels and on
6182 the list of forced labels. */
6183 FORCED_LABEL (label) = 0;
6184 return const0_rtx;
6186 break;
6188 case BUILT_IN_SETJMP_RECEIVER:
6189 /* __builtin_setjmp_receiver is passed the receiver label. */
6190 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6192 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6193 rtx label_r = label_rtx (label);
6195 expand_builtin_setjmp_receiver (label_r);
6196 return const0_rtx;
6198 break;
6200 /* __builtin_longjmp is passed a pointer to an array of five words.
6201 It's similar to the C library longjmp function but works with
6202 __builtin_setjmp above. */
6203 case BUILT_IN_LONGJMP:
6204 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6206 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6207 VOIDmode, EXPAND_NORMAL);
6208 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6210 if (value != const1_rtx)
6212 error ("%<__builtin_longjmp%> second argument must be 1");
6213 return const0_rtx;
6216 expand_builtin_longjmp (buf_addr, value);
6217 return const0_rtx;
6219 break;
6221 case BUILT_IN_NONLOCAL_GOTO:
6222 target = expand_builtin_nonlocal_goto (exp);
6223 if (target)
6224 return target;
6225 break;
6227 /* This updates the setjmp buffer that is its argument with the value
6228 of the current stack pointer. */
6229 case BUILT_IN_UPDATE_SETJMP_BUF:
6230 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6232 rtx buf_addr
6233 = expand_normal (CALL_EXPR_ARG (exp, 0));
6235 expand_builtin_update_setjmp_buf (buf_addr);
6236 return const0_rtx;
6238 break;
6240 case BUILT_IN_TRAP:
6241 expand_builtin_trap ();
6242 return const0_rtx;
6244 case BUILT_IN_UNREACHABLE:
6245 expand_builtin_unreachable ();
6246 return const0_rtx;
6248 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6249 case BUILT_IN_SIGNBITD32:
6250 case BUILT_IN_SIGNBITD64:
6251 case BUILT_IN_SIGNBITD128:
6252 target = expand_builtin_signbit (exp, target);
6253 if (target)
6254 return target;
6255 break;
6257 /* Various hooks for the DWARF 2 __throw routine. */
6258 case BUILT_IN_UNWIND_INIT:
6259 expand_builtin_unwind_init ();
6260 return const0_rtx;
6261 case BUILT_IN_DWARF_CFA:
6262 return virtual_cfa_rtx;
6263 #ifdef DWARF2_UNWIND_INFO
6264 case BUILT_IN_DWARF_SP_COLUMN:
6265 return expand_builtin_dwarf_sp_column ();
6266 case BUILT_IN_INIT_DWARF_REG_SIZES:
6267 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6268 return const0_rtx;
6269 #endif
6270 case BUILT_IN_FROB_RETURN_ADDR:
6271 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6272 case BUILT_IN_EXTRACT_RETURN_ADDR:
6273 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6274 case BUILT_IN_EH_RETURN:
6275 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6276 CALL_EXPR_ARG (exp, 1));
6277 return const0_rtx;
6278 #ifdef EH_RETURN_DATA_REGNO
6279 case BUILT_IN_EH_RETURN_DATA_REGNO:
6280 return expand_builtin_eh_return_data_regno (exp);
6281 #endif
6282 case BUILT_IN_EXTEND_POINTER:
6283 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6284 case BUILT_IN_EH_POINTER:
6285 return expand_builtin_eh_pointer (exp);
6286 case BUILT_IN_EH_FILTER:
6287 return expand_builtin_eh_filter (exp);
6288 case BUILT_IN_EH_COPY_VALUES:
6289 return expand_builtin_eh_copy_values (exp);
6291 case BUILT_IN_VA_START:
6292 return expand_builtin_va_start (exp);
6293 case BUILT_IN_VA_END:
6294 return expand_builtin_va_end (exp);
6295 case BUILT_IN_VA_COPY:
6296 return expand_builtin_va_copy (exp);
6297 case BUILT_IN_EXPECT:
6298 return expand_builtin_expect (exp, target);
6299 case BUILT_IN_ASSUME_ALIGNED:
6300 return expand_builtin_assume_aligned (exp, target);
6301 case BUILT_IN_PREFETCH:
6302 expand_builtin_prefetch (exp);
6303 return const0_rtx;
6305 case BUILT_IN_INIT_TRAMPOLINE:
6306 return expand_builtin_init_trampoline (exp, true);
6307 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6308 return expand_builtin_init_trampoline (exp, false);
6309 case BUILT_IN_ADJUST_TRAMPOLINE:
6310 return expand_builtin_adjust_trampoline (exp);
6312 case BUILT_IN_FORK:
6313 case BUILT_IN_EXECL:
6314 case BUILT_IN_EXECV:
6315 case BUILT_IN_EXECLP:
6316 case BUILT_IN_EXECLE:
6317 case BUILT_IN_EXECVP:
6318 case BUILT_IN_EXECVE:
6319 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6320 if (target)
6321 return target;
6322 break;
6324 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6325 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6326 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6327 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6328 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6329 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6330 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6331 if (target)
6332 return target;
6333 break;
6335 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6336 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6337 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6338 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6339 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6340 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6341 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6342 if (target)
6343 return target;
6344 break;
6346 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6347 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6348 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6349 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6350 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6351 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6352 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6353 if (target)
6354 return target;
6355 break;
6357 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6358 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6359 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6360 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6361 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6362 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6363 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6364 if (target)
6365 return target;
6366 break;
6368 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6369 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6370 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6371 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6372 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6373 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6374 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6375 if (target)
6376 return target;
6377 break;
6379 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6380 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6381 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6382 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6383 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6384 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6385 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6386 if (target)
6387 return target;
6388 break;
6390 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6391 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6392 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6393 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6394 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6395 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6396 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6397 if (target)
6398 return target;
6399 break;
6401 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6402 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6403 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6404 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6405 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6406 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6407 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6408 if (target)
6409 return target;
6410 break;
6412 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6413 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6414 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6415 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6416 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6417 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6418 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6419 if (target)
6420 return target;
6421 break;
6423 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6424 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6425 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6426 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6427 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6428 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6429 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6430 if (target)
6431 return target;
6432 break;
6434 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6435 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6436 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6437 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6438 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6439 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6440 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6441 if (target)
6442 return target;
6443 break;
6445 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6446 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6447 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6448 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6449 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6450 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6451 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6452 if (target)
6453 return target;
6454 break;
6456 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6457 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6458 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6459 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6460 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6461 if (mode == VOIDmode)
6462 mode = TYPE_MODE (boolean_type_node);
6463 if (!target || !register_operand (target, mode))
6464 target = gen_reg_rtx (mode);
6466 mode = get_builtin_sync_mode
6467 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6468 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6469 if (target)
6470 return target;
6471 break;
6473 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6474 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6475 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6476 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6477 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6478 mode = get_builtin_sync_mode
6479 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6480 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6481 if (target)
6482 return target;
6483 break;
6485 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6486 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6487 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6488 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6489 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6490 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6491 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6492 if (target)
6493 return target;
6494 break;
6496 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6497 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6498 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6499 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6500 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6501 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6502 expand_builtin_sync_lock_release (mode, exp);
6503 return const0_rtx;
6505 case BUILT_IN_SYNC_SYNCHRONIZE:
6506 expand_builtin_sync_synchronize ();
6507 return const0_rtx;
6509 case BUILT_IN_ATOMIC_EXCHANGE_1:
6510 case BUILT_IN_ATOMIC_EXCHANGE_2:
6511 case BUILT_IN_ATOMIC_EXCHANGE_4:
6512 case BUILT_IN_ATOMIC_EXCHANGE_8:
6513 case BUILT_IN_ATOMIC_EXCHANGE_16:
6514 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6515 target = expand_builtin_atomic_exchange (mode, exp, target);
6516 if (target)
6517 return target;
6518 break;
6520 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6521 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6522 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6523 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6524 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6526 unsigned int nargs, z;
6527 vec<tree, va_gc> *vec;
6529 mode =
6530 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6531 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6532 if (target)
6533 return target;
6535 /* If this is turned into an external library call, the weak parameter
6536 must be dropped to match the expected parameter list. */
6537 nargs = call_expr_nargs (exp);
6538 vec_alloc (vec, nargs - 1);
6539 for (z = 0; z < 3; z++)
6540 vec->quick_push (CALL_EXPR_ARG (exp, z));
6541 /* Skip the boolean weak parameter. */
6542 for (z = 4; z < 6; z++)
6543 vec->quick_push (CALL_EXPR_ARG (exp, z));
6544 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6545 break;
6548 case BUILT_IN_ATOMIC_LOAD_1:
6549 case BUILT_IN_ATOMIC_LOAD_2:
6550 case BUILT_IN_ATOMIC_LOAD_4:
6551 case BUILT_IN_ATOMIC_LOAD_8:
6552 case BUILT_IN_ATOMIC_LOAD_16:
6553 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6554 target = expand_builtin_atomic_load (mode, exp, target);
6555 if (target)
6556 return target;
6557 break;
6559 case BUILT_IN_ATOMIC_STORE_1:
6560 case BUILT_IN_ATOMIC_STORE_2:
6561 case BUILT_IN_ATOMIC_STORE_4:
6562 case BUILT_IN_ATOMIC_STORE_8:
6563 case BUILT_IN_ATOMIC_STORE_16:
6564 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6565 target = expand_builtin_atomic_store (mode, exp);
6566 if (target)
6567 return const0_rtx;
6568 break;
6570 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6571 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6572 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6573 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6574 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6576 enum built_in_function lib;
6577 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6578 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6579 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6580 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6581 ignore, lib);
6582 if (target)
6583 return target;
6584 break;
6586 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6587 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6588 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6589 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6590 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6592 enum built_in_function lib;
6593 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6594 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6595 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6596 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6597 ignore, lib);
6598 if (target)
6599 return target;
6600 break;
6602 case BUILT_IN_ATOMIC_AND_FETCH_1:
6603 case BUILT_IN_ATOMIC_AND_FETCH_2:
6604 case BUILT_IN_ATOMIC_AND_FETCH_4:
6605 case BUILT_IN_ATOMIC_AND_FETCH_8:
6606 case BUILT_IN_ATOMIC_AND_FETCH_16:
6608 enum built_in_function lib;
6609 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6610 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6611 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6612 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6613 ignore, lib);
6614 if (target)
6615 return target;
6616 break;
6618 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6619 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6620 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6621 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6622 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6624 enum built_in_function lib;
6625 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6626 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6627 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6628 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6629 ignore, lib);
6630 if (target)
6631 return target;
6632 break;
6634 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6635 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6636 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6637 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6638 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6640 enum built_in_function lib;
6641 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6642 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6643 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6644 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6645 ignore, lib);
6646 if (target)
6647 return target;
6648 break;
6650 case BUILT_IN_ATOMIC_OR_FETCH_1:
6651 case BUILT_IN_ATOMIC_OR_FETCH_2:
6652 case BUILT_IN_ATOMIC_OR_FETCH_4:
6653 case BUILT_IN_ATOMIC_OR_FETCH_8:
6654 case BUILT_IN_ATOMIC_OR_FETCH_16:
6656 enum built_in_function lib;
6657 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6658 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6659 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6660 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6661 ignore, lib);
6662 if (target)
6663 return target;
6664 break;
6666 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6667 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6668 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6669 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6670 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6671 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6672 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6673 ignore, BUILT_IN_NONE);
6674 if (target)
6675 return target;
6676 break;
6678 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6679 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6680 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6681 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6682 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6683 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6684 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6685 ignore, BUILT_IN_NONE);
6686 if (target)
6687 return target;
6688 break;
6690 case BUILT_IN_ATOMIC_FETCH_AND_1:
6691 case BUILT_IN_ATOMIC_FETCH_AND_2:
6692 case BUILT_IN_ATOMIC_FETCH_AND_4:
6693 case BUILT_IN_ATOMIC_FETCH_AND_8:
6694 case BUILT_IN_ATOMIC_FETCH_AND_16:
6695 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6696 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6697 ignore, BUILT_IN_NONE);
6698 if (target)
6699 return target;
6700 break;
6702 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6703 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6704 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6705 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6706 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6707 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6708 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6709 ignore, BUILT_IN_NONE);
6710 if (target)
6711 return target;
6712 break;
6714 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6715 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6716 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6717 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6718 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6719 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6720 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6721 ignore, BUILT_IN_NONE);
6722 if (target)
6723 return target;
6724 break;
6726 case BUILT_IN_ATOMIC_FETCH_OR_1:
6727 case BUILT_IN_ATOMIC_FETCH_OR_2:
6728 case BUILT_IN_ATOMIC_FETCH_OR_4:
6729 case BUILT_IN_ATOMIC_FETCH_OR_8:
6730 case BUILT_IN_ATOMIC_FETCH_OR_16:
6731 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6732 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6733 ignore, BUILT_IN_NONE);
6734 if (target)
6735 return target;
6736 break;
6738 case BUILT_IN_ATOMIC_TEST_AND_SET:
6739 return expand_builtin_atomic_test_and_set (exp, target);
6741 case BUILT_IN_ATOMIC_CLEAR:
6742 return expand_builtin_atomic_clear (exp);
6744 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6745 return expand_builtin_atomic_always_lock_free (exp);
6747 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6748 target = expand_builtin_atomic_is_lock_free (exp);
6749 if (target)
6750 return target;
6751 break;
6753 case BUILT_IN_ATOMIC_THREAD_FENCE:
6754 expand_builtin_atomic_thread_fence (exp);
6755 return const0_rtx;
6757 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6758 expand_builtin_atomic_signal_fence (exp);
6759 return const0_rtx;
6761 case BUILT_IN_OBJECT_SIZE:
6762 return expand_builtin_object_size (exp);
6764 case BUILT_IN_MEMCPY_CHK:
6765 case BUILT_IN_MEMPCPY_CHK:
6766 case BUILT_IN_MEMMOVE_CHK:
6767 case BUILT_IN_MEMSET_CHK:
6768 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6769 if (target)
6770 return target;
6771 break;
6773 case BUILT_IN_STRCPY_CHK:
6774 case BUILT_IN_STPCPY_CHK:
6775 case BUILT_IN_STRNCPY_CHK:
6776 case BUILT_IN_STPNCPY_CHK:
6777 case BUILT_IN_STRCAT_CHK:
6778 case BUILT_IN_STRNCAT_CHK:
6779 case BUILT_IN_SNPRINTF_CHK:
6780 case BUILT_IN_VSNPRINTF_CHK:
6781 maybe_emit_chk_warning (exp, fcode);
6782 break;
6784 case BUILT_IN_SPRINTF_CHK:
6785 case BUILT_IN_VSPRINTF_CHK:
6786 maybe_emit_sprintf_chk_warning (exp, fcode);
6787 break;
6789 case BUILT_IN_FREE:
6790 if (warn_free_nonheap_object)
6791 maybe_emit_free_warning (exp);
6792 break;
6794 case BUILT_IN_THREAD_POINTER:
6795 return expand_builtin_thread_pointer (exp, target);
6797 case BUILT_IN_SET_THREAD_POINTER:
6798 expand_builtin_set_thread_pointer (exp);
6799 return const0_rtx;
6801 case BUILT_IN_CILK_DETACH:
6802 expand_builtin_cilk_detach (exp);
6803 return const0_rtx;
6805 case BUILT_IN_CILK_POP_FRAME:
6806 expand_builtin_cilk_pop_frame (exp);
6807 return const0_rtx;
6809 default: /* just do library call, if unknown builtin */
6810 break;
6813 /* The switch statement above can drop through to cause the function
6814 to be called normally. */
6815 return expand_call (exp, target, ignore);
6818 /* Determine whether a tree node represents a call to a built-in
6819 function. If the tree T is a call to a built-in function with
6820 the right number of arguments of the appropriate types, return
6821 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6822 Otherwise the return value is END_BUILTINS. */
6824 enum built_in_function
6825 builtin_mathfn_code (const_tree t)
6827 const_tree fndecl, arg, parmlist;
6828 const_tree argtype, parmtype;
6829 const_call_expr_arg_iterator iter;
6831 if (TREE_CODE (t) != CALL_EXPR
6832 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6833 return END_BUILTINS;
6835 fndecl = get_callee_fndecl (t);
6836 if (fndecl == NULL_TREE
6837 || TREE_CODE (fndecl) != FUNCTION_DECL
6838 || ! DECL_BUILT_IN (fndecl)
6839 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6840 return END_BUILTINS;
6842 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6843 init_const_call_expr_arg_iterator (t, &iter);
6844 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6846 /* If a function doesn't take a variable number of arguments,
6847 the last element in the list will have type `void'. */
6848 parmtype = TREE_VALUE (parmlist);
6849 if (VOID_TYPE_P (parmtype))
6851 if (more_const_call_expr_args_p (&iter))
6852 return END_BUILTINS;
6853 return DECL_FUNCTION_CODE (fndecl);
6856 if (! more_const_call_expr_args_p (&iter))
6857 return END_BUILTINS;
6859 arg = next_const_call_expr_arg (&iter);
6860 argtype = TREE_TYPE (arg);
6862 if (SCALAR_FLOAT_TYPE_P (parmtype))
6864 if (! SCALAR_FLOAT_TYPE_P (argtype))
6865 return END_BUILTINS;
6867 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6869 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6870 return END_BUILTINS;
6872 else if (POINTER_TYPE_P (parmtype))
6874 if (! POINTER_TYPE_P (argtype))
6875 return END_BUILTINS;
6877 else if (INTEGRAL_TYPE_P (parmtype))
6879 if (! INTEGRAL_TYPE_P (argtype))
6880 return END_BUILTINS;
6882 else
6883 return END_BUILTINS;
6886 /* Variable-length argument list. */
6887 return DECL_FUNCTION_CODE (fndecl);
6890 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6891 evaluate to a constant. */
6893 static tree
6894 fold_builtin_constant_p (tree arg)
6896 /* We return 1 for a numeric type that's known to be a constant
6897 value at compile-time or for an aggregate type that's a
6898 literal constant. */
6899 STRIP_NOPS (arg);
6901 /* If we know this is a constant, emit the constant of one. */
6902 if (CONSTANT_CLASS_P (arg)
6903 || (TREE_CODE (arg) == CONSTRUCTOR
6904 && TREE_CONSTANT (arg)))
6905 return integer_one_node;
6906 if (TREE_CODE (arg) == ADDR_EXPR)
6908 tree op = TREE_OPERAND (arg, 0);
6909 if (TREE_CODE (op) == STRING_CST
6910 || (TREE_CODE (op) == ARRAY_REF
6911 && integer_zerop (TREE_OPERAND (op, 1))
6912 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6913 return integer_one_node;
6916 /* If this expression has side effects, show we don't know it to be a
6917 constant. Likewise if it's a pointer or aggregate type since in
6918 those case we only want literals, since those are only optimized
6919 when generating RTL, not later.
6920 And finally, if we are compiling an initializer, not code, we
6921 need to return a definite result now; there's not going to be any
6922 more optimization done. */
6923 if (TREE_SIDE_EFFECTS (arg)
6924 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6925 || POINTER_TYPE_P (TREE_TYPE (arg))
6926 || cfun == 0
6927 || folding_initializer
6928 || force_folding_builtin_constant_p)
6929 return integer_zero_node;
6931 return NULL_TREE;
6934 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6935 return it as a truthvalue. */
6937 static tree
6938 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
6939 tree predictor)
6941 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6943 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6944 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6945 ret_type = TREE_TYPE (TREE_TYPE (fn));
6946 pred_type = TREE_VALUE (arg_types);
6947 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6949 pred = fold_convert_loc (loc, pred_type, pred);
6950 expected = fold_convert_loc (loc, expected_type, expected);
6951 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
6952 predictor);
6954 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6955 build_int_cst (ret_type, 0));
6958 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6959 NULL_TREE if no simplification is possible. */
6961 tree
6962 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
6964 tree inner, fndecl, inner_arg0;
6965 enum tree_code code;
6967 /* Distribute the expected value over short-circuiting operators.
6968 See through the cast from truthvalue_type_node to long. */
6969 inner_arg0 = arg0;
6970 while (TREE_CODE (inner_arg0) == NOP_EXPR
6971 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
6972 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
6973 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
6975 /* If this is a builtin_expect within a builtin_expect keep the
6976 inner one. See through a comparison against a constant. It
6977 might have been added to create a thruthvalue. */
6978 inner = inner_arg0;
6980 if (COMPARISON_CLASS_P (inner)
6981 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6982 inner = TREE_OPERAND (inner, 0);
6984 if (TREE_CODE (inner) == CALL_EXPR
6985 && (fndecl = get_callee_fndecl (inner))
6986 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6987 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6988 return arg0;
6990 inner = inner_arg0;
6991 code = TREE_CODE (inner);
6992 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6994 tree op0 = TREE_OPERAND (inner, 0);
6995 tree op1 = TREE_OPERAND (inner, 1);
6997 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
6998 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
6999 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7001 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7004 /* If the argument isn't invariant then there's nothing else we can do. */
7005 if (!TREE_CONSTANT (inner_arg0))
7006 return NULL_TREE;
7008 /* If we expect that a comparison against the argument will fold to
7009 a constant return the constant. In practice, this means a true
7010 constant or the address of a non-weak symbol. */
7011 inner = inner_arg0;
7012 STRIP_NOPS (inner);
7013 if (TREE_CODE (inner) == ADDR_EXPR)
7017 inner = TREE_OPERAND (inner, 0);
7019 while (TREE_CODE (inner) == COMPONENT_REF
7020 || TREE_CODE (inner) == ARRAY_REF);
7021 if ((TREE_CODE (inner) == VAR_DECL
7022 || TREE_CODE (inner) == FUNCTION_DECL)
7023 && DECL_WEAK (inner))
7024 return NULL_TREE;
7027 /* Otherwise, ARG0 already has the proper type for the return value. */
7028 return arg0;
7031 /* Fold a call to __builtin_classify_type with argument ARG. */
7033 static tree
7034 fold_builtin_classify_type (tree arg)
7036 if (arg == 0)
7037 return build_int_cst (integer_type_node, no_type_class);
7039 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7042 /* Fold a call to __builtin_strlen with argument ARG. */
7044 static tree
7045 fold_builtin_strlen (location_t loc, tree type, tree arg)
7047 if (!validate_arg (arg, POINTER_TYPE))
7048 return NULL_TREE;
7049 else
7051 tree len = c_strlen (arg, 0);
7053 if (len)
7054 return fold_convert_loc (loc, type, len);
7056 return NULL_TREE;
7060 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7062 static tree
7063 fold_builtin_inf (location_t loc, tree type, int warn)
7065 REAL_VALUE_TYPE real;
7067 /* __builtin_inff is intended to be usable to define INFINITY on all
7068 targets. If an infinity is not available, INFINITY expands "to a
7069 positive constant of type float that overflows at translation
7070 time", footnote "In this case, using INFINITY will violate the
7071 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7072 Thus we pedwarn to ensure this constraint violation is
7073 diagnosed. */
7074 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7075 pedwarn (loc, 0, "target format does not support infinity");
7077 real_inf (&real);
7078 return build_real (type, real);
7081 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7083 static tree
7084 fold_builtin_nan (tree arg, tree type, int quiet)
7086 REAL_VALUE_TYPE real;
7087 const char *str;
7089 if (!validate_arg (arg, POINTER_TYPE))
7090 return NULL_TREE;
7091 str = c_getstr (arg);
7092 if (!str)
7093 return NULL_TREE;
7095 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7096 return NULL_TREE;
7098 return build_real (type, real);
7101 /* Return true if the floating point expression T has an integer value.
7102 We also allow +Inf, -Inf and NaN to be considered integer values. */
7104 static bool
7105 integer_valued_real_p (tree t)
7107 switch (TREE_CODE (t))
7109 case FLOAT_EXPR:
7110 return true;
7112 case ABS_EXPR:
7113 case SAVE_EXPR:
7114 return integer_valued_real_p (TREE_OPERAND (t, 0));
7116 case COMPOUND_EXPR:
7117 case MODIFY_EXPR:
7118 case BIND_EXPR:
7119 return integer_valued_real_p (TREE_OPERAND (t, 1));
7121 case PLUS_EXPR:
7122 case MINUS_EXPR:
7123 case MULT_EXPR:
7124 case MIN_EXPR:
7125 case MAX_EXPR:
7126 return integer_valued_real_p (TREE_OPERAND (t, 0))
7127 && integer_valued_real_p (TREE_OPERAND (t, 1));
7129 case COND_EXPR:
7130 return integer_valued_real_p (TREE_OPERAND (t, 1))
7131 && integer_valued_real_p (TREE_OPERAND (t, 2));
7133 case REAL_CST:
7134 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7136 case NOP_EXPR:
7138 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7139 if (TREE_CODE (type) == INTEGER_TYPE)
7140 return true;
7141 if (TREE_CODE (type) == REAL_TYPE)
7142 return integer_valued_real_p (TREE_OPERAND (t, 0));
7143 break;
7146 case CALL_EXPR:
7147 switch (builtin_mathfn_code (t))
7149 CASE_FLT_FN (BUILT_IN_CEIL):
7150 CASE_FLT_FN (BUILT_IN_FLOOR):
7151 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7152 CASE_FLT_FN (BUILT_IN_RINT):
7153 CASE_FLT_FN (BUILT_IN_ROUND):
7154 CASE_FLT_FN (BUILT_IN_TRUNC):
7155 return true;
7157 CASE_FLT_FN (BUILT_IN_FMIN):
7158 CASE_FLT_FN (BUILT_IN_FMAX):
7159 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7160 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7162 default:
7163 break;
7165 break;
7167 default:
7168 break;
7170 return false;
7173 /* FNDECL is assumed to be a builtin where truncation can be propagated
7174 across (for instance floor((double)f) == (double)floorf (f).
7175 Do the transformation for a call with argument ARG. */
7177 static tree
7178 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7180 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7182 if (!validate_arg (arg, REAL_TYPE))
7183 return NULL_TREE;
7185 /* Integer rounding functions are idempotent. */
7186 if (fcode == builtin_mathfn_code (arg))
7187 return arg;
7189 /* If argument is already integer valued, and we don't need to worry
7190 about setting errno, there's no need to perform rounding. */
7191 if (! flag_errno_math && integer_valued_real_p (arg))
7192 return arg;
7194 if (optimize)
7196 tree arg0 = strip_float_extensions (arg);
7197 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7198 tree newtype = TREE_TYPE (arg0);
7199 tree decl;
7201 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7202 && (decl = mathfn_built_in (newtype, fcode)))
7203 return fold_convert_loc (loc, ftype,
7204 build_call_expr_loc (loc, decl, 1,
7205 fold_convert_loc (loc,
7206 newtype,
7207 arg0)));
7209 return NULL_TREE;
7212 /* FNDECL is assumed to be builtin which can narrow the FP type of
7213 the argument, for instance lround((double)f) -> lroundf (f).
7214 Do the transformation for a call with argument ARG. */
7216 static tree
7217 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7219 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7221 if (!validate_arg (arg, REAL_TYPE))
7222 return NULL_TREE;
7224 /* If argument is already integer valued, and we don't need to worry
7225 about setting errno, there's no need to perform rounding. */
7226 if (! flag_errno_math && integer_valued_real_p (arg))
7227 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7228 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7230 if (optimize)
7232 tree ftype = TREE_TYPE (arg);
7233 tree arg0 = strip_float_extensions (arg);
7234 tree newtype = TREE_TYPE (arg0);
7235 tree decl;
7237 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7238 && (decl = mathfn_built_in (newtype, fcode)))
7239 return build_call_expr_loc (loc, decl, 1,
7240 fold_convert_loc (loc, newtype, arg0));
7243 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7244 sizeof (int) == sizeof (long). */
7245 if (TYPE_PRECISION (integer_type_node)
7246 == TYPE_PRECISION (long_integer_type_node))
7248 tree newfn = NULL_TREE;
7249 switch (fcode)
7251 CASE_FLT_FN (BUILT_IN_ICEIL):
7252 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7253 break;
7255 CASE_FLT_FN (BUILT_IN_IFLOOR):
7256 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7257 break;
7259 CASE_FLT_FN (BUILT_IN_IROUND):
7260 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7261 break;
7263 CASE_FLT_FN (BUILT_IN_IRINT):
7264 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7265 break;
7267 default:
7268 break;
7271 if (newfn)
7273 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7274 return fold_convert_loc (loc,
7275 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7279 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7280 sizeof (long long) == sizeof (long). */
7281 if (TYPE_PRECISION (long_long_integer_type_node)
7282 == TYPE_PRECISION (long_integer_type_node))
7284 tree newfn = NULL_TREE;
7285 switch (fcode)
7287 CASE_FLT_FN (BUILT_IN_LLCEIL):
7288 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7289 break;
7291 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7292 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7293 break;
7295 CASE_FLT_FN (BUILT_IN_LLROUND):
7296 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7297 break;
7299 CASE_FLT_FN (BUILT_IN_LLRINT):
7300 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7301 break;
7303 default:
7304 break;
7307 if (newfn)
7309 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7310 return fold_convert_loc (loc,
7311 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7315 return NULL_TREE;
7318 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7319 return type. Return NULL_TREE if no simplification can be made. */
7321 static tree
7322 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7324 tree res;
7326 if (!validate_arg (arg, COMPLEX_TYPE)
7327 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7328 return NULL_TREE;
7330 /* Calculate the result when the argument is a constant. */
7331 if (TREE_CODE (arg) == COMPLEX_CST
7332 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7333 type, mpfr_hypot)))
7334 return res;
7336 if (TREE_CODE (arg) == COMPLEX_EXPR)
7338 tree real = TREE_OPERAND (arg, 0);
7339 tree imag = TREE_OPERAND (arg, 1);
7341 /* If either part is zero, cabs is fabs of the other. */
7342 if (real_zerop (real))
7343 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7344 if (real_zerop (imag))
7345 return fold_build1_loc (loc, ABS_EXPR, type, real);
7347 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7348 if (flag_unsafe_math_optimizations
7349 && operand_equal_p (real, imag, OEP_PURE_SAME))
7351 const REAL_VALUE_TYPE sqrt2_trunc
7352 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7353 STRIP_NOPS (real);
7354 return fold_build2_loc (loc, MULT_EXPR, type,
7355 fold_build1_loc (loc, ABS_EXPR, type, real),
7356 build_real (type, sqrt2_trunc));
7360 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7361 if (TREE_CODE (arg) == NEGATE_EXPR
7362 || TREE_CODE (arg) == CONJ_EXPR)
7363 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7365 /* Don't do this when optimizing for size. */
7366 if (flag_unsafe_math_optimizations
7367 && optimize && optimize_function_for_speed_p (cfun))
7369 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7371 if (sqrtfn != NULL_TREE)
7373 tree rpart, ipart, result;
7375 arg = builtin_save_expr (arg);
7377 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7378 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7380 rpart = builtin_save_expr (rpart);
7381 ipart = builtin_save_expr (ipart);
7383 result = fold_build2_loc (loc, PLUS_EXPR, type,
7384 fold_build2_loc (loc, MULT_EXPR, type,
7385 rpart, rpart),
7386 fold_build2_loc (loc, MULT_EXPR, type,
7387 ipart, ipart));
7389 return build_call_expr_loc (loc, sqrtfn, 1, result);
7393 return NULL_TREE;
7396 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7397 complex tree type of the result. If NEG is true, the imaginary
7398 zero is negative. */
7400 static tree
7401 build_complex_cproj (tree type, bool neg)
7403 REAL_VALUE_TYPE rinf, rzero = dconst0;
7405 real_inf (&rinf);
7406 rzero.sign = neg;
7407 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7408 build_real (TREE_TYPE (type), rzero));
7411 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7412 return type. Return NULL_TREE if no simplification can be made. */
7414 static tree
7415 fold_builtin_cproj (location_t loc, tree arg, tree type)
7417 if (!validate_arg (arg, COMPLEX_TYPE)
7418 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7419 return NULL_TREE;
7421 /* If there are no infinities, return arg. */
7422 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7423 return non_lvalue_loc (loc, arg);
7425 /* Calculate the result when the argument is a constant. */
7426 if (TREE_CODE (arg) == COMPLEX_CST)
7428 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7429 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7431 if (real_isinf (real) || real_isinf (imag))
7432 return build_complex_cproj (type, imag->sign);
7433 else
7434 return arg;
7436 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7438 tree real = TREE_OPERAND (arg, 0);
7439 tree imag = TREE_OPERAND (arg, 1);
7441 STRIP_NOPS (real);
7442 STRIP_NOPS (imag);
7444 /* If the real part is inf and the imag part is known to be
7445 nonnegative, return (inf + 0i). Remember side-effects are
7446 possible in the imag part. */
7447 if (TREE_CODE (real) == REAL_CST
7448 && real_isinf (TREE_REAL_CST_PTR (real))
7449 && tree_expr_nonnegative_p (imag))
7450 return omit_one_operand_loc (loc, type,
7451 build_complex_cproj (type, false),
7452 arg);
7454 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7455 Remember side-effects are possible in the real part. */
7456 if (TREE_CODE (imag) == REAL_CST
7457 && real_isinf (TREE_REAL_CST_PTR (imag)))
7458 return
7459 omit_one_operand_loc (loc, type,
7460 build_complex_cproj (type, TREE_REAL_CST_PTR
7461 (imag)->sign), arg);
7464 return NULL_TREE;
7467 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7468 Return NULL_TREE if no simplification can be made. */
7470 static tree
7471 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7474 enum built_in_function fcode;
7475 tree res;
7477 if (!validate_arg (arg, REAL_TYPE))
7478 return NULL_TREE;
7480 /* Calculate the result when the argument is a constant. */
7481 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7482 return res;
7484 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7485 fcode = builtin_mathfn_code (arg);
7486 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7488 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7489 arg = fold_build2_loc (loc, MULT_EXPR, type,
7490 CALL_EXPR_ARG (arg, 0),
7491 build_real (type, dconsthalf));
7492 return build_call_expr_loc (loc, expfn, 1, arg);
7495 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7496 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7498 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7500 if (powfn)
7502 tree arg0 = CALL_EXPR_ARG (arg, 0);
7503 tree tree_root;
7504 /* The inner root was either sqrt or cbrt. */
7505 /* This was a conditional expression but it triggered a bug
7506 in Sun C 5.5. */
7507 REAL_VALUE_TYPE dconstroot;
7508 if (BUILTIN_SQRT_P (fcode))
7509 dconstroot = dconsthalf;
7510 else
7511 dconstroot = dconst_third ();
7513 /* Adjust for the outer root. */
7514 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7515 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7516 tree_root = build_real (type, dconstroot);
7517 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7521 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7522 if (flag_unsafe_math_optimizations
7523 && (fcode == BUILT_IN_POW
7524 || fcode == BUILT_IN_POWF
7525 || fcode == BUILT_IN_POWL))
7527 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7528 tree arg0 = CALL_EXPR_ARG (arg, 0);
7529 tree arg1 = CALL_EXPR_ARG (arg, 1);
7530 tree narg1;
7531 if (!tree_expr_nonnegative_p (arg0))
7532 arg0 = build1 (ABS_EXPR, type, arg0);
7533 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7534 build_real (type, dconsthalf));
7535 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7538 return NULL_TREE;
7541 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7542 Return NULL_TREE if no simplification can be made. */
7544 static tree
7545 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7547 const enum built_in_function fcode = builtin_mathfn_code (arg);
7548 tree res;
7550 if (!validate_arg (arg, REAL_TYPE))
7551 return NULL_TREE;
7553 /* Calculate the result when the argument is a constant. */
7554 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7555 return res;
7557 if (flag_unsafe_math_optimizations)
7559 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7560 if (BUILTIN_EXPONENT_P (fcode))
7562 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7563 const REAL_VALUE_TYPE third_trunc =
7564 real_value_truncate (TYPE_MODE (type), dconst_third ());
7565 arg = fold_build2_loc (loc, MULT_EXPR, type,
7566 CALL_EXPR_ARG (arg, 0),
7567 build_real (type, third_trunc));
7568 return build_call_expr_loc (loc, expfn, 1, arg);
7571 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7572 if (BUILTIN_SQRT_P (fcode))
7574 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7576 if (powfn)
7578 tree arg0 = CALL_EXPR_ARG (arg, 0);
7579 tree tree_root;
7580 REAL_VALUE_TYPE dconstroot = dconst_third ();
7582 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7583 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7584 tree_root = build_real (type, dconstroot);
7585 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7589 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7590 if (BUILTIN_CBRT_P (fcode))
7592 tree arg0 = CALL_EXPR_ARG (arg, 0);
7593 if (tree_expr_nonnegative_p (arg0))
7595 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7597 if (powfn)
7599 tree tree_root;
7600 REAL_VALUE_TYPE dconstroot;
7602 real_arithmetic (&dconstroot, MULT_EXPR,
7603 dconst_third_ptr (), dconst_third_ptr ());
7604 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7605 tree_root = build_real (type, dconstroot);
7606 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7611 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7612 if (fcode == BUILT_IN_POW
7613 || fcode == BUILT_IN_POWF
7614 || fcode == BUILT_IN_POWL)
7616 tree arg00 = CALL_EXPR_ARG (arg, 0);
7617 tree arg01 = CALL_EXPR_ARG (arg, 1);
7618 if (tree_expr_nonnegative_p (arg00))
7620 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7621 const REAL_VALUE_TYPE dconstroot
7622 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7623 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7624 build_real (type, dconstroot));
7625 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7629 return NULL_TREE;
7632 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7633 TYPE is the type of the return value. Return NULL_TREE if no
7634 simplification can be made. */
7636 static tree
7637 fold_builtin_cos (location_t loc,
7638 tree arg, tree type, tree fndecl)
7640 tree res, narg;
7642 if (!validate_arg (arg, REAL_TYPE))
7643 return NULL_TREE;
7645 /* Calculate the result when the argument is a constant. */
7646 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7647 return res;
7649 /* Optimize cos(-x) into cos (x). */
7650 if ((narg = fold_strip_sign_ops (arg)))
7651 return build_call_expr_loc (loc, fndecl, 1, narg);
7653 return NULL_TREE;
7656 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7657 Return NULL_TREE if no simplification can be made. */
7659 static tree
7660 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7662 if (validate_arg (arg, REAL_TYPE))
7664 tree res, narg;
7666 /* Calculate the result when the argument is a constant. */
7667 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7668 return res;
7670 /* Optimize cosh(-x) into cosh (x). */
7671 if ((narg = fold_strip_sign_ops (arg)))
7672 return build_call_expr_loc (loc, fndecl, 1, narg);
7675 return NULL_TREE;
7678 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7679 argument ARG. TYPE is the type of the return value. Return
7680 NULL_TREE if no simplification can be made. */
7682 static tree
7683 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7684 bool hyper)
7686 if (validate_arg (arg, COMPLEX_TYPE)
7687 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7689 tree tmp;
7691 /* Calculate the result when the argument is a constant. */
7692 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7693 return tmp;
7695 /* Optimize fn(-x) into fn(x). */
7696 if ((tmp = fold_strip_sign_ops (arg)))
7697 return build_call_expr_loc (loc, fndecl, 1, tmp);
7700 return NULL_TREE;
7703 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7704 Return NULL_TREE if no simplification can be made. */
7706 static tree
7707 fold_builtin_tan (tree arg, tree type)
7709 enum built_in_function fcode;
7710 tree res;
7712 if (!validate_arg (arg, REAL_TYPE))
7713 return NULL_TREE;
7715 /* Calculate the result when the argument is a constant. */
7716 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7717 return res;
7719 /* Optimize tan(atan(x)) = x. */
7720 fcode = builtin_mathfn_code (arg);
7721 if (flag_unsafe_math_optimizations
7722 && (fcode == BUILT_IN_ATAN
7723 || fcode == BUILT_IN_ATANF
7724 || fcode == BUILT_IN_ATANL))
7725 return CALL_EXPR_ARG (arg, 0);
7727 return NULL_TREE;
7730 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7731 NULL_TREE if no simplification can be made. */
7733 static tree
7734 fold_builtin_sincos (location_t loc,
7735 tree arg0, tree arg1, tree arg2)
7737 tree type;
7738 tree res, fn, call;
7740 if (!validate_arg (arg0, REAL_TYPE)
7741 || !validate_arg (arg1, POINTER_TYPE)
7742 || !validate_arg (arg2, POINTER_TYPE))
7743 return NULL_TREE;
7745 type = TREE_TYPE (arg0);
7747 /* Calculate the result when the argument is a constant. */
7748 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7749 return res;
7751 /* Canonicalize sincos to cexpi. */
7752 if (!targetm.libc_has_function (function_c99_math_complex))
7753 return NULL_TREE;
7754 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7755 if (!fn)
7756 return NULL_TREE;
7758 call = build_call_expr_loc (loc, fn, 1, arg0);
7759 call = builtin_save_expr (call);
7761 return build2 (COMPOUND_EXPR, void_type_node,
7762 build2 (MODIFY_EXPR, void_type_node,
7763 build_fold_indirect_ref_loc (loc, arg1),
7764 build1 (IMAGPART_EXPR, type, call)),
7765 build2 (MODIFY_EXPR, void_type_node,
7766 build_fold_indirect_ref_loc (loc, arg2),
7767 build1 (REALPART_EXPR, type, call)));
7770 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7771 NULL_TREE if no simplification can be made. */
7773 static tree
7774 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7776 tree rtype;
7777 tree realp, imagp, ifn;
7778 tree res;
7780 if (!validate_arg (arg0, COMPLEX_TYPE)
7781 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7782 return NULL_TREE;
7784 /* Calculate the result when the argument is a constant. */
7785 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7786 return res;
7788 rtype = TREE_TYPE (TREE_TYPE (arg0));
7790 /* In case we can figure out the real part of arg0 and it is constant zero
7791 fold to cexpi. */
7792 if (!targetm.libc_has_function (function_c99_math_complex))
7793 return NULL_TREE;
7794 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7795 if (!ifn)
7796 return NULL_TREE;
7798 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7799 && real_zerop (realp))
7801 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7802 return build_call_expr_loc (loc, ifn, 1, narg);
7805 /* In case we can easily decompose real and imaginary parts split cexp
7806 to exp (r) * cexpi (i). */
7807 if (flag_unsafe_math_optimizations
7808 && realp)
7810 tree rfn, rcall, icall;
7812 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7813 if (!rfn)
7814 return NULL_TREE;
7816 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7817 if (!imagp)
7818 return NULL_TREE;
7820 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7821 icall = builtin_save_expr (icall);
7822 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7823 rcall = builtin_save_expr (rcall);
7824 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7825 fold_build2_loc (loc, MULT_EXPR, rtype,
7826 rcall,
7827 fold_build1_loc (loc, REALPART_EXPR,
7828 rtype, icall)),
7829 fold_build2_loc (loc, MULT_EXPR, rtype,
7830 rcall,
7831 fold_build1_loc (loc, IMAGPART_EXPR,
7832 rtype, icall)));
7835 return NULL_TREE;
7838 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7839 Return NULL_TREE if no simplification can be made. */
7841 static tree
7842 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7844 if (!validate_arg (arg, REAL_TYPE))
7845 return NULL_TREE;
7847 /* Optimize trunc of constant value. */
7848 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7850 REAL_VALUE_TYPE r, x;
7851 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7853 x = TREE_REAL_CST (arg);
7854 real_trunc (&r, TYPE_MODE (type), &x);
7855 return build_real (type, r);
7858 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7861 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7862 Return NULL_TREE if no simplification can be made. */
7864 static tree
7865 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7867 if (!validate_arg (arg, REAL_TYPE))
7868 return NULL_TREE;
7870 /* Optimize floor of constant value. */
7871 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7873 REAL_VALUE_TYPE x;
7875 x = TREE_REAL_CST (arg);
7876 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7878 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7879 REAL_VALUE_TYPE r;
7881 real_floor (&r, TYPE_MODE (type), &x);
7882 return build_real (type, r);
7886 /* Fold floor (x) where x is nonnegative to trunc (x). */
7887 if (tree_expr_nonnegative_p (arg))
7889 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7890 if (truncfn)
7891 return build_call_expr_loc (loc, truncfn, 1, arg);
7894 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7897 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7898 Return NULL_TREE if no simplification can be made. */
7900 static tree
7901 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7903 if (!validate_arg (arg, REAL_TYPE))
7904 return NULL_TREE;
7906 /* Optimize ceil of constant value. */
7907 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7909 REAL_VALUE_TYPE x;
7911 x = TREE_REAL_CST (arg);
7912 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7914 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7915 REAL_VALUE_TYPE r;
7917 real_ceil (&r, TYPE_MODE (type), &x);
7918 return build_real (type, r);
7922 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7925 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7926 Return NULL_TREE if no simplification can be made. */
7928 static tree
7929 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7931 if (!validate_arg (arg, REAL_TYPE))
7932 return NULL_TREE;
7934 /* Optimize round of constant value. */
7935 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7937 REAL_VALUE_TYPE x;
7939 x = TREE_REAL_CST (arg);
7940 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7942 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7943 REAL_VALUE_TYPE r;
7945 real_round (&r, TYPE_MODE (type), &x);
7946 return build_real (type, r);
7950 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7953 /* Fold function call to builtin lround, lroundf or lroundl (or the
7954 corresponding long long versions) and other rounding functions. ARG
7955 is the argument to the call. Return NULL_TREE if no simplification
7956 can be made. */
7958 static tree
7959 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7961 if (!validate_arg (arg, REAL_TYPE))
7962 return NULL_TREE;
7964 /* Optimize lround of constant value. */
7965 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7967 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7969 if (real_isfinite (&x))
7971 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7972 tree ftype = TREE_TYPE (arg);
7973 REAL_VALUE_TYPE r;
7974 bool fail = false;
7976 switch (DECL_FUNCTION_CODE (fndecl))
7978 CASE_FLT_FN (BUILT_IN_IFLOOR):
7979 CASE_FLT_FN (BUILT_IN_LFLOOR):
7980 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7981 real_floor (&r, TYPE_MODE (ftype), &x);
7982 break;
7984 CASE_FLT_FN (BUILT_IN_ICEIL):
7985 CASE_FLT_FN (BUILT_IN_LCEIL):
7986 CASE_FLT_FN (BUILT_IN_LLCEIL):
7987 real_ceil (&r, TYPE_MODE (ftype), &x);
7988 break;
7990 CASE_FLT_FN (BUILT_IN_IROUND):
7991 CASE_FLT_FN (BUILT_IN_LROUND):
7992 CASE_FLT_FN (BUILT_IN_LLROUND):
7993 real_round (&r, TYPE_MODE (ftype), &x);
7994 break;
7996 default:
7997 gcc_unreachable ();
8000 wide_int val = real_to_integer (&r, &fail, TYPE_PRECISION (itype));
8001 if (!fail)
8002 return wide_int_to_tree (itype, val);
8006 switch (DECL_FUNCTION_CODE (fndecl))
8008 CASE_FLT_FN (BUILT_IN_LFLOOR):
8009 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8010 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8011 if (tree_expr_nonnegative_p (arg))
8012 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8013 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8014 break;
8015 default:;
8018 return fold_fixed_mathfn (loc, fndecl, arg);
8021 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8022 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8023 the argument to the call. Return NULL_TREE if no simplification can
8024 be made. */
8026 static tree
8027 fold_builtin_bitop (tree fndecl, tree arg)
8029 if (!validate_arg (arg, INTEGER_TYPE))
8030 return NULL_TREE;
8032 /* Optimize for constant argument. */
8033 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8035 tree type = TREE_TYPE (arg);
8036 int result;
8038 switch (DECL_FUNCTION_CODE (fndecl))
8040 CASE_INT_FN (BUILT_IN_FFS):
8041 result = wi::ffs (arg);
8042 break;
8044 CASE_INT_FN (BUILT_IN_CLZ):
8045 if (wi::ne_p (arg, 0))
8046 result = wi::clz (arg);
8047 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8048 result = TYPE_PRECISION (type);
8049 break;
8051 CASE_INT_FN (BUILT_IN_CTZ):
8052 if (wi::ne_p (arg, 0))
8053 result = wi::ctz (arg);
8054 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8055 result = TYPE_PRECISION (type);
8056 break;
8058 CASE_INT_FN (BUILT_IN_CLRSB):
8059 result = wi::clrsb (arg);
8060 break;
8062 CASE_INT_FN (BUILT_IN_POPCOUNT):
8063 result = wi::popcount (arg);
8064 break;
8066 CASE_INT_FN (BUILT_IN_PARITY):
8067 result = wi::parity (arg);
8068 break;
8070 default:
8071 gcc_unreachable ();
8074 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8077 return NULL_TREE;
8080 /* Fold function call to builtin_bswap and the short, long and long long
8081 variants. Return NULL_TREE if no simplification can be made. */
8082 static tree
8083 fold_builtin_bswap (tree fndecl, tree arg)
8085 if (! validate_arg (arg, INTEGER_TYPE))
8086 return NULL_TREE;
8088 /* Optimize constant value. */
8089 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8091 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8093 switch (DECL_FUNCTION_CODE (fndecl))
8095 case BUILT_IN_BSWAP16:
8096 case BUILT_IN_BSWAP32:
8097 case BUILT_IN_BSWAP64:
8099 signop sgn = TYPE_SIGN (type);
8100 tree result =
8101 wide_int_to_tree (type,
8102 wide_int::from (arg, TYPE_PRECISION (type),
8103 sgn).bswap ());
8104 return result;
8106 default:
8107 gcc_unreachable ();
8111 return NULL_TREE;
8114 /* A subroutine of fold_builtin to fold the various logarithmic
8115 functions. Return NULL_TREE if no simplification can me made.
8116 FUNC is the corresponding MPFR logarithm function. */
8118 static tree
8119 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8120 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8122 if (validate_arg (arg, REAL_TYPE))
8124 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8125 tree res;
8126 const enum built_in_function fcode = builtin_mathfn_code (arg);
8128 /* Calculate the result when the argument is a constant. */
8129 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8130 return res;
8132 /* Special case, optimize logN(expN(x)) = x. */
8133 if (flag_unsafe_math_optimizations
8134 && ((func == mpfr_log
8135 && (fcode == BUILT_IN_EXP
8136 || fcode == BUILT_IN_EXPF
8137 || fcode == BUILT_IN_EXPL))
8138 || (func == mpfr_log2
8139 && (fcode == BUILT_IN_EXP2
8140 || fcode == BUILT_IN_EXP2F
8141 || fcode == BUILT_IN_EXP2L))
8142 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8143 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8145 /* Optimize logN(func()) for various exponential functions. We
8146 want to determine the value "x" and the power "exponent" in
8147 order to transform logN(x**exponent) into exponent*logN(x). */
8148 if (flag_unsafe_math_optimizations)
8150 tree exponent = 0, x = 0;
8152 switch (fcode)
8154 CASE_FLT_FN (BUILT_IN_EXP):
8155 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8156 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8157 dconst_e ()));
8158 exponent = CALL_EXPR_ARG (arg, 0);
8159 break;
8160 CASE_FLT_FN (BUILT_IN_EXP2):
8161 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8162 x = build_real (type, dconst2);
8163 exponent = CALL_EXPR_ARG (arg, 0);
8164 break;
8165 CASE_FLT_FN (BUILT_IN_EXP10):
8166 CASE_FLT_FN (BUILT_IN_POW10):
8167 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8169 REAL_VALUE_TYPE dconst10;
8170 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
8171 x = build_real (type, dconst10);
8173 exponent = CALL_EXPR_ARG (arg, 0);
8174 break;
8175 CASE_FLT_FN (BUILT_IN_SQRT):
8176 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8177 x = CALL_EXPR_ARG (arg, 0);
8178 exponent = build_real (type, dconsthalf);
8179 break;
8180 CASE_FLT_FN (BUILT_IN_CBRT):
8181 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8182 x = CALL_EXPR_ARG (arg, 0);
8183 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8184 dconst_third ()));
8185 break;
8186 CASE_FLT_FN (BUILT_IN_POW):
8187 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8188 x = CALL_EXPR_ARG (arg, 0);
8189 exponent = CALL_EXPR_ARG (arg, 1);
8190 break;
8191 default:
8192 break;
8195 /* Now perform the optimization. */
8196 if (x && exponent)
8198 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8199 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8204 return NULL_TREE;
8207 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8208 NULL_TREE if no simplification can be made. */
8210 static tree
8211 fold_builtin_hypot (location_t loc, tree fndecl,
8212 tree arg0, tree arg1, tree type)
8214 tree res, narg0, narg1;
8216 if (!validate_arg (arg0, REAL_TYPE)
8217 || !validate_arg (arg1, REAL_TYPE))
8218 return NULL_TREE;
8220 /* Calculate the result when the argument is a constant. */
8221 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8222 return res;
8224 /* If either argument to hypot has a negate or abs, strip that off.
8225 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8226 narg0 = fold_strip_sign_ops (arg0);
8227 narg1 = fold_strip_sign_ops (arg1);
8228 if (narg0 || narg1)
8230 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8231 narg1 ? narg1 : arg1);
8234 /* If either argument is zero, hypot is fabs of the other. */
8235 if (real_zerop (arg0))
8236 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8237 else if (real_zerop (arg1))
8238 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8240 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8241 if (flag_unsafe_math_optimizations
8242 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8244 const REAL_VALUE_TYPE sqrt2_trunc
8245 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8246 return fold_build2_loc (loc, MULT_EXPR, type,
8247 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8248 build_real (type, sqrt2_trunc));
8251 return NULL_TREE;
8255 /* Fold a builtin function call to pow, powf, or powl. Return
8256 NULL_TREE if no simplification can be made. */
8257 static tree
8258 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8260 tree res;
8262 if (!validate_arg (arg0, REAL_TYPE)
8263 || !validate_arg (arg1, REAL_TYPE))
8264 return NULL_TREE;
8266 /* Calculate the result when the argument is a constant. */
8267 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8268 return res;
8270 /* Optimize pow(1.0,y) = 1.0. */
8271 if (real_onep (arg0))
8272 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8274 if (TREE_CODE (arg1) == REAL_CST
8275 && !TREE_OVERFLOW (arg1))
8277 REAL_VALUE_TYPE cint;
8278 REAL_VALUE_TYPE c;
8279 HOST_WIDE_INT n;
8281 c = TREE_REAL_CST (arg1);
8283 /* Optimize pow(x,0.0) = 1.0. */
8284 if (REAL_VALUES_EQUAL (c, dconst0))
8285 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8286 arg0);
8288 /* Optimize pow(x,1.0) = x. */
8289 if (REAL_VALUES_EQUAL (c, dconst1))
8290 return arg0;
8292 /* Optimize pow(x,-1.0) = 1.0/x. */
8293 if (REAL_VALUES_EQUAL (c, dconstm1))
8294 return fold_build2_loc (loc, RDIV_EXPR, type,
8295 build_real (type, dconst1), arg0);
8297 /* Optimize pow(x,0.5) = sqrt(x). */
8298 if (flag_unsafe_math_optimizations
8299 && REAL_VALUES_EQUAL (c, dconsthalf))
8301 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8303 if (sqrtfn != NULL_TREE)
8304 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8307 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8308 if (flag_unsafe_math_optimizations)
8310 const REAL_VALUE_TYPE dconstroot
8311 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8313 if (REAL_VALUES_EQUAL (c, dconstroot))
8315 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8316 if (cbrtfn != NULL_TREE)
8317 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8321 /* Check for an integer exponent. */
8322 n = real_to_integer (&c);
8323 real_from_integer (&cint, VOIDmode, n, SIGNED);
8324 if (real_identical (&c, &cint))
8326 /* Attempt to evaluate pow at compile-time, unless this should
8327 raise an exception. */
8328 if (TREE_CODE (arg0) == REAL_CST
8329 && !TREE_OVERFLOW (arg0)
8330 && (n > 0
8331 || (!flag_trapping_math && !flag_errno_math)
8332 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8334 REAL_VALUE_TYPE x;
8335 bool inexact;
8337 x = TREE_REAL_CST (arg0);
8338 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8339 if (flag_unsafe_math_optimizations || !inexact)
8340 return build_real (type, x);
8343 /* Strip sign ops from even integer powers. */
8344 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8346 tree narg0 = fold_strip_sign_ops (arg0);
8347 if (narg0)
8348 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8353 if (flag_unsafe_math_optimizations)
8355 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8357 /* Optimize pow(expN(x),y) = expN(x*y). */
8358 if (BUILTIN_EXPONENT_P (fcode))
8360 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8361 tree arg = CALL_EXPR_ARG (arg0, 0);
8362 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8363 return build_call_expr_loc (loc, expfn, 1, arg);
8366 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8367 if (BUILTIN_SQRT_P (fcode))
8369 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8370 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8371 build_real (type, dconsthalf));
8372 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8375 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8376 if (BUILTIN_CBRT_P (fcode))
8378 tree arg = CALL_EXPR_ARG (arg0, 0);
8379 if (tree_expr_nonnegative_p (arg))
8381 const REAL_VALUE_TYPE dconstroot
8382 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8383 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8384 build_real (type, dconstroot));
8385 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8389 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8390 if (fcode == BUILT_IN_POW
8391 || fcode == BUILT_IN_POWF
8392 || fcode == BUILT_IN_POWL)
8394 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8395 if (tree_expr_nonnegative_p (arg00))
8397 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8398 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8399 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8404 return NULL_TREE;
8407 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8408 Return NULL_TREE if no simplification can be made. */
8409 static tree
8410 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8411 tree arg0, tree arg1, tree type)
8413 if (!validate_arg (arg0, REAL_TYPE)
8414 || !validate_arg (arg1, INTEGER_TYPE))
8415 return NULL_TREE;
8417 /* Optimize pow(1.0,y) = 1.0. */
8418 if (real_onep (arg0))
8419 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8421 if (tree_fits_shwi_p (arg1))
8423 HOST_WIDE_INT c = tree_to_shwi (arg1);
8425 /* Evaluate powi at compile-time. */
8426 if (TREE_CODE (arg0) == REAL_CST
8427 && !TREE_OVERFLOW (arg0))
8429 REAL_VALUE_TYPE x;
8430 x = TREE_REAL_CST (arg0);
8431 real_powi (&x, TYPE_MODE (type), &x, c);
8432 return build_real (type, x);
8435 /* Optimize pow(x,0) = 1.0. */
8436 if (c == 0)
8437 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8438 arg0);
8440 /* Optimize pow(x,1) = x. */
8441 if (c == 1)
8442 return arg0;
8444 /* Optimize pow(x,-1) = 1.0/x. */
8445 if (c == -1)
8446 return fold_build2_loc (loc, RDIV_EXPR, type,
8447 build_real (type, dconst1), arg0);
8450 return NULL_TREE;
8453 /* A subroutine of fold_builtin to fold the various exponent
8454 functions. Return NULL_TREE if no simplification can be made.
8455 FUNC is the corresponding MPFR exponent function. */
8457 static tree
8458 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8459 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8461 if (validate_arg (arg, REAL_TYPE))
8463 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8464 tree res;
8466 /* Calculate the result when the argument is a constant. */
8467 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8468 return res;
8470 /* Optimize expN(logN(x)) = x. */
8471 if (flag_unsafe_math_optimizations)
8473 const enum built_in_function fcode = builtin_mathfn_code (arg);
8475 if ((func == mpfr_exp
8476 && (fcode == BUILT_IN_LOG
8477 || fcode == BUILT_IN_LOGF
8478 || fcode == BUILT_IN_LOGL))
8479 || (func == mpfr_exp2
8480 && (fcode == BUILT_IN_LOG2
8481 || fcode == BUILT_IN_LOG2F
8482 || fcode == BUILT_IN_LOG2L))
8483 || (func == mpfr_exp10
8484 && (fcode == BUILT_IN_LOG10
8485 || fcode == BUILT_IN_LOG10F
8486 || fcode == BUILT_IN_LOG10L)))
8487 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8491 return NULL_TREE;
8494 /* Return true if VAR is a VAR_DECL or a component thereof. */
8496 static bool
8497 var_decl_component_p (tree var)
8499 tree inner = var;
8500 while (handled_component_p (inner))
8501 inner = TREE_OPERAND (inner, 0);
8502 return SSA_VAR_P (inner);
8505 /* Fold function call to builtin memset. Return
8506 NULL_TREE if no simplification can be made. */
8508 static tree
8509 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8510 tree type, bool ignore)
8512 tree var, ret, etype;
8513 unsigned HOST_WIDE_INT length, cval;
8515 if (! validate_arg (dest, POINTER_TYPE)
8516 || ! validate_arg (c, INTEGER_TYPE)
8517 || ! validate_arg (len, INTEGER_TYPE))
8518 return NULL_TREE;
8520 if (! tree_fits_uhwi_p (len))
8521 return NULL_TREE;
8523 /* If the LEN parameter is zero, return DEST. */
8524 if (integer_zerop (len))
8525 return omit_one_operand_loc (loc, type, dest, c);
8527 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8528 return NULL_TREE;
8530 var = dest;
8531 STRIP_NOPS (var);
8532 if (TREE_CODE (var) != ADDR_EXPR)
8533 return NULL_TREE;
8535 var = TREE_OPERAND (var, 0);
8536 if (TREE_THIS_VOLATILE (var))
8537 return NULL_TREE;
8539 etype = TREE_TYPE (var);
8540 if (TREE_CODE (etype) == ARRAY_TYPE)
8541 etype = TREE_TYPE (etype);
8543 if (!INTEGRAL_TYPE_P (etype)
8544 && !POINTER_TYPE_P (etype))
8545 return NULL_TREE;
8547 if (! var_decl_component_p (var))
8548 return NULL_TREE;
8550 length = tree_to_uhwi (len);
8551 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8552 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8553 return NULL_TREE;
8555 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8556 return NULL_TREE;
8558 if (integer_zerop (c))
8559 cval = 0;
8560 else
8562 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8563 return NULL_TREE;
8565 cval = TREE_INT_CST_LOW (c);
8566 cval &= 0xff;
8567 cval |= cval << 8;
8568 cval |= cval << 16;
8569 cval |= (cval << 31) << 1;
8572 ret = build_int_cst_type (etype, cval);
8573 var = build_fold_indirect_ref_loc (loc,
8574 fold_convert_loc (loc,
8575 build_pointer_type (etype),
8576 dest));
8577 ret = build2 (MODIFY_EXPR, etype, var, ret);
8578 if (ignore)
8579 return ret;
8581 return omit_one_operand_loc (loc, type, dest, ret);
8584 /* Fold function call to builtin memset. Return
8585 NULL_TREE if no simplification can be made. */
8587 static tree
8588 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8590 if (! validate_arg (dest, POINTER_TYPE)
8591 || ! validate_arg (size, INTEGER_TYPE))
8592 return NULL_TREE;
8594 if (!ignore)
8595 return NULL_TREE;
8597 /* New argument list transforming bzero(ptr x, int y) to
8598 memset(ptr x, int 0, size_t y). This is done this way
8599 so that if it isn't expanded inline, we fallback to
8600 calling bzero instead of memset. */
8602 return fold_builtin_memset (loc, dest, integer_zero_node,
8603 fold_convert_loc (loc, size_type_node, size),
8604 void_type_node, ignore);
8607 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8608 NULL_TREE if no simplification can be made.
8609 If ENDP is 0, return DEST (like memcpy).
8610 If ENDP is 1, return DEST+LEN (like mempcpy).
8611 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8612 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8613 (memmove). */
8615 static tree
8616 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8617 tree len, tree type, bool ignore, int endp)
8619 tree destvar, srcvar, expr;
8621 if (! validate_arg (dest, POINTER_TYPE)
8622 || ! validate_arg (src, POINTER_TYPE)
8623 || ! validate_arg (len, INTEGER_TYPE))
8624 return NULL_TREE;
8626 /* If the LEN parameter is zero, return DEST. */
8627 if (integer_zerop (len))
8628 return omit_one_operand_loc (loc, type, dest, src);
8630 /* If SRC and DEST are the same (and not volatile), return
8631 DEST{,+LEN,+LEN-1}. */
8632 if (operand_equal_p (src, dest, 0))
8633 expr = len;
8634 else
8636 tree srctype, desttype;
8637 unsigned int src_align, dest_align;
8638 tree off0;
8640 if (endp == 3)
8642 src_align = get_pointer_alignment (src);
8643 dest_align = get_pointer_alignment (dest);
8645 /* Both DEST and SRC must be pointer types.
8646 ??? This is what old code did. Is the testing for pointer types
8647 really mandatory?
8649 If either SRC is readonly or length is 1, we can use memcpy. */
8650 if (!dest_align || !src_align)
8651 return NULL_TREE;
8652 if (readonly_data_expr (src)
8653 || (tree_fits_uhwi_p (len)
8654 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8655 >= tree_to_uhwi (len))))
8657 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8658 if (!fn)
8659 return NULL_TREE;
8660 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8663 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8664 if (TREE_CODE (src) == ADDR_EXPR
8665 && TREE_CODE (dest) == ADDR_EXPR)
8667 tree src_base, dest_base, fn;
8668 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8669 HOST_WIDE_INT size = -1;
8670 HOST_WIDE_INT maxsize = -1;
8672 srcvar = TREE_OPERAND (src, 0);
8673 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8674 &size, &maxsize);
8675 destvar = TREE_OPERAND (dest, 0);
8676 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8677 &size, &maxsize);
8678 if (tree_fits_uhwi_p (len))
8679 maxsize = tree_to_uhwi (len);
8680 else
8681 maxsize = -1;
8682 src_offset /= BITS_PER_UNIT;
8683 dest_offset /= BITS_PER_UNIT;
8684 if (SSA_VAR_P (src_base)
8685 && SSA_VAR_P (dest_base))
8687 if (operand_equal_p (src_base, dest_base, 0)
8688 && ranges_overlap_p (src_offset, maxsize,
8689 dest_offset, maxsize))
8690 return NULL_TREE;
8692 else if (TREE_CODE (src_base) == MEM_REF
8693 && TREE_CODE (dest_base) == MEM_REF)
8695 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8696 TREE_OPERAND (dest_base, 0), 0))
8697 return NULL_TREE;
8698 offset_int off = mem_ref_offset (src_base) + src_offset;
8699 if (!wi::fits_shwi_p (off))
8700 return NULL_TREE;
8701 src_offset = off.to_shwi ();
8703 off = mem_ref_offset (dest_base) + dest_offset;
8704 if (!wi::fits_shwi_p (off))
8705 return NULL_TREE;
8706 dest_offset = off.to_shwi ();
8707 if (ranges_overlap_p (src_offset, maxsize,
8708 dest_offset, maxsize))
8709 return NULL_TREE;
8711 else
8712 return NULL_TREE;
8714 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8715 if (!fn)
8716 return NULL_TREE;
8717 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8720 /* If the destination and source do not alias optimize into
8721 memcpy as well. */
8722 if ((is_gimple_min_invariant (dest)
8723 || TREE_CODE (dest) == SSA_NAME)
8724 && (is_gimple_min_invariant (src)
8725 || TREE_CODE (src) == SSA_NAME))
8727 ao_ref destr, srcr;
8728 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8729 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8730 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8732 tree fn;
8733 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8734 if (!fn)
8735 return NULL_TREE;
8736 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8740 return NULL_TREE;
8743 if (!tree_fits_shwi_p (len))
8744 return NULL_TREE;
8745 /* FIXME:
8746 This logic lose for arguments like (type *)malloc (sizeof (type)),
8747 since we strip the casts of up to VOID return value from malloc.
8748 Perhaps we ought to inherit type from non-VOID argument here? */
8749 STRIP_NOPS (src);
8750 STRIP_NOPS (dest);
8751 if (!POINTER_TYPE_P (TREE_TYPE (src))
8752 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8753 return NULL_TREE;
8754 /* In the following try to find a type that is most natural to be
8755 used for the memcpy source and destination and that allows
8756 the most optimization when memcpy is turned into a plain assignment
8757 using that type. In theory we could always use a char[len] type
8758 but that only gains us that the destination and source possibly
8759 no longer will have their address taken. */
8760 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8761 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8763 tree tem = TREE_OPERAND (src, 0);
8764 STRIP_NOPS (tem);
8765 if (tem != TREE_OPERAND (src, 0))
8766 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8768 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8770 tree tem = TREE_OPERAND (dest, 0);
8771 STRIP_NOPS (tem);
8772 if (tem != TREE_OPERAND (dest, 0))
8773 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8775 srctype = TREE_TYPE (TREE_TYPE (src));
8776 if (TREE_CODE (srctype) == ARRAY_TYPE
8777 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8779 srctype = TREE_TYPE (srctype);
8780 STRIP_NOPS (src);
8781 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8783 desttype = TREE_TYPE (TREE_TYPE (dest));
8784 if (TREE_CODE (desttype) == ARRAY_TYPE
8785 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8787 desttype = TREE_TYPE (desttype);
8788 STRIP_NOPS (dest);
8789 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8791 if (TREE_ADDRESSABLE (srctype)
8792 || TREE_ADDRESSABLE (desttype))
8793 return NULL_TREE;
8795 /* Make sure we are not copying using a floating-point mode or
8796 a type whose size possibly does not match its precision. */
8797 if (FLOAT_MODE_P (TYPE_MODE (desttype))
8798 || TREE_CODE (desttype) == BOOLEAN_TYPE
8799 || TREE_CODE (desttype) == ENUMERAL_TYPE)
8800 desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
8801 if (FLOAT_MODE_P (TYPE_MODE (srctype))
8802 || TREE_CODE (srctype) == BOOLEAN_TYPE
8803 || TREE_CODE (srctype) == ENUMERAL_TYPE)
8804 srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
8805 if (!srctype)
8806 srctype = desttype;
8807 if (!desttype)
8808 desttype = srctype;
8809 if (!srctype)
8810 return NULL_TREE;
8812 src_align = get_pointer_alignment (src);
8813 dest_align = get_pointer_alignment (dest);
8814 if (dest_align < TYPE_ALIGN (desttype)
8815 || src_align < TYPE_ALIGN (srctype))
8816 return NULL_TREE;
8818 if (!ignore)
8819 dest = builtin_save_expr (dest);
8821 /* Build accesses at offset zero with a ref-all character type. */
8822 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8823 ptr_mode, true), 0);
8825 destvar = dest;
8826 STRIP_NOPS (destvar);
8827 if (TREE_CODE (destvar) == ADDR_EXPR
8828 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8829 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8830 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8831 else
8832 destvar = NULL_TREE;
8834 srcvar = src;
8835 STRIP_NOPS (srcvar);
8836 if (TREE_CODE (srcvar) == ADDR_EXPR
8837 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8838 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8840 if (!destvar
8841 || src_align >= TYPE_ALIGN (desttype))
8842 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8843 srcvar, off0);
8844 else if (!STRICT_ALIGNMENT)
8846 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8847 src_align);
8848 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8850 else
8851 srcvar = NULL_TREE;
8853 else
8854 srcvar = NULL_TREE;
8856 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8857 return NULL_TREE;
8859 if (srcvar == NULL_TREE)
8861 STRIP_NOPS (src);
8862 if (src_align >= TYPE_ALIGN (desttype))
8863 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8864 else
8866 if (STRICT_ALIGNMENT)
8867 return NULL_TREE;
8868 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8869 src_align);
8870 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8873 else if (destvar == NULL_TREE)
8875 STRIP_NOPS (dest);
8876 if (dest_align >= TYPE_ALIGN (srctype))
8877 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8878 else
8880 if (STRICT_ALIGNMENT)
8881 return NULL_TREE;
8882 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8883 dest_align);
8884 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8888 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8891 if (ignore)
8892 return expr;
8894 if (endp == 0 || endp == 3)
8895 return omit_one_operand_loc (loc, type, dest, expr);
8897 if (expr == len)
8898 expr = NULL_TREE;
8900 if (endp == 2)
8901 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8902 ssize_int (1));
8904 dest = fold_build_pointer_plus_loc (loc, dest, len);
8905 dest = fold_convert_loc (loc, type, dest);
8906 if (expr)
8907 dest = omit_one_operand_loc (loc, type, dest, expr);
8908 return dest;
8911 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8912 If LEN is not NULL, it represents the length of the string to be
8913 copied. Return NULL_TREE if no simplification can be made. */
8915 tree
8916 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8918 tree fn;
8920 if (!validate_arg (dest, POINTER_TYPE)
8921 || !validate_arg (src, POINTER_TYPE))
8922 return NULL_TREE;
8924 /* If SRC and DEST are the same (and not volatile), return DEST. */
8925 if (operand_equal_p (src, dest, 0))
8926 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8928 if (optimize_function_for_size_p (cfun))
8929 return NULL_TREE;
8931 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8932 if (!fn)
8933 return NULL_TREE;
8935 if (!len)
8937 len = c_strlen (src, 1);
8938 if (! len || TREE_SIDE_EFFECTS (len))
8939 return NULL_TREE;
8942 len = fold_convert_loc (loc, size_type_node, len);
8943 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
8944 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8945 build_call_expr_loc (loc, fn, 3, dest, src, len));
8948 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8949 Return NULL_TREE if no simplification can be made. */
8951 static tree
8952 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8954 tree fn, len, lenp1, call, type;
8956 if (!validate_arg (dest, POINTER_TYPE)
8957 || !validate_arg (src, POINTER_TYPE))
8958 return NULL_TREE;
8960 len = c_strlen (src, 1);
8961 if (!len
8962 || TREE_CODE (len) != INTEGER_CST)
8963 return NULL_TREE;
8965 if (optimize_function_for_size_p (cfun)
8966 /* If length is zero it's small enough. */
8967 && !integer_zerop (len))
8968 return NULL_TREE;
8970 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8971 if (!fn)
8972 return NULL_TREE;
8974 lenp1 = size_binop_loc (loc, PLUS_EXPR,
8975 fold_convert_loc (loc, size_type_node, len),
8976 build_int_cst (size_type_node, 1));
8977 /* We use dest twice in building our expression. Save it from
8978 multiple expansions. */
8979 dest = builtin_save_expr (dest);
8980 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8982 type = TREE_TYPE (TREE_TYPE (fndecl));
8983 dest = fold_build_pointer_plus_loc (loc, dest, len);
8984 dest = fold_convert_loc (loc, type, dest);
8985 dest = omit_one_operand_loc (loc, type, dest, call);
8986 return dest;
8989 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8990 If SLEN is not NULL, it represents the length of the source string.
8991 Return NULL_TREE if no simplification can be made. */
8993 tree
8994 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
8995 tree src, tree len, tree slen)
8997 tree fn;
8999 if (!validate_arg (dest, POINTER_TYPE)
9000 || !validate_arg (src, POINTER_TYPE)
9001 || !validate_arg (len, INTEGER_TYPE))
9002 return NULL_TREE;
9004 /* If the LEN parameter is zero, return DEST. */
9005 if (integer_zerop (len))
9006 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9008 /* We can't compare slen with len as constants below if len is not a
9009 constant. */
9010 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9011 return NULL_TREE;
9013 if (!slen)
9014 slen = c_strlen (src, 1);
9016 /* Now, we must be passed a constant src ptr parameter. */
9017 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9018 return NULL_TREE;
9020 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9022 /* We do not support simplification of this case, though we do
9023 support it when expanding trees into RTL. */
9024 /* FIXME: generate a call to __builtin_memset. */
9025 if (tree_int_cst_lt (slen, len))
9026 return NULL_TREE;
9028 /* OK transform into builtin memcpy. */
9029 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9030 if (!fn)
9031 return NULL_TREE;
9033 len = fold_convert_loc (loc, size_type_node, len);
9034 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9035 build_call_expr_loc (loc, fn, 3, dest, src, len));
9038 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9039 arguments to the call, and TYPE is its return type.
9040 Return NULL_TREE if no simplification can be made. */
9042 static tree
9043 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9045 if (!validate_arg (arg1, POINTER_TYPE)
9046 || !validate_arg (arg2, INTEGER_TYPE)
9047 || !validate_arg (len, INTEGER_TYPE))
9048 return NULL_TREE;
9049 else
9051 const char *p1;
9053 if (TREE_CODE (arg2) != INTEGER_CST
9054 || !tree_fits_uhwi_p (len))
9055 return NULL_TREE;
9057 p1 = c_getstr (arg1);
9058 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9060 char c;
9061 const char *r;
9062 tree tem;
9064 if (target_char_cast (arg2, &c))
9065 return NULL_TREE;
9067 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
9069 if (r == NULL)
9070 return build_int_cst (TREE_TYPE (arg1), 0);
9072 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9073 return fold_convert_loc (loc, type, tem);
9075 return NULL_TREE;
9079 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9080 Return NULL_TREE if no simplification can be made. */
9082 static tree
9083 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9085 const char *p1, *p2;
9087 if (!validate_arg (arg1, POINTER_TYPE)
9088 || !validate_arg (arg2, POINTER_TYPE)
9089 || !validate_arg (len, INTEGER_TYPE))
9090 return NULL_TREE;
9092 /* If the LEN parameter is zero, return zero. */
9093 if (integer_zerop (len))
9094 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9095 arg1, arg2);
9097 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9098 if (operand_equal_p (arg1, arg2, 0))
9099 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9101 p1 = c_getstr (arg1);
9102 p2 = c_getstr (arg2);
9104 /* If all arguments are constant, and the value of len is not greater
9105 than the lengths of arg1 and arg2, evaluate at compile-time. */
9106 if (tree_fits_uhwi_p (len) && p1 && p2
9107 && compare_tree_int (len, strlen (p1) + 1) <= 0
9108 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9110 const int r = memcmp (p1, p2, tree_to_uhwi (len));
9112 if (r > 0)
9113 return integer_one_node;
9114 else if (r < 0)
9115 return integer_minus_one_node;
9116 else
9117 return integer_zero_node;
9120 /* If len parameter is one, return an expression corresponding to
9121 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9122 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9124 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9125 tree cst_uchar_ptr_node
9126 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9128 tree ind1
9129 = fold_convert_loc (loc, integer_type_node,
9130 build1 (INDIRECT_REF, cst_uchar_node,
9131 fold_convert_loc (loc,
9132 cst_uchar_ptr_node,
9133 arg1)));
9134 tree ind2
9135 = fold_convert_loc (loc, integer_type_node,
9136 build1 (INDIRECT_REF, cst_uchar_node,
9137 fold_convert_loc (loc,
9138 cst_uchar_ptr_node,
9139 arg2)));
9140 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9143 return NULL_TREE;
9146 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9147 Return NULL_TREE if no simplification can be made. */
9149 static tree
9150 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9152 const char *p1, *p2;
9154 if (!validate_arg (arg1, POINTER_TYPE)
9155 || !validate_arg (arg2, POINTER_TYPE))
9156 return NULL_TREE;
9158 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9159 if (operand_equal_p (arg1, arg2, 0))
9160 return integer_zero_node;
9162 p1 = c_getstr (arg1);
9163 p2 = c_getstr (arg2);
9165 if (p1 && p2)
9167 const int i = strcmp (p1, p2);
9168 if (i < 0)
9169 return integer_minus_one_node;
9170 else if (i > 0)
9171 return integer_one_node;
9172 else
9173 return integer_zero_node;
9176 /* If the second arg is "", return *(const unsigned char*)arg1. */
9177 if (p2 && *p2 == '\0')
9179 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9180 tree cst_uchar_ptr_node
9181 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9183 return fold_convert_loc (loc, integer_type_node,
9184 build1 (INDIRECT_REF, cst_uchar_node,
9185 fold_convert_loc (loc,
9186 cst_uchar_ptr_node,
9187 arg1)));
9190 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9191 if (p1 && *p1 == '\0')
9193 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9194 tree cst_uchar_ptr_node
9195 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9197 tree temp
9198 = fold_convert_loc (loc, integer_type_node,
9199 build1 (INDIRECT_REF, cst_uchar_node,
9200 fold_convert_loc (loc,
9201 cst_uchar_ptr_node,
9202 arg2)));
9203 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9206 return NULL_TREE;
9209 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9210 Return NULL_TREE if no simplification can be made. */
9212 static tree
9213 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9215 const char *p1, *p2;
9217 if (!validate_arg (arg1, POINTER_TYPE)
9218 || !validate_arg (arg2, POINTER_TYPE)
9219 || !validate_arg (len, INTEGER_TYPE))
9220 return NULL_TREE;
9222 /* If the LEN parameter is zero, return zero. */
9223 if (integer_zerop (len))
9224 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9225 arg1, arg2);
9227 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9228 if (operand_equal_p (arg1, arg2, 0))
9229 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9231 p1 = c_getstr (arg1);
9232 p2 = c_getstr (arg2);
9234 if (tree_fits_uhwi_p (len) && p1 && p2)
9236 const int i = strncmp (p1, p2, tree_to_uhwi (len));
9237 if (i > 0)
9238 return integer_one_node;
9239 else if (i < 0)
9240 return integer_minus_one_node;
9241 else
9242 return integer_zero_node;
9245 /* If the second arg is "", and the length is greater than zero,
9246 return *(const unsigned char*)arg1. */
9247 if (p2 && *p2 == '\0'
9248 && TREE_CODE (len) == INTEGER_CST
9249 && tree_int_cst_sgn (len) == 1)
9251 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9252 tree cst_uchar_ptr_node
9253 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9255 return fold_convert_loc (loc, integer_type_node,
9256 build1 (INDIRECT_REF, cst_uchar_node,
9257 fold_convert_loc (loc,
9258 cst_uchar_ptr_node,
9259 arg1)));
9262 /* If the first arg is "", and the length is greater than zero,
9263 return -*(const unsigned char*)arg2. */
9264 if (p1 && *p1 == '\0'
9265 && TREE_CODE (len) == INTEGER_CST
9266 && tree_int_cst_sgn (len) == 1)
9268 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9269 tree cst_uchar_ptr_node
9270 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9272 tree temp = fold_convert_loc (loc, integer_type_node,
9273 build1 (INDIRECT_REF, cst_uchar_node,
9274 fold_convert_loc (loc,
9275 cst_uchar_ptr_node,
9276 arg2)));
9277 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9280 /* If len parameter is one, return an expression corresponding to
9281 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9282 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9284 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9285 tree cst_uchar_ptr_node
9286 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9288 tree ind1 = fold_convert_loc (loc, integer_type_node,
9289 build1 (INDIRECT_REF, cst_uchar_node,
9290 fold_convert_loc (loc,
9291 cst_uchar_ptr_node,
9292 arg1)));
9293 tree ind2 = fold_convert_loc (loc, integer_type_node,
9294 build1 (INDIRECT_REF, cst_uchar_node,
9295 fold_convert_loc (loc,
9296 cst_uchar_ptr_node,
9297 arg2)));
9298 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9301 return NULL_TREE;
9304 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9305 ARG. Return NULL_TREE if no simplification can be made. */
9307 static tree
9308 fold_builtin_signbit (location_t loc, tree arg, tree type)
9310 if (!validate_arg (arg, REAL_TYPE))
9311 return NULL_TREE;
9313 /* If ARG is a compile-time constant, determine the result. */
9314 if (TREE_CODE (arg) == REAL_CST
9315 && !TREE_OVERFLOW (arg))
9317 REAL_VALUE_TYPE c;
9319 c = TREE_REAL_CST (arg);
9320 return (REAL_VALUE_NEGATIVE (c)
9321 ? build_one_cst (type)
9322 : build_zero_cst (type));
9325 /* If ARG is non-negative, the result is always zero. */
9326 if (tree_expr_nonnegative_p (arg))
9327 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9329 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9330 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9331 return fold_convert (type,
9332 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9333 build_real (TREE_TYPE (arg), dconst0)));
9335 return NULL_TREE;
9338 /* Fold function call to builtin copysign, copysignf or copysignl with
9339 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9340 be made. */
9342 static tree
9343 fold_builtin_copysign (location_t loc, tree fndecl,
9344 tree arg1, tree arg2, tree type)
9346 tree tem;
9348 if (!validate_arg (arg1, REAL_TYPE)
9349 || !validate_arg (arg2, REAL_TYPE))
9350 return NULL_TREE;
9352 /* copysign(X,X) is X. */
9353 if (operand_equal_p (arg1, arg2, 0))
9354 return fold_convert_loc (loc, type, arg1);
9356 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9357 if (TREE_CODE (arg1) == REAL_CST
9358 && TREE_CODE (arg2) == REAL_CST
9359 && !TREE_OVERFLOW (arg1)
9360 && !TREE_OVERFLOW (arg2))
9362 REAL_VALUE_TYPE c1, c2;
9364 c1 = TREE_REAL_CST (arg1);
9365 c2 = TREE_REAL_CST (arg2);
9366 /* c1.sign := c2.sign. */
9367 real_copysign (&c1, &c2);
9368 return build_real (type, c1);
9371 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9372 Remember to evaluate Y for side-effects. */
9373 if (tree_expr_nonnegative_p (arg2))
9374 return omit_one_operand_loc (loc, type,
9375 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9376 arg2);
9378 /* Strip sign changing operations for the first argument. */
9379 tem = fold_strip_sign_ops (arg1);
9380 if (tem)
9381 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9383 return NULL_TREE;
9386 /* Fold a call to builtin isascii with argument ARG. */
9388 static tree
9389 fold_builtin_isascii (location_t loc, tree arg)
9391 if (!validate_arg (arg, INTEGER_TYPE))
9392 return NULL_TREE;
9393 else
9395 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9396 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9397 build_int_cst (integer_type_node,
9398 ~ (unsigned HOST_WIDE_INT) 0x7f));
9399 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9400 arg, integer_zero_node);
9404 /* Fold a call to builtin toascii with argument ARG. */
9406 static tree
9407 fold_builtin_toascii (location_t loc, tree arg)
9409 if (!validate_arg (arg, INTEGER_TYPE))
9410 return NULL_TREE;
9412 /* Transform toascii(c) -> (c & 0x7f). */
9413 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9414 build_int_cst (integer_type_node, 0x7f));
9417 /* Fold a call to builtin isdigit with argument ARG. */
9419 static tree
9420 fold_builtin_isdigit (location_t loc, tree arg)
9422 if (!validate_arg (arg, INTEGER_TYPE))
9423 return NULL_TREE;
9424 else
9426 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9427 /* According to the C standard, isdigit is unaffected by locale.
9428 However, it definitely is affected by the target character set. */
9429 unsigned HOST_WIDE_INT target_digit0
9430 = lang_hooks.to_target_charset ('0');
9432 if (target_digit0 == 0)
9433 return NULL_TREE;
9435 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9436 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9437 build_int_cst (unsigned_type_node, target_digit0));
9438 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9439 build_int_cst (unsigned_type_node, 9));
9443 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9445 static tree
9446 fold_builtin_fabs (location_t loc, tree arg, tree type)
9448 if (!validate_arg (arg, REAL_TYPE))
9449 return NULL_TREE;
9451 arg = fold_convert_loc (loc, type, arg);
9452 if (TREE_CODE (arg) == REAL_CST)
9453 return fold_abs_const (arg, type);
9454 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9457 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9459 static tree
9460 fold_builtin_abs (location_t loc, tree arg, tree type)
9462 if (!validate_arg (arg, INTEGER_TYPE))
9463 return NULL_TREE;
9465 arg = fold_convert_loc (loc, type, arg);
9466 if (TREE_CODE (arg) == INTEGER_CST)
9467 return fold_abs_const (arg, type);
9468 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9471 /* Fold a fma operation with arguments ARG[012]. */
9473 tree
9474 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9475 tree type, tree arg0, tree arg1, tree arg2)
9477 if (TREE_CODE (arg0) == REAL_CST
9478 && TREE_CODE (arg1) == REAL_CST
9479 && TREE_CODE (arg2) == REAL_CST)
9480 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9482 return NULL_TREE;
9485 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9487 static tree
9488 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9490 if (validate_arg (arg0, REAL_TYPE)
9491 && validate_arg (arg1, REAL_TYPE)
9492 && validate_arg (arg2, REAL_TYPE))
9494 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9495 if (tem)
9496 return tem;
9498 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9499 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9500 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9502 return NULL_TREE;
9505 /* Fold a call to builtin fmin or fmax. */
9507 static tree
9508 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9509 tree type, bool max)
9511 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9513 /* Calculate the result when the argument is a constant. */
9514 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9516 if (res)
9517 return res;
9519 /* If either argument is NaN, return the other one. Avoid the
9520 transformation if we get (and honor) a signalling NaN. Using
9521 omit_one_operand() ensures we create a non-lvalue. */
9522 if (TREE_CODE (arg0) == REAL_CST
9523 && real_isnan (&TREE_REAL_CST (arg0))
9524 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9525 || ! TREE_REAL_CST (arg0).signalling))
9526 return omit_one_operand_loc (loc, type, arg1, arg0);
9527 if (TREE_CODE (arg1) == REAL_CST
9528 && real_isnan (&TREE_REAL_CST (arg1))
9529 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9530 || ! TREE_REAL_CST (arg1).signalling))
9531 return omit_one_operand_loc (loc, type, arg0, arg1);
9533 /* Transform fmin/fmax(x,x) -> x. */
9534 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9535 return omit_one_operand_loc (loc, type, arg0, arg1);
9537 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9538 functions to return the numeric arg if the other one is NaN.
9539 These tree codes don't honor that, so only transform if
9540 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9541 handled, so we don't have to worry about it either. */
9542 if (flag_finite_math_only)
9543 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9544 fold_convert_loc (loc, type, arg0),
9545 fold_convert_loc (loc, type, arg1));
9547 return NULL_TREE;
9550 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9552 static tree
9553 fold_builtin_carg (location_t loc, tree arg, tree type)
9555 if (validate_arg (arg, COMPLEX_TYPE)
9556 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9558 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9560 if (atan2_fn)
9562 tree new_arg = builtin_save_expr (arg);
9563 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9564 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9565 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9569 return NULL_TREE;
9572 /* Fold a call to builtin logb/ilogb. */
9574 static tree
9575 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9577 if (! validate_arg (arg, REAL_TYPE))
9578 return NULL_TREE;
9580 STRIP_NOPS (arg);
9582 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9584 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9586 switch (value->cl)
9588 case rvc_nan:
9589 case rvc_inf:
9590 /* If arg is Inf or NaN and we're logb, return it. */
9591 if (TREE_CODE (rettype) == REAL_TYPE)
9593 /* For logb(-Inf) we have to return +Inf. */
9594 if (real_isinf (value) && real_isneg (value))
9596 REAL_VALUE_TYPE tem;
9597 real_inf (&tem);
9598 return build_real (rettype, tem);
9600 return fold_convert_loc (loc, rettype, arg);
9602 /* Fall through... */
9603 case rvc_zero:
9604 /* Zero may set errno and/or raise an exception for logb, also
9605 for ilogb we don't know FP_ILOGB0. */
9606 return NULL_TREE;
9607 case rvc_normal:
9608 /* For normal numbers, proceed iff radix == 2. In GCC,
9609 normalized significands are in the range [0.5, 1.0). We
9610 want the exponent as if they were [1.0, 2.0) so get the
9611 exponent and subtract 1. */
9612 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9613 return fold_convert_loc (loc, rettype,
9614 build_int_cst (integer_type_node,
9615 REAL_EXP (value)-1));
9616 break;
9620 return NULL_TREE;
9623 /* Fold a call to builtin significand, if radix == 2. */
9625 static tree
9626 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9628 if (! validate_arg (arg, REAL_TYPE))
9629 return NULL_TREE;
9631 STRIP_NOPS (arg);
9633 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9635 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9637 switch (value->cl)
9639 case rvc_zero:
9640 case rvc_nan:
9641 case rvc_inf:
9642 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9643 return fold_convert_loc (loc, rettype, arg);
9644 case rvc_normal:
9645 /* For normal numbers, proceed iff radix == 2. */
9646 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9648 REAL_VALUE_TYPE result = *value;
9649 /* In GCC, normalized significands are in the range [0.5,
9650 1.0). We want them to be [1.0, 2.0) so set the
9651 exponent to 1. */
9652 SET_REAL_EXP (&result, 1);
9653 return build_real (rettype, result);
9655 break;
9659 return NULL_TREE;
9662 /* Fold a call to builtin frexp, we can assume the base is 2. */
9664 static tree
9665 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9667 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9668 return NULL_TREE;
9670 STRIP_NOPS (arg0);
9672 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9673 return NULL_TREE;
9675 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9677 /* Proceed if a valid pointer type was passed in. */
9678 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9680 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9681 tree frac, exp;
9683 switch (value->cl)
9685 case rvc_zero:
9686 /* For +-0, return (*exp = 0, +-0). */
9687 exp = integer_zero_node;
9688 frac = arg0;
9689 break;
9690 case rvc_nan:
9691 case rvc_inf:
9692 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9693 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9694 case rvc_normal:
9696 /* Since the frexp function always expects base 2, and in
9697 GCC normalized significands are already in the range
9698 [0.5, 1.0), we have exactly what frexp wants. */
9699 REAL_VALUE_TYPE frac_rvt = *value;
9700 SET_REAL_EXP (&frac_rvt, 0);
9701 frac = build_real (rettype, frac_rvt);
9702 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9704 break;
9705 default:
9706 gcc_unreachable ();
9709 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9710 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9711 TREE_SIDE_EFFECTS (arg1) = 1;
9712 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9715 return NULL_TREE;
9718 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9719 then we can assume the base is two. If it's false, then we have to
9720 check the mode of the TYPE parameter in certain cases. */
9722 static tree
9723 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9724 tree type, bool ldexp)
9726 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9728 STRIP_NOPS (arg0);
9729 STRIP_NOPS (arg1);
9731 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9732 if (real_zerop (arg0) || integer_zerop (arg1)
9733 || (TREE_CODE (arg0) == REAL_CST
9734 && !real_isfinite (&TREE_REAL_CST (arg0))))
9735 return omit_one_operand_loc (loc, type, arg0, arg1);
9737 /* If both arguments are constant, then try to evaluate it. */
9738 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9739 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9740 && tree_fits_shwi_p (arg1))
9742 /* Bound the maximum adjustment to twice the range of the
9743 mode's valid exponents. Use abs to ensure the range is
9744 positive as a sanity check. */
9745 const long max_exp_adj = 2 *
9746 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9747 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9749 /* Get the user-requested adjustment. */
9750 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9752 /* The requested adjustment must be inside this range. This
9753 is a preliminary cap to avoid things like overflow, we
9754 may still fail to compute the result for other reasons. */
9755 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9757 REAL_VALUE_TYPE initial_result;
9759 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9761 /* Ensure we didn't overflow. */
9762 if (! real_isinf (&initial_result))
9764 const REAL_VALUE_TYPE trunc_result
9765 = real_value_truncate (TYPE_MODE (type), initial_result);
9767 /* Only proceed if the target mode can hold the
9768 resulting value. */
9769 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9770 return build_real (type, trunc_result);
9776 return NULL_TREE;
9779 /* Fold a call to builtin modf. */
9781 static tree
9782 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9784 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9785 return NULL_TREE;
9787 STRIP_NOPS (arg0);
9789 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9790 return NULL_TREE;
9792 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9794 /* Proceed if a valid pointer type was passed in. */
9795 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9797 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9798 REAL_VALUE_TYPE trunc, frac;
9800 switch (value->cl)
9802 case rvc_nan:
9803 case rvc_zero:
9804 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9805 trunc = frac = *value;
9806 break;
9807 case rvc_inf:
9808 /* For +-Inf, return (*arg1 = arg0, +-0). */
9809 frac = dconst0;
9810 frac.sign = value->sign;
9811 trunc = *value;
9812 break;
9813 case rvc_normal:
9814 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9815 real_trunc (&trunc, VOIDmode, value);
9816 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9817 /* If the original number was negative and already
9818 integral, then the fractional part is -0.0. */
9819 if (value->sign && frac.cl == rvc_zero)
9820 frac.sign = value->sign;
9821 break;
9824 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9825 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9826 build_real (rettype, trunc));
9827 TREE_SIDE_EFFECTS (arg1) = 1;
9828 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9829 build_real (rettype, frac));
9832 return NULL_TREE;
9835 /* Given a location LOC, an interclass builtin function decl FNDECL
9836 and its single argument ARG, return an folded expression computing
9837 the same, or NULL_TREE if we either couldn't or didn't want to fold
9838 (the latter happen if there's an RTL instruction available). */
9840 static tree
9841 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9843 enum machine_mode mode;
9845 if (!validate_arg (arg, REAL_TYPE))
9846 return NULL_TREE;
9848 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9849 return NULL_TREE;
9851 mode = TYPE_MODE (TREE_TYPE (arg));
9853 /* If there is no optab, try generic code. */
9854 switch (DECL_FUNCTION_CODE (fndecl))
9856 tree result;
9858 CASE_FLT_FN (BUILT_IN_ISINF):
9860 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9861 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9862 tree const type = TREE_TYPE (arg);
9863 REAL_VALUE_TYPE r;
9864 char buf[128];
9866 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9867 real_from_string (&r, buf);
9868 result = build_call_expr (isgr_fn, 2,
9869 fold_build1_loc (loc, ABS_EXPR, type, arg),
9870 build_real (type, r));
9871 return result;
9873 CASE_FLT_FN (BUILT_IN_FINITE):
9874 case BUILT_IN_ISFINITE:
9876 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9877 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9878 tree const type = TREE_TYPE (arg);
9879 REAL_VALUE_TYPE r;
9880 char buf[128];
9882 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9883 real_from_string (&r, buf);
9884 result = build_call_expr (isle_fn, 2,
9885 fold_build1_loc (loc, ABS_EXPR, type, arg),
9886 build_real (type, r));
9887 /*result = fold_build2_loc (loc, UNGT_EXPR,
9888 TREE_TYPE (TREE_TYPE (fndecl)),
9889 fold_build1_loc (loc, ABS_EXPR, type, arg),
9890 build_real (type, r));
9891 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9892 TREE_TYPE (TREE_TYPE (fndecl)),
9893 result);*/
9894 return result;
9896 case BUILT_IN_ISNORMAL:
9898 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9899 islessequal(fabs(x),DBL_MAX). */
9900 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9901 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9902 tree const type = TREE_TYPE (arg);
9903 REAL_VALUE_TYPE rmax, rmin;
9904 char buf[128];
9906 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9907 real_from_string (&rmax, buf);
9908 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9909 real_from_string (&rmin, buf);
9910 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9911 result = build_call_expr (isle_fn, 2, arg,
9912 build_real (type, rmax));
9913 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9914 build_call_expr (isge_fn, 2, arg,
9915 build_real (type, rmin)));
9916 return result;
9918 default:
9919 break;
9922 return NULL_TREE;
9925 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9926 ARG is the argument for the call. */
9928 static tree
9929 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9931 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9932 REAL_VALUE_TYPE r;
9934 if (!validate_arg (arg, REAL_TYPE))
9935 return NULL_TREE;
9937 switch (builtin_index)
9939 case BUILT_IN_ISINF:
9940 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9941 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9943 if (TREE_CODE (arg) == REAL_CST)
9945 r = TREE_REAL_CST (arg);
9946 if (real_isinf (&r))
9947 return real_compare (GT_EXPR, &r, &dconst0)
9948 ? integer_one_node : integer_minus_one_node;
9949 else
9950 return integer_zero_node;
9953 return NULL_TREE;
9955 case BUILT_IN_ISINF_SIGN:
9957 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9958 /* In a boolean context, GCC will fold the inner COND_EXPR to
9959 1. So e.g. "if (isinf_sign(x))" would be folded to just
9960 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9961 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9962 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9963 tree tmp = NULL_TREE;
9965 arg = builtin_save_expr (arg);
9967 if (signbit_fn && isinf_fn)
9969 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9970 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9972 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9973 signbit_call, integer_zero_node);
9974 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9975 isinf_call, integer_zero_node);
9977 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9978 integer_minus_one_node, integer_one_node);
9979 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9980 isinf_call, tmp,
9981 integer_zero_node);
9984 return tmp;
9987 case BUILT_IN_ISFINITE:
9988 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9989 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9990 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9992 if (TREE_CODE (arg) == REAL_CST)
9994 r = TREE_REAL_CST (arg);
9995 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9998 return NULL_TREE;
10000 case BUILT_IN_ISNAN:
10001 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10002 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10004 if (TREE_CODE (arg) == REAL_CST)
10006 r = TREE_REAL_CST (arg);
10007 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10010 arg = builtin_save_expr (arg);
10011 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10013 default:
10014 gcc_unreachable ();
10018 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10019 This builtin will generate code to return the appropriate floating
10020 point classification depending on the value of the floating point
10021 number passed in. The possible return values must be supplied as
10022 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10023 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10024 one floating point argument which is "type generic". */
10026 static tree
10027 fold_builtin_fpclassify (location_t loc, tree exp)
10029 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10030 arg, type, res, tmp;
10031 enum machine_mode mode;
10032 REAL_VALUE_TYPE r;
10033 char buf[128];
10035 /* Verify the required arguments in the original call. */
10036 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10037 INTEGER_TYPE, INTEGER_TYPE,
10038 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10039 return NULL_TREE;
10041 fp_nan = CALL_EXPR_ARG (exp, 0);
10042 fp_infinite = CALL_EXPR_ARG (exp, 1);
10043 fp_normal = CALL_EXPR_ARG (exp, 2);
10044 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10045 fp_zero = CALL_EXPR_ARG (exp, 4);
10046 arg = CALL_EXPR_ARG (exp, 5);
10047 type = TREE_TYPE (arg);
10048 mode = TYPE_MODE (type);
10049 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10051 /* fpclassify(x) ->
10052 isnan(x) ? FP_NAN :
10053 (fabs(x) == Inf ? FP_INFINITE :
10054 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10055 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10057 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10058 build_real (type, dconst0));
10059 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10060 tmp, fp_zero, fp_subnormal);
10062 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10063 real_from_string (&r, buf);
10064 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10065 arg, build_real (type, r));
10066 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10068 if (HONOR_INFINITIES (mode))
10070 real_inf (&r);
10071 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10072 build_real (type, r));
10073 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10074 fp_infinite, res);
10077 if (HONOR_NANS (mode))
10079 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10080 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10083 return res;
10086 /* Fold a call to an unordered comparison function such as
10087 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10088 being called and ARG0 and ARG1 are the arguments for the call.
10089 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10090 the opposite of the desired result. UNORDERED_CODE is used
10091 for modes that can hold NaNs and ORDERED_CODE is used for
10092 the rest. */
10094 static tree
10095 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10096 enum tree_code unordered_code,
10097 enum tree_code ordered_code)
10099 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10100 enum tree_code code;
10101 tree type0, type1;
10102 enum tree_code code0, code1;
10103 tree cmp_type = NULL_TREE;
10105 type0 = TREE_TYPE (arg0);
10106 type1 = TREE_TYPE (arg1);
10108 code0 = TREE_CODE (type0);
10109 code1 = TREE_CODE (type1);
10111 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10112 /* Choose the wider of two real types. */
10113 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10114 ? type0 : type1;
10115 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10116 cmp_type = type0;
10117 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10118 cmp_type = type1;
10120 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10121 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10123 if (unordered_code == UNORDERED_EXPR)
10125 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10126 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10127 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10130 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10131 : ordered_code;
10132 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10133 fold_build2_loc (loc, code, type, arg0, arg1));
10136 /* Fold a call to built-in function FNDECL with 0 arguments.
10137 IGNORE is true if the result of the function call is ignored. This
10138 function returns NULL_TREE if no simplification was possible. */
10140 static tree
10141 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10143 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10144 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10145 switch (fcode)
10147 CASE_FLT_FN (BUILT_IN_INF):
10148 case BUILT_IN_INFD32:
10149 case BUILT_IN_INFD64:
10150 case BUILT_IN_INFD128:
10151 return fold_builtin_inf (loc, type, true);
10153 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10154 return fold_builtin_inf (loc, type, false);
10156 case BUILT_IN_CLASSIFY_TYPE:
10157 return fold_builtin_classify_type (NULL_TREE);
10159 case BUILT_IN_UNREACHABLE:
10160 if (flag_sanitize & SANITIZE_UNREACHABLE
10161 && (current_function_decl == NULL
10162 || !lookup_attribute ("no_sanitize_undefined",
10163 DECL_ATTRIBUTES (current_function_decl))))
10164 return ubsan_instrument_unreachable (loc);
10165 break;
10167 default:
10168 break;
10170 return NULL_TREE;
10173 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10174 IGNORE is true if the result of the function call is ignored. This
10175 function returns NULL_TREE if no simplification was possible. */
10177 static tree
10178 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10180 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10181 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10182 switch (fcode)
10184 case BUILT_IN_CONSTANT_P:
10186 tree val = fold_builtin_constant_p (arg0);
10188 /* Gimplification will pull the CALL_EXPR for the builtin out of
10189 an if condition. When not optimizing, we'll not CSE it back.
10190 To avoid link error types of regressions, return false now. */
10191 if (!val && !optimize)
10192 val = integer_zero_node;
10194 return val;
10197 case BUILT_IN_CLASSIFY_TYPE:
10198 return fold_builtin_classify_type (arg0);
10200 case BUILT_IN_STRLEN:
10201 return fold_builtin_strlen (loc, type, arg0);
10203 CASE_FLT_FN (BUILT_IN_FABS):
10204 case BUILT_IN_FABSD32:
10205 case BUILT_IN_FABSD64:
10206 case BUILT_IN_FABSD128:
10207 return fold_builtin_fabs (loc, arg0, type);
10209 case BUILT_IN_ABS:
10210 case BUILT_IN_LABS:
10211 case BUILT_IN_LLABS:
10212 case BUILT_IN_IMAXABS:
10213 return fold_builtin_abs (loc, arg0, type);
10215 CASE_FLT_FN (BUILT_IN_CONJ):
10216 if (validate_arg (arg0, COMPLEX_TYPE)
10217 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10218 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10219 break;
10221 CASE_FLT_FN (BUILT_IN_CREAL):
10222 if (validate_arg (arg0, COMPLEX_TYPE)
10223 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10224 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10225 break;
10227 CASE_FLT_FN (BUILT_IN_CIMAG):
10228 if (validate_arg (arg0, COMPLEX_TYPE)
10229 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10230 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10231 break;
10233 CASE_FLT_FN (BUILT_IN_CCOS):
10234 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
10236 CASE_FLT_FN (BUILT_IN_CCOSH):
10237 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
10239 CASE_FLT_FN (BUILT_IN_CPROJ):
10240 return fold_builtin_cproj (loc, arg0, type);
10242 CASE_FLT_FN (BUILT_IN_CSIN):
10243 if (validate_arg (arg0, COMPLEX_TYPE)
10244 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10245 return do_mpc_arg1 (arg0, type, mpc_sin);
10246 break;
10248 CASE_FLT_FN (BUILT_IN_CSINH):
10249 if (validate_arg (arg0, COMPLEX_TYPE)
10250 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10251 return do_mpc_arg1 (arg0, type, mpc_sinh);
10252 break;
10254 CASE_FLT_FN (BUILT_IN_CTAN):
10255 if (validate_arg (arg0, COMPLEX_TYPE)
10256 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10257 return do_mpc_arg1 (arg0, type, mpc_tan);
10258 break;
10260 CASE_FLT_FN (BUILT_IN_CTANH):
10261 if (validate_arg (arg0, COMPLEX_TYPE)
10262 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10263 return do_mpc_arg1 (arg0, type, mpc_tanh);
10264 break;
10266 CASE_FLT_FN (BUILT_IN_CLOG):
10267 if (validate_arg (arg0, COMPLEX_TYPE)
10268 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10269 return do_mpc_arg1 (arg0, type, mpc_log);
10270 break;
10272 CASE_FLT_FN (BUILT_IN_CSQRT):
10273 if (validate_arg (arg0, COMPLEX_TYPE)
10274 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10275 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10276 break;
10278 CASE_FLT_FN (BUILT_IN_CASIN):
10279 if (validate_arg (arg0, COMPLEX_TYPE)
10280 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10281 return do_mpc_arg1 (arg0, type, mpc_asin);
10282 break;
10284 CASE_FLT_FN (BUILT_IN_CACOS):
10285 if (validate_arg (arg0, COMPLEX_TYPE)
10286 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10287 return do_mpc_arg1 (arg0, type, mpc_acos);
10288 break;
10290 CASE_FLT_FN (BUILT_IN_CATAN):
10291 if (validate_arg (arg0, COMPLEX_TYPE)
10292 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10293 return do_mpc_arg1 (arg0, type, mpc_atan);
10294 break;
10296 CASE_FLT_FN (BUILT_IN_CASINH):
10297 if (validate_arg (arg0, COMPLEX_TYPE)
10298 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10299 return do_mpc_arg1 (arg0, type, mpc_asinh);
10300 break;
10302 CASE_FLT_FN (BUILT_IN_CACOSH):
10303 if (validate_arg (arg0, COMPLEX_TYPE)
10304 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10305 return do_mpc_arg1 (arg0, type, mpc_acosh);
10306 break;
10308 CASE_FLT_FN (BUILT_IN_CATANH):
10309 if (validate_arg (arg0, COMPLEX_TYPE)
10310 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10311 return do_mpc_arg1 (arg0, type, mpc_atanh);
10312 break;
10314 CASE_FLT_FN (BUILT_IN_CABS):
10315 return fold_builtin_cabs (loc, arg0, type, fndecl);
10317 CASE_FLT_FN (BUILT_IN_CARG):
10318 return fold_builtin_carg (loc, arg0, type);
10320 CASE_FLT_FN (BUILT_IN_SQRT):
10321 return fold_builtin_sqrt (loc, arg0, type);
10323 CASE_FLT_FN (BUILT_IN_CBRT):
10324 return fold_builtin_cbrt (loc, arg0, type);
10326 CASE_FLT_FN (BUILT_IN_ASIN):
10327 if (validate_arg (arg0, REAL_TYPE))
10328 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10329 &dconstm1, &dconst1, true);
10330 break;
10332 CASE_FLT_FN (BUILT_IN_ACOS):
10333 if (validate_arg (arg0, REAL_TYPE))
10334 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10335 &dconstm1, &dconst1, true);
10336 break;
10338 CASE_FLT_FN (BUILT_IN_ATAN):
10339 if (validate_arg (arg0, REAL_TYPE))
10340 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10341 break;
10343 CASE_FLT_FN (BUILT_IN_ASINH):
10344 if (validate_arg (arg0, REAL_TYPE))
10345 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10346 break;
10348 CASE_FLT_FN (BUILT_IN_ACOSH):
10349 if (validate_arg (arg0, REAL_TYPE))
10350 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10351 &dconst1, NULL, true);
10352 break;
10354 CASE_FLT_FN (BUILT_IN_ATANH):
10355 if (validate_arg (arg0, REAL_TYPE))
10356 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10357 &dconstm1, &dconst1, false);
10358 break;
10360 CASE_FLT_FN (BUILT_IN_SIN):
10361 if (validate_arg (arg0, REAL_TYPE))
10362 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10363 break;
10365 CASE_FLT_FN (BUILT_IN_COS):
10366 return fold_builtin_cos (loc, arg0, type, fndecl);
10368 CASE_FLT_FN (BUILT_IN_TAN):
10369 return fold_builtin_tan (arg0, type);
10371 CASE_FLT_FN (BUILT_IN_CEXP):
10372 return fold_builtin_cexp (loc, arg0, type);
10374 CASE_FLT_FN (BUILT_IN_CEXPI):
10375 if (validate_arg (arg0, REAL_TYPE))
10376 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10377 break;
10379 CASE_FLT_FN (BUILT_IN_SINH):
10380 if (validate_arg (arg0, REAL_TYPE))
10381 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10382 break;
10384 CASE_FLT_FN (BUILT_IN_COSH):
10385 return fold_builtin_cosh (loc, arg0, type, fndecl);
10387 CASE_FLT_FN (BUILT_IN_TANH):
10388 if (validate_arg (arg0, REAL_TYPE))
10389 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10390 break;
10392 CASE_FLT_FN (BUILT_IN_ERF):
10393 if (validate_arg (arg0, REAL_TYPE))
10394 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10395 break;
10397 CASE_FLT_FN (BUILT_IN_ERFC):
10398 if (validate_arg (arg0, REAL_TYPE))
10399 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10400 break;
10402 CASE_FLT_FN (BUILT_IN_TGAMMA):
10403 if (validate_arg (arg0, REAL_TYPE))
10404 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10405 break;
10407 CASE_FLT_FN (BUILT_IN_EXP):
10408 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10410 CASE_FLT_FN (BUILT_IN_EXP2):
10411 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10413 CASE_FLT_FN (BUILT_IN_EXP10):
10414 CASE_FLT_FN (BUILT_IN_POW10):
10415 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10417 CASE_FLT_FN (BUILT_IN_EXPM1):
10418 if (validate_arg (arg0, REAL_TYPE))
10419 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10420 break;
10422 CASE_FLT_FN (BUILT_IN_LOG):
10423 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10425 CASE_FLT_FN (BUILT_IN_LOG2):
10426 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10428 CASE_FLT_FN (BUILT_IN_LOG10):
10429 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10431 CASE_FLT_FN (BUILT_IN_LOG1P):
10432 if (validate_arg (arg0, REAL_TYPE))
10433 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10434 &dconstm1, NULL, false);
10435 break;
10437 CASE_FLT_FN (BUILT_IN_J0):
10438 if (validate_arg (arg0, REAL_TYPE))
10439 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10440 NULL, NULL, 0);
10441 break;
10443 CASE_FLT_FN (BUILT_IN_J1):
10444 if (validate_arg (arg0, REAL_TYPE))
10445 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10446 NULL, NULL, 0);
10447 break;
10449 CASE_FLT_FN (BUILT_IN_Y0):
10450 if (validate_arg (arg0, REAL_TYPE))
10451 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10452 &dconst0, NULL, false);
10453 break;
10455 CASE_FLT_FN (BUILT_IN_Y1):
10456 if (validate_arg (arg0, REAL_TYPE))
10457 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10458 &dconst0, NULL, false);
10459 break;
10461 CASE_FLT_FN (BUILT_IN_NAN):
10462 case BUILT_IN_NAND32:
10463 case BUILT_IN_NAND64:
10464 case BUILT_IN_NAND128:
10465 return fold_builtin_nan (arg0, type, true);
10467 CASE_FLT_FN (BUILT_IN_NANS):
10468 return fold_builtin_nan (arg0, type, false);
10470 CASE_FLT_FN (BUILT_IN_FLOOR):
10471 return fold_builtin_floor (loc, fndecl, arg0);
10473 CASE_FLT_FN (BUILT_IN_CEIL):
10474 return fold_builtin_ceil (loc, fndecl, arg0);
10476 CASE_FLT_FN (BUILT_IN_TRUNC):
10477 return fold_builtin_trunc (loc, fndecl, arg0);
10479 CASE_FLT_FN (BUILT_IN_ROUND):
10480 return fold_builtin_round (loc, fndecl, arg0);
10482 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10483 CASE_FLT_FN (BUILT_IN_RINT):
10484 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10486 CASE_FLT_FN (BUILT_IN_ICEIL):
10487 CASE_FLT_FN (BUILT_IN_LCEIL):
10488 CASE_FLT_FN (BUILT_IN_LLCEIL):
10489 CASE_FLT_FN (BUILT_IN_LFLOOR):
10490 CASE_FLT_FN (BUILT_IN_IFLOOR):
10491 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10492 CASE_FLT_FN (BUILT_IN_IROUND):
10493 CASE_FLT_FN (BUILT_IN_LROUND):
10494 CASE_FLT_FN (BUILT_IN_LLROUND):
10495 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10497 CASE_FLT_FN (BUILT_IN_IRINT):
10498 CASE_FLT_FN (BUILT_IN_LRINT):
10499 CASE_FLT_FN (BUILT_IN_LLRINT):
10500 return fold_fixed_mathfn (loc, fndecl, arg0);
10502 case BUILT_IN_BSWAP16:
10503 case BUILT_IN_BSWAP32:
10504 case BUILT_IN_BSWAP64:
10505 return fold_builtin_bswap (fndecl, arg0);
10507 CASE_INT_FN (BUILT_IN_FFS):
10508 CASE_INT_FN (BUILT_IN_CLZ):
10509 CASE_INT_FN (BUILT_IN_CTZ):
10510 CASE_INT_FN (BUILT_IN_CLRSB):
10511 CASE_INT_FN (BUILT_IN_POPCOUNT):
10512 CASE_INT_FN (BUILT_IN_PARITY):
10513 return fold_builtin_bitop (fndecl, arg0);
10515 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10516 return fold_builtin_signbit (loc, arg0, type);
10518 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10519 return fold_builtin_significand (loc, arg0, type);
10521 CASE_FLT_FN (BUILT_IN_ILOGB):
10522 CASE_FLT_FN (BUILT_IN_LOGB):
10523 return fold_builtin_logb (loc, arg0, type);
10525 case BUILT_IN_ISASCII:
10526 return fold_builtin_isascii (loc, arg0);
10528 case BUILT_IN_TOASCII:
10529 return fold_builtin_toascii (loc, arg0);
10531 case BUILT_IN_ISDIGIT:
10532 return fold_builtin_isdigit (loc, arg0);
10534 CASE_FLT_FN (BUILT_IN_FINITE):
10535 case BUILT_IN_FINITED32:
10536 case BUILT_IN_FINITED64:
10537 case BUILT_IN_FINITED128:
10538 case BUILT_IN_ISFINITE:
10540 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10541 if (ret)
10542 return ret;
10543 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10546 CASE_FLT_FN (BUILT_IN_ISINF):
10547 case BUILT_IN_ISINFD32:
10548 case BUILT_IN_ISINFD64:
10549 case BUILT_IN_ISINFD128:
10551 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10552 if (ret)
10553 return ret;
10554 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10557 case BUILT_IN_ISNORMAL:
10558 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10560 case BUILT_IN_ISINF_SIGN:
10561 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10563 CASE_FLT_FN (BUILT_IN_ISNAN):
10564 case BUILT_IN_ISNAND32:
10565 case BUILT_IN_ISNAND64:
10566 case BUILT_IN_ISNAND128:
10567 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10569 case BUILT_IN_PRINTF:
10570 case BUILT_IN_PRINTF_UNLOCKED:
10571 case BUILT_IN_VPRINTF:
10572 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10574 case BUILT_IN_FREE:
10575 if (integer_zerop (arg0))
10576 return build_empty_stmt (loc);
10577 break;
10579 default:
10580 break;
10583 return NULL_TREE;
10587 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10588 IGNORE is true if the result of the function call is ignored. This
10589 function returns NULL_TREE if no simplification was possible. */
10591 static tree
10592 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10594 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10595 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10597 switch (fcode)
10599 CASE_FLT_FN (BUILT_IN_JN):
10600 if (validate_arg (arg0, INTEGER_TYPE)
10601 && validate_arg (arg1, REAL_TYPE))
10602 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10603 break;
10605 CASE_FLT_FN (BUILT_IN_YN):
10606 if (validate_arg (arg0, INTEGER_TYPE)
10607 && validate_arg (arg1, REAL_TYPE))
10608 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10609 &dconst0, false);
10610 break;
10612 CASE_FLT_FN (BUILT_IN_DREM):
10613 CASE_FLT_FN (BUILT_IN_REMAINDER):
10614 if (validate_arg (arg0, REAL_TYPE)
10615 && validate_arg (arg1, REAL_TYPE))
10616 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10617 break;
10619 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10620 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10621 if (validate_arg (arg0, REAL_TYPE)
10622 && validate_arg (arg1, POINTER_TYPE))
10623 return do_mpfr_lgamma_r (arg0, arg1, type);
10624 break;
10626 CASE_FLT_FN (BUILT_IN_ATAN2):
10627 if (validate_arg (arg0, REAL_TYPE)
10628 && validate_arg (arg1, REAL_TYPE))
10629 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10630 break;
10632 CASE_FLT_FN (BUILT_IN_FDIM):
10633 if (validate_arg (arg0, REAL_TYPE)
10634 && validate_arg (arg1, REAL_TYPE))
10635 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10636 break;
10638 CASE_FLT_FN (BUILT_IN_HYPOT):
10639 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10641 CASE_FLT_FN (BUILT_IN_CPOW):
10642 if (validate_arg (arg0, COMPLEX_TYPE)
10643 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10644 && validate_arg (arg1, COMPLEX_TYPE)
10645 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10646 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10647 break;
10649 CASE_FLT_FN (BUILT_IN_LDEXP):
10650 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10651 CASE_FLT_FN (BUILT_IN_SCALBN):
10652 CASE_FLT_FN (BUILT_IN_SCALBLN):
10653 return fold_builtin_load_exponent (loc, arg0, arg1,
10654 type, /*ldexp=*/false);
10656 CASE_FLT_FN (BUILT_IN_FREXP):
10657 return fold_builtin_frexp (loc, arg0, arg1, type);
10659 CASE_FLT_FN (BUILT_IN_MODF):
10660 return fold_builtin_modf (loc, arg0, arg1, type);
10662 case BUILT_IN_BZERO:
10663 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10665 case BUILT_IN_FPUTS:
10666 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10668 case BUILT_IN_FPUTS_UNLOCKED:
10669 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10671 case BUILT_IN_STRSTR:
10672 return fold_builtin_strstr (loc, arg0, arg1, type);
10674 case BUILT_IN_STRCAT:
10675 return fold_builtin_strcat (loc, arg0, arg1, NULL_TREE);
10677 case BUILT_IN_STRSPN:
10678 return fold_builtin_strspn (loc, arg0, arg1);
10680 case BUILT_IN_STRCSPN:
10681 return fold_builtin_strcspn (loc, arg0, arg1);
10683 case BUILT_IN_STRCHR:
10684 case BUILT_IN_INDEX:
10685 return fold_builtin_strchr (loc, arg0, arg1, type);
10687 case BUILT_IN_STRRCHR:
10688 case BUILT_IN_RINDEX:
10689 return fold_builtin_strrchr (loc, arg0, arg1, type);
10691 case BUILT_IN_STRCPY:
10692 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10694 case BUILT_IN_STPCPY:
10695 if (ignore)
10697 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10698 if (!fn)
10699 break;
10701 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10703 else
10704 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10705 break;
10707 case BUILT_IN_STRCMP:
10708 return fold_builtin_strcmp (loc, arg0, arg1);
10710 case BUILT_IN_STRPBRK:
10711 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10713 case BUILT_IN_EXPECT:
10714 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
10716 CASE_FLT_FN (BUILT_IN_POW):
10717 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10719 CASE_FLT_FN (BUILT_IN_POWI):
10720 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10722 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10723 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10725 CASE_FLT_FN (BUILT_IN_FMIN):
10726 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10728 CASE_FLT_FN (BUILT_IN_FMAX):
10729 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10731 case BUILT_IN_ISGREATER:
10732 return fold_builtin_unordered_cmp (loc, fndecl,
10733 arg0, arg1, UNLE_EXPR, LE_EXPR);
10734 case BUILT_IN_ISGREATEREQUAL:
10735 return fold_builtin_unordered_cmp (loc, fndecl,
10736 arg0, arg1, UNLT_EXPR, LT_EXPR);
10737 case BUILT_IN_ISLESS:
10738 return fold_builtin_unordered_cmp (loc, fndecl,
10739 arg0, arg1, UNGE_EXPR, GE_EXPR);
10740 case BUILT_IN_ISLESSEQUAL:
10741 return fold_builtin_unordered_cmp (loc, fndecl,
10742 arg0, arg1, UNGT_EXPR, GT_EXPR);
10743 case BUILT_IN_ISLESSGREATER:
10744 return fold_builtin_unordered_cmp (loc, fndecl,
10745 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10746 case BUILT_IN_ISUNORDERED:
10747 return fold_builtin_unordered_cmp (loc, fndecl,
10748 arg0, arg1, UNORDERED_EXPR,
10749 NOP_EXPR);
10751 /* We do the folding for va_start in the expander. */
10752 case BUILT_IN_VA_START:
10753 break;
10755 case BUILT_IN_SPRINTF:
10756 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10758 case BUILT_IN_OBJECT_SIZE:
10759 return fold_builtin_object_size (arg0, arg1);
10761 case BUILT_IN_PRINTF:
10762 case BUILT_IN_PRINTF_UNLOCKED:
10763 case BUILT_IN_VPRINTF:
10764 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10766 case BUILT_IN_PRINTF_CHK:
10767 case BUILT_IN_VPRINTF_CHK:
10768 if (!validate_arg (arg0, INTEGER_TYPE)
10769 || TREE_SIDE_EFFECTS (arg0))
10770 return NULL_TREE;
10771 else
10772 return fold_builtin_printf (loc, fndecl,
10773 arg1, NULL_TREE, ignore, fcode);
10774 break;
10776 case BUILT_IN_FPRINTF:
10777 case BUILT_IN_FPRINTF_UNLOCKED:
10778 case BUILT_IN_VFPRINTF:
10779 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10780 ignore, fcode);
10782 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10783 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10785 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10786 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10788 default:
10789 break;
10791 return NULL_TREE;
10794 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10795 and ARG2. IGNORE is true if the result of the function call is ignored.
10796 This function returns NULL_TREE if no simplification was possible. */
10798 static tree
10799 fold_builtin_3 (location_t loc, tree fndecl,
10800 tree arg0, tree arg1, tree arg2, bool ignore)
10802 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10803 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10804 switch (fcode)
10807 CASE_FLT_FN (BUILT_IN_SINCOS):
10808 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10810 CASE_FLT_FN (BUILT_IN_FMA):
10811 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10812 break;
10814 CASE_FLT_FN (BUILT_IN_REMQUO):
10815 if (validate_arg (arg0, REAL_TYPE)
10816 && validate_arg (arg1, REAL_TYPE)
10817 && validate_arg (arg2, POINTER_TYPE))
10818 return do_mpfr_remquo (arg0, arg1, arg2);
10819 break;
10821 case BUILT_IN_MEMSET:
10822 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10824 case BUILT_IN_BCOPY:
10825 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10826 void_type_node, true, /*endp=*/3);
10828 case BUILT_IN_MEMCPY:
10829 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10830 type, ignore, /*endp=*/0);
10832 case BUILT_IN_MEMPCPY:
10833 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10834 type, ignore, /*endp=*/1);
10836 case BUILT_IN_MEMMOVE:
10837 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10838 type, ignore, /*endp=*/3);
10840 case BUILT_IN_STRNCAT:
10841 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10843 case BUILT_IN_STRNCPY:
10844 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10846 case BUILT_IN_STRNCMP:
10847 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10849 case BUILT_IN_MEMCHR:
10850 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10852 case BUILT_IN_BCMP:
10853 case BUILT_IN_MEMCMP:
10854 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10856 case BUILT_IN_SPRINTF:
10857 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10859 case BUILT_IN_SNPRINTF:
10860 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10862 case BUILT_IN_STRCPY_CHK:
10863 case BUILT_IN_STPCPY_CHK:
10864 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10865 ignore, fcode);
10867 case BUILT_IN_STRCAT_CHK:
10868 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10870 case BUILT_IN_PRINTF_CHK:
10871 case BUILT_IN_VPRINTF_CHK:
10872 if (!validate_arg (arg0, INTEGER_TYPE)
10873 || TREE_SIDE_EFFECTS (arg0))
10874 return NULL_TREE;
10875 else
10876 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10877 break;
10879 case BUILT_IN_FPRINTF:
10880 case BUILT_IN_FPRINTF_UNLOCKED:
10881 case BUILT_IN_VFPRINTF:
10882 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10883 ignore, fcode);
10885 case BUILT_IN_FPRINTF_CHK:
10886 case BUILT_IN_VFPRINTF_CHK:
10887 if (!validate_arg (arg1, INTEGER_TYPE)
10888 || TREE_SIDE_EFFECTS (arg1))
10889 return NULL_TREE;
10890 else
10891 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10892 ignore, fcode);
10894 case BUILT_IN_EXPECT:
10895 return fold_builtin_expect (loc, arg0, arg1, arg2);
10897 default:
10898 break;
10900 return NULL_TREE;
10903 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10904 ARG2, and ARG3. IGNORE is true if the result of the function call is
10905 ignored. This function returns NULL_TREE if no simplification was
10906 possible. */
10908 static tree
10909 fold_builtin_4 (location_t loc, tree fndecl,
10910 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10912 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10914 switch (fcode)
10916 case BUILT_IN_MEMCPY_CHK:
10917 case BUILT_IN_MEMPCPY_CHK:
10918 case BUILT_IN_MEMMOVE_CHK:
10919 case BUILT_IN_MEMSET_CHK:
10920 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10921 NULL_TREE, ignore,
10922 DECL_FUNCTION_CODE (fndecl));
10924 case BUILT_IN_STRNCPY_CHK:
10925 case BUILT_IN_STPNCPY_CHK:
10926 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
10927 ignore, fcode);
10929 case BUILT_IN_STRNCAT_CHK:
10930 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10932 case BUILT_IN_SNPRINTF:
10933 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10935 case BUILT_IN_FPRINTF_CHK:
10936 case BUILT_IN_VFPRINTF_CHK:
10937 if (!validate_arg (arg1, INTEGER_TYPE)
10938 || TREE_SIDE_EFFECTS (arg1))
10939 return NULL_TREE;
10940 else
10941 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10942 ignore, fcode);
10943 break;
10945 default:
10946 break;
10948 return NULL_TREE;
10951 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10952 arguments, where NARGS <= 4. IGNORE is true if the result of the
10953 function call is ignored. This function returns NULL_TREE if no
10954 simplification was possible. Note that this only folds builtins with
10955 fixed argument patterns. Foldings that do varargs-to-varargs
10956 transformations, or that match calls with more than 4 arguments,
10957 need to be handled with fold_builtin_varargs instead. */
10959 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10961 static tree
10962 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10964 tree ret = NULL_TREE;
10966 switch (nargs)
10968 case 0:
10969 ret = fold_builtin_0 (loc, fndecl, ignore);
10970 break;
10971 case 1:
10972 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10973 break;
10974 case 2:
10975 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10976 break;
10977 case 3:
10978 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10979 break;
10980 case 4:
10981 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10982 ignore);
10983 break;
10984 default:
10985 break;
10987 if (ret)
10989 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10990 SET_EXPR_LOCATION (ret, loc);
10991 TREE_NO_WARNING (ret) = 1;
10992 return ret;
10994 return NULL_TREE;
10997 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10998 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10999 of arguments in ARGS to be omitted. OLDNARGS is the number of
11000 elements in ARGS. */
11002 static tree
11003 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11004 int skip, tree fndecl, int n, va_list newargs)
11006 int nargs = oldnargs - skip + n;
11007 tree *buffer;
11009 if (n > 0)
11011 int i, j;
11013 buffer = XALLOCAVEC (tree, nargs);
11014 for (i = 0; i < n; i++)
11015 buffer[i] = va_arg (newargs, tree);
11016 for (j = skip; j < oldnargs; j++, i++)
11017 buffer[i] = args[j];
11019 else
11020 buffer = args + skip;
11022 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11025 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11026 list ARGS along with N new arguments specified as the "..."
11027 parameters. SKIP is the number of arguments in ARGS to be omitted.
11028 OLDNARGS is the number of elements in ARGS. */
11030 static tree
11031 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11032 int skip, tree fndecl, int n, ...)
11034 va_list ap;
11035 tree t;
11037 va_start (ap, n);
11038 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11039 va_end (ap);
11041 return t;
11044 /* Return true if FNDECL shouldn't be folded right now.
11045 If a built-in function has an inline attribute always_inline
11046 wrapper, defer folding it after always_inline functions have
11047 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11048 might not be performed. */
11050 bool
11051 avoid_folding_inline_builtin (tree fndecl)
11053 return (DECL_DECLARED_INLINE_P (fndecl)
11054 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11055 && cfun
11056 && !cfun->always_inline_functions_inlined
11057 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11060 /* A wrapper function for builtin folding that prevents warnings for
11061 "statement without effect" and the like, caused by removing the
11062 call node earlier than the warning is generated. */
11064 tree
11065 fold_call_expr (location_t loc, tree exp, bool ignore)
11067 tree ret = NULL_TREE;
11068 tree fndecl = get_callee_fndecl (exp);
11069 if (fndecl
11070 && TREE_CODE (fndecl) == FUNCTION_DECL
11071 && DECL_BUILT_IN (fndecl)
11072 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11073 yet. Defer folding until we see all the arguments
11074 (after inlining). */
11075 && !CALL_EXPR_VA_ARG_PACK (exp))
11077 int nargs = call_expr_nargs (exp);
11079 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11080 instead last argument is __builtin_va_arg_pack (). Defer folding
11081 even in that case, until arguments are finalized. */
11082 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11084 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11085 if (fndecl2
11086 && TREE_CODE (fndecl2) == FUNCTION_DECL
11087 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11088 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11089 return NULL_TREE;
11092 if (avoid_folding_inline_builtin (fndecl))
11093 return NULL_TREE;
11095 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11096 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11097 CALL_EXPR_ARGP (exp), ignore);
11098 else
11100 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11102 tree *args = CALL_EXPR_ARGP (exp);
11103 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11105 if (!ret)
11106 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11107 if (ret)
11108 return ret;
11111 return NULL_TREE;
11114 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11115 N arguments are passed in the array ARGARRAY. */
11117 tree
11118 fold_builtin_call_array (location_t loc, tree type,
11119 tree fn,
11120 int n,
11121 tree *argarray)
11123 tree ret = NULL_TREE;
11124 tree exp;
11126 if (TREE_CODE (fn) == ADDR_EXPR)
11128 tree fndecl = TREE_OPERAND (fn, 0);
11129 if (TREE_CODE (fndecl) == FUNCTION_DECL
11130 && DECL_BUILT_IN (fndecl))
11132 /* If last argument is __builtin_va_arg_pack (), arguments to this
11133 function are not finalized yet. Defer folding until they are. */
11134 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11136 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11137 if (fndecl2
11138 && TREE_CODE (fndecl2) == FUNCTION_DECL
11139 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11140 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11141 return build_call_array_loc (loc, type, fn, n, argarray);
11143 if (avoid_folding_inline_builtin (fndecl))
11144 return build_call_array_loc (loc, type, fn, n, argarray);
11145 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11147 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11148 if (ret)
11149 return ret;
11151 return build_call_array_loc (loc, type, fn, n, argarray);
11153 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11155 /* First try the transformations that don't require consing up
11156 an exp. */
11157 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11158 if (ret)
11159 return ret;
11162 /* If we got this far, we need to build an exp. */
11163 exp = build_call_array_loc (loc, type, fn, n, argarray);
11164 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11165 return ret ? ret : exp;
11169 return build_call_array_loc (loc, type, fn, n, argarray);
11172 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11173 along with N new arguments specified as the "..." parameters. SKIP
11174 is the number of arguments in EXP to be omitted. This function is used
11175 to do varargs-to-varargs transformations. */
11177 static tree
11178 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11180 va_list ap;
11181 tree t;
11183 va_start (ap, n);
11184 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11185 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11186 va_end (ap);
11188 return t;
11191 /* Validate a single argument ARG against a tree code CODE representing
11192 a type. */
11194 static bool
11195 validate_arg (const_tree arg, enum tree_code code)
11197 if (!arg)
11198 return false;
11199 else if (code == POINTER_TYPE)
11200 return POINTER_TYPE_P (TREE_TYPE (arg));
11201 else if (code == INTEGER_TYPE)
11202 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11203 return code == TREE_CODE (TREE_TYPE (arg));
11206 /* This function validates the types of a function call argument list
11207 against a specified list of tree_codes. If the last specifier is a 0,
11208 that represents an ellipses, otherwise the last specifier must be a
11209 VOID_TYPE.
11211 This is the GIMPLE version of validate_arglist. Eventually we want to
11212 completely convert builtins.c to work from GIMPLEs and the tree based
11213 validate_arglist will then be removed. */
11215 bool
11216 validate_gimple_arglist (const_gimple call, ...)
11218 enum tree_code code;
11219 bool res = 0;
11220 va_list ap;
11221 const_tree arg;
11222 size_t i;
11224 va_start (ap, call);
11225 i = 0;
11229 code = (enum tree_code) va_arg (ap, int);
11230 switch (code)
11232 case 0:
11233 /* This signifies an ellipses, any further arguments are all ok. */
11234 res = true;
11235 goto end;
11236 case VOID_TYPE:
11237 /* This signifies an endlink, if no arguments remain, return
11238 true, otherwise return false. */
11239 res = (i == gimple_call_num_args (call));
11240 goto end;
11241 default:
11242 /* If no parameters remain or the parameter's code does not
11243 match the specified code, return false. Otherwise continue
11244 checking any remaining arguments. */
11245 arg = gimple_call_arg (call, i++);
11246 if (!validate_arg (arg, code))
11247 goto end;
11248 break;
11251 while (1);
11253 /* We need gotos here since we can only have one VA_CLOSE in a
11254 function. */
11255 end: ;
11256 va_end (ap);
11258 return res;
11261 /* Default target-specific builtin expander that does nothing. */
11264 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11265 rtx target ATTRIBUTE_UNUSED,
11266 rtx subtarget ATTRIBUTE_UNUSED,
11267 enum machine_mode mode ATTRIBUTE_UNUSED,
11268 int ignore ATTRIBUTE_UNUSED)
11270 return NULL_RTX;
11273 /* Returns true is EXP represents data that would potentially reside
11274 in a readonly section. */
11276 static bool
11277 readonly_data_expr (tree exp)
11279 STRIP_NOPS (exp);
11281 if (TREE_CODE (exp) != ADDR_EXPR)
11282 return false;
11284 exp = get_base_address (TREE_OPERAND (exp, 0));
11285 if (!exp)
11286 return false;
11288 /* Make sure we call decl_readonly_section only for trees it
11289 can handle (since it returns true for everything it doesn't
11290 understand). */
11291 if (TREE_CODE (exp) == STRING_CST
11292 || TREE_CODE (exp) == CONSTRUCTOR
11293 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11294 return decl_readonly_section (exp, 0);
11295 else
11296 return false;
11299 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11300 to the call, and TYPE is its return type.
11302 Return NULL_TREE if no simplification was possible, otherwise return the
11303 simplified form of the call as a tree.
11305 The simplified form may be a constant or other expression which
11306 computes the same value, but in a more efficient manner (including
11307 calls to other builtin functions).
11309 The call may contain arguments which need to be evaluated, but
11310 which are not useful to determine the result of the call. In
11311 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11312 COMPOUND_EXPR will be an argument which must be evaluated.
11313 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11314 COMPOUND_EXPR in the chain will contain the tree for the simplified
11315 form of the builtin function call. */
11317 static tree
11318 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11320 if (!validate_arg (s1, POINTER_TYPE)
11321 || !validate_arg (s2, POINTER_TYPE))
11322 return NULL_TREE;
11323 else
11325 tree fn;
11326 const char *p1, *p2;
11328 p2 = c_getstr (s2);
11329 if (p2 == NULL)
11330 return NULL_TREE;
11332 p1 = c_getstr (s1);
11333 if (p1 != NULL)
11335 const char *r = strstr (p1, p2);
11336 tree tem;
11338 if (r == NULL)
11339 return build_int_cst (TREE_TYPE (s1), 0);
11341 /* Return an offset into the constant string argument. */
11342 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11343 return fold_convert_loc (loc, type, tem);
11346 /* The argument is const char *, and the result is char *, so we need
11347 a type conversion here to avoid a warning. */
11348 if (p2[0] == '\0')
11349 return fold_convert_loc (loc, type, s1);
11351 if (p2[1] != '\0')
11352 return NULL_TREE;
11354 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11355 if (!fn)
11356 return NULL_TREE;
11358 /* New argument list transforming strstr(s1, s2) to
11359 strchr(s1, s2[0]). */
11360 return build_call_expr_loc (loc, fn, 2, s1,
11361 build_int_cst (integer_type_node, p2[0]));
11365 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11366 the call, and TYPE is its return type.
11368 Return NULL_TREE if no simplification was possible, otherwise return the
11369 simplified form of the call as a tree.
11371 The simplified form may be a constant or other expression which
11372 computes the same value, but in a more efficient manner (including
11373 calls to other builtin functions).
11375 The call may contain arguments which need to be evaluated, but
11376 which are not useful to determine the result of the call. In
11377 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11378 COMPOUND_EXPR will be an argument which must be evaluated.
11379 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11380 COMPOUND_EXPR in the chain will contain the tree for the simplified
11381 form of the builtin function call. */
11383 static tree
11384 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11386 if (!validate_arg (s1, POINTER_TYPE)
11387 || !validate_arg (s2, INTEGER_TYPE))
11388 return NULL_TREE;
11389 else
11391 const char *p1;
11393 if (TREE_CODE (s2) != INTEGER_CST)
11394 return NULL_TREE;
11396 p1 = c_getstr (s1);
11397 if (p1 != NULL)
11399 char c;
11400 const char *r;
11401 tree tem;
11403 if (target_char_cast (s2, &c))
11404 return NULL_TREE;
11406 r = strchr (p1, c);
11408 if (r == NULL)
11409 return build_int_cst (TREE_TYPE (s1), 0);
11411 /* Return an offset into the constant string argument. */
11412 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11413 return fold_convert_loc (loc, type, tem);
11415 return NULL_TREE;
11419 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11420 the call, and TYPE is its return type.
11422 Return NULL_TREE if no simplification was possible, otherwise return the
11423 simplified form of the call as a tree.
11425 The simplified form may be a constant or other expression which
11426 computes the same value, but in a more efficient manner (including
11427 calls to other builtin functions).
11429 The call may contain arguments which need to be evaluated, but
11430 which are not useful to determine the result of the call. In
11431 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11432 COMPOUND_EXPR will be an argument which must be evaluated.
11433 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11434 COMPOUND_EXPR in the chain will contain the tree for the simplified
11435 form of the builtin function call. */
11437 static tree
11438 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11440 if (!validate_arg (s1, POINTER_TYPE)
11441 || !validate_arg (s2, INTEGER_TYPE))
11442 return NULL_TREE;
11443 else
11445 tree fn;
11446 const char *p1;
11448 if (TREE_CODE (s2) != INTEGER_CST)
11449 return NULL_TREE;
11451 p1 = c_getstr (s1);
11452 if (p1 != NULL)
11454 char c;
11455 const char *r;
11456 tree tem;
11458 if (target_char_cast (s2, &c))
11459 return NULL_TREE;
11461 r = strrchr (p1, c);
11463 if (r == NULL)
11464 return build_int_cst (TREE_TYPE (s1), 0);
11466 /* Return an offset into the constant string argument. */
11467 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11468 return fold_convert_loc (loc, type, tem);
11471 if (! integer_zerop (s2))
11472 return NULL_TREE;
11474 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11475 if (!fn)
11476 return NULL_TREE;
11478 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11479 return build_call_expr_loc (loc, fn, 2, s1, s2);
11483 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11484 to the call, and TYPE is its return type.
11486 Return NULL_TREE if no simplification was possible, otherwise return the
11487 simplified form of the call as a tree.
11489 The simplified form may be a constant or other expression which
11490 computes the same value, but in a more efficient manner (including
11491 calls to other builtin functions).
11493 The call may contain arguments which need to be evaluated, but
11494 which are not useful to determine the result of the call. In
11495 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11496 COMPOUND_EXPR will be an argument which must be evaluated.
11497 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11498 COMPOUND_EXPR in the chain will contain the tree for the simplified
11499 form of the builtin function call. */
11501 static tree
11502 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11504 if (!validate_arg (s1, POINTER_TYPE)
11505 || !validate_arg (s2, POINTER_TYPE))
11506 return NULL_TREE;
11507 else
11509 tree fn;
11510 const char *p1, *p2;
11512 p2 = c_getstr (s2);
11513 if (p2 == NULL)
11514 return NULL_TREE;
11516 p1 = c_getstr (s1);
11517 if (p1 != NULL)
11519 const char *r = strpbrk (p1, p2);
11520 tree tem;
11522 if (r == NULL)
11523 return build_int_cst (TREE_TYPE (s1), 0);
11525 /* Return an offset into the constant string argument. */
11526 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11527 return fold_convert_loc (loc, type, tem);
11530 if (p2[0] == '\0')
11531 /* strpbrk(x, "") == NULL.
11532 Evaluate and ignore s1 in case it had side-effects. */
11533 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11535 if (p2[1] != '\0')
11536 return NULL_TREE; /* Really call strpbrk. */
11538 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11539 if (!fn)
11540 return NULL_TREE;
11542 /* New argument list transforming strpbrk(s1, s2) to
11543 strchr(s1, s2[0]). */
11544 return build_call_expr_loc (loc, fn, 2, s1,
11545 build_int_cst (integer_type_node, p2[0]));
11549 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11550 to the call.
11552 Return NULL_TREE if no simplification was possible, otherwise return the
11553 simplified form of the call as a tree.
11555 The simplified form may be a constant or other expression which
11556 computes the same value, but in a more efficient manner (including
11557 calls to other builtin functions).
11559 The call may contain arguments which need to be evaluated, but
11560 which are not useful to determine the result of the call. In
11561 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11562 COMPOUND_EXPR will be an argument which must be evaluated.
11563 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11564 COMPOUND_EXPR in the chain will contain the tree for the simplified
11565 form of the builtin function call. */
11567 tree
11568 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src,
11569 tree len)
11571 if (!validate_arg (dst, POINTER_TYPE)
11572 || !validate_arg (src, POINTER_TYPE))
11573 return NULL_TREE;
11574 else
11576 const char *p = c_getstr (src);
11578 /* If the string length is zero, return the dst parameter. */
11579 if (p && *p == '\0')
11580 return dst;
11582 if (optimize_insn_for_speed_p ())
11584 /* See if we can store by pieces into (dst + strlen(dst)). */
11585 tree newdst, call;
11586 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11587 tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
11589 if (!strlen_fn || !memcpy_fn)
11590 return NULL_TREE;
11592 /* If the length of the source string isn't computable don't
11593 split strcat into strlen and memcpy. */
11594 if (! len)
11595 len = c_strlen (src, 1);
11596 if (! len || TREE_SIDE_EFFECTS (len))
11597 return NULL_TREE;
11599 /* Stabilize the argument list. */
11600 dst = builtin_save_expr (dst);
11602 /* Create strlen (dst). */
11603 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11604 /* Create (dst p+ strlen (dst)). */
11606 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11607 newdst = builtin_save_expr (newdst);
11609 len = fold_convert_loc (loc, size_type_node, len);
11610 len = size_binop_loc (loc, PLUS_EXPR, len,
11611 build_int_cst (size_type_node, 1));
11613 call = build_call_expr_loc (loc, memcpy_fn, 3, newdst, src, len);
11614 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11616 return NULL_TREE;
11620 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11621 arguments to the call.
11623 Return NULL_TREE if no simplification was possible, otherwise return the
11624 simplified form of the call as a tree.
11626 The simplified form may be a constant or other expression which
11627 computes the same value, but in a more efficient manner (including
11628 calls to other builtin functions).
11630 The call may contain arguments which need to be evaluated, but
11631 which are not useful to determine the result of the call. In
11632 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11633 COMPOUND_EXPR will be an argument which must be evaluated.
11634 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11635 COMPOUND_EXPR in the chain will contain the tree for the simplified
11636 form of the builtin function call. */
11638 static tree
11639 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11641 if (!validate_arg (dst, POINTER_TYPE)
11642 || !validate_arg (src, POINTER_TYPE)
11643 || !validate_arg (len, INTEGER_TYPE))
11644 return NULL_TREE;
11645 else
11647 const char *p = c_getstr (src);
11649 /* If the requested length is zero, or the src parameter string
11650 length is zero, return the dst parameter. */
11651 if (integer_zerop (len) || (p && *p == '\0'))
11652 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11654 /* If the requested len is greater than or equal to the string
11655 length, call strcat. */
11656 if (TREE_CODE (len) == INTEGER_CST && p
11657 && compare_tree_int (len, strlen (p)) >= 0)
11659 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11661 /* If the replacement _DECL isn't initialized, don't do the
11662 transformation. */
11663 if (!fn)
11664 return NULL_TREE;
11666 return build_call_expr_loc (loc, fn, 2, dst, src);
11668 return NULL_TREE;
11672 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11673 to the call.
11675 Return NULL_TREE if no simplification was possible, otherwise return the
11676 simplified form of the call as a tree.
11678 The simplified form may be a constant or other expression which
11679 computes the same value, but in a more efficient manner (including
11680 calls to other builtin functions).
11682 The call may contain arguments which need to be evaluated, but
11683 which are not useful to determine the result of the call. In
11684 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11685 COMPOUND_EXPR will be an argument which must be evaluated.
11686 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11687 COMPOUND_EXPR in the chain will contain the tree for the simplified
11688 form of the builtin function call. */
11690 static tree
11691 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11693 if (!validate_arg (s1, POINTER_TYPE)
11694 || !validate_arg (s2, POINTER_TYPE))
11695 return NULL_TREE;
11696 else
11698 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11700 /* If both arguments are constants, evaluate at compile-time. */
11701 if (p1 && p2)
11703 const size_t r = strspn (p1, p2);
11704 return build_int_cst (size_type_node, r);
11707 /* If either argument is "", return NULL_TREE. */
11708 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11709 /* Evaluate and ignore both arguments in case either one has
11710 side-effects. */
11711 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11712 s1, s2);
11713 return NULL_TREE;
11717 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11718 to the call.
11720 Return NULL_TREE if no simplification was possible, otherwise return the
11721 simplified form of the call as a tree.
11723 The simplified form may be a constant or other expression which
11724 computes the same value, but in a more efficient manner (including
11725 calls to other builtin functions).
11727 The call may contain arguments which need to be evaluated, but
11728 which are not useful to determine the result of the call. In
11729 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11730 COMPOUND_EXPR will be an argument which must be evaluated.
11731 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11732 COMPOUND_EXPR in the chain will contain the tree for the simplified
11733 form of the builtin function call. */
11735 static tree
11736 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11738 if (!validate_arg (s1, POINTER_TYPE)
11739 || !validate_arg (s2, POINTER_TYPE))
11740 return NULL_TREE;
11741 else
11743 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11745 /* If both arguments are constants, evaluate at compile-time. */
11746 if (p1 && p2)
11748 const size_t r = strcspn (p1, p2);
11749 return build_int_cst (size_type_node, r);
11752 /* If the first argument is "", return NULL_TREE. */
11753 if (p1 && *p1 == '\0')
11755 /* Evaluate and ignore argument s2 in case it has
11756 side-effects. */
11757 return omit_one_operand_loc (loc, size_type_node,
11758 size_zero_node, s2);
11761 /* If the second argument is "", return __builtin_strlen(s1). */
11762 if (p2 && *p2 == '\0')
11764 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11766 /* If the replacement _DECL isn't initialized, don't do the
11767 transformation. */
11768 if (!fn)
11769 return NULL_TREE;
11771 return build_call_expr_loc (loc, fn, 1, s1);
11773 return NULL_TREE;
11777 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11778 to the call. IGNORE is true if the value returned
11779 by the builtin will be ignored. UNLOCKED is true is true if this
11780 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11781 the known length of the string. Return NULL_TREE if no simplification
11782 was possible. */
11784 tree
11785 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11786 bool ignore, bool unlocked, tree len)
11788 /* If we're using an unlocked function, assume the other unlocked
11789 functions exist explicitly. */
11790 tree const fn_fputc = (unlocked
11791 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11792 : builtin_decl_implicit (BUILT_IN_FPUTC));
11793 tree const fn_fwrite = (unlocked
11794 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
11795 : builtin_decl_implicit (BUILT_IN_FWRITE));
11797 /* If the return value is used, don't do the transformation. */
11798 if (!ignore)
11799 return NULL_TREE;
11801 /* Verify the arguments in the original call. */
11802 if (!validate_arg (arg0, POINTER_TYPE)
11803 || !validate_arg (arg1, POINTER_TYPE))
11804 return NULL_TREE;
11806 if (! len)
11807 len = c_strlen (arg0, 0);
11809 /* Get the length of the string passed to fputs. If the length
11810 can't be determined, punt. */
11811 if (!len
11812 || TREE_CODE (len) != INTEGER_CST)
11813 return NULL_TREE;
11815 switch (compare_tree_int (len, 1))
11817 case -1: /* length is 0, delete the call entirely . */
11818 return omit_one_operand_loc (loc, integer_type_node,
11819 integer_zero_node, arg1);;
11821 case 0: /* length is 1, call fputc. */
11823 const char *p = c_getstr (arg0);
11825 if (p != NULL)
11827 if (fn_fputc)
11828 return build_call_expr_loc (loc, fn_fputc, 2,
11829 build_int_cst
11830 (integer_type_node, p[0]), arg1);
11831 else
11832 return NULL_TREE;
11835 /* FALLTHROUGH */
11836 case 1: /* length is greater than 1, call fwrite. */
11838 /* If optimizing for size keep fputs. */
11839 if (optimize_function_for_size_p (cfun))
11840 return NULL_TREE;
11841 /* New argument list transforming fputs(string, stream) to
11842 fwrite(string, 1, len, stream). */
11843 if (fn_fwrite)
11844 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11845 size_one_node, len, arg1);
11846 else
11847 return NULL_TREE;
11849 default:
11850 gcc_unreachable ();
11852 return NULL_TREE;
11855 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11856 produced. False otherwise. This is done so that we don't output the error
11857 or warning twice or three times. */
11859 bool
11860 fold_builtin_next_arg (tree exp, bool va_start_p)
11862 tree fntype = TREE_TYPE (current_function_decl);
11863 int nargs = call_expr_nargs (exp);
11864 tree arg;
11865 /* There is good chance the current input_location points inside the
11866 definition of the va_start macro (perhaps on the token for
11867 builtin) in a system header, so warnings will not be emitted.
11868 Use the location in real source code. */
11869 source_location current_location =
11870 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11871 NULL);
11873 if (!stdarg_p (fntype))
11875 error ("%<va_start%> used in function with fixed args");
11876 return true;
11879 if (va_start_p)
11881 if (va_start_p && (nargs != 2))
11883 error ("wrong number of arguments to function %<va_start%>");
11884 return true;
11886 arg = CALL_EXPR_ARG (exp, 1);
11888 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11889 when we checked the arguments and if needed issued a warning. */
11890 else
11892 if (nargs == 0)
11894 /* Evidently an out of date version of <stdarg.h>; can't validate
11895 va_start's second argument, but can still work as intended. */
11896 warning_at (current_location,
11897 OPT_Wvarargs,
11898 "%<__builtin_next_arg%> called without an argument");
11899 return true;
11901 else if (nargs > 1)
11903 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11904 return true;
11906 arg = CALL_EXPR_ARG (exp, 0);
11909 if (TREE_CODE (arg) == SSA_NAME)
11910 arg = SSA_NAME_VAR (arg);
11912 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11913 or __builtin_next_arg (0) the first time we see it, after checking
11914 the arguments and if needed issuing a warning. */
11915 if (!integer_zerop (arg))
11917 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11919 /* Strip off all nops for the sake of the comparison. This
11920 is not quite the same as STRIP_NOPS. It does more.
11921 We must also strip off INDIRECT_EXPR for C++ reference
11922 parameters. */
11923 while (CONVERT_EXPR_P (arg)
11924 || TREE_CODE (arg) == INDIRECT_REF)
11925 arg = TREE_OPERAND (arg, 0);
11926 if (arg != last_parm)
11928 /* FIXME: Sometimes with the tree optimizers we can get the
11929 not the last argument even though the user used the last
11930 argument. We just warn and set the arg to be the last
11931 argument so that we will get wrong-code because of
11932 it. */
11933 warning_at (current_location,
11934 OPT_Wvarargs,
11935 "second parameter of %<va_start%> not last named argument");
11938 /* Undefined by C99 7.15.1.4p4 (va_start):
11939 "If the parameter parmN is declared with the register storage
11940 class, with a function or array type, or with a type that is
11941 not compatible with the type that results after application of
11942 the default argument promotions, the behavior is undefined."
11944 else if (DECL_REGISTER (arg))
11946 warning_at (current_location,
11947 OPT_Wvarargs,
11948 "undefined behaviour when second parameter of "
11949 "%<va_start%> is declared with %<register%> storage");
11952 /* We want to verify the second parameter just once before the tree
11953 optimizers are run and then avoid keeping it in the tree,
11954 as otherwise we could warn even for correct code like:
11955 void foo (int i, ...)
11956 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11957 if (va_start_p)
11958 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11959 else
11960 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11962 return false;
11966 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11967 ORIG may be null if this is a 2-argument call. We don't attempt to
11968 simplify calls with more than 3 arguments.
11970 Return NULL_TREE if no simplification was possible, otherwise return the
11971 simplified form of the call as a tree. If IGNORED is true, it means that
11972 the caller does not use the returned value of the function. */
11974 static tree
11975 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
11976 tree orig, int ignored)
11978 tree call, retval;
11979 const char *fmt_str = NULL;
11981 /* Verify the required arguments in the original call. We deal with two
11982 types of sprintf() calls: 'sprintf (str, fmt)' and
11983 'sprintf (dest, "%s", orig)'. */
11984 if (!validate_arg (dest, POINTER_TYPE)
11985 || !validate_arg (fmt, POINTER_TYPE))
11986 return NULL_TREE;
11987 if (orig && !validate_arg (orig, POINTER_TYPE))
11988 return NULL_TREE;
11990 /* Check whether the format is a literal string constant. */
11991 fmt_str = c_getstr (fmt);
11992 if (fmt_str == NULL)
11993 return NULL_TREE;
11995 call = NULL_TREE;
11996 retval = NULL_TREE;
11998 if (!init_target_chars ())
11999 return NULL_TREE;
12001 /* If the format doesn't contain % args or %%, use strcpy. */
12002 if (strchr (fmt_str, target_percent) == NULL)
12004 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12006 if (!fn)
12007 return NULL_TREE;
12009 /* Don't optimize sprintf (buf, "abc", ptr++). */
12010 if (orig)
12011 return NULL_TREE;
12013 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12014 'format' is known to contain no % formats. */
12015 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12016 if (!ignored)
12017 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12020 /* If the format is "%s", use strcpy if the result isn't used. */
12021 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12023 tree fn;
12024 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12026 if (!fn)
12027 return NULL_TREE;
12029 /* Don't crash on sprintf (str1, "%s"). */
12030 if (!orig)
12031 return NULL_TREE;
12033 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12034 if (!ignored)
12036 retval = c_strlen (orig, 1);
12037 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12038 return NULL_TREE;
12040 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12043 if (call && retval)
12045 retval = fold_convert_loc
12046 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12047 retval);
12048 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12050 else
12051 return call;
12054 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12055 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12056 attempt to simplify calls with more than 4 arguments.
12058 Return NULL_TREE if no simplification was possible, otherwise return the
12059 simplified form of the call as a tree. If IGNORED is true, it means that
12060 the caller does not use the returned value of the function. */
12062 static tree
12063 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12064 tree orig, int ignored)
12066 tree call, retval;
12067 const char *fmt_str = NULL;
12068 unsigned HOST_WIDE_INT destlen;
12070 /* Verify the required arguments in the original call. We deal with two
12071 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12072 'snprintf (dest, cst, "%s", orig)'. */
12073 if (!validate_arg (dest, POINTER_TYPE)
12074 || !validate_arg (destsize, INTEGER_TYPE)
12075 || !validate_arg (fmt, POINTER_TYPE))
12076 return NULL_TREE;
12077 if (orig && !validate_arg (orig, POINTER_TYPE))
12078 return NULL_TREE;
12080 if (!tree_fits_uhwi_p (destsize))
12081 return NULL_TREE;
12083 /* Check whether the format is a literal string constant. */
12084 fmt_str = c_getstr (fmt);
12085 if (fmt_str == NULL)
12086 return NULL_TREE;
12088 call = NULL_TREE;
12089 retval = NULL_TREE;
12091 if (!init_target_chars ())
12092 return NULL_TREE;
12094 destlen = tree_to_uhwi (destsize);
12096 /* If the format doesn't contain % args or %%, use strcpy. */
12097 if (strchr (fmt_str, target_percent) == NULL)
12099 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12100 size_t len = strlen (fmt_str);
12102 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12103 if (orig)
12104 return NULL_TREE;
12106 /* We could expand this as
12107 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12108 or to
12109 memcpy (str, fmt_with_nul_at_cstm1, cst);
12110 but in the former case that might increase code size
12111 and in the latter case grow .rodata section too much.
12112 So punt for now. */
12113 if (len >= destlen)
12114 return NULL_TREE;
12116 if (!fn)
12117 return NULL_TREE;
12119 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12120 'format' is known to contain no % formats and
12121 strlen (fmt) < cst. */
12122 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12124 if (!ignored)
12125 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12128 /* If the format is "%s", use strcpy if the result isn't used. */
12129 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12131 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12132 unsigned HOST_WIDE_INT origlen;
12134 /* Don't crash on snprintf (str1, cst, "%s"). */
12135 if (!orig)
12136 return NULL_TREE;
12138 retval = c_strlen (orig, 1);
12139 if (!retval || !tree_fits_uhwi_p (retval))
12140 return NULL_TREE;
12142 origlen = tree_to_uhwi (retval);
12143 /* We could expand this as
12144 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12145 or to
12146 memcpy (str1, str2_with_nul_at_cstm1, cst);
12147 but in the former case that might increase code size
12148 and in the latter case grow .rodata section too much.
12149 So punt for now. */
12150 if (origlen >= destlen)
12151 return NULL_TREE;
12153 /* Convert snprintf (str1, cst, "%s", str2) into
12154 strcpy (str1, str2) if strlen (str2) < cst. */
12155 if (!fn)
12156 return NULL_TREE;
12158 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12160 if (ignored)
12161 retval = NULL_TREE;
12164 if (call && retval)
12166 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12167 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12168 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12170 else
12171 return call;
12174 /* Expand a call EXP to __builtin_object_size. */
12176 static rtx
12177 expand_builtin_object_size (tree exp)
12179 tree ost;
12180 int object_size_type;
12181 tree fndecl = get_callee_fndecl (exp);
12183 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12185 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12186 exp, fndecl);
12187 expand_builtin_trap ();
12188 return const0_rtx;
12191 ost = CALL_EXPR_ARG (exp, 1);
12192 STRIP_NOPS (ost);
12194 if (TREE_CODE (ost) != INTEGER_CST
12195 || tree_int_cst_sgn (ost) < 0
12196 || compare_tree_int (ost, 3) > 0)
12198 error ("%Klast argument of %D is not integer constant between 0 and 3",
12199 exp, fndecl);
12200 expand_builtin_trap ();
12201 return const0_rtx;
12204 object_size_type = tree_to_shwi (ost);
12206 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12209 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12210 FCODE is the BUILT_IN_* to use.
12211 Return NULL_RTX if we failed; the caller should emit a normal call,
12212 otherwise try to get the result in TARGET, if convenient (and in
12213 mode MODE if that's convenient). */
12215 static rtx
12216 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12217 enum built_in_function fcode)
12219 tree dest, src, len, size;
12221 if (!validate_arglist (exp,
12222 POINTER_TYPE,
12223 fcode == BUILT_IN_MEMSET_CHK
12224 ? INTEGER_TYPE : POINTER_TYPE,
12225 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12226 return NULL_RTX;
12228 dest = CALL_EXPR_ARG (exp, 0);
12229 src = CALL_EXPR_ARG (exp, 1);
12230 len = CALL_EXPR_ARG (exp, 2);
12231 size = CALL_EXPR_ARG (exp, 3);
12233 if (! tree_fits_uhwi_p (size))
12234 return NULL_RTX;
12236 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
12238 tree fn;
12240 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12242 warning_at (tree_nonartificial_location (exp),
12243 0, "%Kcall to %D will always overflow destination buffer",
12244 exp, get_callee_fndecl (exp));
12245 return NULL_RTX;
12248 fn = NULL_TREE;
12249 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12250 mem{cpy,pcpy,move,set} is available. */
12251 switch (fcode)
12253 case BUILT_IN_MEMCPY_CHK:
12254 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12255 break;
12256 case BUILT_IN_MEMPCPY_CHK:
12257 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12258 break;
12259 case BUILT_IN_MEMMOVE_CHK:
12260 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12261 break;
12262 case BUILT_IN_MEMSET_CHK:
12263 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12264 break;
12265 default:
12266 break;
12269 if (! fn)
12270 return NULL_RTX;
12272 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12273 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12274 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12275 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12277 else if (fcode == BUILT_IN_MEMSET_CHK)
12278 return NULL_RTX;
12279 else
12281 unsigned int dest_align = get_pointer_alignment (dest);
12283 /* If DEST is not a pointer type, call the normal function. */
12284 if (dest_align == 0)
12285 return NULL_RTX;
12287 /* If SRC and DEST are the same (and not volatile), do nothing. */
12288 if (operand_equal_p (src, dest, 0))
12290 tree expr;
12292 if (fcode != BUILT_IN_MEMPCPY_CHK)
12294 /* Evaluate and ignore LEN in case it has side-effects. */
12295 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12296 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12299 expr = fold_build_pointer_plus (dest, len);
12300 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12303 /* __memmove_chk special case. */
12304 if (fcode == BUILT_IN_MEMMOVE_CHK)
12306 unsigned int src_align = get_pointer_alignment (src);
12308 if (src_align == 0)
12309 return NULL_RTX;
12311 /* If src is categorized for a readonly section we can use
12312 normal __memcpy_chk. */
12313 if (readonly_data_expr (src))
12315 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12316 if (!fn)
12317 return NULL_RTX;
12318 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12319 dest, src, len, size);
12320 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12321 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12322 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12325 return NULL_RTX;
12329 /* Emit warning if a buffer overflow is detected at compile time. */
12331 static void
12332 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12334 int is_strlen = 0;
12335 tree len, size;
12336 location_t loc = tree_nonartificial_location (exp);
12338 switch (fcode)
12340 case BUILT_IN_STRCPY_CHK:
12341 case BUILT_IN_STPCPY_CHK:
12342 /* For __strcat_chk the warning will be emitted only if overflowing
12343 by at least strlen (dest) + 1 bytes. */
12344 case BUILT_IN_STRCAT_CHK:
12345 len = CALL_EXPR_ARG (exp, 1);
12346 size = CALL_EXPR_ARG (exp, 2);
12347 is_strlen = 1;
12348 break;
12349 case BUILT_IN_STRNCAT_CHK:
12350 case BUILT_IN_STRNCPY_CHK:
12351 case BUILT_IN_STPNCPY_CHK:
12352 len = CALL_EXPR_ARG (exp, 2);
12353 size = CALL_EXPR_ARG (exp, 3);
12354 break;
12355 case BUILT_IN_SNPRINTF_CHK:
12356 case BUILT_IN_VSNPRINTF_CHK:
12357 len = CALL_EXPR_ARG (exp, 1);
12358 size = CALL_EXPR_ARG (exp, 3);
12359 break;
12360 default:
12361 gcc_unreachable ();
12364 if (!len || !size)
12365 return;
12367 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12368 return;
12370 if (is_strlen)
12372 len = c_strlen (len, 1);
12373 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12374 return;
12376 else if (fcode == BUILT_IN_STRNCAT_CHK)
12378 tree src = CALL_EXPR_ARG (exp, 1);
12379 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12380 return;
12381 src = c_strlen (src, 1);
12382 if (! src || ! tree_fits_uhwi_p (src))
12384 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12385 exp, get_callee_fndecl (exp));
12386 return;
12388 else if (tree_int_cst_lt (src, size))
12389 return;
12391 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
12392 return;
12394 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12395 exp, get_callee_fndecl (exp));
12398 /* Emit warning if a buffer overflow is detected at compile time
12399 in __sprintf_chk/__vsprintf_chk calls. */
12401 static void
12402 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12404 tree size, len, fmt;
12405 const char *fmt_str;
12406 int nargs = call_expr_nargs (exp);
12408 /* Verify the required arguments in the original call. */
12410 if (nargs < 4)
12411 return;
12412 size = CALL_EXPR_ARG (exp, 2);
12413 fmt = CALL_EXPR_ARG (exp, 3);
12415 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12416 return;
12418 /* Check whether the format is a literal string constant. */
12419 fmt_str = c_getstr (fmt);
12420 if (fmt_str == NULL)
12421 return;
12423 if (!init_target_chars ())
12424 return;
12426 /* If the format doesn't contain % args or %%, we know its size. */
12427 if (strchr (fmt_str, target_percent) == 0)
12428 len = build_int_cstu (size_type_node, strlen (fmt_str));
12429 /* If the format is "%s" and first ... argument is a string literal,
12430 we know it too. */
12431 else if (fcode == BUILT_IN_SPRINTF_CHK
12432 && strcmp (fmt_str, target_percent_s) == 0)
12434 tree arg;
12436 if (nargs < 5)
12437 return;
12438 arg = CALL_EXPR_ARG (exp, 4);
12439 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12440 return;
12442 len = c_strlen (arg, 1);
12443 if (!len || ! tree_fits_uhwi_p (len))
12444 return;
12446 else
12447 return;
12449 if (! tree_int_cst_lt (len, size))
12450 warning_at (tree_nonartificial_location (exp),
12451 0, "%Kcall to %D will always overflow destination buffer",
12452 exp, get_callee_fndecl (exp));
12455 /* Emit warning if a free is called with address of a variable. */
12457 static void
12458 maybe_emit_free_warning (tree exp)
12460 tree arg = CALL_EXPR_ARG (exp, 0);
12462 STRIP_NOPS (arg);
12463 if (TREE_CODE (arg) != ADDR_EXPR)
12464 return;
12466 arg = get_base_address (TREE_OPERAND (arg, 0));
12467 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12468 return;
12470 if (SSA_VAR_P (arg))
12471 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12472 "%Kattempt to free a non-heap object %qD", exp, arg);
12473 else
12474 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12475 "%Kattempt to free a non-heap object", exp);
12478 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12479 if possible. */
12481 static tree
12482 fold_builtin_object_size (tree ptr, tree ost)
12484 unsigned HOST_WIDE_INT bytes;
12485 int object_size_type;
12487 if (!validate_arg (ptr, POINTER_TYPE)
12488 || !validate_arg (ost, INTEGER_TYPE))
12489 return NULL_TREE;
12491 STRIP_NOPS (ost);
12493 if (TREE_CODE (ost) != INTEGER_CST
12494 || tree_int_cst_sgn (ost) < 0
12495 || compare_tree_int (ost, 3) > 0)
12496 return NULL_TREE;
12498 object_size_type = tree_to_shwi (ost);
12500 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12501 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12502 and (size_t) 0 for types 2 and 3. */
12503 if (TREE_SIDE_EFFECTS (ptr))
12504 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12506 if (TREE_CODE (ptr) == ADDR_EXPR)
12508 bytes = compute_builtin_object_size (ptr, object_size_type);
12509 if (wi::fits_to_tree_p (bytes, size_type_node))
12510 return build_int_cstu (size_type_node, bytes);
12512 else if (TREE_CODE (ptr) == SSA_NAME)
12514 /* If object size is not known yet, delay folding until
12515 later. Maybe subsequent passes will help determining
12516 it. */
12517 bytes = compute_builtin_object_size (ptr, object_size_type);
12518 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12519 && wi::fits_to_tree_p (bytes, size_type_node))
12520 return build_int_cstu (size_type_node, bytes);
12523 return NULL_TREE;
12526 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12527 DEST, SRC, LEN, and SIZE are the arguments to the call.
12528 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12529 code of the builtin. If MAXLEN is not NULL, it is maximum length
12530 passed as third argument. */
12532 tree
12533 fold_builtin_memory_chk (location_t loc, tree fndecl,
12534 tree dest, tree src, tree len, tree size,
12535 tree maxlen, bool ignore,
12536 enum built_in_function fcode)
12538 tree fn;
12540 if (!validate_arg (dest, POINTER_TYPE)
12541 || !validate_arg (src,
12542 (fcode == BUILT_IN_MEMSET_CHK
12543 ? INTEGER_TYPE : POINTER_TYPE))
12544 || !validate_arg (len, INTEGER_TYPE)
12545 || !validate_arg (size, INTEGER_TYPE))
12546 return NULL_TREE;
12548 /* If SRC and DEST are the same (and not volatile), return DEST
12549 (resp. DEST+LEN for __mempcpy_chk). */
12550 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12552 if (fcode != BUILT_IN_MEMPCPY_CHK)
12553 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12554 dest, len);
12555 else
12557 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12558 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12562 if (! tree_fits_uhwi_p (size))
12563 return NULL_TREE;
12565 if (! integer_all_onesp (size))
12567 if (! tree_fits_uhwi_p (len))
12569 /* If LEN is not constant, try MAXLEN too.
12570 For MAXLEN only allow optimizing into non-_ocs function
12571 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12572 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12574 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12576 /* (void) __mempcpy_chk () can be optimized into
12577 (void) __memcpy_chk (). */
12578 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12579 if (!fn)
12580 return NULL_TREE;
12582 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12584 return NULL_TREE;
12587 else
12588 maxlen = len;
12590 if (tree_int_cst_lt (size, maxlen))
12591 return NULL_TREE;
12594 fn = NULL_TREE;
12595 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12596 mem{cpy,pcpy,move,set} is available. */
12597 switch (fcode)
12599 case BUILT_IN_MEMCPY_CHK:
12600 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12601 break;
12602 case BUILT_IN_MEMPCPY_CHK:
12603 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12604 break;
12605 case BUILT_IN_MEMMOVE_CHK:
12606 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12607 break;
12608 case BUILT_IN_MEMSET_CHK:
12609 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12610 break;
12611 default:
12612 break;
12615 if (!fn)
12616 return NULL_TREE;
12618 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12621 /* Fold a call to the __st[rp]cpy_chk builtin.
12622 DEST, SRC, and SIZE are the arguments to the call.
12623 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12624 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12625 strings passed as second argument. */
12627 tree
12628 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12629 tree src, tree size,
12630 tree maxlen, bool ignore,
12631 enum built_in_function fcode)
12633 tree len, fn;
12635 if (!validate_arg (dest, POINTER_TYPE)
12636 || !validate_arg (src, POINTER_TYPE)
12637 || !validate_arg (size, INTEGER_TYPE))
12638 return NULL_TREE;
12640 /* If SRC and DEST are the same (and not volatile), return DEST. */
12641 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12642 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12644 if (! tree_fits_uhwi_p (size))
12645 return NULL_TREE;
12647 if (! integer_all_onesp (size))
12649 len = c_strlen (src, 1);
12650 if (! len || ! tree_fits_uhwi_p (len))
12652 /* If LEN is not constant, try MAXLEN too.
12653 For MAXLEN only allow optimizing into non-_ocs function
12654 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12655 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12657 if (fcode == BUILT_IN_STPCPY_CHK)
12659 if (! ignore)
12660 return NULL_TREE;
12662 /* If return value of __stpcpy_chk is ignored,
12663 optimize into __strcpy_chk. */
12664 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12665 if (!fn)
12666 return NULL_TREE;
12668 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12671 if (! len || TREE_SIDE_EFFECTS (len))
12672 return NULL_TREE;
12674 /* If c_strlen returned something, but not a constant,
12675 transform __strcpy_chk into __memcpy_chk. */
12676 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12677 if (!fn)
12678 return NULL_TREE;
12680 len = fold_convert_loc (loc, size_type_node, len);
12681 len = size_binop_loc (loc, PLUS_EXPR, len,
12682 build_int_cst (size_type_node, 1));
12683 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12684 build_call_expr_loc (loc, fn, 4,
12685 dest, src, len, size));
12688 else
12689 maxlen = len;
12691 if (! tree_int_cst_lt (maxlen, size))
12692 return NULL_TREE;
12695 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12696 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12697 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12698 if (!fn)
12699 return NULL_TREE;
12701 return build_call_expr_loc (loc, fn, 2, dest, src);
12704 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12705 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12706 length passed as third argument. IGNORE is true if return value can be
12707 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12709 tree
12710 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12711 tree len, tree size, tree maxlen, bool ignore,
12712 enum built_in_function fcode)
12714 tree fn;
12716 if (!validate_arg (dest, POINTER_TYPE)
12717 || !validate_arg (src, POINTER_TYPE)
12718 || !validate_arg (len, INTEGER_TYPE)
12719 || !validate_arg (size, INTEGER_TYPE))
12720 return NULL_TREE;
12722 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12724 /* If return value of __stpncpy_chk is ignored,
12725 optimize into __strncpy_chk. */
12726 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12727 if (fn)
12728 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12731 if (! tree_fits_uhwi_p (size))
12732 return NULL_TREE;
12734 if (! integer_all_onesp (size))
12736 if (! tree_fits_uhwi_p (len))
12738 /* If LEN is not constant, try MAXLEN too.
12739 For MAXLEN only allow optimizing into non-_ocs function
12740 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12741 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12742 return NULL_TREE;
12744 else
12745 maxlen = len;
12747 if (tree_int_cst_lt (size, maxlen))
12748 return NULL_TREE;
12751 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12752 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12753 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12754 if (!fn)
12755 return NULL_TREE;
12757 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12760 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12761 are the arguments to the call. */
12763 static tree
12764 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12765 tree src, tree size)
12767 tree fn;
12768 const char *p;
12770 if (!validate_arg (dest, POINTER_TYPE)
12771 || !validate_arg (src, POINTER_TYPE)
12772 || !validate_arg (size, INTEGER_TYPE))
12773 return NULL_TREE;
12775 p = c_getstr (src);
12776 /* If the SRC parameter is "", return DEST. */
12777 if (p && *p == '\0')
12778 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12780 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
12781 return NULL_TREE;
12783 /* If __builtin_strcat_chk is used, assume strcat is available. */
12784 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12785 if (!fn)
12786 return NULL_TREE;
12788 return build_call_expr_loc (loc, fn, 2, dest, src);
12791 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12792 LEN, and SIZE. */
12794 static tree
12795 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12796 tree dest, tree src, tree len, tree size)
12798 tree fn;
12799 const char *p;
12801 if (!validate_arg (dest, POINTER_TYPE)
12802 || !validate_arg (src, POINTER_TYPE)
12803 || !validate_arg (size, INTEGER_TYPE)
12804 || !validate_arg (size, INTEGER_TYPE))
12805 return NULL_TREE;
12807 p = c_getstr (src);
12808 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12809 if (p && *p == '\0')
12810 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12811 else if (integer_zerop (len))
12812 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12814 if (! tree_fits_uhwi_p (size))
12815 return NULL_TREE;
12817 if (! integer_all_onesp (size))
12819 tree src_len = c_strlen (src, 1);
12820 if (src_len
12821 && tree_fits_uhwi_p (src_len)
12822 && tree_fits_uhwi_p (len)
12823 && ! tree_int_cst_lt (len, src_len))
12825 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12826 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
12827 if (!fn)
12828 return NULL_TREE;
12830 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12832 return NULL_TREE;
12835 /* If __builtin_strncat_chk is used, assume strncat is available. */
12836 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
12837 if (!fn)
12838 return NULL_TREE;
12840 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12843 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12844 Return NULL_TREE if a normal call should be emitted rather than
12845 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
12846 or BUILT_IN_VSPRINTF_CHK. */
12848 static tree
12849 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
12850 enum built_in_function fcode)
12852 tree dest, size, len, fn, fmt, flag;
12853 const char *fmt_str;
12855 /* Verify the required arguments in the original call. */
12856 if (nargs < 4)
12857 return NULL_TREE;
12858 dest = args[0];
12859 if (!validate_arg (dest, POINTER_TYPE))
12860 return NULL_TREE;
12861 flag = args[1];
12862 if (!validate_arg (flag, INTEGER_TYPE))
12863 return NULL_TREE;
12864 size = args[2];
12865 if (!validate_arg (size, INTEGER_TYPE))
12866 return NULL_TREE;
12867 fmt = args[3];
12868 if (!validate_arg (fmt, POINTER_TYPE))
12869 return NULL_TREE;
12871 if (! tree_fits_uhwi_p (size))
12872 return NULL_TREE;
12874 len = NULL_TREE;
12876 if (!init_target_chars ())
12877 return NULL_TREE;
12879 /* Check whether the format is a literal string constant. */
12880 fmt_str = c_getstr (fmt);
12881 if (fmt_str != NULL)
12883 /* If the format doesn't contain % args or %%, we know the size. */
12884 if (strchr (fmt_str, target_percent) == 0)
12886 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12887 len = build_int_cstu (size_type_node, strlen (fmt_str));
12889 /* If the format is "%s" and first ... argument is a string literal,
12890 we know the size too. */
12891 else if (fcode == BUILT_IN_SPRINTF_CHK
12892 && strcmp (fmt_str, target_percent_s) == 0)
12894 tree arg;
12896 if (nargs == 5)
12898 arg = args[4];
12899 if (validate_arg (arg, POINTER_TYPE))
12901 len = c_strlen (arg, 1);
12902 if (! len || ! tree_fits_uhwi_p (len))
12903 len = NULL_TREE;
12909 if (! integer_all_onesp (size))
12911 if (! len || ! tree_int_cst_lt (len, size))
12912 return NULL_TREE;
12915 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12916 or if format doesn't contain % chars or is "%s". */
12917 if (! integer_zerop (flag))
12919 if (fmt_str == NULL)
12920 return NULL_TREE;
12921 if (strchr (fmt_str, target_percent) != NULL
12922 && strcmp (fmt_str, target_percent_s))
12923 return NULL_TREE;
12926 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12927 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
12928 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
12929 if (!fn)
12930 return NULL_TREE;
12932 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
12935 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12936 a normal call should be emitted rather than expanding the function
12937 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12939 static tree
12940 fold_builtin_sprintf_chk (location_t loc, tree exp,
12941 enum built_in_function fcode)
12943 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
12944 CALL_EXPR_ARGP (exp), fcode);
12947 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
12948 NULL_TREE if a normal call should be emitted rather than expanding
12949 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12950 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12951 passed as second argument. */
12953 static tree
12954 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
12955 tree maxlen, enum built_in_function fcode)
12957 tree dest, size, len, fn, fmt, flag;
12958 const char *fmt_str;
12960 /* Verify the required arguments in the original call. */
12961 if (nargs < 5)
12962 return NULL_TREE;
12963 dest = args[0];
12964 if (!validate_arg (dest, POINTER_TYPE))
12965 return NULL_TREE;
12966 len = args[1];
12967 if (!validate_arg (len, INTEGER_TYPE))
12968 return NULL_TREE;
12969 flag = args[2];
12970 if (!validate_arg (flag, INTEGER_TYPE))
12971 return NULL_TREE;
12972 size = args[3];
12973 if (!validate_arg (size, INTEGER_TYPE))
12974 return NULL_TREE;
12975 fmt = args[4];
12976 if (!validate_arg (fmt, POINTER_TYPE))
12977 return NULL_TREE;
12979 if (! tree_fits_uhwi_p (size))
12980 return NULL_TREE;
12982 if (! integer_all_onesp (size))
12984 if (! tree_fits_uhwi_p (len))
12986 /* If LEN is not constant, try MAXLEN too.
12987 For MAXLEN only allow optimizing into non-_ocs function
12988 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12989 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12990 return NULL_TREE;
12992 else
12993 maxlen = len;
12995 if (tree_int_cst_lt (size, maxlen))
12996 return NULL_TREE;
12999 if (!init_target_chars ())
13000 return NULL_TREE;
13002 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13003 or if format doesn't contain % chars or is "%s". */
13004 if (! integer_zerop (flag))
13006 fmt_str = c_getstr (fmt);
13007 if (fmt_str == NULL)
13008 return NULL_TREE;
13009 if (strchr (fmt_str, target_percent) != NULL
13010 && strcmp (fmt_str, target_percent_s))
13011 return NULL_TREE;
13014 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13015 available. */
13016 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13017 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13018 if (!fn)
13019 return NULL_TREE;
13021 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13024 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13025 a normal call should be emitted rather than expanding the function
13026 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13027 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13028 passed as second argument. */
13030 static tree
13031 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13032 enum built_in_function fcode)
13034 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13035 CALL_EXPR_ARGP (exp), maxlen, fcode);
13038 /* Builtins with folding operations that operate on "..." arguments
13039 need special handling; we need to store the arguments in a convenient
13040 data structure before attempting any folding. Fortunately there are
13041 only a few builtins that fall into this category. FNDECL is the
13042 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13043 result of the function call is ignored. */
13045 static tree
13046 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
13047 bool ignore ATTRIBUTE_UNUSED)
13049 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13050 tree ret = NULL_TREE;
13052 switch (fcode)
13054 case BUILT_IN_SPRINTF_CHK:
13055 case BUILT_IN_VSPRINTF_CHK:
13056 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
13057 break;
13059 case BUILT_IN_SNPRINTF_CHK:
13060 case BUILT_IN_VSNPRINTF_CHK:
13061 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
13062 break;
13064 case BUILT_IN_FPCLASSIFY:
13065 ret = fold_builtin_fpclassify (loc, exp);
13066 break;
13068 default:
13069 break;
13071 if (ret)
13073 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13074 SET_EXPR_LOCATION (ret, loc);
13075 TREE_NO_WARNING (ret) = 1;
13076 return ret;
13078 return NULL_TREE;
13081 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13082 FMT and ARG are the arguments to the call; we don't fold cases with
13083 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13085 Return NULL_TREE if no simplification was possible, otherwise return the
13086 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13087 code of the function to be simplified. */
13089 static tree
13090 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13091 tree arg, bool ignore,
13092 enum built_in_function fcode)
13094 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13095 const char *fmt_str = NULL;
13097 /* If the return value is used, don't do the transformation. */
13098 if (! ignore)
13099 return NULL_TREE;
13101 /* Verify the required arguments in the original call. */
13102 if (!validate_arg (fmt, POINTER_TYPE))
13103 return NULL_TREE;
13105 /* Check whether the format is a literal string constant. */
13106 fmt_str = c_getstr (fmt);
13107 if (fmt_str == NULL)
13108 return NULL_TREE;
13110 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13112 /* If we're using an unlocked function, assume the other
13113 unlocked functions exist explicitly. */
13114 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13115 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13117 else
13119 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13120 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13123 if (!init_target_chars ())
13124 return NULL_TREE;
13126 if (strcmp (fmt_str, target_percent_s) == 0
13127 || strchr (fmt_str, target_percent) == NULL)
13129 const char *str;
13131 if (strcmp (fmt_str, target_percent_s) == 0)
13133 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13134 return NULL_TREE;
13136 if (!arg || !validate_arg (arg, POINTER_TYPE))
13137 return NULL_TREE;
13139 str = c_getstr (arg);
13140 if (str == NULL)
13141 return NULL_TREE;
13143 else
13145 /* The format specifier doesn't contain any '%' characters. */
13146 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13147 && arg)
13148 return NULL_TREE;
13149 str = fmt_str;
13152 /* If the string was "", printf does nothing. */
13153 if (str[0] == '\0')
13154 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13156 /* If the string has length of 1, call putchar. */
13157 if (str[1] == '\0')
13159 /* Given printf("c"), (where c is any one character,)
13160 convert "c"[0] to an int and pass that to the replacement
13161 function. */
13162 newarg = build_int_cst (integer_type_node, str[0]);
13163 if (fn_putchar)
13164 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13166 else
13168 /* If the string was "string\n", call puts("string"). */
13169 size_t len = strlen (str);
13170 if ((unsigned char)str[len - 1] == target_newline
13171 && (size_t) (int) len == len
13172 && (int) len > 0)
13174 char *newstr;
13175 tree offset_node, string_cst;
13177 /* Create a NUL-terminated string that's one char shorter
13178 than the original, stripping off the trailing '\n'. */
13179 newarg = build_string_literal (len, str);
13180 string_cst = string_constant (newarg, &offset_node);
13181 gcc_checking_assert (string_cst
13182 && (TREE_STRING_LENGTH (string_cst)
13183 == (int) len)
13184 && integer_zerop (offset_node)
13185 && (unsigned char)
13186 TREE_STRING_POINTER (string_cst)[len - 1]
13187 == target_newline);
13188 /* build_string_literal creates a new STRING_CST,
13189 modify it in place to avoid double copying. */
13190 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13191 newstr[len - 1] = '\0';
13192 if (fn_puts)
13193 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13195 else
13196 /* We'd like to arrange to call fputs(string,stdout) here,
13197 but we need stdout and don't have a way to get it yet. */
13198 return NULL_TREE;
13202 /* The other optimizations can be done only on the non-va_list variants. */
13203 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13204 return NULL_TREE;
13206 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13207 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13209 if (!arg || !validate_arg (arg, POINTER_TYPE))
13210 return NULL_TREE;
13211 if (fn_puts)
13212 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13215 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13216 else if (strcmp (fmt_str, target_percent_c) == 0)
13218 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13219 return NULL_TREE;
13220 if (fn_putchar)
13221 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13224 if (!call)
13225 return NULL_TREE;
13227 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13230 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13231 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13232 more than 3 arguments, and ARG may be null in the 2-argument case.
13234 Return NULL_TREE if no simplification was possible, otherwise return the
13235 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13236 code of the function to be simplified. */
13238 static tree
13239 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13240 tree fmt, tree arg, bool ignore,
13241 enum built_in_function fcode)
13243 tree fn_fputc, fn_fputs, call = NULL_TREE;
13244 const char *fmt_str = NULL;
13246 /* If the return value is used, don't do the transformation. */
13247 if (! ignore)
13248 return NULL_TREE;
13250 /* Verify the required arguments in the original call. */
13251 if (!validate_arg (fp, POINTER_TYPE))
13252 return NULL_TREE;
13253 if (!validate_arg (fmt, POINTER_TYPE))
13254 return NULL_TREE;
13256 /* Check whether the format is a literal string constant. */
13257 fmt_str = c_getstr (fmt);
13258 if (fmt_str == NULL)
13259 return NULL_TREE;
13261 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13263 /* If we're using an unlocked function, assume the other
13264 unlocked functions exist explicitly. */
13265 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13266 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13268 else
13270 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13271 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13274 if (!init_target_chars ())
13275 return NULL_TREE;
13277 /* If the format doesn't contain % args or %%, use strcpy. */
13278 if (strchr (fmt_str, target_percent) == NULL)
13280 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13281 && arg)
13282 return NULL_TREE;
13284 /* If the format specifier was "", fprintf does nothing. */
13285 if (fmt_str[0] == '\0')
13287 /* If FP has side-effects, just wait until gimplification is
13288 done. */
13289 if (TREE_SIDE_EFFECTS (fp))
13290 return NULL_TREE;
13292 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13295 /* When "string" doesn't contain %, replace all cases of
13296 fprintf (fp, string) with fputs (string, fp). The fputs
13297 builtin will take care of special cases like length == 1. */
13298 if (fn_fputs)
13299 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13302 /* The other optimizations can be done only on the non-va_list variants. */
13303 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13304 return NULL_TREE;
13306 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13307 else if (strcmp (fmt_str, target_percent_s) == 0)
13309 if (!arg || !validate_arg (arg, POINTER_TYPE))
13310 return NULL_TREE;
13311 if (fn_fputs)
13312 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13315 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13316 else if (strcmp (fmt_str, target_percent_c) == 0)
13318 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13319 return NULL_TREE;
13320 if (fn_fputc)
13321 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13324 if (!call)
13325 return NULL_TREE;
13326 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13329 /* Initialize format string characters in the target charset. */
13331 static bool
13332 init_target_chars (void)
13334 static bool init;
13335 if (!init)
13337 target_newline = lang_hooks.to_target_charset ('\n');
13338 target_percent = lang_hooks.to_target_charset ('%');
13339 target_c = lang_hooks.to_target_charset ('c');
13340 target_s = lang_hooks.to_target_charset ('s');
13341 if (target_newline == 0 || target_percent == 0 || target_c == 0
13342 || target_s == 0)
13343 return false;
13345 target_percent_c[0] = target_percent;
13346 target_percent_c[1] = target_c;
13347 target_percent_c[2] = '\0';
13349 target_percent_s[0] = target_percent;
13350 target_percent_s[1] = target_s;
13351 target_percent_s[2] = '\0';
13353 target_percent_s_newline[0] = target_percent;
13354 target_percent_s_newline[1] = target_s;
13355 target_percent_s_newline[2] = target_newline;
13356 target_percent_s_newline[3] = '\0';
13358 init = true;
13360 return true;
13363 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13364 and no overflow/underflow occurred. INEXACT is true if M was not
13365 exactly calculated. TYPE is the tree type for the result. This
13366 function assumes that you cleared the MPFR flags and then
13367 calculated M to see if anything subsequently set a flag prior to
13368 entering this function. Return NULL_TREE if any checks fail. */
13370 static tree
13371 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13373 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13374 overflow/underflow occurred. If -frounding-math, proceed iff the
13375 result of calling FUNC was exact. */
13376 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13377 && (!flag_rounding_math || !inexact))
13379 REAL_VALUE_TYPE rr;
13381 real_from_mpfr (&rr, m, type, GMP_RNDN);
13382 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13383 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13384 but the mpft_t is not, then we underflowed in the
13385 conversion. */
13386 if (real_isfinite (&rr)
13387 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13389 REAL_VALUE_TYPE rmode;
13391 real_convert (&rmode, TYPE_MODE (type), &rr);
13392 /* Proceed iff the specified mode can hold the value. */
13393 if (real_identical (&rmode, &rr))
13394 return build_real (type, rmode);
13397 return NULL_TREE;
13400 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13401 number and no overflow/underflow occurred. INEXACT is true if M
13402 was not exactly calculated. TYPE is the tree type for the result.
13403 This function assumes that you cleared the MPFR flags and then
13404 calculated M to see if anything subsequently set a flag prior to
13405 entering this function. Return NULL_TREE if any checks fail, if
13406 FORCE_CONVERT is true, then bypass the checks. */
13408 static tree
13409 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13411 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13412 overflow/underflow occurred. If -frounding-math, proceed iff the
13413 result of calling FUNC was exact. */
13414 if (force_convert
13415 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13416 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13417 && (!flag_rounding_math || !inexact)))
13419 REAL_VALUE_TYPE re, im;
13421 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13422 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13423 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13424 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13425 but the mpft_t is not, then we underflowed in the
13426 conversion. */
13427 if (force_convert
13428 || (real_isfinite (&re) && real_isfinite (&im)
13429 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13430 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13432 REAL_VALUE_TYPE re_mode, im_mode;
13434 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13435 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13436 /* Proceed iff the specified mode can hold the value. */
13437 if (force_convert
13438 || (real_identical (&re_mode, &re)
13439 && real_identical (&im_mode, &im)))
13440 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13441 build_real (TREE_TYPE (type), im_mode));
13444 return NULL_TREE;
13447 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13448 FUNC on it and return the resulting value as a tree with type TYPE.
13449 If MIN and/or MAX are not NULL, then the supplied ARG must be
13450 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13451 acceptable values, otherwise they are not. The mpfr precision is
13452 set to the precision of TYPE. We assume that function FUNC returns
13453 zero if the result could be calculated exactly within the requested
13454 precision. */
13456 static tree
13457 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13458 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13459 bool inclusive)
13461 tree result = NULL_TREE;
13463 STRIP_NOPS (arg);
13465 /* To proceed, MPFR must exactly represent the target floating point
13466 format, which only happens when the target base equals two. */
13467 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13468 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13470 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13472 if (real_isfinite (ra)
13473 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13474 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13476 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13477 const int prec = fmt->p;
13478 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13479 int inexact;
13480 mpfr_t m;
13482 mpfr_init2 (m, prec);
13483 mpfr_from_real (m, ra, GMP_RNDN);
13484 mpfr_clear_flags ();
13485 inexact = func (m, m, rnd);
13486 result = do_mpfr_ckconv (m, type, inexact);
13487 mpfr_clear (m);
13491 return result;
13494 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13495 FUNC on it and return the resulting value as a tree with type TYPE.
13496 The mpfr precision is set to the precision of TYPE. We assume that
13497 function FUNC returns zero if the result could be calculated
13498 exactly within the requested precision. */
13500 static tree
13501 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13502 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13504 tree result = NULL_TREE;
13506 STRIP_NOPS (arg1);
13507 STRIP_NOPS (arg2);
13509 /* To proceed, MPFR must exactly represent the target floating point
13510 format, which only happens when the target base equals two. */
13511 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13512 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13513 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13515 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13516 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13518 if (real_isfinite (ra1) && real_isfinite (ra2))
13520 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13521 const int prec = fmt->p;
13522 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13523 int inexact;
13524 mpfr_t m1, m2;
13526 mpfr_inits2 (prec, m1, m2, NULL);
13527 mpfr_from_real (m1, ra1, GMP_RNDN);
13528 mpfr_from_real (m2, ra2, GMP_RNDN);
13529 mpfr_clear_flags ();
13530 inexact = func (m1, m1, m2, rnd);
13531 result = do_mpfr_ckconv (m1, type, inexact);
13532 mpfr_clears (m1, m2, NULL);
13536 return result;
13539 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13540 FUNC on it and return the resulting value as a tree with type TYPE.
13541 The mpfr precision is set to the precision of TYPE. We assume that
13542 function FUNC returns zero if the result could be calculated
13543 exactly within the requested precision. */
13545 static tree
13546 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13547 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13549 tree result = NULL_TREE;
13551 STRIP_NOPS (arg1);
13552 STRIP_NOPS (arg2);
13553 STRIP_NOPS (arg3);
13555 /* To proceed, MPFR must exactly represent the target floating point
13556 format, which only happens when the target base equals two. */
13557 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13558 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13559 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13560 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13562 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13563 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13564 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13566 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13568 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13569 const int prec = fmt->p;
13570 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13571 int inexact;
13572 mpfr_t m1, m2, m3;
13574 mpfr_inits2 (prec, m1, m2, m3, NULL);
13575 mpfr_from_real (m1, ra1, GMP_RNDN);
13576 mpfr_from_real (m2, ra2, GMP_RNDN);
13577 mpfr_from_real (m3, ra3, GMP_RNDN);
13578 mpfr_clear_flags ();
13579 inexact = func (m1, m1, m2, m3, rnd);
13580 result = do_mpfr_ckconv (m1, type, inexact);
13581 mpfr_clears (m1, m2, m3, NULL);
13585 return result;
13588 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13589 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13590 If ARG_SINP and ARG_COSP are NULL then the result is returned
13591 as a complex value.
13592 The type is taken from the type of ARG and is used for setting the
13593 precision of the calculation and results. */
13595 static tree
13596 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13598 tree const type = TREE_TYPE (arg);
13599 tree result = NULL_TREE;
13601 STRIP_NOPS (arg);
13603 /* To proceed, MPFR must exactly represent the target floating point
13604 format, which only happens when the target base equals two. */
13605 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13606 && TREE_CODE (arg) == REAL_CST
13607 && !TREE_OVERFLOW (arg))
13609 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13611 if (real_isfinite (ra))
13613 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13614 const int prec = fmt->p;
13615 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13616 tree result_s, result_c;
13617 int inexact;
13618 mpfr_t m, ms, mc;
13620 mpfr_inits2 (prec, m, ms, mc, NULL);
13621 mpfr_from_real (m, ra, GMP_RNDN);
13622 mpfr_clear_flags ();
13623 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13624 result_s = do_mpfr_ckconv (ms, type, inexact);
13625 result_c = do_mpfr_ckconv (mc, type, inexact);
13626 mpfr_clears (m, ms, mc, NULL);
13627 if (result_s && result_c)
13629 /* If we are to return in a complex value do so. */
13630 if (!arg_sinp && !arg_cosp)
13631 return build_complex (build_complex_type (type),
13632 result_c, result_s);
13634 /* Dereference the sin/cos pointer arguments. */
13635 arg_sinp = build_fold_indirect_ref (arg_sinp);
13636 arg_cosp = build_fold_indirect_ref (arg_cosp);
13637 /* Proceed if valid pointer type were passed in. */
13638 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13639 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13641 /* Set the values. */
13642 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13643 result_s);
13644 TREE_SIDE_EFFECTS (result_s) = 1;
13645 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13646 result_c);
13647 TREE_SIDE_EFFECTS (result_c) = 1;
13648 /* Combine the assignments into a compound expr. */
13649 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13650 result_s, result_c));
13655 return result;
13658 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13659 two-argument mpfr order N Bessel function FUNC on them and return
13660 the resulting value as a tree with type TYPE. The mpfr precision
13661 is set to the precision of TYPE. We assume that function FUNC
13662 returns zero if the result could be calculated exactly within the
13663 requested precision. */
13664 static tree
13665 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13666 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13667 const REAL_VALUE_TYPE *min, bool inclusive)
13669 tree result = NULL_TREE;
13671 STRIP_NOPS (arg1);
13672 STRIP_NOPS (arg2);
13674 /* To proceed, MPFR must exactly represent the target floating point
13675 format, which only happens when the target base equals two. */
13676 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13677 && tree_fits_shwi_p (arg1)
13678 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13680 const HOST_WIDE_INT n = tree_to_shwi (arg1);
13681 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13683 if (n == (long)n
13684 && real_isfinite (ra)
13685 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13687 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13688 const int prec = fmt->p;
13689 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13690 int inexact;
13691 mpfr_t m;
13693 mpfr_init2 (m, prec);
13694 mpfr_from_real (m, ra, GMP_RNDN);
13695 mpfr_clear_flags ();
13696 inexact = func (m, n, m, rnd);
13697 result = do_mpfr_ckconv (m, type, inexact);
13698 mpfr_clear (m);
13702 return result;
13705 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13706 the pointer *(ARG_QUO) and return the result. The type is taken
13707 from the type of ARG0 and is used for setting the precision of the
13708 calculation and results. */
13710 static tree
13711 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13713 tree const type = TREE_TYPE (arg0);
13714 tree result = NULL_TREE;
13716 STRIP_NOPS (arg0);
13717 STRIP_NOPS (arg1);
13719 /* To proceed, MPFR must exactly represent the target floating point
13720 format, which only happens when the target base equals two. */
13721 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13722 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13723 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13725 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13726 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13728 if (real_isfinite (ra0) && real_isfinite (ra1))
13730 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13731 const int prec = fmt->p;
13732 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13733 tree result_rem;
13734 long integer_quo;
13735 mpfr_t m0, m1;
13737 mpfr_inits2 (prec, m0, m1, NULL);
13738 mpfr_from_real (m0, ra0, GMP_RNDN);
13739 mpfr_from_real (m1, ra1, GMP_RNDN);
13740 mpfr_clear_flags ();
13741 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13742 /* Remquo is independent of the rounding mode, so pass
13743 inexact=0 to do_mpfr_ckconv(). */
13744 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13745 mpfr_clears (m0, m1, NULL);
13746 if (result_rem)
13748 /* MPFR calculates quo in the host's long so it may
13749 return more bits in quo than the target int can hold
13750 if sizeof(host long) > sizeof(target int). This can
13751 happen even for native compilers in LP64 mode. In
13752 these cases, modulo the quo value with the largest
13753 number that the target int can hold while leaving one
13754 bit for the sign. */
13755 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13756 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13758 /* Dereference the quo pointer argument. */
13759 arg_quo = build_fold_indirect_ref (arg_quo);
13760 /* Proceed iff a valid pointer type was passed in. */
13761 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13763 /* Set the value. */
13764 tree result_quo
13765 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13766 build_int_cst (TREE_TYPE (arg_quo),
13767 integer_quo));
13768 TREE_SIDE_EFFECTS (result_quo) = 1;
13769 /* Combine the quo assignment with the rem. */
13770 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13771 result_quo, result_rem));
13776 return result;
13779 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13780 resulting value as a tree with type TYPE. The mpfr precision is
13781 set to the precision of TYPE. We assume that this mpfr function
13782 returns zero if the result could be calculated exactly within the
13783 requested precision. In addition, the integer pointer represented
13784 by ARG_SG will be dereferenced and set to the appropriate signgam
13785 (-1,1) value. */
13787 static tree
13788 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13790 tree result = NULL_TREE;
13792 STRIP_NOPS (arg);
13794 /* To proceed, MPFR must exactly represent the target floating point
13795 format, which only happens when the target base equals two. Also
13796 verify ARG is a constant and that ARG_SG is an int pointer. */
13797 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13798 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13799 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13800 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13802 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13804 /* In addition to NaN and Inf, the argument cannot be zero or a
13805 negative integer. */
13806 if (real_isfinite (ra)
13807 && ra->cl != rvc_zero
13808 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
13810 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13811 const int prec = fmt->p;
13812 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13813 int inexact, sg;
13814 mpfr_t m;
13815 tree result_lg;
13817 mpfr_init2 (m, prec);
13818 mpfr_from_real (m, ra, GMP_RNDN);
13819 mpfr_clear_flags ();
13820 inexact = mpfr_lgamma (m, &sg, m, rnd);
13821 result_lg = do_mpfr_ckconv (m, type, inexact);
13822 mpfr_clear (m);
13823 if (result_lg)
13825 tree result_sg;
13827 /* Dereference the arg_sg pointer argument. */
13828 arg_sg = build_fold_indirect_ref (arg_sg);
13829 /* Assign the signgam value into *arg_sg. */
13830 result_sg = fold_build2 (MODIFY_EXPR,
13831 TREE_TYPE (arg_sg), arg_sg,
13832 build_int_cst (TREE_TYPE (arg_sg), sg));
13833 TREE_SIDE_EFFECTS (result_sg) = 1;
13834 /* Combine the signgam assignment with the lgamma result. */
13835 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13836 result_sg, result_lg));
13841 return result;
13844 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13845 function FUNC on it and return the resulting value as a tree with
13846 type TYPE. The mpfr precision is set to the precision of TYPE. We
13847 assume that function FUNC returns zero if the result could be
13848 calculated exactly within the requested precision. */
13850 static tree
13851 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13853 tree result = NULL_TREE;
13855 STRIP_NOPS (arg);
13857 /* To proceed, MPFR must exactly represent the target floating point
13858 format, which only happens when the target base equals two. */
13859 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13860 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13861 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13863 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13864 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13866 if (real_isfinite (re) && real_isfinite (im))
13868 const struct real_format *const fmt =
13869 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13870 const int prec = fmt->p;
13871 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13872 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13873 int inexact;
13874 mpc_t m;
13876 mpc_init2 (m, prec);
13877 mpfr_from_real (mpc_realref (m), re, rnd);
13878 mpfr_from_real (mpc_imagref (m), im, rnd);
13879 mpfr_clear_flags ();
13880 inexact = func (m, m, crnd);
13881 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13882 mpc_clear (m);
13886 return result;
13889 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13890 mpc function FUNC on it and return the resulting value as a tree
13891 with type TYPE. The mpfr precision is set to the precision of
13892 TYPE. We assume that function FUNC returns zero if the result
13893 could be calculated exactly within the requested precision. If
13894 DO_NONFINITE is true, then fold expressions containing Inf or NaN
13895 in the arguments and/or results. */
13897 tree
13898 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13899 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13901 tree result = NULL_TREE;
13903 STRIP_NOPS (arg0);
13904 STRIP_NOPS (arg1);
13906 /* To proceed, MPFR must exactly represent the target floating point
13907 format, which only happens when the target base equals two. */
13908 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13909 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13910 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13911 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13912 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13914 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13915 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13916 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13917 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13919 if (do_nonfinite
13920 || (real_isfinite (re0) && real_isfinite (im0)
13921 && real_isfinite (re1) && real_isfinite (im1)))
13923 const struct real_format *const fmt =
13924 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13925 const int prec = fmt->p;
13926 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13927 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13928 int inexact;
13929 mpc_t m0, m1;
13931 mpc_init2 (m0, prec);
13932 mpc_init2 (m1, prec);
13933 mpfr_from_real (mpc_realref (m0), re0, rnd);
13934 mpfr_from_real (mpc_imagref (m0), im0, rnd);
13935 mpfr_from_real (mpc_realref (m1), re1, rnd);
13936 mpfr_from_real (mpc_imagref (m1), im1, rnd);
13937 mpfr_clear_flags ();
13938 inexact = func (m0, m0, m1, crnd);
13939 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
13940 mpc_clear (m0);
13941 mpc_clear (m1);
13945 return result;
13948 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13949 a normal call should be emitted rather than expanding the function
13950 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13952 static tree
13953 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13955 int nargs = gimple_call_num_args (stmt);
13957 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
13958 (nargs > 0
13959 ? gimple_call_arg_ptr (stmt, 0)
13960 : &error_mark_node), fcode);
13963 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13964 a normal call should be emitted rather than expanding the function
13965 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13966 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13967 passed as second argument. */
13969 tree
13970 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13971 enum built_in_function fcode)
13973 int nargs = gimple_call_num_args (stmt);
13975 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
13976 (nargs > 0
13977 ? gimple_call_arg_ptr (stmt, 0)
13978 : &error_mark_node), maxlen, fcode);
13981 /* Builtins with folding operations that operate on "..." arguments
13982 need special handling; we need to store the arguments in a convenient
13983 data structure before attempting any folding. Fortunately there are
13984 only a few builtins that fall into this category. FNDECL is the
13985 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13986 result of the function call is ignored. */
13988 static tree
13989 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
13990 bool ignore ATTRIBUTE_UNUSED)
13992 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13993 tree ret = NULL_TREE;
13995 switch (fcode)
13997 case BUILT_IN_SPRINTF_CHK:
13998 case BUILT_IN_VSPRINTF_CHK:
13999 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14000 break;
14002 case BUILT_IN_SNPRINTF_CHK:
14003 case BUILT_IN_VSNPRINTF_CHK:
14004 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14006 default:
14007 break;
14009 if (ret)
14011 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14012 TREE_NO_WARNING (ret) = 1;
14013 return ret;
14015 return NULL_TREE;
14018 /* A wrapper function for builtin folding that prevents warnings for
14019 "statement without effect" and the like, caused by removing the
14020 call node earlier than the warning is generated. */
14022 tree
14023 fold_call_stmt (gimple stmt, bool ignore)
14025 tree ret = NULL_TREE;
14026 tree fndecl = gimple_call_fndecl (stmt);
14027 location_t loc = gimple_location (stmt);
14028 if (fndecl
14029 && TREE_CODE (fndecl) == FUNCTION_DECL
14030 && DECL_BUILT_IN (fndecl)
14031 && !gimple_call_va_arg_pack_p (stmt))
14033 int nargs = gimple_call_num_args (stmt);
14034 tree *args = (nargs > 0
14035 ? gimple_call_arg_ptr (stmt, 0)
14036 : &error_mark_node);
14038 if (avoid_folding_inline_builtin (fndecl))
14039 return NULL_TREE;
14040 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14042 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14044 else
14046 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14047 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14048 if (!ret)
14049 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14050 if (ret)
14052 /* Propagate location information from original call to
14053 expansion of builtin. Otherwise things like
14054 maybe_emit_chk_warning, that operate on the expansion
14055 of a builtin, will use the wrong location information. */
14056 if (gimple_has_location (stmt))
14058 tree realret = ret;
14059 if (TREE_CODE (ret) == NOP_EXPR)
14060 realret = TREE_OPERAND (ret, 0);
14061 if (CAN_HAVE_LOCATION_P (realret)
14062 && !EXPR_HAS_LOCATION (realret))
14063 SET_EXPR_LOCATION (realret, loc);
14064 return realret;
14066 return ret;
14070 return NULL_TREE;
14073 /* Look up the function in builtin_decl that corresponds to DECL
14074 and set ASMSPEC as its user assembler name. DECL must be a
14075 function decl that declares a builtin. */
14077 void
14078 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14080 tree builtin;
14081 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14082 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14083 && asmspec != 0);
14085 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14086 set_user_assembler_name (builtin, asmspec);
14087 switch (DECL_FUNCTION_CODE (decl))
14089 case BUILT_IN_MEMCPY:
14090 init_block_move_fn (asmspec);
14091 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14092 break;
14093 case BUILT_IN_MEMSET:
14094 init_block_clear_fn (asmspec);
14095 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14096 break;
14097 case BUILT_IN_MEMMOVE:
14098 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14099 break;
14100 case BUILT_IN_MEMCMP:
14101 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14102 break;
14103 case BUILT_IN_ABORT:
14104 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14105 break;
14106 case BUILT_IN_FFS:
14107 if (INT_TYPE_SIZE < BITS_PER_WORD)
14109 set_user_assembler_libfunc ("ffs", asmspec);
14110 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14111 MODE_INT, 0), "ffs");
14113 break;
14114 default:
14115 break;
14119 /* Return true if DECL is a builtin that expands to a constant or similarly
14120 simple code. */
14121 bool
14122 is_simple_builtin (tree decl)
14124 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14125 switch (DECL_FUNCTION_CODE (decl))
14127 /* Builtins that expand to constants. */
14128 case BUILT_IN_CONSTANT_P:
14129 case BUILT_IN_EXPECT:
14130 case BUILT_IN_OBJECT_SIZE:
14131 case BUILT_IN_UNREACHABLE:
14132 /* Simple register moves or loads from stack. */
14133 case BUILT_IN_ASSUME_ALIGNED:
14134 case BUILT_IN_RETURN_ADDRESS:
14135 case BUILT_IN_EXTRACT_RETURN_ADDR:
14136 case BUILT_IN_FROB_RETURN_ADDR:
14137 case BUILT_IN_RETURN:
14138 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14139 case BUILT_IN_FRAME_ADDRESS:
14140 case BUILT_IN_VA_END:
14141 case BUILT_IN_STACK_SAVE:
14142 case BUILT_IN_STACK_RESTORE:
14143 /* Exception state returns or moves registers around. */
14144 case BUILT_IN_EH_FILTER:
14145 case BUILT_IN_EH_POINTER:
14146 case BUILT_IN_EH_COPY_VALUES:
14147 return true;
14149 default:
14150 return false;
14153 return false;
14156 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14157 most probably expanded inline into reasonably simple code. This is a
14158 superset of is_simple_builtin. */
14159 bool
14160 is_inexpensive_builtin (tree decl)
14162 if (!decl)
14163 return false;
14164 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14165 return true;
14166 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14167 switch (DECL_FUNCTION_CODE (decl))
14169 case BUILT_IN_ABS:
14170 case BUILT_IN_ALLOCA:
14171 case BUILT_IN_ALLOCA_WITH_ALIGN:
14172 case BUILT_IN_BSWAP16:
14173 case BUILT_IN_BSWAP32:
14174 case BUILT_IN_BSWAP64:
14175 case BUILT_IN_CLZ:
14176 case BUILT_IN_CLZIMAX:
14177 case BUILT_IN_CLZL:
14178 case BUILT_IN_CLZLL:
14179 case BUILT_IN_CTZ:
14180 case BUILT_IN_CTZIMAX:
14181 case BUILT_IN_CTZL:
14182 case BUILT_IN_CTZLL:
14183 case BUILT_IN_FFS:
14184 case BUILT_IN_FFSIMAX:
14185 case BUILT_IN_FFSL:
14186 case BUILT_IN_FFSLL:
14187 case BUILT_IN_IMAXABS:
14188 case BUILT_IN_FINITE:
14189 case BUILT_IN_FINITEF:
14190 case BUILT_IN_FINITEL:
14191 case BUILT_IN_FINITED32:
14192 case BUILT_IN_FINITED64:
14193 case BUILT_IN_FINITED128:
14194 case BUILT_IN_FPCLASSIFY:
14195 case BUILT_IN_ISFINITE:
14196 case BUILT_IN_ISINF_SIGN:
14197 case BUILT_IN_ISINF:
14198 case BUILT_IN_ISINFF:
14199 case BUILT_IN_ISINFL:
14200 case BUILT_IN_ISINFD32:
14201 case BUILT_IN_ISINFD64:
14202 case BUILT_IN_ISINFD128:
14203 case BUILT_IN_ISNAN:
14204 case BUILT_IN_ISNANF:
14205 case BUILT_IN_ISNANL:
14206 case BUILT_IN_ISNAND32:
14207 case BUILT_IN_ISNAND64:
14208 case BUILT_IN_ISNAND128:
14209 case BUILT_IN_ISNORMAL:
14210 case BUILT_IN_ISGREATER:
14211 case BUILT_IN_ISGREATEREQUAL:
14212 case BUILT_IN_ISLESS:
14213 case BUILT_IN_ISLESSEQUAL:
14214 case BUILT_IN_ISLESSGREATER:
14215 case BUILT_IN_ISUNORDERED:
14216 case BUILT_IN_VA_ARG_PACK:
14217 case BUILT_IN_VA_ARG_PACK_LEN:
14218 case BUILT_IN_VA_COPY:
14219 case BUILT_IN_TRAP:
14220 case BUILT_IN_SAVEREGS:
14221 case BUILT_IN_POPCOUNTL:
14222 case BUILT_IN_POPCOUNTLL:
14223 case BUILT_IN_POPCOUNTIMAX:
14224 case BUILT_IN_POPCOUNT:
14225 case BUILT_IN_PARITYL:
14226 case BUILT_IN_PARITYLL:
14227 case BUILT_IN_PARITYIMAX:
14228 case BUILT_IN_PARITY:
14229 case BUILT_IN_LABS:
14230 case BUILT_IN_LLABS:
14231 case BUILT_IN_PREFETCH:
14232 return true;
14234 default:
14235 return is_simple_builtin (decl);
14238 return false;