* gcc.dg/c11-complex-1.c: Use dg-add-options ieee.
[official-gcc.git] / gcc / builtins.c
blob7dc980f9bf5368ca344d9aeb3edb24ff9eb0e861
1 /* Expand builtin functions.
2 Copyright (C) 1988-2013 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 "realmpfr.h"
28 #include "gimple.h"
29 #include "flags.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "except.h"
33 #include "function.h"
34 #include "insn-config.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "libfuncs.h"
38 #include "recog.h"
39 #include "output.h"
40 #include "typeclass.h"
41 #include "predict.h"
42 #include "tm_p.h"
43 #include "target.h"
44 #include "langhooks.h"
45 #include "basic-block.h"
46 #include "tree-ssanames.h"
47 #include "tree-dfa.h"
48 #include "value-prof.h"
49 #include "diagnostic-core.h"
50 #include "builtins.h"
51 #include "ubsan.h"
52 #include "cilk.h"
55 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
57 struct target_builtins default_target_builtins;
58 #if SWITCHABLE_TARGET
59 struct target_builtins *this_target_builtins = &default_target_builtins;
60 #endif
62 /* Define the names of the builtin function types and codes. */
63 const char *const built_in_class_names[BUILT_IN_LAST]
64 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
66 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
67 const char * built_in_names[(int) END_BUILTINS] =
69 #include "builtins.def"
71 #undef DEF_BUILTIN
73 /* Setup an array of _DECL trees, make sure each element is
74 initialized to NULL_TREE. */
75 builtin_info_type builtin_info;
77 /* Non-zero if __builtin_constant_p should be folded right away. */
78 bool force_folding_builtin_constant_p;
80 static const char *c_getstr (tree);
81 static rtx c_readstr (const char *, enum machine_mode);
82 static int target_char_cast (tree, char *);
83 static rtx get_memory_rtx (tree, tree);
84 static int apply_args_size (void);
85 static int apply_result_size (void);
86 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
87 static rtx result_vector (int, rtx);
88 #endif
89 static void expand_builtin_update_setjmp_buf (rtx);
90 static void expand_builtin_prefetch (tree);
91 static rtx expand_builtin_apply_args (void);
92 static rtx expand_builtin_apply_args_1 (void);
93 static rtx expand_builtin_apply (rtx, rtx, rtx);
94 static void expand_builtin_return (rtx);
95 static enum type_class type_to_class (tree);
96 static rtx expand_builtin_classify_type (tree);
97 static void expand_errno_check (tree, rtx);
98 static rtx expand_builtin_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
101 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
102 static rtx expand_builtin_interclass_mathfn (tree, rtx);
103 static rtx expand_builtin_sincos (tree);
104 static rtx expand_builtin_cexpi (tree, rtx);
105 static rtx expand_builtin_int_roundingfn (tree, rtx);
106 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
107 static rtx expand_builtin_next_arg (void);
108 static rtx expand_builtin_va_start (tree);
109 static rtx expand_builtin_va_end (tree);
110 static rtx expand_builtin_va_copy (tree);
111 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strcmp (tree, rtx);
113 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
114 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
115 static rtx expand_builtin_memcpy (tree, rtx);
116 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
117 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
118 enum machine_mode, int);
119 static rtx expand_builtin_strcpy (tree, rtx);
120 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
121 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
122 static rtx expand_builtin_strncpy (tree, rtx);
123 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
126 static rtx expand_builtin_bzero (tree);
127 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
128 static rtx expand_builtin_alloca (tree, bool);
129 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
130 static rtx expand_builtin_frame_address (tree, tree);
131 static tree stabilize_va_list_loc (location_t, tree, int);
132 static rtx expand_builtin_expect (tree, rtx);
133 static tree fold_builtin_constant_p (tree);
134 static tree fold_builtin_expect (location_t, tree, tree);
135 static tree fold_builtin_classify_type (tree);
136 static tree fold_builtin_strlen (location_t, tree, tree);
137 static tree fold_builtin_inf (location_t, tree, int);
138 static tree fold_builtin_nan (tree, tree, int);
139 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
140 static bool validate_arg (const_tree, enum tree_code code);
141 static bool integer_valued_real_p (tree);
142 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
143 static bool readonly_data_expr (tree);
144 static rtx expand_builtin_fabs (tree, rtx, rtx);
145 static rtx expand_builtin_signbit (tree, rtx);
146 static tree fold_builtin_sqrt (location_t, tree, tree);
147 static tree fold_builtin_cbrt (location_t, tree, tree);
148 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
149 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
150 static tree fold_builtin_cos (location_t, tree, tree, tree);
151 static tree fold_builtin_cosh (location_t, tree, tree, tree);
152 static tree fold_builtin_tan (tree, tree);
153 static tree fold_builtin_trunc (location_t, tree, tree);
154 static tree fold_builtin_floor (location_t, tree, tree);
155 static tree fold_builtin_ceil (location_t, tree, tree);
156 static tree fold_builtin_round (location_t, tree, tree);
157 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
158 static tree fold_builtin_bitop (tree, tree);
159 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
160 static tree fold_builtin_strchr (location_t, tree, tree, tree);
161 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
162 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
163 static tree fold_builtin_strcmp (location_t, tree, tree);
164 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
165 static tree fold_builtin_signbit (location_t, tree, tree);
166 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
167 static tree fold_builtin_isascii (location_t, tree);
168 static tree fold_builtin_toascii (location_t, tree);
169 static tree fold_builtin_isdigit (location_t, tree);
170 static tree fold_builtin_fabs (location_t, tree, tree);
171 static tree fold_builtin_abs (location_t, tree, tree);
172 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
173 enum tree_code);
174 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
175 static tree fold_builtin_0 (location_t, tree, bool);
176 static tree fold_builtin_1 (location_t, tree, tree, bool);
177 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
178 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
179 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
180 static tree fold_builtin_varargs (location_t, tree, tree, bool);
182 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
183 static tree fold_builtin_strstr (location_t, tree, tree, tree);
184 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
185 static tree fold_builtin_strcat (location_t, tree, tree);
186 static tree fold_builtin_strncat (location_t, tree, tree, tree);
187 static tree fold_builtin_strspn (location_t, tree, tree);
188 static tree fold_builtin_strcspn (location_t, tree, tree);
189 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
190 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
192 static rtx expand_builtin_object_size (tree);
193 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
194 enum built_in_function);
195 static void maybe_emit_chk_warning (tree, enum built_in_function);
196 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
197 static void maybe_emit_free_warning (tree);
198 static tree fold_builtin_object_size (tree, tree);
199 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
200 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
201 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
202 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
203 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
204 enum built_in_function);
205 static bool init_target_chars (void);
207 static unsigned HOST_WIDE_INT target_newline;
208 static unsigned HOST_WIDE_INT target_percent;
209 static unsigned HOST_WIDE_INT target_c;
210 static unsigned HOST_WIDE_INT target_s;
211 static char target_percent_c[3];
212 static char target_percent_s[3];
213 static char target_percent_s_newline[4];
214 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
215 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
216 static tree do_mpfr_arg2 (tree, tree, tree,
217 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
218 static tree do_mpfr_arg3 (tree, tree, tree, tree,
219 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
220 static tree do_mpfr_sincos (tree, tree, tree);
221 static tree do_mpfr_bessel_n (tree, tree, tree,
222 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
223 const REAL_VALUE_TYPE *, bool);
224 static tree do_mpfr_remquo (tree, tree, tree);
225 static tree do_mpfr_lgamma_r (tree, tree, tree);
226 static void expand_builtin_sync_synchronize (void);
228 /* Return true if NAME starts with __builtin_ or __sync_. */
230 static bool
231 is_builtin_name (const char *name)
233 if (strncmp (name, "__builtin_", 10) == 0)
234 return true;
235 if (strncmp (name, "__sync_", 7) == 0)
236 return true;
237 if (strncmp (name, "__atomic_", 9) == 0)
238 return true;
239 if (flag_enable_cilkplus
240 && (!strcmp (name, "__cilkrts_detach")
241 || !strcmp (name, "__cilkrts_pop_frame")))
242 return true;
243 return false;
247 /* Return true if DECL is a function symbol representing a built-in. */
249 bool
250 is_builtin_fn (tree decl)
252 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
255 /* By default we assume that c99 functions are present at the runtime,
256 but sincos is not. */
257 bool
258 default_libc_has_function (enum function_class fn_class)
260 if (fn_class == function_c94
261 || fn_class == function_c99_misc
262 || fn_class == function_c99_math_complex)
263 return true;
265 return false;
268 bool
269 gnu_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED)
271 return true;
274 bool
275 no_c99_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED)
277 return false;
280 /* Return true if NODE should be considered for inline expansion regardless
281 of the optimization level. This means whenever a function is invoked with
282 its "internal" name, which normally contains the prefix "__builtin". */
284 static bool
285 called_as_built_in (tree node)
287 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
288 we want the name used to call the function, not the name it
289 will have. */
290 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
291 return is_builtin_name (name);
294 /* Compute values M and N such that M divides (address of EXP - N) and such
295 that N < M. If these numbers can be determined, store M in alignp and N in
296 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
297 *alignp and any bit-offset to *bitposp.
299 Note that the address (and thus the alignment) computed here is based
300 on the address to which a symbol resolves, whereas DECL_ALIGN is based
301 on the address at which an object is actually located. These two
302 addresses are not always the same. For example, on ARM targets,
303 the address &foo of a Thumb function foo() has the lowest bit set,
304 whereas foo() itself starts on an even address.
306 If ADDR_P is true we are taking the address of the memory reference EXP
307 and thus cannot rely on the access taking place. */
309 static bool
310 get_object_alignment_2 (tree exp, unsigned int *alignp,
311 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
313 HOST_WIDE_INT bitsize, bitpos;
314 tree offset;
315 enum machine_mode mode;
316 int unsignedp, volatilep;
317 unsigned int align = BITS_PER_UNIT;
318 bool known_alignment = false;
320 /* Get the innermost object and the constant (bitpos) and possibly
321 variable (offset) offset of the access. */
322 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
323 &mode, &unsignedp, &volatilep, true);
325 /* Extract alignment information from the innermost object and
326 possibly adjust bitpos and offset. */
327 if (TREE_CODE (exp) == FUNCTION_DECL)
329 /* Function addresses can encode extra information besides their
330 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
331 allows the low bit to be used as a virtual bit, we know
332 that the address itself must be at least 2-byte aligned. */
333 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
334 align = 2 * BITS_PER_UNIT;
336 else if (TREE_CODE (exp) == LABEL_DECL)
338 else if (TREE_CODE (exp) == CONST_DECL)
340 /* The alignment of a CONST_DECL is determined by its initializer. */
341 exp = DECL_INITIAL (exp);
342 align = TYPE_ALIGN (TREE_TYPE (exp));
343 #ifdef CONSTANT_ALIGNMENT
344 if (CONSTANT_CLASS_P (exp))
345 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
346 #endif
347 known_alignment = true;
349 else if (DECL_P (exp))
351 align = DECL_ALIGN (exp);
352 known_alignment = true;
354 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
356 align = TYPE_ALIGN (TREE_TYPE (exp));
358 else if (TREE_CODE (exp) == INDIRECT_REF
359 || TREE_CODE (exp) == MEM_REF
360 || TREE_CODE (exp) == TARGET_MEM_REF)
362 tree addr = TREE_OPERAND (exp, 0);
363 unsigned ptr_align;
364 unsigned HOST_WIDE_INT ptr_bitpos;
366 if (TREE_CODE (addr) == BIT_AND_EXPR
367 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
369 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
370 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
371 align *= BITS_PER_UNIT;
372 addr = TREE_OPERAND (addr, 0);
375 known_alignment
376 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
377 align = MAX (ptr_align, align);
379 /* The alignment of the pointer operand in a TARGET_MEM_REF
380 has to take the variable offset parts into account. */
381 if (TREE_CODE (exp) == TARGET_MEM_REF)
383 if (TMR_INDEX (exp))
385 unsigned HOST_WIDE_INT step = 1;
386 if (TMR_STEP (exp))
387 step = TREE_INT_CST_LOW (TMR_STEP (exp));
388 align = MIN (align, (step & -step) * BITS_PER_UNIT);
390 if (TMR_INDEX2 (exp))
391 align = BITS_PER_UNIT;
392 known_alignment = false;
395 /* When EXP is an actual memory reference then we can use
396 TYPE_ALIGN of a pointer indirection to derive alignment.
397 Do so only if get_pointer_alignment_1 did not reveal absolute
398 alignment knowledge and if using that alignment would
399 improve the situation. */
400 if (!addr_p && !known_alignment
401 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
402 align = TYPE_ALIGN (TREE_TYPE (exp));
403 else
405 /* Else adjust bitpos accordingly. */
406 bitpos += ptr_bitpos;
407 if (TREE_CODE (exp) == MEM_REF
408 || TREE_CODE (exp) == TARGET_MEM_REF)
409 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
412 else if (TREE_CODE (exp) == STRING_CST)
414 /* STRING_CST are the only constant objects we allow to be not
415 wrapped inside a CONST_DECL. */
416 align = TYPE_ALIGN (TREE_TYPE (exp));
417 #ifdef CONSTANT_ALIGNMENT
418 if (CONSTANT_CLASS_P (exp))
419 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
420 #endif
421 known_alignment = true;
424 /* If there is a non-constant offset part extract the maximum
425 alignment that can prevail. */
426 if (offset)
428 int trailing_zeros = tree_ctz (offset);
429 if (trailing_zeros < HOST_BITS_PER_INT)
431 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
432 if (inner)
433 align = MIN (align, inner);
437 *alignp = align;
438 *bitposp = bitpos & (*alignp - 1);
439 return known_alignment;
442 /* For a memory reference expression EXP compute values M and N such that M
443 divides (&EXP - N) and such that N < M. If these numbers can be determined,
444 store M in alignp and N in *BITPOSP and return true. Otherwise return false
445 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
447 bool
448 get_object_alignment_1 (tree exp, unsigned int *alignp,
449 unsigned HOST_WIDE_INT *bitposp)
451 return get_object_alignment_2 (exp, alignp, bitposp, false);
454 /* Return the alignment in bits of EXP, an object. */
456 unsigned int
457 get_object_alignment (tree exp)
459 unsigned HOST_WIDE_INT bitpos = 0;
460 unsigned int align;
462 get_object_alignment_1 (exp, &align, &bitpos);
464 /* align and bitpos now specify known low bits of the pointer.
465 ptr & (align - 1) == bitpos. */
467 if (bitpos != 0)
468 align = (bitpos & -bitpos);
469 return align;
472 /* For a pointer valued expression EXP compute values M and N such that M
473 divides (EXP - N) and such that N < M. If these numbers can be determined,
474 store M in alignp and N in *BITPOSP and return true. Return false if
475 the results are just a conservative approximation.
477 If EXP is not a pointer, false is returned too. */
479 bool
480 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
481 unsigned HOST_WIDE_INT *bitposp)
483 STRIP_NOPS (exp);
485 if (TREE_CODE (exp) == ADDR_EXPR)
486 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
487 alignp, bitposp, true);
488 else if (TREE_CODE (exp) == SSA_NAME
489 && POINTER_TYPE_P (TREE_TYPE (exp)))
491 unsigned int ptr_align, ptr_misalign;
492 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
494 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
496 *bitposp = ptr_misalign * BITS_PER_UNIT;
497 *alignp = ptr_align * BITS_PER_UNIT;
498 /* We cannot really tell whether this result is an approximation. */
499 return true;
501 else
503 *bitposp = 0;
504 *alignp = BITS_PER_UNIT;
505 return false;
508 else if (TREE_CODE (exp) == INTEGER_CST)
510 *alignp = BIGGEST_ALIGNMENT;
511 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
512 & (BIGGEST_ALIGNMENT - 1));
513 return true;
516 *bitposp = 0;
517 *alignp = BITS_PER_UNIT;
518 return false;
521 /* Return the alignment in bits of EXP, a pointer valued expression.
522 The alignment returned is, by default, the alignment of the thing that
523 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
525 Otherwise, look at the expression to see if we can do better, i.e., if the
526 expression is actually pointing at an object whose alignment is tighter. */
528 unsigned int
529 get_pointer_alignment (tree exp)
531 unsigned HOST_WIDE_INT bitpos = 0;
532 unsigned int align;
534 get_pointer_alignment_1 (exp, &align, &bitpos);
536 /* align and bitpos now specify known low bits of the pointer.
537 ptr & (align - 1) == bitpos. */
539 if (bitpos != 0)
540 align = (bitpos & -bitpos);
542 return align;
545 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
546 way, because it could contain a zero byte in the middle.
547 TREE_STRING_LENGTH is the size of the character array, not the string.
549 ONLY_VALUE should be nonzero if the result is not going to be emitted
550 into the instruction stream and zero if it is going to be expanded.
551 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
552 is returned, otherwise NULL, since
553 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
554 evaluate the side-effects.
556 The value returned is of type `ssizetype'.
558 Unfortunately, string_constant can't access the values of const char
559 arrays with initializers, so neither can we do so here. */
561 tree
562 c_strlen (tree src, int only_value)
564 tree offset_node;
565 HOST_WIDE_INT offset;
566 int max;
567 const char *ptr;
568 location_t loc;
570 STRIP_NOPS (src);
571 if (TREE_CODE (src) == COND_EXPR
572 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
574 tree len1, len2;
576 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
577 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
578 if (tree_int_cst_equal (len1, len2))
579 return len1;
582 if (TREE_CODE (src) == COMPOUND_EXPR
583 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
584 return c_strlen (TREE_OPERAND (src, 1), only_value);
586 loc = EXPR_LOC_OR_HERE (src);
588 src = string_constant (src, &offset_node);
589 if (src == 0)
590 return NULL_TREE;
592 max = TREE_STRING_LENGTH (src) - 1;
593 ptr = TREE_STRING_POINTER (src);
595 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
597 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
598 compute the offset to the following null if we don't know where to
599 start searching for it. */
600 int i;
602 for (i = 0; i < max; i++)
603 if (ptr[i] == 0)
604 return NULL_TREE;
606 /* We don't know the starting offset, but we do know that the string
607 has no internal zero bytes. We can assume that the offset falls
608 within the bounds of the string; otherwise, the programmer deserves
609 what he gets. Subtract the offset from the length of the string,
610 and return that. This would perhaps not be valid if we were dealing
611 with named arrays in addition to literal string constants. */
613 return size_diffop_loc (loc, size_int (max), offset_node);
616 /* We have a known offset into the string. Start searching there for
617 a null character if we can represent it as a single HOST_WIDE_INT. */
618 if (offset_node == 0)
619 offset = 0;
620 else if (! tree_fits_shwi_p (offset_node))
621 offset = -1;
622 else
623 offset = tree_to_shwi (offset_node);
625 /* If the offset is known to be out of bounds, warn, and call strlen at
626 runtime. */
627 if (offset < 0 || offset > max)
629 /* Suppress multiple warnings for propagated constant strings. */
630 if (! TREE_NO_WARNING (src))
632 warning_at (loc, 0, "offset outside bounds of constant string");
633 TREE_NO_WARNING (src) = 1;
635 return NULL_TREE;
638 /* Use strlen to search for the first zero byte. Since any strings
639 constructed with build_string will have nulls appended, we win even
640 if we get handed something like (char[4])"abcd".
642 Since OFFSET is our starting index into the string, no further
643 calculation is needed. */
644 return ssize_int (strlen (ptr + offset));
647 /* Return a char pointer for a C string if it is a string constant
648 or sum of string constant and integer constant. */
650 static const char *
651 c_getstr (tree src)
653 tree offset_node;
655 src = string_constant (src, &offset_node);
656 if (src == 0)
657 return 0;
659 if (offset_node == 0)
660 return TREE_STRING_POINTER (src);
661 else if (!tree_fits_uhwi_p (offset_node)
662 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
663 return 0;
665 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
668 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
669 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
671 static rtx
672 c_readstr (const char *str, enum machine_mode mode)
674 HOST_WIDE_INT c[2];
675 HOST_WIDE_INT ch;
676 unsigned int i, j;
678 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
680 c[0] = 0;
681 c[1] = 0;
682 ch = 1;
683 for (i = 0; i < GET_MODE_SIZE (mode); i++)
685 j = i;
686 if (WORDS_BIG_ENDIAN)
687 j = GET_MODE_SIZE (mode) - i - 1;
688 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
689 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
690 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
691 j *= BITS_PER_UNIT;
692 gcc_assert (j < HOST_BITS_PER_DOUBLE_INT);
694 if (ch)
695 ch = (unsigned char) str[i];
696 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
698 return immed_double_const (c[0], c[1], mode);
701 /* Cast a target constant CST to target CHAR and if that value fits into
702 host char type, return zero and put that value into variable pointed to by
703 P. */
705 static int
706 target_char_cast (tree cst, char *p)
708 unsigned HOST_WIDE_INT val, hostval;
710 if (TREE_CODE (cst) != INTEGER_CST
711 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
712 return 1;
714 val = TREE_INT_CST_LOW (cst);
715 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
716 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
718 hostval = val;
719 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
720 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
722 if (val != hostval)
723 return 1;
725 *p = hostval;
726 return 0;
729 /* Similar to save_expr, but assumes that arbitrary code is not executed
730 in between the multiple evaluations. In particular, we assume that a
731 non-addressable local variable will not be modified. */
733 static tree
734 builtin_save_expr (tree exp)
736 if (TREE_CODE (exp) == SSA_NAME
737 || (TREE_ADDRESSABLE (exp) == 0
738 && (TREE_CODE (exp) == PARM_DECL
739 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
740 return exp;
742 return save_expr (exp);
745 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
746 times to get the address of either a higher stack frame, or a return
747 address located within it (depending on FNDECL_CODE). */
749 static rtx
750 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
752 int i;
754 #ifdef INITIAL_FRAME_ADDRESS_RTX
755 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
756 #else
757 rtx tem;
759 /* For a zero count with __builtin_return_address, we don't care what
760 frame address we return, because target-specific definitions will
761 override us. Therefore frame pointer elimination is OK, and using
762 the soft frame pointer is OK.
764 For a nonzero count, or a zero count with __builtin_frame_address,
765 we require a stable offset from the current frame pointer to the
766 previous one, so we must use the hard frame pointer, and
767 we must disable frame pointer elimination. */
768 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
769 tem = frame_pointer_rtx;
770 else
772 tem = hard_frame_pointer_rtx;
774 /* Tell reload not to eliminate the frame pointer. */
775 crtl->accesses_prior_frames = 1;
777 #endif
779 /* Some machines need special handling before we can access
780 arbitrary frames. For example, on the SPARC, we must first flush
781 all register windows to the stack. */
782 #ifdef SETUP_FRAME_ADDRESSES
783 if (count > 0)
784 SETUP_FRAME_ADDRESSES ();
785 #endif
787 /* On the SPARC, the return address is not in the frame, it is in a
788 register. There is no way to access it off of the current frame
789 pointer, but it can be accessed off the previous frame pointer by
790 reading the value from the register window save area. */
791 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
792 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
793 count--;
794 #endif
796 /* Scan back COUNT frames to the specified frame. */
797 for (i = 0; i < count; i++)
799 /* Assume the dynamic chain pointer is in the word that the
800 frame address points to, unless otherwise specified. */
801 #ifdef DYNAMIC_CHAIN_ADDRESS
802 tem = DYNAMIC_CHAIN_ADDRESS (tem);
803 #endif
804 tem = memory_address (Pmode, tem);
805 tem = gen_frame_mem (Pmode, tem);
806 tem = copy_to_reg (tem);
809 /* For __builtin_frame_address, return what we've got. But, on
810 the SPARC for example, we may have to add a bias. */
811 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
812 #ifdef FRAME_ADDR_RTX
813 return FRAME_ADDR_RTX (tem);
814 #else
815 return tem;
816 #endif
818 /* For __builtin_return_address, get the return address from that frame. */
819 #ifdef RETURN_ADDR_RTX
820 tem = RETURN_ADDR_RTX (count, tem);
821 #else
822 tem = memory_address (Pmode,
823 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
824 tem = gen_frame_mem (Pmode, tem);
825 #endif
826 return tem;
829 /* Alias set used for setjmp buffer. */
830 static alias_set_type setjmp_alias_set = -1;
832 /* Construct the leading half of a __builtin_setjmp call. Control will
833 return to RECEIVER_LABEL. This is also called directly by the SJLJ
834 exception handling code. */
836 void
837 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
839 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
840 rtx stack_save;
841 rtx mem;
843 if (setjmp_alias_set == -1)
844 setjmp_alias_set = new_alias_set ();
846 buf_addr = convert_memory_address (Pmode, buf_addr);
848 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
850 /* We store the frame pointer and the address of receiver_label in
851 the buffer and use the rest of it for the stack save area, which
852 is machine-dependent. */
854 mem = gen_rtx_MEM (Pmode, buf_addr);
855 set_mem_alias_set (mem, setjmp_alias_set);
856 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
858 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
859 GET_MODE_SIZE (Pmode))),
860 set_mem_alias_set (mem, setjmp_alias_set);
862 emit_move_insn (validize_mem (mem),
863 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
865 stack_save = gen_rtx_MEM (sa_mode,
866 plus_constant (Pmode, buf_addr,
867 2 * GET_MODE_SIZE (Pmode)));
868 set_mem_alias_set (stack_save, setjmp_alias_set);
869 emit_stack_save (SAVE_NONLOCAL, &stack_save);
871 /* If there is further processing to do, do it. */
872 #ifdef HAVE_builtin_setjmp_setup
873 if (HAVE_builtin_setjmp_setup)
874 emit_insn (gen_builtin_setjmp_setup (buf_addr));
875 #endif
877 /* We have a nonlocal label. */
878 cfun->has_nonlocal_label = 1;
881 /* Construct the trailing part of a __builtin_setjmp call. This is
882 also called directly by the SJLJ exception handling code.
883 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
885 void
886 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
888 rtx chain;
890 /* Mark the FP as used when we get here, so we have to make sure it's
891 marked as used by this function. */
892 emit_use (hard_frame_pointer_rtx);
894 /* Mark the static chain as clobbered here so life information
895 doesn't get messed up for it. */
896 chain = targetm.calls.static_chain (current_function_decl, true);
897 if (chain && REG_P (chain))
898 emit_clobber (chain);
900 /* Now put in the code to restore the frame pointer, and argument
901 pointer, if needed. */
902 #ifdef HAVE_nonlocal_goto
903 if (! HAVE_nonlocal_goto)
904 #endif
905 /* First adjust our frame pointer to its actual value. It was
906 previously set to the start of the virtual area corresponding to
907 the stacked variables when we branched here and now needs to be
908 adjusted to the actual hardware fp value.
910 Assignments to virtual registers are converted by
911 instantiate_virtual_regs into the corresponding assignment
912 to the underlying register (fp in this case) that makes
913 the original assignment true.
914 So the following insn will actually be decrementing fp by
915 STARTING_FRAME_OFFSET. */
916 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
918 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
919 if (fixed_regs[ARG_POINTER_REGNUM])
921 #ifdef ELIMINABLE_REGS
922 /* If the argument pointer can be eliminated in favor of the
923 frame pointer, we don't need to restore it. We assume here
924 that if such an elimination is present, it can always be used.
925 This is the case on all known machines; if we don't make this
926 assumption, we do unnecessary saving on many machines. */
927 size_t i;
928 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
930 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
931 if (elim_regs[i].from == ARG_POINTER_REGNUM
932 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
933 break;
935 if (i == ARRAY_SIZE (elim_regs))
936 #endif
938 /* Now restore our arg pointer from the address at which it
939 was saved in our stack frame. */
940 emit_move_insn (crtl->args.internal_arg_pointer,
941 copy_to_reg (get_arg_pointer_save_area ()));
944 #endif
946 #ifdef HAVE_builtin_setjmp_receiver
947 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
948 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
949 else
950 #endif
951 #ifdef HAVE_nonlocal_goto_receiver
952 if (HAVE_nonlocal_goto_receiver)
953 emit_insn (gen_nonlocal_goto_receiver ());
954 else
955 #endif
956 { /* Nothing */ }
958 /* We must not allow the code we just generated to be reordered by
959 scheduling. Specifically, the update of the frame pointer must
960 happen immediately, not later. Similarly, we must block
961 (frame-related) register values to be used across this code. */
962 emit_insn (gen_blockage ());
965 /* __builtin_longjmp is passed a pointer to an array of five words (not
966 all will be used on all machines). It operates similarly to the C
967 library function of the same name, but is more efficient. Much of
968 the code below is copied from the handling of non-local gotos. */
970 static void
971 expand_builtin_longjmp (rtx buf_addr, rtx value)
973 rtx fp, lab, stack, insn, last;
974 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
976 /* DRAP is needed for stack realign if longjmp is expanded to current
977 function */
978 if (SUPPORTS_STACK_ALIGNMENT)
979 crtl->need_drap = true;
981 if (setjmp_alias_set == -1)
982 setjmp_alias_set = new_alias_set ();
984 buf_addr = convert_memory_address (Pmode, buf_addr);
986 buf_addr = force_reg (Pmode, buf_addr);
988 /* We require that the user must pass a second argument of 1, because
989 that is what builtin_setjmp will return. */
990 gcc_assert (value == const1_rtx);
992 last = get_last_insn ();
993 #ifdef HAVE_builtin_longjmp
994 if (HAVE_builtin_longjmp)
995 emit_insn (gen_builtin_longjmp (buf_addr));
996 else
997 #endif
999 fp = gen_rtx_MEM (Pmode, buf_addr);
1000 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1001 GET_MODE_SIZE (Pmode)));
1003 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1004 2 * GET_MODE_SIZE (Pmode)));
1005 set_mem_alias_set (fp, setjmp_alias_set);
1006 set_mem_alias_set (lab, setjmp_alias_set);
1007 set_mem_alias_set (stack, setjmp_alias_set);
1009 /* Pick up FP, label, and SP from the block and jump. This code is
1010 from expand_goto in stmt.c; see there for detailed comments. */
1011 #ifdef HAVE_nonlocal_goto
1012 if (HAVE_nonlocal_goto)
1013 /* We have to pass a value to the nonlocal_goto pattern that will
1014 get copied into the static_chain pointer, but it does not matter
1015 what that value is, because builtin_setjmp does not use it. */
1016 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1017 else
1018 #endif
1020 lab = copy_to_reg (lab);
1022 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1023 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1025 emit_move_insn (hard_frame_pointer_rtx, fp);
1026 emit_stack_restore (SAVE_NONLOCAL, stack);
1028 emit_use (hard_frame_pointer_rtx);
1029 emit_use (stack_pointer_rtx);
1030 emit_indirect_jump (lab);
1034 /* Search backwards and mark the jump insn as a non-local goto.
1035 Note that this precludes the use of __builtin_longjmp to a
1036 __builtin_setjmp target in the same function. However, we've
1037 already cautioned the user that these functions are for
1038 internal exception handling use only. */
1039 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1041 gcc_assert (insn != last);
1043 if (JUMP_P (insn))
1045 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1046 break;
1048 else if (CALL_P (insn))
1049 break;
1053 static inline bool
1054 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1056 return (iter->i < iter->n);
1059 /* This function validates the types of a function call argument list
1060 against a specified list of tree_codes. If the last specifier is a 0,
1061 that represents an ellipses, otherwise the last specifier must be a
1062 VOID_TYPE. */
1064 static bool
1065 validate_arglist (const_tree callexpr, ...)
1067 enum tree_code code;
1068 bool res = 0;
1069 va_list ap;
1070 const_call_expr_arg_iterator iter;
1071 const_tree arg;
1073 va_start (ap, callexpr);
1074 init_const_call_expr_arg_iterator (callexpr, &iter);
1078 code = (enum tree_code) va_arg (ap, int);
1079 switch (code)
1081 case 0:
1082 /* This signifies an ellipses, any further arguments are all ok. */
1083 res = true;
1084 goto end;
1085 case VOID_TYPE:
1086 /* This signifies an endlink, if no arguments remain, return
1087 true, otherwise return false. */
1088 res = !more_const_call_expr_args_p (&iter);
1089 goto end;
1090 default:
1091 /* If no parameters remain or the parameter's code does not
1092 match the specified code, return false. Otherwise continue
1093 checking any remaining arguments. */
1094 arg = next_const_call_expr_arg (&iter);
1095 if (!validate_arg (arg, code))
1096 goto end;
1097 break;
1100 while (1);
1102 /* We need gotos here since we can only have one VA_CLOSE in a
1103 function. */
1104 end: ;
1105 va_end (ap);
1107 return res;
1110 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1111 and the address of the save area. */
1113 static rtx
1114 expand_builtin_nonlocal_goto (tree exp)
1116 tree t_label, t_save_area;
1117 rtx r_label, r_save_area, r_fp, r_sp, insn;
1119 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1120 return NULL_RTX;
1122 t_label = CALL_EXPR_ARG (exp, 0);
1123 t_save_area = CALL_EXPR_ARG (exp, 1);
1125 r_label = expand_normal (t_label);
1126 r_label = convert_memory_address (Pmode, r_label);
1127 r_save_area = expand_normal (t_save_area);
1128 r_save_area = convert_memory_address (Pmode, r_save_area);
1129 /* Copy the address of the save location to a register just in case it was
1130 based on the frame pointer. */
1131 r_save_area = copy_to_reg (r_save_area);
1132 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1133 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1134 plus_constant (Pmode, r_save_area,
1135 GET_MODE_SIZE (Pmode)));
1137 crtl->has_nonlocal_goto = 1;
1139 #ifdef HAVE_nonlocal_goto
1140 /* ??? We no longer need to pass the static chain value, afaik. */
1141 if (HAVE_nonlocal_goto)
1142 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1143 else
1144 #endif
1146 r_label = copy_to_reg (r_label);
1148 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1149 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1151 /* Restore frame pointer for containing function. */
1152 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1153 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1155 /* USE of hard_frame_pointer_rtx added for consistency;
1156 not clear if really needed. */
1157 emit_use (hard_frame_pointer_rtx);
1158 emit_use (stack_pointer_rtx);
1160 /* If the architecture is using a GP register, we must
1161 conservatively assume that the target function makes use of it.
1162 The prologue of functions with nonlocal gotos must therefore
1163 initialize the GP register to the appropriate value, and we
1164 must then make sure that this value is live at the point
1165 of the jump. (Note that this doesn't necessarily apply
1166 to targets with a nonlocal_goto pattern; they are free
1167 to implement it in their own way. Note also that this is
1168 a no-op if the GP register is a global invariant.) */
1169 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1170 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1171 emit_use (pic_offset_table_rtx);
1173 emit_indirect_jump (r_label);
1176 /* Search backwards to the jump insn and mark it as a
1177 non-local goto. */
1178 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1180 if (JUMP_P (insn))
1182 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1183 break;
1185 else if (CALL_P (insn))
1186 break;
1189 return const0_rtx;
1192 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1193 (not all will be used on all machines) that was passed to __builtin_setjmp.
1194 It updates the stack pointer in that block to correspond to the current
1195 stack pointer. */
1197 static void
1198 expand_builtin_update_setjmp_buf (rtx buf_addr)
1200 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1201 rtx stack_save
1202 = gen_rtx_MEM (sa_mode,
1203 memory_address
1204 (sa_mode,
1205 plus_constant (Pmode, buf_addr,
1206 2 * GET_MODE_SIZE (Pmode))));
1208 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1211 /* Expand a call to __builtin_prefetch. For a target that does not support
1212 data prefetch, evaluate the memory address argument in case it has side
1213 effects. */
1215 static void
1216 expand_builtin_prefetch (tree exp)
1218 tree arg0, arg1, arg2;
1219 int nargs;
1220 rtx op0, op1, op2;
1222 if (!validate_arglist (exp, POINTER_TYPE, 0))
1223 return;
1225 arg0 = CALL_EXPR_ARG (exp, 0);
1227 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1228 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1229 locality). */
1230 nargs = call_expr_nargs (exp);
1231 if (nargs > 1)
1232 arg1 = CALL_EXPR_ARG (exp, 1);
1233 else
1234 arg1 = integer_zero_node;
1235 if (nargs > 2)
1236 arg2 = CALL_EXPR_ARG (exp, 2);
1237 else
1238 arg2 = integer_three_node;
1240 /* Argument 0 is an address. */
1241 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1243 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1244 if (TREE_CODE (arg1) != INTEGER_CST)
1246 error ("second argument to %<__builtin_prefetch%> must be a constant");
1247 arg1 = integer_zero_node;
1249 op1 = expand_normal (arg1);
1250 /* Argument 1 must be either zero or one. */
1251 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1253 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1254 " using zero");
1255 op1 = const0_rtx;
1258 /* Argument 2 (locality) must be a compile-time constant int. */
1259 if (TREE_CODE (arg2) != INTEGER_CST)
1261 error ("third argument to %<__builtin_prefetch%> must be a constant");
1262 arg2 = integer_zero_node;
1264 op2 = expand_normal (arg2);
1265 /* Argument 2 must be 0, 1, 2, or 3. */
1266 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1268 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1269 op2 = const0_rtx;
1272 #ifdef HAVE_prefetch
1273 if (HAVE_prefetch)
1275 struct expand_operand ops[3];
1277 create_address_operand (&ops[0], op0);
1278 create_integer_operand (&ops[1], INTVAL (op1));
1279 create_integer_operand (&ops[2], INTVAL (op2));
1280 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1281 return;
1283 #endif
1285 /* Don't do anything with direct references to volatile memory, but
1286 generate code to handle other side effects. */
1287 if (!MEM_P (op0) && side_effects_p (op0))
1288 emit_insn (op0);
1291 /* Get a MEM rtx for expression EXP which is the address of an operand
1292 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1293 the maximum length of the block of memory that might be accessed or
1294 NULL if unknown. */
1296 static rtx
1297 get_memory_rtx (tree exp, tree len)
1299 tree orig_exp = exp;
1300 rtx addr, mem;
1302 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1303 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1304 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1305 exp = TREE_OPERAND (exp, 0);
1307 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1308 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1310 /* Get an expression we can use to find the attributes to assign to MEM.
1311 First remove any nops. */
1312 while (CONVERT_EXPR_P (exp)
1313 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1314 exp = TREE_OPERAND (exp, 0);
1316 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1317 (as builtin stringops may alias with anything). */
1318 exp = fold_build2 (MEM_REF,
1319 build_array_type (char_type_node,
1320 build_range_type (sizetype,
1321 size_one_node, len)),
1322 exp, build_int_cst (ptr_type_node, 0));
1324 /* If the MEM_REF has no acceptable address, try to get the base object
1325 from the original address we got, and build an all-aliasing
1326 unknown-sized access to that one. */
1327 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1328 set_mem_attributes (mem, exp, 0);
1329 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1330 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1331 0))))
1333 exp = build_fold_addr_expr (exp);
1334 exp = fold_build2 (MEM_REF,
1335 build_array_type (char_type_node,
1336 build_range_type (sizetype,
1337 size_zero_node,
1338 NULL)),
1339 exp, build_int_cst (ptr_type_node, 0));
1340 set_mem_attributes (mem, exp, 0);
1342 set_mem_alias_set (mem, 0);
1343 return mem;
1346 /* Built-in functions to perform an untyped call and return. */
1348 #define apply_args_mode \
1349 (this_target_builtins->x_apply_args_mode)
1350 #define apply_result_mode \
1351 (this_target_builtins->x_apply_result_mode)
1353 /* Return the size required for the block returned by __builtin_apply_args,
1354 and initialize apply_args_mode. */
1356 static int
1357 apply_args_size (void)
1359 static int size = -1;
1360 int align;
1361 unsigned int regno;
1362 enum machine_mode mode;
1364 /* The values computed by this function never change. */
1365 if (size < 0)
1367 /* The first value is the incoming arg-pointer. */
1368 size = GET_MODE_SIZE (Pmode);
1370 /* The second value is the structure value address unless this is
1371 passed as an "invisible" first argument. */
1372 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1373 size += GET_MODE_SIZE (Pmode);
1375 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1376 if (FUNCTION_ARG_REGNO_P (regno))
1378 mode = targetm.calls.get_raw_arg_mode (regno);
1380 gcc_assert (mode != VOIDmode);
1382 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1383 if (size % align != 0)
1384 size = CEIL (size, align) * align;
1385 size += GET_MODE_SIZE (mode);
1386 apply_args_mode[regno] = mode;
1388 else
1390 apply_args_mode[regno] = VOIDmode;
1393 return size;
1396 /* Return the size required for the block returned by __builtin_apply,
1397 and initialize apply_result_mode. */
1399 static int
1400 apply_result_size (void)
1402 static int size = -1;
1403 int align, regno;
1404 enum machine_mode mode;
1406 /* The values computed by this function never change. */
1407 if (size < 0)
1409 size = 0;
1411 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1412 if (targetm.calls.function_value_regno_p (regno))
1414 mode = targetm.calls.get_raw_result_mode (regno);
1416 gcc_assert (mode != VOIDmode);
1418 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1419 if (size % align != 0)
1420 size = CEIL (size, align) * align;
1421 size += GET_MODE_SIZE (mode);
1422 apply_result_mode[regno] = mode;
1424 else
1425 apply_result_mode[regno] = VOIDmode;
1427 /* Allow targets that use untyped_call and untyped_return to override
1428 the size so that machine-specific information can be stored here. */
1429 #ifdef APPLY_RESULT_SIZE
1430 size = APPLY_RESULT_SIZE;
1431 #endif
1433 return size;
1436 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1437 /* Create a vector describing the result block RESULT. If SAVEP is true,
1438 the result block is used to save the values; otherwise it is used to
1439 restore the values. */
1441 static rtx
1442 result_vector (int savep, rtx result)
1444 int regno, size, align, nelts;
1445 enum machine_mode mode;
1446 rtx reg, mem;
1447 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1449 size = nelts = 0;
1450 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1451 if ((mode = apply_result_mode[regno]) != VOIDmode)
1453 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1454 if (size % align != 0)
1455 size = CEIL (size, align) * align;
1456 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1457 mem = adjust_address (result, mode, size);
1458 savevec[nelts++] = (savep
1459 ? gen_rtx_SET (VOIDmode, mem, reg)
1460 : gen_rtx_SET (VOIDmode, reg, mem));
1461 size += GET_MODE_SIZE (mode);
1463 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1465 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1467 /* Save the state required to perform an untyped call with the same
1468 arguments as were passed to the current function. */
1470 static rtx
1471 expand_builtin_apply_args_1 (void)
1473 rtx registers, tem;
1474 int size, align, regno;
1475 enum machine_mode mode;
1476 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1478 /* Create a block where the arg-pointer, structure value address,
1479 and argument registers can be saved. */
1480 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1482 /* Walk past the arg-pointer and structure value address. */
1483 size = GET_MODE_SIZE (Pmode);
1484 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1485 size += GET_MODE_SIZE (Pmode);
1487 /* Save each register used in calling a function to the block. */
1488 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1489 if ((mode = apply_args_mode[regno]) != VOIDmode)
1491 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1492 if (size % align != 0)
1493 size = CEIL (size, align) * align;
1495 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1497 emit_move_insn (adjust_address (registers, mode, size), tem);
1498 size += GET_MODE_SIZE (mode);
1501 /* Save the arg pointer to the block. */
1502 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1503 #ifdef STACK_GROWS_DOWNWARD
1504 /* We need the pointer as the caller actually passed them to us, not
1505 as we might have pretended they were passed. Make sure it's a valid
1506 operand, as emit_move_insn isn't expected to handle a PLUS. */
1508 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1509 NULL_RTX);
1510 #endif
1511 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1513 size = GET_MODE_SIZE (Pmode);
1515 /* Save the structure value address unless this is passed as an
1516 "invisible" first argument. */
1517 if (struct_incoming_value)
1519 emit_move_insn (adjust_address (registers, Pmode, size),
1520 copy_to_reg (struct_incoming_value));
1521 size += GET_MODE_SIZE (Pmode);
1524 /* Return the address of the block. */
1525 return copy_addr_to_reg (XEXP (registers, 0));
1528 /* __builtin_apply_args returns block of memory allocated on
1529 the stack into which is stored the arg pointer, structure
1530 value address, static chain, and all the registers that might
1531 possibly be used in performing a function call. The code is
1532 moved to the start of the function so the incoming values are
1533 saved. */
1535 static rtx
1536 expand_builtin_apply_args (void)
1538 /* Don't do __builtin_apply_args more than once in a function.
1539 Save the result of the first call and reuse it. */
1540 if (apply_args_value != 0)
1541 return apply_args_value;
1543 /* When this function is called, it means that registers must be
1544 saved on entry to this function. So we migrate the
1545 call to the first insn of this function. */
1546 rtx temp;
1547 rtx seq;
1549 start_sequence ();
1550 temp = expand_builtin_apply_args_1 ();
1551 seq = get_insns ();
1552 end_sequence ();
1554 apply_args_value = temp;
1556 /* Put the insns after the NOTE that starts the function.
1557 If this is inside a start_sequence, make the outer-level insn
1558 chain current, so the code is placed at the start of the
1559 function. If internal_arg_pointer is a non-virtual pseudo,
1560 it needs to be placed after the function that initializes
1561 that pseudo. */
1562 push_topmost_sequence ();
1563 if (REG_P (crtl->args.internal_arg_pointer)
1564 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1565 emit_insn_before (seq, parm_birth_insn);
1566 else
1567 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1568 pop_topmost_sequence ();
1569 return temp;
1573 /* Perform an untyped call and save the state required to perform an
1574 untyped return of whatever value was returned by the given function. */
1576 static rtx
1577 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1579 int size, align, regno;
1580 enum machine_mode mode;
1581 rtx incoming_args, result, reg, dest, src, call_insn;
1582 rtx old_stack_level = 0;
1583 rtx call_fusage = 0;
1584 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1586 arguments = convert_memory_address (Pmode, arguments);
1588 /* Create a block where the return registers can be saved. */
1589 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1591 /* Fetch the arg pointer from the ARGUMENTS block. */
1592 incoming_args = gen_reg_rtx (Pmode);
1593 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1594 #ifndef STACK_GROWS_DOWNWARD
1595 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1596 incoming_args, 0, OPTAB_LIB_WIDEN);
1597 #endif
1599 /* Push a new argument block and copy the arguments. Do not allow
1600 the (potential) memcpy call below to interfere with our stack
1601 manipulations. */
1602 do_pending_stack_adjust ();
1603 NO_DEFER_POP;
1605 /* Save the stack with nonlocal if available. */
1606 #ifdef HAVE_save_stack_nonlocal
1607 if (HAVE_save_stack_nonlocal)
1608 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1609 else
1610 #endif
1611 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1613 /* Allocate a block of memory onto the stack and copy the memory
1614 arguments to the outgoing arguments address. We can pass TRUE
1615 as the 4th argument because we just saved the stack pointer
1616 and will restore it right after the call. */
1617 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1619 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1620 may have already set current_function_calls_alloca to true.
1621 current_function_calls_alloca won't be set if argsize is zero,
1622 so we have to guarantee need_drap is true here. */
1623 if (SUPPORTS_STACK_ALIGNMENT)
1624 crtl->need_drap = true;
1626 dest = virtual_outgoing_args_rtx;
1627 #ifndef STACK_GROWS_DOWNWARD
1628 if (CONST_INT_P (argsize))
1629 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1630 else
1631 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1632 #endif
1633 dest = gen_rtx_MEM (BLKmode, dest);
1634 set_mem_align (dest, PARM_BOUNDARY);
1635 src = gen_rtx_MEM (BLKmode, incoming_args);
1636 set_mem_align (src, PARM_BOUNDARY);
1637 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1639 /* Refer to the argument block. */
1640 apply_args_size ();
1641 arguments = gen_rtx_MEM (BLKmode, arguments);
1642 set_mem_align (arguments, PARM_BOUNDARY);
1644 /* Walk past the arg-pointer and structure value address. */
1645 size = GET_MODE_SIZE (Pmode);
1646 if (struct_value)
1647 size += GET_MODE_SIZE (Pmode);
1649 /* Restore each of the registers previously saved. Make USE insns
1650 for each of these registers for use in making the call. */
1651 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1652 if ((mode = apply_args_mode[regno]) != VOIDmode)
1654 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1655 if (size % align != 0)
1656 size = CEIL (size, align) * align;
1657 reg = gen_rtx_REG (mode, regno);
1658 emit_move_insn (reg, adjust_address (arguments, mode, size));
1659 use_reg (&call_fusage, reg);
1660 size += GET_MODE_SIZE (mode);
1663 /* Restore the structure value address unless this is passed as an
1664 "invisible" first argument. */
1665 size = GET_MODE_SIZE (Pmode);
1666 if (struct_value)
1668 rtx value = gen_reg_rtx (Pmode);
1669 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1670 emit_move_insn (struct_value, value);
1671 if (REG_P (struct_value))
1672 use_reg (&call_fusage, struct_value);
1673 size += GET_MODE_SIZE (Pmode);
1676 /* All arguments and registers used for the call are set up by now! */
1677 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1679 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1680 and we don't want to load it into a register as an optimization,
1681 because prepare_call_address already did it if it should be done. */
1682 if (GET_CODE (function) != SYMBOL_REF)
1683 function = memory_address (FUNCTION_MODE, function);
1685 /* Generate the actual call instruction and save the return value. */
1686 #ifdef HAVE_untyped_call
1687 if (HAVE_untyped_call)
1688 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1689 result, result_vector (1, result)));
1690 else
1691 #endif
1692 #ifdef HAVE_call_value
1693 if (HAVE_call_value)
1695 rtx valreg = 0;
1697 /* Locate the unique return register. It is not possible to
1698 express a call that sets more than one return register using
1699 call_value; use untyped_call for that. In fact, untyped_call
1700 only needs to save the return registers in the given block. */
1701 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1702 if ((mode = apply_result_mode[regno]) != VOIDmode)
1704 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1706 valreg = gen_rtx_REG (mode, regno);
1709 emit_call_insn (GEN_CALL_VALUE (valreg,
1710 gen_rtx_MEM (FUNCTION_MODE, function),
1711 const0_rtx, NULL_RTX, const0_rtx));
1713 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1715 else
1716 #endif
1717 gcc_unreachable ();
1719 /* Find the CALL insn we just emitted, and attach the register usage
1720 information. */
1721 call_insn = last_call_insn ();
1722 add_function_usage_to (call_insn, call_fusage);
1724 /* Restore the stack. */
1725 #ifdef HAVE_save_stack_nonlocal
1726 if (HAVE_save_stack_nonlocal)
1727 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1728 else
1729 #endif
1730 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1731 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1733 OK_DEFER_POP;
1735 /* Return the address of the result block. */
1736 result = copy_addr_to_reg (XEXP (result, 0));
1737 return convert_memory_address (ptr_mode, result);
1740 /* Perform an untyped return. */
1742 static void
1743 expand_builtin_return (rtx result)
1745 int size, align, regno;
1746 enum machine_mode mode;
1747 rtx reg;
1748 rtx call_fusage = 0;
1750 result = convert_memory_address (Pmode, result);
1752 apply_result_size ();
1753 result = gen_rtx_MEM (BLKmode, result);
1755 #ifdef HAVE_untyped_return
1756 if (HAVE_untyped_return)
1758 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1759 emit_barrier ();
1760 return;
1762 #endif
1764 /* Restore the return value and note that each value is used. */
1765 size = 0;
1766 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1767 if ((mode = apply_result_mode[regno]) != VOIDmode)
1769 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1770 if (size % align != 0)
1771 size = CEIL (size, align) * align;
1772 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1773 emit_move_insn (reg, adjust_address (result, mode, size));
1775 push_to_sequence (call_fusage);
1776 emit_use (reg);
1777 call_fusage = get_insns ();
1778 end_sequence ();
1779 size += GET_MODE_SIZE (mode);
1782 /* Put the USE insns before the return. */
1783 emit_insn (call_fusage);
1785 /* Return whatever values was restored by jumping directly to the end
1786 of the function. */
1787 expand_naked_return ();
1790 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1792 static enum type_class
1793 type_to_class (tree type)
1795 switch (TREE_CODE (type))
1797 case VOID_TYPE: return void_type_class;
1798 case INTEGER_TYPE: return integer_type_class;
1799 case ENUMERAL_TYPE: return enumeral_type_class;
1800 case BOOLEAN_TYPE: return boolean_type_class;
1801 case POINTER_TYPE: return pointer_type_class;
1802 case REFERENCE_TYPE: return reference_type_class;
1803 case OFFSET_TYPE: return offset_type_class;
1804 case REAL_TYPE: return real_type_class;
1805 case COMPLEX_TYPE: return complex_type_class;
1806 case FUNCTION_TYPE: return function_type_class;
1807 case METHOD_TYPE: return method_type_class;
1808 case RECORD_TYPE: return record_type_class;
1809 case UNION_TYPE:
1810 case QUAL_UNION_TYPE: return union_type_class;
1811 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1812 ? string_type_class : array_type_class);
1813 case LANG_TYPE: return lang_type_class;
1814 default: return no_type_class;
1818 /* Expand a call EXP to __builtin_classify_type. */
1820 static rtx
1821 expand_builtin_classify_type (tree exp)
1823 if (call_expr_nargs (exp))
1824 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1825 return GEN_INT (no_type_class);
1828 /* This helper macro, meant to be used in mathfn_built_in below,
1829 determines which among a set of three builtin math functions is
1830 appropriate for a given type mode. The `F' and `L' cases are
1831 automatically generated from the `double' case. */
1832 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1833 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1834 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1835 fcodel = BUILT_IN_MATHFN##L ; break;
1836 /* Similar to above, but appends _R after any F/L suffix. */
1837 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1838 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1839 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1840 fcodel = BUILT_IN_MATHFN##L_R ; break;
1842 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1843 if available. If IMPLICIT is true use the implicit builtin declaration,
1844 otherwise use the explicit declaration. If we can't do the conversion,
1845 return zero. */
1847 static tree
1848 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1850 enum built_in_function fcode, fcodef, fcodel, fcode2;
1852 switch (fn)
1854 CASE_MATHFN (BUILT_IN_ACOS)
1855 CASE_MATHFN (BUILT_IN_ACOSH)
1856 CASE_MATHFN (BUILT_IN_ASIN)
1857 CASE_MATHFN (BUILT_IN_ASINH)
1858 CASE_MATHFN (BUILT_IN_ATAN)
1859 CASE_MATHFN (BUILT_IN_ATAN2)
1860 CASE_MATHFN (BUILT_IN_ATANH)
1861 CASE_MATHFN (BUILT_IN_CBRT)
1862 CASE_MATHFN (BUILT_IN_CEIL)
1863 CASE_MATHFN (BUILT_IN_CEXPI)
1864 CASE_MATHFN (BUILT_IN_COPYSIGN)
1865 CASE_MATHFN (BUILT_IN_COS)
1866 CASE_MATHFN (BUILT_IN_COSH)
1867 CASE_MATHFN (BUILT_IN_DREM)
1868 CASE_MATHFN (BUILT_IN_ERF)
1869 CASE_MATHFN (BUILT_IN_ERFC)
1870 CASE_MATHFN (BUILT_IN_EXP)
1871 CASE_MATHFN (BUILT_IN_EXP10)
1872 CASE_MATHFN (BUILT_IN_EXP2)
1873 CASE_MATHFN (BUILT_IN_EXPM1)
1874 CASE_MATHFN (BUILT_IN_FABS)
1875 CASE_MATHFN (BUILT_IN_FDIM)
1876 CASE_MATHFN (BUILT_IN_FLOOR)
1877 CASE_MATHFN (BUILT_IN_FMA)
1878 CASE_MATHFN (BUILT_IN_FMAX)
1879 CASE_MATHFN (BUILT_IN_FMIN)
1880 CASE_MATHFN (BUILT_IN_FMOD)
1881 CASE_MATHFN (BUILT_IN_FREXP)
1882 CASE_MATHFN (BUILT_IN_GAMMA)
1883 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1884 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1885 CASE_MATHFN (BUILT_IN_HYPOT)
1886 CASE_MATHFN (BUILT_IN_ILOGB)
1887 CASE_MATHFN (BUILT_IN_ICEIL)
1888 CASE_MATHFN (BUILT_IN_IFLOOR)
1889 CASE_MATHFN (BUILT_IN_INF)
1890 CASE_MATHFN (BUILT_IN_IRINT)
1891 CASE_MATHFN (BUILT_IN_IROUND)
1892 CASE_MATHFN (BUILT_IN_ISINF)
1893 CASE_MATHFN (BUILT_IN_J0)
1894 CASE_MATHFN (BUILT_IN_J1)
1895 CASE_MATHFN (BUILT_IN_JN)
1896 CASE_MATHFN (BUILT_IN_LCEIL)
1897 CASE_MATHFN (BUILT_IN_LDEXP)
1898 CASE_MATHFN (BUILT_IN_LFLOOR)
1899 CASE_MATHFN (BUILT_IN_LGAMMA)
1900 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1901 CASE_MATHFN (BUILT_IN_LLCEIL)
1902 CASE_MATHFN (BUILT_IN_LLFLOOR)
1903 CASE_MATHFN (BUILT_IN_LLRINT)
1904 CASE_MATHFN (BUILT_IN_LLROUND)
1905 CASE_MATHFN (BUILT_IN_LOG)
1906 CASE_MATHFN (BUILT_IN_LOG10)
1907 CASE_MATHFN (BUILT_IN_LOG1P)
1908 CASE_MATHFN (BUILT_IN_LOG2)
1909 CASE_MATHFN (BUILT_IN_LOGB)
1910 CASE_MATHFN (BUILT_IN_LRINT)
1911 CASE_MATHFN (BUILT_IN_LROUND)
1912 CASE_MATHFN (BUILT_IN_MODF)
1913 CASE_MATHFN (BUILT_IN_NAN)
1914 CASE_MATHFN (BUILT_IN_NANS)
1915 CASE_MATHFN (BUILT_IN_NEARBYINT)
1916 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1917 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1918 CASE_MATHFN (BUILT_IN_POW)
1919 CASE_MATHFN (BUILT_IN_POWI)
1920 CASE_MATHFN (BUILT_IN_POW10)
1921 CASE_MATHFN (BUILT_IN_REMAINDER)
1922 CASE_MATHFN (BUILT_IN_REMQUO)
1923 CASE_MATHFN (BUILT_IN_RINT)
1924 CASE_MATHFN (BUILT_IN_ROUND)
1925 CASE_MATHFN (BUILT_IN_SCALB)
1926 CASE_MATHFN (BUILT_IN_SCALBLN)
1927 CASE_MATHFN (BUILT_IN_SCALBN)
1928 CASE_MATHFN (BUILT_IN_SIGNBIT)
1929 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1930 CASE_MATHFN (BUILT_IN_SIN)
1931 CASE_MATHFN (BUILT_IN_SINCOS)
1932 CASE_MATHFN (BUILT_IN_SINH)
1933 CASE_MATHFN (BUILT_IN_SQRT)
1934 CASE_MATHFN (BUILT_IN_TAN)
1935 CASE_MATHFN (BUILT_IN_TANH)
1936 CASE_MATHFN (BUILT_IN_TGAMMA)
1937 CASE_MATHFN (BUILT_IN_TRUNC)
1938 CASE_MATHFN (BUILT_IN_Y0)
1939 CASE_MATHFN (BUILT_IN_Y1)
1940 CASE_MATHFN (BUILT_IN_YN)
1942 default:
1943 return NULL_TREE;
1946 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1947 fcode2 = fcode;
1948 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1949 fcode2 = fcodef;
1950 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1951 fcode2 = fcodel;
1952 else
1953 return NULL_TREE;
1955 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1956 return NULL_TREE;
1958 return builtin_decl_explicit (fcode2);
1961 /* Like mathfn_built_in_1(), but always use the implicit array. */
1963 tree
1964 mathfn_built_in (tree type, enum built_in_function fn)
1966 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1969 /* If errno must be maintained, expand the RTL to check if the result,
1970 TARGET, of a built-in function call, EXP, is NaN, and if so set
1971 errno to EDOM. */
1973 static void
1974 expand_errno_check (tree exp, rtx target)
1976 rtx lab = gen_label_rtx ();
1978 /* Test the result; if it is NaN, set errno=EDOM because
1979 the argument was not in the domain. */
1980 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1981 NULL_RTX, NULL_RTX, lab,
1982 /* The jump is very likely. */
1983 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1985 #ifdef TARGET_EDOM
1986 /* If this built-in doesn't throw an exception, set errno directly. */
1987 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1989 #ifdef GEN_ERRNO_RTX
1990 rtx errno_rtx = GEN_ERRNO_RTX;
1991 #else
1992 rtx errno_rtx
1993 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1994 #endif
1995 emit_move_insn (errno_rtx,
1996 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
1997 emit_label (lab);
1998 return;
2000 #endif
2002 /* Make sure the library call isn't expanded as a tail call. */
2003 CALL_EXPR_TAILCALL (exp) = 0;
2005 /* We can't set errno=EDOM directly; let the library call do it.
2006 Pop the arguments right away in case the call gets deleted. */
2007 NO_DEFER_POP;
2008 expand_call (exp, target, 0);
2009 OK_DEFER_POP;
2010 emit_label (lab);
2013 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2014 Return NULL_RTX if a normal call should be emitted rather than expanding
2015 the function in-line. EXP is the expression that is a call to the builtin
2016 function; if convenient, the result should be placed in TARGET.
2017 SUBTARGET may be used as the target for computing one of EXP's operands. */
2019 static rtx
2020 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2022 optab builtin_optab;
2023 rtx op0, insns;
2024 tree fndecl = get_callee_fndecl (exp);
2025 enum machine_mode mode;
2026 bool errno_set = false;
2027 bool try_widening = false;
2028 tree arg;
2030 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2031 return NULL_RTX;
2033 arg = CALL_EXPR_ARG (exp, 0);
2035 switch (DECL_FUNCTION_CODE (fndecl))
2037 CASE_FLT_FN (BUILT_IN_SQRT):
2038 errno_set = ! tree_expr_nonnegative_p (arg);
2039 try_widening = true;
2040 builtin_optab = sqrt_optab;
2041 break;
2042 CASE_FLT_FN (BUILT_IN_EXP):
2043 errno_set = true; builtin_optab = exp_optab; break;
2044 CASE_FLT_FN (BUILT_IN_EXP10):
2045 CASE_FLT_FN (BUILT_IN_POW10):
2046 errno_set = true; builtin_optab = exp10_optab; break;
2047 CASE_FLT_FN (BUILT_IN_EXP2):
2048 errno_set = true; builtin_optab = exp2_optab; break;
2049 CASE_FLT_FN (BUILT_IN_EXPM1):
2050 errno_set = true; builtin_optab = expm1_optab; break;
2051 CASE_FLT_FN (BUILT_IN_LOGB):
2052 errno_set = true; builtin_optab = logb_optab; break;
2053 CASE_FLT_FN (BUILT_IN_LOG):
2054 errno_set = true; builtin_optab = log_optab; break;
2055 CASE_FLT_FN (BUILT_IN_LOG10):
2056 errno_set = true; builtin_optab = log10_optab; break;
2057 CASE_FLT_FN (BUILT_IN_LOG2):
2058 errno_set = true; builtin_optab = log2_optab; break;
2059 CASE_FLT_FN (BUILT_IN_LOG1P):
2060 errno_set = true; builtin_optab = log1p_optab; break;
2061 CASE_FLT_FN (BUILT_IN_ASIN):
2062 builtin_optab = asin_optab; break;
2063 CASE_FLT_FN (BUILT_IN_ACOS):
2064 builtin_optab = acos_optab; break;
2065 CASE_FLT_FN (BUILT_IN_TAN):
2066 builtin_optab = tan_optab; break;
2067 CASE_FLT_FN (BUILT_IN_ATAN):
2068 builtin_optab = atan_optab; break;
2069 CASE_FLT_FN (BUILT_IN_FLOOR):
2070 builtin_optab = floor_optab; break;
2071 CASE_FLT_FN (BUILT_IN_CEIL):
2072 builtin_optab = ceil_optab; break;
2073 CASE_FLT_FN (BUILT_IN_TRUNC):
2074 builtin_optab = btrunc_optab; break;
2075 CASE_FLT_FN (BUILT_IN_ROUND):
2076 builtin_optab = round_optab; break;
2077 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2078 builtin_optab = nearbyint_optab;
2079 if (flag_trapping_math)
2080 break;
2081 /* Else fallthrough and expand as rint. */
2082 CASE_FLT_FN (BUILT_IN_RINT):
2083 builtin_optab = rint_optab; break;
2084 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2085 builtin_optab = significand_optab; break;
2086 default:
2087 gcc_unreachable ();
2090 /* Make a suitable register to place result in. */
2091 mode = TYPE_MODE (TREE_TYPE (exp));
2093 if (! flag_errno_math || ! HONOR_NANS (mode))
2094 errno_set = false;
2096 /* Before working hard, check whether the instruction is available, but try
2097 to widen the mode for specific operations. */
2098 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2099 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2100 && (!errno_set || !optimize_insn_for_size_p ()))
2102 rtx result = gen_reg_rtx (mode);
2104 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2105 need to expand the argument again. This way, we will not perform
2106 side-effects more the once. */
2107 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2109 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2111 start_sequence ();
2113 /* Compute into RESULT.
2114 Set RESULT to wherever the result comes back. */
2115 result = expand_unop (mode, builtin_optab, op0, result, 0);
2117 if (result != 0)
2119 if (errno_set)
2120 expand_errno_check (exp, result);
2122 /* Output the entire sequence. */
2123 insns = get_insns ();
2124 end_sequence ();
2125 emit_insn (insns);
2126 return result;
2129 /* If we were unable to expand via the builtin, stop the sequence
2130 (without outputting the insns) and call to the library function
2131 with the stabilized argument list. */
2132 end_sequence ();
2135 return expand_call (exp, target, target == const0_rtx);
2138 /* Expand a call to the builtin binary math functions (pow and atan2).
2139 Return NULL_RTX if a normal call should be emitted rather than expanding the
2140 function in-line. EXP is the expression that is a call to the builtin
2141 function; if convenient, the result should be placed in TARGET.
2142 SUBTARGET may be used as the target for computing one of EXP's
2143 operands. */
2145 static rtx
2146 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2148 optab builtin_optab;
2149 rtx op0, op1, insns, result;
2150 int op1_type = REAL_TYPE;
2151 tree fndecl = get_callee_fndecl (exp);
2152 tree arg0, arg1;
2153 enum machine_mode mode;
2154 bool errno_set = true;
2156 switch (DECL_FUNCTION_CODE (fndecl))
2158 CASE_FLT_FN (BUILT_IN_SCALBN):
2159 CASE_FLT_FN (BUILT_IN_SCALBLN):
2160 CASE_FLT_FN (BUILT_IN_LDEXP):
2161 op1_type = INTEGER_TYPE;
2162 default:
2163 break;
2166 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2167 return NULL_RTX;
2169 arg0 = CALL_EXPR_ARG (exp, 0);
2170 arg1 = CALL_EXPR_ARG (exp, 1);
2172 switch (DECL_FUNCTION_CODE (fndecl))
2174 CASE_FLT_FN (BUILT_IN_POW):
2175 builtin_optab = pow_optab; break;
2176 CASE_FLT_FN (BUILT_IN_ATAN2):
2177 builtin_optab = atan2_optab; break;
2178 CASE_FLT_FN (BUILT_IN_SCALB):
2179 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2180 return 0;
2181 builtin_optab = scalb_optab; break;
2182 CASE_FLT_FN (BUILT_IN_SCALBN):
2183 CASE_FLT_FN (BUILT_IN_SCALBLN):
2184 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2185 return 0;
2186 /* Fall through... */
2187 CASE_FLT_FN (BUILT_IN_LDEXP):
2188 builtin_optab = ldexp_optab; break;
2189 CASE_FLT_FN (BUILT_IN_FMOD):
2190 builtin_optab = fmod_optab; break;
2191 CASE_FLT_FN (BUILT_IN_REMAINDER):
2192 CASE_FLT_FN (BUILT_IN_DREM):
2193 builtin_optab = remainder_optab; break;
2194 default:
2195 gcc_unreachable ();
2198 /* Make a suitable register to place result in. */
2199 mode = TYPE_MODE (TREE_TYPE (exp));
2201 /* Before working hard, check whether the instruction is available. */
2202 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2203 return NULL_RTX;
2205 result = gen_reg_rtx (mode);
2207 if (! flag_errno_math || ! HONOR_NANS (mode))
2208 errno_set = false;
2210 if (errno_set && optimize_insn_for_size_p ())
2211 return 0;
2213 /* Always stabilize the argument list. */
2214 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2215 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2217 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2218 op1 = expand_normal (arg1);
2220 start_sequence ();
2222 /* Compute into RESULT.
2223 Set RESULT to wherever the result comes back. */
2224 result = expand_binop (mode, builtin_optab, op0, op1,
2225 result, 0, OPTAB_DIRECT);
2227 /* If we were unable to expand via the builtin, stop the sequence
2228 (without outputting the insns) and call to the library function
2229 with the stabilized argument list. */
2230 if (result == 0)
2232 end_sequence ();
2233 return expand_call (exp, target, target == const0_rtx);
2236 if (errno_set)
2237 expand_errno_check (exp, result);
2239 /* Output the entire sequence. */
2240 insns = get_insns ();
2241 end_sequence ();
2242 emit_insn (insns);
2244 return result;
2247 /* Expand a call to the builtin trinary math functions (fma).
2248 Return NULL_RTX if a normal call should be emitted rather than expanding the
2249 function in-line. EXP is the expression that is a call to the builtin
2250 function; if convenient, the result should be placed in TARGET.
2251 SUBTARGET may be used as the target for computing one of EXP's
2252 operands. */
2254 static rtx
2255 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2257 optab builtin_optab;
2258 rtx op0, op1, op2, insns, result;
2259 tree fndecl = get_callee_fndecl (exp);
2260 tree arg0, arg1, arg2;
2261 enum machine_mode mode;
2263 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2264 return NULL_RTX;
2266 arg0 = CALL_EXPR_ARG (exp, 0);
2267 arg1 = CALL_EXPR_ARG (exp, 1);
2268 arg2 = CALL_EXPR_ARG (exp, 2);
2270 switch (DECL_FUNCTION_CODE (fndecl))
2272 CASE_FLT_FN (BUILT_IN_FMA):
2273 builtin_optab = fma_optab; break;
2274 default:
2275 gcc_unreachable ();
2278 /* Make a suitable register to place result in. */
2279 mode = TYPE_MODE (TREE_TYPE (exp));
2281 /* Before working hard, check whether the instruction is available. */
2282 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2283 return NULL_RTX;
2285 result = gen_reg_rtx (mode);
2287 /* Always stabilize the argument list. */
2288 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2289 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2290 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2292 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2293 op1 = expand_normal (arg1);
2294 op2 = expand_normal (arg2);
2296 start_sequence ();
2298 /* Compute into RESULT.
2299 Set RESULT to wherever the result comes back. */
2300 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2301 result, 0);
2303 /* If we were unable to expand via the builtin, stop the sequence
2304 (without outputting the insns) and call to the library function
2305 with the stabilized argument list. */
2306 if (result == 0)
2308 end_sequence ();
2309 return expand_call (exp, target, target == const0_rtx);
2312 /* Output the entire sequence. */
2313 insns = get_insns ();
2314 end_sequence ();
2315 emit_insn (insns);
2317 return result;
2320 /* Expand a call to the builtin sin and cos math functions.
2321 Return NULL_RTX if a normal call should be emitted rather than expanding the
2322 function in-line. EXP is the expression that is a call to the builtin
2323 function; if convenient, the result should be placed in TARGET.
2324 SUBTARGET may be used as the target for computing one of EXP's
2325 operands. */
2327 static rtx
2328 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2330 optab builtin_optab;
2331 rtx op0, insns;
2332 tree fndecl = get_callee_fndecl (exp);
2333 enum machine_mode mode;
2334 tree arg;
2336 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2337 return NULL_RTX;
2339 arg = CALL_EXPR_ARG (exp, 0);
2341 switch (DECL_FUNCTION_CODE (fndecl))
2343 CASE_FLT_FN (BUILT_IN_SIN):
2344 CASE_FLT_FN (BUILT_IN_COS):
2345 builtin_optab = sincos_optab; break;
2346 default:
2347 gcc_unreachable ();
2350 /* Make a suitable register to place result in. */
2351 mode = TYPE_MODE (TREE_TYPE (exp));
2353 /* Check if sincos insn is available, otherwise fallback
2354 to sin or cos insn. */
2355 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2356 switch (DECL_FUNCTION_CODE (fndecl))
2358 CASE_FLT_FN (BUILT_IN_SIN):
2359 builtin_optab = sin_optab; break;
2360 CASE_FLT_FN (BUILT_IN_COS):
2361 builtin_optab = cos_optab; break;
2362 default:
2363 gcc_unreachable ();
2366 /* Before working hard, check whether the instruction is available. */
2367 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2369 rtx result = gen_reg_rtx (mode);
2371 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2372 need to expand the argument again. This way, we will not perform
2373 side-effects more the once. */
2374 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2376 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2378 start_sequence ();
2380 /* Compute into RESULT.
2381 Set RESULT to wherever the result comes back. */
2382 if (builtin_optab == sincos_optab)
2384 int ok;
2386 switch (DECL_FUNCTION_CODE (fndecl))
2388 CASE_FLT_FN (BUILT_IN_SIN):
2389 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2390 break;
2391 CASE_FLT_FN (BUILT_IN_COS):
2392 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2393 break;
2394 default:
2395 gcc_unreachable ();
2397 gcc_assert (ok);
2399 else
2400 result = expand_unop (mode, builtin_optab, op0, result, 0);
2402 if (result != 0)
2404 /* Output the entire sequence. */
2405 insns = get_insns ();
2406 end_sequence ();
2407 emit_insn (insns);
2408 return result;
2411 /* If we were unable to expand via the builtin, stop the sequence
2412 (without outputting the insns) and call to the library function
2413 with the stabilized argument list. */
2414 end_sequence ();
2417 return expand_call (exp, target, target == const0_rtx);
2420 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2421 return an RTL instruction code that implements the functionality.
2422 If that isn't possible or available return CODE_FOR_nothing. */
2424 static enum insn_code
2425 interclass_mathfn_icode (tree arg, tree fndecl)
2427 bool errno_set = false;
2428 optab builtin_optab = unknown_optab;
2429 enum machine_mode mode;
2431 switch (DECL_FUNCTION_CODE (fndecl))
2433 CASE_FLT_FN (BUILT_IN_ILOGB):
2434 errno_set = true; builtin_optab = ilogb_optab; break;
2435 CASE_FLT_FN (BUILT_IN_ISINF):
2436 builtin_optab = isinf_optab; break;
2437 case BUILT_IN_ISNORMAL:
2438 case BUILT_IN_ISFINITE:
2439 CASE_FLT_FN (BUILT_IN_FINITE):
2440 case BUILT_IN_FINITED32:
2441 case BUILT_IN_FINITED64:
2442 case BUILT_IN_FINITED128:
2443 case BUILT_IN_ISINFD32:
2444 case BUILT_IN_ISINFD64:
2445 case BUILT_IN_ISINFD128:
2446 /* These builtins have no optabs (yet). */
2447 break;
2448 default:
2449 gcc_unreachable ();
2452 /* There's no easy way to detect the case we need to set EDOM. */
2453 if (flag_errno_math && errno_set)
2454 return CODE_FOR_nothing;
2456 /* Optab mode depends on the mode of the input argument. */
2457 mode = TYPE_MODE (TREE_TYPE (arg));
2459 if (builtin_optab)
2460 return optab_handler (builtin_optab, mode);
2461 return CODE_FOR_nothing;
2464 /* Expand a call to one of the builtin math functions that operate on
2465 floating point argument and output an integer result (ilogb, isinf,
2466 isnan, etc).
2467 Return 0 if a normal call should be emitted rather than expanding the
2468 function in-line. EXP is the expression that is a call to the builtin
2469 function; if convenient, the result should be placed in TARGET. */
2471 static rtx
2472 expand_builtin_interclass_mathfn (tree exp, rtx target)
2474 enum insn_code icode = CODE_FOR_nothing;
2475 rtx op0;
2476 tree fndecl = get_callee_fndecl (exp);
2477 enum machine_mode mode;
2478 tree arg;
2480 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2481 return NULL_RTX;
2483 arg = CALL_EXPR_ARG (exp, 0);
2484 icode = interclass_mathfn_icode (arg, fndecl);
2485 mode = TYPE_MODE (TREE_TYPE (arg));
2487 if (icode != CODE_FOR_nothing)
2489 struct expand_operand ops[1];
2490 rtx last = get_last_insn ();
2491 tree orig_arg = arg;
2493 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2494 need to expand the argument again. This way, we will not perform
2495 side-effects more the once. */
2496 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2498 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2500 if (mode != GET_MODE (op0))
2501 op0 = convert_to_mode (mode, op0, 0);
2503 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2504 if (maybe_legitimize_operands (icode, 0, 1, ops)
2505 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2506 return ops[0].value;
2508 delete_insns_since (last);
2509 CALL_EXPR_ARG (exp, 0) = orig_arg;
2512 return NULL_RTX;
2515 /* Expand a call to the builtin sincos math function.
2516 Return NULL_RTX if a normal call should be emitted rather than expanding the
2517 function in-line. EXP is the expression that is a call to the builtin
2518 function. */
2520 static rtx
2521 expand_builtin_sincos (tree exp)
2523 rtx op0, op1, op2, target1, target2;
2524 enum machine_mode mode;
2525 tree arg, sinp, cosp;
2526 int result;
2527 location_t loc = EXPR_LOCATION (exp);
2528 tree alias_type, alias_off;
2530 if (!validate_arglist (exp, REAL_TYPE,
2531 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2532 return NULL_RTX;
2534 arg = CALL_EXPR_ARG (exp, 0);
2535 sinp = CALL_EXPR_ARG (exp, 1);
2536 cosp = CALL_EXPR_ARG (exp, 2);
2538 /* Make a suitable register to place result in. */
2539 mode = TYPE_MODE (TREE_TYPE (arg));
2541 /* Check if sincos insn is available, otherwise emit the call. */
2542 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2543 return NULL_RTX;
2545 target1 = gen_reg_rtx (mode);
2546 target2 = gen_reg_rtx (mode);
2548 op0 = expand_normal (arg);
2549 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2550 alias_off = build_int_cst (alias_type, 0);
2551 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2552 sinp, alias_off));
2553 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2554 cosp, alias_off));
2556 /* Compute into target1 and target2.
2557 Set TARGET to wherever the result comes back. */
2558 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2559 gcc_assert (result);
2561 /* Move target1 and target2 to the memory locations indicated
2562 by op1 and op2. */
2563 emit_move_insn (op1, target1);
2564 emit_move_insn (op2, target2);
2566 return const0_rtx;
2569 /* Expand a call to the internal cexpi builtin to the sincos math function.
2570 EXP is the expression that is a call to the builtin function; if convenient,
2571 the result should be placed in TARGET. */
2573 static rtx
2574 expand_builtin_cexpi (tree exp, rtx target)
2576 tree fndecl = get_callee_fndecl (exp);
2577 tree arg, type;
2578 enum machine_mode mode;
2579 rtx op0, op1, op2;
2580 location_t loc = EXPR_LOCATION (exp);
2582 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2583 return NULL_RTX;
2585 arg = CALL_EXPR_ARG (exp, 0);
2586 type = TREE_TYPE (arg);
2587 mode = TYPE_MODE (TREE_TYPE (arg));
2589 /* Try expanding via a sincos optab, fall back to emitting a libcall
2590 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2591 is only generated from sincos, cexp or if we have either of them. */
2592 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2594 op1 = gen_reg_rtx (mode);
2595 op2 = gen_reg_rtx (mode);
2597 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2599 /* Compute into op1 and op2. */
2600 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2602 else if (targetm.libc_has_function (function_sincos))
2604 tree call, fn = NULL_TREE;
2605 tree top1, top2;
2606 rtx op1a, op2a;
2608 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2609 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2610 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2611 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2612 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2613 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2614 else
2615 gcc_unreachable ();
2617 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2618 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2619 op1a = copy_addr_to_reg (XEXP (op1, 0));
2620 op2a = copy_addr_to_reg (XEXP (op2, 0));
2621 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2622 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2624 /* Make sure not to fold the sincos call again. */
2625 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2626 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2627 call, 3, arg, top1, top2));
2629 else
2631 tree call, fn = NULL_TREE, narg;
2632 tree ctype = build_complex_type (type);
2634 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2635 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2636 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2637 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2638 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2639 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2640 else
2641 gcc_unreachable ();
2643 /* If we don't have a decl for cexp create one. This is the
2644 friendliest fallback if the user calls __builtin_cexpi
2645 without full target C99 function support. */
2646 if (fn == NULL_TREE)
2648 tree fntype;
2649 const char *name = NULL;
2651 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2652 name = "cexpf";
2653 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2654 name = "cexp";
2655 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2656 name = "cexpl";
2658 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2659 fn = build_fn_decl (name, fntype);
2662 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2663 build_real (type, dconst0), arg);
2665 /* Make sure not to fold the cexp call again. */
2666 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2667 return expand_expr (build_call_nary (ctype, call, 1, narg),
2668 target, VOIDmode, EXPAND_NORMAL);
2671 /* Now build the proper return type. */
2672 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2673 make_tree (TREE_TYPE (arg), op2),
2674 make_tree (TREE_TYPE (arg), op1)),
2675 target, VOIDmode, EXPAND_NORMAL);
2678 /* Conveniently construct a function call expression. FNDECL names the
2679 function to be called, N is the number of arguments, and the "..."
2680 parameters are the argument expressions. Unlike build_call_exr
2681 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2683 static tree
2684 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2686 va_list ap;
2687 tree fntype = TREE_TYPE (fndecl);
2688 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2690 va_start (ap, n);
2691 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2692 va_end (ap);
2693 SET_EXPR_LOCATION (fn, loc);
2694 return fn;
2697 /* Expand a call to one of the builtin rounding functions gcc defines
2698 as an extension (lfloor and lceil). As these are gcc extensions we
2699 do not need to worry about setting errno to EDOM.
2700 If expanding via optab fails, lower expression to (int)(floor(x)).
2701 EXP is the expression that is a call to the builtin function;
2702 if convenient, the result should be placed in TARGET. */
2704 static rtx
2705 expand_builtin_int_roundingfn (tree exp, rtx target)
2707 convert_optab builtin_optab;
2708 rtx op0, insns, tmp;
2709 tree fndecl = get_callee_fndecl (exp);
2710 enum built_in_function fallback_fn;
2711 tree fallback_fndecl;
2712 enum machine_mode mode;
2713 tree arg;
2715 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2716 gcc_unreachable ();
2718 arg = CALL_EXPR_ARG (exp, 0);
2720 switch (DECL_FUNCTION_CODE (fndecl))
2722 CASE_FLT_FN (BUILT_IN_ICEIL):
2723 CASE_FLT_FN (BUILT_IN_LCEIL):
2724 CASE_FLT_FN (BUILT_IN_LLCEIL):
2725 builtin_optab = lceil_optab;
2726 fallback_fn = BUILT_IN_CEIL;
2727 break;
2729 CASE_FLT_FN (BUILT_IN_IFLOOR):
2730 CASE_FLT_FN (BUILT_IN_LFLOOR):
2731 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2732 builtin_optab = lfloor_optab;
2733 fallback_fn = BUILT_IN_FLOOR;
2734 break;
2736 default:
2737 gcc_unreachable ();
2740 /* Make a suitable register to place result in. */
2741 mode = TYPE_MODE (TREE_TYPE (exp));
2743 target = gen_reg_rtx (mode);
2745 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2746 need to expand the argument again. This way, we will not perform
2747 side-effects more the once. */
2748 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2750 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2752 start_sequence ();
2754 /* Compute into TARGET. */
2755 if (expand_sfix_optab (target, op0, builtin_optab))
2757 /* Output the entire sequence. */
2758 insns = get_insns ();
2759 end_sequence ();
2760 emit_insn (insns);
2761 return target;
2764 /* If we were unable to expand via the builtin, stop the sequence
2765 (without outputting the insns). */
2766 end_sequence ();
2768 /* Fall back to floating point rounding optab. */
2769 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2771 /* For non-C99 targets we may end up without a fallback fndecl here
2772 if the user called __builtin_lfloor directly. In this case emit
2773 a call to the floor/ceil variants nevertheless. This should result
2774 in the best user experience for not full C99 targets. */
2775 if (fallback_fndecl == NULL_TREE)
2777 tree fntype;
2778 const char *name = NULL;
2780 switch (DECL_FUNCTION_CODE (fndecl))
2782 case BUILT_IN_ICEIL:
2783 case BUILT_IN_LCEIL:
2784 case BUILT_IN_LLCEIL:
2785 name = "ceil";
2786 break;
2787 case BUILT_IN_ICEILF:
2788 case BUILT_IN_LCEILF:
2789 case BUILT_IN_LLCEILF:
2790 name = "ceilf";
2791 break;
2792 case BUILT_IN_ICEILL:
2793 case BUILT_IN_LCEILL:
2794 case BUILT_IN_LLCEILL:
2795 name = "ceill";
2796 break;
2797 case BUILT_IN_IFLOOR:
2798 case BUILT_IN_LFLOOR:
2799 case BUILT_IN_LLFLOOR:
2800 name = "floor";
2801 break;
2802 case BUILT_IN_IFLOORF:
2803 case BUILT_IN_LFLOORF:
2804 case BUILT_IN_LLFLOORF:
2805 name = "floorf";
2806 break;
2807 case BUILT_IN_IFLOORL:
2808 case BUILT_IN_LFLOORL:
2809 case BUILT_IN_LLFLOORL:
2810 name = "floorl";
2811 break;
2812 default:
2813 gcc_unreachable ();
2816 fntype = build_function_type_list (TREE_TYPE (arg),
2817 TREE_TYPE (arg), NULL_TREE);
2818 fallback_fndecl = build_fn_decl (name, fntype);
2821 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2823 tmp = expand_normal (exp);
2824 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2826 /* Truncate the result of floating point optab to integer
2827 via expand_fix (). */
2828 target = gen_reg_rtx (mode);
2829 expand_fix (target, tmp, 0);
2831 return target;
2834 /* Expand a call to one of the builtin math functions doing integer
2835 conversion (lrint).
2836 Return 0 if a normal call should be emitted rather than expanding the
2837 function in-line. EXP is the expression that is a call to the builtin
2838 function; if convenient, the result should be placed in TARGET. */
2840 static rtx
2841 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2843 convert_optab builtin_optab;
2844 rtx op0, insns;
2845 tree fndecl = get_callee_fndecl (exp);
2846 tree arg;
2847 enum machine_mode mode;
2848 enum built_in_function fallback_fn = BUILT_IN_NONE;
2850 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2851 gcc_unreachable ();
2853 arg = CALL_EXPR_ARG (exp, 0);
2855 switch (DECL_FUNCTION_CODE (fndecl))
2857 CASE_FLT_FN (BUILT_IN_IRINT):
2858 fallback_fn = BUILT_IN_LRINT;
2859 /* FALLTHRU */
2860 CASE_FLT_FN (BUILT_IN_LRINT):
2861 CASE_FLT_FN (BUILT_IN_LLRINT):
2862 builtin_optab = lrint_optab;
2863 break;
2865 CASE_FLT_FN (BUILT_IN_IROUND):
2866 fallback_fn = BUILT_IN_LROUND;
2867 /* FALLTHRU */
2868 CASE_FLT_FN (BUILT_IN_LROUND):
2869 CASE_FLT_FN (BUILT_IN_LLROUND):
2870 builtin_optab = lround_optab;
2871 break;
2873 default:
2874 gcc_unreachable ();
2877 /* There's no easy way to detect the case we need to set EDOM. */
2878 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2879 return NULL_RTX;
2881 /* Make a suitable register to place result in. */
2882 mode = TYPE_MODE (TREE_TYPE (exp));
2884 /* There's no easy way to detect the case we need to set EDOM. */
2885 if (!flag_errno_math)
2887 rtx result = gen_reg_rtx (mode);
2889 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2890 need to expand the argument again. This way, we will not perform
2891 side-effects more the once. */
2892 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2894 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2896 start_sequence ();
2898 if (expand_sfix_optab (result, op0, builtin_optab))
2900 /* Output the entire sequence. */
2901 insns = get_insns ();
2902 end_sequence ();
2903 emit_insn (insns);
2904 return result;
2907 /* If we were unable to expand via the builtin, stop the sequence
2908 (without outputting the insns) and call to the library function
2909 with the stabilized argument list. */
2910 end_sequence ();
2913 if (fallback_fn != BUILT_IN_NONE)
2915 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2916 targets, (int) round (x) should never be transformed into
2917 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2918 a call to lround in the hope that the target provides at least some
2919 C99 functions. This should result in the best user experience for
2920 not full C99 targets. */
2921 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2922 fallback_fn, 0);
2924 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2925 fallback_fndecl, 1, arg);
2927 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2928 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2929 return convert_to_mode (mode, target, 0);
2932 return expand_call (exp, target, target == const0_rtx);
2935 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2936 a normal call should be emitted rather than expanding the function
2937 in-line. EXP is the expression that is a call to the builtin
2938 function; if convenient, the result should be placed in TARGET. */
2940 static rtx
2941 expand_builtin_powi (tree exp, rtx target)
2943 tree arg0, arg1;
2944 rtx op0, op1;
2945 enum machine_mode mode;
2946 enum machine_mode mode2;
2948 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2949 return NULL_RTX;
2951 arg0 = CALL_EXPR_ARG (exp, 0);
2952 arg1 = CALL_EXPR_ARG (exp, 1);
2953 mode = TYPE_MODE (TREE_TYPE (exp));
2955 /* Emit a libcall to libgcc. */
2957 /* Mode of the 2nd argument must match that of an int. */
2958 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2960 if (target == NULL_RTX)
2961 target = gen_reg_rtx (mode);
2963 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2964 if (GET_MODE (op0) != mode)
2965 op0 = convert_to_mode (mode, op0, 0);
2966 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2967 if (GET_MODE (op1) != mode2)
2968 op1 = convert_to_mode (mode2, op1, 0);
2970 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2971 target, LCT_CONST, mode, 2,
2972 op0, mode, op1, mode2);
2974 return target;
2977 /* Expand expression EXP which is a call to the strlen builtin. Return
2978 NULL_RTX if we failed the caller should emit a normal call, otherwise
2979 try to get the result in TARGET, if convenient. */
2981 static rtx
2982 expand_builtin_strlen (tree exp, rtx target,
2983 enum machine_mode target_mode)
2985 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2986 return NULL_RTX;
2987 else
2989 struct expand_operand ops[4];
2990 rtx pat;
2991 tree len;
2992 tree src = CALL_EXPR_ARG (exp, 0);
2993 rtx src_reg, before_strlen;
2994 enum machine_mode insn_mode = target_mode;
2995 enum insn_code icode = CODE_FOR_nothing;
2996 unsigned int align;
2998 /* If the length can be computed at compile-time, return it. */
2999 len = c_strlen (src, 0);
3000 if (len)
3001 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3003 /* If the length can be computed at compile-time and is constant
3004 integer, but there are side-effects in src, evaluate
3005 src for side-effects, then return len.
3006 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3007 can be optimized into: i++; x = 3; */
3008 len = c_strlen (src, 1);
3009 if (len && TREE_CODE (len) == INTEGER_CST)
3011 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3012 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3015 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3017 /* If SRC is not a pointer type, don't do this operation inline. */
3018 if (align == 0)
3019 return NULL_RTX;
3021 /* Bail out if we can't compute strlen in the right mode. */
3022 while (insn_mode != VOIDmode)
3024 icode = optab_handler (strlen_optab, insn_mode);
3025 if (icode != CODE_FOR_nothing)
3026 break;
3028 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3030 if (insn_mode == VOIDmode)
3031 return NULL_RTX;
3033 /* Make a place to hold the source address. We will not expand
3034 the actual source until we are sure that the expansion will
3035 not fail -- there are trees that cannot be expanded twice. */
3036 src_reg = gen_reg_rtx (Pmode);
3038 /* Mark the beginning of the strlen sequence so we can emit the
3039 source operand later. */
3040 before_strlen = get_last_insn ();
3042 create_output_operand (&ops[0], target, insn_mode);
3043 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3044 create_integer_operand (&ops[2], 0);
3045 create_integer_operand (&ops[3], align);
3046 if (!maybe_expand_insn (icode, 4, ops))
3047 return NULL_RTX;
3049 /* Now that we are assured of success, expand the source. */
3050 start_sequence ();
3051 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3052 if (pat != src_reg)
3054 #ifdef POINTERS_EXTEND_UNSIGNED
3055 if (GET_MODE (pat) != Pmode)
3056 pat = convert_to_mode (Pmode, pat,
3057 POINTERS_EXTEND_UNSIGNED);
3058 #endif
3059 emit_move_insn (src_reg, pat);
3061 pat = get_insns ();
3062 end_sequence ();
3064 if (before_strlen)
3065 emit_insn_after (pat, before_strlen);
3066 else
3067 emit_insn_before (pat, get_insns ());
3069 /* Return the value in the proper mode for this function. */
3070 if (GET_MODE (ops[0].value) == target_mode)
3071 target = ops[0].value;
3072 else if (target != 0)
3073 convert_move (target, ops[0].value, 0);
3074 else
3075 target = convert_to_mode (target_mode, ops[0].value, 0);
3077 return target;
3081 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3082 bytes from constant string DATA + OFFSET and return it as target
3083 constant. */
3085 static rtx
3086 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3087 enum machine_mode mode)
3089 const char *str = (const char *) data;
3091 gcc_assert (offset >= 0
3092 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3093 <= strlen (str) + 1));
3095 return c_readstr (str + offset, mode);
3098 /* LEN specify length of the block of memcpy/memset operation.
3099 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3100 In some cases we can make very likely guess on max size, then we
3101 set it into PROBABLE_MAX_SIZE. */
3103 static void
3104 determine_block_size (tree len, rtx len_rtx,
3105 unsigned HOST_WIDE_INT *min_size,
3106 unsigned HOST_WIDE_INT *max_size,
3107 unsigned HOST_WIDE_INT *probable_max_size)
3109 if (CONST_INT_P (len_rtx))
3111 *min_size = *max_size = UINTVAL (len_rtx);
3112 return;
3114 else
3116 double_int min, max;
3117 enum value_range_type range_type = VR_UNDEFINED;
3119 /* Determine bounds from the type. */
3120 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3121 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3122 else
3123 *min_size = 0;
3124 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3125 *probable_max_size = *max_size = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3126 else
3127 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3129 if (TREE_CODE (len) == SSA_NAME)
3130 range_type = get_range_info (len, &min, &max);
3131 if (range_type == VR_RANGE)
3133 if (min.fits_uhwi () && *min_size < min.to_uhwi ())
3134 *min_size = min.to_uhwi ();
3135 if (max.fits_uhwi () && *max_size > max.to_uhwi ())
3136 *probable_max_size = *max_size = max.to_uhwi ();
3138 else if (range_type == VR_ANTI_RANGE)
3140 /* Anti range 0...N lets us to determine minmal size to N+1. */
3141 if (min.is_zero ())
3143 if ((max + double_int_one).fits_uhwi ())
3144 *min_size = (max + double_int_one).to_uhwi ();
3146 /* Code like
3148 int n;
3149 if (n < 100)
3150 memcpy (a,b, n)
3152 Produce anti range allowing negative values of N. We still
3153 can use the information and make a guess that N is not negative.
3155 else if (!max.ule (double_int_one.lshift (30))
3156 && min.fits_uhwi ())
3157 *probable_max_size = min.to_uhwi () - 1;
3160 gcc_checking_assert (*max_size <=
3161 (unsigned HOST_WIDE_INT)
3162 GET_MODE_MASK (GET_MODE (len_rtx)));
3165 /* Expand a call EXP to the memcpy builtin.
3166 Return NULL_RTX if we failed, the caller should emit a normal call,
3167 otherwise try to get the result in TARGET, if convenient (and in
3168 mode MODE if that's convenient). */
3170 static rtx
3171 expand_builtin_memcpy (tree exp, rtx target)
3173 if (!validate_arglist (exp,
3174 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3175 return NULL_RTX;
3176 else
3178 tree dest = CALL_EXPR_ARG (exp, 0);
3179 tree src = CALL_EXPR_ARG (exp, 1);
3180 tree len = CALL_EXPR_ARG (exp, 2);
3181 const char *src_str;
3182 unsigned int src_align = get_pointer_alignment (src);
3183 unsigned int dest_align = get_pointer_alignment (dest);
3184 rtx dest_mem, src_mem, dest_addr, len_rtx;
3185 HOST_WIDE_INT expected_size = -1;
3186 unsigned int expected_align = 0;
3187 unsigned HOST_WIDE_INT min_size;
3188 unsigned HOST_WIDE_INT max_size;
3189 unsigned HOST_WIDE_INT probable_max_size;
3191 /* If DEST is not a pointer type, call the normal function. */
3192 if (dest_align == 0)
3193 return NULL_RTX;
3195 /* If either SRC is not a pointer type, don't do this
3196 operation in-line. */
3197 if (src_align == 0)
3198 return NULL_RTX;
3200 if (currently_expanding_gimple_stmt)
3201 stringop_block_profile (currently_expanding_gimple_stmt,
3202 &expected_align, &expected_size);
3204 if (expected_align < dest_align)
3205 expected_align = dest_align;
3206 dest_mem = get_memory_rtx (dest, len);
3207 set_mem_align (dest_mem, dest_align);
3208 len_rtx = expand_normal (len);
3209 determine_block_size (len, len_rtx, &min_size, &max_size,
3210 &probable_max_size);
3211 src_str = c_getstr (src);
3213 /* If SRC is a string constant and block move would be done
3214 by pieces, we can avoid loading the string from memory
3215 and only stored the computed constants. */
3216 if (src_str
3217 && CONST_INT_P (len_rtx)
3218 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3219 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3220 CONST_CAST (char *, src_str),
3221 dest_align, false))
3223 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3224 builtin_memcpy_read_str,
3225 CONST_CAST (char *, src_str),
3226 dest_align, false, 0);
3227 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3228 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3229 return dest_mem;
3232 src_mem = get_memory_rtx (src, len);
3233 set_mem_align (src_mem, src_align);
3235 /* Copy word part most expediently. */
3236 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3237 CALL_EXPR_TAILCALL (exp)
3238 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3239 expected_align, expected_size,
3240 min_size, max_size, probable_max_size);
3242 if (dest_addr == 0)
3244 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3245 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3247 return dest_addr;
3251 /* Expand a call EXP to the mempcpy builtin.
3252 Return NULL_RTX if we failed; the caller should emit a normal call,
3253 otherwise try to get the result in TARGET, if convenient (and in
3254 mode MODE if that's convenient). If ENDP is 0 return the
3255 destination pointer, if ENDP is 1 return the end pointer ala
3256 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3257 stpcpy. */
3259 static rtx
3260 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3262 if (!validate_arglist (exp,
3263 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3264 return NULL_RTX;
3265 else
3267 tree dest = CALL_EXPR_ARG (exp, 0);
3268 tree src = CALL_EXPR_ARG (exp, 1);
3269 tree len = CALL_EXPR_ARG (exp, 2);
3270 return expand_builtin_mempcpy_args (dest, src, len,
3271 target, mode, /*endp=*/ 1);
3275 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3276 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3277 so that this can also be called without constructing an actual CALL_EXPR.
3278 The other arguments and return value are the same as for
3279 expand_builtin_mempcpy. */
3281 static rtx
3282 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3283 rtx target, enum machine_mode mode, int endp)
3285 /* If return value is ignored, transform mempcpy into memcpy. */
3286 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3288 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3289 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3290 dest, src, len);
3291 return expand_expr (result, target, mode, EXPAND_NORMAL);
3293 else
3295 const char *src_str;
3296 unsigned int src_align = get_pointer_alignment (src);
3297 unsigned int dest_align = get_pointer_alignment (dest);
3298 rtx dest_mem, src_mem, len_rtx;
3300 /* If either SRC or DEST is not a pointer type, don't do this
3301 operation in-line. */
3302 if (dest_align == 0 || src_align == 0)
3303 return NULL_RTX;
3305 /* If LEN is not constant, call the normal function. */
3306 if (! tree_fits_uhwi_p (len))
3307 return NULL_RTX;
3309 len_rtx = expand_normal (len);
3310 src_str = c_getstr (src);
3312 /* If SRC is a string constant and block move would be done
3313 by pieces, we can avoid loading the string from memory
3314 and only stored the computed constants. */
3315 if (src_str
3316 && CONST_INT_P (len_rtx)
3317 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3318 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3319 CONST_CAST (char *, src_str),
3320 dest_align, false))
3322 dest_mem = get_memory_rtx (dest, len);
3323 set_mem_align (dest_mem, dest_align);
3324 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3325 builtin_memcpy_read_str,
3326 CONST_CAST (char *, src_str),
3327 dest_align, false, endp);
3328 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3329 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3330 return dest_mem;
3333 if (CONST_INT_P (len_rtx)
3334 && can_move_by_pieces (INTVAL (len_rtx),
3335 MIN (dest_align, src_align)))
3337 dest_mem = get_memory_rtx (dest, len);
3338 set_mem_align (dest_mem, dest_align);
3339 src_mem = get_memory_rtx (src, len);
3340 set_mem_align (src_mem, src_align);
3341 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3342 MIN (dest_align, src_align), endp);
3343 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3344 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3345 return dest_mem;
3348 return NULL_RTX;
3352 #ifndef HAVE_movstr
3353 # define HAVE_movstr 0
3354 # define CODE_FOR_movstr CODE_FOR_nothing
3355 #endif
3357 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3358 we failed, the caller should emit a normal call, otherwise try to
3359 get the result in TARGET, if convenient. If ENDP is 0 return the
3360 destination pointer, if ENDP is 1 return the end pointer ala
3361 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3362 stpcpy. */
3364 static rtx
3365 expand_movstr (tree dest, tree src, rtx target, int endp)
3367 struct expand_operand ops[3];
3368 rtx dest_mem;
3369 rtx src_mem;
3371 if (!HAVE_movstr)
3372 return NULL_RTX;
3374 dest_mem = get_memory_rtx (dest, NULL);
3375 src_mem = get_memory_rtx (src, NULL);
3376 if (!endp)
3378 target = force_reg (Pmode, XEXP (dest_mem, 0));
3379 dest_mem = replace_equiv_address (dest_mem, target);
3382 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3383 create_fixed_operand (&ops[1], dest_mem);
3384 create_fixed_operand (&ops[2], src_mem);
3385 expand_insn (CODE_FOR_movstr, 3, ops);
3387 if (endp && target != const0_rtx)
3389 target = ops[0].value;
3390 /* movstr is supposed to set end to the address of the NUL
3391 terminator. If the caller requested a mempcpy-like return value,
3392 adjust it. */
3393 if (endp == 1)
3395 rtx tem = plus_constant (GET_MODE (target),
3396 gen_lowpart (GET_MODE (target), target), 1);
3397 emit_move_insn (target, force_operand (tem, NULL_RTX));
3400 return target;
3403 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3404 NULL_RTX if we failed the caller should emit a normal call, otherwise
3405 try to get the result in TARGET, if convenient (and in mode MODE if that's
3406 convenient). */
3408 static rtx
3409 expand_builtin_strcpy (tree exp, rtx target)
3411 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3413 tree dest = CALL_EXPR_ARG (exp, 0);
3414 tree src = CALL_EXPR_ARG (exp, 1);
3415 return expand_builtin_strcpy_args (dest, src, target);
3417 return NULL_RTX;
3420 /* Helper function to do the actual work for expand_builtin_strcpy. The
3421 arguments to the builtin_strcpy call DEST and SRC are broken out
3422 so that this can also be called without constructing an actual CALL_EXPR.
3423 The other arguments and return value are the same as for
3424 expand_builtin_strcpy. */
3426 static rtx
3427 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3429 return expand_movstr (dest, src, target, /*endp=*/0);
3432 /* Expand a call EXP to the stpcpy builtin.
3433 Return NULL_RTX if we failed the caller should emit a normal call,
3434 otherwise try to get the result in TARGET, if convenient (and in
3435 mode MODE if that's convenient). */
3437 static rtx
3438 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3440 tree dst, src;
3441 location_t loc = EXPR_LOCATION (exp);
3443 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3444 return NULL_RTX;
3446 dst = CALL_EXPR_ARG (exp, 0);
3447 src = CALL_EXPR_ARG (exp, 1);
3449 /* If return value is ignored, transform stpcpy into strcpy. */
3450 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3452 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3453 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3454 return expand_expr (result, target, mode, EXPAND_NORMAL);
3456 else
3458 tree len, lenp1;
3459 rtx ret;
3461 /* Ensure we get an actual string whose length can be evaluated at
3462 compile-time, not an expression containing a string. This is
3463 because the latter will potentially produce pessimized code
3464 when used to produce the return value. */
3465 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3466 return expand_movstr (dst, src, target, /*endp=*/2);
3468 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3469 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3470 target, mode, /*endp=*/2);
3472 if (ret)
3473 return ret;
3475 if (TREE_CODE (len) == INTEGER_CST)
3477 rtx len_rtx = expand_normal (len);
3479 if (CONST_INT_P (len_rtx))
3481 ret = expand_builtin_strcpy_args (dst, src, target);
3483 if (ret)
3485 if (! target)
3487 if (mode != VOIDmode)
3488 target = gen_reg_rtx (mode);
3489 else
3490 target = gen_reg_rtx (GET_MODE (ret));
3492 if (GET_MODE (target) != GET_MODE (ret))
3493 ret = gen_lowpart (GET_MODE (target), ret);
3495 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3496 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3497 gcc_assert (ret);
3499 return target;
3504 return expand_movstr (dst, src, target, /*endp=*/2);
3508 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3509 bytes from constant string DATA + OFFSET and return it as target
3510 constant. */
3513 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3514 enum machine_mode mode)
3516 const char *str = (const char *) data;
3518 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3519 return const0_rtx;
3521 return c_readstr (str + offset, mode);
3524 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3525 NULL_RTX if we failed the caller should emit a normal call. */
3527 static rtx
3528 expand_builtin_strncpy (tree exp, rtx target)
3530 location_t loc = EXPR_LOCATION (exp);
3532 if (validate_arglist (exp,
3533 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3535 tree dest = CALL_EXPR_ARG (exp, 0);
3536 tree src = CALL_EXPR_ARG (exp, 1);
3537 tree len = CALL_EXPR_ARG (exp, 2);
3538 tree slen = c_strlen (src, 1);
3540 /* We must be passed a constant len and src parameter. */
3541 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3542 return NULL_RTX;
3544 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3546 /* We're required to pad with trailing zeros if the requested
3547 len is greater than strlen(s2)+1. In that case try to
3548 use store_by_pieces, if it fails, punt. */
3549 if (tree_int_cst_lt (slen, len))
3551 unsigned int dest_align = get_pointer_alignment (dest);
3552 const char *p = c_getstr (src);
3553 rtx dest_mem;
3555 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3556 || !can_store_by_pieces (tree_to_uhwi (len),
3557 builtin_strncpy_read_str,
3558 CONST_CAST (char *, p),
3559 dest_align, false))
3560 return NULL_RTX;
3562 dest_mem = get_memory_rtx (dest, len);
3563 store_by_pieces (dest_mem, tree_to_uhwi (len),
3564 builtin_strncpy_read_str,
3565 CONST_CAST (char *, p), dest_align, false, 0);
3566 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3567 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3568 return dest_mem;
3571 return NULL_RTX;
3574 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3575 bytes from constant string DATA + OFFSET and return it as target
3576 constant. */
3579 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3580 enum machine_mode mode)
3582 const char *c = (const char *) data;
3583 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3585 memset (p, *c, GET_MODE_SIZE (mode));
3587 return c_readstr (p, mode);
3590 /* Callback routine for store_by_pieces. Return the RTL of a register
3591 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3592 char value given in the RTL register data. For example, if mode is
3593 4 bytes wide, return the RTL for 0x01010101*data. */
3595 static rtx
3596 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3597 enum machine_mode mode)
3599 rtx target, coeff;
3600 size_t size;
3601 char *p;
3603 size = GET_MODE_SIZE (mode);
3604 if (size == 1)
3605 return (rtx) data;
3607 p = XALLOCAVEC (char, size);
3608 memset (p, 1, size);
3609 coeff = c_readstr (p, mode);
3611 target = convert_to_mode (mode, (rtx) data, 1);
3612 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3613 return force_reg (mode, target);
3616 /* Expand expression EXP, which is a call to the memset builtin. Return
3617 NULL_RTX if we failed the caller should emit a normal call, otherwise
3618 try to get the result in TARGET, if convenient (and in mode MODE if that's
3619 convenient). */
3621 static rtx
3622 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3624 if (!validate_arglist (exp,
3625 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3626 return NULL_RTX;
3627 else
3629 tree dest = CALL_EXPR_ARG (exp, 0);
3630 tree val = CALL_EXPR_ARG (exp, 1);
3631 tree len = CALL_EXPR_ARG (exp, 2);
3632 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3636 /* Helper function to do the actual work for expand_builtin_memset. The
3637 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3638 so that this can also be called without constructing an actual CALL_EXPR.
3639 The other arguments and return value are the same as for
3640 expand_builtin_memset. */
3642 static rtx
3643 expand_builtin_memset_args (tree dest, tree val, tree len,
3644 rtx target, enum machine_mode mode, tree orig_exp)
3646 tree fndecl, fn;
3647 enum built_in_function fcode;
3648 enum machine_mode val_mode;
3649 char c;
3650 unsigned int dest_align;
3651 rtx dest_mem, dest_addr, len_rtx;
3652 HOST_WIDE_INT expected_size = -1;
3653 unsigned int expected_align = 0;
3654 unsigned HOST_WIDE_INT min_size;
3655 unsigned HOST_WIDE_INT max_size;
3656 unsigned HOST_WIDE_INT probable_max_size;
3658 dest_align = get_pointer_alignment (dest);
3660 /* If DEST is not a pointer type, don't do this operation in-line. */
3661 if (dest_align == 0)
3662 return NULL_RTX;
3664 if (currently_expanding_gimple_stmt)
3665 stringop_block_profile (currently_expanding_gimple_stmt,
3666 &expected_align, &expected_size);
3668 if (expected_align < dest_align)
3669 expected_align = dest_align;
3671 /* If the LEN parameter is zero, return DEST. */
3672 if (integer_zerop (len))
3674 /* Evaluate and ignore VAL in case it has side-effects. */
3675 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3676 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3679 /* Stabilize the arguments in case we fail. */
3680 dest = builtin_save_expr (dest);
3681 val = builtin_save_expr (val);
3682 len = builtin_save_expr (len);
3684 len_rtx = expand_normal (len);
3685 determine_block_size (len, len_rtx, &min_size, &max_size,
3686 &probable_max_size);
3687 dest_mem = get_memory_rtx (dest, len);
3688 val_mode = TYPE_MODE (unsigned_char_type_node);
3690 if (TREE_CODE (val) != INTEGER_CST)
3692 rtx val_rtx;
3694 val_rtx = expand_normal (val);
3695 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3697 /* Assume that we can memset by pieces if we can store
3698 * the coefficients by pieces (in the required modes).
3699 * We can't pass builtin_memset_gen_str as that emits RTL. */
3700 c = 1;
3701 if (tree_fits_uhwi_p (len)
3702 && can_store_by_pieces (tree_to_uhwi (len),
3703 builtin_memset_read_str, &c, dest_align,
3704 true))
3706 val_rtx = force_reg (val_mode, val_rtx);
3707 store_by_pieces (dest_mem, tree_to_uhwi (len),
3708 builtin_memset_gen_str, val_rtx, dest_align,
3709 true, 0);
3711 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3712 dest_align, expected_align,
3713 expected_size, min_size, max_size,
3714 probable_max_size))
3715 goto do_libcall;
3717 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3718 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3719 return dest_mem;
3722 if (target_char_cast (val, &c))
3723 goto do_libcall;
3725 if (c)
3727 if (tree_fits_uhwi_p (len)
3728 && can_store_by_pieces (tree_to_uhwi (len),
3729 builtin_memset_read_str, &c, dest_align,
3730 true))
3731 store_by_pieces (dest_mem, tree_to_uhwi (len),
3732 builtin_memset_read_str, &c, dest_align, true, 0);
3733 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3734 gen_int_mode (c, val_mode),
3735 dest_align, expected_align,
3736 expected_size, min_size, max_size,
3737 probable_max_size))
3738 goto do_libcall;
3740 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3741 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3742 return dest_mem;
3745 set_mem_align (dest_mem, dest_align);
3746 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3747 CALL_EXPR_TAILCALL (orig_exp)
3748 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3749 expected_align, expected_size,
3750 min_size, max_size,
3751 probable_max_size);
3753 if (dest_addr == 0)
3755 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3756 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3759 return dest_addr;
3761 do_libcall:
3762 fndecl = get_callee_fndecl (orig_exp);
3763 fcode = DECL_FUNCTION_CODE (fndecl);
3764 if (fcode == BUILT_IN_MEMSET)
3765 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3766 dest, val, len);
3767 else if (fcode == BUILT_IN_BZERO)
3768 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3769 dest, len);
3770 else
3771 gcc_unreachable ();
3772 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3773 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3774 return expand_call (fn, target, target == const0_rtx);
3777 /* Expand expression EXP, which is a call to the bzero builtin. Return
3778 NULL_RTX if we failed the caller should emit a normal call. */
3780 static rtx
3781 expand_builtin_bzero (tree exp)
3783 tree dest, size;
3784 location_t loc = EXPR_LOCATION (exp);
3786 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3787 return NULL_RTX;
3789 dest = CALL_EXPR_ARG (exp, 0);
3790 size = CALL_EXPR_ARG (exp, 1);
3792 /* New argument list transforming bzero(ptr x, int y) to
3793 memset(ptr x, int 0, size_t y). This is done this way
3794 so that if it isn't expanded inline, we fallback to
3795 calling bzero instead of memset. */
3797 return expand_builtin_memset_args (dest, integer_zero_node,
3798 fold_convert_loc (loc,
3799 size_type_node, size),
3800 const0_rtx, VOIDmode, exp);
3803 /* Expand expression EXP, which is a call to the memcmp built-in function.
3804 Return NULL_RTX if we failed and the caller should emit a normal call,
3805 otherwise try to get the result in TARGET, if convenient (and in mode
3806 MODE, if that's convenient). */
3808 static rtx
3809 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3810 ATTRIBUTE_UNUSED enum machine_mode mode)
3812 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3814 if (!validate_arglist (exp,
3815 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3816 return NULL_RTX;
3818 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3819 implementing memcmp because it will stop if it encounters two
3820 zero bytes. */
3821 #if defined HAVE_cmpmemsi
3823 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3824 rtx result;
3825 rtx insn;
3826 tree arg1 = CALL_EXPR_ARG (exp, 0);
3827 tree arg2 = CALL_EXPR_ARG (exp, 1);
3828 tree len = CALL_EXPR_ARG (exp, 2);
3830 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3831 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3832 enum machine_mode insn_mode;
3834 if (HAVE_cmpmemsi)
3835 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3836 else
3837 return NULL_RTX;
3839 /* If we don't have POINTER_TYPE, call the function. */
3840 if (arg1_align == 0 || arg2_align == 0)
3841 return NULL_RTX;
3843 /* Make a place to write the result of the instruction. */
3844 result = target;
3845 if (! (result != 0
3846 && REG_P (result) && GET_MODE (result) == insn_mode
3847 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3848 result = gen_reg_rtx (insn_mode);
3850 arg1_rtx = get_memory_rtx (arg1, len);
3851 arg2_rtx = get_memory_rtx (arg2, len);
3852 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3854 /* Set MEM_SIZE as appropriate. */
3855 if (CONST_INT_P (arg3_rtx))
3857 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3858 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3861 if (HAVE_cmpmemsi)
3862 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3863 GEN_INT (MIN (arg1_align, arg2_align)));
3864 else
3865 gcc_unreachable ();
3867 if (insn)
3868 emit_insn (insn);
3869 else
3870 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3871 TYPE_MODE (integer_type_node), 3,
3872 XEXP (arg1_rtx, 0), Pmode,
3873 XEXP (arg2_rtx, 0), Pmode,
3874 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3875 TYPE_UNSIGNED (sizetype)),
3876 TYPE_MODE (sizetype));
3878 /* Return the value in the proper mode for this function. */
3879 mode = TYPE_MODE (TREE_TYPE (exp));
3880 if (GET_MODE (result) == mode)
3881 return result;
3882 else if (target != 0)
3884 convert_move (target, result, 0);
3885 return target;
3887 else
3888 return convert_to_mode (mode, result, 0);
3890 #endif /* HAVE_cmpmemsi. */
3892 return NULL_RTX;
3895 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3896 if we failed the caller should emit a normal call, otherwise try to get
3897 the result in TARGET, if convenient. */
3899 static rtx
3900 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3902 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3903 return NULL_RTX;
3905 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3906 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3907 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3909 rtx arg1_rtx, arg2_rtx;
3910 rtx result, insn = NULL_RTX;
3911 tree fndecl, fn;
3912 tree arg1 = CALL_EXPR_ARG (exp, 0);
3913 tree arg2 = CALL_EXPR_ARG (exp, 1);
3915 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3916 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3918 /* If we don't have POINTER_TYPE, call the function. */
3919 if (arg1_align == 0 || arg2_align == 0)
3920 return NULL_RTX;
3922 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3923 arg1 = builtin_save_expr (arg1);
3924 arg2 = builtin_save_expr (arg2);
3926 arg1_rtx = get_memory_rtx (arg1, NULL);
3927 arg2_rtx = get_memory_rtx (arg2, NULL);
3929 #ifdef HAVE_cmpstrsi
3930 /* Try to call cmpstrsi. */
3931 if (HAVE_cmpstrsi)
3933 enum machine_mode insn_mode
3934 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3936 /* Make a place to write the result of the instruction. */
3937 result = target;
3938 if (! (result != 0
3939 && REG_P (result) && GET_MODE (result) == insn_mode
3940 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3941 result = gen_reg_rtx (insn_mode);
3943 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3944 GEN_INT (MIN (arg1_align, arg2_align)));
3946 #endif
3947 #ifdef HAVE_cmpstrnsi
3948 /* Try to determine at least one length and call cmpstrnsi. */
3949 if (!insn && HAVE_cmpstrnsi)
3951 tree len;
3952 rtx arg3_rtx;
3954 enum machine_mode insn_mode
3955 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3956 tree len1 = c_strlen (arg1, 1);
3957 tree len2 = c_strlen (arg2, 1);
3959 if (len1)
3960 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3961 if (len2)
3962 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3964 /* If we don't have a constant length for the first, use the length
3965 of the second, if we know it. We don't require a constant for
3966 this case; some cost analysis could be done if both are available
3967 but neither is constant. For now, assume they're equally cheap,
3968 unless one has side effects. If both strings have constant lengths,
3969 use the smaller. */
3971 if (!len1)
3972 len = len2;
3973 else if (!len2)
3974 len = len1;
3975 else if (TREE_SIDE_EFFECTS (len1))
3976 len = len2;
3977 else if (TREE_SIDE_EFFECTS (len2))
3978 len = len1;
3979 else if (TREE_CODE (len1) != INTEGER_CST)
3980 len = len2;
3981 else if (TREE_CODE (len2) != INTEGER_CST)
3982 len = len1;
3983 else if (tree_int_cst_lt (len1, len2))
3984 len = len1;
3985 else
3986 len = len2;
3988 /* If both arguments have side effects, we cannot optimize. */
3989 if (!len || TREE_SIDE_EFFECTS (len))
3990 goto do_libcall;
3992 arg3_rtx = expand_normal (len);
3994 /* Make a place to write the result of the instruction. */
3995 result = target;
3996 if (! (result != 0
3997 && REG_P (result) && GET_MODE (result) == insn_mode
3998 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3999 result = gen_reg_rtx (insn_mode);
4001 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4002 GEN_INT (MIN (arg1_align, arg2_align)));
4004 #endif
4006 if (insn)
4008 enum machine_mode mode;
4009 emit_insn (insn);
4011 /* Return the value in the proper mode for this function. */
4012 mode = TYPE_MODE (TREE_TYPE (exp));
4013 if (GET_MODE (result) == mode)
4014 return result;
4015 if (target == 0)
4016 return convert_to_mode (mode, result, 0);
4017 convert_move (target, result, 0);
4018 return target;
4021 /* Expand the library call ourselves using a stabilized argument
4022 list to avoid re-evaluating the function's arguments twice. */
4023 #ifdef HAVE_cmpstrnsi
4024 do_libcall:
4025 #endif
4026 fndecl = get_callee_fndecl (exp);
4027 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4028 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4029 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4030 return expand_call (fn, target, target == const0_rtx);
4032 #endif
4033 return NULL_RTX;
4036 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4037 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4038 the result in TARGET, if convenient. */
4040 static rtx
4041 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4042 ATTRIBUTE_UNUSED enum machine_mode mode)
4044 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4046 if (!validate_arglist (exp,
4047 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4048 return NULL_RTX;
4050 /* If c_strlen can determine an expression for one of the string
4051 lengths, and it doesn't have side effects, then emit cmpstrnsi
4052 using length MIN(strlen(string)+1, arg3). */
4053 #ifdef HAVE_cmpstrnsi
4054 if (HAVE_cmpstrnsi)
4056 tree len, len1, len2;
4057 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4058 rtx result, insn;
4059 tree fndecl, fn;
4060 tree arg1 = CALL_EXPR_ARG (exp, 0);
4061 tree arg2 = CALL_EXPR_ARG (exp, 1);
4062 tree arg3 = CALL_EXPR_ARG (exp, 2);
4064 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4065 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4066 enum machine_mode insn_mode
4067 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4069 len1 = c_strlen (arg1, 1);
4070 len2 = c_strlen (arg2, 1);
4072 if (len1)
4073 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4074 if (len2)
4075 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4077 /* If we don't have a constant length for the first, use the length
4078 of the second, if we know it. We don't require a constant for
4079 this case; some cost analysis could be done if both are available
4080 but neither is constant. For now, assume they're equally cheap,
4081 unless one has side effects. If both strings have constant lengths,
4082 use the smaller. */
4084 if (!len1)
4085 len = len2;
4086 else if (!len2)
4087 len = len1;
4088 else if (TREE_SIDE_EFFECTS (len1))
4089 len = len2;
4090 else if (TREE_SIDE_EFFECTS (len2))
4091 len = len1;
4092 else if (TREE_CODE (len1) != INTEGER_CST)
4093 len = len2;
4094 else if (TREE_CODE (len2) != INTEGER_CST)
4095 len = len1;
4096 else if (tree_int_cst_lt (len1, len2))
4097 len = len1;
4098 else
4099 len = len2;
4101 /* If both arguments have side effects, we cannot optimize. */
4102 if (!len || TREE_SIDE_EFFECTS (len))
4103 return NULL_RTX;
4105 /* The actual new length parameter is MIN(len,arg3). */
4106 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4107 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4109 /* If we don't have POINTER_TYPE, call the function. */
4110 if (arg1_align == 0 || arg2_align == 0)
4111 return NULL_RTX;
4113 /* Make a place to write the result of the instruction. */
4114 result = target;
4115 if (! (result != 0
4116 && REG_P (result) && GET_MODE (result) == insn_mode
4117 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4118 result = gen_reg_rtx (insn_mode);
4120 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4121 arg1 = builtin_save_expr (arg1);
4122 arg2 = builtin_save_expr (arg2);
4123 len = builtin_save_expr (len);
4125 arg1_rtx = get_memory_rtx (arg1, len);
4126 arg2_rtx = get_memory_rtx (arg2, len);
4127 arg3_rtx = expand_normal (len);
4128 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4129 GEN_INT (MIN (arg1_align, arg2_align)));
4130 if (insn)
4132 emit_insn (insn);
4134 /* Return the value in the proper mode for this function. */
4135 mode = TYPE_MODE (TREE_TYPE (exp));
4136 if (GET_MODE (result) == mode)
4137 return result;
4138 if (target == 0)
4139 return convert_to_mode (mode, result, 0);
4140 convert_move (target, result, 0);
4141 return target;
4144 /* Expand the library call ourselves using a stabilized argument
4145 list to avoid re-evaluating the function's arguments twice. */
4146 fndecl = get_callee_fndecl (exp);
4147 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4148 arg1, arg2, len);
4149 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4150 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4151 return expand_call (fn, target, target == const0_rtx);
4153 #endif
4154 return NULL_RTX;
4157 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4158 if that's convenient. */
4161 expand_builtin_saveregs (void)
4163 rtx val, seq;
4165 /* Don't do __builtin_saveregs more than once in a function.
4166 Save the result of the first call and reuse it. */
4167 if (saveregs_value != 0)
4168 return saveregs_value;
4170 /* When this function is called, it means that registers must be
4171 saved on entry to this function. So we migrate the call to the
4172 first insn of this function. */
4174 start_sequence ();
4176 /* Do whatever the machine needs done in this case. */
4177 val = targetm.calls.expand_builtin_saveregs ();
4179 seq = get_insns ();
4180 end_sequence ();
4182 saveregs_value = val;
4184 /* Put the insns after the NOTE that starts the function. If this
4185 is inside a start_sequence, make the outer-level insn chain current, so
4186 the code is placed at the start of the function. */
4187 push_topmost_sequence ();
4188 emit_insn_after (seq, entry_of_function ());
4189 pop_topmost_sequence ();
4191 return val;
4194 /* Expand a call to __builtin_next_arg. */
4196 static rtx
4197 expand_builtin_next_arg (void)
4199 /* Checking arguments is already done in fold_builtin_next_arg
4200 that must be called before this function. */
4201 return expand_binop (ptr_mode, add_optab,
4202 crtl->args.internal_arg_pointer,
4203 crtl->args.arg_offset_rtx,
4204 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4207 /* Make it easier for the backends by protecting the valist argument
4208 from multiple evaluations. */
4210 static tree
4211 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4213 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4215 /* The current way of determining the type of valist is completely
4216 bogus. We should have the information on the va builtin instead. */
4217 if (!vatype)
4218 vatype = targetm.fn_abi_va_list (cfun->decl);
4220 if (TREE_CODE (vatype) == ARRAY_TYPE)
4222 if (TREE_SIDE_EFFECTS (valist))
4223 valist = save_expr (valist);
4225 /* For this case, the backends will be expecting a pointer to
4226 vatype, but it's possible we've actually been given an array
4227 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4228 So fix it. */
4229 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4231 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4232 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4235 else
4237 tree pt = build_pointer_type (vatype);
4239 if (! needs_lvalue)
4241 if (! TREE_SIDE_EFFECTS (valist))
4242 return valist;
4244 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4245 TREE_SIDE_EFFECTS (valist) = 1;
4248 if (TREE_SIDE_EFFECTS (valist))
4249 valist = save_expr (valist);
4250 valist = fold_build2_loc (loc, MEM_REF,
4251 vatype, valist, build_int_cst (pt, 0));
4254 return valist;
4257 /* The "standard" definition of va_list is void*. */
4259 tree
4260 std_build_builtin_va_list (void)
4262 return ptr_type_node;
4265 /* The "standard" abi va_list is va_list_type_node. */
4267 tree
4268 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4270 return va_list_type_node;
4273 /* The "standard" type of va_list is va_list_type_node. */
4275 tree
4276 std_canonical_va_list_type (tree type)
4278 tree wtype, htype;
4280 if (INDIRECT_REF_P (type))
4281 type = TREE_TYPE (type);
4282 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4283 type = TREE_TYPE (type);
4284 wtype = va_list_type_node;
4285 htype = type;
4286 /* Treat structure va_list types. */
4287 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4288 htype = TREE_TYPE (htype);
4289 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4291 /* If va_list is an array type, the argument may have decayed
4292 to a pointer type, e.g. by being passed to another function.
4293 In that case, unwrap both types so that we can compare the
4294 underlying records. */
4295 if (TREE_CODE (htype) == ARRAY_TYPE
4296 || POINTER_TYPE_P (htype))
4298 wtype = TREE_TYPE (wtype);
4299 htype = TREE_TYPE (htype);
4302 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4303 return va_list_type_node;
4305 return NULL_TREE;
4308 /* The "standard" implementation of va_start: just assign `nextarg' to
4309 the variable. */
4311 void
4312 std_expand_builtin_va_start (tree valist, rtx nextarg)
4314 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4315 convert_move (va_r, nextarg, 0);
4318 /* Expand EXP, a call to __builtin_va_start. */
4320 static rtx
4321 expand_builtin_va_start (tree exp)
4323 rtx nextarg;
4324 tree valist;
4325 location_t loc = EXPR_LOCATION (exp);
4327 if (call_expr_nargs (exp) < 2)
4329 error_at (loc, "too few arguments to function %<va_start%>");
4330 return const0_rtx;
4333 if (fold_builtin_next_arg (exp, true))
4334 return const0_rtx;
4336 nextarg = expand_builtin_next_arg ();
4337 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4339 if (targetm.expand_builtin_va_start)
4340 targetm.expand_builtin_va_start (valist, nextarg);
4341 else
4342 std_expand_builtin_va_start (valist, nextarg);
4344 return const0_rtx;
4347 /* Expand EXP, a call to __builtin_va_end. */
4349 static rtx
4350 expand_builtin_va_end (tree exp)
4352 tree valist = CALL_EXPR_ARG (exp, 0);
4354 /* Evaluate for side effects, if needed. I hate macros that don't
4355 do that. */
4356 if (TREE_SIDE_EFFECTS (valist))
4357 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4359 return const0_rtx;
4362 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4363 builtin rather than just as an assignment in stdarg.h because of the
4364 nastiness of array-type va_list types. */
4366 static rtx
4367 expand_builtin_va_copy (tree exp)
4369 tree dst, src, t;
4370 location_t loc = EXPR_LOCATION (exp);
4372 dst = CALL_EXPR_ARG (exp, 0);
4373 src = CALL_EXPR_ARG (exp, 1);
4375 dst = stabilize_va_list_loc (loc, dst, 1);
4376 src = stabilize_va_list_loc (loc, src, 0);
4378 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4380 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4382 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4383 TREE_SIDE_EFFECTS (t) = 1;
4384 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4386 else
4388 rtx dstb, srcb, size;
4390 /* Evaluate to pointers. */
4391 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4392 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4393 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4394 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4396 dstb = convert_memory_address (Pmode, dstb);
4397 srcb = convert_memory_address (Pmode, srcb);
4399 /* "Dereference" to BLKmode memories. */
4400 dstb = gen_rtx_MEM (BLKmode, dstb);
4401 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4402 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4403 srcb = gen_rtx_MEM (BLKmode, srcb);
4404 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4405 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4407 /* Copy. */
4408 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4411 return const0_rtx;
4414 /* Expand a call to one of the builtin functions __builtin_frame_address or
4415 __builtin_return_address. */
4417 static rtx
4418 expand_builtin_frame_address (tree fndecl, tree exp)
4420 /* The argument must be a nonnegative integer constant.
4421 It counts the number of frames to scan up the stack.
4422 The value is the return address saved in that frame. */
4423 if (call_expr_nargs (exp) == 0)
4424 /* Warning about missing arg was already issued. */
4425 return const0_rtx;
4426 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4428 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4429 error ("invalid argument to %<__builtin_frame_address%>");
4430 else
4431 error ("invalid argument to %<__builtin_return_address%>");
4432 return const0_rtx;
4434 else
4436 rtx tem
4437 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4438 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4440 /* Some ports cannot access arbitrary stack frames. */
4441 if (tem == NULL)
4443 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4444 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4445 else
4446 warning (0, "unsupported argument to %<__builtin_return_address%>");
4447 return const0_rtx;
4450 /* For __builtin_frame_address, return what we've got. */
4451 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4452 return tem;
4454 if (!REG_P (tem)
4455 && ! CONSTANT_P (tem))
4456 tem = copy_addr_to_reg (tem);
4457 return tem;
4461 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4462 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4463 is the same as for allocate_dynamic_stack_space. */
4465 static rtx
4466 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4468 rtx op0;
4469 rtx result;
4470 bool valid_arglist;
4471 unsigned int align;
4472 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4473 == BUILT_IN_ALLOCA_WITH_ALIGN);
4475 valid_arglist
4476 = (alloca_with_align
4477 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4478 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4480 if (!valid_arglist)
4481 return NULL_RTX;
4483 /* Compute the argument. */
4484 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4486 /* Compute the alignment. */
4487 align = (alloca_with_align
4488 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4489 : BIGGEST_ALIGNMENT);
4491 /* Allocate the desired space. */
4492 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4493 result = convert_memory_address (ptr_mode, result);
4495 return result;
4498 /* Expand a call to bswap builtin in EXP.
4499 Return NULL_RTX if a normal call should be emitted rather than expanding the
4500 function in-line. If convenient, the result should be placed in TARGET.
4501 SUBTARGET may be used as the target for computing one of EXP's operands. */
4503 static rtx
4504 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4505 rtx subtarget)
4507 tree arg;
4508 rtx op0;
4510 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4511 return NULL_RTX;
4513 arg = CALL_EXPR_ARG (exp, 0);
4514 op0 = expand_expr (arg,
4515 subtarget && GET_MODE (subtarget) == target_mode
4516 ? subtarget : NULL_RTX,
4517 target_mode, EXPAND_NORMAL);
4518 if (GET_MODE (op0) != target_mode)
4519 op0 = convert_to_mode (target_mode, op0, 1);
4521 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4523 gcc_assert (target);
4525 return convert_to_mode (target_mode, target, 1);
4528 /* Expand a call to a unary builtin in EXP.
4529 Return NULL_RTX if a normal call should be emitted rather than expanding the
4530 function in-line. If convenient, the result should be placed in TARGET.
4531 SUBTARGET may be used as the target for computing one of EXP's operands. */
4533 static rtx
4534 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4535 rtx subtarget, optab op_optab)
4537 rtx op0;
4539 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4540 return NULL_RTX;
4542 /* Compute the argument. */
4543 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4544 (subtarget
4545 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4546 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4547 VOIDmode, EXPAND_NORMAL);
4548 /* Compute op, into TARGET if possible.
4549 Set TARGET to wherever the result comes back. */
4550 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4551 op_optab, op0, target, op_optab != clrsb_optab);
4552 gcc_assert (target);
4554 return convert_to_mode (target_mode, target, 0);
4557 /* Expand a call to __builtin_expect. We just return our argument
4558 as the builtin_expect semantic should've been already executed by
4559 tree branch prediction pass. */
4561 static rtx
4562 expand_builtin_expect (tree exp, rtx target)
4564 tree arg;
4566 if (call_expr_nargs (exp) < 2)
4567 return const0_rtx;
4568 arg = CALL_EXPR_ARG (exp, 0);
4570 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4571 /* When guessing was done, the hints should be already stripped away. */
4572 gcc_assert (!flag_guess_branch_prob
4573 || optimize == 0 || seen_error ());
4574 return target;
4577 /* Expand a call to __builtin_assume_aligned. We just return our first
4578 argument as the builtin_assume_aligned semantic should've been already
4579 executed by CCP. */
4581 static rtx
4582 expand_builtin_assume_aligned (tree exp, rtx target)
4584 if (call_expr_nargs (exp) < 2)
4585 return const0_rtx;
4586 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4587 EXPAND_NORMAL);
4588 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4589 && (call_expr_nargs (exp) < 3
4590 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4591 return target;
4594 void
4595 expand_builtin_trap (void)
4597 #ifdef HAVE_trap
4598 if (HAVE_trap)
4600 rtx insn = emit_insn (gen_trap ());
4601 /* For trap insns when not accumulating outgoing args force
4602 REG_ARGS_SIZE note to prevent crossjumping of calls with
4603 different args sizes. */
4604 if (!ACCUMULATE_OUTGOING_ARGS)
4605 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4607 else
4608 #endif
4609 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4610 emit_barrier ();
4613 /* Expand a call to __builtin_unreachable. We do nothing except emit
4614 a barrier saying that control flow will not pass here.
4616 It is the responsibility of the program being compiled to ensure
4617 that control flow does never reach __builtin_unreachable. */
4618 static void
4619 expand_builtin_unreachable (void)
4621 emit_barrier ();
4624 /* Expand EXP, a call to fabs, fabsf or fabsl.
4625 Return NULL_RTX if a normal call should be emitted rather than expanding
4626 the function inline. If convenient, the result should be placed
4627 in TARGET. SUBTARGET may be used as the target for computing
4628 the operand. */
4630 static rtx
4631 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4633 enum machine_mode mode;
4634 tree arg;
4635 rtx op0;
4637 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4638 return NULL_RTX;
4640 arg = CALL_EXPR_ARG (exp, 0);
4641 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4642 mode = TYPE_MODE (TREE_TYPE (arg));
4643 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4644 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4647 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4648 Return NULL is a normal call should be emitted rather than expanding the
4649 function inline. If convenient, the result should be placed in TARGET.
4650 SUBTARGET may be used as the target for computing the operand. */
4652 static rtx
4653 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4655 rtx op0, op1;
4656 tree arg;
4658 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4659 return NULL_RTX;
4661 arg = CALL_EXPR_ARG (exp, 0);
4662 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4664 arg = CALL_EXPR_ARG (exp, 1);
4665 op1 = expand_normal (arg);
4667 return expand_copysign (op0, op1, target);
4670 /* Create a new constant string literal and return a char* pointer to it.
4671 The STRING_CST value is the LEN characters at STR. */
4672 tree
4673 build_string_literal (int len, const char *str)
4675 tree t, elem, index, type;
4677 t = build_string (len, str);
4678 elem = build_type_variant (char_type_node, 1, 0);
4679 index = build_index_type (size_int (len - 1));
4680 type = build_array_type (elem, index);
4681 TREE_TYPE (t) = type;
4682 TREE_CONSTANT (t) = 1;
4683 TREE_READONLY (t) = 1;
4684 TREE_STATIC (t) = 1;
4686 type = build_pointer_type (elem);
4687 t = build1 (ADDR_EXPR, type,
4688 build4 (ARRAY_REF, elem,
4689 t, integer_zero_node, NULL_TREE, NULL_TREE));
4690 return t;
4693 /* Expand a call to __builtin___clear_cache. */
4695 static rtx
4696 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4698 #ifndef HAVE_clear_cache
4699 #ifdef CLEAR_INSN_CACHE
4700 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4701 does something. Just do the default expansion to a call to
4702 __clear_cache(). */
4703 return NULL_RTX;
4704 #else
4705 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4706 does nothing. There is no need to call it. Do nothing. */
4707 return const0_rtx;
4708 #endif /* CLEAR_INSN_CACHE */
4709 #else
4710 /* We have a "clear_cache" insn, and it will handle everything. */
4711 tree begin, end;
4712 rtx begin_rtx, end_rtx;
4714 /* We must not expand to a library call. If we did, any
4715 fallback library function in libgcc that might contain a call to
4716 __builtin___clear_cache() would recurse infinitely. */
4717 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4719 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4720 return const0_rtx;
4723 if (HAVE_clear_cache)
4725 struct expand_operand ops[2];
4727 begin = CALL_EXPR_ARG (exp, 0);
4728 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4730 end = CALL_EXPR_ARG (exp, 1);
4731 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4733 create_address_operand (&ops[0], begin_rtx);
4734 create_address_operand (&ops[1], end_rtx);
4735 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4736 return const0_rtx;
4738 return const0_rtx;
4739 #endif /* HAVE_clear_cache */
4742 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4744 static rtx
4745 round_trampoline_addr (rtx tramp)
4747 rtx temp, addend, mask;
4749 /* If we don't need too much alignment, we'll have been guaranteed
4750 proper alignment by get_trampoline_type. */
4751 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4752 return tramp;
4754 /* Round address up to desired boundary. */
4755 temp = gen_reg_rtx (Pmode);
4756 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4757 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4759 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4760 temp, 0, OPTAB_LIB_WIDEN);
4761 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4762 temp, 0, OPTAB_LIB_WIDEN);
4764 return tramp;
4767 static rtx
4768 expand_builtin_init_trampoline (tree exp, bool onstack)
4770 tree t_tramp, t_func, t_chain;
4771 rtx m_tramp, r_tramp, r_chain, tmp;
4773 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4774 POINTER_TYPE, VOID_TYPE))
4775 return NULL_RTX;
4777 t_tramp = CALL_EXPR_ARG (exp, 0);
4778 t_func = CALL_EXPR_ARG (exp, 1);
4779 t_chain = CALL_EXPR_ARG (exp, 2);
4781 r_tramp = expand_normal (t_tramp);
4782 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4783 MEM_NOTRAP_P (m_tramp) = 1;
4785 /* If ONSTACK, the TRAMP argument should be the address of a field
4786 within the local function's FRAME decl. Either way, let's see if
4787 we can fill in the MEM_ATTRs for this memory. */
4788 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4789 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4791 /* Creator of a heap trampoline is responsible for making sure the
4792 address is aligned to at least STACK_BOUNDARY. Normally malloc
4793 will ensure this anyhow. */
4794 tmp = round_trampoline_addr (r_tramp);
4795 if (tmp != r_tramp)
4797 m_tramp = change_address (m_tramp, BLKmode, tmp);
4798 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4799 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4802 /* The FUNC argument should be the address of the nested function.
4803 Extract the actual function decl to pass to the hook. */
4804 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4805 t_func = TREE_OPERAND (t_func, 0);
4806 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4808 r_chain = expand_normal (t_chain);
4810 /* Generate insns to initialize the trampoline. */
4811 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4813 if (onstack)
4815 trampolines_created = 1;
4817 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4818 "trampoline generated for nested function %qD", t_func);
4821 return const0_rtx;
4824 static rtx
4825 expand_builtin_adjust_trampoline (tree exp)
4827 rtx tramp;
4829 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4830 return NULL_RTX;
4832 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4833 tramp = round_trampoline_addr (tramp);
4834 if (targetm.calls.trampoline_adjust_address)
4835 tramp = targetm.calls.trampoline_adjust_address (tramp);
4837 return tramp;
4840 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4841 function. The function first checks whether the back end provides
4842 an insn to implement signbit for the respective mode. If not, it
4843 checks whether the floating point format of the value is such that
4844 the sign bit can be extracted. If that is not the case, the
4845 function returns NULL_RTX to indicate that a normal call should be
4846 emitted rather than expanding the function in-line. EXP is the
4847 expression that is a call to the builtin function; if convenient,
4848 the result should be placed in TARGET. */
4849 static rtx
4850 expand_builtin_signbit (tree exp, rtx target)
4852 const struct real_format *fmt;
4853 enum machine_mode fmode, imode, rmode;
4854 tree arg;
4855 int word, bitpos;
4856 enum insn_code icode;
4857 rtx temp;
4858 location_t loc = EXPR_LOCATION (exp);
4860 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4861 return NULL_RTX;
4863 arg = CALL_EXPR_ARG (exp, 0);
4864 fmode = TYPE_MODE (TREE_TYPE (arg));
4865 rmode = TYPE_MODE (TREE_TYPE (exp));
4866 fmt = REAL_MODE_FORMAT (fmode);
4868 arg = builtin_save_expr (arg);
4870 /* Expand the argument yielding a RTX expression. */
4871 temp = expand_normal (arg);
4873 /* Check if the back end provides an insn that handles signbit for the
4874 argument's mode. */
4875 icode = optab_handler (signbit_optab, fmode);
4876 if (icode != CODE_FOR_nothing)
4878 rtx last = get_last_insn ();
4879 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4880 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4881 return target;
4882 delete_insns_since (last);
4885 /* For floating point formats without a sign bit, implement signbit
4886 as "ARG < 0.0". */
4887 bitpos = fmt->signbit_ro;
4888 if (bitpos < 0)
4890 /* But we can't do this if the format supports signed zero. */
4891 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4892 return NULL_RTX;
4894 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4895 build_real (TREE_TYPE (arg), dconst0));
4896 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4899 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4901 imode = int_mode_for_mode (fmode);
4902 if (imode == BLKmode)
4903 return NULL_RTX;
4904 temp = gen_lowpart (imode, temp);
4906 else
4908 imode = word_mode;
4909 /* Handle targets with different FP word orders. */
4910 if (FLOAT_WORDS_BIG_ENDIAN)
4911 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4912 else
4913 word = bitpos / BITS_PER_WORD;
4914 temp = operand_subword_force (temp, word, fmode);
4915 bitpos = bitpos % BITS_PER_WORD;
4918 /* Force the intermediate word_mode (or narrower) result into a
4919 register. This avoids attempting to create paradoxical SUBREGs
4920 of floating point modes below. */
4921 temp = force_reg (imode, temp);
4923 /* If the bitpos is within the "result mode" lowpart, the operation
4924 can be implement with a single bitwise AND. Otherwise, we need
4925 a right shift and an AND. */
4927 if (bitpos < GET_MODE_BITSIZE (rmode))
4929 double_int mask = double_int_zero.set_bit (bitpos);
4931 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4932 temp = gen_lowpart (rmode, temp);
4933 temp = expand_binop (rmode, and_optab, temp,
4934 immed_double_int_const (mask, rmode),
4935 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4937 else
4939 /* Perform a logical right shift to place the signbit in the least
4940 significant bit, then truncate the result to the desired mode
4941 and mask just this bit. */
4942 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4943 temp = gen_lowpart (rmode, temp);
4944 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4945 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4948 return temp;
4951 /* Expand fork or exec calls. TARGET is the desired target of the
4952 call. EXP is the call. FN is the
4953 identificator of the actual function. IGNORE is nonzero if the
4954 value is to be ignored. */
4956 static rtx
4957 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4959 tree id, decl;
4960 tree call;
4962 /* If we are not profiling, just call the function. */
4963 if (!profile_arc_flag)
4964 return NULL_RTX;
4966 /* Otherwise call the wrapper. This should be equivalent for the rest of
4967 compiler, so the code does not diverge, and the wrapper may run the
4968 code necessary for keeping the profiling sane. */
4970 switch (DECL_FUNCTION_CODE (fn))
4972 case BUILT_IN_FORK:
4973 id = get_identifier ("__gcov_fork");
4974 break;
4976 case BUILT_IN_EXECL:
4977 id = get_identifier ("__gcov_execl");
4978 break;
4980 case BUILT_IN_EXECV:
4981 id = get_identifier ("__gcov_execv");
4982 break;
4984 case BUILT_IN_EXECLP:
4985 id = get_identifier ("__gcov_execlp");
4986 break;
4988 case BUILT_IN_EXECLE:
4989 id = get_identifier ("__gcov_execle");
4990 break;
4992 case BUILT_IN_EXECVP:
4993 id = get_identifier ("__gcov_execvp");
4994 break;
4996 case BUILT_IN_EXECVE:
4997 id = get_identifier ("__gcov_execve");
4998 break;
5000 default:
5001 gcc_unreachable ();
5004 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5005 FUNCTION_DECL, id, TREE_TYPE (fn));
5006 DECL_EXTERNAL (decl) = 1;
5007 TREE_PUBLIC (decl) = 1;
5008 DECL_ARTIFICIAL (decl) = 1;
5009 TREE_NOTHROW (decl) = 1;
5010 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5011 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5012 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5013 return expand_call (call, target, ignore);
5018 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5019 the pointer in these functions is void*, the tree optimizers may remove
5020 casts. The mode computed in expand_builtin isn't reliable either, due
5021 to __sync_bool_compare_and_swap.
5023 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5024 group of builtins. This gives us log2 of the mode size. */
5026 static inline enum machine_mode
5027 get_builtin_sync_mode (int fcode_diff)
5029 /* The size is not negotiable, so ask not to get BLKmode in return
5030 if the target indicates that a smaller size would be better. */
5031 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5034 /* Expand the memory expression LOC and return the appropriate memory operand
5035 for the builtin_sync operations. */
5037 static rtx
5038 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5040 rtx addr, mem;
5042 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5043 addr = convert_memory_address (Pmode, addr);
5045 /* Note that we explicitly do not want any alias information for this
5046 memory, so that we kill all other live memories. Otherwise we don't
5047 satisfy the full barrier semantics of the intrinsic. */
5048 mem = validize_mem (gen_rtx_MEM (mode, addr));
5050 /* The alignment needs to be at least according to that of the mode. */
5051 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5052 get_pointer_alignment (loc)));
5053 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5054 MEM_VOLATILE_P (mem) = 1;
5056 return mem;
5059 /* Make sure an argument is in the right mode.
5060 EXP is the tree argument.
5061 MODE is the mode it should be in. */
5063 static rtx
5064 expand_expr_force_mode (tree exp, enum machine_mode mode)
5066 rtx val;
5067 enum machine_mode old_mode;
5069 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5070 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5071 of CONST_INTs, where we know the old_mode only from the call argument. */
5073 old_mode = GET_MODE (val);
5074 if (old_mode == VOIDmode)
5075 old_mode = TYPE_MODE (TREE_TYPE (exp));
5076 val = convert_modes (mode, old_mode, val, 1);
5077 return val;
5081 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5082 EXP is the CALL_EXPR. CODE is the rtx code
5083 that corresponds to the arithmetic or logical operation from the name;
5084 an exception here is that NOT actually means NAND. TARGET is an optional
5085 place for us to store the results; AFTER is true if this is the
5086 fetch_and_xxx form. */
5088 static rtx
5089 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5090 enum rtx_code code, bool after,
5091 rtx target)
5093 rtx val, mem;
5094 location_t loc = EXPR_LOCATION (exp);
5096 if (code == NOT && warn_sync_nand)
5098 tree fndecl = get_callee_fndecl (exp);
5099 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5101 static bool warned_f_a_n, warned_n_a_f;
5103 switch (fcode)
5105 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5106 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5107 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5108 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5109 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5110 if (warned_f_a_n)
5111 break;
5113 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5114 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5115 warned_f_a_n = true;
5116 break;
5118 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5119 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5120 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5121 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5122 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5123 if (warned_n_a_f)
5124 break;
5126 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5127 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5128 warned_n_a_f = true;
5129 break;
5131 default:
5132 gcc_unreachable ();
5136 /* Expand the operands. */
5137 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5138 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5140 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5141 after);
5144 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5145 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5146 true if this is the boolean form. TARGET is a place for us to store the
5147 results; this is NOT optional if IS_BOOL is true. */
5149 static rtx
5150 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5151 bool is_bool, rtx target)
5153 rtx old_val, new_val, mem;
5154 rtx *pbool, *poval;
5156 /* Expand the operands. */
5157 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5158 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5159 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5161 pbool = poval = NULL;
5162 if (target != const0_rtx)
5164 if (is_bool)
5165 pbool = &target;
5166 else
5167 poval = &target;
5169 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5170 false, MEMMODEL_SEQ_CST,
5171 MEMMODEL_SEQ_CST))
5172 return NULL_RTX;
5174 return target;
5177 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5178 general form is actually an atomic exchange, and some targets only
5179 support a reduced form with the second argument being a constant 1.
5180 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5181 the results. */
5183 static rtx
5184 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5185 rtx target)
5187 rtx val, mem;
5189 /* Expand the operands. */
5190 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5191 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5193 return expand_sync_lock_test_and_set (target, mem, val);
5196 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5198 static void
5199 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5201 rtx mem;
5203 /* Expand the operands. */
5204 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5206 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5209 /* Given an integer representing an ``enum memmodel'', verify its
5210 correctness and return the memory model enum. */
5212 static enum memmodel
5213 get_memmodel (tree exp)
5215 rtx op;
5216 unsigned HOST_WIDE_INT val;
5218 /* If the parameter is not a constant, it's a run time value so we'll just
5219 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5220 if (TREE_CODE (exp) != INTEGER_CST)
5221 return MEMMODEL_SEQ_CST;
5223 op = expand_normal (exp);
5225 val = INTVAL (op);
5226 if (targetm.memmodel_check)
5227 val = targetm.memmodel_check (val);
5228 else if (val & ~MEMMODEL_MASK)
5230 warning (OPT_Winvalid_memory_model,
5231 "Unknown architecture specifier in memory model to builtin.");
5232 return MEMMODEL_SEQ_CST;
5235 if ((INTVAL (op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5237 warning (OPT_Winvalid_memory_model,
5238 "invalid memory model argument to builtin");
5239 return MEMMODEL_SEQ_CST;
5242 return (enum memmodel) val;
5245 /* Expand the __atomic_exchange intrinsic:
5246 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5247 EXP is the CALL_EXPR.
5248 TARGET is an optional place for us to store the results. */
5250 static rtx
5251 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5253 rtx val, mem;
5254 enum memmodel model;
5256 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5257 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5259 error ("invalid memory model for %<__atomic_exchange%>");
5260 return NULL_RTX;
5263 if (!flag_inline_atomics)
5264 return NULL_RTX;
5266 /* Expand the operands. */
5267 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5268 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5270 return expand_atomic_exchange (target, mem, val, model);
5273 /* Expand the __atomic_compare_exchange intrinsic:
5274 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5275 TYPE desired, BOOL weak,
5276 enum memmodel success,
5277 enum memmodel failure)
5278 EXP is the CALL_EXPR.
5279 TARGET is an optional place for us to store the results. */
5281 static rtx
5282 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5283 rtx target)
5285 rtx expect, desired, mem, oldval;
5286 enum memmodel success, failure;
5287 tree weak;
5288 bool is_weak;
5290 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5291 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5293 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5294 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5296 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5297 return NULL_RTX;
5300 if (failure > success)
5302 error ("failure memory model cannot be stronger than success "
5303 "memory model for %<__atomic_compare_exchange%>");
5304 return NULL_RTX;
5307 if (!flag_inline_atomics)
5308 return NULL_RTX;
5310 /* Expand the operands. */
5311 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5313 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5314 expect = convert_memory_address (Pmode, expect);
5315 expect = gen_rtx_MEM (mode, expect);
5316 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5318 weak = CALL_EXPR_ARG (exp, 3);
5319 is_weak = false;
5320 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5321 is_weak = true;
5323 oldval = expect;
5324 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5325 &oldval, mem, oldval, desired,
5326 is_weak, success, failure))
5327 return NULL_RTX;
5329 if (oldval != expect)
5330 emit_move_insn (expect, oldval);
5332 return target;
5335 /* Expand the __atomic_load intrinsic:
5336 TYPE __atomic_load (TYPE *object, enum memmodel)
5337 EXP is the CALL_EXPR.
5338 TARGET is an optional place for us to store the results. */
5340 static rtx
5341 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5343 rtx mem;
5344 enum memmodel model;
5346 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5347 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5348 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5350 error ("invalid memory model for %<__atomic_load%>");
5351 return NULL_RTX;
5354 if (!flag_inline_atomics)
5355 return NULL_RTX;
5357 /* Expand the operand. */
5358 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5360 return expand_atomic_load (target, mem, model);
5364 /* Expand the __atomic_store intrinsic:
5365 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5366 EXP is the CALL_EXPR.
5367 TARGET is an optional place for us to store the results. */
5369 static rtx
5370 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5372 rtx mem, val;
5373 enum memmodel model;
5375 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5376 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5377 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5378 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5380 error ("invalid memory model for %<__atomic_store%>");
5381 return NULL_RTX;
5384 if (!flag_inline_atomics)
5385 return NULL_RTX;
5387 /* Expand the operands. */
5388 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5389 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5391 return expand_atomic_store (mem, val, model, false);
5394 /* Expand the __atomic_fetch_XXX intrinsic:
5395 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5396 EXP is the CALL_EXPR.
5397 TARGET is an optional place for us to store the results.
5398 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5399 FETCH_AFTER is true if returning the result of the operation.
5400 FETCH_AFTER is false if returning the value before the operation.
5401 IGNORE is true if the result is not used.
5402 EXT_CALL is the correct builtin for an external call if this cannot be
5403 resolved to an instruction sequence. */
5405 static rtx
5406 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5407 enum rtx_code code, bool fetch_after,
5408 bool ignore, enum built_in_function ext_call)
5410 rtx val, mem, ret;
5411 enum memmodel model;
5412 tree fndecl;
5413 tree addr;
5415 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5417 /* Expand the operands. */
5418 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5419 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5421 /* Only try generating instructions if inlining is turned on. */
5422 if (flag_inline_atomics)
5424 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5425 if (ret)
5426 return ret;
5429 /* Return if a different routine isn't needed for the library call. */
5430 if (ext_call == BUILT_IN_NONE)
5431 return NULL_RTX;
5433 /* Change the call to the specified function. */
5434 fndecl = get_callee_fndecl (exp);
5435 addr = CALL_EXPR_FN (exp);
5436 STRIP_NOPS (addr);
5438 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5439 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5441 /* Expand the call here so we can emit trailing code. */
5442 ret = expand_call (exp, target, ignore);
5444 /* Replace the original function just in case it matters. */
5445 TREE_OPERAND (addr, 0) = fndecl;
5447 /* Then issue the arithmetic correction to return the right result. */
5448 if (!ignore)
5450 if (code == NOT)
5452 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5453 OPTAB_LIB_WIDEN);
5454 ret = expand_simple_unop (mode, NOT, ret, target, true);
5456 else
5457 ret = expand_simple_binop (mode, code, ret, val, target, true,
5458 OPTAB_LIB_WIDEN);
5460 return ret;
5464 #ifndef HAVE_atomic_clear
5465 # define HAVE_atomic_clear 0
5466 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5467 #endif
5469 /* Expand an atomic clear operation.
5470 void _atomic_clear (BOOL *obj, enum memmodel)
5471 EXP is the call expression. */
5473 static rtx
5474 expand_builtin_atomic_clear (tree exp)
5476 enum machine_mode mode;
5477 rtx mem, ret;
5478 enum memmodel model;
5480 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5481 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5482 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5484 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5485 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5487 error ("invalid memory model for %<__atomic_store%>");
5488 return const0_rtx;
5491 if (HAVE_atomic_clear)
5493 emit_insn (gen_atomic_clear (mem, model));
5494 return const0_rtx;
5497 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5498 Failing that, a store is issued by __atomic_store. The only way this can
5499 fail is if the bool type is larger than a word size. Unlikely, but
5500 handle it anyway for completeness. Assume a single threaded model since
5501 there is no atomic support in this case, and no barriers are required. */
5502 ret = expand_atomic_store (mem, const0_rtx, model, true);
5503 if (!ret)
5504 emit_move_insn (mem, const0_rtx);
5505 return const0_rtx;
5508 /* Expand an atomic test_and_set operation.
5509 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5510 EXP is the call expression. */
5512 static rtx
5513 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5515 rtx mem;
5516 enum memmodel model;
5517 enum machine_mode mode;
5519 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5520 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5521 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5523 return expand_atomic_test_and_set (target, mem, model);
5527 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5528 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5530 static tree
5531 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5533 int size;
5534 enum machine_mode mode;
5535 unsigned int mode_align, type_align;
5537 if (TREE_CODE (arg0) != INTEGER_CST)
5538 return NULL_TREE;
5540 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5541 mode = mode_for_size (size, MODE_INT, 0);
5542 mode_align = GET_MODE_ALIGNMENT (mode);
5544 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5545 type_align = mode_align;
5546 else
5548 tree ttype = TREE_TYPE (arg1);
5550 /* This function is usually invoked and folded immediately by the front
5551 end before anything else has a chance to look at it. The pointer
5552 parameter at this point is usually cast to a void *, so check for that
5553 and look past the cast. */
5554 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5555 && VOID_TYPE_P (TREE_TYPE (ttype)))
5556 arg1 = TREE_OPERAND (arg1, 0);
5558 ttype = TREE_TYPE (arg1);
5559 gcc_assert (POINTER_TYPE_P (ttype));
5561 /* Get the underlying type of the object. */
5562 ttype = TREE_TYPE (ttype);
5563 type_align = TYPE_ALIGN (ttype);
5566 /* If the object has smaller alignment, the the lock free routines cannot
5567 be used. */
5568 if (type_align < mode_align)
5569 return boolean_false_node;
5571 /* Check if a compare_and_swap pattern exists for the mode which represents
5572 the required size. The pattern is not allowed to fail, so the existence
5573 of the pattern indicates support is present. */
5574 if (can_compare_and_swap_p (mode, true))
5575 return boolean_true_node;
5576 else
5577 return boolean_false_node;
5580 /* Return true if the parameters to call EXP represent an object which will
5581 always generate lock free instructions. The first argument represents the
5582 size of the object, and the second parameter is a pointer to the object
5583 itself. If NULL is passed for the object, then the result is based on
5584 typical alignment for an object of the specified size. Otherwise return
5585 false. */
5587 static rtx
5588 expand_builtin_atomic_always_lock_free (tree exp)
5590 tree size;
5591 tree arg0 = CALL_EXPR_ARG (exp, 0);
5592 tree arg1 = CALL_EXPR_ARG (exp, 1);
5594 if (TREE_CODE (arg0) != INTEGER_CST)
5596 error ("non-constant argument 1 to __atomic_always_lock_free");
5597 return const0_rtx;
5600 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5601 if (size == boolean_true_node)
5602 return const1_rtx;
5603 return const0_rtx;
5606 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5607 is lock free on this architecture. */
5609 static tree
5610 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5612 if (!flag_inline_atomics)
5613 return NULL_TREE;
5615 /* If it isn't always lock free, don't generate a result. */
5616 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5617 return boolean_true_node;
5619 return NULL_TREE;
5622 /* Return true if the parameters to call EXP represent an object which will
5623 always generate lock free instructions. The first argument represents the
5624 size of the object, and the second parameter is a pointer to the object
5625 itself. If NULL is passed for the object, then the result is based on
5626 typical alignment for an object of the specified size. Otherwise return
5627 NULL*/
5629 static rtx
5630 expand_builtin_atomic_is_lock_free (tree exp)
5632 tree size;
5633 tree arg0 = CALL_EXPR_ARG (exp, 0);
5634 tree arg1 = CALL_EXPR_ARG (exp, 1);
5636 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5638 error ("non-integer argument 1 to __atomic_is_lock_free");
5639 return NULL_RTX;
5642 if (!flag_inline_atomics)
5643 return NULL_RTX;
5645 /* If the value is known at compile time, return the RTX for it. */
5646 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5647 if (size == boolean_true_node)
5648 return const1_rtx;
5650 return NULL_RTX;
5653 /* Expand the __atomic_thread_fence intrinsic:
5654 void __atomic_thread_fence (enum memmodel)
5655 EXP is the CALL_EXPR. */
5657 static void
5658 expand_builtin_atomic_thread_fence (tree exp)
5660 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5661 expand_mem_thread_fence (model);
5664 /* Expand the __atomic_signal_fence intrinsic:
5665 void __atomic_signal_fence (enum memmodel)
5666 EXP is the CALL_EXPR. */
5668 static void
5669 expand_builtin_atomic_signal_fence (tree exp)
5671 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5672 expand_mem_signal_fence (model);
5675 /* Expand the __sync_synchronize intrinsic. */
5677 static void
5678 expand_builtin_sync_synchronize (void)
5680 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5683 static rtx
5684 expand_builtin_thread_pointer (tree exp, rtx target)
5686 enum insn_code icode;
5687 if (!validate_arglist (exp, VOID_TYPE))
5688 return const0_rtx;
5689 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5690 if (icode != CODE_FOR_nothing)
5692 struct expand_operand op;
5693 if (!REG_P (target) || GET_MODE (target) != Pmode)
5694 target = gen_reg_rtx (Pmode);
5695 create_output_operand (&op, target, Pmode);
5696 expand_insn (icode, 1, &op);
5697 return target;
5699 error ("__builtin_thread_pointer is not supported on this target");
5700 return const0_rtx;
5703 static void
5704 expand_builtin_set_thread_pointer (tree exp)
5706 enum insn_code icode;
5707 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5708 return;
5709 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5710 if (icode != CODE_FOR_nothing)
5712 struct expand_operand op;
5713 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5714 Pmode, EXPAND_NORMAL);
5715 create_input_operand (&op, val, Pmode);
5716 expand_insn (icode, 1, &op);
5717 return;
5719 error ("__builtin_set_thread_pointer is not supported on this target");
5723 /* Emit code to restore the current value of stack. */
5725 static void
5726 expand_stack_restore (tree var)
5728 rtx prev, sa = expand_normal (var);
5730 sa = convert_memory_address (Pmode, sa);
5732 prev = get_last_insn ();
5733 emit_stack_restore (SAVE_BLOCK, sa);
5734 fixup_args_size_notes (prev, get_last_insn (), 0);
5738 /* Emit code to save the current value of stack. */
5740 static rtx
5741 expand_stack_save (void)
5743 rtx ret = NULL_RTX;
5745 do_pending_stack_adjust ();
5746 emit_stack_save (SAVE_BLOCK, &ret);
5747 return ret;
5750 /* Expand an expression EXP that calls a built-in function,
5751 with result going to TARGET if that's convenient
5752 (and in mode MODE if that's convenient).
5753 SUBTARGET may be used as the target for computing one of EXP's operands.
5754 IGNORE is nonzero if the value is to be ignored. */
5757 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5758 int ignore)
5760 tree fndecl = get_callee_fndecl (exp);
5761 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5762 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5763 int flags;
5765 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5766 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5768 /* When not optimizing, generate calls to library functions for a certain
5769 set of builtins. */
5770 if (!optimize
5771 && !called_as_built_in (fndecl)
5772 && fcode != BUILT_IN_FORK
5773 && fcode != BUILT_IN_EXECL
5774 && fcode != BUILT_IN_EXECV
5775 && fcode != BUILT_IN_EXECLP
5776 && fcode != BUILT_IN_EXECLE
5777 && fcode != BUILT_IN_EXECVP
5778 && fcode != BUILT_IN_EXECVE
5779 && fcode != BUILT_IN_ALLOCA
5780 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5781 && fcode != BUILT_IN_FREE
5782 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
5783 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
5784 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
5785 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
5786 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5787 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
5788 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
5789 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
5790 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
5791 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
5792 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND)
5793 return expand_call (exp, target, ignore);
5795 /* The built-in function expanders test for target == const0_rtx
5796 to determine whether the function's result will be ignored. */
5797 if (ignore)
5798 target = const0_rtx;
5800 /* If the result of a pure or const built-in function is ignored, and
5801 none of its arguments are volatile, we can avoid expanding the
5802 built-in call and just evaluate the arguments for side-effects. */
5803 if (target == const0_rtx
5804 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5805 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5807 bool volatilep = false;
5808 tree arg;
5809 call_expr_arg_iterator iter;
5811 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5812 if (TREE_THIS_VOLATILE (arg))
5814 volatilep = true;
5815 break;
5818 if (! volatilep)
5820 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5821 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5822 return const0_rtx;
5826 switch (fcode)
5828 CASE_FLT_FN (BUILT_IN_FABS):
5829 case BUILT_IN_FABSD32:
5830 case BUILT_IN_FABSD64:
5831 case BUILT_IN_FABSD128:
5832 target = expand_builtin_fabs (exp, target, subtarget);
5833 if (target)
5834 return target;
5835 break;
5837 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5838 target = expand_builtin_copysign (exp, target, subtarget);
5839 if (target)
5840 return target;
5841 break;
5843 /* Just do a normal library call if we were unable to fold
5844 the values. */
5845 CASE_FLT_FN (BUILT_IN_CABS):
5846 break;
5848 CASE_FLT_FN (BUILT_IN_EXP):
5849 CASE_FLT_FN (BUILT_IN_EXP10):
5850 CASE_FLT_FN (BUILT_IN_POW10):
5851 CASE_FLT_FN (BUILT_IN_EXP2):
5852 CASE_FLT_FN (BUILT_IN_EXPM1):
5853 CASE_FLT_FN (BUILT_IN_LOGB):
5854 CASE_FLT_FN (BUILT_IN_LOG):
5855 CASE_FLT_FN (BUILT_IN_LOG10):
5856 CASE_FLT_FN (BUILT_IN_LOG2):
5857 CASE_FLT_FN (BUILT_IN_LOG1P):
5858 CASE_FLT_FN (BUILT_IN_TAN):
5859 CASE_FLT_FN (BUILT_IN_ASIN):
5860 CASE_FLT_FN (BUILT_IN_ACOS):
5861 CASE_FLT_FN (BUILT_IN_ATAN):
5862 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5863 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5864 because of possible accuracy problems. */
5865 if (! flag_unsafe_math_optimizations)
5866 break;
5867 CASE_FLT_FN (BUILT_IN_SQRT):
5868 CASE_FLT_FN (BUILT_IN_FLOOR):
5869 CASE_FLT_FN (BUILT_IN_CEIL):
5870 CASE_FLT_FN (BUILT_IN_TRUNC):
5871 CASE_FLT_FN (BUILT_IN_ROUND):
5872 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5873 CASE_FLT_FN (BUILT_IN_RINT):
5874 target = expand_builtin_mathfn (exp, target, subtarget);
5875 if (target)
5876 return target;
5877 break;
5879 CASE_FLT_FN (BUILT_IN_FMA):
5880 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5881 if (target)
5882 return target;
5883 break;
5885 CASE_FLT_FN (BUILT_IN_ILOGB):
5886 if (! flag_unsafe_math_optimizations)
5887 break;
5888 CASE_FLT_FN (BUILT_IN_ISINF):
5889 CASE_FLT_FN (BUILT_IN_FINITE):
5890 case BUILT_IN_ISFINITE:
5891 case BUILT_IN_ISNORMAL:
5892 target = expand_builtin_interclass_mathfn (exp, target);
5893 if (target)
5894 return target;
5895 break;
5897 CASE_FLT_FN (BUILT_IN_ICEIL):
5898 CASE_FLT_FN (BUILT_IN_LCEIL):
5899 CASE_FLT_FN (BUILT_IN_LLCEIL):
5900 CASE_FLT_FN (BUILT_IN_LFLOOR):
5901 CASE_FLT_FN (BUILT_IN_IFLOOR):
5902 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5903 target = expand_builtin_int_roundingfn (exp, target);
5904 if (target)
5905 return target;
5906 break;
5908 CASE_FLT_FN (BUILT_IN_IRINT):
5909 CASE_FLT_FN (BUILT_IN_LRINT):
5910 CASE_FLT_FN (BUILT_IN_LLRINT):
5911 CASE_FLT_FN (BUILT_IN_IROUND):
5912 CASE_FLT_FN (BUILT_IN_LROUND):
5913 CASE_FLT_FN (BUILT_IN_LLROUND):
5914 target = expand_builtin_int_roundingfn_2 (exp, target);
5915 if (target)
5916 return target;
5917 break;
5919 CASE_FLT_FN (BUILT_IN_POWI):
5920 target = expand_builtin_powi (exp, target);
5921 if (target)
5922 return target;
5923 break;
5925 CASE_FLT_FN (BUILT_IN_ATAN2):
5926 CASE_FLT_FN (BUILT_IN_LDEXP):
5927 CASE_FLT_FN (BUILT_IN_SCALB):
5928 CASE_FLT_FN (BUILT_IN_SCALBN):
5929 CASE_FLT_FN (BUILT_IN_SCALBLN):
5930 if (! flag_unsafe_math_optimizations)
5931 break;
5933 CASE_FLT_FN (BUILT_IN_FMOD):
5934 CASE_FLT_FN (BUILT_IN_REMAINDER):
5935 CASE_FLT_FN (BUILT_IN_DREM):
5936 CASE_FLT_FN (BUILT_IN_POW):
5937 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5938 if (target)
5939 return target;
5940 break;
5942 CASE_FLT_FN (BUILT_IN_CEXPI):
5943 target = expand_builtin_cexpi (exp, target);
5944 gcc_assert (target);
5945 return target;
5947 CASE_FLT_FN (BUILT_IN_SIN):
5948 CASE_FLT_FN (BUILT_IN_COS):
5949 if (! flag_unsafe_math_optimizations)
5950 break;
5951 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5952 if (target)
5953 return target;
5954 break;
5956 CASE_FLT_FN (BUILT_IN_SINCOS):
5957 if (! flag_unsafe_math_optimizations)
5958 break;
5959 target = expand_builtin_sincos (exp);
5960 if (target)
5961 return target;
5962 break;
5964 case BUILT_IN_APPLY_ARGS:
5965 return expand_builtin_apply_args ();
5967 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5968 FUNCTION with a copy of the parameters described by
5969 ARGUMENTS, and ARGSIZE. It returns a block of memory
5970 allocated on the stack into which is stored all the registers
5971 that might possibly be used for returning the result of a
5972 function. ARGUMENTS is the value returned by
5973 __builtin_apply_args. ARGSIZE is the number of bytes of
5974 arguments that must be copied. ??? How should this value be
5975 computed? We'll also need a safe worst case value for varargs
5976 functions. */
5977 case BUILT_IN_APPLY:
5978 if (!validate_arglist (exp, POINTER_TYPE,
5979 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5980 && !validate_arglist (exp, REFERENCE_TYPE,
5981 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5982 return const0_rtx;
5983 else
5985 rtx ops[3];
5987 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5988 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5989 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5991 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5994 /* __builtin_return (RESULT) causes the function to return the
5995 value described by RESULT. RESULT is address of the block of
5996 memory returned by __builtin_apply. */
5997 case BUILT_IN_RETURN:
5998 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5999 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6000 return const0_rtx;
6002 case BUILT_IN_SAVEREGS:
6003 return expand_builtin_saveregs ();
6005 case BUILT_IN_VA_ARG_PACK:
6006 /* All valid uses of __builtin_va_arg_pack () are removed during
6007 inlining. */
6008 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6009 return const0_rtx;
6011 case BUILT_IN_VA_ARG_PACK_LEN:
6012 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6013 inlining. */
6014 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6015 return const0_rtx;
6017 /* Return the address of the first anonymous stack arg. */
6018 case BUILT_IN_NEXT_ARG:
6019 if (fold_builtin_next_arg (exp, false))
6020 return const0_rtx;
6021 return expand_builtin_next_arg ();
6023 case BUILT_IN_CLEAR_CACHE:
6024 target = expand_builtin___clear_cache (exp);
6025 if (target)
6026 return target;
6027 break;
6029 case BUILT_IN_CLASSIFY_TYPE:
6030 return expand_builtin_classify_type (exp);
6032 case BUILT_IN_CONSTANT_P:
6033 return const0_rtx;
6035 case BUILT_IN_FRAME_ADDRESS:
6036 case BUILT_IN_RETURN_ADDRESS:
6037 return expand_builtin_frame_address (fndecl, exp);
6039 /* Returns the address of the area where the structure is returned.
6040 0 otherwise. */
6041 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6042 if (call_expr_nargs (exp) != 0
6043 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6044 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6045 return const0_rtx;
6046 else
6047 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6049 case BUILT_IN_ALLOCA:
6050 case BUILT_IN_ALLOCA_WITH_ALIGN:
6051 /* If the allocation stems from the declaration of a variable-sized
6052 object, it cannot accumulate. */
6053 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6054 if (target)
6055 return target;
6056 break;
6058 case BUILT_IN_STACK_SAVE:
6059 return expand_stack_save ();
6061 case BUILT_IN_STACK_RESTORE:
6062 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6063 return const0_rtx;
6065 case BUILT_IN_BSWAP16:
6066 case BUILT_IN_BSWAP32:
6067 case BUILT_IN_BSWAP64:
6068 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6069 if (target)
6070 return target;
6071 break;
6073 CASE_INT_FN (BUILT_IN_FFS):
6074 target = expand_builtin_unop (target_mode, exp, target,
6075 subtarget, ffs_optab);
6076 if (target)
6077 return target;
6078 break;
6080 CASE_INT_FN (BUILT_IN_CLZ):
6081 target = expand_builtin_unop (target_mode, exp, target,
6082 subtarget, clz_optab);
6083 if (target)
6084 return target;
6085 break;
6087 CASE_INT_FN (BUILT_IN_CTZ):
6088 target = expand_builtin_unop (target_mode, exp, target,
6089 subtarget, ctz_optab);
6090 if (target)
6091 return target;
6092 break;
6094 CASE_INT_FN (BUILT_IN_CLRSB):
6095 target = expand_builtin_unop (target_mode, exp, target,
6096 subtarget, clrsb_optab);
6097 if (target)
6098 return target;
6099 break;
6101 CASE_INT_FN (BUILT_IN_POPCOUNT):
6102 target = expand_builtin_unop (target_mode, exp, target,
6103 subtarget, popcount_optab);
6104 if (target)
6105 return target;
6106 break;
6108 CASE_INT_FN (BUILT_IN_PARITY):
6109 target = expand_builtin_unop (target_mode, exp, target,
6110 subtarget, parity_optab);
6111 if (target)
6112 return target;
6113 break;
6115 case BUILT_IN_STRLEN:
6116 target = expand_builtin_strlen (exp, target, target_mode);
6117 if (target)
6118 return target;
6119 break;
6121 case BUILT_IN_STRCPY:
6122 target = expand_builtin_strcpy (exp, target);
6123 if (target)
6124 return target;
6125 break;
6127 case BUILT_IN_STRNCPY:
6128 target = expand_builtin_strncpy (exp, target);
6129 if (target)
6130 return target;
6131 break;
6133 case BUILT_IN_STPCPY:
6134 target = expand_builtin_stpcpy (exp, target, mode);
6135 if (target)
6136 return target;
6137 break;
6139 case BUILT_IN_MEMCPY:
6140 target = expand_builtin_memcpy (exp, target);
6141 if (target)
6142 return target;
6143 break;
6145 case BUILT_IN_MEMPCPY:
6146 target = expand_builtin_mempcpy (exp, target, mode);
6147 if (target)
6148 return target;
6149 break;
6151 case BUILT_IN_MEMSET:
6152 target = expand_builtin_memset (exp, target, mode);
6153 if (target)
6154 return target;
6155 break;
6157 case BUILT_IN_BZERO:
6158 target = expand_builtin_bzero (exp);
6159 if (target)
6160 return target;
6161 break;
6163 case BUILT_IN_STRCMP:
6164 target = expand_builtin_strcmp (exp, target);
6165 if (target)
6166 return target;
6167 break;
6169 case BUILT_IN_STRNCMP:
6170 target = expand_builtin_strncmp (exp, target, mode);
6171 if (target)
6172 return target;
6173 break;
6175 case BUILT_IN_BCMP:
6176 case BUILT_IN_MEMCMP:
6177 target = expand_builtin_memcmp (exp, target, mode);
6178 if (target)
6179 return target;
6180 break;
6182 case BUILT_IN_SETJMP:
6183 /* This should have been lowered to the builtins below. */
6184 gcc_unreachable ();
6186 case BUILT_IN_SETJMP_SETUP:
6187 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6188 and the receiver label. */
6189 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6191 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6192 VOIDmode, EXPAND_NORMAL);
6193 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6194 rtx label_r = label_rtx (label);
6196 /* This is copied from the handling of non-local gotos. */
6197 expand_builtin_setjmp_setup (buf_addr, label_r);
6198 nonlocal_goto_handler_labels
6199 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6200 nonlocal_goto_handler_labels);
6201 /* ??? Do not let expand_label treat us as such since we would
6202 not want to be both on the list of non-local labels and on
6203 the list of forced labels. */
6204 FORCED_LABEL (label) = 0;
6205 return const0_rtx;
6207 break;
6209 case BUILT_IN_SETJMP_DISPATCHER:
6210 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6211 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6213 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6214 rtx label_r = label_rtx (label);
6216 /* Remove the dispatcher label from the list of non-local labels
6217 since the receiver labels have been added to it above. */
6218 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6219 return const0_rtx;
6221 break;
6223 case BUILT_IN_SETJMP_RECEIVER:
6224 /* __builtin_setjmp_receiver is passed the receiver label. */
6225 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6227 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6228 rtx label_r = label_rtx (label);
6230 expand_builtin_setjmp_receiver (label_r);
6231 return const0_rtx;
6233 break;
6235 /* __builtin_longjmp is passed a pointer to an array of five words.
6236 It's similar to the C library longjmp function but works with
6237 __builtin_setjmp above. */
6238 case BUILT_IN_LONGJMP:
6239 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6241 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6242 VOIDmode, EXPAND_NORMAL);
6243 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6245 if (value != const1_rtx)
6247 error ("%<__builtin_longjmp%> second argument must be 1");
6248 return const0_rtx;
6251 expand_builtin_longjmp (buf_addr, value);
6252 return const0_rtx;
6254 break;
6256 case BUILT_IN_NONLOCAL_GOTO:
6257 target = expand_builtin_nonlocal_goto (exp);
6258 if (target)
6259 return target;
6260 break;
6262 /* This updates the setjmp buffer that is its argument with the value
6263 of the current stack pointer. */
6264 case BUILT_IN_UPDATE_SETJMP_BUF:
6265 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6267 rtx buf_addr
6268 = expand_normal (CALL_EXPR_ARG (exp, 0));
6270 expand_builtin_update_setjmp_buf (buf_addr);
6271 return const0_rtx;
6273 break;
6275 case BUILT_IN_TRAP:
6276 expand_builtin_trap ();
6277 return const0_rtx;
6279 case BUILT_IN_UNREACHABLE:
6280 expand_builtin_unreachable ();
6281 return const0_rtx;
6283 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6284 case BUILT_IN_SIGNBITD32:
6285 case BUILT_IN_SIGNBITD64:
6286 case BUILT_IN_SIGNBITD128:
6287 target = expand_builtin_signbit (exp, target);
6288 if (target)
6289 return target;
6290 break;
6292 /* Various hooks for the DWARF 2 __throw routine. */
6293 case BUILT_IN_UNWIND_INIT:
6294 expand_builtin_unwind_init ();
6295 return const0_rtx;
6296 case BUILT_IN_DWARF_CFA:
6297 return virtual_cfa_rtx;
6298 #ifdef DWARF2_UNWIND_INFO
6299 case BUILT_IN_DWARF_SP_COLUMN:
6300 return expand_builtin_dwarf_sp_column ();
6301 case BUILT_IN_INIT_DWARF_REG_SIZES:
6302 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6303 return const0_rtx;
6304 #endif
6305 case BUILT_IN_FROB_RETURN_ADDR:
6306 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6307 case BUILT_IN_EXTRACT_RETURN_ADDR:
6308 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6309 case BUILT_IN_EH_RETURN:
6310 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6311 CALL_EXPR_ARG (exp, 1));
6312 return const0_rtx;
6313 #ifdef EH_RETURN_DATA_REGNO
6314 case BUILT_IN_EH_RETURN_DATA_REGNO:
6315 return expand_builtin_eh_return_data_regno (exp);
6316 #endif
6317 case BUILT_IN_EXTEND_POINTER:
6318 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6319 case BUILT_IN_EH_POINTER:
6320 return expand_builtin_eh_pointer (exp);
6321 case BUILT_IN_EH_FILTER:
6322 return expand_builtin_eh_filter (exp);
6323 case BUILT_IN_EH_COPY_VALUES:
6324 return expand_builtin_eh_copy_values (exp);
6326 case BUILT_IN_VA_START:
6327 return expand_builtin_va_start (exp);
6328 case BUILT_IN_VA_END:
6329 return expand_builtin_va_end (exp);
6330 case BUILT_IN_VA_COPY:
6331 return expand_builtin_va_copy (exp);
6332 case BUILT_IN_EXPECT:
6333 return expand_builtin_expect (exp, target);
6334 case BUILT_IN_ASSUME_ALIGNED:
6335 return expand_builtin_assume_aligned (exp, target);
6336 case BUILT_IN_PREFETCH:
6337 expand_builtin_prefetch (exp);
6338 return const0_rtx;
6340 case BUILT_IN_INIT_TRAMPOLINE:
6341 return expand_builtin_init_trampoline (exp, true);
6342 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6343 return expand_builtin_init_trampoline (exp, false);
6344 case BUILT_IN_ADJUST_TRAMPOLINE:
6345 return expand_builtin_adjust_trampoline (exp);
6347 case BUILT_IN_FORK:
6348 case BUILT_IN_EXECL:
6349 case BUILT_IN_EXECV:
6350 case BUILT_IN_EXECLP:
6351 case BUILT_IN_EXECLE:
6352 case BUILT_IN_EXECVP:
6353 case BUILT_IN_EXECVE:
6354 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6355 if (target)
6356 return target;
6357 break;
6359 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6360 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6361 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6362 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6363 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6364 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6365 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6366 if (target)
6367 return target;
6368 break;
6370 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6371 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6372 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6373 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6374 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6375 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6376 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6377 if (target)
6378 return target;
6379 break;
6381 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6382 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6383 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6384 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6385 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6386 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6387 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6388 if (target)
6389 return target;
6390 break;
6392 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6393 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6394 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6395 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6396 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6397 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6398 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6399 if (target)
6400 return target;
6401 break;
6403 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6404 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6405 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6406 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6407 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6408 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6409 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6410 if (target)
6411 return target;
6412 break;
6414 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6415 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6416 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6417 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6418 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6419 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6420 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6421 if (target)
6422 return target;
6423 break;
6425 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6426 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6427 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6428 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6429 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6430 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6431 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6432 if (target)
6433 return target;
6434 break;
6436 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6437 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6438 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6439 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6440 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6441 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6442 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6443 if (target)
6444 return target;
6445 break;
6447 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6448 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6449 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6450 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6451 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6452 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6453 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6454 if (target)
6455 return target;
6456 break;
6458 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6459 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6460 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6461 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6462 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6463 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6464 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6465 if (target)
6466 return target;
6467 break;
6469 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6470 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6471 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6472 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6473 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6474 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6475 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6476 if (target)
6477 return target;
6478 break;
6480 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6481 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6482 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6483 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6484 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6485 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6486 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6487 if (target)
6488 return target;
6489 break;
6491 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6492 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6493 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6494 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6495 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6496 if (mode == VOIDmode)
6497 mode = TYPE_MODE (boolean_type_node);
6498 if (!target || !register_operand (target, mode))
6499 target = gen_reg_rtx (mode);
6501 mode = get_builtin_sync_mode
6502 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6503 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6504 if (target)
6505 return target;
6506 break;
6508 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6509 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6510 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6511 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6512 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6513 mode = get_builtin_sync_mode
6514 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6515 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6516 if (target)
6517 return target;
6518 break;
6520 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6521 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6522 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6523 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6524 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6525 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6526 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6527 if (target)
6528 return target;
6529 break;
6531 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6532 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6533 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6534 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6535 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6536 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6537 expand_builtin_sync_lock_release (mode, exp);
6538 return const0_rtx;
6540 case BUILT_IN_SYNC_SYNCHRONIZE:
6541 expand_builtin_sync_synchronize ();
6542 return const0_rtx;
6544 case BUILT_IN_ATOMIC_EXCHANGE_1:
6545 case BUILT_IN_ATOMIC_EXCHANGE_2:
6546 case BUILT_IN_ATOMIC_EXCHANGE_4:
6547 case BUILT_IN_ATOMIC_EXCHANGE_8:
6548 case BUILT_IN_ATOMIC_EXCHANGE_16:
6549 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6550 target = expand_builtin_atomic_exchange (mode, exp, target);
6551 if (target)
6552 return target;
6553 break;
6555 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6556 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6557 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6558 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6559 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6561 unsigned int nargs, z;
6562 vec<tree, va_gc> *vec;
6564 mode =
6565 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6566 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6567 if (target)
6568 return target;
6570 /* If this is turned into an external library call, the weak parameter
6571 must be dropped to match the expected parameter list. */
6572 nargs = call_expr_nargs (exp);
6573 vec_alloc (vec, nargs - 1);
6574 for (z = 0; z < 3; z++)
6575 vec->quick_push (CALL_EXPR_ARG (exp, z));
6576 /* Skip the boolean weak parameter. */
6577 for (z = 4; z < 6; z++)
6578 vec->quick_push (CALL_EXPR_ARG (exp, z));
6579 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6580 break;
6583 case BUILT_IN_ATOMIC_LOAD_1:
6584 case BUILT_IN_ATOMIC_LOAD_2:
6585 case BUILT_IN_ATOMIC_LOAD_4:
6586 case BUILT_IN_ATOMIC_LOAD_8:
6587 case BUILT_IN_ATOMIC_LOAD_16:
6588 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6589 target = expand_builtin_atomic_load (mode, exp, target);
6590 if (target)
6591 return target;
6592 break;
6594 case BUILT_IN_ATOMIC_STORE_1:
6595 case BUILT_IN_ATOMIC_STORE_2:
6596 case BUILT_IN_ATOMIC_STORE_4:
6597 case BUILT_IN_ATOMIC_STORE_8:
6598 case BUILT_IN_ATOMIC_STORE_16:
6599 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6600 target = expand_builtin_atomic_store (mode, exp);
6601 if (target)
6602 return const0_rtx;
6603 break;
6605 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6606 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6607 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6608 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6609 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6611 enum built_in_function lib;
6612 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6613 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6614 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6615 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6616 ignore, lib);
6617 if (target)
6618 return target;
6619 break;
6621 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6622 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6623 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6624 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6625 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6627 enum built_in_function lib;
6628 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6629 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6630 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6631 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6632 ignore, lib);
6633 if (target)
6634 return target;
6635 break;
6637 case BUILT_IN_ATOMIC_AND_FETCH_1:
6638 case BUILT_IN_ATOMIC_AND_FETCH_2:
6639 case BUILT_IN_ATOMIC_AND_FETCH_4:
6640 case BUILT_IN_ATOMIC_AND_FETCH_8:
6641 case BUILT_IN_ATOMIC_AND_FETCH_16:
6643 enum built_in_function lib;
6644 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6645 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6646 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6647 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6648 ignore, lib);
6649 if (target)
6650 return target;
6651 break;
6653 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6654 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6655 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6656 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6657 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6659 enum built_in_function lib;
6660 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6661 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6662 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6663 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6664 ignore, lib);
6665 if (target)
6666 return target;
6667 break;
6669 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6670 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6671 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6672 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6673 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6675 enum built_in_function lib;
6676 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6677 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6678 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6679 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6680 ignore, lib);
6681 if (target)
6682 return target;
6683 break;
6685 case BUILT_IN_ATOMIC_OR_FETCH_1:
6686 case BUILT_IN_ATOMIC_OR_FETCH_2:
6687 case BUILT_IN_ATOMIC_OR_FETCH_4:
6688 case BUILT_IN_ATOMIC_OR_FETCH_8:
6689 case BUILT_IN_ATOMIC_OR_FETCH_16:
6691 enum built_in_function lib;
6692 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6693 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6694 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6695 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6696 ignore, lib);
6697 if (target)
6698 return target;
6699 break;
6701 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6702 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6703 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6704 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6705 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6706 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6707 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6708 ignore, BUILT_IN_NONE);
6709 if (target)
6710 return target;
6711 break;
6713 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6714 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6715 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6716 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6717 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6718 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6719 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6720 ignore, BUILT_IN_NONE);
6721 if (target)
6722 return target;
6723 break;
6725 case BUILT_IN_ATOMIC_FETCH_AND_1:
6726 case BUILT_IN_ATOMIC_FETCH_AND_2:
6727 case BUILT_IN_ATOMIC_FETCH_AND_4:
6728 case BUILT_IN_ATOMIC_FETCH_AND_8:
6729 case BUILT_IN_ATOMIC_FETCH_AND_16:
6730 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6731 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6732 ignore, BUILT_IN_NONE);
6733 if (target)
6734 return target;
6735 break;
6737 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6738 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6739 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6740 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6741 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6742 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6743 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6744 ignore, BUILT_IN_NONE);
6745 if (target)
6746 return target;
6747 break;
6749 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6750 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6751 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6752 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6753 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6754 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6755 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6756 ignore, BUILT_IN_NONE);
6757 if (target)
6758 return target;
6759 break;
6761 case BUILT_IN_ATOMIC_FETCH_OR_1:
6762 case BUILT_IN_ATOMIC_FETCH_OR_2:
6763 case BUILT_IN_ATOMIC_FETCH_OR_4:
6764 case BUILT_IN_ATOMIC_FETCH_OR_8:
6765 case BUILT_IN_ATOMIC_FETCH_OR_16:
6766 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6767 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6768 ignore, BUILT_IN_NONE);
6769 if (target)
6770 return target;
6771 break;
6773 case BUILT_IN_ATOMIC_TEST_AND_SET:
6774 return expand_builtin_atomic_test_and_set (exp, target);
6776 case BUILT_IN_ATOMIC_CLEAR:
6777 return expand_builtin_atomic_clear (exp);
6779 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6780 return expand_builtin_atomic_always_lock_free (exp);
6782 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6783 target = expand_builtin_atomic_is_lock_free (exp);
6784 if (target)
6785 return target;
6786 break;
6788 case BUILT_IN_ATOMIC_THREAD_FENCE:
6789 expand_builtin_atomic_thread_fence (exp);
6790 return const0_rtx;
6792 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6793 expand_builtin_atomic_signal_fence (exp);
6794 return const0_rtx;
6796 case BUILT_IN_OBJECT_SIZE:
6797 return expand_builtin_object_size (exp);
6799 case BUILT_IN_MEMCPY_CHK:
6800 case BUILT_IN_MEMPCPY_CHK:
6801 case BUILT_IN_MEMMOVE_CHK:
6802 case BUILT_IN_MEMSET_CHK:
6803 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6804 if (target)
6805 return target;
6806 break;
6808 case BUILT_IN_STRCPY_CHK:
6809 case BUILT_IN_STPCPY_CHK:
6810 case BUILT_IN_STRNCPY_CHK:
6811 case BUILT_IN_STPNCPY_CHK:
6812 case BUILT_IN_STRCAT_CHK:
6813 case BUILT_IN_STRNCAT_CHK:
6814 case BUILT_IN_SNPRINTF_CHK:
6815 case BUILT_IN_VSNPRINTF_CHK:
6816 maybe_emit_chk_warning (exp, fcode);
6817 break;
6819 case BUILT_IN_SPRINTF_CHK:
6820 case BUILT_IN_VSPRINTF_CHK:
6821 maybe_emit_sprintf_chk_warning (exp, fcode);
6822 break;
6824 case BUILT_IN_FREE:
6825 if (warn_free_nonheap_object)
6826 maybe_emit_free_warning (exp);
6827 break;
6829 case BUILT_IN_THREAD_POINTER:
6830 return expand_builtin_thread_pointer (exp, target);
6832 case BUILT_IN_SET_THREAD_POINTER:
6833 expand_builtin_set_thread_pointer (exp);
6834 return const0_rtx;
6836 case BUILT_IN_CILK_DETACH:
6837 expand_builtin_cilk_detach (exp);
6838 return const0_rtx;
6840 case BUILT_IN_CILK_POP_FRAME:
6841 expand_builtin_cilk_pop_frame (exp);
6842 return const0_rtx;
6844 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
6845 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
6846 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
6847 return expand_normal (CALL_EXPR_ARG (exp, 0));
6849 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
6850 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
6851 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
6852 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
6853 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
6854 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
6855 case BUILT_IN_CHKP_GET_PTR_LBOUND:
6856 case BUILT_IN_CHKP_GET_PTR_UBOUND:
6857 /* We allow user CHKP builtins if Pointer Bounds
6858 Checker is off. */
6859 if (!flag_check_pointer_bounds)
6861 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
6862 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS)
6863 return expand_normal (CALL_EXPR_ARG (exp, 0));
6864 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
6865 return expand_normal (size_zero_node);
6866 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
6867 return expand_normal (size_int (-1));
6868 else
6869 return const0_rtx;
6871 /* FALLTHROUGH */
6873 case BUILT_IN_CHKP_BNDMK:
6874 case BUILT_IN_CHKP_BNDSTX:
6875 case BUILT_IN_CHKP_BNDCL:
6876 case BUILT_IN_CHKP_BNDCU:
6877 case BUILT_IN_CHKP_BNDLDX:
6878 case BUILT_IN_CHKP_BNDRET:
6879 case BUILT_IN_CHKP_INTERSECT:
6880 case BUILT_IN_CHKP_ARG_BND:
6881 case BUILT_IN_CHKP_NARROW:
6882 case BUILT_IN_CHKP_EXTRACT_LOWER:
6883 case BUILT_IN_CHKP_EXTRACT_UPPER:
6884 /* Software implementation of pointers checker is NYI.
6885 Target support is required. */
6886 error ("Your target platform does not support -fcheck-pointers");
6887 break;
6889 default: /* just do library call, if unknown builtin */
6890 break;
6893 /* The switch statement above can drop through to cause the function
6894 to be called normally. */
6895 return expand_call (exp, target, ignore);
6898 /* Determine whether a tree node represents a call to a built-in
6899 function. If the tree T is a call to a built-in function with
6900 the right number of arguments of the appropriate types, return
6901 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6902 Otherwise the return value is END_BUILTINS. */
6904 enum built_in_function
6905 builtin_mathfn_code (const_tree t)
6907 const_tree fndecl, arg, parmlist;
6908 const_tree argtype, parmtype;
6909 const_call_expr_arg_iterator iter;
6911 if (TREE_CODE (t) != CALL_EXPR
6912 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6913 return END_BUILTINS;
6915 fndecl = get_callee_fndecl (t);
6916 if (fndecl == NULL_TREE
6917 || TREE_CODE (fndecl) != FUNCTION_DECL
6918 || ! DECL_BUILT_IN (fndecl)
6919 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6920 return END_BUILTINS;
6922 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6923 init_const_call_expr_arg_iterator (t, &iter);
6924 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6926 /* If a function doesn't take a variable number of arguments,
6927 the last element in the list will have type `void'. */
6928 parmtype = TREE_VALUE (parmlist);
6929 if (VOID_TYPE_P (parmtype))
6931 if (more_const_call_expr_args_p (&iter))
6932 return END_BUILTINS;
6933 return DECL_FUNCTION_CODE (fndecl);
6936 if (! more_const_call_expr_args_p (&iter))
6937 return END_BUILTINS;
6939 arg = next_const_call_expr_arg (&iter);
6940 argtype = TREE_TYPE (arg);
6942 if (SCALAR_FLOAT_TYPE_P (parmtype))
6944 if (! SCALAR_FLOAT_TYPE_P (argtype))
6945 return END_BUILTINS;
6947 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6949 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6950 return END_BUILTINS;
6952 else if (POINTER_TYPE_P (parmtype))
6954 if (! POINTER_TYPE_P (argtype))
6955 return END_BUILTINS;
6957 else if (INTEGRAL_TYPE_P (parmtype))
6959 if (! INTEGRAL_TYPE_P (argtype))
6960 return END_BUILTINS;
6962 else
6963 return END_BUILTINS;
6966 /* Variable-length argument list. */
6967 return DECL_FUNCTION_CODE (fndecl);
6970 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6971 evaluate to a constant. */
6973 static tree
6974 fold_builtin_constant_p (tree arg)
6976 /* We return 1 for a numeric type that's known to be a constant
6977 value at compile-time or for an aggregate type that's a
6978 literal constant. */
6979 STRIP_NOPS (arg);
6981 /* If we know this is a constant, emit the constant of one. */
6982 if (CONSTANT_CLASS_P (arg)
6983 || (TREE_CODE (arg) == CONSTRUCTOR
6984 && TREE_CONSTANT (arg)))
6985 return integer_one_node;
6986 if (TREE_CODE (arg) == ADDR_EXPR)
6988 tree op = TREE_OPERAND (arg, 0);
6989 if (TREE_CODE (op) == STRING_CST
6990 || (TREE_CODE (op) == ARRAY_REF
6991 && integer_zerop (TREE_OPERAND (op, 1))
6992 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6993 return integer_one_node;
6996 /* If this expression has side effects, show we don't know it to be a
6997 constant. Likewise if it's a pointer or aggregate type since in
6998 those case we only want literals, since those are only optimized
6999 when generating RTL, not later.
7000 And finally, if we are compiling an initializer, not code, we
7001 need to return a definite result now; there's not going to be any
7002 more optimization done. */
7003 if (TREE_SIDE_EFFECTS (arg)
7004 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7005 || POINTER_TYPE_P (TREE_TYPE (arg))
7006 || cfun == 0
7007 || folding_initializer
7008 || force_folding_builtin_constant_p)
7009 return integer_zero_node;
7011 return NULL_TREE;
7014 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7015 return it as a truthvalue. */
7017 static tree
7018 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
7020 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7022 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
7023 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7024 ret_type = TREE_TYPE (TREE_TYPE (fn));
7025 pred_type = TREE_VALUE (arg_types);
7026 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7028 pred = fold_convert_loc (loc, pred_type, pred);
7029 expected = fold_convert_loc (loc, expected_type, expected);
7030 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
7032 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7033 build_int_cst (ret_type, 0));
7036 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7037 NULL_TREE if no simplification is possible. */
7039 static tree
7040 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
7042 tree inner, fndecl, inner_arg0;
7043 enum tree_code code;
7045 /* Distribute the expected value over short-circuiting operators.
7046 See through the cast from truthvalue_type_node to long. */
7047 inner_arg0 = arg0;
7048 while (TREE_CODE (inner_arg0) == NOP_EXPR
7049 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7050 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7051 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7053 /* If this is a builtin_expect within a builtin_expect keep the
7054 inner one. See through a comparison against a constant. It
7055 might have been added to create a thruthvalue. */
7056 inner = inner_arg0;
7058 if (COMPARISON_CLASS_P (inner)
7059 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7060 inner = TREE_OPERAND (inner, 0);
7062 if (TREE_CODE (inner) == CALL_EXPR
7063 && (fndecl = get_callee_fndecl (inner))
7064 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7065 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7066 return arg0;
7068 inner = inner_arg0;
7069 code = TREE_CODE (inner);
7070 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7072 tree op0 = TREE_OPERAND (inner, 0);
7073 tree op1 = TREE_OPERAND (inner, 1);
7075 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7076 op1 = build_builtin_expect_predicate (loc, op1, arg1);
7077 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7079 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7082 /* If the argument isn't invariant then there's nothing else we can do. */
7083 if (!TREE_CONSTANT (inner_arg0))
7084 return NULL_TREE;
7086 /* If we expect that a comparison against the argument will fold to
7087 a constant return the constant. In practice, this means a true
7088 constant or the address of a non-weak symbol. */
7089 inner = inner_arg0;
7090 STRIP_NOPS (inner);
7091 if (TREE_CODE (inner) == ADDR_EXPR)
7095 inner = TREE_OPERAND (inner, 0);
7097 while (TREE_CODE (inner) == COMPONENT_REF
7098 || TREE_CODE (inner) == ARRAY_REF);
7099 if ((TREE_CODE (inner) == VAR_DECL
7100 || TREE_CODE (inner) == FUNCTION_DECL)
7101 && DECL_WEAK (inner))
7102 return NULL_TREE;
7105 /* Otherwise, ARG0 already has the proper type for the return value. */
7106 return arg0;
7109 /* Fold a call to __builtin_classify_type with argument ARG. */
7111 static tree
7112 fold_builtin_classify_type (tree arg)
7114 if (arg == 0)
7115 return build_int_cst (integer_type_node, no_type_class);
7117 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7120 /* Fold a call to __builtin_strlen with argument ARG. */
7122 static tree
7123 fold_builtin_strlen (location_t loc, tree type, tree arg)
7125 if (!validate_arg (arg, POINTER_TYPE))
7126 return NULL_TREE;
7127 else
7129 tree len = c_strlen (arg, 0);
7131 if (len)
7132 return fold_convert_loc (loc, type, len);
7134 return NULL_TREE;
7138 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7140 static tree
7141 fold_builtin_inf (location_t loc, tree type, int warn)
7143 REAL_VALUE_TYPE real;
7145 /* __builtin_inff is intended to be usable to define INFINITY on all
7146 targets. If an infinity is not available, INFINITY expands "to a
7147 positive constant of type float that overflows at translation
7148 time", footnote "In this case, using INFINITY will violate the
7149 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7150 Thus we pedwarn to ensure this constraint violation is
7151 diagnosed. */
7152 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7153 pedwarn (loc, 0, "target format does not support infinity");
7155 real_inf (&real);
7156 return build_real (type, real);
7159 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7161 static tree
7162 fold_builtin_nan (tree arg, tree type, int quiet)
7164 REAL_VALUE_TYPE real;
7165 const char *str;
7167 if (!validate_arg (arg, POINTER_TYPE))
7168 return NULL_TREE;
7169 str = c_getstr (arg);
7170 if (!str)
7171 return NULL_TREE;
7173 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7174 return NULL_TREE;
7176 return build_real (type, real);
7179 /* Return true if the floating point expression T has an integer value.
7180 We also allow +Inf, -Inf and NaN to be considered integer values. */
7182 static bool
7183 integer_valued_real_p (tree t)
7185 switch (TREE_CODE (t))
7187 case FLOAT_EXPR:
7188 return true;
7190 case ABS_EXPR:
7191 case SAVE_EXPR:
7192 return integer_valued_real_p (TREE_OPERAND (t, 0));
7194 case COMPOUND_EXPR:
7195 case MODIFY_EXPR:
7196 case BIND_EXPR:
7197 return integer_valued_real_p (TREE_OPERAND (t, 1));
7199 case PLUS_EXPR:
7200 case MINUS_EXPR:
7201 case MULT_EXPR:
7202 case MIN_EXPR:
7203 case MAX_EXPR:
7204 return integer_valued_real_p (TREE_OPERAND (t, 0))
7205 && integer_valued_real_p (TREE_OPERAND (t, 1));
7207 case COND_EXPR:
7208 return integer_valued_real_p (TREE_OPERAND (t, 1))
7209 && integer_valued_real_p (TREE_OPERAND (t, 2));
7211 case REAL_CST:
7212 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7214 case NOP_EXPR:
7216 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7217 if (TREE_CODE (type) == INTEGER_TYPE)
7218 return true;
7219 if (TREE_CODE (type) == REAL_TYPE)
7220 return integer_valued_real_p (TREE_OPERAND (t, 0));
7221 break;
7224 case CALL_EXPR:
7225 switch (builtin_mathfn_code (t))
7227 CASE_FLT_FN (BUILT_IN_CEIL):
7228 CASE_FLT_FN (BUILT_IN_FLOOR):
7229 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7230 CASE_FLT_FN (BUILT_IN_RINT):
7231 CASE_FLT_FN (BUILT_IN_ROUND):
7232 CASE_FLT_FN (BUILT_IN_TRUNC):
7233 return true;
7235 CASE_FLT_FN (BUILT_IN_FMIN):
7236 CASE_FLT_FN (BUILT_IN_FMAX):
7237 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7238 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7240 default:
7241 break;
7243 break;
7245 default:
7246 break;
7248 return false;
7251 /* FNDECL is assumed to be a builtin where truncation can be propagated
7252 across (for instance floor((double)f) == (double)floorf (f).
7253 Do the transformation for a call with argument ARG. */
7255 static tree
7256 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7258 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7260 if (!validate_arg (arg, REAL_TYPE))
7261 return NULL_TREE;
7263 /* Integer rounding functions are idempotent. */
7264 if (fcode == builtin_mathfn_code (arg))
7265 return arg;
7267 /* If argument is already integer valued, and we don't need to worry
7268 about setting errno, there's no need to perform rounding. */
7269 if (! flag_errno_math && integer_valued_real_p (arg))
7270 return arg;
7272 if (optimize)
7274 tree arg0 = strip_float_extensions (arg);
7275 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7276 tree newtype = TREE_TYPE (arg0);
7277 tree decl;
7279 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7280 && (decl = mathfn_built_in (newtype, fcode)))
7281 return fold_convert_loc (loc, ftype,
7282 build_call_expr_loc (loc, decl, 1,
7283 fold_convert_loc (loc,
7284 newtype,
7285 arg0)));
7287 return NULL_TREE;
7290 /* FNDECL is assumed to be builtin which can narrow the FP type of
7291 the argument, for instance lround((double)f) -> lroundf (f).
7292 Do the transformation for a call with argument ARG. */
7294 static tree
7295 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7297 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7299 if (!validate_arg (arg, REAL_TYPE))
7300 return NULL_TREE;
7302 /* If argument is already integer valued, and we don't need to worry
7303 about setting errno, there's no need to perform rounding. */
7304 if (! flag_errno_math && integer_valued_real_p (arg))
7305 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7306 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7308 if (optimize)
7310 tree ftype = TREE_TYPE (arg);
7311 tree arg0 = strip_float_extensions (arg);
7312 tree newtype = TREE_TYPE (arg0);
7313 tree decl;
7315 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7316 && (decl = mathfn_built_in (newtype, fcode)))
7317 return build_call_expr_loc (loc, decl, 1,
7318 fold_convert_loc (loc, newtype, arg0));
7321 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7322 sizeof (int) == sizeof (long). */
7323 if (TYPE_PRECISION (integer_type_node)
7324 == TYPE_PRECISION (long_integer_type_node))
7326 tree newfn = NULL_TREE;
7327 switch (fcode)
7329 CASE_FLT_FN (BUILT_IN_ICEIL):
7330 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7331 break;
7333 CASE_FLT_FN (BUILT_IN_IFLOOR):
7334 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7335 break;
7337 CASE_FLT_FN (BUILT_IN_IROUND):
7338 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7339 break;
7341 CASE_FLT_FN (BUILT_IN_IRINT):
7342 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7343 break;
7345 default:
7346 break;
7349 if (newfn)
7351 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7352 return fold_convert_loc (loc,
7353 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7357 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7358 sizeof (long long) == sizeof (long). */
7359 if (TYPE_PRECISION (long_long_integer_type_node)
7360 == TYPE_PRECISION (long_integer_type_node))
7362 tree newfn = NULL_TREE;
7363 switch (fcode)
7365 CASE_FLT_FN (BUILT_IN_LLCEIL):
7366 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7367 break;
7369 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7370 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7371 break;
7373 CASE_FLT_FN (BUILT_IN_LLROUND):
7374 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7375 break;
7377 CASE_FLT_FN (BUILT_IN_LLRINT):
7378 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7379 break;
7381 default:
7382 break;
7385 if (newfn)
7387 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7388 return fold_convert_loc (loc,
7389 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7393 return NULL_TREE;
7396 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7397 return type. Return NULL_TREE if no simplification can be made. */
7399 static tree
7400 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7402 tree res;
7404 if (!validate_arg (arg, COMPLEX_TYPE)
7405 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7406 return NULL_TREE;
7408 /* Calculate the result when the argument is a constant. */
7409 if (TREE_CODE (arg) == COMPLEX_CST
7410 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7411 type, mpfr_hypot)))
7412 return res;
7414 if (TREE_CODE (arg) == COMPLEX_EXPR)
7416 tree real = TREE_OPERAND (arg, 0);
7417 tree imag = TREE_OPERAND (arg, 1);
7419 /* If either part is zero, cabs is fabs of the other. */
7420 if (real_zerop (real))
7421 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7422 if (real_zerop (imag))
7423 return fold_build1_loc (loc, ABS_EXPR, type, real);
7425 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7426 if (flag_unsafe_math_optimizations
7427 && operand_equal_p (real, imag, OEP_PURE_SAME))
7429 const REAL_VALUE_TYPE sqrt2_trunc
7430 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7431 STRIP_NOPS (real);
7432 return fold_build2_loc (loc, MULT_EXPR, type,
7433 fold_build1_loc (loc, ABS_EXPR, type, real),
7434 build_real (type, sqrt2_trunc));
7438 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7439 if (TREE_CODE (arg) == NEGATE_EXPR
7440 || TREE_CODE (arg) == CONJ_EXPR)
7441 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7443 /* Don't do this when optimizing for size. */
7444 if (flag_unsafe_math_optimizations
7445 && optimize && optimize_function_for_speed_p (cfun))
7447 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7449 if (sqrtfn != NULL_TREE)
7451 tree rpart, ipart, result;
7453 arg = builtin_save_expr (arg);
7455 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7456 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7458 rpart = builtin_save_expr (rpart);
7459 ipart = builtin_save_expr (ipart);
7461 result = fold_build2_loc (loc, PLUS_EXPR, type,
7462 fold_build2_loc (loc, MULT_EXPR, type,
7463 rpart, rpart),
7464 fold_build2_loc (loc, MULT_EXPR, type,
7465 ipart, ipart));
7467 return build_call_expr_loc (loc, sqrtfn, 1, result);
7471 return NULL_TREE;
7474 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7475 complex tree type of the result. If NEG is true, the imaginary
7476 zero is negative. */
7478 static tree
7479 build_complex_cproj (tree type, bool neg)
7481 REAL_VALUE_TYPE rinf, rzero = dconst0;
7483 real_inf (&rinf);
7484 rzero.sign = neg;
7485 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7486 build_real (TREE_TYPE (type), rzero));
7489 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7490 return type. Return NULL_TREE if no simplification can be made. */
7492 static tree
7493 fold_builtin_cproj (location_t loc, tree arg, tree type)
7495 if (!validate_arg (arg, COMPLEX_TYPE)
7496 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7497 return NULL_TREE;
7499 /* If there are no infinities, return arg. */
7500 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7501 return non_lvalue_loc (loc, arg);
7503 /* Calculate the result when the argument is a constant. */
7504 if (TREE_CODE (arg) == COMPLEX_CST)
7506 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7507 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7509 if (real_isinf (real) || real_isinf (imag))
7510 return build_complex_cproj (type, imag->sign);
7511 else
7512 return arg;
7514 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7516 tree real = TREE_OPERAND (arg, 0);
7517 tree imag = TREE_OPERAND (arg, 1);
7519 STRIP_NOPS (real);
7520 STRIP_NOPS (imag);
7522 /* If the real part is inf and the imag part is known to be
7523 nonnegative, return (inf + 0i). Remember side-effects are
7524 possible in the imag part. */
7525 if (TREE_CODE (real) == REAL_CST
7526 && real_isinf (TREE_REAL_CST_PTR (real))
7527 && tree_expr_nonnegative_p (imag))
7528 return omit_one_operand_loc (loc, type,
7529 build_complex_cproj (type, false),
7530 arg);
7532 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7533 Remember side-effects are possible in the real part. */
7534 if (TREE_CODE (imag) == REAL_CST
7535 && real_isinf (TREE_REAL_CST_PTR (imag)))
7536 return
7537 omit_one_operand_loc (loc, type,
7538 build_complex_cproj (type, TREE_REAL_CST_PTR
7539 (imag)->sign), arg);
7542 return NULL_TREE;
7545 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7546 Return NULL_TREE if no simplification can be made. */
7548 static tree
7549 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7552 enum built_in_function fcode;
7553 tree res;
7555 if (!validate_arg (arg, REAL_TYPE))
7556 return NULL_TREE;
7558 /* Calculate the result when the argument is a constant. */
7559 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7560 return res;
7562 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7563 fcode = builtin_mathfn_code (arg);
7564 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7566 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7567 arg = fold_build2_loc (loc, MULT_EXPR, type,
7568 CALL_EXPR_ARG (arg, 0),
7569 build_real (type, dconsthalf));
7570 return build_call_expr_loc (loc, expfn, 1, arg);
7573 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7574 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7576 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7578 if (powfn)
7580 tree arg0 = CALL_EXPR_ARG (arg, 0);
7581 tree tree_root;
7582 /* The inner root was either sqrt or cbrt. */
7583 /* This was a conditional expression but it triggered a bug
7584 in Sun C 5.5. */
7585 REAL_VALUE_TYPE dconstroot;
7586 if (BUILTIN_SQRT_P (fcode))
7587 dconstroot = dconsthalf;
7588 else
7589 dconstroot = dconst_third ();
7591 /* Adjust for the outer root. */
7592 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7593 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7594 tree_root = build_real (type, dconstroot);
7595 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7599 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7600 if (flag_unsafe_math_optimizations
7601 && (fcode == BUILT_IN_POW
7602 || fcode == BUILT_IN_POWF
7603 || fcode == BUILT_IN_POWL))
7605 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7606 tree arg0 = CALL_EXPR_ARG (arg, 0);
7607 tree arg1 = CALL_EXPR_ARG (arg, 1);
7608 tree narg1;
7609 if (!tree_expr_nonnegative_p (arg0))
7610 arg0 = build1 (ABS_EXPR, type, arg0);
7611 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7612 build_real (type, dconsthalf));
7613 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7616 return NULL_TREE;
7619 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7620 Return NULL_TREE if no simplification can be made. */
7622 static tree
7623 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7625 const enum built_in_function fcode = builtin_mathfn_code (arg);
7626 tree res;
7628 if (!validate_arg (arg, REAL_TYPE))
7629 return NULL_TREE;
7631 /* Calculate the result when the argument is a constant. */
7632 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7633 return res;
7635 if (flag_unsafe_math_optimizations)
7637 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7638 if (BUILTIN_EXPONENT_P (fcode))
7640 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7641 const REAL_VALUE_TYPE third_trunc =
7642 real_value_truncate (TYPE_MODE (type), dconst_third ());
7643 arg = fold_build2_loc (loc, MULT_EXPR, type,
7644 CALL_EXPR_ARG (arg, 0),
7645 build_real (type, third_trunc));
7646 return build_call_expr_loc (loc, expfn, 1, arg);
7649 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7650 if (BUILTIN_SQRT_P (fcode))
7652 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7654 if (powfn)
7656 tree arg0 = CALL_EXPR_ARG (arg, 0);
7657 tree tree_root;
7658 REAL_VALUE_TYPE dconstroot = dconst_third ();
7660 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7661 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7662 tree_root = build_real (type, dconstroot);
7663 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7667 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7668 if (BUILTIN_CBRT_P (fcode))
7670 tree arg0 = CALL_EXPR_ARG (arg, 0);
7671 if (tree_expr_nonnegative_p (arg0))
7673 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7675 if (powfn)
7677 tree tree_root;
7678 REAL_VALUE_TYPE dconstroot;
7680 real_arithmetic (&dconstroot, MULT_EXPR,
7681 dconst_third_ptr (), dconst_third_ptr ());
7682 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7683 tree_root = build_real (type, dconstroot);
7684 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7689 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7690 if (fcode == BUILT_IN_POW
7691 || fcode == BUILT_IN_POWF
7692 || fcode == BUILT_IN_POWL)
7694 tree arg00 = CALL_EXPR_ARG (arg, 0);
7695 tree arg01 = CALL_EXPR_ARG (arg, 1);
7696 if (tree_expr_nonnegative_p (arg00))
7698 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7699 const REAL_VALUE_TYPE dconstroot
7700 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7701 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7702 build_real (type, dconstroot));
7703 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7707 return NULL_TREE;
7710 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7711 TYPE is the type of the return value. Return NULL_TREE if no
7712 simplification can be made. */
7714 static tree
7715 fold_builtin_cos (location_t loc,
7716 tree arg, tree type, tree fndecl)
7718 tree res, narg;
7720 if (!validate_arg (arg, REAL_TYPE))
7721 return NULL_TREE;
7723 /* Calculate the result when the argument is a constant. */
7724 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7725 return res;
7727 /* Optimize cos(-x) into cos (x). */
7728 if ((narg = fold_strip_sign_ops (arg)))
7729 return build_call_expr_loc (loc, fndecl, 1, narg);
7731 return NULL_TREE;
7734 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7735 Return NULL_TREE if no simplification can be made. */
7737 static tree
7738 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7740 if (validate_arg (arg, REAL_TYPE))
7742 tree res, narg;
7744 /* Calculate the result when the argument is a constant. */
7745 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7746 return res;
7748 /* Optimize cosh(-x) into cosh (x). */
7749 if ((narg = fold_strip_sign_ops (arg)))
7750 return build_call_expr_loc (loc, fndecl, 1, narg);
7753 return NULL_TREE;
7756 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7757 argument ARG. TYPE is the type of the return value. Return
7758 NULL_TREE if no simplification can be made. */
7760 static tree
7761 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7762 bool hyper)
7764 if (validate_arg (arg, COMPLEX_TYPE)
7765 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7767 tree tmp;
7769 /* Calculate the result when the argument is a constant. */
7770 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7771 return tmp;
7773 /* Optimize fn(-x) into fn(x). */
7774 if ((tmp = fold_strip_sign_ops (arg)))
7775 return build_call_expr_loc (loc, fndecl, 1, tmp);
7778 return NULL_TREE;
7781 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7782 Return NULL_TREE if no simplification can be made. */
7784 static tree
7785 fold_builtin_tan (tree arg, tree type)
7787 enum built_in_function fcode;
7788 tree res;
7790 if (!validate_arg (arg, REAL_TYPE))
7791 return NULL_TREE;
7793 /* Calculate the result when the argument is a constant. */
7794 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7795 return res;
7797 /* Optimize tan(atan(x)) = x. */
7798 fcode = builtin_mathfn_code (arg);
7799 if (flag_unsafe_math_optimizations
7800 && (fcode == BUILT_IN_ATAN
7801 || fcode == BUILT_IN_ATANF
7802 || fcode == BUILT_IN_ATANL))
7803 return CALL_EXPR_ARG (arg, 0);
7805 return NULL_TREE;
7808 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7809 NULL_TREE if no simplification can be made. */
7811 static tree
7812 fold_builtin_sincos (location_t loc,
7813 tree arg0, tree arg1, tree arg2)
7815 tree type;
7816 tree res, fn, call;
7818 if (!validate_arg (arg0, REAL_TYPE)
7819 || !validate_arg (arg1, POINTER_TYPE)
7820 || !validate_arg (arg2, POINTER_TYPE))
7821 return NULL_TREE;
7823 type = TREE_TYPE (arg0);
7825 /* Calculate the result when the argument is a constant. */
7826 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7827 return res;
7829 /* Canonicalize sincos to cexpi. */
7830 if (!targetm.libc_has_function (function_c99_math_complex))
7831 return NULL_TREE;
7832 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7833 if (!fn)
7834 return NULL_TREE;
7836 call = build_call_expr_loc (loc, fn, 1, arg0);
7837 call = builtin_save_expr (call);
7839 return build2 (COMPOUND_EXPR, void_type_node,
7840 build2 (MODIFY_EXPR, void_type_node,
7841 build_fold_indirect_ref_loc (loc, arg1),
7842 build1 (IMAGPART_EXPR, type, call)),
7843 build2 (MODIFY_EXPR, void_type_node,
7844 build_fold_indirect_ref_loc (loc, arg2),
7845 build1 (REALPART_EXPR, type, call)));
7848 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7849 NULL_TREE if no simplification can be made. */
7851 static tree
7852 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7854 tree rtype;
7855 tree realp, imagp, ifn;
7856 tree res;
7858 if (!validate_arg (arg0, COMPLEX_TYPE)
7859 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7860 return NULL_TREE;
7862 /* Calculate the result when the argument is a constant. */
7863 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7864 return res;
7866 rtype = TREE_TYPE (TREE_TYPE (arg0));
7868 /* In case we can figure out the real part of arg0 and it is constant zero
7869 fold to cexpi. */
7870 if (!targetm.libc_has_function (function_c99_math_complex))
7871 return NULL_TREE;
7872 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7873 if (!ifn)
7874 return NULL_TREE;
7876 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7877 && real_zerop (realp))
7879 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7880 return build_call_expr_loc (loc, ifn, 1, narg);
7883 /* In case we can easily decompose real and imaginary parts split cexp
7884 to exp (r) * cexpi (i). */
7885 if (flag_unsafe_math_optimizations
7886 && realp)
7888 tree rfn, rcall, icall;
7890 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7891 if (!rfn)
7892 return NULL_TREE;
7894 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7895 if (!imagp)
7896 return NULL_TREE;
7898 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7899 icall = builtin_save_expr (icall);
7900 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7901 rcall = builtin_save_expr (rcall);
7902 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7903 fold_build2_loc (loc, MULT_EXPR, rtype,
7904 rcall,
7905 fold_build1_loc (loc, REALPART_EXPR,
7906 rtype, icall)),
7907 fold_build2_loc (loc, MULT_EXPR, rtype,
7908 rcall,
7909 fold_build1_loc (loc, IMAGPART_EXPR,
7910 rtype, icall)));
7913 return NULL_TREE;
7916 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7917 Return NULL_TREE if no simplification can be made. */
7919 static tree
7920 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7922 if (!validate_arg (arg, REAL_TYPE))
7923 return NULL_TREE;
7925 /* Optimize trunc of constant value. */
7926 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7928 REAL_VALUE_TYPE r, x;
7929 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7931 x = TREE_REAL_CST (arg);
7932 real_trunc (&r, TYPE_MODE (type), &x);
7933 return build_real (type, r);
7936 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7939 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7940 Return NULL_TREE if no simplification can be made. */
7942 static tree
7943 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7945 if (!validate_arg (arg, REAL_TYPE))
7946 return NULL_TREE;
7948 /* Optimize floor of constant value. */
7949 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7951 REAL_VALUE_TYPE x;
7953 x = TREE_REAL_CST (arg);
7954 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7956 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7957 REAL_VALUE_TYPE r;
7959 real_floor (&r, TYPE_MODE (type), &x);
7960 return build_real (type, r);
7964 /* Fold floor (x) where x is nonnegative to trunc (x). */
7965 if (tree_expr_nonnegative_p (arg))
7967 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7968 if (truncfn)
7969 return build_call_expr_loc (loc, truncfn, 1, arg);
7972 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7975 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7976 Return NULL_TREE if no simplification can be made. */
7978 static tree
7979 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7981 if (!validate_arg (arg, REAL_TYPE))
7982 return NULL_TREE;
7984 /* Optimize ceil of constant value. */
7985 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7987 REAL_VALUE_TYPE x;
7989 x = TREE_REAL_CST (arg);
7990 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7992 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7993 REAL_VALUE_TYPE r;
7995 real_ceil (&r, TYPE_MODE (type), &x);
7996 return build_real (type, r);
8000 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8003 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8004 Return NULL_TREE if no simplification can be made. */
8006 static tree
8007 fold_builtin_round (location_t loc, tree fndecl, tree arg)
8009 if (!validate_arg (arg, REAL_TYPE))
8010 return NULL_TREE;
8012 /* Optimize round of constant value. */
8013 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8015 REAL_VALUE_TYPE x;
8017 x = TREE_REAL_CST (arg);
8018 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8020 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8021 REAL_VALUE_TYPE r;
8023 real_round (&r, TYPE_MODE (type), &x);
8024 return build_real (type, r);
8028 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8031 /* Fold function call to builtin lround, lroundf or lroundl (or the
8032 corresponding long long versions) and other rounding functions. ARG
8033 is the argument to the call. Return NULL_TREE if no simplification
8034 can be made. */
8036 static tree
8037 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8039 if (!validate_arg (arg, REAL_TYPE))
8040 return NULL_TREE;
8042 /* Optimize lround of constant value. */
8043 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8045 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8047 if (real_isfinite (&x))
8049 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8050 tree ftype = TREE_TYPE (arg);
8051 double_int val;
8052 REAL_VALUE_TYPE r;
8054 switch (DECL_FUNCTION_CODE (fndecl))
8056 CASE_FLT_FN (BUILT_IN_IFLOOR):
8057 CASE_FLT_FN (BUILT_IN_LFLOOR):
8058 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8059 real_floor (&r, TYPE_MODE (ftype), &x);
8060 break;
8062 CASE_FLT_FN (BUILT_IN_ICEIL):
8063 CASE_FLT_FN (BUILT_IN_LCEIL):
8064 CASE_FLT_FN (BUILT_IN_LLCEIL):
8065 real_ceil (&r, TYPE_MODE (ftype), &x);
8066 break;
8068 CASE_FLT_FN (BUILT_IN_IROUND):
8069 CASE_FLT_FN (BUILT_IN_LROUND):
8070 CASE_FLT_FN (BUILT_IN_LLROUND):
8071 real_round (&r, TYPE_MODE (ftype), &x);
8072 break;
8074 default:
8075 gcc_unreachable ();
8078 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8079 if (double_int_fits_to_tree_p (itype, val))
8080 return double_int_to_tree (itype, val);
8084 switch (DECL_FUNCTION_CODE (fndecl))
8086 CASE_FLT_FN (BUILT_IN_LFLOOR):
8087 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8088 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8089 if (tree_expr_nonnegative_p (arg))
8090 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8091 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8092 break;
8093 default:;
8096 return fold_fixed_mathfn (loc, fndecl, arg);
8099 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8100 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8101 the argument to the call. Return NULL_TREE if no simplification can
8102 be made. */
8104 static tree
8105 fold_builtin_bitop (tree fndecl, tree arg)
8107 if (!validate_arg (arg, INTEGER_TYPE))
8108 return NULL_TREE;
8110 /* Optimize for constant argument. */
8111 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8113 HOST_WIDE_INT hi, width, result;
8114 unsigned HOST_WIDE_INT lo;
8115 tree type;
8117 type = TREE_TYPE (arg);
8118 width = TYPE_PRECISION (type);
8119 lo = TREE_INT_CST_LOW (arg);
8121 /* Clear all the bits that are beyond the type's precision. */
8122 if (width > HOST_BITS_PER_WIDE_INT)
8124 hi = TREE_INT_CST_HIGH (arg);
8125 if (width < HOST_BITS_PER_DOUBLE_INT)
8126 hi &= ~(HOST_WIDE_INT_M1U << (width - HOST_BITS_PER_WIDE_INT));
8128 else
8130 hi = 0;
8131 if (width < HOST_BITS_PER_WIDE_INT)
8132 lo &= ~(HOST_WIDE_INT_M1U << width);
8135 switch (DECL_FUNCTION_CODE (fndecl))
8137 CASE_INT_FN (BUILT_IN_FFS):
8138 if (lo != 0)
8139 result = ffs_hwi (lo);
8140 else if (hi != 0)
8141 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8142 else
8143 result = 0;
8144 break;
8146 CASE_INT_FN (BUILT_IN_CLZ):
8147 if (hi != 0)
8148 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8149 else if (lo != 0)
8150 result = width - floor_log2 (lo) - 1;
8151 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8152 result = width;
8153 break;
8155 CASE_INT_FN (BUILT_IN_CTZ):
8156 if (lo != 0)
8157 result = ctz_hwi (lo);
8158 else if (hi != 0)
8159 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8160 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8161 result = width;
8162 break;
8164 CASE_INT_FN (BUILT_IN_CLRSB):
8165 if (width > 2 * HOST_BITS_PER_WIDE_INT)
8166 return NULL_TREE;
8167 if (width > HOST_BITS_PER_WIDE_INT
8168 && (hi & ((unsigned HOST_WIDE_INT) 1
8169 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8171 hi = ~hi & ~(HOST_WIDE_INT_M1U
8172 << (width - HOST_BITS_PER_WIDE_INT - 1));
8173 lo = ~lo;
8175 else if (width <= HOST_BITS_PER_WIDE_INT
8176 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8177 lo = ~lo & ~(HOST_WIDE_INT_M1U << (width - 1));
8178 if (hi != 0)
8179 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8180 else if (lo != 0)
8181 result = width - floor_log2 (lo) - 2;
8182 else
8183 result = width - 1;
8184 break;
8186 CASE_INT_FN (BUILT_IN_POPCOUNT):
8187 result = 0;
8188 while (lo)
8189 result++, lo &= lo - 1;
8190 while (hi)
8191 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8192 break;
8194 CASE_INT_FN (BUILT_IN_PARITY):
8195 result = 0;
8196 while (lo)
8197 result++, lo &= lo - 1;
8198 while (hi)
8199 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8200 result &= 1;
8201 break;
8203 default:
8204 gcc_unreachable ();
8207 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8210 return NULL_TREE;
8213 /* Fold function call to builtin_bswap and the short, long and long long
8214 variants. Return NULL_TREE if no simplification can be made. */
8215 static tree
8216 fold_builtin_bswap (tree fndecl, tree arg)
8218 if (! validate_arg (arg, INTEGER_TYPE))
8219 return NULL_TREE;
8221 /* Optimize constant value. */
8222 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8224 HOST_WIDE_INT hi, width, r_hi = 0;
8225 unsigned HOST_WIDE_INT lo, r_lo = 0;
8226 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8228 width = TYPE_PRECISION (type);
8229 lo = TREE_INT_CST_LOW (arg);
8230 hi = TREE_INT_CST_HIGH (arg);
8232 switch (DECL_FUNCTION_CODE (fndecl))
8234 case BUILT_IN_BSWAP16:
8235 case BUILT_IN_BSWAP32:
8236 case BUILT_IN_BSWAP64:
8238 int s;
8240 for (s = 0; s < width; s += 8)
8242 int d = width - s - 8;
8243 unsigned HOST_WIDE_INT byte;
8245 if (s < HOST_BITS_PER_WIDE_INT)
8246 byte = (lo >> s) & 0xff;
8247 else
8248 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8250 if (d < HOST_BITS_PER_WIDE_INT)
8251 r_lo |= byte << d;
8252 else
8253 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8257 break;
8259 default:
8260 gcc_unreachable ();
8263 if (width < HOST_BITS_PER_WIDE_INT)
8264 return build_int_cst (type, r_lo);
8265 else
8266 return build_int_cst_wide (type, r_lo, r_hi);
8269 return NULL_TREE;
8272 /* A subroutine of fold_builtin to fold the various logarithmic
8273 functions. Return NULL_TREE if no simplification can me made.
8274 FUNC is the corresponding MPFR logarithm function. */
8276 static tree
8277 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8278 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8280 if (validate_arg (arg, REAL_TYPE))
8282 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8283 tree res;
8284 const enum built_in_function fcode = builtin_mathfn_code (arg);
8286 /* Calculate the result when the argument is a constant. */
8287 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8288 return res;
8290 /* Special case, optimize logN(expN(x)) = x. */
8291 if (flag_unsafe_math_optimizations
8292 && ((func == mpfr_log
8293 && (fcode == BUILT_IN_EXP
8294 || fcode == BUILT_IN_EXPF
8295 || fcode == BUILT_IN_EXPL))
8296 || (func == mpfr_log2
8297 && (fcode == BUILT_IN_EXP2
8298 || fcode == BUILT_IN_EXP2F
8299 || fcode == BUILT_IN_EXP2L))
8300 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8301 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8303 /* Optimize logN(func()) for various exponential functions. We
8304 want to determine the value "x" and the power "exponent" in
8305 order to transform logN(x**exponent) into exponent*logN(x). */
8306 if (flag_unsafe_math_optimizations)
8308 tree exponent = 0, x = 0;
8310 switch (fcode)
8312 CASE_FLT_FN (BUILT_IN_EXP):
8313 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8314 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8315 dconst_e ()));
8316 exponent = CALL_EXPR_ARG (arg, 0);
8317 break;
8318 CASE_FLT_FN (BUILT_IN_EXP2):
8319 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8320 x = build_real (type, dconst2);
8321 exponent = CALL_EXPR_ARG (arg, 0);
8322 break;
8323 CASE_FLT_FN (BUILT_IN_EXP10):
8324 CASE_FLT_FN (BUILT_IN_POW10):
8325 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8327 REAL_VALUE_TYPE dconst10;
8328 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8329 x = build_real (type, dconst10);
8331 exponent = CALL_EXPR_ARG (arg, 0);
8332 break;
8333 CASE_FLT_FN (BUILT_IN_SQRT):
8334 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8335 x = CALL_EXPR_ARG (arg, 0);
8336 exponent = build_real (type, dconsthalf);
8337 break;
8338 CASE_FLT_FN (BUILT_IN_CBRT):
8339 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8340 x = CALL_EXPR_ARG (arg, 0);
8341 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8342 dconst_third ()));
8343 break;
8344 CASE_FLT_FN (BUILT_IN_POW):
8345 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8346 x = CALL_EXPR_ARG (arg, 0);
8347 exponent = CALL_EXPR_ARG (arg, 1);
8348 break;
8349 default:
8350 break;
8353 /* Now perform the optimization. */
8354 if (x && exponent)
8356 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8357 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8362 return NULL_TREE;
8365 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8366 NULL_TREE if no simplification can be made. */
8368 static tree
8369 fold_builtin_hypot (location_t loc, tree fndecl,
8370 tree arg0, tree arg1, tree type)
8372 tree res, narg0, narg1;
8374 if (!validate_arg (arg0, REAL_TYPE)
8375 || !validate_arg (arg1, REAL_TYPE))
8376 return NULL_TREE;
8378 /* Calculate the result when the argument is a constant. */
8379 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8380 return res;
8382 /* If either argument to hypot has a negate or abs, strip that off.
8383 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8384 narg0 = fold_strip_sign_ops (arg0);
8385 narg1 = fold_strip_sign_ops (arg1);
8386 if (narg0 || narg1)
8388 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8389 narg1 ? narg1 : arg1);
8392 /* If either argument is zero, hypot is fabs of the other. */
8393 if (real_zerop (arg0))
8394 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8395 else if (real_zerop (arg1))
8396 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8398 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8399 if (flag_unsafe_math_optimizations
8400 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8402 const REAL_VALUE_TYPE sqrt2_trunc
8403 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8404 return fold_build2_loc (loc, MULT_EXPR, type,
8405 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8406 build_real (type, sqrt2_trunc));
8409 return NULL_TREE;
8413 /* Fold a builtin function call to pow, powf, or powl. Return
8414 NULL_TREE if no simplification can be made. */
8415 static tree
8416 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8418 tree res;
8420 if (!validate_arg (arg0, REAL_TYPE)
8421 || !validate_arg (arg1, REAL_TYPE))
8422 return NULL_TREE;
8424 /* Calculate the result when the argument is a constant. */
8425 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8426 return res;
8428 /* Optimize pow(1.0,y) = 1.0. */
8429 if (real_onep (arg0))
8430 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8432 if (TREE_CODE (arg1) == REAL_CST
8433 && !TREE_OVERFLOW (arg1))
8435 REAL_VALUE_TYPE cint;
8436 REAL_VALUE_TYPE c;
8437 HOST_WIDE_INT n;
8439 c = TREE_REAL_CST (arg1);
8441 /* Optimize pow(x,0.0) = 1.0. */
8442 if (REAL_VALUES_EQUAL (c, dconst0))
8443 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8444 arg0);
8446 /* Optimize pow(x,1.0) = x. */
8447 if (REAL_VALUES_EQUAL (c, dconst1))
8448 return arg0;
8450 /* Optimize pow(x,-1.0) = 1.0/x. */
8451 if (REAL_VALUES_EQUAL (c, dconstm1))
8452 return fold_build2_loc (loc, RDIV_EXPR, type,
8453 build_real (type, dconst1), arg0);
8455 /* Optimize pow(x,0.5) = sqrt(x). */
8456 if (flag_unsafe_math_optimizations
8457 && REAL_VALUES_EQUAL (c, dconsthalf))
8459 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8461 if (sqrtfn != NULL_TREE)
8462 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8465 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8466 if (flag_unsafe_math_optimizations)
8468 const REAL_VALUE_TYPE dconstroot
8469 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8471 if (REAL_VALUES_EQUAL (c, dconstroot))
8473 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8474 if (cbrtfn != NULL_TREE)
8475 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8479 /* Check for an integer exponent. */
8480 n = real_to_integer (&c);
8481 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8482 if (real_identical (&c, &cint))
8484 /* Attempt to evaluate pow at compile-time, unless this should
8485 raise an exception. */
8486 if (TREE_CODE (arg0) == REAL_CST
8487 && !TREE_OVERFLOW (arg0)
8488 && (n > 0
8489 || (!flag_trapping_math && !flag_errno_math)
8490 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8492 REAL_VALUE_TYPE x;
8493 bool inexact;
8495 x = TREE_REAL_CST (arg0);
8496 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8497 if (flag_unsafe_math_optimizations || !inexact)
8498 return build_real (type, x);
8501 /* Strip sign ops from even integer powers. */
8502 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8504 tree narg0 = fold_strip_sign_ops (arg0);
8505 if (narg0)
8506 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8511 if (flag_unsafe_math_optimizations)
8513 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8515 /* Optimize pow(expN(x),y) = expN(x*y). */
8516 if (BUILTIN_EXPONENT_P (fcode))
8518 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8519 tree arg = CALL_EXPR_ARG (arg0, 0);
8520 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8521 return build_call_expr_loc (loc, expfn, 1, arg);
8524 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8525 if (BUILTIN_SQRT_P (fcode))
8527 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8528 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8529 build_real (type, dconsthalf));
8530 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8533 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8534 if (BUILTIN_CBRT_P (fcode))
8536 tree arg = CALL_EXPR_ARG (arg0, 0);
8537 if (tree_expr_nonnegative_p (arg))
8539 const REAL_VALUE_TYPE dconstroot
8540 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8541 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8542 build_real (type, dconstroot));
8543 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8547 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8548 if (fcode == BUILT_IN_POW
8549 || fcode == BUILT_IN_POWF
8550 || fcode == BUILT_IN_POWL)
8552 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8553 if (tree_expr_nonnegative_p (arg00))
8555 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8556 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8557 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8562 return NULL_TREE;
8565 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8566 Return NULL_TREE if no simplification can be made. */
8567 static tree
8568 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8569 tree arg0, tree arg1, tree type)
8571 if (!validate_arg (arg0, REAL_TYPE)
8572 || !validate_arg (arg1, INTEGER_TYPE))
8573 return NULL_TREE;
8575 /* Optimize pow(1.0,y) = 1.0. */
8576 if (real_onep (arg0))
8577 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8579 if (tree_fits_shwi_p (arg1))
8581 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8583 /* Evaluate powi at compile-time. */
8584 if (TREE_CODE (arg0) == REAL_CST
8585 && !TREE_OVERFLOW (arg0))
8587 REAL_VALUE_TYPE x;
8588 x = TREE_REAL_CST (arg0);
8589 real_powi (&x, TYPE_MODE (type), &x, c);
8590 return build_real (type, x);
8593 /* Optimize pow(x,0) = 1.0. */
8594 if (c == 0)
8595 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8596 arg0);
8598 /* Optimize pow(x,1) = x. */
8599 if (c == 1)
8600 return arg0;
8602 /* Optimize pow(x,-1) = 1.0/x. */
8603 if (c == -1)
8604 return fold_build2_loc (loc, RDIV_EXPR, type,
8605 build_real (type, dconst1), arg0);
8608 return NULL_TREE;
8611 /* A subroutine of fold_builtin to fold the various exponent
8612 functions. Return NULL_TREE if no simplification can be made.
8613 FUNC is the corresponding MPFR exponent function. */
8615 static tree
8616 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8617 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8619 if (validate_arg (arg, REAL_TYPE))
8621 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8622 tree res;
8624 /* Calculate the result when the argument is a constant. */
8625 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8626 return res;
8628 /* Optimize expN(logN(x)) = x. */
8629 if (flag_unsafe_math_optimizations)
8631 const enum built_in_function fcode = builtin_mathfn_code (arg);
8633 if ((func == mpfr_exp
8634 && (fcode == BUILT_IN_LOG
8635 || fcode == BUILT_IN_LOGF
8636 || fcode == BUILT_IN_LOGL))
8637 || (func == mpfr_exp2
8638 && (fcode == BUILT_IN_LOG2
8639 || fcode == BUILT_IN_LOG2F
8640 || fcode == BUILT_IN_LOG2L))
8641 || (func == mpfr_exp10
8642 && (fcode == BUILT_IN_LOG10
8643 || fcode == BUILT_IN_LOG10F
8644 || fcode == BUILT_IN_LOG10L)))
8645 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8649 return NULL_TREE;
8652 /* Return true if VAR is a VAR_DECL or a component thereof. */
8654 static bool
8655 var_decl_component_p (tree var)
8657 tree inner = var;
8658 while (handled_component_p (inner))
8659 inner = TREE_OPERAND (inner, 0);
8660 return SSA_VAR_P (inner);
8663 /* Fold function call to builtin memset. Return
8664 NULL_TREE if no simplification can be made. */
8666 static tree
8667 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8668 tree type, bool ignore)
8670 tree var, ret, etype;
8671 unsigned HOST_WIDE_INT length, cval;
8673 if (! validate_arg (dest, POINTER_TYPE)
8674 || ! validate_arg (c, INTEGER_TYPE)
8675 || ! validate_arg (len, INTEGER_TYPE))
8676 return NULL_TREE;
8678 if (! tree_fits_uhwi_p (len))
8679 return NULL_TREE;
8681 /* If the LEN parameter is zero, return DEST. */
8682 if (integer_zerop (len))
8683 return omit_one_operand_loc (loc, type, dest, c);
8685 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8686 return NULL_TREE;
8688 var = dest;
8689 STRIP_NOPS (var);
8690 if (TREE_CODE (var) != ADDR_EXPR)
8691 return NULL_TREE;
8693 var = TREE_OPERAND (var, 0);
8694 if (TREE_THIS_VOLATILE (var))
8695 return NULL_TREE;
8697 etype = TREE_TYPE (var);
8698 if (TREE_CODE (etype) == ARRAY_TYPE)
8699 etype = TREE_TYPE (etype);
8701 if (!INTEGRAL_TYPE_P (etype)
8702 && !POINTER_TYPE_P (etype))
8703 return NULL_TREE;
8705 if (! var_decl_component_p (var))
8706 return NULL_TREE;
8708 length = tree_to_uhwi (len);
8709 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8710 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8711 return NULL_TREE;
8713 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8714 return NULL_TREE;
8716 if (integer_zerop (c))
8717 cval = 0;
8718 else
8720 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8721 return NULL_TREE;
8723 cval = TREE_INT_CST_LOW (c);
8724 cval &= 0xff;
8725 cval |= cval << 8;
8726 cval |= cval << 16;
8727 cval |= (cval << 31) << 1;
8730 ret = build_int_cst_type (etype, cval);
8731 var = build_fold_indirect_ref_loc (loc,
8732 fold_convert_loc (loc,
8733 build_pointer_type (etype),
8734 dest));
8735 ret = build2 (MODIFY_EXPR, etype, var, ret);
8736 if (ignore)
8737 return ret;
8739 return omit_one_operand_loc (loc, type, dest, ret);
8742 /* Fold function call to builtin memset. Return
8743 NULL_TREE if no simplification can be made. */
8745 static tree
8746 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8748 if (! validate_arg (dest, POINTER_TYPE)
8749 || ! validate_arg (size, INTEGER_TYPE))
8750 return NULL_TREE;
8752 if (!ignore)
8753 return NULL_TREE;
8755 /* New argument list transforming bzero(ptr x, int y) to
8756 memset(ptr x, int 0, size_t y). This is done this way
8757 so that if it isn't expanded inline, we fallback to
8758 calling bzero instead of memset. */
8760 return fold_builtin_memset (loc, dest, integer_zero_node,
8761 fold_convert_loc (loc, size_type_node, size),
8762 void_type_node, ignore);
8765 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8766 NULL_TREE if no simplification can be made.
8767 If ENDP is 0, return DEST (like memcpy).
8768 If ENDP is 1, return DEST+LEN (like mempcpy).
8769 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8770 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8771 (memmove). */
8773 static tree
8774 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8775 tree len, tree type, bool ignore, int endp)
8777 tree destvar, srcvar, expr;
8779 if (! validate_arg (dest, POINTER_TYPE)
8780 || ! validate_arg (src, POINTER_TYPE)
8781 || ! validate_arg (len, INTEGER_TYPE))
8782 return NULL_TREE;
8784 /* If the LEN parameter is zero, return DEST. */
8785 if (integer_zerop (len))
8786 return omit_one_operand_loc (loc, type, dest, src);
8788 /* If SRC and DEST are the same (and not volatile), return
8789 DEST{,+LEN,+LEN-1}. */
8790 if (operand_equal_p (src, dest, 0))
8791 expr = len;
8792 else
8794 tree srctype, desttype;
8795 unsigned int src_align, dest_align;
8796 tree off0;
8798 if (endp == 3)
8800 src_align = get_pointer_alignment (src);
8801 dest_align = get_pointer_alignment (dest);
8803 /* Both DEST and SRC must be pointer types.
8804 ??? This is what old code did. Is the testing for pointer types
8805 really mandatory?
8807 If either SRC is readonly or length is 1, we can use memcpy. */
8808 if (!dest_align || !src_align)
8809 return NULL_TREE;
8810 if (readonly_data_expr (src)
8811 || (tree_fits_uhwi_p (len)
8812 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8813 >= tree_to_uhwi (len))))
8815 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8816 if (!fn)
8817 return NULL_TREE;
8818 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8821 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8822 if (TREE_CODE (src) == ADDR_EXPR
8823 && TREE_CODE (dest) == ADDR_EXPR)
8825 tree src_base, dest_base, fn;
8826 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8827 HOST_WIDE_INT size = -1;
8828 HOST_WIDE_INT maxsize = -1;
8830 srcvar = TREE_OPERAND (src, 0);
8831 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8832 &size, &maxsize);
8833 destvar = TREE_OPERAND (dest, 0);
8834 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8835 &size, &maxsize);
8836 if (tree_fits_uhwi_p (len))
8837 maxsize = tree_to_uhwi (len);
8838 else
8839 maxsize = -1;
8840 src_offset /= BITS_PER_UNIT;
8841 dest_offset /= BITS_PER_UNIT;
8842 if (SSA_VAR_P (src_base)
8843 && SSA_VAR_P (dest_base))
8845 if (operand_equal_p (src_base, dest_base, 0)
8846 && ranges_overlap_p (src_offset, maxsize,
8847 dest_offset, maxsize))
8848 return NULL_TREE;
8850 else if (TREE_CODE (src_base) == MEM_REF
8851 && TREE_CODE (dest_base) == MEM_REF)
8853 double_int off;
8854 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8855 TREE_OPERAND (dest_base, 0), 0))
8856 return NULL_TREE;
8857 off = mem_ref_offset (src_base) +
8858 double_int::from_shwi (src_offset);
8859 if (!off.fits_shwi ())
8860 return NULL_TREE;
8861 src_offset = off.low;
8862 off = mem_ref_offset (dest_base) +
8863 double_int::from_shwi (dest_offset);
8864 if (!off.fits_shwi ())
8865 return NULL_TREE;
8866 dest_offset = off.low;
8867 if (ranges_overlap_p (src_offset, maxsize,
8868 dest_offset, maxsize))
8869 return NULL_TREE;
8871 else
8872 return NULL_TREE;
8874 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8875 if (!fn)
8876 return NULL_TREE;
8877 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8880 /* If the destination and source do not alias optimize into
8881 memcpy as well. */
8882 if ((is_gimple_min_invariant (dest)
8883 || TREE_CODE (dest) == SSA_NAME)
8884 && (is_gimple_min_invariant (src)
8885 || TREE_CODE (src) == SSA_NAME))
8887 ao_ref destr, srcr;
8888 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8889 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8890 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8892 tree fn;
8893 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8894 if (!fn)
8895 return NULL_TREE;
8896 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8900 return NULL_TREE;
8903 if (!tree_fits_shwi_p (len))
8904 return NULL_TREE;
8905 /* FIXME:
8906 This logic lose for arguments like (type *)malloc (sizeof (type)),
8907 since we strip the casts of up to VOID return value from malloc.
8908 Perhaps we ought to inherit type from non-VOID argument here? */
8909 STRIP_NOPS (src);
8910 STRIP_NOPS (dest);
8911 if (!POINTER_TYPE_P (TREE_TYPE (src))
8912 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8913 return NULL_TREE;
8914 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8915 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8917 tree tem = TREE_OPERAND (src, 0);
8918 STRIP_NOPS (tem);
8919 if (tem != TREE_OPERAND (src, 0))
8920 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8922 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8924 tree tem = TREE_OPERAND (dest, 0);
8925 STRIP_NOPS (tem);
8926 if (tem != TREE_OPERAND (dest, 0))
8927 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8929 srctype = TREE_TYPE (TREE_TYPE (src));
8930 if (TREE_CODE (srctype) == ARRAY_TYPE
8931 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8933 srctype = TREE_TYPE (srctype);
8934 STRIP_NOPS (src);
8935 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8937 desttype = TREE_TYPE (TREE_TYPE (dest));
8938 if (TREE_CODE (desttype) == ARRAY_TYPE
8939 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8941 desttype = TREE_TYPE (desttype);
8942 STRIP_NOPS (dest);
8943 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8945 if (TREE_ADDRESSABLE (srctype)
8946 || TREE_ADDRESSABLE (desttype))
8947 return NULL_TREE;
8949 src_align = get_pointer_alignment (src);
8950 dest_align = get_pointer_alignment (dest);
8951 if (dest_align < TYPE_ALIGN (desttype)
8952 || src_align < TYPE_ALIGN (srctype))
8953 return NULL_TREE;
8955 if (!ignore)
8956 dest = builtin_save_expr (dest);
8958 /* Build accesses at offset zero with a ref-all character type. */
8959 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8960 ptr_mode, true), 0);
8962 destvar = dest;
8963 STRIP_NOPS (destvar);
8964 if (TREE_CODE (destvar) == ADDR_EXPR
8965 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8966 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8967 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8968 else
8969 destvar = NULL_TREE;
8971 srcvar = src;
8972 STRIP_NOPS (srcvar);
8973 if (TREE_CODE (srcvar) == ADDR_EXPR
8974 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8975 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8977 if (!destvar
8978 || src_align >= TYPE_ALIGN (desttype))
8979 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8980 srcvar, off0);
8981 else if (!STRICT_ALIGNMENT)
8983 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8984 src_align);
8985 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8987 else
8988 srcvar = NULL_TREE;
8990 else
8991 srcvar = NULL_TREE;
8993 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8994 return NULL_TREE;
8996 if (srcvar == NULL_TREE)
8998 STRIP_NOPS (src);
8999 if (src_align >= TYPE_ALIGN (desttype))
9000 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
9001 else
9003 if (STRICT_ALIGNMENT)
9004 return NULL_TREE;
9005 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
9006 src_align);
9007 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
9010 else if (destvar == NULL_TREE)
9012 STRIP_NOPS (dest);
9013 if (dest_align >= TYPE_ALIGN (srctype))
9014 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
9015 else
9017 if (STRICT_ALIGNMENT)
9018 return NULL_TREE;
9019 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
9020 dest_align);
9021 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
9025 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
9028 if (ignore)
9029 return expr;
9031 if (endp == 0 || endp == 3)
9032 return omit_one_operand_loc (loc, type, dest, expr);
9034 if (expr == len)
9035 expr = NULL_TREE;
9037 if (endp == 2)
9038 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
9039 ssize_int (1));
9041 dest = fold_build_pointer_plus_loc (loc, dest, len);
9042 dest = fold_convert_loc (loc, type, dest);
9043 if (expr)
9044 dest = omit_one_operand_loc (loc, type, dest, expr);
9045 return dest;
9048 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9049 If LEN is not NULL, it represents the length of the string to be
9050 copied. Return NULL_TREE if no simplification can be made. */
9052 tree
9053 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
9055 tree fn;
9057 if (!validate_arg (dest, POINTER_TYPE)
9058 || !validate_arg (src, POINTER_TYPE))
9059 return NULL_TREE;
9061 /* If SRC and DEST are the same (and not volatile), return DEST. */
9062 if (operand_equal_p (src, dest, 0))
9063 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9065 if (optimize_function_for_size_p (cfun))
9066 return NULL_TREE;
9068 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9069 if (!fn)
9070 return NULL_TREE;
9072 if (!len)
9074 len = c_strlen (src, 1);
9075 if (! len || TREE_SIDE_EFFECTS (len))
9076 return NULL_TREE;
9079 len = fold_convert_loc (loc, size_type_node, len);
9080 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9081 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9082 build_call_expr_loc (loc, fn, 3, dest, src, len));
9085 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9086 Return NULL_TREE if no simplification can be made. */
9088 static tree
9089 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9091 tree fn, len, lenp1, call, type;
9093 if (!validate_arg (dest, POINTER_TYPE)
9094 || !validate_arg (src, POINTER_TYPE))
9095 return NULL_TREE;
9097 len = c_strlen (src, 1);
9098 if (!len
9099 || TREE_CODE (len) != INTEGER_CST)
9100 return NULL_TREE;
9102 if (optimize_function_for_size_p (cfun)
9103 /* If length is zero it's small enough. */
9104 && !integer_zerop (len))
9105 return NULL_TREE;
9107 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9108 if (!fn)
9109 return NULL_TREE;
9111 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9112 fold_convert_loc (loc, size_type_node, len),
9113 build_int_cst (size_type_node, 1));
9114 /* We use dest twice in building our expression. Save it from
9115 multiple expansions. */
9116 dest = builtin_save_expr (dest);
9117 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9119 type = TREE_TYPE (TREE_TYPE (fndecl));
9120 dest = fold_build_pointer_plus_loc (loc, dest, len);
9121 dest = fold_convert_loc (loc, type, dest);
9122 dest = omit_one_operand_loc (loc, type, dest, call);
9123 return dest;
9126 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9127 If SLEN is not NULL, it represents the length of the source string.
9128 Return NULL_TREE if no simplification can be made. */
9130 tree
9131 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9132 tree src, tree len, tree slen)
9134 tree fn;
9136 if (!validate_arg (dest, POINTER_TYPE)
9137 || !validate_arg (src, POINTER_TYPE)
9138 || !validate_arg (len, INTEGER_TYPE))
9139 return NULL_TREE;
9141 /* If the LEN parameter is zero, return DEST. */
9142 if (integer_zerop (len))
9143 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9145 /* We can't compare slen with len as constants below if len is not a
9146 constant. */
9147 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9148 return NULL_TREE;
9150 if (!slen)
9151 slen = c_strlen (src, 1);
9153 /* Now, we must be passed a constant src ptr parameter. */
9154 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9155 return NULL_TREE;
9157 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9159 /* We do not support simplification of this case, though we do
9160 support it when expanding trees into RTL. */
9161 /* FIXME: generate a call to __builtin_memset. */
9162 if (tree_int_cst_lt (slen, len))
9163 return NULL_TREE;
9165 /* OK transform into builtin memcpy. */
9166 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9167 if (!fn)
9168 return NULL_TREE;
9170 len = fold_convert_loc (loc, size_type_node, len);
9171 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9172 build_call_expr_loc (loc, fn, 3, dest, src, len));
9175 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9176 arguments to the call, and TYPE is its return type.
9177 Return NULL_TREE if no simplification can be made. */
9179 static tree
9180 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9182 if (!validate_arg (arg1, POINTER_TYPE)
9183 || !validate_arg (arg2, INTEGER_TYPE)
9184 || !validate_arg (len, INTEGER_TYPE))
9185 return NULL_TREE;
9186 else
9188 const char *p1;
9190 if (TREE_CODE (arg2) != INTEGER_CST
9191 || !tree_fits_uhwi_p (len))
9192 return NULL_TREE;
9194 p1 = c_getstr (arg1);
9195 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9197 char c;
9198 const char *r;
9199 tree tem;
9201 if (target_char_cast (arg2, &c))
9202 return NULL_TREE;
9204 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
9206 if (r == NULL)
9207 return build_int_cst (TREE_TYPE (arg1), 0);
9209 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9210 return fold_convert_loc (loc, type, tem);
9212 return NULL_TREE;
9216 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9217 Return NULL_TREE if no simplification can be made. */
9219 static tree
9220 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9222 const char *p1, *p2;
9224 if (!validate_arg (arg1, POINTER_TYPE)
9225 || !validate_arg (arg2, POINTER_TYPE)
9226 || !validate_arg (len, INTEGER_TYPE))
9227 return NULL_TREE;
9229 /* If the LEN parameter is zero, return zero. */
9230 if (integer_zerop (len))
9231 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9232 arg1, arg2);
9234 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9235 if (operand_equal_p (arg1, arg2, 0))
9236 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9238 p1 = c_getstr (arg1);
9239 p2 = c_getstr (arg2);
9241 /* If all arguments are constant, and the value of len is not greater
9242 than the lengths of arg1 and arg2, evaluate at compile-time. */
9243 if (tree_fits_uhwi_p (len) && p1 && p2
9244 && compare_tree_int (len, strlen (p1) + 1) <= 0
9245 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9247 const int r = memcmp (p1, p2, tree_to_uhwi (len));
9249 if (r > 0)
9250 return integer_one_node;
9251 else if (r < 0)
9252 return integer_minus_one_node;
9253 else
9254 return integer_zero_node;
9257 /* If len parameter is one, return an expression corresponding to
9258 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9259 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9261 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9262 tree cst_uchar_ptr_node
9263 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9265 tree ind1
9266 = fold_convert_loc (loc, integer_type_node,
9267 build1 (INDIRECT_REF, cst_uchar_node,
9268 fold_convert_loc (loc,
9269 cst_uchar_ptr_node,
9270 arg1)));
9271 tree ind2
9272 = fold_convert_loc (loc, integer_type_node,
9273 build1 (INDIRECT_REF, cst_uchar_node,
9274 fold_convert_loc (loc,
9275 cst_uchar_ptr_node,
9276 arg2)));
9277 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9280 return NULL_TREE;
9283 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9284 Return NULL_TREE if no simplification can be made. */
9286 static tree
9287 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9289 const char *p1, *p2;
9291 if (!validate_arg (arg1, POINTER_TYPE)
9292 || !validate_arg (arg2, POINTER_TYPE))
9293 return NULL_TREE;
9295 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9296 if (operand_equal_p (arg1, arg2, 0))
9297 return integer_zero_node;
9299 p1 = c_getstr (arg1);
9300 p2 = c_getstr (arg2);
9302 if (p1 && p2)
9304 const int i = strcmp (p1, p2);
9305 if (i < 0)
9306 return integer_minus_one_node;
9307 else if (i > 0)
9308 return integer_one_node;
9309 else
9310 return integer_zero_node;
9313 /* If the second arg is "", return *(const unsigned char*)arg1. */
9314 if (p2 && *p2 == '\0')
9316 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9317 tree cst_uchar_ptr_node
9318 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9320 return fold_convert_loc (loc, integer_type_node,
9321 build1 (INDIRECT_REF, cst_uchar_node,
9322 fold_convert_loc (loc,
9323 cst_uchar_ptr_node,
9324 arg1)));
9327 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9328 if (p1 && *p1 == '\0')
9330 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9331 tree cst_uchar_ptr_node
9332 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9334 tree temp
9335 = fold_convert_loc (loc, integer_type_node,
9336 build1 (INDIRECT_REF, cst_uchar_node,
9337 fold_convert_loc (loc,
9338 cst_uchar_ptr_node,
9339 arg2)));
9340 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9343 return NULL_TREE;
9346 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9347 Return NULL_TREE if no simplification can be made. */
9349 static tree
9350 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9352 const char *p1, *p2;
9354 if (!validate_arg (arg1, POINTER_TYPE)
9355 || !validate_arg (arg2, POINTER_TYPE)
9356 || !validate_arg (len, INTEGER_TYPE))
9357 return NULL_TREE;
9359 /* If the LEN parameter is zero, return zero. */
9360 if (integer_zerop (len))
9361 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9362 arg1, arg2);
9364 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9365 if (operand_equal_p (arg1, arg2, 0))
9366 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9368 p1 = c_getstr (arg1);
9369 p2 = c_getstr (arg2);
9371 if (tree_fits_uhwi_p (len) && p1 && p2)
9373 const int i = strncmp (p1, p2, tree_to_uhwi (len));
9374 if (i > 0)
9375 return integer_one_node;
9376 else if (i < 0)
9377 return integer_minus_one_node;
9378 else
9379 return integer_zero_node;
9382 /* If the second arg is "", and the length is greater than zero,
9383 return *(const unsigned char*)arg1. */
9384 if (p2 && *p2 == '\0'
9385 && TREE_CODE (len) == INTEGER_CST
9386 && tree_int_cst_sgn (len) == 1)
9388 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9389 tree cst_uchar_ptr_node
9390 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9392 return fold_convert_loc (loc, integer_type_node,
9393 build1 (INDIRECT_REF, cst_uchar_node,
9394 fold_convert_loc (loc,
9395 cst_uchar_ptr_node,
9396 arg1)));
9399 /* If the first arg is "", and the length is greater than zero,
9400 return -*(const unsigned char*)arg2. */
9401 if (p1 && *p1 == '\0'
9402 && TREE_CODE (len) == INTEGER_CST
9403 && tree_int_cst_sgn (len) == 1)
9405 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9406 tree cst_uchar_ptr_node
9407 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9409 tree temp = fold_convert_loc (loc, integer_type_node,
9410 build1 (INDIRECT_REF, cst_uchar_node,
9411 fold_convert_loc (loc,
9412 cst_uchar_ptr_node,
9413 arg2)));
9414 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9417 /* If len parameter is one, return an expression corresponding to
9418 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9419 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
9421 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9422 tree cst_uchar_ptr_node
9423 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9425 tree ind1 = fold_convert_loc (loc, integer_type_node,
9426 build1 (INDIRECT_REF, cst_uchar_node,
9427 fold_convert_loc (loc,
9428 cst_uchar_ptr_node,
9429 arg1)));
9430 tree ind2 = fold_convert_loc (loc, integer_type_node,
9431 build1 (INDIRECT_REF, cst_uchar_node,
9432 fold_convert_loc (loc,
9433 cst_uchar_ptr_node,
9434 arg2)));
9435 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9438 return NULL_TREE;
9441 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9442 ARG. Return NULL_TREE if no simplification can be made. */
9444 static tree
9445 fold_builtin_signbit (location_t loc, tree arg, tree type)
9447 if (!validate_arg (arg, REAL_TYPE))
9448 return NULL_TREE;
9450 /* If ARG is a compile-time constant, determine the result. */
9451 if (TREE_CODE (arg) == REAL_CST
9452 && !TREE_OVERFLOW (arg))
9454 REAL_VALUE_TYPE c;
9456 c = TREE_REAL_CST (arg);
9457 return (REAL_VALUE_NEGATIVE (c)
9458 ? build_one_cst (type)
9459 : build_zero_cst (type));
9462 /* If ARG is non-negative, the result is always zero. */
9463 if (tree_expr_nonnegative_p (arg))
9464 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9466 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9467 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9468 return fold_convert (type,
9469 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9470 build_real (TREE_TYPE (arg), dconst0)));
9472 return NULL_TREE;
9475 /* Fold function call to builtin copysign, copysignf or copysignl with
9476 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9477 be made. */
9479 static tree
9480 fold_builtin_copysign (location_t loc, tree fndecl,
9481 tree arg1, tree arg2, tree type)
9483 tree tem;
9485 if (!validate_arg (arg1, REAL_TYPE)
9486 || !validate_arg (arg2, REAL_TYPE))
9487 return NULL_TREE;
9489 /* copysign(X,X) is X. */
9490 if (operand_equal_p (arg1, arg2, 0))
9491 return fold_convert_loc (loc, type, arg1);
9493 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9494 if (TREE_CODE (arg1) == REAL_CST
9495 && TREE_CODE (arg2) == REAL_CST
9496 && !TREE_OVERFLOW (arg1)
9497 && !TREE_OVERFLOW (arg2))
9499 REAL_VALUE_TYPE c1, c2;
9501 c1 = TREE_REAL_CST (arg1);
9502 c2 = TREE_REAL_CST (arg2);
9503 /* c1.sign := c2.sign. */
9504 real_copysign (&c1, &c2);
9505 return build_real (type, c1);
9508 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9509 Remember to evaluate Y for side-effects. */
9510 if (tree_expr_nonnegative_p (arg2))
9511 return omit_one_operand_loc (loc, type,
9512 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9513 arg2);
9515 /* Strip sign changing operations for the first argument. */
9516 tem = fold_strip_sign_ops (arg1);
9517 if (tem)
9518 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9520 return NULL_TREE;
9523 /* Fold a call to builtin isascii with argument ARG. */
9525 static tree
9526 fold_builtin_isascii (location_t loc, tree arg)
9528 if (!validate_arg (arg, INTEGER_TYPE))
9529 return NULL_TREE;
9530 else
9532 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9533 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9534 build_int_cst (integer_type_node,
9535 ~ (unsigned HOST_WIDE_INT) 0x7f));
9536 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9537 arg, integer_zero_node);
9541 /* Fold a call to builtin toascii with argument ARG. */
9543 static tree
9544 fold_builtin_toascii (location_t loc, tree arg)
9546 if (!validate_arg (arg, INTEGER_TYPE))
9547 return NULL_TREE;
9549 /* Transform toascii(c) -> (c & 0x7f). */
9550 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9551 build_int_cst (integer_type_node, 0x7f));
9554 /* Fold a call to builtin isdigit with argument ARG. */
9556 static tree
9557 fold_builtin_isdigit (location_t loc, tree arg)
9559 if (!validate_arg (arg, INTEGER_TYPE))
9560 return NULL_TREE;
9561 else
9563 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9564 /* According to the C standard, isdigit is unaffected by locale.
9565 However, it definitely is affected by the target character set. */
9566 unsigned HOST_WIDE_INT target_digit0
9567 = lang_hooks.to_target_charset ('0');
9569 if (target_digit0 == 0)
9570 return NULL_TREE;
9572 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9573 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9574 build_int_cst (unsigned_type_node, target_digit0));
9575 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9576 build_int_cst (unsigned_type_node, 9));
9580 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9582 static tree
9583 fold_builtin_fabs (location_t loc, tree arg, tree type)
9585 if (!validate_arg (arg, REAL_TYPE))
9586 return NULL_TREE;
9588 arg = fold_convert_loc (loc, type, arg);
9589 if (TREE_CODE (arg) == REAL_CST)
9590 return fold_abs_const (arg, type);
9591 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9594 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9596 static tree
9597 fold_builtin_abs (location_t loc, tree arg, tree type)
9599 if (!validate_arg (arg, INTEGER_TYPE))
9600 return NULL_TREE;
9602 arg = fold_convert_loc (loc, type, arg);
9603 if (TREE_CODE (arg) == INTEGER_CST)
9604 return fold_abs_const (arg, type);
9605 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9608 /* Fold a fma operation with arguments ARG[012]. */
9610 tree
9611 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9612 tree type, tree arg0, tree arg1, tree arg2)
9614 if (TREE_CODE (arg0) == REAL_CST
9615 && TREE_CODE (arg1) == REAL_CST
9616 && TREE_CODE (arg2) == REAL_CST)
9617 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9619 return NULL_TREE;
9622 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9624 static tree
9625 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9627 if (validate_arg (arg0, REAL_TYPE)
9628 && validate_arg (arg1, REAL_TYPE)
9629 && validate_arg (arg2, REAL_TYPE))
9631 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9632 if (tem)
9633 return tem;
9635 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9636 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9637 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9639 return NULL_TREE;
9642 /* Fold a call to builtin fmin or fmax. */
9644 static tree
9645 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9646 tree type, bool max)
9648 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9650 /* Calculate the result when the argument is a constant. */
9651 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9653 if (res)
9654 return res;
9656 /* If either argument is NaN, return the other one. Avoid the
9657 transformation if we get (and honor) a signalling NaN. Using
9658 omit_one_operand() ensures we create a non-lvalue. */
9659 if (TREE_CODE (arg0) == REAL_CST
9660 && real_isnan (&TREE_REAL_CST (arg0))
9661 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9662 || ! TREE_REAL_CST (arg0).signalling))
9663 return omit_one_operand_loc (loc, type, arg1, arg0);
9664 if (TREE_CODE (arg1) == REAL_CST
9665 && real_isnan (&TREE_REAL_CST (arg1))
9666 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9667 || ! TREE_REAL_CST (arg1).signalling))
9668 return omit_one_operand_loc (loc, type, arg0, arg1);
9670 /* Transform fmin/fmax(x,x) -> x. */
9671 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9672 return omit_one_operand_loc (loc, type, arg0, arg1);
9674 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9675 functions to return the numeric arg if the other one is NaN.
9676 These tree codes don't honor that, so only transform if
9677 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9678 handled, so we don't have to worry about it either. */
9679 if (flag_finite_math_only)
9680 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9681 fold_convert_loc (loc, type, arg0),
9682 fold_convert_loc (loc, type, arg1));
9684 return NULL_TREE;
9687 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9689 static tree
9690 fold_builtin_carg (location_t loc, tree arg, tree type)
9692 if (validate_arg (arg, COMPLEX_TYPE)
9693 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9695 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9697 if (atan2_fn)
9699 tree new_arg = builtin_save_expr (arg);
9700 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9701 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9702 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9706 return NULL_TREE;
9709 /* Fold a call to builtin logb/ilogb. */
9711 static tree
9712 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9714 if (! validate_arg (arg, REAL_TYPE))
9715 return NULL_TREE;
9717 STRIP_NOPS (arg);
9719 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9721 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9723 switch (value->cl)
9725 case rvc_nan:
9726 case rvc_inf:
9727 /* If arg is Inf or NaN and we're logb, return it. */
9728 if (TREE_CODE (rettype) == REAL_TYPE)
9730 /* For logb(-Inf) we have to return +Inf. */
9731 if (real_isinf (value) && real_isneg (value))
9733 REAL_VALUE_TYPE tem;
9734 real_inf (&tem);
9735 return build_real (rettype, tem);
9737 return fold_convert_loc (loc, rettype, arg);
9739 /* Fall through... */
9740 case rvc_zero:
9741 /* Zero may set errno and/or raise an exception for logb, also
9742 for ilogb we don't know FP_ILOGB0. */
9743 return NULL_TREE;
9744 case rvc_normal:
9745 /* For normal numbers, proceed iff radix == 2. In GCC,
9746 normalized significands are in the range [0.5, 1.0). We
9747 want the exponent as if they were [1.0, 2.0) so get the
9748 exponent and subtract 1. */
9749 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9750 return fold_convert_loc (loc, rettype,
9751 build_int_cst (integer_type_node,
9752 REAL_EXP (value)-1));
9753 break;
9757 return NULL_TREE;
9760 /* Fold a call to builtin significand, if radix == 2. */
9762 static tree
9763 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9765 if (! validate_arg (arg, REAL_TYPE))
9766 return NULL_TREE;
9768 STRIP_NOPS (arg);
9770 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9772 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9774 switch (value->cl)
9776 case rvc_zero:
9777 case rvc_nan:
9778 case rvc_inf:
9779 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9780 return fold_convert_loc (loc, rettype, arg);
9781 case rvc_normal:
9782 /* For normal numbers, proceed iff radix == 2. */
9783 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9785 REAL_VALUE_TYPE result = *value;
9786 /* In GCC, normalized significands are in the range [0.5,
9787 1.0). We want them to be [1.0, 2.0) so set the
9788 exponent to 1. */
9789 SET_REAL_EXP (&result, 1);
9790 return build_real (rettype, result);
9792 break;
9796 return NULL_TREE;
9799 /* Fold a call to builtin frexp, we can assume the base is 2. */
9801 static tree
9802 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9804 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9805 return NULL_TREE;
9807 STRIP_NOPS (arg0);
9809 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9810 return NULL_TREE;
9812 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9814 /* Proceed if a valid pointer type was passed in. */
9815 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9817 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9818 tree frac, exp;
9820 switch (value->cl)
9822 case rvc_zero:
9823 /* For +-0, return (*exp = 0, +-0). */
9824 exp = integer_zero_node;
9825 frac = arg0;
9826 break;
9827 case rvc_nan:
9828 case rvc_inf:
9829 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9830 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9831 case rvc_normal:
9833 /* Since the frexp function always expects base 2, and in
9834 GCC normalized significands are already in the range
9835 [0.5, 1.0), we have exactly what frexp wants. */
9836 REAL_VALUE_TYPE frac_rvt = *value;
9837 SET_REAL_EXP (&frac_rvt, 0);
9838 frac = build_real (rettype, frac_rvt);
9839 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9841 break;
9842 default:
9843 gcc_unreachable ();
9846 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9847 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9848 TREE_SIDE_EFFECTS (arg1) = 1;
9849 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9852 return NULL_TREE;
9855 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9856 then we can assume the base is two. If it's false, then we have to
9857 check the mode of the TYPE parameter in certain cases. */
9859 static tree
9860 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9861 tree type, bool ldexp)
9863 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9865 STRIP_NOPS (arg0);
9866 STRIP_NOPS (arg1);
9868 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9869 if (real_zerop (arg0) || integer_zerop (arg1)
9870 || (TREE_CODE (arg0) == REAL_CST
9871 && !real_isfinite (&TREE_REAL_CST (arg0))))
9872 return omit_one_operand_loc (loc, type, arg0, arg1);
9874 /* If both arguments are constant, then try to evaluate it. */
9875 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9876 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9877 && tree_fits_shwi_p (arg1))
9879 /* Bound the maximum adjustment to twice the range of the
9880 mode's valid exponents. Use abs to ensure the range is
9881 positive as a sanity check. */
9882 const long max_exp_adj = 2 *
9883 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9884 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9886 /* Get the user-requested adjustment. */
9887 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9889 /* The requested adjustment must be inside this range. This
9890 is a preliminary cap to avoid things like overflow, we
9891 may still fail to compute the result for other reasons. */
9892 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9894 REAL_VALUE_TYPE initial_result;
9896 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9898 /* Ensure we didn't overflow. */
9899 if (! real_isinf (&initial_result))
9901 const REAL_VALUE_TYPE trunc_result
9902 = real_value_truncate (TYPE_MODE (type), initial_result);
9904 /* Only proceed if the target mode can hold the
9905 resulting value. */
9906 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9907 return build_real (type, trunc_result);
9913 return NULL_TREE;
9916 /* Fold a call to builtin modf. */
9918 static tree
9919 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9921 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9922 return NULL_TREE;
9924 STRIP_NOPS (arg0);
9926 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9927 return NULL_TREE;
9929 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9931 /* Proceed if a valid pointer type was passed in. */
9932 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9934 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9935 REAL_VALUE_TYPE trunc, frac;
9937 switch (value->cl)
9939 case rvc_nan:
9940 case rvc_zero:
9941 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9942 trunc = frac = *value;
9943 break;
9944 case rvc_inf:
9945 /* For +-Inf, return (*arg1 = arg0, +-0). */
9946 frac = dconst0;
9947 frac.sign = value->sign;
9948 trunc = *value;
9949 break;
9950 case rvc_normal:
9951 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9952 real_trunc (&trunc, VOIDmode, value);
9953 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9954 /* If the original number was negative and already
9955 integral, then the fractional part is -0.0. */
9956 if (value->sign && frac.cl == rvc_zero)
9957 frac.sign = value->sign;
9958 break;
9961 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9962 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9963 build_real (rettype, trunc));
9964 TREE_SIDE_EFFECTS (arg1) = 1;
9965 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9966 build_real (rettype, frac));
9969 return NULL_TREE;
9972 /* Given a location LOC, an interclass builtin function decl FNDECL
9973 and its single argument ARG, return an folded expression computing
9974 the same, or NULL_TREE if we either couldn't or didn't want to fold
9975 (the latter happen if there's an RTL instruction available). */
9977 static tree
9978 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9980 enum machine_mode mode;
9982 if (!validate_arg (arg, REAL_TYPE))
9983 return NULL_TREE;
9985 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9986 return NULL_TREE;
9988 mode = TYPE_MODE (TREE_TYPE (arg));
9990 /* If there is no optab, try generic code. */
9991 switch (DECL_FUNCTION_CODE (fndecl))
9993 tree result;
9995 CASE_FLT_FN (BUILT_IN_ISINF):
9997 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9998 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9999 tree const type = TREE_TYPE (arg);
10000 REAL_VALUE_TYPE r;
10001 char buf[128];
10003 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10004 real_from_string (&r, buf);
10005 result = build_call_expr (isgr_fn, 2,
10006 fold_build1_loc (loc, ABS_EXPR, type, arg),
10007 build_real (type, r));
10008 return result;
10010 CASE_FLT_FN (BUILT_IN_FINITE):
10011 case BUILT_IN_ISFINITE:
10013 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
10014 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
10015 tree const type = TREE_TYPE (arg);
10016 REAL_VALUE_TYPE r;
10017 char buf[128];
10019 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10020 real_from_string (&r, buf);
10021 result = build_call_expr (isle_fn, 2,
10022 fold_build1_loc (loc, ABS_EXPR, type, arg),
10023 build_real (type, r));
10024 /*result = fold_build2_loc (loc, UNGT_EXPR,
10025 TREE_TYPE (TREE_TYPE (fndecl)),
10026 fold_build1_loc (loc, ABS_EXPR, type, arg),
10027 build_real (type, r));
10028 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
10029 TREE_TYPE (TREE_TYPE (fndecl)),
10030 result);*/
10031 return result;
10033 case BUILT_IN_ISNORMAL:
10035 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
10036 islessequal(fabs(x),DBL_MAX). */
10037 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
10038 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
10039 tree const type = TREE_TYPE (arg);
10040 REAL_VALUE_TYPE rmax, rmin;
10041 char buf[128];
10043 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
10044 real_from_string (&rmax, buf);
10045 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10046 real_from_string (&rmin, buf);
10047 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10048 result = build_call_expr (isle_fn, 2, arg,
10049 build_real (type, rmax));
10050 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
10051 build_call_expr (isge_fn, 2, arg,
10052 build_real (type, rmin)));
10053 return result;
10055 default:
10056 break;
10059 return NULL_TREE;
10062 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10063 ARG is the argument for the call. */
10065 static tree
10066 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
10068 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10069 REAL_VALUE_TYPE r;
10071 if (!validate_arg (arg, REAL_TYPE))
10072 return NULL_TREE;
10074 switch (builtin_index)
10076 case BUILT_IN_ISINF:
10077 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10078 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10080 if (TREE_CODE (arg) == REAL_CST)
10082 r = TREE_REAL_CST (arg);
10083 if (real_isinf (&r))
10084 return real_compare (GT_EXPR, &r, &dconst0)
10085 ? integer_one_node : integer_minus_one_node;
10086 else
10087 return integer_zero_node;
10090 return NULL_TREE;
10092 case BUILT_IN_ISINF_SIGN:
10094 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10095 /* In a boolean context, GCC will fold the inner COND_EXPR to
10096 1. So e.g. "if (isinf_sign(x))" would be folded to just
10097 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10098 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10099 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10100 tree tmp = NULL_TREE;
10102 arg = builtin_save_expr (arg);
10104 if (signbit_fn && isinf_fn)
10106 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10107 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10109 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10110 signbit_call, integer_zero_node);
10111 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10112 isinf_call, integer_zero_node);
10114 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10115 integer_minus_one_node, integer_one_node);
10116 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10117 isinf_call, tmp,
10118 integer_zero_node);
10121 return tmp;
10124 case BUILT_IN_ISFINITE:
10125 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10126 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10127 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10129 if (TREE_CODE (arg) == REAL_CST)
10131 r = TREE_REAL_CST (arg);
10132 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10135 return NULL_TREE;
10137 case BUILT_IN_ISNAN:
10138 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10139 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10141 if (TREE_CODE (arg) == REAL_CST)
10143 r = TREE_REAL_CST (arg);
10144 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10147 arg = builtin_save_expr (arg);
10148 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10150 default:
10151 gcc_unreachable ();
10155 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10156 This builtin will generate code to return the appropriate floating
10157 point classification depending on the value of the floating point
10158 number passed in. The possible return values must be supplied as
10159 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10160 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10161 one floating point argument which is "type generic". */
10163 static tree
10164 fold_builtin_fpclassify (location_t loc, tree exp)
10166 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10167 arg, type, res, tmp;
10168 enum machine_mode mode;
10169 REAL_VALUE_TYPE r;
10170 char buf[128];
10172 /* Verify the required arguments in the original call. */
10173 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10174 INTEGER_TYPE, INTEGER_TYPE,
10175 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10176 return NULL_TREE;
10178 fp_nan = CALL_EXPR_ARG (exp, 0);
10179 fp_infinite = CALL_EXPR_ARG (exp, 1);
10180 fp_normal = CALL_EXPR_ARG (exp, 2);
10181 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10182 fp_zero = CALL_EXPR_ARG (exp, 4);
10183 arg = CALL_EXPR_ARG (exp, 5);
10184 type = TREE_TYPE (arg);
10185 mode = TYPE_MODE (type);
10186 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10188 /* fpclassify(x) ->
10189 isnan(x) ? FP_NAN :
10190 (fabs(x) == Inf ? FP_INFINITE :
10191 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10192 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10194 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10195 build_real (type, dconst0));
10196 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10197 tmp, fp_zero, fp_subnormal);
10199 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10200 real_from_string (&r, buf);
10201 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10202 arg, build_real (type, r));
10203 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10205 if (HONOR_INFINITIES (mode))
10207 real_inf (&r);
10208 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10209 build_real (type, r));
10210 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10211 fp_infinite, res);
10214 if (HONOR_NANS (mode))
10216 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10217 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10220 return res;
10223 /* Fold a call to an unordered comparison function such as
10224 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10225 being called and ARG0 and ARG1 are the arguments for the call.
10226 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10227 the opposite of the desired result. UNORDERED_CODE is used
10228 for modes that can hold NaNs and ORDERED_CODE is used for
10229 the rest. */
10231 static tree
10232 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10233 enum tree_code unordered_code,
10234 enum tree_code ordered_code)
10236 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10237 enum tree_code code;
10238 tree type0, type1;
10239 enum tree_code code0, code1;
10240 tree cmp_type = NULL_TREE;
10242 type0 = TREE_TYPE (arg0);
10243 type1 = TREE_TYPE (arg1);
10245 code0 = TREE_CODE (type0);
10246 code1 = TREE_CODE (type1);
10248 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10249 /* Choose the wider of two real types. */
10250 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10251 ? type0 : type1;
10252 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10253 cmp_type = type0;
10254 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10255 cmp_type = type1;
10257 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10258 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10260 if (unordered_code == UNORDERED_EXPR)
10262 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10263 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10264 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10267 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10268 : ordered_code;
10269 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10270 fold_build2_loc (loc, code, type, arg0, arg1));
10273 /* Fold a call to built-in function FNDECL with 0 arguments.
10274 IGNORE is true if the result of the function call is ignored. This
10275 function returns NULL_TREE if no simplification was possible. */
10277 static tree
10278 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10280 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10281 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10282 switch (fcode)
10284 CASE_FLT_FN (BUILT_IN_INF):
10285 case BUILT_IN_INFD32:
10286 case BUILT_IN_INFD64:
10287 case BUILT_IN_INFD128:
10288 return fold_builtin_inf (loc, type, true);
10290 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10291 return fold_builtin_inf (loc, type, false);
10293 case BUILT_IN_CLASSIFY_TYPE:
10294 return fold_builtin_classify_type (NULL_TREE);
10296 case BUILT_IN_UNREACHABLE:
10297 if (flag_sanitize & SANITIZE_UNREACHABLE
10298 && (current_function_decl == NULL
10299 || !lookup_attribute ("no_sanitize_undefined",
10300 DECL_ATTRIBUTES (current_function_decl))))
10301 return ubsan_instrument_unreachable (loc);
10302 break;
10304 default:
10305 break;
10307 return NULL_TREE;
10310 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10311 IGNORE is true if the result of the function call is ignored. This
10312 function returns NULL_TREE if no simplification was possible. */
10314 static tree
10315 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10317 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10318 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10319 switch (fcode)
10321 case BUILT_IN_CONSTANT_P:
10323 tree val = fold_builtin_constant_p (arg0);
10325 /* Gimplification will pull the CALL_EXPR for the builtin out of
10326 an if condition. When not optimizing, we'll not CSE it back.
10327 To avoid link error types of regressions, return false now. */
10328 if (!val && !optimize)
10329 val = integer_zero_node;
10331 return val;
10334 case BUILT_IN_CLASSIFY_TYPE:
10335 return fold_builtin_classify_type (arg0);
10337 case BUILT_IN_STRLEN:
10338 return fold_builtin_strlen (loc, type, arg0);
10340 CASE_FLT_FN (BUILT_IN_FABS):
10341 case BUILT_IN_FABSD32:
10342 case BUILT_IN_FABSD64:
10343 case BUILT_IN_FABSD128:
10344 return fold_builtin_fabs (loc, arg0, type);
10346 case BUILT_IN_ABS:
10347 case BUILT_IN_LABS:
10348 case BUILT_IN_LLABS:
10349 case BUILT_IN_IMAXABS:
10350 return fold_builtin_abs (loc, arg0, type);
10352 CASE_FLT_FN (BUILT_IN_CONJ):
10353 if (validate_arg (arg0, COMPLEX_TYPE)
10354 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10355 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10356 break;
10358 CASE_FLT_FN (BUILT_IN_CREAL):
10359 if (validate_arg (arg0, COMPLEX_TYPE)
10360 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10361 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10362 break;
10364 CASE_FLT_FN (BUILT_IN_CIMAG):
10365 if (validate_arg (arg0, COMPLEX_TYPE)
10366 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10367 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10368 break;
10370 CASE_FLT_FN (BUILT_IN_CCOS):
10371 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
10373 CASE_FLT_FN (BUILT_IN_CCOSH):
10374 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
10376 CASE_FLT_FN (BUILT_IN_CPROJ):
10377 return fold_builtin_cproj (loc, arg0, type);
10379 CASE_FLT_FN (BUILT_IN_CSIN):
10380 if (validate_arg (arg0, COMPLEX_TYPE)
10381 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10382 return do_mpc_arg1 (arg0, type, mpc_sin);
10383 break;
10385 CASE_FLT_FN (BUILT_IN_CSINH):
10386 if (validate_arg (arg0, COMPLEX_TYPE)
10387 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10388 return do_mpc_arg1 (arg0, type, mpc_sinh);
10389 break;
10391 CASE_FLT_FN (BUILT_IN_CTAN):
10392 if (validate_arg (arg0, COMPLEX_TYPE)
10393 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10394 return do_mpc_arg1 (arg0, type, mpc_tan);
10395 break;
10397 CASE_FLT_FN (BUILT_IN_CTANH):
10398 if (validate_arg (arg0, COMPLEX_TYPE)
10399 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10400 return do_mpc_arg1 (arg0, type, mpc_tanh);
10401 break;
10403 CASE_FLT_FN (BUILT_IN_CLOG):
10404 if (validate_arg (arg0, COMPLEX_TYPE)
10405 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10406 return do_mpc_arg1 (arg0, type, mpc_log);
10407 break;
10409 CASE_FLT_FN (BUILT_IN_CSQRT):
10410 if (validate_arg (arg0, COMPLEX_TYPE)
10411 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10412 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10413 break;
10415 CASE_FLT_FN (BUILT_IN_CASIN):
10416 if (validate_arg (arg0, COMPLEX_TYPE)
10417 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10418 return do_mpc_arg1 (arg0, type, mpc_asin);
10419 break;
10421 CASE_FLT_FN (BUILT_IN_CACOS):
10422 if (validate_arg (arg0, COMPLEX_TYPE)
10423 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10424 return do_mpc_arg1 (arg0, type, mpc_acos);
10425 break;
10427 CASE_FLT_FN (BUILT_IN_CATAN):
10428 if (validate_arg (arg0, COMPLEX_TYPE)
10429 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10430 return do_mpc_arg1 (arg0, type, mpc_atan);
10431 break;
10433 CASE_FLT_FN (BUILT_IN_CASINH):
10434 if (validate_arg (arg0, COMPLEX_TYPE)
10435 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10436 return do_mpc_arg1 (arg0, type, mpc_asinh);
10437 break;
10439 CASE_FLT_FN (BUILT_IN_CACOSH):
10440 if (validate_arg (arg0, COMPLEX_TYPE)
10441 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10442 return do_mpc_arg1 (arg0, type, mpc_acosh);
10443 break;
10445 CASE_FLT_FN (BUILT_IN_CATANH):
10446 if (validate_arg (arg0, COMPLEX_TYPE)
10447 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10448 return do_mpc_arg1 (arg0, type, mpc_atanh);
10449 break;
10451 CASE_FLT_FN (BUILT_IN_CABS):
10452 return fold_builtin_cabs (loc, arg0, type, fndecl);
10454 CASE_FLT_FN (BUILT_IN_CARG):
10455 return fold_builtin_carg (loc, arg0, type);
10457 CASE_FLT_FN (BUILT_IN_SQRT):
10458 return fold_builtin_sqrt (loc, arg0, type);
10460 CASE_FLT_FN (BUILT_IN_CBRT):
10461 return fold_builtin_cbrt (loc, arg0, type);
10463 CASE_FLT_FN (BUILT_IN_ASIN):
10464 if (validate_arg (arg0, REAL_TYPE))
10465 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10466 &dconstm1, &dconst1, true);
10467 break;
10469 CASE_FLT_FN (BUILT_IN_ACOS):
10470 if (validate_arg (arg0, REAL_TYPE))
10471 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10472 &dconstm1, &dconst1, true);
10473 break;
10475 CASE_FLT_FN (BUILT_IN_ATAN):
10476 if (validate_arg (arg0, REAL_TYPE))
10477 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10478 break;
10480 CASE_FLT_FN (BUILT_IN_ASINH):
10481 if (validate_arg (arg0, REAL_TYPE))
10482 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10483 break;
10485 CASE_FLT_FN (BUILT_IN_ACOSH):
10486 if (validate_arg (arg0, REAL_TYPE))
10487 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10488 &dconst1, NULL, true);
10489 break;
10491 CASE_FLT_FN (BUILT_IN_ATANH):
10492 if (validate_arg (arg0, REAL_TYPE))
10493 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10494 &dconstm1, &dconst1, false);
10495 break;
10497 CASE_FLT_FN (BUILT_IN_SIN):
10498 if (validate_arg (arg0, REAL_TYPE))
10499 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10500 break;
10502 CASE_FLT_FN (BUILT_IN_COS):
10503 return fold_builtin_cos (loc, arg0, type, fndecl);
10505 CASE_FLT_FN (BUILT_IN_TAN):
10506 return fold_builtin_tan (arg0, type);
10508 CASE_FLT_FN (BUILT_IN_CEXP):
10509 return fold_builtin_cexp (loc, arg0, type);
10511 CASE_FLT_FN (BUILT_IN_CEXPI):
10512 if (validate_arg (arg0, REAL_TYPE))
10513 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10514 break;
10516 CASE_FLT_FN (BUILT_IN_SINH):
10517 if (validate_arg (arg0, REAL_TYPE))
10518 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10519 break;
10521 CASE_FLT_FN (BUILT_IN_COSH):
10522 return fold_builtin_cosh (loc, arg0, type, fndecl);
10524 CASE_FLT_FN (BUILT_IN_TANH):
10525 if (validate_arg (arg0, REAL_TYPE))
10526 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10527 break;
10529 CASE_FLT_FN (BUILT_IN_ERF):
10530 if (validate_arg (arg0, REAL_TYPE))
10531 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10532 break;
10534 CASE_FLT_FN (BUILT_IN_ERFC):
10535 if (validate_arg (arg0, REAL_TYPE))
10536 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10537 break;
10539 CASE_FLT_FN (BUILT_IN_TGAMMA):
10540 if (validate_arg (arg0, REAL_TYPE))
10541 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10542 break;
10544 CASE_FLT_FN (BUILT_IN_EXP):
10545 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10547 CASE_FLT_FN (BUILT_IN_EXP2):
10548 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10550 CASE_FLT_FN (BUILT_IN_EXP10):
10551 CASE_FLT_FN (BUILT_IN_POW10):
10552 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10554 CASE_FLT_FN (BUILT_IN_EXPM1):
10555 if (validate_arg (arg0, REAL_TYPE))
10556 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10557 break;
10559 CASE_FLT_FN (BUILT_IN_LOG):
10560 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10562 CASE_FLT_FN (BUILT_IN_LOG2):
10563 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10565 CASE_FLT_FN (BUILT_IN_LOG10):
10566 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10568 CASE_FLT_FN (BUILT_IN_LOG1P):
10569 if (validate_arg (arg0, REAL_TYPE))
10570 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10571 &dconstm1, NULL, false);
10572 break;
10574 CASE_FLT_FN (BUILT_IN_J0):
10575 if (validate_arg (arg0, REAL_TYPE))
10576 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10577 NULL, NULL, 0);
10578 break;
10580 CASE_FLT_FN (BUILT_IN_J1):
10581 if (validate_arg (arg0, REAL_TYPE))
10582 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10583 NULL, NULL, 0);
10584 break;
10586 CASE_FLT_FN (BUILT_IN_Y0):
10587 if (validate_arg (arg0, REAL_TYPE))
10588 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10589 &dconst0, NULL, false);
10590 break;
10592 CASE_FLT_FN (BUILT_IN_Y1):
10593 if (validate_arg (arg0, REAL_TYPE))
10594 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10595 &dconst0, NULL, false);
10596 break;
10598 CASE_FLT_FN (BUILT_IN_NAN):
10599 case BUILT_IN_NAND32:
10600 case BUILT_IN_NAND64:
10601 case BUILT_IN_NAND128:
10602 return fold_builtin_nan (arg0, type, true);
10604 CASE_FLT_FN (BUILT_IN_NANS):
10605 return fold_builtin_nan (arg0, type, false);
10607 CASE_FLT_FN (BUILT_IN_FLOOR):
10608 return fold_builtin_floor (loc, fndecl, arg0);
10610 CASE_FLT_FN (BUILT_IN_CEIL):
10611 return fold_builtin_ceil (loc, fndecl, arg0);
10613 CASE_FLT_FN (BUILT_IN_TRUNC):
10614 return fold_builtin_trunc (loc, fndecl, arg0);
10616 CASE_FLT_FN (BUILT_IN_ROUND):
10617 return fold_builtin_round (loc, fndecl, arg0);
10619 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10620 CASE_FLT_FN (BUILT_IN_RINT):
10621 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10623 CASE_FLT_FN (BUILT_IN_ICEIL):
10624 CASE_FLT_FN (BUILT_IN_LCEIL):
10625 CASE_FLT_FN (BUILT_IN_LLCEIL):
10626 CASE_FLT_FN (BUILT_IN_LFLOOR):
10627 CASE_FLT_FN (BUILT_IN_IFLOOR):
10628 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10629 CASE_FLT_FN (BUILT_IN_IROUND):
10630 CASE_FLT_FN (BUILT_IN_LROUND):
10631 CASE_FLT_FN (BUILT_IN_LLROUND):
10632 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10634 CASE_FLT_FN (BUILT_IN_IRINT):
10635 CASE_FLT_FN (BUILT_IN_LRINT):
10636 CASE_FLT_FN (BUILT_IN_LLRINT):
10637 return fold_fixed_mathfn (loc, fndecl, arg0);
10639 case BUILT_IN_BSWAP16:
10640 case BUILT_IN_BSWAP32:
10641 case BUILT_IN_BSWAP64:
10642 return fold_builtin_bswap (fndecl, arg0);
10644 CASE_INT_FN (BUILT_IN_FFS):
10645 CASE_INT_FN (BUILT_IN_CLZ):
10646 CASE_INT_FN (BUILT_IN_CTZ):
10647 CASE_INT_FN (BUILT_IN_CLRSB):
10648 CASE_INT_FN (BUILT_IN_POPCOUNT):
10649 CASE_INT_FN (BUILT_IN_PARITY):
10650 return fold_builtin_bitop (fndecl, arg0);
10652 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10653 return fold_builtin_signbit (loc, arg0, type);
10655 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10656 return fold_builtin_significand (loc, arg0, type);
10658 CASE_FLT_FN (BUILT_IN_ILOGB):
10659 CASE_FLT_FN (BUILT_IN_LOGB):
10660 return fold_builtin_logb (loc, arg0, type);
10662 case BUILT_IN_ISASCII:
10663 return fold_builtin_isascii (loc, arg0);
10665 case BUILT_IN_TOASCII:
10666 return fold_builtin_toascii (loc, arg0);
10668 case BUILT_IN_ISDIGIT:
10669 return fold_builtin_isdigit (loc, arg0);
10671 CASE_FLT_FN (BUILT_IN_FINITE):
10672 case BUILT_IN_FINITED32:
10673 case BUILT_IN_FINITED64:
10674 case BUILT_IN_FINITED128:
10675 case BUILT_IN_ISFINITE:
10677 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10678 if (ret)
10679 return ret;
10680 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10683 CASE_FLT_FN (BUILT_IN_ISINF):
10684 case BUILT_IN_ISINFD32:
10685 case BUILT_IN_ISINFD64:
10686 case BUILT_IN_ISINFD128:
10688 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10689 if (ret)
10690 return ret;
10691 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10694 case BUILT_IN_ISNORMAL:
10695 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10697 case BUILT_IN_ISINF_SIGN:
10698 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10700 CASE_FLT_FN (BUILT_IN_ISNAN):
10701 case BUILT_IN_ISNAND32:
10702 case BUILT_IN_ISNAND64:
10703 case BUILT_IN_ISNAND128:
10704 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10706 case BUILT_IN_PRINTF:
10707 case BUILT_IN_PRINTF_UNLOCKED:
10708 case BUILT_IN_VPRINTF:
10709 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10711 case BUILT_IN_FREE:
10712 if (integer_zerop (arg0))
10713 return build_empty_stmt (loc);
10714 break;
10716 default:
10717 break;
10720 return NULL_TREE;
10724 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10725 IGNORE is true if the result of the function call is ignored. This
10726 function returns NULL_TREE if no simplification was possible. */
10728 static tree
10729 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10731 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10732 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10734 switch (fcode)
10736 CASE_FLT_FN (BUILT_IN_JN):
10737 if (validate_arg (arg0, INTEGER_TYPE)
10738 && validate_arg (arg1, REAL_TYPE))
10739 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10740 break;
10742 CASE_FLT_FN (BUILT_IN_YN):
10743 if (validate_arg (arg0, INTEGER_TYPE)
10744 && validate_arg (arg1, REAL_TYPE))
10745 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10746 &dconst0, false);
10747 break;
10749 CASE_FLT_FN (BUILT_IN_DREM):
10750 CASE_FLT_FN (BUILT_IN_REMAINDER):
10751 if (validate_arg (arg0, REAL_TYPE)
10752 && validate_arg (arg1, REAL_TYPE))
10753 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10754 break;
10756 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10757 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10758 if (validate_arg (arg0, REAL_TYPE)
10759 && validate_arg (arg1, POINTER_TYPE))
10760 return do_mpfr_lgamma_r (arg0, arg1, type);
10761 break;
10763 CASE_FLT_FN (BUILT_IN_ATAN2):
10764 if (validate_arg (arg0, REAL_TYPE)
10765 && validate_arg (arg1, REAL_TYPE))
10766 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10767 break;
10769 CASE_FLT_FN (BUILT_IN_FDIM):
10770 if (validate_arg (arg0, REAL_TYPE)
10771 && validate_arg (arg1, REAL_TYPE))
10772 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10773 break;
10775 CASE_FLT_FN (BUILT_IN_HYPOT):
10776 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10778 CASE_FLT_FN (BUILT_IN_CPOW):
10779 if (validate_arg (arg0, COMPLEX_TYPE)
10780 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10781 && validate_arg (arg1, COMPLEX_TYPE)
10782 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10783 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10784 break;
10786 CASE_FLT_FN (BUILT_IN_LDEXP):
10787 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10788 CASE_FLT_FN (BUILT_IN_SCALBN):
10789 CASE_FLT_FN (BUILT_IN_SCALBLN):
10790 return fold_builtin_load_exponent (loc, arg0, arg1,
10791 type, /*ldexp=*/false);
10793 CASE_FLT_FN (BUILT_IN_FREXP):
10794 return fold_builtin_frexp (loc, arg0, arg1, type);
10796 CASE_FLT_FN (BUILT_IN_MODF):
10797 return fold_builtin_modf (loc, arg0, arg1, type);
10799 case BUILT_IN_BZERO:
10800 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10802 case BUILT_IN_FPUTS:
10803 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10805 case BUILT_IN_FPUTS_UNLOCKED:
10806 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10808 case BUILT_IN_STRSTR:
10809 return fold_builtin_strstr (loc, arg0, arg1, type);
10811 case BUILT_IN_STRCAT:
10812 return fold_builtin_strcat (loc, arg0, arg1);
10814 case BUILT_IN_STRSPN:
10815 return fold_builtin_strspn (loc, arg0, arg1);
10817 case BUILT_IN_STRCSPN:
10818 return fold_builtin_strcspn (loc, arg0, arg1);
10820 case BUILT_IN_STRCHR:
10821 case BUILT_IN_INDEX:
10822 return fold_builtin_strchr (loc, arg0, arg1, type);
10824 case BUILT_IN_STRRCHR:
10825 case BUILT_IN_RINDEX:
10826 return fold_builtin_strrchr (loc, arg0, arg1, type);
10828 case BUILT_IN_STRCPY:
10829 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10831 case BUILT_IN_STPCPY:
10832 if (ignore)
10834 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10835 if (!fn)
10836 break;
10838 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10840 else
10841 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10842 break;
10844 case BUILT_IN_STRCMP:
10845 return fold_builtin_strcmp (loc, arg0, arg1);
10847 case BUILT_IN_STRPBRK:
10848 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10850 case BUILT_IN_EXPECT:
10851 return fold_builtin_expect (loc, arg0, arg1);
10853 CASE_FLT_FN (BUILT_IN_POW):
10854 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10856 CASE_FLT_FN (BUILT_IN_POWI):
10857 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10859 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10860 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10862 CASE_FLT_FN (BUILT_IN_FMIN):
10863 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10865 CASE_FLT_FN (BUILT_IN_FMAX):
10866 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10868 case BUILT_IN_ISGREATER:
10869 return fold_builtin_unordered_cmp (loc, fndecl,
10870 arg0, arg1, UNLE_EXPR, LE_EXPR);
10871 case BUILT_IN_ISGREATEREQUAL:
10872 return fold_builtin_unordered_cmp (loc, fndecl,
10873 arg0, arg1, UNLT_EXPR, LT_EXPR);
10874 case BUILT_IN_ISLESS:
10875 return fold_builtin_unordered_cmp (loc, fndecl,
10876 arg0, arg1, UNGE_EXPR, GE_EXPR);
10877 case BUILT_IN_ISLESSEQUAL:
10878 return fold_builtin_unordered_cmp (loc, fndecl,
10879 arg0, arg1, UNGT_EXPR, GT_EXPR);
10880 case BUILT_IN_ISLESSGREATER:
10881 return fold_builtin_unordered_cmp (loc, fndecl,
10882 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10883 case BUILT_IN_ISUNORDERED:
10884 return fold_builtin_unordered_cmp (loc, fndecl,
10885 arg0, arg1, UNORDERED_EXPR,
10886 NOP_EXPR);
10888 /* We do the folding for va_start in the expander. */
10889 case BUILT_IN_VA_START:
10890 break;
10892 case BUILT_IN_SPRINTF:
10893 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10895 case BUILT_IN_OBJECT_SIZE:
10896 return fold_builtin_object_size (arg0, arg1);
10898 case BUILT_IN_PRINTF:
10899 case BUILT_IN_PRINTF_UNLOCKED:
10900 case BUILT_IN_VPRINTF:
10901 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10903 case BUILT_IN_PRINTF_CHK:
10904 case BUILT_IN_VPRINTF_CHK:
10905 if (!validate_arg (arg0, INTEGER_TYPE)
10906 || TREE_SIDE_EFFECTS (arg0))
10907 return NULL_TREE;
10908 else
10909 return fold_builtin_printf (loc, fndecl,
10910 arg1, NULL_TREE, ignore, fcode);
10911 break;
10913 case BUILT_IN_FPRINTF:
10914 case BUILT_IN_FPRINTF_UNLOCKED:
10915 case BUILT_IN_VFPRINTF:
10916 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10917 ignore, fcode);
10919 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10920 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10922 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10923 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10925 default:
10926 break;
10928 return NULL_TREE;
10931 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10932 and ARG2. IGNORE is true if the result of the function call is ignored.
10933 This function returns NULL_TREE if no simplification was possible. */
10935 static tree
10936 fold_builtin_3 (location_t loc, tree fndecl,
10937 tree arg0, tree arg1, tree arg2, bool ignore)
10939 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10940 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10941 switch (fcode)
10944 CASE_FLT_FN (BUILT_IN_SINCOS):
10945 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10947 CASE_FLT_FN (BUILT_IN_FMA):
10948 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10949 break;
10951 CASE_FLT_FN (BUILT_IN_REMQUO):
10952 if (validate_arg (arg0, REAL_TYPE)
10953 && validate_arg (arg1, REAL_TYPE)
10954 && validate_arg (arg2, POINTER_TYPE))
10955 return do_mpfr_remquo (arg0, arg1, arg2);
10956 break;
10958 case BUILT_IN_MEMSET:
10959 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10961 case BUILT_IN_BCOPY:
10962 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10963 void_type_node, true, /*endp=*/3);
10965 case BUILT_IN_MEMCPY:
10966 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10967 type, ignore, /*endp=*/0);
10969 case BUILT_IN_MEMPCPY:
10970 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10971 type, ignore, /*endp=*/1);
10973 case BUILT_IN_MEMMOVE:
10974 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10975 type, ignore, /*endp=*/3);
10977 case BUILT_IN_STRNCAT:
10978 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10980 case BUILT_IN_STRNCPY:
10981 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10983 case BUILT_IN_STRNCMP:
10984 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10986 case BUILT_IN_MEMCHR:
10987 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10989 case BUILT_IN_BCMP:
10990 case BUILT_IN_MEMCMP:
10991 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10993 case BUILT_IN_SPRINTF:
10994 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10996 case BUILT_IN_SNPRINTF:
10997 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10999 case BUILT_IN_STRCPY_CHK:
11000 case BUILT_IN_STPCPY_CHK:
11001 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
11002 ignore, fcode);
11004 case BUILT_IN_STRCAT_CHK:
11005 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
11007 case BUILT_IN_PRINTF_CHK:
11008 case BUILT_IN_VPRINTF_CHK:
11009 if (!validate_arg (arg0, INTEGER_TYPE)
11010 || TREE_SIDE_EFFECTS (arg0))
11011 return NULL_TREE;
11012 else
11013 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
11014 break;
11016 case BUILT_IN_FPRINTF:
11017 case BUILT_IN_FPRINTF_UNLOCKED:
11018 case BUILT_IN_VFPRINTF:
11019 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
11020 ignore, fcode);
11022 case BUILT_IN_FPRINTF_CHK:
11023 case BUILT_IN_VFPRINTF_CHK:
11024 if (!validate_arg (arg1, INTEGER_TYPE)
11025 || TREE_SIDE_EFFECTS (arg1))
11026 return NULL_TREE;
11027 else
11028 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
11029 ignore, fcode);
11031 default:
11032 break;
11034 return NULL_TREE;
11037 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
11038 ARG2, and ARG3. IGNORE is true if the result of the function call is
11039 ignored. This function returns NULL_TREE if no simplification was
11040 possible. */
11042 static tree
11043 fold_builtin_4 (location_t loc, tree fndecl,
11044 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
11046 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11048 switch (fcode)
11050 case BUILT_IN_MEMCPY_CHK:
11051 case BUILT_IN_MEMPCPY_CHK:
11052 case BUILT_IN_MEMMOVE_CHK:
11053 case BUILT_IN_MEMSET_CHK:
11054 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
11055 NULL_TREE, ignore,
11056 DECL_FUNCTION_CODE (fndecl));
11058 case BUILT_IN_STRNCPY_CHK:
11059 case BUILT_IN_STPNCPY_CHK:
11060 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
11061 ignore, fcode);
11063 case BUILT_IN_STRNCAT_CHK:
11064 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
11066 case BUILT_IN_SNPRINTF:
11067 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
11069 case BUILT_IN_FPRINTF_CHK:
11070 case BUILT_IN_VFPRINTF_CHK:
11071 if (!validate_arg (arg1, INTEGER_TYPE)
11072 || TREE_SIDE_EFFECTS (arg1))
11073 return NULL_TREE;
11074 else
11075 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
11076 ignore, fcode);
11077 break;
11079 default:
11080 break;
11082 return NULL_TREE;
11085 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11086 arguments, where NARGS <= 4. IGNORE is true if the result of the
11087 function call is ignored. This function returns NULL_TREE if no
11088 simplification was possible. Note that this only folds builtins with
11089 fixed argument patterns. Foldings that do varargs-to-varargs
11090 transformations, or that match calls with more than 4 arguments,
11091 need to be handled with fold_builtin_varargs instead. */
11093 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11095 static tree
11096 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11098 tree ret = NULL_TREE;
11100 switch (nargs)
11102 case 0:
11103 ret = fold_builtin_0 (loc, fndecl, ignore);
11104 break;
11105 case 1:
11106 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11107 break;
11108 case 2:
11109 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11110 break;
11111 case 3:
11112 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11113 break;
11114 case 4:
11115 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11116 ignore);
11117 break;
11118 default:
11119 break;
11121 if (ret)
11123 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11124 SET_EXPR_LOCATION (ret, loc);
11125 TREE_NO_WARNING (ret) = 1;
11126 return ret;
11128 return NULL_TREE;
11131 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11132 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11133 of arguments in ARGS to be omitted. OLDNARGS is the number of
11134 elements in ARGS. */
11136 static tree
11137 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11138 int skip, tree fndecl, int n, va_list newargs)
11140 int nargs = oldnargs - skip + n;
11141 tree *buffer;
11143 if (n > 0)
11145 int i, j;
11147 buffer = XALLOCAVEC (tree, nargs);
11148 for (i = 0; i < n; i++)
11149 buffer[i] = va_arg (newargs, tree);
11150 for (j = skip; j < oldnargs; j++, i++)
11151 buffer[i] = args[j];
11153 else
11154 buffer = args + skip;
11156 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11159 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11160 list ARGS along with N new arguments specified as the "..."
11161 parameters. SKIP is the number of arguments in ARGS to be omitted.
11162 OLDNARGS is the number of elements in ARGS. */
11164 static tree
11165 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11166 int skip, tree fndecl, int n, ...)
11168 va_list ap;
11169 tree t;
11171 va_start (ap, n);
11172 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11173 va_end (ap);
11175 return t;
11178 /* Return true if FNDECL shouldn't be folded right now.
11179 If a built-in function has an inline attribute always_inline
11180 wrapper, defer folding it after always_inline functions have
11181 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11182 might not be performed. */
11184 bool
11185 avoid_folding_inline_builtin (tree fndecl)
11187 return (DECL_DECLARED_INLINE_P (fndecl)
11188 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11189 && cfun
11190 && !cfun->always_inline_functions_inlined
11191 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11194 /* A wrapper function for builtin folding that prevents warnings for
11195 "statement without effect" and the like, caused by removing the
11196 call node earlier than the warning is generated. */
11198 tree
11199 fold_call_expr (location_t loc, tree exp, bool ignore)
11201 tree ret = NULL_TREE;
11202 tree fndecl = get_callee_fndecl (exp);
11203 if (fndecl
11204 && TREE_CODE (fndecl) == FUNCTION_DECL
11205 && DECL_BUILT_IN (fndecl)
11206 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11207 yet. Defer folding until we see all the arguments
11208 (after inlining). */
11209 && !CALL_EXPR_VA_ARG_PACK (exp))
11211 int nargs = call_expr_nargs (exp);
11213 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11214 instead last argument is __builtin_va_arg_pack (). Defer folding
11215 even in that case, until arguments are finalized. */
11216 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11218 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11219 if (fndecl2
11220 && TREE_CODE (fndecl2) == FUNCTION_DECL
11221 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11222 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11223 return NULL_TREE;
11226 if (avoid_folding_inline_builtin (fndecl))
11227 return NULL_TREE;
11229 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11230 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11231 CALL_EXPR_ARGP (exp), ignore);
11232 else
11234 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11236 tree *args = CALL_EXPR_ARGP (exp);
11237 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11239 if (!ret)
11240 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11241 if (ret)
11242 return ret;
11245 return NULL_TREE;
11248 /* Conveniently construct a function call expression. FNDECL names the
11249 function to be called and N arguments are passed in the array
11250 ARGARRAY. */
11252 tree
11253 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11255 tree fntype = TREE_TYPE (fndecl);
11256 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11258 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11261 /* Conveniently construct a function call expression. FNDECL names the
11262 function to be called and the arguments are passed in the vector
11263 VEC. */
11265 tree
11266 build_call_expr_loc_vec (location_t loc, tree fndecl, vec<tree, va_gc> *vec)
11268 return build_call_expr_loc_array (loc, fndecl, vec_safe_length (vec),
11269 vec_safe_address (vec));
11273 /* Conveniently construct a function call expression. FNDECL names the
11274 function to be called, N is the number of arguments, and the "..."
11275 parameters are the argument expressions. */
11277 tree
11278 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11280 va_list ap;
11281 tree *argarray = XALLOCAVEC (tree, n);
11282 int i;
11284 va_start (ap, n);
11285 for (i = 0; i < n; i++)
11286 argarray[i] = va_arg (ap, tree);
11287 va_end (ap);
11288 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11291 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11292 varargs macros aren't supported by all bootstrap compilers. */
11294 tree
11295 build_call_expr (tree fndecl, int n, ...)
11297 va_list ap;
11298 tree *argarray = XALLOCAVEC (tree, n);
11299 int i;
11301 va_start (ap, n);
11302 for (i = 0; i < n; i++)
11303 argarray[i] = va_arg (ap, tree);
11304 va_end (ap);
11305 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11308 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11309 N arguments are passed in the array ARGARRAY. */
11311 tree
11312 fold_builtin_call_array (location_t loc, tree type,
11313 tree fn,
11314 int n,
11315 tree *argarray)
11317 tree ret = NULL_TREE;
11318 tree exp;
11320 if (TREE_CODE (fn) == ADDR_EXPR)
11322 tree fndecl = TREE_OPERAND (fn, 0);
11323 if (TREE_CODE (fndecl) == FUNCTION_DECL
11324 && DECL_BUILT_IN (fndecl))
11326 /* If last argument is __builtin_va_arg_pack (), arguments to this
11327 function are not finalized yet. Defer folding until they are. */
11328 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11330 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11331 if (fndecl2
11332 && TREE_CODE (fndecl2) == FUNCTION_DECL
11333 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11334 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11335 return build_call_array_loc (loc, type, fn, n, argarray);
11337 if (avoid_folding_inline_builtin (fndecl))
11338 return build_call_array_loc (loc, type, fn, n, argarray);
11339 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11341 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11342 if (ret)
11343 return ret;
11345 return build_call_array_loc (loc, type, fn, n, argarray);
11347 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11349 /* First try the transformations that don't require consing up
11350 an exp. */
11351 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11352 if (ret)
11353 return ret;
11356 /* If we got this far, we need to build an exp. */
11357 exp = build_call_array_loc (loc, type, fn, n, argarray);
11358 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11359 return ret ? ret : exp;
11363 return build_call_array_loc (loc, type, fn, n, argarray);
11366 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11367 along with N new arguments specified as the "..." parameters. SKIP
11368 is the number of arguments in EXP to be omitted. This function is used
11369 to do varargs-to-varargs transformations. */
11371 static tree
11372 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11374 va_list ap;
11375 tree t;
11377 va_start (ap, n);
11378 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11379 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11380 va_end (ap);
11382 return t;
11385 /* Validate a single argument ARG against a tree code CODE representing
11386 a type. */
11388 static bool
11389 validate_arg (const_tree arg, enum tree_code code)
11391 if (!arg)
11392 return false;
11393 else if (code == POINTER_TYPE)
11394 return POINTER_TYPE_P (TREE_TYPE (arg));
11395 else if (code == INTEGER_TYPE)
11396 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11397 return code == TREE_CODE (TREE_TYPE (arg));
11400 /* This function validates the types of a function call argument list
11401 against a specified list of tree_codes. If the last specifier is a 0,
11402 that represents an ellipses, otherwise the last specifier must be a
11403 VOID_TYPE.
11405 This is the GIMPLE version of validate_arglist. Eventually we want to
11406 completely convert builtins.c to work from GIMPLEs and the tree based
11407 validate_arglist will then be removed. */
11409 bool
11410 validate_gimple_arglist (const_gimple call, ...)
11412 enum tree_code code;
11413 bool res = 0;
11414 va_list ap;
11415 const_tree arg;
11416 size_t i;
11418 va_start (ap, call);
11419 i = 0;
11423 code = (enum tree_code) va_arg (ap, int);
11424 switch (code)
11426 case 0:
11427 /* This signifies an ellipses, any further arguments are all ok. */
11428 res = true;
11429 goto end;
11430 case VOID_TYPE:
11431 /* This signifies an endlink, if no arguments remain, return
11432 true, otherwise return false. */
11433 res = (i == gimple_call_num_args (call));
11434 goto end;
11435 default:
11436 /* If no parameters remain or the parameter's code does not
11437 match the specified code, return false. Otherwise continue
11438 checking any remaining arguments. */
11439 arg = gimple_call_arg (call, i++);
11440 if (!validate_arg (arg, code))
11441 goto end;
11442 break;
11445 while (1);
11447 /* We need gotos here since we can only have one VA_CLOSE in a
11448 function. */
11449 end: ;
11450 va_end (ap);
11452 return res;
11455 /* Default target-specific builtin expander that does nothing. */
11458 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11459 rtx target ATTRIBUTE_UNUSED,
11460 rtx subtarget ATTRIBUTE_UNUSED,
11461 enum machine_mode mode ATTRIBUTE_UNUSED,
11462 int ignore ATTRIBUTE_UNUSED)
11464 return NULL_RTX;
11467 /* Returns true is EXP represents data that would potentially reside
11468 in a readonly section. */
11470 static bool
11471 readonly_data_expr (tree exp)
11473 STRIP_NOPS (exp);
11475 if (TREE_CODE (exp) != ADDR_EXPR)
11476 return false;
11478 exp = get_base_address (TREE_OPERAND (exp, 0));
11479 if (!exp)
11480 return false;
11482 /* Make sure we call decl_readonly_section only for trees it
11483 can handle (since it returns true for everything it doesn't
11484 understand). */
11485 if (TREE_CODE (exp) == STRING_CST
11486 || TREE_CODE (exp) == CONSTRUCTOR
11487 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11488 return decl_readonly_section (exp, 0);
11489 else
11490 return false;
11493 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11494 to the call, and TYPE is its return type.
11496 Return NULL_TREE if no simplification was possible, otherwise return the
11497 simplified form of the call as a tree.
11499 The simplified form may be a constant or other expression which
11500 computes the same value, but in a more efficient manner (including
11501 calls to other builtin functions).
11503 The call may contain arguments which need to be evaluated, but
11504 which are not useful to determine the result of the call. In
11505 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11506 COMPOUND_EXPR will be an argument which must be evaluated.
11507 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11508 COMPOUND_EXPR in the chain will contain the tree for the simplified
11509 form of the builtin function call. */
11511 static tree
11512 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11514 if (!validate_arg (s1, POINTER_TYPE)
11515 || !validate_arg (s2, POINTER_TYPE))
11516 return NULL_TREE;
11517 else
11519 tree fn;
11520 const char *p1, *p2;
11522 p2 = c_getstr (s2);
11523 if (p2 == NULL)
11524 return NULL_TREE;
11526 p1 = c_getstr (s1);
11527 if (p1 != NULL)
11529 const char *r = strstr (p1, p2);
11530 tree tem;
11532 if (r == NULL)
11533 return build_int_cst (TREE_TYPE (s1), 0);
11535 /* Return an offset into the constant string argument. */
11536 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11537 return fold_convert_loc (loc, type, tem);
11540 /* The argument is const char *, and the result is char *, so we need
11541 a type conversion here to avoid a warning. */
11542 if (p2[0] == '\0')
11543 return fold_convert_loc (loc, type, s1);
11545 if (p2[1] != '\0')
11546 return NULL_TREE;
11548 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11549 if (!fn)
11550 return NULL_TREE;
11552 /* New argument list transforming strstr(s1, s2) to
11553 strchr(s1, s2[0]). */
11554 return build_call_expr_loc (loc, fn, 2, s1,
11555 build_int_cst (integer_type_node, p2[0]));
11559 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11560 the call, and TYPE is its return type.
11562 Return NULL_TREE if no simplification was possible, otherwise return the
11563 simplified form of the call as a tree.
11565 The simplified form may be a constant or other expression which
11566 computes the same value, but in a more efficient manner (including
11567 calls to other builtin functions).
11569 The call may contain arguments which need to be evaluated, but
11570 which are not useful to determine the result of the call. In
11571 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11572 COMPOUND_EXPR will be an argument which must be evaluated.
11573 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11574 COMPOUND_EXPR in the chain will contain the tree for the simplified
11575 form of the builtin function call. */
11577 static tree
11578 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11580 if (!validate_arg (s1, POINTER_TYPE)
11581 || !validate_arg (s2, INTEGER_TYPE))
11582 return NULL_TREE;
11583 else
11585 const char *p1;
11587 if (TREE_CODE (s2) != INTEGER_CST)
11588 return NULL_TREE;
11590 p1 = c_getstr (s1);
11591 if (p1 != NULL)
11593 char c;
11594 const char *r;
11595 tree tem;
11597 if (target_char_cast (s2, &c))
11598 return NULL_TREE;
11600 r = strchr (p1, c);
11602 if (r == NULL)
11603 return build_int_cst (TREE_TYPE (s1), 0);
11605 /* Return an offset into the constant string argument. */
11606 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11607 return fold_convert_loc (loc, type, tem);
11609 return NULL_TREE;
11613 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11614 the call, and TYPE is its return type.
11616 Return NULL_TREE if no simplification was possible, otherwise return the
11617 simplified form of the call as a tree.
11619 The simplified form may be a constant or other expression which
11620 computes the same value, but in a more efficient manner (including
11621 calls to other builtin functions).
11623 The call may contain arguments which need to be evaluated, but
11624 which are not useful to determine the result of the call. In
11625 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11626 COMPOUND_EXPR will be an argument which must be evaluated.
11627 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11628 COMPOUND_EXPR in the chain will contain the tree for the simplified
11629 form of the builtin function call. */
11631 static tree
11632 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11634 if (!validate_arg (s1, POINTER_TYPE)
11635 || !validate_arg (s2, INTEGER_TYPE))
11636 return NULL_TREE;
11637 else
11639 tree fn;
11640 const char *p1;
11642 if (TREE_CODE (s2) != INTEGER_CST)
11643 return NULL_TREE;
11645 p1 = c_getstr (s1);
11646 if (p1 != NULL)
11648 char c;
11649 const char *r;
11650 tree tem;
11652 if (target_char_cast (s2, &c))
11653 return NULL_TREE;
11655 r = strrchr (p1, c);
11657 if (r == NULL)
11658 return build_int_cst (TREE_TYPE (s1), 0);
11660 /* Return an offset into the constant string argument. */
11661 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11662 return fold_convert_loc (loc, type, tem);
11665 if (! integer_zerop (s2))
11666 return NULL_TREE;
11668 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11669 if (!fn)
11670 return NULL_TREE;
11672 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11673 return build_call_expr_loc (loc, fn, 2, s1, s2);
11677 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11678 to the call, and TYPE is its return type.
11680 Return NULL_TREE if no simplification was possible, otherwise return the
11681 simplified form of the call as a tree.
11683 The simplified form may be a constant or other expression which
11684 computes the same value, but in a more efficient manner (including
11685 calls to other builtin functions).
11687 The call may contain arguments which need to be evaluated, but
11688 which are not useful to determine the result of the call. In
11689 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11690 COMPOUND_EXPR will be an argument which must be evaluated.
11691 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11692 COMPOUND_EXPR in the chain will contain the tree for the simplified
11693 form of the builtin function call. */
11695 static tree
11696 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11698 if (!validate_arg (s1, POINTER_TYPE)
11699 || !validate_arg (s2, POINTER_TYPE))
11700 return NULL_TREE;
11701 else
11703 tree fn;
11704 const char *p1, *p2;
11706 p2 = c_getstr (s2);
11707 if (p2 == NULL)
11708 return NULL_TREE;
11710 p1 = c_getstr (s1);
11711 if (p1 != NULL)
11713 const char *r = strpbrk (p1, p2);
11714 tree tem;
11716 if (r == NULL)
11717 return build_int_cst (TREE_TYPE (s1), 0);
11719 /* Return an offset into the constant string argument. */
11720 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11721 return fold_convert_loc (loc, type, tem);
11724 if (p2[0] == '\0')
11725 /* strpbrk(x, "") == NULL.
11726 Evaluate and ignore s1 in case it had side-effects. */
11727 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11729 if (p2[1] != '\0')
11730 return NULL_TREE; /* Really call strpbrk. */
11732 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11733 if (!fn)
11734 return NULL_TREE;
11736 /* New argument list transforming strpbrk(s1, s2) to
11737 strchr(s1, s2[0]). */
11738 return build_call_expr_loc (loc, fn, 2, s1,
11739 build_int_cst (integer_type_node, p2[0]));
11743 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11744 to the call.
11746 Return NULL_TREE if no simplification was possible, otherwise return the
11747 simplified form of the call as a tree.
11749 The simplified form may be a constant or other expression which
11750 computes the same value, but in a more efficient manner (including
11751 calls to other builtin functions).
11753 The call may contain arguments which need to be evaluated, but
11754 which are not useful to determine the result of the call. In
11755 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11756 COMPOUND_EXPR will be an argument which must be evaluated.
11757 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11758 COMPOUND_EXPR in the chain will contain the tree for the simplified
11759 form of the builtin function call. */
11761 static tree
11762 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11764 if (!validate_arg (dst, POINTER_TYPE)
11765 || !validate_arg (src, POINTER_TYPE))
11766 return NULL_TREE;
11767 else
11769 const char *p = c_getstr (src);
11771 /* If the string length is zero, return the dst parameter. */
11772 if (p && *p == '\0')
11773 return dst;
11775 if (optimize_insn_for_speed_p ())
11777 /* See if we can store by pieces into (dst + strlen(dst)). */
11778 tree newdst, call;
11779 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11780 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11782 if (!strlen_fn || !strcpy_fn)
11783 return NULL_TREE;
11785 /* If we don't have a movstr we don't want to emit an strcpy
11786 call. We have to do that if the length of the source string
11787 isn't computable (in that case we can use memcpy probably
11788 later expanding to a sequence of mov instructions). If we
11789 have movstr instructions we can emit strcpy calls. */
11790 if (!HAVE_movstr)
11792 tree len = c_strlen (src, 1);
11793 if (! len || TREE_SIDE_EFFECTS (len))
11794 return NULL_TREE;
11797 /* Stabilize the argument list. */
11798 dst = builtin_save_expr (dst);
11800 /* Create strlen (dst). */
11801 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11802 /* Create (dst p+ strlen (dst)). */
11804 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11805 newdst = builtin_save_expr (newdst);
11807 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11808 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11810 return NULL_TREE;
11814 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11815 arguments to the call.
11817 Return NULL_TREE if no simplification was possible, otherwise return the
11818 simplified form of the call as a tree.
11820 The simplified form may be a constant or other expression which
11821 computes the same value, but in a more efficient manner (including
11822 calls to other builtin functions).
11824 The call may contain arguments which need to be evaluated, but
11825 which are not useful to determine the result of the call. In
11826 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11827 COMPOUND_EXPR will be an argument which must be evaluated.
11828 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11829 COMPOUND_EXPR in the chain will contain the tree for the simplified
11830 form of the builtin function call. */
11832 static tree
11833 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11835 if (!validate_arg (dst, POINTER_TYPE)
11836 || !validate_arg (src, POINTER_TYPE)
11837 || !validate_arg (len, INTEGER_TYPE))
11838 return NULL_TREE;
11839 else
11841 const char *p = c_getstr (src);
11843 /* If the requested length is zero, or the src parameter string
11844 length is zero, return the dst parameter. */
11845 if (integer_zerop (len) || (p && *p == '\0'))
11846 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11848 /* If the requested len is greater than or equal to the string
11849 length, call strcat. */
11850 if (TREE_CODE (len) == INTEGER_CST && p
11851 && compare_tree_int (len, strlen (p)) >= 0)
11853 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11855 /* If the replacement _DECL isn't initialized, don't do the
11856 transformation. */
11857 if (!fn)
11858 return NULL_TREE;
11860 return build_call_expr_loc (loc, fn, 2, dst, src);
11862 return NULL_TREE;
11866 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11867 to the call.
11869 Return NULL_TREE if no simplification was possible, otherwise return the
11870 simplified form of the call as a tree.
11872 The simplified form may be a constant or other expression which
11873 computes the same value, but in a more efficient manner (including
11874 calls to other builtin functions).
11876 The call may contain arguments which need to be evaluated, but
11877 which are not useful to determine the result of the call. In
11878 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11879 COMPOUND_EXPR will be an argument which must be evaluated.
11880 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11881 COMPOUND_EXPR in the chain will contain the tree for the simplified
11882 form of the builtin function call. */
11884 static tree
11885 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11887 if (!validate_arg (s1, POINTER_TYPE)
11888 || !validate_arg (s2, POINTER_TYPE))
11889 return NULL_TREE;
11890 else
11892 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11894 /* If both arguments are constants, evaluate at compile-time. */
11895 if (p1 && p2)
11897 const size_t r = strspn (p1, p2);
11898 return build_int_cst (size_type_node, r);
11901 /* If either argument is "", return NULL_TREE. */
11902 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11903 /* Evaluate and ignore both arguments in case either one has
11904 side-effects. */
11905 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11906 s1, s2);
11907 return NULL_TREE;
11911 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11912 to the call.
11914 Return NULL_TREE if no simplification was possible, otherwise return the
11915 simplified form of the call as a tree.
11917 The simplified form may be a constant or other expression which
11918 computes the same value, but in a more efficient manner (including
11919 calls to other builtin functions).
11921 The call may contain arguments which need to be evaluated, but
11922 which are not useful to determine the result of the call. In
11923 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11924 COMPOUND_EXPR will be an argument which must be evaluated.
11925 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11926 COMPOUND_EXPR in the chain will contain the tree for the simplified
11927 form of the builtin function call. */
11929 static tree
11930 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11932 if (!validate_arg (s1, POINTER_TYPE)
11933 || !validate_arg (s2, POINTER_TYPE))
11934 return NULL_TREE;
11935 else
11937 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11939 /* If both arguments are constants, evaluate at compile-time. */
11940 if (p1 && p2)
11942 const size_t r = strcspn (p1, p2);
11943 return build_int_cst (size_type_node, r);
11946 /* If the first argument is "", return NULL_TREE. */
11947 if (p1 && *p1 == '\0')
11949 /* Evaluate and ignore argument s2 in case it has
11950 side-effects. */
11951 return omit_one_operand_loc (loc, size_type_node,
11952 size_zero_node, s2);
11955 /* If the second argument is "", return __builtin_strlen(s1). */
11956 if (p2 && *p2 == '\0')
11958 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11960 /* If the replacement _DECL isn't initialized, don't do the
11961 transformation. */
11962 if (!fn)
11963 return NULL_TREE;
11965 return build_call_expr_loc (loc, fn, 1, s1);
11967 return NULL_TREE;
11971 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11972 to the call. IGNORE is true if the value returned
11973 by the builtin will be ignored. UNLOCKED is true is true if this
11974 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11975 the known length of the string. Return NULL_TREE if no simplification
11976 was possible. */
11978 tree
11979 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11980 bool ignore, bool unlocked, tree len)
11982 /* If we're using an unlocked function, assume the other unlocked
11983 functions exist explicitly. */
11984 tree const fn_fputc = (unlocked
11985 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11986 : builtin_decl_implicit (BUILT_IN_FPUTC));
11987 tree const fn_fwrite = (unlocked
11988 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
11989 : builtin_decl_implicit (BUILT_IN_FWRITE));
11991 /* If the return value is used, don't do the transformation. */
11992 if (!ignore)
11993 return NULL_TREE;
11995 /* Verify the arguments in the original call. */
11996 if (!validate_arg (arg0, POINTER_TYPE)
11997 || !validate_arg (arg1, POINTER_TYPE))
11998 return NULL_TREE;
12000 if (! len)
12001 len = c_strlen (arg0, 0);
12003 /* Get the length of the string passed to fputs. If the length
12004 can't be determined, punt. */
12005 if (!len
12006 || TREE_CODE (len) != INTEGER_CST)
12007 return NULL_TREE;
12009 switch (compare_tree_int (len, 1))
12011 case -1: /* length is 0, delete the call entirely . */
12012 return omit_one_operand_loc (loc, integer_type_node,
12013 integer_zero_node, arg1);;
12015 case 0: /* length is 1, call fputc. */
12017 const char *p = c_getstr (arg0);
12019 if (p != NULL)
12021 if (fn_fputc)
12022 return build_call_expr_loc (loc, fn_fputc, 2,
12023 build_int_cst
12024 (integer_type_node, p[0]), arg1);
12025 else
12026 return NULL_TREE;
12029 /* FALLTHROUGH */
12030 case 1: /* length is greater than 1, call fwrite. */
12032 /* If optimizing for size keep fputs. */
12033 if (optimize_function_for_size_p (cfun))
12034 return NULL_TREE;
12035 /* New argument list transforming fputs(string, stream) to
12036 fwrite(string, 1, len, stream). */
12037 if (fn_fwrite)
12038 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12039 size_one_node, len, arg1);
12040 else
12041 return NULL_TREE;
12043 default:
12044 gcc_unreachable ();
12046 return NULL_TREE;
12049 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12050 produced. False otherwise. This is done so that we don't output the error
12051 or warning twice or three times. */
12053 bool
12054 fold_builtin_next_arg (tree exp, bool va_start_p)
12056 tree fntype = TREE_TYPE (current_function_decl);
12057 int nargs = call_expr_nargs (exp);
12058 tree arg;
12059 /* There is good chance the current input_location points inside the
12060 definition of the va_start macro (perhaps on the token for
12061 builtin) in a system header, so warnings will not be emitted.
12062 Use the location in real source code. */
12063 source_location current_location =
12064 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
12065 NULL);
12067 if (!stdarg_p (fntype))
12069 error ("%<va_start%> used in function with fixed args");
12070 return true;
12073 if (va_start_p)
12075 if (va_start_p && (nargs != 2))
12077 error ("wrong number of arguments to function %<va_start%>");
12078 return true;
12080 arg = CALL_EXPR_ARG (exp, 1);
12082 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12083 when we checked the arguments and if needed issued a warning. */
12084 else
12086 if (nargs == 0)
12088 /* Evidently an out of date version of <stdarg.h>; can't validate
12089 va_start's second argument, but can still work as intended. */
12090 warning_at (current_location,
12091 OPT_Wvarargs,
12092 "%<__builtin_next_arg%> called without an argument");
12093 return true;
12095 else if (nargs > 1)
12097 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12098 return true;
12100 arg = CALL_EXPR_ARG (exp, 0);
12103 if (TREE_CODE (arg) == SSA_NAME)
12104 arg = SSA_NAME_VAR (arg);
12106 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12107 or __builtin_next_arg (0) the first time we see it, after checking
12108 the arguments and if needed issuing a warning. */
12109 if (!integer_zerop (arg))
12111 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12113 /* Strip off all nops for the sake of the comparison. This
12114 is not quite the same as STRIP_NOPS. It does more.
12115 We must also strip off INDIRECT_EXPR for C++ reference
12116 parameters. */
12117 while (CONVERT_EXPR_P (arg)
12118 || TREE_CODE (arg) == INDIRECT_REF)
12119 arg = TREE_OPERAND (arg, 0);
12120 if (arg != last_parm)
12122 /* FIXME: Sometimes with the tree optimizers we can get the
12123 not the last argument even though the user used the last
12124 argument. We just warn and set the arg to be the last
12125 argument so that we will get wrong-code because of
12126 it. */
12127 warning_at (current_location,
12128 OPT_Wvarargs,
12129 "second parameter of %<va_start%> not last named argument");
12132 /* Undefined by C99 7.15.1.4p4 (va_start):
12133 "If the parameter parmN is declared with the register storage
12134 class, with a function or array type, or with a type that is
12135 not compatible with the type that results after application of
12136 the default argument promotions, the behavior is undefined."
12138 else if (DECL_REGISTER (arg))
12140 warning_at (current_location,
12141 OPT_Wvarargs,
12142 "undefined behaviour when second parameter of "
12143 "%<va_start%> is declared with %<register%> storage");
12146 /* We want to verify the second parameter just once before the tree
12147 optimizers are run and then avoid keeping it in the tree,
12148 as otherwise we could warn even for correct code like:
12149 void foo (int i, ...)
12150 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12151 if (va_start_p)
12152 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12153 else
12154 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12156 return false;
12160 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12161 ORIG may be null if this is a 2-argument call. We don't attempt to
12162 simplify calls with more than 3 arguments.
12164 Return NULL_TREE if no simplification was possible, otherwise return the
12165 simplified form of the call as a tree. If IGNORED is true, it means that
12166 the caller does not use the returned value of the function. */
12168 static tree
12169 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12170 tree orig, int ignored)
12172 tree call, retval;
12173 const char *fmt_str = NULL;
12175 /* Verify the required arguments in the original call. We deal with two
12176 types of sprintf() calls: 'sprintf (str, fmt)' and
12177 'sprintf (dest, "%s", orig)'. */
12178 if (!validate_arg (dest, POINTER_TYPE)
12179 || !validate_arg (fmt, POINTER_TYPE))
12180 return NULL_TREE;
12181 if (orig && !validate_arg (orig, POINTER_TYPE))
12182 return NULL_TREE;
12184 /* Check whether the format is a literal string constant. */
12185 fmt_str = c_getstr (fmt);
12186 if (fmt_str == NULL)
12187 return NULL_TREE;
12189 call = NULL_TREE;
12190 retval = NULL_TREE;
12192 if (!init_target_chars ())
12193 return NULL_TREE;
12195 /* If the format doesn't contain % args or %%, use strcpy. */
12196 if (strchr (fmt_str, target_percent) == NULL)
12198 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12200 if (!fn)
12201 return NULL_TREE;
12203 /* Don't optimize sprintf (buf, "abc", ptr++). */
12204 if (orig)
12205 return NULL_TREE;
12207 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12208 'format' is known to contain no % formats. */
12209 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12210 if (!ignored)
12211 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12214 /* If the format is "%s", use strcpy if the result isn't used. */
12215 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12217 tree fn;
12218 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12220 if (!fn)
12221 return NULL_TREE;
12223 /* Don't crash on sprintf (str1, "%s"). */
12224 if (!orig)
12225 return NULL_TREE;
12227 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12228 if (!ignored)
12230 retval = c_strlen (orig, 1);
12231 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12232 return NULL_TREE;
12234 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12237 if (call && retval)
12239 retval = fold_convert_loc
12240 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12241 retval);
12242 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12244 else
12245 return call;
12248 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12249 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12250 attempt to simplify calls with more than 4 arguments.
12252 Return NULL_TREE if no simplification was possible, otherwise return the
12253 simplified form of the call as a tree. If IGNORED is true, it means that
12254 the caller does not use the returned value of the function. */
12256 static tree
12257 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12258 tree orig, int ignored)
12260 tree call, retval;
12261 const char *fmt_str = NULL;
12262 unsigned HOST_WIDE_INT destlen;
12264 /* Verify the required arguments in the original call. We deal with two
12265 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12266 'snprintf (dest, cst, "%s", orig)'. */
12267 if (!validate_arg (dest, POINTER_TYPE)
12268 || !validate_arg (destsize, INTEGER_TYPE)
12269 || !validate_arg (fmt, POINTER_TYPE))
12270 return NULL_TREE;
12271 if (orig && !validate_arg (orig, POINTER_TYPE))
12272 return NULL_TREE;
12274 if (!tree_fits_uhwi_p (destsize))
12275 return NULL_TREE;
12277 /* Check whether the format is a literal string constant. */
12278 fmt_str = c_getstr (fmt);
12279 if (fmt_str == NULL)
12280 return NULL_TREE;
12282 call = NULL_TREE;
12283 retval = NULL_TREE;
12285 if (!init_target_chars ())
12286 return NULL_TREE;
12288 destlen = tree_to_uhwi (destsize);
12290 /* If the format doesn't contain % args or %%, use strcpy. */
12291 if (strchr (fmt_str, target_percent) == NULL)
12293 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12294 size_t len = strlen (fmt_str);
12296 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12297 if (orig)
12298 return NULL_TREE;
12300 /* We could expand this as
12301 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12302 or to
12303 memcpy (str, fmt_with_nul_at_cstm1, cst);
12304 but in the former case that might increase code size
12305 and in the latter case grow .rodata section too much.
12306 So punt for now. */
12307 if (len >= destlen)
12308 return NULL_TREE;
12310 if (!fn)
12311 return NULL_TREE;
12313 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12314 'format' is known to contain no % formats and
12315 strlen (fmt) < cst. */
12316 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12318 if (!ignored)
12319 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12322 /* If the format is "%s", use strcpy if the result isn't used. */
12323 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12325 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12326 unsigned HOST_WIDE_INT origlen;
12328 /* Don't crash on snprintf (str1, cst, "%s"). */
12329 if (!orig)
12330 return NULL_TREE;
12332 retval = c_strlen (orig, 1);
12333 if (!retval || !tree_fits_uhwi_p (retval))
12334 return NULL_TREE;
12336 origlen = tree_to_uhwi (retval);
12337 /* We could expand this as
12338 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12339 or to
12340 memcpy (str1, str2_with_nul_at_cstm1, cst);
12341 but in the former case that might increase code size
12342 and in the latter case grow .rodata section too much.
12343 So punt for now. */
12344 if (origlen >= destlen)
12345 return NULL_TREE;
12347 /* Convert snprintf (str1, cst, "%s", str2) into
12348 strcpy (str1, str2) if strlen (str2) < cst. */
12349 if (!fn)
12350 return NULL_TREE;
12352 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12354 if (ignored)
12355 retval = NULL_TREE;
12358 if (call && retval)
12360 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12361 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12362 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12364 else
12365 return call;
12368 /* Expand a call EXP to __builtin_object_size. */
12371 expand_builtin_object_size (tree exp)
12373 tree ost;
12374 int object_size_type;
12375 tree fndecl = get_callee_fndecl (exp);
12377 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12379 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12380 exp, fndecl);
12381 expand_builtin_trap ();
12382 return const0_rtx;
12385 ost = CALL_EXPR_ARG (exp, 1);
12386 STRIP_NOPS (ost);
12388 if (TREE_CODE (ost) != INTEGER_CST
12389 || tree_int_cst_sgn (ost) < 0
12390 || compare_tree_int (ost, 3) > 0)
12392 error ("%Klast argument of %D is not integer constant between 0 and 3",
12393 exp, fndecl);
12394 expand_builtin_trap ();
12395 return const0_rtx;
12398 object_size_type = tree_to_shwi (ost);
12400 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12403 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12404 FCODE is the BUILT_IN_* to use.
12405 Return NULL_RTX if we failed; the caller should emit a normal call,
12406 otherwise try to get the result in TARGET, if convenient (and in
12407 mode MODE if that's convenient). */
12409 static rtx
12410 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12411 enum built_in_function fcode)
12413 tree dest, src, len, size;
12415 if (!validate_arglist (exp,
12416 POINTER_TYPE,
12417 fcode == BUILT_IN_MEMSET_CHK
12418 ? INTEGER_TYPE : POINTER_TYPE,
12419 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12420 return NULL_RTX;
12422 dest = CALL_EXPR_ARG (exp, 0);
12423 src = CALL_EXPR_ARG (exp, 1);
12424 len = CALL_EXPR_ARG (exp, 2);
12425 size = CALL_EXPR_ARG (exp, 3);
12427 if (! tree_fits_uhwi_p (size))
12428 return NULL_RTX;
12430 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
12432 tree fn;
12434 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12436 warning_at (tree_nonartificial_location (exp),
12437 0, "%Kcall to %D will always overflow destination buffer",
12438 exp, get_callee_fndecl (exp));
12439 return NULL_RTX;
12442 fn = NULL_TREE;
12443 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12444 mem{cpy,pcpy,move,set} is available. */
12445 switch (fcode)
12447 case BUILT_IN_MEMCPY_CHK:
12448 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12449 break;
12450 case BUILT_IN_MEMPCPY_CHK:
12451 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12452 break;
12453 case BUILT_IN_MEMMOVE_CHK:
12454 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12455 break;
12456 case BUILT_IN_MEMSET_CHK:
12457 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12458 break;
12459 default:
12460 break;
12463 if (! fn)
12464 return NULL_RTX;
12466 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12467 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12468 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12469 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12471 else if (fcode == BUILT_IN_MEMSET_CHK)
12472 return NULL_RTX;
12473 else
12475 unsigned int dest_align = get_pointer_alignment (dest);
12477 /* If DEST is not a pointer type, call the normal function. */
12478 if (dest_align == 0)
12479 return NULL_RTX;
12481 /* If SRC and DEST are the same (and not volatile), do nothing. */
12482 if (operand_equal_p (src, dest, 0))
12484 tree expr;
12486 if (fcode != BUILT_IN_MEMPCPY_CHK)
12488 /* Evaluate and ignore LEN in case it has side-effects. */
12489 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12490 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12493 expr = fold_build_pointer_plus (dest, len);
12494 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12497 /* __memmove_chk special case. */
12498 if (fcode == BUILT_IN_MEMMOVE_CHK)
12500 unsigned int src_align = get_pointer_alignment (src);
12502 if (src_align == 0)
12503 return NULL_RTX;
12505 /* If src is categorized for a readonly section we can use
12506 normal __memcpy_chk. */
12507 if (readonly_data_expr (src))
12509 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12510 if (!fn)
12511 return NULL_RTX;
12512 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12513 dest, src, len, size);
12514 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12515 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12516 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12519 return NULL_RTX;
12523 /* Emit warning if a buffer overflow is detected at compile time. */
12525 static void
12526 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12528 int is_strlen = 0;
12529 tree len, size;
12530 location_t loc = tree_nonartificial_location (exp);
12532 switch (fcode)
12534 case BUILT_IN_STRCPY_CHK:
12535 case BUILT_IN_STPCPY_CHK:
12536 /* For __strcat_chk the warning will be emitted only if overflowing
12537 by at least strlen (dest) + 1 bytes. */
12538 case BUILT_IN_STRCAT_CHK:
12539 len = CALL_EXPR_ARG (exp, 1);
12540 size = CALL_EXPR_ARG (exp, 2);
12541 is_strlen = 1;
12542 break;
12543 case BUILT_IN_STRNCAT_CHK:
12544 case BUILT_IN_STRNCPY_CHK:
12545 case BUILT_IN_STPNCPY_CHK:
12546 len = CALL_EXPR_ARG (exp, 2);
12547 size = CALL_EXPR_ARG (exp, 3);
12548 break;
12549 case BUILT_IN_SNPRINTF_CHK:
12550 case BUILT_IN_VSNPRINTF_CHK:
12551 len = CALL_EXPR_ARG (exp, 1);
12552 size = CALL_EXPR_ARG (exp, 3);
12553 break;
12554 default:
12555 gcc_unreachable ();
12558 if (!len || !size)
12559 return;
12561 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12562 return;
12564 if (is_strlen)
12566 len = c_strlen (len, 1);
12567 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12568 return;
12570 else if (fcode == BUILT_IN_STRNCAT_CHK)
12572 tree src = CALL_EXPR_ARG (exp, 1);
12573 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
12574 return;
12575 src = c_strlen (src, 1);
12576 if (! src || ! tree_fits_uhwi_p (src))
12578 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12579 exp, get_callee_fndecl (exp));
12580 return;
12582 else if (tree_int_cst_lt (src, size))
12583 return;
12585 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
12586 return;
12588 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12589 exp, get_callee_fndecl (exp));
12592 /* Emit warning if a buffer overflow is detected at compile time
12593 in __sprintf_chk/__vsprintf_chk calls. */
12595 static void
12596 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12598 tree size, len, fmt;
12599 const char *fmt_str;
12600 int nargs = call_expr_nargs (exp);
12602 /* Verify the required arguments in the original call. */
12604 if (nargs < 4)
12605 return;
12606 size = CALL_EXPR_ARG (exp, 2);
12607 fmt = CALL_EXPR_ARG (exp, 3);
12609 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
12610 return;
12612 /* Check whether the format is a literal string constant. */
12613 fmt_str = c_getstr (fmt);
12614 if (fmt_str == NULL)
12615 return;
12617 if (!init_target_chars ())
12618 return;
12620 /* If the format doesn't contain % args or %%, we know its size. */
12621 if (strchr (fmt_str, target_percent) == 0)
12622 len = build_int_cstu (size_type_node, strlen (fmt_str));
12623 /* If the format is "%s" and first ... argument is a string literal,
12624 we know it too. */
12625 else if (fcode == BUILT_IN_SPRINTF_CHK
12626 && strcmp (fmt_str, target_percent_s) == 0)
12628 tree arg;
12630 if (nargs < 5)
12631 return;
12632 arg = CALL_EXPR_ARG (exp, 4);
12633 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12634 return;
12636 len = c_strlen (arg, 1);
12637 if (!len || ! tree_fits_uhwi_p (len))
12638 return;
12640 else
12641 return;
12643 if (! tree_int_cst_lt (len, size))
12644 warning_at (tree_nonartificial_location (exp),
12645 0, "%Kcall to %D will always overflow destination buffer",
12646 exp, get_callee_fndecl (exp));
12649 /* Emit warning if a free is called with address of a variable. */
12651 static void
12652 maybe_emit_free_warning (tree exp)
12654 tree arg = CALL_EXPR_ARG (exp, 0);
12656 STRIP_NOPS (arg);
12657 if (TREE_CODE (arg) != ADDR_EXPR)
12658 return;
12660 arg = get_base_address (TREE_OPERAND (arg, 0));
12661 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12662 return;
12664 if (SSA_VAR_P (arg))
12665 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12666 "%Kattempt to free a non-heap object %qD", exp, arg);
12667 else
12668 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12669 "%Kattempt to free a non-heap object", exp);
12672 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12673 if possible. */
12675 tree
12676 fold_builtin_object_size (tree ptr, tree ost)
12678 unsigned HOST_WIDE_INT bytes;
12679 int object_size_type;
12681 if (!validate_arg (ptr, POINTER_TYPE)
12682 || !validate_arg (ost, INTEGER_TYPE))
12683 return NULL_TREE;
12685 STRIP_NOPS (ost);
12687 if (TREE_CODE (ost) != INTEGER_CST
12688 || tree_int_cst_sgn (ost) < 0
12689 || compare_tree_int (ost, 3) > 0)
12690 return NULL_TREE;
12692 object_size_type = tree_to_shwi (ost);
12694 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12695 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12696 and (size_t) 0 for types 2 and 3. */
12697 if (TREE_SIDE_EFFECTS (ptr))
12698 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12700 if (TREE_CODE (ptr) == ADDR_EXPR)
12702 bytes = compute_builtin_object_size (ptr, object_size_type);
12703 if (double_int_fits_to_tree_p (size_type_node,
12704 double_int::from_uhwi (bytes)))
12705 return build_int_cstu (size_type_node, bytes);
12707 else if (TREE_CODE (ptr) == SSA_NAME)
12709 /* If object size is not known yet, delay folding until
12710 later. Maybe subsequent passes will help determining
12711 it. */
12712 bytes = compute_builtin_object_size (ptr, object_size_type);
12713 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12714 && double_int_fits_to_tree_p (size_type_node,
12715 double_int::from_uhwi (bytes)))
12716 return build_int_cstu (size_type_node, bytes);
12719 return NULL_TREE;
12722 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12723 DEST, SRC, LEN, and SIZE are the arguments to the call.
12724 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12725 code of the builtin. If MAXLEN is not NULL, it is maximum length
12726 passed as third argument. */
12728 tree
12729 fold_builtin_memory_chk (location_t loc, tree fndecl,
12730 tree dest, tree src, tree len, tree size,
12731 tree maxlen, bool ignore,
12732 enum built_in_function fcode)
12734 tree fn;
12736 if (!validate_arg (dest, POINTER_TYPE)
12737 || !validate_arg (src,
12738 (fcode == BUILT_IN_MEMSET_CHK
12739 ? INTEGER_TYPE : POINTER_TYPE))
12740 || !validate_arg (len, INTEGER_TYPE)
12741 || !validate_arg (size, INTEGER_TYPE))
12742 return NULL_TREE;
12744 /* If SRC and DEST are the same (and not volatile), return DEST
12745 (resp. DEST+LEN for __mempcpy_chk). */
12746 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12748 if (fcode != BUILT_IN_MEMPCPY_CHK)
12749 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12750 dest, len);
12751 else
12753 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12754 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12758 if (! tree_fits_uhwi_p (size))
12759 return NULL_TREE;
12761 if (! integer_all_onesp (size))
12763 if (! tree_fits_uhwi_p (len))
12765 /* If LEN is not constant, try MAXLEN too.
12766 For MAXLEN only allow optimizing into non-_ocs function
12767 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12768 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12770 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12772 /* (void) __mempcpy_chk () can be optimized into
12773 (void) __memcpy_chk (). */
12774 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12775 if (!fn)
12776 return NULL_TREE;
12778 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12780 return NULL_TREE;
12783 else
12784 maxlen = len;
12786 if (tree_int_cst_lt (size, maxlen))
12787 return NULL_TREE;
12790 fn = NULL_TREE;
12791 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12792 mem{cpy,pcpy,move,set} is available. */
12793 switch (fcode)
12795 case BUILT_IN_MEMCPY_CHK:
12796 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12797 break;
12798 case BUILT_IN_MEMPCPY_CHK:
12799 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12800 break;
12801 case BUILT_IN_MEMMOVE_CHK:
12802 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12803 break;
12804 case BUILT_IN_MEMSET_CHK:
12805 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12806 break;
12807 default:
12808 break;
12811 if (!fn)
12812 return NULL_TREE;
12814 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12817 /* Fold a call to the __st[rp]cpy_chk builtin.
12818 DEST, SRC, and SIZE are the arguments to the call.
12819 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12820 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12821 strings passed as second argument. */
12823 tree
12824 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12825 tree src, tree size,
12826 tree maxlen, bool ignore,
12827 enum built_in_function fcode)
12829 tree len, fn;
12831 if (!validate_arg (dest, POINTER_TYPE)
12832 || !validate_arg (src, POINTER_TYPE)
12833 || !validate_arg (size, INTEGER_TYPE))
12834 return NULL_TREE;
12836 /* If SRC and DEST are the same (and not volatile), return DEST. */
12837 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12838 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12840 if (! tree_fits_uhwi_p (size))
12841 return NULL_TREE;
12843 if (! integer_all_onesp (size))
12845 len = c_strlen (src, 1);
12846 if (! len || ! tree_fits_uhwi_p (len))
12848 /* If LEN is not constant, try MAXLEN too.
12849 For MAXLEN only allow optimizing into non-_ocs function
12850 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12851 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12853 if (fcode == BUILT_IN_STPCPY_CHK)
12855 if (! ignore)
12856 return NULL_TREE;
12858 /* If return value of __stpcpy_chk is ignored,
12859 optimize into __strcpy_chk. */
12860 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12861 if (!fn)
12862 return NULL_TREE;
12864 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12867 if (! len || TREE_SIDE_EFFECTS (len))
12868 return NULL_TREE;
12870 /* If c_strlen returned something, but not a constant,
12871 transform __strcpy_chk into __memcpy_chk. */
12872 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12873 if (!fn)
12874 return NULL_TREE;
12876 len = fold_convert_loc (loc, size_type_node, len);
12877 len = size_binop_loc (loc, PLUS_EXPR, len,
12878 build_int_cst (size_type_node, 1));
12879 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12880 build_call_expr_loc (loc, fn, 4,
12881 dest, src, len, size));
12884 else
12885 maxlen = len;
12887 if (! tree_int_cst_lt (maxlen, size))
12888 return NULL_TREE;
12891 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12892 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12893 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12894 if (!fn)
12895 return NULL_TREE;
12897 return build_call_expr_loc (loc, fn, 2, dest, src);
12900 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12901 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12902 length passed as third argument. IGNORE is true if return value can be
12903 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12905 tree
12906 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12907 tree len, tree size, tree maxlen, bool ignore,
12908 enum built_in_function fcode)
12910 tree fn;
12912 if (!validate_arg (dest, POINTER_TYPE)
12913 || !validate_arg (src, POINTER_TYPE)
12914 || !validate_arg (len, INTEGER_TYPE)
12915 || !validate_arg (size, INTEGER_TYPE))
12916 return NULL_TREE;
12918 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12920 /* If return value of __stpncpy_chk is ignored,
12921 optimize into __strncpy_chk. */
12922 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12923 if (fn)
12924 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12927 if (! tree_fits_uhwi_p (size))
12928 return NULL_TREE;
12930 if (! integer_all_onesp (size))
12932 if (! tree_fits_uhwi_p (len))
12934 /* If LEN is not constant, try MAXLEN too.
12935 For MAXLEN only allow optimizing into non-_ocs function
12936 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12937 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
12938 return NULL_TREE;
12940 else
12941 maxlen = len;
12943 if (tree_int_cst_lt (size, maxlen))
12944 return NULL_TREE;
12947 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12948 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12949 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12950 if (!fn)
12951 return NULL_TREE;
12953 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12956 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12957 are the arguments to the call. */
12959 static tree
12960 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12961 tree src, tree size)
12963 tree fn;
12964 const char *p;
12966 if (!validate_arg (dest, POINTER_TYPE)
12967 || !validate_arg (src, POINTER_TYPE)
12968 || !validate_arg (size, INTEGER_TYPE))
12969 return NULL_TREE;
12971 p = c_getstr (src);
12972 /* If the SRC parameter is "", return DEST. */
12973 if (p && *p == '\0')
12974 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12976 if (! tree_fits_uhwi_p (size) || ! integer_all_onesp (size))
12977 return NULL_TREE;
12979 /* If __builtin_strcat_chk is used, assume strcat is available. */
12980 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12981 if (!fn)
12982 return NULL_TREE;
12984 return build_call_expr_loc (loc, fn, 2, dest, src);
12987 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12988 LEN, and SIZE. */
12990 static tree
12991 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12992 tree dest, tree src, tree len, tree size)
12994 tree fn;
12995 const char *p;
12997 if (!validate_arg (dest, POINTER_TYPE)
12998 || !validate_arg (src, POINTER_TYPE)
12999 || !validate_arg (size, INTEGER_TYPE)
13000 || !validate_arg (size, INTEGER_TYPE))
13001 return NULL_TREE;
13003 p = c_getstr (src);
13004 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
13005 if (p && *p == '\0')
13006 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
13007 else if (integer_zerop (len))
13008 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
13010 if (! tree_fits_uhwi_p (size))
13011 return NULL_TREE;
13013 if (! integer_all_onesp (size))
13015 tree src_len = c_strlen (src, 1);
13016 if (src_len
13017 && tree_fits_uhwi_p (src_len)
13018 && tree_fits_uhwi_p (len)
13019 && ! tree_int_cst_lt (len, src_len))
13021 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
13022 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
13023 if (!fn)
13024 return NULL_TREE;
13026 return build_call_expr_loc (loc, fn, 3, dest, src, size);
13028 return NULL_TREE;
13031 /* If __builtin_strncat_chk is used, assume strncat is available. */
13032 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13033 if (!fn)
13034 return NULL_TREE;
13036 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13039 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13040 Return NULL_TREE if a normal call should be emitted rather than
13041 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13042 or BUILT_IN_VSPRINTF_CHK. */
13044 static tree
13045 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13046 enum built_in_function fcode)
13048 tree dest, size, len, fn, fmt, flag;
13049 const char *fmt_str;
13051 /* Verify the required arguments in the original call. */
13052 if (nargs < 4)
13053 return NULL_TREE;
13054 dest = args[0];
13055 if (!validate_arg (dest, POINTER_TYPE))
13056 return NULL_TREE;
13057 flag = args[1];
13058 if (!validate_arg (flag, INTEGER_TYPE))
13059 return NULL_TREE;
13060 size = args[2];
13061 if (!validate_arg (size, INTEGER_TYPE))
13062 return NULL_TREE;
13063 fmt = args[3];
13064 if (!validate_arg (fmt, POINTER_TYPE))
13065 return NULL_TREE;
13067 if (! tree_fits_uhwi_p (size))
13068 return NULL_TREE;
13070 len = NULL_TREE;
13072 if (!init_target_chars ())
13073 return NULL_TREE;
13075 /* Check whether the format is a literal string constant. */
13076 fmt_str = c_getstr (fmt);
13077 if (fmt_str != NULL)
13079 /* If the format doesn't contain % args or %%, we know the size. */
13080 if (strchr (fmt_str, target_percent) == 0)
13082 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13083 len = build_int_cstu (size_type_node, strlen (fmt_str));
13085 /* If the format is "%s" and first ... argument is a string literal,
13086 we know the size too. */
13087 else if (fcode == BUILT_IN_SPRINTF_CHK
13088 && strcmp (fmt_str, target_percent_s) == 0)
13090 tree arg;
13092 if (nargs == 5)
13094 arg = args[4];
13095 if (validate_arg (arg, POINTER_TYPE))
13097 len = c_strlen (arg, 1);
13098 if (! len || ! tree_fits_uhwi_p (len))
13099 len = NULL_TREE;
13105 if (! integer_all_onesp (size))
13107 if (! len || ! tree_int_cst_lt (len, size))
13108 return NULL_TREE;
13111 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13112 or if format doesn't contain % chars or is "%s". */
13113 if (! integer_zerop (flag))
13115 if (fmt_str == NULL)
13116 return NULL_TREE;
13117 if (strchr (fmt_str, target_percent) != NULL
13118 && strcmp (fmt_str, target_percent_s))
13119 return NULL_TREE;
13122 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13123 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13124 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13125 if (!fn)
13126 return NULL_TREE;
13128 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13131 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13132 a normal call should be emitted rather than expanding the function
13133 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13135 static tree
13136 fold_builtin_sprintf_chk (location_t loc, tree exp,
13137 enum built_in_function fcode)
13139 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13140 CALL_EXPR_ARGP (exp), fcode);
13143 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13144 NULL_TREE if a normal call should be emitted rather than expanding
13145 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13146 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13147 passed as second argument. */
13149 static tree
13150 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13151 tree maxlen, enum built_in_function fcode)
13153 tree dest, size, len, fn, fmt, flag;
13154 const char *fmt_str;
13156 /* Verify the required arguments in the original call. */
13157 if (nargs < 5)
13158 return NULL_TREE;
13159 dest = args[0];
13160 if (!validate_arg (dest, POINTER_TYPE))
13161 return NULL_TREE;
13162 len = args[1];
13163 if (!validate_arg (len, INTEGER_TYPE))
13164 return NULL_TREE;
13165 flag = args[2];
13166 if (!validate_arg (flag, INTEGER_TYPE))
13167 return NULL_TREE;
13168 size = args[3];
13169 if (!validate_arg (size, INTEGER_TYPE))
13170 return NULL_TREE;
13171 fmt = args[4];
13172 if (!validate_arg (fmt, POINTER_TYPE))
13173 return NULL_TREE;
13175 if (! tree_fits_uhwi_p (size))
13176 return NULL_TREE;
13178 if (! integer_all_onesp (size))
13180 if (! tree_fits_uhwi_p (len))
13182 /* If LEN is not constant, try MAXLEN too.
13183 For MAXLEN only allow optimizing into non-_ocs function
13184 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13185 if (maxlen == NULL_TREE || ! tree_fits_uhwi_p (maxlen))
13186 return NULL_TREE;
13188 else
13189 maxlen = len;
13191 if (tree_int_cst_lt (size, maxlen))
13192 return NULL_TREE;
13195 if (!init_target_chars ())
13196 return NULL_TREE;
13198 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13199 or if format doesn't contain % chars or is "%s". */
13200 if (! integer_zerop (flag))
13202 fmt_str = c_getstr (fmt);
13203 if (fmt_str == NULL)
13204 return NULL_TREE;
13205 if (strchr (fmt_str, target_percent) != NULL
13206 && strcmp (fmt_str, target_percent_s))
13207 return NULL_TREE;
13210 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13211 available. */
13212 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13213 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13214 if (!fn)
13215 return NULL_TREE;
13217 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13220 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13221 a normal call should be emitted rather than expanding the function
13222 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13223 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13224 passed as second argument. */
13226 static tree
13227 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13228 enum built_in_function fcode)
13230 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13231 CALL_EXPR_ARGP (exp), maxlen, fcode);
13234 /* Builtins with folding operations that operate on "..." arguments
13235 need special handling; we need to store the arguments in a convenient
13236 data structure before attempting any folding. Fortunately there are
13237 only a few builtins that fall into this category. FNDECL is the
13238 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13239 result of the function call is ignored. */
13241 static tree
13242 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
13243 bool ignore ATTRIBUTE_UNUSED)
13245 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13246 tree ret = NULL_TREE;
13248 switch (fcode)
13250 case BUILT_IN_SPRINTF_CHK:
13251 case BUILT_IN_VSPRINTF_CHK:
13252 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
13253 break;
13255 case BUILT_IN_SNPRINTF_CHK:
13256 case BUILT_IN_VSNPRINTF_CHK:
13257 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
13258 break;
13260 case BUILT_IN_FPCLASSIFY:
13261 ret = fold_builtin_fpclassify (loc, exp);
13262 break;
13264 default:
13265 break;
13267 if (ret)
13269 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13270 SET_EXPR_LOCATION (ret, loc);
13271 TREE_NO_WARNING (ret) = 1;
13272 return ret;
13274 return NULL_TREE;
13277 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13278 FMT and ARG are the arguments to the call; we don't fold cases with
13279 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13281 Return NULL_TREE if no simplification was possible, otherwise return the
13282 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13283 code of the function to be simplified. */
13285 static tree
13286 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13287 tree arg, bool ignore,
13288 enum built_in_function fcode)
13290 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13291 const char *fmt_str = NULL;
13293 /* If the return value is used, don't do the transformation. */
13294 if (! ignore)
13295 return NULL_TREE;
13297 /* Verify the required arguments in the original call. */
13298 if (!validate_arg (fmt, POINTER_TYPE))
13299 return NULL_TREE;
13301 /* Check whether the format is a literal string constant. */
13302 fmt_str = c_getstr (fmt);
13303 if (fmt_str == NULL)
13304 return NULL_TREE;
13306 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13308 /* If we're using an unlocked function, assume the other
13309 unlocked functions exist explicitly. */
13310 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13311 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13313 else
13315 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13316 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13319 if (!init_target_chars ())
13320 return NULL_TREE;
13322 if (strcmp (fmt_str, target_percent_s) == 0
13323 || strchr (fmt_str, target_percent) == NULL)
13325 const char *str;
13327 if (strcmp (fmt_str, target_percent_s) == 0)
13329 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13330 return NULL_TREE;
13332 if (!arg || !validate_arg (arg, POINTER_TYPE))
13333 return NULL_TREE;
13335 str = c_getstr (arg);
13336 if (str == NULL)
13337 return NULL_TREE;
13339 else
13341 /* The format specifier doesn't contain any '%' characters. */
13342 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13343 && arg)
13344 return NULL_TREE;
13345 str = fmt_str;
13348 /* If the string was "", printf does nothing. */
13349 if (str[0] == '\0')
13350 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13352 /* If the string has length of 1, call putchar. */
13353 if (str[1] == '\0')
13355 /* Given printf("c"), (where c is any one character,)
13356 convert "c"[0] to an int and pass that to the replacement
13357 function. */
13358 newarg = build_int_cst (integer_type_node, str[0]);
13359 if (fn_putchar)
13360 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13362 else
13364 /* If the string was "string\n", call puts("string"). */
13365 size_t len = strlen (str);
13366 if ((unsigned char)str[len - 1] == target_newline
13367 && (size_t) (int) len == len
13368 && (int) len > 0)
13370 char *newstr;
13371 tree offset_node, string_cst;
13373 /* Create a NUL-terminated string that's one char shorter
13374 than the original, stripping off the trailing '\n'. */
13375 newarg = build_string_literal (len, str);
13376 string_cst = string_constant (newarg, &offset_node);
13377 gcc_checking_assert (string_cst
13378 && (TREE_STRING_LENGTH (string_cst)
13379 == (int) len)
13380 && integer_zerop (offset_node)
13381 && (unsigned char)
13382 TREE_STRING_POINTER (string_cst)[len - 1]
13383 == target_newline);
13384 /* build_string_literal creates a new STRING_CST,
13385 modify it in place to avoid double copying. */
13386 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13387 newstr[len - 1] = '\0';
13388 if (fn_puts)
13389 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13391 else
13392 /* We'd like to arrange to call fputs(string,stdout) here,
13393 but we need stdout and don't have a way to get it yet. */
13394 return NULL_TREE;
13398 /* The other optimizations can be done only on the non-va_list variants. */
13399 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13400 return NULL_TREE;
13402 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13403 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13405 if (!arg || !validate_arg (arg, POINTER_TYPE))
13406 return NULL_TREE;
13407 if (fn_puts)
13408 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13411 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13412 else if (strcmp (fmt_str, target_percent_c) == 0)
13414 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13415 return NULL_TREE;
13416 if (fn_putchar)
13417 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13420 if (!call)
13421 return NULL_TREE;
13423 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13426 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13427 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13428 more than 3 arguments, and ARG may be null in the 2-argument case.
13430 Return NULL_TREE if no simplification was possible, otherwise return the
13431 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13432 code of the function to be simplified. */
13434 static tree
13435 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13436 tree fmt, tree arg, bool ignore,
13437 enum built_in_function fcode)
13439 tree fn_fputc, fn_fputs, call = NULL_TREE;
13440 const char *fmt_str = NULL;
13442 /* If the return value is used, don't do the transformation. */
13443 if (! ignore)
13444 return NULL_TREE;
13446 /* Verify the required arguments in the original call. */
13447 if (!validate_arg (fp, POINTER_TYPE))
13448 return NULL_TREE;
13449 if (!validate_arg (fmt, POINTER_TYPE))
13450 return NULL_TREE;
13452 /* Check whether the format is a literal string constant. */
13453 fmt_str = c_getstr (fmt);
13454 if (fmt_str == NULL)
13455 return NULL_TREE;
13457 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13459 /* If we're using an unlocked function, assume the other
13460 unlocked functions exist explicitly. */
13461 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13462 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13464 else
13466 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13467 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13470 if (!init_target_chars ())
13471 return NULL_TREE;
13473 /* If the format doesn't contain % args or %%, use strcpy. */
13474 if (strchr (fmt_str, target_percent) == NULL)
13476 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13477 && arg)
13478 return NULL_TREE;
13480 /* If the format specifier was "", fprintf does nothing. */
13481 if (fmt_str[0] == '\0')
13483 /* If FP has side-effects, just wait until gimplification is
13484 done. */
13485 if (TREE_SIDE_EFFECTS (fp))
13486 return NULL_TREE;
13488 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13491 /* When "string" doesn't contain %, replace all cases of
13492 fprintf (fp, string) with fputs (string, fp). The fputs
13493 builtin will take care of special cases like length == 1. */
13494 if (fn_fputs)
13495 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13498 /* The other optimizations can be done only on the non-va_list variants. */
13499 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13500 return NULL_TREE;
13502 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13503 else if (strcmp (fmt_str, target_percent_s) == 0)
13505 if (!arg || !validate_arg (arg, POINTER_TYPE))
13506 return NULL_TREE;
13507 if (fn_fputs)
13508 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13511 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13512 else if (strcmp (fmt_str, target_percent_c) == 0)
13514 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13515 return NULL_TREE;
13516 if (fn_fputc)
13517 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13520 if (!call)
13521 return NULL_TREE;
13522 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13525 /* Initialize format string characters in the target charset. */
13527 static bool
13528 init_target_chars (void)
13530 static bool init;
13531 if (!init)
13533 target_newline = lang_hooks.to_target_charset ('\n');
13534 target_percent = lang_hooks.to_target_charset ('%');
13535 target_c = lang_hooks.to_target_charset ('c');
13536 target_s = lang_hooks.to_target_charset ('s');
13537 if (target_newline == 0 || target_percent == 0 || target_c == 0
13538 || target_s == 0)
13539 return false;
13541 target_percent_c[0] = target_percent;
13542 target_percent_c[1] = target_c;
13543 target_percent_c[2] = '\0';
13545 target_percent_s[0] = target_percent;
13546 target_percent_s[1] = target_s;
13547 target_percent_s[2] = '\0';
13549 target_percent_s_newline[0] = target_percent;
13550 target_percent_s_newline[1] = target_s;
13551 target_percent_s_newline[2] = target_newline;
13552 target_percent_s_newline[3] = '\0';
13554 init = true;
13556 return true;
13559 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13560 and no overflow/underflow occurred. INEXACT is true if M was not
13561 exactly calculated. TYPE is the tree type for the result. This
13562 function assumes that you cleared the MPFR flags and then
13563 calculated M to see if anything subsequently set a flag prior to
13564 entering this function. Return NULL_TREE if any checks fail. */
13566 static tree
13567 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13569 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13570 overflow/underflow occurred. If -frounding-math, proceed iff the
13571 result of calling FUNC was exact. */
13572 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13573 && (!flag_rounding_math || !inexact))
13575 REAL_VALUE_TYPE rr;
13577 real_from_mpfr (&rr, m, type, GMP_RNDN);
13578 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13579 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13580 but the mpft_t is not, then we underflowed in the
13581 conversion. */
13582 if (real_isfinite (&rr)
13583 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13585 REAL_VALUE_TYPE rmode;
13587 real_convert (&rmode, TYPE_MODE (type), &rr);
13588 /* Proceed iff the specified mode can hold the value. */
13589 if (real_identical (&rmode, &rr))
13590 return build_real (type, rmode);
13593 return NULL_TREE;
13596 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13597 number and no overflow/underflow occurred. INEXACT is true if M
13598 was not exactly calculated. TYPE is the tree type for the result.
13599 This function assumes that you cleared the MPFR flags and then
13600 calculated M to see if anything subsequently set a flag prior to
13601 entering this function. Return NULL_TREE if any checks fail, if
13602 FORCE_CONVERT is true, then bypass the checks. */
13604 static tree
13605 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13607 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13608 overflow/underflow occurred. If -frounding-math, proceed iff the
13609 result of calling FUNC was exact. */
13610 if (force_convert
13611 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13612 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13613 && (!flag_rounding_math || !inexact)))
13615 REAL_VALUE_TYPE re, im;
13617 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13618 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13619 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13620 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13621 but the mpft_t is not, then we underflowed in the
13622 conversion. */
13623 if (force_convert
13624 || (real_isfinite (&re) && real_isfinite (&im)
13625 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13626 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13628 REAL_VALUE_TYPE re_mode, im_mode;
13630 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13631 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13632 /* Proceed iff the specified mode can hold the value. */
13633 if (force_convert
13634 || (real_identical (&re_mode, &re)
13635 && real_identical (&im_mode, &im)))
13636 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13637 build_real (TREE_TYPE (type), im_mode));
13640 return NULL_TREE;
13643 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13644 FUNC on it and return the resulting value as a tree with type TYPE.
13645 If MIN and/or MAX are not NULL, then the supplied ARG must be
13646 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13647 acceptable values, otherwise they are not. The mpfr precision is
13648 set to the precision of TYPE. We assume that function FUNC returns
13649 zero if the result could be calculated exactly within the requested
13650 precision. */
13652 static tree
13653 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13654 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13655 bool inclusive)
13657 tree result = NULL_TREE;
13659 STRIP_NOPS (arg);
13661 /* To proceed, MPFR must exactly represent the target floating point
13662 format, which only happens when the target base equals two. */
13663 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13664 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13666 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13668 if (real_isfinite (ra)
13669 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13670 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13672 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13673 const int prec = fmt->p;
13674 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13675 int inexact;
13676 mpfr_t m;
13678 mpfr_init2 (m, prec);
13679 mpfr_from_real (m, ra, GMP_RNDN);
13680 mpfr_clear_flags ();
13681 inexact = func (m, m, rnd);
13682 result = do_mpfr_ckconv (m, type, inexact);
13683 mpfr_clear (m);
13687 return result;
13690 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13691 FUNC on it and return the resulting value as a tree with type TYPE.
13692 The mpfr precision is set to the precision of TYPE. We assume that
13693 function FUNC returns zero if the result could be calculated
13694 exactly within the requested precision. */
13696 static tree
13697 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13698 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13700 tree result = NULL_TREE;
13702 STRIP_NOPS (arg1);
13703 STRIP_NOPS (arg2);
13705 /* To proceed, MPFR must exactly represent the target floating point
13706 format, which only happens when the target base equals two. */
13707 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13708 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13709 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13711 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13712 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13714 if (real_isfinite (ra1) && real_isfinite (ra2))
13716 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13717 const int prec = fmt->p;
13718 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13719 int inexact;
13720 mpfr_t m1, m2;
13722 mpfr_inits2 (prec, m1, m2, NULL);
13723 mpfr_from_real (m1, ra1, GMP_RNDN);
13724 mpfr_from_real (m2, ra2, GMP_RNDN);
13725 mpfr_clear_flags ();
13726 inexact = func (m1, m1, m2, rnd);
13727 result = do_mpfr_ckconv (m1, type, inexact);
13728 mpfr_clears (m1, m2, NULL);
13732 return result;
13735 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13736 FUNC on it and return the resulting value as a tree with type TYPE.
13737 The mpfr precision is set to the precision of TYPE. We assume that
13738 function FUNC returns zero if the result could be calculated
13739 exactly within the requested precision. */
13741 static tree
13742 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13743 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13745 tree result = NULL_TREE;
13747 STRIP_NOPS (arg1);
13748 STRIP_NOPS (arg2);
13749 STRIP_NOPS (arg3);
13751 /* To proceed, MPFR must exactly represent the target floating point
13752 format, which only happens when the target base equals two. */
13753 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13754 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13755 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13756 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13758 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13759 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13760 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13762 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13764 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13765 const int prec = fmt->p;
13766 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13767 int inexact;
13768 mpfr_t m1, m2, m3;
13770 mpfr_inits2 (prec, m1, m2, m3, NULL);
13771 mpfr_from_real (m1, ra1, GMP_RNDN);
13772 mpfr_from_real (m2, ra2, GMP_RNDN);
13773 mpfr_from_real (m3, ra3, GMP_RNDN);
13774 mpfr_clear_flags ();
13775 inexact = func (m1, m1, m2, m3, rnd);
13776 result = do_mpfr_ckconv (m1, type, inexact);
13777 mpfr_clears (m1, m2, m3, NULL);
13781 return result;
13784 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13785 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13786 If ARG_SINP and ARG_COSP are NULL then the result is returned
13787 as a complex value.
13788 The type is taken from the type of ARG and is used for setting the
13789 precision of the calculation and results. */
13791 static tree
13792 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13794 tree const type = TREE_TYPE (arg);
13795 tree result = NULL_TREE;
13797 STRIP_NOPS (arg);
13799 /* To proceed, MPFR must exactly represent the target floating point
13800 format, which only happens when the target base equals two. */
13801 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13802 && TREE_CODE (arg) == REAL_CST
13803 && !TREE_OVERFLOW (arg))
13805 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13807 if (real_isfinite (ra))
13809 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13810 const int prec = fmt->p;
13811 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13812 tree result_s, result_c;
13813 int inexact;
13814 mpfr_t m, ms, mc;
13816 mpfr_inits2 (prec, m, ms, mc, NULL);
13817 mpfr_from_real (m, ra, GMP_RNDN);
13818 mpfr_clear_flags ();
13819 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13820 result_s = do_mpfr_ckconv (ms, type, inexact);
13821 result_c = do_mpfr_ckconv (mc, type, inexact);
13822 mpfr_clears (m, ms, mc, NULL);
13823 if (result_s && result_c)
13825 /* If we are to return in a complex value do so. */
13826 if (!arg_sinp && !arg_cosp)
13827 return build_complex (build_complex_type (type),
13828 result_c, result_s);
13830 /* Dereference the sin/cos pointer arguments. */
13831 arg_sinp = build_fold_indirect_ref (arg_sinp);
13832 arg_cosp = build_fold_indirect_ref (arg_cosp);
13833 /* Proceed if valid pointer type were passed in. */
13834 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13835 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13837 /* Set the values. */
13838 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13839 result_s);
13840 TREE_SIDE_EFFECTS (result_s) = 1;
13841 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13842 result_c);
13843 TREE_SIDE_EFFECTS (result_c) = 1;
13844 /* Combine the assignments into a compound expr. */
13845 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13846 result_s, result_c));
13851 return result;
13854 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13855 two-argument mpfr order N Bessel function FUNC on them and return
13856 the resulting value as a tree with type TYPE. The mpfr precision
13857 is set to the precision of TYPE. We assume that function FUNC
13858 returns zero if the result could be calculated exactly within the
13859 requested precision. */
13860 static tree
13861 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13862 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13863 const REAL_VALUE_TYPE *min, bool inclusive)
13865 tree result = NULL_TREE;
13867 STRIP_NOPS (arg1);
13868 STRIP_NOPS (arg2);
13870 /* To proceed, MPFR must exactly represent the target floating point
13871 format, which only happens when the target base equals two. */
13872 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13873 && tree_fits_shwi_p (arg1)
13874 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13876 const HOST_WIDE_INT n = tree_to_shwi (arg1);
13877 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13879 if (n == (long)n
13880 && real_isfinite (ra)
13881 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13883 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13884 const int prec = fmt->p;
13885 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13886 int inexact;
13887 mpfr_t m;
13889 mpfr_init2 (m, prec);
13890 mpfr_from_real (m, ra, GMP_RNDN);
13891 mpfr_clear_flags ();
13892 inexact = func (m, n, m, rnd);
13893 result = do_mpfr_ckconv (m, type, inexact);
13894 mpfr_clear (m);
13898 return result;
13901 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13902 the pointer *(ARG_QUO) and return the result. The type is taken
13903 from the type of ARG0 and is used for setting the precision of the
13904 calculation and results. */
13906 static tree
13907 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13909 tree const type = TREE_TYPE (arg0);
13910 tree result = NULL_TREE;
13912 STRIP_NOPS (arg0);
13913 STRIP_NOPS (arg1);
13915 /* To proceed, MPFR must exactly represent the target floating point
13916 format, which only happens when the target base equals two. */
13917 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13918 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13919 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13921 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13922 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13924 if (real_isfinite (ra0) && real_isfinite (ra1))
13926 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13927 const int prec = fmt->p;
13928 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13929 tree result_rem;
13930 long integer_quo;
13931 mpfr_t m0, m1;
13933 mpfr_inits2 (prec, m0, m1, NULL);
13934 mpfr_from_real (m0, ra0, GMP_RNDN);
13935 mpfr_from_real (m1, ra1, GMP_RNDN);
13936 mpfr_clear_flags ();
13937 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13938 /* Remquo is independent of the rounding mode, so pass
13939 inexact=0 to do_mpfr_ckconv(). */
13940 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13941 mpfr_clears (m0, m1, NULL);
13942 if (result_rem)
13944 /* MPFR calculates quo in the host's long so it may
13945 return more bits in quo than the target int can hold
13946 if sizeof(host long) > sizeof(target int). This can
13947 happen even for native compilers in LP64 mode. In
13948 these cases, modulo the quo value with the largest
13949 number that the target int can hold while leaving one
13950 bit for the sign. */
13951 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13952 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13954 /* Dereference the quo pointer argument. */
13955 arg_quo = build_fold_indirect_ref (arg_quo);
13956 /* Proceed iff a valid pointer type was passed in. */
13957 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13959 /* Set the value. */
13960 tree result_quo
13961 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13962 build_int_cst (TREE_TYPE (arg_quo),
13963 integer_quo));
13964 TREE_SIDE_EFFECTS (result_quo) = 1;
13965 /* Combine the quo assignment with the rem. */
13966 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13967 result_quo, result_rem));
13972 return result;
13975 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13976 resulting value as a tree with type TYPE. The mpfr precision is
13977 set to the precision of TYPE. We assume that this mpfr function
13978 returns zero if the result could be calculated exactly within the
13979 requested precision. In addition, the integer pointer represented
13980 by ARG_SG will be dereferenced and set to the appropriate signgam
13981 (-1,1) value. */
13983 static tree
13984 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13986 tree result = NULL_TREE;
13988 STRIP_NOPS (arg);
13990 /* To proceed, MPFR must exactly represent the target floating point
13991 format, which only happens when the target base equals two. Also
13992 verify ARG is a constant and that ARG_SG is an int pointer. */
13993 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13994 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13995 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13996 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13998 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
14000 /* In addition to NaN and Inf, the argument cannot be zero or a
14001 negative integer. */
14002 if (real_isfinite (ra)
14003 && ra->cl != rvc_zero
14004 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
14006 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
14007 const int prec = fmt->p;
14008 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
14009 int inexact, sg;
14010 mpfr_t m;
14011 tree result_lg;
14013 mpfr_init2 (m, prec);
14014 mpfr_from_real (m, ra, GMP_RNDN);
14015 mpfr_clear_flags ();
14016 inexact = mpfr_lgamma (m, &sg, m, rnd);
14017 result_lg = do_mpfr_ckconv (m, type, inexact);
14018 mpfr_clear (m);
14019 if (result_lg)
14021 tree result_sg;
14023 /* Dereference the arg_sg pointer argument. */
14024 arg_sg = build_fold_indirect_ref (arg_sg);
14025 /* Assign the signgam value into *arg_sg. */
14026 result_sg = fold_build2 (MODIFY_EXPR,
14027 TREE_TYPE (arg_sg), arg_sg,
14028 build_int_cst (TREE_TYPE (arg_sg), sg));
14029 TREE_SIDE_EFFECTS (result_sg) = 1;
14030 /* Combine the signgam assignment with the lgamma result. */
14031 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
14032 result_sg, result_lg));
14037 return result;
14040 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14041 function FUNC on it and return the resulting value as a tree with
14042 type TYPE. The mpfr precision is set to the precision of TYPE. We
14043 assume that function FUNC returns zero if the result could be
14044 calculated exactly within the requested precision. */
14046 static tree
14047 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
14049 tree result = NULL_TREE;
14051 STRIP_NOPS (arg);
14053 /* To proceed, MPFR must exactly represent the target floating point
14054 format, which only happens when the target base equals two. */
14055 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14056 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14057 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14059 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14060 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14062 if (real_isfinite (re) && real_isfinite (im))
14064 const struct real_format *const fmt =
14065 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14066 const int prec = fmt->p;
14067 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14068 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14069 int inexact;
14070 mpc_t m;
14072 mpc_init2 (m, prec);
14073 mpfr_from_real (mpc_realref (m), re, rnd);
14074 mpfr_from_real (mpc_imagref (m), im, rnd);
14075 mpfr_clear_flags ();
14076 inexact = func (m, m, crnd);
14077 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14078 mpc_clear (m);
14082 return result;
14085 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14086 mpc function FUNC on it and return the resulting value as a tree
14087 with type TYPE. The mpfr precision is set to the precision of
14088 TYPE. We assume that function FUNC returns zero if the result
14089 could be calculated exactly within the requested precision. If
14090 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14091 in the arguments and/or results. */
14093 tree
14094 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14095 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14097 tree result = NULL_TREE;
14099 STRIP_NOPS (arg0);
14100 STRIP_NOPS (arg1);
14102 /* To proceed, MPFR must exactly represent the target floating point
14103 format, which only happens when the target base equals two. */
14104 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14105 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14106 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14107 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14108 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14110 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14111 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14112 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14113 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14115 if (do_nonfinite
14116 || (real_isfinite (re0) && real_isfinite (im0)
14117 && real_isfinite (re1) && real_isfinite (im1)))
14119 const struct real_format *const fmt =
14120 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14121 const int prec = fmt->p;
14122 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14123 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14124 int inexact;
14125 mpc_t m0, m1;
14127 mpc_init2 (m0, prec);
14128 mpc_init2 (m1, prec);
14129 mpfr_from_real (mpc_realref (m0), re0, rnd);
14130 mpfr_from_real (mpc_imagref (m0), im0, rnd);
14131 mpfr_from_real (mpc_realref (m1), re1, rnd);
14132 mpfr_from_real (mpc_imagref (m1), im1, rnd);
14133 mpfr_clear_flags ();
14134 inexact = func (m0, m0, m1, crnd);
14135 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14136 mpc_clear (m0);
14137 mpc_clear (m1);
14141 return result;
14144 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14145 a normal call should be emitted rather than expanding the function
14146 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14148 static tree
14149 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14151 int nargs = gimple_call_num_args (stmt);
14153 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14154 (nargs > 0
14155 ? gimple_call_arg_ptr (stmt, 0)
14156 : &error_mark_node), fcode);
14159 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14160 a normal call should be emitted rather than expanding the function
14161 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14162 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14163 passed as second argument. */
14165 tree
14166 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14167 enum built_in_function fcode)
14169 int nargs = gimple_call_num_args (stmt);
14171 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14172 (nargs > 0
14173 ? gimple_call_arg_ptr (stmt, 0)
14174 : &error_mark_node), maxlen, fcode);
14177 /* Builtins with folding operations that operate on "..." arguments
14178 need special handling; we need to store the arguments in a convenient
14179 data structure before attempting any folding. Fortunately there are
14180 only a few builtins that fall into this category. FNDECL is the
14181 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14182 result of the function call is ignored. */
14184 static tree
14185 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14186 bool ignore ATTRIBUTE_UNUSED)
14188 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14189 tree ret = NULL_TREE;
14191 switch (fcode)
14193 case BUILT_IN_SPRINTF_CHK:
14194 case BUILT_IN_VSPRINTF_CHK:
14195 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14196 break;
14198 case BUILT_IN_SNPRINTF_CHK:
14199 case BUILT_IN_VSNPRINTF_CHK:
14200 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14202 default:
14203 break;
14205 if (ret)
14207 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14208 TREE_NO_WARNING (ret) = 1;
14209 return ret;
14211 return NULL_TREE;
14214 /* A wrapper function for builtin folding that prevents warnings for
14215 "statement without effect" and the like, caused by removing the
14216 call node earlier than the warning is generated. */
14218 tree
14219 fold_call_stmt (gimple stmt, bool ignore)
14221 tree ret = NULL_TREE;
14222 tree fndecl = gimple_call_fndecl (stmt);
14223 location_t loc = gimple_location (stmt);
14224 if (fndecl
14225 && TREE_CODE (fndecl) == FUNCTION_DECL
14226 && DECL_BUILT_IN (fndecl)
14227 && !gimple_call_va_arg_pack_p (stmt))
14229 int nargs = gimple_call_num_args (stmt);
14230 tree *args = (nargs > 0
14231 ? gimple_call_arg_ptr (stmt, 0)
14232 : &error_mark_node);
14234 if (avoid_folding_inline_builtin (fndecl))
14235 return NULL_TREE;
14236 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14238 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14240 else
14242 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14243 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14244 if (!ret)
14245 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14246 if (ret)
14248 /* Propagate location information from original call to
14249 expansion of builtin. Otherwise things like
14250 maybe_emit_chk_warning, that operate on the expansion
14251 of a builtin, will use the wrong location information. */
14252 if (gimple_has_location (stmt))
14254 tree realret = ret;
14255 if (TREE_CODE (ret) == NOP_EXPR)
14256 realret = TREE_OPERAND (ret, 0);
14257 if (CAN_HAVE_LOCATION_P (realret)
14258 && !EXPR_HAS_LOCATION (realret))
14259 SET_EXPR_LOCATION (realret, loc);
14260 return realret;
14262 return ret;
14266 return NULL_TREE;
14269 /* Look up the function in builtin_decl that corresponds to DECL
14270 and set ASMSPEC as its user assembler name. DECL must be a
14271 function decl that declares a builtin. */
14273 void
14274 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14276 tree builtin;
14277 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14278 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14279 && asmspec != 0);
14281 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14282 set_user_assembler_name (builtin, asmspec);
14283 switch (DECL_FUNCTION_CODE (decl))
14285 case BUILT_IN_MEMCPY:
14286 init_block_move_fn (asmspec);
14287 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14288 break;
14289 case BUILT_IN_MEMSET:
14290 init_block_clear_fn (asmspec);
14291 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14292 break;
14293 case BUILT_IN_MEMMOVE:
14294 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14295 break;
14296 case BUILT_IN_MEMCMP:
14297 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14298 break;
14299 case BUILT_IN_ABORT:
14300 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14301 break;
14302 case BUILT_IN_FFS:
14303 if (INT_TYPE_SIZE < BITS_PER_WORD)
14305 set_user_assembler_libfunc ("ffs", asmspec);
14306 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14307 MODE_INT, 0), "ffs");
14309 break;
14310 default:
14311 break;
14315 /* Return true if DECL is a builtin that expands to a constant or similarly
14316 simple code. */
14317 bool
14318 is_simple_builtin (tree decl)
14320 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14321 switch (DECL_FUNCTION_CODE (decl))
14323 /* Builtins that expand to constants. */
14324 case BUILT_IN_CONSTANT_P:
14325 case BUILT_IN_EXPECT:
14326 case BUILT_IN_OBJECT_SIZE:
14327 case BUILT_IN_UNREACHABLE:
14328 /* Simple register moves or loads from stack. */
14329 case BUILT_IN_ASSUME_ALIGNED:
14330 case BUILT_IN_RETURN_ADDRESS:
14331 case BUILT_IN_EXTRACT_RETURN_ADDR:
14332 case BUILT_IN_FROB_RETURN_ADDR:
14333 case BUILT_IN_RETURN:
14334 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14335 case BUILT_IN_FRAME_ADDRESS:
14336 case BUILT_IN_VA_END:
14337 case BUILT_IN_STACK_SAVE:
14338 case BUILT_IN_STACK_RESTORE:
14339 /* Exception state returns or moves registers around. */
14340 case BUILT_IN_EH_FILTER:
14341 case BUILT_IN_EH_POINTER:
14342 case BUILT_IN_EH_COPY_VALUES:
14343 return true;
14345 default:
14346 return false;
14349 return false;
14352 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14353 most probably expanded inline into reasonably simple code. This is a
14354 superset of is_simple_builtin. */
14355 bool
14356 is_inexpensive_builtin (tree decl)
14358 if (!decl)
14359 return false;
14360 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14361 return true;
14362 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14363 switch (DECL_FUNCTION_CODE (decl))
14365 case BUILT_IN_ABS:
14366 case BUILT_IN_ALLOCA:
14367 case BUILT_IN_ALLOCA_WITH_ALIGN:
14368 case BUILT_IN_BSWAP16:
14369 case BUILT_IN_BSWAP32:
14370 case BUILT_IN_BSWAP64:
14371 case BUILT_IN_CLZ:
14372 case BUILT_IN_CLZIMAX:
14373 case BUILT_IN_CLZL:
14374 case BUILT_IN_CLZLL:
14375 case BUILT_IN_CTZ:
14376 case BUILT_IN_CTZIMAX:
14377 case BUILT_IN_CTZL:
14378 case BUILT_IN_CTZLL:
14379 case BUILT_IN_FFS:
14380 case BUILT_IN_FFSIMAX:
14381 case BUILT_IN_FFSL:
14382 case BUILT_IN_FFSLL:
14383 case BUILT_IN_IMAXABS:
14384 case BUILT_IN_FINITE:
14385 case BUILT_IN_FINITEF:
14386 case BUILT_IN_FINITEL:
14387 case BUILT_IN_FINITED32:
14388 case BUILT_IN_FINITED64:
14389 case BUILT_IN_FINITED128:
14390 case BUILT_IN_FPCLASSIFY:
14391 case BUILT_IN_ISFINITE:
14392 case BUILT_IN_ISINF_SIGN:
14393 case BUILT_IN_ISINF:
14394 case BUILT_IN_ISINFF:
14395 case BUILT_IN_ISINFL:
14396 case BUILT_IN_ISINFD32:
14397 case BUILT_IN_ISINFD64:
14398 case BUILT_IN_ISINFD128:
14399 case BUILT_IN_ISNAN:
14400 case BUILT_IN_ISNANF:
14401 case BUILT_IN_ISNANL:
14402 case BUILT_IN_ISNAND32:
14403 case BUILT_IN_ISNAND64:
14404 case BUILT_IN_ISNAND128:
14405 case BUILT_IN_ISNORMAL:
14406 case BUILT_IN_ISGREATER:
14407 case BUILT_IN_ISGREATEREQUAL:
14408 case BUILT_IN_ISLESS:
14409 case BUILT_IN_ISLESSEQUAL:
14410 case BUILT_IN_ISLESSGREATER:
14411 case BUILT_IN_ISUNORDERED:
14412 case BUILT_IN_VA_ARG_PACK:
14413 case BUILT_IN_VA_ARG_PACK_LEN:
14414 case BUILT_IN_VA_COPY:
14415 case BUILT_IN_TRAP:
14416 case BUILT_IN_SAVEREGS:
14417 case BUILT_IN_POPCOUNTL:
14418 case BUILT_IN_POPCOUNTLL:
14419 case BUILT_IN_POPCOUNTIMAX:
14420 case BUILT_IN_POPCOUNT:
14421 case BUILT_IN_PARITYL:
14422 case BUILT_IN_PARITYLL:
14423 case BUILT_IN_PARITYIMAX:
14424 case BUILT_IN_PARITY:
14425 case BUILT_IN_LABS:
14426 case BUILT_IN_LLABS:
14427 case BUILT_IN_PREFETCH:
14428 return true;
14430 default:
14431 return is_simple_builtin (decl);
14434 return false;