2014-10-24 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / builtins.c
blobe47b936df55b3e138c2e923326d2c9e387c188d4
1 /* Expand builtin functions.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "machmode.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "stor-layout.h"
29 #include "calls.h"
30 #include "varasm.h"
31 #include "tree-object-size.h"
32 #include "realmpfr.h"
33 #include "basic-block.h"
34 #include "tree-ssa-alias.h"
35 #include "internal-fn.h"
36 #include "gimple-expr.h"
37 #include "is-a.h"
38 #include "gimple.h"
39 #include "flags.h"
40 #include "regs.h"
41 #include "hard-reg-set.h"
42 #include "except.h"
43 #include "hashtab.h"
44 #include "hash-set.h"
45 #include "vec.h"
46 #include "input.h"
47 #include "function.h"
48 #include "insn-config.h"
49 #include "expr.h"
50 #include "optabs.h"
51 #include "libfuncs.h"
52 #include "recog.h"
53 #include "output.h"
54 #include "typeclass.h"
55 #include "predict.h"
56 #include "tm_p.h"
57 #include "target.h"
58 #include "langhooks.h"
59 #include "tree-ssanames.h"
60 #include "tree-dfa.h"
61 #include "value-prof.h"
62 #include "diagnostic-core.h"
63 #include "builtins.h"
64 #include "ubsan.h"
65 #include "cilk.h"
68 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
70 struct target_builtins default_target_builtins;
71 #if SWITCHABLE_TARGET
72 struct target_builtins *this_target_builtins = &default_target_builtins;
73 #endif
75 /* Define the names of the builtin function types and codes. */
76 const char *const built_in_class_names[BUILT_IN_LAST]
77 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
79 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
80 const char * built_in_names[(int) END_BUILTINS] =
82 #include "builtins.def"
84 #undef DEF_BUILTIN
86 /* Setup an array of _DECL trees, make sure each element is
87 initialized to NULL_TREE. */
88 builtin_info_type builtin_info;
90 /* Non-zero if __builtin_constant_p should be folded right away. */
91 bool force_folding_builtin_constant_p;
93 static rtx c_readstr (const char *, enum machine_mode);
94 static int target_char_cast (tree, char *);
95 static rtx get_memory_rtx (tree, tree);
96 static int apply_args_size (void);
97 static int apply_result_size (void);
98 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
99 static rtx result_vector (int, rtx);
100 #endif
101 static void expand_builtin_update_setjmp_buf (rtx);
102 static void expand_builtin_prefetch (tree);
103 static rtx expand_builtin_apply_args (void);
104 static rtx expand_builtin_apply_args_1 (void);
105 static rtx expand_builtin_apply (rtx, rtx, rtx);
106 static void expand_builtin_return (rtx);
107 static enum type_class type_to_class (tree);
108 static rtx expand_builtin_classify_type (tree);
109 static void expand_errno_check (tree, rtx);
110 static rtx expand_builtin_mathfn (tree, rtx, rtx);
111 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
112 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
113 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
114 static rtx expand_builtin_interclass_mathfn (tree, rtx);
115 static rtx expand_builtin_sincos (tree);
116 static rtx expand_builtin_cexpi (tree, rtx);
117 static rtx expand_builtin_int_roundingfn (tree, rtx);
118 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
119 static rtx expand_builtin_next_arg (void);
120 static rtx expand_builtin_va_start (tree);
121 static rtx expand_builtin_va_end (tree);
122 static rtx expand_builtin_va_copy (tree);
123 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
124 static rtx expand_builtin_strcmp (tree, rtx);
125 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
126 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
127 static rtx expand_builtin_memcpy (tree, rtx);
128 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
130 enum machine_mode, int);
131 static rtx expand_builtin_strcpy (tree, rtx);
132 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
133 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
134 static rtx expand_builtin_strncpy (tree, rtx);
135 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
136 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
137 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
138 static rtx expand_builtin_bzero (tree);
139 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
140 static rtx expand_builtin_alloca (tree, bool);
141 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
142 static rtx expand_builtin_frame_address (tree, tree);
143 static tree stabilize_va_list_loc (location_t, tree, int);
144 static rtx expand_builtin_expect (tree, rtx);
145 static tree fold_builtin_constant_p (tree);
146 static tree fold_builtin_classify_type (tree);
147 static tree fold_builtin_strlen (location_t, tree, tree);
148 static tree fold_builtin_inf (location_t, tree, int);
149 static tree fold_builtin_nan (tree, tree, int);
150 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
151 static bool validate_arg (const_tree, enum tree_code code);
152 static bool integer_valued_real_p (tree);
153 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
154 static rtx expand_builtin_fabs (tree, rtx, rtx);
155 static rtx expand_builtin_signbit (tree, rtx);
156 static tree fold_builtin_sqrt (location_t, tree, tree);
157 static tree fold_builtin_cbrt (location_t, tree, tree);
158 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
159 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
160 static tree fold_builtin_cos (location_t, tree, tree, tree);
161 static tree fold_builtin_cosh (location_t, tree, tree, tree);
162 static tree fold_builtin_tan (tree, tree);
163 static tree fold_builtin_trunc (location_t, tree, tree);
164 static tree fold_builtin_floor (location_t, tree, tree);
165 static tree fold_builtin_ceil (location_t, tree, tree);
166 static tree fold_builtin_round (location_t, tree, tree);
167 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
168 static tree fold_builtin_bitop (tree, tree);
169 static tree fold_builtin_strchr (location_t, tree, tree, tree);
170 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
171 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
172 static tree fold_builtin_strcmp (location_t, tree, tree);
173 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
174 static tree fold_builtin_signbit (location_t, tree, tree);
175 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
176 static tree fold_builtin_isascii (location_t, tree);
177 static tree fold_builtin_toascii (location_t, tree);
178 static tree fold_builtin_isdigit (location_t, tree);
179 static tree fold_builtin_fabs (location_t, tree, tree);
180 static tree fold_builtin_abs (location_t, tree, tree);
181 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
182 enum tree_code);
183 static tree fold_builtin_0 (location_t, tree, bool);
184 static tree fold_builtin_1 (location_t, tree, tree, bool);
185 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
186 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
187 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
188 static tree fold_builtin_varargs (location_t, tree, tree, bool);
190 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
191 static tree fold_builtin_strstr (location_t, tree, tree, tree);
192 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
193 static tree fold_builtin_strncat (location_t, tree, tree, tree);
194 static tree fold_builtin_strspn (location_t, tree, tree);
195 static tree fold_builtin_strcspn (location_t, tree, tree);
197 static rtx expand_builtin_object_size (tree);
198 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
199 enum built_in_function);
200 static void maybe_emit_chk_warning (tree, enum built_in_function);
201 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
202 static void maybe_emit_free_warning (tree);
203 static tree fold_builtin_object_size (tree, tree);
204 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
205 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
206 enum built_in_function);
208 static unsigned HOST_WIDE_INT target_newline;
209 unsigned HOST_WIDE_INT target_percent;
210 static unsigned HOST_WIDE_INT target_c;
211 static unsigned HOST_WIDE_INT target_s;
212 static char target_percent_c[3];
213 char target_percent_s[3];
214 static char target_percent_s_newline[4];
215 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
216 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
217 static tree do_mpfr_arg2 (tree, tree, tree,
218 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
219 static tree do_mpfr_arg3 (tree, tree, tree, tree,
220 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
221 static tree do_mpfr_sincos (tree, tree, tree);
222 static tree do_mpfr_bessel_n (tree, tree, tree,
223 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
224 const REAL_VALUE_TYPE *, bool);
225 static tree do_mpfr_remquo (tree, tree, tree);
226 static tree do_mpfr_lgamma_r (tree, tree, tree);
227 static void expand_builtin_sync_synchronize (void);
229 /* Return true if NAME starts with __builtin_ or __sync_. */
231 static bool
232 is_builtin_name (const char *name)
234 if (strncmp (name, "__builtin_", 10) == 0)
235 return true;
236 if (strncmp (name, "__sync_", 7) == 0)
237 return true;
238 if (strncmp (name, "__atomic_", 9) == 0)
239 return true;
240 if (flag_cilkplus
241 && (!strcmp (name, "__cilkrts_detach")
242 || !strcmp (name, "__cilkrts_pop_frame")))
243 return true;
244 return false;
248 /* Return true if DECL is a function symbol representing a built-in. */
250 bool
251 is_builtin_fn (tree decl)
253 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
256 /* Return true if NODE should be considered for inline expansion regardless
257 of the optimization level. This means whenever a function is invoked with
258 its "internal" name, which normally contains the prefix "__builtin". */
260 static bool
261 called_as_built_in (tree node)
263 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
264 we want the name used to call the function, not the name it
265 will have. */
266 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
267 return is_builtin_name (name);
270 /* Compute values M and N such that M divides (address of EXP - N) and such
271 that N < M. If these numbers can be determined, store M in alignp and N in
272 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
273 *alignp and any bit-offset to *bitposp.
275 Note that the address (and thus the alignment) computed here is based
276 on the address to which a symbol resolves, whereas DECL_ALIGN is based
277 on the address at which an object is actually located. These two
278 addresses are not always the same. For example, on ARM targets,
279 the address &foo of a Thumb function foo() has the lowest bit set,
280 whereas foo() itself starts on an even address.
282 If ADDR_P is true we are taking the address of the memory reference EXP
283 and thus cannot rely on the access taking place. */
285 static bool
286 get_object_alignment_2 (tree exp, unsigned int *alignp,
287 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
289 HOST_WIDE_INT bitsize, bitpos;
290 tree offset;
291 enum machine_mode mode;
292 int unsignedp, volatilep;
293 unsigned int align = BITS_PER_UNIT;
294 bool known_alignment = false;
296 /* Get the innermost object and the constant (bitpos) and possibly
297 variable (offset) offset of the access. */
298 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
299 &mode, &unsignedp, &volatilep, true);
301 /* Extract alignment information from the innermost object and
302 possibly adjust bitpos and offset. */
303 if (TREE_CODE (exp) == FUNCTION_DECL)
305 /* Function addresses can encode extra information besides their
306 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
307 allows the low bit to be used as a virtual bit, we know
308 that the address itself must be at least 2-byte aligned. */
309 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
310 align = 2 * BITS_PER_UNIT;
312 else if (TREE_CODE (exp) == LABEL_DECL)
314 else if (TREE_CODE (exp) == CONST_DECL)
316 /* The alignment of a CONST_DECL is determined by its initializer. */
317 exp = DECL_INITIAL (exp);
318 align = TYPE_ALIGN (TREE_TYPE (exp));
319 #ifdef CONSTANT_ALIGNMENT
320 if (CONSTANT_CLASS_P (exp))
321 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
322 #endif
323 known_alignment = true;
325 else if (DECL_P (exp))
327 align = DECL_ALIGN (exp);
328 known_alignment = true;
330 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
332 align = TYPE_ALIGN (TREE_TYPE (exp));
334 else if (TREE_CODE (exp) == INDIRECT_REF
335 || TREE_CODE (exp) == MEM_REF
336 || TREE_CODE (exp) == TARGET_MEM_REF)
338 tree addr = TREE_OPERAND (exp, 0);
339 unsigned ptr_align;
340 unsigned HOST_WIDE_INT ptr_bitpos;
342 if (TREE_CODE (addr) == BIT_AND_EXPR
343 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
345 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
346 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
347 align *= BITS_PER_UNIT;
348 addr = TREE_OPERAND (addr, 0);
351 known_alignment
352 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
353 align = MAX (ptr_align, align);
355 /* The alignment of the pointer operand in a TARGET_MEM_REF
356 has to take the variable offset parts into account. */
357 if (TREE_CODE (exp) == TARGET_MEM_REF)
359 if (TMR_INDEX (exp))
361 unsigned HOST_WIDE_INT step = 1;
362 if (TMR_STEP (exp))
363 step = TREE_INT_CST_LOW (TMR_STEP (exp));
364 align = MIN (align, (step & -step) * BITS_PER_UNIT);
366 if (TMR_INDEX2 (exp))
367 align = BITS_PER_UNIT;
368 known_alignment = false;
371 /* When EXP is an actual memory reference then we can use
372 TYPE_ALIGN of a pointer indirection to derive alignment.
373 Do so only if get_pointer_alignment_1 did not reveal absolute
374 alignment knowledge and if using that alignment would
375 improve the situation. */
376 if (!addr_p && !known_alignment
377 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
378 align = TYPE_ALIGN (TREE_TYPE (exp));
379 else
381 /* Else adjust bitpos accordingly. */
382 bitpos += ptr_bitpos;
383 if (TREE_CODE (exp) == MEM_REF
384 || TREE_CODE (exp) == TARGET_MEM_REF)
385 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
388 else if (TREE_CODE (exp) == STRING_CST)
390 /* STRING_CST are the only constant objects we allow to be not
391 wrapped inside a CONST_DECL. */
392 align = TYPE_ALIGN (TREE_TYPE (exp));
393 #ifdef CONSTANT_ALIGNMENT
394 if (CONSTANT_CLASS_P (exp))
395 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
396 #endif
397 known_alignment = true;
400 /* If there is a non-constant offset part extract the maximum
401 alignment that can prevail. */
402 if (offset)
404 unsigned int trailing_zeros = tree_ctz (offset);
405 if (trailing_zeros < HOST_BITS_PER_INT)
407 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
408 if (inner)
409 align = MIN (align, inner);
413 *alignp = align;
414 *bitposp = bitpos & (*alignp - 1);
415 return known_alignment;
418 /* For a memory reference expression EXP compute values M and N such that M
419 divides (&EXP - N) and such that N < M. If these numbers can be determined,
420 store M in alignp and N in *BITPOSP and return true. Otherwise return false
421 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
423 bool
424 get_object_alignment_1 (tree exp, unsigned int *alignp,
425 unsigned HOST_WIDE_INT *bitposp)
427 return get_object_alignment_2 (exp, alignp, bitposp, false);
430 /* Return the alignment in bits of EXP, an object. */
432 unsigned int
433 get_object_alignment (tree exp)
435 unsigned HOST_WIDE_INT bitpos = 0;
436 unsigned int align;
438 get_object_alignment_1 (exp, &align, &bitpos);
440 /* align and bitpos now specify known low bits of the pointer.
441 ptr & (align - 1) == bitpos. */
443 if (bitpos != 0)
444 align = (bitpos & -bitpos);
445 return align;
448 /* For a pointer valued expression EXP compute values M and N such that M
449 divides (EXP - N) and such that N < M. If these numbers can be determined,
450 store M in alignp and N in *BITPOSP and return true. Return false if
451 the results are just a conservative approximation.
453 If EXP is not a pointer, false is returned too. */
455 bool
456 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
457 unsigned HOST_WIDE_INT *bitposp)
459 STRIP_NOPS (exp);
461 if (TREE_CODE (exp) == ADDR_EXPR)
462 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
463 alignp, bitposp, true);
464 else if (TREE_CODE (exp) == SSA_NAME
465 && POINTER_TYPE_P (TREE_TYPE (exp)))
467 unsigned int ptr_align, ptr_misalign;
468 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
470 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
472 *bitposp = ptr_misalign * BITS_PER_UNIT;
473 *alignp = ptr_align * BITS_PER_UNIT;
474 /* We cannot really tell whether this result is an approximation. */
475 return true;
477 else
479 *bitposp = 0;
480 *alignp = BITS_PER_UNIT;
481 return false;
484 else if (TREE_CODE (exp) == INTEGER_CST)
486 *alignp = BIGGEST_ALIGNMENT;
487 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
488 & (BIGGEST_ALIGNMENT - 1));
489 return true;
492 *bitposp = 0;
493 *alignp = BITS_PER_UNIT;
494 return false;
497 /* Return the alignment in bits of EXP, a pointer valued expression.
498 The alignment returned is, by default, the alignment of the thing that
499 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
501 Otherwise, look at the expression to see if we can do better, i.e., if the
502 expression is actually pointing at an object whose alignment is tighter. */
504 unsigned int
505 get_pointer_alignment (tree exp)
507 unsigned HOST_WIDE_INT bitpos = 0;
508 unsigned int align;
510 get_pointer_alignment_1 (exp, &align, &bitpos);
512 /* align and bitpos now specify known low bits of the pointer.
513 ptr & (align - 1) == bitpos. */
515 if (bitpos != 0)
516 align = (bitpos & -bitpos);
518 return align;
521 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
522 way, because it could contain a zero byte in the middle.
523 TREE_STRING_LENGTH is the size of the character array, not the string.
525 ONLY_VALUE should be nonzero if the result is not going to be emitted
526 into the instruction stream and zero if it is going to be expanded.
527 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
528 is returned, otherwise NULL, since
529 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
530 evaluate the side-effects.
532 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
533 accesses. Note that this implies the result is not going to be emitted
534 into the instruction stream.
536 The value returned is of type `ssizetype'.
538 Unfortunately, string_constant can't access the values of const char
539 arrays with initializers, so neither can we do so here. */
541 tree
542 c_strlen (tree src, int only_value)
544 tree offset_node;
545 HOST_WIDE_INT offset;
546 int max;
547 const char *ptr;
548 location_t loc;
550 STRIP_NOPS (src);
551 if (TREE_CODE (src) == COND_EXPR
552 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
554 tree len1, len2;
556 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
557 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
558 if (tree_int_cst_equal (len1, len2))
559 return len1;
562 if (TREE_CODE (src) == COMPOUND_EXPR
563 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
564 return c_strlen (TREE_OPERAND (src, 1), only_value);
566 loc = EXPR_LOC_OR_LOC (src, input_location);
568 src = string_constant (src, &offset_node);
569 if (src == 0)
570 return NULL_TREE;
572 max = TREE_STRING_LENGTH (src) - 1;
573 ptr = TREE_STRING_POINTER (src);
575 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
577 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
578 compute the offset to the following null if we don't know where to
579 start searching for it. */
580 int i;
582 for (i = 0; i < max; i++)
583 if (ptr[i] == 0)
584 return NULL_TREE;
586 /* We don't know the starting offset, but we do know that the string
587 has no internal zero bytes. We can assume that the offset falls
588 within the bounds of the string; otherwise, the programmer deserves
589 what he gets. Subtract the offset from the length of the string,
590 and return that. This would perhaps not be valid if we were dealing
591 with named arrays in addition to literal string constants. */
593 return size_diffop_loc (loc, size_int (max), offset_node);
596 /* We have a known offset into the string. Start searching there for
597 a null character if we can represent it as a single HOST_WIDE_INT. */
598 if (offset_node == 0)
599 offset = 0;
600 else if (! tree_fits_shwi_p (offset_node))
601 offset = -1;
602 else
603 offset = tree_to_shwi (offset_node);
605 /* If the offset is known to be out of bounds, warn, and call strlen at
606 runtime. */
607 if (offset < 0 || offset > max)
609 /* Suppress multiple warnings for propagated constant strings. */
610 if (only_value != 2
611 && !TREE_NO_WARNING (src))
613 warning_at (loc, 0, "offset outside bounds of constant string");
614 TREE_NO_WARNING (src) = 1;
616 return NULL_TREE;
619 /* Use strlen to search for the first zero byte. Since any strings
620 constructed with build_string will have nulls appended, we win even
621 if we get handed something like (char[4])"abcd".
623 Since OFFSET is our starting index into the string, no further
624 calculation is needed. */
625 return ssize_int (strlen (ptr + offset));
628 /* Return a char pointer for a C string if it is a string constant
629 or sum of string constant and integer constant. */
631 const char *
632 c_getstr (tree src)
634 tree offset_node;
636 src = string_constant (src, &offset_node);
637 if (src == 0)
638 return 0;
640 if (offset_node == 0)
641 return TREE_STRING_POINTER (src);
642 else if (!tree_fits_uhwi_p (offset_node)
643 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
644 return 0;
646 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
649 /* Return a constant integer corresponding to target reading
650 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
652 static rtx
653 c_readstr (const char *str, enum machine_mode mode)
655 HOST_WIDE_INT ch;
656 unsigned int i, j;
657 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
659 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
660 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
661 / HOST_BITS_PER_WIDE_INT;
663 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
664 for (i = 0; i < len; i++)
665 tmp[i] = 0;
667 ch = 1;
668 for (i = 0; i < GET_MODE_SIZE (mode); i++)
670 j = i;
671 if (WORDS_BIG_ENDIAN)
672 j = GET_MODE_SIZE (mode) - i - 1;
673 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
674 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
675 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
676 j *= BITS_PER_UNIT;
678 if (ch)
679 ch = (unsigned char) str[i];
680 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
683 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
684 return immed_wide_int_const (c, mode);
687 /* Cast a target constant CST to target CHAR and if that value fits into
688 host char type, return zero and put that value into variable pointed to by
689 P. */
691 static int
692 target_char_cast (tree cst, char *p)
694 unsigned HOST_WIDE_INT val, hostval;
696 if (TREE_CODE (cst) != INTEGER_CST
697 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
698 return 1;
700 /* Do not care if it fits or not right here. */
701 val = TREE_INT_CST_LOW (cst);
703 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
704 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
706 hostval = val;
707 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
708 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
710 if (val != hostval)
711 return 1;
713 *p = hostval;
714 return 0;
717 /* Similar to save_expr, but assumes that arbitrary code is not executed
718 in between the multiple evaluations. In particular, we assume that a
719 non-addressable local variable will not be modified. */
721 static tree
722 builtin_save_expr (tree exp)
724 if (TREE_CODE (exp) == SSA_NAME
725 || (TREE_ADDRESSABLE (exp) == 0
726 && (TREE_CODE (exp) == PARM_DECL
727 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
728 return exp;
730 return save_expr (exp);
733 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
734 times to get the address of either a higher stack frame, or a return
735 address located within it (depending on FNDECL_CODE). */
737 static rtx
738 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
740 int i;
742 #ifdef INITIAL_FRAME_ADDRESS_RTX
743 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
744 #else
745 rtx tem;
747 /* For a zero count with __builtin_return_address, we don't care what
748 frame address we return, because target-specific definitions will
749 override us. Therefore frame pointer elimination is OK, and using
750 the soft frame pointer is OK.
752 For a nonzero count, or a zero count with __builtin_frame_address,
753 we require a stable offset from the current frame pointer to the
754 previous one, so we must use the hard frame pointer, and
755 we must disable frame pointer elimination. */
756 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
757 tem = frame_pointer_rtx;
758 else
760 tem = hard_frame_pointer_rtx;
762 /* Tell reload not to eliminate the frame pointer. */
763 crtl->accesses_prior_frames = 1;
765 #endif
767 /* Some machines need special handling before we can access
768 arbitrary frames. For example, on the SPARC, we must first flush
769 all register windows to the stack. */
770 #ifdef SETUP_FRAME_ADDRESSES
771 if (count > 0)
772 SETUP_FRAME_ADDRESSES ();
773 #endif
775 /* On the SPARC, the return address is not in the frame, it is in a
776 register. There is no way to access it off of the current frame
777 pointer, but it can be accessed off the previous frame pointer by
778 reading the value from the register window save area. */
779 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
780 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
781 count--;
782 #endif
784 /* Scan back COUNT frames to the specified frame. */
785 for (i = 0; i < count; i++)
787 /* Assume the dynamic chain pointer is in the word that the
788 frame address points to, unless otherwise specified. */
789 #ifdef DYNAMIC_CHAIN_ADDRESS
790 tem = DYNAMIC_CHAIN_ADDRESS (tem);
791 #endif
792 tem = memory_address (Pmode, tem);
793 tem = gen_frame_mem (Pmode, tem);
794 tem = copy_to_reg (tem);
797 /* For __builtin_frame_address, return what we've got. But, on
798 the SPARC for example, we may have to add a bias. */
799 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
800 #ifdef FRAME_ADDR_RTX
801 return FRAME_ADDR_RTX (tem);
802 #else
803 return tem;
804 #endif
806 /* For __builtin_return_address, get the return address from that frame. */
807 #ifdef RETURN_ADDR_RTX
808 tem = RETURN_ADDR_RTX (count, tem);
809 #else
810 tem = memory_address (Pmode,
811 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
812 tem = gen_frame_mem (Pmode, tem);
813 #endif
814 return tem;
817 /* Alias set used for setjmp buffer. */
818 static alias_set_type setjmp_alias_set = -1;
820 /* Construct the leading half of a __builtin_setjmp call. Control will
821 return to RECEIVER_LABEL. This is also called directly by the SJLJ
822 exception handling code. */
824 void
825 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
827 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
828 rtx stack_save;
829 rtx mem;
831 if (setjmp_alias_set == -1)
832 setjmp_alias_set = new_alias_set ();
834 buf_addr = convert_memory_address (Pmode, buf_addr);
836 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
838 /* We store the frame pointer and the address of receiver_label in
839 the buffer and use the rest of it for the stack save area, which
840 is machine-dependent. */
842 mem = gen_rtx_MEM (Pmode, buf_addr);
843 set_mem_alias_set (mem, setjmp_alias_set);
844 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
846 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
847 GET_MODE_SIZE (Pmode))),
848 set_mem_alias_set (mem, setjmp_alias_set);
850 emit_move_insn (validize_mem (mem),
851 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
853 stack_save = gen_rtx_MEM (sa_mode,
854 plus_constant (Pmode, buf_addr,
855 2 * GET_MODE_SIZE (Pmode)));
856 set_mem_alias_set (stack_save, setjmp_alias_set);
857 emit_stack_save (SAVE_NONLOCAL, &stack_save);
859 /* If there is further processing to do, do it. */
860 #ifdef HAVE_builtin_setjmp_setup
861 if (HAVE_builtin_setjmp_setup)
862 emit_insn (gen_builtin_setjmp_setup (buf_addr));
863 #endif
865 /* We have a nonlocal label. */
866 cfun->has_nonlocal_label = 1;
869 /* Construct the trailing part of a __builtin_setjmp call. This is
870 also called directly by the SJLJ exception handling code.
871 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
873 void
874 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
876 rtx chain;
878 /* Mark the FP as used when we get here, so we have to make sure it's
879 marked as used by this function. */
880 emit_use (hard_frame_pointer_rtx);
882 /* Mark the static chain as clobbered here so life information
883 doesn't get messed up for it. */
884 chain = targetm.calls.static_chain (current_function_decl, true);
885 if (chain && REG_P (chain))
886 emit_clobber (chain);
888 /* Now put in the code to restore the frame pointer, and argument
889 pointer, if needed. */
890 #ifdef HAVE_nonlocal_goto
891 if (! HAVE_nonlocal_goto)
892 #endif
894 /* First adjust our frame pointer to its actual value. It was
895 previously set to the start of the virtual area corresponding to
896 the stacked variables when we branched here and now needs to be
897 adjusted to the actual hardware fp value.
899 Assignments to virtual registers are converted by
900 instantiate_virtual_regs into the corresponding assignment
901 to the underlying register (fp in this case) that makes
902 the original assignment true.
903 So the following insn will actually be decrementing fp by
904 STARTING_FRAME_OFFSET. */
905 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
907 /* Restoring the frame pointer also modifies the hard frame pointer.
908 Mark it used (so that the previous assignment remains live once
909 the frame pointer is eliminated) and clobbered (to represent the
910 implicit update from the assignment). */
911 emit_use (hard_frame_pointer_rtx);
912 emit_clobber (hard_frame_pointer_rtx);
915 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
916 if (fixed_regs[ARG_POINTER_REGNUM])
918 #ifdef ELIMINABLE_REGS
919 /* If the argument pointer can be eliminated in favor of the
920 frame pointer, we don't need to restore it. We assume here
921 that if such an elimination is present, it can always be used.
922 This is the case on all known machines; if we don't make this
923 assumption, we do unnecessary saving on many machines. */
924 size_t i;
925 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
927 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
928 if (elim_regs[i].from == ARG_POINTER_REGNUM
929 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
930 break;
932 if (i == ARRAY_SIZE (elim_regs))
933 #endif
935 /* Now restore our arg pointer from the address at which it
936 was saved in our stack frame. */
937 emit_move_insn (crtl->args.internal_arg_pointer,
938 copy_to_reg (get_arg_pointer_save_area ()));
941 #endif
943 #ifdef HAVE_builtin_setjmp_receiver
944 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
945 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
946 else
947 #endif
948 #ifdef HAVE_nonlocal_goto_receiver
949 if (HAVE_nonlocal_goto_receiver)
950 emit_insn (gen_nonlocal_goto_receiver ());
951 else
952 #endif
953 { /* Nothing */ }
955 /* We must not allow the code we just generated to be reordered by
956 scheduling. Specifically, the update of the frame pointer must
957 happen immediately, not later. */
958 emit_insn (gen_blockage ());
961 /* __builtin_longjmp is passed a pointer to an array of five words (not
962 all will be used on all machines). It operates similarly to the C
963 library function of the same name, but is more efficient. Much of
964 the code below is copied from the handling of non-local gotos. */
966 static void
967 expand_builtin_longjmp (rtx buf_addr, rtx value)
969 rtx fp, lab, stack;
970 rtx_insn *insn, *last;
971 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
973 /* DRAP is needed for stack realign if longjmp is expanded to current
974 function */
975 if (SUPPORTS_STACK_ALIGNMENT)
976 crtl->need_drap = true;
978 if (setjmp_alias_set == -1)
979 setjmp_alias_set = new_alias_set ();
981 buf_addr = convert_memory_address (Pmode, buf_addr);
983 buf_addr = force_reg (Pmode, buf_addr);
985 /* We require that the user must pass a second argument of 1, because
986 that is what builtin_setjmp will return. */
987 gcc_assert (value == const1_rtx);
989 last = get_last_insn ();
990 #ifdef HAVE_builtin_longjmp
991 if (HAVE_builtin_longjmp)
992 emit_insn (gen_builtin_longjmp (buf_addr));
993 else
994 #endif
996 fp = gen_rtx_MEM (Pmode, buf_addr);
997 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
998 GET_MODE_SIZE (Pmode)));
1000 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1001 2 * GET_MODE_SIZE (Pmode)));
1002 set_mem_alias_set (fp, setjmp_alias_set);
1003 set_mem_alias_set (lab, setjmp_alias_set);
1004 set_mem_alias_set (stack, setjmp_alias_set);
1006 /* Pick up FP, label, and SP from the block and jump. This code is
1007 from expand_goto in stmt.c; see there for detailed comments. */
1008 #ifdef HAVE_nonlocal_goto
1009 if (HAVE_nonlocal_goto)
1010 /* We have to pass a value to the nonlocal_goto pattern that will
1011 get copied into the static_chain pointer, but it does not matter
1012 what that value is, because builtin_setjmp does not use it. */
1013 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1014 else
1015 #endif
1017 lab = copy_to_reg (lab);
1019 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1020 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1022 emit_move_insn (hard_frame_pointer_rtx, fp);
1023 emit_stack_restore (SAVE_NONLOCAL, stack);
1025 emit_use (hard_frame_pointer_rtx);
1026 emit_use (stack_pointer_rtx);
1027 emit_indirect_jump (lab);
1031 /* Search backwards and mark the jump insn as a non-local goto.
1032 Note that this precludes the use of __builtin_longjmp to a
1033 __builtin_setjmp target in the same function. However, we've
1034 already cautioned the user that these functions are for
1035 internal exception handling use only. */
1036 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1038 gcc_assert (insn != last);
1040 if (JUMP_P (insn))
1042 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1043 break;
1045 else if (CALL_P (insn))
1046 break;
1050 static inline bool
1051 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1053 return (iter->i < iter->n);
1056 /* This function validates the types of a function call argument list
1057 against a specified list of tree_codes. If the last specifier is a 0,
1058 that represents an ellipses, otherwise the last specifier must be a
1059 VOID_TYPE. */
1061 static bool
1062 validate_arglist (const_tree callexpr, ...)
1064 enum tree_code code;
1065 bool res = 0;
1066 va_list ap;
1067 const_call_expr_arg_iterator iter;
1068 const_tree arg;
1070 va_start (ap, callexpr);
1071 init_const_call_expr_arg_iterator (callexpr, &iter);
1075 code = (enum tree_code) va_arg (ap, int);
1076 switch (code)
1078 case 0:
1079 /* This signifies an ellipses, any further arguments are all ok. */
1080 res = true;
1081 goto end;
1082 case VOID_TYPE:
1083 /* This signifies an endlink, if no arguments remain, return
1084 true, otherwise return false. */
1085 res = !more_const_call_expr_args_p (&iter);
1086 goto end;
1087 default:
1088 /* If no parameters remain or the parameter's code does not
1089 match the specified code, return false. Otherwise continue
1090 checking any remaining arguments. */
1091 arg = next_const_call_expr_arg (&iter);
1092 if (!validate_arg (arg, code))
1093 goto end;
1094 break;
1097 while (1);
1099 /* We need gotos here since we can only have one VA_CLOSE in a
1100 function. */
1101 end: ;
1102 va_end (ap);
1104 return res;
1107 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1108 and the address of the save area. */
1110 static rtx
1111 expand_builtin_nonlocal_goto (tree exp)
1113 tree t_label, t_save_area;
1114 rtx r_label, r_save_area, r_fp, r_sp;
1115 rtx_insn *insn;
1117 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1118 return NULL_RTX;
1120 t_label = CALL_EXPR_ARG (exp, 0);
1121 t_save_area = CALL_EXPR_ARG (exp, 1);
1123 r_label = expand_normal (t_label);
1124 r_label = convert_memory_address (Pmode, r_label);
1125 r_save_area = expand_normal (t_save_area);
1126 r_save_area = convert_memory_address (Pmode, r_save_area);
1127 /* Copy the address of the save location to a register just in case it was
1128 based on the frame pointer. */
1129 r_save_area = copy_to_reg (r_save_area);
1130 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1131 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1132 plus_constant (Pmode, r_save_area,
1133 GET_MODE_SIZE (Pmode)));
1135 crtl->has_nonlocal_goto = 1;
1137 #ifdef HAVE_nonlocal_goto
1138 /* ??? We no longer need to pass the static chain value, afaik. */
1139 if (HAVE_nonlocal_goto)
1140 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1141 else
1142 #endif
1144 r_label = copy_to_reg (r_label);
1146 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1147 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1149 /* Restore frame pointer for containing function. */
1150 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1151 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1153 /* USE of hard_frame_pointer_rtx added for consistency;
1154 not clear if really needed. */
1155 emit_use (hard_frame_pointer_rtx);
1156 emit_use (stack_pointer_rtx);
1158 /* If the architecture is using a GP register, we must
1159 conservatively assume that the target function makes use of it.
1160 The prologue of functions with nonlocal gotos must therefore
1161 initialize the GP register to the appropriate value, and we
1162 must then make sure that this value is live at the point
1163 of the jump. (Note that this doesn't necessarily apply
1164 to targets with a nonlocal_goto pattern; they are free
1165 to implement it in their own way. Note also that this is
1166 a no-op if the GP register is a global invariant.) */
1167 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1168 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1169 emit_use (pic_offset_table_rtx);
1171 emit_indirect_jump (r_label);
1174 /* Search backwards to the jump insn and mark it as a
1175 non-local goto. */
1176 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1178 if (JUMP_P (insn))
1180 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1181 break;
1183 else if (CALL_P (insn))
1184 break;
1187 return const0_rtx;
1190 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1191 (not all will be used on all machines) that was passed to __builtin_setjmp.
1192 It updates the stack pointer in that block to correspond to the current
1193 stack pointer. */
1195 static void
1196 expand_builtin_update_setjmp_buf (rtx buf_addr)
1198 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1199 rtx stack_save
1200 = gen_rtx_MEM (sa_mode,
1201 memory_address
1202 (sa_mode,
1203 plus_constant (Pmode, buf_addr,
1204 2 * GET_MODE_SIZE (Pmode))));
1206 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1209 /* Expand a call to __builtin_prefetch. For a target that does not support
1210 data prefetch, evaluate the memory address argument in case it has side
1211 effects. */
1213 static void
1214 expand_builtin_prefetch (tree exp)
1216 tree arg0, arg1, arg2;
1217 int nargs;
1218 rtx op0, op1, op2;
1220 if (!validate_arglist (exp, POINTER_TYPE, 0))
1221 return;
1223 arg0 = CALL_EXPR_ARG (exp, 0);
1225 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1226 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1227 locality). */
1228 nargs = call_expr_nargs (exp);
1229 if (nargs > 1)
1230 arg1 = CALL_EXPR_ARG (exp, 1);
1231 else
1232 arg1 = integer_zero_node;
1233 if (nargs > 2)
1234 arg2 = CALL_EXPR_ARG (exp, 2);
1235 else
1236 arg2 = integer_three_node;
1238 /* Argument 0 is an address. */
1239 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1241 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1242 if (TREE_CODE (arg1) != INTEGER_CST)
1244 error ("second argument to %<__builtin_prefetch%> must be a constant");
1245 arg1 = integer_zero_node;
1247 op1 = expand_normal (arg1);
1248 /* Argument 1 must be either zero or one. */
1249 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1251 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1252 " using zero");
1253 op1 = const0_rtx;
1256 /* Argument 2 (locality) must be a compile-time constant int. */
1257 if (TREE_CODE (arg2) != INTEGER_CST)
1259 error ("third argument to %<__builtin_prefetch%> must be a constant");
1260 arg2 = integer_zero_node;
1262 op2 = expand_normal (arg2);
1263 /* Argument 2 must be 0, 1, 2, or 3. */
1264 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1266 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1267 op2 = const0_rtx;
1270 #ifdef HAVE_prefetch
1271 if (HAVE_prefetch)
1273 struct expand_operand ops[3];
1275 create_address_operand (&ops[0], op0);
1276 create_integer_operand (&ops[1], INTVAL (op1));
1277 create_integer_operand (&ops[2], INTVAL (op2));
1278 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1279 return;
1281 #endif
1283 /* Don't do anything with direct references to volatile memory, but
1284 generate code to handle other side effects. */
1285 if (!MEM_P (op0) && side_effects_p (op0))
1286 emit_insn (op0);
1289 /* Get a MEM rtx for expression EXP which is the address of an operand
1290 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1291 the maximum length of the block of memory that might be accessed or
1292 NULL if unknown. */
1294 static rtx
1295 get_memory_rtx (tree exp, tree len)
1297 tree orig_exp = exp;
1298 rtx addr, mem;
1300 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1301 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1302 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1303 exp = TREE_OPERAND (exp, 0);
1305 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1306 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1308 /* Get an expression we can use to find the attributes to assign to MEM.
1309 First remove any nops. */
1310 while (CONVERT_EXPR_P (exp)
1311 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1312 exp = TREE_OPERAND (exp, 0);
1314 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1315 (as builtin stringops may alias with anything). */
1316 exp = fold_build2 (MEM_REF,
1317 build_array_type (char_type_node,
1318 build_range_type (sizetype,
1319 size_one_node, len)),
1320 exp, build_int_cst (ptr_type_node, 0));
1322 /* If the MEM_REF has no acceptable address, try to get the base object
1323 from the original address we got, and build an all-aliasing
1324 unknown-sized access to that one. */
1325 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1326 set_mem_attributes (mem, exp, 0);
1327 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1328 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1329 0))))
1331 exp = build_fold_addr_expr (exp);
1332 exp = fold_build2 (MEM_REF,
1333 build_array_type (char_type_node,
1334 build_range_type (sizetype,
1335 size_zero_node,
1336 NULL)),
1337 exp, build_int_cst (ptr_type_node, 0));
1338 set_mem_attributes (mem, exp, 0);
1340 set_mem_alias_set (mem, 0);
1341 return mem;
1344 /* Built-in functions to perform an untyped call and return. */
1346 #define apply_args_mode \
1347 (this_target_builtins->x_apply_args_mode)
1348 #define apply_result_mode \
1349 (this_target_builtins->x_apply_result_mode)
1351 /* Return the size required for the block returned by __builtin_apply_args,
1352 and initialize apply_args_mode. */
1354 static int
1355 apply_args_size (void)
1357 static int size = -1;
1358 int align;
1359 unsigned int regno;
1360 enum machine_mode mode;
1362 /* The values computed by this function never change. */
1363 if (size < 0)
1365 /* The first value is the incoming arg-pointer. */
1366 size = GET_MODE_SIZE (Pmode);
1368 /* The second value is the structure value address unless this is
1369 passed as an "invisible" first argument. */
1370 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1371 size += GET_MODE_SIZE (Pmode);
1373 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1374 if (FUNCTION_ARG_REGNO_P (regno))
1376 mode = targetm.calls.get_raw_arg_mode (regno);
1378 gcc_assert (mode != VOIDmode);
1380 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1381 if (size % align != 0)
1382 size = CEIL (size, align) * align;
1383 size += GET_MODE_SIZE (mode);
1384 apply_args_mode[regno] = mode;
1386 else
1388 apply_args_mode[regno] = VOIDmode;
1391 return size;
1394 /* Return the size required for the block returned by __builtin_apply,
1395 and initialize apply_result_mode. */
1397 static int
1398 apply_result_size (void)
1400 static int size = -1;
1401 int align, regno;
1402 enum machine_mode mode;
1404 /* The values computed by this function never change. */
1405 if (size < 0)
1407 size = 0;
1409 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1410 if (targetm.calls.function_value_regno_p (regno))
1412 mode = targetm.calls.get_raw_result_mode (regno);
1414 gcc_assert (mode != VOIDmode);
1416 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1417 if (size % align != 0)
1418 size = CEIL (size, align) * align;
1419 size += GET_MODE_SIZE (mode);
1420 apply_result_mode[regno] = mode;
1422 else
1423 apply_result_mode[regno] = VOIDmode;
1425 /* Allow targets that use untyped_call and untyped_return to override
1426 the size so that machine-specific information can be stored here. */
1427 #ifdef APPLY_RESULT_SIZE
1428 size = APPLY_RESULT_SIZE;
1429 #endif
1431 return size;
1434 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1435 /* Create a vector describing the result block RESULT. If SAVEP is true,
1436 the result block is used to save the values; otherwise it is used to
1437 restore the values. */
1439 static rtx
1440 result_vector (int savep, rtx result)
1442 int regno, size, align, nelts;
1443 enum machine_mode mode;
1444 rtx reg, mem;
1445 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1447 size = nelts = 0;
1448 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1449 if ((mode = apply_result_mode[regno]) != VOIDmode)
1451 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1452 if (size % align != 0)
1453 size = CEIL (size, align) * align;
1454 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1455 mem = adjust_address (result, mode, size);
1456 savevec[nelts++] = (savep
1457 ? gen_rtx_SET (VOIDmode, mem, reg)
1458 : gen_rtx_SET (VOIDmode, reg, mem));
1459 size += GET_MODE_SIZE (mode);
1461 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1463 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1465 /* Save the state required to perform an untyped call with the same
1466 arguments as were passed to the current function. */
1468 static rtx
1469 expand_builtin_apply_args_1 (void)
1471 rtx registers, tem;
1472 int size, align, regno;
1473 enum machine_mode mode;
1474 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1476 /* Create a block where the arg-pointer, structure value address,
1477 and argument registers can be saved. */
1478 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1480 /* Walk past the arg-pointer and structure value address. */
1481 size = GET_MODE_SIZE (Pmode);
1482 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1483 size += GET_MODE_SIZE (Pmode);
1485 /* Save each register used in calling a function to the block. */
1486 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1487 if ((mode = apply_args_mode[regno]) != VOIDmode)
1489 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1490 if (size % align != 0)
1491 size = CEIL (size, align) * align;
1493 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1495 emit_move_insn (adjust_address (registers, mode, size), tem);
1496 size += GET_MODE_SIZE (mode);
1499 /* Save the arg pointer to the block. */
1500 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1501 #ifdef STACK_GROWS_DOWNWARD
1502 /* We need the pointer as the caller actually passed them to us, not
1503 as we might have pretended they were passed. Make sure it's a valid
1504 operand, as emit_move_insn isn't expected to handle a PLUS. */
1506 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1507 NULL_RTX);
1508 #endif
1509 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1511 size = GET_MODE_SIZE (Pmode);
1513 /* Save the structure value address unless this is passed as an
1514 "invisible" first argument. */
1515 if (struct_incoming_value)
1517 emit_move_insn (adjust_address (registers, Pmode, size),
1518 copy_to_reg (struct_incoming_value));
1519 size += GET_MODE_SIZE (Pmode);
1522 /* Return the address of the block. */
1523 return copy_addr_to_reg (XEXP (registers, 0));
1526 /* __builtin_apply_args returns block of memory allocated on
1527 the stack into which is stored the arg pointer, structure
1528 value address, static chain, and all the registers that might
1529 possibly be used in performing a function call. The code is
1530 moved to the start of the function so the incoming values are
1531 saved. */
1533 static rtx
1534 expand_builtin_apply_args (void)
1536 /* Don't do __builtin_apply_args more than once in a function.
1537 Save the result of the first call and reuse it. */
1538 if (apply_args_value != 0)
1539 return apply_args_value;
1541 /* When this function is called, it means that registers must be
1542 saved on entry to this function. So we migrate the
1543 call to the first insn of this function. */
1544 rtx temp;
1545 rtx seq;
1547 start_sequence ();
1548 temp = expand_builtin_apply_args_1 ();
1549 seq = get_insns ();
1550 end_sequence ();
1552 apply_args_value = temp;
1554 /* Put the insns after the NOTE that starts the function.
1555 If this is inside a start_sequence, make the outer-level insn
1556 chain current, so the code is placed at the start of the
1557 function. If internal_arg_pointer is a non-virtual pseudo,
1558 it needs to be placed after the function that initializes
1559 that pseudo. */
1560 push_topmost_sequence ();
1561 if (REG_P (crtl->args.internal_arg_pointer)
1562 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1563 emit_insn_before (seq, parm_birth_insn);
1564 else
1565 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1566 pop_topmost_sequence ();
1567 return temp;
1571 /* Perform an untyped call and save the state required to perform an
1572 untyped return of whatever value was returned by the given function. */
1574 static rtx
1575 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1577 int size, align, regno;
1578 enum machine_mode mode;
1579 rtx incoming_args, result, reg, dest, src;
1580 rtx_call_insn *call_insn;
1581 rtx old_stack_level = 0;
1582 rtx call_fusage = 0;
1583 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1585 arguments = convert_memory_address (Pmode, arguments);
1587 /* Create a block where the return registers can be saved. */
1588 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1590 /* Fetch the arg pointer from the ARGUMENTS block. */
1591 incoming_args = gen_reg_rtx (Pmode);
1592 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1593 #ifndef STACK_GROWS_DOWNWARD
1594 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1595 incoming_args, 0, OPTAB_LIB_WIDEN);
1596 #endif
1598 /* Push a new argument block and copy the arguments. Do not allow
1599 the (potential) memcpy call below to interfere with our stack
1600 manipulations. */
1601 do_pending_stack_adjust ();
1602 NO_DEFER_POP;
1604 /* Save the stack with nonlocal if available. */
1605 #ifdef HAVE_save_stack_nonlocal
1606 if (HAVE_save_stack_nonlocal)
1607 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1608 else
1609 #endif
1610 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1612 /* Allocate a block of memory onto the stack and copy the memory
1613 arguments to the outgoing arguments address. We can pass TRUE
1614 as the 4th argument because we just saved the stack pointer
1615 and will restore it right after the call. */
1616 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1618 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1619 may have already set current_function_calls_alloca to true.
1620 current_function_calls_alloca won't be set if argsize is zero,
1621 so we have to guarantee need_drap is true here. */
1622 if (SUPPORTS_STACK_ALIGNMENT)
1623 crtl->need_drap = true;
1625 dest = virtual_outgoing_args_rtx;
1626 #ifndef STACK_GROWS_DOWNWARD
1627 if (CONST_INT_P (argsize))
1628 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1629 else
1630 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1631 #endif
1632 dest = gen_rtx_MEM (BLKmode, dest);
1633 set_mem_align (dest, PARM_BOUNDARY);
1634 src = gen_rtx_MEM (BLKmode, incoming_args);
1635 set_mem_align (src, PARM_BOUNDARY);
1636 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1638 /* Refer to the argument block. */
1639 apply_args_size ();
1640 arguments = gen_rtx_MEM (BLKmode, arguments);
1641 set_mem_align (arguments, PARM_BOUNDARY);
1643 /* Walk past the arg-pointer and structure value address. */
1644 size = GET_MODE_SIZE (Pmode);
1645 if (struct_value)
1646 size += GET_MODE_SIZE (Pmode);
1648 /* Restore each of the registers previously saved. Make USE insns
1649 for each of these registers for use in making the call. */
1650 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1651 if ((mode = apply_args_mode[regno]) != VOIDmode)
1653 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1654 if (size % align != 0)
1655 size = CEIL (size, align) * align;
1656 reg = gen_rtx_REG (mode, regno);
1657 emit_move_insn (reg, adjust_address (arguments, mode, size));
1658 use_reg (&call_fusage, reg);
1659 size += GET_MODE_SIZE (mode);
1662 /* Restore the structure value address unless this is passed as an
1663 "invisible" first argument. */
1664 size = GET_MODE_SIZE (Pmode);
1665 if (struct_value)
1667 rtx value = gen_reg_rtx (Pmode);
1668 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1669 emit_move_insn (struct_value, value);
1670 if (REG_P (struct_value))
1671 use_reg (&call_fusage, struct_value);
1672 size += GET_MODE_SIZE (Pmode);
1675 /* All arguments and registers used for the call are set up by now! */
1676 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1678 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1679 and we don't want to load it into a register as an optimization,
1680 because prepare_call_address already did it if it should be done. */
1681 if (GET_CODE (function) != SYMBOL_REF)
1682 function = memory_address (FUNCTION_MODE, function);
1684 /* Generate the actual call instruction and save the return value. */
1685 #ifdef HAVE_untyped_call
1686 if (HAVE_untyped_call)
1687 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1688 result, result_vector (1, result)));
1689 else
1690 #endif
1691 #ifdef HAVE_call_value
1692 if (HAVE_call_value)
1694 rtx valreg = 0;
1696 /* Locate the unique return register. It is not possible to
1697 express a call that sets more than one return register using
1698 call_value; use untyped_call for that. In fact, untyped_call
1699 only needs to save the return registers in the given block. */
1700 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1701 if ((mode = apply_result_mode[regno]) != VOIDmode)
1703 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1705 valreg = gen_rtx_REG (mode, regno);
1708 emit_call_insn (GEN_CALL_VALUE (valreg,
1709 gen_rtx_MEM (FUNCTION_MODE, function),
1710 const0_rtx, NULL_RTX, const0_rtx));
1712 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1714 else
1715 #endif
1716 gcc_unreachable ();
1718 /* Find the CALL insn we just emitted, and attach the register usage
1719 information. */
1720 call_insn = last_call_insn ();
1721 add_function_usage_to (call_insn, call_fusage);
1723 /* Restore the stack. */
1724 #ifdef HAVE_save_stack_nonlocal
1725 if (HAVE_save_stack_nonlocal)
1726 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1727 else
1728 #endif
1729 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1730 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1732 OK_DEFER_POP;
1734 /* Return the address of the result block. */
1735 result = copy_addr_to_reg (XEXP (result, 0));
1736 return convert_memory_address (ptr_mode, result);
1739 /* Perform an untyped return. */
1741 static void
1742 expand_builtin_return (rtx result)
1744 int size, align, regno;
1745 enum machine_mode mode;
1746 rtx reg;
1747 rtx_insn *call_fusage = 0;
1749 result = convert_memory_address (Pmode, result);
1751 apply_result_size ();
1752 result = gen_rtx_MEM (BLKmode, result);
1754 #ifdef HAVE_untyped_return
1755 if (HAVE_untyped_return)
1757 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1758 emit_barrier ();
1759 return;
1761 #endif
1763 /* Restore the return value and note that each value is used. */
1764 size = 0;
1765 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1766 if ((mode = apply_result_mode[regno]) != VOIDmode)
1768 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1769 if (size % align != 0)
1770 size = CEIL (size, align) * align;
1771 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1772 emit_move_insn (reg, adjust_address (result, mode, size));
1774 push_to_sequence (call_fusage);
1775 emit_use (reg);
1776 call_fusage = get_insns ();
1777 end_sequence ();
1778 size += GET_MODE_SIZE (mode);
1781 /* Put the USE insns before the return. */
1782 emit_insn (call_fusage);
1784 /* Return whatever values was restored by jumping directly to the end
1785 of the function. */
1786 expand_naked_return ();
1789 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1791 static enum type_class
1792 type_to_class (tree type)
1794 switch (TREE_CODE (type))
1796 case VOID_TYPE: return void_type_class;
1797 case INTEGER_TYPE: return integer_type_class;
1798 case ENUMERAL_TYPE: return enumeral_type_class;
1799 case BOOLEAN_TYPE: return boolean_type_class;
1800 case POINTER_TYPE: return pointer_type_class;
1801 case REFERENCE_TYPE: return reference_type_class;
1802 case OFFSET_TYPE: return offset_type_class;
1803 case REAL_TYPE: return real_type_class;
1804 case COMPLEX_TYPE: return complex_type_class;
1805 case FUNCTION_TYPE: return function_type_class;
1806 case METHOD_TYPE: return method_type_class;
1807 case RECORD_TYPE: return record_type_class;
1808 case UNION_TYPE:
1809 case QUAL_UNION_TYPE: return union_type_class;
1810 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1811 ? string_type_class : array_type_class);
1812 case LANG_TYPE: return lang_type_class;
1813 default: return no_type_class;
1817 /* Expand a call EXP to __builtin_classify_type. */
1819 static rtx
1820 expand_builtin_classify_type (tree exp)
1822 if (call_expr_nargs (exp))
1823 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1824 return GEN_INT (no_type_class);
1827 /* This helper macro, meant to be used in mathfn_built_in below,
1828 determines which among a set of three builtin math functions is
1829 appropriate for a given type mode. The `F' and `L' cases are
1830 automatically generated from the `double' case. */
1831 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1832 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1833 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1834 fcodel = BUILT_IN_MATHFN##L ; break;
1835 /* Similar to above, but appends _R after any F/L suffix. */
1836 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1837 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1838 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1839 fcodel = BUILT_IN_MATHFN##L_R ; break;
1841 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1842 if available. If IMPLICIT is true use the implicit builtin declaration,
1843 otherwise use the explicit declaration. If we can't do the conversion,
1844 return zero. */
1846 static tree
1847 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1849 enum built_in_function fcode, fcodef, fcodel, fcode2;
1851 switch (fn)
1853 CASE_MATHFN (BUILT_IN_ACOS)
1854 CASE_MATHFN (BUILT_IN_ACOSH)
1855 CASE_MATHFN (BUILT_IN_ASIN)
1856 CASE_MATHFN (BUILT_IN_ASINH)
1857 CASE_MATHFN (BUILT_IN_ATAN)
1858 CASE_MATHFN (BUILT_IN_ATAN2)
1859 CASE_MATHFN (BUILT_IN_ATANH)
1860 CASE_MATHFN (BUILT_IN_CBRT)
1861 CASE_MATHFN (BUILT_IN_CEIL)
1862 CASE_MATHFN (BUILT_IN_CEXPI)
1863 CASE_MATHFN (BUILT_IN_COPYSIGN)
1864 CASE_MATHFN (BUILT_IN_COS)
1865 CASE_MATHFN (BUILT_IN_COSH)
1866 CASE_MATHFN (BUILT_IN_DREM)
1867 CASE_MATHFN (BUILT_IN_ERF)
1868 CASE_MATHFN (BUILT_IN_ERFC)
1869 CASE_MATHFN (BUILT_IN_EXP)
1870 CASE_MATHFN (BUILT_IN_EXP10)
1871 CASE_MATHFN (BUILT_IN_EXP2)
1872 CASE_MATHFN (BUILT_IN_EXPM1)
1873 CASE_MATHFN (BUILT_IN_FABS)
1874 CASE_MATHFN (BUILT_IN_FDIM)
1875 CASE_MATHFN (BUILT_IN_FLOOR)
1876 CASE_MATHFN (BUILT_IN_FMA)
1877 CASE_MATHFN (BUILT_IN_FMAX)
1878 CASE_MATHFN (BUILT_IN_FMIN)
1879 CASE_MATHFN (BUILT_IN_FMOD)
1880 CASE_MATHFN (BUILT_IN_FREXP)
1881 CASE_MATHFN (BUILT_IN_GAMMA)
1882 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1883 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1884 CASE_MATHFN (BUILT_IN_HYPOT)
1885 CASE_MATHFN (BUILT_IN_ILOGB)
1886 CASE_MATHFN (BUILT_IN_ICEIL)
1887 CASE_MATHFN (BUILT_IN_IFLOOR)
1888 CASE_MATHFN (BUILT_IN_INF)
1889 CASE_MATHFN (BUILT_IN_IRINT)
1890 CASE_MATHFN (BUILT_IN_IROUND)
1891 CASE_MATHFN (BUILT_IN_ISINF)
1892 CASE_MATHFN (BUILT_IN_J0)
1893 CASE_MATHFN (BUILT_IN_J1)
1894 CASE_MATHFN (BUILT_IN_JN)
1895 CASE_MATHFN (BUILT_IN_LCEIL)
1896 CASE_MATHFN (BUILT_IN_LDEXP)
1897 CASE_MATHFN (BUILT_IN_LFLOOR)
1898 CASE_MATHFN (BUILT_IN_LGAMMA)
1899 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1900 CASE_MATHFN (BUILT_IN_LLCEIL)
1901 CASE_MATHFN (BUILT_IN_LLFLOOR)
1902 CASE_MATHFN (BUILT_IN_LLRINT)
1903 CASE_MATHFN (BUILT_IN_LLROUND)
1904 CASE_MATHFN (BUILT_IN_LOG)
1905 CASE_MATHFN (BUILT_IN_LOG10)
1906 CASE_MATHFN (BUILT_IN_LOG1P)
1907 CASE_MATHFN (BUILT_IN_LOG2)
1908 CASE_MATHFN (BUILT_IN_LOGB)
1909 CASE_MATHFN (BUILT_IN_LRINT)
1910 CASE_MATHFN (BUILT_IN_LROUND)
1911 CASE_MATHFN (BUILT_IN_MODF)
1912 CASE_MATHFN (BUILT_IN_NAN)
1913 CASE_MATHFN (BUILT_IN_NANS)
1914 CASE_MATHFN (BUILT_IN_NEARBYINT)
1915 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1916 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1917 CASE_MATHFN (BUILT_IN_POW)
1918 CASE_MATHFN (BUILT_IN_POWI)
1919 CASE_MATHFN (BUILT_IN_POW10)
1920 CASE_MATHFN (BUILT_IN_REMAINDER)
1921 CASE_MATHFN (BUILT_IN_REMQUO)
1922 CASE_MATHFN (BUILT_IN_RINT)
1923 CASE_MATHFN (BUILT_IN_ROUND)
1924 CASE_MATHFN (BUILT_IN_SCALB)
1925 CASE_MATHFN (BUILT_IN_SCALBLN)
1926 CASE_MATHFN (BUILT_IN_SCALBN)
1927 CASE_MATHFN (BUILT_IN_SIGNBIT)
1928 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1929 CASE_MATHFN (BUILT_IN_SIN)
1930 CASE_MATHFN (BUILT_IN_SINCOS)
1931 CASE_MATHFN (BUILT_IN_SINH)
1932 CASE_MATHFN (BUILT_IN_SQRT)
1933 CASE_MATHFN (BUILT_IN_TAN)
1934 CASE_MATHFN (BUILT_IN_TANH)
1935 CASE_MATHFN (BUILT_IN_TGAMMA)
1936 CASE_MATHFN (BUILT_IN_TRUNC)
1937 CASE_MATHFN (BUILT_IN_Y0)
1938 CASE_MATHFN (BUILT_IN_Y1)
1939 CASE_MATHFN (BUILT_IN_YN)
1941 default:
1942 return NULL_TREE;
1945 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1946 fcode2 = fcode;
1947 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1948 fcode2 = fcodef;
1949 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1950 fcode2 = fcodel;
1951 else
1952 return NULL_TREE;
1954 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1955 return NULL_TREE;
1957 return builtin_decl_explicit (fcode2);
1960 /* Like mathfn_built_in_1(), but always use the implicit array. */
1962 tree
1963 mathfn_built_in (tree type, enum built_in_function fn)
1965 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1968 /* If errno must be maintained, expand the RTL to check if the result,
1969 TARGET, of a built-in function call, EXP, is NaN, and if so set
1970 errno to EDOM. */
1972 static void
1973 expand_errno_check (tree exp, rtx target)
1975 rtx_code_label *lab = gen_label_rtx ();
1977 /* Test the result; if it is NaN, set errno=EDOM because
1978 the argument was not in the domain. */
1979 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1980 NULL_RTX, NULL_RTX, lab,
1981 /* The jump is very likely. */
1982 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1984 #ifdef TARGET_EDOM
1985 /* If this built-in doesn't throw an exception, set errno directly. */
1986 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1988 #ifdef GEN_ERRNO_RTX
1989 rtx errno_rtx = GEN_ERRNO_RTX;
1990 #else
1991 rtx errno_rtx
1992 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1993 #endif
1994 emit_move_insn (errno_rtx,
1995 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
1996 emit_label (lab);
1997 return;
1999 #endif
2001 /* Make sure the library call isn't expanded as a tail call. */
2002 CALL_EXPR_TAILCALL (exp) = 0;
2004 /* We can't set errno=EDOM directly; let the library call do it.
2005 Pop the arguments right away in case the call gets deleted. */
2006 NO_DEFER_POP;
2007 expand_call (exp, target, 0);
2008 OK_DEFER_POP;
2009 emit_label (lab);
2012 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2013 Return NULL_RTX if a normal call should be emitted rather than expanding
2014 the function in-line. EXP is the expression that is a call to the builtin
2015 function; if convenient, the result should be placed in TARGET.
2016 SUBTARGET may be used as the target for computing one of EXP's operands. */
2018 static rtx
2019 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2021 optab builtin_optab;
2022 rtx op0;
2023 rtx_insn *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, result;
2150 rtx_insn *insns;
2151 int op1_type = REAL_TYPE;
2152 tree fndecl = get_callee_fndecl (exp);
2153 tree arg0, arg1;
2154 enum machine_mode mode;
2155 bool errno_set = true;
2157 switch (DECL_FUNCTION_CODE (fndecl))
2159 CASE_FLT_FN (BUILT_IN_SCALBN):
2160 CASE_FLT_FN (BUILT_IN_SCALBLN):
2161 CASE_FLT_FN (BUILT_IN_LDEXP):
2162 op1_type = INTEGER_TYPE;
2163 default:
2164 break;
2167 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2168 return NULL_RTX;
2170 arg0 = CALL_EXPR_ARG (exp, 0);
2171 arg1 = CALL_EXPR_ARG (exp, 1);
2173 switch (DECL_FUNCTION_CODE (fndecl))
2175 CASE_FLT_FN (BUILT_IN_POW):
2176 builtin_optab = pow_optab; break;
2177 CASE_FLT_FN (BUILT_IN_ATAN2):
2178 builtin_optab = atan2_optab; break;
2179 CASE_FLT_FN (BUILT_IN_SCALB):
2180 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2181 return 0;
2182 builtin_optab = scalb_optab; break;
2183 CASE_FLT_FN (BUILT_IN_SCALBN):
2184 CASE_FLT_FN (BUILT_IN_SCALBLN):
2185 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2186 return 0;
2187 /* Fall through... */
2188 CASE_FLT_FN (BUILT_IN_LDEXP):
2189 builtin_optab = ldexp_optab; break;
2190 CASE_FLT_FN (BUILT_IN_FMOD):
2191 builtin_optab = fmod_optab; break;
2192 CASE_FLT_FN (BUILT_IN_REMAINDER):
2193 CASE_FLT_FN (BUILT_IN_DREM):
2194 builtin_optab = remainder_optab; break;
2195 default:
2196 gcc_unreachable ();
2199 /* Make a suitable register to place result in. */
2200 mode = TYPE_MODE (TREE_TYPE (exp));
2202 /* Before working hard, check whether the instruction is available. */
2203 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2204 return NULL_RTX;
2206 result = gen_reg_rtx (mode);
2208 if (! flag_errno_math || ! HONOR_NANS (mode))
2209 errno_set = false;
2211 if (errno_set && optimize_insn_for_size_p ())
2212 return 0;
2214 /* Always stabilize the argument list. */
2215 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2216 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2218 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2219 op1 = expand_normal (arg1);
2221 start_sequence ();
2223 /* Compute into RESULT.
2224 Set RESULT to wherever the result comes back. */
2225 result = expand_binop (mode, builtin_optab, op0, op1,
2226 result, 0, OPTAB_DIRECT);
2228 /* If we were unable to expand via the builtin, stop the sequence
2229 (without outputting the insns) and call to the library function
2230 with the stabilized argument list. */
2231 if (result == 0)
2233 end_sequence ();
2234 return expand_call (exp, target, target == const0_rtx);
2237 if (errno_set)
2238 expand_errno_check (exp, result);
2240 /* Output the entire sequence. */
2241 insns = get_insns ();
2242 end_sequence ();
2243 emit_insn (insns);
2245 return result;
2248 /* Expand a call to the builtin trinary math functions (fma).
2249 Return NULL_RTX if a normal call should be emitted rather than expanding the
2250 function in-line. EXP is the expression that is a call to the builtin
2251 function; if convenient, the result should be placed in TARGET.
2252 SUBTARGET may be used as the target for computing one of EXP's
2253 operands. */
2255 static rtx
2256 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2258 optab builtin_optab;
2259 rtx op0, op1, op2, result;
2260 rtx_insn *insns;
2261 tree fndecl = get_callee_fndecl (exp);
2262 tree arg0, arg1, arg2;
2263 enum machine_mode mode;
2265 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2266 return NULL_RTX;
2268 arg0 = CALL_EXPR_ARG (exp, 0);
2269 arg1 = CALL_EXPR_ARG (exp, 1);
2270 arg2 = CALL_EXPR_ARG (exp, 2);
2272 switch (DECL_FUNCTION_CODE (fndecl))
2274 CASE_FLT_FN (BUILT_IN_FMA):
2275 builtin_optab = fma_optab; break;
2276 default:
2277 gcc_unreachable ();
2280 /* Make a suitable register to place result in. */
2281 mode = TYPE_MODE (TREE_TYPE (exp));
2283 /* Before working hard, check whether the instruction is available. */
2284 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2285 return NULL_RTX;
2287 result = gen_reg_rtx (mode);
2289 /* Always stabilize the argument list. */
2290 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2291 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2292 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2294 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2295 op1 = expand_normal (arg1);
2296 op2 = expand_normal (arg2);
2298 start_sequence ();
2300 /* Compute into RESULT.
2301 Set RESULT to wherever the result comes back. */
2302 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2303 result, 0);
2305 /* If we were unable to expand via the builtin, stop the sequence
2306 (without outputting the insns) and call to the library function
2307 with the stabilized argument list. */
2308 if (result == 0)
2310 end_sequence ();
2311 return expand_call (exp, target, target == const0_rtx);
2314 /* Output the entire sequence. */
2315 insns = get_insns ();
2316 end_sequence ();
2317 emit_insn (insns);
2319 return result;
2322 /* Expand a call to the builtin sin and cos math functions.
2323 Return NULL_RTX if a normal call should be emitted rather than expanding the
2324 function in-line. EXP is the expression that is a call to the builtin
2325 function; if convenient, the result should be placed in TARGET.
2326 SUBTARGET may be used as the target for computing one of EXP's
2327 operands. */
2329 static rtx
2330 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2332 optab builtin_optab;
2333 rtx op0;
2334 rtx_insn *insns;
2335 tree fndecl = get_callee_fndecl (exp);
2336 enum machine_mode mode;
2337 tree arg;
2339 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2340 return NULL_RTX;
2342 arg = CALL_EXPR_ARG (exp, 0);
2344 switch (DECL_FUNCTION_CODE (fndecl))
2346 CASE_FLT_FN (BUILT_IN_SIN):
2347 CASE_FLT_FN (BUILT_IN_COS):
2348 builtin_optab = sincos_optab; break;
2349 default:
2350 gcc_unreachable ();
2353 /* Make a suitable register to place result in. */
2354 mode = TYPE_MODE (TREE_TYPE (exp));
2356 /* Check if sincos insn is available, otherwise fallback
2357 to sin or cos insn. */
2358 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2359 switch (DECL_FUNCTION_CODE (fndecl))
2361 CASE_FLT_FN (BUILT_IN_SIN):
2362 builtin_optab = sin_optab; break;
2363 CASE_FLT_FN (BUILT_IN_COS):
2364 builtin_optab = cos_optab; break;
2365 default:
2366 gcc_unreachable ();
2369 /* Before working hard, check whether the instruction is available. */
2370 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2372 rtx result = gen_reg_rtx (mode);
2374 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2375 need to expand the argument again. This way, we will not perform
2376 side-effects more the once. */
2377 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2379 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2381 start_sequence ();
2383 /* Compute into RESULT.
2384 Set RESULT to wherever the result comes back. */
2385 if (builtin_optab == sincos_optab)
2387 int ok;
2389 switch (DECL_FUNCTION_CODE (fndecl))
2391 CASE_FLT_FN (BUILT_IN_SIN):
2392 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2393 break;
2394 CASE_FLT_FN (BUILT_IN_COS):
2395 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2396 break;
2397 default:
2398 gcc_unreachable ();
2400 gcc_assert (ok);
2402 else
2403 result = expand_unop (mode, builtin_optab, op0, result, 0);
2405 if (result != 0)
2407 /* Output the entire sequence. */
2408 insns = get_insns ();
2409 end_sequence ();
2410 emit_insn (insns);
2411 return result;
2414 /* If we were unable to expand via the builtin, stop the sequence
2415 (without outputting the insns) and call to the library function
2416 with the stabilized argument list. */
2417 end_sequence ();
2420 return expand_call (exp, target, target == const0_rtx);
2423 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2424 return an RTL instruction code that implements the functionality.
2425 If that isn't possible or available return CODE_FOR_nothing. */
2427 static enum insn_code
2428 interclass_mathfn_icode (tree arg, tree fndecl)
2430 bool errno_set = false;
2431 optab builtin_optab = unknown_optab;
2432 enum machine_mode mode;
2434 switch (DECL_FUNCTION_CODE (fndecl))
2436 CASE_FLT_FN (BUILT_IN_ILOGB):
2437 errno_set = true; builtin_optab = ilogb_optab; break;
2438 CASE_FLT_FN (BUILT_IN_ISINF):
2439 builtin_optab = isinf_optab; break;
2440 case BUILT_IN_ISNORMAL:
2441 case BUILT_IN_ISFINITE:
2442 CASE_FLT_FN (BUILT_IN_FINITE):
2443 case BUILT_IN_FINITED32:
2444 case BUILT_IN_FINITED64:
2445 case BUILT_IN_FINITED128:
2446 case BUILT_IN_ISINFD32:
2447 case BUILT_IN_ISINFD64:
2448 case BUILT_IN_ISINFD128:
2449 /* These builtins have no optabs (yet). */
2450 break;
2451 default:
2452 gcc_unreachable ();
2455 /* There's no easy way to detect the case we need to set EDOM. */
2456 if (flag_errno_math && errno_set)
2457 return CODE_FOR_nothing;
2459 /* Optab mode depends on the mode of the input argument. */
2460 mode = TYPE_MODE (TREE_TYPE (arg));
2462 if (builtin_optab)
2463 return optab_handler (builtin_optab, mode);
2464 return CODE_FOR_nothing;
2467 /* Expand a call to one of the builtin math functions that operate on
2468 floating point argument and output an integer result (ilogb, isinf,
2469 isnan, etc).
2470 Return 0 if a normal call should be emitted rather than expanding the
2471 function in-line. EXP is the expression that is a call to the builtin
2472 function; if convenient, the result should be placed in TARGET. */
2474 static rtx
2475 expand_builtin_interclass_mathfn (tree exp, rtx target)
2477 enum insn_code icode = CODE_FOR_nothing;
2478 rtx op0;
2479 tree fndecl = get_callee_fndecl (exp);
2480 enum machine_mode mode;
2481 tree arg;
2483 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2484 return NULL_RTX;
2486 arg = CALL_EXPR_ARG (exp, 0);
2487 icode = interclass_mathfn_icode (arg, fndecl);
2488 mode = TYPE_MODE (TREE_TYPE (arg));
2490 if (icode != CODE_FOR_nothing)
2492 struct expand_operand ops[1];
2493 rtx_insn *last = get_last_insn ();
2494 tree orig_arg = arg;
2496 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2497 need to expand the argument again. This way, we will not perform
2498 side-effects more the once. */
2499 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2501 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2503 if (mode != GET_MODE (op0))
2504 op0 = convert_to_mode (mode, op0, 0);
2506 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2507 if (maybe_legitimize_operands (icode, 0, 1, ops)
2508 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2509 return ops[0].value;
2511 delete_insns_since (last);
2512 CALL_EXPR_ARG (exp, 0) = orig_arg;
2515 return NULL_RTX;
2518 /* Expand a call to the builtin sincos math function.
2519 Return NULL_RTX if a normal call should be emitted rather than expanding the
2520 function in-line. EXP is the expression that is a call to the builtin
2521 function. */
2523 static rtx
2524 expand_builtin_sincos (tree exp)
2526 rtx op0, op1, op2, target1, target2;
2527 enum machine_mode mode;
2528 tree arg, sinp, cosp;
2529 int result;
2530 location_t loc = EXPR_LOCATION (exp);
2531 tree alias_type, alias_off;
2533 if (!validate_arglist (exp, REAL_TYPE,
2534 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2535 return NULL_RTX;
2537 arg = CALL_EXPR_ARG (exp, 0);
2538 sinp = CALL_EXPR_ARG (exp, 1);
2539 cosp = CALL_EXPR_ARG (exp, 2);
2541 /* Make a suitable register to place result in. */
2542 mode = TYPE_MODE (TREE_TYPE (arg));
2544 /* Check if sincos insn is available, otherwise emit the call. */
2545 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2546 return NULL_RTX;
2548 target1 = gen_reg_rtx (mode);
2549 target2 = gen_reg_rtx (mode);
2551 op0 = expand_normal (arg);
2552 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2553 alias_off = build_int_cst (alias_type, 0);
2554 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2555 sinp, alias_off));
2556 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2557 cosp, alias_off));
2559 /* Compute into target1 and target2.
2560 Set TARGET to wherever the result comes back. */
2561 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2562 gcc_assert (result);
2564 /* Move target1 and target2 to the memory locations indicated
2565 by op1 and op2. */
2566 emit_move_insn (op1, target1);
2567 emit_move_insn (op2, target2);
2569 return const0_rtx;
2572 /* Expand a call to the internal cexpi builtin to the sincos math function.
2573 EXP is the expression that is a call to the builtin function; if convenient,
2574 the result should be placed in TARGET. */
2576 static rtx
2577 expand_builtin_cexpi (tree exp, rtx target)
2579 tree fndecl = get_callee_fndecl (exp);
2580 tree arg, type;
2581 enum machine_mode mode;
2582 rtx op0, op1, op2;
2583 location_t loc = EXPR_LOCATION (exp);
2585 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2586 return NULL_RTX;
2588 arg = CALL_EXPR_ARG (exp, 0);
2589 type = TREE_TYPE (arg);
2590 mode = TYPE_MODE (TREE_TYPE (arg));
2592 /* Try expanding via a sincos optab, fall back to emitting a libcall
2593 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2594 is only generated from sincos, cexp or if we have either of them. */
2595 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2597 op1 = gen_reg_rtx (mode);
2598 op2 = gen_reg_rtx (mode);
2600 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2602 /* Compute into op1 and op2. */
2603 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2605 else if (targetm.libc_has_function (function_sincos))
2607 tree call, fn = NULL_TREE;
2608 tree top1, top2;
2609 rtx op1a, op2a;
2611 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2612 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2613 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2614 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2615 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2616 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2617 else
2618 gcc_unreachable ();
2620 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2621 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2622 op1a = copy_addr_to_reg (XEXP (op1, 0));
2623 op2a = copy_addr_to_reg (XEXP (op2, 0));
2624 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2625 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2627 /* Make sure not to fold the sincos call again. */
2628 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2629 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2630 call, 3, arg, top1, top2));
2632 else
2634 tree call, fn = NULL_TREE, narg;
2635 tree ctype = build_complex_type (type);
2637 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2638 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2639 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2640 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2641 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2642 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2643 else
2644 gcc_unreachable ();
2646 /* If we don't have a decl for cexp create one. This is the
2647 friendliest fallback if the user calls __builtin_cexpi
2648 without full target C99 function support. */
2649 if (fn == NULL_TREE)
2651 tree fntype;
2652 const char *name = NULL;
2654 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2655 name = "cexpf";
2656 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2657 name = "cexp";
2658 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2659 name = "cexpl";
2661 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2662 fn = build_fn_decl (name, fntype);
2665 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2666 build_real (type, dconst0), arg);
2668 /* Make sure not to fold the cexp call again. */
2669 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2670 return expand_expr (build_call_nary (ctype, call, 1, narg),
2671 target, VOIDmode, EXPAND_NORMAL);
2674 /* Now build the proper return type. */
2675 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2676 make_tree (TREE_TYPE (arg), op2),
2677 make_tree (TREE_TYPE (arg), op1)),
2678 target, VOIDmode, EXPAND_NORMAL);
2681 /* Conveniently construct a function call expression. FNDECL names the
2682 function to be called, N is the number of arguments, and the "..."
2683 parameters are the argument expressions. Unlike build_call_exr
2684 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2686 static tree
2687 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2689 va_list ap;
2690 tree fntype = TREE_TYPE (fndecl);
2691 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2693 va_start (ap, n);
2694 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2695 va_end (ap);
2696 SET_EXPR_LOCATION (fn, loc);
2697 return fn;
2700 /* Expand a call to one of the builtin rounding functions gcc defines
2701 as an extension (lfloor and lceil). As these are gcc extensions we
2702 do not need to worry about setting errno to EDOM.
2703 If expanding via optab fails, lower expression to (int)(floor(x)).
2704 EXP is the expression that is a call to the builtin function;
2705 if convenient, the result should be placed in TARGET. */
2707 static rtx
2708 expand_builtin_int_roundingfn (tree exp, rtx target)
2710 convert_optab builtin_optab;
2711 rtx op0, tmp;
2712 rtx_insn *insns;
2713 tree fndecl = get_callee_fndecl (exp);
2714 enum built_in_function fallback_fn;
2715 tree fallback_fndecl;
2716 enum machine_mode mode;
2717 tree arg;
2719 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2720 gcc_unreachable ();
2722 arg = CALL_EXPR_ARG (exp, 0);
2724 switch (DECL_FUNCTION_CODE (fndecl))
2726 CASE_FLT_FN (BUILT_IN_ICEIL):
2727 CASE_FLT_FN (BUILT_IN_LCEIL):
2728 CASE_FLT_FN (BUILT_IN_LLCEIL):
2729 builtin_optab = lceil_optab;
2730 fallback_fn = BUILT_IN_CEIL;
2731 break;
2733 CASE_FLT_FN (BUILT_IN_IFLOOR):
2734 CASE_FLT_FN (BUILT_IN_LFLOOR):
2735 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2736 builtin_optab = lfloor_optab;
2737 fallback_fn = BUILT_IN_FLOOR;
2738 break;
2740 default:
2741 gcc_unreachable ();
2744 /* Make a suitable register to place result in. */
2745 mode = TYPE_MODE (TREE_TYPE (exp));
2747 target = gen_reg_rtx (mode);
2749 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2750 need to expand the argument again. This way, we will not perform
2751 side-effects more the once. */
2752 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2754 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2756 start_sequence ();
2758 /* Compute into TARGET. */
2759 if (expand_sfix_optab (target, op0, builtin_optab))
2761 /* Output the entire sequence. */
2762 insns = get_insns ();
2763 end_sequence ();
2764 emit_insn (insns);
2765 return target;
2768 /* If we were unable to expand via the builtin, stop the sequence
2769 (without outputting the insns). */
2770 end_sequence ();
2772 /* Fall back to floating point rounding optab. */
2773 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2775 /* For non-C99 targets we may end up without a fallback fndecl here
2776 if the user called __builtin_lfloor directly. In this case emit
2777 a call to the floor/ceil variants nevertheless. This should result
2778 in the best user experience for not full C99 targets. */
2779 if (fallback_fndecl == NULL_TREE)
2781 tree fntype;
2782 const char *name = NULL;
2784 switch (DECL_FUNCTION_CODE (fndecl))
2786 case BUILT_IN_ICEIL:
2787 case BUILT_IN_LCEIL:
2788 case BUILT_IN_LLCEIL:
2789 name = "ceil";
2790 break;
2791 case BUILT_IN_ICEILF:
2792 case BUILT_IN_LCEILF:
2793 case BUILT_IN_LLCEILF:
2794 name = "ceilf";
2795 break;
2796 case BUILT_IN_ICEILL:
2797 case BUILT_IN_LCEILL:
2798 case BUILT_IN_LLCEILL:
2799 name = "ceill";
2800 break;
2801 case BUILT_IN_IFLOOR:
2802 case BUILT_IN_LFLOOR:
2803 case BUILT_IN_LLFLOOR:
2804 name = "floor";
2805 break;
2806 case BUILT_IN_IFLOORF:
2807 case BUILT_IN_LFLOORF:
2808 case BUILT_IN_LLFLOORF:
2809 name = "floorf";
2810 break;
2811 case BUILT_IN_IFLOORL:
2812 case BUILT_IN_LFLOORL:
2813 case BUILT_IN_LLFLOORL:
2814 name = "floorl";
2815 break;
2816 default:
2817 gcc_unreachable ();
2820 fntype = build_function_type_list (TREE_TYPE (arg),
2821 TREE_TYPE (arg), NULL_TREE);
2822 fallback_fndecl = build_fn_decl (name, fntype);
2825 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2827 tmp = expand_normal (exp);
2828 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2830 /* Truncate the result of floating point optab to integer
2831 via expand_fix (). */
2832 target = gen_reg_rtx (mode);
2833 expand_fix (target, tmp, 0);
2835 return target;
2838 /* Expand a call to one of the builtin math functions doing integer
2839 conversion (lrint).
2840 Return 0 if a normal call should be emitted rather than expanding the
2841 function in-line. EXP is the expression that is a call to the builtin
2842 function; if convenient, the result should be placed in TARGET. */
2844 static rtx
2845 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2847 convert_optab builtin_optab;
2848 rtx op0;
2849 rtx_insn *insns;
2850 tree fndecl = get_callee_fndecl (exp);
2851 tree arg;
2852 enum machine_mode mode;
2853 enum built_in_function fallback_fn = BUILT_IN_NONE;
2855 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2856 gcc_unreachable ();
2858 arg = CALL_EXPR_ARG (exp, 0);
2860 switch (DECL_FUNCTION_CODE (fndecl))
2862 CASE_FLT_FN (BUILT_IN_IRINT):
2863 fallback_fn = BUILT_IN_LRINT;
2864 /* FALLTHRU */
2865 CASE_FLT_FN (BUILT_IN_LRINT):
2866 CASE_FLT_FN (BUILT_IN_LLRINT):
2867 builtin_optab = lrint_optab;
2868 break;
2870 CASE_FLT_FN (BUILT_IN_IROUND):
2871 fallback_fn = BUILT_IN_LROUND;
2872 /* FALLTHRU */
2873 CASE_FLT_FN (BUILT_IN_LROUND):
2874 CASE_FLT_FN (BUILT_IN_LLROUND):
2875 builtin_optab = lround_optab;
2876 break;
2878 default:
2879 gcc_unreachable ();
2882 /* There's no easy way to detect the case we need to set EDOM. */
2883 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2884 return NULL_RTX;
2886 /* Make a suitable register to place result in. */
2887 mode = TYPE_MODE (TREE_TYPE (exp));
2889 /* There's no easy way to detect the case we need to set EDOM. */
2890 if (!flag_errno_math)
2892 rtx result = gen_reg_rtx (mode);
2894 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2895 need to expand the argument again. This way, we will not perform
2896 side-effects more the once. */
2897 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2899 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2901 start_sequence ();
2903 if (expand_sfix_optab (result, op0, builtin_optab))
2905 /* Output the entire sequence. */
2906 insns = get_insns ();
2907 end_sequence ();
2908 emit_insn (insns);
2909 return result;
2912 /* If we were unable to expand via the builtin, stop the sequence
2913 (without outputting the insns) and call to the library function
2914 with the stabilized argument list. */
2915 end_sequence ();
2918 if (fallback_fn != BUILT_IN_NONE)
2920 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2921 targets, (int) round (x) should never be transformed into
2922 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2923 a call to lround in the hope that the target provides at least some
2924 C99 functions. This should result in the best user experience for
2925 not full C99 targets. */
2926 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2927 fallback_fn, 0);
2929 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2930 fallback_fndecl, 1, arg);
2932 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2933 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2934 return convert_to_mode (mode, target, 0);
2937 return expand_call (exp, target, target == const0_rtx);
2940 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2941 a normal call should be emitted rather than expanding the function
2942 in-line. EXP is the expression that is a call to the builtin
2943 function; if convenient, the result should be placed in TARGET. */
2945 static rtx
2946 expand_builtin_powi (tree exp, rtx target)
2948 tree arg0, arg1;
2949 rtx op0, op1;
2950 enum machine_mode mode;
2951 enum machine_mode mode2;
2953 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2954 return NULL_RTX;
2956 arg0 = CALL_EXPR_ARG (exp, 0);
2957 arg1 = CALL_EXPR_ARG (exp, 1);
2958 mode = TYPE_MODE (TREE_TYPE (exp));
2960 /* Emit a libcall to libgcc. */
2962 /* Mode of the 2nd argument must match that of an int. */
2963 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2965 if (target == NULL_RTX)
2966 target = gen_reg_rtx (mode);
2968 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2969 if (GET_MODE (op0) != mode)
2970 op0 = convert_to_mode (mode, op0, 0);
2971 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2972 if (GET_MODE (op1) != mode2)
2973 op1 = convert_to_mode (mode2, op1, 0);
2975 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2976 target, LCT_CONST, mode, 2,
2977 op0, mode, op1, mode2);
2979 return target;
2982 /* Expand expression EXP which is a call to the strlen builtin. Return
2983 NULL_RTX if we failed the caller should emit a normal call, otherwise
2984 try to get the result in TARGET, if convenient. */
2986 static rtx
2987 expand_builtin_strlen (tree exp, rtx target,
2988 enum machine_mode target_mode)
2990 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2991 return NULL_RTX;
2992 else
2994 struct expand_operand ops[4];
2995 rtx pat;
2996 tree len;
2997 tree src = CALL_EXPR_ARG (exp, 0);
2998 rtx src_reg;
2999 rtx_insn *before_strlen;
3000 enum machine_mode insn_mode = target_mode;
3001 enum insn_code icode = CODE_FOR_nothing;
3002 unsigned int align;
3004 /* If the length can be computed at compile-time, return it. */
3005 len = c_strlen (src, 0);
3006 if (len)
3007 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3009 /* If the length can be computed at compile-time and is constant
3010 integer, but there are side-effects in src, evaluate
3011 src for side-effects, then return len.
3012 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3013 can be optimized into: i++; x = 3; */
3014 len = c_strlen (src, 1);
3015 if (len && TREE_CODE (len) == INTEGER_CST)
3017 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3018 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3021 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3023 /* If SRC is not a pointer type, don't do this operation inline. */
3024 if (align == 0)
3025 return NULL_RTX;
3027 /* Bail out if we can't compute strlen in the right mode. */
3028 while (insn_mode != VOIDmode)
3030 icode = optab_handler (strlen_optab, insn_mode);
3031 if (icode != CODE_FOR_nothing)
3032 break;
3034 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3036 if (insn_mode == VOIDmode)
3037 return NULL_RTX;
3039 /* Make a place to hold the source address. We will not expand
3040 the actual source until we are sure that the expansion will
3041 not fail -- there are trees that cannot be expanded twice. */
3042 src_reg = gen_reg_rtx (Pmode);
3044 /* Mark the beginning of the strlen sequence so we can emit the
3045 source operand later. */
3046 before_strlen = get_last_insn ();
3048 create_output_operand (&ops[0], target, insn_mode);
3049 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3050 create_integer_operand (&ops[2], 0);
3051 create_integer_operand (&ops[3], align);
3052 if (!maybe_expand_insn (icode, 4, ops))
3053 return NULL_RTX;
3055 /* Now that we are assured of success, expand the source. */
3056 start_sequence ();
3057 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3058 if (pat != src_reg)
3060 #ifdef POINTERS_EXTEND_UNSIGNED
3061 if (GET_MODE (pat) != Pmode)
3062 pat = convert_to_mode (Pmode, pat,
3063 POINTERS_EXTEND_UNSIGNED);
3064 #endif
3065 emit_move_insn (src_reg, pat);
3067 pat = get_insns ();
3068 end_sequence ();
3070 if (before_strlen)
3071 emit_insn_after (pat, before_strlen);
3072 else
3073 emit_insn_before (pat, get_insns ());
3075 /* Return the value in the proper mode for this function. */
3076 if (GET_MODE (ops[0].value) == target_mode)
3077 target = ops[0].value;
3078 else if (target != 0)
3079 convert_move (target, ops[0].value, 0);
3080 else
3081 target = convert_to_mode (target_mode, ops[0].value, 0);
3083 return target;
3087 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3088 bytes from constant string DATA + OFFSET and return it as target
3089 constant. */
3091 static rtx
3092 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3093 enum machine_mode mode)
3095 const char *str = (const char *) data;
3097 gcc_assert (offset >= 0
3098 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3099 <= strlen (str) + 1));
3101 return c_readstr (str + offset, mode);
3104 /* LEN specify length of the block of memcpy/memset operation.
3105 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3106 In some cases we can make very likely guess on max size, then we
3107 set it into PROBABLE_MAX_SIZE. */
3109 static void
3110 determine_block_size (tree len, rtx len_rtx,
3111 unsigned HOST_WIDE_INT *min_size,
3112 unsigned HOST_WIDE_INT *max_size,
3113 unsigned HOST_WIDE_INT *probable_max_size)
3115 if (CONST_INT_P (len_rtx))
3117 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3118 return;
3120 else
3122 wide_int min, max;
3123 enum value_range_type range_type = VR_UNDEFINED;
3125 /* Determine bounds from the type. */
3126 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3127 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3128 else
3129 *min_size = 0;
3130 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3131 *probable_max_size = *max_size
3132 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3133 else
3134 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3136 if (TREE_CODE (len) == SSA_NAME)
3137 range_type = get_range_info (len, &min, &max);
3138 if (range_type == VR_RANGE)
3140 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3141 *min_size = min.to_uhwi ();
3142 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3143 *probable_max_size = *max_size = max.to_uhwi ();
3145 else if (range_type == VR_ANTI_RANGE)
3147 /* Anti range 0...N lets us to determine minimal size to N+1. */
3148 if (min == 0)
3150 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
3151 *min_size = max.to_uhwi () + 1;
3153 /* Code like
3155 int n;
3156 if (n < 100)
3157 memcpy (a, b, n)
3159 Produce anti range allowing negative values of N. We still
3160 can use the information and make a guess that N is not negative.
3162 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3163 *probable_max_size = min.to_uhwi () - 1;
3166 gcc_checking_assert (*max_size <=
3167 (unsigned HOST_WIDE_INT)
3168 GET_MODE_MASK (GET_MODE (len_rtx)));
3171 /* Expand a call EXP to the memcpy builtin.
3172 Return NULL_RTX if we failed, the caller should emit a normal call,
3173 otherwise try to get the result in TARGET, if convenient (and in
3174 mode MODE if that's convenient). */
3176 static rtx
3177 expand_builtin_memcpy (tree exp, rtx target)
3179 if (!validate_arglist (exp,
3180 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3181 return NULL_RTX;
3182 else
3184 tree dest = CALL_EXPR_ARG (exp, 0);
3185 tree src = CALL_EXPR_ARG (exp, 1);
3186 tree len = CALL_EXPR_ARG (exp, 2);
3187 const char *src_str;
3188 unsigned int src_align = get_pointer_alignment (src);
3189 unsigned int dest_align = get_pointer_alignment (dest);
3190 rtx dest_mem, src_mem, dest_addr, len_rtx;
3191 HOST_WIDE_INT expected_size = -1;
3192 unsigned int expected_align = 0;
3193 unsigned HOST_WIDE_INT min_size;
3194 unsigned HOST_WIDE_INT max_size;
3195 unsigned HOST_WIDE_INT probable_max_size;
3197 /* If DEST is not a pointer type, call the normal function. */
3198 if (dest_align == 0)
3199 return NULL_RTX;
3201 /* If either SRC is not a pointer type, don't do this
3202 operation in-line. */
3203 if (src_align == 0)
3204 return NULL_RTX;
3206 if (currently_expanding_gimple_stmt)
3207 stringop_block_profile (currently_expanding_gimple_stmt,
3208 &expected_align, &expected_size);
3210 if (expected_align < dest_align)
3211 expected_align = dest_align;
3212 dest_mem = get_memory_rtx (dest, len);
3213 set_mem_align (dest_mem, dest_align);
3214 len_rtx = expand_normal (len);
3215 determine_block_size (len, len_rtx, &min_size, &max_size,
3216 &probable_max_size);
3217 src_str = c_getstr (src);
3219 /* If SRC is a string constant and block move would be done
3220 by pieces, we can avoid loading the string from memory
3221 and only stored the computed constants. */
3222 if (src_str
3223 && CONST_INT_P (len_rtx)
3224 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3225 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3226 CONST_CAST (char *, src_str),
3227 dest_align, false))
3229 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3230 builtin_memcpy_read_str,
3231 CONST_CAST (char *, src_str),
3232 dest_align, false, 0);
3233 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3234 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3235 return dest_mem;
3238 src_mem = get_memory_rtx (src, len);
3239 set_mem_align (src_mem, src_align);
3241 /* Copy word part most expediently. */
3242 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3243 CALL_EXPR_TAILCALL (exp)
3244 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3245 expected_align, expected_size,
3246 min_size, max_size, probable_max_size);
3248 if (dest_addr == 0)
3250 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3251 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3253 return dest_addr;
3257 /* Expand a call EXP to the mempcpy builtin.
3258 Return NULL_RTX if we failed; the caller should emit a normal call,
3259 otherwise try to get the result in TARGET, if convenient (and in
3260 mode MODE if that's convenient). If ENDP is 0 return the
3261 destination pointer, if ENDP is 1 return the end pointer ala
3262 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3263 stpcpy. */
3265 static rtx
3266 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3268 if (!validate_arglist (exp,
3269 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3270 return NULL_RTX;
3271 else
3273 tree dest = CALL_EXPR_ARG (exp, 0);
3274 tree src = CALL_EXPR_ARG (exp, 1);
3275 tree len = CALL_EXPR_ARG (exp, 2);
3276 return expand_builtin_mempcpy_args (dest, src, len,
3277 target, mode, /*endp=*/ 1);
3281 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3282 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3283 so that this can also be called without constructing an actual CALL_EXPR.
3284 The other arguments and return value are the same as for
3285 expand_builtin_mempcpy. */
3287 static rtx
3288 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3289 rtx target, enum machine_mode mode, int endp)
3291 /* If return value is ignored, transform mempcpy into memcpy. */
3292 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3294 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3295 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3296 dest, src, len);
3297 return expand_expr (result, target, mode, EXPAND_NORMAL);
3299 else
3301 const char *src_str;
3302 unsigned int src_align = get_pointer_alignment (src);
3303 unsigned int dest_align = get_pointer_alignment (dest);
3304 rtx dest_mem, src_mem, len_rtx;
3306 /* If either SRC or DEST is not a pointer type, don't do this
3307 operation in-line. */
3308 if (dest_align == 0 || src_align == 0)
3309 return NULL_RTX;
3311 /* If LEN is not constant, call the normal function. */
3312 if (! tree_fits_uhwi_p (len))
3313 return NULL_RTX;
3315 len_rtx = expand_normal (len);
3316 src_str = c_getstr (src);
3318 /* If SRC is a string constant and block move would be done
3319 by pieces, we can avoid loading the string from memory
3320 and only stored the computed constants. */
3321 if (src_str
3322 && CONST_INT_P (len_rtx)
3323 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3324 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3325 CONST_CAST (char *, src_str),
3326 dest_align, false))
3328 dest_mem = get_memory_rtx (dest, len);
3329 set_mem_align (dest_mem, dest_align);
3330 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3331 builtin_memcpy_read_str,
3332 CONST_CAST (char *, src_str),
3333 dest_align, false, endp);
3334 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3335 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3336 return dest_mem;
3339 if (CONST_INT_P (len_rtx)
3340 && can_move_by_pieces (INTVAL (len_rtx),
3341 MIN (dest_align, src_align)))
3343 dest_mem = get_memory_rtx (dest, len);
3344 set_mem_align (dest_mem, dest_align);
3345 src_mem = get_memory_rtx (src, len);
3346 set_mem_align (src_mem, src_align);
3347 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3348 MIN (dest_align, src_align), endp);
3349 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3350 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3351 return dest_mem;
3354 return NULL_RTX;
3358 #ifndef HAVE_movstr
3359 # define HAVE_movstr 0
3360 # define CODE_FOR_movstr CODE_FOR_nothing
3361 #endif
3363 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3364 we failed, the caller should emit a normal call, otherwise try to
3365 get the result in TARGET, if convenient. If ENDP is 0 return the
3366 destination pointer, if ENDP is 1 return the end pointer ala
3367 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3368 stpcpy. */
3370 static rtx
3371 expand_movstr (tree dest, tree src, rtx target, int endp)
3373 struct expand_operand ops[3];
3374 rtx dest_mem;
3375 rtx src_mem;
3377 if (!HAVE_movstr)
3378 return NULL_RTX;
3380 dest_mem = get_memory_rtx (dest, NULL);
3381 src_mem = get_memory_rtx (src, NULL);
3382 if (!endp)
3384 target = force_reg (Pmode, XEXP (dest_mem, 0));
3385 dest_mem = replace_equiv_address (dest_mem, target);
3388 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3389 create_fixed_operand (&ops[1], dest_mem);
3390 create_fixed_operand (&ops[2], src_mem);
3391 if (!maybe_expand_insn (CODE_FOR_movstr, 3, ops))
3392 return NULL_RTX;
3394 if (endp && target != const0_rtx)
3396 target = ops[0].value;
3397 /* movstr is supposed to set end to the address of the NUL
3398 terminator. If the caller requested a mempcpy-like return value,
3399 adjust it. */
3400 if (endp == 1)
3402 rtx tem = plus_constant (GET_MODE (target),
3403 gen_lowpart (GET_MODE (target), target), 1);
3404 emit_move_insn (target, force_operand (tem, NULL_RTX));
3407 return target;
3410 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3411 NULL_RTX if we failed the caller should emit a normal call, otherwise
3412 try to get the result in TARGET, if convenient (and in mode MODE if that's
3413 convenient). */
3415 static rtx
3416 expand_builtin_strcpy (tree exp, rtx target)
3418 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3420 tree dest = CALL_EXPR_ARG (exp, 0);
3421 tree src = CALL_EXPR_ARG (exp, 1);
3422 return expand_builtin_strcpy_args (dest, src, target);
3424 return NULL_RTX;
3427 /* Helper function to do the actual work for expand_builtin_strcpy. The
3428 arguments to the builtin_strcpy call DEST and SRC are broken out
3429 so that this can also be called without constructing an actual CALL_EXPR.
3430 The other arguments and return value are the same as for
3431 expand_builtin_strcpy. */
3433 static rtx
3434 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3436 return expand_movstr (dest, src, target, /*endp=*/0);
3439 /* Expand a call EXP to the stpcpy builtin.
3440 Return NULL_RTX if we failed the caller should emit a normal call,
3441 otherwise try to get the result in TARGET, if convenient (and in
3442 mode MODE if that's convenient). */
3444 static rtx
3445 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3447 tree dst, src;
3448 location_t loc = EXPR_LOCATION (exp);
3450 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3451 return NULL_RTX;
3453 dst = CALL_EXPR_ARG (exp, 0);
3454 src = CALL_EXPR_ARG (exp, 1);
3456 /* If return value is ignored, transform stpcpy into strcpy. */
3457 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3459 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3460 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3461 return expand_expr (result, target, mode, EXPAND_NORMAL);
3463 else
3465 tree len, lenp1;
3466 rtx ret;
3468 /* Ensure we get an actual string whose length can be evaluated at
3469 compile-time, not an expression containing a string. This is
3470 because the latter will potentially produce pessimized code
3471 when used to produce the return value. */
3472 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3473 return expand_movstr (dst, src, target, /*endp=*/2);
3475 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3476 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3477 target, mode, /*endp=*/2);
3479 if (ret)
3480 return ret;
3482 if (TREE_CODE (len) == INTEGER_CST)
3484 rtx len_rtx = expand_normal (len);
3486 if (CONST_INT_P (len_rtx))
3488 ret = expand_builtin_strcpy_args (dst, src, target);
3490 if (ret)
3492 if (! target)
3494 if (mode != VOIDmode)
3495 target = gen_reg_rtx (mode);
3496 else
3497 target = gen_reg_rtx (GET_MODE (ret));
3499 if (GET_MODE (target) != GET_MODE (ret))
3500 ret = gen_lowpart (GET_MODE (target), ret);
3502 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3503 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3504 gcc_assert (ret);
3506 return target;
3511 return expand_movstr (dst, src, target, /*endp=*/2);
3515 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3516 bytes from constant string DATA + OFFSET and return it as target
3517 constant. */
3520 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3521 enum machine_mode mode)
3523 const char *str = (const char *) data;
3525 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3526 return const0_rtx;
3528 return c_readstr (str + offset, mode);
3531 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3532 NULL_RTX if we failed the caller should emit a normal call. */
3534 static rtx
3535 expand_builtin_strncpy (tree exp, rtx target)
3537 location_t loc = EXPR_LOCATION (exp);
3539 if (validate_arglist (exp,
3540 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3542 tree dest = CALL_EXPR_ARG (exp, 0);
3543 tree src = CALL_EXPR_ARG (exp, 1);
3544 tree len = CALL_EXPR_ARG (exp, 2);
3545 tree slen = c_strlen (src, 1);
3547 /* We must be passed a constant len and src parameter. */
3548 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3549 return NULL_RTX;
3551 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3553 /* We're required to pad with trailing zeros if the requested
3554 len is greater than strlen(s2)+1. In that case try to
3555 use store_by_pieces, if it fails, punt. */
3556 if (tree_int_cst_lt (slen, len))
3558 unsigned int dest_align = get_pointer_alignment (dest);
3559 const char *p = c_getstr (src);
3560 rtx dest_mem;
3562 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3563 || !can_store_by_pieces (tree_to_uhwi (len),
3564 builtin_strncpy_read_str,
3565 CONST_CAST (char *, p),
3566 dest_align, false))
3567 return NULL_RTX;
3569 dest_mem = get_memory_rtx (dest, len);
3570 store_by_pieces (dest_mem, tree_to_uhwi (len),
3571 builtin_strncpy_read_str,
3572 CONST_CAST (char *, p), dest_align, false, 0);
3573 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3574 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3575 return dest_mem;
3578 return NULL_RTX;
3581 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3582 bytes from constant string DATA + OFFSET and return it as target
3583 constant. */
3586 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3587 enum machine_mode mode)
3589 const char *c = (const char *) data;
3590 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3592 memset (p, *c, GET_MODE_SIZE (mode));
3594 return c_readstr (p, mode);
3597 /* Callback routine for store_by_pieces. Return the RTL of a register
3598 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3599 char value given in the RTL register data. For example, if mode is
3600 4 bytes wide, return the RTL for 0x01010101*data. */
3602 static rtx
3603 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3604 enum machine_mode mode)
3606 rtx target, coeff;
3607 size_t size;
3608 char *p;
3610 size = GET_MODE_SIZE (mode);
3611 if (size == 1)
3612 return (rtx) data;
3614 p = XALLOCAVEC (char, size);
3615 memset (p, 1, size);
3616 coeff = c_readstr (p, mode);
3618 target = convert_to_mode (mode, (rtx) data, 1);
3619 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3620 return force_reg (mode, target);
3623 /* Expand expression EXP, which is a call to the memset builtin. Return
3624 NULL_RTX if we failed the caller should emit a normal call, otherwise
3625 try to get the result in TARGET, if convenient (and in mode MODE if that's
3626 convenient). */
3628 static rtx
3629 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3631 if (!validate_arglist (exp,
3632 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3633 return NULL_RTX;
3634 else
3636 tree dest = CALL_EXPR_ARG (exp, 0);
3637 tree val = CALL_EXPR_ARG (exp, 1);
3638 tree len = CALL_EXPR_ARG (exp, 2);
3639 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3643 /* Helper function to do the actual work for expand_builtin_memset. The
3644 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3645 so that this can also be called without constructing an actual CALL_EXPR.
3646 The other arguments and return value are the same as for
3647 expand_builtin_memset. */
3649 static rtx
3650 expand_builtin_memset_args (tree dest, tree val, tree len,
3651 rtx target, enum machine_mode mode, tree orig_exp)
3653 tree fndecl, fn;
3654 enum built_in_function fcode;
3655 enum machine_mode val_mode;
3656 char c;
3657 unsigned int dest_align;
3658 rtx dest_mem, dest_addr, len_rtx;
3659 HOST_WIDE_INT expected_size = -1;
3660 unsigned int expected_align = 0;
3661 unsigned HOST_WIDE_INT min_size;
3662 unsigned HOST_WIDE_INT max_size;
3663 unsigned HOST_WIDE_INT probable_max_size;
3665 dest_align = get_pointer_alignment (dest);
3667 /* If DEST is not a pointer type, don't do this operation in-line. */
3668 if (dest_align == 0)
3669 return NULL_RTX;
3671 if (currently_expanding_gimple_stmt)
3672 stringop_block_profile (currently_expanding_gimple_stmt,
3673 &expected_align, &expected_size);
3675 if (expected_align < dest_align)
3676 expected_align = dest_align;
3678 /* If the LEN parameter is zero, return DEST. */
3679 if (integer_zerop (len))
3681 /* Evaluate and ignore VAL in case it has side-effects. */
3682 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3683 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3686 /* Stabilize the arguments in case we fail. */
3687 dest = builtin_save_expr (dest);
3688 val = builtin_save_expr (val);
3689 len = builtin_save_expr (len);
3691 len_rtx = expand_normal (len);
3692 determine_block_size (len, len_rtx, &min_size, &max_size,
3693 &probable_max_size);
3694 dest_mem = get_memory_rtx (dest, len);
3695 val_mode = TYPE_MODE (unsigned_char_type_node);
3697 if (TREE_CODE (val) != INTEGER_CST)
3699 rtx val_rtx;
3701 val_rtx = expand_normal (val);
3702 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3704 /* Assume that we can memset by pieces if we can store
3705 * the coefficients by pieces (in the required modes).
3706 * We can't pass builtin_memset_gen_str as that emits RTL. */
3707 c = 1;
3708 if (tree_fits_uhwi_p (len)
3709 && can_store_by_pieces (tree_to_uhwi (len),
3710 builtin_memset_read_str, &c, dest_align,
3711 true))
3713 val_rtx = force_reg (val_mode, val_rtx);
3714 store_by_pieces (dest_mem, tree_to_uhwi (len),
3715 builtin_memset_gen_str, val_rtx, dest_align,
3716 true, 0);
3718 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3719 dest_align, expected_align,
3720 expected_size, min_size, max_size,
3721 probable_max_size))
3722 goto do_libcall;
3724 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3725 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3726 return dest_mem;
3729 if (target_char_cast (val, &c))
3730 goto do_libcall;
3732 if (c)
3734 if (tree_fits_uhwi_p (len)
3735 && can_store_by_pieces (tree_to_uhwi (len),
3736 builtin_memset_read_str, &c, dest_align,
3737 true))
3738 store_by_pieces (dest_mem, tree_to_uhwi (len),
3739 builtin_memset_read_str, &c, dest_align, true, 0);
3740 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3741 gen_int_mode (c, val_mode),
3742 dest_align, expected_align,
3743 expected_size, min_size, max_size,
3744 probable_max_size))
3745 goto do_libcall;
3747 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3748 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3749 return dest_mem;
3752 set_mem_align (dest_mem, dest_align);
3753 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3754 CALL_EXPR_TAILCALL (orig_exp)
3755 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3756 expected_align, expected_size,
3757 min_size, max_size,
3758 probable_max_size);
3760 if (dest_addr == 0)
3762 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3763 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3766 return dest_addr;
3768 do_libcall:
3769 fndecl = get_callee_fndecl (orig_exp);
3770 fcode = DECL_FUNCTION_CODE (fndecl);
3771 if (fcode == BUILT_IN_MEMSET)
3772 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3773 dest, val, len);
3774 else if (fcode == BUILT_IN_BZERO)
3775 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3776 dest, len);
3777 else
3778 gcc_unreachable ();
3779 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3780 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3781 return expand_call (fn, target, target == const0_rtx);
3784 /* Expand expression EXP, which is a call to the bzero builtin. Return
3785 NULL_RTX if we failed the caller should emit a normal call. */
3787 static rtx
3788 expand_builtin_bzero (tree exp)
3790 tree dest, size;
3791 location_t loc = EXPR_LOCATION (exp);
3793 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3794 return NULL_RTX;
3796 dest = CALL_EXPR_ARG (exp, 0);
3797 size = CALL_EXPR_ARG (exp, 1);
3799 /* New argument list transforming bzero(ptr x, int y) to
3800 memset(ptr x, int 0, size_t y). This is done this way
3801 so that if it isn't expanded inline, we fallback to
3802 calling bzero instead of memset. */
3804 return expand_builtin_memset_args (dest, integer_zero_node,
3805 fold_convert_loc (loc,
3806 size_type_node, size),
3807 const0_rtx, VOIDmode, exp);
3810 /* Expand expression EXP, which is a call to the memcmp built-in function.
3811 Return NULL_RTX if we failed and the caller should emit a normal call,
3812 otherwise try to get the result in TARGET, if convenient (and in mode
3813 MODE, if that's convenient). */
3815 static rtx
3816 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3817 ATTRIBUTE_UNUSED enum machine_mode mode)
3819 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3821 if (!validate_arglist (exp,
3822 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3823 return NULL_RTX;
3825 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3826 implementing memcmp because it will stop if it encounters two
3827 zero bytes. */
3828 #if defined HAVE_cmpmemsi
3830 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3831 rtx result;
3832 rtx insn;
3833 tree arg1 = CALL_EXPR_ARG (exp, 0);
3834 tree arg2 = CALL_EXPR_ARG (exp, 1);
3835 tree len = CALL_EXPR_ARG (exp, 2);
3837 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3838 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3839 enum machine_mode insn_mode;
3841 if (HAVE_cmpmemsi)
3842 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3843 else
3844 return NULL_RTX;
3846 /* If we don't have POINTER_TYPE, call the function. */
3847 if (arg1_align == 0 || arg2_align == 0)
3848 return NULL_RTX;
3850 /* Make a place to write the result of the instruction. */
3851 result = target;
3852 if (! (result != 0
3853 && REG_P (result) && GET_MODE (result) == insn_mode
3854 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3855 result = gen_reg_rtx (insn_mode);
3857 arg1_rtx = get_memory_rtx (arg1, len);
3858 arg2_rtx = get_memory_rtx (arg2, len);
3859 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3861 /* Set MEM_SIZE as appropriate. */
3862 if (CONST_INT_P (arg3_rtx))
3864 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3865 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3868 if (HAVE_cmpmemsi)
3869 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3870 GEN_INT (MIN (arg1_align, arg2_align)));
3871 else
3872 gcc_unreachable ();
3874 if (insn)
3875 emit_insn (insn);
3876 else
3877 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3878 TYPE_MODE (integer_type_node), 3,
3879 XEXP (arg1_rtx, 0), Pmode,
3880 XEXP (arg2_rtx, 0), Pmode,
3881 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3882 TYPE_UNSIGNED (sizetype)),
3883 TYPE_MODE (sizetype));
3885 /* Return the value in the proper mode for this function. */
3886 mode = TYPE_MODE (TREE_TYPE (exp));
3887 if (GET_MODE (result) == mode)
3888 return result;
3889 else if (target != 0)
3891 convert_move (target, result, 0);
3892 return target;
3894 else
3895 return convert_to_mode (mode, result, 0);
3897 #endif /* HAVE_cmpmemsi. */
3899 return NULL_RTX;
3902 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3903 if we failed the caller should emit a normal call, otherwise try to get
3904 the result in TARGET, if convenient. */
3906 static rtx
3907 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3909 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3910 return NULL_RTX;
3912 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3913 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3914 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3916 rtx arg1_rtx, arg2_rtx;
3917 rtx result, insn = NULL_RTX;
3918 tree fndecl, fn;
3919 tree arg1 = CALL_EXPR_ARG (exp, 0);
3920 tree arg2 = CALL_EXPR_ARG (exp, 1);
3922 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3923 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3925 /* If we don't have POINTER_TYPE, call the function. */
3926 if (arg1_align == 0 || arg2_align == 0)
3927 return NULL_RTX;
3929 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3930 arg1 = builtin_save_expr (arg1);
3931 arg2 = builtin_save_expr (arg2);
3933 arg1_rtx = get_memory_rtx (arg1, NULL);
3934 arg2_rtx = get_memory_rtx (arg2, NULL);
3936 #ifdef HAVE_cmpstrsi
3937 /* Try to call cmpstrsi. */
3938 if (HAVE_cmpstrsi)
3940 enum machine_mode insn_mode
3941 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3943 /* Make a place to write the result of the instruction. */
3944 result = target;
3945 if (! (result != 0
3946 && REG_P (result) && GET_MODE (result) == insn_mode
3947 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3948 result = gen_reg_rtx (insn_mode);
3950 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3951 GEN_INT (MIN (arg1_align, arg2_align)));
3953 #endif
3954 #ifdef HAVE_cmpstrnsi
3955 /* Try to determine at least one length and call cmpstrnsi. */
3956 if (!insn && HAVE_cmpstrnsi)
3958 tree len;
3959 rtx arg3_rtx;
3961 enum machine_mode insn_mode
3962 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3963 tree len1 = c_strlen (arg1, 1);
3964 tree len2 = c_strlen (arg2, 1);
3966 if (len1)
3967 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3968 if (len2)
3969 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3971 /* If we don't have a constant length for the first, use the length
3972 of the second, if we know it. We don't require a constant for
3973 this case; some cost analysis could be done if both are available
3974 but neither is constant. For now, assume they're equally cheap,
3975 unless one has side effects. If both strings have constant lengths,
3976 use the smaller. */
3978 if (!len1)
3979 len = len2;
3980 else if (!len2)
3981 len = len1;
3982 else if (TREE_SIDE_EFFECTS (len1))
3983 len = len2;
3984 else if (TREE_SIDE_EFFECTS (len2))
3985 len = len1;
3986 else if (TREE_CODE (len1) != INTEGER_CST)
3987 len = len2;
3988 else if (TREE_CODE (len2) != INTEGER_CST)
3989 len = len1;
3990 else if (tree_int_cst_lt (len1, len2))
3991 len = len1;
3992 else
3993 len = len2;
3995 /* If both arguments have side effects, we cannot optimize. */
3996 if (!len || TREE_SIDE_EFFECTS (len))
3997 goto do_libcall;
3999 arg3_rtx = expand_normal (len);
4001 /* Make a place to write the result of the instruction. */
4002 result = target;
4003 if (! (result != 0
4004 && REG_P (result) && GET_MODE (result) == insn_mode
4005 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4006 result = gen_reg_rtx (insn_mode);
4008 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4009 GEN_INT (MIN (arg1_align, arg2_align)));
4011 #endif
4013 if (insn)
4015 enum machine_mode mode;
4016 emit_insn (insn);
4018 /* Return the value in the proper mode for this function. */
4019 mode = TYPE_MODE (TREE_TYPE (exp));
4020 if (GET_MODE (result) == mode)
4021 return result;
4022 if (target == 0)
4023 return convert_to_mode (mode, result, 0);
4024 convert_move (target, result, 0);
4025 return target;
4028 /* Expand the library call ourselves using a stabilized argument
4029 list to avoid re-evaluating the function's arguments twice. */
4030 #ifdef HAVE_cmpstrnsi
4031 do_libcall:
4032 #endif
4033 fndecl = get_callee_fndecl (exp);
4034 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4035 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4036 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4037 return expand_call (fn, target, target == const0_rtx);
4039 #endif
4040 return NULL_RTX;
4043 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4044 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4045 the result in TARGET, if convenient. */
4047 static rtx
4048 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4049 ATTRIBUTE_UNUSED enum machine_mode mode)
4051 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4053 if (!validate_arglist (exp,
4054 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4055 return NULL_RTX;
4057 /* If c_strlen can determine an expression for one of the string
4058 lengths, and it doesn't have side effects, then emit cmpstrnsi
4059 using length MIN(strlen(string)+1, arg3). */
4060 #ifdef HAVE_cmpstrnsi
4061 if (HAVE_cmpstrnsi)
4063 tree len, len1, len2;
4064 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4065 rtx result, insn;
4066 tree fndecl, fn;
4067 tree arg1 = CALL_EXPR_ARG (exp, 0);
4068 tree arg2 = CALL_EXPR_ARG (exp, 1);
4069 tree arg3 = CALL_EXPR_ARG (exp, 2);
4071 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4072 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4073 enum machine_mode insn_mode
4074 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4076 len1 = c_strlen (arg1, 1);
4077 len2 = c_strlen (arg2, 1);
4079 if (len1)
4080 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4081 if (len2)
4082 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4084 /* If we don't have a constant length for the first, use the length
4085 of the second, if we know it. We don't require a constant for
4086 this case; some cost analysis could be done if both are available
4087 but neither is constant. For now, assume they're equally cheap,
4088 unless one has side effects. If both strings have constant lengths,
4089 use the smaller. */
4091 if (!len1)
4092 len = len2;
4093 else if (!len2)
4094 len = len1;
4095 else if (TREE_SIDE_EFFECTS (len1))
4096 len = len2;
4097 else if (TREE_SIDE_EFFECTS (len2))
4098 len = len1;
4099 else if (TREE_CODE (len1) != INTEGER_CST)
4100 len = len2;
4101 else if (TREE_CODE (len2) != INTEGER_CST)
4102 len = len1;
4103 else if (tree_int_cst_lt (len1, len2))
4104 len = len1;
4105 else
4106 len = len2;
4108 /* If both arguments have side effects, we cannot optimize. */
4109 if (!len || TREE_SIDE_EFFECTS (len))
4110 return NULL_RTX;
4112 /* The actual new length parameter is MIN(len,arg3). */
4113 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4114 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4116 /* If we don't have POINTER_TYPE, call the function. */
4117 if (arg1_align == 0 || arg2_align == 0)
4118 return NULL_RTX;
4120 /* Make a place to write the result of the instruction. */
4121 result = target;
4122 if (! (result != 0
4123 && REG_P (result) && GET_MODE (result) == insn_mode
4124 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4125 result = gen_reg_rtx (insn_mode);
4127 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4128 arg1 = builtin_save_expr (arg1);
4129 arg2 = builtin_save_expr (arg2);
4130 len = builtin_save_expr (len);
4132 arg1_rtx = get_memory_rtx (arg1, len);
4133 arg2_rtx = get_memory_rtx (arg2, len);
4134 arg3_rtx = expand_normal (len);
4135 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4136 GEN_INT (MIN (arg1_align, arg2_align)));
4137 if (insn)
4139 emit_insn (insn);
4141 /* Return the value in the proper mode for this function. */
4142 mode = TYPE_MODE (TREE_TYPE (exp));
4143 if (GET_MODE (result) == mode)
4144 return result;
4145 if (target == 0)
4146 return convert_to_mode (mode, result, 0);
4147 convert_move (target, result, 0);
4148 return target;
4151 /* Expand the library call ourselves using a stabilized argument
4152 list to avoid re-evaluating the function's arguments twice. */
4153 fndecl = get_callee_fndecl (exp);
4154 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4155 arg1, arg2, len);
4156 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4157 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4158 return expand_call (fn, target, target == const0_rtx);
4160 #endif
4161 return NULL_RTX;
4164 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4165 if that's convenient. */
4168 expand_builtin_saveregs (void)
4170 rtx val;
4171 rtx_insn *seq;
4173 /* Don't do __builtin_saveregs more than once in a function.
4174 Save the result of the first call and reuse it. */
4175 if (saveregs_value != 0)
4176 return saveregs_value;
4178 /* When this function is called, it means that registers must be
4179 saved on entry to this function. So we migrate the call to the
4180 first insn of this function. */
4182 start_sequence ();
4184 /* Do whatever the machine needs done in this case. */
4185 val = targetm.calls.expand_builtin_saveregs ();
4187 seq = get_insns ();
4188 end_sequence ();
4190 saveregs_value = val;
4192 /* Put the insns after the NOTE that starts the function. If this
4193 is inside a start_sequence, make the outer-level insn chain current, so
4194 the code is placed at the start of the function. */
4195 push_topmost_sequence ();
4196 emit_insn_after (seq, entry_of_function ());
4197 pop_topmost_sequence ();
4199 return val;
4202 /* Expand a call to __builtin_next_arg. */
4204 static rtx
4205 expand_builtin_next_arg (void)
4207 /* Checking arguments is already done in fold_builtin_next_arg
4208 that must be called before this function. */
4209 return expand_binop (ptr_mode, add_optab,
4210 crtl->args.internal_arg_pointer,
4211 crtl->args.arg_offset_rtx,
4212 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4215 /* Make it easier for the backends by protecting the valist argument
4216 from multiple evaluations. */
4218 static tree
4219 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4221 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4223 /* The current way of determining the type of valist is completely
4224 bogus. We should have the information on the va builtin instead. */
4225 if (!vatype)
4226 vatype = targetm.fn_abi_va_list (cfun->decl);
4228 if (TREE_CODE (vatype) == ARRAY_TYPE)
4230 if (TREE_SIDE_EFFECTS (valist))
4231 valist = save_expr (valist);
4233 /* For this case, the backends will be expecting a pointer to
4234 vatype, but it's possible we've actually been given an array
4235 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4236 So fix it. */
4237 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4239 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4240 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4243 else
4245 tree pt = build_pointer_type (vatype);
4247 if (! needs_lvalue)
4249 if (! TREE_SIDE_EFFECTS (valist))
4250 return valist;
4252 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4253 TREE_SIDE_EFFECTS (valist) = 1;
4256 if (TREE_SIDE_EFFECTS (valist))
4257 valist = save_expr (valist);
4258 valist = fold_build2_loc (loc, MEM_REF,
4259 vatype, valist, build_int_cst (pt, 0));
4262 return valist;
4265 /* The "standard" definition of va_list is void*. */
4267 tree
4268 std_build_builtin_va_list (void)
4270 return ptr_type_node;
4273 /* The "standard" abi va_list is va_list_type_node. */
4275 tree
4276 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4278 return va_list_type_node;
4281 /* The "standard" type of va_list is va_list_type_node. */
4283 tree
4284 std_canonical_va_list_type (tree type)
4286 tree wtype, htype;
4288 if (INDIRECT_REF_P (type))
4289 type = TREE_TYPE (type);
4290 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4291 type = TREE_TYPE (type);
4292 wtype = va_list_type_node;
4293 htype = type;
4294 /* Treat structure va_list types. */
4295 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4296 htype = TREE_TYPE (htype);
4297 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4299 /* If va_list is an array type, the argument may have decayed
4300 to a pointer type, e.g. by being passed to another function.
4301 In that case, unwrap both types so that we can compare the
4302 underlying records. */
4303 if (TREE_CODE (htype) == ARRAY_TYPE
4304 || POINTER_TYPE_P (htype))
4306 wtype = TREE_TYPE (wtype);
4307 htype = TREE_TYPE (htype);
4310 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4311 return va_list_type_node;
4313 return NULL_TREE;
4316 /* The "standard" implementation of va_start: just assign `nextarg' to
4317 the variable. */
4319 void
4320 std_expand_builtin_va_start (tree valist, rtx nextarg)
4322 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4323 convert_move (va_r, nextarg, 0);
4326 /* Expand EXP, a call to __builtin_va_start. */
4328 static rtx
4329 expand_builtin_va_start (tree exp)
4331 rtx nextarg;
4332 tree valist;
4333 location_t loc = EXPR_LOCATION (exp);
4335 if (call_expr_nargs (exp) < 2)
4337 error_at (loc, "too few arguments to function %<va_start%>");
4338 return const0_rtx;
4341 if (fold_builtin_next_arg (exp, true))
4342 return const0_rtx;
4344 nextarg = expand_builtin_next_arg ();
4345 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4347 if (targetm.expand_builtin_va_start)
4348 targetm.expand_builtin_va_start (valist, nextarg);
4349 else
4350 std_expand_builtin_va_start (valist, nextarg);
4352 return const0_rtx;
4355 /* Expand EXP, a call to __builtin_va_end. */
4357 static rtx
4358 expand_builtin_va_end (tree exp)
4360 tree valist = CALL_EXPR_ARG (exp, 0);
4362 /* Evaluate for side effects, if needed. I hate macros that don't
4363 do that. */
4364 if (TREE_SIDE_EFFECTS (valist))
4365 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4367 return const0_rtx;
4370 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4371 builtin rather than just as an assignment in stdarg.h because of the
4372 nastiness of array-type va_list types. */
4374 static rtx
4375 expand_builtin_va_copy (tree exp)
4377 tree dst, src, t;
4378 location_t loc = EXPR_LOCATION (exp);
4380 dst = CALL_EXPR_ARG (exp, 0);
4381 src = CALL_EXPR_ARG (exp, 1);
4383 dst = stabilize_va_list_loc (loc, dst, 1);
4384 src = stabilize_va_list_loc (loc, src, 0);
4386 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4388 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4390 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4391 TREE_SIDE_EFFECTS (t) = 1;
4392 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4394 else
4396 rtx dstb, srcb, size;
4398 /* Evaluate to pointers. */
4399 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4400 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4401 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4402 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4404 dstb = convert_memory_address (Pmode, dstb);
4405 srcb = convert_memory_address (Pmode, srcb);
4407 /* "Dereference" to BLKmode memories. */
4408 dstb = gen_rtx_MEM (BLKmode, dstb);
4409 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4410 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4411 srcb = gen_rtx_MEM (BLKmode, srcb);
4412 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4413 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4415 /* Copy. */
4416 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4419 return const0_rtx;
4422 /* Expand a call to one of the builtin functions __builtin_frame_address or
4423 __builtin_return_address. */
4425 static rtx
4426 expand_builtin_frame_address (tree fndecl, tree exp)
4428 /* The argument must be a nonnegative integer constant.
4429 It counts the number of frames to scan up the stack.
4430 The value is the return address saved in that frame. */
4431 if (call_expr_nargs (exp) == 0)
4432 /* Warning about missing arg was already issued. */
4433 return const0_rtx;
4434 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4436 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4437 error ("invalid argument to %<__builtin_frame_address%>");
4438 else
4439 error ("invalid argument to %<__builtin_return_address%>");
4440 return const0_rtx;
4442 else
4444 rtx tem
4445 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4446 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4448 /* Some ports cannot access arbitrary stack frames. */
4449 if (tem == NULL)
4451 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4452 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4453 else
4454 warning (0, "unsupported argument to %<__builtin_return_address%>");
4455 return const0_rtx;
4458 /* For __builtin_frame_address, return what we've got. */
4459 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4460 return tem;
4462 if (!REG_P (tem)
4463 && ! CONSTANT_P (tem))
4464 tem = copy_addr_to_reg (tem);
4465 return tem;
4469 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4470 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4471 is the same as for allocate_dynamic_stack_space. */
4473 static rtx
4474 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4476 rtx op0;
4477 rtx result;
4478 bool valid_arglist;
4479 unsigned int align;
4480 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4481 == BUILT_IN_ALLOCA_WITH_ALIGN);
4483 valid_arglist
4484 = (alloca_with_align
4485 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4486 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4488 if (!valid_arglist)
4489 return NULL_RTX;
4491 /* Compute the argument. */
4492 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4494 /* Compute the alignment. */
4495 align = (alloca_with_align
4496 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4497 : BIGGEST_ALIGNMENT);
4499 /* Allocate the desired space. */
4500 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4501 result = convert_memory_address (ptr_mode, result);
4503 return result;
4506 /* Expand a call to bswap builtin in EXP.
4507 Return NULL_RTX if a normal call should be emitted rather than expanding the
4508 function in-line. If convenient, the result should be placed in TARGET.
4509 SUBTARGET may be used as the target for computing one of EXP's operands. */
4511 static rtx
4512 expand_builtin_bswap (enum machine_mode target_mode, tree exp, rtx target,
4513 rtx subtarget)
4515 tree arg;
4516 rtx op0;
4518 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4519 return NULL_RTX;
4521 arg = CALL_EXPR_ARG (exp, 0);
4522 op0 = expand_expr (arg,
4523 subtarget && GET_MODE (subtarget) == target_mode
4524 ? subtarget : NULL_RTX,
4525 target_mode, EXPAND_NORMAL);
4526 if (GET_MODE (op0) != target_mode)
4527 op0 = convert_to_mode (target_mode, op0, 1);
4529 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4531 gcc_assert (target);
4533 return convert_to_mode (target_mode, target, 1);
4536 /* Expand a call to a unary builtin in EXP.
4537 Return NULL_RTX if a normal call should be emitted rather than expanding the
4538 function in-line. If convenient, the result should be placed in TARGET.
4539 SUBTARGET may be used as the target for computing one of EXP's operands. */
4541 static rtx
4542 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4543 rtx subtarget, optab op_optab)
4545 rtx op0;
4547 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4548 return NULL_RTX;
4550 /* Compute the argument. */
4551 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4552 (subtarget
4553 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4554 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4555 VOIDmode, EXPAND_NORMAL);
4556 /* Compute op, into TARGET if possible.
4557 Set TARGET to wherever the result comes back. */
4558 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4559 op_optab, op0, target, op_optab != clrsb_optab);
4560 gcc_assert (target);
4562 return convert_to_mode (target_mode, target, 0);
4565 /* Expand a call to __builtin_expect. We just return our argument
4566 as the builtin_expect semantic should've been already executed by
4567 tree branch prediction pass. */
4569 static rtx
4570 expand_builtin_expect (tree exp, rtx target)
4572 tree arg;
4574 if (call_expr_nargs (exp) < 2)
4575 return const0_rtx;
4576 arg = CALL_EXPR_ARG (exp, 0);
4578 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4579 /* When guessing was done, the hints should be already stripped away. */
4580 gcc_assert (!flag_guess_branch_prob
4581 || optimize == 0 || seen_error ());
4582 return target;
4585 /* Expand a call to __builtin_assume_aligned. We just return our first
4586 argument as the builtin_assume_aligned semantic should've been already
4587 executed by CCP. */
4589 static rtx
4590 expand_builtin_assume_aligned (tree exp, rtx target)
4592 if (call_expr_nargs (exp) < 2)
4593 return const0_rtx;
4594 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4595 EXPAND_NORMAL);
4596 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4597 && (call_expr_nargs (exp) < 3
4598 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4599 return target;
4602 void
4603 expand_builtin_trap (void)
4605 #ifdef HAVE_trap
4606 if (HAVE_trap)
4608 rtx insn = emit_insn (gen_trap ());
4609 /* For trap insns when not accumulating outgoing args force
4610 REG_ARGS_SIZE note to prevent crossjumping of calls with
4611 different args sizes. */
4612 if (!ACCUMULATE_OUTGOING_ARGS)
4613 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4615 else
4616 #endif
4617 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4618 emit_barrier ();
4621 /* Expand a call to __builtin_unreachable. We do nothing except emit
4622 a barrier saying that control flow will not pass here.
4624 It is the responsibility of the program being compiled to ensure
4625 that control flow does never reach __builtin_unreachable. */
4626 static void
4627 expand_builtin_unreachable (void)
4629 emit_barrier ();
4632 /* Expand EXP, a call to fabs, fabsf or fabsl.
4633 Return NULL_RTX if a normal call should be emitted rather than expanding
4634 the function inline. If convenient, the result should be placed
4635 in TARGET. SUBTARGET may be used as the target for computing
4636 the operand. */
4638 static rtx
4639 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4641 enum machine_mode mode;
4642 tree arg;
4643 rtx op0;
4645 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4646 return NULL_RTX;
4648 arg = CALL_EXPR_ARG (exp, 0);
4649 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4650 mode = TYPE_MODE (TREE_TYPE (arg));
4651 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4652 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4655 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4656 Return NULL is a normal call should be emitted rather than expanding the
4657 function inline. If convenient, the result should be placed in TARGET.
4658 SUBTARGET may be used as the target for computing the operand. */
4660 static rtx
4661 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4663 rtx op0, op1;
4664 tree arg;
4666 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4667 return NULL_RTX;
4669 arg = CALL_EXPR_ARG (exp, 0);
4670 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4672 arg = CALL_EXPR_ARG (exp, 1);
4673 op1 = expand_normal (arg);
4675 return expand_copysign (op0, op1, target);
4678 /* Expand a call to __builtin___clear_cache. */
4680 static rtx
4681 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4683 #ifndef HAVE_clear_cache
4684 #ifdef CLEAR_INSN_CACHE
4685 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4686 does something. Just do the default expansion to a call to
4687 __clear_cache(). */
4688 return NULL_RTX;
4689 #else
4690 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4691 does nothing. There is no need to call it. Do nothing. */
4692 return const0_rtx;
4693 #endif /* CLEAR_INSN_CACHE */
4694 #else
4695 /* We have a "clear_cache" insn, and it will handle everything. */
4696 tree begin, end;
4697 rtx begin_rtx, end_rtx;
4699 /* We must not expand to a library call. If we did, any
4700 fallback library function in libgcc that might contain a call to
4701 __builtin___clear_cache() would recurse infinitely. */
4702 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4704 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4705 return const0_rtx;
4708 if (HAVE_clear_cache)
4710 struct expand_operand ops[2];
4712 begin = CALL_EXPR_ARG (exp, 0);
4713 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4715 end = CALL_EXPR_ARG (exp, 1);
4716 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4718 create_address_operand (&ops[0], begin_rtx);
4719 create_address_operand (&ops[1], end_rtx);
4720 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4721 return const0_rtx;
4723 return const0_rtx;
4724 #endif /* HAVE_clear_cache */
4727 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4729 static rtx
4730 round_trampoline_addr (rtx tramp)
4732 rtx temp, addend, mask;
4734 /* If we don't need too much alignment, we'll have been guaranteed
4735 proper alignment by get_trampoline_type. */
4736 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4737 return tramp;
4739 /* Round address up to desired boundary. */
4740 temp = gen_reg_rtx (Pmode);
4741 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4742 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4744 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4745 temp, 0, OPTAB_LIB_WIDEN);
4746 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4747 temp, 0, OPTAB_LIB_WIDEN);
4749 return tramp;
4752 static rtx
4753 expand_builtin_init_trampoline (tree exp, bool onstack)
4755 tree t_tramp, t_func, t_chain;
4756 rtx m_tramp, r_tramp, r_chain, tmp;
4758 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4759 POINTER_TYPE, VOID_TYPE))
4760 return NULL_RTX;
4762 t_tramp = CALL_EXPR_ARG (exp, 0);
4763 t_func = CALL_EXPR_ARG (exp, 1);
4764 t_chain = CALL_EXPR_ARG (exp, 2);
4766 r_tramp = expand_normal (t_tramp);
4767 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4768 MEM_NOTRAP_P (m_tramp) = 1;
4770 /* If ONSTACK, the TRAMP argument should be the address of a field
4771 within the local function's FRAME decl. Either way, let's see if
4772 we can fill in the MEM_ATTRs for this memory. */
4773 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4774 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4776 /* Creator of a heap trampoline is responsible for making sure the
4777 address is aligned to at least STACK_BOUNDARY. Normally malloc
4778 will ensure this anyhow. */
4779 tmp = round_trampoline_addr (r_tramp);
4780 if (tmp != r_tramp)
4782 m_tramp = change_address (m_tramp, BLKmode, tmp);
4783 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4784 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4787 /* The FUNC argument should be the address of the nested function.
4788 Extract the actual function decl to pass to the hook. */
4789 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4790 t_func = TREE_OPERAND (t_func, 0);
4791 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4793 r_chain = expand_normal (t_chain);
4795 /* Generate insns to initialize the trampoline. */
4796 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4798 if (onstack)
4800 trampolines_created = 1;
4802 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4803 "trampoline generated for nested function %qD", t_func);
4806 return const0_rtx;
4809 static rtx
4810 expand_builtin_adjust_trampoline (tree exp)
4812 rtx tramp;
4814 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4815 return NULL_RTX;
4817 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4818 tramp = round_trampoline_addr (tramp);
4819 if (targetm.calls.trampoline_adjust_address)
4820 tramp = targetm.calls.trampoline_adjust_address (tramp);
4822 return tramp;
4825 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4826 function. The function first checks whether the back end provides
4827 an insn to implement signbit for the respective mode. If not, it
4828 checks whether the floating point format of the value is such that
4829 the sign bit can be extracted. If that is not the case, the
4830 function returns NULL_RTX to indicate that a normal call should be
4831 emitted rather than expanding the function in-line. EXP is the
4832 expression that is a call to the builtin function; if convenient,
4833 the result should be placed in TARGET. */
4834 static rtx
4835 expand_builtin_signbit (tree exp, rtx target)
4837 const struct real_format *fmt;
4838 enum machine_mode fmode, imode, rmode;
4839 tree arg;
4840 int word, bitpos;
4841 enum insn_code icode;
4842 rtx temp;
4843 location_t loc = EXPR_LOCATION (exp);
4845 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4846 return NULL_RTX;
4848 arg = CALL_EXPR_ARG (exp, 0);
4849 fmode = TYPE_MODE (TREE_TYPE (arg));
4850 rmode = TYPE_MODE (TREE_TYPE (exp));
4851 fmt = REAL_MODE_FORMAT (fmode);
4853 arg = builtin_save_expr (arg);
4855 /* Expand the argument yielding a RTX expression. */
4856 temp = expand_normal (arg);
4858 /* Check if the back end provides an insn that handles signbit for the
4859 argument's mode. */
4860 icode = optab_handler (signbit_optab, fmode);
4861 if (icode != CODE_FOR_nothing)
4863 rtx_insn *last = get_last_insn ();
4864 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4865 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4866 return target;
4867 delete_insns_since (last);
4870 /* For floating point formats without a sign bit, implement signbit
4871 as "ARG < 0.0". */
4872 bitpos = fmt->signbit_ro;
4873 if (bitpos < 0)
4875 /* But we can't do this if the format supports signed zero. */
4876 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4877 return NULL_RTX;
4879 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4880 build_real (TREE_TYPE (arg), dconst0));
4881 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4884 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4886 imode = int_mode_for_mode (fmode);
4887 if (imode == BLKmode)
4888 return NULL_RTX;
4889 temp = gen_lowpart (imode, temp);
4891 else
4893 imode = word_mode;
4894 /* Handle targets with different FP word orders. */
4895 if (FLOAT_WORDS_BIG_ENDIAN)
4896 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4897 else
4898 word = bitpos / BITS_PER_WORD;
4899 temp = operand_subword_force (temp, word, fmode);
4900 bitpos = bitpos % BITS_PER_WORD;
4903 /* Force the intermediate word_mode (or narrower) result into a
4904 register. This avoids attempting to create paradoxical SUBREGs
4905 of floating point modes below. */
4906 temp = force_reg (imode, temp);
4908 /* If the bitpos is within the "result mode" lowpart, the operation
4909 can be implement with a single bitwise AND. Otherwise, we need
4910 a right shift and an AND. */
4912 if (bitpos < GET_MODE_BITSIZE (rmode))
4914 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
4916 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4917 temp = gen_lowpart (rmode, temp);
4918 temp = expand_binop (rmode, and_optab, temp,
4919 immed_wide_int_const (mask, rmode),
4920 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4922 else
4924 /* Perform a logical right shift to place the signbit in the least
4925 significant bit, then truncate the result to the desired mode
4926 and mask just this bit. */
4927 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4928 temp = gen_lowpart (rmode, temp);
4929 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4930 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4933 return temp;
4936 /* Expand fork or exec calls. TARGET is the desired target of the
4937 call. EXP is the call. FN is the
4938 identificator of the actual function. IGNORE is nonzero if the
4939 value is to be ignored. */
4941 static rtx
4942 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4944 tree id, decl;
4945 tree call;
4947 /* If we are not profiling, just call the function. */
4948 if (!profile_arc_flag)
4949 return NULL_RTX;
4951 /* Otherwise call the wrapper. This should be equivalent for the rest of
4952 compiler, so the code does not diverge, and the wrapper may run the
4953 code necessary for keeping the profiling sane. */
4955 switch (DECL_FUNCTION_CODE (fn))
4957 case BUILT_IN_FORK:
4958 id = get_identifier ("__gcov_fork");
4959 break;
4961 case BUILT_IN_EXECL:
4962 id = get_identifier ("__gcov_execl");
4963 break;
4965 case BUILT_IN_EXECV:
4966 id = get_identifier ("__gcov_execv");
4967 break;
4969 case BUILT_IN_EXECLP:
4970 id = get_identifier ("__gcov_execlp");
4971 break;
4973 case BUILT_IN_EXECLE:
4974 id = get_identifier ("__gcov_execle");
4975 break;
4977 case BUILT_IN_EXECVP:
4978 id = get_identifier ("__gcov_execvp");
4979 break;
4981 case BUILT_IN_EXECVE:
4982 id = get_identifier ("__gcov_execve");
4983 break;
4985 default:
4986 gcc_unreachable ();
4989 decl = build_decl (DECL_SOURCE_LOCATION (fn),
4990 FUNCTION_DECL, id, TREE_TYPE (fn));
4991 DECL_EXTERNAL (decl) = 1;
4992 TREE_PUBLIC (decl) = 1;
4993 DECL_ARTIFICIAL (decl) = 1;
4994 TREE_NOTHROW (decl) = 1;
4995 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4996 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4997 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
4998 return expand_call (call, target, ignore);
5003 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5004 the pointer in these functions is void*, the tree optimizers may remove
5005 casts. The mode computed in expand_builtin isn't reliable either, due
5006 to __sync_bool_compare_and_swap.
5008 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5009 group of builtins. This gives us log2 of the mode size. */
5011 static inline enum machine_mode
5012 get_builtin_sync_mode (int fcode_diff)
5014 /* The size is not negotiable, so ask not to get BLKmode in return
5015 if the target indicates that a smaller size would be better. */
5016 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5019 /* Expand the memory expression LOC and return the appropriate memory operand
5020 for the builtin_sync operations. */
5022 static rtx
5023 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5025 rtx addr, mem;
5027 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5028 addr = convert_memory_address (Pmode, addr);
5030 /* Note that we explicitly do not want any alias information for this
5031 memory, so that we kill all other live memories. Otherwise we don't
5032 satisfy the full barrier semantics of the intrinsic. */
5033 mem = validize_mem (gen_rtx_MEM (mode, addr));
5035 /* The alignment needs to be at least according to that of the mode. */
5036 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5037 get_pointer_alignment (loc)));
5038 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5039 MEM_VOLATILE_P (mem) = 1;
5041 return mem;
5044 /* Make sure an argument is in the right mode.
5045 EXP is the tree argument.
5046 MODE is the mode it should be in. */
5048 static rtx
5049 expand_expr_force_mode (tree exp, enum machine_mode mode)
5051 rtx val;
5052 enum machine_mode old_mode;
5054 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5055 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5056 of CONST_INTs, where we know the old_mode only from the call argument. */
5058 old_mode = GET_MODE (val);
5059 if (old_mode == VOIDmode)
5060 old_mode = TYPE_MODE (TREE_TYPE (exp));
5061 val = convert_modes (mode, old_mode, val, 1);
5062 return val;
5066 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5067 EXP is the CALL_EXPR. CODE is the rtx code
5068 that corresponds to the arithmetic or logical operation from the name;
5069 an exception here is that NOT actually means NAND. TARGET is an optional
5070 place for us to store the results; AFTER is true if this is the
5071 fetch_and_xxx form. */
5073 static rtx
5074 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5075 enum rtx_code code, bool after,
5076 rtx target)
5078 rtx val, mem;
5079 location_t loc = EXPR_LOCATION (exp);
5081 if (code == NOT && warn_sync_nand)
5083 tree fndecl = get_callee_fndecl (exp);
5084 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5086 static bool warned_f_a_n, warned_n_a_f;
5088 switch (fcode)
5090 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5091 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5092 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5093 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5094 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5095 if (warned_f_a_n)
5096 break;
5098 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5099 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5100 warned_f_a_n = true;
5101 break;
5103 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5104 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5105 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5106 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5107 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5108 if (warned_n_a_f)
5109 break;
5111 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5112 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5113 warned_n_a_f = true;
5114 break;
5116 default:
5117 gcc_unreachable ();
5121 /* Expand the operands. */
5122 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5123 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5125 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5126 after);
5129 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5130 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5131 true if this is the boolean form. TARGET is a place for us to store the
5132 results; this is NOT optional if IS_BOOL is true. */
5134 static rtx
5135 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5136 bool is_bool, rtx target)
5138 rtx old_val, new_val, mem;
5139 rtx *pbool, *poval;
5141 /* Expand the operands. */
5142 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5143 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5144 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5146 pbool = poval = NULL;
5147 if (target != const0_rtx)
5149 if (is_bool)
5150 pbool = &target;
5151 else
5152 poval = &target;
5154 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5155 false, MEMMODEL_SEQ_CST,
5156 MEMMODEL_SEQ_CST))
5157 return NULL_RTX;
5159 return target;
5162 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5163 general form is actually an atomic exchange, and some targets only
5164 support a reduced form with the second argument being a constant 1.
5165 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5166 the results. */
5168 static rtx
5169 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5170 rtx target)
5172 rtx val, mem;
5174 /* Expand the operands. */
5175 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5176 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5178 return expand_sync_lock_test_and_set (target, mem, val);
5181 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5183 static void
5184 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5186 rtx mem;
5188 /* Expand the operands. */
5189 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5191 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5194 /* Given an integer representing an ``enum memmodel'', verify its
5195 correctness and return the memory model enum. */
5197 static enum memmodel
5198 get_memmodel (tree exp)
5200 rtx op;
5201 unsigned HOST_WIDE_INT val;
5203 /* If the parameter is not a constant, it's a run time value so we'll just
5204 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5205 if (TREE_CODE (exp) != INTEGER_CST)
5206 return MEMMODEL_SEQ_CST;
5208 op = expand_normal (exp);
5210 val = INTVAL (op);
5211 if (targetm.memmodel_check)
5212 val = targetm.memmodel_check (val);
5213 else if (val & ~MEMMODEL_MASK)
5215 warning (OPT_Winvalid_memory_model,
5216 "Unknown architecture specifier in memory model to builtin.");
5217 return MEMMODEL_SEQ_CST;
5220 if ((INTVAL (op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5222 warning (OPT_Winvalid_memory_model,
5223 "invalid memory model argument to builtin");
5224 return MEMMODEL_SEQ_CST;
5227 return (enum memmodel) val;
5230 /* Expand the __atomic_exchange intrinsic:
5231 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5232 EXP is the CALL_EXPR.
5233 TARGET is an optional place for us to store the results. */
5235 static rtx
5236 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5238 rtx val, mem;
5239 enum memmodel model;
5241 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5242 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME)
5244 error ("invalid memory model for %<__atomic_exchange%>");
5245 return NULL_RTX;
5248 if (!flag_inline_atomics)
5249 return NULL_RTX;
5251 /* Expand the operands. */
5252 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5253 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5255 return expand_atomic_exchange (target, mem, val, model);
5258 /* Expand the __atomic_compare_exchange intrinsic:
5259 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5260 TYPE desired, BOOL weak,
5261 enum memmodel success,
5262 enum memmodel failure)
5263 EXP is the CALL_EXPR.
5264 TARGET is an optional place for us to store the results. */
5266 static rtx
5267 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5268 rtx target)
5270 rtx expect, desired, mem, oldval;
5271 rtx_code_label *label;
5272 enum memmodel success, failure;
5273 tree weak;
5274 bool is_weak;
5276 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5277 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5279 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5280 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5282 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5283 return NULL_RTX;
5286 if (failure > success)
5288 error ("failure memory model cannot be stronger than success "
5289 "memory model for %<__atomic_compare_exchange%>");
5290 return NULL_RTX;
5293 if (!flag_inline_atomics)
5294 return NULL_RTX;
5296 /* Expand the operands. */
5297 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5299 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5300 expect = convert_memory_address (Pmode, expect);
5301 expect = gen_rtx_MEM (mode, expect);
5302 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5304 weak = CALL_EXPR_ARG (exp, 3);
5305 is_weak = false;
5306 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5307 is_weak = true;
5309 if (target == const0_rtx)
5310 target = NULL;
5312 /* Lest the rtl backend create a race condition with an imporoper store
5313 to memory, always create a new pseudo for OLDVAL. */
5314 oldval = NULL;
5316 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5317 is_weak, success, failure))
5318 return NULL_RTX;
5320 /* Conditionally store back to EXPECT, lest we create a race condition
5321 with an improper store to memory. */
5322 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5323 the normal case where EXPECT is totally private, i.e. a register. At
5324 which point the store can be unconditional. */
5325 label = gen_label_rtx ();
5326 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL, VOIDmode, 1, label);
5327 emit_move_insn (expect, oldval);
5328 emit_label (label);
5330 return target;
5333 /* Expand the __atomic_load intrinsic:
5334 TYPE __atomic_load (TYPE *object, enum memmodel)
5335 EXP is the CALL_EXPR.
5336 TARGET is an optional place for us to store the results. */
5338 static rtx
5339 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5341 rtx mem;
5342 enum memmodel model;
5344 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5345 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5346 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5348 error ("invalid memory model for %<__atomic_load%>");
5349 return NULL_RTX;
5352 if (!flag_inline_atomics)
5353 return NULL_RTX;
5355 /* Expand the operand. */
5356 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5358 return expand_atomic_load (target, mem, model);
5362 /* Expand the __atomic_store intrinsic:
5363 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5364 EXP is the CALL_EXPR.
5365 TARGET is an optional place for us to store the results. */
5367 static rtx
5368 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5370 rtx mem, val;
5371 enum memmodel model;
5373 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5374 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5375 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5376 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5378 error ("invalid memory model for %<__atomic_store%>");
5379 return NULL_RTX;
5382 if (!flag_inline_atomics)
5383 return NULL_RTX;
5385 /* Expand the operands. */
5386 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5387 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5389 return expand_atomic_store (mem, val, model, false);
5392 /* Expand the __atomic_fetch_XXX intrinsic:
5393 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5394 EXP is the CALL_EXPR.
5395 TARGET is an optional place for us to store the results.
5396 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5397 FETCH_AFTER is true if returning the result of the operation.
5398 FETCH_AFTER is false if returning the value before the operation.
5399 IGNORE is true if the result is not used.
5400 EXT_CALL is the correct builtin for an external call if this cannot be
5401 resolved to an instruction sequence. */
5403 static rtx
5404 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5405 enum rtx_code code, bool fetch_after,
5406 bool ignore, enum built_in_function ext_call)
5408 rtx val, mem, ret;
5409 enum memmodel model;
5410 tree fndecl;
5411 tree addr;
5413 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5415 /* Expand the operands. */
5416 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5417 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5419 /* Only try generating instructions if inlining is turned on. */
5420 if (flag_inline_atomics)
5422 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5423 if (ret)
5424 return ret;
5427 /* Return if a different routine isn't needed for the library call. */
5428 if (ext_call == BUILT_IN_NONE)
5429 return NULL_RTX;
5431 /* Change the call to the specified function. */
5432 fndecl = get_callee_fndecl (exp);
5433 addr = CALL_EXPR_FN (exp);
5434 STRIP_NOPS (addr);
5436 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5437 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5439 /* Expand the call here so we can emit trailing code. */
5440 ret = expand_call (exp, target, ignore);
5442 /* Replace the original function just in case it matters. */
5443 TREE_OPERAND (addr, 0) = fndecl;
5445 /* Then issue the arithmetic correction to return the right result. */
5446 if (!ignore)
5448 if (code == NOT)
5450 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5451 OPTAB_LIB_WIDEN);
5452 ret = expand_simple_unop (mode, NOT, ret, target, true);
5454 else
5455 ret = expand_simple_binop (mode, code, ret, val, target, true,
5456 OPTAB_LIB_WIDEN);
5458 return ret;
5462 #ifndef HAVE_atomic_clear
5463 # define HAVE_atomic_clear 0
5464 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5465 #endif
5467 /* Expand an atomic clear operation.
5468 void _atomic_clear (BOOL *obj, enum memmodel)
5469 EXP is the call expression. */
5471 static rtx
5472 expand_builtin_atomic_clear (tree exp)
5474 enum machine_mode mode;
5475 rtx mem, ret;
5476 enum memmodel model;
5478 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5479 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5480 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5482 if ((model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5483 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5485 error ("invalid memory model for %<__atomic_store%>");
5486 return const0_rtx;
5489 if (HAVE_atomic_clear)
5491 emit_insn (gen_atomic_clear (mem, model));
5492 return const0_rtx;
5495 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5496 Failing that, a store is issued by __atomic_store. The only way this can
5497 fail is if the bool type is larger than a word size. Unlikely, but
5498 handle it anyway for completeness. Assume a single threaded model since
5499 there is no atomic support in this case, and no barriers are required. */
5500 ret = expand_atomic_store (mem, const0_rtx, model, true);
5501 if (!ret)
5502 emit_move_insn (mem, const0_rtx);
5503 return const0_rtx;
5506 /* Expand an atomic test_and_set operation.
5507 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5508 EXP is the call expression. */
5510 static rtx
5511 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5513 rtx mem;
5514 enum memmodel model;
5515 enum machine_mode mode;
5517 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5518 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5519 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5521 return expand_atomic_test_and_set (target, mem, model);
5525 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5526 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5528 static tree
5529 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5531 int size;
5532 enum machine_mode mode;
5533 unsigned int mode_align, type_align;
5535 if (TREE_CODE (arg0) != INTEGER_CST)
5536 return NULL_TREE;
5538 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5539 mode = mode_for_size (size, MODE_INT, 0);
5540 mode_align = GET_MODE_ALIGNMENT (mode);
5542 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5543 type_align = mode_align;
5544 else
5546 tree ttype = TREE_TYPE (arg1);
5548 /* This function is usually invoked and folded immediately by the front
5549 end before anything else has a chance to look at it. The pointer
5550 parameter at this point is usually cast to a void *, so check for that
5551 and look past the cast. */
5552 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5553 && VOID_TYPE_P (TREE_TYPE (ttype)))
5554 arg1 = TREE_OPERAND (arg1, 0);
5556 ttype = TREE_TYPE (arg1);
5557 gcc_assert (POINTER_TYPE_P (ttype));
5559 /* Get the underlying type of the object. */
5560 ttype = TREE_TYPE (ttype);
5561 type_align = TYPE_ALIGN (ttype);
5564 /* If the object has smaller alignment, the the lock free routines cannot
5565 be used. */
5566 if (type_align < mode_align)
5567 return boolean_false_node;
5569 /* Check if a compare_and_swap pattern exists for the mode which represents
5570 the required size. The pattern is not allowed to fail, so the existence
5571 of the pattern indicates support is present. */
5572 if (can_compare_and_swap_p (mode, true))
5573 return boolean_true_node;
5574 else
5575 return boolean_false_node;
5578 /* Return true if the parameters to call EXP represent an object which will
5579 always generate lock free instructions. The first argument represents the
5580 size of the object, and the second parameter is a pointer to the object
5581 itself. If NULL is passed for the object, then the result is based on
5582 typical alignment for an object of the specified size. Otherwise return
5583 false. */
5585 static rtx
5586 expand_builtin_atomic_always_lock_free (tree exp)
5588 tree size;
5589 tree arg0 = CALL_EXPR_ARG (exp, 0);
5590 tree arg1 = CALL_EXPR_ARG (exp, 1);
5592 if (TREE_CODE (arg0) != INTEGER_CST)
5594 error ("non-constant argument 1 to __atomic_always_lock_free");
5595 return const0_rtx;
5598 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5599 if (size == boolean_true_node)
5600 return const1_rtx;
5601 return const0_rtx;
5604 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5605 is lock free on this architecture. */
5607 static tree
5608 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5610 if (!flag_inline_atomics)
5611 return NULL_TREE;
5613 /* If it isn't always lock free, don't generate a result. */
5614 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5615 return boolean_true_node;
5617 return NULL_TREE;
5620 /* Return true if the parameters to call EXP represent an object which will
5621 always generate lock free instructions. The first argument represents the
5622 size of the object, and the second parameter is a pointer to the object
5623 itself. If NULL is passed for the object, then the result is based on
5624 typical alignment for an object of the specified size. Otherwise return
5625 NULL*/
5627 static rtx
5628 expand_builtin_atomic_is_lock_free (tree exp)
5630 tree size;
5631 tree arg0 = CALL_EXPR_ARG (exp, 0);
5632 tree arg1 = CALL_EXPR_ARG (exp, 1);
5634 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5636 error ("non-integer argument 1 to __atomic_is_lock_free");
5637 return NULL_RTX;
5640 if (!flag_inline_atomics)
5641 return NULL_RTX;
5643 /* If the value is known at compile time, return the RTX for it. */
5644 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5645 if (size == boolean_true_node)
5646 return const1_rtx;
5648 return NULL_RTX;
5651 /* Expand the __atomic_thread_fence intrinsic:
5652 void __atomic_thread_fence (enum memmodel)
5653 EXP is the CALL_EXPR. */
5655 static void
5656 expand_builtin_atomic_thread_fence (tree exp)
5658 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5659 expand_mem_thread_fence (model);
5662 /* Expand the __atomic_signal_fence intrinsic:
5663 void __atomic_signal_fence (enum memmodel)
5664 EXP is the CALL_EXPR. */
5666 static void
5667 expand_builtin_atomic_signal_fence (tree exp)
5669 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5670 expand_mem_signal_fence (model);
5673 /* Expand the __sync_synchronize intrinsic. */
5675 static void
5676 expand_builtin_sync_synchronize (void)
5678 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5681 static rtx
5682 expand_builtin_thread_pointer (tree exp, rtx target)
5684 enum insn_code icode;
5685 if (!validate_arglist (exp, VOID_TYPE))
5686 return const0_rtx;
5687 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5688 if (icode != CODE_FOR_nothing)
5690 struct expand_operand op;
5691 /* If the target is not sutitable then create a new target. */
5692 if (target == NULL_RTX
5693 || !REG_P (target)
5694 || GET_MODE (target) != Pmode)
5695 target = gen_reg_rtx (Pmode);
5696 create_output_operand (&op, target, Pmode);
5697 expand_insn (icode, 1, &op);
5698 return target;
5700 error ("__builtin_thread_pointer is not supported on this target");
5701 return const0_rtx;
5704 static void
5705 expand_builtin_set_thread_pointer (tree exp)
5707 enum insn_code icode;
5708 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5709 return;
5710 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5711 if (icode != CODE_FOR_nothing)
5713 struct expand_operand op;
5714 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5715 Pmode, EXPAND_NORMAL);
5716 create_input_operand (&op, val, Pmode);
5717 expand_insn (icode, 1, &op);
5718 return;
5720 error ("__builtin_set_thread_pointer is not supported on this target");
5724 /* Emit code to restore the current value of stack. */
5726 static void
5727 expand_stack_restore (tree var)
5729 rtx_insn *prev;
5730 rtx sa = expand_normal (var);
5732 sa = convert_memory_address (Pmode, sa);
5734 prev = get_last_insn ();
5735 emit_stack_restore (SAVE_BLOCK, sa);
5736 fixup_args_size_notes (prev, get_last_insn (), 0);
5740 /* Emit code to save the current value of stack. */
5742 static rtx
5743 expand_stack_save (void)
5745 rtx ret = NULL_RTX;
5747 do_pending_stack_adjust ();
5748 emit_stack_save (SAVE_BLOCK, &ret);
5749 return ret;
5752 /* Expand an expression EXP that calls a built-in function,
5753 with result going to TARGET if that's convenient
5754 (and in mode MODE if that's convenient).
5755 SUBTARGET may be used as the target for computing one of EXP's operands.
5756 IGNORE is nonzero if the value is to be ignored. */
5759 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5760 int ignore)
5762 tree fndecl = get_callee_fndecl (exp);
5763 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5764 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5765 int flags;
5767 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5768 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5770 /* When not optimizing, generate calls to library functions for a certain
5771 set of builtins. */
5772 if (!optimize
5773 && !called_as_built_in (fndecl)
5774 && fcode != BUILT_IN_FORK
5775 && fcode != BUILT_IN_EXECL
5776 && fcode != BUILT_IN_EXECV
5777 && fcode != BUILT_IN_EXECLP
5778 && fcode != BUILT_IN_EXECLE
5779 && fcode != BUILT_IN_EXECVP
5780 && fcode != BUILT_IN_EXECVE
5781 && fcode != BUILT_IN_ALLOCA
5782 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5783 && fcode != BUILT_IN_FREE)
5784 return expand_call (exp, target, ignore);
5786 /* The built-in function expanders test for target == const0_rtx
5787 to determine whether the function's result will be ignored. */
5788 if (ignore)
5789 target = const0_rtx;
5791 /* If the result of a pure or const built-in function is ignored, and
5792 none of its arguments are volatile, we can avoid expanding the
5793 built-in call and just evaluate the arguments for side-effects. */
5794 if (target == const0_rtx
5795 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5796 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5798 bool volatilep = false;
5799 tree arg;
5800 call_expr_arg_iterator iter;
5802 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5803 if (TREE_THIS_VOLATILE (arg))
5805 volatilep = true;
5806 break;
5809 if (! volatilep)
5811 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5812 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5813 return const0_rtx;
5817 switch (fcode)
5819 CASE_FLT_FN (BUILT_IN_FABS):
5820 case BUILT_IN_FABSD32:
5821 case BUILT_IN_FABSD64:
5822 case BUILT_IN_FABSD128:
5823 target = expand_builtin_fabs (exp, target, subtarget);
5824 if (target)
5825 return target;
5826 break;
5828 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5829 target = expand_builtin_copysign (exp, target, subtarget);
5830 if (target)
5831 return target;
5832 break;
5834 /* Just do a normal library call if we were unable to fold
5835 the values. */
5836 CASE_FLT_FN (BUILT_IN_CABS):
5837 break;
5839 CASE_FLT_FN (BUILT_IN_EXP):
5840 CASE_FLT_FN (BUILT_IN_EXP10):
5841 CASE_FLT_FN (BUILT_IN_POW10):
5842 CASE_FLT_FN (BUILT_IN_EXP2):
5843 CASE_FLT_FN (BUILT_IN_EXPM1):
5844 CASE_FLT_FN (BUILT_IN_LOGB):
5845 CASE_FLT_FN (BUILT_IN_LOG):
5846 CASE_FLT_FN (BUILT_IN_LOG10):
5847 CASE_FLT_FN (BUILT_IN_LOG2):
5848 CASE_FLT_FN (BUILT_IN_LOG1P):
5849 CASE_FLT_FN (BUILT_IN_TAN):
5850 CASE_FLT_FN (BUILT_IN_ASIN):
5851 CASE_FLT_FN (BUILT_IN_ACOS):
5852 CASE_FLT_FN (BUILT_IN_ATAN):
5853 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5854 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5855 because of possible accuracy problems. */
5856 if (! flag_unsafe_math_optimizations)
5857 break;
5858 CASE_FLT_FN (BUILT_IN_SQRT):
5859 CASE_FLT_FN (BUILT_IN_FLOOR):
5860 CASE_FLT_FN (BUILT_IN_CEIL):
5861 CASE_FLT_FN (BUILT_IN_TRUNC):
5862 CASE_FLT_FN (BUILT_IN_ROUND):
5863 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5864 CASE_FLT_FN (BUILT_IN_RINT):
5865 target = expand_builtin_mathfn (exp, target, subtarget);
5866 if (target)
5867 return target;
5868 break;
5870 CASE_FLT_FN (BUILT_IN_FMA):
5871 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5872 if (target)
5873 return target;
5874 break;
5876 CASE_FLT_FN (BUILT_IN_ILOGB):
5877 if (! flag_unsafe_math_optimizations)
5878 break;
5879 CASE_FLT_FN (BUILT_IN_ISINF):
5880 CASE_FLT_FN (BUILT_IN_FINITE):
5881 case BUILT_IN_ISFINITE:
5882 case BUILT_IN_ISNORMAL:
5883 target = expand_builtin_interclass_mathfn (exp, target);
5884 if (target)
5885 return target;
5886 break;
5888 CASE_FLT_FN (BUILT_IN_ICEIL):
5889 CASE_FLT_FN (BUILT_IN_LCEIL):
5890 CASE_FLT_FN (BUILT_IN_LLCEIL):
5891 CASE_FLT_FN (BUILT_IN_LFLOOR):
5892 CASE_FLT_FN (BUILT_IN_IFLOOR):
5893 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5894 target = expand_builtin_int_roundingfn (exp, target);
5895 if (target)
5896 return target;
5897 break;
5899 CASE_FLT_FN (BUILT_IN_IRINT):
5900 CASE_FLT_FN (BUILT_IN_LRINT):
5901 CASE_FLT_FN (BUILT_IN_LLRINT):
5902 CASE_FLT_FN (BUILT_IN_IROUND):
5903 CASE_FLT_FN (BUILT_IN_LROUND):
5904 CASE_FLT_FN (BUILT_IN_LLROUND):
5905 target = expand_builtin_int_roundingfn_2 (exp, target);
5906 if (target)
5907 return target;
5908 break;
5910 CASE_FLT_FN (BUILT_IN_POWI):
5911 target = expand_builtin_powi (exp, target);
5912 if (target)
5913 return target;
5914 break;
5916 CASE_FLT_FN (BUILT_IN_ATAN2):
5917 CASE_FLT_FN (BUILT_IN_LDEXP):
5918 CASE_FLT_FN (BUILT_IN_SCALB):
5919 CASE_FLT_FN (BUILT_IN_SCALBN):
5920 CASE_FLT_FN (BUILT_IN_SCALBLN):
5921 if (! flag_unsafe_math_optimizations)
5922 break;
5924 CASE_FLT_FN (BUILT_IN_FMOD):
5925 CASE_FLT_FN (BUILT_IN_REMAINDER):
5926 CASE_FLT_FN (BUILT_IN_DREM):
5927 CASE_FLT_FN (BUILT_IN_POW):
5928 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5929 if (target)
5930 return target;
5931 break;
5933 CASE_FLT_FN (BUILT_IN_CEXPI):
5934 target = expand_builtin_cexpi (exp, target);
5935 gcc_assert (target);
5936 return target;
5938 CASE_FLT_FN (BUILT_IN_SIN):
5939 CASE_FLT_FN (BUILT_IN_COS):
5940 if (! flag_unsafe_math_optimizations)
5941 break;
5942 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5943 if (target)
5944 return target;
5945 break;
5947 CASE_FLT_FN (BUILT_IN_SINCOS):
5948 if (! flag_unsafe_math_optimizations)
5949 break;
5950 target = expand_builtin_sincos (exp);
5951 if (target)
5952 return target;
5953 break;
5955 case BUILT_IN_APPLY_ARGS:
5956 return expand_builtin_apply_args ();
5958 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5959 FUNCTION with a copy of the parameters described by
5960 ARGUMENTS, and ARGSIZE. It returns a block of memory
5961 allocated on the stack into which is stored all the registers
5962 that might possibly be used for returning the result of a
5963 function. ARGUMENTS is the value returned by
5964 __builtin_apply_args. ARGSIZE is the number of bytes of
5965 arguments that must be copied. ??? How should this value be
5966 computed? We'll also need a safe worst case value for varargs
5967 functions. */
5968 case BUILT_IN_APPLY:
5969 if (!validate_arglist (exp, POINTER_TYPE,
5970 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5971 && !validate_arglist (exp, REFERENCE_TYPE,
5972 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5973 return const0_rtx;
5974 else
5976 rtx ops[3];
5978 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5979 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5980 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5982 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5985 /* __builtin_return (RESULT) causes the function to return the
5986 value described by RESULT. RESULT is address of the block of
5987 memory returned by __builtin_apply. */
5988 case BUILT_IN_RETURN:
5989 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5990 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5991 return const0_rtx;
5993 case BUILT_IN_SAVEREGS:
5994 return expand_builtin_saveregs ();
5996 case BUILT_IN_VA_ARG_PACK:
5997 /* All valid uses of __builtin_va_arg_pack () are removed during
5998 inlining. */
5999 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6000 return const0_rtx;
6002 case BUILT_IN_VA_ARG_PACK_LEN:
6003 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6004 inlining. */
6005 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6006 return const0_rtx;
6008 /* Return the address of the first anonymous stack arg. */
6009 case BUILT_IN_NEXT_ARG:
6010 if (fold_builtin_next_arg (exp, false))
6011 return const0_rtx;
6012 return expand_builtin_next_arg ();
6014 case BUILT_IN_CLEAR_CACHE:
6015 target = expand_builtin___clear_cache (exp);
6016 if (target)
6017 return target;
6018 break;
6020 case BUILT_IN_CLASSIFY_TYPE:
6021 return expand_builtin_classify_type (exp);
6023 case BUILT_IN_CONSTANT_P:
6024 return const0_rtx;
6026 case BUILT_IN_FRAME_ADDRESS:
6027 case BUILT_IN_RETURN_ADDRESS:
6028 return expand_builtin_frame_address (fndecl, exp);
6030 /* Returns the address of the area where the structure is returned.
6031 0 otherwise. */
6032 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6033 if (call_expr_nargs (exp) != 0
6034 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6035 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6036 return const0_rtx;
6037 else
6038 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6040 case BUILT_IN_ALLOCA:
6041 case BUILT_IN_ALLOCA_WITH_ALIGN:
6042 /* If the allocation stems from the declaration of a variable-sized
6043 object, it cannot accumulate. */
6044 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6045 if (target)
6046 return target;
6047 break;
6049 case BUILT_IN_STACK_SAVE:
6050 return expand_stack_save ();
6052 case BUILT_IN_STACK_RESTORE:
6053 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6054 return const0_rtx;
6056 case BUILT_IN_BSWAP16:
6057 case BUILT_IN_BSWAP32:
6058 case BUILT_IN_BSWAP64:
6059 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6060 if (target)
6061 return target;
6062 break;
6064 CASE_INT_FN (BUILT_IN_FFS):
6065 target = expand_builtin_unop (target_mode, exp, target,
6066 subtarget, ffs_optab);
6067 if (target)
6068 return target;
6069 break;
6071 CASE_INT_FN (BUILT_IN_CLZ):
6072 target = expand_builtin_unop (target_mode, exp, target,
6073 subtarget, clz_optab);
6074 if (target)
6075 return target;
6076 break;
6078 CASE_INT_FN (BUILT_IN_CTZ):
6079 target = expand_builtin_unop (target_mode, exp, target,
6080 subtarget, ctz_optab);
6081 if (target)
6082 return target;
6083 break;
6085 CASE_INT_FN (BUILT_IN_CLRSB):
6086 target = expand_builtin_unop (target_mode, exp, target,
6087 subtarget, clrsb_optab);
6088 if (target)
6089 return target;
6090 break;
6092 CASE_INT_FN (BUILT_IN_POPCOUNT):
6093 target = expand_builtin_unop (target_mode, exp, target,
6094 subtarget, popcount_optab);
6095 if (target)
6096 return target;
6097 break;
6099 CASE_INT_FN (BUILT_IN_PARITY):
6100 target = expand_builtin_unop (target_mode, exp, target,
6101 subtarget, parity_optab);
6102 if (target)
6103 return target;
6104 break;
6106 case BUILT_IN_STRLEN:
6107 target = expand_builtin_strlen (exp, target, target_mode);
6108 if (target)
6109 return target;
6110 break;
6112 case BUILT_IN_STRCPY:
6113 target = expand_builtin_strcpy (exp, target);
6114 if (target)
6115 return target;
6116 break;
6118 case BUILT_IN_STRNCPY:
6119 target = expand_builtin_strncpy (exp, target);
6120 if (target)
6121 return target;
6122 break;
6124 case BUILT_IN_STPCPY:
6125 target = expand_builtin_stpcpy (exp, target, mode);
6126 if (target)
6127 return target;
6128 break;
6130 case BUILT_IN_MEMCPY:
6131 target = expand_builtin_memcpy (exp, target);
6132 if (target)
6133 return target;
6134 break;
6136 case BUILT_IN_MEMPCPY:
6137 target = expand_builtin_mempcpy (exp, target, mode);
6138 if (target)
6139 return target;
6140 break;
6142 case BUILT_IN_MEMSET:
6143 target = expand_builtin_memset (exp, target, mode);
6144 if (target)
6145 return target;
6146 break;
6148 case BUILT_IN_BZERO:
6149 target = expand_builtin_bzero (exp);
6150 if (target)
6151 return target;
6152 break;
6154 case BUILT_IN_STRCMP:
6155 target = expand_builtin_strcmp (exp, target);
6156 if (target)
6157 return target;
6158 break;
6160 case BUILT_IN_STRNCMP:
6161 target = expand_builtin_strncmp (exp, target, mode);
6162 if (target)
6163 return target;
6164 break;
6166 case BUILT_IN_BCMP:
6167 case BUILT_IN_MEMCMP:
6168 target = expand_builtin_memcmp (exp, target, mode);
6169 if (target)
6170 return target;
6171 break;
6173 case BUILT_IN_SETJMP:
6174 /* This should have been lowered to the builtins below. */
6175 gcc_unreachable ();
6177 case BUILT_IN_SETJMP_SETUP:
6178 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6179 and the receiver label. */
6180 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6182 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6183 VOIDmode, EXPAND_NORMAL);
6184 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6185 rtx label_r = label_rtx (label);
6187 /* This is copied from the handling of non-local gotos. */
6188 expand_builtin_setjmp_setup (buf_addr, label_r);
6189 nonlocal_goto_handler_labels
6190 = gen_rtx_INSN_LIST (VOIDmode, label_r,
6191 nonlocal_goto_handler_labels);
6192 /* ??? Do not let expand_label treat us as such since we would
6193 not want to be both on the list of non-local labels and on
6194 the list of forced labels. */
6195 FORCED_LABEL (label) = 0;
6196 return const0_rtx;
6198 break;
6200 case BUILT_IN_SETJMP_RECEIVER:
6201 /* __builtin_setjmp_receiver is passed the receiver label. */
6202 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6204 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6205 rtx label_r = label_rtx (label);
6207 expand_builtin_setjmp_receiver (label_r);
6208 return const0_rtx;
6210 break;
6212 /* __builtin_longjmp is passed a pointer to an array of five words.
6213 It's similar to the C library longjmp function but works with
6214 __builtin_setjmp above. */
6215 case BUILT_IN_LONGJMP:
6216 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6218 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6219 VOIDmode, EXPAND_NORMAL);
6220 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6222 if (value != const1_rtx)
6224 error ("%<__builtin_longjmp%> second argument must be 1");
6225 return const0_rtx;
6228 expand_builtin_longjmp (buf_addr, value);
6229 return const0_rtx;
6231 break;
6233 case BUILT_IN_NONLOCAL_GOTO:
6234 target = expand_builtin_nonlocal_goto (exp);
6235 if (target)
6236 return target;
6237 break;
6239 /* This updates the setjmp buffer that is its argument with the value
6240 of the current stack pointer. */
6241 case BUILT_IN_UPDATE_SETJMP_BUF:
6242 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6244 rtx buf_addr
6245 = expand_normal (CALL_EXPR_ARG (exp, 0));
6247 expand_builtin_update_setjmp_buf (buf_addr);
6248 return const0_rtx;
6250 break;
6252 case BUILT_IN_TRAP:
6253 expand_builtin_trap ();
6254 return const0_rtx;
6256 case BUILT_IN_UNREACHABLE:
6257 expand_builtin_unreachable ();
6258 return const0_rtx;
6260 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6261 case BUILT_IN_SIGNBITD32:
6262 case BUILT_IN_SIGNBITD64:
6263 case BUILT_IN_SIGNBITD128:
6264 target = expand_builtin_signbit (exp, target);
6265 if (target)
6266 return target;
6267 break;
6269 /* Various hooks for the DWARF 2 __throw routine. */
6270 case BUILT_IN_UNWIND_INIT:
6271 expand_builtin_unwind_init ();
6272 return const0_rtx;
6273 case BUILT_IN_DWARF_CFA:
6274 return virtual_cfa_rtx;
6275 #ifdef DWARF2_UNWIND_INFO
6276 case BUILT_IN_DWARF_SP_COLUMN:
6277 return expand_builtin_dwarf_sp_column ();
6278 case BUILT_IN_INIT_DWARF_REG_SIZES:
6279 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6280 return const0_rtx;
6281 #endif
6282 case BUILT_IN_FROB_RETURN_ADDR:
6283 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6284 case BUILT_IN_EXTRACT_RETURN_ADDR:
6285 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6286 case BUILT_IN_EH_RETURN:
6287 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6288 CALL_EXPR_ARG (exp, 1));
6289 return const0_rtx;
6290 #ifdef EH_RETURN_DATA_REGNO
6291 case BUILT_IN_EH_RETURN_DATA_REGNO:
6292 return expand_builtin_eh_return_data_regno (exp);
6293 #endif
6294 case BUILT_IN_EXTEND_POINTER:
6295 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6296 case BUILT_IN_EH_POINTER:
6297 return expand_builtin_eh_pointer (exp);
6298 case BUILT_IN_EH_FILTER:
6299 return expand_builtin_eh_filter (exp);
6300 case BUILT_IN_EH_COPY_VALUES:
6301 return expand_builtin_eh_copy_values (exp);
6303 case BUILT_IN_VA_START:
6304 return expand_builtin_va_start (exp);
6305 case BUILT_IN_VA_END:
6306 return expand_builtin_va_end (exp);
6307 case BUILT_IN_VA_COPY:
6308 return expand_builtin_va_copy (exp);
6309 case BUILT_IN_EXPECT:
6310 return expand_builtin_expect (exp, target);
6311 case BUILT_IN_ASSUME_ALIGNED:
6312 return expand_builtin_assume_aligned (exp, target);
6313 case BUILT_IN_PREFETCH:
6314 expand_builtin_prefetch (exp);
6315 return const0_rtx;
6317 case BUILT_IN_INIT_TRAMPOLINE:
6318 return expand_builtin_init_trampoline (exp, true);
6319 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6320 return expand_builtin_init_trampoline (exp, false);
6321 case BUILT_IN_ADJUST_TRAMPOLINE:
6322 return expand_builtin_adjust_trampoline (exp);
6324 case BUILT_IN_FORK:
6325 case BUILT_IN_EXECL:
6326 case BUILT_IN_EXECV:
6327 case BUILT_IN_EXECLP:
6328 case BUILT_IN_EXECLE:
6329 case BUILT_IN_EXECVP:
6330 case BUILT_IN_EXECVE:
6331 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6332 if (target)
6333 return target;
6334 break;
6336 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6337 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6338 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6339 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6340 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6341 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6342 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6343 if (target)
6344 return target;
6345 break;
6347 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6348 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6349 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6350 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6351 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6352 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6353 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6354 if (target)
6355 return target;
6356 break;
6358 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6359 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6360 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6361 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6362 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6363 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6364 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6365 if (target)
6366 return target;
6367 break;
6369 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6370 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6371 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6372 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6373 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6374 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6375 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6376 if (target)
6377 return target;
6378 break;
6380 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6381 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6382 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6383 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6384 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6385 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6386 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6387 if (target)
6388 return target;
6389 break;
6391 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6392 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6393 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6394 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6395 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6396 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6397 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6398 if (target)
6399 return target;
6400 break;
6402 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6403 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6404 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6405 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6406 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6407 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6408 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6409 if (target)
6410 return target;
6411 break;
6413 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6414 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6415 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6416 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6417 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6418 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6419 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6420 if (target)
6421 return target;
6422 break;
6424 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6425 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6426 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6427 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6428 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6429 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6430 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6431 if (target)
6432 return target;
6433 break;
6435 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6436 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6437 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6438 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6439 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6440 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6441 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6442 if (target)
6443 return target;
6444 break;
6446 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6447 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6448 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6449 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6450 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6451 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6452 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6453 if (target)
6454 return target;
6455 break;
6457 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6458 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6459 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6460 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6461 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6462 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6463 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6464 if (target)
6465 return target;
6466 break;
6468 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6469 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6470 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6471 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6472 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6473 if (mode == VOIDmode)
6474 mode = TYPE_MODE (boolean_type_node);
6475 if (!target || !register_operand (target, mode))
6476 target = gen_reg_rtx (mode);
6478 mode = get_builtin_sync_mode
6479 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6480 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6481 if (target)
6482 return target;
6483 break;
6485 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6486 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6487 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6488 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6489 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6490 mode = get_builtin_sync_mode
6491 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6492 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6493 if (target)
6494 return target;
6495 break;
6497 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6498 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6499 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6500 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6501 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6502 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6503 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6504 if (target)
6505 return target;
6506 break;
6508 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6509 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6510 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6511 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6512 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6513 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6514 expand_builtin_sync_lock_release (mode, exp);
6515 return const0_rtx;
6517 case BUILT_IN_SYNC_SYNCHRONIZE:
6518 expand_builtin_sync_synchronize ();
6519 return const0_rtx;
6521 case BUILT_IN_ATOMIC_EXCHANGE_1:
6522 case BUILT_IN_ATOMIC_EXCHANGE_2:
6523 case BUILT_IN_ATOMIC_EXCHANGE_4:
6524 case BUILT_IN_ATOMIC_EXCHANGE_8:
6525 case BUILT_IN_ATOMIC_EXCHANGE_16:
6526 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6527 target = expand_builtin_atomic_exchange (mode, exp, target);
6528 if (target)
6529 return target;
6530 break;
6532 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6533 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6534 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6535 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6536 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6538 unsigned int nargs, z;
6539 vec<tree, va_gc> *vec;
6541 mode =
6542 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6543 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6544 if (target)
6545 return target;
6547 /* If this is turned into an external library call, the weak parameter
6548 must be dropped to match the expected parameter list. */
6549 nargs = call_expr_nargs (exp);
6550 vec_alloc (vec, nargs - 1);
6551 for (z = 0; z < 3; z++)
6552 vec->quick_push (CALL_EXPR_ARG (exp, z));
6553 /* Skip the boolean weak parameter. */
6554 for (z = 4; z < 6; z++)
6555 vec->quick_push (CALL_EXPR_ARG (exp, z));
6556 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6557 break;
6560 case BUILT_IN_ATOMIC_LOAD_1:
6561 case BUILT_IN_ATOMIC_LOAD_2:
6562 case BUILT_IN_ATOMIC_LOAD_4:
6563 case BUILT_IN_ATOMIC_LOAD_8:
6564 case BUILT_IN_ATOMIC_LOAD_16:
6565 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6566 target = expand_builtin_atomic_load (mode, exp, target);
6567 if (target)
6568 return target;
6569 break;
6571 case BUILT_IN_ATOMIC_STORE_1:
6572 case BUILT_IN_ATOMIC_STORE_2:
6573 case BUILT_IN_ATOMIC_STORE_4:
6574 case BUILT_IN_ATOMIC_STORE_8:
6575 case BUILT_IN_ATOMIC_STORE_16:
6576 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6577 target = expand_builtin_atomic_store (mode, exp);
6578 if (target)
6579 return const0_rtx;
6580 break;
6582 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6583 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6584 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6585 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6586 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6588 enum built_in_function lib;
6589 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6590 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6591 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6592 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6593 ignore, lib);
6594 if (target)
6595 return target;
6596 break;
6598 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6599 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6600 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6601 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6602 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6604 enum built_in_function lib;
6605 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6606 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6607 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6608 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6609 ignore, lib);
6610 if (target)
6611 return target;
6612 break;
6614 case BUILT_IN_ATOMIC_AND_FETCH_1:
6615 case BUILT_IN_ATOMIC_AND_FETCH_2:
6616 case BUILT_IN_ATOMIC_AND_FETCH_4:
6617 case BUILT_IN_ATOMIC_AND_FETCH_8:
6618 case BUILT_IN_ATOMIC_AND_FETCH_16:
6620 enum built_in_function lib;
6621 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6622 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6623 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6624 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6625 ignore, lib);
6626 if (target)
6627 return target;
6628 break;
6630 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6631 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6632 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6633 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6634 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6636 enum built_in_function lib;
6637 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6638 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6639 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6640 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6641 ignore, lib);
6642 if (target)
6643 return target;
6644 break;
6646 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6647 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6648 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6649 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6650 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6652 enum built_in_function lib;
6653 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6654 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6655 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6656 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6657 ignore, lib);
6658 if (target)
6659 return target;
6660 break;
6662 case BUILT_IN_ATOMIC_OR_FETCH_1:
6663 case BUILT_IN_ATOMIC_OR_FETCH_2:
6664 case BUILT_IN_ATOMIC_OR_FETCH_4:
6665 case BUILT_IN_ATOMIC_OR_FETCH_8:
6666 case BUILT_IN_ATOMIC_OR_FETCH_16:
6668 enum built_in_function lib;
6669 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6670 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6671 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6672 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6673 ignore, lib);
6674 if (target)
6675 return target;
6676 break;
6678 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6679 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6680 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6681 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6682 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6683 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6684 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6685 ignore, BUILT_IN_NONE);
6686 if (target)
6687 return target;
6688 break;
6690 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6691 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6692 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6693 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6694 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6695 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6696 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6697 ignore, BUILT_IN_NONE);
6698 if (target)
6699 return target;
6700 break;
6702 case BUILT_IN_ATOMIC_FETCH_AND_1:
6703 case BUILT_IN_ATOMIC_FETCH_AND_2:
6704 case BUILT_IN_ATOMIC_FETCH_AND_4:
6705 case BUILT_IN_ATOMIC_FETCH_AND_8:
6706 case BUILT_IN_ATOMIC_FETCH_AND_16:
6707 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6708 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6709 ignore, BUILT_IN_NONE);
6710 if (target)
6711 return target;
6712 break;
6714 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6715 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6716 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6717 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6718 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6719 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6720 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6721 ignore, BUILT_IN_NONE);
6722 if (target)
6723 return target;
6724 break;
6726 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6727 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6728 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6729 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6730 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6731 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6732 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6733 ignore, BUILT_IN_NONE);
6734 if (target)
6735 return target;
6736 break;
6738 case BUILT_IN_ATOMIC_FETCH_OR_1:
6739 case BUILT_IN_ATOMIC_FETCH_OR_2:
6740 case BUILT_IN_ATOMIC_FETCH_OR_4:
6741 case BUILT_IN_ATOMIC_FETCH_OR_8:
6742 case BUILT_IN_ATOMIC_FETCH_OR_16:
6743 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6744 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6745 ignore, BUILT_IN_NONE);
6746 if (target)
6747 return target;
6748 break;
6750 case BUILT_IN_ATOMIC_TEST_AND_SET:
6751 return expand_builtin_atomic_test_and_set (exp, target);
6753 case BUILT_IN_ATOMIC_CLEAR:
6754 return expand_builtin_atomic_clear (exp);
6756 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6757 return expand_builtin_atomic_always_lock_free (exp);
6759 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6760 target = expand_builtin_atomic_is_lock_free (exp);
6761 if (target)
6762 return target;
6763 break;
6765 case BUILT_IN_ATOMIC_THREAD_FENCE:
6766 expand_builtin_atomic_thread_fence (exp);
6767 return const0_rtx;
6769 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6770 expand_builtin_atomic_signal_fence (exp);
6771 return const0_rtx;
6773 case BUILT_IN_OBJECT_SIZE:
6774 return expand_builtin_object_size (exp);
6776 case BUILT_IN_MEMCPY_CHK:
6777 case BUILT_IN_MEMPCPY_CHK:
6778 case BUILT_IN_MEMMOVE_CHK:
6779 case BUILT_IN_MEMSET_CHK:
6780 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6781 if (target)
6782 return target;
6783 break;
6785 case BUILT_IN_STRCPY_CHK:
6786 case BUILT_IN_STPCPY_CHK:
6787 case BUILT_IN_STRNCPY_CHK:
6788 case BUILT_IN_STPNCPY_CHK:
6789 case BUILT_IN_STRCAT_CHK:
6790 case BUILT_IN_STRNCAT_CHK:
6791 case BUILT_IN_SNPRINTF_CHK:
6792 case BUILT_IN_VSNPRINTF_CHK:
6793 maybe_emit_chk_warning (exp, fcode);
6794 break;
6796 case BUILT_IN_SPRINTF_CHK:
6797 case BUILT_IN_VSPRINTF_CHK:
6798 maybe_emit_sprintf_chk_warning (exp, fcode);
6799 break;
6801 case BUILT_IN_FREE:
6802 if (warn_free_nonheap_object)
6803 maybe_emit_free_warning (exp);
6804 break;
6806 case BUILT_IN_THREAD_POINTER:
6807 return expand_builtin_thread_pointer (exp, target);
6809 case BUILT_IN_SET_THREAD_POINTER:
6810 expand_builtin_set_thread_pointer (exp);
6811 return const0_rtx;
6813 case BUILT_IN_CILK_DETACH:
6814 expand_builtin_cilk_detach (exp);
6815 return const0_rtx;
6817 case BUILT_IN_CILK_POP_FRAME:
6818 expand_builtin_cilk_pop_frame (exp);
6819 return const0_rtx;
6821 default: /* just do library call, if unknown builtin */
6822 break;
6825 /* The switch statement above can drop through to cause the function
6826 to be called normally. */
6827 return expand_call (exp, target, ignore);
6830 /* Determine whether a tree node represents a call to a built-in
6831 function. If the tree T is a call to a built-in function with
6832 the right number of arguments of the appropriate types, return
6833 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6834 Otherwise the return value is END_BUILTINS. */
6836 enum built_in_function
6837 builtin_mathfn_code (const_tree t)
6839 const_tree fndecl, arg, parmlist;
6840 const_tree argtype, parmtype;
6841 const_call_expr_arg_iterator iter;
6843 if (TREE_CODE (t) != CALL_EXPR
6844 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6845 return END_BUILTINS;
6847 fndecl = get_callee_fndecl (t);
6848 if (fndecl == NULL_TREE
6849 || TREE_CODE (fndecl) != FUNCTION_DECL
6850 || ! DECL_BUILT_IN (fndecl)
6851 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6852 return END_BUILTINS;
6854 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6855 init_const_call_expr_arg_iterator (t, &iter);
6856 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6858 /* If a function doesn't take a variable number of arguments,
6859 the last element in the list will have type `void'. */
6860 parmtype = TREE_VALUE (parmlist);
6861 if (VOID_TYPE_P (parmtype))
6863 if (more_const_call_expr_args_p (&iter))
6864 return END_BUILTINS;
6865 return DECL_FUNCTION_CODE (fndecl);
6868 if (! more_const_call_expr_args_p (&iter))
6869 return END_BUILTINS;
6871 arg = next_const_call_expr_arg (&iter);
6872 argtype = TREE_TYPE (arg);
6874 if (SCALAR_FLOAT_TYPE_P (parmtype))
6876 if (! SCALAR_FLOAT_TYPE_P (argtype))
6877 return END_BUILTINS;
6879 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6881 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6882 return END_BUILTINS;
6884 else if (POINTER_TYPE_P (parmtype))
6886 if (! POINTER_TYPE_P (argtype))
6887 return END_BUILTINS;
6889 else if (INTEGRAL_TYPE_P (parmtype))
6891 if (! INTEGRAL_TYPE_P (argtype))
6892 return END_BUILTINS;
6894 else
6895 return END_BUILTINS;
6898 /* Variable-length argument list. */
6899 return DECL_FUNCTION_CODE (fndecl);
6902 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6903 evaluate to a constant. */
6905 static tree
6906 fold_builtin_constant_p (tree arg)
6908 /* We return 1 for a numeric type that's known to be a constant
6909 value at compile-time or for an aggregate type that's a
6910 literal constant. */
6911 STRIP_NOPS (arg);
6913 /* If we know this is a constant, emit the constant of one. */
6914 if (CONSTANT_CLASS_P (arg)
6915 || (TREE_CODE (arg) == CONSTRUCTOR
6916 && TREE_CONSTANT (arg)))
6917 return integer_one_node;
6918 if (TREE_CODE (arg) == ADDR_EXPR)
6920 tree op = TREE_OPERAND (arg, 0);
6921 if (TREE_CODE (op) == STRING_CST
6922 || (TREE_CODE (op) == ARRAY_REF
6923 && integer_zerop (TREE_OPERAND (op, 1))
6924 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6925 return integer_one_node;
6928 /* If this expression has side effects, show we don't know it to be a
6929 constant. Likewise if it's a pointer or aggregate type since in
6930 those case we only want literals, since those are only optimized
6931 when generating RTL, not later.
6932 And finally, if we are compiling an initializer, not code, we
6933 need to return a definite result now; there's not going to be any
6934 more optimization done. */
6935 if (TREE_SIDE_EFFECTS (arg)
6936 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6937 || POINTER_TYPE_P (TREE_TYPE (arg))
6938 || cfun == 0
6939 || folding_initializer
6940 || force_folding_builtin_constant_p)
6941 return integer_zero_node;
6943 return NULL_TREE;
6946 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6947 return it as a truthvalue. */
6949 static tree
6950 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
6951 tree predictor)
6953 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6955 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6956 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6957 ret_type = TREE_TYPE (TREE_TYPE (fn));
6958 pred_type = TREE_VALUE (arg_types);
6959 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6961 pred = fold_convert_loc (loc, pred_type, pred);
6962 expected = fold_convert_loc (loc, expected_type, expected);
6963 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
6964 predictor);
6966 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6967 build_int_cst (ret_type, 0));
6970 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6971 NULL_TREE if no simplification is possible. */
6973 tree
6974 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
6976 tree inner, fndecl, inner_arg0;
6977 enum tree_code code;
6979 /* Distribute the expected value over short-circuiting operators.
6980 See through the cast from truthvalue_type_node to long. */
6981 inner_arg0 = arg0;
6982 while (TREE_CODE (inner_arg0) == NOP_EXPR
6983 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
6984 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
6985 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
6987 /* If this is a builtin_expect within a builtin_expect keep the
6988 inner one. See through a comparison against a constant. It
6989 might have been added to create a thruthvalue. */
6990 inner = inner_arg0;
6992 if (COMPARISON_CLASS_P (inner)
6993 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6994 inner = TREE_OPERAND (inner, 0);
6996 if (TREE_CODE (inner) == CALL_EXPR
6997 && (fndecl = get_callee_fndecl (inner))
6998 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6999 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7000 return arg0;
7002 inner = inner_arg0;
7003 code = TREE_CODE (inner);
7004 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7006 tree op0 = TREE_OPERAND (inner, 0);
7007 tree op1 = TREE_OPERAND (inner, 1);
7009 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
7010 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
7011 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7013 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7016 /* If the argument isn't invariant then there's nothing else we can do. */
7017 if (!TREE_CONSTANT (inner_arg0))
7018 return NULL_TREE;
7020 /* If we expect that a comparison against the argument will fold to
7021 a constant return the constant. In practice, this means a true
7022 constant or the address of a non-weak symbol. */
7023 inner = inner_arg0;
7024 STRIP_NOPS (inner);
7025 if (TREE_CODE (inner) == ADDR_EXPR)
7029 inner = TREE_OPERAND (inner, 0);
7031 while (TREE_CODE (inner) == COMPONENT_REF
7032 || TREE_CODE (inner) == ARRAY_REF);
7033 if ((TREE_CODE (inner) == VAR_DECL
7034 || TREE_CODE (inner) == FUNCTION_DECL)
7035 && DECL_WEAK (inner))
7036 return NULL_TREE;
7039 /* Otherwise, ARG0 already has the proper type for the return value. */
7040 return arg0;
7043 /* Fold a call to __builtin_classify_type with argument ARG. */
7045 static tree
7046 fold_builtin_classify_type (tree arg)
7048 if (arg == 0)
7049 return build_int_cst (integer_type_node, no_type_class);
7051 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7054 /* Fold a call to __builtin_strlen with argument ARG. */
7056 static tree
7057 fold_builtin_strlen (location_t loc, tree type, tree arg)
7059 if (!validate_arg (arg, POINTER_TYPE))
7060 return NULL_TREE;
7061 else
7063 tree len = c_strlen (arg, 0);
7065 if (len)
7066 return fold_convert_loc (loc, type, len);
7068 return NULL_TREE;
7072 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7074 static tree
7075 fold_builtin_inf (location_t loc, tree type, int warn)
7077 REAL_VALUE_TYPE real;
7079 /* __builtin_inff is intended to be usable to define INFINITY on all
7080 targets. If an infinity is not available, INFINITY expands "to a
7081 positive constant of type float that overflows at translation
7082 time", footnote "In this case, using INFINITY will violate the
7083 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7084 Thus we pedwarn to ensure this constraint violation is
7085 diagnosed. */
7086 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7087 pedwarn (loc, 0, "target format does not support infinity");
7089 real_inf (&real);
7090 return build_real (type, real);
7093 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7095 static tree
7096 fold_builtin_nan (tree arg, tree type, int quiet)
7098 REAL_VALUE_TYPE real;
7099 const char *str;
7101 if (!validate_arg (arg, POINTER_TYPE))
7102 return NULL_TREE;
7103 str = c_getstr (arg);
7104 if (!str)
7105 return NULL_TREE;
7107 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7108 return NULL_TREE;
7110 return build_real (type, real);
7113 /* Return true if the floating point expression T has an integer value.
7114 We also allow +Inf, -Inf and NaN to be considered integer values. */
7116 static bool
7117 integer_valued_real_p (tree t)
7119 switch (TREE_CODE (t))
7121 case FLOAT_EXPR:
7122 return true;
7124 case ABS_EXPR:
7125 case SAVE_EXPR:
7126 return integer_valued_real_p (TREE_OPERAND (t, 0));
7128 case COMPOUND_EXPR:
7129 case MODIFY_EXPR:
7130 case BIND_EXPR:
7131 return integer_valued_real_p (TREE_OPERAND (t, 1));
7133 case PLUS_EXPR:
7134 case MINUS_EXPR:
7135 case MULT_EXPR:
7136 case MIN_EXPR:
7137 case MAX_EXPR:
7138 return integer_valued_real_p (TREE_OPERAND (t, 0))
7139 && integer_valued_real_p (TREE_OPERAND (t, 1));
7141 case COND_EXPR:
7142 return integer_valued_real_p (TREE_OPERAND (t, 1))
7143 && integer_valued_real_p (TREE_OPERAND (t, 2));
7145 case REAL_CST:
7146 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7148 case NOP_EXPR:
7150 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7151 if (TREE_CODE (type) == INTEGER_TYPE)
7152 return true;
7153 if (TREE_CODE (type) == REAL_TYPE)
7154 return integer_valued_real_p (TREE_OPERAND (t, 0));
7155 break;
7158 case CALL_EXPR:
7159 switch (builtin_mathfn_code (t))
7161 CASE_FLT_FN (BUILT_IN_CEIL):
7162 CASE_FLT_FN (BUILT_IN_FLOOR):
7163 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7164 CASE_FLT_FN (BUILT_IN_RINT):
7165 CASE_FLT_FN (BUILT_IN_ROUND):
7166 CASE_FLT_FN (BUILT_IN_TRUNC):
7167 return true;
7169 CASE_FLT_FN (BUILT_IN_FMIN):
7170 CASE_FLT_FN (BUILT_IN_FMAX):
7171 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7172 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7174 default:
7175 break;
7177 break;
7179 default:
7180 break;
7182 return false;
7185 /* FNDECL is assumed to be a builtin where truncation can be propagated
7186 across (for instance floor((double)f) == (double)floorf (f).
7187 Do the transformation for a call with argument ARG. */
7189 static tree
7190 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7192 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7194 if (!validate_arg (arg, REAL_TYPE))
7195 return NULL_TREE;
7197 /* Integer rounding functions are idempotent. */
7198 if (fcode == builtin_mathfn_code (arg))
7199 return arg;
7201 /* If argument is already integer valued, and we don't need to worry
7202 about setting errno, there's no need to perform rounding. */
7203 if (! flag_errno_math && integer_valued_real_p (arg))
7204 return arg;
7206 if (optimize)
7208 tree arg0 = strip_float_extensions (arg);
7209 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7210 tree newtype = TREE_TYPE (arg0);
7211 tree decl;
7213 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7214 && (decl = mathfn_built_in (newtype, fcode)))
7215 return fold_convert_loc (loc, ftype,
7216 build_call_expr_loc (loc, decl, 1,
7217 fold_convert_loc (loc,
7218 newtype,
7219 arg0)));
7221 return NULL_TREE;
7224 /* FNDECL is assumed to be builtin which can narrow the FP type of
7225 the argument, for instance lround((double)f) -> lroundf (f).
7226 Do the transformation for a call with argument ARG. */
7228 static tree
7229 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7231 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7233 if (!validate_arg (arg, REAL_TYPE))
7234 return NULL_TREE;
7236 /* If argument is already integer valued, and we don't need to worry
7237 about setting errno, there's no need to perform rounding. */
7238 if (! flag_errno_math && integer_valued_real_p (arg))
7239 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7240 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7242 if (optimize)
7244 tree ftype = TREE_TYPE (arg);
7245 tree arg0 = strip_float_extensions (arg);
7246 tree newtype = TREE_TYPE (arg0);
7247 tree decl;
7249 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7250 && (decl = mathfn_built_in (newtype, fcode)))
7251 return build_call_expr_loc (loc, decl, 1,
7252 fold_convert_loc (loc, newtype, arg0));
7255 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7256 sizeof (int) == sizeof (long). */
7257 if (TYPE_PRECISION (integer_type_node)
7258 == TYPE_PRECISION (long_integer_type_node))
7260 tree newfn = NULL_TREE;
7261 switch (fcode)
7263 CASE_FLT_FN (BUILT_IN_ICEIL):
7264 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7265 break;
7267 CASE_FLT_FN (BUILT_IN_IFLOOR):
7268 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7269 break;
7271 CASE_FLT_FN (BUILT_IN_IROUND):
7272 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7273 break;
7275 CASE_FLT_FN (BUILT_IN_IRINT):
7276 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7277 break;
7279 default:
7280 break;
7283 if (newfn)
7285 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7286 return fold_convert_loc (loc,
7287 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7291 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7292 sizeof (long long) == sizeof (long). */
7293 if (TYPE_PRECISION (long_long_integer_type_node)
7294 == TYPE_PRECISION (long_integer_type_node))
7296 tree newfn = NULL_TREE;
7297 switch (fcode)
7299 CASE_FLT_FN (BUILT_IN_LLCEIL):
7300 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7301 break;
7303 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7304 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7305 break;
7307 CASE_FLT_FN (BUILT_IN_LLROUND):
7308 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7309 break;
7311 CASE_FLT_FN (BUILT_IN_LLRINT):
7312 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7313 break;
7315 default:
7316 break;
7319 if (newfn)
7321 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7322 return fold_convert_loc (loc,
7323 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7327 return NULL_TREE;
7330 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7331 return type. Return NULL_TREE if no simplification can be made. */
7333 static tree
7334 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7336 tree res;
7338 if (!validate_arg (arg, COMPLEX_TYPE)
7339 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7340 return NULL_TREE;
7342 /* Calculate the result when the argument is a constant. */
7343 if (TREE_CODE (arg) == COMPLEX_CST
7344 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7345 type, mpfr_hypot)))
7346 return res;
7348 if (TREE_CODE (arg) == COMPLEX_EXPR)
7350 tree real = TREE_OPERAND (arg, 0);
7351 tree imag = TREE_OPERAND (arg, 1);
7353 /* If either part is zero, cabs is fabs of the other. */
7354 if (real_zerop (real))
7355 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7356 if (real_zerop (imag))
7357 return fold_build1_loc (loc, ABS_EXPR, type, real);
7359 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7360 if (flag_unsafe_math_optimizations
7361 && operand_equal_p (real, imag, OEP_PURE_SAME))
7363 const REAL_VALUE_TYPE sqrt2_trunc
7364 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7365 STRIP_NOPS (real);
7366 return fold_build2_loc (loc, MULT_EXPR, type,
7367 fold_build1_loc (loc, ABS_EXPR, type, real),
7368 build_real (type, sqrt2_trunc));
7372 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7373 if (TREE_CODE (arg) == NEGATE_EXPR
7374 || TREE_CODE (arg) == CONJ_EXPR)
7375 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7377 /* Don't do this when optimizing for size. */
7378 if (flag_unsafe_math_optimizations
7379 && optimize && optimize_function_for_speed_p (cfun))
7381 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7383 if (sqrtfn != NULL_TREE)
7385 tree rpart, ipart, result;
7387 arg = builtin_save_expr (arg);
7389 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7390 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7392 rpart = builtin_save_expr (rpart);
7393 ipart = builtin_save_expr (ipart);
7395 result = fold_build2_loc (loc, PLUS_EXPR, type,
7396 fold_build2_loc (loc, MULT_EXPR, type,
7397 rpart, rpart),
7398 fold_build2_loc (loc, MULT_EXPR, type,
7399 ipart, ipart));
7401 return build_call_expr_loc (loc, sqrtfn, 1, result);
7405 return NULL_TREE;
7408 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7409 complex tree type of the result. If NEG is true, the imaginary
7410 zero is negative. */
7412 static tree
7413 build_complex_cproj (tree type, bool neg)
7415 REAL_VALUE_TYPE rinf, rzero = dconst0;
7417 real_inf (&rinf);
7418 rzero.sign = neg;
7419 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7420 build_real (TREE_TYPE (type), rzero));
7423 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7424 return type. Return NULL_TREE if no simplification can be made. */
7426 static tree
7427 fold_builtin_cproj (location_t loc, tree arg, tree type)
7429 if (!validate_arg (arg, COMPLEX_TYPE)
7430 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7431 return NULL_TREE;
7433 /* If there are no infinities, return arg. */
7434 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7435 return non_lvalue_loc (loc, arg);
7437 /* Calculate the result when the argument is a constant. */
7438 if (TREE_CODE (arg) == COMPLEX_CST)
7440 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7441 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7443 if (real_isinf (real) || real_isinf (imag))
7444 return build_complex_cproj (type, imag->sign);
7445 else
7446 return arg;
7448 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7450 tree real = TREE_OPERAND (arg, 0);
7451 tree imag = TREE_OPERAND (arg, 1);
7453 STRIP_NOPS (real);
7454 STRIP_NOPS (imag);
7456 /* If the real part is inf and the imag part is known to be
7457 nonnegative, return (inf + 0i). Remember side-effects are
7458 possible in the imag part. */
7459 if (TREE_CODE (real) == REAL_CST
7460 && real_isinf (TREE_REAL_CST_PTR (real))
7461 && tree_expr_nonnegative_p (imag))
7462 return omit_one_operand_loc (loc, type,
7463 build_complex_cproj (type, false),
7464 arg);
7466 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7467 Remember side-effects are possible in the real part. */
7468 if (TREE_CODE (imag) == REAL_CST
7469 && real_isinf (TREE_REAL_CST_PTR (imag)))
7470 return
7471 omit_one_operand_loc (loc, type,
7472 build_complex_cproj (type, TREE_REAL_CST_PTR
7473 (imag)->sign), arg);
7476 return NULL_TREE;
7479 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7480 Return NULL_TREE if no simplification can be made. */
7482 static tree
7483 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7486 enum built_in_function fcode;
7487 tree res;
7489 if (!validate_arg (arg, REAL_TYPE))
7490 return NULL_TREE;
7492 /* Calculate the result when the argument is a constant. */
7493 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7494 return res;
7496 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7497 fcode = builtin_mathfn_code (arg);
7498 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7500 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7501 arg = fold_build2_loc (loc, MULT_EXPR, type,
7502 CALL_EXPR_ARG (arg, 0),
7503 build_real (type, dconsthalf));
7504 return build_call_expr_loc (loc, expfn, 1, arg);
7507 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7508 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7510 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7512 if (powfn)
7514 tree arg0 = CALL_EXPR_ARG (arg, 0);
7515 tree tree_root;
7516 /* The inner root was either sqrt or cbrt. */
7517 /* This was a conditional expression but it triggered a bug
7518 in Sun C 5.5. */
7519 REAL_VALUE_TYPE dconstroot;
7520 if (BUILTIN_SQRT_P (fcode))
7521 dconstroot = dconsthalf;
7522 else
7523 dconstroot = dconst_third ();
7525 /* Adjust for the outer root. */
7526 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7527 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7528 tree_root = build_real (type, dconstroot);
7529 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7533 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7534 if (flag_unsafe_math_optimizations
7535 && (fcode == BUILT_IN_POW
7536 || fcode == BUILT_IN_POWF
7537 || fcode == BUILT_IN_POWL))
7539 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7540 tree arg0 = CALL_EXPR_ARG (arg, 0);
7541 tree arg1 = CALL_EXPR_ARG (arg, 1);
7542 tree narg1;
7543 if (!tree_expr_nonnegative_p (arg0))
7544 arg0 = build1 (ABS_EXPR, type, arg0);
7545 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7546 build_real (type, dconsthalf));
7547 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7550 return NULL_TREE;
7553 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7554 Return NULL_TREE if no simplification can be made. */
7556 static tree
7557 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7559 const enum built_in_function fcode = builtin_mathfn_code (arg);
7560 tree res;
7562 if (!validate_arg (arg, REAL_TYPE))
7563 return NULL_TREE;
7565 /* Calculate the result when the argument is a constant. */
7566 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7567 return res;
7569 if (flag_unsafe_math_optimizations)
7571 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7572 if (BUILTIN_EXPONENT_P (fcode))
7574 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7575 const REAL_VALUE_TYPE third_trunc =
7576 real_value_truncate (TYPE_MODE (type), dconst_third ());
7577 arg = fold_build2_loc (loc, MULT_EXPR, type,
7578 CALL_EXPR_ARG (arg, 0),
7579 build_real (type, third_trunc));
7580 return build_call_expr_loc (loc, expfn, 1, arg);
7583 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7584 if (BUILTIN_SQRT_P (fcode))
7586 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7588 if (powfn)
7590 tree arg0 = CALL_EXPR_ARG (arg, 0);
7591 tree tree_root;
7592 REAL_VALUE_TYPE dconstroot = dconst_third ();
7594 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7595 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7596 tree_root = build_real (type, dconstroot);
7597 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7601 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7602 if (BUILTIN_CBRT_P (fcode))
7604 tree arg0 = CALL_EXPR_ARG (arg, 0);
7605 if (tree_expr_nonnegative_p (arg0))
7607 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7609 if (powfn)
7611 tree tree_root;
7612 REAL_VALUE_TYPE dconstroot;
7614 real_arithmetic (&dconstroot, MULT_EXPR,
7615 dconst_third_ptr (), dconst_third_ptr ());
7616 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7617 tree_root = build_real (type, dconstroot);
7618 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7623 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7624 if (fcode == BUILT_IN_POW
7625 || fcode == BUILT_IN_POWF
7626 || fcode == BUILT_IN_POWL)
7628 tree arg00 = CALL_EXPR_ARG (arg, 0);
7629 tree arg01 = CALL_EXPR_ARG (arg, 1);
7630 if (tree_expr_nonnegative_p (arg00))
7632 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7633 const REAL_VALUE_TYPE dconstroot
7634 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7635 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7636 build_real (type, dconstroot));
7637 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7641 return NULL_TREE;
7644 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7645 TYPE is the type of the return value. Return NULL_TREE if no
7646 simplification can be made. */
7648 static tree
7649 fold_builtin_cos (location_t loc,
7650 tree arg, tree type, tree fndecl)
7652 tree res, narg;
7654 if (!validate_arg (arg, REAL_TYPE))
7655 return NULL_TREE;
7657 /* Calculate the result when the argument is a constant. */
7658 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7659 return res;
7661 /* Optimize cos(-x) into cos (x). */
7662 if ((narg = fold_strip_sign_ops (arg)))
7663 return build_call_expr_loc (loc, fndecl, 1, narg);
7665 return NULL_TREE;
7668 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7669 Return NULL_TREE if no simplification can be made. */
7671 static tree
7672 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7674 if (validate_arg (arg, REAL_TYPE))
7676 tree res, narg;
7678 /* Calculate the result when the argument is a constant. */
7679 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7680 return res;
7682 /* Optimize cosh(-x) into cosh (x). */
7683 if ((narg = fold_strip_sign_ops (arg)))
7684 return build_call_expr_loc (loc, fndecl, 1, narg);
7687 return NULL_TREE;
7690 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7691 argument ARG. TYPE is the type of the return value. Return
7692 NULL_TREE if no simplification can be made. */
7694 static tree
7695 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7696 bool hyper)
7698 if (validate_arg (arg, COMPLEX_TYPE)
7699 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7701 tree tmp;
7703 /* Calculate the result when the argument is a constant. */
7704 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7705 return tmp;
7707 /* Optimize fn(-x) into fn(x). */
7708 if ((tmp = fold_strip_sign_ops (arg)))
7709 return build_call_expr_loc (loc, fndecl, 1, tmp);
7712 return NULL_TREE;
7715 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7716 Return NULL_TREE if no simplification can be made. */
7718 static tree
7719 fold_builtin_tan (tree arg, tree type)
7721 enum built_in_function fcode;
7722 tree res;
7724 if (!validate_arg (arg, REAL_TYPE))
7725 return NULL_TREE;
7727 /* Calculate the result when the argument is a constant. */
7728 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7729 return res;
7731 /* Optimize tan(atan(x)) = x. */
7732 fcode = builtin_mathfn_code (arg);
7733 if (flag_unsafe_math_optimizations
7734 && (fcode == BUILT_IN_ATAN
7735 || fcode == BUILT_IN_ATANF
7736 || fcode == BUILT_IN_ATANL))
7737 return CALL_EXPR_ARG (arg, 0);
7739 return NULL_TREE;
7742 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7743 NULL_TREE if no simplification can be made. */
7745 static tree
7746 fold_builtin_sincos (location_t loc,
7747 tree arg0, tree arg1, tree arg2)
7749 tree type;
7750 tree res, fn, call;
7752 if (!validate_arg (arg0, REAL_TYPE)
7753 || !validate_arg (arg1, POINTER_TYPE)
7754 || !validate_arg (arg2, POINTER_TYPE))
7755 return NULL_TREE;
7757 type = TREE_TYPE (arg0);
7759 /* Calculate the result when the argument is a constant. */
7760 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7761 return res;
7763 /* Canonicalize sincos to cexpi. */
7764 if (!targetm.libc_has_function (function_c99_math_complex))
7765 return NULL_TREE;
7766 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7767 if (!fn)
7768 return NULL_TREE;
7770 call = build_call_expr_loc (loc, fn, 1, arg0);
7771 call = builtin_save_expr (call);
7773 return build2 (COMPOUND_EXPR, void_type_node,
7774 build2 (MODIFY_EXPR, void_type_node,
7775 build_fold_indirect_ref_loc (loc, arg1),
7776 build1 (IMAGPART_EXPR, type, call)),
7777 build2 (MODIFY_EXPR, void_type_node,
7778 build_fold_indirect_ref_loc (loc, arg2),
7779 build1 (REALPART_EXPR, type, call)));
7782 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7783 NULL_TREE if no simplification can be made. */
7785 static tree
7786 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7788 tree rtype;
7789 tree realp, imagp, ifn;
7790 tree res;
7792 if (!validate_arg (arg0, COMPLEX_TYPE)
7793 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7794 return NULL_TREE;
7796 /* Calculate the result when the argument is a constant. */
7797 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7798 return res;
7800 rtype = TREE_TYPE (TREE_TYPE (arg0));
7802 /* In case we can figure out the real part of arg0 and it is constant zero
7803 fold to cexpi. */
7804 if (!targetm.libc_has_function (function_c99_math_complex))
7805 return NULL_TREE;
7806 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7807 if (!ifn)
7808 return NULL_TREE;
7810 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7811 && real_zerop (realp))
7813 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7814 return build_call_expr_loc (loc, ifn, 1, narg);
7817 /* In case we can easily decompose real and imaginary parts split cexp
7818 to exp (r) * cexpi (i). */
7819 if (flag_unsafe_math_optimizations
7820 && realp)
7822 tree rfn, rcall, icall;
7824 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7825 if (!rfn)
7826 return NULL_TREE;
7828 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7829 if (!imagp)
7830 return NULL_TREE;
7832 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7833 icall = builtin_save_expr (icall);
7834 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7835 rcall = builtin_save_expr (rcall);
7836 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7837 fold_build2_loc (loc, MULT_EXPR, rtype,
7838 rcall,
7839 fold_build1_loc (loc, REALPART_EXPR,
7840 rtype, icall)),
7841 fold_build2_loc (loc, MULT_EXPR, rtype,
7842 rcall,
7843 fold_build1_loc (loc, IMAGPART_EXPR,
7844 rtype, icall)));
7847 return NULL_TREE;
7850 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7851 Return NULL_TREE if no simplification can be made. */
7853 static tree
7854 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7856 if (!validate_arg (arg, REAL_TYPE))
7857 return NULL_TREE;
7859 /* Optimize trunc of constant value. */
7860 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7862 REAL_VALUE_TYPE r, x;
7863 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7865 x = TREE_REAL_CST (arg);
7866 real_trunc (&r, TYPE_MODE (type), &x);
7867 return build_real (type, r);
7870 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7873 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7874 Return NULL_TREE if no simplification can be made. */
7876 static tree
7877 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7879 if (!validate_arg (arg, REAL_TYPE))
7880 return NULL_TREE;
7882 /* Optimize floor of constant value. */
7883 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7885 REAL_VALUE_TYPE x;
7887 x = TREE_REAL_CST (arg);
7888 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7890 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7891 REAL_VALUE_TYPE r;
7893 real_floor (&r, TYPE_MODE (type), &x);
7894 return build_real (type, r);
7898 /* Fold floor (x) where x is nonnegative to trunc (x). */
7899 if (tree_expr_nonnegative_p (arg))
7901 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7902 if (truncfn)
7903 return build_call_expr_loc (loc, truncfn, 1, arg);
7906 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7909 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7910 Return NULL_TREE if no simplification can be made. */
7912 static tree
7913 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7915 if (!validate_arg (arg, REAL_TYPE))
7916 return NULL_TREE;
7918 /* Optimize ceil of constant value. */
7919 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7921 REAL_VALUE_TYPE x;
7923 x = TREE_REAL_CST (arg);
7924 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7926 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7927 REAL_VALUE_TYPE r;
7929 real_ceil (&r, TYPE_MODE (type), &x);
7930 return build_real (type, r);
7934 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7937 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7938 Return NULL_TREE if no simplification can be made. */
7940 static tree
7941 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7943 if (!validate_arg (arg, REAL_TYPE))
7944 return NULL_TREE;
7946 /* Optimize round of constant value. */
7947 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7949 REAL_VALUE_TYPE x;
7951 x = TREE_REAL_CST (arg);
7952 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7954 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7955 REAL_VALUE_TYPE r;
7957 real_round (&r, TYPE_MODE (type), &x);
7958 return build_real (type, r);
7962 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7965 /* Fold function call to builtin lround, lroundf or lroundl (or the
7966 corresponding long long versions) and other rounding functions. ARG
7967 is the argument to the call. Return NULL_TREE if no simplification
7968 can be made. */
7970 static tree
7971 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7973 if (!validate_arg (arg, REAL_TYPE))
7974 return NULL_TREE;
7976 /* Optimize lround of constant value. */
7977 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7979 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7981 if (real_isfinite (&x))
7983 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7984 tree ftype = TREE_TYPE (arg);
7985 REAL_VALUE_TYPE r;
7986 bool fail = false;
7988 switch (DECL_FUNCTION_CODE (fndecl))
7990 CASE_FLT_FN (BUILT_IN_IFLOOR):
7991 CASE_FLT_FN (BUILT_IN_LFLOOR):
7992 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7993 real_floor (&r, TYPE_MODE (ftype), &x);
7994 break;
7996 CASE_FLT_FN (BUILT_IN_ICEIL):
7997 CASE_FLT_FN (BUILT_IN_LCEIL):
7998 CASE_FLT_FN (BUILT_IN_LLCEIL):
7999 real_ceil (&r, TYPE_MODE (ftype), &x);
8000 break;
8002 CASE_FLT_FN (BUILT_IN_IROUND):
8003 CASE_FLT_FN (BUILT_IN_LROUND):
8004 CASE_FLT_FN (BUILT_IN_LLROUND):
8005 real_round (&r, TYPE_MODE (ftype), &x);
8006 break;
8008 default:
8009 gcc_unreachable ();
8012 wide_int val = real_to_integer (&r, &fail, TYPE_PRECISION (itype));
8013 if (!fail)
8014 return wide_int_to_tree (itype, val);
8018 switch (DECL_FUNCTION_CODE (fndecl))
8020 CASE_FLT_FN (BUILT_IN_LFLOOR):
8021 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8022 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8023 if (tree_expr_nonnegative_p (arg))
8024 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8025 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8026 break;
8027 default:;
8030 return fold_fixed_mathfn (loc, fndecl, arg);
8033 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8034 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8035 the argument to the call. Return NULL_TREE if no simplification can
8036 be made. */
8038 static tree
8039 fold_builtin_bitop (tree fndecl, tree arg)
8041 if (!validate_arg (arg, INTEGER_TYPE))
8042 return NULL_TREE;
8044 /* Optimize for constant argument. */
8045 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8047 tree type = TREE_TYPE (arg);
8048 int result;
8050 switch (DECL_FUNCTION_CODE (fndecl))
8052 CASE_INT_FN (BUILT_IN_FFS):
8053 result = wi::ffs (arg);
8054 break;
8056 CASE_INT_FN (BUILT_IN_CLZ):
8057 if (wi::ne_p (arg, 0))
8058 result = wi::clz (arg);
8059 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8060 result = TYPE_PRECISION (type);
8061 break;
8063 CASE_INT_FN (BUILT_IN_CTZ):
8064 if (wi::ne_p (arg, 0))
8065 result = wi::ctz (arg);
8066 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8067 result = TYPE_PRECISION (type);
8068 break;
8070 CASE_INT_FN (BUILT_IN_CLRSB):
8071 result = wi::clrsb (arg);
8072 break;
8074 CASE_INT_FN (BUILT_IN_POPCOUNT):
8075 result = wi::popcount (arg);
8076 break;
8078 CASE_INT_FN (BUILT_IN_PARITY):
8079 result = wi::parity (arg);
8080 break;
8082 default:
8083 gcc_unreachable ();
8086 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8089 return NULL_TREE;
8092 /* Fold function call to builtin_bswap and the short, long and long long
8093 variants. Return NULL_TREE if no simplification can be made. */
8094 static tree
8095 fold_builtin_bswap (tree fndecl, tree arg)
8097 if (! validate_arg (arg, INTEGER_TYPE))
8098 return NULL_TREE;
8100 /* Optimize constant value. */
8101 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8103 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8105 switch (DECL_FUNCTION_CODE (fndecl))
8107 case BUILT_IN_BSWAP16:
8108 case BUILT_IN_BSWAP32:
8109 case BUILT_IN_BSWAP64:
8111 signop sgn = TYPE_SIGN (type);
8112 tree result =
8113 wide_int_to_tree (type,
8114 wide_int::from (arg, TYPE_PRECISION (type),
8115 sgn).bswap ());
8116 return result;
8118 default:
8119 gcc_unreachable ();
8123 return NULL_TREE;
8126 /* A subroutine of fold_builtin to fold the various logarithmic
8127 functions. Return NULL_TREE if no simplification can me made.
8128 FUNC is the corresponding MPFR logarithm function. */
8130 static tree
8131 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8132 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8134 if (validate_arg (arg, REAL_TYPE))
8136 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8137 tree res;
8138 const enum built_in_function fcode = builtin_mathfn_code (arg);
8140 /* Calculate the result when the argument is a constant. */
8141 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8142 return res;
8144 /* Special case, optimize logN(expN(x)) = x. */
8145 if (flag_unsafe_math_optimizations
8146 && ((func == mpfr_log
8147 && (fcode == BUILT_IN_EXP
8148 || fcode == BUILT_IN_EXPF
8149 || fcode == BUILT_IN_EXPL))
8150 || (func == mpfr_log2
8151 && (fcode == BUILT_IN_EXP2
8152 || fcode == BUILT_IN_EXP2F
8153 || fcode == BUILT_IN_EXP2L))
8154 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8155 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8157 /* Optimize logN(func()) for various exponential functions. We
8158 want to determine the value "x" and the power "exponent" in
8159 order to transform logN(x**exponent) into exponent*logN(x). */
8160 if (flag_unsafe_math_optimizations)
8162 tree exponent = 0, x = 0;
8164 switch (fcode)
8166 CASE_FLT_FN (BUILT_IN_EXP):
8167 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8168 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8169 dconst_e ()));
8170 exponent = CALL_EXPR_ARG (arg, 0);
8171 break;
8172 CASE_FLT_FN (BUILT_IN_EXP2):
8173 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8174 x = build_real (type, dconst2);
8175 exponent = CALL_EXPR_ARG (arg, 0);
8176 break;
8177 CASE_FLT_FN (BUILT_IN_EXP10):
8178 CASE_FLT_FN (BUILT_IN_POW10):
8179 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8181 REAL_VALUE_TYPE dconst10;
8182 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
8183 x = build_real (type, dconst10);
8185 exponent = CALL_EXPR_ARG (arg, 0);
8186 break;
8187 CASE_FLT_FN (BUILT_IN_SQRT):
8188 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8189 x = CALL_EXPR_ARG (arg, 0);
8190 exponent = build_real (type, dconsthalf);
8191 break;
8192 CASE_FLT_FN (BUILT_IN_CBRT):
8193 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8194 x = CALL_EXPR_ARG (arg, 0);
8195 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8196 dconst_third ()));
8197 break;
8198 CASE_FLT_FN (BUILT_IN_POW):
8199 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8200 x = CALL_EXPR_ARG (arg, 0);
8201 exponent = CALL_EXPR_ARG (arg, 1);
8202 break;
8203 default:
8204 break;
8207 /* Now perform the optimization. */
8208 if (x && exponent)
8210 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8211 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8216 return NULL_TREE;
8219 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8220 NULL_TREE if no simplification can be made. */
8222 static tree
8223 fold_builtin_hypot (location_t loc, tree fndecl,
8224 tree arg0, tree arg1, tree type)
8226 tree res, narg0, narg1;
8228 if (!validate_arg (arg0, REAL_TYPE)
8229 || !validate_arg (arg1, REAL_TYPE))
8230 return NULL_TREE;
8232 /* Calculate the result when the argument is a constant. */
8233 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8234 return res;
8236 /* If either argument to hypot has a negate or abs, strip that off.
8237 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8238 narg0 = fold_strip_sign_ops (arg0);
8239 narg1 = fold_strip_sign_ops (arg1);
8240 if (narg0 || narg1)
8242 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8243 narg1 ? narg1 : arg1);
8246 /* If either argument is zero, hypot is fabs of the other. */
8247 if (real_zerop (arg0))
8248 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8249 else if (real_zerop (arg1))
8250 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8252 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8253 if (flag_unsafe_math_optimizations
8254 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8256 const REAL_VALUE_TYPE sqrt2_trunc
8257 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8258 return fold_build2_loc (loc, MULT_EXPR, type,
8259 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8260 build_real (type, sqrt2_trunc));
8263 return NULL_TREE;
8267 /* Fold a builtin function call to pow, powf, or powl. Return
8268 NULL_TREE if no simplification can be made. */
8269 static tree
8270 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8272 tree res;
8274 if (!validate_arg (arg0, REAL_TYPE)
8275 || !validate_arg (arg1, REAL_TYPE))
8276 return NULL_TREE;
8278 /* Calculate the result when the argument is a constant. */
8279 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8280 return res;
8282 /* Optimize pow(1.0,y) = 1.0. */
8283 if (real_onep (arg0))
8284 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8286 if (TREE_CODE (arg1) == REAL_CST
8287 && !TREE_OVERFLOW (arg1))
8289 REAL_VALUE_TYPE cint;
8290 REAL_VALUE_TYPE c;
8291 HOST_WIDE_INT n;
8293 c = TREE_REAL_CST (arg1);
8295 /* Optimize pow(x,0.0) = 1.0. */
8296 if (REAL_VALUES_EQUAL (c, dconst0))
8297 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8298 arg0);
8300 /* Optimize pow(x,1.0) = x. */
8301 if (REAL_VALUES_EQUAL (c, dconst1))
8302 return arg0;
8304 /* Optimize pow(x,-1.0) = 1.0/x. */
8305 if (REAL_VALUES_EQUAL (c, dconstm1))
8306 return fold_build2_loc (loc, RDIV_EXPR, type,
8307 build_real (type, dconst1), arg0);
8309 /* Optimize pow(x,0.5) = sqrt(x). */
8310 if (flag_unsafe_math_optimizations
8311 && REAL_VALUES_EQUAL (c, dconsthalf))
8313 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8315 if (sqrtfn != NULL_TREE)
8316 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8319 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8320 if (flag_unsafe_math_optimizations)
8322 const REAL_VALUE_TYPE dconstroot
8323 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8325 if (REAL_VALUES_EQUAL (c, dconstroot))
8327 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8328 if (cbrtfn != NULL_TREE)
8329 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8333 /* Check for an integer exponent. */
8334 n = real_to_integer (&c);
8335 real_from_integer (&cint, VOIDmode, n, SIGNED);
8336 if (real_identical (&c, &cint))
8338 /* Attempt to evaluate pow at compile-time, unless this should
8339 raise an exception. */
8340 if (TREE_CODE (arg0) == REAL_CST
8341 && !TREE_OVERFLOW (arg0)
8342 && (n > 0
8343 || (!flag_trapping_math && !flag_errno_math)
8344 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8346 REAL_VALUE_TYPE x;
8347 bool inexact;
8349 x = TREE_REAL_CST (arg0);
8350 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8351 if (flag_unsafe_math_optimizations || !inexact)
8352 return build_real (type, x);
8355 /* Strip sign ops from even integer powers. */
8356 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8358 tree narg0 = fold_strip_sign_ops (arg0);
8359 if (narg0)
8360 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8365 if (flag_unsafe_math_optimizations)
8367 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8369 /* Optimize pow(expN(x),y) = expN(x*y). */
8370 if (BUILTIN_EXPONENT_P (fcode))
8372 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8373 tree arg = CALL_EXPR_ARG (arg0, 0);
8374 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8375 return build_call_expr_loc (loc, expfn, 1, arg);
8378 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8379 if (BUILTIN_SQRT_P (fcode))
8381 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8382 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8383 build_real (type, dconsthalf));
8384 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8387 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8388 if (BUILTIN_CBRT_P (fcode))
8390 tree arg = CALL_EXPR_ARG (arg0, 0);
8391 if (tree_expr_nonnegative_p (arg))
8393 const REAL_VALUE_TYPE dconstroot
8394 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8395 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8396 build_real (type, dconstroot));
8397 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8401 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8402 if (fcode == BUILT_IN_POW
8403 || fcode == BUILT_IN_POWF
8404 || fcode == BUILT_IN_POWL)
8406 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8407 if (tree_expr_nonnegative_p (arg00))
8409 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8410 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8411 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8416 return NULL_TREE;
8419 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8420 Return NULL_TREE if no simplification can be made. */
8421 static tree
8422 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8423 tree arg0, tree arg1, tree type)
8425 if (!validate_arg (arg0, REAL_TYPE)
8426 || !validate_arg (arg1, INTEGER_TYPE))
8427 return NULL_TREE;
8429 /* Optimize pow(1.0,y) = 1.0. */
8430 if (real_onep (arg0))
8431 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8433 if (tree_fits_shwi_p (arg1))
8435 HOST_WIDE_INT c = tree_to_shwi (arg1);
8437 /* Evaluate powi at compile-time. */
8438 if (TREE_CODE (arg0) == REAL_CST
8439 && !TREE_OVERFLOW (arg0))
8441 REAL_VALUE_TYPE x;
8442 x = TREE_REAL_CST (arg0);
8443 real_powi (&x, TYPE_MODE (type), &x, c);
8444 return build_real (type, x);
8447 /* Optimize pow(x,0) = 1.0. */
8448 if (c == 0)
8449 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8450 arg0);
8452 /* Optimize pow(x,1) = x. */
8453 if (c == 1)
8454 return arg0;
8456 /* Optimize pow(x,-1) = 1.0/x. */
8457 if (c == -1)
8458 return fold_build2_loc (loc, RDIV_EXPR, type,
8459 build_real (type, dconst1), arg0);
8462 return NULL_TREE;
8465 /* A subroutine of fold_builtin to fold the various exponent
8466 functions. Return NULL_TREE if no simplification can be made.
8467 FUNC is the corresponding MPFR exponent function. */
8469 static tree
8470 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8471 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8473 if (validate_arg (arg, REAL_TYPE))
8475 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8476 tree res;
8478 /* Calculate the result when the argument is a constant. */
8479 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8480 return res;
8482 /* Optimize expN(logN(x)) = x. */
8483 if (flag_unsafe_math_optimizations)
8485 const enum built_in_function fcode = builtin_mathfn_code (arg);
8487 if ((func == mpfr_exp
8488 && (fcode == BUILT_IN_LOG
8489 || fcode == BUILT_IN_LOGF
8490 || fcode == BUILT_IN_LOGL))
8491 || (func == mpfr_exp2
8492 && (fcode == BUILT_IN_LOG2
8493 || fcode == BUILT_IN_LOG2F
8494 || fcode == BUILT_IN_LOG2L))
8495 || (func == mpfr_exp10
8496 && (fcode == BUILT_IN_LOG10
8497 || fcode == BUILT_IN_LOG10F
8498 || fcode == BUILT_IN_LOG10L)))
8499 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8503 return NULL_TREE;
8506 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8507 Return NULL_TREE if no simplification can be made. */
8509 static tree
8510 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8512 tree fn, len, lenp1, call, type;
8514 if (!validate_arg (dest, POINTER_TYPE)
8515 || !validate_arg (src, POINTER_TYPE))
8516 return NULL_TREE;
8518 len = c_strlen (src, 1);
8519 if (!len
8520 || TREE_CODE (len) != INTEGER_CST)
8521 return NULL_TREE;
8523 if (optimize_function_for_size_p (cfun)
8524 /* If length is zero it's small enough. */
8525 && !integer_zerop (len))
8526 return NULL_TREE;
8528 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8529 if (!fn)
8530 return NULL_TREE;
8532 lenp1 = size_binop_loc (loc, PLUS_EXPR,
8533 fold_convert_loc (loc, size_type_node, len),
8534 build_int_cst (size_type_node, 1));
8535 /* We use dest twice in building our expression. Save it from
8536 multiple expansions. */
8537 dest = builtin_save_expr (dest);
8538 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8540 type = TREE_TYPE (TREE_TYPE (fndecl));
8541 dest = fold_build_pointer_plus_loc (loc, dest, len);
8542 dest = fold_convert_loc (loc, type, dest);
8543 dest = omit_one_operand_loc (loc, type, dest, call);
8544 return dest;
8547 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8548 arguments to the call, and TYPE is its return type.
8549 Return NULL_TREE if no simplification can be made. */
8551 static tree
8552 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8554 if (!validate_arg (arg1, POINTER_TYPE)
8555 || !validate_arg (arg2, INTEGER_TYPE)
8556 || !validate_arg (len, INTEGER_TYPE))
8557 return NULL_TREE;
8558 else
8560 const char *p1;
8562 if (TREE_CODE (arg2) != INTEGER_CST
8563 || !tree_fits_uhwi_p (len))
8564 return NULL_TREE;
8566 p1 = c_getstr (arg1);
8567 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8569 char c;
8570 const char *r;
8571 tree tem;
8573 if (target_char_cast (arg2, &c))
8574 return NULL_TREE;
8576 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
8578 if (r == NULL)
8579 return build_int_cst (TREE_TYPE (arg1), 0);
8581 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
8582 return fold_convert_loc (loc, type, tem);
8584 return NULL_TREE;
8588 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8589 Return NULL_TREE if no simplification can be made. */
8591 static tree
8592 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8594 const char *p1, *p2;
8596 if (!validate_arg (arg1, POINTER_TYPE)
8597 || !validate_arg (arg2, POINTER_TYPE)
8598 || !validate_arg (len, INTEGER_TYPE))
8599 return NULL_TREE;
8601 /* If the LEN parameter is zero, return zero. */
8602 if (integer_zerop (len))
8603 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8604 arg1, arg2);
8606 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8607 if (operand_equal_p (arg1, arg2, 0))
8608 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8610 p1 = c_getstr (arg1);
8611 p2 = c_getstr (arg2);
8613 /* If all arguments are constant, and the value of len is not greater
8614 than the lengths of arg1 and arg2, evaluate at compile-time. */
8615 if (tree_fits_uhwi_p (len) && p1 && p2
8616 && compare_tree_int (len, strlen (p1) + 1) <= 0
8617 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8619 const int r = memcmp (p1, p2, tree_to_uhwi (len));
8621 if (r > 0)
8622 return integer_one_node;
8623 else if (r < 0)
8624 return integer_minus_one_node;
8625 else
8626 return integer_zero_node;
8629 /* If len parameter is one, return an expression corresponding to
8630 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8631 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8633 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8634 tree cst_uchar_ptr_node
8635 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8637 tree ind1
8638 = fold_convert_loc (loc, integer_type_node,
8639 build1 (INDIRECT_REF, cst_uchar_node,
8640 fold_convert_loc (loc,
8641 cst_uchar_ptr_node,
8642 arg1)));
8643 tree ind2
8644 = fold_convert_loc (loc, integer_type_node,
8645 build1 (INDIRECT_REF, cst_uchar_node,
8646 fold_convert_loc (loc,
8647 cst_uchar_ptr_node,
8648 arg2)));
8649 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8652 return NULL_TREE;
8655 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8656 Return NULL_TREE if no simplification can be made. */
8658 static tree
8659 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8661 const char *p1, *p2;
8663 if (!validate_arg (arg1, POINTER_TYPE)
8664 || !validate_arg (arg2, POINTER_TYPE))
8665 return NULL_TREE;
8667 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8668 if (operand_equal_p (arg1, arg2, 0))
8669 return integer_zero_node;
8671 p1 = c_getstr (arg1);
8672 p2 = c_getstr (arg2);
8674 if (p1 && p2)
8676 const int i = strcmp (p1, p2);
8677 if (i < 0)
8678 return integer_minus_one_node;
8679 else if (i > 0)
8680 return integer_one_node;
8681 else
8682 return integer_zero_node;
8685 /* If the second arg is "", return *(const unsigned char*)arg1. */
8686 if (p2 && *p2 == '\0')
8688 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8689 tree cst_uchar_ptr_node
8690 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8692 return fold_convert_loc (loc, integer_type_node,
8693 build1 (INDIRECT_REF, cst_uchar_node,
8694 fold_convert_loc (loc,
8695 cst_uchar_ptr_node,
8696 arg1)));
8699 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8700 if (p1 && *p1 == '\0')
8702 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8703 tree cst_uchar_ptr_node
8704 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8706 tree temp
8707 = fold_convert_loc (loc, integer_type_node,
8708 build1 (INDIRECT_REF, cst_uchar_node,
8709 fold_convert_loc (loc,
8710 cst_uchar_ptr_node,
8711 arg2)));
8712 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8715 return NULL_TREE;
8718 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8719 Return NULL_TREE if no simplification can be made. */
8721 static tree
8722 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8724 const char *p1, *p2;
8726 if (!validate_arg (arg1, POINTER_TYPE)
8727 || !validate_arg (arg2, POINTER_TYPE)
8728 || !validate_arg (len, INTEGER_TYPE))
8729 return NULL_TREE;
8731 /* If the LEN parameter is zero, return zero. */
8732 if (integer_zerop (len))
8733 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8734 arg1, arg2);
8736 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8737 if (operand_equal_p (arg1, arg2, 0))
8738 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8740 p1 = c_getstr (arg1);
8741 p2 = c_getstr (arg2);
8743 if (tree_fits_uhwi_p (len) && p1 && p2)
8745 const int i = strncmp (p1, p2, tree_to_uhwi (len));
8746 if (i > 0)
8747 return integer_one_node;
8748 else if (i < 0)
8749 return integer_minus_one_node;
8750 else
8751 return integer_zero_node;
8754 /* If the second arg is "", and the length is greater than zero,
8755 return *(const unsigned char*)arg1. */
8756 if (p2 && *p2 == '\0'
8757 && TREE_CODE (len) == INTEGER_CST
8758 && tree_int_cst_sgn (len) == 1)
8760 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8761 tree cst_uchar_ptr_node
8762 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8764 return fold_convert_loc (loc, integer_type_node,
8765 build1 (INDIRECT_REF, cst_uchar_node,
8766 fold_convert_loc (loc,
8767 cst_uchar_ptr_node,
8768 arg1)));
8771 /* If the first arg is "", and the length is greater than zero,
8772 return -*(const unsigned char*)arg2. */
8773 if (p1 && *p1 == '\0'
8774 && TREE_CODE (len) == INTEGER_CST
8775 && tree_int_cst_sgn (len) == 1)
8777 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8778 tree cst_uchar_ptr_node
8779 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8781 tree temp = fold_convert_loc (loc, integer_type_node,
8782 build1 (INDIRECT_REF, cst_uchar_node,
8783 fold_convert_loc (loc,
8784 cst_uchar_ptr_node,
8785 arg2)));
8786 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8789 /* If len parameter is one, return an expression corresponding to
8790 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8791 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8793 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8794 tree cst_uchar_ptr_node
8795 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8797 tree ind1 = fold_convert_loc (loc, integer_type_node,
8798 build1 (INDIRECT_REF, cst_uchar_node,
8799 fold_convert_loc (loc,
8800 cst_uchar_ptr_node,
8801 arg1)));
8802 tree ind2 = fold_convert_loc (loc, integer_type_node,
8803 build1 (INDIRECT_REF, cst_uchar_node,
8804 fold_convert_loc (loc,
8805 cst_uchar_ptr_node,
8806 arg2)));
8807 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8810 return NULL_TREE;
8813 /* Fold function call to builtin signbit, signbitf or signbitl with argument
8814 ARG. Return NULL_TREE if no simplification can be made. */
8816 static tree
8817 fold_builtin_signbit (location_t loc, tree arg, tree type)
8819 if (!validate_arg (arg, REAL_TYPE))
8820 return NULL_TREE;
8822 /* If ARG is a compile-time constant, determine the result. */
8823 if (TREE_CODE (arg) == REAL_CST
8824 && !TREE_OVERFLOW (arg))
8826 REAL_VALUE_TYPE c;
8828 c = TREE_REAL_CST (arg);
8829 return (REAL_VALUE_NEGATIVE (c)
8830 ? build_one_cst (type)
8831 : build_zero_cst (type));
8834 /* If ARG is non-negative, the result is always zero. */
8835 if (tree_expr_nonnegative_p (arg))
8836 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8838 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8839 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8840 return fold_convert (type,
8841 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
8842 build_real (TREE_TYPE (arg), dconst0)));
8844 return NULL_TREE;
8847 /* Fold function call to builtin copysign, copysignf or copysignl with
8848 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
8849 be made. */
8851 static tree
8852 fold_builtin_copysign (location_t loc, tree fndecl,
8853 tree arg1, tree arg2, tree type)
8855 tree tem;
8857 if (!validate_arg (arg1, REAL_TYPE)
8858 || !validate_arg (arg2, REAL_TYPE))
8859 return NULL_TREE;
8861 /* copysign(X,X) is X. */
8862 if (operand_equal_p (arg1, arg2, 0))
8863 return fold_convert_loc (loc, type, arg1);
8865 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8866 if (TREE_CODE (arg1) == REAL_CST
8867 && TREE_CODE (arg2) == REAL_CST
8868 && !TREE_OVERFLOW (arg1)
8869 && !TREE_OVERFLOW (arg2))
8871 REAL_VALUE_TYPE c1, c2;
8873 c1 = TREE_REAL_CST (arg1);
8874 c2 = TREE_REAL_CST (arg2);
8875 /* c1.sign := c2.sign. */
8876 real_copysign (&c1, &c2);
8877 return build_real (type, c1);
8880 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8881 Remember to evaluate Y for side-effects. */
8882 if (tree_expr_nonnegative_p (arg2))
8883 return omit_one_operand_loc (loc, type,
8884 fold_build1_loc (loc, ABS_EXPR, type, arg1),
8885 arg2);
8887 /* Strip sign changing operations for the first argument. */
8888 tem = fold_strip_sign_ops (arg1);
8889 if (tem)
8890 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
8892 return NULL_TREE;
8895 /* Fold a call to builtin isascii with argument ARG. */
8897 static tree
8898 fold_builtin_isascii (location_t loc, tree arg)
8900 if (!validate_arg (arg, INTEGER_TYPE))
8901 return NULL_TREE;
8902 else
8904 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8905 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8906 build_int_cst (integer_type_node,
8907 ~ (unsigned HOST_WIDE_INT) 0x7f));
8908 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8909 arg, integer_zero_node);
8913 /* Fold a call to builtin toascii with argument ARG. */
8915 static tree
8916 fold_builtin_toascii (location_t loc, tree arg)
8918 if (!validate_arg (arg, INTEGER_TYPE))
8919 return NULL_TREE;
8921 /* Transform toascii(c) -> (c & 0x7f). */
8922 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8923 build_int_cst (integer_type_node, 0x7f));
8926 /* Fold a call to builtin isdigit with argument ARG. */
8928 static tree
8929 fold_builtin_isdigit (location_t loc, tree arg)
8931 if (!validate_arg (arg, INTEGER_TYPE))
8932 return NULL_TREE;
8933 else
8935 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8936 /* According to the C standard, isdigit is unaffected by locale.
8937 However, it definitely is affected by the target character set. */
8938 unsigned HOST_WIDE_INT target_digit0
8939 = lang_hooks.to_target_charset ('0');
8941 if (target_digit0 == 0)
8942 return NULL_TREE;
8944 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8945 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8946 build_int_cst (unsigned_type_node, target_digit0));
8947 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8948 build_int_cst (unsigned_type_node, 9));
8952 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8954 static tree
8955 fold_builtin_fabs (location_t loc, tree arg, tree type)
8957 if (!validate_arg (arg, REAL_TYPE))
8958 return NULL_TREE;
8960 arg = fold_convert_loc (loc, type, arg);
8961 if (TREE_CODE (arg) == REAL_CST)
8962 return fold_abs_const (arg, type);
8963 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8966 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8968 static tree
8969 fold_builtin_abs (location_t loc, tree arg, tree type)
8971 if (!validate_arg (arg, INTEGER_TYPE))
8972 return NULL_TREE;
8974 arg = fold_convert_loc (loc, type, arg);
8975 if (TREE_CODE (arg) == INTEGER_CST)
8976 return fold_abs_const (arg, type);
8977 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8980 /* Fold a fma operation with arguments ARG[012]. */
8982 tree
8983 fold_fma (location_t loc ATTRIBUTE_UNUSED,
8984 tree type, tree arg0, tree arg1, tree arg2)
8986 if (TREE_CODE (arg0) == REAL_CST
8987 && TREE_CODE (arg1) == REAL_CST
8988 && TREE_CODE (arg2) == REAL_CST)
8989 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
8991 return NULL_TREE;
8994 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
8996 static tree
8997 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
8999 if (validate_arg (arg0, REAL_TYPE)
9000 && validate_arg (arg1, REAL_TYPE)
9001 && validate_arg (arg2, REAL_TYPE))
9003 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9004 if (tem)
9005 return tem;
9007 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9008 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9009 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9011 return NULL_TREE;
9014 /* Fold a call to builtin fmin or fmax. */
9016 static tree
9017 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9018 tree type, bool max)
9020 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9022 /* Calculate the result when the argument is a constant. */
9023 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9025 if (res)
9026 return res;
9028 /* If either argument is NaN, return the other one. Avoid the
9029 transformation if we get (and honor) a signalling NaN. Using
9030 omit_one_operand() ensures we create a non-lvalue. */
9031 if (TREE_CODE (arg0) == REAL_CST
9032 && real_isnan (&TREE_REAL_CST (arg0))
9033 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9034 || ! TREE_REAL_CST (arg0).signalling))
9035 return omit_one_operand_loc (loc, type, arg1, arg0);
9036 if (TREE_CODE (arg1) == REAL_CST
9037 && real_isnan (&TREE_REAL_CST (arg1))
9038 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9039 || ! TREE_REAL_CST (arg1).signalling))
9040 return omit_one_operand_loc (loc, type, arg0, arg1);
9042 /* Transform fmin/fmax(x,x) -> x. */
9043 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9044 return omit_one_operand_loc (loc, type, arg0, arg1);
9046 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9047 functions to return the numeric arg if the other one is NaN.
9048 These tree codes don't honor that, so only transform if
9049 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9050 handled, so we don't have to worry about it either. */
9051 if (flag_finite_math_only)
9052 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9053 fold_convert_loc (loc, type, arg0),
9054 fold_convert_loc (loc, type, arg1));
9056 return NULL_TREE;
9059 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9061 static tree
9062 fold_builtin_carg (location_t loc, tree arg, tree type)
9064 if (validate_arg (arg, COMPLEX_TYPE)
9065 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9067 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9069 if (atan2_fn)
9071 tree new_arg = builtin_save_expr (arg);
9072 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9073 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9074 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9078 return NULL_TREE;
9081 /* Fold a call to builtin logb/ilogb. */
9083 static tree
9084 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9086 if (! validate_arg (arg, REAL_TYPE))
9087 return NULL_TREE;
9089 STRIP_NOPS (arg);
9091 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9093 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9095 switch (value->cl)
9097 case rvc_nan:
9098 case rvc_inf:
9099 /* If arg is Inf or NaN and we're logb, return it. */
9100 if (TREE_CODE (rettype) == REAL_TYPE)
9102 /* For logb(-Inf) we have to return +Inf. */
9103 if (real_isinf (value) && real_isneg (value))
9105 REAL_VALUE_TYPE tem;
9106 real_inf (&tem);
9107 return build_real (rettype, tem);
9109 return fold_convert_loc (loc, rettype, arg);
9111 /* Fall through... */
9112 case rvc_zero:
9113 /* Zero may set errno and/or raise an exception for logb, also
9114 for ilogb we don't know FP_ILOGB0. */
9115 return NULL_TREE;
9116 case rvc_normal:
9117 /* For normal numbers, proceed iff radix == 2. In GCC,
9118 normalized significands are in the range [0.5, 1.0). We
9119 want the exponent as if they were [1.0, 2.0) so get the
9120 exponent and subtract 1. */
9121 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9122 return fold_convert_loc (loc, rettype,
9123 build_int_cst (integer_type_node,
9124 REAL_EXP (value)-1));
9125 break;
9129 return NULL_TREE;
9132 /* Fold a call to builtin significand, if radix == 2. */
9134 static tree
9135 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9137 if (! validate_arg (arg, REAL_TYPE))
9138 return NULL_TREE;
9140 STRIP_NOPS (arg);
9142 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9144 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9146 switch (value->cl)
9148 case rvc_zero:
9149 case rvc_nan:
9150 case rvc_inf:
9151 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9152 return fold_convert_loc (loc, rettype, arg);
9153 case rvc_normal:
9154 /* For normal numbers, proceed iff radix == 2. */
9155 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9157 REAL_VALUE_TYPE result = *value;
9158 /* In GCC, normalized significands are in the range [0.5,
9159 1.0). We want them to be [1.0, 2.0) so set the
9160 exponent to 1. */
9161 SET_REAL_EXP (&result, 1);
9162 return build_real (rettype, result);
9164 break;
9168 return NULL_TREE;
9171 /* Fold a call to builtin frexp, we can assume the base is 2. */
9173 static tree
9174 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9176 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9177 return NULL_TREE;
9179 STRIP_NOPS (arg0);
9181 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9182 return NULL_TREE;
9184 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9186 /* Proceed if a valid pointer type was passed in. */
9187 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9189 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9190 tree frac, exp;
9192 switch (value->cl)
9194 case rvc_zero:
9195 /* For +-0, return (*exp = 0, +-0). */
9196 exp = integer_zero_node;
9197 frac = arg0;
9198 break;
9199 case rvc_nan:
9200 case rvc_inf:
9201 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9202 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9203 case rvc_normal:
9205 /* Since the frexp function always expects base 2, and in
9206 GCC normalized significands are already in the range
9207 [0.5, 1.0), we have exactly what frexp wants. */
9208 REAL_VALUE_TYPE frac_rvt = *value;
9209 SET_REAL_EXP (&frac_rvt, 0);
9210 frac = build_real (rettype, frac_rvt);
9211 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9213 break;
9214 default:
9215 gcc_unreachable ();
9218 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9219 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9220 TREE_SIDE_EFFECTS (arg1) = 1;
9221 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9224 return NULL_TREE;
9227 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9228 then we can assume the base is two. If it's false, then we have to
9229 check the mode of the TYPE parameter in certain cases. */
9231 static tree
9232 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9233 tree type, bool ldexp)
9235 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9237 STRIP_NOPS (arg0);
9238 STRIP_NOPS (arg1);
9240 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9241 if (real_zerop (arg0) || integer_zerop (arg1)
9242 || (TREE_CODE (arg0) == REAL_CST
9243 && !real_isfinite (&TREE_REAL_CST (arg0))))
9244 return omit_one_operand_loc (loc, type, arg0, arg1);
9246 /* If both arguments are constant, then try to evaluate it. */
9247 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9248 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9249 && tree_fits_shwi_p (arg1))
9251 /* Bound the maximum adjustment to twice the range of the
9252 mode's valid exponents. Use abs to ensure the range is
9253 positive as a sanity check. */
9254 const long max_exp_adj = 2 *
9255 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9256 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9258 /* Get the user-requested adjustment. */
9259 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9261 /* The requested adjustment must be inside this range. This
9262 is a preliminary cap to avoid things like overflow, we
9263 may still fail to compute the result for other reasons. */
9264 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9266 REAL_VALUE_TYPE initial_result;
9268 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9270 /* Ensure we didn't overflow. */
9271 if (! real_isinf (&initial_result))
9273 const REAL_VALUE_TYPE trunc_result
9274 = real_value_truncate (TYPE_MODE (type), initial_result);
9276 /* Only proceed if the target mode can hold the
9277 resulting value. */
9278 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9279 return build_real (type, trunc_result);
9285 return NULL_TREE;
9288 /* Fold a call to builtin modf. */
9290 static tree
9291 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9293 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9294 return NULL_TREE;
9296 STRIP_NOPS (arg0);
9298 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9299 return NULL_TREE;
9301 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9303 /* Proceed if a valid pointer type was passed in. */
9304 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9306 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9307 REAL_VALUE_TYPE trunc, frac;
9309 switch (value->cl)
9311 case rvc_nan:
9312 case rvc_zero:
9313 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9314 trunc = frac = *value;
9315 break;
9316 case rvc_inf:
9317 /* For +-Inf, return (*arg1 = arg0, +-0). */
9318 frac = dconst0;
9319 frac.sign = value->sign;
9320 trunc = *value;
9321 break;
9322 case rvc_normal:
9323 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9324 real_trunc (&trunc, VOIDmode, value);
9325 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9326 /* If the original number was negative and already
9327 integral, then the fractional part is -0.0. */
9328 if (value->sign && frac.cl == rvc_zero)
9329 frac.sign = value->sign;
9330 break;
9333 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9334 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9335 build_real (rettype, trunc));
9336 TREE_SIDE_EFFECTS (arg1) = 1;
9337 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9338 build_real (rettype, frac));
9341 return NULL_TREE;
9344 /* Given a location LOC, an interclass builtin function decl FNDECL
9345 and its single argument ARG, return an folded expression computing
9346 the same, or NULL_TREE if we either couldn't or didn't want to fold
9347 (the latter happen if there's an RTL instruction available). */
9349 static tree
9350 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9352 enum machine_mode mode;
9354 if (!validate_arg (arg, REAL_TYPE))
9355 return NULL_TREE;
9357 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9358 return NULL_TREE;
9360 mode = TYPE_MODE (TREE_TYPE (arg));
9362 /* If there is no optab, try generic code. */
9363 switch (DECL_FUNCTION_CODE (fndecl))
9365 tree result;
9367 CASE_FLT_FN (BUILT_IN_ISINF):
9369 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9370 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9371 tree const type = TREE_TYPE (arg);
9372 REAL_VALUE_TYPE r;
9373 char buf[128];
9375 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9376 real_from_string (&r, buf);
9377 result = build_call_expr (isgr_fn, 2,
9378 fold_build1_loc (loc, ABS_EXPR, type, arg),
9379 build_real (type, r));
9380 return result;
9382 CASE_FLT_FN (BUILT_IN_FINITE):
9383 case BUILT_IN_ISFINITE:
9385 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9386 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9387 tree const type = TREE_TYPE (arg);
9388 REAL_VALUE_TYPE r;
9389 char buf[128];
9391 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9392 real_from_string (&r, buf);
9393 result = build_call_expr (isle_fn, 2,
9394 fold_build1_loc (loc, ABS_EXPR, type, arg),
9395 build_real (type, r));
9396 /*result = fold_build2_loc (loc, UNGT_EXPR,
9397 TREE_TYPE (TREE_TYPE (fndecl)),
9398 fold_build1_loc (loc, ABS_EXPR, type, arg),
9399 build_real (type, r));
9400 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9401 TREE_TYPE (TREE_TYPE (fndecl)),
9402 result);*/
9403 return result;
9405 case BUILT_IN_ISNORMAL:
9407 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9408 islessequal(fabs(x),DBL_MAX). */
9409 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9410 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9411 tree const type = TREE_TYPE (arg);
9412 REAL_VALUE_TYPE rmax, rmin;
9413 char buf[128];
9415 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9416 real_from_string (&rmax, buf);
9417 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9418 real_from_string (&rmin, buf);
9419 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9420 result = build_call_expr (isle_fn, 2, arg,
9421 build_real (type, rmax));
9422 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9423 build_call_expr (isge_fn, 2, arg,
9424 build_real (type, rmin)));
9425 return result;
9427 default:
9428 break;
9431 return NULL_TREE;
9434 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9435 ARG is the argument for the call. */
9437 static tree
9438 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9440 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9441 REAL_VALUE_TYPE r;
9443 if (!validate_arg (arg, REAL_TYPE))
9444 return NULL_TREE;
9446 switch (builtin_index)
9448 case BUILT_IN_ISINF:
9449 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9450 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9452 if (TREE_CODE (arg) == REAL_CST)
9454 r = TREE_REAL_CST (arg);
9455 if (real_isinf (&r))
9456 return real_compare (GT_EXPR, &r, &dconst0)
9457 ? integer_one_node : integer_minus_one_node;
9458 else
9459 return integer_zero_node;
9462 return NULL_TREE;
9464 case BUILT_IN_ISINF_SIGN:
9466 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9467 /* In a boolean context, GCC will fold the inner COND_EXPR to
9468 1. So e.g. "if (isinf_sign(x))" would be folded to just
9469 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9470 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9471 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9472 tree tmp = NULL_TREE;
9474 arg = builtin_save_expr (arg);
9476 if (signbit_fn && isinf_fn)
9478 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9479 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9481 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9482 signbit_call, integer_zero_node);
9483 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9484 isinf_call, integer_zero_node);
9486 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9487 integer_minus_one_node, integer_one_node);
9488 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9489 isinf_call, tmp,
9490 integer_zero_node);
9493 return tmp;
9496 case BUILT_IN_ISFINITE:
9497 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9498 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9499 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9501 if (TREE_CODE (arg) == REAL_CST)
9503 r = TREE_REAL_CST (arg);
9504 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9507 return NULL_TREE;
9509 case BUILT_IN_ISNAN:
9510 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9511 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9513 if (TREE_CODE (arg) == REAL_CST)
9515 r = TREE_REAL_CST (arg);
9516 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9519 arg = builtin_save_expr (arg);
9520 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9522 default:
9523 gcc_unreachable ();
9527 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9528 This builtin will generate code to return the appropriate floating
9529 point classification depending on the value of the floating point
9530 number passed in. The possible return values must be supplied as
9531 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9532 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9533 one floating point argument which is "type generic". */
9535 static tree
9536 fold_builtin_fpclassify (location_t loc, tree exp)
9538 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9539 arg, type, res, tmp;
9540 enum machine_mode mode;
9541 REAL_VALUE_TYPE r;
9542 char buf[128];
9544 /* Verify the required arguments in the original call. */
9545 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9546 INTEGER_TYPE, INTEGER_TYPE,
9547 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9548 return NULL_TREE;
9550 fp_nan = CALL_EXPR_ARG (exp, 0);
9551 fp_infinite = CALL_EXPR_ARG (exp, 1);
9552 fp_normal = CALL_EXPR_ARG (exp, 2);
9553 fp_subnormal = CALL_EXPR_ARG (exp, 3);
9554 fp_zero = CALL_EXPR_ARG (exp, 4);
9555 arg = CALL_EXPR_ARG (exp, 5);
9556 type = TREE_TYPE (arg);
9557 mode = TYPE_MODE (type);
9558 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9560 /* fpclassify(x) ->
9561 isnan(x) ? FP_NAN :
9562 (fabs(x) == Inf ? FP_INFINITE :
9563 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9564 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9566 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9567 build_real (type, dconst0));
9568 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9569 tmp, fp_zero, fp_subnormal);
9571 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9572 real_from_string (&r, buf);
9573 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9574 arg, build_real (type, r));
9575 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9577 if (HONOR_INFINITIES (mode))
9579 real_inf (&r);
9580 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9581 build_real (type, r));
9582 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9583 fp_infinite, res);
9586 if (HONOR_NANS (mode))
9588 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9589 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9592 return res;
9595 /* Fold a call to an unordered comparison function such as
9596 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9597 being called and ARG0 and ARG1 are the arguments for the call.
9598 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9599 the opposite of the desired result. UNORDERED_CODE is used
9600 for modes that can hold NaNs and ORDERED_CODE is used for
9601 the rest. */
9603 static tree
9604 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9605 enum tree_code unordered_code,
9606 enum tree_code ordered_code)
9608 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9609 enum tree_code code;
9610 tree type0, type1;
9611 enum tree_code code0, code1;
9612 tree cmp_type = NULL_TREE;
9614 type0 = TREE_TYPE (arg0);
9615 type1 = TREE_TYPE (arg1);
9617 code0 = TREE_CODE (type0);
9618 code1 = TREE_CODE (type1);
9620 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9621 /* Choose the wider of two real types. */
9622 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9623 ? type0 : type1;
9624 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9625 cmp_type = type0;
9626 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9627 cmp_type = type1;
9629 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9630 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9632 if (unordered_code == UNORDERED_EXPR)
9634 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9635 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9636 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9639 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9640 : ordered_code;
9641 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9642 fold_build2_loc (loc, code, type, arg0, arg1));
9645 /* Fold a call to built-in function FNDECL with 0 arguments.
9646 IGNORE is true if the result of the function call is ignored. This
9647 function returns NULL_TREE if no simplification was possible. */
9649 static tree
9650 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9652 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9653 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9654 switch (fcode)
9656 CASE_FLT_FN (BUILT_IN_INF):
9657 case BUILT_IN_INFD32:
9658 case BUILT_IN_INFD64:
9659 case BUILT_IN_INFD128:
9660 return fold_builtin_inf (loc, type, true);
9662 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9663 return fold_builtin_inf (loc, type, false);
9665 case BUILT_IN_CLASSIFY_TYPE:
9666 return fold_builtin_classify_type (NULL_TREE);
9668 case BUILT_IN_UNREACHABLE:
9669 if (flag_sanitize & SANITIZE_UNREACHABLE
9670 && (current_function_decl == NULL
9671 || !lookup_attribute ("no_sanitize_undefined",
9672 DECL_ATTRIBUTES (current_function_decl))))
9673 return ubsan_instrument_unreachable (loc);
9674 break;
9676 default:
9677 break;
9679 return NULL_TREE;
9682 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9683 IGNORE is true if the result of the function call is ignored. This
9684 function returns NULL_TREE if no simplification was possible. */
9686 static tree
9687 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9689 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9690 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9691 switch (fcode)
9693 case BUILT_IN_CONSTANT_P:
9695 tree val = fold_builtin_constant_p (arg0);
9697 /* Gimplification will pull the CALL_EXPR for the builtin out of
9698 an if condition. When not optimizing, we'll not CSE it back.
9699 To avoid link error types of regressions, return false now. */
9700 if (!val && !optimize)
9701 val = integer_zero_node;
9703 return val;
9706 case BUILT_IN_CLASSIFY_TYPE:
9707 return fold_builtin_classify_type (arg0);
9709 case BUILT_IN_STRLEN:
9710 return fold_builtin_strlen (loc, type, arg0);
9712 CASE_FLT_FN (BUILT_IN_FABS):
9713 case BUILT_IN_FABSD32:
9714 case BUILT_IN_FABSD64:
9715 case BUILT_IN_FABSD128:
9716 return fold_builtin_fabs (loc, arg0, type);
9718 case BUILT_IN_ABS:
9719 case BUILT_IN_LABS:
9720 case BUILT_IN_LLABS:
9721 case BUILT_IN_IMAXABS:
9722 return fold_builtin_abs (loc, arg0, type);
9724 CASE_FLT_FN (BUILT_IN_CONJ):
9725 if (validate_arg (arg0, COMPLEX_TYPE)
9726 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9727 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9728 break;
9730 CASE_FLT_FN (BUILT_IN_CREAL):
9731 if (validate_arg (arg0, COMPLEX_TYPE)
9732 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9733 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9734 break;
9736 CASE_FLT_FN (BUILT_IN_CIMAG):
9737 if (validate_arg (arg0, COMPLEX_TYPE)
9738 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9739 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9740 break;
9742 CASE_FLT_FN (BUILT_IN_CCOS):
9743 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
9745 CASE_FLT_FN (BUILT_IN_CCOSH):
9746 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
9748 CASE_FLT_FN (BUILT_IN_CPROJ):
9749 return fold_builtin_cproj (loc, arg0, type);
9751 CASE_FLT_FN (BUILT_IN_CSIN):
9752 if (validate_arg (arg0, COMPLEX_TYPE)
9753 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9754 return do_mpc_arg1 (arg0, type, mpc_sin);
9755 break;
9757 CASE_FLT_FN (BUILT_IN_CSINH):
9758 if (validate_arg (arg0, COMPLEX_TYPE)
9759 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9760 return do_mpc_arg1 (arg0, type, mpc_sinh);
9761 break;
9763 CASE_FLT_FN (BUILT_IN_CTAN):
9764 if (validate_arg (arg0, COMPLEX_TYPE)
9765 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9766 return do_mpc_arg1 (arg0, type, mpc_tan);
9767 break;
9769 CASE_FLT_FN (BUILT_IN_CTANH):
9770 if (validate_arg (arg0, COMPLEX_TYPE)
9771 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9772 return do_mpc_arg1 (arg0, type, mpc_tanh);
9773 break;
9775 CASE_FLT_FN (BUILT_IN_CLOG):
9776 if (validate_arg (arg0, COMPLEX_TYPE)
9777 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9778 return do_mpc_arg1 (arg0, type, mpc_log);
9779 break;
9781 CASE_FLT_FN (BUILT_IN_CSQRT):
9782 if (validate_arg (arg0, COMPLEX_TYPE)
9783 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9784 return do_mpc_arg1 (arg0, type, mpc_sqrt);
9785 break;
9787 CASE_FLT_FN (BUILT_IN_CASIN):
9788 if (validate_arg (arg0, COMPLEX_TYPE)
9789 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9790 return do_mpc_arg1 (arg0, type, mpc_asin);
9791 break;
9793 CASE_FLT_FN (BUILT_IN_CACOS):
9794 if (validate_arg (arg0, COMPLEX_TYPE)
9795 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9796 return do_mpc_arg1 (arg0, type, mpc_acos);
9797 break;
9799 CASE_FLT_FN (BUILT_IN_CATAN):
9800 if (validate_arg (arg0, COMPLEX_TYPE)
9801 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9802 return do_mpc_arg1 (arg0, type, mpc_atan);
9803 break;
9805 CASE_FLT_FN (BUILT_IN_CASINH):
9806 if (validate_arg (arg0, COMPLEX_TYPE)
9807 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9808 return do_mpc_arg1 (arg0, type, mpc_asinh);
9809 break;
9811 CASE_FLT_FN (BUILT_IN_CACOSH):
9812 if (validate_arg (arg0, COMPLEX_TYPE)
9813 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9814 return do_mpc_arg1 (arg0, type, mpc_acosh);
9815 break;
9817 CASE_FLT_FN (BUILT_IN_CATANH):
9818 if (validate_arg (arg0, COMPLEX_TYPE)
9819 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9820 return do_mpc_arg1 (arg0, type, mpc_atanh);
9821 break;
9823 CASE_FLT_FN (BUILT_IN_CABS):
9824 return fold_builtin_cabs (loc, arg0, type, fndecl);
9826 CASE_FLT_FN (BUILT_IN_CARG):
9827 return fold_builtin_carg (loc, arg0, type);
9829 CASE_FLT_FN (BUILT_IN_SQRT):
9830 return fold_builtin_sqrt (loc, arg0, type);
9832 CASE_FLT_FN (BUILT_IN_CBRT):
9833 return fold_builtin_cbrt (loc, arg0, type);
9835 CASE_FLT_FN (BUILT_IN_ASIN):
9836 if (validate_arg (arg0, REAL_TYPE))
9837 return do_mpfr_arg1 (arg0, type, mpfr_asin,
9838 &dconstm1, &dconst1, true);
9839 break;
9841 CASE_FLT_FN (BUILT_IN_ACOS):
9842 if (validate_arg (arg0, REAL_TYPE))
9843 return do_mpfr_arg1 (arg0, type, mpfr_acos,
9844 &dconstm1, &dconst1, true);
9845 break;
9847 CASE_FLT_FN (BUILT_IN_ATAN):
9848 if (validate_arg (arg0, REAL_TYPE))
9849 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
9850 break;
9852 CASE_FLT_FN (BUILT_IN_ASINH):
9853 if (validate_arg (arg0, REAL_TYPE))
9854 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
9855 break;
9857 CASE_FLT_FN (BUILT_IN_ACOSH):
9858 if (validate_arg (arg0, REAL_TYPE))
9859 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
9860 &dconst1, NULL, true);
9861 break;
9863 CASE_FLT_FN (BUILT_IN_ATANH):
9864 if (validate_arg (arg0, REAL_TYPE))
9865 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
9866 &dconstm1, &dconst1, false);
9867 break;
9869 CASE_FLT_FN (BUILT_IN_SIN):
9870 if (validate_arg (arg0, REAL_TYPE))
9871 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
9872 break;
9874 CASE_FLT_FN (BUILT_IN_COS):
9875 return fold_builtin_cos (loc, arg0, type, fndecl);
9877 CASE_FLT_FN (BUILT_IN_TAN):
9878 return fold_builtin_tan (arg0, type);
9880 CASE_FLT_FN (BUILT_IN_CEXP):
9881 return fold_builtin_cexp (loc, arg0, type);
9883 CASE_FLT_FN (BUILT_IN_CEXPI):
9884 if (validate_arg (arg0, REAL_TYPE))
9885 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
9886 break;
9888 CASE_FLT_FN (BUILT_IN_SINH):
9889 if (validate_arg (arg0, REAL_TYPE))
9890 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
9891 break;
9893 CASE_FLT_FN (BUILT_IN_COSH):
9894 return fold_builtin_cosh (loc, arg0, type, fndecl);
9896 CASE_FLT_FN (BUILT_IN_TANH):
9897 if (validate_arg (arg0, REAL_TYPE))
9898 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
9899 break;
9901 CASE_FLT_FN (BUILT_IN_ERF):
9902 if (validate_arg (arg0, REAL_TYPE))
9903 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
9904 break;
9906 CASE_FLT_FN (BUILT_IN_ERFC):
9907 if (validate_arg (arg0, REAL_TYPE))
9908 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
9909 break;
9911 CASE_FLT_FN (BUILT_IN_TGAMMA):
9912 if (validate_arg (arg0, REAL_TYPE))
9913 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
9914 break;
9916 CASE_FLT_FN (BUILT_IN_EXP):
9917 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
9919 CASE_FLT_FN (BUILT_IN_EXP2):
9920 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
9922 CASE_FLT_FN (BUILT_IN_EXP10):
9923 CASE_FLT_FN (BUILT_IN_POW10):
9924 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
9926 CASE_FLT_FN (BUILT_IN_EXPM1):
9927 if (validate_arg (arg0, REAL_TYPE))
9928 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
9929 break;
9931 CASE_FLT_FN (BUILT_IN_LOG):
9932 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
9934 CASE_FLT_FN (BUILT_IN_LOG2):
9935 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
9937 CASE_FLT_FN (BUILT_IN_LOG10):
9938 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
9940 CASE_FLT_FN (BUILT_IN_LOG1P):
9941 if (validate_arg (arg0, REAL_TYPE))
9942 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
9943 &dconstm1, NULL, false);
9944 break;
9946 CASE_FLT_FN (BUILT_IN_J0):
9947 if (validate_arg (arg0, REAL_TYPE))
9948 return do_mpfr_arg1 (arg0, type, mpfr_j0,
9949 NULL, NULL, 0);
9950 break;
9952 CASE_FLT_FN (BUILT_IN_J1):
9953 if (validate_arg (arg0, REAL_TYPE))
9954 return do_mpfr_arg1 (arg0, type, mpfr_j1,
9955 NULL, NULL, 0);
9956 break;
9958 CASE_FLT_FN (BUILT_IN_Y0):
9959 if (validate_arg (arg0, REAL_TYPE))
9960 return do_mpfr_arg1 (arg0, type, mpfr_y0,
9961 &dconst0, NULL, false);
9962 break;
9964 CASE_FLT_FN (BUILT_IN_Y1):
9965 if (validate_arg (arg0, REAL_TYPE))
9966 return do_mpfr_arg1 (arg0, type, mpfr_y1,
9967 &dconst0, NULL, false);
9968 break;
9970 CASE_FLT_FN (BUILT_IN_NAN):
9971 case BUILT_IN_NAND32:
9972 case BUILT_IN_NAND64:
9973 case BUILT_IN_NAND128:
9974 return fold_builtin_nan (arg0, type, true);
9976 CASE_FLT_FN (BUILT_IN_NANS):
9977 return fold_builtin_nan (arg0, type, false);
9979 CASE_FLT_FN (BUILT_IN_FLOOR):
9980 return fold_builtin_floor (loc, fndecl, arg0);
9982 CASE_FLT_FN (BUILT_IN_CEIL):
9983 return fold_builtin_ceil (loc, fndecl, arg0);
9985 CASE_FLT_FN (BUILT_IN_TRUNC):
9986 return fold_builtin_trunc (loc, fndecl, arg0);
9988 CASE_FLT_FN (BUILT_IN_ROUND):
9989 return fold_builtin_round (loc, fndecl, arg0);
9991 CASE_FLT_FN (BUILT_IN_NEARBYINT):
9992 CASE_FLT_FN (BUILT_IN_RINT):
9993 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
9995 CASE_FLT_FN (BUILT_IN_ICEIL):
9996 CASE_FLT_FN (BUILT_IN_LCEIL):
9997 CASE_FLT_FN (BUILT_IN_LLCEIL):
9998 CASE_FLT_FN (BUILT_IN_LFLOOR):
9999 CASE_FLT_FN (BUILT_IN_IFLOOR):
10000 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10001 CASE_FLT_FN (BUILT_IN_IROUND):
10002 CASE_FLT_FN (BUILT_IN_LROUND):
10003 CASE_FLT_FN (BUILT_IN_LLROUND):
10004 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10006 CASE_FLT_FN (BUILT_IN_IRINT):
10007 CASE_FLT_FN (BUILT_IN_LRINT):
10008 CASE_FLT_FN (BUILT_IN_LLRINT):
10009 return fold_fixed_mathfn (loc, fndecl, arg0);
10011 case BUILT_IN_BSWAP16:
10012 case BUILT_IN_BSWAP32:
10013 case BUILT_IN_BSWAP64:
10014 return fold_builtin_bswap (fndecl, arg0);
10016 CASE_INT_FN (BUILT_IN_FFS):
10017 CASE_INT_FN (BUILT_IN_CLZ):
10018 CASE_INT_FN (BUILT_IN_CTZ):
10019 CASE_INT_FN (BUILT_IN_CLRSB):
10020 CASE_INT_FN (BUILT_IN_POPCOUNT):
10021 CASE_INT_FN (BUILT_IN_PARITY):
10022 return fold_builtin_bitop (fndecl, arg0);
10024 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10025 return fold_builtin_signbit (loc, arg0, type);
10027 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10028 return fold_builtin_significand (loc, arg0, type);
10030 CASE_FLT_FN (BUILT_IN_ILOGB):
10031 CASE_FLT_FN (BUILT_IN_LOGB):
10032 return fold_builtin_logb (loc, arg0, type);
10034 case BUILT_IN_ISASCII:
10035 return fold_builtin_isascii (loc, arg0);
10037 case BUILT_IN_TOASCII:
10038 return fold_builtin_toascii (loc, arg0);
10040 case BUILT_IN_ISDIGIT:
10041 return fold_builtin_isdigit (loc, arg0);
10043 CASE_FLT_FN (BUILT_IN_FINITE):
10044 case BUILT_IN_FINITED32:
10045 case BUILT_IN_FINITED64:
10046 case BUILT_IN_FINITED128:
10047 case BUILT_IN_ISFINITE:
10049 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10050 if (ret)
10051 return ret;
10052 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10055 CASE_FLT_FN (BUILT_IN_ISINF):
10056 case BUILT_IN_ISINFD32:
10057 case BUILT_IN_ISINFD64:
10058 case BUILT_IN_ISINFD128:
10060 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10061 if (ret)
10062 return ret;
10063 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10066 case BUILT_IN_ISNORMAL:
10067 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10069 case BUILT_IN_ISINF_SIGN:
10070 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10072 CASE_FLT_FN (BUILT_IN_ISNAN):
10073 case BUILT_IN_ISNAND32:
10074 case BUILT_IN_ISNAND64:
10075 case BUILT_IN_ISNAND128:
10076 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10078 case BUILT_IN_PRINTF:
10079 case BUILT_IN_PRINTF_UNLOCKED:
10080 case BUILT_IN_VPRINTF:
10081 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10083 case BUILT_IN_FREE:
10084 if (integer_zerop (arg0))
10085 return build_empty_stmt (loc);
10086 break;
10088 default:
10089 break;
10092 return NULL_TREE;
10096 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10097 IGNORE is true if the result of the function call is ignored. This
10098 function returns NULL_TREE if no simplification was possible. */
10100 static tree
10101 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10103 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10104 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10106 switch (fcode)
10108 CASE_FLT_FN (BUILT_IN_JN):
10109 if (validate_arg (arg0, INTEGER_TYPE)
10110 && validate_arg (arg1, REAL_TYPE))
10111 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10112 break;
10114 CASE_FLT_FN (BUILT_IN_YN):
10115 if (validate_arg (arg0, INTEGER_TYPE)
10116 && validate_arg (arg1, REAL_TYPE))
10117 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10118 &dconst0, false);
10119 break;
10121 CASE_FLT_FN (BUILT_IN_DREM):
10122 CASE_FLT_FN (BUILT_IN_REMAINDER):
10123 if (validate_arg (arg0, REAL_TYPE)
10124 && validate_arg (arg1, REAL_TYPE))
10125 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10126 break;
10128 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10129 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10130 if (validate_arg (arg0, REAL_TYPE)
10131 && validate_arg (arg1, POINTER_TYPE))
10132 return do_mpfr_lgamma_r (arg0, arg1, type);
10133 break;
10135 CASE_FLT_FN (BUILT_IN_ATAN2):
10136 if (validate_arg (arg0, REAL_TYPE)
10137 && validate_arg (arg1, REAL_TYPE))
10138 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10139 break;
10141 CASE_FLT_FN (BUILT_IN_FDIM):
10142 if (validate_arg (arg0, REAL_TYPE)
10143 && validate_arg (arg1, REAL_TYPE))
10144 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10145 break;
10147 CASE_FLT_FN (BUILT_IN_HYPOT):
10148 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10150 CASE_FLT_FN (BUILT_IN_CPOW):
10151 if (validate_arg (arg0, COMPLEX_TYPE)
10152 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10153 && validate_arg (arg1, COMPLEX_TYPE)
10154 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10155 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10156 break;
10158 CASE_FLT_FN (BUILT_IN_LDEXP):
10159 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10160 CASE_FLT_FN (BUILT_IN_SCALBN):
10161 CASE_FLT_FN (BUILT_IN_SCALBLN):
10162 return fold_builtin_load_exponent (loc, arg0, arg1,
10163 type, /*ldexp=*/false);
10165 CASE_FLT_FN (BUILT_IN_FREXP):
10166 return fold_builtin_frexp (loc, arg0, arg1, type);
10168 CASE_FLT_FN (BUILT_IN_MODF):
10169 return fold_builtin_modf (loc, arg0, arg1, type);
10171 case BUILT_IN_STRSTR:
10172 return fold_builtin_strstr (loc, arg0, arg1, type);
10174 case BUILT_IN_STRSPN:
10175 return fold_builtin_strspn (loc, arg0, arg1);
10177 case BUILT_IN_STRCSPN:
10178 return fold_builtin_strcspn (loc, arg0, arg1);
10180 case BUILT_IN_STRCHR:
10181 case BUILT_IN_INDEX:
10182 return fold_builtin_strchr (loc, arg0, arg1, type);
10184 case BUILT_IN_STRRCHR:
10185 case BUILT_IN_RINDEX:
10186 return fold_builtin_strrchr (loc, arg0, arg1, type);
10188 case BUILT_IN_STPCPY:
10189 if (ignore)
10191 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10192 if (!fn)
10193 break;
10195 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10197 else
10198 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10199 break;
10201 case BUILT_IN_STRCMP:
10202 return fold_builtin_strcmp (loc, arg0, arg1);
10204 case BUILT_IN_STRPBRK:
10205 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10207 case BUILT_IN_EXPECT:
10208 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
10210 CASE_FLT_FN (BUILT_IN_POW):
10211 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10213 CASE_FLT_FN (BUILT_IN_POWI):
10214 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10216 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10217 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10219 CASE_FLT_FN (BUILT_IN_FMIN):
10220 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10222 CASE_FLT_FN (BUILT_IN_FMAX):
10223 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10225 case BUILT_IN_ISGREATER:
10226 return fold_builtin_unordered_cmp (loc, fndecl,
10227 arg0, arg1, UNLE_EXPR, LE_EXPR);
10228 case BUILT_IN_ISGREATEREQUAL:
10229 return fold_builtin_unordered_cmp (loc, fndecl,
10230 arg0, arg1, UNLT_EXPR, LT_EXPR);
10231 case BUILT_IN_ISLESS:
10232 return fold_builtin_unordered_cmp (loc, fndecl,
10233 arg0, arg1, UNGE_EXPR, GE_EXPR);
10234 case BUILT_IN_ISLESSEQUAL:
10235 return fold_builtin_unordered_cmp (loc, fndecl,
10236 arg0, arg1, UNGT_EXPR, GT_EXPR);
10237 case BUILT_IN_ISLESSGREATER:
10238 return fold_builtin_unordered_cmp (loc, fndecl,
10239 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10240 case BUILT_IN_ISUNORDERED:
10241 return fold_builtin_unordered_cmp (loc, fndecl,
10242 arg0, arg1, UNORDERED_EXPR,
10243 NOP_EXPR);
10245 /* We do the folding for va_start in the expander. */
10246 case BUILT_IN_VA_START:
10247 break;
10249 case BUILT_IN_OBJECT_SIZE:
10250 return fold_builtin_object_size (arg0, arg1);
10252 case BUILT_IN_PRINTF:
10253 case BUILT_IN_PRINTF_UNLOCKED:
10254 case BUILT_IN_VPRINTF:
10255 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10257 case BUILT_IN_PRINTF_CHK:
10258 case BUILT_IN_VPRINTF_CHK:
10259 if (!validate_arg (arg0, INTEGER_TYPE)
10260 || TREE_SIDE_EFFECTS (arg0))
10261 return NULL_TREE;
10262 else
10263 return fold_builtin_printf (loc, fndecl,
10264 arg1, NULL_TREE, ignore, fcode);
10265 break;
10267 case BUILT_IN_FPRINTF:
10268 case BUILT_IN_FPRINTF_UNLOCKED:
10269 case BUILT_IN_VFPRINTF:
10270 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10271 ignore, fcode);
10273 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10274 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10276 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10277 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10279 default:
10280 break;
10282 return NULL_TREE;
10285 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10286 and ARG2. IGNORE is true if the result of the function call is ignored.
10287 This function returns NULL_TREE if no simplification was possible. */
10289 static tree
10290 fold_builtin_3 (location_t loc, tree fndecl,
10291 tree arg0, tree arg1, tree arg2, bool ignore)
10293 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10294 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10295 switch (fcode)
10298 CASE_FLT_FN (BUILT_IN_SINCOS):
10299 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10301 CASE_FLT_FN (BUILT_IN_FMA):
10302 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10303 break;
10305 CASE_FLT_FN (BUILT_IN_REMQUO):
10306 if (validate_arg (arg0, REAL_TYPE)
10307 && validate_arg (arg1, REAL_TYPE)
10308 && validate_arg (arg2, POINTER_TYPE))
10309 return do_mpfr_remquo (arg0, arg1, arg2);
10310 break;
10312 case BUILT_IN_STRNCAT:
10313 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10315 case BUILT_IN_STRNCMP:
10316 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10318 case BUILT_IN_MEMCHR:
10319 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10321 case BUILT_IN_BCMP:
10322 case BUILT_IN_MEMCMP:
10323 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10325 case BUILT_IN_PRINTF_CHK:
10326 case BUILT_IN_VPRINTF_CHK:
10327 if (!validate_arg (arg0, INTEGER_TYPE)
10328 || TREE_SIDE_EFFECTS (arg0))
10329 return NULL_TREE;
10330 else
10331 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10332 break;
10334 case BUILT_IN_FPRINTF:
10335 case BUILT_IN_FPRINTF_UNLOCKED:
10336 case BUILT_IN_VFPRINTF:
10337 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10338 ignore, fcode);
10340 case BUILT_IN_FPRINTF_CHK:
10341 case BUILT_IN_VFPRINTF_CHK:
10342 if (!validate_arg (arg1, INTEGER_TYPE)
10343 || TREE_SIDE_EFFECTS (arg1))
10344 return NULL_TREE;
10345 else
10346 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10347 ignore, fcode);
10349 case BUILT_IN_EXPECT:
10350 return fold_builtin_expect (loc, arg0, arg1, arg2);
10352 default:
10353 break;
10355 return NULL_TREE;
10358 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10359 ARG2, and ARG3. IGNORE is true if the result of the function call is
10360 ignored. This function returns NULL_TREE if no simplification was
10361 possible. */
10363 static tree
10364 fold_builtin_4 (location_t loc, tree fndecl,
10365 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10367 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10369 switch (fcode)
10371 case BUILT_IN_FPRINTF_CHK:
10372 case BUILT_IN_VFPRINTF_CHK:
10373 if (!validate_arg (arg1, INTEGER_TYPE)
10374 || TREE_SIDE_EFFECTS (arg1))
10375 return NULL_TREE;
10376 else
10377 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10378 ignore, fcode);
10379 break;
10381 default:
10382 break;
10384 return NULL_TREE;
10387 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10388 arguments, where NARGS <= 4. IGNORE is true if the result of the
10389 function call is ignored. This function returns NULL_TREE if no
10390 simplification was possible. Note that this only folds builtins with
10391 fixed argument patterns. Foldings that do varargs-to-varargs
10392 transformations, or that match calls with more than 4 arguments,
10393 need to be handled with fold_builtin_varargs instead. */
10395 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10397 tree
10398 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10400 tree ret = NULL_TREE;
10402 switch (nargs)
10404 case 0:
10405 ret = fold_builtin_0 (loc, fndecl, ignore);
10406 break;
10407 case 1:
10408 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10409 break;
10410 case 2:
10411 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10412 break;
10413 case 3:
10414 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10415 break;
10416 case 4:
10417 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10418 ignore);
10419 break;
10420 default:
10421 break;
10423 if (ret)
10425 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10426 SET_EXPR_LOCATION (ret, loc);
10427 TREE_NO_WARNING (ret) = 1;
10428 return ret;
10430 return NULL_TREE;
10433 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10434 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10435 of arguments in ARGS to be omitted. OLDNARGS is the number of
10436 elements in ARGS. */
10438 static tree
10439 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10440 int skip, tree fndecl, int n, va_list newargs)
10442 int nargs = oldnargs - skip + n;
10443 tree *buffer;
10445 if (n > 0)
10447 int i, j;
10449 buffer = XALLOCAVEC (tree, nargs);
10450 for (i = 0; i < n; i++)
10451 buffer[i] = va_arg (newargs, tree);
10452 for (j = skip; j < oldnargs; j++, i++)
10453 buffer[i] = args[j];
10455 else
10456 buffer = args + skip;
10458 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10461 /* Return true if FNDECL shouldn't be folded right now.
10462 If a built-in function has an inline attribute always_inline
10463 wrapper, defer folding it after always_inline functions have
10464 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10465 might not be performed. */
10467 bool
10468 avoid_folding_inline_builtin (tree fndecl)
10470 return (DECL_DECLARED_INLINE_P (fndecl)
10471 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10472 && cfun
10473 && !cfun->always_inline_functions_inlined
10474 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10477 /* A wrapper function for builtin folding that prevents warnings for
10478 "statement without effect" and the like, caused by removing the
10479 call node earlier than the warning is generated. */
10481 tree
10482 fold_call_expr (location_t loc, tree exp, bool ignore)
10484 tree ret = NULL_TREE;
10485 tree fndecl = get_callee_fndecl (exp);
10486 if (fndecl
10487 && TREE_CODE (fndecl) == FUNCTION_DECL
10488 && DECL_BUILT_IN (fndecl)
10489 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10490 yet. Defer folding until we see all the arguments
10491 (after inlining). */
10492 && !CALL_EXPR_VA_ARG_PACK (exp))
10494 int nargs = call_expr_nargs (exp);
10496 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10497 instead last argument is __builtin_va_arg_pack (). Defer folding
10498 even in that case, until arguments are finalized. */
10499 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10501 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10502 if (fndecl2
10503 && TREE_CODE (fndecl2) == FUNCTION_DECL
10504 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10505 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10506 return NULL_TREE;
10509 if (avoid_folding_inline_builtin (fndecl))
10510 return NULL_TREE;
10512 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10513 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10514 CALL_EXPR_ARGP (exp), ignore);
10515 else
10517 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10519 tree *args = CALL_EXPR_ARGP (exp);
10520 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10522 if (!ret)
10523 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10524 if (ret)
10525 return ret;
10528 return NULL_TREE;
10531 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10532 N arguments are passed in the array ARGARRAY. */
10534 tree
10535 fold_builtin_call_array (location_t loc, tree type,
10536 tree fn,
10537 int n,
10538 tree *argarray)
10540 tree ret = NULL_TREE;
10541 tree exp;
10543 if (TREE_CODE (fn) == ADDR_EXPR)
10545 tree fndecl = TREE_OPERAND (fn, 0);
10546 if (TREE_CODE (fndecl) == FUNCTION_DECL
10547 && DECL_BUILT_IN (fndecl))
10549 /* If last argument is __builtin_va_arg_pack (), arguments to this
10550 function are not finalized yet. Defer folding until they are. */
10551 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10553 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10554 if (fndecl2
10555 && TREE_CODE (fndecl2) == FUNCTION_DECL
10556 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10557 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10558 return build_call_array_loc (loc, type, fn, n, argarray);
10560 if (avoid_folding_inline_builtin (fndecl))
10561 return build_call_array_loc (loc, type, fn, n, argarray);
10562 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10564 ret = targetm.fold_builtin (fndecl, n, argarray, false);
10565 if (ret)
10566 return ret;
10568 return build_call_array_loc (loc, type, fn, n, argarray);
10570 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10572 /* First try the transformations that don't require consing up
10573 an exp. */
10574 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10575 if (ret)
10576 return ret;
10579 /* If we got this far, we need to build an exp. */
10580 exp = build_call_array_loc (loc, type, fn, n, argarray);
10581 ret = fold_builtin_varargs (loc, fndecl, exp, false);
10582 return ret ? ret : exp;
10586 return build_call_array_loc (loc, type, fn, n, argarray);
10589 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10590 along with N new arguments specified as the "..." parameters. SKIP
10591 is the number of arguments in EXP to be omitted. This function is used
10592 to do varargs-to-varargs transformations. */
10594 static tree
10595 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10597 va_list ap;
10598 tree t;
10600 va_start (ap, n);
10601 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10602 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10603 va_end (ap);
10605 return t;
10608 /* Validate a single argument ARG against a tree code CODE representing
10609 a type. */
10611 static bool
10612 validate_arg (const_tree arg, enum tree_code code)
10614 if (!arg)
10615 return false;
10616 else if (code == POINTER_TYPE)
10617 return POINTER_TYPE_P (TREE_TYPE (arg));
10618 else if (code == INTEGER_TYPE)
10619 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10620 return code == TREE_CODE (TREE_TYPE (arg));
10623 /* This function validates the types of a function call argument list
10624 against a specified list of tree_codes. If the last specifier is a 0,
10625 that represents an ellipses, otherwise the last specifier must be a
10626 VOID_TYPE.
10628 This is the GIMPLE version of validate_arglist. Eventually we want to
10629 completely convert builtins.c to work from GIMPLEs and the tree based
10630 validate_arglist will then be removed. */
10632 bool
10633 validate_gimple_arglist (const_gimple call, ...)
10635 enum tree_code code;
10636 bool res = 0;
10637 va_list ap;
10638 const_tree arg;
10639 size_t i;
10641 va_start (ap, call);
10642 i = 0;
10646 code = (enum tree_code) va_arg (ap, int);
10647 switch (code)
10649 case 0:
10650 /* This signifies an ellipses, any further arguments are all ok. */
10651 res = true;
10652 goto end;
10653 case VOID_TYPE:
10654 /* This signifies an endlink, if no arguments remain, return
10655 true, otherwise return false. */
10656 res = (i == gimple_call_num_args (call));
10657 goto end;
10658 default:
10659 /* If no parameters remain or the parameter's code does not
10660 match the specified code, return false. Otherwise continue
10661 checking any remaining arguments. */
10662 arg = gimple_call_arg (call, i++);
10663 if (!validate_arg (arg, code))
10664 goto end;
10665 break;
10668 while (1);
10670 /* We need gotos here since we can only have one VA_CLOSE in a
10671 function. */
10672 end: ;
10673 va_end (ap);
10675 return res;
10678 /* Default target-specific builtin expander that does nothing. */
10681 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10682 rtx target ATTRIBUTE_UNUSED,
10683 rtx subtarget ATTRIBUTE_UNUSED,
10684 enum machine_mode mode ATTRIBUTE_UNUSED,
10685 int ignore ATTRIBUTE_UNUSED)
10687 return NULL_RTX;
10690 /* Returns true is EXP represents data that would potentially reside
10691 in a readonly section. */
10693 bool
10694 readonly_data_expr (tree exp)
10696 STRIP_NOPS (exp);
10698 if (TREE_CODE (exp) != ADDR_EXPR)
10699 return false;
10701 exp = get_base_address (TREE_OPERAND (exp, 0));
10702 if (!exp)
10703 return false;
10705 /* Make sure we call decl_readonly_section only for trees it
10706 can handle (since it returns true for everything it doesn't
10707 understand). */
10708 if (TREE_CODE (exp) == STRING_CST
10709 || TREE_CODE (exp) == CONSTRUCTOR
10710 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
10711 return decl_readonly_section (exp, 0);
10712 else
10713 return false;
10716 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10717 to the call, and TYPE is its return type.
10719 Return NULL_TREE if no simplification was possible, otherwise return the
10720 simplified form of the call as a tree.
10722 The simplified form may be a constant or other expression which
10723 computes the same value, but in a more efficient manner (including
10724 calls to other builtin functions).
10726 The call may contain arguments which need to be evaluated, but
10727 which are not useful to determine the result of the call. In
10728 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10729 COMPOUND_EXPR will be an argument which must be evaluated.
10730 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10731 COMPOUND_EXPR in the chain will contain the tree for the simplified
10732 form of the builtin function call. */
10734 static tree
10735 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
10737 if (!validate_arg (s1, POINTER_TYPE)
10738 || !validate_arg (s2, POINTER_TYPE))
10739 return NULL_TREE;
10740 else
10742 tree fn;
10743 const char *p1, *p2;
10745 p2 = c_getstr (s2);
10746 if (p2 == NULL)
10747 return NULL_TREE;
10749 p1 = c_getstr (s1);
10750 if (p1 != NULL)
10752 const char *r = strstr (p1, p2);
10753 tree tem;
10755 if (r == NULL)
10756 return build_int_cst (TREE_TYPE (s1), 0);
10758 /* Return an offset into the constant string argument. */
10759 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10760 return fold_convert_loc (loc, type, tem);
10763 /* The argument is const char *, and the result is char *, so we need
10764 a type conversion here to avoid a warning. */
10765 if (p2[0] == '\0')
10766 return fold_convert_loc (loc, type, s1);
10768 if (p2[1] != '\0')
10769 return NULL_TREE;
10771 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10772 if (!fn)
10773 return NULL_TREE;
10775 /* New argument list transforming strstr(s1, s2) to
10776 strchr(s1, s2[0]). */
10777 return build_call_expr_loc (loc, fn, 2, s1,
10778 build_int_cst (integer_type_node, p2[0]));
10782 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10783 the call, and TYPE is its return type.
10785 Return NULL_TREE if no simplification was possible, otherwise return the
10786 simplified form of the call as a tree.
10788 The simplified form may be a constant or other expression which
10789 computes the same value, but in a more efficient manner (including
10790 calls to other builtin functions).
10792 The call may contain arguments which need to be evaluated, but
10793 which are not useful to determine the result of the call. In
10794 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10795 COMPOUND_EXPR will be an argument which must be evaluated.
10796 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10797 COMPOUND_EXPR in the chain will contain the tree for the simplified
10798 form of the builtin function call. */
10800 static tree
10801 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
10803 if (!validate_arg (s1, POINTER_TYPE)
10804 || !validate_arg (s2, INTEGER_TYPE))
10805 return NULL_TREE;
10806 else
10808 const char *p1;
10810 if (TREE_CODE (s2) != INTEGER_CST)
10811 return NULL_TREE;
10813 p1 = c_getstr (s1);
10814 if (p1 != NULL)
10816 char c;
10817 const char *r;
10818 tree tem;
10820 if (target_char_cast (s2, &c))
10821 return NULL_TREE;
10823 r = strchr (p1, c);
10825 if (r == NULL)
10826 return build_int_cst (TREE_TYPE (s1), 0);
10828 /* Return an offset into the constant string argument. */
10829 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10830 return fold_convert_loc (loc, type, tem);
10832 return NULL_TREE;
10836 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10837 the call, and TYPE is its return type.
10839 Return NULL_TREE if no simplification was possible, otherwise return the
10840 simplified form of the call as a tree.
10842 The simplified form may be a constant or other expression which
10843 computes the same value, but in a more efficient manner (including
10844 calls to other builtin functions).
10846 The call may contain arguments which need to be evaluated, but
10847 which are not useful to determine the result of the call. In
10848 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10849 COMPOUND_EXPR will be an argument which must be evaluated.
10850 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10851 COMPOUND_EXPR in the chain will contain the tree for the simplified
10852 form of the builtin function call. */
10854 static tree
10855 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
10857 if (!validate_arg (s1, POINTER_TYPE)
10858 || !validate_arg (s2, INTEGER_TYPE))
10859 return NULL_TREE;
10860 else
10862 tree fn;
10863 const char *p1;
10865 if (TREE_CODE (s2) != INTEGER_CST)
10866 return NULL_TREE;
10868 p1 = c_getstr (s1);
10869 if (p1 != NULL)
10871 char c;
10872 const char *r;
10873 tree tem;
10875 if (target_char_cast (s2, &c))
10876 return NULL_TREE;
10878 r = strrchr (p1, c);
10880 if (r == NULL)
10881 return build_int_cst (TREE_TYPE (s1), 0);
10883 /* Return an offset into the constant string argument. */
10884 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10885 return fold_convert_loc (loc, type, tem);
10888 if (! integer_zerop (s2))
10889 return NULL_TREE;
10891 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10892 if (!fn)
10893 return NULL_TREE;
10895 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
10896 return build_call_expr_loc (loc, fn, 2, s1, s2);
10900 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
10901 to the call, and TYPE is its return type.
10903 Return NULL_TREE if no simplification was possible, otherwise return the
10904 simplified form of the call as a tree.
10906 The simplified form may be a constant or other expression which
10907 computes the same value, but in a more efficient manner (including
10908 calls to other builtin functions).
10910 The call may contain arguments which need to be evaluated, but
10911 which are not useful to determine the result of the call. In
10912 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10913 COMPOUND_EXPR will be an argument which must be evaluated.
10914 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10915 COMPOUND_EXPR in the chain will contain the tree for the simplified
10916 form of the builtin function call. */
10918 static tree
10919 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
10921 if (!validate_arg (s1, POINTER_TYPE)
10922 || !validate_arg (s2, POINTER_TYPE))
10923 return NULL_TREE;
10924 else
10926 tree fn;
10927 const char *p1, *p2;
10929 p2 = c_getstr (s2);
10930 if (p2 == NULL)
10931 return NULL_TREE;
10933 p1 = c_getstr (s1);
10934 if (p1 != NULL)
10936 const char *r = strpbrk (p1, p2);
10937 tree tem;
10939 if (r == NULL)
10940 return build_int_cst (TREE_TYPE (s1), 0);
10942 /* Return an offset into the constant string argument. */
10943 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10944 return fold_convert_loc (loc, type, tem);
10947 if (p2[0] == '\0')
10948 /* strpbrk(x, "") == NULL.
10949 Evaluate and ignore s1 in case it had side-effects. */
10950 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
10952 if (p2[1] != '\0')
10953 return NULL_TREE; /* Really call strpbrk. */
10955 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10956 if (!fn)
10957 return NULL_TREE;
10959 /* New argument list transforming strpbrk(s1, s2) to
10960 strchr(s1, s2[0]). */
10961 return build_call_expr_loc (loc, fn, 2, s1,
10962 build_int_cst (integer_type_node, p2[0]));
10966 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
10967 arguments to the call.
10969 Return NULL_TREE if no simplification was possible, otherwise return the
10970 simplified form of the call as a tree.
10972 The simplified form may be a constant or other expression which
10973 computes the same value, but in a more efficient manner (including
10974 calls to other builtin functions).
10976 The call may contain arguments which need to be evaluated, but
10977 which are not useful to determine the result of the call. In
10978 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10979 COMPOUND_EXPR will be an argument which must be evaluated.
10980 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10981 COMPOUND_EXPR in the chain will contain the tree for the simplified
10982 form of the builtin function call. */
10984 static tree
10985 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
10987 if (!validate_arg (dst, POINTER_TYPE)
10988 || !validate_arg (src, POINTER_TYPE)
10989 || !validate_arg (len, INTEGER_TYPE))
10990 return NULL_TREE;
10991 else
10993 const char *p = c_getstr (src);
10995 /* If the requested length is zero, or the src parameter string
10996 length is zero, return the dst parameter. */
10997 if (integer_zerop (len) || (p && *p == '\0'))
10998 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11000 /* If the requested len is greater than or equal to the string
11001 length, call strcat. */
11002 if (TREE_CODE (len) == INTEGER_CST && p
11003 && compare_tree_int (len, strlen (p)) >= 0)
11005 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11007 /* If the replacement _DECL isn't initialized, don't do the
11008 transformation. */
11009 if (!fn)
11010 return NULL_TREE;
11012 return build_call_expr_loc (loc, fn, 2, dst, src);
11014 return NULL_TREE;
11018 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11019 to the call.
11021 Return NULL_TREE if no simplification was possible, otherwise return the
11022 simplified form of the call as a tree.
11024 The simplified form may be a constant or other expression which
11025 computes the same value, but in a more efficient manner (including
11026 calls to other builtin functions).
11028 The call may contain arguments which need to be evaluated, but
11029 which are not useful to determine the result of the call. In
11030 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11031 COMPOUND_EXPR will be an argument which must be evaluated.
11032 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11033 COMPOUND_EXPR in the chain will contain the tree for the simplified
11034 form of the builtin function call. */
11036 static tree
11037 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11039 if (!validate_arg (s1, POINTER_TYPE)
11040 || !validate_arg (s2, POINTER_TYPE))
11041 return NULL_TREE;
11042 else
11044 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11046 /* If both arguments are constants, evaluate at compile-time. */
11047 if (p1 && p2)
11049 const size_t r = strspn (p1, p2);
11050 return build_int_cst (size_type_node, r);
11053 /* If either argument is "", return NULL_TREE. */
11054 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11055 /* Evaluate and ignore both arguments in case either one has
11056 side-effects. */
11057 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11058 s1, s2);
11059 return NULL_TREE;
11063 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11064 to the call.
11066 Return NULL_TREE if no simplification was possible, otherwise return the
11067 simplified form of the call as a tree.
11069 The simplified form may be a constant or other expression which
11070 computes the same value, but in a more efficient manner (including
11071 calls to other builtin functions).
11073 The call may contain arguments which need to be evaluated, but
11074 which are not useful to determine the result of the call. In
11075 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11076 COMPOUND_EXPR will be an argument which must be evaluated.
11077 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11078 COMPOUND_EXPR in the chain will contain the tree for the simplified
11079 form of the builtin function call. */
11081 static tree
11082 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11084 if (!validate_arg (s1, POINTER_TYPE)
11085 || !validate_arg (s2, POINTER_TYPE))
11086 return NULL_TREE;
11087 else
11089 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11091 /* If both arguments are constants, evaluate at compile-time. */
11092 if (p1 && p2)
11094 const size_t r = strcspn (p1, p2);
11095 return build_int_cst (size_type_node, r);
11098 /* If the first argument is "", return NULL_TREE. */
11099 if (p1 && *p1 == '\0')
11101 /* Evaluate and ignore argument s2 in case it has
11102 side-effects. */
11103 return omit_one_operand_loc (loc, size_type_node,
11104 size_zero_node, s2);
11107 /* If the second argument is "", return __builtin_strlen(s1). */
11108 if (p2 && *p2 == '\0')
11110 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11112 /* If the replacement _DECL isn't initialized, don't do the
11113 transformation. */
11114 if (!fn)
11115 return NULL_TREE;
11117 return build_call_expr_loc (loc, fn, 1, s1);
11119 return NULL_TREE;
11123 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11124 produced. False otherwise. This is done so that we don't output the error
11125 or warning twice or three times. */
11127 bool
11128 fold_builtin_next_arg (tree exp, bool va_start_p)
11130 tree fntype = TREE_TYPE (current_function_decl);
11131 int nargs = call_expr_nargs (exp);
11132 tree arg;
11133 /* There is good chance the current input_location points inside the
11134 definition of the va_start macro (perhaps on the token for
11135 builtin) in a system header, so warnings will not be emitted.
11136 Use the location in real source code. */
11137 source_location current_location =
11138 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11139 NULL);
11141 if (!stdarg_p (fntype))
11143 error ("%<va_start%> used in function with fixed args");
11144 return true;
11147 if (va_start_p)
11149 if (va_start_p && (nargs != 2))
11151 error ("wrong number of arguments to function %<va_start%>");
11152 return true;
11154 arg = CALL_EXPR_ARG (exp, 1);
11156 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11157 when we checked the arguments and if needed issued a warning. */
11158 else
11160 if (nargs == 0)
11162 /* Evidently an out of date version of <stdarg.h>; can't validate
11163 va_start's second argument, but can still work as intended. */
11164 warning_at (current_location,
11165 OPT_Wvarargs,
11166 "%<__builtin_next_arg%> called without an argument");
11167 return true;
11169 else if (nargs > 1)
11171 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11172 return true;
11174 arg = CALL_EXPR_ARG (exp, 0);
11177 if (TREE_CODE (arg) == SSA_NAME)
11178 arg = SSA_NAME_VAR (arg);
11180 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11181 or __builtin_next_arg (0) the first time we see it, after checking
11182 the arguments and if needed issuing a warning. */
11183 if (!integer_zerop (arg))
11185 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11187 /* Strip off all nops for the sake of the comparison. This
11188 is not quite the same as STRIP_NOPS. It does more.
11189 We must also strip off INDIRECT_EXPR for C++ reference
11190 parameters. */
11191 while (CONVERT_EXPR_P (arg)
11192 || TREE_CODE (arg) == INDIRECT_REF)
11193 arg = TREE_OPERAND (arg, 0);
11194 if (arg != last_parm)
11196 /* FIXME: Sometimes with the tree optimizers we can get the
11197 not the last argument even though the user used the last
11198 argument. We just warn and set the arg to be the last
11199 argument so that we will get wrong-code because of
11200 it. */
11201 warning_at (current_location,
11202 OPT_Wvarargs,
11203 "second parameter of %<va_start%> not last named argument");
11206 /* Undefined by C99 7.15.1.4p4 (va_start):
11207 "If the parameter parmN is declared with the register storage
11208 class, with a function or array type, or with a type that is
11209 not compatible with the type that results after application of
11210 the default argument promotions, the behavior is undefined."
11212 else if (DECL_REGISTER (arg))
11214 warning_at (current_location,
11215 OPT_Wvarargs,
11216 "undefined behaviour when second parameter of "
11217 "%<va_start%> is declared with %<register%> storage");
11220 /* We want to verify the second parameter just once before the tree
11221 optimizers are run and then avoid keeping it in the tree,
11222 as otherwise we could warn even for correct code like:
11223 void foo (int i, ...)
11224 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11225 if (va_start_p)
11226 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11227 else
11228 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11230 return false;
11234 /* Expand a call EXP to __builtin_object_size. */
11236 static rtx
11237 expand_builtin_object_size (tree exp)
11239 tree ost;
11240 int object_size_type;
11241 tree fndecl = get_callee_fndecl (exp);
11243 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11245 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11246 exp, fndecl);
11247 expand_builtin_trap ();
11248 return const0_rtx;
11251 ost = CALL_EXPR_ARG (exp, 1);
11252 STRIP_NOPS (ost);
11254 if (TREE_CODE (ost) != INTEGER_CST
11255 || tree_int_cst_sgn (ost) < 0
11256 || compare_tree_int (ost, 3) > 0)
11258 error ("%Klast argument of %D is not integer constant between 0 and 3",
11259 exp, fndecl);
11260 expand_builtin_trap ();
11261 return const0_rtx;
11264 object_size_type = tree_to_shwi (ost);
11266 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11269 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11270 FCODE is the BUILT_IN_* to use.
11271 Return NULL_RTX if we failed; the caller should emit a normal call,
11272 otherwise try to get the result in TARGET, if convenient (and in
11273 mode MODE if that's convenient). */
11275 static rtx
11276 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
11277 enum built_in_function fcode)
11279 tree dest, src, len, size;
11281 if (!validate_arglist (exp,
11282 POINTER_TYPE,
11283 fcode == BUILT_IN_MEMSET_CHK
11284 ? INTEGER_TYPE : POINTER_TYPE,
11285 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11286 return NULL_RTX;
11288 dest = CALL_EXPR_ARG (exp, 0);
11289 src = CALL_EXPR_ARG (exp, 1);
11290 len = CALL_EXPR_ARG (exp, 2);
11291 size = CALL_EXPR_ARG (exp, 3);
11293 if (! tree_fits_uhwi_p (size))
11294 return NULL_RTX;
11296 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11298 tree fn;
11300 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11302 warning_at (tree_nonartificial_location (exp),
11303 0, "%Kcall to %D will always overflow destination buffer",
11304 exp, get_callee_fndecl (exp));
11305 return NULL_RTX;
11308 fn = NULL_TREE;
11309 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11310 mem{cpy,pcpy,move,set} is available. */
11311 switch (fcode)
11313 case BUILT_IN_MEMCPY_CHK:
11314 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11315 break;
11316 case BUILT_IN_MEMPCPY_CHK:
11317 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11318 break;
11319 case BUILT_IN_MEMMOVE_CHK:
11320 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11321 break;
11322 case BUILT_IN_MEMSET_CHK:
11323 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11324 break;
11325 default:
11326 break;
11329 if (! fn)
11330 return NULL_RTX;
11332 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11333 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11334 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11335 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11337 else if (fcode == BUILT_IN_MEMSET_CHK)
11338 return NULL_RTX;
11339 else
11341 unsigned int dest_align = get_pointer_alignment (dest);
11343 /* If DEST is not a pointer type, call the normal function. */
11344 if (dest_align == 0)
11345 return NULL_RTX;
11347 /* If SRC and DEST are the same (and not volatile), do nothing. */
11348 if (operand_equal_p (src, dest, 0))
11350 tree expr;
11352 if (fcode != BUILT_IN_MEMPCPY_CHK)
11354 /* Evaluate and ignore LEN in case it has side-effects. */
11355 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11356 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11359 expr = fold_build_pointer_plus (dest, len);
11360 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11363 /* __memmove_chk special case. */
11364 if (fcode == BUILT_IN_MEMMOVE_CHK)
11366 unsigned int src_align = get_pointer_alignment (src);
11368 if (src_align == 0)
11369 return NULL_RTX;
11371 /* If src is categorized for a readonly section we can use
11372 normal __memcpy_chk. */
11373 if (readonly_data_expr (src))
11375 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11376 if (!fn)
11377 return NULL_RTX;
11378 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11379 dest, src, len, size);
11380 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11381 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11382 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11385 return NULL_RTX;
11389 /* Emit warning if a buffer overflow is detected at compile time. */
11391 static void
11392 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11394 int is_strlen = 0;
11395 tree len, size;
11396 location_t loc = tree_nonartificial_location (exp);
11398 switch (fcode)
11400 case BUILT_IN_STRCPY_CHK:
11401 case BUILT_IN_STPCPY_CHK:
11402 /* For __strcat_chk the warning will be emitted only if overflowing
11403 by at least strlen (dest) + 1 bytes. */
11404 case BUILT_IN_STRCAT_CHK:
11405 len = CALL_EXPR_ARG (exp, 1);
11406 size = CALL_EXPR_ARG (exp, 2);
11407 is_strlen = 1;
11408 break;
11409 case BUILT_IN_STRNCAT_CHK:
11410 case BUILT_IN_STRNCPY_CHK:
11411 case BUILT_IN_STPNCPY_CHK:
11412 len = CALL_EXPR_ARG (exp, 2);
11413 size = CALL_EXPR_ARG (exp, 3);
11414 break;
11415 case BUILT_IN_SNPRINTF_CHK:
11416 case BUILT_IN_VSNPRINTF_CHK:
11417 len = CALL_EXPR_ARG (exp, 1);
11418 size = CALL_EXPR_ARG (exp, 3);
11419 break;
11420 default:
11421 gcc_unreachable ();
11424 if (!len || !size)
11425 return;
11427 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11428 return;
11430 if (is_strlen)
11432 len = c_strlen (len, 1);
11433 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11434 return;
11436 else if (fcode == BUILT_IN_STRNCAT_CHK)
11438 tree src = CALL_EXPR_ARG (exp, 1);
11439 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11440 return;
11441 src = c_strlen (src, 1);
11442 if (! src || ! tree_fits_uhwi_p (src))
11444 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
11445 exp, get_callee_fndecl (exp));
11446 return;
11448 else if (tree_int_cst_lt (src, size))
11449 return;
11451 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
11452 return;
11454 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
11455 exp, get_callee_fndecl (exp));
11458 /* Emit warning if a buffer overflow is detected at compile time
11459 in __sprintf_chk/__vsprintf_chk calls. */
11461 static void
11462 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11464 tree size, len, fmt;
11465 const char *fmt_str;
11466 int nargs = call_expr_nargs (exp);
11468 /* Verify the required arguments in the original call. */
11470 if (nargs < 4)
11471 return;
11472 size = CALL_EXPR_ARG (exp, 2);
11473 fmt = CALL_EXPR_ARG (exp, 3);
11475 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11476 return;
11478 /* Check whether the format is a literal string constant. */
11479 fmt_str = c_getstr (fmt);
11480 if (fmt_str == NULL)
11481 return;
11483 if (!init_target_chars ())
11484 return;
11486 /* If the format doesn't contain % args or %%, we know its size. */
11487 if (strchr (fmt_str, target_percent) == 0)
11488 len = build_int_cstu (size_type_node, strlen (fmt_str));
11489 /* If the format is "%s" and first ... argument is a string literal,
11490 we know it too. */
11491 else if (fcode == BUILT_IN_SPRINTF_CHK
11492 && strcmp (fmt_str, target_percent_s) == 0)
11494 tree arg;
11496 if (nargs < 5)
11497 return;
11498 arg = CALL_EXPR_ARG (exp, 4);
11499 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11500 return;
11502 len = c_strlen (arg, 1);
11503 if (!len || ! tree_fits_uhwi_p (len))
11504 return;
11506 else
11507 return;
11509 if (! tree_int_cst_lt (len, size))
11510 warning_at (tree_nonartificial_location (exp),
11511 0, "%Kcall to %D will always overflow destination buffer",
11512 exp, get_callee_fndecl (exp));
11515 /* Emit warning if a free is called with address of a variable. */
11517 static void
11518 maybe_emit_free_warning (tree exp)
11520 tree arg = CALL_EXPR_ARG (exp, 0);
11522 STRIP_NOPS (arg);
11523 if (TREE_CODE (arg) != ADDR_EXPR)
11524 return;
11526 arg = get_base_address (TREE_OPERAND (arg, 0));
11527 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
11528 return;
11530 if (SSA_VAR_P (arg))
11531 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11532 "%Kattempt to free a non-heap object %qD", exp, arg);
11533 else
11534 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11535 "%Kattempt to free a non-heap object", exp);
11538 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11539 if possible. */
11541 static tree
11542 fold_builtin_object_size (tree ptr, tree ost)
11544 unsigned HOST_WIDE_INT bytes;
11545 int object_size_type;
11547 if (!validate_arg (ptr, POINTER_TYPE)
11548 || !validate_arg (ost, INTEGER_TYPE))
11549 return NULL_TREE;
11551 STRIP_NOPS (ost);
11553 if (TREE_CODE (ost) != INTEGER_CST
11554 || tree_int_cst_sgn (ost) < 0
11555 || compare_tree_int (ost, 3) > 0)
11556 return NULL_TREE;
11558 object_size_type = tree_to_shwi (ost);
11560 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11561 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11562 and (size_t) 0 for types 2 and 3. */
11563 if (TREE_SIDE_EFFECTS (ptr))
11564 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11566 if (TREE_CODE (ptr) == ADDR_EXPR)
11568 bytes = compute_builtin_object_size (ptr, object_size_type);
11569 if (wi::fits_to_tree_p (bytes, size_type_node))
11570 return build_int_cstu (size_type_node, bytes);
11572 else if (TREE_CODE (ptr) == SSA_NAME)
11574 /* If object size is not known yet, delay folding until
11575 later. Maybe subsequent passes will help determining
11576 it. */
11577 bytes = compute_builtin_object_size (ptr, object_size_type);
11578 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
11579 && wi::fits_to_tree_p (bytes, size_type_node))
11580 return build_int_cstu (size_type_node, bytes);
11583 return NULL_TREE;
11586 /* Builtins with folding operations that operate on "..." arguments
11587 need special handling; we need to store the arguments in a convenient
11588 data structure before attempting any folding. Fortunately there are
11589 only a few builtins that fall into this category. FNDECL is the
11590 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11591 result of the function call is ignored. */
11593 static tree
11594 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11595 bool ignore ATTRIBUTE_UNUSED)
11597 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11598 tree ret = NULL_TREE;
11600 switch (fcode)
11602 case BUILT_IN_FPCLASSIFY:
11603 ret = fold_builtin_fpclassify (loc, exp);
11604 break;
11606 default:
11607 break;
11609 if (ret)
11611 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11612 SET_EXPR_LOCATION (ret, loc);
11613 TREE_NO_WARNING (ret) = 1;
11614 return ret;
11616 return NULL_TREE;
11619 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11620 FMT and ARG are the arguments to the call; we don't fold cases with
11621 more than 2 arguments, and ARG may be null if this is a 1-argument case.
11623 Return NULL_TREE if no simplification was possible, otherwise return the
11624 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11625 code of the function to be simplified. */
11627 static tree
11628 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
11629 tree arg, bool ignore,
11630 enum built_in_function fcode)
11632 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
11633 const char *fmt_str = NULL;
11635 /* If the return value is used, don't do the transformation. */
11636 if (! ignore)
11637 return NULL_TREE;
11639 /* Verify the required arguments in the original call. */
11640 if (!validate_arg (fmt, POINTER_TYPE))
11641 return NULL_TREE;
11643 /* Check whether the format is a literal string constant. */
11644 fmt_str = c_getstr (fmt);
11645 if (fmt_str == NULL)
11646 return NULL_TREE;
11648 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
11650 /* If we're using an unlocked function, assume the other
11651 unlocked functions exist explicitly. */
11652 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
11653 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
11655 else
11657 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
11658 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
11661 if (!init_target_chars ())
11662 return NULL_TREE;
11664 if (strcmp (fmt_str, target_percent_s) == 0
11665 || strchr (fmt_str, target_percent) == NULL)
11667 const char *str;
11669 if (strcmp (fmt_str, target_percent_s) == 0)
11671 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11672 return NULL_TREE;
11674 if (!arg || !validate_arg (arg, POINTER_TYPE))
11675 return NULL_TREE;
11677 str = c_getstr (arg);
11678 if (str == NULL)
11679 return NULL_TREE;
11681 else
11683 /* The format specifier doesn't contain any '%' characters. */
11684 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
11685 && arg)
11686 return NULL_TREE;
11687 str = fmt_str;
11690 /* If the string was "", printf does nothing. */
11691 if (str[0] == '\0')
11692 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11694 /* If the string has length of 1, call putchar. */
11695 if (str[1] == '\0')
11697 /* Given printf("c"), (where c is any one character,)
11698 convert "c"[0] to an int and pass that to the replacement
11699 function. */
11700 newarg = build_int_cst (integer_type_node, str[0]);
11701 if (fn_putchar)
11702 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
11704 else
11706 /* If the string was "string\n", call puts("string"). */
11707 size_t len = strlen (str);
11708 if ((unsigned char)str[len - 1] == target_newline
11709 && (size_t) (int) len == len
11710 && (int) len > 0)
11712 char *newstr;
11713 tree offset_node, string_cst;
11715 /* Create a NUL-terminated string that's one char shorter
11716 than the original, stripping off the trailing '\n'. */
11717 newarg = build_string_literal (len, str);
11718 string_cst = string_constant (newarg, &offset_node);
11719 gcc_checking_assert (string_cst
11720 && (TREE_STRING_LENGTH (string_cst)
11721 == (int) len)
11722 && integer_zerop (offset_node)
11723 && (unsigned char)
11724 TREE_STRING_POINTER (string_cst)[len - 1]
11725 == target_newline);
11726 /* build_string_literal creates a new STRING_CST,
11727 modify it in place to avoid double copying. */
11728 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
11729 newstr[len - 1] = '\0';
11730 if (fn_puts)
11731 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
11733 else
11734 /* We'd like to arrange to call fputs(string,stdout) here,
11735 but we need stdout and don't have a way to get it yet. */
11736 return NULL_TREE;
11740 /* The other optimizations can be done only on the non-va_list variants. */
11741 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
11742 return NULL_TREE;
11744 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11745 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
11747 if (!arg || !validate_arg (arg, POINTER_TYPE))
11748 return NULL_TREE;
11749 if (fn_puts)
11750 call = build_call_expr_loc (loc, fn_puts, 1, arg);
11753 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11754 else if (strcmp (fmt_str, target_percent_c) == 0)
11756 if (!arg || !validate_arg (arg, INTEGER_TYPE))
11757 return NULL_TREE;
11758 if (fn_putchar)
11759 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
11762 if (!call)
11763 return NULL_TREE;
11765 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
11768 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11769 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
11770 more than 3 arguments, and ARG may be null in the 2-argument case.
11772 Return NULL_TREE if no simplification was possible, otherwise return the
11773 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11774 code of the function to be simplified. */
11776 static tree
11777 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
11778 tree fmt, tree arg, bool ignore,
11779 enum built_in_function fcode)
11781 tree fn_fputc, fn_fputs, call = NULL_TREE;
11782 const char *fmt_str = NULL;
11784 /* If the return value is used, don't do the transformation. */
11785 if (! ignore)
11786 return NULL_TREE;
11788 /* Verify the required arguments in the original call. */
11789 if (!validate_arg (fp, POINTER_TYPE))
11790 return NULL_TREE;
11791 if (!validate_arg (fmt, POINTER_TYPE))
11792 return NULL_TREE;
11794 /* Check whether the format is a literal string constant. */
11795 fmt_str = c_getstr (fmt);
11796 if (fmt_str == NULL)
11797 return NULL_TREE;
11799 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
11801 /* If we're using an unlocked function, assume the other
11802 unlocked functions exist explicitly. */
11803 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
11804 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
11806 else
11808 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
11809 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
11812 if (!init_target_chars ())
11813 return NULL_TREE;
11815 /* If the format doesn't contain % args or %%, use strcpy. */
11816 if (strchr (fmt_str, target_percent) == NULL)
11818 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
11819 && arg)
11820 return NULL_TREE;
11822 /* If the format specifier was "", fprintf does nothing. */
11823 if (fmt_str[0] == '\0')
11825 /* If FP has side-effects, just wait until gimplification is
11826 done. */
11827 if (TREE_SIDE_EFFECTS (fp))
11828 return NULL_TREE;
11830 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
11833 /* When "string" doesn't contain %, replace all cases of
11834 fprintf (fp, string) with fputs (string, fp). The fputs
11835 builtin will take care of special cases like length == 1. */
11836 if (fn_fputs)
11837 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
11840 /* The other optimizations can be done only on the non-va_list variants. */
11841 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
11842 return NULL_TREE;
11844 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11845 else if (strcmp (fmt_str, target_percent_s) == 0)
11847 if (!arg || !validate_arg (arg, POINTER_TYPE))
11848 return NULL_TREE;
11849 if (fn_fputs)
11850 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
11853 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11854 else if (strcmp (fmt_str, target_percent_c) == 0)
11856 if (!arg || !validate_arg (arg, INTEGER_TYPE))
11857 return NULL_TREE;
11858 if (fn_fputc)
11859 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
11862 if (!call)
11863 return NULL_TREE;
11864 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
11867 /* Initialize format string characters in the target charset. */
11869 bool
11870 init_target_chars (void)
11872 static bool init;
11873 if (!init)
11875 target_newline = lang_hooks.to_target_charset ('\n');
11876 target_percent = lang_hooks.to_target_charset ('%');
11877 target_c = lang_hooks.to_target_charset ('c');
11878 target_s = lang_hooks.to_target_charset ('s');
11879 if (target_newline == 0 || target_percent == 0 || target_c == 0
11880 || target_s == 0)
11881 return false;
11883 target_percent_c[0] = target_percent;
11884 target_percent_c[1] = target_c;
11885 target_percent_c[2] = '\0';
11887 target_percent_s[0] = target_percent;
11888 target_percent_s[1] = target_s;
11889 target_percent_s[2] = '\0';
11891 target_percent_s_newline[0] = target_percent;
11892 target_percent_s_newline[1] = target_s;
11893 target_percent_s_newline[2] = target_newline;
11894 target_percent_s_newline[3] = '\0';
11896 init = true;
11898 return true;
11901 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11902 and no overflow/underflow occurred. INEXACT is true if M was not
11903 exactly calculated. TYPE is the tree type for the result. This
11904 function assumes that you cleared the MPFR flags and then
11905 calculated M to see if anything subsequently set a flag prior to
11906 entering this function. Return NULL_TREE if any checks fail. */
11908 static tree
11909 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
11911 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11912 overflow/underflow occurred. If -frounding-math, proceed iff the
11913 result of calling FUNC was exact. */
11914 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11915 && (!flag_rounding_math || !inexact))
11917 REAL_VALUE_TYPE rr;
11919 real_from_mpfr (&rr, m, type, GMP_RNDN);
11920 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11921 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11922 but the mpft_t is not, then we underflowed in the
11923 conversion. */
11924 if (real_isfinite (&rr)
11925 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11927 REAL_VALUE_TYPE rmode;
11929 real_convert (&rmode, TYPE_MODE (type), &rr);
11930 /* Proceed iff the specified mode can hold the value. */
11931 if (real_identical (&rmode, &rr))
11932 return build_real (type, rmode);
11935 return NULL_TREE;
11938 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
11939 number and no overflow/underflow occurred. INEXACT is true if M
11940 was not exactly calculated. TYPE is the tree type for the result.
11941 This function assumes that you cleared the MPFR flags and then
11942 calculated M to see if anything subsequently set a flag prior to
11943 entering this function. Return NULL_TREE if any checks fail, if
11944 FORCE_CONVERT is true, then bypass the checks. */
11946 static tree
11947 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
11949 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11950 overflow/underflow occurred. If -frounding-math, proceed iff the
11951 result of calling FUNC was exact. */
11952 if (force_convert
11953 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
11954 && !mpfr_overflow_p () && !mpfr_underflow_p ()
11955 && (!flag_rounding_math || !inexact)))
11957 REAL_VALUE_TYPE re, im;
11959 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
11960 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
11961 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11962 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11963 but the mpft_t is not, then we underflowed in the
11964 conversion. */
11965 if (force_convert
11966 || (real_isfinite (&re) && real_isfinite (&im)
11967 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
11968 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
11970 REAL_VALUE_TYPE re_mode, im_mode;
11972 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
11973 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
11974 /* Proceed iff the specified mode can hold the value. */
11975 if (force_convert
11976 || (real_identical (&re_mode, &re)
11977 && real_identical (&im_mode, &im)))
11978 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
11979 build_real (TREE_TYPE (type), im_mode));
11982 return NULL_TREE;
11985 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11986 FUNC on it and return the resulting value as a tree with type TYPE.
11987 If MIN and/or MAX are not NULL, then the supplied ARG must be
11988 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11989 acceptable values, otherwise they are not. The mpfr precision is
11990 set to the precision of TYPE. We assume that function FUNC returns
11991 zero if the result could be calculated exactly within the requested
11992 precision. */
11994 static tree
11995 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11996 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11997 bool inclusive)
11999 tree result = NULL_TREE;
12001 STRIP_NOPS (arg);
12003 /* To proceed, MPFR must exactly represent the target floating point
12004 format, which only happens when the target base equals two. */
12005 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12006 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
12008 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
12010 if (real_isfinite (ra)
12011 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
12012 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
12014 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12015 const int prec = fmt->p;
12016 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12017 int inexact;
12018 mpfr_t m;
12020 mpfr_init2 (m, prec);
12021 mpfr_from_real (m, ra, GMP_RNDN);
12022 mpfr_clear_flags ();
12023 inexact = func (m, m, rnd);
12024 result = do_mpfr_ckconv (m, type, inexact);
12025 mpfr_clear (m);
12029 return result;
12032 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
12033 FUNC on it and return the resulting value as a tree with type TYPE.
12034 The mpfr precision is set to the precision of TYPE. We assume that
12035 function FUNC returns zero if the result could be calculated
12036 exactly within the requested precision. */
12038 static tree
12039 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
12040 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12042 tree result = NULL_TREE;
12044 STRIP_NOPS (arg1);
12045 STRIP_NOPS (arg2);
12047 /* To proceed, MPFR must exactly represent the target floating point
12048 format, which only happens when the target base equals two. */
12049 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12050 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12051 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12053 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12054 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12056 if (real_isfinite (ra1) && real_isfinite (ra2))
12058 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12059 const int prec = fmt->p;
12060 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12061 int inexact;
12062 mpfr_t m1, m2;
12064 mpfr_inits2 (prec, m1, m2, NULL);
12065 mpfr_from_real (m1, ra1, GMP_RNDN);
12066 mpfr_from_real (m2, ra2, GMP_RNDN);
12067 mpfr_clear_flags ();
12068 inexact = func (m1, m1, m2, rnd);
12069 result = do_mpfr_ckconv (m1, type, inexact);
12070 mpfr_clears (m1, m2, NULL);
12074 return result;
12077 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
12078 FUNC on it and return the resulting value as a tree with type TYPE.
12079 The mpfr precision is set to the precision of TYPE. We assume that
12080 function FUNC returns zero if the result could be calculated
12081 exactly within the requested precision. */
12083 static tree
12084 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
12085 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12087 tree result = NULL_TREE;
12089 STRIP_NOPS (arg1);
12090 STRIP_NOPS (arg2);
12091 STRIP_NOPS (arg3);
12093 /* To proceed, MPFR must exactly represent the target floating point
12094 format, which only happens when the target base equals two. */
12095 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12096 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12097 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
12098 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
12100 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12101 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12102 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
12104 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
12106 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12107 const int prec = fmt->p;
12108 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12109 int inexact;
12110 mpfr_t m1, m2, m3;
12112 mpfr_inits2 (prec, m1, m2, m3, NULL);
12113 mpfr_from_real (m1, ra1, GMP_RNDN);
12114 mpfr_from_real (m2, ra2, GMP_RNDN);
12115 mpfr_from_real (m3, ra3, GMP_RNDN);
12116 mpfr_clear_flags ();
12117 inexact = func (m1, m1, m2, m3, rnd);
12118 result = do_mpfr_ckconv (m1, type, inexact);
12119 mpfr_clears (m1, m2, m3, NULL);
12123 return result;
12126 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
12127 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
12128 If ARG_SINP and ARG_COSP are NULL then the result is returned
12129 as a complex value.
12130 The type is taken from the type of ARG and is used for setting the
12131 precision of the calculation and results. */
12133 static tree
12134 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
12136 tree const type = TREE_TYPE (arg);
12137 tree result = NULL_TREE;
12139 STRIP_NOPS (arg);
12141 /* To proceed, MPFR must exactly represent the target floating point
12142 format, which only happens when the target base equals two. */
12143 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12144 && TREE_CODE (arg) == REAL_CST
12145 && !TREE_OVERFLOW (arg))
12147 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
12149 if (real_isfinite (ra))
12151 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12152 const int prec = fmt->p;
12153 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12154 tree result_s, result_c;
12155 int inexact;
12156 mpfr_t m, ms, mc;
12158 mpfr_inits2 (prec, m, ms, mc, NULL);
12159 mpfr_from_real (m, ra, GMP_RNDN);
12160 mpfr_clear_flags ();
12161 inexact = mpfr_sin_cos (ms, mc, m, rnd);
12162 result_s = do_mpfr_ckconv (ms, type, inexact);
12163 result_c = do_mpfr_ckconv (mc, type, inexact);
12164 mpfr_clears (m, ms, mc, NULL);
12165 if (result_s && result_c)
12167 /* If we are to return in a complex value do so. */
12168 if (!arg_sinp && !arg_cosp)
12169 return build_complex (build_complex_type (type),
12170 result_c, result_s);
12172 /* Dereference the sin/cos pointer arguments. */
12173 arg_sinp = build_fold_indirect_ref (arg_sinp);
12174 arg_cosp = build_fold_indirect_ref (arg_cosp);
12175 /* Proceed if valid pointer type were passed in. */
12176 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
12177 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
12179 /* Set the values. */
12180 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
12181 result_s);
12182 TREE_SIDE_EFFECTS (result_s) = 1;
12183 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
12184 result_c);
12185 TREE_SIDE_EFFECTS (result_c) = 1;
12186 /* Combine the assignments into a compound expr. */
12187 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12188 result_s, result_c));
12193 return result;
12196 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
12197 two-argument mpfr order N Bessel function FUNC on them and return
12198 the resulting value as a tree with type TYPE. The mpfr precision
12199 is set to the precision of TYPE. We assume that function FUNC
12200 returns zero if the result could be calculated exactly within the
12201 requested precision. */
12202 static tree
12203 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
12204 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
12205 const REAL_VALUE_TYPE *min, bool inclusive)
12207 tree result = NULL_TREE;
12209 STRIP_NOPS (arg1);
12210 STRIP_NOPS (arg2);
12212 /* To proceed, MPFR must exactly represent the target floating point
12213 format, which only happens when the target base equals two. */
12214 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12215 && tree_fits_shwi_p (arg1)
12216 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12218 const HOST_WIDE_INT n = tree_to_shwi (arg1);
12219 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
12221 if (n == (long)n
12222 && real_isfinite (ra)
12223 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
12225 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12226 const int prec = fmt->p;
12227 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12228 int inexact;
12229 mpfr_t m;
12231 mpfr_init2 (m, prec);
12232 mpfr_from_real (m, ra, GMP_RNDN);
12233 mpfr_clear_flags ();
12234 inexact = func (m, n, m, rnd);
12235 result = do_mpfr_ckconv (m, type, inexact);
12236 mpfr_clear (m);
12240 return result;
12243 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12244 the pointer *(ARG_QUO) and return the result. The type is taken
12245 from the type of ARG0 and is used for setting the precision of the
12246 calculation and results. */
12248 static tree
12249 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
12251 tree const type = TREE_TYPE (arg0);
12252 tree result = NULL_TREE;
12254 STRIP_NOPS (arg0);
12255 STRIP_NOPS (arg1);
12257 /* To proceed, MPFR must exactly represent the target floating point
12258 format, which only happens when the target base equals two. */
12259 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12260 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12261 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12263 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12264 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12266 if (real_isfinite (ra0) && real_isfinite (ra1))
12268 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12269 const int prec = fmt->p;
12270 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12271 tree result_rem;
12272 long integer_quo;
12273 mpfr_t m0, m1;
12275 mpfr_inits2 (prec, m0, m1, NULL);
12276 mpfr_from_real (m0, ra0, GMP_RNDN);
12277 mpfr_from_real (m1, ra1, GMP_RNDN);
12278 mpfr_clear_flags ();
12279 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12280 /* Remquo is independent of the rounding mode, so pass
12281 inexact=0 to do_mpfr_ckconv(). */
12282 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12283 mpfr_clears (m0, m1, NULL);
12284 if (result_rem)
12286 /* MPFR calculates quo in the host's long so it may
12287 return more bits in quo than the target int can hold
12288 if sizeof(host long) > sizeof(target int). This can
12289 happen even for native compilers in LP64 mode. In
12290 these cases, modulo the quo value with the largest
12291 number that the target int can hold while leaving one
12292 bit for the sign. */
12293 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12294 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12296 /* Dereference the quo pointer argument. */
12297 arg_quo = build_fold_indirect_ref (arg_quo);
12298 /* Proceed iff a valid pointer type was passed in. */
12299 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12301 /* Set the value. */
12302 tree result_quo
12303 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12304 build_int_cst (TREE_TYPE (arg_quo),
12305 integer_quo));
12306 TREE_SIDE_EFFECTS (result_quo) = 1;
12307 /* Combine the quo assignment with the rem. */
12308 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12309 result_quo, result_rem));
12314 return result;
12317 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12318 resulting value as a tree with type TYPE. The mpfr precision is
12319 set to the precision of TYPE. We assume that this mpfr function
12320 returns zero if the result could be calculated exactly within the
12321 requested precision. In addition, the integer pointer represented
12322 by ARG_SG will be dereferenced and set to the appropriate signgam
12323 (-1,1) value. */
12325 static tree
12326 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12328 tree result = NULL_TREE;
12330 STRIP_NOPS (arg);
12332 /* To proceed, MPFR must exactly represent the target floating point
12333 format, which only happens when the target base equals two. Also
12334 verify ARG is a constant and that ARG_SG is an int pointer. */
12335 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12336 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12337 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12338 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12340 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12342 /* In addition to NaN and Inf, the argument cannot be zero or a
12343 negative integer. */
12344 if (real_isfinite (ra)
12345 && ra->cl != rvc_zero
12346 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12348 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12349 const int prec = fmt->p;
12350 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12351 int inexact, sg;
12352 mpfr_t m;
12353 tree result_lg;
12355 mpfr_init2 (m, prec);
12356 mpfr_from_real (m, ra, GMP_RNDN);
12357 mpfr_clear_flags ();
12358 inexact = mpfr_lgamma (m, &sg, m, rnd);
12359 result_lg = do_mpfr_ckconv (m, type, inexact);
12360 mpfr_clear (m);
12361 if (result_lg)
12363 tree result_sg;
12365 /* Dereference the arg_sg pointer argument. */
12366 arg_sg = build_fold_indirect_ref (arg_sg);
12367 /* Assign the signgam value into *arg_sg. */
12368 result_sg = fold_build2 (MODIFY_EXPR,
12369 TREE_TYPE (arg_sg), arg_sg,
12370 build_int_cst (TREE_TYPE (arg_sg), sg));
12371 TREE_SIDE_EFFECTS (result_sg) = 1;
12372 /* Combine the signgam assignment with the lgamma result. */
12373 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12374 result_sg, result_lg));
12379 return result;
12382 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
12383 function FUNC on it and return the resulting value as a tree with
12384 type TYPE. The mpfr precision is set to the precision of TYPE. We
12385 assume that function FUNC returns zero if the result could be
12386 calculated exactly within the requested precision. */
12388 static tree
12389 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
12391 tree result = NULL_TREE;
12393 STRIP_NOPS (arg);
12395 /* To proceed, MPFR must exactly represent the target floating point
12396 format, which only happens when the target base equals two. */
12397 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
12398 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
12399 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
12401 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
12402 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
12404 if (real_isfinite (re) && real_isfinite (im))
12406 const struct real_format *const fmt =
12407 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12408 const int prec = fmt->p;
12409 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12410 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12411 int inexact;
12412 mpc_t m;
12414 mpc_init2 (m, prec);
12415 mpfr_from_real (mpc_realref (m), re, rnd);
12416 mpfr_from_real (mpc_imagref (m), im, rnd);
12417 mpfr_clear_flags ();
12418 inexact = func (m, m, crnd);
12419 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
12420 mpc_clear (m);
12424 return result;
12427 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12428 mpc function FUNC on it and return the resulting value as a tree
12429 with type TYPE. The mpfr precision is set to the precision of
12430 TYPE. We assume that function FUNC returns zero if the result
12431 could be calculated exactly within the requested precision. If
12432 DO_NONFINITE is true, then fold expressions containing Inf or NaN
12433 in the arguments and/or results. */
12435 tree
12436 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12437 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12439 tree result = NULL_TREE;
12441 STRIP_NOPS (arg0);
12442 STRIP_NOPS (arg1);
12444 /* To proceed, MPFR must exactly represent the target floating point
12445 format, which only happens when the target base equals two. */
12446 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12447 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
12448 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12449 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
12450 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12452 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12453 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12454 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12455 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12457 if (do_nonfinite
12458 || (real_isfinite (re0) && real_isfinite (im0)
12459 && real_isfinite (re1) && real_isfinite (im1)))
12461 const struct real_format *const fmt =
12462 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12463 const int prec = fmt->p;
12464 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12465 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12466 int inexact;
12467 mpc_t m0, m1;
12469 mpc_init2 (m0, prec);
12470 mpc_init2 (m1, prec);
12471 mpfr_from_real (mpc_realref (m0), re0, rnd);
12472 mpfr_from_real (mpc_imagref (m0), im0, rnd);
12473 mpfr_from_real (mpc_realref (m1), re1, rnd);
12474 mpfr_from_real (mpc_imagref (m1), im1, rnd);
12475 mpfr_clear_flags ();
12476 inexact = func (m0, m0, m1, crnd);
12477 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12478 mpc_clear (m0);
12479 mpc_clear (m1);
12483 return result;
12486 /* A wrapper function for builtin folding that prevents warnings for
12487 "statement without effect" and the like, caused by removing the
12488 call node earlier than the warning is generated. */
12490 tree
12491 fold_call_stmt (gimple stmt, bool ignore)
12493 tree ret = NULL_TREE;
12494 tree fndecl = gimple_call_fndecl (stmt);
12495 location_t loc = gimple_location (stmt);
12496 if (fndecl
12497 && TREE_CODE (fndecl) == FUNCTION_DECL
12498 && DECL_BUILT_IN (fndecl)
12499 && !gimple_call_va_arg_pack_p (stmt))
12501 int nargs = gimple_call_num_args (stmt);
12502 tree *args = (nargs > 0
12503 ? gimple_call_arg_ptr (stmt, 0)
12504 : &error_mark_node);
12506 if (avoid_folding_inline_builtin (fndecl))
12507 return NULL_TREE;
12508 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12510 return targetm.fold_builtin (fndecl, nargs, args, ignore);
12512 else
12514 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
12515 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
12516 if (ret)
12518 /* Propagate location information from original call to
12519 expansion of builtin. Otherwise things like
12520 maybe_emit_chk_warning, that operate on the expansion
12521 of a builtin, will use the wrong location information. */
12522 if (gimple_has_location (stmt))
12524 tree realret = ret;
12525 if (TREE_CODE (ret) == NOP_EXPR)
12526 realret = TREE_OPERAND (ret, 0);
12527 if (CAN_HAVE_LOCATION_P (realret)
12528 && !EXPR_HAS_LOCATION (realret))
12529 SET_EXPR_LOCATION (realret, loc);
12530 return realret;
12532 return ret;
12536 return NULL_TREE;
12539 /* Look up the function in builtin_decl that corresponds to DECL
12540 and set ASMSPEC as its user assembler name. DECL must be a
12541 function decl that declares a builtin. */
12543 void
12544 set_builtin_user_assembler_name (tree decl, const char *asmspec)
12546 tree builtin;
12547 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
12548 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
12549 && asmspec != 0);
12551 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12552 set_user_assembler_name (builtin, asmspec);
12553 switch (DECL_FUNCTION_CODE (decl))
12555 case BUILT_IN_MEMCPY:
12556 init_block_move_fn (asmspec);
12557 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
12558 break;
12559 case BUILT_IN_MEMSET:
12560 init_block_clear_fn (asmspec);
12561 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
12562 break;
12563 case BUILT_IN_MEMMOVE:
12564 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
12565 break;
12566 case BUILT_IN_MEMCMP:
12567 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
12568 break;
12569 case BUILT_IN_ABORT:
12570 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
12571 break;
12572 case BUILT_IN_FFS:
12573 if (INT_TYPE_SIZE < BITS_PER_WORD)
12575 set_user_assembler_libfunc ("ffs", asmspec);
12576 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
12577 MODE_INT, 0), "ffs");
12579 break;
12580 default:
12581 break;
12585 /* Return true if DECL is a builtin that expands to a constant or similarly
12586 simple code. */
12587 bool
12588 is_simple_builtin (tree decl)
12590 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12591 switch (DECL_FUNCTION_CODE (decl))
12593 /* Builtins that expand to constants. */
12594 case BUILT_IN_CONSTANT_P:
12595 case BUILT_IN_EXPECT:
12596 case BUILT_IN_OBJECT_SIZE:
12597 case BUILT_IN_UNREACHABLE:
12598 /* Simple register moves or loads from stack. */
12599 case BUILT_IN_ASSUME_ALIGNED:
12600 case BUILT_IN_RETURN_ADDRESS:
12601 case BUILT_IN_EXTRACT_RETURN_ADDR:
12602 case BUILT_IN_FROB_RETURN_ADDR:
12603 case BUILT_IN_RETURN:
12604 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12605 case BUILT_IN_FRAME_ADDRESS:
12606 case BUILT_IN_VA_END:
12607 case BUILT_IN_STACK_SAVE:
12608 case BUILT_IN_STACK_RESTORE:
12609 /* Exception state returns or moves registers around. */
12610 case BUILT_IN_EH_FILTER:
12611 case BUILT_IN_EH_POINTER:
12612 case BUILT_IN_EH_COPY_VALUES:
12613 return true;
12615 default:
12616 return false;
12619 return false;
12622 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12623 most probably expanded inline into reasonably simple code. This is a
12624 superset of is_simple_builtin. */
12625 bool
12626 is_inexpensive_builtin (tree decl)
12628 if (!decl)
12629 return false;
12630 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12631 return true;
12632 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12633 switch (DECL_FUNCTION_CODE (decl))
12635 case BUILT_IN_ABS:
12636 case BUILT_IN_ALLOCA:
12637 case BUILT_IN_ALLOCA_WITH_ALIGN:
12638 case BUILT_IN_BSWAP16:
12639 case BUILT_IN_BSWAP32:
12640 case BUILT_IN_BSWAP64:
12641 case BUILT_IN_CLZ:
12642 case BUILT_IN_CLZIMAX:
12643 case BUILT_IN_CLZL:
12644 case BUILT_IN_CLZLL:
12645 case BUILT_IN_CTZ:
12646 case BUILT_IN_CTZIMAX:
12647 case BUILT_IN_CTZL:
12648 case BUILT_IN_CTZLL:
12649 case BUILT_IN_FFS:
12650 case BUILT_IN_FFSIMAX:
12651 case BUILT_IN_FFSL:
12652 case BUILT_IN_FFSLL:
12653 case BUILT_IN_IMAXABS:
12654 case BUILT_IN_FINITE:
12655 case BUILT_IN_FINITEF:
12656 case BUILT_IN_FINITEL:
12657 case BUILT_IN_FINITED32:
12658 case BUILT_IN_FINITED64:
12659 case BUILT_IN_FINITED128:
12660 case BUILT_IN_FPCLASSIFY:
12661 case BUILT_IN_ISFINITE:
12662 case BUILT_IN_ISINF_SIGN:
12663 case BUILT_IN_ISINF:
12664 case BUILT_IN_ISINFF:
12665 case BUILT_IN_ISINFL:
12666 case BUILT_IN_ISINFD32:
12667 case BUILT_IN_ISINFD64:
12668 case BUILT_IN_ISINFD128:
12669 case BUILT_IN_ISNAN:
12670 case BUILT_IN_ISNANF:
12671 case BUILT_IN_ISNANL:
12672 case BUILT_IN_ISNAND32:
12673 case BUILT_IN_ISNAND64:
12674 case BUILT_IN_ISNAND128:
12675 case BUILT_IN_ISNORMAL:
12676 case BUILT_IN_ISGREATER:
12677 case BUILT_IN_ISGREATEREQUAL:
12678 case BUILT_IN_ISLESS:
12679 case BUILT_IN_ISLESSEQUAL:
12680 case BUILT_IN_ISLESSGREATER:
12681 case BUILT_IN_ISUNORDERED:
12682 case BUILT_IN_VA_ARG_PACK:
12683 case BUILT_IN_VA_ARG_PACK_LEN:
12684 case BUILT_IN_VA_COPY:
12685 case BUILT_IN_TRAP:
12686 case BUILT_IN_SAVEREGS:
12687 case BUILT_IN_POPCOUNTL:
12688 case BUILT_IN_POPCOUNTLL:
12689 case BUILT_IN_POPCOUNTIMAX:
12690 case BUILT_IN_POPCOUNT:
12691 case BUILT_IN_PARITYL:
12692 case BUILT_IN_PARITYLL:
12693 case BUILT_IN_PARITYIMAX:
12694 case BUILT_IN_PARITY:
12695 case BUILT_IN_LABS:
12696 case BUILT_IN_LLABS:
12697 case BUILT_IN_PREFETCH:
12698 return true;
12700 default:
12701 return is_simple_builtin (decl);
12704 return false;