2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / builtins.c
blobe8fe3db55177391b3f70c098da8b4d0ab66640f3
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 "rtl.h"
25 #include "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "stringpool.h"
31 #include "stor-layout.h"
32 #include "calls.h"
33 #include "varasm.h"
34 #include "tree-object-size.h"
35 #include "realmpfr.h"
36 #include "predict.h"
37 #include "hard-reg-set.h"
38 #include "function.h"
39 #include "cfgrtl.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "flags.h"
47 #include "regs.h"
48 #include "except.h"
49 #include "insn-config.h"
50 #include "expmed.h"
51 #include "dojump.h"
52 #include "explow.h"
53 #include "emit-rtl.h"
54 #include "stmt.h"
55 #include "expr.h"
56 #include "insn-codes.h"
57 #include "optabs.h"
58 #include "libfuncs.h"
59 #include "recog.h"
60 #include "output.h"
61 #include "typeclass.h"
62 #include "tm_p.h"
63 #include "target.h"
64 #include "langhooks.h"
65 #include "tree-ssanames.h"
66 #include "tree-dfa.h"
67 #include "value-prof.h"
68 #include "diagnostic-core.h"
69 #include "builtins.h"
70 #include "asan.h"
71 #include "cilk.h"
72 #include "ipa-ref.h"
73 #include "lto-streamer.h"
74 #include "cgraph.h"
75 #include "tree-chkp.h"
76 #include "rtl-chkp.h"
77 #include "gomp-constants.h"
80 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
82 struct target_builtins default_target_builtins;
83 #if SWITCHABLE_TARGET
84 struct target_builtins *this_target_builtins = &default_target_builtins;
85 #endif
87 /* Define the names of the builtin function types and codes. */
88 const char *const built_in_class_names[BUILT_IN_LAST]
89 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
91 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
92 const char * built_in_names[(int) END_BUILTINS] =
94 #include "builtins.def"
96 #undef DEF_BUILTIN
98 /* Setup an array of builtin_info_type, make sure each element decl is
99 initialized to NULL_TREE. */
100 builtin_info_type builtin_info[(int)END_BUILTINS];
102 /* Non-zero if __builtin_constant_p should be folded right away. */
103 bool force_folding_builtin_constant_p;
105 static rtx c_readstr (const char *, machine_mode);
106 static int target_char_cast (tree, char *);
107 static rtx get_memory_rtx (tree, tree);
108 static int apply_args_size (void);
109 static int apply_result_size (void);
110 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
111 static rtx result_vector (int, rtx);
112 #endif
113 static void expand_builtin_prefetch (tree);
114 static rtx expand_builtin_apply_args (void);
115 static rtx expand_builtin_apply_args_1 (void);
116 static rtx expand_builtin_apply (rtx, rtx, rtx);
117 static void expand_builtin_return (rtx);
118 static enum type_class type_to_class (tree);
119 static rtx expand_builtin_classify_type (tree);
120 static void expand_errno_check (tree, rtx);
121 static rtx expand_builtin_mathfn (tree, rtx, rtx);
122 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
123 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
124 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
125 static rtx expand_builtin_interclass_mathfn (tree, rtx);
126 static rtx expand_builtin_sincos (tree);
127 static rtx expand_builtin_cexpi (tree, rtx);
128 static rtx expand_builtin_int_roundingfn (tree, rtx);
129 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
130 static rtx expand_builtin_next_arg (void);
131 static rtx expand_builtin_va_start (tree);
132 static rtx expand_builtin_va_end (tree);
133 static rtx expand_builtin_va_copy (tree);
134 static rtx expand_builtin_memcmp (tree, rtx, machine_mode);
135 static rtx expand_builtin_strcmp (tree, rtx);
136 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
137 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, machine_mode);
138 static rtx expand_builtin_memcpy (tree, rtx);
139 static rtx expand_builtin_memcpy_with_bounds (tree, rtx);
140 static rtx expand_builtin_memcpy_args (tree, tree, tree, rtx, tree);
141 static rtx expand_builtin_mempcpy (tree, rtx, machine_mode);
142 static rtx expand_builtin_mempcpy_with_bounds (tree, rtx, machine_mode);
143 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
144 machine_mode, int, tree);
145 static rtx expand_builtin_strcpy (tree, rtx);
146 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
147 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
148 static rtx expand_builtin_strncpy (tree, rtx);
149 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, machine_mode);
150 static rtx expand_builtin_memset (tree, rtx, machine_mode);
151 static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode);
152 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
153 static rtx expand_builtin_bzero (tree);
154 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
155 static rtx expand_builtin_alloca (tree, bool);
156 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
157 static rtx expand_builtin_frame_address (tree, tree);
158 static tree stabilize_va_list_loc (location_t, tree, int);
159 static rtx expand_builtin_expect (tree, rtx);
160 static tree fold_builtin_constant_p (tree);
161 static tree fold_builtin_classify_type (tree);
162 static tree fold_builtin_strlen (location_t, tree, tree);
163 static tree fold_builtin_inf (location_t, tree, int);
164 static tree fold_builtin_nan (tree, tree, int);
165 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
166 static bool validate_arg (const_tree, enum tree_code code);
167 static bool integer_valued_real_p (tree);
168 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
169 static rtx expand_builtin_fabs (tree, rtx, rtx);
170 static rtx expand_builtin_signbit (tree, rtx);
171 static tree fold_builtin_sqrt (location_t, tree, tree);
172 static tree fold_builtin_cbrt (location_t, tree, tree);
173 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
174 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
175 static tree fold_builtin_cos (location_t, tree, tree, tree);
176 static tree fold_builtin_cosh (location_t, tree, tree, tree);
177 static tree fold_builtin_tan (tree, tree);
178 static tree fold_builtin_trunc (location_t, tree, tree);
179 static tree fold_builtin_floor (location_t, tree, tree);
180 static tree fold_builtin_ceil (location_t, tree, tree);
181 static tree fold_builtin_round (location_t, tree, tree);
182 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
183 static tree fold_builtin_bitop (tree, tree);
184 static tree fold_builtin_strchr (location_t, tree, tree, tree);
185 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
186 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
187 static tree fold_builtin_strcmp (location_t, tree, tree);
188 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
189 static tree fold_builtin_signbit (location_t, tree, tree);
190 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
191 static tree fold_builtin_isascii (location_t, tree);
192 static tree fold_builtin_toascii (location_t, tree);
193 static tree fold_builtin_isdigit (location_t, tree);
194 static tree fold_builtin_fabs (location_t, tree, tree);
195 static tree fold_builtin_abs (location_t, tree, tree);
196 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
197 enum tree_code);
198 static tree fold_builtin_0 (location_t, tree);
199 static tree fold_builtin_1 (location_t, tree, tree);
200 static tree fold_builtin_2 (location_t, tree, tree, tree);
201 static tree fold_builtin_3 (location_t, tree, tree, tree, tree);
202 static tree fold_builtin_varargs (location_t, tree, tree*, int);
204 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
205 static tree fold_builtin_strstr (location_t, tree, tree, tree);
206 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
207 static tree fold_builtin_strspn (location_t, tree, tree);
208 static tree fold_builtin_strcspn (location_t, tree, tree);
210 static rtx expand_builtin_object_size (tree);
211 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
212 enum built_in_function);
213 static void maybe_emit_chk_warning (tree, enum built_in_function);
214 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
215 static void maybe_emit_free_warning (tree);
216 static tree fold_builtin_object_size (tree, tree);
218 unsigned HOST_WIDE_INT target_newline;
219 unsigned HOST_WIDE_INT target_percent;
220 static unsigned HOST_WIDE_INT target_c;
221 static unsigned HOST_WIDE_INT target_s;
222 char target_percent_c[3];
223 char target_percent_s[3];
224 char target_percent_s_newline[4];
225 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
226 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
227 static tree do_mpfr_arg2 (tree, tree, tree,
228 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
229 static tree do_mpfr_arg3 (tree, tree, tree, tree,
230 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
231 static tree do_mpfr_sincos (tree, tree, tree);
232 static tree do_mpfr_bessel_n (tree, tree, tree,
233 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
234 const REAL_VALUE_TYPE *, bool);
235 static tree do_mpfr_remquo (tree, tree, tree);
236 static tree do_mpfr_lgamma_r (tree, tree, tree);
237 static void expand_builtin_sync_synchronize (void);
239 /* Return true if NAME starts with __builtin_ or __sync_. */
241 static bool
242 is_builtin_name (const char *name)
244 if (strncmp (name, "__builtin_", 10) == 0)
245 return true;
246 if (strncmp (name, "__sync_", 7) == 0)
247 return true;
248 if (strncmp (name, "__atomic_", 9) == 0)
249 return true;
250 if (flag_cilkplus
251 && (!strcmp (name, "__cilkrts_detach")
252 || !strcmp (name, "__cilkrts_pop_frame")))
253 return true;
254 return false;
258 /* Return true if DECL is a function symbol representing a built-in. */
260 bool
261 is_builtin_fn (tree decl)
263 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
266 /* Return true if NODE should be considered for inline expansion regardless
267 of the optimization level. This means whenever a function is invoked with
268 its "internal" name, which normally contains the prefix "__builtin". */
270 static bool
271 called_as_built_in (tree node)
273 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
274 we want the name used to call the function, not the name it
275 will have. */
276 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
277 return is_builtin_name (name);
280 /* Compute values M and N such that M divides (address of EXP - N) and such
281 that N < M. If these numbers can be determined, store M in alignp and N in
282 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
283 *alignp and any bit-offset to *bitposp.
285 Note that the address (and thus the alignment) computed here is based
286 on the address to which a symbol resolves, whereas DECL_ALIGN is based
287 on the address at which an object is actually located. These two
288 addresses are not always the same. For example, on ARM targets,
289 the address &foo of a Thumb function foo() has the lowest bit set,
290 whereas foo() itself starts on an even address.
292 If ADDR_P is true we are taking the address of the memory reference EXP
293 and thus cannot rely on the access taking place. */
295 static bool
296 get_object_alignment_2 (tree exp, unsigned int *alignp,
297 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
299 HOST_WIDE_INT bitsize, bitpos;
300 tree offset;
301 machine_mode mode;
302 int unsignedp, volatilep;
303 unsigned int align = BITS_PER_UNIT;
304 bool known_alignment = false;
306 /* Get the innermost object and the constant (bitpos) and possibly
307 variable (offset) offset of the access. */
308 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
309 &mode, &unsignedp, &volatilep, true);
311 /* Extract alignment information from the innermost object and
312 possibly adjust bitpos and offset. */
313 if (TREE_CODE (exp) == FUNCTION_DECL)
315 /* Function addresses can encode extra information besides their
316 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
317 allows the low bit to be used as a virtual bit, we know
318 that the address itself must be at least 2-byte aligned. */
319 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
320 align = 2 * BITS_PER_UNIT;
322 else if (TREE_CODE (exp) == LABEL_DECL)
324 else if (TREE_CODE (exp) == CONST_DECL)
326 /* The alignment of a CONST_DECL is determined by its initializer. */
327 exp = DECL_INITIAL (exp);
328 align = TYPE_ALIGN (TREE_TYPE (exp));
329 #ifdef CONSTANT_ALIGNMENT
330 if (CONSTANT_CLASS_P (exp))
331 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
332 #endif
333 known_alignment = true;
335 else if (DECL_P (exp))
337 align = DECL_ALIGN (exp);
338 known_alignment = true;
340 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
342 align = TYPE_ALIGN (TREE_TYPE (exp));
344 else if (TREE_CODE (exp) == INDIRECT_REF
345 || TREE_CODE (exp) == MEM_REF
346 || TREE_CODE (exp) == TARGET_MEM_REF)
348 tree addr = TREE_OPERAND (exp, 0);
349 unsigned ptr_align;
350 unsigned HOST_WIDE_INT ptr_bitpos;
351 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
353 /* If the address is explicitely aligned, handle that. */
354 if (TREE_CODE (addr) == BIT_AND_EXPR
355 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
357 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
358 ptr_bitmask *= BITS_PER_UNIT;
359 align = ptr_bitmask & -ptr_bitmask;
360 addr = TREE_OPERAND (addr, 0);
363 known_alignment
364 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
365 align = MAX (ptr_align, align);
367 /* Re-apply explicit alignment to the bitpos. */
368 ptr_bitpos &= ptr_bitmask;
370 /* The alignment of the pointer operand in a TARGET_MEM_REF
371 has to take the variable offset parts into account. */
372 if (TREE_CODE (exp) == TARGET_MEM_REF)
374 if (TMR_INDEX (exp))
376 unsigned HOST_WIDE_INT step = 1;
377 if (TMR_STEP (exp))
378 step = TREE_INT_CST_LOW (TMR_STEP (exp));
379 align = MIN (align, (step & -step) * BITS_PER_UNIT);
381 if (TMR_INDEX2 (exp))
382 align = BITS_PER_UNIT;
383 known_alignment = false;
386 /* When EXP is an actual memory reference then we can use
387 TYPE_ALIGN of a pointer indirection to derive alignment.
388 Do so only if get_pointer_alignment_1 did not reveal absolute
389 alignment knowledge and if using that alignment would
390 improve the situation. */
391 if (!addr_p && !known_alignment
392 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
393 align = TYPE_ALIGN (TREE_TYPE (exp));
394 else
396 /* Else adjust bitpos accordingly. */
397 bitpos += ptr_bitpos;
398 if (TREE_CODE (exp) == MEM_REF
399 || TREE_CODE (exp) == TARGET_MEM_REF)
400 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
403 else if (TREE_CODE (exp) == STRING_CST)
405 /* STRING_CST are the only constant objects we allow to be not
406 wrapped inside a CONST_DECL. */
407 align = TYPE_ALIGN (TREE_TYPE (exp));
408 #ifdef CONSTANT_ALIGNMENT
409 if (CONSTANT_CLASS_P (exp))
410 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
411 #endif
412 known_alignment = true;
415 /* If there is a non-constant offset part extract the maximum
416 alignment that can prevail. */
417 if (offset)
419 unsigned int trailing_zeros = tree_ctz (offset);
420 if (trailing_zeros < HOST_BITS_PER_INT)
422 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
423 if (inner)
424 align = MIN (align, inner);
428 *alignp = align;
429 *bitposp = bitpos & (*alignp - 1);
430 return known_alignment;
433 /* For a memory reference expression EXP compute values M and N such that M
434 divides (&EXP - N) and such that N < M. If these numbers can be determined,
435 store M in alignp and N in *BITPOSP and return true. Otherwise return false
436 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
438 bool
439 get_object_alignment_1 (tree exp, unsigned int *alignp,
440 unsigned HOST_WIDE_INT *bitposp)
442 return get_object_alignment_2 (exp, alignp, bitposp, false);
445 /* Return the alignment in bits of EXP, an object. */
447 unsigned int
448 get_object_alignment (tree exp)
450 unsigned HOST_WIDE_INT bitpos = 0;
451 unsigned int align;
453 get_object_alignment_1 (exp, &align, &bitpos);
455 /* align and bitpos now specify known low bits of the pointer.
456 ptr & (align - 1) == bitpos. */
458 if (bitpos != 0)
459 align = (bitpos & -bitpos);
460 return align;
463 /* For a pointer valued expression EXP compute values M and N such that M
464 divides (EXP - N) and such that N < M. If these numbers can be determined,
465 store M in alignp and N in *BITPOSP and return true. Return false if
466 the results are just a conservative approximation.
468 If EXP is not a pointer, false is returned too. */
470 bool
471 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
472 unsigned HOST_WIDE_INT *bitposp)
474 STRIP_NOPS (exp);
476 if (TREE_CODE (exp) == ADDR_EXPR)
477 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
478 alignp, bitposp, true);
479 else if (TREE_CODE (exp) == SSA_NAME
480 && POINTER_TYPE_P (TREE_TYPE (exp)))
482 unsigned int ptr_align, ptr_misalign;
483 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
485 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
487 *bitposp = ptr_misalign * BITS_PER_UNIT;
488 *alignp = ptr_align * BITS_PER_UNIT;
489 /* We cannot really tell whether this result is an approximation. */
490 return true;
492 else
494 *bitposp = 0;
495 *alignp = BITS_PER_UNIT;
496 return false;
499 else if (TREE_CODE (exp) == INTEGER_CST)
501 *alignp = BIGGEST_ALIGNMENT;
502 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
503 & (BIGGEST_ALIGNMENT - 1));
504 return true;
507 *bitposp = 0;
508 *alignp = BITS_PER_UNIT;
509 return false;
512 /* Return the alignment in bits of EXP, a pointer valued expression.
513 The alignment returned is, by default, the alignment of the thing that
514 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
516 Otherwise, look at the expression to see if we can do better, i.e., if the
517 expression is actually pointing at an object whose alignment is tighter. */
519 unsigned int
520 get_pointer_alignment (tree exp)
522 unsigned HOST_WIDE_INT bitpos = 0;
523 unsigned int align;
525 get_pointer_alignment_1 (exp, &align, &bitpos);
527 /* align and bitpos now specify known low bits of the pointer.
528 ptr & (align - 1) == bitpos. */
530 if (bitpos != 0)
531 align = (bitpos & -bitpos);
533 return align;
536 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
537 way, because it could contain a zero byte in the middle.
538 TREE_STRING_LENGTH is the size of the character array, not the string.
540 ONLY_VALUE should be nonzero if the result is not going to be emitted
541 into the instruction stream and zero if it is going to be expanded.
542 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
543 is returned, otherwise NULL, since
544 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
545 evaluate the side-effects.
547 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
548 accesses. Note that this implies the result is not going to be emitted
549 into the instruction stream.
551 The value returned is of type `ssizetype'.
553 Unfortunately, string_constant can't access the values of const char
554 arrays with initializers, so neither can we do so here. */
556 tree
557 c_strlen (tree src, int only_value)
559 tree offset_node;
560 HOST_WIDE_INT offset;
561 int max;
562 const char *ptr;
563 location_t loc;
565 STRIP_NOPS (src);
566 if (TREE_CODE (src) == COND_EXPR
567 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
569 tree len1, len2;
571 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
572 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
573 if (tree_int_cst_equal (len1, len2))
574 return len1;
577 if (TREE_CODE (src) == COMPOUND_EXPR
578 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
579 return c_strlen (TREE_OPERAND (src, 1), only_value);
581 loc = EXPR_LOC_OR_LOC (src, input_location);
583 src = string_constant (src, &offset_node);
584 if (src == 0)
585 return NULL_TREE;
587 max = TREE_STRING_LENGTH (src) - 1;
588 ptr = TREE_STRING_POINTER (src);
590 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
592 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
593 compute the offset to the following null if we don't know where to
594 start searching for it. */
595 int i;
597 for (i = 0; i < max; i++)
598 if (ptr[i] == 0)
599 return NULL_TREE;
601 /* We don't know the starting offset, but we do know that the string
602 has no internal zero bytes. We can assume that the offset falls
603 within the bounds of the string; otherwise, the programmer deserves
604 what he gets. Subtract the offset from the length of the string,
605 and return that. This would perhaps not be valid if we were dealing
606 with named arrays in addition to literal string constants. */
608 return size_diffop_loc (loc, size_int (max), offset_node);
611 /* We have a known offset into the string. Start searching there for
612 a null character if we can represent it as a single HOST_WIDE_INT. */
613 if (offset_node == 0)
614 offset = 0;
615 else if (! tree_fits_shwi_p (offset_node))
616 offset = -1;
617 else
618 offset = tree_to_shwi (offset_node);
620 /* If the offset is known to be out of bounds, warn, and call strlen at
621 runtime. */
622 if (offset < 0 || offset > max)
624 /* Suppress multiple warnings for propagated constant strings. */
625 if (only_value != 2
626 && !TREE_NO_WARNING (src))
628 warning_at (loc, 0, "offset outside bounds of constant string");
629 TREE_NO_WARNING (src) = 1;
631 return NULL_TREE;
634 /* Use strlen to search for the first zero byte. Since any strings
635 constructed with build_string will have nulls appended, we win even
636 if we get handed something like (char[4])"abcd".
638 Since OFFSET is our starting index into the string, no further
639 calculation is needed. */
640 return ssize_int (strlen (ptr + offset));
643 /* Return a char pointer for a C string if it is a string constant
644 or sum of string constant and integer constant. */
646 const char *
647 c_getstr (tree src)
649 tree offset_node;
651 src = string_constant (src, &offset_node);
652 if (src == 0)
653 return 0;
655 if (offset_node == 0)
656 return TREE_STRING_POINTER (src);
657 else if (!tree_fits_uhwi_p (offset_node)
658 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
659 return 0;
661 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
664 /* Return a constant integer corresponding to target reading
665 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
667 static rtx
668 c_readstr (const char *str, machine_mode mode)
670 HOST_WIDE_INT ch;
671 unsigned int i, j;
672 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
674 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
675 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
676 / HOST_BITS_PER_WIDE_INT;
678 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
679 for (i = 0; i < len; i++)
680 tmp[i] = 0;
682 ch = 1;
683 for (i = 0; i < GET_MODE_SIZE (mode); i++)
685 j = i;
686 if (WORDS_BIG_ENDIAN)
687 j = GET_MODE_SIZE (mode) - i - 1;
688 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
689 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
690 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
691 j *= BITS_PER_UNIT;
693 if (ch)
694 ch = (unsigned char) str[i];
695 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
698 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
699 return immed_wide_int_const (c, mode);
702 /* Cast a target constant CST to target CHAR and if that value fits into
703 host char type, return zero and put that value into variable pointed to by
704 P. */
706 static int
707 target_char_cast (tree cst, char *p)
709 unsigned HOST_WIDE_INT val, hostval;
711 if (TREE_CODE (cst) != INTEGER_CST
712 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
713 return 1;
715 /* Do not care if it fits or not right here. */
716 val = TREE_INT_CST_LOW (cst);
718 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
719 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
721 hostval = val;
722 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
723 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
725 if (val != hostval)
726 return 1;
728 *p = hostval;
729 return 0;
732 /* Similar to save_expr, but assumes that arbitrary code is not executed
733 in between the multiple evaluations. In particular, we assume that a
734 non-addressable local variable will not be modified. */
736 static tree
737 builtin_save_expr (tree exp)
739 if (TREE_CODE (exp) == SSA_NAME
740 || (TREE_ADDRESSABLE (exp) == 0
741 && (TREE_CODE (exp) == PARM_DECL
742 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
743 return exp;
745 return save_expr (exp);
748 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
749 times to get the address of either a higher stack frame, or a return
750 address located within it (depending on FNDECL_CODE). */
752 static rtx
753 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
755 int i;
757 #ifdef INITIAL_FRAME_ADDRESS_RTX
758 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
759 #else
760 rtx tem;
762 /* For a zero count with __builtin_return_address, we don't care what
763 frame address we return, because target-specific definitions will
764 override us. Therefore frame pointer elimination is OK, and using
765 the soft frame pointer is OK.
767 For a nonzero count, or a zero count with __builtin_frame_address,
768 we require a stable offset from the current frame pointer to the
769 previous one, so we must use the hard frame pointer, and
770 we must disable frame pointer elimination. */
771 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
772 tem = frame_pointer_rtx;
773 else
775 tem = hard_frame_pointer_rtx;
777 /* Tell reload not to eliminate the frame pointer. */
778 crtl->accesses_prior_frames = 1;
780 #endif
782 /* Some machines need special handling before we can access
783 arbitrary frames. For example, on the SPARC, we must first flush
784 all register windows to the stack. */
785 #ifdef SETUP_FRAME_ADDRESSES
786 if (count > 0)
787 SETUP_FRAME_ADDRESSES ();
788 #endif
790 /* On the SPARC, the return address is not in the frame, it is in a
791 register. There is no way to access it off of the current frame
792 pointer, but it can be accessed off the previous frame pointer by
793 reading the value from the register window save area. */
794 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
795 count--;
797 /* Scan back COUNT frames to the specified frame. */
798 for (i = 0; i < count; i++)
800 /* Assume the dynamic chain pointer is in the word that the
801 frame address points to, unless otherwise specified. */
802 #ifdef DYNAMIC_CHAIN_ADDRESS
803 tem = DYNAMIC_CHAIN_ADDRESS (tem);
804 #endif
805 tem = memory_address (Pmode, tem);
806 tem = gen_frame_mem (Pmode, tem);
807 tem = copy_to_reg (tem);
810 /* For __builtin_frame_address, return what we've got. But, on
811 the SPARC for example, we may have to add a bias. */
812 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
813 #ifdef FRAME_ADDR_RTX
814 return FRAME_ADDR_RTX (tem);
815 #else
816 return tem;
817 #endif
819 /* For __builtin_return_address, get the return address from that frame. */
820 #ifdef RETURN_ADDR_RTX
821 tem = RETURN_ADDR_RTX (count, tem);
822 #else
823 tem = memory_address (Pmode,
824 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
825 tem = gen_frame_mem (Pmode, tem);
826 #endif
827 return tem;
830 /* Alias set used for setjmp buffer. */
831 static alias_set_type setjmp_alias_set = -1;
833 /* Construct the leading half of a __builtin_setjmp call. Control will
834 return to RECEIVER_LABEL. This is also called directly by the SJLJ
835 exception handling code. */
837 void
838 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
840 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
841 rtx stack_save;
842 rtx mem;
844 if (setjmp_alias_set == -1)
845 setjmp_alias_set = new_alias_set ();
847 buf_addr = convert_memory_address (Pmode, buf_addr);
849 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
851 /* We store the frame pointer and the address of receiver_label in
852 the buffer and use the rest of it for the stack save area, which
853 is machine-dependent. */
855 mem = gen_rtx_MEM (Pmode, buf_addr);
856 set_mem_alias_set (mem, setjmp_alias_set);
857 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
859 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
860 GET_MODE_SIZE (Pmode))),
861 set_mem_alias_set (mem, setjmp_alias_set);
863 emit_move_insn (validize_mem (mem),
864 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
866 stack_save = gen_rtx_MEM (sa_mode,
867 plus_constant (Pmode, buf_addr,
868 2 * GET_MODE_SIZE (Pmode)));
869 set_mem_alias_set (stack_save, setjmp_alias_set);
870 emit_stack_save (SAVE_NONLOCAL, &stack_save);
872 /* If there is further processing to do, do it. */
873 #ifdef HAVE_builtin_setjmp_setup
874 if (HAVE_builtin_setjmp_setup)
875 emit_insn (gen_builtin_setjmp_setup (buf_addr));
876 #endif
878 /* We have a nonlocal label. */
879 cfun->has_nonlocal_label = 1;
882 /* Construct the trailing part of a __builtin_setjmp call. This is
883 also called directly by the SJLJ exception handling code.
884 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
886 void
887 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
889 rtx chain;
891 /* Mark the FP as used when we get here, so we have to make sure it's
892 marked as used by this function. */
893 emit_use (hard_frame_pointer_rtx);
895 /* Mark the static chain as clobbered here so life information
896 doesn't get messed up for it. */
897 chain = targetm.calls.static_chain (current_function_decl, true);
898 if (chain && REG_P (chain))
899 emit_clobber (chain);
901 /* Now put in the code to restore the frame pointer, and argument
902 pointer, if needed. */
903 #ifdef HAVE_nonlocal_goto
904 if (! HAVE_nonlocal_goto)
905 #endif
907 /* First adjust our frame pointer to its actual value. It was
908 previously set to the start of the virtual area corresponding to
909 the stacked variables when we branched here and now needs to be
910 adjusted to the actual hardware fp value.
912 Assignments to virtual registers are converted by
913 instantiate_virtual_regs into the corresponding assignment
914 to the underlying register (fp in this case) that makes
915 the original assignment true.
916 So the following insn will actually be decrementing fp by
917 STARTING_FRAME_OFFSET. */
918 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
920 /* Restoring the frame pointer also modifies the hard frame pointer.
921 Mark it used (so that the previous assignment remains live once
922 the frame pointer is eliminated) and clobbered (to represent the
923 implicit update from the assignment). */
924 emit_use (hard_frame_pointer_rtx);
925 emit_clobber (hard_frame_pointer_rtx);
928 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
929 if (fixed_regs[ARG_POINTER_REGNUM])
931 #ifdef ELIMINABLE_REGS
932 /* If the argument pointer can be eliminated in favor of the
933 frame pointer, we don't need to restore it. We assume here
934 that if such an elimination is present, it can always be used.
935 This is the case on all known machines; if we don't make this
936 assumption, we do unnecessary saving on many machines. */
937 size_t i;
938 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
940 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
941 if (elim_regs[i].from == ARG_POINTER_REGNUM
942 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
943 break;
945 if (i == ARRAY_SIZE (elim_regs))
946 #endif
948 /* Now restore our arg pointer from the address at which it
949 was saved in our stack frame. */
950 emit_move_insn (crtl->args.internal_arg_pointer,
951 copy_to_reg (get_arg_pointer_save_area ()));
954 #endif
956 #ifdef HAVE_builtin_setjmp_receiver
957 if (receiver_label != NULL && HAVE_builtin_setjmp_receiver)
958 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
959 else
960 #endif
961 #ifdef HAVE_nonlocal_goto_receiver
962 if (HAVE_nonlocal_goto_receiver)
963 emit_insn (gen_nonlocal_goto_receiver ());
964 else
965 #endif
966 { /* Nothing */ }
968 /* We must not allow the code we just generated to be reordered by
969 scheduling. Specifically, the update of the frame pointer must
970 happen immediately, not later. */
971 emit_insn (gen_blockage ());
974 /* __builtin_longjmp is passed a pointer to an array of five words (not
975 all will be used on all machines). It operates similarly to the C
976 library function of the same name, but is more efficient. Much of
977 the code below is copied from the handling of non-local gotos. */
979 static void
980 expand_builtin_longjmp (rtx buf_addr, rtx value)
982 rtx fp, lab, stack;
983 rtx_insn *insn, *last;
984 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
986 /* DRAP is needed for stack realign if longjmp is expanded to current
987 function */
988 if (SUPPORTS_STACK_ALIGNMENT)
989 crtl->need_drap = true;
991 if (setjmp_alias_set == -1)
992 setjmp_alias_set = new_alias_set ();
994 buf_addr = convert_memory_address (Pmode, buf_addr);
996 buf_addr = force_reg (Pmode, buf_addr);
998 /* We require that the user must pass a second argument of 1, because
999 that is what builtin_setjmp will return. */
1000 gcc_assert (value == const1_rtx);
1002 last = get_last_insn ();
1003 #ifdef HAVE_builtin_longjmp
1004 if (HAVE_builtin_longjmp)
1005 emit_insn (gen_builtin_longjmp (buf_addr));
1006 else
1007 #endif
1009 fp = gen_rtx_MEM (Pmode, buf_addr);
1010 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1011 GET_MODE_SIZE (Pmode)));
1013 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1014 2 * GET_MODE_SIZE (Pmode)));
1015 set_mem_alias_set (fp, setjmp_alias_set);
1016 set_mem_alias_set (lab, setjmp_alias_set);
1017 set_mem_alias_set (stack, setjmp_alias_set);
1019 /* Pick up FP, label, and SP from the block and jump. This code is
1020 from expand_goto in stmt.c; see there for detailed comments. */
1021 #ifdef HAVE_nonlocal_goto
1022 if (HAVE_nonlocal_goto)
1023 /* We have to pass a value to the nonlocal_goto pattern that will
1024 get copied into the static_chain pointer, but it does not matter
1025 what that value is, because builtin_setjmp does not use it. */
1026 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
1027 else
1028 #endif
1030 lab = copy_to_reg (lab);
1032 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1033 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1035 emit_move_insn (hard_frame_pointer_rtx, fp);
1036 emit_stack_restore (SAVE_NONLOCAL, stack);
1038 emit_use (hard_frame_pointer_rtx);
1039 emit_use (stack_pointer_rtx);
1040 emit_indirect_jump (lab);
1044 /* Search backwards and mark the jump insn as a non-local goto.
1045 Note that this precludes the use of __builtin_longjmp to a
1046 __builtin_setjmp target in the same function. However, we've
1047 already cautioned the user that these functions are for
1048 internal exception handling use only. */
1049 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1051 gcc_assert (insn != last);
1053 if (JUMP_P (insn))
1055 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1056 break;
1058 else if (CALL_P (insn))
1059 break;
1063 static inline bool
1064 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1066 return (iter->i < iter->n);
1069 /* This function validates the types of a function call argument list
1070 against a specified list of tree_codes. If the last specifier is a 0,
1071 that represents an ellipses, otherwise the last specifier must be a
1072 VOID_TYPE. */
1074 static bool
1075 validate_arglist (const_tree callexpr, ...)
1077 enum tree_code code;
1078 bool res = 0;
1079 va_list ap;
1080 const_call_expr_arg_iterator iter;
1081 const_tree arg;
1083 va_start (ap, callexpr);
1084 init_const_call_expr_arg_iterator (callexpr, &iter);
1088 code = (enum tree_code) va_arg (ap, int);
1089 switch (code)
1091 case 0:
1092 /* This signifies an ellipses, any further arguments are all ok. */
1093 res = true;
1094 goto end;
1095 case VOID_TYPE:
1096 /* This signifies an endlink, if no arguments remain, return
1097 true, otherwise return false. */
1098 res = !more_const_call_expr_args_p (&iter);
1099 goto end;
1100 default:
1101 /* If no parameters remain or the parameter's code does not
1102 match the specified code, return false. Otherwise continue
1103 checking any remaining arguments. */
1104 arg = next_const_call_expr_arg (&iter);
1105 if (!validate_arg (arg, code))
1106 goto end;
1107 break;
1110 while (1);
1112 /* We need gotos here since we can only have one VA_CLOSE in a
1113 function. */
1114 end: ;
1115 va_end (ap);
1117 return res;
1120 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1121 and the address of the save area. */
1123 static rtx
1124 expand_builtin_nonlocal_goto (tree exp)
1126 tree t_label, t_save_area;
1127 rtx r_label, r_save_area, r_fp, r_sp;
1128 rtx_insn *insn;
1130 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1131 return NULL_RTX;
1133 t_label = CALL_EXPR_ARG (exp, 0);
1134 t_save_area = CALL_EXPR_ARG (exp, 1);
1136 r_label = expand_normal (t_label);
1137 r_label = convert_memory_address (Pmode, r_label);
1138 r_save_area = expand_normal (t_save_area);
1139 r_save_area = convert_memory_address (Pmode, r_save_area);
1140 /* Copy the address of the save location to a register just in case it was
1141 based on the frame pointer. */
1142 r_save_area = copy_to_reg (r_save_area);
1143 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1144 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1145 plus_constant (Pmode, r_save_area,
1146 GET_MODE_SIZE (Pmode)));
1148 crtl->has_nonlocal_goto = 1;
1150 #ifdef HAVE_nonlocal_goto
1151 /* ??? We no longer need to pass the static chain value, afaik. */
1152 if (HAVE_nonlocal_goto)
1153 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1154 else
1155 #endif
1157 r_label = copy_to_reg (r_label);
1159 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1160 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1162 /* Restore frame pointer for containing function. */
1163 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1164 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1166 /* USE of hard_frame_pointer_rtx added for consistency;
1167 not clear if really needed. */
1168 emit_use (hard_frame_pointer_rtx);
1169 emit_use (stack_pointer_rtx);
1171 /* If the architecture is using a GP register, we must
1172 conservatively assume that the target function makes use of it.
1173 The prologue of functions with nonlocal gotos must therefore
1174 initialize the GP register to the appropriate value, and we
1175 must then make sure that this value is live at the point
1176 of the jump. (Note that this doesn't necessarily apply
1177 to targets with a nonlocal_goto pattern; they are free
1178 to implement it in their own way. Note also that this is
1179 a no-op if the GP register is a global invariant.) */
1180 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1181 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1182 emit_use (pic_offset_table_rtx);
1184 emit_indirect_jump (r_label);
1187 /* Search backwards to the jump insn and mark it as a
1188 non-local goto. */
1189 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1191 if (JUMP_P (insn))
1193 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1194 break;
1196 else if (CALL_P (insn))
1197 break;
1200 return const0_rtx;
1203 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1204 (not all will be used on all machines) that was passed to __builtin_setjmp.
1205 It updates the stack pointer in that block to the current value. This is
1206 also called directly by the SJLJ exception handling code. */
1208 void
1209 expand_builtin_update_setjmp_buf (rtx buf_addr)
1211 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1212 rtx stack_save
1213 = gen_rtx_MEM (sa_mode,
1214 memory_address
1215 (sa_mode,
1216 plus_constant (Pmode, buf_addr,
1217 2 * GET_MODE_SIZE (Pmode))));
1219 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1222 /* Expand a call to __builtin_prefetch. For a target that does not support
1223 data prefetch, evaluate the memory address argument in case it has side
1224 effects. */
1226 static void
1227 expand_builtin_prefetch (tree exp)
1229 tree arg0, arg1, arg2;
1230 int nargs;
1231 rtx op0, op1, op2;
1233 if (!validate_arglist (exp, POINTER_TYPE, 0))
1234 return;
1236 arg0 = CALL_EXPR_ARG (exp, 0);
1238 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1239 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1240 locality). */
1241 nargs = call_expr_nargs (exp);
1242 if (nargs > 1)
1243 arg1 = CALL_EXPR_ARG (exp, 1);
1244 else
1245 arg1 = integer_zero_node;
1246 if (nargs > 2)
1247 arg2 = CALL_EXPR_ARG (exp, 2);
1248 else
1249 arg2 = integer_three_node;
1251 /* Argument 0 is an address. */
1252 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1254 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1255 if (TREE_CODE (arg1) != INTEGER_CST)
1257 error ("second argument to %<__builtin_prefetch%> must be a constant");
1258 arg1 = integer_zero_node;
1260 op1 = expand_normal (arg1);
1261 /* Argument 1 must be either zero or one. */
1262 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1264 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1265 " using zero");
1266 op1 = const0_rtx;
1269 /* Argument 2 (locality) must be a compile-time constant int. */
1270 if (TREE_CODE (arg2) != INTEGER_CST)
1272 error ("third argument to %<__builtin_prefetch%> must be a constant");
1273 arg2 = integer_zero_node;
1275 op2 = expand_normal (arg2);
1276 /* Argument 2 must be 0, 1, 2, or 3. */
1277 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1279 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1280 op2 = const0_rtx;
1283 #ifdef HAVE_prefetch
1284 if (HAVE_prefetch)
1286 struct expand_operand ops[3];
1288 create_address_operand (&ops[0], op0);
1289 create_integer_operand (&ops[1], INTVAL (op1));
1290 create_integer_operand (&ops[2], INTVAL (op2));
1291 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1292 return;
1294 #endif
1296 /* Don't do anything with direct references to volatile memory, but
1297 generate code to handle other side effects. */
1298 if (!MEM_P (op0) && side_effects_p (op0))
1299 emit_insn (op0);
1302 /* Get a MEM rtx for expression EXP which is the address of an operand
1303 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1304 the maximum length of the block of memory that might be accessed or
1305 NULL if unknown. */
1307 static rtx
1308 get_memory_rtx (tree exp, tree len)
1310 tree orig_exp = exp;
1311 rtx addr, mem;
1313 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1314 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1315 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1316 exp = TREE_OPERAND (exp, 0);
1318 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1319 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1321 /* Get an expression we can use to find the attributes to assign to MEM.
1322 First remove any nops. */
1323 while (CONVERT_EXPR_P (exp)
1324 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1325 exp = TREE_OPERAND (exp, 0);
1327 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1328 (as builtin stringops may alias with anything). */
1329 exp = fold_build2 (MEM_REF,
1330 build_array_type (char_type_node,
1331 build_range_type (sizetype,
1332 size_one_node, len)),
1333 exp, build_int_cst (ptr_type_node, 0));
1335 /* If the MEM_REF has no acceptable address, try to get the base object
1336 from the original address we got, and build an all-aliasing
1337 unknown-sized access to that one. */
1338 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1339 set_mem_attributes (mem, exp, 0);
1340 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1341 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1342 0))))
1344 exp = build_fold_addr_expr (exp);
1345 exp = fold_build2 (MEM_REF,
1346 build_array_type (char_type_node,
1347 build_range_type (sizetype,
1348 size_zero_node,
1349 NULL)),
1350 exp, build_int_cst (ptr_type_node, 0));
1351 set_mem_attributes (mem, exp, 0);
1353 set_mem_alias_set (mem, 0);
1354 return mem;
1357 /* Built-in functions to perform an untyped call and return. */
1359 #define apply_args_mode \
1360 (this_target_builtins->x_apply_args_mode)
1361 #define apply_result_mode \
1362 (this_target_builtins->x_apply_result_mode)
1364 /* Return the size required for the block returned by __builtin_apply_args,
1365 and initialize apply_args_mode. */
1367 static int
1368 apply_args_size (void)
1370 static int size = -1;
1371 int align;
1372 unsigned int regno;
1373 machine_mode mode;
1375 /* The values computed by this function never change. */
1376 if (size < 0)
1378 /* The first value is the incoming arg-pointer. */
1379 size = GET_MODE_SIZE (Pmode);
1381 /* The second value is the structure value address unless this is
1382 passed as an "invisible" first argument. */
1383 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1384 size += GET_MODE_SIZE (Pmode);
1386 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1387 if (FUNCTION_ARG_REGNO_P (regno))
1389 mode = targetm.calls.get_raw_arg_mode (regno);
1391 gcc_assert (mode != VOIDmode);
1393 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1394 if (size % align != 0)
1395 size = CEIL (size, align) * align;
1396 size += GET_MODE_SIZE (mode);
1397 apply_args_mode[regno] = mode;
1399 else
1401 apply_args_mode[regno] = VOIDmode;
1404 return size;
1407 /* Return the size required for the block returned by __builtin_apply,
1408 and initialize apply_result_mode. */
1410 static int
1411 apply_result_size (void)
1413 static int size = -1;
1414 int align, regno;
1415 machine_mode mode;
1417 /* The values computed by this function never change. */
1418 if (size < 0)
1420 size = 0;
1422 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1423 if (targetm.calls.function_value_regno_p (regno))
1425 mode = targetm.calls.get_raw_result_mode (regno);
1427 gcc_assert (mode != VOIDmode);
1429 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1430 if (size % align != 0)
1431 size = CEIL (size, align) * align;
1432 size += GET_MODE_SIZE (mode);
1433 apply_result_mode[regno] = mode;
1435 else
1436 apply_result_mode[regno] = VOIDmode;
1438 /* Allow targets that use untyped_call and untyped_return to override
1439 the size so that machine-specific information can be stored here. */
1440 #ifdef APPLY_RESULT_SIZE
1441 size = APPLY_RESULT_SIZE;
1442 #endif
1444 return size;
1447 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1448 /* Create a vector describing the result block RESULT. If SAVEP is true,
1449 the result block is used to save the values; otherwise it is used to
1450 restore the values. */
1452 static rtx
1453 result_vector (int savep, rtx result)
1455 int regno, size, align, nelts;
1456 machine_mode mode;
1457 rtx reg, mem;
1458 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1460 size = nelts = 0;
1461 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1462 if ((mode = apply_result_mode[regno]) != VOIDmode)
1464 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1465 if (size % align != 0)
1466 size = CEIL (size, align) * align;
1467 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1468 mem = adjust_address (result, mode, size);
1469 savevec[nelts++] = (savep
1470 ? gen_rtx_SET (mem, reg)
1471 : gen_rtx_SET (reg, mem));
1472 size += GET_MODE_SIZE (mode);
1474 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1476 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1478 /* Save the state required to perform an untyped call with the same
1479 arguments as were passed to the current function. */
1481 static rtx
1482 expand_builtin_apply_args_1 (void)
1484 rtx registers, tem;
1485 int size, align, regno;
1486 machine_mode mode;
1487 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1489 /* Create a block where the arg-pointer, structure value address,
1490 and argument registers can be saved. */
1491 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1493 /* Walk past the arg-pointer and structure value address. */
1494 size = GET_MODE_SIZE (Pmode);
1495 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1496 size += GET_MODE_SIZE (Pmode);
1498 /* Save each register used in calling a function to the block. */
1499 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1500 if ((mode = apply_args_mode[regno]) != VOIDmode)
1502 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1503 if (size % align != 0)
1504 size = CEIL (size, align) * align;
1506 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1508 emit_move_insn (adjust_address (registers, mode, size), tem);
1509 size += GET_MODE_SIZE (mode);
1512 /* Save the arg pointer to the block. */
1513 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1514 /* We need the pointer as the caller actually passed them to us, not
1515 as we might have pretended they were passed. Make sure it's a valid
1516 operand, as emit_move_insn isn't expected to handle a PLUS. */
1517 if (STACK_GROWS_DOWNWARD)
1519 = force_operand (plus_constant (Pmode, tem,
1520 crtl->args.pretend_args_size),
1521 NULL_RTX);
1522 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1524 size = GET_MODE_SIZE (Pmode);
1526 /* Save the structure value address unless this is passed as an
1527 "invisible" first argument. */
1528 if (struct_incoming_value)
1530 emit_move_insn (adjust_address (registers, Pmode, size),
1531 copy_to_reg (struct_incoming_value));
1532 size += GET_MODE_SIZE (Pmode);
1535 /* Return the address of the block. */
1536 return copy_addr_to_reg (XEXP (registers, 0));
1539 /* __builtin_apply_args returns block of memory allocated on
1540 the stack into which is stored the arg pointer, structure
1541 value address, static chain, and all the registers that might
1542 possibly be used in performing a function call. The code is
1543 moved to the start of the function so the incoming values are
1544 saved. */
1546 static rtx
1547 expand_builtin_apply_args (void)
1549 /* Don't do __builtin_apply_args more than once in a function.
1550 Save the result of the first call and reuse it. */
1551 if (apply_args_value != 0)
1552 return apply_args_value;
1554 /* When this function is called, it means that registers must be
1555 saved on entry to this function. So we migrate the
1556 call to the first insn of this function. */
1557 rtx temp;
1559 start_sequence ();
1560 temp = expand_builtin_apply_args_1 ();
1561 rtx_insn *seq = get_insns ();
1562 end_sequence ();
1564 apply_args_value = temp;
1566 /* Put the insns after the NOTE that starts the function.
1567 If this is inside a start_sequence, make the outer-level insn
1568 chain current, so the code is placed at the start of the
1569 function. If internal_arg_pointer is a non-virtual pseudo,
1570 it needs to be placed after the function that initializes
1571 that pseudo. */
1572 push_topmost_sequence ();
1573 if (REG_P (crtl->args.internal_arg_pointer)
1574 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1575 emit_insn_before (seq, parm_birth_insn);
1576 else
1577 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1578 pop_topmost_sequence ();
1579 return temp;
1583 /* Perform an untyped call and save the state required to perform an
1584 untyped return of whatever value was returned by the given function. */
1586 static rtx
1587 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1589 int size, align, regno;
1590 machine_mode mode;
1591 rtx incoming_args, result, reg, dest, src;
1592 rtx_call_insn *call_insn;
1593 rtx old_stack_level = 0;
1594 rtx call_fusage = 0;
1595 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1597 arguments = convert_memory_address (Pmode, arguments);
1599 /* Create a block where the return registers can be saved. */
1600 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1602 /* Fetch the arg pointer from the ARGUMENTS block. */
1603 incoming_args = gen_reg_rtx (Pmode);
1604 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1605 if (!STACK_GROWS_DOWNWARD)
1606 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1607 incoming_args, 0, OPTAB_LIB_WIDEN);
1609 /* Push a new argument block and copy the arguments. Do not allow
1610 the (potential) memcpy call below to interfere with our stack
1611 manipulations. */
1612 do_pending_stack_adjust ();
1613 NO_DEFER_POP;
1615 /* Save the stack with nonlocal if available. */
1616 #ifdef HAVE_save_stack_nonlocal
1617 if (HAVE_save_stack_nonlocal)
1618 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1619 else
1620 #endif
1621 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1623 /* Allocate a block of memory onto the stack and copy the memory
1624 arguments to the outgoing arguments address. We can pass TRUE
1625 as the 4th argument because we just saved the stack pointer
1626 and will restore it right after the call. */
1627 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1629 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1630 may have already set current_function_calls_alloca to true.
1631 current_function_calls_alloca won't be set if argsize is zero,
1632 so we have to guarantee need_drap is true here. */
1633 if (SUPPORTS_STACK_ALIGNMENT)
1634 crtl->need_drap = true;
1636 dest = virtual_outgoing_args_rtx;
1637 if (!STACK_GROWS_DOWNWARD)
1639 if (CONST_INT_P (argsize))
1640 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1641 else
1642 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1644 dest = gen_rtx_MEM (BLKmode, dest);
1645 set_mem_align (dest, PARM_BOUNDARY);
1646 src = gen_rtx_MEM (BLKmode, incoming_args);
1647 set_mem_align (src, PARM_BOUNDARY);
1648 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1650 /* Refer to the argument block. */
1651 apply_args_size ();
1652 arguments = gen_rtx_MEM (BLKmode, arguments);
1653 set_mem_align (arguments, PARM_BOUNDARY);
1655 /* Walk past the arg-pointer and structure value address. */
1656 size = GET_MODE_SIZE (Pmode);
1657 if (struct_value)
1658 size += GET_MODE_SIZE (Pmode);
1660 /* Restore each of the registers previously saved. Make USE insns
1661 for each of these registers for use in making the call. */
1662 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1663 if ((mode = apply_args_mode[regno]) != VOIDmode)
1665 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1666 if (size % align != 0)
1667 size = CEIL (size, align) * align;
1668 reg = gen_rtx_REG (mode, regno);
1669 emit_move_insn (reg, adjust_address (arguments, mode, size));
1670 use_reg (&call_fusage, reg);
1671 size += GET_MODE_SIZE (mode);
1674 /* Restore the structure value address unless this is passed as an
1675 "invisible" first argument. */
1676 size = GET_MODE_SIZE (Pmode);
1677 if (struct_value)
1679 rtx value = gen_reg_rtx (Pmode);
1680 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1681 emit_move_insn (struct_value, value);
1682 if (REG_P (struct_value))
1683 use_reg (&call_fusage, struct_value);
1684 size += GET_MODE_SIZE (Pmode);
1687 /* All arguments and registers used for the call are set up by now! */
1688 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1690 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1691 and we don't want to load it into a register as an optimization,
1692 because prepare_call_address already did it if it should be done. */
1693 if (GET_CODE (function) != SYMBOL_REF)
1694 function = memory_address (FUNCTION_MODE, function);
1696 /* Generate the actual call instruction and save the return value. */
1697 #ifdef HAVE_untyped_call
1698 if (HAVE_untyped_call)
1699 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1700 result, result_vector (1, result)));
1701 else
1702 #endif
1703 #ifdef HAVE_call_value
1704 if (HAVE_call_value)
1706 rtx valreg = 0;
1708 /* Locate the unique return register. It is not possible to
1709 express a call that sets more than one return register using
1710 call_value; use untyped_call for that. In fact, untyped_call
1711 only needs to save the return registers in the given block. */
1712 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1713 if ((mode = apply_result_mode[regno]) != VOIDmode)
1715 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1717 valreg = gen_rtx_REG (mode, regno);
1720 emit_call_insn (GEN_CALL_VALUE (valreg,
1721 gen_rtx_MEM (FUNCTION_MODE, function),
1722 const0_rtx, NULL_RTX, const0_rtx));
1724 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1726 else
1727 #endif
1728 gcc_unreachable ();
1730 /* Find the CALL insn we just emitted, and attach the register usage
1731 information. */
1732 call_insn = last_call_insn ();
1733 add_function_usage_to (call_insn, call_fusage);
1735 /* Restore the stack. */
1736 #ifdef HAVE_save_stack_nonlocal
1737 if (HAVE_save_stack_nonlocal)
1738 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1739 else
1740 #endif
1741 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1742 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1744 OK_DEFER_POP;
1746 /* Return the address of the result block. */
1747 result = copy_addr_to_reg (XEXP (result, 0));
1748 return convert_memory_address (ptr_mode, result);
1751 /* Perform an untyped return. */
1753 static void
1754 expand_builtin_return (rtx result)
1756 int size, align, regno;
1757 machine_mode mode;
1758 rtx reg;
1759 rtx_insn *call_fusage = 0;
1761 result = convert_memory_address (Pmode, result);
1763 apply_result_size ();
1764 result = gen_rtx_MEM (BLKmode, result);
1766 #ifdef HAVE_untyped_return
1767 if (HAVE_untyped_return)
1769 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1770 emit_barrier ();
1771 return;
1773 #endif
1775 /* Restore the return value and note that each value is used. */
1776 size = 0;
1777 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1778 if ((mode = apply_result_mode[regno]) != VOIDmode)
1780 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1781 if (size % align != 0)
1782 size = CEIL (size, align) * align;
1783 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1784 emit_move_insn (reg, adjust_address (result, mode, size));
1786 push_to_sequence (call_fusage);
1787 emit_use (reg);
1788 call_fusage = get_insns ();
1789 end_sequence ();
1790 size += GET_MODE_SIZE (mode);
1793 /* Put the USE insns before the return. */
1794 emit_insn (call_fusage);
1796 /* Return whatever values was restored by jumping directly to the end
1797 of the function. */
1798 expand_naked_return ();
1801 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1803 static enum type_class
1804 type_to_class (tree type)
1806 switch (TREE_CODE (type))
1808 case VOID_TYPE: return void_type_class;
1809 case INTEGER_TYPE: return integer_type_class;
1810 case ENUMERAL_TYPE: return enumeral_type_class;
1811 case BOOLEAN_TYPE: return boolean_type_class;
1812 case POINTER_TYPE: return pointer_type_class;
1813 case REFERENCE_TYPE: return reference_type_class;
1814 case OFFSET_TYPE: return offset_type_class;
1815 case REAL_TYPE: return real_type_class;
1816 case COMPLEX_TYPE: return complex_type_class;
1817 case FUNCTION_TYPE: return function_type_class;
1818 case METHOD_TYPE: return method_type_class;
1819 case RECORD_TYPE: return record_type_class;
1820 case UNION_TYPE:
1821 case QUAL_UNION_TYPE: return union_type_class;
1822 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1823 ? string_type_class : array_type_class);
1824 case LANG_TYPE: return lang_type_class;
1825 default: return no_type_class;
1829 /* Expand a call EXP to __builtin_classify_type. */
1831 static rtx
1832 expand_builtin_classify_type (tree exp)
1834 if (call_expr_nargs (exp))
1835 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1836 return GEN_INT (no_type_class);
1839 /* This helper macro, meant to be used in mathfn_built_in below,
1840 determines which among a set of three builtin math functions is
1841 appropriate for a given type mode. The `F' and `L' cases are
1842 automatically generated from the `double' case. */
1843 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1844 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1845 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1846 fcodel = BUILT_IN_MATHFN##L ; break;
1847 /* Similar to above, but appends _R after any F/L suffix. */
1848 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1849 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1850 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1851 fcodel = BUILT_IN_MATHFN##L_R ; break;
1853 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1854 if available. If IMPLICIT is true use the implicit builtin declaration,
1855 otherwise use the explicit declaration. If we can't do the conversion,
1856 return zero. */
1858 static tree
1859 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1861 enum built_in_function fcode, fcodef, fcodel, fcode2;
1863 switch (fn)
1865 CASE_MATHFN (BUILT_IN_ACOS)
1866 CASE_MATHFN (BUILT_IN_ACOSH)
1867 CASE_MATHFN (BUILT_IN_ASIN)
1868 CASE_MATHFN (BUILT_IN_ASINH)
1869 CASE_MATHFN (BUILT_IN_ATAN)
1870 CASE_MATHFN (BUILT_IN_ATAN2)
1871 CASE_MATHFN (BUILT_IN_ATANH)
1872 CASE_MATHFN (BUILT_IN_CBRT)
1873 CASE_MATHFN (BUILT_IN_CEIL)
1874 CASE_MATHFN (BUILT_IN_CEXPI)
1875 CASE_MATHFN (BUILT_IN_COPYSIGN)
1876 CASE_MATHFN (BUILT_IN_COS)
1877 CASE_MATHFN (BUILT_IN_COSH)
1878 CASE_MATHFN (BUILT_IN_DREM)
1879 CASE_MATHFN (BUILT_IN_ERF)
1880 CASE_MATHFN (BUILT_IN_ERFC)
1881 CASE_MATHFN (BUILT_IN_EXP)
1882 CASE_MATHFN (BUILT_IN_EXP10)
1883 CASE_MATHFN (BUILT_IN_EXP2)
1884 CASE_MATHFN (BUILT_IN_EXPM1)
1885 CASE_MATHFN (BUILT_IN_FABS)
1886 CASE_MATHFN (BUILT_IN_FDIM)
1887 CASE_MATHFN (BUILT_IN_FLOOR)
1888 CASE_MATHFN (BUILT_IN_FMA)
1889 CASE_MATHFN (BUILT_IN_FMAX)
1890 CASE_MATHFN (BUILT_IN_FMIN)
1891 CASE_MATHFN (BUILT_IN_FMOD)
1892 CASE_MATHFN (BUILT_IN_FREXP)
1893 CASE_MATHFN (BUILT_IN_GAMMA)
1894 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1895 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1896 CASE_MATHFN (BUILT_IN_HYPOT)
1897 CASE_MATHFN (BUILT_IN_ILOGB)
1898 CASE_MATHFN (BUILT_IN_ICEIL)
1899 CASE_MATHFN (BUILT_IN_IFLOOR)
1900 CASE_MATHFN (BUILT_IN_INF)
1901 CASE_MATHFN (BUILT_IN_IRINT)
1902 CASE_MATHFN (BUILT_IN_IROUND)
1903 CASE_MATHFN (BUILT_IN_ISINF)
1904 CASE_MATHFN (BUILT_IN_J0)
1905 CASE_MATHFN (BUILT_IN_J1)
1906 CASE_MATHFN (BUILT_IN_JN)
1907 CASE_MATHFN (BUILT_IN_LCEIL)
1908 CASE_MATHFN (BUILT_IN_LDEXP)
1909 CASE_MATHFN (BUILT_IN_LFLOOR)
1910 CASE_MATHFN (BUILT_IN_LGAMMA)
1911 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1912 CASE_MATHFN (BUILT_IN_LLCEIL)
1913 CASE_MATHFN (BUILT_IN_LLFLOOR)
1914 CASE_MATHFN (BUILT_IN_LLRINT)
1915 CASE_MATHFN (BUILT_IN_LLROUND)
1916 CASE_MATHFN (BUILT_IN_LOG)
1917 CASE_MATHFN (BUILT_IN_LOG10)
1918 CASE_MATHFN (BUILT_IN_LOG1P)
1919 CASE_MATHFN (BUILT_IN_LOG2)
1920 CASE_MATHFN (BUILT_IN_LOGB)
1921 CASE_MATHFN (BUILT_IN_LRINT)
1922 CASE_MATHFN (BUILT_IN_LROUND)
1923 CASE_MATHFN (BUILT_IN_MODF)
1924 CASE_MATHFN (BUILT_IN_NAN)
1925 CASE_MATHFN (BUILT_IN_NANS)
1926 CASE_MATHFN (BUILT_IN_NEARBYINT)
1927 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1928 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1929 CASE_MATHFN (BUILT_IN_POW)
1930 CASE_MATHFN (BUILT_IN_POWI)
1931 CASE_MATHFN (BUILT_IN_POW10)
1932 CASE_MATHFN (BUILT_IN_REMAINDER)
1933 CASE_MATHFN (BUILT_IN_REMQUO)
1934 CASE_MATHFN (BUILT_IN_RINT)
1935 CASE_MATHFN (BUILT_IN_ROUND)
1936 CASE_MATHFN (BUILT_IN_SCALB)
1937 CASE_MATHFN (BUILT_IN_SCALBLN)
1938 CASE_MATHFN (BUILT_IN_SCALBN)
1939 CASE_MATHFN (BUILT_IN_SIGNBIT)
1940 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1941 CASE_MATHFN (BUILT_IN_SIN)
1942 CASE_MATHFN (BUILT_IN_SINCOS)
1943 CASE_MATHFN (BUILT_IN_SINH)
1944 CASE_MATHFN (BUILT_IN_SQRT)
1945 CASE_MATHFN (BUILT_IN_TAN)
1946 CASE_MATHFN (BUILT_IN_TANH)
1947 CASE_MATHFN (BUILT_IN_TGAMMA)
1948 CASE_MATHFN (BUILT_IN_TRUNC)
1949 CASE_MATHFN (BUILT_IN_Y0)
1950 CASE_MATHFN (BUILT_IN_Y1)
1951 CASE_MATHFN (BUILT_IN_YN)
1953 default:
1954 return NULL_TREE;
1957 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1958 fcode2 = fcode;
1959 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1960 fcode2 = fcodef;
1961 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1962 fcode2 = fcodel;
1963 else
1964 return NULL_TREE;
1966 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1967 return NULL_TREE;
1969 return builtin_decl_explicit (fcode2);
1972 /* Like mathfn_built_in_1(), but always use the implicit array. */
1974 tree
1975 mathfn_built_in (tree type, enum built_in_function fn)
1977 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1980 /* If errno must be maintained, expand the RTL to check if the result,
1981 TARGET, of a built-in function call, EXP, is NaN, and if so set
1982 errno to EDOM. */
1984 static void
1985 expand_errno_check (tree exp, rtx target)
1987 rtx_code_label *lab = gen_label_rtx ();
1989 /* Test the result; if it is NaN, set errno=EDOM because
1990 the argument was not in the domain. */
1991 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1992 NULL_RTX, NULL, lab,
1993 /* The jump is very likely. */
1994 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1996 #ifdef TARGET_EDOM
1997 /* If this built-in doesn't throw an exception, set errno directly. */
1998 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
2000 #ifdef GEN_ERRNO_RTX
2001 rtx errno_rtx = GEN_ERRNO_RTX;
2002 #else
2003 rtx errno_rtx
2004 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
2005 #endif
2006 emit_move_insn (errno_rtx,
2007 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
2008 emit_label (lab);
2009 return;
2011 #endif
2013 /* Make sure the library call isn't expanded as a tail call. */
2014 CALL_EXPR_TAILCALL (exp) = 0;
2016 /* We can't set errno=EDOM directly; let the library call do it.
2017 Pop the arguments right away in case the call gets deleted. */
2018 NO_DEFER_POP;
2019 expand_call (exp, target, 0);
2020 OK_DEFER_POP;
2021 emit_label (lab);
2024 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2025 Return NULL_RTX if a normal call should be emitted rather than expanding
2026 the function in-line. EXP is the expression that is a call to the builtin
2027 function; if convenient, the result should be placed in TARGET.
2028 SUBTARGET may be used as the target for computing one of EXP's operands. */
2030 static rtx
2031 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2033 optab builtin_optab;
2034 rtx op0;
2035 rtx_insn *insns;
2036 tree fndecl = get_callee_fndecl (exp);
2037 machine_mode mode;
2038 bool errno_set = false;
2039 bool try_widening = false;
2040 tree arg;
2042 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2043 return NULL_RTX;
2045 arg = CALL_EXPR_ARG (exp, 0);
2047 switch (DECL_FUNCTION_CODE (fndecl))
2049 CASE_FLT_FN (BUILT_IN_SQRT):
2050 errno_set = ! tree_expr_nonnegative_p (arg);
2051 try_widening = true;
2052 builtin_optab = sqrt_optab;
2053 break;
2054 CASE_FLT_FN (BUILT_IN_EXP):
2055 errno_set = true; builtin_optab = exp_optab; break;
2056 CASE_FLT_FN (BUILT_IN_EXP10):
2057 CASE_FLT_FN (BUILT_IN_POW10):
2058 errno_set = true; builtin_optab = exp10_optab; break;
2059 CASE_FLT_FN (BUILT_IN_EXP2):
2060 errno_set = true; builtin_optab = exp2_optab; break;
2061 CASE_FLT_FN (BUILT_IN_EXPM1):
2062 errno_set = true; builtin_optab = expm1_optab; break;
2063 CASE_FLT_FN (BUILT_IN_LOGB):
2064 errno_set = true; builtin_optab = logb_optab; break;
2065 CASE_FLT_FN (BUILT_IN_LOG):
2066 errno_set = true; builtin_optab = log_optab; break;
2067 CASE_FLT_FN (BUILT_IN_LOG10):
2068 errno_set = true; builtin_optab = log10_optab; break;
2069 CASE_FLT_FN (BUILT_IN_LOG2):
2070 errno_set = true; builtin_optab = log2_optab; break;
2071 CASE_FLT_FN (BUILT_IN_LOG1P):
2072 errno_set = true; builtin_optab = log1p_optab; break;
2073 CASE_FLT_FN (BUILT_IN_ASIN):
2074 builtin_optab = asin_optab; break;
2075 CASE_FLT_FN (BUILT_IN_ACOS):
2076 builtin_optab = acos_optab; break;
2077 CASE_FLT_FN (BUILT_IN_TAN):
2078 builtin_optab = tan_optab; break;
2079 CASE_FLT_FN (BUILT_IN_ATAN):
2080 builtin_optab = atan_optab; break;
2081 CASE_FLT_FN (BUILT_IN_FLOOR):
2082 builtin_optab = floor_optab; break;
2083 CASE_FLT_FN (BUILT_IN_CEIL):
2084 builtin_optab = ceil_optab; break;
2085 CASE_FLT_FN (BUILT_IN_TRUNC):
2086 builtin_optab = btrunc_optab; break;
2087 CASE_FLT_FN (BUILT_IN_ROUND):
2088 builtin_optab = round_optab; break;
2089 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2090 builtin_optab = nearbyint_optab;
2091 if (flag_trapping_math)
2092 break;
2093 /* Else fallthrough and expand as rint. */
2094 CASE_FLT_FN (BUILT_IN_RINT):
2095 builtin_optab = rint_optab; break;
2096 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2097 builtin_optab = significand_optab; break;
2098 default:
2099 gcc_unreachable ();
2102 /* Make a suitable register to place result in. */
2103 mode = TYPE_MODE (TREE_TYPE (exp));
2105 if (! flag_errno_math || ! HONOR_NANS (mode))
2106 errno_set = false;
2108 /* Before working hard, check whether the instruction is available, but try
2109 to widen the mode for specific operations. */
2110 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2111 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2112 && (!errno_set || !optimize_insn_for_size_p ()))
2114 rtx result = gen_reg_rtx (mode);
2116 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2117 need to expand the argument again. This way, we will not perform
2118 side-effects more the once. */
2119 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2121 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2123 start_sequence ();
2125 /* Compute into RESULT.
2126 Set RESULT to wherever the result comes back. */
2127 result = expand_unop (mode, builtin_optab, op0, result, 0);
2129 if (result != 0)
2131 if (errno_set)
2132 expand_errno_check (exp, result);
2134 /* Output the entire sequence. */
2135 insns = get_insns ();
2136 end_sequence ();
2137 emit_insn (insns);
2138 return result;
2141 /* If we were unable to expand via the builtin, stop the sequence
2142 (without outputting the insns) and call to the library function
2143 with the stabilized argument list. */
2144 end_sequence ();
2147 return expand_call (exp, target, target == const0_rtx);
2150 /* Expand a call to the builtin binary math functions (pow and atan2).
2151 Return NULL_RTX if a normal call should be emitted rather than expanding the
2152 function in-line. EXP is the expression that is a call to the builtin
2153 function; if convenient, the result should be placed in TARGET.
2154 SUBTARGET may be used as the target for computing one of EXP's
2155 operands. */
2157 static rtx
2158 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2160 optab builtin_optab;
2161 rtx op0, op1, result;
2162 rtx_insn *insns;
2163 int op1_type = REAL_TYPE;
2164 tree fndecl = get_callee_fndecl (exp);
2165 tree arg0, arg1;
2166 machine_mode mode;
2167 bool errno_set = true;
2169 switch (DECL_FUNCTION_CODE (fndecl))
2171 CASE_FLT_FN (BUILT_IN_SCALBN):
2172 CASE_FLT_FN (BUILT_IN_SCALBLN):
2173 CASE_FLT_FN (BUILT_IN_LDEXP):
2174 op1_type = INTEGER_TYPE;
2175 default:
2176 break;
2179 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2180 return NULL_RTX;
2182 arg0 = CALL_EXPR_ARG (exp, 0);
2183 arg1 = CALL_EXPR_ARG (exp, 1);
2185 switch (DECL_FUNCTION_CODE (fndecl))
2187 CASE_FLT_FN (BUILT_IN_POW):
2188 builtin_optab = pow_optab; break;
2189 CASE_FLT_FN (BUILT_IN_ATAN2):
2190 builtin_optab = atan2_optab; break;
2191 CASE_FLT_FN (BUILT_IN_SCALB):
2192 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2193 return 0;
2194 builtin_optab = scalb_optab; break;
2195 CASE_FLT_FN (BUILT_IN_SCALBN):
2196 CASE_FLT_FN (BUILT_IN_SCALBLN):
2197 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2198 return 0;
2199 /* Fall through... */
2200 CASE_FLT_FN (BUILT_IN_LDEXP):
2201 builtin_optab = ldexp_optab; break;
2202 CASE_FLT_FN (BUILT_IN_FMOD):
2203 builtin_optab = fmod_optab; break;
2204 CASE_FLT_FN (BUILT_IN_REMAINDER):
2205 CASE_FLT_FN (BUILT_IN_DREM):
2206 builtin_optab = remainder_optab; break;
2207 default:
2208 gcc_unreachable ();
2211 /* Make a suitable register to place result in. */
2212 mode = TYPE_MODE (TREE_TYPE (exp));
2214 /* Before working hard, check whether the instruction is available. */
2215 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2216 return NULL_RTX;
2218 result = gen_reg_rtx (mode);
2220 if (! flag_errno_math || ! HONOR_NANS (mode))
2221 errno_set = false;
2223 if (errno_set && optimize_insn_for_size_p ())
2224 return 0;
2226 /* Always stabilize the argument list. */
2227 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2228 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2230 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2231 op1 = expand_normal (arg1);
2233 start_sequence ();
2235 /* Compute into RESULT.
2236 Set RESULT to wherever the result comes back. */
2237 result = expand_binop (mode, builtin_optab, op0, op1,
2238 result, 0, OPTAB_DIRECT);
2240 /* If we were unable to expand via the builtin, stop the sequence
2241 (without outputting the insns) and call to the library function
2242 with the stabilized argument list. */
2243 if (result == 0)
2245 end_sequence ();
2246 return expand_call (exp, target, target == const0_rtx);
2249 if (errno_set)
2250 expand_errno_check (exp, result);
2252 /* Output the entire sequence. */
2253 insns = get_insns ();
2254 end_sequence ();
2255 emit_insn (insns);
2257 return result;
2260 /* Expand a call to the builtin trinary math functions (fma).
2261 Return NULL_RTX if a normal call should be emitted rather than expanding the
2262 function in-line. EXP is the expression that is a call to the builtin
2263 function; if convenient, the result should be placed in TARGET.
2264 SUBTARGET may be used as the target for computing one of EXP's
2265 operands. */
2267 static rtx
2268 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2270 optab builtin_optab;
2271 rtx op0, op1, op2, result;
2272 rtx_insn *insns;
2273 tree fndecl = get_callee_fndecl (exp);
2274 tree arg0, arg1, arg2;
2275 machine_mode mode;
2277 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2278 return NULL_RTX;
2280 arg0 = CALL_EXPR_ARG (exp, 0);
2281 arg1 = CALL_EXPR_ARG (exp, 1);
2282 arg2 = CALL_EXPR_ARG (exp, 2);
2284 switch (DECL_FUNCTION_CODE (fndecl))
2286 CASE_FLT_FN (BUILT_IN_FMA):
2287 builtin_optab = fma_optab; break;
2288 default:
2289 gcc_unreachable ();
2292 /* Make a suitable register to place result in. */
2293 mode = TYPE_MODE (TREE_TYPE (exp));
2295 /* Before working hard, check whether the instruction is available. */
2296 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2297 return NULL_RTX;
2299 result = gen_reg_rtx (mode);
2301 /* Always stabilize the argument list. */
2302 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2303 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2304 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2306 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2307 op1 = expand_normal (arg1);
2308 op2 = expand_normal (arg2);
2310 start_sequence ();
2312 /* Compute into RESULT.
2313 Set RESULT to wherever the result comes back. */
2314 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2315 result, 0);
2317 /* If we were unable to expand via the builtin, stop the sequence
2318 (without outputting the insns) and call to the library function
2319 with the stabilized argument list. */
2320 if (result == 0)
2322 end_sequence ();
2323 return expand_call (exp, target, target == const0_rtx);
2326 /* Output the entire sequence. */
2327 insns = get_insns ();
2328 end_sequence ();
2329 emit_insn (insns);
2331 return result;
2334 /* Expand a call to the builtin sin and cos math functions.
2335 Return NULL_RTX if a normal call should be emitted rather than expanding the
2336 function in-line. EXP is the expression that is a call to the builtin
2337 function; if convenient, the result should be placed in TARGET.
2338 SUBTARGET may be used as the target for computing one of EXP's
2339 operands. */
2341 static rtx
2342 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2344 optab builtin_optab;
2345 rtx op0;
2346 rtx_insn *insns;
2347 tree fndecl = get_callee_fndecl (exp);
2348 machine_mode mode;
2349 tree arg;
2351 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2352 return NULL_RTX;
2354 arg = CALL_EXPR_ARG (exp, 0);
2356 switch (DECL_FUNCTION_CODE (fndecl))
2358 CASE_FLT_FN (BUILT_IN_SIN):
2359 CASE_FLT_FN (BUILT_IN_COS):
2360 builtin_optab = sincos_optab; break;
2361 default:
2362 gcc_unreachable ();
2365 /* Make a suitable register to place result in. */
2366 mode = TYPE_MODE (TREE_TYPE (exp));
2368 /* Check if sincos insn is available, otherwise fallback
2369 to sin or cos insn. */
2370 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2371 switch (DECL_FUNCTION_CODE (fndecl))
2373 CASE_FLT_FN (BUILT_IN_SIN):
2374 builtin_optab = sin_optab; break;
2375 CASE_FLT_FN (BUILT_IN_COS):
2376 builtin_optab = cos_optab; break;
2377 default:
2378 gcc_unreachable ();
2381 /* Before working hard, check whether the instruction is available. */
2382 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2384 rtx result = gen_reg_rtx (mode);
2386 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2387 need to expand the argument again. This way, we will not perform
2388 side-effects more the once. */
2389 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2391 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2393 start_sequence ();
2395 /* Compute into RESULT.
2396 Set RESULT to wherever the result comes back. */
2397 if (builtin_optab == sincos_optab)
2399 int ok;
2401 switch (DECL_FUNCTION_CODE (fndecl))
2403 CASE_FLT_FN (BUILT_IN_SIN):
2404 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2405 break;
2406 CASE_FLT_FN (BUILT_IN_COS):
2407 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2408 break;
2409 default:
2410 gcc_unreachable ();
2412 gcc_assert (ok);
2414 else
2415 result = expand_unop (mode, builtin_optab, op0, result, 0);
2417 if (result != 0)
2419 /* Output the entire sequence. */
2420 insns = get_insns ();
2421 end_sequence ();
2422 emit_insn (insns);
2423 return result;
2426 /* If we were unable to expand via the builtin, stop the sequence
2427 (without outputting the insns) and call to the library function
2428 with the stabilized argument list. */
2429 end_sequence ();
2432 return expand_call (exp, target, target == const0_rtx);
2435 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2436 return an RTL instruction code that implements the functionality.
2437 If that isn't possible or available return CODE_FOR_nothing. */
2439 static enum insn_code
2440 interclass_mathfn_icode (tree arg, tree fndecl)
2442 bool errno_set = false;
2443 optab builtin_optab = unknown_optab;
2444 machine_mode mode;
2446 switch (DECL_FUNCTION_CODE (fndecl))
2448 CASE_FLT_FN (BUILT_IN_ILOGB):
2449 errno_set = true; builtin_optab = ilogb_optab; break;
2450 CASE_FLT_FN (BUILT_IN_ISINF):
2451 builtin_optab = isinf_optab; break;
2452 case BUILT_IN_ISNORMAL:
2453 case BUILT_IN_ISFINITE:
2454 CASE_FLT_FN (BUILT_IN_FINITE):
2455 case BUILT_IN_FINITED32:
2456 case BUILT_IN_FINITED64:
2457 case BUILT_IN_FINITED128:
2458 case BUILT_IN_ISINFD32:
2459 case BUILT_IN_ISINFD64:
2460 case BUILT_IN_ISINFD128:
2461 /* These builtins have no optabs (yet). */
2462 break;
2463 default:
2464 gcc_unreachable ();
2467 /* There's no easy way to detect the case we need to set EDOM. */
2468 if (flag_errno_math && errno_set)
2469 return CODE_FOR_nothing;
2471 /* Optab mode depends on the mode of the input argument. */
2472 mode = TYPE_MODE (TREE_TYPE (arg));
2474 if (builtin_optab)
2475 return optab_handler (builtin_optab, mode);
2476 return CODE_FOR_nothing;
2479 /* Expand a call to one of the builtin math functions that operate on
2480 floating point argument and output an integer result (ilogb, isinf,
2481 isnan, etc).
2482 Return 0 if a normal call should be emitted rather than expanding the
2483 function in-line. EXP is the expression that is a call to the builtin
2484 function; if convenient, the result should be placed in TARGET. */
2486 static rtx
2487 expand_builtin_interclass_mathfn (tree exp, rtx target)
2489 enum insn_code icode = CODE_FOR_nothing;
2490 rtx op0;
2491 tree fndecl = get_callee_fndecl (exp);
2492 machine_mode mode;
2493 tree arg;
2495 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2496 return NULL_RTX;
2498 arg = CALL_EXPR_ARG (exp, 0);
2499 icode = interclass_mathfn_icode (arg, fndecl);
2500 mode = TYPE_MODE (TREE_TYPE (arg));
2502 if (icode != CODE_FOR_nothing)
2504 struct expand_operand ops[1];
2505 rtx_insn *last = get_last_insn ();
2506 tree orig_arg = arg;
2508 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2509 need to expand the argument again. This way, we will not perform
2510 side-effects more the once. */
2511 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2513 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2515 if (mode != GET_MODE (op0))
2516 op0 = convert_to_mode (mode, op0, 0);
2518 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2519 if (maybe_legitimize_operands (icode, 0, 1, ops)
2520 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2521 return ops[0].value;
2523 delete_insns_since (last);
2524 CALL_EXPR_ARG (exp, 0) = orig_arg;
2527 return NULL_RTX;
2530 /* Expand a call to the builtin sincos math function.
2531 Return NULL_RTX if a normal call should be emitted rather than expanding the
2532 function in-line. EXP is the expression that is a call to the builtin
2533 function. */
2535 static rtx
2536 expand_builtin_sincos (tree exp)
2538 rtx op0, op1, op2, target1, target2;
2539 machine_mode mode;
2540 tree arg, sinp, cosp;
2541 int result;
2542 location_t loc = EXPR_LOCATION (exp);
2543 tree alias_type, alias_off;
2545 if (!validate_arglist (exp, REAL_TYPE,
2546 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2547 return NULL_RTX;
2549 arg = CALL_EXPR_ARG (exp, 0);
2550 sinp = CALL_EXPR_ARG (exp, 1);
2551 cosp = CALL_EXPR_ARG (exp, 2);
2553 /* Make a suitable register to place result in. */
2554 mode = TYPE_MODE (TREE_TYPE (arg));
2556 /* Check if sincos insn is available, otherwise emit the call. */
2557 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2558 return NULL_RTX;
2560 target1 = gen_reg_rtx (mode);
2561 target2 = gen_reg_rtx (mode);
2563 op0 = expand_normal (arg);
2564 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2565 alias_off = build_int_cst (alias_type, 0);
2566 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2567 sinp, alias_off));
2568 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2569 cosp, alias_off));
2571 /* Compute into target1 and target2.
2572 Set TARGET to wherever the result comes back. */
2573 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2574 gcc_assert (result);
2576 /* Move target1 and target2 to the memory locations indicated
2577 by op1 and op2. */
2578 emit_move_insn (op1, target1);
2579 emit_move_insn (op2, target2);
2581 return const0_rtx;
2584 /* Expand a call to the internal cexpi builtin to the sincos math function.
2585 EXP is the expression that is a call to the builtin function; if convenient,
2586 the result should be placed in TARGET. */
2588 static rtx
2589 expand_builtin_cexpi (tree exp, rtx target)
2591 tree fndecl = get_callee_fndecl (exp);
2592 tree arg, type;
2593 machine_mode mode;
2594 rtx op0, op1, op2;
2595 location_t loc = EXPR_LOCATION (exp);
2597 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2598 return NULL_RTX;
2600 arg = CALL_EXPR_ARG (exp, 0);
2601 type = TREE_TYPE (arg);
2602 mode = TYPE_MODE (TREE_TYPE (arg));
2604 /* Try expanding via a sincos optab, fall back to emitting a libcall
2605 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2606 is only generated from sincos, cexp or if we have either of them. */
2607 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2609 op1 = gen_reg_rtx (mode);
2610 op2 = gen_reg_rtx (mode);
2612 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2614 /* Compute into op1 and op2. */
2615 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2617 else if (targetm.libc_has_function (function_sincos))
2619 tree call, fn = NULL_TREE;
2620 tree top1, top2;
2621 rtx op1a, op2a;
2623 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2624 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2625 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2626 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2627 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2628 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2629 else
2630 gcc_unreachable ();
2632 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2633 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2634 op1a = copy_addr_to_reg (XEXP (op1, 0));
2635 op2a = copy_addr_to_reg (XEXP (op2, 0));
2636 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2637 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2639 /* Make sure not to fold the sincos call again. */
2640 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2641 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2642 call, 3, arg, top1, top2));
2644 else
2646 tree call, fn = NULL_TREE, narg;
2647 tree ctype = build_complex_type (type);
2649 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2650 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2651 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2652 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2653 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2654 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2655 else
2656 gcc_unreachable ();
2658 /* If we don't have a decl for cexp create one. This is the
2659 friendliest fallback if the user calls __builtin_cexpi
2660 without full target C99 function support. */
2661 if (fn == NULL_TREE)
2663 tree fntype;
2664 const char *name = NULL;
2666 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2667 name = "cexpf";
2668 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2669 name = "cexp";
2670 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2671 name = "cexpl";
2673 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2674 fn = build_fn_decl (name, fntype);
2677 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2678 build_real (type, dconst0), arg);
2680 /* Make sure not to fold the cexp call again. */
2681 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2682 return expand_expr (build_call_nary (ctype, call, 1, narg),
2683 target, VOIDmode, EXPAND_NORMAL);
2686 /* Now build the proper return type. */
2687 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2688 make_tree (TREE_TYPE (arg), op2),
2689 make_tree (TREE_TYPE (arg), op1)),
2690 target, VOIDmode, EXPAND_NORMAL);
2693 /* Conveniently construct a function call expression. FNDECL names the
2694 function to be called, N is the number of arguments, and the "..."
2695 parameters are the argument expressions. Unlike build_call_exr
2696 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2698 static tree
2699 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2701 va_list ap;
2702 tree fntype = TREE_TYPE (fndecl);
2703 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2705 va_start (ap, n);
2706 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2707 va_end (ap);
2708 SET_EXPR_LOCATION (fn, loc);
2709 return fn;
2712 /* Expand a call to one of the builtin rounding functions gcc defines
2713 as an extension (lfloor and lceil). As these are gcc extensions we
2714 do not need to worry about setting errno to EDOM.
2715 If expanding via optab fails, lower expression to (int)(floor(x)).
2716 EXP is the expression that is a call to the builtin function;
2717 if convenient, the result should be placed in TARGET. */
2719 static rtx
2720 expand_builtin_int_roundingfn (tree exp, rtx target)
2722 convert_optab builtin_optab;
2723 rtx op0, tmp;
2724 rtx_insn *insns;
2725 tree fndecl = get_callee_fndecl (exp);
2726 enum built_in_function fallback_fn;
2727 tree fallback_fndecl;
2728 machine_mode mode;
2729 tree arg;
2731 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2732 gcc_unreachable ();
2734 arg = CALL_EXPR_ARG (exp, 0);
2736 switch (DECL_FUNCTION_CODE (fndecl))
2738 CASE_FLT_FN (BUILT_IN_ICEIL):
2739 CASE_FLT_FN (BUILT_IN_LCEIL):
2740 CASE_FLT_FN (BUILT_IN_LLCEIL):
2741 builtin_optab = lceil_optab;
2742 fallback_fn = BUILT_IN_CEIL;
2743 break;
2745 CASE_FLT_FN (BUILT_IN_IFLOOR):
2746 CASE_FLT_FN (BUILT_IN_LFLOOR):
2747 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2748 builtin_optab = lfloor_optab;
2749 fallback_fn = BUILT_IN_FLOOR;
2750 break;
2752 default:
2753 gcc_unreachable ();
2756 /* Make a suitable register to place result in. */
2757 mode = TYPE_MODE (TREE_TYPE (exp));
2759 target = gen_reg_rtx (mode);
2761 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2762 need to expand the argument again. This way, we will not perform
2763 side-effects more the once. */
2764 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2766 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2768 start_sequence ();
2770 /* Compute into TARGET. */
2771 if (expand_sfix_optab (target, op0, builtin_optab))
2773 /* Output the entire sequence. */
2774 insns = get_insns ();
2775 end_sequence ();
2776 emit_insn (insns);
2777 return target;
2780 /* If we were unable to expand via the builtin, stop the sequence
2781 (without outputting the insns). */
2782 end_sequence ();
2784 /* Fall back to floating point rounding optab. */
2785 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2787 /* For non-C99 targets we may end up without a fallback fndecl here
2788 if the user called __builtin_lfloor directly. In this case emit
2789 a call to the floor/ceil variants nevertheless. This should result
2790 in the best user experience for not full C99 targets. */
2791 if (fallback_fndecl == NULL_TREE)
2793 tree fntype;
2794 const char *name = NULL;
2796 switch (DECL_FUNCTION_CODE (fndecl))
2798 case BUILT_IN_ICEIL:
2799 case BUILT_IN_LCEIL:
2800 case BUILT_IN_LLCEIL:
2801 name = "ceil";
2802 break;
2803 case BUILT_IN_ICEILF:
2804 case BUILT_IN_LCEILF:
2805 case BUILT_IN_LLCEILF:
2806 name = "ceilf";
2807 break;
2808 case BUILT_IN_ICEILL:
2809 case BUILT_IN_LCEILL:
2810 case BUILT_IN_LLCEILL:
2811 name = "ceill";
2812 break;
2813 case BUILT_IN_IFLOOR:
2814 case BUILT_IN_LFLOOR:
2815 case BUILT_IN_LLFLOOR:
2816 name = "floor";
2817 break;
2818 case BUILT_IN_IFLOORF:
2819 case BUILT_IN_LFLOORF:
2820 case BUILT_IN_LLFLOORF:
2821 name = "floorf";
2822 break;
2823 case BUILT_IN_IFLOORL:
2824 case BUILT_IN_LFLOORL:
2825 case BUILT_IN_LLFLOORL:
2826 name = "floorl";
2827 break;
2828 default:
2829 gcc_unreachable ();
2832 fntype = build_function_type_list (TREE_TYPE (arg),
2833 TREE_TYPE (arg), NULL_TREE);
2834 fallback_fndecl = build_fn_decl (name, fntype);
2837 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2839 tmp = expand_normal (exp);
2840 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2842 /* Truncate the result of floating point optab to integer
2843 via expand_fix (). */
2844 target = gen_reg_rtx (mode);
2845 expand_fix (target, tmp, 0);
2847 return target;
2850 /* Expand a call to one of the builtin math functions doing integer
2851 conversion (lrint).
2852 Return 0 if a normal call should be emitted rather than expanding the
2853 function in-line. EXP is the expression that is a call to the builtin
2854 function; if convenient, the result should be placed in TARGET. */
2856 static rtx
2857 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2859 convert_optab builtin_optab;
2860 rtx op0;
2861 rtx_insn *insns;
2862 tree fndecl = get_callee_fndecl (exp);
2863 tree arg;
2864 machine_mode mode;
2865 enum built_in_function fallback_fn = BUILT_IN_NONE;
2867 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2868 gcc_unreachable ();
2870 arg = CALL_EXPR_ARG (exp, 0);
2872 switch (DECL_FUNCTION_CODE (fndecl))
2874 CASE_FLT_FN (BUILT_IN_IRINT):
2875 fallback_fn = BUILT_IN_LRINT;
2876 /* FALLTHRU */
2877 CASE_FLT_FN (BUILT_IN_LRINT):
2878 CASE_FLT_FN (BUILT_IN_LLRINT):
2879 builtin_optab = lrint_optab;
2880 break;
2882 CASE_FLT_FN (BUILT_IN_IROUND):
2883 fallback_fn = BUILT_IN_LROUND;
2884 /* FALLTHRU */
2885 CASE_FLT_FN (BUILT_IN_LROUND):
2886 CASE_FLT_FN (BUILT_IN_LLROUND):
2887 builtin_optab = lround_optab;
2888 break;
2890 default:
2891 gcc_unreachable ();
2894 /* There's no easy way to detect the case we need to set EDOM. */
2895 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2896 return NULL_RTX;
2898 /* Make a suitable register to place result in. */
2899 mode = TYPE_MODE (TREE_TYPE (exp));
2901 /* There's no easy way to detect the case we need to set EDOM. */
2902 if (!flag_errno_math)
2904 rtx result = gen_reg_rtx (mode);
2906 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2907 need to expand the argument again. This way, we will not perform
2908 side-effects more the once. */
2909 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2911 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2913 start_sequence ();
2915 if (expand_sfix_optab (result, op0, builtin_optab))
2917 /* Output the entire sequence. */
2918 insns = get_insns ();
2919 end_sequence ();
2920 emit_insn (insns);
2921 return result;
2924 /* If we were unable to expand via the builtin, stop the sequence
2925 (without outputting the insns) and call to the library function
2926 with the stabilized argument list. */
2927 end_sequence ();
2930 if (fallback_fn != BUILT_IN_NONE)
2932 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2933 targets, (int) round (x) should never be transformed into
2934 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2935 a call to lround in the hope that the target provides at least some
2936 C99 functions. This should result in the best user experience for
2937 not full C99 targets. */
2938 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2939 fallback_fn, 0);
2941 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2942 fallback_fndecl, 1, arg);
2944 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2945 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2946 return convert_to_mode (mode, target, 0);
2949 return expand_call (exp, target, target == const0_rtx);
2952 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2953 a normal call should be emitted rather than expanding the function
2954 in-line. EXP is the expression that is a call to the builtin
2955 function; if convenient, the result should be placed in TARGET. */
2957 static rtx
2958 expand_builtin_powi (tree exp, rtx target)
2960 tree arg0, arg1;
2961 rtx op0, op1;
2962 machine_mode mode;
2963 machine_mode mode2;
2965 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2966 return NULL_RTX;
2968 arg0 = CALL_EXPR_ARG (exp, 0);
2969 arg1 = CALL_EXPR_ARG (exp, 1);
2970 mode = TYPE_MODE (TREE_TYPE (exp));
2972 /* Emit a libcall to libgcc. */
2974 /* Mode of the 2nd argument must match that of an int. */
2975 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2977 if (target == NULL_RTX)
2978 target = gen_reg_rtx (mode);
2980 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2981 if (GET_MODE (op0) != mode)
2982 op0 = convert_to_mode (mode, op0, 0);
2983 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2984 if (GET_MODE (op1) != mode2)
2985 op1 = convert_to_mode (mode2, op1, 0);
2987 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2988 target, LCT_CONST, mode, 2,
2989 op0, mode, op1, mode2);
2991 return target;
2994 /* Expand expression EXP which is a call to the strlen builtin. Return
2995 NULL_RTX if we failed the caller should emit a normal call, otherwise
2996 try to get the result in TARGET, if convenient. */
2998 static rtx
2999 expand_builtin_strlen (tree exp, rtx target,
3000 machine_mode target_mode)
3002 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3003 return NULL_RTX;
3004 else
3006 struct expand_operand ops[4];
3007 rtx pat;
3008 tree len;
3009 tree src = CALL_EXPR_ARG (exp, 0);
3010 rtx src_reg;
3011 rtx_insn *before_strlen;
3012 machine_mode insn_mode = target_mode;
3013 enum insn_code icode = CODE_FOR_nothing;
3014 unsigned int align;
3016 /* If the length can be computed at compile-time, return it. */
3017 len = c_strlen (src, 0);
3018 if (len)
3019 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3021 /* If the length can be computed at compile-time and is constant
3022 integer, but there are side-effects in src, evaluate
3023 src for side-effects, then return len.
3024 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3025 can be optimized into: i++; x = 3; */
3026 len = c_strlen (src, 1);
3027 if (len && TREE_CODE (len) == INTEGER_CST)
3029 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3030 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3033 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3035 /* If SRC is not a pointer type, don't do this operation inline. */
3036 if (align == 0)
3037 return NULL_RTX;
3039 /* Bail out if we can't compute strlen in the right mode. */
3040 while (insn_mode != VOIDmode)
3042 icode = optab_handler (strlen_optab, insn_mode);
3043 if (icode != CODE_FOR_nothing)
3044 break;
3046 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3048 if (insn_mode == VOIDmode)
3049 return NULL_RTX;
3051 /* Make a place to hold the source address. We will not expand
3052 the actual source until we are sure that the expansion will
3053 not fail -- there are trees that cannot be expanded twice. */
3054 src_reg = gen_reg_rtx (Pmode);
3056 /* Mark the beginning of the strlen sequence so we can emit the
3057 source operand later. */
3058 before_strlen = get_last_insn ();
3060 create_output_operand (&ops[0], target, insn_mode);
3061 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3062 create_integer_operand (&ops[2], 0);
3063 create_integer_operand (&ops[3], align);
3064 if (!maybe_expand_insn (icode, 4, ops))
3065 return NULL_RTX;
3067 /* Now that we are assured of success, expand the source. */
3068 start_sequence ();
3069 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3070 if (pat != src_reg)
3072 #ifdef POINTERS_EXTEND_UNSIGNED
3073 if (GET_MODE (pat) != Pmode)
3074 pat = convert_to_mode (Pmode, pat,
3075 POINTERS_EXTEND_UNSIGNED);
3076 #endif
3077 emit_move_insn (src_reg, pat);
3079 pat = get_insns ();
3080 end_sequence ();
3082 if (before_strlen)
3083 emit_insn_after (pat, before_strlen);
3084 else
3085 emit_insn_before (pat, get_insns ());
3087 /* Return the value in the proper mode for this function. */
3088 if (GET_MODE (ops[0].value) == target_mode)
3089 target = ops[0].value;
3090 else if (target != 0)
3091 convert_move (target, ops[0].value, 0);
3092 else
3093 target = convert_to_mode (target_mode, ops[0].value, 0);
3095 return target;
3099 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3100 bytes from constant string DATA + OFFSET and return it as target
3101 constant. */
3103 static rtx
3104 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3105 machine_mode mode)
3107 const char *str = (const char *) data;
3109 gcc_assert (offset >= 0
3110 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3111 <= strlen (str) + 1));
3113 return c_readstr (str + offset, mode);
3116 /* LEN specify length of the block of memcpy/memset operation.
3117 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3118 In some cases we can make very likely guess on max size, then we
3119 set it into PROBABLE_MAX_SIZE. */
3121 static void
3122 determine_block_size (tree len, rtx len_rtx,
3123 unsigned HOST_WIDE_INT *min_size,
3124 unsigned HOST_WIDE_INT *max_size,
3125 unsigned HOST_WIDE_INT *probable_max_size)
3127 if (CONST_INT_P (len_rtx))
3129 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3130 return;
3132 else
3134 wide_int min, max;
3135 enum value_range_type range_type = VR_UNDEFINED;
3137 /* Determine bounds from the type. */
3138 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3139 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3140 else
3141 *min_size = 0;
3142 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3143 *probable_max_size = *max_size
3144 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3145 else
3146 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3148 if (TREE_CODE (len) == SSA_NAME)
3149 range_type = get_range_info (len, &min, &max);
3150 if (range_type == VR_RANGE)
3152 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3153 *min_size = min.to_uhwi ();
3154 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3155 *probable_max_size = *max_size = max.to_uhwi ();
3157 else if (range_type == VR_ANTI_RANGE)
3159 /* Anti range 0...N lets us to determine minimal size to N+1. */
3160 if (min == 0)
3162 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
3163 *min_size = max.to_uhwi () + 1;
3165 /* Code like
3167 int n;
3168 if (n < 100)
3169 memcpy (a, b, n)
3171 Produce anti range allowing negative values of N. We still
3172 can use the information and make a guess that N is not negative.
3174 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3175 *probable_max_size = min.to_uhwi () - 1;
3178 gcc_checking_assert (*max_size <=
3179 (unsigned HOST_WIDE_INT)
3180 GET_MODE_MASK (GET_MODE (len_rtx)));
3183 /* Helper function to do the actual work for expand_builtin_memcpy. */
3185 static rtx
3186 expand_builtin_memcpy_args (tree dest, tree src, tree len, rtx target, tree exp)
3188 const char *src_str;
3189 unsigned int src_align = get_pointer_alignment (src);
3190 unsigned int dest_align = get_pointer_alignment (dest);
3191 rtx dest_mem, src_mem, dest_addr, len_rtx;
3192 HOST_WIDE_INT expected_size = -1;
3193 unsigned int expected_align = 0;
3194 unsigned HOST_WIDE_INT min_size;
3195 unsigned HOST_WIDE_INT max_size;
3196 unsigned HOST_WIDE_INT probable_max_size;
3198 /* If DEST is not a pointer type, call the normal function. */
3199 if (dest_align == 0)
3200 return NULL_RTX;
3202 /* If either SRC is not a pointer type, don't do this
3203 operation in-line. */
3204 if (src_align == 0)
3205 return NULL_RTX;
3207 if (currently_expanding_gimple_stmt)
3208 stringop_block_profile (currently_expanding_gimple_stmt,
3209 &expected_align, &expected_size);
3211 if (expected_align < dest_align)
3212 expected_align = dest_align;
3213 dest_mem = get_memory_rtx (dest, len);
3214 set_mem_align (dest_mem, dest_align);
3215 len_rtx = expand_normal (len);
3216 determine_block_size (len, len_rtx, &min_size, &max_size,
3217 &probable_max_size);
3218 src_str = c_getstr (src);
3220 /* If SRC is a string constant and block move would be done
3221 by pieces, we can avoid loading the string from memory
3222 and only stored the computed constants. */
3223 if (src_str
3224 && CONST_INT_P (len_rtx)
3225 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3226 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3227 CONST_CAST (char *, src_str),
3228 dest_align, false))
3230 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3231 builtin_memcpy_read_str,
3232 CONST_CAST (char *, src_str),
3233 dest_align, false, 0);
3234 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3235 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3236 return dest_mem;
3239 src_mem = get_memory_rtx (src, len);
3240 set_mem_align (src_mem, src_align);
3242 /* Copy word part most expediently. */
3243 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3244 CALL_EXPR_TAILCALL (exp)
3245 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3246 expected_align, expected_size,
3247 min_size, max_size, probable_max_size);
3249 if (dest_addr == 0)
3251 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3252 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3255 return dest_addr;
3258 /* Expand a call EXP to the memcpy builtin.
3259 Return NULL_RTX if we failed, the caller should emit a normal call,
3260 otherwise try to get the result in TARGET, if convenient (and in
3261 mode MODE if that's convenient). */
3263 static rtx
3264 expand_builtin_memcpy (tree exp, rtx target)
3266 if (!validate_arglist (exp,
3267 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3268 return NULL_RTX;
3269 else
3271 tree dest = CALL_EXPR_ARG (exp, 0);
3272 tree src = CALL_EXPR_ARG (exp, 1);
3273 tree len = CALL_EXPR_ARG (exp, 2);
3274 return expand_builtin_memcpy_args (dest, src, len, target, exp);
3278 /* Expand an instrumented call EXP to the memcpy builtin.
3279 Return NULL_RTX if we failed, the caller should emit a normal call,
3280 otherwise try to get the result in TARGET, if convenient (and in
3281 mode MODE if that's convenient). */
3283 static rtx
3284 expand_builtin_memcpy_with_bounds (tree exp, rtx target)
3286 if (!validate_arglist (exp,
3287 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3288 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3289 INTEGER_TYPE, VOID_TYPE))
3290 return NULL_RTX;
3291 else
3293 tree dest = CALL_EXPR_ARG (exp, 0);
3294 tree src = CALL_EXPR_ARG (exp, 2);
3295 tree len = CALL_EXPR_ARG (exp, 4);
3296 rtx res = expand_builtin_memcpy_args (dest, src, len, target, exp);
3298 /* Return src bounds with the result. */
3299 if (res)
3301 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3302 expand_normal (CALL_EXPR_ARG (exp, 1)));
3303 res = chkp_join_splitted_slot (res, bnd);
3305 return res;
3309 /* Expand a call EXP to the mempcpy builtin.
3310 Return NULL_RTX if we failed; the caller should emit a normal call,
3311 otherwise try to get the result in TARGET, if convenient (and in
3312 mode MODE if that's convenient). If ENDP is 0 return the
3313 destination pointer, if ENDP is 1 return the end pointer ala
3314 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3315 stpcpy. */
3317 static rtx
3318 expand_builtin_mempcpy (tree exp, rtx target, machine_mode mode)
3320 if (!validate_arglist (exp,
3321 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3322 return NULL_RTX;
3323 else
3325 tree dest = CALL_EXPR_ARG (exp, 0);
3326 tree src = CALL_EXPR_ARG (exp, 1);
3327 tree len = CALL_EXPR_ARG (exp, 2);
3328 return expand_builtin_mempcpy_args (dest, src, len,
3329 target, mode, /*endp=*/ 1,
3330 exp);
3334 /* Expand an instrumented call EXP to the mempcpy builtin.
3335 Return NULL_RTX if we failed, the caller should emit a normal call,
3336 otherwise try to get the result in TARGET, if convenient (and in
3337 mode MODE if that's convenient). */
3339 static rtx
3340 expand_builtin_mempcpy_with_bounds (tree exp, rtx target, machine_mode mode)
3342 if (!validate_arglist (exp,
3343 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3344 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3345 INTEGER_TYPE, VOID_TYPE))
3346 return NULL_RTX;
3347 else
3349 tree dest = CALL_EXPR_ARG (exp, 0);
3350 tree src = CALL_EXPR_ARG (exp, 2);
3351 tree len = CALL_EXPR_ARG (exp, 4);
3352 rtx res = expand_builtin_mempcpy_args (dest, src, len, target,
3353 mode, 1, exp);
3355 /* Return src bounds with the result. */
3356 if (res)
3358 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3359 expand_normal (CALL_EXPR_ARG (exp, 1)));
3360 res = chkp_join_splitted_slot (res, bnd);
3362 return res;
3366 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3367 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3368 so that this can also be called without constructing an actual CALL_EXPR.
3369 The other arguments and return value are the same as for
3370 expand_builtin_mempcpy. */
3372 static rtx
3373 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3374 rtx target, machine_mode mode, int endp,
3375 tree orig_exp)
3377 tree fndecl = get_callee_fndecl (orig_exp);
3379 /* If return value is ignored, transform mempcpy into memcpy. */
3380 if (target == const0_rtx
3381 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
3382 && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP))
3384 tree fn = builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP);
3385 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3386 dest, src, len);
3387 return expand_expr (result, target, mode, EXPAND_NORMAL);
3389 else if (target == const0_rtx
3390 && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3392 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3393 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3394 dest, src, len);
3395 return expand_expr (result, target, mode, EXPAND_NORMAL);
3397 else
3399 const char *src_str;
3400 unsigned int src_align = get_pointer_alignment (src);
3401 unsigned int dest_align = get_pointer_alignment (dest);
3402 rtx dest_mem, src_mem, len_rtx;
3404 /* If either SRC or DEST is not a pointer type, don't do this
3405 operation in-line. */
3406 if (dest_align == 0 || src_align == 0)
3407 return NULL_RTX;
3409 /* If LEN is not constant, call the normal function. */
3410 if (! tree_fits_uhwi_p (len))
3411 return NULL_RTX;
3413 len_rtx = expand_normal (len);
3414 src_str = c_getstr (src);
3416 /* If SRC is a string constant and block move would be done
3417 by pieces, we can avoid loading the string from memory
3418 and only stored the computed constants. */
3419 if (src_str
3420 && CONST_INT_P (len_rtx)
3421 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3422 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3423 CONST_CAST (char *, src_str),
3424 dest_align, false))
3426 dest_mem = get_memory_rtx (dest, len);
3427 set_mem_align (dest_mem, dest_align);
3428 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3429 builtin_memcpy_read_str,
3430 CONST_CAST (char *, src_str),
3431 dest_align, false, endp);
3432 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3433 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3434 return dest_mem;
3437 if (CONST_INT_P (len_rtx)
3438 && can_move_by_pieces (INTVAL (len_rtx),
3439 MIN (dest_align, src_align)))
3441 dest_mem = get_memory_rtx (dest, len);
3442 set_mem_align (dest_mem, dest_align);
3443 src_mem = get_memory_rtx (src, len);
3444 set_mem_align (src_mem, src_align);
3445 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3446 MIN (dest_align, src_align), endp);
3447 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3448 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3449 return dest_mem;
3452 return NULL_RTX;
3456 #ifndef HAVE_movstr
3457 # define HAVE_movstr 0
3458 # define CODE_FOR_movstr CODE_FOR_nothing
3459 #endif
3461 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3462 we failed, the caller should emit a normal call, otherwise try to
3463 get the result in TARGET, if convenient. If ENDP is 0 return the
3464 destination pointer, if ENDP is 1 return the end pointer ala
3465 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3466 stpcpy. */
3468 static rtx
3469 expand_movstr (tree dest, tree src, rtx target, int endp)
3471 struct expand_operand ops[3];
3472 rtx dest_mem;
3473 rtx src_mem;
3475 if (!HAVE_movstr)
3476 return NULL_RTX;
3478 dest_mem = get_memory_rtx (dest, NULL);
3479 src_mem = get_memory_rtx (src, NULL);
3480 if (!endp)
3482 target = force_reg (Pmode, XEXP (dest_mem, 0));
3483 dest_mem = replace_equiv_address (dest_mem, target);
3486 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3487 create_fixed_operand (&ops[1], dest_mem);
3488 create_fixed_operand (&ops[2], src_mem);
3489 if (!maybe_expand_insn (CODE_FOR_movstr, 3, ops))
3490 return NULL_RTX;
3492 if (endp && target != const0_rtx)
3494 target = ops[0].value;
3495 /* movstr is supposed to set end to the address of the NUL
3496 terminator. If the caller requested a mempcpy-like return value,
3497 adjust it. */
3498 if (endp == 1)
3500 rtx tem = plus_constant (GET_MODE (target),
3501 gen_lowpart (GET_MODE (target), target), 1);
3502 emit_move_insn (target, force_operand (tem, NULL_RTX));
3505 return target;
3508 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3509 NULL_RTX if we failed the caller should emit a normal call, otherwise
3510 try to get the result in TARGET, if convenient (and in mode MODE if that's
3511 convenient). */
3513 static rtx
3514 expand_builtin_strcpy (tree exp, rtx target)
3516 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3518 tree dest = CALL_EXPR_ARG (exp, 0);
3519 tree src = CALL_EXPR_ARG (exp, 1);
3520 return expand_builtin_strcpy_args (dest, src, target);
3522 return NULL_RTX;
3525 /* Helper function to do the actual work for expand_builtin_strcpy. The
3526 arguments to the builtin_strcpy call DEST and SRC are broken out
3527 so that this can also be called without constructing an actual CALL_EXPR.
3528 The other arguments and return value are the same as for
3529 expand_builtin_strcpy. */
3531 static rtx
3532 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3534 return expand_movstr (dest, src, target, /*endp=*/0);
3537 /* Expand a call EXP to the stpcpy builtin.
3538 Return NULL_RTX if we failed the caller should emit a normal call,
3539 otherwise try to get the result in TARGET, if convenient (and in
3540 mode MODE if that's convenient). */
3542 static rtx
3543 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3545 tree dst, src;
3546 location_t loc = EXPR_LOCATION (exp);
3548 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3549 return NULL_RTX;
3551 dst = CALL_EXPR_ARG (exp, 0);
3552 src = CALL_EXPR_ARG (exp, 1);
3554 /* If return value is ignored, transform stpcpy into strcpy. */
3555 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3557 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3558 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3559 return expand_expr (result, target, mode, EXPAND_NORMAL);
3561 else
3563 tree len, lenp1;
3564 rtx ret;
3566 /* Ensure we get an actual string whose length can be evaluated at
3567 compile-time, not an expression containing a string. This is
3568 because the latter will potentially produce pessimized code
3569 when used to produce the return value. */
3570 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3571 return expand_movstr (dst, src, target, /*endp=*/2);
3573 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3574 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3575 target, mode, /*endp=*/2,
3576 exp);
3578 if (ret)
3579 return ret;
3581 if (TREE_CODE (len) == INTEGER_CST)
3583 rtx len_rtx = expand_normal (len);
3585 if (CONST_INT_P (len_rtx))
3587 ret = expand_builtin_strcpy_args (dst, src, target);
3589 if (ret)
3591 if (! target)
3593 if (mode != VOIDmode)
3594 target = gen_reg_rtx (mode);
3595 else
3596 target = gen_reg_rtx (GET_MODE (ret));
3598 if (GET_MODE (target) != GET_MODE (ret))
3599 ret = gen_lowpart (GET_MODE (target), ret);
3601 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3602 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3603 gcc_assert (ret);
3605 return target;
3610 return expand_movstr (dst, src, target, /*endp=*/2);
3614 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3615 bytes from constant string DATA + OFFSET and return it as target
3616 constant. */
3619 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3620 machine_mode mode)
3622 const char *str = (const char *) data;
3624 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3625 return const0_rtx;
3627 return c_readstr (str + offset, mode);
3630 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3631 NULL_RTX if we failed the caller should emit a normal call. */
3633 static rtx
3634 expand_builtin_strncpy (tree exp, rtx target)
3636 location_t loc = EXPR_LOCATION (exp);
3638 if (validate_arglist (exp,
3639 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3641 tree dest = CALL_EXPR_ARG (exp, 0);
3642 tree src = CALL_EXPR_ARG (exp, 1);
3643 tree len = CALL_EXPR_ARG (exp, 2);
3644 tree slen = c_strlen (src, 1);
3646 /* We must be passed a constant len and src parameter. */
3647 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3648 return NULL_RTX;
3650 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3652 /* We're required to pad with trailing zeros if the requested
3653 len is greater than strlen(s2)+1. In that case try to
3654 use store_by_pieces, if it fails, punt. */
3655 if (tree_int_cst_lt (slen, len))
3657 unsigned int dest_align = get_pointer_alignment (dest);
3658 const char *p = c_getstr (src);
3659 rtx dest_mem;
3661 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3662 || !can_store_by_pieces (tree_to_uhwi (len),
3663 builtin_strncpy_read_str,
3664 CONST_CAST (char *, p),
3665 dest_align, false))
3666 return NULL_RTX;
3668 dest_mem = get_memory_rtx (dest, len);
3669 store_by_pieces (dest_mem, tree_to_uhwi (len),
3670 builtin_strncpy_read_str,
3671 CONST_CAST (char *, p), dest_align, false, 0);
3672 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3673 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3674 return dest_mem;
3677 return NULL_RTX;
3680 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3681 bytes from constant string DATA + OFFSET and return it as target
3682 constant. */
3685 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3686 machine_mode mode)
3688 const char *c = (const char *) data;
3689 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3691 memset (p, *c, GET_MODE_SIZE (mode));
3693 return c_readstr (p, mode);
3696 /* Callback routine for store_by_pieces. Return the RTL of a register
3697 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3698 char value given in the RTL register data. For example, if mode is
3699 4 bytes wide, return the RTL for 0x01010101*data. */
3701 static rtx
3702 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3703 machine_mode mode)
3705 rtx target, coeff;
3706 size_t size;
3707 char *p;
3709 size = GET_MODE_SIZE (mode);
3710 if (size == 1)
3711 return (rtx) data;
3713 p = XALLOCAVEC (char, size);
3714 memset (p, 1, size);
3715 coeff = c_readstr (p, mode);
3717 target = convert_to_mode (mode, (rtx) data, 1);
3718 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3719 return force_reg (mode, target);
3722 /* Expand expression EXP, which is a call to the memset builtin. Return
3723 NULL_RTX if we failed the caller should emit a normal call, otherwise
3724 try to get the result in TARGET, if convenient (and in mode MODE if that's
3725 convenient). */
3727 static rtx
3728 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
3730 if (!validate_arglist (exp,
3731 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3732 return NULL_RTX;
3733 else
3735 tree dest = CALL_EXPR_ARG (exp, 0);
3736 tree val = CALL_EXPR_ARG (exp, 1);
3737 tree len = CALL_EXPR_ARG (exp, 2);
3738 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3742 /* Expand expression EXP, which is an instrumented call to the memset builtin.
3743 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
3744 try to get the result in TARGET, if convenient (and in mode MODE if that's
3745 convenient). */
3747 static rtx
3748 expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode)
3750 if (!validate_arglist (exp,
3751 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3752 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3753 return NULL_RTX;
3754 else
3756 tree dest = CALL_EXPR_ARG (exp, 0);
3757 tree val = CALL_EXPR_ARG (exp, 2);
3758 tree len = CALL_EXPR_ARG (exp, 3);
3759 rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp);
3761 /* Return src bounds with the result. */
3762 if (res)
3764 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3765 expand_normal (CALL_EXPR_ARG (exp, 1)));
3766 res = chkp_join_splitted_slot (res, bnd);
3768 return res;
3772 /* Helper function to do the actual work for expand_builtin_memset. The
3773 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3774 so that this can also be called without constructing an actual CALL_EXPR.
3775 The other arguments and return value are the same as for
3776 expand_builtin_memset. */
3778 static rtx
3779 expand_builtin_memset_args (tree dest, tree val, tree len,
3780 rtx target, machine_mode mode, tree orig_exp)
3782 tree fndecl, fn;
3783 enum built_in_function fcode;
3784 machine_mode val_mode;
3785 char c;
3786 unsigned int dest_align;
3787 rtx dest_mem, dest_addr, len_rtx;
3788 HOST_WIDE_INT expected_size = -1;
3789 unsigned int expected_align = 0;
3790 unsigned HOST_WIDE_INT min_size;
3791 unsigned HOST_WIDE_INT max_size;
3792 unsigned HOST_WIDE_INT probable_max_size;
3794 dest_align = get_pointer_alignment (dest);
3796 /* If DEST is not a pointer type, don't do this operation in-line. */
3797 if (dest_align == 0)
3798 return NULL_RTX;
3800 if (currently_expanding_gimple_stmt)
3801 stringop_block_profile (currently_expanding_gimple_stmt,
3802 &expected_align, &expected_size);
3804 if (expected_align < dest_align)
3805 expected_align = dest_align;
3807 /* If the LEN parameter is zero, return DEST. */
3808 if (integer_zerop (len))
3810 /* Evaluate and ignore VAL in case it has side-effects. */
3811 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3812 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3815 /* Stabilize the arguments in case we fail. */
3816 dest = builtin_save_expr (dest);
3817 val = builtin_save_expr (val);
3818 len = builtin_save_expr (len);
3820 len_rtx = expand_normal (len);
3821 determine_block_size (len, len_rtx, &min_size, &max_size,
3822 &probable_max_size);
3823 dest_mem = get_memory_rtx (dest, len);
3824 val_mode = TYPE_MODE (unsigned_char_type_node);
3826 if (TREE_CODE (val) != INTEGER_CST)
3828 rtx val_rtx;
3830 val_rtx = expand_normal (val);
3831 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3833 /* Assume that we can memset by pieces if we can store
3834 * the coefficients by pieces (in the required modes).
3835 * We can't pass builtin_memset_gen_str as that emits RTL. */
3836 c = 1;
3837 if (tree_fits_uhwi_p (len)
3838 && can_store_by_pieces (tree_to_uhwi (len),
3839 builtin_memset_read_str, &c, dest_align,
3840 true))
3842 val_rtx = force_reg (val_mode, val_rtx);
3843 store_by_pieces (dest_mem, tree_to_uhwi (len),
3844 builtin_memset_gen_str, val_rtx, dest_align,
3845 true, 0);
3847 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3848 dest_align, expected_align,
3849 expected_size, min_size, max_size,
3850 probable_max_size))
3851 goto do_libcall;
3853 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3854 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3855 return dest_mem;
3858 if (target_char_cast (val, &c))
3859 goto do_libcall;
3861 if (c)
3863 if (tree_fits_uhwi_p (len)
3864 && can_store_by_pieces (tree_to_uhwi (len),
3865 builtin_memset_read_str, &c, dest_align,
3866 true))
3867 store_by_pieces (dest_mem, tree_to_uhwi (len),
3868 builtin_memset_read_str, &c, dest_align, true, 0);
3869 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3870 gen_int_mode (c, val_mode),
3871 dest_align, expected_align,
3872 expected_size, min_size, max_size,
3873 probable_max_size))
3874 goto do_libcall;
3876 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3877 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3878 return dest_mem;
3881 set_mem_align (dest_mem, dest_align);
3882 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3883 CALL_EXPR_TAILCALL (orig_exp)
3884 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3885 expected_align, expected_size,
3886 min_size, max_size,
3887 probable_max_size);
3889 if (dest_addr == 0)
3891 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3892 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3895 return dest_addr;
3897 do_libcall:
3898 fndecl = get_callee_fndecl (orig_exp);
3899 fcode = DECL_FUNCTION_CODE (fndecl);
3900 if (fcode == BUILT_IN_MEMSET
3901 || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP)
3902 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3903 dest, val, len);
3904 else if (fcode == BUILT_IN_BZERO)
3905 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3906 dest, len);
3907 else
3908 gcc_unreachable ();
3909 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3910 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3911 return expand_call (fn, target, target == const0_rtx);
3914 /* Expand expression EXP, which is a call to the bzero builtin. Return
3915 NULL_RTX if we failed the caller should emit a normal call. */
3917 static rtx
3918 expand_builtin_bzero (tree exp)
3920 tree dest, size;
3921 location_t loc = EXPR_LOCATION (exp);
3923 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3924 return NULL_RTX;
3926 dest = CALL_EXPR_ARG (exp, 0);
3927 size = CALL_EXPR_ARG (exp, 1);
3929 /* New argument list transforming bzero(ptr x, int y) to
3930 memset(ptr x, int 0, size_t y). This is done this way
3931 so that if it isn't expanded inline, we fallback to
3932 calling bzero instead of memset. */
3934 return expand_builtin_memset_args (dest, integer_zero_node,
3935 fold_convert_loc (loc,
3936 size_type_node, size),
3937 const0_rtx, VOIDmode, exp);
3940 /* Expand expression EXP, which is a call to the memcmp built-in function.
3941 Return NULL_RTX if we failed and the caller should emit a normal call,
3942 otherwise try to get the result in TARGET, if convenient (and in mode
3943 MODE, if that's convenient). */
3945 static rtx
3946 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3947 ATTRIBUTE_UNUSED machine_mode mode)
3949 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3951 if (!validate_arglist (exp,
3952 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3953 return NULL_RTX;
3955 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3956 implementing memcmp because it will stop if it encounters two
3957 zero bytes. */
3958 #if defined HAVE_cmpmemsi
3960 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3961 rtx result;
3962 rtx insn;
3963 tree arg1 = CALL_EXPR_ARG (exp, 0);
3964 tree arg2 = CALL_EXPR_ARG (exp, 1);
3965 tree len = CALL_EXPR_ARG (exp, 2);
3967 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3968 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3969 machine_mode insn_mode;
3971 if (HAVE_cmpmemsi)
3972 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3973 else
3974 return NULL_RTX;
3976 /* If we don't have POINTER_TYPE, call the function. */
3977 if (arg1_align == 0 || arg2_align == 0)
3978 return NULL_RTX;
3980 /* Make a place to write the result of the instruction. */
3981 result = target;
3982 if (! (result != 0
3983 && REG_P (result) && GET_MODE (result) == insn_mode
3984 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3985 result = gen_reg_rtx (insn_mode);
3987 arg1_rtx = get_memory_rtx (arg1, len);
3988 arg2_rtx = get_memory_rtx (arg2, len);
3989 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3991 /* Set MEM_SIZE as appropriate. */
3992 if (CONST_INT_P (arg3_rtx))
3994 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3995 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3998 if (HAVE_cmpmemsi)
3999 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4000 GEN_INT (MIN (arg1_align, arg2_align)));
4001 else
4002 gcc_unreachable ();
4004 if (insn)
4005 emit_insn (insn);
4006 else
4007 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4008 TYPE_MODE (integer_type_node), 3,
4009 XEXP (arg1_rtx, 0), Pmode,
4010 XEXP (arg2_rtx, 0), Pmode,
4011 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4012 TYPE_UNSIGNED (sizetype)),
4013 TYPE_MODE (sizetype));
4015 /* Return the value in the proper mode for this function. */
4016 mode = TYPE_MODE (TREE_TYPE (exp));
4017 if (GET_MODE (result) == mode)
4018 return result;
4019 else if (target != 0)
4021 convert_move (target, result, 0);
4022 return target;
4024 else
4025 return convert_to_mode (mode, result, 0);
4027 #endif /* HAVE_cmpmemsi. */
4029 return NULL_RTX;
4032 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4033 if we failed the caller should emit a normal call, otherwise try to get
4034 the result in TARGET, if convenient. */
4036 static rtx
4037 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4039 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4040 return NULL_RTX;
4042 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4043 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
4044 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
4046 rtx arg1_rtx, arg2_rtx;
4047 rtx result, insn = NULL_RTX;
4048 tree fndecl, fn;
4049 tree arg1 = CALL_EXPR_ARG (exp, 0);
4050 tree arg2 = CALL_EXPR_ARG (exp, 1);
4052 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4053 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4055 /* If we don't have POINTER_TYPE, call the function. */
4056 if (arg1_align == 0 || arg2_align == 0)
4057 return NULL_RTX;
4059 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4060 arg1 = builtin_save_expr (arg1);
4061 arg2 = builtin_save_expr (arg2);
4063 arg1_rtx = get_memory_rtx (arg1, NULL);
4064 arg2_rtx = get_memory_rtx (arg2, NULL);
4066 #ifdef HAVE_cmpstrsi
4067 /* Try to call cmpstrsi. */
4068 if (HAVE_cmpstrsi)
4070 machine_mode insn_mode
4071 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
4073 /* Make a place to write the result of the instruction. */
4074 result = target;
4075 if (! (result != 0
4076 && REG_P (result) && GET_MODE (result) == insn_mode
4077 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4078 result = gen_reg_rtx (insn_mode);
4080 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
4081 GEN_INT (MIN (arg1_align, arg2_align)));
4083 #endif
4084 #ifdef HAVE_cmpstrnsi
4085 /* Try to determine at least one length and call cmpstrnsi. */
4086 if (!insn && HAVE_cmpstrnsi)
4088 tree len;
4089 rtx arg3_rtx;
4091 machine_mode insn_mode
4092 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4093 tree len1 = c_strlen (arg1, 1);
4094 tree len2 = c_strlen (arg2, 1);
4096 if (len1)
4097 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4098 if (len2)
4099 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4101 /* If we don't have a constant length for the first, use the length
4102 of the second, if we know it. We don't require a constant for
4103 this case; some cost analysis could be done if both are available
4104 but neither is constant. For now, assume they're equally cheap,
4105 unless one has side effects. If both strings have constant lengths,
4106 use the smaller. */
4108 if (!len1)
4109 len = len2;
4110 else if (!len2)
4111 len = len1;
4112 else if (TREE_SIDE_EFFECTS (len1))
4113 len = len2;
4114 else if (TREE_SIDE_EFFECTS (len2))
4115 len = len1;
4116 else if (TREE_CODE (len1) != INTEGER_CST)
4117 len = len2;
4118 else if (TREE_CODE (len2) != INTEGER_CST)
4119 len = len1;
4120 else if (tree_int_cst_lt (len1, len2))
4121 len = len1;
4122 else
4123 len = len2;
4125 /* If both arguments have side effects, we cannot optimize. */
4126 if (!len || TREE_SIDE_EFFECTS (len))
4127 goto do_libcall;
4129 arg3_rtx = expand_normal (len);
4131 /* Make a place to write the result of the instruction. */
4132 result = target;
4133 if (! (result != 0
4134 && REG_P (result) && GET_MODE (result) == insn_mode
4135 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4136 result = gen_reg_rtx (insn_mode);
4138 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4139 GEN_INT (MIN (arg1_align, arg2_align)));
4141 #endif
4143 if (insn)
4145 machine_mode mode;
4146 emit_insn (insn);
4148 /* Return the value in the proper mode for this function. */
4149 mode = TYPE_MODE (TREE_TYPE (exp));
4150 if (GET_MODE (result) == mode)
4151 return result;
4152 if (target == 0)
4153 return convert_to_mode (mode, result, 0);
4154 convert_move (target, result, 0);
4155 return target;
4158 /* Expand the library call ourselves using a stabilized argument
4159 list to avoid re-evaluating the function's arguments twice. */
4160 #ifdef HAVE_cmpstrnsi
4161 do_libcall:
4162 #endif
4163 fndecl = get_callee_fndecl (exp);
4164 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4165 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4166 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4167 return expand_call (fn, target, target == const0_rtx);
4169 #endif
4170 return NULL_RTX;
4173 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4174 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4175 the result in TARGET, if convenient. */
4177 static rtx
4178 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4179 ATTRIBUTE_UNUSED machine_mode mode)
4181 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4183 if (!validate_arglist (exp,
4184 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4185 return NULL_RTX;
4187 /* If c_strlen can determine an expression for one of the string
4188 lengths, and it doesn't have side effects, then emit cmpstrnsi
4189 using length MIN(strlen(string)+1, arg3). */
4190 #ifdef HAVE_cmpstrnsi
4191 if (HAVE_cmpstrnsi)
4193 tree len, len1, len2;
4194 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4195 rtx result, insn;
4196 tree fndecl, fn;
4197 tree arg1 = CALL_EXPR_ARG (exp, 0);
4198 tree arg2 = CALL_EXPR_ARG (exp, 1);
4199 tree arg3 = CALL_EXPR_ARG (exp, 2);
4201 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4202 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4203 machine_mode insn_mode
4204 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
4206 len1 = c_strlen (arg1, 1);
4207 len2 = c_strlen (arg2, 1);
4209 if (len1)
4210 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4211 if (len2)
4212 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4214 /* If we don't have a constant length for the first, use the length
4215 of the second, if we know it. We don't require a constant for
4216 this case; some cost analysis could be done if both are available
4217 but neither is constant. For now, assume they're equally cheap,
4218 unless one has side effects. If both strings have constant lengths,
4219 use the smaller. */
4221 if (!len1)
4222 len = len2;
4223 else if (!len2)
4224 len = len1;
4225 else if (TREE_SIDE_EFFECTS (len1))
4226 len = len2;
4227 else if (TREE_SIDE_EFFECTS (len2))
4228 len = len1;
4229 else if (TREE_CODE (len1) != INTEGER_CST)
4230 len = len2;
4231 else if (TREE_CODE (len2) != INTEGER_CST)
4232 len = len1;
4233 else if (tree_int_cst_lt (len1, len2))
4234 len = len1;
4235 else
4236 len = len2;
4238 /* If both arguments have side effects, we cannot optimize. */
4239 if (!len || TREE_SIDE_EFFECTS (len))
4240 return NULL_RTX;
4242 /* The actual new length parameter is MIN(len,arg3). */
4243 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4244 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4246 /* If we don't have POINTER_TYPE, call the function. */
4247 if (arg1_align == 0 || arg2_align == 0)
4248 return NULL_RTX;
4250 /* Make a place to write the result of the instruction. */
4251 result = target;
4252 if (! (result != 0
4253 && REG_P (result) && GET_MODE (result) == insn_mode
4254 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4255 result = gen_reg_rtx (insn_mode);
4257 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4258 arg1 = builtin_save_expr (arg1);
4259 arg2 = builtin_save_expr (arg2);
4260 len = builtin_save_expr (len);
4262 arg1_rtx = get_memory_rtx (arg1, len);
4263 arg2_rtx = get_memory_rtx (arg2, len);
4264 arg3_rtx = expand_normal (len);
4265 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4266 GEN_INT (MIN (arg1_align, arg2_align)));
4267 if (insn)
4269 emit_insn (insn);
4271 /* Return the value in the proper mode for this function. */
4272 mode = TYPE_MODE (TREE_TYPE (exp));
4273 if (GET_MODE (result) == mode)
4274 return result;
4275 if (target == 0)
4276 return convert_to_mode (mode, result, 0);
4277 convert_move (target, result, 0);
4278 return target;
4281 /* Expand the library call ourselves using a stabilized argument
4282 list to avoid re-evaluating the function's arguments twice. */
4283 fndecl = get_callee_fndecl (exp);
4284 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4285 arg1, arg2, len);
4286 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4287 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4288 return expand_call (fn, target, target == const0_rtx);
4290 #endif
4291 return NULL_RTX;
4294 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4295 if that's convenient. */
4298 expand_builtin_saveregs (void)
4300 rtx val;
4301 rtx_insn *seq;
4303 /* Don't do __builtin_saveregs more than once in a function.
4304 Save the result of the first call and reuse it. */
4305 if (saveregs_value != 0)
4306 return saveregs_value;
4308 /* When this function is called, it means that registers must be
4309 saved on entry to this function. So we migrate the call to the
4310 first insn of this function. */
4312 start_sequence ();
4314 /* Do whatever the machine needs done in this case. */
4315 val = targetm.calls.expand_builtin_saveregs ();
4317 seq = get_insns ();
4318 end_sequence ();
4320 saveregs_value = val;
4322 /* Put the insns after the NOTE that starts the function. If this
4323 is inside a start_sequence, make the outer-level insn chain current, so
4324 the code is placed at the start of the function. */
4325 push_topmost_sequence ();
4326 emit_insn_after (seq, entry_of_function ());
4327 pop_topmost_sequence ();
4329 return val;
4332 /* Expand a call to __builtin_next_arg. */
4334 static rtx
4335 expand_builtin_next_arg (void)
4337 /* Checking arguments is already done in fold_builtin_next_arg
4338 that must be called before this function. */
4339 return expand_binop (ptr_mode, add_optab,
4340 crtl->args.internal_arg_pointer,
4341 crtl->args.arg_offset_rtx,
4342 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4345 /* Make it easier for the backends by protecting the valist argument
4346 from multiple evaluations. */
4348 static tree
4349 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4351 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4353 /* The current way of determining the type of valist is completely
4354 bogus. We should have the information on the va builtin instead. */
4355 if (!vatype)
4356 vatype = targetm.fn_abi_va_list (cfun->decl);
4358 if (TREE_CODE (vatype) == ARRAY_TYPE)
4360 if (TREE_SIDE_EFFECTS (valist))
4361 valist = save_expr (valist);
4363 /* For this case, the backends will be expecting a pointer to
4364 vatype, but it's possible we've actually been given an array
4365 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4366 So fix it. */
4367 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4369 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4370 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4373 else
4375 tree pt = build_pointer_type (vatype);
4377 if (! needs_lvalue)
4379 if (! TREE_SIDE_EFFECTS (valist))
4380 return valist;
4382 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4383 TREE_SIDE_EFFECTS (valist) = 1;
4386 if (TREE_SIDE_EFFECTS (valist))
4387 valist = save_expr (valist);
4388 valist = fold_build2_loc (loc, MEM_REF,
4389 vatype, valist, build_int_cst (pt, 0));
4392 return valist;
4395 /* The "standard" definition of va_list is void*. */
4397 tree
4398 std_build_builtin_va_list (void)
4400 return ptr_type_node;
4403 /* The "standard" abi va_list is va_list_type_node. */
4405 tree
4406 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4408 return va_list_type_node;
4411 /* The "standard" type of va_list is va_list_type_node. */
4413 tree
4414 std_canonical_va_list_type (tree type)
4416 tree wtype, htype;
4418 if (INDIRECT_REF_P (type))
4419 type = TREE_TYPE (type);
4420 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4421 type = TREE_TYPE (type);
4422 wtype = va_list_type_node;
4423 htype = type;
4424 /* Treat structure va_list types. */
4425 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4426 htype = TREE_TYPE (htype);
4427 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4429 /* If va_list is an array type, the argument may have decayed
4430 to a pointer type, e.g. by being passed to another function.
4431 In that case, unwrap both types so that we can compare the
4432 underlying records. */
4433 if (TREE_CODE (htype) == ARRAY_TYPE
4434 || POINTER_TYPE_P (htype))
4436 wtype = TREE_TYPE (wtype);
4437 htype = TREE_TYPE (htype);
4440 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4441 return va_list_type_node;
4443 return NULL_TREE;
4446 /* The "standard" implementation of va_start: just assign `nextarg' to
4447 the variable. */
4449 void
4450 std_expand_builtin_va_start (tree valist, rtx nextarg)
4452 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4453 convert_move (va_r, nextarg, 0);
4455 /* We do not have any valid bounds for the pointer, so
4456 just store zero bounds for it. */
4457 if (chkp_function_instrumented_p (current_function_decl))
4458 chkp_expand_bounds_reset_for_mem (valist,
4459 make_tree (TREE_TYPE (valist),
4460 nextarg));
4463 /* Expand EXP, a call to __builtin_va_start. */
4465 static rtx
4466 expand_builtin_va_start (tree exp)
4468 rtx nextarg;
4469 tree valist;
4470 location_t loc = EXPR_LOCATION (exp);
4472 if (call_expr_nargs (exp) < 2)
4474 error_at (loc, "too few arguments to function %<va_start%>");
4475 return const0_rtx;
4478 if (fold_builtin_next_arg (exp, true))
4479 return const0_rtx;
4481 nextarg = expand_builtin_next_arg ();
4482 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4484 if (targetm.expand_builtin_va_start)
4485 targetm.expand_builtin_va_start (valist, nextarg);
4486 else
4487 std_expand_builtin_va_start (valist, nextarg);
4489 return const0_rtx;
4492 /* Expand EXP, a call to __builtin_va_end. */
4494 static rtx
4495 expand_builtin_va_end (tree exp)
4497 tree valist = CALL_EXPR_ARG (exp, 0);
4499 /* Evaluate for side effects, if needed. I hate macros that don't
4500 do that. */
4501 if (TREE_SIDE_EFFECTS (valist))
4502 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4504 return const0_rtx;
4507 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4508 builtin rather than just as an assignment in stdarg.h because of the
4509 nastiness of array-type va_list types. */
4511 static rtx
4512 expand_builtin_va_copy (tree exp)
4514 tree dst, src, t;
4515 location_t loc = EXPR_LOCATION (exp);
4517 dst = CALL_EXPR_ARG (exp, 0);
4518 src = CALL_EXPR_ARG (exp, 1);
4520 dst = stabilize_va_list_loc (loc, dst, 1);
4521 src = stabilize_va_list_loc (loc, src, 0);
4523 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4525 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4527 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4528 TREE_SIDE_EFFECTS (t) = 1;
4529 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4531 else
4533 rtx dstb, srcb, size;
4535 /* Evaluate to pointers. */
4536 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4537 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4538 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4539 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4541 dstb = convert_memory_address (Pmode, dstb);
4542 srcb = convert_memory_address (Pmode, srcb);
4544 /* "Dereference" to BLKmode memories. */
4545 dstb = gen_rtx_MEM (BLKmode, dstb);
4546 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4547 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4548 srcb = gen_rtx_MEM (BLKmode, srcb);
4549 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4550 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4552 /* Copy. */
4553 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4556 return const0_rtx;
4559 /* Expand a call to one of the builtin functions __builtin_frame_address or
4560 __builtin_return_address. */
4562 static rtx
4563 expand_builtin_frame_address (tree fndecl, tree exp)
4565 /* The argument must be a nonnegative integer constant.
4566 It counts the number of frames to scan up the stack.
4567 The value is the return address saved in that frame. */
4568 if (call_expr_nargs (exp) == 0)
4569 /* Warning about missing arg was already issued. */
4570 return const0_rtx;
4571 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4573 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4574 error ("invalid argument to %<__builtin_frame_address%>");
4575 else
4576 error ("invalid argument to %<__builtin_return_address%>");
4577 return const0_rtx;
4579 else
4581 rtx tem
4582 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4583 tree_to_uhwi (CALL_EXPR_ARG (exp, 0)));
4585 /* Some ports cannot access arbitrary stack frames. */
4586 if (tem == NULL)
4588 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4589 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4590 else
4591 warning (0, "unsupported argument to %<__builtin_return_address%>");
4592 return const0_rtx;
4595 /* For __builtin_frame_address, return what we've got. */
4596 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4597 return tem;
4599 if (!REG_P (tem)
4600 && ! CONSTANT_P (tem))
4601 tem = copy_addr_to_reg (tem);
4602 return tem;
4606 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4607 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4608 is the same as for allocate_dynamic_stack_space. */
4610 static rtx
4611 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4613 rtx op0;
4614 rtx result;
4615 bool valid_arglist;
4616 unsigned int align;
4617 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4618 == BUILT_IN_ALLOCA_WITH_ALIGN);
4620 valid_arglist
4621 = (alloca_with_align
4622 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4623 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4625 if (!valid_arglist)
4626 return NULL_RTX;
4628 /* Compute the argument. */
4629 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4631 /* Compute the alignment. */
4632 align = (alloca_with_align
4633 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4634 : BIGGEST_ALIGNMENT);
4636 /* Allocate the desired space. */
4637 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4638 result = convert_memory_address (ptr_mode, result);
4640 return result;
4643 /* Expand a call to bswap builtin in EXP.
4644 Return NULL_RTX if a normal call should be emitted rather than expanding the
4645 function in-line. If convenient, the result should be placed in TARGET.
4646 SUBTARGET may be used as the target for computing one of EXP's operands. */
4648 static rtx
4649 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
4650 rtx subtarget)
4652 tree arg;
4653 rtx op0;
4655 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4656 return NULL_RTX;
4658 arg = CALL_EXPR_ARG (exp, 0);
4659 op0 = expand_expr (arg,
4660 subtarget && GET_MODE (subtarget) == target_mode
4661 ? subtarget : NULL_RTX,
4662 target_mode, EXPAND_NORMAL);
4663 if (GET_MODE (op0) != target_mode)
4664 op0 = convert_to_mode (target_mode, op0, 1);
4666 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4668 gcc_assert (target);
4670 return convert_to_mode (target_mode, target, 1);
4673 /* Expand a call to a unary builtin in EXP.
4674 Return NULL_RTX if a normal call should be emitted rather than expanding the
4675 function in-line. If convenient, the result should be placed in TARGET.
4676 SUBTARGET may be used as the target for computing one of EXP's operands. */
4678 static rtx
4679 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
4680 rtx subtarget, optab op_optab)
4682 rtx op0;
4684 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4685 return NULL_RTX;
4687 /* Compute the argument. */
4688 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4689 (subtarget
4690 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4691 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4692 VOIDmode, EXPAND_NORMAL);
4693 /* Compute op, into TARGET if possible.
4694 Set TARGET to wherever the result comes back. */
4695 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4696 op_optab, op0, target, op_optab != clrsb_optab);
4697 gcc_assert (target);
4699 return convert_to_mode (target_mode, target, 0);
4702 /* Expand a call to __builtin_expect. We just return our argument
4703 as the builtin_expect semantic should've been already executed by
4704 tree branch prediction pass. */
4706 static rtx
4707 expand_builtin_expect (tree exp, rtx target)
4709 tree arg;
4711 if (call_expr_nargs (exp) < 2)
4712 return const0_rtx;
4713 arg = CALL_EXPR_ARG (exp, 0);
4715 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4716 /* When guessing was done, the hints should be already stripped away. */
4717 gcc_assert (!flag_guess_branch_prob
4718 || optimize == 0 || seen_error ());
4719 return target;
4722 /* Expand a call to __builtin_assume_aligned. We just return our first
4723 argument as the builtin_assume_aligned semantic should've been already
4724 executed by CCP. */
4726 static rtx
4727 expand_builtin_assume_aligned (tree exp, rtx target)
4729 if (call_expr_nargs (exp) < 2)
4730 return const0_rtx;
4731 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4732 EXPAND_NORMAL);
4733 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4734 && (call_expr_nargs (exp) < 3
4735 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4736 return target;
4739 void
4740 expand_builtin_trap (void)
4742 #ifdef HAVE_trap
4743 if (HAVE_trap)
4745 rtx_insn *insn = emit_insn (gen_trap ());
4746 /* For trap insns when not accumulating outgoing args force
4747 REG_ARGS_SIZE note to prevent crossjumping of calls with
4748 different args sizes. */
4749 if (!ACCUMULATE_OUTGOING_ARGS)
4750 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4752 else
4753 #endif
4754 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4755 emit_barrier ();
4758 /* Expand a call to __builtin_unreachable. We do nothing except emit
4759 a barrier saying that control flow will not pass here.
4761 It is the responsibility of the program being compiled to ensure
4762 that control flow does never reach __builtin_unreachable. */
4763 static void
4764 expand_builtin_unreachable (void)
4766 emit_barrier ();
4769 /* Expand EXP, a call to fabs, fabsf or fabsl.
4770 Return NULL_RTX if a normal call should be emitted rather than expanding
4771 the function inline. If convenient, the result should be placed
4772 in TARGET. SUBTARGET may be used as the target for computing
4773 the operand. */
4775 static rtx
4776 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4778 machine_mode mode;
4779 tree arg;
4780 rtx op0;
4782 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4783 return NULL_RTX;
4785 arg = CALL_EXPR_ARG (exp, 0);
4786 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4787 mode = TYPE_MODE (TREE_TYPE (arg));
4788 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4789 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4792 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4793 Return NULL is a normal call should be emitted rather than expanding the
4794 function inline. If convenient, the result should be placed in TARGET.
4795 SUBTARGET may be used as the target for computing the operand. */
4797 static rtx
4798 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4800 rtx op0, op1;
4801 tree arg;
4803 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4804 return NULL_RTX;
4806 arg = CALL_EXPR_ARG (exp, 0);
4807 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4809 arg = CALL_EXPR_ARG (exp, 1);
4810 op1 = expand_normal (arg);
4812 return expand_copysign (op0, op1, target);
4815 /* Expand a call to __builtin___clear_cache. */
4817 static rtx
4818 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4820 #ifndef HAVE_clear_cache
4821 #ifdef CLEAR_INSN_CACHE
4822 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4823 does something. Just do the default expansion to a call to
4824 __clear_cache(). */
4825 return NULL_RTX;
4826 #else
4827 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4828 does nothing. There is no need to call it. Do nothing. */
4829 return const0_rtx;
4830 #endif /* CLEAR_INSN_CACHE */
4831 #else
4832 /* We have a "clear_cache" insn, and it will handle everything. */
4833 tree begin, end;
4834 rtx begin_rtx, end_rtx;
4836 /* We must not expand to a library call. If we did, any
4837 fallback library function in libgcc that might contain a call to
4838 __builtin___clear_cache() would recurse infinitely. */
4839 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4841 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4842 return const0_rtx;
4845 if (HAVE_clear_cache)
4847 struct expand_operand ops[2];
4849 begin = CALL_EXPR_ARG (exp, 0);
4850 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4852 end = CALL_EXPR_ARG (exp, 1);
4853 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4855 create_address_operand (&ops[0], begin_rtx);
4856 create_address_operand (&ops[1], end_rtx);
4857 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4858 return const0_rtx;
4860 return const0_rtx;
4861 #endif /* HAVE_clear_cache */
4864 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4866 static rtx
4867 round_trampoline_addr (rtx tramp)
4869 rtx temp, addend, mask;
4871 /* If we don't need too much alignment, we'll have been guaranteed
4872 proper alignment by get_trampoline_type. */
4873 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4874 return tramp;
4876 /* Round address up to desired boundary. */
4877 temp = gen_reg_rtx (Pmode);
4878 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4879 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4881 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4882 temp, 0, OPTAB_LIB_WIDEN);
4883 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4884 temp, 0, OPTAB_LIB_WIDEN);
4886 return tramp;
4889 static rtx
4890 expand_builtin_init_trampoline (tree exp, bool onstack)
4892 tree t_tramp, t_func, t_chain;
4893 rtx m_tramp, r_tramp, r_chain, tmp;
4895 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4896 POINTER_TYPE, VOID_TYPE))
4897 return NULL_RTX;
4899 t_tramp = CALL_EXPR_ARG (exp, 0);
4900 t_func = CALL_EXPR_ARG (exp, 1);
4901 t_chain = CALL_EXPR_ARG (exp, 2);
4903 r_tramp = expand_normal (t_tramp);
4904 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4905 MEM_NOTRAP_P (m_tramp) = 1;
4907 /* If ONSTACK, the TRAMP argument should be the address of a field
4908 within the local function's FRAME decl. Either way, let's see if
4909 we can fill in the MEM_ATTRs for this memory. */
4910 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4911 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4913 /* Creator of a heap trampoline is responsible for making sure the
4914 address is aligned to at least STACK_BOUNDARY. Normally malloc
4915 will ensure this anyhow. */
4916 tmp = round_trampoline_addr (r_tramp);
4917 if (tmp != r_tramp)
4919 m_tramp = change_address (m_tramp, BLKmode, tmp);
4920 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4921 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4924 /* The FUNC argument should be the address of the nested function.
4925 Extract the actual function decl to pass to the hook. */
4926 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4927 t_func = TREE_OPERAND (t_func, 0);
4928 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4930 r_chain = expand_normal (t_chain);
4932 /* Generate insns to initialize the trampoline. */
4933 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4935 if (onstack)
4937 trampolines_created = 1;
4939 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4940 "trampoline generated for nested function %qD", t_func);
4943 return const0_rtx;
4946 static rtx
4947 expand_builtin_adjust_trampoline (tree exp)
4949 rtx tramp;
4951 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4952 return NULL_RTX;
4954 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4955 tramp = round_trampoline_addr (tramp);
4956 if (targetm.calls.trampoline_adjust_address)
4957 tramp = targetm.calls.trampoline_adjust_address (tramp);
4959 return tramp;
4962 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4963 function. The function first checks whether the back end provides
4964 an insn to implement signbit for the respective mode. If not, it
4965 checks whether the floating point format of the value is such that
4966 the sign bit can be extracted. If that is not the case, the
4967 function returns NULL_RTX to indicate that a normal call should be
4968 emitted rather than expanding the function in-line. EXP is the
4969 expression that is a call to the builtin function; if convenient,
4970 the result should be placed in TARGET. */
4971 static rtx
4972 expand_builtin_signbit (tree exp, rtx target)
4974 const struct real_format *fmt;
4975 machine_mode fmode, imode, rmode;
4976 tree arg;
4977 int word, bitpos;
4978 enum insn_code icode;
4979 rtx temp;
4980 location_t loc = EXPR_LOCATION (exp);
4982 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4983 return NULL_RTX;
4985 arg = CALL_EXPR_ARG (exp, 0);
4986 fmode = TYPE_MODE (TREE_TYPE (arg));
4987 rmode = TYPE_MODE (TREE_TYPE (exp));
4988 fmt = REAL_MODE_FORMAT (fmode);
4990 arg = builtin_save_expr (arg);
4992 /* Expand the argument yielding a RTX expression. */
4993 temp = expand_normal (arg);
4995 /* Check if the back end provides an insn that handles signbit for the
4996 argument's mode. */
4997 icode = optab_handler (signbit_optab, fmode);
4998 if (icode != CODE_FOR_nothing)
5000 rtx_insn *last = get_last_insn ();
5001 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5002 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5003 return target;
5004 delete_insns_since (last);
5007 /* For floating point formats without a sign bit, implement signbit
5008 as "ARG < 0.0". */
5009 bitpos = fmt->signbit_ro;
5010 if (bitpos < 0)
5012 /* But we can't do this if the format supports signed zero. */
5013 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
5014 return NULL_RTX;
5016 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5017 build_real (TREE_TYPE (arg), dconst0));
5018 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5021 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5023 imode = int_mode_for_mode (fmode);
5024 if (imode == BLKmode)
5025 return NULL_RTX;
5026 temp = gen_lowpart (imode, temp);
5028 else
5030 imode = word_mode;
5031 /* Handle targets with different FP word orders. */
5032 if (FLOAT_WORDS_BIG_ENDIAN)
5033 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5034 else
5035 word = bitpos / BITS_PER_WORD;
5036 temp = operand_subword_force (temp, word, fmode);
5037 bitpos = bitpos % BITS_PER_WORD;
5040 /* Force the intermediate word_mode (or narrower) result into a
5041 register. This avoids attempting to create paradoxical SUBREGs
5042 of floating point modes below. */
5043 temp = force_reg (imode, temp);
5045 /* If the bitpos is within the "result mode" lowpart, the operation
5046 can be implement with a single bitwise AND. Otherwise, we need
5047 a right shift and an AND. */
5049 if (bitpos < GET_MODE_BITSIZE (rmode))
5051 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5053 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5054 temp = gen_lowpart (rmode, temp);
5055 temp = expand_binop (rmode, and_optab, temp,
5056 immed_wide_int_const (mask, rmode),
5057 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5059 else
5061 /* Perform a logical right shift to place the signbit in the least
5062 significant bit, then truncate the result to the desired mode
5063 and mask just this bit. */
5064 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5065 temp = gen_lowpart (rmode, temp);
5066 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5067 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5070 return temp;
5073 /* Expand fork or exec calls. TARGET is the desired target of the
5074 call. EXP is the call. FN is the
5075 identificator of the actual function. IGNORE is nonzero if the
5076 value is to be ignored. */
5078 static rtx
5079 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5081 tree id, decl;
5082 tree call;
5084 /* If we are not profiling, just call the function. */
5085 if (!profile_arc_flag)
5086 return NULL_RTX;
5088 /* Otherwise call the wrapper. This should be equivalent for the rest of
5089 compiler, so the code does not diverge, and the wrapper may run the
5090 code necessary for keeping the profiling sane. */
5092 switch (DECL_FUNCTION_CODE (fn))
5094 case BUILT_IN_FORK:
5095 id = get_identifier ("__gcov_fork");
5096 break;
5098 case BUILT_IN_EXECL:
5099 id = get_identifier ("__gcov_execl");
5100 break;
5102 case BUILT_IN_EXECV:
5103 id = get_identifier ("__gcov_execv");
5104 break;
5106 case BUILT_IN_EXECLP:
5107 id = get_identifier ("__gcov_execlp");
5108 break;
5110 case BUILT_IN_EXECLE:
5111 id = get_identifier ("__gcov_execle");
5112 break;
5114 case BUILT_IN_EXECVP:
5115 id = get_identifier ("__gcov_execvp");
5116 break;
5118 case BUILT_IN_EXECVE:
5119 id = get_identifier ("__gcov_execve");
5120 break;
5122 default:
5123 gcc_unreachable ();
5126 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5127 FUNCTION_DECL, id, TREE_TYPE (fn));
5128 DECL_EXTERNAL (decl) = 1;
5129 TREE_PUBLIC (decl) = 1;
5130 DECL_ARTIFICIAL (decl) = 1;
5131 TREE_NOTHROW (decl) = 1;
5132 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5133 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5134 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5135 return expand_call (call, target, ignore);
5140 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5141 the pointer in these functions is void*, the tree optimizers may remove
5142 casts. The mode computed in expand_builtin isn't reliable either, due
5143 to __sync_bool_compare_and_swap.
5145 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5146 group of builtins. This gives us log2 of the mode size. */
5148 static inline machine_mode
5149 get_builtin_sync_mode (int fcode_diff)
5151 /* The size is not negotiable, so ask not to get BLKmode in return
5152 if the target indicates that a smaller size would be better. */
5153 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5156 /* Expand the memory expression LOC and return the appropriate memory operand
5157 for the builtin_sync operations. */
5159 static rtx
5160 get_builtin_sync_mem (tree loc, machine_mode mode)
5162 rtx addr, mem;
5164 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5165 addr = convert_memory_address (Pmode, addr);
5167 /* Note that we explicitly do not want any alias information for this
5168 memory, so that we kill all other live memories. Otherwise we don't
5169 satisfy the full barrier semantics of the intrinsic. */
5170 mem = validize_mem (gen_rtx_MEM (mode, addr));
5172 /* The alignment needs to be at least according to that of the mode. */
5173 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5174 get_pointer_alignment (loc)));
5175 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5176 MEM_VOLATILE_P (mem) = 1;
5178 return mem;
5181 /* Make sure an argument is in the right mode.
5182 EXP is the tree argument.
5183 MODE is the mode it should be in. */
5185 static rtx
5186 expand_expr_force_mode (tree exp, machine_mode mode)
5188 rtx val;
5189 machine_mode old_mode;
5191 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5192 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5193 of CONST_INTs, where we know the old_mode only from the call argument. */
5195 old_mode = GET_MODE (val);
5196 if (old_mode == VOIDmode)
5197 old_mode = TYPE_MODE (TREE_TYPE (exp));
5198 val = convert_modes (mode, old_mode, val, 1);
5199 return val;
5203 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5204 EXP is the CALL_EXPR. CODE is the rtx code
5205 that corresponds to the arithmetic or logical operation from the name;
5206 an exception here is that NOT actually means NAND. TARGET is an optional
5207 place for us to store the results; AFTER is true if this is the
5208 fetch_and_xxx form. */
5210 static rtx
5211 expand_builtin_sync_operation (machine_mode mode, tree exp,
5212 enum rtx_code code, bool after,
5213 rtx target)
5215 rtx val, mem;
5216 location_t loc = EXPR_LOCATION (exp);
5218 if (code == NOT && warn_sync_nand)
5220 tree fndecl = get_callee_fndecl (exp);
5221 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5223 static bool warned_f_a_n, warned_n_a_f;
5225 switch (fcode)
5227 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5228 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5229 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5230 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5231 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5232 if (warned_f_a_n)
5233 break;
5235 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5236 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5237 warned_f_a_n = true;
5238 break;
5240 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5241 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5242 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5243 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5244 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5245 if (warned_n_a_f)
5246 break;
5248 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5249 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5250 warned_n_a_f = true;
5251 break;
5253 default:
5254 gcc_unreachable ();
5258 /* Expand the operands. */
5259 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5260 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5262 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
5263 after);
5266 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5267 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5268 true if this is the boolean form. TARGET is a place for us to store the
5269 results; this is NOT optional if IS_BOOL is true. */
5271 static rtx
5272 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5273 bool is_bool, rtx target)
5275 rtx old_val, new_val, mem;
5276 rtx *pbool, *poval;
5278 /* Expand the operands. */
5279 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5280 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5281 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5283 pbool = poval = NULL;
5284 if (target != const0_rtx)
5286 if (is_bool)
5287 pbool = &target;
5288 else
5289 poval = &target;
5291 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5292 false, MEMMODEL_SYNC_SEQ_CST,
5293 MEMMODEL_SYNC_SEQ_CST))
5294 return NULL_RTX;
5296 return target;
5299 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5300 general form is actually an atomic exchange, and some targets only
5301 support a reduced form with the second argument being a constant 1.
5302 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5303 the results. */
5305 static rtx
5306 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5307 rtx target)
5309 rtx val, mem;
5311 /* Expand the operands. */
5312 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5313 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5315 return expand_sync_lock_test_and_set (target, mem, val);
5318 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5320 static void
5321 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5323 rtx mem;
5325 /* Expand the operands. */
5326 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5328 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
5331 /* Given an integer representing an ``enum memmodel'', verify its
5332 correctness and return the memory model enum. */
5334 static enum memmodel
5335 get_memmodel (tree exp)
5337 rtx op;
5338 unsigned HOST_WIDE_INT val;
5340 /* If the parameter is not a constant, it's a run time value so we'll just
5341 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5342 if (TREE_CODE (exp) != INTEGER_CST)
5343 return MEMMODEL_SEQ_CST;
5345 op = expand_normal (exp);
5347 val = INTVAL (op);
5348 if (targetm.memmodel_check)
5349 val = targetm.memmodel_check (val);
5350 else if (val & ~MEMMODEL_MASK)
5352 warning (OPT_Winvalid_memory_model,
5353 "Unknown architecture specifier in memory model to builtin.");
5354 return MEMMODEL_SEQ_CST;
5357 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5358 if (memmodel_base (val) >= MEMMODEL_LAST)
5360 warning (OPT_Winvalid_memory_model,
5361 "invalid memory model argument to builtin");
5362 return MEMMODEL_SEQ_CST;
5365 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5366 be conservative and promote consume to acquire. */
5367 if (val == MEMMODEL_CONSUME)
5368 val = MEMMODEL_ACQUIRE;
5370 return (enum memmodel) val;
5373 /* Expand the __atomic_exchange intrinsic:
5374 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5375 EXP is the CALL_EXPR.
5376 TARGET is an optional place for us to store the results. */
5378 static rtx
5379 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5381 rtx val, mem;
5382 enum memmodel model;
5384 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5386 if (!flag_inline_atomics)
5387 return NULL_RTX;
5389 /* Expand the operands. */
5390 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5391 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5393 return expand_atomic_exchange (target, mem, val, model);
5396 /* Expand the __atomic_compare_exchange intrinsic:
5397 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5398 TYPE desired, BOOL weak,
5399 enum memmodel success,
5400 enum memmodel failure)
5401 EXP is the CALL_EXPR.
5402 TARGET is an optional place for us to store the results. */
5404 static rtx
5405 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5406 rtx target)
5408 rtx expect, desired, mem, oldval;
5409 rtx_code_label *label;
5410 enum memmodel success, failure;
5411 tree weak;
5412 bool is_weak;
5414 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5415 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5417 if (failure > success)
5419 warning (OPT_Winvalid_memory_model,
5420 "failure memory model cannot be stronger than success memory "
5421 "model for %<__atomic_compare_exchange%>");
5422 success = MEMMODEL_SEQ_CST;
5425 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5427 warning (OPT_Winvalid_memory_model,
5428 "invalid failure memory model for "
5429 "%<__atomic_compare_exchange%>");
5430 failure = MEMMODEL_SEQ_CST;
5431 success = MEMMODEL_SEQ_CST;
5435 if (!flag_inline_atomics)
5436 return NULL_RTX;
5438 /* Expand the operands. */
5439 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5441 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5442 expect = convert_memory_address (Pmode, expect);
5443 expect = gen_rtx_MEM (mode, expect);
5444 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5446 weak = CALL_EXPR_ARG (exp, 3);
5447 is_weak = false;
5448 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5449 is_weak = true;
5451 if (target == const0_rtx)
5452 target = NULL;
5454 /* Lest the rtl backend create a race condition with an imporoper store
5455 to memory, always create a new pseudo for OLDVAL. */
5456 oldval = NULL;
5458 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5459 is_weak, success, failure))
5460 return NULL_RTX;
5462 /* Conditionally store back to EXPECT, lest we create a race condition
5463 with an improper store to memory. */
5464 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5465 the normal case where EXPECT is totally private, i.e. a register. At
5466 which point the store can be unconditional. */
5467 label = gen_label_rtx ();
5468 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
5469 GET_MODE (target), 1, label);
5470 emit_move_insn (expect, oldval);
5471 emit_label (label);
5473 return target;
5476 /* Expand the __atomic_load intrinsic:
5477 TYPE __atomic_load (TYPE *object, enum memmodel)
5478 EXP is the CALL_EXPR.
5479 TARGET is an optional place for us to store the results. */
5481 static rtx
5482 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
5484 rtx mem;
5485 enum memmodel model;
5487 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5488 if (is_mm_release (model) || is_mm_acq_rel (model))
5490 warning (OPT_Winvalid_memory_model,
5491 "invalid memory model for %<__atomic_load%>");
5492 model = MEMMODEL_SEQ_CST;
5495 if (!flag_inline_atomics)
5496 return NULL_RTX;
5498 /* Expand the operand. */
5499 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5501 return expand_atomic_load (target, mem, model);
5505 /* Expand the __atomic_store intrinsic:
5506 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5507 EXP is the CALL_EXPR.
5508 TARGET is an optional place for us to store the results. */
5510 static rtx
5511 expand_builtin_atomic_store (machine_mode mode, tree exp)
5513 rtx mem, val;
5514 enum memmodel model;
5516 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5517 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
5518 || is_mm_release (model)))
5520 warning (OPT_Winvalid_memory_model,
5521 "invalid memory model for %<__atomic_store%>");
5522 model = MEMMODEL_SEQ_CST;
5525 if (!flag_inline_atomics)
5526 return NULL_RTX;
5528 /* Expand the operands. */
5529 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5530 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5532 return expand_atomic_store (mem, val, model, false);
5535 /* Expand the __atomic_fetch_XXX intrinsic:
5536 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5537 EXP is the CALL_EXPR.
5538 TARGET is an optional place for us to store the results.
5539 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5540 FETCH_AFTER is true if returning the result of the operation.
5541 FETCH_AFTER is false if returning the value before the operation.
5542 IGNORE is true if the result is not used.
5543 EXT_CALL is the correct builtin for an external call if this cannot be
5544 resolved to an instruction sequence. */
5546 static rtx
5547 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
5548 enum rtx_code code, bool fetch_after,
5549 bool ignore, enum built_in_function ext_call)
5551 rtx val, mem, ret;
5552 enum memmodel model;
5553 tree fndecl;
5554 tree addr;
5556 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5558 /* Expand the operands. */
5559 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5560 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5562 /* Only try generating instructions if inlining is turned on. */
5563 if (flag_inline_atomics)
5565 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5566 if (ret)
5567 return ret;
5570 /* Return if a different routine isn't needed for the library call. */
5571 if (ext_call == BUILT_IN_NONE)
5572 return NULL_RTX;
5574 /* Change the call to the specified function. */
5575 fndecl = get_callee_fndecl (exp);
5576 addr = CALL_EXPR_FN (exp);
5577 STRIP_NOPS (addr);
5579 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5580 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5582 /* Expand the call here so we can emit trailing code. */
5583 ret = expand_call (exp, target, ignore);
5585 /* Replace the original function just in case it matters. */
5586 TREE_OPERAND (addr, 0) = fndecl;
5588 /* Then issue the arithmetic correction to return the right result. */
5589 if (!ignore)
5591 if (code == NOT)
5593 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5594 OPTAB_LIB_WIDEN);
5595 ret = expand_simple_unop (mode, NOT, ret, target, true);
5597 else
5598 ret = expand_simple_binop (mode, code, ret, val, target, true,
5599 OPTAB_LIB_WIDEN);
5601 return ret;
5605 #ifndef HAVE_atomic_clear
5606 # define HAVE_atomic_clear 0
5607 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5608 #endif
5610 /* Expand an atomic clear operation.
5611 void _atomic_clear (BOOL *obj, enum memmodel)
5612 EXP is the call expression. */
5614 static rtx
5615 expand_builtin_atomic_clear (tree exp)
5617 machine_mode mode;
5618 rtx mem, ret;
5619 enum memmodel model;
5621 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5622 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5623 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5625 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
5627 warning (OPT_Winvalid_memory_model,
5628 "invalid memory model for %<__atomic_store%>");
5629 model = MEMMODEL_SEQ_CST;
5632 if (HAVE_atomic_clear)
5634 emit_insn (gen_atomic_clear (mem, model));
5635 return const0_rtx;
5638 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5639 Failing that, a store is issued by __atomic_store. The only way this can
5640 fail is if the bool type is larger than a word size. Unlikely, but
5641 handle it anyway for completeness. Assume a single threaded model since
5642 there is no atomic support in this case, and no barriers are required. */
5643 ret = expand_atomic_store (mem, const0_rtx, model, true);
5644 if (!ret)
5645 emit_move_insn (mem, const0_rtx);
5646 return const0_rtx;
5649 /* Expand an atomic test_and_set operation.
5650 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5651 EXP is the call expression. */
5653 static rtx
5654 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5656 rtx mem;
5657 enum memmodel model;
5658 machine_mode mode;
5660 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5661 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5662 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5664 return expand_atomic_test_and_set (target, mem, model);
5668 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5669 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5671 static tree
5672 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5674 int size;
5675 machine_mode mode;
5676 unsigned int mode_align, type_align;
5678 if (TREE_CODE (arg0) != INTEGER_CST)
5679 return NULL_TREE;
5681 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5682 mode = mode_for_size (size, MODE_INT, 0);
5683 mode_align = GET_MODE_ALIGNMENT (mode);
5685 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5686 type_align = mode_align;
5687 else
5689 tree ttype = TREE_TYPE (arg1);
5691 /* This function is usually invoked and folded immediately by the front
5692 end before anything else has a chance to look at it. The pointer
5693 parameter at this point is usually cast to a void *, so check for that
5694 and look past the cast. */
5695 if (CONVERT_EXPR_P (arg1) && POINTER_TYPE_P (ttype)
5696 && VOID_TYPE_P (TREE_TYPE (ttype)))
5697 arg1 = TREE_OPERAND (arg1, 0);
5699 ttype = TREE_TYPE (arg1);
5700 gcc_assert (POINTER_TYPE_P (ttype));
5702 /* Get the underlying type of the object. */
5703 ttype = TREE_TYPE (ttype);
5704 type_align = TYPE_ALIGN (ttype);
5707 /* If the object has smaller alignment, the the lock free routines cannot
5708 be used. */
5709 if (type_align < mode_align)
5710 return boolean_false_node;
5712 /* Check if a compare_and_swap pattern exists for the mode which represents
5713 the required size. The pattern is not allowed to fail, so the existence
5714 of the pattern indicates support is present. */
5715 if (can_compare_and_swap_p (mode, true))
5716 return boolean_true_node;
5717 else
5718 return boolean_false_node;
5721 /* Return true if the parameters to call EXP represent an object which will
5722 always generate lock free instructions. The first argument represents the
5723 size of the object, and the second parameter is a pointer to the object
5724 itself. If NULL is passed for the object, then the result is based on
5725 typical alignment for an object of the specified size. Otherwise return
5726 false. */
5728 static rtx
5729 expand_builtin_atomic_always_lock_free (tree exp)
5731 tree size;
5732 tree arg0 = CALL_EXPR_ARG (exp, 0);
5733 tree arg1 = CALL_EXPR_ARG (exp, 1);
5735 if (TREE_CODE (arg0) != INTEGER_CST)
5737 error ("non-constant argument 1 to __atomic_always_lock_free");
5738 return const0_rtx;
5741 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5742 if (size == boolean_true_node)
5743 return const1_rtx;
5744 return const0_rtx;
5747 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5748 is lock free on this architecture. */
5750 static tree
5751 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5753 if (!flag_inline_atomics)
5754 return NULL_TREE;
5756 /* If it isn't always lock free, don't generate a result. */
5757 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5758 return boolean_true_node;
5760 return NULL_TREE;
5763 /* Return true if the parameters to call EXP represent an object which will
5764 always generate lock free instructions. The first argument represents the
5765 size of the object, and the second parameter is a pointer to the object
5766 itself. If NULL is passed for the object, then the result is based on
5767 typical alignment for an object of the specified size. Otherwise return
5768 NULL*/
5770 static rtx
5771 expand_builtin_atomic_is_lock_free (tree exp)
5773 tree size;
5774 tree arg0 = CALL_EXPR_ARG (exp, 0);
5775 tree arg1 = CALL_EXPR_ARG (exp, 1);
5777 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5779 error ("non-integer argument 1 to __atomic_is_lock_free");
5780 return NULL_RTX;
5783 if (!flag_inline_atomics)
5784 return NULL_RTX;
5786 /* If the value is known at compile time, return the RTX for it. */
5787 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5788 if (size == boolean_true_node)
5789 return const1_rtx;
5791 return NULL_RTX;
5794 /* Expand the __atomic_thread_fence intrinsic:
5795 void __atomic_thread_fence (enum memmodel)
5796 EXP is the CALL_EXPR. */
5798 static void
5799 expand_builtin_atomic_thread_fence (tree exp)
5801 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5802 expand_mem_thread_fence (model);
5805 /* Expand the __atomic_signal_fence intrinsic:
5806 void __atomic_signal_fence (enum memmodel)
5807 EXP is the CALL_EXPR. */
5809 static void
5810 expand_builtin_atomic_signal_fence (tree exp)
5812 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5813 expand_mem_signal_fence (model);
5816 /* Expand the __sync_synchronize intrinsic. */
5818 static void
5819 expand_builtin_sync_synchronize (void)
5821 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
5824 static rtx
5825 expand_builtin_thread_pointer (tree exp, rtx target)
5827 enum insn_code icode;
5828 if (!validate_arglist (exp, VOID_TYPE))
5829 return const0_rtx;
5830 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5831 if (icode != CODE_FOR_nothing)
5833 struct expand_operand op;
5834 /* If the target is not sutitable then create a new target. */
5835 if (target == NULL_RTX
5836 || !REG_P (target)
5837 || GET_MODE (target) != Pmode)
5838 target = gen_reg_rtx (Pmode);
5839 create_output_operand (&op, target, Pmode);
5840 expand_insn (icode, 1, &op);
5841 return target;
5843 error ("__builtin_thread_pointer is not supported on this target");
5844 return const0_rtx;
5847 static void
5848 expand_builtin_set_thread_pointer (tree exp)
5850 enum insn_code icode;
5851 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5852 return;
5853 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5854 if (icode != CODE_FOR_nothing)
5856 struct expand_operand op;
5857 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5858 Pmode, EXPAND_NORMAL);
5859 create_input_operand (&op, val, Pmode);
5860 expand_insn (icode, 1, &op);
5861 return;
5863 error ("__builtin_set_thread_pointer is not supported on this target");
5867 /* Emit code to restore the current value of stack. */
5869 static void
5870 expand_stack_restore (tree var)
5872 rtx_insn *prev;
5873 rtx sa = expand_normal (var);
5875 sa = convert_memory_address (Pmode, sa);
5877 prev = get_last_insn ();
5878 emit_stack_restore (SAVE_BLOCK, sa);
5880 record_new_stack_level ();
5882 fixup_args_size_notes (prev, get_last_insn (), 0);
5885 /* Emit code to save the current value of stack. */
5887 static rtx
5888 expand_stack_save (void)
5890 rtx ret = NULL_RTX;
5892 emit_stack_save (SAVE_BLOCK, &ret);
5893 return ret;
5897 /* Expand OpenACC acc_on_device.
5899 This has to happen late (that is, not in early folding; expand_builtin_*,
5900 rather than fold_builtin_*), as we have to act differently for host and
5901 acceleration device (ACCEL_COMPILER conditional). */
5903 static rtx
5904 expand_builtin_acc_on_device (tree exp ATTRIBUTE_UNUSED,
5905 rtx target ATTRIBUTE_UNUSED)
5907 #ifdef ACCEL_COMPILER
5908 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5909 return NULL_RTX;
5911 tree arg = CALL_EXPR_ARG (exp, 0);
5913 /* Return (arg == v1 || arg == v2) ? 1 : 0. */
5914 machine_mode v_mode = TYPE_MODE (TREE_TYPE (arg));
5915 rtx v = expand_normal (arg), v1, v2;
5916 v1 = GEN_INT (GOMP_DEVICE_NOT_HOST);
5917 v2 = GEN_INT (ACCEL_COMPILER_acc_device);
5918 machine_mode target_mode = TYPE_MODE (integer_type_node);
5919 if (!target || !register_operand (target, target_mode))
5920 target = gen_reg_rtx (target_mode);
5921 emit_move_insn (target, const1_rtx);
5922 rtx_code_label *done_label = gen_label_rtx ();
5923 do_compare_rtx_and_jump (v, v1, EQ, false, v_mode, NULL_RTX,
5924 NULL, done_label, PROB_EVEN);
5925 do_compare_rtx_and_jump (v, v2, EQ, false, v_mode, NULL_RTX,
5926 NULL, done_label, PROB_EVEN);
5927 emit_move_insn (target, const0_rtx);
5928 emit_label (done_label);
5930 return target;
5931 #else
5932 return NULL;
5933 #endif
5937 /* Expand an expression EXP that calls a built-in function,
5938 with result going to TARGET if that's convenient
5939 (and in mode MODE if that's convenient).
5940 SUBTARGET may be used as the target for computing one of EXP's operands.
5941 IGNORE is nonzero if the value is to be ignored. */
5944 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
5945 int ignore)
5947 tree fndecl = get_callee_fndecl (exp);
5948 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5949 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5950 int flags;
5952 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5953 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5955 /* When ASan is enabled, we don't want to expand some memory/string
5956 builtins and rely on libsanitizer's hooks. This allows us to avoid
5957 redundant checks and be sure, that possible overflow will be detected
5958 by ASan. */
5960 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
5961 return expand_call (exp, target, ignore);
5963 /* When not optimizing, generate calls to library functions for a certain
5964 set of builtins. */
5965 if (!optimize
5966 && !called_as_built_in (fndecl)
5967 && fcode != BUILT_IN_FORK
5968 && fcode != BUILT_IN_EXECL
5969 && fcode != BUILT_IN_EXECV
5970 && fcode != BUILT_IN_EXECLP
5971 && fcode != BUILT_IN_EXECLE
5972 && fcode != BUILT_IN_EXECVP
5973 && fcode != BUILT_IN_EXECVE
5974 && fcode != BUILT_IN_ALLOCA
5975 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5976 && fcode != BUILT_IN_FREE
5977 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
5978 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
5979 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
5980 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
5981 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5982 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
5983 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
5984 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
5985 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
5986 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
5987 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND
5988 && fcode != BUILT_IN_CHKP_BNDRET)
5989 return expand_call (exp, target, ignore);
5991 /* The built-in function expanders test for target == const0_rtx
5992 to determine whether the function's result will be ignored. */
5993 if (ignore)
5994 target = const0_rtx;
5996 /* If the result of a pure or const built-in function is ignored, and
5997 none of its arguments are volatile, we can avoid expanding the
5998 built-in call and just evaluate the arguments for side-effects. */
5999 if (target == const0_rtx
6000 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
6001 && !(flags & ECF_LOOPING_CONST_OR_PURE))
6003 bool volatilep = false;
6004 tree arg;
6005 call_expr_arg_iterator iter;
6007 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6008 if (TREE_THIS_VOLATILE (arg))
6010 volatilep = true;
6011 break;
6014 if (! volatilep)
6016 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6017 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6018 return const0_rtx;
6022 /* expand_builtin_with_bounds is supposed to be used for
6023 instrumented builtin calls. */
6024 gcc_assert (!CALL_WITH_BOUNDS_P (exp));
6026 switch (fcode)
6028 CASE_FLT_FN (BUILT_IN_FABS):
6029 case BUILT_IN_FABSD32:
6030 case BUILT_IN_FABSD64:
6031 case BUILT_IN_FABSD128:
6032 target = expand_builtin_fabs (exp, target, subtarget);
6033 if (target)
6034 return target;
6035 break;
6037 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6038 target = expand_builtin_copysign (exp, target, subtarget);
6039 if (target)
6040 return target;
6041 break;
6043 /* Just do a normal library call if we were unable to fold
6044 the values. */
6045 CASE_FLT_FN (BUILT_IN_CABS):
6046 break;
6048 CASE_FLT_FN (BUILT_IN_EXP):
6049 CASE_FLT_FN (BUILT_IN_EXP10):
6050 CASE_FLT_FN (BUILT_IN_POW10):
6051 CASE_FLT_FN (BUILT_IN_EXP2):
6052 CASE_FLT_FN (BUILT_IN_EXPM1):
6053 CASE_FLT_FN (BUILT_IN_LOGB):
6054 CASE_FLT_FN (BUILT_IN_LOG):
6055 CASE_FLT_FN (BUILT_IN_LOG10):
6056 CASE_FLT_FN (BUILT_IN_LOG2):
6057 CASE_FLT_FN (BUILT_IN_LOG1P):
6058 CASE_FLT_FN (BUILT_IN_TAN):
6059 CASE_FLT_FN (BUILT_IN_ASIN):
6060 CASE_FLT_FN (BUILT_IN_ACOS):
6061 CASE_FLT_FN (BUILT_IN_ATAN):
6062 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
6063 /* Treat these like sqrt only if unsafe math optimizations are allowed,
6064 because of possible accuracy problems. */
6065 if (! flag_unsafe_math_optimizations)
6066 break;
6067 CASE_FLT_FN (BUILT_IN_SQRT):
6068 CASE_FLT_FN (BUILT_IN_FLOOR):
6069 CASE_FLT_FN (BUILT_IN_CEIL):
6070 CASE_FLT_FN (BUILT_IN_TRUNC):
6071 CASE_FLT_FN (BUILT_IN_ROUND):
6072 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6073 CASE_FLT_FN (BUILT_IN_RINT):
6074 target = expand_builtin_mathfn (exp, target, subtarget);
6075 if (target)
6076 return target;
6077 break;
6079 CASE_FLT_FN (BUILT_IN_FMA):
6080 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
6081 if (target)
6082 return target;
6083 break;
6085 CASE_FLT_FN (BUILT_IN_ILOGB):
6086 if (! flag_unsafe_math_optimizations)
6087 break;
6088 CASE_FLT_FN (BUILT_IN_ISINF):
6089 CASE_FLT_FN (BUILT_IN_FINITE):
6090 case BUILT_IN_ISFINITE:
6091 case BUILT_IN_ISNORMAL:
6092 target = expand_builtin_interclass_mathfn (exp, target);
6093 if (target)
6094 return target;
6095 break;
6097 CASE_FLT_FN (BUILT_IN_ICEIL):
6098 CASE_FLT_FN (BUILT_IN_LCEIL):
6099 CASE_FLT_FN (BUILT_IN_LLCEIL):
6100 CASE_FLT_FN (BUILT_IN_LFLOOR):
6101 CASE_FLT_FN (BUILT_IN_IFLOOR):
6102 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6103 target = expand_builtin_int_roundingfn (exp, target);
6104 if (target)
6105 return target;
6106 break;
6108 CASE_FLT_FN (BUILT_IN_IRINT):
6109 CASE_FLT_FN (BUILT_IN_LRINT):
6110 CASE_FLT_FN (BUILT_IN_LLRINT):
6111 CASE_FLT_FN (BUILT_IN_IROUND):
6112 CASE_FLT_FN (BUILT_IN_LROUND):
6113 CASE_FLT_FN (BUILT_IN_LLROUND):
6114 target = expand_builtin_int_roundingfn_2 (exp, target);
6115 if (target)
6116 return target;
6117 break;
6119 CASE_FLT_FN (BUILT_IN_POWI):
6120 target = expand_builtin_powi (exp, target);
6121 if (target)
6122 return target;
6123 break;
6125 CASE_FLT_FN (BUILT_IN_ATAN2):
6126 CASE_FLT_FN (BUILT_IN_LDEXP):
6127 CASE_FLT_FN (BUILT_IN_SCALB):
6128 CASE_FLT_FN (BUILT_IN_SCALBN):
6129 CASE_FLT_FN (BUILT_IN_SCALBLN):
6130 if (! flag_unsafe_math_optimizations)
6131 break;
6133 CASE_FLT_FN (BUILT_IN_FMOD):
6134 CASE_FLT_FN (BUILT_IN_REMAINDER):
6135 CASE_FLT_FN (BUILT_IN_DREM):
6136 CASE_FLT_FN (BUILT_IN_POW):
6137 target = expand_builtin_mathfn_2 (exp, target, subtarget);
6138 if (target)
6139 return target;
6140 break;
6142 CASE_FLT_FN (BUILT_IN_CEXPI):
6143 target = expand_builtin_cexpi (exp, target);
6144 gcc_assert (target);
6145 return target;
6147 CASE_FLT_FN (BUILT_IN_SIN):
6148 CASE_FLT_FN (BUILT_IN_COS):
6149 if (! flag_unsafe_math_optimizations)
6150 break;
6151 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6152 if (target)
6153 return target;
6154 break;
6156 CASE_FLT_FN (BUILT_IN_SINCOS):
6157 if (! flag_unsafe_math_optimizations)
6158 break;
6159 target = expand_builtin_sincos (exp);
6160 if (target)
6161 return target;
6162 break;
6164 case BUILT_IN_APPLY_ARGS:
6165 return expand_builtin_apply_args ();
6167 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6168 FUNCTION with a copy of the parameters described by
6169 ARGUMENTS, and ARGSIZE. It returns a block of memory
6170 allocated on the stack into which is stored all the registers
6171 that might possibly be used for returning the result of a
6172 function. ARGUMENTS is the value returned by
6173 __builtin_apply_args. ARGSIZE is the number of bytes of
6174 arguments that must be copied. ??? How should this value be
6175 computed? We'll also need a safe worst case value for varargs
6176 functions. */
6177 case BUILT_IN_APPLY:
6178 if (!validate_arglist (exp, POINTER_TYPE,
6179 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6180 && !validate_arglist (exp, REFERENCE_TYPE,
6181 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6182 return const0_rtx;
6183 else
6185 rtx ops[3];
6187 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6188 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6189 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6191 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6194 /* __builtin_return (RESULT) causes the function to return the
6195 value described by RESULT. RESULT is address of the block of
6196 memory returned by __builtin_apply. */
6197 case BUILT_IN_RETURN:
6198 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6199 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6200 return const0_rtx;
6202 case BUILT_IN_SAVEREGS:
6203 return expand_builtin_saveregs ();
6205 case BUILT_IN_VA_ARG_PACK:
6206 /* All valid uses of __builtin_va_arg_pack () are removed during
6207 inlining. */
6208 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6209 return const0_rtx;
6211 case BUILT_IN_VA_ARG_PACK_LEN:
6212 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6213 inlining. */
6214 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6215 return const0_rtx;
6217 /* Return the address of the first anonymous stack arg. */
6218 case BUILT_IN_NEXT_ARG:
6219 if (fold_builtin_next_arg (exp, false))
6220 return const0_rtx;
6221 return expand_builtin_next_arg ();
6223 case BUILT_IN_CLEAR_CACHE:
6224 target = expand_builtin___clear_cache (exp);
6225 if (target)
6226 return target;
6227 break;
6229 case BUILT_IN_CLASSIFY_TYPE:
6230 return expand_builtin_classify_type (exp);
6232 case BUILT_IN_CONSTANT_P:
6233 return const0_rtx;
6235 case BUILT_IN_FRAME_ADDRESS:
6236 case BUILT_IN_RETURN_ADDRESS:
6237 return expand_builtin_frame_address (fndecl, exp);
6239 /* Returns the address of the area where the structure is returned.
6240 0 otherwise. */
6241 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6242 if (call_expr_nargs (exp) != 0
6243 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6244 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6245 return const0_rtx;
6246 else
6247 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6249 case BUILT_IN_ALLOCA:
6250 case BUILT_IN_ALLOCA_WITH_ALIGN:
6251 /* If the allocation stems from the declaration of a variable-sized
6252 object, it cannot accumulate. */
6253 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6254 if (target)
6255 return target;
6256 break;
6258 case BUILT_IN_STACK_SAVE:
6259 return expand_stack_save ();
6261 case BUILT_IN_STACK_RESTORE:
6262 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6263 return const0_rtx;
6265 case BUILT_IN_BSWAP16:
6266 case BUILT_IN_BSWAP32:
6267 case BUILT_IN_BSWAP64:
6268 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6269 if (target)
6270 return target;
6271 break;
6273 CASE_INT_FN (BUILT_IN_FFS):
6274 target = expand_builtin_unop (target_mode, exp, target,
6275 subtarget, ffs_optab);
6276 if (target)
6277 return target;
6278 break;
6280 CASE_INT_FN (BUILT_IN_CLZ):
6281 target = expand_builtin_unop (target_mode, exp, target,
6282 subtarget, clz_optab);
6283 if (target)
6284 return target;
6285 break;
6287 CASE_INT_FN (BUILT_IN_CTZ):
6288 target = expand_builtin_unop (target_mode, exp, target,
6289 subtarget, ctz_optab);
6290 if (target)
6291 return target;
6292 break;
6294 CASE_INT_FN (BUILT_IN_CLRSB):
6295 target = expand_builtin_unop (target_mode, exp, target,
6296 subtarget, clrsb_optab);
6297 if (target)
6298 return target;
6299 break;
6301 CASE_INT_FN (BUILT_IN_POPCOUNT):
6302 target = expand_builtin_unop (target_mode, exp, target,
6303 subtarget, popcount_optab);
6304 if (target)
6305 return target;
6306 break;
6308 CASE_INT_FN (BUILT_IN_PARITY):
6309 target = expand_builtin_unop (target_mode, exp, target,
6310 subtarget, parity_optab);
6311 if (target)
6312 return target;
6313 break;
6315 case BUILT_IN_STRLEN:
6316 target = expand_builtin_strlen (exp, target, target_mode);
6317 if (target)
6318 return target;
6319 break;
6321 case BUILT_IN_STRCPY:
6322 target = expand_builtin_strcpy (exp, target);
6323 if (target)
6324 return target;
6325 break;
6327 case BUILT_IN_STRNCPY:
6328 target = expand_builtin_strncpy (exp, target);
6329 if (target)
6330 return target;
6331 break;
6333 case BUILT_IN_STPCPY:
6334 target = expand_builtin_stpcpy (exp, target, mode);
6335 if (target)
6336 return target;
6337 break;
6339 case BUILT_IN_MEMCPY:
6340 target = expand_builtin_memcpy (exp, target);
6341 if (target)
6342 return target;
6343 break;
6345 case BUILT_IN_MEMPCPY:
6346 target = expand_builtin_mempcpy (exp, target, mode);
6347 if (target)
6348 return target;
6349 break;
6351 case BUILT_IN_MEMSET:
6352 target = expand_builtin_memset (exp, target, mode);
6353 if (target)
6354 return target;
6355 break;
6357 case BUILT_IN_BZERO:
6358 target = expand_builtin_bzero (exp);
6359 if (target)
6360 return target;
6361 break;
6363 case BUILT_IN_STRCMP:
6364 target = expand_builtin_strcmp (exp, target);
6365 if (target)
6366 return target;
6367 break;
6369 case BUILT_IN_STRNCMP:
6370 target = expand_builtin_strncmp (exp, target, mode);
6371 if (target)
6372 return target;
6373 break;
6375 case BUILT_IN_BCMP:
6376 case BUILT_IN_MEMCMP:
6377 target = expand_builtin_memcmp (exp, target, mode);
6378 if (target)
6379 return target;
6380 break;
6382 case BUILT_IN_SETJMP:
6383 /* This should have been lowered to the builtins below. */
6384 gcc_unreachable ();
6386 case BUILT_IN_SETJMP_SETUP:
6387 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6388 and the receiver label. */
6389 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6391 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6392 VOIDmode, EXPAND_NORMAL);
6393 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6394 rtx_insn *label_r = label_rtx (label);
6396 /* This is copied from the handling of non-local gotos. */
6397 expand_builtin_setjmp_setup (buf_addr, label_r);
6398 nonlocal_goto_handler_labels
6399 = gen_rtx_INSN_LIST (VOIDmode, label_r,
6400 nonlocal_goto_handler_labels);
6401 /* ??? Do not let expand_label treat us as such since we would
6402 not want to be both on the list of non-local labels and on
6403 the list of forced labels. */
6404 FORCED_LABEL (label) = 0;
6405 return const0_rtx;
6407 break;
6409 case BUILT_IN_SETJMP_RECEIVER:
6410 /* __builtin_setjmp_receiver is passed the receiver label. */
6411 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6413 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6414 rtx_insn *label_r = label_rtx (label);
6416 expand_builtin_setjmp_receiver (label_r);
6417 return const0_rtx;
6419 break;
6421 /* __builtin_longjmp is passed a pointer to an array of five words.
6422 It's similar to the C library longjmp function but works with
6423 __builtin_setjmp above. */
6424 case BUILT_IN_LONGJMP:
6425 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6427 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6428 VOIDmode, EXPAND_NORMAL);
6429 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6431 if (value != const1_rtx)
6433 error ("%<__builtin_longjmp%> second argument must be 1");
6434 return const0_rtx;
6437 expand_builtin_longjmp (buf_addr, value);
6438 return const0_rtx;
6440 break;
6442 case BUILT_IN_NONLOCAL_GOTO:
6443 target = expand_builtin_nonlocal_goto (exp);
6444 if (target)
6445 return target;
6446 break;
6448 /* This updates the setjmp buffer that is its argument with the value
6449 of the current stack pointer. */
6450 case BUILT_IN_UPDATE_SETJMP_BUF:
6451 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6453 rtx buf_addr
6454 = expand_normal (CALL_EXPR_ARG (exp, 0));
6456 expand_builtin_update_setjmp_buf (buf_addr);
6457 return const0_rtx;
6459 break;
6461 case BUILT_IN_TRAP:
6462 expand_builtin_trap ();
6463 return const0_rtx;
6465 case BUILT_IN_UNREACHABLE:
6466 expand_builtin_unreachable ();
6467 return const0_rtx;
6469 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6470 case BUILT_IN_SIGNBITD32:
6471 case BUILT_IN_SIGNBITD64:
6472 case BUILT_IN_SIGNBITD128:
6473 target = expand_builtin_signbit (exp, target);
6474 if (target)
6475 return target;
6476 break;
6478 /* Various hooks for the DWARF 2 __throw routine. */
6479 case BUILT_IN_UNWIND_INIT:
6480 expand_builtin_unwind_init ();
6481 return const0_rtx;
6482 case BUILT_IN_DWARF_CFA:
6483 return virtual_cfa_rtx;
6484 #ifdef DWARF2_UNWIND_INFO
6485 case BUILT_IN_DWARF_SP_COLUMN:
6486 return expand_builtin_dwarf_sp_column ();
6487 case BUILT_IN_INIT_DWARF_REG_SIZES:
6488 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6489 return const0_rtx;
6490 #endif
6491 case BUILT_IN_FROB_RETURN_ADDR:
6492 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6493 case BUILT_IN_EXTRACT_RETURN_ADDR:
6494 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6495 case BUILT_IN_EH_RETURN:
6496 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6497 CALL_EXPR_ARG (exp, 1));
6498 return const0_rtx;
6499 case BUILT_IN_EH_RETURN_DATA_REGNO:
6500 return expand_builtin_eh_return_data_regno (exp);
6501 case BUILT_IN_EXTEND_POINTER:
6502 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6503 case BUILT_IN_EH_POINTER:
6504 return expand_builtin_eh_pointer (exp);
6505 case BUILT_IN_EH_FILTER:
6506 return expand_builtin_eh_filter (exp);
6507 case BUILT_IN_EH_COPY_VALUES:
6508 return expand_builtin_eh_copy_values (exp);
6510 case BUILT_IN_VA_START:
6511 return expand_builtin_va_start (exp);
6512 case BUILT_IN_VA_END:
6513 return expand_builtin_va_end (exp);
6514 case BUILT_IN_VA_COPY:
6515 return expand_builtin_va_copy (exp);
6516 case BUILT_IN_EXPECT:
6517 return expand_builtin_expect (exp, target);
6518 case BUILT_IN_ASSUME_ALIGNED:
6519 return expand_builtin_assume_aligned (exp, target);
6520 case BUILT_IN_PREFETCH:
6521 expand_builtin_prefetch (exp);
6522 return const0_rtx;
6524 case BUILT_IN_INIT_TRAMPOLINE:
6525 return expand_builtin_init_trampoline (exp, true);
6526 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6527 return expand_builtin_init_trampoline (exp, false);
6528 case BUILT_IN_ADJUST_TRAMPOLINE:
6529 return expand_builtin_adjust_trampoline (exp);
6531 case BUILT_IN_FORK:
6532 case BUILT_IN_EXECL:
6533 case BUILT_IN_EXECV:
6534 case BUILT_IN_EXECLP:
6535 case BUILT_IN_EXECLE:
6536 case BUILT_IN_EXECVP:
6537 case BUILT_IN_EXECVE:
6538 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6539 if (target)
6540 return target;
6541 break;
6543 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6544 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6545 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6546 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6547 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6548 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6549 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6550 if (target)
6551 return target;
6552 break;
6554 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6555 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6556 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6557 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6558 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6559 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6560 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6561 if (target)
6562 return target;
6563 break;
6565 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6566 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6567 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6568 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6569 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6570 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6571 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6572 if (target)
6573 return target;
6574 break;
6576 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6577 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6578 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6579 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6580 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6581 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6582 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6583 if (target)
6584 return target;
6585 break;
6587 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6588 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6589 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6590 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6591 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6592 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6593 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6594 if (target)
6595 return target;
6596 break;
6598 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6599 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6600 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6601 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6602 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6603 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6604 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6605 if (target)
6606 return target;
6607 break;
6609 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6610 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6611 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6612 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6613 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6614 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6615 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6616 if (target)
6617 return target;
6618 break;
6620 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6621 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6622 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6623 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6624 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6625 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6626 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6627 if (target)
6628 return target;
6629 break;
6631 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6632 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6633 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6634 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6635 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6636 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6637 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6638 if (target)
6639 return target;
6640 break;
6642 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6643 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6644 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6645 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6646 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6647 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6648 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6649 if (target)
6650 return target;
6651 break;
6653 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6654 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6655 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6656 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6657 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6658 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6659 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6660 if (target)
6661 return target;
6662 break;
6664 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6665 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6666 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6667 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6668 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6669 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6670 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6671 if (target)
6672 return target;
6673 break;
6675 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6676 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6677 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6678 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6679 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6680 if (mode == VOIDmode)
6681 mode = TYPE_MODE (boolean_type_node);
6682 if (!target || !register_operand (target, mode))
6683 target = gen_reg_rtx (mode);
6685 mode = get_builtin_sync_mode
6686 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6687 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6688 if (target)
6689 return target;
6690 break;
6692 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6693 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6694 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6695 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6696 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6697 mode = get_builtin_sync_mode
6698 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6699 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6700 if (target)
6701 return target;
6702 break;
6704 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6705 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6706 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6707 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6708 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6709 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6710 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6711 if (target)
6712 return target;
6713 break;
6715 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6716 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6717 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6718 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6719 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6720 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6721 expand_builtin_sync_lock_release (mode, exp);
6722 return const0_rtx;
6724 case BUILT_IN_SYNC_SYNCHRONIZE:
6725 expand_builtin_sync_synchronize ();
6726 return const0_rtx;
6728 case BUILT_IN_ATOMIC_EXCHANGE_1:
6729 case BUILT_IN_ATOMIC_EXCHANGE_2:
6730 case BUILT_IN_ATOMIC_EXCHANGE_4:
6731 case BUILT_IN_ATOMIC_EXCHANGE_8:
6732 case BUILT_IN_ATOMIC_EXCHANGE_16:
6733 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6734 target = expand_builtin_atomic_exchange (mode, exp, target);
6735 if (target)
6736 return target;
6737 break;
6739 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6740 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6741 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6742 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6743 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6745 unsigned int nargs, z;
6746 vec<tree, va_gc> *vec;
6748 mode =
6749 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6750 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6751 if (target)
6752 return target;
6754 /* If this is turned into an external library call, the weak parameter
6755 must be dropped to match the expected parameter list. */
6756 nargs = call_expr_nargs (exp);
6757 vec_alloc (vec, nargs - 1);
6758 for (z = 0; z < 3; z++)
6759 vec->quick_push (CALL_EXPR_ARG (exp, z));
6760 /* Skip the boolean weak parameter. */
6761 for (z = 4; z < 6; z++)
6762 vec->quick_push (CALL_EXPR_ARG (exp, z));
6763 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6764 break;
6767 case BUILT_IN_ATOMIC_LOAD_1:
6768 case BUILT_IN_ATOMIC_LOAD_2:
6769 case BUILT_IN_ATOMIC_LOAD_4:
6770 case BUILT_IN_ATOMIC_LOAD_8:
6771 case BUILT_IN_ATOMIC_LOAD_16:
6772 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6773 target = expand_builtin_atomic_load (mode, exp, target);
6774 if (target)
6775 return target;
6776 break;
6778 case BUILT_IN_ATOMIC_STORE_1:
6779 case BUILT_IN_ATOMIC_STORE_2:
6780 case BUILT_IN_ATOMIC_STORE_4:
6781 case BUILT_IN_ATOMIC_STORE_8:
6782 case BUILT_IN_ATOMIC_STORE_16:
6783 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6784 target = expand_builtin_atomic_store (mode, exp);
6785 if (target)
6786 return const0_rtx;
6787 break;
6789 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6790 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6791 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6792 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6793 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6795 enum built_in_function lib;
6796 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6797 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6798 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6799 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6800 ignore, lib);
6801 if (target)
6802 return target;
6803 break;
6805 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6806 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6807 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6808 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6809 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6811 enum built_in_function lib;
6812 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6813 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6814 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6815 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6816 ignore, lib);
6817 if (target)
6818 return target;
6819 break;
6821 case BUILT_IN_ATOMIC_AND_FETCH_1:
6822 case BUILT_IN_ATOMIC_AND_FETCH_2:
6823 case BUILT_IN_ATOMIC_AND_FETCH_4:
6824 case BUILT_IN_ATOMIC_AND_FETCH_8:
6825 case BUILT_IN_ATOMIC_AND_FETCH_16:
6827 enum built_in_function lib;
6828 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6829 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6830 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6831 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6832 ignore, lib);
6833 if (target)
6834 return target;
6835 break;
6837 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6838 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6839 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6840 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6841 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6843 enum built_in_function lib;
6844 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6845 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6846 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6847 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6848 ignore, lib);
6849 if (target)
6850 return target;
6851 break;
6853 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6854 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6855 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6856 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6857 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6859 enum built_in_function lib;
6860 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6861 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6862 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6863 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6864 ignore, lib);
6865 if (target)
6866 return target;
6867 break;
6869 case BUILT_IN_ATOMIC_OR_FETCH_1:
6870 case BUILT_IN_ATOMIC_OR_FETCH_2:
6871 case BUILT_IN_ATOMIC_OR_FETCH_4:
6872 case BUILT_IN_ATOMIC_OR_FETCH_8:
6873 case BUILT_IN_ATOMIC_OR_FETCH_16:
6875 enum built_in_function lib;
6876 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6877 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6878 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6879 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6880 ignore, lib);
6881 if (target)
6882 return target;
6883 break;
6885 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6886 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6887 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6888 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6889 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6890 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6891 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6892 ignore, BUILT_IN_NONE);
6893 if (target)
6894 return target;
6895 break;
6897 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6898 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6899 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6900 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6901 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6902 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6903 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6904 ignore, BUILT_IN_NONE);
6905 if (target)
6906 return target;
6907 break;
6909 case BUILT_IN_ATOMIC_FETCH_AND_1:
6910 case BUILT_IN_ATOMIC_FETCH_AND_2:
6911 case BUILT_IN_ATOMIC_FETCH_AND_4:
6912 case BUILT_IN_ATOMIC_FETCH_AND_8:
6913 case BUILT_IN_ATOMIC_FETCH_AND_16:
6914 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6915 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6916 ignore, BUILT_IN_NONE);
6917 if (target)
6918 return target;
6919 break;
6921 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6922 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6923 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6924 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6925 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6926 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6927 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6928 ignore, BUILT_IN_NONE);
6929 if (target)
6930 return target;
6931 break;
6933 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6934 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6935 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6936 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6937 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6938 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6939 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6940 ignore, BUILT_IN_NONE);
6941 if (target)
6942 return target;
6943 break;
6945 case BUILT_IN_ATOMIC_FETCH_OR_1:
6946 case BUILT_IN_ATOMIC_FETCH_OR_2:
6947 case BUILT_IN_ATOMIC_FETCH_OR_4:
6948 case BUILT_IN_ATOMIC_FETCH_OR_8:
6949 case BUILT_IN_ATOMIC_FETCH_OR_16:
6950 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6951 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6952 ignore, BUILT_IN_NONE);
6953 if (target)
6954 return target;
6955 break;
6957 case BUILT_IN_ATOMIC_TEST_AND_SET:
6958 return expand_builtin_atomic_test_and_set (exp, target);
6960 case BUILT_IN_ATOMIC_CLEAR:
6961 return expand_builtin_atomic_clear (exp);
6963 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6964 return expand_builtin_atomic_always_lock_free (exp);
6966 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6967 target = expand_builtin_atomic_is_lock_free (exp);
6968 if (target)
6969 return target;
6970 break;
6972 case BUILT_IN_ATOMIC_THREAD_FENCE:
6973 expand_builtin_atomic_thread_fence (exp);
6974 return const0_rtx;
6976 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6977 expand_builtin_atomic_signal_fence (exp);
6978 return const0_rtx;
6980 case BUILT_IN_OBJECT_SIZE:
6981 return expand_builtin_object_size (exp);
6983 case BUILT_IN_MEMCPY_CHK:
6984 case BUILT_IN_MEMPCPY_CHK:
6985 case BUILT_IN_MEMMOVE_CHK:
6986 case BUILT_IN_MEMSET_CHK:
6987 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6988 if (target)
6989 return target;
6990 break;
6992 case BUILT_IN_STRCPY_CHK:
6993 case BUILT_IN_STPCPY_CHK:
6994 case BUILT_IN_STRNCPY_CHK:
6995 case BUILT_IN_STPNCPY_CHK:
6996 case BUILT_IN_STRCAT_CHK:
6997 case BUILT_IN_STRNCAT_CHK:
6998 case BUILT_IN_SNPRINTF_CHK:
6999 case BUILT_IN_VSNPRINTF_CHK:
7000 maybe_emit_chk_warning (exp, fcode);
7001 break;
7003 case BUILT_IN_SPRINTF_CHK:
7004 case BUILT_IN_VSPRINTF_CHK:
7005 maybe_emit_sprintf_chk_warning (exp, fcode);
7006 break;
7008 case BUILT_IN_FREE:
7009 if (warn_free_nonheap_object)
7010 maybe_emit_free_warning (exp);
7011 break;
7013 case BUILT_IN_THREAD_POINTER:
7014 return expand_builtin_thread_pointer (exp, target);
7016 case BUILT_IN_SET_THREAD_POINTER:
7017 expand_builtin_set_thread_pointer (exp);
7018 return const0_rtx;
7020 case BUILT_IN_CILK_DETACH:
7021 expand_builtin_cilk_detach (exp);
7022 return const0_rtx;
7024 case BUILT_IN_CILK_POP_FRAME:
7025 expand_builtin_cilk_pop_frame (exp);
7026 return const0_rtx;
7028 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
7029 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
7030 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
7031 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
7032 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
7033 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
7034 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
7035 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
7036 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
7037 case BUILT_IN_CHKP_GET_PTR_LBOUND:
7038 case BUILT_IN_CHKP_GET_PTR_UBOUND:
7039 /* We allow user CHKP builtins if Pointer Bounds
7040 Checker is off. */
7041 if (!chkp_function_instrumented_p (current_function_decl))
7043 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
7044 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS
7045 || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS
7046 || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS
7047 || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS)
7048 return expand_normal (CALL_EXPR_ARG (exp, 0));
7049 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
7050 return expand_normal (size_zero_node);
7051 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
7052 return expand_normal (size_int (-1));
7053 else
7054 return const0_rtx;
7056 /* FALLTHROUGH */
7058 case BUILT_IN_CHKP_BNDMK:
7059 case BUILT_IN_CHKP_BNDSTX:
7060 case BUILT_IN_CHKP_BNDCL:
7061 case BUILT_IN_CHKP_BNDCU:
7062 case BUILT_IN_CHKP_BNDLDX:
7063 case BUILT_IN_CHKP_BNDRET:
7064 case BUILT_IN_CHKP_INTERSECT:
7065 case BUILT_IN_CHKP_NARROW:
7066 case BUILT_IN_CHKP_EXTRACT_LOWER:
7067 case BUILT_IN_CHKP_EXTRACT_UPPER:
7068 /* Software implementation of Pointer Bounds Checker is NYI.
7069 Target support is required. */
7070 error ("Your target platform does not support -fcheck-pointer-bounds");
7071 break;
7073 case BUILT_IN_ACC_ON_DEVICE:
7074 target = expand_builtin_acc_on_device (exp, target);
7075 if (target)
7076 return target;
7077 break;
7079 default: /* just do library call, if unknown builtin */
7080 break;
7083 /* The switch statement above can drop through to cause the function
7084 to be called normally. */
7085 return expand_call (exp, target, ignore);
7088 /* Similar to expand_builtin but is used for instrumented calls. */
7091 expand_builtin_with_bounds (tree exp, rtx target,
7092 rtx subtarget ATTRIBUTE_UNUSED,
7093 machine_mode mode, int ignore)
7095 tree fndecl = get_callee_fndecl (exp);
7096 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7098 gcc_assert (CALL_WITH_BOUNDS_P (exp));
7100 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7101 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7103 gcc_assert (fcode > BEGIN_CHKP_BUILTINS
7104 && fcode < END_CHKP_BUILTINS);
7106 switch (fcode)
7108 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP:
7109 target = expand_builtin_memcpy_with_bounds (exp, target);
7110 if (target)
7111 return target;
7112 break;
7114 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP:
7115 target = expand_builtin_mempcpy_with_bounds (exp, target, mode);
7116 if (target)
7117 return target;
7118 break;
7120 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP:
7121 target = expand_builtin_memset_with_bounds (exp, target, mode);
7122 if (target)
7123 return target;
7124 break;
7126 default:
7127 break;
7130 /* The switch statement above can drop through to cause the function
7131 to be called normally. */
7132 return expand_call (exp, target, ignore);
7135 /* Determine whether a tree node represents a call to a built-in
7136 function. If the tree T is a call to a built-in function with
7137 the right number of arguments of the appropriate types, return
7138 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7139 Otherwise the return value is END_BUILTINS. */
7141 enum built_in_function
7142 builtin_mathfn_code (const_tree t)
7144 const_tree fndecl, arg, parmlist;
7145 const_tree argtype, parmtype;
7146 const_call_expr_arg_iterator iter;
7148 if (TREE_CODE (t) != CALL_EXPR
7149 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
7150 return END_BUILTINS;
7152 fndecl = get_callee_fndecl (t);
7153 if (fndecl == NULL_TREE
7154 || TREE_CODE (fndecl) != FUNCTION_DECL
7155 || ! DECL_BUILT_IN (fndecl)
7156 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7157 return END_BUILTINS;
7159 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7160 init_const_call_expr_arg_iterator (t, &iter);
7161 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7163 /* If a function doesn't take a variable number of arguments,
7164 the last element in the list will have type `void'. */
7165 parmtype = TREE_VALUE (parmlist);
7166 if (VOID_TYPE_P (parmtype))
7168 if (more_const_call_expr_args_p (&iter))
7169 return END_BUILTINS;
7170 return DECL_FUNCTION_CODE (fndecl);
7173 if (! more_const_call_expr_args_p (&iter))
7174 return END_BUILTINS;
7176 arg = next_const_call_expr_arg (&iter);
7177 argtype = TREE_TYPE (arg);
7179 if (SCALAR_FLOAT_TYPE_P (parmtype))
7181 if (! SCALAR_FLOAT_TYPE_P (argtype))
7182 return END_BUILTINS;
7184 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7186 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7187 return END_BUILTINS;
7189 else if (POINTER_TYPE_P (parmtype))
7191 if (! POINTER_TYPE_P (argtype))
7192 return END_BUILTINS;
7194 else if (INTEGRAL_TYPE_P (parmtype))
7196 if (! INTEGRAL_TYPE_P (argtype))
7197 return END_BUILTINS;
7199 else
7200 return END_BUILTINS;
7203 /* Variable-length argument list. */
7204 return DECL_FUNCTION_CODE (fndecl);
7207 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7208 evaluate to a constant. */
7210 static tree
7211 fold_builtin_constant_p (tree arg)
7213 /* We return 1 for a numeric type that's known to be a constant
7214 value at compile-time or for an aggregate type that's a
7215 literal constant. */
7216 STRIP_NOPS (arg);
7218 /* If we know this is a constant, emit the constant of one. */
7219 if (CONSTANT_CLASS_P (arg)
7220 || (TREE_CODE (arg) == CONSTRUCTOR
7221 && TREE_CONSTANT (arg)))
7222 return integer_one_node;
7223 if (TREE_CODE (arg) == ADDR_EXPR)
7225 tree op = TREE_OPERAND (arg, 0);
7226 if (TREE_CODE (op) == STRING_CST
7227 || (TREE_CODE (op) == ARRAY_REF
7228 && integer_zerop (TREE_OPERAND (op, 1))
7229 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7230 return integer_one_node;
7233 /* If this expression has side effects, show we don't know it to be a
7234 constant. Likewise if it's a pointer or aggregate type since in
7235 those case we only want literals, since those are only optimized
7236 when generating RTL, not later.
7237 And finally, if we are compiling an initializer, not code, we
7238 need to return a definite result now; there's not going to be any
7239 more optimization done. */
7240 if (TREE_SIDE_EFFECTS (arg)
7241 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7242 || POINTER_TYPE_P (TREE_TYPE (arg))
7243 || cfun == 0
7244 || folding_initializer
7245 || force_folding_builtin_constant_p)
7246 return integer_zero_node;
7248 return NULL_TREE;
7251 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7252 return it as a truthvalue. */
7254 static tree
7255 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
7256 tree predictor)
7258 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7260 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
7261 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7262 ret_type = TREE_TYPE (TREE_TYPE (fn));
7263 pred_type = TREE_VALUE (arg_types);
7264 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7266 pred = fold_convert_loc (loc, pred_type, pred);
7267 expected = fold_convert_loc (loc, expected_type, expected);
7268 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
7269 predictor);
7271 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7272 build_int_cst (ret_type, 0));
7275 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7276 NULL_TREE if no simplification is possible. */
7278 tree
7279 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
7281 tree inner, fndecl, inner_arg0;
7282 enum tree_code code;
7284 /* Distribute the expected value over short-circuiting operators.
7285 See through the cast from truthvalue_type_node to long. */
7286 inner_arg0 = arg0;
7287 while (CONVERT_EXPR_P (inner_arg0)
7288 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7289 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7290 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7292 /* If this is a builtin_expect within a builtin_expect keep the
7293 inner one. See through a comparison against a constant. It
7294 might have been added to create a thruthvalue. */
7295 inner = inner_arg0;
7297 if (COMPARISON_CLASS_P (inner)
7298 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7299 inner = TREE_OPERAND (inner, 0);
7301 if (TREE_CODE (inner) == CALL_EXPR
7302 && (fndecl = get_callee_fndecl (inner))
7303 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7304 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7305 return arg0;
7307 inner = inner_arg0;
7308 code = TREE_CODE (inner);
7309 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7311 tree op0 = TREE_OPERAND (inner, 0);
7312 tree op1 = TREE_OPERAND (inner, 1);
7314 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
7315 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
7316 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7318 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7321 /* If the argument isn't invariant then there's nothing else we can do. */
7322 if (!TREE_CONSTANT (inner_arg0))
7323 return NULL_TREE;
7325 /* If we expect that a comparison against the argument will fold to
7326 a constant return the constant. In practice, this means a true
7327 constant or the address of a non-weak symbol. */
7328 inner = inner_arg0;
7329 STRIP_NOPS (inner);
7330 if (TREE_CODE (inner) == ADDR_EXPR)
7334 inner = TREE_OPERAND (inner, 0);
7336 while (TREE_CODE (inner) == COMPONENT_REF
7337 || TREE_CODE (inner) == ARRAY_REF);
7338 if ((TREE_CODE (inner) == VAR_DECL
7339 || TREE_CODE (inner) == FUNCTION_DECL)
7340 && DECL_WEAK (inner))
7341 return NULL_TREE;
7344 /* Otherwise, ARG0 already has the proper type for the return value. */
7345 return arg0;
7348 /* Fold a call to __builtin_classify_type with argument ARG. */
7350 static tree
7351 fold_builtin_classify_type (tree arg)
7353 if (arg == 0)
7354 return build_int_cst (integer_type_node, no_type_class);
7356 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7359 /* Fold a call to __builtin_strlen with argument ARG. */
7361 static tree
7362 fold_builtin_strlen (location_t loc, tree type, tree arg)
7364 if (!validate_arg (arg, POINTER_TYPE))
7365 return NULL_TREE;
7366 else
7368 tree len = c_strlen (arg, 0);
7370 if (len)
7371 return fold_convert_loc (loc, type, len);
7373 return NULL_TREE;
7377 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7379 static tree
7380 fold_builtin_inf (location_t loc, tree type, int warn)
7382 REAL_VALUE_TYPE real;
7384 /* __builtin_inff is intended to be usable to define INFINITY on all
7385 targets. If an infinity is not available, INFINITY expands "to a
7386 positive constant of type float that overflows at translation
7387 time", footnote "In this case, using INFINITY will violate the
7388 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7389 Thus we pedwarn to ensure this constraint violation is
7390 diagnosed. */
7391 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7392 pedwarn (loc, 0, "target format does not support infinity");
7394 real_inf (&real);
7395 return build_real (type, real);
7398 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7400 static tree
7401 fold_builtin_nan (tree arg, tree type, int quiet)
7403 REAL_VALUE_TYPE real;
7404 const char *str;
7406 if (!validate_arg (arg, POINTER_TYPE))
7407 return NULL_TREE;
7408 str = c_getstr (arg);
7409 if (!str)
7410 return NULL_TREE;
7412 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7413 return NULL_TREE;
7415 return build_real (type, real);
7418 /* Return true if the floating point expression T has an integer value.
7419 We also allow +Inf, -Inf and NaN to be considered integer values. */
7421 static bool
7422 integer_valued_real_p (tree t)
7424 switch (TREE_CODE (t))
7426 case FLOAT_EXPR:
7427 return true;
7429 case ABS_EXPR:
7430 case SAVE_EXPR:
7431 return integer_valued_real_p (TREE_OPERAND (t, 0));
7433 case COMPOUND_EXPR:
7434 case MODIFY_EXPR:
7435 case BIND_EXPR:
7436 return integer_valued_real_p (TREE_OPERAND (t, 1));
7438 case PLUS_EXPR:
7439 case MINUS_EXPR:
7440 case MULT_EXPR:
7441 case MIN_EXPR:
7442 case MAX_EXPR:
7443 return integer_valued_real_p (TREE_OPERAND (t, 0))
7444 && integer_valued_real_p (TREE_OPERAND (t, 1));
7446 case COND_EXPR:
7447 return integer_valued_real_p (TREE_OPERAND (t, 1))
7448 && integer_valued_real_p (TREE_OPERAND (t, 2));
7450 case REAL_CST:
7451 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7453 CASE_CONVERT:
7455 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7456 if (TREE_CODE (type) == INTEGER_TYPE)
7457 return true;
7458 if (TREE_CODE (type) == REAL_TYPE)
7459 return integer_valued_real_p (TREE_OPERAND (t, 0));
7460 break;
7463 case CALL_EXPR:
7464 switch (builtin_mathfn_code (t))
7466 CASE_FLT_FN (BUILT_IN_CEIL):
7467 CASE_FLT_FN (BUILT_IN_FLOOR):
7468 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7469 CASE_FLT_FN (BUILT_IN_RINT):
7470 CASE_FLT_FN (BUILT_IN_ROUND):
7471 CASE_FLT_FN (BUILT_IN_TRUNC):
7472 return true;
7474 CASE_FLT_FN (BUILT_IN_FMIN):
7475 CASE_FLT_FN (BUILT_IN_FMAX):
7476 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7477 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7479 default:
7480 break;
7482 break;
7484 default:
7485 break;
7487 return false;
7490 /* FNDECL is assumed to be a builtin where truncation can be propagated
7491 across (for instance floor((double)f) == (double)floorf (f).
7492 Do the transformation for a call with argument ARG. */
7494 static tree
7495 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7497 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7499 if (!validate_arg (arg, REAL_TYPE))
7500 return NULL_TREE;
7502 /* Integer rounding functions are idempotent. */
7503 if (fcode == builtin_mathfn_code (arg))
7504 return arg;
7506 /* If argument is already integer valued, and we don't need to worry
7507 about setting errno, there's no need to perform rounding. */
7508 if (! flag_errno_math && integer_valued_real_p (arg))
7509 return arg;
7511 if (optimize)
7513 tree arg0 = strip_float_extensions (arg);
7514 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7515 tree newtype = TREE_TYPE (arg0);
7516 tree decl;
7518 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7519 && (decl = mathfn_built_in (newtype, fcode)))
7520 return fold_convert_loc (loc, ftype,
7521 build_call_expr_loc (loc, decl, 1,
7522 fold_convert_loc (loc,
7523 newtype,
7524 arg0)));
7526 return NULL_TREE;
7529 /* FNDECL is assumed to be builtin which can narrow the FP type of
7530 the argument, for instance lround((double)f) -> lroundf (f).
7531 Do the transformation for a call with argument ARG. */
7533 static tree
7534 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7536 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7538 if (!validate_arg (arg, REAL_TYPE))
7539 return NULL_TREE;
7541 /* If argument is already integer valued, and we don't need to worry
7542 about setting errno, there's no need to perform rounding. */
7543 if (! flag_errno_math && integer_valued_real_p (arg))
7544 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7545 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7547 if (optimize)
7549 tree ftype = TREE_TYPE (arg);
7550 tree arg0 = strip_float_extensions (arg);
7551 tree newtype = TREE_TYPE (arg0);
7552 tree decl;
7554 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7555 && (decl = mathfn_built_in (newtype, fcode)))
7556 return build_call_expr_loc (loc, decl, 1,
7557 fold_convert_loc (loc, newtype, arg0));
7560 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7561 sizeof (int) == sizeof (long). */
7562 if (TYPE_PRECISION (integer_type_node)
7563 == TYPE_PRECISION (long_integer_type_node))
7565 tree newfn = NULL_TREE;
7566 switch (fcode)
7568 CASE_FLT_FN (BUILT_IN_ICEIL):
7569 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7570 break;
7572 CASE_FLT_FN (BUILT_IN_IFLOOR):
7573 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7574 break;
7576 CASE_FLT_FN (BUILT_IN_IROUND):
7577 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7578 break;
7580 CASE_FLT_FN (BUILT_IN_IRINT):
7581 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7582 break;
7584 default:
7585 break;
7588 if (newfn)
7590 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7591 return fold_convert_loc (loc,
7592 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7596 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7597 sizeof (long long) == sizeof (long). */
7598 if (TYPE_PRECISION (long_long_integer_type_node)
7599 == TYPE_PRECISION (long_integer_type_node))
7601 tree newfn = NULL_TREE;
7602 switch (fcode)
7604 CASE_FLT_FN (BUILT_IN_LLCEIL):
7605 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7606 break;
7608 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7609 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7610 break;
7612 CASE_FLT_FN (BUILT_IN_LLROUND):
7613 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7614 break;
7616 CASE_FLT_FN (BUILT_IN_LLRINT):
7617 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7618 break;
7620 default:
7621 break;
7624 if (newfn)
7626 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7627 return fold_convert_loc (loc,
7628 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7632 return NULL_TREE;
7635 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7636 return type. Return NULL_TREE if no simplification can be made. */
7638 static tree
7639 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7641 tree res;
7643 if (!validate_arg (arg, COMPLEX_TYPE)
7644 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7645 return NULL_TREE;
7647 /* Calculate the result when the argument is a constant. */
7648 if (TREE_CODE (arg) == COMPLEX_CST
7649 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7650 type, mpfr_hypot)))
7651 return res;
7653 if (TREE_CODE (arg) == COMPLEX_EXPR)
7655 tree real = TREE_OPERAND (arg, 0);
7656 tree imag = TREE_OPERAND (arg, 1);
7658 /* If either part is zero, cabs is fabs of the other. */
7659 if (real_zerop (real))
7660 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7661 if (real_zerop (imag))
7662 return fold_build1_loc (loc, ABS_EXPR, type, real);
7664 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7665 if (flag_unsafe_math_optimizations
7666 && operand_equal_p (real, imag, OEP_PURE_SAME))
7668 const REAL_VALUE_TYPE sqrt2_trunc
7669 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7670 STRIP_NOPS (real);
7671 return fold_build2_loc (loc, MULT_EXPR, type,
7672 fold_build1_loc (loc, ABS_EXPR, type, real),
7673 build_real (type, sqrt2_trunc));
7677 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7678 if (TREE_CODE (arg) == NEGATE_EXPR
7679 || TREE_CODE (arg) == CONJ_EXPR)
7680 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7682 /* Don't do this when optimizing for size. */
7683 if (flag_unsafe_math_optimizations
7684 && optimize && optimize_function_for_speed_p (cfun))
7686 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7688 if (sqrtfn != NULL_TREE)
7690 tree rpart, ipart, result;
7692 arg = builtin_save_expr (arg);
7694 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7695 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7697 rpart = builtin_save_expr (rpart);
7698 ipart = builtin_save_expr (ipart);
7700 result = fold_build2_loc (loc, PLUS_EXPR, type,
7701 fold_build2_loc (loc, MULT_EXPR, type,
7702 rpart, rpart),
7703 fold_build2_loc (loc, MULT_EXPR, type,
7704 ipart, ipart));
7706 return build_call_expr_loc (loc, sqrtfn, 1, result);
7710 return NULL_TREE;
7713 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7714 complex tree type of the result. If NEG is true, the imaginary
7715 zero is negative. */
7717 static tree
7718 build_complex_cproj (tree type, bool neg)
7720 REAL_VALUE_TYPE rinf, rzero = dconst0;
7722 real_inf (&rinf);
7723 rzero.sign = neg;
7724 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7725 build_real (TREE_TYPE (type), rzero));
7728 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7729 return type. Return NULL_TREE if no simplification can be made. */
7731 static tree
7732 fold_builtin_cproj (location_t loc, tree arg, tree type)
7734 if (!validate_arg (arg, COMPLEX_TYPE)
7735 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7736 return NULL_TREE;
7738 /* If there are no infinities, return arg. */
7739 if (! HONOR_INFINITIES (type))
7740 return non_lvalue_loc (loc, arg);
7742 /* Calculate the result when the argument is a constant. */
7743 if (TREE_CODE (arg) == COMPLEX_CST)
7745 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7746 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7748 if (real_isinf (real) || real_isinf (imag))
7749 return build_complex_cproj (type, imag->sign);
7750 else
7751 return arg;
7753 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7755 tree real = TREE_OPERAND (arg, 0);
7756 tree imag = TREE_OPERAND (arg, 1);
7758 STRIP_NOPS (real);
7759 STRIP_NOPS (imag);
7761 /* If the real part is inf and the imag part is known to be
7762 nonnegative, return (inf + 0i). Remember side-effects are
7763 possible in the imag part. */
7764 if (TREE_CODE (real) == REAL_CST
7765 && real_isinf (TREE_REAL_CST_PTR (real))
7766 && tree_expr_nonnegative_p (imag))
7767 return omit_one_operand_loc (loc, type,
7768 build_complex_cproj (type, false),
7769 arg);
7771 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7772 Remember side-effects are possible in the real part. */
7773 if (TREE_CODE (imag) == REAL_CST
7774 && real_isinf (TREE_REAL_CST_PTR (imag)))
7775 return
7776 omit_one_operand_loc (loc, type,
7777 build_complex_cproj (type, TREE_REAL_CST_PTR
7778 (imag)->sign), arg);
7781 return NULL_TREE;
7784 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7785 Return NULL_TREE if no simplification can be made. */
7787 static tree
7788 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7791 enum built_in_function fcode;
7792 tree res;
7794 if (!validate_arg (arg, REAL_TYPE))
7795 return NULL_TREE;
7797 /* Calculate the result when the argument is a constant. */
7798 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7799 return res;
7801 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7802 fcode = builtin_mathfn_code (arg);
7803 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7805 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7806 arg = fold_build2_loc (loc, MULT_EXPR, type,
7807 CALL_EXPR_ARG (arg, 0),
7808 build_real (type, dconsthalf));
7809 return build_call_expr_loc (loc, expfn, 1, arg);
7812 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7813 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7815 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7817 if (powfn)
7819 tree arg0 = CALL_EXPR_ARG (arg, 0);
7820 tree tree_root;
7821 /* The inner root was either sqrt or cbrt. */
7822 /* This was a conditional expression but it triggered a bug
7823 in Sun C 5.5. */
7824 REAL_VALUE_TYPE dconstroot;
7825 if (BUILTIN_SQRT_P (fcode))
7826 dconstroot = dconsthalf;
7827 else
7828 dconstroot = dconst_third ();
7830 /* Adjust for the outer root. */
7831 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7832 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7833 tree_root = build_real (type, dconstroot);
7834 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7838 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7839 if (flag_unsafe_math_optimizations
7840 && (fcode == BUILT_IN_POW
7841 || fcode == BUILT_IN_POWF
7842 || fcode == BUILT_IN_POWL))
7844 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7845 tree arg0 = CALL_EXPR_ARG (arg, 0);
7846 tree arg1 = CALL_EXPR_ARG (arg, 1);
7847 tree narg1;
7848 if (!tree_expr_nonnegative_p (arg0))
7849 arg0 = build1 (ABS_EXPR, type, arg0);
7850 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7851 build_real (type, dconsthalf));
7852 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7855 return NULL_TREE;
7858 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7859 Return NULL_TREE if no simplification can be made. */
7861 static tree
7862 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7864 const enum built_in_function fcode = builtin_mathfn_code (arg);
7865 tree res;
7867 if (!validate_arg (arg, REAL_TYPE))
7868 return NULL_TREE;
7870 /* Calculate the result when the argument is a constant. */
7871 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7872 return res;
7874 if (flag_unsafe_math_optimizations)
7876 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7877 if (BUILTIN_EXPONENT_P (fcode))
7879 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7880 const REAL_VALUE_TYPE third_trunc =
7881 real_value_truncate (TYPE_MODE (type), dconst_third ());
7882 arg = fold_build2_loc (loc, MULT_EXPR, type,
7883 CALL_EXPR_ARG (arg, 0),
7884 build_real (type, third_trunc));
7885 return build_call_expr_loc (loc, expfn, 1, arg);
7888 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7889 if (BUILTIN_SQRT_P (fcode))
7891 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7893 if (powfn)
7895 tree arg0 = CALL_EXPR_ARG (arg, 0);
7896 tree tree_root;
7897 REAL_VALUE_TYPE dconstroot = dconst_third ();
7899 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7900 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7901 tree_root = build_real (type, dconstroot);
7902 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7906 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7907 if (BUILTIN_CBRT_P (fcode))
7909 tree arg0 = CALL_EXPR_ARG (arg, 0);
7910 if (tree_expr_nonnegative_p (arg0))
7912 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7914 if (powfn)
7916 tree tree_root;
7917 REAL_VALUE_TYPE dconstroot;
7919 real_arithmetic (&dconstroot, MULT_EXPR,
7920 dconst_third_ptr (), dconst_third_ptr ());
7921 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7922 tree_root = build_real (type, dconstroot);
7923 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7928 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7929 if (fcode == BUILT_IN_POW
7930 || fcode == BUILT_IN_POWF
7931 || fcode == BUILT_IN_POWL)
7933 tree arg00 = CALL_EXPR_ARG (arg, 0);
7934 tree arg01 = CALL_EXPR_ARG (arg, 1);
7935 if (tree_expr_nonnegative_p (arg00))
7937 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7938 const REAL_VALUE_TYPE dconstroot
7939 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7940 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7941 build_real (type, dconstroot));
7942 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7946 return NULL_TREE;
7949 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7950 TYPE is the type of the return value. Return NULL_TREE if no
7951 simplification can be made. */
7953 static tree
7954 fold_builtin_cos (location_t loc,
7955 tree arg, tree type, tree fndecl)
7957 tree res, narg;
7959 if (!validate_arg (arg, REAL_TYPE))
7960 return NULL_TREE;
7962 /* Calculate the result when the argument is a constant. */
7963 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7964 return res;
7966 /* Optimize cos(-x) into cos (x). */
7967 if ((narg = fold_strip_sign_ops (arg)))
7968 return build_call_expr_loc (loc, fndecl, 1, narg);
7970 return NULL_TREE;
7973 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7974 Return NULL_TREE if no simplification can be made. */
7976 static tree
7977 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7979 if (validate_arg (arg, REAL_TYPE))
7981 tree res, narg;
7983 /* Calculate the result when the argument is a constant. */
7984 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7985 return res;
7987 /* Optimize cosh(-x) into cosh (x). */
7988 if ((narg = fold_strip_sign_ops (arg)))
7989 return build_call_expr_loc (loc, fndecl, 1, narg);
7992 return NULL_TREE;
7995 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7996 argument ARG. TYPE is the type of the return value. Return
7997 NULL_TREE if no simplification can be made. */
7999 static tree
8000 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
8001 bool hyper)
8003 if (validate_arg (arg, COMPLEX_TYPE)
8004 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8006 tree tmp;
8008 /* Calculate the result when the argument is a constant. */
8009 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
8010 return tmp;
8012 /* Optimize fn(-x) into fn(x). */
8013 if ((tmp = fold_strip_sign_ops (arg)))
8014 return build_call_expr_loc (loc, fndecl, 1, tmp);
8017 return NULL_TREE;
8020 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
8021 Return NULL_TREE if no simplification can be made. */
8023 static tree
8024 fold_builtin_tan (tree arg, tree type)
8026 enum built_in_function fcode;
8027 tree res;
8029 if (!validate_arg (arg, REAL_TYPE))
8030 return NULL_TREE;
8032 /* Calculate the result when the argument is a constant. */
8033 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
8034 return res;
8036 /* Optimize tan(atan(x)) = x. */
8037 fcode = builtin_mathfn_code (arg);
8038 if (flag_unsafe_math_optimizations
8039 && (fcode == BUILT_IN_ATAN
8040 || fcode == BUILT_IN_ATANF
8041 || fcode == BUILT_IN_ATANL))
8042 return CALL_EXPR_ARG (arg, 0);
8044 return NULL_TREE;
8047 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8048 NULL_TREE if no simplification can be made. */
8050 static tree
8051 fold_builtin_sincos (location_t loc,
8052 tree arg0, tree arg1, tree arg2)
8054 tree type;
8055 tree res, fn, call;
8057 if (!validate_arg (arg0, REAL_TYPE)
8058 || !validate_arg (arg1, POINTER_TYPE)
8059 || !validate_arg (arg2, POINTER_TYPE))
8060 return NULL_TREE;
8062 type = TREE_TYPE (arg0);
8064 /* Calculate the result when the argument is a constant. */
8065 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
8066 return res;
8068 /* Canonicalize sincos to cexpi. */
8069 if (!targetm.libc_has_function (function_c99_math_complex))
8070 return NULL_TREE;
8071 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
8072 if (!fn)
8073 return NULL_TREE;
8075 call = build_call_expr_loc (loc, fn, 1, arg0);
8076 call = builtin_save_expr (call);
8078 return build2 (COMPOUND_EXPR, void_type_node,
8079 build2 (MODIFY_EXPR, void_type_node,
8080 build_fold_indirect_ref_loc (loc, arg1),
8081 build1 (IMAGPART_EXPR, type, call)),
8082 build2 (MODIFY_EXPR, void_type_node,
8083 build_fold_indirect_ref_loc (loc, arg2),
8084 build1 (REALPART_EXPR, type, call)));
8087 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
8088 NULL_TREE if no simplification can be made. */
8090 static tree
8091 fold_builtin_cexp (location_t loc, tree arg0, tree type)
8093 tree rtype;
8094 tree realp, imagp, ifn;
8095 tree res;
8097 if (!validate_arg (arg0, COMPLEX_TYPE)
8098 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
8099 return NULL_TREE;
8101 /* Calculate the result when the argument is a constant. */
8102 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
8103 return res;
8105 rtype = TREE_TYPE (TREE_TYPE (arg0));
8107 /* In case we can figure out the real part of arg0 and it is constant zero
8108 fold to cexpi. */
8109 if (!targetm.libc_has_function (function_c99_math_complex))
8110 return NULL_TREE;
8111 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
8112 if (!ifn)
8113 return NULL_TREE;
8115 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
8116 && real_zerop (realp))
8118 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
8119 return build_call_expr_loc (loc, ifn, 1, narg);
8122 /* In case we can easily decompose real and imaginary parts split cexp
8123 to exp (r) * cexpi (i). */
8124 if (flag_unsafe_math_optimizations
8125 && realp)
8127 tree rfn, rcall, icall;
8129 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
8130 if (!rfn)
8131 return NULL_TREE;
8133 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
8134 if (!imagp)
8135 return NULL_TREE;
8137 icall = build_call_expr_loc (loc, ifn, 1, imagp);
8138 icall = builtin_save_expr (icall);
8139 rcall = build_call_expr_loc (loc, rfn, 1, realp);
8140 rcall = builtin_save_expr (rcall);
8141 return fold_build2_loc (loc, COMPLEX_EXPR, type,
8142 fold_build2_loc (loc, MULT_EXPR, rtype,
8143 rcall,
8144 fold_build1_loc (loc, REALPART_EXPR,
8145 rtype, icall)),
8146 fold_build2_loc (loc, MULT_EXPR, rtype,
8147 rcall,
8148 fold_build1_loc (loc, IMAGPART_EXPR,
8149 rtype, icall)));
8152 return NULL_TREE;
8155 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
8156 Return NULL_TREE if no simplification can be made. */
8158 static tree
8159 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
8161 if (!validate_arg (arg, REAL_TYPE))
8162 return NULL_TREE;
8164 /* Optimize trunc of constant value. */
8165 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8167 REAL_VALUE_TYPE r, x;
8168 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8170 x = TREE_REAL_CST (arg);
8171 real_trunc (&r, TYPE_MODE (type), &x);
8172 return build_real (type, r);
8175 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8178 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
8179 Return NULL_TREE if no simplification can be made. */
8181 static tree
8182 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
8184 if (!validate_arg (arg, REAL_TYPE))
8185 return NULL_TREE;
8187 /* Optimize floor of constant value. */
8188 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8190 REAL_VALUE_TYPE x;
8192 x = TREE_REAL_CST (arg);
8193 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8195 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8196 REAL_VALUE_TYPE r;
8198 real_floor (&r, TYPE_MODE (type), &x);
8199 return build_real (type, r);
8203 /* Fold floor (x) where x is nonnegative to trunc (x). */
8204 if (tree_expr_nonnegative_p (arg))
8206 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
8207 if (truncfn)
8208 return build_call_expr_loc (loc, truncfn, 1, arg);
8211 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8214 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
8215 Return NULL_TREE if no simplification can be made. */
8217 static tree
8218 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
8220 if (!validate_arg (arg, REAL_TYPE))
8221 return NULL_TREE;
8223 /* Optimize ceil of constant value. */
8224 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8226 REAL_VALUE_TYPE x;
8228 x = TREE_REAL_CST (arg);
8229 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8231 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8232 REAL_VALUE_TYPE r;
8234 real_ceil (&r, TYPE_MODE (type), &x);
8235 return build_real (type, r);
8239 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8242 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8243 Return NULL_TREE if no simplification can be made. */
8245 static tree
8246 fold_builtin_round (location_t loc, tree fndecl, tree arg)
8248 if (!validate_arg (arg, REAL_TYPE))
8249 return NULL_TREE;
8251 /* Optimize round of constant value. */
8252 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8254 REAL_VALUE_TYPE x;
8256 x = TREE_REAL_CST (arg);
8257 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8259 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8260 REAL_VALUE_TYPE r;
8262 real_round (&r, TYPE_MODE (type), &x);
8263 return build_real (type, r);
8267 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8270 /* Fold function call to builtin lround, lroundf or lroundl (or the
8271 corresponding long long versions) and other rounding functions. ARG
8272 is the argument to the call. Return NULL_TREE if no simplification
8273 can be made. */
8275 static tree
8276 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8278 if (!validate_arg (arg, REAL_TYPE))
8279 return NULL_TREE;
8281 /* Optimize lround of constant value. */
8282 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8284 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8286 if (real_isfinite (&x))
8288 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8289 tree ftype = TREE_TYPE (arg);
8290 REAL_VALUE_TYPE r;
8291 bool fail = false;
8293 switch (DECL_FUNCTION_CODE (fndecl))
8295 CASE_FLT_FN (BUILT_IN_IFLOOR):
8296 CASE_FLT_FN (BUILT_IN_LFLOOR):
8297 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8298 real_floor (&r, TYPE_MODE (ftype), &x);
8299 break;
8301 CASE_FLT_FN (BUILT_IN_ICEIL):
8302 CASE_FLT_FN (BUILT_IN_LCEIL):
8303 CASE_FLT_FN (BUILT_IN_LLCEIL):
8304 real_ceil (&r, TYPE_MODE (ftype), &x);
8305 break;
8307 CASE_FLT_FN (BUILT_IN_IROUND):
8308 CASE_FLT_FN (BUILT_IN_LROUND):
8309 CASE_FLT_FN (BUILT_IN_LLROUND):
8310 real_round (&r, TYPE_MODE (ftype), &x);
8311 break;
8313 default:
8314 gcc_unreachable ();
8317 wide_int val = real_to_integer (&r, &fail, TYPE_PRECISION (itype));
8318 if (!fail)
8319 return wide_int_to_tree (itype, val);
8323 switch (DECL_FUNCTION_CODE (fndecl))
8325 CASE_FLT_FN (BUILT_IN_LFLOOR):
8326 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8327 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8328 if (tree_expr_nonnegative_p (arg))
8329 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8330 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8331 break;
8332 default:;
8335 return fold_fixed_mathfn (loc, fndecl, arg);
8338 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8339 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8340 the argument to the call. Return NULL_TREE if no simplification can
8341 be made. */
8343 static tree
8344 fold_builtin_bitop (tree fndecl, tree arg)
8346 if (!validate_arg (arg, INTEGER_TYPE))
8347 return NULL_TREE;
8349 /* Optimize for constant argument. */
8350 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8352 tree type = TREE_TYPE (arg);
8353 int result;
8355 switch (DECL_FUNCTION_CODE (fndecl))
8357 CASE_INT_FN (BUILT_IN_FFS):
8358 result = wi::ffs (arg);
8359 break;
8361 CASE_INT_FN (BUILT_IN_CLZ):
8362 if (wi::ne_p (arg, 0))
8363 result = wi::clz (arg);
8364 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8365 result = TYPE_PRECISION (type);
8366 break;
8368 CASE_INT_FN (BUILT_IN_CTZ):
8369 if (wi::ne_p (arg, 0))
8370 result = wi::ctz (arg);
8371 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8372 result = TYPE_PRECISION (type);
8373 break;
8375 CASE_INT_FN (BUILT_IN_CLRSB):
8376 result = wi::clrsb (arg);
8377 break;
8379 CASE_INT_FN (BUILT_IN_POPCOUNT):
8380 result = wi::popcount (arg);
8381 break;
8383 CASE_INT_FN (BUILT_IN_PARITY):
8384 result = wi::parity (arg);
8385 break;
8387 default:
8388 gcc_unreachable ();
8391 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8394 return NULL_TREE;
8397 /* Fold function call to builtin_bswap and the short, long and long long
8398 variants. Return NULL_TREE if no simplification can be made. */
8399 static tree
8400 fold_builtin_bswap (tree fndecl, tree arg)
8402 if (! validate_arg (arg, INTEGER_TYPE))
8403 return NULL_TREE;
8405 /* Optimize constant value. */
8406 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8408 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8410 switch (DECL_FUNCTION_CODE (fndecl))
8412 case BUILT_IN_BSWAP16:
8413 case BUILT_IN_BSWAP32:
8414 case BUILT_IN_BSWAP64:
8416 signop sgn = TYPE_SIGN (type);
8417 tree result =
8418 wide_int_to_tree (type,
8419 wide_int::from (arg, TYPE_PRECISION (type),
8420 sgn).bswap ());
8421 return result;
8423 default:
8424 gcc_unreachable ();
8428 return NULL_TREE;
8431 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8432 NULL_TREE if no simplification can be made. */
8434 static tree
8435 fold_builtin_hypot (location_t loc, tree fndecl,
8436 tree arg0, tree arg1, tree type)
8438 tree res, narg0, narg1;
8440 if (!validate_arg (arg0, REAL_TYPE)
8441 || !validate_arg (arg1, REAL_TYPE))
8442 return NULL_TREE;
8444 /* Calculate the result when the argument is a constant. */
8445 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8446 return res;
8448 /* If either argument to hypot has a negate or abs, strip that off.
8449 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8450 narg0 = fold_strip_sign_ops (arg0);
8451 narg1 = fold_strip_sign_ops (arg1);
8452 if (narg0 || narg1)
8454 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8455 narg1 ? narg1 : arg1);
8458 /* If either argument is zero, hypot is fabs of the other. */
8459 if (real_zerop (arg0))
8460 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8461 else if (real_zerop (arg1))
8462 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8464 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8465 if (flag_unsafe_math_optimizations
8466 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8468 const REAL_VALUE_TYPE sqrt2_trunc
8469 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8470 return fold_build2_loc (loc, MULT_EXPR, type,
8471 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8472 build_real (type, sqrt2_trunc));
8475 return NULL_TREE;
8479 /* Fold a builtin function call to pow, powf, or powl. Return
8480 NULL_TREE if no simplification can be made. */
8481 static tree
8482 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8484 tree res;
8486 if (!validate_arg (arg0, REAL_TYPE)
8487 || !validate_arg (arg1, REAL_TYPE))
8488 return NULL_TREE;
8490 /* Calculate the result when the argument is a constant. */
8491 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8492 return res;
8494 /* Optimize pow(1.0,y) = 1.0. */
8495 if (real_onep (arg0))
8496 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8498 if (TREE_CODE (arg1) == REAL_CST
8499 && !TREE_OVERFLOW (arg1))
8501 REAL_VALUE_TYPE cint;
8502 REAL_VALUE_TYPE c;
8503 HOST_WIDE_INT n;
8505 c = TREE_REAL_CST (arg1);
8507 /* Optimize pow(x,0.0) = 1.0. */
8508 if (REAL_VALUES_EQUAL (c, dconst0))
8509 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8510 arg0);
8512 /* Optimize pow(x,1.0) = x. */
8513 if (REAL_VALUES_EQUAL (c, dconst1))
8514 return arg0;
8516 /* Optimize pow(x,-1.0) = 1.0/x. */
8517 if (REAL_VALUES_EQUAL (c, dconstm1))
8518 return fold_build2_loc (loc, RDIV_EXPR, type,
8519 build_real (type, dconst1), arg0);
8521 /* Optimize pow(x,0.5) = sqrt(x). */
8522 if (flag_unsafe_math_optimizations
8523 && REAL_VALUES_EQUAL (c, dconsthalf))
8525 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8527 if (sqrtfn != NULL_TREE)
8528 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8531 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8532 if (flag_unsafe_math_optimizations)
8534 const REAL_VALUE_TYPE dconstroot
8535 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8537 if (REAL_VALUES_EQUAL (c, dconstroot))
8539 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8540 if (cbrtfn != NULL_TREE)
8541 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8545 /* Check for an integer exponent. */
8546 n = real_to_integer (&c);
8547 real_from_integer (&cint, VOIDmode, n, SIGNED);
8548 if (real_identical (&c, &cint))
8550 /* Attempt to evaluate pow at compile-time, unless this should
8551 raise an exception. */
8552 if (TREE_CODE (arg0) == REAL_CST
8553 && !TREE_OVERFLOW (arg0)
8554 && (n > 0
8555 || (!flag_trapping_math && !flag_errno_math)
8556 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8558 REAL_VALUE_TYPE x;
8559 bool inexact;
8561 x = TREE_REAL_CST (arg0);
8562 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8563 if (flag_unsafe_math_optimizations || !inexact)
8564 return build_real (type, x);
8567 /* Strip sign ops from even integer powers. */
8568 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8570 tree narg0 = fold_strip_sign_ops (arg0);
8571 if (narg0)
8572 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8577 if (flag_unsafe_math_optimizations)
8579 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8581 /* Optimize pow(expN(x),y) = expN(x*y). */
8582 if (BUILTIN_EXPONENT_P (fcode))
8584 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8585 tree arg = CALL_EXPR_ARG (arg0, 0);
8586 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8587 return build_call_expr_loc (loc, expfn, 1, arg);
8590 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8591 if (BUILTIN_SQRT_P (fcode))
8593 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8594 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8595 build_real (type, dconsthalf));
8596 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8599 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8600 if (BUILTIN_CBRT_P (fcode))
8602 tree arg = CALL_EXPR_ARG (arg0, 0);
8603 if (tree_expr_nonnegative_p (arg))
8605 const REAL_VALUE_TYPE dconstroot
8606 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8607 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8608 build_real (type, dconstroot));
8609 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8613 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8614 if (fcode == BUILT_IN_POW
8615 || fcode == BUILT_IN_POWF
8616 || fcode == BUILT_IN_POWL)
8618 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8619 if (tree_expr_nonnegative_p (arg00))
8621 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8622 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8623 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8628 return NULL_TREE;
8631 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8632 Return NULL_TREE if no simplification can be made. */
8633 static tree
8634 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8635 tree arg0, tree arg1, tree type)
8637 if (!validate_arg (arg0, REAL_TYPE)
8638 || !validate_arg (arg1, INTEGER_TYPE))
8639 return NULL_TREE;
8641 /* Optimize pow(1.0,y) = 1.0. */
8642 if (real_onep (arg0))
8643 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8645 if (tree_fits_shwi_p (arg1))
8647 HOST_WIDE_INT c = tree_to_shwi (arg1);
8649 /* Evaluate powi at compile-time. */
8650 if (TREE_CODE (arg0) == REAL_CST
8651 && !TREE_OVERFLOW (arg0))
8653 REAL_VALUE_TYPE x;
8654 x = TREE_REAL_CST (arg0);
8655 real_powi (&x, TYPE_MODE (type), &x, c);
8656 return build_real (type, x);
8659 /* Optimize pow(x,0) = 1.0. */
8660 if (c == 0)
8661 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8662 arg0);
8664 /* Optimize pow(x,1) = x. */
8665 if (c == 1)
8666 return arg0;
8668 /* Optimize pow(x,-1) = 1.0/x. */
8669 if (c == -1)
8670 return fold_build2_loc (loc, RDIV_EXPR, type,
8671 build_real (type, dconst1), arg0);
8674 return NULL_TREE;
8677 /* A subroutine of fold_builtin to fold the various exponent
8678 functions. Return NULL_TREE if no simplification can be made.
8679 FUNC is the corresponding MPFR exponent function. */
8681 static tree
8682 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8683 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8685 if (validate_arg (arg, REAL_TYPE))
8687 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8688 tree res;
8690 /* Calculate the result when the argument is a constant. */
8691 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8692 return res;
8694 /* Optimize expN(logN(x)) = x. */
8695 if (flag_unsafe_math_optimizations)
8697 const enum built_in_function fcode = builtin_mathfn_code (arg);
8699 if ((func == mpfr_exp
8700 && (fcode == BUILT_IN_LOG
8701 || fcode == BUILT_IN_LOGF
8702 || fcode == BUILT_IN_LOGL))
8703 || (func == mpfr_exp2
8704 && (fcode == BUILT_IN_LOG2
8705 || fcode == BUILT_IN_LOG2F
8706 || fcode == BUILT_IN_LOG2L))
8707 || (func == mpfr_exp10
8708 && (fcode == BUILT_IN_LOG10
8709 || fcode == BUILT_IN_LOG10F
8710 || fcode == BUILT_IN_LOG10L)))
8711 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8715 return NULL_TREE;
8718 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8719 arguments to the call, and TYPE is its return type.
8720 Return NULL_TREE if no simplification can be made. */
8722 static tree
8723 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8725 if (!validate_arg (arg1, POINTER_TYPE)
8726 || !validate_arg (arg2, INTEGER_TYPE)
8727 || !validate_arg (len, INTEGER_TYPE))
8728 return NULL_TREE;
8729 else
8731 const char *p1;
8733 if (TREE_CODE (arg2) != INTEGER_CST
8734 || !tree_fits_uhwi_p (len))
8735 return NULL_TREE;
8737 p1 = c_getstr (arg1);
8738 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8740 char c;
8741 const char *r;
8742 tree tem;
8744 if (target_char_cast (arg2, &c))
8745 return NULL_TREE;
8747 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
8749 if (r == NULL)
8750 return build_int_cst (TREE_TYPE (arg1), 0);
8752 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
8753 return fold_convert_loc (loc, type, tem);
8755 return NULL_TREE;
8759 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8760 Return NULL_TREE if no simplification can be made. */
8762 static tree
8763 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8765 const char *p1, *p2;
8767 if (!validate_arg (arg1, POINTER_TYPE)
8768 || !validate_arg (arg2, POINTER_TYPE)
8769 || !validate_arg (len, INTEGER_TYPE))
8770 return NULL_TREE;
8772 /* If the LEN parameter is zero, return zero. */
8773 if (integer_zerop (len))
8774 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8775 arg1, arg2);
8777 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8778 if (operand_equal_p (arg1, arg2, 0))
8779 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8781 p1 = c_getstr (arg1);
8782 p2 = c_getstr (arg2);
8784 /* If all arguments are constant, and the value of len is not greater
8785 than the lengths of arg1 and arg2, evaluate at compile-time. */
8786 if (tree_fits_uhwi_p (len) && p1 && p2
8787 && compare_tree_int (len, strlen (p1) + 1) <= 0
8788 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8790 const int r = memcmp (p1, p2, tree_to_uhwi (len));
8792 if (r > 0)
8793 return integer_one_node;
8794 else if (r < 0)
8795 return integer_minus_one_node;
8796 else
8797 return integer_zero_node;
8800 /* If len parameter is one, return an expression corresponding to
8801 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8802 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8804 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8805 tree cst_uchar_ptr_node
8806 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8808 tree ind1
8809 = fold_convert_loc (loc, integer_type_node,
8810 build1 (INDIRECT_REF, cst_uchar_node,
8811 fold_convert_loc (loc,
8812 cst_uchar_ptr_node,
8813 arg1)));
8814 tree ind2
8815 = fold_convert_loc (loc, integer_type_node,
8816 build1 (INDIRECT_REF, cst_uchar_node,
8817 fold_convert_loc (loc,
8818 cst_uchar_ptr_node,
8819 arg2)));
8820 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8823 return NULL_TREE;
8826 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8827 Return NULL_TREE if no simplification can be made. */
8829 static tree
8830 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8832 const char *p1, *p2;
8834 if (!validate_arg (arg1, POINTER_TYPE)
8835 || !validate_arg (arg2, POINTER_TYPE))
8836 return NULL_TREE;
8838 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8839 if (operand_equal_p (arg1, arg2, 0))
8840 return integer_zero_node;
8842 p1 = c_getstr (arg1);
8843 p2 = c_getstr (arg2);
8845 if (p1 && p2)
8847 const int i = strcmp (p1, p2);
8848 if (i < 0)
8849 return integer_minus_one_node;
8850 else if (i > 0)
8851 return integer_one_node;
8852 else
8853 return integer_zero_node;
8856 /* If the second arg is "", return *(const unsigned char*)arg1. */
8857 if (p2 && *p2 == '\0')
8859 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8860 tree cst_uchar_ptr_node
8861 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8863 return fold_convert_loc (loc, integer_type_node,
8864 build1 (INDIRECT_REF, cst_uchar_node,
8865 fold_convert_loc (loc,
8866 cst_uchar_ptr_node,
8867 arg1)));
8870 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8871 if (p1 && *p1 == '\0')
8873 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8874 tree cst_uchar_ptr_node
8875 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8877 tree temp
8878 = fold_convert_loc (loc, integer_type_node,
8879 build1 (INDIRECT_REF, cst_uchar_node,
8880 fold_convert_loc (loc,
8881 cst_uchar_ptr_node,
8882 arg2)));
8883 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8886 return NULL_TREE;
8889 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8890 Return NULL_TREE if no simplification can be made. */
8892 static tree
8893 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8895 const char *p1, *p2;
8897 if (!validate_arg (arg1, POINTER_TYPE)
8898 || !validate_arg (arg2, POINTER_TYPE)
8899 || !validate_arg (len, INTEGER_TYPE))
8900 return NULL_TREE;
8902 /* If the LEN parameter is zero, return zero. */
8903 if (integer_zerop (len))
8904 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8905 arg1, arg2);
8907 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8908 if (operand_equal_p (arg1, arg2, 0))
8909 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8911 p1 = c_getstr (arg1);
8912 p2 = c_getstr (arg2);
8914 if (tree_fits_uhwi_p (len) && p1 && p2)
8916 const int i = strncmp (p1, p2, tree_to_uhwi (len));
8917 if (i > 0)
8918 return integer_one_node;
8919 else if (i < 0)
8920 return integer_minus_one_node;
8921 else
8922 return integer_zero_node;
8925 /* If the second arg is "", and the length is greater than zero,
8926 return *(const unsigned char*)arg1. */
8927 if (p2 && *p2 == '\0'
8928 && TREE_CODE (len) == INTEGER_CST
8929 && tree_int_cst_sgn (len) == 1)
8931 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8932 tree cst_uchar_ptr_node
8933 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8935 return fold_convert_loc (loc, integer_type_node,
8936 build1 (INDIRECT_REF, cst_uchar_node,
8937 fold_convert_loc (loc,
8938 cst_uchar_ptr_node,
8939 arg1)));
8942 /* If the first arg is "", and the length is greater than zero,
8943 return -*(const unsigned char*)arg2. */
8944 if (p1 && *p1 == '\0'
8945 && TREE_CODE (len) == INTEGER_CST
8946 && tree_int_cst_sgn (len) == 1)
8948 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8949 tree cst_uchar_ptr_node
8950 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8952 tree temp = fold_convert_loc (loc, integer_type_node,
8953 build1 (INDIRECT_REF, cst_uchar_node,
8954 fold_convert_loc (loc,
8955 cst_uchar_ptr_node,
8956 arg2)));
8957 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8960 /* If len parameter is one, return an expression corresponding to
8961 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8962 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8964 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8965 tree cst_uchar_ptr_node
8966 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8968 tree ind1 = fold_convert_loc (loc, integer_type_node,
8969 build1 (INDIRECT_REF, cst_uchar_node,
8970 fold_convert_loc (loc,
8971 cst_uchar_ptr_node,
8972 arg1)));
8973 tree ind2 = fold_convert_loc (loc, integer_type_node,
8974 build1 (INDIRECT_REF, cst_uchar_node,
8975 fold_convert_loc (loc,
8976 cst_uchar_ptr_node,
8977 arg2)));
8978 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8981 return NULL_TREE;
8984 /* Fold function call to builtin signbit, signbitf or signbitl with argument
8985 ARG. Return NULL_TREE if no simplification can be made. */
8987 static tree
8988 fold_builtin_signbit (location_t loc, tree arg, tree type)
8990 if (!validate_arg (arg, REAL_TYPE))
8991 return NULL_TREE;
8993 /* If ARG is a compile-time constant, determine the result. */
8994 if (TREE_CODE (arg) == REAL_CST
8995 && !TREE_OVERFLOW (arg))
8997 REAL_VALUE_TYPE c;
8999 c = TREE_REAL_CST (arg);
9000 return (REAL_VALUE_NEGATIVE (c)
9001 ? build_one_cst (type)
9002 : build_zero_cst (type));
9005 /* If ARG is non-negative, the result is always zero. */
9006 if (tree_expr_nonnegative_p (arg))
9007 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9009 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9010 if (!HONOR_SIGNED_ZEROS (arg))
9011 return fold_convert (type,
9012 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9013 build_real (TREE_TYPE (arg), dconst0)));
9015 return NULL_TREE;
9018 /* Fold function call to builtin copysign, copysignf or copysignl with
9019 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9020 be made. */
9022 static tree
9023 fold_builtin_copysign (location_t loc, tree fndecl,
9024 tree arg1, tree arg2, tree type)
9026 tree tem;
9028 if (!validate_arg (arg1, REAL_TYPE)
9029 || !validate_arg (arg2, REAL_TYPE))
9030 return NULL_TREE;
9032 /* copysign(X,X) is X. */
9033 if (operand_equal_p (arg1, arg2, 0))
9034 return fold_convert_loc (loc, type, arg1);
9036 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9037 if (TREE_CODE (arg1) == REAL_CST
9038 && TREE_CODE (arg2) == REAL_CST
9039 && !TREE_OVERFLOW (arg1)
9040 && !TREE_OVERFLOW (arg2))
9042 REAL_VALUE_TYPE c1, c2;
9044 c1 = TREE_REAL_CST (arg1);
9045 c2 = TREE_REAL_CST (arg2);
9046 /* c1.sign := c2.sign. */
9047 real_copysign (&c1, &c2);
9048 return build_real (type, c1);
9051 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9052 Remember to evaluate Y for side-effects. */
9053 if (tree_expr_nonnegative_p (arg2))
9054 return omit_one_operand_loc (loc, type,
9055 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9056 arg2);
9058 /* Strip sign changing operations for the first argument. */
9059 tem = fold_strip_sign_ops (arg1);
9060 if (tem)
9061 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9063 return NULL_TREE;
9066 /* Fold a call to builtin isascii with argument ARG. */
9068 static tree
9069 fold_builtin_isascii (location_t loc, tree arg)
9071 if (!validate_arg (arg, INTEGER_TYPE))
9072 return NULL_TREE;
9073 else
9075 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9076 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9077 build_int_cst (integer_type_node,
9078 ~ (unsigned HOST_WIDE_INT) 0x7f));
9079 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9080 arg, integer_zero_node);
9084 /* Fold a call to builtin toascii with argument ARG. */
9086 static tree
9087 fold_builtin_toascii (location_t loc, tree arg)
9089 if (!validate_arg (arg, INTEGER_TYPE))
9090 return NULL_TREE;
9092 /* Transform toascii(c) -> (c & 0x7f). */
9093 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9094 build_int_cst (integer_type_node, 0x7f));
9097 /* Fold a call to builtin isdigit with argument ARG. */
9099 static tree
9100 fold_builtin_isdigit (location_t loc, tree arg)
9102 if (!validate_arg (arg, INTEGER_TYPE))
9103 return NULL_TREE;
9104 else
9106 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9107 /* According to the C standard, isdigit is unaffected by locale.
9108 However, it definitely is affected by the target character set. */
9109 unsigned HOST_WIDE_INT target_digit0
9110 = lang_hooks.to_target_charset ('0');
9112 if (target_digit0 == 0)
9113 return NULL_TREE;
9115 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9116 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9117 build_int_cst (unsigned_type_node, target_digit0));
9118 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9119 build_int_cst (unsigned_type_node, 9));
9123 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9125 static tree
9126 fold_builtin_fabs (location_t loc, tree arg, tree type)
9128 if (!validate_arg (arg, REAL_TYPE))
9129 return NULL_TREE;
9131 arg = fold_convert_loc (loc, type, arg);
9132 if (TREE_CODE (arg) == REAL_CST)
9133 return fold_abs_const (arg, type);
9134 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9137 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9139 static tree
9140 fold_builtin_abs (location_t loc, tree arg, tree type)
9142 if (!validate_arg (arg, INTEGER_TYPE))
9143 return NULL_TREE;
9145 arg = fold_convert_loc (loc, type, arg);
9146 if (TREE_CODE (arg) == INTEGER_CST)
9147 return fold_abs_const (arg, type);
9148 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9151 /* Fold a fma operation with arguments ARG[012]. */
9153 tree
9154 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9155 tree type, tree arg0, tree arg1, tree arg2)
9157 if (TREE_CODE (arg0) == REAL_CST
9158 && TREE_CODE (arg1) == REAL_CST
9159 && TREE_CODE (arg2) == REAL_CST)
9160 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9162 return NULL_TREE;
9165 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9167 static tree
9168 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9170 if (validate_arg (arg0, REAL_TYPE)
9171 && validate_arg (arg1, REAL_TYPE)
9172 && validate_arg (arg2, REAL_TYPE))
9174 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9175 if (tem)
9176 return tem;
9178 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9179 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9180 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9182 return NULL_TREE;
9185 /* Fold a call to builtin fmin or fmax. */
9187 static tree
9188 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9189 tree type, bool max)
9191 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9193 /* Calculate the result when the argument is a constant. */
9194 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9196 if (res)
9197 return res;
9199 /* If either argument is NaN, return the other one. Avoid the
9200 transformation if we get (and honor) a signalling NaN. Using
9201 omit_one_operand() ensures we create a non-lvalue. */
9202 if (TREE_CODE (arg0) == REAL_CST
9203 && real_isnan (&TREE_REAL_CST (arg0))
9204 && (! HONOR_SNANS (arg0)
9205 || ! TREE_REAL_CST (arg0).signalling))
9206 return omit_one_operand_loc (loc, type, arg1, arg0);
9207 if (TREE_CODE (arg1) == REAL_CST
9208 && real_isnan (&TREE_REAL_CST (arg1))
9209 && (! HONOR_SNANS (arg1)
9210 || ! TREE_REAL_CST (arg1).signalling))
9211 return omit_one_operand_loc (loc, type, arg0, arg1);
9213 /* Transform fmin/fmax(x,x) -> x. */
9214 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9215 return omit_one_operand_loc (loc, type, arg0, arg1);
9217 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9218 functions to return the numeric arg if the other one is NaN.
9219 These tree codes don't honor that, so only transform if
9220 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9221 handled, so we don't have to worry about it either. */
9222 if (flag_finite_math_only)
9223 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9224 fold_convert_loc (loc, type, arg0),
9225 fold_convert_loc (loc, type, arg1));
9227 return NULL_TREE;
9230 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9232 static tree
9233 fold_builtin_carg (location_t loc, tree arg, tree type)
9235 if (validate_arg (arg, COMPLEX_TYPE)
9236 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9238 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9240 if (atan2_fn)
9242 tree new_arg = builtin_save_expr (arg);
9243 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9244 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9245 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9249 return NULL_TREE;
9252 /* Fold a call to builtin logb/ilogb. */
9254 static tree
9255 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9257 if (! validate_arg (arg, REAL_TYPE))
9258 return NULL_TREE;
9260 STRIP_NOPS (arg);
9262 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9264 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9266 switch (value->cl)
9268 case rvc_nan:
9269 case rvc_inf:
9270 /* If arg is Inf or NaN and we're logb, return it. */
9271 if (TREE_CODE (rettype) == REAL_TYPE)
9273 /* For logb(-Inf) we have to return +Inf. */
9274 if (real_isinf (value) && real_isneg (value))
9276 REAL_VALUE_TYPE tem;
9277 real_inf (&tem);
9278 return build_real (rettype, tem);
9280 return fold_convert_loc (loc, rettype, arg);
9282 /* Fall through... */
9283 case rvc_zero:
9284 /* Zero may set errno and/or raise an exception for logb, also
9285 for ilogb we don't know FP_ILOGB0. */
9286 return NULL_TREE;
9287 case rvc_normal:
9288 /* For normal numbers, proceed iff radix == 2. In GCC,
9289 normalized significands are in the range [0.5, 1.0). We
9290 want the exponent as if they were [1.0, 2.0) so get the
9291 exponent and subtract 1. */
9292 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9293 return fold_convert_loc (loc, rettype,
9294 build_int_cst (integer_type_node,
9295 REAL_EXP (value)-1));
9296 break;
9300 return NULL_TREE;
9303 /* Fold a call to builtin significand, if radix == 2. */
9305 static tree
9306 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9308 if (! validate_arg (arg, REAL_TYPE))
9309 return NULL_TREE;
9311 STRIP_NOPS (arg);
9313 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9315 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9317 switch (value->cl)
9319 case rvc_zero:
9320 case rvc_nan:
9321 case rvc_inf:
9322 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9323 return fold_convert_loc (loc, rettype, arg);
9324 case rvc_normal:
9325 /* For normal numbers, proceed iff radix == 2. */
9326 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9328 REAL_VALUE_TYPE result = *value;
9329 /* In GCC, normalized significands are in the range [0.5,
9330 1.0). We want them to be [1.0, 2.0) so set the
9331 exponent to 1. */
9332 SET_REAL_EXP (&result, 1);
9333 return build_real (rettype, result);
9335 break;
9339 return NULL_TREE;
9342 /* Fold a call to builtin frexp, we can assume the base is 2. */
9344 static tree
9345 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9347 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9348 return NULL_TREE;
9350 STRIP_NOPS (arg0);
9352 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9353 return NULL_TREE;
9355 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9357 /* Proceed if a valid pointer type was passed in. */
9358 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9360 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9361 tree frac, exp;
9363 switch (value->cl)
9365 case rvc_zero:
9366 /* For +-0, return (*exp = 0, +-0). */
9367 exp = integer_zero_node;
9368 frac = arg0;
9369 break;
9370 case rvc_nan:
9371 case rvc_inf:
9372 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9373 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9374 case rvc_normal:
9376 /* Since the frexp function always expects base 2, and in
9377 GCC normalized significands are already in the range
9378 [0.5, 1.0), we have exactly what frexp wants. */
9379 REAL_VALUE_TYPE frac_rvt = *value;
9380 SET_REAL_EXP (&frac_rvt, 0);
9381 frac = build_real (rettype, frac_rvt);
9382 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9384 break;
9385 default:
9386 gcc_unreachable ();
9389 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9390 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9391 TREE_SIDE_EFFECTS (arg1) = 1;
9392 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9395 return NULL_TREE;
9398 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9399 then we can assume the base is two. If it's false, then we have to
9400 check the mode of the TYPE parameter in certain cases. */
9402 static tree
9403 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9404 tree type, bool ldexp)
9406 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9408 STRIP_NOPS (arg0);
9409 STRIP_NOPS (arg1);
9411 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9412 if (real_zerop (arg0) || integer_zerop (arg1)
9413 || (TREE_CODE (arg0) == REAL_CST
9414 && !real_isfinite (&TREE_REAL_CST (arg0))))
9415 return omit_one_operand_loc (loc, type, arg0, arg1);
9417 /* If both arguments are constant, then try to evaluate it. */
9418 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9419 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9420 && tree_fits_shwi_p (arg1))
9422 /* Bound the maximum adjustment to twice the range of the
9423 mode's valid exponents. Use abs to ensure the range is
9424 positive as a sanity check. */
9425 const long max_exp_adj = 2 *
9426 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9427 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9429 /* Get the user-requested adjustment. */
9430 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9432 /* The requested adjustment must be inside this range. This
9433 is a preliminary cap to avoid things like overflow, we
9434 may still fail to compute the result for other reasons. */
9435 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9437 REAL_VALUE_TYPE initial_result;
9439 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9441 /* Ensure we didn't overflow. */
9442 if (! real_isinf (&initial_result))
9444 const REAL_VALUE_TYPE trunc_result
9445 = real_value_truncate (TYPE_MODE (type), initial_result);
9447 /* Only proceed if the target mode can hold the
9448 resulting value. */
9449 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9450 return build_real (type, trunc_result);
9456 return NULL_TREE;
9459 /* Fold a call to builtin modf. */
9461 static tree
9462 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9464 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9465 return NULL_TREE;
9467 STRIP_NOPS (arg0);
9469 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9470 return NULL_TREE;
9472 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9474 /* Proceed if a valid pointer type was passed in. */
9475 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9477 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9478 REAL_VALUE_TYPE trunc, frac;
9480 switch (value->cl)
9482 case rvc_nan:
9483 case rvc_zero:
9484 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9485 trunc = frac = *value;
9486 break;
9487 case rvc_inf:
9488 /* For +-Inf, return (*arg1 = arg0, +-0). */
9489 frac = dconst0;
9490 frac.sign = value->sign;
9491 trunc = *value;
9492 break;
9493 case rvc_normal:
9494 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9495 real_trunc (&trunc, VOIDmode, value);
9496 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9497 /* If the original number was negative and already
9498 integral, then the fractional part is -0.0. */
9499 if (value->sign && frac.cl == rvc_zero)
9500 frac.sign = value->sign;
9501 break;
9504 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9505 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9506 build_real (rettype, trunc));
9507 TREE_SIDE_EFFECTS (arg1) = 1;
9508 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9509 build_real (rettype, frac));
9512 return NULL_TREE;
9515 /* Given a location LOC, an interclass builtin function decl FNDECL
9516 and its single argument ARG, return an folded expression computing
9517 the same, or NULL_TREE if we either couldn't or didn't want to fold
9518 (the latter happen if there's an RTL instruction available). */
9520 static tree
9521 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9523 machine_mode mode;
9525 if (!validate_arg (arg, REAL_TYPE))
9526 return NULL_TREE;
9528 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9529 return NULL_TREE;
9531 mode = TYPE_MODE (TREE_TYPE (arg));
9533 /* If there is no optab, try generic code. */
9534 switch (DECL_FUNCTION_CODE (fndecl))
9536 tree result;
9538 CASE_FLT_FN (BUILT_IN_ISINF):
9540 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9541 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9542 tree const type = TREE_TYPE (arg);
9543 REAL_VALUE_TYPE r;
9544 char buf[128];
9546 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9547 real_from_string (&r, buf);
9548 result = build_call_expr (isgr_fn, 2,
9549 fold_build1_loc (loc, ABS_EXPR, type, arg),
9550 build_real (type, r));
9551 return result;
9553 CASE_FLT_FN (BUILT_IN_FINITE):
9554 case BUILT_IN_ISFINITE:
9556 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9557 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9558 tree const type = TREE_TYPE (arg);
9559 REAL_VALUE_TYPE r;
9560 char buf[128];
9562 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9563 real_from_string (&r, buf);
9564 result = build_call_expr (isle_fn, 2,
9565 fold_build1_loc (loc, ABS_EXPR, type, arg),
9566 build_real (type, r));
9567 /*result = fold_build2_loc (loc, UNGT_EXPR,
9568 TREE_TYPE (TREE_TYPE (fndecl)),
9569 fold_build1_loc (loc, ABS_EXPR, type, arg),
9570 build_real (type, r));
9571 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9572 TREE_TYPE (TREE_TYPE (fndecl)),
9573 result);*/
9574 return result;
9576 case BUILT_IN_ISNORMAL:
9578 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9579 islessequal(fabs(x),DBL_MAX). */
9580 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9581 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9582 tree const type = TREE_TYPE (arg);
9583 REAL_VALUE_TYPE rmax, rmin;
9584 char buf[128];
9586 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9587 real_from_string (&rmax, buf);
9588 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9589 real_from_string (&rmin, buf);
9590 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9591 result = build_call_expr (isle_fn, 2, arg,
9592 build_real (type, rmax));
9593 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9594 build_call_expr (isge_fn, 2, arg,
9595 build_real (type, rmin)));
9596 return result;
9598 default:
9599 break;
9602 return NULL_TREE;
9605 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9606 ARG is the argument for the call. */
9608 static tree
9609 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9611 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9612 REAL_VALUE_TYPE r;
9614 if (!validate_arg (arg, REAL_TYPE))
9615 return NULL_TREE;
9617 switch (builtin_index)
9619 case BUILT_IN_ISINF:
9620 if (!HONOR_INFINITIES (arg))
9621 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9623 if (TREE_CODE (arg) == REAL_CST)
9625 r = TREE_REAL_CST (arg);
9626 if (real_isinf (&r))
9627 return real_compare (GT_EXPR, &r, &dconst0)
9628 ? integer_one_node : integer_minus_one_node;
9629 else
9630 return integer_zero_node;
9633 return NULL_TREE;
9635 case BUILT_IN_ISINF_SIGN:
9637 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9638 /* In a boolean context, GCC will fold the inner COND_EXPR to
9639 1. So e.g. "if (isinf_sign(x))" would be folded to just
9640 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9641 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9642 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9643 tree tmp = NULL_TREE;
9645 arg = builtin_save_expr (arg);
9647 if (signbit_fn && isinf_fn)
9649 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9650 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9652 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9653 signbit_call, integer_zero_node);
9654 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9655 isinf_call, integer_zero_node);
9657 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9658 integer_minus_one_node, integer_one_node);
9659 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9660 isinf_call, tmp,
9661 integer_zero_node);
9664 return tmp;
9667 case BUILT_IN_ISFINITE:
9668 if (!HONOR_NANS (arg)
9669 && !HONOR_INFINITIES (arg))
9670 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9672 if (TREE_CODE (arg) == REAL_CST)
9674 r = TREE_REAL_CST (arg);
9675 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9678 return NULL_TREE;
9680 case BUILT_IN_ISNAN:
9681 if (!HONOR_NANS (arg))
9682 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9684 if (TREE_CODE (arg) == REAL_CST)
9686 r = TREE_REAL_CST (arg);
9687 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9690 arg = builtin_save_expr (arg);
9691 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9693 default:
9694 gcc_unreachable ();
9698 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9699 This builtin will generate code to return the appropriate floating
9700 point classification depending on the value of the floating point
9701 number passed in. The possible return values must be supplied as
9702 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9703 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9704 one floating point argument which is "type generic". */
9706 static tree
9707 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9709 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9710 arg, type, res, tmp;
9711 machine_mode mode;
9712 REAL_VALUE_TYPE r;
9713 char buf[128];
9715 /* Verify the required arguments in the original call. */
9716 if (nargs != 6
9717 || !validate_arg (args[0], INTEGER_TYPE)
9718 || !validate_arg (args[1], INTEGER_TYPE)
9719 || !validate_arg (args[2], INTEGER_TYPE)
9720 || !validate_arg (args[3], INTEGER_TYPE)
9721 || !validate_arg (args[4], INTEGER_TYPE)
9722 || !validate_arg (args[5], REAL_TYPE))
9723 return NULL_TREE;
9725 fp_nan = args[0];
9726 fp_infinite = args[1];
9727 fp_normal = args[2];
9728 fp_subnormal = args[3];
9729 fp_zero = args[4];
9730 arg = args[5];
9731 type = TREE_TYPE (arg);
9732 mode = TYPE_MODE (type);
9733 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9735 /* fpclassify(x) ->
9736 isnan(x) ? FP_NAN :
9737 (fabs(x) == Inf ? FP_INFINITE :
9738 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9739 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9741 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9742 build_real (type, dconst0));
9743 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9744 tmp, fp_zero, fp_subnormal);
9746 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9747 real_from_string (&r, buf);
9748 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9749 arg, build_real (type, r));
9750 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9752 if (HONOR_INFINITIES (mode))
9754 real_inf (&r);
9755 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9756 build_real (type, r));
9757 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9758 fp_infinite, res);
9761 if (HONOR_NANS (mode))
9763 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9764 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9767 return res;
9770 /* Fold a call to an unordered comparison function such as
9771 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9772 being called and ARG0 and ARG1 are the arguments for the call.
9773 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9774 the opposite of the desired result. UNORDERED_CODE is used
9775 for modes that can hold NaNs and ORDERED_CODE is used for
9776 the rest. */
9778 static tree
9779 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9780 enum tree_code unordered_code,
9781 enum tree_code ordered_code)
9783 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9784 enum tree_code code;
9785 tree type0, type1;
9786 enum tree_code code0, code1;
9787 tree cmp_type = NULL_TREE;
9789 type0 = TREE_TYPE (arg0);
9790 type1 = TREE_TYPE (arg1);
9792 code0 = TREE_CODE (type0);
9793 code1 = TREE_CODE (type1);
9795 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9796 /* Choose the wider of two real types. */
9797 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9798 ? type0 : type1;
9799 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9800 cmp_type = type0;
9801 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9802 cmp_type = type1;
9804 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9805 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9807 if (unordered_code == UNORDERED_EXPR)
9809 if (!HONOR_NANS (arg0))
9810 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9811 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9814 code = HONOR_NANS (arg0) ? unordered_code : ordered_code;
9815 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9816 fold_build2_loc (loc, code, type, arg0, arg1));
9819 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9820 arithmetics if it can never overflow, or into internal functions that
9821 return both result of arithmetics and overflowed boolean flag in
9822 a complex integer result, or some other check for overflow. */
9824 static tree
9825 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
9826 tree arg0, tree arg1, tree arg2)
9828 enum internal_fn ifn = IFN_LAST;
9829 tree type = TREE_TYPE (TREE_TYPE (arg2));
9830 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
9831 switch (fcode)
9833 case BUILT_IN_ADD_OVERFLOW:
9834 case BUILT_IN_SADD_OVERFLOW:
9835 case BUILT_IN_SADDL_OVERFLOW:
9836 case BUILT_IN_SADDLL_OVERFLOW:
9837 case BUILT_IN_UADD_OVERFLOW:
9838 case BUILT_IN_UADDL_OVERFLOW:
9839 case BUILT_IN_UADDLL_OVERFLOW:
9840 ifn = IFN_ADD_OVERFLOW;
9841 break;
9842 case BUILT_IN_SUB_OVERFLOW:
9843 case BUILT_IN_SSUB_OVERFLOW:
9844 case BUILT_IN_SSUBL_OVERFLOW:
9845 case BUILT_IN_SSUBLL_OVERFLOW:
9846 case BUILT_IN_USUB_OVERFLOW:
9847 case BUILT_IN_USUBL_OVERFLOW:
9848 case BUILT_IN_USUBLL_OVERFLOW:
9849 ifn = IFN_SUB_OVERFLOW;
9850 break;
9851 case BUILT_IN_MUL_OVERFLOW:
9852 case BUILT_IN_SMUL_OVERFLOW:
9853 case BUILT_IN_SMULL_OVERFLOW:
9854 case BUILT_IN_SMULLL_OVERFLOW:
9855 case BUILT_IN_UMUL_OVERFLOW:
9856 case BUILT_IN_UMULL_OVERFLOW:
9857 case BUILT_IN_UMULLL_OVERFLOW:
9858 ifn = IFN_MUL_OVERFLOW;
9859 break;
9860 default:
9861 gcc_unreachable ();
9863 tree ctype = build_complex_type (type);
9864 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
9865 2, arg0, arg1);
9866 tree tgt = save_expr (call);
9867 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9868 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9869 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
9870 tree store
9871 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
9872 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
9875 /* Fold a call to built-in function FNDECL with 0 arguments.
9876 This function returns NULL_TREE if no simplification was possible. */
9878 static tree
9879 fold_builtin_0 (location_t loc, tree fndecl)
9881 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9882 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9883 switch (fcode)
9885 CASE_FLT_FN (BUILT_IN_INF):
9886 case BUILT_IN_INFD32:
9887 case BUILT_IN_INFD64:
9888 case BUILT_IN_INFD128:
9889 return fold_builtin_inf (loc, type, true);
9891 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9892 return fold_builtin_inf (loc, type, false);
9894 case BUILT_IN_CLASSIFY_TYPE:
9895 return fold_builtin_classify_type (NULL_TREE);
9897 default:
9898 break;
9900 return NULL_TREE;
9903 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9904 This function returns NULL_TREE if no simplification was possible. */
9906 static tree
9907 fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
9909 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9910 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9911 switch (fcode)
9913 case BUILT_IN_CONSTANT_P:
9915 tree val = fold_builtin_constant_p (arg0);
9917 /* Gimplification will pull the CALL_EXPR for the builtin out of
9918 an if condition. When not optimizing, we'll not CSE it back.
9919 To avoid link error types of regressions, return false now. */
9920 if (!val && !optimize)
9921 val = integer_zero_node;
9923 return val;
9926 case BUILT_IN_CLASSIFY_TYPE:
9927 return fold_builtin_classify_type (arg0);
9929 case BUILT_IN_STRLEN:
9930 return fold_builtin_strlen (loc, type, arg0);
9932 CASE_FLT_FN (BUILT_IN_FABS):
9933 case BUILT_IN_FABSD32:
9934 case BUILT_IN_FABSD64:
9935 case BUILT_IN_FABSD128:
9936 return fold_builtin_fabs (loc, arg0, type);
9938 case BUILT_IN_ABS:
9939 case BUILT_IN_LABS:
9940 case BUILT_IN_LLABS:
9941 case BUILT_IN_IMAXABS:
9942 return fold_builtin_abs (loc, arg0, type);
9944 CASE_FLT_FN (BUILT_IN_CONJ):
9945 if (validate_arg (arg0, COMPLEX_TYPE)
9946 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9947 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9948 break;
9950 CASE_FLT_FN (BUILT_IN_CREAL):
9951 if (validate_arg (arg0, COMPLEX_TYPE)
9952 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9953 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9954 break;
9956 CASE_FLT_FN (BUILT_IN_CIMAG):
9957 if (validate_arg (arg0, COMPLEX_TYPE)
9958 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9959 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9960 break;
9962 CASE_FLT_FN (BUILT_IN_CCOS):
9963 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
9965 CASE_FLT_FN (BUILT_IN_CCOSH):
9966 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
9968 CASE_FLT_FN (BUILT_IN_CPROJ):
9969 return fold_builtin_cproj (loc, arg0, type);
9971 CASE_FLT_FN (BUILT_IN_CSIN):
9972 if (validate_arg (arg0, COMPLEX_TYPE)
9973 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9974 return do_mpc_arg1 (arg0, type, mpc_sin);
9975 break;
9977 CASE_FLT_FN (BUILT_IN_CSINH):
9978 if (validate_arg (arg0, COMPLEX_TYPE)
9979 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9980 return do_mpc_arg1 (arg0, type, mpc_sinh);
9981 break;
9983 CASE_FLT_FN (BUILT_IN_CTAN):
9984 if (validate_arg (arg0, COMPLEX_TYPE)
9985 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9986 return do_mpc_arg1 (arg0, type, mpc_tan);
9987 break;
9989 CASE_FLT_FN (BUILT_IN_CTANH):
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_tanh);
9993 break;
9995 CASE_FLT_FN (BUILT_IN_CLOG):
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_log);
9999 break;
10001 CASE_FLT_FN (BUILT_IN_CSQRT):
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_sqrt);
10005 break;
10007 CASE_FLT_FN (BUILT_IN_CASIN):
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_asin);
10011 break;
10013 CASE_FLT_FN (BUILT_IN_CACOS):
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_acos);
10017 break;
10019 CASE_FLT_FN (BUILT_IN_CATAN):
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_atan);
10023 break;
10025 CASE_FLT_FN (BUILT_IN_CASINH):
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_asinh);
10029 break;
10031 CASE_FLT_FN (BUILT_IN_CACOSH):
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_acosh);
10035 break;
10037 CASE_FLT_FN (BUILT_IN_CATANH):
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_atanh);
10041 break;
10043 CASE_FLT_FN (BUILT_IN_CABS):
10044 return fold_builtin_cabs (loc, arg0, type, fndecl);
10046 CASE_FLT_FN (BUILT_IN_CARG):
10047 return fold_builtin_carg (loc, arg0, type);
10049 CASE_FLT_FN (BUILT_IN_SQRT):
10050 return fold_builtin_sqrt (loc, arg0, type);
10052 CASE_FLT_FN (BUILT_IN_CBRT):
10053 return fold_builtin_cbrt (loc, arg0, type);
10055 CASE_FLT_FN (BUILT_IN_ASIN):
10056 if (validate_arg (arg0, REAL_TYPE))
10057 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10058 &dconstm1, &dconst1, true);
10059 break;
10061 CASE_FLT_FN (BUILT_IN_ACOS):
10062 if (validate_arg (arg0, REAL_TYPE))
10063 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10064 &dconstm1, &dconst1, true);
10065 break;
10067 CASE_FLT_FN (BUILT_IN_ATAN):
10068 if (validate_arg (arg0, REAL_TYPE))
10069 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10070 break;
10072 CASE_FLT_FN (BUILT_IN_ASINH):
10073 if (validate_arg (arg0, REAL_TYPE))
10074 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10075 break;
10077 CASE_FLT_FN (BUILT_IN_ACOSH):
10078 if (validate_arg (arg0, REAL_TYPE))
10079 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10080 &dconst1, NULL, true);
10081 break;
10083 CASE_FLT_FN (BUILT_IN_ATANH):
10084 if (validate_arg (arg0, REAL_TYPE))
10085 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10086 &dconstm1, &dconst1, false);
10087 break;
10089 CASE_FLT_FN (BUILT_IN_SIN):
10090 if (validate_arg (arg0, REAL_TYPE))
10091 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10092 break;
10094 CASE_FLT_FN (BUILT_IN_COS):
10095 return fold_builtin_cos (loc, arg0, type, fndecl);
10097 CASE_FLT_FN (BUILT_IN_TAN):
10098 return fold_builtin_tan (arg0, type);
10100 CASE_FLT_FN (BUILT_IN_CEXP):
10101 return fold_builtin_cexp (loc, arg0, type);
10103 CASE_FLT_FN (BUILT_IN_CEXPI):
10104 if (validate_arg (arg0, REAL_TYPE))
10105 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10106 break;
10108 CASE_FLT_FN (BUILT_IN_SINH):
10109 if (validate_arg (arg0, REAL_TYPE))
10110 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10111 break;
10113 CASE_FLT_FN (BUILT_IN_COSH):
10114 return fold_builtin_cosh (loc, arg0, type, fndecl);
10116 CASE_FLT_FN (BUILT_IN_TANH):
10117 if (validate_arg (arg0, REAL_TYPE))
10118 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10119 break;
10121 CASE_FLT_FN (BUILT_IN_ERF):
10122 if (validate_arg (arg0, REAL_TYPE))
10123 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10124 break;
10126 CASE_FLT_FN (BUILT_IN_ERFC):
10127 if (validate_arg (arg0, REAL_TYPE))
10128 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10129 break;
10131 CASE_FLT_FN (BUILT_IN_TGAMMA):
10132 if (validate_arg (arg0, REAL_TYPE))
10133 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10134 break;
10136 CASE_FLT_FN (BUILT_IN_EXP):
10137 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10139 CASE_FLT_FN (BUILT_IN_EXP2):
10140 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10142 CASE_FLT_FN (BUILT_IN_EXP10):
10143 CASE_FLT_FN (BUILT_IN_POW10):
10144 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10146 CASE_FLT_FN (BUILT_IN_EXPM1):
10147 if (validate_arg (arg0, REAL_TYPE))
10148 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10149 break;
10151 CASE_FLT_FN (BUILT_IN_LOG):
10152 if (validate_arg (arg0, REAL_TYPE))
10153 return do_mpfr_arg1 (arg0, type, mpfr_log, &dconst0, NULL, false);
10154 break;
10156 CASE_FLT_FN (BUILT_IN_LOG2):
10157 if (validate_arg (arg0, REAL_TYPE))
10158 return do_mpfr_arg1 (arg0, type, mpfr_log2, &dconst0, NULL, false);
10159 break;
10161 CASE_FLT_FN (BUILT_IN_LOG10):
10162 if (validate_arg (arg0, REAL_TYPE))
10163 return do_mpfr_arg1 (arg0, type, mpfr_log10, &dconst0, NULL, false);
10164 break;
10166 CASE_FLT_FN (BUILT_IN_LOG1P):
10167 if (validate_arg (arg0, REAL_TYPE))
10168 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10169 &dconstm1, NULL, false);
10170 break;
10172 CASE_FLT_FN (BUILT_IN_J0):
10173 if (validate_arg (arg0, REAL_TYPE))
10174 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10175 NULL, NULL, 0);
10176 break;
10178 CASE_FLT_FN (BUILT_IN_J1):
10179 if (validate_arg (arg0, REAL_TYPE))
10180 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10181 NULL, NULL, 0);
10182 break;
10184 CASE_FLT_FN (BUILT_IN_Y0):
10185 if (validate_arg (arg0, REAL_TYPE))
10186 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10187 &dconst0, NULL, false);
10188 break;
10190 CASE_FLT_FN (BUILT_IN_Y1):
10191 if (validate_arg (arg0, REAL_TYPE))
10192 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10193 &dconst0, NULL, false);
10194 break;
10196 CASE_FLT_FN (BUILT_IN_NAN):
10197 case BUILT_IN_NAND32:
10198 case BUILT_IN_NAND64:
10199 case BUILT_IN_NAND128:
10200 return fold_builtin_nan (arg0, type, true);
10202 CASE_FLT_FN (BUILT_IN_NANS):
10203 return fold_builtin_nan (arg0, type, false);
10205 CASE_FLT_FN (BUILT_IN_FLOOR):
10206 return fold_builtin_floor (loc, fndecl, arg0);
10208 CASE_FLT_FN (BUILT_IN_CEIL):
10209 return fold_builtin_ceil (loc, fndecl, arg0);
10211 CASE_FLT_FN (BUILT_IN_TRUNC):
10212 return fold_builtin_trunc (loc, fndecl, arg0);
10214 CASE_FLT_FN (BUILT_IN_ROUND):
10215 return fold_builtin_round (loc, fndecl, arg0);
10217 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10218 CASE_FLT_FN (BUILT_IN_RINT):
10219 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10221 CASE_FLT_FN (BUILT_IN_ICEIL):
10222 CASE_FLT_FN (BUILT_IN_LCEIL):
10223 CASE_FLT_FN (BUILT_IN_LLCEIL):
10224 CASE_FLT_FN (BUILT_IN_LFLOOR):
10225 CASE_FLT_FN (BUILT_IN_IFLOOR):
10226 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10227 CASE_FLT_FN (BUILT_IN_IROUND):
10228 CASE_FLT_FN (BUILT_IN_LROUND):
10229 CASE_FLT_FN (BUILT_IN_LLROUND):
10230 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10232 CASE_FLT_FN (BUILT_IN_IRINT):
10233 CASE_FLT_FN (BUILT_IN_LRINT):
10234 CASE_FLT_FN (BUILT_IN_LLRINT):
10235 return fold_fixed_mathfn (loc, fndecl, arg0);
10237 case BUILT_IN_BSWAP16:
10238 case BUILT_IN_BSWAP32:
10239 case BUILT_IN_BSWAP64:
10240 return fold_builtin_bswap (fndecl, arg0);
10242 CASE_INT_FN (BUILT_IN_FFS):
10243 CASE_INT_FN (BUILT_IN_CLZ):
10244 CASE_INT_FN (BUILT_IN_CTZ):
10245 CASE_INT_FN (BUILT_IN_CLRSB):
10246 CASE_INT_FN (BUILT_IN_POPCOUNT):
10247 CASE_INT_FN (BUILT_IN_PARITY):
10248 return fold_builtin_bitop (fndecl, arg0);
10250 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10251 return fold_builtin_signbit (loc, arg0, type);
10253 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10254 return fold_builtin_significand (loc, arg0, type);
10256 CASE_FLT_FN (BUILT_IN_ILOGB):
10257 CASE_FLT_FN (BUILT_IN_LOGB):
10258 return fold_builtin_logb (loc, arg0, type);
10260 case BUILT_IN_ISASCII:
10261 return fold_builtin_isascii (loc, arg0);
10263 case BUILT_IN_TOASCII:
10264 return fold_builtin_toascii (loc, arg0);
10266 case BUILT_IN_ISDIGIT:
10267 return fold_builtin_isdigit (loc, arg0);
10269 CASE_FLT_FN (BUILT_IN_FINITE):
10270 case BUILT_IN_FINITED32:
10271 case BUILT_IN_FINITED64:
10272 case BUILT_IN_FINITED128:
10273 case BUILT_IN_ISFINITE:
10275 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10276 if (ret)
10277 return ret;
10278 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10281 CASE_FLT_FN (BUILT_IN_ISINF):
10282 case BUILT_IN_ISINFD32:
10283 case BUILT_IN_ISINFD64:
10284 case BUILT_IN_ISINFD128:
10286 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10287 if (ret)
10288 return ret;
10289 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10292 case BUILT_IN_ISNORMAL:
10293 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10295 case BUILT_IN_ISINF_SIGN:
10296 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10298 CASE_FLT_FN (BUILT_IN_ISNAN):
10299 case BUILT_IN_ISNAND32:
10300 case BUILT_IN_ISNAND64:
10301 case BUILT_IN_ISNAND128:
10302 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10304 case BUILT_IN_FREE:
10305 if (integer_zerop (arg0))
10306 return build_empty_stmt (loc);
10307 break;
10309 default:
10310 break;
10313 return NULL_TREE;
10317 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10318 This function returns NULL_TREE if no simplification was possible. */
10320 static tree
10321 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
10323 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10324 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10326 switch (fcode)
10328 CASE_FLT_FN (BUILT_IN_JN):
10329 if (validate_arg (arg0, INTEGER_TYPE)
10330 && validate_arg (arg1, REAL_TYPE))
10331 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10332 break;
10334 CASE_FLT_FN (BUILT_IN_YN):
10335 if (validate_arg (arg0, INTEGER_TYPE)
10336 && validate_arg (arg1, REAL_TYPE))
10337 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10338 &dconst0, false);
10339 break;
10341 CASE_FLT_FN (BUILT_IN_DREM):
10342 CASE_FLT_FN (BUILT_IN_REMAINDER):
10343 if (validate_arg (arg0, REAL_TYPE)
10344 && validate_arg (arg1, REAL_TYPE))
10345 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10346 break;
10348 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10349 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10350 if (validate_arg (arg0, REAL_TYPE)
10351 && validate_arg (arg1, POINTER_TYPE))
10352 return do_mpfr_lgamma_r (arg0, arg1, type);
10353 break;
10355 CASE_FLT_FN (BUILT_IN_ATAN2):
10356 if (validate_arg (arg0, REAL_TYPE)
10357 && validate_arg (arg1, REAL_TYPE))
10358 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10359 break;
10361 CASE_FLT_FN (BUILT_IN_FDIM):
10362 if (validate_arg (arg0, REAL_TYPE)
10363 && validate_arg (arg1, REAL_TYPE))
10364 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10365 break;
10367 CASE_FLT_FN (BUILT_IN_HYPOT):
10368 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10370 CASE_FLT_FN (BUILT_IN_CPOW):
10371 if (validate_arg (arg0, COMPLEX_TYPE)
10372 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10373 && validate_arg (arg1, COMPLEX_TYPE)
10374 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10375 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10376 break;
10378 CASE_FLT_FN (BUILT_IN_LDEXP):
10379 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10380 CASE_FLT_FN (BUILT_IN_SCALBN):
10381 CASE_FLT_FN (BUILT_IN_SCALBLN):
10382 return fold_builtin_load_exponent (loc, arg0, arg1,
10383 type, /*ldexp=*/false);
10385 CASE_FLT_FN (BUILT_IN_FREXP):
10386 return fold_builtin_frexp (loc, arg0, arg1, type);
10388 CASE_FLT_FN (BUILT_IN_MODF):
10389 return fold_builtin_modf (loc, arg0, arg1, type);
10391 case BUILT_IN_STRSTR:
10392 return fold_builtin_strstr (loc, arg0, arg1, type);
10394 case BUILT_IN_STRSPN:
10395 return fold_builtin_strspn (loc, arg0, arg1);
10397 case BUILT_IN_STRCSPN:
10398 return fold_builtin_strcspn (loc, arg0, arg1);
10400 case BUILT_IN_STRCHR:
10401 case BUILT_IN_INDEX:
10402 return fold_builtin_strchr (loc, arg0, arg1, type);
10404 case BUILT_IN_STRRCHR:
10405 case BUILT_IN_RINDEX:
10406 return fold_builtin_strrchr (loc, arg0, arg1, type);
10408 case BUILT_IN_STRCMP:
10409 return fold_builtin_strcmp (loc, arg0, arg1);
10411 case BUILT_IN_STRPBRK:
10412 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10414 case BUILT_IN_EXPECT:
10415 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
10417 CASE_FLT_FN (BUILT_IN_POW):
10418 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10420 CASE_FLT_FN (BUILT_IN_POWI):
10421 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10423 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10424 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10426 CASE_FLT_FN (BUILT_IN_FMIN):
10427 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10429 CASE_FLT_FN (BUILT_IN_FMAX):
10430 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10432 case BUILT_IN_ISGREATER:
10433 return fold_builtin_unordered_cmp (loc, fndecl,
10434 arg0, arg1, UNLE_EXPR, LE_EXPR);
10435 case BUILT_IN_ISGREATEREQUAL:
10436 return fold_builtin_unordered_cmp (loc, fndecl,
10437 arg0, arg1, UNLT_EXPR, LT_EXPR);
10438 case BUILT_IN_ISLESS:
10439 return fold_builtin_unordered_cmp (loc, fndecl,
10440 arg0, arg1, UNGE_EXPR, GE_EXPR);
10441 case BUILT_IN_ISLESSEQUAL:
10442 return fold_builtin_unordered_cmp (loc, fndecl,
10443 arg0, arg1, UNGT_EXPR, GT_EXPR);
10444 case BUILT_IN_ISLESSGREATER:
10445 return fold_builtin_unordered_cmp (loc, fndecl,
10446 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10447 case BUILT_IN_ISUNORDERED:
10448 return fold_builtin_unordered_cmp (loc, fndecl,
10449 arg0, arg1, UNORDERED_EXPR,
10450 NOP_EXPR);
10452 /* We do the folding for va_start in the expander. */
10453 case BUILT_IN_VA_START:
10454 break;
10456 case BUILT_IN_OBJECT_SIZE:
10457 return fold_builtin_object_size (arg0, arg1);
10459 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10460 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10462 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10463 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10465 default:
10466 break;
10468 return NULL_TREE;
10471 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10472 and ARG2.
10473 This function returns NULL_TREE if no simplification was possible. */
10475 static tree
10476 fold_builtin_3 (location_t loc, tree fndecl,
10477 tree arg0, tree arg1, tree arg2)
10479 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10480 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10481 switch (fcode)
10484 CASE_FLT_FN (BUILT_IN_SINCOS):
10485 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10487 CASE_FLT_FN (BUILT_IN_FMA):
10488 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10489 break;
10491 CASE_FLT_FN (BUILT_IN_REMQUO):
10492 if (validate_arg (arg0, REAL_TYPE)
10493 && validate_arg (arg1, REAL_TYPE)
10494 && validate_arg (arg2, POINTER_TYPE))
10495 return do_mpfr_remquo (arg0, arg1, arg2);
10496 break;
10498 case BUILT_IN_STRNCMP:
10499 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10501 case BUILT_IN_MEMCHR:
10502 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10504 case BUILT_IN_BCMP:
10505 case BUILT_IN_MEMCMP:
10506 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10508 case BUILT_IN_EXPECT:
10509 return fold_builtin_expect (loc, arg0, arg1, arg2);
10511 case BUILT_IN_ADD_OVERFLOW:
10512 case BUILT_IN_SUB_OVERFLOW:
10513 case BUILT_IN_MUL_OVERFLOW:
10514 case BUILT_IN_SADD_OVERFLOW:
10515 case BUILT_IN_SADDL_OVERFLOW:
10516 case BUILT_IN_SADDLL_OVERFLOW:
10517 case BUILT_IN_SSUB_OVERFLOW:
10518 case BUILT_IN_SSUBL_OVERFLOW:
10519 case BUILT_IN_SSUBLL_OVERFLOW:
10520 case BUILT_IN_SMUL_OVERFLOW:
10521 case BUILT_IN_SMULL_OVERFLOW:
10522 case BUILT_IN_SMULLL_OVERFLOW:
10523 case BUILT_IN_UADD_OVERFLOW:
10524 case BUILT_IN_UADDL_OVERFLOW:
10525 case BUILT_IN_UADDLL_OVERFLOW:
10526 case BUILT_IN_USUB_OVERFLOW:
10527 case BUILT_IN_USUBL_OVERFLOW:
10528 case BUILT_IN_USUBLL_OVERFLOW:
10529 case BUILT_IN_UMUL_OVERFLOW:
10530 case BUILT_IN_UMULL_OVERFLOW:
10531 case BUILT_IN_UMULLL_OVERFLOW:
10532 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
10534 default:
10535 break;
10537 return NULL_TREE;
10540 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10541 arguments. IGNORE is true if the result of the
10542 function call is ignored. This function returns NULL_TREE if no
10543 simplification was possible. */
10545 tree
10546 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool)
10548 tree ret = NULL_TREE;
10550 switch (nargs)
10552 case 0:
10553 ret = fold_builtin_0 (loc, fndecl);
10554 break;
10555 case 1:
10556 ret = fold_builtin_1 (loc, fndecl, args[0]);
10557 break;
10558 case 2:
10559 ret = fold_builtin_2 (loc, fndecl, args[0], args[1]);
10560 break;
10561 case 3:
10562 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
10563 break;
10564 default:
10565 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
10566 break;
10568 if (ret)
10570 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10571 SET_EXPR_LOCATION (ret, loc);
10572 TREE_NO_WARNING (ret) = 1;
10573 return ret;
10575 return NULL_TREE;
10578 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10579 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10580 of arguments in ARGS to be omitted. OLDNARGS is the number of
10581 elements in ARGS. */
10583 static tree
10584 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10585 int skip, tree fndecl, int n, va_list newargs)
10587 int nargs = oldnargs - skip + n;
10588 tree *buffer;
10590 if (n > 0)
10592 int i, j;
10594 buffer = XALLOCAVEC (tree, nargs);
10595 for (i = 0; i < n; i++)
10596 buffer[i] = va_arg (newargs, tree);
10597 for (j = skip; j < oldnargs; j++, i++)
10598 buffer[i] = args[j];
10600 else
10601 buffer = args + skip;
10603 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10606 /* Return true if FNDECL shouldn't be folded right now.
10607 If a built-in function has an inline attribute always_inline
10608 wrapper, defer folding it after always_inline functions have
10609 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10610 might not be performed. */
10612 bool
10613 avoid_folding_inline_builtin (tree fndecl)
10615 return (DECL_DECLARED_INLINE_P (fndecl)
10616 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10617 && cfun
10618 && !cfun->always_inline_functions_inlined
10619 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10622 /* A wrapper function for builtin folding that prevents warnings for
10623 "statement without effect" and the like, caused by removing the
10624 call node earlier than the warning is generated. */
10626 tree
10627 fold_call_expr (location_t loc, tree exp, bool ignore)
10629 tree ret = NULL_TREE;
10630 tree fndecl = get_callee_fndecl (exp);
10631 if (fndecl
10632 && TREE_CODE (fndecl) == FUNCTION_DECL
10633 && DECL_BUILT_IN (fndecl)
10634 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10635 yet. Defer folding until we see all the arguments
10636 (after inlining). */
10637 && !CALL_EXPR_VA_ARG_PACK (exp))
10639 int nargs = call_expr_nargs (exp);
10641 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10642 instead last argument is __builtin_va_arg_pack (). Defer folding
10643 even in that case, until arguments are finalized. */
10644 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10646 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10647 if (fndecl2
10648 && TREE_CODE (fndecl2) == FUNCTION_DECL
10649 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10650 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10651 return NULL_TREE;
10654 if (avoid_folding_inline_builtin (fndecl))
10655 return NULL_TREE;
10657 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10658 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10659 CALL_EXPR_ARGP (exp), ignore);
10660 else
10662 tree *args = CALL_EXPR_ARGP (exp);
10663 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10664 if (ret)
10665 return ret;
10668 return NULL_TREE;
10671 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
10672 N arguments are passed in the array ARGARRAY. Return a folded
10673 expression or NULL_TREE if no simplification was possible. */
10675 tree
10676 fold_builtin_call_array (location_t loc, tree,
10677 tree fn,
10678 int n,
10679 tree *argarray)
10681 if (TREE_CODE (fn) != ADDR_EXPR)
10682 return NULL_TREE;
10684 tree fndecl = TREE_OPERAND (fn, 0);
10685 if (TREE_CODE (fndecl) == FUNCTION_DECL
10686 && DECL_BUILT_IN (fndecl))
10688 /* If last argument is __builtin_va_arg_pack (), arguments to this
10689 function are not finalized yet. Defer folding until they are. */
10690 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10692 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10693 if (fndecl2
10694 && TREE_CODE (fndecl2) == FUNCTION_DECL
10695 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10696 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10697 return NULL_TREE;
10699 if (avoid_folding_inline_builtin (fndecl))
10700 return NULL_TREE;
10701 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10702 return targetm.fold_builtin (fndecl, n, argarray, false);
10703 else
10704 return fold_builtin_n (loc, fndecl, argarray, n, false);
10707 return NULL_TREE;
10710 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10711 along with N new arguments specified as the "..." parameters. SKIP
10712 is the number of arguments in EXP to be omitted. This function is used
10713 to do varargs-to-varargs transformations. */
10715 static tree
10716 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10718 va_list ap;
10719 tree t;
10721 va_start (ap, n);
10722 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10723 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10724 va_end (ap);
10726 return t;
10729 /* Validate a single argument ARG against a tree code CODE representing
10730 a type. */
10732 static bool
10733 validate_arg (const_tree arg, enum tree_code code)
10735 if (!arg)
10736 return false;
10737 else if (code == POINTER_TYPE)
10738 return POINTER_TYPE_P (TREE_TYPE (arg));
10739 else if (code == INTEGER_TYPE)
10740 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10741 return code == TREE_CODE (TREE_TYPE (arg));
10744 /* This function validates the types of a function call argument list
10745 against a specified list of tree_codes. If the last specifier is a 0,
10746 that represents an ellipses, otherwise the last specifier must be a
10747 VOID_TYPE.
10749 This is the GIMPLE version of validate_arglist. Eventually we want to
10750 completely convert builtins.c to work from GIMPLEs and the tree based
10751 validate_arglist will then be removed. */
10753 bool
10754 validate_gimple_arglist (const gcall *call, ...)
10756 enum tree_code code;
10757 bool res = 0;
10758 va_list ap;
10759 const_tree arg;
10760 size_t i;
10762 va_start (ap, call);
10763 i = 0;
10767 code = (enum tree_code) va_arg (ap, int);
10768 switch (code)
10770 case 0:
10771 /* This signifies an ellipses, any further arguments are all ok. */
10772 res = true;
10773 goto end;
10774 case VOID_TYPE:
10775 /* This signifies an endlink, if no arguments remain, return
10776 true, otherwise return false. */
10777 res = (i == gimple_call_num_args (call));
10778 goto end;
10779 default:
10780 /* If no parameters remain or the parameter's code does not
10781 match the specified code, return false. Otherwise continue
10782 checking any remaining arguments. */
10783 arg = gimple_call_arg (call, i++);
10784 if (!validate_arg (arg, code))
10785 goto end;
10786 break;
10789 while (1);
10791 /* We need gotos here since we can only have one VA_CLOSE in a
10792 function. */
10793 end: ;
10794 va_end (ap);
10796 return res;
10799 /* Default target-specific builtin expander that does nothing. */
10802 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10803 rtx target ATTRIBUTE_UNUSED,
10804 rtx subtarget ATTRIBUTE_UNUSED,
10805 machine_mode mode ATTRIBUTE_UNUSED,
10806 int ignore ATTRIBUTE_UNUSED)
10808 return NULL_RTX;
10811 /* Returns true is EXP represents data that would potentially reside
10812 in a readonly section. */
10814 bool
10815 readonly_data_expr (tree exp)
10817 STRIP_NOPS (exp);
10819 if (TREE_CODE (exp) != ADDR_EXPR)
10820 return false;
10822 exp = get_base_address (TREE_OPERAND (exp, 0));
10823 if (!exp)
10824 return false;
10826 /* Make sure we call decl_readonly_section only for trees it
10827 can handle (since it returns true for everything it doesn't
10828 understand). */
10829 if (TREE_CODE (exp) == STRING_CST
10830 || TREE_CODE (exp) == CONSTRUCTOR
10831 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
10832 return decl_readonly_section (exp, 0);
10833 else
10834 return false;
10837 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10838 to the call, and TYPE is its return type.
10840 Return NULL_TREE if no simplification was possible, otherwise return the
10841 simplified form of the call as a tree.
10843 The simplified form may be a constant or other expression which
10844 computes the same value, but in a more efficient manner (including
10845 calls to other builtin functions).
10847 The call may contain arguments which need to be evaluated, but
10848 which are not useful to determine the result of the call. In
10849 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10850 COMPOUND_EXPR will be an argument which must be evaluated.
10851 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10852 COMPOUND_EXPR in the chain will contain the tree for the simplified
10853 form of the builtin function call. */
10855 static tree
10856 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
10858 if (!validate_arg (s1, POINTER_TYPE)
10859 || !validate_arg (s2, POINTER_TYPE))
10860 return NULL_TREE;
10861 else
10863 tree fn;
10864 const char *p1, *p2;
10866 p2 = c_getstr (s2);
10867 if (p2 == NULL)
10868 return NULL_TREE;
10870 p1 = c_getstr (s1);
10871 if (p1 != NULL)
10873 const char *r = strstr (p1, p2);
10874 tree tem;
10876 if (r == NULL)
10877 return build_int_cst (TREE_TYPE (s1), 0);
10879 /* Return an offset into the constant string argument. */
10880 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10881 return fold_convert_loc (loc, type, tem);
10884 /* The argument is const char *, and the result is char *, so we need
10885 a type conversion here to avoid a warning. */
10886 if (p2[0] == '\0')
10887 return fold_convert_loc (loc, type, s1);
10889 if (p2[1] != '\0')
10890 return NULL_TREE;
10892 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10893 if (!fn)
10894 return NULL_TREE;
10896 /* New argument list transforming strstr(s1, s2) to
10897 strchr(s1, s2[0]). */
10898 return build_call_expr_loc (loc, fn, 2, s1,
10899 build_int_cst (integer_type_node, p2[0]));
10903 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10904 the call, and TYPE is its return type.
10906 Return NULL_TREE if no simplification was possible, otherwise return the
10907 simplified form of the call as a tree.
10909 The simplified form may be a constant or other expression which
10910 computes the same value, but in a more efficient manner (including
10911 calls to other builtin functions).
10913 The call may contain arguments which need to be evaluated, but
10914 which are not useful to determine the result of the call. In
10915 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10916 COMPOUND_EXPR will be an argument which must be evaluated.
10917 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10918 COMPOUND_EXPR in the chain will contain the tree for the simplified
10919 form of the builtin function call. */
10921 static tree
10922 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
10924 if (!validate_arg (s1, POINTER_TYPE)
10925 || !validate_arg (s2, INTEGER_TYPE))
10926 return NULL_TREE;
10927 else
10929 const char *p1;
10931 if (TREE_CODE (s2) != INTEGER_CST)
10932 return NULL_TREE;
10934 p1 = c_getstr (s1);
10935 if (p1 != NULL)
10937 char c;
10938 const char *r;
10939 tree tem;
10941 if (target_char_cast (s2, &c))
10942 return NULL_TREE;
10944 r = strchr (p1, c);
10946 if (r == NULL)
10947 return build_int_cst (TREE_TYPE (s1), 0);
10949 /* Return an offset into the constant string argument. */
10950 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10951 return fold_convert_loc (loc, type, tem);
10953 return NULL_TREE;
10957 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10958 the call, and TYPE is its return type.
10960 Return NULL_TREE if no simplification was possible, otherwise return the
10961 simplified form of the call as a tree.
10963 The simplified form may be a constant or other expression which
10964 computes the same value, but in a more efficient manner (including
10965 calls to other builtin functions).
10967 The call may contain arguments which need to be evaluated, but
10968 which are not useful to determine the result of the call. In
10969 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10970 COMPOUND_EXPR will be an argument which must be evaluated.
10971 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10972 COMPOUND_EXPR in the chain will contain the tree for the simplified
10973 form of the builtin function call. */
10975 static tree
10976 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
10978 if (!validate_arg (s1, POINTER_TYPE)
10979 || !validate_arg (s2, INTEGER_TYPE))
10980 return NULL_TREE;
10981 else
10983 tree fn;
10984 const char *p1;
10986 if (TREE_CODE (s2) != INTEGER_CST)
10987 return NULL_TREE;
10989 p1 = c_getstr (s1);
10990 if (p1 != NULL)
10992 char c;
10993 const char *r;
10994 tree tem;
10996 if (target_char_cast (s2, &c))
10997 return NULL_TREE;
10999 r = strrchr (p1, c);
11001 if (r == NULL)
11002 return build_int_cst (TREE_TYPE (s1), 0);
11004 /* Return an offset into the constant string argument. */
11005 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11006 return fold_convert_loc (loc, type, tem);
11009 if (! integer_zerop (s2))
11010 return NULL_TREE;
11012 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11013 if (!fn)
11014 return NULL_TREE;
11016 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11017 return build_call_expr_loc (loc, fn, 2, s1, s2);
11021 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11022 to the call, and TYPE is its return type.
11024 Return NULL_TREE if no simplification was possible, otherwise return the
11025 simplified form of the call as a tree.
11027 The simplified form may be a constant or other expression which
11028 computes the same value, but in a more efficient manner (including
11029 calls to other builtin functions).
11031 The call may contain arguments which need to be evaluated, but
11032 which are not useful to determine the result of the call. In
11033 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11034 COMPOUND_EXPR will be an argument which must be evaluated.
11035 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11036 COMPOUND_EXPR in the chain will contain the tree for the simplified
11037 form of the builtin function call. */
11039 static tree
11040 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11042 if (!validate_arg (s1, POINTER_TYPE)
11043 || !validate_arg (s2, POINTER_TYPE))
11044 return NULL_TREE;
11045 else
11047 tree fn;
11048 const char *p1, *p2;
11050 p2 = c_getstr (s2);
11051 if (p2 == NULL)
11052 return NULL_TREE;
11054 p1 = c_getstr (s1);
11055 if (p1 != NULL)
11057 const char *r = strpbrk (p1, p2);
11058 tree tem;
11060 if (r == NULL)
11061 return build_int_cst (TREE_TYPE (s1), 0);
11063 /* Return an offset into the constant string argument. */
11064 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11065 return fold_convert_loc (loc, type, tem);
11068 if (p2[0] == '\0')
11069 /* strpbrk(x, "") == NULL.
11070 Evaluate and ignore s1 in case it had side-effects. */
11071 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11073 if (p2[1] != '\0')
11074 return NULL_TREE; /* Really call strpbrk. */
11076 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11077 if (!fn)
11078 return NULL_TREE;
11080 /* New argument list transforming strpbrk(s1, s2) to
11081 strchr(s1, s2[0]). */
11082 return build_call_expr_loc (loc, fn, 2, s1,
11083 build_int_cst (integer_type_node, p2[0]));
11087 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11088 to the call.
11090 Return NULL_TREE if no simplification was possible, otherwise return the
11091 simplified form of the call as a tree.
11093 The simplified form may be a constant or other expression which
11094 computes the same value, but in a more efficient manner (including
11095 calls to other builtin functions).
11097 The call may contain arguments which need to be evaluated, but
11098 which are not useful to determine the result of the call. In
11099 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11100 COMPOUND_EXPR will be an argument which must be evaluated.
11101 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11102 COMPOUND_EXPR in the chain will contain the tree for the simplified
11103 form of the builtin function call. */
11105 static tree
11106 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11108 if (!validate_arg (s1, POINTER_TYPE)
11109 || !validate_arg (s2, POINTER_TYPE))
11110 return NULL_TREE;
11111 else
11113 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11115 /* If both arguments are constants, evaluate at compile-time. */
11116 if (p1 && p2)
11118 const size_t r = strspn (p1, p2);
11119 return build_int_cst (size_type_node, r);
11122 /* If either argument is "", return NULL_TREE. */
11123 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11124 /* Evaluate and ignore both arguments in case either one has
11125 side-effects. */
11126 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11127 s1, s2);
11128 return NULL_TREE;
11132 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11133 to the call.
11135 Return NULL_TREE if no simplification was possible, otherwise return the
11136 simplified form of the call as a tree.
11138 The simplified form may be a constant or other expression which
11139 computes the same value, but in a more efficient manner (including
11140 calls to other builtin functions).
11142 The call may contain arguments which need to be evaluated, but
11143 which are not useful to determine the result of the call. In
11144 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11145 COMPOUND_EXPR will be an argument which must be evaluated.
11146 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11147 COMPOUND_EXPR in the chain will contain the tree for the simplified
11148 form of the builtin function call. */
11150 static tree
11151 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11153 if (!validate_arg (s1, POINTER_TYPE)
11154 || !validate_arg (s2, POINTER_TYPE))
11155 return NULL_TREE;
11156 else
11158 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11160 /* If both arguments are constants, evaluate at compile-time. */
11161 if (p1 && p2)
11163 const size_t r = strcspn (p1, p2);
11164 return build_int_cst (size_type_node, r);
11167 /* If the first argument is "", return NULL_TREE. */
11168 if (p1 && *p1 == '\0')
11170 /* Evaluate and ignore argument s2 in case it has
11171 side-effects. */
11172 return omit_one_operand_loc (loc, size_type_node,
11173 size_zero_node, s2);
11176 /* If the second argument is "", return __builtin_strlen(s1). */
11177 if (p2 && *p2 == '\0')
11179 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11181 /* If the replacement _DECL isn't initialized, don't do the
11182 transformation. */
11183 if (!fn)
11184 return NULL_TREE;
11186 return build_call_expr_loc (loc, fn, 1, s1);
11188 return NULL_TREE;
11192 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11193 produced. False otherwise. This is done so that we don't output the error
11194 or warning twice or three times. */
11196 bool
11197 fold_builtin_next_arg (tree exp, bool va_start_p)
11199 tree fntype = TREE_TYPE (current_function_decl);
11200 int nargs = call_expr_nargs (exp);
11201 tree arg;
11202 /* There is good chance the current input_location points inside the
11203 definition of the va_start macro (perhaps on the token for
11204 builtin) in a system header, so warnings will not be emitted.
11205 Use the location in real source code. */
11206 source_location current_location =
11207 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
11208 NULL);
11210 if (!stdarg_p (fntype))
11212 error ("%<va_start%> used in function with fixed args");
11213 return true;
11216 if (va_start_p)
11218 if (va_start_p && (nargs != 2))
11220 error ("wrong number of arguments to function %<va_start%>");
11221 return true;
11223 arg = CALL_EXPR_ARG (exp, 1);
11225 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11226 when we checked the arguments and if needed issued a warning. */
11227 else
11229 if (nargs == 0)
11231 /* Evidently an out of date version of <stdarg.h>; can't validate
11232 va_start's second argument, but can still work as intended. */
11233 warning_at (current_location,
11234 OPT_Wvarargs,
11235 "%<__builtin_next_arg%> called without an argument");
11236 return true;
11238 else if (nargs > 1)
11240 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11241 return true;
11243 arg = CALL_EXPR_ARG (exp, 0);
11246 if (TREE_CODE (arg) == SSA_NAME)
11247 arg = SSA_NAME_VAR (arg);
11249 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11250 or __builtin_next_arg (0) the first time we see it, after checking
11251 the arguments and if needed issuing a warning. */
11252 if (!integer_zerop (arg))
11254 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11256 /* Strip off all nops for the sake of the comparison. This
11257 is not quite the same as STRIP_NOPS. It does more.
11258 We must also strip off INDIRECT_EXPR for C++ reference
11259 parameters. */
11260 while (CONVERT_EXPR_P (arg)
11261 || TREE_CODE (arg) == INDIRECT_REF)
11262 arg = TREE_OPERAND (arg, 0);
11263 if (arg != last_parm)
11265 /* FIXME: Sometimes with the tree optimizers we can get the
11266 not the last argument even though the user used the last
11267 argument. We just warn and set the arg to be the last
11268 argument so that we will get wrong-code because of
11269 it. */
11270 warning_at (current_location,
11271 OPT_Wvarargs,
11272 "second parameter of %<va_start%> not last named argument");
11275 /* Undefined by C99 7.15.1.4p4 (va_start):
11276 "If the parameter parmN is declared with the register storage
11277 class, with a function or array type, or with a type that is
11278 not compatible with the type that results after application of
11279 the default argument promotions, the behavior is undefined."
11281 else if (DECL_REGISTER (arg))
11283 warning_at (current_location,
11284 OPT_Wvarargs,
11285 "undefined behaviour when second parameter of "
11286 "%<va_start%> is declared with %<register%> storage");
11289 /* We want to verify the second parameter just once before the tree
11290 optimizers are run and then avoid keeping it in the tree,
11291 as otherwise we could warn even for correct code like:
11292 void foo (int i, ...)
11293 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11294 if (va_start_p)
11295 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11296 else
11297 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11299 return false;
11303 /* Expand a call EXP to __builtin_object_size. */
11305 static rtx
11306 expand_builtin_object_size (tree exp)
11308 tree ost;
11309 int object_size_type;
11310 tree fndecl = get_callee_fndecl (exp);
11312 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11314 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11315 exp, fndecl);
11316 expand_builtin_trap ();
11317 return const0_rtx;
11320 ost = CALL_EXPR_ARG (exp, 1);
11321 STRIP_NOPS (ost);
11323 if (TREE_CODE (ost) != INTEGER_CST
11324 || tree_int_cst_sgn (ost) < 0
11325 || compare_tree_int (ost, 3) > 0)
11327 error ("%Klast argument of %D is not integer constant between 0 and 3",
11328 exp, fndecl);
11329 expand_builtin_trap ();
11330 return const0_rtx;
11333 object_size_type = tree_to_shwi (ost);
11335 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11338 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11339 FCODE is the BUILT_IN_* to use.
11340 Return NULL_RTX if we failed; the caller should emit a normal call,
11341 otherwise try to get the result in TARGET, if convenient (and in
11342 mode MODE if that's convenient). */
11344 static rtx
11345 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11346 enum built_in_function fcode)
11348 tree dest, src, len, size;
11350 if (!validate_arglist (exp,
11351 POINTER_TYPE,
11352 fcode == BUILT_IN_MEMSET_CHK
11353 ? INTEGER_TYPE : POINTER_TYPE,
11354 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11355 return NULL_RTX;
11357 dest = CALL_EXPR_ARG (exp, 0);
11358 src = CALL_EXPR_ARG (exp, 1);
11359 len = CALL_EXPR_ARG (exp, 2);
11360 size = CALL_EXPR_ARG (exp, 3);
11362 if (! tree_fits_uhwi_p (size))
11363 return NULL_RTX;
11365 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11367 tree fn;
11369 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11371 warning_at (tree_nonartificial_location (exp),
11372 0, "%Kcall to %D will always overflow destination buffer",
11373 exp, get_callee_fndecl (exp));
11374 return NULL_RTX;
11377 fn = NULL_TREE;
11378 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11379 mem{cpy,pcpy,move,set} is available. */
11380 switch (fcode)
11382 case BUILT_IN_MEMCPY_CHK:
11383 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11384 break;
11385 case BUILT_IN_MEMPCPY_CHK:
11386 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11387 break;
11388 case BUILT_IN_MEMMOVE_CHK:
11389 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11390 break;
11391 case BUILT_IN_MEMSET_CHK:
11392 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11393 break;
11394 default:
11395 break;
11398 if (! fn)
11399 return NULL_RTX;
11401 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11402 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11403 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11404 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11406 else if (fcode == BUILT_IN_MEMSET_CHK)
11407 return NULL_RTX;
11408 else
11410 unsigned int dest_align = get_pointer_alignment (dest);
11412 /* If DEST is not a pointer type, call the normal function. */
11413 if (dest_align == 0)
11414 return NULL_RTX;
11416 /* If SRC and DEST are the same (and not volatile), do nothing. */
11417 if (operand_equal_p (src, dest, 0))
11419 tree expr;
11421 if (fcode != BUILT_IN_MEMPCPY_CHK)
11423 /* Evaluate and ignore LEN in case it has side-effects. */
11424 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11425 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11428 expr = fold_build_pointer_plus (dest, len);
11429 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11432 /* __memmove_chk special case. */
11433 if (fcode == BUILT_IN_MEMMOVE_CHK)
11435 unsigned int src_align = get_pointer_alignment (src);
11437 if (src_align == 0)
11438 return NULL_RTX;
11440 /* If src is categorized for a readonly section we can use
11441 normal __memcpy_chk. */
11442 if (readonly_data_expr (src))
11444 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11445 if (!fn)
11446 return NULL_RTX;
11447 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11448 dest, src, len, size);
11449 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11450 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11451 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11454 return NULL_RTX;
11458 /* Emit warning if a buffer overflow is detected at compile time. */
11460 static void
11461 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11463 int is_strlen = 0;
11464 tree len, size;
11465 location_t loc = tree_nonartificial_location (exp);
11467 switch (fcode)
11469 case BUILT_IN_STRCPY_CHK:
11470 case BUILT_IN_STPCPY_CHK:
11471 /* For __strcat_chk the warning will be emitted only if overflowing
11472 by at least strlen (dest) + 1 bytes. */
11473 case BUILT_IN_STRCAT_CHK:
11474 len = CALL_EXPR_ARG (exp, 1);
11475 size = CALL_EXPR_ARG (exp, 2);
11476 is_strlen = 1;
11477 break;
11478 case BUILT_IN_STRNCAT_CHK:
11479 case BUILT_IN_STRNCPY_CHK:
11480 case BUILT_IN_STPNCPY_CHK:
11481 len = CALL_EXPR_ARG (exp, 2);
11482 size = CALL_EXPR_ARG (exp, 3);
11483 break;
11484 case BUILT_IN_SNPRINTF_CHK:
11485 case BUILT_IN_VSNPRINTF_CHK:
11486 len = CALL_EXPR_ARG (exp, 1);
11487 size = CALL_EXPR_ARG (exp, 3);
11488 break;
11489 default:
11490 gcc_unreachable ();
11493 if (!len || !size)
11494 return;
11496 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11497 return;
11499 if (is_strlen)
11501 len = c_strlen (len, 1);
11502 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11503 return;
11505 else if (fcode == BUILT_IN_STRNCAT_CHK)
11507 tree src = CALL_EXPR_ARG (exp, 1);
11508 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11509 return;
11510 src = c_strlen (src, 1);
11511 if (! src || ! tree_fits_uhwi_p (src))
11513 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
11514 exp, get_callee_fndecl (exp));
11515 return;
11517 else if (tree_int_cst_lt (src, size))
11518 return;
11520 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
11521 return;
11523 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
11524 exp, get_callee_fndecl (exp));
11527 /* Emit warning if a buffer overflow is detected at compile time
11528 in __sprintf_chk/__vsprintf_chk calls. */
11530 static void
11531 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11533 tree size, len, fmt;
11534 const char *fmt_str;
11535 int nargs = call_expr_nargs (exp);
11537 /* Verify the required arguments in the original call. */
11539 if (nargs < 4)
11540 return;
11541 size = CALL_EXPR_ARG (exp, 2);
11542 fmt = CALL_EXPR_ARG (exp, 3);
11544 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11545 return;
11547 /* Check whether the format is a literal string constant. */
11548 fmt_str = c_getstr (fmt);
11549 if (fmt_str == NULL)
11550 return;
11552 if (!init_target_chars ())
11553 return;
11555 /* If the format doesn't contain % args or %%, we know its size. */
11556 if (strchr (fmt_str, target_percent) == 0)
11557 len = build_int_cstu (size_type_node, strlen (fmt_str));
11558 /* If the format is "%s" and first ... argument is a string literal,
11559 we know it too. */
11560 else if (fcode == BUILT_IN_SPRINTF_CHK
11561 && strcmp (fmt_str, target_percent_s) == 0)
11563 tree arg;
11565 if (nargs < 5)
11566 return;
11567 arg = CALL_EXPR_ARG (exp, 4);
11568 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11569 return;
11571 len = c_strlen (arg, 1);
11572 if (!len || ! tree_fits_uhwi_p (len))
11573 return;
11575 else
11576 return;
11578 if (! tree_int_cst_lt (len, size))
11579 warning_at (tree_nonartificial_location (exp),
11580 0, "%Kcall to %D will always overflow destination buffer",
11581 exp, get_callee_fndecl (exp));
11584 /* Emit warning if a free is called with address of a variable. */
11586 static void
11587 maybe_emit_free_warning (tree exp)
11589 tree arg = CALL_EXPR_ARG (exp, 0);
11591 STRIP_NOPS (arg);
11592 if (TREE_CODE (arg) != ADDR_EXPR)
11593 return;
11595 arg = get_base_address (TREE_OPERAND (arg, 0));
11596 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
11597 return;
11599 if (SSA_VAR_P (arg))
11600 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11601 "%Kattempt to free a non-heap object %qD", exp, arg);
11602 else
11603 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11604 "%Kattempt to free a non-heap object", exp);
11607 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11608 if possible. */
11610 static tree
11611 fold_builtin_object_size (tree ptr, tree ost)
11613 unsigned HOST_WIDE_INT bytes;
11614 int object_size_type;
11616 if (!validate_arg (ptr, POINTER_TYPE)
11617 || !validate_arg (ost, INTEGER_TYPE))
11618 return NULL_TREE;
11620 STRIP_NOPS (ost);
11622 if (TREE_CODE (ost) != INTEGER_CST
11623 || tree_int_cst_sgn (ost) < 0
11624 || compare_tree_int (ost, 3) > 0)
11625 return NULL_TREE;
11627 object_size_type = tree_to_shwi (ost);
11629 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11630 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11631 and (size_t) 0 for types 2 and 3. */
11632 if (TREE_SIDE_EFFECTS (ptr))
11633 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11635 if (TREE_CODE (ptr) == ADDR_EXPR)
11637 bytes = compute_builtin_object_size (ptr, object_size_type);
11638 if (wi::fits_to_tree_p (bytes, size_type_node))
11639 return build_int_cstu (size_type_node, bytes);
11641 else if (TREE_CODE (ptr) == SSA_NAME)
11643 /* If object size is not known yet, delay folding until
11644 later. Maybe subsequent passes will help determining
11645 it. */
11646 bytes = compute_builtin_object_size (ptr, object_size_type);
11647 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
11648 && wi::fits_to_tree_p (bytes, size_type_node))
11649 return build_int_cstu (size_type_node, bytes);
11652 return NULL_TREE;
11655 /* Builtins with folding operations that operate on "..." arguments
11656 need special handling; we need to store the arguments in a convenient
11657 data structure before attempting any folding. Fortunately there are
11658 only a few builtins that fall into this category. FNDECL is the
11659 function, EXP is the CALL_EXPR for the call. */
11661 static tree
11662 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
11664 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11665 tree ret = NULL_TREE;
11667 switch (fcode)
11669 case BUILT_IN_FPCLASSIFY:
11670 ret = fold_builtin_fpclassify (loc, args, nargs);
11671 break;
11673 default:
11674 break;
11676 if (ret)
11678 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11679 SET_EXPR_LOCATION (ret, loc);
11680 TREE_NO_WARNING (ret) = 1;
11681 return ret;
11683 return NULL_TREE;
11686 /* Initialize format string characters in the target charset. */
11688 bool
11689 init_target_chars (void)
11691 static bool init;
11692 if (!init)
11694 target_newline = lang_hooks.to_target_charset ('\n');
11695 target_percent = lang_hooks.to_target_charset ('%');
11696 target_c = lang_hooks.to_target_charset ('c');
11697 target_s = lang_hooks.to_target_charset ('s');
11698 if (target_newline == 0 || target_percent == 0 || target_c == 0
11699 || target_s == 0)
11700 return false;
11702 target_percent_c[0] = target_percent;
11703 target_percent_c[1] = target_c;
11704 target_percent_c[2] = '\0';
11706 target_percent_s[0] = target_percent;
11707 target_percent_s[1] = target_s;
11708 target_percent_s[2] = '\0';
11710 target_percent_s_newline[0] = target_percent;
11711 target_percent_s_newline[1] = target_s;
11712 target_percent_s_newline[2] = target_newline;
11713 target_percent_s_newline[3] = '\0';
11715 init = true;
11717 return true;
11720 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11721 and no overflow/underflow occurred. INEXACT is true if M was not
11722 exactly calculated. TYPE is the tree type for the result. This
11723 function assumes that you cleared the MPFR flags and then
11724 calculated M to see if anything subsequently set a flag prior to
11725 entering this function. Return NULL_TREE if any checks fail. */
11727 static tree
11728 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
11730 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11731 overflow/underflow occurred. If -frounding-math, proceed iff the
11732 result of calling FUNC was exact. */
11733 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11734 && (!flag_rounding_math || !inexact))
11736 REAL_VALUE_TYPE rr;
11738 real_from_mpfr (&rr, m, type, GMP_RNDN);
11739 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11740 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11741 but the mpft_t is not, then we underflowed in the
11742 conversion. */
11743 if (real_isfinite (&rr)
11744 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11746 REAL_VALUE_TYPE rmode;
11748 real_convert (&rmode, TYPE_MODE (type), &rr);
11749 /* Proceed iff the specified mode can hold the value. */
11750 if (real_identical (&rmode, &rr))
11751 return build_real (type, rmode);
11754 return NULL_TREE;
11757 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
11758 number and no overflow/underflow occurred. INEXACT is true if M
11759 was not exactly calculated. TYPE is the tree type for the result.
11760 This function assumes that you cleared the MPFR flags and then
11761 calculated M to see if anything subsequently set a flag prior to
11762 entering this function. Return NULL_TREE if any checks fail, if
11763 FORCE_CONVERT is true, then bypass the checks. */
11765 static tree
11766 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
11768 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11769 overflow/underflow occurred. If -frounding-math, proceed iff the
11770 result of calling FUNC was exact. */
11771 if (force_convert
11772 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
11773 && !mpfr_overflow_p () && !mpfr_underflow_p ()
11774 && (!flag_rounding_math || !inexact)))
11776 REAL_VALUE_TYPE re, im;
11778 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
11779 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
11780 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11781 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11782 but the mpft_t is not, then we underflowed in the
11783 conversion. */
11784 if (force_convert
11785 || (real_isfinite (&re) && real_isfinite (&im)
11786 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
11787 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
11789 REAL_VALUE_TYPE re_mode, im_mode;
11791 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
11792 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
11793 /* Proceed iff the specified mode can hold the value. */
11794 if (force_convert
11795 || (real_identical (&re_mode, &re)
11796 && real_identical (&im_mode, &im)))
11797 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
11798 build_real (TREE_TYPE (type), im_mode));
11801 return NULL_TREE;
11804 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11805 FUNC on it and return the resulting value as a tree with type TYPE.
11806 If MIN and/or MAX are not NULL, then the supplied ARG must be
11807 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11808 acceptable values, otherwise they are not. The mpfr precision is
11809 set to the precision of TYPE. We assume that function FUNC returns
11810 zero if the result could be calculated exactly within the requested
11811 precision. */
11813 static tree
11814 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11815 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11816 bool inclusive)
11818 tree result = NULL_TREE;
11820 STRIP_NOPS (arg);
11822 /* To proceed, MPFR must exactly represent the target floating point
11823 format, which only happens when the target base equals two. */
11824 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11825 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
11827 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11829 if (real_isfinite (ra)
11830 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
11831 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
11833 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11834 const int prec = fmt->p;
11835 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11836 int inexact;
11837 mpfr_t m;
11839 mpfr_init2 (m, prec);
11840 mpfr_from_real (m, ra, GMP_RNDN);
11841 mpfr_clear_flags ();
11842 inexact = func (m, m, rnd);
11843 result = do_mpfr_ckconv (m, type, inexact);
11844 mpfr_clear (m);
11848 return result;
11851 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11852 FUNC on it and return the resulting value as a tree with type TYPE.
11853 The mpfr precision is set to the precision of TYPE. We assume that
11854 function FUNC returns zero if the result could be calculated
11855 exactly within the requested precision. */
11857 static tree
11858 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
11859 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11861 tree result = NULL_TREE;
11863 STRIP_NOPS (arg1);
11864 STRIP_NOPS (arg2);
11866 /* To proceed, MPFR must exactly represent the target floating point
11867 format, which only happens when the target base equals two. */
11868 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11869 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11870 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
11872 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11873 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11875 if (real_isfinite (ra1) && real_isfinite (ra2))
11877 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11878 const int prec = fmt->p;
11879 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11880 int inexact;
11881 mpfr_t m1, m2;
11883 mpfr_inits2 (prec, m1, m2, NULL);
11884 mpfr_from_real (m1, ra1, GMP_RNDN);
11885 mpfr_from_real (m2, ra2, GMP_RNDN);
11886 mpfr_clear_flags ();
11887 inexact = func (m1, m1, m2, rnd);
11888 result = do_mpfr_ckconv (m1, type, inexact);
11889 mpfr_clears (m1, m2, NULL);
11893 return result;
11896 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
11897 FUNC on it and return the resulting value as a tree with type TYPE.
11898 The mpfr precision is set to the precision of TYPE. We assume that
11899 function FUNC returns zero if the result could be calculated
11900 exactly within the requested precision. */
11902 static tree
11903 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
11904 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11906 tree result = NULL_TREE;
11908 STRIP_NOPS (arg1);
11909 STRIP_NOPS (arg2);
11910 STRIP_NOPS (arg3);
11912 /* To proceed, MPFR must exactly represent the target floating point
11913 format, which only happens when the target base equals two. */
11914 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11915 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11916 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
11917 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
11919 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11920 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11921 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
11923 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
11925 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11926 const int prec = fmt->p;
11927 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11928 int inexact;
11929 mpfr_t m1, m2, m3;
11931 mpfr_inits2 (prec, m1, m2, m3, NULL);
11932 mpfr_from_real (m1, ra1, GMP_RNDN);
11933 mpfr_from_real (m2, ra2, GMP_RNDN);
11934 mpfr_from_real (m3, ra3, GMP_RNDN);
11935 mpfr_clear_flags ();
11936 inexact = func (m1, m1, m2, m3, rnd);
11937 result = do_mpfr_ckconv (m1, type, inexact);
11938 mpfr_clears (m1, m2, m3, NULL);
11942 return result;
11945 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11946 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11947 If ARG_SINP and ARG_COSP are NULL then the result is returned
11948 as a complex value.
11949 The type is taken from the type of ARG and is used for setting the
11950 precision of the calculation and results. */
11952 static tree
11953 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
11955 tree const type = TREE_TYPE (arg);
11956 tree result = NULL_TREE;
11958 STRIP_NOPS (arg);
11960 /* To proceed, MPFR must exactly represent the target floating point
11961 format, which only happens when the target base equals two. */
11962 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11963 && TREE_CODE (arg) == REAL_CST
11964 && !TREE_OVERFLOW (arg))
11966 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11968 if (real_isfinite (ra))
11970 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11971 const int prec = fmt->p;
11972 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11973 tree result_s, result_c;
11974 int inexact;
11975 mpfr_t m, ms, mc;
11977 mpfr_inits2 (prec, m, ms, mc, NULL);
11978 mpfr_from_real (m, ra, GMP_RNDN);
11979 mpfr_clear_flags ();
11980 inexact = mpfr_sin_cos (ms, mc, m, rnd);
11981 result_s = do_mpfr_ckconv (ms, type, inexact);
11982 result_c = do_mpfr_ckconv (mc, type, inexact);
11983 mpfr_clears (m, ms, mc, NULL);
11984 if (result_s && result_c)
11986 /* If we are to return in a complex value do so. */
11987 if (!arg_sinp && !arg_cosp)
11988 return build_complex (build_complex_type (type),
11989 result_c, result_s);
11991 /* Dereference the sin/cos pointer arguments. */
11992 arg_sinp = build_fold_indirect_ref (arg_sinp);
11993 arg_cosp = build_fold_indirect_ref (arg_cosp);
11994 /* Proceed if valid pointer type were passed in. */
11995 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
11996 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
11998 /* Set the values. */
11999 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
12000 result_s);
12001 TREE_SIDE_EFFECTS (result_s) = 1;
12002 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
12003 result_c);
12004 TREE_SIDE_EFFECTS (result_c) = 1;
12005 /* Combine the assignments into a compound expr. */
12006 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12007 result_s, result_c));
12012 return result;
12015 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
12016 two-argument mpfr order N Bessel function FUNC on them and return
12017 the resulting value as a tree with type TYPE. The mpfr precision
12018 is set to the precision of TYPE. We assume that function FUNC
12019 returns zero if the result could be calculated exactly within the
12020 requested precision. */
12021 static tree
12022 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
12023 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
12024 const REAL_VALUE_TYPE *min, bool inclusive)
12026 tree result = NULL_TREE;
12028 STRIP_NOPS (arg1);
12029 STRIP_NOPS (arg2);
12031 /* To proceed, MPFR must exactly represent the target floating point
12032 format, which only happens when the target base equals two. */
12033 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12034 && tree_fits_shwi_p (arg1)
12035 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12037 const HOST_WIDE_INT n = tree_to_shwi (arg1);
12038 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
12040 if (n == (long)n
12041 && real_isfinite (ra)
12042 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
12044 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12045 const int prec = fmt->p;
12046 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12047 int inexact;
12048 mpfr_t m;
12050 mpfr_init2 (m, prec);
12051 mpfr_from_real (m, ra, GMP_RNDN);
12052 mpfr_clear_flags ();
12053 inexact = func (m, n, m, rnd);
12054 result = do_mpfr_ckconv (m, type, inexact);
12055 mpfr_clear (m);
12059 return result;
12062 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12063 the pointer *(ARG_QUO) and return the result. The type is taken
12064 from the type of ARG0 and is used for setting the precision of the
12065 calculation and results. */
12067 static tree
12068 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
12070 tree const type = TREE_TYPE (arg0);
12071 tree result = NULL_TREE;
12073 STRIP_NOPS (arg0);
12074 STRIP_NOPS (arg1);
12076 /* To proceed, MPFR must exactly represent the target floating point
12077 format, which only happens when the target base equals two. */
12078 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12079 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
12080 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
12082 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
12083 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
12085 if (real_isfinite (ra0) && real_isfinite (ra1))
12087 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12088 const int prec = fmt->p;
12089 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12090 tree result_rem;
12091 long integer_quo;
12092 mpfr_t m0, m1;
12094 mpfr_inits2 (prec, m0, m1, NULL);
12095 mpfr_from_real (m0, ra0, GMP_RNDN);
12096 mpfr_from_real (m1, ra1, GMP_RNDN);
12097 mpfr_clear_flags ();
12098 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
12099 /* Remquo is independent of the rounding mode, so pass
12100 inexact=0 to do_mpfr_ckconv(). */
12101 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
12102 mpfr_clears (m0, m1, NULL);
12103 if (result_rem)
12105 /* MPFR calculates quo in the host's long so it may
12106 return more bits in quo than the target int can hold
12107 if sizeof(host long) > sizeof(target int). This can
12108 happen even for native compilers in LP64 mode. In
12109 these cases, modulo the quo value with the largest
12110 number that the target int can hold while leaving one
12111 bit for the sign. */
12112 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
12113 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
12115 /* Dereference the quo pointer argument. */
12116 arg_quo = build_fold_indirect_ref (arg_quo);
12117 /* Proceed iff a valid pointer type was passed in. */
12118 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
12120 /* Set the value. */
12121 tree result_quo
12122 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
12123 build_int_cst (TREE_TYPE (arg_quo),
12124 integer_quo));
12125 TREE_SIDE_EFFECTS (result_quo) = 1;
12126 /* Combine the quo assignment with the rem. */
12127 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12128 result_quo, result_rem));
12133 return result;
12136 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12137 resulting value as a tree with type TYPE. The mpfr precision is
12138 set to the precision of TYPE. We assume that this mpfr function
12139 returns zero if the result could be calculated exactly within the
12140 requested precision. In addition, the integer pointer represented
12141 by ARG_SG will be dereferenced and set to the appropriate signgam
12142 (-1,1) value. */
12144 static tree
12145 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
12147 tree result = NULL_TREE;
12149 STRIP_NOPS (arg);
12151 /* To proceed, MPFR must exactly represent the target floating point
12152 format, which only happens when the target base equals two. Also
12153 verify ARG is a constant and that ARG_SG is an int pointer. */
12154 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12155 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
12156 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
12157 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
12159 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
12161 /* In addition to NaN and Inf, the argument cannot be zero or a
12162 negative integer. */
12163 if (real_isfinite (ra)
12164 && ra->cl != rvc_zero
12165 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
12167 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12168 const int prec = fmt->p;
12169 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12170 int inexact, sg;
12171 mpfr_t m;
12172 tree result_lg;
12174 mpfr_init2 (m, prec);
12175 mpfr_from_real (m, ra, GMP_RNDN);
12176 mpfr_clear_flags ();
12177 inexact = mpfr_lgamma (m, &sg, m, rnd);
12178 result_lg = do_mpfr_ckconv (m, type, inexact);
12179 mpfr_clear (m);
12180 if (result_lg)
12182 tree result_sg;
12184 /* Dereference the arg_sg pointer argument. */
12185 arg_sg = build_fold_indirect_ref (arg_sg);
12186 /* Assign the signgam value into *arg_sg. */
12187 result_sg = fold_build2 (MODIFY_EXPR,
12188 TREE_TYPE (arg_sg), arg_sg,
12189 build_int_cst (TREE_TYPE (arg_sg), sg));
12190 TREE_SIDE_EFFECTS (result_sg) = 1;
12191 /* Combine the signgam assignment with the lgamma result. */
12192 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
12193 result_sg, result_lg));
12198 return result;
12201 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
12202 function FUNC on it and return the resulting value as a tree with
12203 type TYPE. The mpfr precision is set to the precision of TYPE. We
12204 assume that function FUNC returns zero if the result could be
12205 calculated exactly within the requested precision. */
12207 static tree
12208 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
12210 tree result = NULL_TREE;
12212 STRIP_NOPS (arg);
12214 /* To proceed, MPFR must exactly represent the target floating point
12215 format, which only happens when the target base equals two. */
12216 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
12217 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
12218 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
12220 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
12221 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
12223 if (real_isfinite (re) && real_isfinite (im))
12225 const struct real_format *const fmt =
12226 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12227 const int prec = fmt->p;
12228 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12229 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12230 int inexact;
12231 mpc_t m;
12233 mpc_init2 (m, prec);
12234 mpfr_from_real (mpc_realref (m), re, rnd);
12235 mpfr_from_real (mpc_imagref (m), im, rnd);
12236 mpfr_clear_flags ();
12237 inexact = func (m, m, crnd);
12238 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
12239 mpc_clear (m);
12243 return result;
12246 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12247 mpc function FUNC on it and return the resulting value as a tree
12248 with type TYPE. The mpfr precision is set to the precision of
12249 TYPE. We assume that function FUNC returns zero if the result
12250 could be calculated exactly within the requested precision. If
12251 DO_NONFINITE is true, then fold expressions containing Inf or NaN
12252 in the arguments and/or results. */
12254 tree
12255 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
12256 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
12258 tree result = NULL_TREE;
12260 STRIP_NOPS (arg0);
12261 STRIP_NOPS (arg1);
12263 /* To proceed, MPFR must exactly represent the target floating point
12264 format, which only happens when the target base equals two. */
12265 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12266 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
12267 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12268 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
12269 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12271 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12272 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12273 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12274 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12276 if (do_nonfinite
12277 || (real_isfinite (re0) && real_isfinite (im0)
12278 && real_isfinite (re1) && real_isfinite (im1)))
12280 const struct real_format *const fmt =
12281 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12282 const int prec = fmt->p;
12283 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12284 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12285 int inexact;
12286 mpc_t m0, m1;
12288 mpc_init2 (m0, prec);
12289 mpc_init2 (m1, prec);
12290 mpfr_from_real (mpc_realref (m0), re0, rnd);
12291 mpfr_from_real (mpc_imagref (m0), im0, rnd);
12292 mpfr_from_real (mpc_realref (m1), re1, rnd);
12293 mpfr_from_real (mpc_imagref (m1), im1, rnd);
12294 mpfr_clear_flags ();
12295 inexact = func (m0, m0, m1, crnd);
12296 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12297 mpc_clear (m0);
12298 mpc_clear (m1);
12302 return result;
12305 /* A wrapper function for builtin folding that prevents warnings for
12306 "statement without effect" and the like, caused by removing the
12307 call node earlier than the warning is generated. */
12309 tree
12310 fold_call_stmt (gcall *stmt, bool ignore)
12312 tree ret = NULL_TREE;
12313 tree fndecl = gimple_call_fndecl (stmt);
12314 location_t loc = gimple_location (stmt);
12315 if (fndecl
12316 && TREE_CODE (fndecl) == FUNCTION_DECL
12317 && DECL_BUILT_IN (fndecl)
12318 && !gimple_call_va_arg_pack_p (stmt))
12320 int nargs = gimple_call_num_args (stmt);
12321 tree *args = (nargs > 0
12322 ? gimple_call_arg_ptr (stmt, 0)
12323 : &error_mark_node);
12325 if (avoid_folding_inline_builtin (fndecl))
12326 return NULL_TREE;
12327 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12329 return targetm.fold_builtin (fndecl, nargs, args, ignore);
12331 else
12333 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
12334 if (ret)
12336 /* Propagate location information from original call to
12337 expansion of builtin. Otherwise things like
12338 maybe_emit_chk_warning, that operate on the expansion
12339 of a builtin, will use the wrong location information. */
12340 if (gimple_has_location (stmt))
12342 tree realret = ret;
12343 if (TREE_CODE (ret) == NOP_EXPR)
12344 realret = TREE_OPERAND (ret, 0);
12345 if (CAN_HAVE_LOCATION_P (realret)
12346 && !EXPR_HAS_LOCATION (realret))
12347 SET_EXPR_LOCATION (realret, loc);
12348 return realret;
12350 return ret;
12354 return NULL_TREE;
12357 /* Look up the function in builtin_decl that corresponds to DECL
12358 and set ASMSPEC as its user assembler name. DECL must be a
12359 function decl that declares a builtin. */
12361 void
12362 set_builtin_user_assembler_name (tree decl, const char *asmspec)
12364 tree builtin;
12365 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
12366 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
12367 && asmspec != 0);
12369 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12370 set_user_assembler_name (builtin, asmspec);
12371 switch (DECL_FUNCTION_CODE (decl))
12373 case BUILT_IN_MEMCPY:
12374 init_block_move_fn (asmspec);
12375 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
12376 break;
12377 case BUILT_IN_MEMSET:
12378 init_block_clear_fn (asmspec);
12379 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
12380 break;
12381 case BUILT_IN_MEMMOVE:
12382 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
12383 break;
12384 case BUILT_IN_MEMCMP:
12385 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
12386 break;
12387 case BUILT_IN_ABORT:
12388 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
12389 break;
12390 case BUILT_IN_FFS:
12391 if (INT_TYPE_SIZE < BITS_PER_WORD)
12393 set_user_assembler_libfunc ("ffs", asmspec);
12394 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
12395 MODE_INT, 0), "ffs");
12397 break;
12398 default:
12399 break;
12403 /* Return true if DECL is a builtin that expands to a constant or similarly
12404 simple code. */
12405 bool
12406 is_simple_builtin (tree decl)
12408 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12409 switch (DECL_FUNCTION_CODE (decl))
12411 /* Builtins that expand to constants. */
12412 case BUILT_IN_CONSTANT_P:
12413 case BUILT_IN_EXPECT:
12414 case BUILT_IN_OBJECT_SIZE:
12415 case BUILT_IN_UNREACHABLE:
12416 /* Simple register moves or loads from stack. */
12417 case BUILT_IN_ASSUME_ALIGNED:
12418 case BUILT_IN_RETURN_ADDRESS:
12419 case BUILT_IN_EXTRACT_RETURN_ADDR:
12420 case BUILT_IN_FROB_RETURN_ADDR:
12421 case BUILT_IN_RETURN:
12422 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12423 case BUILT_IN_FRAME_ADDRESS:
12424 case BUILT_IN_VA_END:
12425 case BUILT_IN_STACK_SAVE:
12426 case BUILT_IN_STACK_RESTORE:
12427 /* Exception state returns or moves registers around. */
12428 case BUILT_IN_EH_FILTER:
12429 case BUILT_IN_EH_POINTER:
12430 case BUILT_IN_EH_COPY_VALUES:
12431 return true;
12433 default:
12434 return false;
12437 return false;
12440 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12441 most probably expanded inline into reasonably simple code. This is a
12442 superset of is_simple_builtin. */
12443 bool
12444 is_inexpensive_builtin (tree decl)
12446 if (!decl)
12447 return false;
12448 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12449 return true;
12450 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12451 switch (DECL_FUNCTION_CODE (decl))
12453 case BUILT_IN_ABS:
12454 case BUILT_IN_ALLOCA:
12455 case BUILT_IN_ALLOCA_WITH_ALIGN:
12456 case BUILT_IN_BSWAP16:
12457 case BUILT_IN_BSWAP32:
12458 case BUILT_IN_BSWAP64:
12459 case BUILT_IN_CLZ:
12460 case BUILT_IN_CLZIMAX:
12461 case BUILT_IN_CLZL:
12462 case BUILT_IN_CLZLL:
12463 case BUILT_IN_CTZ:
12464 case BUILT_IN_CTZIMAX:
12465 case BUILT_IN_CTZL:
12466 case BUILT_IN_CTZLL:
12467 case BUILT_IN_FFS:
12468 case BUILT_IN_FFSIMAX:
12469 case BUILT_IN_FFSL:
12470 case BUILT_IN_FFSLL:
12471 case BUILT_IN_IMAXABS:
12472 case BUILT_IN_FINITE:
12473 case BUILT_IN_FINITEF:
12474 case BUILT_IN_FINITEL:
12475 case BUILT_IN_FINITED32:
12476 case BUILT_IN_FINITED64:
12477 case BUILT_IN_FINITED128:
12478 case BUILT_IN_FPCLASSIFY:
12479 case BUILT_IN_ISFINITE:
12480 case BUILT_IN_ISINF_SIGN:
12481 case BUILT_IN_ISINF:
12482 case BUILT_IN_ISINFF:
12483 case BUILT_IN_ISINFL:
12484 case BUILT_IN_ISINFD32:
12485 case BUILT_IN_ISINFD64:
12486 case BUILT_IN_ISINFD128:
12487 case BUILT_IN_ISNAN:
12488 case BUILT_IN_ISNANF:
12489 case BUILT_IN_ISNANL:
12490 case BUILT_IN_ISNAND32:
12491 case BUILT_IN_ISNAND64:
12492 case BUILT_IN_ISNAND128:
12493 case BUILT_IN_ISNORMAL:
12494 case BUILT_IN_ISGREATER:
12495 case BUILT_IN_ISGREATEREQUAL:
12496 case BUILT_IN_ISLESS:
12497 case BUILT_IN_ISLESSEQUAL:
12498 case BUILT_IN_ISLESSGREATER:
12499 case BUILT_IN_ISUNORDERED:
12500 case BUILT_IN_VA_ARG_PACK:
12501 case BUILT_IN_VA_ARG_PACK_LEN:
12502 case BUILT_IN_VA_COPY:
12503 case BUILT_IN_TRAP:
12504 case BUILT_IN_SAVEREGS:
12505 case BUILT_IN_POPCOUNTL:
12506 case BUILT_IN_POPCOUNTLL:
12507 case BUILT_IN_POPCOUNTIMAX:
12508 case BUILT_IN_POPCOUNT:
12509 case BUILT_IN_PARITYL:
12510 case BUILT_IN_PARITYLL:
12511 case BUILT_IN_PARITYIMAX:
12512 case BUILT_IN_PARITY:
12513 case BUILT_IN_LABS:
12514 case BUILT_IN_LLABS:
12515 case BUILT_IN_PREFETCH:
12516 case BUILT_IN_ACC_ON_DEVICE:
12517 return true;
12519 default:
12520 return is_simple_builtin (decl);
12523 return false;