2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / builtins.c
blobb4ac53553fe47c1ba8b17e17e455b2f87efa191b
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 /* Legacy warning! Please add no further builtin simplifications here
21 (apart from pure constant folding) - builtin simplifications should go
22 to match.pd or gimple-fold.c instead. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "predict.h"
29 #include "tree.h"
30 #include "gimple.h"
31 #include "rtl.h"
32 #include "alias.h"
33 #include "fold-const.h"
34 #include "stringpool.h"
35 #include "stor-layout.h"
36 #include "calls.h"
37 #include "varasm.h"
38 #include "tree-object-size.h"
39 #include "realmpfr.h"
40 #include "cfgrtl.h"
41 #include "internal-fn.h"
42 #include "flags.h"
43 #include "regs.h"
44 #include "except.h"
45 #include "insn-config.h"
46 #include "expmed.h"
47 #include "dojump.h"
48 #include "explow.h"
49 #include "emit-rtl.h"
50 #include "stmt.h"
51 #include "expr.h"
52 #include "insn-codes.h"
53 #include "optabs.h"
54 #include "libfuncs.h"
55 #include "recog.h"
56 #include "output.h"
57 #include "typeclass.h"
58 #include "tm_p.h"
59 #include "target.h"
60 #include "langhooks.h"
61 #include "tree-ssanames.h"
62 #include "tree-dfa.h"
63 #include "value-prof.h"
64 #include "diagnostic-core.h"
65 #include "builtins.h"
66 #include "asan.h"
67 #include "cilk.h"
68 #include "cgraph.h"
69 #include "tree-chkp.h"
70 #include "rtl-chkp.h"
73 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
75 struct target_builtins default_target_builtins;
76 #if SWITCHABLE_TARGET
77 struct target_builtins *this_target_builtins = &default_target_builtins;
78 #endif
80 /* Define the names of the builtin function types and codes. */
81 const char *const built_in_class_names[BUILT_IN_LAST]
82 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
84 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
85 const char * built_in_names[(int) END_BUILTINS] =
87 #include "builtins.def"
89 #undef DEF_BUILTIN
91 /* Setup an array of builtin_info_type, make sure each element decl is
92 initialized to NULL_TREE. */
93 builtin_info_type builtin_info[(int)END_BUILTINS];
95 /* Non-zero if __builtin_constant_p should be folded right away. */
96 bool force_folding_builtin_constant_p;
98 static rtx c_readstr (const char *, machine_mode);
99 static int target_char_cast (tree, char *);
100 static rtx get_memory_rtx (tree, tree);
101 static int apply_args_size (void);
102 static int apply_result_size (void);
103 static rtx result_vector (int, rtx);
104 static void expand_builtin_prefetch (tree);
105 static rtx expand_builtin_apply_args (void);
106 static rtx expand_builtin_apply_args_1 (void);
107 static rtx expand_builtin_apply (rtx, rtx, rtx);
108 static void expand_builtin_return (rtx);
109 static enum type_class type_to_class (tree);
110 static rtx expand_builtin_classify_type (tree);
111 static void expand_errno_check (tree, rtx);
112 static rtx expand_builtin_mathfn (tree, rtx, rtx);
113 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
114 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
115 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
116 static rtx expand_builtin_interclass_mathfn (tree, rtx);
117 static rtx expand_builtin_sincos (tree);
118 static rtx expand_builtin_cexpi (tree, rtx);
119 static rtx expand_builtin_int_roundingfn (tree, rtx);
120 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
121 static rtx expand_builtin_next_arg (void);
122 static rtx expand_builtin_va_start (tree);
123 static rtx expand_builtin_va_end (tree);
124 static rtx expand_builtin_va_copy (tree);
125 static rtx expand_builtin_strcmp (tree, rtx);
126 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
127 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, machine_mode);
128 static rtx expand_builtin_memcpy (tree, rtx);
129 static rtx expand_builtin_memcpy_with_bounds (tree, rtx);
130 static rtx expand_builtin_memcpy_args (tree, tree, tree, rtx, tree);
131 static rtx expand_builtin_mempcpy (tree, rtx, machine_mode);
132 static rtx expand_builtin_mempcpy_with_bounds (tree, rtx, machine_mode);
133 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
134 machine_mode, int, tree);
135 static rtx expand_builtin_strcpy (tree, rtx);
136 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
137 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
138 static rtx expand_builtin_strncpy (tree, rtx);
139 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, machine_mode);
140 static rtx expand_builtin_memset (tree, rtx, machine_mode);
141 static rtx expand_builtin_memset_with_bounds (tree, rtx, machine_mode);
142 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
143 static rtx expand_builtin_bzero (tree);
144 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
145 static rtx expand_builtin_alloca (tree, bool);
146 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
147 static rtx expand_builtin_frame_address (tree, tree);
148 static tree stabilize_va_list_loc (location_t, tree, int);
149 static rtx expand_builtin_expect (tree, rtx);
150 static tree fold_builtin_constant_p (tree);
151 static tree fold_builtin_classify_type (tree);
152 static tree fold_builtin_strlen (location_t, tree, tree);
153 static tree fold_builtin_inf (location_t, tree, int);
154 static tree fold_builtin_nan (tree, tree, int);
155 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
156 static bool validate_arg (const_tree, enum tree_code code);
157 static bool integer_valued_real_p (tree);
158 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
159 static rtx expand_builtin_fabs (tree, rtx, rtx);
160 static rtx expand_builtin_signbit (tree, rtx);
161 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
162 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
163 static tree fold_builtin_cos (location_t, tree, tree, tree);
164 static tree fold_builtin_cosh (location_t, tree, tree, tree);
165 static tree fold_builtin_tan (tree, tree);
166 static tree fold_builtin_trunc (location_t, tree, tree);
167 static tree fold_builtin_floor (location_t, tree, tree);
168 static tree fold_builtin_ceil (location_t, tree, tree);
169 static tree fold_builtin_round (location_t, tree, tree);
170 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
171 static tree fold_builtin_bitop (tree, tree);
172 static tree fold_builtin_strchr (location_t, tree, tree, tree);
173 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
174 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
175 static tree fold_builtin_strcmp (location_t, tree, tree);
176 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
177 static tree fold_builtin_signbit (location_t, tree, tree);
178 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
179 static tree fold_builtin_isascii (location_t, tree);
180 static tree fold_builtin_toascii (location_t, tree);
181 static tree fold_builtin_isdigit (location_t, tree);
182 static tree fold_builtin_fabs (location_t, tree, tree);
183 static tree fold_builtin_abs (location_t, tree, tree);
184 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
185 enum tree_code);
186 static tree fold_builtin_0 (location_t, tree);
187 static tree fold_builtin_1 (location_t, tree, tree);
188 static tree fold_builtin_2 (location_t, tree, tree, tree);
189 static tree fold_builtin_3 (location_t, tree, tree, tree, tree);
190 static tree fold_builtin_varargs (location_t, tree, tree*, int);
192 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
193 static tree fold_builtin_strstr (location_t, tree, tree, tree);
194 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
195 static tree fold_builtin_strspn (location_t, tree, tree);
196 static tree fold_builtin_strcspn (location_t, tree, tree);
198 static rtx expand_builtin_object_size (tree);
199 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
200 enum built_in_function);
201 static void maybe_emit_chk_warning (tree, enum built_in_function);
202 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
203 static void maybe_emit_free_warning (tree);
204 static tree fold_builtin_object_size (tree, tree);
206 unsigned HOST_WIDE_INT target_newline;
207 unsigned HOST_WIDE_INT target_percent;
208 static unsigned HOST_WIDE_INT target_c;
209 static unsigned HOST_WIDE_INT target_s;
210 char target_percent_c[3];
211 char target_percent_s[3];
212 char target_percent_s_newline[4];
213 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
214 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
215 static tree do_mpfr_arg2 (tree, tree, tree,
216 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
217 static tree do_mpfr_arg3 (tree, tree, tree, tree,
218 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
219 static tree do_mpfr_sincos (tree, tree, tree);
220 static tree do_mpfr_bessel_n (tree, tree, tree,
221 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
222 const REAL_VALUE_TYPE *, bool);
223 static tree do_mpfr_remquo (tree, tree, tree);
224 static tree do_mpfr_lgamma_r (tree, tree, tree);
225 static void expand_builtin_sync_synchronize (void);
227 /* Return true if NAME starts with __builtin_ or __sync_. */
229 static bool
230 is_builtin_name (const char *name)
232 if (strncmp (name, "__builtin_", 10) == 0)
233 return true;
234 if (strncmp (name, "__sync_", 7) == 0)
235 return true;
236 if (strncmp (name, "__atomic_", 9) == 0)
237 return true;
238 if (flag_cilkplus
239 && (!strcmp (name, "__cilkrts_detach")
240 || !strcmp (name, "__cilkrts_pop_frame")))
241 return true;
242 return false;
246 /* Return true if DECL is a function symbol representing a built-in. */
248 bool
249 is_builtin_fn (tree decl)
251 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
254 /* Return true if NODE should be considered for inline expansion regardless
255 of the optimization level. This means whenever a function is invoked with
256 its "internal" name, which normally contains the prefix "__builtin". */
258 static bool
259 called_as_built_in (tree node)
261 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
262 we want the name used to call the function, not the name it
263 will have. */
264 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
265 return is_builtin_name (name);
268 /* Compute values M and N such that M divides (address of EXP - N) and such
269 that N < M. If these numbers can be determined, store M in alignp and N in
270 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
271 *alignp and any bit-offset to *bitposp.
273 Note that the address (and thus the alignment) computed here is based
274 on the address to which a symbol resolves, whereas DECL_ALIGN is based
275 on the address at which an object is actually located. These two
276 addresses are not always the same. For example, on ARM targets,
277 the address &foo of a Thumb function foo() has the lowest bit set,
278 whereas foo() itself starts on an even address.
280 If ADDR_P is true we are taking the address of the memory reference EXP
281 and thus cannot rely on the access taking place. */
283 static bool
284 get_object_alignment_2 (tree exp, unsigned int *alignp,
285 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
287 HOST_WIDE_INT bitsize, bitpos;
288 tree offset;
289 machine_mode mode;
290 int unsignedp, volatilep;
291 unsigned int align = BITS_PER_UNIT;
292 bool known_alignment = false;
294 /* Get the innermost object and the constant (bitpos) and possibly
295 variable (offset) offset of the access. */
296 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
297 &mode, &unsignedp, &volatilep, true);
299 /* Extract alignment information from the innermost object and
300 possibly adjust bitpos and offset. */
301 if (TREE_CODE (exp) == FUNCTION_DECL)
303 /* Function addresses can encode extra information besides their
304 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
305 allows the low bit to be used as a virtual bit, we know
306 that the address itself must be at least 2-byte aligned. */
307 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
308 align = 2 * BITS_PER_UNIT;
310 else if (TREE_CODE (exp) == LABEL_DECL)
312 else if (TREE_CODE (exp) == CONST_DECL)
314 /* The alignment of a CONST_DECL is determined by its initializer. */
315 exp = DECL_INITIAL (exp);
316 align = TYPE_ALIGN (TREE_TYPE (exp));
317 if (CONSTANT_CLASS_P (exp))
318 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
320 known_alignment = true;
322 else if (DECL_P (exp))
324 align = DECL_ALIGN (exp);
325 known_alignment = true;
327 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
329 align = TYPE_ALIGN (TREE_TYPE (exp));
331 else if (TREE_CODE (exp) == INDIRECT_REF
332 || TREE_CODE (exp) == MEM_REF
333 || TREE_CODE (exp) == TARGET_MEM_REF)
335 tree addr = TREE_OPERAND (exp, 0);
336 unsigned ptr_align;
337 unsigned HOST_WIDE_INT ptr_bitpos;
338 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
340 /* If the address is explicitely aligned, handle that. */
341 if (TREE_CODE (addr) == BIT_AND_EXPR
342 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
344 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
345 ptr_bitmask *= BITS_PER_UNIT;
346 align = ptr_bitmask & -ptr_bitmask;
347 addr = TREE_OPERAND (addr, 0);
350 known_alignment
351 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
352 align = MAX (ptr_align, align);
354 /* Re-apply explicit alignment to the bitpos. */
355 ptr_bitpos &= ptr_bitmask;
357 /* The alignment of the pointer operand in a TARGET_MEM_REF
358 has to take the variable offset parts into account. */
359 if (TREE_CODE (exp) == TARGET_MEM_REF)
361 if (TMR_INDEX (exp))
363 unsigned HOST_WIDE_INT step = 1;
364 if (TMR_STEP (exp))
365 step = TREE_INT_CST_LOW (TMR_STEP (exp));
366 align = MIN (align, (step & -step) * BITS_PER_UNIT);
368 if (TMR_INDEX2 (exp))
369 align = BITS_PER_UNIT;
370 known_alignment = false;
373 /* When EXP is an actual memory reference then we can use
374 TYPE_ALIGN of a pointer indirection to derive alignment.
375 Do so only if get_pointer_alignment_1 did not reveal absolute
376 alignment knowledge and if using that alignment would
377 improve the situation. */
378 if (!addr_p && !known_alignment
379 && TYPE_ALIGN (TREE_TYPE (exp)) > align)
380 align = TYPE_ALIGN (TREE_TYPE (exp));
381 else
383 /* Else adjust bitpos accordingly. */
384 bitpos += ptr_bitpos;
385 if (TREE_CODE (exp) == MEM_REF
386 || TREE_CODE (exp) == TARGET_MEM_REF)
387 bitpos += mem_ref_offset (exp).to_short_addr () * BITS_PER_UNIT;
390 else if (TREE_CODE (exp) == STRING_CST)
392 /* STRING_CST are the only constant objects we allow to be not
393 wrapped inside a CONST_DECL. */
394 align = TYPE_ALIGN (TREE_TYPE (exp));
395 if (CONSTANT_CLASS_P (exp))
396 align = (unsigned) CONSTANT_ALIGNMENT (exp, align);
398 known_alignment = true;
401 /* If there is a non-constant offset part extract the maximum
402 alignment that can prevail. */
403 if (offset)
405 unsigned int trailing_zeros = tree_ctz (offset);
406 if (trailing_zeros < HOST_BITS_PER_INT)
408 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
409 if (inner)
410 align = MIN (align, inner);
414 *alignp = align;
415 *bitposp = bitpos & (*alignp - 1);
416 return known_alignment;
419 /* For a memory reference expression EXP compute values M and N such that M
420 divides (&EXP - N) and such that N < M. If these numbers can be determined,
421 store M in alignp and N in *BITPOSP and return true. Otherwise return false
422 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
424 bool
425 get_object_alignment_1 (tree exp, unsigned int *alignp,
426 unsigned HOST_WIDE_INT *bitposp)
428 return get_object_alignment_2 (exp, alignp, bitposp, false);
431 /* Return the alignment in bits of EXP, an object. */
433 unsigned int
434 get_object_alignment (tree exp)
436 unsigned HOST_WIDE_INT bitpos = 0;
437 unsigned int align;
439 get_object_alignment_1 (exp, &align, &bitpos);
441 /* align and bitpos now specify known low bits of the pointer.
442 ptr & (align - 1) == bitpos. */
444 if (bitpos != 0)
445 align = (bitpos & -bitpos);
446 return align;
449 /* For a pointer valued expression EXP compute values M and N such that M
450 divides (EXP - N) and such that N < M. If these numbers can be determined,
451 store M in alignp and N in *BITPOSP and return true. Return false if
452 the results are just a conservative approximation.
454 If EXP is not a pointer, false is returned too. */
456 bool
457 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
458 unsigned HOST_WIDE_INT *bitposp)
460 STRIP_NOPS (exp);
462 if (TREE_CODE (exp) == ADDR_EXPR)
463 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
464 alignp, bitposp, true);
465 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
467 unsigned int align;
468 unsigned HOST_WIDE_INT bitpos;
469 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
470 &align, &bitpos);
471 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
472 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
473 else
475 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
476 if (trailing_zeros < HOST_BITS_PER_INT)
478 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
479 if (inner)
480 align = MIN (align, inner);
483 *alignp = align;
484 *bitposp = bitpos & (align - 1);
485 return res;
487 else if (TREE_CODE (exp) == SSA_NAME
488 && POINTER_TYPE_P (TREE_TYPE (exp)))
490 unsigned int ptr_align, ptr_misalign;
491 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
493 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
495 *bitposp = ptr_misalign * BITS_PER_UNIT;
496 *alignp = ptr_align * BITS_PER_UNIT;
497 /* We cannot really tell whether this result is an approximation. */
498 return true;
500 else
502 *bitposp = 0;
503 *alignp = BITS_PER_UNIT;
504 return false;
507 else if (TREE_CODE (exp) == INTEGER_CST)
509 *alignp = BIGGEST_ALIGNMENT;
510 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
511 & (BIGGEST_ALIGNMENT - 1));
512 return true;
515 *bitposp = 0;
516 *alignp = BITS_PER_UNIT;
517 return false;
520 /* Return the alignment in bits of EXP, a pointer valued expression.
521 The alignment returned is, by default, the alignment of the thing that
522 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
524 Otherwise, look at the expression to see if we can do better, i.e., if the
525 expression is actually pointing at an object whose alignment is tighter. */
527 unsigned int
528 get_pointer_alignment (tree exp)
530 unsigned HOST_WIDE_INT bitpos = 0;
531 unsigned int align;
533 get_pointer_alignment_1 (exp, &align, &bitpos);
535 /* align and bitpos now specify known low bits of the pointer.
536 ptr & (align - 1) == bitpos. */
538 if (bitpos != 0)
539 align = (bitpos & -bitpos);
541 return align;
544 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
545 way, because it could contain a zero byte in the middle.
546 TREE_STRING_LENGTH is the size of the character array, not the string.
548 ONLY_VALUE should be nonzero if the result is not going to be emitted
549 into the instruction stream and zero if it is going to be expanded.
550 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
551 is returned, otherwise NULL, since
552 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
553 evaluate the side-effects.
555 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
556 accesses. Note that this implies the result is not going to be emitted
557 into the instruction stream.
559 The value returned is of type `ssizetype'.
561 Unfortunately, string_constant can't access the values of const char
562 arrays with initializers, so neither can we do so here. */
564 tree
565 c_strlen (tree src, int only_value)
567 tree offset_node;
568 HOST_WIDE_INT offset;
569 int max;
570 const char *ptr;
571 location_t loc;
573 STRIP_NOPS (src);
574 if (TREE_CODE (src) == COND_EXPR
575 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
577 tree len1, len2;
579 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
580 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
581 if (tree_int_cst_equal (len1, len2))
582 return len1;
585 if (TREE_CODE (src) == COMPOUND_EXPR
586 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
587 return c_strlen (TREE_OPERAND (src, 1), only_value);
589 loc = EXPR_LOC_OR_LOC (src, input_location);
591 src = string_constant (src, &offset_node);
592 if (src == 0)
593 return NULL_TREE;
595 max = TREE_STRING_LENGTH (src) - 1;
596 ptr = TREE_STRING_POINTER (src);
598 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
600 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
601 compute the offset to the following null if we don't know where to
602 start searching for it. */
603 int i;
605 for (i = 0; i < max; i++)
606 if (ptr[i] == 0)
607 return NULL_TREE;
609 /* We don't know the starting offset, but we do know that the string
610 has no internal zero bytes. We can assume that the offset falls
611 within the bounds of the string; otherwise, the programmer deserves
612 what he gets. Subtract the offset from the length of the string,
613 and return that. This would perhaps not be valid if we were dealing
614 with named arrays in addition to literal string constants. */
616 return size_diffop_loc (loc, size_int (max), offset_node);
619 /* We have a known offset into the string. Start searching there for
620 a null character if we can represent it as a single HOST_WIDE_INT. */
621 if (offset_node == 0)
622 offset = 0;
623 else if (! tree_fits_shwi_p (offset_node))
624 offset = -1;
625 else
626 offset = tree_to_shwi (offset_node);
628 /* If the offset is known to be out of bounds, warn, and call strlen at
629 runtime. */
630 if (offset < 0 || offset > max)
632 /* Suppress multiple warnings for propagated constant strings. */
633 if (only_value != 2
634 && !TREE_NO_WARNING (src))
636 warning_at (loc, 0, "offset outside bounds of constant string");
637 TREE_NO_WARNING (src) = 1;
639 return NULL_TREE;
642 /* Use strlen to search for the first zero byte. Since any strings
643 constructed with build_string will have nulls appended, we win even
644 if we get handed something like (char[4])"abcd".
646 Since OFFSET is our starting index into the string, no further
647 calculation is needed. */
648 return ssize_int (strlen (ptr + offset));
651 /* Return a char pointer for a C string if it is a string constant
652 or sum of string constant and integer constant. */
654 const char *
655 c_getstr (tree src)
657 tree offset_node;
659 src = string_constant (src, &offset_node);
660 if (src == 0)
661 return 0;
663 if (offset_node == 0)
664 return TREE_STRING_POINTER (src);
665 else if (!tree_fits_uhwi_p (offset_node)
666 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
667 return 0;
669 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);
672 /* Return a constant integer corresponding to target reading
673 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
675 static rtx
676 c_readstr (const char *str, machine_mode mode)
678 HOST_WIDE_INT ch;
679 unsigned int i, j;
680 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
682 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
683 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
684 / HOST_BITS_PER_WIDE_INT;
686 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
687 for (i = 0; i < len; i++)
688 tmp[i] = 0;
690 ch = 1;
691 for (i = 0; i < GET_MODE_SIZE (mode); i++)
693 j = i;
694 if (WORDS_BIG_ENDIAN)
695 j = GET_MODE_SIZE (mode) - i - 1;
696 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
697 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
698 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
699 j *= BITS_PER_UNIT;
701 if (ch)
702 ch = (unsigned char) str[i];
703 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
706 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
707 return immed_wide_int_const (c, mode);
710 /* Cast a target constant CST to target CHAR and if that value fits into
711 host char type, return zero and put that value into variable pointed to by
712 P. */
714 static int
715 target_char_cast (tree cst, char *p)
717 unsigned HOST_WIDE_INT val, hostval;
719 if (TREE_CODE (cst) != INTEGER_CST
720 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
721 return 1;
723 /* Do not care if it fits or not right here. */
724 val = TREE_INT_CST_LOW (cst);
726 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
727 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
729 hostval = val;
730 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
731 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
733 if (val != hostval)
734 return 1;
736 *p = hostval;
737 return 0;
740 /* Similar to save_expr, but assumes that arbitrary code is not executed
741 in between the multiple evaluations. In particular, we assume that a
742 non-addressable local variable will not be modified. */
744 static tree
745 builtin_save_expr (tree exp)
747 if (TREE_CODE (exp) == SSA_NAME
748 || (TREE_ADDRESSABLE (exp) == 0
749 && (TREE_CODE (exp) == PARM_DECL
750 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
751 return exp;
753 return save_expr (exp);
756 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
757 times to get the address of either a higher stack frame, or a return
758 address located within it (depending on FNDECL_CODE). */
760 static rtx
761 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
763 int i;
764 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
765 if (tem == NULL_RTX)
767 /* For a zero count with __builtin_return_address, we don't care what
768 frame address we return, because target-specific definitions will
769 override us. Therefore frame pointer elimination is OK, and using
770 the soft frame pointer is OK.
772 For a nonzero count, or a zero count with __builtin_frame_address,
773 we require a stable offset from the current frame pointer to the
774 previous one, so we must use the hard frame pointer, and
775 we must disable frame pointer elimination. */
776 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
777 tem = frame_pointer_rtx;
778 else
780 tem = hard_frame_pointer_rtx;
782 /* Tell reload not to eliminate the frame pointer. */
783 crtl->accesses_prior_frames = 1;
787 if (count > 0)
788 SETUP_FRAME_ADDRESSES ();
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 tem = DYNAMIC_CHAIN_ADDRESS (tem);
803 tem = memory_address (Pmode, tem);
804 tem = gen_frame_mem (Pmode, tem);
805 tem = copy_to_reg (tem);
808 /* For __builtin_frame_address, return what we've got. But, on
809 the SPARC for example, we may have to add a bias. */
810 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
811 return FRAME_ADDR_RTX (tem);
813 /* For __builtin_return_address, get the return address from that frame. */
814 #ifdef RETURN_ADDR_RTX
815 tem = RETURN_ADDR_RTX (count, tem);
816 #else
817 tem = memory_address (Pmode,
818 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
819 tem = gen_frame_mem (Pmode, tem);
820 #endif
821 return tem;
824 /* Alias set used for setjmp buffer. */
825 static alias_set_type setjmp_alias_set = -1;
827 /* Construct the leading half of a __builtin_setjmp call. Control will
828 return to RECEIVER_LABEL. This is also called directly by the SJLJ
829 exception handling code. */
831 void
832 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
834 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
835 rtx stack_save;
836 rtx mem;
838 if (setjmp_alias_set == -1)
839 setjmp_alias_set = new_alias_set ();
841 buf_addr = convert_memory_address (Pmode, buf_addr);
843 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
845 /* We store the frame pointer and the address of receiver_label in
846 the buffer and use the rest of it for the stack save area, which
847 is machine-dependent. */
849 mem = gen_rtx_MEM (Pmode, buf_addr);
850 set_mem_alias_set (mem, setjmp_alias_set);
851 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
853 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
854 GET_MODE_SIZE (Pmode))),
855 set_mem_alias_set (mem, setjmp_alias_set);
857 emit_move_insn (validize_mem (mem),
858 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
860 stack_save = gen_rtx_MEM (sa_mode,
861 plus_constant (Pmode, buf_addr,
862 2 * GET_MODE_SIZE (Pmode)));
863 set_mem_alias_set (stack_save, setjmp_alias_set);
864 emit_stack_save (SAVE_NONLOCAL, &stack_save);
866 /* If there is further processing to do, do it. */
867 if (targetm.have_builtin_setjmp_setup ())
868 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
870 /* We have a nonlocal label. */
871 cfun->has_nonlocal_label = 1;
874 /* Construct the trailing part of a __builtin_setjmp call. This is
875 also called directly by the SJLJ exception handling code.
876 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
878 void
879 expand_builtin_setjmp_receiver (rtx receiver_label)
881 rtx chain;
883 /* Mark the FP as used when we get here, so we have to make sure it's
884 marked as used by this function. */
885 emit_use (hard_frame_pointer_rtx);
887 /* Mark the static chain as clobbered here so life information
888 doesn't get messed up for it. */
889 chain = targetm.calls.static_chain (current_function_decl, true);
890 if (chain && REG_P (chain))
891 emit_clobber (chain);
893 /* Now put in the code to restore the frame pointer, and argument
894 pointer, if needed. */
895 if (! targetm.have_nonlocal_goto ())
897 /* First adjust our frame pointer to its actual value. It was
898 previously set to the start of the virtual area corresponding to
899 the stacked variables when we branched here and now needs to be
900 adjusted to the actual hardware fp value.
902 Assignments to virtual registers are converted by
903 instantiate_virtual_regs into the corresponding assignment
904 to the underlying register (fp in this case) that makes
905 the original assignment true.
906 So the following insn will actually be decrementing fp by
907 STARTING_FRAME_OFFSET. */
908 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
910 /* Restoring the frame pointer also modifies the hard frame pointer.
911 Mark it used (so that the previous assignment remains live once
912 the frame pointer is eliminated) and clobbered (to represent the
913 implicit update from the assignment). */
914 emit_use (hard_frame_pointer_rtx);
915 emit_clobber (hard_frame_pointer_rtx);
918 if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
920 #ifdef ELIMINABLE_REGS
921 /* If the argument pointer can be eliminated in favor of the
922 frame pointer, we don't need to restore it. We assume here
923 that if such an elimination is present, it can always be used.
924 This is the case on all known machines; if we don't make this
925 assumption, we do unnecessary saving on many machines. */
926 size_t i;
927 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
929 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
930 if (elim_regs[i].from == ARG_POINTER_REGNUM
931 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
932 break;
934 if (i == ARRAY_SIZE (elim_regs))
935 #endif
937 /* Now restore our arg pointer from the address at which it
938 was saved in our stack frame. */
939 emit_move_insn (crtl->args.internal_arg_pointer,
940 copy_to_reg (get_arg_pointer_save_area ()));
944 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
945 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
946 else if (targetm.have_nonlocal_goto_receiver ())
947 emit_insn (targetm.gen_nonlocal_goto_receiver ());
948 else
949 { /* Nothing */ }
951 /* We must not allow the code we just generated to be reordered by
952 scheduling. Specifically, the update of the frame pointer must
953 happen immediately, not later. */
954 emit_insn (gen_blockage ());
957 /* __builtin_longjmp is passed a pointer to an array of five words (not
958 all will be used on all machines). It operates similarly to the C
959 library function of the same name, but is more efficient. Much of
960 the code below is copied from the handling of non-local gotos. */
962 static void
963 expand_builtin_longjmp (rtx buf_addr, rtx value)
965 rtx fp, lab, stack;
966 rtx_insn *insn, *last;
967 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
969 /* DRAP is needed for stack realign if longjmp is expanded to current
970 function */
971 if (SUPPORTS_STACK_ALIGNMENT)
972 crtl->need_drap = true;
974 if (setjmp_alias_set == -1)
975 setjmp_alias_set = new_alias_set ();
977 buf_addr = convert_memory_address (Pmode, buf_addr);
979 buf_addr = force_reg (Pmode, buf_addr);
981 /* We require that the user must pass a second argument of 1, because
982 that is what builtin_setjmp will return. */
983 gcc_assert (value == const1_rtx);
985 last = get_last_insn ();
986 if (targetm.have_builtin_longjmp ())
987 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
988 else
990 fp = gen_rtx_MEM (Pmode, buf_addr);
991 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
992 GET_MODE_SIZE (Pmode)));
994 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
995 2 * GET_MODE_SIZE (Pmode)));
996 set_mem_alias_set (fp, setjmp_alias_set);
997 set_mem_alias_set (lab, setjmp_alias_set);
998 set_mem_alias_set (stack, setjmp_alias_set);
1000 /* Pick up FP, label, and SP from the block and jump. This code is
1001 from expand_goto in stmt.c; see there for detailed comments. */
1002 if (targetm.have_nonlocal_goto ())
1003 /* We have to pass a value to the nonlocal_goto pattern that will
1004 get copied into the static_chain pointer, but it does not matter
1005 what that value is, because builtin_setjmp does not use it. */
1006 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1007 else
1009 lab = copy_to_reg (lab);
1011 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1012 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1014 emit_move_insn (hard_frame_pointer_rtx, fp);
1015 emit_stack_restore (SAVE_NONLOCAL, stack);
1017 emit_use (hard_frame_pointer_rtx);
1018 emit_use (stack_pointer_rtx);
1019 emit_indirect_jump (lab);
1023 /* Search backwards and mark the jump insn as a non-local goto.
1024 Note that this precludes the use of __builtin_longjmp to a
1025 __builtin_setjmp target in the same function. However, we've
1026 already cautioned the user that these functions are for
1027 internal exception handling use only. */
1028 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1030 gcc_assert (insn != last);
1032 if (JUMP_P (insn))
1034 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1035 break;
1037 else if (CALL_P (insn))
1038 break;
1042 static inline bool
1043 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1045 return (iter->i < iter->n);
1048 /* This function validates the types of a function call argument list
1049 against a specified list of tree_codes. If the last specifier is a 0,
1050 that represents an ellipses, otherwise the last specifier must be a
1051 VOID_TYPE. */
1053 static bool
1054 validate_arglist (const_tree callexpr, ...)
1056 enum tree_code code;
1057 bool res = 0;
1058 va_list ap;
1059 const_call_expr_arg_iterator iter;
1060 const_tree arg;
1062 va_start (ap, callexpr);
1063 init_const_call_expr_arg_iterator (callexpr, &iter);
1067 code = (enum tree_code) va_arg (ap, int);
1068 switch (code)
1070 case 0:
1071 /* This signifies an ellipses, any further arguments are all ok. */
1072 res = true;
1073 goto end;
1074 case VOID_TYPE:
1075 /* This signifies an endlink, if no arguments remain, return
1076 true, otherwise return false. */
1077 res = !more_const_call_expr_args_p (&iter);
1078 goto end;
1079 default:
1080 /* If no parameters remain or the parameter's code does not
1081 match the specified code, return false. Otherwise continue
1082 checking any remaining arguments. */
1083 arg = next_const_call_expr_arg (&iter);
1084 if (!validate_arg (arg, code))
1085 goto end;
1086 break;
1089 while (1);
1091 /* We need gotos here since we can only have one VA_CLOSE in a
1092 function. */
1093 end: ;
1094 va_end (ap);
1096 return res;
1099 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1100 and the address of the save area. */
1102 static rtx
1103 expand_builtin_nonlocal_goto (tree exp)
1105 tree t_label, t_save_area;
1106 rtx r_label, r_save_area, r_fp, r_sp;
1107 rtx_insn *insn;
1109 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1110 return NULL_RTX;
1112 t_label = CALL_EXPR_ARG (exp, 0);
1113 t_save_area = CALL_EXPR_ARG (exp, 1);
1115 r_label = expand_normal (t_label);
1116 r_label = convert_memory_address (Pmode, r_label);
1117 r_save_area = expand_normal (t_save_area);
1118 r_save_area = convert_memory_address (Pmode, r_save_area);
1119 /* Copy the address of the save location to a register just in case it was
1120 based on the frame pointer. */
1121 r_save_area = copy_to_reg (r_save_area);
1122 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1123 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1124 plus_constant (Pmode, r_save_area,
1125 GET_MODE_SIZE (Pmode)));
1127 crtl->has_nonlocal_goto = 1;
1129 /* ??? We no longer need to pass the static chain value, afaik. */
1130 if (targetm.have_nonlocal_goto ())
1131 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1132 else
1134 r_label = copy_to_reg (r_label);
1136 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1137 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1139 /* Restore frame pointer for containing function. */
1140 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1141 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1143 /* USE of hard_frame_pointer_rtx added for consistency;
1144 not clear if really needed. */
1145 emit_use (hard_frame_pointer_rtx);
1146 emit_use (stack_pointer_rtx);
1148 /* If the architecture is using a GP register, we must
1149 conservatively assume that the target function makes use of it.
1150 The prologue of functions with nonlocal gotos must therefore
1151 initialize the GP register to the appropriate value, and we
1152 must then make sure that this value is live at the point
1153 of the jump. (Note that this doesn't necessarily apply
1154 to targets with a nonlocal_goto pattern; they are free
1155 to implement it in their own way. Note also that this is
1156 a no-op if the GP register is a global invariant.) */
1157 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1158 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1159 emit_use (pic_offset_table_rtx);
1161 emit_indirect_jump (r_label);
1164 /* Search backwards to the jump insn and mark it as a
1165 non-local goto. */
1166 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1168 if (JUMP_P (insn))
1170 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1171 break;
1173 else if (CALL_P (insn))
1174 break;
1177 return const0_rtx;
1180 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1181 (not all will be used on all machines) that was passed to __builtin_setjmp.
1182 It updates the stack pointer in that block to the current value. This is
1183 also called directly by the SJLJ exception handling code. */
1185 void
1186 expand_builtin_update_setjmp_buf (rtx buf_addr)
1188 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1189 rtx stack_save
1190 = gen_rtx_MEM (sa_mode,
1191 memory_address
1192 (sa_mode,
1193 plus_constant (Pmode, buf_addr,
1194 2 * GET_MODE_SIZE (Pmode))));
1196 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1199 /* Expand a call to __builtin_prefetch. For a target that does not support
1200 data prefetch, evaluate the memory address argument in case it has side
1201 effects. */
1203 static void
1204 expand_builtin_prefetch (tree exp)
1206 tree arg0, arg1, arg2;
1207 int nargs;
1208 rtx op0, op1, op2;
1210 if (!validate_arglist (exp, POINTER_TYPE, 0))
1211 return;
1213 arg0 = CALL_EXPR_ARG (exp, 0);
1215 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1216 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1217 locality). */
1218 nargs = call_expr_nargs (exp);
1219 if (nargs > 1)
1220 arg1 = CALL_EXPR_ARG (exp, 1);
1221 else
1222 arg1 = integer_zero_node;
1223 if (nargs > 2)
1224 arg2 = CALL_EXPR_ARG (exp, 2);
1225 else
1226 arg2 = integer_three_node;
1228 /* Argument 0 is an address. */
1229 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1231 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1232 if (TREE_CODE (arg1) != INTEGER_CST)
1234 error ("second argument to %<__builtin_prefetch%> must be a constant");
1235 arg1 = integer_zero_node;
1237 op1 = expand_normal (arg1);
1238 /* Argument 1 must be either zero or one. */
1239 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1241 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1242 " using zero");
1243 op1 = const0_rtx;
1246 /* Argument 2 (locality) must be a compile-time constant int. */
1247 if (TREE_CODE (arg2) != INTEGER_CST)
1249 error ("third argument to %<__builtin_prefetch%> must be a constant");
1250 arg2 = integer_zero_node;
1252 op2 = expand_normal (arg2);
1253 /* Argument 2 must be 0, 1, 2, or 3. */
1254 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1256 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1257 op2 = const0_rtx;
1260 if (targetm.have_prefetch ())
1262 struct expand_operand ops[3];
1264 create_address_operand (&ops[0], op0);
1265 create_integer_operand (&ops[1], INTVAL (op1));
1266 create_integer_operand (&ops[2], INTVAL (op2));
1267 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1268 return;
1271 /* Don't do anything with direct references to volatile memory, but
1272 generate code to handle other side effects. */
1273 if (!MEM_P (op0) && side_effects_p (op0))
1274 emit_insn (op0);
1277 /* Get a MEM rtx for expression EXP which is the address of an operand
1278 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1279 the maximum length of the block of memory that might be accessed or
1280 NULL if unknown. */
1282 static rtx
1283 get_memory_rtx (tree exp, tree len)
1285 tree orig_exp = exp;
1286 rtx addr, mem;
1288 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1289 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1290 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1291 exp = TREE_OPERAND (exp, 0);
1293 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1294 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1296 /* Get an expression we can use to find the attributes to assign to MEM.
1297 First remove any nops. */
1298 while (CONVERT_EXPR_P (exp)
1299 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1300 exp = TREE_OPERAND (exp, 0);
1302 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1303 (as builtin stringops may alias with anything). */
1304 exp = fold_build2 (MEM_REF,
1305 build_array_type (char_type_node,
1306 build_range_type (sizetype,
1307 size_one_node, len)),
1308 exp, build_int_cst (ptr_type_node, 0));
1310 /* If the MEM_REF has no acceptable address, try to get the base object
1311 from the original address we got, and build an all-aliasing
1312 unknown-sized access to that one. */
1313 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1314 set_mem_attributes (mem, exp, 0);
1315 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1316 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1317 0))))
1319 exp = build_fold_addr_expr (exp);
1320 exp = fold_build2 (MEM_REF,
1321 build_array_type (char_type_node,
1322 build_range_type (sizetype,
1323 size_zero_node,
1324 NULL)),
1325 exp, build_int_cst (ptr_type_node, 0));
1326 set_mem_attributes (mem, exp, 0);
1328 set_mem_alias_set (mem, 0);
1329 return mem;
1332 /* Built-in functions to perform an untyped call and return. */
1334 #define apply_args_mode \
1335 (this_target_builtins->x_apply_args_mode)
1336 #define apply_result_mode \
1337 (this_target_builtins->x_apply_result_mode)
1339 /* Return the size required for the block returned by __builtin_apply_args,
1340 and initialize apply_args_mode. */
1342 static int
1343 apply_args_size (void)
1345 static int size = -1;
1346 int align;
1347 unsigned int regno;
1348 machine_mode mode;
1350 /* The values computed by this function never change. */
1351 if (size < 0)
1353 /* The first value is the incoming arg-pointer. */
1354 size = GET_MODE_SIZE (Pmode);
1356 /* The second value is the structure value address unless this is
1357 passed as an "invisible" first argument. */
1358 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1359 size += GET_MODE_SIZE (Pmode);
1361 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1362 if (FUNCTION_ARG_REGNO_P (regno))
1364 mode = targetm.calls.get_raw_arg_mode (regno);
1366 gcc_assert (mode != VOIDmode);
1368 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1369 if (size % align != 0)
1370 size = CEIL (size, align) * align;
1371 size += GET_MODE_SIZE (mode);
1372 apply_args_mode[regno] = mode;
1374 else
1376 apply_args_mode[regno] = VOIDmode;
1379 return size;
1382 /* Return the size required for the block returned by __builtin_apply,
1383 and initialize apply_result_mode. */
1385 static int
1386 apply_result_size (void)
1388 static int size = -1;
1389 int align, regno;
1390 machine_mode mode;
1392 /* The values computed by this function never change. */
1393 if (size < 0)
1395 size = 0;
1397 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1398 if (targetm.calls.function_value_regno_p (regno))
1400 mode = targetm.calls.get_raw_result_mode (regno);
1402 gcc_assert (mode != VOIDmode);
1404 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1405 if (size % align != 0)
1406 size = CEIL (size, align) * align;
1407 size += GET_MODE_SIZE (mode);
1408 apply_result_mode[regno] = mode;
1410 else
1411 apply_result_mode[regno] = VOIDmode;
1413 /* Allow targets that use untyped_call and untyped_return to override
1414 the size so that machine-specific information can be stored here. */
1415 #ifdef APPLY_RESULT_SIZE
1416 size = APPLY_RESULT_SIZE;
1417 #endif
1419 return size;
1422 /* Create a vector describing the result block RESULT. If SAVEP is true,
1423 the result block is used to save the values; otherwise it is used to
1424 restore the values. */
1426 static rtx
1427 result_vector (int savep, rtx result)
1429 int regno, size, align, nelts;
1430 machine_mode mode;
1431 rtx reg, mem;
1432 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1434 size = nelts = 0;
1435 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1436 if ((mode = apply_result_mode[regno]) != VOIDmode)
1438 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1439 if (size % align != 0)
1440 size = CEIL (size, align) * align;
1441 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1442 mem = adjust_address (result, mode, size);
1443 savevec[nelts++] = (savep
1444 ? gen_rtx_SET (mem, reg)
1445 : gen_rtx_SET (reg, mem));
1446 size += GET_MODE_SIZE (mode);
1448 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1451 /* Save the state required to perform an untyped call with the same
1452 arguments as were passed to the current function. */
1454 static rtx
1455 expand_builtin_apply_args_1 (void)
1457 rtx registers, tem;
1458 int size, align, regno;
1459 machine_mode mode;
1460 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1462 /* Create a block where the arg-pointer, structure value address,
1463 and argument registers can be saved. */
1464 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1466 /* Walk past the arg-pointer and structure value address. */
1467 size = GET_MODE_SIZE (Pmode);
1468 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1469 size += GET_MODE_SIZE (Pmode);
1471 /* Save each register used in calling a function to the block. */
1472 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1473 if ((mode = apply_args_mode[regno]) != VOIDmode)
1475 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1476 if (size % align != 0)
1477 size = CEIL (size, align) * align;
1479 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1481 emit_move_insn (adjust_address (registers, mode, size), tem);
1482 size += GET_MODE_SIZE (mode);
1485 /* Save the arg pointer to the block. */
1486 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1487 /* We need the pointer as the caller actually passed them to us, not
1488 as we might have pretended they were passed. Make sure it's a valid
1489 operand, as emit_move_insn isn't expected to handle a PLUS. */
1490 if (STACK_GROWS_DOWNWARD)
1492 = force_operand (plus_constant (Pmode, tem,
1493 crtl->args.pretend_args_size),
1494 NULL_RTX);
1495 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1497 size = GET_MODE_SIZE (Pmode);
1499 /* Save the structure value address unless this is passed as an
1500 "invisible" first argument. */
1501 if (struct_incoming_value)
1503 emit_move_insn (adjust_address (registers, Pmode, size),
1504 copy_to_reg (struct_incoming_value));
1505 size += GET_MODE_SIZE (Pmode);
1508 /* Return the address of the block. */
1509 return copy_addr_to_reg (XEXP (registers, 0));
1512 /* __builtin_apply_args returns block of memory allocated on
1513 the stack into which is stored the arg pointer, structure
1514 value address, static chain, and all the registers that might
1515 possibly be used in performing a function call. The code is
1516 moved to the start of the function so the incoming values are
1517 saved. */
1519 static rtx
1520 expand_builtin_apply_args (void)
1522 /* Don't do __builtin_apply_args more than once in a function.
1523 Save the result of the first call and reuse it. */
1524 if (apply_args_value != 0)
1525 return apply_args_value;
1527 /* When this function is called, it means that registers must be
1528 saved on entry to this function. So we migrate the
1529 call to the first insn of this function. */
1530 rtx temp;
1532 start_sequence ();
1533 temp = expand_builtin_apply_args_1 ();
1534 rtx_insn *seq = get_insns ();
1535 end_sequence ();
1537 apply_args_value = temp;
1539 /* Put the insns after the NOTE that starts the function.
1540 If this is inside a start_sequence, make the outer-level insn
1541 chain current, so the code is placed at the start of the
1542 function. If internal_arg_pointer is a non-virtual pseudo,
1543 it needs to be placed after the function that initializes
1544 that pseudo. */
1545 push_topmost_sequence ();
1546 if (REG_P (crtl->args.internal_arg_pointer)
1547 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1548 emit_insn_before (seq, parm_birth_insn);
1549 else
1550 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1551 pop_topmost_sequence ();
1552 return temp;
1556 /* Perform an untyped call and save the state required to perform an
1557 untyped return of whatever value was returned by the given function. */
1559 static rtx
1560 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1562 int size, align, regno;
1563 machine_mode mode;
1564 rtx incoming_args, result, reg, dest, src;
1565 rtx_call_insn *call_insn;
1566 rtx old_stack_level = 0;
1567 rtx call_fusage = 0;
1568 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1570 arguments = convert_memory_address (Pmode, arguments);
1572 /* Create a block where the return registers can be saved. */
1573 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1575 /* Fetch the arg pointer from the ARGUMENTS block. */
1576 incoming_args = gen_reg_rtx (Pmode);
1577 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1578 if (!STACK_GROWS_DOWNWARD)
1579 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1580 incoming_args, 0, OPTAB_LIB_WIDEN);
1582 /* Push a new argument block and copy the arguments. Do not allow
1583 the (potential) memcpy call below to interfere with our stack
1584 manipulations. */
1585 do_pending_stack_adjust ();
1586 NO_DEFER_POP;
1588 /* Save the stack with nonlocal if available. */
1589 if (targetm.have_save_stack_nonlocal ())
1590 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1591 else
1592 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1594 /* Allocate a block of memory onto the stack and copy the memory
1595 arguments to the outgoing arguments address. We can pass TRUE
1596 as the 4th argument because we just saved the stack pointer
1597 and will restore it right after the call. */
1598 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1600 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1601 may have already set current_function_calls_alloca to true.
1602 current_function_calls_alloca won't be set if argsize is zero,
1603 so we have to guarantee need_drap is true here. */
1604 if (SUPPORTS_STACK_ALIGNMENT)
1605 crtl->need_drap = true;
1607 dest = virtual_outgoing_args_rtx;
1608 if (!STACK_GROWS_DOWNWARD)
1610 if (CONST_INT_P (argsize))
1611 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1612 else
1613 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1615 dest = gen_rtx_MEM (BLKmode, dest);
1616 set_mem_align (dest, PARM_BOUNDARY);
1617 src = gen_rtx_MEM (BLKmode, incoming_args);
1618 set_mem_align (src, PARM_BOUNDARY);
1619 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1621 /* Refer to the argument block. */
1622 apply_args_size ();
1623 arguments = gen_rtx_MEM (BLKmode, arguments);
1624 set_mem_align (arguments, PARM_BOUNDARY);
1626 /* Walk past the arg-pointer and structure value address. */
1627 size = GET_MODE_SIZE (Pmode);
1628 if (struct_value)
1629 size += GET_MODE_SIZE (Pmode);
1631 /* Restore each of the registers previously saved. Make USE insns
1632 for each of these registers for use in making the call. */
1633 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1634 if ((mode = apply_args_mode[regno]) != VOIDmode)
1636 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1637 if (size % align != 0)
1638 size = CEIL (size, align) * align;
1639 reg = gen_rtx_REG (mode, regno);
1640 emit_move_insn (reg, adjust_address (arguments, mode, size));
1641 use_reg (&call_fusage, reg);
1642 size += GET_MODE_SIZE (mode);
1645 /* Restore the structure value address unless this is passed as an
1646 "invisible" first argument. */
1647 size = GET_MODE_SIZE (Pmode);
1648 if (struct_value)
1650 rtx value = gen_reg_rtx (Pmode);
1651 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1652 emit_move_insn (struct_value, value);
1653 if (REG_P (struct_value))
1654 use_reg (&call_fusage, struct_value);
1655 size += GET_MODE_SIZE (Pmode);
1658 /* All arguments and registers used for the call are set up by now! */
1659 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1661 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1662 and we don't want to load it into a register as an optimization,
1663 because prepare_call_address already did it if it should be done. */
1664 if (GET_CODE (function) != SYMBOL_REF)
1665 function = memory_address (FUNCTION_MODE, function);
1667 /* Generate the actual call instruction and save the return value. */
1668 if (targetm.have_untyped_call ())
1670 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1671 emit_call_insn (targetm.gen_untyped_call (mem, result,
1672 result_vector (1, result)));
1674 else if (targetm.have_call_value ())
1676 rtx valreg = 0;
1678 /* Locate the unique return register. It is not possible to
1679 express a call that sets more than one return register using
1680 call_value; use untyped_call for that. In fact, untyped_call
1681 only needs to save the return registers in the given block. */
1682 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1683 if ((mode = apply_result_mode[regno]) != VOIDmode)
1685 gcc_assert (!valreg); /* have_untyped_call required. */
1687 valreg = gen_rtx_REG (mode, regno);
1690 emit_insn (targetm.gen_call_value (valreg,
1691 gen_rtx_MEM (FUNCTION_MODE, function),
1692 const0_rtx, NULL_RTX, const0_rtx));
1694 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1696 else
1697 gcc_unreachable ();
1699 /* Find the CALL insn we just emitted, and attach the register usage
1700 information. */
1701 call_insn = last_call_insn ();
1702 add_function_usage_to (call_insn, call_fusage);
1704 /* Restore the stack. */
1705 if (targetm.have_save_stack_nonlocal ())
1706 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1707 else
1708 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1709 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1711 OK_DEFER_POP;
1713 /* Return the address of the result block. */
1714 result = copy_addr_to_reg (XEXP (result, 0));
1715 return convert_memory_address (ptr_mode, result);
1718 /* Perform an untyped return. */
1720 static void
1721 expand_builtin_return (rtx result)
1723 int size, align, regno;
1724 machine_mode mode;
1725 rtx reg;
1726 rtx_insn *call_fusage = 0;
1728 result = convert_memory_address (Pmode, result);
1730 apply_result_size ();
1731 result = gen_rtx_MEM (BLKmode, result);
1733 if (targetm.have_untyped_return ())
1735 rtx vector = result_vector (0, result);
1736 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1737 emit_barrier ();
1738 return;
1741 /* Restore the return value and note that each value is used. */
1742 size = 0;
1743 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1744 if ((mode = apply_result_mode[regno]) != VOIDmode)
1746 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1747 if (size % align != 0)
1748 size = CEIL (size, align) * align;
1749 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1750 emit_move_insn (reg, adjust_address (result, mode, size));
1752 push_to_sequence (call_fusage);
1753 emit_use (reg);
1754 call_fusage = get_insns ();
1755 end_sequence ();
1756 size += GET_MODE_SIZE (mode);
1759 /* Put the USE insns before the return. */
1760 emit_insn (call_fusage);
1762 /* Return whatever values was restored by jumping directly to the end
1763 of the function. */
1764 expand_naked_return ();
1767 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1769 static enum type_class
1770 type_to_class (tree type)
1772 switch (TREE_CODE (type))
1774 case VOID_TYPE: return void_type_class;
1775 case INTEGER_TYPE: return integer_type_class;
1776 case ENUMERAL_TYPE: return enumeral_type_class;
1777 case BOOLEAN_TYPE: return boolean_type_class;
1778 case POINTER_TYPE: return pointer_type_class;
1779 case REFERENCE_TYPE: return reference_type_class;
1780 case OFFSET_TYPE: return offset_type_class;
1781 case REAL_TYPE: return real_type_class;
1782 case COMPLEX_TYPE: return complex_type_class;
1783 case FUNCTION_TYPE: return function_type_class;
1784 case METHOD_TYPE: return method_type_class;
1785 case RECORD_TYPE: return record_type_class;
1786 case UNION_TYPE:
1787 case QUAL_UNION_TYPE: return union_type_class;
1788 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1789 ? string_type_class : array_type_class);
1790 case LANG_TYPE: return lang_type_class;
1791 default: return no_type_class;
1795 /* Expand a call EXP to __builtin_classify_type. */
1797 static rtx
1798 expand_builtin_classify_type (tree exp)
1800 if (call_expr_nargs (exp))
1801 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1802 return GEN_INT (no_type_class);
1805 /* This helper macro, meant to be used in mathfn_built_in below,
1806 determines which among a set of three builtin math functions is
1807 appropriate for a given type mode. The `F' and `L' cases are
1808 automatically generated from the `double' case. */
1809 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1810 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1811 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1812 fcodel = BUILT_IN_MATHFN##L ; break;
1813 /* Similar to above, but appends _R after any F/L suffix. */
1814 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1815 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1816 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1817 fcodel = BUILT_IN_MATHFN##L_R ; break;
1819 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1820 if available. If IMPLICIT is true use the implicit builtin declaration,
1821 otherwise use the explicit declaration. If we can't do the conversion,
1822 return zero. */
1824 static tree
1825 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1827 enum built_in_function fcode, fcodef, fcodel, fcode2;
1829 switch (fn)
1831 CASE_MATHFN (BUILT_IN_ACOS)
1832 CASE_MATHFN (BUILT_IN_ACOSH)
1833 CASE_MATHFN (BUILT_IN_ASIN)
1834 CASE_MATHFN (BUILT_IN_ASINH)
1835 CASE_MATHFN (BUILT_IN_ATAN)
1836 CASE_MATHFN (BUILT_IN_ATAN2)
1837 CASE_MATHFN (BUILT_IN_ATANH)
1838 CASE_MATHFN (BUILT_IN_CBRT)
1839 CASE_MATHFN (BUILT_IN_CEIL)
1840 CASE_MATHFN (BUILT_IN_CEXPI)
1841 CASE_MATHFN (BUILT_IN_COPYSIGN)
1842 CASE_MATHFN (BUILT_IN_COS)
1843 CASE_MATHFN (BUILT_IN_COSH)
1844 CASE_MATHFN (BUILT_IN_DREM)
1845 CASE_MATHFN (BUILT_IN_ERF)
1846 CASE_MATHFN (BUILT_IN_ERFC)
1847 CASE_MATHFN (BUILT_IN_EXP)
1848 CASE_MATHFN (BUILT_IN_EXP10)
1849 CASE_MATHFN (BUILT_IN_EXP2)
1850 CASE_MATHFN (BUILT_IN_EXPM1)
1851 CASE_MATHFN (BUILT_IN_FABS)
1852 CASE_MATHFN (BUILT_IN_FDIM)
1853 CASE_MATHFN (BUILT_IN_FLOOR)
1854 CASE_MATHFN (BUILT_IN_FMA)
1855 CASE_MATHFN (BUILT_IN_FMAX)
1856 CASE_MATHFN (BUILT_IN_FMIN)
1857 CASE_MATHFN (BUILT_IN_FMOD)
1858 CASE_MATHFN (BUILT_IN_FREXP)
1859 CASE_MATHFN (BUILT_IN_GAMMA)
1860 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1861 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1862 CASE_MATHFN (BUILT_IN_HYPOT)
1863 CASE_MATHFN (BUILT_IN_ILOGB)
1864 CASE_MATHFN (BUILT_IN_ICEIL)
1865 CASE_MATHFN (BUILT_IN_IFLOOR)
1866 CASE_MATHFN (BUILT_IN_INF)
1867 CASE_MATHFN (BUILT_IN_IRINT)
1868 CASE_MATHFN (BUILT_IN_IROUND)
1869 CASE_MATHFN (BUILT_IN_ISINF)
1870 CASE_MATHFN (BUILT_IN_J0)
1871 CASE_MATHFN (BUILT_IN_J1)
1872 CASE_MATHFN (BUILT_IN_JN)
1873 CASE_MATHFN (BUILT_IN_LCEIL)
1874 CASE_MATHFN (BUILT_IN_LDEXP)
1875 CASE_MATHFN (BUILT_IN_LFLOOR)
1876 CASE_MATHFN (BUILT_IN_LGAMMA)
1877 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1878 CASE_MATHFN (BUILT_IN_LLCEIL)
1879 CASE_MATHFN (BUILT_IN_LLFLOOR)
1880 CASE_MATHFN (BUILT_IN_LLRINT)
1881 CASE_MATHFN (BUILT_IN_LLROUND)
1882 CASE_MATHFN (BUILT_IN_LOG)
1883 CASE_MATHFN (BUILT_IN_LOG10)
1884 CASE_MATHFN (BUILT_IN_LOG1P)
1885 CASE_MATHFN (BUILT_IN_LOG2)
1886 CASE_MATHFN (BUILT_IN_LOGB)
1887 CASE_MATHFN (BUILT_IN_LRINT)
1888 CASE_MATHFN (BUILT_IN_LROUND)
1889 CASE_MATHFN (BUILT_IN_MODF)
1890 CASE_MATHFN (BUILT_IN_NAN)
1891 CASE_MATHFN (BUILT_IN_NANS)
1892 CASE_MATHFN (BUILT_IN_NEARBYINT)
1893 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1894 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1895 CASE_MATHFN (BUILT_IN_POW)
1896 CASE_MATHFN (BUILT_IN_POWI)
1897 CASE_MATHFN (BUILT_IN_POW10)
1898 CASE_MATHFN (BUILT_IN_REMAINDER)
1899 CASE_MATHFN (BUILT_IN_REMQUO)
1900 CASE_MATHFN (BUILT_IN_RINT)
1901 CASE_MATHFN (BUILT_IN_ROUND)
1902 CASE_MATHFN (BUILT_IN_SCALB)
1903 CASE_MATHFN (BUILT_IN_SCALBLN)
1904 CASE_MATHFN (BUILT_IN_SCALBN)
1905 CASE_MATHFN (BUILT_IN_SIGNBIT)
1906 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1907 CASE_MATHFN (BUILT_IN_SIN)
1908 CASE_MATHFN (BUILT_IN_SINCOS)
1909 CASE_MATHFN (BUILT_IN_SINH)
1910 CASE_MATHFN (BUILT_IN_SQRT)
1911 CASE_MATHFN (BUILT_IN_TAN)
1912 CASE_MATHFN (BUILT_IN_TANH)
1913 CASE_MATHFN (BUILT_IN_TGAMMA)
1914 CASE_MATHFN (BUILT_IN_TRUNC)
1915 CASE_MATHFN (BUILT_IN_Y0)
1916 CASE_MATHFN (BUILT_IN_Y1)
1917 CASE_MATHFN (BUILT_IN_YN)
1919 default:
1920 return NULL_TREE;
1923 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1924 fcode2 = fcode;
1925 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1926 fcode2 = fcodef;
1927 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1928 fcode2 = fcodel;
1929 else
1930 return NULL_TREE;
1932 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1933 return NULL_TREE;
1935 return builtin_decl_explicit (fcode2);
1938 /* Like mathfn_built_in_1(), but always use the implicit array. */
1940 tree
1941 mathfn_built_in (tree type, enum built_in_function fn)
1943 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1946 /* If errno must be maintained, expand the RTL to check if the result,
1947 TARGET, of a built-in function call, EXP, is NaN, and if so set
1948 errno to EDOM. */
1950 static void
1951 expand_errno_check (tree exp, rtx target)
1953 rtx_code_label *lab = gen_label_rtx ();
1955 /* Test the result; if it is NaN, set errno=EDOM because
1956 the argument was not in the domain. */
1957 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1958 NULL_RTX, NULL, lab,
1959 /* The jump is very likely. */
1960 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1962 #ifdef TARGET_EDOM
1963 /* If this built-in doesn't throw an exception, set errno directly. */
1964 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1966 #ifdef GEN_ERRNO_RTX
1967 rtx errno_rtx = GEN_ERRNO_RTX;
1968 #else
1969 rtx errno_rtx
1970 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1971 #endif
1972 emit_move_insn (errno_rtx,
1973 gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
1974 emit_label (lab);
1975 return;
1977 #endif
1979 /* Make sure the library call isn't expanded as a tail call. */
1980 CALL_EXPR_TAILCALL (exp) = 0;
1982 /* We can't set errno=EDOM directly; let the library call do it.
1983 Pop the arguments right away in case the call gets deleted. */
1984 NO_DEFER_POP;
1985 expand_call (exp, target, 0);
1986 OK_DEFER_POP;
1987 emit_label (lab);
1990 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1991 Return NULL_RTX if a normal call should be emitted rather than expanding
1992 the function in-line. EXP is the expression that is a call to the builtin
1993 function; if convenient, the result should be placed in TARGET.
1994 SUBTARGET may be used as the target for computing one of EXP's operands. */
1996 static rtx
1997 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1999 optab builtin_optab;
2000 rtx op0;
2001 rtx_insn *insns;
2002 tree fndecl = get_callee_fndecl (exp);
2003 machine_mode mode;
2004 bool errno_set = false;
2005 bool try_widening = false;
2006 tree arg;
2008 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2009 return NULL_RTX;
2011 arg = CALL_EXPR_ARG (exp, 0);
2013 switch (DECL_FUNCTION_CODE (fndecl))
2015 CASE_FLT_FN (BUILT_IN_SQRT):
2016 errno_set = ! tree_expr_nonnegative_p (arg);
2017 try_widening = true;
2018 builtin_optab = sqrt_optab;
2019 break;
2020 CASE_FLT_FN (BUILT_IN_EXP):
2021 errno_set = true; builtin_optab = exp_optab; break;
2022 CASE_FLT_FN (BUILT_IN_EXP10):
2023 CASE_FLT_FN (BUILT_IN_POW10):
2024 errno_set = true; builtin_optab = exp10_optab; break;
2025 CASE_FLT_FN (BUILT_IN_EXP2):
2026 errno_set = true; builtin_optab = exp2_optab; break;
2027 CASE_FLT_FN (BUILT_IN_EXPM1):
2028 errno_set = true; builtin_optab = expm1_optab; break;
2029 CASE_FLT_FN (BUILT_IN_LOGB):
2030 errno_set = true; builtin_optab = logb_optab; break;
2031 CASE_FLT_FN (BUILT_IN_LOG):
2032 errno_set = true; builtin_optab = log_optab; break;
2033 CASE_FLT_FN (BUILT_IN_LOG10):
2034 errno_set = true; builtin_optab = log10_optab; break;
2035 CASE_FLT_FN (BUILT_IN_LOG2):
2036 errno_set = true; builtin_optab = log2_optab; break;
2037 CASE_FLT_FN (BUILT_IN_LOG1P):
2038 errno_set = true; builtin_optab = log1p_optab; break;
2039 CASE_FLT_FN (BUILT_IN_ASIN):
2040 builtin_optab = asin_optab; break;
2041 CASE_FLT_FN (BUILT_IN_ACOS):
2042 builtin_optab = acos_optab; break;
2043 CASE_FLT_FN (BUILT_IN_TAN):
2044 builtin_optab = tan_optab; break;
2045 CASE_FLT_FN (BUILT_IN_ATAN):
2046 builtin_optab = atan_optab; break;
2047 CASE_FLT_FN (BUILT_IN_FLOOR):
2048 builtin_optab = floor_optab; break;
2049 CASE_FLT_FN (BUILT_IN_CEIL):
2050 builtin_optab = ceil_optab; break;
2051 CASE_FLT_FN (BUILT_IN_TRUNC):
2052 builtin_optab = btrunc_optab; break;
2053 CASE_FLT_FN (BUILT_IN_ROUND):
2054 builtin_optab = round_optab; break;
2055 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2056 builtin_optab = nearbyint_optab;
2057 if (flag_trapping_math)
2058 break;
2059 /* Else fallthrough and expand as rint. */
2060 CASE_FLT_FN (BUILT_IN_RINT):
2061 builtin_optab = rint_optab; break;
2062 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2063 builtin_optab = significand_optab; break;
2064 default:
2065 gcc_unreachable ();
2068 /* Make a suitable register to place result in. */
2069 mode = TYPE_MODE (TREE_TYPE (exp));
2071 if (! flag_errno_math || ! HONOR_NANS (mode))
2072 errno_set = false;
2074 /* Before working hard, check whether the instruction is available, but try
2075 to widen the mode for specific operations. */
2076 if ((optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2077 || (try_widening && !excess_precision_type (TREE_TYPE (exp))))
2078 && (!errno_set || !optimize_insn_for_size_p ()))
2080 rtx result = gen_reg_rtx (mode);
2082 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2083 need to expand the argument again. This way, we will not perform
2084 side-effects more the once. */
2085 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2087 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2089 start_sequence ();
2091 /* Compute into RESULT.
2092 Set RESULT to wherever the result comes back. */
2093 result = expand_unop (mode, builtin_optab, op0, result, 0);
2095 if (result != 0)
2097 if (errno_set)
2098 expand_errno_check (exp, result);
2100 /* Output the entire sequence. */
2101 insns = get_insns ();
2102 end_sequence ();
2103 emit_insn (insns);
2104 return result;
2107 /* If we were unable to expand via the builtin, stop the sequence
2108 (without outputting the insns) and call to the library function
2109 with the stabilized argument list. */
2110 end_sequence ();
2113 return expand_call (exp, target, target == const0_rtx);
2116 /* Expand a call to the builtin binary math functions (pow and atan2).
2117 Return NULL_RTX if a normal call should be emitted rather than expanding the
2118 function in-line. EXP is the expression that is a call to the builtin
2119 function; if convenient, the result should be placed in TARGET.
2120 SUBTARGET may be used as the target for computing one of EXP's
2121 operands. */
2123 static rtx
2124 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2126 optab builtin_optab;
2127 rtx op0, op1, result;
2128 rtx_insn *insns;
2129 int op1_type = REAL_TYPE;
2130 tree fndecl = get_callee_fndecl (exp);
2131 tree arg0, arg1;
2132 machine_mode mode;
2133 bool errno_set = true;
2135 switch (DECL_FUNCTION_CODE (fndecl))
2137 CASE_FLT_FN (BUILT_IN_SCALBN):
2138 CASE_FLT_FN (BUILT_IN_SCALBLN):
2139 CASE_FLT_FN (BUILT_IN_LDEXP):
2140 op1_type = INTEGER_TYPE;
2141 default:
2142 break;
2145 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2146 return NULL_RTX;
2148 arg0 = CALL_EXPR_ARG (exp, 0);
2149 arg1 = CALL_EXPR_ARG (exp, 1);
2151 switch (DECL_FUNCTION_CODE (fndecl))
2153 CASE_FLT_FN (BUILT_IN_POW):
2154 builtin_optab = pow_optab; break;
2155 CASE_FLT_FN (BUILT_IN_ATAN2):
2156 builtin_optab = atan2_optab; break;
2157 CASE_FLT_FN (BUILT_IN_SCALB):
2158 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2159 return 0;
2160 builtin_optab = scalb_optab; break;
2161 CASE_FLT_FN (BUILT_IN_SCALBN):
2162 CASE_FLT_FN (BUILT_IN_SCALBLN):
2163 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2164 return 0;
2165 /* Fall through... */
2166 CASE_FLT_FN (BUILT_IN_LDEXP):
2167 builtin_optab = ldexp_optab; break;
2168 CASE_FLT_FN (BUILT_IN_FMOD):
2169 builtin_optab = fmod_optab; break;
2170 CASE_FLT_FN (BUILT_IN_REMAINDER):
2171 CASE_FLT_FN (BUILT_IN_DREM):
2172 builtin_optab = remainder_optab; break;
2173 default:
2174 gcc_unreachable ();
2177 /* Make a suitable register to place result in. */
2178 mode = TYPE_MODE (TREE_TYPE (exp));
2180 /* Before working hard, check whether the instruction is available. */
2181 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2182 return NULL_RTX;
2184 result = gen_reg_rtx (mode);
2186 if (! flag_errno_math || ! HONOR_NANS (mode))
2187 errno_set = false;
2189 if (errno_set && optimize_insn_for_size_p ())
2190 return 0;
2192 /* Always stabilize the argument list. */
2193 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2194 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2196 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2197 op1 = expand_normal (arg1);
2199 start_sequence ();
2201 /* Compute into RESULT.
2202 Set RESULT to wherever the result comes back. */
2203 result = expand_binop (mode, builtin_optab, op0, op1,
2204 result, 0, OPTAB_DIRECT);
2206 /* If we were unable to expand via the builtin, stop the sequence
2207 (without outputting the insns) and call to the library function
2208 with the stabilized argument list. */
2209 if (result == 0)
2211 end_sequence ();
2212 return expand_call (exp, target, target == const0_rtx);
2215 if (errno_set)
2216 expand_errno_check (exp, result);
2218 /* Output the entire sequence. */
2219 insns = get_insns ();
2220 end_sequence ();
2221 emit_insn (insns);
2223 return result;
2226 /* Expand a call to the builtin trinary math functions (fma).
2227 Return NULL_RTX if a normal call should be emitted rather than expanding the
2228 function in-line. EXP is the expression that is a call to the builtin
2229 function; if convenient, the result should be placed in TARGET.
2230 SUBTARGET may be used as the target for computing one of EXP's
2231 operands. */
2233 static rtx
2234 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2236 optab builtin_optab;
2237 rtx op0, op1, op2, result;
2238 rtx_insn *insns;
2239 tree fndecl = get_callee_fndecl (exp);
2240 tree arg0, arg1, arg2;
2241 machine_mode mode;
2243 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2244 return NULL_RTX;
2246 arg0 = CALL_EXPR_ARG (exp, 0);
2247 arg1 = CALL_EXPR_ARG (exp, 1);
2248 arg2 = CALL_EXPR_ARG (exp, 2);
2250 switch (DECL_FUNCTION_CODE (fndecl))
2252 CASE_FLT_FN (BUILT_IN_FMA):
2253 builtin_optab = fma_optab; break;
2254 default:
2255 gcc_unreachable ();
2258 /* Make a suitable register to place result in. */
2259 mode = TYPE_MODE (TREE_TYPE (exp));
2261 /* Before working hard, check whether the instruction is available. */
2262 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2263 return NULL_RTX;
2265 result = gen_reg_rtx (mode);
2267 /* Always stabilize the argument list. */
2268 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2269 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2270 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2272 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2273 op1 = expand_normal (arg1);
2274 op2 = expand_normal (arg2);
2276 start_sequence ();
2278 /* Compute into RESULT.
2279 Set RESULT to wherever the result comes back. */
2280 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2281 result, 0);
2283 /* If we were unable to expand via the builtin, stop the sequence
2284 (without outputting the insns) and call to the library function
2285 with the stabilized argument list. */
2286 if (result == 0)
2288 end_sequence ();
2289 return expand_call (exp, target, target == const0_rtx);
2292 /* Output the entire sequence. */
2293 insns = get_insns ();
2294 end_sequence ();
2295 emit_insn (insns);
2297 return result;
2300 /* Expand a call to the builtin sin and cos math functions.
2301 Return NULL_RTX if a normal call should be emitted rather than expanding the
2302 function in-line. EXP is the expression that is a call to the builtin
2303 function; if convenient, the result should be placed in TARGET.
2304 SUBTARGET may be used as the target for computing one of EXP's
2305 operands. */
2307 static rtx
2308 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2310 optab builtin_optab;
2311 rtx op0;
2312 rtx_insn *insns;
2313 tree fndecl = get_callee_fndecl (exp);
2314 machine_mode mode;
2315 tree arg;
2317 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2318 return NULL_RTX;
2320 arg = CALL_EXPR_ARG (exp, 0);
2322 switch (DECL_FUNCTION_CODE (fndecl))
2324 CASE_FLT_FN (BUILT_IN_SIN):
2325 CASE_FLT_FN (BUILT_IN_COS):
2326 builtin_optab = sincos_optab; break;
2327 default:
2328 gcc_unreachable ();
2331 /* Make a suitable register to place result in. */
2332 mode = TYPE_MODE (TREE_TYPE (exp));
2334 /* Check if sincos insn is available, otherwise fallback
2335 to sin or cos insn. */
2336 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2337 switch (DECL_FUNCTION_CODE (fndecl))
2339 CASE_FLT_FN (BUILT_IN_SIN):
2340 builtin_optab = sin_optab; break;
2341 CASE_FLT_FN (BUILT_IN_COS):
2342 builtin_optab = cos_optab; break;
2343 default:
2344 gcc_unreachable ();
2347 /* Before working hard, check whether the instruction is available. */
2348 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2350 rtx result = gen_reg_rtx (mode);
2352 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2353 need to expand the argument again. This way, we will not perform
2354 side-effects more the once. */
2355 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2357 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2359 start_sequence ();
2361 /* Compute into RESULT.
2362 Set RESULT to wherever the result comes back. */
2363 if (builtin_optab == sincos_optab)
2365 int ok;
2367 switch (DECL_FUNCTION_CODE (fndecl))
2369 CASE_FLT_FN (BUILT_IN_SIN):
2370 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2371 break;
2372 CASE_FLT_FN (BUILT_IN_COS):
2373 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2374 break;
2375 default:
2376 gcc_unreachable ();
2378 gcc_assert (ok);
2380 else
2381 result = expand_unop (mode, builtin_optab, op0, result, 0);
2383 if (result != 0)
2385 /* Output the entire sequence. */
2386 insns = get_insns ();
2387 end_sequence ();
2388 emit_insn (insns);
2389 return result;
2392 /* If we were unable to expand via the builtin, stop the sequence
2393 (without outputting the insns) and call to the library function
2394 with the stabilized argument list. */
2395 end_sequence ();
2398 return expand_call (exp, target, target == const0_rtx);
2401 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2402 return an RTL instruction code that implements the functionality.
2403 If that isn't possible or available return CODE_FOR_nothing. */
2405 static enum insn_code
2406 interclass_mathfn_icode (tree arg, tree fndecl)
2408 bool errno_set = false;
2409 optab builtin_optab = unknown_optab;
2410 machine_mode mode;
2412 switch (DECL_FUNCTION_CODE (fndecl))
2414 CASE_FLT_FN (BUILT_IN_ILOGB):
2415 errno_set = true; builtin_optab = ilogb_optab; break;
2416 CASE_FLT_FN (BUILT_IN_ISINF):
2417 builtin_optab = isinf_optab; break;
2418 case BUILT_IN_ISNORMAL:
2419 case BUILT_IN_ISFINITE:
2420 CASE_FLT_FN (BUILT_IN_FINITE):
2421 case BUILT_IN_FINITED32:
2422 case BUILT_IN_FINITED64:
2423 case BUILT_IN_FINITED128:
2424 case BUILT_IN_ISINFD32:
2425 case BUILT_IN_ISINFD64:
2426 case BUILT_IN_ISINFD128:
2427 /* These builtins have no optabs (yet). */
2428 break;
2429 default:
2430 gcc_unreachable ();
2433 /* There's no easy way to detect the case we need to set EDOM. */
2434 if (flag_errno_math && errno_set)
2435 return CODE_FOR_nothing;
2437 /* Optab mode depends on the mode of the input argument. */
2438 mode = TYPE_MODE (TREE_TYPE (arg));
2440 if (builtin_optab)
2441 return optab_handler (builtin_optab, mode);
2442 return CODE_FOR_nothing;
2445 /* Expand a call to one of the builtin math functions that operate on
2446 floating point argument and output an integer result (ilogb, isinf,
2447 isnan, etc).
2448 Return 0 if a normal call should be emitted rather than expanding the
2449 function in-line. EXP is the expression that is a call to the builtin
2450 function; if convenient, the result should be placed in TARGET. */
2452 static rtx
2453 expand_builtin_interclass_mathfn (tree exp, rtx target)
2455 enum insn_code icode = CODE_FOR_nothing;
2456 rtx op0;
2457 tree fndecl = get_callee_fndecl (exp);
2458 machine_mode mode;
2459 tree arg;
2461 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2462 return NULL_RTX;
2464 arg = CALL_EXPR_ARG (exp, 0);
2465 icode = interclass_mathfn_icode (arg, fndecl);
2466 mode = TYPE_MODE (TREE_TYPE (arg));
2468 if (icode != CODE_FOR_nothing)
2470 struct expand_operand ops[1];
2471 rtx_insn *last = get_last_insn ();
2472 tree orig_arg = arg;
2474 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2475 need to expand the argument again. This way, we will not perform
2476 side-effects more the once. */
2477 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2479 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2481 if (mode != GET_MODE (op0))
2482 op0 = convert_to_mode (mode, op0, 0);
2484 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2485 if (maybe_legitimize_operands (icode, 0, 1, ops)
2486 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2487 return ops[0].value;
2489 delete_insns_since (last);
2490 CALL_EXPR_ARG (exp, 0) = orig_arg;
2493 return NULL_RTX;
2496 /* Expand a call to the builtin sincos math function.
2497 Return NULL_RTX if a normal call should be emitted rather than expanding the
2498 function in-line. EXP is the expression that is a call to the builtin
2499 function. */
2501 static rtx
2502 expand_builtin_sincos (tree exp)
2504 rtx op0, op1, op2, target1, target2;
2505 machine_mode mode;
2506 tree arg, sinp, cosp;
2507 int result;
2508 location_t loc = EXPR_LOCATION (exp);
2509 tree alias_type, alias_off;
2511 if (!validate_arglist (exp, REAL_TYPE,
2512 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2513 return NULL_RTX;
2515 arg = CALL_EXPR_ARG (exp, 0);
2516 sinp = CALL_EXPR_ARG (exp, 1);
2517 cosp = CALL_EXPR_ARG (exp, 2);
2519 /* Make a suitable register to place result in. */
2520 mode = TYPE_MODE (TREE_TYPE (arg));
2522 /* Check if sincos insn is available, otherwise emit the call. */
2523 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2524 return NULL_RTX;
2526 target1 = gen_reg_rtx (mode);
2527 target2 = gen_reg_rtx (mode);
2529 op0 = expand_normal (arg);
2530 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2531 alias_off = build_int_cst (alias_type, 0);
2532 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2533 sinp, alias_off));
2534 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2535 cosp, alias_off));
2537 /* Compute into target1 and target2.
2538 Set TARGET to wherever the result comes back. */
2539 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2540 gcc_assert (result);
2542 /* Move target1 and target2 to the memory locations indicated
2543 by op1 and op2. */
2544 emit_move_insn (op1, target1);
2545 emit_move_insn (op2, target2);
2547 return const0_rtx;
2550 /* Expand a call to the internal cexpi builtin to the sincos math function.
2551 EXP is the expression that is a call to the builtin function; if convenient,
2552 the result should be placed in TARGET. */
2554 static rtx
2555 expand_builtin_cexpi (tree exp, rtx target)
2557 tree fndecl = get_callee_fndecl (exp);
2558 tree arg, type;
2559 machine_mode mode;
2560 rtx op0, op1, op2;
2561 location_t loc = EXPR_LOCATION (exp);
2563 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2564 return NULL_RTX;
2566 arg = CALL_EXPR_ARG (exp, 0);
2567 type = TREE_TYPE (arg);
2568 mode = TYPE_MODE (TREE_TYPE (arg));
2570 /* Try expanding via a sincos optab, fall back to emitting a libcall
2571 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2572 is only generated from sincos, cexp or if we have either of them. */
2573 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2575 op1 = gen_reg_rtx (mode);
2576 op2 = gen_reg_rtx (mode);
2578 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2580 /* Compute into op1 and op2. */
2581 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2583 else if (targetm.libc_has_function (function_sincos))
2585 tree call, fn = NULL_TREE;
2586 tree top1, top2;
2587 rtx op1a, op2a;
2589 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2590 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2591 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2592 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2593 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2594 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2595 else
2596 gcc_unreachable ();
2598 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2599 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2600 op1a = copy_addr_to_reg (XEXP (op1, 0));
2601 op2a = copy_addr_to_reg (XEXP (op2, 0));
2602 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2603 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2605 /* Make sure not to fold the sincos call again. */
2606 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2607 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2608 call, 3, arg, top1, top2));
2610 else
2612 tree call, fn = NULL_TREE, narg;
2613 tree ctype = build_complex_type (type);
2615 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2616 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2617 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2618 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2619 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2620 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2621 else
2622 gcc_unreachable ();
2624 /* If we don't have a decl for cexp create one. This is the
2625 friendliest fallback if the user calls __builtin_cexpi
2626 without full target C99 function support. */
2627 if (fn == NULL_TREE)
2629 tree fntype;
2630 const char *name = NULL;
2632 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2633 name = "cexpf";
2634 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2635 name = "cexp";
2636 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2637 name = "cexpl";
2639 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2640 fn = build_fn_decl (name, fntype);
2643 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2644 build_real (type, dconst0), arg);
2646 /* Make sure not to fold the cexp call again. */
2647 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2648 return expand_expr (build_call_nary (ctype, call, 1, narg),
2649 target, VOIDmode, EXPAND_NORMAL);
2652 /* Now build the proper return type. */
2653 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2654 make_tree (TREE_TYPE (arg), op2),
2655 make_tree (TREE_TYPE (arg), op1)),
2656 target, VOIDmode, EXPAND_NORMAL);
2659 /* Conveniently construct a function call expression. FNDECL names the
2660 function to be called, N is the number of arguments, and the "..."
2661 parameters are the argument expressions. Unlike build_call_exr
2662 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2664 static tree
2665 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2667 va_list ap;
2668 tree fntype = TREE_TYPE (fndecl);
2669 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2671 va_start (ap, n);
2672 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2673 va_end (ap);
2674 SET_EXPR_LOCATION (fn, loc);
2675 return fn;
2678 /* Expand a call to one of the builtin rounding functions gcc defines
2679 as an extension (lfloor and lceil). As these are gcc extensions we
2680 do not need to worry about setting errno to EDOM.
2681 If expanding via optab fails, lower expression to (int)(floor(x)).
2682 EXP is the expression that is a call to the builtin function;
2683 if convenient, the result should be placed in TARGET. */
2685 static rtx
2686 expand_builtin_int_roundingfn (tree exp, rtx target)
2688 convert_optab builtin_optab;
2689 rtx op0, tmp;
2690 rtx_insn *insns;
2691 tree fndecl = get_callee_fndecl (exp);
2692 enum built_in_function fallback_fn;
2693 tree fallback_fndecl;
2694 machine_mode mode;
2695 tree arg;
2697 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2698 gcc_unreachable ();
2700 arg = CALL_EXPR_ARG (exp, 0);
2702 switch (DECL_FUNCTION_CODE (fndecl))
2704 CASE_FLT_FN (BUILT_IN_ICEIL):
2705 CASE_FLT_FN (BUILT_IN_LCEIL):
2706 CASE_FLT_FN (BUILT_IN_LLCEIL):
2707 builtin_optab = lceil_optab;
2708 fallback_fn = BUILT_IN_CEIL;
2709 break;
2711 CASE_FLT_FN (BUILT_IN_IFLOOR):
2712 CASE_FLT_FN (BUILT_IN_LFLOOR):
2713 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2714 builtin_optab = lfloor_optab;
2715 fallback_fn = BUILT_IN_FLOOR;
2716 break;
2718 default:
2719 gcc_unreachable ();
2722 /* Make a suitable register to place result in. */
2723 mode = TYPE_MODE (TREE_TYPE (exp));
2725 target = gen_reg_rtx (mode);
2727 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2728 need to expand the argument again. This way, we will not perform
2729 side-effects more the once. */
2730 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2732 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2734 start_sequence ();
2736 /* Compute into TARGET. */
2737 if (expand_sfix_optab (target, op0, builtin_optab))
2739 /* Output the entire sequence. */
2740 insns = get_insns ();
2741 end_sequence ();
2742 emit_insn (insns);
2743 return target;
2746 /* If we were unable to expand via the builtin, stop the sequence
2747 (without outputting the insns). */
2748 end_sequence ();
2750 /* Fall back to floating point rounding optab. */
2751 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2753 /* For non-C99 targets we may end up without a fallback fndecl here
2754 if the user called __builtin_lfloor directly. In this case emit
2755 a call to the floor/ceil variants nevertheless. This should result
2756 in the best user experience for not full C99 targets. */
2757 if (fallback_fndecl == NULL_TREE)
2759 tree fntype;
2760 const char *name = NULL;
2762 switch (DECL_FUNCTION_CODE (fndecl))
2764 case BUILT_IN_ICEIL:
2765 case BUILT_IN_LCEIL:
2766 case BUILT_IN_LLCEIL:
2767 name = "ceil";
2768 break;
2769 case BUILT_IN_ICEILF:
2770 case BUILT_IN_LCEILF:
2771 case BUILT_IN_LLCEILF:
2772 name = "ceilf";
2773 break;
2774 case BUILT_IN_ICEILL:
2775 case BUILT_IN_LCEILL:
2776 case BUILT_IN_LLCEILL:
2777 name = "ceill";
2778 break;
2779 case BUILT_IN_IFLOOR:
2780 case BUILT_IN_LFLOOR:
2781 case BUILT_IN_LLFLOOR:
2782 name = "floor";
2783 break;
2784 case BUILT_IN_IFLOORF:
2785 case BUILT_IN_LFLOORF:
2786 case BUILT_IN_LLFLOORF:
2787 name = "floorf";
2788 break;
2789 case BUILT_IN_IFLOORL:
2790 case BUILT_IN_LFLOORL:
2791 case BUILT_IN_LLFLOORL:
2792 name = "floorl";
2793 break;
2794 default:
2795 gcc_unreachable ();
2798 fntype = build_function_type_list (TREE_TYPE (arg),
2799 TREE_TYPE (arg), NULL_TREE);
2800 fallback_fndecl = build_fn_decl (name, fntype);
2803 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2805 tmp = expand_normal (exp);
2806 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2808 /* Truncate the result of floating point optab to integer
2809 via expand_fix (). */
2810 target = gen_reg_rtx (mode);
2811 expand_fix (target, tmp, 0);
2813 return target;
2816 /* Expand a call to one of the builtin math functions doing integer
2817 conversion (lrint).
2818 Return 0 if a normal call should be emitted rather than expanding the
2819 function in-line. EXP is the expression that is a call to the builtin
2820 function; if convenient, the result should be placed in TARGET. */
2822 static rtx
2823 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2825 convert_optab builtin_optab;
2826 rtx op0;
2827 rtx_insn *insns;
2828 tree fndecl = get_callee_fndecl (exp);
2829 tree arg;
2830 machine_mode mode;
2831 enum built_in_function fallback_fn = BUILT_IN_NONE;
2833 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2834 gcc_unreachable ();
2836 arg = CALL_EXPR_ARG (exp, 0);
2838 switch (DECL_FUNCTION_CODE (fndecl))
2840 CASE_FLT_FN (BUILT_IN_IRINT):
2841 fallback_fn = BUILT_IN_LRINT;
2842 /* FALLTHRU */
2843 CASE_FLT_FN (BUILT_IN_LRINT):
2844 CASE_FLT_FN (BUILT_IN_LLRINT):
2845 builtin_optab = lrint_optab;
2846 break;
2848 CASE_FLT_FN (BUILT_IN_IROUND):
2849 fallback_fn = BUILT_IN_LROUND;
2850 /* FALLTHRU */
2851 CASE_FLT_FN (BUILT_IN_LROUND):
2852 CASE_FLT_FN (BUILT_IN_LLROUND):
2853 builtin_optab = lround_optab;
2854 break;
2856 default:
2857 gcc_unreachable ();
2860 /* There's no easy way to detect the case we need to set EDOM. */
2861 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2862 return NULL_RTX;
2864 /* Make a suitable register to place result in. */
2865 mode = TYPE_MODE (TREE_TYPE (exp));
2867 /* There's no easy way to detect the case we need to set EDOM. */
2868 if (!flag_errno_math)
2870 rtx result = gen_reg_rtx (mode);
2872 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2873 need to expand the argument again. This way, we will not perform
2874 side-effects more the once. */
2875 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2877 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2879 start_sequence ();
2881 if (expand_sfix_optab (result, op0, builtin_optab))
2883 /* Output the entire sequence. */
2884 insns = get_insns ();
2885 end_sequence ();
2886 emit_insn (insns);
2887 return result;
2890 /* If we were unable to expand via the builtin, stop the sequence
2891 (without outputting the insns) and call to the library function
2892 with the stabilized argument list. */
2893 end_sequence ();
2896 if (fallback_fn != BUILT_IN_NONE)
2898 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2899 targets, (int) round (x) should never be transformed into
2900 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2901 a call to lround in the hope that the target provides at least some
2902 C99 functions. This should result in the best user experience for
2903 not full C99 targets. */
2904 tree fallback_fndecl = mathfn_built_in_1 (TREE_TYPE (arg),
2905 fallback_fn, 0);
2907 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2908 fallback_fndecl, 1, arg);
2910 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2911 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2912 return convert_to_mode (mode, target, 0);
2915 return expand_call (exp, target, target == const0_rtx);
2918 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2919 a normal call should be emitted rather than expanding the function
2920 in-line. EXP is the expression that is a call to the builtin
2921 function; if convenient, the result should be placed in TARGET. */
2923 static rtx
2924 expand_builtin_powi (tree exp, rtx target)
2926 tree arg0, arg1;
2927 rtx op0, op1;
2928 machine_mode mode;
2929 machine_mode mode2;
2931 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2932 return NULL_RTX;
2934 arg0 = CALL_EXPR_ARG (exp, 0);
2935 arg1 = CALL_EXPR_ARG (exp, 1);
2936 mode = TYPE_MODE (TREE_TYPE (exp));
2938 /* Emit a libcall to libgcc. */
2940 /* Mode of the 2nd argument must match that of an int. */
2941 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2943 if (target == NULL_RTX)
2944 target = gen_reg_rtx (mode);
2946 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2947 if (GET_MODE (op0) != mode)
2948 op0 = convert_to_mode (mode, op0, 0);
2949 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2950 if (GET_MODE (op1) != mode2)
2951 op1 = convert_to_mode (mode2, op1, 0);
2953 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2954 target, LCT_CONST, mode, 2,
2955 op0, mode, op1, mode2);
2957 return target;
2960 /* Expand expression EXP which is a call to the strlen builtin. Return
2961 NULL_RTX if we failed the caller should emit a normal call, otherwise
2962 try to get the result in TARGET, if convenient. */
2964 static rtx
2965 expand_builtin_strlen (tree exp, rtx target,
2966 machine_mode target_mode)
2968 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2969 return NULL_RTX;
2970 else
2972 struct expand_operand ops[4];
2973 rtx pat;
2974 tree len;
2975 tree src = CALL_EXPR_ARG (exp, 0);
2976 rtx src_reg;
2977 rtx_insn *before_strlen;
2978 machine_mode insn_mode = target_mode;
2979 enum insn_code icode = CODE_FOR_nothing;
2980 unsigned int align;
2982 /* If the length can be computed at compile-time, return it. */
2983 len = c_strlen (src, 0);
2984 if (len)
2985 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2987 /* If the length can be computed at compile-time and is constant
2988 integer, but there are side-effects in src, evaluate
2989 src for side-effects, then return len.
2990 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2991 can be optimized into: i++; x = 3; */
2992 len = c_strlen (src, 1);
2993 if (len && TREE_CODE (len) == INTEGER_CST)
2995 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2996 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2999 align = get_pointer_alignment (src) / BITS_PER_UNIT;
3001 /* If SRC is not a pointer type, don't do this operation inline. */
3002 if (align == 0)
3003 return NULL_RTX;
3005 /* Bail out if we can't compute strlen in the right mode. */
3006 while (insn_mode != VOIDmode)
3008 icode = optab_handler (strlen_optab, insn_mode);
3009 if (icode != CODE_FOR_nothing)
3010 break;
3012 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
3014 if (insn_mode == VOIDmode)
3015 return NULL_RTX;
3017 /* Make a place to hold the source address. We will not expand
3018 the actual source until we are sure that the expansion will
3019 not fail -- there are trees that cannot be expanded twice. */
3020 src_reg = gen_reg_rtx (Pmode);
3022 /* Mark the beginning of the strlen sequence so we can emit the
3023 source operand later. */
3024 before_strlen = get_last_insn ();
3026 create_output_operand (&ops[0], target, insn_mode);
3027 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3028 create_integer_operand (&ops[2], 0);
3029 create_integer_operand (&ops[3], align);
3030 if (!maybe_expand_insn (icode, 4, ops))
3031 return NULL_RTX;
3033 /* Now that we are assured of success, expand the source. */
3034 start_sequence ();
3035 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3036 if (pat != src_reg)
3038 #ifdef POINTERS_EXTEND_UNSIGNED
3039 if (GET_MODE (pat) != Pmode)
3040 pat = convert_to_mode (Pmode, pat,
3041 POINTERS_EXTEND_UNSIGNED);
3042 #endif
3043 emit_move_insn (src_reg, pat);
3045 pat = get_insns ();
3046 end_sequence ();
3048 if (before_strlen)
3049 emit_insn_after (pat, before_strlen);
3050 else
3051 emit_insn_before (pat, get_insns ());
3053 /* Return the value in the proper mode for this function. */
3054 if (GET_MODE (ops[0].value) == target_mode)
3055 target = ops[0].value;
3056 else if (target != 0)
3057 convert_move (target, ops[0].value, 0);
3058 else
3059 target = convert_to_mode (target_mode, ops[0].value, 0);
3061 return target;
3065 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3066 bytes from constant string DATA + OFFSET and return it as target
3067 constant. */
3069 static rtx
3070 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3071 machine_mode mode)
3073 const char *str = (const char *) data;
3075 gcc_assert (offset >= 0
3076 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3077 <= strlen (str) + 1));
3079 return c_readstr (str + offset, mode);
3082 /* LEN specify length of the block of memcpy/memset operation.
3083 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3084 In some cases we can make very likely guess on max size, then we
3085 set it into PROBABLE_MAX_SIZE. */
3087 static void
3088 determine_block_size (tree len, rtx len_rtx,
3089 unsigned HOST_WIDE_INT *min_size,
3090 unsigned HOST_WIDE_INT *max_size,
3091 unsigned HOST_WIDE_INT *probable_max_size)
3093 if (CONST_INT_P (len_rtx))
3095 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3096 return;
3098 else
3100 wide_int min, max;
3101 enum value_range_type range_type = VR_UNDEFINED;
3103 /* Determine bounds from the type. */
3104 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3105 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3106 else
3107 *min_size = 0;
3108 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3109 *probable_max_size = *max_size
3110 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3111 else
3112 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3114 if (TREE_CODE (len) == SSA_NAME)
3115 range_type = get_range_info (len, &min, &max);
3116 if (range_type == VR_RANGE)
3118 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3119 *min_size = min.to_uhwi ();
3120 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3121 *probable_max_size = *max_size = max.to_uhwi ();
3123 else if (range_type == VR_ANTI_RANGE)
3125 /* Anti range 0...N lets us to determine minimal size to N+1. */
3126 if (min == 0)
3128 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
3129 *min_size = max.to_uhwi () + 1;
3131 /* Code like
3133 int n;
3134 if (n < 100)
3135 memcpy (a, b, n)
3137 Produce anti range allowing negative values of N. We still
3138 can use the information and make a guess that N is not negative.
3140 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3141 *probable_max_size = min.to_uhwi () - 1;
3144 gcc_checking_assert (*max_size <=
3145 (unsigned HOST_WIDE_INT)
3146 GET_MODE_MASK (GET_MODE (len_rtx)));
3149 /* Helper function to do the actual work for expand_builtin_memcpy. */
3151 static rtx
3152 expand_builtin_memcpy_args (tree dest, tree src, tree len, rtx target, tree exp)
3154 const char *src_str;
3155 unsigned int src_align = get_pointer_alignment (src);
3156 unsigned int dest_align = get_pointer_alignment (dest);
3157 rtx dest_mem, src_mem, dest_addr, len_rtx;
3158 HOST_WIDE_INT expected_size = -1;
3159 unsigned int expected_align = 0;
3160 unsigned HOST_WIDE_INT min_size;
3161 unsigned HOST_WIDE_INT max_size;
3162 unsigned HOST_WIDE_INT probable_max_size;
3164 /* If DEST is not a pointer type, call the normal function. */
3165 if (dest_align == 0)
3166 return NULL_RTX;
3168 /* If either SRC is not a pointer type, don't do this
3169 operation in-line. */
3170 if (src_align == 0)
3171 return NULL_RTX;
3173 if (currently_expanding_gimple_stmt)
3174 stringop_block_profile (currently_expanding_gimple_stmt,
3175 &expected_align, &expected_size);
3177 if (expected_align < dest_align)
3178 expected_align = dest_align;
3179 dest_mem = get_memory_rtx (dest, len);
3180 set_mem_align (dest_mem, dest_align);
3181 len_rtx = expand_normal (len);
3182 determine_block_size (len, len_rtx, &min_size, &max_size,
3183 &probable_max_size);
3184 src_str = c_getstr (src);
3186 /* If SRC is a string constant and block move would be done
3187 by pieces, we can avoid loading the string from memory
3188 and only stored the computed constants. */
3189 if (src_str
3190 && CONST_INT_P (len_rtx)
3191 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3192 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3193 CONST_CAST (char *, src_str),
3194 dest_align, false))
3196 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3197 builtin_memcpy_read_str,
3198 CONST_CAST (char *, src_str),
3199 dest_align, false, 0);
3200 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3201 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3202 return dest_mem;
3205 src_mem = get_memory_rtx (src, len);
3206 set_mem_align (src_mem, src_align);
3208 /* Copy word part most expediently. */
3209 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3210 CALL_EXPR_TAILCALL (exp)
3211 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3212 expected_align, expected_size,
3213 min_size, max_size, probable_max_size);
3215 if (dest_addr == 0)
3217 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3218 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3221 return dest_addr;
3224 /* Expand a call EXP to the memcpy builtin.
3225 Return NULL_RTX if we failed, the caller should emit a normal call,
3226 otherwise try to get the result in TARGET, if convenient (and in
3227 mode MODE if that's convenient). */
3229 static rtx
3230 expand_builtin_memcpy (tree exp, rtx target)
3232 if (!validate_arglist (exp,
3233 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3234 return NULL_RTX;
3235 else
3237 tree dest = CALL_EXPR_ARG (exp, 0);
3238 tree src = CALL_EXPR_ARG (exp, 1);
3239 tree len = CALL_EXPR_ARG (exp, 2);
3240 return expand_builtin_memcpy_args (dest, src, len, target, exp);
3244 /* Expand an instrumented call EXP to the memcpy builtin.
3245 Return NULL_RTX if we failed, the caller should emit a normal call,
3246 otherwise try to get the result in TARGET, if convenient (and in
3247 mode MODE if that's convenient). */
3249 static rtx
3250 expand_builtin_memcpy_with_bounds (tree exp, rtx target)
3252 if (!validate_arglist (exp,
3253 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3254 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3255 INTEGER_TYPE, VOID_TYPE))
3256 return NULL_RTX;
3257 else
3259 tree dest = CALL_EXPR_ARG (exp, 0);
3260 tree src = CALL_EXPR_ARG (exp, 2);
3261 tree len = CALL_EXPR_ARG (exp, 4);
3262 rtx res = expand_builtin_memcpy_args (dest, src, len, target, exp);
3264 /* Return src bounds with the result. */
3265 if (res)
3267 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3268 expand_normal (CALL_EXPR_ARG (exp, 1)));
3269 res = chkp_join_splitted_slot (res, bnd);
3271 return res;
3275 /* Expand a call EXP to the mempcpy builtin.
3276 Return NULL_RTX if we failed; the caller should emit a normal call,
3277 otherwise try to get the result in TARGET, if convenient (and in
3278 mode MODE if that's convenient). If ENDP is 0 return the
3279 destination pointer, if ENDP is 1 return the end pointer ala
3280 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3281 stpcpy. */
3283 static rtx
3284 expand_builtin_mempcpy (tree exp, rtx target, machine_mode mode)
3286 if (!validate_arglist (exp,
3287 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3288 return NULL_RTX;
3289 else
3291 tree dest = CALL_EXPR_ARG (exp, 0);
3292 tree src = CALL_EXPR_ARG (exp, 1);
3293 tree len = CALL_EXPR_ARG (exp, 2);
3294 return expand_builtin_mempcpy_args (dest, src, len,
3295 target, mode, /*endp=*/ 1,
3296 exp);
3300 /* Expand an instrumented call EXP to the mempcpy builtin.
3301 Return NULL_RTX if we failed, the caller should emit a normal call,
3302 otherwise try to get the result in TARGET, if convenient (and in
3303 mode MODE if that's convenient). */
3305 static rtx
3306 expand_builtin_mempcpy_with_bounds (tree exp, rtx target, machine_mode mode)
3308 if (!validate_arglist (exp,
3309 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3310 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3311 INTEGER_TYPE, VOID_TYPE))
3312 return NULL_RTX;
3313 else
3315 tree dest = CALL_EXPR_ARG (exp, 0);
3316 tree src = CALL_EXPR_ARG (exp, 2);
3317 tree len = CALL_EXPR_ARG (exp, 4);
3318 rtx res = expand_builtin_mempcpy_args (dest, src, len, target,
3319 mode, 1, exp);
3321 /* Return src bounds with the result. */
3322 if (res)
3324 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3325 expand_normal (CALL_EXPR_ARG (exp, 1)));
3326 res = chkp_join_splitted_slot (res, bnd);
3328 return res;
3332 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3333 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3334 so that this can also be called without constructing an actual CALL_EXPR.
3335 The other arguments and return value are the same as for
3336 expand_builtin_mempcpy. */
3338 static rtx
3339 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3340 rtx target, machine_mode mode, int endp,
3341 tree orig_exp)
3343 tree fndecl = get_callee_fndecl (orig_exp);
3345 /* If return value is ignored, transform mempcpy into memcpy. */
3346 if (target == const0_rtx
3347 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
3348 && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP))
3350 tree fn = builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP);
3351 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3352 dest, src, len);
3353 return expand_expr (result, target, mode, EXPAND_NORMAL);
3355 else if (target == const0_rtx
3356 && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3358 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3359 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3360 dest, src, len);
3361 return expand_expr (result, target, mode, EXPAND_NORMAL);
3363 else
3365 const char *src_str;
3366 unsigned int src_align = get_pointer_alignment (src);
3367 unsigned int dest_align = get_pointer_alignment (dest);
3368 rtx dest_mem, src_mem, len_rtx;
3370 /* If either SRC or DEST is not a pointer type, don't do this
3371 operation in-line. */
3372 if (dest_align == 0 || src_align == 0)
3373 return NULL_RTX;
3375 /* If LEN is not constant, call the normal function. */
3376 if (! tree_fits_uhwi_p (len))
3377 return NULL_RTX;
3379 len_rtx = expand_normal (len);
3380 src_str = c_getstr (src);
3382 /* If SRC is a string constant and block move would be done
3383 by pieces, we can avoid loading the string from memory
3384 and only stored the computed constants. */
3385 if (src_str
3386 && CONST_INT_P (len_rtx)
3387 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3388 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3389 CONST_CAST (char *, src_str),
3390 dest_align, false))
3392 dest_mem = get_memory_rtx (dest, len);
3393 set_mem_align (dest_mem, dest_align);
3394 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3395 builtin_memcpy_read_str,
3396 CONST_CAST (char *, src_str),
3397 dest_align, false, endp);
3398 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3399 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3400 return dest_mem;
3403 if (CONST_INT_P (len_rtx)
3404 && can_move_by_pieces (INTVAL (len_rtx),
3405 MIN (dest_align, src_align)))
3407 dest_mem = get_memory_rtx (dest, len);
3408 set_mem_align (dest_mem, dest_align);
3409 src_mem = get_memory_rtx (src, len);
3410 set_mem_align (src_mem, src_align);
3411 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3412 MIN (dest_align, src_align), endp);
3413 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3414 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3415 return dest_mem;
3418 return NULL_RTX;
3422 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3423 we failed, the caller should emit a normal call, otherwise try to
3424 get the result in TARGET, if convenient. If ENDP is 0 return the
3425 destination pointer, if ENDP is 1 return the end pointer ala
3426 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3427 stpcpy. */
3429 static rtx
3430 expand_movstr (tree dest, tree src, rtx target, int endp)
3432 struct expand_operand ops[3];
3433 rtx dest_mem;
3434 rtx src_mem;
3436 if (!targetm.have_movstr ())
3437 return NULL_RTX;
3439 dest_mem = get_memory_rtx (dest, NULL);
3440 src_mem = get_memory_rtx (src, NULL);
3441 if (!endp)
3443 target = force_reg (Pmode, XEXP (dest_mem, 0));
3444 dest_mem = replace_equiv_address (dest_mem, target);
3447 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3448 create_fixed_operand (&ops[1], dest_mem);
3449 create_fixed_operand (&ops[2], src_mem);
3450 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3451 return NULL_RTX;
3453 if (endp && target != const0_rtx)
3455 target = ops[0].value;
3456 /* movstr is supposed to set end to the address of the NUL
3457 terminator. If the caller requested a mempcpy-like return value,
3458 adjust it. */
3459 if (endp == 1)
3461 rtx tem = plus_constant (GET_MODE (target),
3462 gen_lowpart (GET_MODE (target), target), 1);
3463 emit_move_insn (target, force_operand (tem, NULL_RTX));
3466 return target;
3469 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3470 NULL_RTX if we failed the caller should emit a normal call, otherwise
3471 try to get the result in TARGET, if convenient (and in mode MODE if that's
3472 convenient). */
3474 static rtx
3475 expand_builtin_strcpy (tree exp, rtx target)
3477 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3479 tree dest = CALL_EXPR_ARG (exp, 0);
3480 tree src = CALL_EXPR_ARG (exp, 1);
3481 return expand_builtin_strcpy_args (dest, src, target);
3483 return NULL_RTX;
3486 /* Helper function to do the actual work for expand_builtin_strcpy. The
3487 arguments to the builtin_strcpy call DEST and SRC are broken out
3488 so that this can also be called without constructing an actual CALL_EXPR.
3489 The other arguments and return value are the same as for
3490 expand_builtin_strcpy. */
3492 static rtx
3493 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3495 return expand_movstr (dest, src, target, /*endp=*/0);
3498 /* Expand a call EXP to the stpcpy builtin.
3499 Return NULL_RTX if we failed the caller should emit a normal call,
3500 otherwise try to get the result in TARGET, if convenient (and in
3501 mode MODE if that's convenient). */
3503 static rtx
3504 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3506 tree dst, src;
3507 location_t loc = EXPR_LOCATION (exp);
3509 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3510 return NULL_RTX;
3512 dst = CALL_EXPR_ARG (exp, 0);
3513 src = CALL_EXPR_ARG (exp, 1);
3515 /* If return value is ignored, transform stpcpy into strcpy. */
3516 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3518 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3519 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3520 return expand_expr (result, target, mode, EXPAND_NORMAL);
3522 else
3524 tree len, lenp1;
3525 rtx ret;
3527 /* Ensure we get an actual string whose length can be evaluated at
3528 compile-time, not an expression containing a string. This is
3529 because the latter will potentially produce pessimized code
3530 when used to produce the return value. */
3531 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3532 return expand_movstr (dst, src, target, /*endp=*/2);
3534 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3535 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3536 target, mode, /*endp=*/2,
3537 exp);
3539 if (ret)
3540 return ret;
3542 if (TREE_CODE (len) == INTEGER_CST)
3544 rtx len_rtx = expand_normal (len);
3546 if (CONST_INT_P (len_rtx))
3548 ret = expand_builtin_strcpy_args (dst, src, target);
3550 if (ret)
3552 if (! target)
3554 if (mode != VOIDmode)
3555 target = gen_reg_rtx (mode);
3556 else
3557 target = gen_reg_rtx (GET_MODE (ret));
3559 if (GET_MODE (target) != GET_MODE (ret))
3560 ret = gen_lowpart (GET_MODE (target), ret);
3562 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3563 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3564 gcc_assert (ret);
3566 return target;
3571 return expand_movstr (dst, src, target, /*endp=*/2);
3575 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3576 bytes from constant string DATA + OFFSET and return it as target
3577 constant. */
3580 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3581 machine_mode mode)
3583 const char *str = (const char *) data;
3585 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3586 return const0_rtx;
3588 return c_readstr (str + offset, mode);
3591 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3592 NULL_RTX if we failed the caller should emit a normal call. */
3594 static rtx
3595 expand_builtin_strncpy (tree exp, rtx target)
3597 location_t loc = EXPR_LOCATION (exp);
3599 if (validate_arglist (exp,
3600 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3602 tree dest = CALL_EXPR_ARG (exp, 0);
3603 tree src = CALL_EXPR_ARG (exp, 1);
3604 tree len = CALL_EXPR_ARG (exp, 2);
3605 tree slen = c_strlen (src, 1);
3607 /* We must be passed a constant len and src parameter. */
3608 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3609 return NULL_RTX;
3611 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3613 /* We're required to pad with trailing zeros if the requested
3614 len is greater than strlen(s2)+1. In that case try to
3615 use store_by_pieces, if it fails, punt. */
3616 if (tree_int_cst_lt (slen, len))
3618 unsigned int dest_align = get_pointer_alignment (dest);
3619 const char *p = c_getstr (src);
3620 rtx dest_mem;
3622 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3623 || !can_store_by_pieces (tree_to_uhwi (len),
3624 builtin_strncpy_read_str,
3625 CONST_CAST (char *, p),
3626 dest_align, false))
3627 return NULL_RTX;
3629 dest_mem = get_memory_rtx (dest, len);
3630 store_by_pieces (dest_mem, tree_to_uhwi (len),
3631 builtin_strncpy_read_str,
3632 CONST_CAST (char *, p), dest_align, false, 0);
3633 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3634 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3635 return dest_mem;
3638 return NULL_RTX;
3641 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3642 bytes from constant string DATA + OFFSET and return it as target
3643 constant. */
3646 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3647 machine_mode mode)
3649 const char *c = (const char *) data;
3650 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3652 memset (p, *c, GET_MODE_SIZE (mode));
3654 return c_readstr (p, mode);
3657 /* Callback routine for store_by_pieces. Return the RTL of a register
3658 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3659 char value given in the RTL register data. For example, if mode is
3660 4 bytes wide, return the RTL for 0x01010101*data. */
3662 static rtx
3663 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3664 machine_mode mode)
3666 rtx target, coeff;
3667 size_t size;
3668 char *p;
3670 size = GET_MODE_SIZE (mode);
3671 if (size == 1)
3672 return (rtx) data;
3674 p = XALLOCAVEC (char, size);
3675 memset (p, 1, size);
3676 coeff = c_readstr (p, mode);
3678 target = convert_to_mode (mode, (rtx) data, 1);
3679 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3680 return force_reg (mode, target);
3683 /* Expand expression EXP, which is a call to the memset builtin. Return
3684 NULL_RTX if we failed the caller should emit a normal call, otherwise
3685 try to get the result in TARGET, if convenient (and in mode MODE if that's
3686 convenient). */
3688 static rtx
3689 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
3691 if (!validate_arglist (exp,
3692 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3693 return NULL_RTX;
3694 else
3696 tree dest = CALL_EXPR_ARG (exp, 0);
3697 tree val = CALL_EXPR_ARG (exp, 1);
3698 tree len = CALL_EXPR_ARG (exp, 2);
3699 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3703 /* Expand expression EXP, which is an instrumented call to the memset builtin.
3704 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
3705 try to get the result in TARGET, if convenient (and in mode MODE if that's
3706 convenient). */
3708 static rtx
3709 expand_builtin_memset_with_bounds (tree exp, rtx target, machine_mode mode)
3711 if (!validate_arglist (exp,
3712 POINTER_TYPE, POINTER_BOUNDS_TYPE,
3713 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3714 return NULL_RTX;
3715 else
3717 tree dest = CALL_EXPR_ARG (exp, 0);
3718 tree val = CALL_EXPR_ARG (exp, 2);
3719 tree len = CALL_EXPR_ARG (exp, 3);
3720 rtx res = expand_builtin_memset_args (dest, val, len, target, mode, exp);
3722 /* Return src bounds with the result. */
3723 if (res)
3725 rtx bnd = force_reg (targetm.chkp_bound_mode (),
3726 expand_normal (CALL_EXPR_ARG (exp, 1)));
3727 res = chkp_join_splitted_slot (res, bnd);
3729 return res;
3733 /* Helper function to do the actual work for expand_builtin_memset. The
3734 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3735 so that this can also be called without constructing an actual CALL_EXPR.
3736 The other arguments and return value are the same as for
3737 expand_builtin_memset. */
3739 static rtx
3740 expand_builtin_memset_args (tree dest, tree val, tree len,
3741 rtx target, machine_mode mode, tree orig_exp)
3743 tree fndecl, fn;
3744 enum built_in_function fcode;
3745 machine_mode val_mode;
3746 char c;
3747 unsigned int dest_align;
3748 rtx dest_mem, dest_addr, len_rtx;
3749 HOST_WIDE_INT expected_size = -1;
3750 unsigned int expected_align = 0;
3751 unsigned HOST_WIDE_INT min_size;
3752 unsigned HOST_WIDE_INT max_size;
3753 unsigned HOST_WIDE_INT probable_max_size;
3755 dest_align = get_pointer_alignment (dest);
3757 /* If DEST is not a pointer type, don't do this operation in-line. */
3758 if (dest_align == 0)
3759 return NULL_RTX;
3761 if (currently_expanding_gimple_stmt)
3762 stringop_block_profile (currently_expanding_gimple_stmt,
3763 &expected_align, &expected_size);
3765 if (expected_align < dest_align)
3766 expected_align = dest_align;
3768 /* If the LEN parameter is zero, return DEST. */
3769 if (integer_zerop (len))
3771 /* Evaluate and ignore VAL in case it has side-effects. */
3772 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3773 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3776 /* Stabilize the arguments in case we fail. */
3777 dest = builtin_save_expr (dest);
3778 val = builtin_save_expr (val);
3779 len = builtin_save_expr (len);
3781 len_rtx = expand_normal (len);
3782 determine_block_size (len, len_rtx, &min_size, &max_size,
3783 &probable_max_size);
3784 dest_mem = get_memory_rtx (dest, len);
3785 val_mode = TYPE_MODE (unsigned_char_type_node);
3787 if (TREE_CODE (val) != INTEGER_CST)
3789 rtx val_rtx;
3791 val_rtx = expand_normal (val);
3792 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3794 /* Assume that we can memset by pieces if we can store
3795 * the coefficients by pieces (in the required modes).
3796 * We can't pass builtin_memset_gen_str as that emits RTL. */
3797 c = 1;
3798 if (tree_fits_uhwi_p (len)
3799 && can_store_by_pieces (tree_to_uhwi (len),
3800 builtin_memset_read_str, &c, dest_align,
3801 true))
3803 val_rtx = force_reg (val_mode, val_rtx);
3804 store_by_pieces (dest_mem, tree_to_uhwi (len),
3805 builtin_memset_gen_str, val_rtx, dest_align,
3806 true, 0);
3808 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3809 dest_align, expected_align,
3810 expected_size, min_size, max_size,
3811 probable_max_size))
3812 goto do_libcall;
3814 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3815 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3816 return dest_mem;
3819 if (target_char_cast (val, &c))
3820 goto do_libcall;
3822 if (c)
3824 if (tree_fits_uhwi_p (len)
3825 && can_store_by_pieces (tree_to_uhwi (len),
3826 builtin_memset_read_str, &c, dest_align,
3827 true))
3828 store_by_pieces (dest_mem, tree_to_uhwi (len),
3829 builtin_memset_read_str, &c, dest_align, true, 0);
3830 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3831 gen_int_mode (c, val_mode),
3832 dest_align, expected_align,
3833 expected_size, min_size, max_size,
3834 probable_max_size))
3835 goto do_libcall;
3837 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3838 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3839 return dest_mem;
3842 set_mem_align (dest_mem, dest_align);
3843 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3844 CALL_EXPR_TAILCALL (orig_exp)
3845 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3846 expected_align, expected_size,
3847 min_size, max_size,
3848 probable_max_size);
3850 if (dest_addr == 0)
3852 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3853 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3856 return dest_addr;
3858 do_libcall:
3859 fndecl = get_callee_fndecl (orig_exp);
3860 fcode = DECL_FUNCTION_CODE (fndecl);
3861 if (fcode == BUILT_IN_MEMSET
3862 || fcode == BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP)
3863 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3864 dest, val, len);
3865 else if (fcode == BUILT_IN_BZERO)
3866 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3867 dest, len);
3868 else
3869 gcc_unreachable ();
3870 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3871 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3872 return expand_call (fn, target, target == const0_rtx);
3875 /* Expand expression EXP, which is a call to the bzero builtin. Return
3876 NULL_RTX if we failed the caller should emit a normal call. */
3878 static rtx
3879 expand_builtin_bzero (tree exp)
3881 tree dest, size;
3882 location_t loc = EXPR_LOCATION (exp);
3884 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3885 return NULL_RTX;
3887 dest = CALL_EXPR_ARG (exp, 0);
3888 size = CALL_EXPR_ARG (exp, 1);
3890 /* New argument list transforming bzero(ptr x, int y) to
3891 memset(ptr x, int 0, size_t y). This is done this way
3892 so that if it isn't expanded inline, we fallback to
3893 calling bzero instead of memset. */
3895 return expand_builtin_memset_args (dest, integer_zero_node,
3896 fold_convert_loc (loc,
3897 size_type_node, size),
3898 const0_rtx, VOIDmode, exp);
3901 /* Try to expand cmpstr operation ICODE with the given operands.
3902 Return the result rtx on success, otherwise return null. */
3904 static rtx
3905 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
3906 HOST_WIDE_INT align)
3908 machine_mode insn_mode = insn_data[icode].operand[0].mode;
3910 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
3911 target = NULL_RTX;
3913 struct expand_operand ops[4];
3914 create_output_operand (&ops[0], target, insn_mode);
3915 create_fixed_operand (&ops[1], arg1_rtx);
3916 create_fixed_operand (&ops[2], arg2_rtx);
3917 create_integer_operand (&ops[3], align);
3918 if (maybe_expand_insn (icode, 4, ops))
3919 return ops[0].value;
3920 return NULL_RTX;
3923 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
3924 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
3925 otherwise return null. */
3927 static rtx
3928 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
3929 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
3930 HOST_WIDE_INT align)
3932 machine_mode insn_mode = insn_data[icode].operand[0].mode;
3934 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
3935 target = NULL_RTX;
3937 struct expand_operand ops[5];
3938 create_output_operand (&ops[0], target, insn_mode);
3939 create_fixed_operand (&ops[1], arg1_rtx);
3940 create_fixed_operand (&ops[2], arg2_rtx);
3941 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
3942 TYPE_UNSIGNED (arg3_type));
3943 create_integer_operand (&ops[4], align);
3944 if (maybe_expand_insn (icode, 5, ops))
3945 return ops[0].value;
3946 return NULL_RTX;
3949 /* Expand expression EXP, which is a call to the memcmp built-in function.
3950 Return NULL_RTX if we failed and the caller should emit a normal call,
3951 otherwise try to get the result in TARGET, if convenient. */
3953 static rtx
3954 expand_builtin_memcmp (tree exp, rtx target)
3956 if (!validate_arglist (exp,
3957 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3958 return NULL_RTX;
3960 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3961 implementing memcmp because it will stop if it encounters two
3962 zero bytes. */
3963 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
3964 if (icode == CODE_FOR_nothing)
3965 return NULL_RTX;
3967 tree arg1 = CALL_EXPR_ARG (exp, 0);
3968 tree arg2 = CALL_EXPR_ARG (exp, 1);
3969 tree len = CALL_EXPR_ARG (exp, 2);
3971 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3972 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3974 /* If we don't have POINTER_TYPE, call the function. */
3975 if (arg1_align == 0 || arg2_align == 0)
3976 return NULL_RTX;
3978 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
3979 location_t loc = EXPR_LOCATION (exp);
3980 rtx arg1_rtx = get_memory_rtx (arg1, len);
3981 rtx arg2_rtx = get_memory_rtx (arg2, len);
3982 rtx arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3984 /* Set MEM_SIZE as appropriate. */
3985 if (CONST_INT_P (arg3_rtx))
3987 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3988 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3991 rtx result = expand_cmpstrn_or_cmpmem (icode, target, arg1_rtx, arg2_rtx,
3992 TREE_TYPE (len), arg3_rtx,
3993 MIN (arg1_align, arg2_align));
3994 if (result)
3996 /* Return the value in the proper mode for this function. */
3997 if (GET_MODE (result) == mode)
3998 return result;
4000 if (target != 0)
4002 convert_move (target, result, 0);
4003 return target;
4006 return convert_to_mode (mode, result, 0);
4009 result = target;
4010 if (! (result != 0
4011 && REG_P (result) && GET_MODE (result) == mode
4012 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4013 result = gen_reg_rtx (mode);
4015 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
4016 TYPE_MODE (integer_type_node), 3,
4017 XEXP (arg1_rtx, 0), Pmode,
4018 XEXP (arg2_rtx, 0), Pmode,
4019 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
4020 TYPE_UNSIGNED (sizetype)),
4021 TYPE_MODE (sizetype));
4022 return result;
4025 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4026 if we failed the caller should emit a normal call, otherwise try to get
4027 the result in TARGET, if convenient. */
4029 static rtx
4030 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4032 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4033 return NULL_RTX;
4035 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4036 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4037 if (cmpstr_icode != CODE_FOR_nothing || cmpstrn_icode != CODE_FOR_nothing)
4039 rtx arg1_rtx, arg2_rtx;
4040 tree fndecl, fn;
4041 tree arg1 = CALL_EXPR_ARG (exp, 0);
4042 tree arg2 = CALL_EXPR_ARG (exp, 1);
4043 rtx result = NULL_RTX;
4045 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4046 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4048 /* If we don't have POINTER_TYPE, call the function. */
4049 if (arg1_align == 0 || arg2_align == 0)
4050 return NULL_RTX;
4052 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4053 arg1 = builtin_save_expr (arg1);
4054 arg2 = builtin_save_expr (arg2);
4056 arg1_rtx = get_memory_rtx (arg1, NULL);
4057 arg2_rtx = get_memory_rtx (arg2, NULL);
4059 /* Try to call cmpstrsi. */
4060 if (cmpstr_icode != CODE_FOR_nothing)
4061 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4062 MIN (arg1_align, arg2_align));
4064 /* Try to determine at least one length and call cmpstrnsi. */
4065 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4067 tree len;
4068 rtx arg3_rtx;
4070 tree len1 = c_strlen (arg1, 1);
4071 tree len2 = c_strlen (arg2, 1);
4073 if (len1)
4074 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4075 if (len2)
4076 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4078 /* If we don't have a constant length for the first, use the length
4079 of the second, if we know it. We don't require a constant for
4080 this case; some cost analysis could be done if both are available
4081 but neither is constant. For now, assume they're equally cheap,
4082 unless one has side effects. If both strings have constant lengths,
4083 use the smaller. */
4085 if (!len1)
4086 len = len2;
4087 else if (!len2)
4088 len = len1;
4089 else if (TREE_SIDE_EFFECTS (len1))
4090 len = len2;
4091 else if (TREE_SIDE_EFFECTS (len2))
4092 len = len1;
4093 else if (TREE_CODE (len1) != INTEGER_CST)
4094 len = len2;
4095 else if (TREE_CODE (len2) != INTEGER_CST)
4096 len = len1;
4097 else if (tree_int_cst_lt (len1, len2))
4098 len = len1;
4099 else
4100 len = len2;
4102 /* If both arguments have side effects, we cannot optimize. */
4103 if (len && !TREE_SIDE_EFFECTS (len))
4105 arg3_rtx = expand_normal (len);
4106 result = expand_cmpstrn_or_cmpmem
4107 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4108 arg3_rtx, MIN (arg1_align, arg2_align));
4112 if (result)
4114 /* Return the value in the proper mode for this function. */
4115 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4116 if (GET_MODE (result) == mode)
4117 return result;
4118 if (target == 0)
4119 return convert_to_mode (mode, result, 0);
4120 convert_move (target, result, 0);
4121 return target;
4124 /* Expand the library call ourselves using a stabilized argument
4125 list to avoid re-evaluating the function's arguments twice. */
4126 fndecl = get_callee_fndecl (exp);
4127 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4128 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4129 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4130 return expand_call (fn, target, target == const0_rtx);
4132 return NULL_RTX;
4135 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4136 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4137 the result in TARGET, if convenient. */
4139 static rtx
4140 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4141 ATTRIBUTE_UNUSED machine_mode mode)
4143 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
4145 if (!validate_arglist (exp,
4146 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4147 return NULL_RTX;
4149 /* If c_strlen can determine an expression for one of the string
4150 lengths, and it doesn't have side effects, then emit cmpstrnsi
4151 using length MIN(strlen(string)+1, arg3). */
4152 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4153 if (cmpstrn_icode != CODE_FOR_nothing)
4155 tree len, len1, len2;
4156 rtx arg1_rtx, arg2_rtx, arg3_rtx;
4157 rtx result;
4158 tree fndecl, fn;
4159 tree arg1 = CALL_EXPR_ARG (exp, 0);
4160 tree arg2 = CALL_EXPR_ARG (exp, 1);
4161 tree arg3 = CALL_EXPR_ARG (exp, 2);
4163 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4164 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4166 len1 = c_strlen (arg1, 1);
4167 len2 = c_strlen (arg2, 1);
4169 if (len1)
4170 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4171 if (len2)
4172 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4174 /* If we don't have a constant length for the first, use the length
4175 of the second, if we know it. We don't require a constant for
4176 this case; some cost analysis could be done if both are available
4177 but neither is constant. For now, assume they're equally cheap,
4178 unless one has side effects. If both strings have constant lengths,
4179 use the smaller. */
4181 if (!len1)
4182 len = len2;
4183 else if (!len2)
4184 len = len1;
4185 else if (TREE_SIDE_EFFECTS (len1))
4186 len = len2;
4187 else if (TREE_SIDE_EFFECTS (len2))
4188 len = len1;
4189 else if (TREE_CODE (len1) != INTEGER_CST)
4190 len = len2;
4191 else if (TREE_CODE (len2) != INTEGER_CST)
4192 len = len1;
4193 else if (tree_int_cst_lt (len1, len2))
4194 len = len1;
4195 else
4196 len = len2;
4198 /* If both arguments have side effects, we cannot optimize. */
4199 if (!len || TREE_SIDE_EFFECTS (len))
4200 return NULL_RTX;
4202 /* The actual new length parameter is MIN(len,arg3). */
4203 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
4204 fold_convert_loc (loc, TREE_TYPE (len), arg3));
4206 /* If we don't have POINTER_TYPE, call the function. */
4207 if (arg1_align == 0 || arg2_align == 0)
4208 return NULL_RTX;
4210 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4211 arg1 = builtin_save_expr (arg1);
4212 arg2 = builtin_save_expr (arg2);
4213 len = builtin_save_expr (len);
4215 arg1_rtx = get_memory_rtx (arg1, len);
4216 arg2_rtx = get_memory_rtx (arg2, len);
4217 arg3_rtx = expand_normal (len);
4218 result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4219 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4220 MIN (arg1_align, arg2_align));
4221 if (result)
4223 /* Return the value in the proper mode for this function. */
4224 mode = TYPE_MODE (TREE_TYPE (exp));
4225 if (GET_MODE (result) == mode)
4226 return result;
4227 if (target == 0)
4228 return convert_to_mode (mode, result, 0);
4229 convert_move (target, result, 0);
4230 return target;
4233 /* Expand the library call ourselves using a stabilized argument
4234 list to avoid re-evaluating the function's arguments twice. */
4235 fndecl = get_callee_fndecl (exp);
4236 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4237 arg1, arg2, len);
4238 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4239 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4240 return expand_call (fn, target, target == const0_rtx);
4242 return NULL_RTX;
4245 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4246 if that's convenient. */
4249 expand_builtin_saveregs (void)
4251 rtx val;
4252 rtx_insn *seq;
4254 /* Don't do __builtin_saveregs more than once in a function.
4255 Save the result of the first call and reuse it. */
4256 if (saveregs_value != 0)
4257 return saveregs_value;
4259 /* When this function is called, it means that registers must be
4260 saved on entry to this function. So we migrate the call to the
4261 first insn of this function. */
4263 start_sequence ();
4265 /* Do whatever the machine needs done in this case. */
4266 val = targetm.calls.expand_builtin_saveregs ();
4268 seq = get_insns ();
4269 end_sequence ();
4271 saveregs_value = val;
4273 /* Put the insns after the NOTE that starts the function. If this
4274 is inside a start_sequence, make the outer-level insn chain current, so
4275 the code is placed at the start of the function. */
4276 push_topmost_sequence ();
4277 emit_insn_after (seq, entry_of_function ());
4278 pop_topmost_sequence ();
4280 return val;
4283 /* Expand a call to __builtin_next_arg. */
4285 static rtx
4286 expand_builtin_next_arg (void)
4288 /* Checking arguments is already done in fold_builtin_next_arg
4289 that must be called before this function. */
4290 return expand_binop (ptr_mode, add_optab,
4291 crtl->args.internal_arg_pointer,
4292 crtl->args.arg_offset_rtx,
4293 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4296 /* Make it easier for the backends by protecting the valist argument
4297 from multiple evaluations. */
4299 static tree
4300 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4302 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4304 /* The current way of determining the type of valist is completely
4305 bogus. We should have the information on the va builtin instead. */
4306 if (!vatype)
4307 vatype = targetm.fn_abi_va_list (cfun->decl);
4309 if (TREE_CODE (vatype) == ARRAY_TYPE)
4311 if (TREE_SIDE_EFFECTS (valist))
4312 valist = save_expr (valist);
4314 /* For this case, the backends will be expecting a pointer to
4315 vatype, but it's possible we've actually been given an array
4316 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4317 So fix it. */
4318 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4320 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4321 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4324 else
4326 tree pt = build_pointer_type (vatype);
4328 if (! needs_lvalue)
4330 if (! TREE_SIDE_EFFECTS (valist))
4331 return valist;
4333 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4334 TREE_SIDE_EFFECTS (valist) = 1;
4337 if (TREE_SIDE_EFFECTS (valist))
4338 valist = save_expr (valist);
4339 valist = fold_build2_loc (loc, MEM_REF,
4340 vatype, valist, build_int_cst (pt, 0));
4343 return valist;
4346 /* The "standard" definition of va_list is void*. */
4348 tree
4349 std_build_builtin_va_list (void)
4351 return ptr_type_node;
4354 /* The "standard" abi va_list is va_list_type_node. */
4356 tree
4357 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4359 return va_list_type_node;
4362 /* The "standard" type of va_list is va_list_type_node. */
4364 tree
4365 std_canonical_va_list_type (tree type)
4367 tree wtype, htype;
4369 if (INDIRECT_REF_P (type))
4370 type = TREE_TYPE (type);
4371 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE (type)))
4372 type = TREE_TYPE (type);
4373 wtype = va_list_type_node;
4374 htype = type;
4375 /* Treat structure va_list types. */
4376 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4377 htype = TREE_TYPE (htype);
4378 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4380 /* If va_list is an array type, the argument may have decayed
4381 to a pointer type, e.g. by being passed to another function.
4382 In that case, unwrap both types so that we can compare the
4383 underlying records. */
4384 if (TREE_CODE (htype) == ARRAY_TYPE
4385 || POINTER_TYPE_P (htype))
4387 wtype = TREE_TYPE (wtype);
4388 htype = TREE_TYPE (htype);
4391 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4392 return va_list_type_node;
4394 return NULL_TREE;
4397 /* The "standard" implementation of va_start: just assign `nextarg' to
4398 the variable. */
4400 void
4401 std_expand_builtin_va_start (tree valist, rtx nextarg)
4403 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4404 convert_move (va_r, nextarg, 0);
4406 /* We do not have any valid bounds for the pointer, so
4407 just store zero bounds for it. */
4408 if (chkp_function_instrumented_p (current_function_decl))
4409 chkp_expand_bounds_reset_for_mem (valist,
4410 make_tree (TREE_TYPE (valist),
4411 nextarg));
4414 /* Expand EXP, a call to __builtin_va_start. */
4416 static rtx
4417 expand_builtin_va_start (tree exp)
4419 rtx nextarg;
4420 tree valist;
4421 location_t loc = EXPR_LOCATION (exp);
4423 if (call_expr_nargs (exp) < 2)
4425 error_at (loc, "too few arguments to function %<va_start%>");
4426 return const0_rtx;
4429 if (fold_builtin_next_arg (exp, true))
4430 return const0_rtx;
4432 nextarg = expand_builtin_next_arg ();
4433 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4435 if (targetm.expand_builtin_va_start)
4436 targetm.expand_builtin_va_start (valist, nextarg);
4437 else
4438 std_expand_builtin_va_start (valist, nextarg);
4440 return const0_rtx;
4443 /* Expand EXP, a call to __builtin_va_end. */
4445 static rtx
4446 expand_builtin_va_end (tree exp)
4448 tree valist = CALL_EXPR_ARG (exp, 0);
4450 /* Evaluate for side effects, if needed. I hate macros that don't
4451 do that. */
4452 if (TREE_SIDE_EFFECTS (valist))
4453 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4455 return const0_rtx;
4458 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4459 builtin rather than just as an assignment in stdarg.h because of the
4460 nastiness of array-type va_list types. */
4462 static rtx
4463 expand_builtin_va_copy (tree exp)
4465 tree dst, src, t;
4466 location_t loc = EXPR_LOCATION (exp);
4468 dst = CALL_EXPR_ARG (exp, 0);
4469 src = CALL_EXPR_ARG (exp, 1);
4471 dst = stabilize_va_list_loc (loc, dst, 1);
4472 src = stabilize_va_list_loc (loc, src, 0);
4474 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4476 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4478 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4479 TREE_SIDE_EFFECTS (t) = 1;
4480 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4482 else
4484 rtx dstb, srcb, size;
4486 /* Evaluate to pointers. */
4487 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4488 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4489 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4490 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4492 dstb = convert_memory_address (Pmode, dstb);
4493 srcb = convert_memory_address (Pmode, srcb);
4495 /* "Dereference" to BLKmode memories. */
4496 dstb = gen_rtx_MEM (BLKmode, dstb);
4497 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4498 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4499 srcb = gen_rtx_MEM (BLKmode, srcb);
4500 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4501 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4503 /* Copy. */
4504 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4507 return const0_rtx;
4510 /* Expand a call to one of the builtin functions __builtin_frame_address or
4511 __builtin_return_address. */
4513 static rtx
4514 expand_builtin_frame_address (tree fndecl, tree exp)
4516 /* The argument must be a nonnegative integer constant.
4517 It counts the number of frames to scan up the stack.
4518 The value is either the frame pointer value or the return
4519 address saved in that frame. */
4520 if (call_expr_nargs (exp) == 0)
4521 /* Warning about missing arg was already issued. */
4522 return const0_rtx;
4523 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4525 error ("invalid argument to %qD", fndecl);
4526 return const0_rtx;
4528 else
4530 /* Number of frames to scan up the stack. */
4531 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
4533 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
4535 /* Some ports cannot access arbitrary stack frames. */
4536 if (tem == NULL)
4538 warning (0, "unsupported argument to %qD", fndecl);
4539 return const0_rtx;
4542 if (count)
4544 /* Warn since no effort is made to ensure that any frame
4545 beyond the current one exists or can be safely reached. */
4546 warning (OPT_Wframe_address, "calling %qD with "
4547 "a nonzero argument is unsafe", fndecl);
4550 /* For __builtin_frame_address, return what we've got. */
4551 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4552 return tem;
4554 if (!REG_P (tem)
4555 && ! CONSTANT_P (tem))
4556 tem = copy_addr_to_reg (tem);
4557 return tem;
4561 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4562 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4563 is the same as for allocate_dynamic_stack_space. */
4565 static rtx
4566 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4568 rtx op0;
4569 rtx result;
4570 bool valid_arglist;
4571 unsigned int align;
4572 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4573 == BUILT_IN_ALLOCA_WITH_ALIGN);
4575 valid_arglist
4576 = (alloca_with_align
4577 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4578 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4580 if (!valid_arglist)
4581 return NULL_RTX;
4583 /* Compute the argument. */
4584 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4586 /* Compute the alignment. */
4587 align = (alloca_with_align
4588 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4589 : BIGGEST_ALIGNMENT);
4591 /* Allocate the desired space. */
4592 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4593 result = convert_memory_address (ptr_mode, result);
4595 return result;
4598 /* Expand a call to bswap builtin in EXP.
4599 Return NULL_RTX if a normal call should be emitted rather than expanding the
4600 function in-line. If convenient, the result should be placed in TARGET.
4601 SUBTARGET may be used as the target for computing one of EXP's operands. */
4603 static rtx
4604 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
4605 rtx subtarget)
4607 tree arg;
4608 rtx op0;
4610 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4611 return NULL_RTX;
4613 arg = CALL_EXPR_ARG (exp, 0);
4614 op0 = expand_expr (arg,
4615 subtarget && GET_MODE (subtarget) == target_mode
4616 ? subtarget : NULL_RTX,
4617 target_mode, EXPAND_NORMAL);
4618 if (GET_MODE (op0) != target_mode)
4619 op0 = convert_to_mode (target_mode, op0, 1);
4621 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
4623 gcc_assert (target);
4625 return convert_to_mode (target_mode, target, 1);
4628 /* Expand a call to a unary builtin in EXP.
4629 Return NULL_RTX if a normal call should be emitted rather than expanding the
4630 function in-line. If convenient, the result should be placed in TARGET.
4631 SUBTARGET may be used as the target for computing one of EXP's operands. */
4633 static rtx
4634 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
4635 rtx subtarget, optab op_optab)
4637 rtx op0;
4639 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4640 return NULL_RTX;
4642 /* Compute the argument. */
4643 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4644 (subtarget
4645 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4646 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4647 VOIDmode, EXPAND_NORMAL);
4648 /* Compute op, into TARGET if possible.
4649 Set TARGET to wherever the result comes back. */
4650 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4651 op_optab, op0, target, op_optab != clrsb_optab);
4652 gcc_assert (target);
4654 return convert_to_mode (target_mode, target, 0);
4657 /* Expand a call to __builtin_expect. We just return our argument
4658 as the builtin_expect semantic should've been already executed by
4659 tree branch prediction pass. */
4661 static rtx
4662 expand_builtin_expect (tree exp, rtx target)
4664 tree arg;
4666 if (call_expr_nargs (exp) < 2)
4667 return const0_rtx;
4668 arg = CALL_EXPR_ARG (exp, 0);
4670 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4671 /* When guessing was done, the hints should be already stripped away. */
4672 gcc_assert (!flag_guess_branch_prob
4673 || optimize == 0 || seen_error ());
4674 return target;
4677 /* Expand a call to __builtin_assume_aligned. We just return our first
4678 argument as the builtin_assume_aligned semantic should've been already
4679 executed by CCP. */
4681 static rtx
4682 expand_builtin_assume_aligned (tree exp, rtx target)
4684 if (call_expr_nargs (exp) < 2)
4685 return const0_rtx;
4686 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4687 EXPAND_NORMAL);
4688 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4689 && (call_expr_nargs (exp) < 3
4690 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4691 return target;
4694 void
4695 expand_builtin_trap (void)
4697 if (targetm.have_trap ())
4699 rtx_insn *insn = emit_insn (targetm.gen_trap ());
4700 /* For trap insns when not accumulating outgoing args force
4701 REG_ARGS_SIZE note to prevent crossjumping of calls with
4702 different args sizes. */
4703 if (!ACCUMULATE_OUTGOING_ARGS)
4704 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4706 else
4707 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4708 emit_barrier ();
4711 /* Expand a call to __builtin_unreachable. We do nothing except emit
4712 a barrier saying that control flow will not pass here.
4714 It is the responsibility of the program being compiled to ensure
4715 that control flow does never reach __builtin_unreachable. */
4716 static void
4717 expand_builtin_unreachable (void)
4719 emit_barrier ();
4722 /* Expand EXP, a call to fabs, fabsf or fabsl.
4723 Return NULL_RTX if a normal call should be emitted rather than expanding
4724 the function inline. If convenient, the result should be placed
4725 in TARGET. SUBTARGET may be used as the target for computing
4726 the operand. */
4728 static rtx
4729 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4731 machine_mode mode;
4732 tree arg;
4733 rtx op0;
4735 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4736 return NULL_RTX;
4738 arg = CALL_EXPR_ARG (exp, 0);
4739 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4740 mode = TYPE_MODE (TREE_TYPE (arg));
4741 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4742 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4745 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4746 Return NULL is a normal call should be emitted rather than expanding the
4747 function inline. If convenient, the result should be placed in TARGET.
4748 SUBTARGET may be used as the target for computing the operand. */
4750 static rtx
4751 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4753 rtx op0, op1;
4754 tree arg;
4756 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4757 return NULL_RTX;
4759 arg = CALL_EXPR_ARG (exp, 0);
4760 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4762 arg = CALL_EXPR_ARG (exp, 1);
4763 op1 = expand_normal (arg);
4765 return expand_copysign (op0, op1, target);
4768 /* Expand a call to __builtin___clear_cache. */
4770 static rtx
4771 expand_builtin___clear_cache (tree exp)
4773 if (!targetm.code_for_clear_cache)
4775 #ifdef CLEAR_INSN_CACHE
4776 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4777 does something. Just do the default expansion to a call to
4778 __clear_cache(). */
4779 return NULL_RTX;
4780 #else
4781 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4782 does nothing. There is no need to call it. Do nothing. */
4783 return const0_rtx;
4784 #endif /* CLEAR_INSN_CACHE */
4787 /* We have a "clear_cache" insn, and it will handle everything. */
4788 tree begin, end;
4789 rtx begin_rtx, end_rtx;
4791 /* We must not expand to a library call. If we did, any
4792 fallback library function in libgcc that might contain a call to
4793 __builtin___clear_cache() would recurse infinitely. */
4794 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4796 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4797 return const0_rtx;
4800 if (targetm.have_clear_cache ())
4802 struct expand_operand ops[2];
4804 begin = CALL_EXPR_ARG (exp, 0);
4805 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4807 end = CALL_EXPR_ARG (exp, 1);
4808 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4810 create_address_operand (&ops[0], begin_rtx);
4811 create_address_operand (&ops[1], end_rtx);
4812 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
4813 return const0_rtx;
4815 return const0_rtx;
4818 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4820 static rtx
4821 round_trampoline_addr (rtx tramp)
4823 rtx temp, addend, mask;
4825 /* If we don't need too much alignment, we'll have been guaranteed
4826 proper alignment by get_trampoline_type. */
4827 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4828 return tramp;
4830 /* Round address up to desired boundary. */
4831 temp = gen_reg_rtx (Pmode);
4832 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
4833 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
4835 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4836 temp, 0, OPTAB_LIB_WIDEN);
4837 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4838 temp, 0, OPTAB_LIB_WIDEN);
4840 return tramp;
4843 static rtx
4844 expand_builtin_init_trampoline (tree exp, bool onstack)
4846 tree t_tramp, t_func, t_chain;
4847 rtx m_tramp, r_tramp, r_chain, tmp;
4849 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4850 POINTER_TYPE, VOID_TYPE))
4851 return NULL_RTX;
4853 t_tramp = CALL_EXPR_ARG (exp, 0);
4854 t_func = CALL_EXPR_ARG (exp, 1);
4855 t_chain = CALL_EXPR_ARG (exp, 2);
4857 r_tramp = expand_normal (t_tramp);
4858 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4859 MEM_NOTRAP_P (m_tramp) = 1;
4861 /* If ONSTACK, the TRAMP argument should be the address of a field
4862 within the local function's FRAME decl. Either way, let's see if
4863 we can fill in the MEM_ATTRs for this memory. */
4864 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4865 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
4867 /* Creator of a heap trampoline is responsible for making sure the
4868 address is aligned to at least STACK_BOUNDARY. Normally malloc
4869 will ensure this anyhow. */
4870 tmp = round_trampoline_addr (r_tramp);
4871 if (tmp != r_tramp)
4873 m_tramp = change_address (m_tramp, BLKmode, tmp);
4874 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4875 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4878 /* The FUNC argument should be the address of the nested function.
4879 Extract the actual function decl to pass to the hook. */
4880 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4881 t_func = TREE_OPERAND (t_func, 0);
4882 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4884 r_chain = expand_normal (t_chain);
4886 /* Generate insns to initialize the trampoline. */
4887 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4889 if (onstack)
4891 trampolines_created = 1;
4893 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4894 "trampoline generated for nested function %qD", t_func);
4897 return const0_rtx;
4900 static rtx
4901 expand_builtin_adjust_trampoline (tree exp)
4903 rtx tramp;
4905 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4906 return NULL_RTX;
4908 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4909 tramp = round_trampoline_addr (tramp);
4910 if (targetm.calls.trampoline_adjust_address)
4911 tramp = targetm.calls.trampoline_adjust_address (tramp);
4913 return tramp;
4916 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4917 function. The function first checks whether the back end provides
4918 an insn to implement signbit for the respective mode. If not, it
4919 checks whether the floating point format of the value is such that
4920 the sign bit can be extracted. If that is not the case, error out.
4921 EXP is the expression that is a call to the builtin function; if
4922 convenient, the result should be placed in TARGET. */
4923 static rtx
4924 expand_builtin_signbit (tree exp, rtx target)
4926 const struct real_format *fmt;
4927 machine_mode fmode, imode, rmode;
4928 tree arg;
4929 int word, bitpos;
4930 enum insn_code icode;
4931 rtx temp;
4932 location_t loc = EXPR_LOCATION (exp);
4934 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4935 return NULL_RTX;
4937 arg = CALL_EXPR_ARG (exp, 0);
4938 fmode = TYPE_MODE (TREE_TYPE (arg));
4939 rmode = TYPE_MODE (TREE_TYPE (exp));
4940 fmt = REAL_MODE_FORMAT (fmode);
4942 arg = builtin_save_expr (arg);
4944 /* Expand the argument yielding a RTX expression. */
4945 temp = expand_normal (arg);
4947 /* Check if the back end provides an insn that handles signbit for the
4948 argument's mode. */
4949 icode = optab_handler (signbit_optab, fmode);
4950 if (icode != CODE_FOR_nothing)
4952 rtx_insn *last = get_last_insn ();
4953 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4954 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4955 return target;
4956 delete_insns_since (last);
4959 /* For floating point formats without a sign bit, implement signbit
4960 as "ARG < 0.0". */
4961 bitpos = fmt->signbit_ro;
4962 if (bitpos < 0)
4964 /* But we can't do this if the format supports signed zero. */
4965 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
4967 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4968 build_real (TREE_TYPE (arg), dconst0));
4969 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4972 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4974 imode = int_mode_for_mode (fmode);
4975 gcc_assert (imode != BLKmode);
4976 temp = gen_lowpart (imode, temp);
4978 else
4980 imode = word_mode;
4981 /* Handle targets with different FP word orders. */
4982 if (FLOAT_WORDS_BIG_ENDIAN)
4983 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4984 else
4985 word = bitpos / BITS_PER_WORD;
4986 temp = operand_subword_force (temp, word, fmode);
4987 bitpos = bitpos % BITS_PER_WORD;
4990 /* Force the intermediate word_mode (or narrower) result into a
4991 register. This avoids attempting to create paradoxical SUBREGs
4992 of floating point modes below. */
4993 temp = force_reg (imode, temp);
4995 /* If the bitpos is within the "result mode" lowpart, the operation
4996 can be implement with a single bitwise AND. Otherwise, we need
4997 a right shift and an AND. */
4999 if (bitpos < GET_MODE_BITSIZE (rmode))
5001 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5003 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5004 temp = gen_lowpart (rmode, temp);
5005 temp = expand_binop (rmode, and_optab, temp,
5006 immed_wide_int_const (mask, rmode),
5007 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5009 else
5011 /* Perform a logical right shift to place the signbit in the least
5012 significant bit, then truncate the result to the desired mode
5013 and mask just this bit. */
5014 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5015 temp = gen_lowpart (rmode, temp);
5016 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5017 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5020 return temp;
5023 /* Expand fork or exec calls. TARGET is the desired target of the
5024 call. EXP is the call. FN is the
5025 identificator of the actual function. IGNORE is nonzero if the
5026 value is to be ignored. */
5028 static rtx
5029 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5031 tree id, decl;
5032 tree call;
5034 /* If we are not profiling, just call the function. */
5035 if (!profile_arc_flag)
5036 return NULL_RTX;
5038 /* Otherwise call the wrapper. This should be equivalent for the rest of
5039 compiler, so the code does not diverge, and the wrapper may run the
5040 code necessary for keeping the profiling sane. */
5042 switch (DECL_FUNCTION_CODE (fn))
5044 case BUILT_IN_FORK:
5045 id = get_identifier ("__gcov_fork");
5046 break;
5048 case BUILT_IN_EXECL:
5049 id = get_identifier ("__gcov_execl");
5050 break;
5052 case BUILT_IN_EXECV:
5053 id = get_identifier ("__gcov_execv");
5054 break;
5056 case BUILT_IN_EXECLP:
5057 id = get_identifier ("__gcov_execlp");
5058 break;
5060 case BUILT_IN_EXECLE:
5061 id = get_identifier ("__gcov_execle");
5062 break;
5064 case BUILT_IN_EXECVP:
5065 id = get_identifier ("__gcov_execvp");
5066 break;
5068 case BUILT_IN_EXECVE:
5069 id = get_identifier ("__gcov_execve");
5070 break;
5072 default:
5073 gcc_unreachable ();
5076 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5077 FUNCTION_DECL, id, TREE_TYPE (fn));
5078 DECL_EXTERNAL (decl) = 1;
5079 TREE_PUBLIC (decl) = 1;
5080 DECL_ARTIFICIAL (decl) = 1;
5081 TREE_NOTHROW (decl) = 1;
5082 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5083 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5084 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5085 return expand_call (call, target, ignore);
5090 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5091 the pointer in these functions is void*, the tree optimizers may remove
5092 casts. The mode computed in expand_builtin isn't reliable either, due
5093 to __sync_bool_compare_and_swap.
5095 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5096 group of builtins. This gives us log2 of the mode size. */
5098 static inline machine_mode
5099 get_builtin_sync_mode (int fcode_diff)
5101 /* The size is not negotiable, so ask not to get BLKmode in return
5102 if the target indicates that a smaller size would be better. */
5103 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5106 /* Expand the memory expression LOC and return the appropriate memory operand
5107 for the builtin_sync operations. */
5109 static rtx
5110 get_builtin_sync_mem (tree loc, machine_mode mode)
5112 rtx addr, mem;
5114 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5115 addr = convert_memory_address (Pmode, addr);
5117 /* Note that we explicitly do not want any alias information for this
5118 memory, so that we kill all other live memories. Otherwise we don't
5119 satisfy the full barrier semantics of the intrinsic. */
5120 mem = validize_mem (gen_rtx_MEM (mode, addr));
5122 /* The alignment needs to be at least according to that of the mode. */
5123 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5124 get_pointer_alignment (loc)));
5125 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5126 MEM_VOLATILE_P (mem) = 1;
5128 return mem;
5131 /* Make sure an argument is in the right mode.
5132 EXP is the tree argument.
5133 MODE is the mode it should be in. */
5135 static rtx
5136 expand_expr_force_mode (tree exp, machine_mode mode)
5138 rtx val;
5139 machine_mode old_mode;
5141 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5142 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5143 of CONST_INTs, where we know the old_mode only from the call argument. */
5145 old_mode = GET_MODE (val);
5146 if (old_mode == VOIDmode)
5147 old_mode = TYPE_MODE (TREE_TYPE (exp));
5148 val = convert_modes (mode, old_mode, val, 1);
5149 return val;
5153 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5154 EXP is the CALL_EXPR. CODE is the rtx code
5155 that corresponds to the arithmetic or logical operation from the name;
5156 an exception here is that NOT actually means NAND. TARGET is an optional
5157 place for us to store the results; AFTER is true if this is the
5158 fetch_and_xxx form. */
5160 static rtx
5161 expand_builtin_sync_operation (machine_mode mode, tree exp,
5162 enum rtx_code code, bool after,
5163 rtx target)
5165 rtx val, mem;
5166 location_t loc = EXPR_LOCATION (exp);
5168 if (code == NOT && warn_sync_nand)
5170 tree fndecl = get_callee_fndecl (exp);
5171 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5173 static bool warned_f_a_n, warned_n_a_f;
5175 switch (fcode)
5177 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5178 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5179 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5180 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5181 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5182 if (warned_f_a_n)
5183 break;
5185 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5186 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5187 warned_f_a_n = true;
5188 break;
5190 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5191 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5192 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5193 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5194 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5195 if (warned_n_a_f)
5196 break;
5198 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5199 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5200 warned_n_a_f = true;
5201 break;
5203 default:
5204 gcc_unreachable ();
5208 /* Expand the operands. */
5209 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5210 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5212 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
5213 after);
5216 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5217 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5218 true if this is the boolean form. TARGET is a place for us to store the
5219 results; this is NOT optional if IS_BOOL is true. */
5221 static rtx
5222 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5223 bool is_bool, rtx target)
5225 rtx old_val, new_val, mem;
5226 rtx *pbool, *poval;
5228 /* Expand the operands. */
5229 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5230 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5231 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5233 pbool = poval = NULL;
5234 if (target != const0_rtx)
5236 if (is_bool)
5237 pbool = &target;
5238 else
5239 poval = &target;
5241 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5242 false, MEMMODEL_SYNC_SEQ_CST,
5243 MEMMODEL_SYNC_SEQ_CST))
5244 return NULL_RTX;
5246 return target;
5249 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5250 general form is actually an atomic exchange, and some targets only
5251 support a reduced form with the second argument being a constant 1.
5252 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5253 the results. */
5255 static rtx
5256 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5257 rtx target)
5259 rtx val, mem;
5261 /* Expand the operands. */
5262 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5263 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5265 return expand_sync_lock_test_and_set (target, mem, val);
5268 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5270 static void
5271 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5273 rtx mem;
5275 /* Expand the operands. */
5276 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5278 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
5281 /* Given an integer representing an ``enum memmodel'', verify its
5282 correctness and return the memory model enum. */
5284 static enum memmodel
5285 get_memmodel (tree exp)
5287 rtx op;
5288 unsigned HOST_WIDE_INT val;
5290 /* If the parameter is not a constant, it's a run time value so we'll just
5291 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5292 if (TREE_CODE (exp) != INTEGER_CST)
5293 return MEMMODEL_SEQ_CST;
5295 op = expand_normal (exp);
5297 val = INTVAL (op);
5298 if (targetm.memmodel_check)
5299 val = targetm.memmodel_check (val);
5300 else if (val & ~MEMMODEL_MASK)
5302 warning (OPT_Winvalid_memory_model,
5303 "Unknown architecture specifier in memory model to builtin.");
5304 return MEMMODEL_SEQ_CST;
5307 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5308 if (memmodel_base (val) >= MEMMODEL_LAST)
5310 warning (OPT_Winvalid_memory_model,
5311 "invalid memory model argument to builtin");
5312 return MEMMODEL_SEQ_CST;
5315 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5316 be conservative and promote consume to acquire. */
5317 if (val == MEMMODEL_CONSUME)
5318 val = MEMMODEL_ACQUIRE;
5320 return (enum memmodel) val;
5323 /* Expand the __atomic_exchange intrinsic:
5324 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5325 EXP is the CALL_EXPR.
5326 TARGET is an optional place for us to store the results. */
5328 static rtx
5329 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5331 rtx val, mem;
5332 enum memmodel model;
5334 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5336 if (!flag_inline_atomics)
5337 return NULL_RTX;
5339 /* Expand the operands. */
5340 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5341 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5343 return expand_atomic_exchange (target, mem, val, model);
5346 /* Expand the __atomic_compare_exchange intrinsic:
5347 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5348 TYPE desired, BOOL weak,
5349 enum memmodel success,
5350 enum memmodel failure)
5351 EXP is the CALL_EXPR.
5352 TARGET is an optional place for us to store the results. */
5354 static rtx
5355 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5356 rtx target)
5358 rtx expect, desired, mem, oldval;
5359 rtx_code_label *label;
5360 enum memmodel success, failure;
5361 tree weak;
5362 bool is_weak;
5364 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5365 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5367 if (failure > success)
5369 warning (OPT_Winvalid_memory_model,
5370 "failure memory model cannot be stronger than success memory "
5371 "model for %<__atomic_compare_exchange%>");
5372 success = MEMMODEL_SEQ_CST;
5375 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5377 warning (OPT_Winvalid_memory_model,
5378 "invalid failure memory model for "
5379 "%<__atomic_compare_exchange%>");
5380 failure = MEMMODEL_SEQ_CST;
5381 success = MEMMODEL_SEQ_CST;
5385 if (!flag_inline_atomics)
5386 return NULL_RTX;
5388 /* Expand the operands. */
5389 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5391 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5392 expect = convert_memory_address (Pmode, expect);
5393 expect = gen_rtx_MEM (mode, expect);
5394 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5396 weak = CALL_EXPR_ARG (exp, 3);
5397 is_weak = false;
5398 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5399 is_weak = true;
5401 if (target == const0_rtx)
5402 target = NULL;
5404 /* Lest the rtl backend create a race condition with an imporoper store
5405 to memory, always create a new pseudo for OLDVAL. */
5406 oldval = NULL;
5408 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5409 is_weak, success, failure))
5410 return NULL_RTX;
5412 /* Conditionally store back to EXPECT, lest we create a race condition
5413 with an improper store to memory. */
5414 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5415 the normal case where EXPECT is totally private, i.e. a register. At
5416 which point the store can be unconditional. */
5417 label = gen_label_rtx ();
5418 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
5419 GET_MODE (target), 1, label);
5420 emit_move_insn (expect, oldval);
5421 emit_label (label);
5423 return target;
5426 /* Expand the __atomic_load intrinsic:
5427 TYPE __atomic_load (TYPE *object, enum memmodel)
5428 EXP is the CALL_EXPR.
5429 TARGET is an optional place for us to store the results. */
5431 static rtx
5432 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
5434 rtx mem;
5435 enum memmodel model;
5437 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5438 if (is_mm_release (model) || is_mm_acq_rel (model))
5440 warning (OPT_Winvalid_memory_model,
5441 "invalid memory model for %<__atomic_load%>");
5442 model = MEMMODEL_SEQ_CST;
5445 if (!flag_inline_atomics)
5446 return NULL_RTX;
5448 /* Expand the operand. */
5449 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5451 return expand_atomic_load (target, mem, model);
5455 /* Expand the __atomic_store intrinsic:
5456 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5457 EXP is the CALL_EXPR.
5458 TARGET is an optional place for us to store the results. */
5460 static rtx
5461 expand_builtin_atomic_store (machine_mode mode, tree exp)
5463 rtx mem, val;
5464 enum memmodel model;
5466 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5467 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
5468 || is_mm_release (model)))
5470 warning (OPT_Winvalid_memory_model,
5471 "invalid memory model for %<__atomic_store%>");
5472 model = MEMMODEL_SEQ_CST;
5475 if (!flag_inline_atomics)
5476 return NULL_RTX;
5478 /* Expand the operands. */
5479 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5480 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5482 return expand_atomic_store (mem, val, model, false);
5485 /* Expand the __atomic_fetch_XXX intrinsic:
5486 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5487 EXP is the CALL_EXPR.
5488 TARGET is an optional place for us to store the results.
5489 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5490 FETCH_AFTER is true if returning the result of the operation.
5491 FETCH_AFTER is false if returning the value before the operation.
5492 IGNORE is true if the result is not used.
5493 EXT_CALL is the correct builtin for an external call if this cannot be
5494 resolved to an instruction sequence. */
5496 static rtx
5497 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
5498 enum rtx_code code, bool fetch_after,
5499 bool ignore, enum built_in_function ext_call)
5501 rtx val, mem, ret;
5502 enum memmodel model;
5503 tree fndecl;
5504 tree addr;
5506 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5508 /* Expand the operands. */
5509 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5510 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5512 /* Only try generating instructions if inlining is turned on. */
5513 if (flag_inline_atomics)
5515 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5516 if (ret)
5517 return ret;
5520 /* Return if a different routine isn't needed for the library call. */
5521 if (ext_call == BUILT_IN_NONE)
5522 return NULL_RTX;
5524 /* Change the call to the specified function. */
5525 fndecl = get_callee_fndecl (exp);
5526 addr = CALL_EXPR_FN (exp);
5527 STRIP_NOPS (addr);
5529 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5530 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
5532 /* Expand the call here so we can emit trailing code. */
5533 ret = expand_call (exp, target, ignore);
5535 /* Replace the original function just in case it matters. */
5536 TREE_OPERAND (addr, 0) = fndecl;
5538 /* Then issue the arithmetic correction to return the right result. */
5539 if (!ignore)
5541 if (code == NOT)
5543 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5544 OPTAB_LIB_WIDEN);
5545 ret = expand_simple_unop (mode, NOT, ret, target, true);
5547 else
5548 ret = expand_simple_binop (mode, code, ret, val, target, true,
5549 OPTAB_LIB_WIDEN);
5551 return ret;
5554 /* Expand an atomic clear operation.
5555 void _atomic_clear (BOOL *obj, enum memmodel)
5556 EXP is the call expression. */
5558 static rtx
5559 expand_builtin_atomic_clear (tree exp)
5561 machine_mode mode;
5562 rtx mem, ret;
5563 enum memmodel model;
5565 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5566 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5567 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5569 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
5571 warning (OPT_Winvalid_memory_model,
5572 "invalid memory model for %<__atomic_store%>");
5573 model = MEMMODEL_SEQ_CST;
5576 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5577 Failing that, a store is issued by __atomic_store. The only way this can
5578 fail is if the bool type is larger than a word size. Unlikely, but
5579 handle it anyway for completeness. Assume a single threaded model since
5580 there is no atomic support in this case, and no barriers are required. */
5581 ret = expand_atomic_store (mem, const0_rtx, model, true);
5582 if (!ret)
5583 emit_move_insn (mem, const0_rtx);
5584 return const0_rtx;
5587 /* Expand an atomic test_and_set operation.
5588 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5589 EXP is the call expression. */
5591 static rtx
5592 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5594 rtx mem;
5595 enum memmodel model;
5596 machine_mode mode;
5598 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5599 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5600 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5602 return expand_atomic_test_and_set (target, mem, model);
5606 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5607 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5609 static tree
5610 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5612 int size;
5613 machine_mode mode;
5614 unsigned int mode_align, type_align;
5616 if (TREE_CODE (arg0) != INTEGER_CST)
5617 return NULL_TREE;
5619 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5620 mode = mode_for_size (size, MODE_INT, 0);
5621 mode_align = GET_MODE_ALIGNMENT (mode);
5623 if (TREE_CODE (arg1) == INTEGER_CST)
5625 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
5627 /* Either this argument is null, or it's a fake pointer encoding
5628 the alignment of the object. */
5629 val = val & -val;
5630 val *= BITS_PER_UNIT;
5632 if (val == 0 || mode_align < val)
5633 type_align = mode_align;
5634 else
5635 type_align = val;
5637 else
5639 tree ttype = TREE_TYPE (arg1);
5641 /* This function is usually invoked and folded immediately by the front
5642 end before anything else has a chance to look at it. The pointer
5643 parameter at this point is usually cast to a void *, so check for that
5644 and look past the cast. */
5645 if (CONVERT_EXPR_P (arg1) && POINTER_TYPE_P (ttype)
5646 && VOID_TYPE_P (TREE_TYPE (ttype)))
5647 arg1 = TREE_OPERAND (arg1, 0);
5649 ttype = TREE_TYPE (arg1);
5650 gcc_assert (POINTER_TYPE_P (ttype));
5652 /* Get the underlying type of the object. */
5653 ttype = TREE_TYPE (ttype);
5654 type_align = TYPE_ALIGN (ttype);
5657 /* If the object has smaller alignment, the lock free routines cannot
5658 be used. */
5659 if (type_align < mode_align)
5660 return boolean_false_node;
5662 /* Check if a compare_and_swap pattern exists for the mode which represents
5663 the required size. The pattern is not allowed to fail, so the existence
5664 of the pattern indicates support is present. */
5665 if (can_compare_and_swap_p (mode, true))
5666 return boolean_true_node;
5667 else
5668 return boolean_false_node;
5671 /* Return true if the parameters to call EXP represent an object which will
5672 always generate lock free instructions. The first argument represents the
5673 size of the object, and the second parameter is a pointer to the object
5674 itself. If NULL is passed for the object, then the result is based on
5675 typical alignment for an object of the specified size. Otherwise return
5676 false. */
5678 static rtx
5679 expand_builtin_atomic_always_lock_free (tree exp)
5681 tree size;
5682 tree arg0 = CALL_EXPR_ARG (exp, 0);
5683 tree arg1 = CALL_EXPR_ARG (exp, 1);
5685 if (TREE_CODE (arg0) != INTEGER_CST)
5687 error ("non-constant argument 1 to __atomic_always_lock_free");
5688 return const0_rtx;
5691 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5692 if (size == boolean_true_node)
5693 return const1_rtx;
5694 return const0_rtx;
5697 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5698 is lock free on this architecture. */
5700 static tree
5701 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5703 if (!flag_inline_atomics)
5704 return NULL_TREE;
5706 /* If it isn't always lock free, don't generate a result. */
5707 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
5708 return boolean_true_node;
5710 return NULL_TREE;
5713 /* Return true if the parameters to call EXP represent an object which will
5714 always generate lock free instructions. The first argument represents the
5715 size of the object, and the second parameter is a pointer to the object
5716 itself. If NULL is passed for the object, then the result is based on
5717 typical alignment for an object of the specified size. Otherwise return
5718 NULL*/
5720 static rtx
5721 expand_builtin_atomic_is_lock_free (tree exp)
5723 tree size;
5724 tree arg0 = CALL_EXPR_ARG (exp, 0);
5725 tree arg1 = CALL_EXPR_ARG (exp, 1);
5727 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5729 error ("non-integer argument 1 to __atomic_is_lock_free");
5730 return NULL_RTX;
5733 if (!flag_inline_atomics)
5734 return NULL_RTX;
5736 /* If the value is known at compile time, return the RTX for it. */
5737 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5738 if (size == boolean_true_node)
5739 return const1_rtx;
5741 return NULL_RTX;
5744 /* Expand the __atomic_thread_fence intrinsic:
5745 void __atomic_thread_fence (enum memmodel)
5746 EXP is the CALL_EXPR. */
5748 static void
5749 expand_builtin_atomic_thread_fence (tree exp)
5751 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5752 expand_mem_thread_fence (model);
5755 /* Expand the __atomic_signal_fence intrinsic:
5756 void __atomic_signal_fence (enum memmodel)
5757 EXP is the CALL_EXPR. */
5759 static void
5760 expand_builtin_atomic_signal_fence (tree exp)
5762 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5763 expand_mem_signal_fence (model);
5766 /* Expand the __sync_synchronize intrinsic. */
5768 static void
5769 expand_builtin_sync_synchronize (void)
5771 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
5774 static rtx
5775 expand_builtin_thread_pointer (tree exp, rtx target)
5777 enum insn_code icode;
5778 if (!validate_arglist (exp, VOID_TYPE))
5779 return const0_rtx;
5780 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
5781 if (icode != CODE_FOR_nothing)
5783 struct expand_operand op;
5784 /* If the target is not sutitable then create a new target. */
5785 if (target == NULL_RTX
5786 || !REG_P (target)
5787 || GET_MODE (target) != Pmode)
5788 target = gen_reg_rtx (Pmode);
5789 create_output_operand (&op, target, Pmode);
5790 expand_insn (icode, 1, &op);
5791 return target;
5793 error ("__builtin_thread_pointer is not supported on this target");
5794 return const0_rtx;
5797 static void
5798 expand_builtin_set_thread_pointer (tree exp)
5800 enum insn_code icode;
5801 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5802 return;
5803 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
5804 if (icode != CODE_FOR_nothing)
5806 struct expand_operand op;
5807 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
5808 Pmode, EXPAND_NORMAL);
5809 create_input_operand (&op, val, Pmode);
5810 expand_insn (icode, 1, &op);
5811 return;
5813 error ("__builtin_set_thread_pointer is not supported on this target");
5817 /* Emit code to restore the current value of stack. */
5819 static void
5820 expand_stack_restore (tree var)
5822 rtx_insn *prev;
5823 rtx sa = expand_normal (var);
5825 sa = convert_memory_address (Pmode, sa);
5827 prev = get_last_insn ();
5828 emit_stack_restore (SAVE_BLOCK, sa);
5830 record_new_stack_level ();
5832 fixup_args_size_notes (prev, get_last_insn (), 0);
5835 /* Emit code to save the current value of stack. */
5837 static rtx
5838 expand_stack_save (void)
5840 rtx ret = NULL_RTX;
5842 emit_stack_save (SAVE_BLOCK, &ret);
5843 return ret;
5847 /* Expand an expression EXP that calls a built-in function,
5848 with result going to TARGET if that's convenient
5849 (and in mode MODE if that's convenient).
5850 SUBTARGET may be used as the target for computing one of EXP's operands.
5851 IGNORE is nonzero if the value is to be ignored. */
5854 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
5855 int ignore)
5857 tree fndecl = get_callee_fndecl (exp);
5858 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5859 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5860 int flags;
5862 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5863 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5865 /* When ASan is enabled, we don't want to expand some memory/string
5866 builtins and rely on libsanitizer's hooks. This allows us to avoid
5867 redundant checks and be sure, that possible overflow will be detected
5868 by ASan. */
5870 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
5871 return expand_call (exp, target, ignore);
5873 /* When not optimizing, generate calls to library functions for a certain
5874 set of builtins. */
5875 if (!optimize
5876 && !called_as_built_in (fndecl)
5877 && fcode != BUILT_IN_FORK
5878 && fcode != BUILT_IN_EXECL
5879 && fcode != BUILT_IN_EXECV
5880 && fcode != BUILT_IN_EXECLP
5881 && fcode != BUILT_IN_EXECLE
5882 && fcode != BUILT_IN_EXECVP
5883 && fcode != BUILT_IN_EXECVE
5884 && fcode != BUILT_IN_ALLOCA
5885 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5886 && fcode != BUILT_IN_FREE
5887 && fcode != BUILT_IN_CHKP_SET_PTR_BOUNDS
5888 && fcode != BUILT_IN_CHKP_INIT_PTR_BOUNDS
5889 && fcode != BUILT_IN_CHKP_NULL_PTR_BOUNDS
5890 && fcode != BUILT_IN_CHKP_COPY_PTR_BOUNDS
5891 && fcode != BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5892 && fcode != BUILT_IN_CHKP_STORE_PTR_BOUNDS
5893 && fcode != BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
5894 && fcode != BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
5895 && fcode != BUILT_IN_CHKP_CHECK_PTR_BOUNDS
5896 && fcode != BUILT_IN_CHKP_GET_PTR_LBOUND
5897 && fcode != BUILT_IN_CHKP_GET_PTR_UBOUND
5898 && fcode != BUILT_IN_CHKP_BNDRET)
5899 return expand_call (exp, target, ignore);
5901 /* The built-in function expanders test for target == const0_rtx
5902 to determine whether the function's result will be ignored. */
5903 if (ignore)
5904 target = const0_rtx;
5906 /* If the result of a pure or const built-in function is ignored, and
5907 none of its arguments are volatile, we can avoid expanding the
5908 built-in call and just evaluate the arguments for side-effects. */
5909 if (target == const0_rtx
5910 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5911 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5913 bool volatilep = false;
5914 tree arg;
5915 call_expr_arg_iterator iter;
5917 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5918 if (TREE_THIS_VOLATILE (arg))
5920 volatilep = true;
5921 break;
5924 if (! volatilep)
5926 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5927 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5928 return const0_rtx;
5932 /* expand_builtin_with_bounds is supposed to be used for
5933 instrumented builtin calls. */
5934 gcc_assert (!CALL_WITH_BOUNDS_P (exp));
5936 switch (fcode)
5938 CASE_FLT_FN (BUILT_IN_FABS):
5939 case BUILT_IN_FABSD32:
5940 case BUILT_IN_FABSD64:
5941 case BUILT_IN_FABSD128:
5942 target = expand_builtin_fabs (exp, target, subtarget);
5943 if (target)
5944 return target;
5945 break;
5947 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5948 target = expand_builtin_copysign (exp, target, subtarget);
5949 if (target)
5950 return target;
5951 break;
5953 /* Just do a normal library call if we were unable to fold
5954 the values. */
5955 CASE_FLT_FN (BUILT_IN_CABS):
5956 break;
5958 CASE_FLT_FN (BUILT_IN_EXP):
5959 CASE_FLT_FN (BUILT_IN_EXP10):
5960 CASE_FLT_FN (BUILT_IN_POW10):
5961 CASE_FLT_FN (BUILT_IN_EXP2):
5962 CASE_FLT_FN (BUILT_IN_EXPM1):
5963 CASE_FLT_FN (BUILT_IN_LOGB):
5964 CASE_FLT_FN (BUILT_IN_LOG):
5965 CASE_FLT_FN (BUILT_IN_LOG10):
5966 CASE_FLT_FN (BUILT_IN_LOG2):
5967 CASE_FLT_FN (BUILT_IN_LOG1P):
5968 CASE_FLT_FN (BUILT_IN_TAN):
5969 CASE_FLT_FN (BUILT_IN_ASIN):
5970 CASE_FLT_FN (BUILT_IN_ACOS):
5971 CASE_FLT_FN (BUILT_IN_ATAN):
5972 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5973 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5974 because of possible accuracy problems. */
5975 if (! flag_unsafe_math_optimizations)
5976 break;
5977 CASE_FLT_FN (BUILT_IN_SQRT):
5978 CASE_FLT_FN (BUILT_IN_FLOOR):
5979 CASE_FLT_FN (BUILT_IN_CEIL):
5980 CASE_FLT_FN (BUILT_IN_TRUNC):
5981 CASE_FLT_FN (BUILT_IN_ROUND):
5982 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5983 CASE_FLT_FN (BUILT_IN_RINT):
5984 target = expand_builtin_mathfn (exp, target, subtarget);
5985 if (target)
5986 return target;
5987 break;
5989 CASE_FLT_FN (BUILT_IN_FMA):
5990 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5991 if (target)
5992 return target;
5993 break;
5995 CASE_FLT_FN (BUILT_IN_ILOGB):
5996 if (! flag_unsafe_math_optimizations)
5997 break;
5998 CASE_FLT_FN (BUILT_IN_ISINF):
5999 CASE_FLT_FN (BUILT_IN_FINITE):
6000 case BUILT_IN_ISFINITE:
6001 case BUILT_IN_ISNORMAL:
6002 target = expand_builtin_interclass_mathfn (exp, target);
6003 if (target)
6004 return target;
6005 break;
6007 CASE_FLT_FN (BUILT_IN_ICEIL):
6008 CASE_FLT_FN (BUILT_IN_LCEIL):
6009 CASE_FLT_FN (BUILT_IN_LLCEIL):
6010 CASE_FLT_FN (BUILT_IN_LFLOOR):
6011 CASE_FLT_FN (BUILT_IN_IFLOOR):
6012 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6013 target = expand_builtin_int_roundingfn (exp, target);
6014 if (target)
6015 return target;
6016 break;
6018 CASE_FLT_FN (BUILT_IN_IRINT):
6019 CASE_FLT_FN (BUILT_IN_LRINT):
6020 CASE_FLT_FN (BUILT_IN_LLRINT):
6021 CASE_FLT_FN (BUILT_IN_IROUND):
6022 CASE_FLT_FN (BUILT_IN_LROUND):
6023 CASE_FLT_FN (BUILT_IN_LLROUND):
6024 target = expand_builtin_int_roundingfn_2 (exp, target);
6025 if (target)
6026 return target;
6027 break;
6029 CASE_FLT_FN (BUILT_IN_POWI):
6030 target = expand_builtin_powi (exp, target);
6031 if (target)
6032 return target;
6033 break;
6035 CASE_FLT_FN (BUILT_IN_ATAN2):
6036 CASE_FLT_FN (BUILT_IN_LDEXP):
6037 CASE_FLT_FN (BUILT_IN_SCALB):
6038 CASE_FLT_FN (BUILT_IN_SCALBN):
6039 CASE_FLT_FN (BUILT_IN_SCALBLN):
6040 if (! flag_unsafe_math_optimizations)
6041 break;
6043 CASE_FLT_FN (BUILT_IN_FMOD):
6044 CASE_FLT_FN (BUILT_IN_REMAINDER):
6045 CASE_FLT_FN (BUILT_IN_DREM):
6046 CASE_FLT_FN (BUILT_IN_POW):
6047 target = expand_builtin_mathfn_2 (exp, target, subtarget);
6048 if (target)
6049 return target;
6050 break;
6052 CASE_FLT_FN (BUILT_IN_CEXPI):
6053 target = expand_builtin_cexpi (exp, target);
6054 gcc_assert (target);
6055 return target;
6057 CASE_FLT_FN (BUILT_IN_SIN):
6058 CASE_FLT_FN (BUILT_IN_COS):
6059 if (! flag_unsafe_math_optimizations)
6060 break;
6061 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6062 if (target)
6063 return target;
6064 break;
6066 CASE_FLT_FN (BUILT_IN_SINCOS):
6067 if (! flag_unsafe_math_optimizations)
6068 break;
6069 target = expand_builtin_sincos (exp);
6070 if (target)
6071 return target;
6072 break;
6074 case BUILT_IN_APPLY_ARGS:
6075 return expand_builtin_apply_args ();
6077 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6078 FUNCTION with a copy of the parameters described by
6079 ARGUMENTS, and ARGSIZE. It returns a block of memory
6080 allocated on the stack into which is stored all the registers
6081 that might possibly be used for returning the result of a
6082 function. ARGUMENTS is the value returned by
6083 __builtin_apply_args. ARGSIZE is the number of bytes of
6084 arguments that must be copied. ??? How should this value be
6085 computed? We'll also need a safe worst case value for varargs
6086 functions. */
6087 case BUILT_IN_APPLY:
6088 if (!validate_arglist (exp, POINTER_TYPE,
6089 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6090 && !validate_arglist (exp, REFERENCE_TYPE,
6091 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6092 return const0_rtx;
6093 else
6095 rtx ops[3];
6097 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6098 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6099 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6101 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6104 /* __builtin_return (RESULT) causes the function to return the
6105 value described by RESULT. RESULT is address of the block of
6106 memory returned by __builtin_apply. */
6107 case BUILT_IN_RETURN:
6108 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6109 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6110 return const0_rtx;
6112 case BUILT_IN_SAVEREGS:
6113 return expand_builtin_saveregs ();
6115 case BUILT_IN_VA_ARG_PACK:
6116 /* All valid uses of __builtin_va_arg_pack () are removed during
6117 inlining. */
6118 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6119 return const0_rtx;
6121 case BUILT_IN_VA_ARG_PACK_LEN:
6122 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6123 inlining. */
6124 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6125 return const0_rtx;
6127 /* Return the address of the first anonymous stack arg. */
6128 case BUILT_IN_NEXT_ARG:
6129 if (fold_builtin_next_arg (exp, false))
6130 return const0_rtx;
6131 return expand_builtin_next_arg ();
6133 case BUILT_IN_CLEAR_CACHE:
6134 target = expand_builtin___clear_cache (exp);
6135 if (target)
6136 return target;
6137 break;
6139 case BUILT_IN_CLASSIFY_TYPE:
6140 return expand_builtin_classify_type (exp);
6142 case BUILT_IN_CONSTANT_P:
6143 return const0_rtx;
6145 case BUILT_IN_FRAME_ADDRESS:
6146 case BUILT_IN_RETURN_ADDRESS:
6147 return expand_builtin_frame_address (fndecl, exp);
6149 /* Returns the address of the area where the structure is returned.
6150 0 otherwise. */
6151 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6152 if (call_expr_nargs (exp) != 0
6153 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6154 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6155 return const0_rtx;
6156 else
6157 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6159 case BUILT_IN_ALLOCA:
6160 case BUILT_IN_ALLOCA_WITH_ALIGN:
6161 /* If the allocation stems from the declaration of a variable-sized
6162 object, it cannot accumulate. */
6163 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6164 if (target)
6165 return target;
6166 break;
6168 case BUILT_IN_STACK_SAVE:
6169 return expand_stack_save ();
6171 case BUILT_IN_STACK_RESTORE:
6172 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6173 return const0_rtx;
6175 case BUILT_IN_BSWAP16:
6176 case BUILT_IN_BSWAP32:
6177 case BUILT_IN_BSWAP64:
6178 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6179 if (target)
6180 return target;
6181 break;
6183 CASE_INT_FN (BUILT_IN_FFS):
6184 target = expand_builtin_unop (target_mode, exp, target,
6185 subtarget, ffs_optab);
6186 if (target)
6187 return target;
6188 break;
6190 CASE_INT_FN (BUILT_IN_CLZ):
6191 target = expand_builtin_unop (target_mode, exp, target,
6192 subtarget, clz_optab);
6193 if (target)
6194 return target;
6195 break;
6197 CASE_INT_FN (BUILT_IN_CTZ):
6198 target = expand_builtin_unop (target_mode, exp, target,
6199 subtarget, ctz_optab);
6200 if (target)
6201 return target;
6202 break;
6204 CASE_INT_FN (BUILT_IN_CLRSB):
6205 target = expand_builtin_unop (target_mode, exp, target,
6206 subtarget, clrsb_optab);
6207 if (target)
6208 return target;
6209 break;
6211 CASE_INT_FN (BUILT_IN_POPCOUNT):
6212 target = expand_builtin_unop (target_mode, exp, target,
6213 subtarget, popcount_optab);
6214 if (target)
6215 return target;
6216 break;
6218 CASE_INT_FN (BUILT_IN_PARITY):
6219 target = expand_builtin_unop (target_mode, exp, target,
6220 subtarget, parity_optab);
6221 if (target)
6222 return target;
6223 break;
6225 case BUILT_IN_STRLEN:
6226 target = expand_builtin_strlen (exp, target, target_mode);
6227 if (target)
6228 return target;
6229 break;
6231 case BUILT_IN_STRCPY:
6232 target = expand_builtin_strcpy (exp, target);
6233 if (target)
6234 return target;
6235 break;
6237 case BUILT_IN_STRNCPY:
6238 target = expand_builtin_strncpy (exp, target);
6239 if (target)
6240 return target;
6241 break;
6243 case BUILT_IN_STPCPY:
6244 target = expand_builtin_stpcpy (exp, target, mode);
6245 if (target)
6246 return target;
6247 break;
6249 case BUILT_IN_MEMCPY:
6250 target = expand_builtin_memcpy (exp, target);
6251 if (target)
6252 return target;
6253 break;
6255 case BUILT_IN_MEMPCPY:
6256 target = expand_builtin_mempcpy (exp, target, mode);
6257 if (target)
6258 return target;
6259 break;
6261 case BUILT_IN_MEMSET:
6262 target = expand_builtin_memset (exp, target, mode);
6263 if (target)
6264 return target;
6265 break;
6267 case BUILT_IN_BZERO:
6268 target = expand_builtin_bzero (exp);
6269 if (target)
6270 return target;
6271 break;
6273 case BUILT_IN_STRCMP:
6274 target = expand_builtin_strcmp (exp, target);
6275 if (target)
6276 return target;
6277 break;
6279 case BUILT_IN_STRNCMP:
6280 target = expand_builtin_strncmp (exp, target, mode);
6281 if (target)
6282 return target;
6283 break;
6285 case BUILT_IN_BCMP:
6286 case BUILT_IN_MEMCMP:
6287 target = expand_builtin_memcmp (exp, target);
6288 if (target)
6289 return target;
6290 break;
6292 case BUILT_IN_SETJMP:
6293 /* This should have been lowered to the builtins below. */
6294 gcc_unreachable ();
6296 case BUILT_IN_SETJMP_SETUP:
6297 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6298 and the receiver label. */
6299 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6301 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6302 VOIDmode, EXPAND_NORMAL);
6303 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6304 rtx_insn *label_r = label_rtx (label);
6306 /* This is copied from the handling of non-local gotos. */
6307 expand_builtin_setjmp_setup (buf_addr, label_r);
6308 nonlocal_goto_handler_labels
6309 = gen_rtx_INSN_LIST (VOIDmode, label_r,
6310 nonlocal_goto_handler_labels);
6311 /* ??? Do not let expand_label treat us as such since we would
6312 not want to be both on the list of non-local labels and on
6313 the list of forced labels. */
6314 FORCED_LABEL (label) = 0;
6315 return const0_rtx;
6317 break;
6319 case BUILT_IN_SETJMP_RECEIVER:
6320 /* __builtin_setjmp_receiver is passed the receiver label. */
6321 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6323 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6324 rtx_insn *label_r = label_rtx (label);
6326 expand_builtin_setjmp_receiver (label_r);
6327 return const0_rtx;
6329 break;
6331 /* __builtin_longjmp is passed a pointer to an array of five words.
6332 It's similar to the C library longjmp function but works with
6333 __builtin_setjmp above. */
6334 case BUILT_IN_LONGJMP:
6335 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6337 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6338 VOIDmode, EXPAND_NORMAL);
6339 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6341 if (value != const1_rtx)
6343 error ("%<__builtin_longjmp%> second argument must be 1");
6344 return const0_rtx;
6347 expand_builtin_longjmp (buf_addr, value);
6348 return const0_rtx;
6350 break;
6352 case BUILT_IN_NONLOCAL_GOTO:
6353 target = expand_builtin_nonlocal_goto (exp);
6354 if (target)
6355 return target;
6356 break;
6358 /* This updates the setjmp buffer that is its argument with the value
6359 of the current stack pointer. */
6360 case BUILT_IN_UPDATE_SETJMP_BUF:
6361 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6363 rtx buf_addr
6364 = expand_normal (CALL_EXPR_ARG (exp, 0));
6366 expand_builtin_update_setjmp_buf (buf_addr);
6367 return const0_rtx;
6369 break;
6371 case BUILT_IN_TRAP:
6372 expand_builtin_trap ();
6373 return const0_rtx;
6375 case BUILT_IN_UNREACHABLE:
6376 expand_builtin_unreachable ();
6377 return const0_rtx;
6379 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6380 case BUILT_IN_SIGNBITD32:
6381 case BUILT_IN_SIGNBITD64:
6382 case BUILT_IN_SIGNBITD128:
6383 target = expand_builtin_signbit (exp, target);
6384 if (target)
6385 return target;
6386 break;
6388 /* Various hooks for the DWARF 2 __throw routine. */
6389 case BUILT_IN_UNWIND_INIT:
6390 expand_builtin_unwind_init ();
6391 return const0_rtx;
6392 case BUILT_IN_DWARF_CFA:
6393 return virtual_cfa_rtx;
6394 #ifdef DWARF2_UNWIND_INFO
6395 case BUILT_IN_DWARF_SP_COLUMN:
6396 return expand_builtin_dwarf_sp_column ();
6397 case BUILT_IN_INIT_DWARF_REG_SIZES:
6398 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6399 return const0_rtx;
6400 #endif
6401 case BUILT_IN_FROB_RETURN_ADDR:
6402 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6403 case BUILT_IN_EXTRACT_RETURN_ADDR:
6404 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6405 case BUILT_IN_EH_RETURN:
6406 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6407 CALL_EXPR_ARG (exp, 1));
6408 return const0_rtx;
6409 case BUILT_IN_EH_RETURN_DATA_REGNO:
6410 return expand_builtin_eh_return_data_regno (exp);
6411 case BUILT_IN_EXTEND_POINTER:
6412 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6413 case BUILT_IN_EH_POINTER:
6414 return expand_builtin_eh_pointer (exp);
6415 case BUILT_IN_EH_FILTER:
6416 return expand_builtin_eh_filter (exp);
6417 case BUILT_IN_EH_COPY_VALUES:
6418 return expand_builtin_eh_copy_values (exp);
6420 case BUILT_IN_VA_START:
6421 return expand_builtin_va_start (exp);
6422 case BUILT_IN_VA_END:
6423 return expand_builtin_va_end (exp);
6424 case BUILT_IN_VA_COPY:
6425 return expand_builtin_va_copy (exp);
6426 case BUILT_IN_EXPECT:
6427 return expand_builtin_expect (exp, target);
6428 case BUILT_IN_ASSUME_ALIGNED:
6429 return expand_builtin_assume_aligned (exp, target);
6430 case BUILT_IN_PREFETCH:
6431 expand_builtin_prefetch (exp);
6432 return const0_rtx;
6434 case BUILT_IN_INIT_TRAMPOLINE:
6435 return expand_builtin_init_trampoline (exp, true);
6436 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6437 return expand_builtin_init_trampoline (exp, false);
6438 case BUILT_IN_ADJUST_TRAMPOLINE:
6439 return expand_builtin_adjust_trampoline (exp);
6441 case BUILT_IN_FORK:
6442 case BUILT_IN_EXECL:
6443 case BUILT_IN_EXECV:
6444 case BUILT_IN_EXECLP:
6445 case BUILT_IN_EXECLE:
6446 case BUILT_IN_EXECVP:
6447 case BUILT_IN_EXECVE:
6448 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6449 if (target)
6450 return target;
6451 break;
6453 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6454 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6455 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6456 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6457 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6458 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6459 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6460 if (target)
6461 return target;
6462 break;
6464 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6465 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6466 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6467 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6468 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6469 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6470 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6471 if (target)
6472 return target;
6473 break;
6475 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6476 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6477 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6478 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6479 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6480 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6481 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6482 if (target)
6483 return target;
6484 break;
6486 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6487 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6488 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6489 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6490 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6491 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6492 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6493 if (target)
6494 return target;
6495 break;
6497 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6498 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6499 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6500 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6501 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6502 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6503 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6504 if (target)
6505 return target;
6506 break;
6508 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6509 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6510 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6511 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6512 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6513 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6514 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6515 if (target)
6516 return target;
6517 break;
6519 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6520 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6521 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6522 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6523 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6524 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6525 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6526 if (target)
6527 return target;
6528 break;
6530 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6531 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6532 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6533 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6534 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6535 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6536 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6537 if (target)
6538 return target;
6539 break;
6541 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6542 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6543 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6544 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6545 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6546 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6547 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6548 if (target)
6549 return target;
6550 break;
6552 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6553 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6554 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6555 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6556 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6557 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6558 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6559 if (target)
6560 return target;
6561 break;
6563 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6564 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6565 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6566 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6567 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6568 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6569 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6570 if (target)
6571 return target;
6572 break;
6574 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6575 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6576 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6577 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6578 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6579 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6580 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6581 if (target)
6582 return target;
6583 break;
6585 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6586 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6587 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6588 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6589 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6590 if (mode == VOIDmode)
6591 mode = TYPE_MODE (boolean_type_node);
6592 if (!target || !register_operand (target, mode))
6593 target = gen_reg_rtx (mode);
6595 mode = get_builtin_sync_mode
6596 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6597 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6598 if (target)
6599 return target;
6600 break;
6602 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6603 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6604 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6605 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6606 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6607 mode = get_builtin_sync_mode
6608 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6609 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6610 if (target)
6611 return target;
6612 break;
6614 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6615 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6616 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6617 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6618 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6619 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6620 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6621 if (target)
6622 return target;
6623 break;
6625 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6626 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6627 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6628 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6629 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6630 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6631 expand_builtin_sync_lock_release (mode, exp);
6632 return const0_rtx;
6634 case BUILT_IN_SYNC_SYNCHRONIZE:
6635 expand_builtin_sync_synchronize ();
6636 return const0_rtx;
6638 case BUILT_IN_ATOMIC_EXCHANGE_1:
6639 case BUILT_IN_ATOMIC_EXCHANGE_2:
6640 case BUILT_IN_ATOMIC_EXCHANGE_4:
6641 case BUILT_IN_ATOMIC_EXCHANGE_8:
6642 case BUILT_IN_ATOMIC_EXCHANGE_16:
6643 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6644 target = expand_builtin_atomic_exchange (mode, exp, target);
6645 if (target)
6646 return target;
6647 break;
6649 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6650 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6651 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6652 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6653 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6655 unsigned int nargs, z;
6656 vec<tree, va_gc> *vec;
6658 mode =
6659 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6660 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6661 if (target)
6662 return target;
6664 /* If this is turned into an external library call, the weak parameter
6665 must be dropped to match the expected parameter list. */
6666 nargs = call_expr_nargs (exp);
6667 vec_alloc (vec, nargs - 1);
6668 for (z = 0; z < 3; z++)
6669 vec->quick_push (CALL_EXPR_ARG (exp, z));
6670 /* Skip the boolean weak parameter. */
6671 for (z = 4; z < 6; z++)
6672 vec->quick_push (CALL_EXPR_ARG (exp, z));
6673 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6674 break;
6677 case BUILT_IN_ATOMIC_LOAD_1:
6678 case BUILT_IN_ATOMIC_LOAD_2:
6679 case BUILT_IN_ATOMIC_LOAD_4:
6680 case BUILT_IN_ATOMIC_LOAD_8:
6681 case BUILT_IN_ATOMIC_LOAD_16:
6682 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6683 target = expand_builtin_atomic_load (mode, exp, target);
6684 if (target)
6685 return target;
6686 break;
6688 case BUILT_IN_ATOMIC_STORE_1:
6689 case BUILT_IN_ATOMIC_STORE_2:
6690 case BUILT_IN_ATOMIC_STORE_4:
6691 case BUILT_IN_ATOMIC_STORE_8:
6692 case BUILT_IN_ATOMIC_STORE_16:
6693 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6694 target = expand_builtin_atomic_store (mode, exp);
6695 if (target)
6696 return const0_rtx;
6697 break;
6699 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6700 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6701 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6702 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6703 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6705 enum built_in_function lib;
6706 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6707 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6708 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6709 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6710 ignore, lib);
6711 if (target)
6712 return target;
6713 break;
6715 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6716 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6717 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6718 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6719 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6721 enum built_in_function lib;
6722 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6723 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6724 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6725 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6726 ignore, lib);
6727 if (target)
6728 return target;
6729 break;
6731 case BUILT_IN_ATOMIC_AND_FETCH_1:
6732 case BUILT_IN_ATOMIC_AND_FETCH_2:
6733 case BUILT_IN_ATOMIC_AND_FETCH_4:
6734 case BUILT_IN_ATOMIC_AND_FETCH_8:
6735 case BUILT_IN_ATOMIC_AND_FETCH_16:
6737 enum built_in_function lib;
6738 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6739 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6740 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6741 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6742 ignore, lib);
6743 if (target)
6744 return target;
6745 break;
6747 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6748 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6749 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6750 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6751 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6753 enum built_in_function lib;
6754 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6755 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6756 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6757 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6758 ignore, lib);
6759 if (target)
6760 return target;
6761 break;
6763 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6764 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6765 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6766 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6767 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6769 enum built_in_function lib;
6770 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6771 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6772 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6773 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6774 ignore, lib);
6775 if (target)
6776 return target;
6777 break;
6779 case BUILT_IN_ATOMIC_OR_FETCH_1:
6780 case BUILT_IN_ATOMIC_OR_FETCH_2:
6781 case BUILT_IN_ATOMIC_OR_FETCH_4:
6782 case BUILT_IN_ATOMIC_OR_FETCH_8:
6783 case BUILT_IN_ATOMIC_OR_FETCH_16:
6785 enum built_in_function lib;
6786 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6787 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6788 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6789 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6790 ignore, lib);
6791 if (target)
6792 return target;
6793 break;
6795 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6796 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6797 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6798 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6799 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6800 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6801 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6802 ignore, BUILT_IN_NONE);
6803 if (target)
6804 return target;
6805 break;
6807 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6808 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6809 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6810 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6811 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6812 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6813 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6814 ignore, BUILT_IN_NONE);
6815 if (target)
6816 return target;
6817 break;
6819 case BUILT_IN_ATOMIC_FETCH_AND_1:
6820 case BUILT_IN_ATOMIC_FETCH_AND_2:
6821 case BUILT_IN_ATOMIC_FETCH_AND_4:
6822 case BUILT_IN_ATOMIC_FETCH_AND_8:
6823 case BUILT_IN_ATOMIC_FETCH_AND_16:
6824 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6825 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6826 ignore, BUILT_IN_NONE);
6827 if (target)
6828 return target;
6829 break;
6831 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6832 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6833 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6834 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6835 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6836 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6837 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6838 ignore, BUILT_IN_NONE);
6839 if (target)
6840 return target;
6841 break;
6843 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6844 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6845 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6846 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6847 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6848 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6849 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6850 ignore, BUILT_IN_NONE);
6851 if (target)
6852 return target;
6853 break;
6855 case BUILT_IN_ATOMIC_FETCH_OR_1:
6856 case BUILT_IN_ATOMIC_FETCH_OR_2:
6857 case BUILT_IN_ATOMIC_FETCH_OR_4:
6858 case BUILT_IN_ATOMIC_FETCH_OR_8:
6859 case BUILT_IN_ATOMIC_FETCH_OR_16:
6860 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6861 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6862 ignore, BUILT_IN_NONE);
6863 if (target)
6864 return target;
6865 break;
6867 case BUILT_IN_ATOMIC_TEST_AND_SET:
6868 return expand_builtin_atomic_test_and_set (exp, target);
6870 case BUILT_IN_ATOMIC_CLEAR:
6871 return expand_builtin_atomic_clear (exp);
6873 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6874 return expand_builtin_atomic_always_lock_free (exp);
6876 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6877 target = expand_builtin_atomic_is_lock_free (exp);
6878 if (target)
6879 return target;
6880 break;
6882 case BUILT_IN_ATOMIC_THREAD_FENCE:
6883 expand_builtin_atomic_thread_fence (exp);
6884 return const0_rtx;
6886 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6887 expand_builtin_atomic_signal_fence (exp);
6888 return const0_rtx;
6890 case BUILT_IN_OBJECT_SIZE:
6891 return expand_builtin_object_size (exp);
6893 case BUILT_IN_MEMCPY_CHK:
6894 case BUILT_IN_MEMPCPY_CHK:
6895 case BUILT_IN_MEMMOVE_CHK:
6896 case BUILT_IN_MEMSET_CHK:
6897 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6898 if (target)
6899 return target;
6900 break;
6902 case BUILT_IN_STRCPY_CHK:
6903 case BUILT_IN_STPCPY_CHK:
6904 case BUILT_IN_STRNCPY_CHK:
6905 case BUILT_IN_STPNCPY_CHK:
6906 case BUILT_IN_STRCAT_CHK:
6907 case BUILT_IN_STRNCAT_CHK:
6908 case BUILT_IN_SNPRINTF_CHK:
6909 case BUILT_IN_VSNPRINTF_CHK:
6910 maybe_emit_chk_warning (exp, fcode);
6911 break;
6913 case BUILT_IN_SPRINTF_CHK:
6914 case BUILT_IN_VSPRINTF_CHK:
6915 maybe_emit_sprintf_chk_warning (exp, fcode);
6916 break;
6918 case BUILT_IN_FREE:
6919 if (warn_free_nonheap_object)
6920 maybe_emit_free_warning (exp);
6921 break;
6923 case BUILT_IN_THREAD_POINTER:
6924 return expand_builtin_thread_pointer (exp, target);
6926 case BUILT_IN_SET_THREAD_POINTER:
6927 expand_builtin_set_thread_pointer (exp);
6928 return const0_rtx;
6930 case BUILT_IN_CILK_DETACH:
6931 expand_builtin_cilk_detach (exp);
6932 return const0_rtx;
6934 case BUILT_IN_CILK_POP_FRAME:
6935 expand_builtin_cilk_pop_frame (exp);
6936 return const0_rtx;
6938 case BUILT_IN_CHKP_INIT_PTR_BOUNDS:
6939 case BUILT_IN_CHKP_NULL_PTR_BOUNDS:
6940 case BUILT_IN_CHKP_COPY_PTR_BOUNDS:
6941 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS:
6942 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS:
6943 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS:
6944 case BUILT_IN_CHKP_SET_PTR_BOUNDS:
6945 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS:
6946 case BUILT_IN_CHKP_STORE_PTR_BOUNDS:
6947 case BUILT_IN_CHKP_GET_PTR_LBOUND:
6948 case BUILT_IN_CHKP_GET_PTR_UBOUND:
6949 /* We allow user CHKP builtins if Pointer Bounds
6950 Checker is off. */
6951 if (!chkp_function_instrumented_p (current_function_decl))
6953 if (fcode == BUILT_IN_CHKP_SET_PTR_BOUNDS
6954 || fcode == BUILT_IN_CHKP_NARROW_PTR_BOUNDS
6955 || fcode == BUILT_IN_CHKP_INIT_PTR_BOUNDS
6956 || fcode == BUILT_IN_CHKP_NULL_PTR_BOUNDS
6957 || fcode == BUILT_IN_CHKP_COPY_PTR_BOUNDS)
6958 return expand_normal (CALL_EXPR_ARG (exp, 0));
6959 else if (fcode == BUILT_IN_CHKP_GET_PTR_LBOUND)
6960 return expand_normal (size_zero_node);
6961 else if (fcode == BUILT_IN_CHKP_GET_PTR_UBOUND)
6962 return expand_normal (size_int (-1));
6963 else
6964 return const0_rtx;
6966 /* FALLTHROUGH */
6968 case BUILT_IN_CHKP_BNDMK:
6969 case BUILT_IN_CHKP_BNDSTX:
6970 case BUILT_IN_CHKP_BNDCL:
6971 case BUILT_IN_CHKP_BNDCU:
6972 case BUILT_IN_CHKP_BNDLDX:
6973 case BUILT_IN_CHKP_BNDRET:
6974 case BUILT_IN_CHKP_INTERSECT:
6975 case BUILT_IN_CHKP_NARROW:
6976 case BUILT_IN_CHKP_EXTRACT_LOWER:
6977 case BUILT_IN_CHKP_EXTRACT_UPPER:
6978 /* Software implementation of Pointer Bounds Checker is NYI.
6979 Target support is required. */
6980 error ("Your target platform does not support -fcheck-pointer-bounds");
6981 break;
6983 case BUILT_IN_ACC_ON_DEVICE:
6984 /* Do library call, if we failed to expand the builtin when
6985 folding. */
6986 break;
6988 default: /* just do library call, if unknown builtin */
6989 break;
6992 /* The switch statement above can drop through to cause the function
6993 to be called normally. */
6994 return expand_call (exp, target, ignore);
6997 /* Similar to expand_builtin but is used for instrumented calls. */
7000 expand_builtin_with_bounds (tree exp, rtx target,
7001 rtx subtarget ATTRIBUTE_UNUSED,
7002 machine_mode mode, int ignore)
7004 tree fndecl = get_callee_fndecl (exp);
7005 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7007 gcc_assert (CALL_WITH_BOUNDS_P (exp));
7009 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7010 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7012 gcc_assert (fcode > BEGIN_CHKP_BUILTINS
7013 && fcode < END_CHKP_BUILTINS);
7015 switch (fcode)
7017 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP:
7018 target = expand_builtin_memcpy_with_bounds (exp, target);
7019 if (target)
7020 return target;
7021 break;
7023 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP:
7024 target = expand_builtin_mempcpy_with_bounds (exp, target, mode);
7025 if (target)
7026 return target;
7027 break;
7029 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP:
7030 target = expand_builtin_memset_with_bounds (exp, target, mode);
7031 if (target)
7032 return target;
7033 break;
7035 default:
7036 break;
7039 /* The switch statement above can drop through to cause the function
7040 to be called normally. */
7041 return expand_call (exp, target, ignore);
7044 /* Determine whether a tree node represents a call to a built-in
7045 function. If the tree T is a call to a built-in function with
7046 the right number of arguments of the appropriate types, return
7047 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7048 Otherwise the return value is END_BUILTINS. */
7050 enum built_in_function
7051 builtin_mathfn_code (const_tree t)
7053 const_tree fndecl, arg, parmlist;
7054 const_tree argtype, parmtype;
7055 const_call_expr_arg_iterator iter;
7057 if (TREE_CODE (t) != CALL_EXPR
7058 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
7059 return END_BUILTINS;
7061 fndecl = get_callee_fndecl (t);
7062 if (fndecl == NULL_TREE
7063 || TREE_CODE (fndecl) != FUNCTION_DECL
7064 || ! DECL_BUILT_IN (fndecl)
7065 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7066 return END_BUILTINS;
7068 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7069 init_const_call_expr_arg_iterator (t, &iter);
7070 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7072 /* If a function doesn't take a variable number of arguments,
7073 the last element in the list will have type `void'. */
7074 parmtype = TREE_VALUE (parmlist);
7075 if (VOID_TYPE_P (parmtype))
7077 if (more_const_call_expr_args_p (&iter))
7078 return END_BUILTINS;
7079 return DECL_FUNCTION_CODE (fndecl);
7082 if (! more_const_call_expr_args_p (&iter))
7083 return END_BUILTINS;
7085 arg = next_const_call_expr_arg (&iter);
7086 argtype = TREE_TYPE (arg);
7088 if (SCALAR_FLOAT_TYPE_P (parmtype))
7090 if (! SCALAR_FLOAT_TYPE_P (argtype))
7091 return END_BUILTINS;
7093 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7095 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7096 return END_BUILTINS;
7098 else if (POINTER_TYPE_P (parmtype))
7100 if (! POINTER_TYPE_P (argtype))
7101 return END_BUILTINS;
7103 else if (INTEGRAL_TYPE_P (parmtype))
7105 if (! INTEGRAL_TYPE_P (argtype))
7106 return END_BUILTINS;
7108 else
7109 return END_BUILTINS;
7112 /* Variable-length argument list. */
7113 return DECL_FUNCTION_CODE (fndecl);
7116 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7117 evaluate to a constant. */
7119 static tree
7120 fold_builtin_constant_p (tree arg)
7122 /* We return 1 for a numeric type that's known to be a constant
7123 value at compile-time or for an aggregate type that's a
7124 literal constant. */
7125 STRIP_NOPS (arg);
7127 /* If we know this is a constant, emit the constant of one. */
7128 if (CONSTANT_CLASS_P (arg)
7129 || (TREE_CODE (arg) == CONSTRUCTOR
7130 && TREE_CONSTANT (arg)))
7131 return integer_one_node;
7132 if (TREE_CODE (arg) == ADDR_EXPR)
7134 tree op = TREE_OPERAND (arg, 0);
7135 if (TREE_CODE (op) == STRING_CST
7136 || (TREE_CODE (op) == ARRAY_REF
7137 && integer_zerop (TREE_OPERAND (op, 1))
7138 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7139 return integer_one_node;
7142 /* If this expression has side effects, show we don't know it to be a
7143 constant. Likewise if it's a pointer or aggregate type since in
7144 those case we only want literals, since those are only optimized
7145 when generating RTL, not later.
7146 And finally, if we are compiling an initializer, not code, we
7147 need to return a definite result now; there's not going to be any
7148 more optimization done. */
7149 if (TREE_SIDE_EFFECTS (arg)
7150 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7151 || POINTER_TYPE_P (TREE_TYPE (arg))
7152 || cfun == 0
7153 || folding_initializer
7154 || force_folding_builtin_constant_p)
7155 return integer_zero_node;
7157 return NULL_TREE;
7160 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7161 return it as a truthvalue. */
7163 static tree
7164 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
7165 tree predictor)
7167 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7169 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
7170 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7171 ret_type = TREE_TYPE (TREE_TYPE (fn));
7172 pred_type = TREE_VALUE (arg_types);
7173 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7175 pred = fold_convert_loc (loc, pred_type, pred);
7176 expected = fold_convert_loc (loc, expected_type, expected);
7177 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
7178 predictor);
7180 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7181 build_int_cst (ret_type, 0));
7184 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7185 NULL_TREE if no simplification is possible. */
7187 tree
7188 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
7190 tree inner, fndecl, inner_arg0;
7191 enum tree_code code;
7193 /* Distribute the expected value over short-circuiting operators.
7194 See through the cast from truthvalue_type_node to long. */
7195 inner_arg0 = arg0;
7196 while (CONVERT_EXPR_P (inner_arg0)
7197 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7198 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7199 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
7201 /* If this is a builtin_expect within a builtin_expect keep the
7202 inner one. See through a comparison against a constant. It
7203 might have been added to create a thruthvalue. */
7204 inner = inner_arg0;
7206 if (COMPARISON_CLASS_P (inner)
7207 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7208 inner = TREE_OPERAND (inner, 0);
7210 if (TREE_CODE (inner) == CALL_EXPR
7211 && (fndecl = get_callee_fndecl (inner))
7212 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7213 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7214 return arg0;
7216 inner = inner_arg0;
7217 code = TREE_CODE (inner);
7218 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7220 tree op0 = TREE_OPERAND (inner, 0);
7221 tree op1 = TREE_OPERAND (inner, 1);
7223 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
7224 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
7225 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7227 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7230 /* If the argument isn't invariant then there's nothing else we can do. */
7231 if (!TREE_CONSTANT (inner_arg0))
7232 return NULL_TREE;
7234 /* If we expect that a comparison against the argument will fold to
7235 a constant return the constant. In practice, this means a true
7236 constant or the address of a non-weak symbol. */
7237 inner = inner_arg0;
7238 STRIP_NOPS (inner);
7239 if (TREE_CODE (inner) == ADDR_EXPR)
7243 inner = TREE_OPERAND (inner, 0);
7245 while (TREE_CODE (inner) == COMPONENT_REF
7246 || TREE_CODE (inner) == ARRAY_REF);
7247 if ((TREE_CODE (inner) == VAR_DECL
7248 || TREE_CODE (inner) == FUNCTION_DECL)
7249 && DECL_WEAK (inner))
7250 return NULL_TREE;
7253 /* Otherwise, ARG0 already has the proper type for the return value. */
7254 return arg0;
7257 /* Fold a call to __builtin_classify_type with argument ARG. */
7259 static tree
7260 fold_builtin_classify_type (tree arg)
7262 if (arg == 0)
7263 return build_int_cst (integer_type_node, no_type_class);
7265 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7268 /* Fold a call to __builtin_strlen with argument ARG. */
7270 static tree
7271 fold_builtin_strlen (location_t loc, tree type, tree arg)
7273 if (!validate_arg (arg, POINTER_TYPE))
7274 return NULL_TREE;
7275 else
7277 tree len = c_strlen (arg, 0);
7279 if (len)
7280 return fold_convert_loc (loc, type, len);
7282 return NULL_TREE;
7286 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7288 static tree
7289 fold_builtin_inf (location_t loc, tree type, int warn)
7291 REAL_VALUE_TYPE real;
7293 /* __builtin_inff is intended to be usable to define INFINITY on all
7294 targets. If an infinity is not available, INFINITY expands "to a
7295 positive constant of type float that overflows at translation
7296 time", footnote "In this case, using INFINITY will violate the
7297 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7298 Thus we pedwarn to ensure this constraint violation is
7299 diagnosed. */
7300 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7301 pedwarn (loc, 0, "target format does not support infinity");
7303 real_inf (&real);
7304 return build_real (type, real);
7307 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7309 static tree
7310 fold_builtin_nan (tree arg, tree type, int quiet)
7312 REAL_VALUE_TYPE real;
7313 const char *str;
7315 if (!validate_arg (arg, POINTER_TYPE))
7316 return NULL_TREE;
7317 str = c_getstr (arg);
7318 if (!str)
7319 return NULL_TREE;
7321 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7322 return NULL_TREE;
7324 return build_real (type, real);
7327 /* Return true if the floating point expression T has an integer value.
7328 We also allow +Inf, -Inf and NaN to be considered integer values. */
7330 static bool
7331 integer_valued_real_p (tree t)
7333 switch (TREE_CODE (t))
7335 case FLOAT_EXPR:
7336 return true;
7338 case ABS_EXPR:
7339 case SAVE_EXPR:
7340 return integer_valued_real_p (TREE_OPERAND (t, 0));
7342 case COMPOUND_EXPR:
7343 case MODIFY_EXPR:
7344 case BIND_EXPR:
7345 return integer_valued_real_p (TREE_OPERAND (t, 1));
7347 case PLUS_EXPR:
7348 case MINUS_EXPR:
7349 case MULT_EXPR:
7350 case MIN_EXPR:
7351 case MAX_EXPR:
7352 return integer_valued_real_p (TREE_OPERAND (t, 0))
7353 && integer_valued_real_p (TREE_OPERAND (t, 1));
7355 case COND_EXPR:
7356 return integer_valued_real_p (TREE_OPERAND (t, 1))
7357 && integer_valued_real_p (TREE_OPERAND (t, 2));
7359 case REAL_CST:
7360 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7362 CASE_CONVERT:
7364 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7365 if (TREE_CODE (type) == INTEGER_TYPE)
7366 return true;
7367 if (TREE_CODE (type) == REAL_TYPE)
7368 return integer_valued_real_p (TREE_OPERAND (t, 0));
7369 break;
7372 case CALL_EXPR:
7373 switch (builtin_mathfn_code (t))
7375 CASE_FLT_FN (BUILT_IN_CEIL):
7376 CASE_FLT_FN (BUILT_IN_FLOOR):
7377 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7378 CASE_FLT_FN (BUILT_IN_RINT):
7379 CASE_FLT_FN (BUILT_IN_ROUND):
7380 CASE_FLT_FN (BUILT_IN_TRUNC):
7381 return true;
7383 CASE_FLT_FN (BUILT_IN_FMIN):
7384 CASE_FLT_FN (BUILT_IN_FMAX):
7385 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7386 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7388 default:
7389 break;
7391 break;
7393 default:
7394 break;
7396 return false;
7399 /* FNDECL is assumed to be a builtin where truncation can be propagated
7400 across (for instance floor((double)f) == (double)floorf (f).
7401 Do the transformation for a call with argument ARG. */
7403 static tree
7404 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7406 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7408 if (!validate_arg (arg, REAL_TYPE))
7409 return NULL_TREE;
7411 /* Integer rounding functions are idempotent. */
7412 if (fcode == builtin_mathfn_code (arg))
7413 return arg;
7415 /* If argument is already integer valued, and we don't need to worry
7416 about setting errno, there's no need to perform rounding. */
7417 if (! flag_errno_math && integer_valued_real_p (arg))
7418 return arg;
7420 if (optimize)
7422 tree arg0 = strip_float_extensions (arg);
7423 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7424 tree newtype = TREE_TYPE (arg0);
7425 tree decl;
7427 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7428 && (decl = mathfn_built_in (newtype, fcode)))
7429 return fold_convert_loc (loc, ftype,
7430 build_call_expr_loc (loc, decl, 1,
7431 fold_convert_loc (loc,
7432 newtype,
7433 arg0)));
7435 return NULL_TREE;
7438 /* FNDECL is assumed to be builtin which can narrow the FP type of
7439 the argument, for instance lround((double)f) -> lroundf (f).
7440 Do the transformation for a call with argument ARG. */
7442 static tree
7443 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7445 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7447 if (!validate_arg (arg, REAL_TYPE))
7448 return NULL_TREE;
7450 /* If argument is already integer valued, and we don't need to worry
7451 about setting errno, there's no need to perform rounding. */
7452 if (! flag_errno_math && integer_valued_real_p (arg))
7453 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7454 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7456 if (optimize)
7458 tree ftype = TREE_TYPE (arg);
7459 tree arg0 = strip_float_extensions (arg);
7460 tree newtype = TREE_TYPE (arg0);
7461 tree decl;
7463 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7464 && (decl = mathfn_built_in (newtype, fcode)))
7465 return build_call_expr_loc (loc, decl, 1,
7466 fold_convert_loc (loc, newtype, arg0));
7469 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7470 sizeof (int) == sizeof (long). */
7471 if (TYPE_PRECISION (integer_type_node)
7472 == TYPE_PRECISION (long_integer_type_node))
7474 tree newfn = NULL_TREE;
7475 switch (fcode)
7477 CASE_FLT_FN (BUILT_IN_ICEIL):
7478 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7479 break;
7481 CASE_FLT_FN (BUILT_IN_IFLOOR):
7482 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7483 break;
7485 CASE_FLT_FN (BUILT_IN_IROUND):
7486 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7487 break;
7489 CASE_FLT_FN (BUILT_IN_IRINT):
7490 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7491 break;
7493 default:
7494 break;
7497 if (newfn)
7499 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7500 return fold_convert_loc (loc,
7501 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7505 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7506 sizeof (long long) == sizeof (long). */
7507 if (TYPE_PRECISION (long_long_integer_type_node)
7508 == TYPE_PRECISION (long_integer_type_node))
7510 tree newfn = NULL_TREE;
7511 switch (fcode)
7513 CASE_FLT_FN (BUILT_IN_LLCEIL):
7514 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7515 break;
7517 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7518 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7519 break;
7521 CASE_FLT_FN (BUILT_IN_LLROUND):
7522 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7523 break;
7525 CASE_FLT_FN (BUILT_IN_LLRINT):
7526 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7527 break;
7529 default:
7530 break;
7533 if (newfn)
7535 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7536 return fold_convert_loc (loc,
7537 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7541 return NULL_TREE;
7544 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7545 return type. Return NULL_TREE if no simplification can be made. */
7547 static tree
7548 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7550 tree res;
7552 if (!validate_arg (arg, COMPLEX_TYPE)
7553 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7554 return NULL_TREE;
7556 /* Calculate the result when the argument is a constant. */
7557 if (TREE_CODE (arg) == COMPLEX_CST
7558 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7559 type, mpfr_hypot)))
7560 return res;
7562 if (TREE_CODE (arg) == COMPLEX_EXPR)
7564 tree real = TREE_OPERAND (arg, 0);
7565 tree imag = TREE_OPERAND (arg, 1);
7567 /* If either part is zero, cabs is fabs of the other. */
7568 if (real_zerop (real))
7569 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7570 if (real_zerop (imag))
7571 return fold_build1_loc (loc, ABS_EXPR, type, real);
7573 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7574 if (flag_unsafe_math_optimizations
7575 && operand_equal_p (real, imag, OEP_PURE_SAME))
7577 STRIP_NOPS (real);
7578 return fold_build2_loc (loc, MULT_EXPR, type,
7579 fold_build1_loc (loc, ABS_EXPR, type, real),
7580 build_real_truncate (type, dconst_sqrt2 ()));
7584 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7585 if (TREE_CODE (arg) == NEGATE_EXPR
7586 || TREE_CODE (arg) == CONJ_EXPR)
7587 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7589 /* Don't do this when optimizing for size. */
7590 if (flag_unsafe_math_optimizations
7591 && optimize && optimize_function_for_speed_p (cfun))
7593 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7595 if (sqrtfn != NULL_TREE)
7597 tree rpart, ipart, result;
7599 arg = builtin_save_expr (arg);
7601 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7602 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7604 rpart = builtin_save_expr (rpart);
7605 ipart = builtin_save_expr (ipart);
7607 result = fold_build2_loc (loc, PLUS_EXPR, type,
7608 fold_build2_loc (loc, MULT_EXPR, type,
7609 rpart, rpart),
7610 fold_build2_loc (loc, MULT_EXPR, type,
7611 ipart, ipart));
7613 return build_call_expr_loc (loc, sqrtfn, 1, result);
7617 return NULL_TREE;
7620 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7621 complex tree type of the result. If NEG is true, the imaginary
7622 zero is negative. */
7624 static tree
7625 build_complex_cproj (tree type, bool neg)
7627 REAL_VALUE_TYPE rinf, rzero = dconst0;
7629 real_inf (&rinf);
7630 rzero.sign = neg;
7631 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7632 build_real (TREE_TYPE (type), rzero));
7635 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7636 return type. Return NULL_TREE if no simplification can be made. */
7638 static tree
7639 fold_builtin_cproj (location_t loc, tree arg, tree type)
7641 if (!validate_arg (arg, COMPLEX_TYPE)
7642 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7643 return NULL_TREE;
7645 /* If there are no infinities, return arg. */
7646 if (! HONOR_INFINITIES (type))
7647 return non_lvalue_loc (loc, arg);
7649 /* Calculate the result when the argument is a constant. */
7650 if (TREE_CODE (arg) == COMPLEX_CST)
7652 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7653 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7655 if (real_isinf (real) || real_isinf (imag))
7656 return build_complex_cproj (type, imag->sign);
7657 else
7658 return arg;
7660 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7662 tree real = TREE_OPERAND (arg, 0);
7663 tree imag = TREE_OPERAND (arg, 1);
7665 STRIP_NOPS (real);
7666 STRIP_NOPS (imag);
7668 /* If the real part is inf and the imag part is known to be
7669 nonnegative, return (inf + 0i). Remember side-effects are
7670 possible in the imag part. */
7671 if (TREE_CODE (real) == REAL_CST
7672 && real_isinf (TREE_REAL_CST_PTR (real))
7673 && tree_expr_nonnegative_p (imag))
7674 return omit_one_operand_loc (loc, type,
7675 build_complex_cproj (type, false),
7676 arg);
7678 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7679 Remember side-effects are possible in the real part. */
7680 if (TREE_CODE (imag) == REAL_CST
7681 && real_isinf (TREE_REAL_CST_PTR (imag)))
7682 return
7683 omit_one_operand_loc (loc, type,
7684 build_complex_cproj (type, TREE_REAL_CST_PTR
7685 (imag)->sign), arg);
7688 return NULL_TREE;
7691 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7692 TYPE is the type of the return value. Return NULL_TREE if no
7693 simplification can be made. */
7695 static tree
7696 fold_builtin_cos (location_t loc,
7697 tree arg, tree type, tree fndecl)
7699 tree res, narg;
7701 if (!validate_arg (arg, REAL_TYPE))
7702 return NULL_TREE;
7704 /* Calculate the result when the argument is a constant. */
7705 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7706 return res;
7708 /* Optimize cos(-x) into cos (x). */
7709 if ((narg = fold_strip_sign_ops (arg)))
7710 return build_call_expr_loc (loc, fndecl, 1, narg);
7712 return NULL_TREE;
7715 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7716 Return NULL_TREE if no simplification can be made. */
7718 static tree
7719 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7721 if (validate_arg (arg, REAL_TYPE))
7723 tree res, narg;
7725 /* Calculate the result when the argument is a constant. */
7726 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7727 return res;
7729 /* Optimize cosh(-x) into cosh (x). */
7730 if ((narg = fold_strip_sign_ops (arg)))
7731 return build_call_expr_loc (loc, fndecl, 1, narg);
7734 return NULL_TREE;
7737 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7738 argument ARG. TYPE is the type of the return value. Return
7739 NULL_TREE if no simplification can be made. */
7741 static tree
7742 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7743 bool hyper)
7745 if (validate_arg (arg, COMPLEX_TYPE)
7746 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7748 tree tmp;
7750 /* Calculate the result when the argument is a constant. */
7751 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7752 return tmp;
7754 /* Optimize fn(-x) into fn(x). */
7755 if ((tmp = fold_strip_sign_ops (arg)))
7756 return build_call_expr_loc (loc, fndecl, 1, tmp);
7759 return NULL_TREE;
7762 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7763 Return NULL_TREE if no simplification can be made. */
7765 static tree
7766 fold_builtin_tan (tree arg, tree type)
7768 enum built_in_function fcode;
7769 tree res;
7771 if (!validate_arg (arg, REAL_TYPE))
7772 return NULL_TREE;
7774 /* Calculate the result when the argument is a constant. */
7775 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7776 return res;
7778 /* Optimize tan(atan(x)) = x. */
7779 fcode = builtin_mathfn_code (arg);
7780 if (flag_unsafe_math_optimizations
7781 && (fcode == BUILT_IN_ATAN
7782 || fcode == BUILT_IN_ATANF
7783 || fcode == BUILT_IN_ATANL))
7784 return CALL_EXPR_ARG (arg, 0);
7786 return NULL_TREE;
7789 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7790 NULL_TREE if no simplification can be made. */
7792 static tree
7793 fold_builtin_sincos (location_t loc,
7794 tree arg0, tree arg1, tree arg2)
7796 tree type;
7797 tree res, fn, call;
7799 if (!validate_arg (arg0, REAL_TYPE)
7800 || !validate_arg (arg1, POINTER_TYPE)
7801 || !validate_arg (arg2, POINTER_TYPE))
7802 return NULL_TREE;
7804 type = TREE_TYPE (arg0);
7806 /* Calculate the result when the argument is a constant. */
7807 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7808 return res;
7810 /* Canonicalize sincos to cexpi. */
7811 if (!targetm.libc_has_function (function_c99_math_complex))
7812 return NULL_TREE;
7813 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7814 if (!fn)
7815 return NULL_TREE;
7817 call = build_call_expr_loc (loc, fn, 1, arg0);
7818 call = builtin_save_expr (call);
7820 return build2 (COMPOUND_EXPR, void_type_node,
7821 build2 (MODIFY_EXPR, void_type_node,
7822 build_fold_indirect_ref_loc (loc, arg1),
7823 build1 (IMAGPART_EXPR, type, call)),
7824 build2 (MODIFY_EXPR, void_type_node,
7825 build_fold_indirect_ref_loc (loc, arg2),
7826 build1 (REALPART_EXPR, type, call)));
7829 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7830 NULL_TREE if no simplification can be made. */
7832 static tree
7833 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7835 tree rtype;
7836 tree realp, imagp, ifn;
7837 tree res;
7839 if (!validate_arg (arg0, COMPLEX_TYPE)
7840 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7841 return NULL_TREE;
7843 /* Calculate the result when the argument is a constant. */
7844 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7845 return res;
7847 rtype = TREE_TYPE (TREE_TYPE (arg0));
7849 /* In case we can figure out the real part of arg0 and it is constant zero
7850 fold to cexpi. */
7851 if (!targetm.libc_has_function (function_c99_math_complex))
7852 return NULL_TREE;
7853 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7854 if (!ifn)
7855 return NULL_TREE;
7857 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7858 && real_zerop (realp))
7860 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7861 return build_call_expr_loc (loc, ifn, 1, narg);
7864 /* In case we can easily decompose real and imaginary parts split cexp
7865 to exp (r) * cexpi (i). */
7866 if (flag_unsafe_math_optimizations
7867 && realp)
7869 tree rfn, rcall, icall;
7871 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7872 if (!rfn)
7873 return NULL_TREE;
7875 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7876 if (!imagp)
7877 return NULL_TREE;
7879 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7880 icall = builtin_save_expr (icall);
7881 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7882 rcall = builtin_save_expr (rcall);
7883 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7884 fold_build2_loc (loc, MULT_EXPR, rtype,
7885 rcall,
7886 fold_build1_loc (loc, REALPART_EXPR,
7887 rtype, icall)),
7888 fold_build2_loc (loc, MULT_EXPR, rtype,
7889 rcall,
7890 fold_build1_loc (loc, IMAGPART_EXPR,
7891 rtype, icall)));
7894 return NULL_TREE;
7897 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7898 Return NULL_TREE if no simplification can be made. */
7900 static tree
7901 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7903 if (!validate_arg (arg, REAL_TYPE))
7904 return NULL_TREE;
7906 /* Optimize trunc of constant value. */
7907 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7909 REAL_VALUE_TYPE r, x;
7910 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7912 x = TREE_REAL_CST (arg);
7913 real_trunc (&r, TYPE_MODE (type), &x);
7914 return build_real (type, r);
7917 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7920 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7921 Return NULL_TREE if no simplification can be made. */
7923 static tree
7924 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7926 if (!validate_arg (arg, REAL_TYPE))
7927 return NULL_TREE;
7929 /* Optimize floor of constant value. */
7930 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7932 REAL_VALUE_TYPE x;
7934 x = TREE_REAL_CST (arg);
7935 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7937 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7938 REAL_VALUE_TYPE r;
7940 real_floor (&r, TYPE_MODE (type), &x);
7941 return build_real (type, r);
7945 /* Fold floor (x) where x is nonnegative to trunc (x). */
7946 if (tree_expr_nonnegative_p (arg))
7948 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7949 if (truncfn)
7950 return build_call_expr_loc (loc, truncfn, 1, arg);
7953 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7956 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7957 Return NULL_TREE if no simplification can be made. */
7959 static tree
7960 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7962 if (!validate_arg (arg, REAL_TYPE))
7963 return NULL_TREE;
7965 /* Optimize ceil of constant value. */
7966 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7968 REAL_VALUE_TYPE x;
7970 x = TREE_REAL_CST (arg);
7971 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7973 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7974 REAL_VALUE_TYPE r;
7976 real_ceil (&r, TYPE_MODE (type), &x);
7977 return build_real (type, r);
7981 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7984 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7985 Return NULL_TREE if no simplification can be made. */
7987 static tree
7988 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7990 if (!validate_arg (arg, REAL_TYPE))
7991 return NULL_TREE;
7993 /* Optimize round of constant value. */
7994 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7996 REAL_VALUE_TYPE x;
7998 x = TREE_REAL_CST (arg);
7999 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
8001 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8002 REAL_VALUE_TYPE r;
8004 real_round (&r, TYPE_MODE (type), &x);
8005 return build_real (type, r);
8009 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
8012 /* Fold function call to builtin lround, lroundf or lroundl (or the
8013 corresponding long long versions) and other rounding functions. ARG
8014 is the argument to the call. Return NULL_TREE if no simplification
8015 can be made. */
8017 static tree
8018 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
8020 if (!validate_arg (arg, REAL_TYPE))
8021 return NULL_TREE;
8023 /* Optimize lround of constant value. */
8024 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
8026 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
8028 if (real_isfinite (&x))
8030 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
8031 tree ftype = TREE_TYPE (arg);
8032 REAL_VALUE_TYPE r;
8033 bool fail = false;
8035 switch (DECL_FUNCTION_CODE (fndecl))
8037 CASE_FLT_FN (BUILT_IN_IFLOOR):
8038 CASE_FLT_FN (BUILT_IN_LFLOOR):
8039 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8040 real_floor (&r, TYPE_MODE (ftype), &x);
8041 break;
8043 CASE_FLT_FN (BUILT_IN_ICEIL):
8044 CASE_FLT_FN (BUILT_IN_LCEIL):
8045 CASE_FLT_FN (BUILT_IN_LLCEIL):
8046 real_ceil (&r, TYPE_MODE (ftype), &x);
8047 break;
8049 CASE_FLT_FN (BUILT_IN_IROUND):
8050 CASE_FLT_FN (BUILT_IN_LROUND):
8051 CASE_FLT_FN (BUILT_IN_LLROUND):
8052 real_round (&r, TYPE_MODE (ftype), &x);
8053 break;
8055 default:
8056 gcc_unreachable ();
8059 wide_int val = real_to_integer (&r, &fail, TYPE_PRECISION (itype));
8060 if (!fail)
8061 return wide_int_to_tree (itype, val);
8065 switch (DECL_FUNCTION_CODE (fndecl))
8067 CASE_FLT_FN (BUILT_IN_LFLOOR):
8068 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8069 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8070 if (tree_expr_nonnegative_p (arg))
8071 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8072 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8073 break;
8074 default:;
8077 return fold_fixed_mathfn (loc, fndecl, arg);
8080 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8081 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8082 the argument to the call. Return NULL_TREE if no simplification can
8083 be made. */
8085 static tree
8086 fold_builtin_bitop (tree fndecl, tree arg)
8088 if (!validate_arg (arg, INTEGER_TYPE))
8089 return NULL_TREE;
8091 /* Optimize for constant argument. */
8092 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8094 tree type = TREE_TYPE (arg);
8095 int result;
8097 switch (DECL_FUNCTION_CODE (fndecl))
8099 CASE_INT_FN (BUILT_IN_FFS):
8100 result = wi::ffs (arg);
8101 break;
8103 CASE_INT_FN (BUILT_IN_CLZ):
8104 if (wi::ne_p (arg, 0))
8105 result = wi::clz (arg);
8106 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8107 result = TYPE_PRECISION (type);
8108 break;
8110 CASE_INT_FN (BUILT_IN_CTZ):
8111 if (wi::ne_p (arg, 0))
8112 result = wi::ctz (arg);
8113 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8114 result = TYPE_PRECISION (type);
8115 break;
8117 CASE_INT_FN (BUILT_IN_CLRSB):
8118 result = wi::clrsb (arg);
8119 break;
8121 CASE_INT_FN (BUILT_IN_POPCOUNT):
8122 result = wi::popcount (arg);
8123 break;
8125 CASE_INT_FN (BUILT_IN_PARITY):
8126 result = wi::parity (arg);
8127 break;
8129 default:
8130 gcc_unreachable ();
8133 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8136 return NULL_TREE;
8139 /* Fold function call to builtin_bswap and the short, long and long long
8140 variants. Return NULL_TREE if no simplification can be made. */
8141 static tree
8142 fold_builtin_bswap (tree fndecl, tree arg)
8144 if (! validate_arg (arg, INTEGER_TYPE))
8145 return NULL_TREE;
8147 /* Optimize constant value. */
8148 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8150 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8152 switch (DECL_FUNCTION_CODE (fndecl))
8154 case BUILT_IN_BSWAP16:
8155 case BUILT_IN_BSWAP32:
8156 case BUILT_IN_BSWAP64:
8158 signop sgn = TYPE_SIGN (type);
8159 tree result =
8160 wide_int_to_tree (type,
8161 wide_int::from (arg, TYPE_PRECISION (type),
8162 sgn).bswap ());
8163 return result;
8165 default:
8166 gcc_unreachable ();
8170 return NULL_TREE;
8173 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8174 NULL_TREE if no simplification can be made. */
8176 static tree
8177 fold_builtin_hypot (location_t loc, tree fndecl,
8178 tree arg0, tree arg1, tree type)
8180 tree res, narg0, narg1;
8182 if (!validate_arg (arg0, REAL_TYPE)
8183 || !validate_arg (arg1, REAL_TYPE))
8184 return NULL_TREE;
8186 /* Calculate the result when the argument is a constant. */
8187 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8188 return res;
8190 /* If either argument to hypot has a negate or abs, strip that off.
8191 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8192 narg0 = fold_strip_sign_ops (arg0);
8193 narg1 = fold_strip_sign_ops (arg1);
8194 if (narg0 || narg1)
8196 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8197 narg1 ? narg1 : arg1);
8200 /* If either argument is zero, hypot is fabs of the other. */
8201 if (real_zerop (arg0))
8202 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8203 else if (real_zerop (arg1))
8204 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8206 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8207 if (flag_unsafe_math_optimizations
8208 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8209 return fold_build2_loc (loc, MULT_EXPR, type,
8210 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8211 build_real_truncate (type, dconst_sqrt2 ()));
8213 return NULL_TREE;
8217 /* Fold a builtin function call to pow, powf, or powl. Return
8218 NULL_TREE if no simplification can be made. */
8219 static tree
8220 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8222 tree res;
8224 if (!validate_arg (arg0, REAL_TYPE)
8225 || !validate_arg (arg1, REAL_TYPE))
8226 return NULL_TREE;
8228 /* Calculate the result when the argument is a constant. */
8229 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8230 return res;
8232 /* Optimize pow(1.0,y) = 1.0. */
8233 if (real_onep (arg0))
8234 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8236 if (TREE_CODE (arg1) == REAL_CST
8237 && !TREE_OVERFLOW (arg1))
8239 REAL_VALUE_TYPE cint;
8240 REAL_VALUE_TYPE c;
8241 HOST_WIDE_INT n;
8243 c = TREE_REAL_CST (arg1);
8245 /* Optimize pow(x,0.0) = 1.0. */
8246 if (real_equal (&c, &dconst0))
8247 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8248 arg0);
8250 /* Optimize pow(x,1.0) = x. */
8251 if (real_equal (&c, &dconst1))
8252 return arg0;
8254 /* Optimize pow(x,-1.0) = 1.0/x. */
8255 if (real_equal (&c, &dconstm1))
8256 return fold_build2_loc (loc, RDIV_EXPR, type,
8257 build_real (type, dconst1), arg0);
8259 /* Optimize pow(x,0.5) = sqrt(x). */
8260 if (flag_unsafe_math_optimizations
8261 && real_equal (&c, &dconsthalf))
8263 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8265 if (sqrtfn != NULL_TREE)
8266 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8269 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8270 if (flag_unsafe_math_optimizations)
8272 const REAL_VALUE_TYPE dconstroot
8273 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8275 if (real_equal (&c, &dconstroot))
8277 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8278 if (cbrtfn != NULL_TREE)
8279 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8283 /* Check for an integer exponent. */
8284 n = real_to_integer (&c);
8285 real_from_integer (&cint, VOIDmode, n, SIGNED);
8286 if (real_identical (&c, &cint))
8288 /* Attempt to evaluate pow at compile-time, unless this should
8289 raise an exception. */
8290 if (TREE_CODE (arg0) == REAL_CST
8291 && !TREE_OVERFLOW (arg0)
8292 && (n > 0
8293 || (!flag_trapping_math && !flag_errno_math)
8294 || !real_equal (&TREE_REAL_CST (arg0), &dconst0)))
8296 REAL_VALUE_TYPE x;
8297 bool inexact;
8299 x = TREE_REAL_CST (arg0);
8300 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8301 if (flag_unsafe_math_optimizations || !inexact)
8302 return build_real (type, x);
8305 /* Strip sign ops from even integer powers. */
8306 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8308 tree narg0 = fold_strip_sign_ops (arg0);
8309 if (narg0)
8310 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8315 if (flag_unsafe_math_optimizations)
8317 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8319 /* Optimize pow(expN(x),y) = expN(x*y). */
8320 if (BUILTIN_EXPONENT_P (fcode))
8322 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8323 tree arg = CALL_EXPR_ARG (arg0, 0);
8324 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8325 return build_call_expr_loc (loc, expfn, 1, arg);
8328 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8329 if (BUILTIN_SQRT_P (fcode))
8331 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8332 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8333 build_real (type, dconsthalf));
8334 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8337 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8338 if (BUILTIN_CBRT_P (fcode))
8340 tree arg = CALL_EXPR_ARG (arg0, 0);
8341 if (tree_expr_nonnegative_p (arg))
8343 tree c = build_real_truncate (type, dconst_third ());
8344 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1, c);
8345 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8349 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8350 if (fcode == BUILT_IN_POW
8351 || fcode == BUILT_IN_POWF
8352 || fcode == BUILT_IN_POWL)
8354 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8355 if (tree_expr_nonnegative_p (arg00))
8357 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8358 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8359 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8364 return NULL_TREE;
8367 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8368 Return NULL_TREE if no simplification can be made. */
8369 static tree
8370 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8371 tree arg0, tree arg1, tree type)
8373 if (!validate_arg (arg0, REAL_TYPE)
8374 || !validate_arg (arg1, INTEGER_TYPE))
8375 return NULL_TREE;
8377 /* Optimize pow(1.0,y) = 1.0. */
8378 if (real_onep (arg0))
8379 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8381 if (tree_fits_shwi_p (arg1))
8383 HOST_WIDE_INT c = tree_to_shwi (arg1);
8385 /* Evaluate powi at compile-time. */
8386 if (TREE_CODE (arg0) == REAL_CST
8387 && !TREE_OVERFLOW (arg0))
8389 REAL_VALUE_TYPE x;
8390 x = TREE_REAL_CST (arg0);
8391 real_powi (&x, TYPE_MODE (type), &x, c);
8392 return build_real (type, x);
8395 /* Optimize pow(x,0) = 1.0. */
8396 if (c == 0)
8397 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8398 arg0);
8400 /* Optimize pow(x,1) = x. */
8401 if (c == 1)
8402 return arg0;
8404 /* Optimize pow(x,-1) = 1.0/x. */
8405 if (c == -1)
8406 return fold_build2_loc (loc, RDIV_EXPR, type,
8407 build_real (type, dconst1), arg0);
8410 return NULL_TREE;
8413 /* A subroutine of fold_builtin to fold the various exponent
8414 functions. Return NULL_TREE if no simplification can be made.
8415 FUNC is the corresponding MPFR exponent function. */
8417 static tree
8418 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8419 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8421 if (validate_arg (arg, REAL_TYPE))
8423 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8424 tree res;
8426 /* Calculate the result when the argument is a constant. */
8427 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8428 return res;
8430 /* Optimize expN(logN(x)) = x. */
8431 if (flag_unsafe_math_optimizations)
8433 const enum built_in_function fcode = builtin_mathfn_code (arg);
8435 if ((func == mpfr_exp
8436 && (fcode == BUILT_IN_LOG
8437 || fcode == BUILT_IN_LOGF
8438 || fcode == BUILT_IN_LOGL))
8439 || (func == mpfr_exp2
8440 && (fcode == BUILT_IN_LOG2
8441 || fcode == BUILT_IN_LOG2F
8442 || fcode == BUILT_IN_LOG2L))
8443 || (func == mpfr_exp10
8444 && (fcode == BUILT_IN_LOG10
8445 || fcode == BUILT_IN_LOG10F
8446 || fcode == BUILT_IN_LOG10L)))
8447 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8451 return NULL_TREE;
8454 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8455 arguments to the call, and TYPE is its return type.
8456 Return NULL_TREE if no simplification can be made. */
8458 static tree
8459 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8461 if (!validate_arg (arg1, POINTER_TYPE)
8462 || !validate_arg (arg2, INTEGER_TYPE)
8463 || !validate_arg (len, INTEGER_TYPE))
8464 return NULL_TREE;
8465 else
8467 const char *p1;
8469 if (TREE_CODE (arg2) != INTEGER_CST
8470 || !tree_fits_uhwi_p (len))
8471 return NULL_TREE;
8473 p1 = c_getstr (arg1);
8474 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8476 char c;
8477 const char *r;
8478 tree tem;
8480 if (target_char_cast (arg2, &c))
8481 return NULL_TREE;
8483 r = (const char *) memchr (p1, c, tree_to_uhwi (len));
8485 if (r == NULL)
8486 return build_int_cst (TREE_TYPE (arg1), 0);
8488 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
8489 return fold_convert_loc (loc, type, tem);
8491 return NULL_TREE;
8495 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8496 Return NULL_TREE if no simplification can be made. */
8498 static tree
8499 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8501 const char *p1, *p2;
8503 if (!validate_arg (arg1, POINTER_TYPE)
8504 || !validate_arg (arg2, POINTER_TYPE)
8505 || !validate_arg (len, INTEGER_TYPE))
8506 return NULL_TREE;
8508 /* If the LEN parameter is zero, return zero. */
8509 if (integer_zerop (len))
8510 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8511 arg1, arg2);
8513 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8514 if (operand_equal_p (arg1, arg2, 0))
8515 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8517 p1 = c_getstr (arg1);
8518 p2 = c_getstr (arg2);
8520 /* If all arguments are constant, and the value of len is not greater
8521 than the lengths of arg1 and arg2, evaluate at compile-time. */
8522 if (tree_fits_uhwi_p (len) && p1 && p2
8523 && compare_tree_int (len, strlen (p1) + 1) <= 0
8524 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8526 const int r = memcmp (p1, p2, tree_to_uhwi (len));
8528 if (r > 0)
8529 return integer_one_node;
8530 else if (r < 0)
8531 return integer_minus_one_node;
8532 else
8533 return integer_zero_node;
8536 /* If len parameter is one, return an expression corresponding to
8537 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8538 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8540 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8541 tree cst_uchar_ptr_node
8542 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8544 tree ind1
8545 = fold_convert_loc (loc, integer_type_node,
8546 build1 (INDIRECT_REF, cst_uchar_node,
8547 fold_convert_loc (loc,
8548 cst_uchar_ptr_node,
8549 arg1)));
8550 tree ind2
8551 = fold_convert_loc (loc, integer_type_node,
8552 build1 (INDIRECT_REF, cst_uchar_node,
8553 fold_convert_loc (loc,
8554 cst_uchar_ptr_node,
8555 arg2)));
8556 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8559 return NULL_TREE;
8562 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8563 Return NULL_TREE if no simplification can be made. */
8565 static tree
8566 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8568 const char *p1, *p2;
8570 if (!validate_arg (arg1, POINTER_TYPE)
8571 || !validate_arg (arg2, POINTER_TYPE))
8572 return NULL_TREE;
8574 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8575 if (operand_equal_p (arg1, arg2, 0))
8576 return integer_zero_node;
8578 p1 = c_getstr (arg1);
8579 p2 = c_getstr (arg2);
8581 if (p1 && p2)
8583 const int i = strcmp (p1, p2);
8584 if (i < 0)
8585 return integer_minus_one_node;
8586 else if (i > 0)
8587 return integer_one_node;
8588 else
8589 return integer_zero_node;
8592 /* If the second arg is "", return *(const unsigned char*)arg1. */
8593 if (p2 && *p2 == '\0')
8595 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8596 tree cst_uchar_ptr_node
8597 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8599 return fold_convert_loc (loc, integer_type_node,
8600 build1 (INDIRECT_REF, cst_uchar_node,
8601 fold_convert_loc (loc,
8602 cst_uchar_ptr_node,
8603 arg1)));
8606 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8607 if (p1 && *p1 == '\0')
8609 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8610 tree cst_uchar_ptr_node
8611 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8613 tree temp
8614 = fold_convert_loc (loc, integer_type_node,
8615 build1 (INDIRECT_REF, cst_uchar_node,
8616 fold_convert_loc (loc,
8617 cst_uchar_ptr_node,
8618 arg2)));
8619 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8622 return NULL_TREE;
8625 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8626 Return NULL_TREE if no simplification can be made. */
8628 static tree
8629 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8631 const char *p1, *p2;
8633 if (!validate_arg (arg1, POINTER_TYPE)
8634 || !validate_arg (arg2, POINTER_TYPE)
8635 || !validate_arg (len, INTEGER_TYPE))
8636 return NULL_TREE;
8638 /* If the LEN parameter is zero, return zero. */
8639 if (integer_zerop (len))
8640 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8641 arg1, arg2);
8643 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8644 if (operand_equal_p (arg1, arg2, 0))
8645 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8647 p1 = c_getstr (arg1);
8648 p2 = c_getstr (arg2);
8650 if (tree_fits_uhwi_p (len) && p1 && p2)
8652 const int i = strncmp (p1, p2, tree_to_uhwi (len));
8653 if (i > 0)
8654 return integer_one_node;
8655 else if (i < 0)
8656 return integer_minus_one_node;
8657 else
8658 return integer_zero_node;
8661 /* If the second arg is "", and the length is greater than zero,
8662 return *(const unsigned char*)arg1. */
8663 if (p2 && *p2 == '\0'
8664 && TREE_CODE (len) == INTEGER_CST
8665 && tree_int_cst_sgn (len) == 1)
8667 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8668 tree cst_uchar_ptr_node
8669 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8671 return fold_convert_loc (loc, integer_type_node,
8672 build1 (INDIRECT_REF, cst_uchar_node,
8673 fold_convert_loc (loc,
8674 cst_uchar_ptr_node,
8675 arg1)));
8678 /* If the first arg is "", and the length is greater than zero,
8679 return -*(const unsigned char*)arg2. */
8680 if (p1 && *p1 == '\0'
8681 && TREE_CODE (len) == INTEGER_CST
8682 && tree_int_cst_sgn (len) == 1)
8684 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8685 tree cst_uchar_ptr_node
8686 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8688 tree temp = fold_convert_loc (loc, integer_type_node,
8689 build1 (INDIRECT_REF, cst_uchar_node,
8690 fold_convert_loc (loc,
8691 cst_uchar_ptr_node,
8692 arg2)));
8693 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8696 /* If len parameter is one, return an expression corresponding to
8697 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8698 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8700 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8701 tree cst_uchar_ptr_node
8702 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8704 tree ind1 = fold_convert_loc (loc, integer_type_node,
8705 build1 (INDIRECT_REF, cst_uchar_node,
8706 fold_convert_loc (loc,
8707 cst_uchar_ptr_node,
8708 arg1)));
8709 tree ind2 = fold_convert_loc (loc, integer_type_node,
8710 build1 (INDIRECT_REF, cst_uchar_node,
8711 fold_convert_loc (loc,
8712 cst_uchar_ptr_node,
8713 arg2)));
8714 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8717 return NULL_TREE;
8720 /* Fold function call to builtin signbit, signbitf or signbitl with argument
8721 ARG. Return NULL_TREE if no simplification can be made. */
8723 static tree
8724 fold_builtin_signbit (location_t loc, tree arg, tree type)
8726 if (!validate_arg (arg, REAL_TYPE))
8727 return NULL_TREE;
8729 /* If ARG is a compile-time constant, determine the result. */
8730 if (TREE_CODE (arg) == REAL_CST
8731 && !TREE_OVERFLOW (arg))
8733 REAL_VALUE_TYPE c;
8735 c = TREE_REAL_CST (arg);
8736 return (REAL_VALUE_NEGATIVE (c)
8737 ? build_one_cst (type)
8738 : build_zero_cst (type));
8741 /* If ARG is non-negative, the result is always zero. */
8742 if (tree_expr_nonnegative_p (arg))
8743 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8745 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8746 if (!HONOR_SIGNED_ZEROS (arg))
8747 return fold_convert (type,
8748 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
8749 build_real (TREE_TYPE (arg), dconst0)));
8751 return NULL_TREE;
8754 /* Fold function call to builtin copysign, copysignf or copysignl with
8755 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
8756 be made. */
8758 static tree
8759 fold_builtin_copysign (location_t loc, tree fndecl,
8760 tree arg1, tree arg2, tree type)
8762 tree tem;
8764 if (!validate_arg (arg1, REAL_TYPE)
8765 || !validate_arg (arg2, REAL_TYPE))
8766 return NULL_TREE;
8768 /* copysign(X,X) is X. */
8769 if (operand_equal_p (arg1, arg2, 0))
8770 return fold_convert_loc (loc, type, arg1);
8772 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8773 if (TREE_CODE (arg1) == REAL_CST
8774 && TREE_CODE (arg2) == REAL_CST
8775 && !TREE_OVERFLOW (arg1)
8776 && !TREE_OVERFLOW (arg2))
8778 REAL_VALUE_TYPE c1, c2;
8780 c1 = TREE_REAL_CST (arg1);
8781 c2 = TREE_REAL_CST (arg2);
8782 /* c1.sign := c2.sign. */
8783 real_copysign (&c1, &c2);
8784 return build_real (type, c1);
8787 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8788 Remember to evaluate Y for side-effects. */
8789 if (tree_expr_nonnegative_p (arg2))
8790 return omit_one_operand_loc (loc, type,
8791 fold_build1_loc (loc, ABS_EXPR, type, arg1),
8792 arg2);
8794 /* Strip sign changing operations for the first argument. */
8795 tem = fold_strip_sign_ops (arg1);
8796 if (tem)
8797 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
8799 return NULL_TREE;
8802 /* Fold a call to builtin isascii with argument ARG. */
8804 static tree
8805 fold_builtin_isascii (location_t loc, tree arg)
8807 if (!validate_arg (arg, INTEGER_TYPE))
8808 return NULL_TREE;
8809 else
8811 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8812 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8813 build_int_cst (integer_type_node,
8814 ~ (unsigned HOST_WIDE_INT) 0x7f));
8815 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8816 arg, integer_zero_node);
8820 /* Fold a call to builtin toascii with argument ARG. */
8822 static tree
8823 fold_builtin_toascii (location_t loc, tree arg)
8825 if (!validate_arg (arg, INTEGER_TYPE))
8826 return NULL_TREE;
8828 /* Transform toascii(c) -> (c & 0x7f). */
8829 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8830 build_int_cst (integer_type_node, 0x7f));
8833 /* Fold a call to builtin isdigit with argument ARG. */
8835 static tree
8836 fold_builtin_isdigit (location_t loc, tree arg)
8838 if (!validate_arg (arg, INTEGER_TYPE))
8839 return NULL_TREE;
8840 else
8842 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8843 /* According to the C standard, isdigit is unaffected by locale.
8844 However, it definitely is affected by the target character set. */
8845 unsigned HOST_WIDE_INT target_digit0
8846 = lang_hooks.to_target_charset ('0');
8848 if (target_digit0 == 0)
8849 return NULL_TREE;
8851 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8852 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8853 build_int_cst (unsigned_type_node, target_digit0));
8854 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8855 build_int_cst (unsigned_type_node, 9));
8859 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8861 static tree
8862 fold_builtin_fabs (location_t loc, tree arg, tree type)
8864 if (!validate_arg (arg, REAL_TYPE))
8865 return NULL_TREE;
8867 arg = fold_convert_loc (loc, type, arg);
8868 if (TREE_CODE (arg) == REAL_CST)
8869 return fold_abs_const (arg, type);
8870 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8873 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8875 static tree
8876 fold_builtin_abs (location_t loc, tree arg, tree type)
8878 if (!validate_arg (arg, INTEGER_TYPE))
8879 return NULL_TREE;
8881 arg = fold_convert_loc (loc, type, arg);
8882 if (TREE_CODE (arg) == INTEGER_CST)
8883 return fold_abs_const (arg, type);
8884 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8887 /* Fold a fma operation with arguments ARG[012]. */
8889 tree
8890 fold_fma (location_t loc ATTRIBUTE_UNUSED,
8891 tree type, tree arg0, tree arg1, tree arg2)
8893 if (TREE_CODE (arg0) == REAL_CST
8894 && TREE_CODE (arg1) == REAL_CST
8895 && TREE_CODE (arg2) == REAL_CST)
8896 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
8898 return NULL_TREE;
8901 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
8903 static tree
8904 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
8906 if (validate_arg (arg0, REAL_TYPE)
8907 && validate_arg (arg1, REAL_TYPE)
8908 && validate_arg (arg2, REAL_TYPE))
8910 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
8911 if (tem)
8912 return tem;
8914 /* ??? Only expand to FMA_EXPR if it's directly supported. */
8915 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
8916 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
8918 return NULL_TREE;
8921 /* Fold a call to builtin fmin or fmax. */
8923 static tree
8924 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
8925 tree type, bool max)
8927 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
8929 /* Calculate the result when the argument is a constant. */
8930 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
8932 if (res)
8933 return res;
8935 /* If either argument is NaN, return the other one. Avoid the
8936 transformation if we get (and honor) a signalling NaN. Using
8937 omit_one_operand() ensures we create a non-lvalue. */
8938 if (TREE_CODE (arg0) == REAL_CST
8939 && real_isnan (&TREE_REAL_CST (arg0))
8940 && (! HONOR_SNANS (arg0)
8941 || ! TREE_REAL_CST (arg0).signalling))
8942 return omit_one_operand_loc (loc, type, arg1, arg0);
8943 if (TREE_CODE (arg1) == REAL_CST
8944 && real_isnan (&TREE_REAL_CST (arg1))
8945 && (! HONOR_SNANS (arg1)
8946 || ! TREE_REAL_CST (arg1).signalling))
8947 return omit_one_operand_loc (loc, type, arg0, arg1);
8949 /* Transform fmin/fmax(x,x) -> x. */
8950 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8951 return omit_one_operand_loc (loc, type, arg0, arg1);
8953 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
8954 functions to return the numeric arg if the other one is NaN.
8955 These tree codes don't honor that, so only transform if
8956 -ffinite-math-only is set. C99 doesn't require -0.0 to be
8957 handled, so we don't have to worry about it either. */
8958 if (flag_finite_math_only)
8959 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
8960 fold_convert_loc (loc, type, arg0),
8961 fold_convert_loc (loc, type, arg1));
8963 return NULL_TREE;
8966 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8968 static tree
8969 fold_builtin_carg (location_t loc, tree arg, tree type)
8971 if (validate_arg (arg, COMPLEX_TYPE)
8972 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8974 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8976 if (atan2_fn)
8978 tree new_arg = builtin_save_expr (arg);
8979 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8980 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8981 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8985 return NULL_TREE;
8988 /* Fold a call to builtin logb/ilogb. */
8990 static tree
8991 fold_builtin_logb (location_t loc, tree arg, tree rettype)
8993 if (! validate_arg (arg, REAL_TYPE))
8994 return NULL_TREE;
8996 STRIP_NOPS (arg);
8998 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9000 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9002 switch (value->cl)
9004 case rvc_nan:
9005 case rvc_inf:
9006 /* If arg is Inf or NaN and we're logb, return it. */
9007 if (TREE_CODE (rettype) == REAL_TYPE)
9009 /* For logb(-Inf) we have to return +Inf. */
9010 if (real_isinf (value) && real_isneg (value))
9012 REAL_VALUE_TYPE tem;
9013 real_inf (&tem);
9014 return build_real (rettype, tem);
9016 return fold_convert_loc (loc, rettype, arg);
9018 /* Fall through... */
9019 case rvc_zero:
9020 /* Zero may set errno and/or raise an exception for logb, also
9021 for ilogb we don't know FP_ILOGB0. */
9022 return NULL_TREE;
9023 case rvc_normal:
9024 /* For normal numbers, proceed iff radix == 2. In GCC,
9025 normalized significands are in the range [0.5, 1.0). We
9026 want the exponent as if they were [1.0, 2.0) so get the
9027 exponent and subtract 1. */
9028 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9029 return fold_convert_loc (loc, rettype,
9030 build_int_cst (integer_type_node,
9031 REAL_EXP (value)-1));
9032 break;
9036 return NULL_TREE;
9039 /* Fold a call to builtin significand, if radix == 2. */
9041 static tree
9042 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9044 if (! validate_arg (arg, REAL_TYPE))
9045 return NULL_TREE;
9047 STRIP_NOPS (arg);
9049 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9051 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9053 switch (value->cl)
9055 case rvc_zero:
9056 case rvc_nan:
9057 case rvc_inf:
9058 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9059 return fold_convert_loc (loc, rettype, arg);
9060 case rvc_normal:
9061 /* For normal numbers, proceed iff radix == 2. */
9062 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9064 REAL_VALUE_TYPE result = *value;
9065 /* In GCC, normalized significands are in the range [0.5,
9066 1.0). We want them to be [1.0, 2.0) so set the
9067 exponent to 1. */
9068 SET_REAL_EXP (&result, 1);
9069 return build_real (rettype, result);
9071 break;
9075 return NULL_TREE;
9078 /* Fold a call to builtin frexp, we can assume the base is 2. */
9080 static tree
9081 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9083 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9084 return NULL_TREE;
9086 STRIP_NOPS (arg0);
9088 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9089 return NULL_TREE;
9091 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9093 /* Proceed if a valid pointer type was passed in. */
9094 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9096 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9097 tree frac, exp;
9099 switch (value->cl)
9101 case rvc_zero:
9102 /* For +-0, return (*exp = 0, +-0). */
9103 exp = integer_zero_node;
9104 frac = arg0;
9105 break;
9106 case rvc_nan:
9107 case rvc_inf:
9108 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9109 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9110 case rvc_normal:
9112 /* Since the frexp function always expects base 2, and in
9113 GCC normalized significands are already in the range
9114 [0.5, 1.0), we have exactly what frexp wants. */
9115 REAL_VALUE_TYPE frac_rvt = *value;
9116 SET_REAL_EXP (&frac_rvt, 0);
9117 frac = build_real (rettype, frac_rvt);
9118 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9120 break;
9121 default:
9122 gcc_unreachable ();
9125 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9126 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9127 TREE_SIDE_EFFECTS (arg1) = 1;
9128 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9131 return NULL_TREE;
9134 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9135 then we can assume the base is two. If it's false, then we have to
9136 check the mode of the TYPE parameter in certain cases. */
9138 static tree
9139 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9140 tree type, bool ldexp)
9142 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9144 STRIP_NOPS (arg0);
9145 STRIP_NOPS (arg1);
9147 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9148 if (real_zerop (arg0) || integer_zerop (arg1)
9149 || (TREE_CODE (arg0) == REAL_CST
9150 && !real_isfinite (&TREE_REAL_CST (arg0))))
9151 return omit_one_operand_loc (loc, type, arg0, arg1);
9153 /* If both arguments are constant, then try to evaluate it. */
9154 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9155 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9156 && tree_fits_shwi_p (arg1))
9158 /* Bound the maximum adjustment to twice the range of the
9159 mode's valid exponents. Use abs to ensure the range is
9160 positive as a sanity check. */
9161 const long max_exp_adj = 2 *
9162 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9163 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9165 /* Get the user-requested adjustment. */
9166 const HOST_WIDE_INT req_exp_adj = tree_to_shwi (arg1);
9168 /* The requested adjustment must be inside this range. This
9169 is a preliminary cap to avoid things like overflow, we
9170 may still fail to compute the result for other reasons. */
9171 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9173 REAL_VALUE_TYPE initial_result;
9175 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9177 /* Ensure we didn't overflow. */
9178 if (! real_isinf (&initial_result))
9180 const REAL_VALUE_TYPE trunc_result
9181 = real_value_truncate (TYPE_MODE (type), initial_result);
9183 /* Only proceed if the target mode can hold the
9184 resulting value. */
9185 if (real_equal (&initial_result, &trunc_result))
9186 return build_real (type, trunc_result);
9192 return NULL_TREE;
9195 /* Fold a call to builtin modf. */
9197 static tree
9198 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9200 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9201 return NULL_TREE;
9203 STRIP_NOPS (arg0);
9205 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9206 return NULL_TREE;
9208 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9210 /* Proceed if a valid pointer type was passed in. */
9211 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9213 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9214 REAL_VALUE_TYPE trunc, frac;
9216 switch (value->cl)
9218 case rvc_nan:
9219 case rvc_zero:
9220 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9221 trunc = frac = *value;
9222 break;
9223 case rvc_inf:
9224 /* For +-Inf, return (*arg1 = arg0, +-0). */
9225 frac = dconst0;
9226 frac.sign = value->sign;
9227 trunc = *value;
9228 break;
9229 case rvc_normal:
9230 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9231 real_trunc (&trunc, VOIDmode, value);
9232 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9233 /* If the original number was negative and already
9234 integral, then the fractional part is -0.0. */
9235 if (value->sign && frac.cl == rvc_zero)
9236 frac.sign = value->sign;
9237 break;
9240 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9241 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9242 build_real (rettype, trunc));
9243 TREE_SIDE_EFFECTS (arg1) = 1;
9244 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9245 build_real (rettype, frac));
9248 return NULL_TREE;
9251 /* Given a location LOC, an interclass builtin function decl FNDECL
9252 and its single argument ARG, return an folded expression computing
9253 the same, or NULL_TREE if we either couldn't or didn't want to fold
9254 (the latter happen if there's an RTL instruction available). */
9256 static tree
9257 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9259 machine_mode mode;
9261 if (!validate_arg (arg, REAL_TYPE))
9262 return NULL_TREE;
9264 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9265 return NULL_TREE;
9267 mode = TYPE_MODE (TREE_TYPE (arg));
9269 /* If there is no optab, try generic code. */
9270 switch (DECL_FUNCTION_CODE (fndecl))
9272 tree result;
9274 CASE_FLT_FN (BUILT_IN_ISINF):
9276 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9277 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9278 tree const type = TREE_TYPE (arg);
9279 REAL_VALUE_TYPE r;
9280 char buf[128];
9282 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9283 real_from_string (&r, buf);
9284 result = build_call_expr (isgr_fn, 2,
9285 fold_build1_loc (loc, ABS_EXPR, type, arg),
9286 build_real (type, r));
9287 return result;
9289 CASE_FLT_FN (BUILT_IN_FINITE):
9290 case BUILT_IN_ISFINITE:
9292 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9293 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9294 tree const type = TREE_TYPE (arg);
9295 REAL_VALUE_TYPE r;
9296 char buf[128];
9298 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9299 real_from_string (&r, buf);
9300 result = build_call_expr (isle_fn, 2,
9301 fold_build1_loc (loc, ABS_EXPR, type, arg),
9302 build_real (type, r));
9303 /*result = fold_build2_loc (loc, UNGT_EXPR,
9304 TREE_TYPE (TREE_TYPE (fndecl)),
9305 fold_build1_loc (loc, ABS_EXPR, type, arg),
9306 build_real (type, r));
9307 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9308 TREE_TYPE (TREE_TYPE (fndecl)),
9309 result);*/
9310 return result;
9312 case BUILT_IN_ISNORMAL:
9314 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9315 islessequal(fabs(x),DBL_MAX). */
9316 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9317 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9318 tree const type = TREE_TYPE (arg);
9319 REAL_VALUE_TYPE rmax, rmin;
9320 char buf[128];
9322 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9323 real_from_string (&rmax, buf);
9324 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9325 real_from_string (&rmin, buf);
9326 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9327 result = build_call_expr (isle_fn, 2, arg,
9328 build_real (type, rmax));
9329 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9330 build_call_expr (isge_fn, 2, arg,
9331 build_real (type, rmin)));
9332 return result;
9334 default:
9335 break;
9338 return NULL_TREE;
9341 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9342 ARG is the argument for the call. */
9344 static tree
9345 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9347 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9348 REAL_VALUE_TYPE r;
9350 if (!validate_arg (arg, REAL_TYPE))
9351 return NULL_TREE;
9353 switch (builtin_index)
9355 case BUILT_IN_ISINF:
9356 if (!HONOR_INFINITIES (arg))
9357 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9359 if (TREE_CODE (arg) == REAL_CST)
9361 r = TREE_REAL_CST (arg);
9362 if (real_isinf (&r))
9363 return real_compare (GT_EXPR, &r, &dconst0)
9364 ? integer_one_node : integer_minus_one_node;
9365 else
9366 return integer_zero_node;
9369 return NULL_TREE;
9371 case BUILT_IN_ISINF_SIGN:
9373 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9374 /* In a boolean context, GCC will fold the inner COND_EXPR to
9375 1. So e.g. "if (isinf_sign(x))" would be folded to just
9376 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9377 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9378 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9379 tree tmp = NULL_TREE;
9381 arg = builtin_save_expr (arg);
9383 if (signbit_fn && isinf_fn)
9385 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9386 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9388 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9389 signbit_call, integer_zero_node);
9390 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9391 isinf_call, integer_zero_node);
9393 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9394 integer_minus_one_node, integer_one_node);
9395 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9396 isinf_call, tmp,
9397 integer_zero_node);
9400 return tmp;
9403 case BUILT_IN_ISFINITE:
9404 if (!HONOR_NANS (arg)
9405 && !HONOR_INFINITIES (arg))
9406 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9408 if (TREE_CODE (arg) == REAL_CST)
9410 r = TREE_REAL_CST (arg);
9411 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9414 return NULL_TREE;
9416 case BUILT_IN_ISNAN:
9417 if (!HONOR_NANS (arg))
9418 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9420 if (TREE_CODE (arg) == REAL_CST)
9422 r = TREE_REAL_CST (arg);
9423 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9426 arg = builtin_save_expr (arg);
9427 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9429 default:
9430 gcc_unreachable ();
9434 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9435 This builtin will generate code to return the appropriate floating
9436 point classification depending on the value of the floating point
9437 number passed in. The possible return values must be supplied as
9438 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9439 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9440 one floating point argument which is "type generic". */
9442 static tree
9443 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9445 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9446 arg, type, res, tmp;
9447 machine_mode mode;
9448 REAL_VALUE_TYPE r;
9449 char buf[128];
9451 /* Verify the required arguments in the original call. */
9452 if (nargs != 6
9453 || !validate_arg (args[0], INTEGER_TYPE)
9454 || !validate_arg (args[1], INTEGER_TYPE)
9455 || !validate_arg (args[2], INTEGER_TYPE)
9456 || !validate_arg (args[3], INTEGER_TYPE)
9457 || !validate_arg (args[4], INTEGER_TYPE)
9458 || !validate_arg (args[5], REAL_TYPE))
9459 return NULL_TREE;
9461 fp_nan = args[0];
9462 fp_infinite = args[1];
9463 fp_normal = args[2];
9464 fp_subnormal = args[3];
9465 fp_zero = args[4];
9466 arg = args[5];
9467 type = TREE_TYPE (arg);
9468 mode = TYPE_MODE (type);
9469 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9471 /* fpclassify(x) ->
9472 isnan(x) ? FP_NAN :
9473 (fabs(x) == Inf ? FP_INFINITE :
9474 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9475 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9477 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9478 build_real (type, dconst0));
9479 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9480 tmp, fp_zero, fp_subnormal);
9482 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9483 real_from_string (&r, buf);
9484 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9485 arg, build_real (type, r));
9486 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9488 if (HONOR_INFINITIES (mode))
9490 real_inf (&r);
9491 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9492 build_real (type, r));
9493 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9494 fp_infinite, res);
9497 if (HONOR_NANS (mode))
9499 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9500 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9503 return res;
9506 /* Fold a call to an unordered comparison function such as
9507 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9508 being called and ARG0 and ARG1 are the arguments for the call.
9509 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9510 the opposite of the desired result. UNORDERED_CODE is used
9511 for modes that can hold NaNs and ORDERED_CODE is used for
9512 the rest. */
9514 static tree
9515 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9516 enum tree_code unordered_code,
9517 enum tree_code ordered_code)
9519 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9520 enum tree_code code;
9521 tree type0, type1;
9522 enum tree_code code0, code1;
9523 tree cmp_type = NULL_TREE;
9525 type0 = TREE_TYPE (arg0);
9526 type1 = TREE_TYPE (arg1);
9528 code0 = TREE_CODE (type0);
9529 code1 = TREE_CODE (type1);
9531 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9532 /* Choose the wider of two real types. */
9533 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9534 ? type0 : type1;
9535 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9536 cmp_type = type0;
9537 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9538 cmp_type = type1;
9540 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9541 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9543 if (unordered_code == UNORDERED_EXPR)
9545 if (!HONOR_NANS (arg0))
9546 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9547 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9550 code = HONOR_NANS (arg0) ? unordered_code : ordered_code;
9551 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9552 fold_build2_loc (loc, code, type, arg0, arg1));
9555 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9556 arithmetics if it can never overflow, or into internal functions that
9557 return both result of arithmetics and overflowed boolean flag in
9558 a complex integer result, or some other check for overflow. */
9560 static tree
9561 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
9562 tree arg0, tree arg1, tree arg2)
9564 enum internal_fn ifn = IFN_LAST;
9565 tree type = TREE_TYPE (TREE_TYPE (arg2));
9566 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
9567 switch (fcode)
9569 case BUILT_IN_ADD_OVERFLOW:
9570 case BUILT_IN_SADD_OVERFLOW:
9571 case BUILT_IN_SADDL_OVERFLOW:
9572 case BUILT_IN_SADDLL_OVERFLOW:
9573 case BUILT_IN_UADD_OVERFLOW:
9574 case BUILT_IN_UADDL_OVERFLOW:
9575 case BUILT_IN_UADDLL_OVERFLOW:
9576 ifn = IFN_ADD_OVERFLOW;
9577 break;
9578 case BUILT_IN_SUB_OVERFLOW:
9579 case BUILT_IN_SSUB_OVERFLOW:
9580 case BUILT_IN_SSUBL_OVERFLOW:
9581 case BUILT_IN_SSUBLL_OVERFLOW:
9582 case BUILT_IN_USUB_OVERFLOW:
9583 case BUILT_IN_USUBL_OVERFLOW:
9584 case BUILT_IN_USUBLL_OVERFLOW:
9585 ifn = IFN_SUB_OVERFLOW;
9586 break;
9587 case BUILT_IN_MUL_OVERFLOW:
9588 case BUILT_IN_SMUL_OVERFLOW:
9589 case BUILT_IN_SMULL_OVERFLOW:
9590 case BUILT_IN_SMULLL_OVERFLOW:
9591 case BUILT_IN_UMUL_OVERFLOW:
9592 case BUILT_IN_UMULL_OVERFLOW:
9593 case BUILT_IN_UMULLL_OVERFLOW:
9594 ifn = IFN_MUL_OVERFLOW;
9595 break;
9596 default:
9597 gcc_unreachable ();
9599 tree ctype = build_complex_type (type);
9600 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
9601 2, arg0, arg1);
9602 tree tgt = save_expr (call);
9603 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9604 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9605 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
9606 tree store
9607 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
9608 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
9611 /* Fold a call to built-in function FNDECL with 0 arguments.
9612 This function returns NULL_TREE if no simplification was possible. */
9614 static tree
9615 fold_builtin_0 (location_t loc, tree fndecl)
9617 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9618 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9619 switch (fcode)
9621 CASE_FLT_FN (BUILT_IN_INF):
9622 case BUILT_IN_INFD32:
9623 case BUILT_IN_INFD64:
9624 case BUILT_IN_INFD128:
9625 return fold_builtin_inf (loc, type, true);
9627 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9628 return fold_builtin_inf (loc, type, false);
9630 case BUILT_IN_CLASSIFY_TYPE:
9631 return fold_builtin_classify_type (NULL_TREE);
9633 default:
9634 break;
9636 return NULL_TREE;
9639 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9640 This function returns NULL_TREE if no simplification was possible. */
9642 static tree
9643 fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
9645 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9646 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9647 switch (fcode)
9649 case BUILT_IN_CONSTANT_P:
9651 tree val = fold_builtin_constant_p (arg0);
9653 /* Gimplification will pull the CALL_EXPR for the builtin out of
9654 an if condition. When not optimizing, we'll not CSE it back.
9655 To avoid link error types of regressions, return false now. */
9656 if (!val && !optimize)
9657 val = integer_zero_node;
9659 return val;
9662 case BUILT_IN_CLASSIFY_TYPE:
9663 return fold_builtin_classify_type (arg0);
9665 case BUILT_IN_STRLEN:
9666 return fold_builtin_strlen (loc, type, arg0);
9668 CASE_FLT_FN (BUILT_IN_FABS):
9669 case BUILT_IN_FABSD32:
9670 case BUILT_IN_FABSD64:
9671 case BUILT_IN_FABSD128:
9672 return fold_builtin_fabs (loc, arg0, type);
9674 case BUILT_IN_ABS:
9675 case BUILT_IN_LABS:
9676 case BUILT_IN_LLABS:
9677 case BUILT_IN_IMAXABS:
9678 return fold_builtin_abs (loc, arg0, type);
9680 CASE_FLT_FN (BUILT_IN_CONJ):
9681 if (validate_arg (arg0, COMPLEX_TYPE)
9682 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9683 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9684 break;
9686 CASE_FLT_FN (BUILT_IN_CREAL):
9687 if (validate_arg (arg0, COMPLEX_TYPE)
9688 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9689 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9690 break;
9692 CASE_FLT_FN (BUILT_IN_CIMAG):
9693 if (validate_arg (arg0, COMPLEX_TYPE)
9694 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9695 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9696 break;
9698 CASE_FLT_FN (BUILT_IN_CCOS):
9699 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ false);
9701 CASE_FLT_FN (BUILT_IN_CCOSH):
9702 return fold_builtin_ccos (loc, arg0, type, fndecl, /*hyper=*/ true);
9704 CASE_FLT_FN (BUILT_IN_CPROJ):
9705 return fold_builtin_cproj (loc, arg0, type);
9707 CASE_FLT_FN (BUILT_IN_CSIN):
9708 if (validate_arg (arg0, COMPLEX_TYPE)
9709 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9710 return do_mpc_arg1 (arg0, type, mpc_sin);
9711 break;
9713 CASE_FLT_FN (BUILT_IN_CSINH):
9714 if (validate_arg (arg0, COMPLEX_TYPE)
9715 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9716 return do_mpc_arg1 (arg0, type, mpc_sinh);
9717 break;
9719 CASE_FLT_FN (BUILT_IN_CTAN):
9720 if (validate_arg (arg0, COMPLEX_TYPE)
9721 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9722 return do_mpc_arg1 (arg0, type, mpc_tan);
9723 break;
9725 CASE_FLT_FN (BUILT_IN_CTANH):
9726 if (validate_arg (arg0, COMPLEX_TYPE)
9727 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9728 return do_mpc_arg1 (arg0, type, mpc_tanh);
9729 break;
9731 CASE_FLT_FN (BUILT_IN_CLOG):
9732 if (validate_arg (arg0, COMPLEX_TYPE)
9733 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9734 return do_mpc_arg1 (arg0, type, mpc_log);
9735 break;
9737 CASE_FLT_FN (BUILT_IN_CSQRT):
9738 if (validate_arg (arg0, COMPLEX_TYPE)
9739 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9740 return do_mpc_arg1 (arg0, type, mpc_sqrt);
9741 break;
9743 CASE_FLT_FN (BUILT_IN_CASIN):
9744 if (validate_arg (arg0, COMPLEX_TYPE)
9745 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9746 return do_mpc_arg1 (arg0, type, mpc_asin);
9747 break;
9749 CASE_FLT_FN (BUILT_IN_CACOS):
9750 if (validate_arg (arg0, COMPLEX_TYPE)
9751 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9752 return do_mpc_arg1 (arg0, type, mpc_acos);
9753 break;
9755 CASE_FLT_FN (BUILT_IN_CATAN):
9756 if (validate_arg (arg0, COMPLEX_TYPE)
9757 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9758 return do_mpc_arg1 (arg0, type, mpc_atan);
9759 break;
9761 CASE_FLT_FN (BUILT_IN_CASINH):
9762 if (validate_arg (arg0, COMPLEX_TYPE)
9763 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9764 return do_mpc_arg1 (arg0, type, mpc_asinh);
9765 break;
9767 CASE_FLT_FN (BUILT_IN_CACOSH):
9768 if (validate_arg (arg0, COMPLEX_TYPE)
9769 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9770 return do_mpc_arg1 (arg0, type, mpc_acosh);
9771 break;
9773 CASE_FLT_FN (BUILT_IN_CATANH):
9774 if (validate_arg (arg0, COMPLEX_TYPE)
9775 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9776 return do_mpc_arg1 (arg0, type, mpc_atanh);
9777 break;
9779 CASE_FLT_FN (BUILT_IN_CABS):
9780 return fold_builtin_cabs (loc, arg0, type, fndecl);
9782 CASE_FLT_FN (BUILT_IN_CARG):
9783 return fold_builtin_carg (loc, arg0, type);
9785 CASE_FLT_FN (BUILT_IN_SQRT):
9786 if (validate_arg (arg0, REAL_TYPE))
9787 return do_mpfr_arg1 (arg0, type, mpfr_sqrt, &dconst0, NULL, true);
9788 break;
9790 CASE_FLT_FN (BUILT_IN_CBRT):
9791 if (validate_arg (arg0, REAL_TYPE))
9792 return do_mpfr_arg1 (arg0, type, mpfr_cbrt, NULL, NULL, 0);
9793 break;
9795 CASE_FLT_FN (BUILT_IN_ASIN):
9796 if (validate_arg (arg0, REAL_TYPE))
9797 return do_mpfr_arg1 (arg0, type, mpfr_asin,
9798 &dconstm1, &dconst1, true);
9799 break;
9801 CASE_FLT_FN (BUILT_IN_ACOS):
9802 if (validate_arg (arg0, REAL_TYPE))
9803 return do_mpfr_arg1 (arg0, type, mpfr_acos,
9804 &dconstm1, &dconst1, true);
9805 break;
9807 CASE_FLT_FN (BUILT_IN_ATAN):
9808 if (validate_arg (arg0, REAL_TYPE))
9809 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
9810 break;
9812 CASE_FLT_FN (BUILT_IN_ASINH):
9813 if (validate_arg (arg0, REAL_TYPE))
9814 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
9815 break;
9817 CASE_FLT_FN (BUILT_IN_ACOSH):
9818 if (validate_arg (arg0, REAL_TYPE))
9819 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
9820 &dconst1, NULL, true);
9821 break;
9823 CASE_FLT_FN (BUILT_IN_ATANH):
9824 if (validate_arg (arg0, REAL_TYPE))
9825 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
9826 &dconstm1, &dconst1, false);
9827 break;
9829 CASE_FLT_FN (BUILT_IN_SIN):
9830 if (validate_arg (arg0, REAL_TYPE))
9831 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
9832 break;
9834 CASE_FLT_FN (BUILT_IN_COS):
9835 return fold_builtin_cos (loc, arg0, type, fndecl);
9837 CASE_FLT_FN (BUILT_IN_TAN):
9838 return fold_builtin_tan (arg0, type);
9840 CASE_FLT_FN (BUILT_IN_CEXP):
9841 return fold_builtin_cexp (loc, arg0, type);
9843 CASE_FLT_FN (BUILT_IN_CEXPI):
9844 if (validate_arg (arg0, REAL_TYPE))
9845 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
9846 break;
9848 CASE_FLT_FN (BUILT_IN_SINH):
9849 if (validate_arg (arg0, REAL_TYPE))
9850 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
9851 break;
9853 CASE_FLT_FN (BUILT_IN_COSH):
9854 return fold_builtin_cosh (loc, arg0, type, fndecl);
9856 CASE_FLT_FN (BUILT_IN_TANH):
9857 if (validate_arg (arg0, REAL_TYPE))
9858 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
9859 break;
9861 CASE_FLT_FN (BUILT_IN_ERF):
9862 if (validate_arg (arg0, REAL_TYPE))
9863 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
9864 break;
9866 CASE_FLT_FN (BUILT_IN_ERFC):
9867 if (validate_arg (arg0, REAL_TYPE))
9868 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
9869 break;
9871 CASE_FLT_FN (BUILT_IN_TGAMMA):
9872 if (validate_arg (arg0, REAL_TYPE))
9873 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
9874 break;
9876 CASE_FLT_FN (BUILT_IN_EXP):
9877 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
9879 CASE_FLT_FN (BUILT_IN_EXP2):
9880 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
9882 CASE_FLT_FN (BUILT_IN_EXP10):
9883 CASE_FLT_FN (BUILT_IN_POW10):
9884 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
9886 CASE_FLT_FN (BUILT_IN_EXPM1):
9887 if (validate_arg (arg0, REAL_TYPE))
9888 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
9889 break;
9891 CASE_FLT_FN (BUILT_IN_LOG):
9892 if (validate_arg (arg0, REAL_TYPE))
9893 return do_mpfr_arg1 (arg0, type, mpfr_log, &dconst0, NULL, false);
9894 break;
9896 CASE_FLT_FN (BUILT_IN_LOG2):
9897 if (validate_arg (arg0, REAL_TYPE))
9898 return do_mpfr_arg1 (arg0, type, mpfr_log2, &dconst0, NULL, false);
9899 break;
9901 CASE_FLT_FN (BUILT_IN_LOG10):
9902 if (validate_arg (arg0, REAL_TYPE))
9903 return do_mpfr_arg1 (arg0, type, mpfr_log10, &dconst0, NULL, false);
9904 break;
9906 CASE_FLT_FN (BUILT_IN_LOG1P):
9907 if (validate_arg (arg0, REAL_TYPE))
9908 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
9909 &dconstm1, NULL, false);
9910 break;
9912 CASE_FLT_FN (BUILT_IN_J0):
9913 if (validate_arg (arg0, REAL_TYPE))
9914 return do_mpfr_arg1 (arg0, type, mpfr_j0,
9915 NULL, NULL, 0);
9916 break;
9918 CASE_FLT_FN (BUILT_IN_J1):
9919 if (validate_arg (arg0, REAL_TYPE))
9920 return do_mpfr_arg1 (arg0, type, mpfr_j1,
9921 NULL, NULL, 0);
9922 break;
9924 CASE_FLT_FN (BUILT_IN_Y0):
9925 if (validate_arg (arg0, REAL_TYPE))
9926 return do_mpfr_arg1 (arg0, type, mpfr_y0,
9927 &dconst0, NULL, false);
9928 break;
9930 CASE_FLT_FN (BUILT_IN_Y1):
9931 if (validate_arg (arg0, REAL_TYPE))
9932 return do_mpfr_arg1 (arg0, type, mpfr_y1,
9933 &dconst0, NULL, false);
9934 break;
9936 CASE_FLT_FN (BUILT_IN_NAN):
9937 case BUILT_IN_NAND32:
9938 case BUILT_IN_NAND64:
9939 case BUILT_IN_NAND128:
9940 return fold_builtin_nan (arg0, type, true);
9942 CASE_FLT_FN (BUILT_IN_NANS):
9943 return fold_builtin_nan (arg0, type, false);
9945 CASE_FLT_FN (BUILT_IN_FLOOR):
9946 return fold_builtin_floor (loc, fndecl, arg0);
9948 CASE_FLT_FN (BUILT_IN_CEIL):
9949 return fold_builtin_ceil (loc, fndecl, arg0);
9951 CASE_FLT_FN (BUILT_IN_TRUNC):
9952 return fold_builtin_trunc (loc, fndecl, arg0);
9954 CASE_FLT_FN (BUILT_IN_ROUND):
9955 return fold_builtin_round (loc, fndecl, arg0);
9957 CASE_FLT_FN (BUILT_IN_NEARBYINT):
9958 CASE_FLT_FN (BUILT_IN_RINT):
9959 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
9961 CASE_FLT_FN (BUILT_IN_ICEIL):
9962 CASE_FLT_FN (BUILT_IN_LCEIL):
9963 CASE_FLT_FN (BUILT_IN_LLCEIL):
9964 CASE_FLT_FN (BUILT_IN_LFLOOR):
9965 CASE_FLT_FN (BUILT_IN_IFLOOR):
9966 CASE_FLT_FN (BUILT_IN_LLFLOOR):
9967 CASE_FLT_FN (BUILT_IN_IROUND):
9968 CASE_FLT_FN (BUILT_IN_LROUND):
9969 CASE_FLT_FN (BUILT_IN_LLROUND):
9970 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
9972 CASE_FLT_FN (BUILT_IN_IRINT):
9973 CASE_FLT_FN (BUILT_IN_LRINT):
9974 CASE_FLT_FN (BUILT_IN_LLRINT):
9975 return fold_fixed_mathfn (loc, fndecl, arg0);
9977 case BUILT_IN_BSWAP16:
9978 case BUILT_IN_BSWAP32:
9979 case BUILT_IN_BSWAP64:
9980 return fold_builtin_bswap (fndecl, arg0);
9982 CASE_INT_FN (BUILT_IN_FFS):
9983 CASE_INT_FN (BUILT_IN_CLZ):
9984 CASE_INT_FN (BUILT_IN_CTZ):
9985 CASE_INT_FN (BUILT_IN_CLRSB):
9986 CASE_INT_FN (BUILT_IN_POPCOUNT):
9987 CASE_INT_FN (BUILT_IN_PARITY):
9988 return fold_builtin_bitop (fndecl, arg0);
9990 CASE_FLT_FN (BUILT_IN_SIGNBIT):
9991 return fold_builtin_signbit (loc, arg0, type);
9993 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
9994 return fold_builtin_significand (loc, arg0, type);
9996 CASE_FLT_FN (BUILT_IN_ILOGB):
9997 CASE_FLT_FN (BUILT_IN_LOGB):
9998 return fold_builtin_logb (loc, arg0, type);
10000 case BUILT_IN_ISASCII:
10001 return fold_builtin_isascii (loc, arg0);
10003 case BUILT_IN_TOASCII:
10004 return fold_builtin_toascii (loc, arg0);
10006 case BUILT_IN_ISDIGIT:
10007 return fold_builtin_isdigit (loc, arg0);
10009 CASE_FLT_FN (BUILT_IN_FINITE):
10010 case BUILT_IN_FINITED32:
10011 case BUILT_IN_FINITED64:
10012 case BUILT_IN_FINITED128:
10013 case BUILT_IN_ISFINITE:
10015 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10016 if (ret)
10017 return ret;
10018 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10021 CASE_FLT_FN (BUILT_IN_ISINF):
10022 case BUILT_IN_ISINFD32:
10023 case BUILT_IN_ISINFD64:
10024 case BUILT_IN_ISINFD128:
10026 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10027 if (ret)
10028 return ret;
10029 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10032 case BUILT_IN_ISNORMAL:
10033 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10035 case BUILT_IN_ISINF_SIGN:
10036 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10038 CASE_FLT_FN (BUILT_IN_ISNAN):
10039 case BUILT_IN_ISNAND32:
10040 case BUILT_IN_ISNAND64:
10041 case BUILT_IN_ISNAND128:
10042 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10044 case BUILT_IN_FREE:
10045 if (integer_zerop (arg0))
10046 return build_empty_stmt (loc);
10047 break;
10049 default:
10050 break;
10053 return NULL_TREE;
10057 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10058 This function returns NULL_TREE if no simplification was possible. */
10060 static tree
10061 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
10063 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10064 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10066 switch (fcode)
10068 CASE_FLT_FN (BUILT_IN_JN):
10069 if (validate_arg (arg0, INTEGER_TYPE)
10070 && validate_arg (arg1, REAL_TYPE))
10071 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10072 break;
10074 CASE_FLT_FN (BUILT_IN_YN):
10075 if (validate_arg (arg0, INTEGER_TYPE)
10076 && validate_arg (arg1, REAL_TYPE))
10077 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10078 &dconst0, false);
10079 break;
10081 CASE_FLT_FN (BUILT_IN_DREM):
10082 CASE_FLT_FN (BUILT_IN_REMAINDER):
10083 if (validate_arg (arg0, REAL_TYPE)
10084 && validate_arg (arg1, REAL_TYPE))
10085 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10086 break;
10088 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10089 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10090 if (validate_arg (arg0, REAL_TYPE)
10091 && validate_arg (arg1, POINTER_TYPE))
10092 return do_mpfr_lgamma_r (arg0, arg1, type);
10093 break;
10095 CASE_FLT_FN (BUILT_IN_ATAN2):
10096 if (validate_arg (arg0, REAL_TYPE)
10097 && validate_arg (arg1, REAL_TYPE))
10098 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10099 break;
10101 CASE_FLT_FN (BUILT_IN_FDIM):
10102 if (validate_arg (arg0, REAL_TYPE)
10103 && validate_arg (arg1, REAL_TYPE))
10104 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10105 break;
10107 CASE_FLT_FN (BUILT_IN_HYPOT):
10108 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10110 CASE_FLT_FN (BUILT_IN_CPOW):
10111 if (validate_arg (arg0, COMPLEX_TYPE)
10112 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10113 && validate_arg (arg1, COMPLEX_TYPE)
10114 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10115 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10116 break;
10118 CASE_FLT_FN (BUILT_IN_LDEXP):
10119 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10120 CASE_FLT_FN (BUILT_IN_SCALBN):
10121 CASE_FLT_FN (BUILT_IN_SCALBLN):
10122 return fold_builtin_load_exponent (loc, arg0, arg1,
10123 type, /*ldexp=*/false);
10125 CASE_FLT_FN (BUILT_IN_FREXP):
10126 return fold_builtin_frexp (loc, arg0, arg1, type);
10128 CASE_FLT_FN (BUILT_IN_MODF):
10129 return fold_builtin_modf (loc, arg0, arg1, type);
10131 case BUILT_IN_STRSTR:
10132 return fold_builtin_strstr (loc, arg0, arg1, type);
10134 case BUILT_IN_STRSPN:
10135 return fold_builtin_strspn (loc, arg0, arg1);
10137 case BUILT_IN_STRCSPN:
10138 return fold_builtin_strcspn (loc, arg0, arg1);
10140 case BUILT_IN_STRCHR:
10141 case BUILT_IN_INDEX:
10142 return fold_builtin_strchr (loc, arg0, arg1, type);
10144 case BUILT_IN_STRRCHR:
10145 case BUILT_IN_RINDEX:
10146 return fold_builtin_strrchr (loc, arg0, arg1, type);
10148 case BUILT_IN_STRCMP:
10149 return fold_builtin_strcmp (loc, arg0, arg1);
10151 case BUILT_IN_STRPBRK:
10152 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10154 case BUILT_IN_EXPECT:
10155 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
10157 CASE_FLT_FN (BUILT_IN_POW):
10158 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10160 CASE_FLT_FN (BUILT_IN_POWI):
10161 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10163 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10164 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10166 CASE_FLT_FN (BUILT_IN_FMIN):
10167 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10169 CASE_FLT_FN (BUILT_IN_FMAX):
10170 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10172 case BUILT_IN_ISGREATER:
10173 return fold_builtin_unordered_cmp (loc, fndecl,
10174 arg0, arg1, UNLE_EXPR, LE_EXPR);
10175 case BUILT_IN_ISGREATEREQUAL:
10176 return fold_builtin_unordered_cmp (loc, fndecl,
10177 arg0, arg1, UNLT_EXPR, LT_EXPR);
10178 case BUILT_IN_ISLESS:
10179 return fold_builtin_unordered_cmp (loc, fndecl,
10180 arg0, arg1, UNGE_EXPR, GE_EXPR);
10181 case BUILT_IN_ISLESSEQUAL:
10182 return fold_builtin_unordered_cmp (loc, fndecl,
10183 arg0, arg1, UNGT_EXPR, GT_EXPR);
10184 case BUILT_IN_ISLESSGREATER:
10185 return fold_builtin_unordered_cmp (loc, fndecl,
10186 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10187 case BUILT_IN_ISUNORDERED:
10188 return fold_builtin_unordered_cmp (loc, fndecl,
10189 arg0, arg1, UNORDERED_EXPR,
10190 NOP_EXPR);
10192 /* We do the folding for va_start in the expander. */
10193 case BUILT_IN_VA_START:
10194 break;
10196 case BUILT_IN_OBJECT_SIZE:
10197 return fold_builtin_object_size (arg0, arg1);
10199 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10200 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10202 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10203 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10205 default:
10206 break;
10208 return NULL_TREE;
10211 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10212 and ARG2.
10213 This function returns NULL_TREE if no simplification was possible. */
10215 static tree
10216 fold_builtin_3 (location_t loc, tree fndecl,
10217 tree arg0, tree arg1, tree arg2)
10219 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10220 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10221 switch (fcode)
10224 CASE_FLT_FN (BUILT_IN_SINCOS):
10225 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10227 CASE_FLT_FN (BUILT_IN_FMA):
10228 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10229 break;
10231 CASE_FLT_FN (BUILT_IN_REMQUO):
10232 if (validate_arg (arg0, REAL_TYPE)
10233 && validate_arg (arg1, REAL_TYPE)
10234 && validate_arg (arg2, POINTER_TYPE))
10235 return do_mpfr_remquo (arg0, arg1, arg2);
10236 break;
10238 case BUILT_IN_STRNCMP:
10239 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10241 case BUILT_IN_MEMCHR:
10242 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10244 case BUILT_IN_BCMP:
10245 case BUILT_IN_MEMCMP:
10246 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10248 case BUILT_IN_EXPECT:
10249 return fold_builtin_expect (loc, arg0, arg1, arg2);
10251 case BUILT_IN_ADD_OVERFLOW:
10252 case BUILT_IN_SUB_OVERFLOW:
10253 case BUILT_IN_MUL_OVERFLOW:
10254 case BUILT_IN_SADD_OVERFLOW:
10255 case BUILT_IN_SADDL_OVERFLOW:
10256 case BUILT_IN_SADDLL_OVERFLOW:
10257 case BUILT_IN_SSUB_OVERFLOW:
10258 case BUILT_IN_SSUBL_OVERFLOW:
10259 case BUILT_IN_SSUBLL_OVERFLOW:
10260 case BUILT_IN_SMUL_OVERFLOW:
10261 case BUILT_IN_SMULL_OVERFLOW:
10262 case BUILT_IN_SMULLL_OVERFLOW:
10263 case BUILT_IN_UADD_OVERFLOW:
10264 case BUILT_IN_UADDL_OVERFLOW:
10265 case BUILT_IN_UADDLL_OVERFLOW:
10266 case BUILT_IN_USUB_OVERFLOW:
10267 case BUILT_IN_USUBL_OVERFLOW:
10268 case BUILT_IN_USUBLL_OVERFLOW:
10269 case BUILT_IN_UMUL_OVERFLOW:
10270 case BUILT_IN_UMULL_OVERFLOW:
10271 case BUILT_IN_UMULLL_OVERFLOW:
10272 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
10274 default:
10275 break;
10277 return NULL_TREE;
10280 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10281 arguments. IGNORE is true if the result of the
10282 function call is ignored. This function returns NULL_TREE if no
10283 simplification was possible. */
10285 tree
10286 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool)
10288 tree ret = NULL_TREE;
10290 switch (nargs)
10292 case 0:
10293 ret = fold_builtin_0 (loc, fndecl);
10294 break;
10295 case 1:
10296 ret = fold_builtin_1 (loc, fndecl, args[0]);
10297 break;
10298 case 2:
10299 ret = fold_builtin_2 (loc, fndecl, args[0], args[1]);
10300 break;
10301 case 3:
10302 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
10303 break;
10304 default:
10305 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
10306 break;
10308 if (ret)
10310 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10311 SET_EXPR_LOCATION (ret, loc);
10312 TREE_NO_WARNING (ret) = 1;
10313 return ret;
10315 return NULL_TREE;
10318 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10319 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10320 of arguments in ARGS to be omitted. OLDNARGS is the number of
10321 elements in ARGS. */
10323 static tree
10324 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10325 int skip, tree fndecl, int n, va_list newargs)
10327 int nargs = oldnargs - skip + n;
10328 tree *buffer;
10330 if (n > 0)
10332 int i, j;
10334 buffer = XALLOCAVEC (tree, nargs);
10335 for (i = 0; i < n; i++)
10336 buffer[i] = va_arg (newargs, tree);
10337 for (j = skip; j < oldnargs; j++, i++)
10338 buffer[i] = args[j];
10340 else
10341 buffer = args + skip;
10343 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10346 /* Return true if FNDECL shouldn't be folded right now.
10347 If a built-in function has an inline attribute always_inline
10348 wrapper, defer folding it after always_inline functions have
10349 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10350 might not be performed. */
10352 bool
10353 avoid_folding_inline_builtin (tree fndecl)
10355 return (DECL_DECLARED_INLINE_P (fndecl)
10356 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10357 && cfun
10358 && !cfun->always_inline_functions_inlined
10359 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10362 /* A wrapper function for builtin folding that prevents warnings for
10363 "statement without effect" and the like, caused by removing the
10364 call node earlier than the warning is generated. */
10366 tree
10367 fold_call_expr (location_t loc, tree exp, bool ignore)
10369 tree ret = NULL_TREE;
10370 tree fndecl = get_callee_fndecl (exp);
10371 if (fndecl
10372 && TREE_CODE (fndecl) == FUNCTION_DECL
10373 && DECL_BUILT_IN (fndecl)
10374 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10375 yet. Defer folding until we see all the arguments
10376 (after inlining). */
10377 && !CALL_EXPR_VA_ARG_PACK (exp))
10379 int nargs = call_expr_nargs (exp);
10381 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10382 instead last argument is __builtin_va_arg_pack (). Defer folding
10383 even in that case, until arguments are finalized. */
10384 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10386 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10387 if (fndecl2
10388 && TREE_CODE (fndecl2) == FUNCTION_DECL
10389 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10390 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10391 return NULL_TREE;
10394 if (avoid_folding_inline_builtin (fndecl))
10395 return NULL_TREE;
10397 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10398 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10399 CALL_EXPR_ARGP (exp), ignore);
10400 else
10402 tree *args = CALL_EXPR_ARGP (exp);
10403 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10404 if (ret)
10405 return ret;
10408 return NULL_TREE;
10411 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
10412 N arguments are passed in the array ARGARRAY. Return a folded
10413 expression or NULL_TREE if no simplification was possible. */
10415 tree
10416 fold_builtin_call_array (location_t loc, tree,
10417 tree fn,
10418 int n,
10419 tree *argarray)
10421 if (TREE_CODE (fn) != ADDR_EXPR)
10422 return NULL_TREE;
10424 tree fndecl = TREE_OPERAND (fn, 0);
10425 if (TREE_CODE (fndecl) == FUNCTION_DECL
10426 && DECL_BUILT_IN (fndecl))
10428 /* If last argument is __builtin_va_arg_pack (), arguments to this
10429 function are not finalized yet. Defer folding until they are. */
10430 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10432 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10433 if (fndecl2
10434 && TREE_CODE (fndecl2) == FUNCTION_DECL
10435 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10436 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10437 return NULL_TREE;
10439 if (avoid_folding_inline_builtin (fndecl))
10440 return NULL_TREE;
10441 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10442 return targetm.fold_builtin (fndecl, n, argarray, false);
10443 else
10444 return fold_builtin_n (loc, fndecl, argarray, n, false);
10447 return NULL_TREE;
10450 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10451 along with N new arguments specified as the "..." parameters. SKIP
10452 is the number of arguments in EXP to be omitted. This function is used
10453 to do varargs-to-varargs transformations. */
10455 static tree
10456 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10458 va_list ap;
10459 tree t;
10461 va_start (ap, n);
10462 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10463 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10464 va_end (ap);
10466 return t;
10469 /* Validate a single argument ARG against a tree code CODE representing
10470 a type. */
10472 static bool
10473 validate_arg (const_tree arg, enum tree_code code)
10475 if (!arg)
10476 return false;
10477 else if (code == POINTER_TYPE)
10478 return POINTER_TYPE_P (TREE_TYPE (arg));
10479 else if (code == INTEGER_TYPE)
10480 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10481 return code == TREE_CODE (TREE_TYPE (arg));
10484 /* This function validates the types of a function call argument list
10485 against a specified list of tree_codes. If the last specifier is a 0,
10486 that represents an ellipses, otherwise the last specifier must be a
10487 VOID_TYPE.
10489 This is the GIMPLE version of validate_arglist. Eventually we want to
10490 completely convert builtins.c to work from GIMPLEs and the tree based
10491 validate_arglist will then be removed. */
10493 bool
10494 validate_gimple_arglist (const gcall *call, ...)
10496 enum tree_code code;
10497 bool res = 0;
10498 va_list ap;
10499 const_tree arg;
10500 size_t i;
10502 va_start (ap, call);
10503 i = 0;
10507 code = (enum tree_code) va_arg (ap, int);
10508 switch (code)
10510 case 0:
10511 /* This signifies an ellipses, any further arguments are all ok. */
10512 res = true;
10513 goto end;
10514 case VOID_TYPE:
10515 /* This signifies an endlink, if no arguments remain, return
10516 true, otherwise return false. */
10517 res = (i == gimple_call_num_args (call));
10518 goto end;
10519 default:
10520 /* If no parameters remain or the parameter's code does not
10521 match the specified code, return false. Otherwise continue
10522 checking any remaining arguments. */
10523 arg = gimple_call_arg (call, i++);
10524 if (!validate_arg (arg, code))
10525 goto end;
10526 break;
10529 while (1);
10531 /* We need gotos here since we can only have one VA_CLOSE in a
10532 function. */
10533 end: ;
10534 va_end (ap);
10536 return res;
10539 /* Default target-specific builtin expander that does nothing. */
10542 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10543 rtx target ATTRIBUTE_UNUSED,
10544 rtx subtarget ATTRIBUTE_UNUSED,
10545 machine_mode mode ATTRIBUTE_UNUSED,
10546 int ignore ATTRIBUTE_UNUSED)
10548 return NULL_RTX;
10551 /* Returns true is EXP represents data that would potentially reside
10552 in a readonly section. */
10554 bool
10555 readonly_data_expr (tree exp)
10557 STRIP_NOPS (exp);
10559 if (TREE_CODE (exp) != ADDR_EXPR)
10560 return false;
10562 exp = get_base_address (TREE_OPERAND (exp, 0));
10563 if (!exp)
10564 return false;
10566 /* Make sure we call decl_readonly_section only for trees it
10567 can handle (since it returns true for everything it doesn't
10568 understand). */
10569 if (TREE_CODE (exp) == STRING_CST
10570 || TREE_CODE (exp) == CONSTRUCTOR
10571 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
10572 return decl_readonly_section (exp, 0);
10573 else
10574 return false;
10577 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10578 to the call, and TYPE is its return type.
10580 Return NULL_TREE if no simplification was possible, otherwise return the
10581 simplified form of the call as a tree.
10583 The simplified form may be a constant or other expression which
10584 computes the same value, but in a more efficient manner (including
10585 calls to other builtin functions).
10587 The call may contain arguments which need to be evaluated, but
10588 which are not useful to determine the result of the call. In
10589 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10590 COMPOUND_EXPR will be an argument which must be evaluated.
10591 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10592 COMPOUND_EXPR in the chain will contain the tree for the simplified
10593 form of the builtin function call. */
10595 static tree
10596 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
10598 if (!validate_arg (s1, POINTER_TYPE)
10599 || !validate_arg (s2, POINTER_TYPE))
10600 return NULL_TREE;
10601 else
10603 tree fn;
10604 const char *p1, *p2;
10606 p2 = c_getstr (s2);
10607 if (p2 == NULL)
10608 return NULL_TREE;
10610 p1 = c_getstr (s1);
10611 if (p1 != NULL)
10613 const char *r = strstr (p1, p2);
10614 tree tem;
10616 if (r == NULL)
10617 return build_int_cst (TREE_TYPE (s1), 0);
10619 /* Return an offset into the constant string argument. */
10620 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10621 return fold_convert_loc (loc, type, tem);
10624 /* The argument is const char *, and the result is char *, so we need
10625 a type conversion here to avoid a warning. */
10626 if (p2[0] == '\0')
10627 return fold_convert_loc (loc, type, s1);
10629 if (p2[1] != '\0')
10630 return NULL_TREE;
10632 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10633 if (!fn)
10634 return NULL_TREE;
10636 /* New argument list transforming strstr(s1, s2) to
10637 strchr(s1, s2[0]). */
10638 return build_call_expr_loc (loc, fn, 2, s1,
10639 build_int_cst (integer_type_node, p2[0]));
10643 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10644 the call, and TYPE is its return type.
10646 Return NULL_TREE if no simplification was possible, otherwise return the
10647 simplified form of the call as a tree.
10649 The simplified form may be a constant or other expression which
10650 computes the same value, but in a more efficient manner (including
10651 calls to other builtin functions).
10653 The call may contain arguments which need to be evaluated, but
10654 which are not useful to determine the result of the call. In
10655 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10656 COMPOUND_EXPR will be an argument which must be evaluated.
10657 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10658 COMPOUND_EXPR in the chain will contain the tree for the simplified
10659 form of the builtin function call. */
10661 static tree
10662 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
10664 if (!validate_arg (s1, POINTER_TYPE)
10665 || !validate_arg (s2, INTEGER_TYPE))
10666 return NULL_TREE;
10667 else
10669 const char *p1;
10671 if (TREE_CODE (s2) != INTEGER_CST)
10672 return NULL_TREE;
10674 p1 = c_getstr (s1);
10675 if (p1 != NULL)
10677 char c;
10678 const char *r;
10679 tree tem;
10681 if (target_char_cast (s2, &c))
10682 return NULL_TREE;
10684 r = strchr (p1, c);
10686 if (r == NULL)
10687 return build_int_cst (TREE_TYPE (s1), 0);
10689 /* Return an offset into the constant string argument. */
10690 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10691 return fold_convert_loc (loc, type, tem);
10693 return NULL_TREE;
10697 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10698 the call, and TYPE is its return type.
10700 Return NULL_TREE if no simplification was possible, otherwise return the
10701 simplified form of the call as a tree.
10703 The simplified form may be a constant or other expression which
10704 computes the same value, but in a more efficient manner (including
10705 calls to other builtin functions).
10707 The call may contain arguments which need to be evaluated, but
10708 which are not useful to determine the result of the call. In
10709 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10710 COMPOUND_EXPR will be an argument which must be evaluated.
10711 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10712 COMPOUND_EXPR in the chain will contain the tree for the simplified
10713 form of the builtin function call. */
10715 static tree
10716 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
10718 if (!validate_arg (s1, POINTER_TYPE)
10719 || !validate_arg (s2, INTEGER_TYPE))
10720 return NULL_TREE;
10721 else
10723 tree fn;
10724 const char *p1;
10726 if (TREE_CODE (s2) != INTEGER_CST)
10727 return NULL_TREE;
10729 p1 = c_getstr (s1);
10730 if (p1 != NULL)
10732 char c;
10733 const char *r;
10734 tree tem;
10736 if (target_char_cast (s2, &c))
10737 return NULL_TREE;
10739 r = strrchr (p1, c);
10741 if (r == NULL)
10742 return build_int_cst (TREE_TYPE (s1), 0);
10744 /* Return an offset into the constant string argument. */
10745 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10746 return fold_convert_loc (loc, type, tem);
10749 if (! integer_zerop (s2))
10750 return NULL_TREE;
10752 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10753 if (!fn)
10754 return NULL_TREE;
10756 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
10757 return build_call_expr_loc (loc, fn, 2, s1, s2);
10761 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
10762 to the call, and TYPE is its return type.
10764 Return NULL_TREE if no simplification was possible, otherwise return the
10765 simplified form of the call as a tree.
10767 The simplified form may be a constant or other expression which
10768 computes the same value, but in a more efficient manner (including
10769 calls to other builtin functions).
10771 The call may contain arguments which need to be evaluated, but
10772 which are not useful to determine the result of the call. In
10773 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10774 COMPOUND_EXPR will be an argument which must be evaluated.
10775 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10776 COMPOUND_EXPR in the chain will contain the tree for the simplified
10777 form of the builtin function call. */
10779 static tree
10780 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
10782 if (!validate_arg (s1, POINTER_TYPE)
10783 || !validate_arg (s2, POINTER_TYPE))
10784 return NULL_TREE;
10785 else
10787 tree fn;
10788 const char *p1, *p2;
10790 p2 = c_getstr (s2);
10791 if (p2 == NULL)
10792 return NULL_TREE;
10794 p1 = c_getstr (s1);
10795 if (p1 != NULL)
10797 const char *r = strpbrk (p1, p2);
10798 tree tem;
10800 if (r == NULL)
10801 return build_int_cst (TREE_TYPE (s1), 0);
10803 /* Return an offset into the constant string argument. */
10804 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10805 return fold_convert_loc (loc, type, tem);
10808 if (p2[0] == '\0')
10809 /* strpbrk(x, "") == NULL.
10810 Evaluate and ignore s1 in case it had side-effects. */
10811 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
10813 if (p2[1] != '\0')
10814 return NULL_TREE; /* Really call strpbrk. */
10816 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10817 if (!fn)
10818 return NULL_TREE;
10820 /* New argument list transforming strpbrk(s1, s2) to
10821 strchr(s1, s2[0]). */
10822 return build_call_expr_loc (loc, fn, 2, s1,
10823 build_int_cst (integer_type_node, p2[0]));
10827 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
10828 to the call.
10830 Return NULL_TREE if no simplification was possible, otherwise return the
10831 simplified form of the call as a tree.
10833 The simplified form may be a constant or other expression which
10834 computes the same value, but in a more efficient manner (including
10835 calls to other builtin functions).
10837 The call may contain arguments which need to be evaluated, but
10838 which are not useful to determine the result of the call. In
10839 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10840 COMPOUND_EXPR will be an argument which must be evaluated.
10841 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10842 COMPOUND_EXPR in the chain will contain the tree for the simplified
10843 form of the builtin function call. */
10845 static tree
10846 fold_builtin_strspn (location_t loc, tree s1, tree s2)
10848 if (!validate_arg (s1, POINTER_TYPE)
10849 || !validate_arg (s2, POINTER_TYPE))
10850 return NULL_TREE;
10851 else
10853 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10855 /* If both arguments are constants, evaluate at compile-time. */
10856 if (p1 && p2)
10858 const size_t r = strspn (p1, p2);
10859 return build_int_cst (size_type_node, r);
10862 /* If either argument is "", return NULL_TREE. */
10863 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
10864 /* Evaluate and ignore both arguments in case either one has
10865 side-effects. */
10866 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
10867 s1, s2);
10868 return NULL_TREE;
10872 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
10873 to the call.
10875 Return NULL_TREE if no simplification was possible, otherwise return the
10876 simplified form of the call as a tree.
10878 The simplified form may be a constant or other expression which
10879 computes the same value, but in a more efficient manner (including
10880 calls to other builtin functions).
10882 The call may contain arguments which need to be evaluated, but
10883 which are not useful to determine the result of the call. In
10884 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10885 COMPOUND_EXPR will be an argument which must be evaluated.
10886 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10887 COMPOUND_EXPR in the chain will contain the tree for the simplified
10888 form of the builtin function call. */
10890 static tree
10891 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
10893 if (!validate_arg (s1, POINTER_TYPE)
10894 || !validate_arg (s2, POINTER_TYPE))
10895 return NULL_TREE;
10896 else
10898 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10900 /* If both arguments are constants, evaluate at compile-time. */
10901 if (p1 && p2)
10903 const size_t r = strcspn (p1, p2);
10904 return build_int_cst (size_type_node, r);
10907 /* If the first argument is "", return NULL_TREE. */
10908 if (p1 && *p1 == '\0')
10910 /* Evaluate and ignore argument s2 in case it has
10911 side-effects. */
10912 return omit_one_operand_loc (loc, size_type_node,
10913 size_zero_node, s2);
10916 /* If the second argument is "", return __builtin_strlen(s1). */
10917 if (p2 && *p2 == '\0')
10919 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
10921 /* If the replacement _DECL isn't initialized, don't do the
10922 transformation. */
10923 if (!fn)
10924 return NULL_TREE;
10926 return build_call_expr_loc (loc, fn, 1, s1);
10928 return NULL_TREE;
10932 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
10933 produced. False otherwise. This is done so that we don't output the error
10934 or warning twice or three times. */
10936 bool
10937 fold_builtin_next_arg (tree exp, bool va_start_p)
10939 tree fntype = TREE_TYPE (current_function_decl);
10940 int nargs = call_expr_nargs (exp);
10941 tree arg;
10942 /* There is good chance the current input_location points inside the
10943 definition of the va_start macro (perhaps on the token for
10944 builtin) in a system header, so warnings will not be emitted.
10945 Use the location in real source code. */
10946 source_location current_location =
10947 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
10948 NULL);
10950 if (!stdarg_p (fntype))
10952 error ("%<va_start%> used in function with fixed args");
10953 return true;
10956 if (va_start_p)
10958 if (va_start_p && (nargs != 2))
10960 error ("wrong number of arguments to function %<va_start%>");
10961 return true;
10963 arg = CALL_EXPR_ARG (exp, 1);
10965 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10966 when we checked the arguments and if needed issued a warning. */
10967 else
10969 if (nargs == 0)
10971 /* Evidently an out of date version of <stdarg.h>; can't validate
10972 va_start's second argument, but can still work as intended. */
10973 warning_at (current_location,
10974 OPT_Wvarargs,
10975 "%<__builtin_next_arg%> called without an argument");
10976 return true;
10978 else if (nargs > 1)
10980 error ("wrong number of arguments to function %<__builtin_next_arg%>");
10981 return true;
10983 arg = CALL_EXPR_ARG (exp, 0);
10986 if (TREE_CODE (arg) == SSA_NAME)
10987 arg = SSA_NAME_VAR (arg);
10989 /* We destructively modify the call to be __builtin_va_start (ap, 0)
10990 or __builtin_next_arg (0) the first time we see it, after checking
10991 the arguments and if needed issuing a warning. */
10992 if (!integer_zerop (arg))
10994 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10996 /* Strip off all nops for the sake of the comparison. This
10997 is not quite the same as STRIP_NOPS. It does more.
10998 We must also strip off INDIRECT_EXPR for C++ reference
10999 parameters. */
11000 while (CONVERT_EXPR_P (arg)
11001 || TREE_CODE (arg) == INDIRECT_REF)
11002 arg = TREE_OPERAND (arg, 0);
11003 if (arg != last_parm)
11005 /* FIXME: Sometimes with the tree optimizers we can get the
11006 not the last argument even though the user used the last
11007 argument. We just warn and set the arg to be the last
11008 argument so that we will get wrong-code because of
11009 it. */
11010 warning_at (current_location,
11011 OPT_Wvarargs,
11012 "second parameter of %<va_start%> not last named argument");
11015 /* Undefined by C99 7.15.1.4p4 (va_start):
11016 "If the parameter parmN is declared with the register storage
11017 class, with a function or array type, or with a type that is
11018 not compatible with the type that results after application of
11019 the default argument promotions, the behavior is undefined."
11021 else if (DECL_REGISTER (arg))
11023 warning_at (current_location,
11024 OPT_Wvarargs,
11025 "undefined behaviour when second parameter of "
11026 "%<va_start%> is declared with %<register%> storage");
11029 /* We want to verify the second parameter just once before the tree
11030 optimizers are run and then avoid keeping it in the tree,
11031 as otherwise we could warn even for correct code like:
11032 void foo (int i, ...)
11033 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11034 if (va_start_p)
11035 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11036 else
11037 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11039 return false;
11043 /* Expand a call EXP to __builtin_object_size. */
11045 static rtx
11046 expand_builtin_object_size (tree exp)
11048 tree ost;
11049 int object_size_type;
11050 tree fndecl = get_callee_fndecl (exp);
11052 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11054 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11055 exp, fndecl);
11056 expand_builtin_trap ();
11057 return const0_rtx;
11060 ost = CALL_EXPR_ARG (exp, 1);
11061 STRIP_NOPS (ost);
11063 if (TREE_CODE (ost) != INTEGER_CST
11064 || tree_int_cst_sgn (ost) < 0
11065 || compare_tree_int (ost, 3) > 0)
11067 error ("%Klast argument of %D is not integer constant between 0 and 3",
11068 exp, fndecl);
11069 expand_builtin_trap ();
11070 return const0_rtx;
11073 object_size_type = tree_to_shwi (ost);
11075 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11078 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11079 FCODE is the BUILT_IN_* to use.
11080 Return NULL_RTX if we failed; the caller should emit a normal call,
11081 otherwise try to get the result in TARGET, if convenient (and in
11082 mode MODE if that's convenient). */
11084 static rtx
11085 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
11086 enum built_in_function fcode)
11088 tree dest, src, len, size;
11090 if (!validate_arglist (exp,
11091 POINTER_TYPE,
11092 fcode == BUILT_IN_MEMSET_CHK
11093 ? INTEGER_TYPE : POINTER_TYPE,
11094 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11095 return NULL_RTX;
11097 dest = CALL_EXPR_ARG (exp, 0);
11098 src = CALL_EXPR_ARG (exp, 1);
11099 len = CALL_EXPR_ARG (exp, 2);
11100 size = CALL_EXPR_ARG (exp, 3);
11102 if (! tree_fits_uhwi_p (size))
11103 return NULL_RTX;
11105 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
11107 tree fn;
11109 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11111 warning_at (tree_nonartificial_location (exp),
11112 0, "%Kcall to %D will always overflow destination buffer",
11113 exp, get_callee_fndecl (exp));
11114 return NULL_RTX;
11117 fn = NULL_TREE;
11118 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11119 mem{cpy,pcpy,move,set} is available. */
11120 switch (fcode)
11122 case BUILT_IN_MEMCPY_CHK:
11123 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
11124 break;
11125 case BUILT_IN_MEMPCPY_CHK:
11126 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
11127 break;
11128 case BUILT_IN_MEMMOVE_CHK:
11129 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
11130 break;
11131 case BUILT_IN_MEMSET_CHK:
11132 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
11133 break;
11134 default:
11135 break;
11138 if (! fn)
11139 return NULL_RTX;
11141 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11142 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11143 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11144 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11146 else if (fcode == BUILT_IN_MEMSET_CHK)
11147 return NULL_RTX;
11148 else
11150 unsigned int dest_align = get_pointer_alignment (dest);
11152 /* If DEST is not a pointer type, call the normal function. */
11153 if (dest_align == 0)
11154 return NULL_RTX;
11156 /* If SRC and DEST are the same (and not volatile), do nothing. */
11157 if (operand_equal_p (src, dest, 0))
11159 tree expr;
11161 if (fcode != BUILT_IN_MEMPCPY_CHK)
11163 /* Evaluate and ignore LEN in case it has side-effects. */
11164 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11165 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11168 expr = fold_build_pointer_plus (dest, len);
11169 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11172 /* __memmove_chk special case. */
11173 if (fcode == BUILT_IN_MEMMOVE_CHK)
11175 unsigned int src_align = get_pointer_alignment (src);
11177 if (src_align == 0)
11178 return NULL_RTX;
11180 /* If src is categorized for a readonly section we can use
11181 normal __memcpy_chk. */
11182 if (readonly_data_expr (src))
11184 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
11185 if (!fn)
11186 return NULL_RTX;
11187 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11188 dest, src, len, size);
11189 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11190 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11191 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11194 return NULL_RTX;
11198 /* Emit warning if a buffer overflow is detected at compile time. */
11200 static void
11201 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11203 int is_strlen = 0;
11204 tree len, size;
11205 location_t loc = tree_nonartificial_location (exp);
11207 switch (fcode)
11209 case BUILT_IN_STRCPY_CHK:
11210 case BUILT_IN_STPCPY_CHK:
11211 /* For __strcat_chk the warning will be emitted only if overflowing
11212 by at least strlen (dest) + 1 bytes. */
11213 case BUILT_IN_STRCAT_CHK:
11214 len = CALL_EXPR_ARG (exp, 1);
11215 size = CALL_EXPR_ARG (exp, 2);
11216 is_strlen = 1;
11217 break;
11218 case BUILT_IN_STRNCAT_CHK:
11219 case BUILT_IN_STRNCPY_CHK:
11220 case BUILT_IN_STPNCPY_CHK:
11221 len = CALL_EXPR_ARG (exp, 2);
11222 size = CALL_EXPR_ARG (exp, 3);
11223 break;
11224 case BUILT_IN_SNPRINTF_CHK:
11225 case BUILT_IN_VSNPRINTF_CHK:
11226 len = CALL_EXPR_ARG (exp, 1);
11227 size = CALL_EXPR_ARG (exp, 3);
11228 break;
11229 default:
11230 gcc_unreachable ();
11233 if (!len || !size)
11234 return;
11236 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11237 return;
11239 if (is_strlen)
11241 len = c_strlen (len, 1);
11242 if (! len || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11243 return;
11245 else if (fcode == BUILT_IN_STRNCAT_CHK)
11247 tree src = CALL_EXPR_ARG (exp, 1);
11248 if (! src || ! tree_fits_uhwi_p (len) || tree_int_cst_lt (len, size))
11249 return;
11250 src = c_strlen (src, 1);
11251 if (! src || ! tree_fits_uhwi_p (src))
11253 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
11254 exp, get_callee_fndecl (exp));
11255 return;
11257 else if (tree_int_cst_lt (src, size))
11258 return;
11260 else if (! tree_fits_uhwi_p (len) || ! tree_int_cst_lt (size, len))
11261 return;
11263 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
11264 exp, get_callee_fndecl (exp));
11267 /* Emit warning if a buffer overflow is detected at compile time
11268 in __sprintf_chk/__vsprintf_chk calls. */
11270 static void
11271 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11273 tree size, len, fmt;
11274 const char *fmt_str;
11275 int nargs = call_expr_nargs (exp);
11277 /* Verify the required arguments in the original call. */
11279 if (nargs < 4)
11280 return;
11281 size = CALL_EXPR_ARG (exp, 2);
11282 fmt = CALL_EXPR_ARG (exp, 3);
11284 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
11285 return;
11287 /* Check whether the format is a literal string constant. */
11288 fmt_str = c_getstr (fmt);
11289 if (fmt_str == NULL)
11290 return;
11292 if (!init_target_chars ())
11293 return;
11295 /* If the format doesn't contain % args or %%, we know its size. */
11296 if (strchr (fmt_str, target_percent) == 0)
11297 len = build_int_cstu (size_type_node, strlen (fmt_str));
11298 /* If the format is "%s" and first ... argument is a string literal,
11299 we know it too. */
11300 else if (fcode == BUILT_IN_SPRINTF_CHK
11301 && strcmp (fmt_str, target_percent_s) == 0)
11303 tree arg;
11305 if (nargs < 5)
11306 return;
11307 arg = CALL_EXPR_ARG (exp, 4);
11308 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11309 return;
11311 len = c_strlen (arg, 1);
11312 if (!len || ! tree_fits_uhwi_p (len))
11313 return;
11315 else
11316 return;
11318 if (! tree_int_cst_lt (len, size))
11319 warning_at (tree_nonartificial_location (exp),
11320 0, "%Kcall to %D will always overflow destination buffer",
11321 exp, get_callee_fndecl (exp));
11324 /* Emit warning if a free is called with address of a variable. */
11326 static void
11327 maybe_emit_free_warning (tree exp)
11329 tree arg = CALL_EXPR_ARG (exp, 0);
11331 STRIP_NOPS (arg);
11332 if (TREE_CODE (arg) != ADDR_EXPR)
11333 return;
11335 arg = get_base_address (TREE_OPERAND (arg, 0));
11336 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
11337 return;
11339 if (SSA_VAR_P (arg))
11340 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11341 "%Kattempt to free a non-heap object %qD", exp, arg);
11342 else
11343 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11344 "%Kattempt to free a non-heap object", exp);
11347 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11348 if possible. */
11350 static tree
11351 fold_builtin_object_size (tree ptr, tree ost)
11353 unsigned HOST_WIDE_INT bytes;
11354 int object_size_type;
11356 if (!validate_arg (ptr, POINTER_TYPE)
11357 || !validate_arg (ost, INTEGER_TYPE))
11358 return NULL_TREE;
11360 STRIP_NOPS (ost);
11362 if (TREE_CODE (ost) != INTEGER_CST
11363 || tree_int_cst_sgn (ost) < 0
11364 || compare_tree_int (ost, 3) > 0)
11365 return NULL_TREE;
11367 object_size_type = tree_to_shwi (ost);
11369 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11370 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11371 and (size_t) 0 for types 2 and 3. */
11372 if (TREE_SIDE_EFFECTS (ptr))
11373 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11375 if (TREE_CODE (ptr) == ADDR_EXPR)
11377 bytes = compute_builtin_object_size (ptr, object_size_type);
11378 if (wi::fits_to_tree_p (bytes, size_type_node))
11379 return build_int_cstu (size_type_node, bytes);
11381 else if (TREE_CODE (ptr) == SSA_NAME)
11383 /* If object size is not known yet, delay folding until
11384 later. Maybe subsequent passes will help determining
11385 it. */
11386 bytes = compute_builtin_object_size (ptr, object_size_type);
11387 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
11388 && wi::fits_to_tree_p (bytes, size_type_node))
11389 return build_int_cstu (size_type_node, bytes);
11392 return NULL_TREE;
11395 /* Builtins with folding operations that operate on "..." arguments
11396 need special handling; we need to store the arguments in a convenient
11397 data structure before attempting any folding. Fortunately there are
11398 only a few builtins that fall into this category. FNDECL is the
11399 function, EXP is the CALL_EXPR for the call. */
11401 static tree
11402 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
11404 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11405 tree ret = NULL_TREE;
11407 switch (fcode)
11409 case BUILT_IN_FPCLASSIFY:
11410 ret = fold_builtin_fpclassify (loc, args, nargs);
11411 break;
11413 default:
11414 break;
11416 if (ret)
11418 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11419 SET_EXPR_LOCATION (ret, loc);
11420 TREE_NO_WARNING (ret) = 1;
11421 return ret;
11423 return NULL_TREE;
11426 /* Initialize format string characters in the target charset. */
11428 bool
11429 init_target_chars (void)
11431 static bool init;
11432 if (!init)
11434 target_newline = lang_hooks.to_target_charset ('\n');
11435 target_percent = lang_hooks.to_target_charset ('%');
11436 target_c = lang_hooks.to_target_charset ('c');
11437 target_s = lang_hooks.to_target_charset ('s');
11438 if (target_newline == 0 || target_percent == 0 || target_c == 0
11439 || target_s == 0)
11440 return false;
11442 target_percent_c[0] = target_percent;
11443 target_percent_c[1] = target_c;
11444 target_percent_c[2] = '\0';
11446 target_percent_s[0] = target_percent;
11447 target_percent_s[1] = target_s;
11448 target_percent_s[2] = '\0';
11450 target_percent_s_newline[0] = target_percent;
11451 target_percent_s_newline[1] = target_s;
11452 target_percent_s_newline[2] = target_newline;
11453 target_percent_s_newline[3] = '\0';
11455 init = true;
11457 return true;
11460 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11461 and no overflow/underflow occurred. INEXACT is true if M was not
11462 exactly calculated. TYPE is the tree type for the result. This
11463 function assumes that you cleared the MPFR flags and then
11464 calculated M to see if anything subsequently set a flag prior to
11465 entering this function. Return NULL_TREE if any checks fail. */
11467 static tree
11468 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
11470 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11471 overflow/underflow occurred. If -frounding-math, proceed iff the
11472 result of calling FUNC was exact. */
11473 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11474 && (!flag_rounding_math || !inexact))
11476 REAL_VALUE_TYPE rr;
11478 real_from_mpfr (&rr, m, type, GMP_RNDN);
11479 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11480 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11481 but the mpft_t is not, then we underflowed in the
11482 conversion. */
11483 if (real_isfinite (&rr)
11484 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
11486 REAL_VALUE_TYPE rmode;
11488 real_convert (&rmode, TYPE_MODE (type), &rr);
11489 /* Proceed iff the specified mode can hold the value. */
11490 if (real_identical (&rmode, &rr))
11491 return build_real (type, rmode);
11494 return NULL_TREE;
11497 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
11498 number and no overflow/underflow occurred. INEXACT is true if M
11499 was not exactly calculated. TYPE is the tree type for the result.
11500 This function assumes that you cleared the MPFR flags and then
11501 calculated M to see if anything subsequently set a flag prior to
11502 entering this function. Return NULL_TREE if any checks fail, if
11503 FORCE_CONVERT is true, then bypass the checks. */
11505 static tree
11506 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
11508 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11509 overflow/underflow occurred. If -frounding-math, proceed iff the
11510 result of calling FUNC was exact. */
11511 if (force_convert
11512 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
11513 && !mpfr_overflow_p () && !mpfr_underflow_p ()
11514 && (!flag_rounding_math || !inexact)))
11516 REAL_VALUE_TYPE re, im;
11518 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
11519 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
11520 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11521 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11522 but the mpft_t is not, then we underflowed in the
11523 conversion. */
11524 if (force_convert
11525 || (real_isfinite (&re) && real_isfinite (&im)
11526 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
11527 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
11529 REAL_VALUE_TYPE re_mode, im_mode;
11531 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
11532 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
11533 /* Proceed iff the specified mode can hold the value. */
11534 if (force_convert
11535 || (real_identical (&re_mode, &re)
11536 && real_identical (&im_mode, &im)))
11537 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
11538 build_real (TREE_TYPE (type), im_mode));
11541 return NULL_TREE;
11544 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11545 FUNC on it and return the resulting value as a tree with type TYPE.
11546 If MIN and/or MAX are not NULL, then the supplied ARG must be
11547 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11548 acceptable values, otherwise they are not. The mpfr precision is
11549 set to the precision of TYPE. We assume that function FUNC returns
11550 zero if the result could be calculated exactly within the requested
11551 precision. */
11553 static tree
11554 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
11555 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
11556 bool inclusive)
11558 tree result = NULL_TREE;
11560 STRIP_NOPS (arg);
11562 /* To proceed, MPFR must exactly represent the target floating point
11563 format, which only happens when the target base equals two. */
11564 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11565 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
11567 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11569 if (real_isfinite (ra)
11570 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
11571 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
11573 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11574 const int prec = fmt->p;
11575 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11576 int inexact;
11577 mpfr_t m;
11579 mpfr_init2 (m, prec);
11580 mpfr_from_real (m, ra, GMP_RNDN);
11581 mpfr_clear_flags ();
11582 inexact = func (m, m, rnd);
11583 result = do_mpfr_ckconv (m, type, inexact);
11584 mpfr_clear (m);
11588 return result;
11591 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11592 FUNC on it and return the resulting value as a tree with type TYPE.
11593 The mpfr precision is set to the precision of TYPE. We assume that
11594 function FUNC returns zero if the result could be calculated
11595 exactly within the requested precision. */
11597 static tree
11598 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
11599 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11601 tree result = NULL_TREE;
11603 STRIP_NOPS (arg1);
11604 STRIP_NOPS (arg2);
11606 /* To proceed, MPFR must exactly represent the target floating point
11607 format, which only happens when the target base equals two. */
11608 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11609 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11610 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
11612 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11613 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11615 if (real_isfinite (ra1) && real_isfinite (ra2))
11617 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11618 const int prec = fmt->p;
11619 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11620 int inexact;
11621 mpfr_t m1, m2;
11623 mpfr_inits2 (prec, m1, m2, NULL);
11624 mpfr_from_real (m1, ra1, GMP_RNDN);
11625 mpfr_from_real (m2, ra2, GMP_RNDN);
11626 mpfr_clear_flags ();
11627 inexact = func (m1, m1, m2, rnd);
11628 result = do_mpfr_ckconv (m1, type, inexact);
11629 mpfr_clears (m1, m2, NULL);
11633 return result;
11636 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
11637 FUNC on it and return the resulting value as a tree with type TYPE.
11638 The mpfr precision is set to the precision of TYPE. We assume that
11639 function FUNC returns zero if the result could be calculated
11640 exactly within the requested precision. */
11642 static tree
11643 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
11644 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
11646 tree result = NULL_TREE;
11648 STRIP_NOPS (arg1);
11649 STRIP_NOPS (arg2);
11650 STRIP_NOPS (arg3);
11652 /* To proceed, MPFR must exactly represent the target floating point
11653 format, which only happens when the target base equals two. */
11654 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11655 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
11656 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
11657 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
11659 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
11660 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
11661 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
11663 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
11665 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11666 const int prec = fmt->p;
11667 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11668 int inexact;
11669 mpfr_t m1, m2, m3;
11671 mpfr_inits2 (prec, m1, m2, m3, NULL);
11672 mpfr_from_real (m1, ra1, GMP_RNDN);
11673 mpfr_from_real (m2, ra2, GMP_RNDN);
11674 mpfr_from_real (m3, ra3, GMP_RNDN);
11675 mpfr_clear_flags ();
11676 inexact = func (m1, m1, m2, m3, rnd);
11677 result = do_mpfr_ckconv (m1, type, inexact);
11678 mpfr_clears (m1, m2, m3, NULL);
11682 return result;
11685 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11686 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11687 If ARG_SINP and ARG_COSP are NULL then the result is returned
11688 as a complex value.
11689 The type is taken from the type of ARG and is used for setting the
11690 precision of the calculation and results. */
11692 static tree
11693 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
11695 tree const type = TREE_TYPE (arg);
11696 tree result = NULL_TREE;
11698 STRIP_NOPS (arg);
11700 /* To proceed, MPFR must exactly represent the target floating point
11701 format, which only happens when the target base equals two. */
11702 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11703 && TREE_CODE (arg) == REAL_CST
11704 && !TREE_OVERFLOW (arg))
11706 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
11708 if (real_isfinite (ra))
11710 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11711 const int prec = fmt->p;
11712 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11713 tree result_s, result_c;
11714 int inexact;
11715 mpfr_t m, ms, mc;
11717 mpfr_inits2 (prec, m, ms, mc, NULL);
11718 mpfr_from_real (m, ra, GMP_RNDN);
11719 mpfr_clear_flags ();
11720 inexact = mpfr_sin_cos (ms, mc, m, rnd);
11721 result_s = do_mpfr_ckconv (ms, type, inexact);
11722 result_c = do_mpfr_ckconv (mc, type, inexact);
11723 mpfr_clears (m, ms, mc, NULL);
11724 if (result_s && result_c)
11726 /* If we are to return in a complex value do so. */
11727 if (!arg_sinp && !arg_cosp)
11728 return build_complex (build_complex_type (type),
11729 result_c, result_s);
11731 /* Dereference the sin/cos pointer arguments. */
11732 arg_sinp = build_fold_indirect_ref (arg_sinp);
11733 arg_cosp = build_fold_indirect_ref (arg_cosp);
11734 /* Proceed if valid pointer type were passed in. */
11735 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
11736 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
11738 /* Set the values. */
11739 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
11740 result_s);
11741 TREE_SIDE_EFFECTS (result_s) = 1;
11742 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
11743 result_c);
11744 TREE_SIDE_EFFECTS (result_c) = 1;
11745 /* Combine the assignments into a compound expr. */
11746 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11747 result_s, result_c));
11752 return result;
11755 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
11756 two-argument mpfr order N Bessel function FUNC on them and return
11757 the resulting value as a tree with type TYPE. The mpfr precision
11758 is set to the precision of TYPE. We assume that function FUNC
11759 returns zero if the result could be calculated exactly within the
11760 requested precision. */
11761 static tree
11762 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
11763 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
11764 const REAL_VALUE_TYPE *min, bool inclusive)
11766 tree result = NULL_TREE;
11768 STRIP_NOPS (arg1);
11769 STRIP_NOPS (arg2);
11771 /* To proceed, MPFR must exactly represent the target floating point
11772 format, which only happens when the target base equals two. */
11773 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11774 && tree_fits_shwi_p (arg1)
11775 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
11777 const HOST_WIDE_INT n = tree_to_shwi (arg1);
11778 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
11780 if (n == (long)n
11781 && real_isfinite (ra)
11782 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
11784 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11785 const int prec = fmt->p;
11786 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11787 int inexact;
11788 mpfr_t m;
11790 mpfr_init2 (m, prec);
11791 mpfr_from_real (m, ra, GMP_RNDN);
11792 mpfr_clear_flags ();
11793 inexact = func (m, n, m, rnd);
11794 result = do_mpfr_ckconv (m, type, inexact);
11795 mpfr_clear (m);
11799 return result;
11802 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
11803 the pointer *(ARG_QUO) and return the result. The type is taken
11804 from the type of ARG0 and is used for setting the precision of the
11805 calculation and results. */
11807 static tree
11808 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
11810 tree const type = TREE_TYPE (arg0);
11811 tree result = NULL_TREE;
11813 STRIP_NOPS (arg0);
11814 STRIP_NOPS (arg1);
11816 /* To proceed, MPFR must exactly represent the target floating point
11817 format, which only happens when the target base equals two. */
11818 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11819 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
11820 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
11822 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
11823 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
11825 if (real_isfinite (ra0) && real_isfinite (ra1))
11827 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11828 const int prec = fmt->p;
11829 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11830 tree result_rem;
11831 long integer_quo;
11832 mpfr_t m0, m1;
11834 mpfr_inits2 (prec, m0, m1, NULL);
11835 mpfr_from_real (m0, ra0, GMP_RNDN);
11836 mpfr_from_real (m1, ra1, GMP_RNDN);
11837 mpfr_clear_flags ();
11838 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
11839 /* Remquo is independent of the rounding mode, so pass
11840 inexact=0 to do_mpfr_ckconv(). */
11841 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
11842 mpfr_clears (m0, m1, NULL);
11843 if (result_rem)
11845 /* MPFR calculates quo in the host's long so it may
11846 return more bits in quo than the target int can hold
11847 if sizeof(host long) > sizeof(target int). This can
11848 happen even for native compilers in LP64 mode. In
11849 these cases, modulo the quo value with the largest
11850 number that the target int can hold while leaving one
11851 bit for the sign. */
11852 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
11853 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
11855 /* Dereference the quo pointer argument. */
11856 arg_quo = build_fold_indirect_ref (arg_quo);
11857 /* Proceed iff a valid pointer type was passed in. */
11858 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
11860 /* Set the value. */
11861 tree result_quo
11862 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
11863 build_int_cst (TREE_TYPE (arg_quo),
11864 integer_quo));
11865 TREE_SIDE_EFFECTS (result_quo) = 1;
11866 /* Combine the quo assignment with the rem. */
11867 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11868 result_quo, result_rem));
11873 return result;
11876 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
11877 resulting value as a tree with type TYPE. The mpfr precision is
11878 set to the precision of TYPE. We assume that this mpfr function
11879 returns zero if the result could be calculated exactly within the
11880 requested precision. In addition, the integer pointer represented
11881 by ARG_SG will be dereferenced and set to the appropriate signgam
11882 (-1,1) value. */
11884 static tree
11885 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
11887 tree result = NULL_TREE;
11889 STRIP_NOPS (arg);
11891 /* To proceed, MPFR must exactly represent the target floating point
11892 format, which only happens when the target base equals two. Also
11893 verify ARG is a constant and that ARG_SG is an int pointer. */
11894 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11895 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
11896 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
11897 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
11899 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
11901 /* In addition to NaN and Inf, the argument cannot be zero or a
11902 negative integer. */
11903 if (real_isfinite (ra)
11904 && ra->cl != rvc_zero
11905 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
11907 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11908 const int prec = fmt->p;
11909 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
11910 int inexact, sg;
11911 mpfr_t m;
11912 tree result_lg;
11914 mpfr_init2 (m, prec);
11915 mpfr_from_real (m, ra, GMP_RNDN);
11916 mpfr_clear_flags ();
11917 inexact = mpfr_lgamma (m, &sg, m, rnd);
11918 result_lg = do_mpfr_ckconv (m, type, inexact);
11919 mpfr_clear (m);
11920 if (result_lg)
11922 tree result_sg;
11924 /* Dereference the arg_sg pointer argument. */
11925 arg_sg = build_fold_indirect_ref (arg_sg);
11926 /* Assign the signgam value into *arg_sg. */
11927 result_sg = fold_build2 (MODIFY_EXPR,
11928 TREE_TYPE (arg_sg), arg_sg,
11929 build_int_cst (TREE_TYPE (arg_sg), sg));
11930 TREE_SIDE_EFFECTS (result_sg) = 1;
11931 /* Combine the signgam assignment with the lgamma result. */
11932 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11933 result_sg, result_lg));
11938 return result;
11941 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
11942 function FUNC on it and return the resulting value as a tree with
11943 type TYPE. The mpfr precision is set to the precision of TYPE. We
11944 assume that function FUNC returns zero if the result could be
11945 calculated exactly within the requested precision. */
11947 static tree
11948 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
11950 tree result = NULL_TREE;
11952 STRIP_NOPS (arg);
11954 /* To proceed, MPFR must exactly represent the target floating point
11955 format, which only happens when the target base equals two. */
11956 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
11957 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
11958 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
11960 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
11961 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
11963 if (real_isfinite (re) && real_isfinite (im))
11965 const struct real_format *const fmt =
11966 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
11967 const int prec = fmt->p;
11968 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
11969 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
11970 int inexact;
11971 mpc_t m;
11973 mpc_init2 (m, prec);
11974 mpfr_from_real (mpc_realref (m), re, rnd);
11975 mpfr_from_real (mpc_imagref (m), im, rnd);
11976 mpfr_clear_flags ();
11977 inexact = func (m, m, crnd);
11978 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
11979 mpc_clear (m);
11983 return result;
11986 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
11987 mpc function FUNC on it and return the resulting value as a tree
11988 with type TYPE. The mpfr precision is set to the precision of
11989 TYPE. We assume that function FUNC returns zero if the result
11990 could be calculated exactly within the requested precision. If
11991 DO_NONFINITE is true, then fold expressions containing Inf or NaN
11992 in the arguments and/or results. */
11994 tree
11995 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
11996 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
11998 tree result = NULL_TREE;
12000 STRIP_NOPS (arg0);
12001 STRIP_NOPS (arg1);
12003 /* To proceed, MPFR must exactly represent the target floating point
12004 format, which only happens when the target base equals two. */
12005 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
12006 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
12007 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
12008 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
12009 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
12011 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
12012 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
12013 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
12014 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
12016 if (do_nonfinite
12017 || (real_isfinite (re0) && real_isfinite (im0)
12018 && real_isfinite (re1) && real_isfinite (im1)))
12020 const struct real_format *const fmt =
12021 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
12022 const int prec = fmt->p;
12023 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
12024 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
12025 int inexact;
12026 mpc_t m0, m1;
12028 mpc_init2 (m0, prec);
12029 mpc_init2 (m1, prec);
12030 mpfr_from_real (mpc_realref (m0), re0, rnd);
12031 mpfr_from_real (mpc_imagref (m0), im0, rnd);
12032 mpfr_from_real (mpc_realref (m1), re1, rnd);
12033 mpfr_from_real (mpc_imagref (m1), im1, rnd);
12034 mpfr_clear_flags ();
12035 inexact = func (m0, m0, m1, crnd);
12036 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
12037 mpc_clear (m0);
12038 mpc_clear (m1);
12042 return result;
12045 /* A wrapper function for builtin folding that prevents warnings for
12046 "statement without effect" and the like, caused by removing the
12047 call node earlier than the warning is generated. */
12049 tree
12050 fold_call_stmt (gcall *stmt, bool ignore)
12052 tree ret = NULL_TREE;
12053 tree fndecl = gimple_call_fndecl (stmt);
12054 location_t loc = gimple_location (stmt);
12055 if (fndecl
12056 && TREE_CODE (fndecl) == FUNCTION_DECL
12057 && DECL_BUILT_IN (fndecl)
12058 && !gimple_call_va_arg_pack_p (stmt))
12060 int nargs = gimple_call_num_args (stmt);
12061 tree *args = (nargs > 0
12062 ? gimple_call_arg_ptr (stmt, 0)
12063 : &error_mark_node);
12065 if (avoid_folding_inline_builtin (fndecl))
12066 return NULL_TREE;
12067 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
12069 return targetm.fold_builtin (fndecl, nargs, args, ignore);
12071 else
12073 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
12074 if (ret)
12076 /* Propagate location information from original call to
12077 expansion of builtin. Otherwise things like
12078 maybe_emit_chk_warning, that operate on the expansion
12079 of a builtin, will use the wrong location information. */
12080 if (gimple_has_location (stmt))
12082 tree realret = ret;
12083 if (TREE_CODE (ret) == NOP_EXPR)
12084 realret = TREE_OPERAND (ret, 0);
12085 if (CAN_HAVE_LOCATION_P (realret)
12086 && !EXPR_HAS_LOCATION (realret))
12087 SET_EXPR_LOCATION (realret, loc);
12088 return realret;
12090 return ret;
12094 return NULL_TREE;
12097 /* Look up the function in builtin_decl that corresponds to DECL
12098 and set ASMSPEC as its user assembler name. DECL must be a
12099 function decl that declares a builtin. */
12101 void
12102 set_builtin_user_assembler_name (tree decl, const char *asmspec)
12104 tree builtin;
12105 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
12106 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
12107 && asmspec != 0);
12109 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
12110 set_user_assembler_name (builtin, asmspec);
12111 switch (DECL_FUNCTION_CODE (decl))
12113 case BUILT_IN_MEMCPY:
12114 init_block_move_fn (asmspec);
12115 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
12116 break;
12117 case BUILT_IN_MEMSET:
12118 init_block_clear_fn (asmspec);
12119 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
12120 break;
12121 case BUILT_IN_MEMMOVE:
12122 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
12123 break;
12124 case BUILT_IN_MEMCMP:
12125 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
12126 break;
12127 case BUILT_IN_ABORT:
12128 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
12129 break;
12130 case BUILT_IN_FFS:
12131 if (INT_TYPE_SIZE < BITS_PER_WORD)
12133 set_user_assembler_libfunc ("ffs", asmspec);
12134 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
12135 MODE_INT, 0), "ffs");
12137 break;
12138 default:
12139 break;
12143 /* Return true if DECL is a builtin that expands to a constant or similarly
12144 simple code. */
12145 bool
12146 is_simple_builtin (tree decl)
12148 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12149 switch (DECL_FUNCTION_CODE (decl))
12151 /* Builtins that expand to constants. */
12152 case BUILT_IN_CONSTANT_P:
12153 case BUILT_IN_EXPECT:
12154 case BUILT_IN_OBJECT_SIZE:
12155 case BUILT_IN_UNREACHABLE:
12156 /* Simple register moves or loads from stack. */
12157 case BUILT_IN_ASSUME_ALIGNED:
12158 case BUILT_IN_RETURN_ADDRESS:
12159 case BUILT_IN_EXTRACT_RETURN_ADDR:
12160 case BUILT_IN_FROB_RETURN_ADDR:
12161 case BUILT_IN_RETURN:
12162 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
12163 case BUILT_IN_FRAME_ADDRESS:
12164 case BUILT_IN_VA_END:
12165 case BUILT_IN_STACK_SAVE:
12166 case BUILT_IN_STACK_RESTORE:
12167 /* Exception state returns or moves registers around. */
12168 case BUILT_IN_EH_FILTER:
12169 case BUILT_IN_EH_POINTER:
12170 case BUILT_IN_EH_COPY_VALUES:
12171 return true;
12173 default:
12174 return false;
12177 return false;
12180 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12181 most probably expanded inline into reasonably simple code. This is a
12182 superset of is_simple_builtin. */
12183 bool
12184 is_inexpensive_builtin (tree decl)
12186 if (!decl)
12187 return false;
12188 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
12189 return true;
12190 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
12191 switch (DECL_FUNCTION_CODE (decl))
12193 case BUILT_IN_ABS:
12194 case BUILT_IN_ALLOCA:
12195 case BUILT_IN_ALLOCA_WITH_ALIGN:
12196 case BUILT_IN_BSWAP16:
12197 case BUILT_IN_BSWAP32:
12198 case BUILT_IN_BSWAP64:
12199 case BUILT_IN_CLZ:
12200 case BUILT_IN_CLZIMAX:
12201 case BUILT_IN_CLZL:
12202 case BUILT_IN_CLZLL:
12203 case BUILT_IN_CTZ:
12204 case BUILT_IN_CTZIMAX:
12205 case BUILT_IN_CTZL:
12206 case BUILT_IN_CTZLL:
12207 case BUILT_IN_FFS:
12208 case BUILT_IN_FFSIMAX:
12209 case BUILT_IN_FFSL:
12210 case BUILT_IN_FFSLL:
12211 case BUILT_IN_IMAXABS:
12212 case BUILT_IN_FINITE:
12213 case BUILT_IN_FINITEF:
12214 case BUILT_IN_FINITEL:
12215 case BUILT_IN_FINITED32:
12216 case BUILT_IN_FINITED64:
12217 case BUILT_IN_FINITED128:
12218 case BUILT_IN_FPCLASSIFY:
12219 case BUILT_IN_ISFINITE:
12220 case BUILT_IN_ISINF_SIGN:
12221 case BUILT_IN_ISINF:
12222 case BUILT_IN_ISINFF:
12223 case BUILT_IN_ISINFL:
12224 case BUILT_IN_ISINFD32:
12225 case BUILT_IN_ISINFD64:
12226 case BUILT_IN_ISINFD128:
12227 case BUILT_IN_ISNAN:
12228 case BUILT_IN_ISNANF:
12229 case BUILT_IN_ISNANL:
12230 case BUILT_IN_ISNAND32:
12231 case BUILT_IN_ISNAND64:
12232 case BUILT_IN_ISNAND128:
12233 case BUILT_IN_ISNORMAL:
12234 case BUILT_IN_ISGREATER:
12235 case BUILT_IN_ISGREATEREQUAL:
12236 case BUILT_IN_ISLESS:
12237 case BUILT_IN_ISLESSEQUAL:
12238 case BUILT_IN_ISLESSGREATER:
12239 case BUILT_IN_ISUNORDERED:
12240 case BUILT_IN_VA_ARG_PACK:
12241 case BUILT_IN_VA_ARG_PACK_LEN:
12242 case BUILT_IN_VA_COPY:
12243 case BUILT_IN_TRAP:
12244 case BUILT_IN_SAVEREGS:
12245 case BUILT_IN_POPCOUNTL:
12246 case BUILT_IN_POPCOUNTLL:
12247 case BUILT_IN_POPCOUNTIMAX:
12248 case BUILT_IN_POPCOUNT:
12249 case BUILT_IN_PARITYL:
12250 case BUILT_IN_PARITYLL:
12251 case BUILT_IN_PARITYIMAX:
12252 case BUILT_IN_PARITY:
12253 case BUILT_IN_LABS:
12254 case BUILT_IN_LLABS:
12255 case BUILT_IN_PREFETCH:
12256 case BUILT_IN_ACC_ON_DEVICE:
12257 return true;
12259 default:
12260 return is_simple_builtin (decl);
12263 return false;