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 tree built_in_decls[(int) END_BUILTINS];
79 /* Declarations used when constructing the builtin implicitly in the compiler.
80 It may be NULL_TREE when this is invalid (for instance runtime is not
81 required to implement the function call in all cases). */
82 tree implicit_built_in_decls[(int) END_BUILTINS];
84 static const char *c_getstr (tree);
85 static rtx c_readstr (const char *, enum machine_mode);
86 static int target_char_cast (tree, char *);
87 static rtx get_memory_rtx (tree, tree);
88 static int apply_args_size (void);
89 static int apply_result_size (void);
90 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
91 static rtx result_vector (int, rtx);
93 static void expand_builtin_update_setjmp_buf (rtx);
94 static void expand_builtin_prefetch (tree);
95 static rtx expand_builtin_apply_args (void);
96 static rtx expand_builtin_apply_args_1 (void);
97 static rtx expand_builtin_apply (rtx, rtx, rtx);
98 static void expand_builtin_return (rtx);
99 static enum type_class type_to_class (tree);
100 static rtx expand_builtin_classify_type (tree);
101 static void expand_errno_check (tree, rtx);
102 static rtx expand_builtin_mathfn (tree, rtx, rtx);
103 static rtx expand_builtin_mathfn_2 (tree, rtx, rtx);
104 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
105 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
106 static rtx expand_builtin_interclass_mathfn (tree, rtx);
107 static rtx expand_builtin_sincos (tree);
108 static rtx expand_builtin_cexpi (tree, rtx);
109 static rtx expand_builtin_int_roundingfn (tree, rtx);
110 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
111 static rtx expand_builtin_next_arg (void);
112 static rtx expand_builtin_va_start (tree);
113 static rtx expand_builtin_va_end (tree);
114 static rtx expand_builtin_va_copy (tree);
115 static rtx expand_builtin_memcmp (tree, rtx, enum machine_mode);
116 static rtx expand_builtin_strcmp (tree, rtx);
117 static rtx expand_builtin_strncmp (tree, rtx, enum machine_mode);
118 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, enum machine_mode);
119 static rtx expand_builtin_memcpy (tree, rtx);
120 static rtx expand_builtin_mempcpy (tree, rtx, enum machine_mode);
121 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx,
122 enum machine_mode, int);
123 static rtx expand_builtin_strcpy (tree, rtx);
124 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
125 static rtx expand_builtin_stpcpy (tree, rtx, enum machine_mode);
126 static rtx expand_builtin_strncpy (tree, rtx);
127 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, enum machine_mode);
128 static rtx expand_builtin_memset (tree, rtx, enum machine_mode);
129 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, enum machine_mode, tree);
130 static rtx expand_builtin_bzero (tree);
131 static rtx expand_builtin_strlen (tree, rtx, enum machine_mode);
132 static rtx expand_builtin_alloca (tree, bool);
133 static rtx expand_builtin_unop (enum machine_mode, tree, rtx, rtx, optab);
134 static rtx expand_builtin_frame_address (tree, tree);
135 static tree stabilize_va_list_loc (location_t, tree, int);
136 static rtx expand_builtin_expect (tree, rtx);
137 static tree fold_builtin_constant_p (tree);
138 static tree fold_builtin_expect (location_t, tree, tree);
139 static tree fold_builtin_classify_type (tree);
140 static tree fold_builtin_strlen (location_t, tree, tree);
141 static tree fold_builtin_inf (location_t, tree, int);
142 static tree fold_builtin_nan (tree, tree, int);
143 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
144 static bool validate_arg (const_tree, enum tree_code code);
145 static bool integer_valued_real_p (tree);
146 static tree fold_trunc_transparent_mathfn (location_t, tree, tree);
147 static bool readonly_data_expr (tree);
148 static rtx expand_builtin_fabs (tree, rtx, rtx);
149 static rtx expand_builtin_signbit (tree, rtx);
150 static tree fold_builtin_sqrt (location_t, tree, tree);
151 static tree fold_builtin_cbrt (location_t, tree, tree);
152 static tree fold_builtin_pow (location_t, tree, tree, tree, tree);
153 static tree fold_builtin_powi (location_t, tree, tree, tree, tree);
154 static tree fold_builtin_cos (location_t, tree, tree, tree);
155 static tree fold_builtin_cosh (location_t, tree, tree, tree);
156 static tree fold_builtin_tan (tree, tree);
157 static tree fold_builtin_trunc (location_t, tree, tree);
158 static tree fold_builtin_floor (location_t, tree, tree);
159 static tree fold_builtin_ceil (location_t, tree, tree);
160 static tree fold_builtin_round (location_t, tree, tree);
161 static tree fold_builtin_int_roundingfn (location_t, tree, tree);
162 static tree fold_builtin_bitop (tree, tree);
163 static tree fold_builtin_memory_op (location_t, tree, tree, tree, tree, bool, int);
164 static tree fold_builtin_strchr (location_t, tree, tree, tree);
165 static tree fold_builtin_memchr (location_t, tree, tree, tree, tree);
166 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
167 static tree fold_builtin_strcmp (location_t, tree, tree);
168 static tree fold_builtin_strncmp (location_t, tree, tree, tree);
169 static tree fold_builtin_signbit (location_t, tree, tree);
170 static tree fold_builtin_copysign (location_t, tree, tree, tree, tree);
171 static tree fold_builtin_isascii (location_t, tree);
172 static tree fold_builtin_toascii (location_t, tree);
173 static tree fold_builtin_isdigit (location_t, tree);
174 static tree fold_builtin_fabs (location_t, tree, tree);
175 static tree fold_builtin_abs (location_t, tree, tree);
176 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
178 static tree fold_builtin_n (location_t, tree, tree *, int, bool);
179 static tree fold_builtin_0 (location_t, tree, bool);
180 static tree fold_builtin_1 (location_t, tree, tree, bool);
181 static tree fold_builtin_2 (location_t, tree, tree, tree, bool);
182 static tree fold_builtin_3 (location_t, tree, tree, tree, tree, bool);
183 static tree fold_builtin_4 (location_t, tree, tree, tree, tree, tree, bool);
184 static tree fold_builtin_varargs (location_t, tree, tree, bool);
186 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
187 static tree fold_builtin_strstr (location_t, tree, tree, tree);
188 static tree fold_builtin_strrchr (location_t, tree, tree, tree);
189 static tree fold_builtin_strcat (location_t, tree, tree);
190 static tree fold_builtin_strncat (location_t, tree, tree, tree);
191 static tree fold_builtin_strspn (location_t, tree, tree);
192 static tree fold_builtin_strcspn (location_t, tree, tree);
193 static tree fold_builtin_sprintf (location_t, tree, tree, tree, int);
194 static tree fold_builtin_snprintf (location_t, tree, tree, tree, tree, int);
196 static rtx expand_builtin_object_size (tree);
197 static rtx expand_builtin_memory_chk (tree, rtx, enum machine_mode,
198 enum built_in_function);
199 static void maybe_emit_chk_warning (tree, enum built_in_function);
200 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
201 static void maybe_emit_free_warning (tree);
202 static tree fold_builtin_object_size (tree, tree);
203 static tree fold_builtin_strcat_chk (location_t, tree, tree, tree, tree);
204 static tree fold_builtin_strncat_chk (location_t, tree, tree, tree, tree, tree);
205 static tree fold_builtin_sprintf_chk (location_t, tree, enum built_in_function);
206 static tree fold_builtin_printf (location_t, tree, tree, tree, bool, enum built_in_function);
207 static tree fold_builtin_fprintf (location_t, tree, tree, tree, tree, bool,
208 enum built_in_function);
209 static bool init_target_chars (void);
211 static unsigned HOST_WIDE_INT target_newline;
212 static unsigned HOST_WIDE_INT target_percent;
213 static unsigned HOST_WIDE_INT target_c;
214 static unsigned HOST_WIDE_INT target_s;
215 static char target_percent_c[3];
216 static char target_percent_s[3];
217 static char target_percent_s_newline[4];
218 static tree do_mpfr_arg1 (tree, tree, int (*)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
219 const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, bool);
220 static tree do_mpfr_arg2 (tree, tree, tree,
221 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
222 static tree do_mpfr_arg3 (tree, tree, tree, tree,
223 int (*)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t));
224 static tree do_mpfr_sincos (tree, tree, tree);
225 static tree do_mpfr_bessel_n (tree, tree, tree,
226 int (*)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
227 const REAL_VALUE_TYPE *, bool);
228 static tree do_mpfr_remquo (tree, tree, tree);
229 static tree do_mpfr_lgamma_r (tree, tree, tree);
231 /* Return true if NAME starts with __builtin_ or __sync_. */
234 is_builtin_name (const char *name)
236 if (strncmp (name, "__builtin_", 10) == 0)
238 if (strncmp (name, "__sync_", 7) == 0)
244 /* Return true if DECL is a function symbol representing a built-in. */
247 is_builtin_fn (tree decl)
249 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
253 /* Return true if NODE should be considered for inline expansion regardless
254 of the optimization level. This means whenever a function is invoked with
255 its "internal" name, which normally contains the prefix "__builtin". */
258 called_as_built_in (tree node)
260 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
261 we want the name used to call the function, not the name it
263 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
264 return is_builtin_name (name);
267 /* Compute values M and N such that M divides (address of EXP - N) and
268 such that N < M. Store N in *BITPOSP and return M.
270 Note that the address (and thus the alignment) computed here is based
271 on the address to which a symbol resolves, whereas DECL_ALIGN is based
272 on the address at which an object is actually located. These two
273 addresses are not always the same. For example, on ARM targets,
274 the address &foo of a Thumb function foo() has the lowest bit set,
275 whereas foo() itself starts on an even address. */
278 get_object_alignment_1 (tree exp, unsigned HOST_WIDE_INT *bitposp)
280 HOST_WIDE_INT bitsize, bitpos;
282 enum machine_mode mode;
283 int unsignedp, volatilep;
284 unsigned int align, inner;
286 /* Get the innermost object and the constant (bitpos) and possibly
287 variable (offset) offset of the access. */
288 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset,
289 &mode, &unsignedp, &volatilep, true);
291 /* Extract alignment information from the innermost object and
292 possibly adjust bitpos and offset. */
293 if (TREE_CODE (exp) == CONST_DECL)
294 exp = DECL_INITIAL (exp);
296 && TREE_CODE (exp) != LABEL_DECL)
298 if (TREE_CODE (exp) == FUNCTION_DECL)
300 /* Function addresses can encode extra information besides their
301 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
302 allows the low bit to be used as a virtual bit, we know
303 that the address itself must be 2-byte aligned. */
304 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
305 align = 2 * BITS_PER_UNIT;
307 align = BITS_PER_UNIT;
310 align = DECL_ALIGN (exp);
312 else if (CONSTANT_CLASS_P (exp))
314 align = TYPE_ALIGN (TREE_TYPE (exp));
315 #ifdef CONSTANT_ALIGNMENT
316 align = (unsigned)CONSTANT_ALIGNMENT (exp, align);
319 else if (TREE_CODE (exp) == VIEW_CONVERT_EXPR)
320 align = TYPE_ALIGN (TREE_TYPE (exp));
321 else if (TREE_CODE (exp) == INDIRECT_REF)
322 align = TYPE_ALIGN (TREE_TYPE (exp));
323 else if (TREE_CODE (exp) == MEM_REF)
325 tree addr = TREE_OPERAND (exp, 0);
326 struct ptr_info_def *pi;
327 if (TREE_CODE (addr) == BIT_AND_EXPR
328 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
330 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
331 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
332 align *= BITS_PER_UNIT;
333 addr = TREE_OPERAND (addr, 0);
336 align = BITS_PER_UNIT;
337 if (TREE_CODE (addr) == SSA_NAME
338 && (pi = SSA_NAME_PTR_INFO (addr)))
340 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
341 align = MAX (pi->align * BITS_PER_UNIT, align);
343 else if (TREE_CODE (addr) == ADDR_EXPR)
344 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0)));
345 bitpos += mem_ref_offset (exp).low * BITS_PER_UNIT;
347 else if (TREE_CODE (exp) == TARGET_MEM_REF)
349 struct ptr_info_def *pi;
350 tree addr = TMR_BASE (exp);
351 if (TREE_CODE (addr) == BIT_AND_EXPR
352 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
354 align = (TREE_INT_CST_LOW (TREE_OPERAND (addr, 1))
355 & -TREE_INT_CST_LOW (TREE_OPERAND (addr, 1)));
356 align *= BITS_PER_UNIT;
357 addr = TREE_OPERAND (addr, 0);
360 align = BITS_PER_UNIT;
361 if (TREE_CODE (addr) == SSA_NAME
362 && (pi = SSA_NAME_PTR_INFO (addr)))
364 bitpos += (pi->misalign * BITS_PER_UNIT) & ~(align - 1);
365 align = MAX (pi->align * BITS_PER_UNIT, align);
367 else if (TREE_CODE (addr) == ADDR_EXPR)
368 align = MAX (align, get_object_alignment (TREE_OPERAND (addr, 0)));
369 if (TMR_OFFSET (exp))
370 bitpos += TREE_INT_CST_LOW (TMR_OFFSET (exp)) * BITS_PER_UNIT;
371 if (TMR_INDEX (exp) && TMR_STEP (exp))
373 unsigned HOST_WIDE_INT step = TREE_INT_CST_LOW (TMR_STEP (exp));
374 align = MIN (align, (step & -step) * BITS_PER_UNIT);
376 else if (TMR_INDEX (exp))
377 align = BITS_PER_UNIT;
378 if (TMR_INDEX2 (exp))
379 align = BITS_PER_UNIT;
382 align = BITS_PER_UNIT;
384 /* If there is a non-constant offset part extract the maximum
385 alignment that can prevail. */
391 if (TREE_CODE (offset) == PLUS_EXPR)
393 next_offset = TREE_OPERAND (offset, 0);
394 offset = TREE_OPERAND (offset, 1);
398 if (host_integerp (offset, 1))
400 /* Any overflow in calculating offset_bits won't change
403 = ((unsigned) tree_low_cst (offset, 1) * BITS_PER_UNIT);
406 inner = MIN (inner, (offset_bits & -offset_bits));
408 else if (TREE_CODE (offset) == MULT_EXPR
409 && host_integerp (TREE_OPERAND (offset, 1), 1))
411 /* Any overflow in calculating offset_factor won't change
413 unsigned offset_factor
414 = ((unsigned) tree_low_cst (TREE_OPERAND (offset, 1), 1)
418 inner = MIN (inner, (offset_factor & -offset_factor));
422 inner = MIN (inner, BITS_PER_UNIT);
425 offset = next_offset;
428 /* Alignment is innermost object alignment adjusted by the constant
429 and non-constant offset parts. */
430 align = MIN (align, inner);
431 bitpos = bitpos & (align - 1);
437 /* Return the alignment in bits of EXP, an object. */
440 get_object_alignment (tree exp)
442 unsigned HOST_WIDE_INT bitpos = 0;
445 align = get_object_alignment_1 (exp, &bitpos);
447 /* align and bitpos now specify known low bits of the pointer.
448 ptr & (align - 1) == bitpos. */
451 align = (bitpos & -bitpos);
456 /* Return the alignment in bits of EXP, a pointer valued expression.
457 The alignment returned is, by default, the alignment of the thing that
458 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
460 Otherwise, look at the expression to see if we can do better, i.e., if the
461 expression is actually pointing at an object whose alignment is tighter. */
464 get_pointer_alignment (tree exp)
468 if (TREE_CODE (exp) == ADDR_EXPR)
469 return get_object_alignment (TREE_OPERAND (exp, 0));
470 else if (TREE_CODE (exp) == SSA_NAME
471 && POINTER_TYPE_P (TREE_TYPE (exp)))
473 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
476 return BITS_PER_UNIT;
477 if (pi->misalign != 0)
478 align = (pi->misalign & -pi->misalign);
481 return align * BITS_PER_UNIT;
484 return POINTER_TYPE_P (TREE_TYPE (exp)) ? BITS_PER_UNIT : 0;
487 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
488 way, because it could contain a zero byte in the middle.
489 TREE_STRING_LENGTH is the size of the character array, not the string.
491 ONLY_VALUE should be nonzero if the result is not going to be emitted
492 into the instruction stream and zero if it is going to be expanded.
493 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
494 is returned, otherwise NULL, since
495 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
496 evaluate the side-effects.
498 The value returned is of type `ssizetype'.
500 Unfortunately, string_constant can't access the values of const char
501 arrays with initializers, so neither can we do so here. */
504 c_strlen (tree src, int only_value)
507 HOST_WIDE_INT offset;
513 if (TREE_CODE (src) == COND_EXPR
514 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
518 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
519 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
520 if (tree_int_cst_equal (len1, len2))
524 if (TREE_CODE (src) == COMPOUND_EXPR
525 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
526 return c_strlen (TREE_OPERAND (src, 1), only_value);
528 loc = EXPR_LOC_OR_HERE (src);
530 src = string_constant (src, &offset_node);
534 max = TREE_STRING_LENGTH (src) - 1;
535 ptr = TREE_STRING_POINTER (src);
537 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
539 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
540 compute the offset to the following null if we don't know where to
541 start searching for it. */
544 for (i = 0; i < max; i++)
548 /* We don't know the starting offset, but we do know that the string
549 has no internal zero bytes. We can assume that the offset falls
550 within the bounds of the string; otherwise, the programmer deserves
551 what he gets. Subtract the offset from the length of the string,
552 and return that. This would perhaps not be valid if we were dealing
553 with named arrays in addition to literal string constants. */
555 return size_diffop_loc (loc, size_int (max), offset_node);
558 /* We have a known offset into the string. Start searching there for
559 a null character if we can represent it as a single HOST_WIDE_INT. */
560 if (offset_node == 0)
562 else if (! host_integerp (offset_node, 0))
565 offset = tree_low_cst (offset_node, 0);
567 /* If the offset is known to be out of bounds, warn, and call strlen at
569 if (offset < 0 || offset > max)
571 /* Suppress multiple warnings for propagated constant strings. */
572 if (! TREE_NO_WARNING (src))
574 warning_at (loc, 0, "offset outside bounds of constant string");
575 TREE_NO_WARNING (src) = 1;
580 /* Use strlen to search for the first zero byte. Since any strings
581 constructed with build_string will have nulls appended, we win even
582 if we get handed something like (char[4])"abcd".
584 Since OFFSET is our starting index into the string, no further
585 calculation is needed. */
586 return ssize_int (strlen (ptr + offset));
589 /* Return a char pointer for a C string if it is a string constant
590 or sum of string constant and integer constant. */
597 src = string_constant (src, &offset_node);
601 if (offset_node == 0)
602 return TREE_STRING_POINTER (src);
603 else if (!host_integerp (offset_node, 1)
604 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
607 return TREE_STRING_POINTER (src) + tree_low_cst (offset_node, 1);
610 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
611 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
614 c_readstr (const char *str, enum machine_mode mode)
620 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
625 for (i = 0; i < GET_MODE_SIZE (mode); i++)
628 if (WORDS_BIG_ENDIAN)
629 j = GET_MODE_SIZE (mode) - i - 1;
630 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
631 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
632 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
634 gcc_assert (j < 2 * HOST_BITS_PER_WIDE_INT);
637 ch = (unsigned char) str[i];
638 c[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
640 return immed_double_const (c[0], c[1], mode);
643 /* Cast a target constant CST to target CHAR and if that value fits into
644 host char type, return zero and put that value into variable pointed to by
648 target_char_cast (tree cst, char *p)
650 unsigned HOST_WIDE_INT val, hostval;
652 if (TREE_CODE (cst) != INTEGER_CST
653 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
656 val = TREE_INT_CST_LOW (cst);
657 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
658 val &= (((unsigned HOST_WIDE_INT) 1) << CHAR_TYPE_SIZE) - 1;
661 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
662 hostval &= (((unsigned HOST_WIDE_INT) 1) << HOST_BITS_PER_CHAR) - 1;
671 /* Similar to save_expr, but assumes that arbitrary code is not executed
672 in between the multiple evaluations. In particular, we assume that a
673 non-addressable local variable will not be modified. */
676 builtin_save_expr (tree exp)
678 if (TREE_CODE (exp) == SSA_NAME
679 || (TREE_ADDRESSABLE (exp) == 0
680 && (TREE_CODE (exp) == PARM_DECL
681 || (TREE_CODE (exp) == VAR_DECL && !TREE_STATIC (exp)))))
684 return save_expr (exp);
687 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
688 times to get the address of either a higher stack frame, or a return
689 address located within it (depending on FNDECL_CODE). */
692 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
696 #ifdef INITIAL_FRAME_ADDRESS_RTX
697 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
701 /* For a zero count with __builtin_return_address, we don't care what
702 frame address we return, because target-specific definitions will
703 override us. Therefore frame pointer elimination is OK, and using
704 the soft frame pointer is OK.
706 For a nonzero count, or a zero count with __builtin_frame_address,
707 we require a stable offset from the current frame pointer to the
708 previous one, so we must use the hard frame pointer, and
709 we must disable frame pointer elimination. */
710 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
711 tem = frame_pointer_rtx;
714 tem = hard_frame_pointer_rtx;
716 /* Tell reload not to eliminate the frame pointer. */
717 crtl->accesses_prior_frames = 1;
721 /* Some machines need special handling before we can access
722 arbitrary frames. For example, on the SPARC, we must first flush
723 all register windows to the stack. */
724 #ifdef SETUP_FRAME_ADDRESSES
726 SETUP_FRAME_ADDRESSES ();
729 /* On the SPARC, the return address is not in the frame, it is in a
730 register. There is no way to access it off of the current frame
731 pointer, but it can be accessed off the previous frame pointer by
732 reading the value from the register window save area. */
733 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
734 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
738 /* Scan back COUNT frames to the specified frame. */
739 for (i = 0; i < count; i++)
741 /* Assume the dynamic chain pointer is in the word that the
742 frame address points to, unless otherwise specified. */
743 #ifdef DYNAMIC_CHAIN_ADDRESS
744 tem = DYNAMIC_CHAIN_ADDRESS (tem);
746 tem = memory_address (Pmode, tem);
747 tem = gen_frame_mem (Pmode, tem);
748 tem = copy_to_reg (tem);
751 /* For __builtin_frame_address, return what we've got. But, on
752 the SPARC for example, we may have to add a bias. */
753 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
754 #ifdef FRAME_ADDR_RTX
755 return FRAME_ADDR_RTX (tem);
760 /* For __builtin_return_address, get the return address from that frame. */
761 #ifdef RETURN_ADDR_RTX
762 tem = RETURN_ADDR_RTX (count, tem);
764 tem = memory_address (Pmode,
765 plus_constant (tem, GET_MODE_SIZE (Pmode)));
766 tem = gen_frame_mem (Pmode, tem);
771 /* Alias set used for setjmp buffer. */
772 static alias_set_type setjmp_alias_set = -1;
774 /* Construct the leading half of a __builtin_setjmp call. Control will
775 return to RECEIVER_LABEL. This is also called directly by the SJLJ
776 exception handling code. */
779 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
781 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
785 if (setjmp_alias_set == -1)
786 setjmp_alias_set = new_alias_set ();
788 buf_addr = convert_memory_address (Pmode, buf_addr);
790 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
792 /* We store the frame pointer and the address of receiver_label in
793 the buffer and use the rest of it for the stack save area, which
794 is machine-dependent. */
796 mem = gen_rtx_MEM (Pmode, buf_addr);
797 set_mem_alias_set (mem, setjmp_alias_set);
798 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
800 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
801 set_mem_alias_set (mem, setjmp_alias_set);
803 emit_move_insn (validize_mem (mem),
804 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
806 stack_save = gen_rtx_MEM (sa_mode,
807 plus_constant (buf_addr,
808 2 * GET_MODE_SIZE (Pmode)));
809 set_mem_alias_set (stack_save, setjmp_alias_set);
810 emit_stack_save (SAVE_NONLOCAL, &stack_save);
812 /* If there is further processing to do, do it. */
813 #ifdef HAVE_builtin_setjmp_setup
814 if (HAVE_builtin_setjmp_setup)
815 emit_insn (gen_builtin_setjmp_setup (buf_addr));
818 /* We have a nonlocal label. */
819 cfun->has_nonlocal_label = 1;
822 /* Construct the trailing part of a __builtin_setjmp call. This is
823 also called directly by the SJLJ exception handling code. */
826 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED)
830 /* Clobber the FP when we get here, so we have to make sure it's
831 marked as used by this function. */
832 emit_use (hard_frame_pointer_rtx);
834 /* Mark the static chain as clobbered here so life information
835 doesn't get messed up for it. */
836 chain = targetm.calls.static_chain (current_function_decl, true);
837 if (chain && REG_P (chain))
838 emit_clobber (chain);
840 /* Now put in the code to restore the frame pointer, and argument
841 pointer, if needed. */
842 #ifdef HAVE_nonlocal_goto
843 if (! HAVE_nonlocal_goto)
846 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
847 /* This might change the hard frame pointer in ways that aren't
848 apparent to early optimization passes, so force a clobber. */
849 emit_clobber (hard_frame_pointer_rtx);
852 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
853 if (fixed_regs[ARG_POINTER_REGNUM])
855 #ifdef ELIMINABLE_REGS
857 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
859 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
860 if (elim_regs[i].from == ARG_POINTER_REGNUM
861 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
864 if (i == ARRAY_SIZE (elim_regs))
867 /* Now restore our arg pointer from the address at which it
868 was saved in our stack frame. */
869 emit_move_insn (crtl->args.internal_arg_pointer,
870 copy_to_reg (get_arg_pointer_save_area ()));
875 #ifdef HAVE_builtin_setjmp_receiver
876 if (HAVE_builtin_setjmp_receiver)
877 emit_insn (gen_builtin_setjmp_receiver (receiver_label));
880 #ifdef HAVE_nonlocal_goto_receiver
881 if (HAVE_nonlocal_goto_receiver)
882 emit_insn (gen_nonlocal_goto_receiver ());
887 /* We must not allow the code we just generated to be reordered by
888 scheduling. Specifically, the update of the frame pointer must
889 happen immediately, not later. */
890 emit_insn (gen_blockage ());
893 /* __builtin_longjmp is passed a pointer to an array of five words (not
894 all will be used on all machines). It operates similarly to the C
895 library function of the same name, but is more efficient. Much of
896 the code below is copied from the handling of non-local gotos. */
899 expand_builtin_longjmp (rtx buf_addr, rtx value)
901 rtx fp, lab, stack, insn, last;
902 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
904 /* DRAP is needed for stack realign if longjmp is expanded to current
906 if (SUPPORTS_STACK_ALIGNMENT)
907 crtl->need_drap = true;
909 if (setjmp_alias_set == -1)
910 setjmp_alias_set = new_alias_set ();
912 buf_addr = convert_memory_address (Pmode, buf_addr);
914 buf_addr = force_reg (Pmode, buf_addr);
916 /* We require that the user must pass a second argument of 1, because
917 that is what builtin_setjmp will return. */
918 gcc_assert (value == const1_rtx);
920 last = get_last_insn ();
921 #ifdef HAVE_builtin_longjmp
922 if (HAVE_builtin_longjmp)
923 emit_insn (gen_builtin_longjmp (buf_addr));
927 fp = gen_rtx_MEM (Pmode, buf_addr);
928 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
929 GET_MODE_SIZE (Pmode)));
931 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
932 2 * GET_MODE_SIZE (Pmode)));
933 set_mem_alias_set (fp, setjmp_alias_set);
934 set_mem_alias_set (lab, setjmp_alias_set);
935 set_mem_alias_set (stack, setjmp_alias_set);
937 /* Pick up FP, label, and SP from the block and jump. This code is
938 from expand_goto in stmt.c; see there for detailed comments. */
939 #ifdef HAVE_nonlocal_goto
940 if (HAVE_nonlocal_goto)
941 /* We have to pass a value to the nonlocal_goto pattern that will
942 get copied into the static_chain pointer, but it does not matter
943 what that value is, because builtin_setjmp does not use it. */
944 emit_insn (gen_nonlocal_goto (value, lab, stack, fp));
948 lab = copy_to_reg (lab);
950 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
951 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
953 emit_move_insn (hard_frame_pointer_rtx, fp);
954 emit_stack_restore (SAVE_NONLOCAL, stack);
956 emit_use (hard_frame_pointer_rtx);
957 emit_use (stack_pointer_rtx);
958 emit_indirect_jump (lab);
962 /* Search backwards and mark the jump insn as a non-local goto.
963 Note that this precludes the use of __builtin_longjmp to a
964 __builtin_setjmp target in the same function. However, we've
965 already cautioned the user that these functions are for
966 internal exception handling use only. */
967 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
969 gcc_assert (insn != last);
973 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
976 else if (CALL_P (insn))
981 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
982 and the address of the save area. */
985 expand_builtin_nonlocal_goto (tree exp)
987 tree t_label, t_save_area;
988 rtx r_label, r_save_area, r_fp, r_sp, insn;
990 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
993 t_label = CALL_EXPR_ARG (exp, 0);
994 t_save_area = CALL_EXPR_ARG (exp, 1);
996 r_label = expand_normal (t_label);
997 r_label = convert_memory_address (Pmode, r_label);
998 r_save_area = expand_normal (t_save_area);
999 r_save_area = convert_memory_address (Pmode, r_save_area);
1000 /* Copy the address of the save location to a register just in case it was
1001 based on the frame pointer. */
1002 r_save_area = copy_to_reg (r_save_area);
1003 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1004 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1005 plus_constant (r_save_area, GET_MODE_SIZE (Pmode)));
1007 crtl->has_nonlocal_goto = 1;
1009 #ifdef HAVE_nonlocal_goto
1010 /* ??? We no longer need to pass the static chain value, afaik. */
1011 if (HAVE_nonlocal_goto)
1012 emit_insn (gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1016 r_label = copy_to_reg (r_label);
1018 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1019 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1021 /* Restore frame pointer for containing function. */
1022 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1023 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1025 /* USE of hard_frame_pointer_rtx added for consistency;
1026 not clear if really needed. */
1027 emit_use (hard_frame_pointer_rtx);
1028 emit_use (stack_pointer_rtx);
1030 /* If the architecture is using a GP register, we must
1031 conservatively assume that the target function makes use of it.
1032 The prologue of functions with nonlocal gotos must therefore
1033 initialize the GP register to the appropriate value, and we
1034 must then make sure that this value is live at the point
1035 of the jump. (Note that this doesn't necessarily apply
1036 to targets with a nonlocal_goto pattern; they are free
1037 to implement it in their own way. Note also that this is
1038 a no-op if the GP register is a global invariant.) */
1039 if ((unsigned) PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
1040 && fixed_regs[PIC_OFFSET_TABLE_REGNUM])
1041 emit_use (pic_offset_table_rtx);
1043 emit_indirect_jump (r_label);
1046 /* Search backwards to the jump insn and mark it as a
1048 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1052 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1055 else if (CALL_P (insn))
1062 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1063 (not all will be used on all machines) that was passed to __builtin_setjmp.
1064 It updates the stack pointer in that block to correspond to the current
1068 expand_builtin_update_setjmp_buf (rtx buf_addr)
1070 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1072 = gen_rtx_MEM (sa_mode,
1075 plus_constant (buf_addr, 2 * GET_MODE_SIZE (Pmode))));
1077 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1080 /* Expand a call to __builtin_prefetch. For a target that does not support
1081 data prefetch, evaluate the memory address argument in case it has side
1085 expand_builtin_prefetch (tree exp)
1087 tree arg0, arg1, arg2;
1091 if (!validate_arglist (exp, POINTER_TYPE, 0))
1094 arg0 = CALL_EXPR_ARG (exp, 0);
1096 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1097 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1099 nargs = call_expr_nargs (exp);
1101 arg1 = CALL_EXPR_ARG (exp, 1);
1103 arg1 = integer_zero_node;
1105 arg2 = CALL_EXPR_ARG (exp, 2);
1107 arg2 = integer_three_node;
1109 /* Argument 0 is an address. */
1110 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1112 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1113 if (TREE_CODE (arg1) != INTEGER_CST)
1115 error ("second argument to %<__builtin_prefetch%> must be a constant");
1116 arg1 = integer_zero_node;
1118 op1 = expand_normal (arg1);
1119 /* Argument 1 must be either zero or one. */
1120 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1122 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1127 /* Argument 2 (locality) must be a compile-time constant int. */
1128 if (TREE_CODE (arg2) != INTEGER_CST)
1130 error ("third argument to %<__builtin_prefetch%> must be a constant");
1131 arg2 = integer_zero_node;
1133 op2 = expand_normal (arg2);
1134 /* Argument 2 must be 0, 1, 2, or 3. */
1135 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1137 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1141 #ifdef HAVE_prefetch
1144 struct expand_operand ops[3];
1146 create_address_operand (&ops[0], op0);
1147 create_integer_operand (&ops[1], INTVAL (op1));
1148 create_integer_operand (&ops[2], INTVAL (op2));
1149 if (maybe_expand_insn (CODE_FOR_prefetch, 3, ops))
1154 /* Don't do anything with direct references to volatile memory, but
1155 generate code to handle other side effects. */
1156 if (!MEM_P (op0) && side_effects_p (op0))
1160 /* Get a MEM rtx for expression EXP which is the address of an operand
1161 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1162 the maximum length of the block of memory that might be accessed or
1166 get_memory_rtx (tree exp, tree len)
1168 tree orig_exp = exp;
1172 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1173 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1174 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1175 exp = TREE_OPERAND (exp, 0);
1177 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1178 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1180 /* Get an expression we can use to find the attributes to assign to MEM.
1181 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1182 we can. First remove any nops. */
1183 while (CONVERT_EXPR_P (exp)
1184 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1185 exp = TREE_OPERAND (exp, 0);
1188 if (TREE_CODE (exp) == POINTER_PLUS_EXPR
1189 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1190 && host_integerp (TREE_OPERAND (exp, 1), 0)
1191 && (off = tree_low_cst (TREE_OPERAND (exp, 1), 0)) > 0)
1192 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1193 else if (TREE_CODE (exp) == ADDR_EXPR)
1194 exp = TREE_OPERAND (exp, 0);
1195 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
1196 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
1200 /* Honor attributes derived from exp, except for the alias set
1201 (as builtin stringops may alias with anything) and the size
1202 (as stringops may access multiple array elements). */
1205 set_mem_attributes (mem, exp, 0);
1208 mem = adjust_automodify_address_nv (mem, BLKmode, NULL, off);
1210 /* Allow the string and memory builtins to overflow from one
1211 field into another, see http://gcc.gnu.org/PR23561.
1212 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1213 memory accessed by the string or memory builtin will fit
1214 within the field. */
1215 if (MEM_EXPR (mem) && TREE_CODE (MEM_EXPR (mem)) == COMPONENT_REF)
1217 tree mem_expr = MEM_EXPR (mem);
1218 HOST_WIDE_INT offset = -1, length = -1;
1221 while (TREE_CODE (inner) == ARRAY_REF
1222 || CONVERT_EXPR_P (inner)
1223 || TREE_CODE (inner) == VIEW_CONVERT_EXPR
1224 || TREE_CODE (inner) == SAVE_EXPR)
1225 inner = TREE_OPERAND (inner, 0);
1227 gcc_assert (TREE_CODE (inner) == COMPONENT_REF);
1229 if (MEM_OFFSET_KNOWN_P (mem))
1230 offset = MEM_OFFSET (mem);
1232 if (offset >= 0 && len && host_integerp (len, 0))
1233 length = tree_low_cst (len, 0);
1235 while (TREE_CODE (inner) == COMPONENT_REF)
1237 tree field = TREE_OPERAND (inner, 1);
1238 gcc_assert (TREE_CODE (mem_expr) == COMPONENT_REF);
1239 gcc_assert (field == TREE_OPERAND (mem_expr, 1));
1241 /* Bitfields are generally not byte-addressable. */
1242 gcc_assert (!DECL_BIT_FIELD (field)
1243 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1244 % BITS_PER_UNIT) == 0
1245 && host_integerp (DECL_SIZE (field), 0)
1246 && (TREE_INT_CST_LOW (DECL_SIZE (field))
1247 % BITS_PER_UNIT) == 0));
1249 /* If we can prove that the memory starting at XEXP (mem, 0) and
1250 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1251 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1252 fields without DECL_SIZE_UNIT like flexible array members. */
1254 && DECL_SIZE_UNIT (field)
1255 && host_integerp (DECL_SIZE_UNIT (field), 0))
1258 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field));
1261 && offset + length <= size)
1266 && host_integerp (DECL_FIELD_OFFSET (field), 0))
1267 offset += TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1268 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1276 mem_expr = TREE_OPERAND (mem_expr, 0);
1277 inner = TREE_OPERAND (inner, 0);
1280 if (mem_expr == NULL)
1282 if (mem_expr != MEM_EXPR (mem))
1284 set_mem_expr (mem, mem_expr);
1286 set_mem_offset (mem, offset);
1288 clear_mem_offset (mem);
1291 set_mem_alias_set (mem, 0);
1292 clear_mem_size (mem);
1298 /* Built-in functions to perform an untyped call and return. */
1300 #define apply_args_mode \
1301 (this_target_builtins->x_apply_args_mode)
1302 #define apply_result_mode \
1303 (this_target_builtins->x_apply_result_mode)
1305 /* Return the size required for the block returned by __builtin_apply_args,
1306 and initialize apply_args_mode. */
1309 apply_args_size (void)
1311 static int size = -1;
1314 enum machine_mode mode;
1316 /* The values computed by this function never change. */
1319 /* The first value is the incoming arg-pointer. */
1320 size = GET_MODE_SIZE (Pmode);
1322 /* The second value is the structure value address unless this is
1323 passed as an "invisible" first argument. */
1324 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1325 size += GET_MODE_SIZE (Pmode);
1327 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1328 if (FUNCTION_ARG_REGNO_P (regno))
1330 mode = targetm.calls.get_raw_arg_mode (regno);
1332 gcc_assert (mode != VOIDmode);
1334 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1335 if (size % align != 0)
1336 size = CEIL (size, align) * align;
1337 size += GET_MODE_SIZE (mode);
1338 apply_args_mode[regno] = mode;
1342 apply_args_mode[regno] = VOIDmode;
1348 /* Return the size required for the block returned by __builtin_apply,
1349 and initialize apply_result_mode. */
1352 apply_result_size (void)
1354 static int size = -1;
1356 enum machine_mode mode;
1358 /* The values computed by this function never change. */
1363 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1364 if (targetm.calls.function_value_regno_p (regno))
1366 mode = targetm.calls.get_raw_result_mode (regno);
1368 gcc_assert (mode != VOIDmode);
1370 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1371 if (size % align != 0)
1372 size = CEIL (size, align) * align;
1373 size += GET_MODE_SIZE (mode);
1374 apply_result_mode[regno] = mode;
1377 apply_result_mode[regno] = VOIDmode;
1379 /* Allow targets that use untyped_call and untyped_return to override
1380 the size so that machine-specific information can be stored here. */
1381 #ifdef APPLY_RESULT_SIZE
1382 size = APPLY_RESULT_SIZE;
1388 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1389 /* Create a vector describing the result block RESULT. If SAVEP is true,
1390 the result block is used to save the values; otherwise it is used to
1391 restore the values. */
1394 result_vector (int savep, rtx result)
1396 int regno, size, align, nelts;
1397 enum machine_mode mode;
1399 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1402 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1403 if ((mode = apply_result_mode[regno]) != VOIDmode)
1405 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1406 if (size % align != 0)
1407 size = CEIL (size, align) * align;
1408 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1409 mem = adjust_address (result, mode, size);
1410 savevec[nelts++] = (savep
1411 ? gen_rtx_SET (VOIDmode, mem, reg)
1412 : gen_rtx_SET (VOIDmode, reg, mem));
1413 size += GET_MODE_SIZE (mode);
1415 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1417 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1419 /* Save the state required to perform an untyped call with the same
1420 arguments as were passed to the current function. */
1423 expand_builtin_apply_args_1 (void)
1426 int size, align, regno;
1427 enum machine_mode mode;
1428 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1430 /* Create a block where the arg-pointer, structure value address,
1431 and argument registers can be saved. */
1432 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1434 /* Walk past the arg-pointer and structure value address. */
1435 size = GET_MODE_SIZE (Pmode);
1436 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1437 size += GET_MODE_SIZE (Pmode);
1439 /* Save each register used in calling a function to the block. */
1440 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1441 if ((mode = apply_args_mode[regno]) != VOIDmode)
1443 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1444 if (size % align != 0)
1445 size = CEIL (size, align) * align;
1447 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1449 emit_move_insn (adjust_address (registers, mode, size), tem);
1450 size += GET_MODE_SIZE (mode);
1453 /* Save the arg pointer to the block. */
1454 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1455 #ifdef STACK_GROWS_DOWNWARD
1456 /* We need the pointer as the caller actually passed them to us, not
1457 as we might have pretended they were passed. Make sure it's a valid
1458 operand, as emit_move_insn isn't expected to handle a PLUS. */
1460 = force_operand (plus_constant (tem, crtl->args.pretend_args_size),
1463 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1465 size = GET_MODE_SIZE (Pmode);
1467 /* Save the structure value address unless this is passed as an
1468 "invisible" first argument. */
1469 if (struct_incoming_value)
1471 emit_move_insn (adjust_address (registers, Pmode, size),
1472 copy_to_reg (struct_incoming_value));
1473 size += GET_MODE_SIZE (Pmode);
1476 /* Return the address of the block. */
1477 return copy_addr_to_reg (XEXP (registers, 0));
1480 /* __builtin_apply_args returns block of memory allocated on
1481 the stack into which is stored the arg pointer, structure
1482 value address, static chain, and all the registers that might
1483 possibly be used in performing a function call. The code is
1484 moved to the start of the function so the incoming values are
1488 expand_builtin_apply_args (void)
1490 /* Don't do __builtin_apply_args more than once in a function.
1491 Save the result of the first call and reuse it. */
1492 if (apply_args_value != 0)
1493 return apply_args_value;
1495 /* When this function is called, it means that registers must be
1496 saved on entry to this function. So we migrate the
1497 call to the first insn of this function. */
1502 temp = expand_builtin_apply_args_1 ();
1506 apply_args_value = temp;
1508 /* Put the insns after the NOTE that starts the function.
1509 If this is inside a start_sequence, make the outer-level insn
1510 chain current, so the code is placed at the start of the
1511 function. If internal_arg_pointer is a non-virtual pseudo,
1512 it needs to be placed after the function that initializes
1514 push_topmost_sequence ();
1515 if (REG_P (crtl->args.internal_arg_pointer)
1516 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1517 emit_insn_before (seq, parm_birth_insn);
1519 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1520 pop_topmost_sequence ();
1525 /* Perform an untyped call and save the state required to perform an
1526 untyped return of whatever value was returned by the given function. */
1529 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1531 int size, align, regno;
1532 enum machine_mode mode;
1533 rtx incoming_args, result, reg, dest, src, call_insn;
1534 rtx old_stack_level = 0;
1535 rtx call_fusage = 0;
1536 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1538 arguments = convert_memory_address (Pmode, arguments);
1540 /* Create a block where the return registers can be saved. */
1541 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1543 /* Fetch the arg pointer from the ARGUMENTS block. */
1544 incoming_args = gen_reg_rtx (Pmode);
1545 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1546 #ifndef STACK_GROWS_DOWNWARD
1547 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1548 incoming_args, 0, OPTAB_LIB_WIDEN);
1551 /* Push a new argument block and copy the arguments. Do not allow
1552 the (potential) memcpy call below to interfere with our stack
1554 do_pending_stack_adjust ();
1557 /* Save the stack with nonlocal if available. */
1558 #ifdef HAVE_save_stack_nonlocal
1559 if (HAVE_save_stack_nonlocal)
1560 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1563 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1565 /* Allocate a block of memory onto the stack and copy the memory
1566 arguments to the outgoing arguments address. We can pass TRUE
1567 as the 4th argument because we just saved the stack pointer
1568 and will restore it right after the call. */
1569 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, true);
1571 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1572 may have already set current_function_calls_alloca to true.
1573 current_function_calls_alloca won't be set if argsize is zero,
1574 so we have to guarantee need_drap is true here. */
1575 if (SUPPORTS_STACK_ALIGNMENT)
1576 crtl->need_drap = true;
1578 dest = virtual_outgoing_args_rtx;
1579 #ifndef STACK_GROWS_DOWNWARD
1580 if (CONST_INT_P (argsize))
1581 dest = plus_constant (dest, -INTVAL (argsize));
1583 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1585 dest = gen_rtx_MEM (BLKmode, dest);
1586 set_mem_align (dest, PARM_BOUNDARY);
1587 src = gen_rtx_MEM (BLKmode, incoming_args);
1588 set_mem_align (src, PARM_BOUNDARY);
1589 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1591 /* Refer to the argument block. */
1593 arguments = gen_rtx_MEM (BLKmode, arguments);
1594 set_mem_align (arguments, PARM_BOUNDARY);
1596 /* Walk past the arg-pointer and structure value address. */
1597 size = GET_MODE_SIZE (Pmode);
1599 size += GET_MODE_SIZE (Pmode);
1601 /* Restore each of the registers previously saved. Make USE insns
1602 for each of these registers for use in making the call. */
1603 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1604 if ((mode = apply_args_mode[regno]) != VOIDmode)
1606 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1607 if (size % align != 0)
1608 size = CEIL (size, align) * align;
1609 reg = gen_rtx_REG (mode, regno);
1610 emit_move_insn (reg, adjust_address (arguments, mode, size));
1611 use_reg (&call_fusage, reg);
1612 size += GET_MODE_SIZE (mode);
1615 /* Restore the structure value address unless this is passed as an
1616 "invisible" first argument. */
1617 size = GET_MODE_SIZE (Pmode);
1620 rtx value = gen_reg_rtx (Pmode);
1621 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1622 emit_move_insn (struct_value, value);
1623 if (REG_P (struct_value))
1624 use_reg (&call_fusage, struct_value);
1625 size += GET_MODE_SIZE (Pmode);
1628 /* All arguments and registers used for the call are set up by now! */
1629 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1631 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1632 and we don't want to load it into a register as an optimization,
1633 because prepare_call_address already did it if it should be done. */
1634 if (GET_CODE (function) != SYMBOL_REF)
1635 function = memory_address (FUNCTION_MODE, function);
1637 /* Generate the actual call instruction and save the return value. */
1638 #ifdef HAVE_untyped_call
1639 if (HAVE_untyped_call)
1640 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
1641 result, result_vector (1, result)));
1644 #ifdef HAVE_call_value
1645 if (HAVE_call_value)
1649 /* Locate the unique return register. It is not possible to
1650 express a call that sets more than one return register using
1651 call_value; use untyped_call for that. In fact, untyped_call
1652 only needs to save the return registers in the given block. */
1653 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1654 if ((mode = apply_result_mode[regno]) != VOIDmode)
1656 gcc_assert (!valreg); /* HAVE_untyped_call required. */
1658 valreg = gen_rtx_REG (mode, regno);
1661 emit_call_insn (GEN_CALL_VALUE (valreg,
1662 gen_rtx_MEM (FUNCTION_MODE, function),
1663 const0_rtx, NULL_RTX, const0_rtx));
1665 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1671 /* Find the CALL insn we just emitted, and attach the register usage
1673 call_insn = last_call_insn ();
1674 add_function_usage_to (call_insn, call_fusage);
1676 /* Restore the stack. */
1677 #ifdef HAVE_save_stack_nonlocal
1678 if (HAVE_save_stack_nonlocal)
1679 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1682 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1683 fixup_args_size_notes (call_insn, get_last_insn(), 0);
1687 /* Return the address of the result block. */
1688 result = copy_addr_to_reg (XEXP (result, 0));
1689 return convert_memory_address (ptr_mode, result);
1692 /* Perform an untyped return. */
1695 expand_builtin_return (rtx result)
1697 int size, align, regno;
1698 enum machine_mode mode;
1700 rtx call_fusage = 0;
1702 result = convert_memory_address (Pmode, result);
1704 apply_result_size ();
1705 result = gen_rtx_MEM (BLKmode, result);
1707 #ifdef HAVE_untyped_return
1708 if (HAVE_untyped_return)
1710 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1716 /* Restore the return value and note that each value is used. */
1718 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1719 if ((mode = apply_result_mode[regno]) != VOIDmode)
1721 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1722 if (size % align != 0)
1723 size = CEIL (size, align) * align;
1724 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1725 emit_move_insn (reg, adjust_address (result, mode, size));
1727 push_to_sequence (call_fusage);
1729 call_fusage = get_insns ();
1731 size += GET_MODE_SIZE (mode);
1734 /* Put the USE insns before the return. */
1735 emit_insn (call_fusage);
1737 /* Return whatever values was restored by jumping directly to the end
1739 expand_naked_return ();
1742 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1744 static enum type_class
1745 type_to_class (tree type)
1747 switch (TREE_CODE (type))
1749 case VOID_TYPE: return void_type_class;
1750 case INTEGER_TYPE: return integer_type_class;
1751 case ENUMERAL_TYPE: return enumeral_type_class;
1752 case BOOLEAN_TYPE: return boolean_type_class;
1753 case POINTER_TYPE: return pointer_type_class;
1754 case REFERENCE_TYPE: return reference_type_class;
1755 case OFFSET_TYPE: return offset_type_class;
1756 case REAL_TYPE: return real_type_class;
1757 case COMPLEX_TYPE: return complex_type_class;
1758 case FUNCTION_TYPE: return function_type_class;
1759 case METHOD_TYPE: return method_type_class;
1760 case RECORD_TYPE: return record_type_class;
1762 case QUAL_UNION_TYPE: return union_type_class;
1763 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1764 ? string_type_class : array_type_class);
1765 case LANG_TYPE: return lang_type_class;
1766 default: return no_type_class;
1770 /* Expand a call EXP to __builtin_classify_type. */
1773 expand_builtin_classify_type (tree exp)
1775 if (call_expr_nargs (exp))
1776 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1777 return GEN_INT (no_type_class);
1780 /* This helper macro, meant to be used in mathfn_built_in below,
1781 determines which among a set of three builtin math functions is
1782 appropriate for a given type mode. The `F' and `L' cases are
1783 automatically generated from the `double' case. */
1784 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1785 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1786 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1787 fcodel = BUILT_IN_MATHFN##L ; break;
1788 /* Similar to above, but appends _R after any F/L suffix. */
1789 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1790 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1791 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1792 fcodel = BUILT_IN_MATHFN##L_R ; break;
1794 /* Return mathematic function equivalent to FN but operating directly
1795 on TYPE, if available. If IMPLICIT is true find the function in
1796 implicit_built_in_decls[], otherwise use built_in_decls[]. If we
1797 can't do the conversion, return zero. */
1800 mathfn_built_in_1 (tree type, enum built_in_function fn, bool implicit)
1802 tree const *const fn_arr
1803 = implicit ? implicit_built_in_decls : built_in_decls;
1804 enum built_in_function fcode, fcodef, fcodel;
1808 CASE_MATHFN (BUILT_IN_ACOS)
1809 CASE_MATHFN (BUILT_IN_ACOSH)
1810 CASE_MATHFN (BUILT_IN_ASIN)
1811 CASE_MATHFN (BUILT_IN_ASINH)
1812 CASE_MATHFN (BUILT_IN_ATAN)
1813 CASE_MATHFN (BUILT_IN_ATAN2)
1814 CASE_MATHFN (BUILT_IN_ATANH)
1815 CASE_MATHFN (BUILT_IN_CBRT)
1816 CASE_MATHFN (BUILT_IN_CEIL)
1817 CASE_MATHFN (BUILT_IN_CEXPI)
1818 CASE_MATHFN (BUILT_IN_COPYSIGN)
1819 CASE_MATHFN (BUILT_IN_COS)
1820 CASE_MATHFN (BUILT_IN_COSH)
1821 CASE_MATHFN (BUILT_IN_DREM)
1822 CASE_MATHFN (BUILT_IN_ERF)
1823 CASE_MATHFN (BUILT_IN_ERFC)
1824 CASE_MATHFN (BUILT_IN_EXP)
1825 CASE_MATHFN (BUILT_IN_EXP10)
1826 CASE_MATHFN (BUILT_IN_EXP2)
1827 CASE_MATHFN (BUILT_IN_EXPM1)
1828 CASE_MATHFN (BUILT_IN_FABS)
1829 CASE_MATHFN (BUILT_IN_FDIM)
1830 CASE_MATHFN (BUILT_IN_FLOOR)
1831 CASE_MATHFN (BUILT_IN_FMA)
1832 CASE_MATHFN (BUILT_IN_FMAX)
1833 CASE_MATHFN (BUILT_IN_FMIN)
1834 CASE_MATHFN (BUILT_IN_FMOD)
1835 CASE_MATHFN (BUILT_IN_FREXP)
1836 CASE_MATHFN (BUILT_IN_GAMMA)
1837 CASE_MATHFN_REENT (BUILT_IN_GAMMA) /* GAMMA_R */
1838 CASE_MATHFN (BUILT_IN_HUGE_VAL)
1839 CASE_MATHFN (BUILT_IN_HYPOT)
1840 CASE_MATHFN (BUILT_IN_ILOGB)
1841 CASE_MATHFN (BUILT_IN_ICEIL)
1842 CASE_MATHFN (BUILT_IN_IFLOOR)
1843 CASE_MATHFN (BUILT_IN_INF)
1844 CASE_MATHFN (BUILT_IN_IRINT)
1845 CASE_MATHFN (BUILT_IN_IROUND)
1846 CASE_MATHFN (BUILT_IN_ISINF)
1847 CASE_MATHFN (BUILT_IN_J0)
1848 CASE_MATHFN (BUILT_IN_J1)
1849 CASE_MATHFN (BUILT_IN_JN)
1850 CASE_MATHFN (BUILT_IN_LCEIL)
1851 CASE_MATHFN (BUILT_IN_LDEXP)
1852 CASE_MATHFN (BUILT_IN_LFLOOR)
1853 CASE_MATHFN (BUILT_IN_LGAMMA)
1854 CASE_MATHFN_REENT (BUILT_IN_LGAMMA) /* LGAMMA_R */
1855 CASE_MATHFN (BUILT_IN_LLCEIL)
1856 CASE_MATHFN (BUILT_IN_LLFLOOR)
1857 CASE_MATHFN (BUILT_IN_LLRINT)
1858 CASE_MATHFN (BUILT_IN_LLROUND)
1859 CASE_MATHFN (BUILT_IN_LOG)
1860 CASE_MATHFN (BUILT_IN_LOG10)
1861 CASE_MATHFN (BUILT_IN_LOG1P)
1862 CASE_MATHFN (BUILT_IN_LOG2)
1863 CASE_MATHFN (BUILT_IN_LOGB)
1864 CASE_MATHFN (BUILT_IN_LRINT)
1865 CASE_MATHFN (BUILT_IN_LROUND)
1866 CASE_MATHFN (BUILT_IN_MODF)
1867 CASE_MATHFN (BUILT_IN_NAN)
1868 CASE_MATHFN (BUILT_IN_NANS)
1869 CASE_MATHFN (BUILT_IN_NEARBYINT)
1870 CASE_MATHFN (BUILT_IN_NEXTAFTER)
1871 CASE_MATHFN (BUILT_IN_NEXTTOWARD)
1872 CASE_MATHFN (BUILT_IN_POW)
1873 CASE_MATHFN (BUILT_IN_POWI)
1874 CASE_MATHFN (BUILT_IN_POW10)
1875 CASE_MATHFN (BUILT_IN_REMAINDER)
1876 CASE_MATHFN (BUILT_IN_REMQUO)
1877 CASE_MATHFN (BUILT_IN_RINT)
1878 CASE_MATHFN (BUILT_IN_ROUND)
1879 CASE_MATHFN (BUILT_IN_SCALB)
1880 CASE_MATHFN (BUILT_IN_SCALBLN)
1881 CASE_MATHFN (BUILT_IN_SCALBN)
1882 CASE_MATHFN (BUILT_IN_SIGNBIT)
1883 CASE_MATHFN (BUILT_IN_SIGNIFICAND)
1884 CASE_MATHFN (BUILT_IN_SIN)
1885 CASE_MATHFN (BUILT_IN_SINCOS)
1886 CASE_MATHFN (BUILT_IN_SINH)
1887 CASE_MATHFN (BUILT_IN_SQRT)
1888 CASE_MATHFN (BUILT_IN_TAN)
1889 CASE_MATHFN (BUILT_IN_TANH)
1890 CASE_MATHFN (BUILT_IN_TGAMMA)
1891 CASE_MATHFN (BUILT_IN_TRUNC)
1892 CASE_MATHFN (BUILT_IN_Y0)
1893 CASE_MATHFN (BUILT_IN_Y1)
1894 CASE_MATHFN (BUILT_IN_YN)
1900 if (TYPE_MAIN_VARIANT (type) == double_type_node)
1901 return fn_arr[fcode];
1902 else if (TYPE_MAIN_VARIANT (type) == float_type_node)
1903 return fn_arr[fcodef];
1904 else if (TYPE_MAIN_VARIANT (type) == long_double_type_node)
1905 return fn_arr[fcodel];
1910 /* Like mathfn_built_in_1(), but always use the implicit array. */
1913 mathfn_built_in (tree type, enum built_in_function fn)
1915 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
1918 /* If errno must be maintained, expand the RTL to check if the result,
1919 TARGET, of a built-in function call, EXP, is NaN, and if so set
1923 expand_errno_check (tree exp, rtx target)
1925 rtx lab = gen_label_rtx ();
1927 /* Test the result; if it is NaN, set errno=EDOM because
1928 the argument was not in the domain. */
1929 do_compare_rtx_and_jump (target, target, EQ, 0, GET_MODE (target),
1930 NULL_RTX, NULL_RTX, lab,
1931 /* The jump is very likely. */
1932 REG_BR_PROB_BASE - (REG_BR_PROB_BASE / 2000 - 1));
1935 /* If this built-in doesn't throw an exception, set errno directly. */
1936 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp), 0)))
1938 #ifdef GEN_ERRNO_RTX
1939 rtx errno_rtx = GEN_ERRNO_RTX;
1942 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1944 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1950 /* Make sure the library call isn't expanded as a tail call. */
1951 CALL_EXPR_TAILCALL (exp) = 0;
1953 /* We can't set errno=EDOM directly; let the library call do it.
1954 Pop the arguments right away in case the call gets deleted. */
1956 expand_call (exp, target, 0);
1961 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1962 Return NULL_RTX if a normal call should be emitted rather than expanding
1963 the function in-line. EXP is the expression that is a call to the builtin
1964 function; if convenient, the result should be placed in TARGET.
1965 SUBTARGET may be used as the target for computing one of EXP's operands. */
1968 expand_builtin_mathfn (tree exp, rtx target, rtx subtarget)
1970 optab builtin_optab;
1972 tree fndecl = get_callee_fndecl (exp);
1973 enum machine_mode mode;
1974 bool errno_set = false;
1977 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
1980 arg = CALL_EXPR_ARG (exp, 0);
1982 switch (DECL_FUNCTION_CODE (fndecl))
1984 CASE_FLT_FN (BUILT_IN_SQRT):
1985 errno_set = ! tree_expr_nonnegative_p (arg);
1986 builtin_optab = sqrt_optab;
1988 CASE_FLT_FN (BUILT_IN_EXP):
1989 errno_set = true; builtin_optab = exp_optab; break;
1990 CASE_FLT_FN (BUILT_IN_EXP10):
1991 CASE_FLT_FN (BUILT_IN_POW10):
1992 errno_set = true; builtin_optab = exp10_optab; break;
1993 CASE_FLT_FN (BUILT_IN_EXP2):
1994 errno_set = true; builtin_optab = exp2_optab; break;
1995 CASE_FLT_FN (BUILT_IN_EXPM1):
1996 errno_set = true; builtin_optab = expm1_optab; break;
1997 CASE_FLT_FN (BUILT_IN_LOGB):
1998 errno_set = true; builtin_optab = logb_optab; break;
1999 CASE_FLT_FN (BUILT_IN_LOG):
2000 errno_set = true; builtin_optab = log_optab; break;
2001 CASE_FLT_FN (BUILT_IN_LOG10):
2002 errno_set = true; builtin_optab = log10_optab; break;
2003 CASE_FLT_FN (BUILT_IN_LOG2):
2004 errno_set = true; builtin_optab = log2_optab; break;
2005 CASE_FLT_FN (BUILT_IN_LOG1P):
2006 errno_set = true; builtin_optab = log1p_optab; break;
2007 CASE_FLT_FN (BUILT_IN_ASIN):
2008 builtin_optab = asin_optab; break;
2009 CASE_FLT_FN (BUILT_IN_ACOS):
2010 builtin_optab = acos_optab; break;
2011 CASE_FLT_FN (BUILT_IN_TAN):
2012 builtin_optab = tan_optab; break;
2013 CASE_FLT_FN (BUILT_IN_ATAN):
2014 builtin_optab = atan_optab; break;
2015 CASE_FLT_FN (BUILT_IN_FLOOR):
2016 builtin_optab = floor_optab; break;
2017 CASE_FLT_FN (BUILT_IN_CEIL):
2018 builtin_optab = ceil_optab; break;
2019 CASE_FLT_FN (BUILT_IN_TRUNC):
2020 builtin_optab = btrunc_optab; break;
2021 CASE_FLT_FN (BUILT_IN_ROUND):
2022 builtin_optab = round_optab; break;
2023 CASE_FLT_FN (BUILT_IN_NEARBYINT):
2024 builtin_optab = nearbyint_optab;
2025 if (flag_trapping_math)
2027 /* Else fallthrough and expand as rint. */
2028 CASE_FLT_FN (BUILT_IN_RINT):
2029 builtin_optab = rint_optab; break;
2030 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
2031 builtin_optab = significand_optab; break;
2036 /* Make a suitable register to place result in. */
2037 mode = TYPE_MODE (TREE_TYPE (exp));
2039 if (! flag_errno_math || ! HONOR_NANS (mode))
2042 /* Before working hard, check whether the instruction is available. */
2043 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing
2044 && (!errno_set || !optimize_insn_for_size_p ()))
2046 target = gen_reg_rtx (mode);
2048 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2049 need to expand the argument again. This way, we will not perform
2050 side-effects more the once. */
2051 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2053 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2057 /* Compute into TARGET.
2058 Set TARGET to wherever the result comes back. */
2059 target = expand_unop (mode, builtin_optab, op0, target, 0);
2064 expand_errno_check (exp, target);
2066 /* Output the entire sequence. */
2067 insns = get_insns ();
2073 /* If we were unable to expand via the builtin, stop the sequence
2074 (without outputting the insns) and call to the library function
2075 with the stabilized argument list. */
2079 return expand_call (exp, target, target == const0_rtx);
2082 /* Expand a call to the builtin binary math functions (pow and atan2).
2083 Return NULL_RTX if a normal call should be emitted rather than expanding the
2084 function in-line. EXP is the expression that is a call to the builtin
2085 function; if convenient, the result should be placed in TARGET.
2086 SUBTARGET may be used as the target for computing one of EXP's
2090 expand_builtin_mathfn_2 (tree exp, rtx target, rtx subtarget)
2092 optab builtin_optab;
2093 rtx op0, op1, insns;
2094 int op1_type = REAL_TYPE;
2095 tree fndecl = get_callee_fndecl (exp);
2097 enum machine_mode mode;
2098 bool errno_set = true;
2100 switch (DECL_FUNCTION_CODE (fndecl))
2102 CASE_FLT_FN (BUILT_IN_SCALBN):
2103 CASE_FLT_FN (BUILT_IN_SCALBLN):
2104 CASE_FLT_FN (BUILT_IN_LDEXP):
2105 op1_type = INTEGER_TYPE;
2110 if (!validate_arglist (exp, REAL_TYPE, op1_type, VOID_TYPE))
2113 arg0 = CALL_EXPR_ARG (exp, 0);
2114 arg1 = CALL_EXPR_ARG (exp, 1);
2116 switch (DECL_FUNCTION_CODE (fndecl))
2118 CASE_FLT_FN (BUILT_IN_POW):
2119 builtin_optab = pow_optab; break;
2120 CASE_FLT_FN (BUILT_IN_ATAN2):
2121 builtin_optab = atan2_optab; break;
2122 CASE_FLT_FN (BUILT_IN_SCALB):
2123 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2125 builtin_optab = scalb_optab; break;
2126 CASE_FLT_FN (BUILT_IN_SCALBN):
2127 CASE_FLT_FN (BUILT_IN_SCALBLN):
2128 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp)))->b != 2)
2130 /* Fall through... */
2131 CASE_FLT_FN (BUILT_IN_LDEXP):
2132 builtin_optab = ldexp_optab; break;
2133 CASE_FLT_FN (BUILT_IN_FMOD):
2134 builtin_optab = fmod_optab; break;
2135 CASE_FLT_FN (BUILT_IN_REMAINDER):
2136 CASE_FLT_FN (BUILT_IN_DREM):
2137 builtin_optab = remainder_optab; break;
2142 /* Make a suitable register to place result in. */
2143 mode = TYPE_MODE (TREE_TYPE (exp));
2145 /* Before working hard, check whether the instruction is available. */
2146 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2149 target = gen_reg_rtx (mode);
2151 if (! flag_errno_math || ! HONOR_NANS (mode))
2154 if (errno_set && optimize_insn_for_size_p ())
2157 /* Always stabilize the argument list. */
2158 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2159 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2161 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2162 op1 = expand_normal (arg1);
2166 /* Compute into TARGET.
2167 Set TARGET to wherever the result comes back. */
2168 target = expand_binop (mode, builtin_optab, op0, op1,
2169 target, 0, OPTAB_DIRECT);
2171 /* If we were unable to expand via the builtin, stop the sequence
2172 (without outputting the insns) and call to the library function
2173 with the stabilized argument list. */
2177 return expand_call (exp, target, target == const0_rtx);
2181 expand_errno_check (exp, target);
2183 /* Output the entire sequence. */
2184 insns = get_insns ();
2191 /* Expand a call to the builtin trinary math functions (fma).
2192 Return NULL_RTX if a normal call should be emitted rather than expanding the
2193 function in-line. EXP is the expression that is a call to the builtin
2194 function; if convenient, the result should be placed in TARGET.
2195 SUBTARGET may be used as the target for computing one of EXP's
2199 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2201 optab builtin_optab;
2202 rtx op0, op1, op2, insns;
2203 tree fndecl = get_callee_fndecl (exp);
2204 tree arg0, arg1, arg2;
2205 enum machine_mode mode;
2207 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2210 arg0 = CALL_EXPR_ARG (exp, 0);
2211 arg1 = CALL_EXPR_ARG (exp, 1);
2212 arg2 = CALL_EXPR_ARG (exp, 2);
2214 switch (DECL_FUNCTION_CODE (fndecl))
2216 CASE_FLT_FN (BUILT_IN_FMA):
2217 builtin_optab = fma_optab; break;
2222 /* Make a suitable register to place result in. */
2223 mode = TYPE_MODE (TREE_TYPE (exp));
2225 /* Before working hard, check whether the instruction is available. */
2226 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2229 target = gen_reg_rtx (mode);
2231 /* Always stabilize the argument list. */
2232 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2233 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2234 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2236 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2237 op1 = expand_normal (arg1);
2238 op2 = expand_normal (arg2);
2242 /* Compute into TARGET.
2243 Set TARGET to wherever the result comes back. */
2244 target = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2247 /* If we were unable to expand via the builtin, stop the sequence
2248 (without outputting the insns) and call to the library function
2249 with the stabilized argument list. */
2253 return expand_call (exp, target, target == const0_rtx);
2256 /* Output the entire sequence. */
2257 insns = get_insns ();
2264 /* Expand a call to the builtin sin and cos math functions.
2265 Return NULL_RTX if a normal call should be emitted rather than expanding the
2266 function in-line. EXP is the expression that is a call to the builtin
2267 function; if convenient, the result should be placed in TARGET.
2268 SUBTARGET may be used as the target for computing one of EXP's
2272 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2274 optab builtin_optab;
2276 tree fndecl = get_callee_fndecl (exp);
2277 enum machine_mode mode;
2280 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2283 arg = CALL_EXPR_ARG (exp, 0);
2285 switch (DECL_FUNCTION_CODE (fndecl))
2287 CASE_FLT_FN (BUILT_IN_SIN):
2288 CASE_FLT_FN (BUILT_IN_COS):
2289 builtin_optab = sincos_optab; break;
2294 /* Make a suitable register to place result in. */
2295 mode = TYPE_MODE (TREE_TYPE (exp));
2297 /* Check if sincos insn is available, otherwise fallback
2298 to sin or cos insn. */
2299 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2300 switch (DECL_FUNCTION_CODE (fndecl))
2302 CASE_FLT_FN (BUILT_IN_SIN):
2303 builtin_optab = sin_optab; break;
2304 CASE_FLT_FN (BUILT_IN_COS):
2305 builtin_optab = cos_optab; break;
2310 /* Before working hard, check whether the instruction is available. */
2311 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2313 target = gen_reg_rtx (mode);
2315 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2316 need to expand the argument again. This way, we will not perform
2317 side-effects more the once. */
2318 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2320 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2324 /* Compute into TARGET.
2325 Set TARGET to wherever the result comes back. */
2326 if (builtin_optab == sincos_optab)
2330 switch (DECL_FUNCTION_CODE (fndecl))
2332 CASE_FLT_FN (BUILT_IN_SIN):
2333 result = expand_twoval_unop (builtin_optab, op0, 0, target, 0);
2335 CASE_FLT_FN (BUILT_IN_COS):
2336 result = expand_twoval_unop (builtin_optab, op0, target, 0, 0);
2341 gcc_assert (result);
2345 target = expand_unop (mode, builtin_optab, op0, target, 0);
2350 /* Output the entire sequence. */
2351 insns = get_insns ();
2357 /* If we were unable to expand via the builtin, stop the sequence
2358 (without outputting the insns) and call to the library function
2359 with the stabilized argument list. */
2363 target = expand_call (exp, target, target == const0_rtx);
2368 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2369 return an RTL instruction code that implements the functionality.
2370 If that isn't possible or available return CODE_FOR_nothing. */
2372 static enum insn_code
2373 interclass_mathfn_icode (tree arg, tree fndecl)
2375 bool errno_set = false;
2376 optab builtin_optab = 0;
2377 enum machine_mode mode;
2379 switch (DECL_FUNCTION_CODE (fndecl))
2381 CASE_FLT_FN (BUILT_IN_ILOGB):
2382 errno_set = true; builtin_optab = ilogb_optab; break;
2383 CASE_FLT_FN (BUILT_IN_ISINF):
2384 builtin_optab = isinf_optab; break;
2385 case BUILT_IN_ISNORMAL:
2386 case BUILT_IN_ISFINITE:
2387 CASE_FLT_FN (BUILT_IN_FINITE):
2388 case BUILT_IN_FINITED32:
2389 case BUILT_IN_FINITED64:
2390 case BUILT_IN_FINITED128:
2391 case BUILT_IN_ISINFD32:
2392 case BUILT_IN_ISINFD64:
2393 case BUILT_IN_ISINFD128:
2394 /* These builtins have no optabs (yet). */
2400 /* There's no easy way to detect the case we need to set EDOM. */
2401 if (flag_errno_math && errno_set)
2402 return CODE_FOR_nothing;
2404 /* Optab mode depends on the mode of the input argument. */
2405 mode = TYPE_MODE (TREE_TYPE (arg));
2408 return optab_handler (builtin_optab, mode);
2409 return CODE_FOR_nothing;
2412 /* Expand a call to one of the builtin math functions that operate on
2413 floating point argument and output an integer result (ilogb, isinf,
2415 Return 0 if a normal call should be emitted rather than expanding the
2416 function in-line. EXP is the expression that is a call to the builtin
2417 function; if convenient, the result should be placed in TARGET. */
2420 expand_builtin_interclass_mathfn (tree exp, rtx target)
2422 enum insn_code icode = CODE_FOR_nothing;
2424 tree fndecl = get_callee_fndecl (exp);
2425 enum machine_mode mode;
2428 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2431 arg = CALL_EXPR_ARG (exp, 0);
2432 icode = interclass_mathfn_icode (arg, fndecl);
2433 mode = TYPE_MODE (TREE_TYPE (arg));
2435 if (icode != CODE_FOR_nothing)
2437 struct expand_operand ops[1];
2438 rtx last = get_last_insn ();
2439 tree orig_arg = arg;
2441 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2442 need to expand the argument again. This way, we will not perform
2443 side-effects more the once. */
2444 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2446 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2448 if (mode != GET_MODE (op0))
2449 op0 = convert_to_mode (mode, op0, 0);
2451 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2452 if (maybe_legitimize_operands (icode, 0, 1, ops)
2453 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2454 return ops[0].value;
2456 delete_insns_since (last);
2457 CALL_EXPR_ARG (exp, 0) = orig_arg;
2463 /* Expand a call to the builtin sincos math function.
2464 Return NULL_RTX if a normal call should be emitted rather than expanding the
2465 function in-line. EXP is the expression that is a call to the builtin
2469 expand_builtin_sincos (tree exp)
2471 rtx op0, op1, op2, target1, target2;
2472 enum machine_mode mode;
2473 tree arg, sinp, cosp;
2475 location_t loc = EXPR_LOCATION (exp);
2476 tree alias_type, alias_off;
2478 if (!validate_arglist (exp, REAL_TYPE,
2479 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2482 arg = CALL_EXPR_ARG (exp, 0);
2483 sinp = CALL_EXPR_ARG (exp, 1);
2484 cosp = CALL_EXPR_ARG (exp, 2);
2486 /* Make a suitable register to place result in. */
2487 mode = TYPE_MODE (TREE_TYPE (arg));
2489 /* Check if sincos insn is available, otherwise emit the call. */
2490 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2493 target1 = gen_reg_rtx (mode);
2494 target2 = gen_reg_rtx (mode);
2496 op0 = expand_normal (arg);
2497 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2498 alias_off = build_int_cst (alias_type, 0);
2499 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2501 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2504 /* Compute into target1 and target2.
2505 Set TARGET to wherever the result comes back. */
2506 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2507 gcc_assert (result);
2509 /* Move target1 and target2 to the memory locations indicated
2511 emit_move_insn (op1, target1);
2512 emit_move_insn (op2, target2);
2517 /* Expand a call to the internal cexpi builtin to the sincos math function.
2518 EXP is the expression that is a call to the builtin function; if convenient,
2519 the result should be placed in TARGET. */
2522 expand_builtin_cexpi (tree exp, rtx target)
2524 tree fndecl = get_callee_fndecl (exp);
2526 enum machine_mode mode;
2528 location_t loc = EXPR_LOCATION (exp);
2530 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2533 arg = CALL_EXPR_ARG (exp, 0);
2534 type = TREE_TYPE (arg);
2535 mode = TYPE_MODE (TREE_TYPE (arg));
2537 /* Try expanding via a sincos optab, fall back to emitting a libcall
2538 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2539 is only generated from sincos, cexp or if we have either of them. */
2540 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2542 op1 = gen_reg_rtx (mode);
2543 op2 = gen_reg_rtx (mode);
2545 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2547 /* Compute into op1 and op2. */
2548 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2550 else if (TARGET_HAS_SINCOS)
2552 tree call, fn = NULL_TREE;
2556 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2557 fn = built_in_decls[BUILT_IN_SINCOSF];
2558 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2559 fn = built_in_decls[BUILT_IN_SINCOS];
2560 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2561 fn = built_in_decls[BUILT_IN_SINCOSL];
2565 op1 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2566 op2 = assign_temp (TREE_TYPE (arg), 0, 1, 1);
2567 op1a = copy_to_mode_reg (Pmode, XEXP (op1, 0));
2568 op2a = copy_to_mode_reg (Pmode, XEXP (op2, 0));
2569 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2570 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2572 /* Make sure not to fold the sincos call again. */
2573 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2574 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2575 call, 3, arg, top1, top2));
2579 tree call, fn = NULL_TREE, narg;
2580 tree ctype = build_complex_type (type);
2582 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2583 fn = built_in_decls[BUILT_IN_CEXPF];
2584 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2585 fn = built_in_decls[BUILT_IN_CEXP];
2586 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2587 fn = built_in_decls[BUILT_IN_CEXPL];
2591 /* If we don't have a decl for cexp create one. This is the
2592 friendliest fallback if the user calls __builtin_cexpi
2593 without full target C99 function support. */
2594 if (fn == NULL_TREE)
2597 const char *name = NULL;
2599 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2601 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2603 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2606 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2607 fn = build_fn_decl (name, fntype);
2610 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2611 build_real (type, dconst0), arg);
2613 /* Make sure not to fold the cexp call again. */
2614 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2615 return expand_expr (build_call_nary (ctype, call, 1, narg),
2616 target, VOIDmode, EXPAND_NORMAL);
2619 /* Now build the proper return type. */
2620 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2621 make_tree (TREE_TYPE (arg), op2),
2622 make_tree (TREE_TYPE (arg), op1)),
2623 target, VOIDmode, EXPAND_NORMAL);
2626 /* Conveniently construct a function call expression. FNDECL names the
2627 function to be called, N is the number of arguments, and the "..."
2628 parameters are the argument expressions. Unlike build_call_exr
2629 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2632 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2635 tree fntype = TREE_TYPE (fndecl);
2636 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2639 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2641 SET_EXPR_LOCATION (fn, loc);
2645 /* Expand a call to one of the builtin rounding functions gcc defines
2646 as an extension (lfloor and lceil). As these are gcc extensions we
2647 do not need to worry about setting errno to EDOM.
2648 If expanding via optab fails, lower expression to (int)(floor(x)).
2649 EXP is the expression that is a call to the builtin function;
2650 if convenient, the result should be placed in TARGET. */
2653 expand_builtin_int_roundingfn (tree exp, rtx target)
2655 convert_optab builtin_optab;
2656 rtx op0, insns, tmp;
2657 tree fndecl = get_callee_fndecl (exp);
2658 enum built_in_function fallback_fn;
2659 tree fallback_fndecl;
2660 enum machine_mode mode;
2663 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2666 arg = CALL_EXPR_ARG (exp, 0);
2668 switch (DECL_FUNCTION_CODE (fndecl))
2670 CASE_FLT_FN (BUILT_IN_ICEIL):
2671 CASE_FLT_FN (BUILT_IN_LCEIL):
2672 CASE_FLT_FN (BUILT_IN_LLCEIL):
2673 builtin_optab = lceil_optab;
2674 fallback_fn = BUILT_IN_CEIL;
2677 CASE_FLT_FN (BUILT_IN_IFLOOR):
2678 CASE_FLT_FN (BUILT_IN_LFLOOR):
2679 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2680 builtin_optab = lfloor_optab;
2681 fallback_fn = BUILT_IN_FLOOR;
2688 /* Make a suitable register to place result in. */
2689 mode = TYPE_MODE (TREE_TYPE (exp));
2691 target = gen_reg_rtx (mode);
2693 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2694 need to expand the argument again. This way, we will not perform
2695 side-effects more the once. */
2696 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2698 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2702 /* Compute into TARGET. */
2703 if (expand_sfix_optab (target, op0, builtin_optab))
2705 /* Output the entire sequence. */
2706 insns = get_insns ();
2712 /* If we were unable to expand via the builtin, stop the sequence
2713 (without outputting the insns). */
2716 /* Fall back to floating point rounding optab. */
2717 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2719 /* For non-C99 targets we may end up without a fallback fndecl here
2720 if the user called __builtin_lfloor directly. In this case emit
2721 a call to the floor/ceil variants nevertheless. This should result
2722 in the best user experience for not full C99 targets. */
2723 if (fallback_fndecl == NULL_TREE)
2726 const char *name = NULL;
2728 switch (DECL_FUNCTION_CODE (fndecl))
2730 case BUILT_IN_ICEIL:
2731 case BUILT_IN_LCEIL:
2732 case BUILT_IN_LLCEIL:
2735 case BUILT_IN_ICEILF:
2736 case BUILT_IN_LCEILF:
2737 case BUILT_IN_LLCEILF:
2740 case BUILT_IN_ICEILL:
2741 case BUILT_IN_LCEILL:
2742 case BUILT_IN_LLCEILL:
2745 case BUILT_IN_IFLOOR:
2746 case BUILT_IN_LFLOOR:
2747 case BUILT_IN_LLFLOOR:
2750 case BUILT_IN_IFLOORF:
2751 case BUILT_IN_LFLOORF:
2752 case BUILT_IN_LLFLOORF:
2755 case BUILT_IN_IFLOORL:
2756 case BUILT_IN_LFLOORL:
2757 case BUILT_IN_LLFLOORL:
2764 fntype = build_function_type_list (TREE_TYPE (arg),
2765 TREE_TYPE (arg), NULL_TREE);
2766 fallback_fndecl = build_fn_decl (name, fntype);
2769 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2771 tmp = expand_normal (exp);
2773 /* Truncate the result of floating point optab to integer
2774 via expand_fix (). */
2775 target = gen_reg_rtx (mode);
2776 expand_fix (target, tmp, 0);
2781 /* Expand a call to one of the builtin math functions doing integer
2783 Return 0 if a normal call should be emitted rather than expanding the
2784 function in-line. EXP is the expression that is a call to the builtin
2785 function; if convenient, the result should be placed in TARGET. */
2788 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2790 convert_optab builtin_optab;
2792 tree fndecl = get_callee_fndecl (exp);
2794 enum machine_mode mode;
2796 /* There's no easy way to detect the case we need to set EDOM. */
2797 if (flag_errno_math)
2800 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2803 arg = CALL_EXPR_ARG (exp, 0);
2805 switch (DECL_FUNCTION_CODE (fndecl))
2807 CASE_FLT_FN (BUILT_IN_IRINT):
2808 CASE_FLT_FN (BUILT_IN_LRINT):
2809 CASE_FLT_FN (BUILT_IN_LLRINT):
2810 builtin_optab = lrint_optab; break;
2812 CASE_FLT_FN (BUILT_IN_IROUND):
2813 CASE_FLT_FN (BUILT_IN_LROUND):
2814 CASE_FLT_FN (BUILT_IN_LLROUND):
2815 builtin_optab = lround_optab; break;
2821 /* Make a suitable register to place result in. */
2822 mode = TYPE_MODE (TREE_TYPE (exp));
2824 target = gen_reg_rtx (mode);
2826 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2827 need to expand the argument again. This way, we will not perform
2828 side-effects more the once. */
2829 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2831 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2835 if (expand_sfix_optab (target, op0, builtin_optab))
2837 /* Output the entire sequence. */
2838 insns = get_insns ();
2844 /* If we were unable to expand via the builtin, stop the sequence
2845 (without outputting the insns) and call to the library function
2846 with the stabilized argument list. */
2849 target = expand_call (exp, target, target == const0_rtx);
2854 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2855 a normal call should be emitted rather than expanding the function
2856 in-line. EXP is the expression that is a call to the builtin
2857 function; if convenient, the result should be placed in TARGET. */
2860 expand_builtin_powi (tree exp, rtx target)
2864 enum machine_mode mode;
2865 enum machine_mode mode2;
2867 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2870 arg0 = CALL_EXPR_ARG (exp, 0);
2871 arg1 = CALL_EXPR_ARG (exp, 1);
2872 mode = TYPE_MODE (TREE_TYPE (exp));
2874 /* Emit a libcall to libgcc. */
2876 /* Mode of the 2nd argument must match that of an int. */
2877 mode2 = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
2879 if (target == NULL_RTX)
2880 target = gen_reg_rtx (mode);
2882 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2883 if (GET_MODE (op0) != mode)
2884 op0 = convert_to_mode (mode, op0, 0);
2885 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2886 if (GET_MODE (op1) != mode2)
2887 op1 = convert_to_mode (mode2, op1, 0);
2889 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2890 target, LCT_CONST, mode, 2,
2891 op0, mode, op1, mode2);
2896 /* Expand expression EXP which is a call to the strlen builtin. Return
2897 NULL_RTX if we failed the caller should emit a normal call, otherwise
2898 try to get the result in TARGET, if convenient. */
2901 expand_builtin_strlen (tree exp, rtx target,
2902 enum machine_mode target_mode)
2904 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2908 struct expand_operand ops[4];
2911 tree src = CALL_EXPR_ARG (exp, 0);
2912 rtx src_reg, before_strlen;
2913 enum machine_mode insn_mode = target_mode;
2914 enum insn_code icode = CODE_FOR_nothing;
2917 /* If the length can be computed at compile-time, return it. */
2918 len = c_strlen (src, 0);
2920 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2922 /* If the length can be computed at compile-time and is constant
2923 integer, but there are side-effects in src, evaluate
2924 src for side-effects, then return len.
2925 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2926 can be optimized into: i++; x = 3; */
2927 len = c_strlen (src, 1);
2928 if (len && TREE_CODE (len) == INTEGER_CST)
2930 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2931 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2934 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2936 /* If SRC is not a pointer type, don't do this operation inline. */
2940 /* Bail out if we can't compute strlen in the right mode. */
2941 while (insn_mode != VOIDmode)
2943 icode = optab_handler (strlen_optab, insn_mode);
2944 if (icode != CODE_FOR_nothing)
2947 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
2949 if (insn_mode == VOIDmode)
2952 /* Make a place to hold the source address. We will not expand
2953 the actual source until we are sure that the expansion will
2954 not fail -- there are trees that cannot be expanded twice. */
2955 src_reg = gen_reg_rtx (Pmode);
2957 /* Mark the beginning of the strlen sequence so we can emit the
2958 source operand later. */
2959 before_strlen = get_last_insn ();
2961 create_output_operand (&ops[0], target, insn_mode);
2962 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2963 create_integer_operand (&ops[2], 0);
2964 create_integer_operand (&ops[3], align);
2965 if (!maybe_expand_insn (icode, 4, ops))
2968 /* Now that we are assured of success, expand the source. */
2970 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2973 #ifdef POINTERS_EXTEND_UNSIGNED
2974 if (GET_MODE (pat) != Pmode)
2975 pat = convert_to_mode (Pmode, pat,
2976 POINTERS_EXTEND_UNSIGNED);
2978 emit_move_insn (src_reg, pat);
2984 emit_insn_after (pat, before_strlen);
2986 emit_insn_before (pat, get_insns ());
2988 /* Return the value in the proper mode for this function. */
2989 if (GET_MODE (ops[0].value) == target_mode)
2990 target = ops[0].value;
2991 else if (target != 0)
2992 convert_move (target, ops[0].value, 0);
2994 target = convert_to_mode (target_mode, ops[0].value, 0);
3000 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3001 bytes from constant string DATA + OFFSET and return it as target
3005 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3006 enum machine_mode mode)
3008 const char *str = (const char *) data;
3010 gcc_assert (offset >= 0
3011 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3012 <= strlen (str) + 1));
3014 return c_readstr (str + offset, mode);
3017 /* Expand a call EXP to the memcpy builtin.
3018 Return NULL_RTX if we failed, the caller should emit a normal call,
3019 otherwise try to get the result in TARGET, if convenient (and in
3020 mode MODE if that's convenient). */
3023 expand_builtin_memcpy (tree exp, rtx target)
3025 if (!validate_arglist (exp,
3026 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3030 tree dest = CALL_EXPR_ARG (exp, 0);
3031 tree src = CALL_EXPR_ARG (exp, 1);
3032 tree len = CALL_EXPR_ARG (exp, 2);
3033 const char *src_str;
3034 unsigned int src_align = get_pointer_alignment (src);
3035 unsigned int dest_align = get_pointer_alignment (dest);
3036 rtx dest_mem, src_mem, dest_addr, len_rtx;
3037 HOST_WIDE_INT expected_size = -1;
3038 unsigned int expected_align = 0;
3040 /* If DEST is not a pointer type, call the normal function. */
3041 if (dest_align == 0)
3044 /* If either SRC is not a pointer type, don't do this
3045 operation in-line. */
3049 if (currently_expanding_gimple_stmt)
3050 stringop_block_profile (currently_expanding_gimple_stmt,
3051 &expected_align, &expected_size);
3053 if (expected_align < dest_align)
3054 expected_align = dest_align;
3055 dest_mem = get_memory_rtx (dest, len);
3056 set_mem_align (dest_mem, dest_align);
3057 len_rtx = expand_normal (len);
3058 src_str = c_getstr (src);
3060 /* If SRC is a string constant and block move would be done
3061 by pieces, we can avoid loading the string from memory
3062 and only stored the computed constants. */
3064 && CONST_INT_P (len_rtx)
3065 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3066 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3067 CONST_CAST (char *, src_str),
3070 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3071 builtin_memcpy_read_str,
3072 CONST_CAST (char *, src_str),
3073 dest_align, false, 0);
3074 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3075 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3079 src_mem = get_memory_rtx (src, len);
3080 set_mem_align (src_mem, src_align);
3082 /* Copy word part most expediently. */
3083 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx,
3084 CALL_EXPR_TAILCALL (exp)
3085 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3086 expected_align, expected_size);
3090 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3091 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3097 /* Expand a call EXP to the mempcpy builtin.
3098 Return NULL_RTX if we failed; the caller should emit a normal call,
3099 otherwise try to get the result in TARGET, if convenient (and in
3100 mode MODE if that's convenient). If ENDP is 0 return the
3101 destination pointer, if ENDP is 1 return the end pointer ala
3102 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3106 expand_builtin_mempcpy (tree exp, rtx target, enum machine_mode mode)
3108 if (!validate_arglist (exp,
3109 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3113 tree dest = CALL_EXPR_ARG (exp, 0);
3114 tree src = CALL_EXPR_ARG (exp, 1);
3115 tree len = CALL_EXPR_ARG (exp, 2);
3116 return expand_builtin_mempcpy_args (dest, src, len,
3117 target, mode, /*endp=*/ 1);
3121 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3122 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3123 so that this can also be called without constructing an actual CALL_EXPR.
3124 The other arguments and return value are the same as for
3125 expand_builtin_mempcpy. */
3128 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3129 rtx target, enum machine_mode mode, int endp)
3131 /* If return value is ignored, transform mempcpy into memcpy. */
3132 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_MEMCPY])
3134 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
3135 tree result = build_call_nofold_loc (UNKNOWN_LOCATION, fn, 3,
3137 return expand_expr (result, target, mode, EXPAND_NORMAL);
3141 const char *src_str;
3142 unsigned int src_align = get_pointer_alignment (src);
3143 unsigned int dest_align = get_pointer_alignment (dest);
3144 rtx dest_mem, src_mem, len_rtx;
3146 /* If either SRC or DEST is not a pointer type, don't do this
3147 operation in-line. */
3148 if (dest_align == 0 || src_align == 0)
3151 /* If LEN is not constant, call the normal function. */
3152 if (! host_integerp (len, 1))
3155 len_rtx = expand_normal (len);
3156 src_str = c_getstr (src);
3158 /* If SRC is a string constant and block move would be done
3159 by pieces, we can avoid loading the string from memory
3160 and only stored the computed constants. */
3162 && CONST_INT_P (len_rtx)
3163 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3164 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3165 CONST_CAST (char *, src_str),
3168 dest_mem = get_memory_rtx (dest, len);
3169 set_mem_align (dest_mem, dest_align);
3170 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3171 builtin_memcpy_read_str,
3172 CONST_CAST (char *, src_str),
3173 dest_align, false, endp);
3174 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3175 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3179 if (CONST_INT_P (len_rtx)
3180 && can_move_by_pieces (INTVAL (len_rtx),
3181 MIN (dest_align, src_align)))
3183 dest_mem = get_memory_rtx (dest, len);
3184 set_mem_align (dest_mem, dest_align);
3185 src_mem = get_memory_rtx (src, len);
3186 set_mem_align (src_mem, src_align);
3187 dest_mem = move_by_pieces (dest_mem, src_mem, INTVAL (len_rtx),
3188 MIN (dest_align, src_align), endp);
3189 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3190 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3199 # define HAVE_movstr 0
3200 # define CODE_FOR_movstr CODE_FOR_nothing
3203 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3204 we failed, the caller should emit a normal call, otherwise try to
3205 get the result in TARGET, if convenient. If ENDP is 0 return the
3206 destination pointer, if ENDP is 1 return the end pointer ala
3207 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3211 expand_movstr (tree dest, tree src, rtx target, int endp)
3213 struct expand_operand ops[3];
3220 dest_mem = get_memory_rtx (dest, NULL);
3221 src_mem = get_memory_rtx (src, NULL);
3224 target = force_reg (Pmode, XEXP (dest_mem, 0));
3225 dest_mem = replace_equiv_address (dest_mem, target);
3228 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3229 create_fixed_operand (&ops[1], dest_mem);
3230 create_fixed_operand (&ops[2], src_mem);
3231 expand_insn (CODE_FOR_movstr, 3, ops);
3233 if (endp && target != const0_rtx)
3235 target = ops[0].value;
3236 /* movstr is supposed to set end to the address of the NUL
3237 terminator. If the caller requested a mempcpy-like return value,
3241 rtx tem = plus_constant (gen_lowpart (GET_MODE (target), target), 1);
3242 emit_move_insn (target, force_operand (tem, NULL_RTX));
3248 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3249 NULL_RTX if we failed the caller should emit a normal call, otherwise
3250 try to get the result in TARGET, if convenient (and in mode MODE if that's
3254 expand_builtin_strcpy (tree exp, rtx target)
3256 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3258 tree dest = CALL_EXPR_ARG (exp, 0);
3259 tree src = CALL_EXPR_ARG (exp, 1);
3260 return expand_builtin_strcpy_args (dest, src, target);
3265 /* Helper function to do the actual work for expand_builtin_strcpy. The
3266 arguments to the builtin_strcpy call DEST and SRC are broken out
3267 so that this can also be called without constructing an actual CALL_EXPR.
3268 The other arguments and return value are the same as for
3269 expand_builtin_strcpy. */
3272 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3274 return expand_movstr (dest, src, target, /*endp=*/0);
3277 /* Expand a call EXP to the stpcpy builtin.
3278 Return NULL_RTX if we failed the caller should emit a normal call,
3279 otherwise try to get the result in TARGET, if convenient (and in
3280 mode MODE if that's convenient). */
3283 expand_builtin_stpcpy (tree exp, rtx target, enum machine_mode mode)
3286 location_t loc = EXPR_LOCATION (exp);
3288 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3291 dst = CALL_EXPR_ARG (exp, 0);
3292 src = CALL_EXPR_ARG (exp, 1);
3294 /* If return value is ignored, transform stpcpy into strcpy. */
3295 if (target == const0_rtx && implicit_built_in_decls[BUILT_IN_STRCPY])
3297 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
3298 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3299 return expand_expr (result, target, mode, EXPAND_NORMAL);
3306 /* Ensure we get an actual string whose length can be evaluated at
3307 compile-time, not an expression containing a string. This is
3308 because the latter will potentially produce pessimized code
3309 when used to produce the return value. */
3310 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3311 return expand_movstr (dst, src, target, /*endp=*/2);
3313 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3314 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3315 target, mode, /*endp=*/2);
3320 if (TREE_CODE (len) == INTEGER_CST)
3322 rtx len_rtx = expand_normal (len);
3324 if (CONST_INT_P (len_rtx))
3326 ret = expand_builtin_strcpy_args (dst, src, target);
3332 if (mode != VOIDmode)
3333 target = gen_reg_rtx (mode);
3335 target = gen_reg_rtx (GET_MODE (ret));
3337 if (GET_MODE (target) != GET_MODE (ret))
3338 ret = gen_lowpart (GET_MODE (target), ret);
3340 ret = plus_constant (ret, INTVAL (len_rtx));
3341 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3349 return expand_movstr (dst, src, target, /*endp=*/2);
3353 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3354 bytes from constant string DATA + OFFSET and return it as target
3358 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3359 enum machine_mode mode)
3361 const char *str = (const char *) data;
3363 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3366 return c_readstr (str + offset, mode);
3369 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3370 NULL_RTX if we failed the caller should emit a normal call. */
3373 expand_builtin_strncpy (tree exp, rtx target)
3375 location_t loc = EXPR_LOCATION (exp);
3377 if (validate_arglist (exp,
3378 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3380 tree dest = CALL_EXPR_ARG (exp, 0);
3381 tree src = CALL_EXPR_ARG (exp, 1);
3382 tree len = CALL_EXPR_ARG (exp, 2);
3383 tree slen = c_strlen (src, 1);
3385 /* We must be passed a constant len and src parameter. */
3386 if (!host_integerp (len, 1) || !slen || !host_integerp (slen, 1))
3389 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3391 /* We're required to pad with trailing zeros if the requested
3392 len is greater than strlen(s2)+1. In that case try to
3393 use store_by_pieces, if it fails, punt. */
3394 if (tree_int_cst_lt (slen, len))
3396 unsigned int dest_align = get_pointer_alignment (dest);
3397 const char *p = c_getstr (src);
3400 if (!p || dest_align == 0 || !host_integerp (len, 1)
3401 || !can_store_by_pieces (tree_low_cst (len, 1),
3402 builtin_strncpy_read_str,
3403 CONST_CAST (char *, p),
3407 dest_mem = get_memory_rtx (dest, len);
3408 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3409 builtin_strncpy_read_str,
3410 CONST_CAST (char *, p), dest_align, false, 0);
3411 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3412 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3419 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3420 bytes from constant string DATA + OFFSET and return it as target
3424 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3425 enum machine_mode mode)
3427 const char *c = (const char *) data;
3428 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
3430 memset (p, *c, GET_MODE_SIZE (mode));
3432 return c_readstr (p, mode);
3435 /* Callback routine for store_by_pieces. Return the RTL of a register
3436 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3437 char value given in the RTL register data. For example, if mode is
3438 4 bytes wide, return the RTL for 0x01010101*data. */
3441 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3442 enum machine_mode mode)
3448 size = GET_MODE_SIZE (mode);
3452 p = XALLOCAVEC (char, size);
3453 memset (p, 1, size);
3454 coeff = c_readstr (p, mode);
3456 target = convert_to_mode (mode, (rtx) data, 1);
3457 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3458 return force_reg (mode, target);
3461 /* Expand expression EXP, which is a call to the memset builtin. Return
3462 NULL_RTX if we failed the caller should emit a normal call, otherwise
3463 try to get the result in TARGET, if convenient (and in mode MODE if that's
3467 expand_builtin_memset (tree exp, rtx target, enum machine_mode mode)
3469 if (!validate_arglist (exp,
3470 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3474 tree dest = CALL_EXPR_ARG (exp, 0);
3475 tree val = CALL_EXPR_ARG (exp, 1);
3476 tree len = CALL_EXPR_ARG (exp, 2);
3477 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3481 /* Helper function to do the actual work for expand_builtin_memset. The
3482 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3483 so that this can also be called without constructing an actual CALL_EXPR.
3484 The other arguments and return value are the same as for
3485 expand_builtin_memset. */
3488 expand_builtin_memset_args (tree dest, tree val, tree len,
3489 rtx target, enum machine_mode mode, tree orig_exp)
3492 enum built_in_function fcode;
3493 enum machine_mode val_mode;
3495 unsigned int dest_align;
3496 rtx dest_mem, dest_addr, len_rtx;
3497 HOST_WIDE_INT expected_size = -1;
3498 unsigned int expected_align = 0;
3500 dest_align = get_pointer_alignment (dest);
3502 /* If DEST is not a pointer type, don't do this operation in-line. */
3503 if (dest_align == 0)
3506 if (currently_expanding_gimple_stmt)
3507 stringop_block_profile (currently_expanding_gimple_stmt,
3508 &expected_align, &expected_size);
3510 if (expected_align < dest_align)
3511 expected_align = dest_align;
3513 /* If the LEN parameter is zero, return DEST. */
3514 if (integer_zerop (len))
3516 /* Evaluate and ignore VAL in case it has side-effects. */
3517 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
3518 return expand_expr (dest, target, mode, EXPAND_NORMAL);
3521 /* Stabilize the arguments in case we fail. */
3522 dest = builtin_save_expr (dest);
3523 val = builtin_save_expr (val);
3524 len = builtin_save_expr (len);
3526 len_rtx = expand_normal (len);
3527 dest_mem = get_memory_rtx (dest, len);
3528 val_mode = TYPE_MODE (unsigned_char_type_node);
3530 if (TREE_CODE (val) != INTEGER_CST)
3534 val_rtx = expand_normal (val);
3535 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
3537 /* Assume that we can memset by pieces if we can store
3538 * the coefficients by pieces (in the required modes).
3539 * We can't pass builtin_memset_gen_str as that emits RTL. */
3541 if (host_integerp (len, 1)
3542 && can_store_by_pieces (tree_low_cst (len, 1),
3543 builtin_memset_read_str, &c, dest_align,
3546 val_rtx = force_reg (val_mode, val_rtx);
3547 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3548 builtin_memset_gen_str, val_rtx, dest_align,
3551 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
3552 dest_align, expected_align,
3556 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3557 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3561 if (target_char_cast (val, &c))
3566 if (host_integerp (len, 1)
3567 && can_store_by_pieces (tree_low_cst (len, 1),
3568 builtin_memset_read_str, &c, dest_align,
3570 store_by_pieces (dest_mem, tree_low_cst (len, 1),
3571 builtin_memset_read_str, &c, dest_align, true, 0);
3572 else if (!set_storage_via_setmem (dest_mem, len_rtx,
3573 gen_int_mode (c, val_mode),
3574 dest_align, expected_align,
3578 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3579 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3583 set_mem_align (dest_mem, dest_align);
3584 dest_addr = clear_storage_hints (dest_mem, len_rtx,
3585 CALL_EXPR_TAILCALL (orig_exp)
3586 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
3587 expected_align, expected_size);
3591 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
3592 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3598 fndecl = get_callee_fndecl (orig_exp);
3599 fcode = DECL_FUNCTION_CODE (fndecl);
3600 if (fcode == BUILT_IN_MEMSET)
3601 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
3603 else if (fcode == BUILT_IN_BZERO)
3604 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
3608 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3609 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
3610 return expand_call (fn, target, target == const0_rtx);
3613 /* Expand expression EXP, which is a call to the bzero builtin. Return
3614 NULL_RTX if we failed the caller should emit a normal call. */
3617 expand_builtin_bzero (tree exp)
3620 location_t loc = EXPR_LOCATION (exp);
3622 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3625 dest = CALL_EXPR_ARG (exp, 0);
3626 size = CALL_EXPR_ARG (exp, 1);
3628 /* New argument list transforming bzero(ptr x, int y) to
3629 memset(ptr x, int 0, size_t y). This is done this way
3630 so that if it isn't expanded inline, we fallback to
3631 calling bzero instead of memset. */
3633 return expand_builtin_memset_args (dest, integer_zero_node,
3634 fold_convert_loc (loc,
3635 size_type_node, size),
3636 const0_rtx, VOIDmode, exp);
3639 /* Expand expression EXP, which is a call to the memcmp built-in function.
3640 Return NULL_RTX if we failed and the caller should emit a normal call,
3641 otherwise try to get the result in TARGET, if convenient (and in mode
3642 MODE, if that's convenient). */
3645 expand_builtin_memcmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3646 ATTRIBUTE_UNUSED enum machine_mode mode)
3648 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3650 if (!validate_arglist (exp,
3651 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3654 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3655 implementing memcmp because it will stop if it encounters two
3657 #if defined HAVE_cmpmemsi
3659 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3662 tree arg1 = CALL_EXPR_ARG (exp, 0);
3663 tree arg2 = CALL_EXPR_ARG (exp, 1);
3664 tree len = CALL_EXPR_ARG (exp, 2);
3666 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3667 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3668 enum machine_mode insn_mode;
3671 insn_mode = insn_data[(int) CODE_FOR_cmpmemsi].operand[0].mode;
3675 /* If we don't have POINTER_TYPE, call the function. */
3676 if (arg1_align == 0 || arg2_align == 0)
3679 /* Make a place to write the result of the instruction. */
3682 && REG_P (result) && GET_MODE (result) == insn_mode
3683 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3684 result = gen_reg_rtx (insn_mode);
3686 arg1_rtx = get_memory_rtx (arg1, len);
3687 arg2_rtx = get_memory_rtx (arg2, len);
3688 arg3_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
3690 /* Set MEM_SIZE as appropriate. */
3691 if (CONST_INT_P (arg3_rtx))
3693 set_mem_size (arg1_rtx, INTVAL (arg3_rtx));
3694 set_mem_size (arg2_rtx, INTVAL (arg3_rtx));
3698 insn = gen_cmpmemsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3699 GEN_INT (MIN (arg1_align, arg2_align)));
3706 emit_library_call_value (memcmp_libfunc, result, LCT_PURE,
3707 TYPE_MODE (integer_type_node), 3,
3708 XEXP (arg1_rtx, 0), Pmode,
3709 XEXP (arg2_rtx, 0), Pmode,
3710 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
3711 TYPE_UNSIGNED (sizetype)),
3712 TYPE_MODE (sizetype));
3714 /* Return the value in the proper mode for this function. */
3715 mode = TYPE_MODE (TREE_TYPE (exp));
3716 if (GET_MODE (result) == mode)
3718 else if (target != 0)
3720 convert_move (target, result, 0);
3724 return convert_to_mode (mode, result, 0);
3726 #endif /* HAVE_cmpmemsi. */
3731 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3732 if we failed the caller should emit a normal call, otherwise try to get
3733 the result in TARGET, if convenient. */
3736 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
3738 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3741 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3742 if (direct_optab_handler (cmpstr_optab, SImode) != CODE_FOR_nothing
3743 || direct_optab_handler (cmpstrn_optab, SImode) != CODE_FOR_nothing)
3745 rtx arg1_rtx, arg2_rtx;
3746 rtx result, insn = NULL_RTX;
3748 tree arg1 = CALL_EXPR_ARG (exp, 0);
3749 tree arg2 = CALL_EXPR_ARG (exp, 1);
3751 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3752 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3754 /* If we don't have POINTER_TYPE, call the function. */
3755 if (arg1_align == 0 || arg2_align == 0)
3758 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3759 arg1 = builtin_save_expr (arg1);
3760 arg2 = builtin_save_expr (arg2);
3762 arg1_rtx = get_memory_rtx (arg1, NULL);
3763 arg2_rtx = get_memory_rtx (arg2, NULL);
3765 #ifdef HAVE_cmpstrsi
3766 /* Try to call cmpstrsi. */
3769 enum machine_mode insn_mode
3770 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3772 /* Make a place to write the result of the instruction. */
3775 && REG_P (result) && GET_MODE (result) == insn_mode
3776 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3777 result = gen_reg_rtx (insn_mode);
3779 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx,
3780 GEN_INT (MIN (arg1_align, arg2_align)));
3783 #ifdef HAVE_cmpstrnsi
3784 /* Try to determine at least one length and call cmpstrnsi. */
3785 if (!insn && HAVE_cmpstrnsi)
3790 enum machine_mode insn_mode
3791 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3792 tree len1 = c_strlen (arg1, 1);
3793 tree len2 = c_strlen (arg2, 1);
3796 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
3798 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
3800 /* If we don't have a constant length for the first, use the length
3801 of the second, if we know it. We don't require a constant for
3802 this case; some cost analysis could be done if both are available
3803 but neither is constant. For now, assume they're equally cheap,
3804 unless one has side effects. If both strings have constant lengths,
3811 else if (TREE_SIDE_EFFECTS (len1))
3813 else if (TREE_SIDE_EFFECTS (len2))
3815 else if (TREE_CODE (len1) != INTEGER_CST)
3817 else if (TREE_CODE (len2) != INTEGER_CST)
3819 else if (tree_int_cst_lt (len1, len2))
3824 /* If both arguments have side effects, we cannot optimize. */
3825 if (!len || TREE_SIDE_EFFECTS (len))
3828 arg3_rtx = expand_normal (len);
3830 /* Make a place to write the result of the instruction. */
3833 && REG_P (result) && GET_MODE (result) == insn_mode
3834 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3835 result = gen_reg_rtx (insn_mode);
3837 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3838 GEN_INT (MIN (arg1_align, arg2_align)));
3844 enum machine_mode mode;
3847 /* Return the value in the proper mode for this function. */
3848 mode = TYPE_MODE (TREE_TYPE (exp));
3849 if (GET_MODE (result) == mode)
3852 return convert_to_mode (mode, result, 0);
3853 convert_move (target, result, 0);
3857 /* Expand the library call ourselves using a stabilized argument
3858 list to avoid re-evaluating the function's arguments twice. */
3859 #ifdef HAVE_cmpstrnsi
3862 fndecl = get_callee_fndecl (exp);
3863 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
3864 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3865 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3866 return expand_call (fn, target, target == const0_rtx);
3872 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3873 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3874 the result in TARGET, if convenient. */
3877 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
3878 ATTRIBUTE_UNUSED enum machine_mode mode)
3880 location_t loc ATTRIBUTE_UNUSED = EXPR_LOCATION (exp);
3882 if (!validate_arglist (exp,
3883 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3886 /* If c_strlen can determine an expression for one of the string
3887 lengths, and it doesn't have side effects, then emit cmpstrnsi
3888 using length MIN(strlen(string)+1, arg3). */
3889 #ifdef HAVE_cmpstrnsi
3892 tree len, len1, len2;
3893 rtx arg1_rtx, arg2_rtx, arg3_rtx;
3896 tree arg1 = CALL_EXPR_ARG (exp, 0);
3897 tree arg2 = CALL_EXPR_ARG (exp, 1);
3898 tree arg3 = CALL_EXPR_ARG (exp, 2);
3900 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
3901 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
3902 enum machine_mode insn_mode
3903 = insn_data[(int) CODE_FOR_cmpstrnsi].operand[0].mode;
3905 len1 = c_strlen (arg1, 1);
3906 len2 = c_strlen (arg2, 1);
3909 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
3911 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
3913 /* If we don't have a constant length for the first, use the length
3914 of the second, if we know it. We don't require a constant for
3915 this case; some cost analysis could be done if both are available
3916 but neither is constant. For now, assume they're equally cheap,
3917 unless one has side effects. If both strings have constant lengths,
3924 else if (TREE_SIDE_EFFECTS (len1))
3926 else if (TREE_SIDE_EFFECTS (len2))
3928 else if (TREE_CODE (len1) != INTEGER_CST)
3930 else if (TREE_CODE (len2) != INTEGER_CST)
3932 else if (tree_int_cst_lt (len1, len2))
3937 /* If both arguments have side effects, we cannot optimize. */
3938 if (!len || TREE_SIDE_EFFECTS (len))
3941 /* The actual new length parameter is MIN(len,arg3). */
3942 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len,
3943 fold_convert_loc (loc, TREE_TYPE (len), arg3));
3945 /* If we don't have POINTER_TYPE, call the function. */
3946 if (arg1_align == 0 || arg2_align == 0)
3949 /* Make a place to write the result of the instruction. */
3952 && REG_P (result) && GET_MODE (result) == insn_mode
3953 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
3954 result = gen_reg_rtx (insn_mode);
3956 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3957 arg1 = builtin_save_expr (arg1);
3958 arg2 = builtin_save_expr (arg2);
3959 len = builtin_save_expr (len);
3961 arg1_rtx = get_memory_rtx (arg1, len);
3962 arg2_rtx = get_memory_rtx (arg2, len);
3963 arg3_rtx = expand_normal (len);
3964 insn = gen_cmpstrnsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
3965 GEN_INT (MIN (arg1_align, arg2_align)));
3970 /* Return the value in the proper mode for this function. */
3971 mode = TYPE_MODE (TREE_TYPE (exp));
3972 if (GET_MODE (result) == mode)
3975 return convert_to_mode (mode, result, 0);
3976 convert_move (target, result, 0);
3980 /* Expand the library call ourselves using a stabilized argument
3981 list to avoid re-evaluating the function's arguments twice. */
3982 fndecl = get_callee_fndecl (exp);
3983 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 3,
3985 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
3986 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
3987 return expand_call (fn, target, target == const0_rtx);
3993 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
3994 if that's convenient. */
3997 expand_builtin_saveregs (void)
4001 /* Don't do __builtin_saveregs more than once in a function.
4002 Save the result of the first call and reuse it. */
4003 if (saveregs_value != 0)
4004 return saveregs_value;
4006 /* When this function is called, it means that registers must be
4007 saved on entry to this function. So we migrate the call to the
4008 first insn of this function. */
4012 /* Do whatever the machine needs done in this case. */
4013 val = targetm.calls.expand_builtin_saveregs ();
4018 saveregs_value = val;
4020 /* Put the insns after the NOTE that starts the function. If this
4021 is inside a start_sequence, make the outer-level insn chain current, so
4022 the code is placed at the start of the function. */
4023 push_topmost_sequence ();
4024 emit_insn_after (seq, entry_of_function ());
4025 pop_topmost_sequence ();
4030 /* Expand a call to __builtin_next_arg. */
4033 expand_builtin_next_arg (void)
4035 /* Checking arguments is already done in fold_builtin_next_arg
4036 that must be called before this function. */
4037 return expand_binop (ptr_mode, add_optab,
4038 crtl->args.internal_arg_pointer,
4039 crtl->args.arg_offset_rtx,
4040 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4043 /* Make it easier for the backends by protecting the valist argument
4044 from multiple evaluations. */
4047 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4049 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4051 /* The current way of determining the type of valist is completely
4052 bogus. We should have the information on the va builtin instead. */
4054 vatype = targetm.fn_abi_va_list (cfun->decl);
4056 if (TREE_CODE (vatype) == ARRAY_TYPE)
4058 if (TREE_SIDE_EFFECTS (valist))
4059 valist = save_expr (valist);
4061 /* For this case, the backends will be expecting a pointer to
4062 vatype, but it's possible we've actually been given an array
4063 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4065 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4067 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4068 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4073 tree pt = build_pointer_type (vatype);
4077 if (! TREE_SIDE_EFFECTS (valist))
4080 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4081 TREE_SIDE_EFFECTS (valist) = 1;
4084 if (TREE_SIDE_EFFECTS (valist))
4085 valist = save_expr (valist);
4086 valist = fold_build2_loc (loc, MEM_REF,
4087 vatype, valist, build_int_cst (pt, 0));
4093 /* The "standard" definition of va_list is void*. */
4096 std_build_builtin_va_list (void)
4098 return ptr_type_node;
4101 /* The "standard" abi va_list is va_list_type_node. */
4104 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4106 return va_list_type_node;
4109 /* The "standard" type of va_list is va_list_type_node. */
4112 std_canonical_va_list_type (tree type)
4116 if (INDIRECT_REF_P (type))
4117 type = TREE_TYPE (type);
4118 else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
4119 type = TREE_TYPE (type);
4120 wtype = va_list_type_node;
4122 /* Treat structure va_list types. */
4123 if (TREE_CODE (wtype) == RECORD_TYPE && POINTER_TYPE_P (htype))
4124 htype = TREE_TYPE (htype);
4125 else if (TREE_CODE (wtype) == ARRAY_TYPE)
4127 /* If va_list is an array type, the argument may have decayed
4128 to a pointer type, e.g. by being passed to another function.
4129 In that case, unwrap both types so that we can compare the
4130 underlying records. */
4131 if (TREE_CODE (htype) == ARRAY_TYPE
4132 || POINTER_TYPE_P (htype))
4134 wtype = TREE_TYPE (wtype);
4135 htype = TREE_TYPE (htype);
4138 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4139 return va_list_type_node;
4144 /* The "standard" implementation of va_start: just assign `nextarg' to
4148 std_expand_builtin_va_start (tree valist, rtx nextarg)
4150 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4151 convert_move (va_r, nextarg, 0);
4154 /* Expand EXP, a call to __builtin_va_start. */
4157 expand_builtin_va_start (tree exp)
4161 location_t loc = EXPR_LOCATION (exp);
4163 if (call_expr_nargs (exp) < 2)
4165 error_at (loc, "too few arguments to function %<va_start%>");
4169 if (fold_builtin_next_arg (exp, true))
4172 nextarg = expand_builtin_next_arg ();
4173 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4175 if (targetm.expand_builtin_va_start)
4176 targetm.expand_builtin_va_start (valist, nextarg);
4178 std_expand_builtin_va_start (valist, nextarg);
4183 /* The "standard" implementation of va_arg: read the value from the
4184 current (padded) address and increment by the (padded) size. */
4187 std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
4190 tree addr, t, type_size, rounded_size, valist_tmp;
4191 unsigned HOST_WIDE_INT align, boundary;
4194 #ifdef ARGS_GROW_DOWNWARD
4195 /* All of the alignment and movement below is for args-grow-up machines.
4196 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4197 implement their own specialized gimplify_va_arg_expr routines. */
4201 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4203 type = build_pointer_type (type);
4205 align = PARM_BOUNDARY / BITS_PER_UNIT;
4206 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
4208 /* When we align parameter on stack for caller, if the parameter
4209 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4210 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4211 here with caller. */
4212 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
4213 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
4215 boundary /= BITS_PER_UNIT;
4217 /* Hoist the valist value into a temporary for the moment. */
4218 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
4220 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4221 requires greater alignment, we must perform dynamic alignment. */
4222 if (boundary > align
4223 && !integer_zerop (TYPE_SIZE (type)))
4225 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4226 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
4227 gimplify_and_add (t, pre_p);
4229 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
4230 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
4232 build_int_cst (TREE_TYPE (valist), -boundary)));
4233 gimplify_and_add (t, pre_p);
4238 /* If the actual alignment is less than the alignment of the type,
4239 adjust the type accordingly so that we don't assume strict alignment
4240 when dereferencing the pointer. */
4241 boundary *= BITS_PER_UNIT;
4242 if (boundary < TYPE_ALIGN (type))
4244 type = build_variant_type_copy (type);
4245 TYPE_ALIGN (type) = boundary;
4248 /* Compute the rounded size of the type. */
4249 type_size = size_in_bytes (type);
4250 rounded_size = round_up (type_size, align);
4252 /* Reduce rounded_size so it's sharable with the postqueue. */
4253 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
4257 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
4259 /* Small args are padded downward. */
4260 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
4261 rounded_size, size_int (align));
4262 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
4263 size_binop (MINUS_EXPR, rounded_size, type_size));
4264 addr = fold_build_pointer_plus (addr, t);
4267 /* Compute new value for AP. */
4268 t = fold_build_pointer_plus (valist_tmp, rounded_size);
4269 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
4270 gimplify_and_add (t, pre_p);
4272 addr = fold_convert (build_pointer_type (type), addr);
4275 addr = build_va_arg_indirect_ref (addr);
4277 return build_va_arg_indirect_ref (addr);
4280 /* Build an indirect-ref expression over the given TREE, which represents a
4281 piece of a va_arg() expansion. */
4283 build_va_arg_indirect_ref (tree addr)
4285 addr = build_simple_mem_ref_loc (EXPR_LOCATION (addr), addr);
4287 if (flag_mudflap) /* Don't instrument va_arg INDIRECT_REF. */
4293 /* Return a dummy expression of type TYPE in order to keep going after an
4297 dummy_object (tree type)
4299 tree t = build_int_cst (build_pointer_type (type), 0);
4300 return build2 (MEM_REF, type, t, t);
4303 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4304 builtin function, but a very special sort of operator. */
4306 enum gimplify_status
4307 gimplify_va_arg_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
4309 tree promoted_type, have_va_type;
4310 tree valist = TREE_OPERAND (*expr_p, 0);
4311 tree type = TREE_TYPE (*expr_p);
4313 location_t loc = EXPR_LOCATION (*expr_p);
4315 /* Verify that valist is of the proper type. */
4316 have_va_type = TREE_TYPE (valist);
4317 if (have_va_type == error_mark_node)
4319 have_va_type = targetm.canonical_va_list_type (have_va_type);
4321 if (have_va_type == NULL_TREE)
4323 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4327 /* Generate a diagnostic for requesting data of a type that cannot
4328 be passed through `...' due to type promotion at the call site. */
4329 if ((promoted_type = lang_hooks.types.type_promotes_to (type))
4332 static bool gave_help;
4335 /* Unfortunately, this is merely undefined, rather than a constraint
4336 violation, so we cannot make this an error. If this call is never
4337 executed, the program is still strictly conforming. */
4338 warned = warning_at (loc, 0,
4339 "%qT is promoted to %qT when passed through %<...%>",
4340 type, promoted_type);
4341 if (!gave_help && warned)
4344 inform (loc, "(so you should pass %qT not %qT to %<va_arg%>)",
4345 promoted_type, type);
4348 /* We can, however, treat "undefined" any way we please.
4349 Call abort to encourage the user to fix the program. */
4351 inform (loc, "if this code is reached, the program will abort");
4352 /* Before the abort, allow the evaluation of the va_list
4353 expression to exit or longjmp. */
4354 gimplify_and_add (valist, pre_p);
4355 t = build_call_expr_loc (loc,
4356 implicit_built_in_decls[BUILT_IN_TRAP], 0);
4357 gimplify_and_add (t, pre_p);
4359 /* This is dead code, but go ahead and finish so that the
4360 mode of the result comes out right. */
4361 *expr_p = dummy_object (type);
4366 /* Make it easier for the backends by protecting the valist argument
4367 from multiple evaluations. */
4368 if (TREE_CODE (have_va_type) == ARRAY_TYPE)
4370 /* For this case, the backends will be expecting a pointer to
4371 TREE_TYPE (abi), but it's possible we've
4372 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4374 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4376 tree p1 = build_pointer_type (TREE_TYPE (have_va_type));
4377 valist = fold_convert_loc (loc, p1,
4378 build_fold_addr_expr_loc (loc, valist));
4381 gimplify_expr (&valist, pre_p, post_p, is_gimple_val, fb_rvalue);
4384 gimplify_expr (&valist, pre_p, post_p, is_gimple_min_lval, fb_lvalue);
4386 if (!targetm.gimplify_va_arg_expr)
4387 /* FIXME: Once most targets are converted we should merely
4388 assert this is non-null. */
4391 *expr_p = targetm.gimplify_va_arg_expr (valist, type, pre_p, post_p);
4396 /* Expand EXP, a call to __builtin_va_end. */
4399 expand_builtin_va_end (tree exp)
4401 tree valist = CALL_EXPR_ARG (exp, 0);
4403 /* Evaluate for side effects, if needed. I hate macros that don't
4405 if (TREE_SIDE_EFFECTS (valist))
4406 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4411 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4412 builtin rather than just as an assignment in stdarg.h because of the
4413 nastiness of array-type va_list types. */
4416 expand_builtin_va_copy (tree exp)
4419 location_t loc = EXPR_LOCATION (exp);
4421 dst = CALL_EXPR_ARG (exp, 0);
4422 src = CALL_EXPR_ARG (exp, 1);
4424 dst = stabilize_va_list_loc (loc, dst, 1);
4425 src = stabilize_va_list_loc (loc, src, 0);
4427 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4429 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4431 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4432 TREE_SIDE_EFFECTS (t) = 1;
4433 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4437 rtx dstb, srcb, size;
4439 /* Evaluate to pointers. */
4440 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4441 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4442 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4443 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4445 dstb = convert_memory_address (Pmode, dstb);
4446 srcb = convert_memory_address (Pmode, srcb);
4448 /* "Dereference" to BLKmode memories. */
4449 dstb = gen_rtx_MEM (BLKmode, dstb);
4450 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4451 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4452 srcb = gen_rtx_MEM (BLKmode, srcb);
4453 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4454 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4457 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4463 /* Expand a call to one of the builtin functions __builtin_frame_address or
4464 __builtin_return_address. */
4467 expand_builtin_frame_address (tree fndecl, tree exp)
4469 /* The argument must be a nonnegative integer constant.
4470 It counts the number of frames to scan up the stack.
4471 The value is the return address saved in that frame. */
4472 if (call_expr_nargs (exp) == 0)
4473 /* Warning about missing arg was already issued. */
4475 else if (! host_integerp (CALL_EXPR_ARG (exp, 0), 1))
4477 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4478 error ("invalid argument to %<__builtin_frame_address%>");
4480 error ("invalid argument to %<__builtin_return_address%>");
4486 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
4487 tree_low_cst (CALL_EXPR_ARG (exp, 0), 1));
4489 /* Some ports cannot access arbitrary stack frames. */
4492 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4493 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4495 warning (0, "unsupported argument to %<__builtin_return_address%>");
4499 /* For __builtin_frame_address, return what we've got. */
4500 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4504 && ! CONSTANT_P (tem))
4505 tem = copy_to_mode_reg (Pmode, tem);
4510 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4511 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4512 is the same as for allocate_dynamic_stack_space. */
4515 expand_builtin_alloca (tree exp, bool cannot_accumulate)
4521 bool alloca_with_align = (DECL_FUNCTION_CODE (get_callee_fndecl (exp))
4522 == BUILT_IN_ALLOCA_WITH_ALIGN);
4524 /* Emit normal call if marked not-inlineable. */
4525 if (CALL_CANNOT_INLINE_P (exp))
4529 = (alloca_with_align
4530 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4531 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4536 /* Compute the argument. */
4537 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4539 /* Compute the alignment. */
4540 align = (alloca_with_align
4541 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1))
4542 : BIGGEST_ALIGNMENT);
4544 /* Allocate the desired space. */
4545 result = allocate_dynamic_stack_space (op0, 0, align, cannot_accumulate);
4546 result = convert_memory_address (ptr_mode, result);
4551 /* Expand a call to a bswap builtin with argument ARG0. MODE
4552 is the mode to expand with. */
4555 expand_builtin_bswap (tree exp, rtx target, rtx subtarget)
4557 enum machine_mode mode;
4561 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4564 arg = CALL_EXPR_ARG (exp, 0);
4565 mode = TYPE_MODE (TREE_TYPE (arg));
4566 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4568 target = expand_unop (mode, bswap_optab, op0, target, 1);
4570 gcc_assert (target);
4572 return convert_to_mode (mode, target, 0);
4575 /* Expand a call to a unary builtin in EXP.
4576 Return NULL_RTX if a normal call should be emitted rather than expanding the
4577 function in-line. If convenient, the result should be placed in TARGET.
4578 SUBTARGET may be used as the target for computing one of EXP's operands. */
4581 expand_builtin_unop (enum machine_mode target_mode, tree exp, rtx target,
4582 rtx subtarget, optab op_optab)
4586 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
4589 /* Compute the argument. */
4590 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
4592 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
4593 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
4594 VOIDmode, EXPAND_NORMAL);
4595 /* Compute op, into TARGET if possible.
4596 Set TARGET to wherever the result comes back. */
4597 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
4598 op_optab, op0, target, op_optab != clrsb_optab);
4599 gcc_assert (target);
4601 return convert_to_mode (target_mode, target, 0);
4604 /* Expand a call to __builtin_expect. We just return our argument
4605 as the builtin_expect semantic should've been already executed by
4606 tree branch prediction pass. */
4609 expand_builtin_expect (tree exp, rtx target)
4613 if (call_expr_nargs (exp) < 2)
4615 arg = CALL_EXPR_ARG (exp, 0);
4617 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4618 /* When guessing was done, the hints should be already stripped away. */
4619 gcc_assert (!flag_guess_branch_prob
4620 || optimize == 0 || seen_error ());
4624 /* Expand a call to __builtin_assume_aligned. We just return our first
4625 argument as the builtin_assume_aligned semantic should've been already
4629 expand_builtin_assume_aligned (tree exp, rtx target)
4631 if (call_expr_nargs (exp) < 2)
4633 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
4635 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
4636 && (call_expr_nargs (exp) < 3
4637 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
4642 expand_builtin_trap (void)
4646 emit_insn (gen_trap ());
4649 emit_library_call (abort_libfunc, LCT_NORETURN, VOIDmode, 0);
4653 /* Expand a call to __builtin_unreachable. We do nothing except emit
4654 a barrier saying that control flow will not pass here.
4656 It is the responsibility of the program being compiled to ensure
4657 that control flow does never reach __builtin_unreachable. */
4659 expand_builtin_unreachable (void)
4664 /* Expand EXP, a call to fabs, fabsf or fabsl.
4665 Return NULL_RTX if a normal call should be emitted rather than expanding
4666 the function inline. If convenient, the result should be placed
4667 in TARGET. SUBTARGET may be used as the target for computing
4671 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
4673 enum machine_mode mode;
4677 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4680 arg = CALL_EXPR_ARG (exp, 0);
4681 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
4682 mode = TYPE_MODE (TREE_TYPE (arg));
4683 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4684 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
4687 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4688 Return NULL is a normal call should be emitted rather than expanding the
4689 function inline. If convenient, the result should be placed in TARGET.
4690 SUBTARGET may be used as the target for computing the operand. */
4693 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
4698 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
4701 arg = CALL_EXPR_ARG (exp, 0);
4702 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
4704 arg = CALL_EXPR_ARG (exp, 1);
4705 op1 = expand_normal (arg);
4707 return expand_copysign (op0, op1, target);
4710 /* Create a new constant string literal and return a char* pointer to it.
4711 The STRING_CST value is the LEN characters at STR. */
4713 build_string_literal (int len, const char *str)
4715 tree t, elem, index, type;
4717 t = build_string (len, str);
4718 elem = build_type_variant (char_type_node, 1, 0);
4719 index = build_index_type (size_int (len - 1));
4720 type = build_array_type (elem, index);
4721 TREE_TYPE (t) = type;
4722 TREE_CONSTANT (t) = 1;
4723 TREE_READONLY (t) = 1;
4724 TREE_STATIC (t) = 1;
4726 type = build_pointer_type (elem);
4727 t = build1 (ADDR_EXPR, type,
4728 build4 (ARRAY_REF, elem,
4729 t, integer_zero_node, NULL_TREE, NULL_TREE));
4733 /* Expand a call to __builtin___clear_cache. */
4736 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED)
4738 #ifndef HAVE_clear_cache
4739 #ifdef CLEAR_INSN_CACHE
4740 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4741 does something. Just do the default expansion to a call to
4745 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4746 does nothing. There is no need to call it. Do nothing. */
4748 #endif /* CLEAR_INSN_CACHE */
4750 /* We have a "clear_cache" insn, and it will handle everything. */
4752 rtx begin_rtx, end_rtx;
4754 /* We must not expand to a library call. If we did, any
4755 fallback library function in libgcc that might contain a call to
4756 __builtin___clear_cache() would recurse infinitely. */
4757 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4759 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4763 if (HAVE_clear_cache)
4765 struct expand_operand ops[2];
4767 begin = CALL_EXPR_ARG (exp, 0);
4768 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
4770 end = CALL_EXPR_ARG (exp, 1);
4771 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
4773 create_address_operand (&ops[0], begin_rtx);
4774 create_address_operand (&ops[1], end_rtx);
4775 if (maybe_expand_insn (CODE_FOR_clear_cache, 2, ops))
4779 #endif /* HAVE_clear_cache */
4782 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4785 round_trampoline_addr (rtx tramp)
4787 rtx temp, addend, mask;
4789 /* If we don't need too much alignment, we'll have been guaranteed
4790 proper alignment by get_trampoline_type. */
4791 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
4794 /* Round address up to desired boundary. */
4795 temp = gen_reg_rtx (Pmode);
4796 addend = GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1);
4797 mask = GEN_INT (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
4799 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
4800 temp, 0, OPTAB_LIB_WIDEN);
4801 tramp = expand_simple_binop (Pmode, AND, temp, mask,
4802 temp, 0, OPTAB_LIB_WIDEN);
4808 expand_builtin_init_trampoline (tree exp)
4810 tree t_tramp, t_func, t_chain;
4811 rtx m_tramp, r_tramp, r_chain, tmp;
4813 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
4814 POINTER_TYPE, VOID_TYPE))
4817 t_tramp = CALL_EXPR_ARG (exp, 0);
4818 t_func = CALL_EXPR_ARG (exp, 1);
4819 t_chain = CALL_EXPR_ARG (exp, 2);
4821 r_tramp = expand_normal (t_tramp);
4822 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
4823 MEM_NOTRAP_P (m_tramp) = 1;
4825 /* The TRAMP argument should be the address of a field within the
4826 local function's FRAME decl. Let's see if we can fill in the
4827 to fill in the MEM_ATTRs for this memory. */
4828 if (TREE_CODE (t_tramp) == ADDR_EXPR)
4829 set_mem_attributes_minus_bitpos (m_tramp, TREE_OPERAND (t_tramp, 0),
4832 tmp = round_trampoline_addr (r_tramp);
4835 m_tramp = change_address (m_tramp, BLKmode, tmp);
4836 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
4837 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
4840 /* The FUNC argument should be the address of the nested function.
4841 Extract the actual function decl to pass to the hook. */
4842 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
4843 t_func = TREE_OPERAND (t_func, 0);
4844 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
4846 r_chain = expand_normal (t_chain);
4848 /* Generate insns to initialize the trampoline. */
4849 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
4851 trampolines_created = 1;
4853 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
4854 "trampoline generated for nested function %qD", t_func);
4860 expand_builtin_adjust_trampoline (tree exp)
4864 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
4867 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
4868 tramp = round_trampoline_addr (tramp);
4869 if (targetm.calls.trampoline_adjust_address)
4870 tramp = targetm.calls.trampoline_adjust_address (tramp);
4875 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4876 function. The function first checks whether the back end provides
4877 an insn to implement signbit for the respective mode. If not, it
4878 checks whether the floating point format of the value is such that
4879 the sign bit can be extracted. If that is not the case, the
4880 function returns NULL_RTX to indicate that a normal call should be
4881 emitted rather than expanding the function in-line. EXP is the
4882 expression that is a call to the builtin function; if convenient,
4883 the result should be placed in TARGET. */
4885 expand_builtin_signbit (tree exp, rtx target)
4887 const struct real_format *fmt;
4888 enum machine_mode fmode, imode, rmode;
4891 enum insn_code icode;
4893 location_t loc = EXPR_LOCATION (exp);
4895 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
4898 arg = CALL_EXPR_ARG (exp, 0);
4899 fmode = TYPE_MODE (TREE_TYPE (arg));
4900 rmode = TYPE_MODE (TREE_TYPE (exp));
4901 fmt = REAL_MODE_FORMAT (fmode);
4903 arg = builtin_save_expr (arg);
4905 /* Expand the argument yielding a RTX expression. */
4906 temp = expand_normal (arg);
4908 /* Check if the back end provides an insn that handles signbit for the
4910 icode = optab_handler (signbit_optab, fmode);
4911 if (icode != CODE_FOR_nothing)
4913 rtx last = get_last_insn ();
4914 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
4915 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
4917 delete_insns_since (last);
4920 /* For floating point formats without a sign bit, implement signbit
4922 bitpos = fmt->signbit_ro;
4925 /* But we can't do this if the format supports signed zero. */
4926 if (fmt->has_signed_zero && HONOR_SIGNED_ZEROS (fmode))
4929 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
4930 build_real (TREE_TYPE (arg), dconst0));
4931 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
4934 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
4936 imode = int_mode_for_mode (fmode);
4937 if (imode == BLKmode)
4939 temp = gen_lowpart (imode, temp);
4944 /* Handle targets with different FP word orders. */
4945 if (FLOAT_WORDS_BIG_ENDIAN)
4946 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
4948 word = bitpos / BITS_PER_WORD;
4949 temp = operand_subword_force (temp, word, fmode);
4950 bitpos = bitpos % BITS_PER_WORD;
4953 /* Force the intermediate word_mode (or narrower) result into a
4954 register. This avoids attempting to create paradoxical SUBREGs
4955 of floating point modes below. */
4956 temp = force_reg (imode, temp);
4958 /* If the bitpos is within the "result mode" lowpart, the operation
4959 can be implement with a single bitwise AND. Otherwise, we need
4960 a right shift and an AND. */
4962 if (bitpos < GET_MODE_BITSIZE (rmode))
4964 double_int mask = double_int_setbit (double_int_zero, bitpos);
4966 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
4967 temp = gen_lowpart (rmode, temp);
4968 temp = expand_binop (rmode, and_optab, temp,
4969 immed_double_int_const (mask, rmode),
4970 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4974 /* Perform a logical right shift to place the signbit in the least
4975 significant bit, then truncate the result to the desired mode
4976 and mask just this bit. */
4977 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
4978 temp = gen_lowpart (rmode, temp);
4979 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
4980 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4986 /* Expand fork or exec calls. TARGET is the desired target of the
4987 call. EXP is the call. FN is the
4988 identificator of the actual function. IGNORE is nonzero if the
4989 value is to be ignored. */
4992 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
4997 /* If we are not profiling, just call the function. */
4998 if (!profile_arc_flag)
5001 /* Otherwise call the wrapper. This should be equivalent for the rest of
5002 compiler, so the code does not diverge, and the wrapper may run the
5003 code necessary for keeping the profiling sane. */
5005 switch (DECL_FUNCTION_CODE (fn))
5008 id = get_identifier ("__gcov_fork");
5011 case BUILT_IN_EXECL:
5012 id = get_identifier ("__gcov_execl");
5015 case BUILT_IN_EXECV:
5016 id = get_identifier ("__gcov_execv");
5019 case BUILT_IN_EXECLP:
5020 id = get_identifier ("__gcov_execlp");
5023 case BUILT_IN_EXECLE:
5024 id = get_identifier ("__gcov_execle");
5027 case BUILT_IN_EXECVP:
5028 id = get_identifier ("__gcov_execvp");
5031 case BUILT_IN_EXECVE:
5032 id = get_identifier ("__gcov_execve");
5039 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5040 FUNCTION_DECL, id, TREE_TYPE (fn));
5041 DECL_EXTERNAL (decl) = 1;
5042 TREE_PUBLIC (decl) = 1;
5043 DECL_ARTIFICIAL (decl) = 1;
5044 TREE_NOTHROW (decl) = 1;
5045 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5046 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5047 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5048 return expand_call (call, target, ignore);
5053 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5054 the pointer in these functions is void*, the tree optimizers may remove
5055 casts. The mode computed in expand_builtin isn't reliable either, due
5056 to __sync_bool_compare_and_swap.
5058 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5059 group of builtins. This gives us log2 of the mode size. */
5061 static inline enum machine_mode
5062 get_builtin_sync_mode (int fcode_diff)
5064 /* The size is not negotiable, so ask not to get BLKmode in return
5065 if the target indicates that a smaller size would be better. */
5066 return mode_for_size (BITS_PER_UNIT << fcode_diff, MODE_INT, 0);
5069 /* Expand the memory expression LOC and return the appropriate memory operand
5070 for the builtin_sync operations. */
5073 get_builtin_sync_mem (tree loc, enum machine_mode mode)
5077 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5078 addr = convert_memory_address (Pmode, addr);
5080 /* Note that we explicitly do not want any alias information for this
5081 memory, so that we kill all other live memories. Otherwise we don't
5082 satisfy the full barrier semantics of the intrinsic. */
5083 mem = validize_mem (gen_rtx_MEM (mode, addr));
5085 /* The alignment needs to be at least according to that of the mode. */
5086 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5087 get_pointer_alignment (loc)));
5088 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5089 MEM_VOLATILE_P (mem) = 1;
5094 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5095 EXP is the CALL_EXPR. CODE is the rtx code
5096 that corresponds to the arithmetic or logical operation from the name;
5097 an exception here is that NOT actually means NAND. TARGET is an optional
5098 place for us to store the results; AFTER is true if this is the
5099 fetch_and_xxx form. IGNORE is true if we don't actually care about
5100 the result of the operation at all. */
5103 expand_builtin_sync_operation (enum machine_mode mode, tree exp,
5104 enum rtx_code code, bool after,
5105 rtx target, bool ignore)
5108 enum machine_mode old_mode;
5109 location_t loc = EXPR_LOCATION (exp);
5111 if (code == NOT && warn_sync_nand)
5113 tree fndecl = get_callee_fndecl (exp);
5114 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5116 static bool warned_f_a_n, warned_n_a_f;
5120 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5121 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5122 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5123 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5124 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5129 fndecl = implicit_built_in_decls[BUILT_IN_SYNC_FETCH_AND_NAND_N];
5130 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5131 warned_f_a_n = true;
5134 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5135 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5136 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5137 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5138 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5143 fndecl = implicit_built_in_decls[BUILT_IN_SYNC_NAND_AND_FETCH_N];
5144 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5145 warned_n_a_f = true;
5153 /* Expand the operands. */
5154 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5156 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5157 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5158 of CONST_INTs, where we know the old_mode only from the call argument. */
5159 old_mode = GET_MODE (val);
5160 if (old_mode == VOIDmode)
5161 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5162 val = convert_modes (mode, old_mode, val, 1);
5165 return expand_sync_operation (mem, val, code);
5167 return expand_sync_fetch_operation (mem, val, code, after, target);
5170 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5171 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5172 true if this is the boolean form. TARGET is a place for us to store the
5173 results; this is NOT optional if IS_BOOL is true. */
5176 expand_builtin_compare_and_swap (enum machine_mode mode, tree exp,
5177 bool is_bool, rtx target)
5179 rtx old_val, new_val, mem;
5180 enum machine_mode old_mode;
5182 /* Expand the operands. */
5183 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5186 old_val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
5187 mode, EXPAND_NORMAL);
5188 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5189 of CONST_INTs, where we know the old_mode only from the call argument. */
5190 old_mode = GET_MODE (old_val);
5191 if (old_mode == VOIDmode)
5192 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5193 old_val = convert_modes (mode, old_mode, old_val, 1);
5195 new_val = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX,
5196 mode, EXPAND_NORMAL);
5197 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5198 of CONST_INTs, where we know the old_mode only from the call argument. */
5199 old_mode = GET_MODE (new_val);
5200 if (old_mode == VOIDmode)
5201 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 2)));
5202 new_val = convert_modes (mode, old_mode, new_val, 1);
5205 return expand_bool_compare_and_swap (mem, old_val, new_val, target);
5207 return expand_val_compare_and_swap (mem, old_val, new_val, target);
5210 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5211 general form is actually an atomic exchange, and some targets only
5212 support a reduced form with the second argument being a constant 1.
5213 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5217 expand_builtin_sync_lock_test_and_set (enum machine_mode mode, tree exp,
5221 enum machine_mode old_mode;
5223 /* Expand the operands. */
5224 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5225 val = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, mode, EXPAND_NORMAL);
5226 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5227 of CONST_INTs, where we know the old_mode only from the call argument. */
5228 old_mode = GET_MODE (val);
5229 if (old_mode == VOIDmode)
5230 old_mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 1)));
5231 val = convert_modes (mode, old_mode, val, 1);
5233 return expand_sync_lock_test_and_set (mem, val, target);
5236 /* Expand the __sync_synchronize intrinsic. */
5239 expand_builtin_sync_synchronize (void)
5242 VEC (tree, gc) *v_clobbers;
5244 #ifdef HAVE_memory_barrier
5245 if (HAVE_memory_barrier)
5247 emit_insn (gen_memory_barrier ());
5252 if (synchronize_libfunc != NULL_RTX)
5254 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
5258 /* If no explicit memory barrier instruction is available, create an
5259 empty asm stmt with a memory clobber. */
5260 v_clobbers = VEC_alloc (tree, gc, 1);
5261 VEC_quick_push (tree, v_clobbers,
5262 tree_cons (NULL, build_string (6, "memory"), NULL));
5263 x = gimple_build_asm_vec ("", NULL, NULL, v_clobbers, NULL);
5264 gimple_asm_set_volatile (x, true);
5265 expand_asm_stmt (x);
5268 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5271 expand_builtin_sync_lock_release (enum machine_mode mode, tree exp)
5273 struct expand_operand ops[2];
5274 enum insn_code icode;
5277 /* Expand the operands. */
5278 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5280 /* If there is an explicit operation in the md file, use it. */
5281 icode = direct_optab_handler (sync_lock_release_optab, mode);
5282 if (icode != CODE_FOR_nothing)
5284 create_fixed_operand (&ops[0], mem);
5285 create_input_operand (&ops[1], const0_rtx, mode);
5286 if (maybe_expand_insn (icode, 2, ops))
5290 /* Otherwise we can implement this operation by emitting a barrier
5291 followed by a store of zero. */
5292 expand_builtin_sync_synchronize ();
5293 emit_move_insn (mem, const0_rtx);
5296 /* Expand an expression EXP that calls a built-in function,
5297 with result going to TARGET if that's convenient
5298 (and in mode MODE if that's convenient).
5299 SUBTARGET may be used as the target for computing one of EXP's operands.
5300 IGNORE is nonzero if the value is to be ignored. */
5303 expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
5306 tree fndecl = get_callee_fndecl (exp);
5307 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5308 enum machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
5311 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
5312 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
5314 /* When not optimizing, generate calls to library functions for a certain
5317 && !called_as_built_in (fndecl)
5318 && DECL_ASSEMBLER_NAME_SET_P (fndecl)
5319 && fcode != BUILT_IN_ALLOCA
5320 && fcode != BUILT_IN_ALLOCA_WITH_ALIGN
5321 && fcode != BUILT_IN_FREE)
5322 return expand_call (exp, target, ignore);
5324 /* The built-in function expanders test for target == const0_rtx
5325 to determine whether the function's result will be ignored. */
5327 target = const0_rtx;
5329 /* If the result of a pure or const built-in function is ignored, and
5330 none of its arguments are volatile, we can avoid expanding the
5331 built-in call and just evaluate the arguments for side-effects. */
5332 if (target == const0_rtx
5333 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
5334 && !(flags & ECF_LOOPING_CONST_OR_PURE))
5336 bool volatilep = false;
5338 call_expr_arg_iterator iter;
5340 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5341 if (TREE_THIS_VOLATILE (arg))
5349 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
5350 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
5357 CASE_FLT_FN (BUILT_IN_FABS):
5358 target = expand_builtin_fabs (exp, target, subtarget);
5363 CASE_FLT_FN (BUILT_IN_COPYSIGN):
5364 target = expand_builtin_copysign (exp, target, subtarget);
5369 /* Just do a normal library call if we were unable to fold
5371 CASE_FLT_FN (BUILT_IN_CABS):
5374 CASE_FLT_FN (BUILT_IN_EXP):
5375 CASE_FLT_FN (BUILT_IN_EXP10):
5376 CASE_FLT_FN (BUILT_IN_POW10):
5377 CASE_FLT_FN (BUILT_IN_EXP2):
5378 CASE_FLT_FN (BUILT_IN_EXPM1):
5379 CASE_FLT_FN (BUILT_IN_LOGB):
5380 CASE_FLT_FN (BUILT_IN_LOG):
5381 CASE_FLT_FN (BUILT_IN_LOG10):
5382 CASE_FLT_FN (BUILT_IN_LOG2):
5383 CASE_FLT_FN (BUILT_IN_LOG1P):
5384 CASE_FLT_FN (BUILT_IN_TAN):
5385 CASE_FLT_FN (BUILT_IN_ASIN):
5386 CASE_FLT_FN (BUILT_IN_ACOS):
5387 CASE_FLT_FN (BUILT_IN_ATAN):
5388 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
5389 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5390 because of possible accuracy problems. */
5391 if (! flag_unsafe_math_optimizations)
5393 CASE_FLT_FN (BUILT_IN_SQRT):
5394 CASE_FLT_FN (BUILT_IN_FLOOR):
5395 CASE_FLT_FN (BUILT_IN_CEIL):
5396 CASE_FLT_FN (BUILT_IN_TRUNC):
5397 CASE_FLT_FN (BUILT_IN_ROUND):
5398 CASE_FLT_FN (BUILT_IN_NEARBYINT):
5399 CASE_FLT_FN (BUILT_IN_RINT):
5400 target = expand_builtin_mathfn (exp, target, subtarget);
5405 CASE_FLT_FN (BUILT_IN_FMA):
5406 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
5411 CASE_FLT_FN (BUILT_IN_ILOGB):
5412 if (! flag_unsafe_math_optimizations)
5414 CASE_FLT_FN (BUILT_IN_ISINF):
5415 CASE_FLT_FN (BUILT_IN_FINITE):
5416 case BUILT_IN_ISFINITE:
5417 case BUILT_IN_ISNORMAL:
5418 target = expand_builtin_interclass_mathfn (exp, target);
5423 CASE_FLT_FN (BUILT_IN_ICEIL):
5424 CASE_FLT_FN (BUILT_IN_LCEIL):
5425 CASE_FLT_FN (BUILT_IN_LLCEIL):
5426 CASE_FLT_FN (BUILT_IN_LFLOOR):
5427 CASE_FLT_FN (BUILT_IN_IFLOOR):
5428 CASE_FLT_FN (BUILT_IN_LLFLOOR):
5429 target = expand_builtin_int_roundingfn (exp, target);
5434 CASE_FLT_FN (BUILT_IN_IRINT):
5435 CASE_FLT_FN (BUILT_IN_LRINT):
5436 CASE_FLT_FN (BUILT_IN_LLRINT):
5437 CASE_FLT_FN (BUILT_IN_IROUND):
5438 CASE_FLT_FN (BUILT_IN_LROUND):
5439 CASE_FLT_FN (BUILT_IN_LLROUND):
5440 target = expand_builtin_int_roundingfn_2 (exp, target);
5445 CASE_FLT_FN (BUILT_IN_POWI):
5446 target = expand_builtin_powi (exp, target);
5451 CASE_FLT_FN (BUILT_IN_ATAN2):
5452 CASE_FLT_FN (BUILT_IN_LDEXP):
5453 CASE_FLT_FN (BUILT_IN_SCALB):
5454 CASE_FLT_FN (BUILT_IN_SCALBN):
5455 CASE_FLT_FN (BUILT_IN_SCALBLN):
5456 if (! flag_unsafe_math_optimizations)
5459 CASE_FLT_FN (BUILT_IN_FMOD):
5460 CASE_FLT_FN (BUILT_IN_REMAINDER):
5461 CASE_FLT_FN (BUILT_IN_DREM):
5462 CASE_FLT_FN (BUILT_IN_POW):
5463 target = expand_builtin_mathfn_2 (exp, target, subtarget);
5468 CASE_FLT_FN (BUILT_IN_CEXPI):
5469 target = expand_builtin_cexpi (exp, target);
5470 gcc_assert (target);
5473 CASE_FLT_FN (BUILT_IN_SIN):
5474 CASE_FLT_FN (BUILT_IN_COS):
5475 if (! flag_unsafe_math_optimizations)
5477 target = expand_builtin_mathfn_3 (exp, target, subtarget);
5482 CASE_FLT_FN (BUILT_IN_SINCOS):
5483 if (! flag_unsafe_math_optimizations)
5485 target = expand_builtin_sincos (exp);
5490 case BUILT_IN_APPLY_ARGS:
5491 return expand_builtin_apply_args ();
5493 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5494 FUNCTION with a copy of the parameters described by
5495 ARGUMENTS, and ARGSIZE. It returns a block of memory
5496 allocated on the stack into which is stored all the registers
5497 that might possibly be used for returning the result of a
5498 function. ARGUMENTS is the value returned by
5499 __builtin_apply_args. ARGSIZE is the number of bytes of
5500 arguments that must be copied. ??? How should this value be
5501 computed? We'll also need a safe worst case value for varargs
5503 case BUILT_IN_APPLY:
5504 if (!validate_arglist (exp, POINTER_TYPE,
5505 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
5506 && !validate_arglist (exp, REFERENCE_TYPE,
5507 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5513 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
5514 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
5515 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
5517 return expand_builtin_apply (ops[0], ops[1], ops[2]);
5520 /* __builtin_return (RESULT) causes the function to return the
5521 value described by RESULT. RESULT is address of the block of
5522 memory returned by __builtin_apply. */
5523 case BUILT_IN_RETURN:
5524 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5525 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
5528 case BUILT_IN_SAVEREGS:
5529 return expand_builtin_saveregs ();
5531 case BUILT_IN_VA_ARG_PACK:
5532 /* All valid uses of __builtin_va_arg_pack () are removed during
5534 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
5537 case BUILT_IN_VA_ARG_PACK_LEN:
5538 /* All valid uses of __builtin_va_arg_pack_len () are removed during
5540 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
5543 /* Return the address of the first anonymous stack arg. */
5544 case BUILT_IN_NEXT_ARG:
5545 if (fold_builtin_next_arg (exp, false))
5547 return expand_builtin_next_arg ();
5549 case BUILT_IN_CLEAR_CACHE:
5550 target = expand_builtin___clear_cache (exp);
5555 case BUILT_IN_CLASSIFY_TYPE:
5556 return expand_builtin_classify_type (exp);
5558 case BUILT_IN_CONSTANT_P:
5561 case BUILT_IN_FRAME_ADDRESS:
5562 case BUILT_IN_RETURN_ADDRESS:
5563 return expand_builtin_frame_address (fndecl, exp);
5565 /* Returns the address of the area where the structure is returned.
5567 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5568 if (call_expr_nargs (exp) != 0
5569 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
5570 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
5573 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5575 case BUILT_IN_ALLOCA:
5576 case BUILT_IN_ALLOCA_WITH_ALIGN:
5577 /* If the allocation stems from the declaration of a variable-sized
5578 object, it cannot accumulate. */
5579 target = expand_builtin_alloca (exp, CALL_ALLOCA_FOR_VAR_P (exp));
5584 case BUILT_IN_STACK_SAVE:
5585 return expand_stack_save ();
5587 case BUILT_IN_STACK_RESTORE:
5588 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
5591 case BUILT_IN_BSWAP32:
5592 case BUILT_IN_BSWAP64:
5593 target = expand_builtin_bswap (exp, target, subtarget);
5599 CASE_INT_FN (BUILT_IN_FFS):
5600 case BUILT_IN_FFSIMAX:
5601 target = expand_builtin_unop (target_mode, exp, target,
5602 subtarget, ffs_optab);
5607 CASE_INT_FN (BUILT_IN_CLZ):
5608 case BUILT_IN_CLZIMAX:
5609 target = expand_builtin_unop (target_mode, exp, target,
5610 subtarget, clz_optab);
5615 CASE_INT_FN (BUILT_IN_CTZ):
5616 case BUILT_IN_CTZIMAX:
5617 target = expand_builtin_unop (target_mode, exp, target,
5618 subtarget, ctz_optab);
5623 CASE_INT_FN (BUILT_IN_CLRSB):
5624 case BUILT_IN_CLRSBIMAX:
5625 target = expand_builtin_unop (target_mode, exp, target,
5626 subtarget, clrsb_optab);
5631 CASE_INT_FN (BUILT_IN_POPCOUNT):
5632 case BUILT_IN_POPCOUNTIMAX:
5633 target = expand_builtin_unop (target_mode, exp, target,
5634 subtarget, popcount_optab);
5639 CASE_INT_FN (BUILT_IN_PARITY):
5640 case BUILT_IN_PARITYIMAX:
5641 target = expand_builtin_unop (target_mode, exp, target,
5642 subtarget, parity_optab);
5647 case BUILT_IN_STRLEN:
5648 target = expand_builtin_strlen (exp, target, target_mode);
5653 case BUILT_IN_STRCPY:
5654 target = expand_builtin_strcpy (exp, target);
5659 case BUILT_IN_STRNCPY:
5660 target = expand_builtin_strncpy (exp, target);
5665 case BUILT_IN_STPCPY:
5666 target = expand_builtin_stpcpy (exp, target, mode);
5671 case BUILT_IN_MEMCPY:
5672 target = expand_builtin_memcpy (exp, target);
5677 case BUILT_IN_MEMPCPY:
5678 target = expand_builtin_mempcpy (exp, target, mode);
5683 case BUILT_IN_MEMSET:
5684 target = expand_builtin_memset (exp, target, mode);
5689 case BUILT_IN_BZERO:
5690 target = expand_builtin_bzero (exp);
5695 case BUILT_IN_STRCMP:
5696 target = expand_builtin_strcmp (exp, target);
5701 case BUILT_IN_STRNCMP:
5702 target = expand_builtin_strncmp (exp, target, mode);
5708 case BUILT_IN_MEMCMP:
5709 target = expand_builtin_memcmp (exp, target, mode);
5714 case BUILT_IN_SETJMP:
5715 /* This should have been lowered to the builtins below. */
5718 case BUILT_IN_SETJMP_SETUP:
5719 /* __builtin_setjmp_setup is passed a pointer to an array of five words
5720 and the receiver label. */
5721 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5723 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
5724 VOIDmode, EXPAND_NORMAL);
5725 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
5726 rtx label_r = label_rtx (label);
5728 /* This is copied from the handling of non-local gotos. */
5729 expand_builtin_setjmp_setup (buf_addr, label_r);
5730 nonlocal_goto_handler_labels
5731 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
5732 nonlocal_goto_handler_labels);
5733 /* ??? Do not let expand_label treat us as such since we would
5734 not want to be both on the list of non-local labels and on
5735 the list of forced labels. */
5736 FORCED_LABEL (label) = 0;
5741 case BUILT_IN_SETJMP_DISPATCHER:
5742 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
5743 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5745 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
5746 rtx label_r = label_rtx (label);
5748 /* Remove the dispatcher label from the list of non-local labels
5749 since the receiver labels have been added to it above. */
5750 remove_node_from_expr_list (label_r, &nonlocal_goto_handler_labels);
5755 case BUILT_IN_SETJMP_RECEIVER:
5756 /* __builtin_setjmp_receiver is passed the receiver label. */
5757 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5759 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
5760 rtx label_r = label_rtx (label);
5762 expand_builtin_setjmp_receiver (label_r);
5767 /* __builtin_longjmp is passed a pointer to an array of five words.
5768 It's similar to the C library longjmp function but works with
5769 __builtin_setjmp above. */
5770 case BUILT_IN_LONGJMP:
5771 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
5773 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
5774 VOIDmode, EXPAND_NORMAL);
5775 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
5777 if (value != const1_rtx)
5779 error ("%<__builtin_longjmp%> second argument must be 1");
5783 expand_builtin_longjmp (buf_addr, value);
5788 case BUILT_IN_NONLOCAL_GOTO:
5789 target = expand_builtin_nonlocal_goto (exp);
5794 /* This updates the setjmp buffer that is its argument with the value
5795 of the current stack pointer. */
5796 case BUILT_IN_UPDATE_SETJMP_BUF:
5797 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5800 = expand_normal (CALL_EXPR_ARG (exp, 0));
5802 expand_builtin_update_setjmp_buf (buf_addr);
5808 expand_builtin_trap ();
5811 case BUILT_IN_UNREACHABLE:
5812 expand_builtin_unreachable ();
5815 CASE_FLT_FN (BUILT_IN_SIGNBIT):
5816 case BUILT_IN_SIGNBITD32:
5817 case BUILT_IN_SIGNBITD64:
5818 case BUILT_IN_SIGNBITD128:
5819 target = expand_builtin_signbit (exp, target);
5824 /* Various hooks for the DWARF 2 __throw routine. */
5825 case BUILT_IN_UNWIND_INIT:
5826 expand_builtin_unwind_init ();
5828 case BUILT_IN_DWARF_CFA:
5829 return virtual_cfa_rtx;
5830 #ifdef DWARF2_UNWIND_INFO
5831 case BUILT_IN_DWARF_SP_COLUMN:
5832 return expand_builtin_dwarf_sp_column ();
5833 case BUILT_IN_INIT_DWARF_REG_SIZES:
5834 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
5837 case BUILT_IN_FROB_RETURN_ADDR:
5838 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
5839 case BUILT_IN_EXTRACT_RETURN_ADDR:
5840 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
5841 case BUILT_IN_EH_RETURN:
5842 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
5843 CALL_EXPR_ARG (exp, 1));
5845 #ifdef EH_RETURN_DATA_REGNO
5846 case BUILT_IN_EH_RETURN_DATA_REGNO:
5847 return expand_builtin_eh_return_data_regno (exp);
5849 case BUILT_IN_EXTEND_POINTER:
5850 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
5851 case BUILT_IN_EH_POINTER:
5852 return expand_builtin_eh_pointer (exp);
5853 case BUILT_IN_EH_FILTER:
5854 return expand_builtin_eh_filter (exp);
5855 case BUILT_IN_EH_COPY_VALUES:
5856 return expand_builtin_eh_copy_values (exp);
5858 case BUILT_IN_VA_START:
5859 return expand_builtin_va_start (exp);
5860 case BUILT_IN_VA_END:
5861 return expand_builtin_va_end (exp);
5862 case BUILT_IN_VA_COPY:
5863 return expand_builtin_va_copy (exp);
5864 case BUILT_IN_EXPECT:
5865 return expand_builtin_expect (exp, target);
5866 case BUILT_IN_ASSUME_ALIGNED:
5867 return expand_builtin_assume_aligned (exp, target);
5868 case BUILT_IN_PREFETCH:
5869 expand_builtin_prefetch (exp);
5872 case BUILT_IN_INIT_TRAMPOLINE:
5873 return expand_builtin_init_trampoline (exp);
5874 case BUILT_IN_ADJUST_TRAMPOLINE:
5875 return expand_builtin_adjust_trampoline (exp);
5878 case BUILT_IN_EXECL:
5879 case BUILT_IN_EXECV:
5880 case BUILT_IN_EXECLP:
5881 case BUILT_IN_EXECLE:
5882 case BUILT_IN_EXECVP:
5883 case BUILT_IN_EXECVE:
5884 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
5889 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
5890 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
5891 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
5892 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
5893 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
5894 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
5895 target = expand_builtin_sync_operation (mode, exp, PLUS,
5896 false, target, ignore);
5901 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
5902 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
5903 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
5904 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
5905 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
5906 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
5907 target = expand_builtin_sync_operation (mode, exp, MINUS,
5908 false, target, ignore);
5913 case BUILT_IN_SYNC_FETCH_AND_OR_1:
5914 case BUILT_IN_SYNC_FETCH_AND_OR_2:
5915 case BUILT_IN_SYNC_FETCH_AND_OR_4:
5916 case BUILT_IN_SYNC_FETCH_AND_OR_8:
5917 case BUILT_IN_SYNC_FETCH_AND_OR_16:
5918 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
5919 target = expand_builtin_sync_operation (mode, exp, IOR,
5920 false, target, ignore);
5925 case BUILT_IN_SYNC_FETCH_AND_AND_1:
5926 case BUILT_IN_SYNC_FETCH_AND_AND_2:
5927 case BUILT_IN_SYNC_FETCH_AND_AND_4:
5928 case BUILT_IN_SYNC_FETCH_AND_AND_8:
5929 case BUILT_IN_SYNC_FETCH_AND_AND_16:
5930 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
5931 target = expand_builtin_sync_operation (mode, exp, AND,
5932 false, target, ignore);
5937 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
5938 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
5939 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
5940 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
5941 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
5942 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
5943 target = expand_builtin_sync_operation (mode, exp, XOR,
5944 false, target, ignore);
5949 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5950 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5951 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5952 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5953 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5954 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
5955 target = expand_builtin_sync_operation (mode, exp, NOT,
5956 false, target, ignore);
5961 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
5962 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
5963 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
5964 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
5965 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
5966 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
5967 target = expand_builtin_sync_operation (mode, exp, PLUS,
5968 true, target, ignore);
5973 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
5974 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
5975 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
5976 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
5977 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
5978 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
5979 target = expand_builtin_sync_operation (mode, exp, MINUS,
5980 true, target, ignore);
5985 case BUILT_IN_SYNC_OR_AND_FETCH_1:
5986 case BUILT_IN_SYNC_OR_AND_FETCH_2:
5987 case BUILT_IN_SYNC_OR_AND_FETCH_4:
5988 case BUILT_IN_SYNC_OR_AND_FETCH_8:
5989 case BUILT_IN_SYNC_OR_AND_FETCH_16:
5990 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
5991 target = expand_builtin_sync_operation (mode, exp, IOR,
5992 true, target, ignore);
5997 case BUILT_IN_SYNC_AND_AND_FETCH_1:
5998 case BUILT_IN_SYNC_AND_AND_FETCH_2:
5999 case BUILT_IN_SYNC_AND_AND_FETCH_4:
6000 case BUILT_IN_SYNC_AND_AND_FETCH_8:
6001 case BUILT_IN_SYNC_AND_AND_FETCH_16:
6002 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
6003 target = expand_builtin_sync_operation (mode, exp, AND,
6004 true, target, ignore);
6009 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
6010 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
6011 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
6012 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
6013 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
6014 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
6015 target = expand_builtin_sync_operation (mode, exp, XOR,
6016 true, target, ignore);
6021 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6022 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6023 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6024 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6025 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6026 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
6027 target = expand_builtin_sync_operation (mode, exp, NOT,
6028 true, target, ignore);
6033 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
6034 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
6035 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
6036 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
6037 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
6038 if (mode == VOIDmode)
6039 mode = TYPE_MODE (boolean_type_node);
6040 if (!target || !register_operand (target, mode))
6041 target = gen_reg_rtx (mode);
6043 mode = get_builtin_sync_mode
6044 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
6045 target = expand_builtin_compare_and_swap (mode, exp, true, target);
6050 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
6051 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
6052 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
6053 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
6054 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
6055 mode = get_builtin_sync_mode
6056 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
6057 target = expand_builtin_compare_and_swap (mode, exp, false, target);
6062 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
6063 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
6064 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
6065 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
6066 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
6067 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
6068 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
6073 case BUILT_IN_SYNC_LOCK_RELEASE_1:
6074 case BUILT_IN_SYNC_LOCK_RELEASE_2:
6075 case BUILT_IN_SYNC_LOCK_RELEASE_4:
6076 case BUILT_IN_SYNC_LOCK_RELEASE_8:
6077 case BUILT_IN_SYNC_LOCK_RELEASE_16:
6078 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
6079 expand_builtin_sync_lock_release (mode, exp);
6082 case BUILT_IN_SYNC_SYNCHRONIZE:
6083 expand_builtin_sync_synchronize ();
6086 case BUILT_IN_OBJECT_SIZE:
6087 return expand_builtin_object_size (exp);
6089 case BUILT_IN_MEMCPY_CHK:
6090 case BUILT_IN_MEMPCPY_CHK:
6091 case BUILT_IN_MEMMOVE_CHK:
6092 case BUILT_IN_MEMSET_CHK:
6093 target = expand_builtin_memory_chk (exp, target, mode, fcode);
6098 case BUILT_IN_STRCPY_CHK:
6099 case BUILT_IN_STPCPY_CHK:
6100 case BUILT_IN_STRNCPY_CHK:
6101 case BUILT_IN_STRCAT_CHK:
6102 case BUILT_IN_STRNCAT_CHK:
6103 case BUILT_IN_SNPRINTF_CHK:
6104 case BUILT_IN_VSNPRINTF_CHK:
6105 maybe_emit_chk_warning (exp, fcode);
6108 case BUILT_IN_SPRINTF_CHK:
6109 case BUILT_IN_VSPRINTF_CHK:
6110 maybe_emit_sprintf_chk_warning (exp, fcode);
6114 if (warn_free_nonheap_object)
6115 maybe_emit_free_warning (exp);
6118 default: /* just do library call, if unknown builtin */
6122 /* The switch statement above can drop through to cause the function
6123 to be called normally. */
6124 return expand_call (exp, target, ignore);
6127 /* Determine whether a tree node represents a call to a built-in
6128 function. If the tree T is a call to a built-in function with
6129 the right number of arguments of the appropriate types, return
6130 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6131 Otherwise the return value is END_BUILTINS. */
6133 enum built_in_function
6134 builtin_mathfn_code (const_tree t)
6136 const_tree fndecl, arg, parmlist;
6137 const_tree argtype, parmtype;
6138 const_call_expr_arg_iterator iter;
6140 if (TREE_CODE (t) != CALL_EXPR
6141 || TREE_CODE (CALL_EXPR_FN (t)) != ADDR_EXPR)
6142 return END_BUILTINS;
6144 fndecl = get_callee_fndecl (t);
6145 if (fndecl == NULL_TREE
6146 || TREE_CODE (fndecl) != FUNCTION_DECL
6147 || ! DECL_BUILT_IN (fndecl)
6148 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6149 return END_BUILTINS;
6151 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6152 init_const_call_expr_arg_iterator (t, &iter);
6153 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
6155 /* If a function doesn't take a variable number of arguments,
6156 the last element in the list will have type `void'. */
6157 parmtype = TREE_VALUE (parmlist);
6158 if (VOID_TYPE_P (parmtype))
6160 if (more_const_call_expr_args_p (&iter))
6161 return END_BUILTINS;
6162 return DECL_FUNCTION_CODE (fndecl);
6165 if (! more_const_call_expr_args_p (&iter))
6166 return END_BUILTINS;
6168 arg = next_const_call_expr_arg (&iter);
6169 argtype = TREE_TYPE (arg);
6171 if (SCALAR_FLOAT_TYPE_P (parmtype))
6173 if (! SCALAR_FLOAT_TYPE_P (argtype))
6174 return END_BUILTINS;
6176 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
6178 if (! COMPLEX_FLOAT_TYPE_P (argtype))
6179 return END_BUILTINS;
6181 else if (POINTER_TYPE_P (parmtype))
6183 if (! POINTER_TYPE_P (argtype))
6184 return END_BUILTINS;
6186 else if (INTEGRAL_TYPE_P (parmtype))
6188 if (! INTEGRAL_TYPE_P (argtype))
6189 return END_BUILTINS;
6192 return END_BUILTINS;
6195 /* Variable-length argument list. */
6196 return DECL_FUNCTION_CODE (fndecl);
6199 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6200 evaluate to a constant. */
6203 fold_builtin_constant_p (tree arg)
6205 /* We return 1 for a numeric type that's known to be a constant
6206 value at compile-time or for an aggregate type that's a
6207 literal constant. */
6210 /* If we know this is a constant, emit the constant of one. */
6211 if (CONSTANT_CLASS_P (arg)
6212 || (TREE_CODE (arg) == CONSTRUCTOR
6213 && TREE_CONSTANT (arg)))
6214 return integer_one_node;
6215 if (TREE_CODE (arg) == ADDR_EXPR)
6217 tree op = TREE_OPERAND (arg, 0);
6218 if (TREE_CODE (op) == STRING_CST
6219 || (TREE_CODE (op) == ARRAY_REF
6220 && integer_zerop (TREE_OPERAND (op, 1))
6221 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
6222 return integer_one_node;
6225 /* If this expression has side effects, show we don't know it to be a
6226 constant. Likewise if it's a pointer or aggregate type since in
6227 those case we only want literals, since those are only optimized
6228 when generating RTL, not later.
6229 And finally, if we are compiling an initializer, not code, we
6230 need to return a definite result now; there's not going to be any
6231 more optimization done. */
6232 if (TREE_SIDE_EFFECTS (arg)
6233 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
6234 || POINTER_TYPE_P (TREE_TYPE (arg))
6236 || folding_initializer)
6237 return integer_zero_node;
6242 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6243 return it as a truthvalue. */
6246 build_builtin_expect_predicate (location_t loc, tree pred, tree expected)
6248 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
6250 fn = built_in_decls[BUILT_IN_EXPECT];
6251 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
6252 ret_type = TREE_TYPE (TREE_TYPE (fn));
6253 pred_type = TREE_VALUE (arg_types);
6254 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
6256 pred = fold_convert_loc (loc, pred_type, pred);
6257 expected = fold_convert_loc (loc, expected_type, expected);
6258 call_expr = build_call_expr_loc (loc, fn, 2, pred, expected);
6260 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
6261 build_int_cst (ret_type, 0));
6264 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6265 NULL_TREE if no simplification is possible. */
6268 fold_builtin_expect (location_t loc, tree arg0, tree arg1)
6270 tree inner, fndecl, inner_arg0;
6271 enum tree_code code;
6273 /* Distribute the expected value over short-circuiting operators.
6274 See through the cast from truthvalue_type_node to long. */
6276 while (TREE_CODE (inner_arg0) == NOP_EXPR
6277 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
6278 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
6279 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
6281 /* If this is a builtin_expect within a builtin_expect keep the
6282 inner one. See through a comparison against a constant. It
6283 might have been added to create a thruthvalue. */
6286 if (COMPARISON_CLASS_P (inner)
6287 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
6288 inner = TREE_OPERAND (inner, 0);
6290 if (TREE_CODE (inner) == CALL_EXPR
6291 && (fndecl = get_callee_fndecl (inner))
6292 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
6293 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
6297 code = TREE_CODE (inner);
6298 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6300 tree op0 = TREE_OPERAND (inner, 0);
6301 tree op1 = TREE_OPERAND (inner, 1);
6303 op0 = build_builtin_expect_predicate (loc, op0, arg1);
6304 op1 = build_builtin_expect_predicate (loc, op1, arg1);
6305 inner = build2 (code, TREE_TYPE (inner), op0, op1);
6307 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
6310 /* If the argument isn't invariant then there's nothing else we can do. */
6311 if (!TREE_CONSTANT (inner_arg0))
6314 /* If we expect that a comparison against the argument will fold to
6315 a constant return the constant. In practice, this means a true
6316 constant or the address of a non-weak symbol. */
6319 if (TREE_CODE (inner) == ADDR_EXPR)
6323 inner = TREE_OPERAND (inner, 0);
6325 while (TREE_CODE (inner) == COMPONENT_REF
6326 || TREE_CODE (inner) == ARRAY_REF);
6327 if ((TREE_CODE (inner) == VAR_DECL
6328 || TREE_CODE (inner) == FUNCTION_DECL)
6329 && DECL_WEAK (inner))
6333 /* Otherwise, ARG0 already has the proper type for the return value. */
6337 /* Fold a call to __builtin_classify_type with argument ARG. */
6340 fold_builtin_classify_type (tree arg)
6343 return build_int_cst (integer_type_node, no_type_class);
6345 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
6348 /* Fold a call to __builtin_strlen with argument ARG. */
6351 fold_builtin_strlen (location_t loc, tree type, tree arg)
6353 if (!validate_arg (arg, POINTER_TYPE))
6357 tree len = c_strlen (arg, 0);
6360 return fold_convert_loc (loc, type, len);
6366 /* Fold a call to __builtin_inf or __builtin_huge_val. */
6369 fold_builtin_inf (location_t loc, tree type, int warn)
6371 REAL_VALUE_TYPE real;
6373 /* __builtin_inff is intended to be usable to define INFINITY on all
6374 targets. If an infinity is not available, INFINITY expands "to a
6375 positive constant of type float that overflows at translation
6376 time", footnote "In this case, using INFINITY will violate the
6377 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
6378 Thus we pedwarn to ensure this constraint violation is
6380 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
6381 pedwarn (loc, 0, "target format does not support infinity");
6384 return build_real (type, real);
6387 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
6390 fold_builtin_nan (tree arg, tree type, int quiet)
6392 REAL_VALUE_TYPE real;
6395 if (!validate_arg (arg, POINTER_TYPE))
6397 str = c_getstr (arg);
6401 if (!real_nan (&real, str, quiet, TYPE_MODE (type)))
6404 return build_real (type, real);
6407 /* Return true if the floating point expression T has an integer value.
6408 We also allow +Inf, -Inf and NaN to be considered integer values. */
6411 integer_valued_real_p (tree t)
6413 switch (TREE_CODE (t))
6420 return integer_valued_real_p (TREE_OPERAND (t, 0));
6425 return integer_valued_real_p (TREE_OPERAND (t, 1));
6432 return integer_valued_real_p (TREE_OPERAND (t, 0))
6433 && integer_valued_real_p (TREE_OPERAND (t, 1));
6436 return integer_valued_real_p (TREE_OPERAND (t, 1))
6437 && integer_valued_real_p (TREE_OPERAND (t, 2));
6440 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
6444 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6445 if (TREE_CODE (type) == INTEGER_TYPE)
6447 if (TREE_CODE (type) == REAL_TYPE)
6448 return integer_valued_real_p (TREE_OPERAND (t, 0));
6453 switch (builtin_mathfn_code (t))
6455 CASE_FLT_FN (BUILT_IN_CEIL):
6456 CASE_FLT_FN (BUILT_IN_FLOOR):
6457 CASE_FLT_FN (BUILT_IN_NEARBYINT):
6458 CASE_FLT_FN (BUILT_IN_RINT):
6459 CASE_FLT_FN (BUILT_IN_ROUND):
6460 CASE_FLT_FN (BUILT_IN_TRUNC):
6463 CASE_FLT_FN (BUILT_IN_FMIN):
6464 CASE_FLT_FN (BUILT_IN_FMAX):
6465 return integer_valued_real_p (CALL_EXPR_ARG (t, 0))
6466 && integer_valued_real_p (CALL_EXPR_ARG (t, 1));
6479 /* FNDECL is assumed to be a builtin where truncation can be propagated
6480 across (for instance floor((double)f) == (double)floorf (f).
6481 Do the transformation for a call with argument ARG. */
6484 fold_trunc_transparent_mathfn (location_t loc, tree fndecl, tree arg)
6486 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6488 if (!validate_arg (arg, REAL_TYPE))
6491 /* Integer rounding functions are idempotent. */
6492 if (fcode == builtin_mathfn_code (arg))
6495 /* If argument is already integer valued, and we don't need to worry
6496 about setting errno, there's no need to perform rounding. */
6497 if (! flag_errno_math && integer_valued_real_p (arg))
6502 tree arg0 = strip_float_extensions (arg);
6503 tree ftype = TREE_TYPE (TREE_TYPE (fndecl));
6504 tree newtype = TREE_TYPE (arg0);
6507 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6508 && (decl = mathfn_built_in (newtype, fcode)))
6509 return fold_convert_loc (loc, ftype,
6510 build_call_expr_loc (loc, decl, 1,
6511 fold_convert_loc (loc,
6518 /* FNDECL is assumed to be builtin which can narrow the FP type of
6519 the argument, for instance lround((double)f) -> lroundf (f).
6520 Do the transformation for a call with argument ARG. */
6523 fold_fixed_mathfn (location_t loc, tree fndecl, tree arg)
6525 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6527 if (!validate_arg (arg, REAL_TYPE))
6530 /* If argument is already integer valued, and we don't need to worry
6531 about setting errno, there's no need to perform rounding. */
6532 if (! flag_errno_math && integer_valued_real_p (arg))
6533 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
6534 TREE_TYPE (TREE_TYPE (fndecl)), arg);
6538 tree ftype = TREE_TYPE (arg);
6539 tree arg0 = strip_float_extensions (arg);
6540 tree newtype = TREE_TYPE (arg0);
6543 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (ftype)
6544 && (decl = mathfn_built_in (newtype, fcode)))
6545 return build_call_expr_loc (loc, decl, 1,
6546 fold_convert_loc (loc, newtype, arg0));
6549 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6550 sizeof (int) == sizeof (long). */
6551 if (TYPE_PRECISION (integer_type_node)
6552 == TYPE_PRECISION (long_integer_type_node))
6554 tree newfn = NULL_TREE;
6557 CASE_FLT_FN (BUILT_IN_ICEIL):
6558 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6561 CASE_FLT_FN (BUILT_IN_IFLOOR):
6562 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6565 CASE_FLT_FN (BUILT_IN_IROUND):
6566 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6569 CASE_FLT_FN (BUILT_IN_IRINT):
6570 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6579 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
6580 return fold_convert_loc (loc,
6581 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
6585 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6586 sizeof (long long) == sizeof (long). */
6587 if (TYPE_PRECISION (long_long_integer_type_node)
6588 == TYPE_PRECISION (long_integer_type_node))
6590 tree newfn = NULL_TREE;
6593 CASE_FLT_FN (BUILT_IN_LLCEIL):
6594 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LCEIL);
6597 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6598 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LFLOOR);
6601 CASE_FLT_FN (BUILT_IN_LLROUND):
6602 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LROUND);
6605 CASE_FLT_FN (BUILT_IN_LLRINT):
6606 newfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_LRINT);
6615 tree newcall = build_call_expr_loc (loc, newfn, 1, arg);
6616 return fold_convert_loc (loc,
6617 TREE_TYPE (TREE_TYPE (fndecl)), newcall);
6624 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
6625 return type. Return NULL_TREE if no simplification can be made. */
6628 fold_builtin_cabs (location_t loc, tree arg, tree type, tree fndecl)
6632 if (!validate_arg (arg, COMPLEX_TYPE)
6633 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6636 /* Calculate the result when the argument is a constant. */
6637 if (TREE_CODE (arg) == COMPLEX_CST
6638 && (res = do_mpfr_arg2 (TREE_REALPART (arg), TREE_IMAGPART (arg),
6642 if (TREE_CODE (arg) == COMPLEX_EXPR)
6644 tree real = TREE_OPERAND (arg, 0);
6645 tree imag = TREE_OPERAND (arg, 1);
6647 /* If either part is zero, cabs is fabs of the other. */
6648 if (real_zerop (real))
6649 return fold_build1_loc (loc, ABS_EXPR, type, imag);
6650 if (real_zerop (imag))
6651 return fold_build1_loc (loc, ABS_EXPR, type, real);
6653 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6654 if (flag_unsafe_math_optimizations
6655 && operand_equal_p (real, imag, OEP_PURE_SAME))
6657 const REAL_VALUE_TYPE sqrt2_trunc
6658 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
6660 return fold_build2_loc (loc, MULT_EXPR, type,
6661 fold_build1_loc (loc, ABS_EXPR, type, real),
6662 build_real (type, sqrt2_trunc));
6666 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
6667 if (TREE_CODE (arg) == NEGATE_EXPR
6668 || TREE_CODE (arg) == CONJ_EXPR)
6669 return build_call_expr_loc (loc, fndecl, 1, TREE_OPERAND (arg, 0));
6671 /* Don't do this when optimizing for size. */
6672 if (flag_unsafe_math_optimizations
6673 && optimize && optimize_function_for_speed_p (cfun))
6675 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
6677 if (sqrtfn != NULL_TREE)
6679 tree rpart, ipart, result;
6681 arg = builtin_save_expr (arg);
6683 rpart = fold_build1_loc (loc, REALPART_EXPR, type, arg);
6684 ipart = fold_build1_loc (loc, IMAGPART_EXPR, type, arg);
6686 rpart = builtin_save_expr (rpart);
6687 ipart = builtin_save_expr (ipart);
6689 result = fold_build2_loc (loc, PLUS_EXPR, type,
6690 fold_build2_loc (loc, MULT_EXPR, type,
6692 fold_build2_loc (loc, MULT_EXPR, type,
6695 return build_call_expr_loc (loc, sqrtfn, 1, result);
6702 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
6703 complex tree type of the result. If NEG is true, the imaginary
6704 zero is negative. */
6707 build_complex_cproj (tree type, bool neg)
6709 REAL_VALUE_TYPE rinf, rzero = dconst0;
6713 return build_complex (type, build_real (TREE_TYPE (type), rinf),
6714 build_real (TREE_TYPE (type), rzero));
6717 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
6718 return type. Return NULL_TREE if no simplification can be made. */
6721 fold_builtin_cproj (location_t loc, tree arg, tree type)
6723 if (!validate_arg (arg, COMPLEX_TYPE)
6724 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != REAL_TYPE)
6727 /* If there are no infinities, return arg. */
6728 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type))))
6729 return non_lvalue_loc (loc, arg);
6731 /* Calculate the result when the argument is a constant. */
6732 if (TREE_CODE (arg) == COMPLEX_CST)
6734 const REAL_VALUE_TYPE *real = TREE_REAL_CST_PTR (TREE_REALPART (arg));
6735 const REAL_VALUE_TYPE *imag = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
6737 if (real_isinf (real) || real_isinf (imag))
6738 return build_complex_cproj (type, imag->sign);
6742 else if (TREE_CODE (arg) == COMPLEX_EXPR)
6744 tree real = TREE_OPERAND (arg, 0);
6745 tree imag = TREE_OPERAND (arg, 1);
6750 /* If the real part is inf and the imag part is known to be
6751 nonnegative, return (inf + 0i). Remember side-effects are
6752 possible in the imag part. */
6753 if (TREE_CODE (real) == REAL_CST
6754 && real_isinf (TREE_REAL_CST_PTR (real))
6755 && tree_expr_nonnegative_p (imag))
6756 return omit_one_operand_loc (loc, type,
6757 build_complex_cproj (type, false),
6760 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
6761 Remember side-effects are possible in the real part. */
6762 if (TREE_CODE (imag) == REAL_CST
6763 && real_isinf (TREE_REAL_CST_PTR (imag)))
6765 omit_one_operand_loc (loc, type,
6766 build_complex_cproj (type, TREE_REAL_CST_PTR
6767 (imag)->sign), arg);
6773 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
6774 Return NULL_TREE if no simplification can be made. */
6777 fold_builtin_sqrt (location_t loc, tree arg, tree type)
6780 enum built_in_function fcode;
6783 if (!validate_arg (arg, REAL_TYPE))
6786 /* Calculate the result when the argument is a constant. */
6787 if ((res = do_mpfr_arg1 (arg, type, mpfr_sqrt, &dconst0, NULL, true)))
6790 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
6791 fcode = builtin_mathfn_code (arg);
6792 if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
6794 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
6795 arg = fold_build2_loc (loc, MULT_EXPR, type,
6796 CALL_EXPR_ARG (arg, 0),
6797 build_real (type, dconsthalf));
6798 return build_call_expr_loc (loc, expfn, 1, arg);
6801 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
6802 if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
6804 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6808 tree arg0 = CALL_EXPR_ARG (arg, 0);
6810 /* The inner root was either sqrt or cbrt. */
6811 /* This was a conditional expression but it triggered a bug
6813 REAL_VALUE_TYPE dconstroot;
6814 if (BUILTIN_SQRT_P (fcode))
6815 dconstroot = dconsthalf;
6817 dconstroot = dconst_third ();
6819 /* Adjust for the outer root. */
6820 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6821 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6822 tree_root = build_real (type, dconstroot);
6823 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
6827 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
6828 if (flag_unsafe_math_optimizations
6829 && (fcode == BUILT_IN_POW
6830 || fcode == BUILT_IN_POWF
6831 || fcode == BUILT_IN_POWL))
6833 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
6834 tree arg0 = CALL_EXPR_ARG (arg, 0);
6835 tree arg1 = CALL_EXPR_ARG (arg, 1);
6837 if (!tree_expr_nonnegative_p (arg0))
6838 arg0 = build1 (ABS_EXPR, type, arg0);
6839 narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
6840 build_real (type, dconsthalf));
6841 return build_call_expr_loc (loc, powfn, 2, arg0, narg1);
6847 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
6848 Return NULL_TREE if no simplification can be made. */
6851 fold_builtin_cbrt (location_t loc, tree arg, tree type)
6853 const enum built_in_function fcode = builtin_mathfn_code (arg);
6856 if (!validate_arg (arg, REAL_TYPE))
6859 /* Calculate the result when the argument is a constant. */
6860 if ((res = do_mpfr_arg1 (arg, type, mpfr_cbrt, NULL, NULL, 0)))
6863 if (flag_unsafe_math_optimizations)
6865 /* Optimize cbrt(expN(x)) -> expN(x/3). */
6866 if (BUILTIN_EXPONENT_P (fcode))
6868 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
6869 const REAL_VALUE_TYPE third_trunc =
6870 real_value_truncate (TYPE_MODE (type), dconst_third ());
6871 arg = fold_build2_loc (loc, MULT_EXPR, type,
6872 CALL_EXPR_ARG (arg, 0),
6873 build_real (type, third_trunc));
6874 return build_call_expr_loc (loc, expfn, 1, arg);
6877 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
6878 if (BUILTIN_SQRT_P (fcode))
6880 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6884 tree arg0 = CALL_EXPR_ARG (arg, 0);
6886 REAL_VALUE_TYPE dconstroot = dconst_third ();
6888 SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1);
6889 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6890 tree_root = build_real (type, dconstroot);
6891 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
6895 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
6896 if (BUILTIN_CBRT_P (fcode))
6898 tree arg0 = CALL_EXPR_ARG (arg, 0);
6899 if (tree_expr_nonnegative_p (arg0))
6901 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6906 REAL_VALUE_TYPE dconstroot;
6908 real_arithmetic (&dconstroot, MULT_EXPR,
6909 dconst_third_ptr (), dconst_third_ptr ());
6910 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
6911 tree_root = build_real (type, dconstroot);
6912 return build_call_expr_loc (loc, powfn, 2, arg0, tree_root);
6917 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
6918 if (fcode == BUILT_IN_POW
6919 || fcode == BUILT_IN_POWF
6920 || fcode == BUILT_IN_POWL)
6922 tree arg00 = CALL_EXPR_ARG (arg, 0);
6923 tree arg01 = CALL_EXPR_ARG (arg, 1);
6924 if (tree_expr_nonnegative_p (arg00))
6926 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg), 0);
6927 const REAL_VALUE_TYPE dconstroot
6928 = real_value_truncate (TYPE_MODE (type), dconst_third ());
6929 tree narg01 = fold_build2_loc (loc, MULT_EXPR, type, arg01,
6930 build_real (type, dconstroot));
6931 return build_call_expr_loc (loc, powfn, 2, arg00, narg01);
6938 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
6939 TYPE is the type of the return value. Return NULL_TREE if no
6940 simplification can be made. */
6943 fold_builtin_cos (location_t loc,
6944 tree arg, tree type, tree fndecl)
6948 if (!validate_arg (arg, REAL_TYPE))
6951 /* Calculate the result when the argument is a constant. */
6952 if ((res = do_mpfr_arg1 (arg, type, mpfr_cos, NULL, NULL, 0)))
6955 /* Optimize cos(-x) into cos (x). */
6956 if ((narg = fold_strip_sign_ops (arg)))
6957 return build_call_expr_loc (loc, fndecl, 1, narg);
6962 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
6963 Return NULL_TREE if no simplification can be made. */
6966 fold_builtin_cosh (location_t loc, tree arg, tree type, tree fndecl)
6968 if (validate_arg (arg, REAL_TYPE))
6972 /* Calculate the result when the argument is a constant. */
6973 if ((res = do_mpfr_arg1 (arg, type, mpfr_cosh, NULL, NULL, 0)))
6976 /* Optimize cosh(-x) into cosh (x). */
6977 if ((narg = fold_strip_sign_ops (arg)))
6978 return build_call_expr_loc (loc, fndecl, 1, narg);
6984 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
6985 argument ARG. TYPE is the type of the return value. Return
6986 NULL_TREE if no simplification can be made. */
6989 fold_builtin_ccos (location_t loc, tree arg, tree type, tree fndecl,
6992 if (validate_arg (arg, COMPLEX_TYPE)
6993 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
6997 /* Calculate the result when the argument is a constant. */
6998 if ((tmp = do_mpc_arg1 (arg, type, (hyper ? mpc_cosh : mpc_cos))))
7001 /* Optimize fn(-x) into fn(x). */
7002 if ((tmp = fold_strip_sign_ops (arg)))
7003 return build_call_expr_loc (loc, fndecl, 1, tmp);
7009 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7010 Return NULL_TREE if no simplification can be made. */
7013 fold_builtin_tan (tree arg, tree type)
7015 enum built_in_function fcode;
7018 if (!validate_arg (arg, REAL_TYPE))
7021 /* Calculate the result when the argument is a constant. */
7022 if ((res = do_mpfr_arg1 (arg, type, mpfr_tan, NULL, NULL, 0)))
7025 /* Optimize tan(atan(x)) = x. */
7026 fcode = builtin_mathfn_code (arg);
7027 if (flag_unsafe_math_optimizations
7028 && (fcode == BUILT_IN_ATAN
7029 || fcode == BUILT_IN_ATANF
7030 || fcode == BUILT_IN_ATANL))
7031 return CALL_EXPR_ARG (arg, 0);
7036 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7037 NULL_TREE if no simplification can be made. */
7040 fold_builtin_sincos (location_t loc,
7041 tree arg0, tree arg1, tree arg2)
7046 if (!validate_arg (arg0, REAL_TYPE)
7047 || !validate_arg (arg1, POINTER_TYPE)
7048 || !validate_arg (arg2, POINTER_TYPE))
7051 type = TREE_TYPE (arg0);
7053 /* Calculate the result when the argument is a constant. */
7054 if ((res = do_mpfr_sincos (arg0, arg1, arg2)))
7057 /* Canonicalize sincos to cexpi. */
7058 if (!TARGET_C99_FUNCTIONS)
7060 fn = mathfn_built_in (type, BUILT_IN_CEXPI);
7064 call = build_call_expr_loc (loc, fn, 1, arg0);
7065 call = builtin_save_expr (call);
7067 return build2 (COMPOUND_EXPR, void_type_node,
7068 build2 (MODIFY_EXPR, void_type_node,
7069 build_fold_indirect_ref_loc (loc, arg1),
7070 build1 (IMAGPART_EXPR, type, call)),
7071 build2 (MODIFY_EXPR, void_type_node,
7072 build_fold_indirect_ref_loc (loc, arg2),
7073 build1 (REALPART_EXPR, type, call)));
7076 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7077 NULL_TREE if no simplification can be made. */
7080 fold_builtin_cexp (location_t loc, tree arg0, tree type)
7083 tree realp, imagp, ifn;
7086 if (!validate_arg (arg0, COMPLEX_TYPE)
7087 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) != REAL_TYPE)
7090 /* Calculate the result when the argument is a constant. */
7091 if ((res = do_mpc_arg1 (arg0, type, mpc_exp)))
7094 rtype = TREE_TYPE (TREE_TYPE (arg0));
7096 /* In case we can figure out the real part of arg0 and it is constant zero
7098 if (!TARGET_C99_FUNCTIONS)
7100 ifn = mathfn_built_in (rtype, BUILT_IN_CEXPI);
7104 if ((realp = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0))
7105 && real_zerop (realp))
7107 tree narg = fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0);
7108 return build_call_expr_loc (loc, ifn, 1, narg);
7111 /* In case we can easily decompose real and imaginary parts split cexp
7112 to exp (r) * cexpi (i). */
7113 if (flag_unsafe_math_optimizations
7116 tree rfn, rcall, icall;
7118 rfn = mathfn_built_in (rtype, BUILT_IN_EXP);
7122 imagp = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
7126 icall = build_call_expr_loc (loc, ifn, 1, imagp);
7127 icall = builtin_save_expr (icall);
7128 rcall = build_call_expr_loc (loc, rfn, 1, realp);
7129 rcall = builtin_save_expr (rcall);
7130 return fold_build2_loc (loc, COMPLEX_EXPR, type,
7131 fold_build2_loc (loc, MULT_EXPR, rtype,
7133 fold_build1_loc (loc, REALPART_EXPR,
7135 fold_build2_loc (loc, MULT_EXPR, rtype,
7137 fold_build1_loc (loc, IMAGPART_EXPR,
7144 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7145 Return NULL_TREE if no simplification can be made. */
7148 fold_builtin_trunc (location_t loc, tree fndecl, tree arg)
7150 if (!validate_arg (arg, REAL_TYPE))
7153 /* Optimize trunc of constant value. */
7154 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7156 REAL_VALUE_TYPE r, x;
7157 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7159 x = TREE_REAL_CST (arg);
7160 real_trunc (&r, TYPE_MODE (type), &x);
7161 return build_real (type, r);
7164 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7167 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7168 Return NULL_TREE if no simplification can be made. */
7171 fold_builtin_floor (location_t loc, tree fndecl, tree arg)
7173 if (!validate_arg (arg, REAL_TYPE))
7176 /* Optimize floor of constant value. */
7177 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7181 x = TREE_REAL_CST (arg);
7182 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7184 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7187 real_floor (&r, TYPE_MODE (type), &x);
7188 return build_real (type, r);
7192 /* Fold floor (x) where x is nonnegative to trunc (x). */
7193 if (tree_expr_nonnegative_p (arg))
7195 tree truncfn = mathfn_built_in (TREE_TYPE (arg), BUILT_IN_TRUNC);
7197 return build_call_expr_loc (loc, truncfn, 1, arg);
7200 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7203 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7204 Return NULL_TREE if no simplification can be made. */
7207 fold_builtin_ceil (location_t loc, tree fndecl, tree arg)
7209 if (!validate_arg (arg, REAL_TYPE))
7212 /* Optimize ceil of constant value. */
7213 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7217 x = TREE_REAL_CST (arg);
7218 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7220 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7223 real_ceil (&r, TYPE_MODE (type), &x);
7224 return build_real (type, r);
7228 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7231 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7232 Return NULL_TREE if no simplification can be made. */
7235 fold_builtin_round (location_t loc, tree fndecl, tree arg)
7237 if (!validate_arg (arg, REAL_TYPE))
7240 /* Optimize round of constant value. */
7241 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7245 x = TREE_REAL_CST (arg);
7246 if (! REAL_VALUE_ISNAN (x) || ! flag_errno_math)
7248 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7251 real_round (&r, TYPE_MODE (type), &x);
7252 return build_real (type, r);
7256 return fold_trunc_transparent_mathfn (loc, fndecl, arg);
7259 /* Fold function call to builtin lround, lroundf or lroundl (or the
7260 corresponding long long versions) and other rounding functions. ARG
7261 is the argument to the call. Return NULL_TREE if no simplification
7265 fold_builtin_int_roundingfn (location_t loc, tree fndecl, tree arg)
7267 if (!validate_arg (arg, REAL_TYPE))
7270 /* Optimize lround of constant value. */
7271 if (TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
7273 const REAL_VALUE_TYPE x = TREE_REAL_CST (arg);
7275 if (real_isfinite (&x))
7277 tree itype = TREE_TYPE (TREE_TYPE (fndecl));
7278 tree ftype = TREE_TYPE (arg);
7282 switch (DECL_FUNCTION_CODE (fndecl))
7284 CASE_FLT_FN (BUILT_IN_IFLOOR):
7285 CASE_FLT_FN (BUILT_IN_LFLOOR):
7286 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7287 real_floor (&r, TYPE_MODE (ftype), &x);
7290 CASE_FLT_FN (BUILT_IN_ICEIL):
7291 CASE_FLT_FN (BUILT_IN_LCEIL):
7292 CASE_FLT_FN (BUILT_IN_LLCEIL):
7293 real_ceil (&r, TYPE_MODE (ftype), &x);
7296 CASE_FLT_FN (BUILT_IN_IROUND):
7297 CASE_FLT_FN (BUILT_IN_LROUND):
7298 CASE_FLT_FN (BUILT_IN_LLROUND):
7299 real_round (&r, TYPE_MODE (ftype), &x);
7306 real_to_integer2 ((HOST_WIDE_INT *)&val.low, &val.high, &r);
7307 if (double_int_fits_to_tree_p (itype, val))
7308 return double_int_to_tree (itype, val);
7312 switch (DECL_FUNCTION_CODE (fndecl))
7314 CASE_FLT_FN (BUILT_IN_LFLOOR):
7315 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7316 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7317 if (tree_expr_nonnegative_p (arg))
7318 return fold_build1_loc (loc, FIX_TRUNC_EXPR,
7319 TREE_TYPE (TREE_TYPE (fndecl)), arg);
7324 return fold_fixed_mathfn (loc, fndecl, arg);
7327 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7328 and their long and long long variants (i.e. ffsl and ffsll). ARG is
7329 the argument to the call. Return NULL_TREE if no simplification can
7333 fold_builtin_bitop (tree fndecl, tree arg)
7335 if (!validate_arg (arg, INTEGER_TYPE))
7338 /* Optimize for constant argument. */
7339 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7341 HOST_WIDE_INT hi, width, result;
7342 unsigned HOST_WIDE_INT lo;
7345 type = TREE_TYPE (arg);
7346 width = TYPE_PRECISION (type);
7347 lo = TREE_INT_CST_LOW (arg);
7349 /* Clear all the bits that are beyond the type's precision. */
7350 if (width > HOST_BITS_PER_WIDE_INT)
7352 hi = TREE_INT_CST_HIGH (arg);
7353 if (width < 2 * HOST_BITS_PER_WIDE_INT)
7354 hi &= ~((unsigned HOST_WIDE_INT) (-1)
7355 << (width - HOST_BITS_PER_WIDE_INT));
7360 if (width < HOST_BITS_PER_WIDE_INT)
7361 lo &= ~((unsigned HOST_WIDE_INT) (-1) << width);
7364 switch (DECL_FUNCTION_CODE (fndecl))
7366 CASE_INT_FN (BUILT_IN_FFS):
7368 result = ffs_hwi (lo);
7370 result = HOST_BITS_PER_WIDE_INT + ffs_hwi (hi);
7375 CASE_INT_FN (BUILT_IN_CLZ):
7377 result = width - floor_log2 (hi) - 1 - HOST_BITS_PER_WIDE_INT;
7379 result = width - floor_log2 (lo) - 1;
7380 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7384 CASE_INT_FN (BUILT_IN_CTZ):
7386 result = ctz_hwi (lo);
7388 result = HOST_BITS_PER_WIDE_INT + ctz_hwi (hi);
7389 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type), result))
7393 CASE_INT_FN (BUILT_IN_CLRSB):
7394 if (width > HOST_BITS_PER_WIDE_INT
7395 && (hi & ((unsigned HOST_WIDE_INT) 1
7396 << (width - HOST_BITS_PER_WIDE_INT - 1))) != 0)
7398 hi = ~hi & ~((unsigned HOST_WIDE_INT) (-1)
7399 << (width - HOST_BITS_PER_WIDE_INT - 1));
7402 else if (width <= HOST_BITS_PER_WIDE_INT
7403 && (lo & ((unsigned HOST_WIDE_INT) 1 << (width - 1))) != 0)
7404 lo = ~lo & ~((unsigned HOST_WIDE_INT) (-1) << (width - 1));
7406 result = width - floor_log2 (hi) - 2 - HOST_BITS_PER_WIDE_INT;
7408 result = width - floor_log2 (lo) - 2;
7413 CASE_INT_FN (BUILT_IN_POPCOUNT):
7416 result++, lo &= lo - 1;
7418 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
7421 CASE_INT_FN (BUILT_IN_PARITY):
7424 result++, lo &= lo - 1;
7426 result++, hi &= (unsigned HOST_WIDE_INT) hi - 1;
7434 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), result);
7440 /* Fold function call to builtin_bswap and the long and long long
7441 variants. Return NULL_TREE if no simplification can be made. */
7443 fold_builtin_bswap (tree fndecl, tree arg)
7445 if (! validate_arg (arg, INTEGER_TYPE))
7448 /* Optimize constant value. */
7449 if (TREE_CODE (arg) == INTEGER_CST && !TREE_OVERFLOW (arg))
7451 HOST_WIDE_INT hi, width, r_hi = 0;
7452 unsigned HOST_WIDE_INT lo, r_lo = 0;
7455 type = TREE_TYPE (arg);
7456 width = TYPE_PRECISION (type);
7457 lo = TREE_INT_CST_LOW (arg);
7458 hi = TREE_INT_CST_HIGH (arg);
7460 switch (DECL_FUNCTION_CODE (fndecl))
7462 case BUILT_IN_BSWAP32:
7463 case BUILT_IN_BSWAP64:
7467 for (s = 0; s < width; s += 8)
7469 int d = width - s - 8;
7470 unsigned HOST_WIDE_INT byte;
7472 if (s < HOST_BITS_PER_WIDE_INT)
7473 byte = (lo >> s) & 0xff;
7475 byte = (hi >> (s - HOST_BITS_PER_WIDE_INT)) & 0xff;
7477 if (d < HOST_BITS_PER_WIDE_INT)
7480 r_hi |= byte << (d - HOST_BITS_PER_WIDE_INT);
7490 if (width < HOST_BITS_PER_WIDE_INT)
7491 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), r_lo);
7493 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl)), r_lo, r_hi);
7499 /* A subroutine of fold_builtin to fold the various logarithmic
7500 functions. Return NULL_TREE if no simplification can me made.
7501 FUNC is the corresponding MPFR logarithm function. */
7504 fold_builtin_logarithm (location_t loc, tree fndecl, tree arg,
7505 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7507 if (validate_arg (arg, REAL_TYPE))
7509 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7511 const enum built_in_function fcode = builtin_mathfn_code (arg);
7513 /* Calculate the result when the argument is a constant. */
7514 if ((res = do_mpfr_arg1 (arg, type, func, &dconst0, NULL, false)))
7517 /* Special case, optimize logN(expN(x)) = x. */
7518 if (flag_unsafe_math_optimizations
7519 && ((func == mpfr_log
7520 && (fcode == BUILT_IN_EXP
7521 || fcode == BUILT_IN_EXPF
7522 || fcode == BUILT_IN_EXPL))
7523 || (func == mpfr_log2
7524 && (fcode == BUILT_IN_EXP2
7525 || fcode == BUILT_IN_EXP2F
7526 || fcode == BUILT_IN_EXP2L))
7527 || (func == mpfr_log10 && (BUILTIN_EXP10_P (fcode)))))
7528 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
7530 /* Optimize logN(func()) for various exponential functions. We
7531 want to determine the value "x" and the power "exponent" in
7532 order to transform logN(x**exponent) into exponent*logN(x). */
7533 if (flag_unsafe_math_optimizations)
7535 tree exponent = 0, x = 0;
7539 CASE_FLT_FN (BUILT_IN_EXP):
7540 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
7541 x = build_real (type, real_value_truncate (TYPE_MODE (type),
7543 exponent = CALL_EXPR_ARG (arg, 0);
7545 CASE_FLT_FN (BUILT_IN_EXP2):
7546 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
7547 x = build_real (type, dconst2);
7548 exponent = CALL_EXPR_ARG (arg, 0);
7550 CASE_FLT_FN (BUILT_IN_EXP10):
7551 CASE_FLT_FN (BUILT_IN_POW10):
7552 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
7554 REAL_VALUE_TYPE dconst10;
7555 real_from_integer (&dconst10, VOIDmode, 10, 0, 0);
7556 x = build_real (type, dconst10);
7558 exponent = CALL_EXPR_ARG (arg, 0);
7560 CASE_FLT_FN (BUILT_IN_SQRT):
7561 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
7562 x = CALL_EXPR_ARG (arg, 0);
7563 exponent = build_real (type, dconsthalf);
7565 CASE_FLT_FN (BUILT_IN_CBRT):
7566 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
7567 x = CALL_EXPR_ARG (arg, 0);
7568 exponent = build_real (type, real_value_truncate (TYPE_MODE (type),
7571 CASE_FLT_FN (BUILT_IN_POW):
7572 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
7573 x = CALL_EXPR_ARG (arg, 0);
7574 exponent = CALL_EXPR_ARG (arg, 1);
7580 /* Now perform the optimization. */
7583 tree logfn = build_call_expr_loc (loc, fndecl, 1, x);
7584 return fold_build2_loc (loc, MULT_EXPR, type, exponent, logfn);
7592 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
7593 NULL_TREE if no simplification can be made. */
7596 fold_builtin_hypot (location_t loc, tree fndecl,
7597 tree arg0, tree arg1, tree type)
7599 tree res, narg0, narg1;
7601 if (!validate_arg (arg0, REAL_TYPE)
7602 || !validate_arg (arg1, REAL_TYPE))
7605 /* Calculate the result when the argument is a constant. */
7606 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_hypot)))
7609 /* If either argument to hypot has a negate or abs, strip that off.
7610 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
7611 narg0 = fold_strip_sign_ops (arg0);
7612 narg1 = fold_strip_sign_ops (arg1);
7615 return build_call_expr_loc (loc, fndecl, 2, narg0 ? narg0 : arg0,
7616 narg1 ? narg1 : arg1);
7619 /* If either argument is zero, hypot is fabs of the other. */
7620 if (real_zerop (arg0))
7621 return fold_build1_loc (loc, ABS_EXPR, type, arg1);
7622 else if (real_zerop (arg1))
7623 return fold_build1_loc (loc, ABS_EXPR, type, arg0);
7625 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7626 if (flag_unsafe_math_optimizations
7627 && operand_equal_p (arg0, arg1, OEP_PURE_SAME))
7629 const REAL_VALUE_TYPE sqrt2_trunc
7630 = real_value_truncate (TYPE_MODE (type), dconst_sqrt2 ());
7631 return fold_build2_loc (loc, MULT_EXPR, type,
7632 fold_build1_loc (loc, ABS_EXPR, type, arg0),
7633 build_real (type, sqrt2_trunc));
7640 /* Fold a builtin function call to pow, powf, or powl. Return
7641 NULL_TREE if no simplification can be made. */
7643 fold_builtin_pow (location_t loc, tree fndecl, tree arg0, tree arg1, tree type)
7647 if (!validate_arg (arg0, REAL_TYPE)
7648 || !validate_arg (arg1, REAL_TYPE))
7651 /* Calculate the result when the argument is a constant. */
7652 if ((res = do_mpfr_arg2 (arg0, arg1, type, mpfr_pow)))
7655 /* Optimize pow(1.0,y) = 1.0. */
7656 if (real_onep (arg0))
7657 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
7659 if (TREE_CODE (arg1) == REAL_CST
7660 && !TREE_OVERFLOW (arg1))
7662 REAL_VALUE_TYPE cint;
7666 c = TREE_REAL_CST (arg1);
7668 /* Optimize pow(x,0.0) = 1.0. */
7669 if (REAL_VALUES_EQUAL (c, dconst0))
7670 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
7673 /* Optimize pow(x,1.0) = x. */
7674 if (REAL_VALUES_EQUAL (c, dconst1))
7677 /* Optimize pow(x,-1.0) = 1.0/x. */
7678 if (REAL_VALUES_EQUAL (c, dconstm1))
7679 return fold_build2_loc (loc, RDIV_EXPR, type,
7680 build_real (type, dconst1), arg0);
7682 /* Optimize pow(x,0.5) = sqrt(x). */
7683 if (flag_unsafe_math_optimizations
7684 && REAL_VALUES_EQUAL (c, dconsthalf))
7686 tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT);
7688 if (sqrtfn != NULL_TREE)
7689 return build_call_expr_loc (loc, sqrtfn, 1, arg0);
7692 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
7693 if (flag_unsafe_math_optimizations)
7695 const REAL_VALUE_TYPE dconstroot
7696 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7698 if (REAL_VALUES_EQUAL (c, dconstroot))
7700 tree cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT);
7701 if (cbrtfn != NULL_TREE)
7702 return build_call_expr_loc (loc, cbrtfn, 1, arg0);
7706 /* Check for an integer exponent. */
7707 n = real_to_integer (&c);
7708 real_from_integer (&cint, VOIDmode, n, n < 0 ? -1 : 0, 0);
7709 if (real_identical (&c, &cint))
7711 /* Attempt to evaluate pow at compile-time, unless this should
7712 raise an exception. */
7713 if (TREE_CODE (arg0) == REAL_CST
7714 && !TREE_OVERFLOW (arg0)
7716 || (!flag_trapping_math && !flag_errno_math)
7717 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), dconst0)))
7722 x = TREE_REAL_CST (arg0);
7723 inexact = real_powi (&x, TYPE_MODE (type), &x, n);
7724 if (flag_unsafe_math_optimizations || !inexact)
7725 return build_real (type, x);
7728 /* Strip sign ops from even integer powers. */
7729 if ((n & 1) == 0 && flag_unsafe_math_optimizations)
7731 tree narg0 = fold_strip_sign_ops (arg0);
7733 return build_call_expr_loc (loc, fndecl, 2, narg0, arg1);
7738 if (flag_unsafe_math_optimizations)
7740 const enum built_in_function fcode = builtin_mathfn_code (arg0);
7742 /* Optimize pow(expN(x),y) = expN(x*y). */
7743 if (BUILTIN_EXPONENT_P (fcode))
7745 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
7746 tree arg = CALL_EXPR_ARG (arg0, 0);
7747 arg = fold_build2_loc (loc, MULT_EXPR, type, arg, arg1);
7748 return build_call_expr_loc (loc, expfn, 1, arg);
7751 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
7752 if (BUILTIN_SQRT_P (fcode))
7754 tree narg0 = CALL_EXPR_ARG (arg0, 0);
7755 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7756 build_real (type, dconsthalf));
7757 return build_call_expr_loc (loc, fndecl, 2, narg0, narg1);
7760 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
7761 if (BUILTIN_CBRT_P (fcode))
7763 tree arg = CALL_EXPR_ARG (arg0, 0);
7764 if (tree_expr_nonnegative_p (arg))
7766 const REAL_VALUE_TYPE dconstroot
7767 = real_value_truncate (TYPE_MODE (type), dconst_third ());
7768 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg1,
7769 build_real (type, dconstroot));
7770 return build_call_expr_loc (loc, fndecl, 2, arg, narg1);
7774 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
7775 if (fcode == BUILT_IN_POW
7776 || fcode == BUILT_IN_POWF
7777 || fcode == BUILT_IN_POWL)
7779 tree arg00 = CALL_EXPR_ARG (arg0, 0);
7780 if (tree_expr_nonnegative_p (arg00))
7782 tree arg01 = CALL_EXPR_ARG (arg0, 1);
7783 tree narg1 = fold_build2_loc (loc, MULT_EXPR, type, arg01, arg1);
7784 return build_call_expr_loc (loc, fndecl, 2, arg00, narg1);
7792 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
7793 Return NULL_TREE if no simplification can be made. */
7795 fold_builtin_powi (location_t loc, tree fndecl ATTRIBUTE_UNUSED,
7796 tree arg0, tree arg1, tree type)
7798 if (!validate_arg (arg0, REAL_TYPE)
7799 || !validate_arg (arg1, INTEGER_TYPE))
7802 /* Optimize pow(1.0,y) = 1.0. */
7803 if (real_onep (arg0))
7804 return omit_one_operand_loc (loc, type, build_real (type, dconst1), arg1);
7806 if (host_integerp (arg1, 0))
7808 HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
7810 /* Evaluate powi at compile-time. */
7811 if (TREE_CODE (arg0) == REAL_CST
7812 && !TREE_OVERFLOW (arg0))
7815 x = TREE_REAL_CST (arg0);
7816 real_powi (&x, TYPE_MODE (type), &x, c);
7817 return build_real (type, x);
7820 /* Optimize pow(x,0) = 1.0. */
7822 return omit_one_operand_loc (loc, type, build_real (type, dconst1),
7825 /* Optimize pow(x,1) = x. */
7829 /* Optimize pow(x,-1) = 1.0/x. */
7831 return fold_build2_loc (loc, RDIV_EXPR, type,
7832 build_real (type, dconst1), arg0);
7838 /* A subroutine of fold_builtin to fold the various exponent
7839 functions. Return NULL_TREE if no simplification can be made.
7840 FUNC is the corresponding MPFR exponent function. */
7843 fold_builtin_exponent (location_t loc, tree fndecl, tree arg,
7844 int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t))
7846 if (validate_arg (arg, REAL_TYPE))
7848 tree type = TREE_TYPE (TREE_TYPE (fndecl));
7851 /* Calculate the result when the argument is a constant. */
7852 if ((res = do_mpfr_arg1 (arg, type, func, NULL, NULL, 0)))
7855 /* Optimize expN(logN(x)) = x. */
7856 if (flag_unsafe_math_optimizations)
7858 const enum built_in_function fcode = builtin_mathfn_code (arg);
7860 if ((func == mpfr_exp
7861 && (fcode == BUILT_IN_LOG
7862 || fcode == BUILT_IN_LOGF
7863 || fcode == BUILT_IN_LOGL))
7864 || (func == mpfr_exp2
7865 && (fcode == BUILT_IN_LOG2
7866 || fcode == BUILT_IN_LOG2F
7867 || fcode == BUILT_IN_LOG2L))
7868 || (func == mpfr_exp10
7869 && (fcode == BUILT_IN_LOG10
7870 || fcode == BUILT_IN_LOG10F
7871 || fcode == BUILT_IN_LOG10L)))
7872 return fold_convert_loc (loc, type, CALL_EXPR_ARG (arg, 0));
7879 /* Return true if VAR is a VAR_DECL or a component thereof. */
7882 var_decl_component_p (tree var)
7885 while (handled_component_p (inner))
7886 inner = TREE_OPERAND (inner, 0);
7887 return SSA_VAR_P (inner);
7890 /* Fold function call to builtin memset. Return
7891 NULL_TREE if no simplification can be made. */
7894 fold_builtin_memset (location_t loc, tree dest, tree c, tree len,
7895 tree type, bool ignore)
7897 tree var, ret, etype;
7898 unsigned HOST_WIDE_INT length, cval;
7900 if (! validate_arg (dest, POINTER_TYPE)
7901 || ! validate_arg (c, INTEGER_TYPE)
7902 || ! validate_arg (len, INTEGER_TYPE))
7905 if (! host_integerp (len, 1))
7908 /* If the LEN parameter is zero, return DEST. */
7909 if (integer_zerop (len))
7910 return omit_one_operand_loc (loc, type, dest, c);
7912 if (TREE_CODE (c) != INTEGER_CST || TREE_SIDE_EFFECTS (dest))
7917 if (TREE_CODE (var) != ADDR_EXPR)
7920 var = TREE_OPERAND (var, 0);
7921 if (TREE_THIS_VOLATILE (var))
7924 etype = TREE_TYPE (var);
7925 if (TREE_CODE (etype) == ARRAY_TYPE)
7926 etype = TREE_TYPE (etype);
7928 if (!INTEGRAL_TYPE_P (etype)
7929 && !POINTER_TYPE_P (etype))
7932 if (! var_decl_component_p (var))
7935 length = tree_low_cst (len, 1);
7936 if (GET_MODE_SIZE (TYPE_MODE (etype)) != length
7937 || get_pointer_alignment (dest) / BITS_PER_UNIT < length)
7940 if (length > HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT)
7943 if (integer_zerop (c))
7947 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || HOST_BITS_PER_WIDE_INT > 64)
7950 cval = TREE_INT_CST_LOW (c);
7954 cval |= (cval << 31) << 1;
7957 ret = build_int_cst_type (etype, cval);
7958 var = build_fold_indirect_ref_loc (loc,
7959 fold_convert_loc (loc,
7960 build_pointer_type (etype),
7962 ret = build2 (MODIFY_EXPR, etype, var, ret);
7966 return omit_one_operand_loc (loc, type, dest, ret);
7969 /* Fold function call to builtin memset. Return
7970 NULL_TREE if no simplification can be made. */
7973 fold_builtin_bzero (location_t loc, tree dest, tree size, bool ignore)
7975 if (! validate_arg (dest, POINTER_TYPE)
7976 || ! validate_arg (size, INTEGER_TYPE))
7982 /* New argument list transforming bzero(ptr x, int y) to
7983 memset(ptr x, int 0, size_t y). This is done this way
7984 so that if it isn't expanded inline, we fallback to
7985 calling bzero instead of memset. */
7987 return fold_builtin_memset (loc, dest, integer_zero_node,
7988 fold_convert_loc (loc, size_type_node, size),
7989 void_type_node, ignore);
7992 /* Fold function call to builtin mem{{,p}cpy,move}. Return
7993 NULL_TREE if no simplification can be made.
7994 If ENDP is 0, return DEST (like memcpy).
7995 If ENDP is 1, return DEST+LEN (like mempcpy).
7996 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
7997 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8001 fold_builtin_memory_op (location_t loc, tree dest, tree src,
8002 tree len, tree type, bool ignore, int endp)
8004 tree destvar, srcvar, expr;
8006 if (! validate_arg (dest, POINTER_TYPE)
8007 || ! validate_arg (src, POINTER_TYPE)
8008 || ! validate_arg (len, INTEGER_TYPE))
8011 /* If the LEN parameter is zero, return DEST. */
8012 if (integer_zerop (len))
8013 return omit_one_operand_loc (loc, type, dest, src);
8015 /* If SRC and DEST are the same (and not volatile), return
8016 DEST{,+LEN,+LEN-1}. */
8017 if (operand_equal_p (src, dest, 0))
8021 tree srctype, desttype;
8022 unsigned int src_align, dest_align;
8027 src_align = get_pointer_alignment (src);
8028 dest_align = get_pointer_alignment (dest);
8030 /* Both DEST and SRC must be pointer types.
8031 ??? This is what old code did. Is the testing for pointer types
8034 If either SRC is readonly or length is 1, we can use memcpy. */
8035 if (!dest_align || !src_align)
8037 if (readonly_data_expr (src)
8038 || (host_integerp (len, 1)
8039 && (MIN (src_align, dest_align) / BITS_PER_UNIT
8040 >= (unsigned HOST_WIDE_INT) tree_low_cst (len, 1))))
8042 tree fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8045 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8048 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8049 if (TREE_CODE (src) == ADDR_EXPR
8050 && TREE_CODE (dest) == ADDR_EXPR)
8052 tree src_base, dest_base, fn;
8053 HOST_WIDE_INT src_offset = 0, dest_offset = 0;
8054 HOST_WIDE_INT size = -1;
8055 HOST_WIDE_INT maxsize = -1;
8057 srcvar = TREE_OPERAND (src, 0);
8058 src_base = get_ref_base_and_extent (srcvar, &src_offset,
8060 destvar = TREE_OPERAND (dest, 0);
8061 dest_base = get_ref_base_and_extent (destvar, &dest_offset,
8063 if (host_integerp (len, 1))
8064 maxsize = tree_low_cst (len, 1);
8067 src_offset /= BITS_PER_UNIT;
8068 dest_offset /= BITS_PER_UNIT;
8069 if (SSA_VAR_P (src_base)
8070 && SSA_VAR_P (dest_base))
8072 if (operand_equal_p (src_base, dest_base, 0)
8073 && ranges_overlap_p (src_offset, maxsize,
8074 dest_offset, maxsize))
8077 else if (TREE_CODE (src_base) == MEM_REF
8078 && TREE_CODE (dest_base) == MEM_REF)
8081 if (! operand_equal_p (TREE_OPERAND (src_base, 0),
8082 TREE_OPERAND (dest_base, 0), 0))
8084 off = double_int_add (mem_ref_offset (src_base),
8085 shwi_to_double_int (src_offset));
8086 if (!double_int_fits_in_shwi_p (off))
8088 src_offset = off.low;
8089 off = double_int_add (mem_ref_offset (dest_base),
8090 shwi_to_double_int (dest_offset));
8091 if (!double_int_fits_in_shwi_p (off))
8093 dest_offset = off.low;
8094 if (ranges_overlap_p (src_offset, maxsize,
8095 dest_offset, maxsize))
8101 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8104 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8107 /* If the destination and source do not alias optimize into
8109 if ((is_gimple_min_invariant (dest)
8110 || TREE_CODE (dest) == SSA_NAME)
8111 && (is_gimple_min_invariant (src)
8112 || TREE_CODE (src) == SSA_NAME))
8115 ao_ref_init_from_ptr_and_size (&destr, dest, len);
8116 ao_ref_init_from_ptr_and_size (&srcr, src, len);
8117 if (!refs_may_alias_p_1 (&destr, &srcr, false))
8120 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8123 return build_call_expr_loc (loc, fn, 3, dest, src, len);
8130 if (!host_integerp (len, 0))
8133 This logic lose for arguments like (type *)malloc (sizeof (type)),
8134 since we strip the casts of up to VOID return value from malloc.
8135 Perhaps we ought to inherit type from non-VOID argument here? */
8138 if (!POINTER_TYPE_P (TREE_TYPE (src))
8139 || !POINTER_TYPE_P (TREE_TYPE (dest)))
8141 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8142 if (TREE_CODE (src) == POINTER_PLUS_EXPR)
8144 tree tem = TREE_OPERAND (src, 0);
8146 if (tem != TREE_OPERAND (src, 0))
8147 src = build1 (NOP_EXPR, TREE_TYPE (tem), src);
8149 if (TREE_CODE (dest) == POINTER_PLUS_EXPR)
8151 tree tem = TREE_OPERAND (dest, 0);
8153 if (tem != TREE_OPERAND (dest, 0))
8154 dest = build1 (NOP_EXPR, TREE_TYPE (tem), dest);
8156 srctype = TREE_TYPE (TREE_TYPE (src));
8157 if (TREE_CODE (srctype) == ARRAY_TYPE
8158 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8160 srctype = TREE_TYPE (srctype);
8162 src = build1 (NOP_EXPR, build_pointer_type (srctype), src);
8164 desttype = TREE_TYPE (TREE_TYPE (dest));
8165 if (TREE_CODE (desttype) == ARRAY_TYPE
8166 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8168 desttype = TREE_TYPE (desttype);
8170 dest = build1 (NOP_EXPR, build_pointer_type (desttype), dest);
8172 if (TREE_ADDRESSABLE (srctype)
8173 || TREE_ADDRESSABLE (desttype))
8176 src_align = get_pointer_alignment (src);
8177 dest_align = get_pointer_alignment (dest);
8178 if (dest_align < TYPE_ALIGN (desttype)
8179 || src_align < TYPE_ALIGN (srctype))
8183 dest = builtin_save_expr (dest);
8185 /* Build accesses at offset zero with a ref-all character type. */
8186 off0 = build_int_cst (build_pointer_type_for_mode (char_type_node,
8187 ptr_mode, true), 0);
8190 STRIP_NOPS (destvar);
8191 if (TREE_CODE (destvar) == ADDR_EXPR
8192 && var_decl_component_p (TREE_OPERAND (destvar, 0))
8193 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype), len))
8194 destvar = fold_build2 (MEM_REF, desttype, destvar, off0);
8196 destvar = NULL_TREE;
8199 STRIP_NOPS (srcvar);
8200 if (TREE_CODE (srcvar) == ADDR_EXPR
8201 && var_decl_component_p (TREE_OPERAND (srcvar, 0))
8202 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype), len))
8205 || src_align >= TYPE_ALIGN (desttype))
8206 srcvar = fold_build2 (MEM_REF, destvar ? desttype : srctype,
8208 else if (!STRICT_ALIGNMENT)
8210 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8212 srcvar = fold_build2 (MEM_REF, srctype, srcvar, off0);
8220 if (srcvar == NULL_TREE && destvar == NULL_TREE)
8223 if (srcvar == NULL_TREE)
8226 if (src_align >= TYPE_ALIGN (desttype))
8227 srcvar = fold_build2 (MEM_REF, desttype, src, off0);
8230 if (STRICT_ALIGNMENT)
8232 srctype = build_aligned_type (TYPE_MAIN_VARIANT (desttype),
8234 srcvar = fold_build2 (MEM_REF, srctype, src, off0);
8237 else if (destvar == NULL_TREE)
8240 if (dest_align >= TYPE_ALIGN (srctype))
8241 destvar = fold_build2 (MEM_REF, srctype, dest, off0);
8244 if (STRICT_ALIGNMENT)
8246 desttype = build_aligned_type (TYPE_MAIN_VARIANT (srctype),
8248 destvar = fold_build2 (MEM_REF, desttype, dest, off0);
8252 expr = build2 (MODIFY_EXPR, TREE_TYPE (destvar), destvar, srcvar);
8258 if (endp == 0 || endp == 3)
8259 return omit_one_operand_loc (loc, type, dest, expr);
8265 len = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (len), len,
8268 dest = fold_build_pointer_plus_loc (loc, dest, len);
8269 dest = fold_convert_loc (loc, type, dest);
8271 dest = omit_one_operand_loc (loc, type, dest, expr);
8275 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8276 If LEN is not NULL, it represents the length of the string to be
8277 copied. Return NULL_TREE if no simplification can be made. */
8280 fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
8284 if (!validate_arg (dest, POINTER_TYPE)
8285 || !validate_arg (src, POINTER_TYPE))
8288 /* If SRC and DEST are the same (and not volatile), return DEST. */
8289 if (operand_equal_p (src, dest, 0))
8290 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
8292 if (optimize_function_for_size_p (cfun))
8295 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8301 len = c_strlen (src, 1);
8302 if (! len || TREE_SIDE_EFFECTS (len))
8306 len = fold_convert_loc (loc, size_type_node, len);
8307 len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
8308 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8309 build_call_expr_loc (loc, fn, 3, dest, src, len));
8312 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8313 Return NULL_TREE if no simplification can be made. */
8316 fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
8318 tree fn, len, lenp1, call, type;
8320 if (!validate_arg (dest, POINTER_TYPE)
8321 || !validate_arg (src, POINTER_TYPE))
8324 len = c_strlen (src, 1);
8326 || TREE_CODE (len) != INTEGER_CST)
8329 if (optimize_function_for_size_p (cfun)
8330 /* If length is zero it's small enough. */
8331 && !integer_zerop (len))
8334 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8338 lenp1 = size_binop_loc (loc, PLUS_EXPR,
8339 fold_convert_loc (loc, size_type_node, len),
8340 build_int_cst (size_type_node, 1));
8341 /* We use dest twice in building our expression. Save it from
8342 multiple expansions. */
8343 dest = builtin_save_expr (dest);
8344 call = build_call_expr_loc (loc, fn, 3, dest, src, lenp1);
8346 type = TREE_TYPE (TREE_TYPE (fndecl));
8347 dest = fold_build_pointer_plus_loc (loc, dest, len);
8348 dest = fold_convert_loc (loc, type, dest);
8349 dest = omit_one_operand_loc (loc, type, dest, call);
8353 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
8354 If SLEN is not NULL, it represents the length of the source string.
8355 Return NULL_TREE if no simplification can be made. */
8358 fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
8359 tree src, tree len, tree slen)
8363 if (!validate_arg (dest, POINTER_TYPE)
8364 || !validate_arg (src, POINTER_TYPE)
8365 || !validate_arg (len, INTEGER_TYPE))
8368 /* If the LEN parameter is zero, return DEST. */
8369 if (integer_zerop (len))
8370 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
8372 /* We can't compare slen with len as constants below if len is not a
8374 if (len == 0 || TREE_CODE (len) != INTEGER_CST)
8378 slen = c_strlen (src, 1);
8380 /* Now, we must be passed a constant src ptr parameter. */
8381 if (slen == 0 || TREE_CODE (slen) != INTEGER_CST)
8384 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
8386 /* We do not support simplification of this case, though we do
8387 support it when expanding trees into RTL. */
8388 /* FIXME: generate a call to __builtin_memset. */
8389 if (tree_int_cst_lt (slen, len))
8392 /* OK transform into builtin memcpy. */
8393 fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
8397 len = fold_convert_loc (loc, size_type_node, len);
8398 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
8399 build_call_expr_loc (loc, fn, 3, dest, src, len));
8402 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8403 arguments to the call, and TYPE is its return type.
8404 Return NULL_TREE if no simplification can be made. */
8407 fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
8409 if (!validate_arg (arg1, POINTER_TYPE)
8410 || !validate_arg (arg2, INTEGER_TYPE)
8411 || !validate_arg (len, INTEGER_TYPE))
8417 if (TREE_CODE (arg2) != INTEGER_CST
8418 || !host_integerp (len, 1))
8421 p1 = c_getstr (arg1);
8422 if (p1 && compare_tree_int (len, strlen (p1) + 1) <= 0)
8428 if (target_char_cast (arg2, &c))
8431 r = (char *) memchr (p1, c, tree_low_cst (len, 1));
8434 return build_int_cst (TREE_TYPE (arg1), 0);
8436 tem = fold_build_pointer_plus_hwi_loc (loc, arg1, r - p1);
8437 return fold_convert_loc (loc, type, tem);
8443 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8444 Return NULL_TREE if no simplification can be made. */
8447 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8449 const char *p1, *p2;
8451 if (!validate_arg (arg1, POINTER_TYPE)
8452 || !validate_arg (arg2, POINTER_TYPE)
8453 || !validate_arg (len, INTEGER_TYPE))
8456 /* If the LEN parameter is zero, return zero. */
8457 if (integer_zerop (len))
8458 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8461 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8462 if (operand_equal_p (arg1, arg2, 0))
8463 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8465 p1 = c_getstr (arg1);
8466 p2 = c_getstr (arg2);
8468 /* If all arguments are constant, and the value of len is not greater
8469 than the lengths of arg1 and arg2, evaluate at compile-time. */
8470 if (host_integerp (len, 1) && p1 && p2
8471 && compare_tree_int (len, strlen (p1) + 1) <= 0
8472 && compare_tree_int (len, strlen (p2) + 1) <= 0)
8474 const int r = memcmp (p1, p2, tree_low_cst (len, 1));
8477 return integer_one_node;
8479 return integer_minus_one_node;
8481 return integer_zero_node;
8484 /* If len parameter is one, return an expression corresponding to
8485 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8486 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8488 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8489 tree cst_uchar_ptr_node
8490 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8493 = fold_convert_loc (loc, integer_type_node,
8494 build1 (INDIRECT_REF, cst_uchar_node,
8495 fold_convert_loc (loc,
8499 = fold_convert_loc (loc, integer_type_node,
8500 build1 (INDIRECT_REF, cst_uchar_node,
8501 fold_convert_loc (loc,
8504 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8510 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8511 Return NULL_TREE if no simplification can be made. */
8514 fold_builtin_strcmp (location_t loc, tree arg1, tree arg2)
8516 const char *p1, *p2;
8518 if (!validate_arg (arg1, POINTER_TYPE)
8519 || !validate_arg (arg2, POINTER_TYPE))
8522 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8523 if (operand_equal_p (arg1, arg2, 0))
8524 return integer_zero_node;
8526 p1 = c_getstr (arg1);
8527 p2 = c_getstr (arg2);
8531 const int i = strcmp (p1, p2);
8533 return integer_minus_one_node;
8535 return integer_one_node;
8537 return integer_zero_node;
8540 /* If the second arg is "", return *(const unsigned char*)arg1. */
8541 if (p2 && *p2 == '\0')
8543 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8544 tree cst_uchar_ptr_node
8545 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8547 return fold_convert_loc (loc, integer_type_node,
8548 build1 (INDIRECT_REF, cst_uchar_node,
8549 fold_convert_loc (loc,
8554 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8555 if (p1 && *p1 == '\0')
8557 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8558 tree cst_uchar_ptr_node
8559 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8562 = fold_convert_loc (loc, integer_type_node,
8563 build1 (INDIRECT_REF, cst_uchar_node,
8564 fold_convert_loc (loc,
8567 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8573 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8574 Return NULL_TREE if no simplification can be made. */
8577 fold_builtin_strncmp (location_t loc, tree arg1, tree arg2, tree len)
8579 const char *p1, *p2;
8581 if (!validate_arg (arg1, POINTER_TYPE)
8582 || !validate_arg (arg2, POINTER_TYPE)
8583 || !validate_arg (len, INTEGER_TYPE))
8586 /* If the LEN parameter is zero, return zero. */
8587 if (integer_zerop (len))
8588 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8591 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8592 if (operand_equal_p (arg1, arg2, 0))
8593 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8595 p1 = c_getstr (arg1);
8596 p2 = c_getstr (arg2);
8598 if (host_integerp (len, 1) && p1 && p2)
8600 const int i = strncmp (p1, p2, tree_low_cst (len, 1));
8602 return integer_one_node;
8604 return integer_minus_one_node;
8606 return integer_zero_node;
8609 /* If the second arg is "", and the length is greater than zero,
8610 return *(const unsigned char*)arg1. */
8611 if (p2 && *p2 == '\0'
8612 && TREE_CODE (len) == INTEGER_CST
8613 && tree_int_cst_sgn (len) == 1)
8615 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8616 tree cst_uchar_ptr_node
8617 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8619 return fold_convert_loc (loc, integer_type_node,
8620 build1 (INDIRECT_REF, cst_uchar_node,
8621 fold_convert_loc (loc,
8626 /* If the first arg is "", and the length is greater than zero,
8627 return -*(const unsigned char*)arg2. */
8628 if (p1 && *p1 == '\0'
8629 && TREE_CODE (len) == INTEGER_CST
8630 && tree_int_cst_sgn (len) == 1)
8632 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8633 tree cst_uchar_ptr_node
8634 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8636 tree temp = fold_convert_loc (loc, integer_type_node,
8637 build1 (INDIRECT_REF, cst_uchar_node,
8638 fold_convert_loc (loc,
8641 return fold_build1_loc (loc, NEGATE_EXPR, integer_type_node, temp);
8644 /* If len parameter is one, return an expression corresponding to
8645 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8646 if (host_integerp (len, 1) && tree_low_cst (len, 1) == 1)
8648 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8649 tree cst_uchar_ptr_node
8650 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8652 tree ind1 = fold_convert_loc (loc, integer_type_node,
8653 build1 (INDIRECT_REF, cst_uchar_node,
8654 fold_convert_loc (loc,
8657 tree ind2 = fold_convert_loc (loc, integer_type_node,
8658 build1 (INDIRECT_REF, cst_uchar_node,
8659 fold_convert_loc (loc,
8662 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8668 /* Fold function call to builtin signbit, signbitf or signbitl with argument
8669 ARG. Return NULL_TREE if no simplification can be made. */
8672 fold_builtin_signbit (location_t loc, tree arg, tree type)
8674 if (!validate_arg (arg, REAL_TYPE))
8677 /* If ARG is a compile-time constant, determine the result. */
8678 if (TREE_CODE (arg) == REAL_CST
8679 && !TREE_OVERFLOW (arg))
8683 c = TREE_REAL_CST (arg);
8684 return (REAL_VALUE_NEGATIVE (c)
8685 ? build_one_cst (type)
8686 : build_zero_cst (type));
8689 /* If ARG is non-negative, the result is always zero. */
8690 if (tree_expr_nonnegative_p (arg))
8691 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8693 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8694 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg))))
8695 return fold_convert (type,
8696 fold_build2_loc (loc, LT_EXPR, boolean_type_node, arg,
8697 build_real (TREE_TYPE (arg), dconst0)));
8702 /* Fold function call to builtin copysign, copysignf or copysignl with
8703 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
8707 fold_builtin_copysign (location_t loc, tree fndecl,
8708 tree arg1, tree arg2, tree type)
8712 if (!validate_arg (arg1, REAL_TYPE)
8713 || !validate_arg (arg2, REAL_TYPE))
8716 /* copysign(X,X) is X. */
8717 if (operand_equal_p (arg1, arg2, 0))
8718 return fold_convert_loc (loc, type, arg1);
8720 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8721 if (TREE_CODE (arg1) == REAL_CST
8722 && TREE_CODE (arg2) == REAL_CST
8723 && !TREE_OVERFLOW (arg1)
8724 && !TREE_OVERFLOW (arg2))
8726 REAL_VALUE_TYPE c1, c2;
8728 c1 = TREE_REAL_CST (arg1);
8729 c2 = TREE_REAL_CST (arg2);
8730 /* c1.sign := c2.sign. */
8731 real_copysign (&c1, &c2);
8732 return build_real (type, c1);
8735 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8736 Remember to evaluate Y for side-effects. */
8737 if (tree_expr_nonnegative_p (arg2))
8738 return omit_one_operand_loc (loc, type,
8739 fold_build1_loc (loc, ABS_EXPR, type, arg1),
8742 /* Strip sign changing operations for the first argument. */
8743 tem = fold_strip_sign_ops (arg1);
8745 return build_call_expr_loc (loc, fndecl, 2, tem, arg2);
8750 /* Fold a call to builtin isascii with argument ARG. */
8753 fold_builtin_isascii (location_t loc, tree arg)
8755 if (!validate_arg (arg, INTEGER_TYPE))
8759 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8760 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8761 build_int_cst (integer_type_node,
8762 ~ (unsigned HOST_WIDE_INT) 0x7f));
8763 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8764 arg, integer_zero_node);
8768 /* Fold a call to builtin toascii with argument ARG. */
8771 fold_builtin_toascii (location_t loc, tree arg)
8773 if (!validate_arg (arg, INTEGER_TYPE))
8776 /* Transform toascii(c) -> (c & 0x7f). */
8777 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8778 build_int_cst (integer_type_node, 0x7f));
8781 /* Fold a call to builtin isdigit with argument ARG. */
8784 fold_builtin_isdigit (location_t loc, tree arg)
8786 if (!validate_arg (arg, INTEGER_TYPE))
8790 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8791 /* According to the C standard, isdigit is unaffected by locale.
8792 However, it definitely is affected by the target character set. */
8793 unsigned HOST_WIDE_INT target_digit0
8794 = lang_hooks.to_target_charset ('0');
8796 if (target_digit0 == 0)
8799 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8800 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8801 build_int_cst (unsigned_type_node, target_digit0));
8802 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8803 build_int_cst (unsigned_type_node, 9));
8807 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8810 fold_builtin_fabs (location_t loc, tree arg, tree type)
8812 if (!validate_arg (arg, REAL_TYPE))
8815 arg = fold_convert_loc (loc, type, arg);
8816 if (TREE_CODE (arg) == REAL_CST)
8817 return fold_abs_const (arg, type);
8818 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8821 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8824 fold_builtin_abs (location_t loc, tree arg, tree type)
8826 if (!validate_arg (arg, INTEGER_TYPE))
8829 arg = fold_convert_loc (loc, type, arg);
8830 if (TREE_CODE (arg) == INTEGER_CST)
8831 return fold_abs_const (arg, type);
8832 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8835 /* Fold a fma operation with arguments ARG[012]. */
8838 fold_fma (location_t loc ATTRIBUTE_UNUSED,
8839 tree type, tree arg0, tree arg1, tree arg2)
8841 if (TREE_CODE (arg0) == REAL_CST
8842 && TREE_CODE (arg1) == REAL_CST
8843 && TREE_CODE (arg2) == REAL_CST)
8844 return do_mpfr_arg3 (arg0, arg1, arg2, type, mpfr_fma);
8849 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
8852 fold_builtin_fma (location_t loc, tree arg0, tree arg1, tree arg2, tree type)
8854 if (validate_arg (arg0, REAL_TYPE)
8855 && validate_arg(arg1, REAL_TYPE)
8856 && validate_arg(arg2, REAL_TYPE))
8858 tree tem = fold_fma (loc, type, arg0, arg1, arg2);
8862 /* ??? Only expand to FMA_EXPR if it's directly supported. */
8863 if (optab_handler (fma_optab, TYPE_MODE (type)) != CODE_FOR_nothing)
8864 return fold_build3_loc (loc, FMA_EXPR, type, arg0, arg1, arg2);
8869 /* Fold a call to builtin fmin or fmax. */
8872 fold_builtin_fmin_fmax (location_t loc, tree arg0, tree arg1,
8873 tree type, bool max)
8875 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, REAL_TYPE))
8877 /* Calculate the result when the argument is a constant. */
8878 tree res = do_mpfr_arg2 (arg0, arg1, type, (max ? mpfr_max : mpfr_min));
8883 /* If either argument is NaN, return the other one. Avoid the
8884 transformation if we get (and honor) a signalling NaN. Using
8885 omit_one_operand() ensures we create a non-lvalue. */
8886 if (TREE_CODE (arg0) == REAL_CST
8887 && real_isnan (&TREE_REAL_CST (arg0))
8888 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8889 || ! TREE_REAL_CST (arg0).signalling))
8890 return omit_one_operand_loc (loc, type, arg1, arg0);
8891 if (TREE_CODE (arg1) == REAL_CST
8892 && real_isnan (&TREE_REAL_CST (arg1))
8893 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
8894 || ! TREE_REAL_CST (arg1).signalling))
8895 return omit_one_operand_loc (loc, type, arg0, arg1);
8897 /* Transform fmin/fmax(x,x) -> x. */
8898 if (operand_equal_p (arg0, arg1, OEP_PURE_SAME))
8899 return omit_one_operand_loc (loc, type, arg0, arg1);
8901 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
8902 functions to return the numeric arg if the other one is NaN.
8903 These tree codes don't honor that, so only transform if
8904 -ffinite-math-only is set. C99 doesn't require -0.0 to be
8905 handled, so we don't have to worry about it either. */
8906 if (flag_finite_math_only)
8907 return fold_build2_loc (loc, (max ? MAX_EXPR : MIN_EXPR), type,
8908 fold_convert_loc (loc, type, arg0),
8909 fold_convert_loc (loc, type, arg1));
8914 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8917 fold_builtin_carg (location_t loc, tree arg, tree type)
8919 if (validate_arg (arg, COMPLEX_TYPE)
8920 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8922 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8926 tree new_arg = builtin_save_expr (arg);
8927 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8928 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8929 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8936 /* Fold a call to builtin logb/ilogb. */
8939 fold_builtin_logb (location_t loc, tree arg, tree rettype)
8941 if (! validate_arg (arg, REAL_TYPE))
8946 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
8948 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
8954 /* If arg is Inf or NaN and we're logb, return it. */
8955 if (TREE_CODE (rettype) == REAL_TYPE)
8956 return fold_convert_loc (loc, rettype, arg);
8957 /* Fall through... */
8959 /* Zero may set errno and/or raise an exception for logb, also
8960 for ilogb we don't know FP_ILOGB0. */
8963 /* For normal numbers, proceed iff radix == 2. In GCC,
8964 normalized significands are in the range [0.5, 1.0). We
8965 want the exponent as if they were [1.0, 2.0) so get the
8966 exponent and subtract 1. */
8967 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
8968 return fold_convert_loc (loc, rettype,
8969 build_int_cst (integer_type_node,
8970 REAL_EXP (value)-1));
8978 /* Fold a call to builtin significand, if radix == 2. */
8981 fold_builtin_significand (location_t loc, tree arg, tree rettype)
8983 if (! validate_arg (arg, REAL_TYPE))
8988 if (TREE_CODE (arg) == REAL_CST && ! TREE_OVERFLOW (arg))
8990 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg);
8997 /* If arg is +-0, +-Inf or +-NaN, then return it. */
8998 return fold_convert_loc (loc, rettype, arg);
9000 /* For normal numbers, proceed iff radix == 2. */
9001 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)))->b == 2)
9003 REAL_VALUE_TYPE result = *value;
9004 /* In GCC, normalized significands are in the range [0.5,
9005 1.0). We want them to be [1.0, 2.0) so set the
9007 SET_REAL_EXP (&result, 1);
9008 return build_real (rettype, result);
9017 /* Fold a call to builtin frexp, we can assume the base is 2. */
9020 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
9022 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9027 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9030 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9032 /* Proceed if a valid pointer type was passed in. */
9033 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
9035 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9041 /* For +-0, return (*exp = 0, +-0). */
9042 exp = integer_zero_node;
9047 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9048 return omit_one_operand_loc (loc, rettype, arg0, arg1);
9051 /* Since the frexp function always expects base 2, and in
9052 GCC normalized significands are already in the range
9053 [0.5, 1.0), we have exactly what frexp wants. */
9054 REAL_VALUE_TYPE frac_rvt = *value;
9055 SET_REAL_EXP (&frac_rvt, 0);
9056 frac = build_real (rettype, frac_rvt);
9057 exp = build_int_cst (integer_type_node, REAL_EXP (value));
9064 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9065 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9066 TREE_SIDE_EFFECTS (arg1) = 1;
9067 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9073 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9074 then we can assume the base is two. If it's false, then we have to
9075 check the mode of the TYPE parameter in certain cases. */
9078 fold_builtin_load_exponent (location_t loc, tree arg0, tree arg1,
9079 tree type, bool ldexp)
9081 if (validate_arg (arg0, REAL_TYPE) && validate_arg (arg1, INTEGER_TYPE))
9086 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9087 if (real_zerop (arg0) || integer_zerop (arg1)
9088 || (TREE_CODE (arg0) == REAL_CST
9089 && !real_isfinite (&TREE_REAL_CST (arg0))))
9090 return omit_one_operand_loc (loc, type, arg0, arg1);
9092 /* If both arguments are constant, then try to evaluate it. */
9093 if ((ldexp || REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2)
9094 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
9095 && host_integerp (arg1, 0))
9097 /* Bound the maximum adjustment to twice the range of the
9098 mode's valid exponents. Use abs to ensure the range is
9099 positive as a sanity check. */
9100 const long max_exp_adj = 2 *
9101 labs (REAL_MODE_FORMAT (TYPE_MODE (type))->emax
9102 - REAL_MODE_FORMAT (TYPE_MODE (type))->emin);
9104 /* Get the user-requested adjustment. */
9105 const HOST_WIDE_INT req_exp_adj = tree_low_cst (arg1, 0);
9107 /* The requested adjustment must be inside this range. This
9108 is a preliminary cap to avoid things like overflow, we
9109 may still fail to compute the result for other reasons. */
9110 if (-max_exp_adj < req_exp_adj && req_exp_adj < max_exp_adj)
9112 REAL_VALUE_TYPE initial_result;
9114 real_ldexp (&initial_result, &TREE_REAL_CST (arg0), req_exp_adj);
9116 /* Ensure we didn't overflow. */
9117 if (! real_isinf (&initial_result))
9119 const REAL_VALUE_TYPE trunc_result
9120 = real_value_truncate (TYPE_MODE (type), initial_result);
9122 /* Only proceed if the target mode can hold the
9124 if (REAL_VALUES_EQUAL (initial_result, trunc_result))
9125 return build_real (type, trunc_result);
9134 /* Fold a call to builtin modf. */
9137 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9139 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9144 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9147 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9149 /* Proceed if a valid pointer type was passed in. */
9150 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9152 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9153 REAL_VALUE_TYPE trunc, frac;
9159 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9160 trunc = frac = *value;
9163 /* For +-Inf, return (*arg1 = arg0, +-0). */
9165 frac.sign = value->sign;
9169 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9170 real_trunc (&trunc, VOIDmode, value);
9171 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9172 /* If the original number was negative and already
9173 integral, then the fractional part is -0.0. */
9174 if (value->sign && frac.cl == rvc_zero)
9175 frac.sign = value->sign;
9179 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9180 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9181 build_real (rettype, trunc));
9182 TREE_SIDE_EFFECTS (arg1) = 1;
9183 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9184 build_real (rettype, frac));
9190 /* Given a location LOC, an interclass builtin function decl FNDECL
9191 and its single argument ARG, return an folded expression computing
9192 the same, or NULL_TREE if we either couldn't or didn't want to fold
9193 (the latter happen if there's an RTL instruction available). */
9196 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9198 enum machine_mode mode;
9200 if (!validate_arg (arg, REAL_TYPE))
9203 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9206 mode = TYPE_MODE (TREE_TYPE (arg));
9208 /* If there is no optab, try generic code. */
9209 switch (DECL_FUNCTION_CODE (fndecl))
9213 CASE_FLT_FN (BUILT_IN_ISINF):
9215 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9216 tree const isgr_fn = built_in_decls[BUILT_IN_ISGREATER];
9217 tree const type = TREE_TYPE (arg);
9221 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9222 real_from_string (&r, buf);
9223 result = build_call_expr (isgr_fn, 2,
9224 fold_build1_loc (loc, ABS_EXPR, type, arg),
9225 build_real (type, r));
9228 CASE_FLT_FN (BUILT_IN_FINITE):
9229 case BUILT_IN_ISFINITE:
9231 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9232 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9233 tree const type = TREE_TYPE (arg);
9237 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9238 real_from_string (&r, buf);
9239 result = build_call_expr (isle_fn, 2,
9240 fold_build1_loc (loc, ABS_EXPR, type, arg),
9241 build_real (type, r));
9242 /*result = fold_build2_loc (loc, UNGT_EXPR,
9243 TREE_TYPE (TREE_TYPE (fndecl)),
9244 fold_build1_loc (loc, ABS_EXPR, type, arg),
9245 build_real (type, r));
9246 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9247 TREE_TYPE (TREE_TYPE (fndecl)),
9251 case BUILT_IN_ISNORMAL:
9253 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9254 islessequal(fabs(x),DBL_MAX). */
9255 tree const isle_fn = built_in_decls[BUILT_IN_ISLESSEQUAL];
9256 tree const isge_fn = built_in_decls[BUILT_IN_ISGREATEREQUAL];
9257 tree const type = TREE_TYPE (arg);
9258 REAL_VALUE_TYPE rmax, rmin;
9261 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
9262 real_from_string (&rmax, buf);
9263 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9264 real_from_string (&rmin, buf);
9265 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9266 result = build_call_expr (isle_fn, 2, arg,
9267 build_real (type, rmax));
9268 result = fold_build2 (BIT_AND_EXPR, integer_type_node, result,
9269 build_call_expr (isge_fn, 2, arg,
9270 build_real (type, rmin)));
9280 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9281 ARG is the argument for the call. */
9284 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9286 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9289 if (!validate_arg (arg, REAL_TYPE))
9292 switch (builtin_index)
9294 case BUILT_IN_ISINF:
9295 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9296 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9298 if (TREE_CODE (arg) == REAL_CST)
9300 r = TREE_REAL_CST (arg);
9301 if (real_isinf (&r))
9302 return real_compare (GT_EXPR, &r, &dconst0)
9303 ? integer_one_node : integer_minus_one_node;
9305 return integer_zero_node;
9310 case BUILT_IN_ISINF_SIGN:
9312 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9313 /* In a boolean context, GCC will fold the inner COND_EXPR to
9314 1. So e.g. "if (isinf_sign(x))" would be folded to just
9315 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9316 tree signbit_fn = mathfn_built_in_1 (TREE_TYPE (arg), BUILT_IN_SIGNBIT, 0);
9317 tree isinf_fn = built_in_decls[BUILT_IN_ISINF];
9318 tree tmp = NULL_TREE;
9320 arg = builtin_save_expr (arg);
9322 if (signbit_fn && isinf_fn)
9324 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9325 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9327 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9328 signbit_call, integer_zero_node);
9329 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9330 isinf_call, integer_zero_node);
9332 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9333 integer_minus_one_node, integer_one_node);
9334 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9342 case BUILT_IN_ISFINITE:
9343 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg)))
9344 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg))))
9345 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9347 if (TREE_CODE (arg) == REAL_CST)
9349 r = TREE_REAL_CST (arg);
9350 return real_isfinite (&r) ? integer_one_node : integer_zero_node;
9355 case BUILT_IN_ISNAN:
9356 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg))))
9357 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9359 if (TREE_CODE (arg) == REAL_CST)
9361 r = TREE_REAL_CST (arg);
9362 return real_isnan (&r) ? integer_one_node : integer_zero_node;
9365 arg = builtin_save_expr (arg);
9366 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9373 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9374 This builtin will generate code to return the appropriate floating
9375 point classification depending on the value of the floating point
9376 number passed in. The possible return values must be supplied as
9377 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9378 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9379 one floating point argument which is "type generic". */
9382 fold_builtin_fpclassify (location_t loc, tree exp)
9384 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9385 arg, type, res, tmp;
9386 enum machine_mode mode;
9390 /* Verify the required arguments in the original call. */
9391 if (!validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE,
9392 INTEGER_TYPE, INTEGER_TYPE,
9393 INTEGER_TYPE, REAL_TYPE, VOID_TYPE))
9396 fp_nan = CALL_EXPR_ARG (exp, 0);
9397 fp_infinite = CALL_EXPR_ARG (exp, 1);
9398 fp_normal = CALL_EXPR_ARG (exp, 2);
9399 fp_subnormal = CALL_EXPR_ARG (exp, 3);
9400 fp_zero = CALL_EXPR_ARG (exp, 4);
9401 arg = CALL_EXPR_ARG (exp, 5);
9402 type = TREE_TYPE (arg);
9403 mode = TYPE_MODE (type);
9404 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9408 (fabs(x) == Inf ? FP_INFINITE :
9409 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9410 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9412 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9413 build_real (type, dconst0));
9414 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9415 tmp, fp_zero, fp_subnormal);
9417 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9418 real_from_string (&r, buf);
9419 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9420 arg, build_real (type, r));
9421 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9423 if (HONOR_INFINITIES (mode))
9426 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9427 build_real (type, r));
9428 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9432 if (HONOR_NANS (mode))
9434 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9435 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9441 /* Fold a call to an unordered comparison function such as
9442 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9443 being called and ARG0 and ARG1 are the arguments for the call.
9444 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9445 the opposite of the desired result. UNORDERED_CODE is used
9446 for modes that can hold NaNs and ORDERED_CODE is used for
9450 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9451 enum tree_code unordered_code,
9452 enum tree_code ordered_code)
9454 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9455 enum tree_code code;
9457 enum tree_code code0, code1;
9458 tree cmp_type = NULL_TREE;
9460 type0 = TREE_TYPE (arg0);
9461 type1 = TREE_TYPE (arg1);
9463 code0 = TREE_CODE (type0);
9464 code1 = TREE_CODE (type1);
9466 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9467 /* Choose the wider of two real types. */
9468 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9470 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9472 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9475 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9476 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9478 if (unordered_code == UNORDERED_EXPR)
9480 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9481 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9482 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9485 code = HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) ? unordered_code
9487 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9488 fold_build2_loc (loc, code, type, arg0, arg1));
9491 /* Fold a call to built-in function FNDECL with 0 arguments.
9492 IGNORE is true if the result of the function call is ignored. This
9493 function returns NULL_TREE if no simplification was possible. */
9496 fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
9498 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9499 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9502 CASE_FLT_FN (BUILT_IN_INF):
9503 case BUILT_IN_INFD32:
9504 case BUILT_IN_INFD64:
9505 case BUILT_IN_INFD128:
9506 return fold_builtin_inf (loc, type, true);
9508 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9509 return fold_builtin_inf (loc, type, false);
9511 case BUILT_IN_CLASSIFY_TYPE:
9512 return fold_builtin_classify_type (NULL_TREE);
9520 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9521 IGNORE is true if the result of the function call is ignored. This
9522 function returns NULL_TREE if no simplification was possible. */
9525 fold_builtin_1 (location_t loc, tree fndecl, tree arg0, bool ignore)
9527 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9528 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9531 case BUILT_IN_CONSTANT_P:
9533 tree val = fold_builtin_constant_p (arg0);
9535 /* Gimplification will pull the CALL_EXPR for the builtin out of
9536 an if condition. When not optimizing, we'll not CSE it back.
9537 To avoid link error types of regressions, return false now. */
9538 if (!val && !optimize)
9539 val = integer_zero_node;
9544 case BUILT_IN_CLASSIFY_TYPE:
9545 return fold_builtin_classify_type (arg0);
9547 case BUILT_IN_STRLEN:
9548 return fold_builtin_strlen (loc, type, arg0);
9550 CASE_FLT_FN (BUILT_IN_FABS):
9551 return fold_builtin_fabs (loc, arg0, type);
9555 case BUILT_IN_LLABS:
9556 case BUILT_IN_IMAXABS:
9557 return fold_builtin_abs (loc, arg0, type);
9559 CASE_FLT_FN (BUILT_IN_CONJ):
9560 if (validate_arg (arg0, COMPLEX_TYPE)
9561 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9562 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9565 CASE_FLT_FN (BUILT_IN_CREAL):
9566 if (validate_arg (arg0, COMPLEX_TYPE)
9567 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9568 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));;
9571 CASE_FLT_FN (BUILT_IN_CIMAG):
9572 if (validate_arg (arg0, COMPLEX_TYPE)
9573 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9574 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9577 CASE_FLT_FN (BUILT_IN_CCOS):
9578 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ false);
9580 CASE_FLT_FN (BUILT_IN_CCOSH):
9581 return fold_builtin_ccos(loc, arg0, type, fndecl, /*hyper=*/ true);
9583 CASE_FLT_FN (BUILT_IN_CPROJ):
9584 return fold_builtin_cproj(loc, arg0, type);
9586 CASE_FLT_FN (BUILT_IN_CSIN):
9587 if (validate_arg (arg0, COMPLEX_TYPE)
9588 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9589 return do_mpc_arg1 (arg0, type, mpc_sin);
9592 CASE_FLT_FN (BUILT_IN_CSINH):
9593 if (validate_arg (arg0, COMPLEX_TYPE)
9594 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9595 return do_mpc_arg1 (arg0, type, mpc_sinh);
9598 CASE_FLT_FN (BUILT_IN_CTAN):
9599 if (validate_arg (arg0, COMPLEX_TYPE)
9600 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9601 return do_mpc_arg1 (arg0, type, mpc_tan);
9604 CASE_FLT_FN (BUILT_IN_CTANH):
9605 if (validate_arg (arg0, COMPLEX_TYPE)
9606 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9607 return do_mpc_arg1 (arg0, type, mpc_tanh);
9610 CASE_FLT_FN (BUILT_IN_CLOG):
9611 if (validate_arg (arg0, COMPLEX_TYPE)
9612 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9613 return do_mpc_arg1 (arg0, type, mpc_log);
9616 CASE_FLT_FN (BUILT_IN_CSQRT):
9617 if (validate_arg (arg0, COMPLEX_TYPE)
9618 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9619 return do_mpc_arg1 (arg0, type, mpc_sqrt);
9622 CASE_FLT_FN (BUILT_IN_CASIN):
9623 if (validate_arg (arg0, COMPLEX_TYPE)
9624 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9625 return do_mpc_arg1 (arg0, type, mpc_asin);
9628 CASE_FLT_FN (BUILT_IN_CACOS):
9629 if (validate_arg (arg0, COMPLEX_TYPE)
9630 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9631 return do_mpc_arg1 (arg0, type, mpc_acos);
9634 CASE_FLT_FN (BUILT_IN_CATAN):
9635 if (validate_arg (arg0, COMPLEX_TYPE)
9636 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9637 return do_mpc_arg1 (arg0, type, mpc_atan);
9640 CASE_FLT_FN (BUILT_IN_CASINH):
9641 if (validate_arg (arg0, COMPLEX_TYPE)
9642 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9643 return do_mpc_arg1 (arg0, type, mpc_asinh);
9646 CASE_FLT_FN (BUILT_IN_CACOSH):
9647 if (validate_arg (arg0, COMPLEX_TYPE)
9648 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9649 return do_mpc_arg1 (arg0, type, mpc_acosh);
9652 CASE_FLT_FN (BUILT_IN_CATANH):
9653 if (validate_arg (arg0, COMPLEX_TYPE)
9654 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9655 return do_mpc_arg1 (arg0, type, mpc_atanh);
9658 CASE_FLT_FN (BUILT_IN_CABS):
9659 return fold_builtin_cabs (loc, arg0, type, fndecl);
9661 CASE_FLT_FN (BUILT_IN_CARG):
9662 return fold_builtin_carg (loc, arg0, type);
9664 CASE_FLT_FN (BUILT_IN_SQRT):
9665 return fold_builtin_sqrt (loc, arg0, type);
9667 CASE_FLT_FN (BUILT_IN_CBRT):
9668 return fold_builtin_cbrt (loc, arg0, type);
9670 CASE_FLT_FN (BUILT_IN_ASIN):
9671 if (validate_arg (arg0, REAL_TYPE))
9672 return do_mpfr_arg1 (arg0, type, mpfr_asin,
9673 &dconstm1, &dconst1, true);
9676 CASE_FLT_FN (BUILT_IN_ACOS):
9677 if (validate_arg (arg0, REAL_TYPE))
9678 return do_mpfr_arg1 (arg0, type, mpfr_acos,
9679 &dconstm1, &dconst1, true);
9682 CASE_FLT_FN (BUILT_IN_ATAN):
9683 if (validate_arg (arg0, REAL_TYPE))
9684 return do_mpfr_arg1 (arg0, type, mpfr_atan, NULL, NULL, 0);
9687 CASE_FLT_FN (BUILT_IN_ASINH):
9688 if (validate_arg (arg0, REAL_TYPE))
9689 return do_mpfr_arg1 (arg0, type, mpfr_asinh, NULL, NULL, 0);
9692 CASE_FLT_FN (BUILT_IN_ACOSH):
9693 if (validate_arg (arg0, REAL_TYPE))
9694 return do_mpfr_arg1 (arg0, type, mpfr_acosh,
9695 &dconst1, NULL, true);
9698 CASE_FLT_FN (BUILT_IN_ATANH):
9699 if (validate_arg (arg0, REAL_TYPE))
9700 return do_mpfr_arg1 (arg0, type, mpfr_atanh,
9701 &dconstm1, &dconst1, false);
9704 CASE_FLT_FN (BUILT_IN_SIN):
9705 if (validate_arg (arg0, REAL_TYPE))
9706 return do_mpfr_arg1 (arg0, type, mpfr_sin, NULL, NULL, 0);
9709 CASE_FLT_FN (BUILT_IN_COS):
9710 return fold_builtin_cos (loc, arg0, type, fndecl);
9712 CASE_FLT_FN (BUILT_IN_TAN):
9713 return fold_builtin_tan (arg0, type);
9715 CASE_FLT_FN (BUILT_IN_CEXP):
9716 return fold_builtin_cexp (loc, arg0, type);
9718 CASE_FLT_FN (BUILT_IN_CEXPI):
9719 if (validate_arg (arg0, REAL_TYPE))
9720 return do_mpfr_sincos (arg0, NULL_TREE, NULL_TREE);
9723 CASE_FLT_FN (BUILT_IN_SINH):
9724 if (validate_arg (arg0, REAL_TYPE))
9725 return do_mpfr_arg1 (arg0, type, mpfr_sinh, NULL, NULL, 0);
9728 CASE_FLT_FN (BUILT_IN_COSH):
9729 return fold_builtin_cosh (loc, arg0, type, fndecl);
9731 CASE_FLT_FN (BUILT_IN_TANH):
9732 if (validate_arg (arg0, REAL_TYPE))
9733 return do_mpfr_arg1 (arg0, type, mpfr_tanh, NULL, NULL, 0);
9736 CASE_FLT_FN (BUILT_IN_ERF):
9737 if (validate_arg (arg0, REAL_TYPE))
9738 return do_mpfr_arg1 (arg0, type, mpfr_erf, NULL, NULL, 0);
9741 CASE_FLT_FN (BUILT_IN_ERFC):
9742 if (validate_arg (arg0, REAL_TYPE))
9743 return do_mpfr_arg1 (arg0, type, mpfr_erfc, NULL, NULL, 0);
9746 CASE_FLT_FN (BUILT_IN_TGAMMA):
9747 if (validate_arg (arg0, REAL_TYPE))
9748 return do_mpfr_arg1 (arg0, type, mpfr_gamma, NULL, NULL, 0);
9751 CASE_FLT_FN (BUILT_IN_EXP):
9752 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp);
9754 CASE_FLT_FN (BUILT_IN_EXP2):
9755 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp2);
9757 CASE_FLT_FN (BUILT_IN_EXP10):
9758 CASE_FLT_FN (BUILT_IN_POW10):
9759 return fold_builtin_exponent (loc, fndecl, arg0, mpfr_exp10);
9761 CASE_FLT_FN (BUILT_IN_EXPM1):
9762 if (validate_arg (arg0, REAL_TYPE))
9763 return do_mpfr_arg1 (arg0, type, mpfr_expm1, NULL, NULL, 0);
9766 CASE_FLT_FN (BUILT_IN_LOG):
9767 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log);
9769 CASE_FLT_FN (BUILT_IN_LOG2):
9770 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log2);
9772 CASE_FLT_FN (BUILT_IN_LOG10):
9773 return fold_builtin_logarithm (loc, fndecl, arg0, mpfr_log10);
9775 CASE_FLT_FN (BUILT_IN_LOG1P):
9776 if (validate_arg (arg0, REAL_TYPE))
9777 return do_mpfr_arg1 (arg0, type, mpfr_log1p,
9778 &dconstm1, NULL, false);
9781 CASE_FLT_FN (BUILT_IN_J0):
9782 if (validate_arg (arg0, REAL_TYPE))
9783 return do_mpfr_arg1 (arg0, type, mpfr_j0,
9787 CASE_FLT_FN (BUILT_IN_J1):
9788 if (validate_arg (arg0, REAL_TYPE))
9789 return do_mpfr_arg1 (arg0, type, mpfr_j1,
9793 CASE_FLT_FN (BUILT_IN_Y0):
9794 if (validate_arg (arg0, REAL_TYPE))
9795 return do_mpfr_arg1 (arg0, type, mpfr_y0,
9796 &dconst0, NULL, false);
9799 CASE_FLT_FN (BUILT_IN_Y1):
9800 if (validate_arg (arg0, REAL_TYPE))
9801 return do_mpfr_arg1 (arg0, type, mpfr_y1,
9802 &dconst0, NULL, false);
9805 CASE_FLT_FN (BUILT_IN_NAN):
9806 case BUILT_IN_NAND32:
9807 case BUILT_IN_NAND64:
9808 case BUILT_IN_NAND128:
9809 return fold_builtin_nan (arg0, type, true);
9811 CASE_FLT_FN (BUILT_IN_NANS):
9812 return fold_builtin_nan (arg0, type, false);
9814 CASE_FLT_FN (BUILT_IN_FLOOR):
9815 return fold_builtin_floor (loc, fndecl, arg0);
9817 CASE_FLT_FN (BUILT_IN_CEIL):
9818 return fold_builtin_ceil (loc, fndecl, arg0);
9820 CASE_FLT_FN (BUILT_IN_TRUNC):
9821 return fold_builtin_trunc (loc, fndecl, arg0);
9823 CASE_FLT_FN (BUILT_IN_ROUND):
9824 return fold_builtin_round (loc, fndecl, arg0);
9826 CASE_FLT_FN (BUILT_IN_NEARBYINT):
9827 CASE_FLT_FN (BUILT_IN_RINT):
9828 return fold_trunc_transparent_mathfn (loc, fndecl, arg0);
9830 CASE_FLT_FN (BUILT_IN_ICEIL):
9831 CASE_FLT_FN (BUILT_IN_LCEIL):
9832 CASE_FLT_FN (BUILT_IN_LLCEIL):
9833 CASE_FLT_FN (BUILT_IN_LFLOOR):
9834 CASE_FLT_FN (BUILT_IN_IFLOOR):
9835 CASE_FLT_FN (BUILT_IN_LLFLOOR):
9836 CASE_FLT_FN (BUILT_IN_IROUND):
9837 CASE_FLT_FN (BUILT_IN_LROUND):
9838 CASE_FLT_FN (BUILT_IN_LLROUND):
9839 return fold_builtin_int_roundingfn (loc, fndecl, arg0);
9841 CASE_FLT_FN (BUILT_IN_IRINT):
9842 CASE_FLT_FN (BUILT_IN_LRINT):
9843 CASE_FLT_FN (BUILT_IN_LLRINT):
9844 return fold_fixed_mathfn (loc, fndecl, arg0);
9846 case BUILT_IN_BSWAP32:
9847 case BUILT_IN_BSWAP64:
9848 return fold_builtin_bswap (fndecl, arg0);
9850 CASE_INT_FN (BUILT_IN_FFS):
9851 CASE_INT_FN (BUILT_IN_CLZ):
9852 CASE_INT_FN (BUILT_IN_CTZ):
9853 CASE_INT_FN (BUILT_IN_CLRSB):
9854 CASE_INT_FN (BUILT_IN_POPCOUNT):
9855 CASE_INT_FN (BUILT_IN_PARITY):
9856 return fold_builtin_bitop (fndecl, arg0);
9858 CASE_FLT_FN (BUILT_IN_SIGNBIT):
9859 return fold_builtin_signbit (loc, arg0, type);
9861 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
9862 return fold_builtin_significand (loc, arg0, type);
9864 CASE_FLT_FN (BUILT_IN_ILOGB):
9865 CASE_FLT_FN (BUILT_IN_LOGB):
9866 return fold_builtin_logb (loc, arg0, type);
9868 case BUILT_IN_ISASCII:
9869 return fold_builtin_isascii (loc, arg0);
9871 case BUILT_IN_TOASCII:
9872 return fold_builtin_toascii (loc, arg0);
9874 case BUILT_IN_ISDIGIT:
9875 return fold_builtin_isdigit (loc, arg0);
9877 CASE_FLT_FN (BUILT_IN_FINITE):
9878 case BUILT_IN_FINITED32:
9879 case BUILT_IN_FINITED64:
9880 case BUILT_IN_FINITED128:
9881 case BUILT_IN_ISFINITE:
9883 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
9886 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9889 CASE_FLT_FN (BUILT_IN_ISINF):
9890 case BUILT_IN_ISINFD32:
9891 case BUILT_IN_ISINFD64:
9892 case BUILT_IN_ISINFD128:
9894 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
9897 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9900 case BUILT_IN_ISNORMAL:
9901 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9903 case BUILT_IN_ISINF_SIGN:
9904 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
9906 CASE_FLT_FN (BUILT_IN_ISNAN):
9907 case BUILT_IN_ISNAND32:
9908 case BUILT_IN_ISNAND64:
9909 case BUILT_IN_ISNAND128:
9910 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
9912 case BUILT_IN_PRINTF:
9913 case BUILT_IN_PRINTF_UNLOCKED:
9914 case BUILT_IN_VPRINTF:
9915 return fold_builtin_printf (loc, fndecl, arg0, NULL_TREE, ignore, fcode);
9918 if (integer_zerop (arg0))
9919 return build_empty_stmt (loc);
9930 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
9931 IGNORE is true if the result of the function call is ignored. This
9932 function returns NULL_TREE if no simplification was possible. */
9935 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1, bool ignore)
9937 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9938 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9942 CASE_FLT_FN (BUILT_IN_JN):
9943 if (validate_arg (arg0, INTEGER_TYPE)
9944 && validate_arg (arg1, REAL_TYPE))
9945 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_jn, NULL, 0);
9948 CASE_FLT_FN (BUILT_IN_YN):
9949 if (validate_arg (arg0, INTEGER_TYPE)
9950 && validate_arg (arg1, REAL_TYPE))
9951 return do_mpfr_bessel_n (arg0, arg1, type, mpfr_yn,
9955 CASE_FLT_FN (BUILT_IN_DREM):
9956 CASE_FLT_FN (BUILT_IN_REMAINDER):
9957 if (validate_arg (arg0, REAL_TYPE)
9958 && validate_arg(arg1, REAL_TYPE))
9959 return do_mpfr_arg2 (arg0, arg1, type, mpfr_remainder);
9962 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
9963 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
9964 if (validate_arg (arg0, REAL_TYPE)
9965 && validate_arg(arg1, POINTER_TYPE))
9966 return do_mpfr_lgamma_r (arg0, arg1, type);
9969 CASE_FLT_FN (BUILT_IN_ATAN2):
9970 if (validate_arg (arg0, REAL_TYPE)
9971 && validate_arg(arg1, REAL_TYPE))
9972 return do_mpfr_arg2 (arg0, arg1, type, mpfr_atan2);
9975 CASE_FLT_FN (BUILT_IN_FDIM):
9976 if (validate_arg (arg0, REAL_TYPE)
9977 && validate_arg(arg1, REAL_TYPE))
9978 return do_mpfr_arg2 (arg0, arg1, type, mpfr_dim);
9981 CASE_FLT_FN (BUILT_IN_HYPOT):
9982 return fold_builtin_hypot (loc, fndecl, arg0, arg1, type);
9984 CASE_FLT_FN (BUILT_IN_CPOW):
9985 if (validate_arg (arg0, COMPLEX_TYPE)
9986 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
9987 && validate_arg (arg1, COMPLEX_TYPE)
9988 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE)
9989 return do_mpc_arg2 (arg0, arg1, type, /*do_nonfinite=*/ 0, mpc_pow);
9992 CASE_FLT_FN (BUILT_IN_LDEXP):
9993 return fold_builtin_load_exponent (loc, arg0, arg1, type, /*ldexp=*/true);
9994 CASE_FLT_FN (BUILT_IN_SCALBN):
9995 CASE_FLT_FN (BUILT_IN_SCALBLN):
9996 return fold_builtin_load_exponent (loc, arg0, arg1,
9997 type, /*ldexp=*/false);
9999 CASE_FLT_FN (BUILT_IN_FREXP):
10000 return fold_builtin_frexp (loc, arg0, arg1, type);
10002 CASE_FLT_FN (BUILT_IN_MODF):
10003 return fold_builtin_modf (loc, arg0, arg1, type);
10005 case BUILT_IN_BZERO:
10006 return fold_builtin_bzero (loc, arg0, arg1, ignore);
10008 case BUILT_IN_FPUTS:
10009 return fold_builtin_fputs (loc, arg0, arg1, ignore, false, NULL_TREE);
10011 case BUILT_IN_FPUTS_UNLOCKED:
10012 return fold_builtin_fputs (loc, arg0, arg1, ignore, true, NULL_TREE);
10014 case BUILT_IN_STRSTR:
10015 return fold_builtin_strstr (loc, arg0, arg1, type);
10017 case BUILT_IN_STRCAT:
10018 return fold_builtin_strcat (loc, arg0, arg1);
10020 case BUILT_IN_STRSPN:
10021 return fold_builtin_strspn (loc, arg0, arg1);
10023 case BUILT_IN_STRCSPN:
10024 return fold_builtin_strcspn (loc, arg0, arg1);
10026 case BUILT_IN_STRCHR:
10027 case BUILT_IN_INDEX:
10028 return fold_builtin_strchr (loc, arg0, arg1, type);
10030 case BUILT_IN_STRRCHR:
10031 case BUILT_IN_RINDEX:
10032 return fold_builtin_strrchr (loc, arg0, arg1, type);
10034 case BUILT_IN_STRCPY:
10035 return fold_builtin_strcpy (loc, fndecl, arg0, arg1, NULL_TREE);
10037 case BUILT_IN_STPCPY:
10040 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
10044 return build_call_expr_loc (loc, fn, 2, arg0, arg1);
10047 return fold_builtin_stpcpy (loc, fndecl, arg0, arg1);
10050 case BUILT_IN_STRCMP:
10051 return fold_builtin_strcmp (loc, arg0, arg1);
10053 case BUILT_IN_STRPBRK:
10054 return fold_builtin_strpbrk (loc, arg0, arg1, type);
10056 case BUILT_IN_EXPECT:
10057 return fold_builtin_expect (loc, arg0, arg1);
10059 CASE_FLT_FN (BUILT_IN_POW):
10060 return fold_builtin_pow (loc, fndecl, arg0, arg1, type);
10062 CASE_FLT_FN (BUILT_IN_POWI):
10063 return fold_builtin_powi (loc, fndecl, arg0, arg1, type);
10065 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10066 return fold_builtin_copysign (loc, fndecl, arg0, arg1, type);
10068 CASE_FLT_FN (BUILT_IN_FMIN):
10069 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/false);
10071 CASE_FLT_FN (BUILT_IN_FMAX):
10072 return fold_builtin_fmin_fmax (loc, arg0, arg1, type, /*max=*/true);
10074 case BUILT_IN_ISGREATER:
10075 return fold_builtin_unordered_cmp (loc, fndecl,
10076 arg0, arg1, UNLE_EXPR, LE_EXPR);
10077 case BUILT_IN_ISGREATEREQUAL:
10078 return fold_builtin_unordered_cmp (loc, fndecl,
10079 arg0, arg1, UNLT_EXPR, LT_EXPR);
10080 case BUILT_IN_ISLESS:
10081 return fold_builtin_unordered_cmp (loc, fndecl,
10082 arg0, arg1, UNGE_EXPR, GE_EXPR);
10083 case BUILT_IN_ISLESSEQUAL:
10084 return fold_builtin_unordered_cmp (loc, fndecl,
10085 arg0, arg1, UNGT_EXPR, GT_EXPR);
10086 case BUILT_IN_ISLESSGREATER:
10087 return fold_builtin_unordered_cmp (loc, fndecl,
10088 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
10089 case BUILT_IN_ISUNORDERED:
10090 return fold_builtin_unordered_cmp (loc, fndecl,
10091 arg0, arg1, UNORDERED_EXPR,
10094 /* We do the folding for va_start in the expander. */
10095 case BUILT_IN_VA_START:
10098 case BUILT_IN_SPRINTF:
10099 return fold_builtin_sprintf (loc, arg0, arg1, NULL_TREE, ignore);
10101 case BUILT_IN_OBJECT_SIZE:
10102 return fold_builtin_object_size (arg0, arg1);
10104 case BUILT_IN_PRINTF:
10105 case BUILT_IN_PRINTF_UNLOCKED:
10106 case BUILT_IN_VPRINTF:
10107 return fold_builtin_printf (loc, fndecl, arg0, arg1, ignore, fcode);
10109 case BUILT_IN_PRINTF_CHK:
10110 case BUILT_IN_VPRINTF_CHK:
10111 if (!validate_arg (arg0, INTEGER_TYPE)
10112 || TREE_SIDE_EFFECTS (arg0))
10115 return fold_builtin_printf (loc, fndecl,
10116 arg1, NULL_TREE, ignore, fcode);
10119 case BUILT_IN_FPRINTF:
10120 case BUILT_IN_FPRINTF_UNLOCKED:
10121 case BUILT_IN_VFPRINTF:
10122 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, NULL_TREE,
10131 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10132 and ARG2. IGNORE is true if the result of the function call is ignored.
10133 This function returns NULL_TREE if no simplification was possible. */
10136 fold_builtin_3 (location_t loc, tree fndecl,
10137 tree arg0, tree arg1, tree arg2, bool ignore)
10139 tree type = TREE_TYPE (TREE_TYPE (fndecl));
10140 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10144 CASE_FLT_FN (BUILT_IN_SINCOS):
10145 return fold_builtin_sincos (loc, arg0, arg1, arg2);
10147 CASE_FLT_FN (BUILT_IN_FMA):
10148 return fold_builtin_fma (loc, arg0, arg1, arg2, type);
10151 CASE_FLT_FN (BUILT_IN_REMQUO):
10152 if (validate_arg (arg0, REAL_TYPE)
10153 && validate_arg(arg1, REAL_TYPE)
10154 && validate_arg(arg2, POINTER_TYPE))
10155 return do_mpfr_remquo (arg0, arg1, arg2);
10158 case BUILT_IN_MEMSET:
10159 return fold_builtin_memset (loc, arg0, arg1, arg2, type, ignore);
10161 case BUILT_IN_BCOPY:
10162 return fold_builtin_memory_op (loc, arg1, arg0, arg2,
10163 void_type_node, true, /*endp=*/3);
10165 case BUILT_IN_MEMCPY:
10166 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10167 type, ignore, /*endp=*/0);
10169 case BUILT_IN_MEMPCPY:
10170 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10171 type, ignore, /*endp=*/1);
10173 case BUILT_IN_MEMMOVE:
10174 return fold_builtin_memory_op (loc, arg0, arg1, arg2,
10175 type, ignore, /*endp=*/3);
10177 case BUILT_IN_STRNCAT:
10178 return fold_builtin_strncat (loc, arg0, arg1, arg2);
10180 case BUILT_IN_STRNCPY:
10181 return fold_builtin_strncpy (loc, fndecl, arg0, arg1, arg2, NULL_TREE);
10183 case BUILT_IN_STRNCMP:
10184 return fold_builtin_strncmp (loc, arg0, arg1, arg2);
10186 case BUILT_IN_MEMCHR:
10187 return fold_builtin_memchr (loc, arg0, arg1, arg2, type);
10189 case BUILT_IN_BCMP:
10190 case BUILT_IN_MEMCMP:
10191 return fold_builtin_memcmp (loc, arg0, arg1, arg2);;
10193 case BUILT_IN_SPRINTF:
10194 return fold_builtin_sprintf (loc, arg0, arg1, arg2, ignore);
10196 case BUILT_IN_SNPRINTF:
10197 return fold_builtin_snprintf (loc, arg0, arg1, arg2, NULL_TREE, ignore);
10199 case BUILT_IN_STRCPY_CHK:
10200 case BUILT_IN_STPCPY_CHK:
10201 return fold_builtin_stxcpy_chk (loc, fndecl, arg0, arg1, arg2, NULL_TREE,
10204 case BUILT_IN_STRCAT_CHK:
10205 return fold_builtin_strcat_chk (loc, fndecl, arg0, arg1, arg2);
10207 case BUILT_IN_PRINTF_CHK:
10208 case BUILT_IN_VPRINTF_CHK:
10209 if (!validate_arg (arg0, INTEGER_TYPE)
10210 || TREE_SIDE_EFFECTS (arg0))
10213 return fold_builtin_printf (loc, fndecl, arg1, arg2, ignore, fcode);
10216 case BUILT_IN_FPRINTF:
10217 case BUILT_IN_FPRINTF_UNLOCKED:
10218 case BUILT_IN_VFPRINTF:
10219 return fold_builtin_fprintf (loc, fndecl, arg0, arg1, arg2,
10222 case BUILT_IN_FPRINTF_CHK:
10223 case BUILT_IN_VFPRINTF_CHK:
10224 if (!validate_arg (arg1, INTEGER_TYPE)
10225 || TREE_SIDE_EFFECTS (arg1))
10228 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, NULL_TREE,
10237 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10238 ARG2, and ARG3. IGNORE is true if the result of the function call is
10239 ignored. This function returns NULL_TREE if no simplification was
10243 fold_builtin_4 (location_t loc, tree fndecl,
10244 tree arg0, tree arg1, tree arg2, tree arg3, bool ignore)
10246 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10250 case BUILT_IN_MEMCPY_CHK:
10251 case BUILT_IN_MEMPCPY_CHK:
10252 case BUILT_IN_MEMMOVE_CHK:
10253 case BUILT_IN_MEMSET_CHK:
10254 return fold_builtin_memory_chk (loc, fndecl, arg0, arg1, arg2, arg3,
10256 DECL_FUNCTION_CODE (fndecl));
10258 case BUILT_IN_STRNCPY_CHK:
10259 return fold_builtin_strncpy_chk (loc, arg0, arg1, arg2, arg3, NULL_TREE);
10261 case BUILT_IN_STRNCAT_CHK:
10262 return fold_builtin_strncat_chk (loc, fndecl, arg0, arg1, arg2, arg3);
10264 case BUILT_IN_SNPRINTF:
10265 return fold_builtin_snprintf (loc, arg0, arg1, arg2, arg3, ignore);
10267 case BUILT_IN_FPRINTF_CHK:
10268 case BUILT_IN_VFPRINTF_CHK:
10269 if (!validate_arg (arg1, INTEGER_TYPE)
10270 || TREE_SIDE_EFFECTS (arg1))
10273 return fold_builtin_fprintf (loc, fndecl, arg0, arg2, arg3,
10283 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10284 arguments, where NARGS <= 4. IGNORE is true if the result of the
10285 function call is ignored. This function returns NULL_TREE if no
10286 simplification was possible. Note that this only folds builtins with
10287 fixed argument patterns. Foldings that do varargs-to-varargs
10288 transformations, or that match calls with more than 4 arguments,
10289 need to be handled with fold_builtin_varargs instead. */
10291 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10294 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool ignore)
10296 tree ret = NULL_TREE;
10301 ret = fold_builtin_0 (loc, fndecl, ignore);
10304 ret = fold_builtin_1 (loc, fndecl, args[0], ignore);
10307 ret = fold_builtin_2 (loc, fndecl, args[0], args[1], ignore);
10310 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2], ignore);
10313 ret = fold_builtin_4 (loc, fndecl, args[0], args[1], args[2], args[3],
10321 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10322 SET_EXPR_LOCATION (ret, loc);
10323 TREE_NO_WARNING (ret) = 1;
10329 /* Builtins with folding operations that operate on "..." arguments
10330 need special handling; we need to store the arguments in a convenient
10331 data structure before attempting any folding. Fortunately there are
10332 only a few builtins that fall into this category. FNDECL is the
10333 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
10334 result of the function call is ignored. */
10337 fold_builtin_varargs (location_t loc, tree fndecl, tree exp,
10338 bool ignore ATTRIBUTE_UNUSED)
10340 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10341 tree ret = NULL_TREE;
10345 case BUILT_IN_SPRINTF_CHK:
10346 case BUILT_IN_VSPRINTF_CHK:
10347 ret = fold_builtin_sprintf_chk (loc, exp, fcode);
10350 case BUILT_IN_SNPRINTF_CHK:
10351 case BUILT_IN_VSNPRINTF_CHK:
10352 ret = fold_builtin_snprintf_chk (loc, exp, NULL_TREE, fcode);
10355 case BUILT_IN_FPCLASSIFY:
10356 ret = fold_builtin_fpclassify (loc, exp);
10364 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10365 SET_EXPR_LOCATION (ret, loc);
10366 TREE_NO_WARNING (ret) = 1;
10372 /* Return true if FNDECL shouldn't be folded right now.
10373 If a built-in function has an inline attribute always_inline
10374 wrapper, defer folding it after always_inline functions have
10375 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10376 might not be performed. */
10379 avoid_folding_inline_builtin (tree fndecl)
10381 return (DECL_DECLARED_INLINE_P (fndecl)
10382 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10384 && !cfun->always_inline_functions_inlined
10385 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10388 /* A wrapper function for builtin folding that prevents warnings for
10389 "statement without effect" and the like, caused by removing the
10390 call node earlier than the warning is generated. */
10393 fold_call_expr (location_t loc, tree exp, bool ignore)
10395 tree ret = NULL_TREE;
10396 tree fndecl = get_callee_fndecl (exp);
10398 && TREE_CODE (fndecl) == FUNCTION_DECL
10399 && DECL_BUILT_IN (fndecl)
10400 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10401 yet. Defer folding until we see all the arguments
10402 (after inlining). */
10403 && !CALL_EXPR_VA_ARG_PACK (exp))
10405 int nargs = call_expr_nargs (exp);
10407 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10408 instead last argument is __builtin_va_arg_pack (). Defer folding
10409 even in that case, until arguments are finalized. */
10410 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10412 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10414 && TREE_CODE (fndecl2) == FUNCTION_DECL
10415 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10416 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10420 if (avoid_folding_inline_builtin (fndecl))
10423 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10424 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10425 CALL_EXPR_ARGP (exp), ignore);
10428 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
10430 tree *args = CALL_EXPR_ARGP (exp);
10431 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10434 ret = fold_builtin_varargs (loc, fndecl, exp, ignore);
10442 /* Conveniently construct a function call expression. FNDECL names the
10443 function to be called and N arguments are passed in the array
10447 build_call_expr_loc_array (location_t loc, tree fndecl, int n, tree *argarray)
10449 tree fntype = TREE_TYPE (fndecl);
10450 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
10452 return fold_builtin_call_array (loc, TREE_TYPE (fntype), fn, n, argarray);
10455 /* Conveniently construct a function call expression. FNDECL names the
10456 function to be called and the arguments are passed in the vector
10460 build_call_expr_loc_vec (location_t loc, tree fndecl, VEC(tree,gc) *vec)
10462 return build_call_expr_loc_array (loc, fndecl, VEC_length (tree, vec),
10463 VEC_address (tree, vec));
10467 /* Conveniently construct a function call expression. FNDECL names the
10468 function to be called, N is the number of arguments, and the "..."
10469 parameters are the argument expressions. */
10472 build_call_expr_loc (location_t loc, tree fndecl, int n, ...)
10475 tree *argarray = XALLOCAVEC (tree, n);
10479 for (i = 0; i < n; i++)
10480 argarray[i] = va_arg (ap, tree);
10482 return build_call_expr_loc_array (loc, fndecl, n, argarray);
10485 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
10486 varargs macros aren't supported by all bootstrap compilers. */
10489 build_call_expr (tree fndecl, int n, ...)
10492 tree *argarray = XALLOCAVEC (tree, n);
10496 for (i = 0; i < n; i++)
10497 argarray[i] = va_arg (ap, tree);
10499 return build_call_expr_loc_array (UNKNOWN_LOCATION, fndecl, n, argarray);
10502 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10503 N arguments are passed in the array ARGARRAY. */
10506 fold_builtin_call_array (location_t loc, tree type,
10511 tree ret = NULL_TREE;
10514 if (TREE_CODE (fn) == ADDR_EXPR)
10516 tree fndecl = TREE_OPERAND (fn, 0);
10517 if (TREE_CODE (fndecl) == FUNCTION_DECL
10518 && DECL_BUILT_IN (fndecl))
10520 /* If last argument is __builtin_va_arg_pack (), arguments to this
10521 function are not finalized yet. Defer folding until they are. */
10522 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10524 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10526 && TREE_CODE (fndecl2) == FUNCTION_DECL
10527 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
10528 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
10529 return build_call_array_loc (loc, type, fn, n, argarray);
10531 if (avoid_folding_inline_builtin (fndecl))
10532 return build_call_array_loc (loc, type, fn, n, argarray);
10533 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10535 ret = targetm.fold_builtin (fndecl, n, argarray, false);
10539 return build_call_array_loc (loc, type, fn, n, argarray);
10541 else if (n <= MAX_ARGS_TO_FOLD_BUILTIN)
10543 /* First try the transformations that don't require consing up
10545 ret = fold_builtin_n (loc, fndecl, argarray, n, false);
10550 /* If we got this far, we need to build an exp. */
10551 exp = build_call_array_loc (loc, type, fn, n, argarray);
10552 ret = fold_builtin_varargs (loc, fndecl, exp, false);
10553 return ret ? ret : exp;
10557 return build_call_array_loc (loc, type, fn, n, argarray);
10560 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10561 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10562 of arguments in ARGS to be omitted. OLDNARGS is the number of
10563 elements in ARGS. */
10566 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10567 int skip, tree fndecl, int n, va_list newargs)
10569 int nargs = oldnargs - skip + n;
10576 buffer = XALLOCAVEC (tree, nargs);
10577 for (i = 0; i < n; i++)
10578 buffer[i] = va_arg (newargs, tree);
10579 for (j = skip; j < oldnargs; j++, i++)
10580 buffer[i] = args[j];
10583 buffer = args + skip;
10585 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10588 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10589 list ARGS along with N new arguments specified as the "..."
10590 parameters. SKIP is the number of arguments in ARGS to be omitted.
10591 OLDNARGS is the number of elements in ARGS. */
10594 rewrite_call_expr_array (location_t loc, int oldnargs, tree *args,
10595 int skip, tree fndecl, int n, ...)
10601 t = rewrite_call_expr_valist (loc, oldnargs, args, skip, fndecl, n, ap);
10607 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10608 along with N new arguments specified as the "..." parameters. SKIP
10609 is the number of arguments in EXP to be omitted. This function is used
10610 to do varargs-to-varargs transformations. */
10613 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10619 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10620 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10626 /* Validate a single argument ARG against a tree code CODE representing
10630 validate_arg (const_tree arg, enum tree_code code)
10634 else if (code == POINTER_TYPE)
10635 return POINTER_TYPE_P (TREE_TYPE (arg));
10636 else if (code == INTEGER_TYPE)
10637 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10638 return code == TREE_CODE (TREE_TYPE (arg));
10641 /* This function validates the types of a function call argument list
10642 against a specified list of tree_codes. If the last specifier is a 0,
10643 that represents an ellipses, otherwise the last specifier must be a
10646 This is the GIMPLE version of validate_arglist. Eventually we want to
10647 completely convert builtins.c to work from GIMPLEs and the tree based
10648 validate_arglist will then be removed. */
10651 validate_gimple_arglist (const_gimple call, ...)
10653 enum tree_code code;
10659 va_start (ap, call);
10664 code = (enum tree_code) va_arg (ap, int);
10668 /* This signifies an ellipses, any further arguments are all ok. */
10672 /* This signifies an endlink, if no arguments remain, return
10673 true, otherwise return false. */
10674 res = (i == gimple_call_num_args (call));
10677 /* If no parameters remain or the parameter's code does not
10678 match the specified code, return false. Otherwise continue
10679 checking any remaining arguments. */
10680 arg = gimple_call_arg (call, i++);
10681 if (!validate_arg (arg, code))
10688 /* We need gotos here since we can only have one VA_CLOSE in a
10696 /* This function validates the types of a function call argument list
10697 against a specified list of tree_codes. If the last specifier is a 0,
10698 that represents an ellipses, otherwise the last specifier must be a
10702 validate_arglist (const_tree callexpr, ...)
10704 enum tree_code code;
10707 const_call_expr_arg_iterator iter;
10710 va_start (ap, callexpr);
10711 init_const_call_expr_arg_iterator (callexpr, &iter);
10715 code = (enum tree_code) va_arg (ap, int);
10719 /* This signifies an ellipses, any further arguments are all ok. */
10723 /* This signifies an endlink, if no arguments remain, return
10724 true, otherwise return false. */
10725 res = !more_const_call_expr_args_p (&iter);
10728 /* If no parameters remain or the parameter's code does not
10729 match the specified code, return false. Otherwise continue
10730 checking any remaining arguments. */
10731 arg = next_const_call_expr_arg (&iter);
10732 if (!validate_arg (arg, code))
10739 /* We need gotos here since we can only have one VA_CLOSE in a
10747 /* Default target-specific builtin expander that does nothing. */
10750 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10751 rtx target ATTRIBUTE_UNUSED,
10752 rtx subtarget ATTRIBUTE_UNUSED,
10753 enum machine_mode mode ATTRIBUTE_UNUSED,
10754 int ignore ATTRIBUTE_UNUSED)
10759 /* Returns true is EXP represents data that would potentially reside
10760 in a readonly section. */
10763 readonly_data_expr (tree exp)
10767 if (TREE_CODE (exp) != ADDR_EXPR)
10770 exp = get_base_address (TREE_OPERAND (exp, 0));
10774 /* Make sure we call decl_readonly_section only for trees it
10775 can handle (since it returns true for everything it doesn't
10777 if (TREE_CODE (exp) == STRING_CST
10778 || TREE_CODE (exp) == CONSTRUCTOR
10779 || (TREE_CODE (exp) == VAR_DECL && TREE_STATIC (exp)))
10780 return decl_readonly_section (exp, 0);
10785 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10786 to the call, and TYPE is its return type.
10788 Return NULL_TREE if no simplification was possible, otherwise return the
10789 simplified form of the call as a tree.
10791 The simplified form may be a constant or other expression which
10792 computes the same value, but in a more efficient manner (including
10793 calls to other builtin functions).
10795 The call may contain arguments which need to be evaluated, but
10796 which are not useful to determine the result of the call. In
10797 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10798 COMPOUND_EXPR will be an argument which must be evaluated.
10799 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10800 COMPOUND_EXPR in the chain will contain the tree for the simplified
10801 form of the builtin function call. */
10804 fold_builtin_strstr (location_t loc, tree s1, tree s2, tree type)
10806 if (!validate_arg (s1, POINTER_TYPE)
10807 || !validate_arg (s2, POINTER_TYPE))
10812 const char *p1, *p2;
10814 p2 = c_getstr (s2);
10818 p1 = c_getstr (s1);
10821 const char *r = strstr (p1, p2);
10825 return build_int_cst (TREE_TYPE (s1), 0);
10827 /* Return an offset into the constant string argument. */
10828 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10829 return fold_convert_loc (loc, type, tem);
10832 /* The argument is const char *, and the result is char *, so we need
10833 a type conversion here to avoid a warning. */
10835 return fold_convert_loc (loc, type, s1);
10840 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
10844 /* New argument list transforming strstr(s1, s2) to
10845 strchr(s1, s2[0]). */
10846 return build_call_expr_loc (loc, fn, 2, s1,
10847 build_int_cst (integer_type_node, p2[0]));
10851 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10852 the call, and TYPE is its return type.
10854 Return NULL_TREE if no simplification was possible, otherwise return the
10855 simplified form of the call as a tree.
10857 The simplified form may be a constant or other expression which
10858 computes the same value, but in a more efficient manner (including
10859 calls to other builtin functions).
10861 The call may contain arguments which need to be evaluated, but
10862 which are not useful to determine the result of the call. In
10863 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10864 COMPOUND_EXPR will be an argument which must be evaluated.
10865 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10866 COMPOUND_EXPR in the chain will contain the tree for the simplified
10867 form of the builtin function call. */
10870 fold_builtin_strchr (location_t loc, tree s1, tree s2, tree type)
10872 if (!validate_arg (s1, POINTER_TYPE)
10873 || !validate_arg (s2, INTEGER_TYPE))
10879 if (TREE_CODE (s2) != INTEGER_CST)
10882 p1 = c_getstr (s1);
10889 if (target_char_cast (s2, &c))
10892 r = strchr (p1, c);
10895 return build_int_cst (TREE_TYPE (s1), 0);
10897 /* Return an offset into the constant string argument. */
10898 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10899 return fold_convert_loc (loc, type, tem);
10905 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10906 the call, and TYPE is its return type.
10908 Return NULL_TREE if no simplification was possible, otherwise return the
10909 simplified form of the call as a tree.
10911 The simplified form may be a constant or other expression which
10912 computes the same value, but in a more efficient manner (including
10913 calls to other builtin functions).
10915 The call may contain arguments which need to be evaluated, but
10916 which are not useful to determine the result of the call. In
10917 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10918 COMPOUND_EXPR will be an argument which must be evaluated.
10919 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10920 COMPOUND_EXPR in the chain will contain the tree for the simplified
10921 form of the builtin function call. */
10924 fold_builtin_strrchr (location_t loc, tree s1, tree s2, tree type)
10926 if (!validate_arg (s1, POINTER_TYPE)
10927 || !validate_arg (s2, INTEGER_TYPE))
10934 if (TREE_CODE (s2) != INTEGER_CST)
10937 p1 = c_getstr (s1);
10944 if (target_char_cast (s2, &c))
10947 r = strrchr (p1, c);
10950 return build_int_cst (TREE_TYPE (s1), 0);
10952 /* Return an offset into the constant string argument. */
10953 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10954 return fold_convert_loc (loc, type, tem);
10957 if (! integer_zerop (s2))
10960 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
10964 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
10965 return build_call_expr_loc (loc, fn, 2, s1, s2);
10969 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
10970 to the call, and TYPE is its return type.
10972 Return NULL_TREE if no simplification was possible, otherwise return the
10973 simplified form of the call as a tree.
10975 The simplified form may be a constant or other expression which
10976 computes the same value, but in a more efficient manner (including
10977 calls to other builtin functions).
10979 The call may contain arguments which need to be evaluated, but
10980 which are not useful to determine the result of the call. In
10981 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10982 COMPOUND_EXPR will be an argument which must be evaluated.
10983 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10984 COMPOUND_EXPR in the chain will contain the tree for the simplified
10985 form of the builtin function call. */
10988 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
10990 if (!validate_arg (s1, POINTER_TYPE)
10991 || !validate_arg (s2, POINTER_TYPE))
10996 const char *p1, *p2;
10998 p2 = c_getstr (s2);
11002 p1 = c_getstr (s1);
11005 const char *r = strpbrk (p1, p2);
11009 return build_int_cst (TREE_TYPE (s1), 0);
11011 /* Return an offset into the constant string argument. */
11012 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
11013 return fold_convert_loc (loc, type, tem);
11017 /* strpbrk(x, "") == NULL.
11018 Evaluate and ignore s1 in case it had side-effects. */
11019 return omit_one_operand_loc (loc, TREE_TYPE (s1), integer_zero_node, s1);
11022 return NULL_TREE; /* Really call strpbrk. */
11024 fn = implicit_built_in_decls[BUILT_IN_STRCHR];
11028 /* New argument list transforming strpbrk(s1, s2) to
11029 strchr(s1, s2[0]). */
11030 return build_call_expr_loc (loc, fn, 2, s1,
11031 build_int_cst (integer_type_node, p2[0]));
11035 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11038 Return NULL_TREE if no simplification was possible, otherwise return the
11039 simplified form of the call as a tree.
11041 The simplified form may be a constant or other expression which
11042 computes the same value, but in a more efficient manner (including
11043 calls to other builtin functions).
11045 The call may contain arguments which need to be evaluated, but
11046 which are not useful to determine the result of the call. In
11047 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11048 COMPOUND_EXPR will be an argument which must be evaluated.
11049 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11050 COMPOUND_EXPR in the chain will contain the tree for the simplified
11051 form of the builtin function call. */
11054 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED, tree dst, tree src)
11056 if (!validate_arg (dst, POINTER_TYPE)
11057 || !validate_arg (src, POINTER_TYPE))
11061 const char *p = c_getstr (src);
11063 /* If the string length is zero, return the dst parameter. */
11064 if (p && *p == '\0')
11067 if (optimize_insn_for_speed_p ())
11069 /* See if we can store by pieces into (dst + strlen(dst)). */
11071 tree strlen_fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11072 tree strcpy_fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11074 if (!strlen_fn || !strcpy_fn)
11077 /* If we don't have a movstr we don't want to emit an strcpy
11078 call. We have to do that if the length of the source string
11079 isn't computable (in that case we can use memcpy probably
11080 later expanding to a sequence of mov instructions). If we
11081 have movstr instructions we can emit strcpy calls. */
11084 tree len = c_strlen (src, 1);
11085 if (! len || TREE_SIDE_EFFECTS (len))
11089 /* Stabilize the argument list. */
11090 dst = builtin_save_expr (dst);
11092 /* Create strlen (dst). */
11093 newdst = build_call_expr_loc (loc, strlen_fn, 1, dst);
11094 /* Create (dst p+ strlen (dst)). */
11096 newdst = fold_build_pointer_plus_loc (loc, dst, newdst);
11097 newdst = builtin_save_expr (newdst);
11099 call = build_call_expr_loc (loc, strcpy_fn, 2, newdst, src);
11100 return build2 (COMPOUND_EXPR, TREE_TYPE (dst), call, dst);
11106 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11107 arguments to the call.
11109 Return NULL_TREE if no simplification was possible, otherwise return the
11110 simplified form of the call as a tree.
11112 The simplified form may be a constant or other expression which
11113 computes the same value, but in a more efficient manner (including
11114 calls to other builtin functions).
11116 The call may contain arguments which need to be evaluated, but
11117 which are not useful to determine the result of the call. In
11118 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11119 COMPOUND_EXPR will be an argument which must be evaluated.
11120 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11121 COMPOUND_EXPR in the chain will contain the tree for the simplified
11122 form of the builtin function call. */
11125 fold_builtin_strncat (location_t loc, tree dst, tree src, tree len)
11127 if (!validate_arg (dst, POINTER_TYPE)
11128 || !validate_arg (src, POINTER_TYPE)
11129 || !validate_arg (len, INTEGER_TYPE))
11133 const char *p = c_getstr (src);
11135 /* If the requested length is zero, or the src parameter string
11136 length is zero, return the dst parameter. */
11137 if (integer_zerop (len) || (p && *p == '\0'))
11138 return omit_two_operands_loc (loc, TREE_TYPE (dst), dst, src, len);
11140 /* If the requested len is greater than or equal to the string
11141 length, call strcat. */
11142 if (TREE_CODE (len) == INTEGER_CST && p
11143 && compare_tree_int (len, strlen (p)) >= 0)
11145 tree fn = implicit_built_in_decls[BUILT_IN_STRCAT];
11147 /* If the replacement _DECL isn't initialized, don't do the
11152 return build_call_expr_loc (loc, fn, 2, dst, src);
11158 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11161 Return NULL_TREE if no simplification was possible, otherwise return the
11162 simplified form of the call as a tree.
11164 The simplified form may be a constant or other expression which
11165 computes the same value, but in a more efficient manner (including
11166 calls to other builtin functions).
11168 The call may contain arguments which need to be evaluated, but
11169 which are not useful to determine the result of the call. In
11170 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11171 COMPOUND_EXPR will be an argument which must be evaluated.
11172 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11173 COMPOUND_EXPR in the chain will contain the tree for the simplified
11174 form of the builtin function call. */
11177 fold_builtin_strspn (location_t loc, tree s1, tree s2)
11179 if (!validate_arg (s1, POINTER_TYPE)
11180 || !validate_arg (s2, POINTER_TYPE))
11184 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11186 /* If both arguments are constants, evaluate at compile-time. */
11189 const size_t r = strspn (p1, p2);
11190 return size_int (r);
11193 /* If either argument is "", return NULL_TREE. */
11194 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
11195 /* Evaluate and ignore both arguments in case either one has
11197 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
11203 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11206 Return NULL_TREE if no simplification was possible, otherwise return the
11207 simplified form of the call as a tree.
11209 The simplified form may be a constant or other expression which
11210 computes the same value, but in a more efficient manner (including
11211 calls to other builtin functions).
11213 The call may contain arguments which need to be evaluated, but
11214 which are not useful to determine the result of the call. In
11215 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11216 COMPOUND_EXPR will be an argument which must be evaluated.
11217 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11218 COMPOUND_EXPR in the chain will contain the tree for the simplified
11219 form of the builtin function call. */
11222 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
11224 if (!validate_arg (s1, POINTER_TYPE)
11225 || !validate_arg (s2, POINTER_TYPE))
11229 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
11231 /* If both arguments are constants, evaluate at compile-time. */
11234 const size_t r = strcspn (p1, p2);
11235 return size_int (r);
11238 /* If the first argument is "", return NULL_TREE. */
11239 if (p1 && *p1 == '\0')
11241 /* Evaluate and ignore argument s2 in case it has
11243 return omit_one_operand_loc (loc, size_type_node,
11244 size_zero_node, s2);
11247 /* If the second argument is "", return __builtin_strlen(s1). */
11248 if (p2 && *p2 == '\0')
11250 tree fn = implicit_built_in_decls[BUILT_IN_STRLEN];
11252 /* If the replacement _DECL isn't initialized, don't do the
11257 return build_call_expr_loc (loc, fn, 1, s1);
11263 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11264 to the call. IGNORE is true if the value returned
11265 by the builtin will be ignored. UNLOCKED is true is true if this
11266 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11267 the known length of the string. Return NULL_TREE if no simplification
11271 fold_builtin_fputs (location_t loc, tree arg0, tree arg1,
11272 bool ignore, bool unlocked, tree len)
11274 /* If we're using an unlocked function, assume the other unlocked
11275 functions exist explicitly. */
11276 tree const fn_fputc = unlocked ? built_in_decls[BUILT_IN_FPUTC_UNLOCKED]
11277 : implicit_built_in_decls[BUILT_IN_FPUTC];
11278 tree const fn_fwrite = unlocked ? built_in_decls[BUILT_IN_FWRITE_UNLOCKED]
11279 : implicit_built_in_decls[BUILT_IN_FWRITE];
11281 /* If the return value is used, don't do the transformation. */
11285 /* Verify the arguments in the original call. */
11286 if (!validate_arg (arg0, POINTER_TYPE)
11287 || !validate_arg (arg1, POINTER_TYPE))
11291 len = c_strlen (arg0, 0);
11293 /* Get the length of the string passed to fputs. If the length
11294 can't be determined, punt. */
11296 || TREE_CODE (len) != INTEGER_CST)
11299 switch (compare_tree_int (len, 1))
11301 case -1: /* length is 0, delete the call entirely . */
11302 return omit_one_operand_loc (loc, integer_type_node,
11303 integer_zero_node, arg1);;
11305 case 0: /* length is 1, call fputc. */
11307 const char *p = c_getstr (arg0);
11312 return build_call_expr_loc (loc, fn_fputc, 2,
11314 (integer_type_node, p[0]), arg1);
11320 case 1: /* length is greater than 1, call fwrite. */
11322 /* If optimizing for size keep fputs. */
11323 if (optimize_function_for_size_p (cfun))
11325 /* New argument list transforming fputs(string, stream) to
11326 fwrite(string, 1, len, stream). */
11328 return build_call_expr_loc (loc, fn_fwrite, 4, arg0,
11329 size_one_node, len, arg1);
11334 gcc_unreachable ();
11339 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11340 produced. False otherwise. This is done so that we don't output the error
11341 or warning twice or three times. */
11344 fold_builtin_next_arg (tree exp, bool va_start_p)
11346 tree fntype = TREE_TYPE (current_function_decl);
11347 int nargs = call_expr_nargs (exp);
11350 if (!stdarg_p (fntype))
11352 error ("%<va_start%> used in function with fixed args");
11358 if (va_start_p && (nargs != 2))
11360 error ("wrong number of arguments to function %<va_start%>");
11363 arg = CALL_EXPR_ARG (exp, 1);
11365 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11366 when we checked the arguments and if needed issued a warning. */
11371 /* Evidently an out of date version of <stdarg.h>; can't validate
11372 va_start's second argument, but can still work as intended. */
11373 warning (0, "%<__builtin_next_arg%> called without an argument");
11376 else if (nargs > 1)
11378 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11381 arg = CALL_EXPR_ARG (exp, 0);
11384 if (TREE_CODE (arg) == SSA_NAME)
11385 arg = SSA_NAME_VAR (arg);
11387 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11388 or __builtin_next_arg (0) the first time we see it, after checking
11389 the arguments and if needed issuing a warning. */
11390 if (!integer_zerop (arg))
11392 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
11394 /* Strip off all nops for the sake of the comparison. This
11395 is not quite the same as STRIP_NOPS. It does more.
11396 We must also strip off INDIRECT_EXPR for C++ reference
11398 while (CONVERT_EXPR_P (arg)
11399 || TREE_CODE (arg) == INDIRECT_REF)
11400 arg = TREE_OPERAND (arg, 0);
11401 if (arg != last_parm)
11403 /* FIXME: Sometimes with the tree optimizers we can get the
11404 not the last argument even though the user used the last
11405 argument. We just warn and set the arg to be the last
11406 argument so that we will get wrong-code because of
11408 warning (0, "second parameter of %<va_start%> not last named argument");
11411 /* Undefined by C99 7.15.1.4p4 (va_start):
11412 "If the parameter parmN is declared with the register storage
11413 class, with a function or array type, or with a type that is
11414 not compatible with the type that results after application of
11415 the default argument promotions, the behavior is undefined."
11417 else if (DECL_REGISTER (arg))
11418 warning (0, "undefined behaviour when second parameter of "
11419 "%<va_start%> is declared with %<register%> storage");
11421 /* We want to verify the second parameter just once before the tree
11422 optimizers are run and then avoid keeping it in the tree,
11423 as otherwise we could warn even for correct code like:
11424 void foo (int i, ...)
11425 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11427 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
11429 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
11435 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
11436 ORIG may be null if this is a 2-argument call. We don't attempt to
11437 simplify calls with more than 3 arguments.
11439 Return NULL_TREE if no simplification was possible, otherwise return the
11440 simplified form of the call as a tree. If IGNORED is true, it means that
11441 the caller does not use the returned value of the function. */
11444 fold_builtin_sprintf (location_t loc, tree dest, tree fmt,
11445 tree orig, int ignored)
11448 const char *fmt_str = NULL;
11450 /* Verify the required arguments in the original call. We deal with two
11451 types of sprintf() calls: 'sprintf (str, fmt)' and
11452 'sprintf (dest, "%s", orig)'. */
11453 if (!validate_arg (dest, POINTER_TYPE)
11454 || !validate_arg (fmt, POINTER_TYPE))
11456 if (orig && !validate_arg (orig, POINTER_TYPE))
11459 /* Check whether the format is a literal string constant. */
11460 fmt_str = c_getstr (fmt);
11461 if (fmt_str == NULL)
11465 retval = NULL_TREE;
11467 if (!init_target_chars ())
11470 /* If the format doesn't contain % args or %%, use strcpy. */
11471 if (strchr (fmt_str, target_percent) == NULL)
11473 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11478 /* Don't optimize sprintf (buf, "abc", ptr++). */
11482 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
11483 'format' is known to contain no % formats. */
11484 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11486 retval = build_int_cst (integer_type_node, strlen (fmt_str));
11489 /* If the format is "%s", use strcpy if the result isn't used. */
11490 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11493 fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11498 /* Don't crash on sprintf (str1, "%s"). */
11502 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
11505 retval = c_strlen (orig, 1);
11506 if (!retval || TREE_CODE (retval) != INTEGER_CST)
11509 call = build_call_expr_loc (loc, fn, 2, dest, orig);
11512 if (call && retval)
11514 retval = fold_convert_loc
11515 (loc, TREE_TYPE (TREE_TYPE (implicit_built_in_decls[BUILT_IN_SPRINTF])),
11517 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11523 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
11524 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
11525 attempt to simplify calls with more than 4 arguments.
11527 Return NULL_TREE if no simplification was possible, otherwise return the
11528 simplified form of the call as a tree. If IGNORED is true, it means that
11529 the caller does not use the returned value of the function. */
11532 fold_builtin_snprintf (location_t loc, tree dest, tree destsize, tree fmt,
11533 tree orig, int ignored)
11536 const char *fmt_str = NULL;
11537 unsigned HOST_WIDE_INT destlen;
11539 /* Verify the required arguments in the original call. We deal with two
11540 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
11541 'snprintf (dest, cst, "%s", orig)'. */
11542 if (!validate_arg (dest, POINTER_TYPE)
11543 || !validate_arg (destsize, INTEGER_TYPE)
11544 || !validate_arg (fmt, POINTER_TYPE))
11546 if (orig && !validate_arg (orig, POINTER_TYPE))
11549 if (!host_integerp (destsize, 1))
11552 /* Check whether the format is a literal string constant. */
11553 fmt_str = c_getstr (fmt);
11554 if (fmt_str == NULL)
11558 retval = NULL_TREE;
11560 if (!init_target_chars ())
11563 destlen = tree_low_cst (destsize, 1);
11565 /* If the format doesn't contain % args or %%, use strcpy. */
11566 if (strchr (fmt_str, target_percent) == NULL)
11568 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11569 size_t len = strlen (fmt_str);
11571 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
11575 /* We could expand this as
11576 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
11578 memcpy (str, fmt_with_nul_at_cstm1, cst);
11579 but in the former case that might increase code size
11580 and in the latter case grow .rodata section too much.
11581 So punt for now. */
11582 if (len >= destlen)
11588 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
11589 'format' is known to contain no % formats and
11590 strlen (fmt) < cst. */
11591 call = build_call_expr_loc (loc, fn, 2, dest, fmt);
11594 retval = build_int_cst (integer_type_node, strlen (fmt_str));
11597 /* If the format is "%s", use strcpy if the result isn't used. */
11598 else if (fmt_str && strcmp (fmt_str, target_percent_s) == 0)
11600 tree fn = implicit_built_in_decls[BUILT_IN_STRCPY];
11601 unsigned HOST_WIDE_INT origlen;
11603 /* Don't crash on snprintf (str1, cst, "%s"). */
11607 retval = c_strlen (orig, 1);
11608 if (!retval || !host_integerp (retval, 1))
11611 origlen = tree_low_cst (retval, 1);
11612 /* We could expand this as
11613 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
11615 memcpy (str1, str2_with_nul_at_cstm1, cst);
11616 but in the former case that might increase code size
11617 and in the latter case grow .rodata section too much.
11618 So punt for now. */
11619 if (origlen >= destlen)
11622 /* Convert snprintf (str1, cst, "%s", str2) into
11623 strcpy (str1, str2) if strlen (str2) < cst. */
11627 call = build_call_expr_loc (loc, fn, 2, dest, orig);
11630 retval = NULL_TREE;
11633 if (call && retval)
11635 tree fn = built_in_decls[BUILT_IN_SNPRINTF];
11636 retval = fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fn)), retval);
11637 return build2 (COMPOUND_EXPR, TREE_TYPE (retval), call, retval);
11643 /* Expand a call EXP to __builtin_object_size. */
11646 expand_builtin_object_size (tree exp)
11649 int object_size_type;
11650 tree fndecl = get_callee_fndecl (exp);
11652 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
11654 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11656 expand_builtin_trap ();
11660 ost = CALL_EXPR_ARG (exp, 1);
11663 if (TREE_CODE (ost) != INTEGER_CST
11664 || tree_int_cst_sgn (ost) < 0
11665 || compare_tree_int (ost, 3) > 0)
11667 error ("%Klast argument of %D is not integer constant between 0 and 3",
11669 expand_builtin_trap ();
11673 object_size_type = tree_low_cst (ost, 0);
11675 return object_size_type < 2 ? constm1_rtx : const0_rtx;
11678 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11679 FCODE is the BUILT_IN_* to use.
11680 Return NULL_RTX if we failed; the caller should emit a normal call,
11681 otherwise try to get the result in TARGET, if convenient (and in
11682 mode MODE if that's convenient). */
11685 expand_builtin_memory_chk (tree exp, rtx target, enum machine_mode mode,
11686 enum built_in_function fcode)
11688 tree dest, src, len, size;
11690 if (!validate_arglist (exp,
11692 fcode == BUILT_IN_MEMSET_CHK
11693 ? INTEGER_TYPE : POINTER_TYPE,
11694 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
11697 dest = CALL_EXPR_ARG (exp, 0);
11698 src = CALL_EXPR_ARG (exp, 1);
11699 len = CALL_EXPR_ARG (exp, 2);
11700 size = CALL_EXPR_ARG (exp, 3);
11702 if (! host_integerp (size, 1))
11705 if (host_integerp (len, 1) || integer_all_onesp (size))
11709 if (! integer_all_onesp (size) && tree_int_cst_lt (size, len))
11711 warning_at (tree_nonartificial_location (exp),
11712 0, "%Kcall to %D will always overflow destination buffer",
11713 exp, get_callee_fndecl (exp));
11718 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11719 mem{cpy,pcpy,move,set} is available. */
11722 case BUILT_IN_MEMCPY_CHK:
11723 fn = built_in_decls[BUILT_IN_MEMCPY];
11725 case BUILT_IN_MEMPCPY_CHK:
11726 fn = built_in_decls[BUILT_IN_MEMPCPY];
11728 case BUILT_IN_MEMMOVE_CHK:
11729 fn = built_in_decls[BUILT_IN_MEMMOVE];
11731 case BUILT_IN_MEMSET_CHK:
11732 fn = built_in_decls[BUILT_IN_MEMSET];
11741 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
11742 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11743 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11744 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11746 else if (fcode == BUILT_IN_MEMSET_CHK)
11750 unsigned int dest_align = get_pointer_alignment (dest);
11752 /* If DEST is not a pointer type, call the normal function. */
11753 if (dest_align == 0)
11756 /* If SRC and DEST are the same (and not volatile), do nothing. */
11757 if (operand_equal_p (src, dest, 0))
11761 if (fcode != BUILT_IN_MEMPCPY_CHK)
11763 /* Evaluate and ignore LEN in case it has side-effects. */
11764 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
11765 return expand_expr (dest, target, mode, EXPAND_NORMAL);
11768 expr = fold_build_pointer_plus (dest, len);
11769 return expand_expr (expr, target, mode, EXPAND_NORMAL);
11772 /* __memmove_chk special case. */
11773 if (fcode == BUILT_IN_MEMMOVE_CHK)
11775 unsigned int src_align = get_pointer_alignment (src);
11777 if (src_align == 0)
11780 /* If src is categorized for a readonly section we can use
11781 normal __memcpy_chk. */
11782 if (readonly_data_expr (src))
11784 tree fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
11787 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
11788 dest, src, len, size);
11789 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
11790 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
11791 return expand_expr (fn, target, mode, EXPAND_NORMAL);
11798 /* Emit warning if a buffer overflow is detected at compile time. */
11801 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
11805 location_t loc = tree_nonartificial_location (exp);
11809 case BUILT_IN_STRCPY_CHK:
11810 case BUILT_IN_STPCPY_CHK:
11811 /* For __strcat_chk the warning will be emitted only if overflowing
11812 by at least strlen (dest) + 1 bytes. */
11813 case BUILT_IN_STRCAT_CHK:
11814 len = CALL_EXPR_ARG (exp, 1);
11815 size = CALL_EXPR_ARG (exp, 2);
11818 case BUILT_IN_STRNCAT_CHK:
11819 case BUILT_IN_STRNCPY_CHK:
11820 len = CALL_EXPR_ARG (exp, 2);
11821 size = CALL_EXPR_ARG (exp, 3);
11823 case BUILT_IN_SNPRINTF_CHK:
11824 case BUILT_IN_VSNPRINTF_CHK:
11825 len = CALL_EXPR_ARG (exp, 1);
11826 size = CALL_EXPR_ARG (exp, 3);
11829 gcc_unreachable ();
11835 if (! host_integerp (size, 1) || integer_all_onesp (size))
11840 len = c_strlen (len, 1);
11841 if (! len || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
11844 else if (fcode == BUILT_IN_STRNCAT_CHK)
11846 tree src = CALL_EXPR_ARG (exp, 1);
11847 if (! src || ! host_integerp (len, 1) || tree_int_cst_lt (len, size))
11849 src = c_strlen (src, 1);
11850 if (! src || ! host_integerp (src, 1))
11852 warning_at (loc, 0, "%Kcall to %D might overflow destination buffer",
11853 exp, get_callee_fndecl (exp));
11856 else if (tree_int_cst_lt (src, size))
11859 else if (! host_integerp (len, 1) || ! tree_int_cst_lt (size, len))
11862 warning_at (loc, 0, "%Kcall to %D will always overflow destination buffer",
11863 exp, get_callee_fndecl (exp));
11866 /* Emit warning if a buffer overflow is detected at compile time
11867 in __sprintf_chk/__vsprintf_chk calls. */
11870 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
11872 tree size, len, fmt;
11873 const char *fmt_str;
11874 int nargs = call_expr_nargs (exp);
11876 /* Verify the required arguments in the original call. */
11880 size = CALL_EXPR_ARG (exp, 2);
11881 fmt = CALL_EXPR_ARG (exp, 3);
11883 if (! host_integerp (size, 1) || integer_all_onesp (size))
11886 /* Check whether the format is a literal string constant. */
11887 fmt_str = c_getstr (fmt);
11888 if (fmt_str == NULL)
11891 if (!init_target_chars ())
11894 /* If the format doesn't contain % args or %%, we know its size. */
11895 if (strchr (fmt_str, target_percent) == 0)
11896 len = build_int_cstu (size_type_node, strlen (fmt_str));
11897 /* If the format is "%s" and first ... argument is a string literal,
11899 else if (fcode == BUILT_IN_SPRINTF_CHK
11900 && strcmp (fmt_str, target_percent_s) == 0)
11906 arg = CALL_EXPR_ARG (exp, 4);
11907 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
11910 len = c_strlen (arg, 1);
11911 if (!len || ! host_integerp (len, 1))
11917 if (! tree_int_cst_lt (len, size))
11918 warning_at (tree_nonartificial_location (exp),
11919 0, "%Kcall to %D will always overflow destination buffer",
11920 exp, get_callee_fndecl (exp));
11923 /* Emit warning if a free is called with address of a variable. */
11926 maybe_emit_free_warning (tree exp)
11928 tree arg = CALL_EXPR_ARG (exp, 0);
11931 if (TREE_CODE (arg) != ADDR_EXPR)
11934 arg = get_base_address (TREE_OPERAND (arg, 0));
11935 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
11938 if (SSA_VAR_P (arg))
11939 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11940 "%Kattempt to free a non-heap object %qD", exp, arg);
11942 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
11943 "%Kattempt to free a non-heap object", exp);
11946 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11950 fold_builtin_object_size (tree ptr, tree ost)
11952 unsigned HOST_WIDE_INT bytes;
11953 int object_size_type;
11955 if (!validate_arg (ptr, POINTER_TYPE)
11956 || !validate_arg (ost, INTEGER_TYPE))
11961 if (TREE_CODE (ost) != INTEGER_CST
11962 || tree_int_cst_sgn (ost) < 0
11963 || compare_tree_int (ost, 3) > 0)
11966 object_size_type = tree_low_cst (ost, 0);
11968 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11969 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11970 and (size_t) 0 for types 2 and 3. */
11971 if (TREE_SIDE_EFFECTS (ptr))
11972 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
11974 if (TREE_CODE (ptr) == ADDR_EXPR)
11976 bytes = compute_builtin_object_size (ptr, object_size_type);
11977 if (double_int_fits_to_tree_p (size_type_node,
11978 uhwi_to_double_int (bytes)))
11979 return build_int_cstu (size_type_node, bytes);
11981 else if (TREE_CODE (ptr) == SSA_NAME)
11983 /* If object size is not known yet, delay folding until
11984 later. Maybe subsequent passes will help determining
11986 bytes = compute_builtin_object_size (ptr, object_size_type);
11987 if (bytes != (unsigned HOST_WIDE_INT) (object_size_type < 2 ? -1 : 0)
11988 && double_int_fits_to_tree_p (size_type_node,
11989 uhwi_to_double_int (bytes)))
11990 return build_int_cstu (size_type_node, bytes);
11996 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11997 DEST, SRC, LEN, and SIZE are the arguments to the call.
11998 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
11999 code of the builtin. If MAXLEN is not NULL, it is maximum length
12000 passed as third argument. */
12003 fold_builtin_memory_chk (location_t loc, tree fndecl,
12004 tree dest, tree src, tree len, tree size,
12005 tree maxlen, bool ignore,
12006 enum built_in_function fcode)
12010 if (!validate_arg (dest, POINTER_TYPE)
12011 || !validate_arg (src,
12012 (fcode == BUILT_IN_MEMSET_CHK
12013 ? INTEGER_TYPE : POINTER_TYPE))
12014 || !validate_arg (len, INTEGER_TYPE)
12015 || !validate_arg (size, INTEGER_TYPE))
12018 /* If SRC and DEST are the same (and not volatile), return DEST
12019 (resp. DEST+LEN for __mempcpy_chk). */
12020 if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0))
12022 if (fcode != BUILT_IN_MEMPCPY_CHK)
12023 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12027 tree temp = fold_build_pointer_plus_loc (loc, dest, len);
12028 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), temp);
12032 if (! host_integerp (size, 1))
12035 if (! integer_all_onesp (size))
12037 if (! host_integerp (len, 1))
12039 /* If LEN is not constant, try MAXLEN too.
12040 For MAXLEN only allow optimizing into non-_ocs function
12041 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12042 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12044 if (fcode == BUILT_IN_MEMPCPY_CHK && ignore)
12046 /* (void) __mempcpy_chk () can be optimized into
12047 (void) __memcpy_chk (). */
12048 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12052 return build_call_expr_loc (loc, fn, 4, dest, src, len, size);
12060 if (tree_int_cst_lt (size, maxlen))
12065 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12066 mem{cpy,pcpy,move,set} is available. */
12069 case BUILT_IN_MEMCPY_CHK:
12070 fn = built_in_decls[BUILT_IN_MEMCPY];
12072 case BUILT_IN_MEMPCPY_CHK:
12073 fn = built_in_decls[BUILT_IN_MEMPCPY];
12075 case BUILT_IN_MEMMOVE_CHK:
12076 fn = built_in_decls[BUILT_IN_MEMMOVE];
12078 case BUILT_IN_MEMSET_CHK:
12079 fn = built_in_decls[BUILT_IN_MEMSET];
12088 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12091 /* Fold a call to the __st[rp]cpy_chk builtin.
12092 DEST, SRC, and SIZE are the arguments to the call.
12093 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12094 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12095 strings passed as second argument. */
12098 fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
12099 tree src, tree size,
12100 tree maxlen, bool ignore,
12101 enum built_in_function fcode)
12105 if (!validate_arg (dest, POINTER_TYPE)
12106 || !validate_arg (src, POINTER_TYPE)
12107 || !validate_arg (size, INTEGER_TYPE))
12110 /* If SRC and DEST are the same (and not volatile), return DEST. */
12111 if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0))
12112 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest);
12114 if (! host_integerp (size, 1))
12117 if (! integer_all_onesp (size))
12119 len = c_strlen (src, 1);
12120 if (! len || ! host_integerp (len, 1))
12122 /* If LEN is not constant, try MAXLEN too.
12123 For MAXLEN only allow optimizing into non-_ocs function
12124 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12125 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12127 if (fcode == BUILT_IN_STPCPY_CHK)
12132 /* If return value of __stpcpy_chk is ignored,
12133 optimize into __strcpy_chk. */
12134 fn = built_in_decls[BUILT_IN_STRCPY_CHK];
12138 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12141 if (! len || TREE_SIDE_EFFECTS (len))
12144 /* If c_strlen returned something, but not a constant,
12145 transform __strcpy_chk into __memcpy_chk. */
12146 fn = built_in_decls[BUILT_IN_MEMCPY_CHK];
12150 len = fold_convert_loc (loc, size_type_node, len);
12151 len = size_binop_loc (loc, PLUS_EXPR, len,
12152 build_int_cst (size_type_node, 1));
12153 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
12154 build_call_expr_loc (loc, fn, 4,
12155 dest, src, len, size));
12161 if (! tree_int_cst_lt (maxlen, size))
12165 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12166 fn = built_in_decls[fcode == BUILT_IN_STPCPY_CHK
12167 ? BUILT_IN_STPCPY : BUILT_IN_STRCPY];
12171 return build_call_expr_loc (loc, fn, 2, dest, src);
12174 /* Fold a call to the __strncpy_chk builtin. DEST, SRC, LEN, and SIZE
12175 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12176 length passed as third argument. */
12179 fold_builtin_strncpy_chk (location_t loc, tree dest, tree src,
12180 tree len, tree size, tree maxlen)
12184 if (!validate_arg (dest, POINTER_TYPE)
12185 || !validate_arg (src, POINTER_TYPE)
12186 || !validate_arg (len, INTEGER_TYPE)
12187 || !validate_arg (size, INTEGER_TYPE))
12190 if (! host_integerp (size, 1))
12193 if (! integer_all_onesp (size))
12195 if (! host_integerp (len, 1))
12197 /* If LEN is not constant, try MAXLEN too.
12198 For MAXLEN only allow optimizing into non-_ocs function
12199 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12200 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12206 if (tree_int_cst_lt (size, maxlen))
12210 /* If __builtin_strncpy_chk is used, assume strncpy is available. */
12211 fn = built_in_decls[BUILT_IN_STRNCPY];
12215 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12218 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12219 are the arguments to the call. */
12222 fold_builtin_strcat_chk (location_t loc, tree fndecl, tree dest,
12223 tree src, tree size)
12228 if (!validate_arg (dest, POINTER_TYPE)
12229 || !validate_arg (src, POINTER_TYPE)
12230 || !validate_arg (size, INTEGER_TYPE))
12233 p = c_getstr (src);
12234 /* If the SRC parameter is "", return DEST. */
12235 if (p && *p == '\0')
12236 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12238 if (! host_integerp (size, 1) || ! integer_all_onesp (size))
12241 /* If __builtin_strcat_chk is used, assume strcat is available. */
12242 fn = built_in_decls[BUILT_IN_STRCAT];
12246 return build_call_expr_loc (loc, fn, 2, dest, src);
12249 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12253 fold_builtin_strncat_chk (location_t loc, tree fndecl,
12254 tree dest, tree src, tree len, tree size)
12259 if (!validate_arg (dest, POINTER_TYPE)
12260 || !validate_arg (src, POINTER_TYPE)
12261 || !validate_arg (size, INTEGER_TYPE)
12262 || !validate_arg (size, INTEGER_TYPE))
12265 p = c_getstr (src);
12266 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12267 if (p && *p == '\0')
12268 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, len);
12269 else if (integer_zerop (len))
12270 return omit_one_operand_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), dest, src);
12272 if (! host_integerp (size, 1))
12275 if (! integer_all_onesp (size))
12277 tree src_len = c_strlen (src, 1);
12279 && host_integerp (src_len, 1)
12280 && host_integerp (len, 1)
12281 && ! tree_int_cst_lt (len, src_len))
12283 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12284 fn = built_in_decls[BUILT_IN_STRCAT_CHK];
12288 return build_call_expr_loc (loc, fn, 3, dest, src, size);
12293 /* If __builtin_strncat_chk is used, assume strncat is available. */
12294 fn = built_in_decls[BUILT_IN_STRNCAT];
12298 return build_call_expr_loc (loc, fn, 3, dest, src, len);
12301 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12302 Return NULL_TREE if a normal call should be emitted rather than
12303 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
12304 or BUILT_IN_VSPRINTF_CHK. */
12307 fold_builtin_sprintf_chk_1 (location_t loc, int nargs, tree *args,
12308 enum built_in_function fcode)
12310 tree dest, size, len, fn, fmt, flag;
12311 const char *fmt_str;
12313 /* Verify the required arguments in the original call. */
12317 if (!validate_arg (dest, POINTER_TYPE))
12320 if (!validate_arg (flag, INTEGER_TYPE))
12323 if (!validate_arg (size, INTEGER_TYPE))
12326 if (!validate_arg (fmt, POINTER_TYPE))
12329 if (! host_integerp (size, 1))
12334 if (!init_target_chars ())
12337 /* Check whether the format is a literal string constant. */
12338 fmt_str = c_getstr (fmt);
12339 if (fmt_str != NULL)
12341 /* If the format doesn't contain % args or %%, we know the size. */
12342 if (strchr (fmt_str, target_percent) == 0)
12344 if (fcode != BUILT_IN_SPRINTF_CHK || nargs == 4)
12345 len = build_int_cstu (size_type_node, strlen (fmt_str));
12347 /* If the format is "%s" and first ... argument is a string literal,
12348 we know the size too. */
12349 else if (fcode == BUILT_IN_SPRINTF_CHK
12350 && strcmp (fmt_str, target_percent_s) == 0)
12357 if (validate_arg (arg, POINTER_TYPE))
12359 len = c_strlen (arg, 1);
12360 if (! len || ! host_integerp (len, 1))
12367 if (! integer_all_onesp (size))
12369 if (! len || ! tree_int_cst_lt (len, size))
12373 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
12374 or if format doesn't contain % chars or is "%s". */
12375 if (! integer_zerop (flag))
12377 if (fmt_str == NULL)
12379 if (strchr (fmt_str, target_percent) != NULL
12380 && strcmp (fmt_str, target_percent_s))
12384 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
12385 fn = built_in_decls[fcode == BUILT_IN_VSPRINTF_CHK
12386 ? BUILT_IN_VSPRINTF : BUILT_IN_SPRINTF];
12390 return rewrite_call_expr_array (loc, nargs, args, 4, fn, 2, dest, fmt);
12393 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
12394 a normal call should be emitted rather than expanding the function
12395 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
12398 fold_builtin_sprintf_chk (location_t loc, tree exp,
12399 enum built_in_function fcode)
12401 return fold_builtin_sprintf_chk_1 (loc, call_expr_nargs (exp),
12402 CALL_EXPR_ARGP (exp), fcode);
12405 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
12406 NULL_TREE if a normal call should be emitted rather than expanding
12407 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12408 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12409 passed as second argument. */
12412 fold_builtin_snprintf_chk_1 (location_t loc, int nargs, tree *args,
12413 tree maxlen, enum built_in_function fcode)
12415 tree dest, size, len, fn, fmt, flag;
12416 const char *fmt_str;
12418 /* Verify the required arguments in the original call. */
12422 if (!validate_arg (dest, POINTER_TYPE))
12425 if (!validate_arg (len, INTEGER_TYPE))
12428 if (!validate_arg (flag, INTEGER_TYPE))
12431 if (!validate_arg (size, INTEGER_TYPE))
12434 if (!validate_arg (fmt, POINTER_TYPE))
12437 if (! host_integerp (size, 1))
12440 if (! integer_all_onesp (size))
12442 if (! host_integerp (len, 1))
12444 /* If LEN is not constant, try MAXLEN too.
12445 For MAXLEN only allow optimizing into non-_ocs function
12446 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12447 if (maxlen == NULL_TREE || ! host_integerp (maxlen, 1))
12453 if (tree_int_cst_lt (size, maxlen))
12457 if (!init_target_chars ())
12460 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
12461 or if format doesn't contain % chars or is "%s". */
12462 if (! integer_zerop (flag))
12464 fmt_str = c_getstr (fmt);
12465 if (fmt_str == NULL)
12467 if (strchr (fmt_str, target_percent) != NULL
12468 && strcmp (fmt_str, target_percent_s))
12472 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
12474 fn = built_in_decls[fcode == BUILT_IN_VSNPRINTF_CHK
12475 ? BUILT_IN_VSNPRINTF : BUILT_IN_SNPRINTF];
12479 return rewrite_call_expr_array (loc, nargs, args, 5, fn, 3, dest, len, fmt);
12482 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
12483 a normal call should be emitted rather than expanding the function
12484 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
12485 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
12486 passed as second argument. */
12489 fold_builtin_snprintf_chk (location_t loc, tree exp, tree maxlen,
12490 enum built_in_function fcode)
12492 return fold_builtin_snprintf_chk_1 (loc, call_expr_nargs (exp),
12493 CALL_EXPR_ARGP (exp), maxlen, fcode);
12496 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
12497 FMT and ARG are the arguments to the call; we don't fold cases with
12498 more than 2 arguments, and ARG may be null if this is a 1-argument case.
12500 Return NULL_TREE if no simplification was possible, otherwise return the
12501 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12502 code of the function to be simplified. */
12505 fold_builtin_printf (location_t loc, tree fndecl, tree fmt,
12506 tree arg, bool ignore,
12507 enum built_in_function fcode)
12509 tree fn_putchar, fn_puts, newarg, call = NULL_TREE;
12510 const char *fmt_str = NULL;
12512 /* If the return value is used, don't do the transformation. */
12516 /* Verify the required arguments in the original call. */
12517 if (!validate_arg (fmt, POINTER_TYPE))
12520 /* Check whether the format is a literal string constant. */
12521 fmt_str = c_getstr (fmt);
12522 if (fmt_str == NULL)
12525 if (fcode == BUILT_IN_PRINTF_UNLOCKED)
12527 /* If we're using an unlocked function, assume the other
12528 unlocked functions exist explicitly. */
12529 fn_putchar = built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED];
12530 fn_puts = built_in_decls[BUILT_IN_PUTS_UNLOCKED];
12534 fn_putchar = implicit_built_in_decls[BUILT_IN_PUTCHAR];
12535 fn_puts = implicit_built_in_decls[BUILT_IN_PUTS];
12538 if (!init_target_chars ())
12541 if (strcmp (fmt_str, target_percent_s) == 0
12542 || strchr (fmt_str, target_percent) == NULL)
12546 if (strcmp (fmt_str, target_percent_s) == 0)
12548 if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12551 if (!arg || !validate_arg (arg, POINTER_TYPE))
12554 str = c_getstr (arg);
12560 /* The format specifier doesn't contain any '%' characters. */
12561 if (fcode != BUILT_IN_VPRINTF && fcode != BUILT_IN_VPRINTF_CHK
12567 /* If the string was "", printf does nothing. */
12568 if (str[0] == '\0')
12569 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12571 /* If the string has length of 1, call putchar. */
12572 if (str[1] == '\0')
12574 /* Given printf("c"), (where c is any one character,)
12575 convert "c"[0] to an int and pass that to the replacement
12577 newarg = build_int_cst (integer_type_node, str[0]);
12579 call = build_call_expr_loc (loc, fn_putchar, 1, newarg);
12583 /* If the string was "string\n", call puts("string"). */
12584 size_t len = strlen (str);
12585 if ((unsigned char)str[len - 1] == target_newline
12586 && (size_t) (int) len == len
12590 tree offset_node, string_cst;
12592 /* Create a NUL-terminated string that's one char shorter
12593 than the original, stripping off the trailing '\n'. */
12594 newarg = build_string_literal (len, str);
12595 string_cst = string_constant (newarg, &offset_node);
12596 gcc_checking_assert (string_cst
12597 && (TREE_STRING_LENGTH (string_cst)
12599 && integer_zerop (offset_node)
12601 TREE_STRING_POINTER (string_cst)[len - 1]
12602 == target_newline);
12603 /* build_string_literal creates a new STRING_CST,
12604 modify it in place to avoid double copying. */
12605 newstr = CONST_CAST (char *, TREE_STRING_POINTER (string_cst));
12606 newstr[len - 1] = '\0';
12608 call = build_call_expr_loc (loc, fn_puts, 1, newarg);
12611 /* We'd like to arrange to call fputs(string,stdout) here,
12612 but we need stdout and don't have a way to get it yet. */
12617 /* The other optimizations can be done only on the non-va_list variants. */
12618 else if (fcode == BUILT_IN_VPRINTF || fcode == BUILT_IN_VPRINTF_CHK)
12621 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
12622 else if (strcmp (fmt_str, target_percent_s_newline) == 0)
12624 if (!arg || !validate_arg (arg, POINTER_TYPE))
12627 call = build_call_expr_loc (loc, fn_puts, 1, arg);
12630 /* If the format specifier was "%c", call __builtin_putchar(arg). */
12631 else if (strcmp (fmt_str, target_percent_c) == 0)
12633 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12636 call = build_call_expr_loc (loc, fn_putchar, 1, arg);
12642 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
12645 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
12646 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
12647 more than 3 arguments, and ARG may be null in the 2-argument case.
12649 Return NULL_TREE if no simplification was possible, otherwise return the
12650 simplified form of the call as a tree. FCODE is the BUILT_IN_*
12651 code of the function to be simplified. */
12654 fold_builtin_fprintf (location_t loc, tree fndecl, tree fp,
12655 tree fmt, tree arg, bool ignore,
12656 enum built_in_function fcode)
12658 tree fn_fputc, fn_fputs, call = NULL_TREE;
12659 const char *fmt_str = NULL;
12661 /* If the return value is used, don't do the transformation. */
12665 /* Verify the required arguments in the original call. */
12666 if (!validate_arg (fp, POINTER_TYPE))
12668 if (!validate_arg (fmt, POINTER_TYPE))
12671 /* Check whether the format is a literal string constant. */
12672 fmt_str = c_getstr (fmt);
12673 if (fmt_str == NULL)
12676 if (fcode == BUILT_IN_FPRINTF_UNLOCKED)
12678 /* If we're using an unlocked function, assume the other
12679 unlocked functions exist explicitly. */
12680 fn_fputc = built_in_decls[BUILT_IN_FPUTC_UNLOCKED];
12681 fn_fputs = built_in_decls[BUILT_IN_FPUTS_UNLOCKED];
12685 fn_fputc = implicit_built_in_decls[BUILT_IN_FPUTC];
12686 fn_fputs = implicit_built_in_decls[BUILT_IN_FPUTS];
12689 if (!init_target_chars ())
12692 /* If the format doesn't contain % args or %%, use strcpy. */
12693 if (strchr (fmt_str, target_percent) == NULL)
12695 if (fcode != BUILT_IN_VFPRINTF && fcode != BUILT_IN_VFPRINTF_CHK
12699 /* If the format specifier was "", fprintf does nothing. */
12700 if (fmt_str[0] == '\0')
12702 /* If FP has side-effects, just wait until gimplification is
12704 if (TREE_SIDE_EFFECTS (fp))
12707 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
12710 /* When "string" doesn't contain %, replace all cases of
12711 fprintf (fp, string) with fputs (string, fp). The fputs
12712 builtin will take care of special cases like length == 1. */
12714 call = build_call_expr_loc (loc, fn_fputs, 2, fmt, fp);
12717 /* The other optimizations can be done only on the non-va_list variants. */
12718 else if (fcode == BUILT_IN_VFPRINTF || fcode == BUILT_IN_VFPRINTF_CHK)
12721 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
12722 else if (strcmp (fmt_str, target_percent_s) == 0)
12724 if (!arg || !validate_arg (arg, POINTER_TYPE))
12727 call = build_call_expr_loc (loc, fn_fputs, 2, arg, fp);
12730 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
12731 else if (strcmp (fmt_str, target_percent_c) == 0)
12733 if (!arg || !validate_arg (arg, INTEGER_TYPE))
12736 call = build_call_expr_loc (loc, fn_fputc, 2, arg, fp);
12741 return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)), call);
12744 /* Initialize format string characters in the target charset. */
12747 init_target_chars (void)
12752 target_newline = lang_hooks.to_target_charset ('\n');
12753 target_percent = lang_hooks.to_target_charset ('%');
12754 target_c = lang_hooks.to_target_charset ('c');
12755 target_s = lang_hooks.to_target_charset ('s');
12756 if (target_newline == 0 || target_percent == 0 || target_c == 0
12760 target_percent_c[0] = target_percent;
12761 target_percent_c[1] = target_c;
12762 target_percent_c[2] = '\0';
12764 target_percent_s[0] = target_percent;
12765 target_percent_s[1] = target_s;
12766 target_percent_s[2] = '\0';
12768 target_percent_s_newline[0] = target_percent;
12769 target_percent_s_newline[1] = target_s;
12770 target_percent_s_newline[2] = target_newline;
12771 target_percent_s_newline[3] = '\0';
12778 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
12779 and no overflow/underflow occurred. INEXACT is true if M was not
12780 exactly calculated. TYPE is the tree type for the result. This
12781 function assumes that you cleared the MPFR flags and then
12782 calculated M to see if anything subsequently set a flag prior to
12783 entering this function. Return NULL_TREE if any checks fail. */
12786 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
12788 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12789 overflow/underflow occurred. If -frounding-math, proceed iff the
12790 result of calling FUNC was exact. */
12791 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
12792 && (!flag_rounding_math || !inexact))
12794 REAL_VALUE_TYPE rr;
12796 real_from_mpfr (&rr, m, type, GMP_RNDN);
12797 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
12798 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12799 but the mpft_t is not, then we underflowed in the
12801 if (real_isfinite (&rr)
12802 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
12804 REAL_VALUE_TYPE rmode;
12806 real_convert (&rmode, TYPE_MODE (type), &rr);
12807 /* Proceed iff the specified mode can hold the value. */
12808 if (real_identical (&rmode, &rr))
12809 return build_real (type, rmode);
12815 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
12816 number and no overflow/underflow occurred. INEXACT is true if M
12817 was not exactly calculated. TYPE is the tree type for the result.
12818 This function assumes that you cleared the MPFR flags and then
12819 calculated M to see if anything subsequently set a flag prior to
12820 entering this function. Return NULL_TREE if any checks fail, if
12821 FORCE_CONVERT is true, then bypass the checks. */
12824 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
12826 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12827 overflow/underflow occurred. If -frounding-math, proceed iff the
12828 result of calling FUNC was exact. */
12830 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
12831 && !mpfr_overflow_p () && !mpfr_underflow_p ()
12832 && (!flag_rounding_math || !inexact)))
12834 REAL_VALUE_TYPE re, im;
12836 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
12837 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
12838 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
12839 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12840 but the mpft_t is not, then we underflowed in the
12843 || (real_isfinite (&re) && real_isfinite (&im)
12844 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
12845 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
12847 REAL_VALUE_TYPE re_mode, im_mode;
12849 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
12850 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
12851 /* Proceed iff the specified mode can hold the value. */
12853 || (real_identical (&re_mode, &re)
12854 && real_identical (&im_mode, &im)))
12855 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
12856 build_real (TREE_TYPE (type), im_mode));
12862 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
12863 FUNC on it and return the resulting value as a tree with type TYPE.
12864 If MIN and/or MAX are not NULL, then the supplied ARG must be
12865 within those bounds. If INCLUSIVE is true, then MIN/MAX are
12866 acceptable values, otherwise they are not. The mpfr precision is
12867 set to the precision of TYPE. We assume that function FUNC returns
12868 zero if the result could be calculated exactly within the requested
12872 do_mpfr_arg1 (tree arg, tree type, int (*func)(mpfr_ptr, mpfr_srcptr, mp_rnd_t),
12873 const REAL_VALUE_TYPE *min, const REAL_VALUE_TYPE *max,
12876 tree result = NULL_TREE;
12880 /* To proceed, MPFR must exactly represent the target floating point
12881 format, which only happens when the target base equals two. */
12882 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12883 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg))
12885 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
12887 if (real_isfinite (ra)
12888 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min))
12889 && (!max || real_compare (inclusive ? LE_EXPR: LT_EXPR , ra, max)))
12891 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12892 const int prec = fmt->p;
12893 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12897 mpfr_init2 (m, prec);
12898 mpfr_from_real (m, ra, GMP_RNDN);
12899 mpfr_clear_flags ();
12900 inexact = func (m, m, rnd);
12901 result = do_mpfr_ckconv (m, type, inexact);
12909 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
12910 FUNC on it and return the resulting value as a tree with type TYPE.
12911 The mpfr precision is set to the precision of TYPE. We assume that
12912 function FUNC returns zero if the result could be calculated
12913 exactly within the requested precision. */
12916 do_mpfr_arg2 (tree arg1, tree arg2, tree type,
12917 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12919 tree result = NULL_TREE;
12924 /* To proceed, MPFR must exactly represent the target floating point
12925 format, which only happens when the target base equals two. */
12926 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12927 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12928 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
12930 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12931 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12933 if (real_isfinite (ra1) && real_isfinite (ra2))
12935 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12936 const int prec = fmt->p;
12937 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12941 mpfr_inits2 (prec, m1, m2, NULL);
12942 mpfr_from_real (m1, ra1, GMP_RNDN);
12943 mpfr_from_real (m2, ra2, GMP_RNDN);
12944 mpfr_clear_flags ();
12945 inexact = func (m1, m1, m2, rnd);
12946 result = do_mpfr_ckconv (m1, type, inexact);
12947 mpfr_clears (m1, m2, NULL);
12954 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
12955 FUNC on it and return the resulting value as a tree with type TYPE.
12956 The mpfr precision is set to the precision of TYPE. We assume that
12957 function FUNC returns zero if the result could be calculated
12958 exactly within the requested precision. */
12961 do_mpfr_arg3 (tree arg1, tree arg2, tree arg3, tree type,
12962 int (*func)(mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_srcptr, mp_rnd_t))
12964 tree result = NULL_TREE;
12970 /* To proceed, MPFR must exactly represent the target floating point
12971 format, which only happens when the target base equals two. */
12972 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
12973 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1)
12974 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2)
12975 && TREE_CODE (arg3) == REAL_CST && !TREE_OVERFLOW (arg3))
12977 const REAL_VALUE_TYPE *const ra1 = &TREE_REAL_CST (arg1);
12978 const REAL_VALUE_TYPE *const ra2 = &TREE_REAL_CST (arg2);
12979 const REAL_VALUE_TYPE *const ra3 = &TREE_REAL_CST (arg3);
12981 if (real_isfinite (ra1) && real_isfinite (ra2) && real_isfinite (ra3))
12983 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
12984 const int prec = fmt->p;
12985 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
12989 mpfr_inits2 (prec, m1, m2, m3, NULL);
12990 mpfr_from_real (m1, ra1, GMP_RNDN);
12991 mpfr_from_real (m2, ra2, GMP_RNDN);
12992 mpfr_from_real (m3, ra3, GMP_RNDN);
12993 mpfr_clear_flags ();
12994 inexact = func (m1, m1, m2, m3, rnd);
12995 result = do_mpfr_ckconv (m1, type, inexact);
12996 mpfr_clears (m1, m2, m3, NULL);
13003 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13004 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13005 If ARG_SINP and ARG_COSP are NULL then the result is returned
13006 as a complex value.
13007 The type is taken from the type of ARG and is used for setting the
13008 precision of the calculation and results. */
13011 do_mpfr_sincos (tree arg, tree arg_sinp, tree arg_cosp)
13013 tree const type = TREE_TYPE (arg);
13014 tree result = NULL_TREE;
13018 /* To proceed, MPFR must exactly represent the target floating point
13019 format, which only happens when the target base equals two. */
13020 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13021 && TREE_CODE (arg) == REAL_CST
13022 && !TREE_OVERFLOW (arg))
13024 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg);
13026 if (real_isfinite (ra))
13028 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13029 const int prec = fmt->p;
13030 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13031 tree result_s, result_c;
13035 mpfr_inits2 (prec, m, ms, mc, NULL);
13036 mpfr_from_real (m, ra, GMP_RNDN);
13037 mpfr_clear_flags ();
13038 inexact = mpfr_sin_cos (ms, mc, m, rnd);
13039 result_s = do_mpfr_ckconv (ms, type, inexact);
13040 result_c = do_mpfr_ckconv (mc, type, inexact);
13041 mpfr_clears (m, ms, mc, NULL);
13042 if (result_s && result_c)
13044 /* If we are to return in a complex value do so. */
13045 if (!arg_sinp && !arg_cosp)
13046 return build_complex (build_complex_type (type),
13047 result_c, result_s);
13049 /* Dereference the sin/cos pointer arguments. */
13050 arg_sinp = build_fold_indirect_ref (arg_sinp);
13051 arg_cosp = build_fold_indirect_ref (arg_cosp);
13052 /* Proceed if valid pointer type were passed in. */
13053 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp)) == TYPE_MAIN_VARIANT (type)
13054 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp)) == TYPE_MAIN_VARIANT (type))
13056 /* Set the values. */
13057 result_s = fold_build2 (MODIFY_EXPR, type, arg_sinp,
13059 TREE_SIDE_EFFECTS (result_s) = 1;
13060 result_c = fold_build2 (MODIFY_EXPR, type, arg_cosp,
13062 TREE_SIDE_EFFECTS (result_c) = 1;
13063 /* Combine the assignments into a compound expr. */
13064 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13065 result_s, result_c));
13073 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13074 two-argument mpfr order N Bessel function FUNC on them and return
13075 the resulting value as a tree with type TYPE. The mpfr precision
13076 is set to the precision of TYPE. We assume that function FUNC
13077 returns zero if the result could be calculated exactly within the
13078 requested precision. */
13080 do_mpfr_bessel_n (tree arg1, tree arg2, tree type,
13081 int (*func)(mpfr_ptr, long, mpfr_srcptr, mp_rnd_t),
13082 const REAL_VALUE_TYPE *min, bool inclusive)
13084 tree result = NULL_TREE;
13089 /* To proceed, MPFR must exactly represent the target floating point
13090 format, which only happens when the target base equals two. */
13091 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13092 && host_integerp (arg1, 0)
13093 && TREE_CODE (arg2) == REAL_CST && !TREE_OVERFLOW (arg2))
13095 const HOST_WIDE_INT n = tree_low_cst(arg1, 0);
13096 const REAL_VALUE_TYPE *const ra = &TREE_REAL_CST (arg2);
13099 && real_isfinite (ra)
13100 && (!min || real_compare (inclusive ? GE_EXPR: GT_EXPR , ra, min)))
13102 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13103 const int prec = fmt->p;
13104 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13108 mpfr_init2 (m, prec);
13109 mpfr_from_real (m, ra, GMP_RNDN);
13110 mpfr_clear_flags ();
13111 inexact = func (m, n, m, rnd);
13112 result = do_mpfr_ckconv (m, type, inexact);
13120 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13121 the pointer *(ARG_QUO) and return the result. The type is taken
13122 from the type of ARG0 and is used for setting the precision of the
13123 calculation and results. */
13126 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
13128 tree const type = TREE_TYPE (arg0);
13129 tree result = NULL_TREE;
13134 /* To proceed, MPFR must exactly represent the target floating point
13135 format, which only happens when the target base equals two. */
13136 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13137 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
13138 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
13140 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
13141 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
13143 if (real_isfinite (ra0) && real_isfinite (ra1))
13145 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13146 const int prec = fmt->p;
13147 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13152 mpfr_inits2 (prec, m0, m1, NULL);
13153 mpfr_from_real (m0, ra0, GMP_RNDN);
13154 mpfr_from_real (m1, ra1, GMP_RNDN);
13155 mpfr_clear_flags ();
13156 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
13157 /* Remquo is independent of the rounding mode, so pass
13158 inexact=0 to do_mpfr_ckconv(). */
13159 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
13160 mpfr_clears (m0, m1, NULL);
13163 /* MPFR calculates quo in the host's long so it may
13164 return more bits in quo than the target int can hold
13165 if sizeof(host long) > sizeof(target int). This can
13166 happen even for native compilers in LP64 mode. In
13167 these cases, modulo the quo value with the largest
13168 number that the target int can hold while leaving one
13169 bit for the sign. */
13170 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
13171 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
13173 /* Dereference the quo pointer argument. */
13174 arg_quo = build_fold_indirect_ref (arg_quo);
13175 /* Proceed iff a valid pointer type was passed in. */
13176 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
13178 /* Set the value. */
13180 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
13181 build_int_cst (TREE_TYPE (arg_quo),
13183 TREE_SIDE_EFFECTS (result_quo) = 1;
13184 /* Combine the quo assignment with the rem. */
13185 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13186 result_quo, result_rem));
13194 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13195 resulting value as a tree with type TYPE. The mpfr precision is
13196 set to the precision of TYPE. We assume that this mpfr function
13197 returns zero if the result could be calculated exactly within the
13198 requested precision. In addition, the integer pointer represented
13199 by ARG_SG will be dereferenced and set to the appropriate signgam
13203 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
13205 tree result = NULL_TREE;
13209 /* To proceed, MPFR must exactly represent the target floating point
13210 format, which only happens when the target base equals two. Also
13211 verify ARG is a constant and that ARG_SG is an int pointer. */
13212 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
13213 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
13214 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
13215 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
13217 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
13219 /* In addition to NaN and Inf, the argument cannot be zero or a
13220 negative integer. */
13221 if (real_isfinite (ra)
13222 && ra->cl != rvc_zero
13223 && !(real_isneg(ra) && real_isinteger(ra, TYPE_MODE (type))))
13225 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
13226 const int prec = fmt->p;
13227 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
13232 mpfr_init2 (m, prec);
13233 mpfr_from_real (m, ra, GMP_RNDN);
13234 mpfr_clear_flags ();
13235 inexact = mpfr_lgamma (m, &sg, m, rnd);
13236 result_lg = do_mpfr_ckconv (m, type, inexact);
13242 /* Dereference the arg_sg pointer argument. */
13243 arg_sg = build_fold_indirect_ref (arg_sg);
13244 /* Assign the signgam value into *arg_sg. */
13245 result_sg = fold_build2 (MODIFY_EXPR,
13246 TREE_TYPE (arg_sg), arg_sg,
13247 build_int_cst (TREE_TYPE (arg_sg), sg));
13248 TREE_SIDE_EFFECTS (result_sg) = 1;
13249 /* Combine the signgam assignment with the lgamma result. */
13250 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
13251 result_sg, result_lg));
13259 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13260 function FUNC on it and return the resulting value as a tree with
13261 type TYPE. The mpfr precision is set to the precision of TYPE. We
13262 assume that function FUNC returns zero if the result could be
13263 calculated exactly within the requested precision. */
13266 do_mpc_arg1 (tree arg, tree type, int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
13268 tree result = NULL_TREE;
13272 /* To proceed, MPFR must exactly represent the target floating point
13273 format, which only happens when the target base equals two. */
13274 if (TREE_CODE (arg) == COMPLEX_CST && !TREE_OVERFLOW (arg)
13275 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE
13276 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg))))->b == 2)
13278 const REAL_VALUE_TYPE *const re = TREE_REAL_CST_PTR (TREE_REALPART (arg));
13279 const REAL_VALUE_TYPE *const im = TREE_REAL_CST_PTR (TREE_IMAGPART (arg));
13281 if (real_isfinite (re) && real_isfinite (im))
13283 const struct real_format *const fmt =
13284 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13285 const int prec = fmt->p;
13286 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13287 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13291 mpc_init2 (m, prec);
13292 mpfr_from_real (mpc_realref(m), re, rnd);
13293 mpfr_from_real (mpc_imagref(m), im, rnd);
13294 mpfr_clear_flags ();
13295 inexact = func (m, m, crnd);
13296 result = do_mpc_ckconv (m, type, inexact, /*force_convert=*/ 0);
13304 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
13305 mpc function FUNC on it and return the resulting value as a tree
13306 with type TYPE. The mpfr precision is set to the precision of
13307 TYPE. We assume that function FUNC returns zero if the result
13308 could be calculated exactly within the requested precision. If
13309 DO_NONFINITE is true, then fold expressions containing Inf or NaN
13310 in the arguments and/or results. */
13313 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
13314 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
13316 tree result = NULL_TREE;
13321 /* To proceed, MPFR must exactly represent the target floating point
13322 format, which only happens when the target base equals two. */
13323 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
13324 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
13325 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
13326 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
13327 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
13329 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
13330 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
13331 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
13332 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
13335 || (real_isfinite (re0) && real_isfinite (im0)
13336 && real_isfinite (re1) && real_isfinite (im1)))
13338 const struct real_format *const fmt =
13339 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
13340 const int prec = fmt->p;
13341 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
13342 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
13346 mpc_init2 (m0, prec);
13347 mpc_init2 (m1, prec);
13348 mpfr_from_real (mpc_realref(m0), re0, rnd);
13349 mpfr_from_real (mpc_imagref(m0), im0, rnd);
13350 mpfr_from_real (mpc_realref(m1), re1, rnd);
13351 mpfr_from_real (mpc_imagref(m1), im1, rnd);
13352 mpfr_clear_flags ();
13353 inexact = func (m0, m0, m1, crnd);
13354 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
13363 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
13364 a normal call should be emitted rather than expanding the function
13365 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13368 gimple_fold_builtin_sprintf_chk (gimple stmt, enum built_in_function fcode)
13370 int nargs = gimple_call_num_args (stmt);
13372 return fold_builtin_sprintf_chk_1 (gimple_location (stmt), nargs,
13374 ? gimple_call_arg_ptr (stmt, 0)
13375 : &error_mark_node), fcode);
13378 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
13379 a normal call should be emitted rather than expanding the function
13380 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13381 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13382 passed as second argument. */
13385 gimple_fold_builtin_snprintf_chk (gimple stmt, tree maxlen,
13386 enum built_in_function fcode)
13388 int nargs = gimple_call_num_args (stmt);
13390 return fold_builtin_snprintf_chk_1 (gimple_location (stmt), nargs,
13392 ? gimple_call_arg_ptr (stmt, 0)
13393 : &error_mark_node), maxlen, fcode);
13396 /* Builtins with folding operations that operate on "..." arguments
13397 need special handling; we need to store the arguments in a convenient
13398 data structure before attempting any folding. Fortunately there are
13399 only a few builtins that fall into this category. FNDECL is the
13400 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13401 result of the function call is ignored. */
13404 gimple_fold_builtin_varargs (tree fndecl, gimple stmt,
13405 bool ignore ATTRIBUTE_UNUSED)
13407 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
13408 tree ret = NULL_TREE;
13412 case BUILT_IN_SPRINTF_CHK:
13413 case BUILT_IN_VSPRINTF_CHK:
13414 ret = gimple_fold_builtin_sprintf_chk (stmt, fcode);
13417 case BUILT_IN_SNPRINTF_CHK:
13418 case BUILT_IN_VSNPRINTF_CHK:
13419 ret = gimple_fold_builtin_snprintf_chk (stmt, NULL_TREE, fcode);
13426 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
13427 TREE_NO_WARNING (ret) = 1;
13433 /* A wrapper function for builtin folding that prevents warnings for
13434 "statement without effect" and the like, caused by removing the
13435 call node earlier than the warning is generated. */
13438 fold_call_stmt (gimple stmt, bool ignore)
13440 tree ret = NULL_TREE;
13441 tree fndecl = gimple_call_fndecl (stmt);
13442 location_t loc = gimple_location (stmt);
13444 && TREE_CODE (fndecl) == FUNCTION_DECL
13445 && DECL_BUILT_IN (fndecl)
13446 && !gimple_call_va_arg_pack_p (stmt))
13448 int nargs = gimple_call_num_args (stmt);
13449 tree *args = (nargs > 0
13450 ? gimple_call_arg_ptr (stmt, 0)
13451 : &error_mark_node);
13453 if (avoid_folding_inline_builtin (fndecl))
13455 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
13457 return targetm.fold_builtin (fndecl, nargs, args, ignore);
13461 if (nargs <= MAX_ARGS_TO_FOLD_BUILTIN)
13462 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
13464 ret = gimple_fold_builtin_varargs (fndecl, stmt, ignore);
13467 /* Propagate location information from original call to
13468 expansion of builtin. Otherwise things like
13469 maybe_emit_chk_warning, that operate on the expansion
13470 of a builtin, will use the wrong location information. */
13471 if (gimple_has_location (stmt))
13473 tree realret = ret;
13474 if (TREE_CODE (ret) == NOP_EXPR)
13475 realret = TREE_OPERAND (ret, 0);
13476 if (CAN_HAVE_LOCATION_P (realret)
13477 && !EXPR_HAS_LOCATION (realret))
13478 SET_EXPR_LOCATION (realret, loc);
13488 /* Look up the function in built_in_decls that corresponds to DECL
13489 and set ASMSPEC as its user assembler name. DECL must be a
13490 function decl that declares a builtin. */
13493 set_builtin_user_assembler_name (tree decl, const char *asmspec)
13496 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
13497 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
13500 builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
13501 set_user_assembler_name (builtin, asmspec);
13502 switch (DECL_FUNCTION_CODE (decl))
13504 case BUILT_IN_MEMCPY:
13505 init_block_move_fn (asmspec);
13506 memcpy_libfunc = set_user_assembler_libfunc ("memcpy", asmspec);
13508 case BUILT_IN_MEMSET:
13509 init_block_clear_fn (asmspec);
13510 memset_libfunc = set_user_assembler_libfunc ("memset", asmspec);
13512 case BUILT_IN_MEMMOVE:
13513 memmove_libfunc = set_user_assembler_libfunc ("memmove", asmspec);
13515 case BUILT_IN_MEMCMP:
13516 memcmp_libfunc = set_user_assembler_libfunc ("memcmp", asmspec);
13518 case BUILT_IN_ABORT:
13519 abort_libfunc = set_user_assembler_libfunc ("abort", asmspec);
13522 if (INT_TYPE_SIZE < BITS_PER_WORD)
13524 set_user_assembler_libfunc ("ffs", asmspec);
13525 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE,
13526 MODE_INT, 0), "ffs");
13534 /* Return true if DECL is a builtin that expands to a constant or similarly
13537 is_simple_builtin (tree decl)
13539 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13540 switch (DECL_FUNCTION_CODE (decl))
13542 /* Builtins that expand to constants. */
13543 case BUILT_IN_CONSTANT_P:
13544 case BUILT_IN_EXPECT:
13545 case BUILT_IN_OBJECT_SIZE:
13546 case BUILT_IN_UNREACHABLE:
13547 /* Simple register moves or loads from stack. */
13548 case BUILT_IN_ASSUME_ALIGNED:
13549 case BUILT_IN_RETURN_ADDRESS:
13550 case BUILT_IN_EXTRACT_RETURN_ADDR:
13551 case BUILT_IN_FROB_RETURN_ADDR:
13552 case BUILT_IN_RETURN:
13553 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
13554 case BUILT_IN_FRAME_ADDRESS:
13555 case BUILT_IN_VA_END:
13556 case BUILT_IN_STACK_SAVE:
13557 case BUILT_IN_STACK_RESTORE:
13558 /* Exception state returns or moves registers around. */
13559 case BUILT_IN_EH_FILTER:
13560 case BUILT_IN_EH_POINTER:
13561 case BUILT_IN_EH_COPY_VALUES:
13571 /* Return true if DECL is a builtin that is not expensive, i.e., they are
13572 most probably expanded inline into reasonably simple code. This is a
13573 superset of is_simple_builtin. */
13575 is_inexpensive_builtin (tree decl)
13579 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
13581 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
13582 switch (DECL_FUNCTION_CODE (decl))
13585 case BUILT_IN_ALLOCA:
13586 case BUILT_IN_ALLOCA_WITH_ALIGN:
13587 case BUILT_IN_BSWAP32:
13588 case BUILT_IN_BSWAP64:
13590 case BUILT_IN_CLZIMAX:
13591 case BUILT_IN_CLZL:
13592 case BUILT_IN_CLZLL:
13594 case BUILT_IN_CTZIMAX:
13595 case BUILT_IN_CTZL:
13596 case BUILT_IN_CTZLL:
13598 case BUILT_IN_FFSIMAX:
13599 case BUILT_IN_FFSL:
13600 case BUILT_IN_FFSLL:
13601 case BUILT_IN_IMAXABS:
13602 case BUILT_IN_FINITE:
13603 case BUILT_IN_FINITEF:
13604 case BUILT_IN_FINITEL:
13605 case BUILT_IN_FINITED32:
13606 case BUILT_IN_FINITED64:
13607 case BUILT_IN_FINITED128:
13608 case BUILT_IN_FPCLASSIFY:
13609 case BUILT_IN_ISFINITE:
13610 case BUILT_IN_ISINF_SIGN:
13611 case BUILT_IN_ISINF:
13612 case BUILT_IN_ISINFF:
13613 case BUILT_IN_ISINFL:
13614 case BUILT_IN_ISINFD32:
13615 case BUILT_IN_ISINFD64:
13616 case BUILT_IN_ISINFD128:
13617 case BUILT_IN_ISNAN:
13618 case BUILT_IN_ISNANF:
13619 case BUILT_IN_ISNANL:
13620 case BUILT_IN_ISNAND32:
13621 case BUILT_IN_ISNAND64:
13622 case BUILT_IN_ISNAND128:
13623 case BUILT_IN_ISNORMAL:
13624 case BUILT_IN_ISGREATER:
13625 case BUILT_IN_ISGREATEREQUAL:
13626 case BUILT_IN_ISLESS:
13627 case BUILT_IN_ISLESSEQUAL:
13628 case BUILT_IN_ISLESSGREATER:
13629 case BUILT_IN_ISUNORDERED:
13630 case BUILT_IN_VA_ARG_PACK:
13631 case BUILT_IN_VA_ARG_PACK_LEN:
13632 case BUILT_IN_VA_COPY:
13633 case BUILT_IN_TRAP:
13634 case BUILT_IN_SAVEREGS:
13635 case BUILT_IN_POPCOUNTL:
13636 case BUILT_IN_POPCOUNTLL:
13637 case BUILT_IN_POPCOUNTIMAX:
13638 case BUILT_IN_POPCOUNT:
13639 case BUILT_IN_PARITYL:
13640 case BUILT_IN_PARITYLL:
13641 case BUILT_IN_PARITYIMAX:
13642 case BUILT_IN_PARITY:
13643 case BUILT_IN_LABS:
13644 case BUILT_IN_LLABS:
13645 case BUILT_IN_PREFETCH:
13649 return is_simple_builtin (decl);