1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
33 #include "hard-reg-set.h"
36 #include "insn-config.h"
42 #include "typeclass.h"
46 #include "langhooks.h"
47 #include "basic-block.h"
48 #include "tree-mudflap.h"
49 #include "tree-flow.h"
50 #include "value-prof.h"
51 #include "diagnostic-core.h"
55 #ifndef PAD_VARARGS_DOWN
56 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
58 static tree
do_mpc_arg1 (tree
, tree
, int (*)(mpc_ptr
, mpc_srcptr
, mpc_rnd_t
));
60 struct target_builtins default_target_builtins
;
62 struct target_builtins
*this_target_builtins
= &default_target_builtins
;
65 /* Define the names of the builtin function types and codes. */
66 const char *const built_in_class_names
[4]
67 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
69 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
70 const char * built_in_names
[(int) END_BUILTINS
] =
72 #include "builtins.def"
76 /* Setup an array of _DECL trees, make sure each element is
77 initialized to NULL_TREE. */
78 builtin_info_type builtin_info
;
80 static const char *c_getstr (tree
);
81 static rtx
c_readstr (const char *, enum machine_mode
);
82 static int target_char_cast (tree
, char *);
83 static rtx
get_memory_rtx (tree
, tree
);
84 static int apply_args_size (void);
85 static int apply_result_size (void);
86 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
87 static rtx
result_vector (int, rtx
);
89 static void expand_builtin_update_setjmp_buf (rtx
);
90 static void expand_builtin_prefetch (tree
);
91 static rtx
expand_builtin_apply_args (void);
92 static rtx
expand_builtin_apply_args_1 (void);
93 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
94 static void expand_builtin_return (rtx
);
95 static enum type_class
type_to_class (tree
);
96 static rtx
expand_builtin_classify_type (tree
);
97 static void expand_errno_check (tree
, rtx
);
98 static rtx
expand_builtin_mathfn (tree
, rtx
, rtx
);
99 static rtx
expand_builtin_mathfn_2 (tree
, rtx
, rtx
);
100 static rtx
expand_builtin_mathfn_3 (tree
, rtx
, rtx
);
101 static rtx
expand_builtin_mathfn_ternary (tree
, rtx
, rtx
);
102 static rtx
expand_builtin_interclass_mathfn (tree
, rtx
);
103 static rtx
expand_builtin_sincos (tree
);
104 static rtx
expand_builtin_cexpi (tree
, rtx
);
105 static rtx
expand_builtin_int_roundingfn (tree
, rtx
);
106 static rtx
expand_builtin_int_roundingfn_2 (tree
, rtx
);
107 static rtx
expand_builtin_next_arg (void);
108 static rtx
expand_builtin_va_start (tree
);
109 static rtx
expand_builtin_va_end (tree
);
110 static rtx
expand_builtin_va_copy (tree
);
111 static rtx
expand_builtin_memcmp (tree
, rtx
, enum machine_mode
);
112 static rtx
expand_builtin_strcmp (tree
, rtx
);
113 static rtx
expand_builtin_strncmp (tree
, rtx
, enum machine_mode
);
114 static rtx
builtin_memcpy_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
115 static rtx
expand_builtin_memcpy (tree
, rtx
);
116 static rtx
expand_builtin_mempcpy (tree
, rtx
, enum machine_mode
);
117 static rtx
expand_builtin_mempcpy_args (tree
, tree
, tree
, rtx
,
118 enum machine_mode
, int);
119 static rtx
expand_builtin_strcpy (tree
, rtx
);
120 static rtx
expand_builtin_strcpy_args (tree
, tree
, rtx
);
121 static rtx
expand_builtin_stpcpy (tree
, rtx
, enum machine_mode
);
122 static rtx
expand_builtin_strncpy (tree
, rtx
);
123 static rtx
builtin_memset_gen_str (void *, HOST_WIDE_INT
, enum machine_mode
);
124 static rtx
expand_builtin_memset (tree
, rtx
, enum machine_mode
);
125 static rtx
expand_builtin_memset_args (tree
, tree
, tree
, rtx
, enum machine_mode
, tree
);
126 static rtx
expand_builtin_bzero (tree
);
127 static rtx
expand_builtin_strlen (tree
, rtx
, enum machine_mode
);
128 static rtx
expand_builtin_alloca (tree
, bool);
129 static rtx
expand_builtin_unop (enum machine_mode
, tree
, rtx
, rtx
, optab
);
130 static rtx
expand_builtin_frame_address (tree
, tree
);
131 static tree
stabilize_va_list_loc (location_t
, tree
, int);
132 static rtx
expand_builtin_expect (tree
, rtx
);
133 static tree
fold_builtin_constant_p (tree
);
134 static tree
fold_builtin_expect (location_t
, tree
, tree
);
135 static tree
fold_builtin_classify_type (tree
);
136 static tree
fold_builtin_strlen (location_t
, tree
, tree
);
137 static tree
fold_builtin_inf (location_t
, tree
, int);
138 static tree
fold_builtin_nan (tree
, tree
, int);
139 static tree
rewrite_call_expr (location_t
, tree
, int, tree
, int, ...);
140 static bool validate_arg (const_tree
, enum tree_code code
);
141 static bool integer_valued_real_p (tree
);
142 static tree
fold_trunc_transparent_mathfn (location_t
, tree
, tree
);
143 static bool readonly_data_expr (tree
);
144 static rtx
expand_builtin_fabs (tree
, rtx
, rtx
);
145 static rtx
expand_builtin_signbit (tree
, rtx
);
146 static tree
fold_builtin_sqrt (location_t
, tree
, tree
);
147 static tree
fold_builtin_cbrt (location_t
, tree
, tree
);
148 static tree
fold_builtin_pow (location_t
, tree
, tree
, tree
, tree
);
149 static tree
fold_builtin_powi (location_t
, tree
, tree
, tree
, tree
);
150 static tree
fold_builtin_cos (location_t
, tree
, tree
, tree
);
151 static tree
fold_builtin_cosh (location_t
, tree
, tree
, tree
);
152 static tree
fold_builtin_tan (tree
, tree
);
153 static tree
fold_builtin_trunc (location_t
, tree
, tree
);
154 static tree
fold_builtin_floor (location_t
, tree
, tree
);
155 static tree
fold_builtin_ceil (location_t
, tree
, tree
);
156 static tree
fold_builtin_round (location_t
, tree
, tree
);
157 static tree
fold_builtin_int_roundingfn (location_t
, tree
, tree
);
158 static tree
fold_builtin_bitop (tree
, tree
);
159 static tree
fold_builtin_memory_op (location_t
, tree
, tree
, tree
, tree
, bool, int);
160 static tree
fold_builtin_strchr (location_t
, tree
, tree
, tree
);
161 static tree
fold_builtin_memchr (location_t
, tree
, tree
, tree
, tree
);
162 static tree
fold_builtin_memcmp (location_t
, tree
, tree
, tree
);
163 static tree
fold_builtin_strcmp (location_t
, tree
, tree
);
164 static tree
fold_builtin_strncmp (location_t
, tree
, tree
, tree
);
165 static tree
fold_builtin_signbit (location_t
, tree
, tree
);
166 static tree
fold_builtin_copysign (location_t
, tree
, tree
, tree
, tree
);
167 static tree
fold_builtin_isascii (location_t
, tree
);
168 static tree
fold_builtin_toascii (location_t
, tree
);
169 static tree
fold_builtin_isdigit (location_t
, tree
);
170 static tree
fold_builtin_fabs (location_t
, tree
, tree
);
171 static tree
fold_builtin_abs (location_t
, tree
, tree
);
172 static tree
fold_builtin_unordered_cmp (location_t
, tree
, tree
, tree
, enum tree_code
,
174 static tree
fold_builtin_n (location_t
, tree
, tree
*, int, bool);
175 static tree
fold_builtin_0 (location_t
, tree
, bool);
176 static tree
fold_builtin_1 (location_t
, tree
, tree
, bool);
177 static tree
fold_builtin_2 (location_t
, tree
, tree
, tree
, bool);
178 static tree
fold_builtin_3 (location_t
, tree
, tree
, tree
, tree
, bool);
179 static tree
fold_builtin_4 (location_t
, tree
, tree
, tree
, tree
, tree
, bool);
180 static tree
fold_builtin_varargs (location_t
, tree
, tree
, bool);
182 static tree
fold_builtin_strpbrk (location_t
, tree
, tree
, tree
);
183 static tree
fold_builtin_strstr (location_t
, tree
, tree
, tree
);
184 static tree
fold_builtin_strrchr (location_t
, tree
, tree
, tree
);
185 static tree
fold_builtin_strcat (location_t
, tree
, tree
);
186 static tree
fold_builtin_strncat (location_t
, tree
, tree
, tree
);
187 static tree
fold_builtin_strspn (location_t
, tree
, tree
);
188 static tree
fold_builtin_strcspn (location_t
, tree
, tree
);
189 static tree
fold_builtin_sprintf (location_t
, tree
, tree
, tree
, int);
190 static tree
fold_builtin_snprintf (location_t
, tree
, tree
, tree
, tree
, int);
192 static rtx
expand_builtin_object_size (tree
);
193 static rtx
expand_builtin_memory_chk (tree
, rtx
, enum machine_mode
,
194 enum built_in_function
);
195 static void maybe_emit_chk_warning (tree
, enum built_in_function
);
196 static void maybe_emit_sprintf_chk_warning (tree
, enum built_in_function
);
197 static void maybe_emit_free_warning (tree
);
198 static tree
fold_builtin_object_size (tree
, tree
);
199 static tree
fold_builtin_strcat_chk (location_t
, tree
, tree
, tree
, tree
);
200 static tree
fold_builtin_strncat_chk (location_t
, tree
, tree
, tree
, tree
, tree
);
201 static tree
fold_builtin_sprintf_chk (location_t
, tree
, enum built_in_function
);
202 static tree
fold_builtin_printf (location_t
, tree
, tree
, tree
, bool, enum built_in_function
);
203 static tree
fold_builtin_fprintf (location_t
, tree
, tree
, tree
, tree
, bool,
204 enum built_in_function
);
205 static bool init_target_chars (void);
207 static unsigned HOST_WIDE_INT target_newline
;
208 static unsigned HOST_WIDE_INT target_percent
;
209 static unsigned HOST_WIDE_INT target_c
;
210 static unsigned HOST_WIDE_INT target_s
;
211 static char target_percent_c
[3];
212 static char target_percent_s
[3];
213 static char target_percent_s_newline
[4];
214 static tree
do_mpfr_arg1 (tree
, tree
, int (*)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
215 const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, bool);
216 static tree
do_mpfr_arg2 (tree
, tree
, tree
,
217 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
218 static tree
do_mpfr_arg3 (tree
, tree
, tree
, tree
,
219 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
220 static tree
do_mpfr_sincos (tree
, tree
, tree
);
221 static tree
do_mpfr_bessel_n (tree
, tree
, tree
,
222 int (*)(mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
223 const REAL_VALUE_TYPE
*, bool);
224 static tree
do_mpfr_remquo (tree
, tree
, tree
);
225 static tree
do_mpfr_lgamma_r (tree
, tree
, tree
);
226 static void expand_builtin_sync_synchronize (void);
228 /* Return true if NAME starts with __builtin_ or __sync_. */
231 is_builtin_name (const char *name
)
233 if (strncmp (name
, "__builtin_", 10) == 0)
235 if (strncmp (name
, "__sync_", 7) == 0)
237 if (strncmp (name
, "__atomic_", 9) == 0)
243 /* Return true if DECL is a function symbol representing a built-in. */
246 is_builtin_fn (tree decl
)
248 return TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
);
252 /* Return true if NODE should be considered for inline expansion regardless
253 of the optimization level. This means whenever a function is invoked with
254 its "internal" name, which normally contains the prefix "__builtin". */
257 called_as_built_in (tree node
)
259 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
260 we want the name used to call the function, not the name it
262 const char *name
= IDENTIFIER_POINTER (DECL_NAME (node
));
263 return is_builtin_name (name
);
266 /* Compute values M and N such that M divides (address of EXP - N) and
267 such that N < M. Store N in *BITPOSP and return M.
269 Note that the address (and thus the alignment) computed here is based
270 on the address to which a symbol resolves, whereas DECL_ALIGN is based
271 on the address at which an object is actually located. These two
272 addresses are not always the same. For example, on ARM targets,
273 the address &foo of a Thumb function foo() has the lowest bit set,
274 whereas foo() itself starts on an even address. */
277 get_object_alignment_1 (tree exp
, unsigned HOST_WIDE_INT
*bitposp
)
279 HOST_WIDE_INT bitsize
, bitpos
;
281 enum machine_mode mode
;
282 int unsignedp
, volatilep
;
283 unsigned int align
, inner
;
285 /* Get the innermost object and the constant (bitpos) and possibly
286 variable (offset) offset of the access. */
287 exp
= get_inner_reference (exp
, &bitsize
, &bitpos
, &offset
,
288 &mode
, &unsignedp
, &volatilep
, true);
290 /* Extract alignment information from the innermost object and
291 possibly adjust bitpos and offset. */
292 if (TREE_CODE (exp
) == CONST_DECL
)
293 exp
= DECL_INITIAL (exp
);
295 && TREE_CODE (exp
) != LABEL_DECL
)
297 if (TREE_CODE (exp
) == FUNCTION_DECL
)
299 /* Function addresses can encode extra information besides their
300 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
301 allows the low bit to be used as a virtual bit, we know
302 that the address itself must be 2-byte aligned. */
303 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_pfn
)
304 align
= 2 * BITS_PER_UNIT
;
306 align
= BITS_PER_UNIT
;
309 align
= DECL_ALIGN (exp
);
311 else if (CONSTANT_CLASS_P (exp
))
313 align
= TYPE_ALIGN (TREE_TYPE (exp
));
314 #ifdef CONSTANT_ALIGNMENT
315 align
= (unsigned)CONSTANT_ALIGNMENT (exp
, align
);
318 else if (TREE_CODE (exp
) == VIEW_CONVERT_EXPR
)
319 align
= TYPE_ALIGN (TREE_TYPE (exp
));
320 else if (TREE_CODE (exp
) == INDIRECT_REF
)
321 align
= TYPE_ALIGN (TREE_TYPE (exp
));
322 else if (TREE_CODE (exp
) == MEM_REF
)
324 tree addr
= TREE_OPERAND (exp
, 0);
325 struct ptr_info_def
*pi
;
326 if (TREE_CODE (addr
) == BIT_AND_EXPR
327 && TREE_CODE (TREE_OPERAND (addr
, 1)) == INTEGER_CST
)
329 align
= (TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1))
330 & -TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1)));
331 align
*= BITS_PER_UNIT
;
332 addr
= TREE_OPERAND (addr
, 0);
335 align
= BITS_PER_UNIT
;
336 if (TREE_CODE (addr
) == SSA_NAME
337 && (pi
= SSA_NAME_PTR_INFO (addr
)))
339 bitpos
+= (pi
->misalign
* BITS_PER_UNIT
) & ~(align
- 1);
340 align
= MAX (pi
->align
* BITS_PER_UNIT
, align
);
342 else if (TREE_CODE (addr
) == ADDR_EXPR
)
343 align
= MAX (align
, get_object_alignment (TREE_OPERAND (addr
, 0)));
344 bitpos
+= mem_ref_offset (exp
).low
* BITS_PER_UNIT
;
346 else if (TREE_CODE (exp
) == TARGET_MEM_REF
)
348 struct ptr_info_def
*pi
;
349 tree addr
= TMR_BASE (exp
);
350 if (TREE_CODE (addr
) == BIT_AND_EXPR
351 && TREE_CODE (TREE_OPERAND (addr
, 1)) == INTEGER_CST
)
353 align
= (TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1))
354 & -TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1)));
355 align
*= BITS_PER_UNIT
;
356 addr
= TREE_OPERAND (addr
, 0);
359 align
= BITS_PER_UNIT
;
360 if (TREE_CODE (addr
) == SSA_NAME
361 && (pi
= SSA_NAME_PTR_INFO (addr
)))
363 bitpos
+= (pi
->misalign
* BITS_PER_UNIT
) & ~(align
- 1);
364 align
= MAX (pi
->align
* BITS_PER_UNIT
, align
);
366 else if (TREE_CODE (addr
) == ADDR_EXPR
)
367 align
= MAX (align
, get_object_alignment (TREE_OPERAND (addr
, 0)));
368 if (TMR_OFFSET (exp
))
369 bitpos
+= TREE_INT_CST_LOW (TMR_OFFSET (exp
)) * BITS_PER_UNIT
;
370 if (TMR_INDEX (exp
) && TMR_STEP (exp
))
372 unsigned HOST_WIDE_INT step
= TREE_INT_CST_LOW (TMR_STEP (exp
));
373 align
= MIN (align
, (step
& -step
) * BITS_PER_UNIT
);
375 else if (TMR_INDEX (exp
))
376 align
= BITS_PER_UNIT
;
377 if (TMR_INDEX2 (exp
))
378 align
= BITS_PER_UNIT
;
381 align
= BITS_PER_UNIT
;
383 /* If there is a non-constant offset part extract the maximum
384 alignment that can prevail. */
390 if (TREE_CODE (offset
) == PLUS_EXPR
)
392 next_offset
= TREE_OPERAND (offset
, 0);
393 offset
= TREE_OPERAND (offset
, 1);
397 if (host_integerp (offset
, 1))
399 /* Any overflow in calculating offset_bits won't change
402 = ((unsigned) tree_low_cst (offset
, 1) * BITS_PER_UNIT
);
405 inner
= MIN (inner
, (offset_bits
& -offset_bits
));
407 else if (TREE_CODE (offset
) == MULT_EXPR
408 && host_integerp (TREE_OPERAND (offset
, 1), 1))
410 /* Any overflow in calculating offset_factor won't change
412 unsigned offset_factor
413 = ((unsigned) tree_low_cst (TREE_OPERAND (offset
, 1), 1)
417 inner
= MIN (inner
, (offset_factor
& -offset_factor
));
421 inner
= MIN (inner
, BITS_PER_UNIT
);
424 offset
= next_offset
;
427 /* Alignment is innermost object alignment adjusted by the constant
428 and non-constant offset parts. */
429 align
= MIN (align
, inner
);
430 bitpos
= bitpos
& (align
- 1);
436 /* Return the alignment in bits of EXP, an object. */
439 get_object_alignment (tree exp
)
441 unsigned HOST_WIDE_INT bitpos
= 0;
444 align
= get_object_alignment_1 (exp
, &bitpos
);
446 /* align and bitpos now specify known low bits of the pointer.
447 ptr & (align - 1) == bitpos. */
450 align
= (bitpos
& -bitpos
);
455 /* Return the alignment of object EXP, also considering its type when we do
456 not know of explicit misalignment. Only handle MEM_REF and TARGET_MEM_REF.
458 ??? Note that, in the general case, the type of an expression is not kept
459 consistent with misalignment information by the front-end, for example when
460 taking the address of a member of a packed structure. However, in most of
461 the cases, expressions have the alignment of their type so we optimistically
462 fall back to this alignment when we cannot compute a misalignment. */
465 get_object_or_type_alignment (tree exp
)
467 unsigned HOST_WIDE_INT misalign
;
468 unsigned int align
= get_object_alignment_1 (exp
, &misalign
);
470 gcc_assert (TREE_CODE (exp
) == MEM_REF
|| TREE_CODE (exp
) == TARGET_MEM_REF
);
473 align
= (misalign
& -misalign
);
475 align
= MAX (TYPE_ALIGN (TREE_TYPE (exp
)), align
);
480 /* Return the alignment in bits of EXP, a pointer valued expression.
481 The alignment returned is, by default, the alignment of the thing that
482 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
484 Otherwise, look at the expression to see if we can do better, i.e., if the
485 expression is actually pointing at an object whose alignment is tighter. */
488 get_pointer_alignment (tree exp
)
492 if (TREE_CODE (exp
) == ADDR_EXPR
)
493 return get_object_alignment (TREE_OPERAND (exp
, 0));
494 else if (TREE_CODE (exp
) == SSA_NAME
495 && POINTER_TYPE_P (TREE_TYPE (exp
)))
497 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (exp
);
500 return BITS_PER_UNIT
;
501 if (pi
->misalign
!= 0)
502 align
= (pi
->misalign
& -pi
->misalign
);
505 return align
* BITS_PER_UNIT
;
508 return POINTER_TYPE_P (TREE_TYPE (exp
)) ? BITS_PER_UNIT
: 0;
511 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
512 way, because it could contain a zero byte in the middle.
513 TREE_STRING_LENGTH is the size of the character array, not the string.
515 ONLY_VALUE should be nonzero if the result is not going to be emitted
516 into the instruction stream and zero if it is going to be expanded.
517 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
518 is returned, otherwise NULL, since
519 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
520 evaluate the side-effects.
522 The value returned is of type `ssizetype'.
524 Unfortunately, string_constant can't access the values of const char
525 arrays with initializers, so neither can we do so here. */
528 c_strlen (tree src
, int only_value
)
531 HOST_WIDE_INT offset
;
537 if (TREE_CODE (src
) == COND_EXPR
538 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
542 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
);
543 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
);
544 if (tree_int_cst_equal (len1
, len2
))
548 if (TREE_CODE (src
) == COMPOUND_EXPR
549 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
550 return c_strlen (TREE_OPERAND (src
, 1), only_value
);
552 loc
= EXPR_LOC_OR_HERE (src
);
554 src
= string_constant (src
, &offset_node
);
558 max
= TREE_STRING_LENGTH (src
) - 1;
559 ptr
= TREE_STRING_POINTER (src
);
561 if (offset_node
&& TREE_CODE (offset_node
) != INTEGER_CST
)
563 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
564 compute the offset to the following null if we don't know where to
565 start searching for it. */
568 for (i
= 0; i
< max
; i
++)
572 /* We don't know the starting offset, but we do know that the string
573 has no internal zero bytes. We can assume that the offset falls
574 within the bounds of the string; otherwise, the programmer deserves
575 what he gets. Subtract the offset from the length of the string,
576 and return that. This would perhaps not be valid if we were dealing
577 with named arrays in addition to literal string constants. */
579 return size_diffop_loc (loc
, size_int (max
), offset_node
);
582 /* We have a known offset into the string. Start searching there for
583 a null character if we can represent it as a single HOST_WIDE_INT. */
584 if (offset_node
== 0)
586 else if (! host_integerp (offset_node
, 0))
589 offset
= tree_low_cst (offset_node
, 0);
591 /* If the offset is known to be out of bounds, warn, and call strlen at
593 if (offset
< 0 || offset
> max
)
595 /* Suppress multiple warnings for propagated constant strings. */
596 if (! TREE_NO_WARNING (src
))
598 warning_at (loc
, 0, "offset outside bounds of constant string");
599 TREE_NO_WARNING (src
) = 1;
604 /* Use strlen to search for the first zero byte. Since any strings
605 constructed with build_string will have nulls appended, we win even
606 if we get handed something like (char[4])"abcd".
608 Since OFFSET is our starting index into the string, no further
609 calculation is needed. */
610 return ssize_int (strlen (ptr
+ offset
));
613 /* Return a char pointer for a C string if it is a string constant
614 or sum of string constant and integer constant. */
621 src
= string_constant (src
, &offset_node
);
625 if (offset_node
== 0)
626 return TREE_STRING_POINTER (src
);
627 else if (!host_integerp (offset_node
, 1)
628 || compare_tree_int (offset_node
, TREE_STRING_LENGTH (src
) - 1) > 0)
631 return TREE_STRING_POINTER (src
) + tree_low_cst (offset_node
, 1);
634 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
635 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
638 c_readstr (const char *str
, enum machine_mode mode
)
644 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
649 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
652 if (WORDS_BIG_ENDIAN
)
653 j
= GET_MODE_SIZE (mode
) - i
- 1;
654 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
655 && GET_MODE_SIZE (mode
) >= UNITS_PER_WORD
)
656 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
658 gcc_assert (j
< 2 * HOST_BITS_PER_WIDE_INT
);
661 ch
= (unsigned char) str
[i
];
662 c
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
664 return immed_double_const (c
[0], c
[1], mode
);
667 /* Cast a target constant CST to target CHAR and if that value fits into
668 host char type, return zero and put that value into variable pointed to by
672 target_char_cast (tree cst
, char *p
)
674 unsigned HOST_WIDE_INT val
, hostval
;
676 if (TREE_CODE (cst
) != INTEGER_CST
677 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
680 val
= TREE_INT_CST_LOW (cst
);
681 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
682 val
&= (((unsigned HOST_WIDE_INT
) 1) << CHAR_TYPE_SIZE
) - 1;
685 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
686 hostval
&= (((unsigned HOST_WIDE_INT
) 1) << HOST_BITS_PER_CHAR
) - 1;
695 /* Similar to save_expr, but assumes that arbitrary code is not executed
696 in between the multiple evaluations. In particular, we assume that a
697 non-addressable local variable will not be modified. */
700 builtin_save_expr (tree exp
)
702 if (TREE_CODE (exp
) == SSA_NAME
703 || (TREE_ADDRESSABLE (exp
) == 0
704 && (TREE_CODE (exp
) == PARM_DECL
705 || (TREE_CODE (exp
) == VAR_DECL
&& !TREE_STATIC (exp
)))))
708 return save_expr (exp
);
711 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
712 times to get the address of either a higher stack frame, or a return
713 address located within it (depending on FNDECL_CODE). */
716 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
)
720 #ifdef INITIAL_FRAME_ADDRESS_RTX
721 rtx tem
= INITIAL_FRAME_ADDRESS_RTX
;
725 /* For a zero count with __builtin_return_address, we don't care what
726 frame address we return, because target-specific definitions will
727 override us. Therefore frame pointer elimination is OK, and using
728 the soft frame pointer is OK.
730 For a nonzero count, or a zero count with __builtin_frame_address,
731 we require a stable offset from the current frame pointer to the
732 previous one, so we must use the hard frame pointer, and
733 we must disable frame pointer elimination. */
734 if (count
== 0 && fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
735 tem
= frame_pointer_rtx
;
738 tem
= hard_frame_pointer_rtx
;
740 /* Tell reload not to eliminate the frame pointer. */
741 crtl
->accesses_prior_frames
= 1;
745 /* Some machines need special handling before we can access
746 arbitrary frames. For example, on the SPARC, we must first flush
747 all register windows to the stack. */
748 #ifdef SETUP_FRAME_ADDRESSES
750 SETUP_FRAME_ADDRESSES ();
753 /* On the SPARC, the return address is not in the frame, it is in a
754 register. There is no way to access it off of the current frame
755 pointer, but it can be accessed off the previous frame pointer by
756 reading the value from the register window save area. */
757 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
758 if (fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
762 /* Scan back COUNT frames to the specified frame. */
763 for (i
= 0; i
< count
; i
++)
765 /* Assume the dynamic chain pointer is in the word that the
766 frame address points to, unless otherwise specified. */
767 #ifdef DYNAMIC_CHAIN_ADDRESS
768 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
770 tem
= memory_address (Pmode
, tem
);
771 tem
= gen_frame_mem (Pmode
, tem
);
772 tem
= copy_to_reg (tem
);
775 /* For __builtin_frame_address, return what we've got. But, on
776 the SPARC for example, we may have to add a bias. */
777 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
778 #ifdef FRAME_ADDR_RTX
779 return FRAME_ADDR_RTX (tem
);
784 /* For __builtin_return_address, get the return address from that frame. */
785 #ifdef RETURN_ADDR_RTX
786 tem
= RETURN_ADDR_RTX (count
, tem
);
788 tem
= memory_address (Pmode
,
789 plus_constant (tem
, GET_MODE_SIZE (Pmode
)));
790 tem
= gen_frame_mem (Pmode
, tem
);
795 /* Alias set used for setjmp buffer. */
796 static alias_set_type setjmp_alias_set
= -1;
798 /* Construct the leading half of a __builtin_setjmp call. Control will
799 return to RECEIVER_LABEL. This is also called directly by the SJLJ
800 exception handling code. */
803 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
805 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
809 if (setjmp_alias_set
== -1)
810 setjmp_alias_set
= new_alias_set ();
812 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
814 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
816 /* We store the frame pointer and the address of receiver_label in
817 the buffer and use the rest of it for the stack save area, which
818 is machine-dependent. */
820 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
821 set_mem_alias_set (mem
, setjmp_alias_set
);
822 emit_move_insn (mem
, targetm
.builtin_setjmp_frame_value ());
824 mem
= gen_rtx_MEM (Pmode
, plus_constant (buf_addr
, GET_MODE_SIZE (Pmode
))),
825 set_mem_alias_set (mem
, setjmp_alias_set
);
827 emit_move_insn (validize_mem (mem
),
828 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
830 stack_save
= gen_rtx_MEM (sa_mode
,
831 plus_constant (buf_addr
,
832 2 * GET_MODE_SIZE (Pmode
)));
833 set_mem_alias_set (stack_save
, setjmp_alias_set
);
834 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
836 /* If there is further processing to do, do it. */
837 #ifdef HAVE_builtin_setjmp_setup
838 if (HAVE_builtin_setjmp_setup
)
839 emit_insn (gen_builtin_setjmp_setup (buf_addr
));
842 /* We have a nonlocal label. */
843 cfun
->has_nonlocal_label
= 1;
846 /* Construct the trailing part of a __builtin_setjmp call. This is
847 also called directly by the SJLJ exception handling code. */
850 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED
)
854 /* Clobber the FP when we get here, so we have to make sure it's
855 marked as used by this function. */
856 emit_use (hard_frame_pointer_rtx
);
858 /* Mark the static chain as clobbered here so life information
859 doesn't get messed up for it. */
860 chain
= targetm
.calls
.static_chain (current_function_decl
, true);
861 if (chain
&& REG_P (chain
))
862 emit_clobber (chain
);
864 /* Now put in the code to restore the frame pointer, and argument
865 pointer, if needed. */
866 #ifdef HAVE_nonlocal_goto
867 if (! HAVE_nonlocal_goto
)
870 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
871 /* This might change the hard frame pointer in ways that aren't
872 apparent to early optimization passes, so force a clobber. */
873 emit_clobber (hard_frame_pointer_rtx
);
876 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
877 if (fixed_regs
[ARG_POINTER_REGNUM
])
879 #ifdef ELIMINABLE_REGS
881 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
883 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
884 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
885 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
888 if (i
== ARRAY_SIZE (elim_regs
))
891 /* Now restore our arg pointer from the address at which it
892 was saved in our stack frame. */
893 emit_move_insn (crtl
->args
.internal_arg_pointer
,
894 copy_to_reg (get_arg_pointer_save_area ()));
899 #ifdef HAVE_builtin_setjmp_receiver
900 if (HAVE_builtin_setjmp_receiver
)
901 emit_insn (gen_builtin_setjmp_receiver (receiver_label
));
904 #ifdef HAVE_nonlocal_goto_receiver
905 if (HAVE_nonlocal_goto_receiver
)
906 emit_insn (gen_nonlocal_goto_receiver ());
911 /* We must not allow the code we just generated to be reordered by
912 scheduling. Specifically, the update of the frame pointer must
913 happen immediately, not later. */
914 emit_insn (gen_blockage ());
917 /* __builtin_longjmp is passed a pointer to an array of five words (not
918 all will be used on all machines). It operates similarly to the C
919 library function of the same name, but is more efficient. Much of
920 the code below is copied from the handling of non-local gotos. */
923 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
925 rtx fp
, lab
, stack
, insn
, last
;
926 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
928 /* DRAP is needed for stack realign if longjmp is expanded to current
930 if (SUPPORTS_STACK_ALIGNMENT
)
931 crtl
->need_drap
= true;
933 if (setjmp_alias_set
== -1)
934 setjmp_alias_set
= new_alias_set ();
936 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
938 buf_addr
= force_reg (Pmode
, buf_addr
);
940 /* We require that the user must pass a second argument of 1, because
941 that is what builtin_setjmp will return. */
942 gcc_assert (value
== const1_rtx
);
944 last
= get_last_insn ();
945 #ifdef HAVE_builtin_longjmp
946 if (HAVE_builtin_longjmp
)
947 emit_insn (gen_builtin_longjmp (buf_addr
));
951 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
952 lab
= gen_rtx_MEM (Pmode
, plus_constant (buf_addr
,
953 GET_MODE_SIZE (Pmode
)));
955 stack
= gen_rtx_MEM (sa_mode
, plus_constant (buf_addr
,
956 2 * GET_MODE_SIZE (Pmode
)));
957 set_mem_alias_set (fp
, setjmp_alias_set
);
958 set_mem_alias_set (lab
, setjmp_alias_set
);
959 set_mem_alias_set (stack
, setjmp_alias_set
);
961 /* Pick up FP, label, and SP from the block and jump. This code is
962 from expand_goto in stmt.c; see there for detailed comments. */
963 #ifdef HAVE_nonlocal_goto
964 if (HAVE_nonlocal_goto
)
965 /* We have to pass a value to the nonlocal_goto pattern that will
966 get copied into the static_chain pointer, but it does not matter
967 what that value is, because builtin_setjmp does not use it. */
968 emit_insn (gen_nonlocal_goto (value
, lab
, stack
, fp
));
972 lab
= copy_to_reg (lab
);
974 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
975 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
977 emit_move_insn (hard_frame_pointer_rtx
, fp
);
978 emit_stack_restore (SAVE_NONLOCAL
, stack
);
980 emit_use (hard_frame_pointer_rtx
);
981 emit_use (stack_pointer_rtx
);
982 emit_indirect_jump (lab
);
986 /* Search backwards and mark the jump insn as a non-local goto.
987 Note that this precludes the use of __builtin_longjmp to a
988 __builtin_setjmp target in the same function. However, we've
989 already cautioned the user that these functions are for
990 internal exception handling use only. */
991 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
993 gcc_assert (insn
!= last
);
997 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1000 else if (CALL_P (insn
))
1005 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1006 and the address of the save area. */
1009 expand_builtin_nonlocal_goto (tree exp
)
1011 tree t_label
, t_save_area
;
1012 rtx r_label
, r_save_area
, r_fp
, r_sp
, insn
;
1014 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
1017 t_label
= CALL_EXPR_ARG (exp
, 0);
1018 t_save_area
= CALL_EXPR_ARG (exp
, 1);
1020 r_label
= expand_normal (t_label
);
1021 r_label
= convert_memory_address (Pmode
, r_label
);
1022 r_save_area
= expand_normal (t_save_area
);
1023 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
1024 /* Copy the address of the save location to a register just in case it was
1025 based on the frame pointer. */
1026 r_save_area
= copy_to_reg (r_save_area
);
1027 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
1028 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
1029 plus_constant (r_save_area
, GET_MODE_SIZE (Pmode
)));
1031 crtl
->has_nonlocal_goto
= 1;
1033 #ifdef HAVE_nonlocal_goto
1034 /* ??? We no longer need to pass the static chain value, afaik. */
1035 if (HAVE_nonlocal_goto
)
1036 emit_insn (gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
1040 r_label
= copy_to_reg (r_label
);
1042 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1043 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1045 /* Restore frame pointer for containing function. */
1046 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
1047 emit_stack_restore (SAVE_NONLOCAL
, r_sp
);
1049 /* USE of hard_frame_pointer_rtx added for consistency;
1050 not clear if really needed. */
1051 emit_use (hard_frame_pointer_rtx
);
1052 emit_use (stack_pointer_rtx
);
1054 /* If the architecture is using a GP register, we must
1055 conservatively assume that the target function makes use of it.
1056 The prologue of functions with nonlocal gotos must therefore
1057 initialize the GP register to the appropriate value, and we
1058 must then make sure that this value is live at the point
1059 of the jump. (Note that this doesn't necessarily apply
1060 to targets with a nonlocal_goto pattern; they are free
1061 to implement it in their own way. Note also that this is
1062 a no-op if the GP register is a global invariant.) */
1063 if ((unsigned) PIC_OFFSET_TABLE_REGNUM
!= INVALID_REGNUM
1064 && fixed_regs
[PIC_OFFSET_TABLE_REGNUM
])
1065 emit_use (pic_offset_table_rtx
);
1067 emit_indirect_jump (r_label
);
1070 /* Search backwards to the jump insn and mark it as a
1072 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1076 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1079 else if (CALL_P (insn
))
1086 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1087 (not all will be used on all machines) that was passed to __builtin_setjmp.
1088 It updates the stack pointer in that block to correspond to the current
1092 expand_builtin_update_setjmp_buf (rtx buf_addr
)
1094 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
1096 = gen_rtx_MEM (sa_mode
,
1099 plus_constant (buf_addr
, 2 * GET_MODE_SIZE (Pmode
))));
1101 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
1104 /* Expand a call to __builtin_prefetch. For a target that does not support
1105 data prefetch, evaluate the memory address argument in case it has side
1109 expand_builtin_prefetch (tree exp
)
1111 tree arg0
, arg1
, arg2
;
1115 if (!validate_arglist (exp
, POINTER_TYPE
, 0))
1118 arg0
= CALL_EXPR_ARG (exp
, 0);
1120 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1121 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1123 nargs
= call_expr_nargs (exp
);
1125 arg1
= CALL_EXPR_ARG (exp
, 1);
1127 arg1
= integer_zero_node
;
1129 arg2
= CALL_EXPR_ARG (exp
, 2);
1131 arg2
= integer_three_node
;
1133 /* Argument 0 is an address. */
1134 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
1136 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1137 if (TREE_CODE (arg1
) != INTEGER_CST
)
1139 error ("second argument to %<__builtin_prefetch%> must be a constant");
1140 arg1
= integer_zero_node
;
1142 op1
= expand_normal (arg1
);
1143 /* Argument 1 must be either zero or one. */
1144 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
1146 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1151 /* Argument 2 (locality) must be a compile-time constant int. */
1152 if (TREE_CODE (arg2
) != INTEGER_CST
)
1154 error ("third argument to %<__builtin_prefetch%> must be a constant");
1155 arg2
= integer_zero_node
;
1157 op2
= expand_normal (arg2
);
1158 /* Argument 2 must be 0, 1, 2, or 3. */
1159 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
1161 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1165 #ifdef HAVE_prefetch
1168 struct expand_operand ops
[3];
1170 create_address_operand (&ops
[0], op0
);
1171 create_integer_operand (&ops
[1], INTVAL (op1
));
1172 create_integer_operand (&ops
[2], INTVAL (op2
));
1173 if (maybe_expand_insn (CODE_FOR_prefetch
, 3, ops
))
1178 /* Don't do anything with direct references to volatile memory, but
1179 generate code to handle other side effects. */
1180 if (!MEM_P (op0
) && side_effects_p (op0
))
1184 /* Get a MEM rtx for expression EXP which is the address of an operand
1185 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1186 the maximum length of the block of memory that might be accessed or
1190 get_memory_rtx (tree exp
, tree len
)
1192 tree orig_exp
= exp
;
1196 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1197 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1198 if (TREE_CODE (exp
) == SAVE_EXPR
&& !SAVE_EXPR_RESOLVED_P (exp
))
1199 exp
= TREE_OPERAND (exp
, 0);
1201 addr
= expand_expr (orig_exp
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
1202 mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
1204 /* Get an expression we can use to find the attributes to assign to MEM.
1205 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
1206 we can. First remove any nops. */
1207 while (CONVERT_EXPR_P (exp
)
1208 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
1209 exp
= TREE_OPERAND (exp
, 0);
1212 if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
1213 && TREE_CODE (TREE_OPERAND (exp
, 0)) == ADDR_EXPR
1214 && host_integerp (TREE_OPERAND (exp
, 1), 0)
1215 && (off
= tree_low_cst (TREE_OPERAND (exp
, 1), 0)) > 0)
1216 exp
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1217 else if (TREE_CODE (exp
) == ADDR_EXPR
)
1218 exp
= TREE_OPERAND (exp
, 0);
1219 else if (POINTER_TYPE_P (TREE_TYPE (exp
)))
1220 exp
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (exp
)), exp
);
1224 /* Honor attributes derived from exp, except for the alias set
1225 (as builtin stringops may alias with anything) and the size
1226 (as stringops may access multiple array elements). */
1229 set_mem_attributes (mem
, exp
, 0);
1232 mem
= adjust_automodify_address_nv (mem
, BLKmode
, NULL
, off
);
1234 /* Allow the string and memory builtins to overflow from one
1235 field into another, see http://gcc.gnu.org/PR23561.
1236 Thus avoid COMPONENT_REFs in MEM_EXPR unless we know the whole
1237 memory accessed by the string or memory builtin will fit
1238 within the field. */
1239 if (MEM_EXPR (mem
) && TREE_CODE (MEM_EXPR (mem
)) == COMPONENT_REF
)
1241 tree mem_expr
= MEM_EXPR (mem
);
1242 HOST_WIDE_INT offset
= -1, length
= -1;
1245 while (TREE_CODE (inner
) == ARRAY_REF
1246 || CONVERT_EXPR_P (inner
)
1247 || TREE_CODE (inner
) == VIEW_CONVERT_EXPR
1248 || TREE_CODE (inner
) == SAVE_EXPR
)
1249 inner
= TREE_OPERAND (inner
, 0);
1251 gcc_assert (TREE_CODE (inner
) == COMPONENT_REF
);
1253 if (MEM_OFFSET_KNOWN_P (mem
))
1254 offset
= MEM_OFFSET (mem
);
1256 if (offset
>= 0 && len
&& host_integerp (len
, 0))
1257 length
= tree_low_cst (len
, 0);
1259 while (TREE_CODE (inner
) == COMPONENT_REF
)
1261 tree field
= TREE_OPERAND (inner
, 1);
1262 gcc_assert (TREE_CODE (mem_expr
) == COMPONENT_REF
);
1263 gcc_assert (field
== TREE_OPERAND (mem_expr
, 1));
1265 /* Bitfields are generally not byte-addressable. */
1266 gcc_assert (!DECL_BIT_FIELD (field
)
1267 || ((tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1)
1268 % BITS_PER_UNIT
) == 0
1269 && host_integerp (DECL_SIZE (field
), 0)
1270 && (TREE_INT_CST_LOW (DECL_SIZE (field
))
1271 % BITS_PER_UNIT
) == 0));
1273 /* If we can prove that the memory starting at XEXP (mem, 0) and
1274 ending at XEXP (mem, 0) + LENGTH will fit into this field, we
1275 can keep the COMPONENT_REF in MEM_EXPR. But be careful with
1276 fields without DECL_SIZE_UNIT like flexible array members. */
1278 && DECL_SIZE_UNIT (field
)
1279 && host_integerp (DECL_SIZE_UNIT (field
), 0))
1282 = TREE_INT_CST_LOW (DECL_SIZE_UNIT (field
));
1285 && offset
+ length
<= size
)
1290 && host_integerp (DECL_FIELD_OFFSET (field
), 0))
1291 offset
+= TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field
))
1292 + tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1)
1300 mem_expr
= TREE_OPERAND (mem_expr
, 0);
1301 inner
= TREE_OPERAND (inner
, 0);
1304 if (mem_expr
== NULL
)
1306 if (mem_expr
!= MEM_EXPR (mem
))
1308 set_mem_expr (mem
, mem_expr
);
1310 set_mem_offset (mem
, offset
);
1312 clear_mem_offset (mem
);
1315 set_mem_alias_set (mem
, 0);
1316 clear_mem_size (mem
);
1322 /* Built-in functions to perform an untyped call and return. */
1324 #define apply_args_mode \
1325 (this_target_builtins->x_apply_args_mode)
1326 #define apply_result_mode \
1327 (this_target_builtins->x_apply_result_mode)
1329 /* Return the size required for the block returned by __builtin_apply_args,
1330 and initialize apply_args_mode. */
1333 apply_args_size (void)
1335 static int size
= -1;
1338 enum machine_mode mode
;
1340 /* The values computed by this function never change. */
1343 /* The first value is the incoming arg-pointer. */
1344 size
= GET_MODE_SIZE (Pmode
);
1346 /* The second value is the structure value address unless this is
1347 passed as an "invisible" first argument. */
1348 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1349 size
+= GET_MODE_SIZE (Pmode
);
1351 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1352 if (FUNCTION_ARG_REGNO_P (regno
))
1354 mode
= targetm
.calls
.get_raw_arg_mode (regno
);
1356 gcc_assert (mode
!= VOIDmode
);
1358 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1359 if (size
% align
!= 0)
1360 size
= CEIL (size
, align
) * align
;
1361 size
+= GET_MODE_SIZE (mode
);
1362 apply_args_mode
[regno
] = mode
;
1366 apply_args_mode
[regno
] = VOIDmode
;
1372 /* Return the size required for the block returned by __builtin_apply,
1373 and initialize apply_result_mode. */
1376 apply_result_size (void)
1378 static int size
= -1;
1380 enum machine_mode mode
;
1382 /* The values computed by this function never change. */
1387 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1388 if (targetm
.calls
.function_value_regno_p (regno
))
1390 mode
= targetm
.calls
.get_raw_result_mode (regno
);
1392 gcc_assert (mode
!= VOIDmode
);
1394 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1395 if (size
% align
!= 0)
1396 size
= CEIL (size
, align
) * align
;
1397 size
+= GET_MODE_SIZE (mode
);
1398 apply_result_mode
[regno
] = mode
;
1401 apply_result_mode
[regno
] = VOIDmode
;
1403 /* Allow targets that use untyped_call and untyped_return to override
1404 the size so that machine-specific information can be stored here. */
1405 #ifdef APPLY_RESULT_SIZE
1406 size
= APPLY_RESULT_SIZE
;
1412 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1413 /* Create a vector describing the result block RESULT. If SAVEP is true,
1414 the result block is used to save the values; otherwise it is used to
1415 restore the values. */
1418 result_vector (int savep
, rtx result
)
1420 int regno
, size
, align
, nelts
;
1421 enum machine_mode mode
;
1423 rtx
*savevec
= XALLOCAVEC (rtx
, FIRST_PSEUDO_REGISTER
);
1426 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1427 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1429 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1430 if (size
% align
!= 0)
1431 size
= CEIL (size
, align
) * align
;
1432 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
1433 mem
= adjust_address (result
, mode
, size
);
1434 savevec
[nelts
++] = (savep
1435 ? gen_rtx_SET (VOIDmode
, mem
, reg
)
1436 : gen_rtx_SET (VOIDmode
, reg
, mem
));
1437 size
+= GET_MODE_SIZE (mode
);
1439 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
1441 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1443 /* Save the state required to perform an untyped call with the same
1444 arguments as were passed to the current function. */
1447 expand_builtin_apply_args_1 (void)
1450 int size
, align
, regno
;
1451 enum machine_mode mode
;
1452 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
1454 /* Create a block where the arg-pointer, structure value address,
1455 and argument registers can be saved. */
1456 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
1458 /* Walk past the arg-pointer and structure value address. */
1459 size
= GET_MODE_SIZE (Pmode
);
1460 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1461 size
+= GET_MODE_SIZE (Pmode
);
1463 /* Save each register used in calling a function to the block. */
1464 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1465 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1467 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1468 if (size
% align
!= 0)
1469 size
= CEIL (size
, align
) * align
;
1471 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1473 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
1474 size
+= GET_MODE_SIZE (mode
);
1477 /* Save the arg pointer to the block. */
1478 tem
= copy_to_reg (crtl
->args
.internal_arg_pointer
);
1479 #ifdef STACK_GROWS_DOWNWARD
1480 /* We need the pointer as the caller actually passed them to us, not
1481 as we might have pretended they were passed. Make sure it's a valid
1482 operand, as emit_move_insn isn't expected to handle a PLUS. */
1484 = force_operand (plus_constant (tem
, crtl
->args
.pretend_args_size
),
1487 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
1489 size
= GET_MODE_SIZE (Pmode
);
1491 /* Save the structure value address unless this is passed as an
1492 "invisible" first argument. */
1493 if (struct_incoming_value
)
1495 emit_move_insn (adjust_address (registers
, Pmode
, size
),
1496 copy_to_reg (struct_incoming_value
));
1497 size
+= GET_MODE_SIZE (Pmode
);
1500 /* Return the address of the block. */
1501 return copy_addr_to_reg (XEXP (registers
, 0));
1504 /* __builtin_apply_args returns block of memory allocated on
1505 the stack into which is stored the arg pointer, structure
1506 value address, static chain, and all the registers that might
1507 possibly be used in performing a function call. The code is
1508 moved to the start of the function so the incoming values are
1512 expand_builtin_apply_args (void)
1514 /* Don't do __builtin_apply_args more than once in a function.
1515 Save the result of the first call and reuse it. */
1516 if (apply_args_value
!= 0)
1517 return apply_args_value
;
1519 /* When this function is called, it means that registers must be
1520 saved on entry to this function. So we migrate the
1521 call to the first insn of this function. */
1526 temp
= expand_builtin_apply_args_1 ();
1530 apply_args_value
= temp
;
1532 /* Put the insns after the NOTE that starts the function.
1533 If this is inside a start_sequence, make the outer-level insn
1534 chain current, so the code is placed at the start of the
1535 function. If internal_arg_pointer is a non-virtual pseudo,
1536 it needs to be placed after the function that initializes
1538 push_topmost_sequence ();
1539 if (REG_P (crtl
->args
.internal_arg_pointer
)
1540 && REGNO (crtl
->args
.internal_arg_pointer
) > LAST_VIRTUAL_REGISTER
)
1541 emit_insn_before (seq
, parm_birth_insn
);
1543 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
1544 pop_topmost_sequence ();
1549 /* Perform an untyped call and save the state required to perform an
1550 untyped return of whatever value was returned by the given function. */
1553 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
1555 int size
, align
, regno
;
1556 enum machine_mode mode
;
1557 rtx incoming_args
, result
, reg
, dest
, src
, call_insn
;
1558 rtx old_stack_level
= 0;
1559 rtx call_fusage
= 0;
1560 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
1562 arguments
= convert_memory_address (Pmode
, arguments
);
1564 /* Create a block where the return registers can be saved. */
1565 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
1567 /* Fetch the arg pointer from the ARGUMENTS block. */
1568 incoming_args
= gen_reg_rtx (Pmode
);
1569 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
1570 #ifndef STACK_GROWS_DOWNWARD
1571 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
1572 incoming_args
, 0, OPTAB_LIB_WIDEN
);
1575 /* Push a new argument block and copy the arguments. Do not allow
1576 the (potential) memcpy call below to interfere with our stack
1578 do_pending_stack_adjust ();
1581 /* Save the stack with nonlocal if available. */
1582 #ifdef HAVE_save_stack_nonlocal
1583 if (HAVE_save_stack_nonlocal
)
1584 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
);
1587 emit_stack_save (SAVE_BLOCK
, &old_stack_level
);
1589 /* Allocate a block of memory onto the stack and copy the memory
1590 arguments to the outgoing arguments address. We can pass TRUE
1591 as the 4th argument because we just saved the stack pointer
1592 and will restore it right after the call. */
1593 allocate_dynamic_stack_space (argsize
, 0, BIGGEST_ALIGNMENT
, true);
1595 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1596 may have already set current_function_calls_alloca to true.
1597 current_function_calls_alloca won't be set if argsize is zero,
1598 so we have to guarantee need_drap is true here. */
1599 if (SUPPORTS_STACK_ALIGNMENT
)
1600 crtl
->need_drap
= true;
1602 dest
= virtual_outgoing_args_rtx
;
1603 #ifndef STACK_GROWS_DOWNWARD
1604 if (CONST_INT_P (argsize
))
1605 dest
= plus_constant (dest
, -INTVAL (argsize
));
1607 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
1609 dest
= gen_rtx_MEM (BLKmode
, dest
);
1610 set_mem_align (dest
, PARM_BOUNDARY
);
1611 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
1612 set_mem_align (src
, PARM_BOUNDARY
);
1613 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
1615 /* Refer to the argument block. */
1617 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
1618 set_mem_align (arguments
, PARM_BOUNDARY
);
1620 /* Walk past the arg-pointer and structure value address. */
1621 size
= GET_MODE_SIZE (Pmode
);
1623 size
+= GET_MODE_SIZE (Pmode
);
1625 /* Restore each of the registers previously saved. Make USE insns
1626 for each of these registers for use in making the call. */
1627 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1628 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1630 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1631 if (size
% align
!= 0)
1632 size
= CEIL (size
, align
) * align
;
1633 reg
= gen_rtx_REG (mode
, regno
);
1634 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
1635 use_reg (&call_fusage
, reg
);
1636 size
+= GET_MODE_SIZE (mode
);
1639 /* Restore the structure value address unless this is passed as an
1640 "invisible" first argument. */
1641 size
= GET_MODE_SIZE (Pmode
);
1644 rtx value
= gen_reg_rtx (Pmode
);
1645 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
1646 emit_move_insn (struct_value
, value
);
1647 if (REG_P (struct_value
))
1648 use_reg (&call_fusage
, struct_value
);
1649 size
+= GET_MODE_SIZE (Pmode
);
1652 /* All arguments and registers used for the call are set up by now! */
1653 function
= prepare_call_address (NULL
, function
, NULL
, &call_fusage
, 0, 0);
1655 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1656 and we don't want to load it into a register as an optimization,
1657 because prepare_call_address already did it if it should be done. */
1658 if (GET_CODE (function
) != SYMBOL_REF
)
1659 function
= memory_address (FUNCTION_MODE
, function
);
1661 /* Generate the actual call instruction and save the return value. */
1662 #ifdef HAVE_untyped_call
1663 if (HAVE_untyped_call
)
1664 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE
, function
),
1665 result
, result_vector (1, result
)));
1668 #ifdef HAVE_call_value
1669 if (HAVE_call_value
)
1673 /* Locate the unique return register. It is not possible to
1674 express a call that sets more than one return register using
1675 call_value; use untyped_call for that. In fact, untyped_call
1676 only needs to save the return registers in the given block. */
1677 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1678 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1680 gcc_assert (!valreg
); /* HAVE_untyped_call required. */
1682 valreg
= gen_rtx_REG (mode
, regno
);
1685 emit_call_insn (GEN_CALL_VALUE (valreg
,
1686 gen_rtx_MEM (FUNCTION_MODE
, function
),
1687 const0_rtx
, NULL_RTX
, const0_rtx
));
1689 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
1695 /* Find the CALL insn we just emitted, and attach the register usage
1697 call_insn
= last_call_insn ();
1698 add_function_usage_to (call_insn
, call_fusage
);
1700 /* Restore the stack. */
1701 #ifdef HAVE_save_stack_nonlocal
1702 if (HAVE_save_stack_nonlocal
)
1703 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
);
1706 emit_stack_restore (SAVE_BLOCK
, old_stack_level
);
1707 fixup_args_size_notes (call_insn
, get_last_insn(), 0);
1711 /* Return the address of the result block. */
1712 result
= copy_addr_to_reg (XEXP (result
, 0));
1713 return convert_memory_address (ptr_mode
, result
);
1716 /* Perform an untyped return. */
1719 expand_builtin_return (rtx result
)
1721 int size
, align
, regno
;
1722 enum machine_mode mode
;
1724 rtx call_fusage
= 0;
1726 result
= convert_memory_address (Pmode
, result
);
1728 apply_result_size ();
1729 result
= gen_rtx_MEM (BLKmode
, result
);
1731 #ifdef HAVE_untyped_return
1732 if (HAVE_untyped_return
)
1734 emit_jump_insn (gen_untyped_return (result
, result_vector (0, result
)));
1740 /* Restore the return value and note that each value is used. */
1742 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1743 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1745 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1746 if (size
% align
!= 0)
1747 size
= CEIL (size
, align
) * align
;
1748 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1749 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
1751 push_to_sequence (call_fusage
);
1753 call_fusage
= get_insns ();
1755 size
+= GET_MODE_SIZE (mode
);
1758 /* Put the USE insns before the return. */
1759 emit_insn (call_fusage
);
1761 /* Return whatever values was restored by jumping directly to the end
1763 expand_naked_return ();
1766 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1768 static enum type_class
1769 type_to_class (tree type
)
1771 switch (TREE_CODE (type
))
1773 case VOID_TYPE
: return void_type_class
;
1774 case INTEGER_TYPE
: return integer_type_class
;
1775 case ENUMERAL_TYPE
: return enumeral_type_class
;
1776 case BOOLEAN_TYPE
: return boolean_type_class
;
1777 case POINTER_TYPE
: return pointer_type_class
;
1778 case REFERENCE_TYPE
: return reference_type_class
;
1779 case OFFSET_TYPE
: return offset_type_class
;
1780 case REAL_TYPE
: return real_type_class
;
1781 case COMPLEX_TYPE
: return complex_type_class
;
1782 case FUNCTION_TYPE
: return function_type_class
;
1783 case METHOD_TYPE
: return method_type_class
;
1784 case RECORD_TYPE
: return record_type_class
;
1786 case QUAL_UNION_TYPE
: return union_type_class
;
1787 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
1788 ? string_type_class
: array_type_class
);
1789 case LANG_TYPE
: return lang_type_class
;
1790 default: return no_type_class
;
1794 /* Expand a call EXP to __builtin_classify_type. */
1797 expand_builtin_classify_type (tree exp
)
1799 if (call_expr_nargs (exp
))
1800 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))));
1801 return GEN_INT (no_type_class
);
1804 /* This helper macro, meant to be used in mathfn_built_in below,
1805 determines which among a set of three builtin math functions is
1806 appropriate for a given type mode. The `F' and `L' cases are
1807 automatically generated from the `double' case. */
1808 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1809 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1810 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1811 fcodel = BUILT_IN_MATHFN##L ; break;
1812 /* Similar to above, but appends _R after any F/L suffix. */
1813 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1814 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1815 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1816 fcodel = BUILT_IN_MATHFN##L_R ; break;
1818 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1819 if available. If IMPLICIT is true use the implicit builtin declaration,
1820 otherwise use the explicit declaration. If we can't do the conversion,
1824 mathfn_built_in_1 (tree type
, enum built_in_function fn
, bool implicit_p
)
1826 enum built_in_function fcode
, fcodef
, fcodel
, fcode2
;
1830 CASE_MATHFN (BUILT_IN_ACOS
)
1831 CASE_MATHFN (BUILT_IN_ACOSH
)
1832 CASE_MATHFN (BUILT_IN_ASIN
)
1833 CASE_MATHFN (BUILT_IN_ASINH
)
1834 CASE_MATHFN (BUILT_IN_ATAN
)
1835 CASE_MATHFN (BUILT_IN_ATAN2
)
1836 CASE_MATHFN (BUILT_IN_ATANH
)
1837 CASE_MATHFN (BUILT_IN_CBRT
)
1838 CASE_MATHFN (BUILT_IN_CEIL
)
1839 CASE_MATHFN (BUILT_IN_CEXPI
)
1840 CASE_MATHFN (BUILT_IN_COPYSIGN
)
1841 CASE_MATHFN (BUILT_IN_COS
)
1842 CASE_MATHFN (BUILT_IN_COSH
)
1843 CASE_MATHFN (BUILT_IN_DREM
)
1844 CASE_MATHFN (BUILT_IN_ERF
)
1845 CASE_MATHFN (BUILT_IN_ERFC
)
1846 CASE_MATHFN (BUILT_IN_EXP
)
1847 CASE_MATHFN (BUILT_IN_EXP10
)
1848 CASE_MATHFN (BUILT_IN_EXP2
)
1849 CASE_MATHFN (BUILT_IN_EXPM1
)
1850 CASE_MATHFN (BUILT_IN_FABS
)
1851 CASE_MATHFN (BUILT_IN_FDIM
)
1852 CASE_MATHFN (BUILT_IN_FLOOR
)
1853 CASE_MATHFN (BUILT_IN_FMA
)
1854 CASE_MATHFN (BUILT_IN_FMAX
)
1855 CASE_MATHFN (BUILT_IN_FMIN
)
1856 CASE_MATHFN (BUILT_IN_FMOD
)
1857 CASE_MATHFN (BUILT_IN_FREXP
)
1858 CASE_MATHFN (BUILT_IN_GAMMA
)
1859 CASE_MATHFN_REENT (BUILT_IN_GAMMA
) /* GAMMA_R */
1860 CASE_MATHFN (BUILT_IN_HUGE_VAL
)
1861 CASE_MATHFN (BUILT_IN_HYPOT
)
1862 CASE_MATHFN (BUILT_IN_ILOGB
)
1863 CASE_MATHFN (BUILT_IN_ICEIL
)
1864 CASE_MATHFN (BUILT_IN_IFLOOR
)
1865 CASE_MATHFN (BUILT_IN_INF
)
1866 CASE_MATHFN (BUILT_IN_IRINT
)
1867 CASE_MATHFN (BUILT_IN_IROUND
)
1868 CASE_MATHFN (BUILT_IN_ISINF
)
1869 CASE_MATHFN (BUILT_IN_J0
)
1870 CASE_MATHFN (BUILT_IN_J1
)
1871 CASE_MATHFN (BUILT_IN_JN
)
1872 CASE_MATHFN (BUILT_IN_LCEIL
)
1873 CASE_MATHFN (BUILT_IN_LDEXP
)
1874 CASE_MATHFN (BUILT_IN_LFLOOR
)
1875 CASE_MATHFN (BUILT_IN_LGAMMA
)
1876 CASE_MATHFN_REENT (BUILT_IN_LGAMMA
) /* LGAMMA_R */
1877 CASE_MATHFN (BUILT_IN_LLCEIL
)
1878 CASE_MATHFN (BUILT_IN_LLFLOOR
)
1879 CASE_MATHFN (BUILT_IN_LLRINT
)
1880 CASE_MATHFN (BUILT_IN_LLROUND
)
1881 CASE_MATHFN (BUILT_IN_LOG
)
1882 CASE_MATHFN (BUILT_IN_LOG10
)
1883 CASE_MATHFN (BUILT_IN_LOG1P
)
1884 CASE_MATHFN (BUILT_IN_LOG2
)
1885 CASE_MATHFN (BUILT_IN_LOGB
)
1886 CASE_MATHFN (BUILT_IN_LRINT
)
1887 CASE_MATHFN (BUILT_IN_LROUND
)
1888 CASE_MATHFN (BUILT_IN_MODF
)
1889 CASE_MATHFN (BUILT_IN_NAN
)
1890 CASE_MATHFN (BUILT_IN_NANS
)
1891 CASE_MATHFN (BUILT_IN_NEARBYINT
)
1892 CASE_MATHFN (BUILT_IN_NEXTAFTER
)
1893 CASE_MATHFN (BUILT_IN_NEXTTOWARD
)
1894 CASE_MATHFN (BUILT_IN_POW
)
1895 CASE_MATHFN (BUILT_IN_POWI
)
1896 CASE_MATHFN (BUILT_IN_POW10
)
1897 CASE_MATHFN (BUILT_IN_REMAINDER
)
1898 CASE_MATHFN (BUILT_IN_REMQUO
)
1899 CASE_MATHFN (BUILT_IN_RINT
)
1900 CASE_MATHFN (BUILT_IN_ROUND
)
1901 CASE_MATHFN (BUILT_IN_SCALB
)
1902 CASE_MATHFN (BUILT_IN_SCALBLN
)
1903 CASE_MATHFN (BUILT_IN_SCALBN
)
1904 CASE_MATHFN (BUILT_IN_SIGNBIT
)
1905 CASE_MATHFN (BUILT_IN_SIGNIFICAND
)
1906 CASE_MATHFN (BUILT_IN_SIN
)
1907 CASE_MATHFN (BUILT_IN_SINCOS
)
1908 CASE_MATHFN (BUILT_IN_SINH
)
1909 CASE_MATHFN (BUILT_IN_SQRT
)
1910 CASE_MATHFN (BUILT_IN_TAN
)
1911 CASE_MATHFN (BUILT_IN_TANH
)
1912 CASE_MATHFN (BUILT_IN_TGAMMA
)
1913 CASE_MATHFN (BUILT_IN_TRUNC
)
1914 CASE_MATHFN (BUILT_IN_Y0
)
1915 CASE_MATHFN (BUILT_IN_Y1
)
1916 CASE_MATHFN (BUILT_IN_YN
)
1922 if (TYPE_MAIN_VARIANT (type
) == double_type_node
)
1924 else if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1926 else if (TYPE_MAIN_VARIANT (type
) == long_double_type_node
)
1931 if (implicit_p
&& !builtin_decl_implicit_p (fcode2
))
1934 return builtin_decl_explicit (fcode2
);
1937 /* Like mathfn_built_in_1(), but always use the implicit array. */
1940 mathfn_built_in (tree type
, enum built_in_function fn
)
1942 return mathfn_built_in_1 (type
, fn
, /*implicit=*/ 1);
1945 /* If errno must be maintained, expand the RTL to check if the result,
1946 TARGET, of a built-in function call, EXP, is NaN, and if so set
1950 expand_errno_check (tree exp
, rtx target
)
1952 rtx lab
= gen_label_rtx ();
1954 /* Test the result; if it is NaN, set errno=EDOM because
1955 the argument was not in the domain. */
1956 do_compare_rtx_and_jump (target
, target
, EQ
, 0, GET_MODE (target
),
1957 NULL_RTX
, NULL_RTX
, lab
,
1958 /* The jump is very likely. */
1959 REG_BR_PROB_BASE
- (REG_BR_PROB_BASE
/ 2000 - 1));
1962 /* If this built-in doesn't throw an exception, set errno directly. */
1963 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp
), 0)))
1965 #ifdef GEN_ERRNO_RTX
1966 rtx errno_rtx
= GEN_ERRNO_RTX
;
1969 = gen_rtx_MEM (word_mode
, gen_rtx_SYMBOL_REF (Pmode
, "errno"));
1971 emit_move_insn (errno_rtx
, GEN_INT (TARGET_EDOM
));
1977 /* Make sure the library call isn't expanded as a tail call. */
1978 CALL_EXPR_TAILCALL (exp
) = 0;
1980 /* We can't set errno=EDOM directly; let the library call do it.
1981 Pop the arguments right away in case the call gets deleted. */
1983 expand_call (exp
, target
, 0);
1988 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1989 Return NULL_RTX if a normal call should be emitted rather than expanding
1990 the function in-line. EXP is the expression that is a call to the builtin
1991 function; if convenient, the result should be placed in TARGET.
1992 SUBTARGET may be used as the target for computing one of EXP's operands. */
1995 expand_builtin_mathfn (tree exp
, rtx target
, rtx subtarget
)
1997 optab builtin_optab
;
1999 tree fndecl
= get_callee_fndecl (exp
);
2000 enum machine_mode mode
;
2001 bool errno_set
= false;
2004 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2007 arg
= CALL_EXPR_ARG (exp
, 0);
2009 switch (DECL_FUNCTION_CODE (fndecl
))
2011 CASE_FLT_FN (BUILT_IN_SQRT
):
2012 errno_set
= ! tree_expr_nonnegative_p (arg
);
2013 builtin_optab
= sqrt_optab
;
2015 CASE_FLT_FN (BUILT_IN_EXP
):
2016 errno_set
= true; builtin_optab
= exp_optab
; break;
2017 CASE_FLT_FN (BUILT_IN_EXP10
):
2018 CASE_FLT_FN (BUILT_IN_POW10
):
2019 errno_set
= true; builtin_optab
= exp10_optab
; break;
2020 CASE_FLT_FN (BUILT_IN_EXP2
):
2021 errno_set
= true; builtin_optab
= exp2_optab
; break;
2022 CASE_FLT_FN (BUILT_IN_EXPM1
):
2023 errno_set
= true; builtin_optab
= expm1_optab
; break;
2024 CASE_FLT_FN (BUILT_IN_LOGB
):
2025 errno_set
= true; builtin_optab
= logb_optab
; break;
2026 CASE_FLT_FN (BUILT_IN_LOG
):
2027 errno_set
= true; builtin_optab
= log_optab
; break;
2028 CASE_FLT_FN (BUILT_IN_LOG10
):
2029 errno_set
= true; builtin_optab
= log10_optab
; break;
2030 CASE_FLT_FN (BUILT_IN_LOG2
):
2031 errno_set
= true; builtin_optab
= log2_optab
; break;
2032 CASE_FLT_FN (BUILT_IN_LOG1P
):
2033 errno_set
= true; builtin_optab
= log1p_optab
; break;
2034 CASE_FLT_FN (BUILT_IN_ASIN
):
2035 builtin_optab
= asin_optab
; break;
2036 CASE_FLT_FN (BUILT_IN_ACOS
):
2037 builtin_optab
= acos_optab
; break;
2038 CASE_FLT_FN (BUILT_IN_TAN
):
2039 builtin_optab
= tan_optab
; break;
2040 CASE_FLT_FN (BUILT_IN_ATAN
):
2041 builtin_optab
= atan_optab
; break;
2042 CASE_FLT_FN (BUILT_IN_FLOOR
):
2043 builtin_optab
= floor_optab
; break;
2044 CASE_FLT_FN (BUILT_IN_CEIL
):
2045 builtin_optab
= ceil_optab
; break;
2046 CASE_FLT_FN (BUILT_IN_TRUNC
):
2047 builtin_optab
= btrunc_optab
; break;
2048 CASE_FLT_FN (BUILT_IN_ROUND
):
2049 builtin_optab
= round_optab
; break;
2050 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
2051 builtin_optab
= nearbyint_optab
;
2052 if (flag_trapping_math
)
2054 /* Else fallthrough and expand as rint. */
2055 CASE_FLT_FN (BUILT_IN_RINT
):
2056 builtin_optab
= rint_optab
; break;
2057 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
2058 builtin_optab
= significand_optab
; break;
2063 /* Make a suitable register to place result in. */
2064 mode
= TYPE_MODE (TREE_TYPE (exp
));
2066 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2069 /* Before working hard, check whether the instruction is available. */
2070 if (optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
2071 && (!errno_set
|| !optimize_insn_for_size_p ()))
2073 target
= gen_reg_rtx (mode
);
2075 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2076 need to expand the argument again. This way, we will not perform
2077 side-effects more the once. */
2078 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2080 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2084 /* Compute into TARGET.
2085 Set TARGET to wherever the result comes back. */
2086 target
= expand_unop (mode
, builtin_optab
, op0
, target
, 0);
2091 expand_errno_check (exp
, target
);
2093 /* Output the entire sequence. */
2094 insns
= get_insns ();
2100 /* If we were unable to expand via the builtin, stop the sequence
2101 (without outputting the insns) and call to the library function
2102 with the stabilized argument list. */
2106 return expand_call (exp
, target
, target
== const0_rtx
);
2109 /* Expand a call to the builtin binary math functions (pow and atan2).
2110 Return NULL_RTX if a normal call should be emitted rather than expanding the
2111 function in-line. EXP is the expression that is a call to the builtin
2112 function; if convenient, the result should be placed in TARGET.
2113 SUBTARGET may be used as the target for computing one of EXP's
2117 expand_builtin_mathfn_2 (tree exp
, rtx target
, rtx subtarget
)
2119 optab builtin_optab
;
2120 rtx op0
, op1
, insns
;
2121 int op1_type
= REAL_TYPE
;
2122 tree fndecl
= get_callee_fndecl (exp
);
2124 enum machine_mode mode
;
2125 bool errno_set
= true;
2127 switch (DECL_FUNCTION_CODE (fndecl
))
2129 CASE_FLT_FN (BUILT_IN_SCALBN
):
2130 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2131 CASE_FLT_FN (BUILT_IN_LDEXP
):
2132 op1_type
= INTEGER_TYPE
;
2137 if (!validate_arglist (exp
, REAL_TYPE
, op1_type
, VOID_TYPE
))
2140 arg0
= CALL_EXPR_ARG (exp
, 0);
2141 arg1
= CALL_EXPR_ARG (exp
, 1);
2143 switch (DECL_FUNCTION_CODE (fndecl
))
2145 CASE_FLT_FN (BUILT_IN_POW
):
2146 builtin_optab
= pow_optab
; break;
2147 CASE_FLT_FN (BUILT_IN_ATAN2
):
2148 builtin_optab
= atan2_optab
; break;
2149 CASE_FLT_FN (BUILT_IN_SCALB
):
2150 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2152 builtin_optab
= scalb_optab
; break;
2153 CASE_FLT_FN (BUILT_IN_SCALBN
):
2154 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2155 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2157 /* Fall through... */
2158 CASE_FLT_FN (BUILT_IN_LDEXP
):
2159 builtin_optab
= ldexp_optab
; break;
2160 CASE_FLT_FN (BUILT_IN_FMOD
):
2161 builtin_optab
= fmod_optab
; break;
2162 CASE_FLT_FN (BUILT_IN_REMAINDER
):
2163 CASE_FLT_FN (BUILT_IN_DREM
):
2164 builtin_optab
= remainder_optab
; break;
2169 /* Make a suitable register to place result in. */
2170 mode
= TYPE_MODE (TREE_TYPE (exp
));
2172 /* Before working hard, check whether the instruction is available. */
2173 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2176 target
= gen_reg_rtx (mode
);
2178 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2181 if (errno_set
&& optimize_insn_for_size_p ())
2184 /* Always stabilize the argument list. */
2185 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2186 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2188 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2189 op1
= expand_normal (arg1
);
2193 /* Compute into TARGET.
2194 Set TARGET to wherever the result comes back. */
2195 target
= expand_binop (mode
, builtin_optab
, op0
, op1
,
2196 target
, 0, OPTAB_DIRECT
);
2198 /* If we were unable to expand via the builtin, stop the sequence
2199 (without outputting the insns) and call to the library function
2200 with the stabilized argument list. */
2204 return expand_call (exp
, target
, target
== const0_rtx
);
2208 expand_errno_check (exp
, target
);
2210 /* Output the entire sequence. */
2211 insns
= get_insns ();
2218 /* Expand a call to the builtin trinary math functions (fma).
2219 Return NULL_RTX if a normal call should be emitted rather than expanding the
2220 function in-line. EXP is the expression that is a call to the builtin
2221 function; if convenient, the result should be placed in TARGET.
2222 SUBTARGET may be used as the target for computing one of EXP's
2226 expand_builtin_mathfn_ternary (tree exp
, rtx target
, rtx subtarget
)
2228 optab builtin_optab
;
2229 rtx op0
, op1
, op2
, insns
;
2230 tree fndecl
= get_callee_fndecl (exp
);
2231 tree arg0
, arg1
, arg2
;
2232 enum machine_mode mode
;
2234 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
2237 arg0
= CALL_EXPR_ARG (exp
, 0);
2238 arg1
= CALL_EXPR_ARG (exp
, 1);
2239 arg2
= CALL_EXPR_ARG (exp
, 2);
2241 switch (DECL_FUNCTION_CODE (fndecl
))
2243 CASE_FLT_FN (BUILT_IN_FMA
):
2244 builtin_optab
= fma_optab
; break;
2249 /* Make a suitable register to place result in. */
2250 mode
= TYPE_MODE (TREE_TYPE (exp
));
2252 /* Before working hard, check whether the instruction is available. */
2253 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2256 target
= gen_reg_rtx (mode
);
2258 /* Always stabilize the argument list. */
2259 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2260 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2261 CALL_EXPR_ARG (exp
, 2) = arg2
= builtin_save_expr (arg2
);
2263 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2264 op1
= expand_normal (arg1
);
2265 op2
= expand_normal (arg2
);
2269 /* Compute into TARGET.
2270 Set TARGET to wherever the result comes back. */
2271 target
= expand_ternary_op (mode
, builtin_optab
, op0
, op1
, op2
,
2274 /* If we were unable to expand via the builtin, stop the sequence
2275 (without outputting the insns) and call to the library function
2276 with the stabilized argument list. */
2280 return expand_call (exp
, target
, target
== const0_rtx
);
2283 /* Output the entire sequence. */
2284 insns
= get_insns ();
2291 /* Expand a call to the builtin sin and cos math functions.
2292 Return NULL_RTX if a normal call should be emitted rather than expanding the
2293 function in-line. EXP is the expression that is a call to the builtin
2294 function; if convenient, the result should be placed in TARGET.
2295 SUBTARGET may be used as the target for computing one of EXP's
2299 expand_builtin_mathfn_3 (tree exp
, rtx target
, rtx subtarget
)
2301 optab builtin_optab
;
2303 tree fndecl
= get_callee_fndecl (exp
);
2304 enum machine_mode mode
;
2307 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2310 arg
= CALL_EXPR_ARG (exp
, 0);
2312 switch (DECL_FUNCTION_CODE (fndecl
))
2314 CASE_FLT_FN (BUILT_IN_SIN
):
2315 CASE_FLT_FN (BUILT_IN_COS
):
2316 builtin_optab
= sincos_optab
; break;
2321 /* Make a suitable register to place result in. */
2322 mode
= TYPE_MODE (TREE_TYPE (exp
));
2324 /* Check if sincos insn is available, otherwise fallback
2325 to sin or cos insn. */
2326 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2327 switch (DECL_FUNCTION_CODE (fndecl
))
2329 CASE_FLT_FN (BUILT_IN_SIN
):
2330 builtin_optab
= sin_optab
; break;
2331 CASE_FLT_FN (BUILT_IN_COS
):
2332 builtin_optab
= cos_optab
; break;
2337 /* Before working hard, check whether the instruction is available. */
2338 if (optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
)
2340 target
= gen_reg_rtx (mode
);
2342 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2343 need to expand the argument again. This way, we will not perform
2344 side-effects more the once. */
2345 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2347 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2351 /* Compute into TARGET.
2352 Set TARGET to wherever the result comes back. */
2353 if (builtin_optab
== sincos_optab
)
2357 switch (DECL_FUNCTION_CODE (fndecl
))
2359 CASE_FLT_FN (BUILT_IN_SIN
):
2360 result
= expand_twoval_unop (builtin_optab
, op0
, 0, target
, 0);
2362 CASE_FLT_FN (BUILT_IN_COS
):
2363 result
= expand_twoval_unop (builtin_optab
, op0
, target
, 0, 0);
2368 gcc_assert (result
);
2372 target
= expand_unop (mode
, builtin_optab
, op0
, target
, 0);
2377 /* Output the entire sequence. */
2378 insns
= get_insns ();
2384 /* If we were unable to expand via the builtin, stop the sequence
2385 (without outputting the insns) and call to the library function
2386 with the stabilized argument list. */
2390 target
= expand_call (exp
, target
, target
== const0_rtx
);
2395 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2396 return an RTL instruction code that implements the functionality.
2397 If that isn't possible or available return CODE_FOR_nothing. */
2399 static enum insn_code
2400 interclass_mathfn_icode (tree arg
, tree fndecl
)
2402 bool errno_set
= false;
2403 optab builtin_optab
= 0;
2404 enum machine_mode mode
;
2406 switch (DECL_FUNCTION_CODE (fndecl
))
2408 CASE_FLT_FN (BUILT_IN_ILOGB
):
2409 errno_set
= true; builtin_optab
= ilogb_optab
; break;
2410 CASE_FLT_FN (BUILT_IN_ISINF
):
2411 builtin_optab
= isinf_optab
; break;
2412 case BUILT_IN_ISNORMAL
:
2413 case BUILT_IN_ISFINITE
:
2414 CASE_FLT_FN (BUILT_IN_FINITE
):
2415 case BUILT_IN_FINITED32
:
2416 case BUILT_IN_FINITED64
:
2417 case BUILT_IN_FINITED128
:
2418 case BUILT_IN_ISINFD32
:
2419 case BUILT_IN_ISINFD64
:
2420 case BUILT_IN_ISINFD128
:
2421 /* These builtins have no optabs (yet). */
2427 /* There's no easy way to detect the case we need to set EDOM. */
2428 if (flag_errno_math
&& errno_set
)
2429 return CODE_FOR_nothing
;
2431 /* Optab mode depends on the mode of the input argument. */
2432 mode
= TYPE_MODE (TREE_TYPE (arg
));
2435 return optab_handler (builtin_optab
, mode
);
2436 return CODE_FOR_nothing
;
2439 /* Expand a call to one of the builtin math functions that operate on
2440 floating point argument and output an integer result (ilogb, isinf,
2442 Return 0 if a normal call should be emitted rather than expanding the
2443 function in-line. EXP is the expression that is a call to the builtin
2444 function; if convenient, the result should be placed in TARGET. */
2447 expand_builtin_interclass_mathfn (tree exp
, rtx target
)
2449 enum insn_code icode
= CODE_FOR_nothing
;
2451 tree fndecl
= get_callee_fndecl (exp
);
2452 enum machine_mode mode
;
2455 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2458 arg
= CALL_EXPR_ARG (exp
, 0);
2459 icode
= interclass_mathfn_icode (arg
, fndecl
);
2460 mode
= TYPE_MODE (TREE_TYPE (arg
));
2462 if (icode
!= CODE_FOR_nothing
)
2464 struct expand_operand ops
[1];
2465 rtx last
= get_last_insn ();
2466 tree orig_arg
= arg
;
2468 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2469 need to expand the argument again. This way, we will not perform
2470 side-effects more the once. */
2471 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2473 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2475 if (mode
!= GET_MODE (op0
))
2476 op0
= convert_to_mode (mode
, op0
, 0);
2478 create_output_operand (&ops
[0], target
, TYPE_MODE (TREE_TYPE (exp
)));
2479 if (maybe_legitimize_operands (icode
, 0, 1, ops
)
2480 && maybe_emit_unop_insn (icode
, ops
[0].value
, op0
, UNKNOWN
))
2481 return ops
[0].value
;
2483 delete_insns_since (last
);
2484 CALL_EXPR_ARG (exp
, 0) = orig_arg
;
2490 /* Expand a call to the builtin sincos math function.
2491 Return NULL_RTX if a normal call should be emitted rather than expanding the
2492 function in-line. EXP is the expression that is a call to the builtin
2496 expand_builtin_sincos (tree exp
)
2498 rtx op0
, op1
, op2
, target1
, target2
;
2499 enum machine_mode mode
;
2500 tree arg
, sinp
, cosp
;
2502 location_t loc
= EXPR_LOCATION (exp
);
2503 tree alias_type
, alias_off
;
2505 if (!validate_arglist (exp
, REAL_TYPE
,
2506 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2509 arg
= CALL_EXPR_ARG (exp
, 0);
2510 sinp
= CALL_EXPR_ARG (exp
, 1);
2511 cosp
= CALL_EXPR_ARG (exp
, 2);
2513 /* Make a suitable register to place result in. */
2514 mode
= TYPE_MODE (TREE_TYPE (arg
));
2516 /* Check if sincos insn is available, otherwise emit the call. */
2517 if (optab_handler (sincos_optab
, mode
) == CODE_FOR_nothing
)
2520 target1
= gen_reg_rtx (mode
);
2521 target2
= gen_reg_rtx (mode
);
2523 op0
= expand_normal (arg
);
2524 alias_type
= build_pointer_type_for_mode (TREE_TYPE (arg
), ptr_mode
, true);
2525 alias_off
= build_int_cst (alias_type
, 0);
2526 op1
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2528 op2
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2531 /* Compute into target1 and target2.
2532 Set TARGET to wherever the result comes back. */
2533 result
= expand_twoval_unop (sincos_optab
, op0
, target2
, target1
, 0);
2534 gcc_assert (result
);
2536 /* Move target1 and target2 to the memory locations indicated
2538 emit_move_insn (op1
, target1
);
2539 emit_move_insn (op2
, target2
);
2544 /* Expand a call to the internal cexpi builtin to the sincos math function.
2545 EXP is the expression that is a call to the builtin function; if convenient,
2546 the result should be placed in TARGET. */
2549 expand_builtin_cexpi (tree exp
, rtx target
)
2551 tree fndecl
= get_callee_fndecl (exp
);
2553 enum machine_mode mode
;
2555 location_t loc
= EXPR_LOCATION (exp
);
2557 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2560 arg
= CALL_EXPR_ARG (exp
, 0);
2561 type
= TREE_TYPE (arg
);
2562 mode
= TYPE_MODE (TREE_TYPE (arg
));
2564 /* Try expanding via a sincos optab, fall back to emitting a libcall
2565 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2566 is only generated from sincos, cexp or if we have either of them. */
2567 if (optab_handler (sincos_optab
, mode
) != CODE_FOR_nothing
)
2569 op1
= gen_reg_rtx (mode
);
2570 op2
= gen_reg_rtx (mode
);
2572 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2574 /* Compute into op1 and op2. */
2575 expand_twoval_unop (sincos_optab
, op0
, op2
, op1
, 0);
2577 else if (TARGET_HAS_SINCOS
)
2579 tree call
, fn
= NULL_TREE
;
2583 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2584 fn
= builtin_decl_explicit (BUILT_IN_SINCOSF
);
2585 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2586 fn
= builtin_decl_explicit (BUILT_IN_SINCOS
);
2587 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2588 fn
= builtin_decl_explicit (BUILT_IN_SINCOSL
);
2592 op1
= assign_temp (TREE_TYPE (arg
), 0, 1, 1);
2593 op2
= assign_temp (TREE_TYPE (arg
), 0, 1, 1);
2594 op1a
= copy_to_mode_reg (Pmode
, XEXP (op1
, 0));
2595 op2a
= copy_to_mode_reg (Pmode
, XEXP (op2
, 0));
2596 top1
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op1a
);
2597 top2
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op2a
);
2599 /* Make sure not to fold the sincos call again. */
2600 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2601 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn
)),
2602 call
, 3, arg
, top1
, top2
));
2606 tree call
, fn
= NULL_TREE
, narg
;
2607 tree ctype
= build_complex_type (type
);
2609 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2610 fn
= builtin_decl_explicit (BUILT_IN_CEXPF
);
2611 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2612 fn
= builtin_decl_explicit (BUILT_IN_CEXP
);
2613 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2614 fn
= builtin_decl_explicit (BUILT_IN_CEXPL
);
2618 /* If we don't have a decl for cexp create one. This is the
2619 friendliest fallback if the user calls __builtin_cexpi
2620 without full target C99 function support. */
2621 if (fn
== NULL_TREE
)
2624 const char *name
= NULL
;
2626 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2628 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2630 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2633 fntype
= build_function_type_list (ctype
, ctype
, NULL_TREE
);
2634 fn
= build_fn_decl (name
, fntype
);
2637 narg
= fold_build2_loc (loc
, COMPLEX_EXPR
, ctype
,
2638 build_real (type
, dconst0
), arg
);
2640 /* Make sure not to fold the cexp call again. */
2641 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2642 return expand_expr (build_call_nary (ctype
, call
, 1, narg
),
2643 target
, VOIDmode
, EXPAND_NORMAL
);
2646 /* Now build the proper return type. */
2647 return expand_expr (build2 (COMPLEX_EXPR
, build_complex_type (type
),
2648 make_tree (TREE_TYPE (arg
), op2
),
2649 make_tree (TREE_TYPE (arg
), op1
)),
2650 target
, VOIDmode
, EXPAND_NORMAL
);
2653 /* Conveniently construct a function call expression. FNDECL names the
2654 function to be called, N is the number of arguments, and the "..."
2655 parameters are the argument expressions. Unlike build_call_exr
2656 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2659 build_call_nofold_loc (location_t loc
, tree fndecl
, int n
, ...)
2662 tree fntype
= TREE_TYPE (fndecl
);
2663 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), fndecl
);
2666 fn
= build_call_valist (TREE_TYPE (fntype
), fn
, n
, ap
);
2668 SET_EXPR_LOCATION (fn
, loc
);
2672 /* Expand a call to one of the builtin rounding functions gcc defines
2673 as an extension (lfloor and lceil). As these are gcc extensions we
2674 do not need to worry about setting errno to EDOM.
2675 If expanding via optab fails, lower expression to (int)(floor(x)).
2676 EXP is the expression that is a call to the builtin function;
2677 if convenient, the result should be placed in TARGET. */
2680 expand_builtin_int_roundingfn (tree exp
, rtx target
)
2682 convert_optab builtin_optab
;
2683 rtx op0
, insns
, tmp
;
2684 tree fndecl
= get_callee_fndecl (exp
);
2685 enum built_in_function fallback_fn
;
2686 tree fallback_fndecl
;
2687 enum machine_mode mode
;
2690 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2693 arg
= CALL_EXPR_ARG (exp
, 0);
2695 switch (DECL_FUNCTION_CODE (fndecl
))
2697 CASE_FLT_FN (BUILT_IN_ICEIL
):
2698 CASE_FLT_FN (BUILT_IN_LCEIL
):
2699 CASE_FLT_FN (BUILT_IN_LLCEIL
):
2700 builtin_optab
= lceil_optab
;
2701 fallback_fn
= BUILT_IN_CEIL
;
2704 CASE_FLT_FN (BUILT_IN_IFLOOR
):
2705 CASE_FLT_FN (BUILT_IN_LFLOOR
):
2706 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
2707 builtin_optab
= lfloor_optab
;
2708 fallback_fn
= BUILT_IN_FLOOR
;
2715 /* Make a suitable register to place result in. */
2716 mode
= TYPE_MODE (TREE_TYPE (exp
));
2718 target
= gen_reg_rtx (mode
);
2720 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2721 need to expand the argument again. This way, we will not perform
2722 side-effects more the once. */
2723 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2725 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2729 /* Compute into TARGET. */
2730 if (expand_sfix_optab (target
, op0
, builtin_optab
))
2732 /* Output the entire sequence. */
2733 insns
= get_insns ();
2739 /* If we were unable to expand via the builtin, stop the sequence
2740 (without outputting the insns). */
2743 /* Fall back to floating point rounding optab. */
2744 fallback_fndecl
= mathfn_built_in (TREE_TYPE (arg
), fallback_fn
);
2746 /* For non-C99 targets we may end up without a fallback fndecl here
2747 if the user called __builtin_lfloor directly. In this case emit
2748 a call to the floor/ceil variants nevertheless. This should result
2749 in the best user experience for not full C99 targets. */
2750 if (fallback_fndecl
== NULL_TREE
)
2753 const char *name
= NULL
;
2755 switch (DECL_FUNCTION_CODE (fndecl
))
2757 case BUILT_IN_ICEIL
:
2758 case BUILT_IN_LCEIL
:
2759 case BUILT_IN_LLCEIL
:
2762 case BUILT_IN_ICEILF
:
2763 case BUILT_IN_LCEILF
:
2764 case BUILT_IN_LLCEILF
:
2767 case BUILT_IN_ICEILL
:
2768 case BUILT_IN_LCEILL
:
2769 case BUILT_IN_LLCEILL
:
2772 case BUILT_IN_IFLOOR
:
2773 case BUILT_IN_LFLOOR
:
2774 case BUILT_IN_LLFLOOR
:
2777 case BUILT_IN_IFLOORF
:
2778 case BUILT_IN_LFLOORF
:
2779 case BUILT_IN_LLFLOORF
:
2782 case BUILT_IN_IFLOORL
:
2783 case BUILT_IN_LFLOORL
:
2784 case BUILT_IN_LLFLOORL
:
2791 fntype
= build_function_type_list (TREE_TYPE (arg
),
2792 TREE_TYPE (arg
), NULL_TREE
);
2793 fallback_fndecl
= build_fn_decl (name
, fntype
);
2796 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
), fallback_fndecl
, 1, arg
);
2798 tmp
= expand_normal (exp
);
2800 /* Truncate the result of floating point optab to integer
2801 via expand_fix (). */
2802 target
= gen_reg_rtx (mode
);
2803 expand_fix (target
, tmp
, 0);
2808 /* Expand a call to one of the builtin math functions doing integer
2810 Return 0 if a normal call should be emitted rather than expanding the
2811 function in-line. EXP is the expression that is a call to the builtin
2812 function; if convenient, the result should be placed in TARGET. */
2815 expand_builtin_int_roundingfn_2 (tree exp
, rtx target
)
2817 convert_optab builtin_optab
;
2819 tree fndecl
= get_callee_fndecl (exp
);
2821 enum machine_mode mode
;
2823 /* There's no easy way to detect the case we need to set EDOM. */
2824 if (flag_errno_math
)
2827 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2830 arg
= CALL_EXPR_ARG (exp
, 0);
2832 switch (DECL_FUNCTION_CODE (fndecl
))
2834 CASE_FLT_FN (BUILT_IN_IRINT
):
2835 CASE_FLT_FN (BUILT_IN_LRINT
):
2836 CASE_FLT_FN (BUILT_IN_LLRINT
):
2837 builtin_optab
= lrint_optab
; break;
2839 CASE_FLT_FN (BUILT_IN_IROUND
):
2840 CASE_FLT_FN (BUILT_IN_LROUND
):
2841 CASE_FLT_FN (BUILT_IN_LLROUND
):
2842 builtin_optab
= lround_optab
; break;
2848 /* Make a suitable register to place result in. */
2849 mode
= TYPE_MODE (TREE_TYPE (exp
));
2851 target
= gen_reg_rtx (mode
);
2853 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2854 need to expand the argument again. This way, we will not perform
2855 side-effects more the once. */
2856 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2858 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2862 if (expand_sfix_optab (target
, op0
, builtin_optab
))
2864 /* Output the entire sequence. */
2865 insns
= get_insns ();
2871 /* If we were unable to expand via the builtin, stop the sequence
2872 (without outputting the insns) and call to the library function
2873 with the stabilized argument list. */
2876 target
= expand_call (exp
, target
, target
== const0_rtx
);
2881 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2882 a normal call should be emitted rather than expanding the function
2883 in-line. EXP is the expression that is a call to the builtin
2884 function; if convenient, the result should be placed in TARGET. */
2887 expand_builtin_powi (tree exp
, rtx target
)
2891 enum machine_mode mode
;
2892 enum machine_mode mode2
;
2894 if (! validate_arglist (exp
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2897 arg0
= CALL_EXPR_ARG (exp
, 0);
2898 arg1
= CALL_EXPR_ARG (exp
, 1);
2899 mode
= TYPE_MODE (TREE_TYPE (exp
));
2901 /* Emit a libcall to libgcc. */
2903 /* Mode of the 2nd argument must match that of an int. */
2904 mode2
= mode_for_size (INT_TYPE_SIZE
, MODE_INT
, 0);
2906 if (target
== NULL_RTX
)
2907 target
= gen_reg_rtx (mode
);
2909 op0
= expand_expr (arg0
, NULL_RTX
, mode
, EXPAND_NORMAL
);
2910 if (GET_MODE (op0
) != mode
)
2911 op0
= convert_to_mode (mode
, op0
, 0);
2912 op1
= expand_expr (arg1
, NULL_RTX
, mode2
, EXPAND_NORMAL
);
2913 if (GET_MODE (op1
) != mode2
)
2914 op1
= convert_to_mode (mode2
, op1
, 0);
2916 target
= emit_library_call_value (optab_libfunc (powi_optab
, mode
),
2917 target
, LCT_CONST
, mode
, 2,
2918 op0
, mode
, op1
, mode2
);
2923 /* Expand expression EXP which is a call to the strlen builtin. Return
2924 NULL_RTX if we failed the caller should emit a normal call, otherwise
2925 try to get the result in TARGET, if convenient. */
2928 expand_builtin_strlen (tree exp
, rtx target
,
2929 enum machine_mode target_mode
)
2931 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
2935 struct expand_operand ops
[4];
2938 tree src
= CALL_EXPR_ARG (exp
, 0);
2939 rtx src_reg
, before_strlen
;
2940 enum machine_mode insn_mode
= target_mode
;
2941 enum insn_code icode
= CODE_FOR_nothing
;
2944 /* If the length can be computed at compile-time, return it. */
2945 len
= c_strlen (src
, 0);
2947 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2949 /* If the length can be computed at compile-time and is constant
2950 integer, but there are side-effects in src, evaluate
2951 src for side-effects, then return len.
2952 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2953 can be optimized into: i++; x = 3; */
2954 len
= c_strlen (src
, 1);
2955 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
2957 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
2958 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2961 align
= get_pointer_alignment (src
) / BITS_PER_UNIT
;
2963 /* If SRC is not a pointer type, don't do this operation inline. */
2967 /* Bail out if we can't compute strlen in the right mode. */
2968 while (insn_mode
!= VOIDmode
)
2970 icode
= optab_handler (strlen_optab
, insn_mode
);
2971 if (icode
!= CODE_FOR_nothing
)
2974 insn_mode
= GET_MODE_WIDER_MODE (insn_mode
);
2976 if (insn_mode
== VOIDmode
)
2979 /* Make a place to hold the source address. We will not expand
2980 the actual source until we are sure that the expansion will
2981 not fail -- there are trees that cannot be expanded twice. */
2982 src_reg
= gen_reg_rtx (Pmode
);
2984 /* Mark the beginning of the strlen sequence so we can emit the
2985 source operand later. */
2986 before_strlen
= get_last_insn ();
2988 create_output_operand (&ops
[0], target
, insn_mode
);
2989 create_fixed_operand (&ops
[1], gen_rtx_MEM (BLKmode
, src_reg
));
2990 create_integer_operand (&ops
[2], 0);
2991 create_integer_operand (&ops
[3], align
);
2992 if (!maybe_expand_insn (icode
, 4, ops
))
2995 /* Now that we are assured of success, expand the source. */
2997 pat
= expand_expr (src
, src_reg
, Pmode
, EXPAND_NORMAL
);
3000 #ifdef POINTERS_EXTEND_UNSIGNED
3001 if (GET_MODE (pat
) != Pmode
)
3002 pat
= convert_to_mode (Pmode
, pat
,
3003 POINTERS_EXTEND_UNSIGNED
);
3005 emit_move_insn (src_reg
, pat
);
3011 emit_insn_after (pat
, before_strlen
);
3013 emit_insn_before (pat
, get_insns ());
3015 /* Return the value in the proper mode for this function. */
3016 if (GET_MODE (ops
[0].value
) == target_mode
)
3017 target
= ops
[0].value
;
3018 else if (target
!= 0)
3019 convert_move (target
, ops
[0].value
, 0);
3021 target
= convert_to_mode (target_mode
, ops
[0].value
, 0);
3027 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3028 bytes from constant string DATA + OFFSET and return it as target
3032 builtin_memcpy_read_str (void *data
, HOST_WIDE_INT offset
,
3033 enum machine_mode mode
)
3035 const char *str
= (const char *) data
;
3037 gcc_assert (offset
>= 0
3038 && ((unsigned HOST_WIDE_INT
) offset
+ GET_MODE_SIZE (mode
)
3039 <= strlen (str
) + 1));
3041 return c_readstr (str
+ offset
, mode
);
3044 /* Expand a call EXP to the memcpy builtin.
3045 Return NULL_RTX if we failed, the caller should emit a normal call,
3046 otherwise try to get the result in TARGET, if convenient (and in
3047 mode MODE if that's convenient). */
3050 expand_builtin_memcpy (tree exp
, rtx target
)
3052 if (!validate_arglist (exp
,
3053 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3057 tree dest
= CALL_EXPR_ARG (exp
, 0);
3058 tree src
= CALL_EXPR_ARG (exp
, 1);
3059 tree len
= CALL_EXPR_ARG (exp
, 2);
3060 const char *src_str
;
3061 unsigned int src_align
= get_pointer_alignment (src
);
3062 unsigned int dest_align
= get_pointer_alignment (dest
);
3063 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
3064 HOST_WIDE_INT expected_size
= -1;
3065 unsigned int expected_align
= 0;
3067 /* If DEST is not a pointer type, call the normal function. */
3068 if (dest_align
== 0)
3071 /* If either SRC is not a pointer type, don't do this
3072 operation in-line. */
3076 if (currently_expanding_gimple_stmt
)
3077 stringop_block_profile (currently_expanding_gimple_stmt
,
3078 &expected_align
, &expected_size
);
3080 if (expected_align
< dest_align
)
3081 expected_align
= dest_align
;
3082 dest_mem
= get_memory_rtx (dest
, len
);
3083 set_mem_align (dest_mem
, dest_align
);
3084 len_rtx
= expand_normal (len
);
3085 src_str
= c_getstr (src
);
3087 /* If SRC is a string constant and block move would be done
3088 by pieces, we can avoid loading the string from memory
3089 and only stored the computed constants. */
3091 && CONST_INT_P (len_rtx
)
3092 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3093 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3094 CONST_CAST (char *, src_str
),
3097 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3098 builtin_memcpy_read_str
,
3099 CONST_CAST (char *, src_str
),
3100 dest_align
, false, 0);
3101 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3102 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3106 src_mem
= get_memory_rtx (src
, len
);
3107 set_mem_align (src_mem
, src_align
);
3109 /* Copy word part most expediently. */
3110 dest_addr
= emit_block_move_hints (dest_mem
, src_mem
, len_rtx
,
3111 CALL_EXPR_TAILCALL (exp
)
3112 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3113 expected_align
, expected_size
);
3117 dest_addr
= force_operand (XEXP (dest_mem
, 0), target
);
3118 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3124 /* Expand a call EXP to the mempcpy builtin.
3125 Return NULL_RTX if we failed; the caller should emit a normal call,
3126 otherwise try to get the result in TARGET, if convenient (and in
3127 mode MODE if that's convenient). If ENDP is 0 return the
3128 destination pointer, if ENDP is 1 return the end pointer ala
3129 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3133 expand_builtin_mempcpy (tree exp
, rtx target
, enum machine_mode mode
)
3135 if (!validate_arglist (exp
,
3136 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3140 tree dest
= CALL_EXPR_ARG (exp
, 0);
3141 tree src
= CALL_EXPR_ARG (exp
, 1);
3142 tree len
= CALL_EXPR_ARG (exp
, 2);
3143 return expand_builtin_mempcpy_args (dest
, src
, len
,
3144 target
, mode
, /*endp=*/ 1);
3148 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3149 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3150 so that this can also be called without constructing an actual CALL_EXPR.
3151 The other arguments and return value are the same as for
3152 expand_builtin_mempcpy. */
3155 expand_builtin_mempcpy_args (tree dest
, tree src
, tree len
,
3156 rtx target
, enum machine_mode mode
, int endp
)
3158 /* If return value is ignored, transform mempcpy into memcpy. */
3159 if (target
== const0_rtx
&& builtin_decl_implicit_p (BUILT_IN_MEMCPY
))
3161 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
3162 tree result
= build_call_nofold_loc (UNKNOWN_LOCATION
, fn
, 3,
3164 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3168 const char *src_str
;
3169 unsigned int src_align
= get_pointer_alignment (src
);
3170 unsigned int dest_align
= get_pointer_alignment (dest
);
3171 rtx dest_mem
, src_mem
, len_rtx
;
3173 /* If either SRC or DEST is not a pointer type, don't do this
3174 operation in-line. */
3175 if (dest_align
== 0 || src_align
== 0)
3178 /* If LEN is not constant, call the normal function. */
3179 if (! host_integerp (len
, 1))
3182 len_rtx
= expand_normal (len
);
3183 src_str
= c_getstr (src
);
3185 /* If SRC is a string constant and block move would be done
3186 by pieces, we can avoid loading the string from memory
3187 and only stored the computed constants. */
3189 && CONST_INT_P (len_rtx
)
3190 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3191 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3192 CONST_CAST (char *, src_str
),
3195 dest_mem
= get_memory_rtx (dest
, len
);
3196 set_mem_align (dest_mem
, dest_align
);
3197 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3198 builtin_memcpy_read_str
,
3199 CONST_CAST (char *, src_str
),
3200 dest_align
, false, endp
);
3201 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3202 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3206 if (CONST_INT_P (len_rtx
)
3207 && can_move_by_pieces (INTVAL (len_rtx
),
3208 MIN (dest_align
, src_align
)))
3210 dest_mem
= get_memory_rtx (dest
, len
);
3211 set_mem_align (dest_mem
, dest_align
);
3212 src_mem
= get_memory_rtx (src
, len
);
3213 set_mem_align (src_mem
, src_align
);
3214 dest_mem
= move_by_pieces (dest_mem
, src_mem
, INTVAL (len_rtx
),
3215 MIN (dest_align
, src_align
), endp
);
3216 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3217 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3226 # define HAVE_movstr 0
3227 # define CODE_FOR_movstr CODE_FOR_nothing
3230 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3231 we failed, the caller should emit a normal call, otherwise try to
3232 get the result in TARGET, if convenient. If ENDP is 0 return the
3233 destination pointer, if ENDP is 1 return the end pointer ala
3234 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3238 expand_movstr (tree dest
, tree src
, rtx target
, int endp
)
3240 struct expand_operand ops
[3];
3247 dest_mem
= get_memory_rtx (dest
, NULL
);
3248 src_mem
= get_memory_rtx (src
, NULL
);
3251 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
3252 dest_mem
= replace_equiv_address (dest_mem
, target
);
3255 create_output_operand (&ops
[0], endp
? target
: NULL_RTX
, Pmode
);
3256 create_fixed_operand (&ops
[1], dest_mem
);
3257 create_fixed_operand (&ops
[2], src_mem
);
3258 expand_insn (CODE_FOR_movstr
, 3, ops
);
3260 if (endp
&& target
!= const0_rtx
)
3262 target
= ops
[0].value
;
3263 /* movstr is supposed to set end to the address of the NUL
3264 terminator. If the caller requested a mempcpy-like return value,
3268 rtx tem
= plus_constant (gen_lowpart (GET_MODE (target
), target
), 1);
3269 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
3275 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3276 NULL_RTX if we failed the caller should emit a normal call, otherwise
3277 try to get the result in TARGET, if convenient (and in mode MODE if that's
3281 expand_builtin_strcpy (tree exp
, rtx target
)
3283 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3285 tree dest
= CALL_EXPR_ARG (exp
, 0);
3286 tree src
= CALL_EXPR_ARG (exp
, 1);
3287 return expand_builtin_strcpy_args (dest
, src
, target
);
3292 /* Helper function to do the actual work for expand_builtin_strcpy. The
3293 arguments to the builtin_strcpy call DEST and SRC are broken out
3294 so that this can also be called without constructing an actual CALL_EXPR.
3295 The other arguments and return value are the same as for
3296 expand_builtin_strcpy. */
3299 expand_builtin_strcpy_args (tree dest
, tree src
, rtx target
)
3301 return expand_movstr (dest
, src
, target
, /*endp=*/0);
3304 /* Expand a call EXP to the stpcpy builtin.
3305 Return NULL_RTX if we failed the caller should emit a normal call,
3306 otherwise try to get the result in TARGET, if convenient (and in
3307 mode MODE if that's convenient). */
3310 expand_builtin_stpcpy (tree exp
, rtx target
, enum machine_mode mode
)
3313 location_t loc
= EXPR_LOCATION (exp
);
3315 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3318 dst
= CALL_EXPR_ARG (exp
, 0);
3319 src
= CALL_EXPR_ARG (exp
, 1);
3321 /* If return value is ignored, transform stpcpy into strcpy. */
3322 if (target
== const0_rtx
&& builtin_decl_implicit (BUILT_IN_STRCPY
))
3324 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3325 tree result
= build_call_nofold_loc (loc
, fn
, 2, dst
, src
);
3326 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3333 /* Ensure we get an actual string whose length can be evaluated at
3334 compile-time, not an expression containing a string. This is
3335 because the latter will potentially produce pessimized code
3336 when used to produce the return value. */
3337 if (! c_getstr (src
) || ! (len
= c_strlen (src
, 0)))
3338 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3340 lenp1
= size_binop_loc (loc
, PLUS_EXPR
, len
, ssize_int (1));
3341 ret
= expand_builtin_mempcpy_args (dst
, src
, lenp1
,
3342 target
, mode
, /*endp=*/2);
3347 if (TREE_CODE (len
) == INTEGER_CST
)
3349 rtx len_rtx
= expand_normal (len
);
3351 if (CONST_INT_P (len_rtx
))
3353 ret
= expand_builtin_strcpy_args (dst
, src
, target
);
3359 if (mode
!= VOIDmode
)
3360 target
= gen_reg_rtx (mode
);
3362 target
= gen_reg_rtx (GET_MODE (ret
));
3364 if (GET_MODE (target
) != GET_MODE (ret
))
3365 ret
= gen_lowpart (GET_MODE (target
), ret
);
3367 ret
= plus_constant (ret
, INTVAL (len_rtx
));
3368 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
3376 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3380 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3381 bytes from constant string DATA + OFFSET and return it as target
3385 builtin_strncpy_read_str (void *data
, HOST_WIDE_INT offset
,
3386 enum machine_mode mode
)
3388 const char *str
= (const char *) data
;
3390 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
3393 return c_readstr (str
+ offset
, mode
);
3396 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3397 NULL_RTX if we failed the caller should emit a normal call. */
3400 expand_builtin_strncpy (tree exp
, rtx target
)
3402 location_t loc
= EXPR_LOCATION (exp
);
3404 if (validate_arglist (exp
,
3405 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3407 tree dest
= CALL_EXPR_ARG (exp
, 0);
3408 tree src
= CALL_EXPR_ARG (exp
, 1);
3409 tree len
= CALL_EXPR_ARG (exp
, 2);
3410 tree slen
= c_strlen (src
, 1);
3412 /* We must be passed a constant len and src parameter. */
3413 if (!host_integerp (len
, 1) || !slen
|| !host_integerp (slen
, 1))
3416 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
3418 /* We're required to pad with trailing zeros if the requested
3419 len is greater than strlen(s2)+1. In that case try to
3420 use store_by_pieces, if it fails, punt. */
3421 if (tree_int_cst_lt (slen
, len
))
3423 unsigned int dest_align
= get_pointer_alignment (dest
);
3424 const char *p
= c_getstr (src
);
3427 if (!p
|| dest_align
== 0 || !host_integerp (len
, 1)
3428 || !can_store_by_pieces (tree_low_cst (len
, 1),
3429 builtin_strncpy_read_str
,
3430 CONST_CAST (char *, p
),
3434 dest_mem
= get_memory_rtx (dest
, len
);
3435 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3436 builtin_strncpy_read_str
,
3437 CONST_CAST (char *, p
), dest_align
, false, 0);
3438 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3439 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3446 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3447 bytes from constant string DATA + OFFSET and return it as target
3451 builtin_memset_read_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3452 enum machine_mode mode
)
3454 const char *c
= (const char *) data
;
3455 char *p
= XALLOCAVEC (char, GET_MODE_SIZE (mode
));
3457 memset (p
, *c
, GET_MODE_SIZE (mode
));
3459 return c_readstr (p
, mode
);
3462 /* Callback routine for store_by_pieces. Return the RTL of a register
3463 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3464 char value given in the RTL register data. For example, if mode is
3465 4 bytes wide, return the RTL for 0x01010101*data. */
3468 builtin_memset_gen_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3469 enum machine_mode mode
)
3475 size
= GET_MODE_SIZE (mode
);
3479 p
= XALLOCAVEC (char, size
);
3480 memset (p
, 1, size
);
3481 coeff
= c_readstr (p
, mode
);
3483 target
= convert_to_mode (mode
, (rtx
) data
, 1);
3484 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
3485 return force_reg (mode
, target
);
3488 /* Expand expression EXP, which is a call to the memset builtin. Return
3489 NULL_RTX if we failed the caller should emit a normal call, otherwise
3490 try to get the result in TARGET, if convenient (and in mode MODE if that's
3494 expand_builtin_memset (tree exp
, rtx target
, enum machine_mode mode
)
3496 if (!validate_arglist (exp
,
3497 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3501 tree dest
= CALL_EXPR_ARG (exp
, 0);
3502 tree val
= CALL_EXPR_ARG (exp
, 1);
3503 tree len
= CALL_EXPR_ARG (exp
, 2);
3504 return expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
3508 /* Helper function to do the actual work for expand_builtin_memset. The
3509 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3510 so that this can also be called without constructing an actual CALL_EXPR.
3511 The other arguments and return value are the same as for
3512 expand_builtin_memset. */
3515 expand_builtin_memset_args (tree dest
, tree val
, tree len
,
3516 rtx target
, enum machine_mode mode
, tree orig_exp
)
3519 enum built_in_function fcode
;
3520 enum machine_mode val_mode
;
3522 unsigned int dest_align
;
3523 rtx dest_mem
, dest_addr
, len_rtx
;
3524 HOST_WIDE_INT expected_size
= -1;
3525 unsigned int expected_align
= 0;
3527 dest_align
= get_pointer_alignment (dest
);
3529 /* If DEST is not a pointer type, don't do this operation in-line. */
3530 if (dest_align
== 0)
3533 if (currently_expanding_gimple_stmt
)
3534 stringop_block_profile (currently_expanding_gimple_stmt
,
3535 &expected_align
, &expected_size
);
3537 if (expected_align
< dest_align
)
3538 expected_align
= dest_align
;
3540 /* If the LEN parameter is zero, return DEST. */
3541 if (integer_zerop (len
))
3543 /* Evaluate and ignore VAL in case it has side-effects. */
3544 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3545 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
3548 /* Stabilize the arguments in case we fail. */
3549 dest
= builtin_save_expr (dest
);
3550 val
= builtin_save_expr (val
);
3551 len
= builtin_save_expr (len
);
3553 len_rtx
= expand_normal (len
);
3554 dest_mem
= get_memory_rtx (dest
, len
);
3555 val_mode
= TYPE_MODE (unsigned_char_type_node
);
3557 if (TREE_CODE (val
) != INTEGER_CST
)
3561 val_rtx
= expand_normal (val
);
3562 val_rtx
= convert_to_mode (val_mode
, val_rtx
, 0);
3564 /* Assume that we can memset by pieces if we can store
3565 * the coefficients by pieces (in the required modes).
3566 * We can't pass builtin_memset_gen_str as that emits RTL. */
3568 if (host_integerp (len
, 1)
3569 && can_store_by_pieces (tree_low_cst (len
, 1),
3570 builtin_memset_read_str
, &c
, dest_align
,
3573 val_rtx
= force_reg (val_mode
, val_rtx
);
3574 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3575 builtin_memset_gen_str
, val_rtx
, dest_align
,
3578 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, val_rtx
,
3579 dest_align
, expected_align
,
3583 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3584 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3588 if (target_char_cast (val
, &c
))
3593 if (host_integerp (len
, 1)
3594 && can_store_by_pieces (tree_low_cst (len
, 1),
3595 builtin_memset_read_str
, &c
, dest_align
,
3597 store_by_pieces (dest_mem
, tree_low_cst (len
, 1),
3598 builtin_memset_read_str
, &c
, dest_align
, true, 0);
3599 else if (!set_storage_via_setmem (dest_mem
, len_rtx
,
3600 gen_int_mode (c
, val_mode
),
3601 dest_align
, expected_align
,
3605 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3606 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3610 set_mem_align (dest_mem
, dest_align
);
3611 dest_addr
= clear_storage_hints (dest_mem
, len_rtx
,
3612 CALL_EXPR_TAILCALL (orig_exp
)
3613 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3614 expected_align
, expected_size
);
3618 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3619 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3625 fndecl
= get_callee_fndecl (orig_exp
);
3626 fcode
= DECL_FUNCTION_CODE (fndecl
);
3627 if (fcode
== BUILT_IN_MEMSET
)
3628 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 3,
3630 else if (fcode
== BUILT_IN_BZERO
)
3631 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 2,
3635 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
3636 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
3637 return expand_call (fn
, target
, target
== const0_rtx
);
3640 /* Expand expression EXP, which is a call to the bzero builtin. Return
3641 NULL_RTX if we failed the caller should emit a normal call. */
3644 expand_builtin_bzero (tree exp
)
3647 location_t loc
= EXPR_LOCATION (exp
);
3649 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3652 dest
= CALL_EXPR_ARG (exp
, 0);
3653 size
= CALL_EXPR_ARG (exp
, 1);
3655 /* New argument list transforming bzero(ptr x, int y) to
3656 memset(ptr x, int 0, size_t y). This is done this way
3657 so that if it isn't expanded inline, we fallback to
3658 calling bzero instead of memset. */
3660 return expand_builtin_memset_args (dest
, integer_zero_node
,
3661 fold_convert_loc (loc
,
3662 size_type_node
, size
),
3663 const0_rtx
, VOIDmode
, exp
);
3666 /* Expand expression EXP, which is a call to the memcmp built-in function.
3667 Return NULL_RTX if we failed and the caller should emit a normal call,
3668 otherwise try to get the result in TARGET, if convenient (and in mode
3669 MODE, if that's convenient). */
3672 expand_builtin_memcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
3673 ATTRIBUTE_UNUSED
enum machine_mode mode
)
3675 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
3677 if (!validate_arglist (exp
,
3678 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3681 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3682 implementing memcmp because it will stop if it encounters two
3684 #if defined HAVE_cmpmemsi
3686 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3689 tree arg1
= CALL_EXPR_ARG (exp
, 0);
3690 tree arg2
= CALL_EXPR_ARG (exp
, 1);
3691 tree len
= CALL_EXPR_ARG (exp
, 2);
3693 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
3694 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
3695 enum machine_mode insn_mode
;
3698 insn_mode
= insn_data
[(int) CODE_FOR_cmpmemsi
].operand
[0].mode
;
3702 /* If we don't have POINTER_TYPE, call the function. */
3703 if (arg1_align
== 0 || arg2_align
== 0)
3706 /* Make a place to write the result of the instruction. */
3709 && REG_P (result
) && GET_MODE (result
) == insn_mode
3710 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3711 result
= gen_reg_rtx (insn_mode
);
3713 arg1_rtx
= get_memory_rtx (arg1
, len
);
3714 arg2_rtx
= get_memory_rtx (arg2
, len
);
3715 arg3_rtx
= expand_normal (fold_convert_loc (loc
, sizetype
, len
));
3717 /* Set MEM_SIZE as appropriate. */
3718 if (CONST_INT_P (arg3_rtx
))
3720 set_mem_size (arg1_rtx
, INTVAL (arg3_rtx
));
3721 set_mem_size (arg2_rtx
, INTVAL (arg3_rtx
));
3725 insn
= gen_cmpmemsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3726 GEN_INT (MIN (arg1_align
, arg2_align
)));
3733 emit_library_call_value (memcmp_libfunc
, result
, LCT_PURE
,
3734 TYPE_MODE (integer_type_node
), 3,
3735 XEXP (arg1_rtx
, 0), Pmode
,
3736 XEXP (arg2_rtx
, 0), Pmode
,
3737 convert_to_mode (TYPE_MODE (sizetype
), arg3_rtx
,
3738 TYPE_UNSIGNED (sizetype
)),
3739 TYPE_MODE (sizetype
));
3741 /* Return the value in the proper mode for this function. */
3742 mode
= TYPE_MODE (TREE_TYPE (exp
));
3743 if (GET_MODE (result
) == mode
)
3745 else if (target
!= 0)
3747 convert_move (target
, result
, 0);
3751 return convert_to_mode (mode
, result
, 0);
3753 #endif /* HAVE_cmpmemsi. */
3758 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3759 if we failed the caller should emit a normal call, otherwise try to get
3760 the result in TARGET, if convenient. */
3763 expand_builtin_strcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
)
3765 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3768 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3769 if (direct_optab_handler (cmpstr_optab
, SImode
) != CODE_FOR_nothing
3770 || direct_optab_handler (cmpstrn_optab
, SImode
) != CODE_FOR_nothing
)
3772 rtx arg1_rtx
, arg2_rtx
;
3773 rtx result
, insn
= NULL_RTX
;
3775 tree arg1
= CALL_EXPR_ARG (exp
, 0);
3776 tree arg2
= CALL_EXPR_ARG (exp
, 1);
3778 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
3779 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
3781 /* If we don't have POINTER_TYPE, call the function. */
3782 if (arg1_align
== 0 || arg2_align
== 0)
3785 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3786 arg1
= builtin_save_expr (arg1
);
3787 arg2
= builtin_save_expr (arg2
);
3789 arg1_rtx
= get_memory_rtx (arg1
, NULL
);
3790 arg2_rtx
= get_memory_rtx (arg2
, NULL
);
3792 #ifdef HAVE_cmpstrsi
3793 /* Try to call cmpstrsi. */
3796 enum machine_mode insn_mode
3797 = insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
3799 /* Make a place to write the result of the instruction. */
3802 && REG_P (result
) && GET_MODE (result
) == insn_mode
3803 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3804 result
= gen_reg_rtx (insn_mode
);
3806 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
,
3807 GEN_INT (MIN (arg1_align
, arg2_align
)));
3810 #ifdef HAVE_cmpstrnsi
3811 /* Try to determine at least one length and call cmpstrnsi. */
3812 if (!insn
&& HAVE_cmpstrnsi
)
3817 enum machine_mode insn_mode
3818 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
3819 tree len1
= c_strlen (arg1
, 1);
3820 tree len2
= c_strlen (arg2
, 1);
3823 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
3825 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
3827 /* If we don't have a constant length for the first, use the length
3828 of the second, if we know it. We don't require a constant for
3829 this case; some cost analysis could be done if both are available
3830 but neither is constant. For now, assume they're equally cheap,
3831 unless one has side effects. If both strings have constant lengths,
3838 else if (TREE_SIDE_EFFECTS (len1
))
3840 else if (TREE_SIDE_EFFECTS (len2
))
3842 else if (TREE_CODE (len1
) != INTEGER_CST
)
3844 else if (TREE_CODE (len2
) != INTEGER_CST
)
3846 else if (tree_int_cst_lt (len1
, len2
))
3851 /* If both arguments have side effects, we cannot optimize. */
3852 if (!len
|| TREE_SIDE_EFFECTS (len
))
3855 arg3_rtx
= expand_normal (len
);
3857 /* Make a place to write the result of the instruction. */
3860 && REG_P (result
) && GET_MODE (result
) == insn_mode
3861 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3862 result
= gen_reg_rtx (insn_mode
);
3864 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3865 GEN_INT (MIN (arg1_align
, arg2_align
)));
3871 enum machine_mode mode
;
3874 /* Return the value in the proper mode for this function. */
3875 mode
= TYPE_MODE (TREE_TYPE (exp
));
3876 if (GET_MODE (result
) == mode
)
3879 return convert_to_mode (mode
, result
, 0);
3880 convert_move (target
, result
, 0);
3884 /* Expand the library call ourselves using a stabilized argument
3885 list to avoid re-evaluating the function's arguments twice. */
3886 #ifdef HAVE_cmpstrnsi
3889 fndecl
= get_callee_fndecl (exp
);
3890 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 2, arg1
, arg2
);
3891 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
3892 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
3893 return expand_call (fn
, target
, target
== const0_rtx
);
3899 /* Expand expression EXP, which is a call to the strncmp builtin. Return
3900 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
3901 the result in TARGET, if convenient. */
3904 expand_builtin_strncmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
3905 ATTRIBUTE_UNUSED
enum machine_mode mode
)
3907 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
3909 if (!validate_arglist (exp
,
3910 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3913 /* If c_strlen can determine an expression for one of the string
3914 lengths, and it doesn't have side effects, then emit cmpstrnsi
3915 using length MIN(strlen(string)+1, arg3). */
3916 #ifdef HAVE_cmpstrnsi
3919 tree len
, len1
, len2
;
3920 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3923 tree arg1
= CALL_EXPR_ARG (exp
, 0);
3924 tree arg2
= CALL_EXPR_ARG (exp
, 1);
3925 tree arg3
= CALL_EXPR_ARG (exp
, 2);
3927 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
3928 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
3929 enum machine_mode insn_mode
3930 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
3932 len1
= c_strlen (arg1
, 1);
3933 len2
= c_strlen (arg2
, 1);
3936 len1
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len1
);
3938 len2
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len2
);
3940 /* If we don't have a constant length for the first, use the length
3941 of the second, if we know it. We don't require a constant for
3942 this case; some cost analysis could be done if both are available
3943 but neither is constant. For now, assume they're equally cheap,
3944 unless one has side effects. If both strings have constant lengths,
3951 else if (TREE_SIDE_EFFECTS (len1
))
3953 else if (TREE_SIDE_EFFECTS (len2
))
3955 else if (TREE_CODE (len1
) != INTEGER_CST
)
3957 else if (TREE_CODE (len2
) != INTEGER_CST
)
3959 else if (tree_int_cst_lt (len1
, len2
))
3964 /* If both arguments have side effects, we cannot optimize. */
3965 if (!len
|| TREE_SIDE_EFFECTS (len
))
3968 /* The actual new length parameter is MIN(len,arg3). */
3969 len
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (len
), len
,
3970 fold_convert_loc (loc
, TREE_TYPE (len
), arg3
));
3972 /* If we don't have POINTER_TYPE, call the function. */
3973 if (arg1_align
== 0 || arg2_align
== 0)
3976 /* Make a place to write the result of the instruction. */
3979 && REG_P (result
) && GET_MODE (result
) == insn_mode
3980 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3981 result
= gen_reg_rtx (insn_mode
);
3983 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
3984 arg1
= builtin_save_expr (arg1
);
3985 arg2
= builtin_save_expr (arg2
);
3986 len
= builtin_save_expr (len
);
3988 arg1_rtx
= get_memory_rtx (arg1
, len
);
3989 arg2_rtx
= get_memory_rtx (arg2
, len
);
3990 arg3_rtx
= expand_normal (len
);
3991 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3992 GEN_INT (MIN (arg1_align
, arg2_align
)));
3997 /* Return the value in the proper mode for this function. */
3998 mode
= TYPE_MODE (TREE_TYPE (exp
));
3999 if (GET_MODE (result
) == mode
)
4002 return convert_to_mode (mode
, result
, 0);
4003 convert_move (target
, result
, 0);
4007 /* Expand the library call ourselves using a stabilized argument
4008 list to avoid re-evaluating the function's arguments twice. */
4009 fndecl
= get_callee_fndecl (exp
);
4010 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 3,
4012 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4013 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4014 return expand_call (fn
, target
, target
== const0_rtx
);
4020 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4021 if that's convenient. */
4024 expand_builtin_saveregs (void)
4028 /* Don't do __builtin_saveregs more than once in a function.
4029 Save the result of the first call and reuse it. */
4030 if (saveregs_value
!= 0)
4031 return saveregs_value
;
4033 /* When this function is called, it means that registers must be
4034 saved on entry to this function. So we migrate the call to the
4035 first insn of this function. */
4039 /* Do whatever the machine needs done in this case. */
4040 val
= targetm
.calls
.expand_builtin_saveregs ();
4045 saveregs_value
= val
;
4047 /* Put the insns after the NOTE that starts the function. If this
4048 is inside a start_sequence, make the outer-level insn chain current, so
4049 the code is placed at the start of the function. */
4050 push_topmost_sequence ();
4051 emit_insn_after (seq
, entry_of_function ());
4052 pop_topmost_sequence ();
4057 /* Expand a call to __builtin_next_arg. */
4060 expand_builtin_next_arg (void)
4062 /* Checking arguments is already done in fold_builtin_next_arg
4063 that must be called before this function. */
4064 return expand_binop (ptr_mode
, add_optab
,
4065 crtl
->args
.internal_arg_pointer
,
4066 crtl
->args
.arg_offset_rtx
,
4067 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
4070 /* Make it easier for the backends by protecting the valist argument
4071 from multiple evaluations. */
4074 stabilize_va_list_loc (location_t loc
, tree valist
, int needs_lvalue
)
4076 tree vatype
= targetm
.canonical_va_list_type (TREE_TYPE (valist
));
4078 /* The current way of determining the type of valist is completely
4079 bogus. We should have the information on the va builtin instead. */
4081 vatype
= targetm
.fn_abi_va_list (cfun
->decl
);
4083 if (TREE_CODE (vatype
) == ARRAY_TYPE
)
4085 if (TREE_SIDE_EFFECTS (valist
))
4086 valist
= save_expr (valist
);
4088 /* For this case, the backends will be expecting a pointer to
4089 vatype, but it's possible we've actually been given an array
4090 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4092 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4094 tree p1
= build_pointer_type (TREE_TYPE (vatype
));
4095 valist
= build_fold_addr_expr_with_type_loc (loc
, valist
, p1
);
4100 tree pt
= build_pointer_type (vatype
);
4104 if (! TREE_SIDE_EFFECTS (valist
))
4107 valist
= fold_build1_loc (loc
, ADDR_EXPR
, pt
, valist
);
4108 TREE_SIDE_EFFECTS (valist
) = 1;
4111 if (TREE_SIDE_EFFECTS (valist
))
4112 valist
= save_expr (valist
);
4113 valist
= fold_build2_loc (loc
, MEM_REF
,
4114 vatype
, valist
, build_int_cst (pt
, 0));
4120 /* The "standard" definition of va_list is void*. */
4123 std_build_builtin_va_list (void)
4125 return ptr_type_node
;
4128 /* The "standard" abi va_list is va_list_type_node. */
4131 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED
)
4133 return va_list_type_node
;
4136 /* The "standard" type of va_list is va_list_type_node. */
4139 std_canonical_va_list_type (tree type
)
4143 if (INDIRECT_REF_P (type
))
4144 type
= TREE_TYPE (type
);
4145 else if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (TREE_TYPE(type
)))
4146 type
= TREE_TYPE (type
);
4147 wtype
= va_list_type_node
;
4149 /* Treat structure va_list types. */
4150 if (TREE_CODE (wtype
) == RECORD_TYPE
&& POINTER_TYPE_P (htype
))
4151 htype
= TREE_TYPE (htype
);
4152 else if (TREE_CODE (wtype
) == ARRAY_TYPE
)
4154 /* If va_list is an array type, the argument may have decayed
4155 to a pointer type, e.g. by being passed to another function.
4156 In that case, unwrap both types so that we can compare the
4157 underlying records. */
4158 if (TREE_CODE (htype
) == ARRAY_TYPE
4159 || POINTER_TYPE_P (htype
))
4161 wtype
= TREE_TYPE (wtype
);
4162 htype
= TREE_TYPE (htype
);
4165 if (TYPE_MAIN_VARIANT (wtype
) == TYPE_MAIN_VARIANT (htype
))
4166 return va_list_type_node
;
4171 /* The "standard" implementation of va_start: just assign `nextarg' to
4175 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
4177 rtx va_r
= expand_expr (valist
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
4178 convert_move (va_r
, nextarg
, 0);
4181 /* Expand EXP, a call to __builtin_va_start. */
4184 expand_builtin_va_start (tree exp
)
4188 location_t loc
= EXPR_LOCATION (exp
);
4190 if (call_expr_nargs (exp
) < 2)
4192 error_at (loc
, "too few arguments to function %<va_start%>");
4196 if (fold_builtin_next_arg (exp
, true))
4199 nextarg
= expand_builtin_next_arg ();
4200 valist
= stabilize_va_list_loc (loc
, CALL_EXPR_ARG (exp
, 0), 1);
4202 if (targetm
.expand_builtin_va_start
)
4203 targetm
.expand_builtin_va_start (valist
, nextarg
);
4205 std_expand_builtin_va_start (valist
, nextarg
);
4210 /* The "standard" implementation of va_arg: read the value from the
4211 current (padded) address and increment by the (padded) size. */
4214 std_gimplify_va_arg_expr (tree valist
, tree type
, gimple_seq
*pre_p
,
4217 tree addr
, t
, type_size
, rounded_size
, valist_tmp
;
4218 unsigned HOST_WIDE_INT align
, boundary
;
4221 #ifdef ARGS_GROW_DOWNWARD
4222 /* All of the alignment and movement below is for args-grow-up machines.
4223 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
4224 implement their own specialized gimplify_va_arg_expr routines. */
4228 indirect
= pass_by_reference (NULL
, TYPE_MODE (type
), type
, false);
4230 type
= build_pointer_type (type
);
4232 align
= PARM_BOUNDARY
/ BITS_PER_UNIT
;
4233 boundary
= targetm
.calls
.function_arg_boundary (TYPE_MODE (type
), type
);
4235 /* When we align parameter on stack for caller, if the parameter
4236 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
4237 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
4238 here with caller. */
4239 if (boundary
> MAX_SUPPORTED_STACK_ALIGNMENT
)
4240 boundary
= MAX_SUPPORTED_STACK_ALIGNMENT
;
4242 boundary
/= BITS_PER_UNIT
;
4244 /* Hoist the valist value into a temporary for the moment. */
4245 valist_tmp
= get_initialized_tmp_var (valist
, pre_p
, NULL
);
4247 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
4248 requires greater alignment, we must perform dynamic alignment. */
4249 if (boundary
> align
4250 && !integer_zerop (TYPE_SIZE (type
)))
4252 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist_tmp
,
4253 fold_build_pointer_plus_hwi (valist_tmp
, boundary
- 1));
4254 gimplify_and_add (t
, pre_p
);
4256 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist_tmp
,
4257 fold_build2 (BIT_AND_EXPR
, TREE_TYPE (valist
),
4259 build_int_cst (TREE_TYPE (valist
), -boundary
)));
4260 gimplify_and_add (t
, pre_p
);
4265 /* If the actual alignment is less than the alignment of the type,
4266 adjust the type accordingly so that we don't assume strict alignment
4267 when dereferencing the pointer. */
4268 boundary
*= BITS_PER_UNIT
;
4269 if (boundary
< TYPE_ALIGN (type
))
4271 type
= build_variant_type_copy (type
);
4272 TYPE_ALIGN (type
) = boundary
;
4275 /* Compute the rounded size of the type. */
4276 type_size
= size_in_bytes (type
);
4277 rounded_size
= round_up (type_size
, align
);
4279 /* Reduce rounded_size so it's sharable with the postqueue. */
4280 gimplify_expr (&rounded_size
, pre_p
, post_p
, is_gimple_val
, fb_rvalue
);
4284 if (PAD_VARARGS_DOWN
&& !integer_zerop (rounded_size
))
4286 /* Small args are padded downward. */
4287 t
= fold_build2_loc (input_location
, GT_EXPR
, sizetype
,
4288 rounded_size
, size_int (align
));
4289 t
= fold_build3 (COND_EXPR
, sizetype
, t
, size_zero_node
,
4290 size_binop (MINUS_EXPR
, rounded_size
, type_size
));
4291 addr
= fold_build_pointer_plus (addr
, t
);
4294 /* Compute new value for AP. */
4295 t
= fold_build_pointer_plus (valist_tmp
, rounded_size
);
4296 t
= build2 (MODIFY_EXPR
, TREE_TYPE (valist
), valist
, t
);
4297 gimplify_and_add (t
, pre_p
);
4299 addr
= fold_convert (build_pointer_type (type
), addr
);
4302 addr
= build_va_arg_indirect_ref (addr
);
4304 return build_va_arg_indirect_ref (addr
);
4307 /* Build an indirect-ref expression over the given TREE, which represents a
4308 piece of a va_arg() expansion. */
4310 build_va_arg_indirect_ref (tree addr
)
4312 addr
= build_simple_mem_ref_loc (EXPR_LOCATION (addr
), addr
);
4314 if (flag_mudflap
) /* Don't instrument va_arg INDIRECT_REF. */
4320 /* Return a dummy expression of type TYPE in order to keep going after an
4324 dummy_object (tree type
)
4326 tree t
= build_int_cst (build_pointer_type (type
), 0);
4327 return build2 (MEM_REF
, type
, t
, t
);
4330 /* Gimplify __builtin_va_arg, aka VA_ARG_EXPR, which is not really a
4331 builtin function, but a very special sort of operator. */
4333 enum gimplify_status
4334 gimplify_va_arg_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
4336 tree promoted_type
, have_va_type
;
4337 tree valist
= TREE_OPERAND (*expr_p
, 0);
4338 tree type
= TREE_TYPE (*expr_p
);
4340 location_t loc
= EXPR_LOCATION (*expr_p
);
4342 /* Verify that valist is of the proper type. */
4343 have_va_type
= TREE_TYPE (valist
);
4344 if (have_va_type
== error_mark_node
)
4346 have_va_type
= targetm
.canonical_va_list_type (have_va_type
);
4348 if (have_va_type
== NULL_TREE
)
4350 error_at (loc
, "first argument to %<va_arg%> not of type %<va_list%>");
4354 /* Generate a diagnostic for requesting data of a type that cannot
4355 be passed through `...' due to type promotion at the call site. */
4356 if ((promoted_type
= lang_hooks
.types
.type_promotes_to (type
))
4359 static bool gave_help
;
4362 /* Unfortunately, this is merely undefined, rather than a constraint
4363 violation, so we cannot make this an error. If this call is never
4364 executed, the program is still strictly conforming. */
4365 warned
= warning_at (loc
, 0,
4366 "%qT is promoted to %qT when passed through %<...%>",
4367 type
, promoted_type
);
4368 if (!gave_help
&& warned
)
4371 inform (loc
, "(so you should pass %qT not %qT to %<va_arg%>)",
4372 promoted_type
, type
);
4375 /* We can, however, treat "undefined" any way we please.
4376 Call abort to encourage the user to fix the program. */
4378 inform (loc
, "if this code is reached, the program will abort");
4379 /* Before the abort, allow the evaluation of the va_list
4380 expression to exit or longjmp. */
4381 gimplify_and_add (valist
, pre_p
);
4382 t
= build_call_expr_loc (loc
,
4383 builtin_decl_implicit (BUILT_IN_TRAP
), 0);
4384 gimplify_and_add (t
, pre_p
);
4386 /* This is dead code, but go ahead and finish so that the
4387 mode of the result comes out right. */
4388 *expr_p
= dummy_object (type
);
4393 /* Make it easier for the backends by protecting the valist argument
4394 from multiple evaluations. */
4395 if (TREE_CODE (have_va_type
) == ARRAY_TYPE
)
4397 /* For this case, the backends will be expecting a pointer to
4398 TREE_TYPE (abi), but it's possible we've
4399 actually been given an array (an actual TARGET_FN_ABI_VA_LIST).
4401 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4403 tree p1
= build_pointer_type (TREE_TYPE (have_va_type
));
4404 valist
= fold_convert_loc (loc
, p1
,
4405 build_fold_addr_expr_loc (loc
, valist
));
4408 gimplify_expr (&valist
, pre_p
, post_p
, is_gimple_val
, fb_rvalue
);
4411 gimplify_expr (&valist
, pre_p
, post_p
, is_gimple_min_lval
, fb_lvalue
);
4413 if (!targetm
.gimplify_va_arg_expr
)
4414 /* FIXME: Once most targets are converted we should merely
4415 assert this is non-null. */
4418 *expr_p
= targetm
.gimplify_va_arg_expr (valist
, type
, pre_p
, post_p
);
4423 /* Expand EXP, a call to __builtin_va_end. */
4426 expand_builtin_va_end (tree exp
)
4428 tree valist
= CALL_EXPR_ARG (exp
, 0);
4430 /* Evaluate for side effects, if needed. I hate macros that don't
4432 if (TREE_SIDE_EFFECTS (valist
))
4433 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4438 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4439 builtin rather than just as an assignment in stdarg.h because of the
4440 nastiness of array-type va_list types. */
4443 expand_builtin_va_copy (tree exp
)
4446 location_t loc
= EXPR_LOCATION (exp
);
4448 dst
= CALL_EXPR_ARG (exp
, 0);
4449 src
= CALL_EXPR_ARG (exp
, 1);
4451 dst
= stabilize_va_list_loc (loc
, dst
, 1);
4452 src
= stabilize_va_list_loc (loc
, src
, 0);
4454 gcc_assert (cfun
!= NULL
&& cfun
->decl
!= NULL_TREE
);
4456 if (TREE_CODE (targetm
.fn_abi_va_list (cfun
->decl
)) != ARRAY_TYPE
)
4458 t
= build2 (MODIFY_EXPR
, targetm
.fn_abi_va_list (cfun
->decl
), dst
, src
);
4459 TREE_SIDE_EFFECTS (t
) = 1;
4460 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4464 rtx dstb
, srcb
, size
;
4466 /* Evaluate to pointers. */
4467 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4468 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4469 size
= expand_expr (TYPE_SIZE_UNIT (targetm
.fn_abi_va_list (cfun
->decl
)),
4470 NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
4472 dstb
= convert_memory_address (Pmode
, dstb
);
4473 srcb
= convert_memory_address (Pmode
, srcb
);
4475 /* "Dereference" to BLKmode memories. */
4476 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
4477 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
4478 set_mem_align (dstb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4479 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
4480 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
4481 set_mem_align (srcb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4484 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
4490 /* Expand a call to one of the builtin functions __builtin_frame_address or
4491 __builtin_return_address. */
4494 expand_builtin_frame_address (tree fndecl
, tree exp
)
4496 /* The argument must be a nonnegative integer constant.
4497 It counts the number of frames to scan up the stack.
4498 The value is the return address saved in that frame. */
4499 if (call_expr_nargs (exp
) == 0)
4500 /* Warning about missing arg was already issued. */
4502 else if (! host_integerp (CALL_EXPR_ARG (exp
, 0), 1))
4504 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4505 error ("invalid argument to %<__builtin_frame_address%>");
4507 error ("invalid argument to %<__builtin_return_address%>");
4513 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
),
4514 tree_low_cst (CALL_EXPR_ARG (exp
, 0), 1));
4516 /* Some ports cannot access arbitrary stack frames. */
4519 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4520 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4522 warning (0, "unsupported argument to %<__builtin_return_address%>");
4526 /* For __builtin_frame_address, return what we've got. */
4527 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4531 && ! CONSTANT_P (tem
))
4532 tem
= copy_to_mode_reg (Pmode
, tem
);
4537 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4538 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4539 is the same as for allocate_dynamic_stack_space. */
4542 expand_builtin_alloca (tree exp
, bool cannot_accumulate
)
4548 bool alloca_with_align
= (DECL_FUNCTION_CODE (get_callee_fndecl (exp
))
4549 == BUILT_IN_ALLOCA_WITH_ALIGN
);
4551 /* Emit normal call if we use mudflap. */
4556 = (alloca_with_align
4557 ? validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
4558 : validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
));
4563 /* Compute the argument. */
4564 op0
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4566 /* Compute the alignment. */
4567 align
= (alloca_with_align
4568 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp
, 1))
4569 : BIGGEST_ALIGNMENT
);
4571 /* Allocate the desired space. */
4572 result
= allocate_dynamic_stack_space (op0
, 0, align
, cannot_accumulate
);
4573 result
= convert_memory_address (ptr_mode
, result
);
4578 /* Expand a call to a bswap builtin with argument ARG0. MODE
4579 is the mode to expand with. */
4582 expand_builtin_bswap (tree exp
, rtx target
, rtx subtarget
)
4584 enum machine_mode mode
;
4588 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4591 arg
= CALL_EXPR_ARG (exp
, 0);
4592 mode
= TYPE_MODE (TREE_TYPE (arg
));
4593 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4595 target
= expand_unop (mode
, bswap_optab
, op0
, target
, 1);
4597 gcc_assert (target
);
4599 return convert_to_mode (mode
, target
, 0);
4602 /* Expand a call to a unary builtin in EXP.
4603 Return NULL_RTX if a normal call should be emitted rather than expanding the
4604 function in-line. If convenient, the result should be placed in TARGET.
4605 SUBTARGET may be used as the target for computing one of EXP's operands. */
4608 expand_builtin_unop (enum machine_mode target_mode
, tree exp
, rtx target
,
4609 rtx subtarget
, optab op_optab
)
4613 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4616 /* Compute the argument. */
4617 op0
= expand_expr (CALL_EXPR_ARG (exp
, 0),
4619 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0)))
4620 == GET_MODE (subtarget
))) ? subtarget
: NULL_RTX
,
4621 VOIDmode
, EXPAND_NORMAL
);
4622 /* Compute op, into TARGET if possible.
4623 Set TARGET to wherever the result comes back. */
4624 target
= expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))),
4625 op_optab
, op0
, target
, op_optab
!= clrsb_optab
);
4626 gcc_assert (target
);
4628 return convert_to_mode (target_mode
, target
, 0);
4631 /* Expand a call to __builtin_expect. We just return our argument
4632 as the builtin_expect semantic should've been already executed by
4633 tree branch prediction pass. */
4636 expand_builtin_expect (tree exp
, rtx target
)
4640 if (call_expr_nargs (exp
) < 2)
4642 arg
= CALL_EXPR_ARG (exp
, 0);
4644 target
= expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4645 /* When guessing was done, the hints should be already stripped away. */
4646 gcc_assert (!flag_guess_branch_prob
4647 || optimize
== 0 || seen_error ());
4651 /* Expand a call to __builtin_assume_aligned. We just return our first
4652 argument as the builtin_assume_aligned semantic should've been already
4656 expand_builtin_assume_aligned (tree exp
, rtx target
)
4658 if (call_expr_nargs (exp
) < 2)
4660 target
= expand_expr (CALL_EXPR_ARG (exp
, 0), target
, VOIDmode
,
4662 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 1))
4663 && (call_expr_nargs (exp
) < 3
4664 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 2))));
4669 expand_builtin_trap (void)
4673 emit_insn (gen_trap ());
4676 emit_library_call (abort_libfunc
, LCT_NORETURN
, VOIDmode
, 0);
4680 /* Expand a call to __builtin_unreachable. We do nothing except emit
4681 a barrier saying that control flow will not pass here.
4683 It is the responsibility of the program being compiled to ensure
4684 that control flow does never reach __builtin_unreachable. */
4686 expand_builtin_unreachable (void)
4691 /* Expand EXP, a call to fabs, fabsf or fabsl.
4692 Return NULL_RTX if a normal call should be emitted rather than expanding
4693 the function inline. If convenient, the result should be placed
4694 in TARGET. SUBTARGET may be used as the target for computing
4698 expand_builtin_fabs (tree exp
, rtx target
, rtx subtarget
)
4700 enum machine_mode mode
;
4704 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4707 arg
= CALL_EXPR_ARG (exp
, 0);
4708 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
4709 mode
= TYPE_MODE (TREE_TYPE (arg
));
4710 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4711 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
4714 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4715 Return NULL is a normal call should be emitted rather than expanding the
4716 function inline. If convenient, the result should be placed in TARGET.
4717 SUBTARGET may be used as the target for computing the operand. */
4720 expand_builtin_copysign (tree exp
, rtx target
, rtx subtarget
)
4725 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
4728 arg
= CALL_EXPR_ARG (exp
, 0);
4729 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4731 arg
= CALL_EXPR_ARG (exp
, 1);
4732 op1
= expand_normal (arg
);
4734 return expand_copysign (op0
, op1
, target
);
4737 /* Create a new constant string literal and return a char* pointer to it.
4738 The STRING_CST value is the LEN characters at STR. */
4740 build_string_literal (int len
, const char *str
)
4742 tree t
, elem
, index
, type
;
4744 t
= build_string (len
, str
);
4745 elem
= build_type_variant (char_type_node
, 1, 0);
4746 index
= build_index_type (size_int (len
- 1));
4747 type
= build_array_type (elem
, index
);
4748 TREE_TYPE (t
) = type
;
4749 TREE_CONSTANT (t
) = 1;
4750 TREE_READONLY (t
) = 1;
4751 TREE_STATIC (t
) = 1;
4753 type
= build_pointer_type (elem
);
4754 t
= build1 (ADDR_EXPR
, type
,
4755 build4 (ARRAY_REF
, elem
,
4756 t
, integer_zero_node
, NULL_TREE
, NULL_TREE
));
4760 /* Expand a call to __builtin___clear_cache. */
4763 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED
)
4765 #ifndef HAVE_clear_cache
4766 #ifdef CLEAR_INSN_CACHE
4767 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4768 does something. Just do the default expansion to a call to
4772 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4773 does nothing. There is no need to call it. Do nothing. */
4775 #endif /* CLEAR_INSN_CACHE */
4777 /* We have a "clear_cache" insn, and it will handle everything. */
4779 rtx begin_rtx
, end_rtx
;
4781 /* We must not expand to a library call. If we did, any
4782 fallback library function in libgcc that might contain a call to
4783 __builtin___clear_cache() would recurse infinitely. */
4784 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4786 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4790 if (HAVE_clear_cache
)
4792 struct expand_operand ops
[2];
4794 begin
= CALL_EXPR_ARG (exp
, 0);
4795 begin_rtx
= expand_expr (begin
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4797 end
= CALL_EXPR_ARG (exp
, 1);
4798 end_rtx
= expand_expr (end
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4800 create_address_operand (&ops
[0], begin_rtx
);
4801 create_address_operand (&ops
[1], end_rtx
);
4802 if (maybe_expand_insn (CODE_FOR_clear_cache
, 2, ops
))
4806 #endif /* HAVE_clear_cache */
4809 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4812 round_trampoline_addr (rtx tramp
)
4814 rtx temp
, addend
, mask
;
4816 /* If we don't need too much alignment, we'll have been guaranteed
4817 proper alignment by get_trampoline_type. */
4818 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
4821 /* Round address up to desired boundary. */
4822 temp
= gen_reg_rtx (Pmode
);
4823 addend
= GEN_INT (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1);
4824 mask
= GEN_INT (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
);
4826 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
4827 temp
, 0, OPTAB_LIB_WIDEN
);
4828 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
4829 temp
, 0, OPTAB_LIB_WIDEN
);
4835 expand_builtin_init_trampoline (tree exp
)
4837 tree t_tramp
, t_func
, t_chain
;
4838 rtx m_tramp
, r_tramp
, r_chain
, tmp
;
4840 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
,
4841 POINTER_TYPE
, VOID_TYPE
))
4844 t_tramp
= CALL_EXPR_ARG (exp
, 0);
4845 t_func
= CALL_EXPR_ARG (exp
, 1);
4846 t_chain
= CALL_EXPR_ARG (exp
, 2);
4848 r_tramp
= expand_normal (t_tramp
);
4849 m_tramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
4850 MEM_NOTRAP_P (m_tramp
) = 1;
4852 /* The TRAMP argument should be the address of a field within the
4853 local function's FRAME decl. Let's see if we can fill in the
4854 to fill in the MEM_ATTRs for this memory. */
4855 if (TREE_CODE (t_tramp
) == ADDR_EXPR
)
4856 set_mem_attributes_minus_bitpos (m_tramp
, TREE_OPERAND (t_tramp
, 0),
4859 tmp
= round_trampoline_addr (r_tramp
);
4862 m_tramp
= change_address (m_tramp
, BLKmode
, tmp
);
4863 set_mem_align (m_tramp
, TRAMPOLINE_ALIGNMENT
);
4864 set_mem_size (m_tramp
, TRAMPOLINE_SIZE
);
4867 /* The FUNC argument should be the address of the nested function.
4868 Extract the actual function decl to pass to the hook. */
4869 gcc_assert (TREE_CODE (t_func
) == ADDR_EXPR
);
4870 t_func
= TREE_OPERAND (t_func
, 0);
4871 gcc_assert (TREE_CODE (t_func
) == FUNCTION_DECL
);
4873 r_chain
= expand_normal (t_chain
);
4875 /* Generate insns to initialize the trampoline. */
4876 targetm
.calls
.trampoline_init (m_tramp
, t_func
, r_chain
);
4878 trampolines_created
= 1;
4880 warning_at (DECL_SOURCE_LOCATION (t_func
), OPT_Wtrampolines
,
4881 "trampoline generated for nested function %qD", t_func
);
4887 expand_builtin_adjust_trampoline (tree exp
)
4891 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
4894 tramp
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4895 tramp
= round_trampoline_addr (tramp
);
4896 if (targetm
.calls
.trampoline_adjust_address
)
4897 tramp
= targetm
.calls
.trampoline_adjust_address (tramp
);
4902 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4903 function. The function first checks whether the back end provides
4904 an insn to implement signbit for the respective mode. If not, it
4905 checks whether the floating point format of the value is such that
4906 the sign bit can be extracted. If that is not the case, the
4907 function returns NULL_RTX to indicate that a normal call should be
4908 emitted rather than expanding the function in-line. EXP is the
4909 expression that is a call to the builtin function; if convenient,
4910 the result should be placed in TARGET. */
4912 expand_builtin_signbit (tree exp
, rtx target
)
4914 const struct real_format
*fmt
;
4915 enum machine_mode fmode
, imode
, rmode
;
4918 enum insn_code icode
;
4920 location_t loc
= EXPR_LOCATION (exp
);
4922 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4925 arg
= CALL_EXPR_ARG (exp
, 0);
4926 fmode
= TYPE_MODE (TREE_TYPE (arg
));
4927 rmode
= TYPE_MODE (TREE_TYPE (exp
));
4928 fmt
= REAL_MODE_FORMAT (fmode
);
4930 arg
= builtin_save_expr (arg
);
4932 /* Expand the argument yielding a RTX expression. */
4933 temp
= expand_normal (arg
);
4935 /* Check if the back end provides an insn that handles signbit for the
4937 icode
= optab_handler (signbit_optab
, fmode
);
4938 if (icode
!= CODE_FOR_nothing
)
4940 rtx last
= get_last_insn ();
4941 target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
4942 if (maybe_emit_unop_insn (icode
, target
, temp
, UNKNOWN
))
4944 delete_insns_since (last
);
4947 /* For floating point formats without a sign bit, implement signbit
4949 bitpos
= fmt
->signbit_ro
;
4952 /* But we can't do this if the format supports signed zero. */
4953 if (fmt
->has_signed_zero
&& HONOR_SIGNED_ZEROS (fmode
))
4956 arg
= fold_build2_loc (loc
, LT_EXPR
, TREE_TYPE (exp
), arg
,
4957 build_real (TREE_TYPE (arg
), dconst0
));
4958 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4961 if (GET_MODE_SIZE (fmode
) <= UNITS_PER_WORD
)
4963 imode
= int_mode_for_mode (fmode
);
4964 if (imode
== BLKmode
)
4966 temp
= gen_lowpart (imode
, temp
);
4971 /* Handle targets with different FP word orders. */
4972 if (FLOAT_WORDS_BIG_ENDIAN
)
4973 word
= (GET_MODE_BITSIZE (fmode
) - bitpos
) / BITS_PER_WORD
;
4975 word
= bitpos
/ BITS_PER_WORD
;
4976 temp
= operand_subword_force (temp
, word
, fmode
);
4977 bitpos
= bitpos
% BITS_PER_WORD
;
4980 /* Force the intermediate word_mode (or narrower) result into a
4981 register. This avoids attempting to create paradoxical SUBREGs
4982 of floating point modes below. */
4983 temp
= force_reg (imode
, temp
);
4985 /* If the bitpos is within the "result mode" lowpart, the operation
4986 can be implement with a single bitwise AND. Otherwise, we need
4987 a right shift and an AND. */
4989 if (bitpos
< GET_MODE_BITSIZE (rmode
))
4991 double_int mask
= double_int_setbit (double_int_zero
, bitpos
);
4993 if (GET_MODE_SIZE (imode
) > GET_MODE_SIZE (rmode
))
4994 temp
= gen_lowpart (rmode
, temp
);
4995 temp
= expand_binop (rmode
, and_optab
, temp
,
4996 immed_double_int_const (mask
, rmode
),
4997 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5001 /* Perform a logical right shift to place the signbit in the least
5002 significant bit, then truncate the result to the desired mode
5003 and mask just this bit. */
5004 temp
= expand_shift (RSHIFT_EXPR
, imode
, temp
, bitpos
, NULL_RTX
, 1);
5005 temp
= gen_lowpart (rmode
, temp
);
5006 temp
= expand_binop (rmode
, and_optab
, temp
, const1_rtx
,
5007 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5013 /* Expand fork or exec calls. TARGET is the desired target of the
5014 call. EXP is the call. FN is the
5015 identificator of the actual function. IGNORE is nonzero if the
5016 value is to be ignored. */
5019 expand_builtin_fork_or_exec (tree fn
, tree exp
, rtx target
, int ignore
)
5024 /* If we are not profiling, just call the function. */
5025 if (!profile_arc_flag
)
5028 /* Otherwise call the wrapper. This should be equivalent for the rest of
5029 compiler, so the code does not diverge, and the wrapper may run the
5030 code necessary for keeping the profiling sane. */
5032 switch (DECL_FUNCTION_CODE (fn
))
5035 id
= get_identifier ("__gcov_fork");
5038 case BUILT_IN_EXECL
:
5039 id
= get_identifier ("__gcov_execl");
5042 case BUILT_IN_EXECV
:
5043 id
= get_identifier ("__gcov_execv");
5046 case BUILT_IN_EXECLP
:
5047 id
= get_identifier ("__gcov_execlp");
5050 case BUILT_IN_EXECLE
:
5051 id
= get_identifier ("__gcov_execle");
5054 case BUILT_IN_EXECVP
:
5055 id
= get_identifier ("__gcov_execvp");
5058 case BUILT_IN_EXECVE
:
5059 id
= get_identifier ("__gcov_execve");
5066 decl
= build_decl (DECL_SOURCE_LOCATION (fn
),
5067 FUNCTION_DECL
, id
, TREE_TYPE (fn
));
5068 DECL_EXTERNAL (decl
) = 1;
5069 TREE_PUBLIC (decl
) = 1;
5070 DECL_ARTIFICIAL (decl
) = 1;
5071 TREE_NOTHROW (decl
) = 1;
5072 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
5073 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
5074 call
= rewrite_call_expr (EXPR_LOCATION (exp
), exp
, 0, decl
, 0);
5075 return expand_call (call
, target
, ignore
);
5080 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5081 the pointer in these functions is void*, the tree optimizers may remove
5082 casts. The mode computed in expand_builtin isn't reliable either, due
5083 to __sync_bool_compare_and_swap.
5085 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5086 group of builtins. This gives us log2 of the mode size. */
5088 static inline enum machine_mode
5089 get_builtin_sync_mode (int fcode_diff
)
5091 /* The size is not negotiable, so ask not to get BLKmode in return
5092 if the target indicates that a smaller size would be better. */
5093 return mode_for_size (BITS_PER_UNIT
<< fcode_diff
, MODE_INT
, 0);
5096 /* Expand the memory expression LOC and return the appropriate memory operand
5097 for the builtin_sync operations. */
5100 get_builtin_sync_mem (tree loc
, enum machine_mode mode
)
5104 addr
= expand_expr (loc
, NULL_RTX
, ptr_mode
, EXPAND_SUM
);
5105 addr
= convert_memory_address (Pmode
, addr
);
5107 /* Note that we explicitly do not want any alias information for this
5108 memory, so that we kill all other live memories. Otherwise we don't
5109 satisfy the full barrier semantics of the intrinsic. */
5110 mem
= validize_mem (gen_rtx_MEM (mode
, addr
));
5112 /* The alignment needs to be at least according to that of the mode. */
5113 set_mem_align (mem
, MAX (GET_MODE_ALIGNMENT (mode
),
5114 get_pointer_alignment (loc
)));
5115 set_mem_alias_set (mem
, ALIAS_SET_MEMORY_BARRIER
);
5116 MEM_VOLATILE_P (mem
) = 1;
5121 /* Make sure an argument is in the right mode.
5122 EXP is the tree argument.
5123 MODE is the mode it should be in. */
5126 expand_expr_force_mode (tree exp
, enum machine_mode mode
)
5129 enum machine_mode old_mode
;
5131 val
= expand_expr (exp
, NULL_RTX
, mode
, EXPAND_NORMAL
);
5132 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5133 of CONST_INTs, where we know the old_mode only from the call argument. */
5135 old_mode
= GET_MODE (val
);
5136 if (old_mode
== VOIDmode
)
5137 old_mode
= TYPE_MODE (TREE_TYPE (exp
));
5138 val
= convert_modes (mode
, old_mode
, val
, 1);
5143 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5144 EXP is the CALL_EXPR. CODE is the rtx code
5145 that corresponds to the arithmetic or logical operation from the name;
5146 an exception here is that NOT actually means NAND. TARGET is an optional
5147 place for us to store the results; AFTER is true if this is the
5148 fetch_and_xxx form. */
5151 expand_builtin_sync_operation (enum machine_mode mode
, tree exp
,
5152 enum rtx_code code
, bool after
,
5156 location_t loc
= EXPR_LOCATION (exp
);
5158 if (code
== NOT
&& warn_sync_nand
)
5160 tree fndecl
= get_callee_fndecl (exp
);
5161 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5163 static bool warned_f_a_n
, warned_n_a_f
;
5167 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
5168 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
5169 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
5170 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
5171 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
5175 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N
);
5176 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5177 warned_f_a_n
= true;
5180 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
5181 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
5182 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
5183 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
5184 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
5188 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N
);
5189 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5190 warned_n_a_f
= true;
5198 /* Expand the operands. */
5199 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5200 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5202 return expand_atomic_fetch_op (target
, mem
, val
, code
, MEMMODEL_SEQ_CST
,
5206 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5207 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5208 true if this is the boolean form. TARGET is a place for us to store the
5209 results; this is NOT optional if IS_BOOL is true. */
5212 expand_builtin_compare_and_swap (enum machine_mode mode
, tree exp
,
5213 bool is_bool
, rtx target
)
5215 rtx old_val
, new_val
, mem
;
5218 /* Expand the operands. */
5219 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5220 old_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5221 new_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5223 pbool
= poval
= NULL
;
5224 if (target
!= const0_rtx
)
5231 if (!expand_atomic_compare_and_swap (pbool
, poval
, mem
, old_val
, new_val
,
5232 false, MEMMODEL_SEQ_CST
,
5239 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5240 general form is actually an atomic exchange, and some targets only
5241 support a reduced form with the second argument being a constant 1.
5242 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5246 expand_builtin_sync_lock_test_and_set (enum machine_mode mode
, tree exp
,
5251 /* Expand the operands. */
5252 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5253 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5255 return expand_sync_lock_test_and_set (target
, mem
, val
);
5258 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5261 expand_builtin_sync_lock_release (enum machine_mode mode
, tree exp
)
5265 /* Expand the operands. */
5266 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5268 expand_atomic_store (mem
, const0_rtx
, MEMMODEL_RELEASE
, true);
5271 /* Given an integer representing an ``enum memmodel'', verify its
5272 correctness and return the memory model enum. */
5274 static enum memmodel
5275 get_memmodel (tree exp
)
5279 /* If the parameter is not a constant, it's a run time value so we'll just
5280 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5281 if (TREE_CODE (exp
) != INTEGER_CST
)
5282 return MEMMODEL_SEQ_CST
;
5284 op
= expand_normal (exp
);
5285 if (INTVAL (op
) < 0 || INTVAL (op
) >= MEMMODEL_LAST
)
5287 warning (OPT_Winvalid_memory_model
,
5288 "invalid memory model argument to builtin");
5289 return MEMMODEL_SEQ_CST
;
5291 return (enum memmodel
) INTVAL (op
);
5294 /* Expand the __atomic_exchange intrinsic:
5295 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5296 EXP is the CALL_EXPR.
5297 TARGET is an optional place for us to store the results. */
5300 expand_builtin_atomic_exchange (enum machine_mode mode
, tree exp
, rtx target
)
5303 enum memmodel model
;
5305 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5306 if (model
== MEMMODEL_CONSUME
)
5308 error ("invalid memory model for %<__atomic_exchange%>");
5312 if (!flag_inline_atomics
)
5315 /* Expand the operands. */
5316 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5317 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5319 return expand_atomic_exchange (target
, mem
, val
, model
);
5322 /* Expand the __atomic_compare_exchange intrinsic:
5323 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5324 TYPE desired, BOOL weak,
5325 enum memmodel success,
5326 enum memmodel failure)
5327 EXP is the CALL_EXPR.
5328 TARGET is an optional place for us to store the results. */
5331 expand_builtin_atomic_compare_exchange (enum machine_mode mode
, tree exp
,
5334 rtx expect
, desired
, mem
, oldval
;
5335 enum memmodel success
, failure
;
5339 success
= get_memmodel (CALL_EXPR_ARG (exp
, 4));
5340 failure
= get_memmodel (CALL_EXPR_ARG (exp
, 5));
5342 if (failure
== MEMMODEL_RELEASE
|| failure
== MEMMODEL_ACQ_REL
)
5344 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5348 if (failure
> success
)
5350 error ("failure memory model cannot be stronger than success "
5351 "memory model for %<__atomic_compare_exchange%>");
5355 if (!flag_inline_atomics
)
5358 /* Expand the operands. */
5359 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5361 expect
= expand_normal (CALL_EXPR_ARG (exp
, 1));
5362 expect
= convert_memory_address (Pmode
, expect
);
5363 desired
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5365 weak
= CALL_EXPR_ARG (exp
, 3);
5367 if (host_integerp (weak
, 0) && tree_low_cst (weak
, 0) != 0)
5370 oldval
= copy_to_reg (gen_rtx_MEM (mode
, expect
));
5372 if (!expand_atomic_compare_and_swap ((target
== const0_rtx
? NULL
: &target
),
5373 &oldval
, mem
, oldval
, desired
,
5374 is_weak
, success
, failure
))
5377 emit_move_insn (gen_rtx_MEM (mode
, expect
), oldval
);
5381 /* Expand the __atomic_load intrinsic:
5382 TYPE __atomic_load (TYPE *object, enum memmodel)
5383 EXP is the CALL_EXPR.
5384 TARGET is an optional place for us to store the results. */
5387 expand_builtin_atomic_load (enum machine_mode mode
, tree exp
, rtx target
)
5390 enum memmodel model
;
5392 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5393 if (model
== MEMMODEL_RELEASE
5394 || model
== MEMMODEL_ACQ_REL
)
5396 error ("invalid memory model for %<__atomic_load%>");
5400 if (!flag_inline_atomics
)
5403 /* Expand the operand. */
5404 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5406 return expand_atomic_load (target
, mem
, model
);
5410 /* Expand the __atomic_store intrinsic:
5411 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5412 EXP is the CALL_EXPR.
5413 TARGET is an optional place for us to store the results. */
5416 expand_builtin_atomic_store (enum machine_mode mode
, tree exp
)
5419 enum memmodel model
;
5421 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5422 if (model
!= MEMMODEL_RELAXED
5423 && model
!= MEMMODEL_SEQ_CST
5424 && model
!= MEMMODEL_RELEASE
)
5426 error ("invalid memory model for %<__atomic_store%>");
5430 if (!flag_inline_atomics
)
5433 /* Expand the operands. */
5434 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5435 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5437 return expand_atomic_store (mem
, val
, model
, false);
5440 /* Expand the __atomic_fetch_XXX intrinsic:
5441 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5442 EXP is the CALL_EXPR.
5443 TARGET is an optional place for us to store the results.
5444 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5445 FETCH_AFTER is true if returning the result of the operation.
5446 FETCH_AFTER is false if returning the value before the operation.
5447 IGNORE is true if the result is not used.
5448 EXT_CALL is the correct builtin for an external call if this cannot be
5449 resolved to an instruction sequence. */
5452 expand_builtin_atomic_fetch_op (enum machine_mode mode
, tree exp
, rtx target
,
5453 enum rtx_code code
, bool fetch_after
,
5454 bool ignore
, enum built_in_function ext_call
)
5457 enum memmodel model
;
5461 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5463 /* Expand the operands. */
5464 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5465 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5467 /* Only try generating instructions if inlining is turned on. */
5468 if (flag_inline_atomics
)
5470 ret
= expand_atomic_fetch_op (target
, mem
, val
, code
, model
, fetch_after
);
5475 /* Return if a different routine isn't needed for the library call. */
5476 if (ext_call
== BUILT_IN_NONE
)
5479 /* Change the call to the specified function. */
5480 fndecl
= get_callee_fndecl (exp
);
5481 addr
= CALL_EXPR_FN (exp
);
5484 gcc_assert (TREE_OPERAND (addr
, 0) == fndecl
);
5485 TREE_OPERAND (addr
, 0) = builtin_decl_explicit(ext_call
);
5487 /* Expand the call here so we can emit trailing code. */
5488 ret
= expand_call (exp
, target
, ignore
);
5490 /* Replace the original function just in case it matters. */
5491 TREE_OPERAND (addr
, 0) = fndecl
;
5493 /* Then issue the arithmetic correction to return the right result. */
5498 ret
= expand_simple_binop (mode
, AND
, ret
, val
, NULL_RTX
, true,
5500 ret
= expand_simple_unop (mode
, NOT
, ret
, target
, true);
5503 ret
= expand_simple_binop (mode
, code
, ret
, val
, target
, true,
5510 #ifndef HAVE_atomic_clear
5511 # define HAVE_atomic_clear 0
5512 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5515 /* Expand an atomic clear operation.
5516 void _atomic_clear (BOOL *obj, enum memmodel)
5517 EXP is the call expression. */
5520 expand_builtin_atomic_clear (tree exp
)
5522 enum machine_mode mode
;
5524 enum memmodel model
;
5526 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5527 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5528 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5530 if (model
== MEMMODEL_ACQUIRE
|| model
== MEMMODEL_ACQ_REL
)
5532 error ("invalid memory model for %<__atomic_store%>");
5536 if (HAVE_atomic_clear
)
5538 emit_insn (gen_atomic_clear (mem
, model
));
5542 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5543 Failing that, a store is issued by __atomic_store. The only way this can
5544 fail is if the bool type is larger than a word size. Unlikely, but
5545 handle it anyway for completeness. Assume a single threaded model since
5546 there is no atomic support in this case, and no barriers are required. */
5547 ret
= expand_atomic_store (mem
, const0_rtx
, model
, true);
5549 emit_move_insn (mem
, const0_rtx
);
5553 /* Expand an atomic test_and_set operation.
5554 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5555 EXP is the call expression. */
5558 expand_builtin_atomic_test_and_set (tree exp
, rtx target
)
5561 enum memmodel model
;
5562 enum machine_mode mode
;
5564 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5565 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5566 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5568 return expand_atomic_test_and_set (target
, mem
, model
);
5572 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5573 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5576 fold_builtin_atomic_always_lock_free (tree arg0
, tree arg1
)
5579 enum machine_mode mode
;
5580 unsigned int mode_align
, type_align
;
5582 if (TREE_CODE (arg0
) != INTEGER_CST
)
5585 size
= INTVAL (expand_normal (arg0
)) * BITS_PER_UNIT
;
5586 mode
= mode_for_size (size
, MODE_INT
, 0);
5587 mode_align
= GET_MODE_ALIGNMENT (mode
);
5589 if (TREE_CODE (arg1
) == INTEGER_CST
&& INTVAL (expand_normal (arg1
)) == 0)
5590 type_align
= mode_align
;
5593 tree ttype
= TREE_TYPE (arg1
);
5595 /* This function is usually invoked and folded immediately by the front
5596 end before anything else has a chance to look at it. The pointer
5597 parameter at this point is usually cast to a void *, so check for that
5598 and look past the cast. */
5599 if (TREE_CODE (arg1
) == NOP_EXPR
&& POINTER_TYPE_P (ttype
)
5600 && VOID_TYPE_P (TREE_TYPE (ttype
)))
5601 arg1
= TREE_OPERAND (arg1
, 0);
5603 ttype
= TREE_TYPE (arg1
);
5604 gcc_assert (POINTER_TYPE_P (ttype
));
5606 /* Get the underlying type of the object. */
5607 ttype
= TREE_TYPE (ttype
);
5608 type_align
= TYPE_ALIGN (ttype
);
5611 /* If the object has smaller alignment, the the lock free routines cannot
5613 if (type_align
< mode_align
)
5614 return integer_zero_node
;
5616 /* Check if a compare_and_swap pattern exists for the mode which represents
5617 the required size. The pattern is not allowed to fail, so the existence
5618 of the pattern indicates support is present. */
5619 if (can_compare_and_swap_p (mode
, true))
5620 return integer_one_node
;
5622 return integer_zero_node
;
5625 /* Return true if the parameters to call EXP represent an object which will
5626 always generate lock free instructions. The first argument represents the
5627 size of the object, and the second parameter is a pointer to the object
5628 itself. If NULL is passed for the object, then the result is based on
5629 typical alignment for an object of the specified size. Otherwise return
5633 expand_builtin_atomic_always_lock_free (tree exp
)
5636 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5637 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5639 if (TREE_CODE (arg0
) != INTEGER_CST
)
5641 error ("non-constant argument 1 to __atomic_always_lock_free");
5645 size
= fold_builtin_atomic_always_lock_free (arg0
, arg1
);
5646 if (size
== integer_one_node
)
5651 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5652 is lock free on this architecture. */
5655 fold_builtin_atomic_is_lock_free (tree arg0
, tree arg1
)
5657 if (!flag_inline_atomics
)
5660 /* If it isn't always lock free, don't generate a result. */
5661 if (fold_builtin_atomic_always_lock_free (arg0
, arg1
) == integer_one_node
)
5662 return integer_one_node
;
5667 /* Return true if the parameters to call EXP represent an object which will
5668 always generate lock free instructions. The first argument represents the
5669 size of the object, and the second parameter is a pointer to the object
5670 itself. If NULL is passed for the object, then the result is based on
5671 typical alignment for an object of the specified size. Otherwise return
5675 expand_builtin_atomic_is_lock_free (tree exp
)
5678 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5679 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5681 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
5683 error ("non-integer argument 1 to __atomic_is_lock_free");
5687 if (!flag_inline_atomics
)
5690 /* If the value is known at compile time, return the RTX for it. */
5691 size
= fold_builtin_atomic_is_lock_free (arg0
, arg1
);
5692 if (size
== integer_one_node
)
5698 /* Expand the __atomic_thread_fence intrinsic:
5699 void __atomic_thread_fence (enum memmodel)
5700 EXP is the CALL_EXPR. */
5703 expand_builtin_atomic_thread_fence (tree exp
)
5705 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5706 expand_mem_thread_fence (model
);
5709 /* Expand the __atomic_signal_fence intrinsic:
5710 void __atomic_signal_fence (enum memmodel)
5711 EXP is the CALL_EXPR. */
5714 expand_builtin_atomic_signal_fence (tree exp
)
5716 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5717 expand_mem_signal_fence (model
);
5720 /* Expand the __sync_synchronize intrinsic. */
5723 expand_builtin_sync_synchronize (void)
5725 expand_mem_thread_fence (MEMMODEL_SEQ_CST
);
5729 /* Expand an expression EXP that calls a built-in function,
5730 with result going to TARGET if that's convenient
5731 (and in mode MODE if that's convenient).
5732 SUBTARGET may be used as the target for computing one of EXP's operands.
5733 IGNORE is nonzero if the value is to be ignored. */
5736 expand_builtin (tree exp
, rtx target
, rtx subtarget
, enum machine_mode mode
,
5739 tree fndecl
= get_callee_fndecl (exp
);
5740 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5741 enum machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
5744 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5745 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
5747 /* When not optimizing, generate calls to library functions for a certain
5750 && !called_as_built_in (fndecl
)
5751 && DECL_ASSEMBLER_NAME_SET_P (fndecl
)
5752 && fcode
!= BUILT_IN_ALLOCA
5753 && fcode
!= BUILT_IN_ALLOCA_WITH_ALIGN
5754 && fcode
!= BUILT_IN_FREE
)
5755 return expand_call (exp
, target
, ignore
);
5757 /* The built-in function expanders test for target == const0_rtx
5758 to determine whether the function's result will be ignored. */
5760 target
= const0_rtx
;
5762 /* If the result of a pure or const built-in function is ignored, and
5763 none of its arguments are volatile, we can avoid expanding the
5764 built-in call and just evaluate the arguments for side-effects. */
5765 if (target
== const0_rtx
5766 && ((flags
= flags_from_decl_or_type (fndecl
)) & (ECF_CONST
| ECF_PURE
))
5767 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
5769 bool volatilep
= false;
5771 call_expr_arg_iterator iter
;
5773 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5774 if (TREE_THIS_VOLATILE (arg
))
5782 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5783 expand_expr (arg
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5790 CASE_FLT_FN (BUILT_IN_FABS
):
5791 target
= expand_builtin_fabs (exp
, target
, subtarget
);
5796 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
5797 target
= expand_builtin_copysign (exp
, target
, subtarget
);
5802 /* Just do a normal library call if we were unable to fold
5804 CASE_FLT_FN (BUILT_IN_CABS
):
5807 CASE_FLT_FN (BUILT_IN_EXP
):
5808 CASE_FLT_FN (BUILT_IN_EXP10
):
5809 CASE_FLT_FN (BUILT_IN_POW10
):
5810 CASE_FLT_FN (BUILT_IN_EXP2
):
5811 CASE_FLT_FN (BUILT_IN_EXPM1
):
5812 CASE_FLT_FN (BUILT_IN_LOGB
):
5813 CASE_FLT_FN (BUILT_IN_LOG
):
5814 CASE_FLT_FN (BUILT_IN_LOG10
):
5815 CASE_FLT_FN (BUILT_IN_LOG2
):
5816 CASE_FLT_FN (BUILT_IN_LOG1P
):
5817 CASE_FLT_FN (BUILT_IN_TAN
):
5818 CASE_FLT_FN (BUILT_IN_ASIN
):
5819 CASE_FLT_FN (BUILT_IN_ACOS
):
5820 CASE_FLT_FN (BUILT_IN_ATAN
):
5821 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
5822 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5823 because of possible accuracy problems. */
5824 if (! flag_unsafe_math_optimizations
)
5826 CASE_FLT_FN (BUILT_IN_SQRT
):
5827 CASE_FLT_FN (BUILT_IN_FLOOR
):
5828 CASE_FLT_FN (BUILT_IN_CEIL
):
5829 CASE_FLT_FN (BUILT_IN_TRUNC
):
5830 CASE_FLT_FN (BUILT_IN_ROUND
):
5831 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
5832 CASE_FLT_FN (BUILT_IN_RINT
):
5833 target
= expand_builtin_mathfn (exp
, target
, subtarget
);
5838 CASE_FLT_FN (BUILT_IN_FMA
):
5839 target
= expand_builtin_mathfn_ternary (exp
, target
, subtarget
);
5844 CASE_FLT_FN (BUILT_IN_ILOGB
):
5845 if (! flag_unsafe_math_optimizations
)
5847 CASE_FLT_FN (BUILT_IN_ISINF
):
5848 CASE_FLT_FN (BUILT_IN_FINITE
):
5849 case BUILT_IN_ISFINITE
:
5850 case BUILT_IN_ISNORMAL
:
5851 target
= expand_builtin_interclass_mathfn (exp
, target
);
5856 CASE_FLT_FN (BUILT_IN_ICEIL
):
5857 CASE_FLT_FN (BUILT_IN_LCEIL
):
5858 CASE_FLT_FN (BUILT_IN_LLCEIL
):
5859 CASE_FLT_FN (BUILT_IN_LFLOOR
):
5860 CASE_FLT_FN (BUILT_IN_IFLOOR
):
5861 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
5862 target
= expand_builtin_int_roundingfn (exp
, target
);
5867 CASE_FLT_FN (BUILT_IN_IRINT
):
5868 CASE_FLT_FN (BUILT_IN_LRINT
):
5869 CASE_FLT_FN (BUILT_IN_LLRINT
):
5870 CASE_FLT_FN (BUILT_IN_IROUND
):
5871 CASE_FLT_FN (BUILT_IN_LROUND
):
5872 CASE_FLT_FN (BUILT_IN_LLROUND
):
5873 target
= expand_builtin_int_roundingfn_2 (exp
, target
);
5878 CASE_FLT_FN (BUILT_IN_POWI
):
5879 target
= expand_builtin_powi (exp
, target
);
5884 CASE_FLT_FN (BUILT_IN_ATAN2
):
5885 CASE_FLT_FN (BUILT_IN_LDEXP
):
5886 CASE_FLT_FN (BUILT_IN_SCALB
):
5887 CASE_FLT_FN (BUILT_IN_SCALBN
):
5888 CASE_FLT_FN (BUILT_IN_SCALBLN
):
5889 if (! flag_unsafe_math_optimizations
)
5892 CASE_FLT_FN (BUILT_IN_FMOD
):
5893 CASE_FLT_FN (BUILT_IN_REMAINDER
):
5894 CASE_FLT_FN (BUILT_IN_DREM
):
5895 CASE_FLT_FN (BUILT_IN_POW
):
5896 target
= expand_builtin_mathfn_2 (exp
, target
, subtarget
);
5901 CASE_FLT_FN (BUILT_IN_CEXPI
):
5902 target
= expand_builtin_cexpi (exp
, target
);
5903 gcc_assert (target
);
5906 CASE_FLT_FN (BUILT_IN_SIN
):
5907 CASE_FLT_FN (BUILT_IN_COS
):
5908 if (! flag_unsafe_math_optimizations
)
5910 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
5915 CASE_FLT_FN (BUILT_IN_SINCOS
):
5916 if (! flag_unsafe_math_optimizations
)
5918 target
= expand_builtin_sincos (exp
);
5923 case BUILT_IN_APPLY_ARGS
:
5924 return expand_builtin_apply_args ();
5926 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5927 FUNCTION with a copy of the parameters described by
5928 ARGUMENTS, and ARGSIZE. It returns a block of memory
5929 allocated on the stack into which is stored all the registers
5930 that might possibly be used for returning the result of a
5931 function. ARGUMENTS is the value returned by
5932 __builtin_apply_args. ARGSIZE is the number of bytes of
5933 arguments that must be copied. ??? How should this value be
5934 computed? We'll also need a safe worst case value for varargs
5936 case BUILT_IN_APPLY
:
5937 if (!validate_arglist (exp
, POINTER_TYPE
,
5938 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
5939 && !validate_arglist (exp
, REFERENCE_TYPE
,
5940 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
5946 ops
[0] = expand_normal (CALL_EXPR_ARG (exp
, 0));
5947 ops
[1] = expand_normal (CALL_EXPR_ARG (exp
, 1));
5948 ops
[2] = expand_normal (CALL_EXPR_ARG (exp
, 2));
5950 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
5953 /* __builtin_return (RESULT) causes the function to return the
5954 value described by RESULT. RESULT is address of the block of
5955 memory returned by __builtin_apply. */
5956 case BUILT_IN_RETURN
:
5957 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
5958 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp
, 0)));
5961 case BUILT_IN_SAVEREGS
:
5962 return expand_builtin_saveregs ();
5964 case BUILT_IN_VA_ARG_PACK
:
5965 /* All valid uses of __builtin_va_arg_pack () are removed during
5967 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp
);
5970 case BUILT_IN_VA_ARG_PACK_LEN
:
5971 /* All valid uses of __builtin_va_arg_pack_len () are removed during
5973 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp
);
5976 /* Return the address of the first anonymous stack arg. */
5977 case BUILT_IN_NEXT_ARG
:
5978 if (fold_builtin_next_arg (exp
, false))
5980 return expand_builtin_next_arg ();
5982 case BUILT_IN_CLEAR_CACHE
:
5983 target
= expand_builtin___clear_cache (exp
);
5988 case BUILT_IN_CLASSIFY_TYPE
:
5989 return expand_builtin_classify_type (exp
);
5991 case BUILT_IN_CONSTANT_P
:
5994 case BUILT_IN_FRAME_ADDRESS
:
5995 case BUILT_IN_RETURN_ADDRESS
:
5996 return expand_builtin_frame_address (fndecl
, exp
);
5998 /* Returns the address of the area where the structure is returned.
6000 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
6001 if (call_expr_nargs (exp
) != 0
6002 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
6003 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
6006 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
6008 case BUILT_IN_ALLOCA
:
6009 case BUILT_IN_ALLOCA_WITH_ALIGN
:
6010 /* If the allocation stems from the declaration of a variable-sized
6011 object, it cannot accumulate. */
6012 target
= expand_builtin_alloca (exp
, CALL_ALLOCA_FOR_VAR_P (exp
));
6017 case BUILT_IN_STACK_SAVE
:
6018 return expand_stack_save ();
6020 case BUILT_IN_STACK_RESTORE
:
6021 expand_stack_restore (CALL_EXPR_ARG (exp
, 0));
6024 case BUILT_IN_BSWAP32
:
6025 case BUILT_IN_BSWAP64
:
6026 target
= expand_builtin_bswap (exp
, target
, subtarget
);
6032 CASE_INT_FN (BUILT_IN_FFS
):
6033 case BUILT_IN_FFSIMAX
:
6034 target
= expand_builtin_unop (target_mode
, exp
, target
,
6035 subtarget
, ffs_optab
);
6040 CASE_INT_FN (BUILT_IN_CLZ
):
6041 case BUILT_IN_CLZIMAX
:
6042 target
= expand_builtin_unop (target_mode
, exp
, target
,
6043 subtarget
, clz_optab
);
6048 CASE_INT_FN (BUILT_IN_CTZ
):
6049 case BUILT_IN_CTZIMAX
:
6050 target
= expand_builtin_unop (target_mode
, exp
, target
,
6051 subtarget
, ctz_optab
);
6056 CASE_INT_FN (BUILT_IN_CLRSB
):
6057 case BUILT_IN_CLRSBIMAX
:
6058 target
= expand_builtin_unop (target_mode
, exp
, target
,
6059 subtarget
, clrsb_optab
);
6064 CASE_INT_FN (BUILT_IN_POPCOUNT
):
6065 case BUILT_IN_POPCOUNTIMAX
:
6066 target
= expand_builtin_unop (target_mode
, exp
, target
,
6067 subtarget
, popcount_optab
);
6072 CASE_INT_FN (BUILT_IN_PARITY
):
6073 case BUILT_IN_PARITYIMAX
:
6074 target
= expand_builtin_unop (target_mode
, exp
, target
,
6075 subtarget
, parity_optab
);
6080 case BUILT_IN_STRLEN
:
6081 target
= expand_builtin_strlen (exp
, target
, target_mode
);
6086 case BUILT_IN_STRCPY
:
6087 target
= expand_builtin_strcpy (exp
, target
);
6092 case BUILT_IN_STRNCPY
:
6093 target
= expand_builtin_strncpy (exp
, target
);
6098 case BUILT_IN_STPCPY
:
6099 target
= expand_builtin_stpcpy (exp
, target
, mode
);
6104 case BUILT_IN_MEMCPY
:
6105 target
= expand_builtin_memcpy (exp
, target
);
6110 case BUILT_IN_MEMPCPY
:
6111 target
= expand_builtin_mempcpy (exp
, target
, mode
);
6116 case BUILT_IN_MEMSET
:
6117 target
= expand_builtin_memset (exp
, target
, mode
);
6122 case BUILT_IN_BZERO
:
6123 target
= expand_builtin_bzero (exp
);
6128 case BUILT_IN_STRCMP
:
6129 target
= expand_builtin_strcmp (exp
, target
);
6134 case BUILT_IN_STRNCMP
:
6135 target
= expand_builtin_strncmp (exp
, target
, mode
);
6141 case BUILT_IN_MEMCMP
:
6142 target
= expand_builtin_memcmp (exp
, target
, mode
);
6147 case BUILT_IN_SETJMP
:
6148 /* This should have been lowered to the builtins below. */
6151 case BUILT_IN_SETJMP_SETUP
:
6152 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6153 and the receiver label. */
6154 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
6156 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6157 VOIDmode
, EXPAND_NORMAL
);
6158 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 1), 0);
6159 rtx label_r
= label_rtx (label
);
6161 /* This is copied from the handling of non-local gotos. */
6162 expand_builtin_setjmp_setup (buf_addr
, label_r
);
6163 nonlocal_goto_handler_labels
6164 = gen_rtx_EXPR_LIST (VOIDmode
, label_r
,
6165 nonlocal_goto_handler_labels
);
6166 /* ??? Do not let expand_label treat us as such since we would
6167 not want to be both on the list of non-local labels and on
6168 the list of forced labels. */
6169 FORCED_LABEL (label
) = 0;
6174 case BUILT_IN_SETJMP_DISPATCHER
:
6175 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6176 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6178 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 0), 0);
6179 rtx label_r
= label_rtx (label
);
6181 /* Remove the dispatcher label from the list of non-local labels
6182 since the receiver labels have been added to it above. */
6183 remove_node_from_expr_list (label_r
, &nonlocal_goto_handler_labels
);
6188 case BUILT_IN_SETJMP_RECEIVER
:
6189 /* __builtin_setjmp_receiver is passed the receiver label. */
6190 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6192 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 0), 0);
6193 rtx label_r
= label_rtx (label
);
6195 expand_builtin_setjmp_receiver (label_r
);
6200 /* __builtin_longjmp is passed a pointer to an array of five words.
6201 It's similar to the C library longjmp function but works with
6202 __builtin_setjmp above. */
6203 case BUILT_IN_LONGJMP
:
6204 if (validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6206 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6207 VOIDmode
, EXPAND_NORMAL
);
6208 rtx value
= expand_normal (CALL_EXPR_ARG (exp
, 1));
6210 if (value
!= const1_rtx
)
6212 error ("%<__builtin_longjmp%> second argument must be 1");
6216 expand_builtin_longjmp (buf_addr
, value
);
6221 case BUILT_IN_NONLOCAL_GOTO
:
6222 target
= expand_builtin_nonlocal_goto (exp
);
6227 /* This updates the setjmp buffer that is its argument with the value
6228 of the current stack pointer. */
6229 case BUILT_IN_UPDATE_SETJMP_BUF
:
6230 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6233 = expand_normal (CALL_EXPR_ARG (exp
, 0));
6235 expand_builtin_update_setjmp_buf (buf_addr
);
6241 expand_builtin_trap ();
6244 case BUILT_IN_UNREACHABLE
:
6245 expand_builtin_unreachable ();
6248 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
6249 case BUILT_IN_SIGNBITD32
:
6250 case BUILT_IN_SIGNBITD64
:
6251 case BUILT_IN_SIGNBITD128
:
6252 target
= expand_builtin_signbit (exp
, target
);
6257 /* Various hooks for the DWARF 2 __throw routine. */
6258 case BUILT_IN_UNWIND_INIT
:
6259 expand_builtin_unwind_init ();
6261 case BUILT_IN_DWARF_CFA
:
6262 return virtual_cfa_rtx
;
6263 #ifdef DWARF2_UNWIND_INFO
6264 case BUILT_IN_DWARF_SP_COLUMN
:
6265 return expand_builtin_dwarf_sp_column ();
6266 case BUILT_IN_INIT_DWARF_REG_SIZES
:
6267 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp
, 0));
6270 case BUILT_IN_FROB_RETURN_ADDR
:
6271 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp
, 0));
6272 case BUILT_IN_EXTRACT_RETURN_ADDR
:
6273 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp
, 0));
6274 case BUILT_IN_EH_RETURN
:
6275 expand_builtin_eh_return (CALL_EXPR_ARG (exp
, 0),
6276 CALL_EXPR_ARG (exp
, 1));
6278 #ifdef EH_RETURN_DATA_REGNO
6279 case BUILT_IN_EH_RETURN_DATA_REGNO
:
6280 return expand_builtin_eh_return_data_regno (exp
);
6282 case BUILT_IN_EXTEND_POINTER
:
6283 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp
, 0));
6284 case BUILT_IN_EH_POINTER
:
6285 return expand_builtin_eh_pointer (exp
);
6286 case BUILT_IN_EH_FILTER
:
6287 return expand_builtin_eh_filter (exp
);
6288 case BUILT_IN_EH_COPY_VALUES
:
6289 return expand_builtin_eh_copy_values (exp
);
6291 case BUILT_IN_VA_START
:
6292 return expand_builtin_va_start (exp
);
6293 case BUILT_IN_VA_END
:
6294 return expand_builtin_va_end (exp
);
6295 case BUILT_IN_VA_COPY
:
6296 return expand_builtin_va_copy (exp
);
6297 case BUILT_IN_EXPECT
:
6298 return expand_builtin_expect (exp
, target
);
6299 case BUILT_IN_ASSUME_ALIGNED
:
6300 return expand_builtin_assume_aligned (exp
, target
);
6301 case BUILT_IN_PREFETCH
:
6302 expand_builtin_prefetch (exp
);
6305 case BUILT_IN_INIT_TRAMPOLINE
:
6306 return expand_builtin_init_trampoline (exp
);
6307 case BUILT_IN_ADJUST_TRAMPOLINE
:
6308 return expand_builtin_adjust_trampoline (exp
);
6311 case BUILT_IN_EXECL
:
6312 case BUILT_IN_EXECV
:
6313 case BUILT_IN_EXECLP
:
6314 case BUILT_IN_EXECLE
:
6315 case BUILT_IN_EXECVP
:
6316 case BUILT_IN_EXECVE
:
6317 target
= expand_builtin_fork_or_exec (fndecl
, exp
, target
, ignore
);
6322 case BUILT_IN_SYNC_FETCH_AND_ADD_1
:
6323 case BUILT_IN_SYNC_FETCH_AND_ADD_2
:
6324 case BUILT_IN_SYNC_FETCH_AND_ADD_4
:
6325 case BUILT_IN_SYNC_FETCH_AND_ADD_8
:
6326 case BUILT_IN_SYNC_FETCH_AND_ADD_16
:
6327 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_ADD_1
);
6328 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, false, target
);
6333 case BUILT_IN_SYNC_FETCH_AND_SUB_1
:
6334 case BUILT_IN_SYNC_FETCH_AND_SUB_2
:
6335 case BUILT_IN_SYNC_FETCH_AND_SUB_4
:
6336 case BUILT_IN_SYNC_FETCH_AND_SUB_8
:
6337 case BUILT_IN_SYNC_FETCH_AND_SUB_16
:
6338 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_SUB_1
);
6339 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, false, target
);
6344 case BUILT_IN_SYNC_FETCH_AND_OR_1
:
6345 case BUILT_IN_SYNC_FETCH_AND_OR_2
:
6346 case BUILT_IN_SYNC_FETCH_AND_OR_4
:
6347 case BUILT_IN_SYNC_FETCH_AND_OR_8
:
6348 case BUILT_IN_SYNC_FETCH_AND_OR_16
:
6349 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_OR_1
);
6350 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, false, target
);
6355 case BUILT_IN_SYNC_FETCH_AND_AND_1
:
6356 case BUILT_IN_SYNC_FETCH_AND_AND_2
:
6357 case BUILT_IN_SYNC_FETCH_AND_AND_4
:
6358 case BUILT_IN_SYNC_FETCH_AND_AND_8
:
6359 case BUILT_IN_SYNC_FETCH_AND_AND_16
:
6360 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_AND_1
);
6361 target
= expand_builtin_sync_operation (mode
, exp
, AND
, false, target
);
6366 case BUILT_IN_SYNC_FETCH_AND_XOR_1
:
6367 case BUILT_IN_SYNC_FETCH_AND_XOR_2
:
6368 case BUILT_IN_SYNC_FETCH_AND_XOR_4
:
6369 case BUILT_IN_SYNC_FETCH_AND_XOR_8
:
6370 case BUILT_IN_SYNC_FETCH_AND_XOR_16
:
6371 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_XOR_1
);
6372 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, false, target
);
6377 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
6378 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
6379 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
6380 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
6381 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
6382 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_NAND_1
);
6383 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, false, target
);
6388 case BUILT_IN_SYNC_ADD_AND_FETCH_1
:
6389 case BUILT_IN_SYNC_ADD_AND_FETCH_2
:
6390 case BUILT_IN_SYNC_ADD_AND_FETCH_4
:
6391 case BUILT_IN_SYNC_ADD_AND_FETCH_8
:
6392 case BUILT_IN_SYNC_ADD_AND_FETCH_16
:
6393 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_ADD_AND_FETCH_1
);
6394 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, true, target
);
6399 case BUILT_IN_SYNC_SUB_AND_FETCH_1
:
6400 case BUILT_IN_SYNC_SUB_AND_FETCH_2
:
6401 case BUILT_IN_SYNC_SUB_AND_FETCH_4
:
6402 case BUILT_IN_SYNC_SUB_AND_FETCH_8
:
6403 case BUILT_IN_SYNC_SUB_AND_FETCH_16
:
6404 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_SUB_AND_FETCH_1
);
6405 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, true, target
);
6410 case BUILT_IN_SYNC_OR_AND_FETCH_1
:
6411 case BUILT_IN_SYNC_OR_AND_FETCH_2
:
6412 case BUILT_IN_SYNC_OR_AND_FETCH_4
:
6413 case BUILT_IN_SYNC_OR_AND_FETCH_8
:
6414 case BUILT_IN_SYNC_OR_AND_FETCH_16
:
6415 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_OR_AND_FETCH_1
);
6416 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, true, target
);
6421 case BUILT_IN_SYNC_AND_AND_FETCH_1
:
6422 case BUILT_IN_SYNC_AND_AND_FETCH_2
:
6423 case BUILT_IN_SYNC_AND_AND_FETCH_4
:
6424 case BUILT_IN_SYNC_AND_AND_FETCH_8
:
6425 case BUILT_IN_SYNC_AND_AND_FETCH_16
:
6426 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_AND_AND_FETCH_1
);
6427 target
= expand_builtin_sync_operation (mode
, exp
, AND
, true, target
);
6432 case BUILT_IN_SYNC_XOR_AND_FETCH_1
:
6433 case BUILT_IN_SYNC_XOR_AND_FETCH_2
:
6434 case BUILT_IN_SYNC_XOR_AND_FETCH_4
:
6435 case BUILT_IN_SYNC_XOR_AND_FETCH_8
:
6436 case BUILT_IN_SYNC_XOR_AND_FETCH_16
:
6437 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_XOR_AND_FETCH_1
);
6438 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, true, target
);
6443 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
6444 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
6445 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
6446 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
6447 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
6448 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_NAND_AND_FETCH_1
);
6449 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, true, target
);
6454 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
6455 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
6456 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
6457 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
6458 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
6459 if (mode
== VOIDmode
)
6460 mode
= TYPE_MODE (boolean_type_node
);
6461 if (!target
|| !register_operand (target
, mode
))
6462 target
= gen_reg_rtx (mode
);
6464 mode
= get_builtin_sync_mode
6465 (fcode
- BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
);
6466 target
= expand_builtin_compare_and_swap (mode
, exp
, true, target
);
6471 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
:
6472 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2
:
6473 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4
:
6474 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8
:
6475 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16
:
6476 mode
= get_builtin_sync_mode
6477 (fcode
- BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
);
6478 target
= expand_builtin_compare_and_swap (mode
, exp
, false, target
);
6483 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
:
6484 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2
:
6485 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4
:
6486 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8
:
6487 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16
:
6488 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
);
6489 target
= expand_builtin_sync_lock_test_and_set (mode
, exp
, target
);
6494 case BUILT_IN_SYNC_LOCK_RELEASE_1
:
6495 case BUILT_IN_SYNC_LOCK_RELEASE_2
:
6496 case BUILT_IN_SYNC_LOCK_RELEASE_4
:
6497 case BUILT_IN_SYNC_LOCK_RELEASE_8
:
6498 case BUILT_IN_SYNC_LOCK_RELEASE_16
:
6499 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_RELEASE_1
);
6500 expand_builtin_sync_lock_release (mode
, exp
);
6503 case BUILT_IN_SYNC_SYNCHRONIZE
:
6504 expand_builtin_sync_synchronize ();
6507 case BUILT_IN_ATOMIC_EXCHANGE_1
:
6508 case BUILT_IN_ATOMIC_EXCHANGE_2
:
6509 case BUILT_IN_ATOMIC_EXCHANGE_4
:
6510 case BUILT_IN_ATOMIC_EXCHANGE_8
:
6511 case BUILT_IN_ATOMIC_EXCHANGE_16
:
6512 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_EXCHANGE_1
);
6513 target
= expand_builtin_atomic_exchange (mode
, exp
, target
);
6518 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
6519 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
6520 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
6521 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
6522 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
6524 unsigned int nargs
, z
;
6528 get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
);
6529 target
= expand_builtin_atomic_compare_exchange (mode
, exp
, target
);
6533 /* If this is turned into an external library call, the weak parameter
6534 must be dropped to match the expected parameter list. */
6535 nargs
= call_expr_nargs (exp
);
6536 vec
= VEC_alloc (tree
, gc
, nargs
- 1);
6537 for (z
= 0; z
< 3; z
++)
6538 VEC_quick_push (tree
, vec
, CALL_EXPR_ARG (exp
, z
));
6539 /* Skip the boolean weak parameter. */
6540 for (z
= 4; z
< 6; z
++)
6541 VEC_quick_push (tree
, vec
, CALL_EXPR_ARG (exp
, z
));
6542 exp
= build_call_vec (TREE_TYPE (exp
), CALL_EXPR_FN (exp
), vec
);
6546 case BUILT_IN_ATOMIC_LOAD_1
:
6547 case BUILT_IN_ATOMIC_LOAD_2
:
6548 case BUILT_IN_ATOMIC_LOAD_4
:
6549 case BUILT_IN_ATOMIC_LOAD_8
:
6550 case BUILT_IN_ATOMIC_LOAD_16
:
6551 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_LOAD_1
);
6552 target
= expand_builtin_atomic_load (mode
, exp
, target
);
6557 case BUILT_IN_ATOMIC_STORE_1
:
6558 case BUILT_IN_ATOMIC_STORE_2
:
6559 case BUILT_IN_ATOMIC_STORE_4
:
6560 case BUILT_IN_ATOMIC_STORE_8
:
6561 case BUILT_IN_ATOMIC_STORE_16
:
6562 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_STORE_1
);
6563 target
= expand_builtin_atomic_store (mode
, exp
);
6568 case BUILT_IN_ATOMIC_ADD_FETCH_1
:
6569 case BUILT_IN_ATOMIC_ADD_FETCH_2
:
6570 case BUILT_IN_ATOMIC_ADD_FETCH_4
:
6571 case BUILT_IN_ATOMIC_ADD_FETCH_8
:
6572 case BUILT_IN_ATOMIC_ADD_FETCH_16
:
6574 enum built_in_function lib
;
6575 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
);
6576 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_ADD_1
+
6577 (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
));
6578 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, true,
6584 case BUILT_IN_ATOMIC_SUB_FETCH_1
:
6585 case BUILT_IN_ATOMIC_SUB_FETCH_2
:
6586 case BUILT_IN_ATOMIC_SUB_FETCH_4
:
6587 case BUILT_IN_ATOMIC_SUB_FETCH_8
:
6588 case BUILT_IN_ATOMIC_SUB_FETCH_16
:
6590 enum built_in_function lib
;
6591 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
);
6592 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_SUB_1
+
6593 (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
));
6594 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, true,
6600 case BUILT_IN_ATOMIC_AND_FETCH_1
:
6601 case BUILT_IN_ATOMIC_AND_FETCH_2
:
6602 case BUILT_IN_ATOMIC_AND_FETCH_4
:
6603 case BUILT_IN_ATOMIC_AND_FETCH_8
:
6604 case BUILT_IN_ATOMIC_AND_FETCH_16
:
6606 enum built_in_function lib
;
6607 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
);
6608 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_AND_1
+
6609 (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
));
6610 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, true,
6616 case BUILT_IN_ATOMIC_NAND_FETCH_1
:
6617 case BUILT_IN_ATOMIC_NAND_FETCH_2
:
6618 case BUILT_IN_ATOMIC_NAND_FETCH_4
:
6619 case BUILT_IN_ATOMIC_NAND_FETCH_8
:
6620 case BUILT_IN_ATOMIC_NAND_FETCH_16
:
6622 enum built_in_function lib
;
6623 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
);
6624 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_NAND_1
+
6625 (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
));
6626 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, true,
6632 case BUILT_IN_ATOMIC_XOR_FETCH_1
:
6633 case BUILT_IN_ATOMIC_XOR_FETCH_2
:
6634 case BUILT_IN_ATOMIC_XOR_FETCH_4
:
6635 case BUILT_IN_ATOMIC_XOR_FETCH_8
:
6636 case BUILT_IN_ATOMIC_XOR_FETCH_16
:
6638 enum built_in_function lib
;
6639 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
);
6640 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_XOR_1
+
6641 (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
));
6642 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, true,
6648 case BUILT_IN_ATOMIC_OR_FETCH_1
:
6649 case BUILT_IN_ATOMIC_OR_FETCH_2
:
6650 case BUILT_IN_ATOMIC_OR_FETCH_4
:
6651 case BUILT_IN_ATOMIC_OR_FETCH_8
:
6652 case BUILT_IN_ATOMIC_OR_FETCH_16
:
6654 enum built_in_function lib
;
6655 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
);
6656 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_OR_1
+
6657 (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
));
6658 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, true,
6664 case BUILT_IN_ATOMIC_FETCH_ADD_1
:
6665 case BUILT_IN_ATOMIC_FETCH_ADD_2
:
6666 case BUILT_IN_ATOMIC_FETCH_ADD_4
:
6667 case BUILT_IN_ATOMIC_FETCH_ADD_8
:
6668 case BUILT_IN_ATOMIC_FETCH_ADD_16
:
6669 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_ADD_1
);
6670 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, false,
6671 ignore
, BUILT_IN_NONE
);
6676 case BUILT_IN_ATOMIC_FETCH_SUB_1
:
6677 case BUILT_IN_ATOMIC_FETCH_SUB_2
:
6678 case BUILT_IN_ATOMIC_FETCH_SUB_4
:
6679 case BUILT_IN_ATOMIC_FETCH_SUB_8
:
6680 case BUILT_IN_ATOMIC_FETCH_SUB_16
:
6681 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_SUB_1
);
6682 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, false,
6683 ignore
, BUILT_IN_NONE
);
6688 case BUILT_IN_ATOMIC_FETCH_AND_1
:
6689 case BUILT_IN_ATOMIC_FETCH_AND_2
:
6690 case BUILT_IN_ATOMIC_FETCH_AND_4
:
6691 case BUILT_IN_ATOMIC_FETCH_AND_8
:
6692 case BUILT_IN_ATOMIC_FETCH_AND_16
:
6693 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_AND_1
);
6694 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, false,
6695 ignore
, BUILT_IN_NONE
);
6700 case BUILT_IN_ATOMIC_FETCH_NAND_1
:
6701 case BUILT_IN_ATOMIC_FETCH_NAND_2
:
6702 case BUILT_IN_ATOMIC_FETCH_NAND_4
:
6703 case BUILT_IN_ATOMIC_FETCH_NAND_8
:
6704 case BUILT_IN_ATOMIC_FETCH_NAND_16
:
6705 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_NAND_1
);
6706 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, false,
6707 ignore
, BUILT_IN_NONE
);
6712 case BUILT_IN_ATOMIC_FETCH_XOR_1
:
6713 case BUILT_IN_ATOMIC_FETCH_XOR_2
:
6714 case BUILT_IN_ATOMIC_FETCH_XOR_4
:
6715 case BUILT_IN_ATOMIC_FETCH_XOR_8
:
6716 case BUILT_IN_ATOMIC_FETCH_XOR_16
:
6717 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_XOR_1
);
6718 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, false,
6719 ignore
, BUILT_IN_NONE
);
6724 case BUILT_IN_ATOMIC_FETCH_OR_1
:
6725 case BUILT_IN_ATOMIC_FETCH_OR_2
:
6726 case BUILT_IN_ATOMIC_FETCH_OR_4
:
6727 case BUILT_IN_ATOMIC_FETCH_OR_8
:
6728 case BUILT_IN_ATOMIC_FETCH_OR_16
:
6729 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_OR_1
);
6730 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, false,
6731 ignore
, BUILT_IN_NONE
);
6736 case BUILT_IN_ATOMIC_TEST_AND_SET
:
6737 return expand_builtin_atomic_test_and_set (exp
, target
);
6739 case BUILT_IN_ATOMIC_CLEAR
:
6740 return expand_builtin_atomic_clear (exp
);
6742 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
6743 return expand_builtin_atomic_always_lock_free (exp
);
6745 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
6746 target
= expand_builtin_atomic_is_lock_free (exp
);
6751 case BUILT_IN_ATOMIC_THREAD_FENCE
:
6752 expand_builtin_atomic_thread_fence (exp
);
6755 case BUILT_IN_ATOMIC_SIGNAL_FENCE
:
6756 expand_builtin_atomic_signal_fence (exp
);
6759 case BUILT_IN_OBJECT_SIZE
:
6760 return expand_builtin_object_size (exp
);
6762 case BUILT_IN_MEMCPY_CHK
:
6763 case BUILT_IN_MEMPCPY_CHK
:
6764 case BUILT_IN_MEMMOVE_CHK
:
6765 case BUILT_IN_MEMSET_CHK
:
6766 target
= expand_builtin_memory_chk (exp
, target
, mode
, fcode
);
6771 case BUILT_IN_STRCPY_CHK
:
6772 case BUILT_IN_STPCPY_CHK
:
6773 case BUILT_IN_STRNCPY_CHK
:
6774 case BUILT_IN_STPNCPY_CHK
:
6775 case BUILT_IN_STRCAT_CHK
:
6776 case BUILT_IN_STRNCAT_CHK
:
6777 case BUILT_IN_SNPRINTF_CHK
:
6778 case BUILT_IN_VSNPRINTF_CHK
:
6779 maybe_emit_chk_warning (exp
, fcode
);
6782 case BUILT_IN_SPRINTF_CHK
:
6783 case BUILT_IN_VSPRINTF_CHK
:
6784 maybe_emit_sprintf_chk_warning (exp
, fcode
);
6788 if (warn_free_nonheap_object
)
6789 maybe_emit_free_warning (exp
);
6792 default: /* just do library call, if unknown builtin */
6796 /* The switch statement above can drop through to cause the function
6797 to be called normally. */
6798 return expand_call (exp
, target
, ignore
);
6801 /* Determine whether a tree node represents a call to a built-in
6802 function. If the tree T is a call to a built-in function with
6803 the right number of arguments of the appropriate types, return
6804 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6805 Otherwise the return value is END_BUILTINS. */
6807 enum built_in_function
6808 builtin_mathfn_code (const_tree t
)
6810 const_tree fndecl
, arg
, parmlist
;
6811 const_tree argtype
, parmtype
;
6812 const_call_expr_arg_iterator iter
;
6814 if (TREE_CODE (t
) != CALL_EXPR
6815 || TREE_CODE (CALL_EXPR_FN (t
)) != ADDR_EXPR
)
6816 return END_BUILTINS
;
6818 fndecl
= get_callee_fndecl (t
);
6819 if (fndecl
== NULL_TREE
6820 || TREE_CODE (fndecl
) != FUNCTION_DECL
6821 || ! DECL_BUILT_IN (fndecl
)
6822 || DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
6823 return END_BUILTINS
;
6825 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
6826 init_const_call_expr_arg_iterator (t
, &iter
);
6827 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
6829 /* If a function doesn't take a variable number of arguments,
6830 the last element in the list will have type `void'. */
6831 parmtype
= TREE_VALUE (parmlist
);
6832 if (VOID_TYPE_P (parmtype
))
6834 if (more_const_call_expr_args_p (&iter
))
6835 return END_BUILTINS
;
6836 return DECL_FUNCTION_CODE (fndecl
);
6839 if (! more_const_call_expr_args_p (&iter
))
6840 return END_BUILTINS
;
6842 arg
= next_const_call_expr_arg (&iter
);
6843 argtype
= TREE_TYPE (arg
);
6845 if (SCALAR_FLOAT_TYPE_P (parmtype
))
6847 if (! SCALAR_FLOAT_TYPE_P (argtype
))
6848 return END_BUILTINS
;
6850 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
6852 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
6853 return END_BUILTINS
;
6855 else if (POINTER_TYPE_P (parmtype
))
6857 if (! POINTER_TYPE_P (argtype
))
6858 return END_BUILTINS
;
6860 else if (INTEGRAL_TYPE_P (parmtype
))
6862 if (! INTEGRAL_TYPE_P (argtype
))
6863 return END_BUILTINS
;
6866 return END_BUILTINS
;
6869 /* Variable-length argument list. */
6870 return DECL_FUNCTION_CODE (fndecl
);
6873 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6874 evaluate to a constant. */
6877 fold_builtin_constant_p (tree arg
)
6879 /* We return 1 for a numeric type that's known to be a constant
6880 value at compile-time or for an aggregate type that's a
6881 literal constant. */
6884 /* If we know this is a constant, emit the constant of one. */
6885 if (CONSTANT_CLASS_P (arg
)
6886 || (TREE_CODE (arg
) == CONSTRUCTOR
6887 && TREE_CONSTANT (arg
)))
6888 return integer_one_node
;
6889 if (TREE_CODE (arg
) == ADDR_EXPR
)
6891 tree op
= TREE_OPERAND (arg
, 0);
6892 if (TREE_CODE (op
) == STRING_CST
6893 || (TREE_CODE (op
) == ARRAY_REF
6894 && integer_zerop (TREE_OPERAND (op
, 1))
6895 && TREE_CODE (TREE_OPERAND (op
, 0)) == STRING_CST
))
6896 return integer_one_node
;
6899 /* If this expression has side effects, show we don't know it to be a
6900 constant. Likewise if it's a pointer or aggregate type since in
6901 those case we only want literals, since those are only optimized
6902 when generating RTL, not later.
6903 And finally, if we are compiling an initializer, not code, we
6904 need to return a definite result now; there's not going to be any
6905 more optimization done. */
6906 if (TREE_SIDE_EFFECTS (arg
)
6907 || AGGREGATE_TYPE_P (TREE_TYPE (arg
))
6908 || POINTER_TYPE_P (TREE_TYPE (arg
))
6910 || folding_initializer
)
6911 return integer_zero_node
;
6916 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6917 return it as a truthvalue. */
6920 build_builtin_expect_predicate (location_t loc
, tree pred
, tree expected
)
6922 tree fn
, arg_types
, pred_type
, expected_type
, call_expr
, ret_type
;
6924 fn
= builtin_decl_explicit (BUILT_IN_EXPECT
);
6925 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
6926 ret_type
= TREE_TYPE (TREE_TYPE (fn
));
6927 pred_type
= TREE_VALUE (arg_types
);
6928 expected_type
= TREE_VALUE (TREE_CHAIN (arg_types
));
6930 pred
= fold_convert_loc (loc
, pred_type
, pred
);
6931 expected
= fold_convert_loc (loc
, expected_type
, expected
);
6932 call_expr
= build_call_expr_loc (loc
, fn
, 2, pred
, expected
);
6934 return build2 (NE_EXPR
, TREE_TYPE (pred
), call_expr
,
6935 build_int_cst (ret_type
, 0));
6938 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6939 NULL_TREE if no simplification is possible. */
6942 fold_builtin_expect (location_t loc
, tree arg0
, tree arg1
)
6944 tree inner
, fndecl
, inner_arg0
;
6945 enum tree_code code
;
6947 /* Distribute the expected value over short-circuiting operators.
6948 See through the cast from truthvalue_type_node to long. */
6950 while (TREE_CODE (inner_arg0
) == NOP_EXPR
6951 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0
))
6952 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0
, 0))))
6953 inner_arg0
= TREE_OPERAND (inner_arg0
, 0);
6955 /* If this is a builtin_expect within a builtin_expect keep the
6956 inner one. See through a comparison against a constant. It
6957 might have been added to create a thruthvalue. */
6960 if (COMPARISON_CLASS_P (inner
)
6961 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
)
6962 inner
= TREE_OPERAND (inner
, 0);
6964 if (TREE_CODE (inner
) == CALL_EXPR
6965 && (fndecl
= get_callee_fndecl (inner
))
6966 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
6967 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
)
6971 code
= TREE_CODE (inner
);
6972 if (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
6974 tree op0
= TREE_OPERAND (inner
, 0);
6975 tree op1
= TREE_OPERAND (inner
, 1);
6977 op0
= build_builtin_expect_predicate (loc
, op0
, arg1
);
6978 op1
= build_builtin_expect_predicate (loc
, op1
, arg1
);
6979 inner
= build2 (code
, TREE_TYPE (inner
), op0
, op1
);
6981 return fold_convert_loc (loc
, TREE_TYPE (arg0
), inner
);
6984 /* If the argument isn't invariant then there's nothing else we can do. */
6985 if (!TREE_CONSTANT (inner_arg0
))
6988 /* If we expect that a comparison against the argument will fold to
6989 a constant return the constant. In practice, this means a true
6990 constant or the address of a non-weak symbol. */
6993 if (TREE_CODE (inner
) == ADDR_EXPR
)
6997 inner
= TREE_OPERAND (inner
, 0);
6999 while (TREE_CODE (inner
) == COMPONENT_REF
7000 || TREE_CODE (inner
) == ARRAY_REF
);
7001 if ((TREE_CODE (inner
) == VAR_DECL
7002 || TREE_CODE (inner
) == FUNCTION_DECL
)
7003 && DECL_WEAK (inner
))
7007 /* Otherwise, ARG0 already has the proper type for the return value. */
7011 /* Fold a call to __builtin_classify_type with argument ARG. */
7014 fold_builtin_classify_type (tree arg
)
7017 return build_int_cst (integer_type_node
, no_type_class
);
7019 return build_int_cst (integer_type_node
, type_to_class (TREE_TYPE (arg
)));
7022 /* Fold a call to __builtin_strlen with argument ARG. */
7025 fold_builtin_strlen (location_t loc
, tree type
, tree arg
)
7027 if (!validate_arg (arg
, POINTER_TYPE
))
7031 tree len
= c_strlen (arg
, 0);
7034 return fold_convert_loc (loc
, type
, len
);
7040 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7043 fold_builtin_inf (location_t loc
, tree type
, int warn
)
7045 REAL_VALUE_TYPE real
;
7047 /* __builtin_inff is intended to be usable to define INFINITY on all
7048 targets. If an infinity is not available, INFINITY expands "to a
7049 positive constant of type float that overflows at translation
7050 time", footnote "In this case, using INFINITY will violate the
7051 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7052 Thus we pedwarn to ensure this constraint violation is
7054 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
7055 pedwarn (loc
, 0, "target format does not support infinity");
7058 return build_real (type
, real
);
7061 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7064 fold_builtin_nan (tree arg
, tree type
, int quiet
)
7066 REAL_VALUE_TYPE real
;
7069 if (!validate_arg (arg
, POINTER_TYPE
))
7071 str
= c_getstr (arg
);
7075 if (!real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
7078 return build_real (type
, real
);
7081 /* Return true if the floating point expression T has an integer value.
7082 We also allow +Inf, -Inf and NaN to be considered integer values. */
7085 integer_valued_real_p (tree t
)
7087 switch (TREE_CODE (t
))
7094 return integer_valued_real_p (TREE_OPERAND (t
, 0));
7099 return integer_valued_real_p (TREE_OPERAND (t
, 1));
7106 return integer_valued_real_p (TREE_OPERAND (t
, 0))
7107 && integer_valued_real_p (TREE_OPERAND (t
, 1));
7110 return integer_valued_real_p (TREE_OPERAND (t
, 1))
7111 && integer_valued_real_p (TREE_OPERAND (t
, 2));
7114 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
7118 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
7119 if (TREE_CODE (type
) == INTEGER_TYPE
)
7121 if (TREE_CODE (type
) == REAL_TYPE
)
7122 return integer_valued_real_p (TREE_OPERAND (t
, 0));
7127 switch (builtin_mathfn_code (t
))
7129 CASE_FLT_FN (BUILT_IN_CEIL
):
7130 CASE_FLT_FN (BUILT_IN_FLOOR
):
7131 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
7132 CASE_FLT_FN (BUILT_IN_RINT
):
7133 CASE_FLT_FN (BUILT_IN_ROUND
):
7134 CASE_FLT_FN (BUILT_IN_TRUNC
):
7137 CASE_FLT_FN (BUILT_IN_FMIN
):
7138 CASE_FLT_FN (BUILT_IN_FMAX
):
7139 return integer_valued_real_p (CALL_EXPR_ARG (t
, 0))
7140 && integer_valued_real_p (CALL_EXPR_ARG (t
, 1));
7153 /* FNDECL is assumed to be a builtin where truncation can be propagated
7154 across (for instance floor((double)f) == (double)floorf (f).
7155 Do the transformation for a call with argument ARG. */
7158 fold_trunc_transparent_mathfn (location_t loc
, tree fndecl
, tree arg
)
7160 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7162 if (!validate_arg (arg
, REAL_TYPE
))
7165 /* Integer rounding functions are idempotent. */
7166 if (fcode
== builtin_mathfn_code (arg
))
7169 /* If argument is already integer valued, and we don't need to worry
7170 about setting errno, there's no need to perform rounding. */
7171 if (! flag_errno_math
&& integer_valued_real_p (arg
))
7176 tree arg0
= strip_float_extensions (arg
);
7177 tree ftype
= TREE_TYPE (TREE_TYPE (fndecl
));
7178 tree newtype
= TREE_TYPE (arg0
);
7181 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
7182 && (decl
= mathfn_built_in (newtype
, fcode
)))
7183 return fold_convert_loc (loc
, ftype
,
7184 build_call_expr_loc (loc
, decl
, 1,
7185 fold_convert_loc (loc
,
7192 /* FNDECL is assumed to be builtin which can narrow the FP type of
7193 the argument, for instance lround((double)f) -> lroundf (f).
7194 Do the transformation for a call with argument ARG. */
7197 fold_fixed_mathfn (location_t loc
, tree fndecl
, tree arg
)
7199 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7201 if (!validate_arg (arg
, REAL_TYPE
))
7204 /* If argument is already integer valued, and we don't need to worry
7205 about setting errno, there's no need to perform rounding. */
7206 if (! flag_errno_math
&& integer_valued_real_p (arg
))
7207 return fold_build1_loc (loc
, FIX_TRUNC_EXPR
,
7208 TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
7212 tree ftype
= TREE_TYPE (arg
);
7213 tree arg0
= strip_float_extensions (arg
);
7214 tree newtype
= TREE_TYPE (arg0
);
7217 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
7218 && (decl
= mathfn_built_in (newtype
, fcode
)))
7219 return build_call_expr_loc (loc
, decl
, 1,
7220 fold_convert_loc (loc
, newtype
, arg0
));
7223 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7224 sizeof (int) == sizeof (long). */
7225 if (TYPE_PRECISION (integer_type_node
)
7226 == TYPE_PRECISION (long_integer_type_node
))
7228 tree newfn
= NULL_TREE
;
7231 CASE_FLT_FN (BUILT_IN_ICEIL
):
7232 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
7235 CASE_FLT_FN (BUILT_IN_IFLOOR
):
7236 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
7239 CASE_FLT_FN (BUILT_IN_IROUND
):
7240 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
7243 CASE_FLT_FN (BUILT_IN_IRINT
):
7244 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
7253 tree newcall
= build_call_expr_loc (loc
, newfn
, 1, arg
);
7254 return fold_convert_loc (loc
,
7255 TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
7259 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7260 sizeof (long long) == sizeof (long). */
7261 if (TYPE_PRECISION (long_long_integer_type_node
)
7262 == TYPE_PRECISION (long_integer_type_node
))
7264 tree newfn
= NULL_TREE
;
7267 CASE_FLT_FN (BUILT_IN_LLCEIL
):
7268 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
7271 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7272 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
7275 CASE_FLT_FN (BUILT_IN_LLROUND
):
7276 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
7279 CASE_FLT_FN (BUILT_IN_LLRINT
):
7280 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
7289 tree newcall
= build_call_expr_loc (loc
, newfn
, 1, arg
);
7290 return fold_convert_loc (loc
,
7291 TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
7298 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7299 return type. Return NULL_TREE if no simplification can be made. */
7302 fold_builtin_cabs (location_t loc
, tree arg
, tree type
, tree fndecl
)
7306 if (!validate_arg (arg
, COMPLEX_TYPE
)
7307 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
7310 /* Calculate the result when the argument is a constant. */
7311 if (TREE_CODE (arg
) == COMPLEX_CST
7312 && (res
= do_mpfr_arg2 (TREE_REALPART (arg
), TREE_IMAGPART (arg
),
7316 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
7318 tree real
= TREE_OPERAND (arg
, 0);
7319 tree imag
= TREE_OPERAND (arg
, 1);
7321 /* If either part is zero, cabs is fabs of the other. */
7322 if (real_zerop (real
))
7323 return fold_build1_loc (loc
, ABS_EXPR
, type
, imag
);
7324 if (real_zerop (imag
))
7325 return fold_build1_loc (loc
, ABS_EXPR
, type
, real
);
7327 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7328 if (flag_unsafe_math_optimizations
7329 && operand_equal_p (real
, imag
, OEP_PURE_SAME
))
7331 const REAL_VALUE_TYPE sqrt2_trunc
7332 = real_value_truncate (TYPE_MODE (type
), dconst_sqrt2 ());
7334 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7335 fold_build1_loc (loc
, ABS_EXPR
, type
, real
),
7336 build_real (type
, sqrt2_trunc
));
7340 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7341 if (TREE_CODE (arg
) == NEGATE_EXPR
7342 || TREE_CODE (arg
) == CONJ_EXPR
)
7343 return build_call_expr_loc (loc
, fndecl
, 1, TREE_OPERAND (arg
, 0));
7345 /* Don't do this when optimizing for size. */
7346 if (flag_unsafe_math_optimizations
7347 && optimize
&& optimize_function_for_speed_p (cfun
))
7349 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
7351 if (sqrtfn
!= NULL_TREE
)
7353 tree rpart
, ipart
, result
;
7355 arg
= builtin_save_expr (arg
);
7357 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, type
, arg
);
7358 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg
);
7360 rpart
= builtin_save_expr (rpart
);
7361 ipart
= builtin_save_expr (ipart
);
7363 result
= fold_build2_loc (loc
, PLUS_EXPR
, type
,
7364 fold_build2_loc (loc
, MULT_EXPR
, type
,
7366 fold_build2_loc (loc
, MULT_EXPR
, type
,
7369 return build_call_expr_loc (loc
, sqrtfn
, 1, result
);
7376 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7377 complex tree type of the result. If NEG is true, the imaginary
7378 zero is negative. */
7381 build_complex_cproj (tree type
, bool neg
)
7383 REAL_VALUE_TYPE rinf
, rzero
= dconst0
;
7387 return build_complex (type
, build_real (TREE_TYPE (type
), rinf
),
7388 build_real (TREE_TYPE (type
), rzero
));
7391 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7392 return type. Return NULL_TREE if no simplification can be made. */
7395 fold_builtin_cproj (location_t loc
, tree arg
, tree type
)
7397 if (!validate_arg (arg
, COMPLEX_TYPE
)
7398 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
7401 /* If there are no infinities, return arg. */
7402 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type
))))
7403 return non_lvalue_loc (loc
, arg
);
7405 /* Calculate the result when the argument is a constant. */
7406 if (TREE_CODE (arg
) == COMPLEX_CST
)
7408 const REAL_VALUE_TYPE
*real
= TREE_REAL_CST_PTR (TREE_REALPART (arg
));
7409 const REAL_VALUE_TYPE
*imag
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg
));
7411 if (real_isinf (real
) || real_isinf (imag
))
7412 return build_complex_cproj (type
, imag
->sign
);
7416 else if (TREE_CODE (arg
) == COMPLEX_EXPR
)
7418 tree real
= TREE_OPERAND (arg
, 0);
7419 tree imag
= TREE_OPERAND (arg
, 1);
7424 /* If the real part is inf and the imag part is known to be
7425 nonnegative, return (inf + 0i). Remember side-effects are
7426 possible in the imag part. */
7427 if (TREE_CODE (real
) == REAL_CST
7428 && real_isinf (TREE_REAL_CST_PTR (real
))
7429 && tree_expr_nonnegative_p (imag
))
7430 return omit_one_operand_loc (loc
, type
,
7431 build_complex_cproj (type
, false),
7434 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7435 Remember side-effects are possible in the real part. */
7436 if (TREE_CODE (imag
) == REAL_CST
7437 && real_isinf (TREE_REAL_CST_PTR (imag
)))
7439 omit_one_operand_loc (loc
, type
,
7440 build_complex_cproj (type
, TREE_REAL_CST_PTR
7441 (imag
)->sign
), arg
);
7447 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7448 Return NULL_TREE if no simplification can be made. */
7451 fold_builtin_sqrt (location_t loc
, tree arg
, tree type
)
7454 enum built_in_function fcode
;
7457 if (!validate_arg (arg
, REAL_TYPE
))
7460 /* Calculate the result when the argument is a constant. */
7461 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_sqrt
, &dconst0
, NULL
, true)))
7464 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7465 fcode
= builtin_mathfn_code (arg
);
7466 if (flag_unsafe_math_optimizations
&& BUILTIN_EXPONENT_P (fcode
))
7468 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7469 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
7470 CALL_EXPR_ARG (arg
, 0),
7471 build_real (type
, dconsthalf
));
7472 return build_call_expr_loc (loc
, expfn
, 1, arg
);
7475 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7476 if (flag_unsafe_math_optimizations
&& BUILTIN_ROOT_P (fcode
))
7478 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7482 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7484 /* The inner root was either sqrt or cbrt. */
7485 /* This was a conditional expression but it triggered a bug
7487 REAL_VALUE_TYPE dconstroot
;
7488 if (BUILTIN_SQRT_P (fcode
))
7489 dconstroot
= dconsthalf
;
7491 dconstroot
= dconst_third ();
7493 /* Adjust for the outer root. */
7494 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7495 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7496 tree_root
= build_real (type
, dconstroot
);
7497 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7501 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7502 if (flag_unsafe_math_optimizations
7503 && (fcode
== BUILT_IN_POW
7504 || fcode
== BUILT_IN_POWF
7505 || fcode
== BUILT_IN_POWL
))
7507 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7508 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7509 tree arg1
= CALL_EXPR_ARG (arg
, 1);
7511 if (!tree_expr_nonnegative_p (arg0
))
7512 arg0
= build1 (ABS_EXPR
, type
, arg0
);
7513 narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
7514 build_real (type
, dconsthalf
));
7515 return build_call_expr_loc (loc
, powfn
, 2, arg0
, narg1
);
7521 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7522 Return NULL_TREE if no simplification can be made. */
7525 fold_builtin_cbrt (location_t loc
, tree arg
, tree type
)
7527 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
7530 if (!validate_arg (arg
, REAL_TYPE
))
7533 /* Calculate the result when the argument is a constant. */
7534 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cbrt
, NULL
, NULL
, 0)))
7537 if (flag_unsafe_math_optimizations
)
7539 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7540 if (BUILTIN_EXPONENT_P (fcode
))
7542 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7543 const REAL_VALUE_TYPE third_trunc
=
7544 real_value_truncate (TYPE_MODE (type
), dconst_third ());
7545 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
7546 CALL_EXPR_ARG (arg
, 0),
7547 build_real (type
, third_trunc
));
7548 return build_call_expr_loc (loc
, expfn
, 1, arg
);
7551 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7552 if (BUILTIN_SQRT_P (fcode
))
7554 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7558 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7560 REAL_VALUE_TYPE dconstroot
= dconst_third ();
7562 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7563 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7564 tree_root
= build_real (type
, dconstroot
);
7565 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7569 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7570 if (BUILTIN_CBRT_P (fcode
))
7572 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7573 if (tree_expr_nonnegative_p (arg0
))
7575 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7580 REAL_VALUE_TYPE dconstroot
;
7582 real_arithmetic (&dconstroot
, MULT_EXPR
,
7583 dconst_third_ptr (), dconst_third_ptr ());
7584 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7585 tree_root
= build_real (type
, dconstroot
);
7586 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7591 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7592 if (fcode
== BUILT_IN_POW
7593 || fcode
== BUILT_IN_POWF
7594 || fcode
== BUILT_IN_POWL
)
7596 tree arg00
= CALL_EXPR_ARG (arg
, 0);
7597 tree arg01
= CALL_EXPR_ARG (arg
, 1);
7598 if (tree_expr_nonnegative_p (arg00
))
7600 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7601 const REAL_VALUE_TYPE dconstroot
7602 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
7603 tree narg01
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg01
,
7604 build_real (type
, dconstroot
));
7605 return build_call_expr_loc (loc
, powfn
, 2, arg00
, narg01
);
7612 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7613 TYPE is the type of the return value. Return NULL_TREE if no
7614 simplification can be made. */
7617 fold_builtin_cos (location_t loc
,
7618 tree arg
, tree type
, tree fndecl
)
7622 if (!validate_arg (arg
, REAL_TYPE
))
7625 /* Calculate the result when the argument is a constant. */
7626 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cos
, NULL
, NULL
, 0)))
7629 /* Optimize cos(-x) into cos (x). */
7630 if ((narg
= fold_strip_sign_ops (arg
)))
7631 return build_call_expr_loc (loc
, fndecl
, 1, narg
);
7636 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7637 Return NULL_TREE if no simplification can be made. */
7640 fold_builtin_cosh (location_t loc
, tree arg
, tree type
, tree fndecl
)
7642 if (validate_arg (arg
, REAL_TYPE
))
7646 /* Calculate the result when the argument is a constant. */
7647 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cosh
, NULL
, NULL
, 0)))
7650 /* Optimize cosh(-x) into cosh (x). */
7651 if ((narg
= fold_strip_sign_ops (arg
)))
7652 return build_call_expr_loc (loc
, fndecl
, 1, narg
);
7658 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7659 argument ARG. TYPE is the type of the return value. Return
7660 NULL_TREE if no simplification can be made. */
7663 fold_builtin_ccos (location_t loc
, tree arg
, tree type
, tree fndecl
,
7666 if (validate_arg (arg
, COMPLEX_TYPE
)
7667 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
7671 /* Calculate the result when the argument is a constant. */
7672 if ((tmp
= do_mpc_arg1 (arg
, type
, (hyper
? mpc_cosh
: mpc_cos
))))
7675 /* Optimize fn(-x) into fn(x). */
7676 if ((tmp
= fold_strip_sign_ops (arg
)))
7677 return build_call_expr_loc (loc
, fndecl
, 1, tmp
);
7683 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7684 Return NULL_TREE if no simplification can be made. */
7687 fold_builtin_tan (tree arg
, tree type
)
7689 enum built_in_function fcode
;
7692 if (!validate_arg (arg
, REAL_TYPE
))
7695 /* Calculate the result when the argument is a constant. */
7696 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_tan
, NULL
, NULL
, 0)))
7699 /* Optimize tan(atan(x)) = x. */
7700 fcode
= builtin_mathfn_code (arg
);
7701 if (flag_unsafe_math_optimizations
7702 && (fcode
== BUILT_IN_ATAN
7703 || fcode
== BUILT_IN_ATANF
7704 || fcode
== BUILT_IN_ATANL
))
7705 return CALL_EXPR_ARG (arg
, 0);
7710 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7711 NULL_TREE if no simplification can be made. */
7714 fold_builtin_sincos (location_t loc
,
7715 tree arg0
, tree arg1
, tree arg2
)
7720 if (!validate_arg (arg0
, REAL_TYPE
)
7721 || !validate_arg (arg1
, POINTER_TYPE
)
7722 || !validate_arg (arg2
, POINTER_TYPE
))
7725 type
= TREE_TYPE (arg0
);
7727 /* Calculate the result when the argument is a constant. */
7728 if ((res
= do_mpfr_sincos (arg0
, arg1
, arg2
)))
7731 /* Canonicalize sincos to cexpi. */
7732 if (!TARGET_C99_FUNCTIONS
)
7734 fn
= mathfn_built_in (type
, BUILT_IN_CEXPI
);
7738 call
= build_call_expr_loc (loc
, fn
, 1, arg0
);
7739 call
= builtin_save_expr (call
);
7741 return build2 (COMPOUND_EXPR
, void_type_node
,
7742 build2 (MODIFY_EXPR
, void_type_node
,
7743 build_fold_indirect_ref_loc (loc
, arg1
),
7744 build1 (IMAGPART_EXPR
, type
, call
)),
7745 build2 (MODIFY_EXPR
, void_type_node
,
7746 build_fold_indirect_ref_loc (loc
, arg2
),
7747 build1 (REALPART_EXPR
, type
, call
)));
7750 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7751 NULL_TREE if no simplification can be made. */
7754 fold_builtin_cexp (location_t loc
, tree arg0
, tree type
)
7757 tree realp
, imagp
, ifn
;
7760 if (!validate_arg (arg0
, COMPLEX_TYPE
)
7761 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) != REAL_TYPE
)
7764 /* Calculate the result when the argument is a constant. */
7765 if ((res
= do_mpc_arg1 (arg0
, type
, mpc_exp
)))
7768 rtype
= TREE_TYPE (TREE_TYPE (arg0
));
7770 /* In case we can figure out the real part of arg0 and it is constant zero
7772 if (!TARGET_C99_FUNCTIONS
)
7774 ifn
= mathfn_built_in (rtype
, BUILT_IN_CEXPI
);
7778 if ((realp
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
))
7779 && real_zerop (realp
))
7781 tree narg
= fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
7782 return build_call_expr_loc (loc
, ifn
, 1, narg
);
7785 /* In case we can easily decompose real and imaginary parts split cexp
7786 to exp (r) * cexpi (i). */
7787 if (flag_unsafe_math_optimizations
7790 tree rfn
, rcall
, icall
;
7792 rfn
= mathfn_built_in (rtype
, BUILT_IN_EXP
);
7796 imagp
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
7800 icall
= build_call_expr_loc (loc
, ifn
, 1, imagp
);
7801 icall
= builtin_save_expr (icall
);
7802 rcall
= build_call_expr_loc (loc
, rfn
, 1, realp
);
7803 rcall
= builtin_save_expr (rcall
);
7804 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
7805 fold_build2_loc (loc
, MULT_EXPR
, rtype
,
7807 fold_build1_loc (loc
, REALPART_EXPR
,
7809 fold_build2_loc (loc
, MULT_EXPR
, rtype
,
7811 fold_build1_loc (loc
, IMAGPART_EXPR
,
7818 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7819 Return NULL_TREE if no simplification can be made. */
7822 fold_builtin_trunc (location_t loc
, tree fndecl
, tree arg
)
7824 if (!validate_arg (arg
, REAL_TYPE
))
7827 /* Optimize trunc of constant value. */
7828 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7830 REAL_VALUE_TYPE r
, x
;
7831 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7833 x
= TREE_REAL_CST (arg
);
7834 real_trunc (&r
, TYPE_MODE (type
), &x
);
7835 return build_real (type
, r
);
7838 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
7841 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7842 Return NULL_TREE if no simplification can be made. */
7845 fold_builtin_floor (location_t loc
, tree fndecl
, tree arg
)
7847 if (!validate_arg (arg
, REAL_TYPE
))
7850 /* Optimize floor of constant value. */
7851 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7855 x
= TREE_REAL_CST (arg
);
7856 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7858 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7861 real_floor (&r
, TYPE_MODE (type
), &x
);
7862 return build_real (type
, r
);
7866 /* Fold floor (x) where x is nonnegative to trunc (x). */
7867 if (tree_expr_nonnegative_p (arg
))
7869 tree truncfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_TRUNC
);
7871 return build_call_expr_loc (loc
, truncfn
, 1, arg
);
7874 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
7877 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7878 Return NULL_TREE if no simplification can be made. */
7881 fold_builtin_ceil (location_t loc
, tree fndecl
, tree arg
)
7883 if (!validate_arg (arg
, REAL_TYPE
))
7886 /* Optimize ceil of constant value. */
7887 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7891 x
= TREE_REAL_CST (arg
);
7892 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7894 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7897 real_ceil (&r
, TYPE_MODE (type
), &x
);
7898 return build_real (type
, r
);
7902 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
7905 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7906 Return NULL_TREE if no simplification can be made. */
7909 fold_builtin_round (location_t loc
, tree fndecl
, tree arg
)
7911 if (!validate_arg (arg
, REAL_TYPE
))
7914 /* Optimize round of constant value. */
7915 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7919 x
= TREE_REAL_CST (arg
);
7920 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7922 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7925 real_round (&r
, TYPE_MODE (type
), &x
);
7926 return build_real (type
, r
);
7930 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
7933 /* Fold function call to builtin lround, lroundf or lroundl (or the
7934 corresponding long long versions) and other rounding functions. ARG
7935 is the argument to the call. Return NULL_TREE if no simplification
7939 fold_builtin_int_roundingfn (location_t loc
, tree fndecl
, tree arg
)
7941 if (!validate_arg (arg
, REAL_TYPE
))
7944 /* Optimize lround of constant value. */
7945 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7947 const REAL_VALUE_TYPE x
= TREE_REAL_CST (arg
);
7949 if (real_isfinite (&x
))
7951 tree itype
= TREE_TYPE (TREE_TYPE (fndecl
));
7952 tree ftype
= TREE_TYPE (arg
);
7956 switch (DECL_FUNCTION_CODE (fndecl
))
7958 CASE_FLT_FN (BUILT_IN_IFLOOR
):
7959 CASE_FLT_FN (BUILT_IN_LFLOOR
):
7960 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7961 real_floor (&r
, TYPE_MODE (ftype
), &x
);
7964 CASE_FLT_FN (BUILT_IN_ICEIL
):
7965 CASE_FLT_FN (BUILT_IN_LCEIL
):
7966 CASE_FLT_FN (BUILT_IN_LLCEIL
):
7967 real_ceil (&r
, TYPE_MODE (ftype
), &x
);
7970 CASE_FLT_FN (BUILT_IN_IROUND
):
7971 CASE_FLT_FN (BUILT_IN_LROUND
):
7972 CASE_FLT_FN (BUILT_IN_LLROUND
):
7973 real_round (&r
, TYPE_MODE (ftype
), &x
);
7980 real_to_integer2 ((HOST_WIDE_INT
*)&val
.low
, &val
.high
, &r
);
7981 if (double_int_fits_to_tree_p (itype
, val
))
7982 return double_int_to_tree (itype
, val
);
7986 switch (DECL_FUNCTION_CODE (fndecl
))
7988 CASE_FLT_FN (BUILT_IN_LFLOOR
):
7989 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7990 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
7991 if (tree_expr_nonnegative_p (arg
))
7992 return fold_build1_loc (loc
, FIX_TRUNC_EXPR
,
7993 TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
7998 return fold_fixed_mathfn (loc
, fndecl
, arg
);
8001 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8002 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8003 the argument to the call. Return NULL_TREE if no simplification can
8007 fold_builtin_bitop (tree fndecl
, tree arg
)
8009 if (!validate_arg (arg
, INTEGER_TYPE
))
8012 /* Optimize for constant argument. */
8013 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
8015 HOST_WIDE_INT hi
, width
, result
;
8016 unsigned HOST_WIDE_INT lo
;
8019 type
= TREE_TYPE (arg
);
8020 width
= TYPE_PRECISION (type
);
8021 lo
= TREE_INT_CST_LOW (arg
);
8023 /* Clear all the bits that are beyond the type's precision. */
8024 if (width
> HOST_BITS_PER_WIDE_INT
)
8026 hi
= TREE_INT_CST_HIGH (arg
);
8027 if (width
< 2 * HOST_BITS_PER_WIDE_INT
)
8028 hi
&= ~((unsigned HOST_WIDE_INT
) (-1)
8029 << (width
- HOST_BITS_PER_WIDE_INT
));
8034 if (width
< HOST_BITS_PER_WIDE_INT
)
8035 lo
&= ~((unsigned HOST_WIDE_INT
) (-1) << width
);
8038 switch (DECL_FUNCTION_CODE (fndecl
))
8040 CASE_INT_FN (BUILT_IN_FFS
):
8042 result
= ffs_hwi (lo
);
8044 result
= HOST_BITS_PER_WIDE_INT
+ ffs_hwi (hi
);
8049 CASE_INT_FN (BUILT_IN_CLZ
):
8051 result
= width
- floor_log2 (hi
) - 1 - HOST_BITS_PER_WIDE_INT
;
8053 result
= width
- floor_log2 (lo
) - 1;
8054 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
8058 CASE_INT_FN (BUILT_IN_CTZ
):
8060 result
= ctz_hwi (lo
);
8062 result
= HOST_BITS_PER_WIDE_INT
+ ctz_hwi (hi
);
8063 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
8067 CASE_INT_FN (BUILT_IN_CLRSB
):
8068 if (width
> HOST_BITS_PER_WIDE_INT
8069 && (hi
& ((unsigned HOST_WIDE_INT
) 1
8070 << (width
- HOST_BITS_PER_WIDE_INT
- 1))) != 0)
8072 hi
= ~hi
& ~((unsigned HOST_WIDE_INT
) (-1)
8073 << (width
- HOST_BITS_PER_WIDE_INT
- 1));
8076 else if (width
<= HOST_BITS_PER_WIDE_INT
8077 && (lo
& ((unsigned HOST_WIDE_INT
) 1 << (width
- 1))) != 0)
8078 lo
= ~lo
& ~((unsigned HOST_WIDE_INT
) (-1) << (width
- 1));
8080 result
= width
- floor_log2 (hi
) - 2 - HOST_BITS_PER_WIDE_INT
;
8082 result
= width
- floor_log2 (lo
) - 2;
8087 CASE_INT_FN (BUILT_IN_POPCOUNT
):
8090 result
++, lo
&= lo
- 1;
8092 result
++, hi
&= (unsigned HOST_WIDE_INT
) hi
- 1;
8095 CASE_INT_FN (BUILT_IN_PARITY
):
8098 result
++, lo
&= lo
- 1;
8100 result
++, hi
&= (unsigned HOST_WIDE_INT
) hi
- 1;
8108 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), result
);
8114 /* Fold function call to builtin_bswap and the long and long long
8115 variants. Return NULL_TREE if no simplification can be made. */
8117 fold_builtin_bswap (tree fndecl
, tree arg
)
8119 if (! validate_arg (arg
, INTEGER_TYPE
))
8122 /* Optimize constant value. */
8123 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
8125 HOST_WIDE_INT hi
, width
, r_hi
= 0;
8126 unsigned HOST_WIDE_INT lo
, r_lo
= 0;
8129 type
= TREE_TYPE (arg
);
8130 width
= TYPE_PRECISION (type
);
8131 lo
= TREE_INT_CST_LOW (arg
);
8132 hi
= TREE_INT_CST_HIGH (arg
);
8134 switch (DECL_FUNCTION_CODE (fndecl
))
8136 case BUILT_IN_BSWAP32
:
8137 case BUILT_IN_BSWAP64
:
8141 for (s
= 0; s
< width
; s
+= 8)
8143 int d
= width
- s
- 8;
8144 unsigned HOST_WIDE_INT byte
;
8146 if (s
< HOST_BITS_PER_WIDE_INT
)
8147 byte
= (lo
>> s
) & 0xff;
8149 byte
= (hi
>> (s
- HOST_BITS_PER_WIDE_INT
)) & 0xff;
8151 if (d
< HOST_BITS_PER_WIDE_INT
)
8154 r_hi
|= byte
<< (d
- HOST_BITS_PER_WIDE_INT
);
8164 if (width
< HOST_BITS_PER_WIDE_INT
)
8165 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), r_lo
);
8167 return build_int_cst_wide (TREE_TYPE (TREE_TYPE (fndecl
)), r_lo
, r_hi
);
8173 /* A subroutine of fold_builtin to fold the various logarithmic
8174 functions. Return NULL_TREE if no simplification can me made.
8175 FUNC is the corresponding MPFR logarithm function. */
8178 fold_builtin_logarithm (location_t loc
, tree fndecl
, tree arg
,
8179 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
))
8181 if (validate_arg (arg
, REAL_TYPE
))
8183 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8185 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
8187 /* Calculate the result when the argument is a constant. */
8188 if ((res
= do_mpfr_arg1 (arg
, type
, func
, &dconst0
, NULL
, false)))
8191 /* Special case, optimize logN(expN(x)) = x. */
8192 if (flag_unsafe_math_optimizations
8193 && ((func
== mpfr_log
8194 && (fcode
== BUILT_IN_EXP
8195 || fcode
== BUILT_IN_EXPF
8196 || fcode
== BUILT_IN_EXPL
))
8197 || (func
== mpfr_log2
8198 && (fcode
== BUILT_IN_EXP2
8199 || fcode
== BUILT_IN_EXP2F
8200 || fcode
== BUILT_IN_EXP2L
))
8201 || (func
== mpfr_log10
&& (BUILTIN_EXP10_P (fcode
)))))
8202 return fold_convert_loc (loc
, type
, CALL_EXPR_ARG (arg
, 0));
8204 /* Optimize logN(func()) for various exponential functions. We
8205 want to determine the value "x" and the power "exponent" in
8206 order to transform logN(x**exponent) into exponent*logN(x). */
8207 if (flag_unsafe_math_optimizations
)
8209 tree exponent
= 0, x
= 0;
8213 CASE_FLT_FN (BUILT_IN_EXP
):
8214 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8215 x
= build_real (type
, real_value_truncate (TYPE_MODE (type
),
8217 exponent
= CALL_EXPR_ARG (arg
, 0);
8219 CASE_FLT_FN (BUILT_IN_EXP2
):
8220 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8221 x
= build_real (type
, dconst2
);
8222 exponent
= CALL_EXPR_ARG (arg
, 0);
8224 CASE_FLT_FN (BUILT_IN_EXP10
):
8225 CASE_FLT_FN (BUILT_IN_POW10
):
8226 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8228 REAL_VALUE_TYPE dconst10
;
8229 real_from_integer (&dconst10
, VOIDmode
, 10, 0, 0);
8230 x
= build_real (type
, dconst10
);
8232 exponent
= CALL_EXPR_ARG (arg
, 0);
8234 CASE_FLT_FN (BUILT_IN_SQRT
):
8235 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8236 x
= CALL_EXPR_ARG (arg
, 0);
8237 exponent
= build_real (type
, dconsthalf
);
8239 CASE_FLT_FN (BUILT_IN_CBRT
):
8240 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8241 x
= CALL_EXPR_ARG (arg
, 0);
8242 exponent
= build_real (type
, real_value_truncate (TYPE_MODE (type
),
8245 CASE_FLT_FN (BUILT_IN_POW
):
8246 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8247 x
= CALL_EXPR_ARG (arg
, 0);
8248 exponent
= CALL_EXPR_ARG (arg
, 1);
8254 /* Now perform the optimization. */
8257 tree logfn
= build_call_expr_loc (loc
, fndecl
, 1, x
);
8258 return fold_build2_loc (loc
, MULT_EXPR
, type
, exponent
, logfn
);
8266 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8267 NULL_TREE if no simplification can be made. */
8270 fold_builtin_hypot (location_t loc
, tree fndecl
,
8271 tree arg0
, tree arg1
, tree type
)
8273 tree res
, narg0
, narg1
;
8275 if (!validate_arg (arg0
, REAL_TYPE
)
8276 || !validate_arg (arg1
, REAL_TYPE
))
8279 /* Calculate the result when the argument is a constant. */
8280 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_hypot
)))
8283 /* If either argument to hypot has a negate or abs, strip that off.
8284 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8285 narg0
= fold_strip_sign_ops (arg0
);
8286 narg1
= fold_strip_sign_ops (arg1
);
8289 return build_call_expr_loc (loc
, fndecl
, 2, narg0
? narg0
: arg0
,
8290 narg1
? narg1
: arg1
);
8293 /* If either argument is zero, hypot is fabs of the other. */
8294 if (real_zerop (arg0
))
8295 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg1
);
8296 else if (real_zerop (arg1
))
8297 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg0
);
8299 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8300 if (flag_unsafe_math_optimizations
8301 && operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
8303 const REAL_VALUE_TYPE sqrt2_trunc
8304 = real_value_truncate (TYPE_MODE (type
), dconst_sqrt2 ());
8305 return fold_build2_loc (loc
, MULT_EXPR
, type
,
8306 fold_build1_loc (loc
, ABS_EXPR
, type
, arg0
),
8307 build_real (type
, sqrt2_trunc
));
8314 /* Fold a builtin function call to pow, powf, or powl. Return
8315 NULL_TREE if no simplification can be made. */
8317 fold_builtin_pow (location_t loc
, tree fndecl
, tree arg0
, tree arg1
, tree type
)
8321 if (!validate_arg (arg0
, REAL_TYPE
)
8322 || !validate_arg (arg1
, REAL_TYPE
))
8325 /* Calculate the result when the argument is a constant. */
8326 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_pow
)))
8329 /* Optimize pow(1.0,y) = 1.0. */
8330 if (real_onep (arg0
))
8331 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
), arg1
);
8333 if (TREE_CODE (arg1
) == REAL_CST
8334 && !TREE_OVERFLOW (arg1
))
8336 REAL_VALUE_TYPE cint
;
8340 c
= TREE_REAL_CST (arg1
);
8342 /* Optimize pow(x,0.0) = 1.0. */
8343 if (REAL_VALUES_EQUAL (c
, dconst0
))
8344 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
),
8347 /* Optimize pow(x,1.0) = x. */
8348 if (REAL_VALUES_EQUAL (c
, dconst1
))
8351 /* Optimize pow(x,-1.0) = 1.0/x. */
8352 if (REAL_VALUES_EQUAL (c
, dconstm1
))
8353 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
8354 build_real (type
, dconst1
), arg0
);
8356 /* Optimize pow(x,0.5) = sqrt(x). */
8357 if (flag_unsafe_math_optimizations
8358 && REAL_VALUES_EQUAL (c
, dconsthalf
))
8360 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
8362 if (sqrtfn
!= NULL_TREE
)
8363 return build_call_expr_loc (loc
, sqrtfn
, 1, arg0
);
8366 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8367 if (flag_unsafe_math_optimizations
)
8369 const REAL_VALUE_TYPE dconstroot
8370 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
8372 if (REAL_VALUES_EQUAL (c
, dconstroot
))
8374 tree cbrtfn
= mathfn_built_in (type
, BUILT_IN_CBRT
);
8375 if (cbrtfn
!= NULL_TREE
)
8376 return build_call_expr_loc (loc
, cbrtfn
, 1, arg0
);
8380 /* Check for an integer exponent. */
8381 n
= real_to_integer (&c
);
8382 real_from_integer (&cint
, VOIDmode
, n
, n
< 0 ? -1 : 0, 0);
8383 if (real_identical (&c
, &cint
))
8385 /* Attempt to evaluate pow at compile-time, unless this should
8386 raise an exception. */
8387 if (TREE_CODE (arg0
) == REAL_CST
8388 && !TREE_OVERFLOW (arg0
)
8390 || (!flag_trapping_math
&& !flag_errno_math
)
8391 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0
), dconst0
)))
8396 x
= TREE_REAL_CST (arg0
);
8397 inexact
= real_powi (&x
, TYPE_MODE (type
), &x
, n
);
8398 if (flag_unsafe_math_optimizations
|| !inexact
)
8399 return build_real (type
, x
);
8402 /* Strip sign ops from even integer powers. */
8403 if ((n
& 1) == 0 && flag_unsafe_math_optimizations
)
8405 tree narg0
= fold_strip_sign_ops (arg0
);
8407 return build_call_expr_loc (loc
, fndecl
, 2, narg0
, arg1
);
8412 if (flag_unsafe_math_optimizations
)
8414 const enum built_in_function fcode
= builtin_mathfn_code (arg0
);
8416 /* Optimize pow(expN(x),y) = expN(x*y). */
8417 if (BUILTIN_EXPONENT_P (fcode
))
8419 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
8420 tree arg
= CALL_EXPR_ARG (arg0
, 0);
8421 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg
, arg1
);
8422 return build_call_expr_loc (loc
, expfn
, 1, arg
);
8425 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8426 if (BUILTIN_SQRT_P (fcode
))
8428 tree narg0
= CALL_EXPR_ARG (arg0
, 0);
8429 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
8430 build_real (type
, dconsthalf
));
8431 return build_call_expr_loc (loc
, fndecl
, 2, narg0
, narg1
);
8434 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8435 if (BUILTIN_CBRT_P (fcode
))
8437 tree arg
= CALL_EXPR_ARG (arg0
, 0);
8438 if (tree_expr_nonnegative_p (arg
))
8440 const REAL_VALUE_TYPE dconstroot
8441 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
8442 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
8443 build_real (type
, dconstroot
));
8444 return build_call_expr_loc (loc
, fndecl
, 2, arg
, narg1
);
8448 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8449 if (fcode
== BUILT_IN_POW
8450 || fcode
== BUILT_IN_POWF
8451 || fcode
== BUILT_IN_POWL
)
8453 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
8454 if (tree_expr_nonnegative_p (arg00
))
8456 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
8457 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg01
, arg1
);
8458 return build_call_expr_loc (loc
, fndecl
, 2, arg00
, narg1
);
8466 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8467 Return NULL_TREE if no simplification can be made. */
8469 fold_builtin_powi (location_t loc
, tree fndecl ATTRIBUTE_UNUSED
,
8470 tree arg0
, tree arg1
, tree type
)
8472 if (!validate_arg (arg0
, REAL_TYPE
)
8473 || !validate_arg (arg1
, INTEGER_TYPE
))
8476 /* Optimize pow(1.0,y) = 1.0. */
8477 if (real_onep (arg0
))
8478 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
), arg1
);
8480 if (host_integerp (arg1
, 0))
8482 HOST_WIDE_INT c
= TREE_INT_CST_LOW (arg1
);
8484 /* Evaluate powi at compile-time. */
8485 if (TREE_CODE (arg0
) == REAL_CST
8486 && !TREE_OVERFLOW (arg0
))
8489 x
= TREE_REAL_CST (arg0
);
8490 real_powi (&x
, TYPE_MODE (type
), &x
, c
);
8491 return build_real (type
, x
);
8494 /* Optimize pow(x,0) = 1.0. */
8496 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
),
8499 /* Optimize pow(x,1) = x. */
8503 /* Optimize pow(x,-1) = 1.0/x. */
8505 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
8506 build_real (type
, dconst1
), arg0
);
8512 /* A subroutine of fold_builtin to fold the various exponent
8513 functions. Return NULL_TREE if no simplification can be made.
8514 FUNC is the corresponding MPFR exponent function. */
8517 fold_builtin_exponent (location_t loc
, tree fndecl
, tree arg
,
8518 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
))
8520 if (validate_arg (arg
, REAL_TYPE
))
8522 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8525 /* Calculate the result when the argument is a constant. */
8526 if ((res
= do_mpfr_arg1 (arg
, type
, func
, NULL
, NULL
, 0)))
8529 /* Optimize expN(logN(x)) = x. */
8530 if (flag_unsafe_math_optimizations
)
8532 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
8534 if ((func
== mpfr_exp
8535 && (fcode
== BUILT_IN_LOG
8536 || fcode
== BUILT_IN_LOGF
8537 || fcode
== BUILT_IN_LOGL
))
8538 || (func
== mpfr_exp2
8539 && (fcode
== BUILT_IN_LOG2
8540 || fcode
== BUILT_IN_LOG2F
8541 || fcode
== BUILT_IN_LOG2L
))
8542 || (func
== mpfr_exp10
8543 && (fcode
== BUILT_IN_LOG10
8544 || fcode
== BUILT_IN_LOG10F
8545 || fcode
== BUILT_IN_LOG10L
)))
8546 return fold_convert_loc (loc
, type
, CALL_EXPR_ARG (arg
, 0));
8553 /* Return true if VAR is a VAR_DECL or a component thereof. */
8556 var_decl_component_p (tree var
)
8559 while (handled_component_p (inner
))
8560 inner
= TREE_OPERAND (inner
, 0);
8561 return SSA_VAR_P (inner
);
8564 /* Fold function call to builtin memset. Return
8565 NULL_TREE if no simplification can be made. */
8568 fold_builtin_memset (location_t loc
, tree dest
, tree c
, tree len
,
8569 tree type
, bool ignore
)
8571 tree var
, ret
, etype
;
8572 unsigned HOST_WIDE_INT length
, cval
;
8574 if (! validate_arg (dest
, POINTER_TYPE
)
8575 || ! validate_arg (c
, INTEGER_TYPE
)
8576 || ! validate_arg (len
, INTEGER_TYPE
))
8579 if (! host_integerp (len
, 1))
8582 /* If the LEN parameter is zero, return DEST. */
8583 if (integer_zerop (len
))
8584 return omit_one_operand_loc (loc
, type
, dest
, c
);
8586 if (TREE_CODE (c
) != INTEGER_CST
|| TREE_SIDE_EFFECTS (dest
))
8591 if (TREE_CODE (var
) != ADDR_EXPR
)
8594 var
= TREE_OPERAND (var
, 0);
8595 if (TREE_THIS_VOLATILE (var
))
8598 etype
= TREE_TYPE (var
);
8599 if (TREE_CODE (etype
) == ARRAY_TYPE
)
8600 etype
= TREE_TYPE (etype
);
8602 if (!INTEGRAL_TYPE_P (etype
)
8603 && !POINTER_TYPE_P (etype
))
8606 if (! var_decl_component_p (var
))
8609 length
= tree_low_cst (len
, 1);
8610 if (GET_MODE_SIZE (TYPE_MODE (etype
)) != length
8611 || get_pointer_alignment (dest
) / BITS_PER_UNIT
< length
)
8614 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
8617 if (integer_zerop (c
))
8621 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
8624 cval
= TREE_INT_CST_LOW (c
);
8628 cval
|= (cval
<< 31) << 1;
8631 ret
= build_int_cst_type (etype
, cval
);
8632 var
= build_fold_indirect_ref_loc (loc
,
8633 fold_convert_loc (loc
,
8634 build_pointer_type (etype
),
8636 ret
= build2 (MODIFY_EXPR
, etype
, var
, ret
);
8640 return omit_one_operand_loc (loc
, type
, dest
, ret
);
8643 /* Fold function call to builtin memset. Return
8644 NULL_TREE if no simplification can be made. */
8647 fold_builtin_bzero (location_t loc
, tree dest
, tree size
, bool ignore
)
8649 if (! validate_arg (dest
, POINTER_TYPE
)
8650 || ! validate_arg (size
, INTEGER_TYPE
))
8656 /* New argument list transforming bzero(ptr x, int y) to
8657 memset(ptr x, int 0, size_t y). This is done this way
8658 so that if it isn't expanded inline, we fallback to
8659 calling bzero instead of memset. */
8661 return fold_builtin_memset (loc
, dest
, integer_zero_node
,
8662 fold_convert_loc (loc
, size_type_node
, size
),
8663 void_type_node
, ignore
);
8666 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8667 NULL_TREE if no simplification can be made.
8668 If ENDP is 0, return DEST (like memcpy).
8669 If ENDP is 1, return DEST+LEN (like mempcpy).
8670 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8671 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8675 fold_builtin_memory_op (location_t loc
, tree dest
, tree src
,
8676 tree len
, tree type
, bool ignore
, int endp
)
8678 tree destvar
, srcvar
, expr
;
8680 if (! validate_arg (dest
, POINTER_TYPE
)
8681 || ! validate_arg (src
, POINTER_TYPE
)
8682 || ! validate_arg (len
, INTEGER_TYPE
))
8685 /* If the LEN parameter is zero, return DEST. */
8686 if (integer_zerop (len
))
8687 return omit_one_operand_loc (loc
, type
, dest
, src
);
8689 /* If SRC and DEST are the same (and not volatile), return
8690 DEST{,+LEN,+LEN-1}. */
8691 if (operand_equal_p (src
, dest
, 0))
8695 tree srctype
, desttype
;
8696 unsigned int src_align
, dest_align
;
8701 src_align
= get_pointer_alignment (src
);
8702 dest_align
= get_pointer_alignment (dest
);
8704 /* Both DEST and SRC must be pointer types.
8705 ??? This is what old code did. Is the testing for pointer types
8708 If either SRC is readonly or length is 1, we can use memcpy. */
8709 if (!dest_align
|| !src_align
)
8711 if (readonly_data_expr (src
)
8712 || (host_integerp (len
, 1)
8713 && (MIN (src_align
, dest_align
) / BITS_PER_UNIT
8714 >= (unsigned HOST_WIDE_INT
) tree_low_cst (len
, 1))))
8716 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
8719 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
8722 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8723 if (TREE_CODE (src
) == ADDR_EXPR
8724 && TREE_CODE (dest
) == ADDR_EXPR
)
8726 tree src_base
, dest_base
, fn
;
8727 HOST_WIDE_INT src_offset
= 0, dest_offset
= 0;
8728 HOST_WIDE_INT size
= -1;
8729 HOST_WIDE_INT maxsize
= -1;
8731 srcvar
= TREE_OPERAND (src
, 0);
8732 src_base
= get_ref_base_and_extent (srcvar
, &src_offset
,
8734 destvar
= TREE_OPERAND (dest
, 0);
8735 dest_base
= get_ref_base_and_extent (destvar
, &dest_offset
,
8737 if (host_integerp (len
, 1))
8738 maxsize
= tree_low_cst (len
, 1);
8741 src_offset
/= BITS_PER_UNIT
;
8742 dest_offset
/= BITS_PER_UNIT
;
8743 if (SSA_VAR_P (src_base
)
8744 && SSA_VAR_P (dest_base
))
8746 if (operand_equal_p (src_base
, dest_base
, 0)
8747 && ranges_overlap_p (src_offset
, maxsize
,
8748 dest_offset
, maxsize
))
8751 else if (TREE_CODE (src_base
) == MEM_REF
8752 && TREE_CODE (dest_base
) == MEM_REF
)
8755 if (! operand_equal_p (TREE_OPERAND (src_base
, 0),
8756 TREE_OPERAND (dest_base
, 0), 0))
8758 off
= double_int_add (mem_ref_offset (src_base
),
8759 shwi_to_double_int (src_offset
));
8760 if (!double_int_fits_in_shwi_p (off
))
8762 src_offset
= off
.low
;
8763 off
= double_int_add (mem_ref_offset (dest_base
),
8764 shwi_to_double_int (dest_offset
));
8765 if (!double_int_fits_in_shwi_p (off
))
8767 dest_offset
= off
.low
;
8768 if (ranges_overlap_p (src_offset
, maxsize
,
8769 dest_offset
, maxsize
))
8775 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
8778 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
8781 /* If the destination and source do not alias optimize into
8783 if ((is_gimple_min_invariant (dest
)
8784 || TREE_CODE (dest
) == SSA_NAME
)
8785 && (is_gimple_min_invariant (src
)
8786 || TREE_CODE (src
) == SSA_NAME
))
8789 ao_ref_init_from_ptr_and_size (&destr
, dest
, len
);
8790 ao_ref_init_from_ptr_and_size (&srcr
, src
, len
);
8791 if (!refs_may_alias_p_1 (&destr
, &srcr
, false))
8794 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
8797 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
8804 if (!host_integerp (len
, 0))
8807 This logic lose for arguments like (type *)malloc (sizeof (type)),
8808 since we strip the casts of up to VOID return value from malloc.
8809 Perhaps we ought to inherit type from non-VOID argument here? */
8812 if (!POINTER_TYPE_P (TREE_TYPE (src
))
8813 || !POINTER_TYPE_P (TREE_TYPE (dest
)))
8815 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8816 if (TREE_CODE (src
) == POINTER_PLUS_EXPR
)
8818 tree tem
= TREE_OPERAND (src
, 0);
8820 if (tem
!= TREE_OPERAND (src
, 0))
8821 src
= build1 (NOP_EXPR
, TREE_TYPE (tem
), src
);
8823 if (TREE_CODE (dest
) == POINTER_PLUS_EXPR
)
8825 tree tem
= TREE_OPERAND (dest
, 0);
8827 if (tem
!= TREE_OPERAND (dest
, 0))
8828 dest
= build1 (NOP_EXPR
, TREE_TYPE (tem
), dest
);
8830 srctype
= TREE_TYPE (TREE_TYPE (src
));
8831 if (TREE_CODE (srctype
) == ARRAY_TYPE
8832 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
8834 srctype
= TREE_TYPE (srctype
);
8836 src
= build1 (NOP_EXPR
, build_pointer_type (srctype
), src
);
8838 desttype
= TREE_TYPE (TREE_TYPE (dest
));
8839 if (TREE_CODE (desttype
) == ARRAY_TYPE
8840 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
8842 desttype
= TREE_TYPE (desttype
);
8844 dest
= build1 (NOP_EXPR
, build_pointer_type (desttype
), dest
);
8846 if (TREE_ADDRESSABLE (srctype
)
8847 || TREE_ADDRESSABLE (desttype
))
8850 src_align
= get_pointer_alignment (src
);
8851 dest_align
= get_pointer_alignment (dest
);
8852 if (dest_align
< TYPE_ALIGN (desttype
)
8853 || src_align
< TYPE_ALIGN (srctype
))
8857 dest
= builtin_save_expr (dest
);
8859 /* Build accesses at offset zero with a ref-all character type. */
8860 off0
= build_int_cst (build_pointer_type_for_mode (char_type_node
,
8861 ptr_mode
, true), 0);
8864 STRIP_NOPS (destvar
);
8865 if (TREE_CODE (destvar
) == ADDR_EXPR
8866 && var_decl_component_p (TREE_OPERAND (destvar
, 0))
8867 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
8868 destvar
= fold_build2 (MEM_REF
, desttype
, destvar
, off0
);
8870 destvar
= NULL_TREE
;
8873 STRIP_NOPS (srcvar
);
8874 if (TREE_CODE (srcvar
) == ADDR_EXPR
8875 && var_decl_component_p (TREE_OPERAND (srcvar
, 0))
8876 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
8879 || src_align
>= TYPE_ALIGN (desttype
))
8880 srcvar
= fold_build2 (MEM_REF
, destvar
? desttype
: srctype
,
8882 else if (!STRICT_ALIGNMENT
)
8884 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
8886 srcvar
= fold_build2 (MEM_REF
, srctype
, srcvar
, off0
);
8894 if (srcvar
== NULL_TREE
&& destvar
== NULL_TREE
)
8897 if (srcvar
== NULL_TREE
)
8900 if (src_align
>= TYPE_ALIGN (desttype
))
8901 srcvar
= fold_build2 (MEM_REF
, desttype
, src
, off0
);
8904 if (STRICT_ALIGNMENT
)
8906 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
8908 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
8911 else if (destvar
== NULL_TREE
)
8914 if (dest_align
>= TYPE_ALIGN (srctype
))
8915 destvar
= fold_build2 (MEM_REF
, srctype
, dest
, off0
);
8918 if (STRICT_ALIGNMENT
)
8920 desttype
= build_aligned_type (TYPE_MAIN_VARIANT (srctype
),
8922 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
8926 expr
= build2 (MODIFY_EXPR
, TREE_TYPE (destvar
), destvar
, srcvar
);
8932 if (endp
== 0 || endp
== 3)
8933 return omit_one_operand_loc (loc
, type
, dest
, expr
);
8939 len
= fold_build2_loc (loc
, MINUS_EXPR
, TREE_TYPE (len
), len
,
8942 dest
= fold_build_pointer_plus_loc (loc
, dest
, len
);
8943 dest
= fold_convert_loc (loc
, type
, dest
);
8945 dest
= omit_one_operand_loc (loc
, type
, dest
, expr
);
8949 /* Fold function call to builtin strcpy with arguments DEST and SRC.
8950 If LEN is not NULL, it represents the length of the string to be
8951 copied. Return NULL_TREE if no simplification can be made. */
8954 fold_builtin_strcpy (location_t loc
, tree fndecl
, tree dest
, tree src
, tree len
)
8958 if (!validate_arg (dest
, POINTER_TYPE
)
8959 || !validate_arg (src
, POINTER_TYPE
))
8962 /* If SRC and DEST are the same (and not volatile), return DEST. */
8963 if (operand_equal_p (src
, dest
, 0))
8964 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
);
8966 if (optimize_function_for_size_p (cfun
))
8969 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
8975 len
= c_strlen (src
, 1);
8976 if (! len
|| TREE_SIDE_EFFECTS (len
))
8980 len
= fold_convert_loc (loc
, size_type_node
, len
);
8981 len
= size_binop_loc (loc
, PLUS_EXPR
, len
, build_int_cst (size_type_node
, 1));
8982 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)),
8983 build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
));
8986 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8987 Return NULL_TREE if no simplification can be made. */
8990 fold_builtin_stpcpy (location_t loc
, tree fndecl
, tree dest
, tree src
)
8992 tree fn
, len
, lenp1
, call
, type
;
8994 if (!validate_arg (dest
, POINTER_TYPE
)
8995 || !validate_arg (src
, POINTER_TYPE
))
8998 len
= c_strlen (src
, 1);
9000 || TREE_CODE (len
) != INTEGER_CST
)
9003 if (optimize_function_for_size_p (cfun
)
9004 /* If length is zero it's small enough. */
9005 && !integer_zerop (len
))
9008 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
9012 lenp1
= size_binop_loc (loc
, PLUS_EXPR
,
9013 fold_convert_loc (loc
, size_type_node
, len
),
9014 build_int_cst (size_type_node
, 1));
9015 /* We use dest twice in building our expression. Save it from
9016 multiple expansions. */
9017 dest
= builtin_save_expr (dest
);
9018 call
= build_call_expr_loc (loc
, fn
, 3, dest
, src
, lenp1
);
9020 type
= TREE_TYPE (TREE_TYPE (fndecl
));
9021 dest
= fold_build_pointer_plus_loc (loc
, dest
, len
);
9022 dest
= fold_convert_loc (loc
, type
, dest
);
9023 dest
= omit_one_operand_loc (loc
, type
, dest
, call
);
9027 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9028 If SLEN is not NULL, it represents the length of the source string.
9029 Return NULL_TREE if no simplification can be made. */
9032 fold_builtin_strncpy (location_t loc
, tree fndecl
, tree dest
,
9033 tree src
, tree len
, tree slen
)
9037 if (!validate_arg (dest
, POINTER_TYPE
)
9038 || !validate_arg (src
, POINTER_TYPE
)
9039 || !validate_arg (len
, INTEGER_TYPE
))
9042 /* If the LEN parameter is zero, return DEST. */
9043 if (integer_zerop (len
))
9044 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
9046 /* We can't compare slen with len as constants below if len is not a
9048 if (len
== 0 || TREE_CODE (len
) != INTEGER_CST
)
9052 slen
= c_strlen (src
, 1);
9054 /* Now, we must be passed a constant src ptr parameter. */
9055 if (slen
== 0 || TREE_CODE (slen
) != INTEGER_CST
)
9058 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
9060 /* We do not support simplification of this case, though we do
9061 support it when expanding trees into RTL. */
9062 /* FIXME: generate a call to __builtin_memset. */
9063 if (tree_int_cst_lt (slen
, len
))
9066 /* OK transform into builtin memcpy. */
9067 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
9071 len
= fold_convert_loc (loc
, size_type_node
, len
);
9072 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)),
9073 build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
));
9076 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9077 arguments to the call, and TYPE is its return type.
9078 Return NULL_TREE if no simplification can be made. */
9081 fold_builtin_memchr (location_t loc
, tree arg1
, tree arg2
, tree len
, tree type
)
9083 if (!validate_arg (arg1
, POINTER_TYPE
)
9084 || !validate_arg (arg2
, INTEGER_TYPE
)
9085 || !validate_arg (len
, INTEGER_TYPE
))
9091 if (TREE_CODE (arg2
) != INTEGER_CST
9092 || !host_integerp (len
, 1))
9095 p1
= c_getstr (arg1
);
9096 if (p1
&& compare_tree_int (len
, strlen (p1
) + 1) <= 0)
9102 if (target_char_cast (arg2
, &c
))
9105 r
= (const char *) memchr (p1
, c
, tree_low_cst (len
, 1));
9108 return build_int_cst (TREE_TYPE (arg1
), 0);
9110 tem
= fold_build_pointer_plus_hwi_loc (loc
, arg1
, r
- p1
);
9111 return fold_convert_loc (loc
, type
, tem
);
9117 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9118 Return NULL_TREE if no simplification can be made. */
9121 fold_builtin_memcmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
9123 const char *p1
, *p2
;
9125 if (!validate_arg (arg1
, POINTER_TYPE
)
9126 || !validate_arg (arg2
, POINTER_TYPE
)
9127 || !validate_arg (len
, INTEGER_TYPE
))
9130 /* If the LEN parameter is zero, return zero. */
9131 if (integer_zerop (len
))
9132 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
9135 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9136 if (operand_equal_p (arg1
, arg2
, 0))
9137 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
9139 p1
= c_getstr (arg1
);
9140 p2
= c_getstr (arg2
);
9142 /* If all arguments are constant, and the value of len is not greater
9143 than the lengths of arg1 and arg2, evaluate at compile-time. */
9144 if (host_integerp (len
, 1) && p1
&& p2
9145 && compare_tree_int (len
, strlen (p1
) + 1) <= 0
9146 && compare_tree_int (len
, strlen (p2
) + 1) <= 0)
9148 const int r
= memcmp (p1
, p2
, tree_low_cst (len
, 1));
9151 return integer_one_node
;
9153 return integer_minus_one_node
;
9155 return integer_zero_node
;
9158 /* If len parameter is one, return an expression corresponding to
9159 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9160 if (host_integerp (len
, 1) && tree_low_cst (len
, 1) == 1)
9162 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9163 tree cst_uchar_ptr_node
9164 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9167 = fold_convert_loc (loc
, integer_type_node
,
9168 build1 (INDIRECT_REF
, cst_uchar_node
,
9169 fold_convert_loc (loc
,
9173 = fold_convert_loc (loc
, integer_type_node
,
9174 build1 (INDIRECT_REF
, cst_uchar_node
,
9175 fold_convert_loc (loc
,
9178 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
9184 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9185 Return NULL_TREE if no simplification can be made. */
9188 fold_builtin_strcmp (location_t loc
, tree arg1
, tree arg2
)
9190 const char *p1
, *p2
;
9192 if (!validate_arg (arg1
, POINTER_TYPE
)
9193 || !validate_arg (arg2
, POINTER_TYPE
))
9196 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9197 if (operand_equal_p (arg1
, arg2
, 0))
9198 return integer_zero_node
;
9200 p1
= c_getstr (arg1
);
9201 p2
= c_getstr (arg2
);
9205 const int i
= strcmp (p1
, p2
);
9207 return integer_minus_one_node
;
9209 return integer_one_node
;
9211 return integer_zero_node
;
9214 /* If the second arg is "", return *(const unsigned char*)arg1. */
9215 if (p2
&& *p2
== '\0')
9217 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9218 tree cst_uchar_ptr_node
9219 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9221 return fold_convert_loc (loc
, integer_type_node
,
9222 build1 (INDIRECT_REF
, cst_uchar_node
,
9223 fold_convert_loc (loc
,
9228 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9229 if (p1
&& *p1
== '\0')
9231 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9232 tree cst_uchar_ptr_node
9233 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9236 = fold_convert_loc (loc
, integer_type_node
,
9237 build1 (INDIRECT_REF
, cst_uchar_node
,
9238 fold_convert_loc (loc
,
9241 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
9247 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9248 Return NULL_TREE if no simplification can be made. */
9251 fold_builtin_strncmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
9253 const char *p1
, *p2
;
9255 if (!validate_arg (arg1
, POINTER_TYPE
)
9256 || !validate_arg (arg2
, POINTER_TYPE
)
9257 || !validate_arg (len
, INTEGER_TYPE
))
9260 /* If the LEN parameter is zero, return zero. */
9261 if (integer_zerop (len
))
9262 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
9265 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9266 if (operand_equal_p (arg1
, arg2
, 0))
9267 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
9269 p1
= c_getstr (arg1
);
9270 p2
= c_getstr (arg2
);
9272 if (host_integerp (len
, 1) && p1
&& p2
)
9274 const int i
= strncmp (p1
, p2
, tree_low_cst (len
, 1));
9276 return integer_one_node
;
9278 return integer_minus_one_node
;
9280 return integer_zero_node
;
9283 /* If the second arg is "", and the length is greater than zero,
9284 return *(const unsigned char*)arg1. */
9285 if (p2
&& *p2
== '\0'
9286 && TREE_CODE (len
) == INTEGER_CST
9287 && tree_int_cst_sgn (len
) == 1)
9289 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9290 tree cst_uchar_ptr_node
9291 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9293 return fold_convert_loc (loc
, integer_type_node
,
9294 build1 (INDIRECT_REF
, cst_uchar_node
,
9295 fold_convert_loc (loc
,
9300 /* If the first arg is "", and the length is greater than zero,
9301 return -*(const unsigned char*)arg2. */
9302 if (p1
&& *p1
== '\0'
9303 && TREE_CODE (len
) == INTEGER_CST
9304 && tree_int_cst_sgn (len
) == 1)
9306 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9307 tree cst_uchar_ptr_node
9308 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9310 tree temp
= fold_convert_loc (loc
, integer_type_node
,
9311 build1 (INDIRECT_REF
, cst_uchar_node
,
9312 fold_convert_loc (loc
,
9315 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
9318 /* If len parameter is one, return an expression corresponding to
9319 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9320 if (host_integerp (len
, 1) && tree_low_cst (len
, 1) == 1)
9322 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9323 tree cst_uchar_ptr_node
9324 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9326 tree ind1
= fold_convert_loc (loc
, integer_type_node
,
9327 build1 (INDIRECT_REF
, cst_uchar_node
,
9328 fold_convert_loc (loc
,
9331 tree ind2
= fold_convert_loc (loc
, integer_type_node
,
9332 build1 (INDIRECT_REF
, cst_uchar_node
,
9333 fold_convert_loc (loc
,
9336 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
9342 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9343 ARG. Return NULL_TREE if no simplification can be made. */
9346 fold_builtin_signbit (location_t loc
, tree arg
, tree type
)
9348 if (!validate_arg (arg
, REAL_TYPE
))
9351 /* If ARG is a compile-time constant, determine the result. */
9352 if (TREE_CODE (arg
) == REAL_CST
9353 && !TREE_OVERFLOW (arg
))
9357 c
= TREE_REAL_CST (arg
);
9358 return (REAL_VALUE_NEGATIVE (c
)
9359 ? build_one_cst (type
)
9360 : build_zero_cst (type
));
9363 /* If ARG is non-negative, the result is always zero. */
9364 if (tree_expr_nonnegative_p (arg
))
9365 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9367 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9368 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg
))))
9369 return fold_convert (type
,
9370 fold_build2_loc (loc
, LT_EXPR
, boolean_type_node
, arg
,
9371 build_real (TREE_TYPE (arg
), dconst0
)));
9376 /* Fold function call to builtin copysign, copysignf or copysignl with
9377 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9381 fold_builtin_copysign (location_t loc
, tree fndecl
,
9382 tree arg1
, tree arg2
, tree type
)
9386 if (!validate_arg (arg1
, REAL_TYPE
)
9387 || !validate_arg (arg2
, REAL_TYPE
))
9390 /* copysign(X,X) is X. */
9391 if (operand_equal_p (arg1
, arg2
, 0))
9392 return fold_convert_loc (loc
, type
, arg1
);
9394 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9395 if (TREE_CODE (arg1
) == REAL_CST
9396 && TREE_CODE (arg2
) == REAL_CST
9397 && !TREE_OVERFLOW (arg1
)
9398 && !TREE_OVERFLOW (arg2
))
9400 REAL_VALUE_TYPE c1
, c2
;
9402 c1
= TREE_REAL_CST (arg1
);
9403 c2
= TREE_REAL_CST (arg2
);
9404 /* c1.sign := c2.sign. */
9405 real_copysign (&c1
, &c2
);
9406 return build_real (type
, c1
);
9409 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9410 Remember to evaluate Y for side-effects. */
9411 if (tree_expr_nonnegative_p (arg2
))
9412 return omit_one_operand_loc (loc
, type
,
9413 fold_build1_loc (loc
, ABS_EXPR
, type
, arg1
),
9416 /* Strip sign changing operations for the first argument. */
9417 tem
= fold_strip_sign_ops (arg1
);
9419 return build_call_expr_loc (loc
, fndecl
, 2, tem
, arg2
);
9424 /* Fold a call to builtin isascii with argument ARG. */
9427 fold_builtin_isascii (location_t loc
, tree arg
)
9429 if (!validate_arg (arg
, INTEGER_TYPE
))
9433 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9434 arg
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
9435 build_int_cst (integer_type_node
,
9436 ~ (unsigned HOST_WIDE_INT
) 0x7f));
9437 return fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
,
9438 arg
, integer_zero_node
);
9442 /* Fold a call to builtin toascii with argument ARG. */
9445 fold_builtin_toascii (location_t loc
, tree arg
)
9447 if (!validate_arg (arg
, INTEGER_TYPE
))
9450 /* Transform toascii(c) -> (c & 0x7f). */
9451 return fold_build2_loc (loc
, BIT_AND_EXPR
, integer_type_node
, arg
,
9452 build_int_cst (integer_type_node
, 0x7f));
9455 /* Fold a call to builtin isdigit with argument ARG. */
9458 fold_builtin_isdigit (location_t loc
, tree arg
)
9460 if (!validate_arg (arg
, INTEGER_TYPE
))
9464 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9465 /* According to the C standard, isdigit is unaffected by locale.
9466 However, it definitely is affected by the target character set. */
9467 unsigned HOST_WIDE_INT target_digit0
9468 = lang_hooks
.to_target_charset ('0');
9470 if (target_digit0
== 0)
9473 arg
= fold_convert_loc (loc
, unsigned_type_node
, arg
);
9474 arg
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
9475 build_int_cst (unsigned_type_node
, target_digit0
));
9476 return fold_build2_loc (loc
, LE_EXPR
, integer_type_node
, arg
,
9477 build_int_cst (unsigned_type_node
, 9));
9481 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9484 fold_builtin_fabs (location_t loc
, tree arg
, tree type
)
9486 if (!validate_arg (arg
, REAL_TYPE
))
9489 arg
= fold_convert_loc (loc
, type
, arg
);
9490 if (TREE_CODE (arg
) == REAL_CST
)
9491 return fold_abs_const (arg
, type
);
9492 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
9495 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9498 fold_builtin_abs (location_t loc
, tree arg
, tree type
)
9500 if (!validate_arg (arg
, INTEGER_TYPE
))
9503 arg
= fold_convert_loc (loc
, type
, arg
);
9504 if (TREE_CODE (arg
) == INTEGER_CST
)
9505 return fold_abs_const (arg
, type
);
9506 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
9509 /* Fold a fma operation with arguments ARG[012]. */
9512 fold_fma (location_t loc ATTRIBUTE_UNUSED
,
9513 tree type
, tree arg0
, tree arg1
, tree arg2
)
9515 if (TREE_CODE (arg0
) == REAL_CST
9516 && TREE_CODE (arg1
) == REAL_CST
9517 && TREE_CODE (arg2
) == REAL_CST
)
9518 return do_mpfr_arg3 (arg0
, arg1
, arg2
, type
, mpfr_fma
);
9523 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9526 fold_builtin_fma (location_t loc
, tree arg0
, tree arg1
, tree arg2
, tree type
)
9528 if (validate_arg (arg0
, REAL_TYPE
)
9529 && validate_arg(arg1
, REAL_TYPE
)
9530 && validate_arg(arg2
, REAL_TYPE
))
9532 tree tem
= fold_fma (loc
, type
, arg0
, arg1
, arg2
);
9536 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9537 if (optab_handler (fma_optab
, TYPE_MODE (type
)) != CODE_FOR_nothing
)
9538 return fold_build3_loc (loc
, FMA_EXPR
, type
, arg0
, arg1
, arg2
);
9543 /* Fold a call to builtin fmin or fmax. */
9546 fold_builtin_fmin_fmax (location_t loc
, tree arg0
, tree arg1
,
9547 tree type
, bool max
)
9549 if (validate_arg (arg0
, REAL_TYPE
) && validate_arg (arg1
, REAL_TYPE
))
9551 /* Calculate the result when the argument is a constant. */
9552 tree res
= do_mpfr_arg2 (arg0
, arg1
, type
, (max
? mpfr_max
: mpfr_min
));
9557 /* If either argument is NaN, return the other one. Avoid the
9558 transformation if we get (and honor) a signalling NaN. Using
9559 omit_one_operand() ensures we create a non-lvalue. */
9560 if (TREE_CODE (arg0
) == REAL_CST
9561 && real_isnan (&TREE_REAL_CST (arg0
))
9562 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
9563 || ! TREE_REAL_CST (arg0
).signalling
))
9564 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
9565 if (TREE_CODE (arg1
) == REAL_CST
9566 && real_isnan (&TREE_REAL_CST (arg1
))
9567 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
9568 || ! TREE_REAL_CST (arg1
).signalling
))
9569 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9571 /* Transform fmin/fmax(x,x) -> x. */
9572 if (operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
9573 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9575 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9576 functions to return the numeric arg if the other one is NaN.
9577 These tree codes don't honor that, so only transform if
9578 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9579 handled, so we don't have to worry about it either. */
9580 if (flag_finite_math_only
)
9581 return fold_build2_loc (loc
, (max
? MAX_EXPR
: MIN_EXPR
), type
,
9582 fold_convert_loc (loc
, type
, arg0
),
9583 fold_convert_loc (loc
, type
, arg1
));
9588 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9591 fold_builtin_carg (location_t loc
, tree arg
, tree type
)
9593 if (validate_arg (arg
, COMPLEX_TYPE
)
9594 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
9596 tree atan2_fn
= mathfn_built_in (type
, BUILT_IN_ATAN2
);
9600 tree new_arg
= builtin_save_expr (arg
);
9601 tree r_arg
= fold_build1_loc (loc
, REALPART_EXPR
, type
, new_arg
);
9602 tree i_arg
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, new_arg
);
9603 return build_call_expr_loc (loc
, atan2_fn
, 2, i_arg
, r_arg
);
9610 /* Fold a call to builtin logb/ilogb. */
9613 fold_builtin_logb (location_t loc
, tree arg
, tree rettype
)
9615 if (! validate_arg (arg
, REAL_TYPE
))
9620 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_OVERFLOW (arg
))
9622 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg
);
9628 /* If arg is Inf or NaN and we're logb, return it. */
9629 if (TREE_CODE (rettype
) == REAL_TYPE
)
9630 return fold_convert_loc (loc
, rettype
, arg
);
9631 /* Fall through... */
9633 /* Zero may set errno and/or raise an exception for logb, also
9634 for ilogb we don't know FP_ILOGB0. */
9637 /* For normal numbers, proceed iff radix == 2. In GCC,
9638 normalized significands are in the range [0.5, 1.0). We
9639 want the exponent as if they were [1.0, 2.0) so get the
9640 exponent and subtract 1. */
9641 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg
)))->b
== 2)
9642 return fold_convert_loc (loc
, rettype
,
9643 build_int_cst (integer_type_node
,
9644 REAL_EXP (value
)-1));
9652 /* Fold a call to builtin significand, if radix == 2. */
9655 fold_builtin_significand (location_t loc
, tree arg
, tree rettype
)
9657 if (! validate_arg (arg
, REAL_TYPE
))
9662 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_OVERFLOW (arg
))
9664 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg
);
9671 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9672 return fold_convert_loc (loc
, rettype
, arg
);
9674 /* For normal numbers, proceed iff radix == 2. */
9675 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg
)))->b
== 2)
9677 REAL_VALUE_TYPE result
= *value
;
9678 /* In GCC, normalized significands are in the range [0.5,
9679 1.0). We want them to be [1.0, 2.0) so set the
9681 SET_REAL_EXP (&result
, 1);
9682 return build_real (rettype
, result
);
9691 /* Fold a call to builtin frexp, we can assume the base is 2. */
9694 fold_builtin_frexp (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
9696 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
9701 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
9704 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
9706 /* Proceed if a valid pointer type was passed in. */
9707 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == integer_type_node
)
9709 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9715 /* For +-0, return (*exp = 0, +-0). */
9716 exp
= integer_zero_node
;
9721 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9722 return omit_one_operand_loc (loc
, rettype
, arg0
, arg1
);
9725 /* Since the frexp function always expects base 2, and in
9726 GCC normalized significands are already in the range
9727 [0.5, 1.0), we have exactly what frexp wants. */
9728 REAL_VALUE_TYPE frac_rvt
= *value
;
9729 SET_REAL_EXP (&frac_rvt
, 0);
9730 frac
= build_real (rettype
, frac_rvt
);
9731 exp
= build_int_cst (integer_type_node
, REAL_EXP (value
));
9738 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9739 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
, exp
);
9740 TREE_SIDE_EFFECTS (arg1
) = 1;
9741 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
, frac
);
9747 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9748 then we can assume the base is two. If it's false, then we have to
9749 check the mode of the TYPE parameter in certain cases. */
9752 fold_builtin_load_exponent (location_t loc
, tree arg0
, tree arg1
,
9753 tree type
, bool ldexp
)
9755 if (validate_arg (arg0
, REAL_TYPE
) && validate_arg (arg1
, INTEGER_TYPE
))
9760 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9761 if (real_zerop (arg0
) || integer_zerop (arg1
)
9762 || (TREE_CODE (arg0
) == REAL_CST
9763 && !real_isfinite (&TREE_REAL_CST (arg0
))))
9764 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9766 /* If both arguments are constant, then try to evaluate it. */
9767 if ((ldexp
|| REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2)
9768 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
9769 && host_integerp (arg1
, 0))
9771 /* Bound the maximum adjustment to twice the range of the
9772 mode's valid exponents. Use abs to ensure the range is
9773 positive as a sanity check. */
9774 const long max_exp_adj
= 2 *
9775 labs (REAL_MODE_FORMAT (TYPE_MODE (type
))->emax
9776 - REAL_MODE_FORMAT (TYPE_MODE (type
))->emin
);
9778 /* Get the user-requested adjustment. */
9779 const HOST_WIDE_INT req_exp_adj
= tree_low_cst (arg1
, 0);
9781 /* The requested adjustment must be inside this range. This
9782 is a preliminary cap to avoid things like overflow, we
9783 may still fail to compute the result for other reasons. */
9784 if (-max_exp_adj
< req_exp_adj
&& req_exp_adj
< max_exp_adj
)
9786 REAL_VALUE_TYPE initial_result
;
9788 real_ldexp (&initial_result
, &TREE_REAL_CST (arg0
), req_exp_adj
);
9790 /* Ensure we didn't overflow. */
9791 if (! real_isinf (&initial_result
))
9793 const REAL_VALUE_TYPE trunc_result
9794 = real_value_truncate (TYPE_MODE (type
), initial_result
);
9796 /* Only proceed if the target mode can hold the
9798 if (REAL_VALUES_EQUAL (initial_result
, trunc_result
))
9799 return build_real (type
, trunc_result
);
9808 /* Fold a call to builtin modf. */
9811 fold_builtin_modf (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
9813 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
9818 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
9821 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
9823 /* Proceed if a valid pointer type was passed in. */
9824 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == TYPE_MAIN_VARIANT (rettype
))
9826 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9827 REAL_VALUE_TYPE trunc
, frac
;
9833 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9834 trunc
= frac
= *value
;
9837 /* For +-Inf, return (*arg1 = arg0, +-0). */
9839 frac
.sign
= value
->sign
;
9843 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9844 real_trunc (&trunc
, VOIDmode
, value
);
9845 real_arithmetic (&frac
, MINUS_EXPR
, value
, &trunc
);
9846 /* If the original number was negative and already
9847 integral, then the fractional part is -0.0. */
9848 if (value
->sign
&& frac
.cl
== rvc_zero
)
9849 frac
.sign
= value
->sign
;
9853 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9854 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
,
9855 build_real (rettype
, trunc
));
9856 TREE_SIDE_EFFECTS (arg1
) = 1;
9857 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
,
9858 build_real (rettype
, frac
));
9864 /* Given a location LOC, an interclass builtin function decl FNDECL
9865 and its single argument ARG, return an folded expression computing
9866 the same, or NULL_TREE if we either couldn't or didn't want to fold
9867 (the latter happen if there's an RTL instruction available). */
9870 fold_builtin_interclass_mathfn (location_t loc
, tree fndecl
, tree arg
)
9872 enum machine_mode mode
;
9874 if (!validate_arg (arg
, REAL_TYPE
))
9877 if (interclass_mathfn_icode (arg
, fndecl
) != CODE_FOR_nothing
)
9880 mode
= TYPE_MODE (TREE_TYPE (arg
));
9882 /* If there is no optab, try generic code. */
9883 switch (DECL_FUNCTION_CODE (fndecl
))
9887 CASE_FLT_FN (BUILT_IN_ISINF
):
9889 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9890 tree
const isgr_fn
= builtin_decl_explicit (BUILT_IN_ISGREATER
);
9891 tree
const type
= TREE_TYPE (arg
);
9895 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9896 real_from_string (&r
, buf
);
9897 result
= build_call_expr (isgr_fn
, 2,
9898 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
9899 build_real (type
, r
));
9902 CASE_FLT_FN (BUILT_IN_FINITE
):
9903 case BUILT_IN_ISFINITE
:
9905 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9906 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
9907 tree
const type
= TREE_TYPE (arg
);
9911 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9912 real_from_string (&r
, buf
);
9913 result
= build_call_expr (isle_fn
, 2,
9914 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
9915 build_real (type
, r
));
9916 /*result = fold_build2_loc (loc, UNGT_EXPR,
9917 TREE_TYPE (TREE_TYPE (fndecl)),
9918 fold_build1_loc (loc, ABS_EXPR, type, arg),
9919 build_real (type, r));
9920 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9921 TREE_TYPE (TREE_TYPE (fndecl)),
9925 case BUILT_IN_ISNORMAL
:
9927 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9928 islessequal(fabs(x),DBL_MAX). */
9929 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
9930 tree
const isge_fn
= builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL
);
9931 tree
const type
= TREE_TYPE (arg
);
9932 REAL_VALUE_TYPE rmax
, rmin
;
9935 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9936 real_from_string (&rmax
, buf
);
9937 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
9938 real_from_string (&rmin
, buf
);
9939 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
9940 result
= build_call_expr (isle_fn
, 2, arg
,
9941 build_real (type
, rmax
));
9942 result
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, result
,
9943 build_call_expr (isge_fn
, 2, arg
,
9944 build_real (type
, rmin
)));
9954 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9955 ARG is the argument for the call. */
9958 fold_builtin_classify (location_t loc
, tree fndecl
, tree arg
, int builtin_index
)
9960 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9963 if (!validate_arg (arg
, REAL_TYPE
))
9966 switch (builtin_index
)
9968 case BUILT_IN_ISINF
:
9969 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
9970 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9972 if (TREE_CODE (arg
) == REAL_CST
)
9974 r
= TREE_REAL_CST (arg
);
9975 if (real_isinf (&r
))
9976 return real_compare (GT_EXPR
, &r
, &dconst0
)
9977 ? integer_one_node
: integer_minus_one_node
;
9979 return integer_zero_node
;
9984 case BUILT_IN_ISINF_SIGN
:
9986 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9987 /* In a boolean context, GCC will fold the inner COND_EXPR to
9988 1. So e.g. "if (isinf_sign(x))" would be folded to just
9989 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9990 tree signbit_fn
= mathfn_built_in_1 (TREE_TYPE (arg
), BUILT_IN_SIGNBIT
, 0);
9991 tree isinf_fn
= builtin_decl_explicit (BUILT_IN_ISINF
);
9992 tree tmp
= NULL_TREE
;
9994 arg
= builtin_save_expr (arg
);
9996 if (signbit_fn
&& isinf_fn
)
9998 tree signbit_call
= build_call_expr_loc (loc
, signbit_fn
, 1, arg
);
9999 tree isinf_call
= build_call_expr_loc (loc
, isinf_fn
, 1, arg
);
10001 signbit_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
10002 signbit_call
, integer_zero_node
);
10003 isinf_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
10004 isinf_call
, integer_zero_node
);
10006 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, signbit_call
,
10007 integer_minus_one_node
, integer_one_node
);
10008 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
10010 integer_zero_node
);
10016 case BUILT_IN_ISFINITE
:
10017 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg
)))
10018 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
10019 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
10021 if (TREE_CODE (arg
) == REAL_CST
)
10023 r
= TREE_REAL_CST (arg
);
10024 return real_isfinite (&r
) ? integer_one_node
: integer_zero_node
;
10029 case BUILT_IN_ISNAN
:
10030 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg
))))
10031 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
10033 if (TREE_CODE (arg
) == REAL_CST
)
10035 r
= TREE_REAL_CST (arg
);
10036 return real_isnan (&r
) ? integer_one_node
: integer_zero_node
;
10039 arg
= builtin_save_expr (arg
);
10040 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg
, arg
);
10043 gcc_unreachable ();
10047 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10048 This builtin will generate code to return the appropriate floating
10049 point classification depending on the value of the floating point
10050 number passed in. The possible return values must be supplied as
10051 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10052 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10053 one floating point argument which is "type generic". */
10056 fold_builtin_fpclassify (location_t loc
, tree exp
)
10058 tree fp_nan
, fp_infinite
, fp_normal
, fp_subnormal
, fp_zero
,
10059 arg
, type
, res
, tmp
;
10060 enum machine_mode mode
;
10064 /* Verify the required arguments in the original call. */
10065 if (!validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
,
10066 INTEGER_TYPE
, INTEGER_TYPE
,
10067 INTEGER_TYPE
, REAL_TYPE
, VOID_TYPE
))
10070 fp_nan
= CALL_EXPR_ARG (exp
, 0);
10071 fp_infinite
= CALL_EXPR_ARG (exp
, 1);
10072 fp_normal
= CALL_EXPR_ARG (exp
, 2);
10073 fp_subnormal
= CALL_EXPR_ARG (exp
, 3);
10074 fp_zero
= CALL_EXPR_ARG (exp
, 4);
10075 arg
= CALL_EXPR_ARG (exp
, 5);
10076 type
= TREE_TYPE (arg
);
10077 mode
= TYPE_MODE (type
);
10078 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
10080 /* fpclassify(x) ->
10081 isnan(x) ? FP_NAN :
10082 (fabs(x) == Inf ? FP_INFINITE :
10083 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10084 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10086 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
10087 build_real (type
, dconst0
));
10088 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
10089 tmp
, fp_zero
, fp_subnormal
);
10091 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
10092 real_from_string (&r
, buf
);
10093 tmp
= fold_build2_loc (loc
, GE_EXPR
, integer_type_node
,
10094 arg
, build_real (type
, r
));
10095 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, fp_normal
, res
);
10097 if (HONOR_INFINITIES (mode
))
10100 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
10101 build_real (type
, r
));
10102 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
,
10106 if (HONOR_NANS (mode
))
10108 tmp
= fold_build2_loc (loc
, ORDERED_EXPR
, integer_type_node
, arg
, arg
);
10109 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, res
, fp_nan
);
10115 /* Fold a call to an unordered comparison function such as
10116 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10117 being called and ARG0 and ARG1 are the arguments for the call.
10118 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10119 the opposite of the desired result. UNORDERED_CODE is used
10120 for modes that can hold NaNs and ORDERED_CODE is used for
10124 fold_builtin_unordered_cmp (location_t loc
, tree fndecl
, tree arg0
, tree arg1
,
10125 enum tree_code unordered_code
,
10126 enum tree_code ordered_code
)
10128 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10129 enum tree_code code
;
10131 enum tree_code code0
, code1
;
10132 tree cmp_type
= NULL_TREE
;
10134 type0
= TREE_TYPE (arg0
);
10135 type1
= TREE_TYPE (arg1
);
10137 code0
= TREE_CODE (type0
);
10138 code1
= TREE_CODE (type1
);
10140 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
10141 /* Choose the wider of two real types. */
10142 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
10144 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
10146 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
10149 arg0
= fold_convert_loc (loc
, cmp_type
, arg0
);
10150 arg1
= fold_convert_loc (loc
, cmp_type
, arg1
);
10152 if (unordered_code
== UNORDERED_EXPR
)
10154 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
10155 return omit_two_operands_loc (loc
, type
, integer_zero_node
, arg0
, arg1
);
10156 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg0
, arg1
);
10159 code
= HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))) ? unordered_code
10161 return fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
,
10162 fold_build2_loc (loc
, code
, type
, arg0
, arg1
));
10165 /* Fold a call to built-in function FNDECL with 0 arguments.
10166 IGNORE is true if the result of the function call is ignored. This
10167 function returns NULL_TREE if no simplification was possible. */
10170 fold_builtin_0 (location_t loc
, tree fndecl
, bool ignore ATTRIBUTE_UNUSED
)
10172 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10173 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10176 CASE_FLT_FN (BUILT_IN_INF
):
10177 case BUILT_IN_INFD32
:
10178 case BUILT_IN_INFD64
:
10179 case BUILT_IN_INFD128
:
10180 return fold_builtin_inf (loc
, type
, true);
10182 CASE_FLT_FN (BUILT_IN_HUGE_VAL
):
10183 return fold_builtin_inf (loc
, type
, false);
10185 case BUILT_IN_CLASSIFY_TYPE
:
10186 return fold_builtin_classify_type (NULL_TREE
);
10194 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10195 IGNORE is true if the result of the function call is ignored. This
10196 function returns NULL_TREE if no simplification was possible. */
10199 fold_builtin_1 (location_t loc
, tree fndecl
, tree arg0
, bool ignore
)
10201 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10202 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10205 case BUILT_IN_CONSTANT_P
:
10207 tree val
= fold_builtin_constant_p (arg0
);
10209 /* Gimplification will pull the CALL_EXPR for the builtin out of
10210 an if condition. When not optimizing, we'll not CSE it back.
10211 To avoid link error types of regressions, return false now. */
10212 if (!val
&& !optimize
)
10213 val
= integer_zero_node
;
10218 case BUILT_IN_CLASSIFY_TYPE
:
10219 return fold_builtin_classify_type (arg0
);
10221 case BUILT_IN_STRLEN
:
10222 return fold_builtin_strlen (loc
, type
, arg0
);
10224 CASE_FLT_FN (BUILT_IN_FABS
):
10225 return fold_builtin_fabs (loc
, arg0
, type
);
10228 case BUILT_IN_LABS
:
10229 case BUILT_IN_LLABS
:
10230 case BUILT_IN_IMAXABS
:
10231 return fold_builtin_abs (loc
, arg0
, type
);
10233 CASE_FLT_FN (BUILT_IN_CONJ
):
10234 if (validate_arg (arg0
, COMPLEX_TYPE
)
10235 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10236 return fold_build1_loc (loc
, CONJ_EXPR
, type
, arg0
);
10239 CASE_FLT_FN (BUILT_IN_CREAL
):
10240 if (validate_arg (arg0
, COMPLEX_TYPE
)
10241 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10242 return non_lvalue_loc (loc
, fold_build1_loc (loc
, REALPART_EXPR
, type
, arg0
));;
10245 CASE_FLT_FN (BUILT_IN_CIMAG
):
10246 if (validate_arg (arg0
, COMPLEX_TYPE
)
10247 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10248 return non_lvalue_loc (loc
, fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg0
));
10251 CASE_FLT_FN (BUILT_IN_CCOS
):
10252 return fold_builtin_ccos(loc
, arg0
, type
, fndecl
, /*hyper=*/ false);
10254 CASE_FLT_FN (BUILT_IN_CCOSH
):
10255 return fold_builtin_ccos(loc
, arg0
, type
, fndecl
, /*hyper=*/ true);
10257 CASE_FLT_FN (BUILT_IN_CPROJ
):
10258 return fold_builtin_cproj(loc
, arg0
, type
);
10260 CASE_FLT_FN (BUILT_IN_CSIN
):
10261 if (validate_arg (arg0
, COMPLEX_TYPE
)
10262 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10263 return do_mpc_arg1 (arg0
, type
, mpc_sin
);
10266 CASE_FLT_FN (BUILT_IN_CSINH
):
10267 if (validate_arg (arg0
, COMPLEX_TYPE
)
10268 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10269 return do_mpc_arg1 (arg0
, type
, mpc_sinh
);
10272 CASE_FLT_FN (BUILT_IN_CTAN
):
10273 if (validate_arg (arg0
, COMPLEX_TYPE
)
10274 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10275 return do_mpc_arg1 (arg0
, type
, mpc_tan
);
10278 CASE_FLT_FN (BUILT_IN_CTANH
):
10279 if (validate_arg (arg0
, COMPLEX_TYPE
)
10280 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10281 return do_mpc_arg1 (arg0
, type
, mpc_tanh
);
10284 CASE_FLT_FN (BUILT_IN_CLOG
):
10285 if (validate_arg (arg0
, COMPLEX_TYPE
)
10286 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10287 return do_mpc_arg1 (arg0
, type
, mpc_log
);
10290 CASE_FLT_FN (BUILT_IN_CSQRT
):
10291 if (validate_arg (arg0
, COMPLEX_TYPE
)
10292 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10293 return do_mpc_arg1 (arg0
, type
, mpc_sqrt
);
10296 CASE_FLT_FN (BUILT_IN_CASIN
):
10297 if (validate_arg (arg0
, COMPLEX_TYPE
)
10298 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10299 return do_mpc_arg1 (arg0
, type
, mpc_asin
);
10302 CASE_FLT_FN (BUILT_IN_CACOS
):
10303 if (validate_arg (arg0
, COMPLEX_TYPE
)
10304 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10305 return do_mpc_arg1 (arg0
, type
, mpc_acos
);
10308 CASE_FLT_FN (BUILT_IN_CATAN
):
10309 if (validate_arg (arg0
, COMPLEX_TYPE
)
10310 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10311 return do_mpc_arg1 (arg0
, type
, mpc_atan
);
10314 CASE_FLT_FN (BUILT_IN_CASINH
):
10315 if (validate_arg (arg0
, COMPLEX_TYPE
)
10316 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10317 return do_mpc_arg1 (arg0
, type
, mpc_asinh
);
10320 CASE_FLT_FN (BUILT_IN_CACOSH
):
10321 if (validate_arg (arg0
, COMPLEX_TYPE
)
10322 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10323 return do_mpc_arg1 (arg0
, type
, mpc_acosh
);
10326 CASE_FLT_FN (BUILT_IN_CATANH
):
10327 if (validate_arg (arg0
, COMPLEX_TYPE
)
10328 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10329 return do_mpc_arg1 (arg0
, type
, mpc_atanh
);
10332 CASE_FLT_FN (BUILT_IN_CABS
):
10333 return fold_builtin_cabs (loc
, arg0
, type
, fndecl
);
10335 CASE_FLT_FN (BUILT_IN_CARG
):
10336 return fold_builtin_carg (loc
, arg0
, type
);
10338 CASE_FLT_FN (BUILT_IN_SQRT
):
10339 return fold_builtin_sqrt (loc
, arg0
, type
);
10341 CASE_FLT_FN (BUILT_IN_CBRT
):
10342 return fold_builtin_cbrt (loc
, arg0
, type
);
10344 CASE_FLT_FN (BUILT_IN_ASIN
):
10345 if (validate_arg (arg0
, REAL_TYPE
))
10346 return do_mpfr_arg1 (arg0
, type
, mpfr_asin
,
10347 &dconstm1
, &dconst1
, true);
10350 CASE_FLT_FN (BUILT_IN_ACOS
):
10351 if (validate_arg (arg0
, REAL_TYPE
))
10352 return do_mpfr_arg1 (arg0
, type
, mpfr_acos
,
10353 &dconstm1
, &dconst1
, true);
10356 CASE_FLT_FN (BUILT_IN_ATAN
):
10357 if (validate_arg (arg0
, REAL_TYPE
))
10358 return do_mpfr_arg1 (arg0
, type
, mpfr_atan
, NULL
, NULL
, 0);
10361 CASE_FLT_FN (BUILT_IN_ASINH
):
10362 if (validate_arg (arg0
, REAL_TYPE
))
10363 return do_mpfr_arg1 (arg0
, type
, mpfr_asinh
, NULL
, NULL
, 0);
10366 CASE_FLT_FN (BUILT_IN_ACOSH
):
10367 if (validate_arg (arg0
, REAL_TYPE
))
10368 return do_mpfr_arg1 (arg0
, type
, mpfr_acosh
,
10369 &dconst1
, NULL
, true);
10372 CASE_FLT_FN (BUILT_IN_ATANH
):
10373 if (validate_arg (arg0
, REAL_TYPE
))
10374 return do_mpfr_arg1 (arg0
, type
, mpfr_atanh
,
10375 &dconstm1
, &dconst1
, false);
10378 CASE_FLT_FN (BUILT_IN_SIN
):
10379 if (validate_arg (arg0
, REAL_TYPE
))
10380 return do_mpfr_arg1 (arg0
, type
, mpfr_sin
, NULL
, NULL
, 0);
10383 CASE_FLT_FN (BUILT_IN_COS
):
10384 return fold_builtin_cos (loc
, arg0
, type
, fndecl
);
10386 CASE_FLT_FN (BUILT_IN_TAN
):
10387 return fold_builtin_tan (arg0
, type
);
10389 CASE_FLT_FN (BUILT_IN_CEXP
):
10390 return fold_builtin_cexp (loc
, arg0
, type
);
10392 CASE_FLT_FN (BUILT_IN_CEXPI
):
10393 if (validate_arg (arg0
, REAL_TYPE
))
10394 return do_mpfr_sincos (arg0
, NULL_TREE
, NULL_TREE
);
10397 CASE_FLT_FN (BUILT_IN_SINH
):
10398 if (validate_arg (arg0
, REAL_TYPE
))
10399 return do_mpfr_arg1 (arg0
, type
, mpfr_sinh
, NULL
, NULL
, 0);
10402 CASE_FLT_FN (BUILT_IN_COSH
):
10403 return fold_builtin_cosh (loc
, arg0
, type
, fndecl
);
10405 CASE_FLT_FN (BUILT_IN_TANH
):
10406 if (validate_arg (arg0
, REAL_TYPE
))
10407 return do_mpfr_arg1 (arg0
, type
, mpfr_tanh
, NULL
, NULL
, 0);
10410 CASE_FLT_FN (BUILT_IN_ERF
):
10411 if (validate_arg (arg0
, REAL_TYPE
))
10412 return do_mpfr_arg1 (arg0
, type
, mpfr_erf
, NULL
, NULL
, 0);
10415 CASE_FLT_FN (BUILT_IN_ERFC
):
10416 if (validate_arg (arg0
, REAL_TYPE
))
10417 return do_mpfr_arg1 (arg0
, type
, mpfr_erfc
, NULL
, NULL
, 0);
10420 CASE_FLT_FN (BUILT_IN_TGAMMA
):
10421 if (validate_arg (arg0
, REAL_TYPE
))
10422 return do_mpfr_arg1 (arg0
, type
, mpfr_gamma
, NULL
, NULL
, 0);
10425 CASE_FLT_FN (BUILT_IN_EXP
):
10426 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp
);
10428 CASE_FLT_FN (BUILT_IN_EXP2
):
10429 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp2
);
10431 CASE_FLT_FN (BUILT_IN_EXP10
):
10432 CASE_FLT_FN (BUILT_IN_POW10
):
10433 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp10
);
10435 CASE_FLT_FN (BUILT_IN_EXPM1
):
10436 if (validate_arg (arg0
, REAL_TYPE
))
10437 return do_mpfr_arg1 (arg0
, type
, mpfr_expm1
, NULL
, NULL
, 0);
10440 CASE_FLT_FN (BUILT_IN_LOG
):
10441 return fold_builtin_logarithm (loc
, fndecl
, arg0
, mpfr_log
);
10443 CASE_FLT_FN (BUILT_IN_LOG2
):
10444 return fold_builtin_logarithm (loc
, fndecl
, arg0
, mpfr_log2
);
10446 CASE_FLT_FN (BUILT_IN_LOG10
):
10447 return fold_builtin_logarithm (loc
, fndecl
, arg0
, mpfr_log10
);
10449 CASE_FLT_FN (BUILT_IN_LOG1P
):
10450 if (validate_arg (arg0
, REAL_TYPE
))
10451 return do_mpfr_arg1 (arg0
, type
, mpfr_log1p
,
10452 &dconstm1
, NULL
, false);
10455 CASE_FLT_FN (BUILT_IN_J0
):
10456 if (validate_arg (arg0
, REAL_TYPE
))
10457 return do_mpfr_arg1 (arg0
, type
, mpfr_j0
,
10461 CASE_FLT_FN (BUILT_IN_J1
):
10462 if (validate_arg (arg0
, REAL_TYPE
))
10463 return do_mpfr_arg1 (arg0
, type
, mpfr_j1
,
10467 CASE_FLT_FN (BUILT_IN_Y0
):
10468 if (validate_arg (arg0
, REAL_TYPE
))
10469 return do_mpfr_arg1 (arg0
, type
, mpfr_y0
,
10470 &dconst0
, NULL
, false);
10473 CASE_FLT_FN (BUILT_IN_Y1
):
10474 if (validate_arg (arg0
, REAL_TYPE
))
10475 return do_mpfr_arg1 (arg0
, type
, mpfr_y1
,
10476 &dconst0
, NULL
, false);
10479 CASE_FLT_FN (BUILT_IN_NAN
):
10480 case BUILT_IN_NAND32
:
10481 case BUILT_IN_NAND64
:
10482 case BUILT_IN_NAND128
:
10483 return fold_builtin_nan (arg0
, type
, true);
10485 CASE_FLT_FN (BUILT_IN_NANS
):
10486 return fold_builtin_nan (arg0
, type
, false);
10488 CASE_FLT_FN (BUILT_IN_FLOOR
):
10489 return fold_builtin_floor (loc
, fndecl
, arg0
);
10491 CASE_FLT_FN (BUILT_IN_CEIL
):
10492 return fold_builtin_ceil (loc
, fndecl
, arg0
);
10494 CASE_FLT_FN (BUILT_IN_TRUNC
):
10495 return fold_builtin_trunc (loc
, fndecl
, arg0
);
10497 CASE_FLT_FN (BUILT_IN_ROUND
):
10498 return fold_builtin_round (loc
, fndecl
, arg0
);
10500 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
10501 CASE_FLT_FN (BUILT_IN_RINT
):
10502 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg0
);
10504 CASE_FLT_FN (BUILT_IN_ICEIL
):
10505 CASE_FLT_FN (BUILT_IN_LCEIL
):
10506 CASE_FLT_FN (BUILT_IN_LLCEIL
):
10507 CASE_FLT_FN (BUILT_IN_LFLOOR
):
10508 CASE_FLT_FN (BUILT_IN_IFLOOR
):
10509 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
10510 CASE_FLT_FN (BUILT_IN_IROUND
):
10511 CASE_FLT_FN (BUILT_IN_LROUND
):
10512 CASE_FLT_FN (BUILT_IN_LLROUND
):
10513 return fold_builtin_int_roundingfn (loc
, fndecl
, arg0
);
10515 CASE_FLT_FN (BUILT_IN_IRINT
):
10516 CASE_FLT_FN (BUILT_IN_LRINT
):
10517 CASE_FLT_FN (BUILT_IN_LLRINT
):
10518 return fold_fixed_mathfn (loc
, fndecl
, arg0
);
10520 case BUILT_IN_BSWAP32
:
10521 case BUILT_IN_BSWAP64
:
10522 return fold_builtin_bswap (fndecl
, arg0
);
10524 CASE_INT_FN (BUILT_IN_FFS
):
10525 CASE_INT_FN (BUILT_IN_CLZ
):
10526 CASE_INT_FN (BUILT_IN_CTZ
):
10527 CASE_INT_FN (BUILT_IN_CLRSB
):
10528 CASE_INT_FN (BUILT_IN_POPCOUNT
):
10529 CASE_INT_FN (BUILT_IN_PARITY
):
10530 return fold_builtin_bitop (fndecl
, arg0
);
10532 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
10533 return fold_builtin_signbit (loc
, arg0
, type
);
10535 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
10536 return fold_builtin_significand (loc
, arg0
, type
);
10538 CASE_FLT_FN (BUILT_IN_ILOGB
):
10539 CASE_FLT_FN (BUILT_IN_LOGB
):
10540 return fold_builtin_logb (loc
, arg0
, type
);
10542 case BUILT_IN_ISASCII
:
10543 return fold_builtin_isascii (loc
, arg0
);
10545 case BUILT_IN_TOASCII
:
10546 return fold_builtin_toascii (loc
, arg0
);
10548 case BUILT_IN_ISDIGIT
:
10549 return fold_builtin_isdigit (loc
, arg0
);
10551 CASE_FLT_FN (BUILT_IN_FINITE
):
10552 case BUILT_IN_FINITED32
:
10553 case BUILT_IN_FINITED64
:
10554 case BUILT_IN_FINITED128
:
10555 case BUILT_IN_ISFINITE
:
10557 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISFINITE
);
10560 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10563 CASE_FLT_FN (BUILT_IN_ISINF
):
10564 case BUILT_IN_ISINFD32
:
10565 case BUILT_IN_ISINFD64
:
10566 case BUILT_IN_ISINFD128
:
10568 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF
);
10571 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10574 case BUILT_IN_ISNORMAL
:
10575 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10577 case BUILT_IN_ISINF_SIGN
:
10578 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF_SIGN
);
10580 CASE_FLT_FN (BUILT_IN_ISNAN
):
10581 case BUILT_IN_ISNAND32
:
10582 case BUILT_IN_ISNAND64
:
10583 case BUILT_IN_ISNAND128
:
10584 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISNAN
);
10586 case BUILT_IN_PRINTF
:
10587 case BUILT_IN_PRINTF_UNLOCKED
:
10588 case BUILT_IN_VPRINTF
:
10589 return fold_builtin_printf (loc
, fndecl
, arg0
, NULL_TREE
, ignore
, fcode
);
10591 case BUILT_IN_FREE
:
10592 if (integer_zerop (arg0
))
10593 return build_empty_stmt (loc
);
10604 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10605 IGNORE is true if the result of the function call is ignored. This
10606 function returns NULL_TREE if no simplification was possible. */
10609 fold_builtin_2 (location_t loc
, tree fndecl
, tree arg0
, tree arg1
, bool ignore
)
10611 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10612 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10616 CASE_FLT_FN (BUILT_IN_JN
):
10617 if (validate_arg (arg0
, INTEGER_TYPE
)
10618 && validate_arg (arg1
, REAL_TYPE
))
10619 return do_mpfr_bessel_n (arg0
, arg1
, type
, mpfr_jn
, NULL
, 0);
10622 CASE_FLT_FN (BUILT_IN_YN
):
10623 if (validate_arg (arg0
, INTEGER_TYPE
)
10624 && validate_arg (arg1
, REAL_TYPE
))
10625 return do_mpfr_bessel_n (arg0
, arg1
, type
, mpfr_yn
,
10629 CASE_FLT_FN (BUILT_IN_DREM
):
10630 CASE_FLT_FN (BUILT_IN_REMAINDER
):
10631 if (validate_arg (arg0
, REAL_TYPE
)
10632 && validate_arg(arg1
, REAL_TYPE
))
10633 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_remainder
);
10636 CASE_FLT_FN_REENT (BUILT_IN_GAMMA
): /* GAMMA_R */
10637 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA
): /* LGAMMA_R */
10638 if (validate_arg (arg0
, REAL_TYPE
)
10639 && validate_arg(arg1
, POINTER_TYPE
))
10640 return do_mpfr_lgamma_r (arg0
, arg1
, type
);
10643 CASE_FLT_FN (BUILT_IN_ATAN2
):
10644 if (validate_arg (arg0
, REAL_TYPE
)
10645 && validate_arg(arg1
, REAL_TYPE
))
10646 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_atan2
);
10649 CASE_FLT_FN (BUILT_IN_FDIM
):
10650 if (validate_arg (arg0
, REAL_TYPE
)
10651 && validate_arg(arg1
, REAL_TYPE
))
10652 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_dim
);
10655 CASE_FLT_FN (BUILT_IN_HYPOT
):
10656 return fold_builtin_hypot (loc
, fndecl
, arg0
, arg1
, type
);
10658 CASE_FLT_FN (BUILT_IN_CPOW
):
10659 if (validate_arg (arg0
, COMPLEX_TYPE
)
10660 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
10661 && validate_arg (arg1
, COMPLEX_TYPE
)
10662 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
)
10663 return do_mpc_arg2 (arg0
, arg1
, type
, /*do_nonfinite=*/ 0, mpc_pow
);
10666 CASE_FLT_FN (BUILT_IN_LDEXP
):
10667 return fold_builtin_load_exponent (loc
, arg0
, arg1
, type
, /*ldexp=*/true);
10668 CASE_FLT_FN (BUILT_IN_SCALBN
):
10669 CASE_FLT_FN (BUILT_IN_SCALBLN
):
10670 return fold_builtin_load_exponent (loc
, arg0
, arg1
,
10671 type
, /*ldexp=*/false);
10673 CASE_FLT_FN (BUILT_IN_FREXP
):
10674 return fold_builtin_frexp (loc
, arg0
, arg1
, type
);
10676 CASE_FLT_FN (BUILT_IN_MODF
):
10677 return fold_builtin_modf (loc
, arg0
, arg1
, type
);
10679 case BUILT_IN_BZERO
:
10680 return fold_builtin_bzero (loc
, arg0
, arg1
, ignore
);
10682 case BUILT_IN_FPUTS
:
10683 return fold_builtin_fputs (loc
, arg0
, arg1
, ignore
, false, NULL_TREE
);
10685 case BUILT_IN_FPUTS_UNLOCKED
:
10686 return fold_builtin_fputs (loc
, arg0
, arg1
, ignore
, true, NULL_TREE
);
10688 case BUILT_IN_STRSTR
:
10689 return fold_builtin_strstr (loc
, arg0
, arg1
, type
);
10691 case BUILT_IN_STRCAT
:
10692 return fold_builtin_strcat (loc
, arg0
, arg1
);
10694 case BUILT_IN_STRSPN
:
10695 return fold_builtin_strspn (loc
, arg0
, arg1
);
10697 case BUILT_IN_STRCSPN
:
10698 return fold_builtin_strcspn (loc
, arg0
, arg1
);
10700 case BUILT_IN_STRCHR
:
10701 case BUILT_IN_INDEX
:
10702 return fold_builtin_strchr (loc
, arg0
, arg1
, type
);
10704 case BUILT_IN_STRRCHR
:
10705 case BUILT_IN_RINDEX
:
10706 return fold_builtin_strrchr (loc
, arg0
, arg1
, type
);
10708 case BUILT_IN_STRCPY
:
10709 return fold_builtin_strcpy (loc
, fndecl
, arg0
, arg1
, NULL_TREE
);
10711 case BUILT_IN_STPCPY
:
10714 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
10718 return build_call_expr_loc (loc
, fn
, 2, arg0
, arg1
);
10721 return fold_builtin_stpcpy (loc
, fndecl
, arg0
, arg1
);
10724 case BUILT_IN_STRCMP
:
10725 return fold_builtin_strcmp (loc
, arg0
, arg1
);
10727 case BUILT_IN_STRPBRK
:
10728 return fold_builtin_strpbrk (loc
, arg0
, arg1
, type
);
10730 case BUILT_IN_EXPECT
:
10731 return fold_builtin_expect (loc
, arg0
, arg1
);
10733 CASE_FLT_FN (BUILT_IN_POW
):
10734 return fold_builtin_pow (loc
, fndecl
, arg0
, arg1
, type
);
10736 CASE_FLT_FN (BUILT_IN_POWI
):
10737 return fold_builtin_powi (loc
, fndecl
, arg0
, arg1
, type
);
10739 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
10740 return fold_builtin_copysign (loc
, fndecl
, arg0
, arg1
, type
);
10742 CASE_FLT_FN (BUILT_IN_FMIN
):
10743 return fold_builtin_fmin_fmax (loc
, arg0
, arg1
, type
, /*max=*/false);
10745 CASE_FLT_FN (BUILT_IN_FMAX
):
10746 return fold_builtin_fmin_fmax (loc
, arg0
, arg1
, type
, /*max=*/true);
10748 case BUILT_IN_ISGREATER
:
10749 return fold_builtin_unordered_cmp (loc
, fndecl
,
10750 arg0
, arg1
, UNLE_EXPR
, LE_EXPR
);
10751 case BUILT_IN_ISGREATEREQUAL
:
10752 return fold_builtin_unordered_cmp (loc
, fndecl
,
10753 arg0
, arg1
, UNLT_EXPR
, LT_EXPR
);
10754 case BUILT_IN_ISLESS
:
10755 return fold_builtin_unordered_cmp (loc
, fndecl
,
10756 arg0
, arg1
, UNGE_EXPR
, GE_EXPR
);
10757 case BUILT_IN_ISLESSEQUAL
:
10758 return fold_builtin_unordered_cmp (loc
, fndecl
,
10759 arg0
, arg1
, UNGT_EXPR
, GT_EXPR
);
10760 case BUILT_IN_ISLESSGREATER
:
10761 return fold_builtin_unordered_cmp (loc
, fndecl
,
10762 arg0
, arg1
, UNEQ_EXPR
, EQ_EXPR
);
10763 case BUILT_IN_ISUNORDERED
:
10764 return fold_builtin_unordered_cmp (loc
, fndecl
,
10765 arg0
, arg1
, UNORDERED_EXPR
,
10768 /* We do the folding for va_start in the expander. */
10769 case BUILT_IN_VA_START
:
10772 case BUILT_IN_SPRINTF
:
10773 return fold_builtin_sprintf (loc
, arg0
, arg1
, NULL_TREE
, ignore
);
10775 case BUILT_IN_OBJECT_SIZE
:
10776 return fold_builtin_object_size (arg0
, arg1
);
10778 case BUILT_IN_PRINTF
:
10779 case BUILT_IN_PRINTF_UNLOCKED
:
10780 case BUILT_IN_VPRINTF
:
10781 return fold_builtin_printf (loc
, fndecl
, arg0
, arg1
, ignore
, fcode
);
10783 case BUILT_IN_PRINTF_CHK
:
10784 case BUILT_IN_VPRINTF_CHK
:
10785 if (!validate_arg (arg0
, INTEGER_TYPE
)
10786 || TREE_SIDE_EFFECTS (arg0
))
10789 return fold_builtin_printf (loc
, fndecl
,
10790 arg1
, NULL_TREE
, ignore
, fcode
);
10793 case BUILT_IN_FPRINTF
:
10794 case BUILT_IN_FPRINTF_UNLOCKED
:
10795 case BUILT_IN_VFPRINTF
:
10796 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg1
, NULL_TREE
,
10799 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
10800 return fold_builtin_atomic_always_lock_free (arg0
, arg1
);
10802 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
10803 return fold_builtin_atomic_is_lock_free (arg0
, arg1
);
10811 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10812 and ARG2. IGNORE is true if the result of the function call is ignored.
10813 This function returns NULL_TREE if no simplification was possible. */
10816 fold_builtin_3 (location_t loc
, tree fndecl
,
10817 tree arg0
, tree arg1
, tree arg2
, bool ignore
)
10819 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10820 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10824 CASE_FLT_FN (BUILT_IN_SINCOS
):
10825 return fold_builtin_sincos (loc
, arg0
, arg1
, arg2
);
10827 CASE_FLT_FN (BUILT_IN_FMA
):
10828 return fold_builtin_fma (loc
, arg0
, arg1
, arg2
, type
);
10831 CASE_FLT_FN (BUILT_IN_REMQUO
):
10832 if (validate_arg (arg0
, REAL_TYPE
)
10833 && validate_arg(arg1
, REAL_TYPE
)
10834 && validate_arg(arg2
, POINTER_TYPE
))
10835 return do_mpfr_remquo (arg0
, arg1
, arg2
);
10838 case BUILT_IN_MEMSET
:
10839 return fold_builtin_memset (loc
, arg0
, arg1
, arg2
, type
, ignore
);
10841 case BUILT_IN_BCOPY
:
10842 return fold_builtin_memory_op (loc
, arg1
, arg0
, arg2
,
10843 void_type_node
, true, /*endp=*/3);
10845 case BUILT_IN_MEMCPY
:
10846 return fold_builtin_memory_op (loc
, arg0
, arg1
, arg2
,
10847 type
, ignore
, /*endp=*/0);
10849 case BUILT_IN_MEMPCPY
:
10850 return fold_builtin_memory_op (loc
, arg0
, arg1
, arg2
,
10851 type
, ignore
, /*endp=*/1);
10853 case BUILT_IN_MEMMOVE
:
10854 return fold_builtin_memory_op (loc
, arg0
, arg1
, arg2
,
10855 type
, ignore
, /*endp=*/3);
10857 case BUILT_IN_STRNCAT
:
10858 return fold_builtin_strncat (loc
, arg0
, arg1
, arg2
);
10860 case BUILT_IN_STRNCPY
:
10861 return fold_builtin_strncpy (loc
, fndecl
, arg0
, arg1
, arg2
, NULL_TREE
);
10863 case BUILT_IN_STRNCMP
:
10864 return fold_builtin_strncmp (loc
, arg0
, arg1
, arg2
);
10866 case BUILT_IN_MEMCHR
:
10867 return fold_builtin_memchr (loc
, arg0
, arg1
, arg2
, type
);
10869 case BUILT_IN_BCMP
:
10870 case BUILT_IN_MEMCMP
:
10871 return fold_builtin_memcmp (loc
, arg0
, arg1
, arg2
);;
10873 case BUILT_IN_SPRINTF
:
10874 return fold_builtin_sprintf (loc
, arg0
, arg1
, arg2
, ignore
);
10876 case BUILT_IN_SNPRINTF
:
10877 return fold_builtin_snprintf (loc
, arg0
, arg1
, arg2
, NULL_TREE
, ignore
);
10879 case BUILT_IN_STRCPY_CHK
:
10880 case BUILT_IN_STPCPY_CHK
:
10881 return fold_builtin_stxcpy_chk (loc
, fndecl
, arg0
, arg1
, arg2
, NULL_TREE
,
10884 case BUILT_IN_STRCAT_CHK
:
10885 return fold_builtin_strcat_chk (loc
, fndecl
, arg0
, arg1
, arg2
);
10887 case BUILT_IN_PRINTF_CHK
:
10888 case BUILT_IN_VPRINTF_CHK
:
10889 if (!validate_arg (arg0
, INTEGER_TYPE
)
10890 || TREE_SIDE_EFFECTS (arg0
))
10893 return fold_builtin_printf (loc
, fndecl
, arg1
, arg2
, ignore
, fcode
);
10896 case BUILT_IN_FPRINTF
:
10897 case BUILT_IN_FPRINTF_UNLOCKED
:
10898 case BUILT_IN_VFPRINTF
:
10899 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg1
, arg2
,
10902 case BUILT_IN_FPRINTF_CHK
:
10903 case BUILT_IN_VFPRINTF_CHK
:
10904 if (!validate_arg (arg1
, INTEGER_TYPE
)
10905 || TREE_SIDE_EFFECTS (arg1
))
10908 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg2
, NULL_TREE
,
10917 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10918 ARG2, and ARG3. IGNORE is true if the result of the function call is
10919 ignored. This function returns NULL_TREE if no simplification was
10923 fold_builtin_4 (location_t loc
, tree fndecl
,
10924 tree arg0
, tree arg1
, tree arg2
, tree arg3
, bool ignore
)
10926 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10930 case BUILT_IN_MEMCPY_CHK
:
10931 case BUILT_IN_MEMPCPY_CHK
:
10932 case BUILT_IN_MEMMOVE_CHK
:
10933 case BUILT_IN_MEMSET_CHK
:
10934 return fold_builtin_memory_chk (loc
, fndecl
, arg0
, arg1
, arg2
, arg3
,
10936 DECL_FUNCTION_CODE (fndecl
));
10938 case BUILT_IN_STRNCPY_CHK
:
10939 case BUILT_IN_STPNCPY_CHK
:
10940 return fold_builtin_stxncpy_chk (loc
, arg0
, arg1
, arg2
, arg3
, NULL_TREE
,
10943 case BUILT_IN_STRNCAT_CHK
:
10944 return fold_builtin_strncat_chk (loc
, fndecl
, arg0
, arg1
, arg2
, arg3
);
10946 case BUILT_IN_SNPRINTF
:
10947 return fold_builtin_snprintf (loc
, arg0
, arg1
, arg2
, arg3
, ignore
);
10949 case BUILT_IN_FPRINTF_CHK
:
10950 case BUILT_IN_VFPRINTF_CHK
:
10951 if (!validate_arg (arg1
, INTEGER_TYPE
)
10952 || TREE_SIDE_EFFECTS (arg1
))
10955 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg2
, arg3
,
10965 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10966 arguments, where NARGS <= 4. IGNORE is true if the result of the
10967 function call is ignored. This function returns NULL_TREE if no
10968 simplification was possible. Note that this only folds builtins with
10969 fixed argument patterns. Foldings that do varargs-to-varargs
10970 transformations, or that match calls with more than 4 arguments,
10971 need to be handled with fold_builtin_varargs instead. */
10973 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10976 fold_builtin_n (location_t loc
, tree fndecl
, tree
*args
, int nargs
, bool ignore
)
10978 tree ret
= NULL_TREE
;
10983 ret
= fold_builtin_0 (loc
, fndecl
, ignore
);
10986 ret
= fold_builtin_1 (loc
, fndecl
, args
[0], ignore
);
10989 ret
= fold_builtin_2 (loc
, fndecl
, args
[0], args
[1], ignore
);
10992 ret
= fold_builtin_3 (loc
, fndecl
, args
[0], args
[1], args
[2], ignore
);
10995 ret
= fold_builtin_4 (loc
, fndecl
, args
[0], args
[1], args
[2], args
[3],
11003 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
11004 SET_EXPR_LOCATION (ret
, loc
);
11005 TREE_NO_WARNING (ret
) = 1;
11011 /* Builtins with folding operations that operate on "..." arguments
11012 need special handling; we need to store the arguments in a convenient
11013 data structure before attempting any folding. Fortunately there are
11014 only a few builtins that fall into this category. FNDECL is the
11015 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11016 result of the function call is ignored. */
11019 fold_builtin_varargs (location_t loc
, tree fndecl
, tree exp
,
11020 bool ignore ATTRIBUTE_UNUSED
)
11022 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
11023 tree ret
= NULL_TREE
;
11027 case BUILT_IN_SPRINTF_CHK
:
11028 case BUILT_IN_VSPRINTF_CHK
:
11029 ret
= fold_builtin_sprintf_chk (loc
, exp
, fcode
);
11032 case BUILT_IN_SNPRINTF_CHK
:
11033 case BUILT_IN_VSNPRINTF_CHK
:
11034 ret
= fold_builtin_snprintf_chk (loc
, exp
, NULL_TREE
, fcode
);
11037 case BUILT_IN_FPCLASSIFY
:
11038 ret
= fold_builtin_fpclassify (loc
, exp
);
11046 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
11047 SET_EXPR_LOCATION (ret
, loc
);
11048 TREE_NO_WARNING (ret
) = 1;
11054 /* Return true if FNDECL shouldn't be folded right now.
11055 If a built-in function has an inline attribute always_inline
11056 wrapper, defer folding it after always_inline functions have
11057 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11058 might not be performed. */
11061 avoid_folding_inline_builtin (tree fndecl
)
11063 return (DECL_DECLARED_INLINE_P (fndecl
)
11064 && DECL_DISREGARD_INLINE_LIMITS (fndecl
)
11066 && !cfun
->always_inline_functions_inlined
11067 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl
)));
11070 /* A wrapper function for builtin folding that prevents warnings for
11071 "statement without effect" and the like, caused by removing the
11072 call node earlier than the warning is generated. */
11075 fold_call_expr (location_t loc
, tree exp
, bool ignore
)
11077 tree ret
= NULL_TREE
;
11078 tree fndecl
= get_callee_fndecl (exp
);
11080 && TREE_CODE (fndecl
) == FUNCTION_DECL
11081 && DECL_BUILT_IN (fndecl
)
11082 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11083 yet. Defer folding until we see all the arguments
11084 (after inlining). */
11085 && !CALL_EXPR_VA_ARG_PACK (exp
))
11087 int nargs
= call_expr_nargs (exp
);
11089 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11090 instead last argument is __builtin_va_arg_pack (). Defer folding
11091 even in that case, until arguments are finalized. */
11092 if (nargs
&& TREE_CODE (CALL_EXPR_ARG (exp
, nargs
- 1)) == CALL_EXPR
)
11094 tree fndecl2
= get_callee_fndecl (CALL_EXPR_ARG (exp
, nargs
- 1));
11096 && TREE_CODE (fndecl2
) == FUNCTION_DECL
11097 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
11098 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
11102 if (avoid_folding_inline_builtin (fndecl
))
11105 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
11106 return targetm
.fold_builtin (fndecl
, call_expr_nargs (exp
),
11107 CALL_EXPR_ARGP (exp
), ignore
);
11110 if (nargs
<= MAX_ARGS_TO_FOLD_BUILTIN
)
11112 tree
*args
= CALL_EXPR_ARGP (exp
);
11113 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
11116 ret
= fold_builtin_varargs (loc
, fndecl
, exp
, ignore
);
11124 /* Conveniently construct a function call expression. FNDECL names the
11125 function to be called and N arguments are passed in the array
11129 build_call_expr_loc_array (location_t loc
, tree fndecl
, int n
, tree
*argarray
)
11131 tree fntype
= TREE_TYPE (fndecl
);
11132 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), fndecl
);
11134 return fold_builtin_call_array (loc
, TREE_TYPE (fntype
), fn
, n
, argarray
);
11137 /* Conveniently construct a function call expression. FNDECL names the
11138 function to be called and the arguments are passed in the vector
11142 build_call_expr_loc_vec (location_t loc
, tree fndecl
, VEC(tree
,gc
) *vec
)
11144 return build_call_expr_loc_array (loc
, fndecl
, VEC_length (tree
, vec
),
11145 VEC_address (tree
, vec
));
11149 /* Conveniently construct a function call expression. FNDECL names the
11150 function to be called, N is the number of arguments, and the "..."
11151 parameters are the argument expressions. */
11154 build_call_expr_loc (location_t loc
, tree fndecl
, int n
, ...)
11157 tree
*argarray
= XALLOCAVEC (tree
, n
);
11161 for (i
= 0; i
< n
; i
++)
11162 argarray
[i
] = va_arg (ap
, tree
);
11164 return build_call_expr_loc_array (loc
, fndecl
, n
, argarray
);
11167 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11168 varargs macros aren't supported by all bootstrap compilers. */
11171 build_call_expr (tree fndecl
, int n
, ...)
11174 tree
*argarray
= XALLOCAVEC (tree
, n
);
11178 for (i
= 0; i
< n
; i
++)
11179 argarray
[i
] = va_arg (ap
, tree
);
11181 return build_call_expr_loc_array (UNKNOWN_LOCATION
, fndecl
, n
, argarray
);
11184 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11185 N arguments are passed in the array ARGARRAY. */
11188 fold_builtin_call_array (location_t loc
, tree type
,
11193 tree ret
= NULL_TREE
;
11196 if (TREE_CODE (fn
) == ADDR_EXPR
)
11198 tree fndecl
= TREE_OPERAND (fn
, 0);
11199 if (TREE_CODE (fndecl
) == FUNCTION_DECL
11200 && DECL_BUILT_IN (fndecl
))
11202 /* If last argument is __builtin_va_arg_pack (), arguments to this
11203 function are not finalized yet. Defer folding until they are. */
11204 if (n
&& TREE_CODE (argarray
[n
- 1]) == CALL_EXPR
)
11206 tree fndecl2
= get_callee_fndecl (argarray
[n
- 1]);
11208 && TREE_CODE (fndecl2
) == FUNCTION_DECL
11209 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
11210 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
11211 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
11213 if (avoid_folding_inline_builtin (fndecl
))
11214 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
11215 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
11217 ret
= targetm
.fold_builtin (fndecl
, n
, argarray
, false);
11221 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
11223 else if (n
<= MAX_ARGS_TO_FOLD_BUILTIN
)
11225 /* First try the transformations that don't require consing up
11227 ret
= fold_builtin_n (loc
, fndecl
, argarray
, n
, false);
11232 /* If we got this far, we need to build an exp. */
11233 exp
= build_call_array_loc (loc
, type
, fn
, n
, argarray
);
11234 ret
= fold_builtin_varargs (loc
, fndecl
, exp
, false);
11235 return ret
? ret
: exp
;
11239 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
11242 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11243 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11244 of arguments in ARGS to be omitted. OLDNARGS is the number of
11245 elements in ARGS. */
11248 rewrite_call_expr_valist (location_t loc
, int oldnargs
, tree
*args
,
11249 int skip
, tree fndecl
, int n
, va_list newargs
)
11251 int nargs
= oldnargs
- skip
+ n
;
11258 buffer
= XALLOCAVEC (tree
, nargs
);
11259 for (i
= 0; i
< n
; i
++)
11260 buffer
[i
] = va_arg (newargs
, tree
);
11261 for (j
= skip
; j
< oldnargs
; j
++, i
++)
11262 buffer
[i
] = args
[j
];
11265 buffer
= args
+ skip
;
11267 return build_call_expr_loc_array (loc
, fndecl
, nargs
, buffer
);
11270 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11271 list ARGS along with N new arguments specified as the "..."
11272 parameters. SKIP is the number of arguments in ARGS to be omitted.
11273 OLDNARGS is the number of elements in ARGS. */
11276 rewrite_call_expr_array (location_t loc
, int oldnargs
, tree
*args
,
11277 int skip
, tree fndecl
, int n
, ...)
11283 t
= rewrite_call_expr_valist (loc
, oldnargs
, args
, skip
, fndecl
, n
, ap
);
11289 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11290 along with N new arguments specified as the "..." parameters. SKIP
11291 is the number of arguments in EXP to be omitted. This function is used
11292 to do varargs-to-varargs transformations. */
11295 rewrite_call_expr (location_t loc
, tree exp
, int skip
, tree fndecl
, int n
, ...)
11301 t
= rewrite_call_expr_valist (loc
, call_expr_nargs (exp
),
11302 CALL_EXPR_ARGP (exp
), skip
, fndecl
, n
, ap
);
11308 /* Validate a single argument ARG against a tree code CODE representing
11312 validate_arg (const_tree arg
, enum tree_code code
)
11316 else if (code
== POINTER_TYPE
)
11317 return POINTER_TYPE_P (TREE_TYPE (arg
));
11318 else if (code
== INTEGER_TYPE
)
11319 return INTEGRAL_TYPE_P (TREE_TYPE (arg
));
11320 return code
== TREE_CODE (TREE_TYPE (arg
));
11323 /* This function validates the types of a function call argument list
11324 against a specified list of tree_codes. If the last specifier is a 0,
11325 that represents an ellipses, otherwise the last specifier must be a
11328 This is the GIMPLE version of validate_arglist. Eventually we want to
11329 completely convert builtins.c to work from GIMPLEs and the tree based
11330 validate_arglist will then be removed. */
11333 validate_gimple_arglist (const_gimple call
, ...)
11335 enum tree_code code
;
11341 va_start (ap
, call
);
11346 code
= (enum tree_code
) va_arg (ap
, int);
11350 /* This signifies an ellipses, any further arguments are all ok. */
11354 /* This signifies an endlink, if no arguments remain, return
11355 true, otherwise return false. */
11356 res
= (i
== gimple_call_num_args (call
));
11359 /* If no parameters remain or the parameter's code does not
11360 match the specified code, return false. Otherwise continue
11361 checking any remaining arguments. */
11362 arg
= gimple_call_arg (call
, i
++);
11363 if (!validate_arg (arg
, code
))
11370 /* We need gotos here since we can only have one VA_CLOSE in a
11378 /* This function validates the types of a function call argument list
11379 against a specified list of tree_codes. If the last specifier is a 0,
11380 that represents an ellipses, otherwise the last specifier must be a
11384 validate_arglist (const_tree callexpr
, ...)
11386 enum tree_code code
;
11389 const_call_expr_arg_iterator iter
;
11392 va_start (ap
, callexpr
);
11393 init_const_call_expr_arg_iterator (callexpr
, &iter
);
11397 code
= (enum tree_code
) va_arg (ap
, int);
11401 /* This signifies an ellipses, any further arguments are all ok. */
11405 /* This signifies an endlink, if no arguments remain, return
11406 true, otherwise return false. */
11407 res
= !more_const_call_expr_args_p (&iter
);
11410 /* If no parameters remain or the parameter's code does not
11411 match the specified code, return false. Otherwise continue
11412 checking any remaining arguments. */
11413 arg
= next_const_call_expr_arg (&iter
);
11414 if (!validate_arg (arg
, code
))
11421 /* We need gotos here since we can only have one VA_CLOSE in a
11429 /* Default target-specific builtin expander that does nothing. */
11432 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
11433 rtx target ATTRIBUTE_UNUSED
,
11434 rtx subtarget ATTRIBUTE_UNUSED
,
11435 enum machine_mode mode ATTRIBUTE_UNUSED
,
11436 int ignore ATTRIBUTE_UNUSED
)
11441 /* Returns true is EXP represents data that would potentially reside
11442 in a readonly section. */
11445 readonly_data_expr (tree exp
)
11449 if (TREE_CODE (exp
) != ADDR_EXPR
)
11452 exp
= get_base_address (TREE_OPERAND (exp
, 0));
11456 /* Make sure we call decl_readonly_section only for trees it
11457 can handle (since it returns true for everything it doesn't
11459 if (TREE_CODE (exp
) == STRING_CST
11460 || TREE_CODE (exp
) == CONSTRUCTOR
11461 || (TREE_CODE (exp
) == VAR_DECL
&& TREE_STATIC (exp
)))
11462 return decl_readonly_section (exp
, 0);
11467 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11468 to the call, and TYPE is its return type.
11470 Return NULL_TREE if no simplification was possible, otherwise return the
11471 simplified form of the call as a tree.
11473 The simplified form may be a constant or other expression which
11474 computes the same value, but in a more efficient manner (including
11475 calls to other builtin functions).
11477 The call may contain arguments which need to be evaluated, but
11478 which are not useful to determine the result of the call. In
11479 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11480 COMPOUND_EXPR will be an argument which must be evaluated.
11481 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11482 COMPOUND_EXPR in the chain will contain the tree for the simplified
11483 form of the builtin function call. */
11486 fold_builtin_strstr (location_t loc
, tree s1
, tree s2
, tree type
)
11488 if (!validate_arg (s1
, POINTER_TYPE
)
11489 || !validate_arg (s2
, POINTER_TYPE
))
11494 const char *p1
, *p2
;
11496 p2
= c_getstr (s2
);
11500 p1
= c_getstr (s1
);
11503 const char *r
= strstr (p1
, p2
);
11507 return build_int_cst (TREE_TYPE (s1
), 0);
11509 /* Return an offset into the constant string argument. */
11510 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11511 return fold_convert_loc (loc
, type
, tem
);
11514 /* The argument is const char *, and the result is char *, so we need
11515 a type conversion here to avoid a warning. */
11517 return fold_convert_loc (loc
, type
, s1
);
11522 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
11526 /* New argument list transforming strstr(s1, s2) to
11527 strchr(s1, s2[0]). */
11528 return build_call_expr_loc (loc
, fn
, 2, s1
,
11529 build_int_cst (integer_type_node
, p2
[0]));
11533 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11534 the call, and TYPE is its return type.
11536 Return NULL_TREE if no simplification was possible, otherwise return the
11537 simplified form of the call as a tree.
11539 The simplified form may be a constant or other expression which
11540 computes the same value, but in a more efficient manner (including
11541 calls to other builtin functions).
11543 The call may contain arguments which need to be evaluated, but
11544 which are not useful to determine the result of the call. In
11545 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11546 COMPOUND_EXPR will be an argument which must be evaluated.
11547 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11548 COMPOUND_EXPR in the chain will contain the tree for the simplified
11549 form of the builtin function call. */
11552 fold_builtin_strchr (location_t loc
, tree s1
, tree s2
, tree type
)
11554 if (!validate_arg (s1
, POINTER_TYPE
)
11555 || !validate_arg (s2
, INTEGER_TYPE
))
11561 if (TREE_CODE (s2
) != INTEGER_CST
)
11564 p1
= c_getstr (s1
);
11571 if (target_char_cast (s2
, &c
))
11574 r
= strchr (p1
, c
);
11577 return build_int_cst (TREE_TYPE (s1
), 0);
11579 /* Return an offset into the constant string argument. */
11580 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11581 return fold_convert_loc (loc
, type
, tem
);
11587 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11588 the call, and TYPE is its return type.
11590 Return NULL_TREE if no simplification was possible, otherwise return the
11591 simplified form of the call as a tree.
11593 The simplified form may be a constant or other expression which
11594 computes the same value, but in a more efficient manner (including
11595 calls to other builtin functions).
11597 The call may contain arguments which need to be evaluated, but
11598 which are not useful to determine the result of the call. In
11599 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11600 COMPOUND_EXPR will be an argument which must be evaluated.
11601 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11602 COMPOUND_EXPR in the chain will contain the tree for the simplified
11603 form of the builtin function call. */
11606 fold_builtin_strrchr (location_t loc
, tree s1
, tree s2
, tree type
)
11608 if (!validate_arg (s1
, POINTER_TYPE
)
11609 || !validate_arg (s2
, INTEGER_TYPE
))
11616 if (TREE_CODE (s2
) != INTEGER_CST
)
11619 p1
= c_getstr (s1
);
11626 if (target_char_cast (s2
, &c
))
11629 r
= strrchr (p1
, c
);
11632 return build_int_cst (TREE_TYPE (s1
), 0);
11634 /* Return an offset into the constant string argument. */
11635 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11636 return fold_convert_loc (loc
, type
, tem
);
11639 if (! integer_zerop (s2
))
11642 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
11646 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11647 return build_call_expr_loc (loc
, fn
, 2, s1
, s2
);
11651 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11652 to the call, and TYPE is its return type.
11654 Return NULL_TREE if no simplification was possible, otherwise return the
11655 simplified form of the call as a tree.
11657 The simplified form may be a constant or other expression which
11658 computes the same value, but in a more efficient manner (including
11659 calls to other builtin functions).
11661 The call may contain arguments which need to be evaluated, but
11662 which are not useful to determine the result of the call. In
11663 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11664 COMPOUND_EXPR will be an argument which must be evaluated.
11665 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11666 COMPOUND_EXPR in the chain will contain the tree for the simplified
11667 form of the builtin function call. */
11670 fold_builtin_strpbrk (location_t loc
, tree s1
, tree s2
, tree type
)
11672 if (!validate_arg (s1
, POINTER_TYPE
)
11673 || !validate_arg (s2
, POINTER_TYPE
))
11678 const char *p1
, *p2
;
11680 p2
= c_getstr (s2
);
11684 p1
= c_getstr (s1
);
11687 const char *r
= strpbrk (p1
, p2
);
11691 return build_int_cst (TREE_TYPE (s1
), 0);
11693 /* Return an offset into the constant string argument. */
11694 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11695 return fold_convert_loc (loc
, type
, tem
);
11699 /* strpbrk(x, "") == NULL.
11700 Evaluate and ignore s1 in case it had side-effects. */
11701 return omit_one_operand_loc (loc
, TREE_TYPE (s1
), integer_zero_node
, s1
);
11704 return NULL_TREE
; /* Really call strpbrk. */
11706 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
11710 /* New argument list transforming strpbrk(s1, s2) to
11711 strchr(s1, s2[0]). */
11712 return build_call_expr_loc (loc
, fn
, 2, s1
,
11713 build_int_cst (integer_type_node
, p2
[0]));
11717 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11720 Return NULL_TREE if no simplification was possible, otherwise return the
11721 simplified form of the call as a tree.
11723 The simplified form may be a constant or other expression which
11724 computes the same value, but in a more efficient manner (including
11725 calls to other builtin functions).
11727 The call may contain arguments which need to be evaluated, but
11728 which are not useful to determine the result of the call. In
11729 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11730 COMPOUND_EXPR will be an argument which must be evaluated.
11731 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11732 COMPOUND_EXPR in the chain will contain the tree for the simplified
11733 form of the builtin function call. */
11736 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED
, tree dst
, tree src
)
11738 if (!validate_arg (dst
, POINTER_TYPE
)
11739 || !validate_arg (src
, POINTER_TYPE
))
11743 const char *p
= c_getstr (src
);
11745 /* If the string length is zero, return the dst parameter. */
11746 if (p
&& *p
== '\0')
11749 if (optimize_insn_for_speed_p ())
11751 /* See if we can store by pieces into (dst + strlen(dst)). */
11753 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
11754 tree strcpy_fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
11756 if (!strlen_fn
|| !strcpy_fn
)
11759 /* If we don't have a movstr we don't want to emit an strcpy
11760 call. We have to do that if the length of the source string
11761 isn't computable (in that case we can use memcpy probably
11762 later expanding to a sequence of mov instructions). If we
11763 have movstr instructions we can emit strcpy calls. */
11766 tree len
= c_strlen (src
, 1);
11767 if (! len
|| TREE_SIDE_EFFECTS (len
))
11771 /* Stabilize the argument list. */
11772 dst
= builtin_save_expr (dst
);
11774 /* Create strlen (dst). */
11775 newdst
= build_call_expr_loc (loc
, strlen_fn
, 1, dst
);
11776 /* Create (dst p+ strlen (dst)). */
11778 newdst
= fold_build_pointer_plus_loc (loc
, dst
, newdst
);
11779 newdst
= builtin_save_expr (newdst
);
11781 call
= build_call_expr_loc (loc
, strcpy_fn
, 2, newdst
, src
);
11782 return build2 (COMPOUND_EXPR
, TREE_TYPE (dst
), call
, dst
);
11788 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11789 arguments to the call.
11791 Return NULL_TREE if no simplification was possible, otherwise return the
11792 simplified form of the call as a tree.
11794 The simplified form may be a constant or other expression which
11795 computes the same value, but in a more efficient manner (including
11796 calls to other builtin functions).
11798 The call may contain arguments which need to be evaluated, but
11799 which are not useful to determine the result of the call. In
11800 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11801 COMPOUND_EXPR will be an argument which must be evaluated.
11802 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11803 COMPOUND_EXPR in the chain will contain the tree for the simplified
11804 form of the builtin function call. */
11807 fold_builtin_strncat (location_t loc
, tree dst
, tree src
, tree len
)
11809 if (!validate_arg (dst
, POINTER_TYPE
)
11810 || !validate_arg (src
, POINTER_TYPE
)
11811 || !validate_arg (len
, INTEGER_TYPE
))
11815 const char *p
= c_getstr (src
);
11817 /* If the requested length is zero, or the src parameter string
11818 length is zero, return the dst parameter. */
11819 if (integer_zerop (len
) || (p
&& *p
== '\0'))
11820 return omit_two_operands_loc (loc
, TREE_TYPE (dst
), dst
, src
, len
);
11822 /* If the requested len is greater than or equal to the string
11823 length, call strcat. */
11824 if (TREE_CODE (len
) == INTEGER_CST
&& p
11825 && compare_tree_int (len
, strlen (p
)) >= 0)
11827 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
11829 /* If the replacement _DECL isn't initialized, don't do the
11834 return build_call_expr_loc (loc
, fn
, 2, dst
, src
);
11840 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11843 Return NULL_TREE if no simplification was possible, otherwise return the
11844 simplified form of the call as a tree.
11846 The simplified form may be a constant or other expression which
11847 computes the same value, but in a more efficient manner (including
11848 calls to other builtin functions).
11850 The call may contain arguments which need to be evaluated, but
11851 which are not useful to determine the result of the call. In
11852 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11853 COMPOUND_EXPR will be an argument which must be evaluated.
11854 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11855 COMPOUND_EXPR in the chain will contain the tree for the simplified
11856 form of the builtin function call. */
11859 fold_builtin_strspn (location_t loc
, tree s1
, tree s2
)
11861 if (!validate_arg (s1
, POINTER_TYPE
)
11862 || !validate_arg (s2
, POINTER_TYPE
))
11866 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
11868 /* If both arguments are constants, evaluate at compile-time. */
11871 const size_t r
= strspn (p1
, p2
);
11872 return size_int (r
);
11875 /* If either argument is "", return NULL_TREE. */
11876 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
11877 /* Evaluate and ignore both arguments in case either one has
11879 return omit_two_operands_loc (loc
, size_type_node
, size_zero_node
,
11885 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11888 Return NULL_TREE if no simplification was possible, otherwise return the
11889 simplified form of the call as a tree.
11891 The simplified form may be a constant or other expression which
11892 computes the same value, but in a more efficient manner (including
11893 calls to other builtin functions).
11895 The call may contain arguments which need to be evaluated, but
11896 which are not useful to determine the result of the call. In
11897 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11898 COMPOUND_EXPR will be an argument which must be evaluated.
11899 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11900 COMPOUND_EXPR in the chain will contain the tree for the simplified
11901 form of the builtin function call. */
11904 fold_builtin_strcspn (location_t loc
, tree s1
, tree s2
)
11906 if (!validate_arg (s1
, POINTER_TYPE
)
11907 || !validate_arg (s2
, POINTER_TYPE
))
11911 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
11913 /* If both arguments are constants, evaluate at compile-time. */
11916 const size_t r
= strcspn (p1
, p2
);
11917 return size_int (r
);
11920 /* If the first argument is "", return NULL_TREE. */
11921 if (p1
&& *p1
== '\0')
11923 /* Evaluate and ignore argument s2 in case it has
11925 return omit_one_operand_loc (loc
, size_type_node
,
11926 size_zero_node
, s2
);
11929 /* If the second argument is "", return __builtin_strlen(s1). */
11930 if (p2
&& *p2
== '\0')
11932 tree fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
11934 /* If the replacement _DECL isn't initialized, don't do the
11939 return build_call_expr_loc (loc
, fn
, 1, s1
);
11945 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11946 to the call. IGNORE is true if the value returned
11947 by the builtin will be ignored. UNLOCKED is true is true if this
11948 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11949 the known length of the string. Return NULL_TREE if no simplification
11953 fold_builtin_fputs (location_t loc
, tree arg0
, tree arg1
,
11954 bool ignore
, bool unlocked
, tree len
)
11956 /* If we're using an unlocked function, assume the other unlocked
11957 functions exist explicitly. */
11958 tree
const fn_fputc
= (unlocked
11959 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
)
11960 : builtin_decl_implicit (BUILT_IN_FPUTC
));
11961 tree
const fn_fwrite
= (unlocked
11962 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED
)
11963 : builtin_decl_implicit (BUILT_IN_FWRITE
));
11965 /* If the return value is used, don't do the transformation. */
11969 /* Verify the arguments in the original call. */
11970 if (!validate_arg (arg0
, POINTER_TYPE
)
11971 || !validate_arg (arg1
, POINTER_TYPE
))
11975 len
= c_strlen (arg0
, 0);
11977 /* Get the length of the string passed to fputs. If the length
11978 can't be determined, punt. */
11980 || TREE_CODE (len
) != INTEGER_CST
)
11983 switch (compare_tree_int (len
, 1))
11985 case -1: /* length is 0, delete the call entirely . */
11986 return omit_one_operand_loc (loc
, integer_type_node
,
11987 integer_zero_node
, arg1
);;
11989 case 0: /* length is 1, call fputc. */
11991 const char *p
= c_getstr (arg0
);
11996 return build_call_expr_loc (loc
, fn_fputc
, 2,
11998 (integer_type_node
, p
[0]), arg1
);
12004 case 1: /* length is greater than 1, call fwrite. */
12006 /* If optimizing for size keep fputs. */
12007 if (optimize_function_for_size_p (cfun
))
12009 /* New argument list transforming fputs(string, stream) to
12010 fwrite(string, 1, len, stream). */
12012 return build_call_expr_loc (loc
, fn_fwrite
, 4, arg0
,
12013 size_one_node
, len
, arg1
);
12018 gcc_unreachable ();
12023 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12024 produced. False otherwise. This is done so that we don't output the error
12025 or warning twice or three times. */
12028 fold_builtin_next_arg (tree exp
, bool va_start_p
)
12030 tree fntype
= TREE_TYPE (current_function_decl
);
12031 int nargs
= call_expr_nargs (exp
);
12034 if (!stdarg_p (fntype
))
12036 error ("%<va_start%> used in function with fixed args");
12042 if (va_start_p
&& (nargs
!= 2))
12044 error ("wrong number of arguments to function %<va_start%>");
12047 arg
= CALL_EXPR_ARG (exp
, 1);
12049 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12050 when we checked the arguments and if needed issued a warning. */
12055 /* Evidently an out of date version of <stdarg.h>; can't validate
12056 va_start's second argument, but can still work as intended. */
12057 warning (0, "%<__builtin_next_arg%> called without an argument");
12060 else if (nargs
> 1)
12062 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12065 arg
= CALL_EXPR_ARG (exp
, 0);
12068 if (TREE_CODE (arg
) == SSA_NAME
)
12069 arg
= SSA_NAME_VAR (arg
);
12071 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12072 or __builtin_next_arg (0) the first time we see it, after checking
12073 the arguments and if needed issuing a warning. */
12074 if (!integer_zerop (arg
))
12076 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
12078 /* Strip off all nops for the sake of the comparison. This
12079 is not quite the same as STRIP_NOPS. It does more.
12080 We must also strip off INDIRECT_EXPR for C++ reference
12082 while (CONVERT_EXPR_P (arg
)
12083 || TREE_CODE (arg
) == INDIRECT_REF
)
12084 arg
= TREE_OPERAND (arg
, 0);
12085 if (arg
!= last_parm
)
12087 /* FIXME: Sometimes with the tree optimizers we can get the
12088 not the last argument even though the user used the last
12089 argument. We just warn and set the arg to be the last
12090 argument so that we will get wrong-code because of
12092 warning (0, "second parameter of %<va_start%> not last named argument");
12095 /* Undefined by C99 7.15.1.4p4 (va_start):
12096 "If the parameter parmN is declared with the register storage
12097 class, with a function or array type, or with a type that is
12098 not compatible with the type that results after application of
12099 the default argument promotions, the behavior is undefined."
12101 else if (DECL_REGISTER (arg
))
12102 warning (0, "undefined behaviour when second parameter of "
12103 "%<va_start%> is declared with %<register%> storage");
12105 /* We want to verify the second parameter just once before the tree
12106 optimizers are run and then avoid keeping it in the tree,
12107 as otherwise we could warn even for correct code like:
12108 void foo (int i, ...)
12109 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12111 CALL_EXPR_ARG (exp
, 1) = integer_zero_node
;
12113 CALL_EXPR_ARG (exp
, 0) = integer_zero_node
;
12119 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12120 ORIG may be null if this is a 2-argument call. We don't attempt to
12121 simplify calls with more than 3 arguments.
12123 Return NULL_TREE if no simplification was possible, otherwise return the
12124 simplified form of the call as a tree. If IGNORED is true, it means that
12125 the caller does not use the returned value of the function. */
12128 fold_builtin_sprintf (location_t loc
, tree dest
, tree fmt
,
12129 tree orig
, int ignored
)
12132 const char *fmt_str
= NULL
;
12134 /* Verify the required arguments in the original call. We deal with two
12135 types of sprintf() calls: 'sprintf (str, fmt)' and
12136 'sprintf (dest, "%s", orig)'. */
12137 if (!validate_arg (dest
, POINTER_TYPE
)
12138 || !validate_arg (fmt
, POINTER_TYPE
))
12140 if (orig
&& !validate_arg (orig
, POINTER_TYPE
))
12143 /* Check whether the format is a literal string constant. */
12144 fmt_str
= c_getstr (fmt
);
12145 if (fmt_str
== NULL
)
12149 retval
= NULL_TREE
;
12151 if (!init_target_chars ())
12154 /* If the format doesn't contain % args or %%, use strcpy. */
12155 if (strchr (fmt_str
, target_percent
) == NULL
)
12157 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
12162 /* Don't optimize sprintf (buf, "abc", ptr++). */
12166 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12167 'format' is known to contain no % formats. */
12168 call
= build_call_expr_loc (loc
, fn
, 2, dest
, fmt
);
12170 retval
= build_int_cst (integer_type_node
, strlen (fmt_str
));
12173 /* If the format is "%s", use strcpy if the result isn't used. */
12174 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
12177 fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
12182 /* Don't crash on sprintf (str1, "%s"). */
12186 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12189 retval
= c_strlen (orig
, 1);
12190 if (!retval
|| TREE_CODE (retval
) != INTEGER_CST
)
12193 call
= build_call_expr_loc (loc
, fn
, 2, dest
, orig
);
12196 if (call
&& retval
)
12198 retval
= fold_convert_loc
12199 (loc
, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF
))),
12201 return build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), call
, retval
);
12207 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12208 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12209 attempt to simplify calls with more than 4 arguments.
12211 Return NULL_TREE if no simplification was possible, otherwise return the
12212 simplified form of the call as a tree. If IGNORED is true, it means that
12213 the caller does not use the returned value of the function. */
12216 fold_builtin_snprintf (location_t loc
, tree dest
, tree destsize
, tree fmt
,
12217 tree orig
, int ignored
)
12220 const char *fmt_str
= NULL
;
12221 unsigned HOST_WIDE_INT destlen
;
12223 /* Verify the required arguments in the original call. We deal with two
12224 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12225 'snprintf (dest, cst, "%s", orig)'. */
12226 if (!validate_arg (dest
, POINTER_TYPE
)
12227 || !validate_arg (destsize
, INTEGER_TYPE
)
12228 || !validate_arg (fmt
, POINTER_TYPE
))
12230 if (orig
&& !validate_arg (orig
, POINTER_TYPE
))
12233 if (!host_integerp (destsize
, 1))
12236 /* Check whether the format is a literal string constant. */
12237 fmt_str
= c_getstr (fmt
);
12238 if (fmt_str
== NULL
)
12242 retval
= NULL_TREE
;
12244 if (!init_target_chars ())
12247 destlen
= tree_low_cst (destsize
, 1);
12249 /* If the format doesn't contain % args or %%, use strcpy. */
12250 if (strchr (fmt_str
, target_percent
) == NULL
)
12252 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
12253 size_t len
= strlen (fmt_str
);
12255 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12259 /* We could expand this as
12260 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12262 memcpy (str, fmt_with_nul_at_cstm1, cst);
12263 but in the former case that might increase code size
12264 and in the latter case grow .rodata section too much.
12265 So punt for now. */
12266 if (len
>= destlen
)
12272 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12273 'format' is known to contain no % formats and
12274 strlen (fmt) < cst. */
12275 call
= build_call_expr_loc (loc
, fn
, 2, dest
, fmt
);
12278 retval
= build_int_cst (integer_type_node
, strlen (fmt_str
));
12281 /* If the format is "%s", use strcpy if the result isn't used. */
12282 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
12284 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
12285 unsigned HOST_WIDE_INT origlen
;
12287 /* Don't crash on snprintf (str1, cst, "%s"). */
12291 retval
= c_strlen (orig
, 1);
12292 if (!retval
|| !host_integerp (retval
, 1))
12295 origlen
= tree_low_cst (retval
, 1);
12296 /* We could expand this as
12297 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12299 memcpy (str1, str2_with_nul_at_cstm1, cst);
12300 but in the former case that might increase code size
12301 and in the latter case grow .rodata section too much.
12302 So punt for now. */
12303 if (origlen
>= destlen
)
12306 /* Convert snprintf (str1, cst, "%s", str2) into
12307 strcpy (str1, str2) if strlen (str2) < cst. */
12311 call
= build_call_expr_loc (loc
, fn
, 2, dest
, orig
);
12314 retval
= NULL_TREE
;
12317 if (call
&& retval
)
12319 tree fn
= builtin_decl_explicit (BUILT_IN_SNPRINTF
);
12320 retval
= fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fn
)), retval
);
12321 return build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), call
, retval
);
12327 /* Expand a call EXP to __builtin_object_size. */
12330 expand_builtin_object_size (tree exp
)
12333 int object_size_type
;
12334 tree fndecl
= get_callee_fndecl (exp
);
12336 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
12338 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12340 expand_builtin_trap ();
12344 ost
= CALL_EXPR_ARG (exp
, 1);
12347 if (TREE_CODE (ost
) != INTEGER_CST
12348 || tree_int_cst_sgn (ost
) < 0
12349 || compare_tree_int (ost
, 3) > 0)
12351 error ("%Klast argument of %D is not integer constant between 0 and 3",
12353 expand_builtin_trap ();
12357 object_size_type
= tree_low_cst (ost
, 0);
12359 return object_size_type
< 2 ? constm1_rtx
: const0_rtx
;
12362 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12363 FCODE is the BUILT_IN_* to use.
12364 Return NULL_RTX if we failed; the caller should emit a normal call,
12365 otherwise try to get the result in TARGET, if convenient (and in
12366 mode MODE if that's convenient). */
12369 expand_builtin_memory_chk (tree exp
, rtx target
, enum machine_mode mode
,
12370 enum built_in_function fcode
)
12372 tree dest
, src
, len
, size
;
12374 if (!validate_arglist (exp
,
12376 fcode
== BUILT_IN_MEMSET_CHK
12377 ? INTEGER_TYPE
: POINTER_TYPE
,
12378 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
12381 dest
= CALL_EXPR_ARG (exp
, 0);
12382 src
= CALL_EXPR_ARG (exp
, 1);
12383 len
= CALL_EXPR_ARG (exp
, 2);
12384 size
= CALL_EXPR_ARG (exp
, 3);
12386 if (! host_integerp (size
, 1))
12389 if (host_integerp (len
, 1) || integer_all_onesp (size
))
12393 if (! integer_all_onesp (size
) && tree_int_cst_lt (size
, len
))
12395 warning_at (tree_nonartificial_location (exp
),
12396 0, "%Kcall to %D will always overflow destination buffer",
12397 exp
, get_callee_fndecl (exp
));
12402 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12403 mem{cpy,pcpy,move,set} is available. */
12406 case BUILT_IN_MEMCPY_CHK
:
12407 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
12409 case BUILT_IN_MEMPCPY_CHK
:
12410 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
12412 case BUILT_IN_MEMMOVE_CHK
:
12413 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
12415 case BUILT_IN_MEMSET_CHK
:
12416 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
12425 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 3, dest
, src
, len
);
12426 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
12427 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
12428 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
12430 else if (fcode
== BUILT_IN_MEMSET_CHK
)
12434 unsigned int dest_align
= get_pointer_alignment (dest
);
12436 /* If DEST is not a pointer type, call the normal function. */
12437 if (dest_align
== 0)
12440 /* If SRC and DEST are the same (and not volatile), do nothing. */
12441 if (operand_equal_p (src
, dest
, 0))
12445 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
12447 /* Evaluate and ignore LEN in case it has side-effects. */
12448 expand_expr (len
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
12449 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
12452 expr
= fold_build_pointer_plus (dest
, len
);
12453 return expand_expr (expr
, target
, mode
, EXPAND_NORMAL
);
12456 /* __memmove_chk special case. */
12457 if (fcode
== BUILT_IN_MEMMOVE_CHK
)
12459 unsigned int src_align
= get_pointer_alignment (src
);
12461 if (src_align
== 0)
12464 /* If src is categorized for a readonly section we can use
12465 normal __memcpy_chk. */
12466 if (readonly_data_expr (src
))
12468 tree fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
12471 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 4,
12472 dest
, src
, len
, size
);
12473 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
12474 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
12475 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
12482 /* Emit warning if a buffer overflow is detected at compile time. */
12485 maybe_emit_chk_warning (tree exp
, enum built_in_function fcode
)
12489 location_t loc
= tree_nonartificial_location (exp
);
12493 case BUILT_IN_STRCPY_CHK
:
12494 case BUILT_IN_STPCPY_CHK
:
12495 /* For __strcat_chk the warning will be emitted only if overflowing
12496 by at least strlen (dest) + 1 bytes. */
12497 case BUILT_IN_STRCAT_CHK
:
12498 len
= CALL_EXPR_ARG (exp
, 1);
12499 size
= CALL_EXPR_ARG (exp
, 2);
12502 case BUILT_IN_STRNCAT_CHK
:
12503 case BUILT_IN_STRNCPY_CHK
:
12504 case BUILT_IN_STPNCPY_CHK
:
12505 len
= CALL_EXPR_ARG (exp
, 2);
12506 size
= CALL_EXPR_ARG (exp
, 3);
12508 case BUILT_IN_SNPRINTF_CHK
:
12509 case BUILT_IN_VSNPRINTF_CHK
:
12510 len
= CALL_EXPR_ARG (exp
, 1);
12511 size
= CALL_EXPR_ARG (exp
, 3);
12514 gcc_unreachable ();
12520 if (! host_integerp (size
, 1) || integer_all_onesp (size
))
12525 len
= c_strlen (len
, 1);
12526 if (! len
|| ! host_integerp (len
, 1) || tree_int_cst_lt (len
, size
))
12529 else if (fcode
== BUILT_IN_STRNCAT_CHK
)
12531 tree src
= CALL_EXPR_ARG (exp
, 1);
12532 if (! src
|| ! host_integerp (len
, 1) || tree_int_cst_lt (len
, size
))
12534 src
= c_strlen (src
, 1);
12535 if (! src
|| ! host_integerp (src
, 1))
12537 warning_at (loc
, 0, "%Kcall to %D might overflow destination buffer",
12538 exp
, get_callee_fndecl (exp
));
12541 else if (tree_int_cst_lt (src
, size
))
12544 else if (! host_integerp (len
, 1) || ! tree_int_cst_lt (size
, len
))
12547 warning_at (loc
, 0, "%Kcall to %D will always overflow destination buffer",
12548 exp
, get_callee_fndecl (exp
));
12551 /* Emit warning if a buffer overflow is detected at compile time
12552 in __sprintf_chk/__vsprintf_chk calls. */
12555 maybe_emit_sprintf_chk_warning (tree exp
, enum built_in_function fcode
)
12557 tree size
, len
, fmt
;
12558 const char *fmt_str
;
12559 int nargs
= call_expr_nargs (exp
);
12561 /* Verify the required arguments in the original call. */
12565 size
= CALL_EXPR_ARG (exp
, 2);
12566 fmt
= CALL_EXPR_ARG (exp
, 3);
12568 if (! host_integerp (size
, 1) || integer_all_onesp (size
))
12571 /* Check whether the format is a literal string constant. */
12572 fmt_str
= c_getstr (fmt
);
12573 if (fmt_str
== NULL
)
12576 if (!init_target_chars ())
12579 /* If the format doesn't contain % args or %%, we know its size. */
12580 if (strchr (fmt_str
, target_percent
) == 0)
12581 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
12582 /* If the format is "%s" and first ... argument is a string literal,
12584 else if (fcode
== BUILT_IN_SPRINTF_CHK
12585 && strcmp (fmt_str
, target_percent_s
) == 0)
12591 arg
= CALL_EXPR_ARG (exp
, 4);
12592 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
12595 len
= c_strlen (arg
, 1);
12596 if (!len
|| ! host_integerp (len
, 1))
12602 if (! tree_int_cst_lt (len
, size
))
12603 warning_at (tree_nonartificial_location (exp
),
12604 0, "%Kcall to %D will always overflow destination buffer",
12605 exp
, get_callee_fndecl (exp
));
12608 /* Emit warning if a free is called with address of a variable. */
12611 maybe_emit_free_warning (tree exp
)
12613 tree arg
= CALL_EXPR_ARG (exp
, 0);
12616 if (TREE_CODE (arg
) != ADDR_EXPR
)
12619 arg
= get_base_address (TREE_OPERAND (arg
, 0));
12620 if (arg
== NULL
|| INDIRECT_REF_P (arg
) || TREE_CODE (arg
) == MEM_REF
)
12623 if (SSA_VAR_P (arg
))
12624 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
12625 "%Kattempt to free a non-heap object %qD", exp
, arg
);
12627 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
12628 "%Kattempt to free a non-heap object", exp
);
12631 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12635 fold_builtin_object_size (tree ptr
, tree ost
)
12637 unsigned HOST_WIDE_INT bytes
;
12638 int object_size_type
;
12640 if (!validate_arg (ptr
, POINTER_TYPE
)
12641 || !validate_arg (ost
, INTEGER_TYPE
))
12646 if (TREE_CODE (ost
) != INTEGER_CST
12647 || tree_int_cst_sgn (ost
) < 0
12648 || compare_tree_int (ost
, 3) > 0)
12651 object_size_type
= tree_low_cst (ost
, 0);
12653 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12654 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12655 and (size_t) 0 for types 2 and 3. */
12656 if (TREE_SIDE_EFFECTS (ptr
))
12657 return build_int_cst_type (size_type_node
, object_size_type
< 2 ? -1 : 0);
12659 if (TREE_CODE (ptr
) == ADDR_EXPR
)
12661 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
12662 if (double_int_fits_to_tree_p (size_type_node
,
12663 uhwi_to_double_int (bytes
)))
12664 return build_int_cstu (size_type_node
, bytes
);
12666 else if (TREE_CODE (ptr
) == SSA_NAME
)
12668 /* If object size is not known yet, delay folding until
12669 later. Maybe subsequent passes will help determining
12671 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
12672 if (bytes
!= (unsigned HOST_WIDE_INT
) (object_size_type
< 2 ? -1 : 0)
12673 && double_int_fits_to_tree_p (size_type_node
,
12674 uhwi_to_double_int (bytes
)))
12675 return build_int_cstu (size_type_node
, bytes
);
12681 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12682 DEST, SRC, LEN, and SIZE are the arguments to the call.
12683 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12684 code of the builtin. If MAXLEN is not NULL, it is maximum length
12685 passed as third argument. */
12688 fold_builtin_memory_chk (location_t loc
, tree fndecl
,
12689 tree dest
, tree src
, tree len
, tree size
,
12690 tree maxlen
, bool ignore
,
12691 enum built_in_function fcode
)
12695 if (!validate_arg (dest
, POINTER_TYPE
)
12696 || !validate_arg (src
,
12697 (fcode
== BUILT_IN_MEMSET_CHK
12698 ? INTEGER_TYPE
: POINTER_TYPE
))
12699 || !validate_arg (len
, INTEGER_TYPE
)
12700 || !validate_arg (size
, INTEGER_TYPE
))
12703 /* If SRC and DEST are the same (and not volatile), return DEST
12704 (resp. DEST+LEN for __mempcpy_chk). */
12705 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
12707 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
12708 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)),
12712 tree temp
= fold_build_pointer_plus_loc (loc
, dest
, len
);
12713 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), temp
);
12717 if (! host_integerp (size
, 1))
12720 if (! integer_all_onesp (size
))
12722 if (! host_integerp (len
, 1))
12724 /* If LEN is not constant, try MAXLEN too.
12725 For MAXLEN only allow optimizing into non-_ocs function
12726 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12727 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
12729 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
12731 /* (void) __mempcpy_chk () can be optimized into
12732 (void) __memcpy_chk (). */
12733 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
12737 return build_call_expr_loc (loc
, fn
, 4, dest
, src
, len
, size
);
12745 if (tree_int_cst_lt (size
, maxlen
))
12750 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12751 mem{cpy,pcpy,move,set} is available. */
12754 case BUILT_IN_MEMCPY_CHK
:
12755 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
12757 case BUILT_IN_MEMPCPY_CHK
:
12758 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
12760 case BUILT_IN_MEMMOVE_CHK
:
12761 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
12763 case BUILT_IN_MEMSET_CHK
:
12764 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
12773 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
12776 /* Fold a call to the __st[rp]cpy_chk builtin.
12777 DEST, SRC, and SIZE are the arguments to the call.
12778 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12779 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12780 strings passed as second argument. */
12783 fold_builtin_stxcpy_chk (location_t loc
, tree fndecl
, tree dest
,
12784 tree src
, tree size
,
12785 tree maxlen
, bool ignore
,
12786 enum built_in_function fcode
)
12790 if (!validate_arg (dest
, POINTER_TYPE
)
12791 || !validate_arg (src
, POINTER_TYPE
)
12792 || !validate_arg (size
, INTEGER_TYPE
))
12795 /* If SRC and DEST are the same (and not volatile), return DEST. */
12796 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
12797 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
);
12799 if (! host_integerp (size
, 1))
12802 if (! integer_all_onesp (size
))
12804 len
= c_strlen (src
, 1);
12805 if (! len
|| ! host_integerp (len
, 1))
12807 /* If LEN is not constant, try MAXLEN too.
12808 For MAXLEN only allow optimizing into non-_ocs function
12809 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12810 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
12812 if (fcode
== BUILT_IN_STPCPY_CHK
)
12817 /* If return value of __stpcpy_chk is ignored,
12818 optimize into __strcpy_chk. */
12819 fn
= builtin_decl_explicit (BUILT_IN_STRCPY_CHK
);
12823 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, size
);
12826 if (! len
|| TREE_SIDE_EFFECTS (len
))
12829 /* If c_strlen returned something, but not a constant,
12830 transform __strcpy_chk into __memcpy_chk. */
12831 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
12835 len
= fold_convert_loc (loc
, size_type_node
, len
);
12836 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
12837 build_int_cst (size_type_node
, 1));
12838 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)),
12839 build_call_expr_loc (loc
, fn
, 4,
12840 dest
, src
, len
, size
));
12846 if (! tree_int_cst_lt (maxlen
, size
))
12850 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12851 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPCPY_CHK
12852 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
);
12856 return build_call_expr_loc (loc
, fn
, 2, dest
, src
);
12859 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12860 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12861 length passed as third argument. IGNORE is true if return value can be
12862 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12865 fold_builtin_stxncpy_chk (location_t loc
, tree dest
, tree src
,
12866 tree len
, tree size
, tree maxlen
, bool ignore
,
12867 enum built_in_function fcode
)
12871 if (!validate_arg (dest
, POINTER_TYPE
)
12872 || !validate_arg (src
, POINTER_TYPE
)
12873 || !validate_arg (len
, INTEGER_TYPE
)
12874 || !validate_arg (size
, INTEGER_TYPE
))
12877 if (fcode
== BUILT_IN_STPNCPY_CHK
&& ignore
)
12879 /* If return value of __stpncpy_chk is ignored,
12880 optimize into __strncpy_chk. */
12881 fn
= builtin_decl_explicit (BUILT_IN_STRNCPY_CHK
);
12883 return build_call_expr_loc (loc
, fn
, 4, dest
, src
, len
, size
);
12886 if (! host_integerp (size
, 1))
12889 if (! integer_all_onesp (size
))
12891 if (! host_integerp (len
, 1))
12893 /* If LEN is not constant, try MAXLEN too.
12894 For MAXLEN only allow optimizing into non-_ocs function
12895 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12896 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
12902 if (tree_int_cst_lt (size
, maxlen
))
12906 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12907 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPNCPY_CHK
12908 ? BUILT_IN_STPNCPY
: BUILT_IN_STRNCPY
);
12912 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
12915 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12916 are the arguments to the call. */
12919 fold_builtin_strcat_chk (location_t loc
, tree fndecl
, tree dest
,
12920 tree src
, tree size
)
12925 if (!validate_arg (dest
, POINTER_TYPE
)
12926 || !validate_arg (src
, POINTER_TYPE
)
12927 || !validate_arg (size
, INTEGER_TYPE
))
12930 p
= c_getstr (src
);
12931 /* If the SRC parameter is "", return DEST. */
12932 if (p
&& *p
== '\0')
12933 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
12935 if (! host_integerp (size
, 1) || ! integer_all_onesp (size
))
12938 /* If __builtin_strcat_chk is used, assume strcat is available. */
12939 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
12943 return build_call_expr_loc (loc
, fn
, 2, dest
, src
);
12946 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
12950 fold_builtin_strncat_chk (location_t loc
, tree fndecl
,
12951 tree dest
, tree src
, tree len
, tree size
)
12956 if (!validate_arg (dest
, POINTER_TYPE
)
12957 || !validate_arg (src
, POINTER_TYPE
)
12958 || !validate_arg (size
, INTEGER_TYPE
)
12959 || !validate_arg (size
, INTEGER_TYPE
))
12962 p
= c_getstr (src
);
12963 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
12964 if (p
&& *p
== '\0')
12965 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, len
);
12966 else if (integer_zerop (len
))
12967 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
12969 if (! host_integerp (size
, 1))
12972 if (! integer_all_onesp (size
))
12974 tree src_len
= c_strlen (src
, 1);
12976 && host_integerp (src_len
, 1)
12977 && host_integerp (len
, 1)
12978 && ! tree_int_cst_lt (len
, src_len
))
12980 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
12981 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
12985 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, size
);
12990 /* If __builtin_strncat_chk is used, assume strncat is available. */
12991 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
12995 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
12998 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
12999 Return NULL_TREE if a normal call should be emitted rather than
13000 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13001 or BUILT_IN_VSPRINTF_CHK. */
13004 fold_builtin_sprintf_chk_1 (location_t loc
, int nargs
, tree
*args
,
13005 enum built_in_function fcode
)
13007 tree dest
, size
, len
, fn
, fmt
, flag
;
13008 const char *fmt_str
;
13010 /* Verify the required arguments in the original call. */
13014 if (!validate_arg (dest
, POINTER_TYPE
))
13017 if (!validate_arg (flag
, INTEGER_TYPE
))
13020 if (!validate_arg (size
, INTEGER_TYPE
))
13023 if (!validate_arg (fmt
, POINTER_TYPE
))
13026 if (! host_integerp (size
, 1))
13031 if (!init_target_chars ())
13034 /* Check whether the format is a literal string constant. */
13035 fmt_str
= c_getstr (fmt
);
13036 if (fmt_str
!= NULL
)
13038 /* If the format doesn't contain % args or %%, we know the size. */
13039 if (strchr (fmt_str
, target_percent
) == 0)
13041 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| nargs
== 4)
13042 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
13044 /* If the format is "%s" and first ... argument is a string literal,
13045 we know the size too. */
13046 else if (fcode
== BUILT_IN_SPRINTF_CHK
13047 && strcmp (fmt_str
, target_percent_s
) == 0)
13054 if (validate_arg (arg
, POINTER_TYPE
))
13056 len
= c_strlen (arg
, 1);
13057 if (! len
|| ! host_integerp (len
, 1))
13064 if (! integer_all_onesp (size
))
13066 if (! len
|| ! tree_int_cst_lt (len
, size
))
13070 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13071 or if format doesn't contain % chars or is "%s". */
13072 if (! integer_zerop (flag
))
13074 if (fmt_str
== NULL
)
13076 if (strchr (fmt_str
, target_percent
) != NULL
13077 && strcmp (fmt_str
, target_percent_s
))
13081 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13082 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSPRINTF_CHK
13083 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
);
13087 return rewrite_call_expr_array (loc
, nargs
, args
, 4, fn
, 2, dest
, fmt
);
13090 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13091 a normal call should be emitted rather than expanding the function
13092 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13095 fold_builtin_sprintf_chk (location_t loc
, tree exp
,
13096 enum built_in_function fcode
)
13098 return fold_builtin_sprintf_chk_1 (loc
, call_expr_nargs (exp
),
13099 CALL_EXPR_ARGP (exp
), fcode
);
13102 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13103 NULL_TREE if a normal call should be emitted rather than expanding
13104 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13105 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13106 passed as second argument. */
13109 fold_builtin_snprintf_chk_1 (location_t loc
, int nargs
, tree
*args
,
13110 tree maxlen
, enum built_in_function fcode
)
13112 tree dest
, size
, len
, fn
, fmt
, flag
;
13113 const char *fmt_str
;
13115 /* Verify the required arguments in the original call. */
13119 if (!validate_arg (dest
, POINTER_TYPE
))
13122 if (!validate_arg (len
, INTEGER_TYPE
))
13125 if (!validate_arg (flag
, INTEGER_TYPE
))
13128 if (!validate_arg (size
, INTEGER_TYPE
))
13131 if (!validate_arg (fmt
, POINTER_TYPE
))
13134 if (! host_integerp (size
, 1))
13137 if (! integer_all_onesp (size
))
13139 if (! host_integerp (len
, 1))
13141 /* If LEN is not constant, try MAXLEN too.
13142 For MAXLEN only allow optimizing into non-_ocs function
13143 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13144 if (maxlen
== NULL_TREE
|| ! host_integerp (maxlen
, 1))
13150 if (tree_int_cst_lt (size
, maxlen
))
13154 if (!init_target_chars ())
13157 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13158 or if format doesn't contain % chars or is "%s". */
13159 if (! integer_zerop (flag
))
13161 fmt_str
= c_getstr (fmt
);
13162 if (fmt_str
== NULL
)
13164 if (strchr (fmt_str
, target_percent
) != NULL
13165 && strcmp (fmt_str
, target_percent_s
))
13169 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13171 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSNPRINTF_CHK
13172 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
);
13176 return rewrite_call_expr_array (loc
, nargs
, args
, 5, fn
, 3, dest
, len
, fmt
);
13179 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13180 a normal call should be emitted rather than expanding the function
13181 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13182 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13183 passed as second argument. */
13186 fold_builtin_snprintf_chk (location_t loc
, tree exp
, tree maxlen
,
13187 enum built_in_function fcode
)
13189 return fold_builtin_snprintf_chk_1 (loc
, call_expr_nargs (exp
),
13190 CALL_EXPR_ARGP (exp
), maxlen
, fcode
);
13193 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13194 FMT and ARG are the arguments to the call; we don't fold cases with
13195 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13197 Return NULL_TREE if no simplification was possible, otherwise return the
13198 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13199 code of the function to be simplified. */
13202 fold_builtin_printf (location_t loc
, tree fndecl
, tree fmt
,
13203 tree arg
, bool ignore
,
13204 enum built_in_function fcode
)
13206 tree fn_putchar
, fn_puts
, newarg
, call
= NULL_TREE
;
13207 const char *fmt_str
= NULL
;
13209 /* If the return value is used, don't do the transformation. */
13213 /* Verify the required arguments in the original call. */
13214 if (!validate_arg (fmt
, POINTER_TYPE
))
13217 /* Check whether the format is a literal string constant. */
13218 fmt_str
= c_getstr (fmt
);
13219 if (fmt_str
== NULL
)
13222 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
13224 /* If we're using an unlocked function, assume the other
13225 unlocked functions exist explicitly. */
13226 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
13227 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
13231 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
13232 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
13235 if (!init_target_chars ())
13238 if (strcmp (fmt_str
, target_percent_s
) == 0
13239 || strchr (fmt_str
, target_percent
) == NULL
)
13243 if (strcmp (fmt_str
, target_percent_s
) == 0)
13245 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
13248 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
13251 str
= c_getstr (arg
);
13257 /* The format specifier doesn't contain any '%' characters. */
13258 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
13264 /* If the string was "", printf does nothing. */
13265 if (str
[0] == '\0')
13266 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
13268 /* If the string has length of 1, call putchar. */
13269 if (str
[1] == '\0')
13271 /* Given printf("c"), (where c is any one character,)
13272 convert "c"[0] to an int and pass that to the replacement
13274 newarg
= build_int_cst (integer_type_node
, str
[0]);
13276 call
= build_call_expr_loc (loc
, fn_putchar
, 1, newarg
);
13280 /* If the string was "string\n", call puts("string"). */
13281 size_t len
= strlen (str
);
13282 if ((unsigned char)str
[len
- 1] == target_newline
13283 && (size_t) (int) len
== len
13287 tree offset_node
, string_cst
;
13289 /* Create a NUL-terminated string that's one char shorter
13290 than the original, stripping off the trailing '\n'. */
13291 newarg
= build_string_literal (len
, str
);
13292 string_cst
= string_constant (newarg
, &offset_node
);
13293 gcc_checking_assert (string_cst
13294 && (TREE_STRING_LENGTH (string_cst
)
13296 && integer_zerop (offset_node
)
13298 TREE_STRING_POINTER (string_cst
)[len
- 1]
13299 == target_newline
);
13300 /* build_string_literal creates a new STRING_CST,
13301 modify it in place to avoid double copying. */
13302 newstr
= CONST_CAST (char *, TREE_STRING_POINTER (string_cst
));
13303 newstr
[len
- 1] = '\0';
13305 call
= build_call_expr_loc (loc
, fn_puts
, 1, newarg
);
13308 /* We'd like to arrange to call fputs(string,stdout) here,
13309 but we need stdout and don't have a way to get it yet. */
13314 /* The other optimizations can be done only on the non-va_list variants. */
13315 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
13318 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13319 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
13321 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
13324 call
= build_call_expr_loc (loc
, fn_puts
, 1, arg
);
13327 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13328 else if (strcmp (fmt_str
, target_percent_c
) == 0)
13330 if (!arg
|| !validate_arg (arg
, INTEGER_TYPE
))
13333 call
= build_call_expr_loc (loc
, fn_putchar
, 1, arg
);
13339 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), call
);
13342 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13343 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13344 more than 3 arguments, and ARG may be null in the 2-argument case.
13346 Return NULL_TREE if no simplification was possible, otherwise return the
13347 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13348 code of the function to be simplified. */
13351 fold_builtin_fprintf (location_t loc
, tree fndecl
, tree fp
,
13352 tree fmt
, tree arg
, bool ignore
,
13353 enum built_in_function fcode
)
13355 tree fn_fputc
, fn_fputs
, call
= NULL_TREE
;
13356 const char *fmt_str
= NULL
;
13358 /* If the return value is used, don't do the transformation. */
13362 /* Verify the required arguments in the original call. */
13363 if (!validate_arg (fp
, POINTER_TYPE
))
13365 if (!validate_arg (fmt
, POINTER_TYPE
))
13368 /* Check whether the format is a literal string constant. */
13369 fmt_str
= c_getstr (fmt
);
13370 if (fmt_str
== NULL
)
13373 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
13375 /* If we're using an unlocked function, assume the other
13376 unlocked functions exist explicitly. */
13377 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
13378 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
13382 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
13383 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
13386 if (!init_target_chars ())
13389 /* If the format doesn't contain % args or %%, use strcpy. */
13390 if (strchr (fmt_str
, target_percent
) == NULL
)
13392 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
13396 /* If the format specifier was "", fprintf does nothing. */
13397 if (fmt_str
[0] == '\0')
13399 /* If FP has side-effects, just wait until gimplification is
13401 if (TREE_SIDE_EFFECTS (fp
))
13404 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
13407 /* When "string" doesn't contain %, replace all cases of
13408 fprintf (fp, string) with fputs (string, fp). The fputs
13409 builtin will take care of special cases like length == 1. */
13411 call
= build_call_expr_loc (loc
, fn_fputs
, 2, fmt
, fp
);
13414 /* The other optimizations can be done only on the non-va_list variants. */
13415 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
13418 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13419 else if (strcmp (fmt_str
, target_percent_s
) == 0)
13421 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
13424 call
= build_call_expr_loc (loc
, fn_fputs
, 2, arg
, fp
);
13427 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13428 else if (strcmp (fmt_str
, target_percent_c
) == 0)
13430 if (!arg
|| !validate_arg (arg
, INTEGER_TYPE
))
13433 call
= build_call_expr_loc (loc
, fn_fputc
, 2, arg
, fp
);
13438 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), call
);
13441 /* Initialize format string characters in the target charset. */
13444 init_target_chars (void)
13449 target_newline
= lang_hooks
.to_target_charset ('\n');
13450 target_percent
= lang_hooks
.to_target_charset ('%');
13451 target_c
= lang_hooks
.to_target_charset ('c');
13452 target_s
= lang_hooks
.to_target_charset ('s');
13453 if (target_newline
== 0 || target_percent
== 0 || target_c
== 0
13457 target_percent_c
[0] = target_percent
;
13458 target_percent_c
[1] = target_c
;
13459 target_percent_c
[2] = '\0';
13461 target_percent_s
[0] = target_percent
;
13462 target_percent_s
[1] = target_s
;
13463 target_percent_s
[2] = '\0';
13465 target_percent_s_newline
[0] = target_percent
;
13466 target_percent_s_newline
[1] = target_s
;
13467 target_percent_s_newline
[2] = target_newline
;
13468 target_percent_s_newline
[3] = '\0';
13475 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13476 and no overflow/underflow occurred. INEXACT is true if M was not
13477 exactly calculated. TYPE is the tree type for the result. This
13478 function assumes that you cleared the MPFR flags and then
13479 calculated M to see if anything subsequently set a flag prior to
13480 entering this function. Return NULL_TREE if any checks fail. */
13483 do_mpfr_ckconv (mpfr_srcptr m
, tree type
, int inexact
)
13485 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13486 overflow/underflow occurred. If -frounding-math, proceed iff the
13487 result of calling FUNC was exact. */
13488 if (mpfr_number_p (m
) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13489 && (!flag_rounding_math
|| !inexact
))
13491 REAL_VALUE_TYPE rr
;
13493 real_from_mpfr (&rr
, m
, type
, GMP_RNDN
);
13494 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13495 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13496 but the mpft_t is not, then we underflowed in the
13498 if (real_isfinite (&rr
)
13499 && (rr
.cl
== rvc_zero
) == (mpfr_zero_p (m
) != 0))
13501 REAL_VALUE_TYPE rmode
;
13503 real_convert (&rmode
, TYPE_MODE (type
), &rr
);
13504 /* Proceed iff the specified mode can hold the value. */
13505 if (real_identical (&rmode
, &rr
))
13506 return build_real (type
, rmode
);
13512 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13513 number and no overflow/underflow occurred. INEXACT is true if M
13514 was not exactly calculated. TYPE is the tree type for the result.
13515 This function assumes that you cleared the MPFR flags and then
13516 calculated M to see if anything subsequently set a flag prior to
13517 entering this function. Return NULL_TREE if any checks fail, if
13518 FORCE_CONVERT is true, then bypass the checks. */
13521 do_mpc_ckconv (mpc_srcptr m
, tree type
, int inexact
, int force_convert
)
13523 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13524 overflow/underflow occurred. If -frounding-math, proceed iff the
13525 result of calling FUNC was exact. */
13527 || (mpfr_number_p (mpc_realref (m
)) && mpfr_number_p (mpc_imagref (m
))
13528 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13529 && (!flag_rounding_math
|| !inexact
)))
13531 REAL_VALUE_TYPE re
, im
;
13533 real_from_mpfr (&re
, mpc_realref (m
), TREE_TYPE (type
), GMP_RNDN
);
13534 real_from_mpfr (&im
, mpc_imagref (m
), TREE_TYPE (type
), GMP_RNDN
);
13535 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13536 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13537 but the mpft_t is not, then we underflowed in the
13540 || (real_isfinite (&re
) && real_isfinite (&im
)
13541 && (re
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_realref (m
)) != 0)
13542 && (im
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_imagref (m
)) != 0)))
13544 REAL_VALUE_TYPE re_mode
, im_mode
;
13546 real_convert (&re_mode
, TYPE_MODE (TREE_TYPE (type
)), &re
);
13547 real_convert (&im_mode
, TYPE_MODE (TREE_TYPE (type
)), &im
);
13548 /* Proceed iff the specified mode can hold the value. */
13550 || (real_identical (&re_mode
, &re
)
13551 && real_identical (&im_mode
, &im
)))
13552 return build_complex (type
, build_real (TREE_TYPE (type
), re_mode
),
13553 build_real (TREE_TYPE (type
), im_mode
));
13559 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13560 FUNC on it and return the resulting value as a tree with type TYPE.
13561 If MIN and/or MAX are not NULL, then the supplied ARG must be
13562 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13563 acceptable values, otherwise they are not. The mpfr precision is
13564 set to the precision of TYPE. We assume that function FUNC returns
13565 zero if the result could be calculated exactly within the requested
13569 do_mpfr_arg1 (tree arg
, tree type
, int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
13570 const REAL_VALUE_TYPE
*min
, const REAL_VALUE_TYPE
*max
,
13573 tree result
= NULL_TREE
;
13577 /* To proceed, MPFR must exactly represent the target floating point
13578 format, which only happens when the target base equals two. */
13579 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13580 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
13582 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
13584 if (real_isfinite (ra
)
13585 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
))
13586 && (!max
|| real_compare (inclusive
? LE_EXPR
: LT_EXPR
, ra
, max
)))
13588 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13589 const int prec
= fmt
->p
;
13590 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13594 mpfr_init2 (m
, prec
);
13595 mpfr_from_real (m
, ra
, GMP_RNDN
);
13596 mpfr_clear_flags ();
13597 inexact
= func (m
, m
, rnd
);
13598 result
= do_mpfr_ckconv (m
, type
, inexact
);
13606 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13607 FUNC on it and return the resulting value as a tree with type TYPE.
13608 The mpfr precision is set to the precision of TYPE. We assume that
13609 function FUNC returns zero if the result could be calculated
13610 exactly within the requested precision. */
13613 do_mpfr_arg2 (tree arg1
, tree arg2
, tree type
,
13614 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
13616 tree result
= NULL_TREE
;
13621 /* To proceed, MPFR must exactly represent the target floating point
13622 format, which only happens when the target base equals two. */
13623 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13624 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
)
13625 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
))
13627 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
13628 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
13630 if (real_isfinite (ra1
) && real_isfinite (ra2
))
13632 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13633 const int prec
= fmt
->p
;
13634 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13638 mpfr_inits2 (prec
, m1
, m2
, NULL
);
13639 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
13640 mpfr_from_real (m2
, ra2
, GMP_RNDN
);
13641 mpfr_clear_flags ();
13642 inexact
= func (m1
, m1
, m2
, rnd
);
13643 result
= do_mpfr_ckconv (m1
, type
, inexact
);
13644 mpfr_clears (m1
, m2
, NULL
);
13651 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13652 FUNC on it and return the resulting value as a tree with type TYPE.
13653 The mpfr precision is set to the precision of TYPE. We assume that
13654 function FUNC returns zero if the result could be calculated
13655 exactly within the requested precision. */
13658 do_mpfr_arg3 (tree arg1
, tree arg2
, tree arg3
, tree type
,
13659 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
13661 tree result
= NULL_TREE
;
13667 /* To proceed, MPFR must exactly represent the target floating point
13668 format, which only happens when the target base equals two. */
13669 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13670 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
)
13671 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
)
13672 && TREE_CODE (arg3
) == REAL_CST
&& !TREE_OVERFLOW (arg3
))
13674 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
13675 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
13676 const REAL_VALUE_TYPE
*const ra3
= &TREE_REAL_CST (arg3
);
13678 if (real_isfinite (ra1
) && real_isfinite (ra2
) && real_isfinite (ra3
))
13680 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13681 const int prec
= fmt
->p
;
13682 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13686 mpfr_inits2 (prec
, m1
, m2
, m3
, NULL
);
13687 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
13688 mpfr_from_real (m2
, ra2
, GMP_RNDN
);
13689 mpfr_from_real (m3
, ra3
, GMP_RNDN
);
13690 mpfr_clear_flags ();
13691 inexact
= func (m1
, m1
, m2
, m3
, rnd
);
13692 result
= do_mpfr_ckconv (m1
, type
, inexact
);
13693 mpfr_clears (m1
, m2
, m3
, NULL
);
13700 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13701 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13702 If ARG_SINP and ARG_COSP are NULL then the result is returned
13703 as a complex value.
13704 The type is taken from the type of ARG and is used for setting the
13705 precision of the calculation and results. */
13708 do_mpfr_sincos (tree arg
, tree arg_sinp
, tree arg_cosp
)
13710 tree
const type
= TREE_TYPE (arg
);
13711 tree result
= NULL_TREE
;
13715 /* To proceed, MPFR must exactly represent the target floating point
13716 format, which only happens when the target base equals two. */
13717 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13718 && TREE_CODE (arg
) == REAL_CST
13719 && !TREE_OVERFLOW (arg
))
13721 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
13723 if (real_isfinite (ra
))
13725 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13726 const int prec
= fmt
->p
;
13727 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13728 tree result_s
, result_c
;
13732 mpfr_inits2 (prec
, m
, ms
, mc
, NULL
);
13733 mpfr_from_real (m
, ra
, GMP_RNDN
);
13734 mpfr_clear_flags ();
13735 inexact
= mpfr_sin_cos (ms
, mc
, m
, rnd
);
13736 result_s
= do_mpfr_ckconv (ms
, type
, inexact
);
13737 result_c
= do_mpfr_ckconv (mc
, type
, inexact
);
13738 mpfr_clears (m
, ms
, mc
, NULL
);
13739 if (result_s
&& result_c
)
13741 /* If we are to return in a complex value do so. */
13742 if (!arg_sinp
&& !arg_cosp
)
13743 return build_complex (build_complex_type (type
),
13744 result_c
, result_s
);
13746 /* Dereference the sin/cos pointer arguments. */
13747 arg_sinp
= build_fold_indirect_ref (arg_sinp
);
13748 arg_cosp
= build_fold_indirect_ref (arg_cosp
);
13749 /* Proceed if valid pointer type were passed in. */
13750 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp
)) == TYPE_MAIN_VARIANT (type
)
13751 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp
)) == TYPE_MAIN_VARIANT (type
))
13753 /* Set the values. */
13754 result_s
= fold_build2 (MODIFY_EXPR
, type
, arg_sinp
,
13756 TREE_SIDE_EFFECTS (result_s
) = 1;
13757 result_c
= fold_build2 (MODIFY_EXPR
, type
, arg_cosp
,
13759 TREE_SIDE_EFFECTS (result_c
) = 1;
13760 /* Combine the assignments into a compound expr. */
13761 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
13762 result_s
, result_c
));
13770 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13771 two-argument mpfr order N Bessel function FUNC on them and return
13772 the resulting value as a tree with type TYPE. The mpfr precision
13773 is set to the precision of TYPE. We assume that function FUNC
13774 returns zero if the result could be calculated exactly within the
13775 requested precision. */
13777 do_mpfr_bessel_n (tree arg1
, tree arg2
, tree type
,
13778 int (*func
)(mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
13779 const REAL_VALUE_TYPE
*min
, bool inclusive
)
13781 tree result
= NULL_TREE
;
13786 /* To proceed, MPFR must exactly represent the target floating point
13787 format, which only happens when the target base equals two. */
13788 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13789 && host_integerp (arg1
, 0)
13790 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
))
13792 const HOST_WIDE_INT n
= tree_low_cst(arg1
, 0);
13793 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg2
);
13796 && real_isfinite (ra
)
13797 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
)))
13799 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13800 const int prec
= fmt
->p
;
13801 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13805 mpfr_init2 (m
, prec
);
13806 mpfr_from_real (m
, ra
, GMP_RNDN
);
13807 mpfr_clear_flags ();
13808 inexact
= func (m
, n
, m
, rnd
);
13809 result
= do_mpfr_ckconv (m
, type
, inexact
);
13817 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13818 the pointer *(ARG_QUO) and return the result. The type is taken
13819 from the type of ARG0 and is used for setting the precision of the
13820 calculation and results. */
13823 do_mpfr_remquo (tree arg0
, tree arg1
, tree arg_quo
)
13825 tree
const type
= TREE_TYPE (arg0
);
13826 tree result
= NULL_TREE
;
13831 /* To proceed, MPFR must exactly represent the target floating point
13832 format, which only happens when the target base equals two. */
13833 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13834 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
13835 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
))
13837 const REAL_VALUE_TYPE
*const ra0
= TREE_REAL_CST_PTR (arg0
);
13838 const REAL_VALUE_TYPE
*const ra1
= TREE_REAL_CST_PTR (arg1
);
13840 if (real_isfinite (ra0
) && real_isfinite (ra1
))
13842 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13843 const int prec
= fmt
->p
;
13844 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13849 mpfr_inits2 (prec
, m0
, m1
, NULL
);
13850 mpfr_from_real (m0
, ra0
, GMP_RNDN
);
13851 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
13852 mpfr_clear_flags ();
13853 mpfr_remquo (m0
, &integer_quo
, m0
, m1
, rnd
);
13854 /* Remquo is independent of the rounding mode, so pass
13855 inexact=0 to do_mpfr_ckconv(). */
13856 result_rem
= do_mpfr_ckconv (m0
, type
, /*inexact=*/ 0);
13857 mpfr_clears (m0
, m1
, NULL
);
13860 /* MPFR calculates quo in the host's long so it may
13861 return more bits in quo than the target int can hold
13862 if sizeof(host long) > sizeof(target int). This can
13863 happen even for native compilers in LP64 mode. In
13864 these cases, modulo the quo value with the largest
13865 number that the target int can hold while leaving one
13866 bit for the sign. */
13867 if (sizeof (integer_quo
) * CHAR_BIT
> INT_TYPE_SIZE
)
13868 integer_quo
%= (long)(1UL << (INT_TYPE_SIZE
- 1));
13870 /* Dereference the quo pointer argument. */
13871 arg_quo
= build_fold_indirect_ref (arg_quo
);
13872 /* Proceed iff a valid pointer type was passed in. */
13873 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo
)) == integer_type_node
)
13875 /* Set the value. */
13877 = fold_build2 (MODIFY_EXPR
, TREE_TYPE (arg_quo
), arg_quo
,
13878 build_int_cst (TREE_TYPE (arg_quo
),
13880 TREE_SIDE_EFFECTS (result_quo
) = 1;
13881 /* Combine the quo assignment with the rem. */
13882 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
13883 result_quo
, result_rem
));
13891 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13892 resulting value as a tree with type TYPE. The mpfr precision is
13893 set to the precision of TYPE. We assume that this mpfr function
13894 returns zero if the result could be calculated exactly within the
13895 requested precision. In addition, the integer pointer represented
13896 by ARG_SG will be dereferenced and set to the appropriate signgam
13900 do_mpfr_lgamma_r (tree arg
, tree arg_sg
, tree type
)
13902 tree result
= NULL_TREE
;
13906 /* To proceed, MPFR must exactly represent the target floating point
13907 format, which only happens when the target base equals two. Also
13908 verify ARG is a constant and that ARG_SG is an int pointer. */
13909 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13910 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
)
13911 && TREE_CODE (TREE_TYPE (arg_sg
)) == POINTER_TYPE
13912 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg
))) == integer_type_node
)
13914 const REAL_VALUE_TYPE
*const ra
= TREE_REAL_CST_PTR (arg
);
13916 /* In addition to NaN and Inf, the argument cannot be zero or a
13917 negative integer. */
13918 if (real_isfinite (ra
)
13919 && ra
->cl
!= rvc_zero
13920 && !(real_isneg(ra
) && real_isinteger(ra
, TYPE_MODE (type
))))
13922 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13923 const int prec
= fmt
->p
;
13924 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13929 mpfr_init2 (m
, prec
);
13930 mpfr_from_real (m
, ra
, GMP_RNDN
);
13931 mpfr_clear_flags ();
13932 inexact
= mpfr_lgamma (m
, &sg
, m
, rnd
);
13933 result_lg
= do_mpfr_ckconv (m
, type
, inexact
);
13939 /* Dereference the arg_sg pointer argument. */
13940 arg_sg
= build_fold_indirect_ref (arg_sg
);
13941 /* Assign the signgam value into *arg_sg. */
13942 result_sg
= fold_build2 (MODIFY_EXPR
,
13943 TREE_TYPE (arg_sg
), arg_sg
,
13944 build_int_cst (TREE_TYPE (arg_sg
), sg
));
13945 TREE_SIDE_EFFECTS (result_sg
) = 1;
13946 /* Combine the signgam assignment with the lgamma result. */
13947 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
13948 result_sg
, result_lg
));
13956 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
13957 function FUNC on it and return the resulting value as a tree with
13958 type TYPE. The mpfr precision is set to the precision of TYPE. We
13959 assume that function FUNC returns zero if the result could be
13960 calculated exactly within the requested precision. */
13963 do_mpc_arg1 (tree arg
, tree type
, int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_rnd_t
))
13965 tree result
= NULL_TREE
;
13969 /* To proceed, MPFR must exactly represent the target floating point
13970 format, which only happens when the target base equals two. */
13971 if (TREE_CODE (arg
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg
)
13972 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
13973 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg
))))->b
== 2)
13975 const REAL_VALUE_TYPE
*const re
= TREE_REAL_CST_PTR (TREE_REALPART (arg
));
13976 const REAL_VALUE_TYPE
*const im
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg
));
13978 if (real_isfinite (re
) && real_isfinite (im
))
13980 const struct real_format
*const fmt
=
13981 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
13982 const int prec
= fmt
->p
;
13983 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13984 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
13988 mpc_init2 (m
, prec
);
13989 mpfr_from_real (mpc_realref(m
), re
, rnd
);
13990 mpfr_from_real (mpc_imagref(m
), im
, rnd
);
13991 mpfr_clear_flags ();
13992 inexact
= func (m
, m
, crnd
);
13993 result
= do_mpc_ckconv (m
, type
, inexact
, /*force_convert=*/ 0);
14001 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14002 mpc function FUNC on it and return the resulting value as a tree
14003 with type TYPE. The mpfr precision is set to the precision of
14004 TYPE. We assume that function FUNC returns zero if the result
14005 could be calculated exactly within the requested precision. If
14006 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14007 in the arguments and/or results. */
14010 do_mpc_arg2 (tree arg0
, tree arg1
, tree type
, int do_nonfinite
,
14011 int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_srcptr
, mpc_rnd_t
))
14013 tree result
= NULL_TREE
;
14018 /* To proceed, MPFR must exactly represent the target floating point
14019 format, which only happens when the target base equals two. */
14020 if (TREE_CODE (arg0
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg0
)
14021 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
14022 && TREE_CODE (arg1
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg1
)
14023 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
14024 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0
))))->b
== 2)
14026 const REAL_VALUE_TYPE
*const re0
= TREE_REAL_CST_PTR (TREE_REALPART (arg0
));
14027 const REAL_VALUE_TYPE
*const im0
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg0
));
14028 const REAL_VALUE_TYPE
*const re1
= TREE_REAL_CST_PTR (TREE_REALPART (arg1
));
14029 const REAL_VALUE_TYPE
*const im1
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg1
));
14032 || (real_isfinite (re0
) && real_isfinite (im0
)
14033 && real_isfinite (re1
) && real_isfinite (im1
)))
14035 const struct real_format
*const fmt
=
14036 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
14037 const int prec
= fmt
->p
;
14038 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
14039 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
14043 mpc_init2 (m0
, prec
);
14044 mpc_init2 (m1
, prec
);
14045 mpfr_from_real (mpc_realref(m0
), re0
, rnd
);
14046 mpfr_from_real (mpc_imagref(m0
), im0
, rnd
);
14047 mpfr_from_real (mpc_realref(m1
), re1
, rnd
);
14048 mpfr_from_real (mpc_imagref(m1
), im1
, rnd
);
14049 mpfr_clear_flags ();
14050 inexact
= func (m0
, m0
, m1
, crnd
);
14051 result
= do_mpc_ckconv (m0
, type
, inexact
, do_nonfinite
);
14060 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14061 a normal call should be emitted rather than expanding the function
14062 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14065 gimple_fold_builtin_sprintf_chk (gimple stmt
, enum built_in_function fcode
)
14067 int nargs
= gimple_call_num_args (stmt
);
14069 return fold_builtin_sprintf_chk_1 (gimple_location (stmt
), nargs
,
14071 ? gimple_call_arg_ptr (stmt
, 0)
14072 : &error_mark_node
), fcode
);
14075 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14076 a normal call should be emitted rather than expanding the function
14077 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14078 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14079 passed as second argument. */
14082 gimple_fold_builtin_snprintf_chk (gimple stmt
, tree maxlen
,
14083 enum built_in_function fcode
)
14085 int nargs
= gimple_call_num_args (stmt
);
14087 return fold_builtin_snprintf_chk_1 (gimple_location (stmt
), nargs
,
14089 ? gimple_call_arg_ptr (stmt
, 0)
14090 : &error_mark_node
), maxlen
, fcode
);
14093 /* Builtins with folding operations that operate on "..." arguments
14094 need special handling; we need to store the arguments in a convenient
14095 data structure before attempting any folding. Fortunately there are
14096 only a few builtins that fall into this category. FNDECL is the
14097 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14098 result of the function call is ignored. */
14101 gimple_fold_builtin_varargs (tree fndecl
, gimple stmt
,
14102 bool ignore ATTRIBUTE_UNUSED
)
14104 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
14105 tree ret
= NULL_TREE
;
14109 case BUILT_IN_SPRINTF_CHK
:
14110 case BUILT_IN_VSPRINTF_CHK
:
14111 ret
= gimple_fold_builtin_sprintf_chk (stmt
, fcode
);
14114 case BUILT_IN_SNPRINTF_CHK
:
14115 case BUILT_IN_VSNPRINTF_CHK
:
14116 ret
= gimple_fold_builtin_snprintf_chk (stmt
, NULL_TREE
, fcode
);
14123 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
14124 TREE_NO_WARNING (ret
) = 1;
14130 /* A wrapper function for builtin folding that prevents warnings for
14131 "statement without effect" and the like, caused by removing the
14132 call node earlier than the warning is generated. */
14135 fold_call_stmt (gimple stmt
, bool ignore
)
14137 tree ret
= NULL_TREE
;
14138 tree fndecl
= gimple_call_fndecl (stmt
);
14139 location_t loc
= gimple_location (stmt
);
14141 && TREE_CODE (fndecl
) == FUNCTION_DECL
14142 && DECL_BUILT_IN (fndecl
)
14143 && !gimple_call_va_arg_pack_p (stmt
))
14145 int nargs
= gimple_call_num_args (stmt
);
14146 tree
*args
= (nargs
> 0
14147 ? gimple_call_arg_ptr (stmt
, 0)
14148 : &error_mark_node
);
14150 if (avoid_folding_inline_builtin (fndecl
))
14152 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
14154 return targetm
.fold_builtin (fndecl
, nargs
, args
, ignore
);
14158 if (nargs
<= MAX_ARGS_TO_FOLD_BUILTIN
)
14159 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
14161 ret
= gimple_fold_builtin_varargs (fndecl
, stmt
, ignore
);
14164 /* Propagate location information from original call to
14165 expansion of builtin. Otherwise things like
14166 maybe_emit_chk_warning, that operate on the expansion
14167 of a builtin, will use the wrong location information. */
14168 if (gimple_has_location (stmt
))
14170 tree realret
= ret
;
14171 if (TREE_CODE (ret
) == NOP_EXPR
)
14172 realret
= TREE_OPERAND (ret
, 0);
14173 if (CAN_HAVE_LOCATION_P (realret
)
14174 && !EXPR_HAS_LOCATION (realret
))
14175 SET_EXPR_LOCATION (realret
, loc
);
14185 /* Look up the function in builtin_decl that corresponds to DECL
14186 and set ASMSPEC as its user assembler name. DECL must be a
14187 function decl that declares a builtin. */
14190 set_builtin_user_assembler_name (tree decl
, const char *asmspec
)
14193 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
14194 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
14197 builtin
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
14198 set_user_assembler_name (builtin
, asmspec
);
14199 switch (DECL_FUNCTION_CODE (decl
))
14201 case BUILT_IN_MEMCPY
:
14202 init_block_move_fn (asmspec
);
14203 memcpy_libfunc
= set_user_assembler_libfunc ("memcpy", asmspec
);
14205 case BUILT_IN_MEMSET
:
14206 init_block_clear_fn (asmspec
);
14207 memset_libfunc
= set_user_assembler_libfunc ("memset", asmspec
);
14209 case BUILT_IN_MEMMOVE
:
14210 memmove_libfunc
= set_user_assembler_libfunc ("memmove", asmspec
);
14212 case BUILT_IN_MEMCMP
:
14213 memcmp_libfunc
= set_user_assembler_libfunc ("memcmp", asmspec
);
14215 case BUILT_IN_ABORT
:
14216 abort_libfunc
= set_user_assembler_libfunc ("abort", asmspec
);
14219 if (INT_TYPE_SIZE
< BITS_PER_WORD
)
14221 set_user_assembler_libfunc ("ffs", asmspec
);
14222 set_optab_libfunc (ffs_optab
, mode_for_size (INT_TYPE_SIZE
,
14223 MODE_INT
, 0), "ffs");
14231 /* Return true if DECL is a builtin that expands to a constant or similarly
14234 is_simple_builtin (tree decl
)
14236 if (decl
&& DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
14237 switch (DECL_FUNCTION_CODE (decl
))
14239 /* Builtins that expand to constants. */
14240 case BUILT_IN_CONSTANT_P
:
14241 case BUILT_IN_EXPECT
:
14242 case BUILT_IN_OBJECT_SIZE
:
14243 case BUILT_IN_UNREACHABLE
:
14244 /* Simple register moves or loads from stack. */
14245 case BUILT_IN_ASSUME_ALIGNED
:
14246 case BUILT_IN_RETURN_ADDRESS
:
14247 case BUILT_IN_EXTRACT_RETURN_ADDR
:
14248 case BUILT_IN_FROB_RETURN_ADDR
:
14249 case BUILT_IN_RETURN
:
14250 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
14251 case BUILT_IN_FRAME_ADDRESS
:
14252 case BUILT_IN_VA_END
:
14253 case BUILT_IN_STACK_SAVE
:
14254 case BUILT_IN_STACK_RESTORE
:
14255 /* Exception state returns or moves registers around. */
14256 case BUILT_IN_EH_FILTER
:
14257 case BUILT_IN_EH_POINTER
:
14258 case BUILT_IN_EH_COPY_VALUES
:
14268 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14269 most probably expanded inline into reasonably simple code. This is a
14270 superset of is_simple_builtin. */
14272 is_inexpensive_builtin (tree decl
)
14276 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_MD
)
14278 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
14279 switch (DECL_FUNCTION_CODE (decl
))
14282 case BUILT_IN_ALLOCA
:
14283 case BUILT_IN_ALLOCA_WITH_ALIGN
:
14284 case BUILT_IN_BSWAP32
:
14285 case BUILT_IN_BSWAP64
:
14287 case BUILT_IN_CLZIMAX
:
14288 case BUILT_IN_CLZL
:
14289 case BUILT_IN_CLZLL
:
14291 case BUILT_IN_CTZIMAX
:
14292 case BUILT_IN_CTZL
:
14293 case BUILT_IN_CTZLL
:
14295 case BUILT_IN_FFSIMAX
:
14296 case BUILT_IN_FFSL
:
14297 case BUILT_IN_FFSLL
:
14298 case BUILT_IN_IMAXABS
:
14299 case BUILT_IN_FINITE
:
14300 case BUILT_IN_FINITEF
:
14301 case BUILT_IN_FINITEL
:
14302 case BUILT_IN_FINITED32
:
14303 case BUILT_IN_FINITED64
:
14304 case BUILT_IN_FINITED128
:
14305 case BUILT_IN_FPCLASSIFY
:
14306 case BUILT_IN_ISFINITE
:
14307 case BUILT_IN_ISINF_SIGN
:
14308 case BUILT_IN_ISINF
:
14309 case BUILT_IN_ISINFF
:
14310 case BUILT_IN_ISINFL
:
14311 case BUILT_IN_ISINFD32
:
14312 case BUILT_IN_ISINFD64
:
14313 case BUILT_IN_ISINFD128
:
14314 case BUILT_IN_ISNAN
:
14315 case BUILT_IN_ISNANF
:
14316 case BUILT_IN_ISNANL
:
14317 case BUILT_IN_ISNAND32
:
14318 case BUILT_IN_ISNAND64
:
14319 case BUILT_IN_ISNAND128
:
14320 case BUILT_IN_ISNORMAL
:
14321 case BUILT_IN_ISGREATER
:
14322 case BUILT_IN_ISGREATEREQUAL
:
14323 case BUILT_IN_ISLESS
:
14324 case BUILT_IN_ISLESSEQUAL
:
14325 case BUILT_IN_ISLESSGREATER
:
14326 case BUILT_IN_ISUNORDERED
:
14327 case BUILT_IN_VA_ARG_PACK
:
14328 case BUILT_IN_VA_ARG_PACK_LEN
:
14329 case BUILT_IN_VA_COPY
:
14330 case BUILT_IN_TRAP
:
14331 case BUILT_IN_SAVEREGS
:
14332 case BUILT_IN_POPCOUNTL
:
14333 case BUILT_IN_POPCOUNTLL
:
14334 case BUILT_IN_POPCOUNTIMAX
:
14335 case BUILT_IN_POPCOUNT
:
14336 case BUILT_IN_PARITYL
:
14337 case BUILT_IN_PARITYLL
:
14338 case BUILT_IN_PARITYIMAX
:
14339 case BUILT_IN_PARITY
:
14340 case BUILT_IN_LABS
:
14341 case BUILT_IN_LLABS
:
14342 case BUILT_IN_PREFETCH
:
14346 return is_simple_builtin (decl
);