gcc/testsuite/
[official-gcc.git] / gcc / builtins.c
blob140d6ba1a5b674baeeb531a03c6045d5ef8b765c
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 /* By default we assume that c99 functions are present at the runtime,
263 but sincos is not. */
264 bool
265 default_libc_has_function (enum function_class fn_class)
267 if (fn_class == function_c94
268 || fn_class == function_c99_misc
269 || fn_class == function_c99_math_complex)
270 return true;
272 return false;
275 bool
276 gnu_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED)
278 return true;
281 bool
282 no_c99_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED)
284 return false;
287 /* Return true if NODE should be considered for inline expansion regardless
288 of the optimization level. This means whenever a function is invoked with
289 its "internal" name, which normally contains the prefix "__builtin". */
291 static bool
292 called_as_built_in (tree node)
294 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
295 we want the name used to call the function, not the name it
296 will have. */
297 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
298 return is_builtin_name (name);
301 /* Compute values M and N such that M divides (address of EXP - N) and such
302 that N < M. If these numbers can be determined, store M in alignp and N in
303 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
304 *alignp and any bit-offset to *bitposp.
306 Note that the address (and thus the alignment) computed here is based
307 on the address to which a symbol resolves, whereas DECL_ALIGN is based
308 on the address at which an object is actually located. These two
309 addresses are not always the same. For example, on ARM targets,
310 the address &foo of a Thumb function foo() has the lowest bit set,
311 whereas foo() itself starts on an even address.
313 If ADDR_P is true we are taking the address of the memory reference EXP
314 and thus cannot rely on the access taking place. */
316 static bool
317 get_object_alignment_2 (tree exp, unsigned int *alignp,
318 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
320 HOST_WIDE_INT bitsize, bitpos;
321 tree offset;
322 enum machine_mode mode;
323 int unsignedp, volatilep;
324 unsigned int align = BITS_PER_UNIT;
325 bool known_alignment = false;
327 /* Get the innermost object and the constant (bitpos) and possibly
328 variable (offset) offset of the access. */
329 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
330 &mode, &unsignedp, &volatilep, true);
332 /* Extract alignment information from the innermost object and
333 possibly adjust bitpos and offset. */
334 if (TREE_CODE (exp) == FUNCTION_DECL)
336 /* Function addresses can encode extra information besides their
337 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
338 allows the low bit to be used as a virtual bit, we know
339 that the address itself must be at least 2-byte aligned. */
340 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
341 align = 2 * BITS_PER_UNIT;
343 else if (TREE_CODE (exp) == LABEL_DECL)
345 else if (TREE_CODE (exp) == CONST_DECL)
347 /* The alignment of a CONST_DECL is determined by its initializer. */
348 exp = DECL_INITIAL (exp);
349 align = TYPE_ALIGN (TREE_TYPE (exp));
350 #ifdef CONSTANT_ALIGNMENT
351 if (CONSTANT_CLASS_P (exp))
352 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
353 #endif
354 known_alignment = true;
356 else if (DECL_P (exp))
358 align = DECL_ALIGN (exp);
359 known_alignment = true;
361 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
363 align = TYPE_ALIGN (TREE_TYPE (exp));
365 else if (TREE_CODE (exp) == INDIRECT_REF
366 || TREE_CODE (exp) == MEM_REF
367 || TREE_CODE (exp) == TARGET_MEM_REF)
369 tree addr = TREE_OPERAND (exp, 0);
370 unsigned ptr_align;
371 unsigned HOST_WIDE_INT ptr_bitpos;
373 if (TREE_CODE (addr) == BIT_AND_EXPR
374 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
376 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
377 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
378 align *= BITS_PER_UNIT;
379 addr = TREE_OPERAND (addr, 0);
382 known_alignment
383 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
384 align = MAX (ptr_align, align);
386 /* The alignment of the pointer operand in a TARGET_MEM_REF
387 has to take the variable offset parts into account. */
388 if (TREE_CODE (exp) == TARGET_MEM_REF)
390 if (TMR_INDEX (exp))
392 unsigned HOST_WIDE_INT step = 1;
393 if (TMR_STEP (exp))
394 step = TREE_INT_CST_LOW (TMR_STEP (exp));
395 align = MIN (align, (step & -step) * BITS_PER_UNIT);
397 if (TMR_INDEX2 (exp))
398 align = BITS_PER_UNIT;
399 known_alignment = false;
402 /* When EXP is an actual memory reference then we can use
403 TYPE_ALIGN of a pointer indirection to derive alignment.
404 Do so only if get_pointer_alignment_1 did not reveal absolute
405 alignment knowledge and if using that alignment would
406 improve the situation. */
407 if (!addr_p && !known_alignment
408 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
409 align = TYPE_ALIGN (TREE_TYPE (exp));
410 else
412 /* Else adjust bitpos accordingly. */
413 bitpos += ptr_bitpos;
414 if (TREE_CODE (exp) == MEM_REF
415 || TREE_CODE (exp) == TARGET_MEM_REF)
416 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
419 else if (TREE_CODE (exp) == STRING_CST)
421 /* STRING_CST are the only constant objects we allow to be not
422 wrapped inside a CONST_DECL. */
423 align = TYPE_ALIGN (TREE_TYPE (exp));
424 #ifdef CONSTANT_ALIGNMENT
425 if (CONSTANT_CLASS_P (exp))
426 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
427 #endif
428 known_alignment = true;
431 /* If there is a non-constant offset part extract the maximum
432 alignment that can prevail. */
433 if (offset)
435 unsigned int trailing_zeros = tree_ctz (offset);
436 if (trailing_zeros < HOST_BITS_PER_INT)
438 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
439 if (inner)
440 align = MIN (align, inner);
444 *alignp = align;
445 *bitposp = bitpos & (*alignp - 1);
446 return known_alignment;
449 /* For a memory reference expression EXP compute values M and N such that M
450 divides (&EXP - N) and such that N < M. If these numbers can be determined,
451 store M in alignp and N in *BITPOSP and return true. Otherwise return false
452 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
454 bool
455 get_object_alignment_1 (tree exp, unsigned int *alignp,
456 unsigned HOST_WIDE_INT *bitposp)
458 return get_object_alignment_2 (exp, alignp, bitposp, false);
461 /* Return the alignment in bits of EXP, an object. */
463 unsigned int
464 get_object_alignment (tree exp)
466 unsigned HOST_WIDE_INT bitpos = 0;
467 unsigned int align;
469 get_object_alignment_1 (exp, &align, &bitpos);
471 /* align and bitpos now specify known low bits of the pointer.
472 ptr & (align - 1) == bitpos. */
474 if (bitpos != 0)
475 align = (bitpos & -bitpos);
476 return align;
479 /* For a pointer valued expression EXP compute values M and N such that M
480 divides (EXP - N) and such that N < M. If these numbers can be determined,
481 store M in alignp and N in *BITPOSP and return true. Return false if
482 the results are just a conservative approximation.
484 If EXP is not a pointer, false is returned too. */
486 bool
487 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
488 unsigned HOST_WIDE_INT *bitposp)
490 STRIP_NOPS (exp);
492 if (TREE_CODE (exp) == ADDR_EXPR)
493 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
494 alignp, bitposp, true);
495 else if (TREE_CODE (exp) == SSA_NAME
496 && POINTER_TYPE_P (TREE_TYPE (exp)))
498 unsigned int ptr_align, ptr_misalign;
499 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
501 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
503 *bitposp = ptr_misalign * BITS_PER_UNIT;
504 *alignp = ptr_align * BITS_PER_UNIT;
505 /* We cannot really tell whether this result is an approximation. */
506 return true;
508 else
510 *bitposp = 0;
511 *alignp = BITS_PER_UNIT;
512 return false;
515 else if (TREE_CODE (exp) == INTEGER_CST)
517 *alignp = BIGGEST_ALIGNMENT;
518 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
519 & (BIGGEST_ALIGNMENT - 1));
520 return true;
523 *bitposp = 0;
524 *alignp = BITS_PER_UNIT;
525 return false;
528 /* Return the alignment in bits of EXP, a pointer valued expression.
529 The alignment returned is, by default, the alignment of the thing that
530 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
532 Otherwise, look at the expression to see if we can do better, i.e., if the
533 expression is actually pointing at an object whose alignment is tighter. */
535 unsigned int
536 get_pointer_alignment (tree exp)
538 unsigned HOST_WIDE_INT bitpos = 0;
539 unsigned int align;
541 get_pointer_alignment_1 (exp, &align, &bitpos);
543 /* align and bitpos now specify known low bits of the pointer.
544 ptr & (align - 1) == bitpos. */
546 if (bitpos != 0)
547 align = (bitpos & -bitpos);
549 return align;
552 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
553 way, because it could contain a zero byte in the middle.
554 TREE_STRING_LENGTH is the size of the character array, not the string.
556 ONLY_VALUE should be nonzero if the result is not going to be emitted
557 into the instruction stream and zero if it is going to be expanded.
558 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
559 is returned, otherwise NULL, since
560 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
561 evaluate the side-effects.
563 The value returned is of type `ssizetype'.
565 Unfortunately, string_constant can't access the values of const char
566 arrays with initializers, so neither can we do so here. */
568 tree
569 c_strlen (tree src, int only_value)
571 tree offset_node;
572 HOST_WIDE_INT offset;
573 int max;
574 const char *ptr;
575 location_t loc;
577 STRIP_NOPS (src);
578 if (TREE_CODE (src) == COND_EXPR
579 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
581 tree len1, len2;
583 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
584 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
585 if (tree_int_cst_equal (len1, len2))
586 return len1;
589 if (TREE_CODE (src) == COMPOUND_EXPR
590 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
591 return c_strlen (TREE_OPERAND (src, 1), only_value);
593 loc = EXPR_LOC_OR_LOC (src, input_location);
595 src = string_constant (src, &offset_node);
596 if (src == 0)
597 return NULL_TREE;
599 max = TREE_STRING_LENGTH (src) - 1;
600 ptr = TREE_STRING_POINTER (src);
602 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
604 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
605 compute the offset to the following null if we don't know where to
606 start searching for it. */
607 int i;
609 for (i = 0; i < max; i++)
610 if (ptr[i] == 0)
611 return NULL_TREE;
613 /* We don't know the starting offset, but we do know that the string
614 has no internal zero bytes. We can assume that the offset falls
615 within the bounds of the string; otherwise, the programmer deserves
616 what he gets. Subtract the offset from the length of the string,
617 and return that. This would perhaps not be valid if we were dealing
618 with named arrays in addition to literal string constants. */
620 return size_diffop_loc (loc, size_int (max), offset_node);
623 /* We have a known offset into the string. Start searching there for
624 a null character if we can represent it as a single HOST_WIDE_INT. */
625 if (offset_node == 0)
626 offset = 0;
627 else if (! tree_fits_shwi_p (offset_node))
628 offset = -1;
629 else
630 offset = tree_to_shwi (offset_node);
632 /* If the offset is known to be out of bounds, warn, and call strlen at
633 runtime. */
634 if (offset < 0 || offset > max)
636 /* Suppress multiple warnings for propagated constant strings. */
637 if (! TREE_NO_WARNING (src))
639 warning_at (loc, 0, "offset outside bounds of constant string");
640 TREE_NO_WARNING (src) = 1;
642 return NULL_TREE;
645 /* Use strlen to search for the first zero byte. Since any strings
646 constructed with build_string will have nulls appended, we win even
647 if we get handed something like (char[4])"abcd".
649 Since OFFSET is our starting index into the string, no further
650 calculation is needed. */
651 return ssize_int (strlen (ptr + offset));
654 /* Return a char pointer for a C string if it is a string constant
655 or sum of string constant and integer constant. */
657 static const char *
658 c_getstr (tree src)
660 tree offset_node;
662 src = string_constant (src, &offset_node);
663 if (src == 0)
664 return 0;
666 if (offset_node == 0)
667 return TREE_STRING_POINTER (src);
668 else if (!tree_fits_uhwi_p (offset_node)
669 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
670 return 0;
672 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
675 /* Return a constant integer corresponding to target reading
676 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
678 static rtx
679 c_readstr (const char *str, enum machine_mode mode)
681 HOST_WIDE_INT ch;
682 unsigned int i, j;
683 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
685 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
686 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
687 / HOST_BITS_PER_WIDE_INT;
689 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
690 for (i = 0; i < len; i++)
691 tmp[i] = 0;
693 ch = 1;
694 for (i = 0; i < GET_MODE_SIZE (mode); i++)
696 j = i;
697 if (WORDS_BIG_ENDIAN)
698 j = GET_MODE_SIZE (mode) - i - 1;
699 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
700 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
701 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
702 j *= BITS_PER_UNIT;
704 if (ch)
705 ch = (unsigned char) str[i];
706 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
709 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
710 return immed_wide_int_const (c, mode);
713 /* Cast a target constant CST to target CHAR and if that value fits into
714 host char type, return zero and put that value into variable pointed to by
715 P. */
717 static int
718 target_char_cast (tree cst, char *p)
720 unsigned HOST_WIDE_INT val, hostval;
722 if (TREE_CODE (cst) != INTEGER_CST
723 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
724 return 1;
726 /* Do not care if it fits or not right here. */
727 val = TREE_INT_CST_LOW (cst);
729 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
730 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
732 hostval = val;
733 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
734 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
736 if (val != hostval)
737 return 1;
739 *p = hostval;
740 return 0;
743 /* Similar to save_expr, but assumes that arbitrary code is not executed
744 in between the multiple evaluations. In particular, we assume that a
745 non-addressable local variable will not be modified. */
747 static tree
748 builtin_save_expr (tree exp)
750 if (TREE_CODE (exp) == SSA_NAME
751 || (TREE_ADDRESSABLE (exp) == 0
752 && (TREE_CODE (exp) == PARM_DECL
753 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
754 return exp;
756 return save_expr (exp);
759 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
760 times to get the address of either a higher stack frame, or a return
761 address located within it (depending on FNDECL_CODE). */
763 static rtx
764 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
766 int i;
768 #ifdef INITIAL_FRAME_ADDRESS_RTX
769 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
770 #else
771 rtx tem;
773 /* For a zero count with __builtin_return_address, we don't care what
774 frame address we return, because target-specific definitions will
775 override us. Therefore frame pointer elimination is OK, and using
776 the soft frame pointer is OK.
778 For a nonzero count, or a zero count with __builtin_frame_address,
779 we require a stable offset from the current frame pointer to the
780 previous one, so we must use the hard frame pointer, and
781 we must disable frame pointer elimination. */
782 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
783 tem = frame_pointer_rtx;
784 else
786 tem = hard_frame_pointer_rtx;
788 /* Tell reload not to eliminate the frame pointer. */
789 crtl->accesses_prior_frames = 1;
791 #endif
793 /* Some machines need special handling before we can access
794 arbitrary frames. For example, on the SPARC, we must first flush
795 all register windows to the stack. */
796 #ifdef SETUP_FRAME_ADDRESSES
797 if (count > 0)
798 SETUP_FRAME_ADDRESSES ();
799 #endif
801 /* On the SPARC, the return address is not in the frame, it is in a
802 register. There is no way to access it off of the current frame
803 pointer, but it can be accessed off the previous frame pointer by
804 reading the value from the register window save area. */
805 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
806 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
807 count--;
808 #endif
810 /* Scan back COUNT frames to the specified frame. */
811 for (i = 0; i < count; i++)
813 /* Assume the dynamic chain pointer is in the word that the
814 frame address points to, unless otherwise specified. */
815 #ifdef DYNAMIC_CHAIN_ADDRESS
816 tem = DYNAMIC_CHAIN_ADDRESS (tem);
817 #endif
818 tem = memory_address (Pmode, tem);
819 tem = gen_frame_mem (Pmode, tem);
820 tem = copy_to_reg (tem);
823 /* For __builtin_frame_address, return what we've got. But, on
824 the SPARC for example, we may have to add a bias. */
825 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
826 #ifdef FRAME_ADDR_RTX
827 return FRAME_ADDR_RTX (tem);
828 #else
829 return tem;
830 #endif
832 /* For __builtin_return_address, get the return address from that frame. */
833 #ifdef RETURN_ADDR_RTX
834 tem = RETURN_ADDR_RTX (count, tem);
835 #else
836 tem = memory_address (Pmode,
837 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
838 tem = gen_frame_mem (Pmode, tem);
839 #endif
840 return tem;
843 /* Alias set used for setjmp buffer. */
844 static alias_set_type setjmp_alias_set = -1;
846 /* Construct the leading half of a __builtin_setjmp call. Control will
847 return to RECEIVER_LABEL. This is also called directly by the SJLJ
848 exception handling code. */
850 void
851 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
853 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
854 rtx stack_save;
855 rtx mem;
857 if (setjmp_alias_set == -1)
858 setjmp_alias_set = new_alias_set ();
860 buf_addr = convert_memory_address (Pmode, buf_addr);
862 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
864 /* We store the frame pointer and the address of receiver_label in
865 the buffer and use the rest of it for the stack save area, which
866 is machine-dependent. */
868 mem = gen_rtx_MEM (Pmode, buf_addr);
869 set_mem_alias_set (mem, setjmp_alias_set);
870 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
872 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
873 GET_MODE_SIZE (Pmode))),
874 set_mem_alias_set (mem, setjmp_alias_set);
876 emit_move_insn (validize_mem (mem),
877 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
879 stack_save = gen_rtx_MEM (sa_mode,
880 plus_constant (Pmode, buf_addr,
881 2 * GET_MODE_SIZE (Pmode)));
882 set_mem_alias_set (stack_save, setjmp_alias_set);
883 emit_stack_save (SAVE_NONLOCAL, &stack_save);
885 /* If there is further processing to do, do it. */
886 #ifdef HAVE_builtin_setjmp_setup
887 if (HAVE_builtin_setjmp_setup)
888 emit_insn (gen_builtin_setjmp_setup (buf_addr));
889 #endif
891 /* We have a nonlocal label. */
892 cfun->has_nonlocal_label = 1;
895 /* Construct the trailing part of a __builtin_setjmp call. This is
896 also called directly by the SJLJ exception handling code.
897 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
899 void
900 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
902 rtx chain;
904 /* Mark the FP as used when we get here, so we have to make sure it's
905 marked as used by this function. */
906 emit_use (hard_frame_pointer_rtx);
908 /* Mark the static chain as clobbered here so life information
909 doesn't get messed up for it. */
910 chain = targetm.calls.static_chain (current_function_decl, true);
911 if (chain && REG_P (chain))
912 emit_clobber (chain);
914 /* Now put in the code to restore the frame pointer, and argument
915 pointer, if needed. */
916 #ifdef HAVE_nonlocal_goto
917 if (! HAVE_nonlocal_goto)
918 #endif
920 /* First adjust our frame pointer to its actual value. It was
921 previously set to the start of the virtual area corresponding to
922 the stacked variables when we branched here and now needs to be
923 adjusted to the actual hardware fp value.
925 Assignments to virtual registers are converted by
926 instantiate_virtual_regs into the corresponding assignment
927 to the underlying register (fp in this case) that makes
928 the original assignment true.
929 So the following insn will actually be decrementing fp by
930 STARTING_FRAME_OFFSET. */
931 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
933 /* Restoring the frame pointer also modifies the hard frame pointer.
934 Mark it used (so that the previous assignment remains live once
935 the frame pointer is eliminated) and clobbered (to represent the
936 implicit update from the assignment). */
937 emit_use (hard_frame_pointer_rtx);
938 emit_clobber (hard_frame_pointer_rtx);
941 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
942 if (fixed_regs[ARG_POINTER_REGNUM])
944 #ifdef ELIMINABLE_REGS
945 /* If the argument pointer can be eliminated in favor of the
946 frame pointer, we don't need to restore it. We assume here
947 that if such an elimination is present, it can always be used.
948 This is the case on all known machines; if we don't make this
949 assumption, we do unnecessary saving on many machines. */
950 size_t i;
951 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
953 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
954 if (elim_regs[i].from == ARG_POINTER_REGNUM
955 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
956 break;
958 if (i == ARRAY_SIZE (elim_regs))
959 #endif
961 /* Now restore our arg pointer from the address at which it
962 was saved in our stack frame. */
963 emit_move_insn (crtl->args.internal_arg_pointer,
964 copy_to_reg (get_arg_pointer_save_area ()));
967 #endif
969 #ifdef HAVE_builtin_setjmp_receiver
970 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
971 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
972 else
973 #endif
974 #ifdef HAVE_nonlocal_goto_receiver
975 if (HAVE_nonlocal_goto_receiver)
976 emit_insn (gen_nonlocal_goto_receiver ());
977 else
978 #endif
979 { /* Nothing */ }
981 /* We must not allow the code we just generated to be reordered by
982 scheduling. Specifically, the update of the frame pointer must
983 happen immediately, not later. */
984 emit_insn (gen_blockage ());
987 /* __builtin_longjmp is passed a pointer to an array of five words (not
988 all will be used on all machines). It operates similarly to the C
989 library function of the same name, but is more efficient. Much of
990 the code below is copied from the handling of non-local gotos. */
992 static void
993 expand_builtin_longjmp (rtx buf_addr, rtx value)
995 rtx fp, lab, stack, insn, last;
996 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
998 /* DRAP is needed for stack realign if longjmp is expanded to current
999 function */
1000 if (SUPPORTS_STACK_ALIGNMENT)
1001 crtl->need_drap = true;
1003 if (setjmp_alias_set == -1)
1004 setjmp_alias_set = new_alias_set ();
1006 buf_addr = convert_memory_address (Pmode, buf_addr);
1008 buf_addr = force_reg (Pmode, buf_addr);
1010 /* We require that the user must pass a second argument of 1, because
1011 that is what builtin_setjmp will return. */
1012 gcc_assert (value == const1_rtx);
1014 last = get_last_insn ();
1015 #ifdef HAVE_builtin_longjmp
1016 if (HAVE_builtin_longjmp)
1017 emit_insn (gen_builtin_longjmp (buf_addr));
1018 else
1019 #endif
1021 fp = gen_rtx_MEM (Pmode, buf_addr);
1022 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1023 GET_MODE_SIZE (Pmode)));
1025 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1026 2 * GET_MODE_SIZE (Pmode)));
1027 set_mem_alias_set (fp, setjmp_alias_set);
1028 set_mem_alias_set (lab, setjmp_alias_set);
1029 set_mem_alias_set (stack, setjmp_alias_set);
1031 /* Pick up FP, label, and SP from the block and jump. This code is
1032 from expand_goto in stmt.c; see there for detailed comments. */
1033 #ifdef HAVE_nonlocal_goto
1034 if (HAVE_nonlocal_goto)
1035 /* We have to pass a value to the nonlocal_goto pattern that will
1036 get copied into the static_chain pointer, but it does not matter
1037 what that value is, because builtin_setjmp does not use it. */
1038 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1039 else
1040 #endif
1042 lab = copy_to_reg (lab);
1044 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1045 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1047 emit_move_insn (hard_frame_pointer_rtx, fp);
1048 emit_stack_restore (SAVE_NONLOCAL, stack);
1050 emit_use (hard_frame_pointer_rtx);
1051 emit_use (stack_pointer_rtx);
1052 emit_indirect_jump (lab);
1056 /* Search backwards and mark the jump insn as a non-local goto.
1057 Note that this precludes the use of __builtin_longjmp to a
1058 __builtin_setjmp target in the same function. However, we've
1059 already cautioned the user that these functions are for
1060 internal exception handling use only. */
1061 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1063 gcc_assert (insn != last);
1065 if (JUMP_P (insn))
1067 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1068 break;
1070 else if (CALL_P (insn))
1071 break;
1075 static inline bool
1076 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1078 return (iter->i < iter->n);
1081 /* This function validates the types of a function call argument list
1082 against a specified list of tree_codes. If the last specifier is a 0,
1083 that represents an ellipses, otherwise the last specifier must be a
1084 VOID_TYPE. */
1086 static bool
1087 validate_arglist (const_tree callexpr, ...)
1089 enum tree_code code;
1090 bool res = 0;
1091 va_list ap;
1092 const_call_expr_arg_iterator iter;
1093 const_tree arg;
1095 va_start (ap, callexpr);
1096 init_const_call_expr_arg_iterator (callexpr, &iter);
1100 code = (enum tree_code) va_arg (ap, int);
1101 switch (code)
1103 case 0:
1104 /* This signifies an ellipses, any further arguments are all ok. */
1105 res = true;
1106 goto end;
1107 case VOID_TYPE:
1108 /* This signifies an endlink, if no arguments remain, return
1109 true, otherwise return false. */
1110 res = !more_const_call_expr_args_p (&iter);
1111 goto end;
1112 default:
1113 /* If no parameters remain or the parameter's code does not
1114 match the specified code, return false. Otherwise continue
1115 checking any remaining arguments. */
1116 arg = next_const_call_expr_arg (&iter);
1117 if (!validate_arg (arg, code))
1118 goto end;
1119 break;
1122 while (1);
1124 /* We need gotos here since we can only have one VA_CLOSE in a
1125 function. */
1126 end: ;
1127 va_end (ap);
1129 return res;
1132 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1133 and the address of the save area. */
1135 static rtx
1136 expand_builtin_nonlocal_goto (tree exp)
1138 tree t_label, t_save_area;
1139 rtx r_label, r_save_area, r_fp, r_sp, insn;
1141 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1142 return NULL_RTX;
1144 t_label = CALL_EXPR_ARG (exp, 0);
1145 t_save_area = CALL_EXPR_ARG (exp, 1);
1147 r_label = expand_normal (t_label);
1148 r_label = convert_memory_address (Pmode, r_label);
1149 r_save_area = expand_normal (t_save_area);
1150 r_save_area = convert_memory_address (Pmode, r_save_area);
1151 /* Copy the address of the save location to a register just in case it was
1152 based on the frame pointer. */
1153 r_save_area = copy_to_reg (r_save_area);
1154 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1155 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1156 plus_constant (Pmode, r_save_area,
1157 GET_MODE_SIZE (Pmode)));
1159 crtl->has_nonlocal_goto = 1;
1161 #ifdef HAVE_nonlocal_goto
1162 /* ??? We no longer need to pass the static chain value, afaik. */
1163 if (HAVE_nonlocal_goto)
1164 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1165 else
1166 #endif
1168 r_label = copy_to_reg (r_label);
1170 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1171 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1173 /* Restore frame pointer for containing function. */
1174 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1175 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1177 /* USE of hard_frame_pointer_rtx added for consistency;
1178 not clear if really needed. */
1179 emit_use (hard_frame_pointer_rtx);
1180 emit_use (stack_pointer_rtx);
1182 /* If the architecture is using a GP register, we must
1183 conservatively assume that the target function makes use of it.
1184 The prologue of functions with nonlocal gotos must therefore
1185 initialize the GP register to the appropriate value, and we
1186 must then make sure that this value is live at the point
1187 of the jump. (Note that this doesn't necessarily apply
1188 to targets with a nonlocal_goto pattern; they are free
1189 to implement it in their own way. Note also that this is
1190 a no-op if the GP register is a global invariant.) */
1191 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1192 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1193 emit_use (pic_offset_table_rtx);
1195 emit_indirect_jump (r_label);
1198 /* Search backwards to the jump insn and mark it as a
1199 non-local goto. */
1200 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1202 if (JUMP_P (insn))
1204 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1205 break;
1207 else if (CALL_P (insn))
1208 break;
1211 return const0_rtx;
1214 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1215 (not all will be used on all machines) that was passed to __builtin_setjmp.
1216 It updates the stack pointer in that block to correspond to the current
1217 stack pointer. */
1219 static void
1220 expand_builtin_update_setjmp_buf (rtx buf_addr)
1222 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1223 rtx stack_save
1224 = gen_rtx_MEM (sa_mode,
1225 memory_address
1226 (sa_mode,
1227 plus_constant (Pmode, buf_addr,
1228 2 * GET_MODE_SIZE (Pmode))));
1230 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1233 /* Expand a call to __builtin_prefetch. For a target that does not support
1234 data prefetch, evaluate the memory address argument in case it has side
1235 effects. */
1237 static void
1238 expand_builtin_prefetch (tree exp)
1240 tree arg0, arg1, arg2;
1241 int nargs;
1242 rtx op0, op1, op2;
1244 if (!validate_arglist (exp, POINTER_TYPE, 0))
1245 return;
1247 arg0 = CALL_EXPR_ARG (exp, 0);
1249 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1250 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1251 locality). */
1252 nargs = call_expr_nargs (exp);
1253 if (nargs > 1)
1254 arg1 = CALL_EXPR_ARG (exp, 1);
1255 else
1256 arg1 = integer_zero_node;
1257 if (nargs > 2)
1258 arg2 = CALL_EXPR_ARG (exp, 2);
1259 else
1260 arg2 = integer_three_node;
1262 /* Argument 0 is an address. */
1263 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1265 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1266 if (TREE_CODE (arg1) != INTEGER_CST)
1268 error ("second argument to %<__builtin_prefetch%> must be a constant");
1269 arg1 = integer_zero_node;
1271 op1 = expand_normal (arg1);
1272 /* Argument 1 must be either zero or one. */
1273 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1275 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1276 " using zero");
1277 op1 = const0_rtx;
1280 /* Argument 2 (locality) must be a compile-time constant int. */
1281 if (TREE_CODE (arg2) != INTEGER_CST)
1283 error ("third argument to %<__builtin_prefetch%> must be a constant");
1284 arg2 = integer_zero_node;
1286 op2 = expand_normal (arg2);
1287 /* Argument 2 must be 0, 1, 2, or 3. */
1288 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1290 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1291 op2 = const0_rtx;
1294 #ifdef HAVE_prefetch
1295 if (HAVE_prefetch)
1297 struct expand_operand ops[3];
1299 create_address_operand (&ops[0], op0);
1300 create_integer_operand (&ops[1], INTVAL (op1));
1301 create_integer_operand (&ops[2], INTVAL (op2));
1302 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1303 return;
1305 #endif
1307 /* Don't do anything with direct references to volatile memory, but
1308 generate code to handle other side effects. */
1309 if (!MEM_P (op0) && side_effects_p (op0))
1310 emit_insn (op0);
1313 /* Get a MEM rtx for expression EXP which is the address of an operand
1314 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1315 the maximum length of the block of memory that might be accessed or
1316 NULL if unknown. */
1318 static rtx
1319 get_memory_rtx (tree exp, tree len)
1321 tree orig_exp = exp;
1322 rtx addr, mem;
1324 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1325 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1326 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1327 exp = TREE_OPERAND (exp, 0);
1329 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1330 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1332 /* Get an expression we can use to find the attributes to assign to MEM.
1333 First remove any nops. */
1334 while (CONVERT_EXPR_P (exp)
1335 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1336 exp = TREE_OPERAND (exp, 0);
1338 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1339 (as builtin stringops may alias with anything). */
1340 exp = fold_build2 (MEM_REF,
1341 build_array_type (char_type_node,
1342 build_range_type (sizetype,
1343 size_one_node, len)),
1344 exp, build_int_cst (ptr_type_node, 0));
1346 /* If the MEM_REF has no acceptable address, try to get the base object
1347 from the original address we got, and build an all-aliasing
1348 unknown-sized access to that one. */
1349 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1350 set_mem_attributes (mem, exp, 0);
1351 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1352 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1353 0))))
1355 exp = build_fold_addr_expr (exp);
1356 exp = fold_build2 (MEM_REF,
1357 build_array_type (char_type_node,
1358 build_range_type (sizetype,
1359 size_zero_node,
1360 NULL)),
1361 exp, build_int_cst (ptr_type_node, 0));
1362 set_mem_attributes (mem, exp, 0);
1364 set_mem_alias_set (mem, 0);
1365 return mem;
1368 /* Built-in functions to perform an untyped call and return. */
1370 #define apply_args_mode \
1371 (this_target_builtins->x_apply_args_mode)
1372 #define apply_result_mode \
1373 (this_target_builtins->x_apply_result_mode)
1375 /* Return the size required for the block returned by __builtin_apply_args,
1376 and initialize apply_args_mode. */
1378 static int
1379 apply_args_size (void)
1381 static int size = -1;
1382 int align;
1383 unsigned int regno;
1384 enum machine_mode mode;
1386 /* The values computed by this function never change. */
1387 if (size < 0)
1389 /* The first value is the incoming arg-pointer. */
1390 size = GET_MODE_SIZE (Pmode);
1392 /* The second value is the structure value address unless this is
1393 passed as an "invisible" first argument. */
1394 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1395 size += GET_MODE_SIZE (Pmode);
1397 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1398 if (FUNCTION_ARG_REGNO_P (regno))
1400 mode = targetm.calls.get_raw_arg_mode (regno);
1402 gcc_assert (mode != VOIDmode);
1404 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1405 if (size % align != 0)
1406 size = CEIL (size, align) * align;
1407 size += GET_MODE_SIZE (mode);
1408 apply_args_mode[regno] = mode;
1410 else
1412 apply_args_mode[regno] = VOIDmode;
1415 return size;
1418 /* Return the size required for the block returned by __builtin_apply,
1419 and initialize apply_result_mode. */
1421 static int
1422 apply_result_size (void)
1424 static int size = -1;
1425 int align, regno;
1426 enum machine_mode mode;
1428 /* The values computed by this function never change. */
1429 if (size < 0)
1431 size = 0;
1433 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1434 if (targetm.calls.function_value_regno_p (regno))
1436 mode = targetm.calls.get_raw_result_mode (regno);
1438 gcc_assert (mode != VOIDmode);
1440 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1441 if (size % align != 0)
1442 size = CEIL (size, align) * align;
1443 size += GET_MODE_SIZE (mode);
1444 apply_result_mode[regno] = mode;
1446 else
1447 apply_result_mode[regno] = VOIDmode;
1449 /* Allow targets that use untyped_call and untyped_return to override
1450 the size so that machine-specific information can be stored here. */
1451 #ifdef APPLY_RESULT_SIZE
1452 size = APPLY_RESULT_SIZE;
1453 #endif
1455 return size;
1458 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1459 /* Create a vector describing the result block RESULT. If SAVEP is true,
1460 the result block is used to save the values; otherwise it is used to
1461 restore the values. */
1463 static rtx
1464 result_vector (int savep, rtx result)
1466 int regno, size, align, nelts;
1467 enum machine_mode mode;
1468 rtx reg, mem;
1469 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1471 size = nelts = 0;
1472 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1473 if ((mode = apply_result_mode[regno]) != VOIDmode)
1475 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1476 if (size % align != 0)
1477 size = CEIL (size, align) * align;
1478 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1479 mem = adjust_address (result, mode, size);
1480 savevec[nelts++] = (savep
1481 ? gen_rtx_SET (VOIDmode, mem, reg)
1482 : gen_rtx_SET (VOIDmode, reg, mem));
1483 size += GET_MODE_SIZE (mode);
1485 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1487 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1489 /* Save the state required to perform an untyped call with the same
1490 arguments as were passed to the current function. */
1492 static rtx
1493 expand_builtin_apply_args_1 (void)
1495 rtx registers, tem;
1496 int size, align, regno;
1497 enum machine_mode mode;
1498 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1500 /* Create a block where the arg-pointer, structure value address,
1501 and argument registers can be saved. */
1502 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1504 /* Walk past the arg-pointer and structure value address. */
1505 size = GET_MODE_SIZE (Pmode);
1506 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1507 size += GET_MODE_SIZE (Pmode);
1509 /* Save each register used in calling a function to the block. */
1510 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1511 if ((mode = apply_args_mode[regno]) != VOIDmode)
1513 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1514 if (size % align != 0)
1515 size = CEIL (size, align) * align;
1517 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1519 emit_move_insn (adjust_address (registers, mode, size), tem);
1520 size += GET_MODE_SIZE (mode);
1523 /* Save the arg pointer to the block. */
1524 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1525 #ifdef STACK_GROWS_DOWNWARD
1526 /* We need the pointer as the caller actually passed them to us, not
1527 as we might have pretended they were passed. Make sure it's a valid
1528 operand, as emit_move_insn isn't expected to handle a PLUS. */
1530 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1531 NULL_RTX);
1532 #endif
1533 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1535 size = GET_MODE_SIZE (Pmode);
1537 /* Save the structure value address unless this is passed as an
1538 "invisible" first argument. */
1539 if (struct_incoming_value)
1541 emit_move_insn (adjust_address (registers, Pmode, size),
1542 copy_to_reg (struct_incoming_value));
1543 size += GET_MODE_SIZE (Pmode);
1546 /* Return the address of the block. */
1547 return copy_addr_to_reg (XEXP (registers, 0));
1550 /* __builtin_apply_args returns block of memory allocated on
1551 the stack into which is stored the arg pointer, structure
1552 value address, static chain, and all the registers that might
1553 possibly be used in performing a function call. The code is
1554 moved to the start of the function so the incoming values are
1555 saved. */
1557 static rtx
1558 expand_builtin_apply_args (void)
1560 /* Don't do __builtin_apply_args more than once in a function.
1561 Save the result of the first call and reuse it. */
1562 if (apply_args_value != 0)
1563 return apply_args_value;
1565 /* When this function is called, it means that registers must be
1566 saved on entry to this function. So we migrate the
1567 call to the first insn of this function. */
1568 rtx temp;
1569 rtx seq;
1571 start_sequence ();
1572 temp = expand_builtin_apply_args_1 ();
1573 seq = get_insns ();
1574 end_sequence ();
1576 apply_args_value = temp;
1578 /* Put the insns after the NOTE that starts the function.
1579 If this is inside a start_sequence, make the outer-level insn
1580 chain current, so the code is placed at the start of the
1581 function. If internal_arg_pointer is a non-virtual pseudo,
1582 it needs to be placed after the function that initializes
1583 that pseudo. */
1584 push_topmost_sequence ();
1585 if (REG_P (crtl->args.internal_arg_pointer)
1586 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1587 emit_insn_before (seq, parm_birth_insn);
1588 else
1589 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1590 pop_topmost_sequence ();
1591 return temp;
1595 /* Perform an untyped call and save the state required to perform an
1596 untyped return of whatever value was returned by the given function. */
1598 static rtx
1599 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1601 int size, align, regno;
1602 enum machine_mode mode;
1603 rtx incoming_args, result, reg, dest, src, call_insn;
1604 rtx old_stack_level = 0;
1605 rtx call_fusage = 0;
1606 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1608 arguments = convert_memory_address (Pmode, arguments);
1610 /* Create a block where the return registers can be saved. */
1611 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1613 /* Fetch the arg pointer from the ARGUMENTS block. */
1614 incoming_args = gen_reg_rtx (Pmode);
1615 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1616 #ifndef STACK_GROWS_DOWNWARD
1617 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1618 incoming_args, 0, OPTAB_LIB_WIDEN);
1619 #endif
1621 /* Push a new argument block and copy the arguments. Do not allow
1622 the (potential) memcpy call below to interfere with our stack
1623 manipulations. */
1624 do_pending_stack_adjust ();
1625 NO_DEFER_POP;
1627 /* Save the stack with nonlocal if available. */
1628 #ifdef HAVE_save_stack_nonlocal
1629 if (HAVE_save_stack_nonlocal)
1630 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1631 else
1632 #endif
1633 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1635 /* Allocate a block of memory onto the stack and copy the memory
1636 arguments to the outgoing arguments address. We can pass TRUE
1637 as the 4th argument because we just saved the stack pointer
1638 and will restore it right after the call. */
1639 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1641 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1642 may have already set current_function_calls_alloca to true.
1643 current_function_calls_alloca won't be set if argsize is zero,
1644 so we have to guarantee need_drap is true here. */
1645 if (SUPPORTS_STACK_ALIGNMENT)
1646 crtl->need_drap = true;
1648 dest = virtual_outgoing_args_rtx;
1649 #ifndef STACK_GROWS_DOWNWARD
1650 if (CONST_INT_P (argsize))
1651 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1652 else
1653 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1654 #endif
1655 dest = gen_rtx_MEM (BLKmode, dest);
1656 set_mem_align (dest, PARM_BOUNDARY);
1657 src = gen_rtx_MEM (BLKmode, incoming_args);
1658 set_mem_align (src, PARM_BOUNDARY);
1659 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1661 /* Refer to the argument block. */
1662 apply_args_size ();
1663 arguments = gen_rtx_MEM (BLKmode, arguments);
1664 set_mem_align (arguments, PARM_BOUNDARY);
1666 /* Walk past the arg-pointer and structure value address. */
1667 size = GET_MODE_SIZE (Pmode);
1668 if (struct_value)
1669 size += GET_MODE_SIZE (Pmode);
1671 /* Restore each of the registers previously saved. Make USE insns
1672 for each of these registers for use in making the call. */
1673 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1674 if ((mode = apply_args_mode[regno]) != VOIDmode)
1676 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1677 if (size % align != 0)
1678 size = CEIL (size, align) * align;
1679 reg = gen_rtx_REG (mode, regno);
1680 emit_move_insn (reg, adjust_address (arguments, mode, size));
1681 use_reg (&call_fusage, reg);
1682 size += GET_MODE_SIZE (mode);
1685 /* Restore the structure value address unless this is passed as an
1686 "invisible" first argument. */
1687 size = GET_MODE_SIZE (Pmode);
1688 if (struct_value)
1690 rtx value = gen_reg_rtx (Pmode);
1691 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1692 emit_move_insn (struct_value, value);
1693 if (REG_P (struct_value))
1694 use_reg (&call_fusage, struct_value);
1695 size += GET_MODE_SIZE (Pmode);
1698 /* All arguments and registers used for the call are set up by now! */
1699 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1701 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1702 and we don't want to load it into a register as an optimization,
1703 because prepare_call_address already did it if it should be done. */
1704 if (GET_CODE (function) != SYMBOL_REF)
1705 function = memory_address (FUNCTION_MODE, function);
1707 /* Generate the actual call instruction and save the return value. */
1708 #ifdef HAVE_untyped_call
1709 if (HAVE_untyped_call)
1710 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1711 result, result_vector (1, result)));
1712 else
1713 #endif
1714 #ifdef HAVE_call_value
1715 if (HAVE_call_value)
1717 rtx valreg = 0;
1719 /* Locate the unique return register. It is not possible to
1720 express a call that sets more than one return register using
1721 call_value; use untyped_call for that. In fact, untyped_call
1722 only needs to save the return registers in the given block. */
1723 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1724 if ((mode = apply_result_mode[regno]) != VOIDmode)
1726 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1728 valreg = gen_rtx_REG (mode, regno);
1731 emit_call_insn (GEN_CALL_VALUE (valreg,
1732 gen_rtx_MEM (FUNCTION_MODE, function),
1733 const0_rtx, NULL_RTX, const0_rtx));
1735 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1737 else
1738 #endif
1739 gcc_unreachable ();
1741 /* Find the CALL insn we just emitted, and attach the register usage
1742 information. */
1743 call_insn = last_call_insn ();
1744 add_function_usage_to (call_insn, call_fusage);
1746 /* Restore the stack. */
1747 #ifdef HAVE_save_stack_nonlocal
1748 if (HAVE_save_stack_nonlocal)
1749 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1750 else
1751 #endif
1752 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1753 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1755 OK_DEFER_POP;
1757 /* Return the address of the result block. */
1758 result = copy_addr_to_reg (XEXP (result, 0));
1759 return convert_memory_address (ptr_mode, result);
1762 /* Perform an untyped return. */
1764 static void
1765 expand_builtin_return (rtx result)
1767 int size, align, regno;
1768 enum machine_mode mode;
1769 rtx reg;
1770 rtx call_fusage = 0;
1772 result = convert_memory_address (Pmode, result);
1774 apply_result_size ();
1775 result = gen_rtx_MEM (BLKmode, result);
1777 #ifdef HAVE_untyped_return
1778 if (HAVE_untyped_return)
1780 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1781 emit_barrier ();
1782 return;
1784 #endif
1786 /* Restore the return value and note that each value is used. */
1787 size = 0;
1788 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1789 if ((mode = apply_result_mode[regno]) != VOIDmode)
1791 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1792 if (size % align != 0)
1793 size = CEIL (size, align) * align;
1794 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1795 emit_move_insn (reg, adjust_address (result, mode, size));
1797 push_to_sequence (call_fusage);
1798 emit_use (reg);
1799 call_fusage = get_insns ();
1800 end_sequence ();
1801 size += GET_MODE_SIZE (mode);
1804 /* Put the USE insns before the return. */
1805 emit_insn (call_fusage);
1807 /* Return whatever values was restored by jumping directly to the end
1808 of the function. */
1809 expand_naked_return ();
1812 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1814 static enum type_class
1815 type_to_class (tree type)
1817 switch (TREE_CODE (type))
1819 case VOID_TYPE: return void_type_class;
1820 case INTEGER_TYPE: return integer_type_class;
1821 case ENUMERAL_TYPE: return enumeral_type_class;
1822 case BOOLEAN_TYPE: return boolean_type_class;
1823 case POINTER_TYPE: return pointer_type_class;
1824 case REFERENCE_TYPE: return reference_type_class;
1825 case OFFSET_TYPE: return offset_type_class;
1826 case REAL_TYPE: return real_type_class;
1827 case COMPLEX_TYPE: return complex_type_class;
1828 case FUNCTION_TYPE: return function_type_class;
1829 case METHOD_TYPE: return method_type_class;
1830 case RECORD_TYPE: return record_type_class;
1831 case UNION_TYPE:
1832 case QUAL_UNION_TYPE: return union_type_class;
1833 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1834 ? string_type_class : array_type_class);
1835 case LANG_TYPE: return lang_type_class;
1836 default: return no_type_class;
1840 /* Expand a call EXP to __builtin_classify_type. */
1842 static rtx
1843 expand_builtin_classify_type (tree exp)
1845 if (call_expr_nargs (exp))
1846 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1847 return GEN_INT (no_type_class);
1850 /* This helper macro, meant to be used in mathfn_built_in below,
1851 determines which among a set of three builtin math functions is
1852 appropriate for a given type mode. The `F' and `L' cases are
1853 automatically generated from the `double' case. */
1854 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1855 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1856 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1857 fcodel = BUILT_IN_MATHFN##L ; break;
1858 /* Similar to above, but appends _R after any F/L suffix. */
1859 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1860 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1861 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1862 fcodel = BUILT_IN_MATHFN##L_R ; break;
1864 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1865 if available. If IMPLICIT is true use the implicit builtin declaration,
1866 otherwise use the explicit declaration. If we can't do the conversion,
1867 return zero. */
1869 static tree
1870 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1872 enum built_in_function fcode, fcodef, fcodel, fcode2;
1874 switch (fn)
1876 CASE_MATHFN (BUILT_IN_ACOS)
1877 CASE_MATHFN (BUILT_IN_ACOSH)
1878 CASE_MATHFN (BUILT_IN_ASIN)
1879 CASE_MATHFN (BUILT_IN_ASINH)
1880 CASE_MATHFN (BUILT_IN_ATAN)
1881 CASE_MATHFN (BUILT_IN_ATAN2)
1882 CASE_MATHFN (BUILT_IN_ATANH)
1883 CASE_MATHFN (BUILT_IN_CBRT)
1884 CASE_MATHFN (BUILT_IN_CEIL)
1885 CASE_MATHFN (BUILT_IN_CEXPI)
1886 CASE_MATHFN (BUILT_IN_COPYSIGN)
1887 CASE_MATHFN (BUILT_IN_COS)
1888 CASE_MATHFN (BUILT_IN_COSH)
1889 CASE_MATHFN (BUILT_IN_DREM)
1890 CASE_MATHFN (BUILT_IN_ERF)
1891 CASE_MATHFN (BUILT_IN_ERFC)
1892 CASE_MATHFN (BUILT_IN_EXP)
1893 CASE_MATHFN (BUILT_IN_EXP10)
1894 CASE_MATHFN (BUILT_IN_EXP2)
1895 CASE_MATHFN (BUILT_IN_EXPM1)
1896 CASE_MATHFN (BUILT_IN_FABS)
1897 CASE_MATHFN (BUILT_IN_FDIM)
1898 CASE_MATHFN (BUILT_IN_FLOOR)
1899 CASE_MATHFN (BUILT_IN_FMA)
1900 CASE_MATHFN (BUILT_IN_FMAX)
1901 CASE_MATHFN (BUILT_IN_FMIN)
1902 CASE_MATHFN (BUILT_IN_FMOD)
1903 CASE_MATHFN (BUILT_IN_FREXP)
1904 CASE_MATHFN (BUILT_IN_GAMMA)
1905 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1906 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1907 CASE_MATHFN (BUILT_IN_HYPOT)
1908 CASE_MATHFN (BUILT_IN_ILOGB)
1909 CASE_MATHFN (BUILT_IN_ICEIL)
1910 CASE_MATHFN (BUILT_IN_IFLOOR)
1911 CASE_MATHFN (BUILT_IN_INF)
1912 CASE_MATHFN (BUILT_IN_IRINT)
1913 CASE_MATHFN (BUILT_IN_IROUND)
1914 CASE_MATHFN (BUILT_IN_ISINF)
1915 CASE_MATHFN (BUILT_IN_J0)
1916 CASE_MATHFN (BUILT_IN_J1)
1917 CASE_MATHFN (BUILT_IN_JN)
1918 CASE_MATHFN (BUILT_IN_LCEIL)
1919 CASE_MATHFN (BUILT_IN_LDEXP)
1920 CASE_MATHFN (BUILT_IN_LFLOOR)
1921 CASE_MATHFN (BUILT_IN_LGAMMA)
1922 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1923 CASE_MATHFN (BUILT_IN_LLCEIL)
1924 CASE_MATHFN (BUILT_IN_LLFLOOR)
1925 CASE_MATHFN (BUILT_IN_LLRINT)
1926 CASE_MATHFN (BUILT_IN_LLROUND)
1927 CASE_MATHFN (BUILT_IN_LOG)
1928 CASE_MATHFN (BUILT_IN_LOG10)
1929 CASE_MATHFN (BUILT_IN_LOG1P)
1930 CASE_MATHFN (BUILT_IN_LOG2)
1931 CASE_MATHFN (BUILT_IN_LOGB)
1932 CASE_MATHFN (BUILT_IN_LRINT)
1933 CASE_MATHFN (BUILT_IN_LROUND)
1934 CASE_MATHFN (BUILT_IN_MODF)
1935 CASE_MATHFN (BUILT_IN_NAN)
1936 CASE_MATHFN (BUILT_IN_NANS)
1937 CASE_MATHFN (BUILT_IN_NEARBYINT)
1938 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1939 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1940 CASE_MATHFN (BUILT_IN_POW)
1941 CASE_MATHFN (BUILT_IN_POWI)
1942 CASE_MATHFN (BUILT_IN_POW10)
1943 CASE_MATHFN (BUILT_IN_REMAINDER)
1944 CASE_MATHFN (BUILT_IN_REMQUO)
1945 CASE_MATHFN (BUILT_IN_RINT)
1946 CASE_MATHFN (BUILT_IN_ROUND)
1947 CASE_MATHFN (BUILT_IN_SCALB)
1948 CASE_MATHFN (BUILT_IN_SCALBLN)
1949 CASE_MATHFN (BUILT_IN_SCALBN)
1950 CASE_MATHFN (BUILT_IN_SIGNBIT)
1951 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1952 CASE_MATHFN (BUILT_IN_SIN)
1953 CASE_MATHFN (BUILT_IN_SINCOS)
1954 CASE_MATHFN (BUILT_IN_SINH)
1955 CASE_MATHFN (BUILT_IN_SQRT)
1956 CASE_MATHFN (BUILT_IN_TAN)
1957 CASE_MATHFN (BUILT_IN_TANH)
1958 CASE_MATHFN (BUILT_IN_TGAMMA)
1959 CASE_MATHFN (BUILT_IN_TRUNC)
1960 CASE_MATHFN (BUILT_IN_Y0)
1961 CASE_MATHFN (BUILT_IN_Y1)
1962 CASE_MATHFN (BUILT_IN_YN)
1964 default:
1965 return NULL_TREE;
1968 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1969 fcode2 = fcode;
1970 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1971 fcode2 = fcodef;
1972 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1973 fcode2 = fcodel;
1974 else
1975 return NULL_TREE;
1977 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1978 return NULL_TREE;
1980 return builtin_decl_explicit (fcode2);
1983 /* Like mathfn_built_in_1(), but always use the implicit array. */
1985 tree
1986 mathfn_built_in (tree type, enum built_in_function fn)
1988 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1991 /* If errno must be maintained, expand the RTL to check if the result,
1992 TARGET, of a built-in function call, EXP, is NaN, and if so set
1993 errno to EDOM. */
1995 static void
1996 expand_errno_check (tree exp, rtx target)
1998 rtx lab = gen_label_rtx ();
2000 /* Test the result; if it is NaN, set errno=EDOM because
2001 the argument was not in the domain. */
2002 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
2003 NULL_RTX, NULL_RTX, lab,
2004 /* The jump is very likely. */
2005 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
2007 #ifdef TARGET_EDOM
2008 /* If this built-in doesn't throw an exception, set errno directly. */
2009 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
2011 #ifdef GEN_ERRNO_RTX
2012 rtx errno_rtx = GEN_ERRNO_RTX;
2013 #else
2014 rtx errno_rtx
2015 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
2016 #endif
2017 emit_move_insn (errno_rtx,
2018 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
2019 emit_label (lab);
2020 return;
2022 #endif
2024 /* Make sure the library call isn't expanded as a tail call. */
2025 CALL_EXPR_TAILCALL (exp) = 0;
2027 /* We can't set errno=EDOM directly; let the library call do it.
2028 Pop the arguments right away in case the call gets deleted. */
2029 NO_DEFER_POP;
2030 expand_call (exp, target, 0);
2031 OK_DEFER_POP;
2032 emit_label (lab);
2035 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2036 Return NULL_RTX if a normal call should be emitted rather than expanding
2037 the function in-line. EXP is the expression that is a call to the builtin
2038 function; if convenient, the result should be placed in TARGET.
2039 SUBTARGET may be used as the target for computing one of EXP's operands. */
2041 static rtx
2042 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2044 optab builtin_optab;
2045 rtx op0, insns;
2046 tree fndecl = get_callee_fndecl (exp);
2047 enum machine_mode mode;
2048 bool errno_set = false;
2049 bool try_widening = false;
2050 tree arg;
2052 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2053 return NULL_RTX;
2055 arg = CALL_EXPR_ARG (exp, 0);
2057 switch (DECL_FUNCTION_CODE (fndecl))
2059 CASE_FLT_FN (BUILT_IN_SQRT):
2060 errno_set = ! tree_expr_nonnegative_p (arg);
2061 try_widening = true;
2062 builtin_optab = sqrt_optab;
2063 break;
2064 CASE_FLT_FN (BUILT_IN_EXP):
2065 errno_set = true; builtin_optab = exp_optab; break;
2066 CASE_FLT_FN (BUILT_IN_EXP10):
2067 CASE_FLT_FN (BUILT_IN_POW10):
2068 errno_set = true; builtin_optab = exp10_optab; break;
2069 CASE_FLT_FN (BUILT_IN_EXP2):
2070 errno_set = true; builtin_optab = exp2_optab; break;
2071 CASE_FLT_FN (BUILT_IN_EXPM1):
2072 errno_set = true; builtin_optab = expm1_optab; break;
2073 CASE_FLT_FN (BUILT_IN_LOGB):
2074 errno_set = true; builtin_optab = logb_optab; break;
2075 CASE_FLT_FN (BUILT_IN_LOG):
2076 errno_set = true; builtin_optab = log_optab; break;
2077 CASE_FLT_FN (BUILT_IN_LOG10):
2078 errno_set = true; builtin_optab = log10_optab; break;
2079 CASE_FLT_FN (BUILT_IN_LOG2):
2080 errno_set = true; builtin_optab = log2_optab; break;
2081 CASE_FLT_FN (BUILT_IN_LOG1P):
2082 errno_set = true; builtin_optab = log1p_optab; break;
2083 CASE_FLT_FN (BUILT_IN_ASIN):
2084 builtin_optab = asin_optab; break;
2085 CASE_FLT_FN (BUILT_IN_ACOS):
2086 builtin_optab = acos_optab; break;
2087 CASE_FLT_FN (BUILT_IN_TAN):
2088 builtin_optab = tan_optab; break;
2089 CASE_FLT_FN (BUILT_IN_ATAN):
2090 builtin_optab = atan_optab; break;
2091 CASE_FLT_FN (BUILT_IN_FLOOR):
2092 builtin_optab = floor_optab; break;
2093 CASE_FLT_FN (BUILT_IN_CEIL):
2094 builtin_optab = ceil_optab; break;
2095 CASE_FLT_FN (BUILT_IN_TRUNC):
2096 builtin_optab = btrunc_optab; break;
2097 CASE_FLT_FN (BUILT_IN_ROUND):
2098 builtin_optab = round_optab; break;
2099 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2100 builtin_optab = nearbyint_optab;
2101 if (flag_trapping_math)
2102 break;
2103 /* Else fallthrough and expand as rint. */
2104 CASE_FLT_FN (BUILT_IN_RINT):
2105 builtin_optab = rint_optab; break;
2106 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2107 builtin_optab = significand_optab; break;
2108 default:
2109 gcc_unreachable ();
2112 /* Make a suitable register to place result in. */
2113 mode = TYPE_MODE (TREE_TYPE (exp));
2115 if (! flag_errno_math || ! HONOR_NANS (mode))
2116 errno_set = false;
2118 /* Before working hard, check whether the instruction is available, but try
2119 to widen the mode for specific operations. */
2120 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2121 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2122 && (!errno_set || !optimize_insn_for_size_p ()))
2124 rtx result = gen_reg_rtx (mode);
2126 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2127 need to expand the argument again. This way, we will not perform
2128 side-effects more the once. */
2129 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2131 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2133 start_sequence ();
2135 /* Compute into RESULT.
2136 Set RESULT to wherever the result comes back. */
2137 result = expand_unop (mode, builtin_optab, op0, result, 0);
2139 if (result != 0)
2141 if (errno_set)
2142 expand_errno_check (exp, result);
2144 /* Output the entire sequence. */
2145 insns = get_insns ();
2146 end_sequence ();
2147 emit_insn (insns);
2148 return result;
2151 /* If we were unable to expand via the builtin, stop the sequence
2152 (without outputting the insns) and call to the library function
2153 with the stabilized argument list. */
2154 end_sequence ();
2157 return expand_call (exp, target, target == const0_rtx);
2160 /* Expand a call to the builtin binary math functions (pow and atan2).
2161 Return NULL_RTX if a normal call should be emitted rather than expanding the
2162 function in-line. EXP is the expression that is a call to the builtin
2163 function; if convenient, the result should be placed in TARGET.
2164 SUBTARGET may be used as the target for computing one of EXP's
2165 operands. */
2167 static rtx
2168 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2170 optab builtin_optab;
2171 rtx op0, op1, insns, result;
2172 int op1_type = REAL_TYPE;
2173 tree fndecl = get_callee_fndecl (exp);
2174 tree arg0, arg1;
2175 enum machine_mode mode;
2176 bool errno_set = true;
2178 switch (DECL_FUNCTION_CODE (fndecl))
2180 CASE_FLT_FN (BUILT_IN_SCALBN):
2181 CASE_FLT_FN (BUILT_IN_SCALBLN):
2182 CASE_FLT_FN (BUILT_IN_LDEXP):
2183 op1_type = INTEGER_TYPE;
2184 default:
2185 break;
2188 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2189 return NULL_RTX;
2191 arg0 = CALL_EXPR_ARG (exp, 0);
2192 arg1 = CALL_EXPR_ARG (exp, 1);
2194 switch (DECL_FUNCTION_CODE (fndecl))
2196 CASE_FLT_FN (BUILT_IN_POW):
2197 builtin_optab = pow_optab; break;
2198 CASE_FLT_FN (BUILT_IN_ATAN2):
2199 builtin_optab = atan2_optab; break;
2200 CASE_FLT_FN (BUILT_IN_SCALB):
2201 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2202 return 0;
2203 builtin_optab = scalb_optab; break;
2204 CASE_FLT_FN (BUILT_IN_SCALBN):
2205 CASE_FLT_FN (BUILT_IN_SCALBLN):
2206 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2207 return 0;
2208 /* Fall through... */
2209 CASE_FLT_FN (BUILT_IN_LDEXP):
2210 builtin_optab = ldexp_optab; break;
2211 CASE_FLT_FN (BUILT_IN_FMOD):
2212 builtin_optab = fmod_optab; break;
2213 CASE_FLT_FN (BUILT_IN_REMAINDER):
2214 CASE_FLT_FN (BUILT_IN_DREM):
2215 builtin_optab = remainder_optab; break;
2216 default:
2217 gcc_unreachable ();
2220 /* Make a suitable register to place result in. */
2221 mode = TYPE_MODE (TREE_TYPE (exp));
2223 /* Before working hard, check whether the instruction is available. */
2224 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2225 return NULL_RTX;
2227 result = gen_reg_rtx (mode);
2229 if (! flag_errno_math || ! HONOR_NANS (mode))
2230 errno_set = false;
2232 if (errno_set && optimize_insn_for_size_p ())
2233 return 0;
2235 /* Always stabilize the argument list. */
2236 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2237 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2239 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2240 op1 = expand_normal (arg1);
2242 start_sequence ();
2244 /* Compute into RESULT.
2245 Set RESULT to wherever the result comes back. */
2246 result = expand_binop (mode, builtin_optab, op0, op1,
2247 result, 0, OPTAB_DIRECT);
2249 /* If we were unable to expand via the builtin, stop the sequence
2250 (without outputting the insns) and call to the library function
2251 with the stabilized argument list. */
2252 if (result == 0)
2254 end_sequence ();
2255 return expand_call (exp, target, target == const0_rtx);
2258 if (errno_set)
2259 expand_errno_check (exp, result);
2261 /* Output the entire sequence. */
2262 insns = get_insns ();
2263 end_sequence ();
2264 emit_insn (insns);
2266 return result;
2269 /* Expand a call to the builtin trinary math functions (fma).
2270 Return NULL_RTX if a normal call should be emitted rather than expanding the
2271 function in-line. EXP is the expression that is a call to the builtin
2272 function; if convenient, the result should be placed in TARGET.
2273 SUBTARGET may be used as the target for computing one of EXP's
2274 operands. */
2276 static rtx
2277 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2279 optab builtin_optab;
2280 rtx op0, op1, op2, insns, result;
2281 tree fndecl = get_callee_fndecl (exp);
2282 tree arg0, arg1, arg2;
2283 enum machine_mode mode;
2285 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2286 return NULL_RTX;
2288 arg0 = CALL_EXPR_ARG (exp, 0);
2289 arg1 = CALL_EXPR_ARG (exp, 1);
2290 arg2 = CALL_EXPR_ARG (exp, 2);
2292 switch (DECL_FUNCTION_CODE (fndecl))
2294 CASE_FLT_FN (BUILT_IN_FMA):
2295 builtin_optab = fma_optab; break;
2296 default:
2297 gcc_unreachable ();
2300 /* Make a suitable register to place result in. */
2301 mode = TYPE_MODE (TREE_TYPE (exp));
2303 /* Before working hard, check whether the instruction is available. */
2304 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2305 return NULL_RTX;
2307 result = gen_reg_rtx (mode);
2309 /* Always stabilize the argument list. */
2310 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2311 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2312 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2314 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2315 op1 = expand_normal (arg1);
2316 op2 = expand_normal (arg2);
2318 start_sequence ();
2320 /* Compute into RESULT.
2321 Set RESULT to wherever the result comes back. */
2322 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2323 result, 0);
2325 /* If we were unable to expand via the builtin, stop the sequence
2326 (without outputting the insns) and call to the library function
2327 with the stabilized argument list. */
2328 if (result == 0)
2330 end_sequence ();
2331 return expand_call (exp, target, target == const0_rtx);
2334 /* Output the entire sequence. */
2335 insns = get_insns ();
2336 end_sequence ();
2337 emit_insn (insns);
2339 return result;
2342 /* Expand a call to the builtin sin and cos math functions.
2343 Return NULL_RTX if a normal call should be emitted rather than expanding the
2344 function in-line. EXP is the expression that is a call to the builtin
2345 function; if convenient, the result should be placed in TARGET.
2346 SUBTARGET may be used as the target for computing one of EXP's
2347 operands. */
2349 static rtx
2350 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2352 optab builtin_optab;
2353 rtx op0, insns;
2354 tree fndecl = get_callee_fndecl (exp);
2355 enum machine_mode mode;
2356 tree arg;
2358 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2359 return NULL_RTX;
2361 arg = CALL_EXPR_ARG (exp, 0);
2363 switch (DECL_FUNCTION_CODE (fndecl))
2365 CASE_FLT_FN (BUILT_IN_SIN):
2366 CASE_FLT_FN (BUILT_IN_COS):
2367 builtin_optab = sincos_optab; break;
2368 default:
2369 gcc_unreachable ();
2372 /* Make a suitable register to place result in. */
2373 mode = TYPE_MODE (TREE_TYPE (exp));
2375 /* Check if sincos insn is available, otherwise fallback
2376 to sin or cos insn. */
2377 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2378 switch (DECL_FUNCTION_CODE (fndecl))
2380 CASE_FLT_FN (BUILT_IN_SIN):
2381 builtin_optab = sin_optab; break;
2382 CASE_FLT_FN (BUILT_IN_COS):
2383 builtin_optab = cos_optab; break;
2384 default:
2385 gcc_unreachable ();
2388 /* Before working hard, check whether the instruction is available. */
2389 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2391 rtx result = gen_reg_rtx (mode);
2393 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2394 need to expand the argument again. This way, we will not perform
2395 side-effects more the once. */
2396 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2398 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2400 start_sequence ();
2402 /* Compute into RESULT.
2403 Set RESULT to wherever the result comes back. */
2404 if (builtin_optab == sincos_optab)
2406 int ok;
2408 switch (DECL_FUNCTION_CODE (fndecl))
2410 CASE_FLT_FN (BUILT_IN_SIN):
2411 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2412 break;
2413 CASE_FLT_FN (BUILT_IN_COS):
2414 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2415 break;
2416 default:
2417 gcc_unreachable ();
2419 gcc_assert (ok);
2421 else
2422 result = expand_unop (mode, builtin_optab, op0, result, 0);
2424 if (result != 0)
2426 /* Output the entire sequence. */
2427 insns = get_insns ();
2428 end_sequence ();
2429 emit_insn (insns);
2430 return result;
2433 /* If we were unable to expand via the builtin, stop the sequence
2434 (without outputting the insns) and call to the library function
2435 with the stabilized argument list. */
2436 end_sequence ();
2439 return expand_call (exp, target, target == const0_rtx);
2442 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2443 return an RTL instruction code that implements the functionality.
2444 If that isn't possible or available return CODE_FOR_nothing. */
2446 static enum insn_code
2447 interclass_mathfn_icode (tree arg, tree fndecl)
2449 bool errno_set = false;
2450 optab builtin_optab = unknown_optab;
2451 enum machine_mode mode;
2453 switch (DECL_FUNCTION_CODE (fndecl))
2455 CASE_FLT_FN (BUILT_IN_ILOGB):
2456 errno_set = true; builtin_optab = ilogb_optab; break;
2457 CASE_FLT_FN (BUILT_IN_ISINF):
2458 builtin_optab = isinf_optab; break;
2459 case BUILT_IN_ISNORMAL:
2460 case BUILT_IN_ISFINITE:
2461 CASE_FLT_FN (BUILT_IN_FINITE):
2462 case BUILT_IN_FINITED32:
2463 case BUILT_IN_FINITED64:
2464 case BUILT_IN_FINITED128:
2465 case BUILT_IN_ISINFD32:
2466 case BUILT_IN_ISINFD64:
2467 case BUILT_IN_ISINFD128:
2468 /* These builtins have no optabs (yet). */
2469 break;
2470 default:
2471 gcc_unreachable ();
2474 /* There's no easy way to detect the case we need to set EDOM. */
2475 if (flag_errno_math && errno_set)
2476 return CODE_FOR_nothing;
2478 /* Optab mode depends on the mode of the input argument. */
2479 mode = TYPE_MODE (TREE_TYPE (arg));
2481 if (builtin_optab)
2482 return optab_handler (builtin_optab, mode);
2483 return CODE_FOR_nothing;
2486 /* Expand a call to one of the builtin math functions that operate on
2487 floating point argument and output an integer result (ilogb, isinf,
2488 isnan, etc).
2489 Return 0 if a normal call should be emitted rather than expanding the
2490 function in-line. EXP is the expression that is a call to the builtin
2491 function; if convenient, the result should be placed in TARGET. */
2493 static rtx
2494 expand_builtin_interclass_mathfn (tree exp, rtx target)
2496 enum insn_code icode = CODE_FOR_nothing;
2497 rtx op0;
2498 tree fndecl = get_callee_fndecl (exp);
2499 enum machine_mode mode;
2500 tree arg;
2502 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2503 return NULL_RTX;
2505 arg = CALL_EXPR_ARG (exp, 0);
2506 icode = interclass_mathfn_icode (arg, fndecl);
2507 mode = TYPE_MODE (TREE_TYPE (arg));
2509 if (icode != CODE_FOR_nothing)
2511 struct expand_operand ops[1];
2512 rtx last = get_last_insn ();
2513 tree orig_arg = arg;
2515 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2516 need to expand the argument again. This way, we will not perform
2517 side-effects more the once. */
2518 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2520 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2522 if (mode != GET_MODE (op0))
2523 op0 = convert_to_mode (mode, op0, 0);
2525 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2526 if (maybe_legitimize_operands (icode, 0, 1, ops)
2527 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2528 return ops[0].value;
2530 delete_insns_since (last);
2531 CALL_EXPR_ARG (exp, 0) = orig_arg;
2534 return NULL_RTX;
2537 /* Expand a call to the builtin sincos math function.
2538 Return NULL_RTX if a normal call should be emitted rather than expanding the
2539 function in-line. EXP is the expression that is a call to the builtin
2540 function. */
2542 static rtx
2543 expand_builtin_sincos (tree exp)
2545 rtx op0, op1, op2, target1, target2;
2546 enum machine_mode mode;
2547 tree arg, sinp, cosp;
2548 int result;
2549 location_t loc = EXPR_LOCATION (exp);
2550 tree alias_type, alias_off;
2552 if (!validate_arglist (exp, REAL_TYPE,
2553 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2554 return NULL_RTX;
2556 arg = CALL_EXPR_ARG (exp, 0);
2557 sinp = CALL_EXPR_ARG (exp, 1);
2558 cosp = CALL_EXPR_ARG (exp, 2);
2560 /* Make a suitable register to place result in. */
2561 mode = TYPE_MODE (TREE_TYPE (arg));
2563 /* Check if sincos insn is available, otherwise emit the call. */
2564 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2565 return NULL_RTX;
2567 target1 = gen_reg_rtx (mode);
2568 target2 = gen_reg_rtx (mode);
2570 op0 = expand_normal (arg);
2571 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2572 alias_off = build_int_cst (alias_type, 0);
2573 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2574 sinp, alias_off));
2575 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2576 cosp, alias_off));
2578 /* Compute into target1 and target2.
2579 Set TARGET to wherever the result comes back. */
2580 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2581 gcc_assert (result);
2583 /* Move target1 and target2 to the memory locations indicated
2584 by op1 and op2. */
2585 emit_move_insn (op1, target1);
2586 emit_move_insn (op2, target2);
2588 return const0_rtx;
2591 /* Expand a call to the internal cexpi builtin to the sincos math function.
2592 EXP is the expression that is a call to the builtin function; if convenient,
2593 the result should be placed in TARGET. */
2595 static rtx
2596 expand_builtin_cexpi (tree exp, rtx target)
2598 tree fndecl = get_callee_fndecl (exp);
2599 tree arg, type;
2600 enum machine_mode mode;
2601 rtx op0, op1, op2;
2602 location_t loc = EXPR_LOCATION (exp);
2604 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2605 return NULL_RTX;
2607 arg = CALL_EXPR_ARG (exp, 0);
2608 type = TREE_TYPE (arg);
2609 mode = TYPE_MODE (TREE_TYPE (arg));
2611 /* Try expanding via a sincos optab, fall back to emitting a libcall
2612 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2613 is only generated from sincos, cexp or if we have either of them. */
2614 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2616 op1 = gen_reg_rtx (mode);
2617 op2 = gen_reg_rtx (mode);
2619 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2621 /* Compute into op1 and op2. */
2622 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2624 else if (targetm.libc_has_function (function_sincos))
2626 tree call, fn = NULL_TREE;
2627 tree top1, top2;
2628 rtx op1a, op2a;
2630 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2631 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2632 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2633 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2634 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2635 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2636 else
2637 gcc_unreachable ();
2639 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2640 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2641 op1a = copy_addr_to_reg (XEXP (op1, 0));
2642 op2a = copy_addr_to_reg (XEXP (op2, 0));
2643 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2644 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2646 /* Make sure not to fold the sincos call again. */
2647 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2648 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2649 call, 3, arg, top1, top2));
2651 else
2653 tree call, fn = NULL_TREE, narg;
2654 tree ctype = build_complex_type (type);
2656 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2657 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2658 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2659 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2660 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2661 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2662 else
2663 gcc_unreachable ();
2665 /* If we don't have a decl for cexp create one. This is the
2666 friendliest fallback if the user calls __builtin_cexpi
2667 without full target C99 function support. */
2668 if (fn == NULL_TREE)
2670 tree fntype;
2671 const char *name = NULL;
2673 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2674 name = "cexpf";
2675 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2676 name = "cexp";
2677 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2678 name = "cexpl";
2680 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2681 fn = build_fn_decl (name, fntype);
2684 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2685 build_real (type, dconst0), arg);
2687 /* Make sure not to fold the cexp call again. */
2688 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2689 return expand_expr (build_call_nary (ctype, call, 1, narg),
2690 target, VOIDmode, EXPAND_NORMAL);
2693 /* Now build the proper return type. */
2694 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2695 make_tree (TREE_TYPE (arg), op2),
2696 make_tree (TREE_TYPE (arg), op1)),
2697 target, VOIDmode, EXPAND_NORMAL);
2700 /* Conveniently construct a function call expression. FNDECL names the
2701 function to be called, N is the number of arguments, and the "..."
2702 parameters are the argument expressions. Unlike build_call_exr
2703 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2705 static tree
2706 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2708 va_list ap;
2709 tree fntype = TREE_TYPE (fndecl);
2710 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2712 va_start (ap, n);
2713 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2714 va_end (ap);
2715 SET_EXPR_LOCATION (fn, loc);
2716 return fn;
2719 /* Expand a call to one of the builtin rounding functions gcc defines
2720 as an extension (lfloor and lceil). As these are gcc extensions we
2721 do not need to worry about setting errno to EDOM.
2722 If expanding via optab fails, lower expression to (int)(floor(x)).
2723 EXP is the expression that is a call to the builtin function;
2724 if convenient, the result should be placed in TARGET. */
2726 static rtx
2727 expand_builtin_int_roundingfn (tree exp, rtx target)
2729 convert_optab builtin_optab;
2730 rtx op0, insns, tmp;
2731 tree fndecl = get_callee_fndecl (exp);
2732 enum built_in_function fallback_fn;
2733 tree fallback_fndecl;
2734 enum machine_mode mode;
2735 tree arg;
2737 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2738 gcc_unreachable ();
2740 arg = CALL_EXPR_ARG (exp, 0);
2742 switch (DECL_FUNCTION_CODE (fndecl))
2744 CASE_FLT_FN (BUILT_IN_ICEIL):
2745 CASE_FLT_FN (BUILT_IN_LCEIL):
2746 CASE_FLT_FN (BUILT_IN_LLCEIL):
2747 builtin_optab = lceil_optab;
2748 fallback_fn = BUILT_IN_CEIL;
2749 break;
2751 CASE_FLT_FN (BUILT_IN_IFLOOR):
2752 CASE_FLT_FN (BUILT_IN_LFLOOR):
2753 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2754 builtin_optab = lfloor_optab;
2755 fallback_fn = BUILT_IN_FLOOR;
2756 break;
2758 default:
2759 gcc_unreachable ();
2762 /* Make a suitable register to place result in. */
2763 mode = TYPE_MODE (TREE_TYPE (exp));
2765 target = gen_reg_rtx (mode);
2767 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2768 need to expand the argument again. This way, we will not perform
2769 side-effects more the once. */
2770 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2772 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2774 start_sequence ();
2776 /* Compute into TARGET. */
2777 if (expand_sfix_optab (target, op0, builtin_optab))
2779 /* Output the entire sequence. */
2780 insns = get_insns ();
2781 end_sequence ();
2782 emit_insn (insns);
2783 return target;
2786 /* If we were unable to expand via the builtin, stop the sequence
2787 (without outputting the insns). */
2788 end_sequence ();
2790 /* Fall back to floating point rounding optab. */
2791 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2793 /* For non-C99 targets we may end up without a fallback fndecl here
2794 if the user called __builtin_lfloor directly. In this case emit
2795 a call to the floor/ceil variants nevertheless. This should result
2796 in the best user experience for not full C99 targets. */
2797 if (fallback_fndecl == NULL_TREE)
2799 tree fntype;
2800 const char *name = NULL;
2802 switch (DECL_FUNCTION_CODE (fndecl))
2804 case BUILT_IN_ICEIL:
2805 case BUILT_IN_LCEIL:
2806 case BUILT_IN_LLCEIL:
2807 name = "ceil";
2808 break;
2809 case BUILT_IN_ICEILF:
2810 case BUILT_IN_LCEILF:
2811 case BUILT_IN_LLCEILF:
2812 name = "ceilf";
2813 break;
2814 case BUILT_IN_ICEILL:
2815 case BUILT_IN_LCEILL:
2816 case BUILT_IN_LLCEILL:
2817 name = "ceill";
2818 break;
2819 case BUILT_IN_IFLOOR:
2820 case BUILT_IN_LFLOOR:
2821 case BUILT_IN_LLFLOOR:
2822 name = "floor";
2823 break;
2824 case BUILT_IN_IFLOORF:
2825 case BUILT_IN_LFLOORF:
2826 case BUILT_IN_LLFLOORF:
2827 name = "floorf";
2828 break;
2829 case BUILT_IN_IFLOORL:
2830 case BUILT_IN_LFLOORL:
2831 case BUILT_IN_LLFLOORL:
2832 name = "floorl";
2833 break;
2834 default:
2835 gcc_unreachable ();
2838 fntype = build_function_type_list (TREE_TYPE (arg),
2839 TREE_TYPE (arg), NULL_TREE);
2840 fallback_fndecl = build_fn_decl (name, fntype);
2843 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2845 tmp = expand_normal (exp);
2846 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2848 /* Truncate the result of floating point optab to integer
2849 via expand_fix (). */
2850 target = gen_reg_rtx (mode);
2851 expand_fix (target, tmp, 0);
2853 return target;
2856 /* Expand a call to one of the builtin math functions doing integer
2857 conversion (lrint).
2858 Return 0 if a normal call should be emitted rather than expanding the
2859 function in-line. EXP is the expression that is a call to the builtin
2860 function; if convenient, the result should be placed in TARGET. */
2862 static rtx
2863 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2865 convert_optab builtin_optab;
2866 rtx op0, insns;
2867 tree fndecl = get_callee_fndecl (exp);
2868 tree arg;
2869 enum machine_mode mode;
2870 enum built_in_function fallback_fn = BUILT_IN_NONE;
2872 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2873 gcc_unreachable ();
2875 arg = CALL_EXPR_ARG (exp, 0);
2877 switch (DECL_FUNCTION_CODE (fndecl))
2879 CASE_FLT_FN (BUILT_IN_IRINT):
2880 fallback_fn = BUILT_IN_LRINT;
2881 /* FALLTHRU */
2882 CASE_FLT_FN (BUILT_IN_LRINT):
2883 CASE_FLT_FN (BUILT_IN_LLRINT):
2884 builtin_optab = lrint_optab;
2885 break;
2887 CASE_FLT_FN (BUILT_IN_IROUND):
2888 fallback_fn = BUILT_IN_LROUND;
2889 /* FALLTHRU */
2890 CASE_FLT_FN (BUILT_IN_LROUND):
2891 CASE_FLT_FN (BUILT_IN_LLROUND):
2892 builtin_optab = lround_optab;
2893 break;
2895 default:
2896 gcc_unreachable ();
2899 /* There's no easy way to detect the case we need to set EDOM. */
2900 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2901 return NULL_RTX;
2903 /* Make a suitable register to place result in. */
2904 mode = TYPE_MODE (TREE_TYPE (exp));
2906 /* There's no easy way to detect the case we need to set EDOM. */
2907 if (!flag_errno_math)
2909 rtx result = gen_reg_rtx (mode);
2911 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2912 need to expand the argument again. This way, we will not perform
2913 side-effects more the once. */
2914 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2916 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2918 start_sequence ();
2920 if (expand_sfix_optab (result, op0, builtin_optab))
2922 /* Output the entire sequence. */
2923 insns = get_insns ();
2924 end_sequence ();
2925 emit_insn (insns);
2926 return result;
2929 /* If we were unable to expand via the builtin, stop the sequence
2930 (without outputting the insns) and call to the library function
2931 with the stabilized argument list. */
2932 end_sequence ();
2935 if (fallback_fn != BUILT_IN_NONE)
2937 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2938 targets, (int) round (x) should never be transformed into
2939 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2940 a call to lround in the hope that the target provides at least some
2941 C99 functions. This should result in the best user experience for
2942 not full C99 targets. */
2943 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2944 fallback_fn, 0);
2946 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2947 fallback_fndecl, 1, arg);
2949 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2950 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2951 return convert_to_mode (mode, target, 0);
2954 return expand_call (exp, target, target == const0_rtx);
2957 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2958 a normal call should be emitted rather than expanding the function
2959 in-line. EXP is the expression that is a call to the builtin
2960 function; if convenient, the result should be placed in TARGET. */
2962 static rtx
2963 expand_builtin_powi (tree exp, rtx target)
2965 tree arg0, arg1;
2966 rtx op0, op1;
2967 enum machine_mode mode;
2968 enum machine_mode mode2;
2970 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2971 return NULL_RTX;
2973 arg0 = CALL_EXPR_ARG (exp, 0);
2974 arg1 = CALL_EXPR_ARG (exp, 1);
2975 mode = TYPE_MODE (TREE_TYPE (exp));
2977 /* Emit a libcall to libgcc. */
2979 /* Mode of the 2nd argument must match that of an int. */
2980 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2982 if (target == NULL_RTX)
2983 target = gen_reg_rtx (mode);
2985 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2986 if (GET_MODE (op0) != mode)
2987 op0 = convert_to_mode (mode, op0, 0);
2988 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2989 if (GET_MODE (op1) != mode2)
2990 op1 = convert_to_mode (mode2, op1, 0);
2992 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2993 target, LCT_CONST, mode, 2,
2994 op0, mode, op1, mode2);
2996 return target;
2999 /* Expand expression EXP which is a call to the strlen builtin. Return
3000 NULL_RTX if we failed the caller should emit a normal call, otherwise
3001 try to get the result in TARGET, if convenient. */
3003 static rtx
3004 expand_builtin_strlen (tree exp, rtx target,
3005 enum machine_mode target_mode)
3007 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3008 return NULL_RTX;
3009 else
3011 struct expand_operand ops[4];
3012 rtx pat;
3013 tree len;
3014 tree src = CALL_EXPR_ARG (exp, 0);
3015 rtx src_reg, before_strlen;
3016 enum machine_mode insn_mode = target_mode;
3017 enum insn_code icode = CODE_FOR_nothing;
3018 unsigned int align;
3020 /* If the length can be computed at compile-time, return it. */
3021 len = c_strlen (src, 0);
3022 if (len)
3023 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3025 /* If the length can be computed at compile-time and is constant
3026 integer, but there are side-effects in src, evaluate
3027 src for side-effects, then return len.
3028 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3029 can be optimized into: i++; x = 3; */
3030 len = c_strlen (src, 1);
3031 if (len && TREE_CODE (len) == INTEGER_CST)
3033 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3034 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3037 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3039 /* If SRC is not a pointer type, don't do this operation inline. */
3040 if (align == 0)
3041 return NULL_RTX;
3043 /* Bail out if we can't compute strlen in the right mode. */
3044 while (insn_mode != VOIDmode)
3046 icode = optab_handler (strlen_optab, insn_mode);
3047 if (icode != CODE_FOR_nothing)
3048 break;
3050 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3052 if (insn_mode == VOIDmode)
3053 return NULL_RTX;
3055 /* Make a place to hold the source address. We will not expand
3056 the actual source until we are sure that the expansion will
3057 not fail -- there are trees that cannot be expanded twice. */
3058 src_reg = gen_reg_rtx (Pmode);
3060 /* Mark the beginning of the strlen sequence so we can emit the
3061 source operand later. */
3062 before_strlen = get_last_insn ();
3064 create_output_operand (&ops[0], target, insn_mode);
3065 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3066 create_integer_operand (&ops[2], 0);
3067 create_integer_operand (&ops[3], align);
3068 if (!maybe_expand_insn (icode, 4, ops))
3069 return NULL_RTX;
3071 /* Now that we are assured of success, expand the source. */
3072 start_sequence ();
3073 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3074 if (pat != src_reg)
3076 #ifdef POINTERS_EXTEND_UNSIGNED
3077 if (GET_MODE (pat) != Pmode)
3078 pat = convert_to_mode (Pmode, pat,
3079 POINTERS_EXTEND_UNSIGNED);
3080 #endif
3081 emit_move_insn (src_reg, pat);
3083 pat = get_insns ();
3084 end_sequence ();
3086 if (before_strlen)
3087 emit_insn_after (pat, before_strlen);
3088 else
3089 emit_insn_before (pat, get_insns ());
3091 /* Return the value in the proper mode for this function. */
3092 if (GET_MODE (ops[0].value) == target_mode)
3093 target = ops[0].value;
3094 else if (target != 0)
3095 convert_move (target, ops[0].value, 0);
3096 else
3097 target = convert_to_mode (target_mode, ops[0].value, 0);
3099 return target;
3103 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3104 bytes from constant string DATA + OFFSET and return it as target
3105 constant. */
3107 static rtx
3108 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3109 enum machine_mode mode)
3111 const char *str = (const char *) data;
3113 gcc_assert (offset >= 0
3114 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3115 <= strlen (str) + 1));
3117 return c_readstr (str + offset, mode);
3120 /* LEN specify length of the block of memcpy/memset operation.
3121 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3122 In some cases we can make very likely guess on max size, then we
3123 set it into PROBABLE_MAX_SIZE. */
3125 static void
3126 determine_block_size (tree len, rtx len_rtx,
3127 unsigned HOST_WIDE_INT *min_size,
3128 unsigned HOST_WIDE_INT *max_size,
3129 unsigned HOST_WIDE_INT *probable_max_size)
3131 if (CONST_INT_P (len_rtx))
3133 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3134 return;
3136 else
3138 wide_int min, max;
3139 enum value_range_type range_type = VR_UNDEFINED;
3141 /* Determine bounds from the type. */
3142 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3143 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3144 else
3145 *min_size = 0;
3146 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3147 *probable_max_size = *max_size
3148 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3149 else
3150 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3152 if (TREE_CODE (len) == SSA_NAME)
3153 range_type = get_range_info (len, &min, &max);
3154 if (range_type == VR_RANGE)
3156 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3157 *min_size = min.to_uhwi ();
3158 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3159 *probable_max_size = *max_size = max.to_uhwi ();
3161 else if (range_type == VR_ANTI_RANGE)
3163 /* Anti range 0...N lets us to determine minimal size to N+1. */
3164 if (min == 0)
3166 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
3167 *min_size = max.to_uhwi () + 1;
3169 /* Code like
3171 int n;
3172 if (n < 100)
3173 memcpy (a, b, n)
3175 Produce anti range allowing negative values of N. We still
3176 can use the information and make a guess that N is not negative.
3178 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3179 *probable_max_size = min.to_uhwi () - 1;
3182 gcc_checking_assert (*max_size <=
3183 (unsigned HOST_WIDE_INT)
3184 GET_MODE_MASK (GET_MODE (len_rtx)));
3187 /* Expand a call EXP to the memcpy builtin.
3188 Return NULL_RTX if we failed, the caller should emit a normal call,
3189 otherwise try to get the result in TARGET, if convenient (and in
3190 mode MODE if that's convenient). */
3192 static rtx
3193 expand_builtin_memcpy (tree exp, rtx target)
3195 if (!validate_arglist (exp,
3196 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3197 return NULL_RTX;
3198 else
3200 tree dest = CALL_EXPR_ARG (exp, 0);
3201 tree src = CALL_EXPR_ARG (exp, 1);
3202 tree len = CALL_EXPR_ARG (exp, 2);
3203 const char *src_str;
3204 unsigned int src_align = get_pointer_alignment (src);
3205 unsigned int dest_align = get_pointer_alignment (dest);
3206 rtx dest_mem, src_mem, dest_addr, len_rtx;
3207 HOST_WIDE_INT expected_size = -1;
3208 unsigned int expected_align = 0;
3209 unsigned HOST_WIDE_INT min_size;
3210 unsigned HOST_WIDE_INT max_size;
3211 unsigned HOST_WIDE_INT probable_max_size;
3213 /* If DEST is not a pointer type, call the normal function. */
3214 if (dest_align == 0)
3215 return NULL_RTX;
3217 /* If either SRC is not a pointer type, don't do this
3218 operation in-line. */
3219 if (src_align == 0)
3220 return NULL_RTX;
3222 if (currently_expanding_gimple_stmt)
3223 stringop_block_profile (currently_expanding_gimple_stmt,
3224 &expected_align, &expected_size);
3226 if (expected_align < dest_align)
3227 expected_align = dest_align;
3228 dest_mem = get_memory_rtx (dest, len);
3229 set_mem_align (dest_mem, dest_align);
3230 len_rtx = expand_normal (len);
3231 determine_block_size (len, len_rtx, &min_size, &max_size,
3232 &probable_max_size);
3233 src_str = c_getstr (src);
3235 /* If SRC is a string constant and block move would be done
3236 by pieces, we can avoid loading the string from memory
3237 and only stored the computed constants. */
3238 if (src_str
3239 && CONST_INT_P (len_rtx)
3240 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3241 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3242 CONST_CAST (char *, src_str),
3243 dest_align, false))
3245 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3246 builtin_memcpy_read_str,
3247 CONST_CAST (char *, src_str),
3248 dest_align, false, 0);
3249 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3250 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3251 return dest_mem;
3254 src_mem = get_memory_rtx (src, len);
3255 set_mem_align (src_mem, src_align);
3257 /* Copy word part most expediently. */
3258 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3259 CALL_EXPR_TAILCALL (exp)
3260 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3261 expected_align, expected_size,
3262 min_size, max_size, probable_max_size);
3264 if (dest_addr == 0)
3266 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3267 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3269 return dest_addr;
3273 /* Expand a call EXP to the mempcpy builtin.
3274 Return NULL_RTX if we failed; the caller should emit a normal call,
3275 otherwise try to get the result in TARGET, if convenient (and in
3276 mode MODE if that's convenient). If ENDP is 0 return the
3277 destination pointer, if ENDP is 1 return the end pointer ala
3278 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3279 stpcpy. */
3281 static rtx
3282 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3284 if (!validate_arglist (exp,
3285 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3286 return NULL_RTX;
3287 else
3289 tree dest = CALL_EXPR_ARG (exp, 0);
3290 tree src = CALL_EXPR_ARG (exp, 1);
3291 tree len = CALL_EXPR_ARG (exp, 2);
3292 return expand_builtin_mempcpy_args (dest, src, len,
3293 target, mode, /*endp=*/ 1);
3297 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3298 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3299 so that this can also be called without constructing an actual CALL_EXPR.
3300 The other arguments and return value are the same as for
3301 expand_builtin_mempcpy. */
3303 static rtx
3304 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3305 rtx target, enum machine_mode mode, int endp)
3307 /* If return value is ignored, transform mempcpy into memcpy. */
3308 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3310 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3311 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3312 dest, src, len);
3313 return expand_expr (result, target, mode, EXPAND_NORMAL);
3315 else
3317 const char *src_str;
3318 unsigned int src_align = get_pointer_alignment (src);
3319 unsigned int dest_align = get_pointer_alignment (dest);
3320 rtx dest_mem, src_mem, len_rtx;
3322 /* If either SRC or DEST is not a pointer type, don't do this
3323 operation in-line. */
3324 if (dest_align == 0 || src_align == 0)
3325 return NULL_RTX;
3327 /* If LEN is not constant, call the normal function. */
3328 if (! tree_fits_uhwi_p (len))
3329 return NULL_RTX;
3331 len_rtx = expand_normal (len);
3332 src_str = c_getstr (src);
3334 /* If SRC is a string constant and block move would be done
3335 by pieces, we can avoid loading the string from memory
3336 and only stored the computed constants. */
3337 if (src_str
3338 && CONST_INT_P (len_rtx)
3339 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3340 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3341 CONST_CAST (char *, src_str),
3342 dest_align, false))
3344 dest_mem = get_memory_rtx (dest, len);
3345 set_mem_align (dest_mem, dest_align);
3346 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3347 builtin_memcpy_read_str,
3348 CONST_CAST (char *, src_str),
3349 dest_align, false, endp);
3350 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3351 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3352 return dest_mem;
3355 if (CONST_INT_P (len_rtx)
3356 && can_move_by_pieces (INTVAL (len_rtx),
3357 MIN (dest_align, src_align)))
3359 dest_mem = get_memory_rtx (dest, len);
3360 set_mem_align (dest_mem, dest_align);
3361 src_mem = get_memory_rtx (src, len);
3362 set_mem_align (src_mem, src_align);
3363 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3364 MIN (dest_align, src_align), endp);
3365 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3366 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3367 return dest_mem;
3370 return NULL_RTX;
3374 #ifndef HAVE_movstr
3375 # define HAVE_movstr 0
3376 # define CODE_FOR_movstr CODE_FOR_nothing
3377 #endif
3379 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3380 we failed, the caller should emit a normal call, otherwise try to
3381 get the result in TARGET, if convenient. If ENDP is 0 return the
3382 destination pointer, if ENDP is 1 return the end pointer ala
3383 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3384 stpcpy. */
3386 static rtx
3387 expand_movstr (tree dest, tree src, rtx target, int endp)
3389 struct expand_operand ops[3];
3390 rtx dest_mem;
3391 rtx src_mem;
3393 if (!HAVE_movstr)
3394 return NULL_RTX;
3396 dest_mem = get_memory_rtx (dest, NULL);
3397 src_mem = get_memory_rtx (src, NULL);
3398 if (!endp)
3400 target = force_reg (Pmode, XEXP (dest_mem, 0));
3401 dest_mem = replace_equiv_address (dest_mem, target);
3404 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3405 create_fixed_operand (&ops[1], dest_mem);
3406 create_fixed_operand (&ops[2], src_mem);
3407 if (!maybe_expand_insn (CODE_FOR_movstr, 3, ops))
3408 return NULL_RTX;
3410 if (endp && target != const0_rtx)
3412 target = ops[0].value;
3413 /* movstr is supposed to set end to the address of the NUL
3414 terminator. If the caller requested a mempcpy-like return value,
3415 adjust it. */
3416 if (endp == 1)
3418 rtx tem = plus_constant (GET_MODE (target),
3419 gen_lowpart (GET_MODE (target), target), 1);
3420 emit_move_insn (target, force_operand (tem, NULL_RTX));
3423 return target;
3426 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3427 NULL_RTX if we failed the caller should emit a normal call, otherwise
3428 try to get the result in TARGET, if convenient (and in mode MODE if that's
3429 convenient). */
3431 static rtx
3432 expand_builtin_strcpy (tree exp, rtx target)
3434 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3436 tree dest = CALL_EXPR_ARG (exp, 0);
3437 tree src = CALL_EXPR_ARG (exp, 1);
3438 return expand_builtin_strcpy_args (dest, src, target);
3440 return NULL_RTX;
3443 /* Helper function to do the actual work for expand_builtin_strcpy. The
3444 arguments to the builtin_strcpy call DEST and SRC are broken out
3445 so that this can also be called without constructing an actual CALL_EXPR.
3446 The other arguments and return value are the same as for
3447 expand_builtin_strcpy. */
3449 static rtx
3450 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3452 return expand_movstr (dest, src, target, /*endp=*/0);
3455 /* Expand a call EXP to the stpcpy builtin.
3456 Return NULL_RTX if we failed the caller should emit a normal call,
3457 otherwise try to get the result in TARGET, if convenient (and in
3458 mode MODE if that's convenient). */
3460 static rtx
3461 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3463 tree dst, src;
3464 location_t loc = EXPR_LOCATION (exp);
3466 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3467 return NULL_RTX;
3469 dst = CALL_EXPR_ARG (exp, 0);
3470 src = CALL_EXPR_ARG (exp, 1);
3472 /* If return value is ignored, transform stpcpy into strcpy. */
3473 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3475 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3476 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3477 return expand_expr (result, target, mode, EXPAND_NORMAL);
3479 else
3481 tree len, lenp1;
3482 rtx ret;
3484 /* Ensure we get an actual string whose length can be evaluated at
3485 compile-time, not an expression containing a string. This is
3486 because the latter will potentially produce pessimized code
3487 when used to produce the return value. */
3488 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3489 return expand_movstr (dst, src, target, /*endp=*/2);
3491 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3492 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3493 target, mode, /*endp=*/2);
3495 if (ret)
3496 return ret;
3498 if (TREE_CODE (len) == INTEGER_CST)
3500 rtx len_rtx = expand_normal (len);
3502 if (CONST_INT_P (len_rtx))
3504 ret = expand_builtin_strcpy_args (dst, src, target);
3506 if (ret)
3508 if (! target)
3510 if (mode != VOIDmode)
3511 target = gen_reg_rtx (mode);
3512 else
3513 target = gen_reg_rtx (GET_MODE (ret));
3515 if (GET_MODE (target) != GET_MODE (ret))
3516 ret = gen_lowpart (GET_MODE (target), ret);
3518 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3519 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3520 gcc_assert (ret);
3522 return target;
3527 return expand_movstr (dst, src, target, /*endp=*/2);
3531 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3532 bytes from constant string DATA + OFFSET and return it as target
3533 constant. */
3536 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3537 enum machine_mode mode)
3539 const char *str = (const char *) data;
3541 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3542 return const0_rtx;
3544 return c_readstr (str + offset, mode);
3547 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3548 NULL_RTX if we failed the caller should emit a normal call. */
3550 static rtx
3551 expand_builtin_strncpy (tree exp, rtx target)
3553 location_t loc = EXPR_LOCATION (exp);
3555 if (validate_arglist (exp,
3556 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3558 tree dest = CALL_EXPR_ARG (exp, 0);
3559 tree src = CALL_EXPR_ARG (exp, 1);
3560 tree len = CALL_EXPR_ARG (exp, 2);
3561 tree slen = c_strlen (src, 1);
3563 /* We must be passed a constant len and src parameter. */
3564 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3565 return NULL_RTX;
3567 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3569 /* We're required to pad with trailing zeros if the requested
3570 len is greater than strlen(s2)+1. In that case try to
3571 use store_by_pieces, if it fails, punt. */
3572 if (tree_int_cst_lt (slen, len))
3574 unsigned int dest_align = get_pointer_alignment (dest);
3575 const char *p = c_getstr (src);
3576 rtx dest_mem;
3578 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3579 || !can_store_by_pieces (tree_to_uhwi (len),
3580 builtin_strncpy_read_str,
3581 CONST_CAST (char *, p),
3582 dest_align, false))
3583 return NULL_RTX;
3585 dest_mem = get_memory_rtx (dest, len);
3586 store_by_pieces (dest_mem, tree_to_uhwi (len),
3587 builtin_strncpy_read_str,
3588 CONST_CAST (char *, p), dest_align, false, 0);
3589 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3590 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3591 return dest_mem;
3594 return NULL_RTX;
3597 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3598 bytes from constant string DATA + OFFSET and return it as target
3599 constant. */
3602 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3603 enum machine_mode mode)
3605 const char *c = (const char *) data;
3606 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3608 memset (p, *c, GET_MODE_SIZE (mode));
3610 return c_readstr (p, mode);
3613 /* Callback routine for store_by_pieces. Return the RTL of a register
3614 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3615 char value given in the RTL register data. For example, if mode is
3616 4 bytes wide, return the RTL for 0x01010101*data. */
3618 static rtx
3619 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3620 enum machine_mode mode)
3622 rtx target, coeff;
3623 size_t size;
3624 char *p;
3626 size = GET_MODE_SIZE (mode);
3627 if (size == 1)
3628 return (rtx) data;
3630 p = XALLOCAVEC (char, size);
3631 memset (p, 1, size);
3632 coeff = c_readstr (p, mode);
3634 target = convert_to_mode (mode, (rtx) data, 1);
3635 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3636 return force_reg (mode, target);
3639 /* Expand expression EXP, which is a call to the memset builtin. Return
3640 NULL_RTX if we failed the caller should emit a normal call, otherwise
3641 try to get the result in TARGET, if convenient (and in mode MODE if that's
3642 convenient). */
3644 static rtx
3645 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3647 if (!validate_arglist (exp,
3648 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3649 return NULL_RTX;
3650 else
3652 tree dest = CALL_EXPR_ARG (exp, 0);
3653 tree val = CALL_EXPR_ARG (exp, 1);
3654 tree len = CALL_EXPR_ARG (exp, 2);
3655 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3659 /* Helper function to do the actual work for expand_builtin_memset. The
3660 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3661 so that this can also be called without constructing an actual CALL_EXPR.
3662 The other arguments and return value are the same as for
3663 expand_builtin_memset. */
3665 static rtx
3666 expand_builtin_memset_args (tree dest, tree val, tree len,
3667 rtx target, enum machine_mode mode, tree orig_exp)
3669 tree fndecl, fn;
3670 enum built_in_function fcode;
3671 enum machine_mode val_mode;
3672 char c;
3673 unsigned int dest_align;
3674 rtx dest_mem, dest_addr, len_rtx;
3675 HOST_WIDE_INT expected_size = -1;
3676 unsigned int expected_align = 0;
3677 unsigned HOST_WIDE_INT min_size;
3678 unsigned HOST_WIDE_INT max_size;
3679 unsigned HOST_WIDE_INT probable_max_size;
3681 dest_align = get_pointer_alignment (dest);
3683 /* If DEST is not a pointer type, don't do this operation in-line. */
3684 if (dest_align == 0)
3685 return NULL_RTX;
3687 if (currently_expanding_gimple_stmt)
3688 stringop_block_profile (currently_expanding_gimple_stmt,
3689 &expected_align, &expected_size);
3691 if (expected_align < dest_align)
3692 expected_align = dest_align;
3694 /* If the LEN parameter is zero, return DEST. */
3695 if (integer_zerop (len))
3697 /* Evaluate and ignore VAL in case it has side-effects. */
3698 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3699 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3702 /* Stabilize the arguments in case we fail. */
3703 dest = builtin_save_expr (dest);
3704 val = builtin_save_expr (val);
3705 len = builtin_save_expr (len);
3707 len_rtx = expand_normal (len);
3708 determine_block_size (len, len_rtx, &min_size, &max_size,
3709 &probable_max_size);
3710 dest_mem = get_memory_rtx (dest, len);
3711 val_mode = TYPE_MODE (unsigned_char_type_node);
3713 if (TREE_CODE (val) != INTEGER_CST)
3715 rtx val_rtx;
3717 val_rtx = expand_normal (val);
3718 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3720 /* Assume that we can memset by pieces if we can store
3721 * the coefficients by pieces (in the required modes).
3722 * We can't pass builtin_memset_gen_str as that emits RTL. */
3723 c = 1;
3724 if (tree_fits_uhwi_p (len)
3725 && can_store_by_pieces (tree_to_uhwi (len),
3726 builtin_memset_read_str, &c, dest_align,
3727 true))
3729 val_rtx = force_reg (val_mode, val_rtx);
3730 store_by_pieces (dest_mem, tree_to_uhwi (len),
3731 builtin_memset_gen_str, val_rtx, dest_align,
3732 true, 0);
3734 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3735 dest_align, expected_align,
3736 expected_size, min_size, max_size,
3737 probable_max_size))
3738 goto do_libcall;
3740 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3741 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3742 return dest_mem;
3745 if (target_char_cast (val, &c))
3746 goto do_libcall;
3748 if (c)
3750 if (tree_fits_uhwi_p (len)
3751 && can_store_by_pieces (tree_to_uhwi (len),
3752 builtin_memset_read_str, &c, dest_align,
3753 true))
3754 store_by_pieces (dest_mem, tree_to_uhwi (len),
3755 builtin_memset_read_str, &c, dest_align, true, 0);
3756 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3757 gen_int_mode (c, val_mode),
3758 dest_align, expected_align,
3759 expected_size, min_size, max_size,
3760 probable_max_size))
3761 goto do_libcall;
3763 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3764 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3765 return dest_mem;
3768 set_mem_align (dest_mem, dest_align);
3769 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3770 CALL_EXPR_TAILCALL (orig_exp)
3771 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3772 expected_align, expected_size,
3773 min_size, max_size,
3774 probable_max_size);
3776 if (dest_addr == 0)
3778 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3779 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3782 return dest_addr;
3784 do_libcall:
3785 fndecl = get_callee_fndecl (orig_exp);
3786 fcode = DECL_FUNCTION_CODE (fndecl);
3787 if (fcode == BUILT_IN_MEMSET)
3788 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3789 dest, val, len);
3790 else if (fcode == BUILT_IN_BZERO)
3791 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3792 dest, len);
3793 else
3794 gcc_unreachable ();
3795 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3796 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3797 return expand_call (fn, target, target == const0_rtx);
3800 /* Expand expression EXP, which is a call to the bzero builtin. Return
3801 NULL_RTX if we failed the caller should emit a normal call. */
3803 static rtx
3804 expand_builtin_bzero (tree exp)
3806 tree dest, size;
3807 location_t loc = EXPR_LOCATION (exp);
3809 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3810 return NULL_RTX;
3812 dest = CALL_EXPR_ARG (exp, 0);
3813 size = CALL_EXPR_ARG (exp, 1);
3815 /* New argument list transforming bzero(ptr x, int y) to
3816 memset(ptr x, int 0, size_t y). This is done this way
3817 so that if it isn't expanded inline, we fallback to
3818 calling bzero instead of memset. */
3820 return expand_builtin_memset_args (dest, integer_zero_node,
3821 fold_convert_loc (loc,
3822 size_type_node, size),
3823 const0_rtx, VOIDmode, exp);
3826 /* Expand expression EXP, which is a call to the memcmp built-in function.
3827 Return NULL_RTX if we failed and the caller should emit a normal call,
3828 otherwise try to get the result in TARGET, if convenient (and in mode
3829 MODE, if that's convenient). */
3831 static rtx
3832 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3833 ATTRIBUTE_UNUSED enum machine_mode mode)
3835 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3837 if (!validate_arglist (exp,
3838 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3839 return NULL_RTX;
3841 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3842 implementing memcmp because it will stop if it encounters two
3843 zero bytes. */
3844 #if defined HAVE_cmpmemsi
3846 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3847 rtx result;
3848 rtx insn;
3849 tree arg1 = CALL_EXPR_ARG (exp, 0);
3850 tree arg2 = CALL_EXPR_ARG (exp, 1);
3851 tree len = CALL_EXPR_ARG (exp, 2);
3853 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3854 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3855 enum machine_mode insn_mode;
3857 if (HAVE_cmpmemsi)
3858 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3859 else
3860 return NULL_RTX;
3862 /* If we don't have POINTER_TYPE, call the function. */
3863 if (arg1_align == 0 || arg2_align == 0)
3864 return NULL_RTX;
3866 /* Make a place to write the result of the instruction. */
3867 result = target;
3868 if (! (result != 0
3869 && REG_P (result) && GET_MODE (result) == insn_mode
3870 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3871 result = gen_reg_rtx (insn_mode);
3873 arg1_rtx = get_memory_rtx (arg1, len);
3874 arg2_rtx = get_memory_rtx (arg2, len);
3875 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3877 /* Set MEM_SIZE as appropriate. */
3878 if (CONST_INT_P (arg3_rtx))
3880 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3881 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3884 if (HAVE_cmpmemsi)
3885 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3886 GEN_INT (MIN (arg1_align, arg2_align)));
3887 else
3888 gcc_unreachable ();
3890 if (insn)
3891 emit_insn (insn);
3892 else
3893 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3894 TYPE_MODE (integer_type_node), 3,
3895 XEXP (arg1_rtx, 0), Pmode,
3896 XEXP (arg2_rtx, 0), Pmode,
3897 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3898 TYPE_UNSIGNED (sizetype)),
3899 TYPE_MODE (sizetype));
3901 /* Return the value in the proper mode for this function. */
3902 mode = TYPE_MODE (TREE_TYPE (exp));
3903 if (GET_MODE (result) == mode)
3904 return result;
3905 else if (target != 0)
3907 convert_move (target, result, 0);
3908 return target;
3910 else
3911 return convert_to_mode (mode, result, 0);
3913 #endif /* HAVE_cmpmemsi. */
3915 return NULL_RTX;
3918 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3919 if we failed the caller should emit a normal call, otherwise try to get
3920 the result in TARGET, if convenient. */
3922 static rtx
3923 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3925 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3926 return NULL_RTX;
3928 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3929 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3930 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3932 rtx arg1_rtx, arg2_rtx;
3933 rtx result, insn = NULL_RTX;
3934 tree fndecl, fn;
3935 tree arg1 = CALL_EXPR_ARG (exp, 0);
3936 tree arg2 = CALL_EXPR_ARG (exp, 1);
3938 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3939 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3941 /* If we don't have POINTER_TYPE, call the function. */
3942 if (arg1_align == 0 || arg2_align == 0)
3943 return NULL_RTX;
3945 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3946 arg1 = builtin_save_expr (arg1);
3947 arg2 = builtin_save_expr (arg2);
3949 arg1_rtx = get_memory_rtx (arg1, NULL);
3950 arg2_rtx = get_memory_rtx (arg2, NULL);
3952 #ifdef HAVE_cmpstrsi
3953 /* Try to call cmpstrsi. */
3954 if (HAVE_cmpstrsi)
3956 enum machine_mode insn_mode
3957 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3959 /* Make a place to write the result of the instruction. */
3960 result = target;
3961 if (! (result != 0
3962 && REG_P (result) && GET_MODE (result) == insn_mode
3963 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3964 result = gen_reg_rtx (insn_mode);
3966 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3967 GEN_INT (MIN (arg1_align, arg2_align)));
3969 #endif
3970 #ifdef HAVE_cmpstrnsi
3971 /* Try to determine at least one length and call cmpstrnsi. */
3972 if (!insn && HAVE_cmpstrnsi)
3974 tree len;
3975 rtx arg3_rtx;
3977 enum machine_mode insn_mode
3978 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3979 tree len1 = c_strlen (arg1, 1);
3980 tree len2 = c_strlen (arg2, 1);
3982 if (len1)
3983 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3984 if (len2)
3985 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3987 /* If we don't have a constant length for the first, use the length
3988 of the second, if we know it. We don't require a constant for
3989 this case; some cost analysis could be done if both are available
3990 but neither is constant. For now, assume they're equally cheap,
3991 unless one has side effects. If both strings have constant lengths,
3992 use the smaller. */
3994 if (!len1)
3995 len = len2;
3996 else if (!len2)
3997 len = len1;
3998 else if (TREE_SIDE_EFFECTS (len1))
3999 len = len2;
4000 else if (TREE_SIDE_EFFECTS (len2))
4001 len = len1;
4002 else if (TREE_CODE (len1) != INTEGER_CST)
4003 len = len2;
4004 else if (TREE_CODE (len2) != INTEGER_CST)
4005 len = len1;
4006 else if (tree_int_cst_lt (len1, len2))
4007 len = len1;
4008 else
4009 len = len2;
4011 /* If both arguments have side effects, we cannot optimize. */
4012 if (!len || TREE_SIDE_EFFECTS (len))
4013 goto do_libcall;
4015 arg3_rtx = expand_normal (len);
4017 /* Make a place to write the result of the instruction. */
4018 result = target;
4019 if (! (result != 0
4020 && REG_P (result) && GET_MODE (result) == insn_mode
4021 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4022 result = gen_reg_rtx (insn_mode);
4024 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4025 GEN_INT (MIN (arg1_align, arg2_align)));
4027 #endif
4029 if (insn)
4031 enum machine_mode mode;
4032 emit_insn (insn);
4034 /* Return the value in the proper mode for this function. */
4035 mode = TYPE_MODE (TREE_TYPE (exp));
4036 if (GET_MODE (result) == mode)
4037 return result;
4038 if (target == 0)
4039 return convert_to_mode (mode, result, 0);
4040 convert_move (target, result, 0);
4041 return target;
4044 /* Expand the library call ourselves using a stabilized argument
4045 list to avoid re-evaluating the function's arguments twice. */
4046 #ifdef HAVE_cmpstrnsi
4047 do_libcall:
4048 #endif
4049 fndecl = get_callee_fndecl (exp);
4050 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4051 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4052 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4053 return expand_call (fn, target, target == const0_rtx);
4055 #endif
4056 return NULL_RTX;
4059 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4060 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4061 the result in TARGET, if convenient. */
4063 static rtx
4064 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4065 ATTRIBUTE_UNUSED enum machine_mode mode)
4067 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4069 if (!validate_arglist (exp,
4070 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4071 return NULL_RTX;
4073 /* If c_strlen can determine an expression for one of the string
4074 lengths, and it doesn't have side effects, then emit cmpstrnsi
4075 using length MIN(strlen(string)+1, arg3). */
4076 #ifdef HAVE_cmpstrnsi
4077 if (HAVE_cmpstrnsi)
4079 tree len, len1, len2;
4080 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4081 rtx result, insn;
4082 tree fndecl, fn;
4083 tree arg1 = CALL_EXPR_ARG (exp, 0);
4084 tree arg2 = CALL_EXPR_ARG (exp, 1);
4085 tree arg3 = CALL_EXPR_ARG (exp, 2);
4087 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4088 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4089 enum machine_mode insn_mode
4090 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4092 len1 = c_strlen (arg1, 1);
4093 len2 = c_strlen (arg2, 1);
4095 if (len1)
4096 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4097 if (len2)
4098 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4100 /* If we don't have a constant length for the first, use the length
4101 of the second, if we know it. We don't require a constant for
4102 this case; some cost analysis could be done if both are available
4103 but neither is constant. For now, assume they're equally cheap,
4104 unless one has side effects. If both strings have constant lengths,
4105 use the smaller. */
4107 if (!len1)
4108 len = len2;
4109 else if (!len2)
4110 len = len1;
4111 else if (TREE_SIDE_EFFECTS (len1))
4112 len = len2;
4113 else if (TREE_SIDE_EFFECTS (len2))
4114 len = len1;
4115 else if (TREE_CODE (len1) != INTEGER_CST)
4116 len = len2;
4117 else if (TREE_CODE (len2) != INTEGER_CST)
4118 len = len1;
4119 else if (tree_int_cst_lt (len1, len2))
4120 len = len1;
4121 else
4122 len = len2;
4124 /* If both arguments have side effects, we cannot optimize. */
4125 if (!len || TREE_SIDE_EFFECTS (len))
4126 return NULL_RTX;
4128 /* The actual new length parameter is MIN(len,arg3). */
4129 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4130 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4132 /* If we don't have POINTER_TYPE, call the function. */
4133 if (arg1_align == 0 || arg2_align == 0)
4134 return NULL_RTX;
4136 /* Make a place to write the result of the instruction. */
4137 result = target;
4138 if (! (result != 0
4139 && REG_P (result) && GET_MODE (result) == insn_mode
4140 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4141 result = gen_reg_rtx (insn_mode);
4143 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4144 arg1 = builtin_save_expr (arg1);
4145 arg2 = builtin_save_expr (arg2);
4146 len = builtin_save_expr (len);
4148 arg1_rtx = get_memory_rtx (arg1, len);
4149 arg2_rtx = get_memory_rtx (arg2, len);
4150 arg3_rtx = expand_normal (len);
4151 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4152 GEN_INT (MIN (arg1_align, arg2_align)));
4153 if (insn)
4155 emit_insn (insn);
4157 /* Return the value in the proper mode for this function. */
4158 mode = TYPE_MODE (TREE_TYPE (exp));
4159 if (GET_MODE (result) == mode)
4160 return result;
4161 if (target == 0)
4162 return convert_to_mode (mode, result, 0);
4163 convert_move (target, result, 0);
4164 return target;
4167 /* Expand the library call ourselves using a stabilized argument
4168 list to avoid re-evaluating the function's arguments twice. */
4169 fndecl = get_callee_fndecl (exp);
4170 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4171 arg1, arg2, len);
4172 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4173 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4174 return expand_call (fn, target, target == const0_rtx);
4176 #endif
4177 return NULL_RTX;
4180 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4181 if that's convenient. */
4184 expand_builtin_saveregs (void)
4186 rtx val, seq;
4188 /* Don't do __builtin_saveregs more than once in a function.
4189 Save the result of the first call and reuse it. */
4190 if (saveregs_value != 0)
4191 return saveregs_value;
4193 /* When this function is called, it means that registers must be
4194 saved on entry to this function. So we migrate the call to the
4195 first insn of this function. */
4197 start_sequence ();
4199 /* Do whatever the machine needs done in this case. */
4200 val = targetm.calls.expand_builtin_saveregs ();
4202 seq = get_insns ();
4203 end_sequence ();
4205 saveregs_value = val;
4207 /* Put the insns after the NOTE that starts the function. If this
4208 is inside a start_sequence, make the outer-level insn chain current, so
4209 the code is placed at the start of the function. */
4210 push_topmost_sequence ();
4211 emit_insn_after (seq, entry_of_function ());
4212 pop_topmost_sequence ();
4214 return val;
4217 /* Expand a call to __builtin_next_arg. */
4219 static rtx
4220 expand_builtin_next_arg (void)
4222 /* Checking arguments is already done in fold_builtin_next_arg
4223 that must be called before this function. */
4224 return expand_binop (ptr_mode, add_optab,
4225 crtl->args.internal_arg_pointer,
4226 crtl->args.arg_offset_rtx,
4227 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4230 /* Make it easier for the backends by protecting the valist argument
4231 from multiple evaluations. */
4233 static tree
4234 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4236 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4238 /* The current way of determining the type of valist is completely
4239 bogus. We should have the information on the va builtin instead. */
4240 if (!vatype)
4241 vatype = targetm.fn_abi_va_list (cfun->decl);
4243 if (TREE_CODE (vatype) == ARRAY_TYPE)
4245 if (TREE_SIDE_EFFECTS (valist))
4246 valist = save_expr (valist);
4248 /* For this case, the backends will be expecting a pointer to
4249 vatype, but it's possible we've actually been given an array
4250 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4251 So fix it. */
4252 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4254 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4255 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4258 else
4260 tree pt = build_pointer_type (vatype);
4262 if (! needs_lvalue)
4264 if (! TREE_SIDE_EFFECTS (valist))
4265 return valist;
4267 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4268 TREE_SIDE_EFFECTS (valist) = 1;
4271 if (TREE_SIDE_EFFECTS (valist))
4272 valist = save_expr (valist);
4273 valist = fold_build2_loc (loc, MEM_REF,
4274 vatype, valist, build_int_cst (pt, 0));
4277 return valist;
4280 /* The "standard" definition of va_list is void*. */
4282 tree
4283 std_build_builtin_va_list (void)
4285 return ptr_type_node;
4288 /* The "standard" abi va_list is va_list_type_node. */
4290 tree
4291 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4293 return va_list_type_node;
4296 /* The "standard" type of va_list is va_list_type_node. */
4298 tree
4299 std_canonical_va_list_type (tree type)
4301 tree wtype, htype;
4303 if (INDIRECT_REF_P (type))
4304 type = TREE_TYPE (type);
4305 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4306 type = TREE_TYPE (type);
4307 wtype = va_list_type_node;
4308 htype = type;
4309 /* Treat structure va_list types. */
4310 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4311 htype = TREE_TYPE (htype);
4312 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4314 /* If va_list is an array type, the argument may have decayed
4315 to a pointer type, e.g. by being passed to another function.
4316 In that case, unwrap both types so that we can compare the
4317 underlying records. */
4318 if (TREE_CODE (htype) == ARRAY_TYPE
4319 || POINTER_TYPE_P (htype))
4321 wtype = TREE_TYPE (wtype);
4322 htype = TREE_TYPE (htype);
4325 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4326 return va_list_type_node;
4328 return NULL_TREE;
4331 /* The "standard" implementation of va_start: just assign `nextarg' to
4332 the variable. */
4334 void
4335 std_expand_builtin_va_start (tree valist, rtx nextarg)
4337 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4338 convert_move (va_r, nextarg, 0);
4341 /* Expand EXP, a call to __builtin_va_start. */
4343 static rtx
4344 expand_builtin_va_start (tree exp)
4346 rtx nextarg;
4347 tree valist;
4348 location_t loc = EXPR_LOCATION (exp);
4350 if (call_expr_nargs (exp) < 2)
4352 error_at (loc, "too few arguments to function %<va_start%>");
4353 return const0_rtx;
4356 if (fold_builtin_next_arg (exp, true))
4357 return const0_rtx;
4359 nextarg = expand_builtin_next_arg ();
4360 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4362 if (targetm.expand_builtin_va_start)
4363 targetm.expand_builtin_va_start (valist, nextarg);
4364 else
4365 std_expand_builtin_va_start (valist, nextarg);
4367 return const0_rtx;
4370 /* Expand EXP, a call to __builtin_va_end. */
4372 static rtx
4373 expand_builtin_va_end (tree exp)
4375 tree valist = CALL_EXPR_ARG (exp, 0);
4377 /* Evaluate for side effects, if needed. I hate macros that don't
4378 do that. */
4379 if (TREE_SIDE_EFFECTS (valist))
4380 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4382 return const0_rtx;
4385 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4386 builtin rather than just as an assignment in stdarg.h because of the
4387 nastiness of array-type va_list types. */
4389 static rtx
4390 expand_builtin_va_copy (tree exp)
4392 tree dst, src, t;
4393 location_t loc = EXPR_LOCATION (exp);
4395 dst = CALL_EXPR_ARG (exp, 0);
4396 src = CALL_EXPR_ARG (exp, 1);
4398 dst = stabilize_va_list_loc (loc, dst, 1);
4399 src = stabilize_va_list_loc (loc, src, 0);
4401 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4403 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4405 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4406 TREE_SIDE_EFFECTS (t) = 1;
4407 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4409 else
4411 rtx dstb, srcb, size;
4413 /* Evaluate to pointers. */
4414 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4415 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4416 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4417 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4419 dstb = convert_memory_address (Pmode, dstb);
4420 srcb = convert_memory_address (Pmode, srcb);
4422 /* "Dereference" to BLKmode memories. */
4423 dstb = gen_rtx_MEM (BLKmode, dstb);
4424 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4425 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4426 srcb = gen_rtx_MEM (BLKmode, srcb);
4427 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4428 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4430 /* Copy. */
4431 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4434 return const0_rtx;
4437 /* Expand a call to one of the builtin functions __builtin_frame_address or
4438 __builtin_return_address. */
4440 static rtx
4441 expand_builtin_frame_address (tree fndecl, tree exp)
4443 /* The argument must be a nonnegative integer constant.
4444 It counts the number of frames to scan up the stack.
4445 The value is the return address saved in that frame. */
4446 if (call_expr_nargs (exp) == 0)
4447 /* Warning about missing arg was already issued. */
4448 return const0_rtx;
4449 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4451 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4452 error ("invalid argument to %<__builtin_frame_address%>");
4453 else
4454 error ("invalid argument to %<__builtin_return_address%>");
4455 return const0_rtx;
4457 else
4459 rtx tem
4460 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4461 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4463 /* Some ports cannot access arbitrary stack frames. */
4464 if (tem == NULL)
4466 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4467 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4468 else
4469 warning (0, "unsupported argument to %<__builtin_return_address%>");
4470 return const0_rtx;
4473 /* For __builtin_frame_address, return what we've got. */
4474 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4475 return tem;
4477 if (!REG_P (tem)
4478 && ! CONSTANT_P (tem))
4479 tem = copy_addr_to_reg (tem);
4480 return tem;
4484 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4485 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4486 is the same as for allocate_dynamic_stack_space. */
4488 static rtx
4489 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4491 rtx op0;
4492 rtx result;
4493 bool valid_arglist;
4494 unsigned int align;
4495 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4496 == BUILT_IN_ALLOCA_WITH_ALIGN);
4498 valid_arglist
4499 = (alloca_with_align
4500 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4501 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4503 if (!valid_arglist)
4504 return NULL_RTX;
4506 /* Compute the argument. */
4507 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4509 /* Compute the alignment. */
4510 align = (alloca_with_align
4511 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4512 : BIGGEST_ALIGNMENT);
4514 /* Allocate the desired space. */
4515 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4516 result = convert_memory_address (ptr_mode, result);
4518 return result;
4521 /* Expand a call to bswap builtin in EXP.
4522 Return NULL_RTX if a normal call should be emitted rather than expanding the
4523 function in-line. If convenient, the result should be placed in TARGET.
4524 SUBTARGET may be used as the target for computing one of EXP's operands. */
4526 static rtx
4527 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4528 rtx subtarget)
4530 tree arg;
4531 rtx op0;
4533 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4534 return NULL_RTX;
4536 arg = CALL_EXPR_ARG (exp, 0);
4537 op0 = expand_expr (arg,
4538 subtarget && GET_MODE (subtarget) == target_mode
4539 ? subtarget : NULL_RTX,
4540 target_mode, EXPAND_NORMAL);
4541 if (GET_MODE (op0) != target_mode)
4542 op0 = convert_to_mode (target_mode, op0, 1);
4544 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4546 gcc_assert (target);
4548 return convert_to_mode (target_mode, target, 1);
4551 /* Expand a call to a unary builtin in EXP.
4552 Return NULL_RTX if a normal call should be emitted rather than expanding the
4553 function in-line. If convenient, the result should be placed in TARGET.
4554 SUBTARGET may be used as the target for computing one of EXP's operands. */
4556 static rtx
4557 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4558 rtx subtarget, optab op_optab)
4560 rtx op0;
4562 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4563 return NULL_RTX;
4565 /* Compute the argument. */
4566 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4567 (subtarget
4568 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4569 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4570 VOIDmode, EXPAND_NORMAL);
4571 /* Compute op, into TARGET if possible.
4572 Set TARGET to wherever the result comes back. */
4573 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4574 op_optab, op0, target, op_optab != clrsb_optab);
4575 gcc_assert (target);
4577 return convert_to_mode (target_mode, target, 0);
4580 /* Expand a call to __builtin_expect. We just return our argument
4581 as the builtin_expect semantic should've been already executed by
4582 tree branch prediction pass. */
4584 static rtx
4585 expand_builtin_expect (tree exp, rtx target)
4587 tree arg;
4589 if (call_expr_nargs (exp) < 2)
4590 return const0_rtx;
4591 arg = CALL_EXPR_ARG (exp, 0);
4593 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4594 /* When guessing was done, the hints should be already stripped away. */
4595 gcc_assert (!flag_guess_branch_prob
4596 || optimize == 0 || seen_error ());
4597 return target;
4600 /* Expand a call to __builtin_assume_aligned. We just return our first
4601 argument as the builtin_assume_aligned semantic should've been already
4602 executed by CCP. */
4604 static rtx
4605 expand_builtin_assume_aligned (tree exp, rtx target)
4607 if (call_expr_nargs (exp) < 2)
4608 return const0_rtx;
4609 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4610 EXPAND_NORMAL);
4611 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4612 && (call_expr_nargs (exp) < 3
4613 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4614 return target;
4617 void
4618 expand_builtin_trap (void)
4620 #ifdef HAVE_trap
4621 if (HAVE_trap)
4623 rtx insn = emit_insn (gen_trap ());
4624 /* For trap insns when not accumulating outgoing args force
4625 REG_ARGS_SIZE note to prevent crossjumping of calls with
4626 different args sizes. */
4627 if (!ACCUMULATE_OUTGOING_ARGS)
4628 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4630 else
4631 #endif
4632 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4633 emit_barrier ();
4636 /* Expand a call to __builtin_unreachable. We do nothing except emit
4637 a barrier saying that control flow will not pass here.
4639 It is the responsibility of the program being compiled to ensure
4640 that control flow does never reach __builtin_unreachable. */
4641 static void
4642 expand_builtin_unreachable (void)
4644 emit_barrier ();
4647 /* Expand EXP, a call to fabs, fabsf or fabsl.
4648 Return NULL_RTX if a normal call should be emitted rather than expanding
4649 the function inline. If convenient, the result should be placed
4650 in TARGET. SUBTARGET may be used as the target for computing
4651 the operand. */
4653 static rtx
4654 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4656 enum machine_mode mode;
4657 tree arg;
4658 rtx op0;
4660 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4661 return NULL_RTX;
4663 arg = CALL_EXPR_ARG (exp, 0);
4664 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4665 mode = TYPE_MODE (TREE_TYPE (arg));
4666 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4667 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4670 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4671 Return NULL is a normal call should be emitted rather than expanding the
4672 function inline. If convenient, the result should be placed in TARGET.
4673 SUBTARGET may be used as the target for computing the operand. */
4675 static rtx
4676 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4678 rtx op0, op1;
4679 tree arg;
4681 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4682 return NULL_RTX;
4684 arg = CALL_EXPR_ARG (exp, 0);
4685 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4687 arg = CALL_EXPR_ARG (exp, 1);
4688 op1 = expand_normal (arg);
4690 return expand_copysign (op0, op1, target);
4693 /* Create a new constant string literal and return a char* pointer to it.
4694 The STRING_CST value is the LEN characters at STR. */
4695 tree
4696 build_string_literal (int len, const char *str)
4698 tree t, elem, index, type;
4700 t = build_string (len, str);
4701 elem = build_type_variant (char_type_node, 1, 0);
4702 index = build_index_type (size_int (len - 1));
4703 type = build_array_type (elem, index);
4704 TREE_TYPE (t) = type;
4705 TREE_CONSTANT (t) = 1;
4706 TREE_READONLY (t) = 1;
4707 TREE_STATIC (t) = 1;
4709 type = build_pointer_type (elem);
4710 t = build1 (ADDR_EXPR, type,
4711 build4 (ARRAY_REF, elem,
4712 t, integer_zero_node, NULL_TREE, NULL_TREE));
4713 return t;
4716 /* Expand a call to __builtin___clear_cache. */
4718 static rtx
4719 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4721 #ifndef HAVE_clear_cache
4722 #ifdef CLEAR_INSN_CACHE
4723 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4724 does something. Just do the default expansion to a call to
4725 __clear_cache(). */
4726 return NULL_RTX;
4727 #else
4728 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4729 does nothing. There is no need to call it. Do nothing. */
4730 return const0_rtx;
4731 #endif /* CLEAR_INSN_CACHE */
4732 #else
4733 /* We have a "clear_cache" insn, and it will handle everything. */
4734 tree begin, end;
4735 rtx begin_rtx, end_rtx;
4737 /* We must not expand to a library call. If we did, any
4738 fallback library function in libgcc that might contain a call to
4739 __builtin___clear_cache() would recurse infinitely. */
4740 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4742 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4743 return const0_rtx;
4746 if (HAVE_clear_cache)
4748 struct expand_operand ops[2];
4750 begin = CALL_EXPR_ARG (exp, 0);
4751 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4753 end = CALL_EXPR_ARG (exp, 1);
4754 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4756 create_address_operand (&ops[0], begin_rtx);
4757 create_address_operand (&ops[1], end_rtx);
4758 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4759 return const0_rtx;
4761 return const0_rtx;
4762 #endif /* HAVE_clear_cache */
4765 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4767 static rtx
4768 round_trampoline_addr (rtx tramp)
4770 rtx temp, addend, mask;
4772 /* If we don't need too much alignment, we'll have been guaranteed
4773 proper alignment by get_trampoline_type. */
4774 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4775 return tramp;
4777 /* Round address up to desired boundary. */
4778 temp = gen_reg_rtx (Pmode);
4779 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4780 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4782 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4783 temp, 0, OPTAB_LIB_WIDEN);
4784 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4785 temp, 0, OPTAB_LIB_WIDEN);
4787 return tramp;
4790 static rtx
4791 expand_builtin_init_trampoline (tree exp, bool onstack)
4793 tree t_tramp, t_func, t_chain;
4794 rtx m_tramp, r_tramp, r_chain, tmp;
4796 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4797 POINTER_TYPE, VOID_TYPE))
4798 return NULL_RTX;
4800 t_tramp = CALL_EXPR_ARG (exp, 0);
4801 t_func = CALL_EXPR_ARG (exp, 1);
4802 t_chain = CALL_EXPR_ARG (exp, 2);
4804 r_tramp = expand_normal (t_tramp);
4805 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4806 MEM_NOTRAP_P (m_tramp) = 1;
4808 /* If ONSTACK, the TRAMP argument should be the address of a field
4809 within the local function's FRAME decl. Either way, let's see if
4810 we can fill in the MEM_ATTRs for this memory. */
4811 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4812 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4814 /* Creator of a heap trampoline is responsible for making sure the
4815 address is aligned to at least STACK_BOUNDARY. Normally malloc
4816 will ensure this anyhow. */
4817 tmp = round_trampoline_addr (r_tramp);
4818 if (tmp != r_tramp)
4820 m_tramp = change_address (m_tramp, BLKmode, tmp);
4821 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4822 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4825 /* The FUNC argument should be the address of the nested function.
4826 Extract the actual function decl to pass to the hook. */
4827 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4828 t_func = TREE_OPERAND (t_func, 0);
4829 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4831 r_chain = expand_normal (t_chain);
4833 /* Generate insns to initialize the trampoline. */
4834 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4836 if (onstack)
4838 trampolines_created = 1;
4840 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4841 "trampoline generated for nested function %qD", t_func);
4844 return const0_rtx;
4847 static rtx
4848 expand_builtin_adjust_trampoline (tree exp)
4850 rtx tramp;
4852 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4853 return NULL_RTX;
4855 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4856 tramp = round_trampoline_addr (tramp);
4857 if (targetm.calls.trampoline_adjust_address)
4858 tramp = targetm.calls.trampoline_adjust_address (tramp);
4860 return tramp;
4863 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4864 function. The function first checks whether the back end provides
4865 an insn to implement signbit for the respective mode. If not, it
4866 checks whether the floating point format of the value is such that
4867 the sign bit can be extracted. If that is not the case, the
4868 function returns NULL_RTX to indicate that a normal call should be
4869 emitted rather than expanding the function in-line. EXP is the
4870 expression that is a call to the builtin function; if convenient,
4871 the result should be placed in TARGET. */
4872 static rtx
4873 expand_builtin_signbit (tree exp, rtx target)
4875 const struct real_format *fmt;
4876 enum machine_mode fmode, imode, rmode;
4877 tree arg;
4878 int word, bitpos;
4879 enum insn_code icode;
4880 rtx temp;
4881 location_t loc = EXPR_LOCATION (exp);
4883 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4884 return NULL_RTX;
4886 arg = CALL_EXPR_ARG (exp, 0);
4887 fmode = TYPE_MODE (TREE_TYPE (arg));
4888 rmode = TYPE_MODE (TREE_TYPE (exp));
4889 fmt = REAL_MODE_FORMAT (fmode);
4891 arg = builtin_save_expr (arg);
4893 /* Expand the argument yielding a RTX expression. */
4894 temp = expand_normal (arg);
4896 /* Check if the back end provides an insn that handles signbit for the
4897 argument's mode. */
4898 icode = optab_handler (signbit_optab, fmode);
4899 if (icode != CODE_FOR_nothing)
4901 rtx last = get_last_insn ();
4902 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4903 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4904 return target;
4905 delete_insns_since (last);
4908 /* For floating point formats without a sign bit, implement signbit
4909 as "ARG < 0.0". */
4910 bitpos = fmt->signbit_ro;
4911 if (bitpos < 0)
4913 /* But we can't do this if the format supports signed zero. */
4914 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4915 return NULL_RTX;
4917 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4918 build_real (TREE_TYPE (arg), dconst0));
4919 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4922 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4924 imode = int_mode_for_mode (fmode);
4925 if (imode == BLKmode)
4926 return NULL_RTX;
4927 temp = gen_lowpart (imode, temp);
4929 else
4931 imode = word_mode;
4932 /* Handle targets with different FP word orders. */
4933 if (FLOAT_WORDS_BIG_ENDIAN)
4934 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4935 else
4936 word = bitpos / BITS_PER_WORD;
4937 temp = operand_subword_force (temp, word, fmode);
4938 bitpos = bitpos % BITS_PER_WORD;
4941 /* Force the intermediate word_mode (or narrower) result into a
4942 register. This avoids attempting to create paradoxical SUBREGs
4943 of floating point modes below. */
4944 temp = force_reg (imode, temp);
4946 /* If the bitpos is within the "result mode" lowpart, the operation
4947 can be implement with a single bitwise AND. Otherwise, we need
4948 a right shift and an AND. */
4950 if (bitpos < GET_MODE_BITSIZE (rmode))
4952 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
4954 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4955 temp = gen_lowpart (rmode, temp);
4956 temp = expand_binop (rmode, and_optab, temp,
4957 immed_wide_int_const (mask, rmode),
4958 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4960 else
4962 /* Perform a logical right shift to place the signbit in the least
4963 significant bit, then truncate the result to the desired mode
4964 and mask just this bit. */
4965 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4966 temp = gen_lowpart (rmode, temp);
4967 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4968 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4971 return temp;
4974 /* Expand fork or exec calls. TARGET is the desired target of the
4975 call. EXP is the call. FN is the
4976 identificator of the actual function. IGNORE is nonzero if the
4977 value is to be ignored. */
4979 static rtx
4980 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4982 tree id, decl;
4983 tree call;
4985 /* If we are not profiling, just call the function. */
4986 if (!profile_arc_flag)
4987 return NULL_RTX;
4989 /* Otherwise call the wrapper. This should be equivalent for the rest of
4990 compiler, so the code does not diverge, and the wrapper may run the
4991 code necessary for keeping the profiling sane. */
4993 switch (DECL_FUNCTION_CODE (fn))
4995 case BUILT_IN_FORK:
4996 id = get_identifier ("__gcov_fork");
4997 break;
4999 case BUILT_IN_EXECL:
5000 id = get_identifier ("__gcov_execl");
5001 break;
5003 case BUILT_IN_EXECV:
5004 id = get_identifier ("__gcov_execv");
5005 break;
5007 case BUILT_IN_EXECLP:
5008 id = get_identifier ("__gcov_execlp");
5009 break;
5011 case BUILT_IN_EXECLE:
5012 id = get_identifier ("__gcov_execle");
5013 break;
5015 case BUILT_IN_EXECVP:
5016 id = get_identifier ("__gcov_execvp");
5017 break;
5019 case BUILT_IN_EXECVE:
5020 id = get_identifier ("__gcov_execve");
5021 break;
5023 default:
5024 gcc_unreachable ();
5027 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5028 FUNCTION_DECL, id, TREE_TYPE (fn));
5029 DECL_EXTERNAL (decl) = 1;
5030 TREE_PUBLIC (decl) = 1;
5031 DECL_ARTIFICIAL (decl) = 1;
5032 TREE_NOTHROW (decl) = 1;
5033 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5034 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5035 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5036 return expand_call (call, target, ignore);
5041 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5042 the pointer in these functions is void*, the tree optimizers may remove
5043 casts. The mode computed in expand_builtin isn't reliable either, due
5044 to __sync_bool_compare_and_swap.
5046 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5047 group of builtins. This gives us log2 of the mode size. */
5049 static inline enum machine_mode
5050 get_builtin_sync_mode (int fcode_diff)
5052 /* The size is not negotiable, so ask not to get BLKmode in return
5053 if the target indicates that a smaller size would be better. */
5054 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5057 /* Expand the memory expression LOC and return the appropriate memory operand
5058 for the builtin_sync operations. */
5060 static rtx
5061 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5063 rtx addr, mem;
5065 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5066 addr = convert_memory_address (Pmode, addr);
5068 /* Note that we explicitly do not want any alias information for this
5069 memory, so that we kill all other live memories. Otherwise we don't
5070 satisfy the full barrier semantics of the intrinsic. */
5071 mem = validize_mem (gen_rtx_MEM (mode, addr));
5073 /* The alignment needs to be at least according to that of the mode. */
5074 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5075 get_pointer_alignment (loc)));
5076 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5077 MEM_VOLATILE_P (mem) = 1;
5079 return mem;
5082 /* Make sure an argument is in the right mode.
5083 EXP is the tree argument.
5084 MODE is the mode it should be in. */
5086 static rtx
5087 expand_expr_force_mode (tree exp, enum machine_mode mode)
5089 rtx val;
5090 enum machine_mode old_mode;
5092 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5093 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5094 of CONST_INTs, where we know the old_mode only from the call argument. */
5096 old_mode = GET_MODE (val);
5097 if (old_mode == VOIDmode)
5098 old_mode = TYPE_MODE (TREE_TYPE (exp));
5099 val = convert_modes (mode, old_mode, val, 1);
5100 return val;
5104 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5105 EXP is the CALL_EXPR. CODE is the rtx code
5106 that corresponds to the arithmetic or logical operation from the name;
5107 an exception here is that NOT actually means NAND. TARGET is an optional
5108 place for us to store the results; AFTER is true if this is the
5109 fetch_and_xxx form. */
5111 static rtx
5112 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5113 enum rtx_code code, bool after,
5114 rtx target)
5116 rtx val, mem;
5117 location_t loc = EXPR_LOCATION (exp);
5119 if (code == NOT && warn_sync_nand)
5121 tree fndecl = get_callee_fndecl (exp);
5122 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5124 static bool warned_f_a_n, warned_n_a_f;
5126 switch (fcode)
5128 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5129 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5130 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5131 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5132 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5133 if (warned_f_a_n)
5134 break;
5136 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5137 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5138 warned_f_a_n = true;
5139 break;
5141 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5142 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5143 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5144 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5145 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5146 if (warned_n_a_f)
5147 break;
5149 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5150 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5151 warned_n_a_f = true;
5152 break;
5154 default:
5155 gcc_unreachable ();
5159 /* Expand the operands. */
5160 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5161 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5163 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5164 after);
5167 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5168 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5169 true if this is the boolean form. TARGET is a place for us to store the
5170 results; this is NOT optional if IS_BOOL is true. */
5172 static rtx
5173 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5174 bool is_bool, rtx target)
5176 rtx old_val, new_val, mem;
5177 rtx *pbool, *poval;
5179 /* Expand the operands. */
5180 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5181 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5182 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5184 pbool = poval = NULL;
5185 if (target != const0_rtx)
5187 if (is_bool)
5188 pbool = &target;
5189 else
5190 poval = &target;
5192 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5193 false, MEMMODEL_SEQ_CST,
5194 MEMMODEL_SEQ_CST))
5195 return NULL_RTX;
5197 return target;
5200 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5201 general form is actually an atomic exchange, and some targets only
5202 support a reduced form with the second argument being a constant 1.
5203 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5204 the results. */
5206 static rtx
5207 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5208 rtx target)
5210 rtx val, mem;
5212 /* Expand the operands. */
5213 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5214 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5216 return expand_sync_lock_test_and_set (target, mem, val);
5219 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5221 static void
5222 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5224 rtx mem;
5226 /* Expand the operands. */
5227 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5229 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5232 /* Given an integer representing an ``enum memmodel'', verify its
5233 correctness and return the memory model enum. */
5235 static enum memmodel
5236 get_memmodel (tree exp)
5238 rtx op;
5239 unsigned HOST_WIDE_INT val;
5241 /* If the parameter is not a constant, it's a run time value so we'll just
5242 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5243 if (TREE_CODE (exp) != INTEGER_CST)
5244 return MEMMODEL_SEQ_CST;
5246 op = expand_normal (exp);
5248 val = INTVAL (op);
5249 if (targetm.memmodel_check)
5250 val = targetm.memmodel_check (val);
5251 else if (val & ~MEMMODEL_MASK)
5253 warning (OPT_Winvalid_memory_model,
5254 "Unknown architecture specifier in memory model to builtin.");
5255 return MEMMODEL_SEQ_CST;
5258 if ((INTVAL (op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5260 warning (OPT_Winvalid_memory_model,
5261 "invalid memory model argument to builtin");
5262 return MEMMODEL_SEQ_CST;
5265 return (enum memmodel) val;
5268 /* Expand the __atomic_exchange intrinsic:
5269 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5270 EXP is the CALL_EXPR.
5271 TARGET is an optional place for us to store the results. */
5273 static rtx
5274 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5276 rtx val, mem;
5277 enum memmodel model;
5279 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5280 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5282 error ("invalid memory model for %<__atomic_exchange%>");
5283 return NULL_RTX;
5286 if (!flag_inline_atomics)
5287 return NULL_RTX;
5289 /* Expand the operands. */
5290 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5291 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5293 return expand_atomic_exchange (target, mem, val, model);
5296 /* Expand the __atomic_compare_exchange intrinsic:
5297 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5298 TYPE desired, BOOL weak,
5299 enum memmodel success,
5300 enum memmodel failure)
5301 EXP is the CALL_EXPR.
5302 TARGET is an optional place for us to store the results. */
5304 static rtx
5305 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5306 rtx target)
5308 rtx expect, desired, mem, oldval, label;
5309 enum memmodel success, failure;
5310 tree weak;
5311 bool is_weak;
5313 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5314 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5316 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5317 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5319 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5320 return NULL_RTX;
5323 if (failure > success)
5325 error ("failure memory model cannot be stronger than success "
5326 "memory model for %<__atomic_compare_exchange%>");
5327 return NULL_RTX;
5330 if (!flag_inline_atomics)
5331 return NULL_RTX;
5333 /* Expand the operands. */
5334 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5336 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5337 expect = convert_memory_address (Pmode, expect);
5338 expect = gen_rtx_MEM (mode, expect);
5339 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5341 weak = CALL_EXPR_ARG (exp, 3);
5342 is_weak = false;
5343 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5344 is_weak = true;
5346 if (target == const0_rtx)
5347 target = NULL;
5349 /* Lest the rtl backend create a race condition with an imporoper store
5350 to memory, always create a new pseudo for OLDVAL. */
5351 oldval = NULL;
5353 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5354 is_weak, success, failure))
5355 return NULL_RTX;
5357 /* Conditionally store back to EXPECT, lest we create a race condition
5358 with an improper store to memory. */
5359 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5360 the normal case where EXPECT is totally private, i.e. a register. At
5361 which point the store can be unconditional. */
5362 label = gen_label_rtx ();
5363 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL, VOIDmode, 1, label);
5364 emit_move_insn (expect, oldval);
5365 emit_label (label);
5367 return target;
5370 /* Expand the __atomic_load intrinsic:
5371 TYPE __atomic_load (TYPE *object, enum memmodel)
5372 EXP is the CALL_EXPR.
5373 TARGET is an optional place for us to store the results. */
5375 static rtx
5376 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5378 rtx mem;
5379 enum memmodel model;
5381 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5382 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5383 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5385 error ("invalid memory model for %<__atomic_load%>");
5386 return NULL_RTX;
5389 if (!flag_inline_atomics)
5390 return NULL_RTX;
5392 /* Expand the operand. */
5393 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5395 return expand_atomic_load (target, mem, model);
5399 /* Expand the __atomic_store intrinsic:
5400 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5401 EXP is the CALL_EXPR.
5402 TARGET is an optional place for us to store the results. */
5404 static rtx
5405 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5407 rtx mem, val;
5408 enum memmodel model;
5410 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5411 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5412 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5413 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5415 error ("invalid memory model for %<__atomic_store%>");
5416 return NULL_RTX;
5419 if (!flag_inline_atomics)
5420 return NULL_RTX;
5422 /* Expand the operands. */
5423 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5424 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5426 return expand_atomic_store (mem, val, model, false);
5429 /* Expand the __atomic_fetch_XXX intrinsic:
5430 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5431 EXP is the CALL_EXPR.
5432 TARGET is an optional place for us to store the results.
5433 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5434 FETCH_AFTER is true if returning the result of the operation.
5435 FETCH_AFTER is false if returning the value before the operation.
5436 IGNORE is true if the result is not used.
5437 EXT_CALL is the correct builtin for an external call if this cannot be
5438 resolved to an instruction sequence. */
5440 static rtx
5441 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5442 enum rtx_code code, bool fetch_after,
5443 bool ignore, enum built_in_function ext_call)
5445 rtx val, mem, ret;
5446 enum memmodel model;
5447 tree fndecl;
5448 tree addr;
5450 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5452 /* Expand the operands. */
5453 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5454 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5456 /* Only try generating instructions if inlining is turned on. */
5457 if (flag_inline_atomics)
5459 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5460 if (ret)
5461 return ret;
5464 /* Return if a different routine isn't needed for the library call. */
5465 if (ext_call == BUILT_IN_NONE)
5466 return NULL_RTX;
5468 /* Change the call to the specified function. */
5469 fndecl = get_callee_fndecl (exp);
5470 addr = CALL_EXPR_FN (exp);
5471 STRIP_NOPS (addr);
5473 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5474 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5476 /* Expand the call here so we can emit trailing code. */
5477 ret = expand_call (exp, target, ignore);
5479 /* Replace the original function just in case it matters. */
5480 TREE_OPERAND (addr, 0) = fndecl;
5482 /* Then issue the arithmetic correction to return the right result. */
5483 if (!ignore)
5485 if (code == NOT)
5487 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5488 OPTAB_LIB_WIDEN);
5489 ret = expand_simple_unop (mode, NOT, ret, target, true);
5491 else
5492 ret = expand_simple_binop (mode, code, ret, val, target, true,
5493 OPTAB_LIB_WIDEN);
5495 return ret;
5499 #ifndef HAVE_atomic_clear
5500 # define HAVE_atomic_clear 0
5501 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5502 #endif
5504 /* Expand an atomic clear operation.
5505 void _atomic_clear (BOOL *obj, enum memmodel)
5506 EXP is the call expression. */
5508 static rtx
5509 expand_builtin_atomic_clear (tree exp)
5511 enum machine_mode mode;
5512 rtx mem, ret;
5513 enum memmodel model;
5515 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5516 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5517 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5519 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5520 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5522 error ("invalid memory model for %<__atomic_store%>");
5523 return const0_rtx;
5526 if (HAVE_atomic_clear)
5528 emit_insn (gen_atomic_clear (mem, model));
5529 return const0_rtx;
5532 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5533 Failing that, a store is issued by __atomic_store. The only way this can
5534 fail is if the bool type is larger than a word size. Unlikely, but
5535 handle it anyway for completeness. Assume a single threaded model since
5536 there is no atomic support in this case, and no barriers are required. */
5537 ret = expand_atomic_store (mem, const0_rtx, model, true);
5538 if (!ret)
5539 emit_move_insn (mem, const0_rtx);
5540 return const0_rtx;
5543 /* Expand an atomic test_and_set operation.
5544 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5545 EXP is the call expression. */
5547 static rtx
5548 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5550 rtx mem;
5551 enum memmodel model;
5552 enum machine_mode mode;
5554 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5555 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5556 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5558 return expand_atomic_test_and_set (target, mem, model);
5562 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5563 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5565 static tree
5566 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5568 int size;
5569 enum machine_mode mode;
5570 unsigned int mode_align, type_align;
5572 if (TREE_CODE (arg0) != INTEGER_CST)
5573 return NULL_TREE;
5575 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5576 mode = mode_for_size (size, MODE_INT, 0);
5577 mode_align = GET_MODE_ALIGNMENT (mode);
5579 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5580 type_align = mode_align;
5581 else
5583 tree ttype = TREE_TYPE (arg1);
5585 /* This function is usually invoked and folded immediately by the front
5586 end before anything else has a chance to look at it. The pointer
5587 parameter at this point is usually cast to a void *, so check for that
5588 and look past the cast. */
5589 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5590 && VOID_TYPE_P (TREE_TYPE (ttype)))
5591 arg1 = TREE_OPERAND (arg1, 0);
5593 ttype = TREE_TYPE (arg1);
5594 gcc_assert (POINTER_TYPE_P (ttype));
5596 /* Get the underlying type of the object. */
5597 ttype = TREE_TYPE (ttype);
5598 type_align = TYPE_ALIGN (ttype);
5601 /* If the object has smaller alignment, the the lock free routines cannot
5602 be used. */
5603 if (type_align < mode_align)
5604 return boolean_false_node;
5606 /* Check if a compare_and_swap pattern exists for the mode which represents
5607 the required size. The pattern is not allowed to fail, so the existence
5608 of the pattern indicates support is present. */
5609 if (can_compare_and_swap_p (mode, true))
5610 return boolean_true_node;
5611 else
5612 return boolean_false_node;
5615 /* Return true if the parameters to call EXP represent an object which will
5616 always generate lock free instructions. The first argument represents the
5617 size of the object, and the second parameter is a pointer to the object
5618 itself. If NULL is passed for the object, then the result is based on
5619 typical alignment for an object of the specified size. Otherwise return
5620 false. */
5622 static rtx
5623 expand_builtin_atomic_always_lock_free (tree exp)
5625 tree size;
5626 tree arg0 = CALL_EXPR_ARG (exp, 0);
5627 tree arg1 = CALL_EXPR_ARG (exp, 1);
5629 if (TREE_CODE (arg0) != INTEGER_CST)
5631 error ("non-constant argument 1 to __atomic_always_lock_free");
5632 return const0_rtx;
5635 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5636 if (size == boolean_true_node)
5637 return const1_rtx;
5638 return const0_rtx;
5641 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5642 is lock free on this architecture. */
5644 static tree
5645 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5647 if (!flag_inline_atomics)
5648 return NULL_TREE;
5650 /* If it isn't always lock free, don't generate a result. */
5651 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5652 return boolean_true_node;
5654 return NULL_TREE;
5657 /* Return true if the parameters to call EXP represent an object which will
5658 always generate lock free instructions. The first argument represents the
5659 size of the object, and the second parameter is a pointer to the object
5660 itself. If NULL is passed for the object, then the result is based on
5661 typical alignment for an object of the specified size. Otherwise return
5662 NULL*/
5664 static rtx
5665 expand_builtin_atomic_is_lock_free (tree exp)
5667 tree size;
5668 tree arg0 = CALL_EXPR_ARG (exp, 0);
5669 tree arg1 = CALL_EXPR_ARG (exp, 1);
5671 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5673 error ("non-integer argument 1 to __atomic_is_lock_free");
5674 return NULL_RTX;
5677 if (!flag_inline_atomics)
5678 return NULL_RTX;
5680 /* If the value is known at compile time, return the RTX for it. */
5681 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5682 if (size == boolean_true_node)
5683 return const1_rtx;
5685 return NULL_RTX;
5688 /* Expand the __atomic_thread_fence intrinsic:
5689 void __atomic_thread_fence (enum memmodel)
5690 EXP is the CALL_EXPR. */
5692 static void
5693 expand_builtin_atomic_thread_fence (tree exp)
5695 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5696 expand_mem_thread_fence (model);
5699 /* Expand the __atomic_signal_fence intrinsic:
5700 void __atomic_signal_fence (enum memmodel)
5701 EXP is the CALL_EXPR. */
5703 static void
5704 expand_builtin_atomic_signal_fence (tree exp)
5706 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5707 expand_mem_signal_fence (model);
5710 /* Expand the __sync_synchronize intrinsic. */
5712 static void
5713 expand_builtin_sync_synchronize (void)
5715 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5718 static rtx
5719 expand_builtin_thread_pointer (tree exp, rtx target)
5721 enum insn_code icode;
5722 if (!validate_arglist (exp, VOID_TYPE))
5723 return const0_rtx;
5724 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5725 if (icode != CODE_FOR_nothing)
5727 struct expand_operand op;
5728 /* If the target is not sutitable then create a new target. */
5729 if (target == NULL_RTX
5730 || !REG_P (target)
5731 || GET_MODE (target) != Pmode)
5732 target = gen_reg_rtx (Pmode);
5733 create_output_operand (&op, target, Pmode);
5734 expand_insn (icode, 1, &op);
5735 return target;
5737 error ("__builtin_thread_pointer is not supported on this target");
5738 return const0_rtx;
5741 static void
5742 expand_builtin_set_thread_pointer (tree exp)
5744 enum insn_code icode;
5745 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5746 return;
5747 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5748 if (icode != CODE_FOR_nothing)
5750 struct expand_operand op;
5751 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5752 Pmode, EXPAND_NORMAL);
5753 create_input_operand (&op, val, Pmode);
5754 expand_insn (icode, 1, &op);
5755 return;
5757 error ("__builtin_set_thread_pointer is not supported on this target");
5761 /* Emit code to restore the current value of stack. */
5763 static void
5764 expand_stack_restore (tree var)
5766 rtx prev, sa = expand_normal (var);
5768 sa = convert_memory_address (Pmode, sa);
5770 prev = get_last_insn ();
5771 emit_stack_restore (SAVE_BLOCK, sa);
5772 fixup_args_size_notes (prev, get_last_insn (), 0);
5776 /* Emit code to save the current value of stack. */
5778 static rtx
5779 expand_stack_save (void)
5781 rtx ret = NULL_RTX;
5783 do_pending_stack_adjust ();
5784 emit_stack_save (SAVE_BLOCK, &ret);
5785 return ret;
5788 /* Expand an expression EXP that calls a built-in function,
5789 with result going to TARGET if that's convenient
5790 (and in mode MODE if that's convenient).
5791 SUBTARGET may be used as the target for computing one of EXP's operands.
5792 IGNORE is nonzero if the value is to be ignored. */
5795 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5796 int ignore)
5798 tree fndecl = get_callee_fndecl (exp);
5799 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5800 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5801 int flags;
5803 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5804 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5806 /* When not optimizing, generate calls to library functions for a certain
5807 set of builtins. */
5808 if (!optimize
5809 && !called_as_built_in (fndecl)
5810 && fcode != BUILT_IN_FORK
5811 && fcode != BUILT_IN_EXECL
5812 && fcode != BUILT_IN_EXECV
5813 && fcode != BUILT_IN_EXECLP
5814 && fcode != BUILT_IN_EXECLE
5815 && fcode != BUILT_IN_EXECVP
5816 && fcode != BUILT_IN_EXECVE
5817 && fcode != BUILT_IN_ALLOCA
5818 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5819 && fcode != BUILT_IN_FREE)
5820 return expand_call (exp, target, ignore);
5822 /* The built-in function expanders test for target == const0_rtx
5823 to determine whether the function's result will be ignored. */
5824 if (ignore)
5825 target = const0_rtx;
5827 /* If the result of a pure or const built-in function is ignored, and
5828 none of its arguments are volatile, we can avoid expanding the
5829 built-in call and just evaluate the arguments for side-effects. */
5830 if (target == const0_rtx
5831 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5832 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5834 bool volatilep = false;
5835 tree arg;
5836 call_expr_arg_iterator iter;
5838 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5839 if (TREE_THIS_VOLATILE (arg))
5841 volatilep = true;
5842 break;
5845 if (! volatilep)
5847 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5848 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5849 return const0_rtx;
5853 switch (fcode)
5855 CASE_FLT_FN (BUILT_IN_FABS):
5856 case BUILT_IN_FABSD32:
5857 case BUILT_IN_FABSD64:
5858 case BUILT_IN_FABSD128:
5859 target = expand_builtin_fabs (exp, target, subtarget);
5860 if (target)
5861 return target;
5862 break;
5864 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5865 target = expand_builtin_copysign (exp, target, subtarget);
5866 if (target)
5867 return target;
5868 break;
5870 /* Just do a normal library call if we were unable to fold
5871 the values. */
5872 CASE_FLT_FN (BUILT_IN_CABS):
5873 break;
5875 CASE_FLT_FN (BUILT_IN_EXP):
5876 CASE_FLT_FN (BUILT_IN_EXP10):
5877 CASE_FLT_FN (BUILT_IN_POW10):
5878 CASE_FLT_FN (BUILT_IN_EXP2):
5879 CASE_FLT_FN (BUILT_IN_EXPM1):
5880 CASE_FLT_FN (BUILT_IN_LOGB):
5881 CASE_FLT_FN (BUILT_IN_LOG):
5882 CASE_FLT_FN (BUILT_IN_LOG10):
5883 CASE_FLT_FN (BUILT_IN_LOG2):
5884 CASE_FLT_FN (BUILT_IN_LOG1P):
5885 CASE_FLT_FN (BUILT_IN_TAN):
5886 CASE_FLT_FN (BUILT_IN_ASIN):
5887 CASE_FLT_FN (BUILT_IN_ACOS):
5888 CASE_FLT_FN (BUILT_IN_ATAN):
5889 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5890 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5891 because of possible accuracy problems. */
5892 if (! flag_unsafe_math_optimizations)
5893 break;
5894 CASE_FLT_FN (BUILT_IN_SQRT):
5895 CASE_FLT_FN (BUILT_IN_FLOOR):
5896 CASE_FLT_FN (BUILT_IN_CEIL):
5897 CASE_FLT_FN (BUILT_IN_TRUNC):
5898 CASE_FLT_FN (BUILT_IN_ROUND):
5899 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5900 CASE_FLT_FN (BUILT_IN_RINT):
5901 target = expand_builtin_mathfn (exp, target, subtarget);
5902 if (target)
5903 return target;
5904 break;
5906 CASE_FLT_FN (BUILT_IN_FMA):
5907 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5908 if (target)
5909 return target;
5910 break;
5912 CASE_FLT_FN (BUILT_IN_ILOGB):
5913 if (! flag_unsafe_math_optimizations)
5914 break;
5915 CASE_FLT_FN (BUILT_IN_ISINF):
5916 CASE_FLT_FN (BUILT_IN_FINITE):
5917 case BUILT_IN_ISFINITE:
5918 case BUILT_IN_ISNORMAL:
5919 target = expand_builtin_interclass_mathfn (exp, target);
5920 if (target)
5921 return target;
5922 break;
5924 CASE_FLT_FN (BUILT_IN_ICEIL):
5925 CASE_FLT_FN (BUILT_IN_LCEIL):
5926 CASE_FLT_FN (BUILT_IN_LLCEIL):
5927 CASE_FLT_FN (BUILT_IN_LFLOOR):
5928 CASE_FLT_FN (BUILT_IN_IFLOOR):
5929 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5930 target = expand_builtin_int_roundingfn (exp, target);
5931 if (target)
5932 return target;
5933 break;
5935 CASE_FLT_FN (BUILT_IN_IRINT):
5936 CASE_FLT_FN (BUILT_IN_LRINT):
5937 CASE_FLT_FN (BUILT_IN_LLRINT):
5938 CASE_FLT_FN (BUILT_IN_IROUND):
5939 CASE_FLT_FN (BUILT_IN_LROUND):
5940 CASE_FLT_FN (BUILT_IN_LLROUND):
5941 target = expand_builtin_int_roundingfn_2 (exp, target);
5942 if (target)
5943 return target;
5944 break;
5946 CASE_FLT_FN (BUILT_IN_POWI):
5947 target = expand_builtin_powi (exp, target);
5948 if (target)
5949 return target;
5950 break;
5952 CASE_FLT_FN (BUILT_IN_ATAN2):
5953 CASE_FLT_FN (BUILT_IN_LDEXP):
5954 CASE_FLT_FN (BUILT_IN_SCALB):
5955 CASE_FLT_FN (BUILT_IN_SCALBN):
5956 CASE_FLT_FN (BUILT_IN_SCALBLN):
5957 if (! flag_unsafe_math_optimizations)
5958 break;
5960 CASE_FLT_FN (BUILT_IN_FMOD):
5961 CASE_FLT_FN (BUILT_IN_REMAINDER):
5962 CASE_FLT_FN (BUILT_IN_DREM):
5963 CASE_FLT_FN (BUILT_IN_POW):
5964 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5965 if (target)
5966 return target;
5967 break;
5969 CASE_FLT_FN (BUILT_IN_CEXPI):
5970 target = expand_builtin_cexpi (exp, target);
5971 gcc_assert (target);
5972 return target;
5974 CASE_FLT_FN (BUILT_IN_SIN):
5975 CASE_FLT_FN (BUILT_IN_COS):
5976 if (! flag_unsafe_math_optimizations)
5977 break;
5978 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5979 if (target)
5980 return target;
5981 break;
5983 CASE_FLT_FN (BUILT_IN_SINCOS):
5984 if (! flag_unsafe_math_optimizations)
5985 break;
5986 target = expand_builtin_sincos (exp);
5987 if (target)
5988 return target;
5989 break;
5991 case BUILT_IN_APPLY_ARGS:
5992 return expand_builtin_apply_args ();
5994 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5995 FUNCTION with a copy of the parameters described by
5996 ARGUMENTS, and ARGSIZE. It returns a block of memory
5997 allocated on the stack into which is stored all the registers
5998 that might possibly be used for returning the result of a
5999 function. ARGUMENTS is the value returned by
6000 __builtin_apply_args. ARGSIZE is the number of bytes of
6001 arguments that must be copied. ??? How should this value be
6002 computed? We'll also need a safe worst case value for varargs
6003 functions. */
6004 case BUILT_IN_APPLY:
6005 if (!validate_arglist (exp, POINTER_TYPE,
6006 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6007 && !validate_arglist (exp, REFERENCE_TYPE,
6008 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6009 return const0_rtx;
6010 else
6012 rtx ops[3];
6014 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6015 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6016 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6018 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6021 /* __builtin_return (RESULT) causes the function to return the
6022 value described by RESULT. RESULT is address of the block of
6023 memory returned by __builtin_apply. */
6024 case BUILT_IN_RETURN:
6025 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6026 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6027 return const0_rtx;
6029 case BUILT_IN_SAVEREGS:
6030 return expand_builtin_saveregs ();
6032 case BUILT_IN_VA_ARG_PACK:
6033 /* All valid uses of __builtin_va_arg_pack () are removed during
6034 inlining. */
6035 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6036 return const0_rtx;
6038 case BUILT_IN_VA_ARG_PACK_LEN:
6039 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6040 inlining. */
6041 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6042 return const0_rtx;
6044 /* Return the address of the first anonymous stack arg. */
6045 case BUILT_IN_NEXT_ARG:
6046 if (fold_builtin_next_arg (exp, false))
6047 return const0_rtx;
6048 return expand_builtin_next_arg ();
6050 case BUILT_IN_CLEAR_CACHE:
6051 target = expand_builtin___clear_cache (exp);
6052 if (target)
6053 return target;
6054 break;
6056 case BUILT_IN_CLASSIFY_TYPE:
6057 return expand_builtin_classify_type (exp);
6059 case BUILT_IN_CONSTANT_P:
6060 return const0_rtx;
6062 case BUILT_IN_FRAME_ADDRESS:
6063 case BUILT_IN_RETURN_ADDRESS:
6064 return expand_builtin_frame_address (fndecl, exp);
6066 /* Returns the address of the area where the structure is returned.
6067 0 otherwise. */
6068 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6069 if (call_expr_nargs (exp) != 0
6070 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6071 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6072 return const0_rtx;
6073 else
6074 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6076 case BUILT_IN_ALLOCA:
6077 case BUILT_IN_ALLOCA_WITH_ALIGN:
6078 /* If the allocation stems from the declaration of a variable-sized
6079 object, it cannot accumulate. */
6080 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6081 if (target)
6082 return target;
6083 break;
6085 case BUILT_IN_STACK_SAVE:
6086 return expand_stack_save ();
6088 case BUILT_IN_STACK_RESTORE:
6089 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6090 return const0_rtx;
6092 case BUILT_IN_BSWAP16:
6093 case BUILT_IN_BSWAP32:
6094 case BUILT_IN_BSWAP64:
6095 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6096 if (target)
6097 return target;
6098 break;
6100 CASE_INT_FN (BUILT_IN_FFS):
6101 target = expand_builtin_unop (target_mode, exp, target,
6102 subtarget, ffs_optab);
6103 if (target)
6104 return target;
6105 break;
6107 CASE_INT_FN (BUILT_IN_CLZ):
6108 target = expand_builtin_unop (target_mode, exp, target,
6109 subtarget, clz_optab);
6110 if (target)
6111 return target;
6112 break;
6114 CASE_INT_FN (BUILT_IN_CTZ):
6115 target = expand_builtin_unop (target_mode, exp, target,
6116 subtarget, ctz_optab);
6117 if (target)
6118 return target;
6119 break;
6121 CASE_INT_FN (BUILT_IN_CLRSB):
6122 target = expand_builtin_unop (target_mode, exp, target,
6123 subtarget, clrsb_optab);
6124 if (target)
6125 return target;
6126 break;
6128 CASE_INT_FN (BUILT_IN_POPCOUNT):
6129 target = expand_builtin_unop (target_mode, exp, target,
6130 subtarget, popcount_optab);
6131 if (target)
6132 return target;
6133 break;
6135 CASE_INT_FN (BUILT_IN_PARITY):
6136 target = expand_builtin_unop (target_mode, exp, target,
6137 subtarget, parity_optab);
6138 if (target)
6139 return target;
6140 break;
6142 case BUILT_IN_STRLEN:
6143 target = expand_builtin_strlen (exp, target, target_mode);
6144 if (target)
6145 return target;
6146 break;
6148 case BUILT_IN_STRCPY:
6149 target = expand_builtin_strcpy (exp, target);
6150 if (target)
6151 return target;
6152 break;
6154 case BUILT_IN_STRNCPY:
6155 target = expand_builtin_strncpy (exp, target);
6156 if (target)
6157 return target;
6158 break;
6160 case BUILT_IN_STPCPY:
6161 target = expand_builtin_stpcpy (exp, target, mode);
6162 if (target)
6163 return target;
6164 break;
6166 case BUILT_IN_MEMCPY:
6167 target = expand_builtin_memcpy (exp, target);
6168 if (target)
6169 return target;
6170 break;
6172 case BUILT_IN_MEMPCPY:
6173 target = expand_builtin_mempcpy (exp, target, mode);
6174 if (target)
6175 return target;
6176 break;
6178 case BUILT_IN_MEMSET:
6179 target = expand_builtin_memset (exp, target, mode);
6180 if (target)
6181 return target;
6182 break;
6184 case BUILT_IN_BZERO:
6185 target = expand_builtin_bzero (exp);
6186 if (target)
6187 return target;
6188 break;
6190 case BUILT_IN_STRCMP:
6191 target = expand_builtin_strcmp (exp, target);
6192 if (target)
6193 return target;
6194 break;
6196 case BUILT_IN_STRNCMP:
6197 target = expand_builtin_strncmp (exp, target, mode);
6198 if (target)
6199 return target;
6200 break;
6202 case BUILT_IN_BCMP:
6203 case BUILT_IN_MEMCMP:
6204 target = expand_builtin_memcmp (exp, target, mode);
6205 if (target)
6206 return target;
6207 break;
6209 case BUILT_IN_SETJMP:
6210 /* This should have been lowered to the builtins below. */
6211 gcc_unreachable ();
6213 case BUILT_IN_SETJMP_SETUP:
6214 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6215 and the receiver label. */
6216 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6218 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6219 VOIDmode, EXPAND_NORMAL);
6220 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6221 rtx label_r = label_rtx (label);
6223 /* This is copied from the handling of non-local gotos. */
6224 expand_builtin_setjmp_setup (buf_addr, label_r);
6225 nonlocal_goto_handler_labels
6226 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6227 nonlocal_goto_handler_labels);
6228 /* ??? Do not let expand_label treat us as such since we would
6229 not want to be both on the list of non-local labels and on
6230 the list of forced labels. */
6231 FORCED_LABEL (label) = 0;
6232 return const0_rtx;
6234 break;
6236 case BUILT_IN_SETJMP_RECEIVER:
6237 /* __builtin_setjmp_receiver is passed the receiver label. */
6238 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6240 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6241 rtx label_r = label_rtx (label);
6243 expand_builtin_setjmp_receiver (label_r);
6244 return const0_rtx;
6246 break;
6248 /* __builtin_longjmp is passed a pointer to an array of five words.
6249 It's similar to the C library longjmp function but works with
6250 __builtin_setjmp above. */
6251 case BUILT_IN_LONGJMP:
6252 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6254 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6255 VOIDmode, EXPAND_NORMAL);
6256 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6258 if (value != const1_rtx)
6260 error ("%<__builtin_longjmp%> second argument must be 1");
6261 return const0_rtx;
6264 expand_builtin_longjmp (buf_addr, value);
6265 return const0_rtx;
6267 break;
6269 case BUILT_IN_NONLOCAL_GOTO:
6270 target = expand_builtin_nonlocal_goto (exp);
6271 if (target)
6272 return target;
6273 break;
6275 /* This updates the setjmp buffer that is its argument with the value
6276 of the current stack pointer. */
6277 case BUILT_IN_UPDATE_SETJMP_BUF:
6278 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6280 rtx buf_addr
6281 = expand_normal (CALL_EXPR_ARG (exp, 0));
6283 expand_builtin_update_setjmp_buf (buf_addr);
6284 return const0_rtx;
6286 break;
6288 case BUILT_IN_TRAP:
6289 expand_builtin_trap ();
6290 return const0_rtx;
6292 case BUILT_IN_UNREACHABLE:
6293 expand_builtin_unreachable ();
6294 return const0_rtx;
6296 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6297 case BUILT_IN_SIGNBITD32:
6298 case BUILT_IN_SIGNBITD64:
6299 case BUILT_IN_SIGNBITD128:
6300 target = expand_builtin_signbit (exp, target);
6301 if (target)
6302 return target;
6303 break;
6305 /* Various hooks for the DWARF 2 __throw routine. */
6306 case BUILT_IN_UNWIND_INIT:
6307 expand_builtin_unwind_init ();
6308 return const0_rtx;
6309 case BUILT_IN_DWARF_CFA:
6310 return virtual_cfa_rtx;
6311 #ifdef DWARF2_UNWIND_INFO
6312 case BUILT_IN_DWARF_SP_COLUMN:
6313 return expand_builtin_dwarf_sp_column ();
6314 case BUILT_IN_INIT_DWARF_REG_SIZES:
6315 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6316 return const0_rtx;
6317 #endif
6318 case BUILT_IN_FROB_RETURN_ADDR:
6319 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6320 case BUILT_IN_EXTRACT_RETURN_ADDR:
6321 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6322 case BUILT_IN_EH_RETURN:
6323 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6324 CALL_EXPR_ARG (exp, 1));
6325 return const0_rtx;
6326 #ifdef EH_RETURN_DATA_REGNO
6327 case BUILT_IN_EH_RETURN_DATA_REGNO:
6328 return expand_builtin_eh_return_data_regno (exp);
6329 #endif
6330 case BUILT_IN_EXTEND_POINTER:
6331 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6332 case BUILT_IN_EH_POINTER:
6333 return expand_builtin_eh_pointer (exp);
6334 case BUILT_IN_EH_FILTER:
6335 return expand_builtin_eh_filter (exp);
6336 case BUILT_IN_EH_COPY_VALUES:
6337 return expand_builtin_eh_copy_values (exp);
6339 case BUILT_IN_VA_START:
6340 return expand_builtin_va_start (exp);
6341 case BUILT_IN_VA_END:
6342 return expand_builtin_va_end (exp);
6343 case BUILT_IN_VA_COPY:
6344 return expand_builtin_va_copy (exp);
6345 case BUILT_IN_EXPECT:
6346 return expand_builtin_expect (exp, target);
6347 case BUILT_IN_ASSUME_ALIGNED:
6348 return expand_builtin_assume_aligned (exp, target);
6349 case BUILT_IN_PREFETCH:
6350 expand_builtin_prefetch (exp);
6351 return const0_rtx;
6353 case BUILT_IN_INIT_TRAMPOLINE:
6354 return expand_builtin_init_trampoline (exp, true);
6355 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6356 return expand_builtin_init_trampoline (exp, false);
6357 case BUILT_IN_ADJUST_TRAMPOLINE:
6358 return expand_builtin_adjust_trampoline (exp);
6360 case BUILT_IN_FORK:
6361 case BUILT_IN_EXECL:
6362 case BUILT_IN_EXECV:
6363 case BUILT_IN_EXECLP:
6364 case BUILT_IN_EXECLE:
6365 case BUILT_IN_EXECVP:
6366 case BUILT_IN_EXECVE:
6367 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6368 if (target)
6369 return target;
6370 break;
6372 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6373 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6374 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6375 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6376 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6377 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6378 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6379 if (target)
6380 return target;
6381 break;
6383 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6384 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6385 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6386 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6387 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6388 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6389 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6390 if (target)
6391 return target;
6392 break;
6394 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6395 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6396 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6397 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6398 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6399 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6400 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6401 if (target)
6402 return target;
6403 break;
6405 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6406 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6407 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6408 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6409 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6410 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6411 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6412 if (target)
6413 return target;
6414 break;
6416 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6417 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6418 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6419 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6420 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6421 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6422 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6423 if (target)
6424 return target;
6425 break;
6427 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6428 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6429 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6430 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6431 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6432 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6433 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6434 if (target)
6435 return target;
6436 break;
6438 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6439 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6440 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6441 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6442 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6443 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6444 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6445 if (target)
6446 return target;
6447 break;
6449 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6450 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6451 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6452 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6453 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6454 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6455 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6456 if (target)
6457 return target;
6458 break;
6460 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6461 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6462 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6463 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6464 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6465 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6466 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6467 if (target)
6468 return target;
6469 break;
6471 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6472 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6473 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6474 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6475 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6476 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6477 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6478 if (target)
6479 return target;
6480 break;
6482 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6483 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6484 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6485 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6486 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6487 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6488 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6489 if (target)
6490 return target;
6491 break;
6493 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6494 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6495 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6496 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6497 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6498 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6499 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6500 if (target)
6501 return target;
6502 break;
6504 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6505 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6506 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6507 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6508 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6509 if (mode == VOIDmode)
6510 mode = TYPE_MODE (boolean_type_node);
6511 if (!target || !register_operand (target, mode))
6512 target = gen_reg_rtx (mode);
6514 mode = get_builtin_sync_mode
6515 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6516 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6517 if (target)
6518 return target;
6519 break;
6521 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6522 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6523 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6524 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6525 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6526 mode = get_builtin_sync_mode
6527 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6528 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6529 if (target)
6530 return target;
6531 break;
6533 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6534 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6535 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6536 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6537 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6538 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6539 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6540 if (target)
6541 return target;
6542 break;
6544 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6545 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6546 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6547 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6548 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6549 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6550 expand_builtin_sync_lock_release (mode, exp);
6551 return const0_rtx;
6553 case BUILT_IN_SYNC_SYNCHRONIZE:
6554 expand_builtin_sync_synchronize ();
6555 return const0_rtx;
6557 case BUILT_IN_ATOMIC_EXCHANGE_1:
6558 case BUILT_IN_ATOMIC_EXCHANGE_2:
6559 case BUILT_IN_ATOMIC_EXCHANGE_4:
6560 case BUILT_IN_ATOMIC_EXCHANGE_8:
6561 case BUILT_IN_ATOMIC_EXCHANGE_16:
6562 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6563 target = expand_builtin_atomic_exchange (mode, exp, target);
6564 if (target)
6565 return target;
6566 break;
6568 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6569 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6570 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6571 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6572 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6574 unsigned int nargs, z;
6575 vec<tree, va_gc> *vec;
6577 mode =
6578 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6579 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6580 if (target)
6581 return target;
6583 /* If this is turned into an external library call, the weak parameter
6584 must be dropped to match the expected parameter list. */
6585 nargs = call_expr_nargs (exp);
6586 vec_alloc (vec, nargs - 1);
6587 for (z = 0; z < 3; z++)
6588 vec->quick_push (CALL_EXPR_ARG (exp, z));
6589 /* Skip the boolean weak parameter. */
6590 for (z = 4; z < 6; z++)
6591 vec->quick_push (CALL_EXPR_ARG (exp, z));
6592 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6593 break;
6596 case BUILT_IN_ATOMIC_LOAD_1:
6597 case BUILT_IN_ATOMIC_LOAD_2:
6598 case BUILT_IN_ATOMIC_LOAD_4:
6599 case BUILT_IN_ATOMIC_LOAD_8:
6600 case BUILT_IN_ATOMIC_LOAD_16:
6601 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6602 target = expand_builtin_atomic_load (mode, exp, target);
6603 if (target)
6604 return target;
6605 break;
6607 case BUILT_IN_ATOMIC_STORE_1:
6608 case BUILT_IN_ATOMIC_STORE_2:
6609 case BUILT_IN_ATOMIC_STORE_4:
6610 case BUILT_IN_ATOMIC_STORE_8:
6611 case BUILT_IN_ATOMIC_STORE_16:
6612 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6613 target = expand_builtin_atomic_store (mode, exp);
6614 if (target)
6615 return const0_rtx;
6616 break;
6618 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6619 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6620 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6621 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6622 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6624 enum built_in_function lib;
6625 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6626 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6627 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6628 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6629 ignore, lib);
6630 if (target)
6631 return target;
6632 break;
6634 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6635 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6636 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6637 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6638 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6640 enum built_in_function lib;
6641 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6642 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6643 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6644 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6645 ignore, lib);
6646 if (target)
6647 return target;
6648 break;
6650 case BUILT_IN_ATOMIC_AND_FETCH_1:
6651 case BUILT_IN_ATOMIC_AND_FETCH_2:
6652 case BUILT_IN_ATOMIC_AND_FETCH_4:
6653 case BUILT_IN_ATOMIC_AND_FETCH_8:
6654 case BUILT_IN_ATOMIC_AND_FETCH_16:
6656 enum built_in_function lib;
6657 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6658 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6659 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6660 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6661 ignore, lib);
6662 if (target)
6663 return target;
6664 break;
6666 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6667 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6668 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6669 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6670 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6672 enum built_in_function lib;
6673 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6674 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6675 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6676 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6677 ignore, lib);
6678 if (target)
6679 return target;
6680 break;
6682 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6683 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6684 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6685 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6686 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6688 enum built_in_function lib;
6689 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6690 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6691 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6692 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6693 ignore, lib);
6694 if (target)
6695 return target;
6696 break;
6698 case BUILT_IN_ATOMIC_OR_FETCH_1:
6699 case BUILT_IN_ATOMIC_OR_FETCH_2:
6700 case BUILT_IN_ATOMIC_OR_FETCH_4:
6701 case BUILT_IN_ATOMIC_OR_FETCH_8:
6702 case BUILT_IN_ATOMIC_OR_FETCH_16:
6704 enum built_in_function lib;
6705 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6706 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6707 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6708 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6709 ignore, lib);
6710 if (target)
6711 return target;
6712 break;
6714 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6715 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6716 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6717 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6718 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6719 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6720 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6721 ignore, BUILT_IN_NONE);
6722 if (target)
6723 return target;
6724 break;
6726 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6727 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6728 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6729 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6730 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6731 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6732 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6733 ignore, BUILT_IN_NONE);
6734 if (target)
6735 return target;
6736 break;
6738 case BUILT_IN_ATOMIC_FETCH_AND_1:
6739 case BUILT_IN_ATOMIC_FETCH_AND_2:
6740 case BUILT_IN_ATOMIC_FETCH_AND_4:
6741 case BUILT_IN_ATOMIC_FETCH_AND_8:
6742 case BUILT_IN_ATOMIC_FETCH_AND_16:
6743 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6744 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6745 ignore, BUILT_IN_NONE);
6746 if (target)
6747 return target;
6748 break;
6750 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6751 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6752 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6753 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6754 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6755 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6756 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6757 ignore, BUILT_IN_NONE);
6758 if (target)
6759 return target;
6760 break;
6762 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6763 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6764 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6765 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6766 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6767 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6768 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6769 ignore, BUILT_IN_NONE);
6770 if (target)
6771 return target;
6772 break;
6774 case BUILT_IN_ATOMIC_FETCH_OR_1:
6775 case BUILT_IN_ATOMIC_FETCH_OR_2:
6776 case BUILT_IN_ATOMIC_FETCH_OR_4:
6777 case BUILT_IN_ATOMIC_FETCH_OR_8:
6778 case BUILT_IN_ATOMIC_FETCH_OR_16:
6779 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6780 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6781 ignore, BUILT_IN_NONE);
6782 if (target)
6783 return target;
6784 break;
6786 case BUILT_IN_ATOMIC_TEST_AND_SET:
6787 return expand_builtin_atomic_test_and_set (exp, target);
6789 case BUILT_IN_ATOMIC_CLEAR:
6790 return expand_builtin_atomic_clear (exp);
6792 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6793 return expand_builtin_atomic_always_lock_free (exp);
6795 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6796 target = expand_builtin_atomic_is_lock_free (exp);
6797 if (target)
6798 return target;
6799 break;
6801 case BUILT_IN_ATOMIC_THREAD_FENCE:
6802 expand_builtin_atomic_thread_fence (exp);
6803 return const0_rtx;
6805 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6806 expand_builtin_atomic_signal_fence (exp);
6807 return const0_rtx;
6809 case BUILT_IN_OBJECT_SIZE:
6810 return expand_builtin_object_size (exp);
6812 case BUILT_IN_MEMCPY_CHK:
6813 case BUILT_IN_MEMPCPY_CHK:
6814 case BUILT_IN_MEMMOVE_CHK:
6815 case BUILT_IN_MEMSET_CHK:
6816 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6817 if (target)
6818 return target;
6819 break;
6821 case BUILT_IN_STRCPY_CHK:
6822 case BUILT_IN_STPCPY_CHK:
6823 case BUILT_IN_STRNCPY_CHK:
6824 case BUILT_IN_STPNCPY_CHK:
6825 case BUILT_IN_STRCAT_CHK:
6826 case BUILT_IN_STRNCAT_CHK:
6827 case BUILT_IN_SNPRINTF_CHK:
6828 case BUILT_IN_VSNPRINTF_CHK:
6829 maybe_emit_chk_warning (exp, fcode);
6830 break;
6832 case BUILT_IN_SPRINTF_CHK:
6833 case BUILT_IN_VSPRINTF_CHK:
6834 maybe_emit_sprintf_chk_warning (exp, fcode);
6835 break;
6837 case BUILT_IN_FREE:
6838 if (warn_free_nonheap_object)
6839 maybe_emit_free_warning (exp);
6840 break;
6842 case BUILT_IN_THREAD_POINTER:
6843 return expand_builtin_thread_pointer (exp, target);
6845 case BUILT_IN_SET_THREAD_POINTER:
6846 expand_builtin_set_thread_pointer (exp);
6847 return const0_rtx;
6849 case BUILT_IN_CILK_DETACH:
6850 expand_builtin_cilk_detach (exp);
6851 return const0_rtx;
6853 case BUILT_IN_CILK_POP_FRAME:
6854 expand_builtin_cilk_pop_frame (exp);
6855 return const0_rtx;
6857 default: /* just do library call, if unknown builtin */
6858 break;
6861 /* The switch statement above can drop through to cause the function
6862 to be called normally. */
6863 return expand_call (exp, target, ignore);
6866 /* Determine whether a tree node represents a call to a built-in
6867 function. If the tree T is a call to a built-in function with
6868 the right number of arguments of the appropriate types, return
6869 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6870 Otherwise the return value is END_BUILTINS. */
6872 enum built_in_function
6873 builtin_mathfn_code (const_tree t)
6875 const_tree fndecl, arg, parmlist;
6876 const_tree argtype, parmtype;
6877 const_call_expr_arg_iterator iter;
6879 if (TREE_CODE (t) != CALL_EXPR
6880 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6881 return END_BUILTINS;
6883 fndecl = get_callee_fndecl (t);
6884 if (fndecl == NULL_TREE
6885 || TREE_CODE (fndecl) != FUNCTION_DECL
6886 || ! DECL_BUILT_IN (fndecl)
6887 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6888 return END_BUILTINS;
6890 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6891 init_const_call_expr_arg_iterator (t, &iter);
6892 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6894 /* If a function doesn't take a variable number of arguments,
6895 the last element in the list will have type `void'. */
6896 parmtype = TREE_VALUE (parmlist);
6897 if (VOID_TYPE_P (parmtype))
6899 if (more_const_call_expr_args_p (&iter))
6900 return END_BUILTINS;
6901 return DECL_FUNCTION_CODE (fndecl);
6904 if (! more_const_call_expr_args_p (&iter))
6905 return END_BUILTINS;
6907 arg = next_const_call_expr_arg (&iter);
6908 argtype = TREE_TYPE (arg);
6910 if (SCALAR_FLOAT_TYPE_P (parmtype))
6912 if (! SCALAR_FLOAT_TYPE_P (argtype))
6913 return END_BUILTINS;
6915 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6917 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6918 return END_BUILTINS;
6920 else if (POINTER_TYPE_P (parmtype))
6922 if (! POINTER_TYPE_P (argtype))
6923 return END_BUILTINS;
6925 else if (INTEGRAL_TYPE_P (parmtype))
6927 if (! INTEGRAL_TYPE_P (argtype))
6928 return END_BUILTINS;
6930 else
6931 return END_BUILTINS;
6934 /* Variable-length argument list. */
6935 return DECL_FUNCTION_CODE (fndecl);
6938 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6939 evaluate to a constant. */
6941 static tree
6942 fold_builtin_constant_p (tree arg)
6944 /* We return 1 for a numeric type that's known to be a constant
6945 value at compile-time or for an aggregate type that's a
6946 literal constant. */
6947 STRIP_NOPS (arg);
6949 /* If we know this is a constant, emit the constant of one. */
6950 if (CONSTANT_CLASS_P (arg)
6951 || (TREE_CODE (arg) == CONSTRUCTOR
6952 && TREE_CONSTANT (arg)))
6953 return integer_one_node;
6954 if (TREE_CODE (arg) == ADDR_EXPR)
6956 tree op = TREE_OPERAND (arg, 0);
6957 if (TREE_CODE (op) == STRING_CST
6958 || (TREE_CODE (op) == ARRAY_REF
6959 && integer_zerop (TREE_OPERAND (op, 1))
6960 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6961 return integer_one_node;
6964 /* If this expression has side effects, show we don't know it to be a
6965 constant. Likewise if it's a pointer or aggregate type since in
6966 those case we only want literals, since those are only optimized
6967 when generating RTL, not later.
6968 And finally, if we are compiling an initializer, not code, we
6969 need to return a definite result now; there's not going to be any
6970 more optimization done. */
6971 if (TREE_SIDE_EFFECTS (arg)
6972 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6973 || POINTER_TYPE_P (TREE_TYPE (arg))
6974 || cfun == 0
6975 || folding_initializer
6976 || force_folding_builtin_constant_p)
6977 return integer_zero_node;
6979 return NULL_TREE;
6982 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6983 return it as a truthvalue. */
6985 static tree
6986 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
6987 tree predictor)
6989 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6991 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6992 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6993 ret_type = TREE_TYPE (TREE_TYPE (fn));
6994 pred_type = TREE_VALUE (arg_types);
6995 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6997 pred = fold_convert_loc (loc, pred_type, pred);
6998 expected = fold_convert_loc (loc, expected_type, expected);
6999 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
7000 predictor);
7002 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7003 build_int_cst (ret_type, 0));
7006 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7007 NULL_TREE if no simplification is possible. */
7009 tree
7010 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
7012 tree inner, fndecl, inner_arg0;
7013 enum tree_code code;
7015 /* Distribute the expected value over short-circuiting operators.
7016 See through the cast from truthvalue_type_node to long. */
7017 inner_arg0 = arg0;
7018 while (TREE_CODE (inner_arg0) == NOP_EXPR
7019 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7020 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7021 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7023 /* If this is a builtin_expect within a builtin_expect keep the
7024 inner one. See through a comparison against a constant. It
7025 might have been added to create a thruthvalue. */
7026 inner = inner_arg0;
7028 if (COMPARISON_CLASS_P (inner)
7029 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7030 inner = TREE_OPERAND (inner, 0);
7032 if (TREE_CODE (inner) == CALL_EXPR
7033 && (fndecl = get_callee_fndecl (inner))
7034 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7035 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7036 return arg0;
7038 inner = inner_arg0;
7039 code = TREE_CODE (inner);
7040 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7042 tree op0 = TREE_OPERAND (inner, 0);
7043 tree op1 = TREE_OPERAND (inner, 1);
7045 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
7046 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
7047 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7049 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7052 /* If the argument isn't invariant then there's nothing else we can do. */
7053 if (!TREE_CONSTANT (inner_arg0))
7054 return NULL_TREE;
7056 /* If we expect that a comparison against the argument will fold to
7057 a constant return the constant. In practice, this means a true
7058 constant or the address of a non-weak symbol. */
7059 inner = inner_arg0;
7060 STRIP_NOPS (inner);
7061 if (TREE_CODE (inner) == ADDR_EXPR)
7065 inner = TREE_OPERAND (inner, 0);
7067 while (TREE_CODE (inner) == COMPONENT_REF
7068 || TREE_CODE (inner) == ARRAY_REF);
7069 if ((TREE_CODE (inner) == VAR_DECL
7070 || TREE_CODE (inner) == FUNCTION_DECL)
7071 && DECL_WEAK (inner))
7072 return NULL_TREE;
7075 /* Otherwise, ARG0 already has the proper type for the return value. */
7076 return arg0;
7079 /* Fold a call to __builtin_classify_type with argument ARG. */
7081 static tree
7082 fold_builtin_classify_type (tree arg)
7084 if (arg == 0)
7085 return build_int_cst (integer_type_node, no_type_class);
7087 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7090 /* Fold a call to __builtin_strlen with argument ARG. */
7092 static tree
7093 fold_builtin_strlen (location_t loc, tree type, tree arg)
7095 if (!validate_arg (arg, POINTER_TYPE))
7096 return NULL_TREE;
7097 else
7099 tree len = c_strlen (arg, 0);
7101 if (len)
7102 return fold_convert_loc (loc, type, len);
7104 return NULL_TREE;
7108 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7110 static tree
7111 fold_builtin_inf (location_t loc, tree type, int warn)
7113 REAL_VALUE_TYPE real;
7115 /* __builtin_inff is intended to be usable to define INFINITY on all
7116 targets. If an infinity is not available, INFINITY expands "to a
7117 positive constant of type float that overflows at translation
7118 time", footnote "In this case, using INFINITY will violate the
7119 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7120 Thus we pedwarn to ensure this constraint violation is
7121 diagnosed. */
7122 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7123 pedwarn (loc, 0, "target format does not support infinity");
7125 real_inf (&real);
7126 return build_real (type, real);
7129 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7131 static tree
7132 fold_builtin_nan (tree arg, tree type, int quiet)
7134 REAL_VALUE_TYPE real;
7135 const char *str;
7137 if (!validate_arg (arg, POINTER_TYPE))
7138 return NULL_TREE;
7139 str = c_getstr (arg);
7140 if (!str)
7141 return NULL_TREE;
7143 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7144 return NULL_TREE;
7146 return build_real (type, real);
7149 /* Return true if the floating point expression T has an integer value.
7150 We also allow +Inf, -Inf and NaN to be considered integer values. */
7152 static bool
7153 integer_valued_real_p (tree t)
7155 switch (TREE_CODE (t))
7157 case FLOAT_EXPR:
7158 return true;
7160 case ABS_EXPR:
7161 case SAVE_EXPR:
7162 return integer_valued_real_p (TREE_OPERAND (t, 0));
7164 case COMPOUND_EXPR:
7165 case MODIFY_EXPR:
7166 case BIND_EXPR:
7167 return integer_valued_real_p (TREE_OPERAND (t, 1));
7169 case PLUS_EXPR:
7170 case MINUS_EXPR:
7171 case MULT_EXPR:
7172 case MIN_EXPR:
7173 case MAX_EXPR:
7174 return integer_valued_real_p (TREE_OPERAND (t, 0))
7175 && integer_valued_real_p (TREE_OPERAND (t, 1));
7177 case COND_EXPR:
7178 return integer_valued_real_p (TREE_OPERAND (t, 1))
7179 && integer_valued_real_p (TREE_OPERAND (t, 2));
7181 case REAL_CST:
7182 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7184 case NOP_EXPR:
7186 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7187 if (TREE_CODE (type) == INTEGER_TYPE)
7188 return true;
7189 if (TREE_CODE (type) == REAL_TYPE)
7190 return integer_valued_real_p (TREE_OPERAND (t, 0));
7191 break;
7194 case CALL_EXPR:
7195 switch (builtin_mathfn_code (t))
7197 CASE_FLT_FN (BUILT_IN_CEIL):
7198 CASE_FLT_FN (BUILT_IN_FLOOR):
7199 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7200 CASE_FLT_FN (BUILT_IN_RINT):
7201 CASE_FLT_FN (BUILT_IN_ROUND):
7202 CASE_FLT_FN (BUILT_IN_TRUNC):
7203 return true;
7205 CASE_FLT_FN (BUILT_IN_FMIN):
7206 CASE_FLT_FN (BUILT_IN_FMAX):
7207 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7208 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7210 default:
7211 break;
7213 break;
7215 default:
7216 break;
7218 return false;
7221 /* FNDECL is assumed to be a builtin where truncation can be propagated
7222 across (for instance floor((double)f) == (double)floorf (f).
7223 Do the transformation for a call with argument ARG. */
7225 static tree
7226 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7228 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7230 if (!validate_arg (arg, REAL_TYPE))
7231 return NULL_TREE;
7233 /* Integer rounding functions are idempotent. */
7234 if (fcode == builtin_mathfn_code (arg))
7235 return arg;
7237 /* If argument is already integer valued, and we don't need to worry
7238 about setting errno, there's no need to perform rounding. */
7239 if (! flag_errno_math && integer_valued_real_p (arg))
7240 return arg;
7242 if (optimize)
7244 tree arg0 = strip_float_extensions (arg);
7245 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7246 tree newtype = TREE_TYPE (arg0);
7247 tree decl;
7249 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7250 && (decl = mathfn_built_in (newtype, fcode)))
7251 return fold_convert_loc (loc, ftype,
7252 build_call_expr_loc (loc, decl, 1,
7253 fold_convert_loc (loc,
7254 newtype,
7255 arg0)));
7257 return NULL_TREE;
7260 /* FNDECL is assumed to be builtin which can narrow the FP type of
7261 the argument, for instance lround((double)f) -> lroundf (f).
7262 Do the transformation for a call with argument ARG. */
7264 static tree
7265 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7267 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7269 if (!validate_arg (arg, REAL_TYPE))
7270 return NULL_TREE;
7272 /* If argument is already integer valued, and we don't need to worry
7273 about setting errno, there's no need to perform rounding. */
7274 if (! flag_errno_math && integer_valued_real_p (arg))
7275 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7276 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7278 if (optimize)
7280 tree ftype = TREE_TYPE (arg);
7281 tree arg0 = strip_float_extensions (arg);
7282 tree newtype = TREE_TYPE (arg0);
7283 tree decl;
7285 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7286 && (decl = mathfn_built_in (newtype, fcode)))
7287 return build_call_expr_loc (loc, decl, 1,
7288 fold_convert_loc (loc, newtype, arg0));
7291 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7292 sizeof (int) == sizeof (long). */
7293 if (TYPE_PRECISION (integer_type_node)
7294 == TYPE_PRECISION (long_integer_type_node))
7296 tree newfn = NULL_TREE;
7297 switch (fcode)
7299 CASE_FLT_FN (BUILT_IN_ICEIL):
7300 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7301 break;
7303 CASE_FLT_FN (BUILT_IN_IFLOOR):
7304 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7305 break;
7307 CASE_FLT_FN (BUILT_IN_IROUND):
7308 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7309 break;
7311 CASE_FLT_FN (BUILT_IN_IRINT):
7312 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7313 break;
7315 default:
7316 break;
7319 if (newfn)
7321 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7322 return fold_convert_loc (loc,
7323 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7327 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7328 sizeof (long long) == sizeof (long). */
7329 if (TYPE_PRECISION (long_long_integer_type_node)
7330 == TYPE_PRECISION (long_integer_type_node))
7332 tree newfn = NULL_TREE;
7333 switch (fcode)
7335 CASE_FLT_FN (BUILT_IN_LLCEIL):
7336 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7337 break;
7339 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7340 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7341 break;
7343 CASE_FLT_FN (BUILT_IN_LLROUND):
7344 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7345 break;
7347 CASE_FLT_FN (BUILT_IN_LLRINT):
7348 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7349 break;
7351 default:
7352 break;
7355 if (newfn)
7357 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7358 return fold_convert_loc (loc,
7359 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7363 return NULL_TREE;
7366 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7367 return type. Return NULL_TREE if no simplification can be made. */
7369 static tree
7370 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7372 tree res;
7374 if (!validate_arg (arg, COMPLEX_TYPE)
7375 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7376 return NULL_TREE;
7378 /* Calculate the result when the argument is a constant. */
7379 if (TREE_CODE (arg) == COMPLEX_CST
7380 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7381 type, mpfr_hypot)))
7382 return res;
7384 if (TREE_CODE (arg) == COMPLEX_EXPR)
7386 tree real = TREE_OPERAND (arg, 0);
7387 tree imag = TREE_OPERAND (arg, 1);
7389 /* If either part is zero, cabs is fabs of the other. */
7390 if (real_zerop (real))
7391 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7392 if (real_zerop (imag))
7393 return fold_build1_loc (loc, ABS_EXPR, type, real);
7395 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7396 if (flag_unsafe_math_optimizations
7397 && operand_equal_p (real, imag, OEP_PURE_SAME))
7399 const REAL_VALUE_TYPE sqrt2_trunc
7400 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7401 STRIP_NOPS (real);
7402 return fold_build2_loc (loc, MULT_EXPR, type,
7403 fold_build1_loc (loc, ABS_EXPR, type, real),
7404 build_real (type, sqrt2_trunc));
7408 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7409 if (TREE_CODE (arg) == NEGATE_EXPR
7410 || TREE_CODE (arg) == CONJ_EXPR)
7411 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7413 /* Don't do this when optimizing for size. */
7414 if (flag_unsafe_math_optimizations
7415 && optimize && optimize_function_for_speed_p (cfun))
7417 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7419 if (sqrtfn != NULL_TREE)
7421 tree rpart, ipart, result;
7423 arg = builtin_save_expr (arg);
7425 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7426 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7428 rpart = builtin_save_expr (rpart);
7429 ipart = builtin_save_expr (ipart);
7431 result = fold_build2_loc (loc, PLUS_EXPR, type,
7432 fold_build2_loc (loc, MULT_EXPR, type,
7433 rpart, rpart),
7434 fold_build2_loc (loc, MULT_EXPR, type,
7435 ipart, ipart));
7437 return build_call_expr_loc (loc, sqrtfn, 1, result);
7441 return NULL_TREE;
7444 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7445 complex tree type of the result. If NEG is true, the imaginary
7446 zero is negative. */
7448 static tree
7449 build_complex_cproj (tree type, bool neg)
7451 REAL_VALUE_TYPE rinf, rzero = dconst0;
7453 real_inf (&rinf);
7454 rzero.sign = neg;
7455 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7456 build_real (TREE_TYPE (type), rzero));
7459 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7460 return type. Return NULL_TREE if no simplification can be made. */
7462 static tree
7463 fold_builtin_cproj (location_t loc, tree arg, tree type)
7465 if (!validate_arg (arg, COMPLEX_TYPE)
7466 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7467 return NULL_TREE;
7469 /* If there are no infinities, return arg. */
7470 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7471 return non_lvalue_loc (loc, arg);
7473 /* Calculate the result when the argument is a constant. */
7474 if (TREE_CODE (arg) == COMPLEX_CST)
7476 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7477 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7479 if (real_isinf (real) || real_isinf (imag))
7480 return build_complex_cproj (type, imag->sign);
7481 else
7482 return arg;
7484 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7486 tree real = TREE_OPERAND (arg, 0);
7487 tree imag = TREE_OPERAND (arg, 1);
7489 STRIP_NOPS (real);
7490 STRIP_NOPS (imag);
7492 /* If the real part is inf and the imag part is known to be
7493 nonnegative, return (inf + 0i). Remember side-effects are
7494 possible in the imag part. */
7495 if (TREE_CODE (real) == REAL_CST
7496 && real_isinf (TREE_REAL_CST_PTR (real))
7497 && tree_expr_nonnegative_p (imag))
7498 return omit_one_operand_loc (loc, type,
7499 build_complex_cproj (type, false),
7500 arg);
7502 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7503 Remember side-effects are possible in the real part. */
7504 if (TREE_CODE (imag) == REAL_CST
7505 && real_isinf (TREE_REAL_CST_PTR (imag)))
7506 return
7507 omit_one_operand_loc (loc, type,
7508 build_complex_cproj (type, TREE_REAL_CST_PTR
7509 (imag)->sign), arg);
7512 return NULL_TREE;
7515 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7516 Return NULL_TREE if no simplification can be made. */
7518 static tree
7519 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7522 enum built_in_function fcode;
7523 tree res;
7525 if (!validate_arg (arg, REAL_TYPE))
7526 return NULL_TREE;
7528 /* Calculate the result when the argument is a constant. */
7529 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7530 return res;
7532 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7533 fcode = builtin_mathfn_code (arg);
7534 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7536 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7537 arg = fold_build2_loc (loc, MULT_EXPR, type,
7538 CALL_EXPR_ARG (arg, 0),
7539 build_real (type, dconsthalf));
7540 return build_call_expr_loc (loc, expfn, 1, arg);
7543 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7544 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7546 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7548 if (powfn)
7550 tree arg0 = CALL_EXPR_ARG (arg, 0);
7551 tree tree_root;
7552 /* The inner root was either sqrt or cbrt. */
7553 /* This was a conditional expression but it triggered a bug
7554 in Sun C 5.5. */
7555 REAL_VALUE_TYPE dconstroot;
7556 if (BUILTIN_SQRT_P (fcode))
7557 dconstroot = dconsthalf;
7558 else
7559 dconstroot = dconst_third ();
7561 /* Adjust for the outer root. */
7562 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7563 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7564 tree_root = build_real (type, dconstroot);
7565 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7569 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7570 if (flag_unsafe_math_optimizations
7571 && (fcode == BUILT_IN_POW
7572 || fcode == BUILT_IN_POWF
7573 || fcode == BUILT_IN_POWL))
7575 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7576 tree arg0 = CALL_EXPR_ARG (arg, 0);
7577 tree arg1 = CALL_EXPR_ARG (arg, 1);
7578 tree narg1;
7579 if (!tree_expr_nonnegative_p (arg0))
7580 arg0 = build1 (ABS_EXPR, type, arg0);
7581 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7582 build_real (type, dconsthalf));
7583 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7586 return NULL_TREE;
7589 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7590 Return NULL_TREE if no simplification can be made. */
7592 static tree
7593 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7595 const enum built_in_function fcode = builtin_mathfn_code (arg);
7596 tree res;
7598 if (!validate_arg (arg, REAL_TYPE))
7599 return NULL_TREE;
7601 /* Calculate the result when the argument is a constant. */
7602 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7603 return res;
7605 if (flag_unsafe_math_optimizations)
7607 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7608 if (BUILTIN_EXPONENT_P (fcode))
7610 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7611 const REAL_VALUE_TYPE third_trunc =
7612 real_value_truncate (TYPE_MODE (type), dconst_third ());
7613 arg = fold_build2_loc (loc, MULT_EXPR, type,
7614 CALL_EXPR_ARG (arg, 0),
7615 build_real (type, third_trunc));
7616 return build_call_expr_loc (loc, expfn, 1, arg);
7619 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7620 if (BUILTIN_SQRT_P (fcode))
7622 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7624 if (powfn)
7626 tree arg0 = CALL_EXPR_ARG (arg, 0);
7627 tree tree_root;
7628 REAL_VALUE_TYPE dconstroot = dconst_third ();
7630 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7631 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7632 tree_root = build_real (type, dconstroot);
7633 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7637 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7638 if (BUILTIN_CBRT_P (fcode))
7640 tree arg0 = CALL_EXPR_ARG (arg, 0);
7641 if (tree_expr_nonnegative_p (arg0))
7643 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7645 if (powfn)
7647 tree tree_root;
7648 REAL_VALUE_TYPE dconstroot;
7650 real_arithmetic (&dconstroot, MULT_EXPR,
7651 dconst_third_ptr (), dconst_third_ptr ());
7652 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7653 tree_root = build_real (type, dconstroot);
7654 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7659 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7660 if (fcode == BUILT_IN_POW
7661 || fcode == BUILT_IN_POWF
7662 || fcode == BUILT_IN_POWL)
7664 tree arg00 = CALL_EXPR_ARG (arg, 0);
7665 tree arg01 = CALL_EXPR_ARG (arg, 1);
7666 if (tree_expr_nonnegative_p (arg00))
7668 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7669 const REAL_VALUE_TYPE dconstroot
7670 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7671 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7672 build_real (type, dconstroot));
7673 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7677 return NULL_TREE;
7680 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7681 TYPE is the type of the return value. Return NULL_TREE if no
7682 simplification can be made. */
7684 static tree
7685 fold_builtin_cos (location_t loc,
7686 tree arg, tree type, tree fndecl)
7688 tree res, narg;
7690 if (!validate_arg (arg, REAL_TYPE))
7691 return NULL_TREE;
7693 /* Calculate the result when the argument is a constant. */
7694 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7695 return res;
7697 /* Optimize cos(-x) into cos (x). */
7698 if ((narg = fold_strip_sign_ops (arg)))
7699 return build_call_expr_loc (loc, fndecl, 1, narg);
7701 return NULL_TREE;
7704 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7705 Return NULL_TREE if no simplification can be made. */
7707 static tree
7708 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7710 if (validate_arg (arg, REAL_TYPE))
7712 tree res, narg;
7714 /* Calculate the result when the argument is a constant. */
7715 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7716 return res;
7718 /* Optimize cosh(-x) into cosh (x). */
7719 if ((narg = fold_strip_sign_ops (arg)))
7720 return build_call_expr_loc (loc, fndecl, 1, narg);
7723 return NULL_TREE;
7726 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7727 argument ARG. TYPE is the type of the return value. Return
7728 NULL_TREE if no simplification can be made. */
7730 static tree
7731 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7732 bool hyper)
7734 if (validate_arg (arg, COMPLEX_TYPE)
7735 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7737 tree tmp;
7739 /* Calculate the result when the argument is a constant. */
7740 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7741 return tmp;
7743 /* Optimize fn(-x) into fn(x). */
7744 if ((tmp = fold_strip_sign_ops (arg)))
7745 return build_call_expr_loc (loc, fndecl, 1, tmp);
7748 return NULL_TREE;
7751 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7752 Return NULL_TREE if no simplification can be made. */
7754 static tree
7755 fold_builtin_tan (tree arg, tree type)
7757 enum built_in_function fcode;
7758 tree res;
7760 if (!validate_arg (arg, REAL_TYPE))
7761 return NULL_TREE;
7763 /* Calculate the result when the argument is a constant. */
7764 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7765 return res;
7767 /* Optimize tan(atan(x)) = x. */
7768 fcode = builtin_mathfn_code (arg);
7769 if (flag_unsafe_math_optimizations
7770 && (fcode == BUILT_IN_ATAN
7771 || fcode == BUILT_IN_ATANF
7772 || fcode == BUILT_IN_ATANL))
7773 return CALL_EXPR_ARG (arg, 0);
7775 return NULL_TREE;
7778 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7779 NULL_TREE if no simplification can be made. */
7781 static tree
7782 fold_builtin_sincos (location_t loc,
7783 tree arg0, tree arg1, tree arg2)
7785 tree type;
7786 tree res, fn, call;
7788 if (!validate_arg (arg0, REAL_TYPE)
7789 || !validate_arg (arg1, POINTER_TYPE)
7790 || !validate_arg (arg2, POINTER_TYPE))
7791 return NULL_TREE;
7793 type = TREE_TYPE (arg0);
7795 /* Calculate the result when the argument is a constant. */
7796 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7797 return res;
7799 /* Canonicalize sincos to cexpi. */
7800 if (!targetm.libc_has_function (function_c99_math_complex))
7801 return NULL_TREE;
7802 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7803 if (!fn)
7804 return NULL_TREE;
7806 call = build_call_expr_loc (loc, fn, 1, arg0);
7807 call = builtin_save_expr (call);
7809 return build2 (COMPOUND_EXPR, void_type_node,
7810 build2 (MODIFY_EXPR, void_type_node,
7811 build_fold_indirect_ref_loc (loc, arg1),
7812 build1 (IMAGPART_EXPR, type, call)),
7813 build2 (MODIFY_EXPR, void_type_node,
7814 build_fold_indirect_ref_loc (loc, arg2),
7815 build1 (REALPART_EXPR, type, call)));
7818 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7819 NULL_TREE if no simplification can be made. */
7821 static tree
7822 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7824 tree rtype;
7825 tree realp, imagp, ifn;
7826 tree res;
7828 if (!validate_arg (arg0, COMPLEX_TYPE)
7829 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7830 return NULL_TREE;
7832 /* Calculate the result when the argument is a constant. */
7833 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7834 return res;
7836 rtype = TREE_TYPE (TREE_TYPE (arg0));
7838 /* In case we can figure out the real part of arg0 and it is constant zero
7839 fold to cexpi. */
7840 if (!targetm.libc_has_function (function_c99_math_complex))
7841 return NULL_TREE;
7842 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7843 if (!ifn)
7844 return NULL_TREE;
7846 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7847 && real_zerop (realp))
7849 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7850 return build_call_expr_loc (loc, ifn, 1, narg);
7853 /* In case we can easily decompose real and imaginary parts split cexp
7854 to exp (r) * cexpi (i). */
7855 if (flag_unsafe_math_optimizations
7856 && realp)
7858 tree rfn, rcall, icall;
7860 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7861 if (!rfn)
7862 return NULL_TREE;
7864 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7865 if (!imagp)
7866 return NULL_TREE;
7868 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7869 icall = builtin_save_expr (icall);
7870 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7871 rcall = builtin_save_expr (rcall);
7872 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7873 fold_build2_loc (loc, MULT_EXPR, rtype,
7874 rcall,
7875 fold_build1_loc (loc, REALPART_EXPR,
7876 rtype, icall)),
7877 fold_build2_loc (loc, MULT_EXPR, rtype,
7878 rcall,
7879 fold_build1_loc (loc, IMAGPART_EXPR,
7880 rtype, icall)));
7883 return NULL_TREE;
7886 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7887 Return NULL_TREE if no simplification can be made. */
7889 static tree
7890 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7892 if (!validate_arg (arg, REAL_TYPE))
7893 return NULL_TREE;
7895 /* Optimize trunc of constant value. */
7896 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7898 REAL_VALUE_TYPE r, x;
7899 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7901 x = TREE_REAL_CST (arg);
7902 real_trunc (&r, TYPE_MODE (type), &x);
7903 return build_real (type, r);
7906 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7909 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7910 Return NULL_TREE if no simplification can be made. */
7912 static tree
7913 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7915 if (!validate_arg (arg, REAL_TYPE))
7916 return NULL_TREE;
7918 /* Optimize floor of constant value. */
7919 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7921 REAL_VALUE_TYPE x;
7923 x = TREE_REAL_CST (arg);
7924 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7926 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7927 REAL_VALUE_TYPE r;
7929 real_floor (&r, TYPE_MODE (type), &x);
7930 return build_real (type, r);
7934 /* Fold floor (x) where x is nonnegative to trunc (x). */
7935 if (tree_expr_nonnegative_p (arg))
7937 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7938 if (truncfn)
7939 return build_call_expr_loc (loc, truncfn, 1, arg);
7942 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7945 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7946 Return NULL_TREE if no simplification can be made. */
7948 static tree
7949 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7951 if (!validate_arg (arg, REAL_TYPE))
7952 return NULL_TREE;
7954 /* Optimize ceil of constant value. */
7955 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7957 REAL_VALUE_TYPE x;
7959 x = TREE_REAL_CST (arg);
7960 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7962 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7963 REAL_VALUE_TYPE r;
7965 real_ceil (&r, TYPE_MODE (type), &x);
7966 return build_real (type, r);
7970 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7973 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7974 Return NULL_TREE if no simplification can be made. */
7976 static tree
7977 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7979 if (!validate_arg (arg, REAL_TYPE))
7980 return NULL_TREE;
7982 /* Optimize round of constant value. */
7983 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7985 REAL_VALUE_TYPE x;
7987 x = TREE_REAL_CST (arg);
7988 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7990 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7991 REAL_VALUE_TYPE r;
7993 real_round (&r, TYPE_MODE (type), &x);
7994 return build_real (type, r);
7998 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8001 /* Fold function call to builtin lround, lroundf or lroundl (or the
8002 corresponding long long versions) and other rounding functions. ARG
8003 is the argument to the call. Return NULL_TREE if no simplification
8004 can be made. */
8006 static tree
8007 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8009 if (!validate_arg (arg, REAL_TYPE))
8010 return NULL_TREE;
8012 /* Optimize lround of constant value. */
8013 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8015 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8017 if (real_isfinite (&x))
8019 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8020 tree ftype = TREE_TYPE (arg);
8021 REAL_VALUE_TYPE r;
8022 bool fail = false;
8024 switch (DECL_FUNCTION_CODE (fndecl))
8026 CASE_FLT_FN (BUILT_IN_IFLOOR):
8027 CASE_FLT_FN (BUILT_IN_LFLOOR):
8028 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8029 real_floor (&r, TYPE_MODE (ftype), &x);
8030 break;
8032 CASE_FLT_FN (BUILT_IN_ICEIL):
8033 CASE_FLT_FN (BUILT_IN_LCEIL):
8034 CASE_FLT_FN (BUILT_IN_LLCEIL):
8035 real_ceil (&r, TYPE_MODE (ftype), &x);
8036 break;
8038 CASE_FLT_FN (BUILT_IN_IROUND):
8039 CASE_FLT_FN (BUILT_IN_LROUND):
8040 CASE_FLT_FN (BUILT_IN_LLROUND):
8041 real_round (&r, TYPE_MODE (ftype), &x);
8042 break;
8044 default:
8045 gcc_unreachable ();
8048 wide_int val = real_to_integer (&r, &fail, TYPE_PRECISION (itype));
8049 if (!fail)
8050 return wide_int_to_tree (itype, val);
8054 switch (DECL_FUNCTION_CODE (fndecl))
8056 CASE_FLT_FN (BUILT_IN_LFLOOR):
8057 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8058 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8059 if (tree_expr_nonnegative_p (arg))
8060 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8061 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8062 break;
8063 default:;
8066 return fold_fixed_mathfn (loc, fndecl, arg);
8069 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8070 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8071 the argument to the call. Return NULL_TREE if no simplification can
8072 be made. */
8074 static tree
8075 fold_builtin_bitop (tree fndecl, tree arg)
8077 if (!validate_arg (arg, INTEGER_TYPE))
8078 return NULL_TREE;
8080 /* Optimize for constant argument. */
8081 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8083 tree type = TREE_TYPE (arg);
8084 int result;
8086 switch (DECL_FUNCTION_CODE (fndecl))
8088 CASE_INT_FN (BUILT_IN_FFS):
8089 result = wi::ffs (arg);
8090 break;
8092 CASE_INT_FN (BUILT_IN_CLZ):
8093 if (wi::ne_p (arg, 0))
8094 result = wi::clz (arg);
8095 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8096 result = TYPE_PRECISION (type);
8097 break;
8099 CASE_INT_FN (BUILT_IN_CTZ):
8100 if (wi::ne_p (arg, 0))
8101 result = wi::ctz (arg);
8102 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8103 result = TYPE_PRECISION (type);
8104 break;
8106 CASE_INT_FN (BUILT_IN_CLRSB):
8107 result = wi::clrsb (arg);
8108 break;
8110 CASE_INT_FN (BUILT_IN_POPCOUNT):
8111 result = wi::popcount (arg);
8112 break;
8114 CASE_INT_FN (BUILT_IN_PARITY):
8115 result = wi::parity (arg);
8116 break;
8118 default:
8119 gcc_unreachable ();
8122 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8125 return NULL_TREE;
8128 /* Fold function call to builtin_bswap and the short, long and long long
8129 variants. Return NULL_TREE if no simplification can be made. */
8130 static tree
8131 fold_builtin_bswap (tree fndecl, tree arg)
8133 if (! validate_arg (arg, INTEGER_TYPE))
8134 return NULL_TREE;
8136 /* Optimize constant value. */
8137 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8139 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8141 switch (DECL_FUNCTION_CODE (fndecl))
8143 case BUILT_IN_BSWAP16:
8144 case BUILT_IN_BSWAP32:
8145 case BUILT_IN_BSWAP64:
8147 signop sgn = TYPE_SIGN (type);
8148 tree result =
8149 wide_int_to_tree (type,
8150 wide_int::from (arg, TYPE_PRECISION (type),
8151 sgn).bswap ());
8152 return result;
8154 default:
8155 gcc_unreachable ();
8159 return NULL_TREE;
8162 /* A subroutine of fold_builtin to fold the various logarithmic
8163 functions. Return NULL_TREE if no simplification can me made.
8164 FUNC is the corresponding MPFR logarithm function. */
8166 static tree
8167 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8168 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8170 if (validate_arg (arg, REAL_TYPE))
8172 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8173 tree res;
8174 const enum built_in_function fcode = builtin_mathfn_code (arg);
8176 /* Calculate the result when the argument is a constant. */
8177 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8178 return res;
8180 /* Special case, optimize logN(expN(x)) = x. */
8181 if (flag_unsafe_math_optimizations
8182 && ((func == mpfr_log
8183 && (fcode == BUILT_IN_EXP
8184 || fcode == BUILT_IN_EXPF
8185 || fcode == BUILT_IN_EXPL))
8186 || (func == mpfr_log2
8187 && (fcode == BUILT_IN_EXP2
8188 || fcode == BUILT_IN_EXP2F
8189 || fcode == BUILT_IN_EXP2L))
8190 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8191 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8193 /* Optimize logN(func()) for various exponential functions. We
8194 want to determine the value "x" and the power "exponent" in
8195 order to transform logN(x**exponent) into exponent*logN(x). */
8196 if (flag_unsafe_math_optimizations)
8198 tree exponent = 0, x = 0;
8200 switch (fcode)
8202 CASE_FLT_FN (BUILT_IN_EXP):
8203 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8204 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8205 dconst_e ()));
8206 exponent = CALL_EXPR_ARG (arg, 0);
8207 break;
8208 CASE_FLT_FN (BUILT_IN_EXP2):
8209 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8210 x = build_real (type, dconst2);
8211 exponent = CALL_EXPR_ARG (arg, 0);
8212 break;
8213 CASE_FLT_FN (BUILT_IN_EXP10):
8214 CASE_FLT_FN (BUILT_IN_POW10):
8215 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8217 REAL_VALUE_TYPE dconst10;
8218 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
8219 x = build_real (type, dconst10);
8221 exponent = CALL_EXPR_ARG (arg, 0);
8222 break;
8223 CASE_FLT_FN (BUILT_IN_SQRT):
8224 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8225 x = CALL_EXPR_ARG (arg, 0);
8226 exponent = build_real (type, dconsthalf);
8227 break;
8228 CASE_FLT_FN (BUILT_IN_CBRT):
8229 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8230 x = CALL_EXPR_ARG (arg, 0);
8231 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8232 dconst_third ()));
8233 break;
8234 CASE_FLT_FN (BUILT_IN_POW):
8235 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8236 x = CALL_EXPR_ARG (arg, 0);
8237 exponent = CALL_EXPR_ARG (arg, 1);
8238 break;
8239 default:
8240 break;
8243 /* Now perform the optimization. */
8244 if (x && exponent)
8246 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8247 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8252 return NULL_TREE;
8255 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8256 NULL_TREE if no simplification can be made. */
8258 static tree
8259 fold_builtin_hypot (location_t loc, tree fndecl,
8260 tree arg0, tree arg1, tree type)
8262 tree res, narg0, narg1;
8264 if (!validate_arg (arg0, REAL_TYPE)
8265 || !validate_arg (arg1, REAL_TYPE))
8266 return NULL_TREE;
8268 /* Calculate the result when the argument is a constant. */
8269 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8270 return res;
8272 /* If either argument to hypot has a negate or abs, strip that off.
8273 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8274 narg0 = fold_strip_sign_ops (arg0);
8275 narg1 = fold_strip_sign_ops (arg1);
8276 if (narg0 || narg1)
8278 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8279 narg1 ? narg1 : arg1);
8282 /* If either argument is zero, hypot is fabs of the other. */
8283 if (real_zerop (arg0))
8284 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8285 else if (real_zerop (arg1))
8286 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8288 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8289 if (flag_unsafe_math_optimizations
8290 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8292 const REAL_VALUE_TYPE sqrt2_trunc
8293 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8294 return fold_build2_loc (loc, MULT_EXPR, type,
8295 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8296 build_real (type, sqrt2_trunc));
8299 return NULL_TREE;
8303 /* Fold a builtin function call to pow, powf, or powl. Return
8304 NULL_TREE if no simplification can be made. */
8305 static tree
8306 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8308 tree res;
8310 if (!validate_arg (arg0, REAL_TYPE)
8311 || !validate_arg (arg1, REAL_TYPE))
8312 return NULL_TREE;
8314 /* Calculate the result when the argument is a constant. */
8315 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8316 return res;
8318 /* Optimize pow(1.0,y) = 1.0. */
8319 if (real_onep (arg0))
8320 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8322 if (TREE_CODE (arg1) == REAL_CST
8323 && !TREE_OVERFLOW (arg1))
8325 REAL_VALUE_TYPE cint;
8326 REAL_VALUE_TYPE c;
8327 HOST_WIDE_INT n;
8329 c = TREE_REAL_CST (arg1);
8331 /* Optimize pow(x,0.0) = 1.0. */
8332 if (REAL_VALUES_EQUAL (c, dconst0))
8333 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8334 arg0);
8336 /* Optimize pow(x,1.0) = x. */
8337 if (REAL_VALUES_EQUAL (c, dconst1))
8338 return arg0;
8340 /* Optimize pow(x,-1.0) = 1.0/x. */
8341 if (REAL_VALUES_EQUAL (c, dconstm1))
8342 return fold_build2_loc (loc, RDIV_EXPR, type,
8343 build_real (type, dconst1), arg0);
8345 /* Optimize pow(x,0.5) = sqrt(x). */
8346 if (flag_unsafe_math_optimizations
8347 && REAL_VALUES_EQUAL (c, dconsthalf))
8349 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8351 if (sqrtfn != NULL_TREE)
8352 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8355 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8356 if (flag_unsafe_math_optimizations)
8358 const REAL_VALUE_TYPE dconstroot
8359 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8361 if (REAL_VALUES_EQUAL (c, dconstroot))
8363 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8364 if (cbrtfn != NULL_TREE)
8365 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8369 /* Check for an integer exponent. */
8370 n = real_to_integer (&c);
8371 real_from_integer (&cint, VOIDmode, n, SIGNED);
8372 if (real_identical (&c, &cint))
8374 /* Attempt to evaluate pow at compile-time, unless this should
8375 raise an exception. */
8376 if (TREE_CODE (arg0) == REAL_CST
8377 && !TREE_OVERFLOW (arg0)
8378 && (n > 0
8379 || (!flag_trapping_math && !flag_errno_math)
8380 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8382 REAL_VALUE_TYPE x;
8383 bool inexact;
8385 x = TREE_REAL_CST (arg0);
8386 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8387 if (flag_unsafe_math_optimizations || !inexact)
8388 return build_real (type, x);
8391 /* Strip sign ops from even integer powers. */
8392 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8394 tree narg0 = fold_strip_sign_ops (arg0);
8395 if (narg0)
8396 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8401 if (flag_unsafe_math_optimizations)
8403 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8405 /* Optimize pow(expN(x),y) = expN(x*y). */
8406 if (BUILTIN_EXPONENT_P (fcode))
8408 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8409 tree arg = CALL_EXPR_ARG (arg0, 0);
8410 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8411 return build_call_expr_loc (loc, expfn, 1, arg);
8414 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8415 if (BUILTIN_SQRT_P (fcode))
8417 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8418 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8419 build_real (type, dconsthalf));
8420 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8423 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8424 if (BUILTIN_CBRT_P (fcode))
8426 tree arg = CALL_EXPR_ARG (arg0, 0);
8427 if (tree_expr_nonnegative_p (arg))
8429 const REAL_VALUE_TYPE dconstroot
8430 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8431 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8432 build_real (type, dconstroot));
8433 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8437 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8438 if (fcode == BUILT_IN_POW
8439 || fcode == BUILT_IN_POWF
8440 || fcode == BUILT_IN_POWL)
8442 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8443 if (tree_expr_nonnegative_p (arg00))
8445 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8446 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8447 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8452 return NULL_TREE;
8455 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8456 Return NULL_TREE if no simplification can be made. */
8457 static tree
8458 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8459 tree arg0, tree arg1, tree type)
8461 if (!validate_arg (arg0, REAL_TYPE)
8462 || !validate_arg (arg1, INTEGER_TYPE))
8463 return NULL_TREE;
8465 /* Optimize pow(1.0,y) = 1.0. */
8466 if (real_onep (arg0))
8467 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8469 if (tree_fits_shwi_p (arg1))
8471 HOST_WIDE_INT c = tree_to_shwi (arg1);
8473 /* Evaluate powi at compile-time. */
8474 if (TREE_CODE (arg0) == REAL_CST
8475 && !TREE_OVERFLOW (arg0))
8477 REAL_VALUE_TYPE x;
8478 x = TREE_REAL_CST (arg0);
8479 real_powi (&x, TYPE_MODE (type), &x, c);
8480 return build_real (type, x);
8483 /* Optimize pow(x,0) = 1.0. */
8484 if (c == 0)
8485 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8486 arg0);
8488 /* Optimize pow(x,1) = x. */
8489 if (c == 1)
8490 return arg0;
8492 /* Optimize pow(x,-1) = 1.0/x. */
8493 if (c == -1)
8494 return fold_build2_loc (loc, RDIV_EXPR, type,
8495 build_real (type, dconst1), arg0);
8498 return NULL_TREE;
8501 /* A subroutine of fold_builtin to fold the various exponent
8502 functions. Return NULL_TREE if no simplification can be made.
8503 FUNC is the corresponding MPFR exponent function. */
8505 static tree
8506 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8507 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8509 if (validate_arg (arg, REAL_TYPE))
8511 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8512 tree res;
8514 /* Calculate the result when the argument is a constant. */
8515 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8516 return res;
8518 /* Optimize expN(logN(x)) = x. */
8519 if (flag_unsafe_math_optimizations)
8521 const enum built_in_function fcode = builtin_mathfn_code (arg);
8523 if ((func == mpfr_exp
8524 && (fcode == BUILT_IN_LOG
8525 || fcode == BUILT_IN_LOGF
8526 || fcode == BUILT_IN_LOGL))
8527 || (func == mpfr_exp2
8528 && (fcode == BUILT_IN_LOG2
8529 || fcode == BUILT_IN_LOG2F
8530 || fcode == BUILT_IN_LOG2L))
8531 || (func == mpfr_exp10
8532 && (fcode == BUILT_IN_LOG10
8533 || fcode == BUILT_IN_LOG10F
8534 || fcode == BUILT_IN_LOG10L)))
8535 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8539 return NULL_TREE;
8542 /* Return true if VAR is a VAR_DECL or a component thereof. */
8544 static bool
8545 var_decl_component_p (tree var)
8547 tree inner = var;
8548 while (handled_component_p (inner))
8549 inner = TREE_OPERAND (inner, 0);
8550 return SSA_VAR_P (inner);
8553 /* Fold function call to builtin memset. Return
8554 NULL_TREE if no simplification can be made. */
8556 static tree
8557 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8558 tree type, bool ignore)
8560 tree var, ret, etype;
8561 unsigned HOST_WIDE_INT length, cval;
8563 if (! validate_arg (dest, POINTER_TYPE)
8564 || ! validate_arg (c, INTEGER_TYPE)
8565 || ! validate_arg (len, INTEGER_TYPE))
8566 return NULL_TREE;
8568 if (! tree_fits_uhwi_p (len))
8569 return NULL_TREE;
8571 /* If the LEN parameter is zero, return DEST. */
8572 if (integer_zerop (len))
8573 return omit_one_operand_loc (loc, type, dest, c);
8575 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8576 return NULL_TREE;
8578 var = dest;
8579 STRIP_NOPS (var);
8580 if (TREE_CODE (var) != ADDR_EXPR)
8581 return NULL_TREE;
8583 var = TREE_OPERAND (var, 0);
8584 if (TREE_THIS_VOLATILE (var))
8585 return NULL_TREE;
8587 etype = TREE_TYPE (var);
8588 if (TREE_CODE (etype) == ARRAY_TYPE)
8589 etype = TREE_TYPE (etype);
8591 if (!INTEGRAL_TYPE_P (etype)
8592 && !POINTER_TYPE_P (etype))
8593 return NULL_TREE;
8595 if (! var_decl_component_p (var))
8596 return NULL_TREE;
8598 length = tree_to_uhwi (len);
8599 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8600 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8601 return NULL_TREE;
8603 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8604 return NULL_TREE;
8606 if (integer_zerop (c))
8607 cval = 0;
8608 else
8610 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8611 return NULL_TREE;
8613 cval = TREE_INT_CST_LOW (c);
8614 cval &= 0xff;
8615 cval |= cval << 8;
8616 cval |= cval << 16;
8617 cval |= (cval << 31) << 1;
8620 ret = build_int_cst_type (etype, cval);
8621 var = build_fold_indirect_ref_loc (loc,
8622 fold_convert_loc (loc,
8623 build_pointer_type (etype),
8624 dest));
8625 ret = build2 (MODIFY_EXPR, etype, var, ret);
8626 if (ignore)
8627 return ret;
8629 return omit_one_operand_loc (loc, type, dest, ret);
8632 /* Fold function call to builtin memset. Return
8633 NULL_TREE if no simplification can be made. */
8635 static tree
8636 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8638 if (! validate_arg (dest, POINTER_TYPE)
8639 || ! validate_arg (size, INTEGER_TYPE))
8640 return NULL_TREE;
8642 if (!ignore)
8643 return NULL_TREE;
8645 /* New argument list transforming bzero(ptr x, int y) to
8646 memset(ptr x, int 0, size_t y). This is done this way
8647 so that if it isn't expanded inline, we fallback to
8648 calling bzero instead of memset. */
8650 return fold_builtin_memset (loc, dest, integer_zero_node,
8651 fold_convert_loc (loc, size_type_node, size),
8652 void_type_node, ignore);
8655 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8656 NULL_TREE if no simplification can be made.
8657 If ENDP is 0, return DEST (like memcpy).
8658 If ENDP is 1, return DEST+LEN (like mempcpy).
8659 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8660 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8661 (memmove). */
8663 static tree
8664 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8665 tree len, tree type, bool ignore, int endp)
8667 tree destvar, srcvar, expr;
8669 if (! validate_arg (dest, POINTER_TYPE)
8670 || ! validate_arg (src, POINTER_TYPE)
8671 || ! validate_arg (len, INTEGER_TYPE))
8672 return NULL_TREE;
8674 /* If the LEN parameter is zero, return DEST. */
8675 if (integer_zerop (len))
8676 return omit_one_operand_loc (loc, type, dest, src);
8678 /* If SRC and DEST are the same (and not volatile), return
8679 DEST{,+LEN,+LEN-1}. */
8680 if (operand_equal_p (src, dest, 0))
8681 expr = len;
8682 else
8684 tree srctype, desttype;
8685 unsigned int src_align, dest_align;
8686 tree off0;
8688 if (endp == 3)
8690 src_align = get_pointer_alignment (src);
8691 dest_align = get_pointer_alignment (dest);
8693 /* Both DEST and SRC must be pointer types.
8694 ??? This is what old code did. Is the testing for pointer types
8695 really mandatory?
8697 If either SRC is readonly or length is 1, we can use memcpy. */
8698 if (!dest_align || !src_align)
8699 return NULL_TREE;
8700 if (readonly_data_expr (src)
8701 || (tree_fits_uhwi_p (len)
8702 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8703 >= tree_to_uhwi (len))))
8705 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8706 if (!fn)
8707 return NULL_TREE;
8708 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8711 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8712 if (TREE_CODE (src) == ADDR_EXPR
8713 && TREE_CODE (dest) == ADDR_EXPR)
8715 tree src_base, dest_base, fn;
8716 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8717 HOST_WIDE_INT size = -1;
8718 HOST_WIDE_INT maxsize = -1;
8720 srcvar = TREE_OPERAND (src, 0);
8721 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8722 &size, &maxsize);
8723 destvar = TREE_OPERAND (dest, 0);
8724 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8725 &size, &maxsize);
8726 if (tree_fits_uhwi_p (len))
8727 maxsize = tree_to_uhwi (len);
8728 else
8729 maxsize = -1;
8730 src_offset /= BITS_PER_UNIT;
8731 dest_offset /= BITS_PER_UNIT;
8732 if (SSA_VAR_P (src_base)
8733 && SSA_VAR_P (dest_base))
8735 if (operand_equal_p (src_base, dest_base, 0)
8736 && ranges_overlap_p (src_offset, maxsize,
8737 dest_offset, maxsize))
8738 return NULL_TREE;
8740 else if (TREE_CODE (src_base) == MEM_REF
8741 && TREE_CODE (dest_base) == MEM_REF)
8743 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8744 TREE_OPERAND (dest_base, 0), 0))
8745 return NULL_TREE;
8746 offset_int off = mem_ref_offset (src_base) + src_offset;
8747 if (!wi::fits_shwi_p (off))
8748 return NULL_TREE;
8749 src_offset = off.to_shwi ();
8751 off = mem_ref_offset (dest_base) + dest_offset;
8752 if (!wi::fits_shwi_p (off))
8753 return NULL_TREE;
8754 dest_offset = off.to_shwi ();
8755 if (ranges_overlap_p (src_offset, maxsize,
8756 dest_offset, maxsize))
8757 return NULL_TREE;
8759 else
8760 return NULL_TREE;
8762 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8763 if (!fn)
8764 return NULL_TREE;
8765 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8768 /* If the destination and source do not alias optimize into
8769 memcpy as well. */
8770 if ((is_gimple_min_invariant (dest)
8771 || TREE_CODE (dest) == SSA_NAME)
8772 && (is_gimple_min_invariant (src)
8773 || TREE_CODE (src) == SSA_NAME))
8775 ao_ref destr, srcr;
8776 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8777 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8778 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8780 tree fn;
8781 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8782 if (!fn)
8783 return NULL_TREE;
8784 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8788 return NULL_TREE;
8791 if (!tree_fits_shwi_p (len))
8792 return NULL_TREE;
8793 /* FIXME:
8794 This logic lose for arguments like (type *)malloc (sizeof (type)),
8795 since we strip the casts of up to VOID return value from malloc.
8796 Perhaps we ought to inherit type from non-VOID argument here? */
8797 STRIP_NOPS (src);
8798 STRIP_NOPS (dest);
8799 if (!POINTER_TYPE_P (TREE_TYPE (src))
8800 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8801 return NULL_TREE;
8802 /* In the following try to find a type that is most natural to be
8803 used for the memcpy source and destination and that allows
8804 the most optimization when memcpy is turned into a plain assignment
8805 using that type. In theory we could always use a char[len] type
8806 but that only gains us that the destination and source possibly
8807 no longer will have their address taken. */
8808 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8809 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8811 tree tem = TREE_OPERAND (src, 0);
8812 STRIP_NOPS (tem);
8813 if (tem != TREE_OPERAND (src, 0))
8814 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8816 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8818 tree tem = TREE_OPERAND (dest, 0);
8819 STRIP_NOPS (tem);
8820 if (tem != TREE_OPERAND (dest, 0))
8821 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8823 srctype = TREE_TYPE (TREE_TYPE (src));
8824 if (TREE_CODE (srctype) == ARRAY_TYPE
8825 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8827 srctype = TREE_TYPE (srctype);
8828 STRIP_NOPS (src);
8829 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8831 desttype = TREE_TYPE (TREE_TYPE (dest));
8832 if (TREE_CODE (desttype) == ARRAY_TYPE
8833 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8835 desttype = TREE_TYPE (desttype);
8836 STRIP_NOPS (dest);
8837 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8839 if (TREE_ADDRESSABLE (srctype)
8840 || TREE_ADDRESSABLE (desttype))
8841 return NULL_TREE;
8843 /* Make sure we are not copying using a floating-point mode or
8844 a type whose size possibly does not match its precision. */
8845 if (FLOAT_MODE_P (TYPE_MODE (desttype))
8846 || TREE_CODE (desttype) == BOOLEAN_TYPE
8847 || TREE_CODE (desttype) == ENUMERAL_TYPE)
8848 desttype = bitwise_type_for_mode (TYPE_MODE (desttype));
8849 if (FLOAT_MODE_P (TYPE_MODE (srctype))
8850 || TREE_CODE (srctype) == BOOLEAN_TYPE
8851 || TREE_CODE (srctype) == ENUMERAL_TYPE)
8852 srctype = bitwise_type_for_mode (TYPE_MODE (srctype));
8853 if (!srctype)
8854 srctype = desttype;
8855 if (!desttype)
8856 desttype = srctype;
8857 if (!srctype)
8858 return NULL_TREE;
8860 src_align = get_pointer_alignment (src);
8861 dest_align = get_pointer_alignment (dest);
8862 if (dest_align < TYPE_ALIGN (desttype)
8863 || src_align < TYPE_ALIGN (srctype))
8864 return NULL_TREE;
8866 if (!ignore)
8867 dest = builtin_save_expr (dest);
8869 /* Build accesses at offset zero with a ref-all character type. */
8870 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8871 ptr_mode, true), 0);
8873 destvar = dest;
8874 STRIP_NOPS (destvar);
8875 if (TREE_CODE (destvar) == ADDR_EXPR
8876 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8877 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8878 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8879 else
8880 destvar = NULL_TREE;
8882 srcvar = src;
8883 STRIP_NOPS (srcvar);
8884 if (TREE_CODE (srcvar) == ADDR_EXPR
8885 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8886 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8888 if (!destvar
8889 || src_align >= TYPE_ALIGN (desttype))
8890 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8891 srcvar, off0);
8892 else if (!STRICT_ALIGNMENT)
8894 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8895 src_align);
8896 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8898 else
8899 srcvar = NULL_TREE;
8901 else
8902 srcvar = NULL_TREE;
8904 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8905 return NULL_TREE;
8907 if (srcvar == NULL_TREE)
8909 STRIP_NOPS (src);
8910 if (src_align >= TYPE_ALIGN (desttype))
8911 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8912 else
8914 if (STRICT_ALIGNMENT)
8915 return NULL_TREE;
8916 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8917 src_align);
8918 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8921 else if (destvar == NULL_TREE)
8923 STRIP_NOPS (dest);
8924 if (dest_align >= TYPE_ALIGN (srctype))
8925 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8926 else
8928 if (STRICT_ALIGNMENT)
8929 return NULL_TREE;
8930 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8931 dest_align);
8932 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8936 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8939 if (ignore)
8940 return expr;
8942 if (endp == 0 || endp == 3)
8943 return omit_one_operand_loc (loc, type, dest, expr);
8945 if (expr == len)
8946 expr = NULL_TREE;
8948 if (endp == 2)
8949 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8950 ssize_int (1));
8952 dest = fold_build_pointer_plus_loc (loc, dest, len);
8953 dest = fold_convert_loc (loc, type, dest);
8954 if (expr)
8955 dest = omit_one_operand_loc (loc, type, dest, expr);
8956 return dest;
8959 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8960 If LEN is not NULL, it represents the length of the string to be
8961 copied. Return NULL_TREE if no simplification can be made. */
8963 tree
8964 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8966 tree fn;
8968 if (!validate_arg (dest, POINTER_TYPE)
8969 || !validate_arg (src, POINTER_TYPE))
8970 return NULL_TREE;
8972 /* If SRC and DEST are the same (and not volatile), return DEST. */
8973 if (operand_equal_p (src, dest, 0))
8974 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8976 if (optimize_function_for_size_p (cfun))
8977 return NULL_TREE;
8979 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8980 if (!fn)
8981 return NULL_TREE;
8983 if (!len)
8985 len = c_strlen (src, 1);
8986 if (! len || TREE_SIDE_EFFECTS (len))
8987 return NULL_TREE;
8990 len = fold_convert_loc (loc, size_type_node, len);
8991 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
8992 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8993 build_call_expr_loc (loc, fn, 3, dest, src, len));
8996 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8997 Return NULL_TREE if no simplification can be made. */
8999 static tree
9000 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9002 tree fn, len, lenp1, call, type;
9004 if (!validate_arg (dest, POINTER_TYPE)
9005 || !validate_arg (src, POINTER_TYPE))
9006 return NULL_TREE;
9008 len = c_strlen (src, 1);
9009 if (!len
9010 || TREE_CODE (len) != INTEGER_CST)
9011 return NULL_TREE;
9013 if (optimize_function_for_size_p (cfun)
9014 /* If length is zero it's small enough. */
9015 && !integer_zerop (len))
9016 return NULL_TREE;
9018 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9019 if (!fn)
9020 return NULL_TREE;
9022 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9023 fold_convert_loc (loc, size_type_node, len),
9024 build_int_cst (size_type_node, 1));
9025 /* We use dest twice in building our expression. Save it from
9026 multiple expansions. */
9027 dest = builtin_save_expr (dest);
9028 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9030 type = TREE_TYPE (TREE_TYPE (fndecl));
9031 dest = fold_build_pointer_plus_loc (loc, dest, len);
9032 dest = fold_convert_loc (loc, type, dest);
9033 dest = omit_one_operand_loc (loc, type, dest, call);
9034 return dest;
9037 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9038 If SLEN is not NULL, it represents the length of the source string.
9039 Return NULL_TREE if no simplification can be made. */
9041 tree
9042 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9043 tree src, tree len, tree slen)
9045 tree fn;
9047 if (!validate_arg (dest, POINTER_TYPE)
9048 || !validate_arg (src, POINTER_TYPE)
9049 || !validate_arg (len, INTEGER_TYPE))
9050 return NULL_TREE;
9052 /* If the LEN parameter is zero, return DEST. */
9053 if (integer_zerop (len))
9054 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9056 /* We can't compare slen with len as constants below if len is not a
9057 constant. */
9058 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9059 return NULL_TREE;
9061 if (!slen)
9062 slen = c_strlen (src, 1);
9064 /* Now, we must be passed a constant src ptr parameter. */
9065 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9066 return NULL_TREE;
9068 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9070 /* We do not support simplification of this case, though we do
9071 support it when expanding trees into RTL. */
9072 /* FIXME: generate a call to __builtin_memset. */
9073 if (tree_int_cst_lt (slen, len))
9074 return NULL_TREE;
9076 /* OK transform into builtin memcpy. */
9077 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9078 if (!fn)
9079 return NULL_TREE;
9081 len = fold_convert_loc (loc, size_type_node, len);
9082 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9083 build_call_expr_loc (loc, fn, 3, dest, src, len));
9086 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9087 arguments to the call, and TYPE is its return type.
9088 Return NULL_TREE if no simplification can be made. */
9090 static tree
9091 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9093 if (!validate_arg (arg1, POINTER_TYPE)
9094 || !validate_arg (arg2, INTEGER_TYPE)
9095 || !validate_arg (len, INTEGER_TYPE))
9096 return NULL_TREE;
9097 else
9099 const char *p1;
9101 if (TREE_CODE (arg2) != INTEGER_CST
9102 || !tree_fits_uhwi_p (len))
9103 return NULL_TREE;
9105 p1 = c_getstr (arg1);
9106 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9108 char c;
9109 const char *r;
9110 tree tem;
9112 if (target_char_cast (arg2, &c))
9113 return NULL_TREE;
9115 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
9117 if (r == NULL)
9118 return build_int_cst (TREE_TYPE (arg1), 0);
9120 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9121 return fold_convert_loc (loc, type, tem);
9123 return NULL_TREE;
9127 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9128 Return NULL_TREE if no simplification can be made. */
9130 static tree
9131 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9133 const char *p1, *p2;
9135 if (!validate_arg (arg1, POINTER_TYPE)
9136 || !validate_arg (arg2, POINTER_TYPE)
9137 || !validate_arg (len, INTEGER_TYPE))
9138 return NULL_TREE;
9140 /* If the LEN parameter is zero, return zero. */
9141 if (integer_zerop (len))
9142 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9143 arg1, arg2);
9145 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9146 if (operand_equal_p (arg1, arg2, 0))
9147 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9149 p1 = c_getstr (arg1);
9150 p2 = c_getstr (arg2);
9152 /* If all arguments are constant, and the value of len is not greater
9153 than the lengths of arg1 and arg2, evaluate at compile-time. */
9154 if (tree_fits_uhwi_p (len) && p1 && p2
9155 && compare_tree_int (len, strlen (p1) + 1) <= 0
9156 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9158 const int r = memcmp (p1, p2, tree_to_uhwi (len));
9160 if (r > 0)
9161 return integer_one_node;
9162 else if (r < 0)
9163 return integer_minus_one_node;
9164 else
9165 return integer_zero_node;
9168 /* If len parameter is one, return an expression corresponding to
9169 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9170 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9172 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9173 tree cst_uchar_ptr_node
9174 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9176 tree ind1
9177 = fold_convert_loc (loc, integer_type_node,
9178 build1 (INDIRECT_REF, cst_uchar_node,
9179 fold_convert_loc (loc,
9180 cst_uchar_ptr_node,
9181 arg1)));
9182 tree ind2
9183 = 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 arg2)));
9188 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9191 return NULL_TREE;
9194 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9195 Return NULL_TREE if no simplification can be made. */
9197 static tree
9198 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9200 const char *p1, *p2;
9202 if (!validate_arg (arg1, POINTER_TYPE)
9203 || !validate_arg (arg2, POINTER_TYPE))
9204 return NULL_TREE;
9206 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9207 if (operand_equal_p (arg1, arg2, 0))
9208 return integer_zero_node;
9210 p1 = c_getstr (arg1);
9211 p2 = c_getstr (arg2);
9213 if (p1 && p2)
9215 const int i = strcmp (p1, p2);
9216 if (i < 0)
9217 return integer_minus_one_node;
9218 else if (i > 0)
9219 return integer_one_node;
9220 else
9221 return integer_zero_node;
9224 /* If the second arg is "", return *(const unsigned char*)arg1. */
9225 if (p2 && *p2 == '\0')
9227 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9228 tree cst_uchar_ptr_node
9229 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9231 return fold_convert_loc (loc, integer_type_node,
9232 build1 (INDIRECT_REF, cst_uchar_node,
9233 fold_convert_loc (loc,
9234 cst_uchar_ptr_node,
9235 arg1)));
9238 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9239 if (p1 && *p1 == '\0')
9241 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9242 tree cst_uchar_ptr_node
9243 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9245 tree temp
9246 = fold_convert_loc (loc, integer_type_node,
9247 build1 (INDIRECT_REF, cst_uchar_node,
9248 fold_convert_loc (loc,
9249 cst_uchar_ptr_node,
9250 arg2)));
9251 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9254 return NULL_TREE;
9257 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9258 Return NULL_TREE if no simplification can be made. */
9260 static tree
9261 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9263 const char *p1, *p2;
9265 if (!validate_arg (arg1, POINTER_TYPE)
9266 || !validate_arg (arg2, POINTER_TYPE)
9267 || !validate_arg (len, INTEGER_TYPE))
9268 return NULL_TREE;
9270 /* If the LEN parameter is zero, return zero. */
9271 if (integer_zerop (len))
9272 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9273 arg1, arg2);
9275 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9276 if (operand_equal_p (arg1, arg2, 0))
9277 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9279 p1 = c_getstr (arg1);
9280 p2 = c_getstr (arg2);
9282 if (tree_fits_uhwi_p (len) && p1 && p2)
9284 const int i = strncmp (p1, p2, tree_to_uhwi (len));
9285 if (i > 0)
9286 return integer_one_node;
9287 else if (i < 0)
9288 return integer_minus_one_node;
9289 else
9290 return integer_zero_node;
9293 /* If the second arg is "", and the length is greater than zero,
9294 return *(const unsigned char*)arg1. */
9295 if (p2 && *p2 == '\0'
9296 && TREE_CODE (len) == INTEGER_CST
9297 && tree_int_cst_sgn (len) == 1)
9299 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9300 tree cst_uchar_ptr_node
9301 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9303 return fold_convert_loc (loc, integer_type_node,
9304 build1 (INDIRECT_REF, cst_uchar_node,
9305 fold_convert_loc (loc,
9306 cst_uchar_ptr_node,
9307 arg1)));
9310 /* If the first arg is "", and the length is greater than zero,
9311 return -*(const unsigned char*)arg2. */
9312 if (p1 && *p1 == '\0'
9313 && TREE_CODE (len) == INTEGER_CST
9314 && tree_int_cst_sgn (len) == 1)
9316 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9317 tree cst_uchar_ptr_node
9318 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9320 tree temp = fold_convert_loc (loc, integer_type_node,
9321 build1 (INDIRECT_REF, cst_uchar_node,
9322 fold_convert_loc (loc,
9323 cst_uchar_ptr_node,
9324 arg2)));
9325 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9328 /* If len parameter is one, return an expression corresponding to
9329 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9330 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9332 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9333 tree cst_uchar_ptr_node
9334 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9336 tree ind1 = fold_convert_loc (loc, integer_type_node,
9337 build1 (INDIRECT_REF, cst_uchar_node,
9338 fold_convert_loc (loc,
9339 cst_uchar_ptr_node,
9340 arg1)));
9341 tree ind2 = fold_convert_loc (loc, integer_type_node,
9342 build1 (INDIRECT_REF, cst_uchar_node,
9343 fold_convert_loc (loc,
9344 cst_uchar_ptr_node,
9345 arg2)));
9346 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9349 return NULL_TREE;
9352 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9353 ARG. Return NULL_TREE if no simplification can be made. */
9355 static tree
9356 fold_builtin_signbit (location_t loc, tree arg, tree type)
9358 if (!validate_arg (arg, REAL_TYPE))
9359 return NULL_TREE;
9361 /* If ARG is a compile-time constant, determine the result. */
9362 if (TREE_CODE (arg) == REAL_CST
9363 && !TREE_OVERFLOW (arg))
9365 REAL_VALUE_TYPE c;
9367 c = TREE_REAL_CST (arg);
9368 return (REAL_VALUE_NEGATIVE (c)
9369 ? build_one_cst (type)
9370 : build_zero_cst (type));
9373 /* If ARG is non-negative, the result is always zero. */
9374 if (tree_expr_nonnegative_p (arg))
9375 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9377 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9378 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9379 return fold_convert (type,
9380 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9381 build_real (TREE_TYPE (arg), dconst0)));
9383 return NULL_TREE;
9386 /* Fold function call to builtin copysign, copysignf or copysignl with
9387 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9388 be made. */
9390 static tree
9391 fold_builtin_copysign (location_t loc, tree fndecl,
9392 tree arg1, tree arg2, tree type)
9394 tree tem;
9396 if (!validate_arg (arg1, REAL_TYPE)
9397 || !validate_arg (arg2, REAL_TYPE))
9398 return NULL_TREE;
9400 /* copysign(X,X) is X. */
9401 if (operand_equal_p (arg1, arg2, 0))
9402 return fold_convert_loc (loc, type, arg1);
9404 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9405 if (TREE_CODE (arg1) == REAL_CST
9406 && TREE_CODE (arg2) == REAL_CST
9407 && !TREE_OVERFLOW (arg1)
9408 && !TREE_OVERFLOW (arg2))
9410 REAL_VALUE_TYPE c1, c2;
9412 c1 = TREE_REAL_CST (arg1);
9413 c2 = TREE_REAL_CST (arg2);
9414 /* c1.sign := c2.sign. */
9415 real_copysign (&c1, &c2);
9416 return build_real (type, c1);
9419 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9420 Remember to evaluate Y for side-effects. */
9421 if (tree_expr_nonnegative_p (arg2))
9422 return omit_one_operand_loc (loc, type,
9423 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9424 arg2);
9426 /* Strip sign changing operations for the first argument. */
9427 tem = fold_strip_sign_ops (arg1);
9428 if (tem)
9429 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9431 return NULL_TREE;
9434 /* Fold a call to builtin isascii with argument ARG. */
9436 static tree
9437 fold_builtin_isascii (location_t loc, tree arg)
9439 if (!validate_arg (arg, INTEGER_TYPE))
9440 return NULL_TREE;
9441 else
9443 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9444 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9445 build_int_cst (integer_type_node,
9446 ~ (unsigned HOST_WIDE_INT) 0x7f));
9447 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9448 arg, integer_zero_node);
9452 /* Fold a call to builtin toascii with argument ARG. */
9454 static tree
9455 fold_builtin_toascii (location_t loc, tree arg)
9457 if (!validate_arg (arg, INTEGER_TYPE))
9458 return NULL_TREE;
9460 /* Transform toascii(c) -> (c & 0x7f). */
9461 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9462 build_int_cst (integer_type_node, 0x7f));
9465 /* Fold a call to builtin isdigit with argument ARG. */
9467 static tree
9468 fold_builtin_isdigit (location_t loc, tree arg)
9470 if (!validate_arg (arg, INTEGER_TYPE))
9471 return NULL_TREE;
9472 else
9474 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9475 /* According to the C standard, isdigit is unaffected by locale.
9476 However, it definitely is affected by the target character set. */
9477 unsigned HOST_WIDE_INT target_digit0
9478 = lang_hooks.to_target_charset ('0');
9480 if (target_digit0 == 0)
9481 return NULL_TREE;
9483 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9484 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9485 build_int_cst (unsigned_type_node, target_digit0));
9486 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9487 build_int_cst (unsigned_type_node, 9));
9491 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9493 static tree
9494 fold_builtin_fabs (location_t loc, tree arg, tree type)
9496 if (!validate_arg (arg, REAL_TYPE))
9497 return NULL_TREE;
9499 arg = fold_convert_loc (loc, type, arg);
9500 if (TREE_CODE (arg) == REAL_CST)
9501 return fold_abs_const (arg, type);
9502 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9505 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9507 static tree
9508 fold_builtin_abs (location_t loc, tree arg, tree type)
9510 if (!validate_arg (arg, INTEGER_TYPE))
9511 return NULL_TREE;
9513 arg = fold_convert_loc (loc, type, arg);
9514 if (TREE_CODE (arg) == INTEGER_CST)
9515 return fold_abs_const (arg, type);
9516 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9519 /* Fold a fma operation with arguments ARG[012]. */
9521 tree
9522 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9523 tree type, tree arg0, tree arg1, tree arg2)
9525 if (TREE_CODE (arg0) == REAL_CST
9526 && TREE_CODE (arg1) == REAL_CST
9527 && TREE_CODE (arg2) == REAL_CST)
9528 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9530 return NULL_TREE;
9533 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9535 static tree
9536 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9538 if (validate_arg (arg0, REAL_TYPE)
9539 && validate_arg (arg1, REAL_TYPE)
9540 && validate_arg (arg2, REAL_TYPE))
9542 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9543 if (tem)
9544 return tem;
9546 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9547 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9548 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9550 return NULL_TREE;
9553 /* Fold a call to builtin fmin or fmax. */
9555 static tree
9556 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9557 tree type, bool max)
9559 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9561 /* Calculate the result when the argument is a constant. */
9562 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9564 if (res)
9565 return res;
9567 /* If either argument is NaN, return the other one. Avoid the
9568 transformation if we get (and honor) a signalling NaN. Using
9569 omit_one_operand() ensures we create a non-lvalue. */
9570 if (TREE_CODE (arg0) == REAL_CST
9571 && real_isnan (&TREE_REAL_CST (arg0))
9572 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9573 || ! TREE_REAL_CST (arg0).signalling))
9574 return omit_one_operand_loc (loc, type, arg1, arg0);
9575 if (TREE_CODE (arg1) == REAL_CST
9576 && real_isnan (&TREE_REAL_CST (arg1))
9577 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9578 || ! TREE_REAL_CST (arg1).signalling))
9579 return omit_one_operand_loc (loc, type, arg0, arg1);
9581 /* Transform fmin/fmax(x,x) -> x. */
9582 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9583 return omit_one_operand_loc (loc, type, arg0, arg1);
9585 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9586 functions to return the numeric arg if the other one is NaN.
9587 These tree codes don't honor that, so only transform if
9588 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9589 handled, so we don't have to worry about it either. */
9590 if (flag_finite_math_only)
9591 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9592 fold_convert_loc (loc, type, arg0),
9593 fold_convert_loc (loc, type, arg1));
9595 return NULL_TREE;
9598 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9600 static tree
9601 fold_builtin_carg (location_t loc, tree arg, tree type)
9603 if (validate_arg (arg, COMPLEX_TYPE)
9604 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9606 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9608 if (atan2_fn)
9610 tree new_arg = builtin_save_expr (arg);
9611 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9612 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9613 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9617 return NULL_TREE;
9620 /* Fold a call to builtin logb/ilogb. */
9622 static tree
9623 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9625 if (! validate_arg (arg, REAL_TYPE))
9626 return NULL_TREE;
9628 STRIP_NOPS (arg);
9630 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9632 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9634 switch (value->cl)
9636 case rvc_nan:
9637 case rvc_inf:
9638 /* If arg is Inf or NaN and we're logb, return it. */
9639 if (TREE_CODE (rettype) == REAL_TYPE)
9641 /* For logb(-Inf) we have to return +Inf. */
9642 if (real_isinf (value) && real_isneg (value))
9644 REAL_VALUE_TYPE tem;
9645 real_inf (&tem);
9646 return build_real (rettype, tem);
9648 return fold_convert_loc (loc, rettype, arg);
9650 /* Fall through... */
9651 case rvc_zero:
9652 /* Zero may set errno and/or raise an exception for logb, also
9653 for ilogb we don't know FP_ILOGB0. */
9654 return NULL_TREE;
9655 case rvc_normal:
9656 /* For normal numbers, proceed iff radix == 2. In GCC,
9657 normalized significands are in the range [0.5, 1.0). We
9658 want the exponent as if they were [1.0, 2.0) so get the
9659 exponent and subtract 1. */
9660 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9661 return fold_convert_loc (loc, rettype,
9662 build_int_cst (integer_type_node,
9663 REAL_EXP (value)-1));
9664 break;
9668 return NULL_TREE;
9671 /* Fold a call to builtin significand, if radix == 2. */
9673 static tree
9674 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9676 if (! validate_arg (arg, REAL_TYPE))
9677 return NULL_TREE;
9679 STRIP_NOPS (arg);
9681 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9683 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9685 switch (value->cl)
9687 case rvc_zero:
9688 case rvc_nan:
9689 case rvc_inf:
9690 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9691 return fold_convert_loc (loc, rettype, arg);
9692 case rvc_normal:
9693 /* For normal numbers, proceed iff radix == 2. */
9694 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9696 REAL_VALUE_TYPE result = *value;
9697 /* In GCC, normalized significands are in the range [0.5,
9698 1.0). We want them to be [1.0, 2.0) so set the
9699 exponent to 1. */
9700 SET_REAL_EXP (&result, 1);
9701 return build_real (rettype, result);
9703 break;
9707 return NULL_TREE;
9710 /* Fold a call to builtin frexp, we can assume the base is 2. */
9712 static tree
9713 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9715 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9716 return NULL_TREE;
9718 STRIP_NOPS (arg0);
9720 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9721 return NULL_TREE;
9723 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9725 /* Proceed if a valid pointer type was passed in. */
9726 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9728 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9729 tree frac, exp;
9731 switch (value->cl)
9733 case rvc_zero:
9734 /* For +-0, return (*exp = 0, +-0). */
9735 exp = integer_zero_node;
9736 frac = arg0;
9737 break;
9738 case rvc_nan:
9739 case rvc_inf:
9740 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9741 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9742 case rvc_normal:
9744 /* Since the frexp function always expects base 2, and in
9745 GCC normalized significands are already in the range
9746 [0.5, 1.0), we have exactly what frexp wants. */
9747 REAL_VALUE_TYPE frac_rvt = *value;
9748 SET_REAL_EXP (&frac_rvt, 0);
9749 frac = build_real (rettype, frac_rvt);
9750 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9752 break;
9753 default:
9754 gcc_unreachable ();
9757 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9758 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9759 TREE_SIDE_EFFECTS (arg1) = 1;
9760 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9763 return NULL_TREE;
9766 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9767 then we can assume the base is two. If it's false, then we have to
9768 check the mode of the TYPE parameter in certain cases. */
9770 static tree
9771 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9772 tree type, bool ldexp)
9774 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9776 STRIP_NOPS (arg0);
9777 STRIP_NOPS (arg1);
9779 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9780 if (real_zerop (arg0) || integer_zerop (arg1)
9781 || (TREE_CODE (arg0) == REAL_CST
9782 && !real_isfinite (&TREE_REAL_CST (arg0))))
9783 return omit_one_operand_loc (loc, type, arg0, arg1);
9785 /* If both arguments are constant, then try to evaluate it. */
9786 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9787 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9788 && tree_fits_shwi_p (arg1))
9790 /* Bound the maximum adjustment to twice the range of the
9791 mode's valid exponents. Use abs to ensure the range is
9792 positive as a sanity check. */
9793 const long max_exp_adj = 2 *
9794 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9795 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9797 /* Get the user-requested adjustment. */
9798 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9800 /* The requested adjustment must be inside this range. This
9801 is a preliminary cap to avoid things like overflow, we
9802 may still fail to compute the result for other reasons. */
9803 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9805 REAL_VALUE_TYPE initial_result;
9807 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9809 /* Ensure we didn't overflow. */
9810 if (! real_isinf (&initial_result))
9812 const REAL_VALUE_TYPE trunc_result
9813 = real_value_truncate (TYPE_MODE (type), initial_result);
9815 /* Only proceed if the target mode can hold the
9816 resulting value. */
9817 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9818 return build_real (type, trunc_result);
9824 return NULL_TREE;
9827 /* Fold a call to builtin modf. */
9829 static tree
9830 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9832 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9833 return NULL_TREE;
9835 STRIP_NOPS (arg0);
9837 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9838 return NULL_TREE;
9840 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9842 /* Proceed if a valid pointer type was passed in. */
9843 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9845 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9846 REAL_VALUE_TYPE trunc, frac;
9848 switch (value->cl)
9850 case rvc_nan:
9851 case rvc_zero:
9852 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9853 trunc = frac = *value;
9854 break;
9855 case rvc_inf:
9856 /* For +-Inf, return (*arg1 = arg0, +-0). */
9857 frac = dconst0;
9858 frac.sign = value->sign;
9859 trunc = *value;
9860 break;
9861 case rvc_normal:
9862 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9863 real_trunc (&trunc, VOIDmode, value);
9864 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9865 /* If the original number was negative and already
9866 integral, then the fractional part is -0.0. */
9867 if (value->sign && frac.cl == rvc_zero)
9868 frac.sign = value->sign;
9869 break;
9872 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9873 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9874 build_real (rettype, trunc));
9875 TREE_SIDE_EFFECTS (arg1) = 1;
9876 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9877 build_real (rettype, frac));
9880 return NULL_TREE;
9883 /* Given a location LOC, an interclass builtin function decl FNDECL
9884 and its single argument ARG, return an folded expression computing
9885 the same, or NULL_TREE if we either couldn't or didn't want to fold
9886 (the latter happen if there's an RTL instruction available). */
9888 static tree
9889 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9891 enum machine_mode mode;
9893 if (!validate_arg (arg, REAL_TYPE))
9894 return NULL_TREE;
9896 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9897 return NULL_TREE;
9899 mode = TYPE_MODE (TREE_TYPE (arg));
9901 /* If there is no optab, try generic code. */
9902 switch (DECL_FUNCTION_CODE (fndecl))
9904 tree result;
9906 CASE_FLT_FN (BUILT_IN_ISINF):
9908 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9909 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9910 tree const type = TREE_TYPE (arg);
9911 REAL_VALUE_TYPE r;
9912 char buf[128];
9914 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9915 real_from_string (&r, buf);
9916 result = build_call_expr (isgr_fn, 2,
9917 fold_build1_loc (loc, ABS_EXPR, type, arg),
9918 build_real (type, r));
9919 return result;
9921 CASE_FLT_FN (BUILT_IN_FINITE):
9922 case BUILT_IN_ISFINITE:
9924 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9925 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9926 tree const type = TREE_TYPE (arg);
9927 REAL_VALUE_TYPE r;
9928 char buf[128];
9930 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9931 real_from_string (&r, buf);
9932 result = build_call_expr (isle_fn, 2,
9933 fold_build1_loc (loc, ABS_EXPR, type, arg),
9934 build_real (type, r));
9935 /*result = fold_build2_loc (loc, UNGT_EXPR,
9936 TREE_TYPE (TREE_TYPE (fndecl)),
9937 fold_build1_loc (loc, ABS_EXPR, type, arg),
9938 build_real (type, r));
9939 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9940 TREE_TYPE (TREE_TYPE (fndecl)),
9941 result);*/
9942 return result;
9944 case BUILT_IN_ISNORMAL:
9946 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9947 islessequal(fabs(x),DBL_MAX). */
9948 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9949 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9950 tree const type = TREE_TYPE (arg);
9951 REAL_VALUE_TYPE rmax, rmin;
9952 char buf[128];
9954 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9955 real_from_string (&rmax, buf);
9956 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9957 real_from_string (&rmin, buf);
9958 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9959 result = build_call_expr (isle_fn, 2, arg,
9960 build_real (type, rmax));
9961 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9962 build_call_expr (isge_fn, 2, arg,
9963 build_real (type, rmin)));
9964 return result;
9966 default:
9967 break;
9970 return NULL_TREE;
9973 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9974 ARG is the argument for the call. */
9976 static tree
9977 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9979 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9980 REAL_VALUE_TYPE r;
9982 if (!validate_arg (arg, REAL_TYPE))
9983 return NULL_TREE;
9985 switch (builtin_index)
9987 case BUILT_IN_ISINF:
9988 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9989 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9991 if (TREE_CODE (arg) == REAL_CST)
9993 r = TREE_REAL_CST (arg);
9994 if (real_isinf (&r))
9995 return real_compare (GT_EXPR, &r, &dconst0)
9996 ? integer_one_node : integer_minus_one_node;
9997 else
9998 return integer_zero_node;
10001 return NULL_TREE;
10003 case BUILT_IN_ISINF_SIGN:
10005 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10006 /* In a boolean context, GCC will fold the inner COND_EXPR to
10007 1. So e.g. "if (isinf_sign(x))" would be folded to just
10008 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10009 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10010 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10011 tree tmp = NULL_TREE;
10013 arg = builtin_save_expr (arg);
10015 if (signbit_fn && isinf_fn)
10017 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10018 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10020 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10021 signbit_call, integer_zero_node);
10022 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10023 isinf_call, integer_zero_node);
10025 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10026 integer_minus_one_node, integer_one_node);
10027 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10028 isinf_call, tmp,
10029 integer_zero_node);
10032 return tmp;
10035 case BUILT_IN_ISFINITE:
10036 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10037 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10038 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10040 if (TREE_CODE (arg) == REAL_CST)
10042 r = TREE_REAL_CST (arg);
10043 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10046 return NULL_TREE;
10048 case BUILT_IN_ISNAN:
10049 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10050 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10052 if (TREE_CODE (arg) == REAL_CST)
10054 r = TREE_REAL_CST (arg);
10055 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10058 arg = builtin_save_expr (arg);
10059 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10061 default:
10062 gcc_unreachable ();
10066 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10067 This builtin will generate code to return the appropriate floating
10068 point classification depending on the value of the floating point
10069 number passed in. The possible return values must be supplied as
10070 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10071 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10072 one floating point argument which is "type generic". */
10074 static tree
10075 fold_builtin_fpclassify (location_t loc, tree exp)
10077 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10078 arg, type, res, tmp;
10079 enum machine_mode mode;
10080 REAL_VALUE_TYPE r;
10081 char buf[128];
10083 /* Verify the required arguments in the original call. */
10084 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10085 INTEGER_TYPE, INTEGER_TYPE,
10086 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10087 return NULL_TREE;
10089 fp_nan = CALL_EXPR_ARG (exp, 0);
10090 fp_infinite = CALL_EXPR_ARG (exp, 1);
10091 fp_normal = CALL_EXPR_ARG (exp, 2);
10092 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10093 fp_zero = CALL_EXPR_ARG (exp, 4);
10094 arg = CALL_EXPR_ARG (exp, 5);
10095 type = TREE_TYPE (arg);
10096 mode = TYPE_MODE (type);
10097 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10099 /* fpclassify(x) ->
10100 isnan(x) ? FP_NAN :
10101 (fabs(x) == Inf ? FP_INFINITE :
10102 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10103 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10105 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10106 build_real (type, dconst0));
10107 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10108 tmp, fp_zero, fp_subnormal);
10110 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10111 real_from_string (&r, buf);
10112 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10113 arg, build_real (type, r));
10114 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10116 if (HONOR_INFINITIES (mode))
10118 real_inf (&r);
10119 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10120 build_real (type, r));
10121 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10122 fp_infinite, res);
10125 if (HONOR_NANS (mode))
10127 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10128 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10131 return res;
10134 /* Fold a call to an unordered comparison function such as
10135 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10136 being called and ARG0 and ARG1 are the arguments for the call.
10137 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10138 the opposite of the desired result. UNORDERED_CODE is used
10139 for modes that can hold NaNs and ORDERED_CODE is used for
10140 the rest. */
10142 static tree
10143 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10144 enum tree_code unordered_code,
10145 enum tree_code ordered_code)
10147 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10148 enum tree_code code;
10149 tree type0, type1;
10150 enum tree_code code0, code1;
10151 tree cmp_type = NULL_TREE;
10153 type0 = TREE_TYPE (arg0);
10154 type1 = TREE_TYPE (arg1);
10156 code0 = TREE_CODE (type0);
10157 code1 = TREE_CODE (type1);
10159 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10160 /* Choose the wider of two real types. */
10161 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10162 ? type0 : type1;
10163 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10164 cmp_type = type0;
10165 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10166 cmp_type = type1;
10168 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10169 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10171 if (unordered_code == UNORDERED_EXPR)
10173 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10174 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10175 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10178 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10179 : ordered_code;
10180 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10181 fold_build2_loc (loc, code, type, arg0, arg1));
10184 /* Fold a call to built-in function FNDECL with 0 arguments.
10185 IGNORE is true if the result of the function call is ignored. This
10186 function returns NULL_TREE if no simplification was possible. */
10188 static tree
10189 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10191 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10192 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10193 switch (fcode)
10195 CASE_FLT_FN (BUILT_IN_INF):
10196 case BUILT_IN_INFD32:
10197 case BUILT_IN_INFD64:
10198 case BUILT_IN_INFD128:
10199 return fold_builtin_inf (loc, type, true);
10201 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10202 return fold_builtin_inf (loc, type, false);
10204 case BUILT_IN_CLASSIFY_TYPE:
10205 return fold_builtin_classify_type (NULL_TREE);
10207 case BUILT_IN_UNREACHABLE:
10208 if (flag_sanitize & SANITIZE_UNREACHABLE
10209 && (current_function_decl == NULL
10210 || !lookup_attribute ("no_sanitize_undefined",
10211 DECL_ATTRIBUTES (current_function_decl))))
10212 return ubsan_instrument_unreachable (loc);
10213 break;
10215 default:
10216 break;
10218 return NULL_TREE;
10221 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10222 IGNORE is true if the result of the function call is ignored. This
10223 function returns NULL_TREE if no simplification was possible. */
10225 static tree
10226 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10228 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10229 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10230 switch (fcode)
10232 case BUILT_IN_CONSTANT_P:
10234 tree val = fold_builtin_constant_p (arg0);
10236 /* Gimplification will pull the CALL_EXPR for the builtin out of
10237 an if condition. When not optimizing, we'll not CSE it back.
10238 To avoid link error types of regressions, return false now. */
10239 if (!val && !optimize)
10240 val = integer_zero_node;
10242 return val;
10245 case BUILT_IN_CLASSIFY_TYPE:
10246 return fold_builtin_classify_type (arg0);
10248 case BUILT_IN_STRLEN:
10249 return fold_builtin_strlen (loc, type, arg0);
10251 CASE_FLT_FN (BUILT_IN_FABS):
10252 case BUILT_IN_FABSD32:
10253 case BUILT_IN_FABSD64:
10254 case BUILT_IN_FABSD128:
10255 return fold_builtin_fabs (loc, arg0, type);
10257 case BUILT_IN_ABS:
10258 case BUILT_IN_LABS:
10259 case BUILT_IN_LLABS:
10260 case BUILT_IN_IMAXABS:
10261 return fold_builtin_abs (loc, arg0, type);
10263 CASE_FLT_FN (BUILT_IN_CONJ):
10264 if (validate_arg (arg0, COMPLEX_TYPE)
10265 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10266 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10267 break;
10269 CASE_FLT_FN (BUILT_IN_CREAL):
10270 if (validate_arg (arg0, COMPLEX_TYPE)
10271 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10272 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10273 break;
10275 CASE_FLT_FN (BUILT_IN_CIMAG):
10276 if (validate_arg (arg0, COMPLEX_TYPE)
10277 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10278 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10279 break;
10281 CASE_FLT_FN (BUILT_IN_CCOS):
10282 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
10284 CASE_FLT_FN (BUILT_IN_CCOSH):
10285 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
10287 CASE_FLT_FN (BUILT_IN_CPROJ):
10288 return fold_builtin_cproj (loc, arg0, type);
10290 CASE_FLT_FN (BUILT_IN_CSIN):
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_sin);
10294 break;
10296 CASE_FLT_FN (BUILT_IN_CSINH):
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_sinh);
10300 break;
10302 CASE_FLT_FN (BUILT_IN_CTAN):
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_tan);
10306 break;
10308 CASE_FLT_FN (BUILT_IN_CTANH):
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_tanh);
10312 break;
10314 CASE_FLT_FN (BUILT_IN_CLOG):
10315 if (validate_arg (arg0, COMPLEX_TYPE)
10316 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10317 return do_mpc_arg1 (arg0, type, mpc_log);
10318 break;
10320 CASE_FLT_FN (BUILT_IN_CSQRT):
10321 if (validate_arg (arg0, COMPLEX_TYPE)
10322 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10323 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10324 break;
10326 CASE_FLT_FN (BUILT_IN_CASIN):
10327 if (validate_arg (arg0, COMPLEX_TYPE)
10328 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10329 return do_mpc_arg1 (arg0, type, mpc_asin);
10330 break;
10332 CASE_FLT_FN (BUILT_IN_CACOS):
10333 if (validate_arg (arg0, COMPLEX_TYPE)
10334 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10335 return do_mpc_arg1 (arg0, type, mpc_acos);
10336 break;
10338 CASE_FLT_FN (BUILT_IN_CATAN):
10339 if (validate_arg (arg0, COMPLEX_TYPE)
10340 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10341 return do_mpc_arg1 (arg0, type, mpc_atan);
10342 break;
10344 CASE_FLT_FN (BUILT_IN_CASINH):
10345 if (validate_arg (arg0, COMPLEX_TYPE)
10346 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10347 return do_mpc_arg1 (arg0, type, mpc_asinh);
10348 break;
10350 CASE_FLT_FN (BUILT_IN_CACOSH):
10351 if (validate_arg (arg0, COMPLEX_TYPE)
10352 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10353 return do_mpc_arg1 (arg0, type, mpc_acosh);
10354 break;
10356 CASE_FLT_FN (BUILT_IN_CATANH):
10357 if (validate_arg (arg0, COMPLEX_TYPE)
10358 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10359 return do_mpc_arg1 (arg0, type, mpc_atanh);
10360 break;
10362 CASE_FLT_FN (BUILT_IN_CABS):
10363 return fold_builtin_cabs (loc, arg0, type, fndecl);
10365 CASE_FLT_FN (BUILT_IN_CARG):
10366 return fold_builtin_carg (loc, arg0, type);
10368 CASE_FLT_FN (BUILT_IN_SQRT):
10369 return fold_builtin_sqrt (loc, arg0, type);
10371 CASE_FLT_FN (BUILT_IN_CBRT):
10372 return fold_builtin_cbrt (loc, arg0, type);
10374 CASE_FLT_FN (BUILT_IN_ASIN):
10375 if (validate_arg (arg0, REAL_TYPE))
10376 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10377 &dconstm1, &dconst1, true);
10378 break;
10380 CASE_FLT_FN (BUILT_IN_ACOS):
10381 if (validate_arg (arg0, REAL_TYPE))
10382 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10383 &dconstm1, &dconst1, true);
10384 break;
10386 CASE_FLT_FN (BUILT_IN_ATAN):
10387 if (validate_arg (arg0, REAL_TYPE))
10388 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10389 break;
10391 CASE_FLT_FN (BUILT_IN_ASINH):
10392 if (validate_arg (arg0, REAL_TYPE))
10393 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10394 break;
10396 CASE_FLT_FN (BUILT_IN_ACOSH):
10397 if (validate_arg (arg0, REAL_TYPE))
10398 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10399 &dconst1, NULL, true);
10400 break;
10402 CASE_FLT_FN (BUILT_IN_ATANH):
10403 if (validate_arg (arg0, REAL_TYPE))
10404 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10405 &dconstm1, &dconst1, false);
10406 break;
10408 CASE_FLT_FN (BUILT_IN_SIN):
10409 if (validate_arg (arg0, REAL_TYPE))
10410 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10411 break;
10413 CASE_FLT_FN (BUILT_IN_COS):
10414 return fold_builtin_cos (loc, arg0, type, fndecl);
10416 CASE_FLT_FN (BUILT_IN_TAN):
10417 return fold_builtin_tan (arg0, type);
10419 CASE_FLT_FN (BUILT_IN_CEXP):
10420 return fold_builtin_cexp (loc, arg0, type);
10422 CASE_FLT_FN (BUILT_IN_CEXPI):
10423 if (validate_arg (arg0, REAL_TYPE))
10424 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10425 break;
10427 CASE_FLT_FN (BUILT_IN_SINH):
10428 if (validate_arg (arg0, REAL_TYPE))
10429 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10430 break;
10432 CASE_FLT_FN (BUILT_IN_COSH):
10433 return fold_builtin_cosh (loc, arg0, type, fndecl);
10435 CASE_FLT_FN (BUILT_IN_TANH):
10436 if (validate_arg (arg0, REAL_TYPE))
10437 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10438 break;
10440 CASE_FLT_FN (BUILT_IN_ERF):
10441 if (validate_arg (arg0, REAL_TYPE))
10442 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10443 break;
10445 CASE_FLT_FN (BUILT_IN_ERFC):
10446 if (validate_arg (arg0, REAL_TYPE))
10447 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10448 break;
10450 CASE_FLT_FN (BUILT_IN_TGAMMA):
10451 if (validate_arg (arg0, REAL_TYPE))
10452 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10453 break;
10455 CASE_FLT_FN (BUILT_IN_EXP):
10456 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10458 CASE_FLT_FN (BUILT_IN_EXP2):
10459 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10461 CASE_FLT_FN (BUILT_IN_EXP10):
10462 CASE_FLT_FN (BUILT_IN_POW10):
10463 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10465 CASE_FLT_FN (BUILT_IN_EXPM1):
10466 if (validate_arg (arg0, REAL_TYPE))
10467 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10468 break;
10470 CASE_FLT_FN (BUILT_IN_LOG):
10471 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10473 CASE_FLT_FN (BUILT_IN_LOG2):
10474 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10476 CASE_FLT_FN (BUILT_IN_LOG10):
10477 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10479 CASE_FLT_FN (BUILT_IN_LOG1P):
10480 if (validate_arg (arg0, REAL_TYPE))
10481 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10482 &dconstm1, NULL, false);
10483 break;
10485 CASE_FLT_FN (BUILT_IN_J0):
10486 if (validate_arg (arg0, REAL_TYPE))
10487 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10488 NULL, NULL, 0);
10489 break;
10491 CASE_FLT_FN (BUILT_IN_J1):
10492 if (validate_arg (arg0, REAL_TYPE))
10493 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10494 NULL, NULL, 0);
10495 break;
10497 CASE_FLT_FN (BUILT_IN_Y0):
10498 if (validate_arg (arg0, REAL_TYPE))
10499 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10500 &dconst0, NULL, false);
10501 break;
10503 CASE_FLT_FN (BUILT_IN_Y1):
10504 if (validate_arg (arg0, REAL_TYPE))
10505 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10506 &dconst0, NULL, false);
10507 break;
10509 CASE_FLT_FN (BUILT_IN_NAN):
10510 case BUILT_IN_NAND32:
10511 case BUILT_IN_NAND64:
10512 case BUILT_IN_NAND128:
10513 return fold_builtin_nan (arg0, type, true);
10515 CASE_FLT_FN (BUILT_IN_NANS):
10516 return fold_builtin_nan (arg0, type, false);
10518 CASE_FLT_FN (BUILT_IN_FLOOR):
10519 return fold_builtin_floor (loc, fndecl, arg0);
10521 CASE_FLT_FN (BUILT_IN_CEIL):
10522 return fold_builtin_ceil (loc, fndecl, arg0);
10524 CASE_FLT_FN (BUILT_IN_TRUNC):
10525 return fold_builtin_trunc (loc, fndecl, arg0);
10527 CASE_FLT_FN (BUILT_IN_ROUND):
10528 return fold_builtin_round (loc, fndecl, arg0);
10530 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10531 CASE_FLT_FN (BUILT_IN_RINT):
10532 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10534 CASE_FLT_FN (BUILT_IN_ICEIL):
10535 CASE_FLT_FN (BUILT_IN_LCEIL):
10536 CASE_FLT_FN (BUILT_IN_LLCEIL):
10537 CASE_FLT_FN (BUILT_IN_LFLOOR):
10538 CASE_FLT_FN (BUILT_IN_IFLOOR):
10539 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10540 CASE_FLT_FN (BUILT_IN_IROUND):
10541 CASE_FLT_FN (BUILT_IN_LROUND):
10542 CASE_FLT_FN (BUILT_IN_LLROUND):
10543 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10545 CASE_FLT_FN (BUILT_IN_IRINT):
10546 CASE_FLT_FN (BUILT_IN_LRINT):
10547 CASE_FLT_FN (BUILT_IN_LLRINT):
10548 return fold_fixed_mathfn (loc, fndecl, arg0);
10550 case BUILT_IN_BSWAP16:
10551 case BUILT_IN_BSWAP32:
10552 case BUILT_IN_BSWAP64:
10553 return fold_builtin_bswap (fndecl, arg0);
10555 CASE_INT_FN (BUILT_IN_FFS):
10556 CASE_INT_FN (BUILT_IN_CLZ):
10557 CASE_INT_FN (BUILT_IN_CTZ):
10558 CASE_INT_FN (BUILT_IN_CLRSB):
10559 CASE_INT_FN (BUILT_IN_POPCOUNT):
10560 CASE_INT_FN (BUILT_IN_PARITY):
10561 return fold_builtin_bitop (fndecl, arg0);
10563 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10564 return fold_builtin_signbit (loc, arg0, type);
10566 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10567 return fold_builtin_significand (loc, arg0, type);
10569 CASE_FLT_FN (BUILT_IN_ILOGB):
10570 CASE_FLT_FN (BUILT_IN_LOGB):
10571 return fold_builtin_logb (loc, arg0, type);
10573 case BUILT_IN_ISASCII:
10574 return fold_builtin_isascii (loc, arg0);
10576 case BUILT_IN_TOASCII:
10577 return fold_builtin_toascii (loc, arg0);
10579 case BUILT_IN_ISDIGIT:
10580 return fold_builtin_isdigit (loc, arg0);
10582 CASE_FLT_FN (BUILT_IN_FINITE):
10583 case BUILT_IN_FINITED32:
10584 case BUILT_IN_FINITED64:
10585 case BUILT_IN_FINITED128:
10586 case BUILT_IN_ISFINITE:
10588 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10589 if (ret)
10590 return ret;
10591 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10594 CASE_FLT_FN (BUILT_IN_ISINF):
10595 case BUILT_IN_ISINFD32:
10596 case BUILT_IN_ISINFD64:
10597 case BUILT_IN_ISINFD128:
10599 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10600 if (ret)
10601 return ret;
10602 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10605 case BUILT_IN_ISNORMAL:
10606 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10608 case BUILT_IN_ISINF_SIGN:
10609 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10611 CASE_FLT_FN (BUILT_IN_ISNAN):
10612 case BUILT_IN_ISNAND32:
10613 case BUILT_IN_ISNAND64:
10614 case BUILT_IN_ISNAND128:
10615 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10617 case BUILT_IN_PRINTF:
10618 case BUILT_IN_PRINTF_UNLOCKED:
10619 case BUILT_IN_VPRINTF:
10620 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10622 case BUILT_IN_FREE:
10623 if (integer_zerop (arg0))
10624 return build_empty_stmt (loc);
10625 break;
10627 default:
10628 break;
10631 return NULL_TREE;
10635 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10636 IGNORE is true if the result of the function call is ignored. This
10637 function returns NULL_TREE if no simplification was possible. */
10639 static tree
10640 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10642 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10643 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10645 switch (fcode)
10647 CASE_FLT_FN (BUILT_IN_JN):
10648 if (validate_arg (arg0, INTEGER_TYPE)
10649 && validate_arg (arg1, REAL_TYPE))
10650 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10651 break;
10653 CASE_FLT_FN (BUILT_IN_YN):
10654 if (validate_arg (arg0, INTEGER_TYPE)
10655 && validate_arg (arg1, REAL_TYPE))
10656 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10657 &dconst0, false);
10658 break;
10660 CASE_FLT_FN (BUILT_IN_DREM):
10661 CASE_FLT_FN (BUILT_IN_REMAINDER):
10662 if (validate_arg (arg0, REAL_TYPE)
10663 && validate_arg (arg1, REAL_TYPE))
10664 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10665 break;
10667 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10668 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10669 if (validate_arg (arg0, REAL_TYPE)
10670 && validate_arg (arg1, POINTER_TYPE))
10671 return do_mpfr_lgamma_r (arg0, arg1, type);
10672 break;
10674 CASE_FLT_FN (BUILT_IN_ATAN2):
10675 if (validate_arg (arg0, REAL_TYPE)
10676 && validate_arg (arg1, REAL_TYPE))
10677 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10678 break;
10680 CASE_FLT_FN (BUILT_IN_FDIM):
10681 if (validate_arg (arg0, REAL_TYPE)
10682 && validate_arg (arg1, REAL_TYPE))
10683 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10684 break;
10686 CASE_FLT_FN (BUILT_IN_HYPOT):
10687 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10689 CASE_FLT_FN (BUILT_IN_CPOW):
10690 if (validate_arg (arg0, COMPLEX_TYPE)
10691 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10692 && validate_arg (arg1, COMPLEX_TYPE)
10693 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10694 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10695 break;
10697 CASE_FLT_FN (BUILT_IN_LDEXP):
10698 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10699 CASE_FLT_FN (BUILT_IN_SCALBN):
10700 CASE_FLT_FN (BUILT_IN_SCALBLN):
10701 return fold_builtin_load_exponent (loc, arg0, arg1,
10702 type, /*ldexp=*/false);
10704 CASE_FLT_FN (BUILT_IN_FREXP):
10705 return fold_builtin_frexp (loc, arg0, arg1, type);
10707 CASE_FLT_FN (BUILT_IN_MODF):
10708 return fold_builtin_modf (loc, arg0, arg1, type);
10710 case BUILT_IN_BZERO:
10711 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10713 case BUILT_IN_FPUTS:
10714 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10716 case BUILT_IN_FPUTS_UNLOCKED:
10717 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10719 case BUILT_IN_STRSTR:
10720 return fold_builtin_strstr (loc, arg0, arg1, type);
10722 case BUILT_IN_STRCAT:
10723 return fold_builtin_strcat (loc, arg0, arg1, NULL_TREE);
10725 case BUILT_IN_STRSPN:
10726 return fold_builtin_strspn (loc, arg0, arg1);
10728 case BUILT_IN_STRCSPN:
10729 return fold_builtin_strcspn (loc, arg0, arg1);
10731 case BUILT_IN_STRCHR:
10732 case BUILT_IN_INDEX:
10733 return fold_builtin_strchr (loc, arg0, arg1, type);
10735 case BUILT_IN_STRRCHR:
10736 case BUILT_IN_RINDEX:
10737 return fold_builtin_strrchr (loc, arg0, arg1, type);
10739 case BUILT_IN_STRCPY:
10740 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10742 case BUILT_IN_STPCPY:
10743 if (ignore)
10745 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10746 if (!fn)
10747 break;
10749 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10751 else
10752 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10753 break;
10755 case BUILT_IN_STRCMP:
10756 return fold_builtin_strcmp (loc, arg0, arg1);
10758 case BUILT_IN_STRPBRK:
10759 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10761 case BUILT_IN_EXPECT:
10762 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
10764 CASE_FLT_FN (BUILT_IN_POW):
10765 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10767 CASE_FLT_FN (BUILT_IN_POWI):
10768 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10770 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10771 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10773 CASE_FLT_FN (BUILT_IN_FMIN):
10774 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10776 CASE_FLT_FN (BUILT_IN_FMAX):
10777 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10779 case BUILT_IN_ISGREATER:
10780 return fold_builtin_unordered_cmp (loc, fndecl,
10781 arg0, arg1, UNLE_EXPR, LE_EXPR);
10782 case BUILT_IN_ISGREATEREQUAL:
10783 return fold_builtin_unordered_cmp (loc, fndecl,
10784 arg0, arg1, UNLT_EXPR, LT_EXPR);
10785 case BUILT_IN_ISLESS:
10786 return fold_builtin_unordered_cmp (loc, fndecl,
10787 arg0, arg1, UNGE_EXPR, GE_EXPR);
10788 case BUILT_IN_ISLESSEQUAL:
10789 return fold_builtin_unordered_cmp (loc, fndecl,
10790 arg0, arg1, UNGT_EXPR, GT_EXPR);
10791 case BUILT_IN_ISLESSGREATER:
10792 return fold_builtin_unordered_cmp (loc, fndecl,
10793 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10794 case BUILT_IN_ISUNORDERED:
10795 return fold_builtin_unordered_cmp (loc, fndecl,
10796 arg0, arg1, UNORDERED_EXPR,
10797 NOP_EXPR);
10799 /* We do the folding for va_start in the expander. */
10800 case BUILT_IN_VA_START:
10801 break;
10803 case BUILT_IN_SPRINTF:
10804 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10806 case BUILT_IN_OBJECT_SIZE:
10807 return fold_builtin_object_size (arg0, arg1);
10809 case BUILT_IN_PRINTF:
10810 case BUILT_IN_PRINTF_UNLOCKED:
10811 case BUILT_IN_VPRINTF:
10812 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10814 case BUILT_IN_PRINTF_CHK:
10815 case BUILT_IN_VPRINTF_CHK:
10816 if (!validate_arg (arg0, INTEGER_TYPE)
10817 || TREE_SIDE_EFFECTS (arg0))
10818 return NULL_TREE;
10819 else
10820 return fold_builtin_printf (loc, fndecl,
10821 arg1, NULL_TREE, ignore, fcode);
10822 break;
10824 case BUILT_IN_FPRINTF:
10825 case BUILT_IN_FPRINTF_UNLOCKED:
10826 case BUILT_IN_VFPRINTF:
10827 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10828 ignore, fcode);
10830 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10831 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10833 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10834 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10836 default:
10837 break;
10839 return NULL_TREE;
10842 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10843 and ARG2. IGNORE is true if the result of the function call is ignored.
10844 This function returns NULL_TREE if no simplification was possible. */
10846 static tree
10847 fold_builtin_3 (location_t loc, tree fndecl,
10848 tree arg0, tree arg1, tree arg2, bool ignore)
10850 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10851 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10852 switch (fcode)
10855 CASE_FLT_FN (BUILT_IN_SINCOS):
10856 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10858 CASE_FLT_FN (BUILT_IN_FMA):
10859 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10860 break;
10862 CASE_FLT_FN (BUILT_IN_REMQUO):
10863 if (validate_arg (arg0, REAL_TYPE)
10864 && validate_arg (arg1, REAL_TYPE)
10865 && validate_arg (arg2, POINTER_TYPE))
10866 return do_mpfr_remquo (arg0, arg1, arg2);
10867 break;
10869 case BUILT_IN_MEMSET:
10870 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10872 case BUILT_IN_BCOPY:
10873 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10874 void_type_node, true, /*endp=*/3);
10876 case BUILT_IN_MEMCPY:
10877 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10878 type, ignore, /*endp=*/0);
10880 case BUILT_IN_MEMPCPY:
10881 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10882 type, ignore, /*endp=*/1);
10884 case BUILT_IN_MEMMOVE:
10885 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10886 type, ignore, /*endp=*/3);
10888 case BUILT_IN_STRNCAT:
10889 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10891 case BUILT_IN_STRNCPY:
10892 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10894 case BUILT_IN_STRNCMP:
10895 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10897 case BUILT_IN_MEMCHR:
10898 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10900 case BUILT_IN_BCMP:
10901 case BUILT_IN_MEMCMP:
10902 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10904 case BUILT_IN_SPRINTF:
10905 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10907 case BUILT_IN_SNPRINTF:
10908 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10910 case BUILT_IN_STRCPY_CHK:
10911 case BUILT_IN_STPCPY_CHK:
10912 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10913 ignore, fcode);
10915 case BUILT_IN_STRCAT_CHK:
10916 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10918 case BUILT_IN_PRINTF_CHK:
10919 case BUILT_IN_VPRINTF_CHK:
10920 if (!validate_arg (arg0, INTEGER_TYPE)
10921 || TREE_SIDE_EFFECTS (arg0))
10922 return NULL_TREE;
10923 else
10924 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10925 break;
10927 case BUILT_IN_FPRINTF:
10928 case BUILT_IN_FPRINTF_UNLOCKED:
10929 case BUILT_IN_VFPRINTF:
10930 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10931 ignore, fcode);
10933 case BUILT_IN_FPRINTF_CHK:
10934 case BUILT_IN_VFPRINTF_CHK:
10935 if (!validate_arg (arg1, INTEGER_TYPE)
10936 || TREE_SIDE_EFFECTS (arg1))
10937 return NULL_TREE;
10938 else
10939 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10940 ignore, fcode);
10942 case BUILT_IN_EXPECT:
10943 return fold_builtin_expect (loc, arg0, arg1, arg2);
10945 default:
10946 break;
10948 return NULL_TREE;
10951 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10952 ARG2, and ARG3. IGNORE is true if the result of the function call is
10953 ignored. This function returns NULL_TREE if no simplification was
10954 possible. */
10956 static tree
10957 fold_builtin_4 (location_t loc, tree fndecl,
10958 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10960 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10962 switch (fcode)
10964 case BUILT_IN_MEMCPY_CHK:
10965 case BUILT_IN_MEMPCPY_CHK:
10966 case BUILT_IN_MEMMOVE_CHK:
10967 case BUILT_IN_MEMSET_CHK:
10968 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10969 NULL_TREE, ignore,
10970 DECL_FUNCTION_CODE (fndecl));
10972 case BUILT_IN_STRNCPY_CHK:
10973 case BUILT_IN_STPNCPY_CHK:
10974 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
10975 ignore, fcode);
10977 case BUILT_IN_STRNCAT_CHK:
10978 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10980 case BUILT_IN_SNPRINTF:
10981 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10983 case BUILT_IN_FPRINTF_CHK:
10984 case BUILT_IN_VFPRINTF_CHK:
10985 if (!validate_arg (arg1, INTEGER_TYPE)
10986 || TREE_SIDE_EFFECTS (arg1))
10987 return NULL_TREE;
10988 else
10989 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10990 ignore, fcode);
10991 break;
10993 default:
10994 break;
10996 return NULL_TREE;
10999 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11000 arguments, where NARGS <= 4. IGNORE is true if the result of the
11001 function call is ignored. This function returns NULL_TREE if no
11002 simplification was possible. Note that this only folds builtins with
11003 fixed argument patterns. Foldings that do varargs-to-varargs
11004 transformations, or that match calls with more than 4 arguments,
11005 need to be handled with fold_builtin_varargs instead. */
11007 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11009 static tree
11010 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11012 tree ret = NULL_TREE;
11014 switch (nargs)
11016 case 0:
11017 ret = fold_builtin_0 (loc, fndecl, ignore);
11018 break;
11019 case 1:
11020 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11021 break;
11022 case 2:
11023 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11024 break;
11025 case 3:
11026 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11027 break;
11028 case 4:
11029 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11030 ignore);
11031 break;
11032 default:
11033 break;
11035 if (ret)
11037 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11038 SET_EXPR_LOCATION (ret, loc);
11039 TREE_NO_WARNING (ret) = 1;
11040 return ret;
11042 return NULL_TREE;
11045 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11046 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11047 of arguments in ARGS to be omitted. OLDNARGS is the number of
11048 elements in ARGS. */
11050 static tree
11051 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11052 int skip, tree fndecl, int n, va_list newargs)
11054 int nargs = oldnargs - skip + n;
11055 tree *buffer;
11057 if (n > 0)
11059 int i, j;
11061 buffer = XALLOCAVEC (tree, nargs);
11062 for (i = 0; i < n; i++)
11063 buffer[i] = va_arg (newargs, tree);
11064 for (j = skip; j < oldnargs; j++, i++)
11065 buffer[i] = args[j];
11067 else
11068 buffer = args + skip;
11070 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11073 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11074 list ARGS along with N new arguments specified as the "..."
11075 parameters. SKIP is the number of arguments in ARGS to be omitted.
11076 OLDNARGS is the number of elements in ARGS. */
11078 static tree
11079 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11080 int skip, tree fndecl, int n, ...)
11082 va_list ap;
11083 tree t;
11085 va_start (ap, n);
11086 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11087 va_end (ap);
11089 return t;
11092 /* Return true if FNDECL shouldn't be folded right now.
11093 If a built-in function has an inline attribute always_inline
11094 wrapper, defer folding it after always_inline functions have
11095 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11096 might not be performed. */
11098 bool
11099 avoid_folding_inline_builtin (tree fndecl)
11101 return (DECL_DECLARED_INLINE_P (fndecl)
11102 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11103 && cfun
11104 && !cfun->always_inline_functions_inlined
11105 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11108 /* A wrapper function for builtin folding that prevents warnings for
11109 "statement without effect" and the like, caused by removing the
11110 call node earlier than the warning is generated. */
11112 tree
11113 fold_call_expr (location_t loc, tree exp, bool ignore)
11115 tree ret = NULL_TREE;
11116 tree fndecl = get_callee_fndecl (exp);
11117 if (fndecl
11118 && TREE_CODE (fndecl) == FUNCTION_DECL
11119 && DECL_BUILT_IN (fndecl)
11120 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11121 yet. Defer folding until we see all the arguments
11122 (after inlining). */
11123 && !CALL_EXPR_VA_ARG_PACK (exp))
11125 int nargs = call_expr_nargs (exp);
11127 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11128 instead last argument is __builtin_va_arg_pack (). Defer folding
11129 even in that case, until arguments are finalized. */
11130 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11132 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11133 if (fndecl2
11134 && TREE_CODE (fndecl2) == FUNCTION_DECL
11135 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11136 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11137 return NULL_TREE;
11140 if (avoid_folding_inline_builtin (fndecl))
11141 return NULL_TREE;
11143 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11144 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11145 CALL_EXPR_ARGP (exp), ignore);
11146 else
11148 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11150 tree *args = CALL_EXPR_ARGP (exp);
11151 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11153 if (!ret)
11154 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11155 if (ret)
11156 return ret;
11159 return NULL_TREE;
11162 /* Conveniently construct a function call expression. FNDECL names the
11163 function to be called and N arguments are passed in the array
11164 ARGARRAY. */
11166 tree
11167 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11169 tree fntype = TREE_TYPE (fndecl);
11170 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11172 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11175 /* Conveniently construct a function call expression. FNDECL names the
11176 function to be called and the arguments are passed in the vector
11177 VEC. */
11179 tree
11180 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11182 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11183 vec_safe_address (vec));
11187 /* Conveniently construct a function call expression. FNDECL names the
11188 function to be called, N is the number of arguments, and the "..."
11189 parameters are the argument expressions. */
11191 tree
11192 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11194 va_list ap;
11195 tree *argarray = XALLOCAVEC (tree, n);
11196 int i;
11198 va_start (ap, n);
11199 for (i = 0; i < n; i++)
11200 argarray[i] = va_arg (ap, tree);
11201 va_end (ap);
11202 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11205 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11206 varargs macros aren't supported by all bootstrap compilers. */
11208 tree
11209 build_call_expr (tree fndecl, int n, ...)
11211 va_list ap;
11212 tree *argarray = XALLOCAVEC (tree, n);
11213 int i;
11215 va_start (ap, n);
11216 for (i = 0; i < n; i++)
11217 argarray[i] = va_arg (ap, tree);
11218 va_end (ap);
11219 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11222 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11223 N arguments are passed in the array ARGARRAY. */
11225 tree
11226 fold_builtin_call_array (location_t loc, tree type,
11227 tree fn,
11228 int n,
11229 tree *argarray)
11231 tree ret = NULL_TREE;
11232 tree exp;
11234 if (TREE_CODE (fn) == ADDR_EXPR)
11236 tree fndecl = TREE_OPERAND (fn, 0);
11237 if (TREE_CODE (fndecl) == FUNCTION_DECL
11238 && DECL_BUILT_IN (fndecl))
11240 /* If last argument is __builtin_va_arg_pack (), arguments to this
11241 function are not finalized yet. Defer folding until they are. */
11242 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11244 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11245 if (fndecl2
11246 && TREE_CODE (fndecl2) == FUNCTION_DECL
11247 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11248 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11249 return build_call_array_loc (loc, type, fn, n, argarray);
11251 if (avoid_folding_inline_builtin (fndecl))
11252 return build_call_array_loc (loc, type, fn, n, argarray);
11253 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11255 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11256 if (ret)
11257 return ret;
11259 return build_call_array_loc (loc, type, fn, n, argarray);
11261 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11263 /* First try the transformations that don't require consing up
11264 an exp. */
11265 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11266 if (ret)
11267 return ret;
11270 /* If we got this far, we need to build an exp. */
11271 exp = build_call_array_loc (loc, type, fn, n, argarray);
11272 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11273 return ret ? ret : exp;
11277 return build_call_array_loc (loc, type, fn, n, argarray);
11280 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11281 along with N new arguments specified as the "..." parameters. SKIP
11282 is the number of arguments in EXP to be omitted. This function is used
11283 to do varargs-to-varargs transformations. */
11285 static tree
11286 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11288 va_list ap;
11289 tree t;
11291 va_start (ap, n);
11292 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11293 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11294 va_end (ap);
11296 return t;
11299 /* Validate a single argument ARG against a tree code CODE representing
11300 a type. */
11302 static bool
11303 validate_arg (const_tree arg, enum tree_code code)
11305 if (!arg)
11306 return false;
11307 else if (code == POINTER_TYPE)
11308 return POINTER_TYPE_P (TREE_TYPE (arg));
11309 else if (code == INTEGER_TYPE)
11310 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11311 return code == TREE_CODE (TREE_TYPE (arg));
11314 /* This function validates the types of a function call argument list
11315 against a specified list of tree_codes. If the last specifier is a 0,
11316 that represents an ellipses, otherwise the last specifier must be a
11317 VOID_TYPE.
11319 This is the GIMPLE version of validate_arglist. Eventually we want to
11320 completely convert builtins.c to work from GIMPLEs and the tree based
11321 validate_arglist will then be removed. */
11323 bool
11324 validate_gimple_arglist (const_gimple call, ...)
11326 enum tree_code code;
11327 bool res = 0;
11328 va_list ap;
11329 const_tree arg;
11330 size_t i;
11332 va_start (ap, call);
11333 i = 0;
11337 code = (enum tree_code) va_arg (ap, int);
11338 switch (code)
11340 case 0:
11341 /* This signifies an ellipses, any further arguments are all ok. */
11342 res = true;
11343 goto end;
11344 case VOID_TYPE:
11345 /* This signifies an endlink, if no arguments remain, return
11346 true, otherwise return false. */
11347 res = (i == gimple_call_num_args (call));
11348 goto end;
11349 default:
11350 /* If no parameters remain or the parameter's code does not
11351 match the specified code, return false. Otherwise continue
11352 checking any remaining arguments. */
11353 arg = gimple_call_arg (call, i++);
11354 if (!validate_arg (arg, code))
11355 goto end;
11356 break;
11359 while (1);
11361 /* We need gotos here since we can only have one VA_CLOSE in a
11362 function. */
11363 end: ;
11364 va_end (ap);
11366 return res;
11369 /* Default target-specific builtin expander that does nothing. */
11372 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11373 rtx target ATTRIBUTE_UNUSED,
11374 rtx subtarget ATTRIBUTE_UNUSED,
11375 enum machine_mode mode ATTRIBUTE_UNUSED,
11376 int ignore ATTRIBUTE_UNUSED)
11378 return NULL_RTX;
11381 /* Returns true is EXP represents data that would potentially reside
11382 in a readonly section. */
11384 static bool
11385 readonly_data_expr (tree exp)
11387 STRIP_NOPS (exp);
11389 if (TREE_CODE (exp) != ADDR_EXPR)
11390 return false;
11392 exp = get_base_address (TREE_OPERAND (exp, 0));
11393 if (!exp)
11394 return false;
11396 /* Make sure we call decl_readonly_section only for trees it
11397 can handle (since it returns true for everything it doesn't
11398 understand). */
11399 if (TREE_CODE (exp) == STRING_CST
11400 || TREE_CODE (exp) == CONSTRUCTOR
11401 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11402 return decl_readonly_section (exp, 0);
11403 else
11404 return false;
11407 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11408 to the call, and TYPE is its return type.
11410 Return NULL_TREE if no simplification was possible, otherwise return the
11411 simplified form of the call as a tree.
11413 The simplified form may be a constant or other expression which
11414 computes the same value, but in a more efficient manner (including
11415 calls to other builtin functions).
11417 The call may contain arguments which need to be evaluated, but
11418 which are not useful to determine the result of the call. In
11419 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11420 COMPOUND_EXPR will be an argument which must be evaluated.
11421 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11422 COMPOUND_EXPR in the chain will contain the tree for the simplified
11423 form of the builtin function call. */
11425 static tree
11426 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11428 if (!validate_arg (s1, POINTER_TYPE)
11429 || !validate_arg (s2, POINTER_TYPE))
11430 return NULL_TREE;
11431 else
11433 tree fn;
11434 const char *p1, *p2;
11436 p2 = c_getstr (s2);
11437 if (p2 == NULL)
11438 return NULL_TREE;
11440 p1 = c_getstr (s1);
11441 if (p1 != NULL)
11443 const char *r = strstr (p1, p2);
11444 tree tem;
11446 if (r == NULL)
11447 return build_int_cst (TREE_TYPE (s1), 0);
11449 /* Return an offset into the constant string argument. */
11450 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11451 return fold_convert_loc (loc, type, tem);
11454 /* The argument is const char *, and the result is char *, so we need
11455 a type conversion here to avoid a warning. */
11456 if (p2[0] == '\0')
11457 return fold_convert_loc (loc, type, s1);
11459 if (p2[1] != '\0')
11460 return NULL_TREE;
11462 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11463 if (!fn)
11464 return NULL_TREE;
11466 /* New argument list transforming strstr(s1, s2) to
11467 strchr(s1, s2[0]). */
11468 return build_call_expr_loc (loc, fn, 2, s1,
11469 build_int_cst (integer_type_node, p2[0]));
11473 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11474 the call, and TYPE is its return type.
11476 Return NULL_TREE if no simplification was possible, otherwise return the
11477 simplified form of the call as a tree.
11479 The simplified form may be a constant or other expression which
11480 computes the same value, but in a more efficient manner (including
11481 calls to other builtin functions).
11483 The call may contain arguments which need to be evaluated, but
11484 which are not useful to determine the result of the call. In
11485 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11486 COMPOUND_EXPR will be an argument which must be evaluated.
11487 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11488 COMPOUND_EXPR in the chain will contain the tree for the simplified
11489 form of the builtin function call. */
11491 static tree
11492 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11494 if (!validate_arg (s1, POINTER_TYPE)
11495 || !validate_arg (s2, INTEGER_TYPE))
11496 return NULL_TREE;
11497 else
11499 const char *p1;
11501 if (TREE_CODE (s2) != INTEGER_CST)
11502 return NULL_TREE;
11504 p1 = c_getstr (s1);
11505 if (p1 != NULL)
11507 char c;
11508 const char *r;
11509 tree tem;
11511 if (target_char_cast (s2, &c))
11512 return NULL_TREE;
11514 r = strchr (p1, c);
11516 if (r == NULL)
11517 return build_int_cst (TREE_TYPE (s1), 0);
11519 /* Return an offset into the constant string argument. */
11520 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11521 return fold_convert_loc (loc, type, tem);
11523 return NULL_TREE;
11527 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11528 the call, and TYPE is its return type.
11530 Return NULL_TREE if no simplification was possible, otherwise return the
11531 simplified form of the call as a tree.
11533 The simplified form may be a constant or other expression which
11534 computes the same value, but in a more efficient manner (including
11535 calls to other builtin functions).
11537 The call may contain arguments which need to be evaluated, but
11538 which are not useful to determine the result of the call. In
11539 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11540 COMPOUND_EXPR will be an argument which must be evaluated.
11541 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11542 COMPOUND_EXPR in the chain will contain the tree for the simplified
11543 form of the builtin function call. */
11545 static tree
11546 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11548 if (!validate_arg (s1, POINTER_TYPE)
11549 || !validate_arg (s2, INTEGER_TYPE))
11550 return NULL_TREE;
11551 else
11553 tree fn;
11554 const char *p1;
11556 if (TREE_CODE (s2) != INTEGER_CST)
11557 return NULL_TREE;
11559 p1 = c_getstr (s1);
11560 if (p1 != NULL)
11562 char c;
11563 const char *r;
11564 tree tem;
11566 if (target_char_cast (s2, &c))
11567 return NULL_TREE;
11569 r = strrchr (p1, c);
11571 if (r == NULL)
11572 return build_int_cst (TREE_TYPE (s1), 0);
11574 /* Return an offset into the constant string argument. */
11575 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11576 return fold_convert_loc (loc, type, tem);
11579 if (! integer_zerop (s2))
11580 return NULL_TREE;
11582 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11583 if (!fn)
11584 return NULL_TREE;
11586 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11587 return build_call_expr_loc (loc, fn, 2, s1, s2);
11591 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11592 to the call, and TYPE is its return type.
11594 Return NULL_TREE if no simplification was possible, otherwise return the
11595 simplified form of the call as a tree.
11597 The simplified form may be a constant or other expression which
11598 computes the same value, but in a more efficient manner (including
11599 calls to other builtin functions).
11601 The call may contain arguments which need to be evaluated, but
11602 which are not useful to determine the result of the call. In
11603 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11604 COMPOUND_EXPR will be an argument which must be evaluated.
11605 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11606 COMPOUND_EXPR in the chain will contain the tree for the simplified
11607 form of the builtin function call. */
11609 static tree
11610 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11612 if (!validate_arg (s1, POINTER_TYPE)
11613 || !validate_arg (s2, POINTER_TYPE))
11614 return NULL_TREE;
11615 else
11617 tree fn;
11618 const char *p1, *p2;
11620 p2 = c_getstr (s2);
11621 if (p2 == NULL)
11622 return NULL_TREE;
11624 p1 = c_getstr (s1);
11625 if (p1 != NULL)
11627 const char *r = strpbrk (p1, p2);
11628 tree tem;
11630 if (r == NULL)
11631 return build_int_cst (TREE_TYPE (s1), 0);
11633 /* Return an offset into the constant string argument. */
11634 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11635 return fold_convert_loc (loc, type, tem);
11638 if (p2[0] == '\0')
11639 /* strpbrk(x, "") == NULL.
11640 Evaluate and ignore s1 in case it had side-effects. */
11641 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11643 if (p2[1] != '\0')
11644 return NULL_TREE; /* Really call strpbrk. */
11646 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11647 if (!fn)
11648 return NULL_TREE;
11650 /* New argument list transforming strpbrk(s1, s2) to
11651 strchr(s1, s2[0]). */
11652 return build_call_expr_loc (loc, fn, 2, s1,
11653 build_int_cst (integer_type_node, p2[0]));
11657 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11658 to the call.
11660 Return NULL_TREE if no simplification was possible, otherwise return the
11661 simplified form of the call as a tree.
11663 The simplified form may be a constant or other expression which
11664 computes the same value, but in a more efficient manner (including
11665 calls to other builtin functions).
11667 The call may contain arguments which need to be evaluated, but
11668 which are not useful to determine the result of the call. In
11669 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11670 COMPOUND_EXPR will be an argument which must be evaluated.
11671 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11672 COMPOUND_EXPR in the chain will contain the tree for the simplified
11673 form of the builtin function call. */
11675 tree
11676 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src,
11677 tree len)
11679 if (!validate_arg (dst, POINTER_TYPE)
11680 || !validate_arg (src, POINTER_TYPE))
11681 return NULL_TREE;
11682 else
11684 const char *p = c_getstr (src);
11686 /* If the string length is zero, return the dst parameter. */
11687 if (p && *p == '\0')
11688 return dst;
11690 if (optimize_insn_for_speed_p ())
11692 /* See if we can store by pieces into (dst + strlen(dst)). */
11693 tree newdst, call;
11694 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11695 tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
11697 if (!strlen_fn || !memcpy_fn)
11698 return NULL_TREE;
11700 /* If the length of the source string isn't computable don't
11701 split strcat into strlen and memcpy. */
11702 if (! len)
11703 len = c_strlen (src, 1);
11704 if (! len || TREE_SIDE_EFFECTS (len))
11705 return NULL_TREE;
11707 /* Stabilize the argument list. */
11708 dst = builtin_save_expr (dst);
11710 /* Create strlen (dst). */
11711 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11712 /* Create (dst p+ strlen (dst)). */
11714 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11715 newdst = builtin_save_expr (newdst);
11717 len = fold_convert_loc (loc, size_type_node, len);
11718 len = size_binop_loc (loc, PLUS_EXPR, len,
11719 build_int_cst (size_type_node, 1));
11721 call = build_call_expr_loc (loc, memcpy_fn, 3, newdst, src, len);
11722 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11724 return NULL_TREE;
11728 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11729 arguments to the call.
11731 Return NULL_TREE if no simplification was possible, otherwise return the
11732 simplified form of the call as a tree.
11734 The simplified form may be a constant or other expression which
11735 computes the same value, but in a more efficient manner (including
11736 calls to other builtin functions).
11738 The call may contain arguments which need to be evaluated, but
11739 which are not useful to determine the result of the call. In
11740 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11741 COMPOUND_EXPR will be an argument which must be evaluated.
11742 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11743 COMPOUND_EXPR in the chain will contain the tree for the simplified
11744 form of the builtin function call. */
11746 static tree
11747 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11749 if (!validate_arg (dst, POINTER_TYPE)
11750 || !validate_arg (src, POINTER_TYPE)
11751 || !validate_arg (len, INTEGER_TYPE))
11752 return NULL_TREE;
11753 else
11755 const char *p = c_getstr (src);
11757 /* If the requested length is zero, or the src parameter string
11758 length is zero, return the dst parameter. */
11759 if (integer_zerop (len) || (p && *p == '\0'))
11760 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11762 /* If the requested len is greater than or equal to the string
11763 length, call strcat. */
11764 if (TREE_CODE (len) == INTEGER_CST && p
11765 && compare_tree_int (len, strlen (p)) >= 0)
11767 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11769 /* If the replacement _DECL isn't initialized, don't do the
11770 transformation. */
11771 if (!fn)
11772 return NULL_TREE;
11774 return build_call_expr_loc (loc, fn, 2, dst, src);
11776 return NULL_TREE;
11780 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11781 to the call.
11783 Return NULL_TREE if no simplification was possible, otherwise return the
11784 simplified form of the call as a tree.
11786 The simplified form may be a constant or other expression which
11787 computes the same value, but in a more efficient manner (including
11788 calls to other builtin functions).
11790 The call may contain arguments which need to be evaluated, but
11791 which are not useful to determine the result of the call. In
11792 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11793 COMPOUND_EXPR will be an argument which must be evaluated.
11794 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11795 COMPOUND_EXPR in the chain will contain the tree for the simplified
11796 form of the builtin function call. */
11798 static tree
11799 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11801 if (!validate_arg (s1, POINTER_TYPE)
11802 || !validate_arg (s2, POINTER_TYPE))
11803 return NULL_TREE;
11804 else
11806 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11808 /* If both arguments are constants, evaluate at compile-time. */
11809 if (p1 && p2)
11811 const size_t r = strspn (p1, p2);
11812 return build_int_cst (size_type_node, r);
11815 /* If either argument is "", return NULL_TREE. */
11816 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11817 /* Evaluate and ignore both arguments in case either one has
11818 side-effects. */
11819 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11820 s1, s2);
11821 return NULL_TREE;
11825 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11826 to the call.
11828 Return NULL_TREE if no simplification was possible, otherwise return the
11829 simplified form of the call as a tree.
11831 The simplified form may be a constant or other expression which
11832 computes the same value, but in a more efficient manner (including
11833 calls to other builtin functions).
11835 The call may contain arguments which need to be evaluated, but
11836 which are not useful to determine the result of the call. In
11837 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11838 COMPOUND_EXPR will be an argument which must be evaluated.
11839 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11840 COMPOUND_EXPR in the chain will contain the tree for the simplified
11841 form of the builtin function call. */
11843 static tree
11844 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11846 if (!validate_arg (s1, POINTER_TYPE)
11847 || !validate_arg (s2, POINTER_TYPE))
11848 return NULL_TREE;
11849 else
11851 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11853 /* If both arguments are constants, evaluate at compile-time. */
11854 if (p1 && p2)
11856 const size_t r = strcspn (p1, p2);
11857 return build_int_cst (size_type_node, r);
11860 /* If the first argument is "", return NULL_TREE. */
11861 if (p1 && *p1 == '\0')
11863 /* Evaluate and ignore argument s2 in case it has
11864 side-effects. */
11865 return omit_one_operand_loc (loc, size_type_node,
11866 size_zero_node, s2);
11869 /* If the second argument is "", return __builtin_strlen(s1). */
11870 if (p2 && *p2 == '\0')
11872 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11874 /* If the replacement _DECL isn't initialized, don't do the
11875 transformation. */
11876 if (!fn)
11877 return NULL_TREE;
11879 return build_call_expr_loc (loc, fn, 1, s1);
11881 return NULL_TREE;
11885 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11886 to the call. IGNORE is true if the value returned
11887 by the builtin will be ignored. UNLOCKED is true is true if this
11888 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11889 the known length of the string. Return NULL_TREE if no simplification
11890 was possible. */
11892 tree
11893 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11894 bool ignore, bool unlocked, tree len)
11896 /* If we're using an unlocked function, assume the other unlocked
11897 functions exist explicitly. */
11898 tree const fn_fputc = (unlocked
11899 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11900 : builtin_decl_implicit (BUILT_IN_FPUTC));
11901 tree const fn_fwrite = (unlocked
11902 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
11903 : builtin_decl_implicit (BUILT_IN_FWRITE));
11905 /* If the return value is used, don't do the transformation. */
11906 if (!ignore)
11907 return NULL_TREE;
11909 /* Verify the arguments in the original call. */
11910 if (!validate_arg (arg0, POINTER_TYPE)
11911 || !validate_arg (arg1, POINTER_TYPE))
11912 return NULL_TREE;
11914 if (! len)
11915 len = c_strlen (arg0, 0);
11917 /* Get the length of the string passed to fputs. If the length
11918 can't be determined, punt. */
11919 if (!len
11920 || TREE_CODE (len) != INTEGER_CST)
11921 return NULL_TREE;
11923 switch (compare_tree_int (len, 1))
11925 case -1: /* length is 0, delete the call entirely . */
11926 return omit_one_operand_loc (loc, integer_type_node,
11927 integer_zero_node, arg1);;
11929 case 0: /* length is 1, call fputc. */
11931 const char *p = c_getstr (arg0);
11933 if (p != NULL)
11935 if (fn_fputc)
11936 return build_call_expr_loc (loc, fn_fputc, 2,
11937 build_int_cst
11938 (integer_type_node, p[0]), arg1);
11939 else
11940 return NULL_TREE;
11943 /* FALLTHROUGH */
11944 case 1: /* length is greater than 1, call fwrite. */
11946 /* If optimizing for size keep fputs. */
11947 if (optimize_function_for_size_p (cfun))
11948 return NULL_TREE;
11949 /* New argument list transforming fputs(string, stream) to
11950 fwrite(string, 1, len, stream). */
11951 if (fn_fwrite)
11952 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11953 size_one_node, len, arg1);
11954 else
11955 return NULL_TREE;
11957 default:
11958 gcc_unreachable ();
11960 return NULL_TREE;
11963 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11964 produced. False otherwise. This is done so that we don't output the error
11965 or warning twice or three times. */
11967 bool
11968 fold_builtin_next_arg (tree exp, bool va_start_p)
11970 tree fntype = TREE_TYPE (current_function_decl);
11971 int nargs = call_expr_nargs (exp);
11972 tree arg;
11973 /* There is good chance the current input_location points inside the
11974 definition of the va_start macro (perhaps on the token for
11975 builtin) in a system header, so warnings will not be emitted.
11976 Use the location in real source code. */
11977 source_location current_location =
11978 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11979 NULL);
11981 if (!stdarg_p (fntype))
11983 error ("%<va_start%> used in function with fixed args");
11984 return true;
11987 if (va_start_p)
11989 if (va_start_p && (nargs != 2))
11991 error ("wrong number of arguments to function %<va_start%>");
11992 return true;
11994 arg = CALL_EXPR_ARG (exp, 1);
11996 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11997 when we checked the arguments and if needed issued a warning. */
11998 else
12000 if (nargs == 0)
12002 /* Evidently an out of date version of <stdarg.h>; can't validate
12003 va_start's second argument, but can still work as intended. */
12004 warning_at (current_location,
12005 OPT_Wvarargs,
12006 "%<__builtin_next_arg%> called without an argument");
12007 return true;
12009 else if (nargs > 1)
12011 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12012 return true;
12014 arg = CALL_EXPR_ARG (exp, 0);
12017 if (TREE_CODE (arg) == SSA_NAME)
12018 arg = SSA_NAME_VAR (arg);
12020 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12021 or __builtin_next_arg (0) the first time we see it, after checking
12022 the arguments and if needed issuing a warning. */
12023 if (!integer_zerop (arg))
12025 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12027 /* Strip off all nops for the sake of the comparison. This
12028 is not quite the same as STRIP_NOPS. It does more.
12029 We must also strip off INDIRECT_EXPR for C++ reference
12030 parameters. */
12031 while (CONVERT_EXPR_P (arg)
12032 || TREE_CODE (arg) == INDIRECT_REF)
12033 arg = TREE_OPERAND (arg, 0);
12034 if (arg != last_parm)
12036 /* FIXME: Sometimes with the tree optimizers we can get the
12037 not the last argument even though the user used the last
12038 argument. We just warn and set the arg to be the last
12039 argument so that we will get wrong-code because of
12040 it. */
12041 warning_at (current_location,
12042 OPT_Wvarargs,
12043 "second parameter of %<va_start%> not last named argument");
12046 /* Undefined by C99 7.15.1.4p4 (va_start):
12047 "If the parameter parmN is declared with the register storage
12048 class, with a function or array type, or with a type that is
12049 not compatible with the type that results after application of
12050 the default argument promotions, the behavior is undefined."
12052 else if (DECL_REGISTER (arg))
12054 warning_at (current_location,
12055 OPT_Wvarargs,
12056 "undefined behaviour when second parameter of "
12057 "%<va_start%> is declared with %<register%> storage");
12060 /* We want to verify the second parameter just once before the tree
12061 optimizers are run and then avoid keeping it in the tree,
12062 as otherwise we could warn even for correct code like:
12063 void foo (int i, ...)
12064 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12065 if (va_start_p)
12066 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12067 else
12068 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12070 return false;
12074 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12075 ORIG may be null if this is a 2-argument call. We don't attempt to
12076 simplify calls with more than 3 arguments.
12078 Return NULL_TREE if no simplification was possible, otherwise return the
12079 simplified form of the call as a tree. If IGNORED is true, it means that
12080 the caller does not use the returned value of the function. */
12082 static tree
12083 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12084 tree orig, int ignored)
12086 tree call, retval;
12087 const char *fmt_str = NULL;
12089 /* Verify the required arguments in the original call. We deal with two
12090 types of sprintf() calls: 'sprintf (str, fmt)' and
12091 'sprintf (dest, "%s", orig)'. */
12092 if (!validate_arg (dest, POINTER_TYPE)
12093 || !validate_arg (fmt, POINTER_TYPE))
12094 return NULL_TREE;
12095 if (orig && !validate_arg (orig, POINTER_TYPE))
12096 return NULL_TREE;
12098 /* Check whether the format is a literal string constant. */
12099 fmt_str = c_getstr (fmt);
12100 if (fmt_str == NULL)
12101 return NULL_TREE;
12103 call = NULL_TREE;
12104 retval = NULL_TREE;
12106 if (!init_target_chars ())
12107 return NULL_TREE;
12109 /* If the format doesn't contain % args or %%, use strcpy. */
12110 if (strchr (fmt_str, target_percent) == NULL)
12112 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12114 if (!fn)
12115 return NULL_TREE;
12117 /* Don't optimize sprintf (buf, "abc", ptr++). */
12118 if (orig)
12119 return NULL_TREE;
12121 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12122 'format' is known to contain no % formats. */
12123 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;
12132 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12134 if (!fn)
12135 return NULL_TREE;
12137 /* Don't crash on sprintf (str1, "%s"). */
12138 if (!orig)
12139 return NULL_TREE;
12141 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12142 if (!ignored)
12144 retval = c_strlen (orig, 1);
12145 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12146 return NULL_TREE;
12148 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12151 if (call && retval)
12153 retval = fold_convert_loc
12154 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12155 retval);
12156 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12158 else
12159 return call;
12162 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12163 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12164 attempt to simplify calls with more than 4 arguments.
12166 Return NULL_TREE if no simplification was possible, otherwise return the
12167 simplified form of the call as a tree. If IGNORED is true, it means that
12168 the caller does not use the returned value of the function. */
12170 static tree
12171 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12172 tree orig, int ignored)
12174 tree call, retval;
12175 const char *fmt_str = NULL;
12176 unsigned HOST_WIDE_INT destlen;
12178 /* Verify the required arguments in the original call. We deal with two
12179 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12180 'snprintf (dest, cst, "%s", orig)'. */
12181 if (!validate_arg (dest, POINTER_TYPE)
12182 || !validate_arg (destsize, INTEGER_TYPE)
12183 || !validate_arg (fmt, POINTER_TYPE))
12184 return NULL_TREE;
12185 if (orig && !validate_arg (orig, POINTER_TYPE))
12186 return NULL_TREE;
12188 if (!tree_fits_uhwi_p (destsize))
12189 return NULL_TREE;
12191 /* Check whether the format is a literal string constant. */
12192 fmt_str = c_getstr (fmt);
12193 if (fmt_str == NULL)
12194 return NULL_TREE;
12196 call = NULL_TREE;
12197 retval = NULL_TREE;
12199 if (!init_target_chars ())
12200 return NULL_TREE;
12202 destlen = tree_to_uhwi (destsize);
12204 /* If the format doesn't contain % args or %%, use strcpy. */
12205 if (strchr (fmt_str, target_percent) == NULL)
12207 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12208 size_t len = strlen (fmt_str);
12210 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12211 if (orig)
12212 return NULL_TREE;
12214 /* We could expand this as
12215 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12216 or to
12217 memcpy (str, fmt_with_nul_at_cstm1, cst);
12218 but in the former case that might increase code size
12219 and in the latter case grow .rodata section too much.
12220 So punt for now. */
12221 if (len >= destlen)
12222 return NULL_TREE;
12224 if (!fn)
12225 return NULL_TREE;
12227 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12228 'format' is known to contain no % formats and
12229 strlen (fmt) < cst. */
12230 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12232 if (!ignored)
12233 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12236 /* If the format is "%s", use strcpy if the result isn't used. */
12237 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12239 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12240 unsigned HOST_WIDE_INT origlen;
12242 /* Don't crash on snprintf (str1, cst, "%s"). */
12243 if (!orig)
12244 return NULL_TREE;
12246 retval = c_strlen (orig, 1);
12247 if (!retval || !tree_fits_uhwi_p (retval))
12248 return NULL_TREE;
12250 origlen = tree_to_uhwi (retval);
12251 /* We could expand this as
12252 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12253 or to
12254 memcpy (str1, str2_with_nul_at_cstm1, cst);
12255 but in the former case that might increase code size
12256 and in the latter case grow .rodata section too much.
12257 So punt for now. */
12258 if (origlen >= destlen)
12259 return NULL_TREE;
12261 /* Convert snprintf (str1, cst, "%s", str2) into
12262 strcpy (str1, str2) if strlen (str2) < cst. */
12263 if (!fn)
12264 return NULL_TREE;
12266 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12268 if (ignored)
12269 retval = NULL_TREE;
12272 if (call && retval)
12274 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12275 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12276 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12278 else
12279 return call;
12282 /* Expand a call EXP to __builtin_object_size. */
12285 expand_builtin_object_size (tree exp)
12287 tree ost;
12288 int object_size_type;
12289 tree fndecl = get_callee_fndecl (exp);
12291 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12293 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12294 exp, fndecl);
12295 expand_builtin_trap ();
12296 return const0_rtx;
12299 ost = CALL_EXPR_ARG (exp, 1);
12300 STRIP_NOPS (ost);
12302 if (TREE_CODE (ost) != INTEGER_CST
12303 || tree_int_cst_sgn (ost) < 0
12304 || compare_tree_int (ost, 3) > 0)
12306 error ("%Klast argument of %D is not integer constant between 0 and 3",
12307 exp, fndecl);
12308 expand_builtin_trap ();
12309 return const0_rtx;
12312 object_size_type = tree_to_shwi (ost);
12314 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12317 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12318 FCODE is the BUILT_IN_* to use.
12319 Return NULL_RTX if we failed; the caller should emit a normal call,
12320 otherwise try to get the result in TARGET, if convenient (and in
12321 mode MODE if that's convenient). */
12323 static rtx
12324 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12325 enum built_in_function fcode)
12327 tree dest, src, len, size;
12329 if (!validate_arglist (exp,
12330 POINTER_TYPE,
12331 fcode == BUILT_IN_MEMSET_CHK
12332 ? INTEGER_TYPE : POINTER_TYPE,
12333 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12334 return NULL_RTX;
12336 dest = CALL_EXPR_ARG (exp, 0);
12337 src = CALL_EXPR_ARG (exp, 1);
12338 len = CALL_EXPR_ARG (exp, 2);
12339 size = CALL_EXPR_ARG (exp, 3);
12341 if (! tree_fits_uhwi_p (size))
12342 return NULL_RTX;
12344 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
12346 tree fn;
12348 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12350 warning_at (tree_nonartificial_location (exp),
12351 0, "%Kcall to %D will always overflow destination buffer",
12352 exp, get_callee_fndecl (exp));
12353 return NULL_RTX;
12356 fn = NULL_TREE;
12357 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12358 mem{cpy,pcpy,move,set} is available. */
12359 switch (fcode)
12361 case BUILT_IN_MEMCPY_CHK:
12362 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12363 break;
12364 case BUILT_IN_MEMPCPY_CHK:
12365 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12366 break;
12367 case BUILT_IN_MEMMOVE_CHK:
12368 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12369 break;
12370 case BUILT_IN_MEMSET_CHK:
12371 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12372 break;
12373 default:
12374 break;
12377 if (! fn)
12378 return NULL_RTX;
12380 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12381 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12382 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12383 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12385 else if (fcode == BUILT_IN_MEMSET_CHK)
12386 return NULL_RTX;
12387 else
12389 unsigned int dest_align = get_pointer_alignment (dest);
12391 /* If DEST is not a pointer type, call the normal function. */
12392 if (dest_align == 0)
12393 return NULL_RTX;
12395 /* If SRC and DEST are the same (and not volatile), do nothing. */
12396 if (operand_equal_p (src, dest, 0))
12398 tree expr;
12400 if (fcode != BUILT_IN_MEMPCPY_CHK)
12402 /* Evaluate and ignore LEN in case it has side-effects. */
12403 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12404 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12407 expr = fold_build_pointer_plus (dest, len);
12408 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12411 /* __memmove_chk special case. */
12412 if (fcode == BUILT_IN_MEMMOVE_CHK)
12414 unsigned int src_align = get_pointer_alignment (src);
12416 if (src_align == 0)
12417 return NULL_RTX;
12419 /* If src is categorized for a readonly section we can use
12420 normal __memcpy_chk. */
12421 if (readonly_data_expr (src))
12423 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12424 if (!fn)
12425 return NULL_RTX;
12426 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12427 dest, src, len, size);
12428 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12429 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12430 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12433 return NULL_RTX;
12437 /* Emit warning if a buffer overflow is detected at compile time. */
12439 static void
12440 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12442 int is_strlen = 0;
12443 tree len, size;
12444 location_t loc = tree_nonartificial_location (exp);
12446 switch (fcode)
12448 case BUILT_IN_STRCPY_CHK:
12449 case BUILT_IN_STPCPY_CHK:
12450 /* For __strcat_chk the warning will be emitted only if overflowing
12451 by at least strlen (dest) + 1 bytes. */
12452 case BUILT_IN_STRCAT_CHK:
12453 len = CALL_EXPR_ARG (exp, 1);
12454 size = CALL_EXPR_ARG (exp, 2);
12455 is_strlen = 1;
12456 break;
12457 case BUILT_IN_STRNCAT_CHK:
12458 case BUILT_IN_STRNCPY_CHK:
12459 case BUILT_IN_STPNCPY_CHK:
12460 len = CALL_EXPR_ARG (exp, 2);
12461 size = CALL_EXPR_ARG (exp, 3);
12462 break;
12463 case BUILT_IN_SNPRINTF_CHK:
12464 case BUILT_IN_VSNPRINTF_CHK:
12465 len = CALL_EXPR_ARG (exp, 1);
12466 size = CALL_EXPR_ARG (exp, 3);
12467 break;
12468 default:
12469 gcc_unreachable ();
12472 if (!len || !size)
12473 return;
12475 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12476 return;
12478 if (is_strlen)
12480 len = c_strlen (len, 1);
12481 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12482 return;
12484 else if (fcode == BUILT_IN_STRNCAT_CHK)
12486 tree src = CALL_EXPR_ARG (exp, 1);
12487 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12488 return;
12489 src = c_strlen (src, 1);
12490 if (! src || ! tree_fits_uhwi_p (src))
12492 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12493 exp, get_callee_fndecl (exp));
12494 return;
12496 else if (tree_int_cst_lt (src, size))
12497 return;
12499 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
12500 return;
12502 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12503 exp, get_callee_fndecl (exp));
12506 /* Emit warning if a buffer overflow is detected at compile time
12507 in __sprintf_chk/__vsprintf_chk calls. */
12509 static void
12510 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12512 tree size, len, fmt;
12513 const char *fmt_str;
12514 int nargs = call_expr_nargs (exp);
12516 /* Verify the required arguments in the original call. */
12518 if (nargs < 4)
12519 return;
12520 size = CALL_EXPR_ARG (exp, 2);
12521 fmt = CALL_EXPR_ARG (exp, 3);
12523 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12524 return;
12526 /* Check whether the format is a literal string constant. */
12527 fmt_str = c_getstr (fmt);
12528 if (fmt_str == NULL)
12529 return;
12531 if (!init_target_chars ())
12532 return;
12534 /* If the format doesn't contain % args or %%, we know its size. */
12535 if (strchr (fmt_str, target_percent) == 0)
12536 len = build_int_cstu (size_type_node, strlen (fmt_str));
12537 /* If the format is "%s" and first ... argument is a string literal,
12538 we know it too. */
12539 else if (fcode == BUILT_IN_SPRINTF_CHK
12540 && strcmp (fmt_str, target_percent_s) == 0)
12542 tree arg;
12544 if (nargs < 5)
12545 return;
12546 arg = CALL_EXPR_ARG (exp, 4);
12547 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12548 return;
12550 len = c_strlen (arg, 1);
12551 if (!len || ! tree_fits_uhwi_p (len))
12552 return;
12554 else
12555 return;
12557 if (! tree_int_cst_lt (len, size))
12558 warning_at (tree_nonartificial_location (exp),
12559 0, "%Kcall to %D will always overflow destination buffer",
12560 exp, get_callee_fndecl (exp));
12563 /* Emit warning if a free is called with address of a variable. */
12565 static void
12566 maybe_emit_free_warning (tree exp)
12568 tree arg = CALL_EXPR_ARG (exp, 0);
12570 STRIP_NOPS (arg);
12571 if (TREE_CODE (arg) != ADDR_EXPR)
12572 return;
12574 arg = get_base_address (TREE_OPERAND (arg, 0));
12575 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12576 return;
12578 if (SSA_VAR_P (arg))
12579 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12580 "%Kattempt to free a non-heap object %qD", exp, arg);
12581 else
12582 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12583 "%Kattempt to free a non-heap object", exp);
12586 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12587 if possible. */
12589 tree
12590 fold_builtin_object_size (tree ptr, tree ost)
12592 unsigned HOST_WIDE_INT bytes;
12593 int object_size_type;
12595 if (!validate_arg (ptr, POINTER_TYPE)
12596 || !validate_arg (ost, INTEGER_TYPE))
12597 return NULL_TREE;
12599 STRIP_NOPS (ost);
12601 if (TREE_CODE (ost) != INTEGER_CST
12602 || tree_int_cst_sgn (ost) < 0
12603 || compare_tree_int (ost, 3) > 0)
12604 return NULL_TREE;
12606 object_size_type = tree_to_shwi (ost);
12608 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12609 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12610 and (size_t) 0 for types 2 and 3. */
12611 if (TREE_SIDE_EFFECTS (ptr))
12612 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12614 if (TREE_CODE (ptr) == ADDR_EXPR)
12616 bytes = compute_builtin_object_size (ptr, object_size_type);
12617 if (wi::fits_to_tree_p (bytes, size_type_node))
12618 return build_int_cstu (size_type_node, bytes);
12620 else if (TREE_CODE (ptr) == SSA_NAME)
12622 /* If object size is not known yet, delay folding until
12623 later. Maybe subsequent passes will help determining
12624 it. */
12625 bytes = compute_builtin_object_size (ptr, object_size_type);
12626 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12627 && wi::fits_to_tree_p (bytes, size_type_node))
12628 return build_int_cstu (size_type_node, bytes);
12631 return NULL_TREE;
12634 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12635 DEST, SRC, LEN, and SIZE are the arguments to the call.
12636 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12637 code of the builtin. If MAXLEN is not NULL, it is maximum length
12638 passed as third argument. */
12640 tree
12641 fold_builtin_memory_chk (location_t loc, tree fndecl,
12642 tree dest, tree src, tree len, tree size,
12643 tree maxlen, bool ignore,
12644 enum built_in_function fcode)
12646 tree fn;
12648 if (!validate_arg (dest, POINTER_TYPE)
12649 || !validate_arg (src,
12650 (fcode == BUILT_IN_MEMSET_CHK
12651 ? INTEGER_TYPE : POINTER_TYPE))
12652 || !validate_arg (len, INTEGER_TYPE)
12653 || !validate_arg (size, INTEGER_TYPE))
12654 return NULL_TREE;
12656 /* If SRC and DEST are the same (and not volatile), return DEST
12657 (resp. DEST+LEN for __mempcpy_chk). */
12658 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12660 if (fcode != BUILT_IN_MEMPCPY_CHK)
12661 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12662 dest, len);
12663 else
12665 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12666 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12670 if (! tree_fits_uhwi_p (size))
12671 return NULL_TREE;
12673 if (! integer_all_onesp (size))
12675 if (! tree_fits_uhwi_p (len))
12677 /* If LEN is not constant, try MAXLEN too.
12678 For MAXLEN only allow optimizing into non-_ocs function
12679 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12680 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12682 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12684 /* (void) __mempcpy_chk () can be optimized into
12685 (void) __memcpy_chk (). */
12686 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12687 if (!fn)
12688 return NULL_TREE;
12690 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12692 return NULL_TREE;
12695 else
12696 maxlen = len;
12698 if (tree_int_cst_lt (size, maxlen))
12699 return NULL_TREE;
12702 fn = NULL_TREE;
12703 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12704 mem{cpy,pcpy,move,set} is available. */
12705 switch (fcode)
12707 case BUILT_IN_MEMCPY_CHK:
12708 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12709 break;
12710 case BUILT_IN_MEMPCPY_CHK:
12711 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12712 break;
12713 case BUILT_IN_MEMMOVE_CHK:
12714 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12715 break;
12716 case BUILT_IN_MEMSET_CHK:
12717 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12718 break;
12719 default:
12720 break;
12723 if (!fn)
12724 return NULL_TREE;
12726 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12729 /* Fold a call to the __st[rp]cpy_chk builtin.
12730 DEST, SRC, and SIZE are the arguments to the call.
12731 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12732 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12733 strings passed as second argument. */
12735 tree
12736 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12737 tree src, tree size,
12738 tree maxlen, bool ignore,
12739 enum built_in_function fcode)
12741 tree len, fn;
12743 if (!validate_arg (dest, POINTER_TYPE)
12744 || !validate_arg (src, POINTER_TYPE)
12745 || !validate_arg (size, INTEGER_TYPE))
12746 return NULL_TREE;
12748 /* If SRC and DEST are the same (and not volatile), return DEST. */
12749 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12750 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12752 if (! tree_fits_uhwi_p (size))
12753 return NULL_TREE;
12755 if (! integer_all_onesp (size))
12757 len = c_strlen (src, 1);
12758 if (! len || ! tree_fits_uhwi_p (len))
12760 /* If LEN is not constant, try MAXLEN too.
12761 For MAXLEN only allow optimizing into non-_ocs function
12762 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12763 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12765 if (fcode == BUILT_IN_STPCPY_CHK)
12767 if (! ignore)
12768 return NULL_TREE;
12770 /* If return value of __stpcpy_chk is ignored,
12771 optimize into __strcpy_chk. */
12772 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12773 if (!fn)
12774 return NULL_TREE;
12776 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12779 if (! len || TREE_SIDE_EFFECTS (len))
12780 return NULL_TREE;
12782 /* If c_strlen returned something, but not a constant,
12783 transform __strcpy_chk into __memcpy_chk. */
12784 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12785 if (!fn)
12786 return NULL_TREE;
12788 len = fold_convert_loc (loc, size_type_node, len);
12789 len = size_binop_loc (loc, PLUS_EXPR, len,
12790 build_int_cst (size_type_node, 1));
12791 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12792 build_call_expr_loc (loc, fn, 4,
12793 dest, src, len, size));
12796 else
12797 maxlen = len;
12799 if (! tree_int_cst_lt (maxlen, size))
12800 return NULL_TREE;
12803 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12804 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12805 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12806 if (!fn)
12807 return NULL_TREE;
12809 return build_call_expr_loc (loc, fn, 2, dest, src);
12812 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12813 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12814 length passed as third argument. IGNORE is true if return value can be
12815 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12817 tree
12818 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12819 tree len, tree size, tree maxlen, bool ignore,
12820 enum built_in_function fcode)
12822 tree fn;
12824 if (!validate_arg (dest, POINTER_TYPE)
12825 || !validate_arg (src, POINTER_TYPE)
12826 || !validate_arg (len, INTEGER_TYPE)
12827 || !validate_arg (size, INTEGER_TYPE))
12828 return NULL_TREE;
12830 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12832 /* If return value of __stpncpy_chk is ignored,
12833 optimize into __strncpy_chk. */
12834 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12835 if (fn)
12836 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12839 if (! tree_fits_uhwi_p (size))
12840 return NULL_TREE;
12842 if (! integer_all_onesp (size))
12844 if (! tree_fits_uhwi_p (len))
12846 /* If LEN is not constant, try MAXLEN too.
12847 For MAXLEN only allow optimizing into non-_ocs function
12848 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12849 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12850 return NULL_TREE;
12852 else
12853 maxlen = len;
12855 if (tree_int_cst_lt (size, maxlen))
12856 return NULL_TREE;
12859 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12860 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12861 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12862 if (!fn)
12863 return NULL_TREE;
12865 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12868 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12869 are the arguments to the call. */
12871 static tree
12872 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12873 tree src, tree size)
12875 tree fn;
12876 const char *p;
12878 if (!validate_arg (dest, POINTER_TYPE)
12879 || !validate_arg (src, POINTER_TYPE)
12880 || !validate_arg (size, INTEGER_TYPE))
12881 return NULL_TREE;
12883 p = c_getstr (src);
12884 /* If the SRC parameter is "", return DEST. */
12885 if (p && *p == '\0')
12886 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12888 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
12889 return NULL_TREE;
12891 /* If __builtin_strcat_chk is used, assume strcat is available. */
12892 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12893 if (!fn)
12894 return NULL_TREE;
12896 return build_call_expr_loc (loc, fn, 2, dest, src);
12899 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12900 LEN, and SIZE. */
12902 static tree
12903 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12904 tree dest, tree src, tree len, tree size)
12906 tree fn;
12907 const char *p;
12909 if (!validate_arg (dest, POINTER_TYPE)
12910 || !validate_arg (src, POINTER_TYPE)
12911 || !validate_arg (size, INTEGER_TYPE)
12912 || !validate_arg (size, INTEGER_TYPE))
12913 return NULL_TREE;
12915 p = c_getstr (src);
12916 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12917 if (p && *p == '\0')
12918 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12919 else if (integer_zerop (len))
12920 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12922 if (! tree_fits_uhwi_p (size))
12923 return NULL_TREE;
12925 if (! integer_all_onesp (size))
12927 tree src_len = c_strlen (src, 1);
12928 if (src_len
12929 && tree_fits_uhwi_p (src_len)
12930 && tree_fits_uhwi_p (len)
12931 && ! tree_int_cst_lt (len, src_len))
12933 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12934 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
12935 if (!fn)
12936 return NULL_TREE;
12938 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12940 return NULL_TREE;
12943 /* If __builtin_strncat_chk is used, assume strncat is available. */
12944 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
12945 if (!fn)
12946 return NULL_TREE;
12948 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12951 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12952 Return NULL_TREE if a normal call should be emitted rather than
12953 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
12954 or BUILT_IN_VSPRINTF_CHK. */
12956 static tree
12957 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
12958 enum built_in_function fcode)
12960 tree dest, size, len, fn, fmt, flag;
12961 const char *fmt_str;
12963 /* Verify the required arguments in the original call. */
12964 if (nargs < 4)
12965 return NULL_TREE;
12966 dest = args[0];
12967 if (!validate_arg (dest, POINTER_TYPE))
12968 return NULL_TREE;
12969 flag = args[1];
12970 if (!validate_arg (flag, INTEGER_TYPE))
12971 return NULL_TREE;
12972 size = args[2];
12973 if (!validate_arg (size, INTEGER_TYPE))
12974 return NULL_TREE;
12975 fmt = args[3];
12976 if (!validate_arg (fmt, POINTER_TYPE))
12977 return NULL_TREE;
12979 if (! tree_fits_uhwi_p (size))
12980 return NULL_TREE;
12982 len = NULL_TREE;
12984 if (!init_target_chars ())
12985 return NULL_TREE;
12987 /* Check whether the format is a literal string constant. */
12988 fmt_str = c_getstr (fmt);
12989 if (fmt_str != NULL)
12991 /* If the format doesn't contain % args or %%, we know the size. */
12992 if (strchr (fmt_str, target_percent) == 0)
12994 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12995 len = build_int_cstu (size_type_node, strlen (fmt_str));
12997 /* If the format is "%s" and first ... argument is a string literal,
12998 we know the size too. */
12999 else if (fcode == BUILT_IN_SPRINTF_CHK
13000 && strcmp (fmt_str, target_percent_s) == 0)
13002 tree arg;
13004 if (nargs == 5)
13006 arg = args[4];
13007 if (validate_arg (arg, POINTER_TYPE))
13009 len = c_strlen (arg, 1);
13010 if (! len || ! tree_fits_uhwi_p (len))
13011 len = NULL_TREE;
13017 if (! integer_all_onesp (size))
13019 if (! len || ! tree_int_cst_lt (len, size))
13020 return NULL_TREE;
13023 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13024 or if format doesn't contain % chars or is "%s". */
13025 if (! integer_zerop (flag))
13027 if (fmt_str == NULL)
13028 return NULL_TREE;
13029 if (strchr (fmt_str, target_percent) != NULL
13030 && strcmp (fmt_str, target_percent_s))
13031 return NULL_TREE;
13034 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13035 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13036 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13037 if (!fn)
13038 return NULL_TREE;
13040 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13043 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13044 a normal call should be emitted rather than expanding the function
13045 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13047 static tree
13048 fold_builtin_sprintf_chk (location_t loc, tree exp,
13049 enum built_in_function fcode)
13051 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13052 CALL_EXPR_ARGP (exp), fcode);
13055 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13056 NULL_TREE if a normal call should be emitted rather than expanding
13057 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13058 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13059 passed as second argument. */
13061 static tree
13062 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13063 tree maxlen, enum built_in_function fcode)
13065 tree dest, size, len, fn, fmt, flag;
13066 const char *fmt_str;
13068 /* Verify the required arguments in the original call. */
13069 if (nargs < 5)
13070 return NULL_TREE;
13071 dest = args[0];
13072 if (!validate_arg (dest, POINTER_TYPE))
13073 return NULL_TREE;
13074 len = args[1];
13075 if (!validate_arg (len, INTEGER_TYPE))
13076 return NULL_TREE;
13077 flag = args[2];
13078 if (!validate_arg (flag, INTEGER_TYPE))
13079 return NULL_TREE;
13080 size = args[3];
13081 if (!validate_arg (size, INTEGER_TYPE))
13082 return NULL_TREE;
13083 fmt = args[4];
13084 if (!validate_arg (fmt, POINTER_TYPE))
13085 return NULL_TREE;
13087 if (! tree_fits_uhwi_p (size))
13088 return NULL_TREE;
13090 if (! integer_all_onesp (size))
13092 if (! tree_fits_uhwi_p (len))
13094 /* If LEN is not constant, try MAXLEN too.
13095 For MAXLEN only allow optimizing into non-_ocs function
13096 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13097 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
13098 return NULL_TREE;
13100 else
13101 maxlen = len;
13103 if (tree_int_cst_lt (size, maxlen))
13104 return NULL_TREE;
13107 if (!init_target_chars ())
13108 return NULL_TREE;
13110 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13111 or if format doesn't contain % chars or is "%s". */
13112 if (! integer_zerop (flag))
13114 fmt_str = c_getstr (fmt);
13115 if (fmt_str == NULL)
13116 return NULL_TREE;
13117 if (strchr (fmt_str, target_percent) != NULL
13118 && strcmp (fmt_str, target_percent_s))
13119 return NULL_TREE;
13122 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13123 available. */
13124 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13125 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13126 if (!fn)
13127 return NULL_TREE;
13129 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13132 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13133 a normal call should be emitted rather than expanding the function
13134 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13135 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13136 passed as second argument. */
13138 static tree
13139 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13140 enum built_in_function fcode)
13142 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13143 CALL_EXPR_ARGP (exp), maxlen, fcode);
13146 /* Builtins with folding operations that operate on "..." arguments
13147 need special handling; we need to store the arguments in a convenient
13148 data structure before attempting any folding. Fortunately there are
13149 only a few builtins that fall into this category. FNDECL is the
13150 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13151 result of the function call is ignored. */
13153 static tree
13154 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
13155 bool ignore ATTRIBUTE_UNUSED)
13157 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13158 tree ret = NULL_TREE;
13160 switch (fcode)
13162 case BUILT_IN_SPRINTF_CHK:
13163 case BUILT_IN_VSPRINTF_CHK:
13164 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
13165 break;
13167 case BUILT_IN_SNPRINTF_CHK:
13168 case BUILT_IN_VSNPRINTF_CHK:
13169 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
13170 break;
13172 case BUILT_IN_FPCLASSIFY:
13173 ret = fold_builtin_fpclassify (loc, exp);
13174 break;
13176 default:
13177 break;
13179 if (ret)
13181 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13182 SET_EXPR_LOCATION (ret, loc);
13183 TREE_NO_WARNING (ret) = 1;
13184 return ret;
13186 return NULL_TREE;
13189 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13190 FMT and ARG are the arguments to the call; we don't fold cases with
13191 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13193 Return NULL_TREE if no simplification was possible, otherwise return the
13194 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13195 code of the function to be simplified. */
13197 static tree
13198 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13199 tree arg, bool ignore,
13200 enum built_in_function fcode)
13202 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13203 const char *fmt_str = NULL;
13205 /* If the return value is used, don't do the transformation. */
13206 if (! ignore)
13207 return NULL_TREE;
13209 /* Verify the required arguments in the original call. */
13210 if (!validate_arg (fmt, POINTER_TYPE))
13211 return NULL_TREE;
13213 /* Check whether the format is a literal string constant. */
13214 fmt_str = c_getstr (fmt);
13215 if (fmt_str == NULL)
13216 return NULL_TREE;
13218 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13220 /* If we're using an unlocked function, assume the other
13221 unlocked functions exist explicitly. */
13222 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13223 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13225 else
13227 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13228 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13231 if (!init_target_chars ())
13232 return NULL_TREE;
13234 if (strcmp (fmt_str, target_percent_s) == 0
13235 || strchr (fmt_str, target_percent) == NULL)
13237 const char *str;
13239 if (strcmp (fmt_str, target_percent_s) == 0)
13241 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13242 return NULL_TREE;
13244 if (!arg || !validate_arg (arg, POINTER_TYPE))
13245 return NULL_TREE;
13247 str = c_getstr (arg);
13248 if (str == NULL)
13249 return NULL_TREE;
13251 else
13253 /* The format specifier doesn't contain any '%' characters. */
13254 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13255 && arg)
13256 return NULL_TREE;
13257 str = fmt_str;
13260 /* If the string was "", printf does nothing. */
13261 if (str[0] == '\0')
13262 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13264 /* If the string has length of 1, call putchar. */
13265 if (str[1] == '\0')
13267 /* Given printf("c"), (where c is any one character,)
13268 convert "c"[0] to an int and pass that to the replacement
13269 function. */
13270 newarg = build_int_cst (integer_type_node, str[0]);
13271 if (fn_putchar)
13272 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13274 else
13276 /* If the string was "string\n", call puts("string"). */
13277 size_t len = strlen (str);
13278 if ((unsigned char)str[len - 1] == target_newline
13279 && (size_t) (int) len == len
13280 && (int) len > 0)
13282 char *newstr;
13283 tree offset_node, string_cst;
13285 /* Create a NUL-terminated string that's one char shorter
13286 than the original, stripping off the trailing '\n'. */
13287 newarg = build_string_literal (len, str);
13288 string_cst = string_constant (newarg, &offset_node);
13289 gcc_checking_assert (string_cst
13290 && (TREE_STRING_LENGTH (string_cst)
13291 == (int) len)
13292 && integer_zerop (offset_node)
13293 && (unsigned char)
13294 TREE_STRING_POINTER (string_cst)[len - 1]
13295 == target_newline);
13296 /* build_string_literal creates a new STRING_CST,
13297 modify it in place to avoid double copying. */
13298 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13299 newstr[len - 1] = '\0';
13300 if (fn_puts)
13301 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13303 else
13304 /* We'd like to arrange to call fputs(string,stdout) here,
13305 but we need stdout and don't have a way to get it yet. */
13306 return NULL_TREE;
13310 /* The other optimizations can be done only on the non-va_list variants. */
13311 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13312 return NULL_TREE;
13314 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13315 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13317 if (!arg || !validate_arg (arg, POINTER_TYPE))
13318 return NULL_TREE;
13319 if (fn_puts)
13320 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13323 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13324 else if (strcmp (fmt_str, target_percent_c) == 0)
13326 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13327 return NULL_TREE;
13328 if (fn_putchar)
13329 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13332 if (!call)
13333 return NULL_TREE;
13335 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13338 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13339 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13340 more than 3 arguments, and ARG may be null in the 2-argument case.
13342 Return NULL_TREE if no simplification was possible, otherwise return the
13343 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13344 code of the function to be simplified. */
13346 static tree
13347 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13348 tree fmt, tree arg, bool ignore,
13349 enum built_in_function fcode)
13351 tree fn_fputc, fn_fputs, call = NULL_TREE;
13352 const char *fmt_str = NULL;
13354 /* If the return value is used, don't do the transformation. */
13355 if (! ignore)
13356 return NULL_TREE;
13358 /* Verify the required arguments in the original call. */
13359 if (!validate_arg (fp, POINTER_TYPE))
13360 return NULL_TREE;
13361 if (!validate_arg (fmt, POINTER_TYPE))
13362 return NULL_TREE;
13364 /* Check whether the format is a literal string constant. */
13365 fmt_str = c_getstr (fmt);
13366 if (fmt_str == NULL)
13367 return NULL_TREE;
13369 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13371 /* If we're using an unlocked function, assume the other
13372 unlocked functions exist explicitly. */
13373 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13374 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13376 else
13378 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13379 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13382 if (!init_target_chars ())
13383 return NULL_TREE;
13385 /* If the format doesn't contain % args or %%, use strcpy. */
13386 if (strchr (fmt_str, target_percent) == NULL)
13388 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13389 && arg)
13390 return NULL_TREE;
13392 /* If the format specifier was "", fprintf does nothing. */
13393 if (fmt_str[0] == '\0')
13395 /* If FP has side-effects, just wait until gimplification is
13396 done. */
13397 if (TREE_SIDE_EFFECTS (fp))
13398 return NULL_TREE;
13400 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13403 /* When "string" doesn't contain %, replace all cases of
13404 fprintf (fp, string) with fputs (string, fp). The fputs
13405 builtin will take care of special cases like length == 1. */
13406 if (fn_fputs)
13407 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13410 /* The other optimizations can be done only on the non-va_list variants. */
13411 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13412 return NULL_TREE;
13414 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13415 else if (strcmp (fmt_str, target_percent_s) == 0)
13417 if (!arg || !validate_arg (arg, POINTER_TYPE))
13418 return NULL_TREE;
13419 if (fn_fputs)
13420 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13423 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13424 else if (strcmp (fmt_str, target_percent_c) == 0)
13426 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13427 return NULL_TREE;
13428 if (fn_fputc)
13429 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13432 if (!call)
13433 return NULL_TREE;
13434 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13437 /* Initialize format string characters in the target charset. */
13439 static bool
13440 init_target_chars (void)
13442 static bool init;
13443 if (!init)
13445 target_newline = lang_hooks.to_target_charset ('\n');
13446 target_percent = lang_hooks.to_target_charset ('%');
13447 target_c = lang_hooks.to_target_charset ('c');
13448 target_s = lang_hooks.to_target_charset ('s');
13449 if (target_newline == 0 || target_percent == 0 || target_c == 0
13450 || target_s == 0)
13451 return false;
13453 target_percent_c[0] = target_percent;
13454 target_percent_c[1] = target_c;
13455 target_percent_c[2] = '\0';
13457 target_percent_s[0] = target_percent;
13458 target_percent_s[1] = target_s;
13459 target_percent_s[2] = '\0';
13461 target_percent_s_newline[0] = target_percent;
13462 target_percent_s_newline[1] = target_s;
13463 target_percent_s_newline[2] = target_newline;
13464 target_percent_s_newline[3] = '\0';
13466 init = true;
13468 return true;
13471 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13472 and no overflow/underflow occurred. INEXACT is true if M was not
13473 exactly calculated. TYPE is the tree type for the result. This
13474 function assumes that you cleared the MPFR flags and then
13475 calculated M to see if anything subsequently set a flag prior to
13476 entering this function. Return NULL_TREE if any checks fail. */
13478 static tree
13479 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13481 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13482 overflow/underflow occurred. If -frounding-math, proceed iff the
13483 result of calling FUNC was exact. */
13484 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13485 && (!flag_rounding_math || !inexact))
13487 REAL_VALUE_TYPE rr;
13489 real_from_mpfr (&rr, m, type, GMP_RNDN);
13490 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13491 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13492 but the mpft_t is not, then we underflowed in the
13493 conversion. */
13494 if (real_isfinite (&rr)
13495 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13497 REAL_VALUE_TYPE rmode;
13499 real_convert (&rmode, TYPE_MODE (type), &rr);
13500 /* Proceed iff the specified mode can hold the value. */
13501 if (real_identical (&rmode, &rr))
13502 return build_real (type, rmode);
13505 return NULL_TREE;
13508 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13509 number and no overflow/underflow occurred. INEXACT is true if M
13510 was not exactly calculated. TYPE is the tree type for the result.
13511 This function assumes that you cleared the MPFR flags and then
13512 calculated M to see if anything subsequently set a flag prior to
13513 entering this function. Return NULL_TREE if any checks fail, if
13514 FORCE_CONVERT is true, then bypass the checks. */
13516 static tree
13517 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13519 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13520 overflow/underflow occurred. If -frounding-math, proceed iff the
13521 result of calling FUNC was exact. */
13522 if (force_convert
13523 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13524 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13525 && (!flag_rounding_math || !inexact)))
13527 REAL_VALUE_TYPE re, im;
13529 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13530 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13531 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13532 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13533 but the mpft_t is not, then we underflowed in the
13534 conversion. */
13535 if (force_convert
13536 || (real_isfinite (&re) && real_isfinite (&im)
13537 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13538 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13540 REAL_VALUE_TYPE re_mode, im_mode;
13542 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13543 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13544 /* Proceed iff the specified mode can hold the value. */
13545 if (force_convert
13546 || (real_identical (&re_mode, &re)
13547 && real_identical (&im_mode, &im)))
13548 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13549 build_real (TREE_TYPE (type), im_mode));
13552 return NULL_TREE;
13555 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13556 FUNC on it and return the resulting value as a tree with type TYPE.
13557 If MIN and/or MAX are not NULL, then the supplied ARG must be
13558 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13559 acceptable values, otherwise they are not. The mpfr precision is
13560 set to the precision of TYPE. We assume that function FUNC returns
13561 zero if the result could be calculated exactly within the requested
13562 precision. */
13564 static tree
13565 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13566 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13567 bool inclusive)
13569 tree result = NULL_TREE;
13571 STRIP_NOPS (arg);
13573 /* To proceed, MPFR must exactly represent the target floating point
13574 format, which only happens when the target base equals two. */
13575 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13576 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13578 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13580 if (real_isfinite (ra)
13581 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13582 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13584 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13585 const int prec = fmt->p;
13586 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13587 int inexact;
13588 mpfr_t m;
13590 mpfr_init2 (m, prec);
13591 mpfr_from_real (m, ra, GMP_RNDN);
13592 mpfr_clear_flags ();
13593 inexact = func (m, m, rnd);
13594 result = do_mpfr_ckconv (m, type, inexact);
13595 mpfr_clear (m);
13599 return result;
13602 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13603 FUNC on it and return the resulting value as a tree with type TYPE.
13604 The mpfr precision is set to the precision of TYPE. We assume that
13605 function FUNC returns zero if the result could be calculated
13606 exactly within the requested precision. */
13608 static tree
13609 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13610 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13612 tree result = NULL_TREE;
13614 STRIP_NOPS (arg1);
13615 STRIP_NOPS (arg2);
13617 /* To proceed, MPFR must exactly represent the target floating point
13618 format, which only happens when the target base equals two. */
13619 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13620 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13621 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13623 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13624 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13626 if (real_isfinite (ra1) && real_isfinite (ra2))
13628 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13629 const int prec = fmt->p;
13630 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13631 int inexact;
13632 mpfr_t m1, m2;
13634 mpfr_inits2 (prec, m1, m2, NULL);
13635 mpfr_from_real (m1, ra1, GMP_RNDN);
13636 mpfr_from_real (m2, ra2, GMP_RNDN);
13637 mpfr_clear_flags ();
13638 inexact = func (m1, m1, m2, rnd);
13639 result = do_mpfr_ckconv (m1, type, inexact);
13640 mpfr_clears (m1, m2, NULL);
13644 return result;
13647 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13648 FUNC on it and return the resulting value as a tree with type TYPE.
13649 The mpfr precision is set to the precision of TYPE. We assume that
13650 function FUNC returns zero if the result could be calculated
13651 exactly within the requested precision. */
13653 static tree
13654 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13655 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13657 tree result = NULL_TREE;
13659 STRIP_NOPS (arg1);
13660 STRIP_NOPS (arg2);
13661 STRIP_NOPS (arg3);
13663 /* To proceed, MPFR must exactly represent the target floating point
13664 format, which only happens when the target base equals two. */
13665 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13666 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13667 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13668 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13670 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13671 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13672 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13674 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13676 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13677 const int prec = fmt->p;
13678 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13679 int inexact;
13680 mpfr_t m1, m2, m3;
13682 mpfr_inits2 (prec, m1, m2, m3, NULL);
13683 mpfr_from_real (m1, ra1, GMP_RNDN);
13684 mpfr_from_real (m2, ra2, GMP_RNDN);
13685 mpfr_from_real (m3, ra3, GMP_RNDN);
13686 mpfr_clear_flags ();
13687 inexact = func (m1, m1, m2, m3, rnd);
13688 result = do_mpfr_ckconv (m1, type, inexact);
13689 mpfr_clears (m1, m2, m3, NULL);
13693 return result;
13696 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13697 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13698 If ARG_SINP and ARG_COSP are NULL then the result is returned
13699 as a complex value.
13700 The type is taken from the type of ARG and is used for setting the
13701 precision of the calculation and results. */
13703 static tree
13704 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13706 tree const type = TREE_TYPE (arg);
13707 tree result = NULL_TREE;
13709 STRIP_NOPS (arg);
13711 /* To proceed, MPFR must exactly represent the target floating point
13712 format, which only happens when the target base equals two. */
13713 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13714 && TREE_CODE (arg) == REAL_CST
13715 && !TREE_OVERFLOW (arg))
13717 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13719 if (real_isfinite (ra))
13721 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13722 const int prec = fmt->p;
13723 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13724 tree result_s, result_c;
13725 int inexact;
13726 mpfr_t m, ms, mc;
13728 mpfr_inits2 (prec, m, ms, mc, NULL);
13729 mpfr_from_real (m, ra, GMP_RNDN);
13730 mpfr_clear_flags ();
13731 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13732 result_s = do_mpfr_ckconv (ms, type, inexact);
13733 result_c = do_mpfr_ckconv (mc, type, inexact);
13734 mpfr_clears (m, ms, mc, NULL);
13735 if (result_s && result_c)
13737 /* If we are to return in a complex value do so. */
13738 if (!arg_sinp && !arg_cosp)
13739 return build_complex (build_complex_type (type),
13740 result_c, result_s);
13742 /* Dereference the sin/cos pointer arguments. */
13743 arg_sinp = build_fold_indirect_ref (arg_sinp);
13744 arg_cosp = build_fold_indirect_ref (arg_cosp);
13745 /* Proceed if valid pointer type were passed in. */
13746 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13747 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13749 /* Set the values. */
13750 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13751 result_s);
13752 TREE_SIDE_EFFECTS (result_s) = 1;
13753 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13754 result_c);
13755 TREE_SIDE_EFFECTS (result_c) = 1;
13756 /* Combine the assignments into a compound expr. */
13757 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13758 result_s, result_c));
13763 return result;
13766 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13767 two-argument mpfr order N Bessel function FUNC on them and return
13768 the resulting value as a tree with type TYPE. The mpfr precision
13769 is set to the precision of TYPE. We assume that function FUNC
13770 returns zero if the result could be calculated exactly within the
13771 requested precision. */
13772 static tree
13773 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13774 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13775 const REAL_VALUE_TYPE *min, bool inclusive)
13777 tree result = NULL_TREE;
13779 STRIP_NOPS (arg1);
13780 STRIP_NOPS (arg2);
13782 /* To proceed, MPFR must exactly represent the target floating point
13783 format, which only happens when the target base equals two. */
13784 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13785 && tree_fits_shwi_p (arg1)
13786 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13788 const HOST_WIDE_INT n = tree_to_shwi (arg1);
13789 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13791 if (n == (long)n
13792 && real_isfinite (ra)
13793 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13795 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13796 const int prec = fmt->p;
13797 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13798 int inexact;
13799 mpfr_t m;
13801 mpfr_init2 (m, prec);
13802 mpfr_from_real (m, ra, GMP_RNDN);
13803 mpfr_clear_flags ();
13804 inexact = func (m, n, m, rnd);
13805 result = do_mpfr_ckconv (m, type, inexact);
13806 mpfr_clear (m);
13810 return result;
13813 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13814 the pointer *(ARG_QUO) and return the result. The type is taken
13815 from the type of ARG0 and is used for setting the precision of the
13816 calculation and results. */
13818 static tree
13819 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13821 tree const type = TREE_TYPE (arg0);
13822 tree result = NULL_TREE;
13824 STRIP_NOPS (arg0);
13825 STRIP_NOPS (arg1);
13827 /* To proceed, MPFR must exactly represent the target floating point
13828 format, which only happens when the target base equals two. */
13829 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13830 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13831 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13833 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13834 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13836 if (real_isfinite (ra0) && real_isfinite (ra1))
13838 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13839 const int prec = fmt->p;
13840 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13841 tree result_rem;
13842 long integer_quo;
13843 mpfr_t m0, m1;
13845 mpfr_inits2 (prec, m0, m1, NULL);
13846 mpfr_from_real (m0, ra0, GMP_RNDN);
13847 mpfr_from_real (m1, ra1, GMP_RNDN);
13848 mpfr_clear_flags ();
13849 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13850 /* Remquo is independent of the rounding mode, so pass
13851 inexact=0 to do_mpfr_ckconv(). */
13852 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13853 mpfr_clears (m0, m1, NULL);
13854 if (result_rem)
13856 /* MPFR calculates quo in the host's long so it may
13857 return more bits in quo than the target int can hold
13858 if sizeof(host long) > sizeof(target int). This can
13859 happen even for native compilers in LP64 mode. In
13860 these cases, modulo the quo value with the largest
13861 number that the target int can hold while leaving one
13862 bit for the sign. */
13863 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13864 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13866 /* Dereference the quo pointer argument. */
13867 arg_quo = build_fold_indirect_ref (arg_quo);
13868 /* Proceed iff a valid pointer type was passed in. */
13869 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13871 /* Set the value. */
13872 tree result_quo
13873 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13874 build_int_cst (TREE_TYPE (arg_quo),
13875 integer_quo));
13876 TREE_SIDE_EFFECTS (result_quo) = 1;
13877 /* Combine the quo assignment with the rem. */
13878 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13879 result_quo, result_rem));
13884 return result;
13887 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13888 resulting value as a tree with type TYPE. The mpfr precision is
13889 set to the precision of TYPE. We assume that this mpfr function
13890 returns zero if the result could be calculated exactly within the
13891 requested precision. In addition, the integer pointer represented
13892 by ARG_SG will be dereferenced and set to the appropriate signgam
13893 (-1,1) value. */
13895 static tree
13896 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13898 tree result = NULL_TREE;
13900 STRIP_NOPS (arg);
13902 /* To proceed, MPFR must exactly represent the target floating point
13903 format, which only happens when the target base equals two. Also
13904 verify ARG is a constant and that ARG_SG is an int pointer. */
13905 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13906 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13907 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13908 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13910 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13912 /* In addition to NaN and Inf, the argument cannot be zero or a
13913 negative integer. */
13914 if (real_isfinite (ra)
13915 && ra->cl != rvc_zero
13916 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
13918 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13919 const int prec = fmt->p;
13920 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13921 int inexact, sg;
13922 mpfr_t m;
13923 tree result_lg;
13925 mpfr_init2 (m, prec);
13926 mpfr_from_real (m, ra, GMP_RNDN);
13927 mpfr_clear_flags ();
13928 inexact = mpfr_lgamma (m, &sg, m, rnd);
13929 result_lg = do_mpfr_ckconv (m, type, inexact);
13930 mpfr_clear (m);
13931 if (result_lg)
13933 tree result_sg;
13935 /* Dereference the arg_sg pointer argument. */
13936 arg_sg = build_fold_indirect_ref (arg_sg);
13937 /* Assign the signgam value into *arg_sg. */
13938 result_sg = fold_build2 (MODIFY_EXPR,
13939 TREE_TYPE (arg_sg), arg_sg,
13940 build_int_cst (TREE_TYPE (arg_sg), sg));
13941 TREE_SIDE_EFFECTS (result_sg) = 1;
13942 /* Combine the signgam assignment with the lgamma result. */
13943 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13944 result_sg, result_lg));
13949 return result;
13952 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13953 function FUNC on it and return the resulting value as a tree with
13954 type TYPE. The mpfr precision is set to the precision of TYPE. We
13955 assume that function FUNC returns zero if the result could be
13956 calculated exactly within the requested precision. */
13958 static tree
13959 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13961 tree result = NULL_TREE;
13963 STRIP_NOPS (arg);
13965 /* To proceed, MPFR must exactly represent the target floating point
13966 format, which only happens when the target base equals two. */
13967 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13968 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13969 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13971 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13972 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13974 if (real_isfinite (re) && real_isfinite (im))
13976 const struct real_format *const fmt =
13977 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13978 const int prec = fmt->p;
13979 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13980 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13981 int inexact;
13982 mpc_t m;
13984 mpc_init2 (m, prec);
13985 mpfr_from_real (mpc_realref (m), re, rnd);
13986 mpfr_from_real (mpc_imagref (m), im, rnd);
13987 mpfr_clear_flags ();
13988 inexact = func (m, m, crnd);
13989 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13990 mpc_clear (m);
13994 return result;
13997 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13998 mpc function FUNC on it and return the resulting value as a tree
13999 with type TYPE. The mpfr precision is set to the precision of
14000 TYPE. We assume that function FUNC returns zero if the result
14001 could be calculated exactly within the requested precision. If
14002 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14003 in the arguments and/or results. */
14005 tree
14006 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14007 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14009 tree result = NULL_TREE;
14011 STRIP_NOPS (arg0);
14012 STRIP_NOPS (arg1);
14014 /* To proceed, MPFR must exactly represent the target floating point
14015 format, which only happens when the target base equals two. */
14016 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14017 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14018 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14019 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14020 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14022 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14023 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14024 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14025 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14027 if (do_nonfinite
14028 || (real_isfinite (re0) && real_isfinite (im0)
14029 && real_isfinite (re1) && real_isfinite (im1)))
14031 const struct real_format *const fmt =
14032 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14033 const int prec = fmt->p;
14034 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14035 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14036 int inexact;
14037 mpc_t m0, m1;
14039 mpc_init2 (m0, prec);
14040 mpc_init2 (m1, prec);
14041 mpfr_from_real (mpc_realref (m0), re0, rnd);
14042 mpfr_from_real (mpc_imagref (m0), im0, rnd);
14043 mpfr_from_real (mpc_realref (m1), re1, rnd);
14044 mpfr_from_real (mpc_imagref (m1), im1, rnd);
14045 mpfr_clear_flags ();
14046 inexact = func (m0, m0, m1, crnd);
14047 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14048 mpc_clear (m0);
14049 mpc_clear (m1);
14053 return result;
14056 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14057 a normal call should be emitted rather than expanding the function
14058 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14060 static tree
14061 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14063 int nargs = gimple_call_num_args (stmt);
14065 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14066 (nargs > 0
14067 ? gimple_call_arg_ptr (stmt, 0)
14068 : &error_mark_node), fcode);
14071 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14072 a normal call should be emitted rather than expanding the function
14073 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14074 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14075 passed as second argument. */
14077 tree
14078 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14079 enum built_in_function fcode)
14081 int nargs = gimple_call_num_args (stmt);
14083 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14084 (nargs > 0
14085 ? gimple_call_arg_ptr (stmt, 0)
14086 : &error_mark_node), maxlen, fcode);
14089 /* Builtins with folding operations that operate on "..." arguments
14090 need special handling; we need to store the arguments in a convenient
14091 data structure before attempting any folding. Fortunately there are
14092 only a few builtins that fall into this category. FNDECL is the
14093 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14094 result of the function call is ignored. */
14096 static tree
14097 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14098 bool ignore ATTRIBUTE_UNUSED)
14100 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14101 tree ret = NULL_TREE;
14103 switch (fcode)
14105 case BUILT_IN_SPRINTF_CHK:
14106 case BUILT_IN_VSPRINTF_CHK:
14107 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14108 break;
14110 case BUILT_IN_SNPRINTF_CHK:
14111 case BUILT_IN_VSNPRINTF_CHK:
14112 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14114 default:
14115 break;
14117 if (ret)
14119 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14120 TREE_NO_WARNING (ret) = 1;
14121 return ret;
14123 return NULL_TREE;
14126 /* A wrapper function for builtin folding that prevents warnings for
14127 "statement without effect" and the like, caused by removing the
14128 call node earlier than the warning is generated. */
14130 tree
14131 fold_call_stmt (gimple stmt, bool ignore)
14133 tree ret = NULL_TREE;
14134 tree fndecl = gimple_call_fndecl (stmt);
14135 location_t loc = gimple_location (stmt);
14136 if (fndecl
14137 && TREE_CODE (fndecl) == FUNCTION_DECL
14138 && DECL_BUILT_IN (fndecl)
14139 && !gimple_call_va_arg_pack_p (stmt))
14141 int nargs = gimple_call_num_args (stmt);
14142 tree *args = (nargs > 0
14143 ? gimple_call_arg_ptr (stmt, 0)
14144 : &error_mark_node);
14146 if (avoid_folding_inline_builtin (fndecl))
14147 return NULL_TREE;
14148 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14150 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14152 else
14154 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14155 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14156 if (!ret)
14157 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14158 if (ret)
14160 /* Propagate location information from original call to
14161 expansion of builtin. Otherwise things like
14162 maybe_emit_chk_warning, that operate on the expansion
14163 of a builtin, will use the wrong location information. */
14164 if (gimple_has_location (stmt))
14166 tree realret = ret;
14167 if (TREE_CODE (ret) == NOP_EXPR)
14168 realret = TREE_OPERAND (ret, 0);
14169 if (CAN_HAVE_LOCATION_P (realret)
14170 && !EXPR_HAS_LOCATION (realret))
14171 SET_EXPR_LOCATION (realret, loc);
14172 return realret;
14174 return ret;
14178 return NULL_TREE;
14181 /* Look up the function in builtin_decl that corresponds to DECL
14182 and set ASMSPEC as its user assembler name. DECL must be a
14183 function decl that declares a builtin. */
14185 void
14186 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14188 tree builtin;
14189 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14190 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14191 && asmspec != 0);
14193 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14194 set_user_assembler_name (builtin, asmspec);
14195 switch (DECL_FUNCTION_CODE (decl))
14197 case BUILT_IN_MEMCPY:
14198 init_block_move_fn (asmspec);
14199 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14200 break;
14201 case BUILT_IN_MEMSET:
14202 init_block_clear_fn (asmspec);
14203 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14204 break;
14205 case BUILT_IN_MEMMOVE:
14206 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14207 break;
14208 case BUILT_IN_MEMCMP:
14209 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14210 break;
14211 case BUILT_IN_ABORT:
14212 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14213 break;
14214 case BUILT_IN_FFS:
14215 if (INT_TYPE_SIZE < BITS_PER_WORD)
14217 set_user_assembler_libfunc ("ffs", asmspec);
14218 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14219 MODE_INT, 0), "ffs");
14221 break;
14222 default:
14223 break;
14227 /* Return true if DECL is a builtin that expands to a constant or similarly
14228 simple code. */
14229 bool
14230 is_simple_builtin (tree decl)
14232 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14233 switch (DECL_FUNCTION_CODE (decl))
14235 /* Builtins that expand to constants. */
14236 case BUILT_IN_CONSTANT_P:
14237 case BUILT_IN_EXPECT:
14238 case BUILT_IN_OBJECT_SIZE:
14239 case BUILT_IN_UNREACHABLE:
14240 /* Simple register moves or loads from stack. */
14241 case BUILT_IN_ASSUME_ALIGNED:
14242 case BUILT_IN_RETURN_ADDRESS:
14243 case BUILT_IN_EXTRACT_RETURN_ADDR:
14244 case BUILT_IN_FROB_RETURN_ADDR:
14245 case BUILT_IN_RETURN:
14246 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14247 case BUILT_IN_FRAME_ADDRESS:
14248 case BUILT_IN_VA_END:
14249 case BUILT_IN_STACK_SAVE:
14250 case BUILT_IN_STACK_RESTORE:
14251 /* Exception state returns or moves registers around. */
14252 case BUILT_IN_EH_FILTER:
14253 case BUILT_IN_EH_POINTER:
14254 case BUILT_IN_EH_COPY_VALUES:
14255 return true;
14257 default:
14258 return false;
14261 return false;
14264 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14265 most probably expanded inline into reasonably simple code. This is a
14266 superset of is_simple_builtin. */
14267 bool
14268 is_inexpensive_builtin (tree decl)
14270 if (!decl)
14271 return false;
14272 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14273 return true;
14274 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14275 switch (DECL_FUNCTION_CODE (decl))
14277 case BUILT_IN_ABS:
14278 case BUILT_IN_ALLOCA:
14279 case BUILT_IN_ALLOCA_WITH_ALIGN:
14280 case BUILT_IN_BSWAP16:
14281 case BUILT_IN_BSWAP32:
14282 case BUILT_IN_BSWAP64:
14283 case BUILT_IN_CLZ:
14284 case BUILT_IN_CLZIMAX:
14285 case BUILT_IN_CLZL:
14286 case BUILT_IN_CLZLL:
14287 case BUILT_IN_CTZ:
14288 case BUILT_IN_CTZIMAX:
14289 case BUILT_IN_CTZL:
14290 case BUILT_IN_CTZLL:
14291 case BUILT_IN_FFS:
14292 case BUILT_IN_FFSIMAX:
14293 case BUILT_IN_FFSL:
14294 case BUILT_IN_FFSLL:
14295 case BUILT_IN_IMAXABS:
14296 case BUILT_IN_FINITE:
14297 case BUILT_IN_FINITEF:
14298 case BUILT_IN_FINITEL:
14299 case BUILT_IN_FINITED32:
14300 case BUILT_IN_FINITED64:
14301 case BUILT_IN_FINITED128:
14302 case BUILT_IN_FPCLASSIFY:
14303 case BUILT_IN_ISFINITE:
14304 case BUILT_IN_ISINF_SIGN:
14305 case BUILT_IN_ISINF:
14306 case BUILT_IN_ISINFF:
14307 case BUILT_IN_ISINFL:
14308 case BUILT_IN_ISINFD32:
14309 case BUILT_IN_ISINFD64:
14310 case BUILT_IN_ISINFD128:
14311 case BUILT_IN_ISNAN:
14312 case BUILT_IN_ISNANF:
14313 case BUILT_IN_ISNANL:
14314 case BUILT_IN_ISNAND32:
14315 case BUILT_IN_ISNAND64:
14316 case BUILT_IN_ISNAND128:
14317 case BUILT_IN_ISNORMAL:
14318 case BUILT_IN_ISGREATER:
14319 case BUILT_IN_ISGREATEREQUAL:
14320 case BUILT_IN_ISLESS:
14321 case BUILT_IN_ISLESSEQUAL:
14322 case BUILT_IN_ISLESSGREATER:
14323 case BUILT_IN_ISUNORDERED:
14324 case BUILT_IN_VA_ARG_PACK:
14325 case BUILT_IN_VA_ARG_PACK_LEN:
14326 case BUILT_IN_VA_COPY:
14327 case BUILT_IN_TRAP:
14328 case BUILT_IN_SAVEREGS:
14329 case BUILT_IN_POPCOUNTL:
14330 case BUILT_IN_POPCOUNTLL:
14331 case BUILT_IN_POPCOUNTIMAX:
14332 case BUILT_IN_POPCOUNT:
14333 case BUILT_IN_PARITYL:
14334 case BUILT_IN_PARITYLL:
14335 case BUILT_IN_PARITYIMAX:
14336 case BUILT_IN_PARITY:
14337 case BUILT_IN_LABS:
14338 case BUILT_IN_LLABS:
14339 case BUILT_IN_PREFETCH:
14340 return true;
14342 default:
14343 return is_simple_builtin (decl);
14346 return false;