2015-03-02 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / builtins.c
blobfb871e696a0413860df79cb0c651e3851461530a
1 /* Expand builtin functions.
2 Copyright (C) 1988-2015 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 "hash-set.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stringpool.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "varasm.h"
40 #include "tree-object-size.h"
41 #include "realmpfr.h"
42 #include "predict.h"
43 #include "hashtab.h"
44 #include "hard-reg-set.h"
45 #include "function.h"
46 #include "cfgrtl.h"
47 #include "basic-block.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "gimple-expr.h"
51 #include "is-a.h"
52 #include "gimple.h"
53 #include "flags.h"
54 #include "regs.h"
55 #include "except.h"
56 #include "insn-config.h"
57 #include "statistics.h"
58 #include "real.h"
59 #include "fixed-value.h"
60 #include "expmed.h"
61 #include "dojump.h"
62 #include "explow.h"
63 #include "emit-rtl.h"
64 #include "stmt.h"
65 #include "expr.h"
66 #include "insn-codes.h"
67 #include "optabs.h"
68 #include "libfuncs.h"
69 #include "recog.h"
70 #include "output.h"
71 #include "typeclass.h"
72 #include "tm_p.h"
73 #include "target.h"
74 #include "langhooks.h"
75 #include "tree-ssanames.h"
76 #include "tree-dfa.h"
77 #include "value-prof.h"
78 #include "diagnostic-core.h"
79 #include "builtins.h"
80 #include "asan.h"
81 #include "cilk.h"
82 #include "ipa-ref.h"
83 #include "lto-streamer.h"
84 #include "cgraph.h"
85 #include "tree-chkp.h"
86 #include "rtl-chkp.h"
87 #include "gomp-constants.h"
90 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
92 struct target_builtins default_target_builtins;
93 #if SWITCHABLE_TARGET
94 struct target_builtins *this_target_builtins = &default_target_builtins;
95 #endif
97 /* Define the names of the builtin function types and codes. */
98 const char *const built_in_class_names[BUILT_IN_LAST]
99 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
101 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
102 const char * built_in_names[(int) END_BUILTINS] =
104 #include "builtins.def"
106 #undef DEF_BUILTIN
108 /* Setup an array of builtin_info_type, make sure each element decl is
109 initialized to NULL_TREE. */
110 builtin_info_type builtin_info[(int)END_BUILTINS];
112 /* Non-zero if __builtin_constant_p should be folded right away. */
113 bool force_folding_builtin_constant_p;
115 static rtx c_readstr (const char *, machine_mode);
116 static int target_char_cast (tree, char *);
117 static rtx get_memory_rtx (tree, tree);
118 static int apply_args_size (void);
119 static int apply_result_size (void);
120 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
121 static rtx result_vector (int, rtx);
122 #endif
123 static void expand_builtin_update_setjmp_buf (rtx);
124 static void expand_builtin_prefetch (tree);
125 static rtx expand_builtin_apply_args (void);
126 static rtx expand_builtin_apply_args_1 (void);
127 static rtx expand_builtin_apply (rtx, rtx, rtx);
128 static void expand_builtin_return (rtx);
129 static enum type_class type_to_class (tree);
130 static rtx expand_builtin_classify_type (tree);
131 static void expand_errno_check (tree, rtx);
132 static rtx expand_builtin_mathfn (tree, rtx, rtx);
133 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
134 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
135 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
136 static rtx expand_builtin_interclass_mathfn (tree, rtx);
137 static rtx expand_builtin_sincos (tree);
138 static rtx expand_builtin_cexpi (tree, rtx);
139 static rtx expand_builtin_int_roundingfn (tree, rtx);
140 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
141 static rtx expand_builtin_next_arg (void);
142 static rtx expand_builtin_va_start (tree);
143 static rtx expand_builtin_va_end (tree);
144 static rtx expand_builtin_va_copy (tree);
145 static rtx expand_builtin_memcmp (tree, rtx, machine_mode);
146 static rtx expand_builtin_strcmp (tree, rtx);
147 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
148 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, machine_mode);
149 static rtx expand_builtin_memcpy (tree, rtx);
150 static rtx expand_builtin_memcpy_with_bounds (tree, rtx);
151 static rtx expand_builtin_memcpy_args (tree, tree, tree, rtx, tree);
152 static rtx expand_builtin_mempcpy (tree, rtx, machine_mode);
153 static rtx expand_builtin_mempcpy_with_bounds (tree, rtx, machine_mode);
154 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
155 machine_mode, int, tree);
156 static rtx expand_builtin_strcpy (tree, rtx);
157 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
158 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
159 static rtx expand_builtin_strncpy (tree, rtx);
160 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, machine_mode);
161 static rtx expand_builtin_memset (tree, rtx, machine_mode);
162 static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode);
163 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
164 static rtx expand_builtin_bzero (tree);
165 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
166 static rtx expand_builtin_alloca (tree, bool);
167 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
168 static rtx expand_builtin_frame_address (tree, tree);
169 static tree stabilize_va_list_loc (location_t, tree, int);
170 static rtx expand_builtin_expect (tree, rtx);
171 static tree fold_builtin_constant_p (tree);
172 static tree fold_builtin_classify_type (tree);
173 static tree fold_builtin_strlen (location_t, tree, tree);
174 static tree fold_builtin_inf (location_t, tree, int);
175 static tree fold_builtin_nan (tree, tree, int);
176 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
177 static bool validate_arg (const_tree, enum tree_code code);
178 static bool integer_valued_real_p (tree);
179 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
180 static rtx expand_builtin_fabs (tree, rtx, rtx);
181 static rtx expand_builtin_signbit (tree, rtx);
182 static tree fold_builtin_sqrt (location_t, tree, tree);
183 static tree fold_builtin_cbrt (location_t, tree, tree);
184 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
185 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
186 static tree fold_builtin_cos (location_t, tree, tree, tree);
187 static tree fold_builtin_cosh (location_t, tree, tree, tree);
188 static tree fold_builtin_tan (tree, tree);
189 static tree fold_builtin_trunc (location_t, tree, tree);
190 static tree fold_builtin_floor (location_t, tree, tree);
191 static tree fold_builtin_ceil (location_t, tree, tree);
192 static tree fold_builtin_round (location_t, tree, tree);
193 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
194 static tree fold_builtin_bitop (tree, tree);
195 static tree fold_builtin_strchr (location_t, tree, tree, tree);
196 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
197 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
198 static tree fold_builtin_strcmp (location_t, tree, tree);
199 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
200 static tree fold_builtin_signbit (location_t, tree, tree);
201 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
202 static tree fold_builtin_isascii (location_t, tree);
203 static tree fold_builtin_toascii (location_t, tree);
204 static tree fold_builtin_isdigit (location_t, tree);
205 static tree fold_builtin_fabs (location_t, tree, tree);
206 static tree fold_builtin_abs (location_t, tree, tree);
207 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
208 enum tree_code);
209 static tree fold_builtin_0 (location_t, tree);
210 static tree fold_builtin_1 (location_t, tree, tree);
211 static tree fold_builtin_2 (location_t, tree, tree, tree);
212 static tree fold_builtin_3 (location_t, tree, tree, tree, tree);
213 static tree fold_builtin_varargs (location_t, tree, tree*, int);
215 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
216 static tree fold_builtin_strstr (location_t, tree, tree, tree);
217 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
218 static tree fold_builtin_strspn (location_t, tree, tree);
219 static tree fold_builtin_strcspn (location_t, tree, tree);
221 static rtx expand_builtin_object_size (tree);
222 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
223 enum built_in_function);
224 static void maybe_emit_chk_warning (tree, enum built_in_function);
225 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
226 static void maybe_emit_free_warning (tree);
227 static tree fold_builtin_object_size (tree, tree);
229 unsigned HOST_WIDE_INT target_newline;
230 unsigned HOST_WIDE_INT target_percent;
231 static unsigned HOST_WIDE_INT target_c;
232 static unsigned HOST_WIDE_INT target_s;
233 char target_percent_c[3];
234 char target_percent_s[3];
235 char target_percent_s_newline[4];
236 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
237 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
238 static tree do_mpfr_arg2 (tree, tree, tree,
239 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
240 static tree do_mpfr_arg3 (tree, tree, tree, tree,
241 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
242 static tree do_mpfr_sincos (tree, tree, tree);
243 static tree do_mpfr_bessel_n (tree, tree, tree,
244 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
245 const REAL_VALUE_TYPE *, bool);
246 static tree do_mpfr_remquo (tree, tree, tree);
247 static tree do_mpfr_lgamma_r (tree, tree, tree);
248 static void expand_builtin_sync_synchronize (void);
250 /* Return true if NAME starts with __builtin_ or __sync_. */
252 static bool
253 is_builtin_name (const char *name)
255 if (strncmp (name, "__builtin_", 10) == 0)
256 return true;
257 if (strncmp (name, "__sync_", 7) == 0)
258 return true;
259 if (strncmp (name, "__atomic_", 9) == 0)
260 return true;
261 if (flag_cilkplus
262 && (!strcmp (name, "__cilkrts_detach")
263 || !strcmp (name, "__cilkrts_pop_frame")))
264 return true;
265 return false;
269 /* Return true if DECL is a function symbol representing a built-in. */
271 bool
272 is_builtin_fn (tree decl)
274 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
277 /* Return true if NODE should be considered for inline expansion regardless
278 of the optimization level. This means whenever a function is invoked with
279 its "internal" name, which normally contains the prefix "__builtin". */
281 static bool
282 called_as_built_in (tree node)
284 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
285 we want the name used to call the function, not the name it
286 will have. */
287 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
288 return is_builtin_name (name);
291 /* Compute values M and N such that M divides (address of EXP - N) and such
292 that N < M. If these numbers can be determined, store M in alignp and N in
293 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
294 *alignp and any bit-offset to *bitposp.
296 Note that the address (and thus the alignment) computed here is based
297 on the address to which a symbol resolves, whereas DECL_ALIGN is based
298 on the address at which an object is actually located. These two
299 addresses are not always the same. For example, on ARM targets,
300 the address &foo of a Thumb function foo() has the lowest bit set,
301 whereas foo() itself starts on an even address.
303 If ADDR_P is true we are taking the address of the memory reference EXP
304 and thus cannot rely on the access taking place. */
306 static bool
307 get_object_alignment_2 (tree exp, unsigned int *alignp,
308 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
310 HOST_WIDE_INT bitsize, bitpos;
311 tree offset;
312 machine_mode mode;
313 int unsignedp, volatilep;
314 unsigned int align = BITS_PER_UNIT;
315 bool known_alignment = false;
317 /* Get the innermost object and the constant (bitpos) and possibly
318 variable (offset) offset of the access. */
319 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
320 &mode, &unsignedp, &volatilep, true);
322 /* Extract alignment information from the innermost object and
323 possibly adjust bitpos and offset. */
324 if (TREE_CODE (exp) == FUNCTION_DECL)
326 /* Function addresses can encode extra information besides their
327 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
328 allows the low bit to be used as a virtual bit, we know
329 that the address itself must be at least 2-byte aligned. */
330 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
331 align = 2 * BITS_PER_UNIT;
333 else if (TREE_CODE (exp) == LABEL_DECL)
335 else if (TREE_CODE (exp) == CONST_DECL)
337 /* The alignment of a CONST_DECL is determined by its initializer. */
338 exp = DECL_INITIAL (exp);
339 align = TYPE_ALIGN (TREE_TYPE (exp));
340 #ifdef CONSTANT_ALIGNMENT
341 if (CONSTANT_CLASS_P (exp))
342 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
343 #endif
344 known_alignment = true;
346 else if (DECL_P (exp))
348 align = DECL_ALIGN (exp);
349 known_alignment = true;
351 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
353 align = TYPE_ALIGN (TREE_TYPE (exp));
355 else if (TREE_CODE (exp) == INDIRECT_REF
356 || TREE_CODE (exp) == MEM_REF
357 || TREE_CODE (exp) == TARGET_MEM_REF)
359 tree addr = TREE_OPERAND (exp, 0);
360 unsigned ptr_align;
361 unsigned HOST_WIDE_INT ptr_bitpos;
362 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
364 /* If the address is explicitely aligned, handle that. */
365 if (TREE_CODE (addr) == BIT_AND_EXPR
366 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
368 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
369 ptr_bitmask *= BITS_PER_UNIT;
370 align = ptr_bitmask & -ptr_bitmask;
371 addr = TREE_OPERAND (addr, 0);
374 known_alignment
375 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
376 align = MAX (ptr_align, align);
378 /* Re-apply explicit alignment to the bitpos. */
379 ptr_bitpos &= ptr_bitmask;
381 /* The alignment of the pointer operand in a TARGET_MEM_REF
382 has to take the variable offset parts into account. */
383 if (TREE_CODE (exp) == TARGET_MEM_REF)
385 if (TMR_INDEX (exp))
387 unsigned HOST_WIDE_INT step = 1;
388 if (TMR_STEP (exp))
389 step = TREE_INT_CST_LOW (TMR_STEP (exp));
390 align = MIN (align, (step & -step) * BITS_PER_UNIT);
392 if (TMR_INDEX2 (exp))
393 align = BITS_PER_UNIT;
394 known_alignment = false;
397 /* When EXP is an actual memory reference then we can use
398 TYPE_ALIGN of a pointer indirection to derive alignment.
399 Do so only if get_pointer_alignment_1 did not reveal absolute
400 alignment knowledge and if using that alignment would
401 improve the situation. */
402 if (!addr_p && !known_alignment
403 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
404 align = TYPE_ALIGN (TREE_TYPE (exp));
405 else
407 /* Else adjust bitpos accordingly. */
408 bitpos += ptr_bitpos;
409 if (TREE_CODE (exp) == MEM_REF
410 || TREE_CODE (exp) == TARGET_MEM_REF)
411 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
414 else if (TREE_CODE (exp) == STRING_CST)
416 /* STRING_CST are the only constant objects we allow to be not
417 wrapped inside a CONST_DECL. */
418 align = TYPE_ALIGN (TREE_TYPE (exp));
419 #ifdef CONSTANT_ALIGNMENT
420 if (CONSTANT_CLASS_P (exp))
421 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
422 #endif
423 known_alignment = true;
426 /* If there is a non-constant offset part extract the maximum
427 alignment that can prevail. */
428 if (offset)
430 unsigned int trailing_zeros = tree_ctz (offset);
431 if (trailing_zeros < HOST_BITS_PER_INT)
433 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
434 if (inner)
435 align = MIN (align, inner);
439 *alignp = align;
440 *bitposp = bitpos & (*alignp - 1);
441 return known_alignment;
444 /* For a memory reference expression EXP compute values M and N such that M
445 divides (&EXP - N) and such that N < M. If these numbers can be determined,
446 store M in alignp and N in *BITPOSP and return true. Otherwise return false
447 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
449 bool
450 get_object_alignment_1 (tree exp, unsigned int *alignp,
451 unsigned HOST_WIDE_INT *bitposp)
453 return get_object_alignment_2 (exp, alignp, bitposp, false);
456 /* Return the alignment in bits of EXP, an object. */
458 unsigned int
459 get_object_alignment (tree exp)
461 unsigned HOST_WIDE_INT bitpos = 0;
462 unsigned int align;
464 get_object_alignment_1 (exp, &align, &bitpos);
466 /* align and bitpos now specify known low bits of the pointer.
467 ptr & (align - 1) == bitpos. */
469 if (bitpos != 0)
470 align = (bitpos & -bitpos);
471 return align;
474 /* For a pointer valued expression EXP compute values M and N such that M
475 divides (EXP - N) and such that N < M. If these numbers can be determined,
476 store M in alignp and N in *BITPOSP and return true. Return false if
477 the results are just a conservative approximation.
479 If EXP is not a pointer, false is returned too. */
481 bool
482 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
483 unsigned HOST_WIDE_INT *bitposp)
485 STRIP_NOPS (exp);
487 if (TREE_CODE (exp) == ADDR_EXPR)
488 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
489 alignp, bitposp, true);
490 else if (TREE_CODE (exp) == SSA_NAME
491 && POINTER_TYPE_P (TREE_TYPE (exp)))
493 unsigned int ptr_align, ptr_misalign;
494 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
496 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
498 *bitposp = ptr_misalign * BITS_PER_UNIT;
499 *alignp = ptr_align * BITS_PER_UNIT;
500 /* We cannot really tell whether this result is an approximation. */
501 return true;
503 else
505 *bitposp = 0;
506 *alignp = BITS_PER_UNIT;
507 return false;
510 else if (TREE_CODE (exp) == INTEGER_CST)
512 *alignp = BIGGEST_ALIGNMENT;
513 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
514 & (BIGGEST_ALIGNMENT - 1));
515 return true;
518 *bitposp = 0;
519 *alignp = BITS_PER_UNIT;
520 return false;
523 /* Return the alignment in bits of EXP, a pointer valued expression.
524 The alignment returned is, by default, the alignment of the thing that
525 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
527 Otherwise, look at the expression to see if we can do better, i.e., if the
528 expression is actually pointing at an object whose alignment is tighter. */
530 unsigned int
531 get_pointer_alignment (tree exp)
533 unsigned HOST_WIDE_INT bitpos = 0;
534 unsigned int align;
536 get_pointer_alignment_1 (exp, &align, &bitpos);
538 /* align and bitpos now specify known low bits of the pointer.
539 ptr & (align - 1) == bitpos. */
541 if (bitpos != 0)
542 align = (bitpos & -bitpos);
544 return align;
547 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
548 way, because it could contain a zero byte in the middle.
549 TREE_STRING_LENGTH is the size of the character array, not the string.
551 ONLY_VALUE should be nonzero if the result is not going to be emitted
552 into the instruction stream and zero if it is going to be expanded.
553 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
554 is returned, otherwise NULL, since
555 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
556 evaluate the side-effects.
558 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
559 accesses. Note that this implies the result is not going to be emitted
560 into the instruction stream.
562 The value returned is of type `ssizetype'.
564 Unfortunately, string_constant can't access the values of const char
565 arrays with initializers, so neither can we do so here. */
567 tree
568 c_strlen (tree src, int only_value)
570 tree offset_node;
571 HOST_WIDE_INT offset;
572 int max;
573 const char *ptr;
574 location_t loc;
576 STRIP_NOPS (src);
577 if (TREE_CODE (src) == COND_EXPR
578 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
580 tree len1, len2;
582 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
583 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
584 if (tree_int_cst_equal (len1, len2))
585 return len1;
588 if (TREE_CODE (src) == COMPOUND_EXPR
589 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
590 return c_strlen (TREE_OPERAND (src, 1), only_value);
592 loc = EXPR_LOC_OR_LOC (src, input_location);
594 src = string_constant (src, &offset_node);
595 if (src == 0)
596 return NULL_TREE;
598 max = TREE_STRING_LENGTH (src) - 1;
599 ptr = TREE_STRING_POINTER (src);
601 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
603 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
604 compute the offset to the following null if we don't know where to
605 start searching for it. */
606 int i;
608 for (i = 0; i < max; i++)
609 if (ptr[i] == 0)
610 return NULL_TREE;
612 /* We don't know the starting offset, but we do know that the string
613 has no internal zero bytes. We can assume that the offset falls
614 within the bounds of the string; otherwise, the programmer deserves
615 what he gets. Subtract the offset from the length of the string,
616 and return that. This would perhaps not be valid if we were dealing
617 with named arrays in addition to literal string constants. */
619 return size_diffop_loc (loc, size_int (max), offset_node);
622 /* We have a known offset into the string. Start searching there for
623 a null character if we can represent it as a single HOST_WIDE_INT. */
624 if (offset_node == 0)
625 offset = 0;
626 else if (! tree_fits_shwi_p (offset_node))
627 offset = -1;
628 else
629 offset = tree_to_shwi (offset_node);
631 /* If the offset is known to be out of bounds, warn, and call strlen at
632 runtime. */
633 if (offset < 0 || offset > max)
635 /* Suppress multiple warnings for propagated constant strings. */
636 if (only_value != 2
637 && !TREE_NO_WARNING (src))
639 warning_at (loc, 0, "offset outside bounds of constant string");
640 TREE_NO_WARNING (src) = 1;
642 return NULL_TREE;
645 /* Use strlen to search for the first zero byte. Since any strings
646 constructed with build_string will have nulls appended, we win even
647 if we get handed something like (char[4])"abcd".
649 Since OFFSET is our starting index into the string, no further
650 calculation is needed. */
651 return ssize_int (strlen (ptr + offset));
654 /* Return a char pointer for a C string if it is a string constant
655 or sum of string constant and integer constant. */
657 const char *
658 c_getstr (tree src)
660 tree offset_node;
662 src = string_constant (src, &offset_node);
663 if (src == 0)
664 return 0;
666 if (offset_node == 0)
667 return TREE_STRING_POINTER (src);
668 else if (!tree_fits_uhwi_p (offset_node)
669 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
670 return 0;
672 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
675 /* Return a constant integer corresponding to target reading
676 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
678 static rtx
679 c_readstr (const char *str, machine_mode mode)
681 HOST_WIDE_INT ch;
682 unsigned int i, j;
683 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
685 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
686 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
687 / HOST_BITS_PER_WIDE_INT;
689 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
690 for (i = 0; i < len; i++)
691 tmp[i] = 0;
693 ch = 1;
694 for (i = 0; i < GET_MODE_SIZE (mode); i++)
696 j = i;
697 if (WORDS_BIG_ENDIAN)
698 j = GET_MODE_SIZE (mode) - i - 1;
699 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
700 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
701 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
702 j *= BITS_PER_UNIT;
704 if (ch)
705 ch = (unsigned char) str[i];
706 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
709 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
710 return immed_wide_int_const (c, mode);
713 /* Cast a target constant CST to target CHAR and if that value fits into
714 host char type, return zero and put that value into variable pointed to by
715 P. */
717 static int
718 target_char_cast (tree cst, char *p)
720 unsigned HOST_WIDE_INT val, hostval;
722 if (TREE_CODE (cst) != INTEGER_CST
723 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
724 return 1;
726 /* Do not care if it fits or not right here. */
727 val = TREE_INT_CST_LOW (cst);
729 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
730 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
732 hostval = val;
733 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
734 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
736 if (val != hostval)
737 return 1;
739 *p = hostval;
740 return 0;
743 /* Similar to save_expr, but assumes that arbitrary code is not executed
744 in between the multiple evaluations. In particular, we assume that a
745 non-addressable local variable will not be modified. */
747 static tree
748 builtin_save_expr (tree exp)
750 if (TREE_CODE (exp) == SSA_NAME
751 || (TREE_ADDRESSABLE (exp) == 0
752 && (TREE_CODE (exp) == PARM_DECL
753 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
754 return exp;
756 return save_expr (exp);
759 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
760 times to get the address of either a higher stack frame, or a return
761 address located within it (depending on FNDECL_CODE). */
763 static rtx
764 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
766 int i;
768 #ifdef INITIAL_FRAME_ADDRESS_RTX
769 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
770 #else
771 rtx tem;
773 /* For a zero count with __builtin_return_address, we don't care what
774 frame address we return, because target-specific definitions will
775 override us. Therefore frame pointer elimination is OK, and using
776 the soft frame pointer is OK.
778 For a nonzero count, or a zero count with __builtin_frame_address,
779 we require a stable offset from the current frame pointer to the
780 previous one, so we must use the hard frame pointer, and
781 we must disable frame pointer elimination. */
782 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
783 tem = frame_pointer_rtx;
784 else
786 tem = hard_frame_pointer_rtx;
788 /* Tell reload not to eliminate the frame pointer. */
789 crtl->accesses_prior_frames = 1;
791 #endif
793 /* Some machines need special handling before we can access
794 arbitrary frames. For example, on the SPARC, we must first flush
795 all register windows to the stack. */
796 #ifdef SETUP_FRAME_ADDRESSES
797 if (count > 0)
798 SETUP_FRAME_ADDRESSES ();
799 #endif
801 /* On the SPARC, the return address is not in the frame, it is in a
802 register. There is no way to access it off of the current frame
803 pointer, but it can be accessed off the previous frame pointer by
804 reading the value from the register window save area. */
805 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
806 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
807 count--;
808 #endif
810 /* Scan back COUNT frames to the specified frame. */
811 for (i = 0; i < count; i++)
813 /* Assume the dynamic chain pointer is in the word that the
814 frame address points to, unless otherwise specified. */
815 #ifdef DYNAMIC_CHAIN_ADDRESS
816 tem = DYNAMIC_CHAIN_ADDRESS (tem);
817 #endif
818 tem = memory_address (Pmode, tem);
819 tem = gen_frame_mem (Pmode, tem);
820 tem = copy_to_reg (tem);
823 /* For __builtin_frame_address, return what we've got. But, on
824 the SPARC for example, we may have to add a bias. */
825 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
826 #ifdef FRAME_ADDR_RTX
827 return FRAME_ADDR_RTX (tem);
828 #else
829 return tem;
830 #endif
832 /* For __builtin_return_address, get the return address from that frame. */
833 #ifdef RETURN_ADDR_RTX
834 tem = RETURN_ADDR_RTX (count, tem);
835 #else
836 tem = memory_address (Pmode,
837 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
838 tem = gen_frame_mem (Pmode, tem);
839 #endif
840 return tem;
843 /* Alias set used for setjmp buffer. */
844 static alias_set_type setjmp_alias_set = -1;
846 /* Construct the leading half of a __builtin_setjmp call. Control will
847 return to RECEIVER_LABEL. This is also called directly by the SJLJ
848 exception handling code. */
850 void
851 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
853 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
854 rtx stack_save;
855 rtx mem;
857 if (setjmp_alias_set == -1)
858 setjmp_alias_set = new_alias_set ();
860 buf_addr = convert_memory_address (Pmode, buf_addr);
862 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
864 /* We store the frame pointer and the address of receiver_label in
865 the buffer and use the rest of it for the stack save area, which
866 is machine-dependent. */
868 mem = gen_rtx_MEM (Pmode, buf_addr);
869 set_mem_alias_set (mem, setjmp_alias_set);
870 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
872 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
873 GET_MODE_SIZE (Pmode))),
874 set_mem_alias_set (mem, setjmp_alias_set);
876 emit_move_insn (validize_mem (mem),
877 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
879 stack_save = gen_rtx_MEM (sa_mode,
880 plus_constant (Pmode, buf_addr,
881 2 * GET_MODE_SIZE (Pmode)));
882 set_mem_alias_set (stack_save, setjmp_alias_set);
883 emit_stack_save (SAVE_NONLOCAL, &stack_save);
885 /* If there is further processing to do, do it. */
886 #ifdef HAVE_builtin_setjmp_setup
887 if (HAVE_builtin_setjmp_setup)
888 emit_insn (gen_builtin_setjmp_setup (buf_addr));
889 #endif
891 /* We have a nonlocal label. */
892 cfun->has_nonlocal_label = 1;
895 /* Construct the trailing part of a __builtin_setjmp call. This is
896 also called directly by the SJLJ exception handling code.
897 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
899 void
900 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
902 rtx chain;
904 /* Mark the FP as used when we get here, so we have to make sure it's
905 marked as used by this function. */
906 emit_use (hard_frame_pointer_rtx);
908 /* Mark the static chain as clobbered here so life information
909 doesn't get messed up for it. */
910 chain = targetm.calls.static_chain (current_function_decl, true);
911 if (chain && REG_P (chain))
912 emit_clobber (chain);
914 /* Now put in the code to restore the frame pointer, and argument
915 pointer, if needed. */
916 #ifdef HAVE_nonlocal_goto
917 if (! HAVE_nonlocal_goto)
918 #endif
920 /* First adjust our frame pointer to its actual value. It was
921 previously set to the start of the virtual area corresponding to
922 the stacked variables when we branched here and now needs to be
923 adjusted to the actual hardware fp value.
925 Assignments to virtual registers are converted by
926 instantiate_virtual_regs into the corresponding assignment
927 to the underlying register (fp in this case) that makes
928 the original assignment true.
929 So the following insn will actually be decrementing fp by
930 STARTING_FRAME_OFFSET. */
931 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
933 /* Restoring the frame pointer also modifies the hard frame pointer.
934 Mark it used (so that the previous assignment remains live once
935 the frame pointer is eliminated) and clobbered (to represent the
936 implicit update from the assignment). */
937 emit_use (hard_frame_pointer_rtx);
938 emit_clobber (hard_frame_pointer_rtx);
941 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
942 if (fixed_regs[ARG_POINTER_REGNUM])
944 #ifdef ELIMINABLE_REGS
945 /* If the argument pointer can be eliminated in favor of the
946 frame pointer, we don't need to restore it. We assume here
947 that if such an elimination is present, it can always be used.
948 This is the case on all known machines; if we don't make this
949 assumption, we do unnecessary saving on many machines. */
950 size_t i;
951 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
953 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
954 if (elim_regs[i].from == ARG_POINTER_REGNUM
955 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
956 break;
958 if (i == ARRAY_SIZE (elim_regs))
959 #endif
961 /* Now restore our arg pointer from the address at which it
962 was saved in our stack frame. */
963 emit_move_insn (crtl->args.internal_arg_pointer,
964 copy_to_reg (get_arg_pointer_save_area ()));
967 #endif
969 #ifdef HAVE_builtin_setjmp_receiver
970 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
971 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
972 else
973 #endif
974 #ifdef HAVE_nonlocal_goto_receiver
975 if (HAVE_nonlocal_goto_receiver)
976 emit_insn (gen_nonlocal_goto_receiver ());
977 else
978 #endif
979 { /* Nothing */ }
981 /* We must not allow the code we just generated to be reordered by
982 scheduling. Specifically, the update of the frame pointer must
983 happen immediately, not later. */
984 emit_insn (gen_blockage ());
987 /* __builtin_longjmp is passed a pointer to an array of five words (not
988 all will be used on all machines). It operates similarly to the C
989 library function of the same name, but is more efficient. Much of
990 the code below is copied from the handling of non-local gotos. */
992 static void
993 expand_builtin_longjmp (rtx buf_addr, rtx value)
995 rtx fp, lab, stack;
996 rtx_insn *insn, *last;
997 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
999 /* DRAP is needed for stack realign if longjmp is expanded to current
1000 function */
1001 if (SUPPORTS_STACK_ALIGNMENT)
1002 crtl->need_drap = true;
1004 if (setjmp_alias_set == -1)
1005 setjmp_alias_set = new_alias_set ();
1007 buf_addr = convert_memory_address (Pmode, buf_addr);
1009 buf_addr = force_reg (Pmode, buf_addr);
1011 /* We require that the user must pass a second argument of 1, because
1012 that is what builtin_setjmp will return. */
1013 gcc_assert (value == const1_rtx);
1015 last = get_last_insn ();
1016 #ifdef HAVE_builtin_longjmp
1017 if (HAVE_builtin_longjmp)
1018 emit_insn (gen_builtin_longjmp (buf_addr));
1019 else
1020 #endif
1022 fp = gen_rtx_MEM (Pmode, buf_addr);
1023 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1024 GET_MODE_SIZE (Pmode)));
1026 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1027 2 * GET_MODE_SIZE (Pmode)));
1028 set_mem_alias_set (fp, setjmp_alias_set);
1029 set_mem_alias_set (lab, setjmp_alias_set);
1030 set_mem_alias_set (stack, setjmp_alias_set);
1032 /* Pick up FP, label, and SP from the block and jump. This code is
1033 from expand_goto in stmt.c; see there for detailed comments. */
1034 #ifdef HAVE_nonlocal_goto
1035 if (HAVE_nonlocal_goto)
1036 /* We have to pass a value to the nonlocal_goto pattern that will
1037 get copied into the static_chain pointer, but it does not matter
1038 what that value is, because builtin_setjmp does not use it. */
1039 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1040 else
1041 #endif
1043 lab = copy_to_reg (lab);
1045 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1046 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1048 emit_move_insn (hard_frame_pointer_rtx, fp);
1049 emit_stack_restore (SAVE_NONLOCAL, stack);
1051 emit_use (hard_frame_pointer_rtx);
1052 emit_use (stack_pointer_rtx);
1053 emit_indirect_jump (lab);
1057 /* Search backwards and mark the jump insn as a non-local goto.
1058 Note that this precludes the use of __builtin_longjmp to a
1059 __builtin_setjmp target in the same function. However, we've
1060 already cautioned the user that these functions are for
1061 internal exception handling use only. */
1062 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1064 gcc_assert (insn != last);
1066 if (JUMP_P (insn))
1068 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1069 break;
1071 else if (CALL_P (insn))
1072 break;
1076 static inline bool
1077 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1079 return (iter->i < iter->n);
1082 /* This function validates the types of a function call argument list
1083 against a specified list of tree_codes. If the last specifier is a 0,
1084 that represents an ellipses, otherwise the last specifier must be a
1085 VOID_TYPE. */
1087 static bool
1088 validate_arglist (const_tree callexpr, ...)
1090 enum tree_code code;
1091 bool res = 0;
1092 va_list ap;
1093 const_call_expr_arg_iterator iter;
1094 const_tree arg;
1096 va_start (ap, callexpr);
1097 init_const_call_expr_arg_iterator (callexpr, &iter);
1101 code = (enum tree_code) va_arg (ap, int);
1102 switch (code)
1104 case 0:
1105 /* This signifies an ellipses, any further arguments are all ok. */
1106 res = true;
1107 goto end;
1108 case VOID_TYPE:
1109 /* This signifies an endlink, if no arguments remain, return
1110 true, otherwise return false. */
1111 res = !more_const_call_expr_args_p (&iter);
1112 goto end;
1113 default:
1114 /* If no parameters remain or the parameter's code does not
1115 match the specified code, return false. Otherwise continue
1116 checking any remaining arguments. */
1117 arg = next_const_call_expr_arg (&iter);
1118 if (!validate_arg (arg, code))
1119 goto end;
1120 break;
1123 while (1);
1125 /* We need gotos here since we can only have one VA_CLOSE in a
1126 function. */
1127 end: ;
1128 va_end (ap);
1130 return res;
1133 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1134 and the address of the save area. */
1136 static rtx
1137 expand_builtin_nonlocal_goto (tree exp)
1139 tree t_label, t_save_area;
1140 rtx r_label, r_save_area, r_fp, r_sp;
1141 rtx_insn *insn;
1143 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1144 return NULL_RTX;
1146 t_label = CALL_EXPR_ARG (exp, 0);
1147 t_save_area = CALL_EXPR_ARG (exp, 1);
1149 r_label = expand_normal (t_label);
1150 r_label = convert_memory_address (Pmode, r_label);
1151 r_save_area = expand_normal (t_save_area);
1152 r_save_area = convert_memory_address (Pmode, r_save_area);
1153 /* Copy the address of the save location to a register just in case it was
1154 based on the frame pointer. */
1155 r_save_area = copy_to_reg (r_save_area);
1156 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1157 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1158 plus_constant (Pmode, r_save_area,
1159 GET_MODE_SIZE (Pmode)));
1161 crtl->has_nonlocal_goto = 1;
1163 #ifdef HAVE_nonlocal_goto
1164 /* ??? We no longer need to pass the static chain value, afaik. */
1165 if (HAVE_nonlocal_goto)
1166 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1167 else
1168 #endif
1170 r_label = copy_to_reg (r_label);
1172 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1173 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1175 /* Restore frame pointer for containing function. */
1176 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1177 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1179 /* USE of hard_frame_pointer_rtx added for consistency;
1180 not clear if really needed. */
1181 emit_use (hard_frame_pointer_rtx);
1182 emit_use (stack_pointer_rtx);
1184 /* If the architecture is using a GP register, we must
1185 conservatively assume that the target function makes use of it.
1186 The prologue of functions with nonlocal gotos must therefore
1187 initialize the GP register to the appropriate value, and we
1188 must then make sure that this value is live at the point
1189 of the jump. (Note that this doesn't necessarily apply
1190 to targets with a nonlocal_goto pattern; they are free
1191 to implement it in their own way. Note also that this is
1192 a no-op if the GP register is a global invariant.) */
1193 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1194 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1195 emit_use (pic_offset_table_rtx);
1197 emit_indirect_jump (r_label);
1200 /* Search backwards to the jump insn and mark it as a
1201 non-local goto. */
1202 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1204 if (JUMP_P (insn))
1206 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1207 break;
1209 else if (CALL_P (insn))
1210 break;
1213 return const0_rtx;
1216 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1217 (not all will be used on all machines) that was passed to __builtin_setjmp.
1218 It updates the stack pointer in that block to correspond to the current
1219 stack pointer. */
1221 static void
1222 expand_builtin_update_setjmp_buf (rtx buf_addr)
1224 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1225 rtx stack_save
1226 = gen_rtx_MEM (sa_mode,
1227 memory_address
1228 (sa_mode,
1229 plus_constant (Pmode, buf_addr,
1230 2 * GET_MODE_SIZE (Pmode))));
1232 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1235 /* Expand a call to __builtin_prefetch. For a target that does not support
1236 data prefetch, evaluate the memory address argument in case it has side
1237 effects. */
1239 static void
1240 expand_builtin_prefetch (tree exp)
1242 tree arg0, arg1, arg2;
1243 int nargs;
1244 rtx op0, op1, op2;
1246 if (!validate_arglist (exp, POINTER_TYPE, 0))
1247 return;
1249 arg0 = CALL_EXPR_ARG (exp, 0);
1251 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1252 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1253 locality). */
1254 nargs = call_expr_nargs (exp);
1255 if (nargs > 1)
1256 arg1 = CALL_EXPR_ARG (exp, 1);
1257 else
1258 arg1 = integer_zero_node;
1259 if (nargs > 2)
1260 arg2 = CALL_EXPR_ARG (exp, 2);
1261 else
1262 arg2 = integer_three_node;
1264 /* Argument 0 is an address. */
1265 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1267 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1268 if (TREE_CODE (arg1) != INTEGER_CST)
1270 error ("second argument to %<__builtin_prefetch%> must be a constant");
1271 arg1 = integer_zero_node;
1273 op1 = expand_normal (arg1);
1274 /* Argument 1 must be either zero or one. */
1275 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1277 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1278 " using zero");
1279 op1 = const0_rtx;
1282 /* Argument 2 (locality) must be a compile-time constant int. */
1283 if (TREE_CODE (arg2) != INTEGER_CST)
1285 error ("third argument to %<__builtin_prefetch%> must be a constant");
1286 arg2 = integer_zero_node;
1288 op2 = expand_normal (arg2);
1289 /* Argument 2 must be 0, 1, 2, or 3. */
1290 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1292 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1293 op2 = const0_rtx;
1296 #ifdef HAVE_prefetch
1297 if (HAVE_prefetch)
1299 struct expand_operand ops[3];
1301 create_address_operand (&ops[0], op0);
1302 create_integer_operand (&ops[1], INTVAL (op1));
1303 create_integer_operand (&ops[2], INTVAL (op2));
1304 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1305 return;
1307 #endif
1309 /* Don't do anything with direct references to volatile memory, but
1310 generate code to handle other side effects. */
1311 if (!MEM_P (op0) && side_effects_p (op0))
1312 emit_insn (op0);
1315 /* Get a MEM rtx for expression EXP which is the address of an operand
1316 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1317 the maximum length of the block of memory that might be accessed or
1318 NULL if unknown. */
1320 static rtx
1321 get_memory_rtx (tree exp, tree len)
1323 tree orig_exp = exp;
1324 rtx addr, mem;
1326 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1327 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1328 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1329 exp = TREE_OPERAND (exp, 0);
1331 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1332 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1334 /* Get an expression we can use to find the attributes to assign to MEM.
1335 First remove any nops. */
1336 while (CONVERT_EXPR_P (exp)
1337 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1338 exp = TREE_OPERAND (exp, 0);
1340 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1341 (as builtin stringops may alias with anything). */
1342 exp = fold_build2 (MEM_REF,
1343 build_array_type (char_type_node,
1344 build_range_type (sizetype,
1345 size_one_node, len)),
1346 exp, build_int_cst (ptr_type_node, 0));
1348 /* If the MEM_REF has no acceptable address, try to get the base object
1349 from the original address we got, and build an all-aliasing
1350 unknown-sized access to that one. */
1351 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1352 set_mem_attributes (mem, exp, 0);
1353 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1354 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1355 0))))
1357 exp = build_fold_addr_expr (exp);
1358 exp = fold_build2 (MEM_REF,
1359 build_array_type (char_type_node,
1360 build_range_type (sizetype,
1361 size_zero_node,
1362 NULL)),
1363 exp, build_int_cst (ptr_type_node, 0));
1364 set_mem_attributes (mem, exp, 0);
1366 set_mem_alias_set (mem, 0);
1367 return mem;
1370 /* Built-in functions to perform an untyped call and return. */
1372 #define apply_args_mode \
1373 (this_target_builtins->x_apply_args_mode)
1374 #define apply_result_mode \
1375 (this_target_builtins->x_apply_result_mode)
1377 /* Return the size required for the block returned by __builtin_apply_args,
1378 and initialize apply_args_mode. */
1380 static int
1381 apply_args_size (void)
1383 static int size = -1;
1384 int align;
1385 unsigned int regno;
1386 machine_mode mode;
1388 /* The values computed by this function never change. */
1389 if (size < 0)
1391 /* The first value is the incoming arg-pointer. */
1392 size = GET_MODE_SIZE (Pmode);
1394 /* The second value is the structure value address unless this is
1395 passed as an "invisible" first argument. */
1396 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1397 size += GET_MODE_SIZE (Pmode);
1399 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1400 if (FUNCTION_ARG_REGNO_P (regno))
1402 mode = targetm.calls.get_raw_arg_mode (regno);
1404 gcc_assert (mode != VOIDmode);
1406 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1407 if (size % align != 0)
1408 size = CEIL (size, align) * align;
1409 size += GET_MODE_SIZE (mode);
1410 apply_args_mode[regno] = mode;
1412 else
1414 apply_args_mode[regno] = VOIDmode;
1417 return size;
1420 /* Return the size required for the block returned by __builtin_apply,
1421 and initialize apply_result_mode. */
1423 static int
1424 apply_result_size (void)
1426 static int size = -1;
1427 int align, regno;
1428 machine_mode mode;
1430 /* The values computed by this function never change. */
1431 if (size < 0)
1433 size = 0;
1435 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1436 if (targetm.calls.function_value_regno_p (regno))
1438 mode = targetm.calls.get_raw_result_mode (regno);
1440 gcc_assert (mode != VOIDmode);
1442 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1443 if (size % align != 0)
1444 size = CEIL (size, align) * align;
1445 size += GET_MODE_SIZE (mode);
1446 apply_result_mode[regno] = mode;
1448 else
1449 apply_result_mode[regno] = VOIDmode;
1451 /* Allow targets that use untyped_call and untyped_return to override
1452 the size so that machine-specific information can be stored here. */
1453 #ifdef APPLY_RESULT_SIZE
1454 size = APPLY_RESULT_SIZE;
1455 #endif
1457 return size;
1460 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1461 /* Create a vector describing the result block RESULT. If SAVEP is true,
1462 the result block is used to save the values; otherwise it is used to
1463 restore the values. */
1465 static rtx
1466 result_vector (int savep, rtx result)
1468 int regno, size, align, nelts;
1469 machine_mode mode;
1470 rtx reg, mem;
1471 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1473 size = nelts = 0;
1474 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1475 if ((mode = apply_result_mode[regno]) != VOIDmode)
1477 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1478 if (size % align != 0)
1479 size = CEIL (size, align) * align;
1480 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1481 mem = adjust_address (result, mode, size);
1482 savevec[nelts++] = (savep
1483 ? gen_rtx_SET (VOIDmode, mem, reg)
1484 : gen_rtx_SET (VOIDmode, reg, mem));
1485 size += GET_MODE_SIZE (mode);
1487 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1489 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1491 /* Save the state required to perform an untyped call with the same
1492 arguments as were passed to the current function. */
1494 static rtx
1495 expand_builtin_apply_args_1 (void)
1497 rtx registers, tem;
1498 int size, align, regno;
1499 machine_mode mode;
1500 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1502 /* Create a block where the arg-pointer, structure value address,
1503 and argument registers can be saved. */
1504 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1506 /* Walk past the arg-pointer and structure value address. */
1507 size = GET_MODE_SIZE (Pmode);
1508 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1509 size += GET_MODE_SIZE (Pmode);
1511 /* Save each register used in calling a function to the block. */
1512 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1513 if ((mode = apply_args_mode[regno]) != VOIDmode)
1515 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1516 if (size % align != 0)
1517 size = CEIL (size, align) * align;
1519 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1521 emit_move_insn (adjust_address (registers, mode, size), tem);
1522 size += GET_MODE_SIZE (mode);
1525 /* Save the arg pointer to the block. */
1526 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1527 #ifdef STACK_GROWS_DOWNWARD
1528 /* We need the pointer as the caller actually passed them to us, not
1529 as we might have pretended they were passed. Make sure it's a valid
1530 operand, as emit_move_insn isn't expected to handle a PLUS. */
1532 = force_operand (plus_constant (Pmode, tem, crtl->args.pretend_args_size),
1533 NULL_RTX);
1534 #endif
1535 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1537 size = GET_MODE_SIZE (Pmode);
1539 /* Save the structure value address unless this is passed as an
1540 "invisible" first argument. */
1541 if (struct_incoming_value)
1543 emit_move_insn (adjust_address (registers, Pmode, size),
1544 copy_to_reg (struct_incoming_value));
1545 size += GET_MODE_SIZE (Pmode);
1548 /* Return the address of the block. */
1549 return copy_addr_to_reg (XEXP (registers, 0));
1552 /* __builtin_apply_args returns block of memory allocated on
1553 the stack into which is stored the arg pointer, structure
1554 value address, static chain, and all the registers that might
1555 possibly be used in performing a function call. The code is
1556 moved to the start of the function so the incoming values are
1557 saved. */
1559 static rtx
1560 expand_builtin_apply_args (void)
1562 /* Don't do __builtin_apply_args more than once in a function.
1563 Save the result of the first call and reuse it. */
1564 if (apply_args_value != 0)
1565 return apply_args_value;
1567 /* When this function is called, it means that registers must be
1568 saved on entry to this function. So we migrate the
1569 call to the first insn of this function. */
1570 rtx temp;
1571 rtx seq;
1573 start_sequence ();
1574 temp = expand_builtin_apply_args_1 ();
1575 seq = get_insns ();
1576 end_sequence ();
1578 apply_args_value = temp;
1580 /* Put the insns after the NOTE that starts the function.
1581 If this is inside a start_sequence, make the outer-level insn
1582 chain current, so the code is placed at the start of the
1583 function. If internal_arg_pointer is a non-virtual pseudo,
1584 it needs to be placed after the function that initializes
1585 that pseudo. */
1586 push_topmost_sequence ();
1587 if (REG_P (crtl->args.internal_arg_pointer)
1588 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1589 emit_insn_before (seq, parm_birth_insn);
1590 else
1591 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1592 pop_topmost_sequence ();
1593 return temp;
1597 /* Perform an untyped call and save the state required to perform an
1598 untyped return of whatever value was returned by the given function. */
1600 static rtx
1601 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1603 int size, align, regno;
1604 machine_mode mode;
1605 rtx incoming_args, result, reg, dest, src;
1606 rtx_call_insn *call_insn;
1607 rtx old_stack_level = 0;
1608 rtx call_fusage = 0;
1609 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1611 arguments = convert_memory_address (Pmode, arguments);
1613 /* Create a block where the return registers can be saved. */
1614 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1616 /* Fetch the arg pointer from the ARGUMENTS block. */
1617 incoming_args = gen_reg_rtx (Pmode);
1618 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1619 #ifndef STACK_GROWS_DOWNWARD
1620 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1621 incoming_args, 0, OPTAB_LIB_WIDEN);
1622 #endif
1624 /* Push a new argument block and copy the arguments. Do not allow
1625 the (potential) memcpy call below to interfere with our stack
1626 manipulations. */
1627 do_pending_stack_adjust ();
1628 NO_DEFER_POP;
1630 /* Save the stack with nonlocal if available. */
1631 #ifdef HAVE_save_stack_nonlocal
1632 if (HAVE_save_stack_nonlocal)
1633 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1634 else
1635 #endif
1636 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1638 /* Allocate a block of memory onto the stack and copy the memory
1639 arguments to the outgoing arguments address. We can pass TRUE
1640 as the 4th argument because we just saved the stack pointer
1641 and will restore it right after the call. */
1642 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1644 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1645 may have already set current_function_calls_alloca to true.
1646 current_function_calls_alloca won't be set if argsize is zero,
1647 so we have to guarantee need_drap is true here. */
1648 if (SUPPORTS_STACK_ALIGNMENT)
1649 crtl->need_drap = true;
1651 dest = virtual_outgoing_args_rtx;
1652 #ifndef STACK_GROWS_DOWNWARD
1653 if (CONST_INT_P (argsize))
1654 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1655 else
1656 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1657 #endif
1658 dest = gen_rtx_MEM (BLKmode, dest);
1659 set_mem_align (dest, PARM_BOUNDARY);
1660 src = gen_rtx_MEM (BLKmode, incoming_args);
1661 set_mem_align (src, PARM_BOUNDARY);
1662 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1664 /* Refer to the argument block. */
1665 apply_args_size ();
1666 arguments = gen_rtx_MEM (BLKmode, arguments);
1667 set_mem_align (arguments, PARM_BOUNDARY);
1669 /* Walk past the arg-pointer and structure value address. */
1670 size = GET_MODE_SIZE (Pmode);
1671 if (struct_value)
1672 size += GET_MODE_SIZE (Pmode);
1674 /* Restore each of the registers previously saved. Make USE insns
1675 for each of these registers for use in making the call. */
1676 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1677 if ((mode = apply_args_mode[regno]) != VOIDmode)
1679 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1680 if (size % align != 0)
1681 size = CEIL (size, align) * align;
1682 reg = gen_rtx_REG (mode, regno);
1683 emit_move_insn (reg, adjust_address (arguments, mode, size));
1684 use_reg (&call_fusage, reg);
1685 size += GET_MODE_SIZE (mode);
1688 /* Restore the structure value address unless this is passed as an
1689 "invisible" first argument. */
1690 size = GET_MODE_SIZE (Pmode);
1691 if (struct_value)
1693 rtx value = gen_reg_rtx (Pmode);
1694 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1695 emit_move_insn (struct_value, value);
1696 if (REG_P (struct_value))
1697 use_reg (&call_fusage, struct_value);
1698 size += GET_MODE_SIZE (Pmode);
1701 /* All arguments and registers used for the call are set up by now! */
1702 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1704 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1705 and we don't want to load it into a register as an optimization,
1706 because prepare_call_address already did it if it should be done. */
1707 if (GET_CODE (function) != SYMBOL_REF)
1708 function = memory_address (FUNCTION_MODE, function);
1710 /* Generate the actual call instruction and save the return value. */
1711 #ifdef HAVE_untyped_call
1712 if (HAVE_untyped_call)
1713 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1714 result, result_vector (1, result)));
1715 else
1716 #endif
1717 #ifdef HAVE_call_value
1718 if (HAVE_call_value)
1720 rtx valreg = 0;
1722 /* Locate the unique return register. It is not possible to
1723 express a call that sets more than one return register using
1724 call_value; use untyped_call for that. In fact, untyped_call
1725 only needs to save the return registers in the given block. */
1726 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1727 if ((mode = apply_result_mode[regno]) != VOIDmode)
1729 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1731 valreg = gen_rtx_REG (mode, regno);
1734 emit_call_insn (GEN_CALL_VALUE (valreg,
1735 gen_rtx_MEM (FUNCTION_MODE, function),
1736 const0_rtx, NULL_RTX, const0_rtx));
1738 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1740 else
1741 #endif
1742 gcc_unreachable ();
1744 /* Find the CALL insn we just emitted, and attach the register usage
1745 information. */
1746 call_insn = last_call_insn ();
1747 add_function_usage_to (call_insn, call_fusage);
1749 /* Restore the stack. */
1750 #ifdef HAVE_save_stack_nonlocal
1751 if (HAVE_save_stack_nonlocal)
1752 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1753 else
1754 #endif
1755 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1756 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1758 OK_DEFER_POP;
1760 /* Return the address of the result block. */
1761 result = copy_addr_to_reg (XEXP (result, 0));
1762 return convert_memory_address (ptr_mode, result);
1765 /* Perform an untyped return. */
1767 static void
1768 expand_builtin_return (rtx result)
1770 int size, align, regno;
1771 machine_mode mode;
1772 rtx reg;
1773 rtx_insn *call_fusage = 0;
1775 result = convert_memory_address (Pmode, result);
1777 apply_result_size ();
1778 result = gen_rtx_MEM (BLKmode, result);
1780 #ifdef HAVE_untyped_return
1781 if (HAVE_untyped_return)
1783 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1784 emit_barrier ();
1785 return;
1787 #endif
1789 /* Restore the return value and note that each value is used. */
1790 size = 0;
1791 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1792 if ((mode = apply_result_mode[regno]) != VOIDmode)
1794 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1795 if (size % align != 0)
1796 size = CEIL (size, align) * align;
1797 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1798 emit_move_insn (reg, adjust_address (result, mode, size));
1800 push_to_sequence (call_fusage);
1801 emit_use (reg);
1802 call_fusage = get_insns ();
1803 end_sequence ();
1804 size += GET_MODE_SIZE (mode);
1807 /* Put the USE insns before the return. */
1808 emit_insn (call_fusage);
1810 /* Return whatever values was restored by jumping directly to the end
1811 of the function. */
1812 expand_naked_return ();
1815 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1817 static enum type_class
1818 type_to_class (tree type)
1820 switch (TREE_CODE (type))
1822 case VOID_TYPE: return void_type_class;
1823 case INTEGER_TYPE: return integer_type_class;
1824 case ENUMERAL_TYPE: return enumeral_type_class;
1825 case BOOLEAN_TYPE: return boolean_type_class;
1826 case POINTER_TYPE: return pointer_type_class;
1827 case REFERENCE_TYPE: return reference_type_class;
1828 case OFFSET_TYPE: return offset_type_class;
1829 case REAL_TYPE: return real_type_class;
1830 case COMPLEX_TYPE: return complex_type_class;
1831 case FUNCTION_TYPE: return function_type_class;
1832 case METHOD_TYPE: return method_type_class;
1833 case RECORD_TYPE: return record_type_class;
1834 case UNION_TYPE:
1835 case QUAL_UNION_TYPE: return union_type_class;
1836 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1837 ? string_type_class : array_type_class);
1838 case LANG_TYPE: return lang_type_class;
1839 default: return no_type_class;
1843 /* Expand a call EXP to __builtin_classify_type. */
1845 static rtx
1846 expand_builtin_classify_type (tree exp)
1848 if (call_expr_nargs (exp))
1849 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1850 return GEN_INT (no_type_class);
1853 /* This helper macro, meant to be used in mathfn_built_in below,
1854 determines which among a set of three builtin math functions is
1855 appropriate for a given type mode. The `F' and `L' cases are
1856 automatically generated from the `double' case. */
1857 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1858 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1859 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1860 fcodel = BUILT_IN_MATHFN##L ; break;
1861 /* Similar to above, but appends _R after any F/L suffix. */
1862 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1863 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1864 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1865 fcodel = BUILT_IN_MATHFN##L_R ; break;
1867 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1868 if available. If IMPLICIT is true use the implicit builtin declaration,
1869 otherwise use the explicit declaration. If we can't do the conversion,
1870 return zero. */
1872 static tree
1873 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1875 enum built_in_function fcode, fcodef, fcodel, fcode2;
1877 switch (fn)
1879 CASE_MATHFN (BUILT_IN_ACOS)
1880 CASE_MATHFN (BUILT_IN_ACOSH)
1881 CASE_MATHFN (BUILT_IN_ASIN)
1882 CASE_MATHFN (BUILT_IN_ASINH)
1883 CASE_MATHFN (BUILT_IN_ATAN)
1884 CASE_MATHFN (BUILT_IN_ATAN2)
1885 CASE_MATHFN (BUILT_IN_ATANH)
1886 CASE_MATHFN (BUILT_IN_CBRT)
1887 CASE_MATHFN (BUILT_IN_CEIL)
1888 CASE_MATHFN (BUILT_IN_CEXPI)
1889 CASE_MATHFN (BUILT_IN_COPYSIGN)
1890 CASE_MATHFN (BUILT_IN_COS)
1891 CASE_MATHFN (BUILT_IN_COSH)
1892 CASE_MATHFN (BUILT_IN_DREM)
1893 CASE_MATHFN (BUILT_IN_ERF)
1894 CASE_MATHFN (BUILT_IN_ERFC)
1895 CASE_MATHFN (BUILT_IN_EXP)
1896 CASE_MATHFN (BUILT_IN_EXP10)
1897 CASE_MATHFN (BUILT_IN_EXP2)
1898 CASE_MATHFN (BUILT_IN_EXPM1)
1899 CASE_MATHFN (BUILT_IN_FABS)
1900 CASE_MATHFN (BUILT_IN_FDIM)
1901 CASE_MATHFN (BUILT_IN_FLOOR)
1902 CASE_MATHFN (BUILT_IN_FMA)
1903 CASE_MATHFN (BUILT_IN_FMAX)
1904 CASE_MATHFN (BUILT_IN_FMIN)
1905 CASE_MATHFN (BUILT_IN_FMOD)
1906 CASE_MATHFN (BUILT_IN_FREXP)
1907 CASE_MATHFN (BUILT_IN_GAMMA)
1908 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1909 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1910 CASE_MATHFN (BUILT_IN_HYPOT)
1911 CASE_MATHFN (BUILT_IN_ILOGB)
1912 CASE_MATHFN (BUILT_IN_ICEIL)
1913 CASE_MATHFN (BUILT_IN_IFLOOR)
1914 CASE_MATHFN (BUILT_IN_INF)
1915 CASE_MATHFN (BUILT_IN_IRINT)
1916 CASE_MATHFN (BUILT_IN_IROUND)
1917 CASE_MATHFN (BUILT_IN_ISINF)
1918 CASE_MATHFN (BUILT_IN_J0)
1919 CASE_MATHFN (BUILT_IN_J1)
1920 CASE_MATHFN (BUILT_IN_JN)
1921 CASE_MATHFN (BUILT_IN_LCEIL)
1922 CASE_MATHFN (BUILT_IN_LDEXP)
1923 CASE_MATHFN (BUILT_IN_LFLOOR)
1924 CASE_MATHFN (BUILT_IN_LGAMMA)
1925 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1926 CASE_MATHFN (BUILT_IN_LLCEIL)
1927 CASE_MATHFN (BUILT_IN_LLFLOOR)
1928 CASE_MATHFN (BUILT_IN_LLRINT)
1929 CASE_MATHFN (BUILT_IN_LLROUND)
1930 CASE_MATHFN (BUILT_IN_LOG)
1931 CASE_MATHFN (BUILT_IN_LOG10)
1932 CASE_MATHFN (BUILT_IN_LOG1P)
1933 CASE_MATHFN (BUILT_IN_LOG2)
1934 CASE_MATHFN (BUILT_IN_LOGB)
1935 CASE_MATHFN (BUILT_IN_LRINT)
1936 CASE_MATHFN (BUILT_IN_LROUND)
1937 CASE_MATHFN (BUILT_IN_MODF)
1938 CASE_MATHFN (BUILT_IN_NAN)
1939 CASE_MATHFN (BUILT_IN_NANS)
1940 CASE_MATHFN (BUILT_IN_NEARBYINT)
1941 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1942 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1943 CASE_MATHFN (BUILT_IN_POW)
1944 CASE_MATHFN (BUILT_IN_POWI)
1945 CASE_MATHFN (BUILT_IN_POW10)
1946 CASE_MATHFN (BUILT_IN_REMAINDER)
1947 CASE_MATHFN (BUILT_IN_REMQUO)
1948 CASE_MATHFN (BUILT_IN_RINT)
1949 CASE_MATHFN (BUILT_IN_ROUND)
1950 CASE_MATHFN (BUILT_IN_SCALB)
1951 CASE_MATHFN (BUILT_IN_SCALBLN)
1952 CASE_MATHFN (BUILT_IN_SCALBN)
1953 CASE_MATHFN (BUILT_IN_SIGNBIT)
1954 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1955 CASE_MATHFN (BUILT_IN_SIN)
1956 CASE_MATHFN (BUILT_IN_SINCOS)
1957 CASE_MATHFN (BUILT_IN_SINH)
1958 CASE_MATHFN (BUILT_IN_SQRT)
1959 CASE_MATHFN (BUILT_IN_TAN)
1960 CASE_MATHFN (BUILT_IN_TANH)
1961 CASE_MATHFN (BUILT_IN_TGAMMA)
1962 CASE_MATHFN (BUILT_IN_TRUNC)
1963 CASE_MATHFN (BUILT_IN_Y0)
1964 CASE_MATHFN (BUILT_IN_Y1)
1965 CASE_MATHFN (BUILT_IN_YN)
1967 default:
1968 return NULL_TREE;
1971 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1972 fcode2 = fcode;
1973 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1974 fcode2 = fcodef;
1975 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1976 fcode2 = fcodel;
1977 else
1978 return NULL_TREE;
1980 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1981 return NULL_TREE;
1983 return builtin_decl_explicit (fcode2);
1986 /* Like mathfn_built_in_1(), but always use the implicit array. */
1988 tree
1989 mathfn_built_in (tree type, enum built_in_function fn)
1991 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1994 /* If errno must be maintained, expand the RTL to check if the result,
1995 TARGET, of a built-in function call, EXP, is NaN, and if so set
1996 errno to EDOM. */
1998 static void
1999 expand_errno_check (tree exp, rtx target)
2001 rtx_code_label *lab = gen_label_rtx ();
2003 /* Test the result; if it is NaN, set errno=EDOM because
2004 the argument was not in the domain. */
2005 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
2006 NULL_RTX, NULL_RTX, lab,
2007 /* The jump is very likely. */
2008 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
2010 #ifdef TARGET_EDOM
2011 /* If this built-in doesn't throw an exception, set errno directly. */
2012 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
2014 #ifdef GEN_ERRNO_RTX
2015 rtx errno_rtx = GEN_ERRNO_RTX;
2016 #else
2017 rtx errno_rtx
2018 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
2019 #endif
2020 emit_move_insn (errno_rtx,
2021 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
2022 emit_label (lab);
2023 return;
2025 #endif
2027 /* Make sure the library call isn't expanded as a tail call. */
2028 CALL_EXPR_TAILCALL (exp) = 0;
2030 /* We can't set errno=EDOM directly; let the library call do it.
2031 Pop the arguments right away in case the call gets deleted. */
2032 NO_DEFER_POP;
2033 expand_call (exp, target, 0);
2034 OK_DEFER_POP;
2035 emit_label (lab);
2038 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2039 Return NULL_RTX if a normal call should be emitted rather than expanding
2040 the function in-line. EXP is the expression that is a call to the builtin
2041 function; if convenient, the result should be placed in TARGET.
2042 SUBTARGET may be used as the target for computing one of EXP's operands. */
2044 static rtx
2045 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2047 optab builtin_optab;
2048 rtx op0;
2049 rtx_insn *insns;
2050 tree fndecl = get_callee_fndecl (exp);
2051 machine_mode mode;
2052 bool errno_set = false;
2053 bool try_widening = false;
2054 tree arg;
2056 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2057 return NULL_RTX;
2059 arg = CALL_EXPR_ARG (exp, 0);
2061 switch (DECL_FUNCTION_CODE (fndecl))
2063 CASE_FLT_FN (BUILT_IN_SQRT):
2064 errno_set = ! tree_expr_nonnegative_p (arg);
2065 try_widening = true;
2066 builtin_optab = sqrt_optab;
2067 break;
2068 CASE_FLT_FN (BUILT_IN_EXP):
2069 errno_set = true; builtin_optab = exp_optab; break;
2070 CASE_FLT_FN (BUILT_IN_EXP10):
2071 CASE_FLT_FN (BUILT_IN_POW10):
2072 errno_set = true; builtin_optab = exp10_optab; break;
2073 CASE_FLT_FN (BUILT_IN_EXP2):
2074 errno_set = true; builtin_optab = exp2_optab; break;
2075 CASE_FLT_FN (BUILT_IN_EXPM1):
2076 errno_set = true; builtin_optab = expm1_optab; break;
2077 CASE_FLT_FN (BUILT_IN_LOGB):
2078 errno_set = true; builtin_optab = logb_optab; break;
2079 CASE_FLT_FN (BUILT_IN_LOG):
2080 errno_set = true; builtin_optab = log_optab; break;
2081 CASE_FLT_FN (BUILT_IN_LOG10):
2082 errno_set = true; builtin_optab = log10_optab; break;
2083 CASE_FLT_FN (BUILT_IN_LOG2):
2084 errno_set = true; builtin_optab = log2_optab; break;
2085 CASE_FLT_FN (BUILT_IN_LOG1P):
2086 errno_set = true; builtin_optab = log1p_optab; break;
2087 CASE_FLT_FN (BUILT_IN_ASIN):
2088 builtin_optab = asin_optab; break;
2089 CASE_FLT_FN (BUILT_IN_ACOS):
2090 builtin_optab = acos_optab; break;
2091 CASE_FLT_FN (BUILT_IN_TAN):
2092 builtin_optab = tan_optab; break;
2093 CASE_FLT_FN (BUILT_IN_ATAN):
2094 builtin_optab = atan_optab; break;
2095 CASE_FLT_FN (BUILT_IN_FLOOR):
2096 builtin_optab = floor_optab; break;
2097 CASE_FLT_FN (BUILT_IN_CEIL):
2098 builtin_optab = ceil_optab; break;
2099 CASE_FLT_FN (BUILT_IN_TRUNC):
2100 builtin_optab = btrunc_optab; break;
2101 CASE_FLT_FN (BUILT_IN_ROUND):
2102 builtin_optab = round_optab; break;
2103 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2104 builtin_optab = nearbyint_optab;
2105 if (flag_trapping_math)
2106 break;
2107 /* Else fallthrough and expand as rint. */
2108 CASE_FLT_FN (BUILT_IN_RINT):
2109 builtin_optab = rint_optab; break;
2110 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2111 builtin_optab = significand_optab; break;
2112 default:
2113 gcc_unreachable ();
2116 /* Make a suitable register to place result in. */
2117 mode = TYPE_MODE (TREE_TYPE (exp));
2119 if (! flag_errno_math || ! HONOR_NANS (mode))
2120 errno_set = false;
2122 /* Before working hard, check whether the instruction is available, but try
2123 to widen the mode for specific operations. */
2124 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2125 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2126 && (!errno_set || !optimize_insn_for_size_p ()))
2128 rtx result = gen_reg_rtx (mode);
2130 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2131 need to expand the argument again. This way, we will not perform
2132 side-effects more the once. */
2133 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2135 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2137 start_sequence ();
2139 /* Compute into RESULT.
2140 Set RESULT to wherever the result comes back. */
2141 result = expand_unop (mode, builtin_optab, op0, result, 0);
2143 if (result != 0)
2145 if (errno_set)
2146 expand_errno_check (exp, result);
2148 /* Output the entire sequence. */
2149 insns = get_insns ();
2150 end_sequence ();
2151 emit_insn (insns);
2152 return result;
2155 /* If we were unable to expand via the builtin, stop the sequence
2156 (without outputting the insns) and call to the library function
2157 with the stabilized argument list. */
2158 end_sequence ();
2161 return expand_call (exp, target, target == const0_rtx);
2164 /* Expand a call to the builtin binary math functions (pow and atan2).
2165 Return NULL_RTX if a normal call should be emitted rather than expanding the
2166 function in-line. EXP is the expression that is a call to the builtin
2167 function; if convenient, the result should be placed in TARGET.
2168 SUBTARGET may be used as the target for computing one of EXP's
2169 operands. */
2171 static rtx
2172 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2174 optab builtin_optab;
2175 rtx op0, op1, result;
2176 rtx_insn *insns;
2177 int op1_type = REAL_TYPE;
2178 tree fndecl = get_callee_fndecl (exp);
2179 tree arg0, arg1;
2180 machine_mode mode;
2181 bool errno_set = true;
2183 switch (DECL_FUNCTION_CODE (fndecl))
2185 CASE_FLT_FN (BUILT_IN_SCALBN):
2186 CASE_FLT_FN (BUILT_IN_SCALBLN):
2187 CASE_FLT_FN (BUILT_IN_LDEXP):
2188 op1_type = INTEGER_TYPE;
2189 default:
2190 break;
2193 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2194 return NULL_RTX;
2196 arg0 = CALL_EXPR_ARG (exp, 0);
2197 arg1 = CALL_EXPR_ARG (exp, 1);
2199 switch (DECL_FUNCTION_CODE (fndecl))
2201 CASE_FLT_FN (BUILT_IN_POW):
2202 builtin_optab = pow_optab; break;
2203 CASE_FLT_FN (BUILT_IN_ATAN2):
2204 builtin_optab = atan2_optab; break;
2205 CASE_FLT_FN (BUILT_IN_SCALB):
2206 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2207 return 0;
2208 builtin_optab = scalb_optab; break;
2209 CASE_FLT_FN (BUILT_IN_SCALBN):
2210 CASE_FLT_FN (BUILT_IN_SCALBLN):
2211 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2212 return 0;
2213 /* Fall through... */
2214 CASE_FLT_FN (BUILT_IN_LDEXP):
2215 builtin_optab = ldexp_optab; break;
2216 CASE_FLT_FN (BUILT_IN_FMOD):
2217 builtin_optab = fmod_optab; break;
2218 CASE_FLT_FN (BUILT_IN_REMAINDER):
2219 CASE_FLT_FN (BUILT_IN_DREM):
2220 builtin_optab = remainder_optab; break;
2221 default:
2222 gcc_unreachable ();
2225 /* Make a suitable register to place result in. */
2226 mode = TYPE_MODE (TREE_TYPE (exp));
2228 /* Before working hard, check whether the instruction is available. */
2229 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2230 return NULL_RTX;
2232 result = gen_reg_rtx (mode);
2234 if (! flag_errno_math || ! HONOR_NANS (mode))
2235 errno_set = false;
2237 if (errno_set && optimize_insn_for_size_p ())
2238 return 0;
2240 /* Always stabilize the argument list. */
2241 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2242 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2244 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2245 op1 = expand_normal (arg1);
2247 start_sequence ();
2249 /* Compute into RESULT.
2250 Set RESULT to wherever the result comes back. */
2251 result = expand_binop (mode, builtin_optab, op0, op1,
2252 result, 0, OPTAB_DIRECT);
2254 /* If we were unable to expand via the builtin, stop the sequence
2255 (without outputting the insns) and call to the library function
2256 with the stabilized argument list. */
2257 if (result == 0)
2259 end_sequence ();
2260 return expand_call (exp, target, target == const0_rtx);
2263 if (errno_set)
2264 expand_errno_check (exp, result);
2266 /* Output the entire sequence. */
2267 insns = get_insns ();
2268 end_sequence ();
2269 emit_insn (insns);
2271 return result;
2274 /* Expand a call to the builtin trinary math functions (fma).
2275 Return NULL_RTX if a normal call should be emitted rather than expanding the
2276 function in-line. EXP is the expression that is a call to the builtin
2277 function; if convenient, the result should be placed in TARGET.
2278 SUBTARGET may be used as the target for computing one of EXP's
2279 operands. */
2281 static rtx
2282 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2284 optab builtin_optab;
2285 rtx op0, op1, op2, result;
2286 rtx_insn *insns;
2287 tree fndecl = get_callee_fndecl (exp);
2288 tree arg0, arg1, arg2;
2289 machine_mode mode;
2291 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2292 return NULL_RTX;
2294 arg0 = CALL_EXPR_ARG (exp, 0);
2295 arg1 = CALL_EXPR_ARG (exp, 1);
2296 arg2 = CALL_EXPR_ARG (exp, 2);
2298 switch (DECL_FUNCTION_CODE (fndecl))
2300 CASE_FLT_FN (BUILT_IN_FMA):
2301 builtin_optab = fma_optab; break;
2302 default:
2303 gcc_unreachable ();
2306 /* Make a suitable register to place result in. */
2307 mode = TYPE_MODE (TREE_TYPE (exp));
2309 /* Before working hard, check whether the instruction is available. */
2310 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2311 return NULL_RTX;
2313 result = gen_reg_rtx (mode);
2315 /* Always stabilize the argument list. */
2316 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2317 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2318 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2320 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2321 op1 = expand_normal (arg1);
2322 op2 = expand_normal (arg2);
2324 start_sequence ();
2326 /* Compute into RESULT.
2327 Set RESULT to wherever the result comes back. */
2328 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2329 result, 0);
2331 /* If we were unable to expand via the builtin, stop the sequence
2332 (without outputting the insns) and call to the library function
2333 with the stabilized argument list. */
2334 if (result == 0)
2336 end_sequence ();
2337 return expand_call (exp, target, target == const0_rtx);
2340 /* Output the entire sequence. */
2341 insns = get_insns ();
2342 end_sequence ();
2343 emit_insn (insns);
2345 return result;
2348 /* Expand a call to the builtin sin and cos math functions.
2349 Return NULL_RTX if a normal call should be emitted rather than expanding the
2350 function in-line. EXP is the expression that is a call to the builtin
2351 function; if convenient, the result should be placed in TARGET.
2352 SUBTARGET may be used as the target for computing one of EXP's
2353 operands. */
2355 static rtx
2356 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2358 optab builtin_optab;
2359 rtx op0;
2360 rtx_insn *insns;
2361 tree fndecl = get_callee_fndecl (exp);
2362 machine_mode mode;
2363 tree arg;
2365 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2366 return NULL_RTX;
2368 arg = CALL_EXPR_ARG (exp, 0);
2370 switch (DECL_FUNCTION_CODE (fndecl))
2372 CASE_FLT_FN (BUILT_IN_SIN):
2373 CASE_FLT_FN (BUILT_IN_COS):
2374 builtin_optab = sincos_optab; break;
2375 default:
2376 gcc_unreachable ();
2379 /* Make a suitable register to place result in. */
2380 mode = TYPE_MODE (TREE_TYPE (exp));
2382 /* Check if sincos insn is available, otherwise fallback
2383 to sin or cos insn. */
2384 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2385 switch (DECL_FUNCTION_CODE (fndecl))
2387 CASE_FLT_FN (BUILT_IN_SIN):
2388 builtin_optab = sin_optab; break;
2389 CASE_FLT_FN (BUILT_IN_COS):
2390 builtin_optab = cos_optab; break;
2391 default:
2392 gcc_unreachable ();
2395 /* Before working hard, check whether the instruction is available. */
2396 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2398 rtx result = gen_reg_rtx (mode);
2400 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2401 need to expand the argument again. This way, we will not perform
2402 side-effects more the once. */
2403 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2405 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2407 start_sequence ();
2409 /* Compute into RESULT.
2410 Set RESULT to wherever the result comes back. */
2411 if (builtin_optab == sincos_optab)
2413 int ok;
2415 switch (DECL_FUNCTION_CODE (fndecl))
2417 CASE_FLT_FN (BUILT_IN_SIN):
2418 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2419 break;
2420 CASE_FLT_FN (BUILT_IN_COS):
2421 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2422 break;
2423 default:
2424 gcc_unreachable ();
2426 gcc_assert (ok);
2428 else
2429 result = expand_unop (mode, builtin_optab, op0, result, 0);
2431 if (result != 0)
2433 /* Output the entire sequence. */
2434 insns = get_insns ();
2435 end_sequence ();
2436 emit_insn (insns);
2437 return result;
2440 /* If we were unable to expand via the builtin, stop the sequence
2441 (without outputting the insns) and call to the library function
2442 with the stabilized argument list. */
2443 end_sequence ();
2446 return expand_call (exp, target, target == const0_rtx);
2449 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2450 return an RTL instruction code that implements the functionality.
2451 If that isn't possible or available return CODE_FOR_nothing. */
2453 static enum insn_code
2454 interclass_mathfn_icode (tree arg, tree fndecl)
2456 bool errno_set = false;
2457 optab builtin_optab = unknown_optab;
2458 machine_mode mode;
2460 switch (DECL_FUNCTION_CODE (fndecl))
2462 CASE_FLT_FN (BUILT_IN_ILOGB):
2463 errno_set = true; builtin_optab = ilogb_optab; break;
2464 CASE_FLT_FN (BUILT_IN_ISINF):
2465 builtin_optab = isinf_optab; break;
2466 case BUILT_IN_ISNORMAL:
2467 case BUILT_IN_ISFINITE:
2468 CASE_FLT_FN (BUILT_IN_FINITE):
2469 case BUILT_IN_FINITED32:
2470 case BUILT_IN_FINITED64:
2471 case BUILT_IN_FINITED128:
2472 case BUILT_IN_ISINFD32:
2473 case BUILT_IN_ISINFD64:
2474 case BUILT_IN_ISINFD128:
2475 /* These builtins have no optabs (yet). */
2476 break;
2477 default:
2478 gcc_unreachable ();
2481 /* There's no easy way to detect the case we need to set EDOM. */
2482 if (flag_errno_math && errno_set)
2483 return CODE_FOR_nothing;
2485 /* Optab mode depends on the mode of the input argument. */
2486 mode = TYPE_MODE (TREE_TYPE (arg));
2488 if (builtin_optab)
2489 return optab_handler (builtin_optab, mode);
2490 return CODE_FOR_nothing;
2493 /* Expand a call to one of the builtin math functions that operate on
2494 floating point argument and output an integer result (ilogb, isinf,
2495 isnan, etc).
2496 Return 0 if a normal call should be emitted rather than expanding the
2497 function in-line. EXP is the expression that is a call to the builtin
2498 function; if convenient, the result should be placed in TARGET. */
2500 static rtx
2501 expand_builtin_interclass_mathfn (tree exp, rtx target)
2503 enum insn_code icode = CODE_FOR_nothing;
2504 rtx op0;
2505 tree fndecl = get_callee_fndecl (exp);
2506 machine_mode mode;
2507 tree arg;
2509 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2510 return NULL_RTX;
2512 arg = CALL_EXPR_ARG (exp, 0);
2513 icode = interclass_mathfn_icode (arg, fndecl);
2514 mode = TYPE_MODE (TREE_TYPE (arg));
2516 if (icode != CODE_FOR_nothing)
2518 struct expand_operand ops[1];
2519 rtx_insn *last = get_last_insn ();
2520 tree orig_arg = arg;
2522 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2523 need to expand the argument again. This way, we will not perform
2524 side-effects more the once. */
2525 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2527 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2529 if (mode != GET_MODE (op0))
2530 op0 = convert_to_mode (mode, op0, 0);
2532 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2533 if (maybe_legitimize_operands (icode, 0, 1, ops)
2534 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2535 return ops[0].value;
2537 delete_insns_since (last);
2538 CALL_EXPR_ARG (exp, 0) = orig_arg;
2541 return NULL_RTX;
2544 /* Expand a call to the builtin sincos math function.
2545 Return NULL_RTX if a normal call should be emitted rather than expanding the
2546 function in-line. EXP is the expression that is a call to the builtin
2547 function. */
2549 static rtx
2550 expand_builtin_sincos (tree exp)
2552 rtx op0, op1, op2, target1, target2;
2553 machine_mode mode;
2554 tree arg, sinp, cosp;
2555 int result;
2556 location_t loc = EXPR_LOCATION (exp);
2557 tree alias_type, alias_off;
2559 if (!validate_arglist (exp, REAL_TYPE,
2560 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2561 return NULL_RTX;
2563 arg = CALL_EXPR_ARG (exp, 0);
2564 sinp = CALL_EXPR_ARG (exp, 1);
2565 cosp = CALL_EXPR_ARG (exp, 2);
2567 /* Make a suitable register to place result in. */
2568 mode = TYPE_MODE (TREE_TYPE (arg));
2570 /* Check if sincos insn is available, otherwise emit the call. */
2571 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2572 return NULL_RTX;
2574 target1 = gen_reg_rtx (mode);
2575 target2 = gen_reg_rtx (mode);
2577 op0 = expand_normal (arg);
2578 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2579 alias_off = build_int_cst (alias_type, 0);
2580 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2581 sinp, alias_off));
2582 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2583 cosp, alias_off));
2585 /* Compute into target1 and target2.
2586 Set TARGET to wherever the result comes back. */
2587 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2588 gcc_assert (result);
2590 /* Move target1 and target2 to the memory locations indicated
2591 by op1 and op2. */
2592 emit_move_insn (op1, target1);
2593 emit_move_insn (op2, target2);
2595 return const0_rtx;
2598 /* Expand a call to the internal cexpi builtin to the sincos math function.
2599 EXP is the expression that is a call to the builtin function; if convenient,
2600 the result should be placed in TARGET. */
2602 static rtx
2603 expand_builtin_cexpi (tree exp, rtx target)
2605 tree fndecl = get_callee_fndecl (exp);
2606 tree arg, type;
2607 machine_mode mode;
2608 rtx op0, op1, op2;
2609 location_t loc = EXPR_LOCATION (exp);
2611 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2612 return NULL_RTX;
2614 arg = CALL_EXPR_ARG (exp, 0);
2615 type = TREE_TYPE (arg);
2616 mode = TYPE_MODE (TREE_TYPE (arg));
2618 /* Try expanding via a sincos optab, fall back to emitting a libcall
2619 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2620 is only generated from sincos, cexp or if we have either of them. */
2621 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2623 op1 = gen_reg_rtx (mode);
2624 op2 = gen_reg_rtx (mode);
2626 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2628 /* Compute into op1 and op2. */
2629 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2631 else if (targetm.libc_has_function (function_sincos))
2633 tree call, fn = NULL_TREE;
2634 tree top1, top2;
2635 rtx op1a, op2a;
2637 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2638 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2639 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2640 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2641 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2642 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2643 else
2644 gcc_unreachable ();
2646 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2647 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2648 op1a = copy_addr_to_reg (XEXP (op1, 0));
2649 op2a = copy_addr_to_reg (XEXP (op2, 0));
2650 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2651 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2653 /* Make sure not to fold the sincos call again. */
2654 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2655 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2656 call, 3, arg, top1, top2));
2658 else
2660 tree call, fn = NULL_TREE, narg;
2661 tree ctype = build_complex_type (type);
2663 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2664 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2665 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2666 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2667 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2668 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2669 else
2670 gcc_unreachable ();
2672 /* If we don't have a decl for cexp create one. This is the
2673 friendliest fallback if the user calls __builtin_cexpi
2674 without full target C99 function support. */
2675 if (fn == NULL_TREE)
2677 tree fntype;
2678 const char *name = NULL;
2680 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2681 name = "cexpf";
2682 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2683 name = "cexp";
2684 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2685 name = "cexpl";
2687 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2688 fn = build_fn_decl (name, fntype);
2691 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2692 build_real (type, dconst0), arg);
2694 /* Make sure not to fold the cexp call again. */
2695 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2696 return expand_expr (build_call_nary (ctype, call, 1, narg),
2697 target, VOIDmode, EXPAND_NORMAL);
2700 /* Now build the proper return type. */
2701 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2702 make_tree (TREE_TYPE (arg), op2),
2703 make_tree (TREE_TYPE (arg), op1)),
2704 target, VOIDmode, EXPAND_NORMAL);
2707 /* Conveniently construct a function call expression. FNDECL names the
2708 function to be called, N is the number of arguments, and the "..."
2709 parameters are the argument expressions. Unlike build_call_exr
2710 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2712 static tree
2713 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2715 va_list ap;
2716 tree fntype = TREE_TYPE (fndecl);
2717 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2719 va_start (ap, n);
2720 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2721 va_end (ap);
2722 SET_EXPR_LOCATION (fn, loc);
2723 return fn;
2726 /* Expand a call to one of the builtin rounding functions gcc defines
2727 as an extension (lfloor and lceil). As these are gcc extensions we
2728 do not need to worry about setting errno to EDOM.
2729 If expanding via optab fails, lower expression to (int)(floor(x)).
2730 EXP is the expression that is a call to the builtin function;
2731 if convenient, the result should be placed in TARGET. */
2733 static rtx
2734 expand_builtin_int_roundingfn (tree exp, rtx target)
2736 convert_optab builtin_optab;
2737 rtx op0, tmp;
2738 rtx_insn *insns;
2739 tree fndecl = get_callee_fndecl (exp);
2740 enum built_in_function fallback_fn;
2741 tree fallback_fndecl;
2742 machine_mode mode;
2743 tree arg;
2745 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2746 gcc_unreachable ();
2748 arg = CALL_EXPR_ARG (exp, 0);
2750 switch (DECL_FUNCTION_CODE (fndecl))
2752 CASE_FLT_FN (BUILT_IN_ICEIL):
2753 CASE_FLT_FN (BUILT_IN_LCEIL):
2754 CASE_FLT_FN (BUILT_IN_LLCEIL):
2755 builtin_optab = lceil_optab;
2756 fallback_fn = BUILT_IN_CEIL;
2757 break;
2759 CASE_FLT_FN (BUILT_IN_IFLOOR):
2760 CASE_FLT_FN (BUILT_IN_LFLOOR):
2761 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2762 builtin_optab = lfloor_optab;
2763 fallback_fn = BUILT_IN_FLOOR;
2764 break;
2766 default:
2767 gcc_unreachable ();
2770 /* Make a suitable register to place result in. */
2771 mode = TYPE_MODE (TREE_TYPE (exp));
2773 target = gen_reg_rtx (mode);
2775 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2776 need to expand the argument again. This way, we will not perform
2777 side-effects more the once. */
2778 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2780 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2782 start_sequence ();
2784 /* Compute into TARGET. */
2785 if (expand_sfix_optab (target, op0, builtin_optab))
2787 /* Output the entire sequence. */
2788 insns = get_insns ();
2789 end_sequence ();
2790 emit_insn (insns);
2791 return target;
2794 /* If we were unable to expand via the builtin, stop the sequence
2795 (without outputting the insns). */
2796 end_sequence ();
2798 /* Fall back to floating point rounding optab. */
2799 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2801 /* For non-C99 targets we may end up without a fallback fndecl here
2802 if the user called __builtin_lfloor directly. In this case emit
2803 a call to the floor/ceil variants nevertheless. This should result
2804 in the best user experience for not full C99 targets. */
2805 if (fallback_fndecl == NULL_TREE)
2807 tree fntype;
2808 const char *name = NULL;
2810 switch (DECL_FUNCTION_CODE (fndecl))
2812 case BUILT_IN_ICEIL:
2813 case BUILT_IN_LCEIL:
2814 case BUILT_IN_LLCEIL:
2815 name = "ceil";
2816 break;
2817 case BUILT_IN_ICEILF:
2818 case BUILT_IN_LCEILF:
2819 case BUILT_IN_LLCEILF:
2820 name = "ceilf";
2821 break;
2822 case BUILT_IN_ICEILL:
2823 case BUILT_IN_LCEILL:
2824 case BUILT_IN_LLCEILL:
2825 name = "ceill";
2826 break;
2827 case BUILT_IN_IFLOOR:
2828 case BUILT_IN_LFLOOR:
2829 case BUILT_IN_LLFLOOR:
2830 name = "floor";
2831 break;
2832 case BUILT_IN_IFLOORF:
2833 case BUILT_IN_LFLOORF:
2834 case BUILT_IN_LLFLOORF:
2835 name = "floorf";
2836 break;
2837 case BUILT_IN_IFLOORL:
2838 case BUILT_IN_LFLOORL:
2839 case BUILT_IN_LLFLOORL:
2840 name = "floorl";
2841 break;
2842 default:
2843 gcc_unreachable ();
2846 fntype = build_function_type_list (TREE_TYPE (arg),
2847 TREE_TYPE (arg), NULL_TREE);
2848 fallback_fndecl = build_fn_decl (name, fntype);
2851 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2853 tmp = expand_normal (exp);
2854 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2856 /* Truncate the result of floating point optab to integer
2857 via expand_fix (). */
2858 target = gen_reg_rtx (mode);
2859 expand_fix (target, tmp, 0);
2861 return target;
2864 /* Expand a call to one of the builtin math functions doing integer
2865 conversion (lrint).
2866 Return 0 if a normal call should be emitted rather than expanding the
2867 function in-line. EXP is the expression that is a call to the builtin
2868 function; if convenient, the result should be placed in TARGET. */
2870 static rtx
2871 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2873 convert_optab builtin_optab;
2874 rtx op0;
2875 rtx_insn *insns;
2876 tree fndecl = get_callee_fndecl (exp);
2877 tree arg;
2878 machine_mode mode;
2879 enum built_in_function fallback_fn = BUILT_IN_NONE;
2881 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2882 gcc_unreachable ();
2884 arg = CALL_EXPR_ARG (exp, 0);
2886 switch (DECL_FUNCTION_CODE (fndecl))
2888 CASE_FLT_FN (BUILT_IN_IRINT):
2889 fallback_fn = BUILT_IN_LRINT;
2890 /* FALLTHRU */
2891 CASE_FLT_FN (BUILT_IN_LRINT):
2892 CASE_FLT_FN (BUILT_IN_LLRINT):
2893 builtin_optab = lrint_optab;
2894 break;
2896 CASE_FLT_FN (BUILT_IN_IROUND):
2897 fallback_fn = BUILT_IN_LROUND;
2898 /* FALLTHRU */
2899 CASE_FLT_FN (BUILT_IN_LROUND):
2900 CASE_FLT_FN (BUILT_IN_LLROUND):
2901 builtin_optab = lround_optab;
2902 break;
2904 default:
2905 gcc_unreachable ();
2908 /* There's no easy way to detect the case we need to set EDOM. */
2909 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2910 return NULL_RTX;
2912 /* Make a suitable register to place result in. */
2913 mode = TYPE_MODE (TREE_TYPE (exp));
2915 /* There's no easy way to detect the case we need to set EDOM. */
2916 if (!flag_errno_math)
2918 rtx result = gen_reg_rtx (mode);
2920 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2921 need to expand the argument again. This way, we will not perform
2922 side-effects more the once. */
2923 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2925 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2927 start_sequence ();
2929 if (expand_sfix_optab (result, op0, builtin_optab))
2931 /* Output the entire sequence. */
2932 insns = get_insns ();
2933 end_sequence ();
2934 emit_insn (insns);
2935 return result;
2938 /* If we were unable to expand via the builtin, stop the sequence
2939 (without outputting the insns) and call to the library function
2940 with the stabilized argument list. */
2941 end_sequence ();
2944 if (fallback_fn != BUILT_IN_NONE)
2946 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2947 targets, (int) round (x) should never be transformed into
2948 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2949 a call to lround in the hope that the target provides at least some
2950 C99 functions. This should result in the best user experience for
2951 not full C99 targets. */
2952 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2953 fallback_fn, 0);
2955 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2956 fallback_fndecl, 1, arg);
2958 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2959 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2960 return convert_to_mode (mode, target, 0);
2963 return expand_call (exp, target, target == const0_rtx);
2966 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2967 a normal call should be emitted rather than expanding the function
2968 in-line. EXP is the expression that is a call to the builtin
2969 function; if convenient, the result should be placed in TARGET. */
2971 static rtx
2972 expand_builtin_powi (tree exp, rtx target)
2974 tree arg0, arg1;
2975 rtx op0, op1;
2976 machine_mode mode;
2977 machine_mode mode2;
2979 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2980 return NULL_RTX;
2982 arg0 = CALL_EXPR_ARG (exp, 0);
2983 arg1 = CALL_EXPR_ARG (exp, 1);
2984 mode = TYPE_MODE (TREE_TYPE (exp));
2986 /* Emit a libcall to libgcc. */
2988 /* Mode of the 2nd argument must match that of an int. */
2989 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2991 if (target == NULL_RTX)
2992 target = gen_reg_rtx (mode);
2994 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2995 if (GET_MODE (op0) != mode)
2996 op0 = convert_to_mode (mode, op0, 0);
2997 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2998 if (GET_MODE (op1) != mode2)
2999 op1 = convert_to_mode (mode2, op1, 0);
3001 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3002 target, LCT_CONST, mode, 2,
3003 op0, mode, op1, mode2);
3005 return target;
3008 /* Expand expression EXP which is a call to the strlen builtin. Return
3009 NULL_RTX if we failed the caller should emit a normal call, otherwise
3010 try to get the result in TARGET, if convenient. */
3012 static rtx
3013 expand_builtin_strlen (tree exp, rtx target,
3014 machine_mode target_mode)
3016 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3017 return NULL_RTX;
3018 else
3020 struct expand_operand ops[4];
3021 rtx pat;
3022 tree len;
3023 tree src = CALL_EXPR_ARG (exp, 0);
3024 rtx src_reg;
3025 rtx_insn *before_strlen;
3026 machine_mode insn_mode = target_mode;
3027 enum insn_code icode = CODE_FOR_nothing;
3028 unsigned int align;
3030 /* If the length can be computed at compile-time, return it. */
3031 len = c_strlen (src, 0);
3032 if (len)
3033 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3035 /* If the length can be computed at compile-time and is constant
3036 integer, but there are side-effects in src, evaluate
3037 src for side-effects, then return len.
3038 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3039 can be optimized into: i++; x = 3; */
3040 len = c_strlen (src, 1);
3041 if (len && TREE_CODE (len) == INTEGER_CST)
3043 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3044 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3047 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3049 /* If SRC is not a pointer type, don't do this operation inline. */
3050 if (align == 0)
3051 return NULL_RTX;
3053 /* Bail out if we can't compute strlen in the right mode. */
3054 while (insn_mode != VOIDmode)
3056 icode = optab_handler (strlen_optab, insn_mode);
3057 if (icode != CODE_FOR_nothing)
3058 break;
3060 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3062 if (insn_mode == VOIDmode)
3063 return NULL_RTX;
3065 /* Make a place to hold the source address. We will not expand
3066 the actual source until we are sure that the expansion will
3067 not fail -- there are trees that cannot be expanded twice. */
3068 src_reg = gen_reg_rtx (Pmode);
3070 /* Mark the beginning of the strlen sequence so we can emit the
3071 source operand later. */
3072 before_strlen = get_last_insn ();
3074 create_output_operand (&ops[0], target, insn_mode);
3075 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3076 create_integer_operand (&ops[2], 0);
3077 create_integer_operand (&ops[3], align);
3078 if (!maybe_expand_insn (icode, 4, ops))
3079 return NULL_RTX;
3081 /* Now that we are assured of success, expand the source. */
3082 start_sequence ();
3083 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3084 if (pat != src_reg)
3086 #ifdef POINTERS_EXTEND_UNSIGNED
3087 if (GET_MODE (pat) != Pmode)
3088 pat = convert_to_mode (Pmode, pat,
3089 POINTERS_EXTEND_UNSIGNED);
3090 #endif
3091 emit_move_insn (src_reg, pat);
3093 pat = get_insns ();
3094 end_sequence ();
3096 if (before_strlen)
3097 emit_insn_after (pat, before_strlen);
3098 else
3099 emit_insn_before (pat, get_insns ());
3101 /* Return the value in the proper mode for this function. */
3102 if (GET_MODE (ops[0].value) == target_mode)
3103 target = ops[0].value;
3104 else if (target != 0)
3105 convert_move (target, ops[0].value, 0);
3106 else
3107 target = convert_to_mode (target_mode, ops[0].value, 0);
3109 return target;
3113 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3114 bytes from constant string DATA + OFFSET and return it as target
3115 constant. */
3117 static rtx
3118 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3119 machine_mode mode)
3121 const char *str = (const char *) data;
3123 gcc_assert (offset >= 0
3124 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3125 <= strlen (str) + 1));
3127 return c_readstr (str + offset, mode);
3130 /* LEN specify length of the block of memcpy/memset operation.
3131 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3132 In some cases we can make very likely guess on max size, then we
3133 set it into PROBABLE_MAX_SIZE. */
3135 static void
3136 determine_block_size (tree len, rtx len_rtx,
3137 unsigned HOST_WIDE_INT *min_size,
3138 unsigned HOST_WIDE_INT *max_size,
3139 unsigned HOST_WIDE_INT *probable_max_size)
3141 if (CONST_INT_P (len_rtx))
3143 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3144 return;
3146 else
3148 wide_int min, max;
3149 enum value_range_type range_type = VR_UNDEFINED;
3151 /* Determine bounds from the type. */
3152 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3153 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3154 else
3155 *min_size = 0;
3156 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3157 *probable_max_size = *max_size
3158 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3159 else
3160 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3162 if (TREE_CODE (len) == SSA_NAME)
3163 range_type = get_range_info (len, &min, &max);
3164 if (range_type == VR_RANGE)
3166 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3167 *min_size = min.to_uhwi ();
3168 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3169 *probable_max_size = *max_size = max.to_uhwi ();
3171 else if (range_type == VR_ANTI_RANGE)
3173 /* Anti range 0...N lets us to determine minimal size to N+1. */
3174 if (min == 0)
3176 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
3177 *min_size = max.to_uhwi () + 1;
3179 /* Code like
3181 int n;
3182 if (n < 100)
3183 memcpy (a, b, n)
3185 Produce anti range allowing negative values of N. We still
3186 can use the information and make a guess that N is not negative.
3188 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3189 *probable_max_size = min.to_uhwi () - 1;
3192 gcc_checking_assert (*max_size <=
3193 (unsigned HOST_WIDE_INT)
3194 GET_MODE_MASK (GET_MODE (len_rtx)));
3197 /* Helper function to do the actual work for expand_builtin_memcpy. */
3199 static rtx
3200 expand_builtin_memcpy_args (tree dest, tree src, tree len, rtx target, tree exp)
3202 const char *src_str;
3203 unsigned int src_align = get_pointer_alignment (src);
3204 unsigned int dest_align = get_pointer_alignment (dest);
3205 rtx dest_mem, src_mem, dest_addr, len_rtx;
3206 HOST_WIDE_INT expected_size = -1;
3207 unsigned int expected_align = 0;
3208 unsigned HOST_WIDE_INT min_size;
3209 unsigned HOST_WIDE_INT max_size;
3210 unsigned HOST_WIDE_INT probable_max_size;
3212 /* If DEST is not a pointer type, call the normal function. */
3213 if (dest_align == 0)
3214 return NULL_RTX;
3216 /* If either SRC is not a pointer type, don't do this
3217 operation in-line. */
3218 if (src_align == 0)
3219 return NULL_RTX;
3221 if (currently_expanding_gimple_stmt)
3222 stringop_block_profile (currently_expanding_gimple_stmt,
3223 &expected_align, &expected_size);
3225 if (expected_align < dest_align)
3226 expected_align = dest_align;
3227 dest_mem = get_memory_rtx (dest, len);
3228 set_mem_align (dest_mem, dest_align);
3229 len_rtx = expand_normal (len);
3230 determine_block_size (len, len_rtx, &min_size, &max_size,
3231 &probable_max_size);
3232 src_str = c_getstr (src);
3234 /* If SRC is a string constant and block move would be done
3235 by pieces, we can avoid loading the string from memory
3236 and only stored the computed constants. */
3237 if (src_str
3238 && CONST_INT_P (len_rtx)
3239 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3240 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3241 CONST_CAST (char *, src_str),
3242 dest_align, false))
3244 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3245 builtin_memcpy_read_str,
3246 CONST_CAST (char *, src_str),
3247 dest_align, false, 0);
3248 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3249 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3250 return dest_mem;
3253 src_mem = get_memory_rtx (src, len);
3254 set_mem_align (src_mem, src_align);
3256 /* Copy word part most expediently. */
3257 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3258 CALL_EXPR_TAILCALL (exp)
3259 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3260 expected_align, expected_size,
3261 min_size, max_size, probable_max_size);
3263 if (dest_addr == 0)
3265 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3266 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3269 return dest_addr;
3272 /* Expand a call EXP to the memcpy builtin.
3273 Return NULL_RTX if we failed, the caller should emit a normal call,
3274 otherwise try to get the result in TARGET, if convenient (and in
3275 mode MODE if that's convenient). */
3277 static rtx
3278 expand_builtin_memcpy (tree exp, rtx target)
3280 if (!validate_arglist (exp,
3281 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3282 return NULL_RTX;
3283 else
3285 tree dest = CALL_EXPR_ARG (exp, 0);
3286 tree src = CALL_EXPR_ARG (exp, 1);
3287 tree len = CALL_EXPR_ARG (exp, 2);
3288 return expand_builtin_memcpy_args (dest, src, len, target, exp);
3292 /* Expand an instrumented call EXP to the memcpy builtin.
3293 Return NULL_RTX if we failed, the caller should emit a normal call,
3294 otherwise try to get the result in TARGET, if convenient (and in
3295 mode MODE if that's convenient). */
3297 static rtx
3298 expand_builtin_memcpy_with_bounds (tree exp, rtx target)
3300 if (!validate_arglist (exp,
3301 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3302 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3303 INTEGER_TYPE, VOID_TYPE))
3304 return NULL_RTX;
3305 else
3307 tree dest = CALL_EXPR_ARG (exp, 0);
3308 tree src = CALL_EXPR_ARG (exp, 2);
3309 tree len = CALL_EXPR_ARG (exp, 4);
3310 rtx res = expand_builtin_memcpy_args (dest, src, len, target, exp);
3312 /* Return src bounds with the result. */
3313 if (res)
3315 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3316 expand_normal (CALL_EXPR_ARG (exp, 1)));
3317 res = chkp_join_splitted_slot (res, bnd);
3319 return res;
3323 /* Expand a call EXP to the mempcpy builtin.
3324 Return NULL_RTX if we failed; the caller should emit a normal call,
3325 otherwise try to get the result in TARGET, if convenient (and in
3326 mode MODE if that's convenient). If ENDP is 0 return the
3327 destination pointer, if ENDP is 1 return the end pointer ala
3328 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3329 stpcpy. */
3331 static rtx
3332 expand_builtin_mempcpy (tree exp, rtx target, machine_mode mode)
3334 if (!validate_arglist (exp,
3335 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3336 return NULL_RTX;
3337 else
3339 tree dest = CALL_EXPR_ARG (exp, 0);
3340 tree src = CALL_EXPR_ARG (exp, 1);
3341 tree len = CALL_EXPR_ARG (exp, 2);
3342 return expand_builtin_mempcpy_args (dest, src, len,
3343 target, mode, /*endp=*/ 1,
3344 exp);
3348 /* Expand an instrumented call EXP to the mempcpy builtin.
3349 Return NULL_RTX if we failed, the caller should emit a normal call,
3350 otherwise try to get the result in TARGET, if convenient (and in
3351 mode MODE if that's convenient). */
3353 static rtx
3354 expand_builtin_mempcpy_with_bounds (tree exp, rtx target, machine_mode mode)
3356 if (!validate_arglist (exp,
3357 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3358 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3359 INTEGER_TYPE, VOID_TYPE))
3360 return NULL_RTX;
3361 else
3363 tree dest = CALL_EXPR_ARG (exp, 0);
3364 tree src = CALL_EXPR_ARG (exp, 2);
3365 tree len = CALL_EXPR_ARG (exp, 4);
3366 rtx res = expand_builtin_mempcpy_args (dest, src, len, target,
3367 mode, 1, exp);
3369 /* Return src bounds with the result. */
3370 if (res)
3372 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3373 expand_normal (CALL_EXPR_ARG (exp, 1)));
3374 res = chkp_join_splitted_slot (res, bnd);
3376 return res;
3380 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3381 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3382 so that this can also be called without constructing an actual CALL_EXPR.
3383 The other arguments and return value are the same as for
3384 expand_builtin_mempcpy. */
3386 static rtx
3387 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3388 rtx target, machine_mode mode, int endp,
3389 tree orig_exp)
3391 tree fndecl = get_callee_fndecl (orig_exp);
3393 /* If return value is ignored, transform mempcpy into memcpy. */
3394 if (target == const0_rtx
3395 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
3396 && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP))
3398 tree fn = builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP);
3399 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3400 dest, src, len);
3401 return expand_expr (result, target, mode, EXPAND_NORMAL);
3403 else if (target == const0_rtx
3404 && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3406 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3407 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3408 dest, src, len);
3409 return expand_expr (result, target, mode, EXPAND_NORMAL);
3411 else
3413 const char *src_str;
3414 unsigned int src_align = get_pointer_alignment (src);
3415 unsigned int dest_align = get_pointer_alignment (dest);
3416 rtx dest_mem, src_mem, len_rtx;
3418 /* If either SRC or DEST is not a pointer type, don't do this
3419 operation in-line. */
3420 if (dest_align == 0 || src_align == 0)
3421 return NULL_RTX;
3423 /* If LEN is not constant, call the normal function. */
3424 if (! tree_fits_uhwi_p (len))
3425 return NULL_RTX;
3427 len_rtx = expand_normal (len);
3428 src_str = c_getstr (src);
3430 /* If SRC is a string constant and block move would be done
3431 by pieces, we can avoid loading the string from memory
3432 and only stored the computed constants. */
3433 if (src_str
3434 && CONST_INT_P (len_rtx)
3435 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3436 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3437 CONST_CAST (char *, src_str),
3438 dest_align, false))
3440 dest_mem = get_memory_rtx (dest, len);
3441 set_mem_align (dest_mem, dest_align);
3442 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3443 builtin_memcpy_read_str,
3444 CONST_CAST (char *, src_str),
3445 dest_align, false, endp);
3446 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3447 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3448 return dest_mem;
3451 if (CONST_INT_P (len_rtx)
3452 && can_move_by_pieces (INTVAL (len_rtx),
3453 MIN (dest_align, src_align)))
3455 dest_mem = get_memory_rtx (dest, len);
3456 set_mem_align (dest_mem, dest_align);
3457 src_mem = get_memory_rtx (src, len);
3458 set_mem_align (src_mem, src_align);
3459 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3460 MIN (dest_align, src_align), endp);
3461 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3462 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3463 return dest_mem;
3466 return NULL_RTX;
3470 #ifndef HAVE_movstr
3471 # define HAVE_movstr 0
3472 # define CODE_FOR_movstr CODE_FOR_nothing
3473 #endif
3475 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3476 we failed, the caller should emit a normal call, otherwise try to
3477 get the result in TARGET, if convenient. If ENDP is 0 return the
3478 destination pointer, if ENDP is 1 return the end pointer ala
3479 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3480 stpcpy. */
3482 static rtx
3483 expand_movstr (tree dest, tree src, rtx target, int endp)
3485 struct expand_operand ops[3];
3486 rtx dest_mem;
3487 rtx src_mem;
3489 if (!HAVE_movstr)
3490 return NULL_RTX;
3492 dest_mem = get_memory_rtx (dest, NULL);
3493 src_mem = get_memory_rtx (src, NULL);
3494 if (!endp)
3496 target = force_reg (Pmode, XEXP (dest_mem, 0));
3497 dest_mem = replace_equiv_address (dest_mem, target);
3500 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3501 create_fixed_operand (&ops[1], dest_mem);
3502 create_fixed_operand (&ops[2], src_mem);
3503 if (!maybe_expand_insn (CODE_FOR_movstr, 3, ops))
3504 return NULL_RTX;
3506 if (endp && target != const0_rtx)
3508 target = ops[0].value;
3509 /* movstr is supposed to set end to the address of the NUL
3510 terminator. If the caller requested a mempcpy-like return value,
3511 adjust it. */
3512 if (endp == 1)
3514 rtx tem = plus_constant (GET_MODE (target),
3515 gen_lowpart (GET_MODE (target), target), 1);
3516 emit_move_insn (target, force_operand (tem, NULL_RTX));
3519 return target;
3522 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3523 NULL_RTX if we failed the caller should emit a normal call, otherwise
3524 try to get the result in TARGET, if convenient (and in mode MODE if that's
3525 convenient). */
3527 static rtx
3528 expand_builtin_strcpy (tree exp, rtx target)
3530 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3532 tree dest = CALL_EXPR_ARG (exp, 0);
3533 tree src = CALL_EXPR_ARG (exp, 1);
3534 return expand_builtin_strcpy_args (dest, src, target);
3536 return NULL_RTX;
3539 /* Helper function to do the actual work for expand_builtin_strcpy. The
3540 arguments to the builtin_strcpy call DEST and SRC are broken out
3541 so that this can also be called without constructing an actual CALL_EXPR.
3542 The other arguments and return value are the same as for
3543 expand_builtin_strcpy. */
3545 static rtx
3546 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3548 return expand_movstr (dest, src, target, /*endp=*/0);
3551 /* Expand a call EXP to the stpcpy builtin.
3552 Return NULL_RTX if we failed the caller should emit a normal call,
3553 otherwise try to get the result in TARGET, if convenient (and in
3554 mode MODE if that's convenient). */
3556 static rtx
3557 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3559 tree dst, src;
3560 location_t loc = EXPR_LOCATION (exp);
3562 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3563 return NULL_RTX;
3565 dst = CALL_EXPR_ARG (exp, 0);
3566 src = CALL_EXPR_ARG (exp, 1);
3568 /* If return value is ignored, transform stpcpy into strcpy. */
3569 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3571 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3572 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3573 return expand_expr (result, target, mode, EXPAND_NORMAL);
3575 else
3577 tree len, lenp1;
3578 rtx ret;
3580 /* Ensure we get an actual string whose length can be evaluated at
3581 compile-time, not an expression containing a string. This is
3582 because the latter will potentially produce pessimized code
3583 when used to produce the return value. */
3584 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3585 return expand_movstr (dst, src, target, /*endp=*/2);
3587 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3588 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3589 target, mode, /*endp=*/2,
3590 exp);
3592 if (ret)
3593 return ret;
3595 if (TREE_CODE (len) == INTEGER_CST)
3597 rtx len_rtx = expand_normal (len);
3599 if (CONST_INT_P (len_rtx))
3601 ret = expand_builtin_strcpy_args (dst, src, target);
3603 if (ret)
3605 if (! target)
3607 if (mode != VOIDmode)
3608 target = gen_reg_rtx (mode);
3609 else
3610 target = gen_reg_rtx (GET_MODE (ret));
3612 if (GET_MODE (target) != GET_MODE (ret))
3613 ret = gen_lowpart (GET_MODE (target), ret);
3615 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3616 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3617 gcc_assert (ret);
3619 return target;
3624 return expand_movstr (dst, src, target, /*endp=*/2);
3628 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3629 bytes from constant string DATA + OFFSET and return it as target
3630 constant. */
3633 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3634 machine_mode mode)
3636 const char *str = (const char *) data;
3638 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3639 return const0_rtx;
3641 return c_readstr (str + offset, mode);
3644 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3645 NULL_RTX if we failed the caller should emit a normal call. */
3647 static rtx
3648 expand_builtin_strncpy (tree exp, rtx target)
3650 location_t loc = EXPR_LOCATION (exp);
3652 if (validate_arglist (exp,
3653 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3655 tree dest = CALL_EXPR_ARG (exp, 0);
3656 tree src = CALL_EXPR_ARG (exp, 1);
3657 tree len = CALL_EXPR_ARG (exp, 2);
3658 tree slen = c_strlen (src, 1);
3660 /* We must be passed a constant len and src parameter. */
3661 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3662 return NULL_RTX;
3664 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3666 /* We're required to pad with trailing zeros if the requested
3667 len is greater than strlen(s2)+1. In that case try to
3668 use store_by_pieces, if it fails, punt. */
3669 if (tree_int_cst_lt (slen, len))
3671 unsigned int dest_align = get_pointer_alignment (dest);
3672 const char *p = c_getstr (src);
3673 rtx dest_mem;
3675 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3676 || !can_store_by_pieces (tree_to_uhwi (len),
3677 builtin_strncpy_read_str,
3678 CONST_CAST (char *, p),
3679 dest_align, false))
3680 return NULL_RTX;
3682 dest_mem = get_memory_rtx (dest, len);
3683 store_by_pieces (dest_mem, tree_to_uhwi (len),
3684 builtin_strncpy_read_str,
3685 CONST_CAST (char *, p), dest_align, false, 0);
3686 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3687 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3688 return dest_mem;
3691 return NULL_RTX;
3694 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3695 bytes from constant string DATA + OFFSET and return it as target
3696 constant. */
3699 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3700 machine_mode mode)
3702 const char *c = (const char *) data;
3703 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3705 memset (p, *c, GET_MODE_SIZE (mode));
3707 return c_readstr (p, mode);
3710 /* Callback routine for store_by_pieces. Return the RTL of a register
3711 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3712 char value given in the RTL register data. For example, if mode is
3713 4 bytes wide, return the RTL for 0x01010101*data. */
3715 static rtx
3716 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3717 machine_mode mode)
3719 rtx target, coeff;
3720 size_t size;
3721 char *p;
3723 size = GET_MODE_SIZE (mode);
3724 if (size == 1)
3725 return (rtx) data;
3727 p = XALLOCAVEC (char, size);
3728 memset (p, 1, size);
3729 coeff = c_readstr (p, mode);
3731 target = convert_to_mode (mode, (rtx) data, 1);
3732 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3733 return force_reg (mode, target);
3736 /* Expand expression EXP, which is a call to the memset builtin. Return
3737 NULL_RTX if we failed the caller should emit a normal call, otherwise
3738 try to get the result in TARGET, if convenient (and in mode MODE if that's
3739 convenient). */
3741 static rtx
3742 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
3744 if (!validate_arglist (exp,
3745 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3746 return NULL_RTX;
3747 else
3749 tree dest = CALL_EXPR_ARG (exp, 0);
3750 tree val = CALL_EXPR_ARG (exp, 1);
3751 tree len = CALL_EXPR_ARG (exp, 2);
3752 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3756 /* Expand expression EXP, which is an instrumented call to the memset builtin.
3757 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
3758 try to get the result in TARGET, if convenient (and in mode MODE if that's
3759 convenient). */
3761 static rtx
3762 expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode)
3764 if (!validate_arglist (exp,
3765 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3766 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3767 return NULL_RTX;
3768 else
3770 tree dest = CALL_EXPR_ARG (exp, 0);
3771 tree val = CALL_EXPR_ARG (exp, 2);
3772 tree len = CALL_EXPR_ARG (exp, 3);
3773 rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp);
3775 /* Return src bounds with the result. */
3776 if (res)
3778 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3779 expand_normal (CALL_EXPR_ARG (exp, 1)));
3780 res = chkp_join_splitted_slot (res, bnd);
3782 return res;
3786 /* Helper function to do the actual work for expand_builtin_memset. The
3787 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3788 so that this can also be called without constructing an actual CALL_EXPR.
3789 The other arguments and return value are the same as for
3790 expand_builtin_memset. */
3792 static rtx
3793 expand_builtin_memset_args (tree dest, tree val, tree len,
3794 rtx target, machine_mode mode, tree orig_exp)
3796 tree fndecl, fn;
3797 enum built_in_function fcode;
3798 machine_mode val_mode;
3799 char c;
3800 unsigned int dest_align;
3801 rtx dest_mem, dest_addr, len_rtx;
3802 HOST_WIDE_INT expected_size = -1;
3803 unsigned int expected_align = 0;
3804 unsigned HOST_WIDE_INT min_size;
3805 unsigned HOST_WIDE_INT max_size;
3806 unsigned HOST_WIDE_INT probable_max_size;
3808 dest_align = get_pointer_alignment (dest);
3810 /* If DEST is not a pointer type, don't do this operation in-line. */
3811 if (dest_align == 0)
3812 return NULL_RTX;
3814 if (currently_expanding_gimple_stmt)
3815 stringop_block_profile (currently_expanding_gimple_stmt,
3816 &expected_align, &expected_size);
3818 if (expected_align < dest_align)
3819 expected_align = dest_align;
3821 /* If the LEN parameter is zero, return DEST. */
3822 if (integer_zerop (len))
3824 /* Evaluate and ignore VAL in case it has side-effects. */
3825 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3826 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3829 /* Stabilize the arguments in case we fail. */
3830 dest = builtin_save_expr (dest);
3831 val = builtin_save_expr (val);
3832 len = builtin_save_expr (len);
3834 len_rtx = expand_normal (len);
3835 determine_block_size (len, len_rtx, &min_size, &max_size,
3836 &probable_max_size);
3837 dest_mem = get_memory_rtx (dest, len);
3838 val_mode = TYPE_MODE (unsigned_char_type_node);
3840 if (TREE_CODE (val) != INTEGER_CST)
3842 rtx val_rtx;
3844 val_rtx = expand_normal (val);
3845 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3847 /* Assume that we can memset by pieces if we can store
3848 * the coefficients by pieces (in the required modes).
3849 * We can't pass builtin_memset_gen_str as that emits RTL. */
3850 c = 1;
3851 if (tree_fits_uhwi_p (len)
3852 && can_store_by_pieces (tree_to_uhwi (len),
3853 builtin_memset_read_str, &c, dest_align,
3854 true))
3856 val_rtx = force_reg (val_mode, val_rtx);
3857 store_by_pieces (dest_mem, tree_to_uhwi (len),
3858 builtin_memset_gen_str, val_rtx, dest_align,
3859 true, 0);
3861 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3862 dest_align, expected_align,
3863 expected_size, min_size, max_size,
3864 probable_max_size))
3865 goto do_libcall;
3867 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3868 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3869 return dest_mem;
3872 if (target_char_cast (val, &c))
3873 goto do_libcall;
3875 if (c)
3877 if (tree_fits_uhwi_p (len)
3878 && can_store_by_pieces (tree_to_uhwi (len),
3879 builtin_memset_read_str, &c, dest_align,
3880 true))
3881 store_by_pieces (dest_mem, tree_to_uhwi (len),
3882 builtin_memset_read_str, &c, dest_align, true, 0);
3883 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3884 gen_int_mode (c, val_mode),
3885 dest_align, expected_align,
3886 expected_size, min_size, max_size,
3887 probable_max_size))
3888 goto do_libcall;
3890 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3891 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3892 return dest_mem;
3895 set_mem_align (dest_mem, dest_align);
3896 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3897 CALL_EXPR_TAILCALL (orig_exp)
3898 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3899 expected_align, expected_size,
3900 min_size, max_size,
3901 probable_max_size);
3903 if (dest_addr == 0)
3905 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3906 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3909 return dest_addr;
3911 do_libcall:
3912 fndecl = get_callee_fndecl (orig_exp);
3913 fcode = DECL_FUNCTION_CODE (fndecl);
3914 if (fcode == BUILT_IN_MEMSET
3915 || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP)
3916 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3917 dest, val, len);
3918 else if (fcode == BUILT_IN_BZERO)
3919 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3920 dest, len);
3921 else
3922 gcc_unreachable ();
3923 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3924 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3925 return expand_call (fn, target, target == const0_rtx);
3928 /* Expand expression EXP, which is a call to the bzero builtin. Return
3929 NULL_RTX if we failed the caller should emit a normal call. */
3931 static rtx
3932 expand_builtin_bzero (tree exp)
3934 tree dest, size;
3935 location_t loc = EXPR_LOCATION (exp);
3937 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3938 return NULL_RTX;
3940 dest = CALL_EXPR_ARG (exp, 0);
3941 size = CALL_EXPR_ARG (exp, 1);
3943 /* New argument list transforming bzero(ptr x, int y) to
3944 memset(ptr x, int 0, size_t y). This is done this way
3945 so that if it isn't expanded inline, we fallback to
3946 calling bzero instead of memset. */
3948 return expand_builtin_memset_args (dest, integer_zero_node,
3949 fold_convert_loc (loc,
3950 size_type_node, size),
3951 const0_rtx, VOIDmode, exp);
3954 /* Expand expression EXP, which is a call to the memcmp built-in function.
3955 Return NULL_RTX if we failed and the caller should emit a normal call,
3956 otherwise try to get the result in TARGET, if convenient (and in mode
3957 MODE, if that's convenient). */
3959 static rtx
3960 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3961 ATTRIBUTE_UNUSED machine_mode mode)
3963 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3965 if (!validate_arglist (exp,
3966 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3967 return NULL_RTX;
3969 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3970 implementing memcmp because it will stop if it encounters two
3971 zero bytes. */
3972 #if defined HAVE_cmpmemsi
3974 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3975 rtx result;
3976 rtx insn;
3977 tree arg1 = CALL_EXPR_ARG (exp, 0);
3978 tree arg2 = CALL_EXPR_ARG (exp, 1);
3979 tree len = CALL_EXPR_ARG (exp, 2);
3981 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3982 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3983 machine_mode insn_mode;
3985 if (HAVE_cmpmemsi)
3986 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3987 else
3988 return NULL_RTX;
3990 /* If we don't have POINTER_TYPE, call the function. */
3991 if (arg1_align == 0 || arg2_align == 0)
3992 return NULL_RTX;
3994 /* Make a place to write the result of the instruction. */
3995 result = target;
3996 if (! (result != 0
3997 && REG_P (result) && GET_MODE (result) == insn_mode
3998 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3999 result = gen_reg_rtx (insn_mode);
4001 arg1_rtx = get_memory_rtx (arg1, len);
4002 arg2_rtx = get_memory_rtx (arg2, len);
4003 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4005 /* Set MEM_SIZE as appropriate. */
4006 if (CONST_INT_P (arg3_rtx))
4008 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
4009 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
4012 if (HAVE_cmpmemsi)
4013 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4014 GEN_INT (MIN (arg1_align, arg2_align)));
4015 else
4016 gcc_unreachable ();
4018 if (insn)
4019 emit_insn (insn);
4020 else
4021 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4022 TYPE_MODE (integer_type_node), 3,
4023 XEXP (arg1_rtx, 0), Pmode,
4024 XEXP (arg2_rtx, 0), Pmode,
4025 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4026 TYPE_UNSIGNED (sizetype)),
4027 TYPE_MODE (sizetype));
4029 /* Return the value in the proper mode for this function. */
4030 mode = TYPE_MODE (TREE_TYPE (exp));
4031 if (GET_MODE (result) == mode)
4032 return result;
4033 else if (target != 0)
4035 convert_move (target, result, 0);
4036 return target;
4038 else
4039 return convert_to_mode (mode, result, 0);
4041 #endif /* HAVE_cmpmemsi. */
4043 return NULL_RTX;
4046 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4047 if we failed the caller should emit a normal call, otherwise try to get
4048 the result in TARGET, if convenient. */
4050 static rtx
4051 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4053 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4054 return NULL_RTX;
4056 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4057 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
4058 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
4060 rtx arg1_rtx, arg2_rtx;
4061 rtx result, insn = NULL_RTX;
4062 tree fndecl, fn;
4063 tree arg1 = CALL_EXPR_ARG (exp, 0);
4064 tree arg2 = CALL_EXPR_ARG (exp, 1);
4066 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4067 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4069 /* If we don't have POINTER_TYPE, call the function. */
4070 if (arg1_align == 0 || arg2_align == 0)
4071 return NULL_RTX;
4073 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4074 arg1 = builtin_save_expr (arg1);
4075 arg2 = builtin_save_expr (arg2);
4077 arg1_rtx = get_memory_rtx (arg1, NULL);
4078 arg2_rtx = get_memory_rtx (arg2, NULL);
4080 #ifdef HAVE_cmpstrsi
4081 /* Try to call cmpstrsi. */
4082 if (HAVE_cmpstrsi)
4084 machine_mode insn_mode
4085 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4087 /* Make a place to write the result of the instruction. */
4088 result = target;
4089 if (! (result != 0
4090 && REG_P (result) && GET_MODE (result) == insn_mode
4091 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4092 result = gen_reg_rtx (insn_mode);
4094 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4095 GEN_INT (MIN (arg1_align, arg2_align)));
4097 #endif
4098 #ifdef HAVE_cmpstrnsi
4099 /* Try to determine at least one length and call cmpstrnsi. */
4100 if (!insn && HAVE_cmpstrnsi)
4102 tree len;
4103 rtx arg3_rtx;
4105 machine_mode insn_mode
4106 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4107 tree len1 = c_strlen (arg1, 1);
4108 tree len2 = c_strlen (arg2, 1);
4110 if (len1)
4111 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4112 if (len2)
4113 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4115 /* If we don't have a constant length for the first, use the length
4116 of the second, if we know it. We don't require a constant for
4117 this case; some cost analysis could be done if both are available
4118 but neither is constant. For now, assume they're equally cheap,
4119 unless one has side effects. If both strings have constant lengths,
4120 use the smaller. */
4122 if (!len1)
4123 len = len2;
4124 else if (!len2)
4125 len = len1;
4126 else if (TREE_SIDE_EFFECTS (len1))
4127 len = len2;
4128 else if (TREE_SIDE_EFFECTS (len2))
4129 len = len1;
4130 else if (TREE_CODE (len1) != INTEGER_CST)
4131 len = len2;
4132 else if (TREE_CODE (len2) != INTEGER_CST)
4133 len = len1;
4134 else if (tree_int_cst_lt (len1, len2))
4135 len = len1;
4136 else
4137 len = len2;
4139 /* If both arguments have side effects, we cannot optimize. */
4140 if (!len || TREE_SIDE_EFFECTS (len))
4141 goto do_libcall;
4143 arg3_rtx = expand_normal (len);
4145 /* Make a place to write the result of the instruction. */
4146 result = target;
4147 if (! (result != 0
4148 && REG_P (result) && GET_MODE (result) == insn_mode
4149 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4150 result = gen_reg_rtx (insn_mode);
4152 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4153 GEN_INT (MIN (arg1_align, arg2_align)));
4155 #endif
4157 if (insn)
4159 machine_mode mode;
4160 emit_insn (insn);
4162 /* Return the value in the proper mode for this function. */
4163 mode = TYPE_MODE (TREE_TYPE (exp));
4164 if (GET_MODE (result) == mode)
4165 return result;
4166 if (target == 0)
4167 return convert_to_mode (mode, result, 0);
4168 convert_move (target, result, 0);
4169 return target;
4172 /* Expand the library call ourselves using a stabilized argument
4173 list to avoid re-evaluating the function's arguments twice. */
4174 #ifdef HAVE_cmpstrnsi
4175 do_libcall:
4176 #endif
4177 fndecl = get_callee_fndecl (exp);
4178 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4179 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4180 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4181 return expand_call (fn, target, target == const0_rtx);
4183 #endif
4184 return NULL_RTX;
4187 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4188 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4189 the result in TARGET, if convenient. */
4191 static rtx
4192 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4193 ATTRIBUTE_UNUSED machine_mode mode)
4195 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4197 if (!validate_arglist (exp,
4198 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4199 return NULL_RTX;
4201 /* If c_strlen can determine an expression for one of the string
4202 lengths, and it doesn't have side effects, then emit cmpstrnsi
4203 using length MIN(strlen(string)+1, arg3). */
4204 #ifdef HAVE_cmpstrnsi
4205 if (HAVE_cmpstrnsi)
4207 tree len, len1, len2;
4208 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4209 rtx result, insn;
4210 tree fndecl, fn;
4211 tree arg1 = CALL_EXPR_ARG (exp, 0);
4212 tree arg2 = CALL_EXPR_ARG (exp, 1);
4213 tree arg3 = CALL_EXPR_ARG (exp, 2);
4215 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4216 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4217 machine_mode insn_mode
4218 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4220 len1 = c_strlen (arg1, 1);
4221 len2 = c_strlen (arg2, 1);
4223 if (len1)
4224 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4225 if (len2)
4226 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4228 /* If we don't have a constant length for the first, use the length
4229 of the second, if we know it. We don't require a constant for
4230 this case; some cost analysis could be done if both are available
4231 but neither is constant. For now, assume they're equally cheap,
4232 unless one has side effects. If both strings have constant lengths,
4233 use the smaller. */
4235 if (!len1)
4236 len = len2;
4237 else if (!len2)
4238 len = len1;
4239 else if (TREE_SIDE_EFFECTS (len1))
4240 len = len2;
4241 else if (TREE_SIDE_EFFECTS (len2))
4242 len = len1;
4243 else if (TREE_CODE (len1) != INTEGER_CST)
4244 len = len2;
4245 else if (TREE_CODE (len2) != INTEGER_CST)
4246 len = len1;
4247 else if (tree_int_cst_lt (len1, len2))
4248 len = len1;
4249 else
4250 len = len2;
4252 /* If both arguments have side effects, we cannot optimize. */
4253 if (!len || TREE_SIDE_EFFECTS (len))
4254 return NULL_RTX;
4256 /* The actual new length parameter is MIN(len,arg3). */
4257 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4258 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4260 /* If we don't have POINTER_TYPE, call the function. */
4261 if (arg1_align == 0 || arg2_align == 0)
4262 return NULL_RTX;
4264 /* Make a place to write the result of the instruction. */
4265 result = target;
4266 if (! (result != 0
4267 && REG_P (result) && GET_MODE (result) == insn_mode
4268 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4269 result = gen_reg_rtx (insn_mode);
4271 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4272 arg1 = builtin_save_expr (arg1);
4273 arg2 = builtin_save_expr (arg2);
4274 len = builtin_save_expr (len);
4276 arg1_rtx = get_memory_rtx (arg1, len);
4277 arg2_rtx = get_memory_rtx (arg2, len);
4278 arg3_rtx = expand_normal (len);
4279 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4280 GEN_INT (MIN (arg1_align, arg2_align)));
4281 if (insn)
4283 emit_insn (insn);
4285 /* Return the value in the proper mode for this function. */
4286 mode = TYPE_MODE (TREE_TYPE (exp));
4287 if (GET_MODE (result) == mode)
4288 return result;
4289 if (target == 0)
4290 return convert_to_mode (mode, result, 0);
4291 convert_move (target, result, 0);
4292 return target;
4295 /* Expand the library call ourselves using a stabilized argument
4296 list to avoid re-evaluating the function's arguments twice. */
4297 fndecl = get_callee_fndecl (exp);
4298 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4299 arg1, arg2, len);
4300 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4301 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4302 return expand_call (fn, target, target == const0_rtx);
4304 #endif
4305 return NULL_RTX;
4308 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4309 if that's convenient. */
4312 expand_builtin_saveregs (void)
4314 rtx val;
4315 rtx_insn *seq;
4317 /* Don't do __builtin_saveregs more than once in a function.
4318 Save the result of the first call and reuse it. */
4319 if (saveregs_value != 0)
4320 return saveregs_value;
4322 /* When this function is called, it means that registers must be
4323 saved on entry to this function. So we migrate the call to the
4324 first insn of this function. */
4326 start_sequence ();
4328 /* Do whatever the machine needs done in this case. */
4329 val = targetm.calls.expand_builtin_saveregs ();
4331 seq = get_insns ();
4332 end_sequence ();
4334 saveregs_value = val;
4336 /* Put the insns after the NOTE that starts the function. If this
4337 is inside a start_sequence, make the outer-level insn chain current, so
4338 the code is placed at the start of the function. */
4339 push_topmost_sequence ();
4340 emit_insn_after (seq, entry_of_function ());
4341 pop_topmost_sequence ();
4343 return val;
4346 /* Expand a call to __builtin_next_arg. */
4348 static rtx
4349 expand_builtin_next_arg (void)
4351 /* Checking arguments is already done in fold_builtin_next_arg
4352 that must be called before this function. */
4353 return expand_binop (ptr_mode, add_optab,
4354 crtl->args.internal_arg_pointer,
4355 crtl->args.arg_offset_rtx,
4356 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4359 /* Make it easier for the backends by protecting the valist argument
4360 from multiple evaluations. */
4362 static tree
4363 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4365 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4367 /* The current way of determining the type of valist is completely
4368 bogus. We should have the information on the va builtin instead. */
4369 if (!vatype)
4370 vatype = targetm.fn_abi_va_list (cfun->decl);
4372 if (TREE_CODE (vatype) == ARRAY_TYPE)
4374 if (TREE_SIDE_EFFECTS (valist))
4375 valist = save_expr (valist);
4377 /* For this case, the backends will be expecting a pointer to
4378 vatype, but it's possible we've actually been given an array
4379 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4380 So fix it. */
4381 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4383 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4384 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4387 else
4389 tree pt = build_pointer_type (vatype);
4391 if (! needs_lvalue)
4393 if (! TREE_SIDE_EFFECTS (valist))
4394 return valist;
4396 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4397 TREE_SIDE_EFFECTS (valist) = 1;
4400 if (TREE_SIDE_EFFECTS (valist))
4401 valist = save_expr (valist);
4402 valist = fold_build2_loc (loc, MEM_REF,
4403 vatype, valist, build_int_cst (pt, 0));
4406 return valist;
4409 /* The "standard" definition of va_list is void*. */
4411 tree
4412 std_build_builtin_va_list (void)
4414 return ptr_type_node;
4417 /* The "standard" abi va_list is va_list_type_node. */
4419 tree
4420 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4422 return va_list_type_node;
4425 /* The "standard" type of va_list is va_list_type_node. */
4427 tree
4428 std_canonical_va_list_type (tree type)
4430 tree wtype, htype;
4432 if (INDIRECT_REF_P (type))
4433 type = TREE_TYPE (type);
4434 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4435 type = TREE_TYPE (type);
4436 wtype = va_list_type_node;
4437 htype = type;
4438 /* Treat structure va_list types. */
4439 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4440 htype = TREE_TYPE (htype);
4441 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4443 /* If va_list is an array type, the argument may have decayed
4444 to a pointer type, e.g. by being passed to another function.
4445 In that case, unwrap both types so that we can compare the
4446 underlying records. */
4447 if (TREE_CODE (htype) == ARRAY_TYPE
4448 || POINTER_TYPE_P (htype))
4450 wtype = TREE_TYPE (wtype);
4451 htype = TREE_TYPE (htype);
4454 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4455 return va_list_type_node;
4457 return NULL_TREE;
4460 /* The "standard" implementation of va_start: just assign `nextarg' to
4461 the variable. */
4463 void
4464 std_expand_builtin_va_start (tree valist, rtx nextarg)
4466 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4467 convert_move (va_r, nextarg, 0);
4469 /* We do not have any valid bounds for the pointer, so
4470 just store zero bounds for it. */
4471 if (chkp_function_instrumented_p (current_function_decl))
4472 chkp_expand_bounds_reset_for_mem (valist,
4473 make_tree (TREE_TYPE (valist),
4474 nextarg));
4477 /* Expand EXP, a call to __builtin_va_start. */
4479 static rtx
4480 expand_builtin_va_start (tree exp)
4482 rtx nextarg;
4483 tree valist;
4484 location_t loc = EXPR_LOCATION (exp);
4486 if (call_expr_nargs (exp) < 2)
4488 error_at (loc, "too few arguments to function %<va_start%>");
4489 return const0_rtx;
4492 if (fold_builtin_next_arg (exp, true))
4493 return const0_rtx;
4495 nextarg = expand_builtin_next_arg ();
4496 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4498 if (targetm.expand_builtin_va_start)
4499 targetm.expand_builtin_va_start (valist, nextarg);
4500 else
4501 std_expand_builtin_va_start (valist, nextarg);
4503 return const0_rtx;
4506 /* Expand EXP, a call to __builtin_va_end. */
4508 static rtx
4509 expand_builtin_va_end (tree exp)
4511 tree valist = CALL_EXPR_ARG (exp, 0);
4513 /* Evaluate for side effects, if needed. I hate macros that don't
4514 do that. */
4515 if (TREE_SIDE_EFFECTS (valist))
4516 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4518 return const0_rtx;
4521 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4522 builtin rather than just as an assignment in stdarg.h because of the
4523 nastiness of array-type va_list types. */
4525 static rtx
4526 expand_builtin_va_copy (tree exp)
4528 tree dst, src, t;
4529 location_t loc = EXPR_LOCATION (exp);
4531 dst = CALL_EXPR_ARG (exp, 0);
4532 src = CALL_EXPR_ARG (exp, 1);
4534 dst = stabilize_va_list_loc (loc, dst, 1);
4535 src = stabilize_va_list_loc (loc, src, 0);
4537 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4539 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4541 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4542 TREE_SIDE_EFFECTS (t) = 1;
4543 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4545 else
4547 rtx dstb, srcb, size;
4549 /* Evaluate to pointers. */
4550 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4551 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4552 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4553 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4555 dstb = convert_memory_address (Pmode, dstb);
4556 srcb = convert_memory_address (Pmode, srcb);
4558 /* "Dereference" to BLKmode memories. */
4559 dstb = gen_rtx_MEM (BLKmode, dstb);
4560 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4561 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4562 srcb = gen_rtx_MEM (BLKmode, srcb);
4563 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4564 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4566 /* Copy. */
4567 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4570 return const0_rtx;
4573 /* Expand a call to one of the builtin functions __builtin_frame_address or
4574 __builtin_return_address. */
4576 static rtx
4577 expand_builtin_frame_address (tree fndecl, tree exp)
4579 /* The argument must be a nonnegative integer constant.
4580 It counts the number of frames to scan up the stack.
4581 The value is the return address saved in that frame. */
4582 if (call_expr_nargs (exp) == 0)
4583 /* Warning about missing arg was already issued. */
4584 return const0_rtx;
4585 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4587 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4588 error ("invalid argument to %<__builtin_frame_address%>");
4589 else
4590 error ("invalid argument to %<__builtin_return_address%>");
4591 return const0_rtx;
4593 else
4595 rtx tem
4596 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4597 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4599 /* Some ports cannot access arbitrary stack frames. */
4600 if (tem == NULL)
4602 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4603 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4604 else
4605 warning (0, "unsupported argument to %<__builtin_return_address%>");
4606 return const0_rtx;
4609 /* For __builtin_frame_address, return what we've got. */
4610 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4611 return tem;
4613 if (!REG_P (tem)
4614 && ! CONSTANT_P (tem))
4615 tem = copy_addr_to_reg (tem);
4616 return tem;
4620 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4621 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4622 is the same as for allocate_dynamic_stack_space. */
4624 static rtx
4625 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4627 rtx op0;
4628 rtx result;
4629 bool valid_arglist;
4630 unsigned int align;
4631 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4632 == BUILT_IN_ALLOCA_WITH_ALIGN);
4634 valid_arglist
4635 = (alloca_with_align
4636 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4637 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4639 if (!valid_arglist)
4640 return NULL_RTX;
4642 /* Compute the argument. */
4643 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4645 /* Compute the alignment. */
4646 align = (alloca_with_align
4647 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4648 : BIGGEST_ALIGNMENT);
4650 /* Allocate the desired space. */
4651 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4652 result = convert_memory_address (ptr_mode, result);
4654 return result;
4657 /* Expand a call to bswap builtin in EXP.
4658 Return NULL_RTX if a normal call should be emitted rather than expanding the
4659 function in-line. If convenient, the result should be placed in TARGET.
4660 SUBTARGET may be used as the target for computing one of EXP's operands. */
4662 static rtx
4663 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
4664 rtx subtarget)
4666 tree arg;
4667 rtx op0;
4669 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4670 return NULL_RTX;
4672 arg = CALL_EXPR_ARG (exp, 0);
4673 op0 = expand_expr (arg,
4674 subtarget && GET_MODE (subtarget) == target_mode
4675 ? subtarget : NULL_RTX,
4676 target_mode, EXPAND_NORMAL);
4677 if (GET_MODE (op0) != target_mode)
4678 op0 = convert_to_mode (target_mode, op0, 1);
4680 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4682 gcc_assert (target);
4684 return convert_to_mode (target_mode, target, 1);
4687 /* Expand a call to a unary builtin in EXP.
4688 Return NULL_RTX if a normal call should be emitted rather than expanding the
4689 function in-line. If convenient, the result should be placed in TARGET.
4690 SUBTARGET may be used as the target for computing one of EXP's operands. */
4692 static rtx
4693 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
4694 rtx subtarget, optab op_optab)
4696 rtx op0;
4698 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4699 return NULL_RTX;
4701 /* Compute the argument. */
4702 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4703 (subtarget
4704 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4705 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4706 VOIDmode, EXPAND_NORMAL);
4707 /* Compute op, into TARGET if possible.
4708 Set TARGET to wherever the result comes back. */
4709 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4710 op_optab, op0, target, op_optab != clrsb_optab);
4711 gcc_assert (target);
4713 return convert_to_mode (target_mode, target, 0);
4716 /* Expand a call to __builtin_expect. We just return our argument
4717 as the builtin_expect semantic should've been already executed by
4718 tree branch prediction pass. */
4720 static rtx
4721 expand_builtin_expect (tree exp, rtx target)
4723 tree arg;
4725 if (call_expr_nargs (exp) < 2)
4726 return const0_rtx;
4727 arg = CALL_EXPR_ARG (exp, 0);
4729 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4730 /* When guessing was done, the hints should be already stripped away. */
4731 gcc_assert (!flag_guess_branch_prob
4732 || optimize == 0 || seen_error ());
4733 return target;
4736 /* Expand a call to __builtin_assume_aligned. We just return our first
4737 argument as the builtin_assume_aligned semantic should've been already
4738 executed by CCP. */
4740 static rtx
4741 expand_builtin_assume_aligned (tree exp, rtx target)
4743 if (call_expr_nargs (exp) < 2)
4744 return const0_rtx;
4745 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4746 EXPAND_NORMAL);
4747 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4748 && (call_expr_nargs (exp) < 3
4749 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4750 return target;
4753 void
4754 expand_builtin_trap (void)
4756 #ifdef HAVE_trap
4757 if (HAVE_trap)
4759 rtx insn = emit_insn (gen_trap ());
4760 /* For trap insns when not accumulating outgoing args force
4761 REG_ARGS_SIZE note to prevent crossjumping of calls with
4762 different args sizes. */
4763 if (!ACCUMULATE_OUTGOING_ARGS)
4764 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4766 else
4767 #endif
4768 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4769 emit_barrier ();
4772 /* Expand a call to __builtin_unreachable. We do nothing except emit
4773 a barrier saying that control flow will not pass here.
4775 It is the responsibility of the program being compiled to ensure
4776 that control flow does never reach __builtin_unreachable. */
4777 static void
4778 expand_builtin_unreachable (void)
4780 emit_barrier ();
4783 /* Expand EXP, a call to fabs, fabsf or fabsl.
4784 Return NULL_RTX if a normal call should be emitted rather than expanding
4785 the function inline. If convenient, the result should be placed
4786 in TARGET. SUBTARGET may be used as the target for computing
4787 the operand. */
4789 static rtx
4790 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4792 machine_mode mode;
4793 tree arg;
4794 rtx op0;
4796 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4797 return NULL_RTX;
4799 arg = CALL_EXPR_ARG (exp, 0);
4800 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4801 mode = TYPE_MODE (TREE_TYPE (arg));
4802 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4803 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4806 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4807 Return NULL is a normal call should be emitted rather than expanding the
4808 function inline. If convenient, the result should be placed in TARGET.
4809 SUBTARGET may be used as the target for computing the operand. */
4811 static rtx
4812 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4814 rtx op0, op1;
4815 tree arg;
4817 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4818 return NULL_RTX;
4820 arg = CALL_EXPR_ARG (exp, 0);
4821 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4823 arg = CALL_EXPR_ARG (exp, 1);
4824 op1 = expand_normal (arg);
4826 return expand_copysign (op0, op1, target);
4829 /* Expand a call to __builtin___clear_cache. */
4831 static rtx
4832 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4834 #ifndef HAVE_clear_cache
4835 #ifdef CLEAR_INSN_CACHE
4836 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4837 does something. Just do the default expansion to a call to
4838 __clear_cache(). */
4839 return NULL_RTX;
4840 #else
4841 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4842 does nothing. There is no need to call it. Do nothing. */
4843 return const0_rtx;
4844 #endif /* CLEAR_INSN_CACHE */
4845 #else
4846 /* We have a "clear_cache" insn, and it will handle everything. */
4847 tree begin, end;
4848 rtx begin_rtx, end_rtx;
4850 /* We must not expand to a library call. If we did, any
4851 fallback library function in libgcc that might contain a call to
4852 __builtin___clear_cache() would recurse infinitely. */
4853 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4855 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4856 return const0_rtx;
4859 if (HAVE_clear_cache)
4861 struct expand_operand ops[2];
4863 begin = CALL_EXPR_ARG (exp, 0);
4864 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4866 end = CALL_EXPR_ARG (exp, 1);
4867 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4869 create_address_operand (&ops[0], begin_rtx);
4870 create_address_operand (&ops[1], end_rtx);
4871 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4872 return const0_rtx;
4874 return const0_rtx;
4875 #endif /* HAVE_clear_cache */
4878 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4880 static rtx
4881 round_trampoline_addr (rtx tramp)
4883 rtx temp, addend, mask;
4885 /* If we don't need too much alignment, we'll have been guaranteed
4886 proper alignment by get_trampoline_type. */
4887 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4888 return tramp;
4890 /* Round address up to desired boundary. */
4891 temp = gen_reg_rtx (Pmode);
4892 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4893 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4895 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4896 temp, 0, OPTAB_LIB_WIDEN);
4897 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4898 temp, 0, OPTAB_LIB_WIDEN);
4900 return tramp;
4903 static rtx
4904 expand_builtin_init_trampoline (tree exp, bool onstack)
4906 tree t_tramp, t_func, t_chain;
4907 rtx m_tramp, r_tramp, r_chain, tmp;
4909 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4910 POINTER_TYPE, VOID_TYPE))
4911 return NULL_RTX;
4913 t_tramp = CALL_EXPR_ARG (exp, 0);
4914 t_func = CALL_EXPR_ARG (exp, 1);
4915 t_chain = CALL_EXPR_ARG (exp, 2);
4917 r_tramp = expand_normal (t_tramp);
4918 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4919 MEM_NOTRAP_P (m_tramp) = 1;
4921 /* If ONSTACK, the TRAMP argument should be the address of a field
4922 within the local function's FRAME decl. Either way, let's see if
4923 we can fill in the MEM_ATTRs for this memory. */
4924 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4925 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4927 /* Creator of a heap trampoline is responsible for making sure the
4928 address is aligned to at least STACK_BOUNDARY. Normally malloc
4929 will ensure this anyhow. */
4930 tmp = round_trampoline_addr (r_tramp);
4931 if (tmp != r_tramp)
4933 m_tramp = change_address (m_tramp, BLKmode, tmp);
4934 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4935 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4938 /* The FUNC argument should be the address of the nested function.
4939 Extract the actual function decl to pass to the hook. */
4940 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4941 t_func = TREE_OPERAND (t_func, 0);
4942 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4944 r_chain = expand_normal (t_chain);
4946 /* Generate insns to initialize the trampoline. */
4947 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4949 if (onstack)
4951 trampolines_created = 1;
4953 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4954 "trampoline generated for nested function %qD", t_func);
4957 return const0_rtx;
4960 static rtx
4961 expand_builtin_adjust_trampoline (tree exp)
4963 rtx tramp;
4965 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4966 return NULL_RTX;
4968 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4969 tramp = round_trampoline_addr (tramp);
4970 if (targetm.calls.trampoline_adjust_address)
4971 tramp = targetm.calls.trampoline_adjust_address (tramp);
4973 return tramp;
4976 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4977 function. The function first checks whether the back end provides
4978 an insn to implement signbit for the respective mode. If not, it
4979 checks whether the floating point format of the value is such that
4980 the sign bit can be extracted. If that is not the case, the
4981 function returns NULL_RTX to indicate that a normal call should be
4982 emitted rather than expanding the function in-line. EXP is the
4983 expression that is a call to the builtin function; if convenient,
4984 the result should be placed in TARGET. */
4985 static rtx
4986 expand_builtin_signbit (tree exp, rtx target)
4988 const struct real_format *fmt;
4989 machine_mode fmode, imode, rmode;
4990 tree arg;
4991 int word, bitpos;
4992 enum insn_code icode;
4993 rtx temp;
4994 location_t loc = EXPR_LOCATION (exp);
4996 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4997 return NULL_RTX;
4999 arg = CALL_EXPR_ARG (exp, 0);
5000 fmode = TYPE_MODE (TREE_TYPE (arg));
5001 rmode = TYPE_MODE (TREE_TYPE (exp));
5002 fmt = REAL_MODE_FORMAT (fmode);
5004 arg = builtin_save_expr (arg);
5006 /* Expand the argument yielding a RTX expression. */
5007 temp = expand_normal (arg);
5009 /* Check if the back end provides an insn that handles signbit for the
5010 argument's mode. */
5011 icode = optab_handler (signbit_optab, fmode);
5012 if (icode != CODE_FOR_nothing)
5014 rtx_insn *last = get_last_insn ();
5015 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5016 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5017 return target;
5018 delete_insns_since (last);
5021 /* For floating point formats without a sign bit, implement signbit
5022 as "ARG < 0.0". */
5023 bitpos = fmt->signbit_ro;
5024 if (bitpos < 0)
5026 /* But we can't do this if the format supports signed zero. */
5027 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5028 return NULL_RTX;
5030 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5031 build_real (TREE_TYPE (arg), dconst0));
5032 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5035 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5037 imode = int_mode_for_mode (fmode);
5038 if (imode == BLKmode)
5039 return NULL_RTX;
5040 temp = gen_lowpart (imode, temp);
5042 else
5044 imode = word_mode;
5045 /* Handle targets with different FP word orders. */
5046 if (FLOAT_WORDS_BIG_ENDIAN)
5047 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5048 else
5049 word = bitpos / BITS_PER_WORD;
5050 temp = operand_subword_force (temp, word, fmode);
5051 bitpos = bitpos % BITS_PER_WORD;
5054 /* Force the intermediate word_mode (or narrower) result into a
5055 register. This avoids attempting to create paradoxical SUBREGs
5056 of floating point modes below. */
5057 temp = force_reg (imode, temp);
5059 /* If the bitpos is within the "result mode" lowpart, the operation
5060 can be implement with a single bitwise AND. Otherwise, we need
5061 a right shift and an AND. */
5063 if (bitpos < GET_MODE_BITSIZE (rmode))
5065 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5067 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5068 temp = gen_lowpart (rmode, temp);
5069 temp = expand_binop (rmode, and_optab, temp,
5070 immed_wide_int_const (mask, rmode),
5071 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5073 else
5075 /* Perform a logical right shift to place the signbit in the least
5076 significant bit, then truncate the result to the desired mode
5077 and mask just this bit. */
5078 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5079 temp = gen_lowpart (rmode, temp);
5080 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5081 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5084 return temp;
5087 /* Expand fork or exec calls. TARGET is the desired target of the
5088 call. EXP is the call. FN is the
5089 identificator of the actual function. IGNORE is nonzero if the
5090 value is to be ignored. */
5092 static rtx
5093 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5095 tree id, decl;
5096 tree call;
5098 /* If we are not profiling, just call the function. */
5099 if (!profile_arc_flag)
5100 return NULL_RTX;
5102 /* Otherwise call the wrapper. This should be equivalent for the rest of
5103 compiler, so the code does not diverge, and the wrapper may run the
5104 code necessary for keeping the profiling sane. */
5106 switch (DECL_FUNCTION_CODE (fn))
5108 case BUILT_IN_FORK:
5109 id = get_identifier ("__gcov_fork");
5110 break;
5112 case BUILT_IN_EXECL:
5113 id = get_identifier ("__gcov_execl");
5114 break;
5116 case BUILT_IN_EXECV:
5117 id = get_identifier ("__gcov_execv");
5118 break;
5120 case BUILT_IN_EXECLP:
5121 id = get_identifier ("__gcov_execlp");
5122 break;
5124 case BUILT_IN_EXECLE:
5125 id = get_identifier ("__gcov_execle");
5126 break;
5128 case BUILT_IN_EXECVP:
5129 id = get_identifier ("__gcov_execvp");
5130 break;
5132 case BUILT_IN_EXECVE:
5133 id = get_identifier ("__gcov_execve");
5134 break;
5136 default:
5137 gcc_unreachable ();
5140 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5141 FUNCTION_DECL, id, TREE_TYPE (fn));
5142 DECL_EXTERNAL (decl) = 1;
5143 TREE_PUBLIC (decl) = 1;
5144 DECL_ARTIFICIAL (decl) = 1;
5145 TREE_NOTHROW (decl) = 1;
5146 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5147 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5148 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5149 return expand_call (call, target, ignore);
5154 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5155 the pointer in these functions is void*, the tree optimizers may remove
5156 casts. The mode computed in expand_builtin isn't reliable either, due
5157 to __sync_bool_compare_and_swap.
5159 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5160 group of builtins. This gives us log2 of the mode size. */
5162 static inline machine_mode
5163 get_builtin_sync_mode (int fcode_diff)
5165 /* The size is not negotiable, so ask not to get BLKmode in return
5166 if the target indicates that a smaller size would be better. */
5167 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5170 /* Expand the memory expression LOC and return the appropriate memory operand
5171 for the builtin_sync operations. */
5173 static rtx
5174 get_builtin_sync_mem (tree loc, machine_mode mode)
5176 rtx addr, mem;
5178 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5179 addr = convert_memory_address (Pmode, addr);
5181 /* Note that we explicitly do not want any alias information for this
5182 memory, so that we kill all other live memories. Otherwise we don't
5183 satisfy the full barrier semantics of the intrinsic. */
5184 mem = validize_mem (gen_rtx_MEM (mode, addr));
5186 /* The alignment needs to be at least according to that of the mode. */
5187 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5188 get_pointer_alignment (loc)));
5189 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5190 MEM_VOLATILE_P (mem) = 1;
5192 return mem;
5195 /* Make sure an argument is in the right mode.
5196 EXP is the tree argument.
5197 MODE is the mode it should be in. */
5199 static rtx
5200 expand_expr_force_mode (tree exp, machine_mode mode)
5202 rtx val;
5203 machine_mode old_mode;
5205 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5206 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5207 of CONST_INTs, where we know the old_mode only from the call argument. */
5209 old_mode = GET_MODE (val);
5210 if (old_mode == VOIDmode)
5211 old_mode = TYPE_MODE (TREE_TYPE (exp));
5212 val = convert_modes (mode, old_mode, val, 1);
5213 return val;
5217 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5218 EXP is the CALL_EXPR. CODE is the rtx code
5219 that corresponds to the arithmetic or logical operation from the name;
5220 an exception here is that NOT actually means NAND. TARGET is an optional
5221 place for us to store the results; AFTER is true if this is the
5222 fetch_and_xxx form. */
5224 static rtx
5225 expand_builtin_sync_operation (machine_mode mode, tree exp,
5226 enum rtx_code code, bool after,
5227 rtx target)
5229 rtx val, mem;
5230 location_t loc = EXPR_LOCATION (exp);
5232 if (code == NOT && warn_sync_nand)
5234 tree fndecl = get_callee_fndecl (exp);
5235 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5237 static bool warned_f_a_n, warned_n_a_f;
5239 switch (fcode)
5241 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5242 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5243 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5244 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5245 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5246 if (warned_f_a_n)
5247 break;
5249 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5250 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5251 warned_f_a_n = true;
5252 break;
5254 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5255 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5256 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5257 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5258 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5259 if (warned_n_a_f)
5260 break;
5262 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5263 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5264 warned_n_a_f = true;
5265 break;
5267 default:
5268 gcc_unreachable ();
5272 /* Expand the operands. */
5273 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5274 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5276 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5277 after);
5280 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5281 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5282 true if this is the boolean form. TARGET is a place for us to store the
5283 results; this is NOT optional if IS_BOOL is true. */
5285 static rtx
5286 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5287 bool is_bool, rtx target)
5289 rtx old_val, new_val, mem;
5290 rtx *pbool, *poval;
5292 /* Expand the operands. */
5293 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5294 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5295 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5297 pbool = poval = NULL;
5298 if (target != const0_rtx)
5300 if (is_bool)
5301 pbool = &target;
5302 else
5303 poval = &target;
5305 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5306 false, MEMMODEL_SEQ_CST,
5307 MEMMODEL_SEQ_CST))
5308 return NULL_RTX;
5310 return target;
5313 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5314 general form is actually an atomic exchange, and some targets only
5315 support a reduced form with the second argument being a constant 1.
5316 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5317 the results. */
5319 static rtx
5320 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5321 rtx target)
5323 rtx val, mem;
5325 /* Expand the operands. */
5326 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5327 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5329 return expand_sync_lock_test_and_set (target, mem, val);
5332 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5334 static void
5335 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5337 rtx mem;
5339 /* Expand the operands. */
5340 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5342 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5345 /* Given an integer representing an ``enum memmodel'', verify its
5346 correctness and return the memory model enum. */
5348 static enum memmodel
5349 get_memmodel (tree exp)
5351 rtx op;
5352 unsigned HOST_WIDE_INT val;
5354 /* If the parameter is not a constant, it's a run time value so we'll just
5355 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5356 if (TREE_CODE (exp) != INTEGER_CST)
5357 return MEMMODEL_SEQ_CST;
5359 op = expand_normal (exp);
5361 val = INTVAL (op);
5362 if (targetm.memmodel_check)
5363 val = targetm.memmodel_check (val);
5364 else if (val & ~MEMMODEL_MASK)
5366 warning (OPT_Winvalid_memory_model,
5367 "Unknown architecture specifier in memory model to builtin.");
5368 return MEMMODEL_SEQ_CST;
5371 if ((INTVAL (op) & MEMMODEL_MASK) >= MEMMODEL_LAST)
5373 warning (OPT_Winvalid_memory_model,
5374 "invalid memory model argument to builtin");
5375 return MEMMODEL_SEQ_CST;
5378 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5379 be conservative and promote consume to acquire. */
5380 if (val == MEMMODEL_CONSUME)
5381 val = MEMMODEL_ACQUIRE;
5383 return (enum memmodel) val;
5386 /* Expand the __atomic_exchange intrinsic:
5387 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5388 EXP is the CALL_EXPR.
5389 TARGET is an optional place for us to store the results. */
5391 static rtx
5392 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5394 rtx val, mem;
5395 enum memmodel model;
5397 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5399 if (!flag_inline_atomics)
5400 return NULL_RTX;
5402 /* Expand the operands. */
5403 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5404 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5406 return expand_atomic_exchange (target, mem, val, model);
5409 /* Expand the __atomic_compare_exchange intrinsic:
5410 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5411 TYPE desired, BOOL weak,
5412 enum memmodel success,
5413 enum memmodel failure)
5414 EXP is the CALL_EXPR.
5415 TARGET is an optional place for us to store the results. */
5417 static rtx
5418 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5419 rtx target)
5421 rtx expect, desired, mem, oldval;
5422 rtx_code_label *label;
5423 enum memmodel success, failure;
5424 tree weak;
5425 bool is_weak;
5427 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5428 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5430 if (failure > success)
5432 warning (OPT_Winvalid_memory_model,
5433 "failure memory model cannot be stronger than success memory "
5434 "model for %<__atomic_compare_exchange%>");
5435 success = MEMMODEL_SEQ_CST;
5438 if ((failure & MEMMODEL_MASK) == MEMMODEL_RELEASE
5439 || (failure & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5441 warning (OPT_Winvalid_memory_model,
5442 "invalid failure memory model for "
5443 "%<__atomic_compare_exchange%>");
5444 failure = MEMMODEL_SEQ_CST;
5445 success = MEMMODEL_SEQ_CST;
5449 if (!flag_inline_atomics)
5450 return NULL_RTX;
5452 /* Expand the operands. */
5453 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5455 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5456 expect = convert_memory_address (Pmode, expect);
5457 expect = gen_rtx_MEM (mode, expect);
5458 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5460 weak = CALL_EXPR_ARG (exp, 3);
5461 is_weak = false;
5462 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5463 is_weak = true;
5465 if (target == const0_rtx)
5466 target = NULL;
5468 /* Lest the rtl backend create a race condition with an imporoper store
5469 to memory, always create a new pseudo for OLDVAL. */
5470 oldval = NULL;
5472 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5473 is_weak, success, failure))
5474 return NULL_RTX;
5476 /* Conditionally store back to EXPECT, lest we create a race condition
5477 with an improper store to memory. */
5478 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5479 the normal case where EXPECT is totally private, i.e. a register. At
5480 which point the store can be unconditional. */
5481 label = gen_label_rtx ();
5482 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL, VOIDmode, 1, label);
5483 emit_move_insn (expect, oldval);
5484 emit_label (label);
5486 return target;
5489 /* Expand the __atomic_load intrinsic:
5490 TYPE __atomic_load (TYPE *object, enum memmodel)
5491 EXP is the CALL_EXPR.
5492 TARGET is an optional place for us to store the results. */
5494 static rtx
5495 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
5497 rtx mem;
5498 enum memmodel model;
5500 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5501 if ((model & MEMMODEL_MASK) == MEMMODEL_RELEASE
5502 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5504 warning (OPT_Winvalid_memory_model,
5505 "invalid memory model for %<__atomic_load%>");
5506 model = MEMMODEL_SEQ_CST;
5509 if (!flag_inline_atomics)
5510 return NULL_RTX;
5512 /* Expand the operand. */
5513 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5515 return expand_atomic_load (target, mem, model);
5519 /* Expand the __atomic_store intrinsic:
5520 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5521 EXP is the CALL_EXPR.
5522 TARGET is an optional place for us to store the results. */
5524 static rtx
5525 expand_builtin_atomic_store (machine_mode mode, tree exp)
5527 rtx mem, val;
5528 enum memmodel model;
5530 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5531 if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED
5532 && (model & MEMMODEL_MASK) != MEMMODEL_SEQ_CST
5533 && (model & MEMMODEL_MASK) != MEMMODEL_RELEASE)
5535 warning (OPT_Winvalid_memory_model,
5536 "invalid memory model for %<__atomic_store%>");
5537 model = MEMMODEL_SEQ_CST;
5540 if (!flag_inline_atomics)
5541 return NULL_RTX;
5543 /* Expand the operands. */
5544 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5545 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5547 return expand_atomic_store (mem, val, model, false);
5550 /* Expand the __atomic_fetch_XXX intrinsic:
5551 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5552 EXP is the CALL_EXPR.
5553 TARGET is an optional place for us to store the results.
5554 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5555 FETCH_AFTER is true if returning the result of the operation.
5556 FETCH_AFTER is false if returning the value before the operation.
5557 IGNORE is true if the result is not used.
5558 EXT_CALL is the correct builtin for an external call if this cannot be
5559 resolved to an instruction sequence. */
5561 static rtx
5562 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
5563 enum rtx_code code, bool fetch_after,
5564 bool ignore, enum built_in_function ext_call)
5566 rtx val, mem, ret;
5567 enum memmodel model;
5568 tree fndecl;
5569 tree addr;
5571 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5573 /* Expand the operands. */
5574 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5575 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5577 /* Only try generating instructions if inlining is turned on. */
5578 if (flag_inline_atomics)
5580 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5581 if (ret)
5582 return ret;
5585 /* Return if a different routine isn't needed for the library call. */
5586 if (ext_call == BUILT_IN_NONE)
5587 return NULL_RTX;
5589 /* Change the call to the specified function. */
5590 fndecl = get_callee_fndecl (exp);
5591 addr = CALL_EXPR_FN (exp);
5592 STRIP_NOPS (addr);
5594 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5595 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5597 /* Expand the call here so we can emit trailing code. */
5598 ret = expand_call (exp, target, ignore);
5600 /* Replace the original function just in case it matters. */
5601 TREE_OPERAND (addr, 0) = fndecl;
5603 /* Then issue the arithmetic correction to return the right result. */
5604 if (!ignore)
5606 if (code == NOT)
5608 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5609 OPTAB_LIB_WIDEN);
5610 ret = expand_simple_unop (mode, NOT, ret, target, true);
5612 else
5613 ret = expand_simple_binop (mode, code, ret, val, target, true,
5614 OPTAB_LIB_WIDEN);
5616 return ret;
5620 #ifndef HAVE_atomic_clear
5621 # define HAVE_atomic_clear 0
5622 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5623 #endif
5625 /* Expand an atomic clear operation.
5626 void _atomic_clear (BOOL *obj, enum memmodel)
5627 EXP is the call expression. */
5629 static rtx
5630 expand_builtin_atomic_clear (tree exp)
5632 machine_mode mode;
5633 rtx mem, ret;
5634 enum memmodel model;
5636 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5637 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5638 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5640 if ((model & MEMMODEL_MASK) == MEMMODEL_CONSUME
5641 || (model & MEMMODEL_MASK) == MEMMODEL_ACQUIRE
5642 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
5644 warning (OPT_Winvalid_memory_model,
5645 "invalid memory model for %<__atomic_store%>");
5646 model = MEMMODEL_SEQ_CST;
5649 if (HAVE_atomic_clear)
5651 emit_insn (gen_atomic_clear (mem, model));
5652 return const0_rtx;
5655 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5656 Failing that, a store is issued by __atomic_store. The only way this can
5657 fail is if the bool type is larger than a word size. Unlikely, but
5658 handle it anyway for completeness. Assume a single threaded model since
5659 there is no atomic support in this case, and no barriers are required. */
5660 ret = expand_atomic_store (mem, const0_rtx, model, true);
5661 if (!ret)
5662 emit_move_insn (mem, const0_rtx);
5663 return const0_rtx;
5666 /* Expand an atomic test_and_set operation.
5667 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5668 EXP is the call expression. */
5670 static rtx
5671 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5673 rtx mem;
5674 enum memmodel model;
5675 machine_mode mode;
5677 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5678 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5679 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5681 return expand_atomic_test_and_set (target, mem, model);
5685 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5686 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5688 static tree
5689 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5691 int size;
5692 machine_mode mode;
5693 unsigned int mode_align, type_align;
5695 if (TREE_CODE (arg0) != INTEGER_CST)
5696 return NULL_TREE;
5698 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5699 mode = mode_for_size (size, MODE_INT, 0);
5700 mode_align = GET_MODE_ALIGNMENT (mode);
5702 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5703 type_align = mode_align;
5704 else
5706 tree ttype = TREE_TYPE (arg1);
5708 /* This function is usually invoked and folded immediately by the front
5709 end before anything else has a chance to look at it. The pointer
5710 parameter at this point is usually cast to a void *, so check for that
5711 and look past the cast. */
5712 if (CONVERT_EXPR_P (arg1) && POINTER_TYPE_P (ttype)
5713 && VOID_TYPE_P (TREE_TYPE (ttype)))
5714 arg1 = TREE_OPERAND (arg1, 0);
5716 ttype = TREE_TYPE (arg1);
5717 gcc_assert (POINTER_TYPE_P (ttype));
5719 /* Get the underlying type of the object. */
5720 ttype = TREE_TYPE (ttype);
5721 type_align = TYPE_ALIGN (ttype);
5724 /* If the object has smaller alignment, the the lock free routines cannot
5725 be used. */
5726 if (type_align < mode_align)
5727 return boolean_false_node;
5729 /* Check if a compare_and_swap pattern exists for the mode which represents
5730 the required size. The pattern is not allowed to fail, so the existence
5731 of the pattern indicates support is present. */
5732 if (can_compare_and_swap_p (mode, true))
5733 return boolean_true_node;
5734 else
5735 return boolean_false_node;
5738 /* Return true if the parameters to call EXP represent an object which will
5739 always generate lock free instructions. The first argument represents the
5740 size of the object, and the second parameter is a pointer to the object
5741 itself. If NULL is passed for the object, then the result is based on
5742 typical alignment for an object of the specified size. Otherwise return
5743 false. */
5745 static rtx
5746 expand_builtin_atomic_always_lock_free (tree exp)
5748 tree size;
5749 tree arg0 = CALL_EXPR_ARG (exp, 0);
5750 tree arg1 = CALL_EXPR_ARG (exp, 1);
5752 if (TREE_CODE (arg0) != INTEGER_CST)
5754 error ("non-constant argument 1 to __atomic_always_lock_free");
5755 return const0_rtx;
5758 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5759 if (size == boolean_true_node)
5760 return const1_rtx;
5761 return const0_rtx;
5764 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5765 is lock free on this architecture. */
5767 static tree
5768 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5770 if (!flag_inline_atomics)
5771 return NULL_TREE;
5773 /* If it isn't always lock free, don't generate a result. */
5774 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5775 return boolean_true_node;
5777 return NULL_TREE;
5780 /* Return true if the parameters to call EXP represent an object which will
5781 always generate lock free instructions. The first argument represents the
5782 size of the object, and the second parameter is a pointer to the object
5783 itself. If NULL is passed for the object, then the result is based on
5784 typical alignment for an object of the specified size. Otherwise return
5785 NULL*/
5787 static rtx
5788 expand_builtin_atomic_is_lock_free (tree exp)
5790 tree size;
5791 tree arg0 = CALL_EXPR_ARG (exp, 0);
5792 tree arg1 = CALL_EXPR_ARG (exp, 1);
5794 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5796 error ("non-integer argument 1 to __atomic_is_lock_free");
5797 return NULL_RTX;
5800 if (!flag_inline_atomics)
5801 return NULL_RTX;
5803 /* If the value is known at compile time, return the RTX for it. */
5804 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5805 if (size == boolean_true_node)
5806 return const1_rtx;
5808 return NULL_RTX;
5811 /* Expand the __atomic_thread_fence intrinsic:
5812 void __atomic_thread_fence (enum memmodel)
5813 EXP is the CALL_EXPR. */
5815 static void
5816 expand_builtin_atomic_thread_fence (tree exp)
5818 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5819 expand_mem_thread_fence (model);
5822 /* Expand the __atomic_signal_fence intrinsic:
5823 void __atomic_signal_fence (enum memmodel)
5824 EXP is the CALL_EXPR. */
5826 static void
5827 expand_builtin_atomic_signal_fence (tree exp)
5829 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5830 expand_mem_signal_fence (model);
5833 /* Expand the __sync_synchronize intrinsic. */
5835 static void
5836 expand_builtin_sync_synchronize (void)
5838 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5841 static rtx
5842 expand_builtin_thread_pointer (tree exp, rtx target)
5844 enum insn_code icode;
5845 if (!validate_arglist (exp, VOID_TYPE))
5846 return const0_rtx;
5847 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5848 if (icode != CODE_FOR_nothing)
5850 struct expand_operand op;
5851 /* If the target is not sutitable then create a new target. */
5852 if (target == NULL_RTX
5853 || !REG_P (target)
5854 || GET_MODE (target) != Pmode)
5855 target = gen_reg_rtx (Pmode);
5856 create_output_operand (&op, target, Pmode);
5857 expand_insn (icode, 1, &op);
5858 return target;
5860 error ("__builtin_thread_pointer is not supported on this target");
5861 return const0_rtx;
5864 static void
5865 expand_builtin_set_thread_pointer (tree exp)
5867 enum insn_code icode;
5868 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5869 return;
5870 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5871 if (icode != CODE_FOR_nothing)
5873 struct expand_operand op;
5874 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5875 Pmode, EXPAND_NORMAL);
5876 create_input_operand (&op, val, Pmode);
5877 expand_insn (icode, 1, &op);
5878 return;
5880 error ("__builtin_set_thread_pointer is not supported on this target");
5884 /* Emit code to restore the current value of stack. */
5886 static void
5887 expand_stack_restore (tree var)
5889 rtx_insn *prev;
5890 rtx sa = expand_normal (var);
5892 sa = convert_memory_address (Pmode, sa);
5894 prev = get_last_insn ();
5895 emit_stack_restore (SAVE_BLOCK, sa);
5896 fixup_args_size_notes (prev, get_last_insn (), 0);
5900 /* Emit code to save the current value of stack. */
5902 static rtx
5903 expand_stack_save (void)
5905 rtx ret = NULL_RTX;
5907 do_pending_stack_adjust ();
5908 emit_stack_save (SAVE_BLOCK, &ret);
5909 return ret;
5913 /* Expand OpenACC acc_on_device.
5915 This has to happen late (that is, not in early folding; expand_builtin_*,
5916 rather than fold_builtin_*), as we have to act differently for host and
5917 acceleration device (ACCEL_COMPILER conditional). */
5919 static rtx
5920 expand_builtin_acc_on_device (tree exp, rtx target)
5922 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5923 return NULL_RTX;
5925 tree arg = CALL_EXPR_ARG (exp, 0);
5927 /* Return (arg == v1 || arg == v2) ? 1 : 0. */
5928 machine_mode v_mode = TYPE_MODE (TREE_TYPE (arg));
5929 rtx v = expand_normal (arg), v1, v2;
5930 #ifdef ACCEL_COMPILER
5931 v1 = GEN_INT (GOMP_DEVICE_NOT_HOST);
5932 v2 = GEN_INT (ACCEL_COMPILER_acc_device);
5933 #else
5934 v1 = GEN_INT (GOMP_DEVICE_NONE);
5935 v2 = GEN_INT (GOMP_DEVICE_HOST);
5936 #endif
5937 machine_mode target_mode = TYPE_MODE (integer_type_node);
5938 if (!target || !register_operand (target, target_mode))
5939 target = gen_reg_rtx (target_mode);
5940 emit_move_insn (target, const1_rtx);
5941 rtx_code_label *done_label = gen_label_rtx ();
5942 do_compare_rtx_and_jump (v, v1, EQ, false, v_mode, NULL_RTX,
5943 NULL_RTX, done_label, PROB_EVEN);
5944 do_compare_rtx_and_jump (v, v2, EQ, false, v_mode, NULL_RTX,
5945 NULL_RTX, done_label, PROB_EVEN);
5946 emit_move_insn (target, const0_rtx);
5947 emit_label (done_label);
5949 return target;
5953 /* Expand an expression EXP that calls a built-in function,
5954 with result going to TARGET if that's convenient
5955 (and in mode MODE if that's convenient).
5956 SUBTARGET may be used as the target for computing one of EXP's operands.
5957 IGNORE is nonzero if the value is to be ignored. */
5960 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
5961 int ignore)
5963 tree fndecl = get_callee_fndecl (exp);
5964 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5965 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5966 int flags;
5968 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5969 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5971 /* When ASan is enabled, we don't want to expand some memory/string
5972 builtins and rely on libsanitizer's hooks. This allows us to avoid
5973 redundant checks and be sure, that possible overflow will be detected
5974 by ASan. */
5976 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
5977 return expand_call (exp, target, ignore);
5979 /* When not optimizing, generate calls to library functions for a certain
5980 set of builtins. */
5981 if (!optimize
5982 && !called_as_built_in (fndecl)
5983 && fcode != BUILT_IN_FORK
5984 && fcode != BUILT_IN_EXECL
5985 && fcode != BUILT_IN_EXECV
5986 && fcode != BUILT_IN_EXECLP
5987 && fcode != BUILT_IN_EXECLE
5988 && fcode != BUILT_IN_EXECVP
5989 && fcode != BUILT_IN_EXECVE
5990 && fcode != BUILT_IN_ALLOCA
5991 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5992 && fcode != BUILT_IN_FREE
5993 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
5994 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
5995 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
5996 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
5997 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5998 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
5999 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
6000 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
6001 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
6002 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
6003 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND
6004 && fcode != BUILT_IN_CHKP_BNDRET)
6005 return expand_call (exp, target, ignore);
6007 /* The built-in function expanders test for target == const0_rtx
6008 to determine whether the function's result will be ignored. */
6009 if (ignore)
6010 target = const0_rtx;
6012 /* If the result of a pure or const built-in function is ignored, and
6013 none of its arguments are volatile, we can avoid expanding the
6014 built-in call and just evaluate the arguments for side-effects. */
6015 if (target == const0_rtx
6016 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
6017 && !(flags & ECF_LOOPING_CONST_OR_PURE))
6019 bool volatilep = false;
6020 tree arg;
6021 call_expr_arg_iterator iter;
6023 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6024 if (TREE_THIS_VOLATILE (arg))
6026 volatilep = true;
6027 break;
6030 if (! volatilep)
6032 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6033 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6034 return const0_rtx;
6038 /* expand_builtin_with_bounds is supposed to be used for
6039 instrumented builtin calls. */
6040 gcc_assert (!CALL_WITH_BOUNDS_P (exp));
6042 switch (fcode)
6044 CASE_FLT_FN (BUILT_IN_FABS):
6045 case BUILT_IN_FABSD32:
6046 case BUILT_IN_FABSD64:
6047 case BUILT_IN_FABSD128:
6048 target = expand_builtin_fabs (exp, target, subtarget);
6049 if (target)
6050 return target;
6051 break;
6053 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6054 target = expand_builtin_copysign (exp, target, subtarget);
6055 if (target)
6056 return target;
6057 break;
6059 /* Just do a normal library call if we were unable to fold
6060 the values. */
6061 CASE_FLT_FN (BUILT_IN_CABS):
6062 break;
6064 CASE_FLT_FN (BUILT_IN_EXP):
6065 CASE_FLT_FN (BUILT_IN_EXP10):
6066 CASE_FLT_FN (BUILT_IN_POW10):
6067 CASE_FLT_FN (BUILT_IN_EXP2):
6068 CASE_FLT_FN (BUILT_IN_EXPM1):
6069 CASE_FLT_FN (BUILT_IN_LOGB):
6070 CASE_FLT_FN (BUILT_IN_LOG):
6071 CASE_FLT_FN (BUILT_IN_LOG10):
6072 CASE_FLT_FN (BUILT_IN_LOG2):
6073 CASE_FLT_FN (BUILT_IN_LOG1P):
6074 CASE_FLT_FN (BUILT_IN_TAN):
6075 CASE_FLT_FN (BUILT_IN_ASIN):
6076 CASE_FLT_FN (BUILT_IN_ACOS):
6077 CASE_FLT_FN (BUILT_IN_ATAN):
6078 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
6079 /* Treat these like sqrt only if unsafe math optimizations are allowed,
6080 because of possible accuracy problems. */
6081 if (! flag_unsafe_math_optimizations)
6082 break;
6083 CASE_FLT_FN (BUILT_IN_SQRT):
6084 CASE_FLT_FN (BUILT_IN_FLOOR):
6085 CASE_FLT_FN (BUILT_IN_CEIL):
6086 CASE_FLT_FN (BUILT_IN_TRUNC):
6087 CASE_FLT_FN (BUILT_IN_ROUND):
6088 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6089 CASE_FLT_FN (BUILT_IN_RINT):
6090 target = expand_builtin_mathfn (exp, target, subtarget);
6091 if (target)
6092 return target;
6093 break;
6095 CASE_FLT_FN (BUILT_IN_FMA):
6096 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
6097 if (target)
6098 return target;
6099 break;
6101 CASE_FLT_FN (BUILT_IN_ILOGB):
6102 if (! flag_unsafe_math_optimizations)
6103 break;
6104 CASE_FLT_FN (BUILT_IN_ISINF):
6105 CASE_FLT_FN (BUILT_IN_FINITE):
6106 case BUILT_IN_ISFINITE:
6107 case BUILT_IN_ISNORMAL:
6108 target = expand_builtin_interclass_mathfn (exp, target);
6109 if (target)
6110 return target;
6111 break;
6113 CASE_FLT_FN (BUILT_IN_ICEIL):
6114 CASE_FLT_FN (BUILT_IN_LCEIL):
6115 CASE_FLT_FN (BUILT_IN_LLCEIL):
6116 CASE_FLT_FN (BUILT_IN_LFLOOR):
6117 CASE_FLT_FN (BUILT_IN_IFLOOR):
6118 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6119 target = expand_builtin_int_roundingfn (exp, target);
6120 if (target)
6121 return target;
6122 break;
6124 CASE_FLT_FN (BUILT_IN_IRINT):
6125 CASE_FLT_FN (BUILT_IN_LRINT):
6126 CASE_FLT_FN (BUILT_IN_LLRINT):
6127 CASE_FLT_FN (BUILT_IN_IROUND):
6128 CASE_FLT_FN (BUILT_IN_LROUND):
6129 CASE_FLT_FN (BUILT_IN_LLROUND):
6130 target = expand_builtin_int_roundingfn_2 (exp, target);
6131 if (target)
6132 return target;
6133 break;
6135 CASE_FLT_FN (BUILT_IN_POWI):
6136 target = expand_builtin_powi (exp, target);
6137 if (target)
6138 return target;
6139 break;
6141 CASE_FLT_FN (BUILT_IN_ATAN2):
6142 CASE_FLT_FN (BUILT_IN_LDEXP):
6143 CASE_FLT_FN (BUILT_IN_SCALB):
6144 CASE_FLT_FN (BUILT_IN_SCALBN):
6145 CASE_FLT_FN (BUILT_IN_SCALBLN):
6146 if (! flag_unsafe_math_optimizations)
6147 break;
6149 CASE_FLT_FN (BUILT_IN_FMOD):
6150 CASE_FLT_FN (BUILT_IN_REMAINDER):
6151 CASE_FLT_FN (BUILT_IN_DREM):
6152 CASE_FLT_FN (BUILT_IN_POW):
6153 target = expand_builtin_mathfn_2 (exp, target, subtarget);
6154 if (target)
6155 return target;
6156 break;
6158 CASE_FLT_FN (BUILT_IN_CEXPI):
6159 target = expand_builtin_cexpi (exp, target);
6160 gcc_assert (target);
6161 return target;
6163 CASE_FLT_FN (BUILT_IN_SIN):
6164 CASE_FLT_FN (BUILT_IN_COS):
6165 if (! flag_unsafe_math_optimizations)
6166 break;
6167 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6168 if (target)
6169 return target;
6170 break;
6172 CASE_FLT_FN (BUILT_IN_SINCOS):
6173 if (! flag_unsafe_math_optimizations)
6174 break;
6175 target = expand_builtin_sincos (exp);
6176 if (target)
6177 return target;
6178 break;
6180 case BUILT_IN_APPLY_ARGS:
6181 return expand_builtin_apply_args ();
6183 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6184 FUNCTION with a copy of the parameters described by
6185 ARGUMENTS, and ARGSIZE. It returns a block of memory
6186 allocated on the stack into which is stored all the registers
6187 that might possibly be used for returning the result of a
6188 function. ARGUMENTS is the value returned by
6189 __builtin_apply_args. ARGSIZE is the number of bytes of
6190 arguments that must be copied. ??? How should this value be
6191 computed? We'll also need a safe worst case value for varargs
6192 functions. */
6193 case BUILT_IN_APPLY:
6194 if (!validate_arglist (exp, POINTER_TYPE,
6195 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6196 && !validate_arglist (exp, REFERENCE_TYPE,
6197 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6198 return const0_rtx;
6199 else
6201 rtx ops[3];
6203 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6204 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6205 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6207 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6210 /* __builtin_return (RESULT) causes the function to return the
6211 value described by RESULT. RESULT is address of the block of
6212 memory returned by __builtin_apply. */
6213 case BUILT_IN_RETURN:
6214 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6215 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6216 return const0_rtx;
6218 case BUILT_IN_SAVEREGS:
6219 return expand_builtin_saveregs ();
6221 case BUILT_IN_VA_ARG_PACK:
6222 /* All valid uses of __builtin_va_arg_pack () are removed during
6223 inlining. */
6224 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6225 return const0_rtx;
6227 case BUILT_IN_VA_ARG_PACK_LEN:
6228 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6229 inlining. */
6230 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6231 return const0_rtx;
6233 /* Return the address of the first anonymous stack arg. */
6234 case BUILT_IN_NEXT_ARG:
6235 if (fold_builtin_next_arg (exp, false))
6236 return const0_rtx;
6237 return expand_builtin_next_arg ();
6239 case BUILT_IN_CLEAR_CACHE:
6240 target = expand_builtin___clear_cache (exp);
6241 if (target)
6242 return target;
6243 break;
6245 case BUILT_IN_CLASSIFY_TYPE:
6246 return expand_builtin_classify_type (exp);
6248 case BUILT_IN_CONSTANT_P:
6249 return const0_rtx;
6251 case BUILT_IN_FRAME_ADDRESS:
6252 case BUILT_IN_RETURN_ADDRESS:
6253 return expand_builtin_frame_address (fndecl, exp);
6255 /* Returns the address of the area where the structure is returned.
6256 0 otherwise. */
6257 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6258 if (call_expr_nargs (exp) != 0
6259 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6260 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6261 return const0_rtx;
6262 else
6263 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6265 case BUILT_IN_ALLOCA:
6266 case BUILT_IN_ALLOCA_WITH_ALIGN:
6267 /* If the allocation stems from the declaration of a variable-sized
6268 object, it cannot accumulate. */
6269 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6270 if (target)
6271 return target;
6272 break;
6274 case BUILT_IN_STACK_SAVE:
6275 return expand_stack_save ();
6277 case BUILT_IN_STACK_RESTORE:
6278 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6279 return const0_rtx;
6281 case BUILT_IN_BSWAP16:
6282 case BUILT_IN_BSWAP32:
6283 case BUILT_IN_BSWAP64:
6284 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6285 if (target)
6286 return target;
6287 break;
6289 CASE_INT_FN (BUILT_IN_FFS):
6290 target = expand_builtin_unop (target_mode, exp, target,
6291 subtarget, ffs_optab);
6292 if (target)
6293 return target;
6294 break;
6296 CASE_INT_FN (BUILT_IN_CLZ):
6297 target = expand_builtin_unop (target_mode, exp, target,
6298 subtarget, clz_optab);
6299 if (target)
6300 return target;
6301 break;
6303 CASE_INT_FN (BUILT_IN_CTZ):
6304 target = expand_builtin_unop (target_mode, exp, target,
6305 subtarget, ctz_optab);
6306 if (target)
6307 return target;
6308 break;
6310 CASE_INT_FN (BUILT_IN_CLRSB):
6311 target = expand_builtin_unop (target_mode, exp, target,
6312 subtarget, clrsb_optab);
6313 if (target)
6314 return target;
6315 break;
6317 CASE_INT_FN (BUILT_IN_POPCOUNT):
6318 target = expand_builtin_unop (target_mode, exp, target,
6319 subtarget, popcount_optab);
6320 if (target)
6321 return target;
6322 break;
6324 CASE_INT_FN (BUILT_IN_PARITY):
6325 target = expand_builtin_unop (target_mode, exp, target,
6326 subtarget, parity_optab);
6327 if (target)
6328 return target;
6329 break;
6331 case BUILT_IN_STRLEN:
6332 target = expand_builtin_strlen (exp, target, target_mode);
6333 if (target)
6334 return target;
6335 break;
6337 case BUILT_IN_STRCPY:
6338 target = expand_builtin_strcpy (exp, target);
6339 if (target)
6340 return target;
6341 break;
6343 case BUILT_IN_STRNCPY:
6344 target = expand_builtin_strncpy (exp, target);
6345 if (target)
6346 return target;
6347 break;
6349 case BUILT_IN_STPCPY:
6350 target = expand_builtin_stpcpy (exp, target, mode);
6351 if (target)
6352 return target;
6353 break;
6355 case BUILT_IN_MEMCPY:
6356 target = expand_builtin_memcpy (exp, target);
6357 if (target)
6358 return target;
6359 break;
6361 case BUILT_IN_MEMPCPY:
6362 target = expand_builtin_mempcpy (exp, target, mode);
6363 if (target)
6364 return target;
6365 break;
6367 case BUILT_IN_MEMSET:
6368 target = expand_builtin_memset (exp, target, mode);
6369 if (target)
6370 return target;
6371 break;
6373 case BUILT_IN_BZERO:
6374 target = expand_builtin_bzero (exp);
6375 if (target)
6376 return target;
6377 break;
6379 case BUILT_IN_STRCMP:
6380 target = expand_builtin_strcmp (exp, target);
6381 if (target)
6382 return target;
6383 break;
6385 case BUILT_IN_STRNCMP:
6386 target = expand_builtin_strncmp (exp, target, mode);
6387 if (target)
6388 return target;
6389 break;
6391 case BUILT_IN_BCMP:
6392 case BUILT_IN_MEMCMP:
6393 target = expand_builtin_memcmp (exp, target, mode);
6394 if (target)
6395 return target;
6396 break;
6398 case BUILT_IN_SETJMP:
6399 /* This should have been lowered to the builtins below. */
6400 gcc_unreachable ();
6402 case BUILT_IN_SETJMP_SETUP:
6403 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6404 and the receiver label. */
6405 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6407 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6408 VOIDmode, EXPAND_NORMAL);
6409 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6410 rtx label_r = label_rtx (label);
6412 /* This is copied from the handling of non-local gotos. */
6413 expand_builtin_setjmp_setup (buf_addr, label_r);
6414 nonlocal_goto_handler_labels
6415 = gen_rtx_INSN_LIST (VOIDmode, label_r,
6416 nonlocal_goto_handler_labels);
6417 /* ??? Do not let expand_label treat us as such since we would
6418 not want to be both on the list of non-local labels and on
6419 the list of forced labels. */
6420 FORCED_LABEL (label) = 0;
6421 return const0_rtx;
6423 break;
6425 case BUILT_IN_SETJMP_RECEIVER:
6426 /* __builtin_setjmp_receiver is passed the receiver label. */
6427 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6429 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6430 rtx label_r = label_rtx (label);
6432 expand_builtin_setjmp_receiver (label_r);
6433 return const0_rtx;
6435 break;
6437 /* __builtin_longjmp is passed a pointer to an array of five words.
6438 It's similar to the C library longjmp function but works with
6439 __builtin_setjmp above. */
6440 case BUILT_IN_LONGJMP:
6441 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6443 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6444 VOIDmode, EXPAND_NORMAL);
6445 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6447 if (value != const1_rtx)
6449 error ("%<__builtin_longjmp%> second argument must be 1");
6450 return const0_rtx;
6453 expand_builtin_longjmp (buf_addr, value);
6454 return const0_rtx;
6456 break;
6458 case BUILT_IN_NONLOCAL_GOTO:
6459 target = expand_builtin_nonlocal_goto (exp);
6460 if (target)
6461 return target;
6462 break;
6464 /* This updates the setjmp buffer that is its argument with the value
6465 of the current stack pointer. */
6466 case BUILT_IN_UPDATE_SETJMP_BUF:
6467 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6469 rtx buf_addr
6470 = expand_normal (CALL_EXPR_ARG (exp, 0));
6472 expand_builtin_update_setjmp_buf (buf_addr);
6473 return const0_rtx;
6475 break;
6477 case BUILT_IN_TRAP:
6478 expand_builtin_trap ();
6479 return const0_rtx;
6481 case BUILT_IN_UNREACHABLE:
6482 expand_builtin_unreachable ();
6483 return const0_rtx;
6485 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6486 case BUILT_IN_SIGNBITD32:
6487 case BUILT_IN_SIGNBITD64:
6488 case BUILT_IN_SIGNBITD128:
6489 target = expand_builtin_signbit (exp, target);
6490 if (target)
6491 return target;
6492 break;
6494 /* Various hooks for the DWARF 2 __throw routine. */
6495 case BUILT_IN_UNWIND_INIT:
6496 expand_builtin_unwind_init ();
6497 return const0_rtx;
6498 case BUILT_IN_DWARF_CFA:
6499 return virtual_cfa_rtx;
6500 #ifdef DWARF2_UNWIND_INFO
6501 case BUILT_IN_DWARF_SP_COLUMN:
6502 return expand_builtin_dwarf_sp_column ();
6503 case BUILT_IN_INIT_DWARF_REG_SIZES:
6504 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6505 return const0_rtx;
6506 #endif
6507 case BUILT_IN_FROB_RETURN_ADDR:
6508 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6509 case BUILT_IN_EXTRACT_RETURN_ADDR:
6510 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6511 case BUILT_IN_EH_RETURN:
6512 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6513 CALL_EXPR_ARG (exp, 1));
6514 return const0_rtx;
6515 #ifdef EH_RETURN_DATA_REGNO
6516 case BUILT_IN_EH_RETURN_DATA_REGNO:
6517 return expand_builtin_eh_return_data_regno (exp);
6518 #endif
6519 case BUILT_IN_EXTEND_POINTER:
6520 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6521 case BUILT_IN_EH_POINTER:
6522 return expand_builtin_eh_pointer (exp);
6523 case BUILT_IN_EH_FILTER:
6524 return expand_builtin_eh_filter (exp);
6525 case BUILT_IN_EH_COPY_VALUES:
6526 return expand_builtin_eh_copy_values (exp);
6528 case BUILT_IN_VA_START:
6529 return expand_builtin_va_start (exp);
6530 case BUILT_IN_VA_END:
6531 return expand_builtin_va_end (exp);
6532 case BUILT_IN_VA_COPY:
6533 return expand_builtin_va_copy (exp);
6534 case BUILT_IN_EXPECT:
6535 return expand_builtin_expect (exp, target);
6536 case BUILT_IN_ASSUME_ALIGNED:
6537 return expand_builtin_assume_aligned (exp, target);
6538 case BUILT_IN_PREFETCH:
6539 expand_builtin_prefetch (exp);
6540 return const0_rtx;
6542 case BUILT_IN_INIT_TRAMPOLINE:
6543 return expand_builtin_init_trampoline (exp, true);
6544 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6545 return expand_builtin_init_trampoline (exp, false);
6546 case BUILT_IN_ADJUST_TRAMPOLINE:
6547 return expand_builtin_adjust_trampoline (exp);
6549 case BUILT_IN_FORK:
6550 case BUILT_IN_EXECL:
6551 case BUILT_IN_EXECV:
6552 case BUILT_IN_EXECLP:
6553 case BUILT_IN_EXECLE:
6554 case BUILT_IN_EXECVP:
6555 case BUILT_IN_EXECVE:
6556 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6557 if (target)
6558 return target;
6559 break;
6561 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6562 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6563 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6564 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6565 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6566 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6567 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6568 if (target)
6569 return target;
6570 break;
6572 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6573 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6574 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6575 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6576 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6577 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6578 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6579 if (target)
6580 return target;
6581 break;
6583 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6584 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6585 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6586 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6587 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6588 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6589 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6590 if (target)
6591 return target;
6592 break;
6594 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6595 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6596 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6597 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6598 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6599 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6600 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6601 if (target)
6602 return target;
6603 break;
6605 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6606 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6607 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6608 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6609 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6610 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6611 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6612 if (target)
6613 return target;
6614 break;
6616 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6617 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6618 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6619 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6620 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6621 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6622 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6623 if (target)
6624 return target;
6625 break;
6627 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6628 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6629 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6630 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6631 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6632 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6633 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6634 if (target)
6635 return target;
6636 break;
6638 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6639 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6640 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6641 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6642 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6643 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6644 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6645 if (target)
6646 return target;
6647 break;
6649 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6650 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6651 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6652 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6653 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6654 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6655 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6656 if (target)
6657 return target;
6658 break;
6660 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6661 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6662 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6663 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6664 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6665 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6666 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6667 if (target)
6668 return target;
6669 break;
6671 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6672 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6673 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6674 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6675 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6676 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6677 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6678 if (target)
6679 return target;
6680 break;
6682 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6683 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6684 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6685 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6686 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6687 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6688 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6689 if (target)
6690 return target;
6691 break;
6693 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6694 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6695 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6696 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6697 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6698 if (mode == VOIDmode)
6699 mode = TYPE_MODE (boolean_type_node);
6700 if (!target || !register_operand (target, mode))
6701 target = gen_reg_rtx (mode);
6703 mode = get_builtin_sync_mode
6704 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6705 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6706 if (target)
6707 return target;
6708 break;
6710 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6711 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6712 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6713 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6714 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6715 mode = get_builtin_sync_mode
6716 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6717 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6718 if (target)
6719 return target;
6720 break;
6722 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6723 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6724 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6725 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6726 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6727 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6728 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6729 if (target)
6730 return target;
6731 break;
6733 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6734 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6735 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6736 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6737 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6738 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6739 expand_builtin_sync_lock_release (mode, exp);
6740 return const0_rtx;
6742 case BUILT_IN_SYNC_SYNCHRONIZE:
6743 expand_builtin_sync_synchronize ();
6744 return const0_rtx;
6746 case BUILT_IN_ATOMIC_EXCHANGE_1:
6747 case BUILT_IN_ATOMIC_EXCHANGE_2:
6748 case BUILT_IN_ATOMIC_EXCHANGE_4:
6749 case BUILT_IN_ATOMIC_EXCHANGE_8:
6750 case BUILT_IN_ATOMIC_EXCHANGE_16:
6751 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6752 target = expand_builtin_atomic_exchange (mode, exp, target);
6753 if (target)
6754 return target;
6755 break;
6757 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6758 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6759 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6760 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6761 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6763 unsigned int nargs, z;
6764 vec<tree, va_gc> *vec;
6766 mode =
6767 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6768 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6769 if (target)
6770 return target;
6772 /* If this is turned into an external library call, the weak parameter
6773 must be dropped to match the expected parameter list. */
6774 nargs = call_expr_nargs (exp);
6775 vec_alloc (vec, nargs - 1);
6776 for (z = 0; z < 3; z++)
6777 vec->quick_push (CALL_EXPR_ARG (exp, z));
6778 /* Skip the boolean weak parameter. */
6779 for (z = 4; z < 6; z++)
6780 vec->quick_push (CALL_EXPR_ARG (exp, z));
6781 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6782 break;
6785 case BUILT_IN_ATOMIC_LOAD_1:
6786 case BUILT_IN_ATOMIC_LOAD_2:
6787 case BUILT_IN_ATOMIC_LOAD_4:
6788 case BUILT_IN_ATOMIC_LOAD_8:
6789 case BUILT_IN_ATOMIC_LOAD_16:
6790 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6791 target = expand_builtin_atomic_load (mode, exp, target);
6792 if (target)
6793 return target;
6794 break;
6796 case BUILT_IN_ATOMIC_STORE_1:
6797 case BUILT_IN_ATOMIC_STORE_2:
6798 case BUILT_IN_ATOMIC_STORE_4:
6799 case BUILT_IN_ATOMIC_STORE_8:
6800 case BUILT_IN_ATOMIC_STORE_16:
6801 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6802 target = expand_builtin_atomic_store (mode, exp);
6803 if (target)
6804 return const0_rtx;
6805 break;
6807 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6808 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6809 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6810 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6811 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6813 enum built_in_function lib;
6814 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6815 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6816 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6817 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6818 ignore, lib);
6819 if (target)
6820 return target;
6821 break;
6823 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6824 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6825 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6826 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6827 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6829 enum built_in_function lib;
6830 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6831 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6832 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6833 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6834 ignore, lib);
6835 if (target)
6836 return target;
6837 break;
6839 case BUILT_IN_ATOMIC_AND_FETCH_1:
6840 case BUILT_IN_ATOMIC_AND_FETCH_2:
6841 case BUILT_IN_ATOMIC_AND_FETCH_4:
6842 case BUILT_IN_ATOMIC_AND_FETCH_8:
6843 case BUILT_IN_ATOMIC_AND_FETCH_16:
6845 enum built_in_function lib;
6846 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6847 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6848 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6849 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6850 ignore, lib);
6851 if (target)
6852 return target;
6853 break;
6855 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6856 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6857 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6858 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6859 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6861 enum built_in_function lib;
6862 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6863 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6864 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6865 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6866 ignore, lib);
6867 if (target)
6868 return target;
6869 break;
6871 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6872 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6873 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6874 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6875 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6877 enum built_in_function lib;
6878 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6879 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6880 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6881 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6882 ignore, lib);
6883 if (target)
6884 return target;
6885 break;
6887 case BUILT_IN_ATOMIC_OR_FETCH_1:
6888 case BUILT_IN_ATOMIC_OR_FETCH_2:
6889 case BUILT_IN_ATOMIC_OR_FETCH_4:
6890 case BUILT_IN_ATOMIC_OR_FETCH_8:
6891 case BUILT_IN_ATOMIC_OR_FETCH_16:
6893 enum built_in_function lib;
6894 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6895 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6896 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6897 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6898 ignore, lib);
6899 if (target)
6900 return target;
6901 break;
6903 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6904 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6905 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6906 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6907 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6908 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6909 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6910 ignore, BUILT_IN_NONE);
6911 if (target)
6912 return target;
6913 break;
6915 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6916 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6917 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6918 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6919 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6920 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6921 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6922 ignore, BUILT_IN_NONE);
6923 if (target)
6924 return target;
6925 break;
6927 case BUILT_IN_ATOMIC_FETCH_AND_1:
6928 case BUILT_IN_ATOMIC_FETCH_AND_2:
6929 case BUILT_IN_ATOMIC_FETCH_AND_4:
6930 case BUILT_IN_ATOMIC_FETCH_AND_8:
6931 case BUILT_IN_ATOMIC_FETCH_AND_16:
6932 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6933 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6934 ignore, BUILT_IN_NONE);
6935 if (target)
6936 return target;
6937 break;
6939 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6940 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6941 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6942 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6943 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6944 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6945 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6946 ignore, BUILT_IN_NONE);
6947 if (target)
6948 return target;
6949 break;
6951 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6952 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6953 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6954 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6955 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6956 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6957 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6958 ignore, BUILT_IN_NONE);
6959 if (target)
6960 return target;
6961 break;
6963 case BUILT_IN_ATOMIC_FETCH_OR_1:
6964 case BUILT_IN_ATOMIC_FETCH_OR_2:
6965 case BUILT_IN_ATOMIC_FETCH_OR_4:
6966 case BUILT_IN_ATOMIC_FETCH_OR_8:
6967 case BUILT_IN_ATOMIC_FETCH_OR_16:
6968 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6969 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6970 ignore, BUILT_IN_NONE);
6971 if (target)
6972 return target;
6973 break;
6975 case BUILT_IN_ATOMIC_TEST_AND_SET:
6976 return expand_builtin_atomic_test_and_set (exp, target);
6978 case BUILT_IN_ATOMIC_CLEAR:
6979 return expand_builtin_atomic_clear (exp);
6981 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6982 return expand_builtin_atomic_always_lock_free (exp);
6984 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6985 target = expand_builtin_atomic_is_lock_free (exp);
6986 if (target)
6987 return target;
6988 break;
6990 case BUILT_IN_ATOMIC_THREAD_FENCE:
6991 expand_builtin_atomic_thread_fence (exp);
6992 return const0_rtx;
6994 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6995 expand_builtin_atomic_signal_fence (exp);
6996 return const0_rtx;
6998 case BUILT_IN_OBJECT_SIZE:
6999 return expand_builtin_object_size (exp);
7001 case BUILT_IN_MEMCPY_CHK:
7002 case BUILT_IN_MEMPCPY_CHK:
7003 case BUILT_IN_MEMMOVE_CHK:
7004 case BUILT_IN_MEMSET_CHK:
7005 target = expand_builtin_memory_chk (exp, target, mode, fcode);
7006 if (target)
7007 return target;
7008 break;
7010 case BUILT_IN_STRCPY_CHK:
7011 case BUILT_IN_STPCPY_CHK:
7012 case BUILT_IN_STRNCPY_CHK:
7013 case BUILT_IN_STPNCPY_CHK:
7014 case BUILT_IN_STRCAT_CHK:
7015 case BUILT_IN_STRNCAT_CHK:
7016 case BUILT_IN_SNPRINTF_CHK:
7017 case BUILT_IN_VSNPRINTF_CHK:
7018 maybe_emit_chk_warning (exp, fcode);
7019 break;
7021 case BUILT_IN_SPRINTF_CHK:
7022 case BUILT_IN_VSPRINTF_CHK:
7023 maybe_emit_sprintf_chk_warning (exp, fcode);
7024 break;
7026 case BUILT_IN_FREE:
7027 if (warn_free_nonheap_object)
7028 maybe_emit_free_warning (exp);
7029 break;
7031 case BUILT_IN_THREAD_POINTER:
7032 return expand_builtin_thread_pointer (exp, target);
7034 case BUILT_IN_SET_THREAD_POINTER:
7035 expand_builtin_set_thread_pointer (exp);
7036 return const0_rtx;
7038 case BUILT_IN_CILK_DETACH:
7039 expand_builtin_cilk_detach (exp);
7040 return const0_rtx;
7042 case BUILT_IN_CILK_POP_FRAME:
7043 expand_builtin_cilk_pop_frame (exp);
7044 return const0_rtx;
7046 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
7047 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
7048 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
7049 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
7050 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
7051 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
7052 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
7053 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
7054 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
7055 case BUILT_IN_CHKP_GET_PTR_LBOUND:
7056 case BUILT_IN_CHKP_GET_PTR_UBOUND:
7057 /* We allow user CHKP builtins if Pointer Bounds
7058 Checker is off. */
7059 if (!chkp_function_instrumented_p (current_function_decl))
7061 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
7062 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS
7063 || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS
7064 || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS
7065 || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS)
7066 return expand_normal (CALL_EXPR_ARG (exp, 0));
7067 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
7068 return expand_normal (size_zero_node);
7069 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
7070 return expand_normal (size_int (-1));
7071 else
7072 return const0_rtx;
7074 /* FALLTHROUGH */
7076 case BUILT_IN_CHKP_BNDMK:
7077 case BUILT_IN_CHKP_BNDSTX:
7078 case BUILT_IN_CHKP_BNDCL:
7079 case BUILT_IN_CHKP_BNDCU:
7080 case BUILT_IN_CHKP_BNDLDX:
7081 case BUILT_IN_CHKP_BNDRET:
7082 case BUILT_IN_CHKP_INTERSECT:
7083 case BUILT_IN_CHKP_NARROW:
7084 case BUILT_IN_CHKP_EXTRACT_LOWER:
7085 case BUILT_IN_CHKP_EXTRACT_UPPER:
7086 /* Software implementation of Pointer Bounds Checker is NYI.
7087 Target support is required. */
7088 error ("Your target platform does not support -fcheck-pointer-bounds");
7089 break;
7091 case BUILT_IN_ACC_ON_DEVICE:
7092 target = expand_builtin_acc_on_device (exp, target);
7093 if (target)
7094 return target;
7095 break;
7097 default: /* just do library call, if unknown builtin */
7098 break;
7101 /* The switch statement above can drop through to cause the function
7102 to be called normally. */
7103 return expand_call (exp, target, ignore);
7106 /* Similar to expand_builtin but is used for instrumented calls. */
7109 expand_builtin_with_bounds (tree exp, rtx target,
7110 rtx subtarget ATTRIBUTE_UNUSED,
7111 machine_mode mode, int ignore)
7113 tree fndecl = get_callee_fndecl (exp);
7114 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7116 gcc_assert (CALL_WITH_BOUNDS_P (exp));
7118 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7119 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7121 gcc_assert (fcode > BEGIN_CHKP_BUILTINS
7122 && fcode < END_CHKP_BUILTINS);
7124 switch (fcode)
7126 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP:
7127 target = expand_builtin_memcpy_with_bounds (exp, target);
7128 if (target)
7129 return target;
7130 break;
7132 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP:
7133 target = expand_builtin_mempcpy_with_bounds (exp, target, mode);
7134 if (target)
7135 return target;
7136 break;
7138 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP:
7139 target = expand_builtin_memset_with_bounds (exp, target, mode);
7140 if (target)
7141 return target;
7142 break;
7144 default:
7145 break;
7148 /* The switch statement above can drop through to cause the function
7149 to be called normally. */
7150 return expand_call (exp, target, ignore);
7153 /* Determine whether a tree node represents a call to a built-in
7154 function. If the tree T is a call to a built-in function with
7155 the right number of arguments of the appropriate types, return
7156 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7157 Otherwise the return value is END_BUILTINS. */
7159 enum built_in_function
7160 builtin_mathfn_code (const_tree t)
7162 const_tree fndecl, arg, parmlist;
7163 const_tree argtype, parmtype;
7164 const_call_expr_arg_iterator iter;
7166 if (TREE_CODE (t) != CALL_EXPR
7167 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
7168 return END_BUILTINS;
7170 fndecl = get_callee_fndecl (t);
7171 if (fndecl == NULL_TREE
7172 || TREE_CODE (fndecl) != FUNCTION_DECL
7173 || ! DECL_BUILT_IN (fndecl)
7174 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7175 return END_BUILTINS;
7177 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7178 init_const_call_expr_arg_iterator (t, &iter);
7179 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7181 /* If a function doesn't take a variable number of arguments,
7182 the last element in the list will have type `void'. */
7183 parmtype = TREE_VALUE (parmlist);
7184 if (VOID_TYPE_P (parmtype))
7186 if (more_const_call_expr_args_p (&iter))
7187 return END_BUILTINS;
7188 return DECL_FUNCTION_CODE (fndecl);
7191 if (! more_const_call_expr_args_p (&iter))
7192 return END_BUILTINS;
7194 arg = next_const_call_expr_arg (&iter);
7195 argtype = TREE_TYPE (arg);
7197 if (SCALAR_FLOAT_TYPE_P (parmtype))
7199 if (! SCALAR_FLOAT_TYPE_P (argtype))
7200 return END_BUILTINS;
7202 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7204 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7205 return END_BUILTINS;
7207 else if (POINTER_TYPE_P (parmtype))
7209 if (! POINTER_TYPE_P (argtype))
7210 return END_BUILTINS;
7212 else if (INTEGRAL_TYPE_P (parmtype))
7214 if (! INTEGRAL_TYPE_P (argtype))
7215 return END_BUILTINS;
7217 else
7218 return END_BUILTINS;
7221 /* Variable-length argument list. */
7222 return DECL_FUNCTION_CODE (fndecl);
7225 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7226 evaluate to a constant. */
7228 static tree
7229 fold_builtin_constant_p (tree arg)
7231 /* We return 1 for a numeric type that's known to be a constant
7232 value at compile-time or for an aggregate type that's a
7233 literal constant. */
7234 STRIP_NOPS (arg);
7236 /* If we know this is a constant, emit the constant of one. */
7237 if (CONSTANT_CLASS_P (arg)
7238 || (TREE_CODE (arg) == CONSTRUCTOR
7239 && TREE_CONSTANT (arg)))
7240 return integer_one_node;
7241 if (TREE_CODE (arg) == ADDR_EXPR)
7243 tree op = TREE_OPERAND (arg, 0);
7244 if (TREE_CODE (op) == STRING_CST
7245 || (TREE_CODE (op) == ARRAY_REF
7246 && integer_zerop (TREE_OPERAND (op, 1))
7247 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7248 return integer_one_node;
7251 /* If this expression has side effects, show we don't know it to be a
7252 constant. Likewise if it's a pointer or aggregate type since in
7253 those case we only want literals, since those are only optimized
7254 when generating RTL, not later.
7255 And finally, if we are compiling an initializer, not code, we
7256 need to return a definite result now; there's not going to be any
7257 more optimization done. */
7258 if (TREE_SIDE_EFFECTS (arg)
7259 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7260 || POINTER_TYPE_P (TREE_TYPE (arg))
7261 || cfun == 0
7262 || folding_initializer
7263 || force_folding_builtin_constant_p)
7264 return integer_zero_node;
7266 return NULL_TREE;
7269 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7270 return it as a truthvalue. */
7272 static tree
7273 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
7274 tree predictor)
7276 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7278 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
7279 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7280 ret_type = TREE_TYPE (TREE_TYPE (fn));
7281 pred_type = TREE_VALUE (arg_types);
7282 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7284 pred = fold_convert_loc (loc, pred_type, pred);
7285 expected = fold_convert_loc (loc, expected_type, expected);
7286 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
7287 predictor);
7289 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7290 build_int_cst (ret_type, 0));
7293 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7294 NULL_TREE if no simplification is possible. */
7296 tree
7297 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
7299 tree inner, fndecl, inner_arg0;
7300 enum tree_code code;
7302 /* Distribute the expected value over short-circuiting operators.
7303 See through the cast from truthvalue_type_node to long. */
7304 inner_arg0 = arg0;
7305 while (CONVERT_EXPR_P (inner_arg0)
7306 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7307 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7308 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7310 /* If this is a builtin_expect within a builtin_expect keep the
7311 inner one. See through a comparison against a constant. It
7312 might have been added to create a thruthvalue. */
7313 inner = inner_arg0;
7315 if (COMPARISON_CLASS_P (inner)
7316 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7317 inner = TREE_OPERAND (inner, 0);
7319 if (TREE_CODE (inner) == CALL_EXPR
7320 && (fndecl = get_callee_fndecl (inner))
7321 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7322 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7323 return arg0;
7325 inner = inner_arg0;
7326 code = TREE_CODE (inner);
7327 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7329 tree op0 = TREE_OPERAND (inner, 0);
7330 tree op1 = TREE_OPERAND (inner, 1);
7332 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
7333 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
7334 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7336 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7339 /* If the argument isn't invariant then there's nothing else we can do. */
7340 if (!TREE_CONSTANT (inner_arg0))
7341 return NULL_TREE;
7343 /* If we expect that a comparison against the argument will fold to
7344 a constant return the constant. In practice, this means a true
7345 constant or the address of a non-weak symbol. */
7346 inner = inner_arg0;
7347 STRIP_NOPS (inner);
7348 if (TREE_CODE (inner) == ADDR_EXPR)
7352 inner = TREE_OPERAND (inner, 0);
7354 while (TREE_CODE (inner) == COMPONENT_REF
7355 || TREE_CODE (inner) == ARRAY_REF);
7356 if ((TREE_CODE (inner) == VAR_DECL
7357 || TREE_CODE (inner) == FUNCTION_DECL)
7358 && DECL_WEAK (inner))
7359 return NULL_TREE;
7362 /* Otherwise, ARG0 already has the proper type for the return value. */
7363 return arg0;
7366 /* Fold a call to __builtin_classify_type with argument ARG. */
7368 static tree
7369 fold_builtin_classify_type (tree arg)
7371 if (arg == 0)
7372 return build_int_cst (integer_type_node, no_type_class);
7374 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7377 /* Fold a call to __builtin_strlen with argument ARG. */
7379 static tree
7380 fold_builtin_strlen (location_t loc, tree type, tree arg)
7382 if (!validate_arg (arg, POINTER_TYPE))
7383 return NULL_TREE;
7384 else
7386 tree len = c_strlen (arg, 0);
7388 if (len)
7389 return fold_convert_loc (loc, type, len);
7391 return NULL_TREE;
7395 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7397 static tree
7398 fold_builtin_inf (location_t loc, tree type, int warn)
7400 REAL_VALUE_TYPE real;
7402 /* __builtin_inff is intended to be usable to define INFINITY on all
7403 targets. If an infinity is not available, INFINITY expands "to a
7404 positive constant of type float that overflows at translation
7405 time", footnote "In this case, using INFINITY will violate the
7406 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7407 Thus we pedwarn to ensure this constraint violation is
7408 diagnosed. */
7409 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7410 pedwarn (loc, 0, "target format does not support infinity");
7412 real_inf (&real);
7413 return build_real (type, real);
7416 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7418 static tree
7419 fold_builtin_nan (tree arg, tree type, int quiet)
7421 REAL_VALUE_TYPE real;
7422 const char *str;
7424 if (!validate_arg (arg, POINTER_TYPE))
7425 return NULL_TREE;
7426 str = c_getstr (arg);
7427 if (!str)
7428 return NULL_TREE;
7430 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7431 return NULL_TREE;
7433 return build_real (type, real);
7436 /* Return true if the floating point expression T has an integer value.
7437 We also allow +Inf, -Inf and NaN to be considered integer values. */
7439 static bool
7440 integer_valued_real_p (tree t)
7442 switch (TREE_CODE (t))
7444 case FLOAT_EXPR:
7445 return true;
7447 case ABS_EXPR:
7448 case SAVE_EXPR:
7449 return integer_valued_real_p (TREE_OPERAND (t, 0));
7451 case COMPOUND_EXPR:
7452 case MODIFY_EXPR:
7453 case BIND_EXPR:
7454 return integer_valued_real_p (TREE_OPERAND (t, 1));
7456 case PLUS_EXPR:
7457 case MINUS_EXPR:
7458 case MULT_EXPR:
7459 case MIN_EXPR:
7460 case MAX_EXPR:
7461 return integer_valued_real_p (TREE_OPERAND (t, 0))
7462 && integer_valued_real_p (TREE_OPERAND (t, 1));
7464 case COND_EXPR:
7465 return integer_valued_real_p (TREE_OPERAND (t, 1))
7466 && integer_valued_real_p (TREE_OPERAND (t, 2));
7468 case REAL_CST:
7469 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7471 CASE_CONVERT:
7473 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7474 if (TREE_CODE (type) == INTEGER_TYPE)
7475 return true;
7476 if (TREE_CODE (type) == REAL_TYPE)
7477 return integer_valued_real_p (TREE_OPERAND (t, 0));
7478 break;
7481 case CALL_EXPR:
7482 switch (builtin_mathfn_code (t))
7484 CASE_FLT_FN (BUILT_IN_CEIL):
7485 CASE_FLT_FN (BUILT_IN_FLOOR):
7486 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7487 CASE_FLT_FN (BUILT_IN_RINT):
7488 CASE_FLT_FN (BUILT_IN_ROUND):
7489 CASE_FLT_FN (BUILT_IN_TRUNC):
7490 return true;
7492 CASE_FLT_FN (BUILT_IN_FMIN):
7493 CASE_FLT_FN (BUILT_IN_FMAX):
7494 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7495 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7497 default:
7498 break;
7500 break;
7502 default:
7503 break;
7505 return false;
7508 /* FNDECL is assumed to be a builtin where truncation can be propagated
7509 across (for instance floor((double)f) == (double)floorf (f).
7510 Do the transformation for a call with argument ARG. */
7512 static tree
7513 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7515 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7517 if (!validate_arg (arg, REAL_TYPE))
7518 return NULL_TREE;
7520 /* Integer rounding functions are idempotent. */
7521 if (fcode == builtin_mathfn_code (arg))
7522 return arg;
7524 /* If argument is already integer valued, and we don't need to worry
7525 about setting errno, there's no need to perform rounding. */
7526 if (! flag_errno_math && integer_valued_real_p (arg))
7527 return arg;
7529 if (optimize)
7531 tree arg0 = strip_float_extensions (arg);
7532 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7533 tree newtype = TREE_TYPE (arg0);
7534 tree decl;
7536 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7537 && (decl = mathfn_built_in (newtype, fcode)))
7538 return fold_convert_loc (loc, ftype,
7539 build_call_expr_loc (loc, decl, 1,
7540 fold_convert_loc (loc,
7541 newtype,
7542 arg0)));
7544 return NULL_TREE;
7547 /* FNDECL is assumed to be builtin which can narrow the FP type of
7548 the argument, for instance lround((double)f) -> lroundf (f).
7549 Do the transformation for a call with argument ARG. */
7551 static tree
7552 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7554 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7556 if (!validate_arg (arg, REAL_TYPE))
7557 return NULL_TREE;
7559 /* If argument is already integer valued, and we don't need to worry
7560 about setting errno, there's no need to perform rounding. */
7561 if (! flag_errno_math && integer_valued_real_p (arg))
7562 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7563 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7565 if (optimize)
7567 tree ftype = TREE_TYPE (arg);
7568 tree arg0 = strip_float_extensions (arg);
7569 tree newtype = TREE_TYPE (arg0);
7570 tree decl;
7572 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7573 && (decl = mathfn_built_in (newtype, fcode)))
7574 return build_call_expr_loc (loc, decl, 1,
7575 fold_convert_loc (loc, newtype, arg0));
7578 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7579 sizeof (int) == sizeof (long). */
7580 if (TYPE_PRECISION (integer_type_node)
7581 == TYPE_PRECISION (long_integer_type_node))
7583 tree newfn = NULL_TREE;
7584 switch (fcode)
7586 CASE_FLT_FN (BUILT_IN_ICEIL):
7587 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7588 break;
7590 CASE_FLT_FN (BUILT_IN_IFLOOR):
7591 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7592 break;
7594 CASE_FLT_FN (BUILT_IN_IROUND):
7595 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7596 break;
7598 CASE_FLT_FN (BUILT_IN_IRINT):
7599 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7600 break;
7602 default:
7603 break;
7606 if (newfn)
7608 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7609 return fold_convert_loc (loc,
7610 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7614 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7615 sizeof (long long) == sizeof (long). */
7616 if (TYPE_PRECISION (long_long_integer_type_node)
7617 == TYPE_PRECISION (long_integer_type_node))
7619 tree newfn = NULL_TREE;
7620 switch (fcode)
7622 CASE_FLT_FN (BUILT_IN_LLCEIL):
7623 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7624 break;
7626 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7627 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7628 break;
7630 CASE_FLT_FN (BUILT_IN_LLROUND):
7631 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7632 break;
7634 CASE_FLT_FN (BUILT_IN_LLRINT):
7635 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7636 break;
7638 default:
7639 break;
7642 if (newfn)
7644 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7645 return fold_convert_loc (loc,
7646 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7650 return NULL_TREE;
7653 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7654 return type. Return NULL_TREE if no simplification can be made. */
7656 static tree
7657 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7659 tree res;
7661 if (!validate_arg (arg, COMPLEX_TYPE)
7662 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7663 return NULL_TREE;
7665 /* Calculate the result when the argument is a constant. */
7666 if (TREE_CODE (arg) == COMPLEX_CST
7667 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7668 type, mpfr_hypot)))
7669 return res;
7671 if (TREE_CODE (arg) == COMPLEX_EXPR)
7673 tree real = TREE_OPERAND (arg, 0);
7674 tree imag = TREE_OPERAND (arg, 1);
7676 /* If either part is zero, cabs is fabs of the other. */
7677 if (real_zerop (real))
7678 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7679 if (real_zerop (imag))
7680 return fold_build1_loc (loc, ABS_EXPR, type, real);
7682 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7683 if (flag_unsafe_math_optimizations
7684 && operand_equal_p (real, imag, OEP_PURE_SAME))
7686 const REAL_VALUE_TYPE sqrt2_trunc
7687 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7688 STRIP_NOPS (real);
7689 return fold_build2_loc (loc, MULT_EXPR, type,
7690 fold_build1_loc (loc, ABS_EXPR, type, real),
7691 build_real (type, sqrt2_trunc));
7695 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7696 if (TREE_CODE (arg) == NEGATE_EXPR
7697 || TREE_CODE (arg) == CONJ_EXPR)
7698 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7700 /* Don't do this when optimizing for size. */
7701 if (flag_unsafe_math_optimizations
7702 && optimize && optimize_function_for_speed_p (cfun))
7704 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7706 if (sqrtfn != NULL_TREE)
7708 tree rpart, ipart, result;
7710 arg = builtin_save_expr (arg);
7712 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7713 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7715 rpart = builtin_save_expr (rpart);
7716 ipart = builtin_save_expr (ipart);
7718 result = fold_build2_loc (loc, PLUS_EXPR, type,
7719 fold_build2_loc (loc, MULT_EXPR, type,
7720 rpart, rpart),
7721 fold_build2_loc (loc, MULT_EXPR, type,
7722 ipart, ipart));
7724 return build_call_expr_loc (loc, sqrtfn, 1, result);
7728 return NULL_TREE;
7731 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7732 complex tree type of the result. If NEG is true, the imaginary
7733 zero is negative. */
7735 static tree
7736 build_complex_cproj (tree type, bool neg)
7738 REAL_VALUE_TYPE rinf, rzero = dconst0;
7740 real_inf (&rinf);
7741 rzero.sign = neg;
7742 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7743 build_real (TREE_TYPE (type), rzero));
7746 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7747 return type. Return NULL_TREE if no simplification can be made. */
7749 static tree
7750 fold_builtin_cproj (location_t loc, tree arg, tree type)
7752 if (!validate_arg (arg, COMPLEX_TYPE)
7753 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7754 return NULL_TREE;
7756 /* If there are no infinities, return arg. */
7757 if (! HONOR_INFINITIES (type))
7758 return non_lvalue_loc (loc, arg);
7760 /* Calculate the result when the argument is a constant. */
7761 if (TREE_CODE (arg) == COMPLEX_CST)
7763 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7764 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7766 if (real_isinf (real) || real_isinf (imag))
7767 return build_complex_cproj (type, imag->sign);
7768 else
7769 return arg;
7771 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7773 tree real = TREE_OPERAND (arg, 0);
7774 tree imag = TREE_OPERAND (arg, 1);
7776 STRIP_NOPS (real);
7777 STRIP_NOPS (imag);
7779 /* If the real part is inf and the imag part is known to be
7780 nonnegative, return (inf + 0i). Remember side-effects are
7781 possible in the imag part. */
7782 if (TREE_CODE (real) == REAL_CST
7783 && real_isinf (TREE_REAL_CST_PTR (real))
7784 && tree_expr_nonnegative_p (imag))
7785 return omit_one_operand_loc (loc, type,
7786 build_complex_cproj (type, false),
7787 arg);
7789 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7790 Remember side-effects are possible in the real part. */
7791 if (TREE_CODE (imag) == REAL_CST
7792 && real_isinf (TREE_REAL_CST_PTR (imag)))
7793 return
7794 omit_one_operand_loc (loc, type,
7795 build_complex_cproj (type, TREE_REAL_CST_PTR
7796 (imag)->sign), arg);
7799 return NULL_TREE;
7802 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7803 Return NULL_TREE if no simplification can be made. */
7805 static tree
7806 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7809 enum built_in_function fcode;
7810 tree res;
7812 if (!validate_arg (arg, REAL_TYPE))
7813 return NULL_TREE;
7815 /* Calculate the result when the argument is a constant. */
7816 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7817 return res;
7819 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7820 fcode = builtin_mathfn_code (arg);
7821 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7823 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7824 arg = fold_build2_loc (loc, MULT_EXPR, type,
7825 CALL_EXPR_ARG (arg, 0),
7826 build_real (type, dconsthalf));
7827 return build_call_expr_loc (loc, expfn, 1, arg);
7830 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7831 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7833 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7835 if (powfn)
7837 tree arg0 = CALL_EXPR_ARG (arg, 0);
7838 tree tree_root;
7839 /* The inner root was either sqrt or cbrt. */
7840 /* This was a conditional expression but it triggered a bug
7841 in Sun C 5.5. */
7842 REAL_VALUE_TYPE dconstroot;
7843 if (BUILTIN_SQRT_P (fcode))
7844 dconstroot = dconsthalf;
7845 else
7846 dconstroot = dconst_third ();
7848 /* Adjust for the outer root. */
7849 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7850 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7851 tree_root = build_real (type, dconstroot);
7852 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7856 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7857 if (flag_unsafe_math_optimizations
7858 && (fcode == BUILT_IN_POW
7859 || fcode == BUILT_IN_POWF
7860 || fcode == BUILT_IN_POWL))
7862 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7863 tree arg0 = CALL_EXPR_ARG (arg, 0);
7864 tree arg1 = CALL_EXPR_ARG (arg, 1);
7865 tree narg1;
7866 if (!tree_expr_nonnegative_p (arg0))
7867 arg0 = build1 (ABS_EXPR, type, arg0);
7868 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7869 build_real (type, dconsthalf));
7870 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7873 return NULL_TREE;
7876 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7877 Return NULL_TREE if no simplification can be made. */
7879 static tree
7880 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7882 const enum built_in_function fcode = builtin_mathfn_code (arg);
7883 tree res;
7885 if (!validate_arg (arg, REAL_TYPE))
7886 return NULL_TREE;
7888 /* Calculate the result when the argument is a constant. */
7889 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7890 return res;
7892 if (flag_unsafe_math_optimizations)
7894 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7895 if (BUILTIN_EXPONENT_P (fcode))
7897 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7898 const REAL_VALUE_TYPE third_trunc =
7899 real_value_truncate (TYPE_MODE (type), dconst_third ());
7900 arg = fold_build2_loc (loc, MULT_EXPR, type,
7901 CALL_EXPR_ARG (arg, 0),
7902 build_real (type, third_trunc));
7903 return build_call_expr_loc (loc, expfn, 1, arg);
7906 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7907 if (BUILTIN_SQRT_P (fcode))
7909 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7911 if (powfn)
7913 tree arg0 = CALL_EXPR_ARG (arg, 0);
7914 tree tree_root;
7915 REAL_VALUE_TYPE dconstroot = dconst_third ();
7917 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7918 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7919 tree_root = build_real (type, dconstroot);
7920 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7924 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7925 if (BUILTIN_CBRT_P (fcode))
7927 tree arg0 = CALL_EXPR_ARG (arg, 0);
7928 if (tree_expr_nonnegative_p (arg0))
7930 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7932 if (powfn)
7934 tree tree_root;
7935 REAL_VALUE_TYPE dconstroot;
7937 real_arithmetic (&dconstroot, MULT_EXPR,
7938 dconst_third_ptr (), dconst_third_ptr ());
7939 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7940 tree_root = build_real (type, dconstroot);
7941 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7946 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7947 if (fcode == BUILT_IN_POW
7948 || fcode == BUILT_IN_POWF
7949 || fcode == BUILT_IN_POWL)
7951 tree arg00 = CALL_EXPR_ARG (arg, 0);
7952 tree arg01 = CALL_EXPR_ARG (arg, 1);
7953 if (tree_expr_nonnegative_p (arg00))
7955 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7956 const REAL_VALUE_TYPE dconstroot
7957 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7958 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7959 build_real (type, dconstroot));
7960 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7964 return NULL_TREE;
7967 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7968 TYPE is the type of the return value. Return NULL_TREE if no
7969 simplification can be made. */
7971 static tree
7972 fold_builtin_cos (location_t loc,
7973 tree arg, tree type, tree fndecl)
7975 tree res, narg;
7977 if (!validate_arg (arg, REAL_TYPE))
7978 return NULL_TREE;
7980 /* Calculate the result when the argument is a constant. */
7981 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7982 return res;
7984 /* Optimize cos(-x) into cos (x). */
7985 if ((narg = fold_strip_sign_ops (arg)))
7986 return build_call_expr_loc (loc, fndecl, 1, narg);
7988 return NULL_TREE;
7991 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7992 Return NULL_TREE if no simplification can be made. */
7994 static tree
7995 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7997 if (validate_arg (arg, REAL_TYPE))
7999 tree res, narg;
8001 /* Calculate the result when the argument is a constant. */
8002 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
8003 return res;
8005 /* Optimize cosh(-x) into cosh (x). */
8006 if ((narg = fold_strip_sign_ops (arg)))
8007 return build_call_expr_loc (loc, fndecl, 1, narg);
8010 return NULL_TREE;
8013 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
8014 argument ARG. TYPE is the type of the return value. Return
8015 NULL_TREE if no simplification can be made. */
8017 static tree
8018 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
8019 bool hyper)
8021 if (validate_arg (arg, COMPLEX_TYPE)
8022 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8024 tree tmp;
8026 /* Calculate the result when the argument is a constant. */
8027 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
8028 return tmp;
8030 /* Optimize fn(-x) into fn(x). */
8031 if ((tmp = fold_strip_sign_ops (arg)))
8032 return build_call_expr_loc (loc, fndecl, 1, tmp);
8035 return NULL_TREE;
8038 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
8039 Return NULL_TREE if no simplification can be made. */
8041 static tree
8042 fold_builtin_tan (tree arg, tree type)
8044 enum built_in_function fcode;
8045 tree res;
8047 if (!validate_arg (arg, REAL_TYPE))
8048 return NULL_TREE;
8050 /* Calculate the result when the argument is a constant. */
8051 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
8052 return res;
8054 /* Optimize tan(atan(x)) = x. */
8055 fcode = builtin_mathfn_code (arg);
8056 if (flag_unsafe_math_optimizations
8057 && (fcode == BUILT_IN_ATAN
8058 || fcode == BUILT_IN_ATANF
8059 || fcode == BUILT_IN_ATANL))
8060 return CALL_EXPR_ARG (arg, 0);
8062 return NULL_TREE;
8065 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8066 NULL_TREE if no simplification can be made. */
8068 static tree
8069 fold_builtin_sincos (location_t loc,
8070 tree arg0, tree arg1, tree arg2)
8072 tree type;
8073 tree res, fn, call;
8075 if (!validate_arg (arg0, REAL_TYPE)
8076 || !validate_arg (arg1, POINTER_TYPE)
8077 || !validate_arg (arg2, POINTER_TYPE))
8078 return NULL_TREE;
8080 type = TREE_TYPE (arg0);
8082 /* Calculate the result when the argument is a constant. */
8083 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
8084 return res;
8086 /* Canonicalize sincos to cexpi. */
8087 if (!targetm.libc_has_function (function_c99_math_complex))
8088 return NULL_TREE;
8089 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
8090 if (!fn)
8091 return NULL_TREE;
8093 call = build_call_expr_loc (loc, fn, 1, arg0);
8094 call = builtin_save_expr (call);
8096 return build2 (COMPOUND_EXPR, void_type_node,
8097 build2 (MODIFY_EXPR, void_type_node,
8098 build_fold_indirect_ref_loc (loc, arg1),
8099 build1 (IMAGPART_EXPR, type, call)),
8100 build2 (MODIFY_EXPR, void_type_node,
8101 build_fold_indirect_ref_loc (loc, arg2),
8102 build1 (REALPART_EXPR, type, call)));
8105 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
8106 NULL_TREE if no simplification can be made. */
8108 static tree
8109 fold_builtin_cexp (location_t loc, tree arg0, tree type)
8111 tree rtype;
8112 tree realp, imagp, ifn;
8113 tree res;
8115 if (!validate_arg (arg0, COMPLEX_TYPE)
8116 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
8117 return NULL_TREE;
8119 /* Calculate the result when the argument is a constant. */
8120 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
8121 return res;
8123 rtype = TREE_TYPE (TREE_TYPE (arg0));
8125 /* In case we can figure out the real part of arg0 and it is constant zero
8126 fold to cexpi. */
8127 if (!targetm.libc_has_function (function_c99_math_complex))
8128 return NULL_TREE;
8129 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
8130 if (!ifn)
8131 return NULL_TREE;
8133 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
8134 && real_zerop (realp))
8136 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
8137 return build_call_expr_loc (loc, ifn, 1, narg);
8140 /* In case we can easily decompose real and imaginary parts split cexp
8141 to exp (r) * cexpi (i). */
8142 if (flag_unsafe_math_optimizations
8143 && realp)
8145 tree rfn, rcall, icall;
8147 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
8148 if (!rfn)
8149 return NULL_TREE;
8151 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
8152 if (!imagp)
8153 return NULL_TREE;
8155 icall = build_call_expr_loc (loc, ifn, 1, imagp);
8156 icall = builtin_save_expr (icall);
8157 rcall = build_call_expr_loc (loc, rfn, 1, realp);
8158 rcall = builtin_save_expr (rcall);
8159 return fold_build2_loc (loc, COMPLEX_EXPR, type,
8160 fold_build2_loc (loc, MULT_EXPR, rtype,
8161 rcall,
8162 fold_build1_loc (loc, REALPART_EXPR,
8163 rtype, icall)),
8164 fold_build2_loc (loc, MULT_EXPR, rtype,
8165 rcall,
8166 fold_build1_loc (loc, IMAGPART_EXPR,
8167 rtype, icall)));
8170 return NULL_TREE;
8173 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
8174 Return NULL_TREE if no simplification can be made. */
8176 static tree
8177 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
8179 if (!validate_arg (arg, REAL_TYPE))
8180 return NULL_TREE;
8182 /* Optimize trunc of constant value. */
8183 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8185 REAL_VALUE_TYPE r, x;
8186 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8188 x = TREE_REAL_CST (arg);
8189 real_trunc (&r, TYPE_MODE (type), &x);
8190 return build_real (type, r);
8193 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8196 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
8197 Return NULL_TREE if no simplification can be made. */
8199 static tree
8200 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
8202 if (!validate_arg (arg, REAL_TYPE))
8203 return NULL_TREE;
8205 /* Optimize floor of constant value. */
8206 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8208 REAL_VALUE_TYPE x;
8210 x = TREE_REAL_CST (arg);
8211 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8213 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8214 REAL_VALUE_TYPE r;
8216 real_floor (&r, TYPE_MODE (type), &x);
8217 return build_real (type, r);
8221 /* Fold floor (x) where x is nonnegative to trunc (x). */
8222 if (tree_expr_nonnegative_p (arg))
8224 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
8225 if (truncfn)
8226 return build_call_expr_loc (loc, truncfn, 1, arg);
8229 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8232 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
8233 Return NULL_TREE if no simplification can be made. */
8235 static tree
8236 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
8238 if (!validate_arg (arg, REAL_TYPE))
8239 return NULL_TREE;
8241 /* Optimize ceil of constant value. */
8242 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8244 REAL_VALUE_TYPE x;
8246 x = TREE_REAL_CST (arg);
8247 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8249 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8250 REAL_VALUE_TYPE r;
8252 real_ceil (&r, TYPE_MODE (type), &x);
8253 return build_real (type, r);
8257 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8260 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8261 Return NULL_TREE if no simplification can be made. */
8263 static tree
8264 fold_builtin_round (location_t loc, tree fndecl, tree arg)
8266 if (!validate_arg (arg, REAL_TYPE))
8267 return NULL_TREE;
8269 /* Optimize round of constant value. */
8270 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8272 REAL_VALUE_TYPE x;
8274 x = TREE_REAL_CST (arg);
8275 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8277 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8278 REAL_VALUE_TYPE r;
8280 real_round (&r, TYPE_MODE (type), &x);
8281 return build_real (type, r);
8285 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8288 /* Fold function call to builtin lround, lroundf or lroundl (or the
8289 corresponding long long versions) and other rounding functions. ARG
8290 is the argument to the call. Return NULL_TREE if no simplification
8291 can be made. */
8293 static tree
8294 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8296 if (!validate_arg (arg, REAL_TYPE))
8297 return NULL_TREE;
8299 /* Optimize lround of constant value. */
8300 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8302 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8304 if (real_isfinite (&x))
8306 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8307 tree ftype = TREE_TYPE (arg);
8308 REAL_VALUE_TYPE r;
8309 bool fail = false;
8311 switch (DECL_FUNCTION_CODE (fndecl))
8313 CASE_FLT_FN (BUILT_IN_IFLOOR):
8314 CASE_FLT_FN (BUILT_IN_LFLOOR):
8315 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8316 real_floor (&r, TYPE_MODE (ftype), &x);
8317 break;
8319 CASE_FLT_FN (BUILT_IN_ICEIL):
8320 CASE_FLT_FN (BUILT_IN_LCEIL):
8321 CASE_FLT_FN (BUILT_IN_LLCEIL):
8322 real_ceil (&r, TYPE_MODE (ftype), &x);
8323 break;
8325 CASE_FLT_FN (BUILT_IN_IROUND):
8326 CASE_FLT_FN (BUILT_IN_LROUND):
8327 CASE_FLT_FN (BUILT_IN_LLROUND):
8328 real_round (&r, TYPE_MODE (ftype), &x);
8329 break;
8331 default:
8332 gcc_unreachable ();
8335 wide_int val = real_to_integer (&r, &fail, TYPE_PRECISION (itype));
8336 if (!fail)
8337 return wide_int_to_tree (itype, val);
8341 switch (DECL_FUNCTION_CODE (fndecl))
8343 CASE_FLT_FN (BUILT_IN_LFLOOR):
8344 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8345 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8346 if (tree_expr_nonnegative_p (arg))
8347 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8348 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8349 break;
8350 default:;
8353 return fold_fixed_mathfn (loc, fndecl, arg);
8356 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8357 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8358 the argument to the call. Return NULL_TREE if no simplification can
8359 be made. */
8361 static tree
8362 fold_builtin_bitop (tree fndecl, tree arg)
8364 if (!validate_arg (arg, INTEGER_TYPE))
8365 return NULL_TREE;
8367 /* Optimize for constant argument. */
8368 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8370 tree type = TREE_TYPE (arg);
8371 int result;
8373 switch (DECL_FUNCTION_CODE (fndecl))
8375 CASE_INT_FN (BUILT_IN_FFS):
8376 result = wi::ffs (arg);
8377 break;
8379 CASE_INT_FN (BUILT_IN_CLZ):
8380 if (wi::ne_p (arg, 0))
8381 result = wi::clz (arg);
8382 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8383 result = TYPE_PRECISION (type);
8384 break;
8386 CASE_INT_FN (BUILT_IN_CTZ):
8387 if (wi::ne_p (arg, 0))
8388 result = wi::ctz (arg);
8389 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8390 result = TYPE_PRECISION (type);
8391 break;
8393 CASE_INT_FN (BUILT_IN_CLRSB):
8394 result = wi::clrsb (arg);
8395 break;
8397 CASE_INT_FN (BUILT_IN_POPCOUNT):
8398 result = wi::popcount (arg);
8399 break;
8401 CASE_INT_FN (BUILT_IN_PARITY):
8402 result = wi::parity (arg);
8403 break;
8405 default:
8406 gcc_unreachable ();
8409 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8412 return NULL_TREE;
8415 /* Fold function call to builtin_bswap and the short, long and long long
8416 variants. Return NULL_TREE if no simplification can be made. */
8417 static tree
8418 fold_builtin_bswap (tree fndecl, tree arg)
8420 if (! validate_arg (arg, INTEGER_TYPE))
8421 return NULL_TREE;
8423 /* Optimize constant value. */
8424 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8426 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8428 switch (DECL_FUNCTION_CODE (fndecl))
8430 case BUILT_IN_BSWAP16:
8431 case BUILT_IN_BSWAP32:
8432 case BUILT_IN_BSWAP64:
8434 signop sgn = TYPE_SIGN (type);
8435 tree result =
8436 wide_int_to_tree (type,
8437 wide_int::from (arg, TYPE_PRECISION (type),
8438 sgn).bswap ());
8439 return result;
8441 default:
8442 gcc_unreachable ();
8446 return NULL_TREE;
8449 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8450 NULL_TREE if no simplification can be made. */
8452 static tree
8453 fold_builtin_hypot (location_t loc, tree fndecl,
8454 tree arg0, tree arg1, tree type)
8456 tree res, narg0, narg1;
8458 if (!validate_arg (arg0, REAL_TYPE)
8459 || !validate_arg (arg1, REAL_TYPE))
8460 return NULL_TREE;
8462 /* Calculate the result when the argument is a constant. */
8463 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8464 return res;
8466 /* If either argument to hypot has a negate or abs, strip that off.
8467 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8468 narg0 = fold_strip_sign_ops (arg0);
8469 narg1 = fold_strip_sign_ops (arg1);
8470 if (narg0 || narg1)
8472 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8473 narg1 ? narg1 : arg1);
8476 /* If either argument is zero, hypot is fabs of the other. */
8477 if (real_zerop (arg0))
8478 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8479 else if (real_zerop (arg1))
8480 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8482 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8483 if (flag_unsafe_math_optimizations
8484 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8486 const REAL_VALUE_TYPE sqrt2_trunc
8487 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8488 return fold_build2_loc (loc, MULT_EXPR, type,
8489 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8490 build_real (type, sqrt2_trunc));
8493 return NULL_TREE;
8497 /* Fold a builtin function call to pow, powf, or powl. Return
8498 NULL_TREE if no simplification can be made. */
8499 static tree
8500 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8502 tree res;
8504 if (!validate_arg (arg0, REAL_TYPE)
8505 || !validate_arg (arg1, REAL_TYPE))
8506 return NULL_TREE;
8508 /* Calculate the result when the argument is a constant. */
8509 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8510 return res;
8512 /* Optimize pow(1.0,y) = 1.0. */
8513 if (real_onep (arg0))
8514 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8516 if (TREE_CODE (arg1) == REAL_CST
8517 && !TREE_OVERFLOW (arg1))
8519 REAL_VALUE_TYPE cint;
8520 REAL_VALUE_TYPE c;
8521 HOST_WIDE_INT n;
8523 c = TREE_REAL_CST (arg1);
8525 /* Optimize pow(x,0.0) = 1.0. */
8526 if (REAL_VALUES_EQUAL (c, dconst0))
8527 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8528 arg0);
8530 /* Optimize pow(x,1.0) = x. */
8531 if (REAL_VALUES_EQUAL (c, dconst1))
8532 return arg0;
8534 /* Optimize pow(x,-1.0) = 1.0/x. */
8535 if (REAL_VALUES_EQUAL (c, dconstm1))
8536 return fold_build2_loc (loc, RDIV_EXPR, type,
8537 build_real (type, dconst1), arg0);
8539 /* Optimize pow(x,0.5) = sqrt(x). */
8540 if (flag_unsafe_math_optimizations
8541 && REAL_VALUES_EQUAL (c, dconsthalf))
8543 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8545 if (sqrtfn != NULL_TREE)
8546 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8549 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8550 if (flag_unsafe_math_optimizations)
8552 const REAL_VALUE_TYPE dconstroot
8553 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8555 if (REAL_VALUES_EQUAL (c, dconstroot))
8557 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8558 if (cbrtfn != NULL_TREE)
8559 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8563 /* Check for an integer exponent. */
8564 n = real_to_integer (&c);
8565 real_from_integer (&cint, VOIDmode, n, SIGNED);
8566 if (real_identical (&c, &cint))
8568 /* Attempt to evaluate pow at compile-time, unless this should
8569 raise an exception. */
8570 if (TREE_CODE (arg0) == REAL_CST
8571 && !TREE_OVERFLOW (arg0)
8572 && (n > 0
8573 || (!flag_trapping_math && !flag_errno_math)
8574 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8576 REAL_VALUE_TYPE x;
8577 bool inexact;
8579 x = TREE_REAL_CST (arg0);
8580 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8581 if (flag_unsafe_math_optimizations || !inexact)
8582 return build_real (type, x);
8585 /* Strip sign ops from even integer powers. */
8586 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8588 tree narg0 = fold_strip_sign_ops (arg0);
8589 if (narg0)
8590 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8595 if (flag_unsafe_math_optimizations)
8597 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8599 /* Optimize pow(expN(x),y) = expN(x*y). */
8600 if (BUILTIN_EXPONENT_P (fcode))
8602 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8603 tree arg = CALL_EXPR_ARG (arg0, 0);
8604 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8605 return build_call_expr_loc (loc, expfn, 1, arg);
8608 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8609 if (BUILTIN_SQRT_P (fcode))
8611 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8612 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8613 build_real (type, dconsthalf));
8614 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8617 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8618 if (BUILTIN_CBRT_P (fcode))
8620 tree arg = CALL_EXPR_ARG (arg0, 0);
8621 if (tree_expr_nonnegative_p (arg))
8623 const REAL_VALUE_TYPE dconstroot
8624 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8625 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8626 build_real (type, dconstroot));
8627 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8631 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8632 if (fcode == BUILT_IN_POW
8633 || fcode == BUILT_IN_POWF
8634 || fcode == BUILT_IN_POWL)
8636 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8637 if (tree_expr_nonnegative_p (arg00))
8639 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8640 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8641 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8646 return NULL_TREE;
8649 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8650 Return NULL_TREE if no simplification can be made. */
8651 static tree
8652 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8653 tree arg0, tree arg1, tree type)
8655 if (!validate_arg (arg0, REAL_TYPE)
8656 || !validate_arg (arg1, INTEGER_TYPE))
8657 return NULL_TREE;
8659 /* Optimize pow(1.0,y) = 1.0. */
8660 if (real_onep (arg0))
8661 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8663 if (tree_fits_shwi_p (arg1))
8665 HOST_WIDE_INT c = tree_to_shwi (arg1);
8667 /* Evaluate powi at compile-time. */
8668 if (TREE_CODE (arg0) == REAL_CST
8669 && !TREE_OVERFLOW (arg0))
8671 REAL_VALUE_TYPE x;
8672 x = TREE_REAL_CST (arg0);
8673 real_powi (&x, TYPE_MODE (type), &x, c);
8674 return build_real (type, x);
8677 /* Optimize pow(x,0) = 1.0. */
8678 if (c == 0)
8679 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8680 arg0);
8682 /* Optimize pow(x,1) = x. */
8683 if (c == 1)
8684 return arg0;
8686 /* Optimize pow(x,-1) = 1.0/x. */
8687 if (c == -1)
8688 return fold_build2_loc (loc, RDIV_EXPR, type,
8689 build_real (type, dconst1), arg0);
8692 return NULL_TREE;
8695 /* A subroutine of fold_builtin to fold the various exponent
8696 functions. Return NULL_TREE if no simplification can be made.
8697 FUNC is the corresponding MPFR exponent function. */
8699 static tree
8700 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8701 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8703 if (validate_arg (arg, REAL_TYPE))
8705 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8706 tree res;
8708 /* Calculate the result when the argument is a constant. */
8709 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8710 return res;
8712 /* Optimize expN(logN(x)) = x. */
8713 if (flag_unsafe_math_optimizations)
8715 const enum built_in_function fcode = builtin_mathfn_code (arg);
8717 if ((func == mpfr_exp
8718 && (fcode == BUILT_IN_LOG
8719 || fcode == BUILT_IN_LOGF
8720 || fcode == BUILT_IN_LOGL))
8721 || (func == mpfr_exp2
8722 && (fcode == BUILT_IN_LOG2
8723 || fcode == BUILT_IN_LOG2F
8724 || fcode == BUILT_IN_LOG2L))
8725 || (func == mpfr_exp10
8726 && (fcode == BUILT_IN_LOG10
8727 || fcode == BUILT_IN_LOG10F
8728 || fcode == BUILT_IN_LOG10L)))
8729 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8733 return NULL_TREE;
8736 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8737 arguments to the call, and TYPE is its return type.
8738 Return NULL_TREE if no simplification can be made. */
8740 static tree
8741 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8743 if (!validate_arg (arg1, POINTER_TYPE)
8744 || !validate_arg (arg2, INTEGER_TYPE)
8745 || !validate_arg (len, INTEGER_TYPE))
8746 return NULL_TREE;
8747 else
8749 const char *p1;
8751 if (TREE_CODE (arg2) != INTEGER_CST
8752 || !tree_fits_uhwi_p (len))
8753 return NULL_TREE;
8755 p1 = c_getstr (arg1);
8756 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8758 char c;
8759 const char *r;
8760 tree tem;
8762 if (target_char_cast (arg2, &c))
8763 return NULL_TREE;
8765 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
8767 if (r == NULL)
8768 return build_int_cst (TREE_TYPE (arg1), 0);
8770 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
8771 return fold_convert_loc (loc, type, tem);
8773 return NULL_TREE;
8777 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8778 Return NULL_TREE if no simplification can be made. */
8780 static tree
8781 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8783 const char *p1, *p2;
8785 if (!validate_arg (arg1, POINTER_TYPE)
8786 || !validate_arg (arg2, POINTER_TYPE)
8787 || !validate_arg (len, INTEGER_TYPE))
8788 return NULL_TREE;
8790 /* If the LEN parameter is zero, return zero. */
8791 if (integer_zerop (len))
8792 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8793 arg1, arg2);
8795 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8796 if (operand_equal_p (arg1, arg2, 0))
8797 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8799 p1 = c_getstr (arg1);
8800 p2 = c_getstr (arg2);
8802 /* If all arguments are constant, and the value of len is not greater
8803 than the lengths of arg1 and arg2, evaluate at compile-time. */
8804 if (tree_fits_uhwi_p (len) && p1 && p2
8805 && compare_tree_int (len, strlen (p1) + 1) <= 0
8806 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8808 const int r = memcmp (p1, p2, tree_to_uhwi (len));
8810 if (r > 0)
8811 return integer_one_node;
8812 else if (r < 0)
8813 return integer_minus_one_node;
8814 else
8815 return integer_zero_node;
8818 /* If len parameter is one, return an expression corresponding to
8819 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8820 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8822 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8823 tree cst_uchar_ptr_node
8824 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8826 tree ind1
8827 = fold_convert_loc (loc, integer_type_node,
8828 build1 (INDIRECT_REF, cst_uchar_node,
8829 fold_convert_loc (loc,
8830 cst_uchar_ptr_node,
8831 arg1)));
8832 tree ind2
8833 = fold_convert_loc (loc, integer_type_node,
8834 build1 (INDIRECT_REF, cst_uchar_node,
8835 fold_convert_loc (loc,
8836 cst_uchar_ptr_node,
8837 arg2)));
8838 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8841 return NULL_TREE;
8844 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8845 Return NULL_TREE if no simplification can be made. */
8847 static tree
8848 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8850 const char *p1, *p2;
8852 if (!validate_arg (arg1, POINTER_TYPE)
8853 || !validate_arg (arg2, POINTER_TYPE))
8854 return NULL_TREE;
8856 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8857 if (operand_equal_p (arg1, arg2, 0))
8858 return integer_zero_node;
8860 p1 = c_getstr (arg1);
8861 p2 = c_getstr (arg2);
8863 if (p1 && p2)
8865 const int i = strcmp (p1, p2);
8866 if (i < 0)
8867 return integer_minus_one_node;
8868 else if (i > 0)
8869 return integer_one_node;
8870 else
8871 return integer_zero_node;
8874 /* If the second arg is "", return *(const unsigned char*)arg1. */
8875 if (p2 && *p2 == '\0')
8877 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8878 tree cst_uchar_ptr_node
8879 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8881 return fold_convert_loc (loc, integer_type_node,
8882 build1 (INDIRECT_REF, cst_uchar_node,
8883 fold_convert_loc (loc,
8884 cst_uchar_ptr_node,
8885 arg1)));
8888 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8889 if (p1 && *p1 == '\0')
8891 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8892 tree cst_uchar_ptr_node
8893 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8895 tree temp
8896 = fold_convert_loc (loc, integer_type_node,
8897 build1 (INDIRECT_REF, cst_uchar_node,
8898 fold_convert_loc (loc,
8899 cst_uchar_ptr_node,
8900 arg2)));
8901 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8904 return NULL_TREE;
8907 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8908 Return NULL_TREE if no simplification can be made. */
8910 static tree
8911 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8913 const char *p1, *p2;
8915 if (!validate_arg (arg1, POINTER_TYPE)
8916 || !validate_arg (arg2, POINTER_TYPE)
8917 || !validate_arg (len, INTEGER_TYPE))
8918 return NULL_TREE;
8920 /* If the LEN parameter is zero, return zero. */
8921 if (integer_zerop (len))
8922 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8923 arg1, arg2);
8925 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8926 if (operand_equal_p (arg1, arg2, 0))
8927 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8929 p1 = c_getstr (arg1);
8930 p2 = c_getstr (arg2);
8932 if (tree_fits_uhwi_p (len) && p1 && p2)
8934 const int i = strncmp (p1, p2, tree_to_uhwi (len));
8935 if (i > 0)
8936 return integer_one_node;
8937 else if (i < 0)
8938 return integer_minus_one_node;
8939 else
8940 return integer_zero_node;
8943 /* If the second arg is "", and the length is greater than zero,
8944 return *(const unsigned char*)arg1. */
8945 if (p2 && *p2 == '\0'
8946 && TREE_CODE (len) == INTEGER_CST
8947 && tree_int_cst_sgn (len) == 1)
8949 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8950 tree cst_uchar_ptr_node
8951 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8953 return fold_convert_loc (loc, integer_type_node,
8954 build1 (INDIRECT_REF, cst_uchar_node,
8955 fold_convert_loc (loc,
8956 cst_uchar_ptr_node,
8957 arg1)));
8960 /* If the first arg is "", and the length is greater than zero,
8961 return -*(const unsigned char*)arg2. */
8962 if (p1 && *p1 == '\0'
8963 && TREE_CODE (len) == INTEGER_CST
8964 && tree_int_cst_sgn (len) == 1)
8966 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8967 tree cst_uchar_ptr_node
8968 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8970 tree temp = fold_convert_loc (loc, integer_type_node,
8971 build1 (INDIRECT_REF, cst_uchar_node,
8972 fold_convert_loc (loc,
8973 cst_uchar_ptr_node,
8974 arg2)));
8975 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8978 /* If len parameter is one, return an expression corresponding to
8979 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8980 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8982 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8983 tree cst_uchar_ptr_node
8984 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8986 tree ind1 = fold_convert_loc (loc, integer_type_node,
8987 build1 (INDIRECT_REF, cst_uchar_node,
8988 fold_convert_loc (loc,
8989 cst_uchar_ptr_node,
8990 arg1)));
8991 tree ind2 = fold_convert_loc (loc, integer_type_node,
8992 build1 (INDIRECT_REF, cst_uchar_node,
8993 fold_convert_loc (loc,
8994 cst_uchar_ptr_node,
8995 arg2)));
8996 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8999 return NULL_TREE;
9002 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9003 ARG. Return NULL_TREE if no simplification can be made. */
9005 static tree
9006 fold_builtin_signbit (location_t loc, tree arg, tree type)
9008 if (!validate_arg (arg, REAL_TYPE))
9009 return NULL_TREE;
9011 /* If ARG is a compile-time constant, determine the result. */
9012 if (TREE_CODE (arg) == REAL_CST
9013 && !TREE_OVERFLOW (arg))
9015 REAL_VALUE_TYPE c;
9017 c = TREE_REAL_CST (arg);
9018 return (REAL_VALUE_NEGATIVE (c)
9019 ? build_one_cst (type)
9020 : build_zero_cst (type));
9023 /* If ARG is non-negative, the result is always zero. */
9024 if (tree_expr_nonnegative_p (arg))
9025 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9027 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9028 if (!HONOR_SIGNED_ZEROS (arg))
9029 return fold_convert (type,
9030 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9031 build_real (TREE_TYPE (arg), dconst0)));
9033 return NULL_TREE;
9036 /* Fold function call to builtin copysign, copysignf or copysignl with
9037 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9038 be made. */
9040 static tree
9041 fold_builtin_copysign (location_t loc, tree fndecl,
9042 tree arg1, tree arg2, tree type)
9044 tree tem;
9046 if (!validate_arg (arg1, REAL_TYPE)
9047 || !validate_arg (arg2, REAL_TYPE))
9048 return NULL_TREE;
9050 /* copysign(X,X) is X. */
9051 if (operand_equal_p (arg1, arg2, 0))
9052 return fold_convert_loc (loc, type, arg1);
9054 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9055 if (TREE_CODE (arg1) == REAL_CST
9056 && TREE_CODE (arg2) == REAL_CST
9057 && !TREE_OVERFLOW (arg1)
9058 && !TREE_OVERFLOW (arg2))
9060 REAL_VALUE_TYPE c1, c2;
9062 c1 = TREE_REAL_CST (arg1);
9063 c2 = TREE_REAL_CST (arg2);
9064 /* c1.sign := c2.sign. */
9065 real_copysign (&c1, &c2);
9066 return build_real (type, c1);
9069 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9070 Remember to evaluate Y for side-effects. */
9071 if (tree_expr_nonnegative_p (arg2))
9072 return omit_one_operand_loc (loc, type,
9073 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9074 arg2);
9076 /* Strip sign changing operations for the first argument. */
9077 tem = fold_strip_sign_ops (arg1);
9078 if (tem)
9079 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9081 return NULL_TREE;
9084 /* Fold a call to builtin isascii with argument ARG. */
9086 static tree
9087 fold_builtin_isascii (location_t loc, tree arg)
9089 if (!validate_arg (arg, INTEGER_TYPE))
9090 return NULL_TREE;
9091 else
9093 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9094 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9095 build_int_cst (integer_type_node,
9096 ~ (unsigned HOST_WIDE_INT) 0x7f));
9097 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9098 arg, integer_zero_node);
9102 /* Fold a call to builtin toascii with argument ARG. */
9104 static tree
9105 fold_builtin_toascii (location_t loc, tree arg)
9107 if (!validate_arg (arg, INTEGER_TYPE))
9108 return NULL_TREE;
9110 /* Transform toascii(c) -> (c & 0x7f). */
9111 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9112 build_int_cst (integer_type_node, 0x7f));
9115 /* Fold a call to builtin isdigit with argument ARG. */
9117 static tree
9118 fold_builtin_isdigit (location_t loc, tree arg)
9120 if (!validate_arg (arg, INTEGER_TYPE))
9121 return NULL_TREE;
9122 else
9124 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9125 /* According to the C standard, isdigit is unaffected by locale.
9126 However, it definitely is affected by the target character set. */
9127 unsigned HOST_WIDE_INT target_digit0
9128 = lang_hooks.to_target_charset ('0');
9130 if (target_digit0 == 0)
9131 return NULL_TREE;
9133 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9134 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9135 build_int_cst (unsigned_type_node, target_digit0));
9136 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9137 build_int_cst (unsigned_type_node, 9));
9141 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9143 static tree
9144 fold_builtin_fabs (location_t loc, tree arg, tree type)
9146 if (!validate_arg (arg, REAL_TYPE))
9147 return NULL_TREE;
9149 arg = fold_convert_loc (loc, type, arg);
9150 if (TREE_CODE (arg) == REAL_CST)
9151 return fold_abs_const (arg, type);
9152 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9155 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9157 static tree
9158 fold_builtin_abs (location_t loc, tree arg, tree type)
9160 if (!validate_arg (arg, INTEGER_TYPE))
9161 return NULL_TREE;
9163 arg = fold_convert_loc (loc, type, arg);
9164 if (TREE_CODE (arg) == INTEGER_CST)
9165 return fold_abs_const (arg, type);
9166 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9169 /* Fold a fma operation with arguments ARG[012]. */
9171 tree
9172 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9173 tree type, tree arg0, tree arg1, tree arg2)
9175 if (TREE_CODE (arg0) == REAL_CST
9176 && TREE_CODE (arg1) == REAL_CST
9177 && TREE_CODE (arg2) == REAL_CST)
9178 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9180 return NULL_TREE;
9183 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9185 static tree
9186 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9188 if (validate_arg (arg0, REAL_TYPE)
9189 && validate_arg (arg1, REAL_TYPE)
9190 && validate_arg (arg2, REAL_TYPE))
9192 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9193 if (tem)
9194 return tem;
9196 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9197 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9198 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9200 return NULL_TREE;
9203 /* Fold a call to builtin fmin or fmax. */
9205 static tree
9206 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9207 tree type, bool max)
9209 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9211 /* Calculate the result when the argument is a constant. */
9212 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9214 if (res)
9215 return res;
9217 /* If either argument is NaN, return the other one. Avoid the
9218 transformation if we get (and honor) a signalling NaN. Using
9219 omit_one_operand() ensures we create a non-lvalue. */
9220 if (TREE_CODE (arg0) == REAL_CST
9221 && real_isnan (&TREE_REAL_CST (arg0))
9222 && (! HONOR_SNANS (arg0)
9223 || ! TREE_REAL_CST (arg0).signalling))
9224 return omit_one_operand_loc (loc, type, arg1, arg0);
9225 if (TREE_CODE (arg1) == REAL_CST
9226 && real_isnan (&TREE_REAL_CST (arg1))
9227 && (! HONOR_SNANS (arg1)
9228 || ! TREE_REAL_CST (arg1).signalling))
9229 return omit_one_operand_loc (loc, type, arg0, arg1);
9231 /* Transform fmin/fmax(x,x) -> x. */
9232 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9233 return omit_one_operand_loc (loc, type, arg0, arg1);
9235 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9236 functions to return the numeric arg if the other one is NaN.
9237 These tree codes don't honor that, so only transform if
9238 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9239 handled, so we don't have to worry about it either. */
9240 if (flag_finite_math_only)
9241 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9242 fold_convert_loc (loc, type, arg0),
9243 fold_convert_loc (loc, type, arg1));
9245 return NULL_TREE;
9248 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9250 static tree
9251 fold_builtin_carg (location_t loc, tree arg, tree type)
9253 if (validate_arg (arg, COMPLEX_TYPE)
9254 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9256 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9258 if (atan2_fn)
9260 tree new_arg = builtin_save_expr (arg);
9261 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9262 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9263 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9267 return NULL_TREE;
9270 /* Fold a call to builtin logb/ilogb. */
9272 static tree
9273 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9275 if (! validate_arg (arg, REAL_TYPE))
9276 return NULL_TREE;
9278 STRIP_NOPS (arg);
9280 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9282 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9284 switch (value->cl)
9286 case rvc_nan:
9287 case rvc_inf:
9288 /* If arg is Inf or NaN and we're logb, return it. */
9289 if (TREE_CODE (rettype) == REAL_TYPE)
9291 /* For logb(-Inf) we have to return +Inf. */
9292 if (real_isinf (value) && real_isneg (value))
9294 REAL_VALUE_TYPE tem;
9295 real_inf (&tem);
9296 return build_real (rettype, tem);
9298 return fold_convert_loc (loc, rettype, arg);
9300 /* Fall through... */
9301 case rvc_zero:
9302 /* Zero may set errno and/or raise an exception for logb, also
9303 for ilogb we don't know FP_ILOGB0. */
9304 return NULL_TREE;
9305 case rvc_normal:
9306 /* For normal numbers, proceed iff radix == 2. In GCC,
9307 normalized significands are in the range [0.5, 1.0). We
9308 want the exponent as if they were [1.0, 2.0) so get the
9309 exponent and subtract 1. */
9310 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9311 return fold_convert_loc (loc, rettype,
9312 build_int_cst (integer_type_node,
9313 REAL_EXP (value)-1));
9314 break;
9318 return NULL_TREE;
9321 /* Fold a call to builtin significand, if radix == 2. */
9323 static tree
9324 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9326 if (! validate_arg (arg, REAL_TYPE))
9327 return NULL_TREE;
9329 STRIP_NOPS (arg);
9331 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9333 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9335 switch (value->cl)
9337 case rvc_zero:
9338 case rvc_nan:
9339 case rvc_inf:
9340 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9341 return fold_convert_loc (loc, rettype, arg);
9342 case rvc_normal:
9343 /* For normal numbers, proceed iff radix == 2. */
9344 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9346 REAL_VALUE_TYPE result = *value;
9347 /* In GCC, normalized significands are in the range [0.5,
9348 1.0). We want them to be [1.0, 2.0) so set the
9349 exponent to 1. */
9350 SET_REAL_EXP (&result, 1);
9351 return build_real (rettype, result);
9353 break;
9357 return NULL_TREE;
9360 /* Fold a call to builtin frexp, we can assume the base is 2. */
9362 static tree
9363 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9365 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9366 return NULL_TREE;
9368 STRIP_NOPS (arg0);
9370 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9371 return NULL_TREE;
9373 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9375 /* Proceed if a valid pointer type was passed in. */
9376 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9378 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9379 tree frac, exp;
9381 switch (value->cl)
9383 case rvc_zero:
9384 /* For +-0, return (*exp = 0, +-0). */
9385 exp = integer_zero_node;
9386 frac = arg0;
9387 break;
9388 case rvc_nan:
9389 case rvc_inf:
9390 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9391 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9392 case rvc_normal:
9394 /* Since the frexp function always expects base 2, and in
9395 GCC normalized significands are already in the range
9396 [0.5, 1.0), we have exactly what frexp wants. */
9397 REAL_VALUE_TYPE frac_rvt = *value;
9398 SET_REAL_EXP (&frac_rvt, 0);
9399 frac = build_real (rettype, frac_rvt);
9400 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9402 break;
9403 default:
9404 gcc_unreachable ();
9407 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9408 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9409 TREE_SIDE_EFFECTS (arg1) = 1;
9410 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9413 return NULL_TREE;
9416 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9417 then we can assume the base is two. If it's false, then we have to
9418 check the mode of the TYPE parameter in certain cases. */
9420 static tree
9421 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9422 tree type, bool ldexp)
9424 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9426 STRIP_NOPS (arg0);
9427 STRIP_NOPS (arg1);
9429 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9430 if (real_zerop (arg0) || integer_zerop (arg1)
9431 || (TREE_CODE (arg0) == REAL_CST
9432 && !real_isfinite (&TREE_REAL_CST (arg0))))
9433 return omit_one_operand_loc (loc, type, arg0, arg1);
9435 /* If both arguments are constant, then try to evaluate it. */
9436 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9437 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9438 && tree_fits_shwi_p (arg1))
9440 /* Bound the maximum adjustment to twice the range of the
9441 mode's valid exponents. Use abs to ensure the range is
9442 positive as a sanity check. */
9443 const long max_exp_adj = 2 *
9444 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9445 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9447 /* Get the user-requested adjustment. */
9448 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9450 /* The requested adjustment must be inside this range. This
9451 is a preliminary cap to avoid things like overflow, we
9452 may still fail to compute the result for other reasons. */
9453 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9455 REAL_VALUE_TYPE initial_result;
9457 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9459 /* Ensure we didn't overflow. */
9460 if (! real_isinf (&initial_result))
9462 const REAL_VALUE_TYPE trunc_result
9463 = real_value_truncate (TYPE_MODE (type), initial_result);
9465 /* Only proceed if the target mode can hold the
9466 resulting value. */
9467 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9468 return build_real (type, trunc_result);
9474 return NULL_TREE;
9477 /* Fold a call to builtin modf. */
9479 static tree
9480 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9482 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9483 return NULL_TREE;
9485 STRIP_NOPS (arg0);
9487 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9488 return NULL_TREE;
9490 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9492 /* Proceed if a valid pointer type was passed in. */
9493 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9495 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9496 REAL_VALUE_TYPE trunc, frac;
9498 switch (value->cl)
9500 case rvc_nan:
9501 case rvc_zero:
9502 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9503 trunc = frac = *value;
9504 break;
9505 case rvc_inf:
9506 /* For +-Inf, return (*arg1 = arg0, +-0). */
9507 frac = dconst0;
9508 frac.sign = value->sign;
9509 trunc = *value;
9510 break;
9511 case rvc_normal:
9512 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9513 real_trunc (&trunc, VOIDmode, value);
9514 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9515 /* If the original number was negative and already
9516 integral, then the fractional part is -0.0. */
9517 if (value->sign && frac.cl == rvc_zero)
9518 frac.sign = value->sign;
9519 break;
9522 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9523 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9524 build_real (rettype, trunc));
9525 TREE_SIDE_EFFECTS (arg1) = 1;
9526 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9527 build_real (rettype, frac));
9530 return NULL_TREE;
9533 /* Given a location LOC, an interclass builtin function decl FNDECL
9534 and its single argument ARG, return an folded expression computing
9535 the same, or NULL_TREE if we either couldn't or didn't want to fold
9536 (the latter happen if there's an RTL instruction available). */
9538 static tree
9539 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9541 machine_mode mode;
9543 if (!validate_arg (arg, REAL_TYPE))
9544 return NULL_TREE;
9546 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9547 return NULL_TREE;
9549 mode = TYPE_MODE (TREE_TYPE (arg));
9551 /* If there is no optab, try generic code. */
9552 switch (DECL_FUNCTION_CODE (fndecl))
9554 tree result;
9556 CASE_FLT_FN (BUILT_IN_ISINF):
9558 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9559 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9560 tree const type = TREE_TYPE (arg);
9561 REAL_VALUE_TYPE r;
9562 char buf[128];
9564 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9565 real_from_string (&r, buf);
9566 result = build_call_expr (isgr_fn, 2,
9567 fold_build1_loc (loc, ABS_EXPR, type, arg),
9568 build_real (type, r));
9569 return result;
9571 CASE_FLT_FN (BUILT_IN_FINITE):
9572 case BUILT_IN_ISFINITE:
9574 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9575 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9576 tree const type = TREE_TYPE (arg);
9577 REAL_VALUE_TYPE r;
9578 char buf[128];
9580 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9581 real_from_string (&r, buf);
9582 result = build_call_expr (isle_fn, 2,
9583 fold_build1_loc (loc, ABS_EXPR, type, arg),
9584 build_real (type, r));
9585 /*result = fold_build2_loc (loc, UNGT_EXPR,
9586 TREE_TYPE (TREE_TYPE (fndecl)),
9587 fold_build1_loc (loc, ABS_EXPR, type, arg),
9588 build_real (type, r));
9589 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9590 TREE_TYPE (TREE_TYPE (fndecl)),
9591 result);*/
9592 return result;
9594 case BUILT_IN_ISNORMAL:
9596 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9597 islessequal(fabs(x),DBL_MAX). */
9598 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9599 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9600 tree const type = TREE_TYPE (arg);
9601 REAL_VALUE_TYPE rmax, rmin;
9602 char buf[128];
9604 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9605 real_from_string (&rmax, buf);
9606 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9607 real_from_string (&rmin, buf);
9608 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9609 result = build_call_expr (isle_fn, 2, arg,
9610 build_real (type, rmax));
9611 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9612 build_call_expr (isge_fn, 2, arg,
9613 build_real (type, rmin)));
9614 return result;
9616 default:
9617 break;
9620 return NULL_TREE;
9623 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9624 ARG is the argument for the call. */
9626 static tree
9627 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9629 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9630 REAL_VALUE_TYPE r;
9632 if (!validate_arg (arg, REAL_TYPE))
9633 return NULL_TREE;
9635 switch (builtin_index)
9637 case BUILT_IN_ISINF:
9638 if (!HONOR_INFINITIES (arg))
9639 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9641 if (TREE_CODE (arg) == REAL_CST)
9643 r = TREE_REAL_CST (arg);
9644 if (real_isinf (&r))
9645 return real_compare (GT_EXPR, &r, &dconst0)
9646 ? integer_one_node : integer_minus_one_node;
9647 else
9648 return integer_zero_node;
9651 return NULL_TREE;
9653 case BUILT_IN_ISINF_SIGN:
9655 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9656 /* In a boolean context, GCC will fold the inner COND_EXPR to
9657 1. So e.g. "if (isinf_sign(x))" would be folded to just
9658 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9659 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9660 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9661 tree tmp = NULL_TREE;
9663 arg = builtin_save_expr (arg);
9665 if (signbit_fn && isinf_fn)
9667 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9668 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9670 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9671 signbit_call, integer_zero_node);
9672 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9673 isinf_call, integer_zero_node);
9675 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9676 integer_minus_one_node, integer_one_node);
9677 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9678 isinf_call, tmp,
9679 integer_zero_node);
9682 return tmp;
9685 case BUILT_IN_ISFINITE:
9686 if (!HONOR_NANS (arg)
9687 && !HONOR_INFINITIES (arg))
9688 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9690 if (TREE_CODE (arg) == REAL_CST)
9692 r = TREE_REAL_CST (arg);
9693 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9696 return NULL_TREE;
9698 case BUILT_IN_ISNAN:
9699 if (!HONOR_NANS (arg))
9700 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9702 if (TREE_CODE (arg) == REAL_CST)
9704 r = TREE_REAL_CST (arg);
9705 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9708 arg = builtin_save_expr (arg);
9709 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9711 default:
9712 gcc_unreachable ();
9716 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9717 This builtin will generate code to return the appropriate floating
9718 point classification depending on the value of the floating point
9719 number passed in. The possible return values must be supplied as
9720 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9721 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9722 one floating point argument which is "type generic". */
9724 static tree
9725 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9727 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9728 arg, type, res, tmp;
9729 machine_mode mode;
9730 REAL_VALUE_TYPE r;
9731 char buf[128];
9733 /* Verify the required arguments in the original call. */
9734 if (nargs != 6
9735 || !validate_arg (args[0], INTEGER_TYPE)
9736 || !validate_arg (args[1], INTEGER_TYPE)
9737 || !validate_arg (args[2], INTEGER_TYPE)
9738 || !validate_arg (args[3], INTEGER_TYPE)
9739 || !validate_arg (args[4], INTEGER_TYPE)
9740 || !validate_arg (args[5], REAL_TYPE))
9741 return NULL_TREE;
9743 fp_nan = args[0];
9744 fp_infinite = args[1];
9745 fp_normal = args[2];
9746 fp_subnormal = args[3];
9747 fp_zero = args[4];
9748 arg = args[5];
9749 type = TREE_TYPE (arg);
9750 mode = TYPE_MODE (type);
9751 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9753 /* fpclassify(x) ->
9754 isnan(x) ? FP_NAN :
9755 (fabs(x) == Inf ? FP_INFINITE :
9756 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9757 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9759 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9760 build_real (type, dconst0));
9761 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9762 tmp, fp_zero, fp_subnormal);
9764 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9765 real_from_string (&r, buf);
9766 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9767 arg, build_real (type, r));
9768 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9770 if (HONOR_INFINITIES (mode))
9772 real_inf (&r);
9773 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9774 build_real (type, r));
9775 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9776 fp_infinite, res);
9779 if (HONOR_NANS (mode))
9781 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9782 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9785 return res;
9788 /* Fold a call to an unordered comparison function such as
9789 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9790 being called and ARG0 and ARG1 are the arguments for the call.
9791 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9792 the opposite of the desired result. UNORDERED_CODE is used
9793 for modes that can hold NaNs and ORDERED_CODE is used for
9794 the rest. */
9796 static tree
9797 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9798 enum tree_code unordered_code,
9799 enum tree_code ordered_code)
9801 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9802 enum tree_code code;
9803 tree type0, type1;
9804 enum tree_code code0, code1;
9805 tree cmp_type = NULL_TREE;
9807 type0 = TREE_TYPE (arg0);
9808 type1 = TREE_TYPE (arg1);
9810 code0 = TREE_CODE (type0);
9811 code1 = TREE_CODE (type1);
9813 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9814 /* Choose the wider of two real types. */
9815 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9816 ? type0 : type1;
9817 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9818 cmp_type = type0;
9819 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9820 cmp_type = type1;
9822 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9823 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9825 if (unordered_code == UNORDERED_EXPR)
9827 if (!HONOR_NANS (arg0))
9828 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9829 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9832 code = HONOR_NANS (arg0) ? unordered_code : ordered_code;
9833 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9834 fold_build2_loc (loc, code, type, arg0, arg1));
9837 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9838 arithmetics if it can never overflow, or into internal functions that
9839 return both result of arithmetics and overflowed boolean flag in
9840 a complex integer result, or some other check for overflow. */
9842 static tree
9843 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
9844 tree arg0, tree arg1, tree arg2)
9846 enum internal_fn ifn = IFN_LAST;
9847 tree type = TREE_TYPE (TREE_TYPE (arg2));
9848 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
9849 switch (fcode)
9851 case BUILT_IN_ADD_OVERFLOW:
9852 case BUILT_IN_SADD_OVERFLOW:
9853 case BUILT_IN_SADDL_OVERFLOW:
9854 case BUILT_IN_SADDLL_OVERFLOW:
9855 case BUILT_IN_UADD_OVERFLOW:
9856 case BUILT_IN_UADDL_OVERFLOW:
9857 case BUILT_IN_UADDLL_OVERFLOW:
9858 ifn = IFN_ADD_OVERFLOW;
9859 break;
9860 case BUILT_IN_SUB_OVERFLOW:
9861 case BUILT_IN_SSUB_OVERFLOW:
9862 case BUILT_IN_SSUBL_OVERFLOW:
9863 case BUILT_IN_SSUBLL_OVERFLOW:
9864 case BUILT_IN_USUB_OVERFLOW:
9865 case BUILT_IN_USUBL_OVERFLOW:
9866 case BUILT_IN_USUBLL_OVERFLOW:
9867 ifn = IFN_SUB_OVERFLOW;
9868 break;
9869 case BUILT_IN_MUL_OVERFLOW:
9870 case BUILT_IN_SMUL_OVERFLOW:
9871 case BUILT_IN_SMULL_OVERFLOW:
9872 case BUILT_IN_SMULLL_OVERFLOW:
9873 case BUILT_IN_UMUL_OVERFLOW:
9874 case BUILT_IN_UMULL_OVERFLOW:
9875 case BUILT_IN_UMULLL_OVERFLOW:
9876 ifn = IFN_MUL_OVERFLOW;
9877 break;
9878 default:
9879 gcc_unreachable ();
9881 tree ctype = build_complex_type (type);
9882 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
9883 2, arg0, arg1);
9884 tree tgt = save_expr (call);
9885 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9886 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9887 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
9888 tree store
9889 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
9890 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
9893 /* Fold a call to built-in function FNDECL with 0 arguments.
9894 This function returns NULL_TREE if no simplification was possible. */
9896 static tree
9897 fold_builtin_0 (location_t loc, tree fndecl)
9899 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9900 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9901 switch (fcode)
9903 CASE_FLT_FN (BUILT_IN_INF):
9904 case BUILT_IN_INFD32:
9905 case BUILT_IN_INFD64:
9906 case BUILT_IN_INFD128:
9907 return fold_builtin_inf (loc, type, true);
9909 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9910 return fold_builtin_inf (loc, type, false);
9912 case BUILT_IN_CLASSIFY_TYPE:
9913 return fold_builtin_classify_type (NULL_TREE);
9915 default:
9916 break;
9918 return NULL_TREE;
9921 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9922 This function returns NULL_TREE if no simplification was possible. */
9924 static tree
9925 fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
9927 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9928 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9929 switch (fcode)
9931 case BUILT_IN_CONSTANT_P:
9933 tree val = fold_builtin_constant_p (arg0);
9935 /* Gimplification will pull the CALL_EXPR for the builtin out of
9936 an if condition. When not optimizing, we'll not CSE it back.
9937 To avoid link error types of regressions, return false now. */
9938 if (!val && !optimize)
9939 val = integer_zero_node;
9941 return val;
9944 case BUILT_IN_CLASSIFY_TYPE:
9945 return fold_builtin_classify_type (arg0);
9947 case BUILT_IN_STRLEN:
9948 return fold_builtin_strlen (loc, type, arg0);
9950 CASE_FLT_FN (BUILT_IN_FABS):
9951 case BUILT_IN_FABSD32:
9952 case BUILT_IN_FABSD64:
9953 case BUILT_IN_FABSD128:
9954 return fold_builtin_fabs (loc, arg0, type);
9956 case BUILT_IN_ABS:
9957 case BUILT_IN_LABS:
9958 case BUILT_IN_LLABS:
9959 case BUILT_IN_IMAXABS:
9960 return fold_builtin_abs (loc, arg0, type);
9962 CASE_FLT_FN (BUILT_IN_CONJ):
9963 if (validate_arg (arg0, COMPLEX_TYPE)
9964 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9965 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9966 break;
9968 CASE_FLT_FN (BUILT_IN_CREAL):
9969 if (validate_arg (arg0, COMPLEX_TYPE)
9970 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9971 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9972 break;
9974 CASE_FLT_FN (BUILT_IN_CIMAG):
9975 if (validate_arg (arg0, COMPLEX_TYPE)
9976 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9977 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9978 break;
9980 CASE_FLT_FN (BUILT_IN_CCOS):
9981 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
9983 CASE_FLT_FN (BUILT_IN_CCOSH):
9984 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
9986 CASE_FLT_FN (BUILT_IN_CPROJ):
9987 return fold_builtin_cproj (loc, arg0, type);
9989 CASE_FLT_FN (BUILT_IN_CSIN):
9990 if (validate_arg (arg0, COMPLEX_TYPE)
9991 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9992 return do_mpc_arg1 (arg0, type, mpc_sin);
9993 break;
9995 CASE_FLT_FN (BUILT_IN_CSINH):
9996 if (validate_arg (arg0, COMPLEX_TYPE)
9997 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9998 return do_mpc_arg1 (arg0, type, mpc_sinh);
9999 break;
10001 CASE_FLT_FN (BUILT_IN_CTAN):
10002 if (validate_arg (arg0, COMPLEX_TYPE)
10003 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10004 return do_mpc_arg1 (arg0, type, mpc_tan);
10005 break;
10007 CASE_FLT_FN (BUILT_IN_CTANH):
10008 if (validate_arg (arg0, COMPLEX_TYPE)
10009 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10010 return do_mpc_arg1 (arg0, type, mpc_tanh);
10011 break;
10013 CASE_FLT_FN (BUILT_IN_CLOG):
10014 if (validate_arg (arg0, COMPLEX_TYPE)
10015 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10016 return do_mpc_arg1 (arg0, type, mpc_log);
10017 break;
10019 CASE_FLT_FN (BUILT_IN_CSQRT):
10020 if (validate_arg (arg0, COMPLEX_TYPE)
10021 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10022 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10023 break;
10025 CASE_FLT_FN (BUILT_IN_CASIN):
10026 if (validate_arg (arg0, COMPLEX_TYPE)
10027 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10028 return do_mpc_arg1 (arg0, type, mpc_asin);
10029 break;
10031 CASE_FLT_FN (BUILT_IN_CACOS):
10032 if (validate_arg (arg0, COMPLEX_TYPE)
10033 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10034 return do_mpc_arg1 (arg0, type, mpc_acos);
10035 break;
10037 CASE_FLT_FN (BUILT_IN_CATAN):
10038 if (validate_arg (arg0, COMPLEX_TYPE)
10039 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10040 return do_mpc_arg1 (arg0, type, mpc_atan);
10041 break;
10043 CASE_FLT_FN (BUILT_IN_CASINH):
10044 if (validate_arg (arg0, COMPLEX_TYPE)
10045 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10046 return do_mpc_arg1 (arg0, type, mpc_asinh);
10047 break;
10049 CASE_FLT_FN (BUILT_IN_CACOSH):
10050 if (validate_arg (arg0, COMPLEX_TYPE)
10051 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10052 return do_mpc_arg1 (arg0, type, mpc_acosh);
10053 break;
10055 CASE_FLT_FN (BUILT_IN_CATANH):
10056 if (validate_arg (arg0, COMPLEX_TYPE)
10057 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10058 return do_mpc_arg1 (arg0, type, mpc_atanh);
10059 break;
10061 CASE_FLT_FN (BUILT_IN_CABS):
10062 return fold_builtin_cabs (loc, arg0, type, fndecl);
10064 CASE_FLT_FN (BUILT_IN_CARG):
10065 return fold_builtin_carg (loc, arg0, type);
10067 CASE_FLT_FN (BUILT_IN_SQRT):
10068 return fold_builtin_sqrt (loc, arg0, type);
10070 CASE_FLT_FN (BUILT_IN_CBRT):
10071 return fold_builtin_cbrt (loc, arg0, type);
10073 CASE_FLT_FN (BUILT_IN_ASIN):
10074 if (validate_arg (arg0, REAL_TYPE))
10075 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10076 &dconstm1, &dconst1, true);
10077 break;
10079 CASE_FLT_FN (BUILT_IN_ACOS):
10080 if (validate_arg (arg0, REAL_TYPE))
10081 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10082 &dconstm1, &dconst1, true);
10083 break;
10085 CASE_FLT_FN (BUILT_IN_ATAN):
10086 if (validate_arg (arg0, REAL_TYPE))
10087 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10088 break;
10090 CASE_FLT_FN (BUILT_IN_ASINH):
10091 if (validate_arg (arg0, REAL_TYPE))
10092 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10093 break;
10095 CASE_FLT_FN (BUILT_IN_ACOSH):
10096 if (validate_arg (arg0, REAL_TYPE))
10097 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10098 &dconst1, NULL, true);
10099 break;
10101 CASE_FLT_FN (BUILT_IN_ATANH):
10102 if (validate_arg (arg0, REAL_TYPE))
10103 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10104 &dconstm1, &dconst1, false);
10105 break;
10107 CASE_FLT_FN (BUILT_IN_SIN):
10108 if (validate_arg (arg0, REAL_TYPE))
10109 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10110 break;
10112 CASE_FLT_FN (BUILT_IN_COS):
10113 return fold_builtin_cos (loc, arg0, type, fndecl);
10115 CASE_FLT_FN (BUILT_IN_TAN):
10116 return fold_builtin_tan (arg0, type);
10118 CASE_FLT_FN (BUILT_IN_CEXP):
10119 return fold_builtin_cexp (loc, arg0, type);
10121 CASE_FLT_FN (BUILT_IN_CEXPI):
10122 if (validate_arg (arg0, REAL_TYPE))
10123 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10124 break;
10126 CASE_FLT_FN (BUILT_IN_SINH):
10127 if (validate_arg (arg0, REAL_TYPE))
10128 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10129 break;
10131 CASE_FLT_FN (BUILT_IN_COSH):
10132 return fold_builtin_cosh (loc, arg0, type, fndecl);
10134 CASE_FLT_FN (BUILT_IN_TANH):
10135 if (validate_arg (arg0, REAL_TYPE))
10136 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10137 break;
10139 CASE_FLT_FN (BUILT_IN_ERF):
10140 if (validate_arg (arg0, REAL_TYPE))
10141 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10142 break;
10144 CASE_FLT_FN (BUILT_IN_ERFC):
10145 if (validate_arg (arg0, REAL_TYPE))
10146 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10147 break;
10149 CASE_FLT_FN (BUILT_IN_TGAMMA):
10150 if (validate_arg (arg0, REAL_TYPE))
10151 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10152 break;
10154 CASE_FLT_FN (BUILT_IN_EXP):
10155 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10157 CASE_FLT_FN (BUILT_IN_EXP2):
10158 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10160 CASE_FLT_FN (BUILT_IN_EXP10):
10161 CASE_FLT_FN (BUILT_IN_POW10):
10162 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10164 CASE_FLT_FN (BUILT_IN_EXPM1):
10165 if (validate_arg (arg0, REAL_TYPE))
10166 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10167 break;
10169 CASE_FLT_FN (BUILT_IN_LOG):
10170 if (validate_arg (arg0, REAL_TYPE))
10171 return do_mpfr_arg1 (arg0, type, mpfr_log, &dconst0, NULL, false);
10172 break;
10174 CASE_FLT_FN (BUILT_IN_LOG2):
10175 if (validate_arg (arg0, REAL_TYPE))
10176 return do_mpfr_arg1 (arg0, type, mpfr_log2, &dconst0, NULL, false);
10177 break;
10179 CASE_FLT_FN (BUILT_IN_LOG10):
10180 if (validate_arg (arg0, REAL_TYPE))
10181 return do_mpfr_arg1 (arg0, type, mpfr_log10, &dconst0, NULL, false);
10182 break;
10184 CASE_FLT_FN (BUILT_IN_LOG1P):
10185 if (validate_arg (arg0, REAL_TYPE))
10186 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10187 &dconstm1, NULL, false);
10188 break;
10190 CASE_FLT_FN (BUILT_IN_J0):
10191 if (validate_arg (arg0, REAL_TYPE))
10192 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10193 NULL, NULL, 0);
10194 break;
10196 CASE_FLT_FN (BUILT_IN_J1):
10197 if (validate_arg (arg0, REAL_TYPE))
10198 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10199 NULL, NULL, 0);
10200 break;
10202 CASE_FLT_FN (BUILT_IN_Y0):
10203 if (validate_arg (arg0, REAL_TYPE))
10204 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10205 &dconst0, NULL, false);
10206 break;
10208 CASE_FLT_FN (BUILT_IN_Y1):
10209 if (validate_arg (arg0, REAL_TYPE))
10210 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10211 &dconst0, NULL, false);
10212 break;
10214 CASE_FLT_FN (BUILT_IN_NAN):
10215 case BUILT_IN_NAND32:
10216 case BUILT_IN_NAND64:
10217 case BUILT_IN_NAND128:
10218 return fold_builtin_nan (arg0, type, true);
10220 CASE_FLT_FN (BUILT_IN_NANS):
10221 return fold_builtin_nan (arg0, type, false);
10223 CASE_FLT_FN (BUILT_IN_FLOOR):
10224 return fold_builtin_floor (loc, fndecl, arg0);
10226 CASE_FLT_FN (BUILT_IN_CEIL):
10227 return fold_builtin_ceil (loc, fndecl, arg0);
10229 CASE_FLT_FN (BUILT_IN_TRUNC):
10230 return fold_builtin_trunc (loc, fndecl, arg0);
10232 CASE_FLT_FN (BUILT_IN_ROUND):
10233 return fold_builtin_round (loc, fndecl, arg0);
10235 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10236 CASE_FLT_FN (BUILT_IN_RINT):
10237 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10239 CASE_FLT_FN (BUILT_IN_ICEIL):
10240 CASE_FLT_FN (BUILT_IN_LCEIL):
10241 CASE_FLT_FN (BUILT_IN_LLCEIL):
10242 CASE_FLT_FN (BUILT_IN_LFLOOR):
10243 CASE_FLT_FN (BUILT_IN_IFLOOR):
10244 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10245 CASE_FLT_FN (BUILT_IN_IROUND):
10246 CASE_FLT_FN (BUILT_IN_LROUND):
10247 CASE_FLT_FN (BUILT_IN_LLROUND):
10248 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10250 CASE_FLT_FN (BUILT_IN_IRINT):
10251 CASE_FLT_FN (BUILT_IN_LRINT):
10252 CASE_FLT_FN (BUILT_IN_LLRINT):
10253 return fold_fixed_mathfn (loc, fndecl, arg0);
10255 case BUILT_IN_BSWAP16:
10256 case BUILT_IN_BSWAP32:
10257 case BUILT_IN_BSWAP64:
10258 return fold_builtin_bswap (fndecl, arg0);
10260 CASE_INT_FN (BUILT_IN_FFS):
10261 CASE_INT_FN (BUILT_IN_CLZ):
10262 CASE_INT_FN (BUILT_IN_CTZ):
10263 CASE_INT_FN (BUILT_IN_CLRSB):
10264 CASE_INT_FN (BUILT_IN_POPCOUNT):
10265 CASE_INT_FN (BUILT_IN_PARITY):
10266 return fold_builtin_bitop (fndecl, arg0);
10268 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10269 return fold_builtin_signbit (loc, arg0, type);
10271 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10272 return fold_builtin_significand (loc, arg0, type);
10274 CASE_FLT_FN (BUILT_IN_ILOGB):
10275 CASE_FLT_FN (BUILT_IN_LOGB):
10276 return fold_builtin_logb (loc, arg0, type);
10278 case BUILT_IN_ISASCII:
10279 return fold_builtin_isascii (loc, arg0);
10281 case BUILT_IN_TOASCII:
10282 return fold_builtin_toascii (loc, arg0);
10284 case BUILT_IN_ISDIGIT:
10285 return fold_builtin_isdigit (loc, arg0);
10287 CASE_FLT_FN (BUILT_IN_FINITE):
10288 case BUILT_IN_FINITED32:
10289 case BUILT_IN_FINITED64:
10290 case BUILT_IN_FINITED128:
10291 case BUILT_IN_ISFINITE:
10293 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10294 if (ret)
10295 return ret;
10296 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10299 CASE_FLT_FN (BUILT_IN_ISINF):
10300 case BUILT_IN_ISINFD32:
10301 case BUILT_IN_ISINFD64:
10302 case BUILT_IN_ISINFD128:
10304 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10305 if (ret)
10306 return ret;
10307 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10310 case BUILT_IN_ISNORMAL:
10311 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10313 case BUILT_IN_ISINF_SIGN:
10314 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10316 CASE_FLT_FN (BUILT_IN_ISNAN):
10317 case BUILT_IN_ISNAND32:
10318 case BUILT_IN_ISNAND64:
10319 case BUILT_IN_ISNAND128:
10320 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10322 case BUILT_IN_FREE:
10323 if (integer_zerop (arg0))
10324 return build_empty_stmt (loc);
10325 break;
10327 default:
10328 break;
10331 return NULL_TREE;
10335 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10336 This function returns NULL_TREE if no simplification was possible. */
10338 static tree
10339 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
10341 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10342 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10344 switch (fcode)
10346 CASE_FLT_FN (BUILT_IN_JN):
10347 if (validate_arg (arg0, INTEGER_TYPE)
10348 && validate_arg (arg1, REAL_TYPE))
10349 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10350 break;
10352 CASE_FLT_FN (BUILT_IN_YN):
10353 if (validate_arg (arg0, INTEGER_TYPE)
10354 && validate_arg (arg1, REAL_TYPE))
10355 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10356 &dconst0, false);
10357 break;
10359 CASE_FLT_FN (BUILT_IN_DREM):
10360 CASE_FLT_FN (BUILT_IN_REMAINDER):
10361 if (validate_arg (arg0, REAL_TYPE)
10362 && validate_arg (arg1, REAL_TYPE))
10363 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10364 break;
10366 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10367 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10368 if (validate_arg (arg0, REAL_TYPE)
10369 && validate_arg (arg1, POINTER_TYPE))
10370 return do_mpfr_lgamma_r (arg0, arg1, type);
10371 break;
10373 CASE_FLT_FN (BUILT_IN_ATAN2):
10374 if (validate_arg (arg0, REAL_TYPE)
10375 && validate_arg (arg1, REAL_TYPE))
10376 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10377 break;
10379 CASE_FLT_FN (BUILT_IN_FDIM):
10380 if (validate_arg (arg0, REAL_TYPE)
10381 && validate_arg (arg1, REAL_TYPE))
10382 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10383 break;
10385 CASE_FLT_FN (BUILT_IN_HYPOT):
10386 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10388 CASE_FLT_FN (BUILT_IN_CPOW):
10389 if (validate_arg (arg0, COMPLEX_TYPE)
10390 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10391 && validate_arg (arg1, COMPLEX_TYPE)
10392 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10393 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10394 break;
10396 CASE_FLT_FN (BUILT_IN_LDEXP):
10397 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10398 CASE_FLT_FN (BUILT_IN_SCALBN):
10399 CASE_FLT_FN (BUILT_IN_SCALBLN):
10400 return fold_builtin_load_exponent (loc, arg0, arg1,
10401 type, /*ldexp=*/false);
10403 CASE_FLT_FN (BUILT_IN_FREXP):
10404 return fold_builtin_frexp (loc, arg0, arg1, type);
10406 CASE_FLT_FN (BUILT_IN_MODF):
10407 return fold_builtin_modf (loc, arg0, arg1, type);
10409 case BUILT_IN_STRSTR:
10410 return fold_builtin_strstr (loc, arg0, arg1, type);
10412 case BUILT_IN_STRSPN:
10413 return fold_builtin_strspn (loc, arg0, arg1);
10415 case BUILT_IN_STRCSPN:
10416 return fold_builtin_strcspn (loc, arg0, arg1);
10418 case BUILT_IN_STRCHR:
10419 case BUILT_IN_INDEX:
10420 return fold_builtin_strchr (loc, arg0, arg1, type);
10422 case BUILT_IN_STRRCHR:
10423 case BUILT_IN_RINDEX:
10424 return fold_builtin_strrchr (loc, arg0, arg1, type);
10426 case BUILT_IN_STRCMP:
10427 return fold_builtin_strcmp (loc, arg0, arg1);
10429 case BUILT_IN_STRPBRK:
10430 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10432 case BUILT_IN_EXPECT:
10433 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
10435 CASE_FLT_FN (BUILT_IN_POW):
10436 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10438 CASE_FLT_FN (BUILT_IN_POWI):
10439 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10441 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10442 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10444 CASE_FLT_FN (BUILT_IN_FMIN):
10445 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10447 CASE_FLT_FN (BUILT_IN_FMAX):
10448 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10450 case BUILT_IN_ISGREATER:
10451 return fold_builtin_unordered_cmp (loc, fndecl,
10452 arg0, arg1, UNLE_EXPR, LE_EXPR);
10453 case BUILT_IN_ISGREATEREQUAL:
10454 return fold_builtin_unordered_cmp (loc, fndecl,
10455 arg0, arg1, UNLT_EXPR, LT_EXPR);
10456 case BUILT_IN_ISLESS:
10457 return fold_builtin_unordered_cmp (loc, fndecl,
10458 arg0, arg1, UNGE_EXPR, GE_EXPR);
10459 case BUILT_IN_ISLESSEQUAL:
10460 return fold_builtin_unordered_cmp (loc, fndecl,
10461 arg0, arg1, UNGT_EXPR, GT_EXPR);
10462 case BUILT_IN_ISLESSGREATER:
10463 return fold_builtin_unordered_cmp (loc, fndecl,
10464 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10465 case BUILT_IN_ISUNORDERED:
10466 return fold_builtin_unordered_cmp (loc, fndecl,
10467 arg0, arg1, UNORDERED_EXPR,
10468 NOP_EXPR);
10470 /* We do the folding for va_start in the expander. */
10471 case BUILT_IN_VA_START:
10472 break;
10474 case BUILT_IN_OBJECT_SIZE:
10475 return fold_builtin_object_size (arg0, arg1);
10477 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10478 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10480 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10481 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10483 default:
10484 break;
10486 return NULL_TREE;
10489 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10490 and ARG2.
10491 This function returns NULL_TREE if no simplification was possible. */
10493 static tree
10494 fold_builtin_3 (location_t loc, tree fndecl,
10495 tree arg0, tree arg1, tree arg2)
10497 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10498 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10499 switch (fcode)
10502 CASE_FLT_FN (BUILT_IN_SINCOS):
10503 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10505 CASE_FLT_FN (BUILT_IN_FMA):
10506 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10507 break;
10509 CASE_FLT_FN (BUILT_IN_REMQUO):
10510 if (validate_arg (arg0, REAL_TYPE)
10511 && validate_arg (arg1, REAL_TYPE)
10512 && validate_arg (arg2, POINTER_TYPE))
10513 return do_mpfr_remquo (arg0, arg1, arg2);
10514 break;
10516 case BUILT_IN_STRNCMP:
10517 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10519 case BUILT_IN_MEMCHR:
10520 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10522 case BUILT_IN_BCMP:
10523 case BUILT_IN_MEMCMP:
10524 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10526 case BUILT_IN_EXPECT:
10527 return fold_builtin_expect (loc, arg0, arg1, arg2);
10529 case BUILT_IN_ADD_OVERFLOW:
10530 case BUILT_IN_SUB_OVERFLOW:
10531 case BUILT_IN_MUL_OVERFLOW:
10532 case BUILT_IN_SADD_OVERFLOW:
10533 case BUILT_IN_SADDL_OVERFLOW:
10534 case BUILT_IN_SADDLL_OVERFLOW:
10535 case BUILT_IN_SSUB_OVERFLOW:
10536 case BUILT_IN_SSUBL_OVERFLOW:
10537 case BUILT_IN_SSUBLL_OVERFLOW:
10538 case BUILT_IN_SMUL_OVERFLOW:
10539 case BUILT_IN_SMULL_OVERFLOW:
10540 case BUILT_IN_SMULLL_OVERFLOW:
10541 case BUILT_IN_UADD_OVERFLOW:
10542 case BUILT_IN_UADDL_OVERFLOW:
10543 case BUILT_IN_UADDLL_OVERFLOW:
10544 case BUILT_IN_USUB_OVERFLOW:
10545 case BUILT_IN_USUBL_OVERFLOW:
10546 case BUILT_IN_USUBLL_OVERFLOW:
10547 case BUILT_IN_UMUL_OVERFLOW:
10548 case BUILT_IN_UMULL_OVERFLOW:
10549 case BUILT_IN_UMULLL_OVERFLOW:
10550 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
10552 default:
10553 break;
10555 return NULL_TREE;
10558 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10559 arguments. IGNORE is true if the result of the
10560 function call is ignored. This function returns NULL_TREE if no
10561 simplification was possible. */
10563 tree
10564 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool)
10566 tree ret = NULL_TREE;
10568 switch (nargs)
10570 case 0:
10571 ret = fold_builtin_0 (loc, fndecl);
10572 break;
10573 case 1:
10574 ret = fold_builtin_1 (loc, fndecl, args[0]);
10575 break;
10576 case 2:
10577 ret = fold_builtin_2 (loc, fndecl, args[0], args[1]);
10578 break;
10579 case 3:
10580 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
10581 break;
10582 default:
10583 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
10584 break;
10586 if (ret)
10588 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10589 SET_EXPR_LOCATION (ret, loc);
10590 TREE_NO_WARNING (ret) = 1;
10591 return ret;
10593 return NULL_TREE;
10596 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10597 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10598 of arguments in ARGS to be omitted. OLDNARGS is the number of
10599 elements in ARGS. */
10601 static tree
10602 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10603 int skip, tree fndecl, int n, va_list newargs)
10605 int nargs = oldnargs - skip + n;
10606 tree *buffer;
10608 if (n > 0)
10610 int i, j;
10612 buffer = XALLOCAVEC (tree, nargs);
10613 for (i = 0; i < n; i++)
10614 buffer[i] = va_arg (newargs, tree);
10615 for (j = skip; j < oldnargs; j++, i++)
10616 buffer[i] = args[j];
10618 else
10619 buffer = args + skip;
10621 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10624 /* Return true if FNDECL shouldn't be folded right now.
10625 If a built-in function has an inline attribute always_inline
10626 wrapper, defer folding it after always_inline functions have
10627 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10628 might not be performed. */
10630 bool
10631 avoid_folding_inline_builtin (tree fndecl)
10633 return (DECL_DECLARED_INLINE_P (fndecl)
10634 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10635 && cfun
10636 && !cfun->always_inline_functions_inlined
10637 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10640 /* A wrapper function for builtin folding that prevents warnings for
10641 "statement without effect" and the like, caused by removing the
10642 call node earlier than the warning is generated. */
10644 tree
10645 fold_call_expr (location_t loc, tree exp, bool ignore)
10647 tree ret = NULL_TREE;
10648 tree fndecl = get_callee_fndecl (exp);
10649 if (fndecl
10650 && TREE_CODE (fndecl) == FUNCTION_DECL
10651 && DECL_BUILT_IN (fndecl)
10652 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10653 yet. Defer folding until we see all the arguments
10654 (after inlining). */
10655 && !CALL_EXPR_VA_ARG_PACK (exp))
10657 int nargs = call_expr_nargs (exp);
10659 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10660 instead last argument is __builtin_va_arg_pack (). Defer folding
10661 even in that case, until arguments are finalized. */
10662 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10664 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10665 if (fndecl2
10666 && TREE_CODE (fndecl2) == FUNCTION_DECL
10667 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10668 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10669 return NULL_TREE;
10672 if (avoid_folding_inline_builtin (fndecl))
10673 return NULL_TREE;
10675 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10676 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10677 CALL_EXPR_ARGP (exp), ignore);
10678 else
10680 tree *args = CALL_EXPR_ARGP (exp);
10681 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10682 if (ret)
10683 return ret;
10686 return NULL_TREE;
10689 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
10690 N arguments are passed in the array ARGARRAY. Return a folded
10691 expression or NULL_TREE if no simplification was possible. */
10693 tree
10694 fold_builtin_call_array (location_t loc, tree,
10695 tree fn,
10696 int n,
10697 tree *argarray)
10699 if (TREE_CODE (fn) != ADDR_EXPR)
10700 return NULL_TREE;
10702 tree fndecl = TREE_OPERAND (fn, 0);
10703 if (TREE_CODE (fndecl) == FUNCTION_DECL
10704 && DECL_BUILT_IN (fndecl))
10706 /* If last argument is __builtin_va_arg_pack (), arguments to this
10707 function are not finalized yet. Defer folding until they are. */
10708 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10710 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10711 if (fndecl2
10712 && TREE_CODE (fndecl2) == FUNCTION_DECL
10713 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10714 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10715 return NULL_TREE;
10717 if (avoid_folding_inline_builtin (fndecl))
10718 return NULL_TREE;
10719 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10720 return targetm.fold_builtin (fndecl, n, argarray, false);
10721 else
10722 return fold_builtin_n (loc, fndecl, argarray, n, false);
10725 return NULL_TREE;
10728 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10729 along with N new arguments specified as the "..." parameters. SKIP
10730 is the number of arguments in EXP to be omitted. This function is used
10731 to do varargs-to-varargs transformations. */
10733 static tree
10734 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10736 va_list ap;
10737 tree t;
10739 va_start (ap, n);
10740 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10741 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10742 va_end (ap);
10744 return t;
10747 /* Validate a single argument ARG against a tree code CODE representing
10748 a type. */
10750 static bool
10751 validate_arg (const_tree arg, enum tree_code code)
10753 if (!arg)
10754 return false;
10755 else if (code == POINTER_TYPE)
10756 return POINTER_TYPE_P (TREE_TYPE (arg));
10757 else if (code == INTEGER_TYPE)
10758 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10759 return code == TREE_CODE (TREE_TYPE (arg));
10762 /* This function validates the types of a function call argument list
10763 against a specified list of tree_codes. If the last specifier is a 0,
10764 that represents an ellipses, otherwise the last specifier must be a
10765 VOID_TYPE.
10767 This is the GIMPLE version of validate_arglist. Eventually we want to
10768 completely convert builtins.c to work from GIMPLEs and the tree based
10769 validate_arglist will then be removed. */
10771 bool
10772 validate_gimple_arglist (const gcall *call, ...)
10774 enum tree_code code;
10775 bool res = 0;
10776 va_list ap;
10777 const_tree arg;
10778 size_t i;
10780 va_start (ap, call);
10781 i = 0;
10785 code = (enum tree_code) va_arg (ap, int);
10786 switch (code)
10788 case 0:
10789 /* This signifies an ellipses, any further arguments are all ok. */
10790 res = true;
10791 goto end;
10792 case VOID_TYPE:
10793 /* This signifies an endlink, if no arguments remain, return
10794 true, otherwise return false. */
10795 res = (i == gimple_call_num_args (call));
10796 goto end;
10797 default:
10798 /* If no parameters remain or the parameter's code does not
10799 match the specified code, return false. Otherwise continue
10800 checking any remaining arguments. */
10801 arg = gimple_call_arg (call, i++);
10802 if (!validate_arg (arg, code))
10803 goto end;
10804 break;
10807 while (1);
10809 /* We need gotos here since we can only have one VA_CLOSE in a
10810 function. */
10811 end: ;
10812 va_end (ap);
10814 return res;
10817 /* Default target-specific builtin expander that does nothing. */
10820 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10821 rtx target ATTRIBUTE_UNUSED,
10822 rtx subtarget ATTRIBUTE_UNUSED,
10823 machine_mode mode ATTRIBUTE_UNUSED,
10824 int ignore ATTRIBUTE_UNUSED)
10826 return NULL_RTX;
10829 /* Returns true is EXP represents data that would potentially reside
10830 in a readonly section. */
10832 bool
10833 readonly_data_expr (tree exp)
10835 STRIP_NOPS (exp);
10837 if (TREE_CODE (exp) != ADDR_EXPR)
10838 return false;
10840 exp = get_base_address (TREE_OPERAND (exp, 0));
10841 if (!exp)
10842 return false;
10844 /* Make sure we call decl_readonly_section only for trees it
10845 can handle (since it returns true for everything it doesn't
10846 understand). */
10847 if (TREE_CODE (exp) == STRING_CST
10848 || TREE_CODE (exp) == CONSTRUCTOR
10849 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
10850 return decl_readonly_section (exp, 0);
10851 else
10852 return false;
10855 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10856 to the call, and TYPE is its return type.
10858 Return NULL_TREE if no simplification was possible, otherwise return the
10859 simplified form of the call as a tree.
10861 The simplified form may be a constant or other expression which
10862 computes the same value, but in a more efficient manner (including
10863 calls to other builtin functions).
10865 The call may contain arguments which need to be evaluated, but
10866 which are not useful to determine the result of the call. In
10867 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10868 COMPOUND_EXPR will be an argument which must be evaluated.
10869 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10870 COMPOUND_EXPR in the chain will contain the tree for the simplified
10871 form of the builtin function call. */
10873 static tree
10874 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
10876 if (!validate_arg (s1, POINTER_TYPE)
10877 || !validate_arg (s2, POINTER_TYPE))
10878 return NULL_TREE;
10879 else
10881 tree fn;
10882 const char *p1, *p2;
10884 p2 = c_getstr (s2);
10885 if (p2 == NULL)
10886 return NULL_TREE;
10888 p1 = c_getstr (s1);
10889 if (p1 != NULL)
10891 const char *r = strstr (p1, p2);
10892 tree tem;
10894 if (r == NULL)
10895 return build_int_cst (TREE_TYPE (s1), 0);
10897 /* Return an offset into the constant string argument. */
10898 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10899 return fold_convert_loc (loc, type, tem);
10902 /* The argument is const char *, and the result is char *, so we need
10903 a type conversion here to avoid a warning. */
10904 if (p2[0] == '\0')
10905 return fold_convert_loc (loc, type, s1);
10907 if (p2[1] != '\0')
10908 return NULL_TREE;
10910 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10911 if (!fn)
10912 return NULL_TREE;
10914 /* New argument list transforming strstr(s1, s2) to
10915 strchr(s1, s2[0]). */
10916 return build_call_expr_loc (loc, fn, 2, s1,
10917 build_int_cst (integer_type_node, p2[0]));
10921 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10922 the call, and TYPE is its return type.
10924 Return NULL_TREE if no simplification was possible, otherwise return the
10925 simplified form of the call as a tree.
10927 The simplified form may be a constant or other expression which
10928 computes the same value, but in a more efficient manner (including
10929 calls to other builtin functions).
10931 The call may contain arguments which need to be evaluated, but
10932 which are not useful to determine the result of the call. In
10933 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10934 COMPOUND_EXPR will be an argument which must be evaluated.
10935 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10936 COMPOUND_EXPR in the chain will contain the tree for the simplified
10937 form of the builtin function call. */
10939 static tree
10940 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
10942 if (!validate_arg (s1, POINTER_TYPE)
10943 || !validate_arg (s2, INTEGER_TYPE))
10944 return NULL_TREE;
10945 else
10947 const char *p1;
10949 if (TREE_CODE (s2) != INTEGER_CST)
10950 return NULL_TREE;
10952 p1 = c_getstr (s1);
10953 if (p1 != NULL)
10955 char c;
10956 const char *r;
10957 tree tem;
10959 if (target_char_cast (s2, &c))
10960 return NULL_TREE;
10962 r = strchr (p1, c);
10964 if (r == NULL)
10965 return build_int_cst (TREE_TYPE (s1), 0);
10967 /* Return an offset into the constant string argument. */
10968 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10969 return fold_convert_loc (loc, type, tem);
10971 return NULL_TREE;
10975 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10976 the call, and TYPE is its return type.
10978 Return NULL_TREE if no simplification was possible, otherwise return the
10979 simplified form of the call as a tree.
10981 The simplified form may be a constant or other expression which
10982 computes the same value, but in a more efficient manner (including
10983 calls to other builtin functions).
10985 The call may contain arguments which need to be evaluated, but
10986 which are not useful to determine the result of the call. In
10987 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10988 COMPOUND_EXPR will be an argument which must be evaluated.
10989 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10990 COMPOUND_EXPR in the chain will contain the tree for the simplified
10991 form of the builtin function call. */
10993 static tree
10994 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
10996 if (!validate_arg (s1, POINTER_TYPE)
10997 || !validate_arg (s2, INTEGER_TYPE))
10998 return NULL_TREE;
10999 else
11001 tree fn;
11002 const char *p1;
11004 if (TREE_CODE (s2) != INTEGER_CST)
11005 return NULL_TREE;
11007 p1 = c_getstr (s1);
11008 if (p1 != NULL)
11010 char c;
11011 const char *r;
11012 tree tem;
11014 if (target_char_cast (s2, &c))
11015 return NULL_TREE;
11017 r = strrchr (p1, c);
11019 if (r == NULL)
11020 return build_int_cst (TREE_TYPE (s1), 0);
11022 /* Return an offset into the constant string argument. */
11023 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11024 return fold_convert_loc (loc, type, tem);
11027 if (! integer_zerop (s2))
11028 return NULL_TREE;
11030 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11031 if (!fn)
11032 return NULL_TREE;
11034 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11035 return build_call_expr_loc (loc, fn, 2, s1, s2);
11039 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11040 to the call, and TYPE is its return type.
11042 Return NULL_TREE if no simplification was possible, otherwise return the
11043 simplified form of the call as a tree.
11045 The simplified form may be a constant or other expression which
11046 computes the same value, but in a more efficient manner (including
11047 calls to other builtin functions).
11049 The call may contain arguments which need to be evaluated, but
11050 which are not useful to determine the result of the call. In
11051 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11052 COMPOUND_EXPR will be an argument which must be evaluated.
11053 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11054 COMPOUND_EXPR in the chain will contain the tree for the simplified
11055 form of the builtin function call. */
11057 static tree
11058 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11060 if (!validate_arg (s1, POINTER_TYPE)
11061 || !validate_arg (s2, POINTER_TYPE))
11062 return NULL_TREE;
11063 else
11065 tree fn;
11066 const char *p1, *p2;
11068 p2 = c_getstr (s2);
11069 if (p2 == NULL)
11070 return NULL_TREE;
11072 p1 = c_getstr (s1);
11073 if (p1 != NULL)
11075 const char *r = strpbrk (p1, p2);
11076 tree tem;
11078 if (r == NULL)
11079 return build_int_cst (TREE_TYPE (s1), 0);
11081 /* Return an offset into the constant string argument. */
11082 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11083 return fold_convert_loc (loc, type, tem);
11086 if (p2[0] == '\0')
11087 /* strpbrk(x, "") == NULL.
11088 Evaluate and ignore s1 in case it had side-effects. */
11089 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11091 if (p2[1] != '\0')
11092 return NULL_TREE; /* Really call strpbrk. */
11094 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11095 if (!fn)
11096 return NULL_TREE;
11098 /* New argument list transforming strpbrk(s1, s2) to
11099 strchr(s1, s2[0]). */
11100 return build_call_expr_loc (loc, fn, 2, s1,
11101 build_int_cst (integer_type_node, p2[0]));
11105 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11106 to the call.
11108 Return NULL_TREE if no simplification was possible, otherwise return the
11109 simplified form of the call as a tree.
11111 The simplified form may be a constant or other expression which
11112 computes the same value, but in a more efficient manner (including
11113 calls to other builtin functions).
11115 The call may contain arguments which need to be evaluated, but
11116 which are not useful to determine the result of the call. In
11117 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11118 COMPOUND_EXPR will be an argument which must be evaluated.
11119 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11120 COMPOUND_EXPR in the chain will contain the tree for the simplified
11121 form of the builtin function call. */
11123 static tree
11124 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11126 if (!validate_arg (s1, POINTER_TYPE)
11127 || !validate_arg (s2, POINTER_TYPE))
11128 return NULL_TREE;
11129 else
11131 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11133 /* If both arguments are constants, evaluate at compile-time. */
11134 if (p1 && p2)
11136 const size_t r = strspn (p1, p2);
11137 return build_int_cst (size_type_node, r);
11140 /* If either argument is "", return NULL_TREE. */
11141 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11142 /* Evaluate and ignore both arguments in case either one has
11143 side-effects. */
11144 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11145 s1, s2);
11146 return NULL_TREE;
11150 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11151 to the call.
11153 Return NULL_TREE if no simplification was possible, otherwise return the
11154 simplified form of the call as a tree.
11156 The simplified form may be a constant or other expression which
11157 computes the same value, but in a more efficient manner (including
11158 calls to other builtin functions).
11160 The call may contain arguments which need to be evaluated, but
11161 which are not useful to determine the result of the call. In
11162 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11163 COMPOUND_EXPR will be an argument which must be evaluated.
11164 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11165 COMPOUND_EXPR in the chain will contain the tree for the simplified
11166 form of the builtin function call. */
11168 static tree
11169 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11171 if (!validate_arg (s1, POINTER_TYPE)
11172 || !validate_arg (s2, POINTER_TYPE))
11173 return NULL_TREE;
11174 else
11176 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11178 /* If both arguments are constants, evaluate at compile-time. */
11179 if (p1 && p2)
11181 const size_t r = strcspn (p1, p2);
11182 return build_int_cst (size_type_node, r);
11185 /* If the first argument is "", return NULL_TREE. */
11186 if (p1 && *p1 == '\0')
11188 /* Evaluate and ignore argument s2 in case it has
11189 side-effects. */
11190 return omit_one_operand_loc (loc, size_type_node,
11191 size_zero_node, s2);
11194 /* If the second argument is "", return __builtin_strlen(s1). */
11195 if (p2 && *p2 == '\0')
11197 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11199 /* If the replacement _DECL isn't initialized, don't do the
11200 transformation. */
11201 if (!fn)
11202 return NULL_TREE;
11204 return build_call_expr_loc (loc, fn, 1, s1);
11206 return NULL_TREE;
11210 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11211 produced. False otherwise. This is done so that we don't output the error
11212 or warning twice or three times. */
11214 bool
11215 fold_builtin_next_arg (tree exp, bool va_start_p)
11217 tree fntype = TREE_TYPE (current_function_decl);
11218 int nargs = call_expr_nargs (exp);
11219 tree arg;
11220 /* There is good chance the current input_location points inside the
11221 definition of the va_start macro (perhaps on the token for
11222 builtin) in a system header, so warnings will not be emitted.
11223 Use the location in real source code. */
11224 source_location current_location =
11225 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11226 NULL);
11228 if (!stdarg_p (fntype))
11230 error ("%<va_start%> used in function with fixed args");
11231 return true;
11234 if (va_start_p)
11236 if (va_start_p && (nargs != 2))
11238 error ("wrong number of arguments to function %<va_start%>");
11239 return true;
11241 arg = CALL_EXPR_ARG (exp, 1);
11243 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11244 when we checked the arguments and if needed issued a warning. */
11245 else
11247 if (nargs == 0)
11249 /* Evidently an out of date version of <stdarg.h>; can't validate
11250 va_start's second argument, but can still work as intended. */
11251 warning_at (current_location,
11252 OPT_Wvarargs,
11253 "%<__builtin_next_arg%> called without an argument");
11254 return true;
11256 else if (nargs > 1)
11258 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11259 return true;
11261 arg = CALL_EXPR_ARG (exp, 0);
11264 if (TREE_CODE (arg) == SSA_NAME)
11265 arg = SSA_NAME_VAR (arg);
11267 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11268 or __builtin_next_arg (0) the first time we see it, after checking
11269 the arguments and if needed issuing a warning. */
11270 if (!integer_zerop (arg))
11272 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11274 /* Strip off all nops for the sake of the comparison. This
11275 is not quite the same as STRIP_NOPS. It does more.
11276 We must also strip off INDIRECT_EXPR for C++ reference
11277 parameters. */
11278 while (CONVERT_EXPR_P (arg)
11279 || TREE_CODE (arg) == INDIRECT_REF)
11280 arg = TREE_OPERAND (arg, 0);
11281 if (arg != last_parm)
11283 /* FIXME: Sometimes with the tree optimizers we can get the
11284 not the last argument even though the user used the last
11285 argument. We just warn and set the arg to be the last
11286 argument so that we will get wrong-code because of
11287 it. */
11288 warning_at (current_location,
11289 OPT_Wvarargs,
11290 "second parameter of %<va_start%> not last named argument");
11293 /* Undefined by C99 7.15.1.4p4 (va_start):
11294 "If the parameter parmN is declared with the register storage
11295 class, with a function or array type, or with a type that is
11296 not compatible with the type that results after application of
11297 the default argument promotions, the behavior is undefined."
11299 else if (DECL_REGISTER (arg))
11301 warning_at (current_location,
11302 OPT_Wvarargs,
11303 "undefined behaviour when second parameter of "
11304 "%<va_start%> is declared with %<register%> storage");
11307 /* We want to verify the second parameter just once before the tree
11308 optimizers are run and then avoid keeping it in the tree,
11309 as otherwise we could warn even for correct code like:
11310 void foo (int i, ...)
11311 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11312 if (va_start_p)
11313 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11314 else
11315 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11317 return false;
11321 /* Expand a call EXP to __builtin_object_size. */
11323 static rtx
11324 expand_builtin_object_size (tree exp)
11326 tree ost;
11327 int object_size_type;
11328 tree fndecl = get_callee_fndecl (exp);
11330 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11332 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11333 exp, fndecl);
11334 expand_builtin_trap ();
11335 return const0_rtx;
11338 ost = CALL_EXPR_ARG (exp, 1);
11339 STRIP_NOPS (ost);
11341 if (TREE_CODE (ost) != INTEGER_CST
11342 || tree_int_cst_sgn (ost) < 0
11343 || compare_tree_int (ost, 3) > 0)
11345 error ("%Klast argument of %D is not integer constant between 0 and 3",
11346 exp, fndecl);
11347 expand_builtin_trap ();
11348 return const0_rtx;
11351 object_size_type = tree_to_shwi (ost);
11353 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11356 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11357 FCODE is the BUILT_IN_* to use.
11358 Return NULL_RTX if we failed; the caller should emit a normal call,
11359 otherwise try to get the result in TARGET, if convenient (and in
11360 mode MODE if that's convenient). */
11362 static rtx
11363 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11364 enum built_in_function fcode)
11366 tree dest, src, len, size;
11368 if (!validate_arglist (exp,
11369 POINTER_TYPE,
11370 fcode == BUILT_IN_MEMSET_CHK
11371 ? INTEGER_TYPE : POINTER_TYPE,
11372 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11373 return NULL_RTX;
11375 dest = CALL_EXPR_ARG (exp, 0);
11376 src = CALL_EXPR_ARG (exp, 1);
11377 len = CALL_EXPR_ARG (exp, 2);
11378 size = CALL_EXPR_ARG (exp, 3);
11380 if (! tree_fits_uhwi_p (size))
11381 return NULL_RTX;
11383 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11385 tree fn;
11387 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11389 warning_at (tree_nonartificial_location (exp),
11390 0, "%Kcall to %D will always overflow destination buffer",
11391 exp, get_callee_fndecl (exp));
11392 return NULL_RTX;
11395 fn = NULL_TREE;
11396 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11397 mem{cpy,pcpy,move,set} is available. */
11398 switch (fcode)
11400 case BUILT_IN_MEMCPY_CHK:
11401 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11402 break;
11403 case BUILT_IN_MEMPCPY_CHK:
11404 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11405 break;
11406 case BUILT_IN_MEMMOVE_CHK:
11407 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11408 break;
11409 case BUILT_IN_MEMSET_CHK:
11410 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11411 break;
11412 default:
11413 break;
11416 if (! fn)
11417 return NULL_RTX;
11419 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11420 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11421 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11422 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11424 else if (fcode == BUILT_IN_MEMSET_CHK)
11425 return NULL_RTX;
11426 else
11428 unsigned int dest_align = get_pointer_alignment (dest);
11430 /* If DEST is not a pointer type, call the normal function. */
11431 if (dest_align == 0)
11432 return NULL_RTX;
11434 /* If SRC and DEST are the same (and not volatile), do nothing. */
11435 if (operand_equal_p (src, dest, 0))
11437 tree expr;
11439 if (fcode != BUILT_IN_MEMPCPY_CHK)
11441 /* Evaluate and ignore LEN in case it has side-effects. */
11442 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11443 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11446 expr = fold_build_pointer_plus (dest, len);
11447 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11450 /* __memmove_chk special case. */
11451 if (fcode == BUILT_IN_MEMMOVE_CHK)
11453 unsigned int src_align = get_pointer_alignment (src);
11455 if (src_align == 0)
11456 return NULL_RTX;
11458 /* If src is categorized for a readonly section we can use
11459 normal __memcpy_chk. */
11460 if (readonly_data_expr (src))
11462 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11463 if (!fn)
11464 return NULL_RTX;
11465 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11466 dest, src, len, size);
11467 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11468 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11469 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11472 return NULL_RTX;
11476 /* Emit warning if a buffer overflow is detected at compile time. */
11478 static void
11479 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11481 int is_strlen = 0;
11482 tree len, size;
11483 location_t loc = tree_nonartificial_location (exp);
11485 switch (fcode)
11487 case BUILT_IN_STRCPY_CHK:
11488 case BUILT_IN_STPCPY_CHK:
11489 /* For __strcat_chk the warning will be emitted only if overflowing
11490 by at least strlen (dest) + 1 bytes. */
11491 case BUILT_IN_STRCAT_CHK:
11492 len = CALL_EXPR_ARG (exp, 1);
11493 size = CALL_EXPR_ARG (exp, 2);
11494 is_strlen = 1;
11495 break;
11496 case BUILT_IN_STRNCAT_CHK:
11497 case BUILT_IN_STRNCPY_CHK:
11498 case BUILT_IN_STPNCPY_CHK:
11499 len = CALL_EXPR_ARG (exp, 2);
11500 size = CALL_EXPR_ARG (exp, 3);
11501 break;
11502 case BUILT_IN_SNPRINTF_CHK:
11503 case BUILT_IN_VSNPRINTF_CHK:
11504 len = CALL_EXPR_ARG (exp, 1);
11505 size = CALL_EXPR_ARG (exp, 3);
11506 break;
11507 default:
11508 gcc_unreachable ();
11511 if (!len || !size)
11512 return;
11514 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11515 return;
11517 if (is_strlen)
11519 len = c_strlen (len, 1);
11520 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11521 return;
11523 else if (fcode == BUILT_IN_STRNCAT_CHK)
11525 tree src = CALL_EXPR_ARG (exp, 1);
11526 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11527 return;
11528 src = c_strlen (src, 1);
11529 if (! src || ! tree_fits_uhwi_p (src))
11531 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
11532 exp, get_callee_fndecl (exp));
11533 return;
11535 else if (tree_int_cst_lt (src, size))
11536 return;
11538 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
11539 return;
11541 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
11542 exp, get_callee_fndecl (exp));
11545 /* Emit warning if a buffer overflow is detected at compile time
11546 in __sprintf_chk/__vsprintf_chk calls. */
11548 static void
11549 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11551 tree size, len, fmt;
11552 const char *fmt_str;
11553 int nargs = call_expr_nargs (exp);
11555 /* Verify the required arguments in the original call. */
11557 if (nargs < 4)
11558 return;
11559 size = CALL_EXPR_ARG (exp, 2);
11560 fmt = CALL_EXPR_ARG (exp, 3);
11562 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11563 return;
11565 /* Check whether the format is a literal string constant. */
11566 fmt_str = c_getstr (fmt);
11567 if (fmt_str == NULL)
11568 return;
11570 if (!init_target_chars ())
11571 return;
11573 /* If the format doesn't contain % args or %%, we know its size. */
11574 if (strchr (fmt_str, target_percent) == 0)
11575 len = build_int_cstu (size_type_node, strlen (fmt_str));
11576 /* If the format is "%s" and first ... argument is a string literal,
11577 we know it too. */
11578 else if (fcode == BUILT_IN_SPRINTF_CHK
11579 && strcmp (fmt_str, target_percent_s) == 0)
11581 tree arg;
11583 if (nargs < 5)
11584 return;
11585 arg = CALL_EXPR_ARG (exp, 4);
11586 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11587 return;
11589 len = c_strlen (arg, 1);
11590 if (!len || ! tree_fits_uhwi_p (len))
11591 return;
11593 else
11594 return;
11596 if (! tree_int_cst_lt (len, size))
11597 warning_at (tree_nonartificial_location (exp),
11598 0, "%Kcall to %D will always overflow destination buffer",
11599 exp, get_callee_fndecl (exp));
11602 /* Emit warning if a free is called with address of a variable. */
11604 static void
11605 maybe_emit_free_warning (tree exp)
11607 tree arg = CALL_EXPR_ARG (exp, 0);
11609 STRIP_NOPS (arg);
11610 if (TREE_CODE (arg) != ADDR_EXPR)
11611 return;
11613 arg = get_base_address (TREE_OPERAND (arg, 0));
11614 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
11615 return;
11617 if (SSA_VAR_P (arg))
11618 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11619 "%Kattempt to free a non-heap object %qD", exp, arg);
11620 else
11621 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11622 "%Kattempt to free a non-heap object", exp);
11625 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11626 if possible. */
11628 static tree
11629 fold_builtin_object_size (tree ptr, tree ost)
11631 unsigned HOST_WIDE_INT bytes;
11632 int object_size_type;
11634 if (!validate_arg (ptr, POINTER_TYPE)
11635 || !validate_arg (ost, INTEGER_TYPE))
11636 return NULL_TREE;
11638 STRIP_NOPS (ost);
11640 if (TREE_CODE (ost) != INTEGER_CST
11641 || tree_int_cst_sgn (ost) < 0
11642 || compare_tree_int (ost, 3) > 0)
11643 return NULL_TREE;
11645 object_size_type = tree_to_shwi (ost);
11647 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11648 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11649 and (size_t) 0 for types 2 and 3. */
11650 if (TREE_SIDE_EFFECTS (ptr))
11651 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11653 if (TREE_CODE (ptr) == ADDR_EXPR)
11655 bytes = compute_builtin_object_size (ptr, object_size_type);
11656 if (wi::fits_to_tree_p (bytes, size_type_node))
11657 return build_int_cstu (size_type_node, bytes);
11659 else if (TREE_CODE (ptr) == SSA_NAME)
11661 /* If object size is not known yet, delay folding until
11662 later. Maybe subsequent passes will help determining
11663 it. */
11664 bytes = compute_builtin_object_size (ptr, object_size_type);
11665 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
11666 && wi::fits_to_tree_p (bytes, size_type_node))
11667 return build_int_cstu (size_type_node, bytes);
11670 return NULL_TREE;
11673 /* Builtins with folding operations that operate on "..." arguments
11674 need special handling; we need to store the arguments in a convenient
11675 data structure before attempting any folding. Fortunately there are
11676 only a few builtins that fall into this category. FNDECL is the
11677 function, EXP is the CALL_EXPR for the call. */
11679 static tree
11680 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
11682 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11683 tree ret = NULL_TREE;
11685 switch (fcode)
11687 case BUILT_IN_FPCLASSIFY:
11688 ret = fold_builtin_fpclassify (loc, args, nargs);
11689 break;
11691 default:
11692 break;
11694 if (ret)
11696 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11697 SET_EXPR_LOCATION (ret, loc);
11698 TREE_NO_WARNING (ret) = 1;
11699 return ret;
11701 return NULL_TREE;
11704 /* Initialize format string characters in the target charset. */
11706 bool
11707 init_target_chars (void)
11709 static bool init;
11710 if (!init)
11712 target_newline = lang_hooks.to_target_charset ('\n');
11713 target_percent = lang_hooks.to_target_charset ('%');
11714 target_c = lang_hooks.to_target_charset ('c');
11715 target_s = lang_hooks.to_target_charset ('s');
11716 if (target_newline == 0 || target_percent == 0 || target_c == 0
11717 || target_s == 0)
11718 return false;
11720 target_percent_c[0] = target_percent;
11721 target_percent_c[1] = target_c;
11722 target_percent_c[2] = '\0';
11724 target_percent_s[0] = target_percent;
11725 target_percent_s[1] = target_s;
11726 target_percent_s[2] = '\0';
11728 target_percent_s_newline[0] = target_percent;
11729 target_percent_s_newline[1] = target_s;
11730 target_percent_s_newline[2] = target_newline;
11731 target_percent_s_newline[3] = '\0';
11733 init = true;
11735 return true;
11738 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11739 and no overflow/underflow occurred. INEXACT is true if M was not
11740 exactly calculated. TYPE is the tree type for the result. This
11741 function assumes that you cleared the MPFR flags and then
11742 calculated M to see if anything subsequently set a flag prior to
11743 entering this function. Return NULL_TREE if any checks fail. */
11745 static tree
11746 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
11748 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11749 overflow/underflow occurred. If -frounding-math, proceed iff the
11750 result of calling FUNC was exact. */
11751 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11752 && (!flag_rounding_math || !inexact))
11754 REAL_VALUE_TYPE rr;
11756 real_from_mpfr (&rr, m, type, GMP_RNDN);
11757 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11758 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11759 but the mpft_t is not, then we underflowed in the
11760 conversion. */
11761 if (real_isfinite (&rr)
11762 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11764 REAL_VALUE_TYPE rmode;
11766 real_convert (&rmode, TYPE_MODE (type), &rr);
11767 /* Proceed iff the specified mode can hold the value. */
11768 if (real_identical (&rmode, &rr))
11769 return build_real (type, rmode);
11772 return NULL_TREE;
11775 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
11776 number and no overflow/underflow occurred. INEXACT is true if M
11777 was not exactly calculated. TYPE is the tree type for the result.
11778 This function assumes that you cleared the MPFR flags and then
11779 calculated M to see if anything subsequently set a flag prior to
11780 entering this function. Return NULL_TREE if any checks fail, if
11781 FORCE_CONVERT is true, then bypass the checks. */
11783 static tree
11784 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
11786 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11787 overflow/underflow occurred. If -frounding-math, proceed iff the
11788 result of calling FUNC was exact. */
11789 if (force_convert
11790 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
11791 && !mpfr_overflow_p () && !mpfr_underflow_p ()
11792 && (!flag_rounding_math || !inexact)))
11794 REAL_VALUE_TYPE re, im;
11796 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
11797 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
11798 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11799 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11800 but the mpft_t is not, then we underflowed in the
11801 conversion. */
11802 if (force_convert
11803 || (real_isfinite (&re) && real_isfinite (&im)
11804 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
11805 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
11807 REAL_VALUE_TYPE re_mode, im_mode;
11809 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
11810 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
11811 /* Proceed iff the specified mode can hold the value. */
11812 if (force_convert
11813 || (real_identical (&re_mode, &re)
11814 && real_identical (&im_mode, &im)))
11815 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
11816 build_real (TREE_TYPE (type), im_mode));
11819 return NULL_TREE;
11822 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11823 FUNC on it and return the resulting value as a tree with type TYPE.
11824 If MIN and/or MAX are not NULL, then the supplied ARG must be
11825 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11826 acceptable values, otherwise they are not. The mpfr precision is
11827 set to the precision of TYPE. We assume that function FUNC returns
11828 zero if the result could be calculated exactly within the requested
11829 precision. */
11831 static tree
11832 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11833 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11834 bool inclusive)
11836 tree result = NULL_TREE;
11838 STRIP_NOPS (arg);
11840 /* To proceed, MPFR must exactly represent the target floating point
11841 format, which only happens when the target base equals two. */
11842 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11843 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
11845 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11847 if (real_isfinite (ra)
11848 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
11849 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
11851 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11852 const int prec = fmt->p;
11853 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11854 int inexact;
11855 mpfr_t m;
11857 mpfr_init2 (m, prec);
11858 mpfr_from_real (m, ra, GMP_RNDN);
11859 mpfr_clear_flags ();
11860 inexact = func (m, m, rnd);
11861 result = do_mpfr_ckconv (m, type, inexact);
11862 mpfr_clear (m);
11866 return result;
11869 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11870 FUNC on it and return the resulting value as a tree with type TYPE.
11871 The mpfr precision is set to the precision of TYPE. We assume that
11872 function FUNC returns zero if the result could be calculated
11873 exactly within the requested precision. */
11875 static tree
11876 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
11877 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11879 tree result = NULL_TREE;
11881 STRIP_NOPS (arg1);
11882 STRIP_NOPS (arg2);
11884 /* To proceed, MPFR must exactly represent the target floating point
11885 format, which only happens when the target base equals two. */
11886 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11887 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11888 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
11890 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11891 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11893 if (real_isfinite (ra1) && real_isfinite (ra2))
11895 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11896 const int prec = fmt->p;
11897 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11898 int inexact;
11899 mpfr_t m1, m2;
11901 mpfr_inits2 (prec, m1, m2, NULL);
11902 mpfr_from_real (m1, ra1, GMP_RNDN);
11903 mpfr_from_real (m2, ra2, GMP_RNDN);
11904 mpfr_clear_flags ();
11905 inexact = func (m1, m1, m2, rnd);
11906 result = do_mpfr_ckconv (m1, type, inexact);
11907 mpfr_clears (m1, m2, NULL);
11911 return result;
11914 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
11915 FUNC on it and return the resulting value as a tree with type TYPE.
11916 The mpfr precision is set to the precision of TYPE. We assume that
11917 function FUNC returns zero if the result could be calculated
11918 exactly within the requested precision. */
11920 static tree
11921 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
11922 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11924 tree result = NULL_TREE;
11926 STRIP_NOPS (arg1);
11927 STRIP_NOPS (arg2);
11928 STRIP_NOPS (arg3);
11930 /* To proceed, MPFR must exactly represent the target floating point
11931 format, which only happens when the target base equals two. */
11932 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11933 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11934 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
11935 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
11937 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11938 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11939 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
11941 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
11943 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11944 const int prec = fmt->p;
11945 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11946 int inexact;
11947 mpfr_t m1, m2, m3;
11949 mpfr_inits2 (prec, m1, m2, m3, NULL);
11950 mpfr_from_real (m1, ra1, GMP_RNDN);
11951 mpfr_from_real (m2, ra2, GMP_RNDN);
11952 mpfr_from_real (m3, ra3, GMP_RNDN);
11953 mpfr_clear_flags ();
11954 inexact = func (m1, m1, m2, m3, rnd);
11955 result = do_mpfr_ckconv (m1, type, inexact);
11956 mpfr_clears (m1, m2, m3, NULL);
11960 return result;
11963 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11964 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11965 If ARG_SINP and ARG_COSP are NULL then the result is returned
11966 as a complex value.
11967 The type is taken from the type of ARG and is used for setting the
11968 precision of the calculation and results. */
11970 static tree
11971 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
11973 tree const type = TREE_TYPE (arg);
11974 tree result = NULL_TREE;
11976 STRIP_NOPS (arg);
11978 /* To proceed, MPFR must exactly represent the target floating point
11979 format, which only happens when the target base equals two. */
11980 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11981 && TREE_CODE (arg) == REAL_CST
11982 && !TREE_OVERFLOW (arg))
11984 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11986 if (real_isfinite (ra))
11988 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11989 const int prec = fmt->p;
11990 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11991 tree result_s, result_c;
11992 int inexact;
11993 mpfr_t m, ms, mc;
11995 mpfr_inits2 (prec, m, ms, mc, NULL);
11996 mpfr_from_real (m, ra, GMP_RNDN);
11997 mpfr_clear_flags ();
11998 inexact = mpfr_sin_cos (ms, mc, m, rnd);
11999 result_s = do_mpfr_ckconv (ms, type, inexact);
12000 result_c = do_mpfr_ckconv (mc, type, inexact);
12001 mpfr_clears (m, ms, mc, NULL);
12002 if (result_s && result_c)
12004 /* If we are to return in a complex value do so. */
12005 if (!arg_sinp && !arg_cosp)
12006 return build_complex (build_complex_type (type),
12007 result_c, result_s);
12009 /* Dereference the sin/cos pointer arguments. */
12010 arg_sinp = build_fold_indirect_ref (arg_sinp);
12011 arg_cosp = build_fold_indirect_ref (arg_cosp);
12012 /* Proceed if valid pointer type were passed in. */
12013 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
12014 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
12016 /* Set the values. */
12017 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
12018 result_s);
12019 TREE_SIDE_EFFECTS (result_s) = 1;
12020 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
12021 result_c);
12022 TREE_SIDE_EFFECTS (result_c) = 1;
12023 /* Combine the assignments into a compound expr. */
12024 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12025 result_s, result_c));
12030 return result;
12033 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
12034 two-argument mpfr order N Bessel function FUNC on them and return
12035 the resulting value as a tree with type TYPE. The mpfr precision
12036 is set to the precision of TYPE. We assume that function FUNC
12037 returns zero if the result could be calculated exactly within the
12038 requested precision. */
12039 static tree
12040 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
12041 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
12042 const REAL_VALUE_TYPE *min, bool inclusive)
12044 tree result = NULL_TREE;
12046 STRIP_NOPS (arg1);
12047 STRIP_NOPS (arg2);
12049 /* To proceed, MPFR must exactly represent the target floating point
12050 format, which only happens when the target base equals two. */
12051 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12052 && tree_fits_shwi_p (arg1)
12053 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12055 const HOST_WIDE_INT n = tree_to_shwi (arg1);
12056 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
12058 if (n == (long)n
12059 && real_isfinite (ra)
12060 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
12062 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12063 const int prec = fmt->p;
12064 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12065 int inexact;
12066 mpfr_t m;
12068 mpfr_init2 (m, prec);
12069 mpfr_from_real (m, ra, GMP_RNDN);
12070 mpfr_clear_flags ();
12071 inexact = func (m, n, m, rnd);
12072 result = do_mpfr_ckconv (m, type, inexact);
12073 mpfr_clear (m);
12077 return result;
12080 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12081 the pointer *(ARG_QUO) and return the result. The type is taken
12082 from the type of ARG0 and is used for setting the precision of the
12083 calculation and results. */
12085 static tree
12086 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
12088 tree const type = TREE_TYPE (arg0);
12089 tree result = NULL_TREE;
12091 STRIP_NOPS (arg0);
12092 STRIP_NOPS (arg1);
12094 /* To proceed, MPFR must exactly represent the target floating point
12095 format, which only happens when the target base equals two. */
12096 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12097 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12098 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12100 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12101 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12103 if (real_isfinite (ra0) && real_isfinite (ra1))
12105 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12106 const int prec = fmt->p;
12107 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12108 tree result_rem;
12109 long integer_quo;
12110 mpfr_t m0, m1;
12112 mpfr_inits2 (prec, m0, m1, NULL);
12113 mpfr_from_real (m0, ra0, GMP_RNDN);
12114 mpfr_from_real (m1, ra1, GMP_RNDN);
12115 mpfr_clear_flags ();
12116 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12117 /* Remquo is independent of the rounding mode, so pass
12118 inexact=0 to do_mpfr_ckconv(). */
12119 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12120 mpfr_clears (m0, m1, NULL);
12121 if (result_rem)
12123 /* MPFR calculates quo in the host's long so it may
12124 return more bits in quo than the target int can hold
12125 if sizeof(host long) > sizeof(target int). This can
12126 happen even for native compilers in LP64 mode. In
12127 these cases, modulo the quo value with the largest
12128 number that the target int can hold while leaving one
12129 bit for the sign. */
12130 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12131 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12133 /* Dereference the quo pointer argument. */
12134 arg_quo = build_fold_indirect_ref (arg_quo);
12135 /* Proceed iff a valid pointer type was passed in. */
12136 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12138 /* Set the value. */
12139 tree result_quo
12140 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12141 build_int_cst (TREE_TYPE (arg_quo),
12142 integer_quo));
12143 TREE_SIDE_EFFECTS (result_quo) = 1;
12144 /* Combine the quo assignment with the rem. */
12145 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12146 result_quo, result_rem));
12151 return result;
12154 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12155 resulting value as a tree with type TYPE. The mpfr precision is
12156 set to the precision of TYPE. We assume that this mpfr function
12157 returns zero if the result could be calculated exactly within the
12158 requested precision. In addition, the integer pointer represented
12159 by ARG_SG will be dereferenced and set to the appropriate signgam
12160 (-1,1) value. */
12162 static tree
12163 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12165 tree result = NULL_TREE;
12167 STRIP_NOPS (arg);
12169 /* To proceed, MPFR must exactly represent the target floating point
12170 format, which only happens when the target base equals two. Also
12171 verify ARG is a constant and that ARG_SG is an int pointer. */
12172 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12173 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12174 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12175 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12177 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12179 /* In addition to NaN and Inf, the argument cannot be zero or a
12180 negative integer. */
12181 if (real_isfinite (ra)
12182 && ra->cl != rvc_zero
12183 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12185 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12186 const int prec = fmt->p;
12187 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12188 int inexact, sg;
12189 mpfr_t m;
12190 tree result_lg;
12192 mpfr_init2 (m, prec);
12193 mpfr_from_real (m, ra, GMP_RNDN);
12194 mpfr_clear_flags ();
12195 inexact = mpfr_lgamma (m, &sg, m, rnd);
12196 result_lg = do_mpfr_ckconv (m, type, inexact);
12197 mpfr_clear (m);
12198 if (result_lg)
12200 tree result_sg;
12202 /* Dereference the arg_sg pointer argument. */
12203 arg_sg = build_fold_indirect_ref (arg_sg);
12204 /* Assign the signgam value into *arg_sg. */
12205 result_sg = fold_build2 (MODIFY_EXPR,
12206 TREE_TYPE (arg_sg), arg_sg,
12207 build_int_cst (TREE_TYPE (arg_sg), sg));
12208 TREE_SIDE_EFFECTS (result_sg) = 1;
12209 /* Combine the signgam assignment with the lgamma result. */
12210 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12211 result_sg, result_lg));
12216 return result;
12219 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
12220 function FUNC on it and return the resulting value as a tree with
12221 type TYPE. The mpfr precision is set to the precision of TYPE. We
12222 assume that function FUNC returns zero if the result could be
12223 calculated exactly within the requested precision. */
12225 static tree
12226 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
12228 tree result = NULL_TREE;
12230 STRIP_NOPS (arg);
12232 /* To proceed, MPFR must exactly represent the target floating point
12233 format, which only happens when the target base equals two. */
12234 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
12235 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
12236 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
12238 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
12239 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
12241 if (real_isfinite (re) && real_isfinite (im))
12243 const struct real_format *const fmt =
12244 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12245 const int prec = fmt->p;
12246 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12247 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12248 int inexact;
12249 mpc_t m;
12251 mpc_init2 (m, prec);
12252 mpfr_from_real (mpc_realref (m), re, rnd);
12253 mpfr_from_real (mpc_imagref (m), im, rnd);
12254 mpfr_clear_flags ();
12255 inexact = func (m, m, crnd);
12256 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
12257 mpc_clear (m);
12261 return result;
12264 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12265 mpc function FUNC on it and return the resulting value as a tree
12266 with type TYPE. The mpfr precision is set to the precision of
12267 TYPE. We assume that function FUNC returns zero if the result
12268 could be calculated exactly within the requested precision. If
12269 DO_NONFINITE is true, then fold expressions containing Inf or NaN
12270 in the arguments and/or results. */
12272 tree
12273 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12274 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12276 tree result = NULL_TREE;
12278 STRIP_NOPS (arg0);
12279 STRIP_NOPS (arg1);
12281 /* To proceed, MPFR must exactly represent the target floating point
12282 format, which only happens when the target base equals two. */
12283 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12284 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
12285 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12286 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
12287 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12289 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12290 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12291 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12292 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12294 if (do_nonfinite
12295 || (real_isfinite (re0) && real_isfinite (im0)
12296 && real_isfinite (re1) && real_isfinite (im1)))
12298 const struct real_format *const fmt =
12299 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12300 const int prec = fmt->p;
12301 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12302 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12303 int inexact;
12304 mpc_t m0, m1;
12306 mpc_init2 (m0, prec);
12307 mpc_init2 (m1, prec);
12308 mpfr_from_real (mpc_realref (m0), re0, rnd);
12309 mpfr_from_real (mpc_imagref (m0), im0, rnd);
12310 mpfr_from_real (mpc_realref (m1), re1, rnd);
12311 mpfr_from_real (mpc_imagref (m1), im1, rnd);
12312 mpfr_clear_flags ();
12313 inexact = func (m0, m0, m1, crnd);
12314 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12315 mpc_clear (m0);
12316 mpc_clear (m1);
12320 return result;
12323 /* A wrapper function for builtin folding that prevents warnings for
12324 "statement without effect" and the like, caused by removing the
12325 call node earlier than the warning is generated. */
12327 tree
12328 fold_call_stmt (gcall *stmt, bool ignore)
12330 tree ret = NULL_TREE;
12331 tree fndecl = gimple_call_fndecl (stmt);
12332 location_t loc = gimple_location (stmt);
12333 if (fndecl
12334 && TREE_CODE (fndecl) == FUNCTION_DECL
12335 && DECL_BUILT_IN (fndecl)
12336 && !gimple_call_va_arg_pack_p (stmt))
12338 int nargs = gimple_call_num_args (stmt);
12339 tree *args = (nargs > 0
12340 ? gimple_call_arg_ptr (stmt, 0)
12341 : &error_mark_node);
12343 if (avoid_folding_inline_builtin (fndecl))
12344 return NULL_TREE;
12345 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12347 return targetm.fold_builtin (fndecl, nargs, args, ignore);
12349 else
12351 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
12352 if (ret)
12354 /* Propagate location information from original call to
12355 expansion of builtin. Otherwise things like
12356 maybe_emit_chk_warning, that operate on the expansion
12357 of a builtin, will use the wrong location information. */
12358 if (gimple_has_location (stmt))
12360 tree realret = ret;
12361 if (TREE_CODE (ret) == NOP_EXPR)
12362 realret = TREE_OPERAND (ret, 0);
12363 if (CAN_HAVE_LOCATION_P (realret)
12364 && !EXPR_HAS_LOCATION (realret))
12365 SET_EXPR_LOCATION (realret, loc);
12366 return realret;
12368 return ret;
12372 return NULL_TREE;
12375 /* Look up the function in builtin_decl that corresponds to DECL
12376 and set ASMSPEC as its user assembler name. DECL must be a
12377 function decl that declares a builtin. */
12379 void
12380 set_builtin_user_assembler_name (tree decl, const char *asmspec)
12382 tree builtin;
12383 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
12384 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
12385 && asmspec != 0);
12387 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12388 set_user_assembler_name (builtin, asmspec);
12389 switch (DECL_FUNCTION_CODE (decl))
12391 case BUILT_IN_MEMCPY:
12392 init_block_move_fn (asmspec);
12393 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
12394 break;
12395 case BUILT_IN_MEMSET:
12396 init_block_clear_fn (asmspec);
12397 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
12398 break;
12399 case BUILT_IN_MEMMOVE:
12400 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
12401 break;
12402 case BUILT_IN_MEMCMP:
12403 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
12404 break;
12405 case BUILT_IN_ABORT:
12406 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
12407 break;
12408 case BUILT_IN_FFS:
12409 if (INT_TYPE_SIZE < BITS_PER_WORD)
12411 set_user_assembler_libfunc ("ffs", asmspec);
12412 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
12413 MODE_INT, 0), "ffs");
12415 break;
12416 default:
12417 break;
12421 /* Return true if DECL is a builtin that expands to a constant or similarly
12422 simple code. */
12423 bool
12424 is_simple_builtin (tree decl)
12426 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12427 switch (DECL_FUNCTION_CODE (decl))
12429 /* Builtins that expand to constants. */
12430 case BUILT_IN_CONSTANT_P:
12431 case BUILT_IN_EXPECT:
12432 case BUILT_IN_OBJECT_SIZE:
12433 case BUILT_IN_UNREACHABLE:
12434 /* Simple register moves or loads from stack. */
12435 case BUILT_IN_ASSUME_ALIGNED:
12436 case BUILT_IN_RETURN_ADDRESS:
12437 case BUILT_IN_EXTRACT_RETURN_ADDR:
12438 case BUILT_IN_FROB_RETURN_ADDR:
12439 case BUILT_IN_RETURN:
12440 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12441 case BUILT_IN_FRAME_ADDRESS:
12442 case BUILT_IN_VA_END:
12443 case BUILT_IN_STACK_SAVE:
12444 case BUILT_IN_STACK_RESTORE:
12445 /* Exception state returns or moves registers around. */
12446 case BUILT_IN_EH_FILTER:
12447 case BUILT_IN_EH_POINTER:
12448 case BUILT_IN_EH_COPY_VALUES:
12449 return true;
12451 default:
12452 return false;
12455 return false;
12458 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12459 most probably expanded inline into reasonably simple code. This is a
12460 superset of is_simple_builtin. */
12461 bool
12462 is_inexpensive_builtin (tree decl)
12464 if (!decl)
12465 return false;
12466 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12467 return true;
12468 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12469 switch (DECL_FUNCTION_CODE (decl))
12471 case BUILT_IN_ABS:
12472 case BUILT_IN_ALLOCA:
12473 case BUILT_IN_ALLOCA_WITH_ALIGN:
12474 case BUILT_IN_BSWAP16:
12475 case BUILT_IN_BSWAP32:
12476 case BUILT_IN_BSWAP64:
12477 case BUILT_IN_CLZ:
12478 case BUILT_IN_CLZIMAX:
12479 case BUILT_IN_CLZL:
12480 case BUILT_IN_CLZLL:
12481 case BUILT_IN_CTZ:
12482 case BUILT_IN_CTZIMAX:
12483 case BUILT_IN_CTZL:
12484 case BUILT_IN_CTZLL:
12485 case BUILT_IN_FFS:
12486 case BUILT_IN_FFSIMAX:
12487 case BUILT_IN_FFSL:
12488 case BUILT_IN_FFSLL:
12489 case BUILT_IN_IMAXABS:
12490 case BUILT_IN_FINITE:
12491 case BUILT_IN_FINITEF:
12492 case BUILT_IN_FINITEL:
12493 case BUILT_IN_FINITED32:
12494 case BUILT_IN_FINITED64:
12495 case BUILT_IN_FINITED128:
12496 case BUILT_IN_FPCLASSIFY:
12497 case BUILT_IN_ISFINITE:
12498 case BUILT_IN_ISINF_SIGN:
12499 case BUILT_IN_ISINF:
12500 case BUILT_IN_ISINFF:
12501 case BUILT_IN_ISINFL:
12502 case BUILT_IN_ISINFD32:
12503 case BUILT_IN_ISINFD64:
12504 case BUILT_IN_ISINFD128:
12505 case BUILT_IN_ISNAN:
12506 case BUILT_IN_ISNANF:
12507 case BUILT_IN_ISNANL:
12508 case BUILT_IN_ISNAND32:
12509 case BUILT_IN_ISNAND64:
12510 case BUILT_IN_ISNAND128:
12511 case BUILT_IN_ISNORMAL:
12512 case BUILT_IN_ISGREATER:
12513 case BUILT_IN_ISGREATEREQUAL:
12514 case BUILT_IN_ISLESS:
12515 case BUILT_IN_ISLESSEQUAL:
12516 case BUILT_IN_ISLESSGREATER:
12517 case BUILT_IN_ISUNORDERED:
12518 case BUILT_IN_VA_ARG_PACK:
12519 case BUILT_IN_VA_ARG_PACK_LEN:
12520 case BUILT_IN_VA_COPY:
12521 case BUILT_IN_TRAP:
12522 case BUILT_IN_SAVEREGS:
12523 case BUILT_IN_POPCOUNTL:
12524 case BUILT_IN_POPCOUNTLL:
12525 case BUILT_IN_POPCOUNTIMAX:
12526 case BUILT_IN_POPCOUNT:
12527 case BUILT_IN_PARITYL:
12528 case BUILT_IN_PARITYLL:
12529 case BUILT_IN_PARITYIMAX:
12530 case BUILT_IN_PARITY:
12531 case BUILT_IN_LABS:
12532 case BUILT_IN_LLABS:
12533 case BUILT_IN_PREFETCH:
12534 case BUILT_IN_ACC_ON_DEVICE:
12535 return true;
12537 default:
12538 return is_simple_builtin (decl);
12541 return false;