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 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 in bits of EXP, a pointer valued expression.
456 The alignment returned is, by default, the alignment of the thing that
457 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
459 Otherwise, look at the expression to see if we can do better, i.e., if the
460 expression is actually pointing at an object whose alignment is tighter. */
463 get_pointer_alignment (tree exp)
467 if (TREE_CODE (exp) == ADDR_EXPR)
468 return get_object_alignment (TREE_OPERAND (exp, 0));
469 else if (TREE_CODE (exp) == SSA_NAME
470 && POINTER_TYPE_P (TREE_TYPE (exp)))
472 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
475 return BITS_PER_UNIT;
476 if (pi->misalign != 0)
477 align = (pi->misalign & -pi->misalign);
480 return align * BITS_PER_UNIT;
483 return POINTER_TYPE_P (TREE_TYPE (exp)) ? BITS_PER_UNIT : 0;
486 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
487 way, because it could contain a zero byte in the middle.
488 TREE_STRING_LENGTH is the size of the character array, not the string.
490 ONLY_VALUE should be nonzero if the result is not going to be emitted
491 into the instruction stream and zero if it is going to be expanded.
492 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
493 is returned, otherwise NULL, since
494 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
495 evaluate the side-effects.
497 The value returned is of type `ssizetype'.
499 Unfortunately, string_constant can't access the values of const char
500 arrays with initializers, so neither can we do so here. */
503 c_strlen (tree src, int only_value)
506 HOST_WIDE_INT offset;
512 if (TREE_CODE (src) == COND_EXPR
513 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
517 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
518 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
519 if (tree_int_cst_equal (len1, len2))
523 if (TREE_CODE (src) == COMPOUND_EXPR
524 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
525 return c_strlen (TREE_OPERAND (src, 1), only_value);
527 loc = EXPR_LOC_OR_HERE (src);
529 src = string_constant (src, &offset_node);
533 max = TREE_STRING_LENGTH (src) - 1;
534 ptr = TREE_STRING_POINTER (src);
536 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
538 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
539 compute the offset to the following null if we don't know where to
540 start searching for it. */
543 for (i = 0; i < max; i++)
547 /* We don't know the starting offset, but we do know that the string
548 has no internal zero bytes. We can assume that the offset falls
549 within the bounds of the string; otherwise, the programmer deserves
550 what he gets. Subtract the offset from the length of the string,
551 and return that. This would perhaps not be valid if we were dealing
552 with named arrays in addition to literal string constants. */
554 return size_diffop_loc (loc, size_int (max), offset_node);
557 /* We have a known offset into the string. Start searching there for
558 a null character if we can represent it as a single HOST_WIDE_INT. */
559 if (offset_node == 0)
561 else if (! host_integerp (offset_node, 0))
564 offset = tree_low_cst (offset_node, 0);
566 /* If the offset is known to be out of bounds, warn, and call strlen at
568 if (offset < 0 || offset > max)
570 /* Suppress multiple warnings for propagated constant strings. */
571 if (! TREE_NO_WARNING (src))
573 warning_at (loc, 0, "offset outside bounds of constant string");
574 TREE_NO_WARNING (src) = 1;
579 /* Use strlen to search for the first zero byte. Since any strings
580 constructed with build_string will have nulls appended, we win even
581 if we get handed something like (char[4])"abcd".
583 Since OFFSET is our starting index into the string, no further
584 calculation is needed. */
585 return ssize_int (strlen (ptr + offset));
588 /* Return a char pointer for a C string if it is a string constant
589 or sum of string constant and integer constant. */
596 src = string_constant (src, &offset_node);
600 if (offset_node == 0)
601 return TREE_STRING_POINTER (src);
602 else if (!host_integerp (offset_node, 1)
603 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
606 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
609 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
610 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
613 c_readstr (const char *str, enum machine_mode mode)
619 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
624 for (i = 0; i < GET_MODE_SIZE (mode); i++)
627 if (WORDS_BIG_ENDIAN)
628 j = GET_MODE_SIZE (mode) - i - 1;
629 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
630 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
631 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
633 gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
636 ch = (unsigned char) str[i];
637 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
639 return immed_double_const (c[0], c[1], mode);
642 /* Cast a target constant CST to target CHAR and if that value fits into
643 host char type, return zero and put that value into variable pointed to by
647 target_char_cast (tree cst, char *p)
649 unsigned HOST_WIDE_INT val, hostval;
651 if (TREE_CODE (cst) != INTEGER_CST
652 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
655 val = TREE_INT_CST_LOW (cst);
656 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
657 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
660 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
661 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
670 /* Similar to save_expr, but assumes that arbitrary code is not executed
671 in between the multiple evaluations. In particular, we assume that a
672 non-addressable local variable will not be modified. */
675 builtin_save_expr (tree exp)
677 if (TREE_CODE (exp) == SSA_NAME
678 || (TREE_ADDRESSABLE (exp) == 0
679 && (TREE_CODE (exp) == PARM_DECL
680 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
683 return save_expr (exp);
686 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
687 times to get the address of either a higher stack frame, or a return
688 address located within it (depending on FNDECL_CODE). */
691 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
695 #ifdef INITIAL_FRAME_ADDRESS_RTX
696 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
700 /* For a zero count with __builtin_return_address, we don't care what
701 frame address we return, because target-specific definitions will
702 override us. Therefore frame pointer elimination is OK, and using
703 the soft frame pointer is OK.
705 For a nonzero count, or a zero count with __builtin_frame_address,
706 we require a stable offset from the current frame pointer to the
707 previous one, so we must use the hard frame pointer, and
708 we must disable frame pointer elimination. */
709 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
710 tem = frame_pointer_rtx;
713 tem = hard_frame_pointer_rtx;
715 /* Tell reload not to eliminate the frame pointer. */
716 crtl->accesses_prior_frames = 1;
720 /* Some machines need special handling before we can access
721 arbitrary frames. For example, on the SPARC, we must first flush
722 all register windows to the stack. */
723 #ifdef SETUP_FRAME_ADDRESSES
725 SETUP_FRAME_ADDRESSES ();
728 /* On the SPARC, the return address is not in the frame, it is in a
729 register. There is no way to access it off of the current frame
730 pointer, but it can be accessed off the previous frame pointer by
731 reading the value from the register window save area. */
732 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
733 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
737 /* Scan back COUNT frames to the specified frame. */
738 for (i = 0; i < count; i++)
740 /* Assume the dynamic chain pointer is in the word that the
741 frame address points to, unless otherwise specified. */
742 #ifdef DYNAMIC_CHAIN_ADDRESS
743 tem = DYNAMIC_CHAIN_ADDRESS (tem);
745 tem = memory_address (Pmode, tem);
746 tem = gen_frame_mem (Pmode, tem);
747 tem = copy_to_reg (tem);
750 /* For __builtin_frame_address, return what we've got. But, on
751 the SPARC for example, we may have to add a bias. */
752 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
753 #ifdef FRAME_ADDR_RTX
754 return FRAME_ADDR_RTX (tem);
759 /* For __builtin_return_address, get the return address from that frame. */
760 #ifdef RETURN_ADDR_RTX
761 tem = RETURN_ADDR_RTX (count, tem);
763 tem = memory_address (Pmode,
764 plus_constant (tem, GET_MODE_SIZE (Pmode)));
765 tem = gen_frame_mem (Pmode, tem);
770 /* Alias set used for setjmp buffer. */
771 static alias_set_type setjmp_alias_set = -1;
773 /* Construct the leading half of a __builtin_setjmp call. Control will
774 return to RECEIVER_LABEL. This is also called directly by the SJLJ
775 exception handling code. */
778 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
780 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
784 if (setjmp_alias_set == -1)
785 setjmp_alias_set = new_alias_set ();
787 buf_addr = convert_memory_address (Pmode, buf_addr);
789 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
791 /* We store the frame pointer and the address of receiver_label in
792 the buffer and use the rest of it for the stack save area, which
793 is machine-dependent. */
795 mem = gen_rtx_MEM (Pmode, buf_addr);
796 set_mem_alias_set (mem, setjmp_alias_set);
797 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
799 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
800 set_mem_alias_set (mem, setjmp_alias_set);
802 emit_move_insn (validize_mem (mem),
803 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
805 stack_save = gen_rtx_MEM (sa_mode,
806 plus_constant (buf_addr,
807 2 * GET_MODE_SIZE (Pmode)));
808 set_mem_alias_set (stack_save, setjmp_alias_set);
809 emit_stack_save (SAVE_NONLOCAL, &stack_save);
811 /* If there is further processing to do, do it. */
812 #ifdef HAVE_builtin_setjmp_setup
813 if (HAVE_builtin_setjmp_setup)
814 emit_insn (gen_builtin_setjmp_setup (buf_addr));
817 /* We have a nonlocal label. */
818 cfun->has_nonlocal_label = 1;
821 /* Construct the trailing part of a __builtin_setjmp call. This is
822 also called directly by the SJLJ exception handling code. */
825 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
829 /* Clobber the FP when we get here, so we have to make sure it's
830 marked as used by this function. */
831 emit_use (hard_frame_pointer_rtx);
833 /* Mark the static chain as clobbered here so life information
834 doesn't get messed up for it. */
835 chain = targetm.calls.static_chain (current_function_decl, true);
836 if (chain && REG_P (chain))
837 emit_clobber (chain);
839 /* Now put in the code to restore the frame pointer, and argument
840 pointer, if needed. */
841 #ifdef HAVE_nonlocal_goto
842 if (! HAVE_nonlocal_goto)
845 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
846 /* This might change the hard frame pointer in ways that aren't
847 apparent to early optimization passes, so force a clobber. */
848 emit_clobber (hard_frame_pointer_rtx);
851 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
852 if (fixed_regs[ARG_POINTER_REGNUM])
854 #ifdef ELIMINABLE_REGS
856 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
858 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
859 if (elim_regs[i].from == ARG_POINTER_REGNUM
860 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
863 if (i == ARRAY_SIZE (elim_regs))
866 /* Now restore our arg pointer from the address at which it
867 was saved in our stack frame. */
868 emit_move_insn (crtl->args.internal_arg_pointer,
869 copy_to_reg (get_arg_pointer_save_area ()));
874 #ifdef HAVE_builtin_setjmp_receiver
875 if (HAVE_builtin_setjmp_receiver)
876 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
879 #ifdef HAVE_nonlocal_goto_receiver
880 if (HAVE_nonlocal_goto_receiver)
881 emit_insn (gen_nonlocal_goto_receiver ());
886 /* We must not allow the code we just generated to be reordered by
887 scheduling. Specifically, the update of the frame pointer must
888 happen immediately, not later. */
889 emit_insn (gen_blockage ());
892 /* __builtin_longjmp is passed a pointer to an array of five words (not
893 all will be used on all machines). It operates similarly to the C
894 library function of the same name, but is more efficient. Much of
895 the code below is copied from the handling of non-local gotos. */
898 expand_builtin_longjmp (rtx buf_addr, rtx value)
900 rtx fp, lab, stack, insn, last;
901 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
903 /* DRAP is needed for stack realign if longjmp is expanded to current
905 if (SUPPORTS_STACK_ALIGNMENT)
906 crtl->need_drap = true;
908 if (setjmp_alias_set == -1)
909 setjmp_alias_set = new_alias_set ();
911 buf_addr = convert_memory_address (Pmode, buf_addr);
913 buf_addr = force_reg (Pmode, buf_addr);
915 /* We require that the user must pass a second argument of 1, because
916 that is what builtin_setjmp will return. */
917 gcc_assert (value == const1_rtx);
919 last = get_last_insn ();
920 #ifdef HAVE_builtin_longjmp
921 if (HAVE_builtin_longjmp)
922 emit_insn (gen_builtin_longjmp (buf_addr));
926 fp = gen_rtx_MEM (Pmode, buf_addr);
927 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
928 GET_MODE_SIZE (Pmode)));
930 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
931 2 * GET_MODE_SIZE (Pmode)));
932 set_mem_alias_set (fp, setjmp_alias_set);
933 set_mem_alias_set (lab, setjmp_alias_set);
934 set_mem_alias_set (stack, setjmp_alias_set);
936 /* Pick up FP, label, and SP from the block and jump. This code is
937 from expand_goto in stmt.c; see there for detailed comments. */
938 #ifdef HAVE_nonlocal_goto
939 if (HAVE_nonlocal_goto)
940 /* We have to pass a value to the nonlocal_goto pattern that will
941 get copied into the static_chain pointer, but it does not matter
942 what that value is, because builtin_setjmp does not use it. */
943 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
947 lab = copy_to_reg (lab);
949 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
950 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
952 emit_move_insn (hard_frame_pointer_rtx, fp);
953 emit_stack_restore (SAVE_NONLOCAL, stack);
955 emit_use (hard_frame_pointer_rtx);
956 emit_use (stack_pointer_rtx);
957 emit_indirect_jump (lab);
961 /* Search backwards and mark the jump insn as a non-local goto.
962 Note that this precludes the use of __builtin_longjmp to a
963 __builtin_setjmp target in the same function. However, we've
964 already cautioned the user that these functions are for
965 internal exception handling use only. */
966 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
968 gcc_assert (insn != last);
972 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
975 else if (CALL_P (insn))
980 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
981 and the address of the save area. */
984 expand_builtin_nonlocal_goto (tree exp)
986 tree t_label, t_save_area;
987 rtx r_label, r_save_area, r_fp, r_sp, insn;
989 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
992 t_label = CALL_EXPR_ARG (exp, 0);
993 t_save_area = CALL_EXPR_ARG (exp, 1);
995 r_label = expand_normal (t_label);
996 r_label = convert_memory_address (Pmode, r_label);
997 r_save_area = expand_normal (t_save_area);
998 r_save_area = convert_memory_address (Pmode, r_save_area);
999 /* Copy the address of the save location to a register just in case it was
1000 based on the frame pointer. */
1001 r_save_area = copy_to_reg (r_save_area);
1002 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1003 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1004 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
1006 crtl->has_nonlocal_goto = 1;
1008 #ifdef HAVE_nonlocal_goto
1009 /* ??? We no longer need to pass the static chain value, afaik. */
1010 if (HAVE_nonlocal_goto)
1011 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1015 r_label = copy_to_reg (r_label);
1017 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1018 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1020 /* Restore frame pointer for containing function. */
1021 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1022 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1024 /* USE of hard_frame_pointer_rtx added for consistency;
1025 not clear if really needed. */
1026 emit_use (hard_frame_pointer_rtx);
1027 emit_use (stack_pointer_rtx);
1029 /* If the architecture is using a GP register, we must
1030 conservatively assume that the target function makes use of it.
1031 The prologue of functions with nonlocal gotos must therefore
1032 initialize the GP register to the appropriate value, and we
1033 must then make sure that this value is live at the point
1034 of the jump. (Note that this doesn't necessarily apply
1035 to targets with a nonlocal_goto pattern; they are free
1036 to implement it in their own way. Note also that this is
1037 a no-op if the GP register is a global invariant.) */
1038 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1039 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1040 emit_use (pic_offset_table_rtx);
1042 emit_indirect_jump (r_label);
1045 /* Search backwards to the jump insn and mark it as a
1047 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1051 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1054 else if (CALL_P (insn))
1061 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1062 (not all will be used on all machines) that was passed to __builtin_setjmp.
1063 It updates the stack pointer in that block to correspond to the current
1067 expand_builtin_update_setjmp_buf (rtx buf_addr)
1069 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1071 = gen_rtx_MEM (sa_mode,
1074 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1076 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1079 /* Expand a call to __builtin_prefetch. For a target that does not support
1080 data prefetch, evaluate the memory address argument in case it has side
1084 expand_builtin_prefetch (tree exp)
1086 tree arg0, arg1, arg2;
1090 if (!validate_arglist (exp, POINTER_TYPE, 0))
1093 arg0 = CALL_EXPR_ARG (exp, 0);
1095 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1096 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1098 nargs = call_expr_nargs (exp);
1100 arg1 = CALL_EXPR_ARG (exp, 1);
1102 arg1 = integer_zero_node;
1104 arg2 = CALL_EXPR_ARG (exp, 2);
1106 arg2 = integer_three_node;
1108 /* Argument 0 is an address. */
1109 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1111 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1112 if (TREE_CODE (arg1) != INTEGER_CST)
1114 error ("second argument to %<__builtin_prefetch%> must be a constant");
1115 arg1 = integer_zero_node;
1117 op1 = expand_normal (arg1);
1118 /* Argument 1 must be either zero or one. */
1119 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1121 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1126 /* Argument 2 (locality) must be a compile-time constant int. */
1127 if (TREE_CODE (arg2) != INTEGER_CST)
1129 error ("third argument to %<__builtin_prefetch%> must be a constant");
1130 arg2 = integer_zero_node;
1132 op2 = expand_normal (arg2);
1133 /* Argument 2 must be 0, 1, 2, or 3. */
1134 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1136 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1140 #ifdef HAVE_prefetch
1143 struct expand_operand ops[3];
1145 create_address_operand (&ops[0], op0);
1146 create_integer_operand (&ops[1], INTVAL (op1));
1147 create_integer_operand (&ops[2], INTVAL (op2));
1148 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1153 /* Don't do anything with direct references to volatile memory, but
1154 generate code to handle other side effects. */
1155 if (!MEM_P (op0) && side_effects_p (op0))
1159 /* Get a MEM rtx for expression EXP which is the address of an operand
1160 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1161 the maximum length of the block of memory that might be accessed or
1165 get_memory_rtx (tree exp, tree len)
1167 tree orig_exp = exp;
1171 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1172 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1173 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1174 exp = TREE_OPERAND (exp, 0);
1176 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1177 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1179 /* Get an expression we can use to find the attributes to assign to MEM.
1180 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1181 we can. First remove any nops. */
1182 while (CONVERT_EXPR_P (exp)
1183 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1184 exp = TREE_OPERAND (exp, 0);
1187 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1188 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1189 && host_integerp (TREE_OPERAND (exp, 1), 0)
1190 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1191 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1192 else if (TREE_CODE (exp) == ADDR_EXPR)
1193 exp = TREE_OPERAND (exp, 0);
1194 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1195 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1199 /* Honor attributes derived from exp, except for the alias set
1200 (as builtin stringops may alias with anything) and the size
1201 (as stringops may access multiple array elements). */
1204 set_mem_attributes (mem, exp, 0);
1207 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1209 /* Allow the string and memory builtins to overflow from one
1210 field into another, see http://gcc.gnu.org/PR23561.
1211 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1212 memory accessed by the string or memory builtin will fit
1213 within the field. */
1214 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1216 tree mem_expr = MEM_EXPR (mem);
1217 HOST_WIDE_INT offset = -1, length = -1;
1220 while (TREE_CODE (inner) == ARRAY_REF
1221 || CONVERT_EXPR_P (inner)
1222 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1223 || TREE_CODE (inner) == SAVE_EXPR)
1224 inner = TREE_OPERAND (inner, 0);
1226 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1228 if (MEM_OFFSET_KNOWN_P (mem))
1229 offset = MEM_OFFSET (mem);
1231 if (offset >= 0 && len && host_integerp (len, 0))
1232 length = tree_low_cst (len, 0);
1234 while (TREE_CODE (inner) == COMPONENT_REF)
1236 tree field = TREE_OPERAND (inner, 1);
1237 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1238 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1240 /* Bitfields are generally not byte-addressable. */
1241 gcc_assert (!DECL_BIT_FIELD (field)
1242 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1243 % BITS_PER_UNIT) == 0
1244 && host_integerp (DECL_SIZE (field), 0)
1245 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1246 % BITS_PER_UNIT) == 0));
1248 /* If we can prove that the memory starting at XEXP (mem, 0) and
1249 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1250 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1251 fields without DECL_SIZE_UNIT like flexible array members. */
1253 && DECL_SIZE_UNIT (field)
1254 && host_integerp (DECL_SIZE_UNIT (field), 0))
1257 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1260 && offset + length <= size)
1265 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1266 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1267 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1275 mem_expr = TREE_OPERAND (mem_expr, 0);
1276 inner = TREE_OPERAND (inner, 0);
1279 if (mem_expr == NULL)
1281 if (mem_expr != MEM_EXPR (mem))
1283 set_mem_expr (mem, mem_expr);
1285 set_mem_offset (mem, offset);
1287 clear_mem_offset (mem);
1290 set_mem_alias_set (mem, 0);
1291 clear_mem_size (mem);
1297 /* Built-in functions to perform an untyped call and return. */
1299 #define apply_args_mode \
1300 (this_target_builtins->x_apply_args_mode)
1301 #define apply_result_mode \
1302 (this_target_builtins->x_apply_result_mode)
1304 /* Return the size required for the block returned by __builtin_apply_args,
1305 and initialize apply_args_mode. */
1308 apply_args_size (void)
1310 static int size = -1;
1313 enum machine_mode mode;
1315 /* The values computed by this function never change. */
1318 /* The first value is the incoming arg-pointer. */
1319 size = GET_MODE_SIZE (Pmode);
1321 /* The second value is the structure value address unless this is
1322 passed as an "invisible" first argument. */
1323 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1324 size += GET_MODE_SIZE (Pmode);
1326 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1327 if (FUNCTION_ARG_REGNO_P (regno))
1329 mode = targetm.calls.get_raw_arg_mode (regno);
1331 gcc_assert (mode != VOIDmode);
1333 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1334 if (size % align != 0)
1335 size = CEIL (size, align) * align;
1336 size += GET_MODE_SIZE (mode);
1337 apply_args_mode[regno] = mode;
1341 apply_args_mode[regno] = VOIDmode;
1347 /* Return the size required for the block returned by __builtin_apply,
1348 and initialize apply_result_mode. */
1351 apply_result_size (void)
1353 static int size = -1;
1355 enum machine_mode mode;
1357 /* The values computed by this function never change. */
1362 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1363 if (targetm.calls.function_value_regno_p (regno))
1365 mode = targetm.calls.get_raw_result_mode (regno);
1367 gcc_assert (mode != VOIDmode);
1369 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1370 if (size % align != 0)
1371 size = CEIL (size, align) * align;
1372 size += GET_MODE_SIZE (mode);
1373 apply_result_mode[regno] = mode;
1376 apply_result_mode[regno] = VOIDmode;
1378 /* Allow targets that use untyped_call and untyped_return to override
1379 the size so that machine-specific information can be stored here. */
1380 #ifdef APPLY_RESULT_SIZE
1381 size = APPLY_RESULT_SIZE;
1387 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1388 /* Create a vector describing the result block RESULT. If SAVEP is true,
1389 the result block is used to save the values; otherwise it is used to
1390 restore the values. */
1393 result_vector (int savep, rtx result)
1395 int regno, size, align, nelts;
1396 enum machine_mode mode;
1398 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1401 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1402 if ((mode = apply_result_mode[regno]) != VOIDmode)
1404 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1405 if (size % align != 0)
1406 size = CEIL (size, align) * align;
1407 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1408 mem = adjust_address (result, mode, size);
1409 savevec[nelts++] = (savep
1410 ? gen_rtx_SET (VOIDmode, mem, reg)
1411 : gen_rtx_SET (VOIDmode, reg, mem));
1412 size += GET_MODE_SIZE (mode);
1414 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1416 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1418 /* Save the state required to perform an untyped call with the same
1419 arguments as were passed to the current function. */
1422 expand_builtin_apply_args_1 (void)
1425 int size, align, regno;
1426 enum machine_mode mode;
1427 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1429 /* Create a block where the arg-pointer, structure value address,
1430 and argument registers can be saved. */
1431 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1433 /* Walk past the arg-pointer and structure value address. */
1434 size = GET_MODE_SIZE (Pmode);
1435 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1436 size += GET_MODE_SIZE (Pmode);
1438 /* Save each register used in calling a function to the block. */
1439 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1440 if ((mode = apply_args_mode[regno]) != VOIDmode)
1442 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1443 if (size % align != 0)
1444 size = CEIL (size, align) * align;
1446 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1448 emit_move_insn (adjust_address (registers, mode, size), tem);
1449 size += GET_MODE_SIZE (mode);
1452 /* Save the arg pointer to the block. */
1453 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1454 #ifdef STACK_GROWS_DOWNWARD
1455 /* We need the pointer as the caller actually passed them to us, not
1456 as we might have pretended they were passed. Make sure it's a valid
1457 operand, as emit_move_insn isn't expected to handle a PLUS. */
1459 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1462 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1464 size = GET_MODE_SIZE (Pmode);
1466 /* Save the structure value address unless this is passed as an
1467 "invisible" first argument. */
1468 if (struct_incoming_value)
1470 emit_move_insn (adjust_address (registers, Pmode, size),
1471 copy_to_reg (struct_incoming_value));
1472 size += GET_MODE_SIZE (Pmode);
1475 /* Return the address of the block. */
1476 return copy_addr_to_reg (XEXP (registers, 0));
1479 /* __builtin_apply_args returns block of memory allocated on
1480 the stack into which is stored the arg pointer, structure
1481 value address, static chain, and all the registers that might
1482 possibly be used in performing a function call. The code is
1483 moved to the start of the function so the incoming values are
1487 expand_builtin_apply_args (void)
1489 /* Don't do __builtin_apply_args more than once in a function.
1490 Save the result of the first call and reuse it. */
1491 if (apply_args_value != 0)
1492 return apply_args_value;
1494 /* When this function is called, it means that registers must be
1495 saved on entry to this function. So we migrate the
1496 call to the first insn of this function. */
1501 temp = expand_builtin_apply_args_1 ();
1505 apply_args_value = temp;
1507 /* Put the insns after the NOTE that starts the function.
1508 If this is inside a start_sequence, make the outer-level insn
1509 chain current, so the code is placed at the start of the
1510 function. If internal_arg_pointer is a non-virtual pseudo,
1511 it needs to be placed after the function that initializes
1513 push_topmost_sequence ();
1514 if (REG_P (crtl->args.internal_arg_pointer)
1515 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1516 emit_insn_before (seq, parm_birth_insn);
1518 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1519 pop_topmost_sequence ();
1524 /* Perform an untyped call and save the state required to perform an
1525 untyped return of whatever value was returned by the given function. */
1528 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1530 int size, align, regno;
1531 enum machine_mode mode;
1532 rtx incoming_args, result, reg, dest, src, call_insn;
1533 rtx old_stack_level = 0;
1534 rtx call_fusage = 0;
1535 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1537 arguments = convert_memory_address (Pmode, arguments);
1539 /* Create a block where the return registers can be saved. */
1540 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1542 /* Fetch the arg pointer from the ARGUMENTS block. */
1543 incoming_args = gen_reg_rtx (Pmode);
1544 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1545 #ifndef STACK_GROWS_DOWNWARD
1546 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1547 incoming_args, 0, OPTAB_LIB_WIDEN);
1550 /* Push a new argument block and copy the arguments. Do not allow
1551 the (potential) memcpy call below to interfere with our stack
1553 do_pending_stack_adjust ();
1556 /* Save the stack with nonlocal if available. */
1557 #ifdef HAVE_save_stack_nonlocal
1558 if (HAVE_save_stack_nonlocal)
1559 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1562 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1564 /* Allocate a block of memory onto the stack and copy the memory
1565 arguments to the outgoing arguments address. We can pass TRUE
1566 as the 4th argument because we just saved the stack pointer
1567 and will restore it right after the call. */
1568 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1570 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1571 may have already set current_function_calls_alloca to true.
1572 current_function_calls_alloca won't be set if argsize is zero,
1573 so we have to guarantee need_drap is true here. */
1574 if (SUPPORTS_STACK_ALIGNMENT)
1575 crtl->need_drap = true;
1577 dest = virtual_outgoing_args_rtx;
1578 #ifndef STACK_GROWS_DOWNWARD
1579 if (CONST_INT_P (argsize))
1580 dest = plus_constant (dest, -INTVAL (argsize));
1582 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1584 dest = gen_rtx_MEM (BLKmode, dest);
1585 set_mem_align (dest, PARM_BOUNDARY);
1586 src = gen_rtx_MEM (BLKmode, incoming_args);
1587 set_mem_align (src, PARM_BOUNDARY);
1588 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1590 /* Refer to the argument block. */
1592 arguments = gen_rtx_MEM (BLKmode, arguments);
1593 set_mem_align (arguments, PARM_BOUNDARY);
1595 /* Walk past the arg-pointer and structure value address. */
1596 size = GET_MODE_SIZE (Pmode);
1598 size += GET_MODE_SIZE (Pmode);
1600 /* Restore each of the registers previously saved. Make USE insns
1601 for each of these registers for use in making the call. */
1602 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1603 if ((mode = apply_args_mode[regno]) != VOIDmode)
1605 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1606 if (size % align != 0)
1607 size = CEIL (size, align) * align;
1608 reg = gen_rtx_REG (mode, regno);
1609 emit_move_insn (reg, adjust_address (arguments, mode, size));
1610 use_reg (&call_fusage, reg);
1611 size += GET_MODE_SIZE (mode);
1614 /* Restore the structure value address unless this is passed as an
1615 "invisible" first argument. */
1616 size = GET_MODE_SIZE (Pmode);
1619 rtx value = gen_reg_rtx (Pmode);
1620 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1621 emit_move_insn (struct_value, value);
1622 if (REG_P (struct_value))
1623 use_reg (&call_fusage, struct_value);
1624 size += GET_MODE_SIZE (Pmode);
1627 /* All arguments and registers used for the call are set up by now! */
1628 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1630 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1631 and we don't want to load it into a register as an optimization,
1632 because prepare_call_address already did it if it should be done. */
1633 if (GET_CODE (function) != SYMBOL_REF)
1634 function = memory_address (FUNCTION_MODE, function);
1636 /* Generate the actual call instruction and save the return value. */
1637 #ifdef HAVE_untyped_call
1638 if (HAVE_untyped_call)
1639 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1640 result, result_vector (1, result)));
1643 #ifdef HAVE_call_value
1644 if (HAVE_call_value)
1648 /* Locate the unique return register. It is not possible to
1649 express a call that sets more than one return register using
1650 call_value; use untyped_call for that. In fact, untyped_call
1651 only needs to save the return registers in the given block. */
1652 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1653 if ((mode = apply_result_mode[regno]) != VOIDmode)
1655 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1657 valreg = gen_rtx_REG (mode, regno);
1660 emit_call_insn (GEN_CALL_VALUE (valreg,
1661 gen_rtx_MEM (FUNCTION_MODE, function),
1662 const0_rtx, NULL_RTX, const0_rtx));
1664 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1670 /* Find the CALL insn we just emitted, and attach the register usage
1672 call_insn = last_call_insn ();
1673 add_function_usage_to (call_insn, call_fusage);
1675 /* Restore the stack. */
1676 #ifdef HAVE_save_stack_nonlocal
1677 if (HAVE_save_stack_nonlocal)
1678 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1681 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1682 fixup_args_size_notes (call_insn, get_last_insn(), 0);
1686 /* Return the address of the result block. */
1687 result = copy_addr_to_reg (XEXP (result, 0));
1688 return convert_memory_address (ptr_mode, result);
1691 /* Perform an untyped return. */
1694 expand_builtin_return (rtx result)
1696 int size, align, regno;
1697 enum machine_mode mode;
1699 rtx call_fusage = 0;
1701 result = convert_memory_address (Pmode, result);
1703 apply_result_size ();
1704 result = gen_rtx_MEM (BLKmode, result);
1706 #ifdef HAVE_untyped_return
1707 if (HAVE_untyped_return)
1709 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1715 /* Restore the return value and note that each value is used. */
1717 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1718 if ((mode = apply_result_mode[regno]) != VOIDmode)
1720 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1721 if (size % align != 0)
1722 size = CEIL (size, align) * align;
1723 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1724 emit_move_insn (reg, adjust_address (result, mode, size));
1726 push_to_sequence (call_fusage);
1728 call_fusage = get_insns ();
1730 size += GET_MODE_SIZE (mode);
1733 /* Put the USE insns before the return. */
1734 emit_insn (call_fusage);
1736 /* Return whatever values was restored by jumping directly to the end
1738 expand_naked_return ();
1741 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1743 static enum type_class
1744 type_to_class (tree type)
1746 switch (TREE_CODE (type))
1748 case VOID_TYPE: return void_type_class;
1749 case INTEGER_TYPE: return integer_type_class;
1750 case ENUMERAL_TYPE: return enumeral_type_class;
1751 case BOOLEAN_TYPE: return boolean_type_class;
1752 case POINTER_TYPE: return pointer_type_class;
1753 case REFERENCE_TYPE: return reference_type_class;
1754 case OFFSET_TYPE: return offset_type_class;
1755 case REAL_TYPE: return real_type_class;
1756 case COMPLEX_TYPE: return complex_type_class;
1757 case FUNCTION_TYPE: return function_type_class;
1758 case METHOD_TYPE: return method_type_class;
1759 case RECORD_TYPE: return record_type_class;
1761 case QUAL_UNION_TYPE: return union_type_class;
1762 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1763 ? string_type_class : array_type_class);
1764 case LANG_TYPE: return lang_type_class;
1765 default: return no_type_class;
1769 /* Expand a call EXP to __builtin_classify_type. */
1772 expand_builtin_classify_type (tree exp)
1774 if (call_expr_nargs (exp))
1775 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1776 return GEN_INT (no_type_class);
1779 /* This helper macro, meant to be used in mathfn_built_in below,
1780 determines which among a set of three builtin math functions is
1781 appropriate for a given type mode. The `F' and `L' cases are
1782 automatically generated from the `double' case. */
1783 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1784 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1785 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1786 fcodel = BUILT_IN_MATHFN##L ; break;
1787 /* Similar to above, but appends _R after any F/L suffix. */
1788 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1789 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1790 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1791 fcodel = BUILT_IN_MATHFN##L_R ; break;
1793 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1794 if available. If IMPLICIT is true use the implicit builtin declaration,
1795 otherwise use the explicit declaration. If we can't do the conversion,
1799 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit_p)
1801 enum built_in_function fcode, fcodef, fcodel, fcode2;
1805 CASE_MATHFN (BUILT_IN_ACOS)
1806 CASE_MATHFN (BUILT_IN_ACOSH)
1807 CASE_MATHFN (BUILT_IN_ASIN)
1808 CASE_MATHFN (BUILT_IN_ASINH)
1809 CASE_MATHFN (BUILT_IN_ATAN)
1810 CASE_MATHFN (BUILT_IN_ATAN2)
1811 CASE_MATHFN (BUILT_IN_ATANH)
1812 CASE_MATHFN (BUILT_IN_CBRT)
1813 CASE_MATHFN (BUILT_IN_CEIL)
1814 CASE_MATHFN (BUILT_IN_CEXPI)
1815 CASE_MATHFN (BUILT_IN_COPYSIGN)
1816 CASE_MATHFN (BUILT_IN_COS)
1817 CASE_MATHFN (BUILT_IN_COSH)
1818 CASE_MATHFN (BUILT_IN_DREM)
1819 CASE_MATHFN (BUILT_IN_ERF)
1820 CASE_MATHFN (BUILT_IN_ERFC)
1821 CASE_MATHFN (BUILT_IN_EXP)
1822 CASE_MATHFN (BUILT_IN_EXP10)
1823 CASE_MATHFN (BUILT_IN_EXP2)
1824 CASE_MATHFN (BUILT_IN_EXPM1)
1825 CASE_MATHFN (BUILT_IN_FABS)
1826 CASE_MATHFN (BUILT_IN_FDIM)
1827 CASE_MATHFN (BUILT_IN_FLOOR)
1828 CASE_MATHFN (BUILT_IN_FMA)
1829 CASE_MATHFN (BUILT_IN_FMAX)
1830 CASE_MATHFN (BUILT_IN_FMIN)
1831 CASE_MATHFN (BUILT_IN_FMOD)
1832 CASE_MATHFN (BUILT_IN_FREXP)
1833 CASE_MATHFN (BUILT_IN_GAMMA)
1834 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1835 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1836 CASE_MATHFN (BUILT_IN_HYPOT)
1837 CASE_MATHFN (BUILT_IN_ILOGB)
1838 CASE_MATHFN (BUILT_IN_ICEIL)
1839 CASE_MATHFN (BUILT_IN_IFLOOR)
1840 CASE_MATHFN (BUILT_IN_INF)
1841 CASE_MATHFN (BUILT_IN_IRINT)
1842 CASE_MATHFN (BUILT_IN_IROUND)
1843 CASE_MATHFN (BUILT_IN_ISINF)
1844 CASE_MATHFN (BUILT_IN_J0)
1845 CASE_MATHFN (BUILT_IN_J1)
1846 CASE_MATHFN (BUILT_IN_JN)
1847 CASE_MATHFN (BUILT_IN_LCEIL)
1848 CASE_MATHFN (BUILT_IN_LDEXP)
1849 CASE_MATHFN (BUILT_IN_LFLOOR)
1850 CASE_MATHFN (BUILT_IN_LGAMMA)
1851 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1852 CASE_MATHFN (BUILT_IN_LLCEIL)
1853 CASE_MATHFN (BUILT_IN_LLFLOOR)
1854 CASE_MATHFN (BUILT_IN_LLRINT)
1855 CASE_MATHFN (BUILT_IN_LLROUND)
1856 CASE_MATHFN (BUILT_IN_LOG)
1857 CASE_MATHFN (BUILT_IN_LOG10)
1858 CASE_MATHFN (BUILT_IN_LOG1P)
1859 CASE_MATHFN (BUILT_IN_LOG2)
1860 CASE_MATHFN (BUILT_IN_LOGB)
1861 CASE_MATHFN (BUILT_IN_LRINT)
1862 CASE_MATHFN (BUILT_IN_LROUND)
1863 CASE_MATHFN (BUILT_IN_MODF)
1864 CASE_MATHFN (BUILT_IN_NAN)
1865 CASE_MATHFN (BUILT_IN_NANS)
1866 CASE_MATHFN (BUILT_IN_NEARBYINT)
1867 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1868 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1869 CASE_MATHFN (BUILT_IN_POW)
1870 CASE_MATHFN (BUILT_IN_POWI)
1871 CASE_MATHFN (BUILT_IN_POW10)
1872 CASE_MATHFN (BUILT_IN_REMAINDER)
1873 CASE_MATHFN (BUILT_IN_REMQUO)
1874 CASE_MATHFN (BUILT_IN_RINT)
1875 CASE_MATHFN (BUILT_IN_ROUND)
1876 CASE_MATHFN (BUILT_IN_SCALB)
1877 CASE_MATHFN (BUILT_IN_SCALBLN)
1878 CASE_MATHFN (BUILT_IN_SCALBN)
1879 CASE_MATHFN (BUILT_IN_SIGNBIT)
1880 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1881 CASE_MATHFN (BUILT_IN_SIN)
1882 CASE_MATHFN (BUILT_IN_SINCOS)
1883 CASE_MATHFN (BUILT_IN_SINH)
1884 CASE_MATHFN (BUILT_IN_SQRT)
1885 CASE_MATHFN (BUILT_IN_TAN)
1886 CASE_MATHFN (BUILT_IN_TANH)
1887 CASE_MATHFN (BUILT_IN_TGAMMA)
1888 CASE_MATHFN (BUILT_IN_TRUNC)
1889 CASE_MATHFN (BUILT_IN_Y0)
1890 CASE_MATHFN (BUILT_IN_Y1)
1891 CASE_MATHFN (BUILT_IN_YN)
1897 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1899 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1901 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1906 if (implicit_p && !builtin_decl_implicit_p (fcode2))
1909 return builtin_decl_explicit (fcode2);
1912 /* Like mathfn_built_in_1(), but always use the implicit array. */
1915 mathfn_built_in (tree type, enum built_in_function fn)
1917 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1920 /* If errno must be maintained, expand the RTL to check if the result,
1921 TARGET, of a built-in function call, EXP, is NaN, and if so set
1925 expand_errno_check (tree exp, rtx target)
1927 rtx lab = gen_label_rtx ();
1929 /* Test the result; if it is NaN, set errno=EDOM because
1930 the argument was not in the domain. */
1931 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1932 NULL_RTX, NULL_RTX, lab,
1933 /* The jump is very likely. */
1934 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1937 /* If this built-in doesn't throw an exception, set errno directly. */
1938 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1940 #ifdef GEN_ERRNO_RTX
1941 rtx errno_rtx = GEN_ERRNO_RTX;
1944 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1946 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1952 /* Make sure the library call isn't expanded as a tail call. */
1953 CALL_EXPR_TAILCALL (exp) = 0;
1955 /* We can't set errno=EDOM directly; let the library call do it.
1956 Pop the arguments right away in case the call gets deleted. */
1958 expand_call (exp, target, 0);
1963 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1964 Return NULL_RTX if a normal call should be emitted rather than expanding
1965 the function in-line. EXP is the expression that is a call to the builtin
1966 function; if convenient, the result should be placed in TARGET.
1967 SUBTARGET may be used as the target for computing one of EXP's operands. */
1970 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1972 optab builtin_optab;
1974 tree fndecl = get_callee_fndecl (exp);
1975 enum machine_mode mode;
1976 bool errno_set = false;
1979 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1982 arg = CALL_EXPR_ARG (exp, 0);
1984 switch (DECL_FUNCTION_CODE (fndecl))
1986 CASE_FLT_FN (BUILT_IN_SQRT):
1987 errno_set = ! tree_expr_nonnegative_p (arg);
1988 builtin_optab = sqrt_optab;
1990 CASE_FLT_FN (BUILT_IN_EXP):
1991 errno_set = true; builtin_optab = exp_optab; break;
1992 CASE_FLT_FN (BUILT_IN_EXP10):
1993 CASE_FLT_FN (BUILT_IN_POW10):
1994 errno_set = true; builtin_optab = exp10_optab; break;
1995 CASE_FLT_FN (BUILT_IN_EXP2):
1996 errno_set = true; builtin_optab = exp2_optab; break;
1997 CASE_FLT_FN (BUILT_IN_EXPM1):
1998 errno_set = true; builtin_optab = expm1_optab; break;
1999 CASE_FLT_FN (BUILT_IN_LOGB):
2000 errno_set = true; builtin_optab = logb_optab; break;
2001 CASE_FLT_FN (BUILT_IN_LOG):
2002 errno_set = true; builtin_optab = log_optab; break;
2003 CASE_FLT_FN (BUILT_IN_LOG10):
2004 errno_set = true; builtin_optab = log10_optab; break;
2005 CASE_FLT_FN (BUILT_IN_LOG2):
2006 errno_set = true; builtin_optab = log2_optab; break;
2007 CASE_FLT_FN (BUILT_IN_LOG1P):
2008 errno_set = true; builtin_optab = log1p_optab; break;
2009 CASE_FLT_FN (BUILT_IN_ASIN):
2010 builtin_optab = asin_optab; break;
2011 CASE_FLT_FN (BUILT_IN_ACOS):
2012 builtin_optab = acos_optab; break;
2013 CASE_FLT_FN (BUILT_IN_TAN):
2014 builtin_optab = tan_optab; break;
2015 CASE_FLT_FN (BUILT_IN_ATAN):
2016 builtin_optab = atan_optab; break;
2017 CASE_FLT_FN (BUILT_IN_FLOOR):
2018 builtin_optab = floor_optab; break;
2019 CASE_FLT_FN (BUILT_IN_CEIL):
2020 builtin_optab = ceil_optab; break;
2021 CASE_FLT_FN (BUILT_IN_TRUNC):
2022 builtin_optab = btrunc_optab; break;
2023 CASE_FLT_FN (BUILT_IN_ROUND):
2024 builtin_optab = round_optab; break;
2025 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2026 builtin_optab = nearbyint_optab;
2027 if (flag_trapping_math)
2029 /* Else fallthrough and expand as rint. */
2030 CASE_FLT_FN (BUILT_IN_RINT):
2031 builtin_optab = rint_optab; break;
2032 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2033 builtin_optab = significand_optab; break;
2038 /* Make a suitable register to place result in. */
2039 mode = TYPE_MODE (TREE_TYPE (exp));
2041 if (! flag_errno_math || ! HONOR_NANS (mode))
2044 /* Before working hard, check whether the instruction is available. */
2045 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2046 && (!errno_set || !optimize_insn_for_size_p ()))
2048 target = gen_reg_rtx (mode);
2050 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2051 need to expand the argument again. This way, we will not perform
2052 side-effects more the once. */
2053 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2055 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2059 /* Compute into TARGET.
2060 Set TARGET to wherever the result comes back. */
2061 target = expand_unop (mode, builtin_optab, op0, target, 0);
2066 expand_errno_check (exp, target);
2068 /* Output the entire sequence. */
2069 insns = get_insns ();
2075 /* If we were unable to expand via the builtin, stop the sequence
2076 (without outputting the insns) and call to the library function
2077 with the stabilized argument list. */
2081 return expand_call (exp, target, target == const0_rtx);
2084 /* Expand a call to the builtin binary math functions (pow and atan2).
2085 Return NULL_RTX if a normal call should be emitted rather than expanding the
2086 function in-line. EXP is the expression that is a call to the builtin
2087 function; if convenient, the result should be placed in TARGET.
2088 SUBTARGET may be used as the target for computing one of EXP's
2092 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2094 optab builtin_optab;
2095 rtx op0, op1, insns;
2096 int op1_type = REAL_TYPE;
2097 tree fndecl = get_callee_fndecl (exp);
2099 enum machine_mode mode;
2100 bool errno_set = true;
2102 switch (DECL_FUNCTION_CODE (fndecl))
2104 CASE_FLT_FN (BUILT_IN_SCALBN):
2105 CASE_FLT_FN (BUILT_IN_SCALBLN):
2106 CASE_FLT_FN (BUILT_IN_LDEXP):
2107 op1_type = INTEGER_TYPE;
2112 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2115 arg0 = CALL_EXPR_ARG (exp, 0);
2116 arg1 = CALL_EXPR_ARG (exp, 1);
2118 switch (DECL_FUNCTION_CODE (fndecl))
2120 CASE_FLT_FN (BUILT_IN_POW):
2121 builtin_optab = pow_optab; break;
2122 CASE_FLT_FN (BUILT_IN_ATAN2):
2123 builtin_optab = atan2_optab; break;
2124 CASE_FLT_FN (BUILT_IN_SCALB):
2125 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2127 builtin_optab = scalb_optab; break;
2128 CASE_FLT_FN (BUILT_IN_SCALBN):
2129 CASE_FLT_FN (BUILT_IN_SCALBLN):
2130 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2132 /* Fall through... */
2133 CASE_FLT_FN (BUILT_IN_LDEXP):
2134 builtin_optab = ldexp_optab; break;
2135 CASE_FLT_FN (BUILT_IN_FMOD):
2136 builtin_optab = fmod_optab; break;
2137 CASE_FLT_FN (BUILT_IN_REMAINDER):
2138 CASE_FLT_FN (BUILT_IN_DREM):
2139 builtin_optab = remainder_optab; break;
2144 /* Make a suitable register to place result in. */
2145 mode = TYPE_MODE (TREE_TYPE (exp));
2147 /* Before working hard, check whether the instruction is available. */
2148 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2151 target = gen_reg_rtx (mode);
2153 if (! flag_errno_math || ! HONOR_NANS (mode))
2156 if (errno_set && optimize_insn_for_size_p ())
2159 /* Always stabilize the argument list. */
2160 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2161 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2163 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2164 op1 = expand_normal (arg1);
2168 /* Compute into TARGET.
2169 Set TARGET to wherever the result comes back. */
2170 target = expand_binop (mode, builtin_optab, op0, op1,
2171 target, 0, OPTAB_DIRECT);
2173 /* If we were unable to expand via the builtin, stop the sequence
2174 (without outputting the insns) and call to the library function
2175 with the stabilized argument list. */
2179 return expand_call (exp, target, target == const0_rtx);
2183 expand_errno_check (exp, target);
2185 /* Output the entire sequence. */
2186 insns = get_insns ();
2193 /* Expand a call to the builtin trinary math functions (fma).
2194 Return NULL_RTX if a normal call should be emitted rather than expanding the
2195 function in-line. EXP is the expression that is a call to the builtin
2196 function; if convenient, the result should be placed in TARGET.
2197 SUBTARGET may be used as the target for computing one of EXP's
2201 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2203 optab builtin_optab;
2204 rtx op0, op1, op2, insns;
2205 tree fndecl = get_callee_fndecl (exp);
2206 tree arg0, arg1, arg2;
2207 enum machine_mode mode;
2209 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2212 arg0 = CALL_EXPR_ARG (exp, 0);
2213 arg1 = CALL_EXPR_ARG (exp, 1);
2214 arg2 = CALL_EXPR_ARG (exp, 2);
2216 switch (DECL_FUNCTION_CODE (fndecl))
2218 CASE_FLT_FN (BUILT_IN_FMA):
2219 builtin_optab = fma_optab; break;
2224 /* Make a suitable register to place result in. */
2225 mode = TYPE_MODE (TREE_TYPE (exp));
2227 /* Before working hard, check whether the instruction is available. */
2228 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2231 target = gen_reg_rtx (mode);
2233 /* Always stabilize the argument list. */
2234 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2235 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2236 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2238 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2239 op1 = expand_normal (arg1);
2240 op2 = expand_normal (arg2);
2244 /* Compute into TARGET.
2245 Set TARGET to wherever the result comes back. */
2246 target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2249 /* If we were unable to expand via the builtin, stop the sequence
2250 (without outputting the insns) and call to the library function
2251 with the stabilized argument list. */
2255 return expand_call (exp, target, target == const0_rtx);
2258 /* Output the entire sequence. */
2259 insns = get_insns ();
2266 /* Expand a call to the builtin sin and cos math functions.
2267 Return NULL_RTX if a normal call should be emitted rather than expanding the
2268 function in-line. EXP is the expression that is a call to the builtin
2269 function; if convenient, the result should be placed in TARGET.
2270 SUBTARGET may be used as the target for computing one of EXP's
2274 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2276 optab builtin_optab;
2278 tree fndecl = get_callee_fndecl (exp);
2279 enum machine_mode mode;
2282 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2285 arg = CALL_EXPR_ARG (exp, 0);
2287 switch (DECL_FUNCTION_CODE (fndecl))
2289 CASE_FLT_FN (BUILT_IN_SIN):
2290 CASE_FLT_FN (BUILT_IN_COS):
2291 builtin_optab = sincos_optab; break;
2296 /* Make a suitable register to place result in. */
2297 mode = TYPE_MODE (TREE_TYPE (exp));
2299 /* Check if sincos insn is available, otherwise fallback
2300 to sin or cos insn. */
2301 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2302 switch (DECL_FUNCTION_CODE (fndecl))
2304 CASE_FLT_FN (BUILT_IN_SIN):
2305 builtin_optab = sin_optab; break;
2306 CASE_FLT_FN (BUILT_IN_COS):
2307 builtin_optab = cos_optab; break;
2312 /* Before working hard, check whether the instruction is available. */
2313 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2315 target = gen_reg_rtx (mode);
2317 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2318 need to expand the argument again. This way, we will not perform
2319 side-effects more the once. */
2320 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2322 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2326 /* Compute into TARGET.
2327 Set TARGET to wherever the result comes back. */
2328 if (builtin_optab == sincos_optab)
2332 switch (DECL_FUNCTION_CODE (fndecl))
2334 CASE_FLT_FN (BUILT_IN_SIN):
2335 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2337 CASE_FLT_FN (BUILT_IN_COS):
2338 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2343 gcc_assert (result);
2347 target = expand_unop (mode, builtin_optab, op0, target, 0);
2352 /* Output the entire sequence. */
2353 insns = get_insns ();
2359 /* If we were unable to expand via the builtin, stop the sequence
2360 (without outputting the insns) and call to the library function
2361 with the stabilized argument list. */
2365 target = expand_call (exp, target, target == const0_rtx);
2370 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2371 return an RTL instruction code that implements the functionality.
2372 If that isn't possible or available return CODE_FOR_nothing. */
2374 static enum insn_code
2375 interclass_mathfn_icode (tree arg, tree fndecl)
2377 bool errno_set = false;
2378 optab builtin_optab = 0;
2379 enum machine_mode mode;
2381 switch (DECL_FUNCTION_CODE (fndecl))
2383 CASE_FLT_FN (BUILT_IN_ILOGB):
2384 errno_set = true; builtin_optab = ilogb_optab; break;
2385 CASE_FLT_FN (BUILT_IN_ISINF):
2386 builtin_optab = isinf_optab; break;
2387 case BUILT_IN_ISNORMAL:
2388 case BUILT_IN_ISFINITE:
2389 CASE_FLT_FN (BUILT_IN_FINITE):
2390 case BUILT_IN_FINITED32:
2391 case BUILT_IN_FINITED64:
2392 case BUILT_IN_FINITED128:
2393 case BUILT_IN_ISINFD32:
2394 case BUILT_IN_ISINFD64:
2395 case BUILT_IN_ISINFD128:
2396 /* These builtins have no optabs (yet). */
2402 /* There's no easy way to detect the case we need to set EDOM. */
2403 if (flag_errno_math && errno_set)
2404 return CODE_FOR_nothing;
2406 /* Optab mode depends on the mode of the input argument. */
2407 mode = TYPE_MODE (TREE_TYPE (arg));
2410 return optab_handler (builtin_optab, mode);
2411 return CODE_FOR_nothing;
2414 /* Expand a call to one of the builtin math functions that operate on
2415 floating point argument and output an integer result (ilogb, isinf,
2417 Return 0 if a normal call should be emitted rather than expanding the
2418 function in-line. EXP is the expression that is a call to the builtin
2419 function; if convenient, the result should be placed in TARGET. */
2422 expand_builtin_interclass_mathfn (tree exp, rtx target)
2424 enum insn_code icode = CODE_FOR_nothing;
2426 tree fndecl = get_callee_fndecl (exp);
2427 enum machine_mode mode;
2430 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2433 arg = CALL_EXPR_ARG (exp, 0);
2434 icode = interclass_mathfn_icode (arg, fndecl);
2435 mode = TYPE_MODE (TREE_TYPE (arg));
2437 if (icode != CODE_FOR_nothing)
2439 struct expand_operand ops[1];
2440 rtx last = get_last_insn ();
2441 tree orig_arg = arg;
2443 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2444 need to expand the argument again. This way, we will not perform
2445 side-effects more the once. */
2446 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2448 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2450 if (mode != GET_MODE (op0))
2451 op0 = convert_to_mode (mode, op0, 0);
2453 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2454 if (maybe_legitimize_operands (icode, 0, 1, ops)
2455 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2456 return ops[0].value;
2458 delete_insns_since (last);
2459 CALL_EXPR_ARG (exp, 0) = orig_arg;
2465 /* Expand a call to the builtin sincos math function.
2466 Return NULL_RTX if a normal call should be emitted rather than expanding the
2467 function in-line. EXP is the expression that is a call to the builtin
2471 expand_builtin_sincos (tree exp)
2473 rtx op0, op1, op2, target1, target2;
2474 enum machine_mode mode;
2475 tree arg, sinp, cosp;
2477 location_t loc = EXPR_LOCATION (exp);
2478 tree alias_type, alias_off;
2480 if (!validate_arglist (exp, REAL_TYPE,
2481 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2484 arg = CALL_EXPR_ARG (exp, 0);
2485 sinp = CALL_EXPR_ARG (exp, 1);
2486 cosp = CALL_EXPR_ARG (exp, 2);
2488 /* Make a suitable register to place result in. */
2489 mode = TYPE_MODE (TREE_TYPE (arg));
2491 /* Check if sincos insn is available, otherwise emit the call. */
2492 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2495 target1 = gen_reg_rtx (mode);
2496 target2 = gen_reg_rtx (mode);
2498 op0 = expand_normal (arg);
2499 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2500 alias_off = build_int_cst (alias_type, 0);
2501 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2503 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2506 /* Compute into target1 and target2.
2507 Set TARGET to wherever the result comes back. */
2508 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2509 gcc_assert (result);
2511 /* Move target1 and target2 to the memory locations indicated
2513 emit_move_insn (op1, target1);
2514 emit_move_insn (op2, target2);
2519 /* Expand a call to the internal cexpi builtin to the sincos math function.
2520 EXP is the expression that is a call to the builtin function; if convenient,
2521 the result should be placed in TARGET. */
2524 expand_builtin_cexpi (tree exp, rtx target)
2526 tree fndecl = get_callee_fndecl (exp);
2528 enum machine_mode mode;
2530 location_t loc = EXPR_LOCATION (exp);
2532 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2535 arg = CALL_EXPR_ARG (exp, 0);
2536 type = TREE_TYPE (arg);
2537 mode = TYPE_MODE (TREE_TYPE (arg));
2539 /* Try expanding via a sincos optab, fall back to emitting a libcall
2540 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2541 is only generated from sincos, cexp or if we have either of them. */
2542 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2544 op1 = gen_reg_rtx (mode);
2545 op2 = gen_reg_rtx (mode);
2547 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2549 /* Compute into op1 and op2. */
2550 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2552 else if (TARGET_HAS_SINCOS)
2554 tree call, fn = NULL_TREE;
2558 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2559 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2560 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2561 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2562 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2563 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2567 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2568 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2569 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2570 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2571 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2572 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2574 /* Make sure not to fold the sincos call again. */
2575 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2576 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2577 call, 3, arg, top1, top2));
2581 tree call, fn = NULL_TREE, narg;
2582 tree ctype = build_complex_type (type);
2584 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2585 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2586 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2587 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2588 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2589 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2593 /* If we don't have a decl for cexp create one. This is the
2594 friendliest fallback if the user calls __builtin_cexpi
2595 without full target C99 function support. */
2596 if (fn == NULL_TREE)
2599 const char *name = NULL;
2601 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2603 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2605 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2608 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2609 fn = build_fn_decl (name, fntype);
2612 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2613 build_real (type, dconst0), arg);
2615 /* Make sure not to fold the cexp call again. */
2616 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2617 return expand_expr (build_call_nary (ctype, call, 1, narg),
2618 target, VOIDmode, EXPAND_NORMAL);
2621 /* Now build the proper return type. */
2622 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2623 make_tree (TREE_TYPE (arg), op2),
2624 make_tree (TREE_TYPE (arg), op1)),
2625 target, VOIDmode, EXPAND_NORMAL);
2628 /* Conveniently construct a function call expression. FNDECL names the
2629 function to be called, N is the number of arguments, and the "..."
2630 parameters are the argument expressions. Unlike build_call_exr
2631 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2634 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2637 tree fntype = TREE_TYPE (fndecl);
2638 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2641 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2643 SET_EXPR_LOCATION (fn, loc);
2647 /* Expand a call to one of the builtin rounding functions gcc defines
2648 as an extension (lfloor and lceil). As these are gcc extensions we
2649 do not need to worry about setting errno to EDOM.
2650 If expanding via optab fails, lower expression to (int)(floor(x)).
2651 EXP is the expression that is a call to the builtin function;
2652 if convenient, the result should be placed in TARGET. */
2655 expand_builtin_int_roundingfn (tree exp, rtx target)
2657 convert_optab builtin_optab;
2658 rtx op0, insns, tmp;
2659 tree fndecl = get_callee_fndecl (exp);
2660 enum built_in_function fallback_fn;
2661 tree fallback_fndecl;
2662 enum machine_mode mode;
2665 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2668 arg = CALL_EXPR_ARG (exp, 0);
2670 switch (DECL_FUNCTION_CODE (fndecl))
2672 CASE_FLT_FN (BUILT_IN_ICEIL):
2673 CASE_FLT_FN (BUILT_IN_LCEIL):
2674 CASE_FLT_FN (BUILT_IN_LLCEIL):
2675 builtin_optab = lceil_optab;
2676 fallback_fn = BUILT_IN_CEIL;
2679 CASE_FLT_FN (BUILT_IN_IFLOOR):
2680 CASE_FLT_FN (BUILT_IN_LFLOOR):
2681 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2682 builtin_optab = lfloor_optab;
2683 fallback_fn = BUILT_IN_FLOOR;
2690 /* Make a suitable register to place result in. */
2691 mode = TYPE_MODE (TREE_TYPE (exp));
2693 target = gen_reg_rtx (mode);
2695 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2696 need to expand the argument again. This way, we will not perform
2697 side-effects more the once. */
2698 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2700 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2704 /* Compute into TARGET. */
2705 if (expand_sfix_optab (target, op0, builtin_optab))
2707 /* Output the entire sequence. */
2708 insns = get_insns ();
2714 /* If we were unable to expand via the builtin, stop the sequence
2715 (without outputting the insns). */
2718 /* Fall back to floating point rounding optab. */
2719 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2721 /* For non-C99 targets we may end up without a fallback fndecl here
2722 if the user called __builtin_lfloor directly. In this case emit
2723 a call to the floor/ceil variants nevertheless. This should result
2724 in the best user experience for not full C99 targets. */
2725 if (fallback_fndecl == NULL_TREE)
2728 const char *name = NULL;
2730 switch (DECL_FUNCTION_CODE (fndecl))
2732 case BUILT_IN_ICEIL:
2733 case BUILT_IN_LCEIL:
2734 case BUILT_IN_LLCEIL:
2737 case BUILT_IN_ICEILF:
2738 case BUILT_IN_LCEILF:
2739 case BUILT_IN_LLCEILF:
2742 case BUILT_IN_ICEILL:
2743 case BUILT_IN_LCEILL:
2744 case BUILT_IN_LLCEILL:
2747 case BUILT_IN_IFLOOR:
2748 case BUILT_IN_LFLOOR:
2749 case BUILT_IN_LLFLOOR:
2752 case BUILT_IN_IFLOORF:
2753 case BUILT_IN_LFLOORF:
2754 case BUILT_IN_LLFLOORF:
2757 case BUILT_IN_IFLOORL:
2758 case BUILT_IN_LFLOORL:
2759 case BUILT_IN_LLFLOORL:
2766 fntype = build_function_type_list (TREE_TYPE (arg),
2767 TREE_TYPE (arg), NULL_TREE);
2768 fallback_fndecl = build_fn_decl (name, fntype);
2771 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2773 tmp = expand_normal (exp);
2775 /* Truncate the result of floating point optab to integer
2776 via expand_fix (). */
2777 target = gen_reg_rtx (mode);
2778 expand_fix (target, tmp, 0);
2783 /* Expand a call to one of the builtin math functions doing integer
2785 Return 0 if a normal call should be emitted rather than expanding the
2786 function in-line. EXP is the expression that is a call to the builtin
2787 function; if convenient, the result should be placed in TARGET. */
2790 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2792 convert_optab builtin_optab;
2794 tree fndecl = get_callee_fndecl (exp);
2796 enum machine_mode mode;
2798 /* There's no easy way to detect the case we need to set EDOM. */
2799 if (flag_errno_math)
2802 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2805 arg = CALL_EXPR_ARG (exp, 0);
2807 switch (DECL_FUNCTION_CODE (fndecl))
2809 CASE_FLT_FN (BUILT_IN_IRINT):
2810 CASE_FLT_FN (BUILT_IN_LRINT):
2811 CASE_FLT_FN (BUILT_IN_LLRINT):
2812 builtin_optab = lrint_optab; break;
2814 CASE_FLT_FN (BUILT_IN_IROUND):
2815 CASE_FLT_FN (BUILT_IN_LROUND):
2816 CASE_FLT_FN (BUILT_IN_LLROUND):
2817 builtin_optab = lround_optab; break;
2823 /* Make a suitable register to place result in. */
2824 mode = TYPE_MODE (TREE_TYPE (exp));
2826 target = gen_reg_rtx (mode);
2828 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2829 need to expand the argument again. This way, we will not perform
2830 side-effects more the once. */
2831 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2833 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2837 if (expand_sfix_optab (target, op0, builtin_optab))
2839 /* Output the entire sequence. */
2840 insns = get_insns ();
2846 /* If we were unable to expand via the builtin, stop the sequence
2847 (without outputting the insns) and call to the library function
2848 with the stabilized argument list. */
2851 target = expand_call (exp, target, target == const0_rtx);
2856 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2857 a normal call should be emitted rather than expanding the function
2858 in-line. EXP is the expression that is a call to the builtin
2859 function; if convenient, the result should be placed in TARGET. */
2862 expand_builtin_powi (tree exp, rtx target)
2866 enum machine_mode mode;
2867 enum machine_mode mode2;
2869 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2872 arg0 = CALL_EXPR_ARG (exp, 0);
2873 arg1 = CALL_EXPR_ARG (exp, 1);
2874 mode = TYPE_MODE (TREE_TYPE (exp));
2876 /* Emit a libcall to libgcc. */
2878 /* Mode of the 2nd argument must match that of an int. */
2879 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2881 if (target == NULL_RTX)
2882 target = gen_reg_rtx (mode);
2884 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2885 if (GET_MODE (op0) != mode)
2886 op0 = convert_to_mode (mode, op0, 0);
2887 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2888 if (GET_MODE (op1) != mode2)
2889 op1 = convert_to_mode (mode2, op1, 0);
2891 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2892 target, LCT_CONST, mode, 2,
2893 op0, mode, op1, mode2);
2898 /* Expand expression EXP which is a call to the strlen builtin. Return
2899 NULL_RTX if we failed the caller should emit a normal call, otherwise
2900 try to get the result in TARGET, if convenient. */
2903 expand_builtin_strlen (tree exp, rtx target,
2904 enum machine_mode target_mode)
2906 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2910 struct expand_operand ops[4];
2913 tree src = CALL_EXPR_ARG (exp, 0);
2914 rtx src_reg, before_strlen;
2915 enum machine_mode insn_mode = target_mode;
2916 enum insn_code icode = CODE_FOR_nothing;
2919 /* If the length can be computed at compile-time, return it. */
2920 len = c_strlen (src, 0);
2922 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2924 /* If the length can be computed at compile-time and is constant
2925 integer, but there are side-effects in src, evaluate
2926 src for side-effects, then return len.
2927 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2928 can be optimized into: i++; x = 3; */
2929 len = c_strlen (src, 1);
2930 if (len && TREE_CODE (len) == INTEGER_CST)
2932 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2933 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2936 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2938 /* If SRC is not a pointer type, don't do this operation inline. */
2942 /* Bail out if we can't compute strlen in the right mode. */
2943 while (insn_mode != VOIDmode)
2945 icode = optab_handler (strlen_optab, insn_mode);
2946 if (icode != CODE_FOR_nothing)
2949 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2951 if (insn_mode == VOIDmode)
2954 /* Make a place to hold the source address. We will not expand
2955 the actual source until we are sure that the expansion will
2956 not fail -- there are trees that cannot be expanded twice. */
2957 src_reg = gen_reg_rtx (Pmode);
2959 /* Mark the beginning of the strlen sequence so we can emit the
2960 source operand later. */
2961 before_strlen = get_last_insn ();
2963 create_output_operand (&ops[0], target, insn_mode);
2964 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2965 create_integer_operand (&ops[2], 0);
2966 create_integer_operand (&ops[3], align);
2967 if (!maybe_expand_insn (icode, 4, ops))
2970 /* Now that we are assured of success, expand the source. */
2972 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2975 #ifdef POINTERS_EXTEND_UNSIGNED
2976 if (GET_MODE (pat) != Pmode)
2977 pat = convert_to_mode (Pmode, pat,
2978 POINTERS_EXTEND_UNSIGNED);
2980 emit_move_insn (src_reg, pat);
2986 emit_insn_after (pat, before_strlen);
2988 emit_insn_before (pat, get_insns ());
2990 /* Return the value in the proper mode for this function. */
2991 if (GET_MODE (ops[0].value) == target_mode)
2992 target = ops[0].value;
2993 else if (target != 0)
2994 convert_move (target, ops[0].value, 0);
2996 target = convert_to_mode (target_mode, ops[0].value, 0);
3002 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3003 bytes from constant string DATA + OFFSET and return it as target
3007 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3008 enum machine_mode mode)
3010 const char *str = (const char *) data;
3012 gcc_assert (offset >= 0
3013 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3014 <= strlen (str) + 1));
3016 return c_readstr (str + offset, mode);
3019 /* Expand a call EXP to the memcpy builtin.
3020 Return NULL_RTX if we failed, the caller should emit a normal call,
3021 otherwise try to get the result in TARGET, if convenient (and in
3022 mode MODE if that's convenient). */
3025 expand_builtin_memcpy (tree exp, rtx target)
3027 if (!validate_arglist (exp,
3028 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3032 tree dest = CALL_EXPR_ARG (exp, 0);
3033 tree src = CALL_EXPR_ARG (exp, 1);
3034 tree len = CALL_EXPR_ARG (exp, 2);
3035 const char *src_str;
3036 unsigned int src_align = get_pointer_alignment (src);
3037 unsigned int dest_align = get_pointer_alignment (dest);
3038 rtx dest_mem, src_mem, dest_addr, len_rtx;
3039 HOST_WIDE_INT expected_size = -1;
3040 unsigned int expected_align = 0;
3042 /* If DEST is not a pointer type, call the normal function. */
3043 if (dest_align == 0)
3046 /* If either SRC is not a pointer type, don't do this
3047 operation in-line. */
3051 if (currently_expanding_gimple_stmt)
3052 stringop_block_profile (currently_expanding_gimple_stmt,
3053 &expected_align, &expected_size);
3055 if (expected_align < dest_align)
3056 expected_align = dest_align;
3057 dest_mem = get_memory_rtx (dest, len);
3058 set_mem_align (dest_mem, dest_align);
3059 len_rtx = expand_normal (len);
3060 src_str = c_getstr (src);
3062 /* If SRC is a string constant and block move would be done
3063 by pieces, we can avoid loading the string from memory
3064 and only stored the computed constants. */
3066 && CONST_INT_P (len_rtx)
3067 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3068 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3069 CONST_CAST (char *, src_str),
3072 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3073 builtin_memcpy_read_str,
3074 CONST_CAST (char *, src_str),
3075 dest_align, false, 0);
3076 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3077 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3081 src_mem = get_memory_rtx (src, len);
3082 set_mem_align (src_mem, src_align);
3084 /* Copy word part most expediently. */
3085 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3086 CALL_EXPR_TAILCALL (exp)
3087 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3088 expected_align, expected_size);
3092 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3093 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3099 /* Expand a call EXP to the mempcpy builtin.
3100 Return NULL_RTX if we failed; the caller should emit a normal call,
3101 otherwise try to get the result in TARGET, if convenient (and in
3102 mode MODE if that's convenient). If ENDP is 0 return the
3103 destination pointer, if ENDP is 1 return the end pointer ala
3104 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3108 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3110 if (!validate_arglist (exp,
3111 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3115 tree dest = CALL_EXPR_ARG (exp, 0);
3116 tree src = CALL_EXPR_ARG (exp, 1);
3117 tree len = CALL_EXPR_ARG (exp, 2);
3118 return expand_builtin_mempcpy_args (dest, src, len,
3119 target, mode, /*endp=*/ 1);
3123 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3124 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3125 so that this can also be called without constructing an actual CALL_EXPR.
3126 The other arguments and return value are the same as for
3127 expand_builtin_mempcpy. */
3130 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3131 rtx target, enum machine_mode mode, int endp)
3133 /* If return value is ignored, transform mempcpy into memcpy. */
3134 if (target == const0_rtx && builtin_decl_implicit_p (BUILT_IN_MEMCPY))
3136 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3137 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3139 return expand_expr (result, target, mode, EXPAND_NORMAL);
3143 const char *src_str;
3144 unsigned int src_align = get_pointer_alignment (src);
3145 unsigned int dest_align = get_pointer_alignment (dest);
3146 rtx dest_mem, src_mem, len_rtx;
3148 /* If either SRC or DEST is not a pointer type, don't do this
3149 operation in-line. */
3150 if (dest_align == 0 || src_align == 0)
3153 /* If LEN is not constant, call the normal function. */
3154 if (! host_integerp (len, 1))
3157 len_rtx = expand_normal (len);
3158 src_str = c_getstr (src);
3160 /* If SRC is a string constant and block move would be done
3161 by pieces, we can avoid loading the string from memory
3162 and only stored the computed constants. */
3164 && CONST_INT_P (len_rtx)
3165 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3166 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3167 CONST_CAST (char *, src_str),
3170 dest_mem = get_memory_rtx (dest, len);
3171 set_mem_align (dest_mem, dest_align);
3172 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3173 builtin_memcpy_read_str,
3174 CONST_CAST (char *, src_str),
3175 dest_align, false, endp);
3176 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3177 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3181 if (CONST_INT_P (len_rtx)
3182 && can_move_by_pieces (INTVAL (len_rtx),
3183 MIN (dest_align, src_align)))
3185 dest_mem = get_memory_rtx (dest, len);
3186 set_mem_align (dest_mem, dest_align);
3187 src_mem = get_memory_rtx (src, len);
3188 set_mem_align (src_mem, src_align);
3189 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3190 MIN (dest_align, src_align), endp);
3191 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3192 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3201 # define HAVE_movstr 0
3202 # define CODE_FOR_movstr CODE_FOR_nothing
3205 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3206 we failed, the caller should emit a normal call, otherwise try to
3207 get the result in TARGET, if convenient. If ENDP is 0 return the
3208 destination pointer, if ENDP is 1 return the end pointer ala
3209 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3213 expand_movstr (tree dest, tree src, rtx target, int endp)
3215 struct expand_operand ops[3];
3222 dest_mem = get_memory_rtx (dest, NULL);
3223 src_mem = get_memory_rtx (src, NULL);
3226 target = force_reg (Pmode, XEXP (dest_mem, 0));
3227 dest_mem = replace_equiv_address (dest_mem, target);
3230 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3231 create_fixed_operand (&ops[1], dest_mem);
3232 create_fixed_operand (&ops[2], src_mem);
3233 expand_insn (CODE_FOR_movstr, 3, ops);
3235 if (endp && target != const0_rtx)
3237 target = ops[0].value;
3238 /* movstr is supposed to set end to the address of the NUL
3239 terminator. If the caller requested a mempcpy-like return value,
3243 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), target), 1);
3244 emit_move_insn (target, force_operand (tem, NULL_RTX));
3250 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3251 NULL_RTX if we failed the caller should emit a normal call, otherwise
3252 try to get the result in TARGET, if convenient (and in mode MODE if that's
3256 expand_builtin_strcpy (tree exp, rtx target)
3258 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3260 tree dest = CALL_EXPR_ARG (exp, 0);
3261 tree src = CALL_EXPR_ARG (exp, 1);
3262 return expand_builtin_strcpy_args (dest, src, target);
3267 /* Helper function to do the actual work for expand_builtin_strcpy. The
3268 arguments to the builtin_strcpy call DEST and SRC are broken out
3269 so that this can also be called without constructing an actual CALL_EXPR.
3270 The other arguments and return value are the same as for
3271 expand_builtin_strcpy. */
3274 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3276 return expand_movstr (dest, src, target, /*endp=*/0);
3279 /* Expand a call EXP to the stpcpy builtin.
3280 Return NULL_RTX if we failed the caller should emit a normal call,
3281 otherwise try to get the result in TARGET, if convenient (and in
3282 mode MODE if that's convenient). */
3285 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3288 location_t loc = EXPR_LOCATION (exp);
3290 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3293 dst = CALL_EXPR_ARG (exp, 0);
3294 src = CALL_EXPR_ARG (exp, 1);
3296 /* If return value is ignored, transform stpcpy into strcpy. */
3297 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3299 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3300 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3301 return expand_expr (result, target, mode, EXPAND_NORMAL);
3308 /* Ensure we get an actual string whose length can be evaluated at
3309 compile-time, not an expression containing a string. This is
3310 because the latter will potentially produce pessimized code
3311 when used to produce the return value. */
3312 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3313 return expand_movstr (dst, src, target, /*endp=*/2);
3315 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3316 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3317 target, mode, /*endp=*/2);
3322 if (TREE_CODE (len) == INTEGER_CST)
3324 rtx len_rtx = expand_normal (len);
3326 if (CONST_INT_P (len_rtx))
3328 ret = expand_builtin_strcpy_args (dst, src, target);
3334 if (mode != VOIDmode)
3335 target = gen_reg_rtx (mode);
3337 target = gen_reg_rtx (GET_MODE (ret));
3339 if (GET_MODE (target) != GET_MODE (ret))
3340 ret = gen_lowpart (GET_MODE (target), ret);
3342 ret = plus_constant (ret, INTVAL (len_rtx));
3343 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3351 return expand_movstr (dst, src, target, /*endp=*/2);
3355 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3356 bytes from constant string DATA + OFFSET and return it as target
3360 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3361 enum machine_mode mode)
3363 const char *str = (const char *) data;
3365 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3368 return c_readstr (str + offset, mode);
3371 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3372 NULL_RTX if we failed the caller should emit a normal call. */
3375 expand_builtin_strncpy (tree exp, rtx target)
3377 location_t loc = EXPR_LOCATION (exp);
3379 if (validate_arglist (exp,
3380 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3382 tree dest = CALL_EXPR_ARG (exp, 0);
3383 tree src = CALL_EXPR_ARG (exp, 1);
3384 tree len = CALL_EXPR_ARG (exp, 2);
3385 tree slen = c_strlen (src, 1);
3387 /* We must be passed a constant len and src parameter. */
3388 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3391 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3393 /* We're required to pad with trailing zeros if the requested
3394 len is greater than strlen(s2)+1. In that case try to
3395 use store_by_pieces, if it fails, punt. */
3396 if (tree_int_cst_lt (slen, len))
3398 unsigned int dest_align = get_pointer_alignment (dest);
3399 const char *p = c_getstr (src);
3402 if (!p || dest_align == 0 || !host_integerp (len, 1)
3403 || !can_store_by_pieces (tree_low_cst (len, 1),
3404 builtin_strncpy_read_str,
3405 CONST_CAST (char *, p),
3409 dest_mem = get_memory_rtx (dest, len);
3410 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3411 builtin_strncpy_read_str,
3412 CONST_CAST (char *, p), dest_align, false, 0);
3413 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3414 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3421 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3422 bytes from constant string DATA + OFFSET and return it as target
3426 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3427 enum machine_mode mode)
3429 const char *c = (const char *) data;
3430 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3432 memset (p, *c, GET_MODE_SIZE (mode));
3434 return c_readstr (p, mode);
3437 /* Callback routine for store_by_pieces. Return the RTL of a register
3438 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3439 char value given in the RTL register data. For example, if mode is
3440 4 bytes wide, return the RTL for 0x01010101*data. */
3443 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3444 enum machine_mode mode)
3450 size = GET_MODE_SIZE (mode);
3454 p = XALLOCAVEC (char, size);
3455 memset (p, 1, size);
3456 coeff = c_readstr (p, mode);
3458 target = convert_to_mode (mode, (rtx) data, 1);
3459 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3460 return force_reg (mode, target);
3463 /* Expand expression EXP, which is a call to the memset builtin. Return
3464 NULL_RTX if we failed the caller should emit a normal call, otherwise
3465 try to get the result in TARGET, if convenient (and in mode MODE if that's
3469 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3471 if (!validate_arglist (exp,
3472 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3476 tree dest = CALL_EXPR_ARG (exp, 0);
3477 tree val = CALL_EXPR_ARG (exp, 1);
3478 tree len = CALL_EXPR_ARG (exp, 2);
3479 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3483 /* Helper function to do the actual work for expand_builtin_memset. The
3484 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3485 so that this can also be called without constructing an actual CALL_EXPR.
3486 The other arguments and return value are the same as for
3487 expand_builtin_memset. */
3490 expand_builtin_memset_args (tree dest, tree val, tree len,
3491 rtx target, enum machine_mode mode, tree orig_exp)
3494 enum built_in_function fcode;
3495 enum machine_mode val_mode;
3497 unsigned int dest_align;
3498 rtx dest_mem, dest_addr, len_rtx;
3499 HOST_WIDE_INT expected_size = -1;
3500 unsigned int expected_align = 0;
3502 dest_align = get_pointer_alignment (dest);
3504 /* If DEST is not a pointer type, don't do this operation in-line. */
3505 if (dest_align == 0)
3508 if (currently_expanding_gimple_stmt)
3509 stringop_block_profile (currently_expanding_gimple_stmt,
3510 &expected_align, &expected_size);
3512 if (expected_align < dest_align)
3513 expected_align = dest_align;
3515 /* If the LEN parameter is zero, return DEST. */
3516 if (integer_zerop (len))
3518 /* Evaluate and ignore VAL in case it has side-effects. */
3519 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3520 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3523 /* Stabilize the arguments in case we fail. */
3524 dest = builtin_save_expr (dest);
3525 val = builtin_save_expr (val);
3526 len = builtin_save_expr (len);
3528 len_rtx = expand_normal (len);
3529 dest_mem = get_memory_rtx (dest, len);
3530 val_mode = TYPE_MODE (unsigned_char_type_node);
3532 if (TREE_CODE (val) != INTEGER_CST)
3536 val_rtx = expand_normal (val);
3537 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3539 /* Assume that we can memset by pieces if we can store
3540 * the coefficients by pieces (in the required modes).
3541 * We can't pass builtin_memset_gen_str as that emits RTL. */
3543 if (host_integerp (len, 1)
3544 && can_store_by_pieces (tree_low_cst (len, 1),
3545 builtin_memset_read_str, &c, dest_align,
3548 val_rtx = force_reg (val_mode, val_rtx);
3549 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3550 builtin_memset_gen_str, val_rtx, dest_align,
3553 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3554 dest_align, expected_align,
3558 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3559 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3563 if (target_char_cast (val, &c))
3568 if (host_integerp (len, 1)
3569 && can_store_by_pieces (tree_low_cst (len, 1),
3570 builtin_memset_read_str, &c, dest_align,
3572 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3573 builtin_memset_read_str, &c, dest_align, true, 0);
3574 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3575 gen_int_mode (c, val_mode),
3576 dest_align, expected_align,
3580 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3581 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3585 set_mem_align (dest_mem, dest_align);
3586 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3587 CALL_EXPR_TAILCALL (orig_exp)
3588 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3589 expected_align, expected_size);
3593 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3594 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3600 fndecl = get_callee_fndecl (orig_exp);
3601 fcode = DECL_FUNCTION_CODE (fndecl);
3602 if (fcode == BUILT_IN_MEMSET)
3603 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3605 else if (fcode == BUILT_IN_BZERO)
3606 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3610 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3611 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3612 return expand_call (fn, target, target == const0_rtx);
3615 /* Expand expression EXP, which is a call to the bzero builtin. Return
3616 NULL_RTX if we failed the caller should emit a normal call. */
3619 expand_builtin_bzero (tree exp)
3622 location_t loc = EXPR_LOCATION (exp);
3624 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3627 dest = CALL_EXPR_ARG (exp, 0);
3628 size = CALL_EXPR_ARG (exp, 1);
3630 /* New argument list transforming bzero(ptr x, int y) to
3631 memset(ptr x, int 0, size_t y). This is done this way
3632 so that if it isn't expanded inline, we fallback to
3633 calling bzero instead of memset. */
3635 return expand_builtin_memset_args (dest, integer_zero_node,
3636 fold_convert_loc (loc,
3637 size_type_node, size),
3638 const0_rtx, VOIDmode, exp);
3641 /* Expand expression EXP, which is a call to the memcmp built-in function.
3642 Return NULL_RTX if we failed and the caller should emit a normal call,
3643 otherwise try to get the result in TARGET, if convenient (and in mode
3644 MODE, if that's convenient). */
3647 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3648 ATTRIBUTE_UNUSED enum machine_mode mode)
3650 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3652 if (!validate_arglist (exp,
3653 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3656 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3657 implementing memcmp because it will stop if it encounters two
3659 #if defined HAVE_cmpmemsi
3661 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3664 tree arg1 = CALL_EXPR_ARG (exp, 0);
3665 tree arg2 = CALL_EXPR_ARG (exp, 1);
3666 tree len = CALL_EXPR_ARG (exp, 2);
3668 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3669 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3670 enum machine_mode insn_mode;
3673 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3677 /* If we don't have POINTER_TYPE, call the function. */
3678 if (arg1_align == 0 || arg2_align == 0)
3681 /* Make a place to write the result of the instruction. */
3684 && REG_P (result) && GET_MODE (result) == insn_mode
3685 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3686 result = gen_reg_rtx (insn_mode);
3688 arg1_rtx = get_memory_rtx (arg1, len);
3689 arg2_rtx = get_memory_rtx (arg2, len);
3690 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3692 /* Set MEM_SIZE as appropriate. */
3693 if (CONST_INT_P (arg3_rtx))
3695 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3696 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3700 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3701 GEN_INT (MIN (arg1_align, arg2_align)));
3708 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3709 TYPE_MODE (integer_type_node), 3,
3710 XEXP (arg1_rtx, 0), Pmode,
3711 XEXP (arg2_rtx, 0), Pmode,
3712 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3713 TYPE_UNSIGNED (sizetype)),
3714 TYPE_MODE (sizetype));
3716 /* Return the value in the proper mode for this function. */
3717 mode = TYPE_MODE (TREE_TYPE (exp));
3718 if (GET_MODE (result) == mode)
3720 else if (target != 0)
3722 convert_move (target, result, 0);
3726 return convert_to_mode (mode, result, 0);
3728 #endif /* HAVE_cmpmemsi. */
3733 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3734 if we failed the caller should emit a normal call, otherwise try to get
3735 the result in TARGET, if convenient. */
3738 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3740 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3743 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3744 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3745 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3747 rtx arg1_rtx, arg2_rtx;
3748 rtx result, insn = NULL_RTX;
3750 tree arg1 = CALL_EXPR_ARG (exp, 0);
3751 tree arg2 = CALL_EXPR_ARG (exp, 1);
3753 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3754 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3756 /* If we don't have POINTER_TYPE, call the function. */
3757 if (arg1_align == 0 || arg2_align == 0)
3760 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3761 arg1 = builtin_save_expr (arg1);
3762 arg2 = builtin_save_expr (arg2);
3764 arg1_rtx = get_memory_rtx (arg1, NULL);
3765 arg2_rtx = get_memory_rtx (arg2, NULL);
3767 #ifdef HAVE_cmpstrsi
3768 /* Try to call cmpstrsi. */
3771 enum machine_mode insn_mode
3772 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3774 /* Make a place to write the result of the instruction. */
3777 && REG_P (result) && GET_MODE (result) == insn_mode
3778 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3779 result = gen_reg_rtx (insn_mode);
3781 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3782 GEN_INT (MIN (arg1_align, arg2_align)));
3785 #ifdef HAVE_cmpstrnsi
3786 /* Try to determine at least one length and call cmpstrnsi. */
3787 if (!insn && HAVE_cmpstrnsi)
3792 enum machine_mode insn_mode
3793 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3794 tree len1 = c_strlen (arg1, 1);
3795 tree len2 = c_strlen (arg2, 1);
3798 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3800 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3802 /* If we don't have a constant length for the first, use the length
3803 of the second, if we know it. We don't require a constant for
3804 this case; some cost analysis could be done if both are available
3805 but neither is constant. For now, assume they're equally cheap,
3806 unless one has side effects. If both strings have constant lengths,
3813 else if (TREE_SIDE_EFFECTS (len1))
3815 else if (TREE_SIDE_EFFECTS (len2))
3817 else if (TREE_CODE (len1) != INTEGER_CST)
3819 else if (TREE_CODE (len2) != INTEGER_CST)
3821 else if (tree_int_cst_lt (len1, len2))
3826 /* If both arguments have side effects, we cannot optimize. */
3827 if (!len || TREE_SIDE_EFFECTS (len))
3830 arg3_rtx = expand_normal (len);
3832 /* Make a place to write the result of the instruction. */
3835 && REG_P (result) && GET_MODE (result) == insn_mode
3836 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3837 result = gen_reg_rtx (insn_mode);
3839 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3840 GEN_INT (MIN (arg1_align, arg2_align)));
3846 enum machine_mode mode;
3849 /* Return the value in the proper mode for this function. */
3850 mode = TYPE_MODE (TREE_TYPE (exp));
3851 if (GET_MODE (result) == mode)
3854 return convert_to_mode (mode, result, 0);
3855 convert_move (target, result, 0);
3859 /* Expand the library call ourselves using a stabilized argument
3860 list to avoid re-evaluating the function's arguments twice. */
3861 #ifdef HAVE_cmpstrnsi
3864 fndecl = get_callee_fndecl (exp);
3865 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
3866 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3867 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3868 return expand_call (fn, target, target == const0_rtx);
3874 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3875 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3876 the result in TARGET, if convenient. */
3879 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3880 ATTRIBUTE_UNUSED enum machine_mode mode)
3882 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3884 if (!validate_arglist (exp,
3885 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3888 /* If c_strlen can determine an expression for one of the string
3889 lengths, and it doesn't have side effects, then emit cmpstrnsi
3890 using length MIN(strlen(string)+1, arg3). */
3891 #ifdef HAVE_cmpstrnsi
3894 tree len, len1, len2;
3895 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3898 tree arg1 = CALL_EXPR_ARG (exp, 0);
3899 tree arg2 = CALL_EXPR_ARG (exp, 1);
3900 tree arg3 = CALL_EXPR_ARG (exp, 2);
3902 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3903 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3904 enum machine_mode insn_mode
3905 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3907 len1 = c_strlen (arg1, 1);
3908 len2 = c_strlen (arg2, 1);
3911 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
3913 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
3915 /* If we don't have a constant length for the first, use the length
3916 of the second, if we know it. We don't require a constant for
3917 this case; some cost analysis could be done if both are available
3918 but neither is constant. For now, assume they're equally cheap,
3919 unless one has side effects. If both strings have constant lengths,
3926 else if (TREE_SIDE_EFFECTS (len1))
3928 else if (TREE_SIDE_EFFECTS (len2))
3930 else if (TREE_CODE (len1) != INTEGER_CST)
3932 else if (TREE_CODE (len2) != INTEGER_CST)
3934 else if (tree_int_cst_lt (len1, len2))
3939 /* If both arguments have side effects, we cannot optimize. */
3940 if (!len || TREE_SIDE_EFFECTS (len))
3943 /* The actual new length parameter is MIN(len,arg3). */
3944 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
3945 fold_convert_loc (loc, TREE_TYPE (len), arg3));
3947 /* If we don't have POINTER_TYPE, call the function. */
3948 if (arg1_align == 0 || arg2_align == 0)
3951 /* Make a place to write the result of the instruction. */
3954 && REG_P (result) && GET_MODE (result) == insn_mode
3955 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3956 result = gen_reg_rtx (insn_mode);
3958 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3959 arg1 = builtin_save_expr (arg1);
3960 arg2 = builtin_save_expr (arg2);
3961 len = builtin_save_expr (len);
3963 arg1_rtx = get_memory_rtx (arg1, len);
3964 arg2_rtx = get_memory_rtx (arg2, len);
3965 arg3_rtx = expand_normal (len);
3966 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3967 GEN_INT (MIN (arg1_align, arg2_align)));
3972 /* Return the value in the proper mode for this function. */
3973 mode = TYPE_MODE (TREE_TYPE (exp));
3974 if (GET_MODE (result) == mode)
3977 return convert_to_mode (mode, result, 0);
3978 convert_move (target, result, 0);
3982 /* Expand the library call ourselves using a stabilized argument
3983 list to avoid re-evaluating the function's arguments twice. */
3984 fndecl = get_callee_fndecl (exp);
3985 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
3987 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3988 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3989 return expand_call (fn, target, target == const0_rtx);
3995 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3996 if that's convenient. */
3999 expand_builtin_saveregs (void)
4003 /* Don't do __builtin_saveregs more than once in a function.
4004 Save the result of the first call and reuse it. */
4005 if (saveregs_value != 0)
4006 return saveregs_value;
4008 /* When this function is called, it means that registers must be
4009 saved on entry to this function. So we migrate the call to the
4010 first insn of this function. */
4014 /* Do whatever the machine needs done in this case. */
4015 val = targetm.calls.expand_builtin_saveregs ();
4020 saveregs_value = val;
4022 /* Put the insns after the NOTE that starts the function. If this
4023 is inside a start_sequence, make the outer-level insn chain current, so
4024 the code is placed at the start of the function. */
4025 push_topmost_sequence ();
4026 emit_insn_after (seq, entry_of_function ());
4027 pop_topmost_sequence ();
4032 /* Expand a call to __builtin_next_arg. */
4035 expand_builtin_next_arg (void)
4037 /* Checking arguments is already done in fold_builtin_next_arg
4038 that must be called before this function. */
4039 return expand_binop (ptr_mode, add_optab,
4040 crtl->args.internal_arg_pointer,
4041 crtl->args.arg_offset_rtx,
4042 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4045 /* Make it easier for the backends by protecting the valist argument
4046 from multiple evaluations. */
4049 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4051 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4053 /* The current way of determining the type of valist is completely
4054 bogus. We should have the information on the va builtin instead. */
4056 vatype = targetm.fn_abi_va_list (cfun->decl);
4058 if (TREE_CODE (vatype) == ARRAY_TYPE)
4060 if (TREE_SIDE_EFFECTS (valist))
4061 valist = save_expr (valist);
4063 /* For this case, the backends will be expecting a pointer to
4064 vatype, but it's possible we've actually been given an array
4065 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4067 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4069 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4070 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4075 tree pt = build_pointer_type (vatype);
4079 if (! TREE_SIDE_EFFECTS (valist))
4082 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4083 TREE_SIDE_EFFECTS (valist) = 1;
4086 if (TREE_SIDE_EFFECTS (valist))
4087 valist = save_expr (valist);
4088 valist = fold_build2_loc (loc, MEM_REF,
4089 vatype, valist, build_int_cst (pt, 0));
4095 /* The "standard" definition of va_list is void*. */
4098 std_build_builtin_va_list (void)
4100 return ptr_type_node;
4103 /* The "standard" abi va_list is va_list_type_node. */
4106 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4108 return va_list_type_node;
4111 /* The "standard" type of va_list is va_list_type_node. */
4114 std_canonical_va_list_type (tree type)
4118 if (INDIRECT_REF_P (type))
4119 type = TREE_TYPE (type);
4120 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4121 type = TREE_TYPE (type);
4122 wtype = va_list_type_node;
4124 /* Treat structure va_list types. */
4125 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4126 htype = TREE_TYPE (htype);
4127 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4129 /* If va_list is an array type, the argument may have decayed
4130 to a pointer type, e.g. by being passed to another function.
4131 In that case, unwrap both types so that we can compare the
4132 underlying records. */
4133 if (TREE_CODE (htype) == ARRAY_TYPE
4134 || POINTER_TYPE_P (htype))
4136 wtype = TREE_TYPE (wtype);
4137 htype = TREE_TYPE (htype);
4140 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4141 return va_list_type_node;
4146 /* The "standard" implementation of va_start: just assign `nextarg' to
4150 std_expand_builtin_va_start (tree valist, rtx nextarg)
4152 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4153 convert_move (va_r, nextarg, 0);
4156 /* Expand EXP, a call to __builtin_va_start. */
4159 expand_builtin_va_start (tree exp)
4163 location_t loc = EXPR_LOCATION (exp);
4165 if (call_expr_nargs (exp) < 2)
4167 error_at (loc, "too few arguments to function %<va_start%>");
4171 if (fold_builtin_next_arg (exp, true))
4174 nextarg = expand_builtin_next_arg ();
4175 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4177 if (targetm.expand_builtin_va_start)
4178 targetm.expand_builtin_va_start (valist, nextarg);
4180 std_expand_builtin_va_start (valist, nextarg);
4185 /* The "standard" implementation of va_arg: read the value from the
4186 current (padded) address and increment by the (padded) size. */
4189 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4192 tree addr, t, type_size, rounded_size, valist_tmp;
4193 unsigned HOST_WIDE_INT align, boundary;
4196 #ifdef ARGS_GROW_DOWNWARD
4197 /* All of the alignment and movement below is for args-grow-up machines.
4198 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4199 implement their own specialized gimplify_va_arg_expr routines. */
4203 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4205 type = build_pointer_type (type);
4207 align = PARM_BOUNDARY / BITS_PER_UNIT;
4208 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4210 /* When we align parameter on stack for caller, if the parameter
4211 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4212 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4213 here with caller. */
4214 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4215 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4217 boundary /= BITS_PER_UNIT;
4219 /* Hoist the valist value into a temporary for the moment. */
4220 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4222 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4223 requires greater alignment, we must perform dynamic alignment. */
4224 if (boundary > align
4225 && !integer_zerop (TYPE_SIZE (type)))
4227 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4228 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
4229 gimplify_and_add (t, pre_p);
4231 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4232 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
4234 build_int_cst (TREE_TYPE (valist), -boundary)));
4235 gimplify_and_add (t, pre_p);
4240 /* If the actual alignment is less than the alignment of the type,
4241 adjust the type accordingly so that we don't assume strict alignment
4242 when dereferencing the pointer. */
4243 boundary *= BITS_PER_UNIT;
4244 if (boundary < TYPE_ALIGN (type))
4246 type = build_variant_type_copy (type);
4247 TYPE_ALIGN (type) = boundary;
4250 /* Compute the rounded size of the type. */
4251 type_size = size_in_bytes (type);
4252 rounded_size = round_up (type_size, align);
4254 /* Reduce rounded_size so it's sharable with the postqueue. */
4255 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4259 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4261 /* Small args are padded downward. */
4262 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4263 rounded_size, size_int (align));
4264 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4265 size_binop (MINUS_EXPR, rounded_size, type_size));
4266 addr = fold_build_pointer_plus (addr, t);
4269 /* Compute new value for AP. */
4270 t = fold_build_pointer_plus (valist_tmp, rounded_size);
4271 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4272 gimplify_and_add (t, pre_p);
4274 addr = fold_convert (build_pointer_type (type), addr);
4277 addr = build_va_arg_indirect_ref (addr);
4279 return build_va_arg_indirect_ref (addr);
4282 /* Build an indirect-ref expression over the given TREE, which represents a
4283 piece of a va_arg() expansion. */
4285 build_va_arg_indirect_ref (tree addr)
4287 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4289 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4295 /* Return a dummy expression of type TYPE in order to keep going after an
4299 dummy_object (tree type)
4301 tree t = build_int_cst (build_pointer_type (type), 0);
4302 return build2 (MEM_REF, type, t, t);
4305 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4306 builtin function, but a very special sort of operator. */
4308 enum gimplify_status
4309 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4311 tree promoted_type, have_va_type;
4312 tree valist = TREE_OPERAND (*expr_p, 0);
4313 tree type = TREE_TYPE (*expr_p);
4315 location_t loc = EXPR_LOCATION (*expr_p);
4317 /* Verify that valist is of the proper type. */
4318 have_va_type = TREE_TYPE (valist);
4319 if (have_va_type == error_mark_node)
4321 have_va_type = targetm.canonical_va_list_type (have_va_type);
4323 if (have_va_type == NULL_TREE)
4325 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4329 /* Generate a diagnostic for requesting data of a type that cannot
4330 be passed through `...' due to type promotion at the call site. */
4331 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4334 static bool gave_help;
4337 /* Unfortunately, this is merely undefined, rather than a constraint
4338 violation, so we cannot make this an error. If this call is never
4339 executed, the program is still strictly conforming. */
4340 warned = warning_at (loc, 0,
4341 "%qT is promoted to %qT when passed through %<...%>",
4342 type, promoted_type);
4343 if (!gave_help && warned)
4346 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4347 promoted_type, type);
4350 /* We can, however, treat "undefined" any way we please.
4351 Call abort to encourage the user to fix the program. */
4353 inform (loc, "if this code is reached, the program will abort");
4354 /* Before the abort, allow the evaluation of the va_list
4355 expression to exit or longjmp. */
4356 gimplify_and_add (valist, pre_p);
4357 t = build_call_expr_loc (loc,
4358 builtin_decl_implicit (BUILT_IN_TRAP), 0);
4359 gimplify_and_add (t, pre_p);
4361 /* This is dead code, but go ahead and finish so that the
4362 mode of the result comes out right. */
4363 *expr_p = dummy_object (type);
4368 /* Make it easier for the backends by protecting the valist argument
4369 from multiple evaluations. */
4370 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4372 /* For this case, the backends will be expecting a pointer to
4373 TREE_TYPE (abi), but it's possible we've
4374 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4376 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4378 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4379 valist = fold_convert_loc (loc, p1,
4380 build_fold_addr_expr_loc (loc, valist));
4383 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4386 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4388 if (!targetm.gimplify_va_arg_expr)
4389 /* FIXME: Once most targets are converted we should merely
4390 assert this is non-null. */
4393 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4398 /* Expand EXP, a call to __builtin_va_end. */
4401 expand_builtin_va_end (tree exp)
4403 tree valist = CALL_EXPR_ARG (exp, 0);
4405 /* Evaluate for side effects, if needed. I hate macros that don't
4407 if (TREE_SIDE_EFFECTS (valist))
4408 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4413 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4414 builtin rather than just as an assignment in stdarg.h because of the
4415 nastiness of array-type va_list types. */
4418 expand_builtin_va_copy (tree exp)
4421 location_t loc = EXPR_LOCATION (exp);
4423 dst = CALL_EXPR_ARG (exp, 0);
4424 src = CALL_EXPR_ARG (exp, 1);
4426 dst = stabilize_va_list_loc (loc, dst, 1);
4427 src = stabilize_va_list_loc (loc, src, 0);
4429 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4431 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4433 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4434 TREE_SIDE_EFFECTS (t) = 1;
4435 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4439 rtx dstb, srcb, size;
4441 /* Evaluate to pointers. */
4442 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4443 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4444 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4445 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4447 dstb = convert_memory_address (Pmode, dstb);
4448 srcb = convert_memory_address (Pmode, srcb);
4450 /* "Dereference" to BLKmode memories. */
4451 dstb = gen_rtx_MEM (BLKmode, dstb);
4452 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4453 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4454 srcb = gen_rtx_MEM (BLKmode, srcb);
4455 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4456 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4459 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4465 /* Expand a call to one of the builtin functions __builtin_frame_address or
4466 __builtin_return_address. */
4469 expand_builtin_frame_address (tree fndecl, tree exp)
4471 /* The argument must be a nonnegative integer constant.
4472 It counts the number of frames to scan up the stack.
4473 The value is the return address saved in that frame. */
4474 if (call_expr_nargs (exp) == 0)
4475 /* Warning about missing arg was already issued. */
4477 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4479 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4480 error ("invalid argument to %<__builtin_frame_address%>");
4482 error ("invalid argument to %<__builtin_return_address%>");
4488 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4489 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4491 /* Some ports cannot access arbitrary stack frames. */
4494 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4495 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4497 warning (0, "unsupported argument to %<__builtin_return_address%>");
4501 /* For __builtin_frame_address, return what we've got. */
4502 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4506 && ! CONSTANT_P (tem))
4507 tem = copy_to_mode_reg (Pmode, tem);
4512 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4513 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4514 is the same as for allocate_dynamic_stack_space. */
4517 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4523 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4524 == BUILT_IN_ALLOCA_WITH_ALIGN);
4526 /* Emit normal call if marked not-inlineable. */
4527 if (CALL_CANNOT_INLINE_P (exp))
4531 = (alloca_with_align
4532 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4533 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4538 /* Compute the argument. */
4539 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4541 /* Compute the alignment. */
4542 align = (alloca_with_align
4543 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4544 : BIGGEST_ALIGNMENT);
4546 /* Allocate the desired space. */
4547 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4548 result = convert_memory_address (ptr_mode, result);
4553 /* Expand a call to a bswap builtin with argument ARG0. MODE
4554 is the mode to expand with. */
4557 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
4559 enum machine_mode mode;
4563 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4566 arg = CALL_EXPR_ARG (exp, 0);
4567 mode = TYPE_MODE (TREE_TYPE (arg));
4568 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4570 target = expand_unop (mode, bswap_optab, op0, target, 1);
4572 gcc_assert (target);
4574 return convert_to_mode (mode, target, 0);
4577 /* Expand a call to a unary builtin in EXP.
4578 Return NULL_RTX if a normal call should be emitted rather than expanding the
4579 function in-line. If convenient, the result should be placed in TARGET.
4580 SUBTARGET may be used as the target for computing one of EXP's operands. */
4583 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4584 rtx subtarget, optab op_optab)
4588 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4591 /* Compute the argument. */
4592 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4594 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4595 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4596 VOIDmode, EXPAND_NORMAL);
4597 /* Compute op, into TARGET if possible.
4598 Set TARGET to wherever the result comes back. */
4599 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4600 op_optab, op0, target, op_optab != clrsb_optab);
4601 gcc_assert (target);
4603 return convert_to_mode (target_mode, target, 0);
4606 /* Expand a call to __builtin_expect. We just return our argument
4607 as the builtin_expect semantic should've been already executed by
4608 tree branch prediction pass. */
4611 expand_builtin_expect (tree exp, rtx target)
4615 if (call_expr_nargs (exp) < 2)
4617 arg = CALL_EXPR_ARG (exp, 0);
4619 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4620 /* When guessing was done, the hints should be already stripped away. */
4621 gcc_assert (!flag_guess_branch_prob
4622 || optimize == 0 || seen_error ());
4626 /* Expand a call to __builtin_assume_aligned. We just return our first
4627 argument as the builtin_assume_aligned semantic should've been already
4631 expand_builtin_assume_aligned (tree exp, rtx target)
4633 if (call_expr_nargs (exp) < 2)
4635 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4637 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4638 && (call_expr_nargs (exp) < 3
4639 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4644 expand_builtin_trap (void)
4648 emit_insn (gen_trap ());
4651 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4655 /* Expand a call to __builtin_unreachable. We do nothing except emit
4656 a barrier saying that control flow will not pass here.
4658 It is the responsibility of the program being compiled to ensure
4659 that control flow does never reach __builtin_unreachable. */
4661 expand_builtin_unreachable (void)
4666 /* Expand EXP, a call to fabs, fabsf or fabsl.
4667 Return NULL_RTX if a normal call should be emitted rather than expanding
4668 the function inline. If convenient, the result should be placed
4669 in TARGET. SUBTARGET may be used as the target for computing
4673 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4675 enum machine_mode mode;
4679 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4682 arg = CALL_EXPR_ARG (exp, 0);
4683 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4684 mode = TYPE_MODE (TREE_TYPE (arg));
4685 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4686 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4689 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4690 Return NULL is a normal call should be emitted rather than expanding the
4691 function inline. If convenient, the result should be placed in TARGET.
4692 SUBTARGET may be used as the target for computing the operand. */
4695 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4700 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4703 arg = CALL_EXPR_ARG (exp, 0);
4704 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4706 arg = CALL_EXPR_ARG (exp, 1);
4707 op1 = expand_normal (arg);
4709 return expand_copysign (op0, op1, target);
4712 /* Create a new constant string literal and return a char* pointer to it.
4713 The STRING_CST value is the LEN characters at STR. */
4715 build_string_literal (int len, const char *str)
4717 tree t, elem, index, type;
4719 t = build_string (len, str);
4720 elem = build_type_variant (char_type_node, 1, 0);
4721 index = build_index_type (size_int (len - 1));
4722 type = build_array_type (elem, index);
4723 TREE_TYPE (t) = type;
4724 TREE_CONSTANT (t) = 1;
4725 TREE_READONLY (t) = 1;
4726 TREE_STATIC (t) = 1;
4728 type = build_pointer_type (elem);
4729 t = build1 (ADDR_EXPR, type,
4730 build4 (ARRAY_REF, elem,
4731 t, integer_zero_node, NULL_TREE, NULL_TREE));
4735 /* Expand a call to __builtin___clear_cache. */
4738 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4740 #ifndef HAVE_clear_cache
4741 #ifdef CLEAR_INSN_CACHE
4742 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4743 does something. Just do the default expansion to a call to
4747 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4748 does nothing. There is no need to call it. Do nothing. */
4750 #endif /* CLEAR_INSN_CACHE */
4752 /* We have a "clear_cache" insn, and it will handle everything. */
4754 rtx begin_rtx, end_rtx;
4756 /* We must not expand to a library call. If we did, any
4757 fallback library function in libgcc that might contain a call to
4758 __builtin___clear_cache() would recurse infinitely. */
4759 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4761 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4765 if (HAVE_clear_cache)
4767 struct expand_operand ops[2];
4769 begin = CALL_EXPR_ARG (exp, 0);
4770 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4772 end = CALL_EXPR_ARG (exp, 1);
4773 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4775 create_address_operand (&ops[0], begin_rtx);
4776 create_address_operand (&ops[1], end_rtx);
4777 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4781 #endif /* HAVE_clear_cache */
4784 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4787 round_trampoline_addr (rtx tramp)
4789 rtx temp, addend, mask;
4791 /* If we don't need too much alignment, we'll have been guaranteed
4792 proper alignment by get_trampoline_type. */
4793 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4796 /* Round address up to desired boundary. */
4797 temp = gen_reg_rtx (Pmode);
4798 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4799 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4801 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4802 temp, 0, OPTAB_LIB_WIDEN);
4803 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4804 temp, 0, OPTAB_LIB_WIDEN);
4810 expand_builtin_init_trampoline (tree exp)
4812 tree t_tramp, t_func, t_chain;
4813 rtx m_tramp, r_tramp, r_chain, tmp;
4815 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4816 POINTER_TYPE, VOID_TYPE))
4819 t_tramp = CALL_EXPR_ARG (exp, 0);
4820 t_func = CALL_EXPR_ARG (exp, 1);
4821 t_chain = CALL_EXPR_ARG (exp, 2);
4823 r_tramp = expand_normal (t_tramp);
4824 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4825 MEM_NOTRAP_P (m_tramp) = 1;
4827 /* The TRAMP argument should be the address of a field within the
4828 local function's FRAME decl. Let's see if we can fill in the
4829 to fill in the MEM_ATTRs for this memory. */
4830 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4831 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
4834 tmp = round_trampoline_addr (r_tramp);
4837 m_tramp = change_address (m_tramp, BLKmode, tmp);
4838 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4839 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4842 /* The FUNC argument should be the address of the nested function.
4843 Extract the actual function decl to pass to the hook. */
4844 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4845 t_func = TREE_OPERAND (t_func, 0);
4846 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4848 r_chain = expand_normal (t_chain);
4850 /* Generate insns to initialize the trampoline. */
4851 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4853 trampolines_created = 1;
4855 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4856 "trampoline generated for nested function %qD", t_func);
4862 expand_builtin_adjust_trampoline (tree exp)
4866 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4869 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4870 tramp = round_trampoline_addr (tramp);
4871 if (targetm.calls.trampoline_adjust_address)
4872 tramp = targetm.calls.trampoline_adjust_address (tramp);
4877 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4878 function. The function first checks whether the back end provides
4879 an insn to implement signbit for the respective mode. If not, it
4880 checks whether the floating point format of the value is such that
4881 the sign bit can be extracted. If that is not the case, the
4882 function returns NULL_RTX to indicate that a normal call should be
4883 emitted rather than expanding the function in-line. EXP is the
4884 expression that is a call to the builtin function; if convenient,
4885 the result should be placed in TARGET. */
4887 expand_builtin_signbit (tree exp, rtx target)
4889 const struct real_format *fmt;
4890 enum machine_mode fmode, imode, rmode;
4893 enum insn_code icode;
4895 location_t loc = EXPR_LOCATION (exp);
4897 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4900 arg = CALL_EXPR_ARG (exp, 0);
4901 fmode = TYPE_MODE (TREE_TYPE (arg));
4902 rmode = TYPE_MODE (TREE_TYPE (exp));
4903 fmt = REAL_MODE_FORMAT (fmode);
4905 arg = builtin_save_expr (arg);
4907 /* Expand the argument yielding a RTX expression. */
4908 temp = expand_normal (arg);
4910 /* Check if the back end provides an insn that handles signbit for the
4912 icode = optab_handler (signbit_optab, fmode);
4913 if (icode != CODE_FOR_nothing)
4915 rtx last = get_last_insn ();
4916 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4917 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4919 delete_insns_since (last);
4922 /* For floating point formats without a sign bit, implement signbit
4924 bitpos = fmt->signbit_ro;
4927 /* But we can't do this if the format supports signed zero. */
4928 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4931 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4932 build_real (TREE_TYPE (arg), dconst0));
4933 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4936 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4938 imode = int_mode_for_mode (fmode);
4939 if (imode == BLKmode)
4941 temp = gen_lowpart (imode, temp);
4946 /* Handle targets with different FP word orders. */
4947 if (FLOAT_WORDS_BIG_ENDIAN)
4948 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4950 word = bitpos / BITS_PER_WORD;
4951 temp = operand_subword_force (temp, word, fmode);
4952 bitpos = bitpos % BITS_PER_WORD;
4955 /* Force the intermediate word_mode (or narrower) result into a
4956 register. This avoids attempting to create paradoxical SUBREGs
4957 of floating point modes below. */
4958 temp = force_reg (imode, temp);
4960 /* If the bitpos is within the "result mode" lowpart, the operation
4961 can be implement with a single bitwise AND. Otherwise, we need
4962 a right shift and an AND. */
4964 if (bitpos < GET_MODE_BITSIZE (rmode))
4966 double_int mask = double_int_setbit (double_int_zero, bitpos);
4968 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4969 temp = gen_lowpart (rmode, temp);
4970 temp = expand_binop (rmode, and_optab, temp,
4971 immed_double_int_const (mask, rmode),
4972 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4976 /* Perform a logical right shift to place the signbit in the least
4977 significant bit, then truncate the result to the desired mode
4978 and mask just this bit. */
4979 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4980 temp = gen_lowpart (rmode, temp);
4981 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4982 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4988 /* Expand fork or exec calls. TARGET is the desired target of the
4989 call. EXP is the call. FN is the
4990 identificator of the actual function. IGNORE is nonzero if the
4991 value is to be ignored. */
4994 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4999 /* If we are not profiling, just call the function. */
5000 if (!profile_arc_flag)
5003 /* Otherwise call the wrapper. This should be equivalent for the rest of
5004 compiler, so the code does not diverge, and the wrapper may run the
5005 code necessary for keeping the profiling sane. */
5007 switch (DECL_FUNCTION_CODE (fn))
5010 id = get_identifier ("__gcov_fork");
5013 case BUILT_IN_EXECL:
5014 id = get_identifier ("__gcov_execl");
5017 case BUILT_IN_EXECV:
5018 id = get_identifier ("__gcov_execv");
5021 case BUILT_IN_EXECLP:
5022 id = get_identifier ("__gcov_execlp");
5025 case BUILT_IN_EXECLE:
5026 id = get_identifier ("__gcov_execle");
5029 case BUILT_IN_EXECVP:
5030 id = get_identifier ("__gcov_execvp");
5033 case BUILT_IN_EXECVE:
5034 id = get_identifier ("__gcov_execve");
5041 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5042 FUNCTION_DECL, id, TREE_TYPE (fn));
5043 DECL_EXTERNAL (decl) = 1;
5044 TREE_PUBLIC (decl) = 1;
5045 DECL_ARTIFICIAL (decl) = 1;
5046 TREE_NOTHROW (decl) = 1;
5047 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5048 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5049 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5050 return expand_call (call, target, ignore);
5055 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5056 the pointer in these functions is void*, the tree optimizers may remove
5057 casts. The mode computed in expand_builtin isn't reliable either, due
5058 to __sync_bool_compare_and_swap.
5060 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5061 group of builtins. This gives us log2 of the mode size. */
5063 static inline enum machine_mode
5064 get_builtin_sync_mode (int fcode_diff)
5066 /* The size is not negotiable, so ask not to get BLKmode in return
5067 if the target indicates that a smaller size would be better. */
5068 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5071 /* Expand the memory expression LOC and return the appropriate memory operand
5072 for the builtin_sync operations. */
5075 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5079 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5080 addr = convert_memory_address (Pmode, addr);
5082 /* Note that we explicitly do not want any alias information for this
5083 memory, so that we kill all other live memories. Otherwise we don't
5084 satisfy the full barrier semantics of the intrinsic. */
5085 mem = validize_mem (gen_rtx_MEM (mode, addr));
5087 /* The alignment needs to be at least according to that of the mode. */
5088 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5089 get_pointer_alignment (loc)));
5090 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5091 MEM_VOLATILE_P (mem) = 1;
5096 /* Make sure an argument is in the right mode.
5097 EXP is the tree argument.
5098 MODE is the mode it should be in. */
5101 expand_expr_force_mode (tree exp, enum machine_mode mode)
5104 enum machine_mode old_mode;
5106 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5107 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5108 of CONST_INTs, where we know the old_mode only from the call argument. */
5110 old_mode = GET_MODE (val);
5111 if (old_mode == VOIDmode)
5112 old_mode = TYPE_MODE (TREE_TYPE (exp));
5113 val = convert_modes (mode, old_mode, val, 1);
5118 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5119 EXP is the CALL_EXPR. CODE is the rtx code
5120 that corresponds to the arithmetic or logical operation from the name;
5121 an exception here is that NOT actually means NAND. TARGET is an optional
5122 place for us to store the results; AFTER is true if this is the
5123 fetch_and_xxx form. */
5126 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5127 enum rtx_code code, bool after,
5131 location_t loc = EXPR_LOCATION (exp);
5133 if (code == NOT && warn_sync_nand)
5135 tree fndecl = get_callee_fndecl (exp);
5136 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5138 static bool warned_f_a_n, warned_n_a_f;
5142 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5143 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5144 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5145 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5146 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5150 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5151 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5152 warned_f_a_n = true;
5155 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5156 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5157 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5158 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5159 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5163 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5164 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5165 warned_n_a_f = true;
5173 /* Expand the operands. */
5174 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5175 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5177 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SEQ_CST,
5181 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5182 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5183 true if this is the boolean form. TARGET is a place for us to store the
5184 results; this is NOT optional if IS_BOOL is true. */
5187 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5188 bool is_bool, rtx target)
5190 rtx old_val, new_val, mem;
5193 /* Expand the operands. */
5194 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5195 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5196 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5198 pbool = poval = NULL;
5199 if (target != const0_rtx)
5206 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5207 false, MEMMODEL_SEQ_CST,
5214 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5215 general form is actually an atomic exchange, and some targets only
5216 support a reduced form with the second argument being a constant 1.
5217 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5221 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
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_exchange (target, mem, val, MEMMODEL_ACQUIRE, true);
5233 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5236 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5240 /* Expand the operands. */
5241 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5243 expand_atomic_store (mem, const0_rtx, MEMMODEL_RELEASE, true);
5246 /* Given an integer representing an ``enum memmodel'', verify its
5247 correctness and return the memory model enum. */
5249 static enum memmodel
5250 get_memmodel (tree exp)
5254 /* If the parameter is not a constant, it's a run time value so we'll just
5255 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5256 if (TREE_CODE (exp) != INTEGER_CST)
5257 return MEMMODEL_SEQ_CST;
5259 op = expand_normal (exp);
5260 if (INTVAL (op) < 0 || INTVAL (op) >= MEMMODEL_LAST)
5262 warning (OPT_Winvalid_memory_model,
5263 "invalid memory model argument to builtin");
5264 return MEMMODEL_SEQ_CST;
5266 return (enum memmodel) INTVAL (op);
5269 /* Expand the __atomic_exchange intrinsic:
5270 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5271 EXP is the CALL_EXPR.
5272 TARGET is an optional place for us to store the results. */
5275 expand_builtin_atomic_exchange (enum machine_mode mode, tree exp, rtx target)
5278 enum memmodel model;
5280 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5281 if (model == MEMMODEL_CONSUME)
5283 error ("invalid memory model for %<__atomic_exchange%>");
5287 if (!flag_inline_atomics)
5290 /* Expand the operands. */
5291 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5292 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5294 return expand_atomic_exchange (target, mem, val, model, false);
5297 /* Expand the __atomic_compare_exchange intrinsic:
5298 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5299 TYPE desired, BOOL weak,
5300 enum memmodel success,
5301 enum memmodel failure)
5302 EXP is the CALL_EXPR.
5303 TARGET is an optional place for us to store the results. */
5306 expand_builtin_atomic_compare_exchange (enum machine_mode mode, tree exp,
5309 rtx expect, desired, mem, oldval;
5310 enum memmodel success, failure;
5314 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5315 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5317 if (failure == MEMMODEL_RELEASE || failure == MEMMODEL_ACQ_REL)
5319 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5323 if (failure > success)
5325 error ("failure memory model cannot be stronger than success "
5326 "memory model for %<__atomic_compare_exchange%>");
5330 if (!flag_inline_atomics)
5333 /* Expand the operands. */
5334 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5336 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5337 expect = convert_memory_address (Pmode, expect);
5338 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5340 weak = CALL_EXPR_ARG (exp, 3);
5342 if (host_integerp (weak, 0) && tree_low_cst (weak, 0) != 0)
5345 oldval = copy_to_reg (gen_rtx_MEM (mode, expect));
5347 if (!expand_atomic_compare_and_swap ((target == const0_rtx ? NULL : &target),
5348 &oldval, mem, oldval, desired,
5349 is_weak, success, failure))
5352 emit_move_insn (gen_rtx_MEM (mode, expect), oldval);
5356 /* Expand the __atomic_load intrinsic:
5357 TYPE __atomic_load (TYPE *object, enum memmodel)
5358 EXP is the CALL_EXPR.
5359 TARGET is an optional place for us to store the results. */
5362 expand_builtin_atomic_load (enum machine_mode mode, tree exp, rtx target)
5365 enum memmodel model;
5367 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5368 if (model == MEMMODEL_RELEASE
5369 || model == MEMMODEL_ACQ_REL)
5371 error ("invalid memory model for %<__atomic_load%>");
5375 if (!flag_inline_atomics)
5378 /* Expand the operand. */
5379 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5381 return expand_atomic_load (target, mem, model);
5385 /* Expand the __atomic_store intrinsic:
5386 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5387 EXP is the CALL_EXPR.
5388 TARGET is an optional place for us to store the results. */
5391 expand_builtin_atomic_store (enum machine_mode mode, tree exp)
5394 enum memmodel model;
5396 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5397 if (model != MEMMODEL_RELAXED
5398 && model != MEMMODEL_SEQ_CST
5399 && model != MEMMODEL_RELEASE)
5401 error ("invalid memory model for %<__atomic_store%>");
5405 if (!flag_inline_atomics)
5408 /* Expand the operands. */
5409 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5410 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5412 return expand_atomic_store (mem, val, model, false);
5415 /* Expand the __atomic_fetch_XXX intrinsic:
5416 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5417 EXP is the CALL_EXPR.
5418 TARGET is an optional place for us to store the results.
5419 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5420 FETCH_AFTER is true if returning the result of the operation.
5421 FETCH_AFTER is false if returning the value before the operation.
5422 IGNORE is true if the result is not used.
5423 EXT_CALL is the correct builtin for an external call if this cannot be
5424 resolved to an instruction sequence. */
5427 expand_builtin_atomic_fetch_op (enum machine_mode mode, tree exp, rtx target,
5428 enum rtx_code code, bool fetch_after,
5429 bool ignore, enum built_in_function ext_call)
5432 enum memmodel model;
5436 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5438 /* Expand the operands. */
5439 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5440 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5442 /* Only try generating instructions if inlining is turned on. */
5443 if (flag_inline_atomics)
5445 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
5450 /* Return if a different routine isn't needed for the library call. */
5451 if (ext_call == BUILT_IN_NONE)
5454 /* Change the call to the specified function. */
5455 fndecl = get_callee_fndecl (exp);
5456 addr = CALL_EXPR_FN (exp);
5459 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
5460 TREE_OPERAND (addr, 0) = builtin_decl_explicit(ext_call);
5462 /* Expand the call here so we can emit trailing code. */
5463 ret = expand_call (exp, target, ignore);
5465 /* Replace the original function just in case it matters. */
5466 TREE_OPERAND (addr, 0) = fndecl;
5468 /* Then issue the arithmetic correction to return the right result. */
5473 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
5475 ret = expand_simple_unop (mode, NOT, ret, target, true);
5478 ret = expand_simple_binop (mode, code, ret, val, target, true,
5485 /* Expand an atomic clear operation.
5486 void _atomic_clear (BOOL *obj, enum memmodel)
5487 EXP is the call expression. */
5490 expand_builtin_atomic_clear (tree exp)
5492 enum machine_mode mode;
5494 enum memmodel model;
5496 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5497 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5498 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5500 if (model == MEMMODEL_ACQUIRE || model == MEMMODEL_ACQ_REL)
5502 error ("invalid memory model for %<__atomic_store%>");
5506 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5507 Failing that, a store is issued by __atomic_store. The only way this can
5508 fail is if the bool type is larger than a word size. Unlikely, but
5509 handle it anyway for completeness. Assume a single threaded model since
5510 there is no atomic support in this case, and no barriers are required. */
5511 ret = expand_atomic_store (mem, const0_rtx, model, true);
5513 emit_move_insn (mem, const0_rtx);
5517 /* Expand an atomic test_and_set operation.
5518 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5519 EXP is the call expression. */
5522 expand_builtin_atomic_test_and_set (tree exp)
5525 enum memmodel model;
5526 enum machine_mode mode;
5528 mode = mode_for_size (BOOL_TYPE_SIZE, MODE_INT, 0);
5529 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5530 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
5532 /* Try issuing an exchange. If it is lock free, or if there is a limited
5533 functionality __sync_lock_test_and_set, this will utilize it. */
5534 ret = expand_atomic_exchange (NULL_RTX, mem, const1_rtx, model, true);
5538 /* Otherwise, there is no lock free support for test and set. Simply
5539 perform a load and a store. Since this presumes a non-atomic architecture,
5540 also assume single threadedness and don't issue barriers either. */
5542 ret = gen_reg_rtx (mode);
5543 emit_move_insn (ret, mem);
5544 emit_move_insn (mem, const1_rtx);
5549 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5550 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5553 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
5556 enum machine_mode mode;
5557 unsigned int mode_align, type_align;
5559 if (TREE_CODE (arg0) != INTEGER_CST)
5562 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
5563 mode = mode_for_size (size, MODE_INT, 0);
5564 mode_align = GET_MODE_ALIGNMENT (mode);
5566 if (TREE_CODE (arg1) == INTEGER_CST && INTVAL (expand_normal (arg1)) == 0)
5567 type_align = mode_align;
5570 tree ttype = TREE_TYPE (arg1);
5572 /* This function is usually invoked and folded immediately by the front
5573 end before anything else has a chance to look at it. The pointer
5574 parameter at this point is usually cast to a void *, so check for that
5575 and look past the cast. */
5576 if (TREE_CODE (arg1) == NOP_EXPR && POINTER_TYPE_P (ttype)
5577 && VOID_TYPE_P (TREE_TYPE (ttype)))
5578 arg1 = TREE_OPERAND (arg1, 0);
5580 ttype = TREE_TYPE (arg1);
5581 gcc_assert (POINTER_TYPE_P (ttype));
5583 /* Get the underlying type of the object. */
5584 ttype = TREE_TYPE (ttype);
5585 type_align = TYPE_ALIGN (ttype);
5588 /* If the object has smaller alignment, the the lock free routines cannot
5590 if (type_align < mode_align)
5591 return integer_zero_node;
5593 /* Check if a compare_and_swap pattern exists for the mode which represents
5594 the required size. The pattern is not allowed to fail, so the existence
5595 of the pattern indicates support is present. */
5596 if (can_compare_and_swap_p (mode, true))
5597 return integer_one_node;
5599 return integer_zero_node;
5602 /* Return true if the parameters to call EXP represent an object which will
5603 always generate lock free instructions. The first argument represents the
5604 size of the object, and the second parameter is a pointer to the object
5605 itself. If NULL is passed for the object, then the result is based on
5606 typical alignment for an object of the specified size. Otherwise return
5610 expand_builtin_atomic_always_lock_free (tree exp)
5613 tree arg0 = CALL_EXPR_ARG (exp, 0);
5614 tree arg1 = CALL_EXPR_ARG (exp, 1);
5616 if (TREE_CODE (arg0) != INTEGER_CST)
5618 error ("non-constant argument 1 to __atomic_always_lock_free");
5622 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
5623 if (size == integer_one_node)
5628 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5629 is lock free on this architecture. */
5632 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
5634 if (!flag_inline_atomics)
5637 /* If it isn't always lock free, don't generate a result. */
5638 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == integer_one_node)
5639 return integer_one_node;
5644 /* Return true if the parameters to call EXP represent an object which will
5645 always generate lock free instructions. The first argument represents the
5646 size of the object, and the second parameter is a pointer to the object
5647 itself. If NULL is passed for the object, then the result is based on
5648 typical alignment for an object of the specified size. Otherwise return
5652 expand_builtin_atomic_is_lock_free (tree exp)
5655 tree arg0 = CALL_EXPR_ARG (exp, 0);
5656 tree arg1 = CALL_EXPR_ARG (exp, 1);
5658 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
5660 error ("non-integer argument 1 to __atomic_is_lock_free");
5664 if (!flag_inline_atomics)
5667 /* If the value is known at compile time, return the RTX for it. */
5668 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
5669 if (size == integer_one_node)
5675 /* This routine will either emit the mem_thread_fence pattern or issue a
5676 sync_synchronize to generate a fence for memory model MEMMODEL. */
5678 #ifndef HAVE_mem_thread_fence
5679 # define HAVE_mem_thread_fence 0
5680 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
5684 expand_builtin_mem_thread_fence (enum memmodel model)
5686 if (HAVE_mem_thread_fence)
5687 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
5688 else if (model != MEMMODEL_RELAXED)
5689 expand_builtin_sync_synchronize ();
5692 /* Expand the __atomic_thread_fence intrinsic:
5693 void __atomic_thread_fence (enum memmodel)
5694 EXP is the CALL_EXPR. */
5697 expand_builtin_atomic_thread_fence (tree exp)
5699 enum memmodel model;
5701 model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5702 expand_builtin_mem_thread_fence (model);
5705 /* This routine will either emit the mem_signal_fence pattern or issue a
5706 sync_synchronize to generate a fence for memory model MEMMODEL. */
5708 #ifndef HAVE_mem_signal_fence
5709 # define HAVE_mem_signal_fence 0
5710 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
5714 expand_builtin_mem_signal_fence (enum memmodel model)
5716 if (HAVE_mem_signal_fence)
5717 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
5718 else if (model != MEMMODEL_RELAXED)
5722 /* By default targets are coherent between a thread and the signal
5723 handler running on the same thread. Thus this really becomes a
5724 compiler barrier, in that stores must not be sunk past
5725 (or raised above) a given point. */
5727 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
5728 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
5729 rtvec_alloc (0), rtvec_alloc (0),
5730 rtvec_alloc (0), UNKNOWN_LOCATION);
5731 MEM_VOLATILE_P (asm_op) = 1;
5733 clob = gen_rtx_SCRATCH (VOIDmode);
5734 clob = gen_rtx_MEM (BLKmode, clob);
5735 clob = gen_rtx_CLOBBER (VOIDmode, clob);
5737 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
5741 /* Expand the __atomic_signal_fence intrinsic:
5742 void __atomic_signal_fence (enum memmodel)
5743 EXP is the CALL_EXPR. */
5746 expand_builtin_atomic_signal_fence (tree exp)
5748 enum memmodel model;
5750 model = get_memmodel (CALL_EXPR_ARG (exp, 0));
5751 expand_builtin_mem_signal_fence (model);
5754 /* Expand the __sync_synchronize intrinsic. */
5757 expand_builtin_sync_synchronize (void)
5760 VEC (tree, gc) *v_clobbers;
5762 #ifdef HAVE_memory_barrier
5763 if (HAVE_memory_barrier)
5765 emit_insn (gen_memory_barrier ());
5770 if (synchronize_libfunc != NULL_RTX)
5772 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
5776 /* If no explicit memory barrier instruction is available, create an
5777 empty asm stmt with a memory clobber. */
5778 v_clobbers = VEC_alloc (tree, gc, 1);
5779 VEC_quick_push (tree, v_clobbers,
5780 tree_cons (NULL, build_string (6, "memory"), NULL));
5781 x = gimple_build_asm_vec ("", NULL, NULL, v_clobbers, NULL);
5782 gimple_asm_set_volatile (x, true);
5783 expand_asm_stmt (x);
5787 /* Expand an expression EXP that calls a built-in function,
5788 with result going to TARGET if that's convenient
5789 (and in mode MODE if that's convenient).
5790 SUBTARGET may be used as the target for computing one of EXP's operands.
5791 IGNORE is nonzero if the value is to be ignored. */
5794 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5797 tree fndecl = get_callee_fndecl (exp);
5798 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5799 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5802 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5803 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5805 /* When not optimizing, generate calls to library functions for a certain
5808 && !called_as_built_in (fndecl)
5809 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5810 && fcode != BUILT_IN_ALLOCA
5811 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5812 && fcode != BUILT_IN_FREE)
5813 return expand_call (exp, target, ignore);
5815 /* The built-in function expanders test for target == const0_rtx
5816 to determine whether the function's result will be ignored. */
5818 target = const0_rtx;
5820 /* If the result of a pure or const built-in function is ignored, and
5821 none of its arguments are volatile, we can avoid expanding the
5822 built-in call and just evaluate the arguments for side-effects. */
5823 if (target == const0_rtx
5824 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5825 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5827 bool volatilep = false;
5829 call_expr_arg_iterator iter;
5831 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5832 if (TREE_THIS_VOLATILE (arg))
5840 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5841 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5848 CASE_FLT_FN (BUILT_IN_FABS):
5849 target = expand_builtin_fabs (exp, target, subtarget);
5854 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5855 target = expand_builtin_copysign (exp, target, subtarget);
5860 /* Just do a normal library call if we were unable to fold
5862 CASE_FLT_FN (BUILT_IN_CABS):
5865 CASE_FLT_FN (BUILT_IN_EXP):
5866 CASE_FLT_FN (BUILT_IN_EXP10):
5867 CASE_FLT_FN (BUILT_IN_POW10):
5868 CASE_FLT_FN (BUILT_IN_EXP2):
5869 CASE_FLT_FN (BUILT_IN_EXPM1):
5870 CASE_FLT_FN (BUILT_IN_LOGB):
5871 CASE_FLT_FN (BUILT_IN_LOG):
5872 CASE_FLT_FN (BUILT_IN_LOG10):
5873 CASE_FLT_FN (BUILT_IN_LOG2):
5874 CASE_FLT_FN (BUILT_IN_LOG1P):
5875 CASE_FLT_FN (BUILT_IN_TAN):
5876 CASE_FLT_FN (BUILT_IN_ASIN):
5877 CASE_FLT_FN (BUILT_IN_ACOS):
5878 CASE_FLT_FN (BUILT_IN_ATAN):
5879 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5880 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5881 because of possible accuracy problems. */
5882 if (! flag_unsafe_math_optimizations)
5884 CASE_FLT_FN (BUILT_IN_SQRT):
5885 CASE_FLT_FN (BUILT_IN_FLOOR):
5886 CASE_FLT_FN (BUILT_IN_CEIL):
5887 CASE_FLT_FN (BUILT_IN_TRUNC):
5888 CASE_FLT_FN (BUILT_IN_ROUND):
5889 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5890 CASE_FLT_FN (BUILT_IN_RINT):
5891 target = expand_builtin_mathfn (exp, target, subtarget);
5896 CASE_FLT_FN (BUILT_IN_FMA):
5897 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5902 CASE_FLT_FN (BUILT_IN_ILOGB):
5903 if (! flag_unsafe_math_optimizations)
5905 CASE_FLT_FN (BUILT_IN_ISINF):
5906 CASE_FLT_FN (BUILT_IN_FINITE):
5907 case BUILT_IN_ISFINITE:
5908 case BUILT_IN_ISNORMAL:
5909 target = expand_builtin_interclass_mathfn (exp, target);
5914 CASE_FLT_FN (BUILT_IN_ICEIL):
5915 CASE_FLT_FN (BUILT_IN_LCEIL):
5916 CASE_FLT_FN (BUILT_IN_LLCEIL):
5917 CASE_FLT_FN (BUILT_IN_LFLOOR):
5918 CASE_FLT_FN (BUILT_IN_IFLOOR):
5919 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5920 target = expand_builtin_int_roundingfn (exp, target);
5925 CASE_FLT_FN (BUILT_IN_IRINT):
5926 CASE_FLT_FN (BUILT_IN_LRINT):
5927 CASE_FLT_FN (BUILT_IN_LLRINT):
5928 CASE_FLT_FN (BUILT_IN_IROUND):
5929 CASE_FLT_FN (BUILT_IN_LROUND):
5930 CASE_FLT_FN (BUILT_IN_LLROUND):
5931 target = expand_builtin_int_roundingfn_2 (exp, target);
5936 CASE_FLT_FN (BUILT_IN_POWI):
5937 target = expand_builtin_powi (exp, target);
5942 CASE_FLT_FN (BUILT_IN_ATAN2):
5943 CASE_FLT_FN (BUILT_IN_LDEXP):
5944 CASE_FLT_FN (BUILT_IN_SCALB):
5945 CASE_FLT_FN (BUILT_IN_SCALBN):
5946 CASE_FLT_FN (BUILT_IN_SCALBLN):
5947 if (! flag_unsafe_math_optimizations)
5950 CASE_FLT_FN (BUILT_IN_FMOD):
5951 CASE_FLT_FN (BUILT_IN_REMAINDER):
5952 CASE_FLT_FN (BUILT_IN_DREM):
5953 CASE_FLT_FN (BUILT_IN_POW):
5954 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5959 CASE_FLT_FN (BUILT_IN_CEXPI):
5960 target = expand_builtin_cexpi (exp, target);
5961 gcc_assert (target);
5964 CASE_FLT_FN (BUILT_IN_SIN):
5965 CASE_FLT_FN (BUILT_IN_COS):
5966 if (! flag_unsafe_math_optimizations)
5968 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5973 CASE_FLT_FN (BUILT_IN_SINCOS):
5974 if (! flag_unsafe_math_optimizations)
5976 target = expand_builtin_sincos (exp);
5981 case BUILT_IN_APPLY_ARGS:
5982 return expand_builtin_apply_args ();
5984 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5985 FUNCTION with a copy of the parameters described by
5986 ARGUMENTS, and ARGSIZE. It returns a block of memory
5987 allocated on the stack into which is stored all the registers
5988 that might possibly be used for returning the result of a
5989 function. ARGUMENTS is the value returned by
5990 __builtin_apply_args. ARGSIZE is the number of bytes of
5991 arguments that must be copied. ??? How should this value be
5992 computed? We'll also need a safe worst case value for varargs
5994 case BUILT_IN_APPLY:
5995 if (!validate_arglist (exp, POINTER_TYPE,
5996 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5997 && !validate_arglist (exp, REFERENCE_TYPE,
5998 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6004 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6005 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6006 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6008 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6011 /* __builtin_return (RESULT) causes the function to return the
6012 value described by RESULT. RESULT is address of the block of
6013 memory returned by __builtin_apply. */
6014 case BUILT_IN_RETURN:
6015 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6016 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6019 case BUILT_IN_SAVEREGS:
6020 return expand_builtin_saveregs ();
6022 case BUILT_IN_VA_ARG_PACK:
6023 /* All valid uses of __builtin_va_arg_pack () are removed during
6025 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6028 case BUILT_IN_VA_ARG_PACK_LEN:
6029 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6031 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6034 /* Return the address of the first anonymous stack arg. */
6035 case BUILT_IN_NEXT_ARG:
6036 if (fold_builtin_next_arg (exp, false))
6038 return expand_builtin_next_arg ();
6040 case BUILT_IN_CLEAR_CACHE:
6041 target = expand_builtin___clear_cache (exp);
6046 case BUILT_IN_CLASSIFY_TYPE:
6047 return expand_builtin_classify_type (exp);
6049 case BUILT_IN_CONSTANT_P:
6052 case BUILT_IN_FRAME_ADDRESS:
6053 case BUILT_IN_RETURN_ADDRESS:
6054 return expand_builtin_frame_address (fndecl, exp);
6056 /* Returns the address of the area where the structure is returned.
6058 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6059 if (call_expr_nargs (exp) != 0
6060 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6061 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6064 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6066 case BUILT_IN_ALLOCA:
6067 case BUILT_IN_ALLOCA_WITH_ALIGN:
6068 /* If the allocation stems from the declaration of a variable-sized
6069 object, it cannot accumulate. */
6070 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
6075 case BUILT_IN_STACK_SAVE:
6076 return expand_stack_save ();
6078 case BUILT_IN_STACK_RESTORE:
6079 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6082 case BUILT_IN_BSWAP32:
6083 case BUILT_IN_BSWAP64:
6084 target = expand_builtin_bswap (exp, target, subtarget);
6090 CASE_INT_FN (BUILT_IN_FFS):
6091 case BUILT_IN_FFSIMAX:
6092 target = expand_builtin_unop (target_mode, exp, target,
6093 subtarget, ffs_optab);
6098 CASE_INT_FN (BUILT_IN_CLZ):
6099 case BUILT_IN_CLZIMAX:
6100 target = expand_builtin_unop (target_mode, exp, target,
6101 subtarget, clz_optab);
6106 CASE_INT_FN (BUILT_IN_CTZ):
6107 case BUILT_IN_CTZIMAX:
6108 target = expand_builtin_unop (target_mode, exp, target,
6109 subtarget, ctz_optab);
6114 CASE_INT_FN (BUILT_IN_CLRSB):
6115 case BUILT_IN_CLRSBIMAX:
6116 target = expand_builtin_unop (target_mode, exp, target,
6117 subtarget, clrsb_optab);
6122 CASE_INT_FN (BUILT_IN_POPCOUNT):
6123 case BUILT_IN_POPCOUNTIMAX:
6124 target = expand_builtin_unop (target_mode, exp, target,
6125 subtarget, popcount_optab);
6130 CASE_INT_FN (BUILT_IN_PARITY):
6131 case BUILT_IN_PARITYIMAX:
6132 target = expand_builtin_unop (target_mode, exp, target,
6133 subtarget, parity_optab);
6138 case BUILT_IN_STRLEN:
6139 target = expand_builtin_strlen (exp, target, target_mode);
6144 case BUILT_IN_STRCPY:
6145 target = expand_builtin_strcpy (exp, target);
6150 case BUILT_IN_STRNCPY:
6151 target = expand_builtin_strncpy (exp, target);
6156 case BUILT_IN_STPCPY:
6157 target = expand_builtin_stpcpy (exp, target, mode);
6162 case BUILT_IN_MEMCPY:
6163 target = expand_builtin_memcpy (exp, target);
6168 case BUILT_IN_MEMPCPY:
6169 target = expand_builtin_mempcpy (exp, target, mode);
6174 case BUILT_IN_MEMSET:
6175 target = expand_builtin_memset (exp, target, mode);
6180 case BUILT_IN_BZERO:
6181 target = expand_builtin_bzero (exp);
6186 case BUILT_IN_STRCMP:
6187 target = expand_builtin_strcmp (exp, target);
6192 case BUILT_IN_STRNCMP:
6193 target = expand_builtin_strncmp (exp, target, mode);
6199 case BUILT_IN_MEMCMP:
6200 target = expand_builtin_memcmp (exp, target, mode);
6205 case BUILT_IN_SETJMP:
6206 /* This should have been lowered to the builtins below. */
6209 case BUILT_IN_SETJMP_SETUP:
6210 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6211 and the receiver label. */
6212 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
6214 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6215 VOIDmode, EXPAND_NORMAL);
6216 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
6217 rtx label_r = label_rtx (label);
6219 /* This is copied from the handling of non-local gotos. */
6220 expand_builtin_setjmp_setup (buf_addr, label_r);
6221 nonlocal_goto_handler_labels
6222 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
6223 nonlocal_goto_handler_labels);
6224 /* ??? Do not let expand_label treat us as such since we would
6225 not want to be both on the list of non-local labels and on
6226 the list of forced labels. */
6227 FORCED_LABEL (label) = 0;
6232 case BUILT_IN_SETJMP_DISPATCHER:
6233 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6234 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6236 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6237 rtx label_r = label_rtx (label);
6239 /* Remove the dispatcher label from the list of non-local labels
6240 since the receiver labels have been added to it above. */
6241 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
6246 case BUILT_IN_SETJMP_RECEIVER:
6247 /* __builtin_setjmp_receiver is passed the receiver label. */
6248 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6250 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
6251 rtx label_r = label_rtx (label);
6253 expand_builtin_setjmp_receiver (label_r);
6258 /* __builtin_longjmp is passed a pointer to an array of five words.
6259 It's similar to the C library longjmp function but works with
6260 __builtin_setjmp above. */
6261 case BUILT_IN_LONGJMP:
6262 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6264 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
6265 VOIDmode, EXPAND_NORMAL);
6266 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
6268 if (value != const1_rtx)
6270 error ("%<__builtin_longjmp%> second argument must be 1");
6274 expand_builtin_longjmp (buf_addr, value);
6279 case BUILT_IN_NONLOCAL_GOTO:
6280 target = expand_builtin_nonlocal_goto (exp);
6285 /* This updates the setjmp buffer that is its argument with the value
6286 of the current stack pointer. */
6287 case BUILT_IN_UPDATE_SETJMP_BUF:
6288 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6291 = expand_normal (CALL_EXPR_ARG (exp, 0));
6293 expand_builtin_update_setjmp_buf (buf_addr);
6299 expand_builtin_trap ();
6302 case BUILT_IN_UNREACHABLE:
6303 expand_builtin_unreachable ();
6306 CASE_FLT_FN (BUILT_IN_SIGNBIT):
6307 case BUILT_IN_SIGNBITD32:
6308 case BUILT_IN_SIGNBITD64:
6309 case BUILT_IN_SIGNBITD128:
6310 target = expand_builtin_signbit (exp, target);
6315 /* Various hooks for the DWARF 2 __throw routine. */
6316 case BUILT_IN_UNWIND_INIT:
6317 expand_builtin_unwind_init ();
6319 case BUILT_IN_DWARF_CFA:
6320 return virtual_cfa_rtx;
6321 #ifdef DWARF2_UNWIND_INFO
6322 case BUILT_IN_DWARF_SP_COLUMN:
6323 return expand_builtin_dwarf_sp_column ();
6324 case BUILT_IN_INIT_DWARF_REG_SIZES:
6325 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
6328 case BUILT_IN_FROB_RETURN_ADDR:
6329 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
6330 case BUILT_IN_EXTRACT_RETURN_ADDR:
6331 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
6332 case BUILT_IN_EH_RETURN:
6333 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
6334 CALL_EXPR_ARG (exp, 1));
6336 #ifdef EH_RETURN_DATA_REGNO
6337 case BUILT_IN_EH_RETURN_DATA_REGNO:
6338 return expand_builtin_eh_return_data_regno (exp);
6340 case BUILT_IN_EXTEND_POINTER:
6341 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
6342 case BUILT_IN_EH_POINTER:
6343 return expand_builtin_eh_pointer (exp);
6344 case BUILT_IN_EH_FILTER:
6345 return expand_builtin_eh_filter (exp);
6346 case BUILT_IN_EH_COPY_VALUES:
6347 return expand_builtin_eh_copy_values (exp);
6349 case BUILT_IN_VA_START:
6350 return expand_builtin_va_start (exp);
6351 case BUILT_IN_VA_END:
6352 return expand_builtin_va_end (exp);
6353 case BUILT_IN_VA_COPY:
6354 return expand_builtin_va_copy (exp);
6355 case BUILT_IN_EXPECT:
6356 return expand_builtin_expect (exp, target);
6357 case BUILT_IN_ASSUME_ALIGNED:
6358 return expand_builtin_assume_aligned (exp, target);
6359 case BUILT_IN_PREFETCH:
6360 expand_builtin_prefetch (exp);
6363 case BUILT_IN_INIT_TRAMPOLINE:
6364 return expand_builtin_init_trampoline (exp);
6365 case BUILT_IN_ADJUST_TRAMPOLINE:
6366 return expand_builtin_adjust_trampoline (exp);
6369 case BUILT_IN_EXECL:
6370 case BUILT_IN_EXECV:
6371 case BUILT_IN_EXECLP:
6372 case BUILT_IN_EXECLE:
6373 case BUILT_IN_EXECVP:
6374 case BUILT_IN_EXECVE:
6375 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
6380 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
6381 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
6382 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
6383 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
6384 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
6385 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
6386 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
6391 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
6392 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
6393 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
6394 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
6395 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
6396 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
6397 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
6402 case BUILT_IN_SYNC_FETCH_AND_OR_1:
6403 case BUILT_IN_SYNC_FETCH_AND_OR_2:
6404 case BUILT_IN_SYNC_FETCH_AND_OR_4:
6405 case BUILT_IN_SYNC_FETCH_AND_OR_8:
6406 case BUILT_IN_SYNC_FETCH_AND_OR_16:
6407 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
6408 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
6413 case BUILT_IN_SYNC_FETCH_AND_AND_1:
6414 case BUILT_IN_SYNC_FETCH_AND_AND_2:
6415 case BUILT_IN_SYNC_FETCH_AND_AND_4:
6416 case BUILT_IN_SYNC_FETCH_AND_AND_8:
6417 case BUILT_IN_SYNC_FETCH_AND_AND_16:
6418 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
6419 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
6424 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
6425 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
6426 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
6427 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
6428 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
6429 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
6430 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
6435 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6436 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6437 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6438 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6439 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6440 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
6441 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
6446 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
6447 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
6448 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
6449 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
6450 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
6451 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
6452 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
6457 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
6458 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
6459 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
6460 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
6461 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
6462 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
6463 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
6468 case BUILT_IN_SYNC_OR_AND_FETCH_1:
6469 case BUILT_IN_SYNC_OR_AND_FETCH_2:
6470 case BUILT_IN_SYNC_OR_AND_FETCH_4:
6471 case BUILT_IN_SYNC_OR_AND_FETCH_8:
6472 case BUILT_IN_SYNC_OR_AND_FETCH_16:
6473 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
6474 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
6479 case BUILT_IN_SYNC_AND_AND_FETCH_1:
6480 case BUILT_IN_SYNC_AND_AND_FETCH_2:
6481 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6482 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6483 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6484 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6485 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
6490 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6491 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6492 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6493 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6494 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6495 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6496 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
6501 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6502 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6503 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6504 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6505 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6506 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6507 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
6512 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6513 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6514 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6515 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6516 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6517 if (mode == VOIDmode)
6518 mode = TYPE_MODE (boolean_type_node);
6519 if (!target || !register_operand (target, mode))
6520 target = gen_reg_rtx (mode);
6522 mode = get_builtin_sync_mode
6523 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6524 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6529 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6530 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6531 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6532 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6533 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6534 mode = get_builtin_sync_mode
6535 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6536 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6541 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6542 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6543 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6544 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6545 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6546 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6547 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6552 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6553 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6554 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6555 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6556 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6557 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6558 expand_builtin_sync_lock_release (mode, exp);
6561 case BUILT_IN_SYNC_SYNCHRONIZE:
6562 expand_builtin_sync_synchronize ();
6565 case BUILT_IN_ATOMIC_EXCHANGE_1:
6566 case BUILT_IN_ATOMIC_EXCHANGE_2:
6567 case BUILT_IN_ATOMIC_EXCHANGE_4:
6568 case BUILT_IN_ATOMIC_EXCHANGE_8:
6569 case BUILT_IN_ATOMIC_EXCHANGE_16:
6570 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
6571 target = expand_builtin_atomic_exchange (mode, exp, target);
6576 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
6577 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
6578 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
6579 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
6580 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
6582 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
6583 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
6588 case BUILT_IN_ATOMIC_LOAD_1:
6589 case BUILT_IN_ATOMIC_LOAD_2:
6590 case BUILT_IN_ATOMIC_LOAD_4:
6591 case BUILT_IN_ATOMIC_LOAD_8:
6592 case BUILT_IN_ATOMIC_LOAD_16:
6593 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
6594 target = expand_builtin_atomic_load (mode, exp, target);
6599 case BUILT_IN_ATOMIC_STORE_1:
6600 case BUILT_IN_ATOMIC_STORE_2:
6601 case BUILT_IN_ATOMIC_STORE_4:
6602 case BUILT_IN_ATOMIC_STORE_8:
6603 case BUILT_IN_ATOMIC_STORE_16:
6604 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
6605 target = expand_builtin_atomic_store (mode, exp);
6610 case BUILT_IN_ATOMIC_ADD_FETCH_1:
6611 case BUILT_IN_ATOMIC_ADD_FETCH_2:
6612 case BUILT_IN_ATOMIC_ADD_FETCH_4:
6613 case BUILT_IN_ATOMIC_ADD_FETCH_8:
6614 case BUILT_IN_ATOMIC_ADD_FETCH_16:
6616 enum built_in_function lib;
6617 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
6618 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
6619 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
6620 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
6626 case BUILT_IN_ATOMIC_SUB_FETCH_1:
6627 case BUILT_IN_ATOMIC_SUB_FETCH_2:
6628 case BUILT_IN_ATOMIC_SUB_FETCH_4:
6629 case BUILT_IN_ATOMIC_SUB_FETCH_8:
6630 case BUILT_IN_ATOMIC_SUB_FETCH_16:
6632 enum built_in_function lib;
6633 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
6634 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
6635 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
6636 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
6642 case BUILT_IN_ATOMIC_AND_FETCH_1:
6643 case BUILT_IN_ATOMIC_AND_FETCH_2:
6644 case BUILT_IN_ATOMIC_AND_FETCH_4:
6645 case BUILT_IN_ATOMIC_AND_FETCH_8:
6646 case BUILT_IN_ATOMIC_AND_FETCH_16:
6648 enum built_in_function lib;
6649 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
6650 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
6651 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
6652 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
6658 case BUILT_IN_ATOMIC_NAND_FETCH_1:
6659 case BUILT_IN_ATOMIC_NAND_FETCH_2:
6660 case BUILT_IN_ATOMIC_NAND_FETCH_4:
6661 case BUILT_IN_ATOMIC_NAND_FETCH_8:
6662 case BUILT_IN_ATOMIC_NAND_FETCH_16:
6664 enum built_in_function lib;
6665 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
6666 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
6667 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
6668 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
6674 case BUILT_IN_ATOMIC_XOR_FETCH_1:
6675 case BUILT_IN_ATOMIC_XOR_FETCH_2:
6676 case BUILT_IN_ATOMIC_XOR_FETCH_4:
6677 case BUILT_IN_ATOMIC_XOR_FETCH_8:
6678 case BUILT_IN_ATOMIC_XOR_FETCH_16:
6680 enum built_in_function lib;
6681 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
6682 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
6683 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
6684 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
6690 case BUILT_IN_ATOMIC_OR_FETCH_1:
6691 case BUILT_IN_ATOMIC_OR_FETCH_2:
6692 case BUILT_IN_ATOMIC_OR_FETCH_4:
6693 case BUILT_IN_ATOMIC_OR_FETCH_8:
6694 case BUILT_IN_ATOMIC_OR_FETCH_16:
6696 enum built_in_function lib;
6697 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
6698 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
6699 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
6700 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
6706 case BUILT_IN_ATOMIC_FETCH_ADD_1:
6707 case BUILT_IN_ATOMIC_FETCH_ADD_2:
6708 case BUILT_IN_ATOMIC_FETCH_ADD_4:
6709 case BUILT_IN_ATOMIC_FETCH_ADD_8:
6710 case BUILT_IN_ATOMIC_FETCH_ADD_16:
6711 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
6712 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
6713 ignore, BUILT_IN_NONE);
6718 case BUILT_IN_ATOMIC_FETCH_SUB_1:
6719 case BUILT_IN_ATOMIC_FETCH_SUB_2:
6720 case BUILT_IN_ATOMIC_FETCH_SUB_4:
6721 case BUILT_IN_ATOMIC_FETCH_SUB_8:
6722 case BUILT_IN_ATOMIC_FETCH_SUB_16:
6723 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
6724 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
6725 ignore, BUILT_IN_NONE);
6730 case BUILT_IN_ATOMIC_FETCH_AND_1:
6731 case BUILT_IN_ATOMIC_FETCH_AND_2:
6732 case BUILT_IN_ATOMIC_FETCH_AND_4:
6733 case BUILT_IN_ATOMIC_FETCH_AND_8:
6734 case BUILT_IN_ATOMIC_FETCH_AND_16:
6735 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
6736 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
6737 ignore, BUILT_IN_NONE);
6742 case BUILT_IN_ATOMIC_FETCH_NAND_1:
6743 case BUILT_IN_ATOMIC_FETCH_NAND_2:
6744 case BUILT_IN_ATOMIC_FETCH_NAND_4:
6745 case BUILT_IN_ATOMIC_FETCH_NAND_8:
6746 case BUILT_IN_ATOMIC_FETCH_NAND_16:
6747 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
6748 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
6749 ignore, BUILT_IN_NONE);
6754 case BUILT_IN_ATOMIC_FETCH_XOR_1:
6755 case BUILT_IN_ATOMIC_FETCH_XOR_2:
6756 case BUILT_IN_ATOMIC_FETCH_XOR_4:
6757 case BUILT_IN_ATOMIC_FETCH_XOR_8:
6758 case BUILT_IN_ATOMIC_FETCH_XOR_16:
6759 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
6760 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
6761 ignore, BUILT_IN_NONE);
6766 case BUILT_IN_ATOMIC_FETCH_OR_1:
6767 case BUILT_IN_ATOMIC_FETCH_OR_2:
6768 case BUILT_IN_ATOMIC_FETCH_OR_4:
6769 case BUILT_IN_ATOMIC_FETCH_OR_8:
6770 case BUILT_IN_ATOMIC_FETCH_OR_16:
6771 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
6772 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
6773 ignore, BUILT_IN_NONE);
6778 case BUILT_IN_ATOMIC_TEST_AND_SET:
6779 return expand_builtin_atomic_test_and_set (exp);
6781 case BUILT_IN_ATOMIC_CLEAR:
6782 return expand_builtin_atomic_clear (exp);
6784 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
6785 return expand_builtin_atomic_always_lock_free (exp);
6787 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
6788 target = expand_builtin_atomic_is_lock_free (exp);
6793 case BUILT_IN_ATOMIC_THREAD_FENCE:
6794 expand_builtin_atomic_thread_fence (exp);
6797 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
6798 expand_builtin_atomic_signal_fence (exp);
6801 case BUILT_IN_OBJECT_SIZE:
6802 return expand_builtin_object_size (exp);
6804 case BUILT_IN_MEMCPY_CHK:
6805 case BUILT_IN_MEMPCPY_CHK:
6806 case BUILT_IN_MEMMOVE_CHK:
6807 case BUILT_IN_MEMSET_CHK:
6808 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6813 case BUILT_IN_STRCPY_CHK:
6814 case BUILT_IN_STPCPY_CHK:
6815 case BUILT_IN_STRNCPY_CHK:
6816 case BUILT_IN_STRCAT_CHK:
6817 case BUILT_IN_STRNCAT_CHK:
6818 case BUILT_IN_SNPRINTF_CHK:
6819 case BUILT_IN_VSNPRINTF_CHK:
6820 maybe_emit_chk_warning (exp, fcode);
6823 case BUILT_IN_SPRINTF_CHK:
6824 case BUILT_IN_VSPRINTF_CHK:
6825 maybe_emit_sprintf_chk_warning (exp, fcode);
6829 if (warn_free_nonheap_object)
6830 maybe_emit_free_warning (exp);
6833 default: /* just do library call, if unknown builtin */
6837 /* The switch statement above can drop through to cause the function
6838 to be called normally. */
6839 return expand_call (exp, target, ignore);
6842 /* Determine whether a tree node represents a call to a built-in
6843 function. If the tree T is a call to a built-in function with
6844 the right number of arguments of the appropriate types, return
6845 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6846 Otherwise the return value is END_BUILTINS. */
6848 enum built_in_function
6849 builtin_mathfn_code (const_tree t)
6851 const_tree fndecl, arg, parmlist;
6852 const_tree argtype, parmtype;
6853 const_call_expr_arg_iterator iter;
6855 if (TREE_CODE (t) != CALL_EXPR
6856 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6857 return END_BUILTINS;
6859 fndecl = get_callee_fndecl (t);
6860 if (fndecl == NULL_TREE
6861 || TREE_CODE (fndecl) != FUNCTION_DECL
6862 || ! DECL_BUILT_IN (fndecl)
6863 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6864 return END_BUILTINS;
6866 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6867 init_const_call_expr_arg_iterator (t, &iter);
6868 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6870 /* If a function doesn't take a variable number of arguments,
6871 the last element in the list will have type `void'. */
6872 parmtype = TREE_VALUE (parmlist);
6873 if (VOID_TYPE_P (parmtype))
6875 if (more_const_call_expr_args_p (&iter))
6876 return END_BUILTINS;
6877 return DECL_FUNCTION_CODE (fndecl);
6880 if (! more_const_call_expr_args_p (&iter))
6881 return END_BUILTINS;
6883 arg = next_const_call_expr_arg (&iter);
6884 argtype = TREE_TYPE (arg);
6886 if (SCALAR_FLOAT_TYPE_P (parmtype))
6888 if (! SCALAR_FLOAT_TYPE_P (argtype))
6889 return END_BUILTINS;
6891 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6893 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6894 return END_BUILTINS;
6896 else if (POINTER_TYPE_P (parmtype))
6898 if (! POINTER_TYPE_P (argtype))
6899 return END_BUILTINS;
6901 else if (INTEGRAL_TYPE_P (parmtype))
6903 if (! INTEGRAL_TYPE_P (argtype))
6904 return END_BUILTINS;
6907 return END_BUILTINS;
6910 /* Variable-length argument list. */
6911 return DECL_FUNCTION_CODE (fndecl);
6914 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6915 evaluate to a constant. */
6918 fold_builtin_constant_p (tree arg)
6920 /* We return 1 for a numeric type that's known to be a constant
6921 value at compile-time or for an aggregate type that's a
6922 literal constant. */
6925 /* If we know this is a constant, emit the constant of one. */
6926 if (CONSTANT_CLASS_P (arg)
6927 || (TREE_CODE (arg) == CONSTRUCTOR
6928 && TREE_CONSTANT (arg)))
6929 return integer_one_node;
6930 if (TREE_CODE (arg) == ADDR_EXPR)
6932 tree op = TREE_OPERAND (arg, 0);
6933 if (TREE_CODE (op) == STRING_CST
6934 || (TREE_CODE (op) == ARRAY_REF
6935 && integer_zerop (TREE_OPERAND (op, 1))
6936 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6937 return integer_one_node;
6940 /* If this expression has side effects, show we don't know it to be a
6941 constant. Likewise if it's a pointer or aggregate type since in
6942 those case we only want literals, since those are only optimized
6943 when generating RTL, not later.
6944 And finally, if we are compiling an initializer, not code, we
6945 need to return a definite result now; there's not going to be any
6946 more optimization done. */
6947 if (TREE_SIDE_EFFECTS (arg)
6948 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6949 || POINTER_TYPE_P (TREE_TYPE (arg))
6951 || folding_initializer)
6952 return integer_zero_node;
6957 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6958 return it as a truthvalue. */
6961 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6963 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6965 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
6966 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6967 ret_type = TREE_TYPE (TREE_TYPE (fn));
6968 pred_type = TREE_VALUE (arg_types);
6969 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6971 pred = fold_convert_loc (loc, pred_type, pred);
6972 expected = fold_convert_loc (loc, expected_type, expected);
6973 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6975 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6976 build_int_cst (ret_type, 0));
6979 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6980 NULL_TREE if no simplification is possible. */
6983 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6985 tree inner, fndecl, inner_arg0;
6986 enum tree_code code;
6988 /* Distribute the expected value over short-circuiting operators.
6989 See through the cast from truthvalue_type_node to long. */
6991 while (TREE_CODE (inner_arg0) == NOP_EXPR
6992 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
6993 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
6994 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
6996 /* If this is a builtin_expect within a builtin_expect keep the
6997 inner one. See through a comparison against a constant. It
6998 might have been added to create a thruthvalue. */
7001 if (COMPARISON_CLASS_P (inner)
7002 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
7003 inner = TREE_OPERAND (inner, 0);
7005 if (TREE_CODE (inner) == CALL_EXPR
7006 && (fndecl = get_callee_fndecl (inner))
7007 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
7008 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
7012 code = TREE_CODE (inner);
7013 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
7015 tree op0 = TREE_OPERAND (inner, 0);
7016 tree op1 = TREE_OPERAND (inner, 1);
7018 op0 = build_builtin_expect_predicate (loc, op0, arg1);
7019 op1 = build_builtin_expect_predicate (loc, op1, arg1);
7020 inner = build2 (code, TREE_TYPE (inner), op0, op1);
7022 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
7025 /* If the argument isn't invariant then there's nothing else we can do. */
7026 if (!TREE_CONSTANT (inner_arg0))
7029 /* If we expect that a comparison against the argument will fold to
7030 a constant return the constant. In practice, this means a true
7031 constant or the address of a non-weak symbol. */
7034 if (TREE_CODE (inner) == ADDR_EXPR)
7038 inner = TREE_OPERAND (inner, 0);
7040 while (TREE_CODE (inner) == COMPONENT_REF
7041 || TREE_CODE (inner) == ARRAY_REF);
7042 if ((TREE_CODE (inner) == VAR_DECL
7043 || TREE_CODE (inner) == FUNCTION_DECL)
7044 && DECL_WEAK (inner))
7048 /* Otherwise, ARG0 already has the proper type for the return value. */
7052 /* Fold a call to __builtin_classify_type with argument ARG. */
7055 fold_builtin_classify_type (tree arg)
7058 return build_int_cst (integer_type_node, no_type_class);
7060 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
7063 /* Fold a call to __builtin_strlen with argument ARG. */
7066 fold_builtin_strlen (location_t loc, tree type, tree arg)
7068 if (!validate_arg (arg, POINTER_TYPE))
7072 tree len = c_strlen (arg, 0);
7075 return fold_convert_loc (loc, type, len);
7081 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7084 fold_builtin_inf (location_t loc, tree type, int warn)
7086 REAL_VALUE_TYPE real;
7088 /* __builtin_inff is intended to be usable to define INFINITY on all
7089 targets. If an infinity is not available, INFINITY expands "to a
7090 positive constant of type float that overflows at translation
7091 time", footnote "In this case, using INFINITY will violate the
7092 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7093 Thus we pedwarn to ensure this constraint violation is
7095 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
7096 pedwarn (loc, 0, "target format does not support infinity");
7099 return build_real (type, real);
7102 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7105 fold_builtin_nan (tree arg, tree type, int quiet)
7107 REAL_VALUE_TYPE real;
7110 if (!validate_arg (arg, POINTER_TYPE))
7112 str = c_getstr (arg);
7116 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
7119 return build_real (type, real);
7122 /* Return true if the floating point expression T has an integer value.
7123 We also allow +Inf, -Inf and NaN to be considered integer values. */
7126 integer_valued_real_p (tree t)
7128 switch (TREE_CODE (t))
7135 return integer_valued_real_p (TREE_OPERAND (t, 0));
7140 return integer_valued_real_p (TREE_OPERAND (t, 1));
7147 return integer_valued_real_p (TREE_OPERAND (t, 0))
7148 && integer_valued_real_p (TREE_OPERAND (t, 1));
7151 return integer_valued_real_p (TREE_OPERAND (t, 1))
7152 && integer_valued_real_p (TREE_OPERAND (t, 2));
7155 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
7159 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7160 if (TREE_CODE (type) == INTEGER_TYPE)
7162 if (TREE_CODE (type) == REAL_TYPE)
7163 return integer_valued_real_p (TREE_OPERAND (t, 0));
7168 switch (builtin_mathfn_code (t))
7170 CASE_FLT_FN (BUILT_IN_CEIL):
7171 CASE_FLT_FN (BUILT_IN_FLOOR):
7172 CASE_FLT_FN (BUILT_IN_NEARBYINT):
7173 CASE_FLT_FN (BUILT_IN_RINT):
7174 CASE_FLT_FN (BUILT_IN_ROUND):
7175 CASE_FLT_FN (BUILT_IN_TRUNC):
7178 CASE_FLT_FN (BUILT_IN_FMIN):
7179 CASE_FLT_FN (BUILT_IN_FMAX):
7180 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
7181 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
7194 /* FNDECL is assumed to be a builtin where truncation can be propagated
7195 across (for instance floor((double)f) == (double)floorf (f).
7196 Do the transformation for a call with argument ARG. */
7199 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
7201 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7203 if (!validate_arg (arg, REAL_TYPE))
7206 /* Integer rounding functions are idempotent. */
7207 if (fcode == builtin_mathfn_code (arg))
7210 /* If argument is already integer valued, and we don't need to worry
7211 about setting errno, there's no need to perform rounding. */
7212 if (! flag_errno_math && integer_valued_real_p (arg))
7217 tree arg0 = strip_float_extensions (arg);
7218 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
7219 tree newtype = TREE_TYPE (arg0);
7222 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7223 && (decl = mathfn_built_in (newtype, fcode)))
7224 return fold_convert_loc (loc, ftype,
7225 build_call_expr_loc (loc, decl, 1,
7226 fold_convert_loc (loc,
7233 /* FNDECL is assumed to be builtin which can narrow the FP type of
7234 the argument, for instance lround((double)f) -> lroundf (f).
7235 Do the transformation for a call with argument ARG. */
7238 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
7240 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7242 if (!validate_arg (arg, REAL_TYPE))
7245 /* If argument is already integer valued, and we don't need to worry
7246 about setting errno, there's no need to perform rounding. */
7247 if (! flag_errno_math && integer_valued_real_p (arg))
7248 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7249 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7253 tree ftype = TREE_TYPE (arg);
7254 tree arg0 = strip_float_extensions (arg);
7255 tree newtype = TREE_TYPE (arg0);
7258 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
7259 && (decl = mathfn_built_in (newtype, fcode)))
7260 return build_call_expr_loc (loc, decl, 1,
7261 fold_convert_loc (loc, newtype, arg0));
7264 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7265 sizeof (int) == sizeof (long). */
7266 if (TYPE_PRECISION (integer_type_node)
7267 == TYPE_PRECISION (long_integer_type_node))
7269 tree newfn = NULL_TREE;
7272 CASE_FLT_FN (BUILT_IN_ICEIL):
7273 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7276 CASE_FLT_FN (BUILT_IN_IFLOOR):
7277 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7280 CASE_FLT_FN (BUILT_IN_IROUND):
7281 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7284 CASE_FLT_FN (BUILT_IN_IRINT):
7285 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7294 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7295 return fold_convert_loc (loc,
7296 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7300 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7301 sizeof (long long) == sizeof (long). */
7302 if (TYPE_PRECISION (long_long_integer_type_node)
7303 == TYPE_PRECISION (long_integer_type_node))
7305 tree newfn = NULL_TREE;
7308 CASE_FLT_FN (BUILT_IN_LLCEIL):
7309 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
7312 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7313 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
7316 CASE_FLT_FN (BUILT_IN_LLROUND):
7317 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
7320 CASE_FLT_FN (BUILT_IN_LLRINT):
7321 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
7330 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
7331 return fold_convert_loc (loc,
7332 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
7339 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7340 return type. Return NULL_TREE if no simplification can be made. */
7343 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
7347 if (!validate_arg (arg, COMPLEX_TYPE)
7348 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7351 /* Calculate the result when the argument is a constant. */
7352 if (TREE_CODE (arg) == COMPLEX_CST
7353 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
7357 if (TREE_CODE (arg) == COMPLEX_EXPR)
7359 tree real = TREE_OPERAND (arg, 0);
7360 tree imag = TREE_OPERAND (arg, 1);
7362 /* If either part is zero, cabs is fabs of the other. */
7363 if (real_zerop (real))
7364 return fold_build1_loc (loc, ABS_EXPR, type, imag);
7365 if (real_zerop (imag))
7366 return fold_build1_loc (loc, ABS_EXPR, type, real);
7368 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7369 if (flag_unsafe_math_optimizations
7370 && operand_equal_p (real, imag, OEP_PURE_SAME))
7372 const REAL_VALUE_TYPE sqrt2_trunc
7373 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7375 return fold_build2_loc (loc, MULT_EXPR, type,
7376 fold_build1_loc (loc, ABS_EXPR, type, real),
7377 build_real (type, sqrt2_trunc));
7381 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7382 if (TREE_CODE (arg) == NEGATE_EXPR
7383 || TREE_CODE (arg) == CONJ_EXPR)
7384 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
7386 /* Don't do this when optimizing for size. */
7387 if (flag_unsafe_math_optimizations
7388 && optimize && optimize_function_for_speed_p (cfun))
7390 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7392 if (sqrtfn != NULL_TREE)
7394 tree rpart, ipart, result;
7396 arg = builtin_save_expr (arg);
7398 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
7399 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
7401 rpart = builtin_save_expr (rpart);
7402 ipart = builtin_save_expr (ipart);
7404 result = fold_build2_loc (loc, PLUS_EXPR, type,
7405 fold_build2_loc (loc, MULT_EXPR, type,
7407 fold_build2_loc (loc, MULT_EXPR, type,
7410 return build_call_expr_loc (loc, sqrtfn, 1, result);
7417 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7418 complex tree type of the result. If NEG is true, the imaginary
7419 zero is negative. */
7422 build_complex_cproj (tree type, bool neg)
7424 REAL_VALUE_TYPE rinf, rzero = dconst0;
7428 return build_complex (type, build_real (TREE_TYPE (type), rinf),
7429 build_real (TREE_TYPE (type), rzero));
7432 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7433 return type. Return NULL_TREE if no simplification can be made. */
7436 fold_builtin_cproj (location_t loc, tree arg, tree type)
7438 if (!validate_arg (arg, COMPLEX_TYPE)
7439 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
7442 /* If there are no infinities, return arg. */
7443 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
7444 return non_lvalue_loc (loc, arg);
7446 /* Calculate the result when the argument is a constant. */
7447 if (TREE_CODE (arg) == COMPLEX_CST)
7449 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
7450 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
7452 if (real_isinf (real) || real_isinf (imag))
7453 return build_complex_cproj (type, imag->sign);
7457 else if (TREE_CODE (arg) == COMPLEX_EXPR)
7459 tree real = TREE_OPERAND (arg, 0);
7460 tree imag = TREE_OPERAND (arg, 1);
7465 /* If the real part is inf and the imag part is known to be
7466 nonnegative, return (inf + 0i). Remember side-effects are
7467 possible in the imag part. */
7468 if (TREE_CODE (real) == REAL_CST
7469 && real_isinf (TREE_REAL_CST_PTR (real))
7470 && tree_expr_nonnegative_p (imag))
7471 return omit_one_operand_loc (loc, type,
7472 build_complex_cproj (type, false),
7475 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7476 Remember side-effects are possible in the real part. */
7477 if (TREE_CODE (imag) == REAL_CST
7478 && real_isinf (TREE_REAL_CST_PTR (imag)))
7480 omit_one_operand_loc (loc, type,
7481 build_complex_cproj (type, TREE_REAL_CST_PTR
7482 (imag)->sign), arg);
7488 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7489 Return NULL_TREE if no simplification can be made. */
7492 fold_builtin_sqrt (location_t loc, tree arg, tree type)
7495 enum built_in_function fcode;
7498 if (!validate_arg (arg, REAL_TYPE))
7501 /* Calculate the result when the argument is a constant. */
7502 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
7505 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7506 fcode = builtin_mathfn_code (arg);
7507 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
7509 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7510 arg = fold_build2_loc (loc, MULT_EXPR, type,
7511 CALL_EXPR_ARG (arg, 0),
7512 build_real (type, dconsthalf));
7513 return build_call_expr_loc (loc, expfn, 1, arg);
7516 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7517 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
7519 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7523 tree arg0 = CALL_EXPR_ARG (arg, 0);
7525 /* The inner root was either sqrt or cbrt. */
7526 /* This was a conditional expression but it triggered a bug
7528 REAL_VALUE_TYPE dconstroot;
7529 if (BUILTIN_SQRT_P (fcode))
7530 dconstroot = dconsthalf;
7532 dconstroot = dconst_third ();
7534 /* Adjust for the outer root. */
7535 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7536 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7537 tree_root = build_real (type, dconstroot);
7538 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7542 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7543 if (flag_unsafe_math_optimizations
7544 && (fcode == BUILT_IN_POW
7545 || fcode == BUILT_IN_POWF
7546 || fcode == BUILT_IN_POWL))
7548 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7549 tree arg0 = CALL_EXPR_ARG (arg, 0);
7550 tree arg1 = CALL_EXPR_ARG (arg, 1);
7552 if (!tree_expr_nonnegative_p (arg0))
7553 arg0 = build1 (ABS_EXPR, type, arg0);
7554 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7555 build_real (type, dconsthalf));
7556 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
7562 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7563 Return NULL_TREE if no simplification can be made. */
7566 fold_builtin_cbrt (location_t loc, tree arg, tree type)
7568 const enum built_in_function fcode = builtin_mathfn_code (arg);
7571 if (!validate_arg (arg, REAL_TYPE))
7574 /* Calculate the result when the argument is a constant. */
7575 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
7578 if (flag_unsafe_math_optimizations)
7580 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7581 if (BUILTIN_EXPONENT_P (fcode))
7583 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7584 const REAL_VALUE_TYPE third_trunc =
7585 real_value_truncate (TYPE_MODE (type), dconst_third ());
7586 arg = fold_build2_loc (loc, MULT_EXPR, type,
7587 CALL_EXPR_ARG (arg, 0),
7588 build_real (type, third_trunc));
7589 return build_call_expr_loc (loc, expfn, 1, arg);
7592 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7593 if (BUILTIN_SQRT_P (fcode))
7595 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7599 tree arg0 = CALL_EXPR_ARG (arg, 0);
7601 REAL_VALUE_TYPE dconstroot = dconst_third ();
7603 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
7604 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7605 tree_root = build_real (type, dconstroot);
7606 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7610 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7611 if (BUILTIN_CBRT_P (fcode))
7613 tree arg0 = CALL_EXPR_ARG (arg, 0);
7614 if (tree_expr_nonnegative_p (arg0))
7616 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7621 REAL_VALUE_TYPE dconstroot;
7623 real_arithmetic (&dconstroot, MULT_EXPR,
7624 dconst_third_ptr (), dconst_third_ptr ());
7625 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
7626 tree_root = build_real (type, dconstroot);
7627 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
7632 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7633 if (fcode == BUILT_IN_POW
7634 || fcode == BUILT_IN_POWF
7635 || fcode == BUILT_IN_POWL)
7637 tree arg00 = CALL_EXPR_ARG (arg, 0);
7638 tree arg01 = CALL_EXPR_ARG (arg, 1);
7639 if (tree_expr_nonnegative_p (arg00))
7641 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
7642 const REAL_VALUE_TYPE dconstroot
7643 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7644 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
7645 build_real (type, dconstroot));
7646 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
7653 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7654 TYPE is the type of the return value. Return NULL_TREE if no
7655 simplification can be made. */
7658 fold_builtin_cos (location_t loc,
7659 tree arg, tree type, tree fndecl)
7663 if (!validate_arg (arg, REAL_TYPE))
7666 /* Calculate the result when the argument is a constant. */
7667 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
7670 /* Optimize cos(-x) into cos (x). */
7671 if ((narg = fold_strip_sign_ops (arg)))
7672 return build_call_expr_loc (loc, fndecl, 1, narg);
7677 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7678 Return NULL_TREE if no simplification can be made. */
7681 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
7683 if (validate_arg (arg, REAL_TYPE))
7687 /* Calculate the result when the argument is a constant. */
7688 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
7691 /* Optimize cosh(-x) into cosh (x). */
7692 if ((narg = fold_strip_sign_ops (arg)))
7693 return build_call_expr_loc (loc, fndecl, 1, narg);
7699 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7700 argument ARG. TYPE is the type of the return value. Return
7701 NULL_TREE if no simplification can be made. */
7704 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
7707 if (validate_arg (arg, COMPLEX_TYPE)
7708 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
7712 /* Calculate the result when the argument is a constant. */
7713 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7716 /* Optimize fn(-x) into fn(x). */
7717 if ((tmp = fold_strip_sign_ops (arg)))
7718 return build_call_expr_loc (loc, fndecl, 1, tmp);
7724 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7725 Return NULL_TREE if no simplification can be made. */
7728 fold_builtin_tan (tree arg, tree type)
7730 enum built_in_function fcode;
7733 if (!validate_arg (arg, REAL_TYPE))
7736 /* Calculate the result when the argument is a constant. */
7737 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7740 /* Optimize tan(atan(x)) = x. */
7741 fcode = builtin_mathfn_code (arg);
7742 if (flag_unsafe_math_optimizations
7743 && (fcode == BUILT_IN_ATAN
7744 || fcode == BUILT_IN_ATANF
7745 || fcode == BUILT_IN_ATANL))
7746 return CALL_EXPR_ARG (arg, 0);
7751 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7752 NULL_TREE if no simplification can be made. */
7755 fold_builtin_sincos (location_t loc,
7756 tree arg0, tree arg1, tree arg2)
7761 if (!validate_arg (arg0, REAL_TYPE)
7762 || !validate_arg (arg1, POINTER_TYPE)
7763 || !validate_arg (arg2, POINTER_TYPE))
7766 type = TREE_TYPE (arg0);
7768 /* Calculate the result when the argument is a constant. */
7769 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7772 /* Canonicalize sincos to cexpi. */
7773 if (!TARGET_C99_FUNCTIONS)
7775 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7779 call = build_call_expr_loc (loc, fn, 1, arg0);
7780 call = builtin_save_expr (call);
7782 return build2 (COMPOUND_EXPR, void_type_node,
7783 build2 (MODIFY_EXPR, void_type_node,
7784 build_fold_indirect_ref_loc (loc, arg1),
7785 build1 (IMAGPART_EXPR, type, call)),
7786 build2 (MODIFY_EXPR, void_type_node,
7787 build_fold_indirect_ref_loc (loc, arg2),
7788 build1 (REALPART_EXPR, type, call)));
7791 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7792 NULL_TREE if no simplification can be made. */
7795 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7798 tree realp, imagp, ifn;
7801 if (!validate_arg (arg0, COMPLEX_TYPE)
7802 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7805 /* Calculate the result when the argument is a constant. */
7806 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7809 rtype = TREE_TYPE (TREE_TYPE (arg0));
7811 /* In case we can figure out the real part of arg0 and it is constant zero
7813 if (!TARGET_C99_FUNCTIONS)
7815 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7819 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7820 && real_zerop (realp))
7822 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7823 return build_call_expr_loc (loc, ifn, 1, narg);
7826 /* In case we can easily decompose real and imaginary parts split cexp
7827 to exp (r) * cexpi (i). */
7828 if (flag_unsafe_math_optimizations
7831 tree rfn, rcall, icall;
7833 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7837 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7841 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7842 icall = builtin_save_expr (icall);
7843 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7844 rcall = builtin_save_expr (rcall);
7845 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7846 fold_build2_loc (loc, MULT_EXPR, rtype,
7848 fold_build1_loc (loc, REALPART_EXPR,
7850 fold_build2_loc (loc, MULT_EXPR, rtype,
7852 fold_build1_loc (loc, IMAGPART_EXPR,
7859 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7860 Return NULL_TREE if no simplification can be made. */
7863 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7865 if (!validate_arg (arg, REAL_TYPE))
7868 /* Optimize trunc of constant value. */
7869 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7871 REAL_VALUE_TYPE r, x;
7872 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7874 x = TREE_REAL_CST (arg);
7875 real_trunc (&r, TYPE_MODE (type), &x);
7876 return build_real (type, r);
7879 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7882 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7883 Return NULL_TREE if no simplification can be made. */
7886 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7888 if (!validate_arg (arg, REAL_TYPE))
7891 /* Optimize floor of constant value. */
7892 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7896 x = TREE_REAL_CST (arg);
7897 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7899 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7902 real_floor (&r, TYPE_MODE (type), &x);
7903 return build_real (type, r);
7907 /* Fold floor (x) where x is nonnegative to trunc (x). */
7908 if (tree_expr_nonnegative_p (arg))
7910 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7912 return build_call_expr_loc (loc, truncfn, 1, arg);
7915 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7918 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7919 Return NULL_TREE if no simplification can be made. */
7922 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7924 if (!validate_arg (arg, REAL_TYPE))
7927 /* Optimize ceil of constant value. */
7928 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7932 x = TREE_REAL_CST (arg);
7933 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7935 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7938 real_ceil (&r, TYPE_MODE (type), &x);
7939 return build_real (type, r);
7943 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7946 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7947 Return NULL_TREE if no simplification can be made. */
7950 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7952 if (!validate_arg (arg, REAL_TYPE))
7955 /* Optimize round of constant value. */
7956 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7960 x = TREE_REAL_CST (arg);
7961 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7963 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7966 real_round (&r, TYPE_MODE (type), &x);
7967 return build_real (type, r);
7971 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7974 /* Fold function call to builtin lround, lroundf or lroundl (or the
7975 corresponding long long versions) and other rounding functions. ARG
7976 is the argument to the call. Return NULL_TREE if no simplification
7980 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7982 if (!validate_arg (arg, REAL_TYPE))
7985 /* Optimize lround of constant value. */
7986 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7988 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7990 if (real_isfinite (&x))
7992 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7993 tree ftype = TREE_TYPE (arg);
7997 switch (DECL_FUNCTION_CODE (fndecl))
7999 CASE_FLT_FN (BUILT_IN_IFLOOR):
8000 CASE_FLT_FN (BUILT_IN_LFLOOR):
8001 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8002 real_floor (&r, TYPE_MODE (ftype), &x);
8005 CASE_FLT_FN (BUILT_IN_ICEIL):
8006 CASE_FLT_FN (BUILT_IN_LCEIL):
8007 CASE_FLT_FN (BUILT_IN_LLCEIL):
8008 real_ceil (&r, TYPE_MODE (ftype), &x);
8011 CASE_FLT_FN (BUILT_IN_IROUND):
8012 CASE_FLT_FN (BUILT_IN_LROUND):
8013 CASE_FLT_FN (BUILT_IN_LLROUND):
8014 real_round (&r, TYPE_MODE (ftype), &x);
8021 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
8022 if (double_int_fits_to_tree_p (itype, val))
8023 return double_int_to_tree (itype, val);
8027 switch (DECL_FUNCTION_CODE (fndecl))
8029 CASE_FLT_FN (BUILT_IN_LFLOOR):
8030 CASE_FLT_FN (BUILT_IN_LLFLOOR):
8031 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8032 if (tree_expr_nonnegative_p (arg))
8033 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
8034 TREE_TYPE (TREE_TYPE (fndecl)), arg);
8039 return fold_fixed_mathfn (loc, fndecl, arg);
8042 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8043 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8044 the argument to the call. Return NULL_TREE if no simplification can
8048 fold_builtin_bitop (tree fndecl, tree arg)
8050 if (!validate_arg (arg, INTEGER_TYPE))
8053 /* Optimize for constant argument. */
8054 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8056 HOST_WIDE_INT hi, width, result;
8057 unsigned HOST_WIDE_INT lo;
8060 type = TREE_TYPE (arg);
8061 width = TYPE_PRECISION (type);
8062 lo = TREE_INT_CST_LOW (arg);
8064 /* Clear all the bits that are beyond the type's precision. */
8065 if (width > HOST_BITS_PER_WIDE_INT)
8067 hi = TREE_INT_CST_HIGH (arg);
8068 if (width < 2 * HOST_BITS_PER_WIDE_INT)
8069 hi &= ~((unsigned HOST_WIDE_INT) (-1)
8070 << (width - HOST_BITS_PER_WIDE_INT));
8075 if (width < HOST_BITS_PER_WIDE_INT)
8076 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
8079 switch (DECL_FUNCTION_CODE (fndecl))
8081 CASE_INT_FN (BUILT_IN_FFS):
8083 result = ffs_hwi (lo);
8085 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
8090 CASE_INT_FN (BUILT_IN_CLZ):
8092 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
8094 result = width - floor_log2 (lo) - 1;
8095 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8099 CASE_INT_FN (BUILT_IN_CTZ):
8101 result = ctz_hwi (lo);
8103 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
8104 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
8108 CASE_INT_FN (BUILT_IN_CLRSB):
8109 if (width > HOST_BITS_PER_WIDE_INT
8110 && (hi & ((unsigned HOST_WIDE_INT) 1
8111 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
8113 hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
8114 << (width - HOST_BITS_PER_WIDE_INT - 1));
8117 else if (width <= HOST_BITS_PER_WIDE_INT
8118 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
8119 lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
8121 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
8123 result = width - floor_log2 (lo) - 2;
8128 CASE_INT_FN (BUILT_IN_POPCOUNT):
8131 result++, lo &= lo - 1;
8133 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8136 CASE_INT_FN (BUILT_IN_PARITY):
8139 result++, lo &= lo - 1;
8141 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
8149 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
8155 /* Fold function call to builtin_bswap and the long and long long
8156 variants. Return NULL_TREE if no simplification can be made. */
8158 fold_builtin_bswap (tree fndecl, tree arg)
8160 if (! validate_arg (arg, INTEGER_TYPE))
8163 /* Optimize constant value. */
8164 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
8166 HOST_WIDE_INT hi, width, r_hi = 0;
8167 unsigned HOST_WIDE_INT lo, r_lo = 0;
8170 type = TREE_TYPE (arg);
8171 width = TYPE_PRECISION (type);
8172 lo = TREE_INT_CST_LOW (arg);
8173 hi = TREE_INT_CST_HIGH (arg);
8175 switch (DECL_FUNCTION_CODE (fndecl))
8177 case BUILT_IN_BSWAP32:
8178 case BUILT_IN_BSWAP64:
8182 for (s = 0; s < width; s += 8)
8184 int d = width - s - 8;
8185 unsigned HOST_WIDE_INT byte;
8187 if (s < HOST_BITS_PER_WIDE_INT)
8188 byte = (lo >> s) & 0xff;
8190 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
8192 if (d < HOST_BITS_PER_WIDE_INT)
8195 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
8205 if (width < HOST_BITS_PER_WIDE_INT)
8206 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
8208 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
8214 /* A subroutine of fold_builtin to fold the various logarithmic
8215 functions. Return NULL_TREE if no simplification can me made.
8216 FUNC is the corresponding MPFR logarithm function. */
8219 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
8220 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8222 if (validate_arg (arg, REAL_TYPE))
8224 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8226 const enum built_in_function fcode = builtin_mathfn_code (arg);
8228 /* Calculate the result when the argument is a constant. */
8229 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
8232 /* Special case, optimize logN(expN(x)) = x. */
8233 if (flag_unsafe_math_optimizations
8234 && ((func == mpfr_log
8235 && (fcode == BUILT_IN_EXP
8236 || fcode == BUILT_IN_EXPF
8237 || fcode == BUILT_IN_EXPL))
8238 || (func == mpfr_log2
8239 && (fcode == BUILT_IN_EXP2
8240 || fcode == BUILT_IN_EXP2F
8241 || fcode == BUILT_IN_EXP2L))
8242 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
8243 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8245 /* Optimize logN(func()) for various exponential functions. We
8246 want to determine the value "x" and the power "exponent" in
8247 order to transform logN(x**exponent) into exponent*logN(x). */
8248 if (flag_unsafe_math_optimizations)
8250 tree exponent = 0, x = 0;
8254 CASE_FLT_FN (BUILT_IN_EXP):
8255 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8256 x = build_real (type, real_value_truncate (TYPE_MODE (type),
8258 exponent = CALL_EXPR_ARG (arg, 0);
8260 CASE_FLT_FN (BUILT_IN_EXP2):
8261 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8262 x = build_real (type, dconst2);
8263 exponent = CALL_EXPR_ARG (arg, 0);
8265 CASE_FLT_FN (BUILT_IN_EXP10):
8266 CASE_FLT_FN (BUILT_IN_POW10):
8267 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8269 REAL_VALUE_TYPE dconst10;
8270 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
8271 x = build_real (type, dconst10);
8273 exponent = CALL_EXPR_ARG (arg, 0);
8275 CASE_FLT_FN (BUILT_IN_SQRT):
8276 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8277 x = CALL_EXPR_ARG (arg, 0);
8278 exponent = build_real (type, dconsthalf);
8280 CASE_FLT_FN (BUILT_IN_CBRT):
8281 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8282 x = CALL_EXPR_ARG (arg, 0);
8283 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
8286 CASE_FLT_FN (BUILT_IN_POW):
8287 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8288 x = CALL_EXPR_ARG (arg, 0);
8289 exponent = CALL_EXPR_ARG (arg, 1);
8295 /* Now perform the optimization. */
8298 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
8299 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
8307 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8308 NULL_TREE if no simplification can be made. */
8311 fold_builtin_hypot (location_t loc, tree fndecl,
8312 tree arg0, tree arg1, tree type)
8314 tree res, narg0, narg1;
8316 if (!validate_arg (arg0, REAL_TYPE)
8317 || !validate_arg (arg1, REAL_TYPE))
8320 /* Calculate the result when the argument is a constant. */
8321 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
8324 /* If either argument to hypot has a negate or abs, strip that off.
8325 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8326 narg0 = fold_strip_sign_ops (arg0);
8327 narg1 = fold_strip_sign_ops (arg1);
8330 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
8331 narg1 ? narg1 : arg1);
8334 /* If either argument is zero, hypot is fabs of the other. */
8335 if (real_zerop (arg0))
8336 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
8337 else if (real_zerop (arg1))
8338 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
8340 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8341 if (flag_unsafe_math_optimizations
8342 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8344 const REAL_VALUE_TYPE sqrt2_trunc
8345 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
8346 return fold_build2_loc (loc, MULT_EXPR, type,
8347 fold_build1_loc (loc, ABS_EXPR, type, arg0),
8348 build_real (type, sqrt2_trunc));
8355 /* Fold a builtin function call to pow, powf, or powl. Return
8356 NULL_TREE if no simplification can be made. */
8358 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
8362 if (!validate_arg (arg0, REAL_TYPE)
8363 || !validate_arg (arg1, REAL_TYPE))
8366 /* Calculate the result when the argument is a constant. */
8367 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
8370 /* Optimize pow(1.0,y) = 1.0. */
8371 if (real_onep (arg0))
8372 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8374 if (TREE_CODE (arg1) == REAL_CST
8375 && !TREE_OVERFLOW (arg1))
8377 REAL_VALUE_TYPE cint;
8381 c = TREE_REAL_CST (arg1);
8383 /* Optimize pow(x,0.0) = 1.0. */
8384 if (REAL_VALUES_EQUAL (c, dconst0))
8385 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8388 /* Optimize pow(x,1.0) = x. */
8389 if (REAL_VALUES_EQUAL (c, dconst1))
8392 /* Optimize pow(x,-1.0) = 1.0/x. */
8393 if (REAL_VALUES_EQUAL (c, dconstm1))
8394 return fold_build2_loc (loc, RDIV_EXPR, type,
8395 build_real (type, dconst1), arg0);
8397 /* Optimize pow(x,0.5) = sqrt(x). */
8398 if (flag_unsafe_math_optimizations
8399 && REAL_VALUES_EQUAL (c, dconsthalf))
8401 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
8403 if (sqrtfn != NULL_TREE)
8404 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
8407 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8408 if (flag_unsafe_math_optimizations)
8410 const REAL_VALUE_TYPE dconstroot
8411 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8413 if (REAL_VALUES_EQUAL (c, dconstroot))
8415 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
8416 if (cbrtfn != NULL_TREE)
8417 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
8421 /* Check for an integer exponent. */
8422 n = real_to_integer (&c);
8423 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
8424 if (real_identical (&c, &cint))
8426 /* Attempt to evaluate pow at compile-time, unless this should
8427 raise an exception. */
8428 if (TREE_CODE (arg0) == REAL_CST
8429 && !TREE_OVERFLOW (arg0)
8431 || (!flag_trapping_math && !flag_errno_math)
8432 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
8437 x = TREE_REAL_CST (arg0);
8438 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
8439 if (flag_unsafe_math_optimizations || !inexact)
8440 return build_real (type, x);
8443 /* Strip sign ops from even integer powers. */
8444 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
8446 tree narg0 = fold_strip_sign_ops (arg0);
8448 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
8453 if (flag_unsafe_math_optimizations)
8455 const enum built_in_function fcode = builtin_mathfn_code (arg0);
8457 /* Optimize pow(expN(x),y) = expN(x*y). */
8458 if (BUILTIN_EXPONENT_P (fcode))
8460 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
8461 tree arg = CALL_EXPR_ARG (arg0, 0);
8462 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
8463 return build_call_expr_loc (loc, expfn, 1, arg);
8466 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8467 if (BUILTIN_SQRT_P (fcode))
8469 tree narg0 = CALL_EXPR_ARG (arg0, 0);
8470 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8471 build_real (type, dconsthalf));
8472 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
8475 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8476 if (BUILTIN_CBRT_P (fcode))
8478 tree arg = CALL_EXPR_ARG (arg0, 0);
8479 if (tree_expr_nonnegative_p (arg))
8481 const REAL_VALUE_TYPE dconstroot
8482 = real_value_truncate (TYPE_MODE (type), dconst_third ());
8483 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
8484 build_real (type, dconstroot));
8485 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
8489 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8490 if (fcode == BUILT_IN_POW
8491 || fcode == BUILT_IN_POWF
8492 || fcode == BUILT_IN_POWL)
8494 tree arg00 = CALL_EXPR_ARG (arg0, 0);
8495 if (tree_expr_nonnegative_p (arg00))
8497 tree arg01 = CALL_EXPR_ARG (arg0, 1);
8498 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
8499 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
8507 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8508 Return NULL_TREE if no simplification can be made. */
8510 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
8511 tree arg0, tree arg1, tree type)
8513 if (!validate_arg (arg0, REAL_TYPE)
8514 || !validate_arg (arg1, INTEGER_TYPE))
8517 /* Optimize pow(1.0,y) = 1.0. */
8518 if (real_onep (arg0))
8519 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
8521 if (host_integerp (arg1, 0))
8523 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
8525 /* Evaluate powi at compile-time. */
8526 if (TREE_CODE (arg0) == REAL_CST
8527 && !TREE_OVERFLOW (arg0))
8530 x = TREE_REAL_CST (arg0);
8531 real_powi (&x, TYPE_MODE (type), &x, c);
8532 return build_real (type, x);
8535 /* Optimize pow(x,0) = 1.0. */
8537 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
8540 /* Optimize pow(x,1) = x. */
8544 /* Optimize pow(x,-1) = 1.0/x. */
8546 return fold_build2_loc (loc, RDIV_EXPR, type,
8547 build_real (type, dconst1), arg0);
8553 /* A subroutine of fold_builtin to fold the various exponent
8554 functions. Return NULL_TREE if no simplification can be made.
8555 FUNC is the corresponding MPFR exponent function. */
8558 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
8559 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
8561 if (validate_arg (arg, REAL_TYPE))
8563 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8566 /* Calculate the result when the argument is a constant. */
8567 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
8570 /* Optimize expN(logN(x)) = x. */
8571 if (flag_unsafe_math_optimizations)
8573 const enum built_in_function fcode = builtin_mathfn_code (arg);
8575 if ((func == mpfr_exp
8576 && (fcode == BUILT_IN_LOG
8577 || fcode == BUILT_IN_LOGF
8578 || fcode == BUILT_IN_LOGL))
8579 || (func == mpfr_exp2
8580 && (fcode == BUILT_IN_LOG2
8581 || fcode == BUILT_IN_LOG2F
8582 || fcode == BUILT_IN_LOG2L))
8583 || (func == mpfr_exp10
8584 && (fcode == BUILT_IN_LOG10
8585 || fcode == BUILT_IN_LOG10F
8586 || fcode == BUILT_IN_LOG10L)))
8587 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
8594 /* Return true if VAR is a VAR_DECL or a component thereof. */
8597 var_decl_component_p (tree var)
8600 while (handled_component_p (inner))
8601 inner = TREE_OPERAND (inner, 0);
8602 return SSA_VAR_P (inner);
8605 /* Fold function call to builtin memset. Return
8606 NULL_TREE if no simplification can be made. */
8609 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
8610 tree type, bool ignore)
8612 tree var, ret, etype;
8613 unsigned HOST_WIDE_INT length, cval;
8615 if (! validate_arg (dest, POINTER_TYPE)
8616 || ! validate_arg (c, INTEGER_TYPE)
8617 || ! validate_arg (len, INTEGER_TYPE))
8620 if (! host_integerp (len, 1))
8623 /* If the LEN parameter is zero, return DEST. */
8624 if (integer_zerop (len))
8625 return omit_one_operand_loc (loc, type, dest, c);
8627 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
8632 if (TREE_CODE (var) != ADDR_EXPR)
8635 var = TREE_OPERAND (var, 0);
8636 if (TREE_THIS_VOLATILE (var))
8639 etype = TREE_TYPE (var);
8640 if (TREE_CODE (etype) == ARRAY_TYPE)
8641 etype = TREE_TYPE (etype);
8643 if (!INTEGRAL_TYPE_P (etype)
8644 && !POINTER_TYPE_P (etype))
8647 if (! var_decl_component_p (var))
8650 length = tree_low_cst (len, 1);
8651 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
8652 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
8655 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
8658 if (integer_zerop (c))
8662 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
8665 cval = TREE_INT_CST_LOW (c);
8669 cval |= (cval << 31) << 1;
8672 ret = build_int_cst_type (etype, cval);
8673 var = build_fold_indirect_ref_loc (loc,
8674 fold_convert_loc (loc,
8675 build_pointer_type (etype),
8677 ret = build2 (MODIFY_EXPR, etype, var, ret);
8681 return omit_one_operand_loc (loc, type, dest, ret);
8684 /* Fold function call to builtin memset. Return
8685 NULL_TREE if no simplification can be made. */
8688 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
8690 if (! validate_arg (dest, POINTER_TYPE)
8691 || ! validate_arg (size, INTEGER_TYPE))
8697 /* New argument list transforming bzero(ptr x, int y) to
8698 memset(ptr x, int 0, size_t y). This is done this way
8699 so that if it isn't expanded inline, we fallback to
8700 calling bzero instead of memset. */
8702 return fold_builtin_memset (loc, dest, integer_zero_node,
8703 fold_convert_loc (loc, size_type_node, size),
8704 void_type_node, ignore);
8707 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8708 NULL_TREE if no simplification can be made.
8709 If ENDP is 0, return DEST (like memcpy).
8710 If ENDP is 1, return DEST+LEN (like mempcpy).
8711 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8712 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8716 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8717 tree len, tree type, bool ignore, int endp)
8719 tree destvar, srcvar, expr;
8721 if (! validate_arg (dest, POINTER_TYPE)
8722 || ! validate_arg (src, POINTER_TYPE)
8723 || ! validate_arg (len, INTEGER_TYPE))
8726 /* If the LEN parameter is zero, return DEST. */
8727 if (integer_zerop (len))
8728 return omit_one_operand_loc (loc, type, dest, src);
8730 /* If SRC and DEST are the same (and not volatile), return
8731 DEST{,+LEN,+LEN-1}. */
8732 if (operand_equal_p (src, dest, 0))
8736 tree srctype, desttype;
8737 unsigned int src_align, dest_align;
8742 src_align = get_pointer_alignment (src);
8743 dest_align = get_pointer_alignment (dest);
8745 /* Both DEST and SRC must be pointer types.
8746 ??? This is what old code did. Is the testing for pointer types
8749 If either SRC is readonly or length is 1, we can use memcpy. */
8750 if (!dest_align || !src_align)
8752 if (readonly_data_expr (src)
8753 || (host_integerp (len, 1)
8754 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8755 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8757 tree fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8760 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8763 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8764 if (TREE_CODE (src) == ADDR_EXPR
8765 && TREE_CODE (dest) == ADDR_EXPR)
8767 tree src_base, dest_base, fn;
8768 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8769 HOST_WIDE_INT size = -1;
8770 HOST_WIDE_INT maxsize = -1;
8772 srcvar = TREE_OPERAND (src, 0);
8773 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8775 destvar = TREE_OPERAND (dest, 0);
8776 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8778 if (host_integerp (len, 1))
8779 maxsize = tree_low_cst (len, 1);
8782 src_offset /= BITS_PER_UNIT;
8783 dest_offset /= BITS_PER_UNIT;
8784 if (SSA_VAR_P (src_base)
8785 && SSA_VAR_P (dest_base))
8787 if (operand_equal_p (src_base, dest_base, 0)
8788 && ranges_overlap_p (src_offset, maxsize,
8789 dest_offset, maxsize))
8792 else if (TREE_CODE (src_base) == MEM_REF
8793 && TREE_CODE (dest_base) == MEM_REF)
8796 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8797 TREE_OPERAND (dest_base, 0), 0))
8799 off = double_int_add (mem_ref_offset (src_base),
8800 shwi_to_double_int (src_offset));
8801 if (!double_int_fits_in_shwi_p (off))
8803 src_offset = off.low;
8804 off = double_int_add (mem_ref_offset (dest_base),
8805 shwi_to_double_int (dest_offset));
8806 if (!double_int_fits_in_shwi_p (off))
8808 dest_offset = off.low;
8809 if (ranges_overlap_p (src_offset, maxsize,
8810 dest_offset, maxsize))
8816 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8819 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8822 /* If the destination and source do not alias optimize into
8824 if ((is_gimple_min_invariant (dest)
8825 || TREE_CODE (dest) == SSA_NAME)
8826 && (is_gimple_min_invariant (src)
8827 || TREE_CODE (src) == SSA_NAME))
8830 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8831 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8832 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8835 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
8838 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8845 if (!host_integerp (len, 0))
8848 This logic lose for arguments like (type *)malloc (sizeof (type)),
8849 since we strip the casts of up to VOID return value from malloc.
8850 Perhaps we ought to inherit type from non-VOID argument here? */
8853 if (!POINTER_TYPE_P (TREE_TYPE (src))
8854 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8856 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8857 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8859 tree tem = TREE_OPERAND (src, 0);
8861 if (tem != TREE_OPERAND (src, 0))
8862 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8864 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8866 tree tem = TREE_OPERAND (dest, 0);
8868 if (tem != TREE_OPERAND (dest, 0))
8869 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8871 srctype = TREE_TYPE (TREE_TYPE (src));
8872 if (TREE_CODE (srctype) == ARRAY_TYPE
8873 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8875 srctype = TREE_TYPE (srctype);
8877 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8879 desttype = TREE_TYPE (TREE_TYPE (dest));
8880 if (TREE_CODE (desttype) == ARRAY_TYPE
8881 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8883 desttype = TREE_TYPE (desttype);
8885 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8887 if (TREE_ADDRESSABLE (srctype)
8888 || TREE_ADDRESSABLE (desttype))
8891 src_align = get_pointer_alignment (src);
8892 dest_align = get_pointer_alignment (dest);
8893 if (dest_align < TYPE_ALIGN (desttype)
8894 || src_align < TYPE_ALIGN (srctype))
8898 dest = builtin_save_expr (dest);
8900 /* Build accesses at offset zero with a ref-all character type. */
8901 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8902 ptr_mode, true), 0);
8905 STRIP_NOPS (destvar);
8906 if (TREE_CODE (destvar) == ADDR_EXPR
8907 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8908 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8909 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8911 destvar = NULL_TREE;
8914 STRIP_NOPS (srcvar);
8915 if (TREE_CODE (srcvar) == ADDR_EXPR
8916 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8917 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8920 || src_align >= TYPE_ALIGN (desttype))
8921 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8923 else if (!STRICT_ALIGNMENT)
8925 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8927 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8935 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8938 if (srcvar == NULL_TREE)
8941 if (src_align >= TYPE_ALIGN (desttype))
8942 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8945 if (STRICT_ALIGNMENT)
8947 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8949 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8952 else if (destvar == NULL_TREE)
8955 if (dest_align >= TYPE_ALIGN (srctype))
8956 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8959 if (STRICT_ALIGNMENT)
8961 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8963 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8967 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8973 if (endp == 0 || endp == 3)
8974 return omit_one_operand_loc (loc, type, dest, expr);
8980 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8983 dest = fold_build_pointer_plus_loc (loc, dest, len);
8984 dest = fold_convert_loc (loc, type, dest);
8986 dest = omit_one_operand_loc (loc, type, dest, expr);
8990 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8991 If LEN is not NULL, it represents the length of the string to be
8992 copied. Return NULL_TREE if no simplification can be made. */
8995 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8999 if (!validate_arg (dest, POINTER_TYPE)
9000 || !validate_arg (src, POINTER_TYPE))
9003 /* If SRC and DEST are the same (and not volatile), return DEST. */
9004 if (operand_equal_p (src, dest, 0))
9005 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
9007 if (optimize_function_for_size_p (cfun))
9010 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9016 len = c_strlen (src, 1);
9017 if (! len || TREE_SIDE_EFFECTS (len))
9021 len = fold_convert_loc (loc, size_type_node, len);
9022 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
9023 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9024 build_call_expr_loc (loc, fn, 3, dest, src, len));
9027 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9028 Return NULL_TREE if no simplification can be made. */
9031 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
9033 tree fn, len, lenp1, call, type;
9035 if (!validate_arg (dest, POINTER_TYPE)
9036 || !validate_arg (src, POINTER_TYPE))
9039 len = c_strlen (src, 1);
9041 || TREE_CODE (len) != INTEGER_CST)
9044 if (optimize_function_for_size_p (cfun)
9045 /* If length is zero it's small enough. */
9046 && !integer_zerop (len))
9049 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9053 lenp1 = size_binop_loc (loc, PLUS_EXPR,
9054 fold_convert_loc (loc, size_type_node, len),
9055 build_int_cst (size_type_node, 1));
9056 /* We use dest twice in building our expression. Save it from
9057 multiple expansions. */
9058 dest = builtin_save_expr (dest);
9059 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
9061 type = TREE_TYPE (TREE_TYPE (fndecl));
9062 dest = fold_build_pointer_plus_loc (loc, dest, len);
9063 dest = fold_convert_loc (loc, type, dest);
9064 dest = omit_one_operand_loc (loc, type, dest, call);
9068 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9069 If SLEN is not NULL, it represents the length of the source string.
9070 Return NULL_TREE if no simplification can be made. */
9073 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
9074 tree src, tree len, tree slen)
9078 if (!validate_arg (dest, POINTER_TYPE)
9079 || !validate_arg (src, POINTER_TYPE)
9080 || !validate_arg (len, INTEGER_TYPE))
9083 /* If the LEN parameter is zero, return DEST. */
9084 if (integer_zerop (len))
9085 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
9087 /* We can't compare slen with len as constants below if len is not a
9089 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
9093 slen = c_strlen (src, 1);
9095 /* Now, we must be passed a constant src ptr parameter. */
9096 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
9099 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
9101 /* We do not support simplification of this case, though we do
9102 support it when expanding trees into RTL. */
9103 /* FIXME: generate a call to __builtin_memset. */
9104 if (tree_int_cst_lt (slen, len))
9107 /* OK transform into builtin memcpy. */
9108 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
9112 len = fold_convert_loc (loc, size_type_node, len);
9113 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
9114 build_call_expr_loc (loc, fn, 3, dest, src, len));
9117 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9118 arguments to the call, and TYPE is its return type.
9119 Return NULL_TREE if no simplification can be made. */
9122 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
9124 if (!validate_arg (arg1, POINTER_TYPE)
9125 || !validate_arg (arg2, INTEGER_TYPE)
9126 || !validate_arg (len, INTEGER_TYPE))
9132 if (TREE_CODE (arg2) != INTEGER_CST
9133 || !host_integerp (len, 1))
9136 p1 = c_getstr (arg1);
9137 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
9143 if (target_char_cast (arg2, &c))
9146 r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
9149 return build_int_cst (TREE_TYPE (arg1), 0);
9151 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
9152 return fold_convert_loc (loc, type, tem);
9158 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9159 Return NULL_TREE if no simplification can be made. */
9162 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
9164 const char *p1, *p2;
9166 if (!validate_arg (arg1, POINTER_TYPE)
9167 || !validate_arg (arg2, POINTER_TYPE)
9168 || !validate_arg (len, INTEGER_TYPE))
9171 /* If the LEN parameter is zero, return zero. */
9172 if (integer_zerop (len))
9173 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9176 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9177 if (operand_equal_p (arg1, arg2, 0))
9178 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9180 p1 = c_getstr (arg1);
9181 p2 = c_getstr (arg2);
9183 /* If all arguments are constant, and the value of len is not greater
9184 than the lengths of arg1 and arg2, evaluate at compile-time. */
9185 if (host_integerp (len, 1) && p1 && p2
9186 && compare_tree_int (len, strlen (p1) + 1) <= 0
9187 && compare_tree_int (len, strlen (p2) + 1) <= 0)
9189 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
9192 return integer_one_node;
9194 return integer_minus_one_node;
9196 return integer_zero_node;
9199 /* If len parameter is one, return an expression corresponding to
9200 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9201 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9203 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9204 tree cst_uchar_ptr_node
9205 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9208 = fold_convert_loc (loc, integer_type_node,
9209 build1 (INDIRECT_REF, cst_uchar_node,
9210 fold_convert_loc (loc,
9214 = fold_convert_loc (loc, integer_type_node,
9215 build1 (INDIRECT_REF, cst_uchar_node,
9216 fold_convert_loc (loc,
9219 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9225 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9226 Return NULL_TREE if no simplification can be made. */
9229 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
9231 const char *p1, *p2;
9233 if (!validate_arg (arg1, POINTER_TYPE)
9234 || !validate_arg (arg2, POINTER_TYPE))
9237 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9238 if (operand_equal_p (arg1, arg2, 0))
9239 return integer_zero_node;
9241 p1 = c_getstr (arg1);
9242 p2 = c_getstr (arg2);
9246 const int i = strcmp (p1, p2);
9248 return integer_minus_one_node;
9250 return integer_one_node;
9252 return integer_zero_node;
9255 /* If the second arg is "", return *(const unsigned char*)arg1. */
9256 if (p2 && *p2 == '\0')
9258 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9259 tree cst_uchar_ptr_node
9260 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9262 return fold_convert_loc (loc, integer_type_node,
9263 build1 (INDIRECT_REF, cst_uchar_node,
9264 fold_convert_loc (loc,
9269 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9270 if (p1 && *p1 == '\0')
9272 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9273 tree cst_uchar_ptr_node
9274 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9277 = fold_convert_loc (loc, integer_type_node,
9278 build1 (INDIRECT_REF, cst_uchar_node,
9279 fold_convert_loc (loc,
9282 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9288 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9289 Return NULL_TREE if no simplification can be made. */
9292 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
9294 const char *p1, *p2;
9296 if (!validate_arg (arg1, POINTER_TYPE)
9297 || !validate_arg (arg2, POINTER_TYPE)
9298 || !validate_arg (len, INTEGER_TYPE))
9301 /* If the LEN parameter is zero, return zero. */
9302 if (integer_zerop (len))
9303 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
9306 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9307 if (operand_equal_p (arg1, arg2, 0))
9308 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
9310 p1 = c_getstr (arg1);
9311 p2 = c_getstr (arg2);
9313 if (host_integerp (len, 1) && p1 && p2)
9315 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
9317 return integer_one_node;
9319 return integer_minus_one_node;
9321 return integer_zero_node;
9324 /* If the second arg is "", and the length is greater than zero,
9325 return *(const unsigned char*)arg1. */
9326 if (p2 && *p2 == '\0'
9327 && TREE_CODE (len) == INTEGER_CST
9328 && tree_int_cst_sgn (len) == 1)
9330 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9331 tree cst_uchar_ptr_node
9332 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9334 return fold_convert_loc (loc, integer_type_node,
9335 build1 (INDIRECT_REF, cst_uchar_node,
9336 fold_convert_loc (loc,
9341 /* If the first arg is "", and the length is greater than zero,
9342 return -*(const unsigned char*)arg2. */
9343 if (p1 && *p1 == '\0'
9344 && TREE_CODE (len) == INTEGER_CST
9345 && tree_int_cst_sgn (len) == 1)
9347 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9348 tree cst_uchar_ptr_node
9349 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9351 tree temp = fold_convert_loc (loc, integer_type_node,
9352 build1 (INDIRECT_REF, cst_uchar_node,
9353 fold_convert_loc (loc,
9356 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
9359 /* If len parameter is one, return an expression corresponding to
9360 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9361 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
9363 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
9364 tree cst_uchar_ptr_node
9365 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
9367 tree ind1 = fold_convert_loc (loc, integer_type_node,
9368 build1 (INDIRECT_REF, cst_uchar_node,
9369 fold_convert_loc (loc,
9372 tree ind2 = fold_convert_loc (loc, integer_type_node,
9373 build1 (INDIRECT_REF, cst_uchar_node,
9374 fold_convert_loc (loc,
9377 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
9383 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9384 ARG. Return NULL_TREE if no simplification can be made. */
9387 fold_builtin_signbit (location_t loc, tree arg, tree type)
9389 if (!validate_arg (arg, REAL_TYPE))
9392 /* If ARG is a compile-time constant, determine the result. */
9393 if (TREE_CODE (arg) == REAL_CST
9394 && !TREE_OVERFLOW (arg))
9398 c = TREE_REAL_CST (arg);
9399 return (REAL_VALUE_NEGATIVE (c)
9400 ? build_one_cst (type)
9401 : build_zero_cst (type));
9404 /* If ARG is non-negative, the result is always zero. */
9405 if (tree_expr_nonnegative_p (arg))
9406 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9408 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9409 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
9410 return fold_convert (type,
9411 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
9412 build_real (TREE_TYPE (arg), dconst0)));
9417 /* Fold function call to builtin copysign, copysignf or copysignl with
9418 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9422 fold_builtin_copysign (location_t loc, tree fndecl,
9423 tree arg1, tree arg2, tree type)
9427 if (!validate_arg (arg1, REAL_TYPE)
9428 || !validate_arg (arg2, REAL_TYPE))
9431 /* copysign(X,X) is X. */
9432 if (operand_equal_p (arg1, arg2, 0))
9433 return fold_convert_loc (loc, type, arg1);
9435 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9436 if (TREE_CODE (arg1) == REAL_CST
9437 && TREE_CODE (arg2) == REAL_CST
9438 && !TREE_OVERFLOW (arg1)
9439 && !TREE_OVERFLOW (arg2))
9441 REAL_VALUE_TYPE c1, c2;
9443 c1 = TREE_REAL_CST (arg1);
9444 c2 = TREE_REAL_CST (arg2);
9445 /* c1.sign := c2.sign. */
9446 real_copysign (&c1, &c2);
9447 return build_real (type, c1);
9450 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9451 Remember to evaluate Y for side-effects. */
9452 if (tree_expr_nonnegative_p (arg2))
9453 return omit_one_operand_loc (loc, type,
9454 fold_build1_loc (loc, ABS_EXPR, type, arg1),
9457 /* Strip sign changing operations for the first argument. */
9458 tem = fold_strip_sign_ops (arg1);
9460 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
9465 /* Fold a call to builtin isascii with argument ARG. */
9468 fold_builtin_isascii (location_t loc, tree arg)
9470 if (!validate_arg (arg, INTEGER_TYPE))
9474 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9475 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
9476 build_int_cst (integer_type_node,
9477 ~ (unsigned HOST_WIDE_INT) 0x7f));
9478 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
9479 arg, integer_zero_node);
9483 /* Fold a call to builtin toascii with argument ARG. */
9486 fold_builtin_toascii (location_t loc, tree arg)
9488 if (!validate_arg (arg, INTEGER_TYPE))
9491 /* Transform toascii(c) -> (c & 0x7f). */
9492 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
9493 build_int_cst (integer_type_node, 0x7f));
9496 /* Fold a call to builtin isdigit with argument ARG. */
9499 fold_builtin_isdigit (location_t loc, tree arg)
9501 if (!validate_arg (arg, INTEGER_TYPE))
9505 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9506 /* According to the C standard, isdigit is unaffected by locale.
9507 However, it definitely is affected by the target character set. */
9508 unsigned HOST_WIDE_INT target_digit0
9509 = lang_hooks.to_target_charset ('0');
9511 if (target_digit0 == 0)
9514 arg = fold_convert_loc (loc, unsigned_type_node, arg);
9515 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
9516 build_int_cst (unsigned_type_node, target_digit0));
9517 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
9518 build_int_cst (unsigned_type_node, 9));
9522 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9525 fold_builtin_fabs (location_t loc, tree arg, tree type)
9527 if (!validate_arg (arg, REAL_TYPE))
9530 arg = fold_convert_loc (loc, type, arg);
9531 if (TREE_CODE (arg) == REAL_CST)
9532 return fold_abs_const (arg, type);
9533 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9536 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9539 fold_builtin_abs (location_t loc, tree arg, tree type)
9541 if (!validate_arg (arg, INTEGER_TYPE))
9544 arg = fold_convert_loc (loc, type, arg);
9545 if (TREE_CODE (arg) == INTEGER_CST)
9546 return fold_abs_const (arg, type);
9547 return fold_build1_loc (loc, ABS_EXPR, type, arg);
9550 /* Fold a fma operation with arguments ARG[012]. */
9553 fold_fma (location_t loc ATTRIBUTE_UNUSED,
9554 tree type, tree arg0, tree arg1, tree arg2)
9556 if (TREE_CODE (arg0) == REAL_CST
9557 && TREE_CODE (arg1) == REAL_CST
9558 && TREE_CODE (arg2) == REAL_CST)
9559 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
9564 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9567 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
9569 if (validate_arg (arg0, REAL_TYPE)
9570 && validate_arg(arg1, REAL_TYPE)
9571 && validate_arg(arg2, REAL_TYPE))
9573 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
9577 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9578 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
9579 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
9584 /* Fold a call to builtin fmin or fmax. */
9587 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
9588 tree type, bool max)
9590 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
9592 /* Calculate the result when the argument is a constant. */
9593 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
9598 /* If either argument is NaN, return the other one. Avoid the
9599 transformation if we get (and honor) a signalling NaN. Using
9600 omit_one_operand() ensures we create a non-lvalue. */
9601 if (TREE_CODE (arg0) == REAL_CST
9602 && real_isnan (&TREE_REAL_CST (arg0))
9603 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9604 || ! TREE_REAL_CST (arg0).signalling))
9605 return omit_one_operand_loc (loc, type, arg1, arg0);
9606 if (TREE_CODE (arg1) == REAL_CST
9607 && real_isnan (&TREE_REAL_CST (arg1))
9608 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
9609 || ! TREE_REAL_CST (arg1).signalling))
9610 return omit_one_operand_loc (loc, type, arg0, arg1);
9612 /* Transform fmin/fmax(x,x) -> x. */
9613 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
9614 return omit_one_operand_loc (loc, type, arg0, arg1);
9616 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9617 functions to return the numeric arg if the other one is NaN.
9618 These tree codes don't honor that, so only transform if
9619 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9620 handled, so we don't have to worry about it either. */
9621 if (flag_finite_math_only)
9622 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
9623 fold_convert_loc (loc, type, arg0),
9624 fold_convert_loc (loc, type, arg1));
9629 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9632 fold_builtin_carg (location_t loc, tree arg, tree type)
9634 if (validate_arg (arg, COMPLEX_TYPE)
9635 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
9637 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
9641 tree new_arg = builtin_save_expr (arg);
9642 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
9643 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
9644 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
9651 /* Fold a call to builtin logb/ilogb. */
9654 fold_builtin_logb (location_t loc, tree arg, tree rettype)
9656 if (! validate_arg (arg, REAL_TYPE))
9661 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9663 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9669 /* If arg is Inf or NaN and we're logb, return it. */
9670 if (TREE_CODE (rettype) == REAL_TYPE)
9671 return fold_convert_loc (loc, rettype, arg);
9672 /* Fall through... */
9674 /* Zero may set errno and/or raise an exception for logb, also
9675 for ilogb we don't know FP_ILOGB0. */
9678 /* For normal numbers, proceed iff radix == 2. In GCC,
9679 normalized significands are in the range [0.5, 1.0). We
9680 want the exponent as if they were [1.0, 2.0) so get the
9681 exponent and subtract 1. */
9682 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9683 return fold_convert_loc (loc, rettype,
9684 build_int_cst (integer_type_node,
9685 REAL_EXP (value)-1));
9693 /* Fold a call to builtin significand, if radix == 2. */
9696 fold_builtin_significand (location_t loc, tree arg, tree rettype)
9698 if (! validate_arg (arg, REAL_TYPE))
9703 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
9705 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
9712 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9713 return fold_convert_loc (loc, rettype, arg);
9715 /* For normal numbers, proceed iff radix == 2. */
9716 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9718 REAL_VALUE_TYPE result = *value;
9719 /* In GCC, normalized significands are in the range [0.5,
9720 1.0). We want them to be [1.0, 2.0) so set the
9722 SET_REAL_EXP (&result, 1);
9723 return build_real (rettype, result);
9732 /* Fold a call to builtin frexp, we can assume the base is 2. */
9735 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9737 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9742 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9745 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9747 /* Proceed if a valid pointer type was passed in. */
9748 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9750 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9756 /* For +-0, return (*exp = 0, +-0). */
9757 exp = integer_zero_node;
9762 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9763 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9766 /* Since the frexp function always expects base 2, and in
9767 GCC normalized significands are already in the range
9768 [0.5, 1.0), we have exactly what frexp wants. */
9769 REAL_VALUE_TYPE frac_rvt = *value;
9770 SET_REAL_EXP (&frac_rvt, 0);
9771 frac = build_real (rettype, frac_rvt);
9772 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9779 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9780 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9781 TREE_SIDE_EFFECTS (arg1) = 1;
9782 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9788 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9789 then we can assume the base is two. If it's false, then we have to
9790 check the mode of the TYPE parameter in certain cases. */
9793 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9794 tree type, bool ldexp)
9796 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9801 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9802 if (real_zerop (arg0) || integer_zerop (arg1)
9803 || (TREE_CODE (arg0) == REAL_CST
9804 && !real_isfinite (&TREE_REAL_CST (arg0))))
9805 return omit_one_operand_loc (loc, type, arg0, arg1);
9807 /* If both arguments are constant, then try to evaluate it. */
9808 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9809 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9810 && host_integerp (arg1, 0))
9812 /* Bound the maximum adjustment to twice the range of the
9813 mode's valid exponents. Use abs to ensure the range is
9814 positive as a sanity check. */
9815 const long max_exp_adj = 2 *
9816 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9817 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9819 /* Get the user-requested adjustment. */
9820 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9822 /* The requested adjustment must be inside this range. This
9823 is a preliminary cap to avoid things like overflow, we
9824 may still fail to compute the result for other reasons. */
9825 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9827 REAL_VALUE_TYPE initial_result;
9829 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9831 /* Ensure we didn't overflow. */
9832 if (! real_isinf (&initial_result))
9834 const REAL_VALUE_TYPE trunc_result
9835 = real_value_truncate (TYPE_MODE (type), initial_result);
9837 /* Only proceed if the target mode can hold the
9839 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9840 return build_real (type, trunc_result);
9849 /* Fold a call to builtin modf. */
9852 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9854 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9859 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9862 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9864 /* Proceed if a valid pointer type was passed in. */
9865 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9867 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9868 REAL_VALUE_TYPE trunc, frac;
9874 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9875 trunc = frac = *value;
9878 /* For +-Inf, return (*arg1 = arg0, +-0). */
9880 frac.sign = value->sign;
9884 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9885 real_trunc (&trunc, VOIDmode, value);
9886 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9887 /* If the original number was negative and already
9888 integral, then the fractional part is -0.0. */
9889 if (value->sign && frac.cl == rvc_zero)
9890 frac.sign = value->sign;
9894 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9895 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9896 build_real (rettype, trunc));
9897 TREE_SIDE_EFFECTS (arg1) = 1;
9898 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9899 build_real (rettype, frac));
9905 /* Given a location LOC, an interclass builtin function decl FNDECL
9906 and its single argument ARG, return an folded expression computing
9907 the same, or NULL_TREE if we either couldn't or didn't want to fold
9908 (the latter happen if there's an RTL instruction available). */
9911 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9913 enum machine_mode mode;
9915 if (!validate_arg (arg, REAL_TYPE))
9918 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9921 mode = TYPE_MODE (TREE_TYPE (arg));
9923 /* If there is no optab, try generic code. */
9924 switch (DECL_FUNCTION_CODE (fndecl))
9928 CASE_FLT_FN (BUILT_IN_ISINF):
9930 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9931 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9932 tree const type = TREE_TYPE (arg);
9936 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9937 real_from_string (&r, buf);
9938 result = build_call_expr (isgr_fn, 2,
9939 fold_build1_loc (loc, ABS_EXPR, type, arg),
9940 build_real (type, r));
9943 CASE_FLT_FN (BUILT_IN_FINITE):
9944 case BUILT_IN_ISFINITE:
9946 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9947 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9948 tree const type = TREE_TYPE (arg);
9952 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9953 real_from_string (&r, buf);
9954 result = build_call_expr (isle_fn, 2,
9955 fold_build1_loc (loc, ABS_EXPR, type, arg),
9956 build_real (type, r));
9957 /*result = fold_build2_loc (loc, UNGT_EXPR,
9958 TREE_TYPE (TREE_TYPE (fndecl)),
9959 fold_build1_loc (loc, ABS_EXPR, type, arg),
9960 build_real (type, r));
9961 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9962 TREE_TYPE (TREE_TYPE (fndecl)),
9966 case BUILT_IN_ISNORMAL:
9968 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9969 islessequal(fabs(x),DBL_MAX). */
9970 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9971 tree const isge_fn = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9972 tree const type = TREE_TYPE (arg);
9973 REAL_VALUE_TYPE rmax, rmin;
9976 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9977 real_from_string (&rmax, buf);
9978 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9979 real_from_string (&rmin, buf);
9980 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9981 result = build_call_expr (isle_fn, 2, arg,
9982 build_real (type, rmax));
9983 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9984 build_call_expr (isge_fn, 2, arg,
9985 build_real (type, rmin)));
9995 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9996 ARG is the argument for the call. */
9999 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
10001 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10004 if (!validate_arg (arg, REAL_TYPE))
10007 switch (builtin_index)
10009 case BUILT_IN_ISINF:
10010 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10011 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10013 if (TREE_CODE (arg) == REAL_CST)
10015 r = TREE_REAL_CST (arg);
10016 if (real_isinf (&r))
10017 return real_compare (GT_EXPR, &r, &dconst0)
10018 ? integer_one_node : integer_minus_one_node;
10020 return integer_zero_node;
10025 case BUILT_IN_ISINF_SIGN:
10027 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10028 /* In a boolean context, GCC will fold the inner COND_EXPR to
10029 1. So e.g. "if (isinf_sign(x))" would be folded to just
10030 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10031 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
10032 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
10033 tree tmp = NULL_TREE;
10035 arg = builtin_save_expr (arg);
10037 if (signbit_fn && isinf_fn)
10039 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
10040 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
10042 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10043 signbit_call, integer_zero_node);
10044 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
10045 isinf_call, integer_zero_node);
10047 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
10048 integer_minus_one_node, integer_one_node);
10049 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10051 integer_zero_node);
10057 case BUILT_IN_ISFINITE:
10058 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
10059 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
10060 return omit_one_operand_loc (loc, type, integer_one_node, arg);
10062 if (TREE_CODE (arg) == REAL_CST)
10064 r = TREE_REAL_CST (arg);
10065 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
10070 case BUILT_IN_ISNAN:
10071 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
10072 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
10074 if (TREE_CODE (arg) == REAL_CST)
10076 r = TREE_REAL_CST (arg);
10077 return real_isnan (&r) ? integer_one_node : integer_zero_node;
10080 arg = builtin_save_expr (arg);
10081 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
10084 gcc_unreachable ();
10088 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10089 This builtin will generate code to return the appropriate floating
10090 point classification depending on the value of the floating point
10091 number passed in. The possible return values must be supplied as
10092 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10093 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10094 one floating point argument which is "type generic". */
10097 fold_builtin_fpclassify (location_t loc, tree exp)
10099 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
10100 arg, type, res, tmp;
10101 enum machine_mode mode;
10105 /* Verify the required arguments in the original call. */
10106 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
10107 INTEGER_TYPE, INTEGER_TYPE,
10108 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
10111 fp_nan = CALL_EXPR_ARG (exp, 0);
10112 fp_infinite = CALL_EXPR_ARG (exp, 1);
10113 fp_normal = CALL_EXPR_ARG (exp, 2);
10114 fp_subnormal = CALL_EXPR_ARG (exp, 3);
10115 fp_zero = CALL_EXPR_ARG (exp, 4);
10116 arg = CALL_EXPR_ARG (exp, 5);
10117 type = TREE_TYPE (arg);
10118 mode = TYPE_MODE (type);
10119 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
10121 /* fpclassify(x) ->
10122 isnan(x) ? FP_NAN :
10123 (fabs(x) == Inf ? FP_INFINITE :
10124 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10125 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10127 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10128 build_real (type, dconst0));
10129 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
10130 tmp, fp_zero, fp_subnormal);
10132 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
10133 real_from_string (&r, buf);
10134 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
10135 arg, build_real (type, r));
10136 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
10138 if (HONOR_INFINITIES (mode))
10141 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
10142 build_real (type, r));
10143 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
10147 if (HONOR_NANS (mode))
10149 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
10150 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
10156 /* Fold a call to an unordered comparison function such as
10157 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10158 being called and ARG0 and ARG1 are the arguments for the call.
10159 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10160 the opposite of the desired result. UNORDERED_CODE is used
10161 for modes that can hold NaNs and ORDERED_CODE is used for
10165 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
10166 enum tree_code unordered_code,
10167 enum tree_code ordered_code)
10169 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10170 enum tree_code code;
10172 enum tree_code code0, code1;
10173 tree cmp_type = NULL_TREE;
10175 type0 = TREE_TYPE (arg0);
10176 type1 = TREE_TYPE (arg1);
10178 code0 = TREE_CODE (type0);
10179 code1 = TREE_CODE (type1);
10181 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
10182 /* Choose the wider of two real types. */
10183 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
10185 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10187 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
10190 arg0 = fold_convert_loc (loc, cmp_type, arg0);
10191 arg1 = fold_convert_loc (loc, cmp_type, arg1);
10193 if (unordered_code == UNORDERED_EXPR)
10195 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
10196 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
10197 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
10200 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
10202 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
10203 fold_build2_loc (loc, code, type, arg0, arg1));
10206 /* Fold a call to built-in function FNDECL with 0 arguments.
10207 IGNORE is true if the result of the function call is ignored. This
10208 function returns NULL_TREE if no simplification was possible. */
10211 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
10213 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10214 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10217 CASE_FLT_FN (BUILT_IN_INF):
10218 case BUILT_IN_INFD32:
10219 case BUILT_IN_INFD64:
10220 case BUILT_IN_INFD128:
10221 return fold_builtin_inf (loc, type, true);
10223 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
10224 return fold_builtin_inf (loc, type, false);
10226 case BUILT_IN_CLASSIFY_TYPE:
10227 return fold_builtin_classify_type (NULL_TREE);
10235 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10236 IGNORE is true if the result of the function call is ignored. This
10237 function returns NULL_TREE if no simplification was possible. */
10240 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
10242 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10243 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10246 case BUILT_IN_CONSTANT_P:
10248 tree val = fold_builtin_constant_p (arg0);
10250 /* Gimplification will pull the CALL_EXPR for the builtin out of
10251 an if condition. When not optimizing, we'll not CSE it back.
10252 To avoid link error types of regressions, return false now. */
10253 if (!val && !optimize)
10254 val = integer_zero_node;
10259 case BUILT_IN_CLASSIFY_TYPE:
10260 return fold_builtin_classify_type (arg0);
10262 case BUILT_IN_STRLEN:
10263 return fold_builtin_strlen (loc, type, arg0);
10265 CASE_FLT_FN (BUILT_IN_FABS):
10266 return fold_builtin_fabs (loc, arg0, type);
10269 case BUILT_IN_LABS:
10270 case BUILT_IN_LLABS:
10271 case BUILT_IN_IMAXABS:
10272 return fold_builtin_abs (loc, arg0, type);
10274 CASE_FLT_FN (BUILT_IN_CONJ):
10275 if (validate_arg (arg0, COMPLEX_TYPE)
10276 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10277 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
10280 CASE_FLT_FN (BUILT_IN_CREAL):
10281 if (validate_arg (arg0, COMPLEX_TYPE)
10282 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10283 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
10286 CASE_FLT_FN (BUILT_IN_CIMAG):
10287 if (validate_arg (arg0, COMPLEX_TYPE)
10288 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10289 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
10292 CASE_FLT_FN (BUILT_IN_CCOS):
10293 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
10295 CASE_FLT_FN (BUILT_IN_CCOSH):
10296 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
10298 CASE_FLT_FN (BUILT_IN_CPROJ):
10299 return fold_builtin_cproj(loc, arg0, type);
10301 CASE_FLT_FN (BUILT_IN_CSIN):
10302 if (validate_arg (arg0, COMPLEX_TYPE)
10303 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10304 return do_mpc_arg1 (arg0, type, mpc_sin);
10307 CASE_FLT_FN (BUILT_IN_CSINH):
10308 if (validate_arg (arg0, COMPLEX_TYPE)
10309 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10310 return do_mpc_arg1 (arg0, type, mpc_sinh);
10313 CASE_FLT_FN (BUILT_IN_CTAN):
10314 if (validate_arg (arg0, COMPLEX_TYPE)
10315 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10316 return do_mpc_arg1 (arg0, type, mpc_tan);
10319 CASE_FLT_FN (BUILT_IN_CTANH):
10320 if (validate_arg (arg0, COMPLEX_TYPE)
10321 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10322 return do_mpc_arg1 (arg0, type, mpc_tanh);
10325 CASE_FLT_FN (BUILT_IN_CLOG):
10326 if (validate_arg (arg0, COMPLEX_TYPE)
10327 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10328 return do_mpc_arg1 (arg0, type, mpc_log);
10331 CASE_FLT_FN (BUILT_IN_CSQRT):
10332 if (validate_arg (arg0, COMPLEX_TYPE)
10333 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10334 return do_mpc_arg1 (arg0, type, mpc_sqrt);
10337 CASE_FLT_FN (BUILT_IN_CASIN):
10338 if (validate_arg (arg0, COMPLEX_TYPE)
10339 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10340 return do_mpc_arg1 (arg0, type, mpc_asin);
10343 CASE_FLT_FN (BUILT_IN_CACOS):
10344 if (validate_arg (arg0, COMPLEX_TYPE)
10345 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10346 return do_mpc_arg1 (arg0, type, mpc_acos);
10349 CASE_FLT_FN (BUILT_IN_CATAN):
10350 if (validate_arg (arg0, COMPLEX_TYPE)
10351 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10352 return do_mpc_arg1 (arg0, type, mpc_atan);
10355 CASE_FLT_FN (BUILT_IN_CASINH):
10356 if (validate_arg (arg0, COMPLEX_TYPE)
10357 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10358 return do_mpc_arg1 (arg0, type, mpc_asinh);
10361 CASE_FLT_FN (BUILT_IN_CACOSH):
10362 if (validate_arg (arg0, COMPLEX_TYPE)
10363 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10364 return do_mpc_arg1 (arg0, type, mpc_acosh);
10367 CASE_FLT_FN (BUILT_IN_CATANH):
10368 if (validate_arg (arg0, COMPLEX_TYPE)
10369 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
10370 return do_mpc_arg1 (arg0, type, mpc_atanh);
10373 CASE_FLT_FN (BUILT_IN_CABS):
10374 return fold_builtin_cabs (loc, arg0, type, fndecl);
10376 CASE_FLT_FN (BUILT_IN_CARG):
10377 return fold_builtin_carg (loc, arg0, type);
10379 CASE_FLT_FN (BUILT_IN_SQRT):
10380 return fold_builtin_sqrt (loc, arg0, type);
10382 CASE_FLT_FN (BUILT_IN_CBRT):
10383 return fold_builtin_cbrt (loc, arg0, type);
10385 CASE_FLT_FN (BUILT_IN_ASIN):
10386 if (validate_arg (arg0, REAL_TYPE))
10387 return do_mpfr_arg1 (arg0, type, mpfr_asin,
10388 &dconstm1, &dconst1, true);
10391 CASE_FLT_FN (BUILT_IN_ACOS):
10392 if (validate_arg (arg0, REAL_TYPE))
10393 return do_mpfr_arg1 (arg0, type, mpfr_acos,
10394 &dconstm1, &dconst1, true);
10397 CASE_FLT_FN (BUILT_IN_ATAN):
10398 if (validate_arg (arg0, REAL_TYPE))
10399 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
10402 CASE_FLT_FN (BUILT_IN_ASINH):
10403 if (validate_arg (arg0, REAL_TYPE))
10404 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
10407 CASE_FLT_FN (BUILT_IN_ACOSH):
10408 if (validate_arg (arg0, REAL_TYPE))
10409 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
10410 &dconst1, NULL, true);
10413 CASE_FLT_FN (BUILT_IN_ATANH):
10414 if (validate_arg (arg0, REAL_TYPE))
10415 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
10416 &dconstm1, &dconst1, false);
10419 CASE_FLT_FN (BUILT_IN_SIN):
10420 if (validate_arg (arg0, REAL_TYPE))
10421 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
10424 CASE_FLT_FN (BUILT_IN_COS):
10425 return fold_builtin_cos (loc, arg0, type, fndecl);
10427 CASE_FLT_FN (BUILT_IN_TAN):
10428 return fold_builtin_tan (arg0, type);
10430 CASE_FLT_FN (BUILT_IN_CEXP):
10431 return fold_builtin_cexp (loc, arg0, type);
10433 CASE_FLT_FN (BUILT_IN_CEXPI):
10434 if (validate_arg (arg0, REAL_TYPE))
10435 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
10438 CASE_FLT_FN (BUILT_IN_SINH):
10439 if (validate_arg (arg0, REAL_TYPE))
10440 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
10443 CASE_FLT_FN (BUILT_IN_COSH):
10444 return fold_builtin_cosh (loc, arg0, type, fndecl);
10446 CASE_FLT_FN (BUILT_IN_TANH):
10447 if (validate_arg (arg0, REAL_TYPE))
10448 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
10451 CASE_FLT_FN (BUILT_IN_ERF):
10452 if (validate_arg (arg0, REAL_TYPE))
10453 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
10456 CASE_FLT_FN (BUILT_IN_ERFC):
10457 if (validate_arg (arg0, REAL_TYPE))
10458 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
10461 CASE_FLT_FN (BUILT_IN_TGAMMA):
10462 if (validate_arg (arg0, REAL_TYPE))
10463 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
10466 CASE_FLT_FN (BUILT_IN_EXP):
10467 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
10469 CASE_FLT_FN (BUILT_IN_EXP2):
10470 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
10472 CASE_FLT_FN (BUILT_IN_EXP10):
10473 CASE_FLT_FN (BUILT_IN_POW10):
10474 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
10476 CASE_FLT_FN (BUILT_IN_EXPM1):
10477 if (validate_arg (arg0, REAL_TYPE))
10478 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
10481 CASE_FLT_FN (BUILT_IN_LOG):
10482 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
10484 CASE_FLT_FN (BUILT_IN_LOG2):
10485 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
10487 CASE_FLT_FN (BUILT_IN_LOG10):
10488 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
10490 CASE_FLT_FN (BUILT_IN_LOG1P):
10491 if (validate_arg (arg0, REAL_TYPE))
10492 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
10493 &dconstm1, NULL, false);
10496 CASE_FLT_FN (BUILT_IN_J0):
10497 if (validate_arg (arg0, REAL_TYPE))
10498 return do_mpfr_arg1 (arg0, type, mpfr_j0,
10502 CASE_FLT_FN (BUILT_IN_J1):
10503 if (validate_arg (arg0, REAL_TYPE))
10504 return do_mpfr_arg1 (arg0, type, mpfr_j1,
10508 CASE_FLT_FN (BUILT_IN_Y0):
10509 if (validate_arg (arg0, REAL_TYPE))
10510 return do_mpfr_arg1 (arg0, type, mpfr_y0,
10511 &dconst0, NULL, false);
10514 CASE_FLT_FN (BUILT_IN_Y1):
10515 if (validate_arg (arg0, REAL_TYPE))
10516 return do_mpfr_arg1 (arg0, type, mpfr_y1,
10517 &dconst0, NULL, false);
10520 CASE_FLT_FN (BUILT_IN_NAN):
10521 case BUILT_IN_NAND32:
10522 case BUILT_IN_NAND64:
10523 case BUILT_IN_NAND128:
10524 return fold_builtin_nan (arg0, type, true);
10526 CASE_FLT_FN (BUILT_IN_NANS):
10527 return fold_builtin_nan (arg0, type, false);
10529 CASE_FLT_FN (BUILT_IN_FLOOR):
10530 return fold_builtin_floor (loc, fndecl, arg0);
10532 CASE_FLT_FN (BUILT_IN_CEIL):
10533 return fold_builtin_ceil (loc, fndecl, arg0);
10535 CASE_FLT_FN (BUILT_IN_TRUNC):
10536 return fold_builtin_trunc (loc, fndecl, arg0);
10538 CASE_FLT_FN (BUILT_IN_ROUND):
10539 return fold_builtin_round (loc, fndecl, arg0);
10541 CASE_FLT_FN (BUILT_IN_NEARBYINT):
10542 CASE_FLT_FN (BUILT_IN_RINT):
10543 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
10545 CASE_FLT_FN (BUILT_IN_ICEIL):
10546 CASE_FLT_FN (BUILT_IN_LCEIL):
10547 CASE_FLT_FN (BUILT_IN_LLCEIL):
10548 CASE_FLT_FN (BUILT_IN_LFLOOR):
10549 CASE_FLT_FN (BUILT_IN_IFLOOR):
10550 CASE_FLT_FN (BUILT_IN_LLFLOOR):
10551 CASE_FLT_FN (BUILT_IN_IROUND):
10552 CASE_FLT_FN (BUILT_IN_LROUND):
10553 CASE_FLT_FN (BUILT_IN_LLROUND):
10554 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
10556 CASE_FLT_FN (BUILT_IN_IRINT):
10557 CASE_FLT_FN (BUILT_IN_LRINT):
10558 CASE_FLT_FN (BUILT_IN_LLRINT):
10559 return fold_fixed_mathfn (loc, fndecl, arg0);
10561 case BUILT_IN_BSWAP32:
10562 case BUILT_IN_BSWAP64:
10563 return fold_builtin_bswap (fndecl, arg0);
10565 CASE_INT_FN (BUILT_IN_FFS):
10566 CASE_INT_FN (BUILT_IN_CLZ):
10567 CASE_INT_FN (BUILT_IN_CTZ):
10568 CASE_INT_FN (BUILT_IN_CLRSB):
10569 CASE_INT_FN (BUILT_IN_POPCOUNT):
10570 CASE_INT_FN (BUILT_IN_PARITY):
10571 return fold_builtin_bitop (fndecl, arg0);
10573 CASE_FLT_FN (BUILT_IN_SIGNBIT):
10574 return fold_builtin_signbit (loc, arg0, type);
10576 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
10577 return fold_builtin_significand (loc, arg0, type);
10579 CASE_FLT_FN (BUILT_IN_ILOGB):
10580 CASE_FLT_FN (BUILT_IN_LOGB):
10581 return fold_builtin_logb (loc, arg0, type);
10583 case BUILT_IN_ISASCII:
10584 return fold_builtin_isascii (loc, arg0);
10586 case BUILT_IN_TOASCII:
10587 return fold_builtin_toascii (loc, arg0);
10589 case BUILT_IN_ISDIGIT:
10590 return fold_builtin_isdigit (loc, arg0);
10592 CASE_FLT_FN (BUILT_IN_FINITE):
10593 case BUILT_IN_FINITED32:
10594 case BUILT_IN_FINITED64:
10595 case BUILT_IN_FINITED128:
10596 case BUILT_IN_ISFINITE:
10598 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
10601 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10604 CASE_FLT_FN (BUILT_IN_ISINF):
10605 case BUILT_IN_ISINFD32:
10606 case BUILT_IN_ISINFD64:
10607 case BUILT_IN_ISINFD128:
10609 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
10612 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10615 case BUILT_IN_ISNORMAL:
10616 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
10618 case BUILT_IN_ISINF_SIGN:
10619 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
10621 CASE_FLT_FN (BUILT_IN_ISNAN):
10622 case BUILT_IN_ISNAND32:
10623 case BUILT_IN_ISNAND64:
10624 case BUILT_IN_ISNAND128:
10625 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
10627 case BUILT_IN_PRINTF:
10628 case BUILT_IN_PRINTF_UNLOCKED:
10629 case BUILT_IN_VPRINTF:
10630 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
10632 case BUILT_IN_FREE:
10633 if (integer_zerop (arg0))
10634 return build_empty_stmt (loc);
10645 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10646 IGNORE is true if the result of the function call is ignored. This
10647 function returns NULL_TREE if no simplification was possible. */
10650 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
10652 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10653 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10657 CASE_FLT_FN (BUILT_IN_JN):
10658 if (validate_arg (arg0, INTEGER_TYPE)
10659 && validate_arg (arg1, REAL_TYPE))
10660 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
10663 CASE_FLT_FN (BUILT_IN_YN):
10664 if (validate_arg (arg0, INTEGER_TYPE)
10665 && validate_arg (arg1, REAL_TYPE))
10666 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
10670 CASE_FLT_FN (BUILT_IN_DREM):
10671 CASE_FLT_FN (BUILT_IN_REMAINDER):
10672 if (validate_arg (arg0, REAL_TYPE)
10673 && validate_arg(arg1, REAL_TYPE))
10674 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
10677 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
10678 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
10679 if (validate_arg (arg0, REAL_TYPE)
10680 && validate_arg(arg1, POINTER_TYPE))
10681 return do_mpfr_lgamma_r (arg0, arg1, type);
10684 CASE_FLT_FN (BUILT_IN_ATAN2):
10685 if (validate_arg (arg0, REAL_TYPE)
10686 && validate_arg(arg1, REAL_TYPE))
10687 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
10690 CASE_FLT_FN (BUILT_IN_FDIM):
10691 if (validate_arg (arg0, REAL_TYPE)
10692 && validate_arg(arg1, REAL_TYPE))
10693 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
10696 CASE_FLT_FN (BUILT_IN_HYPOT):
10697 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
10699 CASE_FLT_FN (BUILT_IN_CPOW):
10700 if (validate_arg (arg0, COMPLEX_TYPE)
10701 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10702 && validate_arg (arg1, COMPLEX_TYPE)
10703 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
10704 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
10707 CASE_FLT_FN (BUILT_IN_LDEXP):
10708 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
10709 CASE_FLT_FN (BUILT_IN_SCALBN):
10710 CASE_FLT_FN (BUILT_IN_SCALBLN):
10711 return fold_builtin_load_exponent (loc, arg0, arg1,
10712 type, /*ldexp=*/false);
10714 CASE_FLT_FN (BUILT_IN_FREXP):
10715 return fold_builtin_frexp (loc, arg0, arg1, type);
10717 CASE_FLT_FN (BUILT_IN_MODF):
10718 return fold_builtin_modf (loc, arg0, arg1, type);
10720 case BUILT_IN_BZERO:
10721 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10723 case BUILT_IN_FPUTS:
10724 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10726 case BUILT_IN_FPUTS_UNLOCKED:
10727 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10729 case BUILT_IN_STRSTR:
10730 return fold_builtin_strstr (loc, arg0, arg1, type);
10732 case BUILT_IN_STRCAT:
10733 return fold_builtin_strcat (loc, arg0, arg1);
10735 case BUILT_IN_STRSPN:
10736 return fold_builtin_strspn (loc, arg0, arg1);
10738 case BUILT_IN_STRCSPN:
10739 return fold_builtin_strcspn (loc, arg0, arg1);
10741 case BUILT_IN_STRCHR:
10742 case BUILT_IN_INDEX:
10743 return fold_builtin_strchr (loc, arg0, arg1, type);
10745 case BUILT_IN_STRRCHR:
10746 case BUILT_IN_RINDEX:
10747 return fold_builtin_strrchr (loc, arg0, arg1, type);
10749 case BUILT_IN_STRCPY:
10750 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10752 case BUILT_IN_STPCPY:
10755 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
10759 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10762 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10765 case BUILT_IN_STRCMP:
10766 return fold_builtin_strcmp (loc, arg0, arg1);
10768 case BUILT_IN_STRPBRK:
10769 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10771 case BUILT_IN_EXPECT:
10772 return fold_builtin_expect (loc, arg0, arg1);
10774 CASE_FLT_FN (BUILT_IN_POW):
10775 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10777 CASE_FLT_FN (BUILT_IN_POWI):
10778 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10780 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10781 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10783 CASE_FLT_FN (BUILT_IN_FMIN):
10784 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10786 CASE_FLT_FN (BUILT_IN_FMAX):
10787 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10789 case BUILT_IN_ISGREATER:
10790 return fold_builtin_unordered_cmp (loc, fndecl,
10791 arg0, arg1, UNLE_EXPR, LE_EXPR);
10792 case BUILT_IN_ISGREATEREQUAL:
10793 return fold_builtin_unordered_cmp (loc, fndecl,
10794 arg0, arg1, UNLT_EXPR, LT_EXPR);
10795 case BUILT_IN_ISLESS:
10796 return fold_builtin_unordered_cmp (loc, fndecl,
10797 arg0, arg1, UNGE_EXPR, GE_EXPR);
10798 case BUILT_IN_ISLESSEQUAL:
10799 return fold_builtin_unordered_cmp (loc, fndecl,
10800 arg0, arg1, UNGT_EXPR, GT_EXPR);
10801 case BUILT_IN_ISLESSGREATER:
10802 return fold_builtin_unordered_cmp (loc, fndecl,
10803 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10804 case BUILT_IN_ISUNORDERED:
10805 return fold_builtin_unordered_cmp (loc, fndecl,
10806 arg0, arg1, UNORDERED_EXPR,
10809 /* We do the folding for va_start in the expander. */
10810 case BUILT_IN_VA_START:
10813 case BUILT_IN_SPRINTF:
10814 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10816 case BUILT_IN_OBJECT_SIZE:
10817 return fold_builtin_object_size (arg0, arg1);
10819 case BUILT_IN_PRINTF:
10820 case BUILT_IN_PRINTF_UNLOCKED:
10821 case BUILT_IN_VPRINTF:
10822 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10824 case BUILT_IN_PRINTF_CHK:
10825 case BUILT_IN_VPRINTF_CHK:
10826 if (!validate_arg (arg0, INTEGER_TYPE)
10827 || TREE_SIDE_EFFECTS (arg0))
10830 return fold_builtin_printf (loc, fndecl,
10831 arg1, NULL_TREE, ignore, fcode);
10834 case BUILT_IN_FPRINTF:
10835 case BUILT_IN_FPRINTF_UNLOCKED:
10836 case BUILT_IN_VFPRINTF:
10837 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10840 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
10841 return fold_builtin_atomic_always_lock_free (arg0, arg1);
10843 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
10844 return fold_builtin_atomic_is_lock_free (arg0, arg1);
10852 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10853 and ARG2. IGNORE is true if the result of the function call is ignored.
10854 This function returns NULL_TREE if no simplification was possible. */
10857 fold_builtin_3 (location_t loc, tree fndecl,
10858 tree arg0, tree arg1, tree arg2, bool ignore)
10860 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10861 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10865 CASE_FLT_FN (BUILT_IN_SINCOS):
10866 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10868 CASE_FLT_FN (BUILT_IN_FMA):
10869 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10872 CASE_FLT_FN (BUILT_IN_REMQUO):
10873 if (validate_arg (arg0, REAL_TYPE)
10874 && validate_arg(arg1, REAL_TYPE)
10875 && validate_arg(arg2, POINTER_TYPE))
10876 return do_mpfr_remquo (arg0, arg1, arg2);
10879 case BUILT_IN_MEMSET:
10880 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10882 case BUILT_IN_BCOPY:
10883 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10884 void_type_node, true, /*endp=*/3);
10886 case BUILT_IN_MEMCPY:
10887 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10888 type, ignore, /*endp=*/0);
10890 case BUILT_IN_MEMPCPY:
10891 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10892 type, ignore, /*endp=*/1);
10894 case BUILT_IN_MEMMOVE:
10895 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10896 type, ignore, /*endp=*/3);
10898 case BUILT_IN_STRNCAT:
10899 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10901 case BUILT_IN_STRNCPY:
10902 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10904 case BUILT_IN_STRNCMP:
10905 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10907 case BUILT_IN_MEMCHR:
10908 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10910 case BUILT_IN_BCMP:
10911 case BUILT_IN_MEMCMP:
10912 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10914 case BUILT_IN_SPRINTF:
10915 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10917 case BUILT_IN_SNPRINTF:
10918 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10920 case BUILT_IN_STRCPY_CHK:
10921 case BUILT_IN_STPCPY_CHK:
10922 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10925 case BUILT_IN_STRCAT_CHK:
10926 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10928 case BUILT_IN_PRINTF_CHK:
10929 case BUILT_IN_VPRINTF_CHK:
10930 if (!validate_arg (arg0, INTEGER_TYPE)
10931 || TREE_SIDE_EFFECTS (arg0))
10934 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10937 case BUILT_IN_FPRINTF:
10938 case BUILT_IN_FPRINTF_UNLOCKED:
10939 case BUILT_IN_VFPRINTF:
10940 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10943 case BUILT_IN_FPRINTF_CHK:
10944 case BUILT_IN_VFPRINTF_CHK:
10945 if (!validate_arg (arg1, INTEGER_TYPE)
10946 || TREE_SIDE_EFFECTS (arg1))
10949 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10958 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10959 ARG2, and ARG3. IGNORE is true if the result of the function call is
10960 ignored. This function returns NULL_TREE if no simplification was
10964 fold_builtin_4 (location_t loc, tree fndecl,
10965 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10967 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10971 case BUILT_IN_MEMCPY_CHK:
10972 case BUILT_IN_MEMPCPY_CHK:
10973 case BUILT_IN_MEMMOVE_CHK:
10974 case BUILT_IN_MEMSET_CHK:
10975 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10977 DECL_FUNCTION_CODE (fndecl));
10979 case BUILT_IN_STRNCPY_CHK:
10980 return fold_builtin_strncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE);
10982 case BUILT_IN_STRNCAT_CHK:
10983 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10985 case BUILT_IN_SNPRINTF:
10986 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10988 case BUILT_IN_FPRINTF_CHK:
10989 case BUILT_IN_VFPRINTF_CHK:
10990 if (!validate_arg (arg1, INTEGER_TYPE)
10991 || TREE_SIDE_EFFECTS (arg1))
10994 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
11004 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11005 arguments, where NARGS <= 4. IGNORE is true if the result of the
11006 function call is ignored. This function returns NULL_TREE if no
11007 simplification was possible. Note that this only folds builtins with
11008 fixed argument patterns. Foldings that do varargs-to-varargs
11009 transformations, or that match calls with more than 4 arguments,
11010 need to be handled with fold_builtin_varargs instead. */
11012 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11015 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
11017 tree ret = NULL_TREE;
11022 ret = fold_builtin_0 (loc, fndecl, ignore);
11025 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
11028 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
11031 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
11034 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
11042 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11043 SET_EXPR_LOCATION (ret, loc);
11044 TREE_NO_WARNING (ret) = 1;
11050 /* Builtins with folding operations that operate on "..." arguments
11051 need special handling; we need to store the arguments in a convenient
11052 data structure before attempting any folding. Fortunately there are
11053 only a few builtins that fall into this category. FNDECL is the
11054 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11055 result of the function call is ignored. */
11058 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
11059 bool ignore ATTRIBUTE_UNUSED)
11061 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
11062 tree ret = NULL_TREE;
11066 case BUILT_IN_SPRINTF_CHK:
11067 case BUILT_IN_VSPRINTF_CHK:
11068 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
11071 case BUILT_IN_SNPRINTF_CHK:
11072 case BUILT_IN_VSNPRINTF_CHK:
11073 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
11076 case BUILT_IN_FPCLASSIFY:
11077 ret = fold_builtin_fpclassify (loc, exp);
11085 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
11086 SET_EXPR_LOCATION (ret, loc);
11087 TREE_NO_WARNING (ret) = 1;
11093 /* Return true if FNDECL shouldn't be folded right now.
11094 If a built-in function has an inline attribute always_inline
11095 wrapper, defer folding it after always_inline functions have
11096 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11097 might not be performed. */
11100 avoid_folding_inline_builtin (tree fndecl)
11102 return (DECL_DECLARED_INLINE_P (fndecl)
11103 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
11105 && !cfun->always_inline_functions_inlined
11106 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
11109 /* A wrapper function for builtin folding that prevents warnings for
11110 "statement without effect" and the like, caused by removing the
11111 call node earlier than the warning is generated. */
11114 fold_call_expr (location_t loc, tree exp, bool ignore)
11116 tree ret = NULL_TREE;
11117 tree fndecl = get_callee_fndecl (exp);
11119 && TREE_CODE (fndecl) == FUNCTION_DECL
11120 && DECL_BUILT_IN (fndecl)
11121 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11122 yet. Defer folding until we see all the arguments
11123 (after inlining). */
11124 && !CALL_EXPR_VA_ARG_PACK (exp))
11126 int nargs = call_expr_nargs (exp);
11128 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11129 instead last argument is __builtin_va_arg_pack (). Defer folding
11130 even in that case, until arguments are finalized. */
11131 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
11133 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
11135 && TREE_CODE (fndecl2) == FUNCTION_DECL
11136 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11137 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11141 if (avoid_folding_inline_builtin (fndecl))
11144 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11145 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
11146 CALL_EXPR_ARGP (exp), ignore);
11149 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
11151 tree *args = CALL_EXPR_ARGP (exp);
11152 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
11155 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
11163 /* Conveniently construct a function call expression. FNDECL names the
11164 function to be called and N arguments are passed in the array
11168 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
11170 tree fntype = TREE_TYPE (fndecl);
11171 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
11173 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
11176 /* Conveniently construct a function call expression. FNDECL names the
11177 function to be called and the arguments are passed in the vector
11181 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
11183 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
11184 VEC_address (tree, vec));
11188 /* Conveniently construct a function call expression. FNDECL names the
11189 function to be called, N is the number of arguments, and the "..."
11190 parameters are the argument expressions. */
11193 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
11196 tree *argarray = XALLOCAVEC (tree, n);
11200 for (i = 0; i < n; i++)
11201 argarray[i] = va_arg (ap, tree);
11203 return build_call_expr_loc_array (loc, fndecl, n, argarray);
11206 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11207 varargs macros aren't supported by all bootstrap compilers. */
11210 build_call_expr (tree fndecl, int n, ...)
11213 tree *argarray = XALLOCAVEC (tree, n);
11217 for (i = 0; i < n; i++)
11218 argarray[i] = va_arg (ap, tree);
11220 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
11223 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11224 N arguments are passed in the array ARGARRAY. */
11227 fold_builtin_call_array (location_t loc, tree type,
11232 tree ret = NULL_TREE;
11235 if (TREE_CODE (fn) == ADDR_EXPR)
11237 tree fndecl = TREE_OPERAND (fn, 0);
11238 if (TREE_CODE (fndecl) == FUNCTION_DECL
11239 && DECL_BUILT_IN (fndecl))
11241 /* If last argument is __builtin_va_arg_pack (), arguments to this
11242 function are not finalized yet. Defer folding until they are. */
11243 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
11245 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
11247 && TREE_CODE (fndecl2) == FUNCTION_DECL
11248 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
11249 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
11250 return build_call_array_loc (loc, type, fn, n, argarray);
11252 if (avoid_folding_inline_builtin (fndecl))
11253 return build_call_array_loc (loc, type, fn, n, argarray);
11254 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11256 ret = targetm.fold_builtin (fndecl, n, argarray, false);
11260 return build_call_array_loc (loc, type, fn, n, argarray);
11262 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
11264 /* First try the transformations that don't require consing up
11266 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
11271 /* If we got this far, we need to build an exp. */
11272 exp = build_call_array_loc (loc, type, fn, n, argarray);
11273 ret = fold_builtin_varargs (loc, fndecl, exp, false);
11274 return ret ? ret : exp;
11278 return build_call_array_loc (loc, type, fn, n, argarray);
11281 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11282 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11283 of arguments in ARGS to be omitted. OLDNARGS is the number of
11284 elements in ARGS. */
11287 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
11288 int skip, tree fndecl, int n, va_list newargs)
11290 int nargs = oldnargs - skip + n;
11297 buffer = XALLOCAVEC (tree, nargs);
11298 for (i = 0; i < n; i++)
11299 buffer[i] = va_arg (newargs, tree);
11300 for (j = skip; j < oldnargs; j++, i++)
11301 buffer[i] = args[j];
11304 buffer = args + skip;
11306 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
11309 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11310 list ARGS along with N new arguments specified as the "..."
11311 parameters. SKIP is the number of arguments in ARGS to be omitted.
11312 OLDNARGS is the number of elements in ARGS. */
11315 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
11316 int skip, tree fndecl, int n, ...)
11322 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
11328 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11329 along with N new arguments specified as the "..." parameters. SKIP
11330 is the number of arguments in EXP to be omitted. This function is used
11331 to do varargs-to-varargs transformations. */
11334 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
11340 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
11341 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
11347 /* Validate a single argument ARG against a tree code CODE representing
11351 validate_arg (const_tree arg, enum tree_code code)
11355 else if (code == POINTER_TYPE)
11356 return POINTER_TYPE_P (TREE_TYPE (arg));
11357 else if (code == INTEGER_TYPE)
11358 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
11359 return code == TREE_CODE (TREE_TYPE (arg));
11362 /* This function validates the types of a function call argument list
11363 against a specified list of tree_codes. If the last specifier is a 0,
11364 that represents an ellipses, otherwise the last specifier must be a
11367 This is the GIMPLE version of validate_arglist. Eventually we want to
11368 completely convert builtins.c to work from GIMPLEs and the tree based
11369 validate_arglist will then be removed. */
11372 validate_gimple_arglist (const_gimple call, ...)
11374 enum tree_code code;
11380 va_start (ap, call);
11385 code = (enum tree_code) va_arg (ap, int);
11389 /* This signifies an ellipses, any further arguments are all ok. */
11393 /* This signifies an endlink, if no arguments remain, return
11394 true, otherwise return false. */
11395 res = (i == gimple_call_num_args (call));
11398 /* If no parameters remain or the parameter's code does not
11399 match the specified code, return false. Otherwise continue
11400 checking any remaining arguments. */
11401 arg = gimple_call_arg (call, i++);
11402 if (!validate_arg (arg, code))
11409 /* We need gotos here since we can only have one VA_CLOSE in a
11417 /* This function validates the types of a function call argument list
11418 against a specified list of tree_codes. If the last specifier is a 0,
11419 that represents an ellipses, otherwise the last specifier must be a
11423 validate_arglist (const_tree callexpr, ...)
11425 enum tree_code code;
11428 const_call_expr_arg_iterator iter;
11431 va_start (ap, callexpr);
11432 init_const_call_expr_arg_iterator (callexpr, &iter);
11436 code = (enum tree_code) va_arg (ap, int);
11440 /* This signifies an ellipses, any further arguments are all ok. */
11444 /* This signifies an endlink, if no arguments remain, return
11445 true, otherwise return false. */
11446 res = !more_const_call_expr_args_p (&iter);
11449 /* If no parameters remain or the parameter's code does not
11450 match the specified code, return false. Otherwise continue
11451 checking any remaining arguments. */
11452 arg = next_const_call_expr_arg (&iter);
11453 if (!validate_arg (arg, code))
11460 /* We need gotos here since we can only have one VA_CLOSE in a
11468 /* Default target-specific builtin expander that does nothing. */
11471 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
11472 rtx target ATTRIBUTE_UNUSED,
11473 rtx subtarget ATTRIBUTE_UNUSED,
11474 enum machine_mode mode ATTRIBUTE_UNUSED,
11475 int ignore ATTRIBUTE_UNUSED)
11480 /* Returns true is EXP represents data that would potentially reside
11481 in a readonly section. */
11484 readonly_data_expr (tree exp)
11488 if (TREE_CODE (exp) != ADDR_EXPR)
11491 exp = get_base_address (TREE_OPERAND (exp, 0));
11495 /* Make sure we call decl_readonly_section only for trees it
11496 can handle (since it returns true for everything it doesn't
11498 if (TREE_CODE (exp) == STRING_CST
11499 || TREE_CODE (exp) == CONSTRUCTOR
11500 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
11501 return decl_readonly_section (exp, 0);
11506 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11507 to the call, and TYPE is its return type.
11509 Return NULL_TREE if no simplification was possible, otherwise return the
11510 simplified form of the call as a tree.
11512 The simplified form may be a constant or other expression which
11513 computes the same value, but in a more efficient manner (including
11514 calls to other builtin functions).
11516 The call may contain arguments which need to be evaluated, but
11517 which are not useful to determine the result of the call. In
11518 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11519 COMPOUND_EXPR will be an argument which must be evaluated.
11520 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11521 COMPOUND_EXPR in the chain will contain the tree for the simplified
11522 form of the builtin function call. */
11525 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
11527 if (!validate_arg (s1, POINTER_TYPE)
11528 || !validate_arg (s2, POINTER_TYPE))
11533 const char *p1, *p2;
11535 p2 = c_getstr (s2);
11539 p1 = c_getstr (s1);
11542 const char *r = strstr (p1, p2);
11546 return build_int_cst (TREE_TYPE (s1), 0);
11548 /* Return an offset into the constant string argument. */
11549 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11550 return fold_convert_loc (loc, type, tem);
11553 /* The argument is const char *, and the result is char *, so we need
11554 a type conversion here to avoid a warning. */
11556 return fold_convert_loc (loc, type, s1);
11561 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11565 /* New argument list transforming strstr(s1, s2) to
11566 strchr(s1, s2[0]). */
11567 return build_call_expr_loc (loc, fn, 2, s1,
11568 build_int_cst (integer_type_node, p2[0]));
11572 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11573 the call, and TYPE is its return type.
11575 Return NULL_TREE if no simplification was possible, otherwise return the
11576 simplified form of the call as a tree.
11578 The simplified form may be a constant or other expression which
11579 computes the same value, but in a more efficient manner (including
11580 calls to other builtin functions).
11582 The call may contain arguments which need to be evaluated, but
11583 which are not useful to determine the result of the call. In
11584 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11585 COMPOUND_EXPR will be an argument which must be evaluated.
11586 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11587 COMPOUND_EXPR in the chain will contain the tree for the simplified
11588 form of the builtin function call. */
11591 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
11593 if (!validate_arg (s1, POINTER_TYPE)
11594 || !validate_arg (s2, INTEGER_TYPE))
11600 if (TREE_CODE (s2) != INTEGER_CST)
11603 p1 = c_getstr (s1);
11610 if (target_char_cast (s2, &c))
11613 r = strchr (p1, c);
11616 return build_int_cst (TREE_TYPE (s1), 0);
11618 /* Return an offset into the constant string argument. */
11619 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11620 return fold_convert_loc (loc, type, tem);
11626 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11627 the call, and TYPE is its return type.
11629 Return NULL_TREE if no simplification was possible, otherwise return the
11630 simplified form of the call as a tree.
11632 The simplified form may be a constant or other expression which
11633 computes the same value, but in a more efficient manner (including
11634 calls to other builtin functions).
11636 The call may contain arguments which need to be evaluated, but
11637 which are not useful to determine the result of the call. In
11638 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11639 COMPOUND_EXPR will be an argument which must be evaluated.
11640 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11641 COMPOUND_EXPR in the chain will contain the tree for the simplified
11642 form of the builtin function call. */
11645 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
11647 if (!validate_arg (s1, POINTER_TYPE)
11648 || !validate_arg (s2, INTEGER_TYPE))
11655 if (TREE_CODE (s2) != INTEGER_CST)
11658 p1 = c_getstr (s1);
11665 if (target_char_cast (s2, &c))
11668 r = strrchr (p1, c);
11671 return build_int_cst (TREE_TYPE (s1), 0);
11673 /* Return an offset into the constant string argument. */
11674 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11675 return fold_convert_loc (loc, type, tem);
11678 if (! integer_zerop (s2))
11681 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11685 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11686 return build_call_expr_loc (loc, fn, 2, s1, s2);
11690 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11691 to the call, and TYPE is its return type.
11693 Return NULL_TREE if no simplification was possible, otherwise return the
11694 simplified form of the call as a tree.
11696 The simplified form may be a constant or other expression which
11697 computes the same value, but in a more efficient manner (including
11698 calls to other builtin functions).
11700 The call may contain arguments which need to be evaluated, but
11701 which are not useful to determine the result of the call. In
11702 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11703 COMPOUND_EXPR will be an argument which must be evaluated.
11704 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11705 COMPOUND_EXPR in the chain will contain the tree for the simplified
11706 form of the builtin function call. */
11709 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
11711 if (!validate_arg (s1, POINTER_TYPE)
11712 || !validate_arg (s2, POINTER_TYPE))
11717 const char *p1, *p2;
11719 p2 = c_getstr (s2);
11723 p1 = c_getstr (s1);
11726 const char *r = strpbrk (p1, p2);
11730 return build_int_cst (TREE_TYPE (s1), 0);
11732 /* Return an offset into the constant string argument. */
11733 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11734 return fold_convert_loc (loc, type, tem);
11738 /* strpbrk(x, "") == NULL.
11739 Evaluate and ignore s1 in case it had side-effects. */
11740 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11743 return NULL_TREE; /* Really call strpbrk. */
11745 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
11749 /* New argument list transforming strpbrk(s1, s2) to
11750 strchr(s1, s2[0]). */
11751 return build_call_expr_loc (loc, fn, 2, s1,
11752 build_int_cst (integer_type_node, p2[0]));
11756 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11759 Return NULL_TREE if no simplification was possible, otherwise return the
11760 simplified form of the call as a tree.
11762 The simplified form may be a constant or other expression which
11763 computes the same value, but in a more efficient manner (including
11764 calls to other builtin functions).
11766 The call may contain arguments which need to be evaluated, but
11767 which are not useful to determine the result of the call. In
11768 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11769 COMPOUND_EXPR will be an argument which must be evaluated.
11770 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11771 COMPOUND_EXPR in the chain will contain the tree for the simplified
11772 form of the builtin function call. */
11775 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11777 if (!validate_arg (dst, POINTER_TYPE)
11778 || !validate_arg (src, POINTER_TYPE))
11782 const char *p = c_getstr (src);
11784 /* If the string length is zero, return the dst parameter. */
11785 if (p && *p == '\0')
11788 if (optimize_insn_for_speed_p ())
11790 /* See if we can store by pieces into (dst + strlen(dst)). */
11792 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11793 tree strcpy_fn = builtin_decl_implicit (BUILT_IN_STRCPY);
11795 if (!strlen_fn || !strcpy_fn)
11798 /* If we don't have a movstr we don't want to emit an strcpy
11799 call. We have to do that if the length of the source string
11800 isn't computable (in that case we can use memcpy probably
11801 later expanding to a sequence of mov instructions). If we
11802 have movstr instructions we can emit strcpy calls. */
11805 tree len = c_strlen (src, 1);
11806 if (! len || TREE_SIDE_EFFECTS (len))
11810 /* Stabilize the argument list. */
11811 dst = builtin_save_expr (dst);
11813 /* Create strlen (dst). */
11814 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11815 /* Create (dst p+ strlen (dst)). */
11817 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11818 newdst = builtin_save_expr (newdst);
11820 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11821 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11827 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11828 arguments to the call.
11830 Return NULL_TREE if no simplification was possible, otherwise return the
11831 simplified form of the call as a tree.
11833 The simplified form may be a constant or other expression which
11834 computes the same value, but in a more efficient manner (including
11835 calls to other builtin functions).
11837 The call may contain arguments which need to be evaluated, but
11838 which are not useful to determine the result of the call. In
11839 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11840 COMPOUND_EXPR will be an argument which must be evaluated.
11841 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11842 COMPOUND_EXPR in the chain will contain the tree for the simplified
11843 form of the builtin function call. */
11846 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11848 if (!validate_arg (dst, POINTER_TYPE)
11849 || !validate_arg (src, POINTER_TYPE)
11850 || !validate_arg (len, INTEGER_TYPE))
11854 const char *p = c_getstr (src);
11856 /* If the requested length is zero, or the src parameter string
11857 length is zero, return the dst parameter. */
11858 if (integer_zerop (len) || (p && *p == '\0'))
11859 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11861 /* If the requested len is greater than or equal to the string
11862 length, call strcat. */
11863 if (TREE_CODE (len) == INTEGER_CST && p
11864 && compare_tree_int (len, strlen (p)) >= 0)
11866 tree fn = builtin_decl_implicit (BUILT_IN_STRCAT);
11868 /* If the replacement _DECL isn't initialized, don't do the
11873 return build_call_expr_loc (loc, fn, 2, dst, src);
11879 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11882 Return NULL_TREE if no simplification was possible, otherwise return the
11883 simplified form of the call as a tree.
11885 The simplified form may be a constant or other expression which
11886 computes the same value, but in a more efficient manner (including
11887 calls to other builtin functions).
11889 The call may contain arguments which need to be evaluated, but
11890 which are not useful to determine the result of the call. In
11891 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11892 COMPOUND_EXPR will be an argument which must be evaluated.
11893 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11894 COMPOUND_EXPR in the chain will contain the tree for the simplified
11895 form of the builtin function call. */
11898 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11900 if (!validate_arg (s1, POINTER_TYPE)
11901 || !validate_arg (s2, POINTER_TYPE))
11905 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11907 /* If both arguments are constants, evaluate at compile-time. */
11910 const size_t r = strspn (p1, p2);
11911 return size_int (r);
11914 /* If either argument is "", return NULL_TREE. */
11915 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11916 /* Evaluate and ignore both arguments in case either one has
11918 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11924 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11927 Return NULL_TREE if no simplification was possible, otherwise return the
11928 simplified form of the call as a tree.
11930 The simplified form may be a constant or other expression which
11931 computes the same value, but in a more efficient manner (including
11932 calls to other builtin functions).
11934 The call may contain arguments which need to be evaluated, but
11935 which are not useful to determine the result of the call. In
11936 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11937 COMPOUND_EXPR will be an argument which must be evaluated.
11938 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11939 COMPOUND_EXPR in the chain will contain the tree for the simplified
11940 form of the builtin function call. */
11943 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11945 if (!validate_arg (s1, POINTER_TYPE)
11946 || !validate_arg (s2, POINTER_TYPE))
11950 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11952 /* If both arguments are constants, evaluate at compile-time. */
11955 const size_t r = strcspn (p1, p2);
11956 return size_int (r);
11959 /* If the first argument is "", return NULL_TREE. */
11960 if (p1 && *p1 == '\0')
11962 /* Evaluate and ignore argument s2 in case it has
11964 return omit_one_operand_loc (loc, size_type_node,
11965 size_zero_node, s2);
11968 /* If the second argument is "", return __builtin_strlen(s1). */
11969 if (p2 && *p2 == '\0')
11971 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
11973 /* If the replacement _DECL isn't initialized, don't do the
11978 return build_call_expr_loc (loc, fn, 1, s1);
11984 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11985 to the call. IGNORE is true if the value returned
11986 by the builtin will be ignored. UNLOCKED is true is true if this
11987 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11988 the known length of the string. Return NULL_TREE if no simplification
11992 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11993 bool ignore, bool unlocked, tree len)
11995 /* If we're using an unlocked function, assume the other unlocked
11996 functions exist explicitly. */
11997 tree const fn_fputc = (unlocked
11998 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED)
11999 : builtin_decl_implicit (BUILT_IN_FPUTC));
12000 tree const fn_fwrite = (unlocked
12001 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED)
12002 : builtin_decl_implicit (BUILT_IN_FWRITE));
12004 /* If the return value is used, don't do the transformation. */
12008 /* Verify the arguments in the original call. */
12009 if (!validate_arg (arg0, POINTER_TYPE)
12010 || !validate_arg (arg1, POINTER_TYPE))
12014 len = c_strlen (arg0, 0);
12016 /* Get the length of the string passed to fputs. If the length
12017 can't be determined, punt. */
12019 || TREE_CODE (len) != INTEGER_CST)
12022 switch (compare_tree_int (len, 1))
12024 case -1: /* length is 0, delete the call entirely . */
12025 return omit_one_operand_loc (loc, integer_type_node,
12026 integer_zero_node, arg1);;
12028 case 0: /* length is 1, call fputc. */
12030 const char *p = c_getstr (arg0);
12035 return build_call_expr_loc (loc, fn_fputc, 2,
12037 (integer_type_node, p[0]), arg1);
12043 case 1: /* length is greater than 1, call fwrite. */
12045 /* If optimizing for size keep fputs. */
12046 if (optimize_function_for_size_p (cfun))
12048 /* New argument list transforming fputs(string, stream) to
12049 fwrite(string, 1, len, stream). */
12051 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
12052 size_one_node, len, arg1);
12057 gcc_unreachable ();
12062 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12063 produced. False otherwise. This is done so that we don't output the error
12064 or warning twice or three times. */
12067 fold_builtin_next_arg (tree exp, bool va_start_p)
12069 tree fntype = TREE_TYPE (current_function_decl);
12070 int nargs = call_expr_nargs (exp);
12073 if (!stdarg_p (fntype))
12075 error ("%<va_start%> used in function with fixed args");
12081 if (va_start_p && (nargs != 2))
12083 error ("wrong number of arguments to function %<va_start%>");
12086 arg = CALL_EXPR_ARG (exp, 1);
12088 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12089 when we checked the arguments and if needed issued a warning. */
12094 /* Evidently an out of date version of <stdarg.h>; can't validate
12095 va_start's second argument, but can still work as intended. */
12096 warning (0, "%<__builtin_next_arg%> called without an argument");
12099 else if (nargs > 1)
12101 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12104 arg = CALL_EXPR_ARG (exp, 0);
12107 if (TREE_CODE (arg) == SSA_NAME)
12108 arg = SSA_NAME_VAR (arg);
12110 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12111 or __builtin_next_arg (0) the first time we see it, after checking
12112 the arguments and if needed issuing a warning. */
12113 if (!integer_zerop (arg))
12115 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
12117 /* Strip off all nops for the sake of the comparison. This
12118 is not quite the same as STRIP_NOPS. It does more.
12119 We must also strip off INDIRECT_EXPR for C++ reference
12121 while (CONVERT_EXPR_P (arg)
12122 || TREE_CODE (arg) == INDIRECT_REF)
12123 arg = TREE_OPERAND (arg, 0);
12124 if (arg != last_parm)
12126 /* FIXME: Sometimes with the tree optimizers we can get the
12127 not the last argument even though the user used the last
12128 argument. We just warn and set the arg to be the last
12129 argument so that we will get wrong-code because of
12131 warning (0, "second parameter of %<va_start%> not last named argument");
12134 /* Undefined by C99 7.15.1.4p4 (va_start):
12135 "If the parameter parmN is declared with the register storage
12136 class, with a function or array type, or with a type that is
12137 not compatible with the type that results after application of
12138 the default argument promotions, the behavior is undefined."
12140 else if (DECL_REGISTER (arg))
12141 warning (0, "undefined behaviour when second parameter of "
12142 "%<va_start%> is declared with %<register%> storage");
12144 /* We want to verify the second parameter just once before the tree
12145 optimizers are run and then avoid keeping it in the tree,
12146 as otherwise we could warn even for correct code like:
12147 void foo (int i, ...)
12148 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12150 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
12152 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
12158 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12159 ORIG may be null if this is a 2-argument call. We don't attempt to
12160 simplify calls with more than 3 arguments.
12162 Return NULL_TREE if no simplification was possible, otherwise return the
12163 simplified form of the call as a tree. If IGNORED is true, it means that
12164 the caller does not use the returned value of the function. */
12167 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
12168 tree orig, int ignored)
12171 const char *fmt_str = NULL;
12173 /* Verify the required arguments in the original call. We deal with two
12174 types of sprintf() calls: 'sprintf (str, fmt)' and
12175 'sprintf (dest, "%s", orig)'. */
12176 if (!validate_arg (dest, POINTER_TYPE)
12177 || !validate_arg (fmt, POINTER_TYPE))
12179 if (orig && !validate_arg (orig, POINTER_TYPE))
12182 /* Check whether the format is a literal string constant. */
12183 fmt_str = c_getstr (fmt);
12184 if (fmt_str == NULL)
12188 retval = NULL_TREE;
12190 if (!init_target_chars ())
12193 /* If the format doesn't contain % args or %%, use strcpy. */
12194 if (strchr (fmt_str, target_percent) == NULL)
12196 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12201 /* Don't optimize sprintf (buf, "abc", ptr++). */
12205 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12206 'format' is known to contain no % formats. */
12207 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12209 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12212 /* If the format is "%s", use strcpy if the result isn't used. */
12213 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12216 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12221 /* Don't crash on sprintf (str1, "%s"). */
12225 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12228 retval = c_strlen (orig, 1);
12229 if (!retval || TREE_CODE (retval) != INTEGER_CST)
12232 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12235 if (call && retval)
12237 retval = fold_convert_loc
12238 (loc, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF))),
12240 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12246 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12247 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12248 attempt to simplify calls with more than 4 arguments.
12250 Return NULL_TREE if no simplification was possible, otherwise return the
12251 simplified form of the call as a tree. If IGNORED is true, it means that
12252 the caller does not use the returned value of the function. */
12255 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
12256 tree orig, int ignored)
12259 const char *fmt_str = NULL;
12260 unsigned HOST_WIDE_INT destlen;
12262 /* Verify the required arguments in the original call. We deal with two
12263 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12264 'snprintf (dest, cst, "%s", orig)'. */
12265 if (!validate_arg (dest, POINTER_TYPE)
12266 || !validate_arg (destsize, INTEGER_TYPE)
12267 || !validate_arg (fmt, POINTER_TYPE))
12269 if (orig && !validate_arg (orig, POINTER_TYPE))
12272 if (!host_integerp (destsize, 1))
12275 /* Check whether the format is a literal string constant. */
12276 fmt_str = c_getstr (fmt);
12277 if (fmt_str == NULL)
12281 retval = NULL_TREE;
12283 if (!init_target_chars ())
12286 destlen = tree_low_cst (destsize, 1);
12288 /* If the format doesn't contain % args or %%, use strcpy. */
12289 if (strchr (fmt_str, target_percent) == NULL)
12291 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12292 size_t len = strlen (fmt_str);
12294 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12298 /* We could expand this as
12299 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12301 memcpy (str, fmt_with_nul_at_cstm1, cst);
12302 but in the former case that might increase code size
12303 and in the latter case grow .rodata section too much.
12304 So punt for now. */
12305 if (len >= destlen)
12311 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12312 'format' is known to contain no % formats and
12313 strlen (fmt) < cst. */
12314 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
12317 retval = build_int_cst (integer_type_node, strlen (fmt_str));
12320 /* If the format is "%s", use strcpy if the result isn't used. */
12321 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
12323 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
12324 unsigned HOST_WIDE_INT origlen;
12326 /* Don't crash on snprintf (str1, cst, "%s"). */
12330 retval = c_strlen (orig, 1);
12331 if (!retval || !host_integerp (retval, 1))
12334 origlen = tree_low_cst (retval, 1);
12335 /* We could expand this as
12336 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12338 memcpy (str1, str2_with_nul_at_cstm1, cst);
12339 but in the former case that might increase code size
12340 and in the latter case grow .rodata section too much.
12341 So punt for now. */
12342 if (origlen >= destlen)
12345 /* Convert snprintf (str1, cst, "%s", str2) into
12346 strcpy (str1, str2) if strlen (str2) < cst. */
12350 call = build_call_expr_loc (loc, fn, 2, dest, orig);
12353 retval = NULL_TREE;
12356 if (call && retval)
12358 tree fn = builtin_decl_explicit (BUILT_IN_SNPRINTF);
12359 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
12360 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
12366 /* Expand a call EXP to __builtin_object_size. */
12369 expand_builtin_object_size (tree exp)
12372 int object_size_type;
12373 tree fndecl = get_callee_fndecl (exp);
12375 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
12377 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12379 expand_builtin_trap ();
12383 ost = CALL_EXPR_ARG (exp, 1);
12386 if (TREE_CODE (ost) != INTEGER_CST
12387 || tree_int_cst_sgn (ost) < 0
12388 || compare_tree_int (ost, 3) > 0)
12390 error ("%Klast argument of %D is not integer constant between 0 and 3",
12392 expand_builtin_trap ();
12396 object_size_type = tree_low_cst (ost, 0);
12398 return object_size_type < 2 ? constm1_rtx : const0_rtx;
12401 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12402 FCODE is the BUILT_IN_* to use.
12403 Return NULL_RTX if we failed; the caller should emit a normal call,
12404 otherwise try to get the result in TARGET, if convenient (and in
12405 mode MODE if that's convenient). */
12408 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
12409 enum built_in_function fcode)
12411 tree dest, src, len, size;
12413 if (!validate_arglist (exp,
12415 fcode == BUILT_IN_MEMSET_CHK
12416 ? INTEGER_TYPE : POINTER_TYPE,
12417 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
12420 dest = CALL_EXPR_ARG (exp, 0);
12421 src = CALL_EXPR_ARG (exp, 1);
12422 len = CALL_EXPR_ARG (exp, 2);
12423 size = CALL_EXPR_ARG (exp, 3);
12425 if (! host_integerp (size, 1))
12428 if (host_integerp (len, 1) || integer_all_onesp (size))
12432 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
12434 warning_at (tree_nonartificial_location (exp),
12435 0, "%Kcall to %D will always overflow destination buffer",
12436 exp, get_callee_fndecl (exp));
12441 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12442 mem{cpy,pcpy,move,set} is available. */
12445 case BUILT_IN_MEMCPY_CHK:
12446 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12448 case BUILT_IN_MEMPCPY_CHK:
12449 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12451 case BUILT_IN_MEMMOVE_CHK:
12452 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12454 case BUILT_IN_MEMSET_CHK:
12455 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12464 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
12465 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12466 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12467 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12469 else if (fcode == BUILT_IN_MEMSET_CHK)
12473 unsigned int dest_align = get_pointer_alignment (dest);
12475 /* If DEST is not a pointer type, call the normal function. */
12476 if (dest_align == 0)
12479 /* If SRC and DEST are the same (and not volatile), do nothing. */
12480 if (operand_equal_p (src, dest, 0))
12484 if (fcode != BUILT_IN_MEMPCPY_CHK)
12486 /* Evaluate and ignore LEN in case it has side-effects. */
12487 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
12488 return expand_expr (dest, target, mode, EXPAND_NORMAL);
12491 expr = fold_build_pointer_plus (dest, len);
12492 return expand_expr (expr, target, mode, EXPAND_NORMAL);
12495 /* __memmove_chk special case. */
12496 if (fcode == BUILT_IN_MEMMOVE_CHK)
12498 unsigned int src_align = get_pointer_alignment (src);
12500 if (src_align == 0)
12503 /* If src is categorized for a readonly section we can use
12504 normal __memcpy_chk. */
12505 if (readonly_data_expr (src))
12507 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12510 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
12511 dest, src, len, size);
12512 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
12513 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
12514 return expand_expr (fn, target, mode, EXPAND_NORMAL);
12521 /* Emit warning if a buffer overflow is detected at compile time. */
12524 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
12528 location_t loc = tree_nonartificial_location (exp);
12532 case BUILT_IN_STRCPY_CHK:
12533 case BUILT_IN_STPCPY_CHK:
12534 /* For __strcat_chk the warning will be emitted only if overflowing
12535 by at least strlen (dest) + 1 bytes. */
12536 case BUILT_IN_STRCAT_CHK:
12537 len = CALL_EXPR_ARG (exp, 1);
12538 size = CALL_EXPR_ARG (exp, 2);
12541 case BUILT_IN_STRNCAT_CHK:
12542 case BUILT_IN_STRNCPY_CHK:
12543 len = CALL_EXPR_ARG (exp, 2);
12544 size = CALL_EXPR_ARG (exp, 3);
12546 case BUILT_IN_SNPRINTF_CHK:
12547 case BUILT_IN_VSNPRINTF_CHK:
12548 len = CALL_EXPR_ARG (exp, 1);
12549 size = CALL_EXPR_ARG (exp, 3);
12552 gcc_unreachable ();
12558 if (! host_integerp (size, 1) || integer_all_onesp (size))
12563 len = c_strlen (len, 1);
12564 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12567 else if (fcode == BUILT_IN_STRNCAT_CHK)
12569 tree src = CALL_EXPR_ARG (exp, 1);
12570 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
12572 src = c_strlen (src, 1);
12573 if (! src || ! host_integerp (src, 1))
12575 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
12576 exp, get_callee_fndecl (exp));
12579 else if (tree_int_cst_lt (src, size))
12582 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
12585 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
12586 exp, get_callee_fndecl (exp));
12589 /* Emit warning if a buffer overflow is detected at compile time
12590 in __sprintf_chk/__vsprintf_chk calls. */
12593 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
12595 tree size, len, fmt;
12596 const char *fmt_str;
12597 int nargs = call_expr_nargs (exp);
12599 /* Verify the required arguments in the original call. */
12603 size = CALL_EXPR_ARG (exp, 2);
12604 fmt = CALL_EXPR_ARG (exp, 3);
12606 if (! host_integerp (size, 1) || integer_all_onesp (size))
12609 /* Check whether the format is a literal string constant. */
12610 fmt_str = c_getstr (fmt);
12611 if (fmt_str == NULL)
12614 if (!init_target_chars ())
12617 /* If the format doesn't contain % args or %%, we know its size. */
12618 if (strchr (fmt_str, target_percent) == 0)
12619 len = build_int_cstu (size_type_node, strlen (fmt_str));
12620 /* If the format is "%s" and first ... argument is a string literal,
12622 else if (fcode == BUILT_IN_SPRINTF_CHK
12623 && strcmp (fmt_str, target_percent_s) == 0)
12629 arg = CALL_EXPR_ARG (exp, 4);
12630 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
12633 len = c_strlen (arg, 1);
12634 if (!len || ! host_integerp (len, 1))
12640 if (! tree_int_cst_lt (len, size))
12641 warning_at (tree_nonartificial_location (exp),
12642 0, "%Kcall to %D will always overflow destination buffer",
12643 exp, get_callee_fndecl (exp));
12646 /* Emit warning if a free is called with address of a variable. */
12649 maybe_emit_free_warning (tree exp)
12651 tree arg = CALL_EXPR_ARG (exp, 0);
12654 if (TREE_CODE (arg) != ADDR_EXPR)
12657 arg = get_base_address (TREE_OPERAND (arg, 0));
12658 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
12661 if (SSA_VAR_P (arg))
12662 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12663 "%Kattempt to free a non-heap object %qD", exp, arg);
12665 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
12666 "%Kattempt to free a non-heap object", exp);
12669 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12673 fold_builtin_object_size (tree ptr, tree ost)
12675 unsigned HOST_WIDE_INT bytes;
12676 int object_size_type;
12678 if (!validate_arg (ptr, POINTER_TYPE)
12679 || !validate_arg (ost, INTEGER_TYPE))
12684 if (TREE_CODE (ost) != INTEGER_CST
12685 || tree_int_cst_sgn (ost) < 0
12686 || compare_tree_int (ost, 3) > 0)
12689 object_size_type = tree_low_cst (ost, 0);
12691 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12692 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12693 and (size_t) 0 for types 2 and 3. */
12694 if (TREE_SIDE_EFFECTS (ptr))
12695 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
12697 if (TREE_CODE (ptr) == ADDR_EXPR)
12699 bytes = compute_builtin_object_size (ptr, object_size_type);
12700 if (double_int_fits_to_tree_p (size_type_node,
12701 uhwi_to_double_int (bytes)))
12702 return build_int_cstu (size_type_node, bytes);
12704 else if (TREE_CODE (ptr) == SSA_NAME)
12706 /* If object size is not known yet, delay folding until
12707 later. Maybe subsequent passes will help determining
12709 bytes = compute_builtin_object_size (ptr, object_size_type);
12710 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
12711 && double_int_fits_to_tree_p (size_type_node,
12712 uhwi_to_double_int (bytes)))
12713 return build_int_cstu (size_type_node, bytes);
12719 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12720 DEST, SRC, LEN, and SIZE are the arguments to the call.
12721 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12722 code of the builtin. If MAXLEN is not NULL, it is maximum length
12723 passed as third argument. */
12726 fold_builtin_memory_chk (location_t loc, tree fndecl,
12727 tree dest, tree src, tree len, tree size,
12728 tree maxlen, bool ignore,
12729 enum built_in_function fcode)
12733 if (!validate_arg (dest, POINTER_TYPE)
12734 || !validate_arg (src,
12735 (fcode == BUILT_IN_MEMSET_CHK
12736 ? INTEGER_TYPE : POINTER_TYPE))
12737 || !validate_arg (len, INTEGER_TYPE)
12738 || !validate_arg (size, INTEGER_TYPE))
12741 /* If SRC and DEST are the same (and not volatile), return DEST
12742 (resp. DEST+LEN for __mempcpy_chk). */
12743 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12745 if (fcode != BUILT_IN_MEMPCPY_CHK)
12746 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12750 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12751 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12755 if (! host_integerp (size, 1))
12758 if (! integer_all_onesp (size))
12760 if (! host_integerp (len, 1))
12762 /* If LEN is not constant, try MAXLEN too.
12763 For MAXLEN only allow optimizing into non-_ocs function
12764 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12765 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12767 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12769 /* (void) __mempcpy_chk () can be optimized into
12770 (void) __memcpy_chk (). */
12771 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12775 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12783 if (tree_int_cst_lt (size, maxlen))
12788 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12789 mem{cpy,pcpy,move,set} is available. */
12792 case BUILT_IN_MEMCPY_CHK:
12793 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
12795 case BUILT_IN_MEMPCPY_CHK:
12796 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
12798 case BUILT_IN_MEMMOVE_CHK:
12799 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
12801 case BUILT_IN_MEMSET_CHK:
12802 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
12811 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12814 /* Fold a call to the __st[rp]cpy_chk builtin.
12815 DEST, SRC, and SIZE are the arguments to the call.
12816 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12817 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12818 strings passed as second argument. */
12821 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12822 tree src, tree size,
12823 tree maxlen, bool ignore,
12824 enum built_in_function fcode)
12828 if (!validate_arg (dest, POINTER_TYPE)
12829 || !validate_arg (src, POINTER_TYPE)
12830 || !validate_arg (size, INTEGER_TYPE))
12833 /* If SRC and DEST are the same (and not volatile), return DEST. */
12834 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12835 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12837 if (! host_integerp (size, 1))
12840 if (! integer_all_onesp (size))
12842 len = c_strlen (src, 1);
12843 if (! len || ! host_integerp (len, 1))
12845 /* If LEN is not constant, try MAXLEN too.
12846 For MAXLEN only allow optimizing into non-_ocs function
12847 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12848 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12850 if (fcode == BUILT_IN_STPCPY_CHK)
12855 /* If return value of __stpcpy_chk is ignored,
12856 optimize into __strcpy_chk. */
12857 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
12861 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12864 if (! len || TREE_SIDE_EFFECTS (len))
12867 /* If c_strlen returned something, but not a constant,
12868 transform __strcpy_chk into __memcpy_chk. */
12869 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
12873 len = fold_convert_loc (loc, size_type_node, len);
12874 len = size_binop_loc (loc, PLUS_EXPR, len,
12875 build_int_cst (size_type_node, 1));
12876 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12877 build_call_expr_loc (loc, fn, 4,
12878 dest, src, len, size));
12884 if (! tree_int_cst_lt (maxlen, size))
12888 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12889 fn = builtin_decl_explicit (fcode == BUILT_IN_STPCPY_CHK
12890 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY);
12894 return build_call_expr_loc (loc, fn, 2, dest, src);
12897 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12898 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12899 length passed as third argument. */
12902 fold_builtin_strncpy_chk (location_t loc, tree dest, tree src,
12903 tree len, tree size, tree maxlen)
12907 if (!validate_arg (dest, POINTER_TYPE)
12908 || !validate_arg (src, POINTER_TYPE)
12909 || !validate_arg (len, INTEGER_TYPE)
12910 || !validate_arg (size, INTEGER_TYPE))
12913 if (! host_integerp (size, 1))
12916 if (! integer_all_onesp (size))
12918 if (! host_integerp (len, 1))
12920 /* If LEN is not constant, try MAXLEN too.
12921 For MAXLEN only allow optimizing into non-_ocs function
12922 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12923 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12929 if (tree_int_cst_lt (size, maxlen))
12933 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12934 fn = builtin_decl_explicit (BUILT_IN_STRNCPY);
12938 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12941 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12942 are the arguments to the call. */
12945 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12946 tree src, tree size)
12951 if (!validate_arg (dest, POINTER_TYPE)
12952 || !validate_arg (src, POINTER_TYPE)
12953 || !validate_arg (size, INTEGER_TYPE))
12956 p = c_getstr (src);
12957 /* If the SRC parameter is "", return DEST. */
12958 if (p && *p == '\0')
12959 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12961 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12964 /* If __builtin_strcat_chk is used, assume strcat is available. */
12965 fn = builtin_decl_explicit (BUILT_IN_STRCAT);
12969 return build_call_expr_loc (loc, fn, 2, dest, src);
12972 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12976 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12977 tree dest, tree src, tree len, tree size)
12982 if (!validate_arg (dest, POINTER_TYPE)
12983 || !validate_arg (src, POINTER_TYPE)
12984 || !validate_arg (size, INTEGER_TYPE)
12985 || !validate_arg (size, INTEGER_TYPE))
12988 p = c_getstr (src);
12989 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12990 if (p && *p == '\0')
12991 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12992 else if (integer_zerop (len))
12993 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12995 if (! host_integerp (size, 1))
12998 if (! integer_all_onesp (size))
13000 tree src_len = c_strlen (src, 1);
13002 && host_integerp (src_len, 1)
13003 && host_integerp (len, 1)
13004 && ! tree_int_cst_lt (len, src_len))
13006 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
13007 fn = builtin_decl_explicit (BUILT_IN_STRCAT_CHK);
13011 return build_call_expr_loc (loc, fn, 3, dest, src, size);
13016 /* If __builtin_strncat_chk is used, assume strncat is available. */
13017 fn = builtin_decl_explicit (BUILT_IN_STRNCAT);
13021 return build_call_expr_loc (loc, fn, 3, dest, src, len);
13024 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13025 Return NULL_TREE if a normal call should be emitted rather than
13026 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13027 or BUILT_IN_VSPRINTF_CHK. */
13030 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
13031 enum built_in_function fcode)
13033 tree dest, size, len, fn, fmt, flag;
13034 const char *fmt_str;
13036 /* Verify the required arguments in the original call. */
13040 if (!validate_arg (dest, POINTER_TYPE))
13043 if (!validate_arg (flag, INTEGER_TYPE))
13046 if (!validate_arg (size, INTEGER_TYPE))
13049 if (!validate_arg (fmt, POINTER_TYPE))
13052 if (! host_integerp (size, 1))
13057 if (!init_target_chars ())
13060 /* Check whether the format is a literal string constant. */
13061 fmt_str = c_getstr (fmt);
13062 if (fmt_str != NULL)
13064 /* If the format doesn't contain % args or %%, we know the size. */
13065 if (strchr (fmt_str, target_percent) == 0)
13067 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
13068 len = build_int_cstu (size_type_node, strlen (fmt_str));
13070 /* If the format is "%s" and first ... argument is a string literal,
13071 we know the size too. */
13072 else if (fcode == BUILT_IN_SPRINTF_CHK
13073 && strcmp (fmt_str, target_percent_s) == 0)
13080 if (validate_arg (arg, POINTER_TYPE))
13082 len = c_strlen (arg, 1);
13083 if (! len || ! host_integerp (len, 1))
13090 if (! integer_all_onesp (size))
13092 if (! len || ! tree_int_cst_lt (len, size))
13096 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13097 or if format doesn't contain % chars or is "%s". */
13098 if (! integer_zerop (flag))
13100 if (fmt_str == NULL)
13102 if (strchr (fmt_str, target_percent) != NULL
13103 && strcmp (fmt_str, target_percent_s))
13107 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13108 fn = builtin_decl_explicit (fcode == BUILT_IN_VSPRINTF_CHK
13109 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF);
13113 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
13116 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13117 a normal call should be emitted rather than expanding the function
13118 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13121 fold_builtin_sprintf_chk (location_t loc, tree exp,
13122 enum built_in_function fcode)
13124 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
13125 CALL_EXPR_ARGP (exp), fcode);
13128 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13129 NULL_TREE if a normal call should be emitted rather than expanding
13130 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13131 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13132 passed as second argument. */
13135 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
13136 tree maxlen, enum built_in_function fcode)
13138 tree dest, size, len, fn, fmt, flag;
13139 const char *fmt_str;
13141 /* Verify the required arguments in the original call. */
13145 if (!validate_arg (dest, POINTER_TYPE))
13148 if (!validate_arg (len, INTEGER_TYPE))
13151 if (!validate_arg (flag, INTEGER_TYPE))
13154 if (!validate_arg (size, INTEGER_TYPE))
13157 if (!validate_arg (fmt, POINTER_TYPE))
13160 if (! host_integerp (size, 1))
13163 if (! integer_all_onesp (size))
13165 if (! host_integerp (len, 1))
13167 /* If LEN is not constant, try MAXLEN too.
13168 For MAXLEN only allow optimizing into non-_ocs function
13169 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13170 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
13176 if (tree_int_cst_lt (size, maxlen))
13180 if (!init_target_chars ())
13183 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13184 or if format doesn't contain % chars or is "%s". */
13185 if (! integer_zerop (flag))
13187 fmt_str = c_getstr (fmt);
13188 if (fmt_str == NULL)
13190 if (strchr (fmt_str, target_percent) != NULL
13191 && strcmp (fmt_str, target_percent_s))
13195 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13197 fn = builtin_decl_explicit (fcode == BUILT_IN_VSNPRINTF_CHK
13198 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF);
13202 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
13205 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13206 a normal call should be emitted rather than expanding the function
13207 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13208 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13209 passed as second argument. */
13212 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
13213 enum built_in_function fcode)
13215 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
13216 CALL_EXPR_ARGP (exp), maxlen, fcode);
13219 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13220 FMT and ARG are the arguments to the call; we don't fold cases with
13221 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13223 Return NULL_TREE if no simplification was possible, otherwise return the
13224 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13225 code of the function to be simplified. */
13228 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
13229 tree arg, bool ignore,
13230 enum built_in_function fcode)
13232 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
13233 const char *fmt_str = NULL;
13235 /* If the return value is used, don't do the transformation. */
13239 /* Verify the required arguments in the original call. */
13240 if (!validate_arg (fmt, POINTER_TYPE))
13243 /* Check whether the format is a literal string constant. */
13244 fmt_str = c_getstr (fmt);
13245 if (fmt_str == NULL)
13248 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
13250 /* If we're using an unlocked function, assume the other
13251 unlocked functions exist explicitly. */
13252 fn_putchar = builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED);
13253 fn_puts = builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED);
13257 fn_putchar = builtin_decl_implicit (BUILT_IN_PUTCHAR);
13258 fn_puts = builtin_decl_implicit (BUILT_IN_PUTS);
13261 if (!init_target_chars ())
13264 if (strcmp (fmt_str, target_percent_s) == 0
13265 || strchr (fmt_str, target_percent) == NULL)
13269 if (strcmp (fmt_str, target_percent_s) == 0)
13271 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13274 if (!arg || !validate_arg (arg, POINTER_TYPE))
13277 str = c_getstr (arg);
13283 /* The format specifier doesn't contain any '%' characters. */
13284 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
13290 /* If the string was "", printf does nothing. */
13291 if (str[0] == '\0')
13292 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13294 /* If the string has length of 1, call putchar. */
13295 if (str[1] == '\0')
13297 /* Given printf("c"), (where c is any one character,)
13298 convert "c"[0] to an int and pass that to the replacement
13300 newarg = build_int_cst (integer_type_node, str[0]);
13302 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
13306 /* If the string was "string\n", call puts("string"). */
13307 size_t len = strlen (str);
13308 if ((unsigned char)str[len - 1] == target_newline
13309 && (size_t) (int) len == len
13313 tree offset_node, string_cst;
13315 /* Create a NUL-terminated string that's one char shorter
13316 than the original, stripping off the trailing '\n'. */
13317 newarg = build_string_literal (len, str);
13318 string_cst = string_constant (newarg, &offset_node);
13319 gcc_checking_assert (string_cst
13320 && (TREE_STRING_LENGTH (string_cst)
13322 && integer_zerop (offset_node)
13324 TREE_STRING_POINTER (string_cst)[len - 1]
13325 == target_newline);
13326 /* build_string_literal creates a new STRING_CST,
13327 modify it in place to avoid double copying. */
13328 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
13329 newstr[len - 1] = '\0';
13331 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
13334 /* We'd like to arrange to call fputs(string,stdout) here,
13335 but we need stdout and don't have a way to get it yet. */
13340 /* The other optimizations can be done only on the non-va_list variants. */
13341 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
13344 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13345 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
13347 if (!arg || !validate_arg (arg, POINTER_TYPE))
13350 call = build_call_expr_loc (loc, fn_puts, 1, arg);
13353 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13354 else if (strcmp (fmt_str, target_percent_c) == 0)
13356 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13359 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
13365 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13368 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13369 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13370 more than 3 arguments, and ARG may be null in the 2-argument case.
13372 Return NULL_TREE if no simplification was possible, otherwise return the
13373 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13374 code of the function to be simplified. */
13377 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
13378 tree fmt, tree arg, bool ignore,
13379 enum built_in_function fcode)
13381 tree fn_fputc, fn_fputs, call = NULL_TREE;
13382 const char *fmt_str = NULL;
13384 /* If the return value is used, don't do the transformation. */
13388 /* Verify the required arguments in the original call. */
13389 if (!validate_arg (fp, POINTER_TYPE))
13391 if (!validate_arg (fmt, POINTER_TYPE))
13394 /* Check whether the format is a literal string constant. */
13395 fmt_str = c_getstr (fmt);
13396 if (fmt_str == NULL)
13399 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
13401 /* If we're using an unlocked function, assume the other
13402 unlocked functions exist explicitly. */
13403 fn_fputc = builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED);
13404 fn_fputs = builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED);
13408 fn_fputc = builtin_decl_implicit (BUILT_IN_FPUTC);
13409 fn_fputs = builtin_decl_implicit (BUILT_IN_FPUTS);
13412 if (!init_target_chars ())
13415 /* If the format doesn't contain % args or %%, use strcpy. */
13416 if (strchr (fmt_str, target_percent) == NULL)
13418 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
13422 /* If the format specifier was "", fprintf does nothing. */
13423 if (fmt_str[0] == '\0')
13425 /* If FP has side-effects, just wait until gimplification is
13427 if (TREE_SIDE_EFFECTS (fp))
13430 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
13433 /* When "string" doesn't contain %, replace all cases of
13434 fprintf (fp, string) with fputs (string, fp). The fputs
13435 builtin will take care of special cases like length == 1. */
13437 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
13440 /* The other optimizations can be done only on the non-va_list variants. */
13441 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
13444 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13445 else if (strcmp (fmt_str, target_percent_s) == 0)
13447 if (!arg || !validate_arg (arg, POINTER_TYPE))
13450 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
13453 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13454 else if (strcmp (fmt_str, target_percent_c) == 0)
13456 if (!arg || !validate_arg (arg, INTEGER_TYPE))
13459 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
13464 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
13467 /* Initialize format string characters in the target charset. */
13470 init_target_chars (void)
13475 target_newline = lang_hooks.to_target_charset ('\n');
13476 target_percent = lang_hooks.to_target_charset ('%');
13477 target_c = lang_hooks.to_target_charset ('c');
13478 target_s = lang_hooks.to_target_charset ('s');
13479 if (target_newline == 0 || target_percent == 0 || target_c == 0
13483 target_percent_c[0] = target_percent;
13484 target_percent_c[1] = target_c;
13485 target_percent_c[2] = '\0';
13487 target_percent_s[0] = target_percent;
13488 target_percent_s[1] = target_s;
13489 target_percent_s[2] = '\0';
13491 target_percent_s_newline[0] = target_percent;
13492 target_percent_s_newline[1] = target_s;
13493 target_percent_s_newline[2] = target_newline;
13494 target_percent_s_newline[3] = '\0';
13501 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13502 and no overflow/underflow occurred. INEXACT is true if M was not
13503 exactly calculated. TYPE is the tree type for the result. This
13504 function assumes that you cleared the MPFR flags and then
13505 calculated M to see if anything subsequently set a flag prior to
13506 entering this function. Return NULL_TREE if any checks fail. */
13509 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
13511 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13512 overflow/underflow occurred. If -frounding-math, proceed iff the
13513 result of calling FUNC was exact. */
13514 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13515 && (!flag_rounding_math || !inexact))
13517 REAL_VALUE_TYPE rr;
13519 real_from_mpfr (&rr, m, type, GMP_RNDN);
13520 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13521 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13522 but the mpft_t is not, then we underflowed in the
13524 if (real_isfinite (&rr)
13525 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
13527 REAL_VALUE_TYPE rmode;
13529 real_convert (&rmode, TYPE_MODE (type), &rr);
13530 /* Proceed iff the specified mode can hold the value. */
13531 if (real_identical (&rmode, &rr))
13532 return build_real (type, rmode);
13538 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13539 number and no overflow/underflow occurred. INEXACT is true if M
13540 was not exactly calculated. TYPE is the tree type for the result.
13541 This function assumes that you cleared the MPFR flags and then
13542 calculated M to see if anything subsequently set a flag prior to
13543 entering this function. Return NULL_TREE if any checks fail, if
13544 FORCE_CONVERT is true, then bypass the checks. */
13547 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
13549 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13550 overflow/underflow occurred. If -frounding-math, proceed iff the
13551 result of calling FUNC was exact. */
13553 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
13554 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13555 && (!flag_rounding_math || !inexact)))
13557 REAL_VALUE_TYPE re, im;
13559 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
13560 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
13561 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13562 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13563 but the mpft_t is not, then we underflowed in the
13566 || (real_isfinite (&re) && real_isfinite (&im)
13567 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
13568 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
13570 REAL_VALUE_TYPE re_mode, im_mode;
13572 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
13573 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
13574 /* Proceed iff the specified mode can hold the value. */
13576 || (real_identical (&re_mode, &re)
13577 && real_identical (&im_mode, &im)))
13578 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
13579 build_real (TREE_TYPE (type), im_mode));
13585 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13586 FUNC on it and return the resulting value as a tree with type TYPE.
13587 If MIN and/or MAX are not NULL, then the supplied ARG must be
13588 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13589 acceptable values, otherwise they are not. The mpfr precision is
13590 set to the precision of TYPE. We assume that function FUNC returns
13591 zero if the result could be calculated exactly within the requested
13595 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
13596 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
13599 tree result = NULL_TREE;
13603 /* To proceed, MPFR must exactly represent the target floating point
13604 format, which only happens when the target base equals two. */
13605 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13606 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
13608 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13610 if (real_isfinite (ra)
13611 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
13612 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
13614 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13615 const int prec = fmt->p;
13616 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13620 mpfr_init2 (m, prec);
13621 mpfr_from_real (m, ra, GMP_RNDN);
13622 mpfr_clear_flags ();
13623 inexact = func (m, m, rnd);
13624 result = do_mpfr_ckconv (m, type, inexact);
13632 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13633 FUNC on it and return the resulting value as a tree with type TYPE.
13634 The mpfr precision is set to the precision of TYPE. We assume that
13635 function FUNC returns zero if the result could be calculated
13636 exactly within the requested precision. */
13639 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
13640 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13642 tree result = NULL_TREE;
13647 /* To proceed, MPFR must exactly represent the target floating point
13648 format, which only happens when the target base equals two. */
13649 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13650 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13651 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13653 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13654 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13656 if (real_isfinite (ra1) && real_isfinite (ra2))
13658 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13659 const int prec = fmt->p;
13660 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13664 mpfr_inits2 (prec, m1, m2, NULL);
13665 mpfr_from_real (m1, ra1, GMP_RNDN);
13666 mpfr_from_real (m2, ra2, GMP_RNDN);
13667 mpfr_clear_flags ();
13668 inexact = func (m1, m1, m2, rnd);
13669 result = do_mpfr_ckconv (m1, type, inexact);
13670 mpfr_clears (m1, m2, NULL);
13677 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13678 FUNC on it and return the resulting value as a tree with type TYPE.
13679 The mpfr precision is set to the precision of TYPE. We assume that
13680 function FUNC returns zero if the result could be calculated
13681 exactly within the requested precision. */
13684 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
13685 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
13687 tree result = NULL_TREE;
13693 /* To proceed, MPFR must exactly represent the target floating point
13694 format, which only happens when the target base equals two. */
13695 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13696 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
13697 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
13698 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
13700 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
13701 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
13702 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
13704 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
13706 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13707 const int prec = fmt->p;
13708 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13712 mpfr_inits2 (prec, m1, m2, m3, NULL);
13713 mpfr_from_real (m1, ra1, GMP_RNDN);
13714 mpfr_from_real (m2, ra2, GMP_RNDN);
13715 mpfr_from_real (m3, ra3, GMP_RNDN);
13716 mpfr_clear_flags ();
13717 inexact = func (m1, m1, m2, m3, rnd);
13718 result = do_mpfr_ckconv (m1, type, inexact);
13719 mpfr_clears (m1, m2, m3, NULL);
13726 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13727 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13728 If ARG_SINP and ARG_COSP are NULL then the result is returned
13729 as a complex value.
13730 The type is taken from the type of ARG and is used for setting the
13731 precision of the calculation and results. */
13734 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13736 tree const type = TREE_TYPE (arg);
13737 tree result = NULL_TREE;
13741 /* To proceed, MPFR must exactly represent the target floating point
13742 format, which only happens when the target base equals two. */
13743 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13744 && TREE_CODE (arg) == REAL_CST
13745 && !TREE_OVERFLOW (arg))
13747 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13749 if (real_isfinite (ra))
13751 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13752 const int prec = fmt->p;
13753 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13754 tree result_s, result_c;
13758 mpfr_inits2 (prec, m, ms, mc, NULL);
13759 mpfr_from_real (m, ra, GMP_RNDN);
13760 mpfr_clear_flags ();
13761 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13762 result_s = do_mpfr_ckconv (ms, type, inexact);
13763 result_c = do_mpfr_ckconv (mc, type, inexact);
13764 mpfr_clears (m, ms, mc, NULL);
13765 if (result_s && result_c)
13767 /* If we are to return in a complex value do so. */
13768 if (!arg_sinp && !arg_cosp)
13769 return build_complex (build_complex_type (type),
13770 result_c, result_s);
13772 /* Dereference the sin/cos pointer arguments. */
13773 arg_sinp = build_fold_indirect_ref (arg_sinp);
13774 arg_cosp = build_fold_indirect_ref (arg_cosp);
13775 /* Proceed if valid pointer type were passed in. */
13776 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13777 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13779 /* Set the values. */
13780 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13782 TREE_SIDE_EFFECTS (result_s) = 1;
13783 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13785 TREE_SIDE_EFFECTS (result_c) = 1;
13786 /* Combine the assignments into a compound expr. */
13787 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13788 result_s, result_c));
13796 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13797 two-argument mpfr order N Bessel function FUNC on them and return
13798 the resulting value as a tree with type TYPE. The mpfr precision
13799 is set to the precision of TYPE. We assume that function FUNC
13800 returns zero if the result could be calculated exactly within the
13801 requested precision. */
13803 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13804 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13805 const REAL_VALUE_TYPE *min, bool inclusive)
13807 tree result = NULL_TREE;
13812 /* To proceed, MPFR must exactly represent the target floating point
13813 format, which only happens when the target base equals two. */
13814 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13815 && host_integerp (arg1, 0)
13816 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13818 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13819 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13822 && real_isfinite (ra)
13823 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13825 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13826 const int prec = fmt->p;
13827 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13831 mpfr_init2 (m, prec);
13832 mpfr_from_real (m, ra, GMP_RNDN);
13833 mpfr_clear_flags ();
13834 inexact = func (m, n, m, rnd);
13835 result = do_mpfr_ckconv (m, type, inexact);
13843 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13844 the pointer *(ARG_QUO) and return the result. The type is taken
13845 from the type of ARG0 and is used for setting the precision of the
13846 calculation and results. */
13849 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13851 tree const type = TREE_TYPE (arg0);
13852 tree result = NULL_TREE;
13857 /* To proceed, MPFR must exactly represent the target floating point
13858 format, which only happens when the target base equals two. */
13859 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13860 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13861 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13863 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13864 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13866 if (real_isfinite (ra0) && real_isfinite (ra1))
13868 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13869 const int prec = fmt->p;
13870 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13875 mpfr_inits2 (prec, m0, m1, NULL);
13876 mpfr_from_real (m0, ra0, GMP_RNDN);
13877 mpfr_from_real (m1, ra1, GMP_RNDN);
13878 mpfr_clear_flags ();
13879 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13880 /* Remquo is independent of the rounding mode, so pass
13881 inexact=0 to do_mpfr_ckconv(). */
13882 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13883 mpfr_clears (m0, m1, NULL);
13886 /* MPFR calculates quo in the host's long so it may
13887 return more bits in quo than the target int can hold
13888 if sizeof(host long) > sizeof(target int). This can
13889 happen even for native compilers in LP64 mode. In
13890 these cases, modulo the quo value with the largest
13891 number that the target int can hold while leaving one
13892 bit for the sign. */
13893 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13894 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13896 /* Dereference the quo pointer argument. */
13897 arg_quo = build_fold_indirect_ref (arg_quo);
13898 /* Proceed iff a valid pointer type was passed in. */
13899 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13901 /* Set the value. */
13903 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13904 build_int_cst (TREE_TYPE (arg_quo),
13906 TREE_SIDE_EFFECTS (result_quo) = 1;
13907 /* Combine the quo assignment with the rem. */
13908 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13909 result_quo, result_rem));
13917 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13918 resulting value as a tree with type TYPE. The mpfr precision is
13919 set to the precision of TYPE. We assume that this mpfr function
13920 returns zero if the result could be calculated exactly within the
13921 requested precision. In addition, the integer pointer represented
13922 by ARG_SG will be dereferenced and set to the appropriate signgam
13926 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13928 tree result = NULL_TREE;
13932 /* To proceed, MPFR must exactly represent the target floating point
13933 format, which only happens when the target base equals two. Also
13934 verify ARG is a constant and that ARG_SG is an int pointer. */
13935 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13936 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13937 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13938 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13940 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13942 /* In addition to NaN and Inf, the argument cannot be zero or a
13943 negative integer. */
13944 if (real_isfinite (ra)
13945 && ra->cl != rvc_zero
13946 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13948 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13949 const int prec = fmt->p;
13950 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13955 mpfr_init2 (m, prec);
13956 mpfr_from_real (m, ra, GMP_RNDN);
13957 mpfr_clear_flags ();
13958 inexact = mpfr_lgamma (m, &sg, m, rnd);
13959 result_lg = do_mpfr_ckconv (m, type, inexact);
13965 /* Dereference the arg_sg pointer argument. */
13966 arg_sg = build_fold_indirect_ref (arg_sg);
13967 /* Assign the signgam value into *arg_sg. */
13968 result_sg = fold_build2 (MODIFY_EXPR,
13969 TREE_TYPE (arg_sg), arg_sg,
13970 build_int_cst (TREE_TYPE (arg_sg), sg));
13971 TREE_SIDE_EFFECTS (result_sg) = 1;
13972 /* Combine the signgam assignment with the lgamma result. */
13973 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13974 result_sg, result_lg));
13982 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13983 function FUNC on it and return the resulting value as a tree with
13984 type TYPE. The mpfr precision is set to the precision of TYPE. We
13985 assume that function FUNC returns zero if the result could be
13986 calculated exactly within the requested precision. */
13989 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13991 tree result = NULL_TREE;
13995 /* To proceed, MPFR must exactly represent the target floating point
13996 format, which only happens when the target base equals two. */
13997 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13998 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13999 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
14001 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
14002 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
14004 if (real_isfinite (re) && real_isfinite (im))
14006 const struct real_format *const fmt =
14007 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14008 const int prec = fmt->p;
14009 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14010 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14014 mpc_init2 (m, prec);
14015 mpfr_from_real (mpc_realref(m), re, rnd);
14016 mpfr_from_real (mpc_imagref(m), im, rnd);
14017 mpfr_clear_flags ();
14018 inexact = func (m, m, crnd);
14019 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
14027 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14028 mpc function FUNC on it and return the resulting value as a tree
14029 with type TYPE. The mpfr precision is set to the precision of
14030 TYPE. We assume that function FUNC returns zero if the result
14031 could be calculated exactly within the requested precision. If
14032 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14033 in the arguments and/or results. */
14036 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
14037 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
14039 tree result = NULL_TREE;
14044 /* To proceed, MPFR must exactly represent the target floating point
14045 format, which only happens when the target base equals two. */
14046 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
14047 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
14048 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
14049 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
14050 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
14052 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
14053 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
14054 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
14055 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
14058 || (real_isfinite (re0) && real_isfinite (im0)
14059 && real_isfinite (re1) && real_isfinite (im1)))
14061 const struct real_format *const fmt =
14062 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
14063 const int prec = fmt->p;
14064 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
14065 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
14069 mpc_init2 (m0, prec);
14070 mpc_init2 (m1, prec);
14071 mpfr_from_real (mpc_realref(m0), re0, rnd);
14072 mpfr_from_real (mpc_imagref(m0), im0, rnd);
14073 mpfr_from_real (mpc_realref(m1), re1, rnd);
14074 mpfr_from_real (mpc_imagref(m1), im1, rnd);
14075 mpfr_clear_flags ();
14076 inexact = func (m0, m0, m1, crnd);
14077 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
14086 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14087 a normal call should be emitted rather than expanding the function
14088 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14091 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
14093 int nargs = gimple_call_num_args (stmt);
14095 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
14097 ? gimple_call_arg_ptr (stmt, 0)
14098 : &error_mark_node), fcode);
14101 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14102 a normal call should be emitted rather than expanding the function
14103 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14104 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14105 passed as second argument. */
14108 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
14109 enum built_in_function fcode)
14111 int nargs = gimple_call_num_args (stmt);
14113 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
14115 ? gimple_call_arg_ptr (stmt, 0)
14116 : &error_mark_node), maxlen, fcode);
14119 /* Builtins with folding operations that operate on "..." arguments
14120 need special handling; we need to store the arguments in a convenient
14121 data structure before attempting any folding. Fortunately there are
14122 only a few builtins that fall into this category. FNDECL is the
14123 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14124 result of the function call is ignored. */
14127 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
14128 bool ignore ATTRIBUTE_UNUSED)
14130 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
14131 tree ret = NULL_TREE;
14135 case BUILT_IN_SPRINTF_CHK:
14136 case BUILT_IN_VSPRINTF_CHK:
14137 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
14140 case BUILT_IN_SNPRINTF_CHK:
14141 case BUILT_IN_VSNPRINTF_CHK:
14142 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
14149 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
14150 TREE_NO_WARNING (ret) = 1;
14156 /* A wrapper function for builtin folding that prevents warnings for
14157 "statement without effect" and the like, caused by removing the
14158 call node earlier than the warning is generated. */
14161 fold_call_stmt (gimple stmt, bool ignore)
14163 tree ret = NULL_TREE;
14164 tree fndecl = gimple_call_fndecl (stmt);
14165 location_t loc = gimple_location (stmt);
14167 && TREE_CODE (fndecl) == FUNCTION_DECL
14168 && DECL_BUILT_IN (fndecl)
14169 && !gimple_call_va_arg_pack_p (stmt))
14171 int nargs = gimple_call_num_args (stmt);
14172 tree *args = (nargs > 0
14173 ? gimple_call_arg_ptr (stmt, 0)
14174 : &error_mark_node);
14176 if (avoid_folding_inline_builtin (fndecl))
14178 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
14180 return targetm.fold_builtin (fndecl, nargs, args, ignore);
14184 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
14185 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
14187 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
14190 /* Propagate location information from original call to
14191 expansion of builtin. Otherwise things like
14192 maybe_emit_chk_warning, that operate on the expansion
14193 of a builtin, will use the wrong location information. */
14194 if (gimple_has_location (stmt))
14196 tree realret = ret;
14197 if (TREE_CODE (ret) == NOP_EXPR)
14198 realret = TREE_OPERAND (ret, 0);
14199 if (CAN_HAVE_LOCATION_P (realret)
14200 && !EXPR_HAS_LOCATION (realret))
14201 SET_EXPR_LOCATION (realret, loc);
14211 /* Look up the function in builtin_decl that corresponds to DECL
14212 and set ASMSPEC as its user assembler name. DECL must be a
14213 function decl that declares a builtin. */
14216 set_builtin_user_assembler_name (tree decl, const char *asmspec)
14219 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
14220 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
14223 builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
14224 set_user_assembler_name (builtin, asmspec);
14225 switch (DECL_FUNCTION_CODE (decl))
14227 case BUILT_IN_MEMCPY:
14228 init_block_move_fn (asmspec);
14229 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
14231 case BUILT_IN_MEMSET:
14232 init_block_clear_fn (asmspec);
14233 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
14235 case BUILT_IN_MEMMOVE:
14236 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
14238 case BUILT_IN_MEMCMP:
14239 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
14241 case BUILT_IN_ABORT:
14242 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
14245 if (INT_TYPE_SIZE < BITS_PER_WORD)
14247 set_user_assembler_libfunc ("ffs", asmspec);
14248 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
14249 MODE_INT, 0), "ffs");
14257 /* Return true if DECL is a builtin that expands to a constant or similarly
14260 is_simple_builtin (tree decl)
14262 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14263 switch (DECL_FUNCTION_CODE (decl))
14265 /* Builtins that expand to constants. */
14266 case BUILT_IN_CONSTANT_P:
14267 case BUILT_IN_EXPECT:
14268 case BUILT_IN_OBJECT_SIZE:
14269 case BUILT_IN_UNREACHABLE:
14270 /* Simple register moves or loads from stack. */
14271 case BUILT_IN_ASSUME_ALIGNED:
14272 case BUILT_IN_RETURN_ADDRESS:
14273 case BUILT_IN_EXTRACT_RETURN_ADDR:
14274 case BUILT_IN_FROB_RETURN_ADDR:
14275 case BUILT_IN_RETURN:
14276 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
14277 case BUILT_IN_FRAME_ADDRESS:
14278 case BUILT_IN_VA_END:
14279 case BUILT_IN_STACK_SAVE:
14280 case BUILT_IN_STACK_RESTORE:
14281 /* Exception state returns or moves registers around. */
14282 case BUILT_IN_EH_FILTER:
14283 case BUILT_IN_EH_POINTER:
14284 case BUILT_IN_EH_COPY_VALUES:
14294 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14295 most probably expanded inline into reasonably simple code. This is a
14296 superset of is_simple_builtin. */
14298 is_inexpensive_builtin (tree decl)
14302 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
14304 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
14305 switch (DECL_FUNCTION_CODE (decl))
14308 case BUILT_IN_ALLOCA:
14309 case BUILT_IN_ALLOCA_WITH_ALIGN:
14310 case BUILT_IN_BSWAP32:
14311 case BUILT_IN_BSWAP64:
14313 case BUILT_IN_CLZIMAX:
14314 case BUILT_IN_CLZL:
14315 case BUILT_IN_CLZLL:
14317 case BUILT_IN_CTZIMAX:
14318 case BUILT_IN_CTZL:
14319 case BUILT_IN_CTZLL:
14321 case BUILT_IN_FFSIMAX:
14322 case BUILT_IN_FFSL:
14323 case BUILT_IN_FFSLL:
14324 case BUILT_IN_IMAXABS:
14325 case BUILT_IN_FINITE:
14326 case BUILT_IN_FINITEF:
14327 case BUILT_IN_FINITEL:
14328 case BUILT_IN_FINITED32:
14329 case BUILT_IN_FINITED64:
14330 case BUILT_IN_FINITED128:
14331 case BUILT_IN_FPCLASSIFY:
14332 case BUILT_IN_ISFINITE:
14333 case BUILT_IN_ISINF_SIGN:
14334 case BUILT_IN_ISINF:
14335 case BUILT_IN_ISINFF:
14336 case BUILT_IN_ISINFL:
14337 case BUILT_IN_ISINFD32:
14338 case BUILT_IN_ISINFD64:
14339 case BUILT_IN_ISINFD128:
14340 case BUILT_IN_ISNAN:
14341 case BUILT_IN_ISNANF:
14342 case BUILT_IN_ISNANL:
14343 case BUILT_IN_ISNAND32:
14344 case BUILT_IN_ISNAND64:
14345 case BUILT_IN_ISNAND128:
14346 case BUILT_IN_ISNORMAL:
14347 case BUILT_IN_ISGREATER:
14348 case BUILT_IN_ISGREATEREQUAL:
14349 case BUILT_IN_ISLESS:
14350 case BUILT_IN_ISLESSEQUAL:
14351 case BUILT_IN_ISLESSGREATER:
14352 case BUILT_IN_ISUNORDERED:
14353 case BUILT_IN_VA_ARG_PACK:
14354 case BUILT_IN_VA_ARG_PACK_LEN:
14355 case BUILT_IN_VA_COPY:
14356 case BUILT_IN_TRAP:
14357 case BUILT_IN_SAVEREGS:
14358 case BUILT_IN_POPCOUNTL:
14359 case BUILT_IN_POPCOUNTLL:
14360 case BUILT_IN_POPCOUNTIMAX:
14361 case BUILT_IN_POPCOUNT:
14362 case BUILT_IN_PARITYL:
14363 case BUILT_IN_PARITYLL:
14364 case BUILT_IN_PARITYIMAX:
14365 case BUILT_IN_PARITY:
14366 case BUILT_IN_LABS:
14367 case BUILT_IN_LLABS:
14368 case BUILT_IN_PREFETCH:
14372 return is_simple_builtin (decl);