1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
33 #include "hard-reg-set.h"
36 #include "insn-config.h"
42 #include "typeclass.h"
46 #include "langhooks.h"
47 #include "basic-block.h"
48 #include "tree-mudflap.h"
49 #include "tree-flow.h"
50 #include "value-prof.h"
51 #include "diagnostic-core.h"
55 #ifndef PAD_VARARGS_DOWN
56 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
58 static tree do_mpc_arg1 (tree, tree, int (*)(mpc_ptr, mpc_srcptr, mpc_rnd_t));
60 struct target_builtins default_target_builtins;
62 struct target_builtins *this_target_builtins = &default_target_builtins;
65 /* Define the names of the builtin function types and codes. */
66 const char *const built_in_class_names[4]
67 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
69 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
70 const char * built_in_names[(int) END_BUILTINS] =
72 #include "builtins.def"
76 /* Setup an array of _DECL trees, make sure each element is
77 initialized to NULL_TREE. */
78 builtin_info_type builtin_info;
80 static const char *c_getstr (tree);
81 static rtx c_readstr (const char *, enum machine_mode);
82 static int target_char_cast (tree, char *);
83 static rtx get_memory_rtx (tree, tree);
84 static int apply_args_size (void);
85 static int apply_result_size (void);
86 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
87 static rtx result_vector (int, rtx);
89 static void expand_builtin_update_setjmp_buf (rtx);
90 static void expand_builtin_prefetch (tree);
91 static rtx expand_builtin_apply_args (void);
92 static rtx expand_builtin_apply_args_1 (void);
93 static rtx expand_builtin_apply (rtx, rtx, rtx);
94 static void expand_builtin_return (rtx);
95 static enum type_class type_to_class (tree);
96 static rtx expand_builtin_classify_type (tree);
97 static void expand_errno_check (tree, rtx);
98 static rtx expand_builtin_mathfn (tree, rtx, rtx);
99 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
100 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
101 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
102 static rtx expand_builtin_interclass_mathfn (tree, rtx);
103 static rtx expand_builtin_sincos (tree);
104 static rtx expand_builtin_cexpi (tree, rtx);
105 static rtx expand_builtin_int_roundingfn (tree, rtx);
106 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
107 static rtx expand_builtin_next_arg (void);
108 static rtx expand_builtin_va_start (tree);
109 static rtx expand_builtin_va_end (tree);
110 static rtx expand_builtin_va_copy (tree);
111 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
112 static rtx expand_builtin_strcmp (tree, rtx);
113 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
114 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
115 static rtx expand_builtin_memcpy (tree, rtx);
116 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
117 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
118 enum machine_mode, int);
119 static rtx expand_builtin_strcpy (tree, rtx);
120 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
121 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
122 static rtx expand_builtin_strncpy (tree, rtx);
123 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
124 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
125 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
126 static rtx expand_builtin_bzero (tree);
127 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
128 static rtx expand_builtin_alloca (tree, bool);
129 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
130 static rtx expand_builtin_frame_address (tree, tree);
131 static tree stabilize_va_list_loc (location_t, tree, int);
132 static rtx expand_builtin_expect (tree, rtx);
133 static tree fold_builtin_constant_p (tree);
134 static tree fold_builtin_expect (location_t, tree, tree);
135 static tree fold_builtin_classify_type (tree);
136 static tree fold_builtin_strlen (location_t, tree, tree);
137 static tree fold_builtin_inf (location_t, tree, int);
138 static tree fold_builtin_nan (tree, tree, int);
139 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
140 static bool validate_arg (const_tree, enum tree_code code);
141 static bool integer_valued_real_p (tree);
142 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
143 static bool readonly_data_expr (tree);
144 static rtx expand_builtin_fabs (tree, rtx, rtx);
145 static rtx expand_builtin_signbit (tree, rtx);
146 static tree fold_builtin_sqrt (location_t, tree, tree);
147 static tree fold_builtin_cbrt (location_t, tree, tree);
148 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
149 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
150 static tree fold_builtin_cos (location_t, tree, tree, tree);
151 static tree fold_builtin_cosh (location_t, tree, tree, tree);
152 static tree fold_builtin_tan (tree, tree);
153 static tree fold_builtin_trunc (location_t, tree, tree);
154 static tree fold_builtin_floor (location_t, tree, tree);
155 static tree fold_builtin_ceil (location_t, tree, tree);
156 static tree fold_builtin_round (location_t, tree, tree);
157 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
158 static tree fold_builtin_bitop (tree, tree);
159 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
160 static tree fold_builtin_strchr (location_t, tree, tree, tree);
161 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
162 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
163 static tree fold_builtin_strcmp (location_t, tree, tree);
164 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
165 static tree fold_builtin_signbit (location_t, tree, tree);
166 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
167 static tree fold_builtin_isascii (location_t, tree);
168 static tree fold_builtin_toascii (location_t, tree);
169 static tree fold_builtin_isdigit (location_t, tree);
170 static tree fold_builtin_fabs (location_t, tree, tree);
171 static tree fold_builtin_abs (location_t, tree, tree);
172 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
174 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
175 static tree fold_builtin_0 (location_t, tree, bool);
176 static tree fold_builtin_1 (location_t, tree, tree, bool);
177 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
178 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
179 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
180 static tree fold_builtin_varargs (location_t, tree, tree, bool);
182 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
183 static tree fold_builtin_strstr (location_t, tree, tree, tree);
184 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
185 static tree fold_builtin_strcat (location_t, tree, tree);
186 static tree fold_builtin_strncat (location_t, tree, tree, tree);
187 static tree fold_builtin_strspn (location_t, tree, tree);
188 static tree fold_builtin_strcspn (location_t, tree, tree);
189 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
190 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
192 static rtx expand_builtin_object_size (tree);
193 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
194 enum built_in_function);
195 static void maybe_emit_chk_warning (tree, enum built_in_function);
196 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
197 static void maybe_emit_free_warning (tree);
198 static tree fold_builtin_object_size (tree, tree);
199 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
200 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
201 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
202 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
203 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
204 enum built_in_function);
205 static bool init_target_chars (void);
207 static unsigned HOST_WIDE_INT target_newline;
208 static unsigned HOST_WIDE_INT target_percent;
209 static unsigned HOST_WIDE_INT target_c;
210 static unsigned HOST_WIDE_INT target_s;
211 static char target_percent_c[3];
212 static char target_percent_s[3];
213 static char target_percent_s_newline[4];
214 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
215 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
216 static tree do_mpfr_arg2 (tree, tree, tree,
217 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
218 static tree do_mpfr_arg3 (tree, tree, tree, tree,
219 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
220 static tree do_mpfr_sincos (tree, tree, tree);
221 static tree do_mpfr_bessel_n (tree, tree, tree,
222 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
223 const REAL_VALUE_TYPE *, bool);
224 static tree do_mpfr_remquo (tree, tree, tree);
225 static tree do_mpfr_lgamma_r (tree, tree, tree);
226 static void expand_builtin_sync_synchronize (void);
228 /* Return true if NAME starts with __builtin_ or __sync_. */
231 is_builtin_name (const char *name)
233 if (strncmp (name, "__builtin_", 10) == 0)
235 if (strncmp (name, "__sync_", 7) == 0)
237 if (strncmp (name, "__atomic_", 9) == 0)
243 /* Return true if DECL is a function symbol representing a built-in. */
246 is_builtin_fn (tree decl)
248 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
252 /* Return true if NODE should be considered for inline expansion regardless
253 of the optimization level. This means whenever a function is invoked with
254 its "internal" name, which normally contains the prefix "__builtin". */
257 called_as_built_in (tree node)
259 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
260 we want the name used to call the function, not the name it
262 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
263 return is_builtin_name (name);
266 /* Compute values M and N such that M divides (address of EXP - N) and
267 such that N < M. Store N in *BITPOSP and return M.
269 Note that the address (and thus the alignment) computed here is based
270 on the address to which a symbol resolves, whereas DECL_ALIGN is based
271 on the address at which an object is actually located. These two
272 addresses are not always the same. For example, on ARM targets,
273 the address &foo of a Thumb function foo() has the lowest bit set,
274 whereas foo() itself starts on an even address. */
277 get_object_alignment_1 (tree exp, unsigned HOST_WIDE_INT *bitposp)
279 HOST_WIDE_INT bitsize, bitpos;
281 enum machine_mode mode;
282 int unsignedp, volatilep;
283 unsigned int align, inner;
285 /* Get the innermost object and the constant (bitpos) and possibly
286 variable (offset) offset of the access. */
287 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
288 &mode, &unsignedp, &volatilep, true);
290 /* Extract alignment information from the innermost object and
291 possibly adjust bitpos and offset. */
292 if (TREE_CODE (exp) == CONST_DECL)
293 exp = DECL_INITIAL (exp);
295 && TREE_CODE (exp) != LABEL_DECL)
297 if (TREE_CODE (exp) == FUNCTION_DECL)
299 /* Function addresses can encode extra information besides their
300 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
301 allows the low bit to be used as a virtual bit, we know
302 that the address itself must be 2-byte aligned. */
303 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
304 align = 2 * BITS_PER_UNIT;
306 align = BITS_PER_UNIT;
309 align = DECL_ALIGN (exp);
311 else if (CONSTANT_CLASS_P (exp))
313 align = TYPE_ALIGN (TREE_TYPE (exp));
314 #ifdef CONSTANT_ALIGNMENT
315 align = (unsigned)CONSTANT_ALIGNMENT (exp, align);
318 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
319 align = TYPE_ALIGN (TREE_TYPE (exp));
320 else if (TREE_CODE (exp) == INDIRECT_REF)
321 align = TYPE_ALIGN (TREE_TYPE (exp));
322 else if (TREE_CODE (exp) == MEM_REF)
324 tree addr = TREE_OPERAND (exp, 0);
325 struct ptr_info_def *pi;
326 if (TREE_CODE (addr) == BIT_AND_EXPR
327 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
329 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
330 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
331 align *= BITS_PER_UNIT;
332 addr = TREE_OPERAND (addr, 0);
335 align = BITS_PER_UNIT;
336 if (TREE_CODE (addr) == SSA_NAME
337 && (pi = SSA_NAME_PTR_INFO (addr)))
339 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
340 align = MAX (pi->align * BITS_PER_UNIT, align);
342 else if (TREE_CODE (addr) == ADDR_EXPR)
343 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0)));
344 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
346 else if (TREE_CODE (exp) == TARGET_MEM_REF)
348 struct ptr_info_def *pi;
349 tree addr = TMR_BASE (exp);
350 if (TREE_CODE (addr) == BIT_AND_EXPR
351 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
353 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
354 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
355 align *= BITS_PER_UNIT;
356 addr = TREE_OPERAND (addr, 0);
359 align = BITS_PER_UNIT;
360 if (TREE_CODE (addr) == SSA_NAME
361 && (pi = SSA_NAME_PTR_INFO (addr)))
363 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
364 align = MAX (pi->align * BITS_PER_UNIT, align);
366 else if (TREE_CODE (addr) == ADDR_EXPR)
367 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0)));
368 if (TMR_OFFSET (exp))
369 bitpos += TREE_INT_CST_LOW (TMR_OFFSET (exp)) * BITS_PER_UNIT;
370 if (TMR_INDEX (exp) && TMR_STEP (exp))
372 unsigned HOST_WIDE_INT step = TREE_INT_CST_LOW (TMR_STEP (exp));
373 align = MIN (align, (step & -step) * BITS_PER_UNIT);
375 else if (TMR_INDEX (exp))
376 align = BITS_PER_UNIT;
377 if (TMR_INDEX2 (exp))
378 align = BITS_PER_UNIT;
381 align = BITS_PER_UNIT;
383 /* If there is a non-constant offset part extract the maximum
384 alignment that can prevail. */
390 if (TREE_CODE (offset) == PLUS_EXPR)
392 next_offset = TREE_OPERAND (offset, 0);
393 offset = TREE_OPERAND (offset, 1);
397 if (host_integerp (offset, 1))
399 /* Any overflow in calculating offset_bits won't change
402 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
405 inner = MIN (inner, (offset_bits & -offset_bits));
407 else if (TREE_CODE (offset) == MULT_EXPR
408 && host_integerp (TREE_OPERAND (offset, 1), 1))
410 /* Any overflow in calculating offset_factor won't change
412 unsigned offset_factor
413 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
417 inner = MIN (inner, (offset_factor & -offset_factor));
421 inner = MIN (inner, BITS_PER_UNIT);
424 offset = next_offset;
427 /* Alignment is innermost object alignment adjusted by the constant
428 and non-constant offset parts. */
429 align = MIN (align, inner);
430 bitpos = bitpos & (align - 1);
436 /* Return the alignment in bits of EXP, an object. */
439 get_object_alignment (tree exp)
441 unsigned HOST_WIDE_INT bitpos = 0;
444 align = get_object_alignment_1 (exp, &bitpos);
446 /* align and bitpos now specify known low bits of the pointer.
447 ptr & (align - 1) == bitpos. */
450 align = (bitpos & -bitpos);
455 /* Return the alignment of object EXP, also considering its type when we do
456 not know of explicit misalignment. Only handle MEM_REF and TARGET_MEM_REF.
458 ??? Note that, in the general case, the type of an expression is not kept
459 consistent with misalignment information by the front-end, for example when
460 taking the address of a member of a packed structure. However, in most of
461 the cases, expressions have the alignment of their type so we optimistically
462 fall back to this alignment when we cannot compute a misalignment. */
465 get_object_or_type_alignment (tree exp)
467 unsigned HOST_WIDE_INT misalign;
468 unsigned int align = get_object_alignment_1 (exp, &misalign);
470 gcc_assert (TREE_CODE (exp) == MEM_REF || TREE_CODE (exp) == TARGET_MEM_REF);
473 align = (misalign & -misalign);
475 align = MAX (TYPE_ALIGN (TREE_TYPE (exp)), align);
480 /* For a pointer valued expression EXP compute values M and N such that
481 M divides (EXP - N) and such that N < M. Store N in *BITPOSP and return M.
483 If EXP is not a pointer, 0 is returned. */
486 get_pointer_alignment_1 (tree exp, unsigned HOST_WIDE_INT *bitposp)
490 if (TREE_CODE (exp) == ADDR_EXPR)
491 return get_object_alignment_1 (TREE_OPERAND (exp, 0), bitposp);
492 else if (TREE_CODE (exp) == SSA_NAME
493 && POINTER_TYPE_P (TREE_TYPE (exp)))
495 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
499 return BITS_PER_UNIT;
501 *bitposp = pi->misalign * BITS_PER_UNIT;
502 return pi->align * BITS_PER_UNIT;
506 return POINTER_TYPE_P (TREE_TYPE (exp)) ? BITS_PER_UNIT : 0;
509 /* Return the alignment in bits of EXP, a pointer valued expression.
510 The alignment returned is, by default, the alignment of the thing that
511 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
513 Otherwise, look at the expression to see if we can do better, i.e., if the
514 expression is actually pointing at an object whose alignment is tighter. */
517 get_pointer_alignment (tree exp)
519 unsigned HOST_WIDE_INT bitpos = 0;
522 align = get_pointer_alignment_1 (exp, &bitpos);
524 /* align and bitpos now specify known low bits of the pointer.
525 ptr & (align - 1) == bitpos. */
528 align = (bitpos & -bitpos);
533 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
534 way, because it could contain a zero byte in the middle.
535 TREE_STRING_LENGTH is the size of the character array, not the string.
537 ONLY_VALUE should be nonzero if the result is not going to be emitted
538 into the instruction stream and zero if it is going to be expanded.
539 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
540 is returned, otherwise NULL, since
541 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
542 evaluate the side-effects.
544 The value returned is of type `ssizetype'.
546 Unfortunately, string_constant can't access the values of const char
547 arrays with initializers, so neither can we do so here. */
550 c_strlen (tree src, int only_value)
553 HOST_WIDE_INT offset;
559 if (TREE_CODE (src) == COND_EXPR
560 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
564 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
565 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
566 if (tree_int_cst_equal (len1, len2))
570 if (TREE_CODE (src) == COMPOUND_EXPR
571 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
572 return c_strlen (TREE_OPERAND (src, 1), only_value);
574 loc = EXPR_LOC_OR_HERE (src);
576 src = string_constant (src, &offset_node);
580 max = TREE_STRING_LENGTH (src) - 1;
581 ptr = TREE_STRING_POINTER (src);
583 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
585 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
586 compute the offset to the following null if we don't know where to
587 start searching for it. */
590 for (i = 0; i < max; i++)
594 /* We don't know the starting offset, but we do know that the string
595 has no internal zero bytes. We can assume that the offset falls
596 within the bounds of the string; otherwise, the programmer deserves
597 what he gets. Subtract the offset from the length of the string,
598 and return that. This would perhaps not be valid if we were dealing
599 with named arrays in addition to literal string constants. */
601 return size_diffop_loc (loc, size_int (max), offset_node);
604 /* We have a known offset into the string. Start searching there for
605 a null character if we can represent it as a single HOST_WIDE_INT. */
606 if (offset_node == 0)
608 else if (! host_integerp (offset_node, 0))
611 offset = tree_low_cst (offset_node, 0);
613 /* If the offset is known to be out of bounds, warn, and call strlen at
615 if (offset < 0 || offset > max)
617 /* Suppress multiple warnings for propagated constant strings. */
618 if (! TREE_NO_WARNING (src))
620 warning_at (loc, 0, "offset outside bounds of constant string");
621 TREE_NO_WARNING (src) = 1;
626 /* Use strlen to search for the first zero byte. Since any strings
627 constructed with build_string will have nulls appended, we win even
628 if we get handed something like (char[4])"abcd".
630 Since OFFSET is our starting index into the string, no further
631 calculation is needed. */
632 return ssize_int (strlen (ptr + offset));
635 /* Return a char pointer for a C string if it is a string constant
636 or sum of string constant and integer constant. */
643 src = string_constant (src, &offset_node);
647 if (offset_node == 0)
648 return TREE_STRING_POINTER (src);
649 else if (!host_integerp (offset_node, 1)
650 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
653 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
656 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
657 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
660 c_readstr (const char *str, enum machine_mode mode)
666 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
671 for (i = 0; i < GET_MODE_SIZE (mode); i++)
674 if (WORDS_BIG_ENDIAN)
675 j = GET_MODE_SIZE (mode) - i - 1;
676 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
677 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
678 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
680 gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
683 ch = (unsigned char) str[i];
684 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
686 return immed_double_const (c[0], c[1], mode);
689 /* Cast a target constant CST to target CHAR and if that value fits into
690 host char type, return zero and put that value into variable pointed to by
694 target_char_cast (tree cst, char *p)
696 unsigned HOST_WIDE_INT val, hostval;
698 if (TREE_CODE (cst) != INTEGER_CST
699 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
702 val = TREE_INT_CST_LOW (cst);
703 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
704 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
707 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
708 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
717 /* Similar to save_expr, but assumes that arbitrary code is not executed
718 in between the multiple evaluations. In particular, we assume that a
719 non-addressable local variable will not be modified. */
722 builtin_save_expr (tree exp)
724 if (TREE_CODE (exp) == SSA_NAME
725 || (TREE_ADDRESSABLE (exp) == 0
726 && (TREE_CODE (exp) == PARM_DECL
727 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
730 return save_expr (exp);
733 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
734 times to get the address of either a higher stack frame, or a return
735 address located within it (depending on FNDECL_CODE). */
738 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
742 #ifdef INITIAL_FRAME_ADDRESS_RTX
743 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
747 /* For a zero count with __builtin_return_address, we don't care what
748 frame address we return, because target-specific definitions will
749 override us. Therefore frame pointer elimination is OK, and using
750 the soft frame pointer is OK.
752 For a nonzero count, or a zero count with __builtin_frame_address,
753 we require a stable offset from the current frame pointer to the
754 previous one, so we must use the hard frame pointer, and
755 we must disable frame pointer elimination. */
756 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
757 tem = frame_pointer_rtx;
760 tem = hard_frame_pointer_rtx;
762 /* Tell reload not to eliminate the frame pointer. */
763 crtl->accesses_prior_frames = 1;
767 /* Some machines need special handling before we can access
768 arbitrary frames. For example, on the SPARC, we must first flush
769 all register windows to the stack. */
770 #ifdef SETUP_FRAME_ADDRESSES
772 SETUP_FRAME_ADDRESSES ();
775 /* On the SPARC, the return address is not in the frame, it is in a
776 register. There is no way to access it off of the current frame
777 pointer, but it can be accessed off the previous frame pointer by
778 reading the value from the register window save area. */
779 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
780 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
784 /* Scan back COUNT frames to the specified frame. */
785 for (i = 0; i < count; i++)
787 /* Assume the dynamic chain pointer is in the word that the
788 frame address points to, unless otherwise specified. */
789 #ifdef DYNAMIC_CHAIN_ADDRESS
790 tem = DYNAMIC_CHAIN_ADDRESS (tem);
792 tem = memory_address (Pmode, tem);
793 tem = gen_frame_mem (Pmode, tem);
794 tem = copy_to_reg (tem);
797 /* For __builtin_frame_address, return what we've got. But, on
798 the SPARC for example, we may have to add a bias. */
799 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
800 #ifdef FRAME_ADDR_RTX
801 return FRAME_ADDR_RTX (tem);
806 /* For __builtin_return_address, get the return address from that frame. */
807 #ifdef RETURN_ADDR_RTX
808 tem = RETURN_ADDR_RTX (count, tem);
810 tem = memory_address (Pmode,
811 plus_constant (tem, GET_MODE_SIZE (Pmode)));
812 tem = gen_frame_mem (Pmode, tem);
817 /* Alias set used for setjmp buffer. */
818 static alias_set_type setjmp_alias_set = -1;
820 /* Construct the leading half of a __builtin_setjmp call. Control will
821 return to RECEIVER_LABEL. This is also called directly by the SJLJ
822 exception handling code. */
825 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
827 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
831 if (setjmp_alias_set == -1)
832 setjmp_alias_set = new_alias_set ();
834 buf_addr = convert_memory_address (Pmode, buf_addr);
836 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
838 /* We store the frame pointer and the address of receiver_label in
839 the buffer and use the rest of it for the stack save area, which
840 is machine-dependent. */
842 mem = gen_rtx_MEM (Pmode, buf_addr);
843 set_mem_alias_set (mem, setjmp_alias_set);
844 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
846 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
847 set_mem_alias_set (mem, setjmp_alias_set);
849 emit_move_insn (validize_mem (mem),
850 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
852 stack_save = gen_rtx_MEM (sa_mode,
853 plus_constant (buf_addr,
854 2 * GET_MODE_SIZE (Pmode)));
855 set_mem_alias_set (stack_save, setjmp_alias_set);
856 emit_stack_save (SAVE_NONLOCAL, &stack_save);
858 /* If there is further processing to do, do it. */
859 #ifdef HAVE_builtin_setjmp_setup
860 if (HAVE_builtin_setjmp_setup)
861 emit_insn (gen_builtin_setjmp_setup (buf_addr));
864 /* We have a nonlocal label. */
865 cfun->has_nonlocal_label = 1;
868 /* Construct the trailing part of a __builtin_setjmp call. This is
869 also called directly by the SJLJ exception handling code. */
872 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
876 /* Clobber the FP when we get here, so we have to make sure it's
877 marked as used by this function. */
878 emit_use (hard_frame_pointer_rtx);
880 /* Mark the static chain as clobbered here so life information
881 doesn't get messed up for it. */
882 chain = targetm.calls.static_chain (current_function_decl, true);
883 if (chain && REG_P (chain))
884 emit_clobber (chain);
886 /* Now put in the code to restore the frame pointer, and argument
887 pointer, if needed. */
888 #ifdef HAVE_nonlocal_goto
889 if (! HAVE_nonlocal_goto)
892 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
893 /* This might change the hard frame pointer in ways that aren't
894 apparent to early optimization passes, so force a clobber. */
895 emit_clobber (hard_frame_pointer_rtx);
898 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
899 if (fixed_regs[ARG_POINTER_REGNUM])
901 #ifdef ELIMINABLE_REGS
903 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
905 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
906 if (elim_regs[i].from == ARG_POINTER_REGNUM
907 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
910 if (i == ARRAY_SIZE (elim_regs))
913 /* Now restore our arg pointer from the address at which it
914 was saved in our stack frame. */
915 emit_move_insn (crtl->args.internal_arg_pointer,
916 copy_to_reg (get_arg_pointer_save_area ()));
921 #ifdef HAVE_builtin_setjmp_receiver
922 if (HAVE_builtin_setjmp_receiver)
923 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
926 #ifdef HAVE_nonlocal_goto_receiver
927 if (HAVE_nonlocal_goto_receiver)
928 emit_insn (gen_nonlocal_goto_receiver ());
933 /* We must not allow the code we just generated to be reordered by
934 scheduling. Specifically, the update of the frame pointer must
935 happen immediately, not later. */
936 emit_insn (gen_blockage ());
939 /* __builtin_longjmp is passed a pointer to an array of five words (not
940 all will be used on all machines). It operates similarly to the C
941 library function of the same name, but is more efficient. Much of
942 the code below is copied from the handling of non-local gotos. */
945 expand_builtin_longjmp (rtx buf_addr, rtx value)
947 rtx fp, lab, stack, insn, last;
948 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
950 /* DRAP is needed for stack realign if longjmp is expanded to current
952 if (SUPPORTS_STACK_ALIGNMENT)
953 crtl->need_drap = true;
955 if (setjmp_alias_set == -1)
956 setjmp_alias_set = new_alias_set ();
958 buf_addr = convert_memory_address (Pmode, buf_addr);
960 buf_addr = force_reg (Pmode, buf_addr);
962 /* We require that the user must pass a second argument of 1, because
963 that is what builtin_setjmp will return. */
964 gcc_assert (value == const1_rtx);
966 last = get_last_insn ();
967 #ifdef HAVE_builtin_longjmp
968 if (HAVE_builtin_longjmp)
969 emit_insn (gen_builtin_longjmp (buf_addr));
973 fp = gen_rtx_MEM (Pmode, buf_addr);
974 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
975 GET_MODE_SIZE (Pmode)));
977 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
978 2 * GET_MODE_SIZE (Pmode)));
979 set_mem_alias_set (fp, setjmp_alias_set);
980 set_mem_alias_set (lab, setjmp_alias_set);
981 set_mem_alias_set (stack, setjmp_alias_set);
983 /* Pick up FP, label, and SP from the block and jump. This code is
984 from expand_goto in stmt.c; see there for detailed comments. */
985 #ifdef HAVE_nonlocal_goto
986 if (HAVE_nonlocal_goto)
987 /* We have to pass a value to the nonlocal_goto pattern that will
988 get copied into the static_chain pointer, but it does not matter
989 what that value is, because builtin_setjmp does not use it. */
990 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
994 lab = copy_to_reg (lab);
996 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
997 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
999 emit_move_insn (hard_frame_pointer_rtx, fp);
1000 emit_stack_restore (SAVE_NONLOCAL, stack);
1002 emit_use (hard_frame_pointer_rtx);
1003 emit_use (stack_pointer_rtx);
1004 emit_indirect_jump (lab);
1008 /* Search backwards and mark the jump insn as a non-local goto.
1009 Note that this precludes the use of __builtin_longjmp to a
1010 __builtin_setjmp target in the same function. However, we've
1011 already cautioned the user that these functions are for
1012 internal exception handling use only. */
1013 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1015 gcc_assert (insn != last);
1019 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1022 else if (CALL_P (insn))
1027 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1028 and the address of the save area. */
1031 expand_builtin_nonlocal_goto (tree exp)
1033 tree t_label, t_save_area;
1034 rtx r_label, r_save_area, r_fp, r_sp, insn;
1036 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1039 t_label = CALL_EXPR_ARG (exp, 0);
1040 t_save_area = CALL_EXPR_ARG (exp, 1);
1042 r_label = expand_normal (t_label);
1043 r_label = convert_memory_address (Pmode, r_label);
1044 r_save_area = expand_normal (t_save_area);
1045 r_save_area = convert_memory_address (Pmode, r_save_area);
1046 /* Copy the address of the save location to a register just in case it was
1047 based on the frame pointer. */
1048 r_save_area = copy_to_reg (r_save_area);
1049 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1050 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1051 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
1053 crtl->has_nonlocal_goto = 1;
1055 #ifdef HAVE_nonlocal_goto
1056 /* ??? We no longer need to pass the static chain value, afaik. */
1057 if (HAVE_nonlocal_goto)
1058 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1062 r_label = copy_to_reg (r_label);
1064 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1065 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1067 /* Restore frame pointer for containing function. */
1068 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1069 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1071 /* USE of hard_frame_pointer_rtx added for consistency;
1072 not clear if really needed. */
1073 emit_use (hard_frame_pointer_rtx);
1074 emit_use (stack_pointer_rtx);
1076 /* If the architecture is using a GP register, we must
1077 conservatively assume that the target function makes use of it.
1078 The prologue of functions with nonlocal gotos must therefore
1079 initialize the GP register to the appropriate value, and we
1080 must then make sure that this value is live at the point
1081 of the jump. (Note that this doesn't necessarily apply
1082 to targets with a nonlocal_goto pattern; they are free
1083 to implement it in their own way. Note also that this is
1084 a no-op if the GP register is a global invariant.) */
1085 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1086 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1087 emit_use (pic_offset_table_rtx);
1089 emit_indirect_jump (r_label);
1092 /* Search backwards to the jump insn and mark it as a
1094 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1098 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1101 else if (CALL_P (insn))
1108 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1109 (not all will be used on all machines) that was passed to __builtin_setjmp.
1110 It updates the stack pointer in that block to correspond to the current
1114 expand_builtin_update_setjmp_buf (rtx buf_addr)
1116 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1118 = gen_rtx_MEM (sa_mode,
1121 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1123 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1126 /* Expand a call to __builtin_prefetch. For a target that does not support
1127 data prefetch, evaluate the memory address argument in case it has side
1131 expand_builtin_prefetch (tree exp)
1133 tree arg0, arg1, arg2;
1137 if (!validate_arglist (exp, POINTER_TYPE, 0))
1140 arg0 = CALL_EXPR_ARG (exp, 0);
1142 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1143 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1145 nargs = call_expr_nargs (exp);
1147 arg1 = CALL_EXPR_ARG (exp, 1);
1149 arg1 = integer_zero_node;
1151 arg2 = CALL_EXPR_ARG (exp, 2);
1153 arg2 = integer_three_node;
1155 /* Argument 0 is an address. */
1156 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1158 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1159 if (TREE_CODE (arg1) != INTEGER_CST)
1161 error ("second argument to %<__builtin_prefetch%> must be a constant");
1162 arg1 = integer_zero_node;
1164 op1 = expand_normal (arg1);
1165 /* Argument 1 must be either zero or one. */
1166 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1168 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1173 /* Argument 2 (locality) must be a compile-time constant int. */
1174 if (TREE_CODE (arg2) != INTEGER_CST)
1176 error ("third argument to %<__builtin_prefetch%> must be a constant");
1177 arg2 = integer_zero_node;
1179 op2 = expand_normal (arg2);
1180 /* Argument 2 must be 0, 1, 2, or 3. */
1181 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1183 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1187 #ifdef HAVE_prefetch
1190 struct expand_operand ops[3];
1192 create_address_operand (&ops[0], op0);
1193 create_integer_operand (&ops[1], INTVAL (op1));
1194 create_integer_operand (&ops[2], INTVAL (op2));
1195 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1200 /* Don't do anything with direct references to volatile memory, but
1201 generate code to handle other side effects. */
1202 if (!MEM_P (op0) && side_effects_p (op0))
1206 /* Get a MEM rtx for expression EXP which is the address of an operand
1207 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1208 the maximum length of the block of memory that might be accessed or
1212 get_memory_rtx (tree exp, tree len)
1214 tree orig_exp = exp;
1218 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1219 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1220 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1221 exp = TREE_OPERAND (exp, 0);
1223 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1224 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1226 /* Get an expression we can use to find the attributes to assign to MEM.
1227 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1228 we can. First remove any nops. */
1229 while (CONVERT_EXPR_P (exp)
1230 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1231 exp = TREE_OPERAND (exp, 0);
1234 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1235 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1236 && host_integerp (TREE_OPERAND (exp, 1), 0)
1237 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1238 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1239 else if (TREE_CODE (exp) == ADDR_EXPR)
1240 exp = TREE_OPERAND (exp, 0);
1241 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1242 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1246 /* Honor attributes derived from exp, except for the alias set
1247 (as builtin stringops may alias with anything) and the size
1248 (as stringops may access multiple array elements). */
1251 set_mem_attributes (mem, exp, 0);
1254 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1256 /* Allow the string and memory builtins to overflow from one
1257 field into another, see http://gcc.gnu.org/PR23561.
1258 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1259 memory accessed by the string or memory builtin will fit
1260 within the field. */
1261 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1263 tree mem_expr = MEM_EXPR (mem);
1264 HOST_WIDE_INT offset = -1, length = -1;
1267 while (TREE_CODE (inner) == ARRAY_REF
1268 || CONVERT_EXPR_P (inner)
1269 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1270 || TREE_CODE (inner) == SAVE_EXPR)
1271 inner = TREE_OPERAND (inner, 0);
1273 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1275 if (MEM_OFFSET_KNOWN_P (mem))
1276 offset = MEM_OFFSET (mem);
1278 if (offset >= 0 && len && host_integerp (len, 0))
1279 length = tree_low_cst (len, 0);
1281 while (TREE_CODE (inner) == COMPONENT_REF)
1283 tree field = TREE_OPERAND (inner, 1);
1284 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1285 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1287 /* Bitfields are generally not byte-addressable. */
1288 gcc_assert (!DECL_BIT_FIELD (field)
1289 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1290 % BITS_PER_UNIT) == 0
1291 && host_integerp (DECL_SIZE (field), 0)
1292 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1293 % BITS_PER_UNIT) == 0));
1295 /* If we can prove that the memory starting at XEXP (mem, 0) and
1296 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1297 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1298 fields without DECL_SIZE_UNIT like flexible array members. */
1300 && DECL_SIZE_UNIT (field)
1301 && host_integerp (DECL_SIZE_UNIT (field), 0))
1304 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1307 && offset + length <= size)
1312 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1313 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1314 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1322 mem_expr = TREE_OPERAND (mem_expr, 0);
1323 inner = TREE_OPERAND (inner, 0);
1326 if (mem_expr == NULL)
1328 if (mem_expr != MEM_EXPR (mem))
1330 set_mem_expr (mem, mem_expr);
1332 set_mem_offset (mem, offset);
1334 clear_mem_offset (mem);
1337 set_mem_alias_set (mem, 0);
1338 clear_mem_size (mem);
1344 /* Built-in functions to perform an untyped call and return. */
1346 #define apply_args_mode \
1347 (this_target_builtins->x_apply_args_mode)
1348 #define apply_result_mode \
1349 (this_target_builtins->x_apply_result_mode)
1351 /* Return the size required for the block returned by __builtin_apply_args,
1352 and initialize apply_args_mode. */
1355 apply_args_size (void)
1357 static int size = -1;
1360 enum machine_mode mode;
1362 /* The values computed by this function never change. */
1365 /* The first value is the incoming arg-pointer. */
1366 size = GET_MODE_SIZE (Pmode);
1368 /* The second value is the structure value address unless this is
1369 passed as an "invisible" first argument. */
1370 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1371 size += GET_MODE_SIZE (Pmode);
1373 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1374 if (FUNCTION_ARG_REGNO_P (regno))
1376 mode = targetm.calls.get_raw_arg_mode (regno);
1378 gcc_assert (mode != VOIDmode);
1380 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1381 if (size % align != 0)
1382 size = CEIL (size, align) * align;
1383 size += GET_MODE_SIZE (mode);
1384 apply_args_mode[regno] = mode;
1388 apply_args_mode[regno] = VOIDmode;
1394 /* Return the size required for the block returned by __builtin_apply,
1395 and initialize apply_result_mode. */
1398 apply_result_size (void)
1400 static int size = -1;
1402 enum machine_mode mode;
1404 /* The values computed by this function never change. */
1409 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1410 if (targetm.calls.function_value_regno_p (regno))
1412 mode = targetm.calls.get_raw_result_mode (regno);
1414 gcc_assert (mode != VOIDmode);
1416 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1417 if (size % align != 0)
1418 size = CEIL (size, align) * align;
1419 size += GET_MODE_SIZE (mode);
1420 apply_result_mode[regno] = mode;
1423 apply_result_mode[regno] = VOIDmode;
1425 /* Allow targets that use untyped_call and untyped_return to override
1426 the size so that machine-specific information can be stored here. */
1427 #ifdef APPLY_RESULT_SIZE
1428 size = APPLY_RESULT_SIZE;
1434 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1435 /* Create a vector describing the result block RESULT. If SAVEP is true,
1436 the result block is used to save the values; otherwise it is used to
1437 restore the values. */
1440 result_vector (int savep, rtx result)
1442 int regno, size, align, nelts;
1443 enum machine_mode mode;
1445 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1448 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1449 if ((mode = apply_result_mode[regno]) != VOIDmode)
1451 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1452 if (size % align != 0)
1453 size = CEIL (size, align) * align;
1454 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1455 mem = adjust_address (result, mode, size);
1456 savevec[nelts++] = (savep
1457 ? gen_rtx_SET (VOIDmode, mem, reg)
1458 : gen_rtx_SET (VOIDmode, reg, mem));
1459 size += GET_MODE_SIZE (mode);
1461 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1463 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1465 /* Save the state required to perform an untyped call with the same
1466 arguments as were passed to the current function. */
1469 expand_builtin_apply_args_1 (void)
1472 int size, align, regno;
1473 enum machine_mode mode;
1474 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1476 /* Create a block where the arg-pointer, structure value address,
1477 and argument registers can be saved. */
1478 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1480 /* Walk past the arg-pointer and structure value address. */
1481 size = GET_MODE_SIZE (Pmode);
1482 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1483 size += GET_MODE_SIZE (Pmode);
1485 /* Save each register used in calling a function to the block. */
1486 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1487 if ((mode = apply_args_mode[regno]) != VOIDmode)
1489 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1490 if (size % align != 0)
1491 size = CEIL (size, align) * align;
1493 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1495 emit_move_insn (adjust_address (registers, mode, size), tem);
1496 size += GET_MODE_SIZE (mode);
1499 /* Save the arg pointer to the block. */
1500 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1501 #ifdef STACK_GROWS_DOWNWARD
1502 /* We need the pointer as the caller actually passed them to us, not
1503 as we might have pretended they were passed. Make sure it's a valid
1504 operand, as emit_move_insn isn't expected to handle a PLUS. */
1506 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1509 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1511 size = GET_MODE_SIZE (Pmode);
1513 /* Save the structure value address unless this is passed as an
1514 "invisible" first argument. */
1515 if (struct_incoming_value)
1517 emit_move_insn (adjust_address (registers, Pmode, size),
1518 copy_to_reg (struct_incoming_value));
1519 size += GET_MODE_SIZE (Pmode);
1522 /* Return the address of the block. */
1523 return copy_addr_to_reg (XEXP (registers, 0));
1526 /* __builtin_apply_args returns block of memory allocated on
1527 the stack into which is stored the arg pointer, structure
1528 value address, static chain, and all the registers that might
1529 possibly be used in performing a function call. The code is
1530 moved to the start of the function so the incoming values are
1534 expand_builtin_apply_args (void)
1536 /* Don't do __builtin_apply_args more than once in a function.
1537 Save the result of the first call and reuse it. */
1538 if (apply_args_value != 0)
1539 return apply_args_value;
1541 /* When this function is called, it means that registers must be
1542 saved on entry to this function. So we migrate the
1543 call to the first insn of this function. */
1548 temp = expand_builtin_apply_args_1 ();
1552 apply_args_value = temp;
1554 /* Put the insns after the NOTE that starts the function.
1555 If this is inside a start_sequence, make the outer-level insn
1556 chain current, so the code is placed at the start of the
1557 function. If internal_arg_pointer is a non-virtual pseudo,
1558 it needs to be placed after the function that initializes
1560 push_topmost_sequence ();
1561 if (REG_P (crtl->args.internal_arg_pointer)
1562 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1563 emit_insn_before (seq, parm_birth_insn);
1565 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1566 pop_topmost_sequence ();
1571 /* Perform an untyped call and save the state required to perform an
1572 untyped return of whatever value was returned by the given function. */
1575 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1577 int size, align, regno;
1578 enum machine_mode mode;
1579 rtx incoming_args, result, reg, dest, src, call_insn;
1580 rtx old_stack_level = 0;
1581 rtx call_fusage = 0;
1582 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1584 arguments = convert_memory_address (Pmode, arguments);
1586 /* Create a block where the return registers can be saved. */
1587 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1589 /* Fetch the arg pointer from the ARGUMENTS block. */
1590 incoming_args = gen_reg_rtx (Pmode);
1591 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1592 #ifndef STACK_GROWS_DOWNWARD
1593 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1594 incoming_args, 0, OPTAB_LIB_WIDEN);
1597 /* Push a new argument block and copy the arguments. Do not allow
1598 the (potential) memcpy call below to interfere with our stack
1600 do_pending_stack_adjust ();
1603 /* Save the stack with nonlocal if available. */
1604 #ifdef HAVE_save_stack_nonlocal
1605 if (HAVE_save_stack_nonlocal)
1606 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1609 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1611 /* Allocate a block of memory onto the stack and copy the memory
1612 arguments to the outgoing arguments address. We can pass TRUE
1613 as the 4th argument because we just saved the stack pointer
1614 and will restore it right after the call. */
1615 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1617 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1618 may have already set current_function_calls_alloca to true.
1619 current_function_calls_alloca won't be set if argsize is zero,
1620 so we have to guarantee need_drap is true here. */
1621 if (SUPPORTS_STACK_ALIGNMENT)
1622 crtl->need_drap = true;
1624 dest = virtual_outgoing_args_rtx;
1625 #ifndef STACK_GROWS_DOWNWARD
1626 if (CONST_INT_P (argsize))
1627 dest = plus_constant (dest, -INTVAL (argsize));
1629 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1631 dest = gen_rtx_MEM (BLKmode, dest);
1632 set_mem_align (dest, PARM_BOUNDARY);
1633 src = gen_rtx_MEM (BLKmode, incoming_args);
1634 set_mem_align (src, PARM_BOUNDARY);
1635 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1637 /* Refer to the argument block. */
1639 arguments = gen_rtx_MEM (BLKmode, arguments);
1640 set_mem_align (arguments, PARM_BOUNDARY);
1642 /* Walk past the arg-pointer and structure value address. */
1643 size = GET_MODE_SIZE (Pmode);
1645 size += GET_MODE_SIZE (Pmode);
1647 /* Restore each of the registers previously saved. Make USE insns
1648 for each of these registers for use in making the call. */
1649 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1650 if ((mode = apply_args_mode[regno]) != VOIDmode)
1652 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1653 if (size % align != 0)
1654 size = CEIL (size, align) * align;
1655 reg = gen_rtx_REG (mode, regno);
1656 emit_move_insn (reg, adjust_address (arguments, mode, size));
1657 use_reg (&call_fusage, reg);
1658 size += GET_MODE_SIZE (mode);
1661 /* Restore the structure value address unless this is passed as an
1662 "invisible" first argument. */
1663 size = GET_MODE_SIZE (Pmode);
1666 rtx value = gen_reg_rtx (Pmode);
1667 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1668 emit_move_insn (struct_value, value);
1669 if (REG_P (struct_value))
1670 use_reg (&call_fusage, struct_value);
1671 size += GET_MODE_SIZE (Pmode);
1674 /* All arguments and registers used for the call are set up by now! */
1675 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1677 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1678 and we don't want to load it into a register as an optimization,
1679 because prepare_call_address already did it if it should be done. */
1680 if (GET_CODE (function) != SYMBOL_REF)
1681 function = memory_address (FUNCTION_MODE, function);
1683 /* Generate the actual call instruction and save the return value. */
1684 #ifdef HAVE_untyped_call
1685 if (HAVE_untyped_call)
1686 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1687 result, result_vector (1, result)));
1690 #ifdef HAVE_call_value
1691 if (HAVE_call_value)
1695 /* Locate the unique return register. It is not possible to
1696 express a call that sets more than one return register using
1697 call_value; use untyped_call for that. In fact, untyped_call
1698 only needs to save the return registers in the given block. */
1699 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1700 if ((mode = apply_result_mode[regno]) != VOIDmode)
1702 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1704 valreg = gen_rtx_REG (mode, regno);
1707 emit_call_insn (GEN_CALL_VALUE (valreg,
1708 gen_rtx_MEM (FUNCTION_MODE, function),
1709 const0_rtx, NULL_RTX, const0_rtx));
1711 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1717 /* Find the CALL insn we just emitted, and attach the register usage
1719 call_insn = last_call_insn ();
1720 add_function_usage_to (call_insn, call_fusage);
1722 /* Restore the stack. */
1723 #ifdef HAVE_save_stack_nonlocal
1724 if (HAVE_save_stack_nonlocal)
1725 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1728 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1729 fixup_args_size_notes (call_insn, get_last_insn(), 0);
1733 /* Return the address of the result block. */
1734 result = copy_addr_to_reg (XEXP (result, 0));
1735 return convert_memory_address (ptr_mode, result);
1738 /* Perform an untyped return. */
1741 expand_builtin_return (rtx result)
1743 int size, align, regno;
1744 enum machine_mode mode;
1746 rtx call_fusage = 0;
1748 result = convert_memory_address (Pmode, result);
1750 apply_result_size ();
1751 result = gen_rtx_MEM (BLKmode, result);
1753 #ifdef HAVE_untyped_return
1754 if (HAVE_untyped_return)
1756 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1762 /* Restore the return value and note that each value is used. */
1764 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1765 if ((mode = apply_result_mode[regno]) != VOIDmode)
1767 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1768 if (size % align != 0)
1769 size = CEIL (size, align) * align;
1770 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1771 emit_move_insn (reg, adjust_address (result, mode, size));
1773 push_to_sequence (call_fusage);
1775 call_fusage = get_insns ();
1777 size += GET_MODE_SIZE (mode);
1780 /* Put the USE insns before the return. */
1781 emit_insn (call_fusage);
1783 /* Return whatever values was restored by jumping directly to the end
1785 expand_naked_return ();
1788 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1790 static enum type_class
1791 type_to_class (tree type)
1793 switch (TREE_CODE (type))
1795 case VOID_TYPE: return void_type_class;
1796 case INTEGER_TYPE: return integer_type_class;
1797 case ENUMERAL_TYPE: return enumeral_type_class;
1798 case BOOLEAN_TYPE: return boolean_type_class;
1799 case POINTER_TYPE: return pointer_type_class;
1800 case REFERENCE_TYPE: return reference_type_class;
1801 case OFFSET_TYPE: return offset_type_class;
1802 case REAL_TYPE: return real_type_class;
1803 case COMPLEX_TYPE: return complex_type_class;
1804 case FUNCTION_TYPE: return function_type_class;
1805 case METHOD_TYPE: return method_type_class;
1806 case RECORD_TYPE: return record_type_class;
1808 case QUAL_UNION_TYPE: return union_type_class;
1809 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1810 ? string_type_class : array_type_class);
1811 case LANG_TYPE: return lang_type_class;
1812 default: return no_type_class;
1816 /* Expand a call EXP to __builtin_classify_type. */
1819 expand_builtin_classify_type (tree exp)
1821 if (call_expr_nargs (exp))
1822 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1823 return GEN_INT (no_type_class);
1826 /* This helper macro, meant to be used in mathfn_built_in below,
1827 determines which among a set of three builtin math functions is
1828 appropriate for a given type mode. The `F' and `L' cases are
1829 automatically generated from the `double' case. */
1830 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1831 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1832 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1833 fcodel = BUILT_IN_MATHFN##L ; break;
1834 /* Similar to above, but appends _R after any F/L suffix. */
1835 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1836 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1837 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1838 fcodel = BUILT_IN_MATHFN##L_R ; break;
1840 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1841 if available. If IMPLICIT is true use the implicit builtin declaration,
1842 otherwise use the explicit declaration. If we can't do the conversion,
1846 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1848 enum built_in_function fcode, fcodef, fcodel, fcode2;
1852 CASE_MATHFN (BUILT_IN_ACOS)
1853 CASE_MATHFN (BUILT_IN_ACOSH)
1854 CASE_MATHFN (BUILT_IN_ASIN)
1855 CASE_MATHFN (BUILT_IN_ASINH)
1856 CASE_MATHFN (BUILT_IN_ATAN)
1857 CASE_MATHFN (BUILT_IN_ATAN2)
1858 CASE_MATHFN (BUILT_IN_ATANH)
1859 CASE_MATHFN (BUILT_IN_CBRT)
1860 CASE_MATHFN (BUILT_IN_CEIL)
1861 CASE_MATHFN (BUILT_IN_CEXPI)
1862 CASE_MATHFN (BUILT_IN_COPYSIGN)
1863 CASE_MATHFN (BUILT_IN_COS)
1864 CASE_MATHFN (BUILT_IN_COSH)
1865 CASE_MATHFN (BUILT_IN_DREM)
1866 CASE_MATHFN (BUILT_IN_ERF)
1867 CASE_MATHFN (BUILT_IN_ERFC)
1868 CASE_MATHFN (BUILT_IN_EXP)
1869 CASE_MATHFN (BUILT_IN_EXP10)
1870 CASE_MATHFN (BUILT_IN_EXP2)
1871 CASE_MATHFN (BUILT_IN_EXPM1)
1872 CASE_MATHFN (BUILT_IN_FABS)
1873 CASE_MATHFN (BUILT_IN_FDIM)
1874 CASE_MATHFN (BUILT_IN_FLOOR)
1875 CASE_MATHFN (BUILT_IN_FMA)
1876 CASE_MATHFN (BUILT_IN_FMAX)
1877 CASE_MATHFN (BUILT_IN_FMIN)
1878 CASE_MATHFN (BUILT_IN_FMOD)
1879 CASE_MATHFN (BUILT_IN_FREXP)
1880 CASE_MATHFN (BUILT_IN_GAMMA)
1881 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1882 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1883 CASE_MATHFN (BUILT_IN_HYPOT)
1884 CASE_MATHFN (BUILT_IN_ILOGB)
1885 CASE_MATHFN (BUILT_IN_ICEIL)
1886 CASE_MATHFN (BUILT_IN_IFLOOR)
1887 CASE_MATHFN (BUILT_IN_INF)
1888 CASE_MATHFN (BUILT_IN_IRINT)
1889 CASE_MATHFN (BUILT_IN_IROUND)
1890 CASE_MATHFN (BUILT_IN_ISINF)
1891 CASE_MATHFN (BUILT_IN_J0)
1892 CASE_MATHFN (BUILT_IN_J1)
1893 CASE_MATHFN (BUILT_IN_JN)
1894 CASE_MATHFN (BUILT_IN_LCEIL)
1895 CASE_MATHFN (BUILT_IN_LDEXP)
1896 CASE_MATHFN (BUILT_IN_LFLOOR)
1897 CASE_MATHFN (BUILT_IN_LGAMMA)
1898 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1899 CASE_MATHFN (BUILT_IN_LLCEIL)
1900 CASE_MATHFN (BUILT_IN_LLFLOOR)
1901 CASE_MATHFN (BUILT_IN_LLRINT)
1902 CASE_MATHFN (BUILT_IN_LLROUND)
1903 CASE_MATHFN (BUILT_IN_LOG)
1904 CASE_MATHFN (BUILT_IN_LOG10)
1905 CASE_MATHFN (BUILT_IN_LOG1P)
1906 CASE_MATHFN (BUILT_IN_LOG2)
1907 CASE_MATHFN (BUILT_IN_LOGB)
1908 CASE_MATHFN (BUILT_IN_LRINT)
1909 CASE_MATHFN (BUILT_IN_LROUND)
1910 CASE_MATHFN (BUILT_IN_MODF)
1911 CASE_MATHFN (BUILT_IN_NAN)
1912 CASE_MATHFN (BUILT_IN_NANS)
1913 CASE_MATHFN (BUILT_IN_NEARBYINT)
1914 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1915 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1916 CASE_MATHFN (BUILT_IN_POW)
1917 CASE_MATHFN (BUILT_IN_POWI)
1918 CASE_MATHFN (BUILT_IN_POW10)
1919 CASE_MATHFN (BUILT_IN_REMAINDER)
1920 CASE_MATHFN (BUILT_IN_REMQUO)
1921 CASE_MATHFN (BUILT_IN_RINT)
1922 CASE_MATHFN (BUILT_IN_ROUND)
1923 CASE_MATHFN (BUILT_IN_SCALB)
1924 CASE_MATHFN (BUILT_IN_SCALBLN)
1925 CASE_MATHFN (BUILT_IN_SCALBN)
1926 CASE_MATHFN (BUILT_IN_SIGNBIT)
1927 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1928 CASE_MATHFN (BUILT_IN_SIN)
1929 CASE_MATHFN (BUILT_IN_SINCOS)
1930 CASE_MATHFN (BUILT_IN_SINH)
1931 CASE_MATHFN (BUILT_IN_SQRT)
1932 CASE_MATHFN (BUILT_IN_TAN)
1933 CASE_MATHFN (BUILT_IN_TANH)
1934 CASE_MATHFN (BUILT_IN_TGAMMA)
1935 CASE_MATHFN (BUILT_IN_TRUNC)
1936 CASE_MATHFN (BUILT_IN_Y0)
1937 CASE_MATHFN (BUILT_IN_Y1)
1938 CASE_MATHFN (BUILT_IN_YN)
1944 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1946 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1948 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1953 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1956 return builtin_decl_explicit (fcode2);
1959 /* Like mathfn_built_in_1(), but always use the implicit array. */
1962 mathfn_built_in (tree type, enum built_in_function fn)
1964 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1967 /* If errno must be maintained, expand the RTL to check if the result,
1968 TARGET, of a built-in function call, EXP, is NaN, and if so set
1972 expand_errno_check (tree exp, rtx target)
1974 rtx lab = gen_label_rtx ();
1976 /* Test the result; if it is NaN, set errno=EDOM because
1977 the argument was not in the domain. */
1978 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1979 NULL_RTX, NULL_RTX, lab,
1980 /* The jump is very likely. */
1981 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1984 /* If this built-in doesn't throw an exception, set errno directly. */
1985 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1987 #ifdef GEN_ERRNO_RTX
1988 rtx errno_rtx = GEN_ERRNO_RTX;
1991 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1993 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1999 /* Make sure the library call isn't expanded as a tail call. */
2000 CALL_EXPR_TAILCALL (exp) = 0;
2002 /* We can't set errno=EDOM directly; let the library call do it.
2003 Pop the arguments right away in case the call gets deleted. */
2005 expand_call (exp, target, 0);
2010 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2011 Return NULL_RTX if a normal call should be emitted rather than expanding
2012 the function in-line. EXP is the expression that is a call to the builtin
2013 function; if convenient, the result should be placed in TARGET.
2014 SUBTARGET may be used as the target for computing one of EXP's operands. */
2017 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
2019 optab builtin_optab;
2021 tree fndecl = get_callee_fndecl (exp);
2022 enum machine_mode mode;
2023 bool errno_set = false;
2026 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2029 arg = CALL_EXPR_ARG (exp, 0);
2031 switch (DECL_FUNCTION_CODE (fndecl))
2033 CASE_FLT_FN (BUILT_IN_SQRT):
2034 errno_set = ! tree_expr_nonnegative_p (arg);
2035 builtin_optab = sqrt_optab;
2037 CASE_FLT_FN (BUILT_IN_EXP):
2038 errno_set = true; builtin_optab = exp_optab; break;
2039 CASE_FLT_FN (BUILT_IN_EXP10):
2040 CASE_FLT_FN (BUILT_IN_POW10):
2041 errno_set = true; builtin_optab = exp10_optab; break;
2042 CASE_FLT_FN (BUILT_IN_EXP2):
2043 errno_set = true; builtin_optab = exp2_optab; break;
2044 CASE_FLT_FN (BUILT_IN_EXPM1):
2045 errno_set = true; builtin_optab = expm1_optab; break;
2046 CASE_FLT_FN (BUILT_IN_LOGB):
2047 errno_set = true; builtin_optab = logb_optab; break;
2048 CASE_FLT_FN (BUILT_IN_LOG):
2049 errno_set = true; builtin_optab = log_optab; break;
2050 CASE_FLT_FN (BUILT_IN_LOG10):
2051 errno_set = true; builtin_optab = log10_optab; break;
2052 CASE_FLT_FN (BUILT_IN_LOG2):
2053 errno_set = true; builtin_optab = log2_optab; break;
2054 CASE_FLT_FN (BUILT_IN_LOG1P):
2055 errno_set = true; builtin_optab = log1p_optab; break;
2056 CASE_FLT_FN (BUILT_IN_ASIN):
2057 builtin_optab = asin_optab; break;
2058 CASE_FLT_FN (BUILT_IN_ACOS):
2059 builtin_optab = acos_optab; break;
2060 CASE_FLT_FN (BUILT_IN_TAN):
2061 builtin_optab = tan_optab; break;
2062 CASE_FLT_FN (BUILT_IN_ATAN):
2063 builtin_optab = atan_optab; break;
2064 CASE_FLT_FN (BUILT_IN_FLOOR):
2065 builtin_optab = floor_optab; break;
2066 CASE_FLT_FN (BUILT_IN_CEIL):
2067 builtin_optab = ceil_optab; break;
2068 CASE_FLT_FN (BUILT_IN_TRUNC):
2069 builtin_optab = btrunc_optab; break;
2070 CASE_FLT_FN (BUILT_IN_ROUND):
2071 builtin_optab = round_optab; break;
2072 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2073 builtin_optab = nearbyint_optab;
2074 if (flag_trapping_math)
2076 /* Else fallthrough and expand as rint. */
2077 CASE_FLT_FN (BUILT_IN_RINT):
2078 builtin_optab = rint_optab; break;
2079 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2080 builtin_optab = significand_optab; break;
2085 /* Make a suitable register to place result in. */
2086 mode = TYPE_MODE (TREE_TYPE (exp));
2088 if (! flag_errno_math || ! HONOR_NANS (mode))
2091 /* Before working hard, check whether the instruction is available. */
2092 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2093 && (!errno_set || !optimize_insn_for_size_p ()))
2095 target = gen_reg_rtx (mode);
2097 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2098 need to expand the argument again. This way, we will not perform
2099 side-effects more the once. */
2100 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2102 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2106 /* Compute into TARGET.
2107 Set TARGET to wherever the result comes back. */
2108 target = expand_unop (mode, builtin_optab, op0, target, 0);
2113 expand_errno_check (exp, target);
2115 /* Output the entire sequence. */
2116 insns = get_insns ();
2122 /* If we were unable to expand via the builtin, stop the sequence
2123 (without outputting the insns) and call to the library function
2124 with the stabilized argument list. */
2128 return expand_call (exp, target, target == const0_rtx);
2131 /* Expand a call to the builtin binary math functions (pow and atan2).
2132 Return NULL_RTX if a normal call should be emitted rather than expanding the
2133 function in-line. EXP is the expression that is a call to the builtin
2134 function; if convenient, the result should be placed in TARGET.
2135 SUBTARGET may be used as the target for computing one of EXP's
2139 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2141 optab builtin_optab;
2142 rtx op0, op1, insns;
2143 int op1_type = REAL_TYPE;
2144 tree fndecl = get_callee_fndecl (exp);
2146 enum machine_mode mode;
2147 bool errno_set = true;
2149 switch (DECL_FUNCTION_CODE (fndecl))
2151 CASE_FLT_FN (BUILT_IN_SCALBN):
2152 CASE_FLT_FN (BUILT_IN_SCALBLN):
2153 CASE_FLT_FN (BUILT_IN_LDEXP):
2154 op1_type = INTEGER_TYPE;
2159 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2162 arg0 = CALL_EXPR_ARG (exp, 0);
2163 arg1 = CALL_EXPR_ARG (exp, 1);
2165 switch (DECL_FUNCTION_CODE (fndecl))
2167 CASE_FLT_FN (BUILT_IN_POW):
2168 builtin_optab = pow_optab; break;
2169 CASE_FLT_FN (BUILT_IN_ATAN2):
2170 builtin_optab = atan2_optab; break;
2171 CASE_FLT_FN (BUILT_IN_SCALB):
2172 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2174 builtin_optab = scalb_optab; break;
2175 CASE_FLT_FN (BUILT_IN_SCALBN):
2176 CASE_FLT_FN (BUILT_IN_SCALBLN):
2177 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2179 /* Fall through... */
2180 CASE_FLT_FN (BUILT_IN_LDEXP):
2181 builtin_optab = ldexp_optab; break;
2182 CASE_FLT_FN (BUILT_IN_FMOD):
2183 builtin_optab = fmod_optab; break;
2184 CASE_FLT_FN (BUILT_IN_REMAINDER):
2185 CASE_FLT_FN (BUILT_IN_DREM):
2186 builtin_optab = remainder_optab; break;
2191 /* Make a suitable register to place result in. */
2192 mode = TYPE_MODE (TREE_TYPE (exp));
2194 /* Before working hard, check whether the instruction is available. */
2195 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2198 target = gen_reg_rtx (mode);
2200 if (! flag_errno_math || ! HONOR_NANS (mode))
2203 if (errno_set && optimize_insn_for_size_p ())
2206 /* Always stabilize the argument list. */
2207 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2208 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2210 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2211 op1 = expand_normal (arg1);
2215 /* Compute into TARGET.
2216 Set TARGET to wherever the result comes back. */
2217 target = expand_binop (mode, builtin_optab, op0, op1,
2218 target, 0, OPTAB_DIRECT);
2220 /* If we were unable to expand via the builtin, stop the sequence
2221 (without outputting the insns) and call to the library function
2222 with the stabilized argument list. */
2226 return expand_call (exp, target, target == const0_rtx);
2230 expand_errno_check (exp, target);
2232 /* Output the entire sequence. */
2233 insns = get_insns ();
2240 /* Expand a call to the builtin trinary math functions (fma).
2241 Return NULL_RTX if a normal call should be emitted rather than expanding the
2242 function in-line. EXP is the expression that is a call to the builtin
2243 function; if convenient, the result should be placed in TARGET.
2244 SUBTARGET may be used as the target for computing one of EXP's
2248 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2250 optab builtin_optab;
2251 rtx op0, op1, op2, insns;
2252 tree fndecl = get_callee_fndecl (exp);
2253 tree arg0, arg1, arg2;
2254 enum machine_mode mode;
2256 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2259 arg0 = CALL_EXPR_ARG (exp, 0);
2260 arg1 = CALL_EXPR_ARG (exp, 1);
2261 arg2 = CALL_EXPR_ARG (exp, 2);
2263 switch (DECL_FUNCTION_CODE (fndecl))
2265 CASE_FLT_FN (BUILT_IN_FMA):
2266 builtin_optab = fma_optab; break;
2271 /* Make a suitable register to place result in. */
2272 mode = TYPE_MODE (TREE_TYPE (exp));
2274 /* Before working hard, check whether the instruction is available. */
2275 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2278 target = gen_reg_rtx (mode);
2280 /* Always stabilize the argument list. */
2281 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2282 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2283 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2285 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2286 op1 = expand_normal (arg1);
2287 op2 = expand_normal (arg2);
2291 /* Compute into TARGET.
2292 Set TARGET to wherever the result comes back. */
2293 target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2296 /* If we were unable to expand via the builtin, stop the sequence
2297 (without outputting the insns) and call to the library function
2298 with the stabilized argument list. */
2302 return expand_call (exp, target, target == const0_rtx);
2305 /* Output the entire sequence. */
2306 insns = get_insns ();
2313 /* Expand a call to the builtin sin and cos math functions.
2314 Return NULL_RTX if a normal call should be emitted rather than expanding the
2315 function in-line. EXP is the expression that is a call to the builtin
2316 function; if convenient, the result should be placed in TARGET.
2317 SUBTARGET may be used as the target for computing one of EXP's
2321 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2323 optab builtin_optab;
2325 tree fndecl = get_callee_fndecl (exp);
2326 enum machine_mode mode;
2329 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2332 arg = CALL_EXPR_ARG (exp, 0);
2334 switch (DECL_FUNCTION_CODE (fndecl))
2336 CASE_FLT_FN (BUILT_IN_SIN):
2337 CASE_FLT_FN (BUILT_IN_COS):
2338 builtin_optab = sincos_optab; break;
2343 /* Make a suitable register to place result in. */
2344 mode = TYPE_MODE (TREE_TYPE (exp));
2346 /* Check if sincos insn is available, otherwise fallback
2347 to sin or cos insn. */
2348 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2349 switch (DECL_FUNCTION_CODE (fndecl))
2351 CASE_FLT_FN (BUILT_IN_SIN):
2352 builtin_optab = sin_optab; break;
2353 CASE_FLT_FN (BUILT_IN_COS):
2354 builtin_optab = cos_optab; break;
2359 /* Before working hard, check whether the instruction is available. */
2360 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2362 target = gen_reg_rtx (mode);
2364 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2365 need to expand the argument again. This way, we will not perform
2366 side-effects more the once. */
2367 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2369 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2373 /* Compute into TARGET.
2374 Set TARGET to wherever the result comes back. */
2375 if (builtin_optab == sincos_optab)
2379 switch (DECL_FUNCTION_CODE (fndecl))
2381 CASE_FLT_FN (BUILT_IN_SIN):
2382 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2384 CASE_FLT_FN (BUILT_IN_COS):
2385 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2390 gcc_assert (result);
2394 target = expand_unop (mode, builtin_optab, op0, target, 0);
2399 /* Output the entire sequence. */
2400 insns = get_insns ();
2406 /* If we were unable to expand via the builtin, stop the sequence
2407 (without outputting the insns) and call to the library function
2408 with the stabilized argument list. */
2412 target = expand_call (exp, target, target == const0_rtx);
2417 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2418 return an RTL instruction code that implements the functionality.
2419 If that isn't possible or available return CODE_FOR_nothing. */
2421 static enum insn_code
2422 interclass_mathfn_icode (tree arg, tree fndecl)
2424 bool errno_set = false;
2425 optab builtin_optab = 0;
2426 enum machine_mode mode;
2428 switch (DECL_FUNCTION_CODE (fndecl))
2430 CASE_FLT_FN (BUILT_IN_ILOGB):
2431 errno_set = true; builtin_optab = ilogb_optab; break;
2432 CASE_FLT_FN (BUILT_IN_ISINF):
2433 builtin_optab = isinf_optab; break;
2434 case BUILT_IN_ISNORMAL:
2435 case BUILT_IN_ISFINITE:
2436 CASE_FLT_FN (BUILT_IN_FINITE):
2437 case BUILT_IN_FINITED32:
2438 case BUILT_IN_FINITED64:
2439 case BUILT_IN_FINITED128:
2440 case BUILT_IN_ISINFD32:
2441 case BUILT_IN_ISINFD64:
2442 case BUILT_IN_ISINFD128:
2443 /* These builtins have no optabs (yet). */
2449 /* There's no easy way to detect the case we need to set EDOM. */
2450 if (flag_errno_math && errno_set)
2451 return CODE_FOR_nothing;
2453 /* Optab mode depends on the mode of the input argument. */
2454 mode = TYPE_MODE (TREE_TYPE (arg));
2457 return optab_handler (builtin_optab, mode);
2458 return CODE_FOR_nothing;
2461 /* Expand a call to one of the builtin math functions that operate on
2462 floating point argument and output an integer result (ilogb, isinf,
2464 Return 0 if a normal call should be emitted rather than expanding the
2465 function in-line. EXP is the expression that is a call to the builtin
2466 function; if convenient, the result should be placed in TARGET. */
2469 expand_builtin_interclass_mathfn (tree exp, rtx target)
2471 enum insn_code icode = CODE_FOR_nothing;
2473 tree fndecl = get_callee_fndecl (exp);
2474 enum machine_mode mode;
2477 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2480 arg = CALL_EXPR_ARG (exp, 0);
2481 icode = interclass_mathfn_icode (arg, fndecl);
2482 mode = TYPE_MODE (TREE_TYPE (arg));
2484 if (icode != CODE_FOR_nothing)
2486 struct expand_operand ops[1];
2487 rtx last = get_last_insn ();
2488 tree orig_arg = arg;
2490 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2491 need to expand the argument again. This way, we will not perform
2492 side-effects more the once. */
2493 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2495 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2497 if (mode != GET_MODE (op0))
2498 op0 = convert_to_mode (mode, op0, 0);
2500 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2501 if (maybe_legitimize_operands (icode, 0, 1, ops)
2502 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2503 return ops[0].value;
2505 delete_insns_since (last);
2506 CALL_EXPR_ARG (exp, 0) = orig_arg;
2512 /* Expand a call to the builtin sincos math function.
2513 Return NULL_RTX if a normal call should be emitted rather than expanding the
2514 function in-line. EXP is the expression that is a call to the builtin
2518 expand_builtin_sincos (tree exp)
2520 rtx op0, op1, op2, target1, target2;
2521 enum machine_mode mode;
2522 tree arg, sinp, cosp;
2524 location_t loc = EXPR_LOCATION (exp);
2525 tree alias_type, alias_off;
2527 if (!validate_arglist (exp, REAL_TYPE,
2528 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2531 arg = CALL_EXPR_ARG (exp, 0);
2532 sinp = CALL_EXPR_ARG (exp, 1);
2533 cosp = CALL_EXPR_ARG (exp, 2);
2535 /* Make a suitable register to place result in. */
2536 mode = TYPE_MODE (TREE_TYPE (arg));
2538 /* Check if sincos insn is available, otherwise emit the call. */
2539 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2542 target1 = gen_reg_rtx (mode);
2543 target2 = gen_reg_rtx (mode);
2545 op0 = expand_normal (arg);
2546 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2547 alias_off = build_int_cst (alias_type, 0);
2548 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2550 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2553 /* Compute into target1 and target2.
2554 Set TARGET to wherever the result comes back. */
2555 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2556 gcc_assert (result);
2558 /* Move target1 and target2 to the memory locations indicated
2560 emit_move_insn (op1, target1);
2561 emit_move_insn (op2, target2);
2566 /* Expand a call to the internal cexpi builtin to the sincos math function.
2567 EXP is the expression that is a call to the builtin function; if convenient,
2568 the result should be placed in TARGET. */
2571 expand_builtin_cexpi (tree exp, rtx target)
2573 tree fndecl = get_callee_fndecl (exp);
2575 enum machine_mode mode;
2577 location_t loc = EXPR_LOCATION (exp);
2579 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2582 arg = CALL_EXPR_ARG (exp, 0);
2583 type = TREE_TYPE (arg);
2584 mode = TYPE_MODE (TREE_TYPE (arg));
2586 /* Try expanding via a sincos optab, fall back to emitting a libcall
2587 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2588 is only generated from sincos, cexp or if we have either of them. */
2589 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2591 op1 = gen_reg_rtx (mode);
2592 op2 = gen_reg_rtx (mode);
2594 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2596 /* Compute into op1 and op2. */
2597 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2599 else if (TARGET_HAS_SINCOS)
2601 tree call, fn = NULL_TREE;
2605 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2606 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2607 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2608 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2609 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2610 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2614 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2615 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2616 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2617 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2618 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2619 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2621 /* Make sure not to fold the sincos call again. */
2622 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2623 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2624 call, 3, arg, top1, top2));
2628 tree call, fn = NULL_TREE, narg;
2629 tree ctype = build_complex_type (type);
2631 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2632 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2633 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2634 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2635 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2636 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2640 /* If we don't have a decl for cexp create one. This is the
2641 friendliest fallback if the user calls __builtin_cexpi
2642 without full target C99 function support. */
2643 if (fn == NULL_TREE)
2646 const char *name = NULL;
2648 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2650 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2652 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2655 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2656 fn = build_fn_decl (name, fntype);
2659 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2660 build_real (type, dconst0), arg);
2662 /* Make sure not to fold the cexp call again. */
2663 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2664 return expand_expr (build_call_nary (ctype, call, 1, narg),
2665 target, VOIDmode, EXPAND_NORMAL);
2668 /* Now build the proper return type. */
2669 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2670 make_tree (TREE_TYPE (arg), op2),
2671 make_tree (TREE_TYPE (arg), op1)),
2672 target, VOIDmode, EXPAND_NORMAL);
2675 /* Conveniently construct a function call expression. FNDECL names the
2676 function to be called, N is the number of arguments, and the "..."
2677 parameters are the argument expressions. Unlike build_call_exr
2678 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2681 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2684 tree fntype = TREE_TYPE (fndecl);
2685 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2688 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2690 SET_EXPR_LOCATION (fn, loc);
2694 /* Expand a call to one of the builtin rounding functions gcc defines
2695 as an extension (lfloor and lceil). As these are gcc extensions we
2696 do not need to worry about setting errno to EDOM.
2697 If expanding via optab fails, lower expression to (int)(floor(x)).
2698 EXP is the expression that is a call to the builtin function;
2699 if convenient, the result should be placed in TARGET. */
2702 expand_builtin_int_roundingfn (tree exp, rtx target)
2704 convert_optab builtin_optab;
2705 rtx op0, insns, tmp;
2706 tree fndecl = get_callee_fndecl (exp);
2707 enum built_in_function fallback_fn;
2708 tree fallback_fndecl;
2709 enum machine_mode mode;
2712 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2715 arg = CALL_EXPR_ARG (exp, 0);
2717 switch (DECL_FUNCTION_CODE (fndecl))
2719 CASE_FLT_FN (BUILT_IN_ICEIL):
2720 CASE_FLT_FN (BUILT_IN_LCEIL):
2721 CASE_FLT_FN (BUILT_IN_LLCEIL):
2722 builtin_optab = lceil_optab;
2723 fallback_fn = BUILT_IN_CEIL;
2726 CASE_FLT_FN (BUILT_IN_IFLOOR):
2727 CASE_FLT_FN (BUILT_IN_LFLOOR):
2728 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2729 builtin_optab = lfloor_optab;
2730 fallback_fn = BUILT_IN_FLOOR;
2737 /* Make a suitable register to place result in. */
2738 mode = TYPE_MODE (TREE_TYPE (exp));
2740 target = gen_reg_rtx (mode);
2742 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2743 need to expand the argument again. This way, we will not perform
2744 side-effects more the once. */
2745 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2747 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2751 /* Compute into TARGET. */
2752 if (expand_sfix_optab (target, op0, builtin_optab))
2754 /* Output the entire sequence. */
2755 insns = get_insns ();
2761 /* If we were unable to expand via the builtin, stop the sequence
2762 (without outputting the insns). */
2765 /* Fall back to floating point rounding optab. */
2766 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2768 /* For non-C99 targets we may end up without a fallback fndecl here
2769 if the user called __builtin_lfloor directly. In this case emit
2770 a call to the floor/ceil variants nevertheless. This should result
2771 in the best user experience for not full C99 targets. */
2772 if (fallback_fndecl == NULL_TREE)
2775 const char *name = NULL;
2777 switch (DECL_FUNCTION_CODE (fndecl))
2779 case BUILT_IN_ICEIL:
2780 case BUILT_IN_LCEIL:
2781 case BUILT_IN_LLCEIL:
2784 case BUILT_IN_ICEILF:
2785 case BUILT_IN_LCEILF:
2786 case BUILT_IN_LLCEILF:
2789 case BUILT_IN_ICEILL:
2790 case BUILT_IN_LCEILL:
2791 case BUILT_IN_LLCEILL:
2794 case BUILT_IN_IFLOOR:
2795 case BUILT_IN_LFLOOR:
2796 case BUILT_IN_LLFLOOR:
2799 case BUILT_IN_IFLOORF:
2800 case BUILT_IN_LFLOORF:
2801 case BUILT_IN_LLFLOORF:
2804 case BUILT_IN_IFLOORL:
2805 case BUILT_IN_LFLOORL:
2806 case BUILT_IN_LLFLOORL:
2813 fntype = build_function_type_list (TREE_TYPE (arg),
2814 TREE_TYPE (arg), NULL_TREE);
2815 fallback_fndecl = build_fn_decl (name, fntype);
2818 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2820 tmp = expand_normal (exp);
2822 /* Truncate the result of floating point optab to integer
2823 via expand_fix (). */
2824 target = gen_reg_rtx (mode);
2825 expand_fix (target, tmp, 0);
2830 /* Expand a call to one of the builtin math functions doing integer
2832 Return 0 if a normal call should be emitted rather than expanding the
2833 function in-line. EXP is the expression that is a call to the builtin
2834 function; if convenient, the result should be placed in TARGET. */
2837 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2839 convert_optab builtin_optab;
2841 tree fndecl = get_callee_fndecl (exp);
2843 enum machine_mode mode;
2845 /* There's no easy way to detect the case we need to set EDOM. */
2846 if (flag_errno_math)
2849 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2852 arg = CALL_EXPR_ARG (exp, 0);
2854 switch (DECL_FUNCTION_CODE (fndecl))
2856 CASE_FLT_FN (BUILT_IN_IRINT):
2857 CASE_FLT_FN (BUILT_IN_LRINT):
2858 CASE_FLT_FN (BUILT_IN_LLRINT):
2859 builtin_optab = lrint_optab; break;
2861 CASE_FLT_FN (BUILT_IN_IROUND):
2862 CASE_FLT_FN (BUILT_IN_LROUND):
2863 CASE_FLT_FN (BUILT_IN_LLROUND):
2864 builtin_optab = lround_optab; break;
2870 /* Make a suitable register to place result in. */
2871 mode = TYPE_MODE (TREE_TYPE (exp));
2873 target = gen_reg_rtx (mode);
2875 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2876 need to expand the argument again. This way, we will not perform
2877 side-effects more the once. */
2878 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2880 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2884 if (expand_sfix_optab (target, op0, builtin_optab))
2886 /* Output the entire sequence. */
2887 insns = get_insns ();
2893 /* If we were unable to expand via the builtin, stop the sequence
2894 (without outputting the insns) and call to the library function
2895 with the stabilized argument list. */
2898 target = expand_call (exp, target, target == const0_rtx);
2903 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2904 a normal call should be emitted rather than expanding the function
2905 in-line. EXP is the expression that is a call to the builtin
2906 function; if convenient, the result should be placed in TARGET. */
2909 expand_builtin_powi (tree exp, rtx target)
2913 enum machine_mode mode;
2914 enum machine_mode mode2;
2916 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2919 arg0 = CALL_EXPR_ARG (exp, 0);
2920 arg1 = CALL_EXPR_ARG (exp, 1);
2921 mode = TYPE_MODE (TREE_TYPE (exp));
2923 /* Emit a libcall to libgcc. */
2925 /* Mode of the 2nd argument must match that of an int. */
2926 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2928 if (target == NULL_RTX)
2929 target = gen_reg_rtx (mode);
2931 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2932 if (GET_MODE (op0) != mode)
2933 op0 = convert_to_mode (mode, op0, 0);
2934 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2935 if (GET_MODE (op1) != mode2)
2936 op1 = convert_to_mode (mode2, op1, 0);
2938 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2939 target, LCT_CONST, mode, 2,
2940 op0, mode, op1, mode2);
2945 /* Expand expression EXP which is a call to the strlen builtin. Return
2946 NULL_RTX if we failed the caller should emit a normal call, otherwise
2947 try to get the result in TARGET, if convenient. */
2950 expand_builtin_strlen (tree exp, rtx target,
2951 enum machine_mode target_mode)
2953 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2957 struct expand_operand ops[4];
2960 tree src = CALL_EXPR_ARG (exp, 0);
2961 rtx src_reg, before_strlen;
2962 enum machine_mode insn_mode = target_mode;
2963 enum insn_code icode = CODE_FOR_nothing;
2966 /* If the length can be computed at compile-time, return it. */
2967 len = c_strlen (src, 0);
2969 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2971 /* If the length can be computed at compile-time and is constant
2972 integer, but there are side-effects in src, evaluate
2973 src for side-effects, then return len.
2974 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2975 can be optimized into: i++; x = 3; */
2976 len = c_strlen (src, 1);
2977 if (len && TREE_CODE (len) == INTEGER_CST)
2979 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2980 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2983 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2985 /* If SRC is not a pointer type, don't do this operation inline. */
2989 /* Bail out if we can't compute strlen in the right mode. */
2990 while (insn_mode != VOIDmode)
2992 icode = optab_handler (strlen_optab, insn_mode);
2993 if (icode != CODE_FOR_nothing)
2996 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2998 if (insn_mode == VOIDmode)
3001 /* Make a place to hold the source address. We will not expand
3002 the actual source until we are sure that the expansion will
3003 not fail -- there are trees that cannot be expanded twice. */
3004 src_reg = gen_reg_rtx (Pmode);
3006 /* Mark the beginning of the strlen sequence so we can emit the
3007 source operand later. */
3008 before_strlen = get_last_insn ();
3010 create_output_operand (&ops[0], target, insn_mode);
3011 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3012 create_integer_operand (&ops[2], 0);
3013 create_integer_operand (&ops[3], align);
3014 if (!maybe_expand_insn (icode, 4, ops))
3017 /* Now that we are assured of success, expand the source. */
3019 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3022 #ifdef POINTERS_EXTEND_UNSIGNED
3023 if (GET_MODE (pat) != Pmode)
3024 pat = convert_to_mode (Pmode, pat,
3025 POINTERS_EXTEND_UNSIGNED);
3027 emit_move_insn (src_reg, pat);
3033 emit_insn_after (pat, before_strlen);
3035 emit_insn_before (pat, get_insns ());
3037 /* Return the value in the proper mode for this function. */
3038 if (GET_MODE (ops[0].value) == target_mode)
3039 target = ops[0].value;
3040 else if (target != 0)
3041 convert_move (target, ops[0].value, 0);
3043 target = convert_to_mode (target_mode, ops[0].value, 0);
3049 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3050 bytes from constant string DATA + OFFSET and return it as target
3054 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3055 enum machine_mode mode)
3057 const char *str = (const char *) data;
3059 gcc_assert (offset >= 0
3060 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3061 <= strlen (str) + 1));
3063 return c_readstr (str + offset, mode);
3066 /* Expand a call EXP to the memcpy builtin.
3067 Return NULL_RTX if we failed, the caller should emit a normal call,
3068 otherwise try to get the result in TARGET, if convenient (and in
3069 mode MODE if that's convenient). */
3072 expand_builtin_memcpy (tree exp, rtx target)
3074 if (!validate_arglist (exp,
3075 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3079 tree dest = CALL_EXPR_ARG (exp, 0);
3080 tree src = CALL_EXPR_ARG (exp, 1);
3081 tree len = CALL_EXPR_ARG (exp, 2);
3082 const char *src_str;
3083 unsigned int src_align = get_pointer_alignment (src);
3084 unsigned int dest_align = get_pointer_alignment (dest);
3085 rtx dest_mem, src_mem, dest_addr, len_rtx;
3086 HOST_WIDE_INT expected_size = -1;
3087 unsigned int expected_align = 0;
3089 /* If DEST is not a pointer type, call the normal function. */
3090 if (dest_align == 0)
3093 /* If either SRC is not a pointer type, don't do this
3094 operation in-line. */
3098 if (currently_expanding_gimple_stmt)
3099 stringop_block_profile (currently_expanding_gimple_stmt,
3100 &expected_align, &expected_size);
3102 if (expected_align < dest_align)
3103 expected_align = dest_align;
3104 dest_mem = get_memory_rtx (dest, len);
3105 set_mem_align (dest_mem, dest_align);
3106 len_rtx = expand_normal (len);
3107 src_str = c_getstr (src);
3109 /* If SRC is a string constant and block move would be done
3110 by pieces, we can avoid loading the string from memory
3111 and only stored the computed constants. */
3113 && CONST_INT_P (len_rtx)
3114 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3115 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3116 CONST_CAST (char *, src_str),
3119 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3120 builtin_memcpy_read_str,
3121 CONST_CAST (char *, src_str),
3122 dest_align, false, 0);
3123 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3124 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3128 src_mem = get_memory_rtx (src, len);
3129 set_mem_align (src_mem, src_align);
3131 /* Copy word part most expediently. */
3132 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3133 CALL_EXPR_TAILCALL (exp)
3134 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3135 expected_align, expected_size);
3139 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3140 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3146 /* Expand a call EXP to the mempcpy builtin.
3147 Return NULL_RTX if we failed; the caller should emit a normal call,
3148 otherwise try to get the result in TARGET, if convenient (and in
3149 mode MODE if that's convenient). If ENDP is 0 return the
3150 destination pointer, if ENDP is 1 return the end pointer ala
3151 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3155 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3157 if (!validate_arglist (exp,
3158 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3162 tree dest = CALL_EXPR_ARG (exp, 0);
3163 tree src = CALL_EXPR_ARG (exp, 1);
3164 tree len = CALL_EXPR_ARG (exp, 2);
3165 return expand_builtin_mempcpy_args (dest, src, len,
3166 target, mode, /*endp=*/ 1);
3170 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3171 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3172 so that this can also be called without constructing an actual CALL_EXPR.
3173 The other arguments and return value are the same as for
3174 expand_builtin_mempcpy. */
3177 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3178 rtx target, enum machine_mode mode, int endp)
3180 /* If return value is ignored, transform mempcpy into memcpy. */
3181 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3183 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3184 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3186 return expand_expr (result, target, mode, EXPAND_NORMAL);
3190 const char *src_str;
3191 unsigned int src_align = get_pointer_alignment (src);
3192 unsigned int dest_align = get_pointer_alignment (dest);
3193 rtx dest_mem, src_mem, len_rtx;
3195 /* If either SRC or DEST is not a pointer type, don't do this
3196 operation in-line. */
3197 if (dest_align == 0 || src_align == 0)
3200 /* If LEN is not constant, call the normal function. */
3201 if (! host_integerp (len, 1))
3204 len_rtx = expand_normal (len);
3205 src_str = c_getstr (src);
3207 /* If SRC is a string constant and block move would be done
3208 by pieces, we can avoid loading the string from memory
3209 and only stored the computed constants. */
3211 && CONST_INT_P (len_rtx)
3212 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3213 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3214 CONST_CAST (char *, src_str),
3217 dest_mem = get_memory_rtx (dest, len);
3218 set_mem_align (dest_mem, dest_align);
3219 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3220 builtin_memcpy_read_str,
3221 CONST_CAST (char *, src_str),
3222 dest_align, false, endp);
3223 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3224 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3228 if (CONST_INT_P (len_rtx)
3229 && can_move_by_pieces (INTVAL (len_rtx),
3230 MIN (dest_align, src_align)))
3232 dest_mem = get_memory_rtx (dest, len);
3233 set_mem_align (dest_mem, dest_align);
3234 src_mem = get_memory_rtx (src, len);
3235 set_mem_align (src_mem, src_align);
3236 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3237 MIN (dest_align, src_align), endp);
3238 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3239 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3248 # define HAVE_movstr 0
3249 # define CODE_FOR_movstr CODE_FOR_nothing
3252 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3253 we failed, the caller should emit a normal call, otherwise try to
3254 get the result in TARGET, if convenient. If ENDP is 0 return the
3255 destination pointer, if ENDP is 1 return the end pointer ala
3256 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3260 expand_movstr (tree dest, tree src, rtx target, int endp)
3262 struct expand_operand ops[3];
3269 dest_mem = get_memory_rtx (dest, NULL);
3270 src_mem = get_memory_rtx (src, NULL);
3273 target = force_reg (Pmode, XEXP (dest_mem, 0));
3274 dest_mem = replace_equiv_address (dest_mem, target);
3277 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3278 create_fixed_operand (&ops[1], dest_mem);
3279 create_fixed_operand (&ops[2], src_mem);
3280 expand_insn (CODE_FOR_movstr, 3, ops);
3282 if (endp && target != const0_rtx)
3284 target = ops[0].value;
3285 /* movstr is supposed to set end to the address of the NUL
3286 terminator. If the caller requested a mempcpy-like return value,
3290 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), target), 1);
3291 emit_move_insn (target, force_operand (tem, NULL_RTX));
3297 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3298 NULL_RTX if we failed the caller should emit a normal call, otherwise
3299 try to get the result in TARGET, if convenient (and in mode MODE if that's
3303 expand_builtin_strcpy (tree exp, rtx target)
3305 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3307 tree dest = CALL_EXPR_ARG (exp, 0);
3308 tree src = CALL_EXPR_ARG (exp, 1);
3309 return expand_builtin_strcpy_args (dest, src, target);
3314 /* Helper function to do the actual work for expand_builtin_strcpy. The
3315 arguments to the builtin_strcpy call DEST and SRC are broken out
3316 so that this can also be called without constructing an actual CALL_EXPR.
3317 The other arguments and return value are the same as for
3318 expand_builtin_strcpy. */
3321 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3323 return expand_movstr (dest, src, target, /*endp=*/0);
3326 /* Expand a call EXP to the stpcpy builtin.
3327 Return NULL_RTX if we failed the caller should emit a normal call,
3328 otherwise try to get the result in TARGET, if convenient (and in
3329 mode MODE if that's convenient). */
3332 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3335 location_t loc = EXPR_LOCATION (exp);
3337 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3340 dst = CALL_EXPR_ARG (exp, 0);
3341 src = CALL_EXPR_ARG (exp, 1);
3343 /* If return value is ignored, transform stpcpy into strcpy. */
3344 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3346 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3347 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3348 return expand_expr (result, target, mode, EXPAND_NORMAL);
3355 /* Ensure we get an actual string whose length can be evaluated at
3356 compile-time, not an expression containing a string. This is
3357 because the latter will potentially produce pessimized code
3358 when used to produce the return value. */
3359 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3360 return expand_movstr (dst, src, target, /*endp=*/2);
3362 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3363 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3364 target, mode, /*endp=*/2);
3369 if (TREE_CODE (len) == INTEGER_CST)
3371 rtx len_rtx = expand_normal (len);
3373 if (CONST_INT_P (len_rtx))
3375 ret = expand_builtin_strcpy_args (dst, src, target);
3381 if (mode != VOIDmode)
3382 target = gen_reg_rtx (mode);
3384 target = gen_reg_rtx (GET_MODE (ret));
3386 if (GET_MODE (target) != GET_MODE (ret))
3387 ret = gen_lowpart (GET_MODE (target), ret);
3389 ret = plus_constant (ret, INTVAL (len_rtx));
3390 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3398 return expand_movstr (dst, src, target, /*endp=*/2);
3402 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3403 bytes from constant string DATA + OFFSET and return it as target
3407 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3408 enum machine_mode mode)
3410 const char *str = (const char *) data;
3412 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3415 return c_readstr (str + offset, mode);
3418 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3419 NULL_RTX if we failed the caller should emit a normal call. */
3422 expand_builtin_strncpy (tree exp, rtx target)
3424 location_t loc = EXPR_LOCATION (exp);
3426 if (validate_arglist (exp,
3427 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3429 tree dest = CALL_EXPR_ARG (exp, 0);
3430 tree src = CALL_EXPR_ARG (exp, 1);
3431 tree len = CALL_EXPR_ARG (exp, 2);
3432 tree slen = c_strlen (src, 1);
3434 /* We must be passed a constant len and src parameter. */
3435 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3438 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3440 /* We're required to pad with trailing zeros if the requested
3441 len is greater than strlen(s2)+1. In that case try to
3442 use store_by_pieces, if it fails, punt. */
3443 if (tree_int_cst_lt (slen, len))
3445 unsigned int dest_align = get_pointer_alignment (dest);
3446 const char *p = c_getstr (src);
3449 if (!p || dest_align == 0 || !host_integerp (len, 1)
3450 || !can_store_by_pieces (tree_low_cst (len, 1),
3451 builtin_strncpy_read_str,
3452 CONST_CAST (char *, p),
3456 dest_mem = get_memory_rtx (dest, len);
3457 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3458 builtin_strncpy_read_str,
3459 CONST_CAST (char *, p), dest_align, false, 0);
3460 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3461 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3468 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3469 bytes from constant string DATA + OFFSET and return it as target
3473 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3474 enum machine_mode mode)
3476 const char *c = (const char *) data;
3477 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3479 memset (p, *c, GET_MODE_SIZE (mode));
3481 return c_readstr (p, mode);
3484 /* Callback routine for store_by_pieces. Return the RTL of a register
3485 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3486 char value given in the RTL register data. For example, if mode is
3487 4 bytes wide, return the RTL for 0x01010101*data. */
3490 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3491 enum machine_mode mode)
3497 size = GET_MODE_SIZE (mode);
3501 p = XALLOCAVEC (char, size);
3502 memset (p, 1, size);
3503 coeff = c_readstr (p, mode);
3505 target = convert_to_mode (mode, (rtx) data, 1);
3506 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3507 return force_reg (mode, target);
3510 /* Expand expression EXP, which is a call to the memset builtin. Return
3511 NULL_RTX if we failed the caller should emit a normal call, otherwise
3512 try to get the result in TARGET, if convenient (and in mode MODE if that's
3516 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3518 if (!validate_arglist (exp,
3519 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3523 tree dest = CALL_EXPR_ARG (exp, 0);
3524 tree val = CALL_EXPR_ARG (exp, 1);
3525 tree len = CALL_EXPR_ARG (exp, 2);
3526 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3530 /* Helper function to do the actual work for expand_builtin_memset. The
3531 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3532 so that this can also be called without constructing an actual CALL_EXPR.
3533 The other arguments and return value are the same as for
3534 expand_builtin_memset. */
3537 expand_builtin_memset_args (tree dest, tree val, tree len,
3538 rtx target, enum machine_mode mode, tree orig_exp)
3541 enum built_in_function fcode;
3542 enum machine_mode val_mode;
3544 unsigned int dest_align;
3545 rtx dest_mem, dest_addr, len_rtx;
3546 HOST_WIDE_INT expected_size = -1;
3547 unsigned int expected_align = 0;
3549 dest_align = get_pointer_alignment (dest);
3551 /* If DEST is not a pointer type, don't do this operation in-line. */
3552 if (dest_align == 0)
3555 if (currently_expanding_gimple_stmt)
3556 stringop_block_profile (currently_expanding_gimple_stmt,
3557 &expected_align, &expected_size);
3559 if (expected_align < dest_align)
3560 expected_align = dest_align;
3562 /* If the LEN parameter is zero, return DEST. */
3563 if (integer_zerop (len))
3565 /* Evaluate and ignore VAL in case it has side-effects. */
3566 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3567 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3570 /* Stabilize the arguments in case we fail. */
3571 dest = builtin_save_expr (dest);
3572 val = builtin_save_expr (val);
3573 len = builtin_save_expr (len);
3575 len_rtx = expand_normal (len);
3576 dest_mem = get_memory_rtx (dest, len);
3577 val_mode = TYPE_MODE (unsigned_char_type_node);
3579 if (TREE_CODE (val) != INTEGER_CST)
3583 val_rtx = expand_normal (val);
3584 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3586 /* Assume that we can memset by pieces if we can store
3587 * the coefficients by pieces (in the required modes).
3588 * We can't pass builtin_memset_gen_str as that emits RTL. */
3590 if (host_integerp (len, 1)
3591 && can_store_by_pieces (tree_low_cst (len, 1),
3592 builtin_memset_read_str, &c, dest_align,
3595 val_rtx = force_reg (val_mode, val_rtx);
3596 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3597 builtin_memset_gen_str, val_rtx, dest_align,
3600 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3601 dest_align, expected_align,
3605 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3606 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3610 if (target_char_cast (val, &c))
3615 if (host_integerp (len, 1)
3616 && can_store_by_pieces (tree_low_cst (len, 1),
3617 builtin_memset_read_str, &c, dest_align,
3619 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3620 builtin_memset_read_str, &c, dest_align, true, 0);
3621 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3622 gen_int_mode (c, val_mode),
3623 dest_align, expected_align,
3627 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3628 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3632 set_mem_align (dest_mem, dest_align);
3633 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3634 CALL_EXPR_TAILCALL (orig_exp)
3635 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3636 expected_align, expected_size);
3640 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3641 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3647 fndecl = get_callee_fndecl (orig_exp);
3648 fcode = DECL_FUNCTION_CODE (fndecl);
3649 if (fcode == BUILT_IN_MEMSET)
3650 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3652 else if (fcode == BUILT_IN_BZERO)
3653 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3657 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3658 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3659 return expand_call (fn, target, target == const0_rtx);
3662 /* Expand expression EXP, which is a call to the bzero builtin. Return
3663 NULL_RTX if we failed the caller should emit a normal call. */
3666 expand_builtin_bzero (tree exp)
3669 location_t loc = EXPR_LOCATION (exp);
3671 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3674 dest = CALL_EXPR_ARG (exp, 0);
3675 size = CALL_EXPR_ARG (exp, 1);
3677 /* New argument list transforming bzero(ptr x, int y) to
3678 memset(ptr x, int 0, size_t y). This is done this way
3679 so that if it isn't expanded inline, we fallback to
3680 calling bzero instead of memset. */
3682 return expand_builtin_memset_args (dest, integer_zero_node,
3683 fold_convert_loc (loc,
3684 size_type_node, size),
3685 const0_rtx, VOIDmode, exp);
3688 /* Expand expression EXP, which is a call to the memcmp built-in function.
3689 Return NULL_RTX if we failed and the caller should emit a normal call,
3690 otherwise try to get the result in TARGET, if convenient (and in mode
3691 MODE, if that's convenient). */
3694 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3695 ATTRIBUTE_UNUSED enum machine_mode mode)
3697 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3699 if (!validate_arglist (exp,
3700 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3703 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3704 implementing memcmp because it will stop if it encounters two
3706 #if defined HAVE_cmpmemsi
3708 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3711 tree arg1 = CALL_EXPR_ARG (exp, 0);
3712 tree arg2 = CALL_EXPR_ARG (exp, 1);
3713 tree len = CALL_EXPR_ARG (exp, 2);
3715 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3716 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3717 enum machine_mode insn_mode;
3720 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3724 /* If we don't have POINTER_TYPE, call the function. */
3725 if (arg1_align == 0 || arg2_align == 0)
3728 /* Make a place to write the result of the instruction. */
3731 && REG_P (result) && GET_MODE (result) == insn_mode
3732 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3733 result = gen_reg_rtx (insn_mode);
3735 arg1_rtx = get_memory_rtx (arg1, len);
3736 arg2_rtx = get_memory_rtx (arg2, len);
3737 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3739 /* Set MEM_SIZE as appropriate. */
3740 if (CONST_INT_P (arg3_rtx))
3742 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3743 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3747 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3748 GEN_INT (MIN (arg1_align, arg2_align)));
3755 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3756 TYPE_MODE (integer_type_node), 3,
3757 XEXP (arg1_rtx, 0), Pmode,
3758 XEXP (arg2_rtx, 0), Pmode,
3759 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3760 TYPE_UNSIGNED (sizetype)),
3761 TYPE_MODE (sizetype));
3763 /* Return the value in the proper mode for this function. */
3764 mode = TYPE_MODE (TREE_TYPE (exp));
3765 if (GET_MODE (result) == mode)
3767 else if (target != 0)
3769 convert_move (target, result, 0);
3773 return convert_to_mode (mode, result, 0);
3775 #endif /* HAVE_cmpmemsi. */
3780 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3781 if we failed the caller should emit a normal call, otherwise try to get
3782 the result in TARGET, if convenient. */
3785 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3787 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3790 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3791 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3792 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3794 rtx arg1_rtx, arg2_rtx;
3795 rtx result, insn = NULL_RTX;
3797 tree arg1 = CALL_EXPR_ARG (exp, 0);
3798 tree arg2 = CALL_EXPR_ARG (exp, 1);
3800 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3801 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3803 /* If we don't have POINTER_TYPE, call the function. */
3804 if (arg1_align == 0 || arg2_align == 0)
3807 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3808 arg1 = builtin_save_expr (arg1);
3809 arg2 = builtin_save_expr (arg2);
3811 arg1_rtx = get_memory_rtx (arg1, NULL);
3812 arg2_rtx = get_memory_rtx (arg2, NULL);
3814 #ifdef HAVE_cmpstrsi
3815 /* Try to call cmpstrsi. */
3818 enum machine_mode insn_mode
3819 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3821 /* Make a place to write the result of the instruction. */
3824 && REG_P (result) && GET_MODE (result) == insn_mode
3825 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3826 result = gen_reg_rtx (insn_mode);
3828 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3829 GEN_INT (MIN (arg1_align, arg2_align)));
3832 #ifdef HAVE_cmpstrnsi
3833 /* Try to determine at least one length and call cmpstrnsi. */
3834 if (!insn && HAVE_cmpstrnsi)
3839 enum machine_mode insn_mode
3840 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3841 tree len1 = c_strlen (arg1, 1);
3842 tree len2 = c_strlen (arg2, 1);
3845 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3847 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3849 /* If we don't have a constant length for the first, use the length
3850 of the second, if we know it. We don't require a constant for
3851 this case; some cost analysis could be done if both are available
3852 but neither is constant. For now, assume they're equally cheap,
3853 unless one has side effects. If both strings have constant lengths,
3860 else if (TREE_SIDE_EFFECTS (len1))
3862 else if (TREE_SIDE_EFFECTS (len2))
3864 else if (TREE_CODE (len1) != INTEGER_CST)
3866 else if (TREE_CODE (len2) != INTEGER_CST)
3868 else if (tree_int_cst_lt (len1, len2))
3873 /* If both arguments have side effects, we cannot optimize. */
3874 if (!len || TREE_SIDE_EFFECTS (len))
3877 arg3_rtx = expand_normal (len);
3879 /* Make a place to write the result of the instruction. */
3882 && REG_P (result) && GET_MODE (result) == insn_mode
3883 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3884 result = gen_reg_rtx (insn_mode);
3886 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3887 GEN_INT (MIN (arg1_align, arg2_align)));
3893 enum machine_mode mode;
3896 /* Return the value in the proper mode for this function. */
3897 mode = TYPE_MODE (TREE_TYPE (exp));
3898 if (GET_MODE (result) == mode)
3901 return convert_to_mode (mode, result, 0);
3902 convert_move (target, result, 0);
3906 /* Expand the library call ourselves using a stabilized argument
3907 list to avoid re-evaluating the function's arguments twice. */
3908 #ifdef HAVE_cmpstrnsi
3911 fndecl = get_callee_fndecl (exp);
3912 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
3913 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3914 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3915 return expand_call (fn, target, target == const0_rtx);
3921 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3922 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3923 the result in TARGET, if convenient. */
3926 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3927 ATTRIBUTE_UNUSED enum machine_mode mode)
3929 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3931 if (!validate_arglist (exp,
3932 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3935 /* If c_strlen can determine an expression for one of the string
3936 lengths, and it doesn't have side effects, then emit cmpstrnsi
3937 using length MIN(strlen(string)+1, arg3). */
3938 #ifdef HAVE_cmpstrnsi
3941 tree len, len1, len2;
3942 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3945 tree arg1 = CALL_EXPR_ARG (exp, 0);
3946 tree arg2 = CALL_EXPR_ARG (exp, 1);
3947 tree arg3 = CALL_EXPR_ARG (exp, 2);
3949 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3950 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3951 enum machine_mode insn_mode
3952 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3954 len1 = c_strlen (arg1, 1);
3955 len2 = c_strlen (arg2, 1);
3958 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
3960 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
3962 /* If we don't have a constant length for the first, use the length
3963 of the second, if we know it. We don't require a constant for
3964 this case; some cost analysis could be done if both are available
3965 but neither is constant. For now, assume they're equally cheap,
3966 unless one has side effects. If both strings have constant lengths,
3973 else if (TREE_SIDE_EFFECTS (len1))
3975 else if (TREE_SIDE_EFFECTS (len2))
3977 else if (TREE_CODE (len1) != INTEGER_CST)
3979 else if (TREE_CODE (len2) != INTEGER_CST)
3981 else if (tree_int_cst_lt (len1, len2))
3986 /* If both arguments have side effects, we cannot optimize. */
3987 if (!len || TREE_SIDE_EFFECTS (len))
3990 /* The actual new length parameter is MIN(len,arg3). */
3991 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
3992 fold_convert_loc (loc, TREE_TYPE (len), arg3));
3994 /* If we don't have POINTER_TYPE, call the function. */
3995 if (arg1_align == 0 || arg2_align == 0)
3998 /* Make a place to write the result of the instruction. */
4001 && REG_P (result) && GET_MODE (result) == insn_mode
4002 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
4003 result = gen_reg_rtx (insn_mode);
4005 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4006 arg1 = builtin_save_expr (arg1);
4007 arg2 = builtin_save_expr (arg2);
4008 len = builtin_save_expr (len);
4010 arg1_rtx = get_memory_rtx (arg1, len);
4011 arg2_rtx = get_memory_rtx (arg2, len);
4012 arg3_rtx = expand_normal (len);
4013 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
4014 GEN_INT (MIN (arg1_align, arg2_align)));
4019 /* Return the value in the proper mode for this function. */
4020 mode = TYPE_MODE (TREE_TYPE (exp));
4021 if (GET_MODE (result) == mode)
4024 return convert_to_mode (mode, result, 0);
4025 convert_move (target, result, 0);
4029 /* Expand the library call ourselves using a stabilized argument
4030 list to avoid re-evaluating the function's arguments twice. */
4031 fndecl = get_callee_fndecl (exp);
4032 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
4034 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4035 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4036 return expand_call (fn, target, target == const0_rtx);
4042 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4043 if that's convenient. */
4046 expand_builtin_saveregs (void)
4050 /* Don't do __builtin_saveregs more than once in a function.
4051 Save the result of the first call and reuse it. */
4052 if (saveregs_value != 0)
4053 return saveregs_value;
4055 /* When this function is called, it means that registers must be
4056 saved on entry to this function. So we migrate the call to the
4057 first insn of this function. */
4061 /* Do whatever the machine needs done in this case. */
4062 val = targetm.calls.expand_builtin_saveregs ();
4067 saveregs_value = val;
4069 /* Put the insns after the NOTE that starts the function. If this
4070 is inside a start_sequence, make the outer-level insn chain current, so
4071 the code is placed at the start of the function. */
4072 push_topmost_sequence ();
4073 emit_insn_after (seq, entry_of_function ());
4074 pop_topmost_sequence ();
4079 /* Expand a call to __builtin_next_arg. */
4082 expand_builtin_next_arg (void)
4084 /* Checking arguments is already done in fold_builtin_next_arg
4085 that must be called before this function. */
4086 return expand_binop (ptr_mode, add_optab,
4087 crtl->args.internal_arg_pointer,
4088 crtl->args.arg_offset_rtx,
4089 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4092 /* Make it easier for the backends by protecting the valist argument
4093 from multiple evaluations. */
4096 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4098 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4100 /* The current way of determining the type of valist is completely
4101 bogus. We should have the information on the va builtin instead. */
4103 vatype = targetm.fn_abi_va_list (cfun->decl);
4105 if (TREE_CODE (vatype) == ARRAY_TYPE)
4107 if (TREE_SIDE_EFFECTS (valist))
4108 valist = save_expr (valist);
4110 /* For this case, the backends will be expecting a pointer to
4111 vatype, but it's possible we've actually been given an array
4112 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4114 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4116 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4117 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4122 tree pt = build_pointer_type (vatype);
4126 if (! TREE_SIDE_EFFECTS (valist))
4129 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4130 TREE_SIDE_EFFECTS (valist) = 1;
4133 if (TREE_SIDE_EFFECTS (valist))
4134 valist = save_expr (valist);
4135 valist = fold_build2_loc (loc, MEM_REF,
4136 vatype, valist, build_int_cst (pt, 0));
4142 /* The "standard" definition of va_list is void*. */
4145 std_build_builtin_va_list (void)
4147 return ptr_type_node;
4150 /* The "standard" abi va_list is va_list_type_node. */
4153 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4155 return va_list_type_node;
4158 /* The "standard" type of va_list is va_list_type_node. */
4161 std_canonical_va_list_type (tree type)
4165 if (INDIRECT_REF_P (type))
4166 type = TREE_TYPE (type);
4167 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4168 type = TREE_TYPE (type);
4169 wtype = va_list_type_node;
4171 /* Treat structure va_list types. */
4172 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4173 htype = TREE_TYPE (htype);
4174 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4176 /* If va_list is an array type, the argument may have decayed
4177 to a pointer type, e.g. by being passed to another function.
4178 In that case, unwrap both types so that we can compare the
4179 underlying records. */
4180 if (TREE_CODE (htype) == ARRAY_TYPE
4181 || POINTER_TYPE_P (htype))
4183 wtype = TREE_TYPE (wtype);
4184 htype = TREE_TYPE (htype);
4187 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4188 return va_list_type_node;
4193 /* The "standard" implementation of va_start: just assign `nextarg' to
4197 std_expand_builtin_va_start (tree valist, rtx nextarg)
4199 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4200 convert_move (va_r, nextarg, 0);
4203 /* Expand EXP, a call to __builtin_va_start. */
4206 expand_builtin_va_start (tree exp)
4210 location_t loc = EXPR_LOCATION (exp);
4212 if (call_expr_nargs (exp) < 2)
4214 error_at (loc, "too few arguments to function %<va_start%>");
4218 if (fold_builtin_next_arg (exp, true))
4221 nextarg = expand_builtin_next_arg ();
4222 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4224 if (targetm.expand_builtin_va_start)
4225 targetm.expand_builtin_va_start (valist, nextarg);
4227 std_expand_builtin_va_start (valist, nextarg);
4232 /* The "standard" implementation of va_arg: read the value from the
4233 current (padded) address and increment by the (padded) size. */
4236 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4239 tree addr, t, type_size, rounded_size, valist_tmp;
4240 unsigned HOST_WIDE_INT align, boundary;
4243 #ifdef ARGS_GROW_DOWNWARD
4244 /* All of the alignment and movement below is for args-grow-up machines.
4245 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4246 implement their own specialized gimplify_va_arg_expr routines. */
4250 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4252 type = build_pointer_type (type);
4254 align = PARM_BOUNDARY / BITS_PER_UNIT;
4255 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4257 /* When we align parameter on stack for caller, if the parameter
4258 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4259 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4260 here with caller. */
4261 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4262 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4264 boundary /= BITS_PER_UNIT;
4266 /* Hoist the valist value into a temporary for the moment. */
4267 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4269 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4270 requires greater alignment, we must perform dynamic alignment. */
4271 if (boundary > align
4272 && !integer_zerop (TYPE_SIZE (type)))
4274 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4275 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
4276 gimplify_and_add (t, pre_p);
4278 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4279 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
4281 build_int_cst (TREE_TYPE (valist), -boundary)));
4282 gimplify_and_add (t, pre_p);
4287 /* If the actual alignment is less than the alignment of the type,
4288 adjust the type accordingly so that we don't assume strict alignment
4289 when dereferencing the pointer. */
4290 boundary *= BITS_PER_UNIT;
4291 if (boundary < TYPE_ALIGN (type))
4293 type = build_variant_type_copy (type);
4294 TYPE_ALIGN (type) = boundary;
4297 /* Compute the rounded size of the type. */
4298 type_size = size_in_bytes (type);
4299 rounded_size = round_up (type_size, align);
4301 /* Reduce rounded_size so it's sharable with the postqueue. */
4302 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4306 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4308 /* Small args are padded downward. */
4309 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4310 rounded_size, size_int (align));
4311 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4312 size_binop (MINUS_EXPR, rounded_size, type_size));
4313 addr = fold_build_pointer_plus (addr, t);
4316 /* Compute new value for AP. */
4317 t = fold_build_pointer_plus (valist_tmp, rounded_size);
4318 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4319 gimplify_and_add (t, pre_p);
4321 addr = fold_convert (build_pointer_type (type), addr);
4324 addr = build_va_arg_indirect_ref (addr);
4326 return build_va_arg_indirect_ref (addr);
4329 /* Build an indirect-ref expression over the given TREE, which represents a
4330 piece of a va_arg() expansion. */
4332 build_va_arg_indirect_ref (tree addr)
4334 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4336 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4342 /* Return a dummy expression of type TYPE in order to keep going after an
4346 dummy_object (tree type)
4348 tree t = build_int_cst (build_pointer_type (type), 0);
4349 return build2 (MEM_REF, type, t, t);
4352 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4353 builtin function, but a very special sort of operator. */
4355 enum gimplify_status
4356 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4358 tree promoted_type, have_va_type;
4359 tree valist = TREE_OPERAND (*expr_p, 0);
4360 tree type = TREE_TYPE (*expr_p);
4362 location_t loc = EXPR_LOCATION (*expr_p);
4364 /* Verify that valist is of the proper type. */
4365 have_va_type = TREE_TYPE (valist);
4366 if (have_va_type == error_mark_node)
4368 have_va_type = targetm.canonical_va_list_type (have_va_type);
4370 if (have_va_type == NULL_TREE)
4372 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4376 /* Generate a diagnostic for requesting data of a type that cannot
4377 be passed through `...' due to type promotion at the call site. */
4378 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4381 static bool gave_help;
4384 /* Unfortunately, this is merely undefined, rather than a constraint
4385 violation, so we cannot make this an error. If this call is never
4386 executed, the program is still strictly conforming. */
4387 warned = warning_at (loc, 0,
4388 "%qT is promoted to %qT when passed through %<...%>",
4389 type, promoted_type);
4390 if (!gave_help && warned)
4393 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4394 promoted_type, type);
4397 /* We can, however, treat "undefined" any way we please.
4398 Call abort to encourage the user to fix the program. */
4400 inform (loc, "if this code is reached, the program will abort");
4401 /* Before the abort, allow the evaluation of the va_list
4402 expression to exit or longjmp. */
4403 gimplify_and_add (valist, pre_p);
4404 t = build_call_expr_loc (loc,
4405 builtin_decl_implicit (BUILT_IN_TRAP), 0);
4406 gimplify_and_add (t, pre_p);
4408 /* This is dead code, but go ahead and finish so that the
4409 mode of the result comes out right. */
4410 *expr_p = dummy_object (type);
4415 /* Make it easier for the backends by protecting the valist argument
4416 from multiple evaluations. */
4417 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4419 /* For this case, the backends will be expecting a pointer to
4420 TREE_TYPE (abi), but it's possible we've
4421 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4423 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4425 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4426 valist = fold_convert_loc (loc, p1,
4427 build_fold_addr_expr_loc (loc, valist));
4430 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4433 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4435 if (!targetm.gimplify_va_arg_expr)
4436 /* FIXME: Once most targets are converted we should merely
4437 assert this is non-null. */
4440 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4445 /* Expand EXP, a call to __builtin_va_end. */
4448 expand_builtin_va_end (tree exp)
4450 tree valist = CALL_EXPR_ARG (exp, 0);
4452 /* Evaluate for side effects, if needed. I hate macros that don't
4454 if (TREE_SIDE_EFFECTS (valist))
4455 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4460 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4461 builtin rather than just as an assignment in stdarg.h because of the
4462 nastiness of array-type va_list types. */
4465 expand_builtin_va_copy (tree exp)
4468 location_t loc = EXPR_LOCATION (exp);
4470 dst = CALL_EXPR_ARG (exp, 0);
4471 src = CALL_EXPR_ARG (exp, 1);
4473 dst = stabilize_va_list_loc (loc, dst, 1);
4474 src = stabilize_va_list_loc (loc, src, 0);
4476 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4478 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4480 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4481 TREE_SIDE_EFFECTS (t) = 1;
4482 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4486 rtx dstb, srcb, size;
4488 /* Evaluate to pointers. */
4489 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4490 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4491 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4492 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4494 dstb = convert_memory_address (Pmode, dstb);
4495 srcb = convert_memory_address (Pmode, srcb);
4497 /* "Dereference" to BLKmode memories. */
4498 dstb = gen_rtx_MEM (BLKmode, dstb);
4499 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4500 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4501 srcb = gen_rtx_MEM (BLKmode, srcb);
4502 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4503 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4506 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4512 /* Expand a call to one of the builtin functions __builtin_frame_address or
4513 __builtin_return_address. */
4516 expand_builtin_frame_address (tree fndecl, tree exp)
4518 /* The argument must be a nonnegative integer constant.
4519 It counts the number of frames to scan up the stack.
4520 The value is the return address saved in that frame. */
4521 if (call_expr_nargs (exp) == 0)
4522 /* Warning about missing arg was already issued. */
4524 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4526 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4527 error ("invalid argument to %<__builtin_frame_address%>");
4529 error ("invalid argument to %<__builtin_return_address%>");
4535 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4536 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4538 /* Some ports cannot access arbitrary stack frames. */
4541 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4542 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4544 warning (0, "unsupported argument to %<__builtin_return_address%>");
4548 /* For __builtin_frame_address, return what we've got. */
4549 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4553 && ! CONSTANT_P (tem))
4554 tem = copy_to_mode_reg (Pmode, tem);
4559 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4560 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4561 is the same as for allocate_dynamic_stack_space. */
4564 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4570 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4571 == BUILT_IN_ALLOCA_WITH_ALIGN);
4573 /* Emit normal call if we use mudflap. */
4578 = (alloca_with_align
4579 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4580 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4585 /* Compute the argument. */
4586 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4588 /* Compute the alignment. */
4589 align = (alloca_with_align
4590 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4591 : BIGGEST_ALIGNMENT);
4593 /* Allocate the desired space. */
4594 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4595 result = convert_memory_address (ptr_mode, result);
4600 /* Expand a call to a bswap builtin with argument ARG0. MODE
4601 is the mode to expand with. */
4604 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
4606 enum machine_mode mode;
4610 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4613 arg = CALL_EXPR_ARG (exp, 0);
4614 mode = TYPE_MODE (TREE_TYPE (arg));
4615 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4617 target = expand_unop (mode, bswap_optab, op0, target, 1);
4619 gcc_assert (target);
4621 return convert_to_mode (mode, target, 0);
4624 /* Expand a call to a unary builtin in EXP.
4625 Return NULL_RTX if a normal call should be emitted rather than expanding the
4626 function in-line. If convenient, the result should be placed in TARGET.
4627 SUBTARGET may be used as the target for computing one of EXP's operands. */
4630 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4631 rtx subtarget, optab op_optab)
4635 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4638 /* Compute the argument. */
4639 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4641 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4642 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4643 VOIDmode, EXPAND_NORMAL);
4644 /* Compute op, into TARGET if possible.
4645 Set TARGET to wherever the result comes back. */
4646 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4647 op_optab, op0, target, op_optab != clrsb_optab);
4648 gcc_assert (target);
4650 return convert_to_mode (target_mode, target, 0);
4653 /* Expand a call to __builtin_expect. We just return our argument
4654 as the builtin_expect semantic should've been already executed by
4655 tree branch prediction pass. */
4658 expand_builtin_expect (tree exp, rtx target)
4662 if (call_expr_nargs (exp) < 2)
4664 arg = CALL_EXPR_ARG (exp, 0);
4666 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4667 /* When guessing was done, the hints should be already stripped away. */
4668 gcc_assert (!flag_guess_branch_prob
4669 || optimize == 0 || seen_error ());
4673 /* Expand a call to __builtin_assume_aligned. We just return our first
4674 argument as the builtin_assume_aligned semantic should've been already
4678 expand_builtin_assume_aligned (tree exp, rtx target)
4680 if (call_expr_nargs (exp) < 2)
4682 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4684 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4685 && (call_expr_nargs (exp) < 3
4686 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4691 expand_builtin_trap (void)
4695 emit_insn (gen_trap ());
4698 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4702 /* Expand a call to __builtin_unreachable. We do nothing except emit
4703 a barrier saying that control flow will not pass here.
4705 It is the responsibility of the program being compiled to ensure
4706 that control flow does never reach __builtin_unreachable. */
4708 expand_builtin_unreachable (void)
4713 /* Expand EXP, a call to fabs, fabsf or fabsl.
4714 Return NULL_RTX if a normal call should be emitted rather than expanding
4715 the function inline. If convenient, the result should be placed
4716 in TARGET. SUBTARGET may be used as the target for computing
4720 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4722 enum machine_mode mode;
4726 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4729 arg = CALL_EXPR_ARG (exp, 0);
4730 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4731 mode = TYPE_MODE (TREE_TYPE (arg));
4732 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4733 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4736 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4737 Return NULL is a normal call should be emitted rather than expanding the
4738 function inline. If convenient, the result should be placed in TARGET.
4739 SUBTARGET may be used as the target for computing the operand. */
4742 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4747 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4750 arg = CALL_EXPR_ARG (exp, 0);
4751 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4753 arg = CALL_EXPR_ARG (exp, 1);
4754 op1 = expand_normal (arg);
4756 return expand_copysign (op0, op1, target);
4759 /* Create a new constant string literal and return a char* pointer to it.
4760 The STRING_CST value is the LEN characters at STR. */
4762 build_string_literal (int len, const char *str)
4764 tree t, elem, index, type;
4766 t = build_string (len, str);
4767 elem = build_type_variant (char_type_node, 1, 0);
4768 index = build_index_type (size_int (len - 1));
4769 type = build_array_type (elem, index);
4770 TREE_TYPE (t) = type;
4771 TREE_CONSTANT (t) = 1;
4772 TREE_READONLY (t) = 1;
4773 TREE_STATIC (t) = 1;
4775 type = build_pointer_type (elem);
4776 t = build1 (ADDR_EXPR, type,
4777 build4 (ARRAY_REF, elem,
4778 t, integer_zero_node, NULL_TREE, NULL_TREE));
4782 /* Expand a call to __builtin___clear_cache. */
4785 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4787 #ifndef HAVE_clear_cache
4788 #ifdef CLEAR_INSN_CACHE
4789 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4790 does something. Just do the default expansion to a call to
4794 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4795 does nothing. There is no need to call it. Do nothing. */
4797 #endif /* CLEAR_INSN_CACHE */
4799 /* We have a "clear_cache" insn, and it will handle everything. */
4801 rtx begin_rtx, end_rtx;
4803 /* We must not expand to a library call. If we did, any
4804 fallback library function in libgcc that might contain a call to
4805 __builtin___clear_cache() would recurse infinitely. */
4806 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4808 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4812 if (HAVE_clear_cache)
4814 struct expand_operand ops[2];
4816 begin = CALL_EXPR_ARG (exp, 0);
4817 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4819 end = CALL_EXPR_ARG (exp, 1);
4820 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4822 create_address_operand (&ops[0], begin_rtx);
4823 create_address_operand (&ops[1], end_rtx);
4824 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4828 #endif /* HAVE_clear_cache */
4831 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4834 round_trampoline_addr (rtx tramp)
4836 rtx temp, addend, mask;
4838 /* If we don't need too much alignment, we'll have been guaranteed
4839 proper alignment by get_trampoline_type. */
4840 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4843 /* Round address up to desired boundary. */
4844 temp = gen_reg_rtx (Pmode);
4845 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4846 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4848 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4849 temp, 0, OPTAB_LIB_WIDEN);
4850 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4851 temp, 0, OPTAB_LIB_WIDEN);
4857 expand_builtin_init_trampoline (tree exp, bool onstack)
4859 tree t_tramp, t_func, t_chain;
4860 rtx m_tramp, r_tramp, r_chain, tmp;
4862 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4863 POINTER_TYPE, VOID_TYPE))
4866 t_tramp = CALL_EXPR_ARG (exp, 0);
4867 t_func = CALL_EXPR_ARG (exp, 1);
4868 t_chain = CALL_EXPR_ARG (exp, 2);
4870 r_tramp = expand_normal (t_tramp);
4871 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4872 MEM_NOTRAP_P (m_tramp) = 1;
4874 /* If ONSTACK, the TRAMP argument should be the address of a field
4875 within the local function's FRAME decl. Either way, let's see if
4876 we can fill in the MEM_ATTRs for this memory. */
4877 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4878 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
4881 /* Creator of a heap trampoline is responsible for making sure the
4882 address is aligned to at least STACK_BOUNDARY. Normally malloc
4883 will ensure this anyhow. */
4884 tmp = round_trampoline_addr (r_tramp);
4887 m_tramp = change_address (m_tramp, BLKmode, tmp);
4888 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4889 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4892 /* The FUNC argument should be the address of the nested function.
4893 Extract the actual function decl to pass to the hook. */
4894 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4895 t_func = TREE_OPERAND (t_func, 0);
4896 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4898 r_chain = expand_normal (t_chain);
4900 /* Generate insns to initialize the trampoline. */
4901 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4905 trampolines_created = 1;
4907 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4908 "trampoline generated for nested function %qD", t_func);
4915 expand_builtin_adjust_trampoline (tree exp)
4919 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4922 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4923 tramp = round_trampoline_addr (tramp);
4924 if (targetm.calls.trampoline_adjust_address)
4925 tramp = targetm.calls.trampoline_adjust_address (tramp);
4930 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4931 function. The function first checks whether the back end provides
4932 an insn to implement signbit for the respective mode. If not, it
4933 checks whether the floating point format of the value is such that
4934 the sign bit can be extracted. If that is not the case, the
4935 function returns NULL_RTX to indicate that a normal call should be
4936 emitted rather than expanding the function in-line. EXP is the
4937 expression that is a call to the builtin function; if convenient,
4938 the result should be placed in TARGET. */
4940 expand_builtin_signbit (tree exp, rtx target)
4942 const struct real_format *fmt;
4943 enum machine_mode fmode, imode, rmode;
4946 enum insn_code icode;
4948 location_t loc = EXPR_LOCATION (exp);
4950 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4953 arg = CALL_EXPR_ARG (exp, 0);
4954 fmode = TYPE_MODE (TREE_TYPE (arg));
4955 rmode = TYPE_MODE (TREE_TYPE (exp));
4956 fmt = REAL_MODE_FORMAT (fmode);
4958 arg = builtin_save_expr (arg);
4960 /* Expand the argument yielding a RTX expression. */
4961 temp = expand_normal (arg);
4963 /* Check if the back end provides an insn that handles signbit for the
4965 icode = optab_handler (signbit_optab, fmode);
4966 if (icode != CODE_FOR_nothing)
4968 rtx last = get_last_insn ();
4969 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4970 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4972 delete_insns_since (last);
4975 /* For floating point formats without a sign bit, implement signbit
4977 bitpos = fmt->signbit_ro;
4980 /* But we can't do this if the format supports signed zero. */
4981 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4984 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4985 build_real (TREE_TYPE (arg), dconst0));
4986 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4989 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4991 imode = int_mode_for_mode (fmode);
4992 if (imode == BLKmode)
4994 temp = gen_lowpart (imode, temp);
4999 /* Handle targets with different FP word orders. */
5000 if (FLOAT_WORDS_BIG_ENDIAN)
5001 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5003 word = bitpos / BITS_PER_WORD;
5004 temp = operand_subword_force (temp, word, fmode);
5005 bitpos = bitpos % BITS_PER_WORD;
5008 /* Force the intermediate word_mode (or narrower) result into a
5009 register. This avoids attempting to create paradoxical SUBREGs
5010 of floating point modes below. */
5011 temp = force_reg (imode, temp);
5013 /* If the bitpos is within the "result mode" lowpart, the operation
5014 can be implement with a single bitwise AND. Otherwise, we need
5015 a right shift and an AND. */
5017 if (bitpos < GET_MODE_BITSIZE (rmode))
5019 double_int mask = double_int_setbit (double_int_zero, bitpos);
5021 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5022 temp = gen_lowpart (rmode, temp);
5023 temp = expand_binop (rmode, and_optab, temp,
5024 immed_double_int_const (mask, rmode),
5025 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5029 /* Perform a logical right shift to place the signbit in the least
5030 significant bit, then truncate the result to the desired mode
5031 and mask just this bit. */
5032 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5033 temp = gen_lowpart (rmode, temp);
5034 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5035 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5041 /* Expand fork or exec calls. TARGET is the desired target of the
5042 call. EXP is the call. FN is the
5043 identificator of the actual function. IGNORE is nonzero if the
5044 value is to be ignored. */
5047 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5052 /* If we are not profiling, just call the function. */
5053 if (!profile_arc_flag)
5056 /* Otherwise call the wrapper. This should be equivalent for the rest of
5057 compiler, so the code does not diverge, and the wrapper may run the
5058 code necessary for keeping the profiling sane. */
5060 switch (DECL_FUNCTION_CODE (fn))
5063 id = get_identifier ("__gcov_fork");
5066 case BUILT_IN_EXECL:
5067 id = get_identifier ("__gcov_execl");
5070 case BUILT_IN_EXECV:
5071 id = get_identifier ("__gcov_execv");
5074 case BUILT_IN_EXECLP:
5075 id = get_identifier ("__gcov_execlp");
5078 case BUILT_IN_EXECLE:
5079 id = get_identifier ("__gcov_execle");
5082 case BUILT_IN_EXECVP:
5083 id = get_identifier ("__gcov_execvp");
5086 case BUILT_IN_EXECVE:
5087 id = get_identifier ("__gcov_execve");
5094 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5095 FUNCTION_DECL, id, TREE_TYPE (fn));
5096 DECL_EXTERNAL (decl) = 1;
5097 TREE_PUBLIC (decl) = 1;
5098 DECL_ARTIFICIAL (decl) = 1;
5099 TREE_NOTHROW (decl) = 1;
5100 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5101 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5102 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5103 return expand_call (call, target, ignore);
5108 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5109 the pointer in these functions is void*, the tree optimizers may remove
5110 casts. The mode computed in expand_builtin isn't reliable either, due
5111 to __sync_bool_compare_and_swap.
5113 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5114 group of builtins. This gives us log2 of the mode size. */
5116 static inline enum machine_mode
5117 get_builtin_sync_mode (int fcode_diff)
5119 /* The size is not negotiable, so ask not to get BLKmode in return
5120 if the target indicates that a smaller size would be better. */
5121 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5124 /* Expand the memory expression LOC and return the appropriate memory operand
5125 for the builtin_sync operations. */
5128 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5132 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5133 addr = convert_memory_address (Pmode, addr);
5135 /* Note that we explicitly do not want any alias information for this
5136 memory, so that we kill all other live memories. Otherwise we don't
5137 satisfy the full barrier semantics of the intrinsic. */
5138 mem = validize_mem (gen_rtx_MEM (mode, addr));
5140 /* The alignment needs to be at least according to that of the mode. */
5141 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5142 get_pointer_alignment (loc)));
5143 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5144 MEM_VOLATILE_P (mem) = 1;
5149 /* Make sure an argument is in the right mode.
5150 EXP is the tree argument.
5151 MODE is the mode it should be in. */
5154 expand_expr_force_mode (tree exp, enum machine_mode mode)
5157 enum machine_mode old_mode;
5159 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5160 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5161 of CONST_INTs, where we know the old_mode only from the call argument. */
5163 old_mode = GET_MODE (val);
5164 if (old_mode == VOIDmode)
5165 old_mode = TYPE_MODE (TREE_TYPE (exp));
5166 val = convert_modes (mode, old_mode, val, 1);
5171 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5172 EXP is the CALL_EXPR. CODE is the rtx code
5173 that corresponds to the arithmetic or logical operation from the name;
5174 an exception here is that NOT actually means NAND. TARGET is an optional
5175 place for us to store the results; AFTER is true if this is the
5176 fetch_and_xxx form. */
5179 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5180 enum rtx_code code, bool after,
5184 location_t loc = EXPR_LOCATION (exp);
5186 if (code == NOT && warn_sync_nand)
5188 tree fndecl = get_callee_fndecl (exp);
5189 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5191 static bool warned_f_a_n, warned_n_a_f;
5195 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5196 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5197 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5198 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5199 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5203 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5204 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5205 warned_f_a_n = true;
5208 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5209 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5210 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5211 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5212 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5216 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5217 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5218 warned_n_a_f = true;
5226 /* Expand the operands. */
5227 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5228 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5230 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5234 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5235 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5236 true if this is the boolean form. TARGET is a place for us to store the
5237 results; this is NOT optional if IS_BOOL is true. */
5240 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5241 bool is_bool, rtx target)
5243 rtx old_val, new_val, mem;
5246 /* Expand the operands. */
5247 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5248 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5249 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5251 pbool = poval = NULL;
5252 if (target != const0_rtx)
5259 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5260 false, MEMMODEL_SEQ_CST,
5267 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5268 general form is actually an atomic exchange, and some targets only
5269 support a reduced form with the second argument being a constant 1.
5270 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5274 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5279 /* Expand the operands. */
5280 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5281 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5283 return expand_sync_lock_test_and_set (target, mem, val);
5286 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5289 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5293 /* Expand the operands. */
5294 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5296 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5299 /* Given an integer representing an ``enum memmodel'', verify its
5300 correctness and return the memory model enum. */
5302 static enum memmodel
5303 get_memmodel (tree exp)
5307 /* If the parameter is not a constant, it's a run time value so we'll just
5308 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5309 if (TREE_CODE (exp) != INTEGER_CST)
5310 return MEMMODEL_SEQ_CST;
5312 op = expand_normal (exp);
5313 if (INTVAL (op) < 0 || INTVAL (op) >= MEMMODEL_LAST)
5315 warning (OPT_Winvalid_memory_model,
5316 "invalid memory model argument to builtin");
5317 return MEMMODEL_SEQ_CST;
5319 return (enum memmodel) INTVAL (op);
5322 /* Expand the __atomic_exchange intrinsic:
5323 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5324 EXP is the CALL_EXPR.
5325 TARGET is an optional place for us to store the results. */
5328 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5331 enum memmodel model;
5333 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5334 if (model == MEMMODEL_CONSUME)
5336 error ("invalid memory model for %<__atomic_exchange%>");
5340 if (!flag_inline_atomics)
5343 /* Expand the operands. */
5344 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5345 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5347 return expand_atomic_exchange (target, mem, val, model);
5350 /* Expand the __atomic_compare_exchange intrinsic:
5351 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5352 TYPE desired, BOOL weak,
5353 enum memmodel success,
5354 enum memmodel failure)
5355 EXP is the CALL_EXPR.
5356 TARGET is an optional place for us to store the results. */
5359 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5362 rtx expect, desired, mem, oldval;
5363 enum memmodel success, failure;
5367 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5368 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5370 if (failure == MEMMODEL_RELEASE || failure == MEMMODEL_ACQ_REL)
5372 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5376 if (failure > success)
5378 error ("failure memory model cannot be stronger than success "
5379 "memory model for %<__atomic_compare_exchange%>");
5383 if (!flag_inline_atomics)
5386 /* Expand the operands. */
5387 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5389 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5390 expect = convert_memory_address (Pmode, expect);
5391 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5393 weak = CALL_EXPR_ARG (exp, 3);
5395 if (host_integerp (weak, 0) && tree_low_cst (weak, 0) != 0)
5398 oldval = copy_to_reg (gen_rtx_MEM (mode, expect));
5400 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5401 &oldval, mem, oldval, desired,
5402 is_weak, success, failure))
5405 emit_move_insn (gen_rtx_MEM (mode, expect), oldval);
5409 /* Expand the __atomic_load intrinsic:
5410 TYPE __atomic_load (TYPE *object, enum memmodel)
5411 EXP is the CALL_EXPR.
5412 TARGET is an optional place for us to store the results. */
5415 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5418 enum memmodel model;
5420 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5421 if (model == MEMMODEL_RELEASE
5422 || model == MEMMODEL_ACQ_REL)
5424 error ("invalid memory model for %<__atomic_load%>");
5428 if (!flag_inline_atomics)
5431 /* Expand the operand. */
5432 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5434 return expand_atomic_load (target, mem, model);
5438 /* Expand the __atomic_store intrinsic:
5439 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5440 EXP is the CALL_EXPR.
5441 TARGET is an optional place for us to store the results. */
5444 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5447 enum memmodel model;
5449 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5450 if (model != MEMMODEL_RELAXED
5451 && model != MEMMODEL_SEQ_CST
5452 && model != MEMMODEL_RELEASE)
5454 error ("invalid memory model for %<__atomic_store%>");
5458 if (!flag_inline_atomics)
5461 /* Expand the operands. */
5462 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5463 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5465 return expand_atomic_store (mem, val, model, false);
5468 /* Expand the __atomic_fetch_XXX intrinsic:
5469 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5470 EXP is the CALL_EXPR.
5471 TARGET is an optional place for us to store the results.
5472 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5473 FETCH_AFTER is true if returning the result of the operation.
5474 FETCH_AFTER is false if returning the value before the operation.
5475 IGNORE is true if the result is not used.
5476 EXT_CALL is the correct builtin for an external call if this cannot be
5477 resolved to an instruction sequence. */
5480 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5481 enum rtx_code code, bool fetch_after,
5482 bool ignore, enum built_in_function ext_call)
5485 enum memmodel model;
5489 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5491 /* Expand the operands. */
5492 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5493 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5495 /* Only try generating instructions if inlining is turned on. */
5496 if (flag_inline_atomics)
5498 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5503 /* Return if a different routine isn't needed for the library call. */
5504 if (ext_call == BUILT_IN_NONE)
5507 /* Change the call to the specified function. */
5508 fndecl = get_callee_fndecl (exp);
5509 addr = CALL_EXPR_FN (exp);
5512 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5513 TREE_OPERAND (addr, 0) = builtin_decl_explicit(ext_call);
5515 /* Expand the call here so we can emit trailing code. */
5516 ret = expand_call (exp, target, ignore);
5518 /* Replace the original function just in case it matters. */
5519 TREE_OPERAND (addr, 0) = fndecl;
5521 /* Then issue the arithmetic correction to return the right result. */
5526 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5528 ret = expand_simple_unop (mode, NOT, ret, target, true);
5531 ret = expand_simple_binop (mode, code, ret, val, target, true,
5538 #ifndef HAVE_atomic_clear
5539 # define HAVE_atomic_clear 0
5540 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5543 /* Expand an atomic clear operation.
5544 void _atomic_clear (BOOL *obj, enum memmodel)
5545 EXP is the call expression. */
5548 expand_builtin_atomic_clear (tree exp)
5550 enum machine_mode mode;
5552 enum memmodel model;
5554 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5555 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5556 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5558 if (model == MEMMODEL_ACQUIRE || model == MEMMODEL_ACQ_REL)
5560 error ("invalid memory model for %<__atomic_store%>");
5564 if (HAVE_atomic_clear)
5566 emit_insn (gen_atomic_clear (mem, model));
5570 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5571 Failing that, a store is issued by __atomic_store. The only way this can
5572 fail is if the bool type is larger than a word size. Unlikely, but
5573 handle it anyway for completeness. Assume a single threaded model since
5574 there is no atomic support in this case, and no barriers are required. */
5575 ret = expand_atomic_store (mem, const0_rtx, model, true);
5577 emit_move_insn (mem, const0_rtx);
5581 /* Expand an atomic test_and_set operation.
5582 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5583 EXP is the call expression. */
5586 expand_builtin_atomic_test_and_set (tree exp, rtx target)
5589 enum memmodel model;
5590 enum machine_mode mode;
5592 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5593 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5594 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5596 return expand_atomic_test_and_set (target, mem, model);
5600 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5601 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5604 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5607 enum machine_mode mode;
5608 unsigned int mode_align, type_align;
5610 if (TREE_CODE (arg0) != INTEGER_CST)
5613 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5614 mode = mode_for_size (size, MODE_INT, 0);
5615 mode_align = GET_MODE_ALIGNMENT (mode);
5617 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5618 type_align = mode_align;
5621 tree ttype = TREE_TYPE (arg1);
5623 /* This function is usually invoked and folded immediately by the front
5624 end before anything else has a chance to look at it. The pointer
5625 parameter at this point is usually cast to a void *, so check for that
5626 and look past the cast. */
5627 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5628 && VOID_TYPE_P (TREE_TYPE (ttype)))
5629 arg1 = TREE_OPERAND (arg1, 0);
5631 ttype = TREE_TYPE (arg1);
5632 gcc_assert (POINTER_TYPE_P (ttype));
5634 /* Get the underlying type of the object. */
5635 ttype = TREE_TYPE (ttype);
5636 type_align = TYPE_ALIGN (ttype);
5639 /* If the object has smaller alignment, the the lock free routines cannot
5641 if (type_align < mode_align)
5642 return integer_zero_node;
5644 /* Check if a compare_and_swap pattern exists for the mode which represents
5645 the required size. The pattern is not allowed to fail, so the existence
5646 of the pattern indicates support is present. */
5647 if (can_compare_and_swap_p (mode, true))
5648 return integer_one_node;
5650 return integer_zero_node;
5653 /* Return true if the parameters to call EXP represent an object which will
5654 always generate lock free instructions. The first argument represents the
5655 size of the object, and the second parameter is a pointer to the object
5656 itself. If NULL is passed for the object, then the result is based on
5657 typical alignment for an object of the specified size. Otherwise return
5661 expand_builtin_atomic_always_lock_free (tree exp)
5664 tree arg0 = CALL_EXPR_ARG (exp, 0);
5665 tree arg1 = CALL_EXPR_ARG (exp, 1);
5667 if (TREE_CODE (arg0) != INTEGER_CST)
5669 error ("non-constant argument 1 to __atomic_always_lock_free");
5673 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5674 if (size == integer_one_node)
5679 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5680 is lock free on this architecture. */
5683 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5685 if (!flag_inline_atomics)
5688 /* If it isn't always lock free, don't generate a result. */
5689 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == integer_one_node)
5690 return integer_one_node;
5695 /* Return true if the parameters to call EXP represent an object which will
5696 always generate lock free instructions. The first argument represents the
5697 size of the object, and the second parameter is a pointer to the object
5698 itself. If NULL is passed for the object, then the result is based on
5699 typical alignment for an object of the specified size. Otherwise return
5703 expand_builtin_atomic_is_lock_free (tree exp)
5706 tree arg0 = CALL_EXPR_ARG (exp, 0);
5707 tree arg1 = CALL_EXPR_ARG (exp, 1);
5709 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5711 error ("non-integer argument 1 to __atomic_is_lock_free");
5715 if (!flag_inline_atomics)
5718 /* If the value is known at compile time, return the RTX for it. */
5719 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5720 if (size == integer_one_node)
5726 /* Expand the __atomic_thread_fence intrinsic:
5727 void __atomic_thread_fence (enum memmodel)
5728 EXP is the CALL_EXPR. */
5731 expand_builtin_atomic_thread_fence (tree exp)
5733 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5734 expand_mem_thread_fence (model);
5737 /* Expand the __atomic_signal_fence intrinsic:
5738 void __atomic_signal_fence (enum memmodel)
5739 EXP is the CALL_EXPR. */
5742 expand_builtin_atomic_signal_fence (tree exp)
5744 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5745 expand_mem_signal_fence (model);
5748 /* Expand the __sync_synchronize intrinsic. */
5751 expand_builtin_sync_synchronize (void)
5753 expand_mem_thread_fence (MEMMODEL_SEQ_CST);
5757 /* Expand an expression EXP that calls a built-in function,
5758 with result going to TARGET if that's convenient
5759 (and in mode MODE if that's convenient).
5760 SUBTARGET may be used as the target for computing one of EXP's operands.
5761 IGNORE is nonzero if the value is to be ignored. */
5764 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5767 tree fndecl = get_callee_fndecl (exp);
5768 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5769 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5772 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5773 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5775 /* When not optimizing, generate calls to library functions for a certain
5778 && !called_as_built_in (fndecl)
5779 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5780 && fcode != BUILT_IN_ALLOCA
5781 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5782 && fcode != BUILT_IN_FREE)
5783 return expand_call (exp, target, ignore);
5785 /* The built-in function expanders test for target == const0_rtx
5786 to determine whether the function's result will be ignored. */
5788 target = const0_rtx;
5790 /* If the result of a pure or const built-in function is ignored, and
5791 none of its arguments are volatile, we can avoid expanding the
5792 built-in call and just evaluate the arguments for side-effects. */
5793 if (target == const0_rtx
5794 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5795 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5797 bool volatilep = false;
5799 call_expr_arg_iterator iter;
5801 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5802 if (TREE_THIS_VOLATILE (arg))
5810 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5811 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5818 CASE_FLT_FN (BUILT_IN_FABS):
5819 target = expand_builtin_fabs (exp, target, subtarget);
5824 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5825 target = expand_builtin_copysign (exp, target, subtarget);
5830 /* Just do a normal library call if we were unable to fold
5832 CASE_FLT_FN (BUILT_IN_CABS):
5835 CASE_FLT_FN (BUILT_IN_EXP):
5836 CASE_FLT_FN (BUILT_IN_EXP10):
5837 CASE_FLT_FN (BUILT_IN_POW10):
5838 CASE_FLT_FN (BUILT_IN_EXP2):
5839 CASE_FLT_FN (BUILT_IN_EXPM1):
5840 CASE_FLT_FN (BUILT_IN_LOGB):
5841 CASE_FLT_FN (BUILT_IN_LOG):
5842 CASE_FLT_FN (BUILT_IN_LOG10):
5843 CASE_FLT_FN (BUILT_IN_LOG2):
5844 CASE_FLT_FN (BUILT_IN_LOG1P):
5845 CASE_FLT_FN (BUILT_IN_TAN):
5846 CASE_FLT_FN (BUILT_IN_ASIN):
5847 CASE_FLT_FN (BUILT_IN_ACOS):
5848 CASE_FLT_FN (BUILT_IN_ATAN):
5849 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5850 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5851 because of possible accuracy problems. */
5852 if (! flag_unsafe_math_optimizations)
5854 CASE_FLT_FN (BUILT_IN_SQRT):
5855 CASE_FLT_FN (BUILT_IN_FLOOR):
5856 CASE_FLT_FN (BUILT_IN_CEIL):
5857 CASE_FLT_FN (BUILT_IN_TRUNC):
5858 CASE_FLT_FN (BUILT_IN_ROUND):
5859 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5860 CASE_FLT_FN (BUILT_IN_RINT):
5861 target = expand_builtin_mathfn (exp, target, subtarget);
5866 CASE_FLT_FN (BUILT_IN_FMA):
5867 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5872 CASE_FLT_FN (BUILT_IN_ILOGB):
5873 if (! flag_unsafe_math_optimizations)
5875 CASE_FLT_FN (BUILT_IN_ISINF):
5876 CASE_FLT_FN (BUILT_IN_FINITE):
5877 case BUILT_IN_ISFINITE:
5878 case BUILT_IN_ISNORMAL:
5879 target = expand_builtin_interclass_mathfn (exp, target);
5884 CASE_FLT_FN (BUILT_IN_ICEIL):
5885 CASE_FLT_FN (BUILT_IN_LCEIL):
5886 CASE_FLT_FN (BUILT_IN_LLCEIL):
5887 CASE_FLT_FN (BUILT_IN_LFLOOR):
5888 CASE_FLT_FN (BUILT_IN_IFLOOR):
5889 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5890 target = expand_builtin_int_roundingfn (exp, target);
5895 CASE_FLT_FN (BUILT_IN_IRINT):
5896 CASE_FLT_FN (BUILT_IN_LRINT):
5897 CASE_FLT_FN (BUILT_IN_LLRINT):
5898 CASE_FLT_FN (BUILT_IN_IROUND):
5899 CASE_FLT_FN (BUILT_IN_LROUND):
5900 CASE_FLT_FN (BUILT_IN_LLROUND):
5901 target = expand_builtin_int_roundingfn_2 (exp, target);
5906 CASE_FLT_FN (BUILT_IN_POWI):
5907 target = expand_builtin_powi (exp, target);
5912 CASE_FLT_FN (BUILT_IN_ATAN2):
5913 CASE_FLT_FN (BUILT_IN_LDEXP):
5914 CASE_FLT_FN (BUILT_IN_SCALB):
5915 CASE_FLT_FN (BUILT_IN_SCALBN):
5916 CASE_FLT_FN (BUILT_IN_SCALBLN):
5917 if (! flag_unsafe_math_optimizations)
5920 CASE_FLT_FN (BUILT_IN_FMOD):
5921 CASE_FLT_FN (BUILT_IN_REMAINDER):
5922 CASE_FLT_FN (BUILT_IN_DREM):
5923 CASE_FLT_FN (BUILT_IN_POW):
5924 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5929 CASE_FLT_FN (BUILT_IN_CEXPI):
5930 target = expand_builtin_cexpi (exp, target);
5931 gcc_assert (target);
5934 CASE_FLT_FN (BUILT_IN_SIN):
5935 CASE_FLT_FN (BUILT_IN_COS):
5936 if (! flag_unsafe_math_optimizations)
5938 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5943 CASE_FLT_FN (BUILT_IN_SINCOS):
5944 if (! flag_unsafe_math_optimizations)
5946 target = expand_builtin_sincos (exp);
5951 case BUILT_IN_APPLY_ARGS:
5952 return expand_builtin_apply_args ();
5954 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5955 FUNCTION with a copy of the parameters described by
5956 ARGUMENTS, and ARGSIZE. It returns a block of memory
5957 allocated on the stack into which is stored all the registers
5958 that might possibly be used for returning the result of a
5959 function. ARGUMENTS is the value returned by
5960 __builtin_apply_args. ARGSIZE is the number of bytes of
5961 arguments that must be copied. ??? How should this value be
5962 computed? We'll also need a safe worst case value for varargs
5964 case BUILT_IN_APPLY:
5965 if (!validate_arglist (exp, POINTER_TYPE,
5966 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5967 && !validate_arglist (exp, REFERENCE_TYPE,
5968 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5974 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5975 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5976 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5978 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5981 /* __builtin_return (RESULT) causes the function to return the
5982 value described by RESULT. RESULT is address of the block of
5983 memory returned by __builtin_apply. */
5984 case BUILT_IN_RETURN:
5985 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5986 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5989 case BUILT_IN_SAVEREGS:
5990 return expand_builtin_saveregs ();
5992 case BUILT_IN_VA_ARG_PACK:
5993 /* All valid uses of __builtin_va_arg_pack () are removed during
5995 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
5998 case BUILT_IN_VA_ARG_PACK_LEN:
5999 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6001 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6004 /* Return the address of the first anonymous stack arg. */
6005 case BUILT_IN_NEXT_ARG:
6006 if (fold_builtin_next_arg (exp, false))
6008 return expand_builtin_next_arg ();
6010 case BUILT_IN_CLEAR_CACHE:
6011 target = expand_builtin___clear_cache (exp);
6016 case BUILT_IN_CLASSIFY_TYPE:
6017 return expand_builtin_classify_type (exp);
6019 case BUILT_IN_CONSTANT_P:
6022 case BUILT_IN_FRAME_ADDRESS:
6023 case BUILT_IN_RETURN_ADDRESS:
6024 return expand_builtin_frame_address (fndecl, exp);
6026 /* Returns the address of the area where the structure is returned.
6028 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6029 if (call_expr_nargs (exp) != 0
6030 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6031 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6034 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6036 case BUILT_IN_ALLOCA:
6037 case BUILT_IN_ALLOCA_WITH_ALIGN:
6038 /* If the allocation stems from the declaration of a variable-sized
6039 object, it cannot accumulate. */
6040 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6045 case BUILT_IN_STACK_SAVE:
6046 return expand_stack_save ();
6048 case BUILT_IN_STACK_RESTORE:
6049 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6052 case BUILT_IN_BSWAP32:
6053 case BUILT_IN_BSWAP64:
6054 target = expand_builtin_bswap (exp, target, subtarget);
6060 CASE_INT_FN (BUILT_IN_FFS):
6061 case BUILT_IN_FFSIMAX:
6062 target = expand_builtin_unop (target_mode, exp, target,
6063 subtarget, ffs_optab);
6068 CASE_INT_FN (BUILT_IN_CLZ):
6069 case BUILT_IN_CLZIMAX:
6070 target = expand_builtin_unop (target_mode, exp, target,
6071 subtarget, clz_optab);
6076 CASE_INT_FN (BUILT_IN_CTZ):
6077 case BUILT_IN_CTZIMAX:
6078 target = expand_builtin_unop (target_mode, exp, target,
6079 subtarget, ctz_optab);
6084 CASE_INT_FN (BUILT_IN_CLRSB):
6085 case BUILT_IN_CLRSBIMAX:
6086 target = expand_builtin_unop (target_mode, exp, target,
6087 subtarget, clrsb_optab);
6092 CASE_INT_FN (BUILT_IN_POPCOUNT):
6093 case BUILT_IN_POPCOUNTIMAX:
6094 target = expand_builtin_unop (target_mode, exp, target,
6095 subtarget, popcount_optab);
6100 CASE_INT_FN (BUILT_IN_PARITY):
6101 case BUILT_IN_PARITYIMAX:
6102 target = expand_builtin_unop (target_mode, exp, target,
6103 subtarget, parity_optab);
6108 case BUILT_IN_STRLEN:
6109 target = expand_builtin_strlen (exp, target, target_mode);
6114 case BUILT_IN_STRCPY:
6115 target = expand_builtin_strcpy (exp, target);
6120 case BUILT_IN_STRNCPY:
6121 target = expand_builtin_strncpy (exp, target);
6126 case BUILT_IN_STPCPY:
6127 target = expand_builtin_stpcpy (exp, target, mode);
6132 case BUILT_IN_MEMCPY:
6133 target = expand_builtin_memcpy (exp, target);
6138 case BUILT_IN_MEMPCPY:
6139 target = expand_builtin_mempcpy (exp, target, mode);
6144 case BUILT_IN_MEMSET:
6145 target = expand_builtin_memset (exp, target, mode);
6150 case BUILT_IN_BZERO:
6151 target = expand_builtin_bzero (exp);
6156 case BUILT_IN_STRCMP:
6157 target = expand_builtin_strcmp (exp, target);
6162 case BUILT_IN_STRNCMP:
6163 target = expand_builtin_strncmp (exp, target, mode);
6169 case BUILT_IN_MEMCMP:
6170 target = expand_builtin_memcmp (exp, target, mode);
6175 case BUILT_IN_SETJMP:
6176 /* This should have been lowered to the builtins below. */
6179 case BUILT_IN_SETJMP_SETUP:
6180 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6181 and the receiver label. */
6182 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6184 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6185 VOIDmode, EXPAND_NORMAL);
6186 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6187 rtx label_r = label_rtx (label);
6189 /* This is copied from the handling of non-local gotos. */
6190 expand_builtin_setjmp_setup (buf_addr, label_r);
6191 nonlocal_goto_handler_labels
6192 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6193 nonlocal_goto_handler_labels);
6194 /* ??? Do not let expand_label treat us as such since we would
6195 not want to be both on the list of non-local labels and on
6196 the list of forced labels. */
6197 FORCED_LABEL (label) = 0;
6202 case BUILT_IN_SETJMP_DISPATCHER:
6203 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6204 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6206 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6207 rtx label_r = label_rtx (label);
6209 /* Remove the dispatcher label from the list of non-local labels
6210 since the receiver labels have been added to it above. */
6211 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6216 case BUILT_IN_SETJMP_RECEIVER:
6217 /* __builtin_setjmp_receiver is passed the receiver label. */
6218 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6220 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6221 rtx label_r = label_rtx (label);
6223 expand_builtin_setjmp_receiver (label_r);
6228 /* __builtin_longjmp is passed a pointer to an array of five words.
6229 It's similar to the C library longjmp function but works with
6230 __builtin_setjmp above. */
6231 case BUILT_IN_LONGJMP:
6232 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6234 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6235 VOIDmode, EXPAND_NORMAL);
6236 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6238 if (value != const1_rtx)
6240 error ("%<__builtin_longjmp%> second argument must be 1");
6244 expand_builtin_longjmp (buf_addr, value);
6249 case BUILT_IN_NONLOCAL_GOTO:
6250 target = expand_builtin_nonlocal_goto (exp);
6255 /* This updates the setjmp buffer that is its argument with the value
6256 of the current stack pointer. */
6257 case BUILT_IN_UPDATE_SETJMP_BUF:
6258 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6261 = expand_normal (CALL_EXPR_ARG (exp, 0));
6263 expand_builtin_update_setjmp_buf (buf_addr);
6269 expand_builtin_trap ();
6272 case BUILT_IN_UNREACHABLE:
6273 expand_builtin_unreachable ();
6276 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6277 case BUILT_IN_SIGNBITD32:
6278 case BUILT_IN_SIGNBITD64:
6279 case BUILT_IN_SIGNBITD128:
6280 target = expand_builtin_signbit (exp, target);
6285 /* Various hooks for the DWARF 2 __throw routine. */
6286 case BUILT_IN_UNWIND_INIT:
6287 expand_builtin_unwind_init ();
6289 case BUILT_IN_DWARF_CFA:
6290 return virtual_cfa_rtx;
6291 #ifdef DWARF2_UNWIND_INFO
6292 case BUILT_IN_DWARF_SP_COLUMN:
6293 return expand_builtin_dwarf_sp_column ();
6294 case BUILT_IN_INIT_DWARF_REG_SIZES:
6295 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6298 case BUILT_IN_FROB_RETURN_ADDR:
6299 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6300 case BUILT_IN_EXTRACT_RETURN_ADDR:
6301 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6302 case BUILT_IN_EH_RETURN:
6303 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6304 CALL_EXPR_ARG (exp, 1));
6306 #ifdef EH_RETURN_DATA_REGNO
6307 case BUILT_IN_EH_RETURN_DATA_REGNO:
6308 return expand_builtin_eh_return_data_regno (exp);
6310 case BUILT_IN_EXTEND_POINTER:
6311 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6312 case BUILT_IN_EH_POINTER:
6313 return expand_builtin_eh_pointer (exp);
6314 case BUILT_IN_EH_FILTER:
6315 return expand_builtin_eh_filter (exp);
6316 case BUILT_IN_EH_COPY_VALUES:
6317 return expand_builtin_eh_copy_values (exp);
6319 case BUILT_IN_VA_START:
6320 return expand_builtin_va_start (exp);
6321 case BUILT_IN_VA_END:
6322 return expand_builtin_va_end (exp);
6323 case BUILT_IN_VA_COPY:
6324 return expand_builtin_va_copy (exp);
6325 case BUILT_IN_EXPECT:
6326 return expand_builtin_expect (exp, target);
6327 case BUILT_IN_ASSUME_ALIGNED:
6328 return expand_builtin_assume_aligned (exp, target);
6329 case BUILT_IN_PREFETCH:
6330 expand_builtin_prefetch (exp);
6333 case BUILT_IN_INIT_TRAMPOLINE:
6334 return expand_builtin_init_trampoline (exp, true);
6335 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
6336 return expand_builtin_init_trampoline (exp, false);
6337 case BUILT_IN_ADJUST_TRAMPOLINE:
6338 return expand_builtin_adjust_trampoline (exp);
6341 case BUILT_IN_EXECL:
6342 case BUILT_IN_EXECV:
6343 case BUILT_IN_EXECLP:
6344 case BUILT_IN_EXECLE:
6345 case BUILT_IN_EXECVP:
6346 case BUILT_IN_EXECVE:
6347 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6352 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6353 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6354 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6355 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6356 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6357 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6358 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6363 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6364 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6365 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6366 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6367 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6368 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6369 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6374 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6375 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6376 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6377 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6378 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6379 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6380 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6385 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6386 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6387 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6388 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6389 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6390 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6391 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6396 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6397 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6398 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6399 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6400 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6401 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6402 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6407 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6408 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6409 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6410 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6411 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6412 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6413 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6418 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6419 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6420 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6421 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6422 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6423 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6424 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6429 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6430 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6431 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6432 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6433 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6434 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6435 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6440 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6441 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6442 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6443 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6444 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6445 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6446 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6451 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6452 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6453 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6454 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6455 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6456 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6457 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6462 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6463 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6464 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6465 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6466 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6467 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6468 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6473 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6474 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6475 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6476 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6477 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6478 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6479 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6484 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6485 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6486 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6487 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6488 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6489 if (mode == VOIDmode)
6490 mode = TYPE_MODE (boolean_type_node);
6491 if (!target || !register_operand (target, mode))
6492 target = gen_reg_rtx (mode);
6494 mode = get_builtin_sync_mode
6495 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6496 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6501 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6502 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6503 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6504 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6505 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6506 mode = get_builtin_sync_mode
6507 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6508 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6513 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6514 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6515 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6516 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6517 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6518 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6519 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6524 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6525 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6526 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6527 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6528 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6529 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6530 expand_builtin_sync_lock_release (mode, exp);
6533 case BUILT_IN_SYNC_SYNCHRONIZE:
6534 expand_builtin_sync_synchronize ();
6537 case BUILT_IN_ATOMIC_EXCHANGE_1:
6538 case BUILT_IN_ATOMIC_EXCHANGE_2:
6539 case BUILT_IN_ATOMIC_EXCHANGE_4:
6540 case BUILT_IN_ATOMIC_EXCHANGE_8:
6541 case BUILT_IN_ATOMIC_EXCHANGE_16:
6542 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6543 target = expand_builtin_atomic_exchange (mode, exp, target);
6548 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6549 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6550 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6551 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6552 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6554 unsigned int nargs, z;
6558 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6559 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6563 /* If this is turned into an external library call, the weak parameter
6564 must be dropped to match the expected parameter list. */
6565 nargs = call_expr_nargs (exp);
6566 vec = VEC_alloc (tree, gc, nargs - 1);
6567 for (z = 0; z < 3; z++)
6568 VEC_quick_push (tree, vec, CALL_EXPR_ARG (exp, z));
6569 /* Skip the boolean weak parameter. */
6570 for (z = 4; z < 6; z++)
6571 VEC_quick_push (tree, vec, CALL_EXPR_ARG (exp, z));
6572 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
6576 case BUILT_IN_ATOMIC_LOAD_1:
6577 case BUILT_IN_ATOMIC_LOAD_2:
6578 case BUILT_IN_ATOMIC_LOAD_4:
6579 case BUILT_IN_ATOMIC_LOAD_8:
6580 case BUILT_IN_ATOMIC_LOAD_16:
6581 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6582 target = expand_builtin_atomic_load (mode, exp, target);
6587 case BUILT_IN_ATOMIC_STORE_1:
6588 case BUILT_IN_ATOMIC_STORE_2:
6589 case BUILT_IN_ATOMIC_STORE_4:
6590 case BUILT_IN_ATOMIC_STORE_8:
6591 case BUILT_IN_ATOMIC_STORE_16:
6592 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6593 target = expand_builtin_atomic_store (mode, exp);
6598 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6599 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6600 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6601 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6602 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6604 enum built_in_function lib;
6605 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6606 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6607 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6608 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6614 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6615 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6616 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6617 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6618 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6620 enum built_in_function lib;
6621 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6622 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6623 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6624 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6630 case BUILT_IN_ATOMIC_AND_FETCH_1:
6631 case BUILT_IN_ATOMIC_AND_FETCH_2:
6632 case BUILT_IN_ATOMIC_AND_FETCH_4:
6633 case BUILT_IN_ATOMIC_AND_FETCH_8:
6634 case BUILT_IN_ATOMIC_AND_FETCH_16:
6636 enum built_in_function lib;
6637 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6638 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6639 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6640 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6646 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6647 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6648 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6649 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6650 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6652 enum built_in_function lib;
6653 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6654 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6655 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6656 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6662 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6663 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6664 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6665 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6666 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6668 enum built_in_function lib;
6669 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6670 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6671 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6672 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6678 case BUILT_IN_ATOMIC_OR_FETCH_1:
6679 case BUILT_IN_ATOMIC_OR_FETCH_2:
6680 case BUILT_IN_ATOMIC_OR_FETCH_4:
6681 case BUILT_IN_ATOMIC_OR_FETCH_8:
6682 case BUILT_IN_ATOMIC_OR_FETCH_16:
6684 enum built_in_function lib;
6685 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6686 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6687 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6688 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6694 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6695 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6696 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6697 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6698 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6699 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6700 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6701 ignore, BUILT_IN_NONE);
6706 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6707 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6708 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6709 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6710 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6711 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6712 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6713 ignore, BUILT_IN_NONE);
6718 case BUILT_IN_ATOMIC_FETCH_AND_1:
6719 case BUILT_IN_ATOMIC_FETCH_AND_2:
6720 case BUILT_IN_ATOMIC_FETCH_AND_4:
6721 case BUILT_IN_ATOMIC_FETCH_AND_8:
6722 case BUILT_IN_ATOMIC_FETCH_AND_16:
6723 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6724 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6725 ignore, BUILT_IN_NONE);
6730 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6731 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6732 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6733 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6734 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6735 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6736 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6737 ignore, BUILT_IN_NONE);
6742 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6743 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6744 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6745 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6746 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6747 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6748 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6749 ignore, BUILT_IN_NONE);
6754 case BUILT_IN_ATOMIC_FETCH_OR_1:
6755 case BUILT_IN_ATOMIC_FETCH_OR_2:
6756 case BUILT_IN_ATOMIC_FETCH_OR_4:
6757 case BUILT_IN_ATOMIC_FETCH_OR_8:
6758 case BUILT_IN_ATOMIC_FETCH_OR_16:
6759 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6760 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6761 ignore, BUILT_IN_NONE);
6766 case BUILT_IN_ATOMIC_TEST_AND_SET:
6767 return expand_builtin_atomic_test_and_set (exp, target);
6769 case BUILT_IN_ATOMIC_CLEAR:
6770 return expand_builtin_atomic_clear (exp);
6772 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6773 return expand_builtin_atomic_always_lock_free (exp);
6775 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6776 target = expand_builtin_atomic_is_lock_free (exp);
6781 case BUILT_IN_ATOMIC_THREAD_FENCE:
6782 expand_builtin_atomic_thread_fence (exp);
6785 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6786 expand_builtin_atomic_signal_fence (exp);
6789 case BUILT_IN_OBJECT_SIZE:
6790 return expand_builtin_object_size (exp);
6792 case BUILT_IN_MEMCPY_CHK:
6793 case BUILT_IN_MEMPCPY_CHK:
6794 case BUILT_IN_MEMMOVE_CHK:
6795 case BUILT_IN_MEMSET_CHK:
6796 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6801 case BUILT_IN_STRCPY_CHK:
6802 case BUILT_IN_STPCPY_CHK:
6803 case BUILT_IN_STRNCPY_CHK:
6804 case BUILT_IN_STPNCPY_CHK:
6805 case BUILT_IN_STRCAT_CHK:
6806 case BUILT_IN_STRNCAT_CHK:
6807 case BUILT_IN_SNPRINTF_CHK:
6808 case BUILT_IN_VSNPRINTF_CHK:
6809 maybe_emit_chk_warning (exp, fcode);
6812 case BUILT_IN_SPRINTF_CHK:
6813 case BUILT_IN_VSPRINTF_CHK:
6814 maybe_emit_sprintf_chk_warning (exp, fcode);
6818 if (warn_free_nonheap_object)
6819 maybe_emit_free_warning (exp);
6822 default: /* just do library call, if unknown builtin */
6826 /* The switch statement above can drop through to cause the function
6827 to be called normally. */
6828 return expand_call (exp, target, ignore);
6831 /* Determine whether a tree node represents a call to a built-in
6832 function. If the tree T is a call to a built-in function with
6833 the right number of arguments of the appropriate types, return
6834 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6835 Otherwise the return value is END_BUILTINS. */
6837 enum built_in_function
6838 builtin_mathfn_code (const_tree t)
6840 const_tree fndecl, arg, parmlist;
6841 const_tree argtype, parmtype;
6842 const_call_expr_arg_iterator iter;
6844 if (TREE_CODE (t) != CALL_EXPR
6845 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6846 return END_BUILTINS;
6848 fndecl = get_callee_fndecl (t);
6849 if (fndecl == NULL_TREE
6850 || TREE_CODE (fndecl) != FUNCTION_DECL
6851 || ! DECL_BUILT_IN (fndecl)
6852 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6853 return END_BUILTINS;
6855 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6856 init_const_call_expr_arg_iterator (t, &iter);
6857 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6859 /* If a function doesn't take a variable number of arguments,
6860 the last element in the list will have type `void'. */
6861 parmtype = TREE_VALUE (parmlist);
6862 if (VOID_TYPE_P (parmtype))
6864 if (more_const_call_expr_args_p (&iter))
6865 return END_BUILTINS;
6866 return DECL_FUNCTION_CODE (fndecl);
6869 if (! more_const_call_expr_args_p (&iter))
6870 return END_BUILTINS;
6872 arg = next_const_call_expr_arg (&iter);
6873 argtype = TREE_TYPE (arg);
6875 if (SCALAR_FLOAT_TYPE_P (parmtype))
6877 if (! SCALAR_FLOAT_TYPE_P (argtype))
6878 return END_BUILTINS;
6880 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6882 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6883 return END_BUILTINS;
6885 else if (POINTER_TYPE_P (parmtype))
6887 if (! POINTER_TYPE_P (argtype))
6888 return END_BUILTINS;
6890 else if (INTEGRAL_TYPE_P (parmtype))
6892 if (! INTEGRAL_TYPE_P (argtype))
6893 return END_BUILTINS;
6896 return END_BUILTINS;
6899 /* Variable-length argument list. */
6900 return DECL_FUNCTION_CODE (fndecl);
6903 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6904 evaluate to a constant. */
6907 fold_builtin_constant_p (tree arg)
6909 /* We return 1 for a numeric type that's known to be a constant
6910 value at compile-time or for an aggregate type that's a
6911 literal constant. */
6914 /* If we know this is a constant, emit the constant of one. */
6915 if (CONSTANT_CLASS_P (arg)
6916 || (TREE_CODE (arg) == CONSTRUCTOR
6917 && TREE_CONSTANT (arg)))
6918 return integer_one_node;
6919 if (TREE_CODE (arg) == ADDR_EXPR)
6921 tree op = TREE_OPERAND (arg, 0);
6922 if (TREE_CODE (op) == STRING_CST
6923 || (TREE_CODE (op) == ARRAY_REF
6924 && integer_zerop (TREE_OPERAND (op, 1))
6925 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6926 return integer_one_node;
6929 /* If this expression has side effects, show we don't know it to be a
6930 constant. Likewise if it's a pointer or aggregate type since in
6931 those case we only want literals, since those are only optimized
6932 when generating RTL, not later.
6933 And finally, if we are compiling an initializer, not code, we
6934 need to return a definite result now; there's not going to be any
6935 more optimization done. */
6936 if (TREE_SIDE_EFFECTS (arg)
6937 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6938 || POINTER_TYPE_P (TREE_TYPE (arg))
6940 || folding_initializer)
6941 return integer_zero_node;
6946 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6947 return it as a truthvalue. */
6950 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6952 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6954 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6955 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6956 ret_type = TREE_TYPE (TREE_TYPE (fn));
6957 pred_type = TREE_VALUE (arg_types);
6958 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6960 pred = fold_convert_loc (loc, pred_type, pred);
6961 expected = fold_convert_loc (loc, expected_type, expected);
6962 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6964 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6965 build_int_cst (ret_type, 0));
6968 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6969 NULL_TREE if no simplification is possible. */
6972 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6974 tree inner, fndecl, inner_arg0;
6975 enum tree_code code;
6977 /* Distribute the expected value over short-circuiting operators.
6978 See through the cast from truthvalue_type_node to long. */
6980 while (TREE_CODE (inner_arg0) == NOP_EXPR
6981 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
6982 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
6983 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
6985 /* If this is a builtin_expect within a builtin_expect keep the
6986 inner one. See through a comparison against a constant. It
6987 might have been added to create a thruthvalue. */
6990 if (COMPARISON_CLASS_P (inner)
6991 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6992 inner = TREE_OPERAND (inner, 0);
6994 if (TREE_CODE (inner) == CALL_EXPR
6995 && (fndecl = get_callee_fndecl (inner))
6996 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6997 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7001 code = TREE_CODE (inner);
7002 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7004 tree op0 = TREE_OPERAND (inner, 0);
7005 tree op1 = TREE_OPERAND (inner, 1);
7007 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7008 op1 = build_builtin_expect_predicate (loc, op1, arg1);
7009 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7011 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7014 /* If the argument isn't invariant then there's nothing else we can do. */
7015 if (!TREE_CONSTANT (inner_arg0))
7018 /* If we expect that a comparison against the argument will fold to
7019 a constant return the constant. In practice, this means a true
7020 constant or the address of a non-weak symbol. */
7023 if (TREE_CODE (inner) == ADDR_EXPR)
7027 inner = TREE_OPERAND (inner, 0);
7029 while (TREE_CODE (inner) == COMPONENT_REF
7030 || TREE_CODE (inner) == ARRAY_REF);
7031 if ((TREE_CODE (inner) == VAR_DECL
7032 || TREE_CODE (inner) == FUNCTION_DECL)
7033 && DECL_WEAK (inner))
7037 /* Otherwise, ARG0 already has the proper type for the return value. */
7041 /* Fold a call to __builtin_classify_type with argument ARG. */
7044 fold_builtin_classify_type (tree arg)
7047 return build_int_cst (integer_type_node, no_type_class);
7049 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7052 /* Fold a call to __builtin_strlen with argument ARG. */
7055 fold_builtin_strlen (location_t loc, tree type, tree arg)
7057 if (!validate_arg (arg, POINTER_TYPE))
7061 tree len = c_strlen (arg, 0);
7064 return fold_convert_loc (loc, type, len);
7070 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7073 fold_builtin_inf (location_t loc, tree type, int warn)
7075 REAL_VALUE_TYPE real;
7077 /* __builtin_inff is intended to be usable to define INFINITY on all
7078 targets. If an infinity is not available, INFINITY expands "to a
7079 positive constant of type float that overflows at translation
7080 time", footnote "In this case, using INFINITY will violate the
7081 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7082 Thus we pedwarn to ensure this constraint violation is
7084 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7085 pedwarn (loc, 0, "target format does not support infinity");
7088 return build_real (type, real);
7091 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7094 fold_builtin_nan (tree arg, tree type, int quiet)
7096 REAL_VALUE_TYPE real;
7099 if (!validate_arg (arg, POINTER_TYPE))
7101 str = c_getstr (arg);
7105 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7108 return build_real (type, real);
7111 /* Return true if the floating point expression T has an integer value.
7112 We also allow +Inf, -Inf and NaN to be considered integer values. */
7115 integer_valued_real_p (tree t)
7117 switch (TREE_CODE (t))
7124 return integer_valued_real_p (TREE_OPERAND (t, 0));
7129 return integer_valued_real_p (TREE_OPERAND (t, 1));
7136 return integer_valued_real_p (TREE_OPERAND (t, 0))
7137 && integer_valued_real_p (TREE_OPERAND (t, 1));
7140 return integer_valued_real_p (TREE_OPERAND (t, 1))
7141 && integer_valued_real_p (TREE_OPERAND (t, 2));
7144 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7148 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7149 if (TREE_CODE (type) == INTEGER_TYPE)
7151 if (TREE_CODE (type) == REAL_TYPE)
7152 return integer_valued_real_p (TREE_OPERAND (t, 0));
7157 switch (builtin_mathfn_code (t))
7159 CASE_FLT_FN (BUILT_IN_CEIL):
7160 CASE_FLT_FN (BUILT_IN_FLOOR):
7161 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7162 CASE_FLT_FN (BUILT_IN_RINT):
7163 CASE_FLT_FN (BUILT_IN_ROUND):
7164 CASE_FLT_FN (BUILT_IN_TRUNC):
7167 CASE_FLT_FN (BUILT_IN_FMIN):
7168 CASE_FLT_FN (BUILT_IN_FMAX):
7169 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7170 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7183 /* FNDECL is assumed to be a builtin where truncation can be propagated
7184 across (for instance floor((double)f) == (double)floorf (f).
7185 Do the transformation for a call with argument ARG. */
7188 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7190 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7192 if (!validate_arg (arg, REAL_TYPE))
7195 /* Integer rounding functions are idempotent. */
7196 if (fcode == builtin_mathfn_code (arg))
7199 /* If argument is already integer valued, and we don't need to worry
7200 about setting errno, there's no need to perform rounding. */
7201 if (! flag_errno_math && integer_valued_real_p (arg))
7206 tree arg0 = strip_float_extensions (arg);
7207 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7208 tree newtype = TREE_TYPE (arg0);
7211 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7212 && (decl = mathfn_built_in (newtype, fcode)))
7213 return fold_convert_loc (loc, ftype,
7214 build_call_expr_loc (loc, decl, 1,
7215 fold_convert_loc (loc,
7222 /* FNDECL is assumed to be builtin which can narrow the FP type of
7223 the argument, for instance lround((double)f) -> lroundf (f).
7224 Do the transformation for a call with argument ARG. */
7227 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7229 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7231 if (!validate_arg (arg, REAL_TYPE))
7234 /* If argument is already integer valued, and we don't need to worry
7235 about setting errno, there's no need to perform rounding. */
7236 if (! flag_errno_math && integer_valued_real_p (arg))
7237 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7238 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7242 tree ftype = TREE_TYPE (arg);
7243 tree arg0 = strip_float_extensions (arg);
7244 tree newtype = TREE_TYPE (arg0);
7247 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7248 && (decl = mathfn_built_in (newtype, fcode)))
7249 return build_call_expr_loc (loc, decl, 1,
7250 fold_convert_loc (loc, newtype, arg0));
7253 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7254 sizeof (int) == sizeof (long). */
7255 if (TYPE_PRECISION (integer_type_node)
7256 == TYPE_PRECISION (long_integer_type_node))
7258 tree newfn = NULL_TREE;
7261 CASE_FLT_FN (BUILT_IN_ICEIL):
7262 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7265 CASE_FLT_FN (BUILT_IN_IFLOOR):
7266 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7269 CASE_FLT_FN (BUILT_IN_IROUND):
7270 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7273 CASE_FLT_FN (BUILT_IN_IRINT):
7274 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7283 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7284 return fold_convert_loc (loc,
7285 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7289 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7290 sizeof (long long) == sizeof (long). */
7291 if (TYPE_PRECISION (long_long_integer_type_node)
7292 == TYPE_PRECISION (long_integer_type_node))
7294 tree newfn = NULL_TREE;
7297 CASE_FLT_FN (BUILT_IN_LLCEIL):
7298 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7301 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7302 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7305 CASE_FLT_FN (BUILT_IN_LLROUND):
7306 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7309 CASE_FLT_FN (BUILT_IN_LLRINT):
7310 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7319 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7320 return fold_convert_loc (loc,
7321 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7328 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7329 return type. Return NULL_TREE if no simplification can be made. */
7332 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7336 if (!validate_arg (arg, COMPLEX_TYPE)
7337 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7340 /* Calculate the result when the argument is a constant. */
7341 if (TREE_CODE (arg) == COMPLEX_CST
7342 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7346 if (TREE_CODE (arg) == COMPLEX_EXPR)
7348 tree real = TREE_OPERAND (arg, 0);
7349 tree imag = TREE_OPERAND (arg, 1);
7351 /* If either part is zero, cabs is fabs of the other. */
7352 if (real_zerop (real))
7353 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7354 if (real_zerop (imag))
7355 return fold_build1_loc (loc, ABS_EXPR, type, real);
7357 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7358 if (flag_unsafe_math_optimizations
7359 && operand_equal_p (real, imag, OEP_PURE_SAME))
7361 const REAL_VALUE_TYPE sqrt2_trunc
7362 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7364 return fold_build2_loc (loc, MULT_EXPR, type,
7365 fold_build1_loc (loc, ABS_EXPR, type, real),
7366 build_real (type, sqrt2_trunc));
7370 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7371 if (TREE_CODE (arg) == NEGATE_EXPR
7372 || TREE_CODE (arg) == CONJ_EXPR)
7373 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7375 /* Don't do this when optimizing for size. */
7376 if (flag_unsafe_math_optimizations
7377 && optimize && optimize_function_for_speed_p (cfun))
7379 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7381 if (sqrtfn != NULL_TREE)
7383 tree rpart, ipart, result;
7385 arg = builtin_save_expr (arg);
7387 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7388 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7390 rpart = builtin_save_expr (rpart);
7391 ipart = builtin_save_expr (ipart);
7393 result = fold_build2_loc (loc, PLUS_EXPR, type,
7394 fold_build2_loc (loc, MULT_EXPR, type,
7396 fold_build2_loc (loc, MULT_EXPR, type,
7399 return build_call_expr_loc (loc, sqrtfn, 1, result);
7406 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7407 complex tree type of the result. If NEG is true, the imaginary
7408 zero is negative. */
7411 build_complex_cproj (tree type, bool neg)
7413 REAL_VALUE_TYPE rinf, rzero = dconst0;
7417 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7418 build_real (TREE_TYPE (type), rzero));
7421 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7422 return type. Return NULL_TREE if no simplification can be made. */
7425 fold_builtin_cproj (location_t loc, tree arg, tree type)
7427 if (!validate_arg (arg, COMPLEX_TYPE)
7428 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7431 /* If there are no infinities, return arg. */
7432 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7433 return non_lvalue_loc (loc, arg);
7435 /* Calculate the result when the argument is a constant. */
7436 if (TREE_CODE (arg) == COMPLEX_CST)
7438 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7439 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7441 if (real_isinf (real) || real_isinf (imag))
7442 return build_complex_cproj (type, imag->sign);
7446 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7448 tree real = TREE_OPERAND (arg, 0);
7449 tree imag = TREE_OPERAND (arg, 1);
7454 /* If the real part is inf and the imag part is known to be
7455 nonnegative, return (inf + 0i). Remember side-effects are
7456 possible in the imag part. */
7457 if (TREE_CODE (real) == REAL_CST
7458 && real_isinf (TREE_REAL_CST_PTR (real))
7459 && tree_expr_nonnegative_p (imag))
7460 return omit_one_operand_loc (loc, type,
7461 build_complex_cproj (type, false),
7464 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7465 Remember side-effects are possible in the real part. */
7466 if (TREE_CODE (imag) == REAL_CST
7467 && real_isinf (TREE_REAL_CST_PTR (imag)))
7469 omit_one_operand_loc (loc, type,
7470 build_complex_cproj (type, TREE_REAL_CST_PTR
7471 (imag)->sign), arg);
7477 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7478 Return NULL_TREE if no simplification can be made. */
7481 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7484 enum built_in_function fcode;
7487 if (!validate_arg (arg, REAL_TYPE))
7490 /* Calculate the result when the argument is a constant. */
7491 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7494 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7495 fcode = builtin_mathfn_code (arg);
7496 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7498 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7499 arg = fold_build2_loc (loc, MULT_EXPR, type,
7500 CALL_EXPR_ARG (arg, 0),
7501 build_real (type, dconsthalf));
7502 return build_call_expr_loc (loc, expfn, 1, arg);
7505 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7506 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7508 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7512 tree arg0 = CALL_EXPR_ARG (arg, 0);
7514 /* The inner root was either sqrt or cbrt. */
7515 /* This was a conditional expression but it triggered a bug
7517 REAL_VALUE_TYPE dconstroot;
7518 if (BUILTIN_SQRT_P (fcode))
7519 dconstroot = dconsthalf;
7521 dconstroot = dconst_third ();
7523 /* Adjust for the outer root. */
7524 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7525 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7526 tree_root = build_real (type, dconstroot);
7527 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7531 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7532 if (flag_unsafe_math_optimizations
7533 && (fcode == BUILT_IN_POW
7534 || fcode == BUILT_IN_POWF
7535 || fcode == BUILT_IN_POWL))
7537 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7538 tree arg0 = CALL_EXPR_ARG (arg, 0);
7539 tree arg1 = CALL_EXPR_ARG (arg, 1);
7541 if (!tree_expr_nonnegative_p (arg0))
7542 arg0 = build1 (ABS_EXPR, type, arg0);
7543 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7544 build_real (type, dconsthalf));
7545 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7551 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7552 Return NULL_TREE if no simplification can be made. */
7555 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7557 const enum built_in_function fcode = builtin_mathfn_code (arg);
7560 if (!validate_arg (arg, REAL_TYPE))
7563 /* Calculate the result when the argument is a constant. */
7564 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7567 if (flag_unsafe_math_optimizations)
7569 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7570 if (BUILTIN_EXPONENT_P (fcode))
7572 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7573 const REAL_VALUE_TYPE third_trunc =
7574 real_value_truncate (TYPE_MODE (type), dconst_third ());
7575 arg = fold_build2_loc (loc, MULT_EXPR, type,
7576 CALL_EXPR_ARG (arg, 0),
7577 build_real (type, third_trunc));
7578 return build_call_expr_loc (loc, expfn, 1, arg);
7581 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7582 if (BUILTIN_SQRT_P (fcode))
7584 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7588 tree arg0 = CALL_EXPR_ARG (arg, 0);
7590 REAL_VALUE_TYPE dconstroot = dconst_third ();
7592 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7593 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7594 tree_root = build_real (type, dconstroot);
7595 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7599 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7600 if (BUILTIN_CBRT_P (fcode))
7602 tree arg0 = CALL_EXPR_ARG (arg, 0);
7603 if (tree_expr_nonnegative_p (arg0))
7605 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7610 REAL_VALUE_TYPE dconstroot;
7612 real_arithmetic (&dconstroot, MULT_EXPR,
7613 dconst_third_ptr (), dconst_third_ptr ());
7614 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7615 tree_root = build_real (type, dconstroot);
7616 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7621 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7622 if (fcode == BUILT_IN_POW
7623 || fcode == BUILT_IN_POWF
7624 || fcode == BUILT_IN_POWL)
7626 tree arg00 = CALL_EXPR_ARG (arg, 0);
7627 tree arg01 = CALL_EXPR_ARG (arg, 1);
7628 if (tree_expr_nonnegative_p (arg00))
7630 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7631 const REAL_VALUE_TYPE dconstroot
7632 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7633 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7634 build_real (type, dconstroot));
7635 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7642 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7643 TYPE is the type of the return value. Return NULL_TREE if no
7644 simplification can be made. */
7647 fold_builtin_cos (location_t loc,
7648 tree arg, tree type, tree fndecl)
7652 if (!validate_arg (arg, REAL_TYPE))
7655 /* Calculate the result when the argument is a constant. */
7656 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7659 /* Optimize cos(-x) into cos (x). */
7660 if ((narg = fold_strip_sign_ops (arg)))
7661 return build_call_expr_loc (loc, fndecl, 1, narg);
7666 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7667 Return NULL_TREE if no simplification can be made. */
7670 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7672 if (validate_arg (arg, REAL_TYPE))
7676 /* Calculate the result when the argument is a constant. */
7677 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7680 /* Optimize cosh(-x) into cosh (x). */
7681 if ((narg = fold_strip_sign_ops (arg)))
7682 return build_call_expr_loc (loc, fndecl, 1, narg);
7688 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7689 argument ARG. TYPE is the type of the return value. Return
7690 NULL_TREE if no simplification can be made. */
7693 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7696 if (validate_arg (arg, COMPLEX_TYPE)
7697 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7701 /* Calculate the result when the argument is a constant. */
7702 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7705 /* Optimize fn(-x) into fn(x). */
7706 if ((tmp = fold_strip_sign_ops (arg)))
7707 return build_call_expr_loc (loc, fndecl, 1, tmp);
7713 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7714 Return NULL_TREE if no simplification can be made. */
7717 fold_builtin_tan (tree arg, tree type)
7719 enum built_in_function fcode;
7722 if (!validate_arg (arg, REAL_TYPE))
7725 /* Calculate the result when the argument is a constant. */
7726 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7729 /* Optimize tan(atan(x)) = x. */
7730 fcode = builtin_mathfn_code (arg);
7731 if (flag_unsafe_math_optimizations
7732 && (fcode == BUILT_IN_ATAN
7733 || fcode == BUILT_IN_ATANF
7734 || fcode == BUILT_IN_ATANL))
7735 return CALL_EXPR_ARG (arg, 0);
7740 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7741 NULL_TREE if no simplification can be made. */
7744 fold_builtin_sincos (location_t loc,
7745 tree arg0, tree arg1, tree arg2)
7750 if (!validate_arg (arg0, REAL_TYPE)
7751 || !validate_arg (arg1, POINTER_TYPE)
7752 || !validate_arg (arg2, POINTER_TYPE))
7755 type = TREE_TYPE (arg0);
7757 /* Calculate the result when the argument is a constant. */
7758 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7761 /* Canonicalize sincos to cexpi. */
7762 if (!TARGET_C99_FUNCTIONS)
7764 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7768 call = build_call_expr_loc (loc, fn, 1, arg0);
7769 call = builtin_save_expr (call);
7771 return build2 (COMPOUND_EXPR, void_type_node,
7772 build2 (MODIFY_EXPR, void_type_node,
7773 build_fold_indirect_ref_loc (loc, arg1),
7774 build1 (IMAGPART_EXPR, type, call)),
7775 build2 (MODIFY_EXPR, void_type_node,
7776 build_fold_indirect_ref_loc (loc, arg2),
7777 build1 (REALPART_EXPR, type, call)));
7780 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7781 NULL_TREE if no simplification can be made. */
7784 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7787 tree realp, imagp, ifn;
7790 if (!validate_arg (arg0, COMPLEX_TYPE)
7791 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7794 /* Calculate the result when the argument is a constant. */
7795 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7798 rtype = TREE_TYPE (TREE_TYPE (arg0));
7800 /* In case we can figure out the real part of arg0 and it is constant zero
7802 if (!TARGET_C99_FUNCTIONS)
7804 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7808 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7809 && real_zerop (realp))
7811 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7812 return build_call_expr_loc (loc, ifn, 1, narg);
7815 /* In case we can easily decompose real and imaginary parts split cexp
7816 to exp (r) * cexpi (i). */
7817 if (flag_unsafe_math_optimizations
7820 tree rfn, rcall, icall;
7822 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7826 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7830 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7831 icall = builtin_save_expr (icall);
7832 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7833 rcall = builtin_save_expr (rcall);
7834 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7835 fold_build2_loc (loc, MULT_EXPR, rtype,
7837 fold_build1_loc (loc, REALPART_EXPR,
7839 fold_build2_loc (loc, MULT_EXPR, rtype,
7841 fold_build1_loc (loc, IMAGPART_EXPR,
7848 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7849 Return NULL_TREE if no simplification can be made. */
7852 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7854 if (!validate_arg (arg, REAL_TYPE))
7857 /* Optimize trunc of constant value. */
7858 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7860 REAL_VALUE_TYPE r, x;
7861 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7863 x = TREE_REAL_CST (arg);
7864 real_trunc (&r, TYPE_MODE (type), &x);
7865 return build_real (type, r);
7868 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7871 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7872 Return NULL_TREE if no simplification can be made. */
7875 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7877 if (!validate_arg (arg, REAL_TYPE))
7880 /* Optimize floor of constant value. */
7881 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7885 x = TREE_REAL_CST (arg);
7886 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7888 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7891 real_floor (&r, TYPE_MODE (type), &x);
7892 return build_real (type, r);
7896 /* Fold floor (x) where x is nonnegative to trunc (x). */
7897 if (tree_expr_nonnegative_p (arg))
7899 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7901 return build_call_expr_loc (loc, truncfn, 1, arg);
7904 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7907 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7908 Return NULL_TREE if no simplification can be made. */
7911 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7913 if (!validate_arg (arg, REAL_TYPE))
7916 /* Optimize ceil of constant value. */
7917 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7921 x = TREE_REAL_CST (arg);
7922 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7924 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7927 real_ceil (&r, TYPE_MODE (type), &x);
7928 return build_real (type, r);
7932 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7935 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7936 Return NULL_TREE if no simplification can be made. */
7939 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7941 if (!validate_arg (arg, REAL_TYPE))
7944 /* Optimize round of constant value. */
7945 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7949 x = TREE_REAL_CST (arg);
7950 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7952 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7955 real_round (&r, TYPE_MODE (type), &x);
7956 return build_real (type, r);
7960 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7963 /* Fold function call to builtin lround, lroundf or lroundl (or the
7964 corresponding long long versions) and other rounding functions. ARG
7965 is the argument to the call. Return NULL_TREE if no simplification
7969 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7971 if (!validate_arg (arg, REAL_TYPE))
7974 /* Optimize lround of constant value. */
7975 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7977 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7979 if (real_isfinite (&x))
7981 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7982 tree ftype = TREE_TYPE (arg);
7986 switch (DECL_FUNCTION_CODE (fndecl))
7988 CASE_FLT_FN (BUILT_IN_IFLOOR):
7989 CASE_FLT_FN (BUILT_IN_LFLOOR):
7990 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7991 real_floor (&r, TYPE_MODE (ftype), &x);
7994 CASE_FLT_FN (BUILT_IN_ICEIL):
7995 CASE_FLT_FN (BUILT_IN_LCEIL):
7996 CASE_FLT_FN (BUILT_IN_LLCEIL):
7997 real_ceil (&r, TYPE_MODE (ftype), &x);
8000 CASE_FLT_FN (BUILT_IN_IROUND):
8001 CASE_FLT_FN (BUILT_IN_LROUND):
8002 CASE_FLT_FN (BUILT_IN_LLROUND):
8003 real_round (&r, TYPE_MODE (ftype), &x);
8010 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8011 if (double_int_fits_to_tree_p (itype, val))
8012 return double_int_to_tree (itype, val);
8016 switch (DECL_FUNCTION_CODE (fndecl))
8018 CASE_FLT_FN (BUILT_IN_LFLOOR):
8019 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8020 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8021 if (tree_expr_nonnegative_p (arg))
8022 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8023 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8028 return fold_fixed_mathfn (loc, fndecl, arg);
8031 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8032 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8033 the argument to the call. Return NULL_TREE if no simplification can
8037 fold_builtin_bitop (tree fndecl, tree arg)
8039 if (!validate_arg (arg, INTEGER_TYPE))
8042 /* Optimize for constant argument. */
8043 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8045 HOST_WIDE_INT hi, width, result;
8046 unsigned HOST_WIDE_INT lo;
8049 type = TREE_TYPE (arg);
8050 width = TYPE_PRECISION (type);
8051 lo = TREE_INT_CST_LOW (arg);
8053 /* Clear all the bits that are beyond the type's precision. */
8054 if (width > HOST_BITS_PER_WIDE_INT)
8056 hi = TREE_INT_CST_HIGH (arg);
8057 if (width < 2 * HOST_BITS_PER_WIDE_INT)
8058 hi &= ~((unsigned HOST_WIDE_INT) (-1)
8059 << (width - HOST_BITS_PER_WIDE_INT));
8064 if (width < HOST_BITS_PER_WIDE_INT)
8065 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8068 switch (DECL_FUNCTION_CODE (fndecl))
8070 CASE_INT_FN (BUILT_IN_FFS):
8072 result = ffs_hwi (lo);
8074 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8079 CASE_INT_FN (BUILT_IN_CLZ):
8081 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8083 result = width - floor_log2 (lo) - 1;
8084 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8088 CASE_INT_FN (BUILT_IN_CTZ):
8090 result = ctz_hwi (lo);
8092 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8093 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8097 CASE_INT_FN (BUILT_IN_CLRSB):
8098 if (width > HOST_BITS_PER_WIDE_INT
8099 && (hi & ((unsigned HOST_WIDE_INT) 1
8100 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8102 hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
8103 << (width - HOST_BITS_PER_WIDE_INT - 1));
8106 else if (width <= HOST_BITS_PER_WIDE_INT
8107 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8108 lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
8110 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8112 result = width - floor_log2 (lo) - 2;
8117 CASE_INT_FN (BUILT_IN_POPCOUNT):
8120 result++, lo &= lo - 1;
8122 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8125 CASE_INT_FN (BUILT_IN_PARITY):
8128 result++, lo &= lo - 1;
8130 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8138 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8144 /* Fold function call to builtin_bswap and the long and long long
8145 variants. Return NULL_TREE if no simplification can be made. */
8147 fold_builtin_bswap (tree fndecl, tree arg)
8149 if (! validate_arg (arg, INTEGER_TYPE))
8152 /* Optimize constant value. */
8153 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8155 HOST_WIDE_INT hi, width, r_hi = 0;
8156 unsigned HOST_WIDE_INT lo, r_lo = 0;
8159 type = TREE_TYPE (arg);
8160 width = TYPE_PRECISION (type);
8161 lo = TREE_INT_CST_LOW (arg);
8162 hi = TREE_INT_CST_HIGH (arg);
8164 switch (DECL_FUNCTION_CODE (fndecl))
8166 case BUILT_IN_BSWAP32:
8167 case BUILT_IN_BSWAP64:
8171 for (s = 0; s < width; s += 8)
8173 int d = width - s - 8;
8174 unsigned HOST_WIDE_INT byte;
8176 if (s < HOST_BITS_PER_WIDE_INT)
8177 byte = (lo >> s) & 0xff;
8179 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8181 if (d < HOST_BITS_PER_WIDE_INT)
8184 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8194 if (width < HOST_BITS_PER_WIDE_INT)
8195 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
8197 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
8203 /* A subroutine of fold_builtin to fold the various logarithmic
8204 functions. Return NULL_TREE if no simplification can me made.
8205 FUNC is the corresponding MPFR logarithm function. */
8208 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8209 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8211 if (validate_arg (arg, REAL_TYPE))
8213 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8215 const enum built_in_function fcode = builtin_mathfn_code (arg);
8217 /* Calculate the result when the argument is a constant. */
8218 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8221 /* Special case, optimize logN(expN(x)) = x. */
8222 if (flag_unsafe_math_optimizations
8223 && ((func == mpfr_log
8224 && (fcode == BUILT_IN_EXP
8225 || fcode == BUILT_IN_EXPF
8226 || fcode == BUILT_IN_EXPL))
8227 || (func == mpfr_log2
8228 && (fcode == BUILT_IN_EXP2
8229 || fcode == BUILT_IN_EXP2F
8230 || fcode == BUILT_IN_EXP2L))
8231 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8232 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8234 /* Optimize logN(func()) for various exponential functions. We
8235 want to determine the value "x" and the power "exponent" in
8236 order to transform logN(x**exponent) into exponent*logN(x). */
8237 if (flag_unsafe_math_optimizations)
8239 tree exponent = 0, x = 0;
8243 CASE_FLT_FN (BUILT_IN_EXP):
8244 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8245 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8247 exponent = CALL_EXPR_ARG (arg, 0);
8249 CASE_FLT_FN (BUILT_IN_EXP2):
8250 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8251 x = build_real (type, dconst2);
8252 exponent = CALL_EXPR_ARG (arg, 0);
8254 CASE_FLT_FN (BUILT_IN_EXP10):
8255 CASE_FLT_FN (BUILT_IN_POW10):
8256 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8258 REAL_VALUE_TYPE dconst10;
8259 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8260 x = build_real (type, dconst10);
8262 exponent = CALL_EXPR_ARG (arg, 0);
8264 CASE_FLT_FN (BUILT_IN_SQRT):
8265 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8266 x = CALL_EXPR_ARG (arg, 0);
8267 exponent = build_real (type, dconsthalf);
8269 CASE_FLT_FN (BUILT_IN_CBRT):
8270 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8271 x = CALL_EXPR_ARG (arg, 0);
8272 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8275 CASE_FLT_FN (BUILT_IN_POW):
8276 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8277 x = CALL_EXPR_ARG (arg, 0);
8278 exponent = CALL_EXPR_ARG (arg, 1);
8284 /* Now perform the optimization. */
8287 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8288 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8296 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8297 NULL_TREE if no simplification can be made. */
8300 fold_builtin_hypot (location_t loc, tree fndecl,
8301 tree arg0, tree arg1, tree type)
8303 tree res, narg0, narg1;
8305 if (!validate_arg (arg0, REAL_TYPE)
8306 || !validate_arg (arg1, REAL_TYPE))
8309 /* Calculate the result when the argument is a constant. */
8310 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8313 /* If either argument to hypot has a negate or abs, strip that off.
8314 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8315 narg0 = fold_strip_sign_ops (arg0);
8316 narg1 = fold_strip_sign_ops (arg1);
8319 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8320 narg1 ? narg1 : arg1);
8323 /* If either argument is zero, hypot is fabs of the other. */
8324 if (real_zerop (arg0))
8325 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8326 else if (real_zerop (arg1))
8327 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8329 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8330 if (flag_unsafe_math_optimizations
8331 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8333 const REAL_VALUE_TYPE sqrt2_trunc
8334 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8335 return fold_build2_loc (loc, MULT_EXPR, type,
8336 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8337 build_real (type, sqrt2_trunc));
8344 /* Fold a builtin function call to pow, powf, or powl. Return
8345 NULL_TREE if no simplification can be made. */
8347 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8351 if (!validate_arg (arg0, REAL_TYPE)
8352 || !validate_arg (arg1, REAL_TYPE))
8355 /* Calculate the result when the argument is a constant. */
8356 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8359 /* Optimize pow(1.0,y) = 1.0. */
8360 if (real_onep (arg0))
8361 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8363 if (TREE_CODE (arg1) == REAL_CST
8364 && !TREE_OVERFLOW (arg1))
8366 REAL_VALUE_TYPE cint;
8370 c = TREE_REAL_CST (arg1);
8372 /* Optimize pow(x,0.0) = 1.0. */
8373 if (REAL_VALUES_EQUAL (c, dconst0))
8374 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8377 /* Optimize pow(x,1.0) = x. */
8378 if (REAL_VALUES_EQUAL (c, dconst1))
8381 /* Optimize pow(x,-1.0) = 1.0/x. */
8382 if (REAL_VALUES_EQUAL (c, dconstm1))
8383 return fold_build2_loc (loc, RDIV_EXPR, type,
8384 build_real (type, dconst1), arg0);
8386 /* Optimize pow(x,0.5) = sqrt(x). */
8387 if (flag_unsafe_math_optimizations
8388 && REAL_VALUES_EQUAL (c, dconsthalf))
8390 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8392 if (sqrtfn != NULL_TREE)
8393 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8396 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8397 if (flag_unsafe_math_optimizations)
8399 const REAL_VALUE_TYPE dconstroot
8400 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8402 if (REAL_VALUES_EQUAL (c, dconstroot))
8404 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8405 if (cbrtfn != NULL_TREE)
8406 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8410 /* Check for an integer exponent. */
8411 n = real_to_integer (&c);
8412 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8413 if (real_identical (&c, &cint))
8415 /* Attempt to evaluate pow at compile-time, unless this should
8416 raise an exception. */
8417 if (TREE_CODE (arg0) == REAL_CST
8418 && !TREE_OVERFLOW (arg0)
8420 || (!flag_trapping_math && !flag_errno_math)
8421 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8426 x = TREE_REAL_CST (arg0);
8427 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8428 if (flag_unsafe_math_optimizations || !inexact)
8429 return build_real (type, x);
8432 /* Strip sign ops from even integer powers. */
8433 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8435 tree narg0 = fold_strip_sign_ops (arg0);
8437 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8442 if (flag_unsafe_math_optimizations)
8444 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8446 /* Optimize pow(expN(x),y) = expN(x*y). */
8447 if (BUILTIN_EXPONENT_P (fcode))
8449 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8450 tree arg = CALL_EXPR_ARG (arg0, 0);
8451 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8452 return build_call_expr_loc (loc, expfn, 1, arg);
8455 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8456 if (BUILTIN_SQRT_P (fcode))
8458 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8459 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8460 build_real (type, dconsthalf));
8461 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8464 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8465 if (BUILTIN_CBRT_P (fcode))
8467 tree arg = CALL_EXPR_ARG (arg0, 0);
8468 if (tree_expr_nonnegative_p (arg))
8470 const REAL_VALUE_TYPE dconstroot
8471 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8472 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8473 build_real (type, dconstroot));
8474 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8478 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8479 if (fcode == BUILT_IN_POW
8480 || fcode == BUILT_IN_POWF
8481 || fcode == BUILT_IN_POWL)
8483 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8484 if (tree_expr_nonnegative_p (arg00))
8486 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8487 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8488 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8496 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8497 Return NULL_TREE if no simplification can be made. */
8499 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8500 tree arg0, tree arg1, tree type)
8502 if (!validate_arg (arg0, REAL_TYPE)
8503 || !validate_arg (arg1, INTEGER_TYPE))
8506 /* Optimize pow(1.0,y) = 1.0. */
8507 if (real_onep (arg0))
8508 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8510 if (host_integerp (arg1, 0))
8512 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8514 /* Evaluate powi at compile-time. */
8515 if (TREE_CODE (arg0) == REAL_CST
8516 && !TREE_OVERFLOW (arg0))
8519 x = TREE_REAL_CST (arg0);
8520 real_powi (&x, TYPE_MODE (type), &x, c);
8521 return build_real (type, x);
8524 /* Optimize pow(x,0) = 1.0. */
8526 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8529 /* Optimize pow(x,1) = x. */
8533 /* Optimize pow(x,-1) = 1.0/x. */
8535 return fold_build2_loc (loc, RDIV_EXPR, type,
8536 build_real (type, dconst1), arg0);
8542 /* A subroutine of fold_builtin to fold the various exponent
8543 functions. Return NULL_TREE if no simplification can be made.
8544 FUNC is the corresponding MPFR exponent function. */
8547 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8548 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8550 if (validate_arg (arg, REAL_TYPE))
8552 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8555 /* Calculate the result when the argument is a constant. */
8556 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8559 /* Optimize expN(logN(x)) = x. */
8560 if (flag_unsafe_math_optimizations)
8562 const enum built_in_function fcode = builtin_mathfn_code (arg);
8564 if ((func == mpfr_exp
8565 && (fcode == BUILT_IN_LOG
8566 || fcode == BUILT_IN_LOGF
8567 || fcode == BUILT_IN_LOGL))
8568 || (func == mpfr_exp2
8569 && (fcode == BUILT_IN_LOG2
8570 || fcode == BUILT_IN_LOG2F
8571 || fcode == BUILT_IN_LOG2L))
8572 || (func == mpfr_exp10
8573 && (fcode == BUILT_IN_LOG10
8574 || fcode == BUILT_IN_LOG10F
8575 || fcode == BUILT_IN_LOG10L)))
8576 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8583 /* Return true if VAR is a VAR_DECL or a component thereof. */
8586 var_decl_component_p (tree var)
8589 while (handled_component_p (inner))
8590 inner = TREE_OPERAND (inner, 0);
8591 return SSA_VAR_P (inner);
8594 /* Fold function call to builtin memset. Return
8595 NULL_TREE if no simplification can be made. */
8598 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8599 tree type, bool ignore)
8601 tree var, ret, etype;
8602 unsigned HOST_WIDE_INT length, cval;
8604 if (! validate_arg (dest, POINTER_TYPE)
8605 || ! validate_arg (c, INTEGER_TYPE)
8606 || ! validate_arg (len, INTEGER_TYPE))
8609 if (! host_integerp (len, 1))
8612 /* If the LEN parameter is zero, return DEST. */
8613 if (integer_zerop (len))
8614 return omit_one_operand_loc (loc, type, dest, c);
8616 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8621 if (TREE_CODE (var) != ADDR_EXPR)
8624 var = TREE_OPERAND (var, 0);
8625 if (TREE_THIS_VOLATILE (var))
8628 etype = TREE_TYPE (var);
8629 if (TREE_CODE (etype) == ARRAY_TYPE)
8630 etype = TREE_TYPE (etype);
8632 if (!INTEGRAL_TYPE_P (etype)
8633 && !POINTER_TYPE_P (etype))
8636 if (! var_decl_component_p (var))
8639 length = tree_low_cst (len, 1);
8640 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8641 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8644 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8647 if (integer_zerop (c))
8651 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8654 cval = TREE_INT_CST_LOW (c);
8658 cval |= (cval << 31) << 1;
8661 ret = build_int_cst_type (etype, cval);
8662 var = build_fold_indirect_ref_loc (loc,
8663 fold_convert_loc (loc,
8664 build_pointer_type (etype),
8666 ret = build2 (MODIFY_EXPR, etype, var, ret);
8670 return omit_one_operand_loc (loc, type, dest, ret);
8673 /* Fold function call to builtin memset. Return
8674 NULL_TREE if no simplification can be made. */
8677 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8679 if (! validate_arg (dest, POINTER_TYPE)
8680 || ! validate_arg (size, INTEGER_TYPE))
8686 /* New argument list transforming bzero(ptr x, int y) to
8687 memset(ptr x, int 0, size_t y). This is done this way
8688 so that if it isn't expanded inline, we fallback to
8689 calling bzero instead of memset. */
8691 return fold_builtin_memset (loc, dest, integer_zero_node,
8692 fold_convert_loc (loc, size_type_node, size),
8693 void_type_node, ignore);
8696 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8697 NULL_TREE if no simplification can be made.
8698 If ENDP is 0, return DEST (like memcpy).
8699 If ENDP is 1, return DEST+LEN (like mempcpy).
8700 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8701 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8705 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8706 tree len, tree type, bool ignore, int endp)
8708 tree destvar, srcvar, expr;
8710 if (! validate_arg (dest, POINTER_TYPE)
8711 || ! validate_arg (src, POINTER_TYPE)
8712 || ! validate_arg (len, INTEGER_TYPE))
8715 /* If the LEN parameter is zero, return DEST. */
8716 if (integer_zerop (len))
8717 return omit_one_operand_loc (loc, type, dest, src);
8719 /* If SRC and DEST are the same (and not volatile), return
8720 DEST{,+LEN,+LEN-1}. */
8721 if (operand_equal_p (src, dest, 0))
8725 tree srctype, desttype;
8726 unsigned int src_align, dest_align;
8731 src_align = get_pointer_alignment (src);
8732 dest_align = get_pointer_alignment (dest);
8734 /* Both DEST and SRC must be pointer types.
8735 ??? This is what old code did. Is the testing for pointer types
8738 If either SRC is readonly or length is 1, we can use memcpy. */
8739 if (!dest_align || !src_align)
8741 if (readonly_data_expr (src)
8742 || (host_integerp (len, 1)
8743 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8744 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8746 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8749 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8752 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8753 if (TREE_CODE (src) == ADDR_EXPR
8754 && TREE_CODE (dest) == ADDR_EXPR)
8756 tree src_base, dest_base, fn;
8757 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8758 HOST_WIDE_INT size = -1;
8759 HOST_WIDE_INT maxsize = -1;
8761 srcvar = TREE_OPERAND (src, 0);
8762 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8764 destvar = TREE_OPERAND (dest, 0);
8765 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8767 if (host_integerp (len, 1))
8768 maxsize = tree_low_cst (len, 1);
8771 src_offset /= BITS_PER_UNIT;
8772 dest_offset /= BITS_PER_UNIT;
8773 if (SSA_VAR_P (src_base)
8774 && SSA_VAR_P (dest_base))
8776 if (operand_equal_p (src_base, dest_base, 0)
8777 && ranges_overlap_p (src_offset, maxsize,
8778 dest_offset, maxsize))
8781 else if (TREE_CODE (src_base) == MEM_REF
8782 && TREE_CODE (dest_base) == MEM_REF)
8785 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8786 TREE_OPERAND (dest_base, 0), 0))
8788 off = double_int_add (mem_ref_offset (src_base),
8789 shwi_to_double_int (src_offset));
8790 if (!double_int_fits_in_shwi_p (off))
8792 src_offset = off.low;
8793 off = double_int_add (mem_ref_offset (dest_base),
8794 shwi_to_double_int (dest_offset));
8795 if (!double_int_fits_in_shwi_p (off))
8797 dest_offset = off.low;
8798 if (ranges_overlap_p (src_offset, maxsize,
8799 dest_offset, maxsize))
8805 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8808 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8811 /* If the destination and source do not alias optimize into
8813 if ((is_gimple_min_invariant (dest)
8814 || TREE_CODE (dest) == SSA_NAME)
8815 && (is_gimple_min_invariant (src)
8816 || TREE_CODE (src) == SSA_NAME))
8819 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8820 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8821 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8824 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8827 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8834 if (!host_integerp (len, 0))
8837 This logic lose for arguments like (type *)malloc (sizeof (type)),
8838 since we strip the casts of up to VOID return value from malloc.
8839 Perhaps we ought to inherit type from non-VOID argument here? */
8842 if (!POINTER_TYPE_P (TREE_TYPE (src))
8843 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8845 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8846 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8848 tree tem = TREE_OPERAND (src, 0);
8850 if (tem != TREE_OPERAND (src, 0))
8851 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8853 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8855 tree tem = TREE_OPERAND (dest, 0);
8857 if (tem != TREE_OPERAND (dest, 0))
8858 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8860 srctype = TREE_TYPE (TREE_TYPE (src));
8861 if (TREE_CODE (srctype) == ARRAY_TYPE
8862 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8864 srctype = TREE_TYPE (srctype);
8866 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8868 desttype = TREE_TYPE (TREE_TYPE (dest));
8869 if (TREE_CODE (desttype) == ARRAY_TYPE
8870 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8872 desttype = TREE_TYPE (desttype);
8874 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8876 if (TREE_ADDRESSABLE (srctype)
8877 || TREE_ADDRESSABLE (desttype))
8880 src_align = get_pointer_alignment (src);
8881 dest_align = get_pointer_alignment (dest);
8882 if (dest_align < TYPE_ALIGN (desttype)
8883 || src_align < TYPE_ALIGN (srctype))
8887 dest = builtin_save_expr (dest);
8889 /* Build accesses at offset zero with a ref-all character type. */
8890 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8891 ptr_mode, true), 0);
8894 STRIP_NOPS (destvar);
8895 if (TREE_CODE (destvar) == ADDR_EXPR
8896 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8897 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8898 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8900 destvar = NULL_TREE;
8903 STRIP_NOPS (srcvar);
8904 if (TREE_CODE (srcvar) == ADDR_EXPR
8905 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8906 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8909 || src_align >= TYPE_ALIGN (desttype))
8910 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8912 else if (!STRICT_ALIGNMENT)
8914 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8916 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8924 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8927 if (srcvar == NULL_TREE)
8930 if (src_align >= TYPE_ALIGN (desttype))
8931 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8934 if (STRICT_ALIGNMENT)
8936 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8938 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8941 else if (destvar == NULL_TREE)
8944 if (dest_align >= TYPE_ALIGN (srctype))
8945 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8948 if (STRICT_ALIGNMENT)
8950 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8952 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8956 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8962 if (endp == 0 || endp == 3)
8963 return omit_one_operand_loc (loc, type, dest, expr);
8969 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8972 dest = fold_build_pointer_plus_loc (loc, dest, len);
8973 dest = fold_convert_loc (loc, type, dest);
8975 dest = omit_one_operand_loc (loc, type, dest, expr);
8979 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8980 If LEN is not NULL, it represents the length of the string to be
8981 copied. Return NULL_TREE if no simplification can be made. */
8984 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8988 if (!validate_arg (dest, POINTER_TYPE)
8989 || !validate_arg (src, POINTER_TYPE))
8992 /* If SRC and DEST are the same (and not volatile), return DEST. */
8993 if (operand_equal_p (src, dest, 0))
8994 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8996 if (optimize_function_for_size_p (cfun))
8999 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9005 len = c_strlen (src, 1);
9006 if (! len || TREE_SIDE_EFFECTS (len))
9010 len = fold_convert_loc (loc, size_type_node, len);
9011 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9012 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9013 build_call_expr_loc (loc, fn, 3, dest, src, len));
9016 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9017 Return NULL_TREE if no simplification can be made. */
9020 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9022 tree fn, len, lenp1, call, type;
9024 if (!validate_arg (dest, POINTER_TYPE)
9025 || !validate_arg (src, POINTER_TYPE))
9028 len = c_strlen (src, 1);
9030 || TREE_CODE (len) != INTEGER_CST)
9033 if (optimize_function_for_size_p (cfun)
9034 /* If length is zero it's small enough. */
9035 && !integer_zerop (len))
9038 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9042 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9043 fold_convert_loc (loc, size_type_node, len),
9044 build_int_cst (size_type_node, 1));
9045 /* We use dest twice in building our expression. Save it from
9046 multiple expansions. */
9047 dest = builtin_save_expr (dest);
9048 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9050 type = TREE_TYPE (TREE_TYPE (fndecl));
9051 dest = fold_build_pointer_plus_loc (loc, dest, len);
9052 dest = fold_convert_loc (loc, type, dest);
9053 dest = omit_one_operand_loc (loc, type, dest, call);
9057 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9058 If SLEN is not NULL, it represents the length of the source string.
9059 Return NULL_TREE if no simplification can be made. */
9062 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9063 tree src, tree len, tree slen)
9067 if (!validate_arg (dest, POINTER_TYPE)
9068 || !validate_arg (src, POINTER_TYPE)
9069 || !validate_arg (len, INTEGER_TYPE))
9072 /* If the LEN parameter is zero, return DEST. */
9073 if (integer_zerop (len))
9074 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9076 /* We can't compare slen with len as constants below if len is not a
9078 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9082 slen = c_strlen (src, 1);
9084 /* Now, we must be passed a constant src ptr parameter. */
9085 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9088 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9090 /* We do not support simplification of this case, though we do
9091 support it when expanding trees into RTL. */
9092 /* FIXME: generate a call to __builtin_memset. */
9093 if (tree_int_cst_lt (slen, len))
9096 /* OK transform into builtin memcpy. */
9097 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9101 len = fold_convert_loc (loc, size_type_node, len);
9102 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9103 build_call_expr_loc (loc, fn, 3, dest, src, len));
9106 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9107 arguments to the call, and TYPE is its return type.
9108 Return NULL_TREE if no simplification can be made. */
9111 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9113 if (!validate_arg (arg1, POINTER_TYPE)
9114 || !validate_arg (arg2, INTEGER_TYPE)
9115 || !validate_arg (len, INTEGER_TYPE))
9121 if (TREE_CODE (arg2) != INTEGER_CST
9122 || !host_integerp (len, 1))
9125 p1 = c_getstr (arg1);
9126 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9132 if (target_char_cast (arg2, &c))
9135 r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
9138 return build_int_cst (TREE_TYPE (arg1), 0);
9140 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9141 return fold_convert_loc (loc, type, tem);
9147 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9148 Return NULL_TREE if no simplification can be made. */
9151 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9153 const char *p1, *p2;
9155 if (!validate_arg (arg1, POINTER_TYPE)
9156 || !validate_arg (arg2, POINTER_TYPE)
9157 || !validate_arg (len, INTEGER_TYPE))
9160 /* If the LEN parameter is zero, return zero. */
9161 if (integer_zerop (len))
9162 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9165 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9166 if (operand_equal_p (arg1, arg2, 0))
9167 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9169 p1 = c_getstr (arg1);
9170 p2 = c_getstr (arg2);
9172 /* If all arguments are constant, and the value of len is not greater
9173 than the lengths of arg1 and arg2, evaluate at compile-time. */
9174 if (host_integerp (len, 1) && p1 && p2
9175 && compare_tree_int (len, strlen (p1) + 1) <= 0
9176 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9178 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9181 return integer_one_node;
9183 return integer_minus_one_node;
9185 return integer_zero_node;
9188 /* If len parameter is one, return an expression corresponding to
9189 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9190 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9192 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9193 tree cst_uchar_ptr_node
9194 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9197 = fold_convert_loc (loc, integer_type_node,
9198 build1 (INDIRECT_REF, cst_uchar_node,
9199 fold_convert_loc (loc,
9203 = fold_convert_loc (loc, integer_type_node,
9204 build1 (INDIRECT_REF, cst_uchar_node,
9205 fold_convert_loc (loc,
9208 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9214 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9215 Return NULL_TREE if no simplification can be made. */
9218 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9220 const char *p1, *p2;
9222 if (!validate_arg (arg1, POINTER_TYPE)
9223 || !validate_arg (arg2, POINTER_TYPE))
9226 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9227 if (operand_equal_p (arg1, arg2, 0))
9228 return integer_zero_node;
9230 p1 = c_getstr (arg1);
9231 p2 = c_getstr (arg2);
9235 const int i = strcmp (p1, p2);
9237 return integer_minus_one_node;
9239 return integer_one_node;
9241 return integer_zero_node;
9244 /* If the second arg is "", return *(const unsigned char*)arg1. */
9245 if (p2 && *p2 == '\0')
9247 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9248 tree cst_uchar_ptr_node
9249 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9251 return fold_convert_loc (loc, integer_type_node,
9252 build1 (INDIRECT_REF, cst_uchar_node,
9253 fold_convert_loc (loc,
9258 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9259 if (p1 && *p1 == '\0')
9261 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9262 tree cst_uchar_ptr_node
9263 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9266 = fold_convert_loc (loc, integer_type_node,
9267 build1 (INDIRECT_REF, cst_uchar_node,
9268 fold_convert_loc (loc,
9271 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9277 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9278 Return NULL_TREE if no simplification can be made. */
9281 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9283 const char *p1, *p2;
9285 if (!validate_arg (arg1, POINTER_TYPE)
9286 || !validate_arg (arg2, POINTER_TYPE)
9287 || !validate_arg (len, INTEGER_TYPE))
9290 /* If the LEN parameter is zero, return zero. */
9291 if (integer_zerop (len))
9292 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9295 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9296 if (operand_equal_p (arg1, arg2, 0))
9297 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9299 p1 = c_getstr (arg1);
9300 p2 = c_getstr (arg2);
9302 if (host_integerp (len, 1) && p1 && p2)
9304 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9306 return integer_one_node;
9308 return integer_minus_one_node;
9310 return integer_zero_node;
9313 /* If the second arg is "", and the length is greater than zero,
9314 return *(const unsigned char*)arg1. */
9315 if (p2 && *p2 == '\0'
9316 && TREE_CODE (len) == INTEGER_CST
9317 && tree_int_cst_sgn (len) == 1)
9319 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9320 tree cst_uchar_ptr_node
9321 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9323 return fold_convert_loc (loc, integer_type_node,
9324 build1 (INDIRECT_REF, cst_uchar_node,
9325 fold_convert_loc (loc,
9330 /* If the first arg is "", and the length is greater than zero,
9331 return -*(const unsigned char*)arg2. */
9332 if (p1 && *p1 == '\0'
9333 && TREE_CODE (len) == INTEGER_CST
9334 && tree_int_cst_sgn (len) == 1)
9336 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9337 tree cst_uchar_ptr_node
9338 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9340 tree temp = fold_convert_loc (loc, integer_type_node,
9341 build1 (INDIRECT_REF, cst_uchar_node,
9342 fold_convert_loc (loc,
9345 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9348 /* If len parameter is one, return an expression corresponding to
9349 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9350 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9352 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9353 tree cst_uchar_ptr_node
9354 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9356 tree ind1 = fold_convert_loc (loc, integer_type_node,
9357 build1 (INDIRECT_REF, cst_uchar_node,
9358 fold_convert_loc (loc,
9361 tree ind2 = fold_convert_loc (loc, integer_type_node,
9362 build1 (INDIRECT_REF, cst_uchar_node,
9363 fold_convert_loc (loc,
9366 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9372 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9373 ARG. Return NULL_TREE if no simplification can be made. */
9376 fold_builtin_signbit (location_t loc, tree arg, tree type)
9378 if (!validate_arg (arg, REAL_TYPE))
9381 /* If ARG is a compile-time constant, determine the result. */
9382 if (TREE_CODE (arg) == REAL_CST
9383 && !TREE_OVERFLOW (arg))
9387 c = TREE_REAL_CST (arg);
9388 return (REAL_VALUE_NEGATIVE (c)
9389 ? build_one_cst (type)
9390 : build_zero_cst (type));
9393 /* If ARG is non-negative, the result is always zero. */
9394 if (tree_expr_nonnegative_p (arg))
9395 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9397 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9398 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9399 return fold_convert (type,
9400 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9401 build_real (TREE_TYPE (arg), dconst0)));
9406 /* Fold function call to builtin copysign, copysignf or copysignl with
9407 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9411 fold_builtin_copysign (location_t loc, tree fndecl,
9412 tree arg1, tree arg2, tree type)
9416 if (!validate_arg (arg1, REAL_TYPE)
9417 || !validate_arg (arg2, REAL_TYPE))
9420 /* copysign(X,X) is X. */
9421 if (operand_equal_p (arg1, arg2, 0))
9422 return fold_convert_loc (loc, type, arg1);
9424 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9425 if (TREE_CODE (arg1) == REAL_CST
9426 && TREE_CODE (arg2) == REAL_CST
9427 && !TREE_OVERFLOW (arg1)
9428 && !TREE_OVERFLOW (arg2))
9430 REAL_VALUE_TYPE c1, c2;
9432 c1 = TREE_REAL_CST (arg1);
9433 c2 = TREE_REAL_CST (arg2);
9434 /* c1.sign := c2.sign. */
9435 real_copysign (&c1, &c2);
9436 return build_real (type, c1);
9439 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9440 Remember to evaluate Y for side-effects. */
9441 if (tree_expr_nonnegative_p (arg2))
9442 return omit_one_operand_loc (loc, type,
9443 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9446 /* Strip sign changing operations for the first argument. */
9447 tem = fold_strip_sign_ops (arg1);
9449 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9454 /* Fold a call to builtin isascii with argument ARG. */
9457 fold_builtin_isascii (location_t loc, tree arg)
9459 if (!validate_arg (arg, INTEGER_TYPE))
9463 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9464 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9465 build_int_cst (integer_type_node,
9466 ~ (unsigned HOST_WIDE_INT) 0x7f));
9467 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9468 arg, integer_zero_node);
9472 /* Fold a call to builtin toascii with argument ARG. */
9475 fold_builtin_toascii (location_t loc, tree arg)
9477 if (!validate_arg (arg, INTEGER_TYPE))
9480 /* Transform toascii(c) -> (c & 0x7f). */
9481 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9482 build_int_cst (integer_type_node, 0x7f));
9485 /* Fold a call to builtin isdigit with argument ARG. */
9488 fold_builtin_isdigit (location_t loc, tree arg)
9490 if (!validate_arg (arg, INTEGER_TYPE))
9494 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9495 /* According to the C standard, isdigit is unaffected by locale.
9496 However, it definitely is affected by the target character set. */
9497 unsigned HOST_WIDE_INT target_digit0
9498 = lang_hooks.to_target_charset ('0');
9500 if (target_digit0 == 0)
9503 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9504 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9505 build_int_cst (unsigned_type_node, target_digit0));
9506 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9507 build_int_cst (unsigned_type_node, 9));
9511 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9514 fold_builtin_fabs (location_t loc, tree arg, tree type)
9516 if (!validate_arg (arg, REAL_TYPE))
9519 arg = fold_convert_loc (loc, type, arg);
9520 if (TREE_CODE (arg) == REAL_CST)
9521 return fold_abs_const (arg, type);
9522 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9525 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9528 fold_builtin_abs (location_t loc, tree arg, tree type)
9530 if (!validate_arg (arg, INTEGER_TYPE))
9533 arg = fold_convert_loc (loc, type, arg);
9534 if (TREE_CODE (arg) == INTEGER_CST)
9535 return fold_abs_const (arg, type);
9536 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9539 /* Fold a fma operation with arguments ARG[012]. */
9542 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9543 tree type, tree arg0, tree arg1, tree arg2)
9545 if (TREE_CODE (arg0) == REAL_CST
9546 && TREE_CODE (arg1) == REAL_CST
9547 && TREE_CODE (arg2) == REAL_CST)
9548 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9553 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9556 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9558 if (validate_arg (arg0, REAL_TYPE)
9559 && validate_arg(arg1, REAL_TYPE)
9560 && validate_arg(arg2, REAL_TYPE))
9562 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9566 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9567 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9568 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9573 /* Fold a call to builtin fmin or fmax. */
9576 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9577 tree type, bool max)
9579 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9581 /* Calculate the result when the argument is a constant. */
9582 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9587 /* If either argument is NaN, return the other one. Avoid the
9588 transformation if we get (and honor) a signalling NaN. Using
9589 omit_one_operand() ensures we create a non-lvalue. */
9590 if (TREE_CODE (arg0) == REAL_CST
9591 && real_isnan (&TREE_REAL_CST (arg0))
9592 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9593 || ! TREE_REAL_CST (arg0).signalling))
9594 return omit_one_operand_loc (loc, type, arg1, arg0);
9595 if (TREE_CODE (arg1) == REAL_CST
9596 && real_isnan (&TREE_REAL_CST (arg1))
9597 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9598 || ! TREE_REAL_CST (arg1).signalling))
9599 return omit_one_operand_loc (loc, type, arg0, arg1);
9601 /* Transform fmin/fmax(x,x) -> x. */
9602 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9603 return omit_one_operand_loc (loc, type, arg0, arg1);
9605 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9606 functions to return the numeric arg if the other one is NaN.
9607 These tree codes don't honor that, so only transform if
9608 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9609 handled, so we don't have to worry about it either. */
9610 if (flag_finite_math_only)
9611 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9612 fold_convert_loc (loc, type, arg0),
9613 fold_convert_loc (loc, type, arg1));
9618 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9621 fold_builtin_carg (location_t loc, tree arg, tree type)
9623 if (validate_arg (arg, COMPLEX_TYPE)
9624 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9626 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9630 tree new_arg = builtin_save_expr (arg);
9631 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9632 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9633 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9640 /* Fold a call to builtin logb/ilogb. */
9643 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9645 if (! validate_arg (arg, REAL_TYPE))
9650 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9652 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9658 /* If arg is Inf or NaN and we're logb, return it. */
9659 if (TREE_CODE (rettype) == REAL_TYPE)
9660 return fold_convert_loc (loc, rettype, arg);
9661 /* Fall through... */
9663 /* Zero may set errno and/or raise an exception for logb, also
9664 for ilogb we don't know FP_ILOGB0. */
9667 /* For normal numbers, proceed iff radix == 2. In GCC,
9668 normalized significands are in the range [0.5, 1.0). We
9669 want the exponent as if they were [1.0, 2.0) so get the
9670 exponent and subtract 1. */
9671 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9672 return fold_convert_loc (loc, rettype,
9673 build_int_cst (integer_type_node,
9674 REAL_EXP (value)-1));
9682 /* Fold a call to builtin significand, if radix == 2. */
9685 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9687 if (! validate_arg (arg, REAL_TYPE))
9692 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9694 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9701 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9702 return fold_convert_loc (loc, rettype, arg);
9704 /* For normal numbers, proceed iff radix == 2. */
9705 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9707 REAL_VALUE_TYPE result = *value;
9708 /* In GCC, normalized significands are in the range [0.5,
9709 1.0). We want them to be [1.0, 2.0) so set the
9711 SET_REAL_EXP (&result, 1);
9712 return build_real (rettype, result);
9721 /* Fold a call to builtin frexp, we can assume the base is 2. */
9724 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9726 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9731 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9734 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9736 /* Proceed if a valid pointer type was passed in. */
9737 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9739 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9745 /* For +-0, return (*exp = 0, +-0). */
9746 exp = integer_zero_node;
9751 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9752 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9755 /* Since the frexp function always expects base 2, and in
9756 GCC normalized significands are already in the range
9757 [0.5, 1.0), we have exactly what frexp wants. */
9758 REAL_VALUE_TYPE frac_rvt = *value;
9759 SET_REAL_EXP (&frac_rvt, 0);
9760 frac = build_real (rettype, frac_rvt);
9761 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9768 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9769 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9770 TREE_SIDE_EFFECTS (arg1) = 1;
9771 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9777 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9778 then we can assume the base is two. If it's false, then we have to
9779 check the mode of the TYPE parameter in certain cases. */
9782 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9783 tree type, bool ldexp)
9785 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9790 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9791 if (real_zerop (arg0) || integer_zerop (arg1)
9792 || (TREE_CODE (arg0) == REAL_CST
9793 && !real_isfinite (&TREE_REAL_CST (arg0))))
9794 return omit_one_operand_loc (loc, type, arg0, arg1);
9796 /* If both arguments are constant, then try to evaluate it. */
9797 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9798 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9799 && host_integerp (arg1, 0))
9801 /* Bound the maximum adjustment to twice the range of the
9802 mode's valid exponents. Use abs to ensure the range is
9803 positive as a sanity check. */
9804 const long max_exp_adj = 2 *
9805 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9806 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9808 /* Get the user-requested adjustment. */
9809 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9811 /* The requested adjustment must be inside this range. This
9812 is a preliminary cap to avoid things like overflow, we
9813 may still fail to compute the result for other reasons. */
9814 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9816 REAL_VALUE_TYPE initial_result;
9818 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9820 /* Ensure we didn't overflow. */
9821 if (! real_isinf (&initial_result))
9823 const REAL_VALUE_TYPE trunc_result
9824 = real_value_truncate (TYPE_MODE (type), initial_result);
9826 /* Only proceed if the target mode can hold the
9828 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9829 return build_real (type, trunc_result);
9838 /* Fold a call to builtin modf. */
9841 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9843 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9848 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9851 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9853 /* Proceed if a valid pointer type was passed in. */
9854 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9856 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9857 REAL_VALUE_TYPE trunc, frac;
9863 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9864 trunc = frac = *value;
9867 /* For +-Inf, return (*arg1 = arg0, +-0). */
9869 frac.sign = value->sign;
9873 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9874 real_trunc (&trunc, VOIDmode, value);
9875 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9876 /* If the original number was negative and already
9877 integral, then the fractional part is -0.0. */
9878 if (value->sign && frac.cl == rvc_zero)
9879 frac.sign = value->sign;
9883 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9884 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9885 build_real (rettype, trunc));
9886 TREE_SIDE_EFFECTS (arg1) = 1;
9887 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9888 build_real (rettype, frac));
9894 /* Given a location LOC, an interclass builtin function decl FNDECL
9895 and its single argument ARG, return an folded expression computing
9896 the same, or NULL_TREE if we either couldn't or didn't want to fold
9897 (the latter happen if there's an RTL instruction available). */
9900 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9902 enum machine_mode mode;
9904 if (!validate_arg (arg, REAL_TYPE))
9907 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9910 mode = TYPE_MODE (TREE_TYPE (arg));
9912 /* If there is no optab, try generic code. */
9913 switch (DECL_FUNCTION_CODE (fndecl))
9917 CASE_FLT_FN (BUILT_IN_ISINF):
9919 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9920 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9921 tree const type = TREE_TYPE (arg);
9925 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9926 real_from_string (&r, buf);
9927 result = build_call_expr (isgr_fn, 2,
9928 fold_build1_loc (loc, ABS_EXPR, type, arg),
9929 build_real (type, r));
9932 CASE_FLT_FN (BUILT_IN_FINITE):
9933 case BUILT_IN_ISFINITE:
9935 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9936 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9937 tree const type = TREE_TYPE (arg);
9941 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9942 real_from_string (&r, buf);
9943 result = build_call_expr (isle_fn, 2,
9944 fold_build1_loc (loc, ABS_EXPR, type, arg),
9945 build_real (type, r));
9946 /*result = fold_build2_loc (loc, UNGT_EXPR,
9947 TREE_TYPE (TREE_TYPE (fndecl)),
9948 fold_build1_loc (loc, ABS_EXPR, type, arg),
9949 build_real (type, r));
9950 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9951 TREE_TYPE (TREE_TYPE (fndecl)),
9955 case BUILT_IN_ISNORMAL:
9957 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9958 islessequal(fabs(x),DBL_MAX). */
9959 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9960 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9961 tree const type = TREE_TYPE (arg);
9962 REAL_VALUE_TYPE rmax, rmin;
9965 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9966 real_from_string (&rmax, buf);
9967 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9968 real_from_string (&rmin, buf);
9969 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9970 result = build_call_expr (isle_fn, 2, arg,
9971 build_real (type, rmax));
9972 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9973 build_call_expr (isge_fn, 2, arg,
9974 build_real (type, rmin)));
9984 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9985 ARG is the argument for the call. */
9988 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9990 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9993 if (!validate_arg (arg, REAL_TYPE))
9996 switch (builtin_index)
9998 case BUILT_IN_ISINF:
9999 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10000 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10002 if (TREE_CODE (arg) == REAL_CST)
10004 r = TREE_REAL_CST (arg);
10005 if (real_isinf (&r))
10006 return real_compare (GT_EXPR, &r, &dconst0)
10007 ? integer_one_node : integer_minus_one_node;
10009 return integer_zero_node;
10014 case BUILT_IN_ISINF_SIGN:
10016 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10017 /* In a boolean context, GCC will fold the inner COND_EXPR to
10018 1. So e.g. "if (isinf_sign(x))" would be folded to just
10019 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10020 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10021 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10022 tree tmp = NULL_TREE;
10024 arg = builtin_save_expr (arg);
10026 if (signbit_fn && isinf_fn)
10028 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10029 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10031 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10032 signbit_call, integer_zero_node);
10033 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10034 isinf_call, integer_zero_node);
10036 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10037 integer_minus_one_node, integer_one_node);
10038 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10040 integer_zero_node);
10046 case BUILT_IN_ISFINITE:
10047 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10048 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10049 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10051 if (TREE_CODE (arg) == REAL_CST)
10053 r = TREE_REAL_CST (arg);
10054 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10059 case BUILT_IN_ISNAN:
10060 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10061 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10063 if (TREE_CODE (arg) == REAL_CST)
10065 r = TREE_REAL_CST (arg);
10066 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10069 arg = builtin_save_expr (arg);
10070 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10073 gcc_unreachable ();
10077 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10078 This builtin will generate code to return the appropriate floating
10079 point classification depending on the value of the floating point
10080 number passed in. The possible return values must be supplied as
10081 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10082 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10083 one floating point argument which is "type generic". */
10086 fold_builtin_fpclassify (location_t loc, tree exp)
10088 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10089 arg, type, res, tmp;
10090 enum machine_mode mode;
10094 /* Verify the required arguments in the original call. */
10095 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10096 INTEGER_TYPE, INTEGER_TYPE,
10097 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10100 fp_nan = CALL_EXPR_ARG (exp, 0);
10101 fp_infinite = CALL_EXPR_ARG (exp, 1);
10102 fp_normal = CALL_EXPR_ARG (exp, 2);
10103 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10104 fp_zero = CALL_EXPR_ARG (exp, 4);
10105 arg = CALL_EXPR_ARG (exp, 5);
10106 type = TREE_TYPE (arg);
10107 mode = TYPE_MODE (type);
10108 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10110 /* fpclassify(x) ->
10111 isnan(x) ? FP_NAN :
10112 (fabs(x) == Inf ? FP_INFINITE :
10113 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10114 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10116 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10117 build_real (type, dconst0));
10118 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10119 tmp, fp_zero, fp_subnormal);
10121 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10122 real_from_string (&r, buf);
10123 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10124 arg, build_real (type, r));
10125 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10127 if (HONOR_INFINITIES (mode))
10130 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10131 build_real (type, r));
10132 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10136 if (HONOR_NANS (mode))
10138 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10139 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10145 /* Fold a call to an unordered comparison function such as
10146 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10147 being called and ARG0 and ARG1 are the arguments for the call.
10148 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10149 the opposite of the desired result. UNORDERED_CODE is used
10150 for modes that can hold NaNs and ORDERED_CODE is used for
10154 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10155 enum tree_code unordered_code,
10156 enum tree_code ordered_code)
10158 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10159 enum tree_code code;
10161 enum tree_code code0, code1;
10162 tree cmp_type = NULL_TREE;
10164 type0 = TREE_TYPE (arg0);
10165 type1 = TREE_TYPE (arg1);
10167 code0 = TREE_CODE (type0);
10168 code1 = TREE_CODE (type1);
10170 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10171 /* Choose the wider of two real types. */
10172 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10174 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10176 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10179 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10180 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10182 if (unordered_code == UNORDERED_EXPR)
10184 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10185 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10186 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10189 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10191 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10192 fold_build2_loc (loc, code, type, arg0, arg1));
10195 /* Fold a call to built-in function FNDECL with 0 arguments.
10196 IGNORE is true if the result of the function call is ignored. This
10197 function returns NULL_TREE if no simplification was possible. */
10200 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10202 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10203 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10206 CASE_FLT_FN (BUILT_IN_INF):
10207 case BUILT_IN_INFD32:
10208 case BUILT_IN_INFD64:
10209 case BUILT_IN_INFD128:
10210 return fold_builtin_inf (loc, type, true);
10212 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10213 return fold_builtin_inf (loc, type, false);
10215 case BUILT_IN_CLASSIFY_TYPE:
10216 return fold_builtin_classify_type (NULL_TREE);
10224 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10225 IGNORE is true if the result of the function call is ignored. This
10226 function returns NULL_TREE if no simplification was possible. */
10229 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10231 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10232 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10235 case BUILT_IN_CONSTANT_P:
10237 tree val = fold_builtin_constant_p (arg0);
10239 /* Gimplification will pull the CALL_EXPR for the builtin out of
10240 an if condition. When not optimizing, we'll not CSE it back.
10241 To avoid link error types of regressions, return false now. */
10242 if (!val && !optimize)
10243 val = integer_zero_node;
10248 case BUILT_IN_CLASSIFY_TYPE:
10249 return fold_builtin_classify_type (arg0);
10251 case BUILT_IN_STRLEN:
10252 return fold_builtin_strlen (loc, type, arg0);
10254 CASE_FLT_FN (BUILT_IN_FABS):
10255 return fold_builtin_fabs (loc, arg0, type);
10258 case BUILT_IN_LABS:
10259 case BUILT_IN_LLABS:
10260 case BUILT_IN_IMAXABS:
10261 return fold_builtin_abs (loc, arg0, type);
10263 CASE_FLT_FN (BUILT_IN_CONJ):
10264 if (validate_arg (arg0, COMPLEX_TYPE)
10265 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10266 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10269 CASE_FLT_FN (BUILT_IN_CREAL):
10270 if (validate_arg (arg0, COMPLEX_TYPE)
10271 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10272 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10275 CASE_FLT_FN (BUILT_IN_CIMAG):
10276 if (validate_arg (arg0, COMPLEX_TYPE)
10277 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10278 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10281 CASE_FLT_FN (BUILT_IN_CCOS):
10282 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
10284 CASE_FLT_FN (BUILT_IN_CCOSH):
10285 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
10287 CASE_FLT_FN (BUILT_IN_CPROJ):
10288 return fold_builtin_cproj(loc, arg0, type);
10290 CASE_FLT_FN (BUILT_IN_CSIN):
10291 if (validate_arg (arg0, COMPLEX_TYPE)
10292 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10293 return do_mpc_arg1 (arg0, type, mpc_sin);
10296 CASE_FLT_FN (BUILT_IN_CSINH):
10297 if (validate_arg (arg0, COMPLEX_TYPE)
10298 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10299 return do_mpc_arg1 (arg0, type, mpc_sinh);
10302 CASE_FLT_FN (BUILT_IN_CTAN):
10303 if (validate_arg (arg0, COMPLEX_TYPE)
10304 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10305 return do_mpc_arg1 (arg0, type, mpc_tan);
10308 CASE_FLT_FN (BUILT_IN_CTANH):
10309 if (validate_arg (arg0, COMPLEX_TYPE)
10310 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10311 return do_mpc_arg1 (arg0, type, mpc_tanh);
10314 CASE_FLT_FN (BUILT_IN_CLOG):
10315 if (validate_arg (arg0, COMPLEX_TYPE)
10316 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10317 return do_mpc_arg1 (arg0, type, mpc_log);
10320 CASE_FLT_FN (BUILT_IN_CSQRT):
10321 if (validate_arg (arg0, COMPLEX_TYPE)
10322 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10323 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10326 CASE_FLT_FN (BUILT_IN_CASIN):
10327 if (validate_arg (arg0, COMPLEX_TYPE)
10328 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10329 return do_mpc_arg1 (arg0, type, mpc_asin);
10332 CASE_FLT_FN (BUILT_IN_CACOS):
10333 if (validate_arg (arg0, COMPLEX_TYPE)
10334 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10335 return do_mpc_arg1 (arg0, type, mpc_acos);
10338 CASE_FLT_FN (BUILT_IN_CATAN):
10339 if (validate_arg (arg0, COMPLEX_TYPE)
10340 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10341 return do_mpc_arg1 (arg0, type, mpc_atan);
10344 CASE_FLT_FN (BUILT_IN_CASINH):
10345 if (validate_arg (arg0, COMPLEX_TYPE)
10346 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10347 return do_mpc_arg1 (arg0, type, mpc_asinh);
10350 CASE_FLT_FN (BUILT_IN_CACOSH):
10351 if (validate_arg (arg0, COMPLEX_TYPE)
10352 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10353 return do_mpc_arg1 (arg0, type, mpc_acosh);
10356 CASE_FLT_FN (BUILT_IN_CATANH):
10357 if (validate_arg (arg0, COMPLEX_TYPE)
10358 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10359 return do_mpc_arg1 (arg0, type, mpc_atanh);
10362 CASE_FLT_FN (BUILT_IN_CABS):
10363 return fold_builtin_cabs (loc, arg0, type, fndecl);
10365 CASE_FLT_FN (BUILT_IN_CARG):
10366 return fold_builtin_carg (loc, arg0, type);
10368 CASE_FLT_FN (BUILT_IN_SQRT):
10369 return fold_builtin_sqrt (loc, arg0, type);
10371 CASE_FLT_FN (BUILT_IN_CBRT):
10372 return fold_builtin_cbrt (loc, arg0, type);
10374 CASE_FLT_FN (BUILT_IN_ASIN):
10375 if (validate_arg (arg0, REAL_TYPE))
10376 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10377 &dconstm1, &dconst1, true);
10380 CASE_FLT_FN (BUILT_IN_ACOS):
10381 if (validate_arg (arg0, REAL_TYPE))
10382 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10383 &dconstm1, &dconst1, true);
10386 CASE_FLT_FN (BUILT_IN_ATAN):
10387 if (validate_arg (arg0, REAL_TYPE))
10388 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10391 CASE_FLT_FN (BUILT_IN_ASINH):
10392 if (validate_arg (arg0, REAL_TYPE))
10393 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10396 CASE_FLT_FN (BUILT_IN_ACOSH):
10397 if (validate_arg (arg0, REAL_TYPE))
10398 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10399 &dconst1, NULL, true);
10402 CASE_FLT_FN (BUILT_IN_ATANH):
10403 if (validate_arg (arg0, REAL_TYPE))
10404 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10405 &dconstm1, &dconst1, false);
10408 CASE_FLT_FN (BUILT_IN_SIN):
10409 if (validate_arg (arg0, REAL_TYPE))
10410 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10413 CASE_FLT_FN (BUILT_IN_COS):
10414 return fold_builtin_cos (loc, arg0, type, fndecl);
10416 CASE_FLT_FN (BUILT_IN_TAN):
10417 return fold_builtin_tan (arg0, type);
10419 CASE_FLT_FN (BUILT_IN_CEXP):
10420 return fold_builtin_cexp (loc, arg0, type);
10422 CASE_FLT_FN (BUILT_IN_CEXPI):
10423 if (validate_arg (arg0, REAL_TYPE))
10424 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10427 CASE_FLT_FN (BUILT_IN_SINH):
10428 if (validate_arg (arg0, REAL_TYPE))
10429 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10432 CASE_FLT_FN (BUILT_IN_COSH):
10433 return fold_builtin_cosh (loc, arg0, type, fndecl);
10435 CASE_FLT_FN (BUILT_IN_TANH):
10436 if (validate_arg (arg0, REAL_TYPE))
10437 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10440 CASE_FLT_FN (BUILT_IN_ERF):
10441 if (validate_arg (arg0, REAL_TYPE))
10442 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10445 CASE_FLT_FN (BUILT_IN_ERFC):
10446 if (validate_arg (arg0, REAL_TYPE))
10447 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10450 CASE_FLT_FN (BUILT_IN_TGAMMA):
10451 if (validate_arg (arg0, REAL_TYPE))
10452 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10455 CASE_FLT_FN (BUILT_IN_EXP):
10456 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10458 CASE_FLT_FN (BUILT_IN_EXP2):
10459 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10461 CASE_FLT_FN (BUILT_IN_EXP10):
10462 CASE_FLT_FN (BUILT_IN_POW10):
10463 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10465 CASE_FLT_FN (BUILT_IN_EXPM1):
10466 if (validate_arg (arg0, REAL_TYPE))
10467 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10470 CASE_FLT_FN (BUILT_IN_LOG):
10471 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10473 CASE_FLT_FN (BUILT_IN_LOG2):
10474 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10476 CASE_FLT_FN (BUILT_IN_LOG10):
10477 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10479 CASE_FLT_FN (BUILT_IN_LOG1P):
10480 if (validate_arg (arg0, REAL_TYPE))
10481 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10482 &dconstm1, NULL, false);
10485 CASE_FLT_FN (BUILT_IN_J0):
10486 if (validate_arg (arg0, REAL_TYPE))
10487 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10491 CASE_FLT_FN (BUILT_IN_J1):
10492 if (validate_arg (arg0, REAL_TYPE))
10493 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10497 CASE_FLT_FN (BUILT_IN_Y0):
10498 if (validate_arg (arg0, REAL_TYPE))
10499 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10500 &dconst0, NULL, false);
10503 CASE_FLT_FN (BUILT_IN_Y1):
10504 if (validate_arg (arg0, REAL_TYPE))
10505 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10506 &dconst0, NULL, false);
10509 CASE_FLT_FN (BUILT_IN_NAN):
10510 case BUILT_IN_NAND32:
10511 case BUILT_IN_NAND64:
10512 case BUILT_IN_NAND128:
10513 return fold_builtin_nan (arg0, type, true);
10515 CASE_FLT_FN (BUILT_IN_NANS):
10516 return fold_builtin_nan (arg0, type, false);
10518 CASE_FLT_FN (BUILT_IN_FLOOR):
10519 return fold_builtin_floor (loc, fndecl, arg0);
10521 CASE_FLT_FN (BUILT_IN_CEIL):
10522 return fold_builtin_ceil (loc, fndecl, arg0);
10524 CASE_FLT_FN (BUILT_IN_TRUNC):
10525 return fold_builtin_trunc (loc, fndecl, arg0);
10527 CASE_FLT_FN (BUILT_IN_ROUND):
10528 return fold_builtin_round (loc, fndecl, arg0);
10530 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10531 CASE_FLT_FN (BUILT_IN_RINT):
10532 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10534 CASE_FLT_FN (BUILT_IN_ICEIL):
10535 CASE_FLT_FN (BUILT_IN_LCEIL):
10536 CASE_FLT_FN (BUILT_IN_LLCEIL):
10537 CASE_FLT_FN (BUILT_IN_LFLOOR):
10538 CASE_FLT_FN (BUILT_IN_IFLOOR):
10539 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10540 CASE_FLT_FN (BUILT_IN_IROUND):
10541 CASE_FLT_FN (BUILT_IN_LROUND):
10542 CASE_FLT_FN (BUILT_IN_LLROUND):
10543 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10545 CASE_FLT_FN (BUILT_IN_IRINT):
10546 CASE_FLT_FN (BUILT_IN_LRINT):
10547 CASE_FLT_FN (BUILT_IN_LLRINT):
10548 return fold_fixed_mathfn (loc, fndecl, arg0);
10550 case BUILT_IN_BSWAP32:
10551 case BUILT_IN_BSWAP64:
10552 return fold_builtin_bswap (fndecl, arg0);
10554 CASE_INT_FN (BUILT_IN_FFS):
10555 CASE_INT_FN (BUILT_IN_CLZ):
10556 CASE_INT_FN (BUILT_IN_CTZ):
10557 CASE_INT_FN (BUILT_IN_CLRSB):
10558 CASE_INT_FN (BUILT_IN_POPCOUNT):
10559 CASE_INT_FN (BUILT_IN_PARITY):
10560 return fold_builtin_bitop (fndecl, arg0);
10562 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10563 return fold_builtin_signbit (loc, arg0, type);
10565 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10566 return fold_builtin_significand (loc, arg0, type);
10568 CASE_FLT_FN (BUILT_IN_ILOGB):
10569 CASE_FLT_FN (BUILT_IN_LOGB):
10570 return fold_builtin_logb (loc, arg0, type);
10572 case BUILT_IN_ISASCII:
10573 return fold_builtin_isascii (loc, arg0);
10575 case BUILT_IN_TOASCII:
10576 return fold_builtin_toascii (loc, arg0);
10578 case BUILT_IN_ISDIGIT:
10579 return fold_builtin_isdigit (loc, arg0);
10581 CASE_FLT_FN (BUILT_IN_FINITE):
10582 case BUILT_IN_FINITED32:
10583 case BUILT_IN_FINITED64:
10584 case BUILT_IN_FINITED128:
10585 case BUILT_IN_ISFINITE:
10587 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10590 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10593 CASE_FLT_FN (BUILT_IN_ISINF):
10594 case BUILT_IN_ISINFD32:
10595 case BUILT_IN_ISINFD64:
10596 case BUILT_IN_ISINFD128:
10598 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10601 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10604 case BUILT_IN_ISNORMAL:
10605 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10607 case BUILT_IN_ISINF_SIGN:
10608 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10610 CASE_FLT_FN (BUILT_IN_ISNAN):
10611 case BUILT_IN_ISNAND32:
10612 case BUILT_IN_ISNAND64:
10613 case BUILT_IN_ISNAND128:
10614 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10616 case BUILT_IN_PRINTF:
10617 case BUILT_IN_PRINTF_UNLOCKED:
10618 case BUILT_IN_VPRINTF:
10619 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10621 case BUILT_IN_FREE:
10622 if (integer_zerop (arg0))
10623 return build_empty_stmt (loc);
10634 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10635 IGNORE is true if the result of the function call is ignored. This
10636 function returns NULL_TREE if no simplification was possible. */
10639 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10641 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10642 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10646 CASE_FLT_FN (BUILT_IN_JN):
10647 if (validate_arg (arg0, INTEGER_TYPE)
10648 && validate_arg (arg1, REAL_TYPE))
10649 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10652 CASE_FLT_FN (BUILT_IN_YN):
10653 if (validate_arg (arg0, INTEGER_TYPE)
10654 && validate_arg (arg1, REAL_TYPE))
10655 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10659 CASE_FLT_FN (BUILT_IN_DREM):
10660 CASE_FLT_FN (BUILT_IN_REMAINDER):
10661 if (validate_arg (arg0, REAL_TYPE)
10662 && validate_arg(arg1, REAL_TYPE))
10663 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10666 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10667 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10668 if (validate_arg (arg0, REAL_TYPE)
10669 && validate_arg(arg1, POINTER_TYPE))
10670 return do_mpfr_lgamma_r (arg0, arg1, type);
10673 CASE_FLT_FN (BUILT_IN_ATAN2):
10674 if (validate_arg (arg0, REAL_TYPE)
10675 && validate_arg(arg1, REAL_TYPE))
10676 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10679 CASE_FLT_FN (BUILT_IN_FDIM):
10680 if (validate_arg (arg0, REAL_TYPE)
10681 && validate_arg(arg1, REAL_TYPE))
10682 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10685 CASE_FLT_FN (BUILT_IN_HYPOT):
10686 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10688 CASE_FLT_FN (BUILT_IN_CPOW):
10689 if (validate_arg (arg0, COMPLEX_TYPE)
10690 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10691 && validate_arg (arg1, COMPLEX_TYPE)
10692 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10693 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10696 CASE_FLT_FN (BUILT_IN_LDEXP):
10697 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10698 CASE_FLT_FN (BUILT_IN_SCALBN):
10699 CASE_FLT_FN (BUILT_IN_SCALBLN):
10700 return fold_builtin_load_exponent (loc, arg0, arg1,
10701 type, /*ldexp=*/false);
10703 CASE_FLT_FN (BUILT_IN_FREXP):
10704 return fold_builtin_frexp (loc, arg0, arg1, type);
10706 CASE_FLT_FN (BUILT_IN_MODF):
10707 return fold_builtin_modf (loc, arg0, arg1, type);
10709 case BUILT_IN_BZERO:
10710 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10712 case BUILT_IN_FPUTS:
10713 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10715 case BUILT_IN_FPUTS_UNLOCKED:
10716 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10718 case BUILT_IN_STRSTR:
10719 return fold_builtin_strstr (loc, arg0, arg1, type);
10721 case BUILT_IN_STRCAT:
10722 return fold_builtin_strcat (loc, arg0, arg1);
10724 case BUILT_IN_STRSPN:
10725 return fold_builtin_strspn (loc, arg0, arg1);
10727 case BUILT_IN_STRCSPN:
10728 return fold_builtin_strcspn (loc, arg0, arg1);
10730 case BUILT_IN_STRCHR:
10731 case BUILT_IN_INDEX:
10732 return fold_builtin_strchr (loc, arg0, arg1, type);
10734 case BUILT_IN_STRRCHR:
10735 case BUILT_IN_RINDEX:
10736 return fold_builtin_strrchr (loc, arg0, arg1, type);
10738 case BUILT_IN_STRCPY:
10739 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10741 case BUILT_IN_STPCPY:
10744 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10748 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10751 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10754 case BUILT_IN_STRCMP:
10755 return fold_builtin_strcmp (loc, arg0, arg1);
10757 case BUILT_IN_STRPBRK:
10758 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10760 case BUILT_IN_EXPECT:
10761 return fold_builtin_expect (loc, arg0, arg1);
10763 CASE_FLT_FN (BUILT_IN_POW):
10764 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10766 CASE_FLT_FN (BUILT_IN_POWI):
10767 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10769 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10770 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10772 CASE_FLT_FN (BUILT_IN_FMIN):
10773 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10775 CASE_FLT_FN (BUILT_IN_FMAX):
10776 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10778 case BUILT_IN_ISGREATER:
10779 return fold_builtin_unordered_cmp (loc, fndecl,
10780 arg0, arg1, UNLE_EXPR, LE_EXPR);
10781 case BUILT_IN_ISGREATEREQUAL:
10782 return fold_builtin_unordered_cmp (loc, fndecl,
10783 arg0, arg1, UNLT_EXPR, LT_EXPR);
10784 case BUILT_IN_ISLESS:
10785 return fold_builtin_unordered_cmp (loc, fndecl,
10786 arg0, arg1, UNGE_EXPR, GE_EXPR);
10787 case BUILT_IN_ISLESSEQUAL:
10788 return fold_builtin_unordered_cmp (loc, fndecl,
10789 arg0, arg1, UNGT_EXPR, GT_EXPR);
10790 case BUILT_IN_ISLESSGREATER:
10791 return fold_builtin_unordered_cmp (loc, fndecl,
10792 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10793 case BUILT_IN_ISUNORDERED:
10794 return fold_builtin_unordered_cmp (loc, fndecl,
10795 arg0, arg1, UNORDERED_EXPR,
10798 /* We do the folding for va_start in the expander. */
10799 case BUILT_IN_VA_START:
10802 case BUILT_IN_SPRINTF:
10803 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10805 case BUILT_IN_OBJECT_SIZE:
10806 return fold_builtin_object_size (arg0, arg1);
10808 case BUILT_IN_PRINTF:
10809 case BUILT_IN_PRINTF_UNLOCKED:
10810 case BUILT_IN_VPRINTF:
10811 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10813 case BUILT_IN_PRINTF_CHK:
10814 case BUILT_IN_VPRINTF_CHK:
10815 if (!validate_arg (arg0, INTEGER_TYPE)
10816 || TREE_SIDE_EFFECTS (arg0))
10819 return fold_builtin_printf (loc, fndecl,
10820 arg1, NULL_TREE, ignore, fcode);
10823 case BUILT_IN_FPRINTF:
10824 case BUILT_IN_FPRINTF_UNLOCKED:
10825 case BUILT_IN_VFPRINTF:
10826 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10829 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10830 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10832 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10833 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10841 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10842 and ARG2. IGNORE is true if the result of the function call is ignored.
10843 This function returns NULL_TREE if no simplification was possible. */
10846 fold_builtin_3 (location_t loc, tree fndecl,
10847 tree arg0, tree arg1, tree arg2, bool ignore)
10849 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10850 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10854 CASE_FLT_FN (BUILT_IN_SINCOS):
10855 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10857 CASE_FLT_FN (BUILT_IN_FMA):
10858 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10861 CASE_FLT_FN (BUILT_IN_REMQUO):
10862 if (validate_arg (arg0, REAL_TYPE)
10863 && validate_arg(arg1, REAL_TYPE)
10864 && validate_arg(arg2, POINTER_TYPE))
10865 return do_mpfr_remquo (arg0, arg1, arg2);
10868 case BUILT_IN_MEMSET:
10869 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10871 case BUILT_IN_BCOPY:
10872 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10873 void_type_node, true, /*endp=*/3);
10875 case BUILT_IN_MEMCPY:
10876 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10877 type, ignore, /*endp=*/0);
10879 case BUILT_IN_MEMPCPY:
10880 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10881 type, ignore, /*endp=*/1);
10883 case BUILT_IN_MEMMOVE:
10884 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10885 type, ignore, /*endp=*/3);
10887 case BUILT_IN_STRNCAT:
10888 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10890 case BUILT_IN_STRNCPY:
10891 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10893 case BUILT_IN_STRNCMP:
10894 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10896 case BUILT_IN_MEMCHR:
10897 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10899 case BUILT_IN_BCMP:
10900 case BUILT_IN_MEMCMP:
10901 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10903 case BUILT_IN_SPRINTF:
10904 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10906 case BUILT_IN_SNPRINTF:
10907 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10909 case BUILT_IN_STRCPY_CHK:
10910 case BUILT_IN_STPCPY_CHK:
10911 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10914 case BUILT_IN_STRCAT_CHK:
10915 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10917 case BUILT_IN_PRINTF_CHK:
10918 case BUILT_IN_VPRINTF_CHK:
10919 if (!validate_arg (arg0, INTEGER_TYPE)
10920 || TREE_SIDE_EFFECTS (arg0))
10923 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10926 case BUILT_IN_FPRINTF:
10927 case BUILT_IN_FPRINTF_UNLOCKED:
10928 case BUILT_IN_VFPRINTF:
10929 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10932 case BUILT_IN_FPRINTF_CHK:
10933 case BUILT_IN_VFPRINTF_CHK:
10934 if (!validate_arg (arg1, INTEGER_TYPE)
10935 || TREE_SIDE_EFFECTS (arg1))
10938 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10947 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10948 ARG2, and ARG3. IGNORE is true if the result of the function call is
10949 ignored. This function returns NULL_TREE if no simplification was
10953 fold_builtin_4 (location_t loc, tree fndecl,
10954 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10956 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10960 case BUILT_IN_MEMCPY_CHK:
10961 case BUILT_IN_MEMPCPY_CHK:
10962 case BUILT_IN_MEMMOVE_CHK:
10963 case BUILT_IN_MEMSET_CHK:
10964 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10966 DECL_FUNCTION_CODE (fndecl));
10968 case BUILT_IN_STRNCPY_CHK:
10969 case BUILT_IN_STPNCPY_CHK:
10970 return fold_builtin_stxncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE,
10973 case BUILT_IN_STRNCAT_CHK:
10974 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10976 case BUILT_IN_SNPRINTF:
10977 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10979 case BUILT_IN_FPRINTF_CHK:
10980 case BUILT_IN_VFPRINTF_CHK:
10981 if (!validate_arg (arg1, INTEGER_TYPE)
10982 || TREE_SIDE_EFFECTS (arg1))
10985 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10995 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10996 arguments, where NARGS <= 4. IGNORE is true if the result of the
10997 function call is ignored. This function returns NULL_TREE if no
10998 simplification was possible. Note that this only folds builtins with
10999 fixed argument patterns. Foldings that do varargs-to-varargs
11000 transformations, or that match calls with more than 4 arguments,
11001 need to be handled with fold_builtin_varargs instead. */
11003 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11006 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11008 tree ret = NULL_TREE;
11013 ret = fold_builtin_0 (loc, fndecl, ignore);
11016 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11019 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11022 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11025 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11033 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11034 SET_EXPR_LOCATION (ret, loc);
11035 TREE_NO_WARNING (ret) = 1;
11041 /* Builtins with folding operations that operate on "..." arguments
11042 need special handling; we need to store the arguments in a convenient
11043 data structure before attempting any folding. Fortunately there are
11044 only a few builtins that fall into this category. FNDECL is the
11045 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11046 result of the function call is ignored. */
11049 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11050 bool ignore ATTRIBUTE_UNUSED)
11052 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11053 tree ret = NULL_TREE;
11057 case BUILT_IN_SPRINTF_CHK:
11058 case BUILT_IN_VSPRINTF_CHK:
11059 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
11062 case BUILT_IN_SNPRINTF_CHK:
11063 case BUILT_IN_VSNPRINTF_CHK:
11064 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
11067 case BUILT_IN_FPCLASSIFY:
11068 ret = fold_builtin_fpclassify (loc, exp);
11076 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11077 SET_EXPR_LOCATION (ret, loc);
11078 TREE_NO_WARNING (ret) = 1;
11084 /* Return true if FNDECL shouldn't be folded right now.
11085 If a built-in function has an inline attribute always_inline
11086 wrapper, defer folding it after always_inline functions have
11087 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11088 might not be performed. */
11091 avoid_folding_inline_builtin (tree fndecl)
11093 return (DECL_DECLARED_INLINE_P (fndecl)
11094 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11096 && !cfun->always_inline_functions_inlined
11097 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11100 /* A wrapper function for builtin folding that prevents warnings for
11101 "statement without effect" and the like, caused by removing the
11102 call node earlier than the warning is generated. */
11105 fold_call_expr (location_t loc, tree exp, bool ignore)
11107 tree ret = NULL_TREE;
11108 tree fndecl = get_callee_fndecl (exp);
11110 && TREE_CODE (fndecl) == FUNCTION_DECL
11111 && DECL_BUILT_IN (fndecl)
11112 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11113 yet. Defer folding until we see all the arguments
11114 (after inlining). */
11115 && !CALL_EXPR_VA_ARG_PACK (exp))
11117 int nargs = call_expr_nargs (exp);
11119 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11120 instead last argument is __builtin_va_arg_pack (). Defer folding
11121 even in that case, until arguments are finalized. */
11122 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11124 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11126 && TREE_CODE (fndecl2) == FUNCTION_DECL
11127 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11128 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11132 if (avoid_folding_inline_builtin (fndecl))
11135 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11136 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11137 CALL_EXPR_ARGP (exp), ignore);
11140 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11142 tree *args = CALL_EXPR_ARGP (exp);
11143 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11146 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11154 /* Conveniently construct a function call expression. FNDECL names the
11155 function to be called and N arguments are passed in the array
11159 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11161 tree fntype = TREE_TYPE (fndecl);
11162 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11164 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11167 /* Conveniently construct a function call expression. FNDECL names the
11168 function to be called and the arguments are passed in the vector
11172 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
11174 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
11175 VEC_address (tree, vec));
11179 /* Conveniently construct a function call expression. FNDECL names the
11180 function to be called, N is the number of arguments, and the "..."
11181 parameters are the argument expressions. */
11184 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11187 tree *argarray = XALLOCAVEC (tree, n);
11191 for (i = 0; i < n; i++)
11192 argarray[i] = va_arg (ap, tree);
11194 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11197 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11198 varargs macros aren't supported by all bootstrap compilers. */
11201 build_call_expr (tree fndecl, int n, ...)
11204 tree *argarray = XALLOCAVEC (tree, n);
11208 for (i = 0; i < n; i++)
11209 argarray[i] = va_arg (ap, tree);
11211 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11214 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11215 N arguments are passed in the array ARGARRAY. */
11218 fold_builtin_call_array (location_t loc, tree type,
11223 tree ret = NULL_TREE;
11226 if (TREE_CODE (fn) == ADDR_EXPR)
11228 tree fndecl = TREE_OPERAND (fn, 0);
11229 if (TREE_CODE (fndecl) == FUNCTION_DECL
11230 && DECL_BUILT_IN (fndecl))
11232 /* If last argument is __builtin_va_arg_pack (), arguments to this
11233 function are not finalized yet. Defer folding until they are. */
11234 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11236 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11238 && TREE_CODE (fndecl2) == FUNCTION_DECL
11239 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11240 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11241 return build_call_array_loc (loc, type, fn, n, argarray);
11243 if (avoid_folding_inline_builtin (fndecl))
11244 return build_call_array_loc (loc, type, fn, n, argarray);
11245 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11247 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11251 return build_call_array_loc (loc, type, fn, n, argarray);
11253 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11255 /* First try the transformations that don't require consing up
11257 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11262 /* If we got this far, we need to build an exp. */
11263 exp = build_call_array_loc (loc, type, fn, n, argarray);
11264 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11265 return ret ? ret : exp;
11269 return build_call_array_loc (loc, type, fn, n, argarray);
11272 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11273 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11274 of arguments in ARGS to be omitted. OLDNARGS is the number of
11275 elements in ARGS. */
11278 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11279 int skip, tree fndecl, int n, va_list newargs)
11281 int nargs = oldnargs - skip + n;
11288 buffer = XALLOCAVEC (tree, nargs);
11289 for (i = 0; i < n; i++)
11290 buffer[i] = va_arg (newargs, tree);
11291 for (j = skip; j < oldnargs; j++, i++)
11292 buffer[i] = args[j];
11295 buffer = args + skip;
11297 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11300 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11301 list ARGS along with N new arguments specified as the "..."
11302 parameters. SKIP is the number of arguments in ARGS to be omitted.
11303 OLDNARGS is the number of elements in ARGS. */
11306 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11307 int skip, tree fndecl, int n, ...)
11313 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11319 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11320 along with N new arguments specified as the "..." parameters. SKIP
11321 is the number of arguments in EXP to be omitted. This function is used
11322 to do varargs-to-varargs transformations. */
11325 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11331 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11332 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11338 /* Validate a single argument ARG against a tree code CODE representing
11342 validate_arg (const_tree arg, enum tree_code code)
11346 else if (code == POINTER_TYPE)
11347 return POINTER_TYPE_P (TREE_TYPE (arg));
11348 else if (code == INTEGER_TYPE)
11349 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11350 return code == TREE_CODE (TREE_TYPE (arg));
11353 /* This function validates the types of a function call argument list
11354 against a specified list of tree_codes. If the last specifier is a 0,
11355 that represents an ellipses, otherwise the last specifier must be a
11358 This is the GIMPLE version of validate_arglist. Eventually we want to
11359 completely convert builtins.c to work from GIMPLEs and the tree based
11360 validate_arglist will then be removed. */
11363 validate_gimple_arglist (const_gimple call, ...)
11365 enum tree_code code;
11371 va_start (ap, call);
11376 code = (enum tree_code) va_arg (ap, int);
11380 /* This signifies an ellipses, any further arguments are all ok. */
11384 /* This signifies an endlink, if no arguments remain, return
11385 true, otherwise return false. */
11386 res = (i == gimple_call_num_args (call));
11389 /* If no parameters remain or the parameter's code does not
11390 match the specified code, return false. Otherwise continue
11391 checking any remaining arguments. */
11392 arg = gimple_call_arg (call, i++);
11393 if (!validate_arg (arg, code))
11400 /* We need gotos here since we can only have one VA_CLOSE in a
11408 /* This function validates the types of a function call argument list
11409 against a specified list of tree_codes. If the last specifier is a 0,
11410 that represents an ellipses, otherwise the last specifier must be a
11414 validate_arglist (const_tree callexpr, ...)
11416 enum tree_code code;
11419 const_call_expr_arg_iterator iter;
11422 va_start (ap, callexpr);
11423 init_const_call_expr_arg_iterator (callexpr, &iter);
11427 code = (enum tree_code) va_arg (ap, int);
11431 /* This signifies an ellipses, any further arguments are all ok. */
11435 /* This signifies an endlink, if no arguments remain, return
11436 true, otherwise return false. */
11437 res = !more_const_call_expr_args_p (&iter);
11440 /* If no parameters remain or the parameter's code does not
11441 match the specified code, return false. Otherwise continue
11442 checking any remaining arguments. */
11443 arg = next_const_call_expr_arg (&iter);
11444 if (!validate_arg (arg, code))
11451 /* We need gotos here since we can only have one VA_CLOSE in a
11459 /* Default target-specific builtin expander that does nothing. */
11462 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11463 rtx target ATTRIBUTE_UNUSED,
11464 rtx subtarget ATTRIBUTE_UNUSED,
11465 enum machine_mode mode ATTRIBUTE_UNUSED,
11466 int ignore ATTRIBUTE_UNUSED)
11471 /* Returns true is EXP represents data that would potentially reside
11472 in a readonly section. */
11475 readonly_data_expr (tree exp)
11479 if (TREE_CODE (exp) != ADDR_EXPR)
11482 exp = get_base_address (TREE_OPERAND (exp, 0));
11486 /* Make sure we call decl_readonly_section only for trees it
11487 can handle (since it returns true for everything it doesn't
11489 if (TREE_CODE (exp) == STRING_CST
11490 || TREE_CODE (exp) == CONSTRUCTOR
11491 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11492 return decl_readonly_section (exp, 0);
11497 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11498 to the call, and TYPE is its return type.
11500 Return NULL_TREE if no simplification was possible, otherwise return the
11501 simplified form of the call as a tree.
11503 The simplified form may be a constant or other expression which
11504 computes the same value, but in a more efficient manner (including
11505 calls to other builtin functions).
11507 The call may contain arguments which need to be evaluated, but
11508 which are not useful to determine the result of the call. In
11509 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11510 COMPOUND_EXPR will be an argument which must be evaluated.
11511 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11512 COMPOUND_EXPR in the chain will contain the tree for the simplified
11513 form of the builtin function call. */
11516 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11518 if (!validate_arg (s1, POINTER_TYPE)
11519 || !validate_arg (s2, POINTER_TYPE))
11524 const char *p1, *p2;
11526 p2 = c_getstr (s2);
11530 p1 = c_getstr (s1);
11533 const char *r = strstr (p1, p2);
11537 return build_int_cst (TREE_TYPE (s1), 0);
11539 /* Return an offset into the constant string argument. */
11540 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11541 return fold_convert_loc (loc, type, tem);
11544 /* The argument is const char *, and the result is char *, so we need
11545 a type conversion here to avoid a warning. */
11547 return fold_convert_loc (loc, type, s1);
11552 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11556 /* New argument list transforming strstr(s1, s2) to
11557 strchr(s1, s2[0]). */
11558 return build_call_expr_loc (loc, fn, 2, s1,
11559 build_int_cst (integer_type_node, p2[0]));
11563 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11564 the call, and TYPE is its return type.
11566 Return NULL_TREE if no simplification was possible, otherwise return the
11567 simplified form of the call as a tree.
11569 The simplified form may be a constant or other expression which
11570 computes the same value, but in a more efficient manner (including
11571 calls to other builtin functions).
11573 The call may contain arguments which need to be evaluated, but
11574 which are not useful to determine the result of the call. In
11575 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11576 COMPOUND_EXPR will be an argument which must be evaluated.
11577 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11578 COMPOUND_EXPR in the chain will contain the tree for the simplified
11579 form of the builtin function call. */
11582 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11584 if (!validate_arg (s1, POINTER_TYPE)
11585 || !validate_arg (s2, INTEGER_TYPE))
11591 if (TREE_CODE (s2) != INTEGER_CST)
11594 p1 = c_getstr (s1);
11601 if (target_char_cast (s2, &c))
11604 r = strchr (p1, c);
11607 return build_int_cst (TREE_TYPE (s1), 0);
11609 /* Return an offset into the constant string argument. */
11610 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11611 return fold_convert_loc (loc, type, tem);
11617 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11618 the call, and TYPE is its return type.
11620 Return NULL_TREE if no simplification was possible, otherwise return the
11621 simplified form of the call as a tree.
11623 The simplified form may be a constant or other expression which
11624 computes the same value, but in a more efficient manner (including
11625 calls to other builtin functions).
11627 The call may contain arguments which need to be evaluated, but
11628 which are not useful to determine the result of the call. In
11629 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11630 COMPOUND_EXPR will be an argument which must be evaluated.
11631 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11632 COMPOUND_EXPR in the chain will contain the tree for the simplified
11633 form of the builtin function call. */
11636 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11638 if (!validate_arg (s1, POINTER_TYPE)
11639 || !validate_arg (s2, INTEGER_TYPE))
11646 if (TREE_CODE (s2) != INTEGER_CST)
11649 p1 = c_getstr (s1);
11656 if (target_char_cast (s2, &c))
11659 r = strrchr (p1, c);
11662 return build_int_cst (TREE_TYPE (s1), 0);
11664 /* Return an offset into the constant string argument. */
11665 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11666 return fold_convert_loc (loc, type, tem);
11669 if (! integer_zerop (s2))
11672 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11676 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11677 return build_call_expr_loc (loc, fn, 2, s1, s2);
11681 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11682 to the call, and TYPE is its return type.
11684 Return NULL_TREE if no simplification was possible, otherwise return the
11685 simplified form of the call as a tree.
11687 The simplified form may be a constant or other expression which
11688 computes the same value, but in a more efficient manner (including
11689 calls to other builtin functions).
11691 The call may contain arguments which need to be evaluated, but
11692 which are not useful to determine the result of the call. In
11693 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11694 COMPOUND_EXPR will be an argument which must be evaluated.
11695 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11696 COMPOUND_EXPR in the chain will contain the tree for the simplified
11697 form of the builtin function call. */
11700 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11702 if (!validate_arg (s1, POINTER_TYPE)
11703 || !validate_arg (s2, POINTER_TYPE))
11708 const char *p1, *p2;
11710 p2 = c_getstr (s2);
11714 p1 = c_getstr (s1);
11717 const char *r = strpbrk (p1, p2);
11721 return build_int_cst (TREE_TYPE (s1), 0);
11723 /* Return an offset into the constant string argument. */
11724 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11725 return fold_convert_loc (loc, type, tem);
11729 /* strpbrk(x, "") == NULL.
11730 Evaluate and ignore s1 in case it had side-effects. */
11731 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11734 return NULL_TREE; /* Really call strpbrk. */
11736 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11740 /* New argument list transforming strpbrk(s1, s2) to
11741 strchr(s1, s2[0]). */
11742 return build_call_expr_loc (loc, fn, 2, s1,
11743 build_int_cst (integer_type_node, p2[0]));
11747 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11750 Return NULL_TREE if no simplification was possible, otherwise return the
11751 simplified form of the call as a tree.
11753 The simplified form may be a constant or other expression which
11754 computes the same value, but in a more efficient manner (including
11755 calls to other builtin functions).
11757 The call may contain arguments which need to be evaluated, but
11758 which are not useful to determine the result of the call. In
11759 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11760 COMPOUND_EXPR will be an argument which must be evaluated.
11761 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11762 COMPOUND_EXPR in the chain will contain the tree for the simplified
11763 form of the builtin function call. */
11766 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11768 if (!validate_arg (dst, POINTER_TYPE)
11769 || !validate_arg (src, POINTER_TYPE))
11773 const char *p = c_getstr (src);
11775 /* If the string length is zero, return the dst parameter. */
11776 if (p && *p == '\0')
11779 if (optimize_insn_for_speed_p ())
11781 /* See if we can store by pieces into (dst + strlen(dst)). */
11783 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11784 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11786 if (!strlen_fn || !strcpy_fn)
11789 /* If we don't have a movstr we don't want to emit an strcpy
11790 call. We have to do that if the length of the source string
11791 isn't computable (in that case we can use memcpy probably
11792 later expanding to a sequence of mov instructions). If we
11793 have movstr instructions we can emit strcpy calls. */
11796 tree len = c_strlen (src, 1);
11797 if (! len || TREE_SIDE_EFFECTS (len))
11801 /* Stabilize the argument list. */
11802 dst = builtin_save_expr (dst);
11804 /* Create strlen (dst). */
11805 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11806 /* Create (dst p+ strlen (dst)). */
11808 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11809 newdst = builtin_save_expr (newdst);
11811 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11812 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11818 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11819 arguments to the call.
11821 Return NULL_TREE if no simplification was possible, otherwise return the
11822 simplified form of the call as a tree.
11824 The simplified form may be a constant or other expression which
11825 computes the same value, but in a more efficient manner (including
11826 calls to other builtin functions).
11828 The call may contain arguments which need to be evaluated, but
11829 which are not useful to determine the result of the call. In
11830 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11831 COMPOUND_EXPR will be an argument which must be evaluated.
11832 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11833 COMPOUND_EXPR in the chain will contain the tree for the simplified
11834 form of the builtin function call. */
11837 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11839 if (!validate_arg (dst, POINTER_TYPE)
11840 || !validate_arg (src, POINTER_TYPE)
11841 || !validate_arg (len, INTEGER_TYPE))
11845 const char *p = c_getstr (src);
11847 /* If the requested length is zero, or the src parameter string
11848 length is zero, return the dst parameter. */
11849 if (integer_zerop (len) || (p && *p == '\0'))
11850 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11852 /* If the requested len is greater than or equal to the string
11853 length, call strcat. */
11854 if (TREE_CODE (len) == INTEGER_CST && p
11855 && compare_tree_int (len, strlen (p)) >= 0)
11857 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11859 /* If the replacement _DECL isn't initialized, don't do the
11864 return build_call_expr_loc (loc, fn, 2, dst, src);
11870 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11873 Return NULL_TREE if no simplification was possible, otherwise return the
11874 simplified form of the call as a tree.
11876 The simplified form may be a constant or other expression which
11877 computes the same value, but in a more efficient manner (including
11878 calls to other builtin functions).
11880 The call may contain arguments which need to be evaluated, but
11881 which are not useful to determine the result of the call. In
11882 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11883 COMPOUND_EXPR will be an argument which must be evaluated.
11884 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11885 COMPOUND_EXPR in the chain will contain the tree for the simplified
11886 form of the builtin function call. */
11889 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11891 if (!validate_arg (s1, POINTER_TYPE)
11892 || !validate_arg (s2, POINTER_TYPE))
11896 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11898 /* If both arguments are constants, evaluate at compile-time. */
11901 const size_t r = strspn (p1, p2);
11902 return size_int (r);
11905 /* If either argument is "", return NULL_TREE. */
11906 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11907 /* Evaluate and ignore both arguments in case either one has
11909 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11915 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11918 Return NULL_TREE if no simplification was possible, otherwise return the
11919 simplified form of the call as a tree.
11921 The simplified form may be a constant or other expression which
11922 computes the same value, but in a more efficient manner (including
11923 calls to other builtin functions).
11925 The call may contain arguments which need to be evaluated, but
11926 which are not useful to determine the result of the call. In
11927 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11928 COMPOUND_EXPR will be an argument which must be evaluated.
11929 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11930 COMPOUND_EXPR in the chain will contain the tree for the simplified
11931 form of the builtin function call. */
11934 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11936 if (!validate_arg (s1, POINTER_TYPE)
11937 || !validate_arg (s2, POINTER_TYPE))
11941 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11943 /* If both arguments are constants, evaluate at compile-time. */
11946 const size_t r = strcspn (p1, p2);
11947 return size_int (r);
11950 /* If the first argument is "", return NULL_TREE. */
11951 if (p1 && *p1 == '\0')
11953 /* Evaluate and ignore argument s2 in case it has
11955 return omit_one_operand_loc (loc, size_type_node,
11956 size_zero_node, s2);
11959 /* If the second argument is "", return __builtin_strlen(s1). */
11960 if (p2 && *p2 == '\0')
11962 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11964 /* If the replacement _DECL isn't initialized, don't do the
11969 return build_call_expr_loc (loc, fn, 1, s1);
11975 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11976 to the call. IGNORE is true if the value returned
11977 by the builtin will be ignored. UNLOCKED is true is true if this
11978 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11979 the known length of the string. Return NULL_TREE if no simplification
11983 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11984 bool ignore, bool unlocked, tree len)
11986 /* If we're using an unlocked function, assume the other unlocked
11987 functions exist explicitly. */
11988 tree const fn_fputc = (unlocked
11989 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11990 : builtin_decl_implicit (BUILT_IN_FPUTC));
11991 tree const fn_fwrite = (unlocked
11992 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
11993 : builtin_decl_implicit (BUILT_IN_FWRITE));
11995 /* If the return value is used, don't do the transformation. */
11999 /* Verify the arguments in the original call. */
12000 if (!validate_arg (arg0, POINTER_TYPE)
12001 || !validate_arg (arg1, POINTER_TYPE))
12005 len = c_strlen (arg0, 0);
12007 /* Get the length of the string passed to fputs. If the length
12008 can't be determined, punt. */
12010 || TREE_CODE (len) != INTEGER_CST)
12013 switch (compare_tree_int (len, 1))
12015 case -1: /* length is 0, delete the call entirely . */
12016 return omit_one_operand_loc (loc, integer_type_node,
12017 integer_zero_node, arg1);;
12019 case 0: /* length is 1, call fputc. */
12021 const char *p = c_getstr (arg0);
12026 return build_call_expr_loc (loc, fn_fputc, 2,
12028 (integer_type_node, p[0]), arg1);
12034 case 1: /* length is greater than 1, call fwrite. */
12036 /* If optimizing for size keep fputs. */
12037 if (optimize_function_for_size_p (cfun))
12039 /* New argument list transforming fputs(string, stream) to
12040 fwrite(string, 1, len, stream). */
12042 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12043 size_one_node, len, arg1);
12048 gcc_unreachable ();
12053 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12054 produced. False otherwise. This is done so that we don't output the error
12055 or warning twice or three times. */
12058 fold_builtin_next_arg (tree exp, bool va_start_p)
12060 tree fntype = TREE_TYPE (current_function_decl);
12061 int nargs = call_expr_nargs (exp);
12064 if (!stdarg_p (fntype))
12066 error ("%<va_start%> used in function with fixed args");
12072 if (va_start_p && (nargs != 2))
12074 error ("wrong number of arguments to function %<va_start%>");
12077 arg = CALL_EXPR_ARG (exp, 1);
12079 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12080 when we checked the arguments and if needed issued a warning. */
12085 /* Evidently an out of date version of <stdarg.h>; can't validate
12086 va_start's second argument, but can still work as intended. */
12087 warning (0, "%<__builtin_next_arg%> called without an argument");
12090 else if (nargs > 1)
12092 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12095 arg = CALL_EXPR_ARG (exp, 0);
12098 if (TREE_CODE (arg) == SSA_NAME)
12099 arg = SSA_NAME_VAR (arg);
12101 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12102 or __builtin_next_arg (0) the first time we see it, after checking
12103 the arguments and if needed issuing a warning. */
12104 if (!integer_zerop (arg))
12106 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12108 /* Strip off all nops for the sake of the comparison. This
12109 is not quite the same as STRIP_NOPS. It does more.
12110 We must also strip off INDIRECT_EXPR for C++ reference
12112 while (CONVERT_EXPR_P (arg)
12113 || TREE_CODE (arg) == INDIRECT_REF)
12114 arg = TREE_OPERAND (arg, 0);
12115 if (arg != last_parm)
12117 /* FIXME: Sometimes with the tree optimizers we can get the
12118 not the last argument even though the user used the last
12119 argument. We just warn and set the arg to be the last
12120 argument so that we will get wrong-code because of
12122 warning (0, "second parameter of %<va_start%> not last named argument");
12125 /* Undefined by C99 7.15.1.4p4 (va_start):
12126 "If the parameter parmN is declared with the register storage
12127 class, with a function or array type, or with a type that is
12128 not compatible with the type that results after application of
12129 the default argument promotions, the behavior is undefined."
12131 else if (DECL_REGISTER (arg))
12132 warning (0, "undefined behaviour when second parameter of "
12133 "%<va_start%> is declared with %<register%> storage");
12135 /* We want to verify the second parameter just once before the tree
12136 optimizers are run and then avoid keeping it in the tree,
12137 as otherwise we could warn even for correct code like:
12138 void foo (int i, ...)
12139 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12141 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12143 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12149 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12150 ORIG may be null if this is a 2-argument call. We don't attempt to
12151 simplify calls with more than 3 arguments.
12153 Return NULL_TREE if no simplification was possible, otherwise return the
12154 simplified form of the call as a tree. If IGNORED is true, it means that
12155 the caller does not use the returned value of the function. */
12158 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12159 tree orig, int ignored)
12162 const char *fmt_str = NULL;
12164 /* Verify the required arguments in the original call. We deal with two
12165 types of sprintf() calls: 'sprintf (str, fmt)' and
12166 'sprintf (dest, "%s", orig)'. */
12167 if (!validate_arg (dest, POINTER_TYPE)
12168 || !validate_arg (fmt, POINTER_TYPE))
12170 if (orig && !validate_arg (orig, POINTER_TYPE))
12173 /* Check whether the format is a literal string constant. */
12174 fmt_str = c_getstr (fmt);
12175 if (fmt_str == NULL)
12179 retval = NULL_TREE;
12181 if (!init_target_chars ())
12184 /* If the format doesn't contain % args or %%, use strcpy. */
12185 if (strchr (fmt_str, target_percent) == NULL)
12187 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12192 /* Don't optimize sprintf (buf, "abc", ptr++). */
12196 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12197 'format' is known to contain no % formats. */
12198 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12200 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12203 /* If the format is "%s", use strcpy if the result isn't used. */
12204 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12207 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12212 /* Don't crash on sprintf (str1, "%s"). */
12216 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12219 retval = c_strlen (orig, 1);
12220 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12223 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12226 if (call && retval)
12228 retval = fold_convert_loc
12229 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12231 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12237 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12238 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12239 attempt to simplify calls with more than 4 arguments.
12241 Return NULL_TREE if no simplification was possible, otherwise return the
12242 simplified form of the call as a tree. If IGNORED is true, it means that
12243 the caller does not use the returned value of the function. */
12246 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12247 tree orig, int ignored)
12250 const char *fmt_str = NULL;
12251 unsigned HOST_WIDE_INT destlen;
12253 /* Verify the required arguments in the original call. We deal with two
12254 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12255 'snprintf (dest, cst, "%s", orig)'. */
12256 if (!validate_arg (dest, POINTER_TYPE)
12257 || !validate_arg (destsize, INTEGER_TYPE)
12258 || !validate_arg (fmt, POINTER_TYPE))
12260 if (orig && !validate_arg (orig, POINTER_TYPE))
12263 if (!host_integerp (destsize, 1))
12266 /* Check whether the format is a literal string constant. */
12267 fmt_str = c_getstr (fmt);
12268 if (fmt_str == NULL)
12272 retval = NULL_TREE;
12274 if (!init_target_chars ())
12277 destlen = tree_low_cst (destsize, 1);
12279 /* If the format doesn't contain % args or %%, use strcpy. */
12280 if (strchr (fmt_str, target_percent) == NULL)
12282 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12283 size_t len = strlen (fmt_str);
12285 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12289 /* We could expand this as
12290 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12292 memcpy (str, fmt_with_nul_at_cstm1, cst);
12293 but in the former case that might increase code size
12294 and in the latter case grow .rodata section too much.
12295 So punt for now. */
12296 if (len >= destlen)
12302 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12303 'format' is known to contain no % formats and
12304 strlen (fmt) < cst. */
12305 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12308 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12311 /* If the format is "%s", use strcpy if the result isn't used. */
12312 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12314 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12315 unsigned HOST_WIDE_INT origlen;
12317 /* Don't crash on snprintf (str1, cst, "%s"). */
12321 retval = c_strlen (orig, 1);
12322 if (!retval || !host_integerp (retval, 1))
12325 origlen = tree_low_cst (retval, 1);
12326 /* We could expand this as
12327 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12329 memcpy (str1, str2_with_nul_at_cstm1, cst);
12330 but in the former case that might increase code size
12331 and in the latter case grow .rodata section too much.
12332 So punt for now. */
12333 if (origlen >= destlen)
12336 /* Convert snprintf (str1, cst, "%s", str2) into
12337 strcpy (str1, str2) if strlen (str2) < cst. */
12341 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12344 retval = NULL_TREE;
12347 if (call && retval)
12349 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12350 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12351 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12357 /* Expand a call EXP to __builtin_object_size. */
12360 expand_builtin_object_size (tree exp)
12363 int object_size_type;
12364 tree fndecl = get_callee_fndecl (exp);
12366 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12368 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12370 expand_builtin_trap ();
12374 ost = CALL_EXPR_ARG (exp, 1);
12377 if (TREE_CODE (ost) != INTEGER_CST
12378 || tree_int_cst_sgn (ost) < 0
12379 || compare_tree_int (ost, 3) > 0)
12381 error ("%Klast argument of %D is not integer constant between 0 and 3",
12383 expand_builtin_trap ();
12387 object_size_type = tree_low_cst (ost, 0);
12389 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12392 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12393 FCODE is the BUILT_IN_* to use.
12394 Return NULL_RTX if we failed; the caller should emit a normal call,
12395 otherwise try to get the result in TARGET, if convenient (and in
12396 mode MODE if that's convenient). */
12399 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12400 enum built_in_function fcode)
12402 tree dest, src, len, size;
12404 if (!validate_arglist (exp,
12406 fcode == BUILT_IN_MEMSET_CHK
12407 ? INTEGER_TYPE : POINTER_TYPE,
12408 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12411 dest = CALL_EXPR_ARG (exp, 0);
12412 src = CALL_EXPR_ARG (exp, 1);
12413 len = CALL_EXPR_ARG (exp, 2);
12414 size = CALL_EXPR_ARG (exp, 3);
12416 if (! host_integerp (size, 1))
12419 if (host_integerp (len, 1) || integer_all_onesp (size))
12423 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12425 warning_at (tree_nonartificial_location (exp),
12426 0, "%Kcall to %D will always overflow destination buffer",
12427 exp, get_callee_fndecl (exp));
12432 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12433 mem{cpy,pcpy,move,set} is available. */
12436 case BUILT_IN_MEMCPY_CHK:
12437 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12439 case BUILT_IN_MEMPCPY_CHK:
12440 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12442 case BUILT_IN_MEMMOVE_CHK:
12443 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12445 case BUILT_IN_MEMSET_CHK:
12446 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12455 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12456 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12457 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12458 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12460 else if (fcode == BUILT_IN_MEMSET_CHK)
12464 unsigned int dest_align = get_pointer_alignment (dest);
12466 /* If DEST is not a pointer type, call the normal function. */
12467 if (dest_align == 0)
12470 /* If SRC and DEST are the same (and not volatile), do nothing. */
12471 if (operand_equal_p (src, dest, 0))
12475 if (fcode != BUILT_IN_MEMPCPY_CHK)
12477 /* Evaluate and ignore LEN in case it has side-effects. */
12478 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12479 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12482 expr = fold_build_pointer_plus (dest, len);
12483 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12486 /* __memmove_chk special case. */
12487 if (fcode == BUILT_IN_MEMMOVE_CHK)
12489 unsigned int src_align = get_pointer_alignment (src);
12491 if (src_align == 0)
12494 /* If src is categorized for a readonly section we can use
12495 normal __memcpy_chk. */
12496 if (readonly_data_expr (src))
12498 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12501 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12502 dest, src, len, size);
12503 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12504 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12505 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12512 /* Emit warning if a buffer overflow is detected at compile time. */
12515 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12519 location_t loc = tree_nonartificial_location (exp);
12523 case BUILT_IN_STRCPY_CHK:
12524 case BUILT_IN_STPCPY_CHK:
12525 /* For __strcat_chk the warning will be emitted only if overflowing
12526 by at least strlen (dest) + 1 bytes. */
12527 case BUILT_IN_STRCAT_CHK:
12528 len = CALL_EXPR_ARG (exp, 1);
12529 size = CALL_EXPR_ARG (exp, 2);
12532 case BUILT_IN_STRNCAT_CHK:
12533 case BUILT_IN_STRNCPY_CHK:
12534 case BUILT_IN_STPNCPY_CHK:
12535 len = CALL_EXPR_ARG (exp, 2);
12536 size = CALL_EXPR_ARG (exp, 3);
12538 case BUILT_IN_SNPRINTF_CHK:
12539 case BUILT_IN_VSNPRINTF_CHK:
12540 len = CALL_EXPR_ARG (exp, 1);
12541 size = CALL_EXPR_ARG (exp, 3);
12544 gcc_unreachable ();
12550 if (! host_integerp (size, 1) || integer_all_onesp (size))
12555 len = c_strlen (len, 1);
12556 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12559 else if (fcode == BUILT_IN_STRNCAT_CHK)
12561 tree src = CALL_EXPR_ARG (exp, 1);
12562 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12564 src = c_strlen (src, 1);
12565 if (! src || ! host_integerp (src, 1))
12567 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12568 exp, get_callee_fndecl (exp));
12571 else if (tree_int_cst_lt (src, size))
12574 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12577 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12578 exp, get_callee_fndecl (exp));
12581 /* Emit warning if a buffer overflow is detected at compile time
12582 in __sprintf_chk/__vsprintf_chk calls. */
12585 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12587 tree size, len, fmt;
12588 const char *fmt_str;
12589 int nargs = call_expr_nargs (exp);
12591 /* Verify the required arguments in the original call. */
12595 size = CALL_EXPR_ARG (exp, 2);
12596 fmt = CALL_EXPR_ARG (exp, 3);
12598 if (! host_integerp (size, 1) || integer_all_onesp (size))
12601 /* Check whether the format is a literal string constant. */
12602 fmt_str = c_getstr (fmt);
12603 if (fmt_str == NULL)
12606 if (!init_target_chars ())
12609 /* If the format doesn't contain % args or %%, we know its size. */
12610 if (strchr (fmt_str, target_percent) == 0)
12611 len = build_int_cstu (size_type_node, strlen (fmt_str));
12612 /* If the format is "%s" and first ... argument is a string literal,
12614 else if (fcode == BUILT_IN_SPRINTF_CHK
12615 && strcmp (fmt_str, target_percent_s) == 0)
12621 arg = CALL_EXPR_ARG (exp, 4);
12622 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12625 len = c_strlen (arg, 1);
12626 if (!len || ! host_integerp (len, 1))
12632 if (! tree_int_cst_lt (len, size))
12633 warning_at (tree_nonartificial_location (exp),
12634 0, "%Kcall to %D will always overflow destination buffer",
12635 exp, get_callee_fndecl (exp));
12638 /* Emit warning if a free is called with address of a variable. */
12641 maybe_emit_free_warning (tree exp)
12643 tree arg = CALL_EXPR_ARG (exp, 0);
12646 if (TREE_CODE (arg) != ADDR_EXPR)
12649 arg = get_base_address (TREE_OPERAND (arg, 0));
12650 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12653 if (SSA_VAR_P (arg))
12654 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12655 "%Kattempt to free a non-heap object %qD", exp, arg);
12657 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12658 "%Kattempt to free a non-heap object", exp);
12661 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12665 fold_builtin_object_size (tree ptr, tree ost)
12667 unsigned HOST_WIDE_INT bytes;
12668 int object_size_type;
12670 if (!validate_arg (ptr, POINTER_TYPE)
12671 || !validate_arg (ost, INTEGER_TYPE))
12676 if (TREE_CODE (ost) != INTEGER_CST
12677 || tree_int_cst_sgn (ost) < 0
12678 || compare_tree_int (ost, 3) > 0)
12681 object_size_type = tree_low_cst (ost, 0);
12683 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12684 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12685 and (size_t) 0 for types 2 and 3. */
12686 if (TREE_SIDE_EFFECTS (ptr))
12687 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12689 if (TREE_CODE (ptr) == ADDR_EXPR)
12691 bytes = compute_builtin_object_size (ptr, object_size_type);
12692 if (double_int_fits_to_tree_p (size_type_node,
12693 uhwi_to_double_int (bytes)))
12694 return build_int_cstu (size_type_node, bytes);
12696 else if (TREE_CODE (ptr) == SSA_NAME)
12698 /* If object size is not known yet, delay folding until
12699 later. Maybe subsequent passes will help determining
12701 bytes = compute_builtin_object_size (ptr, object_size_type);
12702 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12703 && double_int_fits_to_tree_p (size_type_node,
12704 uhwi_to_double_int (bytes)))
12705 return build_int_cstu (size_type_node, bytes);
12711 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12712 DEST, SRC, LEN, and SIZE are the arguments to the call.
12713 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12714 code of the builtin. If MAXLEN is not NULL, it is maximum length
12715 passed as third argument. */
12718 fold_builtin_memory_chk (location_t loc, tree fndecl,
12719 tree dest, tree src, tree len, tree size,
12720 tree maxlen, bool ignore,
12721 enum built_in_function fcode)
12725 if (!validate_arg (dest, POINTER_TYPE)
12726 || !validate_arg (src,
12727 (fcode == BUILT_IN_MEMSET_CHK
12728 ? INTEGER_TYPE : POINTER_TYPE))
12729 || !validate_arg (len, INTEGER_TYPE)
12730 || !validate_arg (size, INTEGER_TYPE))
12733 /* If SRC and DEST are the same (and not volatile), return DEST
12734 (resp. DEST+LEN for __mempcpy_chk). */
12735 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12737 if (fcode != BUILT_IN_MEMPCPY_CHK)
12738 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12742 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12743 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12747 if (! host_integerp (size, 1))
12750 if (! integer_all_onesp (size))
12752 if (! host_integerp (len, 1))
12754 /* If LEN is not constant, try MAXLEN too.
12755 For MAXLEN only allow optimizing into non-_ocs function
12756 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12757 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12759 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12761 /* (void) __mempcpy_chk () can be optimized into
12762 (void) __memcpy_chk (). */
12763 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12767 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12775 if (tree_int_cst_lt (size, maxlen))
12780 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12781 mem{cpy,pcpy,move,set} is available. */
12784 case BUILT_IN_MEMCPY_CHK:
12785 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12787 case BUILT_IN_MEMPCPY_CHK:
12788 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12790 case BUILT_IN_MEMMOVE_CHK:
12791 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12793 case BUILT_IN_MEMSET_CHK:
12794 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12803 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12806 /* Fold a call to the __st[rp]cpy_chk builtin.
12807 DEST, SRC, and SIZE are the arguments to the call.
12808 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12809 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12810 strings passed as second argument. */
12813 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12814 tree src, tree size,
12815 tree maxlen, bool ignore,
12816 enum built_in_function fcode)
12820 if (!validate_arg (dest, POINTER_TYPE)
12821 || !validate_arg (src, POINTER_TYPE)
12822 || !validate_arg (size, INTEGER_TYPE))
12825 /* If SRC and DEST are the same (and not volatile), return DEST. */
12826 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12827 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12829 if (! host_integerp (size, 1))
12832 if (! integer_all_onesp (size))
12834 len = c_strlen (src, 1);
12835 if (! len || ! host_integerp (len, 1))
12837 /* If LEN is not constant, try MAXLEN too.
12838 For MAXLEN only allow optimizing into non-_ocs function
12839 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12840 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12842 if (fcode == BUILT_IN_STPCPY_CHK)
12847 /* If return value of __stpcpy_chk is ignored,
12848 optimize into __strcpy_chk. */
12849 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12853 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12856 if (! len || TREE_SIDE_EFFECTS (len))
12859 /* If c_strlen returned something, but not a constant,
12860 transform __strcpy_chk into __memcpy_chk. */
12861 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12865 len = fold_convert_loc (loc, size_type_node, len);
12866 len = size_binop_loc (loc, PLUS_EXPR, len,
12867 build_int_cst (size_type_node, 1));
12868 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12869 build_call_expr_loc (loc, fn, 4,
12870 dest, src, len, size));
12876 if (! tree_int_cst_lt (maxlen, size))
12880 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12881 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12882 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12886 return build_call_expr_loc (loc, fn, 2, dest, src);
12889 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12890 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12891 length passed as third argument. IGNORE is true if return value can be
12892 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12895 fold_builtin_stxncpy_chk (location_t loc, tree dest, tree src,
12896 tree len, tree size, tree maxlen, bool ignore,
12897 enum built_in_function fcode)
12901 if (!validate_arg (dest, POINTER_TYPE)
12902 || !validate_arg (src, POINTER_TYPE)
12903 || !validate_arg (len, INTEGER_TYPE)
12904 || !validate_arg (size, INTEGER_TYPE))
12907 if (fcode == BUILT_IN_STPNCPY_CHK && ignore)
12909 /* If return value of __stpncpy_chk is ignored,
12910 optimize into __strncpy_chk. */
12911 fn = builtin_decl_explicit (BUILT_IN_STRNCPY_CHK);
12913 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12916 if (! host_integerp (size, 1))
12919 if (! integer_all_onesp (size))
12921 if (! host_integerp (len, 1))
12923 /* If LEN is not constant, try MAXLEN too.
12924 For MAXLEN only allow optimizing into non-_ocs function
12925 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12926 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12932 if (tree_int_cst_lt (size, maxlen))
12936 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12937 fn = builtin_decl_explicit (fcode == BUILT_IN_STPNCPY_CHK
12938 ? BUILT_IN_STPNCPY : BUILT_IN_STRNCPY);
12942 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12945 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12946 are the arguments to the call. */
12949 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12950 tree src, tree size)
12955 if (!validate_arg (dest, POINTER_TYPE)
12956 || !validate_arg (src, POINTER_TYPE)
12957 || !validate_arg (size, INTEGER_TYPE))
12960 p = c_getstr (src);
12961 /* If the SRC parameter is "", return DEST. */
12962 if (p && *p == '\0')
12963 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12965 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12968 /* If __builtin_strcat_chk is used, assume strcat is available. */
12969 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12973 return build_call_expr_loc (loc, fn, 2, dest, src);
12976 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12980 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12981 tree dest, tree src, tree len, tree size)
12986 if (!validate_arg (dest, POINTER_TYPE)
12987 || !validate_arg (src, POINTER_TYPE)
12988 || !validate_arg (size, INTEGER_TYPE)
12989 || !validate_arg (size, INTEGER_TYPE))
12992 p = c_getstr (src);
12993 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12994 if (p && *p == '\0')
12995 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12996 else if (integer_zerop (len))
12997 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12999 if (! host_integerp (size, 1))
13002 if (! integer_all_onesp (size))
13004 tree src_len = c_strlen (src, 1);
13006 && host_integerp (src_len, 1)
13007 && host_integerp (len, 1)
13008 && ! tree_int_cst_lt (len, src_len))
13010 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
13011 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
13015 return build_call_expr_loc (loc, fn, 3, dest, src, size);
13020 /* If __builtin_strncat_chk is used, assume strncat is available. */
13021 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13025 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13028 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13029 Return NULL_TREE if a normal call should be emitted rather than
13030 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13031 or BUILT_IN_VSPRINTF_CHK. */
13034 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13035 enum built_in_function fcode)
13037 tree dest, size, len, fn, fmt, flag;
13038 const char *fmt_str;
13040 /* Verify the required arguments in the original call. */
13044 if (!validate_arg (dest, POINTER_TYPE))
13047 if (!validate_arg (flag, INTEGER_TYPE))
13050 if (!validate_arg (size, INTEGER_TYPE))
13053 if (!validate_arg (fmt, POINTER_TYPE))
13056 if (! host_integerp (size, 1))
13061 if (!init_target_chars ())
13064 /* Check whether the format is a literal string constant. */
13065 fmt_str = c_getstr (fmt);
13066 if (fmt_str != NULL)
13068 /* If the format doesn't contain % args or %%, we know the size. */
13069 if (strchr (fmt_str, target_percent) == 0)
13071 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13072 len = build_int_cstu (size_type_node, strlen (fmt_str));
13074 /* If the format is "%s" and first ... argument is a string literal,
13075 we know the size too. */
13076 else if (fcode == BUILT_IN_SPRINTF_CHK
13077 && strcmp (fmt_str, target_percent_s) == 0)
13084 if (validate_arg (arg, POINTER_TYPE))
13086 len = c_strlen (arg, 1);
13087 if (! len || ! host_integerp (len, 1))
13094 if (! integer_all_onesp (size))
13096 if (! len || ! tree_int_cst_lt (len, size))
13100 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13101 or if format doesn't contain % chars or is "%s". */
13102 if (! integer_zerop (flag))
13104 if (fmt_str == NULL)
13106 if (strchr (fmt_str, target_percent) != NULL
13107 && strcmp (fmt_str, target_percent_s))
13111 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13112 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13113 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13117 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13120 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13121 a normal call should be emitted rather than expanding the function
13122 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13125 fold_builtin_sprintf_chk (location_t loc, tree exp,
13126 enum built_in_function fcode)
13128 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13129 CALL_EXPR_ARGP (exp), fcode);
13132 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13133 NULL_TREE if a normal call should be emitted rather than expanding
13134 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13135 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13136 passed as second argument. */
13139 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13140 tree maxlen, enum built_in_function fcode)
13142 tree dest, size, len, fn, fmt, flag;
13143 const char *fmt_str;
13145 /* Verify the required arguments in the original call. */
13149 if (!validate_arg (dest, POINTER_TYPE))
13152 if (!validate_arg (len, INTEGER_TYPE))
13155 if (!validate_arg (flag, INTEGER_TYPE))
13158 if (!validate_arg (size, INTEGER_TYPE))
13161 if (!validate_arg (fmt, POINTER_TYPE))
13164 if (! host_integerp (size, 1))
13167 if (! integer_all_onesp (size))
13169 if (! host_integerp (len, 1))
13171 /* If LEN is not constant, try MAXLEN too.
13172 For MAXLEN only allow optimizing into non-_ocs function
13173 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13174 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13180 if (tree_int_cst_lt (size, maxlen))
13184 if (!init_target_chars ())
13187 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13188 or if format doesn't contain % chars or is "%s". */
13189 if (! integer_zerop (flag))
13191 fmt_str = c_getstr (fmt);
13192 if (fmt_str == NULL)
13194 if (strchr (fmt_str, target_percent) != NULL
13195 && strcmp (fmt_str, target_percent_s))
13199 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13201 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13202 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13206 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13209 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13210 a normal call should be emitted rather than expanding the function
13211 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13212 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13213 passed as second argument. */
13216 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13217 enum built_in_function fcode)
13219 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13220 CALL_EXPR_ARGP (exp), maxlen, fcode);
13223 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13224 FMT and ARG are the arguments to the call; we don't fold cases with
13225 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13227 Return NULL_TREE if no simplification was possible, otherwise return the
13228 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13229 code of the function to be simplified. */
13232 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13233 tree arg, bool ignore,
13234 enum built_in_function fcode)
13236 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13237 const char *fmt_str = NULL;
13239 /* If the return value is used, don't do the transformation. */
13243 /* Verify the required arguments in the original call. */
13244 if (!validate_arg (fmt, POINTER_TYPE))
13247 /* Check whether the format is a literal string constant. */
13248 fmt_str = c_getstr (fmt);
13249 if (fmt_str == NULL)
13252 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13254 /* If we're using an unlocked function, assume the other
13255 unlocked functions exist explicitly. */
13256 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13257 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13261 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13262 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13265 if (!init_target_chars ())
13268 if (strcmp (fmt_str, target_percent_s) == 0
13269 || strchr (fmt_str, target_percent) == NULL)
13273 if (strcmp (fmt_str, target_percent_s) == 0)
13275 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13278 if (!arg || !validate_arg (arg, POINTER_TYPE))
13281 str = c_getstr (arg);
13287 /* The format specifier doesn't contain any '%' characters. */
13288 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13294 /* If the string was "", printf does nothing. */
13295 if (str[0] == '\0')
13296 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13298 /* If the string has length of 1, call putchar. */
13299 if (str[1] == '\0')
13301 /* Given printf("c"), (where c is any one character,)
13302 convert "c"[0] to an int and pass that to the replacement
13304 newarg = build_int_cst (integer_type_node, str[0]);
13306 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13310 /* If the string was "string\n", call puts("string"). */
13311 size_t len = strlen (str);
13312 if ((unsigned char)str[len - 1] == target_newline
13313 && (size_t) (int) len == len
13317 tree offset_node, string_cst;
13319 /* Create a NUL-terminated string that's one char shorter
13320 than the original, stripping off the trailing '\n'. */
13321 newarg = build_string_literal (len, str);
13322 string_cst = string_constant (newarg, &offset_node);
13323 gcc_checking_assert (string_cst
13324 && (TREE_STRING_LENGTH (string_cst)
13326 && integer_zerop (offset_node)
13328 TREE_STRING_POINTER (string_cst)[len - 1]
13329 == target_newline);
13330 /* build_string_literal creates a new STRING_CST,
13331 modify it in place to avoid double copying. */
13332 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13333 newstr[len - 1] = '\0';
13335 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13338 /* We'd like to arrange to call fputs(string,stdout) here,
13339 but we need stdout and don't have a way to get it yet. */
13344 /* The other optimizations can be done only on the non-va_list variants. */
13345 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13348 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13349 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13351 if (!arg || !validate_arg (arg, POINTER_TYPE))
13354 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13357 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13358 else if (strcmp (fmt_str, target_percent_c) == 0)
13360 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13363 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13369 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13372 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13373 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13374 more than 3 arguments, and ARG may be null in the 2-argument case.
13376 Return NULL_TREE if no simplification was possible, otherwise return the
13377 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13378 code of the function to be simplified. */
13381 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13382 tree fmt, tree arg, bool ignore,
13383 enum built_in_function fcode)
13385 tree fn_fputc, fn_fputs, call = NULL_TREE;
13386 const char *fmt_str = NULL;
13388 /* If the return value is used, don't do the transformation. */
13392 /* Verify the required arguments in the original call. */
13393 if (!validate_arg (fp, POINTER_TYPE))
13395 if (!validate_arg (fmt, POINTER_TYPE))
13398 /* Check whether the format is a literal string constant. */
13399 fmt_str = c_getstr (fmt);
13400 if (fmt_str == NULL)
13403 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13405 /* If we're using an unlocked function, assume the other
13406 unlocked functions exist explicitly. */
13407 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13408 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13412 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13413 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13416 if (!init_target_chars ())
13419 /* If the format doesn't contain % args or %%, use strcpy. */
13420 if (strchr (fmt_str, target_percent) == NULL)
13422 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13426 /* If the format specifier was "", fprintf does nothing. */
13427 if (fmt_str[0] == '\0')
13429 /* If FP has side-effects, just wait until gimplification is
13431 if (TREE_SIDE_EFFECTS (fp))
13434 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13437 /* When "string" doesn't contain %, replace all cases of
13438 fprintf (fp, string) with fputs (string, fp). The fputs
13439 builtin will take care of special cases like length == 1. */
13441 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13444 /* The other optimizations can be done only on the non-va_list variants. */
13445 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13448 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13449 else if (strcmp (fmt_str, target_percent_s) == 0)
13451 if (!arg || !validate_arg (arg, POINTER_TYPE))
13454 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13457 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13458 else if (strcmp (fmt_str, target_percent_c) == 0)
13460 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13463 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13468 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13471 /* Initialize format string characters in the target charset. */
13474 init_target_chars (void)
13479 target_newline = lang_hooks.to_target_charset ('\n');
13480 target_percent = lang_hooks.to_target_charset ('%');
13481 target_c = lang_hooks.to_target_charset ('c');
13482 target_s = lang_hooks.to_target_charset ('s');
13483 if (target_newline == 0 || target_percent == 0 || target_c == 0
13487 target_percent_c[0] = target_percent;
13488 target_percent_c[1] = target_c;
13489 target_percent_c[2] = '\0';
13491 target_percent_s[0] = target_percent;
13492 target_percent_s[1] = target_s;
13493 target_percent_s[2] = '\0';
13495 target_percent_s_newline[0] = target_percent;
13496 target_percent_s_newline[1] = target_s;
13497 target_percent_s_newline[2] = target_newline;
13498 target_percent_s_newline[3] = '\0';
13505 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13506 and no overflow/underflow occurred. INEXACT is true if M was not
13507 exactly calculated. TYPE is the tree type for the result. This
13508 function assumes that you cleared the MPFR flags and then
13509 calculated M to see if anything subsequently set a flag prior to
13510 entering this function. Return NULL_TREE if any checks fail. */
13513 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13515 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13516 overflow/underflow occurred. If -frounding-math, proceed iff the
13517 result of calling FUNC was exact. */
13518 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13519 && (!flag_rounding_math || !inexact))
13521 REAL_VALUE_TYPE rr;
13523 real_from_mpfr (&rr, m, type, GMP_RNDN);
13524 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13525 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13526 but the mpft_t is not, then we underflowed in the
13528 if (real_isfinite (&rr)
13529 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13531 REAL_VALUE_TYPE rmode;
13533 real_convert (&rmode, TYPE_MODE (type), &rr);
13534 /* Proceed iff the specified mode can hold the value. */
13535 if (real_identical (&rmode, &rr))
13536 return build_real (type, rmode);
13542 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13543 number and no overflow/underflow occurred. INEXACT is true if M
13544 was not exactly calculated. TYPE is the tree type for the result.
13545 This function assumes that you cleared the MPFR flags and then
13546 calculated M to see if anything subsequently set a flag prior to
13547 entering this function. Return NULL_TREE if any checks fail, if
13548 FORCE_CONVERT is true, then bypass the checks. */
13551 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13553 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13554 overflow/underflow occurred. If -frounding-math, proceed iff the
13555 result of calling FUNC was exact. */
13557 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13558 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13559 && (!flag_rounding_math || !inexact)))
13561 REAL_VALUE_TYPE re, im;
13563 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13564 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13565 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13566 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13567 but the mpft_t is not, then we underflowed in the
13570 || (real_isfinite (&re) && real_isfinite (&im)
13571 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13572 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13574 REAL_VALUE_TYPE re_mode, im_mode;
13576 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13577 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13578 /* Proceed iff the specified mode can hold the value. */
13580 || (real_identical (&re_mode, &re)
13581 && real_identical (&im_mode, &im)))
13582 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13583 build_real (TREE_TYPE (type), im_mode));
13589 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13590 FUNC on it and return the resulting value as a tree with type TYPE.
13591 If MIN and/or MAX are not NULL, then the supplied ARG must be
13592 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13593 acceptable values, otherwise they are not. The mpfr precision is
13594 set to the precision of TYPE. We assume that function FUNC returns
13595 zero if the result could be calculated exactly within the requested
13599 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13600 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13603 tree result = NULL_TREE;
13607 /* To proceed, MPFR must exactly represent the target floating point
13608 format, which only happens when the target base equals two. */
13609 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13610 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13612 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13614 if (real_isfinite (ra)
13615 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13616 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13618 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13619 const int prec = fmt->p;
13620 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13624 mpfr_init2 (m, prec);
13625 mpfr_from_real (m, ra, GMP_RNDN);
13626 mpfr_clear_flags ();
13627 inexact = func (m, m, rnd);
13628 result = do_mpfr_ckconv (m, type, inexact);
13636 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13637 FUNC on it and return the resulting value as a tree with type TYPE.
13638 The mpfr precision is set to the precision of TYPE. We assume that
13639 function FUNC returns zero if the result could be calculated
13640 exactly within the requested precision. */
13643 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13644 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13646 tree result = NULL_TREE;
13651 /* To proceed, MPFR must exactly represent the target floating point
13652 format, which only happens when the target base equals two. */
13653 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13654 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13655 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13657 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13658 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13660 if (real_isfinite (ra1) && real_isfinite (ra2))
13662 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13663 const int prec = fmt->p;
13664 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13668 mpfr_inits2 (prec, m1, m2, NULL);
13669 mpfr_from_real (m1, ra1, GMP_RNDN);
13670 mpfr_from_real (m2, ra2, GMP_RNDN);
13671 mpfr_clear_flags ();
13672 inexact = func (m1, m1, m2, rnd);
13673 result = do_mpfr_ckconv (m1, type, inexact);
13674 mpfr_clears (m1, m2, NULL);
13681 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13682 FUNC on it and return the resulting value as a tree with type TYPE.
13683 The mpfr precision is set to the precision of TYPE. We assume that
13684 function FUNC returns zero if the result could be calculated
13685 exactly within the requested precision. */
13688 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13689 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13691 tree result = NULL_TREE;
13697 /* To proceed, MPFR must exactly represent the target floating point
13698 format, which only happens when the target base equals two. */
13699 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13700 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13701 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13702 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13704 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13705 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13706 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13708 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13710 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13711 const int prec = fmt->p;
13712 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13716 mpfr_inits2 (prec, m1, m2, m3, NULL);
13717 mpfr_from_real (m1, ra1, GMP_RNDN);
13718 mpfr_from_real (m2, ra2, GMP_RNDN);
13719 mpfr_from_real (m3, ra3, GMP_RNDN);
13720 mpfr_clear_flags ();
13721 inexact = func (m1, m1, m2, m3, rnd);
13722 result = do_mpfr_ckconv (m1, type, inexact);
13723 mpfr_clears (m1, m2, m3, NULL);
13730 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13731 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13732 If ARG_SINP and ARG_COSP are NULL then the result is returned
13733 as a complex value.
13734 The type is taken from the type of ARG and is used for setting the
13735 precision of the calculation and results. */
13738 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13740 tree const type = TREE_TYPE (arg);
13741 tree result = NULL_TREE;
13745 /* To proceed, MPFR must exactly represent the target floating point
13746 format, which only happens when the target base equals two. */
13747 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13748 && TREE_CODE (arg) == REAL_CST
13749 && !TREE_OVERFLOW (arg))
13751 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13753 if (real_isfinite (ra))
13755 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13756 const int prec = fmt->p;
13757 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13758 tree result_s, result_c;
13762 mpfr_inits2 (prec, m, ms, mc, NULL);
13763 mpfr_from_real (m, ra, GMP_RNDN);
13764 mpfr_clear_flags ();
13765 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13766 result_s = do_mpfr_ckconv (ms, type, inexact);
13767 result_c = do_mpfr_ckconv (mc, type, inexact);
13768 mpfr_clears (m, ms, mc, NULL);
13769 if (result_s && result_c)
13771 /* If we are to return in a complex value do so. */
13772 if (!arg_sinp && !arg_cosp)
13773 return build_complex (build_complex_type (type),
13774 result_c, result_s);
13776 /* Dereference the sin/cos pointer arguments. */
13777 arg_sinp = build_fold_indirect_ref (arg_sinp);
13778 arg_cosp = build_fold_indirect_ref (arg_cosp);
13779 /* Proceed if valid pointer type were passed in. */
13780 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13781 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13783 /* Set the values. */
13784 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13786 TREE_SIDE_EFFECTS (result_s) = 1;
13787 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13789 TREE_SIDE_EFFECTS (result_c) = 1;
13790 /* Combine the assignments into a compound expr. */
13791 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13792 result_s, result_c));
13800 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13801 two-argument mpfr order N Bessel function FUNC on them and return
13802 the resulting value as a tree with type TYPE. The mpfr precision
13803 is set to the precision of TYPE. We assume that function FUNC
13804 returns zero if the result could be calculated exactly within the
13805 requested precision. */
13807 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13808 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13809 const REAL_VALUE_TYPE *min, bool inclusive)
13811 tree result = NULL_TREE;
13816 /* To proceed, MPFR must exactly represent the target floating point
13817 format, which only happens when the target base equals two. */
13818 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13819 && host_integerp (arg1, 0)
13820 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13822 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13823 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13826 && real_isfinite (ra)
13827 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13829 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13830 const int prec = fmt->p;
13831 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13835 mpfr_init2 (m, prec);
13836 mpfr_from_real (m, ra, GMP_RNDN);
13837 mpfr_clear_flags ();
13838 inexact = func (m, n, m, rnd);
13839 result = do_mpfr_ckconv (m, type, inexact);
13847 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13848 the pointer *(ARG_QUO) and return the result. The type is taken
13849 from the type of ARG0 and is used for setting the precision of the
13850 calculation and results. */
13853 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13855 tree const type = TREE_TYPE (arg0);
13856 tree result = NULL_TREE;
13861 /* To proceed, MPFR must exactly represent the target floating point
13862 format, which only happens when the target base equals two. */
13863 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13864 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13865 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13867 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13868 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13870 if (real_isfinite (ra0) && real_isfinite (ra1))
13872 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13873 const int prec = fmt->p;
13874 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13879 mpfr_inits2 (prec, m0, m1, NULL);
13880 mpfr_from_real (m0, ra0, GMP_RNDN);
13881 mpfr_from_real (m1, ra1, GMP_RNDN);
13882 mpfr_clear_flags ();
13883 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13884 /* Remquo is independent of the rounding mode, so pass
13885 inexact=0 to do_mpfr_ckconv(). */
13886 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13887 mpfr_clears (m0, m1, NULL);
13890 /* MPFR calculates quo in the host's long so it may
13891 return more bits in quo than the target int can hold
13892 if sizeof(host long) > sizeof(target int). This can
13893 happen even for native compilers in LP64 mode. In
13894 these cases, modulo the quo value with the largest
13895 number that the target int can hold while leaving one
13896 bit for the sign. */
13897 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13898 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13900 /* Dereference the quo pointer argument. */
13901 arg_quo = build_fold_indirect_ref (arg_quo);
13902 /* Proceed iff a valid pointer type was passed in. */
13903 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13905 /* Set the value. */
13907 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13908 build_int_cst (TREE_TYPE (arg_quo),
13910 TREE_SIDE_EFFECTS (result_quo) = 1;
13911 /* Combine the quo assignment with the rem. */
13912 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13913 result_quo, result_rem));
13921 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13922 resulting value as a tree with type TYPE. The mpfr precision is
13923 set to the precision of TYPE. We assume that this mpfr function
13924 returns zero if the result could be calculated exactly within the
13925 requested precision. In addition, the integer pointer represented
13926 by ARG_SG will be dereferenced and set to the appropriate signgam
13930 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13932 tree result = NULL_TREE;
13936 /* To proceed, MPFR must exactly represent the target floating point
13937 format, which only happens when the target base equals two. Also
13938 verify ARG is a constant and that ARG_SG is an int pointer. */
13939 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13940 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13941 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13942 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13944 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13946 /* In addition to NaN and Inf, the argument cannot be zero or a
13947 negative integer. */
13948 if (real_isfinite (ra)
13949 && ra->cl != rvc_zero
13950 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13952 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13953 const int prec = fmt->p;
13954 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13959 mpfr_init2 (m, prec);
13960 mpfr_from_real (m, ra, GMP_RNDN);
13961 mpfr_clear_flags ();
13962 inexact = mpfr_lgamma (m, &sg, m, rnd);
13963 result_lg = do_mpfr_ckconv (m, type, inexact);
13969 /* Dereference the arg_sg pointer argument. */
13970 arg_sg = build_fold_indirect_ref (arg_sg);
13971 /* Assign the signgam value into *arg_sg. */
13972 result_sg = fold_build2 (MODIFY_EXPR,
13973 TREE_TYPE (arg_sg), arg_sg,
13974 build_int_cst (TREE_TYPE (arg_sg), sg));
13975 TREE_SIDE_EFFECTS (result_sg) = 1;
13976 /* Combine the signgam assignment with the lgamma result. */
13977 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13978 result_sg, result_lg));
13986 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13987 function FUNC on it and return the resulting value as a tree with
13988 type TYPE. The mpfr precision is set to the precision of TYPE. We
13989 assume that function FUNC returns zero if the result could be
13990 calculated exactly within the requested precision. */
13993 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13995 tree result = NULL_TREE;
13999 /* To proceed, MPFR must exactly represent the target floating point
14000 format, which only happens when the target base equals two. */
14001 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
14002 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
14003 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14005 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14006 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14008 if (real_isfinite (re) && real_isfinite (im))
14010 const struct real_format *const fmt =
14011 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14012 const int prec = fmt->p;
14013 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14014 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14018 mpc_init2 (m, prec);
14019 mpfr_from_real (mpc_realref(m), re, rnd);
14020 mpfr_from_real (mpc_imagref(m), im, rnd);
14021 mpfr_clear_flags ();
14022 inexact = func (m, m, crnd);
14023 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14031 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14032 mpc function FUNC on it and return the resulting value as a tree
14033 with type TYPE. The mpfr precision is set to the precision of
14034 TYPE. We assume that function FUNC returns zero if the result
14035 could be calculated exactly within the requested precision. If
14036 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14037 in the arguments and/or results. */
14040 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14041 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14043 tree result = NULL_TREE;
14048 /* To proceed, MPFR must exactly represent the target floating point
14049 format, which only happens when the target base equals two. */
14050 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14051 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14052 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14053 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14054 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14056 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14057 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14058 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14059 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14062 || (real_isfinite (re0) && real_isfinite (im0)
14063 && real_isfinite (re1) && real_isfinite (im1)))
14065 const struct real_format *const fmt =
14066 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14067 const int prec = fmt->p;
14068 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14069 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14073 mpc_init2 (m0, prec);
14074 mpc_init2 (m1, prec);
14075 mpfr_from_real (mpc_realref(m0), re0, rnd);
14076 mpfr_from_real (mpc_imagref(m0), im0, rnd);
14077 mpfr_from_real (mpc_realref(m1), re1, rnd);
14078 mpfr_from_real (mpc_imagref(m1), im1, rnd);
14079 mpfr_clear_flags ();
14080 inexact = func (m0, m0, m1, crnd);
14081 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14090 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14091 a normal call should be emitted rather than expanding the function
14092 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14095 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14097 int nargs = gimple_call_num_args (stmt);
14099 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14101 ? gimple_call_arg_ptr (stmt, 0)
14102 : &error_mark_node), fcode);
14105 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14106 a normal call should be emitted rather than expanding the function
14107 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14108 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14109 passed as second argument. */
14112 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14113 enum built_in_function fcode)
14115 int nargs = gimple_call_num_args (stmt);
14117 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14119 ? gimple_call_arg_ptr (stmt, 0)
14120 : &error_mark_node), maxlen, fcode);
14123 /* Builtins with folding operations that operate on "..." arguments
14124 need special handling; we need to store the arguments in a convenient
14125 data structure before attempting any folding. Fortunately there are
14126 only a few builtins that fall into this category. FNDECL is the
14127 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14128 result of the function call is ignored. */
14131 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14132 bool ignore ATTRIBUTE_UNUSED)
14134 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14135 tree ret = NULL_TREE;
14139 case BUILT_IN_SPRINTF_CHK:
14140 case BUILT_IN_VSPRINTF_CHK:
14141 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14144 case BUILT_IN_SNPRINTF_CHK:
14145 case BUILT_IN_VSNPRINTF_CHK:
14146 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14153 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14154 TREE_NO_WARNING (ret) = 1;
14160 /* A wrapper function for builtin folding that prevents warnings for
14161 "statement without effect" and the like, caused by removing the
14162 call node earlier than the warning is generated. */
14165 fold_call_stmt (gimple stmt, bool ignore)
14167 tree ret = NULL_TREE;
14168 tree fndecl = gimple_call_fndecl (stmt);
14169 location_t loc = gimple_location (stmt);
14171 && TREE_CODE (fndecl) == FUNCTION_DECL
14172 && DECL_BUILT_IN (fndecl)
14173 && !gimple_call_va_arg_pack_p (stmt))
14175 int nargs = gimple_call_num_args (stmt);
14176 tree *args = (nargs > 0
14177 ? gimple_call_arg_ptr (stmt, 0)
14178 : &error_mark_node);
14180 if (avoid_folding_inline_builtin (fndecl))
14182 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14184 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14188 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14189 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14191 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14194 /* Propagate location information from original call to
14195 expansion of builtin. Otherwise things like
14196 maybe_emit_chk_warning, that operate on the expansion
14197 of a builtin, will use the wrong location information. */
14198 if (gimple_has_location (stmt))
14200 tree realret = ret;
14201 if (TREE_CODE (ret) == NOP_EXPR)
14202 realret = TREE_OPERAND (ret, 0);
14203 if (CAN_HAVE_LOCATION_P (realret)
14204 && !EXPR_HAS_LOCATION (realret))
14205 SET_EXPR_LOCATION (realret, loc);
14215 /* Look up the function in builtin_decl that corresponds to DECL
14216 and set ASMSPEC as its user assembler name. DECL must be a
14217 function decl that declares a builtin. */
14220 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14223 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14224 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14227 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14228 set_user_assembler_name (builtin, asmspec);
14229 switch (DECL_FUNCTION_CODE (decl))
14231 case BUILT_IN_MEMCPY:
14232 init_block_move_fn (asmspec);
14233 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14235 case BUILT_IN_MEMSET:
14236 init_block_clear_fn (asmspec);
14237 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14239 case BUILT_IN_MEMMOVE:
14240 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14242 case BUILT_IN_MEMCMP:
14243 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14245 case BUILT_IN_ABORT:
14246 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14249 if (INT_TYPE_SIZE < BITS_PER_WORD)
14251 set_user_assembler_libfunc ("ffs", asmspec);
14252 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14253 MODE_INT, 0), "ffs");
14261 /* Return true if DECL is a builtin that expands to a constant or similarly
14264 is_simple_builtin (tree decl)
14266 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14267 switch (DECL_FUNCTION_CODE (decl))
14269 /* Builtins that expand to constants. */
14270 case BUILT_IN_CONSTANT_P:
14271 case BUILT_IN_EXPECT:
14272 case BUILT_IN_OBJECT_SIZE:
14273 case BUILT_IN_UNREACHABLE:
14274 /* Simple register moves or loads from stack. */
14275 case BUILT_IN_ASSUME_ALIGNED:
14276 case BUILT_IN_RETURN_ADDRESS:
14277 case BUILT_IN_EXTRACT_RETURN_ADDR:
14278 case BUILT_IN_FROB_RETURN_ADDR:
14279 case BUILT_IN_RETURN:
14280 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14281 case BUILT_IN_FRAME_ADDRESS:
14282 case BUILT_IN_VA_END:
14283 case BUILT_IN_STACK_SAVE:
14284 case BUILT_IN_STACK_RESTORE:
14285 /* Exception state returns or moves registers around. */
14286 case BUILT_IN_EH_FILTER:
14287 case BUILT_IN_EH_POINTER:
14288 case BUILT_IN_EH_COPY_VALUES:
14298 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14299 most probably expanded inline into reasonably simple code. This is a
14300 superset of is_simple_builtin. */
14302 is_inexpensive_builtin (tree decl)
14306 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14308 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14309 switch (DECL_FUNCTION_CODE (decl))
14312 case BUILT_IN_ALLOCA:
14313 case BUILT_IN_ALLOCA_WITH_ALIGN:
14314 case BUILT_IN_BSWAP32:
14315 case BUILT_IN_BSWAP64:
14317 case BUILT_IN_CLZIMAX:
14318 case BUILT_IN_CLZL:
14319 case BUILT_IN_CLZLL:
14321 case BUILT_IN_CTZIMAX:
14322 case BUILT_IN_CTZL:
14323 case BUILT_IN_CTZLL:
14325 case BUILT_IN_FFSIMAX:
14326 case BUILT_IN_FFSL:
14327 case BUILT_IN_FFSLL:
14328 case BUILT_IN_IMAXABS:
14329 case BUILT_IN_FINITE:
14330 case BUILT_IN_FINITEF:
14331 case BUILT_IN_FINITEL:
14332 case BUILT_IN_FINITED32:
14333 case BUILT_IN_FINITED64:
14334 case BUILT_IN_FINITED128:
14335 case BUILT_IN_FPCLASSIFY:
14336 case BUILT_IN_ISFINITE:
14337 case BUILT_IN_ISINF_SIGN:
14338 case BUILT_IN_ISINF:
14339 case BUILT_IN_ISINFF:
14340 case BUILT_IN_ISINFL:
14341 case BUILT_IN_ISINFD32:
14342 case BUILT_IN_ISINFD64:
14343 case BUILT_IN_ISINFD128:
14344 case BUILT_IN_ISNAN:
14345 case BUILT_IN_ISNANF:
14346 case BUILT_IN_ISNANL:
14347 case BUILT_IN_ISNAND32:
14348 case BUILT_IN_ISNAND64:
14349 case BUILT_IN_ISNAND128:
14350 case BUILT_IN_ISNORMAL:
14351 case BUILT_IN_ISGREATER:
14352 case BUILT_IN_ISGREATEREQUAL:
14353 case BUILT_IN_ISLESS:
14354 case BUILT_IN_ISLESSEQUAL:
14355 case BUILT_IN_ISLESSGREATER:
14356 case BUILT_IN_ISUNORDERED:
14357 case BUILT_IN_VA_ARG_PACK:
14358 case BUILT_IN_VA_ARG_PACK_LEN:
14359 case BUILT_IN_VA_COPY:
14360 case BUILT_IN_TRAP:
14361 case BUILT_IN_SAVEREGS:
14362 case BUILT_IN_POPCOUNTL:
14363 case BUILT_IN_POPCOUNTLL:
14364 case BUILT_IN_POPCOUNTIMAX:
14365 case BUILT_IN_POPCOUNT:
14366 case BUILT_IN_PARITYL:
14367 case BUILT_IN_PARITYLL:
14368 case BUILT_IN_PARITYIMAX:
14369 case BUILT_IN_PARITY:
14370 case BUILT_IN_LABS:
14371 case BUILT_IN_LLABS:
14372 case BUILT_IN_PREFETCH:
14376 return is_simple_builtin (decl);