download_prerequisites: Update ISL and CLOOG versions.
[official-gcc.git] / gcc / builtins.c
blobf5f55bf0e2e60ffb009a4368573ec4fbb165ecd8
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_expect (location_t, tree, tree);
144 static tree fold_builtin_classify_type (tree);
145 static tree fold_builtin_strlen (location_t, tree, tree);
146 static tree fold_builtin_inf (location_t, tree, int);
147 static tree fold_builtin_nan (tree, tree, int);
148 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
149 static bool validate_arg (const_tree, enum tree_code code);
150 static bool integer_valued_real_p (tree);
151 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
152 static bool readonly_data_expr (tree);
153 static rtx expand_builtin_fabs (tree, rtx, rtx);
154 static rtx expand_builtin_signbit (tree, rtx);
155 static tree fold_builtin_sqrt (location_t, tree, tree);
156 static tree fold_builtin_cbrt (location_t, tree, tree);
157 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
158 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
159 static tree fold_builtin_cos (location_t, tree, tree, tree);
160 static tree fold_builtin_cosh (location_t, tree, tree, tree);
161 static tree fold_builtin_tan (tree, tree);
162 static tree fold_builtin_trunc (location_t, tree, tree);
163 static tree fold_builtin_floor (location_t, tree, tree);
164 static tree fold_builtin_ceil (location_t, tree, tree);
165 static tree fold_builtin_round (location_t, tree, tree);
166 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
167 static tree fold_builtin_bitop (tree, tree);
168 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
169 static tree fold_builtin_strchr (location_t, tree, tree, tree);
170 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
171 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
172 static tree fold_builtin_strcmp (location_t, tree, tree);
173 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
174 static tree fold_builtin_signbit (location_t, tree, tree);
175 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
176 static tree fold_builtin_isascii (location_t, tree);
177 static tree fold_builtin_toascii (location_t, tree);
178 static tree fold_builtin_isdigit (location_t, tree);
179 static tree fold_builtin_fabs (location_t, tree, tree);
180 static tree fold_builtin_abs (location_t, tree, tree);
181 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
182 enum tree_code);
183 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
184 static tree fold_builtin_0 (location_t, tree, bool);
185 static tree fold_builtin_1 (location_t, tree, tree, bool);
186 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
187 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
188 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
189 static tree fold_builtin_varargs (location_t, tree, tree, bool);
191 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
192 static tree fold_builtin_strstr (location_t, tree, tree, tree);
193 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
194 static tree fold_builtin_strncat (location_t, tree, tree, tree);
195 static tree fold_builtin_strspn (location_t, tree, tree);
196 static tree fold_builtin_strcspn (location_t, tree, tree);
197 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
198 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
200 static rtx expand_builtin_object_size (tree);
201 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
202 enum built_in_function);
203 static void maybe_emit_chk_warning (tree, enum built_in_function);
204 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
205 static void maybe_emit_free_warning (tree);
206 static tree fold_builtin_object_size (tree, tree);
207 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
208 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
209 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
210 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
211 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
212 enum built_in_function);
213 static bool init_target_chars (void);
215 static unsigned HOST_WIDE_INT target_newline;
216 static unsigned HOST_WIDE_INT target_percent;
217 static unsigned HOST_WIDE_INT target_c;
218 static unsigned HOST_WIDE_INT target_s;
219 static char target_percent_c[3];
220 static char target_percent_s[3];
221 static char target_percent_s_newline[4];
222 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
223 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
224 static tree do_mpfr_arg2 (tree, tree, tree,
225 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
226 static tree do_mpfr_arg3 (tree, tree, tree, tree,
227 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
228 static tree do_mpfr_sincos (tree, tree, tree);
229 static tree do_mpfr_bessel_n (tree, tree, tree,
230 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
231 const REAL_VALUE_TYPE *, bool);
232 static tree do_mpfr_remquo (tree, tree, tree);
233 static tree do_mpfr_lgamma_r (tree, tree, tree);
234 static void expand_builtin_sync_synchronize (void);
236 /* Return true if NAME starts with __builtin_ or __sync_. */
238 static bool
239 is_builtin_name (const char *name)
241 if (strncmp (name, "__builtin_", 10) == 0)
242 return true;
243 if (strncmp (name, "__sync_", 7) == 0)
244 return true;
245 if (strncmp (name, "__atomic_", 9) == 0)
246 return true;
247 if (flag_cilkplus
248 && (!strcmp (name, "__cilkrts_detach")
249 || !strcmp (name, "__cilkrts_pop_frame")))
250 return true;
251 return false;
255 /* Return true if DECL is a function symbol representing a built-in. */
257 bool
258 is_builtin_fn (tree decl)
260 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
263 /* By default we assume that c99 functions are present at the runtime,
264 but sincos is not. */
265 bool
266 default_libc_has_function (enum function_class fn_class)
268 if (fn_class == function_c94
269 || fn_class == function_c99_misc
270 || fn_class == function_c99_math_complex)
271 return true;
273 return false;
276 bool
277 gnu_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED)
279 return true;
282 bool
283 no_c99_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED)
285 return false;
288 /* Return true if NODE should be considered for inline expansion regardless
289 of the optimization level. This means whenever a function is invoked with
290 its "internal" name, which normally contains the prefix "__builtin". */
292 static bool
293 called_as_built_in (tree node)
295 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
296 we want the name used to call the function, not the name it
297 will have. */
298 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
299 return is_builtin_name (name);
302 /* Compute values M and N such that M divides (address of EXP - N) and such
303 that N < M. If these numbers can be determined, store M in alignp and N in
304 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
305 *alignp and any bit-offset to *bitposp.
307 Note that the address (and thus the alignment) computed here is based
308 on the address to which a symbol resolves, whereas DECL_ALIGN is based
309 on the address at which an object is actually located. These two
310 addresses are not always the same. For example, on ARM targets,
311 the address &foo of a Thumb function foo() has the lowest bit set,
312 whereas foo() itself starts on an even address.
314 If ADDR_P is true we are taking the address of the memory reference EXP
315 and thus cannot rely on the access taking place. */
317 static bool
318 get_object_alignment_2 (tree exp, unsigned int *alignp,
319 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
321 HOST_WIDE_INT bitsize, bitpos;
322 tree offset;
323 enum machine_mode mode;
324 int unsignedp, volatilep;
325 unsigned int align = BITS_PER_UNIT;
326 bool known_alignment = false;
328 /* Get the innermost object and the constant (bitpos) and possibly
329 variable (offset) offset of the access. */
330 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
331 &mode, &unsignedp, &volatilep, true);
333 /* Extract alignment information from the innermost object and
334 possibly adjust bitpos and offset. */
335 if (TREE_CODE (exp) == FUNCTION_DECL)
337 /* Function addresses can encode extra information besides their
338 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
339 allows the low bit to be used as a virtual bit, we know
340 that the address itself must be at least 2-byte aligned. */
341 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
342 align = 2 * BITS_PER_UNIT;
344 else if (TREE_CODE (exp) == LABEL_DECL)
346 else if (TREE_CODE (exp) == CONST_DECL)
348 /* The alignment of a CONST_DECL is determined by its initializer. */
349 exp = DECL_INITIAL (exp);
350 align = TYPE_ALIGN (TREE_TYPE (exp));
351 #ifdef CONSTANT_ALIGNMENT
352 if (CONSTANT_CLASS_P (exp))
353 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
354 #endif
355 known_alignment = true;
357 else if (DECL_P (exp))
359 align = DECL_ALIGN (exp);
360 known_alignment = true;
362 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
364 align = TYPE_ALIGN (TREE_TYPE (exp));
366 else if (TREE_CODE (exp) == INDIRECT_REF
367 || TREE_CODE (exp) == MEM_REF
368 || TREE_CODE (exp) == TARGET_MEM_REF)
370 tree addr = TREE_OPERAND (exp, 0);
371 unsigned ptr_align;
372 unsigned HOST_WIDE_INT ptr_bitpos;
374 if (TREE_CODE (addr) == BIT_AND_EXPR
375 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
377 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
378 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
379 align *= BITS_PER_UNIT;
380 addr = TREE_OPERAND (addr, 0);
383 known_alignment
384 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
385 align = MAX (ptr_align, align);
387 /* The alignment of the pointer operand in a TARGET_MEM_REF
388 has to take the variable offset parts into account. */
389 if (TREE_CODE (exp) == TARGET_MEM_REF)
391 if (TMR_INDEX (exp))
393 unsigned HOST_WIDE_INT step = 1;
394 if (TMR_STEP (exp))
395 step = TREE_INT_CST_LOW (TMR_STEP (exp));
396 align = MIN (align, (step & -step) * BITS_PER_UNIT);
398 if (TMR_INDEX2 (exp))
399 align = BITS_PER_UNIT;
400 known_alignment = false;
403 /* When EXP is an actual memory reference then we can use
404 TYPE_ALIGN of a pointer indirection to derive alignment.
405 Do so only if get_pointer_alignment_1 did not reveal absolute
406 alignment knowledge and if using that alignment would
407 improve the situation. */
408 if (!addr_p && !known_alignment
409 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
410 align = TYPE_ALIGN (TREE_TYPE (exp));
411 else
413 /* Else adjust bitpos accordingly. */
414 bitpos += ptr_bitpos;
415 if (TREE_CODE (exp) == MEM_REF
416 || TREE_CODE (exp) == TARGET_MEM_REF)
417 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
420 else if (TREE_CODE (exp) == STRING_CST)
422 /* STRING_CST are the only constant objects we allow to be not
423 wrapped inside a CONST_DECL. */
424 align = TYPE_ALIGN (TREE_TYPE (exp));
425 #ifdef CONSTANT_ALIGNMENT
426 if (CONSTANT_CLASS_P (exp))
427 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
428 #endif
429 known_alignment = true;
432 /* If there is a non-constant offset part extract the maximum
433 alignment that can prevail. */
434 if (offset)
436 unsigned int trailing_zeros = tree_ctz (offset);
437 if (trailing_zeros < HOST_BITS_PER_INT)
439 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
440 if (inner)
441 align = MIN (align, inner);
445 *alignp = align;
446 *bitposp = bitpos & (*alignp - 1);
447 return known_alignment;
450 /* For a memory reference expression EXP compute values M and N such that M
451 divides (&EXP - N) and such that N < M. If these numbers can be determined,
452 store M in alignp and N in *BITPOSP and return true. Otherwise return false
453 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
455 bool
456 get_object_alignment_1 (tree exp, unsigned int *alignp,
457 unsigned HOST_WIDE_INT *bitposp)
459 return get_object_alignment_2 (exp, alignp, bitposp, false);
462 /* Return the alignment in bits of EXP, an object. */
464 unsigned int
465 get_object_alignment (tree exp)
467 unsigned HOST_WIDE_INT bitpos = 0;
468 unsigned int align;
470 get_object_alignment_1 (exp, &align, &bitpos);
472 /* align and bitpos now specify known low bits of the pointer.
473 ptr & (align - 1) == bitpos. */
475 if (bitpos != 0)
476 align = (bitpos & -bitpos);
477 return align;
480 /* For a pointer valued expression EXP compute values M and N such that M
481 divides (EXP - N) and such that N < M. If these numbers can be determined,
482 store M in alignp and N in *BITPOSP and return true. Return false if
483 the results are just a conservative approximation.
485 If EXP is not a pointer, false is returned too. */
487 bool
488 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
489 unsigned HOST_WIDE_INT *bitposp)
491 STRIP_NOPS (exp);
493 if (TREE_CODE (exp) == ADDR_EXPR)
494 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
495 alignp, bitposp, true);
496 else if (TREE_CODE (exp) == SSA_NAME
497 && POINTER_TYPE_P (TREE_TYPE (exp)))
499 unsigned int ptr_align, ptr_misalign;
500 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
502 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
504 *bitposp = ptr_misalign * BITS_PER_UNIT;
505 *alignp = ptr_align * BITS_PER_UNIT;
506 /* We cannot really tell whether this result is an approximation. */
507 return true;
509 else
511 *bitposp = 0;
512 *alignp = BITS_PER_UNIT;
513 return false;
516 else if (TREE_CODE (exp) == INTEGER_CST)
518 *alignp = BIGGEST_ALIGNMENT;
519 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
520 & (BIGGEST_ALIGNMENT - 1));
521 return true;
524 *bitposp = 0;
525 *alignp = BITS_PER_UNIT;
526 return false;
529 /* Return the alignment in bits of EXP, a pointer valued expression.
530 The alignment returned is, by default, the alignment of the thing that
531 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
533 Otherwise, look at the expression to see if we can do better, i.e., if the
534 expression is actually pointing at an object whose alignment is tighter. */
536 unsigned int
537 get_pointer_alignment (tree exp)
539 unsigned HOST_WIDE_INT bitpos = 0;
540 unsigned int align;
542 get_pointer_alignment_1 (exp, &align, &bitpos);
544 /* align and bitpos now specify known low bits of the pointer.
545 ptr & (align - 1) == bitpos. */
547 if (bitpos != 0)
548 align = (bitpos & -bitpos);
550 return align;
553 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
554 way, because it could contain a zero byte in the middle.
555 TREE_STRING_LENGTH is the size of the character array, not the string.
557 ONLY_VALUE should be nonzero if the result is not going to be emitted
558 into the instruction stream and zero if it is going to be expanded.
559 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
560 is returned, otherwise NULL, since
561 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
562 evaluate the side-effects.
564 The value returned is of type `ssizetype'.
566 Unfortunately, string_constant can't access the values of const char
567 arrays with initializers, so neither can we do so here. */
569 tree
570 c_strlen (tree src, int only_value)
572 tree offset_node;
573 HOST_WIDE_INT offset;
574 int max;
575 const char *ptr;
576 location_t loc;
578 STRIP_NOPS (src);
579 if (TREE_CODE (src) == COND_EXPR
580 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
582 tree len1, len2;
584 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
585 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
586 if (tree_int_cst_equal (len1, len2))
587 return len1;
590 if (TREE_CODE (src) == COMPOUND_EXPR
591 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
592 return c_strlen (TREE_OPERAND (src, 1), only_value);
594 loc = EXPR_LOC_OR_LOC (src, input_location);
596 src = string_constant (src, &offset_node);
597 if (src == 0)
598 return NULL_TREE;
600 max = TREE_STRING_LENGTH (src) - 1;
601 ptr = TREE_STRING_POINTER (src);
603 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
605 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
606 compute the offset to the following null if we don't know where to
607 start searching for it. */
608 int i;
610 for (i = 0; i < max; i++)
611 if (ptr[i] == 0)
612 return NULL_TREE;
614 /* We don't know the starting offset, but we do know that the string
615 has no internal zero bytes. We can assume that the offset falls
616 within the bounds of the string; otherwise, the programmer deserves
617 what he gets. Subtract the offset from the length of the string,
618 and return that. This would perhaps not be valid if we were dealing
619 with named arrays in addition to literal string constants. */
621 return size_diffop_loc (loc, size_int (max), offset_node);
624 /* We have a known offset into the string. Start searching there for
625 a null character if we can represent it as a single HOST_WIDE_INT. */
626 if (offset_node == 0)
627 offset = 0;
628 else if (! tree_fits_shwi_p (offset_node))
629 offset = -1;
630 else
631 offset = tree_to_shwi (offset_node);
633 /* If the offset is known to be out of bounds, warn, and call strlen at
634 runtime. */
635 if (offset < 0 || offset > max)
637 /* Suppress multiple warnings for propagated constant strings. */
638 if (! TREE_NO_WARNING (src))
640 warning_at (loc, 0, "offset outside bounds of constant string");
641 TREE_NO_WARNING (src) = 1;
643 return NULL_TREE;
646 /* Use strlen to search for the first zero byte. Since any strings
647 constructed with build_string will have nulls appended, we win even
648 if we get handed something like (char[4])"abcd".
650 Since OFFSET is our starting index into the string, no further
651 calculation is needed. */
652 return ssize_int (strlen (ptr + offset));
655 /* Return a char pointer for a C string if it is a string constant
656 or sum of string constant and integer constant. */
658 static const char *
659 c_getstr (tree src)
661 tree offset_node;
663 src = string_constant (src, &offset_node);
664 if (src == 0)
665 return 0;
667 if (offset_node == 0)
668 return TREE_STRING_POINTER (src);
669 else if (!tree_fits_uhwi_p (offset_node)
670 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
671 return 0;
673 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
676 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
677 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
679 static rtx
680 c_readstr (const char *str, enum machine_mode mode)
682 HOST_WIDE_INT c[2];
683 HOST_WIDE_INT ch;
684 unsigned int i, j;
686 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
688 c[0] = 0;
689 c[1] = 0;
690 ch = 1;
691 for (i = 0; i < GET_MODE_SIZE (mode); i++)
693 j = i;
694 if (WORDS_BIG_ENDIAN)
695 j = GET_MODE_SIZE (mode) - i - 1;
696 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
697 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
698 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
699 j *= BITS_PER_UNIT;
700 gcc_assert (j < HOST_BITS_PER_DOUBLE_INT);
702 if (ch)
703 ch = (unsigned char) str[i];
704 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
706 return immed_double_const (c[0], c[1], mode);
709 /* Cast a target constant CST to target CHAR and if that value fits into
710 host char type, return zero and put that value into variable pointed to by
711 P. */
713 static int
714 target_char_cast (tree cst, char *p)
716 unsigned HOST_WIDE_INT val, hostval;
718 if (TREE_CODE (cst) != INTEGER_CST
719 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
720 return 1;
722 val = TREE_INT_CST_LOW (cst);
723 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
724 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
726 hostval = val;
727 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
728 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
730 if (val != hostval)
731 return 1;
733 *p = hostval;
734 return 0;
737 /* Similar to save_expr, but assumes that arbitrary code is not executed
738 in between the multiple evaluations. In particular, we assume that a
739 non-addressable local variable will not be modified. */
741 static tree
742 builtin_save_expr (tree exp)
744 if (TREE_CODE (exp) == SSA_NAME
745 || (TREE_ADDRESSABLE (exp) == 0
746 && (TREE_CODE (exp) == PARM_DECL
747 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
748 return exp;
750 return save_expr (exp);
753 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
754 times to get the address of either a higher stack frame, or a return
755 address located within it (depending on FNDECL_CODE). */
757 static rtx
758 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
760 int i;
762 #ifdef INITIAL_FRAME_ADDRESS_RTX
763 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
764 #else
765 rtx tem;
767 /* For a zero count with __builtin_return_address, we don't care what
768 frame address we return, because target-specific definitions will
769 override us. Therefore frame pointer elimination is OK, and using
770 the soft frame pointer is OK.
772 For a nonzero count, or a zero count with __builtin_frame_address,
773 we require a stable offset from the current frame pointer to the
774 previous one, so we must use the hard frame pointer, and
775 we must disable frame pointer elimination. */
776 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
777 tem = frame_pointer_rtx;
778 else
780 tem = hard_frame_pointer_rtx;
782 /* Tell reload not to eliminate the frame pointer. */
783 crtl->accesses_prior_frames = 1;
785 #endif
787 /* Some machines need special handling before we can access
788 arbitrary frames. For example, on the SPARC, we must first flush
789 all register windows to the stack. */
790 #ifdef SETUP_FRAME_ADDRESSES
791 if (count > 0)
792 SETUP_FRAME_ADDRESSES ();
793 #endif
795 /* On the SPARC, the return address is not in the frame, it is in a
796 register. There is no way to access it off of the current frame
797 pointer, but it can be accessed off the previous frame pointer by
798 reading the value from the register window save area. */
799 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
800 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
801 count--;
802 #endif
804 /* Scan back COUNT frames to the specified frame. */
805 for (i = 0; i < count; i++)
807 /* Assume the dynamic chain pointer is in the word that the
808 frame address points to, unless otherwise specified. */
809 #ifdef DYNAMIC_CHAIN_ADDRESS
810 tem = DYNAMIC_CHAIN_ADDRESS (tem);
811 #endif
812 tem = memory_address (Pmode, tem);
813 tem = gen_frame_mem (Pmode, tem);
814 tem = copy_to_reg (tem);
817 /* For __builtin_frame_address, return what we've got. But, on
818 the SPARC for example, we may have to add a bias. */
819 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
820 #ifdef FRAME_ADDR_RTX
821 return FRAME_ADDR_RTX (tem);
822 #else
823 return tem;
824 #endif
826 /* For __builtin_return_address, get the return address from that frame. */
827 #ifdef RETURN_ADDR_RTX
828 tem = RETURN_ADDR_RTX (count, tem);
829 #else
830 tem = memory_address (Pmode,
831 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
832 tem = gen_frame_mem (Pmode, tem);
833 #endif
834 return tem;
837 /* Alias set used for setjmp buffer. */
838 static alias_set_type setjmp_alias_set = -1;
840 /* Construct the leading half of a __builtin_setjmp call. Control will
841 return to RECEIVER_LABEL. This is also called directly by the SJLJ
842 exception handling code. */
844 void
845 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
847 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
848 rtx stack_save;
849 rtx mem;
851 if (setjmp_alias_set == -1)
852 setjmp_alias_set = new_alias_set ();
854 buf_addr = convert_memory_address (Pmode, buf_addr);
856 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
858 /* We store the frame pointer and the address of receiver_label in
859 the buffer and use the rest of it for the stack save area, which
860 is machine-dependent. */
862 mem = gen_rtx_MEM (Pmode, buf_addr);
863 set_mem_alias_set (mem, setjmp_alias_set);
864 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
866 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
867 GET_MODE_SIZE (Pmode))),
868 set_mem_alias_set (mem, setjmp_alias_set);
870 emit_move_insn (validize_mem (mem),
871 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
873 stack_save = gen_rtx_MEM (sa_mode,
874 plus_constant (Pmode, buf_addr,
875 2 * GET_MODE_SIZE (Pmode)));
876 set_mem_alias_set (stack_save, setjmp_alias_set);
877 emit_stack_save (SAVE_NONLOCAL, &stack_save);
879 /* If there is further processing to do, do it. */
880 #ifdef HAVE_builtin_setjmp_setup
881 if (HAVE_builtin_setjmp_setup)
882 emit_insn (gen_builtin_setjmp_setup (buf_addr));
883 #endif
885 /* We have a nonlocal label. */
886 cfun->has_nonlocal_label = 1;
889 /* Construct the trailing part of a __builtin_setjmp call. This is
890 also called directly by the SJLJ exception handling code.
891 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
893 void
894 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
896 rtx chain;
898 /* Mark the FP as used when we get here, so we have to make sure it's
899 marked as used by this function. */
900 emit_use (hard_frame_pointer_rtx);
902 /* Mark the static chain as clobbered here so life information
903 doesn't get messed up for it. */
904 chain = targetm.calls.static_chain (current_function_decl, true);
905 if (chain && REG_P (chain))
906 emit_clobber (chain);
908 /* Now put in the code to restore the frame pointer, and argument
909 pointer, if needed. */
910 #ifdef HAVE_nonlocal_goto
911 if (! HAVE_nonlocal_goto)
912 #endif
913 /* First adjust our frame pointer to its actual value. It was
914 previously set to the start of the virtual area corresponding to
915 the stacked variables when we branched here and now needs to be
916 adjusted to the actual hardware fp value.
918 Assignments to virtual registers are converted by
919 instantiate_virtual_regs into the corresponding assignment
920 to the underlying register (fp in this case) that makes
921 the original assignment true.
922 So the following insn will actually be decrementing fp by
923 STARTING_FRAME_OFFSET. */
924 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
926 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
927 if (fixed_regs[ARG_POINTER_REGNUM])
929 #ifdef ELIMINABLE_REGS
930 /* If the argument pointer can be eliminated in favor of the
931 frame pointer, we don't need to restore it. We assume here
932 that if such an elimination is present, it can always be used.
933 This is the case on all known machines; if we don't make this
934 assumption, we do unnecessary saving on many machines. */
935 size_t i;
936 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
938 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
939 if (elim_regs[i].from == ARG_POINTER_REGNUM
940 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
941 break;
943 if (i == ARRAY_SIZE (elim_regs))
944 #endif
946 /* Now restore our arg pointer from the address at which it
947 was saved in our stack frame. */
948 emit_move_insn (crtl->args.internal_arg_pointer,
949 copy_to_reg (get_arg_pointer_save_area ()));
952 #endif
954 #ifdef HAVE_builtin_setjmp_receiver
955 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
956 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
957 else
958 #endif
959 #ifdef HAVE_nonlocal_goto_receiver
960 if (HAVE_nonlocal_goto_receiver)
961 emit_insn (gen_nonlocal_goto_receiver ());
962 else
963 #endif
964 { /* Nothing */ }
966 /* We must not allow the code we just generated to be reordered by
967 scheduling. Specifically, the update of the frame pointer must
968 happen immediately, not later. Similarly, we must block
969 (frame-related) register values to be used across this code. */
970 emit_insn (gen_blockage ());
973 /* __builtin_longjmp is passed a pointer to an array of five words (not
974 all will be used on all machines). It operates similarly to the C
975 library function of the same name, but is more efficient. Much of
976 the code below is copied from the handling of non-local gotos. */
978 static void
979 expand_builtin_longjmp (rtx buf_addr, rtx value)
981 rtx fp, lab, stack, insn, last;
982 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
984 /* DRAP is needed for stack realign if longjmp is expanded to current
985 function */
986 if (SUPPORTS_STACK_ALIGNMENT)
987 crtl->need_drap = true;
989 if (setjmp_alias_set == -1)
990 setjmp_alias_set = new_alias_set ();
992 buf_addr = convert_memory_address (Pmode, buf_addr);
994 buf_addr = force_reg (Pmode, buf_addr);
996 /* We require that the user must pass a second argument of 1, because
997 that is what builtin_setjmp will return. */
998 gcc_assert (value == const1_rtx);
1000 last = get_last_insn ();
1001 #ifdef HAVE_builtin_longjmp
1002 if (HAVE_builtin_longjmp)
1003 emit_insn (gen_builtin_longjmp (buf_addr));
1004 else
1005 #endif
1007 fp = gen_rtx_MEM (Pmode, buf_addr);
1008 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1009 GET_MODE_SIZE (Pmode)));
1011 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1012 2 * GET_MODE_SIZE (Pmode)));
1013 set_mem_alias_set (fp, setjmp_alias_set);
1014 set_mem_alias_set (lab, setjmp_alias_set);
1015 set_mem_alias_set (stack, setjmp_alias_set);
1017 /* Pick up FP, label, and SP from the block and jump. This code is
1018 from expand_goto in stmt.c; see there for detailed comments. */
1019 #ifdef HAVE_nonlocal_goto
1020 if (HAVE_nonlocal_goto)
1021 /* We have to pass a value to the nonlocal_goto pattern that will
1022 get copied into the static_chain pointer, but it does not matter
1023 what that value is, because builtin_setjmp does not use it. */
1024 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1025 else
1026 #endif
1028 lab = copy_to_reg (lab);
1030 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1031 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1033 emit_move_insn (hard_frame_pointer_rtx, fp);
1034 emit_stack_restore (SAVE_NONLOCAL, stack);
1036 emit_use (hard_frame_pointer_rtx);
1037 emit_use (stack_pointer_rtx);
1038 emit_indirect_jump (lab);
1042 /* Search backwards and mark the jump insn as a non-local goto.
1043 Note that this precludes the use of __builtin_longjmp to a
1044 __builtin_setjmp target in the same function. However, we've
1045 already cautioned the user that these functions are for
1046 internal exception handling use only. */
1047 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1049 gcc_assert (insn != last);
1051 if (JUMP_P (insn))
1053 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1054 break;
1056 else if (CALL_P (insn))
1057 break;
1061 static inline bool
1062 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1064 return (iter->i < iter->n);
1067 /* This function validates the types of a function call argument list
1068 against a specified list of tree_codes. If the last specifier is a 0,
1069 that represents an ellipses, otherwise the last specifier must be a
1070 VOID_TYPE. */
1072 static bool
1073 validate_arglist (const_tree callexpr, ...)
1075 enum tree_code code;
1076 bool res = 0;
1077 va_list ap;
1078 const_call_expr_arg_iterator iter;
1079 const_tree arg;
1081 va_start (ap, callexpr);
1082 init_const_call_expr_arg_iterator (callexpr, &iter);
1086 code = (enum tree_code) va_arg (ap, int);
1087 switch (code)
1089 case 0:
1090 /* This signifies an ellipses, any further arguments are all ok. */
1091 res = true;
1092 goto end;
1093 case VOID_TYPE:
1094 /* This signifies an endlink, if no arguments remain, return
1095 true, otherwise return false. */
1096 res = !more_const_call_expr_args_p (&iter);
1097 goto end;
1098 default:
1099 /* If no parameters remain or the parameter's code does not
1100 match the specified code, return false. Otherwise continue
1101 checking any remaining arguments. */
1102 arg = next_const_call_expr_arg (&iter);
1103 if (!validate_arg (arg, code))
1104 goto end;
1105 break;
1108 while (1);
1110 /* We need gotos here since we can only have one VA_CLOSE in a
1111 function. */
1112 end: ;
1113 va_end (ap);
1115 return res;
1118 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1119 and the address of the save area. */
1121 static rtx
1122 expand_builtin_nonlocal_goto (tree exp)
1124 tree t_label, t_save_area;
1125 rtx r_label, r_save_area, r_fp, r_sp, insn;
1127 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1128 return NULL_RTX;
1130 t_label = CALL_EXPR_ARG (exp, 0);
1131 t_save_area = CALL_EXPR_ARG (exp, 1);
1133 r_label = expand_normal (t_label);
1134 r_label = convert_memory_address (Pmode, r_label);
1135 r_save_area = expand_normal (t_save_area);
1136 r_save_area = convert_memory_address (Pmode, r_save_area);
1137 /* Copy the address of the save location to a register just in case it was
1138 based on the frame pointer. */
1139 r_save_area = copy_to_reg (r_save_area);
1140 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1141 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1142 plus_constant (Pmode, r_save_area,
1143 GET_MODE_SIZE (Pmode)));
1145 crtl->has_nonlocal_goto = 1;
1147 #ifdef HAVE_nonlocal_goto
1148 /* ??? We no longer need to pass the static chain value, afaik. */
1149 if (HAVE_nonlocal_goto)
1150 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1151 else
1152 #endif
1154 r_label = copy_to_reg (r_label);
1156 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1157 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1159 /* Restore frame pointer for containing function. */
1160 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1161 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1163 /* USE of hard_frame_pointer_rtx added for consistency;
1164 not clear if really needed. */
1165 emit_use (hard_frame_pointer_rtx);
1166 emit_use (stack_pointer_rtx);
1168 /* If the architecture is using a GP register, we must
1169 conservatively assume that the target function makes use of it.
1170 The prologue of functions with nonlocal gotos must therefore
1171 initialize the GP register to the appropriate value, and we
1172 must then make sure that this value is live at the point
1173 of the jump. (Note that this doesn't necessarily apply
1174 to targets with a nonlocal_goto pattern; they are free
1175 to implement it in their own way. Note also that this is
1176 a no-op if the GP register is a global invariant.) */
1177 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1178 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1179 emit_use (pic_offset_table_rtx);
1181 emit_indirect_jump (r_label);
1184 /* Search backwards to the jump insn and mark it as a
1185 non-local goto. */
1186 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1188 if (JUMP_P (insn))
1190 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1191 break;
1193 else if (CALL_P (insn))
1194 break;
1197 return const0_rtx;
1200 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1201 (not all will be used on all machines) that was passed to __builtin_setjmp.
1202 It updates the stack pointer in that block to correspond to the current
1203 stack pointer. */
1205 static void
1206 expand_builtin_update_setjmp_buf (rtx buf_addr)
1208 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1209 rtx stack_save
1210 = gen_rtx_MEM (sa_mode,
1211 memory_address
1212 (sa_mode,
1213 plus_constant (Pmode, buf_addr,
1214 2 * GET_MODE_SIZE (Pmode))));
1216 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1219 /* Expand a call to __builtin_prefetch. For a target that does not support
1220 data prefetch, evaluate the memory address argument in case it has side
1221 effects. */
1223 static void
1224 expand_builtin_prefetch (tree exp)
1226 tree arg0, arg1, arg2;
1227 int nargs;
1228 rtx op0, op1, op2;
1230 if (!validate_arglist (exp, POINTER_TYPE, 0))
1231 return;
1233 arg0 = CALL_EXPR_ARG (exp, 0);
1235 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1236 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1237 locality). */
1238 nargs = call_expr_nargs (exp);
1239 if (nargs > 1)
1240 arg1 = CALL_EXPR_ARG (exp, 1);
1241 else
1242 arg1 = integer_zero_node;
1243 if (nargs > 2)
1244 arg2 = CALL_EXPR_ARG (exp, 2);
1245 else
1246 arg2 = integer_three_node;
1248 /* Argument 0 is an address. */
1249 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1251 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1252 if (TREE_CODE (arg1) != INTEGER_CST)
1254 error ("second argument to %<__builtin_prefetch%> must be a constant");
1255 arg1 = integer_zero_node;
1257 op1 = expand_normal (arg1);
1258 /* Argument 1 must be either zero or one. */
1259 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1261 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1262 " using zero");
1263 op1 = const0_rtx;
1266 /* Argument 2 (locality) must be a compile-time constant int. */
1267 if (TREE_CODE (arg2) != INTEGER_CST)
1269 error ("third argument to %<__builtin_prefetch%> must be a constant");
1270 arg2 = integer_zero_node;
1272 op2 = expand_normal (arg2);
1273 /* Argument 2 must be 0, 1, 2, or 3. */
1274 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1276 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1277 op2 = const0_rtx;
1280 #ifdef HAVE_prefetch
1281 if (HAVE_prefetch)
1283 struct expand_operand ops[3];
1285 create_address_operand (&ops[0], op0);
1286 create_integer_operand (&ops[1], INTVAL (op1));
1287 create_integer_operand (&ops[2], INTVAL (op2));
1288 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1289 return;
1291 #endif
1293 /* Don't do anything with direct references to volatile memory, but
1294 generate code to handle other side effects. */
1295 if (!MEM_P (op0) && side_effects_p (op0))
1296 emit_insn (op0);
1299 /* Get a MEM rtx for expression EXP which is the address of an operand
1300 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1301 the maximum length of the block of memory that might be accessed or
1302 NULL if unknown. */
1304 static rtx
1305 get_memory_rtx (tree exp, tree len)
1307 tree orig_exp = exp;
1308 rtx addr, mem;
1310 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1311 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1312 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1313 exp = TREE_OPERAND (exp, 0);
1315 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1316 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1318 /* Get an expression we can use to find the attributes to assign to MEM.
1319 First remove any nops. */
1320 while (CONVERT_EXPR_P (exp)
1321 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1322 exp = TREE_OPERAND (exp, 0);
1324 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1325 (as builtin stringops may alias with anything). */
1326 exp = fold_build2 (MEM_REF,
1327 build_array_type (char_type_node,
1328 build_range_type (sizetype,
1329 size_one_node, len)),
1330 exp, build_int_cst (ptr_type_node, 0));
1332 /* If the MEM_REF has no acceptable address, try to get the base object
1333 from the original address we got, and build an all-aliasing
1334 unknown-sized access to that one. */
1335 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1336 set_mem_attributes (mem, exp, 0);
1337 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1338 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1339 0))))
1341 exp = build_fold_addr_expr (exp);
1342 exp = fold_build2 (MEM_REF,
1343 build_array_type (char_type_node,
1344 build_range_type (sizetype,
1345 size_zero_node,
1346 NULL)),
1347 exp, build_int_cst (ptr_type_node, 0));
1348 set_mem_attributes (mem, exp, 0);
1350 set_mem_alias_set (mem, 0);
1351 return mem;
1354 /* Built-in functions to perform an untyped call and return. */
1356 #define apply_args_mode \
1357 (this_target_builtins->x_apply_args_mode)
1358 #define apply_result_mode \
1359 (this_target_builtins->x_apply_result_mode)
1361 /* Return the size required for the block returned by __builtin_apply_args,
1362 and initialize apply_args_mode. */
1364 static int
1365 apply_args_size (void)
1367 static int size = -1;
1368 int align;
1369 unsigned int regno;
1370 enum machine_mode mode;
1372 /* The values computed by this function never change. */
1373 if (size < 0)
1375 /* The first value is the incoming arg-pointer. */
1376 size = GET_MODE_SIZE (Pmode);
1378 /* The second value is the structure value address unless this is
1379 passed as an "invisible" first argument. */
1380 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1381 size += GET_MODE_SIZE (Pmode);
1383 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1384 if (FUNCTION_ARG_REGNO_P (regno))
1386 mode = targetm.calls.get_raw_arg_mode (regno);
1388 gcc_assert (mode != VOIDmode);
1390 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1391 if (size % align != 0)
1392 size = CEIL (size, align) * align;
1393 size += GET_MODE_SIZE (mode);
1394 apply_args_mode[regno] = mode;
1396 else
1398 apply_args_mode[regno] = VOIDmode;
1401 return size;
1404 /* Return the size required for the block returned by __builtin_apply,
1405 and initialize apply_result_mode. */
1407 static int
1408 apply_result_size (void)
1410 static int size = -1;
1411 int align, regno;
1412 enum machine_mode mode;
1414 /* The values computed by this function never change. */
1415 if (size < 0)
1417 size = 0;
1419 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1420 if (targetm.calls.function_value_regno_p (regno))
1422 mode = targetm.calls.get_raw_result_mode (regno);
1424 gcc_assert (mode != VOIDmode);
1426 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1427 if (size % align != 0)
1428 size = CEIL (size, align) * align;
1429 size += GET_MODE_SIZE (mode);
1430 apply_result_mode[regno] = mode;
1432 else
1433 apply_result_mode[regno] = VOIDmode;
1435 /* Allow targets that use untyped_call and untyped_return to override
1436 the size so that machine-specific information can be stored here. */
1437 #ifdef APPLY_RESULT_SIZE
1438 size = APPLY_RESULT_SIZE;
1439 #endif
1441 return size;
1444 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1445 /* Create a vector describing the result block RESULT. If SAVEP is true,
1446 the result block is used to save the values; otherwise it is used to
1447 restore the values. */
1449 static rtx
1450 result_vector (int savep, rtx result)
1452 int regno, size, align, nelts;
1453 enum machine_mode mode;
1454 rtx reg, mem;
1455 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1457 size = nelts = 0;
1458 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1459 if ((mode = apply_result_mode[regno]) != VOIDmode)
1461 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1462 if (size % align != 0)
1463 size = CEIL (size, align) * align;
1464 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1465 mem = adjust_address (result, mode, size);
1466 savevec[nelts++] = (savep
1467 ? gen_rtx_SET (VOIDmode, mem, reg)
1468 : gen_rtx_SET (VOIDmode, reg, mem));
1469 size += GET_MODE_SIZE (mode);
1471 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1473 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1475 /* Save the state required to perform an untyped call with the same
1476 arguments as were passed to the current function. */
1478 static rtx
1479 expand_builtin_apply_args_1 (void)
1481 rtx registers, tem;
1482 int size, align, regno;
1483 enum machine_mode mode;
1484 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1486 /* Create a block where the arg-pointer, structure value address,
1487 and argument registers can be saved. */
1488 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1490 /* Walk past the arg-pointer and structure value address. */
1491 size = GET_MODE_SIZE (Pmode);
1492 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1493 size += GET_MODE_SIZE (Pmode);
1495 /* Save each register used in calling a function to the block. */
1496 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1497 if ((mode = apply_args_mode[regno]) != VOIDmode)
1499 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1500 if (size % align != 0)
1501 size = CEIL (size, align) * align;
1503 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1505 emit_move_insn (adjust_address (registers, mode, size), tem);
1506 size += GET_MODE_SIZE (mode);
1509 /* Save the arg pointer to the block. */
1510 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1511 #ifdef STACK_GROWS_DOWNWARD
1512 /* We need the pointer as the caller actually passed them to us, not
1513 as we might have pretended they were passed. Make sure it's a valid
1514 operand, as emit_move_insn isn't expected to handle a PLUS. */
1516 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1517 NULL_RTX);
1518 #endif
1519 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1521 size = GET_MODE_SIZE (Pmode);
1523 /* Save the structure value address unless this is passed as an
1524 "invisible" first argument. */
1525 if (struct_incoming_value)
1527 emit_move_insn (adjust_address (registers, Pmode, size),
1528 copy_to_reg (struct_incoming_value));
1529 size += GET_MODE_SIZE (Pmode);
1532 /* Return the address of the block. */
1533 return copy_addr_to_reg (XEXP (registers, 0));
1536 /* __builtin_apply_args returns block of memory allocated on
1537 the stack into which is stored the arg pointer, structure
1538 value address, static chain, and all the registers that might
1539 possibly be used in performing a function call. The code is
1540 moved to the start of the function so the incoming values are
1541 saved. */
1543 static rtx
1544 expand_builtin_apply_args (void)
1546 /* Don't do __builtin_apply_args more than once in a function.
1547 Save the result of the first call and reuse it. */
1548 if (apply_args_value != 0)
1549 return apply_args_value;
1551 /* When this function is called, it means that registers must be
1552 saved on entry to this function. So we migrate the
1553 call to the first insn of this function. */
1554 rtx temp;
1555 rtx seq;
1557 start_sequence ();
1558 temp = expand_builtin_apply_args_1 ();
1559 seq = get_insns ();
1560 end_sequence ();
1562 apply_args_value = temp;
1564 /* Put the insns after the NOTE that starts the function.
1565 If this is inside a start_sequence, make the outer-level insn
1566 chain current, so the code is placed at the start of the
1567 function. If internal_arg_pointer is a non-virtual pseudo,
1568 it needs to be placed after the function that initializes
1569 that pseudo. */
1570 push_topmost_sequence ();
1571 if (REG_P (crtl->args.internal_arg_pointer)
1572 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1573 emit_insn_before (seq, parm_birth_insn);
1574 else
1575 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1576 pop_topmost_sequence ();
1577 return temp;
1581 /* Perform an untyped call and save the state required to perform an
1582 untyped return of whatever value was returned by the given function. */
1584 static rtx
1585 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1587 int size, align, regno;
1588 enum machine_mode mode;
1589 rtx incoming_args, result, reg, dest, src, call_insn;
1590 rtx old_stack_level = 0;
1591 rtx call_fusage = 0;
1592 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1594 arguments = convert_memory_address (Pmode, arguments);
1596 /* Create a block where the return registers can be saved. */
1597 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1599 /* Fetch the arg pointer from the ARGUMENTS block. */
1600 incoming_args = gen_reg_rtx (Pmode);
1601 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1602 #ifndef STACK_GROWS_DOWNWARD
1603 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1604 incoming_args, 0, OPTAB_LIB_WIDEN);
1605 #endif
1607 /* Push a new argument block and copy the arguments. Do not allow
1608 the (potential) memcpy call below to interfere with our stack
1609 manipulations. */
1610 do_pending_stack_adjust ();
1611 NO_DEFER_POP;
1613 /* Save the stack with nonlocal if available. */
1614 #ifdef HAVE_save_stack_nonlocal
1615 if (HAVE_save_stack_nonlocal)
1616 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1617 else
1618 #endif
1619 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1621 /* Allocate a block of memory onto the stack and copy the memory
1622 arguments to the outgoing arguments address. We can pass TRUE
1623 as the 4th argument because we just saved the stack pointer
1624 and will restore it right after the call. */
1625 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1627 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1628 may have already set current_function_calls_alloca to true.
1629 current_function_calls_alloca won't be set if argsize is zero,
1630 so we have to guarantee need_drap is true here. */
1631 if (SUPPORTS_STACK_ALIGNMENT)
1632 crtl->need_drap = true;
1634 dest = virtual_outgoing_args_rtx;
1635 #ifndef STACK_GROWS_DOWNWARD
1636 if (CONST_INT_P (argsize))
1637 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1638 else
1639 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1640 #endif
1641 dest = gen_rtx_MEM (BLKmode, dest);
1642 set_mem_align (dest, PARM_BOUNDARY);
1643 src = gen_rtx_MEM (BLKmode, incoming_args);
1644 set_mem_align (src, PARM_BOUNDARY);
1645 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1647 /* Refer to the argument block. */
1648 apply_args_size ();
1649 arguments = gen_rtx_MEM (BLKmode, arguments);
1650 set_mem_align (arguments, PARM_BOUNDARY);
1652 /* Walk past the arg-pointer and structure value address. */
1653 size = GET_MODE_SIZE (Pmode);
1654 if (struct_value)
1655 size += GET_MODE_SIZE (Pmode);
1657 /* Restore each of the registers previously saved. Make USE insns
1658 for each of these registers for use in making the call. */
1659 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1660 if ((mode = apply_args_mode[regno]) != VOIDmode)
1662 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1663 if (size % align != 0)
1664 size = CEIL (size, align) * align;
1665 reg = gen_rtx_REG (mode, regno);
1666 emit_move_insn (reg, adjust_address (arguments, mode, size));
1667 use_reg (&call_fusage, reg);
1668 size += GET_MODE_SIZE (mode);
1671 /* Restore the structure value address unless this is passed as an
1672 "invisible" first argument. */
1673 size = GET_MODE_SIZE (Pmode);
1674 if (struct_value)
1676 rtx value = gen_reg_rtx (Pmode);
1677 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1678 emit_move_insn (struct_value, value);
1679 if (REG_P (struct_value))
1680 use_reg (&call_fusage, struct_value);
1681 size += GET_MODE_SIZE (Pmode);
1684 /* All arguments and registers used for the call are set up by now! */
1685 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1687 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1688 and we don't want to load it into a register as an optimization,
1689 because prepare_call_address already did it if it should be done. */
1690 if (GET_CODE (function) != SYMBOL_REF)
1691 function = memory_address (FUNCTION_MODE, function);
1693 /* Generate the actual call instruction and save the return value. */
1694 #ifdef HAVE_untyped_call
1695 if (HAVE_untyped_call)
1696 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1697 result, result_vector (1, result)));
1698 else
1699 #endif
1700 #ifdef HAVE_call_value
1701 if (HAVE_call_value)
1703 rtx valreg = 0;
1705 /* Locate the unique return register. It is not possible to
1706 express a call that sets more than one return register using
1707 call_value; use untyped_call for that. In fact, untyped_call
1708 only needs to save the return registers in the given block. */
1709 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1710 if ((mode = apply_result_mode[regno]) != VOIDmode)
1712 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1714 valreg = gen_rtx_REG (mode, regno);
1717 emit_call_insn (GEN_CALL_VALUE (valreg,
1718 gen_rtx_MEM (FUNCTION_MODE, function),
1719 const0_rtx, NULL_RTX, const0_rtx));
1721 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1723 else
1724 #endif
1725 gcc_unreachable ();
1727 /* Find the CALL insn we just emitted, and attach the register usage
1728 information. */
1729 call_insn = last_call_insn ();
1730 add_function_usage_to (call_insn, call_fusage);
1732 /* Restore the stack. */
1733 #ifdef HAVE_save_stack_nonlocal
1734 if (HAVE_save_stack_nonlocal)
1735 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1736 else
1737 #endif
1738 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1739 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1741 OK_DEFER_POP;
1743 /* Return the address of the result block. */
1744 result = copy_addr_to_reg (XEXP (result, 0));
1745 return convert_memory_address (ptr_mode, result);
1748 /* Perform an untyped return. */
1750 static void
1751 expand_builtin_return (rtx result)
1753 int size, align, regno;
1754 enum machine_mode mode;
1755 rtx reg;
1756 rtx call_fusage = 0;
1758 result = convert_memory_address (Pmode, result);
1760 apply_result_size ();
1761 result = gen_rtx_MEM (BLKmode, result);
1763 #ifdef HAVE_untyped_return
1764 if (HAVE_untyped_return)
1766 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1767 emit_barrier ();
1768 return;
1770 #endif
1772 /* Restore the return value and note that each value is used. */
1773 size = 0;
1774 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1775 if ((mode = apply_result_mode[regno]) != VOIDmode)
1777 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1778 if (size % align != 0)
1779 size = CEIL (size, align) * align;
1780 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1781 emit_move_insn (reg, adjust_address (result, mode, size));
1783 push_to_sequence (call_fusage);
1784 emit_use (reg);
1785 call_fusage = get_insns ();
1786 end_sequence ();
1787 size += GET_MODE_SIZE (mode);
1790 /* Put the USE insns before the return. */
1791 emit_insn (call_fusage);
1793 /* Return whatever values was restored by jumping directly to the end
1794 of the function. */
1795 expand_naked_return ();
1798 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1800 static enum type_class
1801 type_to_class (tree type)
1803 switch (TREE_CODE (type))
1805 case VOID_TYPE: return void_type_class;
1806 case INTEGER_TYPE: return integer_type_class;
1807 case ENUMERAL_TYPE: return enumeral_type_class;
1808 case BOOLEAN_TYPE: return boolean_type_class;
1809 case POINTER_TYPE: return pointer_type_class;
1810 case REFERENCE_TYPE: return reference_type_class;
1811 case OFFSET_TYPE: return offset_type_class;
1812 case REAL_TYPE: return real_type_class;
1813 case COMPLEX_TYPE: return complex_type_class;
1814 case FUNCTION_TYPE: return function_type_class;
1815 case METHOD_TYPE: return method_type_class;
1816 case RECORD_TYPE: return record_type_class;
1817 case UNION_TYPE:
1818 case QUAL_UNION_TYPE: return union_type_class;
1819 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1820 ? string_type_class : array_type_class);
1821 case LANG_TYPE: return lang_type_class;
1822 default: return no_type_class;
1826 /* Expand a call EXP to __builtin_classify_type. */
1828 static rtx
1829 expand_builtin_classify_type (tree exp)
1831 if (call_expr_nargs (exp))
1832 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1833 return GEN_INT (no_type_class);
1836 /* This helper macro, meant to be used in mathfn_built_in below,
1837 determines which among a set of three builtin math functions is
1838 appropriate for a given type mode. The `F' and `L' cases are
1839 automatically generated from the `double' case. */
1840 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1841 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1842 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1843 fcodel = BUILT_IN_MATHFN##L ; break;
1844 /* Similar to above, but appends _R after any F/L suffix. */
1845 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1846 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1847 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1848 fcodel = BUILT_IN_MATHFN##L_R ; break;
1850 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1851 if available. If IMPLICIT is true use the implicit builtin declaration,
1852 otherwise use the explicit declaration. If we can't do the conversion,
1853 return zero. */
1855 static tree
1856 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1858 enum built_in_function fcode, fcodef, fcodel, fcode2;
1860 switch (fn)
1862 CASE_MATHFN (BUILT_IN_ACOS)
1863 CASE_MATHFN (BUILT_IN_ACOSH)
1864 CASE_MATHFN (BUILT_IN_ASIN)
1865 CASE_MATHFN (BUILT_IN_ASINH)
1866 CASE_MATHFN (BUILT_IN_ATAN)
1867 CASE_MATHFN (BUILT_IN_ATAN2)
1868 CASE_MATHFN (BUILT_IN_ATANH)
1869 CASE_MATHFN (BUILT_IN_CBRT)
1870 CASE_MATHFN (BUILT_IN_CEIL)
1871 CASE_MATHFN (BUILT_IN_CEXPI)
1872 CASE_MATHFN (BUILT_IN_COPYSIGN)
1873 CASE_MATHFN (BUILT_IN_COS)
1874 CASE_MATHFN (BUILT_IN_COSH)
1875 CASE_MATHFN (BUILT_IN_DREM)
1876 CASE_MATHFN (BUILT_IN_ERF)
1877 CASE_MATHFN (BUILT_IN_ERFC)
1878 CASE_MATHFN (BUILT_IN_EXP)
1879 CASE_MATHFN (BUILT_IN_EXP10)
1880 CASE_MATHFN (BUILT_IN_EXP2)
1881 CASE_MATHFN (BUILT_IN_EXPM1)
1882 CASE_MATHFN (BUILT_IN_FABS)
1883 CASE_MATHFN (BUILT_IN_FDIM)
1884 CASE_MATHFN (BUILT_IN_FLOOR)
1885 CASE_MATHFN (BUILT_IN_FMA)
1886 CASE_MATHFN (BUILT_IN_FMAX)
1887 CASE_MATHFN (BUILT_IN_FMIN)
1888 CASE_MATHFN (BUILT_IN_FMOD)
1889 CASE_MATHFN (BUILT_IN_FREXP)
1890 CASE_MATHFN (BUILT_IN_GAMMA)
1891 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1892 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1893 CASE_MATHFN (BUILT_IN_HYPOT)
1894 CASE_MATHFN (BUILT_IN_ILOGB)
1895 CASE_MATHFN (BUILT_IN_ICEIL)
1896 CASE_MATHFN (BUILT_IN_IFLOOR)
1897 CASE_MATHFN (BUILT_IN_INF)
1898 CASE_MATHFN (BUILT_IN_IRINT)
1899 CASE_MATHFN (BUILT_IN_IROUND)
1900 CASE_MATHFN (BUILT_IN_ISINF)
1901 CASE_MATHFN (BUILT_IN_J0)
1902 CASE_MATHFN (BUILT_IN_J1)
1903 CASE_MATHFN (BUILT_IN_JN)
1904 CASE_MATHFN (BUILT_IN_LCEIL)
1905 CASE_MATHFN (BUILT_IN_LDEXP)
1906 CASE_MATHFN (BUILT_IN_LFLOOR)
1907 CASE_MATHFN (BUILT_IN_LGAMMA)
1908 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1909 CASE_MATHFN (BUILT_IN_LLCEIL)
1910 CASE_MATHFN (BUILT_IN_LLFLOOR)
1911 CASE_MATHFN (BUILT_IN_LLRINT)
1912 CASE_MATHFN (BUILT_IN_LLROUND)
1913 CASE_MATHFN (BUILT_IN_LOG)
1914 CASE_MATHFN (BUILT_IN_LOG10)
1915 CASE_MATHFN (BUILT_IN_LOG1P)
1916 CASE_MATHFN (BUILT_IN_LOG2)
1917 CASE_MATHFN (BUILT_IN_LOGB)
1918 CASE_MATHFN (BUILT_IN_LRINT)
1919 CASE_MATHFN (BUILT_IN_LROUND)
1920 CASE_MATHFN (BUILT_IN_MODF)
1921 CASE_MATHFN (BUILT_IN_NAN)
1922 CASE_MATHFN (BUILT_IN_NANS)
1923 CASE_MATHFN (BUILT_IN_NEARBYINT)
1924 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1925 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1926 CASE_MATHFN (BUILT_IN_POW)
1927 CASE_MATHFN (BUILT_IN_POWI)
1928 CASE_MATHFN (BUILT_IN_POW10)
1929 CASE_MATHFN (BUILT_IN_REMAINDER)
1930 CASE_MATHFN (BUILT_IN_REMQUO)
1931 CASE_MATHFN (BUILT_IN_RINT)
1932 CASE_MATHFN (BUILT_IN_ROUND)
1933 CASE_MATHFN (BUILT_IN_SCALB)
1934 CASE_MATHFN (BUILT_IN_SCALBLN)
1935 CASE_MATHFN (BUILT_IN_SCALBN)
1936 CASE_MATHFN (BUILT_IN_SIGNBIT)
1937 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1938 CASE_MATHFN (BUILT_IN_SIN)
1939 CASE_MATHFN (BUILT_IN_SINCOS)
1940 CASE_MATHFN (BUILT_IN_SINH)
1941 CASE_MATHFN (BUILT_IN_SQRT)
1942 CASE_MATHFN (BUILT_IN_TAN)
1943 CASE_MATHFN (BUILT_IN_TANH)
1944 CASE_MATHFN (BUILT_IN_TGAMMA)
1945 CASE_MATHFN (BUILT_IN_TRUNC)
1946 CASE_MATHFN (BUILT_IN_Y0)
1947 CASE_MATHFN (BUILT_IN_Y1)
1948 CASE_MATHFN (BUILT_IN_YN)
1950 default:
1951 return NULL_TREE;
1954 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1955 fcode2 = fcode;
1956 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1957 fcode2 = fcodef;
1958 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1959 fcode2 = fcodel;
1960 else
1961 return NULL_TREE;
1963 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1964 return NULL_TREE;
1966 return builtin_decl_explicit (fcode2);
1969 /* Like mathfn_built_in_1(), but always use the implicit array. */
1971 tree
1972 mathfn_built_in (tree type, enum built_in_function fn)
1974 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1977 /* If errno must be maintained, expand the RTL to check if the result,
1978 TARGET, of a built-in function call, EXP, is NaN, and if so set
1979 errno to EDOM. */
1981 static void
1982 expand_errno_check (tree exp, rtx target)
1984 rtx lab = gen_label_rtx ();
1986 /* Test the result; if it is NaN, set errno=EDOM because
1987 the argument was not in the domain. */
1988 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1989 NULL_RTX, NULL_RTX, lab,
1990 /* The jump is very likely. */
1991 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1993 #ifdef TARGET_EDOM
1994 /* If this built-in doesn't throw an exception, set errno directly. */
1995 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1997 #ifdef GEN_ERRNO_RTX
1998 rtx errno_rtx = GEN_ERRNO_RTX;
1999 #else
2000 rtx errno_rtx
2001 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
2002 #endif
2003 emit_move_insn (errno_rtx,
2004 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
2005 emit_label (lab);
2006 return;
2008 #endif
2010 /* Make sure the library call isn't expanded as a tail call. */
2011 CALL_EXPR_TAILCALL (exp) = 0;
2013 /* We can't set errno=EDOM directly; let the library call do it.
2014 Pop the arguments right away in case the call gets deleted. */
2015 NO_DEFER_POP;
2016 expand_call (exp, target, 0);
2017 OK_DEFER_POP;
2018 emit_label (lab);
2021 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2022 Return NULL_RTX if a normal call should be emitted rather than expanding
2023 the function in-line. EXP is the expression that is a call to the builtin
2024 function; if convenient, the result should be placed in TARGET.
2025 SUBTARGET may be used as the target for computing one of EXP's operands. */
2027 static rtx
2028 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2030 optab builtin_optab;
2031 rtx op0, insns;
2032 tree fndecl = get_callee_fndecl (exp);
2033 enum machine_mode mode;
2034 bool errno_set = false;
2035 bool try_widening = false;
2036 tree arg;
2038 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2039 return NULL_RTX;
2041 arg = CALL_EXPR_ARG (exp, 0);
2043 switch (DECL_FUNCTION_CODE (fndecl))
2045 CASE_FLT_FN (BUILT_IN_SQRT):
2046 errno_set = ! tree_expr_nonnegative_p (arg);
2047 try_widening = true;
2048 builtin_optab = sqrt_optab;
2049 break;
2050 CASE_FLT_FN (BUILT_IN_EXP):
2051 errno_set = true; builtin_optab = exp_optab; break;
2052 CASE_FLT_FN (BUILT_IN_EXP10):
2053 CASE_FLT_FN (BUILT_IN_POW10):
2054 errno_set = true; builtin_optab = exp10_optab; break;
2055 CASE_FLT_FN (BUILT_IN_EXP2):
2056 errno_set = true; builtin_optab = exp2_optab; break;
2057 CASE_FLT_FN (BUILT_IN_EXPM1):
2058 errno_set = true; builtin_optab = expm1_optab; break;
2059 CASE_FLT_FN (BUILT_IN_LOGB):
2060 errno_set = true; builtin_optab = logb_optab; break;
2061 CASE_FLT_FN (BUILT_IN_LOG):
2062 errno_set = true; builtin_optab = log_optab; break;
2063 CASE_FLT_FN (BUILT_IN_LOG10):
2064 errno_set = true; builtin_optab = log10_optab; break;
2065 CASE_FLT_FN (BUILT_IN_LOG2):
2066 errno_set = true; builtin_optab = log2_optab; break;
2067 CASE_FLT_FN (BUILT_IN_LOG1P):
2068 errno_set = true; builtin_optab = log1p_optab; break;
2069 CASE_FLT_FN (BUILT_IN_ASIN):
2070 builtin_optab = asin_optab; break;
2071 CASE_FLT_FN (BUILT_IN_ACOS):
2072 builtin_optab = acos_optab; break;
2073 CASE_FLT_FN (BUILT_IN_TAN):
2074 builtin_optab = tan_optab; break;
2075 CASE_FLT_FN (BUILT_IN_ATAN):
2076 builtin_optab = atan_optab; break;
2077 CASE_FLT_FN (BUILT_IN_FLOOR):
2078 builtin_optab = floor_optab; break;
2079 CASE_FLT_FN (BUILT_IN_CEIL):
2080 builtin_optab = ceil_optab; break;
2081 CASE_FLT_FN (BUILT_IN_TRUNC):
2082 builtin_optab = btrunc_optab; break;
2083 CASE_FLT_FN (BUILT_IN_ROUND):
2084 builtin_optab = round_optab; break;
2085 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2086 builtin_optab = nearbyint_optab;
2087 if (flag_trapping_math)
2088 break;
2089 /* Else fallthrough and expand as rint. */
2090 CASE_FLT_FN (BUILT_IN_RINT):
2091 builtin_optab = rint_optab; break;
2092 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2093 builtin_optab = significand_optab; break;
2094 default:
2095 gcc_unreachable ();
2098 /* Make a suitable register to place result in. */
2099 mode = TYPE_MODE (TREE_TYPE (exp));
2101 if (! flag_errno_math || ! HONOR_NANS (mode))
2102 errno_set = false;
2104 /* Before working hard, check whether the instruction is available, but try
2105 to widen the mode for specific operations. */
2106 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2107 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2108 && (!errno_set || !optimize_insn_for_size_p ()))
2110 rtx result = gen_reg_rtx (mode);
2112 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2113 need to expand the argument again. This way, we will not perform
2114 side-effects more the once. */
2115 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2117 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2119 start_sequence ();
2121 /* Compute into RESULT.
2122 Set RESULT to wherever the result comes back. */
2123 result = expand_unop (mode, builtin_optab, op0, result, 0);
2125 if (result != 0)
2127 if (errno_set)
2128 expand_errno_check (exp, result);
2130 /* Output the entire sequence. */
2131 insns = get_insns ();
2132 end_sequence ();
2133 emit_insn (insns);
2134 return result;
2137 /* If we were unable to expand via the builtin, stop the sequence
2138 (without outputting the insns) and call to the library function
2139 with the stabilized argument list. */
2140 end_sequence ();
2143 return expand_call (exp, target, target == const0_rtx);
2146 /* Expand a call to the builtin binary math functions (pow and atan2).
2147 Return NULL_RTX if a normal call should be emitted rather than expanding the
2148 function in-line. EXP is the expression that is a call to the builtin
2149 function; if convenient, the result should be placed in TARGET.
2150 SUBTARGET may be used as the target for computing one of EXP's
2151 operands. */
2153 static rtx
2154 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2156 optab builtin_optab;
2157 rtx op0, op1, insns, result;
2158 int op1_type = REAL_TYPE;
2159 tree fndecl = get_callee_fndecl (exp);
2160 tree arg0, arg1;
2161 enum machine_mode mode;
2162 bool errno_set = true;
2164 switch (DECL_FUNCTION_CODE (fndecl))
2166 CASE_FLT_FN (BUILT_IN_SCALBN):
2167 CASE_FLT_FN (BUILT_IN_SCALBLN):
2168 CASE_FLT_FN (BUILT_IN_LDEXP):
2169 op1_type = INTEGER_TYPE;
2170 default:
2171 break;
2174 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2175 return NULL_RTX;
2177 arg0 = CALL_EXPR_ARG (exp, 0);
2178 arg1 = CALL_EXPR_ARG (exp, 1);
2180 switch (DECL_FUNCTION_CODE (fndecl))
2182 CASE_FLT_FN (BUILT_IN_POW):
2183 builtin_optab = pow_optab; break;
2184 CASE_FLT_FN (BUILT_IN_ATAN2):
2185 builtin_optab = atan2_optab; break;
2186 CASE_FLT_FN (BUILT_IN_SCALB):
2187 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2188 return 0;
2189 builtin_optab = scalb_optab; break;
2190 CASE_FLT_FN (BUILT_IN_SCALBN):
2191 CASE_FLT_FN (BUILT_IN_SCALBLN):
2192 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2193 return 0;
2194 /* Fall through... */
2195 CASE_FLT_FN (BUILT_IN_LDEXP):
2196 builtin_optab = ldexp_optab; break;
2197 CASE_FLT_FN (BUILT_IN_FMOD):
2198 builtin_optab = fmod_optab; break;
2199 CASE_FLT_FN (BUILT_IN_REMAINDER):
2200 CASE_FLT_FN (BUILT_IN_DREM):
2201 builtin_optab = remainder_optab; break;
2202 default:
2203 gcc_unreachable ();
2206 /* Make a suitable register to place result in. */
2207 mode = TYPE_MODE (TREE_TYPE (exp));
2209 /* Before working hard, check whether the instruction is available. */
2210 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2211 return NULL_RTX;
2213 result = gen_reg_rtx (mode);
2215 if (! flag_errno_math || ! HONOR_NANS (mode))
2216 errno_set = false;
2218 if (errno_set && optimize_insn_for_size_p ())
2219 return 0;
2221 /* Always stabilize the argument list. */
2222 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2223 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2225 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2226 op1 = expand_normal (arg1);
2228 start_sequence ();
2230 /* Compute into RESULT.
2231 Set RESULT to wherever the result comes back. */
2232 result = expand_binop (mode, builtin_optab, op0, op1,
2233 result, 0, OPTAB_DIRECT);
2235 /* If we were unable to expand via the builtin, stop the sequence
2236 (without outputting the insns) and call to the library function
2237 with the stabilized argument list. */
2238 if (result == 0)
2240 end_sequence ();
2241 return expand_call (exp, target, target == const0_rtx);
2244 if (errno_set)
2245 expand_errno_check (exp, result);
2247 /* Output the entire sequence. */
2248 insns = get_insns ();
2249 end_sequence ();
2250 emit_insn (insns);
2252 return result;
2255 /* Expand a call to the builtin trinary math functions (fma).
2256 Return NULL_RTX if a normal call should be emitted rather than expanding the
2257 function in-line. EXP is the expression that is a call to the builtin
2258 function; if convenient, the result should be placed in TARGET.
2259 SUBTARGET may be used as the target for computing one of EXP's
2260 operands. */
2262 static rtx
2263 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2265 optab builtin_optab;
2266 rtx op0, op1, op2, insns, result;
2267 tree fndecl = get_callee_fndecl (exp);
2268 tree arg0, arg1, arg2;
2269 enum machine_mode mode;
2271 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2272 return NULL_RTX;
2274 arg0 = CALL_EXPR_ARG (exp, 0);
2275 arg1 = CALL_EXPR_ARG (exp, 1);
2276 arg2 = CALL_EXPR_ARG (exp, 2);
2278 switch (DECL_FUNCTION_CODE (fndecl))
2280 CASE_FLT_FN (BUILT_IN_FMA):
2281 builtin_optab = fma_optab; break;
2282 default:
2283 gcc_unreachable ();
2286 /* Make a suitable register to place result in. */
2287 mode = TYPE_MODE (TREE_TYPE (exp));
2289 /* Before working hard, check whether the instruction is available. */
2290 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2291 return NULL_RTX;
2293 result = gen_reg_rtx (mode);
2295 /* Always stabilize the argument list. */
2296 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2297 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2298 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2300 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2301 op1 = expand_normal (arg1);
2302 op2 = expand_normal (arg2);
2304 start_sequence ();
2306 /* Compute into RESULT.
2307 Set RESULT to wherever the result comes back. */
2308 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2309 result, 0);
2311 /* If we were unable to expand via the builtin, stop the sequence
2312 (without outputting the insns) and call to the library function
2313 with the stabilized argument list. */
2314 if (result == 0)
2316 end_sequence ();
2317 return expand_call (exp, target, target == const0_rtx);
2320 /* Output the entire sequence. */
2321 insns = get_insns ();
2322 end_sequence ();
2323 emit_insn (insns);
2325 return result;
2328 /* Expand a call to the builtin sin and cos math functions.
2329 Return NULL_RTX if a normal call should be emitted rather than expanding the
2330 function in-line. EXP is the expression that is a call to the builtin
2331 function; if convenient, the result should be placed in TARGET.
2332 SUBTARGET may be used as the target for computing one of EXP's
2333 operands. */
2335 static rtx
2336 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2338 optab builtin_optab;
2339 rtx op0, insns;
2340 tree fndecl = get_callee_fndecl (exp);
2341 enum machine_mode mode;
2342 tree arg;
2344 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2345 return NULL_RTX;
2347 arg = CALL_EXPR_ARG (exp, 0);
2349 switch (DECL_FUNCTION_CODE (fndecl))
2351 CASE_FLT_FN (BUILT_IN_SIN):
2352 CASE_FLT_FN (BUILT_IN_COS):
2353 builtin_optab = sincos_optab; break;
2354 default:
2355 gcc_unreachable ();
2358 /* Make a suitable register to place result in. */
2359 mode = TYPE_MODE (TREE_TYPE (exp));
2361 /* Check if sincos insn is available, otherwise fallback
2362 to sin or cos insn. */
2363 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2364 switch (DECL_FUNCTION_CODE (fndecl))
2366 CASE_FLT_FN (BUILT_IN_SIN):
2367 builtin_optab = sin_optab; break;
2368 CASE_FLT_FN (BUILT_IN_COS):
2369 builtin_optab = cos_optab; break;
2370 default:
2371 gcc_unreachable ();
2374 /* Before working hard, check whether the instruction is available. */
2375 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2377 rtx result = gen_reg_rtx (mode);
2379 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2380 need to expand the argument again. This way, we will not perform
2381 side-effects more the once. */
2382 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2384 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2386 start_sequence ();
2388 /* Compute into RESULT.
2389 Set RESULT to wherever the result comes back. */
2390 if (builtin_optab == sincos_optab)
2392 int ok;
2394 switch (DECL_FUNCTION_CODE (fndecl))
2396 CASE_FLT_FN (BUILT_IN_SIN):
2397 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2398 break;
2399 CASE_FLT_FN (BUILT_IN_COS):
2400 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2401 break;
2402 default:
2403 gcc_unreachable ();
2405 gcc_assert (ok);
2407 else
2408 result = expand_unop (mode, builtin_optab, op0, result, 0);
2410 if (result != 0)
2412 /* Output the entire sequence. */
2413 insns = get_insns ();
2414 end_sequence ();
2415 emit_insn (insns);
2416 return result;
2419 /* If we were unable to expand via the builtin, stop the sequence
2420 (without outputting the insns) and call to the library function
2421 with the stabilized argument list. */
2422 end_sequence ();
2425 return expand_call (exp, target, target == const0_rtx);
2428 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2429 return an RTL instruction code that implements the functionality.
2430 If that isn't possible or available return CODE_FOR_nothing. */
2432 static enum insn_code
2433 interclass_mathfn_icode (tree arg, tree fndecl)
2435 bool errno_set = false;
2436 optab builtin_optab = unknown_optab;
2437 enum machine_mode mode;
2439 switch (DECL_FUNCTION_CODE (fndecl))
2441 CASE_FLT_FN (BUILT_IN_ILOGB):
2442 errno_set = true; builtin_optab = ilogb_optab; break;
2443 CASE_FLT_FN (BUILT_IN_ISINF):
2444 builtin_optab = isinf_optab; break;
2445 case BUILT_IN_ISNORMAL:
2446 case BUILT_IN_ISFINITE:
2447 CASE_FLT_FN (BUILT_IN_FINITE):
2448 case BUILT_IN_FINITED32:
2449 case BUILT_IN_FINITED64:
2450 case BUILT_IN_FINITED128:
2451 case BUILT_IN_ISINFD32:
2452 case BUILT_IN_ISINFD64:
2453 case BUILT_IN_ISINFD128:
2454 /* These builtins have no optabs (yet). */
2455 break;
2456 default:
2457 gcc_unreachable ();
2460 /* There's no easy way to detect the case we need to set EDOM. */
2461 if (flag_errno_math && errno_set)
2462 return CODE_FOR_nothing;
2464 /* Optab mode depends on the mode of the input argument. */
2465 mode = TYPE_MODE (TREE_TYPE (arg));
2467 if (builtin_optab)
2468 return optab_handler (builtin_optab, mode);
2469 return CODE_FOR_nothing;
2472 /* Expand a call to one of the builtin math functions that operate on
2473 floating point argument and output an integer result (ilogb, isinf,
2474 isnan, etc).
2475 Return 0 if a normal call should be emitted rather than expanding the
2476 function in-line. EXP is the expression that is a call to the builtin
2477 function; if convenient, the result should be placed in TARGET. */
2479 static rtx
2480 expand_builtin_interclass_mathfn (tree exp, rtx target)
2482 enum insn_code icode = CODE_FOR_nothing;
2483 rtx op0;
2484 tree fndecl = get_callee_fndecl (exp);
2485 enum machine_mode mode;
2486 tree arg;
2488 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2489 return NULL_RTX;
2491 arg = CALL_EXPR_ARG (exp, 0);
2492 icode = interclass_mathfn_icode (arg, fndecl);
2493 mode = TYPE_MODE (TREE_TYPE (arg));
2495 if (icode != CODE_FOR_nothing)
2497 struct expand_operand ops[1];
2498 rtx last = get_last_insn ();
2499 tree orig_arg = arg;
2501 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2502 need to expand the argument again. This way, we will not perform
2503 side-effects more the once. */
2504 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2506 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2508 if (mode != GET_MODE (op0))
2509 op0 = convert_to_mode (mode, op0, 0);
2511 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2512 if (maybe_legitimize_operands (icode, 0, 1, ops)
2513 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2514 return ops[0].value;
2516 delete_insns_since (last);
2517 CALL_EXPR_ARG (exp, 0) = orig_arg;
2520 return NULL_RTX;
2523 /* Expand a call to the builtin sincos math function.
2524 Return NULL_RTX if a normal call should be emitted rather than expanding the
2525 function in-line. EXP is the expression that is a call to the builtin
2526 function. */
2528 static rtx
2529 expand_builtin_sincos (tree exp)
2531 rtx op0, op1, op2, target1, target2;
2532 enum machine_mode mode;
2533 tree arg, sinp, cosp;
2534 int result;
2535 location_t loc = EXPR_LOCATION (exp);
2536 tree alias_type, alias_off;
2538 if (!validate_arglist (exp, REAL_TYPE,
2539 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2540 return NULL_RTX;
2542 arg = CALL_EXPR_ARG (exp, 0);
2543 sinp = CALL_EXPR_ARG (exp, 1);
2544 cosp = CALL_EXPR_ARG (exp, 2);
2546 /* Make a suitable register to place result in. */
2547 mode = TYPE_MODE (TREE_TYPE (arg));
2549 /* Check if sincos insn is available, otherwise emit the call. */
2550 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2551 return NULL_RTX;
2553 target1 = gen_reg_rtx (mode);
2554 target2 = gen_reg_rtx (mode);
2556 op0 = expand_normal (arg);
2557 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2558 alias_off = build_int_cst (alias_type, 0);
2559 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2560 sinp, alias_off));
2561 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2562 cosp, alias_off));
2564 /* Compute into target1 and target2.
2565 Set TARGET to wherever the result comes back. */
2566 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2567 gcc_assert (result);
2569 /* Move target1 and target2 to the memory locations indicated
2570 by op1 and op2. */
2571 emit_move_insn (op1, target1);
2572 emit_move_insn (op2, target2);
2574 return const0_rtx;
2577 /* Expand a call to the internal cexpi builtin to the sincos math function.
2578 EXP is the expression that is a call to the builtin function; if convenient,
2579 the result should be placed in TARGET. */
2581 static rtx
2582 expand_builtin_cexpi (tree exp, rtx target)
2584 tree fndecl = get_callee_fndecl (exp);
2585 tree arg, type;
2586 enum machine_mode mode;
2587 rtx op0, op1, op2;
2588 location_t loc = EXPR_LOCATION (exp);
2590 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2591 return NULL_RTX;
2593 arg = CALL_EXPR_ARG (exp, 0);
2594 type = TREE_TYPE (arg);
2595 mode = TYPE_MODE (TREE_TYPE (arg));
2597 /* Try expanding via a sincos optab, fall back to emitting a libcall
2598 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2599 is only generated from sincos, cexp or if we have either of them. */
2600 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2602 op1 = gen_reg_rtx (mode);
2603 op2 = gen_reg_rtx (mode);
2605 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2607 /* Compute into op1 and op2. */
2608 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2610 else if (targetm.libc_has_function (function_sincos))
2612 tree call, fn = NULL_TREE;
2613 tree top1, top2;
2614 rtx op1a, op2a;
2616 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2617 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2618 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2619 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2620 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2621 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2622 else
2623 gcc_unreachable ();
2625 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2626 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2627 op1a = copy_addr_to_reg (XEXP (op1, 0));
2628 op2a = copy_addr_to_reg (XEXP (op2, 0));
2629 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2630 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2632 /* Make sure not to fold the sincos call again. */
2633 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2634 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2635 call, 3, arg, top1, top2));
2637 else
2639 tree call, fn = NULL_TREE, narg;
2640 tree ctype = build_complex_type (type);
2642 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2643 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2644 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2645 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2646 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2647 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2648 else
2649 gcc_unreachable ();
2651 /* If we don't have a decl for cexp create one. This is the
2652 friendliest fallback if the user calls __builtin_cexpi
2653 without full target C99 function support. */
2654 if (fn == NULL_TREE)
2656 tree fntype;
2657 const char *name = NULL;
2659 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2660 name = "cexpf";
2661 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2662 name = "cexp";
2663 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2664 name = "cexpl";
2666 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2667 fn = build_fn_decl (name, fntype);
2670 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2671 build_real (type, dconst0), arg);
2673 /* Make sure not to fold the cexp call again. */
2674 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2675 return expand_expr (build_call_nary (ctype, call, 1, narg),
2676 target, VOIDmode, EXPAND_NORMAL);
2679 /* Now build the proper return type. */
2680 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2681 make_tree (TREE_TYPE (arg), op2),
2682 make_tree (TREE_TYPE (arg), op1)),
2683 target, VOIDmode, EXPAND_NORMAL);
2686 /* Conveniently construct a function call expression. FNDECL names the
2687 function to be called, N is the number of arguments, and the "..."
2688 parameters are the argument expressions. Unlike build_call_exr
2689 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2691 static tree
2692 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2694 va_list ap;
2695 tree fntype = TREE_TYPE (fndecl);
2696 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2698 va_start (ap, n);
2699 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2700 va_end (ap);
2701 SET_EXPR_LOCATION (fn, loc);
2702 return fn;
2705 /* Expand a call to one of the builtin rounding functions gcc defines
2706 as an extension (lfloor and lceil). As these are gcc extensions we
2707 do not need to worry about setting errno to EDOM.
2708 If expanding via optab fails, lower expression to (int)(floor(x)).
2709 EXP is the expression that is a call to the builtin function;
2710 if convenient, the result should be placed in TARGET. */
2712 static rtx
2713 expand_builtin_int_roundingfn (tree exp, rtx target)
2715 convert_optab builtin_optab;
2716 rtx op0, insns, tmp;
2717 tree fndecl = get_callee_fndecl (exp);
2718 enum built_in_function fallback_fn;
2719 tree fallback_fndecl;
2720 enum machine_mode mode;
2721 tree arg;
2723 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2724 gcc_unreachable ();
2726 arg = CALL_EXPR_ARG (exp, 0);
2728 switch (DECL_FUNCTION_CODE (fndecl))
2730 CASE_FLT_FN (BUILT_IN_ICEIL):
2731 CASE_FLT_FN (BUILT_IN_LCEIL):
2732 CASE_FLT_FN (BUILT_IN_LLCEIL):
2733 builtin_optab = lceil_optab;
2734 fallback_fn = BUILT_IN_CEIL;
2735 break;
2737 CASE_FLT_FN (BUILT_IN_IFLOOR):
2738 CASE_FLT_FN (BUILT_IN_LFLOOR):
2739 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2740 builtin_optab = lfloor_optab;
2741 fallback_fn = BUILT_IN_FLOOR;
2742 break;
2744 default:
2745 gcc_unreachable ();
2748 /* Make a suitable register to place result in. */
2749 mode = TYPE_MODE (TREE_TYPE (exp));
2751 target = gen_reg_rtx (mode);
2753 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2754 need to expand the argument again. This way, we will not perform
2755 side-effects more the once. */
2756 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2758 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2760 start_sequence ();
2762 /* Compute into TARGET. */
2763 if (expand_sfix_optab (target, op0, builtin_optab))
2765 /* Output the entire sequence. */
2766 insns = get_insns ();
2767 end_sequence ();
2768 emit_insn (insns);
2769 return target;
2772 /* If we were unable to expand via the builtin, stop the sequence
2773 (without outputting the insns). */
2774 end_sequence ();
2776 /* Fall back to floating point rounding optab. */
2777 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2779 /* For non-C99 targets we may end up without a fallback fndecl here
2780 if the user called __builtin_lfloor directly. In this case emit
2781 a call to the floor/ceil variants nevertheless. This should result
2782 in the best user experience for not full C99 targets. */
2783 if (fallback_fndecl == NULL_TREE)
2785 tree fntype;
2786 const char *name = NULL;
2788 switch (DECL_FUNCTION_CODE (fndecl))
2790 case BUILT_IN_ICEIL:
2791 case BUILT_IN_LCEIL:
2792 case BUILT_IN_LLCEIL:
2793 name = "ceil";
2794 break;
2795 case BUILT_IN_ICEILF:
2796 case BUILT_IN_LCEILF:
2797 case BUILT_IN_LLCEILF:
2798 name = "ceilf";
2799 break;
2800 case BUILT_IN_ICEILL:
2801 case BUILT_IN_LCEILL:
2802 case BUILT_IN_LLCEILL:
2803 name = "ceill";
2804 break;
2805 case BUILT_IN_IFLOOR:
2806 case BUILT_IN_LFLOOR:
2807 case BUILT_IN_LLFLOOR:
2808 name = "floor";
2809 break;
2810 case BUILT_IN_IFLOORF:
2811 case BUILT_IN_LFLOORF:
2812 case BUILT_IN_LLFLOORF:
2813 name = "floorf";
2814 break;
2815 case BUILT_IN_IFLOORL:
2816 case BUILT_IN_LFLOORL:
2817 case BUILT_IN_LLFLOORL:
2818 name = "floorl";
2819 break;
2820 default:
2821 gcc_unreachable ();
2824 fntype = build_function_type_list (TREE_TYPE (arg),
2825 TREE_TYPE (arg), NULL_TREE);
2826 fallback_fndecl = build_fn_decl (name, fntype);
2829 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2831 tmp = expand_normal (exp);
2832 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2834 /* Truncate the result of floating point optab to integer
2835 via expand_fix (). */
2836 target = gen_reg_rtx (mode);
2837 expand_fix (target, tmp, 0);
2839 return target;
2842 /* Expand a call to one of the builtin math functions doing integer
2843 conversion (lrint).
2844 Return 0 if a normal call should be emitted rather than expanding the
2845 function in-line. EXP is the expression that is a call to the builtin
2846 function; if convenient, the result should be placed in TARGET. */
2848 static rtx
2849 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2851 convert_optab builtin_optab;
2852 rtx op0, insns;
2853 tree fndecl = get_callee_fndecl (exp);
2854 tree arg;
2855 enum machine_mode mode;
2856 enum built_in_function fallback_fn = BUILT_IN_NONE;
2858 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2859 gcc_unreachable ();
2861 arg = CALL_EXPR_ARG (exp, 0);
2863 switch (DECL_FUNCTION_CODE (fndecl))
2865 CASE_FLT_FN (BUILT_IN_IRINT):
2866 fallback_fn = BUILT_IN_LRINT;
2867 /* FALLTHRU */
2868 CASE_FLT_FN (BUILT_IN_LRINT):
2869 CASE_FLT_FN (BUILT_IN_LLRINT):
2870 builtin_optab = lrint_optab;
2871 break;
2873 CASE_FLT_FN (BUILT_IN_IROUND):
2874 fallback_fn = BUILT_IN_LROUND;
2875 /* FALLTHRU */
2876 CASE_FLT_FN (BUILT_IN_LROUND):
2877 CASE_FLT_FN (BUILT_IN_LLROUND):
2878 builtin_optab = lround_optab;
2879 break;
2881 default:
2882 gcc_unreachable ();
2885 /* There's no easy way to detect the case we need to set EDOM. */
2886 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2887 return NULL_RTX;
2889 /* Make a suitable register to place result in. */
2890 mode = TYPE_MODE (TREE_TYPE (exp));
2892 /* There's no easy way to detect the case we need to set EDOM. */
2893 if (!flag_errno_math)
2895 rtx result = gen_reg_rtx (mode);
2897 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2898 need to expand the argument again. This way, we will not perform
2899 side-effects more the once. */
2900 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2902 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2904 start_sequence ();
2906 if (expand_sfix_optab (result, op0, builtin_optab))
2908 /* Output the entire sequence. */
2909 insns = get_insns ();
2910 end_sequence ();
2911 emit_insn (insns);
2912 return result;
2915 /* If we were unable to expand via the builtin, stop the sequence
2916 (without outputting the insns) and call to the library function
2917 with the stabilized argument list. */
2918 end_sequence ();
2921 if (fallback_fn != BUILT_IN_NONE)
2923 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2924 targets, (int) round (x) should never be transformed into
2925 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2926 a call to lround in the hope that the target provides at least some
2927 C99 functions. This should result in the best user experience for
2928 not full C99 targets. */
2929 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2930 fallback_fn, 0);
2932 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2933 fallback_fndecl, 1, arg);
2935 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2936 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2937 return convert_to_mode (mode, target, 0);
2940 return expand_call (exp, target, target == const0_rtx);
2943 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2944 a normal call should be emitted rather than expanding the function
2945 in-line. EXP is the expression that is a call to the builtin
2946 function; if convenient, the result should be placed in TARGET. */
2948 static rtx
2949 expand_builtin_powi (tree exp, rtx target)
2951 tree arg0, arg1;
2952 rtx op0, op1;
2953 enum machine_mode mode;
2954 enum machine_mode mode2;
2956 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2957 return NULL_RTX;
2959 arg0 = CALL_EXPR_ARG (exp, 0);
2960 arg1 = CALL_EXPR_ARG (exp, 1);
2961 mode = TYPE_MODE (TREE_TYPE (exp));
2963 /* Emit a libcall to libgcc. */
2965 /* Mode of the 2nd argument must match that of an int. */
2966 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2968 if (target == NULL_RTX)
2969 target = gen_reg_rtx (mode);
2971 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2972 if (GET_MODE (op0) != mode)
2973 op0 = convert_to_mode (mode, op0, 0);
2974 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2975 if (GET_MODE (op1) != mode2)
2976 op1 = convert_to_mode (mode2, op1, 0);
2978 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2979 target, LCT_CONST, mode, 2,
2980 op0, mode, op1, mode2);
2982 return target;
2985 /* Expand expression EXP which is a call to the strlen builtin. Return
2986 NULL_RTX if we failed the caller should emit a normal call, otherwise
2987 try to get the result in TARGET, if convenient. */
2989 static rtx
2990 expand_builtin_strlen (tree exp, rtx target,
2991 enum machine_mode target_mode)
2993 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2994 return NULL_RTX;
2995 else
2997 struct expand_operand ops[4];
2998 rtx pat;
2999 tree len;
3000 tree src = CALL_EXPR_ARG (exp, 0);
3001 rtx src_reg, before_strlen;
3002 enum machine_mode insn_mode = target_mode;
3003 enum insn_code icode = CODE_FOR_nothing;
3004 unsigned int align;
3006 /* If the length can be computed at compile-time, return it. */
3007 len = c_strlen (src, 0);
3008 if (len)
3009 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3011 /* If the length can be computed at compile-time and is constant
3012 integer, but there are side-effects in src, evaluate
3013 src for side-effects, then return len.
3014 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3015 can be optimized into: i++; x = 3; */
3016 len = c_strlen (src, 1);
3017 if (len && TREE_CODE (len) == INTEGER_CST)
3019 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3020 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3023 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3025 /* If SRC is not a pointer type, don't do this operation inline. */
3026 if (align == 0)
3027 return NULL_RTX;
3029 /* Bail out if we can't compute strlen in the right mode. */
3030 while (insn_mode != VOIDmode)
3032 icode = optab_handler (strlen_optab, insn_mode);
3033 if (icode != CODE_FOR_nothing)
3034 break;
3036 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3038 if (insn_mode == VOIDmode)
3039 return NULL_RTX;
3041 /* Make a place to hold the source address. We will not expand
3042 the actual source until we are sure that the expansion will
3043 not fail -- there are trees that cannot be expanded twice. */
3044 src_reg = gen_reg_rtx (Pmode);
3046 /* Mark the beginning of the strlen sequence so we can emit the
3047 source operand later. */
3048 before_strlen = get_last_insn ();
3050 create_output_operand (&ops[0], target, insn_mode);
3051 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3052 create_integer_operand (&ops[2], 0);
3053 create_integer_operand (&ops[3], align);
3054 if (!maybe_expand_insn (icode, 4, ops))
3055 return NULL_RTX;
3057 /* Now that we are assured of success, expand the source. */
3058 start_sequence ();
3059 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3060 if (pat != src_reg)
3062 #ifdef POINTERS_EXTEND_UNSIGNED
3063 if (GET_MODE (pat) != Pmode)
3064 pat = convert_to_mode (Pmode, pat,
3065 POINTERS_EXTEND_UNSIGNED);
3066 #endif
3067 emit_move_insn (src_reg, pat);
3069 pat = get_insns ();
3070 end_sequence ();
3072 if (before_strlen)
3073 emit_insn_after (pat, before_strlen);
3074 else
3075 emit_insn_before (pat, get_insns ());
3077 /* Return the value in the proper mode for this function. */
3078 if (GET_MODE (ops[0].value) == target_mode)
3079 target = ops[0].value;
3080 else if (target != 0)
3081 convert_move (target, ops[0].value, 0);
3082 else
3083 target = convert_to_mode (target_mode, ops[0].value, 0);
3085 return target;
3089 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3090 bytes from constant string DATA + OFFSET and return it as target
3091 constant. */
3093 static rtx
3094 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3095 enum machine_mode mode)
3097 const char *str = (const char *) data;
3099 gcc_assert (offset >= 0
3100 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3101 <= strlen (str) + 1));
3103 return c_readstr (str + offset, mode);
3106 /* LEN specify length of the block of memcpy/memset operation.
3107 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3108 In some cases we can make very likely guess on max size, then we
3109 set it into PROBABLE_MAX_SIZE. */
3111 static void
3112 determine_block_size (tree len, rtx len_rtx,
3113 unsigned HOST_WIDE_INT *min_size,
3114 unsigned HOST_WIDE_INT *max_size,
3115 unsigned HOST_WIDE_INT *probable_max_size)
3117 if (CONST_INT_P (len_rtx))
3119 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3120 return;
3122 else
3124 double_int min, max;
3125 enum value_range_type range_type = VR_UNDEFINED;
3127 /* Determine bounds from the type. */
3128 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3129 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3130 else
3131 *min_size = 0;
3132 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3133 *probable_max_size = *max_size
3134 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3135 else
3136 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3138 if (TREE_CODE (len) == SSA_NAME)
3139 range_type = get_range_info (len, &min, &max);
3140 if (range_type == VR_RANGE)
3142 if (min.fits_uhwi () && *min_size < min.to_uhwi ())
3143 *min_size = min.to_uhwi ();
3144 if (max.fits_uhwi () && *max_size > max.to_uhwi ())
3145 *probable_max_size = *max_size = max.to_uhwi ();
3147 else if (range_type == VR_ANTI_RANGE)
3149 /* Anti range 0...N lets us to determine minimal size to N+1. */
3150 if (min.is_zero ())
3152 if ((max + double_int_one).fits_uhwi ())
3153 *min_size = (max + double_int_one).to_uhwi ();
3155 /* Code like
3157 int n;
3158 if (n < 100)
3159 memcpy (a, b, n)
3161 Produce anti range allowing negative values of N. We still
3162 can use the information and make a guess that N is not negative.
3164 else if (!max.ule (double_int_one.lshift (30))
3165 && min.fits_uhwi ())
3166 *probable_max_size = min.to_uhwi () - 1;
3169 gcc_checking_assert (*max_size <=
3170 (unsigned HOST_WIDE_INT)
3171 GET_MODE_MASK (GET_MODE (len_rtx)));
3174 /* Expand a call EXP to the memcpy builtin.
3175 Return NULL_RTX if we failed, the caller should emit a normal call,
3176 otherwise try to get the result in TARGET, if convenient (and in
3177 mode MODE if that's convenient). */
3179 static rtx
3180 expand_builtin_memcpy (tree exp, rtx target)
3182 if (!validate_arglist (exp,
3183 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3184 return NULL_RTX;
3185 else
3187 tree dest = CALL_EXPR_ARG (exp, 0);
3188 tree src = CALL_EXPR_ARG (exp, 1);
3189 tree len = CALL_EXPR_ARG (exp, 2);
3190 const char *src_str;
3191 unsigned int src_align = get_pointer_alignment (src);
3192 unsigned int dest_align = get_pointer_alignment (dest);
3193 rtx dest_mem, src_mem, dest_addr, len_rtx;
3194 HOST_WIDE_INT expected_size = -1;
3195 unsigned int expected_align = 0;
3196 unsigned HOST_WIDE_INT min_size;
3197 unsigned HOST_WIDE_INT max_size;
3198 unsigned HOST_WIDE_INT probable_max_size;
3200 /* If DEST is not a pointer type, call the normal function. */
3201 if (dest_align == 0)
3202 return NULL_RTX;
3204 /* If either SRC is not a pointer type, don't do this
3205 operation in-line. */
3206 if (src_align == 0)
3207 return NULL_RTX;
3209 if (currently_expanding_gimple_stmt)
3210 stringop_block_profile (currently_expanding_gimple_stmt,
3211 &expected_align, &expected_size);
3213 if (expected_align < dest_align)
3214 expected_align = dest_align;
3215 dest_mem = get_memory_rtx (dest, len);
3216 set_mem_align (dest_mem, dest_align);
3217 len_rtx = expand_normal (len);
3218 determine_block_size (len, len_rtx, &min_size, &max_size,
3219 &probable_max_size);
3220 src_str = c_getstr (src);
3222 /* If SRC is a string constant and block move would be done
3223 by pieces, we can avoid loading the string from memory
3224 and only stored the computed constants. */
3225 if (src_str
3226 && CONST_INT_P (len_rtx)
3227 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3228 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3229 CONST_CAST (char *, src_str),
3230 dest_align, false))
3232 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3233 builtin_memcpy_read_str,
3234 CONST_CAST (char *, src_str),
3235 dest_align, false, 0);
3236 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3237 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3238 return dest_mem;
3241 src_mem = get_memory_rtx (src, len);
3242 set_mem_align (src_mem, src_align);
3244 /* Copy word part most expediently. */
3245 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3246 CALL_EXPR_TAILCALL (exp)
3247 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3248 expected_align, expected_size,
3249 min_size, max_size, probable_max_size);
3251 if (dest_addr == 0)
3253 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3254 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3256 return dest_addr;
3260 /* Expand a call EXP to the mempcpy builtin.
3261 Return NULL_RTX if we failed; the caller should emit a normal call,
3262 otherwise try to get the result in TARGET, if convenient (and in
3263 mode MODE if that's convenient). If ENDP is 0 return the
3264 destination pointer, if ENDP is 1 return the end pointer ala
3265 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3266 stpcpy. */
3268 static rtx
3269 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3271 if (!validate_arglist (exp,
3272 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3273 return NULL_RTX;
3274 else
3276 tree dest = CALL_EXPR_ARG (exp, 0);
3277 tree src = CALL_EXPR_ARG (exp, 1);
3278 tree len = CALL_EXPR_ARG (exp, 2);
3279 return expand_builtin_mempcpy_args (dest, src, len,
3280 target, mode, /*endp=*/ 1);
3284 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3285 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3286 so that this can also be called without constructing an actual CALL_EXPR.
3287 The other arguments and return value are the same as for
3288 expand_builtin_mempcpy. */
3290 static rtx
3291 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3292 rtx target, enum machine_mode mode, int endp)
3294 /* If return value is ignored, transform mempcpy into memcpy. */
3295 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3297 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3298 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3299 dest, src, len);
3300 return expand_expr (result, target, mode, EXPAND_NORMAL);
3302 else
3304 const char *src_str;
3305 unsigned int src_align = get_pointer_alignment (src);
3306 unsigned int dest_align = get_pointer_alignment (dest);
3307 rtx dest_mem, src_mem, len_rtx;
3309 /* If either SRC or DEST is not a pointer type, don't do this
3310 operation in-line. */
3311 if (dest_align == 0 || src_align == 0)
3312 return NULL_RTX;
3314 /* If LEN is not constant, call the normal function. */
3315 if (! tree_fits_uhwi_p (len))
3316 return NULL_RTX;
3318 len_rtx = expand_normal (len);
3319 src_str = c_getstr (src);
3321 /* If SRC is a string constant and block move would be done
3322 by pieces, we can avoid loading the string from memory
3323 and only stored the computed constants. */
3324 if (src_str
3325 && CONST_INT_P (len_rtx)
3326 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3327 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3328 CONST_CAST (char *, src_str),
3329 dest_align, false))
3331 dest_mem = get_memory_rtx (dest, len);
3332 set_mem_align (dest_mem, dest_align);
3333 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3334 builtin_memcpy_read_str,
3335 CONST_CAST (char *, src_str),
3336 dest_align, false, endp);
3337 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3338 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3339 return dest_mem;
3342 if (CONST_INT_P (len_rtx)
3343 && can_move_by_pieces (INTVAL (len_rtx),
3344 MIN (dest_align, src_align)))
3346 dest_mem = get_memory_rtx (dest, len);
3347 set_mem_align (dest_mem, dest_align);
3348 src_mem = get_memory_rtx (src, len);
3349 set_mem_align (src_mem, src_align);
3350 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3351 MIN (dest_align, src_align), endp);
3352 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3353 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3354 return dest_mem;
3357 return NULL_RTX;
3361 #ifndef HAVE_movstr
3362 # define HAVE_movstr 0
3363 # define CODE_FOR_movstr CODE_FOR_nothing
3364 #endif
3366 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3367 we failed, the caller should emit a normal call, otherwise try to
3368 get the result in TARGET, if convenient. If ENDP is 0 return the
3369 destination pointer, if ENDP is 1 return the end pointer ala
3370 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3371 stpcpy. */
3373 static rtx
3374 expand_movstr (tree dest, tree src, rtx target, int endp)
3376 struct expand_operand ops[3];
3377 rtx dest_mem;
3378 rtx src_mem;
3380 if (!HAVE_movstr)
3381 return NULL_RTX;
3383 dest_mem = get_memory_rtx (dest, NULL);
3384 src_mem = get_memory_rtx (src, NULL);
3385 if (!endp)
3387 target = force_reg (Pmode, XEXP (dest_mem, 0));
3388 dest_mem = replace_equiv_address (dest_mem, target);
3391 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3392 create_fixed_operand (&ops[1], dest_mem);
3393 create_fixed_operand (&ops[2], src_mem);
3394 if (!maybe_expand_insn (CODE_FOR_movstr, 3, ops))
3395 return NULL_RTX;
3397 if (endp && target != const0_rtx)
3399 target = ops[0].value;
3400 /* movstr is supposed to set end to the address of the NUL
3401 terminator. If the caller requested a mempcpy-like return value,
3402 adjust it. */
3403 if (endp == 1)
3405 rtx tem = plus_constant (GET_MODE (target),
3406 gen_lowpart (GET_MODE (target), target), 1);
3407 emit_move_insn (target, force_operand (tem, NULL_RTX));
3410 return target;
3413 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3414 NULL_RTX if we failed the caller should emit a normal call, otherwise
3415 try to get the result in TARGET, if convenient (and in mode MODE if that's
3416 convenient). */
3418 static rtx
3419 expand_builtin_strcpy (tree exp, rtx target)
3421 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3423 tree dest = CALL_EXPR_ARG (exp, 0);
3424 tree src = CALL_EXPR_ARG (exp, 1);
3425 return expand_builtin_strcpy_args (dest, src, target);
3427 return NULL_RTX;
3430 /* Helper function to do the actual work for expand_builtin_strcpy. The
3431 arguments to the builtin_strcpy call DEST and SRC are broken out
3432 so that this can also be called without constructing an actual CALL_EXPR.
3433 The other arguments and return value are the same as for
3434 expand_builtin_strcpy. */
3436 static rtx
3437 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3439 return expand_movstr (dest, src, target, /*endp=*/0);
3442 /* Expand a call EXP to the stpcpy builtin.
3443 Return NULL_RTX if we failed the caller should emit a normal call,
3444 otherwise try to get the result in TARGET, if convenient (and in
3445 mode MODE if that's convenient). */
3447 static rtx
3448 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3450 tree dst, src;
3451 location_t loc = EXPR_LOCATION (exp);
3453 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3454 return NULL_RTX;
3456 dst = CALL_EXPR_ARG (exp, 0);
3457 src = CALL_EXPR_ARG (exp, 1);
3459 /* If return value is ignored, transform stpcpy into strcpy. */
3460 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3462 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3463 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3464 return expand_expr (result, target, mode, EXPAND_NORMAL);
3466 else
3468 tree len, lenp1;
3469 rtx ret;
3471 /* Ensure we get an actual string whose length can be evaluated at
3472 compile-time, not an expression containing a string. This is
3473 because the latter will potentially produce pessimized code
3474 when used to produce the return value. */
3475 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3476 return expand_movstr (dst, src, target, /*endp=*/2);
3478 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3479 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3480 target, mode, /*endp=*/2);
3482 if (ret)
3483 return ret;
3485 if (TREE_CODE (len) == INTEGER_CST)
3487 rtx len_rtx = expand_normal (len);
3489 if (CONST_INT_P (len_rtx))
3491 ret = expand_builtin_strcpy_args (dst, src, target);
3493 if (ret)
3495 if (! target)
3497 if (mode != VOIDmode)
3498 target = gen_reg_rtx (mode);
3499 else
3500 target = gen_reg_rtx (GET_MODE (ret));
3502 if (GET_MODE (target) != GET_MODE (ret))
3503 ret = gen_lowpart (GET_MODE (target), ret);
3505 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3506 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3507 gcc_assert (ret);
3509 return target;
3514 return expand_movstr (dst, src, target, /*endp=*/2);
3518 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3519 bytes from constant string DATA + OFFSET and return it as target
3520 constant. */
3523 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3524 enum machine_mode mode)
3526 const char *str = (const char *) data;
3528 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3529 return const0_rtx;
3531 return c_readstr (str + offset, mode);
3534 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3535 NULL_RTX if we failed the caller should emit a normal call. */
3537 static rtx
3538 expand_builtin_strncpy (tree exp, rtx target)
3540 location_t loc = EXPR_LOCATION (exp);
3542 if (validate_arglist (exp,
3543 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3545 tree dest = CALL_EXPR_ARG (exp, 0);
3546 tree src = CALL_EXPR_ARG (exp, 1);
3547 tree len = CALL_EXPR_ARG (exp, 2);
3548 tree slen = c_strlen (src, 1);
3550 /* We must be passed a constant len and src parameter. */
3551 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3552 return NULL_RTX;
3554 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3556 /* We're required to pad with trailing zeros if the requested
3557 len is greater than strlen(s2)+1. In that case try to
3558 use store_by_pieces, if it fails, punt. */
3559 if (tree_int_cst_lt (slen, len))
3561 unsigned int dest_align = get_pointer_alignment (dest);
3562 const char *p = c_getstr (src);
3563 rtx dest_mem;
3565 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3566 || !can_store_by_pieces (tree_to_uhwi (len),
3567 builtin_strncpy_read_str,
3568 CONST_CAST (char *, p),
3569 dest_align, false))
3570 return NULL_RTX;
3572 dest_mem = get_memory_rtx (dest, len);
3573 store_by_pieces (dest_mem, tree_to_uhwi (len),
3574 builtin_strncpy_read_str,
3575 CONST_CAST (char *, p), dest_align, false, 0);
3576 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3577 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3578 return dest_mem;
3581 return NULL_RTX;
3584 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3585 bytes from constant string DATA + OFFSET and return it as target
3586 constant. */
3589 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3590 enum machine_mode mode)
3592 const char *c = (const char *) data;
3593 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3595 memset (p, *c, GET_MODE_SIZE (mode));
3597 return c_readstr (p, mode);
3600 /* Callback routine for store_by_pieces. Return the RTL of a register
3601 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3602 char value given in the RTL register data. For example, if mode is
3603 4 bytes wide, return the RTL for 0x01010101*data. */
3605 static rtx
3606 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3607 enum machine_mode mode)
3609 rtx target, coeff;
3610 size_t size;
3611 char *p;
3613 size = GET_MODE_SIZE (mode);
3614 if (size == 1)
3615 return (rtx) data;
3617 p = XALLOCAVEC (char, size);
3618 memset (p, 1, size);
3619 coeff = c_readstr (p, mode);
3621 target = convert_to_mode (mode, (rtx) data, 1);
3622 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3623 return force_reg (mode, target);
3626 /* Expand expression EXP, which is a call to the memset builtin. Return
3627 NULL_RTX if we failed the caller should emit a normal call, otherwise
3628 try to get the result in TARGET, if convenient (and in mode MODE if that's
3629 convenient). */
3631 static rtx
3632 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3634 if (!validate_arglist (exp,
3635 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3636 return NULL_RTX;
3637 else
3639 tree dest = CALL_EXPR_ARG (exp, 0);
3640 tree val = CALL_EXPR_ARG (exp, 1);
3641 tree len = CALL_EXPR_ARG (exp, 2);
3642 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3646 /* Helper function to do the actual work for expand_builtin_memset. The
3647 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3648 so that this can also be called without constructing an actual CALL_EXPR.
3649 The other arguments and return value are the same as for
3650 expand_builtin_memset. */
3652 static rtx
3653 expand_builtin_memset_args (tree dest, tree val, tree len,
3654 rtx target, enum machine_mode mode, tree orig_exp)
3656 tree fndecl, fn;
3657 enum built_in_function fcode;
3658 enum machine_mode val_mode;
3659 char c;
3660 unsigned int dest_align;
3661 rtx dest_mem, dest_addr, len_rtx;
3662 HOST_WIDE_INT expected_size = -1;
3663 unsigned int expected_align = 0;
3664 unsigned HOST_WIDE_INT min_size;
3665 unsigned HOST_WIDE_INT max_size;
3666 unsigned HOST_WIDE_INT probable_max_size;
3668 dest_align = get_pointer_alignment (dest);
3670 /* If DEST is not a pointer type, don't do this operation in-line. */
3671 if (dest_align == 0)
3672 return NULL_RTX;
3674 if (currently_expanding_gimple_stmt)
3675 stringop_block_profile (currently_expanding_gimple_stmt,
3676 &expected_align, &expected_size);
3678 if (expected_align < dest_align)
3679 expected_align = dest_align;
3681 /* If the LEN parameter is zero, return DEST. */
3682 if (integer_zerop (len))
3684 /* Evaluate and ignore VAL in case it has side-effects. */
3685 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3686 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3689 /* Stabilize the arguments in case we fail. */
3690 dest = builtin_save_expr (dest);
3691 val = builtin_save_expr (val);
3692 len = builtin_save_expr (len);
3694 len_rtx = expand_normal (len);
3695 determine_block_size (len, len_rtx, &min_size, &max_size,
3696 &probable_max_size);
3697 dest_mem = get_memory_rtx (dest, len);
3698 val_mode = TYPE_MODE (unsigned_char_type_node);
3700 if (TREE_CODE (val) != INTEGER_CST)
3702 rtx val_rtx;
3704 val_rtx = expand_normal (val);
3705 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3707 /* Assume that we can memset by pieces if we can store
3708 * the coefficients by pieces (in the required modes).
3709 * We can't pass builtin_memset_gen_str as that emits RTL. */
3710 c = 1;
3711 if (tree_fits_uhwi_p (len)
3712 && can_store_by_pieces (tree_to_uhwi (len),
3713 builtin_memset_read_str, &c, dest_align,
3714 true))
3716 val_rtx = force_reg (val_mode, val_rtx);
3717 store_by_pieces (dest_mem, tree_to_uhwi (len),
3718 builtin_memset_gen_str, val_rtx, dest_align,
3719 true, 0);
3721 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3722 dest_align, expected_align,
3723 expected_size, min_size, max_size,
3724 probable_max_size))
3725 goto do_libcall;
3727 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3728 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3729 return dest_mem;
3732 if (target_char_cast (val, &c))
3733 goto do_libcall;
3735 if (c)
3737 if (tree_fits_uhwi_p (len)
3738 && can_store_by_pieces (tree_to_uhwi (len),
3739 builtin_memset_read_str, &c, dest_align,
3740 true))
3741 store_by_pieces (dest_mem, tree_to_uhwi (len),
3742 builtin_memset_read_str, &c, dest_align, true, 0);
3743 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3744 gen_int_mode (c, val_mode),
3745 dest_align, expected_align,
3746 expected_size, min_size, max_size,
3747 probable_max_size))
3748 goto do_libcall;
3750 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3751 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3752 return dest_mem;
3755 set_mem_align (dest_mem, dest_align);
3756 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3757 CALL_EXPR_TAILCALL (orig_exp)
3758 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3759 expected_align, expected_size,
3760 min_size, max_size,
3761 probable_max_size);
3763 if (dest_addr == 0)
3765 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3766 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3769 return dest_addr;
3771 do_libcall:
3772 fndecl = get_callee_fndecl (orig_exp);
3773 fcode = DECL_FUNCTION_CODE (fndecl);
3774 if (fcode == BUILT_IN_MEMSET)
3775 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3776 dest, val, len);
3777 else if (fcode == BUILT_IN_BZERO)
3778 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3779 dest, len);
3780 else
3781 gcc_unreachable ();
3782 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3783 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3784 return expand_call (fn, target, target == const0_rtx);
3787 /* Expand expression EXP, which is a call to the bzero builtin. Return
3788 NULL_RTX if we failed the caller should emit a normal call. */
3790 static rtx
3791 expand_builtin_bzero (tree exp)
3793 tree dest, size;
3794 location_t loc = EXPR_LOCATION (exp);
3796 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3797 return NULL_RTX;
3799 dest = CALL_EXPR_ARG (exp, 0);
3800 size = CALL_EXPR_ARG (exp, 1);
3802 /* New argument list transforming bzero(ptr x, int y) to
3803 memset(ptr x, int 0, size_t y). This is done this way
3804 so that if it isn't expanded inline, we fallback to
3805 calling bzero instead of memset. */
3807 return expand_builtin_memset_args (dest, integer_zero_node,
3808 fold_convert_loc (loc,
3809 size_type_node, size),
3810 const0_rtx, VOIDmode, exp);
3813 /* Expand expression EXP, which is a call to the memcmp built-in function.
3814 Return NULL_RTX if we failed and the caller should emit a normal call,
3815 otherwise try to get the result in TARGET, if convenient (and in mode
3816 MODE, if that's convenient). */
3818 static rtx
3819 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3820 ATTRIBUTE_UNUSED enum machine_mode mode)
3822 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3824 if (!validate_arglist (exp,
3825 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3826 return NULL_RTX;
3828 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3829 implementing memcmp because it will stop if it encounters two
3830 zero bytes. */
3831 #if defined HAVE_cmpmemsi
3833 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3834 rtx result;
3835 rtx insn;
3836 tree arg1 = CALL_EXPR_ARG (exp, 0);
3837 tree arg2 = CALL_EXPR_ARG (exp, 1);
3838 tree len = CALL_EXPR_ARG (exp, 2);
3840 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3841 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3842 enum machine_mode insn_mode;
3844 if (HAVE_cmpmemsi)
3845 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3846 else
3847 return NULL_RTX;
3849 /* If we don't have POINTER_TYPE, call the function. */
3850 if (arg1_align == 0 || arg2_align == 0)
3851 return NULL_RTX;
3853 /* Make a place to write the result of the instruction. */
3854 result = target;
3855 if (! (result != 0
3856 && REG_P (result) && GET_MODE (result) == insn_mode
3857 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3858 result = gen_reg_rtx (insn_mode);
3860 arg1_rtx = get_memory_rtx (arg1, len);
3861 arg2_rtx = get_memory_rtx (arg2, len);
3862 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3864 /* Set MEM_SIZE as appropriate. */
3865 if (CONST_INT_P (arg3_rtx))
3867 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3868 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3871 if (HAVE_cmpmemsi)
3872 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3873 GEN_INT (MIN (arg1_align, arg2_align)));
3874 else
3875 gcc_unreachable ();
3877 if (insn)
3878 emit_insn (insn);
3879 else
3880 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3881 TYPE_MODE (integer_type_node), 3,
3882 XEXP (arg1_rtx, 0), Pmode,
3883 XEXP (arg2_rtx, 0), Pmode,
3884 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3885 TYPE_UNSIGNED (sizetype)),
3886 TYPE_MODE (sizetype));
3888 /* Return the value in the proper mode for this function. */
3889 mode = TYPE_MODE (TREE_TYPE (exp));
3890 if (GET_MODE (result) == mode)
3891 return result;
3892 else if (target != 0)
3894 convert_move (target, result, 0);
3895 return target;
3897 else
3898 return convert_to_mode (mode, result, 0);
3900 #endif /* HAVE_cmpmemsi. */
3902 return NULL_RTX;
3905 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3906 if we failed the caller should emit a normal call, otherwise try to get
3907 the result in TARGET, if convenient. */
3909 static rtx
3910 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3912 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3913 return NULL_RTX;
3915 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3916 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3917 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3919 rtx arg1_rtx, arg2_rtx;
3920 rtx result, insn = NULL_RTX;
3921 tree fndecl, fn;
3922 tree arg1 = CALL_EXPR_ARG (exp, 0);
3923 tree arg2 = CALL_EXPR_ARG (exp, 1);
3925 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3926 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3928 /* If we don't have POINTER_TYPE, call the function. */
3929 if (arg1_align == 0 || arg2_align == 0)
3930 return NULL_RTX;
3932 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3933 arg1 = builtin_save_expr (arg1);
3934 arg2 = builtin_save_expr (arg2);
3936 arg1_rtx = get_memory_rtx (arg1, NULL);
3937 arg2_rtx = get_memory_rtx (arg2, NULL);
3939 #ifdef HAVE_cmpstrsi
3940 /* Try to call cmpstrsi. */
3941 if (HAVE_cmpstrsi)
3943 enum machine_mode insn_mode
3944 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3946 /* Make a place to write the result of the instruction. */
3947 result = target;
3948 if (! (result != 0
3949 && REG_P (result) && GET_MODE (result) == insn_mode
3950 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3951 result = gen_reg_rtx (insn_mode);
3953 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3954 GEN_INT (MIN (arg1_align, arg2_align)));
3956 #endif
3957 #ifdef HAVE_cmpstrnsi
3958 /* Try to determine at least one length and call cmpstrnsi. */
3959 if (!insn && HAVE_cmpstrnsi)
3961 tree len;
3962 rtx arg3_rtx;
3964 enum machine_mode insn_mode
3965 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3966 tree len1 = c_strlen (arg1, 1);
3967 tree len2 = c_strlen (arg2, 1);
3969 if (len1)
3970 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3971 if (len2)
3972 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3974 /* If we don't have a constant length for the first, use the length
3975 of the second, if we know it. We don't require a constant for
3976 this case; some cost analysis could be done if both are available
3977 but neither is constant. For now, assume they're equally cheap,
3978 unless one has side effects. If both strings have constant lengths,
3979 use the smaller. */
3981 if (!len1)
3982 len = len2;
3983 else if (!len2)
3984 len = len1;
3985 else if (TREE_SIDE_EFFECTS (len1))
3986 len = len2;
3987 else if (TREE_SIDE_EFFECTS (len2))
3988 len = len1;
3989 else if (TREE_CODE (len1) != INTEGER_CST)
3990 len = len2;
3991 else if (TREE_CODE (len2) != INTEGER_CST)
3992 len = len1;
3993 else if (tree_int_cst_lt (len1, len2))
3994 len = len1;
3995 else
3996 len = len2;
3998 /* If both arguments have side effects, we cannot optimize. */
3999 if (!len || TREE_SIDE_EFFECTS (len))
4000 goto do_libcall;
4002 arg3_rtx = expand_normal (len);
4004 /* Make a place to write the result of the instruction. */
4005 result = target;
4006 if (! (result != 0
4007 && REG_P (result) && GET_MODE (result) == insn_mode
4008 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4009 result = gen_reg_rtx (insn_mode);
4011 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4012 GEN_INT (MIN (arg1_align, arg2_align)));
4014 #endif
4016 if (insn)
4018 enum machine_mode mode;
4019 emit_insn (insn);
4021 /* Return the value in the proper mode for this function. */
4022 mode = TYPE_MODE (TREE_TYPE (exp));
4023 if (GET_MODE (result) == mode)
4024 return result;
4025 if (target == 0)
4026 return convert_to_mode (mode, result, 0);
4027 convert_move (target, result, 0);
4028 return target;
4031 /* Expand the library call ourselves using a stabilized argument
4032 list to avoid re-evaluating the function's arguments twice. */
4033 #ifdef HAVE_cmpstrnsi
4034 do_libcall:
4035 #endif
4036 fndecl = get_callee_fndecl (exp);
4037 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4038 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4039 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4040 return expand_call (fn, target, target == const0_rtx);
4042 #endif
4043 return NULL_RTX;
4046 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4047 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4048 the result in TARGET, if convenient. */
4050 static rtx
4051 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4052 ATTRIBUTE_UNUSED enum machine_mode mode)
4054 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4056 if (!validate_arglist (exp,
4057 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4058 return NULL_RTX;
4060 /* If c_strlen can determine an expression for one of the string
4061 lengths, and it doesn't have side effects, then emit cmpstrnsi
4062 using length MIN(strlen(string)+1, arg3). */
4063 #ifdef HAVE_cmpstrnsi
4064 if (HAVE_cmpstrnsi)
4066 tree len, len1, len2;
4067 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4068 rtx result, insn;
4069 tree fndecl, fn;
4070 tree arg1 = CALL_EXPR_ARG (exp, 0);
4071 tree arg2 = CALL_EXPR_ARG (exp, 1);
4072 tree arg3 = CALL_EXPR_ARG (exp, 2);
4074 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4075 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4076 enum machine_mode insn_mode
4077 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4079 len1 = c_strlen (arg1, 1);
4080 len2 = c_strlen (arg2, 1);
4082 if (len1)
4083 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4084 if (len2)
4085 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4087 /* If we don't have a constant length for the first, use the length
4088 of the second, if we know it. We don't require a constant for
4089 this case; some cost analysis could be done if both are available
4090 but neither is constant. For now, assume they're equally cheap,
4091 unless one has side effects. If both strings have constant lengths,
4092 use the smaller. */
4094 if (!len1)
4095 len = len2;
4096 else if (!len2)
4097 len = len1;
4098 else if (TREE_SIDE_EFFECTS (len1))
4099 len = len2;
4100 else if (TREE_SIDE_EFFECTS (len2))
4101 len = len1;
4102 else if (TREE_CODE (len1) != INTEGER_CST)
4103 len = len2;
4104 else if (TREE_CODE (len2) != INTEGER_CST)
4105 len = len1;
4106 else if (tree_int_cst_lt (len1, len2))
4107 len = len1;
4108 else
4109 len = len2;
4111 /* If both arguments have side effects, we cannot optimize. */
4112 if (!len || TREE_SIDE_EFFECTS (len))
4113 return NULL_RTX;
4115 /* The actual new length parameter is MIN(len,arg3). */
4116 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4117 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4119 /* If we don't have POINTER_TYPE, call the function. */
4120 if (arg1_align == 0 || arg2_align == 0)
4121 return NULL_RTX;
4123 /* Make a place to write the result of the instruction. */
4124 result = target;
4125 if (! (result != 0
4126 && REG_P (result) && GET_MODE (result) == insn_mode
4127 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4128 result = gen_reg_rtx (insn_mode);
4130 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4131 arg1 = builtin_save_expr (arg1);
4132 arg2 = builtin_save_expr (arg2);
4133 len = builtin_save_expr (len);
4135 arg1_rtx = get_memory_rtx (arg1, len);
4136 arg2_rtx = get_memory_rtx (arg2, len);
4137 arg3_rtx = expand_normal (len);
4138 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4139 GEN_INT (MIN (arg1_align, arg2_align)));
4140 if (insn)
4142 emit_insn (insn);
4144 /* Return the value in the proper mode for this function. */
4145 mode = TYPE_MODE (TREE_TYPE (exp));
4146 if (GET_MODE (result) == mode)
4147 return result;
4148 if (target == 0)
4149 return convert_to_mode (mode, result, 0);
4150 convert_move (target, result, 0);
4151 return target;
4154 /* Expand the library call ourselves using a stabilized argument
4155 list to avoid re-evaluating the function's arguments twice. */
4156 fndecl = get_callee_fndecl (exp);
4157 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4158 arg1, arg2, len);
4159 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4160 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4161 return expand_call (fn, target, target == const0_rtx);
4163 #endif
4164 return NULL_RTX;
4167 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4168 if that's convenient. */
4171 expand_builtin_saveregs (void)
4173 rtx val, seq;
4175 /* Don't do __builtin_saveregs more than once in a function.
4176 Save the result of the first call and reuse it. */
4177 if (saveregs_value != 0)
4178 return saveregs_value;
4180 /* When this function is called, it means that registers must be
4181 saved on entry to this function. So we migrate the call to the
4182 first insn of this function. */
4184 start_sequence ();
4186 /* Do whatever the machine needs done in this case. */
4187 val = targetm.calls.expand_builtin_saveregs ();
4189 seq = get_insns ();
4190 end_sequence ();
4192 saveregs_value = val;
4194 /* Put the insns after the NOTE that starts the function. If this
4195 is inside a start_sequence, make the outer-level insn chain current, so
4196 the code is placed at the start of the function. */
4197 push_topmost_sequence ();
4198 emit_insn_after (seq, entry_of_function ());
4199 pop_topmost_sequence ();
4201 return val;
4204 /* Expand a call to __builtin_next_arg. */
4206 static rtx
4207 expand_builtin_next_arg (void)
4209 /* Checking arguments is already done in fold_builtin_next_arg
4210 that must be called before this function. */
4211 return expand_binop (ptr_mode, add_optab,
4212 crtl->args.internal_arg_pointer,
4213 crtl->args.arg_offset_rtx,
4214 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4217 /* Make it easier for the backends by protecting the valist argument
4218 from multiple evaluations. */
4220 static tree
4221 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4223 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4225 /* The current way of determining the type of valist is completely
4226 bogus. We should have the information on the va builtin instead. */
4227 if (!vatype)
4228 vatype = targetm.fn_abi_va_list (cfun->decl);
4230 if (TREE_CODE (vatype) == ARRAY_TYPE)
4232 if (TREE_SIDE_EFFECTS (valist))
4233 valist = save_expr (valist);
4235 /* For this case, the backends will be expecting a pointer to
4236 vatype, but it's possible we've actually been given an array
4237 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4238 So fix it. */
4239 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4241 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4242 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4245 else
4247 tree pt = build_pointer_type (vatype);
4249 if (! needs_lvalue)
4251 if (! TREE_SIDE_EFFECTS (valist))
4252 return valist;
4254 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4255 TREE_SIDE_EFFECTS (valist) = 1;
4258 if (TREE_SIDE_EFFECTS (valist))
4259 valist = save_expr (valist);
4260 valist = fold_build2_loc (loc, MEM_REF,
4261 vatype, valist, build_int_cst (pt, 0));
4264 return valist;
4267 /* The "standard" definition of va_list is void*. */
4269 tree
4270 std_build_builtin_va_list (void)
4272 return ptr_type_node;
4275 /* The "standard" abi va_list is va_list_type_node. */
4277 tree
4278 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4280 return va_list_type_node;
4283 /* The "standard" type of va_list is va_list_type_node. */
4285 tree
4286 std_canonical_va_list_type (tree type)
4288 tree wtype, htype;
4290 if (INDIRECT_REF_P (type))
4291 type = TREE_TYPE (type);
4292 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4293 type = TREE_TYPE (type);
4294 wtype = va_list_type_node;
4295 htype = type;
4296 /* Treat structure va_list types. */
4297 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4298 htype = TREE_TYPE (htype);
4299 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4301 /* If va_list is an array type, the argument may have decayed
4302 to a pointer type, e.g. by being passed to another function.
4303 In that case, unwrap both types so that we can compare the
4304 underlying records. */
4305 if (TREE_CODE (htype) == ARRAY_TYPE
4306 || POINTER_TYPE_P (htype))
4308 wtype = TREE_TYPE (wtype);
4309 htype = TREE_TYPE (htype);
4312 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4313 return va_list_type_node;
4315 return NULL_TREE;
4318 /* The "standard" implementation of va_start: just assign `nextarg' to
4319 the variable. */
4321 void
4322 std_expand_builtin_va_start (tree valist, rtx nextarg)
4324 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4325 convert_move (va_r, nextarg, 0);
4328 /* Expand EXP, a call to __builtin_va_start. */
4330 static rtx
4331 expand_builtin_va_start (tree exp)
4333 rtx nextarg;
4334 tree valist;
4335 location_t loc = EXPR_LOCATION (exp);
4337 if (call_expr_nargs (exp) < 2)
4339 error_at (loc, "too few arguments to function %<va_start%>");
4340 return const0_rtx;
4343 if (fold_builtin_next_arg (exp, true))
4344 return const0_rtx;
4346 nextarg = expand_builtin_next_arg ();
4347 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4349 if (targetm.expand_builtin_va_start)
4350 targetm.expand_builtin_va_start (valist, nextarg);
4351 else
4352 std_expand_builtin_va_start (valist, nextarg);
4354 return const0_rtx;
4357 /* Expand EXP, a call to __builtin_va_end. */
4359 static rtx
4360 expand_builtin_va_end (tree exp)
4362 tree valist = CALL_EXPR_ARG (exp, 0);
4364 /* Evaluate for side effects, if needed. I hate macros that don't
4365 do that. */
4366 if (TREE_SIDE_EFFECTS (valist))
4367 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4369 return const0_rtx;
4372 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4373 builtin rather than just as an assignment in stdarg.h because of the
4374 nastiness of array-type va_list types. */
4376 static rtx
4377 expand_builtin_va_copy (tree exp)
4379 tree dst, src, t;
4380 location_t loc = EXPR_LOCATION (exp);
4382 dst = CALL_EXPR_ARG (exp, 0);
4383 src = CALL_EXPR_ARG (exp, 1);
4385 dst = stabilize_va_list_loc (loc, dst, 1);
4386 src = stabilize_va_list_loc (loc, src, 0);
4388 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4390 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4392 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4393 TREE_SIDE_EFFECTS (t) = 1;
4394 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4396 else
4398 rtx dstb, srcb, size;
4400 /* Evaluate to pointers. */
4401 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4402 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4403 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4404 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4406 dstb = convert_memory_address (Pmode, dstb);
4407 srcb = convert_memory_address (Pmode, srcb);
4409 /* "Dereference" to BLKmode memories. */
4410 dstb = gen_rtx_MEM (BLKmode, dstb);
4411 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4412 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4413 srcb = gen_rtx_MEM (BLKmode, srcb);
4414 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4415 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4417 /* Copy. */
4418 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4421 return const0_rtx;
4424 /* Expand a call to one of the builtin functions __builtin_frame_address or
4425 __builtin_return_address. */
4427 static rtx
4428 expand_builtin_frame_address (tree fndecl, tree exp)
4430 /* The argument must be a nonnegative integer constant.
4431 It counts the number of frames to scan up the stack.
4432 The value is the return address saved in that frame. */
4433 if (call_expr_nargs (exp) == 0)
4434 /* Warning about missing arg was already issued. */
4435 return const0_rtx;
4436 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4438 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4439 error ("invalid argument to %<__builtin_frame_address%>");
4440 else
4441 error ("invalid argument to %<__builtin_return_address%>");
4442 return const0_rtx;
4444 else
4446 rtx tem
4447 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4448 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4450 /* Some ports cannot access arbitrary stack frames. */
4451 if (tem == NULL)
4453 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4454 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4455 else
4456 warning (0, "unsupported argument to %<__builtin_return_address%>");
4457 return const0_rtx;
4460 /* For __builtin_frame_address, return what we've got. */
4461 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4462 return tem;
4464 if (!REG_P (tem)
4465 && ! CONSTANT_P (tem))
4466 tem = copy_addr_to_reg (tem);
4467 return tem;
4471 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4472 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4473 is the same as for allocate_dynamic_stack_space. */
4475 static rtx
4476 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4478 rtx op0;
4479 rtx result;
4480 bool valid_arglist;
4481 unsigned int align;
4482 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4483 == BUILT_IN_ALLOCA_WITH_ALIGN);
4485 valid_arglist
4486 = (alloca_with_align
4487 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4488 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4490 if (!valid_arglist)
4491 return NULL_RTX;
4493 /* Compute the argument. */
4494 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4496 /* Compute the alignment. */
4497 align = (alloca_with_align
4498 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4499 : BIGGEST_ALIGNMENT);
4501 /* Allocate the desired space. */
4502 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4503 result = convert_memory_address (ptr_mode, result);
4505 return result;
4508 /* Expand a call to bswap builtin in EXP.
4509 Return NULL_RTX if a normal call should be emitted rather than expanding the
4510 function in-line. If convenient, the result should be placed in TARGET.
4511 SUBTARGET may be used as the target for computing one of EXP's operands. */
4513 static rtx
4514 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4515 rtx subtarget)
4517 tree arg;
4518 rtx op0;
4520 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4521 return NULL_RTX;
4523 arg = CALL_EXPR_ARG (exp, 0);
4524 op0 = expand_expr (arg,
4525 subtarget && GET_MODE (subtarget) == target_mode
4526 ? subtarget : NULL_RTX,
4527 target_mode, EXPAND_NORMAL);
4528 if (GET_MODE (op0) != target_mode)
4529 op0 = convert_to_mode (target_mode, op0, 1);
4531 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4533 gcc_assert (target);
4535 return convert_to_mode (target_mode, target, 1);
4538 /* Expand a call to a unary builtin in EXP.
4539 Return NULL_RTX if a normal call should be emitted rather than expanding the
4540 function in-line. If convenient, the result should be placed in TARGET.
4541 SUBTARGET may be used as the target for computing one of EXP's operands. */
4543 static rtx
4544 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4545 rtx subtarget, optab op_optab)
4547 rtx op0;
4549 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4550 return NULL_RTX;
4552 /* Compute the argument. */
4553 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4554 (subtarget
4555 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4556 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4557 VOIDmode, EXPAND_NORMAL);
4558 /* Compute op, into TARGET if possible.
4559 Set TARGET to wherever the result comes back. */
4560 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4561 op_optab, op0, target, op_optab != clrsb_optab);
4562 gcc_assert (target);
4564 return convert_to_mode (target_mode, target, 0);
4567 /* Expand a call to __builtin_expect. We just return our argument
4568 as the builtin_expect semantic should've been already executed by
4569 tree branch prediction pass. */
4571 static rtx
4572 expand_builtin_expect (tree exp, rtx target)
4574 tree arg;
4576 if (call_expr_nargs (exp) < 2)
4577 return const0_rtx;
4578 arg = CALL_EXPR_ARG (exp, 0);
4580 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4581 /* When guessing was done, the hints should be already stripped away. */
4582 gcc_assert (!flag_guess_branch_prob
4583 || optimize == 0 || seen_error ());
4584 return target;
4587 /* Expand a call to __builtin_assume_aligned. We just return our first
4588 argument as the builtin_assume_aligned semantic should've been already
4589 executed by CCP. */
4591 static rtx
4592 expand_builtin_assume_aligned (tree exp, rtx target)
4594 if (call_expr_nargs (exp) < 2)
4595 return const0_rtx;
4596 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4597 EXPAND_NORMAL);
4598 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4599 && (call_expr_nargs (exp) < 3
4600 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4601 return target;
4604 void
4605 expand_builtin_trap (void)
4607 #ifdef HAVE_trap
4608 if (HAVE_trap)
4610 rtx insn = emit_insn (gen_trap ());
4611 /* For trap insns when not accumulating outgoing args force
4612 REG_ARGS_SIZE note to prevent crossjumping of calls with
4613 different args sizes. */
4614 if (!ACCUMULATE_OUTGOING_ARGS)
4615 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4617 else
4618 #endif
4619 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4620 emit_barrier ();
4623 /* Expand a call to __builtin_unreachable. We do nothing except emit
4624 a barrier saying that control flow will not pass here.
4626 It is the responsibility of the program being compiled to ensure
4627 that control flow does never reach __builtin_unreachable. */
4628 static void
4629 expand_builtin_unreachable (void)
4631 emit_barrier ();
4634 /* Expand EXP, a call to fabs, fabsf or fabsl.
4635 Return NULL_RTX if a normal call should be emitted rather than expanding
4636 the function inline. If convenient, the result should be placed
4637 in TARGET. SUBTARGET may be used as the target for computing
4638 the operand. */
4640 static rtx
4641 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4643 enum machine_mode mode;
4644 tree arg;
4645 rtx op0;
4647 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4648 return NULL_RTX;
4650 arg = CALL_EXPR_ARG (exp, 0);
4651 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4652 mode = TYPE_MODE (TREE_TYPE (arg));
4653 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4654 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4657 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4658 Return NULL is a normal call should be emitted rather than expanding the
4659 function inline. If convenient, the result should be placed in TARGET.
4660 SUBTARGET may be used as the target for computing the operand. */
4662 static rtx
4663 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4665 rtx op0, op1;
4666 tree arg;
4668 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4669 return NULL_RTX;
4671 arg = CALL_EXPR_ARG (exp, 0);
4672 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4674 arg = CALL_EXPR_ARG (exp, 1);
4675 op1 = expand_normal (arg);
4677 return expand_copysign (op0, op1, target);
4680 /* Create a new constant string literal and return a char* pointer to it.
4681 The STRING_CST value is the LEN characters at STR. */
4682 tree
4683 build_string_literal (int len, const char *str)
4685 tree t, elem, index, type;
4687 t = build_string (len, str);
4688 elem = build_type_variant (char_type_node, 1, 0);
4689 index = build_index_type (size_int (len - 1));
4690 type = build_array_type (elem, index);
4691 TREE_TYPE (t) = type;
4692 TREE_CONSTANT (t) = 1;
4693 TREE_READONLY (t) = 1;
4694 TREE_STATIC (t) = 1;
4696 type = build_pointer_type (elem);
4697 t = build1 (ADDR_EXPR, type,
4698 build4 (ARRAY_REF, elem,
4699 t, integer_zero_node, NULL_TREE, NULL_TREE));
4700 return t;
4703 /* Expand a call to __builtin___clear_cache. */
4705 static rtx
4706 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4708 #ifndef HAVE_clear_cache
4709 #ifdef CLEAR_INSN_CACHE
4710 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4711 does something. Just do the default expansion to a call to
4712 __clear_cache(). */
4713 return NULL_RTX;
4714 #else
4715 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4716 does nothing. There is no need to call it. Do nothing. */
4717 return const0_rtx;
4718 #endif /* CLEAR_INSN_CACHE */
4719 #else
4720 /* We have a "clear_cache" insn, and it will handle everything. */
4721 tree begin, end;
4722 rtx begin_rtx, end_rtx;
4724 /* We must not expand to a library call. If we did, any
4725 fallback library function in libgcc that might contain a call to
4726 __builtin___clear_cache() would recurse infinitely. */
4727 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4729 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4730 return const0_rtx;
4733 if (HAVE_clear_cache)
4735 struct expand_operand ops[2];
4737 begin = CALL_EXPR_ARG (exp, 0);
4738 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4740 end = CALL_EXPR_ARG (exp, 1);
4741 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4743 create_address_operand (&ops[0], begin_rtx);
4744 create_address_operand (&ops[1], end_rtx);
4745 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4746 return const0_rtx;
4748 return const0_rtx;
4749 #endif /* HAVE_clear_cache */
4752 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4754 static rtx
4755 round_trampoline_addr (rtx tramp)
4757 rtx temp, addend, mask;
4759 /* If we don't need too much alignment, we'll have been guaranteed
4760 proper alignment by get_trampoline_type. */
4761 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4762 return tramp;
4764 /* Round address up to desired boundary. */
4765 temp = gen_reg_rtx (Pmode);
4766 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4767 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4769 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4770 temp, 0, OPTAB_LIB_WIDEN);
4771 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4772 temp, 0, OPTAB_LIB_WIDEN);
4774 return tramp;
4777 static rtx
4778 expand_builtin_init_trampoline (tree exp, bool onstack)
4780 tree t_tramp, t_func, t_chain;
4781 rtx m_tramp, r_tramp, r_chain, tmp;
4783 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4784 POINTER_TYPE, VOID_TYPE))
4785 return NULL_RTX;
4787 t_tramp = CALL_EXPR_ARG (exp, 0);
4788 t_func = CALL_EXPR_ARG (exp, 1);
4789 t_chain = CALL_EXPR_ARG (exp, 2);
4791 r_tramp = expand_normal (t_tramp);
4792 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4793 MEM_NOTRAP_P (m_tramp) = 1;
4795 /* If ONSTACK, the TRAMP argument should be the address of a field
4796 within the local function's FRAME decl. Either way, let's see if
4797 we can fill in the MEM_ATTRs for this memory. */
4798 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4799 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4801 /* Creator of a heap trampoline is responsible for making sure the
4802 address is aligned to at least STACK_BOUNDARY. Normally malloc
4803 will ensure this anyhow. */
4804 tmp = round_trampoline_addr (r_tramp);
4805 if (tmp != r_tramp)
4807 m_tramp = change_address (m_tramp, BLKmode, tmp);
4808 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4809 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4812 /* The FUNC argument should be the address of the nested function.
4813 Extract the actual function decl to pass to the hook. */
4814 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4815 t_func = TREE_OPERAND (t_func, 0);
4816 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4818 r_chain = expand_normal (t_chain);
4820 /* Generate insns to initialize the trampoline. */
4821 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4823 if (onstack)
4825 trampolines_created = 1;
4827 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4828 "trampoline generated for nested function %qD", t_func);
4831 return const0_rtx;
4834 static rtx
4835 expand_builtin_adjust_trampoline (tree exp)
4837 rtx tramp;
4839 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4840 return NULL_RTX;
4842 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4843 tramp = round_trampoline_addr (tramp);
4844 if (targetm.calls.trampoline_adjust_address)
4845 tramp = targetm.calls.trampoline_adjust_address (tramp);
4847 return tramp;
4850 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4851 function. The function first checks whether the back end provides
4852 an insn to implement signbit for the respective mode. If not, it
4853 checks whether the floating point format of the value is such that
4854 the sign bit can be extracted. If that is not the case, the
4855 function returns NULL_RTX to indicate that a normal call should be
4856 emitted rather than expanding the function in-line. EXP is the
4857 expression that is a call to the builtin function; if convenient,
4858 the result should be placed in TARGET. */
4859 static rtx
4860 expand_builtin_signbit (tree exp, rtx target)
4862 const struct real_format *fmt;
4863 enum machine_mode fmode, imode, rmode;
4864 tree arg;
4865 int word, bitpos;
4866 enum insn_code icode;
4867 rtx temp;
4868 location_t loc = EXPR_LOCATION (exp);
4870 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4871 return NULL_RTX;
4873 arg = CALL_EXPR_ARG (exp, 0);
4874 fmode = TYPE_MODE (TREE_TYPE (arg));
4875 rmode = TYPE_MODE (TREE_TYPE (exp));
4876 fmt = REAL_MODE_FORMAT (fmode);
4878 arg = builtin_save_expr (arg);
4880 /* Expand the argument yielding a RTX expression. */
4881 temp = expand_normal (arg);
4883 /* Check if the back end provides an insn that handles signbit for the
4884 argument's mode. */
4885 icode = optab_handler (signbit_optab, fmode);
4886 if (icode != CODE_FOR_nothing)
4888 rtx last = get_last_insn ();
4889 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4890 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4891 return target;
4892 delete_insns_since (last);
4895 /* For floating point formats without a sign bit, implement signbit
4896 as "ARG < 0.0". */
4897 bitpos = fmt->signbit_ro;
4898 if (bitpos < 0)
4900 /* But we can't do this if the format supports signed zero. */
4901 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4902 return NULL_RTX;
4904 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4905 build_real (TREE_TYPE (arg), dconst0));
4906 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4909 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4911 imode = int_mode_for_mode (fmode);
4912 if (imode == BLKmode)
4913 return NULL_RTX;
4914 temp = gen_lowpart (imode, temp);
4916 else
4918 imode = word_mode;
4919 /* Handle targets with different FP word orders. */
4920 if (FLOAT_WORDS_BIG_ENDIAN)
4921 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4922 else
4923 word = bitpos / BITS_PER_WORD;
4924 temp = operand_subword_force (temp, word, fmode);
4925 bitpos = bitpos % BITS_PER_WORD;
4928 /* Force the intermediate word_mode (or narrower) result into a
4929 register. This avoids attempting to create paradoxical SUBREGs
4930 of floating point modes below. */
4931 temp = force_reg (imode, temp);
4933 /* If the bitpos is within the "result mode" lowpart, the operation
4934 can be implement with a single bitwise AND. Otherwise, we need
4935 a right shift and an AND. */
4937 if (bitpos < GET_MODE_BITSIZE (rmode))
4939 double_int mask = double_int_zero.set_bit (bitpos);
4941 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4942 temp = gen_lowpart (rmode, temp);
4943 temp = expand_binop (rmode, and_optab, temp,
4944 immed_double_int_const (mask, rmode),
4945 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4947 else
4949 /* Perform a logical right shift to place the signbit in the least
4950 significant bit, then truncate the result to the desired mode
4951 and mask just this bit. */
4952 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4953 temp = gen_lowpart (rmode, temp);
4954 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4955 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4958 return temp;
4961 /* Expand fork or exec calls. TARGET is the desired target of the
4962 call. EXP is the call. FN is the
4963 identificator of the actual function. IGNORE is nonzero if the
4964 value is to be ignored. */
4966 static rtx
4967 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4969 tree id, decl;
4970 tree call;
4972 /* If we are not profiling, just call the function. */
4973 if (!profile_arc_flag)
4974 return NULL_RTX;
4976 /* Otherwise call the wrapper. This should be equivalent for the rest of
4977 compiler, so the code does not diverge, and the wrapper may run the
4978 code necessary for keeping the profiling sane. */
4980 switch (DECL_FUNCTION_CODE (fn))
4982 case BUILT_IN_FORK:
4983 id = get_identifier ("__gcov_fork");
4984 break;
4986 case BUILT_IN_EXECL:
4987 id = get_identifier ("__gcov_execl");
4988 break;
4990 case BUILT_IN_EXECV:
4991 id = get_identifier ("__gcov_execv");
4992 break;
4994 case BUILT_IN_EXECLP:
4995 id = get_identifier ("__gcov_execlp");
4996 break;
4998 case BUILT_IN_EXECLE:
4999 id = get_identifier ("__gcov_execle");
5000 break;
5002 case BUILT_IN_EXECVP:
5003 id = get_identifier ("__gcov_execvp");
5004 break;
5006 case BUILT_IN_EXECVE:
5007 id = get_identifier ("__gcov_execve");
5008 break;
5010 default:
5011 gcc_unreachable ();
5014 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5015 FUNCTION_DECL, id, TREE_TYPE (fn));
5016 DECL_EXTERNAL (decl) = 1;
5017 TREE_PUBLIC (decl) = 1;
5018 DECL_ARTIFICIAL (decl) = 1;
5019 TREE_NOTHROW (decl) = 1;
5020 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5021 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5022 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5023 return expand_call (call, target, ignore);
5028 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5029 the pointer in these functions is void*, the tree optimizers may remove
5030 casts. The mode computed in expand_builtin isn't reliable either, due
5031 to __sync_bool_compare_and_swap.
5033 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5034 group of builtins. This gives us log2 of the mode size. */
5036 static inline enum machine_mode
5037 get_builtin_sync_mode (int fcode_diff)
5039 /* The size is not negotiable, so ask not to get BLKmode in return
5040 if the target indicates that a smaller size would be better. */
5041 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5044 /* Expand the memory expression LOC and return the appropriate memory operand
5045 for the builtin_sync operations. */
5047 static rtx
5048 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5050 rtx addr, mem;
5052 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5053 addr = convert_memory_address (Pmode, addr);
5055 /* Note that we explicitly do not want any alias information for this
5056 memory, so that we kill all other live memories. Otherwise we don't
5057 satisfy the full barrier semantics of the intrinsic. */
5058 mem = validize_mem (gen_rtx_MEM (mode, addr));
5060 /* The alignment needs to be at least according to that of the mode. */
5061 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5062 get_pointer_alignment (loc)));
5063 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5064 MEM_VOLATILE_P (mem) = 1;
5066 return mem;
5069 /* Make sure an argument is in the right mode.
5070 EXP is the tree argument.
5071 MODE is the mode it should be in. */
5073 static rtx
5074 expand_expr_force_mode (tree exp, enum machine_mode mode)
5076 rtx val;
5077 enum machine_mode old_mode;
5079 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5080 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5081 of CONST_INTs, where we know the old_mode only from the call argument. */
5083 old_mode = GET_MODE (val);
5084 if (old_mode == VOIDmode)
5085 old_mode = TYPE_MODE (TREE_TYPE (exp));
5086 val = convert_modes (mode, old_mode, val, 1);
5087 return val;
5091 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5092 EXP is the CALL_EXPR. CODE is the rtx code
5093 that corresponds to the arithmetic or logical operation from the name;
5094 an exception here is that NOT actually means NAND. TARGET is an optional
5095 place for us to store the results; AFTER is true if this is the
5096 fetch_and_xxx form. */
5098 static rtx
5099 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5100 enum rtx_code code, bool after,
5101 rtx target)
5103 rtx val, mem;
5104 location_t loc = EXPR_LOCATION (exp);
5106 if (code == NOT && warn_sync_nand)
5108 tree fndecl = get_callee_fndecl (exp);
5109 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5111 static bool warned_f_a_n, warned_n_a_f;
5113 switch (fcode)
5115 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5116 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5117 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5118 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5119 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5120 if (warned_f_a_n)
5121 break;
5123 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5124 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5125 warned_f_a_n = true;
5126 break;
5128 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5129 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5130 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5131 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5132 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5133 if (warned_n_a_f)
5134 break;
5136 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5137 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5138 warned_n_a_f = true;
5139 break;
5141 default:
5142 gcc_unreachable ();
5146 /* Expand the operands. */
5147 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5148 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5150 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5151 after);
5154 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5155 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5156 true if this is the boolean form. TARGET is a place for us to store the
5157 results; this is NOT optional if IS_BOOL is true. */
5159 static rtx
5160 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5161 bool is_bool, rtx target)
5163 rtx old_val, new_val, mem;
5164 rtx *pbool, *poval;
5166 /* Expand the operands. */
5167 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5168 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5169 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5171 pbool = poval = NULL;
5172 if (target != const0_rtx)
5174 if (is_bool)
5175 pbool = &target;
5176 else
5177 poval = &target;
5179 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5180 false, MEMMODEL_SEQ_CST,
5181 MEMMODEL_SEQ_CST))
5182 return NULL_RTX;
5184 return target;
5187 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5188 general form is actually an atomic exchange, and some targets only
5189 support a reduced form with the second argument being a constant 1.
5190 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5191 the results. */
5193 static rtx
5194 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5195 rtx target)
5197 rtx val, mem;
5199 /* Expand the operands. */
5200 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5201 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5203 return expand_sync_lock_test_and_set (target, mem, val);
5206 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5208 static void
5209 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5211 rtx mem;
5213 /* Expand the operands. */
5214 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5216 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5219 /* Given an integer representing an ``enum memmodel'', verify its
5220 correctness and return the memory model enum. */
5222 static enum memmodel
5223 get_memmodel (tree exp)
5225 rtx op;
5226 unsigned HOST_WIDE_INT val;
5228 /* If the parameter is not a constant, it's a run time value so we'll just
5229 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5230 if (TREE_CODE (exp) != INTEGER_CST)
5231 return MEMMODEL_SEQ_CST;
5233 op = expand_normal (exp);
5235 val = INTVAL (op);
5236 if (targetm.memmodel_check)
5237 val = targetm.memmodel_check (val);
5238 else if (val & ~MEMMODEL_MASK)
5240 warning (OPT_Winvalid_memory_model,
5241 "Unknown architecture specifier in memory model to builtin.");
5242 return MEMMODEL_SEQ_CST;
5245 if ((INTVAL (op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5247 warning (OPT_Winvalid_memory_model,
5248 "invalid memory model argument to builtin");
5249 return MEMMODEL_SEQ_CST;
5252 return (enum memmodel) val;
5255 /* Expand the __atomic_exchange intrinsic:
5256 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5257 EXP is the CALL_EXPR.
5258 TARGET is an optional place for us to store the results. */
5260 static rtx
5261 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5263 rtx val, mem;
5264 enum memmodel model;
5266 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5267 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5269 error ("invalid memory model for %<__atomic_exchange%>");
5270 return NULL_RTX;
5273 if (!flag_inline_atomics)
5274 return NULL_RTX;
5276 /* Expand the operands. */
5277 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5278 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5280 return expand_atomic_exchange (target, mem, val, model);
5283 /* Expand the __atomic_compare_exchange intrinsic:
5284 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5285 TYPE desired, BOOL weak,
5286 enum memmodel success,
5287 enum memmodel failure)
5288 EXP is the CALL_EXPR.
5289 TARGET is an optional place for us to store the results. */
5291 static rtx
5292 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5293 rtx target)
5295 rtx expect, desired, mem, oldval;
5296 enum memmodel success, failure;
5297 tree weak;
5298 bool is_weak;
5300 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5301 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5303 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5304 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5306 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5307 return NULL_RTX;
5310 if (failure > success)
5312 error ("failure memory model cannot be stronger than success "
5313 "memory model for %<__atomic_compare_exchange%>");
5314 return NULL_RTX;
5317 if (!flag_inline_atomics)
5318 return NULL_RTX;
5320 /* Expand the operands. */
5321 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5323 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5324 expect = convert_memory_address (Pmode, expect);
5325 expect = gen_rtx_MEM (mode, expect);
5326 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5328 weak = CALL_EXPR_ARG (exp, 3);
5329 is_weak = false;
5330 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5331 is_weak = true;
5333 oldval = expect;
5334 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5335 &oldval, mem, oldval, desired,
5336 is_weak, success, failure))
5337 return NULL_RTX;
5339 if (oldval != expect)
5340 emit_move_insn (expect, oldval);
5342 return target;
5345 /* Expand the __atomic_load intrinsic:
5346 TYPE __atomic_load (TYPE *object, enum memmodel)
5347 EXP is the CALL_EXPR.
5348 TARGET is an optional place for us to store the results. */
5350 static rtx
5351 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5353 rtx mem;
5354 enum memmodel model;
5356 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5357 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5358 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5360 error ("invalid memory model for %<__atomic_load%>");
5361 return NULL_RTX;
5364 if (!flag_inline_atomics)
5365 return NULL_RTX;
5367 /* Expand the operand. */
5368 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5370 return expand_atomic_load (target, mem, model);
5374 /* Expand the __atomic_store intrinsic:
5375 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5376 EXP is the CALL_EXPR.
5377 TARGET is an optional place for us to store the results. */
5379 static rtx
5380 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5382 rtx mem, val;
5383 enum memmodel model;
5385 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5386 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5387 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5388 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5390 error ("invalid memory model for %<__atomic_store%>");
5391 return NULL_RTX;
5394 if (!flag_inline_atomics)
5395 return NULL_RTX;
5397 /* Expand the operands. */
5398 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5399 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5401 return expand_atomic_store (mem, val, model, false);
5404 /* Expand the __atomic_fetch_XXX intrinsic:
5405 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5406 EXP is the CALL_EXPR.
5407 TARGET is an optional place for us to store the results.
5408 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5409 FETCH_AFTER is true if returning the result of the operation.
5410 FETCH_AFTER is false if returning the value before the operation.
5411 IGNORE is true if the result is not used.
5412 EXT_CALL is the correct builtin for an external call if this cannot be
5413 resolved to an instruction sequence. */
5415 static rtx
5416 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5417 enum rtx_code code, bool fetch_after,
5418 bool ignore, enum built_in_function ext_call)
5420 rtx val, mem, ret;
5421 enum memmodel model;
5422 tree fndecl;
5423 tree addr;
5425 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5427 /* Expand the operands. */
5428 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5429 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5431 /* Only try generating instructions if inlining is turned on. */
5432 if (flag_inline_atomics)
5434 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5435 if (ret)
5436 return ret;
5439 /* Return if a different routine isn't needed for the library call. */
5440 if (ext_call == BUILT_IN_NONE)
5441 return NULL_RTX;
5443 /* Change the call to the specified function. */
5444 fndecl = get_callee_fndecl (exp);
5445 addr = CALL_EXPR_FN (exp);
5446 STRIP_NOPS (addr);
5448 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5449 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5451 /* Expand the call here so we can emit trailing code. */
5452 ret = expand_call (exp, target, ignore);
5454 /* Replace the original function just in case it matters. */
5455 TREE_OPERAND (addr, 0) = fndecl;
5457 /* Then issue the arithmetic correction to return the right result. */
5458 if (!ignore)
5460 if (code == NOT)
5462 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5463 OPTAB_LIB_WIDEN);
5464 ret = expand_simple_unop (mode, NOT, ret, target, true);
5466 else
5467 ret = expand_simple_binop (mode, code, ret, val, target, true,
5468 OPTAB_LIB_WIDEN);
5470 return ret;
5474 #ifndef HAVE_atomic_clear
5475 # define HAVE_atomic_clear 0
5476 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5477 #endif
5479 /* Expand an atomic clear operation.
5480 void _atomic_clear (BOOL *obj, enum memmodel)
5481 EXP is the call expression. */
5483 static rtx
5484 expand_builtin_atomic_clear (tree exp)
5486 enum machine_mode mode;
5487 rtx mem, ret;
5488 enum memmodel model;
5490 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5491 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5492 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5494 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5495 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5497 error ("invalid memory model for %<__atomic_store%>");
5498 return const0_rtx;
5501 if (HAVE_atomic_clear)
5503 emit_insn (gen_atomic_clear (mem, model));
5504 return const0_rtx;
5507 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5508 Failing that, a store is issued by __atomic_store. The only way this can
5509 fail is if the bool type is larger than a word size. Unlikely, but
5510 handle it anyway for completeness. Assume a single threaded model since
5511 there is no atomic support in this case, and no barriers are required. */
5512 ret = expand_atomic_store (mem, const0_rtx, model, true);
5513 if (!ret)
5514 emit_move_insn (mem, const0_rtx);
5515 return const0_rtx;
5518 /* Expand an atomic test_and_set operation.
5519 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5520 EXP is the call expression. */
5522 static rtx
5523 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5525 rtx mem;
5526 enum memmodel model;
5527 enum machine_mode mode;
5529 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5530 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5531 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5533 return expand_atomic_test_and_set (target, mem, model);
5537 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5538 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5540 static tree
5541 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5543 int size;
5544 enum machine_mode mode;
5545 unsigned int mode_align, type_align;
5547 if (TREE_CODE (arg0) != INTEGER_CST)
5548 return NULL_TREE;
5550 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5551 mode = mode_for_size (size, MODE_INT, 0);
5552 mode_align = GET_MODE_ALIGNMENT (mode);
5554 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5555 type_align = mode_align;
5556 else
5558 tree ttype = TREE_TYPE (arg1);
5560 /* This function is usually invoked and folded immediately by the front
5561 end before anything else has a chance to look at it. The pointer
5562 parameter at this point is usually cast to a void *, so check for that
5563 and look past the cast. */
5564 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5565 && VOID_TYPE_P (TREE_TYPE (ttype)))
5566 arg1 = TREE_OPERAND (arg1, 0);
5568 ttype = TREE_TYPE (arg1);
5569 gcc_assert (POINTER_TYPE_P (ttype));
5571 /* Get the underlying type of the object. */
5572 ttype = TREE_TYPE (ttype);
5573 type_align = TYPE_ALIGN (ttype);
5576 /* If the object has smaller alignment, the the lock free routines cannot
5577 be used. */
5578 if (type_align < mode_align)
5579 return boolean_false_node;
5581 /* Check if a compare_and_swap pattern exists for the mode which represents
5582 the required size. The pattern is not allowed to fail, so the existence
5583 of the pattern indicates support is present. */
5584 if (can_compare_and_swap_p (mode, true))
5585 return boolean_true_node;
5586 else
5587 return boolean_false_node;
5590 /* Return true if the parameters to call EXP represent an object which will
5591 always generate lock free instructions. The first argument represents the
5592 size of the object, and the second parameter is a pointer to the object
5593 itself. If NULL is passed for the object, then the result is based on
5594 typical alignment for an object of the specified size. Otherwise return
5595 false. */
5597 static rtx
5598 expand_builtin_atomic_always_lock_free (tree exp)
5600 tree size;
5601 tree arg0 = CALL_EXPR_ARG (exp, 0);
5602 tree arg1 = CALL_EXPR_ARG (exp, 1);
5604 if (TREE_CODE (arg0) != INTEGER_CST)
5606 error ("non-constant argument 1 to __atomic_always_lock_free");
5607 return const0_rtx;
5610 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5611 if (size == boolean_true_node)
5612 return const1_rtx;
5613 return const0_rtx;
5616 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5617 is lock free on this architecture. */
5619 static tree
5620 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5622 if (!flag_inline_atomics)
5623 return NULL_TREE;
5625 /* If it isn't always lock free, don't generate a result. */
5626 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5627 return boolean_true_node;
5629 return NULL_TREE;
5632 /* Return true if the parameters to call EXP represent an object which will
5633 always generate lock free instructions. The first argument represents the
5634 size of the object, and the second parameter is a pointer to the object
5635 itself. If NULL is passed for the object, then the result is based on
5636 typical alignment for an object of the specified size. Otherwise return
5637 NULL*/
5639 static rtx
5640 expand_builtin_atomic_is_lock_free (tree exp)
5642 tree size;
5643 tree arg0 = CALL_EXPR_ARG (exp, 0);
5644 tree arg1 = CALL_EXPR_ARG (exp, 1);
5646 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5648 error ("non-integer argument 1 to __atomic_is_lock_free");
5649 return NULL_RTX;
5652 if (!flag_inline_atomics)
5653 return NULL_RTX;
5655 /* If the value is known at compile time, return the RTX for it. */
5656 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5657 if (size == boolean_true_node)
5658 return const1_rtx;
5660 return NULL_RTX;
5663 /* Expand the __atomic_thread_fence intrinsic:
5664 void __atomic_thread_fence (enum memmodel)
5665 EXP is the CALL_EXPR. */
5667 static void
5668 expand_builtin_atomic_thread_fence (tree exp)
5670 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5671 expand_mem_thread_fence (model);
5674 /* Expand the __atomic_signal_fence intrinsic:
5675 void __atomic_signal_fence (enum memmodel)
5676 EXP is the CALL_EXPR. */
5678 static void
5679 expand_builtin_atomic_signal_fence (tree exp)
5681 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5682 expand_mem_signal_fence (model);
5685 /* Expand the __sync_synchronize intrinsic. */
5687 static void
5688 expand_builtin_sync_synchronize (void)
5690 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5693 static rtx
5694 expand_builtin_thread_pointer (tree exp, rtx target)
5696 enum insn_code icode;
5697 if (!validate_arglist (exp, VOID_TYPE))
5698 return const0_rtx;
5699 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5700 if (icode != CODE_FOR_nothing)
5702 struct expand_operand op;
5703 if (!REG_P (target) || GET_MODE (target) != Pmode)
5704 target = gen_reg_rtx (Pmode);
5705 create_output_operand (&op, target, Pmode);
5706 expand_insn (icode, 1, &op);
5707 return target;
5709 error ("__builtin_thread_pointer is not supported on this target");
5710 return const0_rtx;
5713 static void
5714 expand_builtin_set_thread_pointer (tree exp)
5716 enum insn_code icode;
5717 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5718 return;
5719 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5720 if (icode != CODE_FOR_nothing)
5722 struct expand_operand op;
5723 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5724 Pmode, EXPAND_NORMAL);
5725 create_input_operand (&op, val, Pmode);
5726 expand_insn (icode, 1, &op);
5727 return;
5729 error ("__builtin_set_thread_pointer is not supported on this target");
5733 /* Emit code to restore the current value of stack. */
5735 static void
5736 expand_stack_restore (tree var)
5738 rtx prev, sa = expand_normal (var);
5740 sa = convert_memory_address (Pmode, sa);
5742 prev = get_last_insn ();
5743 emit_stack_restore (SAVE_BLOCK, sa);
5744 fixup_args_size_notes (prev, get_last_insn (), 0);
5748 /* Emit code to save the current value of stack. */
5750 static rtx
5751 expand_stack_save (void)
5753 rtx ret = NULL_RTX;
5755 do_pending_stack_adjust ();
5756 emit_stack_save (SAVE_BLOCK, &ret);
5757 return ret;
5760 /* Expand an expression EXP that calls a built-in function,
5761 with result going to TARGET if that's convenient
5762 (and in mode MODE if that's convenient).
5763 SUBTARGET may be used as the target for computing one of EXP's operands.
5764 IGNORE is nonzero if the value is to be ignored. */
5767 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5768 int ignore)
5770 tree fndecl = get_callee_fndecl (exp);
5771 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5772 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5773 int flags;
5775 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5776 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5778 /* When not optimizing, generate calls to library functions for a certain
5779 set of builtins. */
5780 if (!optimize
5781 && !called_as_built_in (fndecl)
5782 && fcode != BUILT_IN_FORK
5783 && fcode != BUILT_IN_EXECL
5784 && fcode != BUILT_IN_EXECV
5785 && fcode != BUILT_IN_EXECLP
5786 && fcode != BUILT_IN_EXECLE
5787 && fcode != BUILT_IN_EXECVP
5788 && fcode != BUILT_IN_EXECVE
5789 && fcode != BUILT_IN_ALLOCA
5790 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5791 && fcode != BUILT_IN_FREE)
5792 return expand_call (exp, target, ignore);
5794 /* The built-in function expanders test for target == const0_rtx
5795 to determine whether the function's result will be ignored. */
5796 if (ignore)
5797 target = const0_rtx;
5799 /* If the result of a pure or const built-in function is ignored, and
5800 none of its arguments are volatile, we can avoid expanding the
5801 built-in call and just evaluate the arguments for side-effects. */
5802 if (target == const0_rtx
5803 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5804 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5806 bool volatilep = false;
5807 tree arg;
5808 call_expr_arg_iterator iter;
5810 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5811 if (TREE_THIS_VOLATILE (arg))
5813 volatilep = true;
5814 break;
5817 if (! volatilep)
5819 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5820 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5821 return const0_rtx;
5825 switch (fcode)
5827 CASE_FLT_FN (BUILT_IN_FABS):
5828 case BUILT_IN_FABSD32:
5829 case BUILT_IN_FABSD64:
5830 case BUILT_IN_FABSD128:
5831 target = expand_builtin_fabs (exp, target, subtarget);
5832 if (target)
5833 return target;
5834 break;
5836 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5837 target = expand_builtin_copysign (exp, target, subtarget);
5838 if (target)
5839 return target;
5840 break;
5842 /* Just do a normal library call if we were unable to fold
5843 the values. */
5844 CASE_FLT_FN (BUILT_IN_CABS):
5845 break;
5847 CASE_FLT_FN (BUILT_IN_EXP):
5848 CASE_FLT_FN (BUILT_IN_EXP10):
5849 CASE_FLT_FN (BUILT_IN_POW10):
5850 CASE_FLT_FN (BUILT_IN_EXP2):
5851 CASE_FLT_FN (BUILT_IN_EXPM1):
5852 CASE_FLT_FN (BUILT_IN_LOGB):
5853 CASE_FLT_FN (BUILT_IN_LOG):
5854 CASE_FLT_FN (BUILT_IN_LOG10):
5855 CASE_FLT_FN (BUILT_IN_LOG2):
5856 CASE_FLT_FN (BUILT_IN_LOG1P):
5857 CASE_FLT_FN (BUILT_IN_TAN):
5858 CASE_FLT_FN (BUILT_IN_ASIN):
5859 CASE_FLT_FN (BUILT_IN_ACOS):
5860 CASE_FLT_FN (BUILT_IN_ATAN):
5861 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5862 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5863 because of possible accuracy problems. */
5864 if (! flag_unsafe_math_optimizations)
5865 break;
5866 CASE_FLT_FN (BUILT_IN_SQRT):
5867 CASE_FLT_FN (BUILT_IN_FLOOR):
5868 CASE_FLT_FN (BUILT_IN_CEIL):
5869 CASE_FLT_FN (BUILT_IN_TRUNC):
5870 CASE_FLT_FN (BUILT_IN_ROUND):
5871 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5872 CASE_FLT_FN (BUILT_IN_RINT):
5873 target = expand_builtin_mathfn (exp, target, subtarget);
5874 if (target)
5875 return target;
5876 break;
5878 CASE_FLT_FN (BUILT_IN_FMA):
5879 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5880 if (target)
5881 return target;
5882 break;
5884 CASE_FLT_FN (BUILT_IN_ILOGB):
5885 if (! flag_unsafe_math_optimizations)
5886 break;
5887 CASE_FLT_FN (BUILT_IN_ISINF):
5888 CASE_FLT_FN (BUILT_IN_FINITE):
5889 case BUILT_IN_ISFINITE:
5890 case BUILT_IN_ISNORMAL:
5891 target = expand_builtin_interclass_mathfn (exp, target);
5892 if (target)
5893 return target;
5894 break;
5896 CASE_FLT_FN (BUILT_IN_ICEIL):
5897 CASE_FLT_FN (BUILT_IN_LCEIL):
5898 CASE_FLT_FN (BUILT_IN_LLCEIL):
5899 CASE_FLT_FN (BUILT_IN_LFLOOR):
5900 CASE_FLT_FN (BUILT_IN_IFLOOR):
5901 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5902 target = expand_builtin_int_roundingfn (exp, target);
5903 if (target)
5904 return target;
5905 break;
5907 CASE_FLT_FN (BUILT_IN_IRINT):
5908 CASE_FLT_FN (BUILT_IN_LRINT):
5909 CASE_FLT_FN (BUILT_IN_LLRINT):
5910 CASE_FLT_FN (BUILT_IN_IROUND):
5911 CASE_FLT_FN (BUILT_IN_LROUND):
5912 CASE_FLT_FN (BUILT_IN_LLROUND):
5913 target = expand_builtin_int_roundingfn_2 (exp, target);
5914 if (target)
5915 return target;
5916 break;
5918 CASE_FLT_FN (BUILT_IN_POWI):
5919 target = expand_builtin_powi (exp, target);
5920 if (target)
5921 return target;
5922 break;
5924 CASE_FLT_FN (BUILT_IN_ATAN2):
5925 CASE_FLT_FN (BUILT_IN_LDEXP):
5926 CASE_FLT_FN (BUILT_IN_SCALB):
5927 CASE_FLT_FN (BUILT_IN_SCALBN):
5928 CASE_FLT_FN (BUILT_IN_SCALBLN):
5929 if (! flag_unsafe_math_optimizations)
5930 break;
5932 CASE_FLT_FN (BUILT_IN_FMOD):
5933 CASE_FLT_FN (BUILT_IN_REMAINDER):
5934 CASE_FLT_FN (BUILT_IN_DREM):
5935 CASE_FLT_FN (BUILT_IN_POW):
5936 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5937 if (target)
5938 return target;
5939 break;
5941 CASE_FLT_FN (BUILT_IN_CEXPI):
5942 target = expand_builtin_cexpi (exp, target);
5943 gcc_assert (target);
5944 return target;
5946 CASE_FLT_FN (BUILT_IN_SIN):
5947 CASE_FLT_FN (BUILT_IN_COS):
5948 if (! flag_unsafe_math_optimizations)
5949 break;
5950 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5951 if (target)
5952 return target;
5953 break;
5955 CASE_FLT_FN (BUILT_IN_SINCOS):
5956 if (! flag_unsafe_math_optimizations)
5957 break;
5958 target = expand_builtin_sincos (exp);
5959 if (target)
5960 return target;
5961 break;
5963 case BUILT_IN_APPLY_ARGS:
5964 return expand_builtin_apply_args ();
5966 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5967 FUNCTION with a copy of the parameters described by
5968 ARGUMENTS, and ARGSIZE. It returns a block of memory
5969 allocated on the stack into which is stored all the registers
5970 that might possibly be used for returning the result of a
5971 function. ARGUMENTS is the value returned by
5972 __builtin_apply_args. ARGSIZE is the number of bytes of
5973 arguments that must be copied. ??? How should this value be
5974 computed? We'll also need a safe worst case value for varargs
5975 functions. */
5976 case BUILT_IN_APPLY:
5977 if (!validate_arglist (exp, POINTER_TYPE,
5978 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5979 && !validate_arglist (exp, REFERENCE_TYPE,
5980 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5981 return const0_rtx;
5982 else
5984 rtx ops[3];
5986 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5987 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5988 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5990 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5993 /* __builtin_return (RESULT) causes the function to return the
5994 value described by RESULT. RESULT is address of the block of
5995 memory returned by __builtin_apply. */
5996 case BUILT_IN_RETURN:
5997 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5998 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5999 return const0_rtx;
6001 case BUILT_IN_SAVEREGS:
6002 return expand_builtin_saveregs ();
6004 case BUILT_IN_VA_ARG_PACK:
6005 /* All valid uses of __builtin_va_arg_pack () are removed during
6006 inlining. */
6007 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6008 return const0_rtx;
6010 case BUILT_IN_VA_ARG_PACK_LEN:
6011 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6012 inlining. */
6013 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6014 return const0_rtx;
6016 /* Return the address of the first anonymous stack arg. */
6017 case BUILT_IN_NEXT_ARG:
6018 if (fold_builtin_next_arg (exp, false))
6019 return const0_rtx;
6020 return expand_builtin_next_arg ();
6022 case BUILT_IN_CLEAR_CACHE:
6023 target = expand_builtin___clear_cache (exp);
6024 if (target)
6025 return target;
6026 break;
6028 case BUILT_IN_CLASSIFY_TYPE:
6029 return expand_builtin_classify_type (exp);
6031 case BUILT_IN_CONSTANT_P:
6032 return const0_rtx;
6034 case BUILT_IN_FRAME_ADDRESS:
6035 case BUILT_IN_RETURN_ADDRESS:
6036 return expand_builtin_frame_address (fndecl, exp);
6038 /* Returns the address of the area where the structure is returned.
6039 0 otherwise. */
6040 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6041 if (call_expr_nargs (exp) != 0
6042 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6043 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6044 return const0_rtx;
6045 else
6046 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6048 case BUILT_IN_ALLOCA:
6049 case BUILT_IN_ALLOCA_WITH_ALIGN:
6050 /* If the allocation stems from the declaration of a variable-sized
6051 object, it cannot accumulate. */
6052 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6053 if (target)
6054 return target;
6055 break;
6057 case BUILT_IN_STACK_SAVE:
6058 return expand_stack_save ();
6060 case BUILT_IN_STACK_RESTORE:
6061 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6062 return const0_rtx;
6064 case BUILT_IN_BSWAP16:
6065 case BUILT_IN_BSWAP32:
6066 case BUILT_IN_BSWAP64:
6067 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6068 if (target)
6069 return target;
6070 break;
6072 CASE_INT_FN (BUILT_IN_FFS):
6073 target = expand_builtin_unop (target_mode, exp, target,
6074 subtarget, ffs_optab);
6075 if (target)
6076 return target;
6077 break;
6079 CASE_INT_FN (BUILT_IN_CLZ):
6080 target = expand_builtin_unop (target_mode, exp, target,
6081 subtarget, clz_optab);
6082 if (target)
6083 return target;
6084 break;
6086 CASE_INT_FN (BUILT_IN_CTZ):
6087 target = expand_builtin_unop (target_mode, exp, target,
6088 subtarget, ctz_optab);
6089 if (target)
6090 return target;
6091 break;
6093 CASE_INT_FN (BUILT_IN_CLRSB):
6094 target = expand_builtin_unop (target_mode, exp, target,
6095 subtarget, clrsb_optab);
6096 if (target)
6097 return target;
6098 break;
6100 CASE_INT_FN (BUILT_IN_POPCOUNT):
6101 target = expand_builtin_unop (target_mode, exp, target,
6102 subtarget, popcount_optab);
6103 if (target)
6104 return target;
6105 break;
6107 CASE_INT_FN (BUILT_IN_PARITY):
6108 target = expand_builtin_unop (target_mode, exp, target,
6109 subtarget, parity_optab);
6110 if (target)
6111 return target;
6112 break;
6114 case BUILT_IN_STRLEN:
6115 target = expand_builtin_strlen (exp, target, target_mode);
6116 if (target)
6117 return target;
6118 break;
6120 case BUILT_IN_STRCPY:
6121 target = expand_builtin_strcpy (exp, target);
6122 if (target)
6123 return target;
6124 break;
6126 case BUILT_IN_STRNCPY:
6127 target = expand_builtin_strncpy (exp, target);
6128 if (target)
6129 return target;
6130 break;
6132 case BUILT_IN_STPCPY:
6133 target = expand_builtin_stpcpy (exp, target, mode);
6134 if (target)
6135 return target;
6136 break;
6138 case BUILT_IN_MEMCPY:
6139 target = expand_builtin_memcpy (exp, target);
6140 if (target)
6141 return target;
6142 break;
6144 case BUILT_IN_MEMPCPY:
6145 target = expand_builtin_mempcpy (exp, target, mode);
6146 if (target)
6147 return target;
6148 break;
6150 case BUILT_IN_MEMSET:
6151 target = expand_builtin_memset (exp, target, mode);
6152 if (target)
6153 return target;
6154 break;
6156 case BUILT_IN_BZERO:
6157 target = expand_builtin_bzero (exp);
6158 if (target)
6159 return target;
6160 break;
6162 case BUILT_IN_STRCMP:
6163 target = expand_builtin_strcmp (exp, target);
6164 if (target)
6165 return target;
6166 break;
6168 case BUILT_IN_STRNCMP:
6169 target = expand_builtin_strncmp (exp, target, mode);
6170 if (target)
6171 return target;
6172 break;
6174 case BUILT_IN_BCMP:
6175 case BUILT_IN_MEMCMP:
6176 target = expand_builtin_memcmp (exp, target, mode);
6177 if (target)
6178 return target;
6179 break;
6181 case BUILT_IN_SETJMP:
6182 /* This should have been lowered to the builtins below. */
6183 gcc_unreachable ();
6185 case BUILT_IN_SETJMP_SETUP:
6186 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6187 and the receiver label. */
6188 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6190 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6191 VOIDmode, EXPAND_NORMAL);
6192 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6193 rtx label_r = label_rtx (label);
6195 /* This is copied from the handling of non-local gotos. */
6196 expand_builtin_setjmp_setup (buf_addr, label_r);
6197 nonlocal_goto_handler_labels
6198 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6199 nonlocal_goto_handler_labels);
6200 /* ??? Do not let expand_label treat us as such since we would
6201 not want to be both on the list of non-local labels and on
6202 the list of forced labels. */
6203 FORCED_LABEL (label) = 0;
6204 return const0_rtx;
6206 break;
6208 case BUILT_IN_SETJMP_RECEIVER:
6209 /* __builtin_setjmp_receiver is passed the receiver label. */
6210 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6212 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6213 rtx label_r = label_rtx (label);
6215 expand_builtin_setjmp_receiver (label_r);
6216 return const0_rtx;
6218 break;
6220 /* __builtin_longjmp is passed a pointer to an array of five words.
6221 It's similar to the C library longjmp function but works with
6222 __builtin_setjmp above. */
6223 case BUILT_IN_LONGJMP:
6224 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6226 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6227 VOIDmode, EXPAND_NORMAL);
6228 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6230 if (value != const1_rtx)
6232 error ("%<__builtin_longjmp%> second argument must be 1");
6233 return const0_rtx;
6236 expand_builtin_longjmp (buf_addr, value);
6237 return const0_rtx;
6239 break;
6241 case BUILT_IN_NONLOCAL_GOTO:
6242 target = expand_builtin_nonlocal_goto (exp);
6243 if (target)
6244 return target;
6245 break;
6247 /* This updates the setjmp buffer that is its argument with the value
6248 of the current stack pointer. */
6249 case BUILT_IN_UPDATE_SETJMP_BUF:
6250 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6252 rtx buf_addr
6253 = expand_normal (CALL_EXPR_ARG (exp, 0));
6255 expand_builtin_update_setjmp_buf (buf_addr);
6256 return const0_rtx;
6258 break;
6260 case BUILT_IN_TRAP:
6261 expand_builtin_trap ();
6262 return const0_rtx;
6264 case BUILT_IN_UNREACHABLE:
6265 expand_builtin_unreachable ();
6266 return const0_rtx;
6268 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6269 case BUILT_IN_SIGNBITD32:
6270 case BUILT_IN_SIGNBITD64:
6271 case BUILT_IN_SIGNBITD128:
6272 target = expand_builtin_signbit (exp, target);
6273 if (target)
6274 return target;
6275 break;
6277 /* Various hooks for the DWARF 2 __throw routine. */
6278 case BUILT_IN_UNWIND_INIT:
6279 expand_builtin_unwind_init ();
6280 return const0_rtx;
6281 case BUILT_IN_DWARF_CFA:
6282 return virtual_cfa_rtx;
6283 #ifdef DWARF2_UNWIND_INFO
6284 case BUILT_IN_DWARF_SP_COLUMN:
6285 return expand_builtin_dwarf_sp_column ();
6286 case BUILT_IN_INIT_DWARF_REG_SIZES:
6287 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6288 return const0_rtx;
6289 #endif
6290 case BUILT_IN_FROB_RETURN_ADDR:
6291 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6292 case BUILT_IN_EXTRACT_RETURN_ADDR:
6293 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6294 case BUILT_IN_EH_RETURN:
6295 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6296 CALL_EXPR_ARG (exp, 1));
6297 return const0_rtx;
6298 #ifdef EH_RETURN_DATA_REGNO
6299 case BUILT_IN_EH_RETURN_DATA_REGNO:
6300 return expand_builtin_eh_return_data_regno (exp);
6301 #endif
6302 case BUILT_IN_EXTEND_POINTER:
6303 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6304 case BUILT_IN_EH_POINTER:
6305 return expand_builtin_eh_pointer (exp);
6306 case BUILT_IN_EH_FILTER:
6307 return expand_builtin_eh_filter (exp);
6308 case BUILT_IN_EH_COPY_VALUES:
6309 return expand_builtin_eh_copy_values (exp);
6311 case BUILT_IN_VA_START:
6312 return expand_builtin_va_start (exp);
6313 case BUILT_IN_VA_END:
6314 return expand_builtin_va_end (exp);
6315 case BUILT_IN_VA_COPY:
6316 return expand_builtin_va_copy (exp);
6317 case BUILT_IN_EXPECT:
6318 return expand_builtin_expect (exp, target);
6319 case BUILT_IN_ASSUME_ALIGNED:
6320 return expand_builtin_assume_aligned (exp, target);
6321 case BUILT_IN_PREFETCH:
6322 expand_builtin_prefetch (exp);
6323 return const0_rtx;
6325 case BUILT_IN_INIT_TRAMPOLINE:
6326 return expand_builtin_init_trampoline (exp, true);
6327 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6328 return expand_builtin_init_trampoline (exp, false);
6329 case BUILT_IN_ADJUST_TRAMPOLINE:
6330 return expand_builtin_adjust_trampoline (exp);
6332 case BUILT_IN_FORK:
6333 case BUILT_IN_EXECL:
6334 case BUILT_IN_EXECV:
6335 case BUILT_IN_EXECLP:
6336 case BUILT_IN_EXECLE:
6337 case BUILT_IN_EXECVP:
6338 case BUILT_IN_EXECVE:
6339 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6340 if (target)
6341 return target;
6342 break;
6344 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6345 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6346 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6347 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6348 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6349 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6350 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6351 if (target)
6352 return target;
6353 break;
6355 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6356 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6357 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6358 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6359 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6360 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6361 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6362 if (target)
6363 return target;
6364 break;
6366 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6367 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6368 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6369 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6370 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6371 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6372 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6373 if (target)
6374 return target;
6375 break;
6377 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6378 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6379 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6380 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6381 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6382 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6383 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6384 if (target)
6385 return target;
6386 break;
6388 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6389 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6390 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6391 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6392 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6393 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6394 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6395 if (target)
6396 return target;
6397 break;
6399 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6400 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6401 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6402 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6403 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6404 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6405 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6406 if (target)
6407 return target;
6408 break;
6410 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6411 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6412 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6413 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6414 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6415 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6416 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6417 if (target)
6418 return target;
6419 break;
6421 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6422 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6423 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6424 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6425 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6426 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6427 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6428 if (target)
6429 return target;
6430 break;
6432 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6433 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6434 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6435 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6436 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6437 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6438 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6439 if (target)
6440 return target;
6441 break;
6443 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6444 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6445 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6446 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6447 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6448 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6449 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6450 if (target)
6451 return target;
6452 break;
6454 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6455 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6456 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6457 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6458 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6459 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6460 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6461 if (target)
6462 return target;
6463 break;
6465 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6466 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6467 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6468 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6469 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6470 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6471 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6472 if (target)
6473 return target;
6474 break;
6476 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6477 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6478 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6479 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6480 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6481 if (mode == VOIDmode)
6482 mode = TYPE_MODE (boolean_type_node);
6483 if (!target || !register_operand (target, mode))
6484 target = gen_reg_rtx (mode);
6486 mode = get_builtin_sync_mode
6487 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6488 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6489 if (target)
6490 return target;
6491 break;
6493 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6494 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6495 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6496 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6497 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6498 mode = get_builtin_sync_mode
6499 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6500 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6501 if (target)
6502 return target;
6503 break;
6505 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6506 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6507 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6508 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6509 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6510 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6511 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6512 if (target)
6513 return target;
6514 break;
6516 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6517 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6518 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6519 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6520 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6521 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6522 expand_builtin_sync_lock_release (mode, exp);
6523 return const0_rtx;
6525 case BUILT_IN_SYNC_SYNCHRONIZE:
6526 expand_builtin_sync_synchronize ();
6527 return const0_rtx;
6529 case BUILT_IN_ATOMIC_EXCHANGE_1:
6530 case BUILT_IN_ATOMIC_EXCHANGE_2:
6531 case BUILT_IN_ATOMIC_EXCHANGE_4:
6532 case BUILT_IN_ATOMIC_EXCHANGE_8:
6533 case BUILT_IN_ATOMIC_EXCHANGE_16:
6534 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6535 target = expand_builtin_atomic_exchange (mode, exp, target);
6536 if (target)
6537 return target;
6538 break;
6540 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6541 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6542 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6543 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6544 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6546 unsigned int nargs, z;
6547 vec<tree, va_gc> *vec;
6549 mode =
6550 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6551 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6552 if (target)
6553 return target;
6555 /* If this is turned into an external library call, the weak parameter
6556 must be dropped to match the expected parameter list. */
6557 nargs = call_expr_nargs (exp);
6558 vec_alloc (vec, nargs - 1);
6559 for (z = 0; z < 3; z++)
6560 vec->quick_push (CALL_EXPR_ARG (exp, z));
6561 /* Skip the boolean weak parameter. */
6562 for (z = 4; z < 6; z++)
6563 vec->quick_push (CALL_EXPR_ARG (exp, z));
6564 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6565 break;
6568 case BUILT_IN_ATOMIC_LOAD_1:
6569 case BUILT_IN_ATOMIC_LOAD_2:
6570 case BUILT_IN_ATOMIC_LOAD_4:
6571 case BUILT_IN_ATOMIC_LOAD_8:
6572 case BUILT_IN_ATOMIC_LOAD_16:
6573 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6574 target = expand_builtin_atomic_load (mode, exp, target);
6575 if (target)
6576 return target;
6577 break;
6579 case BUILT_IN_ATOMIC_STORE_1:
6580 case BUILT_IN_ATOMIC_STORE_2:
6581 case BUILT_IN_ATOMIC_STORE_4:
6582 case BUILT_IN_ATOMIC_STORE_8:
6583 case BUILT_IN_ATOMIC_STORE_16:
6584 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6585 target = expand_builtin_atomic_store (mode, exp);
6586 if (target)
6587 return const0_rtx;
6588 break;
6590 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6591 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6592 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6593 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6594 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6596 enum built_in_function lib;
6597 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6598 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6599 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6600 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6601 ignore, lib);
6602 if (target)
6603 return target;
6604 break;
6606 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6607 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6608 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6609 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6610 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6612 enum built_in_function lib;
6613 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6614 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6615 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6616 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6617 ignore, lib);
6618 if (target)
6619 return target;
6620 break;
6622 case BUILT_IN_ATOMIC_AND_FETCH_1:
6623 case BUILT_IN_ATOMIC_AND_FETCH_2:
6624 case BUILT_IN_ATOMIC_AND_FETCH_4:
6625 case BUILT_IN_ATOMIC_AND_FETCH_8:
6626 case BUILT_IN_ATOMIC_AND_FETCH_16:
6628 enum built_in_function lib;
6629 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6630 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6631 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6632 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6633 ignore, lib);
6634 if (target)
6635 return target;
6636 break;
6638 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6639 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6640 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6641 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6642 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6644 enum built_in_function lib;
6645 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6646 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6647 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6648 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6649 ignore, lib);
6650 if (target)
6651 return target;
6652 break;
6654 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6655 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6656 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6657 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6658 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6660 enum built_in_function lib;
6661 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6662 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6663 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6664 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6665 ignore, lib);
6666 if (target)
6667 return target;
6668 break;
6670 case BUILT_IN_ATOMIC_OR_FETCH_1:
6671 case BUILT_IN_ATOMIC_OR_FETCH_2:
6672 case BUILT_IN_ATOMIC_OR_FETCH_4:
6673 case BUILT_IN_ATOMIC_OR_FETCH_8:
6674 case BUILT_IN_ATOMIC_OR_FETCH_16:
6676 enum built_in_function lib;
6677 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6678 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6679 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6680 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6681 ignore, lib);
6682 if (target)
6683 return target;
6684 break;
6686 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6687 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6688 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6689 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6690 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6691 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6692 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6693 ignore, BUILT_IN_NONE);
6694 if (target)
6695 return target;
6696 break;
6698 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6699 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6700 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6701 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6702 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6703 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6704 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6705 ignore, BUILT_IN_NONE);
6706 if (target)
6707 return target;
6708 break;
6710 case BUILT_IN_ATOMIC_FETCH_AND_1:
6711 case BUILT_IN_ATOMIC_FETCH_AND_2:
6712 case BUILT_IN_ATOMIC_FETCH_AND_4:
6713 case BUILT_IN_ATOMIC_FETCH_AND_8:
6714 case BUILT_IN_ATOMIC_FETCH_AND_16:
6715 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6716 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6717 ignore, BUILT_IN_NONE);
6718 if (target)
6719 return target;
6720 break;
6722 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6723 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6724 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6725 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6726 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6727 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6728 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6729 ignore, BUILT_IN_NONE);
6730 if (target)
6731 return target;
6732 break;
6734 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6735 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6736 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6737 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6738 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6739 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6740 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6741 ignore, BUILT_IN_NONE);
6742 if (target)
6743 return target;
6744 break;
6746 case BUILT_IN_ATOMIC_FETCH_OR_1:
6747 case BUILT_IN_ATOMIC_FETCH_OR_2:
6748 case BUILT_IN_ATOMIC_FETCH_OR_4:
6749 case BUILT_IN_ATOMIC_FETCH_OR_8:
6750 case BUILT_IN_ATOMIC_FETCH_OR_16:
6751 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6752 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6753 ignore, BUILT_IN_NONE);
6754 if (target)
6755 return target;
6756 break;
6758 case BUILT_IN_ATOMIC_TEST_AND_SET:
6759 return expand_builtin_atomic_test_and_set (exp, target);
6761 case BUILT_IN_ATOMIC_CLEAR:
6762 return expand_builtin_atomic_clear (exp);
6764 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6765 return expand_builtin_atomic_always_lock_free (exp);
6767 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6768 target = expand_builtin_atomic_is_lock_free (exp);
6769 if (target)
6770 return target;
6771 break;
6773 case BUILT_IN_ATOMIC_THREAD_FENCE:
6774 expand_builtin_atomic_thread_fence (exp);
6775 return const0_rtx;
6777 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6778 expand_builtin_atomic_signal_fence (exp);
6779 return const0_rtx;
6781 case BUILT_IN_OBJECT_SIZE:
6782 return expand_builtin_object_size (exp);
6784 case BUILT_IN_MEMCPY_CHK:
6785 case BUILT_IN_MEMPCPY_CHK:
6786 case BUILT_IN_MEMMOVE_CHK:
6787 case BUILT_IN_MEMSET_CHK:
6788 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6789 if (target)
6790 return target;
6791 break;
6793 case BUILT_IN_STRCPY_CHK:
6794 case BUILT_IN_STPCPY_CHK:
6795 case BUILT_IN_STRNCPY_CHK:
6796 case BUILT_IN_STPNCPY_CHK:
6797 case BUILT_IN_STRCAT_CHK:
6798 case BUILT_IN_STRNCAT_CHK:
6799 case BUILT_IN_SNPRINTF_CHK:
6800 case BUILT_IN_VSNPRINTF_CHK:
6801 maybe_emit_chk_warning (exp, fcode);
6802 break;
6804 case BUILT_IN_SPRINTF_CHK:
6805 case BUILT_IN_VSPRINTF_CHK:
6806 maybe_emit_sprintf_chk_warning (exp, fcode);
6807 break;
6809 case BUILT_IN_FREE:
6810 if (warn_free_nonheap_object)
6811 maybe_emit_free_warning (exp);
6812 break;
6814 case BUILT_IN_THREAD_POINTER:
6815 return expand_builtin_thread_pointer (exp, target);
6817 case BUILT_IN_SET_THREAD_POINTER:
6818 expand_builtin_set_thread_pointer (exp);
6819 return const0_rtx;
6821 case BUILT_IN_CILK_DETACH:
6822 expand_builtin_cilk_detach (exp);
6823 return const0_rtx;
6825 case BUILT_IN_CILK_POP_FRAME:
6826 expand_builtin_cilk_pop_frame (exp);
6827 return const0_rtx;
6829 default: /* just do library call, if unknown builtin */
6830 break;
6833 /* The switch statement above can drop through to cause the function
6834 to be called normally. */
6835 return expand_call (exp, target, ignore);
6838 /* Determine whether a tree node represents a call to a built-in
6839 function. If the tree T is a call to a built-in function with
6840 the right number of arguments of the appropriate types, return
6841 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6842 Otherwise the return value is END_BUILTINS. */
6844 enum built_in_function
6845 builtin_mathfn_code (const_tree t)
6847 const_tree fndecl, arg, parmlist;
6848 const_tree argtype, parmtype;
6849 const_call_expr_arg_iterator iter;
6851 if (TREE_CODE (t) != CALL_EXPR
6852 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6853 return END_BUILTINS;
6855 fndecl = get_callee_fndecl (t);
6856 if (fndecl == NULL_TREE
6857 || TREE_CODE (fndecl) != FUNCTION_DECL
6858 || ! DECL_BUILT_IN (fndecl)
6859 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6860 return END_BUILTINS;
6862 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6863 init_const_call_expr_arg_iterator (t, &iter);
6864 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6866 /* If a function doesn't take a variable number of arguments,
6867 the last element in the list will have type `void'. */
6868 parmtype = TREE_VALUE (parmlist);
6869 if (VOID_TYPE_P (parmtype))
6871 if (more_const_call_expr_args_p (&iter))
6872 return END_BUILTINS;
6873 return DECL_FUNCTION_CODE (fndecl);
6876 if (! more_const_call_expr_args_p (&iter))
6877 return END_BUILTINS;
6879 arg = next_const_call_expr_arg (&iter);
6880 argtype = TREE_TYPE (arg);
6882 if (SCALAR_FLOAT_TYPE_P (parmtype))
6884 if (! SCALAR_FLOAT_TYPE_P (argtype))
6885 return END_BUILTINS;
6887 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6889 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6890 return END_BUILTINS;
6892 else if (POINTER_TYPE_P (parmtype))
6894 if (! POINTER_TYPE_P (argtype))
6895 return END_BUILTINS;
6897 else if (INTEGRAL_TYPE_P (parmtype))
6899 if (! INTEGRAL_TYPE_P (argtype))
6900 return END_BUILTINS;
6902 else
6903 return END_BUILTINS;
6906 /* Variable-length argument list. */
6907 return DECL_FUNCTION_CODE (fndecl);
6910 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6911 evaluate to a constant. */
6913 static tree
6914 fold_builtin_constant_p (tree arg)
6916 /* We return 1 for a numeric type that's known to be a constant
6917 value at compile-time or for an aggregate type that's a
6918 literal constant. */
6919 STRIP_NOPS (arg);
6921 /* If we know this is a constant, emit the constant of one. */
6922 if (CONSTANT_CLASS_P (arg)
6923 || (TREE_CODE (arg) == CONSTRUCTOR
6924 && TREE_CONSTANT (arg)))
6925 return integer_one_node;
6926 if (TREE_CODE (arg) == ADDR_EXPR)
6928 tree op = TREE_OPERAND (arg, 0);
6929 if (TREE_CODE (op) == STRING_CST
6930 || (TREE_CODE (op) == ARRAY_REF
6931 && integer_zerop (TREE_OPERAND (op, 1))
6932 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6933 return integer_one_node;
6936 /* If this expression has side effects, show we don't know it to be a
6937 constant. Likewise if it's a pointer or aggregate type since in
6938 those case we only want literals, since those are only optimized
6939 when generating RTL, not later.
6940 And finally, if we are compiling an initializer, not code, we
6941 need to return a definite result now; there's not going to be any
6942 more optimization done. */
6943 if (TREE_SIDE_EFFECTS (arg)
6944 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6945 || POINTER_TYPE_P (TREE_TYPE (arg))
6946 || cfun == 0
6947 || folding_initializer
6948 || force_folding_builtin_constant_p)
6949 return integer_zero_node;
6951 return NULL_TREE;
6954 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6955 return it as a truthvalue. */
6957 static tree
6958 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6960 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6962 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6963 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6964 ret_type = TREE_TYPE (TREE_TYPE (fn));
6965 pred_type = TREE_VALUE (arg_types);
6966 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6968 pred = fold_convert_loc (loc, pred_type, pred);
6969 expected = fold_convert_loc (loc, expected_type, expected);
6970 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6972 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6973 build_int_cst (ret_type, 0));
6976 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6977 NULL_TREE if no simplification is possible. */
6979 static tree
6980 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6982 tree inner, fndecl, inner_arg0;
6983 enum tree_code code;
6985 /* Distribute the expected value over short-circuiting operators.
6986 See through the cast from truthvalue_type_node to long. */
6987 inner_arg0 = arg0;
6988 while (TREE_CODE (inner_arg0) == NOP_EXPR
6989 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
6990 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
6991 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
6993 /* If this is a builtin_expect within a builtin_expect keep the
6994 inner one. See through a comparison against a constant. It
6995 might have been added to create a thruthvalue. */
6996 inner = inner_arg0;
6998 if (COMPARISON_CLASS_P (inner)
6999 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7000 inner = TREE_OPERAND (inner, 0);
7002 if (TREE_CODE (inner) == CALL_EXPR
7003 && (fndecl = get_callee_fndecl (inner))
7004 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7005 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7006 return arg0;
7008 inner = inner_arg0;
7009 code = TREE_CODE (inner);
7010 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7012 tree op0 = TREE_OPERAND (inner, 0);
7013 tree op1 = TREE_OPERAND (inner, 1);
7015 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7016 op1 = build_builtin_expect_predicate (loc, op1, arg1);
7017 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7019 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7022 /* If the argument isn't invariant then there's nothing else we can do. */
7023 if (!TREE_CONSTANT (inner_arg0))
7024 return NULL_TREE;
7026 /* If we expect that a comparison against the argument will fold to
7027 a constant return the constant. In practice, this means a true
7028 constant or the address of a non-weak symbol. */
7029 inner = inner_arg0;
7030 STRIP_NOPS (inner);
7031 if (TREE_CODE (inner) == ADDR_EXPR)
7035 inner = TREE_OPERAND (inner, 0);
7037 while (TREE_CODE (inner) == COMPONENT_REF
7038 || TREE_CODE (inner) == ARRAY_REF);
7039 if ((TREE_CODE (inner) == VAR_DECL
7040 || TREE_CODE (inner) == FUNCTION_DECL)
7041 && DECL_WEAK (inner))
7042 return NULL_TREE;
7045 /* Otherwise, ARG0 already has the proper type for the return value. */
7046 return arg0;
7049 /* Fold a call to __builtin_classify_type with argument ARG. */
7051 static tree
7052 fold_builtin_classify_type (tree arg)
7054 if (arg == 0)
7055 return build_int_cst (integer_type_node, no_type_class);
7057 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7060 /* Fold a call to __builtin_strlen with argument ARG. */
7062 static tree
7063 fold_builtin_strlen (location_t loc, tree type, tree arg)
7065 if (!validate_arg (arg, POINTER_TYPE))
7066 return NULL_TREE;
7067 else
7069 tree len = c_strlen (arg, 0);
7071 if (len)
7072 return fold_convert_loc (loc, type, len);
7074 return NULL_TREE;
7078 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7080 static tree
7081 fold_builtin_inf (location_t loc, tree type, int warn)
7083 REAL_VALUE_TYPE real;
7085 /* __builtin_inff is intended to be usable to define INFINITY on all
7086 targets. If an infinity is not available, INFINITY expands "to a
7087 positive constant of type float that overflows at translation
7088 time", footnote "In this case, using INFINITY will violate the
7089 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7090 Thus we pedwarn to ensure this constraint violation is
7091 diagnosed. */
7092 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7093 pedwarn (loc, 0, "target format does not support infinity");
7095 real_inf (&real);
7096 return build_real (type, real);
7099 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7101 static tree
7102 fold_builtin_nan (tree arg, tree type, int quiet)
7104 REAL_VALUE_TYPE real;
7105 const char *str;
7107 if (!validate_arg (arg, POINTER_TYPE))
7108 return NULL_TREE;
7109 str = c_getstr (arg);
7110 if (!str)
7111 return NULL_TREE;
7113 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7114 return NULL_TREE;
7116 return build_real (type, real);
7119 /* Return true if the floating point expression T has an integer value.
7120 We also allow +Inf, -Inf and NaN to be considered integer values. */
7122 static bool
7123 integer_valued_real_p (tree t)
7125 switch (TREE_CODE (t))
7127 case FLOAT_EXPR:
7128 return true;
7130 case ABS_EXPR:
7131 case SAVE_EXPR:
7132 return integer_valued_real_p (TREE_OPERAND (t, 0));
7134 case COMPOUND_EXPR:
7135 case MODIFY_EXPR:
7136 case BIND_EXPR:
7137 return integer_valued_real_p (TREE_OPERAND (t, 1));
7139 case PLUS_EXPR:
7140 case MINUS_EXPR:
7141 case MULT_EXPR:
7142 case MIN_EXPR:
7143 case MAX_EXPR:
7144 return integer_valued_real_p (TREE_OPERAND (t, 0))
7145 && integer_valued_real_p (TREE_OPERAND (t, 1));
7147 case COND_EXPR:
7148 return integer_valued_real_p (TREE_OPERAND (t, 1))
7149 && integer_valued_real_p (TREE_OPERAND (t, 2));
7151 case REAL_CST:
7152 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7154 case NOP_EXPR:
7156 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7157 if (TREE_CODE (type) == INTEGER_TYPE)
7158 return true;
7159 if (TREE_CODE (type) == REAL_TYPE)
7160 return integer_valued_real_p (TREE_OPERAND (t, 0));
7161 break;
7164 case CALL_EXPR:
7165 switch (builtin_mathfn_code (t))
7167 CASE_FLT_FN (BUILT_IN_CEIL):
7168 CASE_FLT_FN (BUILT_IN_FLOOR):
7169 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7170 CASE_FLT_FN (BUILT_IN_RINT):
7171 CASE_FLT_FN (BUILT_IN_ROUND):
7172 CASE_FLT_FN (BUILT_IN_TRUNC):
7173 return true;
7175 CASE_FLT_FN (BUILT_IN_FMIN):
7176 CASE_FLT_FN (BUILT_IN_FMAX):
7177 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7178 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7180 default:
7181 break;
7183 break;
7185 default:
7186 break;
7188 return false;
7191 /* FNDECL is assumed to be a builtin where truncation can be propagated
7192 across (for instance floor((double)f) == (double)floorf (f).
7193 Do the transformation for a call with argument ARG. */
7195 static tree
7196 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7198 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7200 if (!validate_arg (arg, REAL_TYPE))
7201 return NULL_TREE;
7203 /* Integer rounding functions are idempotent. */
7204 if (fcode == builtin_mathfn_code (arg))
7205 return arg;
7207 /* If argument is already integer valued, and we don't need to worry
7208 about setting errno, there's no need to perform rounding. */
7209 if (! flag_errno_math && integer_valued_real_p (arg))
7210 return arg;
7212 if (optimize)
7214 tree arg0 = strip_float_extensions (arg);
7215 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7216 tree newtype = TREE_TYPE (arg0);
7217 tree decl;
7219 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7220 && (decl = mathfn_built_in (newtype, fcode)))
7221 return fold_convert_loc (loc, ftype,
7222 build_call_expr_loc (loc, decl, 1,
7223 fold_convert_loc (loc,
7224 newtype,
7225 arg0)));
7227 return NULL_TREE;
7230 /* FNDECL is assumed to be builtin which can narrow the FP type of
7231 the argument, for instance lround((double)f) -> lroundf (f).
7232 Do the transformation for a call with argument ARG. */
7234 static tree
7235 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7237 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7239 if (!validate_arg (arg, REAL_TYPE))
7240 return NULL_TREE;
7242 /* If argument is already integer valued, and we don't need to worry
7243 about setting errno, there's no need to perform rounding. */
7244 if (! flag_errno_math && integer_valued_real_p (arg))
7245 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7246 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7248 if (optimize)
7250 tree ftype = TREE_TYPE (arg);
7251 tree arg0 = strip_float_extensions (arg);
7252 tree newtype = TREE_TYPE (arg0);
7253 tree decl;
7255 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7256 && (decl = mathfn_built_in (newtype, fcode)))
7257 return build_call_expr_loc (loc, decl, 1,
7258 fold_convert_loc (loc, newtype, arg0));
7261 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7262 sizeof (int) == sizeof (long). */
7263 if (TYPE_PRECISION (integer_type_node)
7264 == TYPE_PRECISION (long_integer_type_node))
7266 tree newfn = NULL_TREE;
7267 switch (fcode)
7269 CASE_FLT_FN (BUILT_IN_ICEIL):
7270 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7271 break;
7273 CASE_FLT_FN (BUILT_IN_IFLOOR):
7274 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7275 break;
7277 CASE_FLT_FN (BUILT_IN_IROUND):
7278 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7279 break;
7281 CASE_FLT_FN (BUILT_IN_IRINT):
7282 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7283 break;
7285 default:
7286 break;
7289 if (newfn)
7291 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7292 return fold_convert_loc (loc,
7293 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7297 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7298 sizeof (long long) == sizeof (long). */
7299 if (TYPE_PRECISION (long_long_integer_type_node)
7300 == TYPE_PRECISION (long_integer_type_node))
7302 tree newfn = NULL_TREE;
7303 switch (fcode)
7305 CASE_FLT_FN (BUILT_IN_LLCEIL):
7306 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7307 break;
7309 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7310 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7311 break;
7313 CASE_FLT_FN (BUILT_IN_LLROUND):
7314 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7315 break;
7317 CASE_FLT_FN (BUILT_IN_LLRINT):
7318 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7319 break;
7321 default:
7322 break;
7325 if (newfn)
7327 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7328 return fold_convert_loc (loc,
7329 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7333 return NULL_TREE;
7336 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7337 return type. Return NULL_TREE if no simplification can be made. */
7339 static tree
7340 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7342 tree res;
7344 if (!validate_arg (arg, COMPLEX_TYPE)
7345 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7346 return NULL_TREE;
7348 /* Calculate the result when the argument is a constant. */
7349 if (TREE_CODE (arg) == COMPLEX_CST
7350 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7351 type, mpfr_hypot)))
7352 return res;
7354 if (TREE_CODE (arg) == COMPLEX_EXPR)
7356 tree real = TREE_OPERAND (arg, 0);
7357 tree imag = TREE_OPERAND (arg, 1);
7359 /* If either part is zero, cabs is fabs of the other. */
7360 if (real_zerop (real))
7361 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7362 if (real_zerop (imag))
7363 return fold_build1_loc (loc, ABS_EXPR, type, real);
7365 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7366 if (flag_unsafe_math_optimizations
7367 && operand_equal_p (real, imag, OEP_PURE_SAME))
7369 const REAL_VALUE_TYPE sqrt2_trunc
7370 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7371 STRIP_NOPS (real);
7372 return fold_build2_loc (loc, MULT_EXPR, type,
7373 fold_build1_loc (loc, ABS_EXPR, type, real),
7374 build_real (type, sqrt2_trunc));
7378 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7379 if (TREE_CODE (arg) == NEGATE_EXPR
7380 || TREE_CODE (arg) == CONJ_EXPR)
7381 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7383 /* Don't do this when optimizing for size. */
7384 if (flag_unsafe_math_optimizations
7385 && optimize && optimize_function_for_speed_p (cfun))
7387 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7389 if (sqrtfn != NULL_TREE)
7391 tree rpart, ipart, result;
7393 arg = builtin_save_expr (arg);
7395 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7396 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7398 rpart = builtin_save_expr (rpart);
7399 ipart = builtin_save_expr (ipart);
7401 result = fold_build2_loc (loc, PLUS_EXPR, type,
7402 fold_build2_loc (loc, MULT_EXPR, type,
7403 rpart, rpart),
7404 fold_build2_loc (loc, MULT_EXPR, type,
7405 ipart, ipart));
7407 return build_call_expr_loc (loc, sqrtfn, 1, result);
7411 return NULL_TREE;
7414 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7415 complex tree type of the result. If NEG is true, the imaginary
7416 zero is negative. */
7418 static tree
7419 build_complex_cproj (tree type, bool neg)
7421 REAL_VALUE_TYPE rinf, rzero = dconst0;
7423 real_inf (&rinf);
7424 rzero.sign = neg;
7425 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7426 build_real (TREE_TYPE (type), rzero));
7429 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7430 return type. Return NULL_TREE if no simplification can be made. */
7432 static tree
7433 fold_builtin_cproj (location_t loc, tree arg, tree type)
7435 if (!validate_arg (arg, COMPLEX_TYPE)
7436 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7437 return NULL_TREE;
7439 /* If there are no infinities, return arg. */
7440 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7441 return non_lvalue_loc (loc, arg);
7443 /* Calculate the result when the argument is a constant. */
7444 if (TREE_CODE (arg) == COMPLEX_CST)
7446 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7447 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7449 if (real_isinf (real) || real_isinf (imag))
7450 return build_complex_cproj (type, imag->sign);
7451 else
7452 return arg;
7454 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7456 tree real = TREE_OPERAND (arg, 0);
7457 tree imag = TREE_OPERAND (arg, 1);
7459 STRIP_NOPS (real);
7460 STRIP_NOPS (imag);
7462 /* If the real part is inf and the imag part is known to be
7463 nonnegative, return (inf + 0i). Remember side-effects are
7464 possible in the imag part. */
7465 if (TREE_CODE (real) == REAL_CST
7466 && real_isinf (TREE_REAL_CST_PTR (real))
7467 && tree_expr_nonnegative_p (imag))
7468 return omit_one_operand_loc (loc, type,
7469 build_complex_cproj (type, false),
7470 arg);
7472 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7473 Remember side-effects are possible in the real part. */
7474 if (TREE_CODE (imag) == REAL_CST
7475 && real_isinf (TREE_REAL_CST_PTR (imag)))
7476 return
7477 omit_one_operand_loc (loc, type,
7478 build_complex_cproj (type, TREE_REAL_CST_PTR
7479 (imag)->sign), arg);
7482 return NULL_TREE;
7485 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7486 Return NULL_TREE if no simplification can be made. */
7488 static tree
7489 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7492 enum built_in_function fcode;
7493 tree res;
7495 if (!validate_arg (arg, REAL_TYPE))
7496 return NULL_TREE;
7498 /* Calculate the result when the argument is a constant. */
7499 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7500 return res;
7502 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7503 fcode = builtin_mathfn_code (arg);
7504 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7506 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7507 arg = fold_build2_loc (loc, MULT_EXPR, type,
7508 CALL_EXPR_ARG (arg, 0),
7509 build_real (type, dconsthalf));
7510 return build_call_expr_loc (loc, expfn, 1, arg);
7513 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7514 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7516 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7518 if (powfn)
7520 tree arg0 = CALL_EXPR_ARG (arg, 0);
7521 tree tree_root;
7522 /* The inner root was either sqrt or cbrt. */
7523 /* This was a conditional expression but it triggered a bug
7524 in Sun C 5.5. */
7525 REAL_VALUE_TYPE dconstroot;
7526 if (BUILTIN_SQRT_P (fcode))
7527 dconstroot = dconsthalf;
7528 else
7529 dconstroot = dconst_third ();
7531 /* Adjust for the outer root. */
7532 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7533 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7534 tree_root = build_real (type, dconstroot);
7535 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7539 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7540 if (flag_unsafe_math_optimizations
7541 && (fcode == BUILT_IN_POW
7542 || fcode == BUILT_IN_POWF
7543 || fcode == BUILT_IN_POWL))
7545 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7546 tree arg0 = CALL_EXPR_ARG (arg, 0);
7547 tree arg1 = CALL_EXPR_ARG (arg, 1);
7548 tree narg1;
7549 if (!tree_expr_nonnegative_p (arg0))
7550 arg0 = build1 (ABS_EXPR, type, arg0);
7551 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7552 build_real (type, dconsthalf));
7553 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7556 return NULL_TREE;
7559 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7560 Return NULL_TREE if no simplification can be made. */
7562 static tree
7563 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7565 const enum built_in_function fcode = builtin_mathfn_code (arg);
7566 tree res;
7568 if (!validate_arg (arg, REAL_TYPE))
7569 return NULL_TREE;
7571 /* Calculate the result when the argument is a constant. */
7572 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7573 return res;
7575 if (flag_unsafe_math_optimizations)
7577 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7578 if (BUILTIN_EXPONENT_P (fcode))
7580 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7581 const REAL_VALUE_TYPE third_trunc =
7582 real_value_truncate (TYPE_MODE (type), dconst_third ());
7583 arg = fold_build2_loc (loc, MULT_EXPR, type,
7584 CALL_EXPR_ARG (arg, 0),
7585 build_real (type, third_trunc));
7586 return build_call_expr_loc (loc, expfn, 1, arg);
7589 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7590 if (BUILTIN_SQRT_P (fcode))
7592 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7594 if (powfn)
7596 tree arg0 = CALL_EXPR_ARG (arg, 0);
7597 tree tree_root;
7598 REAL_VALUE_TYPE dconstroot = dconst_third ();
7600 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7601 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7602 tree_root = build_real (type, dconstroot);
7603 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7607 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7608 if (BUILTIN_CBRT_P (fcode))
7610 tree arg0 = CALL_EXPR_ARG (arg, 0);
7611 if (tree_expr_nonnegative_p (arg0))
7613 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7615 if (powfn)
7617 tree tree_root;
7618 REAL_VALUE_TYPE dconstroot;
7620 real_arithmetic (&dconstroot, MULT_EXPR,
7621 dconst_third_ptr (), dconst_third_ptr ());
7622 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7623 tree_root = build_real (type, dconstroot);
7624 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7629 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7630 if (fcode == BUILT_IN_POW
7631 || fcode == BUILT_IN_POWF
7632 || fcode == BUILT_IN_POWL)
7634 tree arg00 = CALL_EXPR_ARG (arg, 0);
7635 tree arg01 = CALL_EXPR_ARG (arg, 1);
7636 if (tree_expr_nonnegative_p (arg00))
7638 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7639 const REAL_VALUE_TYPE dconstroot
7640 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7641 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7642 build_real (type, dconstroot));
7643 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7647 return NULL_TREE;
7650 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7651 TYPE is the type of the return value. Return NULL_TREE if no
7652 simplification can be made. */
7654 static tree
7655 fold_builtin_cos (location_t loc,
7656 tree arg, tree type, tree fndecl)
7658 tree res, narg;
7660 if (!validate_arg (arg, REAL_TYPE))
7661 return NULL_TREE;
7663 /* Calculate the result when the argument is a constant. */
7664 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7665 return res;
7667 /* Optimize cos(-x) into cos (x). */
7668 if ((narg = fold_strip_sign_ops (arg)))
7669 return build_call_expr_loc (loc, fndecl, 1, narg);
7671 return NULL_TREE;
7674 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7675 Return NULL_TREE if no simplification can be made. */
7677 static tree
7678 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7680 if (validate_arg (arg, REAL_TYPE))
7682 tree res, narg;
7684 /* Calculate the result when the argument is a constant. */
7685 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7686 return res;
7688 /* Optimize cosh(-x) into cosh (x). */
7689 if ((narg = fold_strip_sign_ops (arg)))
7690 return build_call_expr_loc (loc, fndecl, 1, narg);
7693 return NULL_TREE;
7696 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7697 argument ARG. TYPE is the type of the return value. Return
7698 NULL_TREE if no simplification can be made. */
7700 static tree
7701 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7702 bool hyper)
7704 if (validate_arg (arg, COMPLEX_TYPE)
7705 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7707 tree tmp;
7709 /* Calculate the result when the argument is a constant. */
7710 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7711 return tmp;
7713 /* Optimize fn(-x) into fn(x). */
7714 if ((tmp = fold_strip_sign_ops (arg)))
7715 return build_call_expr_loc (loc, fndecl, 1, tmp);
7718 return NULL_TREE;
7721 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7722 Return NULL_TREE if no simplification can be made. */
7724 static tree
7725 fold_builtin_tan (tree arg, tree type)
7727 enum built_in_function fcode;
7728 tree res;
7730 if (!validate_arg (arg, REAL_TYPE))
7731 return NULL_TREE;
7733 /* Calculate the result when the argument is a constant. */
7734 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7735 return res;
7737 /* Optimize tan(atan(x)) = x. */
7738 fcode = builtin_mathfn_code (arg);
7739 if (flag_unsafe_math_optimizations
7740 && (fcode == BUILT_IN_ATAN
7741 || fcode == BUILT_IN_ATANF
7742 || fcode == BUILT_IN_ATANL))
7743 return CALL_EXPR_ARG (arg, 0);
7745 return NULL_TREE;
7748 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7749 NULL_TREE if no simplification can be made. */
7751 static tree
7752 fold_builtin_sincos (location_t loc,
7753 tree arg0, tree arg1, tree arg2)
7755 tree type;
7756 tree res, fn, call;
7758 if (!validate_arg (arg0, REAL_TYPE)
7759 || !validate_arg (arg1, POINTER_TYPE)
7760 || !validate_arg (arg2, POINTER_TYPE))
7761 return NULL_TREE;
7763 type = TREE_TYPE (arg0);
7765 /* Calculate the result when the argument is a constant. */
7766 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7767 return res;
7769 /* Canonicalize sincos to cexpi. */
7770 if (!targetm.libc_has_function (function_c99_math_complex))
7771 return NULL_TREE;
7772 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7773 if (!fn)
7774 return NULL_TREE;
7776 call = build_call_expr_loc (loc, fn, 1, arg0);
7777 call = builtin_save_expr (call);
7779 return build2 (COMPOUND_EXPR, void_type_node,
7780 build2 (MODIFY_EXPR, void_type_node,
7781 build_fold_indirect_ref_loc (loc, arg1),
7782 build1 (IMAGPART_EXPR, type, call)),
7783 build2 (MODIFY_EXPR, void_type_node,
7784 build_fold_indirect_ref_loc (loc, arg2),
7785 build1 (REALPART_EXPR, type, call)));
7788 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7789 NULL_TREE if no simplification can be made. */
7791 static tree
7792 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7794 tree rtype;
7795 tree realp, imagp, ifn;
7796 tree res;
7798 if (!validate_arg (arg0, COMPLEX_TYPE)
7799 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7800 return NULL_TREE;
7802 /* Calculate the result when the argument is a constant. */
7803 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7804 return res;
7806 rtype = TREE_TYPE (TREE_TYPE (arg0));
7808 /* In case we can figure out the real part of arg0 and it is constant zero
7809 fold to cexpi. */
7810 if (!targetm.libc_has_function (function_c99_math_complex))
7811 return NULL_TREE;
7812 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7813 if (!ifn)
7814 return NULL_TREE;
7816 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7817 && real_zerop (realp))
7819 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7820 return build_call_expr_loc (loc, ifn, 1, narg);
7823 /* In case we can easily decompose real and imaginary parts split cexp
7824 to exp (r) * cexpi (i). */
7825 if (flag_unsafe_math_optimizations
7826 && realp)
7828 tree rfn, rcall, icall;
7830 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7831 if (!rfn)
7832 return NULL_TREE;
7834 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7835 if (!imagp)
7836 return NULL_TREE;
7838 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7839 icall = builtin_save_expr (icall);
7840 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7841 rcall = builtin_save_expr (rcall);
7842 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7843 fold_build2_loc (loc, MULT_EXPR, rtype,
7844 rcall,
7845 fold_build1_loc (loc, REALPART_EXPR,
7846 rtype, icall)),
7847 fold_build2_loc (loc, MULT_EXPR, rtype,
7848 rcall,
7849 fold_build1_loc (loc, IMAGPART_EXPR,
7850 rtype, icall)));
7853 return NULL_TREE;
7856 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7857 Return NULL_TREE if no simplification can be made. */
7859 static tree
7860 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7862 if (!validate_arg (arg, REAL_TYPE))
7863 return NULL_TREE;
7865 /* Optimize trunc of constant value. */
7866 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7868 REAL_VALUE_TYPE r, x;
7869 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7871 x = TREE_REAL_CST (arg);
7872 real_trunc (&r, TYPE_MODE (type), &x);
7873 return build_real (type, r);
7876 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7879 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7880 Return NULL_TREE if no simplification can be made. */
7882 static tree
7883 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7885 if (!validate_arg (arg, REAL_TYPE))
7886 return NULL_TREE;
7888 /* Optimize floor of constant value. */
7889 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7891 REAL_VALUE_TYPE x;
7893 x = TREE_REAL_CST (arg);
7894 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7896 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7897 REAL_VALUE_TYPE r;
7899 real_floor (&r, TYPE_MODE (type), &x);
7900 return build_real (type, r);
7904 /* Fold floor (x) where x is nonnegative to trunc (x). */
7905 if (tree_expr_nonnegative_p (arg))
7907 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7908 if (truncfn)
7909 return build_call_expr_loc (loc, truncfn, 1, arg);
7912 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7915 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7916 Return NULL_TREE if no simplification can be made. */
7918 static tree
7919 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7921 if (!validate_arg (arg, REAL_TYPE))
7922 return NULL_TREE;
7924 /* Optimize ceil of constant value. */
7925 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7927 REAL_VALUE_TYPE x;
7929 x = TREE_REAL_CST (arg);
7930 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7932 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7933 REAL_VALUE_TYPE r;
7935 real_ceil (&r, TYPE_MODE (type), &x);
7936 return build_real (type, r);
7940 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7943 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7944 Return NULL_TREE if no simplification can be made. */
7946 static tree
7947 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7949 if (!validate_arg (arg, REAL_TYPE))
7950 return NULL_TREE;
7952 /* Optimize round of constant value. */
7953 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7955 REAL_VALUE_TYPE x;
7957 x = TREE_REAL_CST (arg);
7958 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7960 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7961 REAL_VALUE_TYPE r;
7963 real_round (&r, TYPE_MODE (type), &x);
7964 return build_real (type, r);
7968 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7971 /* Fold function call to builtin lround, lroundf or lroundl (or the
7972 corresponding long long versions) and other rounding functions. ARG
7973 is the argument to the call. Return NULL_TREE if no simplification
7974 can be made. */
7976 static tree
7977 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7979 if (!validate_arg (arg, REAL_TYPE))
7980 return NULL_TREE;
7982 /* Optimize lround of constant value. */
7983 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7985 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7987 if (real_isfinite (&x))
7989 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7990 tree ftype = TREE_TYPE (arg);
7991 double_int val;
7992 REAL_VALUE_TYPE r;
7994 switch (DECL_FUNCTION_CODE (fndecl))
7996 CASE_FLT_FN (BUILT_IN_IFLOOR):
7997 CASE_FLT_FN (BUILT_IN_LFLOOR):
7998 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7999 real_floor (&r, TYPE_MODE (ftype), &x);
8000 break;
8002 CASE_FLT_FN (BUILT_IN_ICEIL):
8003 CASE_FLT_FN (BUILT_IN_LCEIL):
8004 CASE_FLT_FN (BUILT_IN_LLCEIL):
8005 real_ceil (&r, TYPE_MODE (ftype), &x);
8006 break;
8008 CASE_FLT_FN (BUILT_IN_IROUND):
8009 CASE_FLT_FN (BUILT_IN_LROUND):
8010 CASE_FLT_FN (BUILT_IN_LLROUND):
8011 real_round (&r, TYPE_MODE (ftype), &x);
8012 break;
8014 default:
8015 gcc_unreachable ();
8018 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8019 if (double_int_fits_to_tree_p (itype, val))
8020 return double_int_to_tree (itype, val);
8024 switch (DECL_FUNCTION_CODE (fndecl))
8026 CASE_FLT_FN (BUILT_IN_LFLOOR):
8027 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8028 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8029 if (tree_expr_nonnegative_p (arg))
8030 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8031 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8032 break;
8033 default:;
8036 return fold_fixed_mathfn (loc, fndecl, arg);
8039 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8040 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8041 the argument to the call. Return NULL_TREE if no simplification can
8042 be made. */
8044 static tree
8045 fold_builtin_bitop (tree fndecl, tree arg)
8047 if (!validate_arg (arg, INTEGER_TYPE))
8048 return NULL_TREE;
8050 /* Optimize for constant argument. */
8051 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8053 HOST_WIDE_INT hi, width, result;
8054 unsigned HOST_WIDE_INT lo;
8055 tree type;
8057 type = TREE_TYPE (arg);
8058 width = TYPE_PRECISION (type);
8059 lo = TREE_INT_CST_LOW (arg);
8061 /* Clear all the bits that are beyond the type's precision. */
8062 if (width > HOST_BITS_PER_WIDE_INT)
8064 hi = TREE_INT_CST_HIGH (arg);
8065 if (width < HOST_BITS_PER_DOUBLE_INT)
8066 hi &= ~(HOST_WIDE_INT_M1U << (width - HOST_BITS_PER_WIDE_INT));
8068 else
8070 hi = 0;
8071 if (width < HOST_BITS_PER_WIDE_INT)
8072 lo &= ~(HOST_WIDE_INT_M1U << width);
8075 switch (DECL_FUNCTION_CODE (fndecl))
8077 CASE_INT_FN (BUILT_IN_FFS):
8078 if (lo != 0)
8079 result = ffs_hwi (lo);
8080 else if (hi != 0)
8081 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8082 else
8083 result = 0;
8084 break;
8086 CASE_INT_FN (BUILT_IN_CLZ):
8087 if (hi != 0)
8088 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8089 else if (lo != 0)
8090 result = width - floor_log2 (lo) - 1;
8091 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8092 result = width;
8093 break;
8095 CASE_INT_FN (BUILT_IN_CTZ):
8096 if (lo != 0)
8097 result = ctz_hwi (lo);
8098 else if (hi != 0)
8099 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8100 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8101 result = width;
8102 break;
8104 CASE_INT_FN (BUILT_IN_CLRSB):
8105 if (width > 2 * HOST_BITS_PER_WIDE_INT)
8106 return NULL_TREE;
8107 if (width > HOST_BITS_PER_WIDE_INT
8108 && (hi & ((unsigned HOST_WIDE_INT) 1
8109 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8111 hi = ~hi & ~(HOST_WIDE_INT_M1U
8112 << (width - HOST_BITS_PER_WIDE_INT - 1));
8113 lo = ~lo;
8115 else if (width <= HOST_BITS_PER_WIDE_INT
8116 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8117 lo = ~lo & ~(HOST_WIDE_INT_M1U << (width - 1));
8118 if (hi != 0)
8119 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8120 else if (lo != 0)
8121 result = width - floor_log2 (lo) - 2;
8122 else
8123 result = width - 1;
8124 break;
8126 CASE_INT_FN (BUILT_IN_POPCOUNT):
8127 result = 0;
8128 while (lo)
8129 result++, lo &= lo - 1;
8130 while (hi)
8131 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8132 break;
8134 CASE_INT_FN (BUILT_IN_PARITY):
8135 result = 0;
8136 while (lo)
8137 result++, lo &= lo - 1;
8138 while (hi)
8139 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8140 result &= 1;
8141 break;
8143 default:
8144 gcc_unreachable ();
8147 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8150 return NULL_TREE;
8153 /* Fold function call to builtin_bswap and the short, long and long long
8154 variants. Return NULL_TREE if no simplification can be made. */
8155 static tree
8156 fold_builtin_bswap (tree fndecl, tree arg)
8158 if (! validate_arg (arg, INTEGER_TYPE))
8159 return NULL_TREE;
8161 /* Optimize constant value. */
8162 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8164 HOST_WIDE_INT hi, width, r_hi = 0;
8165 unsigned HOST_WIDE_INT lo, r_lo = 0;
8166 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8168 width = TYPE_PRECISION (type);
8169 lo = TREE_INT_CST_LOW (arg);
8170 hi = TREE_INT_CST_HIGH (arg);
8172 switch (DECL_FUNCTION_CODE (fndecl))
8174 case BUILT_IN_BSWAP16:
8175 case BUILT_IN_BSWAP32:
8176 case BUILT_IN_BSWAP64:
8178 int s;
8180 for (s = 0; s < width; s += 8)
8182 int d = width - s - 8;
8183 unsigned HOST_WIDE_INT byte;
8185 if (s < HOST_BITS_PER_WIDE_INT)
8186 byte = (lo >> s) & 0xff;
8187 else
8188 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8190 if (d < HOST_BITS_PER_WIDE_INT)
8191 r_lo |= byte << d;
8192 else
8193 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8197 break;
8199 default:
8200 gcc_unreachable ();
8203 if (width < HOST_BITS_PER_WIDE_INT)
8204 return build_int_cst (type, r_lo);
8205 else
8206 return build_int_cst_wide (type, r_lo, r_hi);
8209 return NULL_TREE;
8212 /* A subroutine of fold_builtin to fold the various logarithmic
8213 functions. Return NULL_TREE if no simplification can me made.
8214 FUNC is the corresponding MPFR logarithm function. */
8216 static tree
8217 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8218 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8220 if (validate_arg (arg, REAL_TYPE))
8222 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8223 tree res;
8224 const enum built_in_function fcode = builtin_mathfn_code (arg);
8226 /* Calculate the result when the argument is a constant. */
8227 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8228 return res;
8230 /* Special case, optimize logN(expN(x)) = x. */
8231 if (flag_unsafe_math_optimizations
8232 && ((func == mpfr_log
8233 && (fcode == BUILT_IN_EXP
8234 || fcode == BUILT_IN_EXPF
8235 || fcode == BUILT_IN_EXPL))
8236 || (func == mpfr_log2
8237 && (fcode == BUILT_IN_EXP2
8238 || fcode == BUILT_IN_EXP2F
8239 || fcode == BUILT_IN_EXP2L))
8240 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8241 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8243 /* Optimize logN(func()) for various exponential functions. We
8244 want to determine the value "x" and the power "exponent" in
8245 order to transform logN(x**exponent) into exponent*logN(x). */
8246 if (flag_unsafe_math_optimizations)
8248 tree exponent = 0, x = 0;
8250 switch (fcode)
8252 CASE_FLT_FN (BUILT_IN_EXP):
8253 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8254 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8255 dconst_e ()));
8256 exponent = CALL_EXPR_ARG (arg, 0);
8257 break;
8258 CASE_FLT_FN (BUILT_IN_EXP2):
8259 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8260 x = build_real (type, dconst2);
8261 exponent = CALL_EXPR_ARG (arg, 0);
8262 break;
8263 CASE_FLT_FN (BUILT_IN_EXP10):
8264 CASE_FLT_FN (BUILT_IN_POW10):
8265 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8267 REAL_VALUE_TYPE dconst10;
8268 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8269 x = build_real (type, dconst10);
8271 exponent = CALL_EXPR_ARG (arg, 0);
8272 break;
8273 CASE_FLT_FN (BUILT_IN_SQRT):
8274 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8275 x = CALL_EXPR_ARG (arg, 0);
8276 exponent = build_real (type, dconsthalf);
8277 break;
8278 CASE_FLT_FN (BUILT_IN_CBRT):
8279 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8280 x = CALL_EXPR_ARG (arg, 0);
8281 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8282 dconst_third ()));
8283 break;
8284 CASE_FLT_FN (BUILT_IN_POW):
8285 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8286 x = CALL_EXPR_ARG (arg, 0);
8287 exponent = CALL_EXPR_ARG (arg, 1);
8288 break;
8289 default:
8290 break;
8293 /* Now perform the optimization. */
8294 if (x && exponent)
8296 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8297 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8302 return NULL_TREE;
8305 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8306 NULL_TREE if no simplification can be made. */
8308 static tree
8309 fold_builtin_hypot (location_t loc, tree fndecl,
8310 tree arg0, tree arg1, tree type)
8312 tree res, narg0, narg1;
8314 if (!validate_arg (arg0, REAL_TYPE)
8315 || !validate_arg (arg1, REAL_TYPE))
8316 return NULL_TREE;
8318 /* Calculate the result when the argument is a constant. */
8319 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8320 return res;
8322 /* If either argument to hypot has a negate or abs, strip that off.
8323 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8324 narg0 = fold_strip_sign_ops (arg0);
8325 narg1 = fold_strip_sign_ops (arg1);
8326 if (narg0 || narg1)
8328 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8329 narg1 ? narg1 : arg1);
8332 /* If either argument is zero, hypot is fabs of the other. */
8333 if (real_zerop (arg0))
8334 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8335 else if (real_zerop (arg1))
8336 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8338 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8339 if (flag_unsafe_math_optimizations
8340 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8342 const REAL_VALUE_TYPE sqrt2_trunc
8343 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8344 return fold_build2_loc (loc, MULT_EXPR, type,
8345 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8346 build_real (type, sqrt2_trunc));
8349 return NULL_TREE;
8353 /* Fold a builtin function call to pow, powf, or powl. Return
8354 NULL_TREE if no simplification can be made. */
8355 static tree
8356 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8358 tree res;
8360 if (!validate_arg (arg0, REAL_TYPE)
8361 || !validate_arg (arg1, REAL_TYPE))
8362 return NULL_TREE;
8364 /* Calculate the result when the argument is a constant. */
8365 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8366 return res;
8368 /* Optimize pow(1.0,y) = 1.0. */
8369 if (real_onep (arg0))
8370 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8372 if (TREE_CODE (arg1) == REAL_CST
8373 && !TREE_OVERFLOW (arg1))
8375 REAL_VALUE_TYPE cint;
8376 REAL_VALUE_TYPE c;
8377 HOST_WIDE_INT n;
8379 c = TREE_REAL_CST (arg1);
8381 /* Optimize pow(x,0.0) = 1.0. */
8382 if (REAL_VALUES_EQUAL (c, dconst0))
8383 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8384 arg0);
8386 /* Optimize pow(x,1.0) = x. */
8387 if (REAL_VALUES_EQUAL (c, dconst1))
8388 return arg0;
8390 /* Optimize pow(x,-1.0) = 1.0/x. */
8391 if (REAL_VALUES_EQUAL (c, dconstm1))
8392 return fold_build2_loc (loc, RDIV_EXPR, type,
8393 build_real (type, dconst1), arg0);
8395 /* Optimize pow(x,0.5) = sqrt(x). */
8396 if (flag_unsafe_math_optimizations
8397 && REAL_VALUES_EQUAL (c, dconsthalf))
8399 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8401 if (sqrtfn != NULL_TREE)
8402 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8405 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8406 if (flag_unsafe_math_optimizations)
8408 const REAL_VALUE_TYPE dconstroot
8409 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8411 if (REAL_VALUES_EQUAL (c, dconstroot))
8413 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8414 if (cbrtfn != NULL_TREE)
8415 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8419 /* Check for an integer exponent. */
8420 n = real_to_integer (&c);
8421 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8422 if (real_identical (&c, &cint))
8424 /* Attempt to evaluate pow at compile-time, unless this should
8425 raise an exception. */
8426 if (TREE_CODE (arg0) == REAL_CST
8427 && !TREE_OVERFLOW (arg0)
8428 && (n > 0
8429 || (!flag_trapping_math && !flag_errno_math)
8430 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8432 REAL_VALUE_TYPE x;
8433 bool inexact;
8435 x = TREE_REAL_CST (arg0);
8436 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8437 if (flag_unsafe_math_optimizations || !inexact)
8438 return build_real (type, x);
8441 /* Strip sign ops from even integer powers. */
8442 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8444 tree narg0 = fold_strip_sign_ops (arg0);
8445 if (narg0)
8446 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8451 if (flag_unsafe_math_optimizations)
8453 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8455 /* Optimize pow(expN(x),y) = expN(x*y). */
8456 if (BUILTIN_EXPONENT_P (fcode))
8458 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8459 tree arg = CALL_EXPR_ARG (arg0, 0);
8460 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8461 return build_call_expr_loc (loc, expfn, 1, arg);
8464 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8465 if (BUILTIN_SQRT_P (fcode))
8467 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8468 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8469 build_real (type, dconsthalf));
8470 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8473 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8474 if (BUILTIN_CBRT_P (fcode))
8476 tree arg = CALL_EXPR_ARG (arg0, 0);
8477 if (tree_expr_nonnegative_p (arg))
8479 const REAL_VALUE_TYPE dconstroot
8480 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8481 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8482 build_real (type, dconstroot));
8483 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8487 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8488 if (fcode == BUILT_IN_POW
8489 || fcode == BUILT_IN_POWF
8490 || fcode == BUILT_IN_POWL)
8492 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8493 if (tree_expr_nonnegative_p (arg00))
8495 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8496 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8497 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8502 return NULL_TREE;
8505 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8506 Return NULL_TREE if no simplification can be made. */
8507 static tree
8508 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8509 tree arg0, tree arg1, tree type)
8511 if (!validate_arg (arg0, REAL_TYPE)
8512 || !validate_arg (arg1, INTEGER_TYPE))
8513 return NULL_TREE;
8515 /* Optimize pow(1.0,y) = 1.0. */
8516 if (real_onep (arg0))
8517 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8519 if (tree_fits_shwi_p (arg1))
8521 HOST_WIDE_INT c = tree_to_shwi (arg1);
8523 /* Evaluate powi at compile-time. */
8524 if (TREE_CODE (arg0) == REAL_CST
8525 && !TREE_OVERFLOW (arg0))
8527 REAL_VALUE_TYPE x;
8528 x = TREE_REAL_CST (arg0);
8529 real_powi (&x, TYPE_MODE (type), &x, c);
8530 return build_real (type, x);
8533 /* Optimize pow(x,0) = 1.0. */
8534 if (c == 0)
8535 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8536 arg0);
8538 /* Optimize pow(x,1) = x. */
8539 if (c == 1)
8540 return arg0;
8542 /* Optimize pow(x,-1) = 1.0/x. */
8543 if (c == -1)
8544 return fold_build2_loc (loc, RDIV_EXPR, type,
8545 build_real (type, dconst1), arg0);
8548 return NULL_TREE;
8551 /* A subroutine of fold_builtin to fold the various exponent
8552 functions. Return NULL_TREE if no simplification can be made.
8553 FUNC is the corresponding MPFR exponent function. */
8555 static tree
8556 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8557 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8559 if (validate_arg (arg, REAL_TYPE))
8561 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8562 tree res;
8564 /* Calculate the result when the argument is a constant. */
8565 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8566 return res;
8568 /* Optimize expN(logN(x)) = x. */
8569 if (flag_unsafe_math_optimizations)
8571 const enum built_in_function fcode = builtin_mathfn_code (arg);
8573 if ((func == mpfr_exp
8574 && (fcode == BUILT_IN_LOG
8575 || fcode == BUILT_IN_LOGF
8576 || fcode == BUILT_IN_LOGL))
8577 || (func == mpfr_exp2
8578 && (fcode == BUILT_IN_LOG2
8579 || fcode == BUILT_IN_LOG2F
8580 || fcode == BUILT_IN_LOG2L))
8581 || (func == mpfr_exp10
8582 && (fcode == BUILT_IN_LOG10
8583 || fcode == BUILT_IN_LOG10F
8584 || fcode == BUILT_IN_LOG10L)))
8585 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8589 return NULL_TREE;
8592 /* Return true if VAR is a VAR_DECL or a component thereof. */
8594 static bool
8595 var_decl_component_p (tree var)
8597 tree inner = var;
8598 while (handled_component_p (inner))
8599 inner = TREE_OPERAND (inner, 0);
8600 return SSA_VAR_P (inner);
8603 /* Fold function call to builtin memset. Return
8604 NULL_TREE if no simplification can be made. */
8606 static tree
8607 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8608 tree type, bool ignore)
8610 tree var, ret, etype;
8611 unsigned HOST_WIDE_INT length, cval;
8613 if (! validate_arg (dest, POINTER_TYPE)
8614 || ! validate_arg (c, INTEGER_TYPE)
8615 || ! validate_arg (len, INTEGER_TYPE))
8616 return NULL_TREE;
8618 if (! tree_fits_uhwi_p (len))
8619 return NULL_TREE;
8621 /* If the LEN parameter is zero, return DEST. */
8622 if (integer_zerop (len))
8623 return omit_one_operand_loc (loc, type, dest, c);
8625 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8626 return NULL_TREE;
8628 var = dest;
8629 STRIP_NOPS (var);
8630 if (TREE_CODE (var) != ADDR_EXPR)
8631 return NULL_TREE;
8633 var = TREE_OPERAND (var, 0);
8634 if (TREE_THIS_VOLATILE (var))
8635 return NULL_TREE;
8637 etype = TREE_TYPE (var);
8638 if (TREE_CODE (etype) == ARRAY_TYPE)
8639 etype = TREE_TYPE (etype);
8641 if (!INTEGRAL_TYPE_P (etype)
8642 && !POINTER_TYPE_P (etype))
8643 return NULL_TREE;
8645 if (! var_decl_component_p (var))
8646 return NULL_TREE;
8648 length = tree_to_uhwi (len);
8649 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8650 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8651 return NULL_TREE;
8653 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8654 return NULL_TREE;
8656 if (integer_zerop (c))
8657 cval = 0;
8658 else
8660 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8661 return NULL_TREE;
8663 cval = TREE_INT_CST_LOW (c);
8664 cval &= 0xff;
8665 cval |= cval << 8;
8666 cval |= cval << 16;
8667 cval |= (cval << 31) << 1;
8670 ret = build_int_cst_type (etype, cval);
8671 var = build_fold_indirect_ref_loc (loc,
8672 fold_convert_loc (loc,
8673 build_pointer_type (etype),
8674 dest));
8675 ret = build2 (MODIFY_EXPR, etype, var, ret);
8676 if (ignore)
8677 return ret;
8679 return omit_one_operand_loc (loc, type, dest, ret);
8682 /* Fold function call to builtin memset. Return
8683 NULL_TREE if no simplification can be made. */
8685 static tree
8686 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8688 if (! validate_arg (dest, POINTER_TYPE)
8689 || ! validate_arg (size, INTEGER_TYPE))
8690 return NULL_TREE;
8692 if (!ignore)
8693 return NULL_TREE;
8695 /* New argument list transforming bzero(ptr x, int y) to
8696 memset(ptr x, int 0, size_t y). This is done this way
8697 so that if it isn't expanded inline, we fallback to
8698 calling bzero instead of memset. */
8700 return fold_builtin_memset (loc, dest, integer_zero_node,
8701 fold_convert_loc (loc, size_type_node, size),
8702 void_type_node, ignore);
8705 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8706 NULL_TREE if no simplification can be made.
8707 If ENDP is 0, return DEST (like memcpy).
8708 If ENDP is 1, return DEST+LEN (like mempcpy).
8709 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8710 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8711 (memmove). */
8713 static tree
8714 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8715 tree len, tree type, bool ignore, int endp)
8717 tree destvar, srcvar, expr;
8719 if (! validate_arg (dest, POINTER_TYPE)
8720 || ! validate_arg (src, POINTER_TYPE)
8721 || ! validate_arg (len, INTEGER_TYPE))
8722 return NULL_TREE;
8724 /* If the LEN parameter is zero, return DEST. */
8725 if (integer_zerop (len))
8726 return omit_one_operand_loc (loc, type, dest, src);
8728 /* If SRC and DEST are the same (and not volatile), return
8729 DEST{,+LEN,+LEN-1}. */
8730 if (operand_equal_p (src, dest, 0))
8731 expr = len;
8732 else
8734 tree srctype, desttype;
8735 unsigned int src_align, dest_align;
8736 tree off0;
8738 if (endp == 3)
8740 src_align = get_pointer_alignment (src);
8741 dest_align = get_pointer_alignment (dest);
8743 /* Both DEST and SRC must be pointer types.
8744 ??? This is what old code did. Is the testing for pointer types
8745 really mandatory?
8747 If either SRC is readonly or length is 1, we can use memcpy. */
8748 if (!dest_align || !src_align)
8749 return NULL_TREE;
8750 if (readonly_data_expr (src)
8751 || (tree_fits_uhwi_p (len)
8752 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8753 >= tree_to_uhwi (len))))
8755 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8756 if (!fn)
8757 return NULL_TREE;
8758 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8761 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8762 if (TREE_CODE (src) == ADDR_EXPR
8763 && TREE_CODE (dest) == ADDR_EXPR)
8765 tree src_base, dest_base, fn;
8766 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8767 HOST_WIDE_INT size = -1;
8768 HOST_WIDE_INT maxsize = -1;
8770 srcvar = TREE_OPERAND (src, 0);
8771 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8772 &size, &maxsize);
8773 destvar = TREE_OPERAND (dest, 0);
8774 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8775 &size, &maxsize);
8776 if (tree_fits_uhwi_p (len))
8777 maxsize = tree_to_uhwi (len);
8778 else
8779 maxsize = -1;
8780 src_offset /= BITS_PER_UNIT;
8781 dest_offset /= BITS_PER_UNIT;
8782 if (SSA_VAR_P (src_base)
8783 && SSA_VAR_P (dest_base))
8785 if (operand_equal_p (src_base, dest_base, 0)
8786 && ranges_overlap_p (src_offset, maxsize,
8787 dest_offset, maxsize))
8788 return NULL_TREE;
8790 else if (TREE_CODE (src_base) == MEM_REF
8791 && TREE_CODE (dest_base) == MEM_REF)
8793 double_int off;
8794 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8795 TREE_OPERAND (dest_base, 0), 0))
8796 return NULL_TREE;
8797 off = mem_ref_offset (src_base) +
8798 double_int::from_shwi (src_offset);
8799 if (!off.fits_shwi ())
8800 return NULL_TREE;
8801 src_offset = off.low;
8802 off = mem_ref_offset (dest_base) +
8803 double_int::from_shwi (dest_offset);
8804 if (!off.fits_shwi ())
8805 return NULL_TREE;
8806 dest_offset = off.low;
8807 if (ranges_overlap_p (src_offset, maxsize,
8808 dest_offset, maxsize))
8809 return NULL_TREE;
8811 else
8812 return NULL_TREE;
8814 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8815 if (!fn)
8816 return NULL_TREE;
8817 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8820 /* If the destination and source do not alias optimize into
8821 memcpy as well. */
8822 if ((is_gimple_min_invariant (dest)
8823 || TREE_CODE (dest) == SSA_NAME)
8824 && (is_gimple_min_invariant (src)
8825 || TREE_CODE (src) == SSA_NAME))
8827 ao_ref destr, srcr;
8828 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8829 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8830 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8832 tree fn;
8833 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8834 if (!fn)
8835 return NULL_TREE;
8836 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8840 return NULL_TREE;
8843 if (!tree_fits_shwi_p (len))
8844 return NULL_TREE;
8845 /* FIXME:
8846 This logic lose for arguments like (type *)malloc (sizeof (type)),
8847 since we strip the casts of up to VOID return value from malloc.
8848 Perhaps we ought to inherit type from non-VOID argument here? */
8849 STRIP_NOPS (src);
8850 STRIP_NOPS (dest);
8851 if (!POINTER_TYPE_P (TREE_TYPE (src))
8852 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8853 return NULL_TREE;
8854 /* In the following try to find a type that is most natural to be
8855 used for the memcpy source and destination and that allows
8856 the most optimization when memcpy is turned into a plain assignment
8857 using that type. In theory we could always use a char[len] type
8858 but that only gains us that the destination and source possibly
8859 no longer will have their address taken. */
8860 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8861 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8863 tree tem = TREE_OPERAND (src, 0);
8864 STRIP_NOPS (tem);
8865 if (tem != TREE_OPERAND (src, 0))
8866 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8868 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8870 tree tem = TREE_OPERAND (dest, 0);
8871 STRIP_NOPS (tem);
8872 if (tem != TREE_OPERAND (dest, 0))
8873 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8875 srctype = TREE_TYPE (TREE_TYPE (src));
8876 if (TREE_CODE (srctype) == ARRAY_TYPE
8877 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8879 srctype = TREE_TYPE (srctype);
8880 STRIP_NOPS (src);
8881 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8883 desttype = TREE_TYPE (TREE_TYPE (dest));
8884 if (TREE_CODE (desttype) == ARRAY_TYPE
8885 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8887 desttype = TREE_TYPE (desttype);
8888 STRIP_NOPS (dest);
8889 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8891 if (TREE_ADDRESSABLE (srctype)
8892 || TREE_ADDRESSABLE (desttype))
8893 return NULL_TREE;
8895 /* Make sure we are not copying using a floating-point mode or
8896 a type whose size possibly does not match its precision. */
8897 if (FLOAT_MODE_P (TYPE_MODE (desttype))
8898 || TREE_CODE (desttype) == BOOLEAN_TYPE
8899 || TREE_CODE (desttype) == ENUMERAL_TYPE)
8901 /* A more suitable int_mode_for_mode would return a vector
8902 integer mode for a vector float mode or a integer complex
8903 mode for a float complex mode if there isn't a regular
8904 integer mode covering the mode of desttype. */
8905 enum machine_mode mode = int_mode_for_mode (TYPE_MODE (desttype));
8906 if (mode == BLKmode)
8907 desttype = NULL_TREE;
8908 else
8909 desttype = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode),
8912 if (FLOAT_MODE_P (TYPE_MODE (srctype))
8913 || TREE_CODE (srctype) == BOOLEAN_TYPE
8914 || TREE_CODE (srctype) == ENUMERAL_TYPE)
8916 enum machine_mode mode = int_mode_for_mode (TYPE_MODE (srctype));
8917 if (mode == BLKmode)
8918 srctype = NULL_TREE;
8919 else
8920 srctype = build_nonstandard_integer_type (GET_MODE_BITSIZE (mode),
8923 if (!srctype)
8924 srctype = desttype;
8925 if (!desttype)
8926 desttype = srctype;
8927 if (!srctype)
8928 return NULL_TREE;
8930 src_align = get_pointer_alignment (src);
8931 dest_align = get_pointer_alignment (dest);
8932 if (dest_align < TYPE_ALIGN (desttype)
8933 || src_align < TYPE_ALIGN (srctype))
8934 return NULL_TREE;
8936 if (!ignore)
8937 dest = builtin_save_expr (dest);
8939 /* Build accesses at offset zero with a ref-all character type. */
8940 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8941 ptr_mode, true), 0);
8943 destvar = dest;
8944 STRIP_NOPS (destvar);
8945 if (TREE_CODE (destvar) == ADDR_EXPR
8946 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8947 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8948 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8949 else
8950 destvar = NULL_TREE;
8952 srcvar = src;
8953 STRIP_NOPS (srcvar);
8954 if (TREE_CODE (srcvar) == ADDR_EXPR
8955 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8956 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8958 if (!destvar
8959 || src_align >= TYPE_ALIGN (desttype))
8960 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8961 srcvar, off0);
8962 else if (!STRICT_ALIGNMENT)
8964 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8965 src_align);
8966 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8968 else
8969 srcvar = NULL_TREE;
8971 else
8972 srcvar = NULL_TREE;
8974 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8975 return NULL_TREE;
8977 if (srcvar == NULL_TREE)
8979 STRIP_NOPS (src);
8980 if (src_align >= TYPE_ALIGN (desttype))
8981 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8982 else
8984 if (STRICT_ALIGNMENT)
8985 return NULL_TREE;
8986 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8987 src_align);
8988 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8991 else if (destvar == NULL_TREE)
8993 STRIP_NOPS (dest);
8994 if (dest_align >= TYPE_ALIGN (srctype))
8995 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8996 else
8998 if (STRICT_ALIGNMENT)
8999 return NULL_TREE;
9000 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
9001 dest_align);
9002 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
9006 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
9009 if (ignore)
9010 return expr;
9012 if (endp == 0 || endp == 3)
9013 return omit_one_operand_loc (loc, type, dest, expr);
9015 if (expr == len)
9016 expr = NULL_TREE;
9018 if (endp == 2)
9019 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
9020 ssize_int (1));
9022 dest = fold_build_pointer_plus_loc (loc, dest, len);
9023 dest = fold_convert_loc (loc, type, dest);
9024 if (expr)
9025 dest = omit_one_operand_loc (loc, type, dest, expr);
9026 return dest;
9029 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9030 If LEN is not NULL, it represents the length of the string to be
9031 copied. Return NULL_TREE if no simplification can be made. */
9033 tree
9034 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
9036 tree fn;
9038 if (!validate_arg (dest, POINTER_TYPE)
9039 || !validate_arg (src, POINTER_TYPE))
9040 return NULL_TREE;
9042 /* If SRC and DEST are the same (and not volatile), return DEST. */
9043 if (operand_equal_p (src, dest, 0))
9044 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9046 if (optimize_function_for_size_p (cfun))
9047 return NULL_TREE;
9049 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9050 if (!fn)
9051 return NULL_TREE;
9053 if (!len)
9055 len = c_strlen (src, 1);
9056 if (! len || TREE_SIDE_EFFECTS (len))
9057 return NULL_TREE;
9060 len = fold_convert_loc (loc, size_type_node, len);
9061 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9062 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9063 build_call_expr_loc (loc, fn, 3, dest, src, len));
9066 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9067 Return NULL_TREE if no simplification can be made. */
9069 static tree
9070 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9072 tree fn, len, lenp1, call, type;
9074 if (!validate_arg (dest, POINTER_TYPE)
9075 || !validate_arg (src, POINTER_TYPE))
9076 return NULL_TREE;
9078 len = c_strlen (src, 1);
9079 if (!len
9080 || TREE_CODE (len) != INTEGER_CST)
9081 return NULL_TREE;
9083 if (optimize_function_for_size_p (cfun)
9084 /* If length is zero it's small enough. */
9085 && !integer_zerop (len))
9086 return NULL_TREE;
9088 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9089 if (!fn)
9090 return NULL_TREE;
9092 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9093 fold_convert_loc (loc, size_type_node, len),
9094 build_int_cst (size_type_node, 1));
9095 /* We use dest twice in building our expression. Save it from
9096 multiple expansions. */
9097 dest = builtin_save_expr (dest);
9098 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9100 type = TREE_TYPE (TREE_TYPE (fndecl));
9101 dest = fold_build_pointer_plus_loc (loc, dest, len);
9102 dest = fold_convert_loc (loc, type, dest);
9103 dest = omit_one_operand_loc (loc, type, dest, call);
9104 return dest;
9107 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9108 If SLEN is not NULL, it represents the length of the source string.
9109 Return NULL_TREE if no simplification can be made. */
9111 tree
9112 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9113 tree src, tree len, tree slen)
9115 tree fn;
9117 if (!validate_arg (dest, POINTER_TYPE)
9118 || !validate_arg (src, POINTER_TYPE)
9119 || !validate_arg (len, INTEGER_TYPE))
9120 return NULL_TREE;
9122 /* If the LEN parameter is zero, return DEST. */
9123 if (integer_zerop (len))
9124 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9126 /* We can't compare slen with len as constants below if len is not a
9127 constant. */
9128 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9129 return NULL_TREE;
9131 if (!slen)
9132 slen = c_strlen (src, 1);
9134 /* Now, we must be passed a constant src ptr parameter. */
9135 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9136 return NULL_TREE;
9138 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9140 /* We do not support simplification of this case, though we do
9141 support it when expanding trees into RTL. */
9142 /* FIXME: generate a call to __builtin_memset. */
9143 if (tree_int_cst_lt (slen, len))
9144 return NULL_TREE;
9146 /* OK transform into builtin memcpy. */
9147 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9148 if (!fn)
9149 return NULL_TREE;
9151 len = fold_convert_loc (loc, size_type_node, len);
9152 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9153 build_call_expr_loc (loc, fn, 3, dest, src, len));
9156 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9157 arguments to the call, and TYPE is its return type.
9158 Return NULL_TREE if no simplification can be made. */
9160 static tree
9161 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9163 if (!validate_arg (arg1, POINTER_TYPE)
9164 || !validate_arg (arg2, INTEGER_TYPE)
9165 || !validate_arg (len, INTEGER_TYPE))
9166 return NULL_TREE;
9167 else
9169 const char *p1;
9171 if (TREE_CODE (arg2) != INTEGER_CST
9172 || !tree_fits_uhwi_p (len))
9173 return NULL_TREE;
9175 p1 = c_getstr (arg1);
9176 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9178 char c;
9179 const char *r;
9180 tree tem;
9182 if (target_char_cast (arg2, &c))
9183 return NULL_TREE;
9185 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
9187 if (r == NULL)
9188 return build_int_cst (TREE_TYPE (arg1), 0);
9190 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9191 return fold_convert_loc (loc, type, tem);
9193 return NULL_TREE;
9197 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9198 Return NULL_TREE if no simplification can be made. */
9200 static tree
9201 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9203 const char *p1, *p2;
9205 if (!validate_arg (arg1, POINTER_TYPE)
9206 || !validate_arg (arg2, POINTER_TYPE)
9207 || !validate_arg (len, INTEGER_TYPE))
9208 return NULL_TREE;
9210 /* If the LEN parameter is zero, return zero. */
9211 if (integer_zerop (len))
9212 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9213 arg1, arg2);
9215 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9216 if (operand_equal_p (arg1, arg2, 0))
9217 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9219 p1 = c_getstr (arg1);
9220 p2 = c_getstr (arg2);
9222 /* If all arguments are constant, and the value of len is not greater
9223 than the lengths of arg1 and arg2, evaluate at compile-time. */
9224 if (tree_fits_uhwi_p (len) && p1 && p2
9225 && compare_tree_int (len, strlen (p1) + 1) <= 0
9226 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9228 const int r = memcmp (p1, p2, tree_to_uhwi (len));
9230 if (r > 0)
9231 return integer_one_node;
9232 else if (r < 0)
9233 return integer_minus_one_node;
9234 else
9235 return integer_zero_node;
9238 /* If len parameter is one, return an expression corresponding to
9239 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9240 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9242 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9243 tree cst_uchar_ptr_node
9244 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9246 tree ind1
9247 = fold_convert_loc (loc, integer_type_node,
9248 build1 (INDIRECT_REF, cst_uchar_node,
9249 fold_convert_loc (loc,
9250 cst_uchar_ptr_node,
9251 arg1)));
9252 tree ind2
9253 = fold_convert_loc (loc, integer_type_node,
9254 build1 (INDIRECT_REF, cst_uchar_node,
9255 fold_convert_loc (loc,
9256 cst_uchar_ptr_node,
9257 arg2)));
9258 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9261 return NULL_TREE;
9264 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9265 Return NULL_TREE if no simplification can be made. */
9267 static tree
9268 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9270 const char *p1, *p2;
9272 if (!validate_arg (arg1, POINTER_TYPE)
9273 || !validate_arg (arg2, POINTER_TYPE))
9274 return NULL_TREE;
9276 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9277 if (operand_equal_p (arg1, arg2, 0))
9278 return integer_zero_node;
9280 p1 = c_getstr (arg1);
9281 p2 = c_getstr (arg2);
9283 if (p1 && p2)
9285 const int i = strcmp (p1, p2);
9286 if (i < 0)
9287 return integer_minus_one_node;
9288 else if (i > 0)
9289 return integer_one_node;
9290 else
9291 return integer_zero_node;
9294 /* If the second arg is "", return *(const unsigned char*)arg1. */
9295 if (p2 && *p2 == '\0')
9297 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9298 tree cst_uchar_ptr_node
9299 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9301 return fold_convert_loc (loc, integer_type_node,
9302 build1 (INDIRECT_REF, cst_uchar_node,
9303 fold_convert_loc (loc,
9304 cst_uchar_ptr_node,
9305 arg1)));
9308 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9309 if (p1 && *p1 == '\0')
9311 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9312 tree cst_uchar_ptr_node
9313 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9315 tree temp
9316 = fold_convert_loc (loc, integer_type_node,
9317 build1 (INDIRECT_REF, cst_uchar_node,
9318 fold_convert_loc (loc,
9319 cst_uchar_ptr_node,
9320 arg2)));
9321 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9324 return NULL_TREE;
9327 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9328 Return NULL_TREE if no simplification can be made. */
9330 static tree
9331 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9333 const char *p1, *p2;
9335 if (!validate_arg (arg1, POINTER_TYPE)
9336 || !validate_arg (arg2, POINTER_TYPE)
9337 || !validate_arg (len, INTEGER_TYPE))
9338 return NULL_TREE;
9340 /* If the LEN parameter is zero, return zero. */
9341 if (integer_zerop (len))
9342 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9343 arg1, arg2);
9345 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9346 if (operand_equal_p (arg1, arg2, 0))
9347 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9349 p1 = c_getstr (arg1);
9350 p2 = c_getstr (arg2);
9352 if (tree_fits_uhwi_p (len) && p1 && p2)
9354 const int i = strncmp (p1, p2, tree_to_uhwi (len));
9355 if (i > 0)
9356 return integer_one_node;
9357 else if (i < 0)
9358 return integer_minus_one_node;
9359 else
9360 return integer_zero_node;
9363 /* If the second arg is "", and the length is greater than zero,
9364 return *(const unsigned char*)arg1. */
9365 if (p2 && *p2 == '\0'
9366 && TREE_CODE (len) == INTEGER_CST
9367 && tree_int_cst_sgn (len) == 1)
9369 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9370 tree cst_uchar_ptr_node
9371 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9373 return fold_convert_loc (loc, integer_type_node,
9374 build1 (INDIRECT_REF, cst_uchar_node,
9375 fold_convert_loc (loc,
9376 cst_uchar_ptr_node,
9377 arg1)));
9380 /* If the first arg is "", and the length is greater than zero,
9381 return -*(const unsigned char*)arg2. */
9382 if (p1 && *p1 == '\0'
9383 && TREE_CODE (len) == INTEGER_CST
9384 && tree_int_cst_sgn (len) == 1)
9386 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9387 tree cst_uchar_ptr_node
9388 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9390 tree temp = fold_convert_loc (loc, integer_type_node,
9391 build1 (INDIRECT_REF, cst_uchar_node,
9392 fold_convert_loc (loc,
9393 cst_uchar_ptr_node,
9394 arg2)));
9395 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9398 /* If len parameter is one, return an expression corresponding to
9399 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9400 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9402 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9403 tree cst_uchar_ptr_node
9404 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9406 tree ind1 = fold_convert_loc (loc, integer_type_node,
9407 build1 (INDIRECT_REF, cst_uchar_node,
9408 fold_convert_loc (loc,
9409 cst_uchar_ptr_node,
9410 arg1)));
9411 tree ind2 = fold_convert_loc (loc, integer_type_node,
9412 build1 (INDIRECT_REF, cst_uchar_node,
9413 fold_convert_loc (loc,
9414 cst_uchar_ptr_node,
9415 arg2)));
9416 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9419 return NULL_TREE;
9422 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9423 ARG. Return NULL_TREE if no simplification can be made. */
9425 static tree
9426 fold_builtin_signbit (location_t loc, tree arg, tree type)
9428 if (!validate_arg (arg, REAL_TYPE))
9429 return NULL_TREE;
9431 /* If ARG is a compile-time constant, determine the result. */
9432 if (TREE_CODE (arg) == REAL_CST
9433 && !TREE_OVERFLOW (arg))
9435 REAL_VALUE_TYPE c;
9437 c = TREE_REAL_CST (arg);
9438 return (REAL_VALUE_NEGATIVE (c)
9439 ? build_one_cst (type)
9440 : build_zero_cst (type));
9443 /* If ARG is non-negative, the result is always zero. */
9444 if (tree_expr_nonnegative_p (arg))
9445 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9447 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9448 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9449 return fold_convert (type,
9450 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9451 build_real (TREE_TYPE (arg), dconst0)));
9453 return NULL_TREE;
9456 /* Fold function call to builtin copysign, copysignf or copysignl with
9457 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9458 be made. */
9460 static tree
9461 fold_builtin_copysign (location_t loc, tree fndecl,
9462 tree arg1, tree arg2, tree type)
9464 tree tem;
9466 if (!validate_arg (arg1, REAL_TYPE)
9467 || !validate_arg (arg2, REAL_TYPE))
9468 return NULL_TREE;
9470 /* copysign(X,X) is X. */
9471 if (operand_equal_p (arg1, arg2, 0))
9472 return fold_convert_loc (loc, type, arg1);
9474 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9475 if (TREE_CODE (arg1) == REAL_CST
9476 && TREE_CODE (arg2) == REAL_CST
9477 && !TREE_OVERFLOW (arg1)
9478 && !TREE_OVERFLOW (arg2))
9480 REAL_VALUE_TYPE c1, c2;
9482 c1 = TREE_REAL_CST (arg1);
9483 c2 = TREE_REAL_CST (arg2);
9484 /* c1.sign := c2.sign. */
9485 real_copysign (&c1, &c2);
9486 return build_real (type, c1);
9489 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9490 Remember to evaluate Y for side-effects. */
9491 if (tree_expr_nonnegative_p (arg2))
9492 return omit_one_operand_loc (loc, type,
9493 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9494 arg2);
9496 /* Strip sign changing operations for the first argument. */
9497 tem = fold_strip_sign_ops (arg1);
9498 if (tem)
9499 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9501 return NULL_TREE;
9504 /* Fold a call to builtin isascii with argument ARG. */
9506 static tree
9507 fold_builtin_isascii (location_t loc, tree arg)
9509 if (!validate_arg (arg, INTEGER_TYPE))
9510 return NULL_TREE;
9511 else
9513 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9514 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9515 build_int_cst (integer_type_node,
9516 ~ (unsigned HOST_WIDE_INT) 0x7f));
9517 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9518 arg, integer_zero_node);
9522 /* Fold a call to builtin toascii with argument ARG. */
9524 static tree
9525 fold_builtin_toascii (location_t loc, tree arg)
9527 if (!validate_arg (arg, INTEGER_TYPE))
9528 return NULL_TREE;
9530 /* Transform toascii(c) -> (c & 0x7f). */
9531 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9532 build_int_cst (integer_type_node, 0x7f));
9535 /* Fold a call to builtin isdigit with argument ARG. */
9537 static tree
9538 fold_builtin_isdigit (location_t loc, tree arg)
9540 if (!validate_arg (arg, INTEGER_TYPE))
9541 return NULL_TREE;
9542 else
9544 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9545 /* According to the C standard, isdigit is unaffected by locale.
9546 However, it definitely is affected by the target character set. */
9547 unsigned HOST_WIDE_INT target_digit0
9548 = lang_hooks.to_target_charset ('0');
9550 if (target_digit0 == 0)
9551 return NULL_TREE;
9553 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9554 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9555 build_int_cst (unsigned_type_node, target_digit0));
9556 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9557 build_int_cst (unsigned_type_node, 9));
9561 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9563 static tree
9564 fold_builtin_fabs (location_t loc, tree arg, tree type)
9566 if (!validate_arg (arg, REAL_TYPE))
9567 return NULL_TREE;
9569 arg = fold_convert_loc (loc, type, arg);
9570 if (TREE_CODE (arg) == REAL_CST)
9571 return fold_abs_const (arg, type);
9572 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9575 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9577 static tree
9578 fold_builtin_abs (location_t loc, tree arg, tree type)
9580 if (!validate_arg (arg, INTEGER_TYPE))
9581 return NULL_TREE;
9583 arg = fold_convert_loc (loc, type, arg);
9584 if (TREE_CODE (arg) == INTEGER_CST)
9585 return fold_abs_const (arg, type);
9586 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9589 /* Fold a fma operation with arguments ARG[012]. */
9591 tree
9592 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9593 tree type, tree arg0, tree arg1, tree arg2)
9595 if (TREE_CODE (arg0) == REAL_CST
9596 && TREE_CODE (arg1) == REAL_CST
9597 && TREE_CODE (arg2) == REAL_CST)
9598 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9600 return NULL_TREE;
9603 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9605 static tree
9606 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9608 if (validate_arg (arg0, REAL_TYPE)
9609 && validate_arg (arg1, REAL_TYPE)
9610 && validate_arg (arg2, REAL_TYPE))
9612 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9613 if (tem)
9614 return tem;
9616 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9617 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9618 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9620 return NULL_TREE;
9623 /* Fold a call to builtin fmin or fmax. */
9625 static tree
9626 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9627 tree type, bool max)
9629 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9631 /* Calculate the result when the argument is a constant. */
9632 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9634 if (res)
9635 return res;
9637 /* If either argument is NaN, return the other one. Avoid the
9638 transformation if we get (and honor) a signalling NaN. Using
9639 omit_one_operand() ensures we create a non-lvalue. */
9640 if (TREE_CODE (arg0) == REAL_CST
9641 && real_isnan (&TREE_REAL_CST (arg0))
9642 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9643 || ! TREE_REAL_CST (arg0).signalling))
9644 return omit_one_operand_loc (loc, type, arg1, arg0);
9645 if (TREE_CODE (arg1) == REAL_CST
9646 && real_isnan (&TREE_REAL_CST (arg1))
9647 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9648 || ! TREE_REAL_CST (arg1).signalling))
9649 return omit_one_operand_loc (loc, type, arg0, arg1);
9651 /* Transform fmin/fmax(x,x) -> x. */
9652 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9653 return omit_one_operand_loc (loc, type, arg0, arg1);
9655 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9656 functions to return the numeric arg if the other one is NaN.
9657 These tree codes don't honor that, so only transform if
9658 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9659 handled, so we don't have to worry about it either. */
9660 if (flag_finite_math_only)
9661 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9662 fold_convert_loc (loc, type, arg0),
9663 fold_convert_loc (loc, type, arg1));
9665 return NULL_TREE;
9668 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9670 static tree
9671 fold_builtin_carg (location_t loc, tree arg, tree type)
9673 if (validate_arg (arg, COMPLEX_TYPE)
9674 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9676 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9678 if (atan2_fn)
9680 tree new_arg = builtin_save_expr (arg);
9681 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9682 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9683 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9687 return NULL_TREE;
9690 /* Fold a call to builtin logb/ilogb. */
9692 static tree
9693 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9695 if (! validate_arg (arg, REAL_TYPE))
9696 return NULL_TREE;
9698 STRIP_NOPS (arg);
9700 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9702 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9704 switch (value->cl)
9706 case rvc_nan:
9707 case rvc_inf:
9708 /* If arg is Inf or NaN and we're logb, return it. */
9709 if (TREE_CODE (rettype) == REAL_TYPE)
9711 /* For logb(-Inf) we have to return +Inf. */
9712 if (real_isinf (value) && real_isneg (value))
9714 REAL_VALUE_TYPE tem;
9715 real_inf (&tem);
9716 return build_real (rettype, tem);
9718 return fold_convert_loc (loc, rettype, arg);
9720 /* Fall through... */
9721 case rvc_zero:
9722 /* Zero may set errno and/or raise an exception for logb, also
9723 for ilogb we don't know FP_ILOGB0. */
9724 return NULL_TREE;
9725 case rvc_normal:
9726 /* For normal numbers, proceed iff radix == 2. In GCC,
9727 normalized significands are in the range [0.5, 1.0). We
9728 want the exponent as if they were [1.0, 2.0) so get the
9729 exponent and subtract 1. */
9730 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9731 return fold_convert_loc (loc, rettype,
9732 build_int_cst (integer_type_node,
9733 REAL_EXP (value)-1));
9734 break;
9738 return NULL_TREE;
9741 /* Fold a call to builtin significand, if radix == 2. */
9743 static tree
9744 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9746 if (! validate_arg (arg, REAL_TYPE))
9747 return NULL_TREE;
9749 STRIP_NOPS (arg);
9751 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9753 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9755 switch (value->cl)
9757 case rvc_zero:
9758 case rvc_nan:
9759 case rvc_inf:
9760 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9761 return fold_convert_loc (loc, rettype, arg);
9762 case rvc_normal:
9763 /* For normal numbers, proceed iff radix == 2. */
9764 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9766 REAL_VALUE_TYPE result = *value;
9767 /* In GCC, normalized significands are in the range [0.5,
9768 1.0). We want them to be [1.0, 2.0) so set the
9769 exponent to 1. */
9770 SET_REAL_EXP (&result, 1);
9771 return build_real (rettype, result);
9773 break;
9777 return NULL_TREE;
9780 /* Fold a call to builtin frexp, we can assume the base is 2. */
9782 static tree
9783 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9785 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9786 return NULL_TREE;
9788 STRIP_NOPS (arg0);
9790 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9791 return NULL_TREE;
9793 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9795 /* Proceed if a valid pointer type was passed in. */
9796 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9798 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9799 tree frac, exp;
9801 switch (value->cl)
9803 case rvc_zero:
9804 /* For +-0, return (*exp = 0, +-0). */
9805 exp = integer_zero_node;
9806 frac = arg0;
9807 break;
9808 case rvc_nan:
9809 case rvc_inf:
9810 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9811 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9812 case rvc_normal:
9814 /* Since the frexp function always expects base 2, and in
9815 GCC normalized significands are already in the range
9816 [0.5, 1.0), we have exactly what frexp wants. */
9817 REAL_VALUE_TYPE frac_rvt = *value;
9818 SET_REAL_EXP (&frac_rvt, 0);
9819 frac = build_real (rettype, frac_rvt);
9820 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9822 break;
9823 default:
9824 gcc_unreachable ();
9827 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9828 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9829 TREE_SIDE_EFFECTS (arg1) = 1;
9830 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9833 return NULL_TREE;
9836 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9837 then we can assume the base is two. If it's false, then we have to
9838 check the mode of the TYPE parameter in certain cases. */
9840 static tree
9841 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9842 tree type, bool ldexp)
9844 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9846 STRIP_NOPS (arg0);
9847 STRIP_NOPS (arg1);
9849 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9850 if (real_zerop (arg0) || integer_zerop (arg1)
9851 || (TREE_CODE (arg0) == REAL_CST
9852 && !real_isfinite (&TREE_REAL_CST (arg0))))
9853 return omit_one_operand_loc (loc, type, arg0, arg1);
9855 /* If both arguments are constant, then try to evaluate it. */
9856 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9857 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9858 && tree_fits_shwi_p (arg1))
9860 /* Bound the maximum adjustment to twice the range of the
9861 mode's valid exponents. Use abs to ensure the range is
9862 positive as a sanity check. */
9863 const long max_exp_adj = 2 *
9864 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9865 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9867 /* Get the user-requested adjustment. */
9868 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9870 /* The requested adjustment must be inside this range. This
9871 is a preliminary cap to avoid things like overflow, we
9872 may still fail to compute the result for other reasons. */
9873 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9875 REAL_VALUE_TYPE initial_result;
9877 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9879 /* Ensure we didn't overflow. */
9880 if (! real_isinf (&initial_result))
9882 const REAL_VALUE_TYPE trunc_result
9883 = real_value_truncate (TYPE_MODE (type), initial_result);
9885 /* Only proceed if the target mode can hold the
9886 resulting value. */
9887 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9888 return build_real (type, trunc_result);
9894 return NULL_TREE;
9897 /* Fold a call to builtin modf. */
9899 static tree
9900 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9902 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9903 return NULL_TREE;
9905 STRIP_NOPS (arg0);
9907 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9908 return NULL_TREE;
9910 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9912 /* Proceed if a valid pointer type was passed in. */
9913 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9915 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9916 REAL_VALUE_TYPE trunc, frac;
9918 switch (value->cl)
9920 case rvc_nan:
9921 case rvc_zero:
9922 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9923 trunc = frac = *value;
9924 break;
9925 case rvc_inf:
9926 /* For +-Inf, return (*arg1 = arg0, +-0). */
9927 frac = dconst0;
9928 frac.sign = value->sign;
9929 trunc = *value;
9930 break;
9931 case rvc_normal:
9932 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9933 real_trunc (&trunc, VOIDmode, value);
9934 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9935 /* If the original number was negative and already
9936 integral, then the fractional part is -0.0. */
9937 if (value->sign && frac.cl == rvc_zero)
9938 frac.sign = value->sign;
9939 break;
9942 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9943 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9944 build_real (rettype, trunc));
9945 TREE_SIDE_EFFECTS (arg1) = 1;
9946 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9947 build_real (rettype, frac));
9950 return NULL_TREE;
9953 /* Given a location LOC, an interclass builtin function decl FNDECL
9954 and its single argument ARG, return an folded expression computing
9955 the same, or NULL_TREE if we either couldn't or didn't want to fold
9956 (the latter happen if there's an RTL instruction available). */
9958 static tree
9959 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9961 enum machine_mode mode;
9963 if (!validate_arg (arg, REAL_TYPE))
9964 return NULL_TREE;
9966 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9967 return NULL_TREE;
9969 mode = TYPE_MODE (TREE_TYPE (arg));
9971 /* If there is no optab, try generic code. */
9972 switch (DECL_FUNCTION_CODE (fndecl))
9974 tree result;
9976 CASE_FLT_FN (BUILT_IN_ISINF):
9978 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9979 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9980 tree const type = TREE_TYPE (arg);
9981 REAL_VALUE_TYPE r;
9982 char buf[128];
9984 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9985 real_from_string (&r, buf);
9986 result = build_call_expr (isgr_fn, 2,
9987 fold_build1_loc (loc, ABS_EXPR, type, arg),
9988 build_real (type, r));
9989 return result;
9991 CASE_FLT_FN (BUILT_IN_FINITE):
9992 case BUILT_IN_ISFINITE:
9994 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9995 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9996 tree const type = TREE_TYPE (arg);
9997 REAL_VALUE_TYPE r;
9998 char buf[128];
10000 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10001 real_from_string (&r, buf);
10002 result = build_call_expr (isle_fn, 2,
10003 fold_build1_loc (loc, ABS_EXPR, type, arg),
10004 build_real (type, r));
10005 /*result = fold_build2_loc (loc, UNGT_EXPR,
10006 TREE_TYPE (TREE_TYPE (fndecl)),
10007 fold_build1_loc (loc, ABS_EXPR, type, arg),
10008 build_real (type, r));
10009 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
10010 TREE_TYPE (TREE_TYPE (fndecl)),
10011 result);*/
10012 return result;
10014 case BUILT_IN_ISNORMAL:
10016 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
10017 islessequal(fabs(x),DBL_MAX). */
10018 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
10019 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
10020 tree const type = TREE_TYPE (arg);
10021 REAL_VALUE_TYPE rmax, rmin;
10022 char buf[128];
10024 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10025 real_from_string (&rmax, buf);
10026 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10027 real_from_string (&rmin, buf);
10028 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10029 result = build_call_expr (isle_fn, 2, arg,
10030 build_real (type, rmax));
10031 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
10032 build_call_expr (isge_fn, 2, arg,
10033 build_real (type, rmin)));
10034 return result;
10036 default:
10037 break;
10040 return NULL_TREE;
10043 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10044 ARG is the argument for the call. */
10046 static tree
10047 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
10049 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10050 REAL_VALUE_TYPE r;
10052 if (!validate_arg (arg, REAL_TYPE))
10053 return NULL_TREE;
10055 switch (builtin_index)
10057 case BUILT_IN_ISINF:
10058 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10059 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10061 if (TREE_CODE (arg) == REAL_CST)
10063 r = TREE_REAL_CST (arg);
10064 if (real_isinf (&r))
10065 return real_compare (GT_EXPR, &r, &dconst0)
10066 ? integer_one_node : integer_minus_one_node;
10067 else
10068 return integer_zero_node;
10071 return NULL_TREE;
10073 case BUILT_IN_ISINF_SIGN:
10075 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10076 /* In a boolean context, GCC will fold the inner COND_EXPR to
10077 1. So e.g. "if (isinf_sign(x))" would be folded to just
10078 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10079 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10080 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10081 tree tmp = NULL_TREE;
10083 arg = builtin_save_expr (arg);
10085 if (signbit_fn && isinf_fn)
10087 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10088 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10090 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10091 signbit_call, integer_zero_node);
10092 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10093 isinf_call, integer_zero_node);
10095 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10096 integer_minus_one_node, integer_one_node);
10097 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10098 isinf_call, tmp,
10099 integer_zero_node);
10102 return tmp;
10105 case BUILT_IN_ISFINITE:
10106 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10107 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10108 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10110 if (TREE_CODE (arg) == REAL_CST)
10112 r = TREE_REAL_CST (arg);
10113 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10116 return NULL_TREE;
10118 case BUILT_IN_ISNAN:
10119 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10120 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10122 if (TREE_CODE (arg) == REAL_CST)
10124 r = TREE_REAL_CST (arg);
10125 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10128 arg = builtin_save_expr (arg);
10129 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10131 default:
10132 gcc_unreachable ();
10136 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10137 This builtin will generate code to return the appropriate floating
10138 point classification depending on the value of the floating point
10139 number passed in. The possible return values must be supplied as
10140 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10141 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10142 one floating point argument which is "type generic". */
10144 static tree
10145 fold_builtin_fpclassify (location_t loc, tree exp)
10147 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10148 arg, type, res, tmp;
10149 enum machine_mode mode;
10150 REAL_VALUE_TYPE r;
10151 char buf[128];
10153 /* Verify the required arguments in the original call. */
10154 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10155 INTEGER_TYPE, INTEGER_TYPE,
10156 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10157 return NULL_TREE;
10159 fp_nan = CALL_EXPR_ARG (exp, 0);
10160 fp_infinite = CALL_EXPR_ARG (exp, 1);
10161 fp_normal = CALL_EXPR_ARG (exp, 2);
10162 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10163 fp_zero = CALL_EXPR_ARG (exp, 4);
10164 arg = CALL_EXPR_ARG (exp, 5);
10165 type = TREE_TYPE (arg);
10166 mode = TYPE_MODE (type);
10167 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10169 /* fpclassify(x) ->
10170 isnan(x) ? FP_NAN :
10171 (fabs(x) == Inf ? FP_INFINITE :
10172 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10173 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10175 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10176 build_real (type, dconst0));
10177 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10178 tmp, fp_zero, fp_subnormal);
10180 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10181 real_from_string (&r, buf);
10182 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10183 arg, build_real (type, r));
10184 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10186 if (HONOR_INFINITIES (mode))
10188 real_inf (&r);
10189 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10190 build_real (type, r));
10191 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10192 fp_infinite, res);
10195 if (HONOR_NANS (mode))
10197 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10198 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10201 return res;
10204 /* Fold a call to an unordered comparison function such as
10205 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10206 being called and ARG0 and ARG1 are the arguments for the call.
10207 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10208 the opposite of the desired result. UNORDERED_CODE is used
10209 for modes that can hold NaNs and ORDERED_CODE is used for
10210 the rest. */
10212 static tree
10213 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10214 enum tree_code unordered_code,
10215 enum tree_code ordered_code)
10217 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10218 enum tree_code code;
10219 tree type0, type1;
10220 enum tree_code code0, code1;
10221 tree cmp_type = NULL_TREE;
10223 type0 = TREE_TYPE (arg0);
10224 type1 = TREE_TYPE (arg1);
10226 code0 = TREE_CODE (type0);
10227 code1 = TREE_CODE (type1);
10229 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10230 /* Choose the wider of two real types. */
10231 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10232 ? type0 : type1;
10233 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10234 cmp_type = type0;
10235 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10236 cmp_type = type1;
10238 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10239 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10241 if (unordered_code == UNORDERED_EXPR)
10243 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10244 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10245 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10248 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10249 : ordered_code;
10250 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10251 fold_build2_loc (loc, code, type, arg0, arg1));
10254 /* Fold a call to built-in function FNDECL with 0 arguments.
10255 IGNORE is true if the result of the function call is ignored. This
10256 function returns NULL_TREE if no simplification was possible. */
10258 static tree
10259 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10261 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10262 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10263 switch (fcode)
10265 CASE_FLT_FN (BUILT_IN_INF):
10266 case BUILT_IN_INFD32:
10267 case BUILT_IN_INFD64:
10268 case BUILT_IN_INFD128:
10269 return fold_builtin_inf (loc, type, true);
10271 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10272 return fold_builtin_inf (loc, type, false);
10274 case BUILT_IN_CLASSIFY_TYPE:
10275 return fold_builtin_classify_type (NULL_TREE);
10277 case BUILT_IN_UNREACHABLE:
10278 if (flag_sanitize & SANITIZE_UNREACHABLE
10279 && (current_function_decl == NULL
10280 || !lookup_attribute ("no_sanitize_undefined",
10281 DECL_ATTRIBUTES (current_function_decl))))
10282 return ubsan_instrument_unreachable (loc);
10283 break;
10285 default:
10286 break;
10288 return NULL_TREE;
10291 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10292 IGNORE is true if the result of the function call is ignored. This
10293 function returns NULL_TREE if no simplification was possible. */
10295 static tree
10296 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10298 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10299 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10300 switch (fcode)
10302 case BUILT_IN_CONSTANT_P:
10304 tree val = fold_builtin_constant_p (arg0);
10306 /* Gimplification will pull the CALL_EXPR for the builtin out of
10307 an if condition. When not optimizing, we'll not CSE it back.
10308 To avoid link error types of regressions, return false now. */
10309 if (!val && !optimize)
10310 val = integer_zero_node;
10312 return val;
10315 case BUILT_IN_CLASSIFY_TYPE:
10316 return fold_builtin_classify_type (arg0);
10318 case BUILT_IN_STRLEN:
10319 return fold_builtin_strlen (loc, type, arg0);
10321 CASE_FLT_FN (BUILT_IN_FABS):
10322 case BUILT_IN_FABSD32:
10323 case BUILT_IN_FABSD64:
10324 case BUILT_IN_FABSD128:
10325 return fold_builtin_fabs (loc, arg0, type);
10327 case BUILT_IN_ABS:
10328 case BUILT_IN_LABS:
10329 case BUILT_IN_LLABS:
10330 case BUILT_IN_IMAXABS:
10331 return fold_builtin_abs (loc, arg0, type);
10333 CASE_FLT_FN (BUILT_IN_CONJ):
10334 if (validate_arg (arg0, COMPLEX_TYPE)
10335 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10336 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10337 break;
10339 CASE_FLT_FN (BUILT_IN_CREAL):
10340 if (validate_arg (arg0, COMPLEX_TYPE)
10341 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10342 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10343 break;
10345 CASE_FLT_FN (BUILT_IN_CIMAG):
10346 if (validate_arg (arg0, COMPLEX_TYPE)
10347 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10348 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10349 break;
10351 CASE_FLT_FN (BUILT_IN_CCOS):
10352 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
10354 CASE_FLT_FN (BUILT_IN_CCOSH):
10355 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
10357 CASE_FLT_FN (BUILT_IN_CPROJ):
10358 return fold_builtin_cproj (loc, arg0, type);
10360 CASE_FLT_FN (BUILT_IN_CSIN):
10361 if (validate_arg (arg0, COMPLEX_TYPE)
10362 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10363 return do_mpc_arg1 (arg0, type, mpc_sin);
10364 break;
10366 CASE_FLT_FN (BUILT_IN_CSINH):
10367 if (validate_arg (arg0, COMPLEX_TYPE)
10368 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10369 return do_mpc_arg1 (arg0, type, mpc_sinh);
10370 break;
10372 CASE_FLT_FN (BUILT_IN_CTAN):
10373 if (validate_arg (arg0, COMPLEX_TYPE)
10374 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10375 return do_mpc_arg1 (arg0, type, mpc_tan);
10376 break;
10378 CASE_FLT_FN (BUILT_IN_CTANH):
10379 if (validate_arg (arg0, COMPLEX_TYPE)
10380 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10381 return do_mpc_arg1 (arg0, type, mpc_tanh);
10382 break;
10384 CASE_FLT_FN (BUILT_IN_CLOG):
10385 if (validate_arg (arg0, COMPLEX_TYPE)
10386 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10387 return do_mpc_arg1 (arg0, type, mpc_log);
10388 break;
10390 CASE_FLT_FN (BUILT_IN_CSQRT):
10391 if (validate_arg (arg0, COMPLEX_TYPE)
10392 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10393 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10394 break;
10396 CASE_FLT_FN (BUILT_IN_CASIN):
10397 if (validate_arg (arg0, COMPLEX_TYPE)
10398 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10399 return do_mpc_arg1 (arg0, type, mpc_asin);
10400 break;
10402 CASE_FLT_FN (BUILT_IN_CACOS):
10403 if (validate_arg (arg0, COMPLEX_TYPE)
10404 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10405 return do_mpc_arg1 (arg0, type, mpc_acos);
10406 break;
10408 CASE_FLT_FN (BUILT_IN_CATAN):
10409 if (validate_arg (arg0, COMPLEX_TYPE)
10410 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10411 return do_mpc_arg1 (arg0, type, mpc_atan);
10412 break;
10414 CASE_FLT_FN (BUILT_IN_CASINH):
10415 if (validate_arg (arg0, COMPLEX_TYPE)
10416 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10417 return do_mpc_arg1 (arg0, type, mpc_asinh);
10418 break;
10420 CASE_FLT_FN (BUILT_IN_CACOSH):
10421 if (validate_arg (arg0, COMPLEX_TYPE)
10422 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10423 return do_mpc_arg1 (arg0, type, mpc_acosh);
10424 break;
10426 CASE_FLT_FN (BUILT_IN_CATANH):
10427 if (validate_arg (arg0, COMPLEX_TYPE)
10428 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10429 return do_mpc_arg1 (arg0, type, mpc_atanh);
10430 break;
10432 CASE_FLT_FN (BUILT_IN_CABS):
10433 return fold_builtin_cabs (loc, arg0, type, fndecl);
10435 CASE_FLT_FN (BUILT_IN_CARG):
10436 return fold_builtin_carg (loc, arg0, type);
10438 CASE_FLT_FN (BUILT_IN_SQRT):
10439 return fold_builtin_sqrt (loc, arg0, type);
10441 CASE_FLT_FN (BUILT_IN_CBRT):
10442 return fold_builtin_cbrt (loc, arg0, type);
10444 CASE_FLT_FN (BUILT_IN_ASIN):
10445 if (validate_arg (arg0, REAL_TYPE))
10446 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10447 &dconstm1, &dconst1, true);
10448 break;
10450 CASE_FLT_FN (BUILT_IN_ACOS):
10451 if (validate_arg (arg0, REAL_TYPE))
10452 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10453 &dconstm1, &dconst1, true);
10454 break;
10456 CASE_FLT_FN (BUILT_IN_ATAN):
10457 if (validate_arg (arg0, REAL_TYPE))
10458 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10459 break;
10461 CASE_FLT_FN (BUILT_IN_ASINH):
10462 if (validate_arg (arg0, REAL_TYPE))
10463 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10464 break;
10466 CASE_FLT_FN (BUILT_IN_ACOSH):
10467 if (validate_arg (arg0, REAL_TYPE))
10468 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10469 &dconst1, NULL, true);
10470 break;
10472 CASE_FLT_FN (BUILT_IN_ATANH):
10473 if (validate_arg (arg0, REAL_TYPE))
10474 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10475 &dconstm1, &dconst1, false);
10476 break;
10478 CASE_FLT_FN (BUILT_IN_SIN):
10479 if (validate_arg (arg0, REAL_TYPE))
10480 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10481 break;
10483 CASE_FLT_FN (BUILT_IN_COS):
10484 return fold_builtin_cos (loc, arg0, type, fndecl);
10486 CASE_FLT_FN (BUILT_IN_TAN):
10487 return fold_builtin_tan (arg0, type);
10489 CASE_FLT_FN (BUILT_IN_CEXP):
10490 return fold_builtin_cexp (loc, arg0, type);
10492 CASE_FLT_FN (BUILT_IN_CEXPI):
10493 if (validate_arg (arg0, REAL_TYPE))
10494 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10495 break;
10497 CASE_FLT_FN (BUILT_IN_SINH):
10498 if (validate_arg (arg0, REAL_TYPE))
10499 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10500 break;
10502 CASE_FLT_FN (BUILT_IN_COSH):
10503 return fold_builtin_cosh (loc, arg0, type, fndecl);
10505 CASE_FLT_FN (BUILT_IN_TANH):
10506 if (validate_arg (arg0, REAL_TYPE))
10507 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10508 break;
10510 CASE_FLT_FN (BUILT_IN_ERF):
10511 if (validate_arg (arg0, REAL_TYPE))
10512 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10513 break;
10515 CASE_FLT_FN (BUILT_IN_ERFC):
10516 if (validate_arg (arg0, REAL_TYPE))
10517 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10518 break;
10520 CASE_FLT_FN (BUILT_IN_TGAMMA):
10521 if (validate_arg (arg0, REAL_TYPE))
10522 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10523 break;
10525 CASE_FLT_FN (BUILT_IN_EXP):
10526 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10528 CASE_FLT_FN (BUILT_IN_EXP2):
10529 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10531 CASE_FLT_FN (BUILT_IN_EXP10):
10532 CASE_FLT_FN (BUILT_IN_POW10):
10533 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10535 CASE_FLT_FN (BUILT_IN_EXPM1):
10536 if (validate_arg (arg0, REAL_TYPE))
10537 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10538 break;
10540 CASE_FLT_FN (BUILT_IN_LOG):
10541 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10543 CASE_FLT_FN (BUILT_IN_LOG2):
10544 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10546 CASE_FLT_FN (BUILT_IN_LOG10):
10547 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10549 CASE_FLT_FN (BUILT_IN_LOG1P):
10550 if (validate_arg (arg0, REAL_TYPE))
10551 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10552 &dconstm1, NULL, false);
10553 break;
10555 CASE_FLT_FN (BUILT_IN_J0):
10556 if (validate_arg (arg0, REAL_TYPE))
10557 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10558 NULL, NULL, 0);
10559 break;
10561 CASE_FLT_FN (BUILT_IN_J1):
10562 if (validate_arg (arg0, REAL_TYPE))
10563 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10564 NULL, NULL, 0);
10565 break;
10567 CASE_FLT_FN (BUILT_IN_Y0):
10568 if (validate_arg (arg0, REAL_TYPE))
10569 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10570 &dconst0, NULL, false);
10571 break;
10573 CASE_FLT_FN (BUILT_IN_Y1):
10574 if (validate_arg (arg0, REAL_TYPE))
10575 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10576 &dconst0, NULL, false);
10577 break;
10579 CASE_FLT_FN (BUILT_IN_NAN):
10580 case BUILT_IN_NAND32:
10581 case BUILT_IN_NAND64:
10582 case BUILT_IN_NAND128:
10583 return fold_builtin_nan (arg0, type, true);
10585 CASE_FLT_FN (BUILT_IN_NANS):
10586 return fold_builtin_nan (arg0, type, false);
10588 CASE_FLT_FN (BUILT_IN_FLOOR):
10589 return fold_builtin_floor (loc, fndecl, arg0);
10591 CASE_FLT_FN (BUILT_IN_CEIL):
10592 return fold_builtin_ceil (loc, fndecl, arg0);
10594 CASE_FLT_FN (BUILT_IN_TRUNC):
10595 return fold_builtin_trunc (loc, fndecl, arg0);
10597 CASE_FLT_FN (BUILT_IN_ROUND):
10598 return fold_builtin_round (loc, fndecl, arg0);
10600 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10601 CASE_FLT_FN (BUILT_IN_RINT):
10602 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10604 CASE_FLT_FN (BUILT_IN_ICEIL):
10605 CASE_FLT_FN (BUILT_IN_LCEIL):
10606 CASE_FLT_FN (BUILT_IN_LLCEIL):
10607 CASE_FLT_FN (BUILT_IN_LFLOOR):
10608 CASE_FLT_FN (BUILT_IN_IFLOOR):
10609 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10610 CASE_FLT_FN (BUILT_IN_IROUND):
10611 CASE_FLT_FN (BUILT_IN_LROUND):
10612 CASE_FLT_FN (BUILT_IN_LLROUND):
10613 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10615 CASE_FLT_FN (BUILT_IN_IRINT):
10616 CASE_FLT_FN (BUILT_IN_LRINT):
10617 CASE_FLT_FN (BUILT_IN_LLRINT):
10618 return fold_fixed_mathfn (loc, fndecl, arg0);
10620 case BUILT_IN_BSWAP16:
10621 case BUILT_IN_BSWAP32:
10622 case BUILT_IN_BSWAP64:
10623 return fold_builtin_bswap (fndecl, arg0);
10625 CASE_INT_FN (BUILT_IN_FFS):
10626 CASE_INT_FN (BUILT_IN_CLZ):
10627 CASE_INT_FN (BUILT_IN_CTZ):
10628 CASE_INT_FN (BUILT_IN_CLRSB):
10629 CASE_INT_FN (BUILT_IN_POPCOUNT):
10630 CASE_INT_FN (BUILT_IN_PARITY):
10631 return fold_builtin_bitop (fndecl, arg0);
10633 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10634 return fold_builtin_signbit (loc, arg0, type);
10636 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10637 return fold_builtin_significand (loc, arg0, type);
10639 CASE_FLT_FN (BUILT_IN_ILOGB):
10640 CASE_FLT_FN (BUILT_IN_LOGB):
10641 return fold_builtin_logb (loc, arg0, type);
10643 case BUILT_IN_ISASCII:
10644 return fold_builtin_isascii (loc, arg0);
10646 case BUILT_IN_TOASCII:
10647 return fold_builtin_toascii (loc, arg0);
10649 case BUILT_IN_ISDIGIT:
10650 return fold_builtin_isdigit (loc, arg0);
10652 CASE_FLT_FN (BUILT_IN_FINITE):
10653 case BUILT_IN_FINITED32:
10654 case BUILT_IN_FINITED64:
10655 case BUILT_IN_FINITED128:
10656 case BUILT_IN_ISFINITE:
10658 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10659 if (ret)
10660 return ret;
10661 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10664 CASE_FLT_FN (BUILT_IN_ISINF):
10665 case BUILT_IN_ISINFD32:
10666 case BUILT_IN_ISINFD64:
10667 case BUILT_IN_ISINFD128:
10669 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10670 if (ret)
10671 return ret;
10672 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10675 case BUILT_IN_ISNORMAL:
10676 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10678 case BUILT_IN_ISINF_SIGN:
10679 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10681 CASE_FLT_FN (BUILT_IN_ISNAN):
10682 case BUILT_IN_ISNAND32:
10683 case BUILT_IN_ISNAND64:
10684 case BUILT_IN_ISNAND128:
10685 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10687 case BUILT_IN_PRINTF:
10688 case BUILT_IN_PRINTF_UNLOCKED:
10689 case BUILT_IN_VPRINTF:
10690 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10692 case BUILT_IN_FREE:
10693 if (integer_zerop (arg0))
10694 return build_empty_stmt (loc);
10695 break;
10697 default:
10698 break;
10701 return NULL_TREE;
10705 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10706 IGNORE is true if the result of the function call is ignored. This
10707 function returns NULL_TREE if no simplification was possible. */
10709 static tree
10710 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10712 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10713 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10715 switch (fcode)
10717 CASE_FLT_FN (BUILT_IN_JN):
10718 if (validate_arg (arg0, INTEGER_TYPE)
10719 && validate_arg (arg1, REAL_TYPE))
10720 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10721 break;
10723 CASE_FLT_FN (BUILT_IN_YN):
10724 if (validate_arg (arg0, INTEGER_TYPE)
10725 && validate_arg (arg1, REAL_TYPE))
10726 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10727 &dconst0, false);
10728 break;
10730 CASE_FLT_FN (BUILT_IN_DREM):
10731 CASE_FLT_FN (BUILT_IN_REMAINDER):
10732 if (validate_arg (arg0, REAL_TYPE)
10733 && validate_arg (arg1, REAL_TYPE))
10734 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10735 break;
10737 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10738 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10739 if (validate_arg (arg0, REAL_TYPE)
10740 && validate_arg (arg1, POINTER_TYPE))
10741 return do_mpfr_lgamma_r (arg0, arg1, type);
10742 break;
10744 CASE_FLT_FN (BUILT_IN_ATAN2):
10745 if (validate_arg (arg0, REAL_TYPE)
10746 && validate_arg (arg1, REAL_TYPE))
10747 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10748 break;
10750 CASE_FLT_FN (BUILT_IN_FDIM):
10751 if (validate_arg (arg0, REAL_TYPE)
10752 && validate_arg (arg1, REAL_TYPE))
10753 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10754 break;
10756 CASE_FLT_FN (BUILT_IN_HYPOT):
10757 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10759 CASE_FLT_FN (BUILT_IN_CPOW):
10760 if (validate_arg (arg0, COMPLEX_TYPE)
10761 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10762 && validate_arg (arg1, COMPLEX_TYPE)
10763 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10764 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10765 break;
10767 CASE_FLT_FN (BUILT_IN_LDEXP):
10768 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10769 CASE_FLT_FN (BUILT_IN_SCALBN):
10770 CASE_FLT_FN (BUILT_IN_SCALBLN):
10771 return fold_builtin_load_exponent (loc, arg0, arg1,
10772 type, /*ldexp=*/false);
10774 CASE_FLT_FN (BUILT_IN_FREXP):
10775 return fold_builtin_frexp (loc, arg0, arg1, type);
10777 CASE_FLT_FN (BUILT_IN_MODF):
10778 return fold_builtin_modf (loc, arg0, arg1, type);
10780 case BUILT_IN_BZERO:
10781 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10783 case BUILT_IN_FPUTS:
10784 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10786 case BUILT_IN_FPUTS_UNLOCKED:
10787 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10789 case BUILT_IN_STRSTR:
10790 return fold_builtin_strstr (loc, arg0, arg1, type);
10792 case BUILT_IN_STRCAT:
10793 return fold_builtin_strcat (loc, arg0, arg1, NULL_TREE);
10795 case BUILT_IN_STRSPN:
10796 return fold_builtin_strspn (loc, arg0, arg1);
10798 case BUILT_IN_STRCSPN:
10799 return fold_builtin_strcspn (loc, arg0, arg1);
10801 case BUILT_IN_STRCHR:
10802 case BUILT_IN_INDEX:
10803 return fold_builtin_strchr (loc, arg0, arg1, type);
10805 case BUILT_IN_STRRCHR:
10806 case BUILT_IN_RINDEX:
10807 return fold_builtin_strrchr (loc, arg0, arg1, type);
10809 case BUILT_IN_STRCPY:
10810 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10812 case BUILT_IN_STPCPY:
10813 if (ignore)
10815 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10816 if (!fn)
10817 break;
10819 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10821 else
10822 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10823 break;
10825 case BUILT_IN_STRCMP:
10826 return fold_builtin_strcmp (loc, arg0, arg1);
10828 case BUILT_IN_STRPBRK:
10829 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10831 case BUILT_IN_EXPECT:
10832 return fold_builtin_expect (loc, arg0, arg1);
10834 CASE_FLT_FN (BUILT_IN_POW):
10835 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10837 CASE_FLT_FN (BUILT_IN_POWI):
10838 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10840 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10841 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10843 CASE_FLT_FN (BUILT_IN_FMIN):
10844 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10846 CASE_FLT_FN (BUILT_IN_FMAX):
10847 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10849 case BUILT_IN_ISGREATER:
10850 return fold_builtin_unordered_cmp (loc, fndecl,
10851 arg0, arg1, UNLE_EXPR, LE_EXPR);
10852 case BUILT_IN_ISGREATEREQUAL:
10853 return fold_builtin_unordered_cmp (loc, fndecl,
10854 arg0, arg1, UNLT_EXPR, LT_EXPR);
10855 case BUILT_IN_ISLESS:
10856 return fold_builtin_unordered_cmp (loc, fndecl,
10857 arg0, arg1, UNGE_EXPR, GE_EXPR);
10858 case BUILT_IN_ISLESSEQUAL:
10859 return fold_builtin_unordered_cmp (loc, fndecl,
10860 arg0, arg1, UNGT_EXPR, GT_EXPR);
10861 case BUILT_IN_ISLESSGREATER:
10862 return fold_builtin_unordered_cmp (loc, fndecl,
10863 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10864 case BUILT_IN_ISUNORDERED:
10865 return fold_builtin_unordered_cmp (loc, fndecl,
10866 arg0, arg1, UNORDERED_EXPR,
10867 NOP_EXPR);
10869 /* We do the folding for va_start in the expander. */
10870 case BUILT_IN_VA_START:
10871 break;
10873 case BUILT_IN_SPRINTF:
10874 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10876 case BUILT_IN_OBJECT_SIZE:
10877 return fold_builtin_object_size (arg0, arg1);
10879 case BUILT_IN_PRINTF:
10880 case BUILT_IN_PRINTF_UNLOCKED:
10881 case BUILT_IN_VPRINTF:
10882 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10884 case BUILT_IN_PRINTF_CHK:
10885 case BUILT_IN_VPRINTF_CHK:
10886 if (!validate_arg (arg0, INTEGER_TYPE)
10887 || TREE_SIDE_EFFECTS (arg0))
10888 return NULL_TREE;
10889 else
10890 return fold_builtin_printf (loc, fndecl,
10891 arg1, NULL_TREE, ignore, fcode);
10892 break;
10894 case BUILT_IN_FPRINTF:
10895 case BUILT_IN_FPRINTF_UNLOCKED:
10896 case BUILT_IN_VFPRINTF:
10897 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10898 ignore, fcode);
10900 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10901 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10903 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10904 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10906 default:
10907 break;
10909 return NULL_TREE;
10912 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10913 and ARG2. IGNORE is true if the result of the function call is ignored.
10914 This function returns NULL_TREE if no simplification was possible. */
10916 static tree
10917 fold_builtin_3 (location_t loc, tree fndecl,
10918 tree arg0, tree arg1, tree arg2, bool ignore)
10920 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10921 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10922 switch (fcode)
10925 CASE_FLT_FN (BUILT_IN_SINCOS):
10926 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10928 CASE_FLT_FN (BUILT_IN_FMA):
10929 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10930 break;
10932 CASE_FLT_FN (BUILT_IN_REMQUO):
10933 if (validate_arg (arg0, REAL_TYPE)
10934 && validate_arg (arg1, REAL_TYPE)
10935 && validate_arg (arg2, POINTER_TYPE))
10936 return do_mpfr_remquo (arg0, arg1, arg2);
10937 break;
10939 case BUILT_IN_MEMSET:
10940 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10942 case BUILT_IN_BCOPY:
10943 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10944 void_type_node, true, /*endp=*/3);
10946 case BUILT_IN_MEMCPY:
10947 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10948 type, ignore, /*endp=*/0);
10950 case BUILT_IN_MEMPCPY:
10951 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10952 type, ignore, /*endp=*/1);
10954 case BUILT_IN_MEMMOVE:
10955 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10956 type, ignore, /*endp=*/3);
10958 case BUILT_IN_STRNCAT:
10959 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10961 case BUILT_IN_STRNCPY:
10962 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10964 case BUILT_IN_STRNCMP:
10965 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10967 case BUILT_IN_MEMCHR:
10968 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10970 case BUILT_IN_BCMP:
10971 case BUILT_IN_MEMCMP:
10972 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10974 case BUILT_IN_SPRINTF:
10975 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10977 case BUILT_IN_SNPRINTF:
10978 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10980 case BUILT_IN_STRCPY_CHK:
10981 case BUILT_IN_STPCPY_CHK:
10982 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10983 ignore, fcode);
10985 case BUILT_IN_STRCAT_CHK:
10986 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10988 case BUILT_IN_PRINTF_CHK:
10989 case BUILT_IN_VPRINTF_CHK:
10990 if (!validate_arg (arg0, INTEGER_TYPE)
10991 || TREE_SIDE_EFFECTS (arg0))
10992 return NULL_TREE;
10993 else
10994 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10995 break;
10997 case BUILT_IN_FPRINTF:
10998 case BUILT_IN_FPRINTF_UNLOCKED:
10999 case BUILT_IN_VFPRINTF:
11000 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
11001 ignore, fcode);
11003 case BUILT_IN_FPRINTF_CHK:
11004 case BUILT_IN_VFPRINTF_CHK:
11005 if (!validate_arg (arg1, INTEGER_TYPE)
11006 || TREE_SIDE_EFFECTS (arg1))
11007 return NULL_TREE;
11008 else
11009 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
11010 ignore, fcode);
11012 default:
11013 break;
11015 return NULL_TREE;
11018 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
11019 ARG2, and ARG3. IGNORE is true if the result of the function call is
11020 ignored. This function returns NULL_TREE if no simplification was
11021 possible. */
11023 static tree
11024 fold_builtin_4 (location_t loc, tree fndecl,
11025 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
11027 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11029 switch (fcode)
11031 case BUILT_IN_MEMCPY_CHK:
11032 case BUILT_IN_MEMPCPY_CHK:
11033 case BUILT_IN_MEMMOVE_CHK:
11034 case BUILT_IN_MEMSET_CHK:
11035 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
11036 NULL_TREE, ignore,
11037 DECL_FUNCTION_CODE (fndecl));
11039 case BUILT_IN_STRNCPY_CHK:
11040 case BUILT_IN_STPNCPY_CHK:
11041 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
11042 ignore, fcode);
11044 case BUILT_IN_STRNCAT_CHK:
11045 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
11047 case BUILT_IN_SNPRINTF:
11048 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
11050 case BUILT_IN_FPRINTF_CHK:
11051 case BUILT_IN_VFPRINTF_CHK:
11052 if (!validate_arg (arg1, INTEGER_TYPE)
11053 || TREE_SIDE_EFFECTS (arg1))
11054 return NULL_TREE;
11055 else
11056 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
11057 ignore, fcode);
11058 break;
11060 default:
11061 break;
11063 return NULL_TREE;
11066 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11067 arguments, where NARGS <= 4. IGNORE is true if the result of the
11068 function call is ignored. This function returns NULL_TREE if no
11069 simplification was possible. Note that this only folds builtins with
11070 fixed argument patterns. Foldings that do varargs-to-varargs
11071 transformations, or that match calls with more than 4 arguments,
11072 need to be handled with fold_builtin_varargs instead. */
11074 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11076 static tree
11077 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11079 tree ret = NULL_TREE;
11081 switch (nargs)
11083 case 0:
11084 ret = fold_builtin_0 (loc, fndecl, ignore);
11085 break;
11086 case 1:
11087 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11088 break;
11089 case 2:
11090 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11091 break;
11092 case 3:
11093 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11094 break;
11095 case 4:
11096 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11097 ignore);
11098 break;
11099 default:
11100 break;
11102 if (ret)
11104 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11105 SET_EXPR_LOCATION (ret, loc);
11106 TREE_NO_WARNING (ret) = 1;
11107 return ret;
11109 return NULL_TREE;
11112 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11113 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11114 of arguments in ARGS to be omitted. OLDNARGS is the number of
11115 elements in ARGS. */
11117 static tree
11118 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11119 int skip, tree fndecl, int n, va_list newargs)
11121 int nargs = oldnargs - skip + n;
11122 tree *buffer;
11124 if (n > 0)
11126 int i, j;
11128 buffer = XALLOCAVEC (tree, nargs);
11129 for (i = 0; i < n; i++)
11130 buffer[i] = va_arg (newargs, tree);
11131 for (j = skip; j < oldnargs; j++, i++)
11132 buffer[i] = args[j];
11134 else
11135 buffer = args + skip;
11137 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11140 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11141 list ARGS along with N new arguments specified as the "..."
11142 parameters. SKIP is the number of arguments in ARGS to be omitted.
11143 OLDNARGS is the number of elements in ARGS. */
11145 static tree
11146 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11147 int skip, tree fndecl, int n, ...)
11149 va_list ap;
11150 tree t;
11152 va_start (ap, n);
11153 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11154 va_end (ap);
11156 return t;
11159 /* Return true if FNDECL shouldn't be folded right now.
11160 If a built-in function has an inline attribute always_inline
11161 wrapper, defer folding it after always_inline functions have
11162 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11163 might not be performed. */
11165 bool
11166 avoid_folding_inline_builtin (tree fndecl)
11168 return (DECL_DECLARED_INLINE_P (fndecl)
11169 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11170 && cfun
11171 && !cfun->always_inline_functions_inlined
11172 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11175 /* A wrapper function for builtin folding that prevents warnings for
11176 "statement without effect" and the like, caused by removing the
11177 call node earlier than the warning is generated. */
11179 tree
11180 fold_call_expr (location_t loc, tree exp, bool ignore)
11182 tree ret = NULL_TREE;
11183 tree fndecl = get_callee_fndecl (exp);
11184 if (fndecl
11185 && TREE_CODE (fndecl) == FUNCTION_DECL
11186 && DECL_BUILT_IN (fndecl)
11187 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11188 yet. Defer folding until we see all the arguments
11189 (after inlining). */
11190 && !CALL_EXPR_VA_ARG_PACK (exp))
11192 int nargs = call_expr_nargs (exp);
11194 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11195 instead last argument is __builtin_va_arg_pack (). Defer folding
11196 even in that case, until arguments are finalized. */
11197 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11199 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11200 if (fndecl2
11201 && TREE_CODE (fndecl2) == FUNCTION_DECL
11202 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11203 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11204 return NULL_TREE;
11207 if (avoid_folding_inline_builtin (fndecl))
11208 return NULL_TREE;
11210 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11211 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11212 CALL_EXPR_ARGP (exp), ignore);
11213 else
11215 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11217 tree *args = CALL_EXPR_ARGP (exp);
11218 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11220 if (!ret)
11221 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11222 if (ret)
11223 return ret;
11226 return NULL_TREE;
11229 /* Conveniently construct a function call expression. FNDECL names the
11230 function to be called and N arguments are passed in the array
11231 ARGARRAY. */
11233 tree
11234 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11236 tree fntype = TREE_TYPE (fndecl);
11237 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11239 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11242 /* Conveniently construct a function call expression. FNDECL names the
11243 function to be called and the arguments are passed in the vector
11244 VEC. */
11246 tree
11247 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11249 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11250 vec_safe_address (vec));
11254 /* Conveniently construct a function call expression. FNDECL names the
11255 function to be called, N is the number of arguments, and the "..."
11256 parameters are the argument expressions. */
11258 tree
11259 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11261 va_list ap;
11262 tree *argarray = XALLOCAVEC (tree, n);
11263 int i;
11265 va_start (ap, n);
11266 for (i = 0; i < n; i++)
11267 argarray[i] = va_arg (ap, tree);
11268 va_end (ap);
11269 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11272 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11273 varargs macros aren't supported by all bootstrap compilers. */
11275 tree
11276 build_call_expr (tree fndecl, int n, ...)
11278 va_list ap;
11279 tree *argarray = XALLOCAVEC (tree, n);
11280 int i;
11282 va_start (ap, n);
11283 for (i = 0; i < n; i++)
11284 argarray[i] = va_arg (ap, tree);
11285 va_end (ap);
11286 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11289 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11290 N arguments are passed in the array ARGARRAY. */
11292 tree
11293 fold_builtin_call_array (location_t loc, tree type,
11294 tree fn,
11295 int n,
11296 tree *argarray)
11298 tree ret = NULL_TREE;
11299 tree exp;
11301 if (TREE_CODE (fn) == ADDR_EXPR)
11303 tree fndecl = TREE_OPERAND (fn, 0);
11304 if (TREE_CODE (fndecl) == FUNCTION_DECL
11305 && DECL_BUILT_IN (fndecl))
11307 /* If last argument is __builtin_va_arg_pack (), arguments to this
11308 function are not finalized yet. Defer folding until they are. */
11309 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11311 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11312 if (fndecl2
11313 && TREE_CODE (fndecl2) == FUNCTION_DECL
11314 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11315 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11316 return build_call_array_loc (loc, type, fn, n, argarray);
11318 if (avoid_folding_inline_builtin (fndecl))
11319 return build_call_array_loc (loc, type, fn, n, argarray);
11320 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11322 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11323 if (ret)
11324 return ret;
11326 return build_call_array_loc (loc, type, fn, n, argarray);
11328 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11330 /* First try the transformations that don't require consing up
11331 an exp. */
11332 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11333 if (ret)
11334 return ret;
11337 /* If we got this far, we need to build an exp. */
11338 exp = build_call_array_loc (loc, type, fn, n, argarray);
11339 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11340 return ret ? ret : exp;
11344 return build_call_array_loc (loc, type, fn, n, argarray);
11347 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11348 along with N new arguments specified as the "..." parameters. SKIP
11349 is the number of arguments in EXP to be omitted. This function is used
11350 to do varargs-to-varargs transformations. */
11352 static tree
11353 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11355 va_list ap;
11356 tree t;
11358 va_start (ap, n);
11359 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11360 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11361 va_end (ap);
11363 return t;
11366 /* Validate a single argument ARG against a tree code CODE representing
11367 a type. */
11369 static bool
11370 validate_arg (const_tree arg, enum tree_code code)
11372 if (!arg)
11373 return false;
11374 else if (code == POINTER_TYPE)
11375 return POINTER_TYPE_P (TREE_TYPE (arg));
11376 else if (code == INTEGER_TYPE)
11377 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11378 return code == TREE_CODE (TREE_TYPE (arg));
11381 /* This function validates the types of a function call argument list
11382 against a specified list of tree_codes. If the last specifier is a 0,
11383 that represents an ellipses, otherwise the last specifier must be a
11384 VOID_TYPE.
11386 This is the GIMPLE version of validate_arglist. Eventually we want to
11387 completely convert builtins.c to work from GIMPLEs and the tree based
11388 validate_arglist will then be removed. */
11390 bool
11391 validate_gimple_arglist (const_gimple call, ...)
11393 enum tree_code code;
11394 bool res = 0;
11395 va_list ap;
11396 const_tree arg;
11397 size_t i;
11399 va_start (ap, call);
11400 i = 0;
11404 code = (enum tree_code) va_arg (ap, int);
11405 switch (code)
11407 case 0:
11408 /* This signifies an ellipses, any further arguments are all ok. */
11409 res = true;
11410 goto end;
11411 case VOID_TYPE:
11412 /* This signifies an endlink, if no arguments remain, return
11413 true, otherwise return false. */
11414 res = (i == gimple_call_num_args (call));
11415 goto end;
11416 default:
11417 /* If no parameters remain or the parameter's code does not
11418 match the specified code, return false. Otherwise continue
11419 checking any remaining arguments. */
11420 arg = gimple_call_arg (call, i++);
11421 if (!validate_arg (arg, code))
11422 goto end;
11423 break;
11426 while (1);
11428 /* We need gotos here since we can only have one VA_CLOSE in a
11429 function. */
11430 end: ;
11431 va_end (ap);
11433 return res;
11436 /* Default target-specific builtin expander that does nothing. */
11439 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11440 rtx target ATTRIBUTE_UNUSED,
11441 rtx subtarget ATTRIBUTE_UNUSED,
11442 enum machine_mode mode ATTRIBUTE_UNUSED,
11443 int ignore ATTRIBUTE_UNUSED)
11445 return NULL_RTX;
11448 /* Returns true is EXP represents data that would potentially reside
11449 in a readonly section. */
11451 static bool
11452 readonly_data_expr (tree exp)
11454 STRIP_NOPS (exp);
11456 if (TREE_CODE (exp) != ADDR_EXPR)
11457 return false;
11459 exp = get_base_address (TREE_OPERAND (exp, 0));
11460 if (!exp)
11461 return false;
11463 /* Make sure we call decl_readonly_section only for trees it
11464 can handle (since it returns true for everything it doesn't
11465 understand). */
11466 if (TREE_CODE (exp) == STRING_CST
11467 || TREE_CODE (exp) == CONSTRUCTOR
11468 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11469 return decl_readonly_section (exp, 0);
11470 else
11471 return false;
11474 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11475 to the call, and TYPE is its return type.
11477 Return NULL_TREE if no simplification was possible, otherwise return the
11478 simplified form of the call as a tree.
11480 The simplified form may be a constant or other expression which
11481 computes the same value, but in a more efficient manner (including
11482 calls to other builtin functions).
11484 The call may contain arguments which need to be evaluated, but
11485 which are not useful to determine the result of the call. In
11486 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11487 COMPOUND_EXPR will be an argument which must be evaluated.
11488 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11489 COMPOUND_EXPR in the chain will contain the tree for the simplified
11490 form of the builtin function call. */
11492 static tree
11493 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11495 if (!validate_arg (s1, POINTER_TYPE)
11496 || !validate_arg (s2, POINTER_TYPE))
11497 return NULL_TREE;
11498 else
11500 tree fn;
11501 const char *p1, *p2;
11503 p2 = c_getstr (s2);
11504 if (p2 == NULL)
11505 return NULL_TREE;
11507 p1 = c_getstr (s1);
11508 if (p1 != NULL)
11510 const char *r = strstr (p1, p2);
11511 tree tem;
11513 if (r == NULL)
11514 return build_int_cst (TREE_TYPE (s1), 0);
11516 /* Return an offset into the constant string argument. */
11517 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11518 return fold_convert_loc (loc, type, tem);
11521 /* The argument is const char *, and the result is char *, so we need
11522 a type conversion here to avoid a warning. */
11523 if (p2[0] == '\0')
11524 return fold_convert_loc (loc, type, s1);
11526 if (p2[1] != '\0')
11527 return NULL_TREE;
11529 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11530 if (!fn)
11531 return NULL_TREE;
11533 /* New argument list transforming strstr(s1, s2) to
11534 strchr(s1, s2[0]). */
11535 return build_call_expr_loc (loc, fn, 2, s1,
11536 build_int_cst (integer_type_node, p2[0]));
11540 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11541 the call, and TYPE is its return type.
11543 Return NULL_TREE if no simplification was possible, otherwise return the
11544 simplified form of the call as a tree.
11546 The simplified form may be a constant or other expression which
11547 computes the same value, but in a more efficient manner (including
11548 calls to other builtin functions).
11550 The call may contain arguments which need to be evaluated, but
11551 which are not useful to determine the result of the call. In
11552 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11553 COMPOUND_EXPR will be an argument which must be evaluated.
11554 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11555 COMPOUND_EXPR in the chain will contain the tree for the simplified
11556 form of the builtin function call. */
11558 static tree
11559 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11561 if (!validate_arg (s1, POINTER_TYPE)
11562 || !validate_arg (s2, INTEGER_TYPE))
11563 return NULL_TREE;
11564 else
11566 const char *p1;
11568 if (TREE_CODE (s2) != INTEGER_CST)
11569 return NULL_TREE;
11571 p1 = c_getstr (s1);
11572 if (p1 != NULL)
11574 char c;
11575 const char *r;
11576 tree tem;
11578 if (target_char_cast (s2, &c))
11579 return NULL_TREE;
11581 r = strchr (p1, c);
11583 if (r == NULL)
11584 return build_int_cst (TREE_TYPE (s1), 0);
11586 /* Return an offset into the constant string argument. */
11587 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11588 return fold_convert_loc (loc, type, tem);
11590 return NULL_TREE;
11594 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11595 the call, and TYPE is its return type.
11597 Return NULL_TREE if no simplification was possible, otherwise return the
11598 simplified form of the call as a tree.
11600 The simplified form may be a constant or other expression which
11601 computes the same value, but in a more efficient manner (including
11602 calls to other builtin functions).
11604 The call may contain arguments which need to be evaluated, but
11605 which are not useful to determine the result of the call. In
11606 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11607 COMPOUND_EXPR will be an argument which must be evaluated.
11608 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11609 COMPOUND_EXPR in the chain will contain the tree for the simplified
11610 form of the builtin function call. */
11612 static tree
11613 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11615 if (!validate_arg (s1, POINTER_TYPE)
11616 || !validate_arg (s2, INTEGER_TYPE))
11617 return NULL_TREE;
11618 else
11620 tree fn;
11621 const char *p1;
11623 if (TREE_CODE (s2) != INTEGER_CST)
11624 return NULL_TREE;
11626 p1 = c_getstr (s1);
11627 if (p1 != NULL)
11629 char c;
11630 const char *r;
11631 tree tem;
11633 if (target_char_cast (s2, &c))
11634 return NULL_TREE;
11636 r = strrchr (p1, c);
11638 if (r == NULL)
11639 return build_int_cst (TREE_TYPE (s1), 0);
11641 /* Return an offset into the constant string argument. */
11642 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11643 return fold_convert_loc (loc, type, tem);
11646 if (! integer_zerop (s2))
11647 return NULL_TREE;
11649 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11650 if (!fn)
11651 return NULL_TREE;
11653 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11654 return build_call_expr_loc (loc, fn, 2, s1, s2);
11658 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11659 to the call, and TYPE is its return type.
11661 Return NULL_TREE if no simplification was possible, otherwise return the
11662 simplified form of the call as a tree.
11664 The simplified form may be a constant or other expression which
11665 computes the same value, but in a more efficient manner (including
11666 calls to other builtin functions).
11668 The call may contain arguments which need to be evaluated, but
11669 which are not useful to determine the result of the call. In
11670 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11671 COMPOUND_EXPR will be an argument which must be evaluated.
11672 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11673 COMPOUND_EXPR in the chain will contain the tree for the simplified
11674 form of the builtin function call. */
11676 static tree
11677 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11679 if (!validate_arg (s1, POINTER_TYPE)
11680 || !validate_arg (s2, POINTER_TYPE))
11681 return NULL_TREE;
11682 else
11684 tree fn;
11685 const char *p1, *p2;
11687 p2 = c_getstr (s2);
11688 if (p2 == NULL)
11689 return NULL_TREE;
11691 p1 = c_getstr (s1);
11692 if (p1 != NULL)
11694 const char *r = strpbrk (p1, p2);
11695 tree tem;
11697 if (r == NULL)
11698 return build_int_cst (TREE_TYPE (s1), 0);
11700 /* Return an offset into the constant string argument. */
11701 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11702 return fold_convert_loc (loc, type, tem);
11705 if (p2[0] == '\0')
11706 /* strpbrk(x, "") == NULL.
11707 Evaluate and ignore s1 in case it had side-effects. */
11708 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11710 if (p2[1] != '\0')
11711 return NULL_TREE; /* Really call strpbrk. */
11713 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11714 if (!fn)
11715 return NULL_TREE;
11717 /* New argument list transforming strpbrk(s1, s2) to
11718 strchr(s1, s2[0]). */
11719 return build_call_expr_loc (loc, fn, 2, s1,
11720 build_int_cst (integer_type_node, p2[0]));
11724 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11725 to the call.
11727 Return NULL_TREE if no simplification was possible, otherwise return the
11728 simplified form of the call as a tree.
11730 The simplified form may be a constant or other expression which
11731 computes the same value, but in a more efficient manner (including
11732 calls to other builtin functions).
11734 The call may contain arguments which need to be evaluated, but
11735 which are not useful to determine the result of the call. In
11736 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11737 COMPOUND_EXPR will be an argument which must be evaluated.
11738 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11739 COMPOUND_EXPR in the chain will contain the tree for the simplified
11740 form of the builtin function call. */
11742 tree
11743 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src,
11744 tree len)
11746 if (!validate_arg (dst, POINTER_TYPE)
11747 || !validate_arg (src, POINTER_TYPE))
11748 return NULL_TREE;
11749 else
11751 const char *p = c_getstr (src);
11753 /* If the string length is zero, return the dst parameter. */
11754 if (p && *p == '\0')
11755 return dst;
11757 if (optimize_insn_for_speed_p ())
11759 /* See if we can store by pieces into (dst + strlen(dst)). */
11760 tree newdst, call;
11761 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11762 tree memcpy_fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
11764 if (!strlen_fn || !memcpy_fn)
11765 return NULL_TREE;
11767 /* If the length of the source string isn't computable don't
11768 split strcat into strlen and memcpy. */
11769 if (! len)
11770 len = c_strlen (src, 1);
11771 if (! len || TREE_SIDE_EFFECTS (len))
11772 return NULL_TREE;
11774 /* Stabilize the argument list. */
11775 dst = builtin_save_expr (dst);
11777 /* Create strlen (dst). */
11778 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11779 /* Create (dst p+ strlen (dst)). */
11781 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11782 newdst = builtin_save_expr (newdst);
11784 len = fold_convert_loc (loc, size_type_node, len);
11785 len = size_binop_loc (loc, PLUS_EXPR, len,
11786 build_int_cst (size_type_node, 1));
11788 call = build_call_expr_loc (loc, memcpy_fn, 3, newdst, src, len);
11789 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11791 return NULL_TREE;
11795 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11796 arguments to the call.
11798 Return NULL_TREE if no simplification was possible, otherwise return the
11799 simplified form of the call as a tree.
11801 The simplified form may be a constant or other expression which
11802 computes the same value, but in a more efficient manner (including
11803 calls to other builtin functions).
11805 The call may contain arguments which need to be evaluated, but
11806 which are not useful to determine the result of the call. In
11807 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11808 COMPOUND_EXPR will be an argument which must be evaluated.
11809 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11810 COMPOUND_EXPR in the chain will contain the tree for the simplified
11811 form of the builtin function call. */
11813 static tree
11814 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11816 if (!validate_arg (dst, POINTER_TYPE)
11817 || !validate_arg (src, POINTER_TYPE)
11818 || !validate_arg (len, INTEGER_TYPE))
11819 return NULL_TREE;
11820 else
11822 const char *p = c_getstr (src);
11824 /* If the requested length is zero, or the src parameter string
11825 length is zero, return the dst parameter. */
11826 if (integer_zerop (len) || (p && *p == '\0'))
11827 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11829 /* If the requested len is greater than or equal to the string
11830 length, call strcat. */
11831 if (TREE_CODE (len) == INTEGER_CST && p
11832 && compare_tree_int (len, strlen (p)) >= 0)
11834 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11836 /* If the replacement _DECL isn't initialized, don't do the
11837 transformation. */
11838 if (!fn)
11839 return NULL_TREE;
11841 return build_call_expr_loc (loc, fn, 2, dst, src);
11843 return NULL_TREE;
11847 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11848 to the call.
11850 Return NULL_TREE if no simplification was possible, otherwise return the
11851 simplified form of the call as a tree.
11853 The simplified form may be a constant or other expression which
11854 computes the same value, but in a more efficient manner (including
11855 calls to other builtin functions).
11857 The call may contain arguments which need to be evaluated, but
11858 which are not useful to determine the result of the call. In
11859 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11860 COMPOUND_EXPR will be an argument which must be evaluated.
11861 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11862 COMPOUND_EXPR in the chain will contain the tree for the simplified
11863 form of the builtin function call. */
11865 static tree
11866 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11868 if (!validate_arg (s1, POINTER_TYPE)
11869 || !validate_arg (s2, POINTER_TYPE))
11870 return NULL_TREE;
11871 else
11873 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11875 /* If both arguments are constants, evaluate at compile-time. */
11876 if (p1 && p2)
11878 const size_t r = strspn (p1, p2);
11879 return build_int_cst (size_type_node, r);
11882 /* If either argument is "", return NULL_TREE. */
11883 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11884 /* Evaluate and ignore both arguments in case either one has
11885 side-effects. */
11886 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11887 s1, s2);
11888 return NULL_TREE;
11892 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11893 to the call.
11895 Return NULL_TREE if no simplification was possible, otherwise return the
11896 simplified form of the call as a tree.
11898 The simplified form may be a constant or other expression which
11899 computes the same value, but in a more efficient manner (including
11900 calls to other builtin functions).
11902 The call may contain arguments which need to be evaluated, but
11903 which are not useful to determine the result of the call. In
11904 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11905 COMPOUND_EXPR will be an argument which must be evaluated.
11906 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11907 COMPOUND_EXPR in the chain will contain the tree for the simplified
11908 form of the builtin function call. */
11910 static tree
11911 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11913 if (!validate_arg (s1, POINTER_TYPE)
11914 || !validate_arg (s2, POINTER_TYPE))
11915 return NULL_TREE;
11916 else
11918 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11920 /* If both arguments are constants, evaluate at compile-time. */
11921 if (p1 && p2)
11923 const size_t r = strcspn (p1, p2);
11924 return build_int_cst (size_type_node, r);
11927 /* If the first argument is "", return NULL_TREE. */
11928 if (p1 && *p1 == '\0')
11930 /* Evaluate and ignore argument s2 in case it has
11931 side-effects. */
11932 return omit_one_operand_loc (loc, size_type_node,
11933 size_zero_node, s2);
11936 /* If the second argument is "", return __builtin_strlen(s1). */
11937 if (p2 && *p2 == '\0')
11939 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11941 /* If the replacement _DECL isn't initialized, don't do the
11942 transformation. */
11943 if (!fn)
11944 return NULL_TREE;
11946 return build_call_expr_loc (loc, fn, 1, s1);
11948 return NULL_TREE;
11952 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11953 to the call. IGNORE is true if the value returned
11954 by the builtin will be ignored. UNLOCKED is true is true if this
11955 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11956 the known length of the string. Return NULL_TREE if no simplification
11957 was possible. */
11959 tree
11960 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11961 bool ignore, bool unlocked, tree len)
11963 /* If we're using an unlocked function, assume the other unlocked
11964 functions exist explicitly. */
11965 tree const fn_fputc = (unlocked
11966 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11967 : builtin_decl_implicit (BUILT_IN_FPUTC));
11968 tree const fn_fwrite = (unlocked
11969 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
11970 : builtin_decl_implicit (BUILT_IN_FWRITE));
11972 /* If the return value is used, don't do the transformation. */
11973 if (!ignore)
11974 return NULL_TREE;
11976 /* Verify the arguments in the original call. */
11977 if (!validate_arg (arg0, POINTER_TYPE)
11978 || !validate_arg (arg1, POINTER_TYPE))
11979 return NULL_TREE;
11981 if (! len)
11982 len = c_strlen (arg0, 0);
11984 /* Get the length of the string passed to fputs. If the length
11985 can't be determined, punt. */
11986 if (!len
11987 || TREE_CODE (len) != INTEGER_CST)
11988 return NULL_TREE;
11990 switch (compare_tree_int (len, 1))
11992 case -1: /* length is 0, delete the call entirely . */
11993 return omit_one_operand_loc (loc, integer_type_node,
11994 integer_zero_node, arg1);;
11996 case 0: /* length is 1, call fputc. */
11998 const char *p = c_getstr (arg0);
12000 if (p != NULL)
12002 if (fn_fputc)
12003 return build_call_expr_loc (loc, fn_fputc, 2,
12004 build_int_cst
12005 (integer_type_node, p[0]), arg1);
12006 else
12007 return NULL_TREE;
12010 /* FALLTHROUGH */
12011 case 1: /* length is greater than 1, call fwrite. */
12013 /* If optimizing for size keep fputs. */
12014 if (optimize_function_for_size_p (cfun))
12015 return NULL_TREE;
12016 /* New argument list transforming fputs(string, stream) to
12017 fwrite(string, 1, len, stream). */
12018 if (fn_fwrite)
12019 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12020 size_one_node, len, arg1);
12021 else
12022 return NULL_TREE;
12024 default:
12025 gcc_unreachable ();
12027 return NULL_TREE;
12030 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12031 produced. False otherwise. This is done so that we don't output the error
12032 or warning twice or three times. */
12034 bool
12035 fold_builtin_next_arg (tree exp, bool va_start_p)
12037 tree fntype = TREE_TYPE (current_function_decl);
12038 int nargs = call_expr_nargs (exp);
12039 tree arg;
12040 /* There is good chance the current input_location points inside the
12041 definition of the va_start macro (perhaps on the token for
12042 builtin) in a system header, so warnings will not be emitted.
12043 Use the location in real source code. */
12044 source_location current_location =
12045 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
12046 NULL);
12048 if (!stdarg_p (fntype))
12050 error ("%<va_start%> used in function with fixed args");
12051 return true;
12054 if (va_start_p)
12056 if (va_start_p && (nargs != 2))
12058 error ("wrong number of arguments to function %<va_start%>");
12059 return true;
12061 arg = CALL_EXPR_ARG (exp, 1);
12063 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12064 when we checked the arguments and if needed issued a warning. */
12065 else
12067 if (nargs == 0)
12069 /* Evidently an out of date version of <stdarg.h>; can't validate
12070 va_start's second argument, but can still work as intended. */
12071 warning_at (current_location,
12072 OPT_Wvarargs,
12073 "%<__builtin_next_arg%> called without an argument");
12074 return true;
12076 else if (nargs > 1)
12078 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12079 return true;
12081 arg = CALL_EXPR_ARG (exp, 0);
12084 if (TREE_CODE (arg) == SSA_NAME)
12085 arg = SSA_NAME_VAR (arg);
12087 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12088 or __builtin_next_arg (0) the first time we see it, after checking
12089 the arguments and if needed issuing a warning. */
12090 if (!integer_zerop (arg))
12092 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12094 /* Strip off all nops for the sake of the comparison. This
12095 is not quite the same as STRIP_NOPS. It does more.
12096 We must also strip off INDIRECT_EXPR for C++ reference
12097 parameters. */
12098 while (CONVERT_EXPR_P (arg)
12099 || TREE_CODE (arg) == INDIRECT_REF)
12100 arg = TREE_OPERAND (arg, 0);
12101 if (arg != last_parm)
12103 /* FIXME: Sometimes with the tree optimizers we can get the
12104 not the last argument even though the user used the last
12105 argument. We just warn and set the arg to be the last
12106 argument so that we will get wrong-code because of
12107 it. */
12108 warning_at (current_location,
12109 OPT_Wvarargs,
12110 "second parameter of %<va_start%> not last named argument");
12113 /* Undefined by C99 7.15.1.4p4 (va_start):
12114 "If the parameter parmN is declared with the register storage
12115 class, with a function or array type, or with a type that is
12116 not compatible with the type that results after application of
12117 the default argument promotions, the behavior is undefined."
12119 else if (DECL_REGISTER (arg))
12121 warning_at (current_location,
12122 OPT_Wvarargs,
12123 "undefined behaviour when second parameter of "
12124 "%<va_start%> is declared with %<register%> storage");
12127 /* We want to verify the second parameter just once before the tree
12128 optimizers are run and then avoid keeping it in the tree,
12129 as otherwise we could warn even for correct code like:
12130 void foo (int i, ...)
12131 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12132 if (va_start_p)
12133 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12134 else
12135 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12137 return false;
12141 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12142 ORIG may be null if this is a 2-argument call. We don't attempt to
12143 simplify calls with more than 3 arguments.
12145 Return NULL_TREE if no simplification was possible, otherwise return the
12146 simplified form of the call as a tree. If IGNORED is true, it means that
12147 the caller does not use the returned value of the function. */
12149 static tree
12150 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12151 tree orig, int ignored)
12153 tree call, retval;
12154 const char *fmt_str = NULL;
12156 /* Verify the required arguments in the original call. We deal with two
12157 types of sprintf() calls: 'sprintf (str, fmt)' and
12158 'sprintf (dest, "%s", orig)'. */
12159 if (!validate_arg (dest, POINTER_TYPE)
12160 || !validate_arg (fmt, POINTER_TYPE))
12161 return NULL_TREE;
12162 if (orig && !validate_arg (orig, POINTER_TYPE))
12163 return NULL_TREE;
12165 /* Check whether the format is a literal string constant. */
12166 fmt_str = c_getstr (fmt);
12167 if (fmt_str == NULL)
12168 return NULL_TREE;
12170 call = NULL_TREE;
12171 retval = NULL_TREE;
12173 if (!init_target_chars ())
12174 return NULL_TREE;
12176 /* If the format doesn't contain % args or %%, use strcpy. */
12177 if (strchr (fmt_str, target_percent) == NULL)
12179 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12181 if (!fn)
12182 return NULL_TREE;
12184 /* Don't optimize sprintf (buf, "abc", ptr++). */
12185 if (orig)
12186 return NULL_TREE;
12188 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12189 'format' is known to contain no % formats. */
12190 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12191 if (!ignored)
12192 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12195 /* If the format is "%s", use strcpy if the result isn't used. */
12196 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12198 tree fn;
12199 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12201 if (!fn)
12202 return NULL_TREE;
12204 /* Don't crash on sprintf (str1, "%s"). */
12205 if (!orig)
12206 return NULL_TREE;
12208 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12209 if (!ignored)
12211 retval = c_strlen (orig, 1);
12212 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12213 return NULL_TREE;
12215 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12218 if (call && retval)
12220 retval = fold_convert_loc
12221 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12222 retval);
12223 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12225 else
12226 return call;
12229 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12230 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12231 attempt to simplify calls with more than 4 arguments.
12233 Return NULL_TREE if no simplification was possible, otherwise return the
12234 simplified form of the call as a tree. If IGNORED is true, it means that
12235 the caller does not use the returned value of the function. */
12237 static tree
12238 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12239 tree orig, int ignored)
12241 tree call, retval;
12242 const char *fmt_str = NULL;
12243 unsigned HOST_WIDE_INT destlen;
12245 /* Verify the required arguments in the original call. We deal with two
12246 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12247 'snprintf (dest, cst, "%s", orig)'. */
12248 if (!validate_arg (dest, POINTER_TYPE)
12249 || !validate_arg (destsize, INTEGER_TYPE)
12250 || !validate_arg (fmt, POINTER_TYPE))
12251 return NULL_TREE;
12252 if (orig && !validate_arg (orig, POINTER_TYPE))
12253 return NULL_TREE;
12255 if (!tree_fits_uhwi_p (destsize))
12256 return NULL_TREE;
12258 /* Check whether the format is a literal string constant. */
12259 fmt_str = c_getstr (fmt);
12260 if (fmt_str == NULL)
12261 return NULL_TREE;
12263 call = NULL_TREE;
12264 retval = NULL_TREE;
12266 if (!init_target_chars ())
12267 return NULL_TREE;
12269 destlen = tree_to_uhwi (destsize);
12271 /* If the format doesn't contain % args or %%, use strcpy. */
12272 if (strchr (fmt_str, target_percent) == NULL)
12274 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12275 size_t len = strlen (fmt_str);
12277 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12278 if (orig)
12279 return NULL_TREE;
12281 /* We could expand this as
12282 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12283 or to
12284 memcpy (str, fmt_with_nul_at_cstm1, cst);
12285 but in the former case that might increase code size
12286 and in the latter case grow .rodata section too much.
12287 So punt for now. */
12288 if (len >= destlen)
12289 return NULL_TREE;
12291 if (!fn)
12292 return NULL_TREE;
12294 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12295 'format' is known to contain no % formats and
12296 strlen (fmt) < cst. */
12297 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12299 if (!ignored)
12300 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12303 /* If the format is "%s", use strcpy if the result isn't used. */
12304 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12306 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12307 unsigned HOST_WIDE_INT origlen;
12309 /* Don't crash on snprintf (str1, cst, "%s"). */
12310 if (!orig)
12311 return NULL_TREE;
12313 retval = c_strlen (orig, 1);
12314 if (!retval || !tree_fits_uhwi_p (retval))
12315 return NULL_TREE;
12317 origlen = tree_to_uhwi (retval);
12318 /* We could expand this as
12319 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12320 or to
12321 memcpy (str1, str2_with_nul_at_cstm1, cst);
12322 but in the former case that might increase code size
12323 and in the latter case grow .rodata section too much.
12324 So punt for now. */
12325 if (origlen >= destlen)
12326 return NULL_TREE;
12328 /* Convert snprintf (str1, cst, "%s", str2) into
12329 strcpy (str1, str2) if strlen (str2) < cst. */
12330 if (!fn)
12331 return NULL_TREE;
12333 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12335 if (ignored)
12336 retval = NULL_TREE;
12339 if (call && retval)
12341 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12342 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12343 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12345 else
12346 return call;
12349 /* Expand a call EXP to __builtin_object_size. */
12352 expand_builtin_object_size (tree exp)
12354 tree ost;
12355 int object_size_type;
12356 tree fndecl = get_callee_fndecl (exp);
12358 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12360 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12361 exp, fndecl);
12362 expand_builtin_trap ();
12363 return const0_rtx;
12366 ost = CALL_EXPR_ARG (exp, 1);
12367 STRIP_NOPS (ost);
12369 if (TREE_CODE (ost) != INTEGER_CST
12370 || tree_int_cst_sgn (ost) < 0
12371 || compare_tree_int (ost, 3) > 0)
12373 error ("%Klast argument of %D is not integer constant between 0 and 3",
12374 exp, fndecl);
12375 expand_builtin_trap ();
12376 return const0_rtx;
12379 object_size_type = tree_to_shwi (ost);
12381 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12384 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12385 FCODE is the BUILT_IN_* to use.
12386 Return NULL_RTX if we failed; the caller should emit a normal call,
12387 otherwise try to get the result in TARGET, if convenient (and in
12388 mode MODE if that's convenient). */
12390 static rtx
12391 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12392 enum built_in_function fcode)
12394 tree dest, src, len, size;
12396 if (!validate_arglist (exp,
12397 POINTER_TYPE,
12398 fcode == BUILT_IN_MEMSET_CHK
12399 ? INTEGER_TYPE : POINTER_TYPE,
12400 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12401 return NULL_RTX;
12403 dest = CALL_EXPR_ARG (exp, 0);
12404 src = CALL_EXPR_ARG (exp, 1);
12405 len = CALL_EXPR_ARG (exp, 2);
12406 size = CALL_EXPR_ARG (exp, 3);
12408 if (! tree_fits_uhwi_p (size))
12409 return NULL_RTX;
12411 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
12413 tree fn;
12415 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12417 warning_at (tree_nonartificial_location (exp),
12418 0, "%Kcall to %D will always overflow destination buffer",
12419 exp, get_callee_fndecl (exp));
12420 return NULL_RTX;
12423 fn = NULL_TREE;
12424 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12425 mem{cpy,pcpy,move,set} is available. */
12426 switch (fcode)
12428 case BUILT_IN_MEMCPY_CHK:
12429 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12430 break;
12431 case BUILT_IN_MEMPCPY_CHK:
12432 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12433 break;
12434 case BUILT_IN_MEMMOVE_CHK:
12435 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12436 break;
12437 case BUILT_IN_MEMSET_CHK:
12438 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12439 break;
12440 default:
12441 break;
12444 if (! fn)
12445 return NULL_RTX;
12447 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12448 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12449 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12450 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12452 else if (fcode == BUILT_IN_MEMSET_CHK)
12453 return NULL_RTX;
12454 else
12456 unsigned int dest_align = get_pointer_alignment (dest);
12458 /* If DEST is not a pointer type, call the normal function. */
12459 if (dest_align == 0)
12460 return NULL_RTX;
12462 /* If SRC and DEST are the same (and not volatile), do nothing. */
12463 if (operand_equal_p (src, dest, 0))
12465 tree expr;
12467 if (fcode != BUILT_IN_MEMPCPY_CHK)
12469 /* Evaluate and ignore LEN in case it has side-effects. */
12470 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12471 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12474 expr = fold_build_pointer_plus (dest, len);
12475 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12478 /* __memmove_chk special case. */
12479 if (fcode == BUILT_IN_MEMMOVE_CHK)
12481 unsigned int src_align = get_pointer_alignment (src);
12483 if (src_align == 0)
12484 return NULL_RTX;
12486 /* If src is categorized for a readonly section we can use
12487 normal __memcpy_chk. */
12488 if (readonly_data_expr (src))
12490 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12491 if (!fn)
12492 return NULL_RTX;
12493 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12494 dest, src, len, size);
12495 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12496 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12497 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12500 return NULL_RTX;
12504 /* Emit warning if a buffer overflow is detected at compile time. */
12506 static void
12507 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12509 int is_strlen = 0;
12510 tree len, size;
12511 location_t loc = tree_nonartificial_location (exp);
12513 switch (fcode)
12515 case BUILT_IN_STRCPY_CHK:
12516 case BUILT_IN_STPCPY_CHK:
12517 /* For __strcat_chk the warning will be emitted only if overflowing
12518 by at least strlen (dest) + 1 bytes. */
12519 case BUILT_IN_STRCAT_CHK:
12520 len = CALL_EXPR_ARG (exp, 1);
12521 size = CALL_EXPR_ARG (exp, 2);
12522 is_strlen = 1;
12523 break;
12524 case BUILT_IN_STRNCAT_CHK:
12525 case BUILT_IN_STRNCPY_CHK:
12526 case BUILT_IN_STPNCPY_CHK:
12527 len = CALL_EXPR_ARG (exp, 2);
12528 size = CALL_EXPR_ARG (exp, 3);
12529 break;
12530 case BUILT_IN_SNPRINTF_CHK:
12531 case BUILT_IN_VSNPRINTF_CHK:
12532 len = CALL_EXPR_ARG (exp, 1);
12533 size = CALL_EXPR_ARG (exp, 3);
12534 break;
12535 default:
12536 gcc_unreachable ();
12539 if (!len || !size)
12540 return;
12542 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12543 return;
12545 if (is_strlen)
12547 len = c_strlen (len, 1);
12548 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12549 return;
12551 else if (fcode == BUILT_IN_STRNCAT_CHK)
12553 tree src = CALL_EXPR_ARG (exp, 1);
12554 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12555 return;
12556 src = c_strlen (src, 1);
12557 if (! src || ! tree_fits_uhwi_p (src))
12559 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12560 exp, get_callee_fndecl (exp));
12561 return;
12563 else if (tree_int_cst_lt (src, size))
12564 return;
12566 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
12567 return;
12569 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12570 exp, get_callee_fndecl (exp));
12573 /* Emit warning if a buffer overflow is detected at compile time
12574 in __sprintf_chk/__vsprintf_chk calls. */
12576 static void
12577 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12579 tree size, len, fmt;
12580 const char *fmt_str;
12581 int nargs = call_expr_nargs (exp);
12583 /* Verify the required arguments in the original call. */
12585 if (nargs < 4)
12586 return;
12587 size = CALL_EXPR_ARG (exp, 2);
12588 fmt = CALL_EXPR_ARG (exp, 3);
12590 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12591 return;
12593 /* Check whether the format is a literal string constant. */
12594 fmt_str = c_getstr (fmt);
12595 if (fmt_str == NULL)
12596 return;
12598 if (!init_target_chars ())
12599 return;
12601 /* If the format doesn't contain % args or %%, we know its size. */
12602 if (strchr (fmt_str, target_percent) == 0)
12603 len = build_int_cstu (size_type_node, strlen (fmt_str));
12604 /* If the format is "%s" and first ... argument is a string literal,
12605 we know it too. */
12606 else if (fcode == BUILT_IN_SPRINTF_CHK
12607 && strcmp (fmt_str, target_percent_s) == 0)
12609 tree arg;
12611 if (nargs < 5)
12612 return;
12613 arg = CALL_EXPR_ARG (exp, 4);
12614 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12615 return;
12617 len = c_strlen (arg, 1);
12618 if (!len || ! tree_fits_uhwi_p (len))
12619 return;
12621 else
12622 return;
12624 if (! tree_int_cst_lt (len, size))
12625 warning_at (tree_nonartificial_location (exp),
12626 0, "%Kcall to %D will always overflow destination buffer",
12627 exp, get_callee_fndecl (exp));
12630 /* Emit warning if a free is called with address of a variable. */
12632 static void
12633 maybe_emit_free_warning (tree exp)
12635 tree arg = CALL_EXPR_ARG (exp, 0);
12637 STRIP_NOPS (arg);
12638 if (TREE_CODE (arg) != ADDR_EXPR)
12639 return;
12641 arg = get_base_address (TREE_OPERAND (arg, 0));
12642 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12643 return;
12645 if (SSA_VAR_P (arg))
12646 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12647 "%Kattempt to free a non-heap object %qD", exp, arg);
12648 else
12649 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12650 "%Kattempt to free a non-heap object", exp);
12653 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12654 if possible. */
12656 tree
12657 fold_builtin_object_size (tree ptr, tree ost)
12659 unsigned HOST_WIDE_INT bytes;
12660 int object_size_type;
12662 if (!validate_arg (ptr, POINTER_TYPE)
12663 || !validate_arg (ost, INTEGER_TYPE))
12664 return NULL_TREE;
12666 STRIP_NOPS (ost);
12668 if (TREE_CODE (ost) != INTEGER_CST
12669 || tree_int_cst_sgn (ost) < 0
12670 || compare_tree_int (ost, 3) > 0)
12671 return NULL_TREE;
12673 object_size_type = tree_to_shwi (ost);
12675 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12676 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12677 and (size_t) 0 for types 2 and 3. */
12678 if (TREE_SIDE_EFFECTS (ptr))
12679 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12681 if (TREE_CODE (ptr) == ADDR_EXPR)
12683 bytes = compute_builtin_object_size (ptr, object_size_type);
12684 if (double_int_fits_to_tree_p (size_type_node,
12685 double_int::from_uhwi (bytes)))
12686 return build_int_cstu (size_type_node, bytes);
12688 else if (TREE_CODE (ptr) == SSA_NAME)
12690 /* If object size is not known yet, delay folding until
12691 later. Maybe subsequent passes will help determining
12692 it. */
12693 bytes = compute_builtin_object_size (ptr, object_size_type);
12694 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12695 && double_int_fits_to_tree_p (size_type_node,
12696 double_int::from_uhwi (bytes)))
12697 return build_int_cstu (size_type_node, bytes);
12700 return NULL_TREE;
12703 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12704 DEST, SRC, LEN, and SIZE are the arguments to the call.
12705 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12706 code of the builtin. If MAXLEN is not NULL, it is maximum length
12707 passed as third argument. */
12709 tree
12710 fold_builtin_memory_chk (location_t loc, tree fndecl,
12711 tree dest, tree src, tree len, tree size,
12712 tree maxlen, bool ignore,
12713 enum built_in_function fcode)
12715 tree fn;
12717 if (!validate_arg (dest, POINTER_TYPE)
12718 || !validate_arg (src,
12719 (fcode == BUILT_IN_MEMSET_CHK
12720 ? INTEGER_TYPE : POINTER_TYPE))
12721 || !validate_arg (len, INTEGER_TYPE)
12722 || !validate_arg (size, INTEGER_TYPE))
12723 return NULL_TREE;
12725 /* If SRC and DEST are the same (and not volatile), return DEST
12726 (resp. DEST+LEN for __mempcpy_chk). */
12727 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12729 if (fcode != BUILT_IN_MEMPCPY_CHK)
12730 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12731 dest, len);
12732 else
12734 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12735 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12739 if (! tree_fits_uhwi_p (size))
12740 return NULL_TREE;
12742 if (! integer_all_onesp (size))
12744 if (! tree_fits_uhwi_p (len))
12746 /* If LEN is not constant, try MAXLEN too.
12747 For MAXLEN only allow optimizing into non-_ocs function
12748 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12749 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12751 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12753 /* (void) __mempcpy_chk () can be optimized into
12754 (void) __memcpy_chk (). */
12755 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12756 if (!fn)
12757 return NULL_TREE;
12759 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12761 return NULL_TREE;
12764 else
12765 maxlen = len;
12767 if (tree_int_cst_lt (size, maxlen))
12768 return NULL_TREE;
12771 fn = NULL_TREE;
12772 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12773 mem{cpy,pcpy,move,set} is available. */
12774 switch (fcode)
12776 case BUILT_IN_MEMCPY_CHK:
12777 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12778 break;
12779 case BUILT_IN_MEMPCPY_CHK:
12780 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12781 break;
12782 case BUILT_IN_MEMMOVE_CHK:
12783 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12784 break;
12785 case BUILT_IN_MEMSET_CHK:
12786 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12787 break;
12788 default:
12789 break;
12792 if (!fn)
12793 return NULL_TREE;
12795 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12798 /* Fold a call to the __st[rp]cpy_chk builtin.
12799 DEST, SRC, and SIZE are the arguments to the call.
12800 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12801 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12802 strings passed as second argument. */
12804 tree
12805 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12806 tree src, tree size,
12807 tree maxlen, bool ignore,
12808 enum built_in_function fcode)
12810 tree len, fn;
12812 if (!validate_arg (dest, POINTER_TYPE)
12813 || !validate_arg (src, POINTER_TYPE)
12814 || !validate_arg (size, INTEGER_TYPE))
12815 return NULL_TREE;
12817 /* If SRC and DEST are the same (and not volatile), return DEST. */
12818 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12819 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12821 if (! tree_fits_uhwi_p (size))
12822 return NULL_TREE;
12824 if (! integer_all_onesp (size))
12826 len = c_strlen (src, 1);
12827 if (! len || ! tree_fits_uhwi_p (len))
12829 /* If LEN is not constant, try MAXLEN too.
12830 For MAXLEN only allow optimizing into non-_ocs function
12831 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12832 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12834 if (fcode == BUILT_IN_STPCPY_CHK)
12836 if (! ignore)
12837 return NULL_TREE;
12839 /* If return value of __stpcpy_chk is ignored,
12840 optimize into __strcpy_chk. */
12841 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12842 if (!fn)
12843 return NULL_TREE;
12845 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12848 if (! len || TREE_SIDE_EFFECTS (len))
12849 return NULL_TREE;
12851 /* If c_strlen returned something, but not a constant,
12852 transform __strcpy_chk into __memcpy_chk. */
12853 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12854 if (!fn)
12855 return NULL_TREE;
12857 len = fold_convert_loc (loc, size_type_node, len);
12858 len = size_binop_loc (loc, PLUS_EXPR, len,
12859 build_int_cst (size_type_node, 1));
12860 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12861 build_call_expr_loc (loc, fn, 4,
12862 dest, src, len, size));
12865 else
12866 maxlen = len;
12868 if (! tree_int_cst_lt (maxlen, size))
12869 return NULL_TREE;
12872 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12873 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12874 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12875 if (!fn)
12876 return NULL_TREE;
12878 return build_call_expr_loc (loc, fn, 2, dest, src);
12881 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12882 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12883 length passed as third argument. IGNORE is true if return value can be
12884 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12886 tree
12887 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12888 tree len, tree size, tree maxlen, bool ignore,
12889 enum built_in_function fcode)
12891 tree fn;
12893 if (!validate_arg (dest, POINTER_TYPE)
12894 || !validate_arg (src, POINTER_TYPE)
12895 || !validate_arg (len, INTEGER_TYPE)
12896 || !validate_arg (size, INTEGER_TYPE))
12897 return NULL_TREE;
12899 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12901 /* If return value of __stpncpy_chk is ignored,
12902 optimize into __strncpy_chk. */
12903 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12904 if (fn)
12905 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12908 if (! tree_fits_uhwi_p (size))
12909 return NULL_TREE;
12911 if (! integer_all_onesp (size))
12913 if (! tree_fits_uhwi_p (len))
12915 /* If LEN is not constant, try MAXLEN too.
12916 For MAXLEN only allow optimizing into non-_ocs function
12917 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12918 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12919 return NULL_TREE;
12921 else
12922 maxlen = len;
12924 if (tree_int_cst_lt (size, maxlen))
12925 return NULL_TREE;
12928 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12929 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12930 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12931 if (!fn)
12932 return NULL_TREE;
12934 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12937 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12938 are the arguments to the call. */
12940 static tree
12941 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12942 tree src, tree size)
12944 tree fn;
12945 const char *p;
12947 if (!validate_arg (dest, POINTER_TYPE)
12948 || !validate_arg (src, POINTER_TYPE)
12949 || !validate_arg (size, INTEGER_TYPE))
12950 return NULL_TREE;
12952 p = c_getstr (src);
12953 /* If the SRC parameter is "", return DEST. */
12954 if (p && *p == '\0')
12955 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12957 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
12958 return NULL_TREE;
12960 /* If __builtin_strcat_chk is used, assume strcat is available. */
12961 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12962 if (!fn)
12963 return NULL_TREE;
12965 return build_call_expr_loc (loc, fn, 2, dest, src);
12968 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12969 LEN, and SIZE. */
12971 static tree
12972 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12973 tree dest, tree src, tree len, tree size)
12975 tree fn;
12976 const char *p;
12978 if (!validate_arg (dest, POINTER_TYPE)
12979 || !validate_arg (src, POINTER_TYPE)
12980 || !validate_arg (size, INTEGER_TYPE)
12981 || !validate_arg (size, INTEGER_TYPE))
12982 return NULL_TREE;
12984 p = c_getstr (src);
12985 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12986 if (p && *p == '\0')
12987 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12988 else if (integer_zerop (len))
12989 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12991 if (! tree_fits_uhwi_p (size))
12992 return NULL_TREE;
12994 if (! integer_all_onesp (size))
12996 tree src_len = c_strlen (src, 1);
12997 if (src_len
12998 && tree_fits_uhwi_p (src_len)
12999 && tree_fits_uhwi_p (len)
13000 && ! tree_int_cst_lt (len, src_len))
13002 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
13003 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
13004 if (!fn)
13005 return NULL_TREE;
13007 return build_call_expr_loc (loc, fn, 3, dest, src, size);
13009 return NULL_TREE;
13012 /* If __builtin_strncat_chk is used, assume strncat is available. */
13013 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13014 if (!fn)
13015 return NULL_TREE;
13017 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13020 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13021 Return NULL_TREE if a normal call should be emitted rather than
13022 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13023 or BUILT_IN_VSPRINTF_CHK. */
13025 static tree
13026 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13027 enum built_in_function fcode)
13029 tree dest, size, len, fn, fmt, flag;
13030 const char *fmt_str;
13032 /* Verify the required arguments in the original call. */
13033 if (nargs < 4)
13034 return NULL_TREE;
13035 dest = args[0];
13036 if (!validate_arg (dest, POINTER_TYPE))
13037 return NULL_TREE;
13038 flag = args[1];
13039 if (!validate_arg (flag, INTEGER_TYPE))
13040 return NULL_TREE;
13041 size = args[2];
13042 if (!validate_arg (size, INTEGER_TYPE))
13043 return NULL_TREE;
13044 fmt = args[3];
13045 if (!validate_arg (fmt, POINTER_TYPE))
13046 return NULL_TREE;
13048 if (! tree_fits_uhwi_p (size))
13049 return NULL_TREE;
13051 len = NULL_TREE;
13053 if (!init_target_chars ())
13054 return NULL_TREE;
13056 /* Check whether the format is a literal string constant. */
13057 fmt_str = c_getstr (fmt);
13058 if (fmt_str != NULL)
13060 /* If the format doesn't contain % args or %%, we know the size. */
13061 if (strchr (fmt_str, target_percent) == 0)
13063 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13064 len = build_int_cstu (size_type_node, strlen (fmt_str));
13066 /* If the format is "%s" and first ... argument is a string literal,
13067 we know the size too. */
13068 else if (fcode == BUILT_IN_SPRINTF_CHK
13069 && strcmp (fmt_str, target_percent_s) == 0)
13071 tree arg;
13073 if (nargs == 5)
13075 arg = args[4];
13076 if (validate_arg (arg, POINTER_TYPE))
13078 len = c_strlen (arg, 1);
13079 if (! len || ! tree_fits_uhwi_p (len))
13080 len = NULL_TREE;
13086 if (! integer_all_onesp (size))
13088 if (! len || ! tree_int_cst_lt (len, size))
13089 return NULL_TREE;
13092 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13093 or if format doesn't contain % chars or is "%s". */
13094 if (! integer_zerop (flag))
13096 if (fmt_str == NULL)
13097 return NULL_TREE;
13098 if (strchr (fmt_str, target_percent) != NULL
13099 && strcmp (fmt_str, target_percent_s))
13100 return NULL_TREE;
13103 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13104 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13105 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13106 if (!fn)
13107 return NULL_TREE;
13109 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13112 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13113 a normal call should be emitted rather than expanding the function
13114 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13116 static tree
13117 fold_builtin_sprintf_chk (location_t loc, tree exp,
13118 enum built_in_function fcode)
13120 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13121 CALL_EXPR_ARGP (exp), fcode);
13124 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13125 NULL_TREE if a normal call should be emitted rather than expanding
13126 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13127 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13128 passed as second argument. */
13130 static tree
13131 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13132 tree maxlen, enum built_in_function fcode)
13134 tree dest, size, len, fn, fmt, flag;
13135 const char *fmt_str;
13137 /* Verify the required arguments in the original call. */
13138 if (nargs < 5)
13139 return NULL_TREE;
13140 dest = args[0];
13141 if (!validate_arg (dest, POINTER_TYPE))
13142 return NULL_TREE;
13143 len = args[1];
13144 if (!validate_arg (len, INTEGER_TYPE))
13145 return NULL_TREE;
13146 flag = args[2];
13147 if (!validate_arg (flag, INTEGER_TYPE))
13148 return NULL_TREE;
13149 size = args[3];
13150 if (!validate_arg (size, INTEGER_TYPE))
13151 return NULL_TREE;
13152 fmt = args[4];
13153 if (!validate_arg (fmt, POINTER_TYPE))
13154 return NULL_TREE;
13156 if (! tree_fits_uhwi_p (size))
13157 return NULL_TREE;
13159 if (! integer_all_onesp (size))
13161 if (! tree_fits_uhwi_p (len))
13163 /* If LEN is not constant, try MAXLEN too.
13164 For MAXLEN only allow optimizing into non-_ocs function
13165 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13166 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
13167 return NULL_TREE;
13169 else
13170 maxlen = len;
13172 if (tree_int_cst_lt (size, maxlen))
13173 return NULL_TREE;
13176 if (!init_target_chars ())
13177 return NULL_TREE;
13179 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13180 or if format doesn't contain % chars or is "%s". */
13181 if (! integer_zerop (flag))
13183 fmt_str = c_getstr (fmt);
13184 if (fmt_str == NULL)
13185 return NULL_TREE;
13186 if (strchr (fmt_str, target_percent) != NULL
13187 && strcmp (fmt_str, target_percent_s))
13188 return NULL_TREE;
13191 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13192 available. */
13193 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13194 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13195 if (!fn)
13196 return NULL_TREE;
13198 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13201 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13202 a normal call should be emitted rather than expanding the function
13203 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13204 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13205 passed as second argument. */
13207 static tree
13208 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13209 enum built_in_function fcode)
13211 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13212 CALL_EXPR_ARGP (exp), maxlen, fcode);
13215 /* Builtins with folding operations that operate on "..." arguments
13216 need special handling; we need to store the arguments in a convenient
13217 data structure before attempting any folding. Fortunately there are
13218 only a few builtins that fall into this category. FNDECL is the
13219 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13220 result of the function call is ignored. */
13222 static tree
13223 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
13224 bool ignore ATTRIBUTE_UNUSED)
13226 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13227 tree ret = NULL_TREE;
13229 switch (fcode)
13231 case BUILT_IN_SPRINTF_CHK:
13232 case BUILT_IN_VSPRINTF_CHK:
13233 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
13234 break;
13236 case BUILT_IN_SNPRINTF_CHK:
13237 case BUILT_IN_VSNPRINTF_CHK:
13238 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
13239 break;
13241 case BUILT_IN_FPCLASSIFY:
13242 ret = fold_builtin_fpclassify (loc, exp);
13243 break;
13245 default:
13246 break;
13248 if (ret)
13250 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13251 SET_EXPR_LOCATION (ret, loc);
13252 TREE_NO_WARNING (ret) = 1;
13253 return ret;
13255 return NULL_TREE;
13258 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13259 FMT and ARG are the arguments to the call; we don't fold cases with
13260 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13262 Return NULL_TREE if no simplification was possible, otherwise return the
13263 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13264 code of the function to be simplified. */
13266 static tree
13267 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13268 tree arg, bool ignore,
13269 enum built_in_function fcode)
13271 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13272 const char *fmt_str = NULL;
13274 /* If the return value is used, don't do the transformation. */
13275 if (! ignore)
13276 return NULL_TREE;
13278 /* Verify the required arguments in the original call. */
13279 if (!validate_arg (fmt, POINTER_TYPE))
13280 return NULL_TREE;
13282 /* Check whether the format is a literal string constant. */
13283 fmt_str = c_getstr (fmt);
13284 if (fmt_str == NULL)
13285 return NULL_TREE;
13287 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13289 /* If we're using an unlocked function, assume the other
13290 unlocked functions exist explicitly. */
13291 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13292 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13294 else
13296 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13297 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13300 if (!init_target_chars ())
13301 return NULL_TREE;
13303 if (strcmp (fmt_str, target_percent_s) == 0
13304 || strchr (fmt_str, target_percent) == NULL)
13306 const char *str;
13308 if (strcmp (fmt_str, target_percent_s) == 0)
13310 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13311 return NULL_TREE;
13313 if (!arg || !validate_arg (arg, POINTER_TYPE))
13314 return NULL_TREE;
13316 str = c_getstr (arg);
13317 if (str == NULL)
13318 return NULL_TREE;
13320 else
13322 /* The format specifier doesn't contain any '%' characters. */
13323 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13324 && arg)
13325 return NULL_TREE;
13326 str = fmt_str;
13329 /* If the string was "", printf does nothing. */
13330 if (str[0] == '\0')
13331 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13333 /* If the string has length of 1, call putchar. */
13334 if (str[1] == '\0')
13336 /* Given printf("c"), (where c is any one character,)
13337 convert "c"[0] to an int and pass that to the replacement
13338 function. */
13339 newarg = build_int_cst (integer_type_node, str[0]);
13340 if (fn_putchar)
13341 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13343 else
13345 /* If the string was "string\n", call puts("string"). */
13346 size_t len = strlen (str);
13347 if ((unsigned char)str[len - 1] == target_newline
13348 && (size_t) (int) len == len
13349 && (int) len > 0)
13351 char *newstr;
13352 tree offset_node, string_cst;
13354 /* Create a NUL-terminated string that's one char shorter
13355 than the original, stripping off the trailing '\n'. */
13356 newarg = build_string_literal (len, str);
13357 string_cst = string_constant (newarg, &offset_node);
13358 gcc_checking_assert (string_cst
13359 && (TREE_STRING_LENGTH (string_cst)
13360 == (int) len)
13361 && integer_zerop (offset_node)
13362 && (unsigned char)
13363 TREE_STRING_POINTER (string_cst)[len - 1]
13364 == target_newline);
13365 /* build_string_literal creates a new STRING_CST,
13366 modify it in place to avoid double copying. */
13367 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13368 newstr[len - 1] = '\0';
13369 if (fn_puts)
13370 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13372 else
13373 /* We'd like to arrange to call fputs(string,stdout) here,
13374 but we need stdout and don't have a way to get it yet. */
13375 return NULL_TREE;
13379 /* The other optimizations can be done only on the non-va_list variants. */
13380 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13381 return NULL_TREE;
13383 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13384 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13386 if (!arg || !validate_arg (arg, POINTER_TYPE))
13387 return NULL_TREE;
13388 if (fn_puts)
13389 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13392 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13393 else if (strcmp (fmt_str, target_percent_c) == 0)
13395 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13396 return NULL_TREE;
13397 if (fn_putchar)
13398 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13401 if (!call)
13402 return NULL_TREE;
13404 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13407 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13408 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13409 more than 3 arguments, and ARG may be null in the 2-argument case.
13411 Return NULL_TREE if no simplification was possible, otherwise return the
13412 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13413 code of the function to be simplified. */
13415 static tree
13416 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13417 tree fmt, tree arg, bool ignore,
13418 enum built_in_function fcode)
13420 tree fn_fputc, fn_fputs, call = NULL_TREE;
13421 const char *fmt_str = NULL;
13423 /* If the return value is used, don't do the transformation. */
13424 if (! ignore)
13425 return NULL_TREE;
13427 /* Verify the required arguments in the original call. */
13428 if (!validate_arg (fp, POINTER_TYPE))
13429 return NULL_TREE;
13430 if (!validate_arg (fmt, POINTER_TYPE))
13431 return NULL_TREE;
13433 /* Check whether the format is a literal string constant. */
13434 fmt_str = c_getstr (fmt);
13435 if (fmt_str == NULL)
13436 return NULL_TREE;
13438 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13440 /* If we're using an unlocked function, assume the other
13441 unlocked functions exist explicitly. */
13442 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13443 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13445 else
13447 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13448 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13451 if (!init_target_chars ())
13452 return NULL_TREE;
13454 /* If the format doesn't contain % args or %%, use strcpy. */
13455 if (strchr (fmt_str, target_percent) == NULL)
13457 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13458 && arg)
13459 return NULL_TREE;
13461 /* If the format specifier was "", fprintf does nothing. */
13462 if (fmt_str[0] == '\0')
13464 /* If FP has side-effects, just wait until gimplification is
13465 done. */
13466 if (TREE_SIDE_EFFECTS (fp))
13467 return NULL_TREE;
13469 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13472 /* When "string" doesn't contain %, replace all cases of
13473 fprintf (fp, string) with fputs (string, fp). The fputs
13474 builtin will take care of special cases like length == 1. */
13475 if (fn_fputs)
13476 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13479 /* The other optimizations can be done only on the non-va_list variants. */
13480 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13481 return NULL_TREE;
13483 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13484 else if (strcmp (fmt_str, target_percent_s) == 0)
13486 if (!arg || !validate_arg (arg, POINTER_TYPE))
13487 return NULL_TREE;
13488 if (fn_fputs)
13489 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13492 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13493 else if (strcmp (fmt_str, target_percent_c) == 0)
13495 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13496 return NULL_TREE;
13497 if (fn_fputc)
13498 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13501 if (!call)
13502 return NULL_TREE;
13503 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13506 /* Initialize format string characters in the target charset. */
13508 static bool
13509 init_target_chars (void)
13511 static bool init;
13512 if (!init)
13514 target_newline = lang_hooks.to_target_charset ('\n');
13515 target_percent = lang_hooks.to_target_charset ('%');
13516 target_c = lang_hooks.to_target_charset ('c');
13517 target_s = lang_hooks.to_target_charset ('s');
13518 if (target_newline == 0 || target_percent == 0 || target_c == 0
13519 || target_s == 0)
13520 return false;
13522 target_percent_c[0] = target_percent;
13523 target_percent_c[1] = target_c;
13524 target_percent_c[2] = '\0';
13526 target_percent_s[0] = target_percent;
13527 target_percent_s[1] = target_s;
13528 target_percent_s[2] = '\0';
13530 target_percent_s_newline[0] = target_percent;
13531 target_percent_s_newline[1] = target_s;
13532 target_percent_s_newline[2] = target_newline;
13533 target_percent_s_newline[3] = '\0';
13535 init = true;
13537 return true;
13540 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13541 and no overflow/underflow occurred. INEXACT is true if M was not
13542 exactly calculated. TYPE is the tree type for the result. This
13543 function assumes that you cleared the MPFR flags and then
13544 calculated M to see if anything subsequently set a flag prior to
13545 entering this function. Return NULL_TREE if any checks fail. */
13547 static tree
13548 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13550 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13551 overflow/underflow occurred. If -frounding-math, proceed iff the
13552 result of calling FUNC was exact. */
13553 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13554 && (!flag_rounding_math || !inexact))
13556 REAL_VALUE_TYPE rr;
13558 real_from_mpfr (&rr, m, type, GMP_RNDN);
13559 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13560 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13561 but the mpft_t is not, then we underflowed in the
13562 conversion. */
13563 if (real_isfinite (&rr)
13564 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13566 REAL_VALUE_TYPE rmode;
13568 real_convert (&rmode, TYPE_MODE (type), &rr);
13569 /* Proceed iff the specified mode can hold the value. */
13570 if (real_identical (&rmode, &rr))
13571 return build_real (type, rmode);
13574 return NULL_TREE;
13577 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13578 number and no overflow/underflow occurred. INEXACT is true if M
13579 was not exactly calculated. TYPE is the tree type for the result.
13580 This function assumes that you cleared the MPFR flags and then
13581 calculated M to see if anything subsequently set a flag prior to
13582 entering this function. Return NULL_TREE if any checks fail, if
13583 FORCE_CONVERT is true, then bypass the checks. */
13585 static tree
13586 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13588 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13589 overflow/underflow occurred. If -frounding-math, proceed iff the
13590 result of calling FUNC was exact. */
13591 if (force_convert
13592 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13593 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13594 && (!flag_rounding_math || !inexact)))
13596 REAL_VALUE_TYPE re, im;
13598 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13599 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13600 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13601 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13602 but the mpft_t is not, then we underflowed in the
13603 conversion. */
13604 if (force_convert
13605 || (real_isfinite (&re) && real_isfinite (&im)
13606 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13607 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13609 REAL_VALUE_TYPE re_mode, im_mode;
13611 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13612 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13613 /* Proceed iff the specified mode can hold the value. */
13614 if (force_convert
13615 || (real_identical (&re_mode, &re)
13616 && real_identical (&im_mode, &im)))
13617 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13618 build_real (TREE_TYPE (type), im_mode));
13621 return NULL_TREE;
13624 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13625 FUNC on it and return the resulting value as a tree with type TYPE.
13626 If MIN and/or MAX are not NULL, then the supplied ARG must be
13627 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13628 acceptable values, otherwise they are not. The mpfr precision is
13629 set to the precision of TYPE. We assume that function FUNC returns
13630 zero if the result could be calculated exactly within the requested
13631 precision. */
13633 static tree
13634 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13635 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13636 bool inclusive)
13638 tree result = NULL_TREE;
13640 STRIP_NOPS (arg);
13642 /* To proceed, MPFR must exactly represent the target floating point
13643 format, which only happens when the target base equals two. */
13644 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13645 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13647 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13649 if (real_isfinite (ra)
13650 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13651 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13653 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13654 const int prec = fmt->p;
13655 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13656 int inexact;
13657 mpfr_t m;
13659 mpfr_init2 (m, prec);
13660 mpfr_from_real (m, ra, GMP_RNDN);
13661 mpfr_clear_flags ();
13662 inexact = func (m, m, rnd);
13663 result = do_mpfr_ckconv (m, type, inexact);
13664 mpfr_clear (m);
13668 return result;
13671 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13672 FUNC on it and return the resulting value as a tree with type TYPE.
13673 The mpfr precision is set to the precision of TYPE. We assume that
13674 function FUNC returns zero if the result could be calculated
13675 exactly within the requested precision. */
13677 static tree
13678 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13679 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13681 tree result = NULL_TREE;
13683 STRIP_NOPS (arg1);
13684 STRIP_NOPS (arg2);
13686 /* To proceed, MPFR must exactly represent the target floating point
13687 format, which only happens when the target base equals two. */
13688 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13689 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13690 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13692 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13693 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13695 if (real_isfinite (ra1) && real_isfinite (ra2))
13697 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13698 const int prec = fmt->p;
13699 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13700 int inexact;
13701 mpfr_t m1, m2;
13703 mpfr_inits2 (prec, m1, m2, NULL);
13704 mpfr_from_real (m1, ra1, GMP_RNDN);
13705 mpfr_from_real (m2, ra2, GMP_RNDN);
13706 mpfr_clear_flags ();
13707 inexact = func (m1, m1, m2, rnd);
13708 result = do_mpfr_ckconv (m1, type, inexact);
13709 mpfr_clears (m1, m2, NULL);
13713 return result;
13716 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13717 FUNC on it and return the resulting value as a tree with type TYPE.
13718 The mpfr precision is set to the precision of TYPE. We assume that
13719 function FUNC returns zero if the result could be calculated
13720 exactly within the requested precision. */
13722 static tree
13723 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13724 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13726 tree result = NULL_TREE;
13728 STRIP_NOPS (arg1);
13729 STRIP_NOPS (arg2);
13730 STRIP_NOPS (arg3);
13732 /* To proceed, MPFR must exactly represent the target floating point
13733 format, which only happens when the target base equals two. */
13734 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13735 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13736 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13737 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13739 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13740 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13741 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13743 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13745 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13746 const int prec = fmt->p;
13747 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13748 int inexact;
13749 mpfr_t m1, m2, m3;
13751 mpfr_inits2 (prec, m1, m2, m3, NULL);
13752 mpfr_from_real (m1, ra1, GMP_RNDN);
13753 mpfr_from_real (m2, ra2, GMP_RNDN);
13754 mpfr_from_real (m3, ra3, GMP_RNDN);
13755 mpfr_clear_flags ();
13756 inexact = func (m1, m1, m2, m3, rnd);
13757 result = do_mpfr_ckconv (m1, type, inexact);
13758 mpfr_clears (m1, m2, m3, NULL);
13762 return result;
13765 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13766 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13767 If ARG_SINP and ARG_COSP are NULL then the result is returned
13768 as a complex value.
13769 The type is taken from the type of ARG and is used for setting the
13770 precision of the calculation and results. */
13772 static tree
13773 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13775 tree const type = TREE_TYPE (arg);
13776 tree result = NULL_TREE;
13778 STRIP_NOPS (arg);
13780 /* To proceed, MPFR must exactly represent the target floating point
13781 format, which only happens when the target base equals two. */
13782 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13783 && TREE_CODE (arg) == REAL_CST
13784 && !TREE_OVERFLOW (arg))
13786 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13788 if (real_isfinite (ra))
13790 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13791 const int prec = fmt->p;
13792 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13793 tree result_s, result_c;
13794 int inexact;
13795 mpfr_t m, ms, mc;
13797 mpfr_inits2 (prec, m, ms, mc, NULL);
13798 mpfr_from_real (m, ra, GMP_RNDN);
13799 mpfr_clear_flags ();
13800 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13801 result_s = do_mpfr_ckconv (ms, type, inexact);
13802 result_c = do_mpfr_ckconv (mc, type, inexact);
13803 mpfr_clears (m, ms, mc, NULL);
13804 if (result_s && result_c)
13806 /* If we are to return in a complex value do so. */
13807 if (!arg_sinp && !arg_cosp)
13808 return build_complex (build_complex_type (type),
13809 result_c, result_s);
13811 /* Dereference the sin/cos pointer arguments. */
13812 arg_sinp = build_fold_indirect_ref (arg_sinp);
13813 arg_cosp = build_fold_indirect_ref (arg_cosp);
13814 /* Proceed if valid pointer type were passed in. */
13815 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13816 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13818 /* Set the values. */
13819 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13820 result_s);
13821 TREE_SIDE_EFFECTS (result_s) = 1;
13822 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13823 result_c);
13824 TREE_SIDE_EFFECTS (result_c) = 1;
13825 /* Combine the assignments into a compound expr. */
13826 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13827 result_s, result_c));
13832 return result;
13835 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13836 two-argument mpfr order N Bessel function FUNC on them and return
13837 the resulting value as a tree with type TYPE. The mpfr precision
13838 is set to the precision of TYPE. We assume that function FUNC
13839 returns zero if the result could be calculated exactly within the
13840 requested precision. */
13841 static tree
13842 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13843 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13844 const REAL_VALUE_TYPE *min, bool inclusive)
13846 tree result = NULL_TREE;
13848 STRIP_NOPS (arg1);
13849 STRIP_NOPS (arg2);
13851 /* To proceed, MPFR must exactly represent the target floating point
13852 format, which only happens when the target base equals two. */
13853 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13854 && tree_fits_shwi_p (arg1)
13855 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13857 const HOST_WIDE_INT n = tree_to_shwi (arg1);
13858 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13860 if (n == (long)n
13861 && real_isfinite (ra)
13862 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13864 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13865 const int prec = fmt->p;
13866 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13867 int inexact;
13868 mpfr_t m;
13870 mpfr_init2 (m, prec);
13871 mpfr_from_real (m, ra, GMP_RNDN);
13872 mpfr_clear_flags ();
13873 inexact = func (m, n, m, rnd);
13874 result = do_mpfr_ckconv (m, type, inexact);
13875 mpfr_clear (m);
13879 return result;
13882 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13883 the pointer *(ARG_QUO) and return the result. The type is taken
13884 from the type of ARG0 and is used for setting the precision of the
13885 calculation and results. */
13887 static tree
13888 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13890 tree const type = TREE_TYPE (arg0);
13891 tree result = NULL_TREE;
13893 STRIP_NOPS (arg0);
13894 STRIP_NOPS (arg1);
13896 /* To proceed, MPFR must exactly represent the target floating point
13897 format, which only happens when the target base equals two. */
13898 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13899 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13900 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13902 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13903 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13905 if (real_isfinite (ra0) && real_isfinite (ra1))
13907 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13908 const int prec = fmt->p;
13909 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13910 tree result_rem;
13911 long integer_quo;
13912 mpfr_t m0, m1;
13914 mpfr_inits2 (prec, m0, m1, NULL);
13915 mpfr_from_real (m0, ra0, GMP_RNDN);
13916 mpfr_from_real (m1, ra1, GMP_RNDN);
13917 mpfr_clear_flags ();
13918 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13919 /* Remquo is independent of the rounding mode, so pass
13920 inexact=0 to do_mpfr_ckconv(). */
13921 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13922 mpfr_clears (m0, m1, NULL);
13923 if (result_rem)
13925 /* MPFR calculates quo in the host's long so it may
13926 return more bits in quo than the target int can hold
13927 if sizeof(host long) > sizeof(target int). This can
13928 happen even for native compilers in LP64 mode. In
13929 these cases, modulo the quo value with the largest
13930 number that the target int can hold while leaving one
13931 bit for the sign. */
13932 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13933 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13935 /* Dereference the quo pointer argument. */
13936 arg_quo = build_fold_indirect_ref (arg_quo);
13937 /* Proceed iff a valid pointer type was passed in. */
13938 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13940 /* Set the value. */
13941 tree result_quo
13942 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13943 build_int_cst (TREE_TYPE (arg_quo),
13944 integer_quo));
13945 TREE_SIDE_EFFECTS (result_quo) = 1;
13946 /* Combine the quo assignment with the rem. */
13947 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13948 result_quo, result_rem));
13953 return result;
13956 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13957 resulting value as a tree with type TYPE. The mpfr precision is
13958 set to the precision of TYPE. We assume that this mpfr function
13959 returns zero if the result could be calculated exactly within the
13960 requested precision. In addition, the integer pointer represented
13961 by ARG_SG will be dereferenced and set to the appropriate signgam
13962 (-1,1) value. */
13964 static tree
13965 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13967 tree result = NULL_TREE;
13969 STRIP_NOPS (arg);
13971 /* To proceed, MPFR must exactly represent the target floating point
13972 format, which only happens when the target base equals two. Also
13973 verify ARG is a constant and that ARG_SG is an int pointer. */
13974 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13975 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13976 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13977 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13979 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13981 /* In addition to NaN and Inf, the argument cannot be zero or a
13982 negative integer. */
13983 if (real_isfinite (ra)
13984 && ra->cl != rvc_zero
13985 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
13987 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13988 const int prec = fmt->p;
13989 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13990 int inexact, sg;
13991 mpfr_t m;
13992 tree result_lg;
13994 mpfr_init2 (m, prec);
13995 mpfr_from_real (m, ra, GMP_RNDN);
13996 mpfr_clear_flags ();
13997 inexact = mpfr_lgamma (m, &sg, m, rnd);
13998 result_lg = do_mpfr_ckconv (m, type, inexact);
13999 mpfr_clear (m);
14000 if (result_lg)
14002 tree result_sg;
14004 /* Dereference the arg_sg pointer argument. */
14005 arg_sg = build_fold_indirect_ref (arg_sg);
14006 /* Assign the signgam value into *arg_sg. */
14007 result_sg = fold_build2 (MODIFY_EXPR,
14008 TREE_TYPE (arg_sg), arg_sg,
14009 build_int_cst (TREE_TYPE (arg_sg), sg));
14010 TREE_SIDE_EFFECTS (result_sg) = 1;
14011 /* Combine the signgam assignment with the lgamma result. */
14012 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
14013 result_sg, result_lg));
14018 return result;
14021 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14022 function FUNC on it and return the resulting value as a tree with
14023 type TYPE. The mpfr precision is set to the precision of TYPE. We
14024 assume that function FUNC returns zero if the result could be
14025 calculated exactly within the requested precision. */
14027 static tree
14028 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
14030 tree result = NULL_TREE;
14032 STRIP_NOPS (arg);
14034 /* To proceed, MPFR must exactly represent the target floating point
14035 format, which only happens when the target base equals two. */
14036 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14037 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14038 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14040 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14041 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14043 if (real_isfinite (re) && real_isfinite (im))
14045 const struct real_format *const fmt =
14046 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14047 const int prec = fmt->p;
14048 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14049 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14050 int inexact;
14051 mpc_t m;
14053 mpc_init2 (m, prec);
14054 mpfr_from_real (mpc_realref (m), re, rnd);
14055 mpfr_from_real (mpc_imagref (m), im, rnd);
14056 mpfr_clear_flags ();
14057 inexact = func (m, m, crnd);
14058 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14059 mpc_clear (m);
14063 return result;
14066 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14067 mpc function FUNC on it and return the resulting value as a tree
14068 with type TYPE. The mpfr precision is set to the precision of
14069 TYPE. We assume that function FUNC returns zero if the result
14070 could be calculated exactly within the requested precision. If
14071 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14072 in the arguments and/or results. */
14074 tree
14075 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14076 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14078 tree result = NULL_TREE;
14080 STRIP_NOPS (arg0);
14081 STRIP_NOPS (arg1);
14083 /* To proceed, MPFR must exactly represent the target floating point
14084 format, which only happens when the target base equals two. */
14085 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14086 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14087 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14088 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14089 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14091 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14092 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14093 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14094 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14096 if (do_nonfinite
14097 || (real_isfinite (re0) && real_isfinite (im0)
14098 && real_isfinite (re1) && real_isfinite (im1)))
14100 const struct real_format *const fmt =
14101 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14102 const int prec = fmt->p;
14103 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14104 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14105 int inexact;
14106 mpc_t m0, m1;
14108 mpc_init2 (m0, prec);
14109 mpc_init2 (m1, prec);
14110 mpfr_from_real (mpc_realref (m0), re0, rnd);
14111 mpfr_from_real (mpc_imagref (m0), im0, rnd);
14112 mpfr_from_real (mpc_realref (m1), re1, rnd);
14113 mpfr_from_real (mpc_imagref (m1), im1, rnd);
14114 mpfr_clear_flags ();
14115 inexact = func (m0, m0, m1, crnd);
14116 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14117 mpc_clear (m0);
14118 mpc_clear (m1);
14122 return result;
14125 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14126 a normal call should be emitted rather than expanding the function
14127 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14129 static tree
14130 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14132 int nargs = gimple_call_num_args (stmt);
14134 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14135 (nargs > 0
14136 ? gimple_call_arg_ptr (stmt, 0)
14137 : &error_mark_node), fcode);
14140 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14141 a normal call should be emitted rather than expanding the function
14142 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14143 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14144 passed as second argument. */
14146 tree
14147 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14148 enum built_in_function fcode)
14150 int nargs = gimple_call_num_args (stmt);
14152 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14153 (nargs > 0
14154 ? gimple_call_arg_ptr (stmt, 0)
14155 : &error_mark_node), maxlen, fcode);
14158 /* Builtins with folding operations that operate on "..." arguments
14159 need special handling; we need to store the arguments in a convenient
14160 data structure before attempting any folding. Fortunately there are
14161 only a few builtins that fall into this category. FNDECL is the
14162 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14163 result of the function call is ignored. */
14165 static tree
14166 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14167 bool ignore ATTRIBUTE_UNUSED)
14169 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14170 tree ret = NULL_TREE;
14172 switch (fcode)
14174 case BUILT_IN_SPRINTF_CHK:
14175 case BUILT_IN_VSPRINTF_CHK:
14176 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14177 break;
14179 case BUILT_IN_SNPRINTF_CHK:
14180 case BUILT_IN_VSNPRINTF_CHK:
14181 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14183 default:
14184 break;
14186 if (ret)
14188 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14189 TREE_NO_WARNING (ret) = 1;
14190 return ret;
14192 return NULL_TREE;
14195 /* A wrapper function for builtin folding that prevents warnings for
14196 "statement without effect" and the like, caused by removing the
14197 call node earlier than the warning is generated. */
14199 tree
14200 fold_call_stmt (gimple stmt, bool ignore)
14202 tree ret = NULL_TREE;
14203 tree fndecl = gimple_call_fndecl (stmt);
14204 location_t loc = gimple_location (stmt);
14205 if (fndecl
14206 && TREE_CODE (fndecl) == FUNCTION_DECL
14207 && DECL_BUILT_IN (fndecl)
14208 && !gimple_call_va_arg_pack_p (stmt))
14210 int nargs = gimple_call_num_args (stmt);
14211 tree *args = (nargs > 0
14212 ? gimple_call_arg_ptr (stmt, 0)
14213 : &error_mark_node);
14215 if (avoid_folding_inline_builtin (fndecl))
14216 return NULL_TREE;
14217 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14219 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14221 else
14223 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14224 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14225 if (!ret)
14226 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14227 if (ret)
14229 /* Propagate location information from original call to
14230 expansion of builtin. Otherwise things like
14231 maybe_emit_chk_warning, that operate on the expansion
14232 of a builtin, will use the wrong location information. */
14233 if (gimple_has_location (stmt))
14235 tree realret = ret;
14236 if (TREE_CODE (ret) == NOP_EXPR)
14237 realret = TREE_OPERAND (ret, 0);
14238 if (CAN_HAVE_LOCATION_P (realret)
14239 && !EXPR_HAS_LOCATION (realret))
14240 SET_EXPR_LOCATION (realret, loc);
14241 return realret;
14243 return ret;
14247 return NULL_TREE;
14250 /* Look up the function in builtin_decl that corresponds to DECL
14251 and set ASMSPEC as its user assembler name. DECL must be a
14252 function decl that declares a builtin. */
14254 void
14255 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14257 tree builtin;
14258 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14259 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14260 && asmspec != 0);
14262 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14263 set_user_assembler_name (builtin, asmspec);
14264 switch (DECL_FUNCTION_CODE (decl))
14266 case BUILT_IN_MEMCPY:
14267 init_block_move_fn (asmspec);
14268 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14269 break;
14270 case BUILT_IN_MEMSET:
14271 init_block_clear_fn (asmspec);
14272 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14273 break;
14274 case BUILT_IN_MEMMOVE:
14275 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14276 break;
14277 case BUILT_IN_MEMCMP:
14278 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14279 break;
14280 case BUILT_IN_ABORT:
14281 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14282 break;
14283 case BUILT_IN_FFS:
14284 if (INT_TYPE_SIZE < BITS_PER_WORD)
14286 set_user_assembler_libfunc ("ffs", asmspec);
14287 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14288 MODE_INT, 0), "ffs");
14290 break;
14291 default:
14292 break;
14296 /* Return true if DECL is a builtin that expands to a constant or similarly
14297 simple code. */
14298 bool
14299 is_simple_builtin (tree decl)
14301 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14302 switch (DECL_FUNCTION_CODE (decl))
14304 /* Builtins that expand to constants. */
14305 case BUILT_IN_CONSTANT_P:
14306 case BUILT_IN_EXPECT:
14307 case BUILT_IN_OBJECT_SIZE:
14308 case BUILT_IN_UNREACHABLE:
14309 /* Simple register moves or loads from stack. */
14310 case BUILT_IN_ASSUME_ALIGNED:
14311 case BUILT_IN_RETURN_ADDRESS:
14312 case BUILT_IN_EXTRACT_RETURN_ADDR:
14313 case BUILT_IN_FROB_RETURN_ADDR:
14314 case BUILT_IN_RETURN:
14315 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14316 case BUILT_IN_FRAME_ADDRESS:
14317 case BUILT_IN_VA_END:
14318 case BUILT_IN_STACK_SAVE:
14319 case BUILT_IN_STACK_RESTORE:
14320 /* Exception state returns or moves registers around. */
14321 case BUILT_IN_EH_FILTER:
14322 case BUILT_IN_EH_POINTER:
14323 case BUILT_IN_EH_COPY_VALUES:
14324 return true;
14326 default:
14327 return false;
14330 return false;
14333 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14334 most probably expanded inline into reasonably simple code. This is a
14335 superset of is_simple_builtin. */
14336 bool
14337 is_inexpensive_builtin (tree decl)
14339 if (!decl)
14340 return false;
14341 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14342 return true;
14343 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14344 switch (DECL_FUNCTION_CODE (decl))
14346 case BUILT_IN_ABS:
14347 case BUILT_IN_ALLOCA:
14348 case BUILT_IN_ALLOCA_WITH_ALIGN:
14349 case BUILT_IN_BSWAP16:
14350 case BUILT_IN_BSWAP32:
14351 case BUILT_IN_BSWAP64:
14352 case BUILT_IN_CLZ:
14353 case BUILT_IN_CLZIMAX:
14354 case BUILT_IN_CLZL:
14355 case BUILT_IN_CLZLL:
14356 case BUILT_IN_CTZ:
14357 case BUILT_IN_CTZIMAX:
14358 case BUILT_IN_CTZL:
14359 case BUILT_IN_CTZLL:
14360 case BUILT_IN_FFS:
14361 case BUILT_IN_FFSIMAX:
14362 case BUILT_IN_FFSL:
14363 case BUILT_IN_FFSLL:
14364 case BUILT_IN_IMAXABS:
14365 case BUILT_IN_FINITE:
14366 case BUILT_IN_FINITEF:
14367 case BUILT_IN_FINITEL:
14368 case BUILT_IN_FINITED32:
14369 case BUILT_IN_FINITED64:
14370 case BUILT_IN_FINITED128:
14371 case BUILT_IN_FPCLASSIFY:
14372 case BUILT_IN_ISFINITE:
14373 case BUILT_IN_ISINF_SIGN:
14374 case BUILT_IN_ISINF:
14375 case BUILT_IN_ISINFF:
14376 case BUILT_IN_ISINFL:
14377 case BUILT_IN_ISINFD32:
14378 case BUILT_IN_ISINFD64:
14379 case BUILT_IN_ISINFD128:
14380 case BUILT_IN_ISNAN:
14381 case BUILT_IN_ISNANF:
14382 case BUILT_IN_ISNANL:
14383 case BUILT_IN_ISNAND32:
14384 case BUILT_IN_ISNAND64:
14385 case BUILT_IN_ISNAND128:
14386 case BUILT_IN_ISNORMAL:
14387 case BUILT_IN_ISGREATER:
14388 case BUILT_IN_ISGREATEREQUAL:
14389 case BUILT_IN_ISLESS:
14390 case BUILT_IN_ISLESSEQUAL:
14391 case BUILT_IN_ISLESSGREATER:
14392 case BUILT_IN_ISUNORDERED:
14393 case BUILT_IN_VA_ARG_PACK:
14394 case BUILT_IN_VA_ARG_PACK_LEN:
14395 case BUILT_IN_VA_COPY:
14396 case BUILT_IN_TRAP:
14397 case BUILT_IN_SAVEREGS:
14398 case BUILT_IN_POPCOUNTL:
14399 case BUILT_IN_POPCOUNTLL:
14400 case BUILT_IN_POPCOUNTIMAX:
14401 case BUILT_IN_POPCOUNT:
14402 case BUILT_IN_PARITYL:
14403 case BUILT_IN_PARITYLL:
14404 case BUILT_IN_PARITYIMAX:
14405 case BUILT_IN_PARITY:
14406 case BUILT_IN_LABS:
14407 case BUILT_IN_LLABS:
14408 case BUILT_IN_PREFETCH:
14409 return true;
14411 default:
14412 return is_simple_builtin (decl);
14415 return false;