1 /* Expand builtin functions.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "stringpool.h"
28 #include "stor-layout.h"
31 #include "tree-object-size.h"
33 #include "basic-block.h"
34 #include "tree-ssa-alias.h"
35 #include "internal-fn.h"
36 #include "gimple-expr.h"
41 #include "hard-reg-set.h"
44 #include "insn-config.h"
50 #include "typeclass.h"
54 #include "langhooks.h"
55 #include "tree-ssanames.h"
57 #include "value-prof.h"
58 #include "diagnostic-core.h"
64 static tree
do_mpc_arg1 (tree
, tree
, int (*)(mpc_ptr
, mpc_srcptr
, mpc_rnd_t
));
66 struct target_builtins default_target_builtins
;
68 struct target_builtins
*this_target_builtins
= &default_target_builtins
;
71 /* Define the names of the builtin function types and codes. */
72 const char *const built_in_class_names
[BUILT_IN_LAST
]
73 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
75 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
76 const char * built_in_names
[(int) END_BUILTINS
] =
78 #include "builtins.def"
82 /* Setup an array of _DECL trees, make sure each element is
83 initialized to NULL_TREE. */
84 builtin_info_type builtin_info
;
86 /* Non-zero if __builtin_constant_p should be folded right away. */
87 bool force_folding_builtin_constant_p
;
89 static const char *c_getstr (tree
);
90 static rtx
c_readstr (const char *, enum machine_mode
);
91 static int target_char_cast (tree
, char *);
92 static rtx
get_memory_rtx (tree
, tree
);
93 static int apply_args_size (void);
94 static int apply_result_size (void);
95 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
96 static rtx
result_vector (int, rtx
);
98 static void expand_builtin_update_setjmp_buf (rtx
);
99 static void expand_builtin_prefetch (tree
);
100 static rtx
expand_builtin_apply_args (void);
101 static rtx
expand_builtin_apply_args_1 (void);
102 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
103 static void expand_builtin_return (rtx
);
104 static enum type_class
type_to_class (tree
);
105 static rtx
expand_builtin_classify_type (tree
);
106 static void expand_errno_check (tree
, rtx
);
107 static rtx
expand_builtin_mathfn (tree
, rtx
, rtx
);
108 static rtx
expand_builtin_mathfn_2 (tree
, rtx
, rtx
);
109 static rtx
expand_builtin_mathfn_3 (tree
, rtx
, rtx
);
110 static rtx
expand_builtin_mathfn_ternary (tree
, rtx
, rtx
);
111 static rtx
expand_builtin_interclass_mathfn (tree
, rtx
);
112 static rtx
expand_builtin_sincos (tree
);
113 static rtx
expand_builtin_cexpi (tree
, rtx
);
114 static rtx
expand_builtin_int_roundingfn (tree
, rtx
);
115 static rtx
expand_builtin_int_roundingfn_2 (tree
, rtx
);
116 static rtx
expand_builtin_next_arg (void);
117 static rtx
expand_builtin_va_start (tree
);
118 static rtx
expand_builtin_va_end (tree
);
119 static rtx
expand_builtin_va_copy (tree
);
120 static rtx
expand_builtin_memcmp (tree
, rtx
, enum machine_mode
);
121 static rtx
expand_builtin_strcmp (tree
, rtx
);
122 static rtx
expand_builtin_strncmp (tree
, rtx
, enum machine_mode
);
123 static rtx
builtin_memcpy_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
124 static rtx
expand_builtin_memcpy (tree
, rtx
);
125 static rtx
expand_builtin_mempcpy (tree
, rtx
, enum machine_mode
);
126 static rtx
expand_builtin_mempcpy_args (tree
, tree
, tree
, rtx
,
127 enum machine_mode
, int);
128 static rtx
expand_builtin_strcpy (tree
, rtx
);
129 static rtx
expand_builtin_strcpy_args (tree
, tree
, rtx
);
130 static rtx
expand_builtin_stpcpy (tree
, rtx
, enum machine_mode
);
131 static rtx
expand_builtin_strncpy (tree
, rtx
);
132 static rtx
builtin_memset_gen_str (void *, HOST_WIDE_INT
, enum machine_mode
);
133 static rtx
expand_builtin_memset (tree
, rtx
, enum machine_mode
);
134 static rtx
expand_builtin_memset_args (tree
, tree
, tree
, rtx
, enum machine_mode
, tree
);
135 static rtx
expand_builtin_bzero (tree
);
136 static rtx
expand_builtin_strlen (tree
, rtx
, enum machine_mode
);
137 static rtx
expand_builtin_alloca (tree
, bool);
138 static rtx
expand_builtin_unop (enum machine_mode
, tree
, rtx
, rtx
, optab
);
139 static rtx
expand_builtin_frame_address (tree
, tree
);
140 static tree
stabilize_va_list_loc (location_t
, tree
, int);
141 static rtx
expand_builtin_expect (tree
, rtx
);
142 static tree
fold_builtin_constant_p (tree
);
143 static tree
fold_builtin_expect (location_t
, tree
, tree
);
144 static tree
fold_builtin_classify_type (tree
);
145 static tree
fold_builtin_strlen (location_t
, tree
, tree
);
146 static tree
fold_builtin_inf (location_t
, tree
, int);
147 static tree
fold_builtin_nan (tree
, tree
, int);
148 static tree
rewrite_call_expr (location_t
, tree
, int, tree
, int, ...);
149 static bool validate_arg (const_tree
, enum tree_code code
);
150 static bool integer_valued_real_p (tree
);
151 static tree
fold_trunc_transparent_mathfn (location_t
, tree
, tree
);
152 static bool readonly_data_expr (tree
);
153 static rtx
expand_builtin_fabs (tree
, rtx
, rtx
);
154 static rtx
expand_builtin_signbit (tree
, rtx
);
155 static tree
fold_builtin_sqrt (location_t
, tree
, tree
);
156 static tree
fold_builtin_cbrt (location_t
, tree
, tree
);
157 static tree
fold_builtin_pow (location_t
, tree
, tree
, tree
, tree
);
158 static tree
fold_builtin_powi (location_t
, tree
, tree
, tree
, tree
);
159 static tree
fold_builtin_cos (location_t
, tree
, tree
, tree
);
160 static tree
fold_builtin_cosh (location_t
, tree
, tree
, tree
);
161 static tree
fold_builtin_tan (tree
, tree
);
162 static tree
fold_builtin_trunc (location_t
, tree
, tree
);
163 static tree
fold_builtin_floor (location_t
, tree
, tree
);
164 static tree
fold_builtin_ceil (location_t
, tree
, tree
);
165 static tree
fold_builtin_round (location_t
, tree
, tree
);
166 static tree
fold_builtin_int_roundingfn (location_t
, tree
, tree
);
167 static tree
fold_builtin_bitop (tree
, tree
);
168 static tree
fold_builtin_memory_op (location_t
, tree
, tree
, tree
, tree
, bool, int);
169 static tree
fold_builtin_strchr (location_t
, tree
, tree
, tree
);
170 static tree
fold_builtin_memchr (location_t
, tree
, tree
, tree
, tree
);
171 static tree
fold_builtin_memcmp (location_t
, tree
, tree
, tree
);
172 static tree
fold_builtin_strcmp (location_t
, tree
, tree
);
173 static tree
fold_builtin_strncmp (location_t
, tree
, tree
, tree
);
174 static tree
fold_builtin_signbit (location_t
, tree
, tree
);
175 static tree
fold_builtin_copysign (location_t
, tree
, tree
, tree
, tree
);
176 static tree
fold_builtin_isascii (location_t
, tree
);
177 static tree
fold_builtin_toascii (location_t
, tree
);
178 static tree
fold_builtin_isdigit (location_t
, tree
);
179 static tree
fold_builtin_fabs (location_t
, tree
, tree
);
180 static tree
fold_builtin_abs (location_t
, tree
, tree
);
181 static tree
fold_builtin_unordered_cmp (location_t
, tree
, tree
, tree
, enum tree_code
,
183 static tree
fold_builtin_n (location_t
, tree
, tree
*, int, bool);
184 static tree
fold_builtin_0 (location_t
, tree
, bool);
185 static tree
fold_builtin_1 (location_t
, tree
, tree
, bool);
186 static tree
fold_builtin_2 (location_t
, tree
, tree
, tree
, bool);
187 static tree
fold_builtin_3 (location_t
, tree
, tree
, tree
, tree
, bool);
188 static tree
fold_builtin_4 (location_t
, tree
, tree
, tree
, tree
, tree
, bool);
189 static tree
fold_builtin_varargs (location_t
, tree
, tree
, bool);
191 static tree
fold_builtin_strpbrk (location_t
, tree
, tree
, tree
);
192 static tree
fold_builtin_strstr (location_t
, tree
, tree
, tree
);
193 static tree
fold_builtin_strrchr (location_t
, tree
, tree
, tree
);
194 static tree
fold_builtin_strcat (location_t
, tree
, tree
);
195 static tree
fold_builtin_strncat (location_t
, tree
, tree
, tree
);
196 static tree
fold_builtin_strspn (location_t
, tree
, tree
);
197 static tree
fold_builtin_strcspn (location_t
, tree
, tree
);
198 static tree
fold_builtin_sprintf (location_t
, tree
, tree
, tree
, int);
199 static tree
fold_builtin_snprintf (location_t
, tree
, tree
, tree
, tree
, int);
201 static rtx
expand_builtin_object_size (tree
);
202 static rtx
expand_builtin_memory_chk (tree
, rtx
, enum machine_mode
,
203 enum built_in_function
);
204 static void maybe_emit_chk_warning (tree
, enum built_in_function
);
205 static void maybe_emit_sprintf_chk_warning (tree
, enum built_in_function
);
206 static void maybe_emit_free_warning (tree
);
207 static tree
fold_builtin_object_size (tree
, tree
);
208 static tree
fold_builtin_strcat_chk (location_t
, tree
, tree
, tree
, tree
);
209 static tree
fold_builtin_strncat_chk (location_t
, tree
, tree
, tree
, tree
, tree
);
210 static tree
fold_builtin_sprintf_chk (location_t
, tree
, enum built_in_function
);
211 static tree
fold_builtin_printf (location_t
, tree
, tree
, tree
, bool, enum built_in_function
);
212 static tree
fold_builtin_fprintf (location_t
, tree
, tree
, tree
, tree
, bool,
213 enum built_in_function
);
214 static bool init_target_chars (void);
216 static unsigned HOST_WIDE_INT target_newline
;
217 static unsigned HOST_WIDE_INT target_percent
;
218 static unsigned HOST_WIDE_INT target_c
;
219 static unsigned HOST_WIDE_INT target_s
;
220 static char target_percent_c
[3];
221 static char target_percent_s
[3];
222 static char target_percent_s_newline
[4];
223 static tree
do_mpfr_arg1 (tree
, tree
, int (*)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
224 const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, bool);
225 static tree
do_mpfr_arg2 (tree
, tree
, tree
,
226 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
227 static tree
do_mpfr_arg3 (tree
, tree
, tree
, tree
,
228 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
229 static tree
do_mpfr_sincos (tree
, tree
, tree
);
230 static tree
do_mpfr_bessel_n (tree
, tree
, tree
,
231 int (*)(mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
232 const REAL_VALUE_TYPE
*, bool);
233 static tree
do_mpfr_remquo (tree
, tree
, tree
);
234 static tree
do_mpfr_lgamma_r (tree
, tree
, tree
);
235 static void expand_builtin_sync_synchronize (void);
237 /* Return true if NAME starts with __builtin_ or __sync_. */
240 is_builtin_name (const char *name
)
242 if (strncmp (name
, "__builtin_", 10) == 0)
244 if (strncmp (name
, "__sync_", 7) == 0)
246 if (strncmp (name
, "__atomic_", 9) == 0)
248 if (flag_enable_cilkplus
249 && (!strcmp (name
, "__cilkrts_detach")
250 || !strcmp (name
, "__cilkrts_pop_frame")))
256 /* Return true if DECL is a function symbol representing a built-in. */
259 is_builtin_fn (tree decl
)
261 return TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
);
264 /* By default we assume that c99 functions are present at the runtime,
265 but sincos is not. */
267 default_libc_has_function (enum function_class fn_class
)
269 if (fn_class
== function_c94
270 || fn_class
== function_c99_misc
271 || fn_class
== function_c99_math_complex
)
278 gnu_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED
)
284 no_c99_libc_has_function (enum function_class fn_class ATTRIBUTE_UNUSED
)
289 /* Return true if NODE should be considered for inline expansion regardless
290 of the optimization level. This means whenever a function is invoked with
291 its "internal" name, which normally contains the prefix "__builtin". */
294 called_as_built_in (tree node
)
296 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
297 we want the name used to call the function, not the name it
299 const char *name
= IDENTIFIER_POINTER (DECL_NAME (node
));
300 return is_builtin_name (name
);
303 /* Compute values M and N such that M divides (address of EXP - N) and such
304 that N < M. If these numbers can be determined, store M in alignp and N in
305 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
306 *alignp and any bit-offset to *bitposp.
308 Note that the address (and thus the alignment) computed here is based
309 on the address to which a symbol resolves, whereas DECL_ALIGN is based
310 on the address at which an object is actually located. These two
311 addresses are not always the same. For example, on ARM targets,
312 the address &foo of a Thumb function foo() has the lowest bit set,
313 whereas foo() itself starts on an even address.
315 If ADDR_P is true we are taking the address of the memory reference EXP
316 and thus cannot rely on the access taking place. */
319 get_object_alignment_2 (tree exp
, unsigned int *alignp
,
320 unsigned HOST_WIDE_INT
*bitposp
, bool addr_p
)
322 HOST_WIDE_INT bitsize
, bitpos
;
324 enum machine_mode mode
;
325 int unsignedp
, volatilep
;
326 unsigned int align
= BITS_PER_UNIT
;
327 bool known_alignment
= false;
329 /* Get the innermost object and the constant (bitpos) and possibly
330 variable (offset) offset of the access. */
331 exp
= get_inner_reference (exp
, &bitsize
, &bitpos
, &offset
,
332 &mode
, &unsignedp
, &volatilep
, true);
334 /* Extract alignment information from the innermost object and
335 possibly adjust bitpos and offset. */
336 if (TREE_CODE (exp
) == FUNCTION_DECL
)
338 /* Function addresses can encode extra information besides their
339 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
340 allows the low bit to be used as a virtual bit, we know
341 that the address itself must be at least 2-byte aligned. */
342 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_pfn
)
343 align
= 2 * BITS_PER_UNIT
;
345 else if (TREE_CODE (exp
) == LABEL_DECL
)
347 else if (TREE_CODE (exp
) == CONST_DECL
)
349 /* The alignment of a CONST_DECL is determined by its initializer. */
350 exp
= DECL_INITIAL (exp
);
351 align
= TYPE_ALIGN (TREE_TYPE (exp
));
352 #ifdef CONSTANT_ALIGNMENT
353 if (CONSTANT_CLASS_P (exp
))
354 align
= (unsigned) CONSTANT_ALIGNMENT (exp
, align
);
356 known_alignment
= true;
358 else if (DECL_P (exp
))
360 align
= DECL_ALIGN (exp
);
361 known_alignment
= true;
363 else if (TREE_CODE (exp
) == VIEW_CONVERT_EXPR
)
365 align
= TYPE_ALIGN (TREE_TYPE (exp
));
367 else if (TREE_CODE (exp
) == INDIRECT_REF
368 || TREE_CODE (exp
) == MEM_REF
369 || TREE_CODE (exp
) == TARGET_MEM_REF
)
371 tree addr
= TREE_OPERAND (exp
, 0);
373 unsigned HOST_WIDE_INT ptr_bitpos
;
375 if (TREE_CODE (addr
) == BIT_AND_EXPR
376 && TREE_CODE (TREE_OPERAND (addr
, 1)) == INTEGER_CST
)
378 align
= (TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1))
379 & -TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1)));
380 align
*= BITS_PER_UNIT
;
381 addr
= TREE_OPERAND (addr
, 0);
385 = get_pointer_alignment_1 (addr
, &ptr_align
, &ptr_bitpos
);
386 align
= MAX (ptr_align
, align
);
388 /* The alignment of the pointer operand in a TARGET_MEM_REF
389 has to take the variable offset parts into account. */
390 if (TREE_CODE (exp
) == TARGET_MEM_REF
)
394 unsigned HOST_WIDE_INT step
= 1;
396 step
= TREE_INT_CST_LOW (TMR_STEP (exp
));
397 align
= MIN (align
, (step
& -step
) * BITS_PER_UNIT
);
399 if (TMR_INDEX2 (exp
))
400 align
= BITS_PER_UNIT
;
401 known_alignment
= false;
404 /* When EXP is an actual memory reference then we can use
405 TYPE_ALIGN of a pointer indirection to derive alignment.
406 Do so only if get_pointer_alignment_1 did not reveal absolute
407 alignment knowledge and if using that alignment would
408 improve the situation. */
409 if (!addr_p
&& !known_alignment
410 && TYPE_ALIGN (TREE_TYPE (exp
)) > align
)
411 align
= TYPE_ALIGN (TREE_TYPE (exp
));
414 /* Else adjust bitpos accordingly. */
415 bitpos
+= ptr_bitpos
;
416 if (TREE_CODE (exp
) == MEM_REF
417 || TREE_CODE (exp
) == TARGET_MEM_REF
)
418 bitpos
+= mem_ref_offset (exp
).low
* BITS_PER_UNIT
;
421 else if (TREE_CODE (exp
) == STRING_CST
)
423 /* STRING_CST are the only constant objects we allow to be not
424 wrapped inside a CONST_DECL. */
425 align
= TYPE_ALIGN (TREE_TYPE (exp
));
426 #ifdef CONSTANT_ALIGNMENT
427 if (CONSTANT_CLASS_P (exp
))
428 align
= (unsigned) CONSTANT_ALIGNMENT (exp
, align
);
430 known_alignment
= true;
433 /* If there is a non-constant offset part extract the maximum
434 alignment that can prevail. */
437 int trailing_zeros
= tree_ctz (offset
);
438 if (trailing_zeros
< HOST_BITS_PER_INT
)
440 unsigned int inner
= (1U << trailing_zeros
) * BITS_PER_UNIT
;
442 align
= MIN (align
, inner
);
447 *bitposp
= bitpos
& (*alignp
- 1);
448 return known_alignment
;
451 /* For a memory reference expression EXP compute values M and N such that M
452 divides (&EXP - N) and such that N < M. If these numbers can be determined,
453 store M in alignp and N in *BITPOSP and return true. Otherwise return false
454 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
457 get_object_alignment_1 (tree exp
, unsigned int *alignp
,
458 unsigned HOST_WIDE_INT
*bitposp
)
460 return get_object_alignment_2 (exp
, alignp
, bitposp
, false);
463 /* Return the alignment in bits of EXP, an object. */
466 get_object_alignment (tree exp
)
468 unsigned HOST_WIDE_INT bitpos
= 0;
471 get_object_alignment_1 (exp
, &align
, &bitpos
);
473 /* align and bitpos now specify known low bits of the pointer.
474 ptr & (align - 1) == bitpos. */
477 align
= (bitpos
& -bitpos
);
481 /* For a pointer valued expression EXP compute values M and N such that M
482 divides (EXP - N) and such that N < M. If these numbers can be determined,
483 store M in alignp and N in *BITPOSP and return true. Return false if
484 the results are just a conservative approximation.
486 If EXP is not a pointer, false is returned too. */
489 get_pointer_alignment_1 (tree exp
, unsigned int *alignp
,
490 unsigned HOST_WIDE_INT
*bitposp
)
494 if (TREE_CODE (exp
) == ADDR_EXPR
)
495 return get_object_alignment_2 (TREE_OPERAND (exp
, 0),
496 alignp
, bitposp
, true);
497 else if (TREE_CODE (exp
) == SSA_NAME
498 && POINTER_TYPE_P (TREE_TYPE (exp
)))
500 unsigned int ptr_align
, ptr_misalign
;
501 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (exp
);
503 if (pi
&& get_ptr_info_alignment (pi
, &ptr_align
, &ptr_misalign
))
505 *bitposp
= ptr_misalign
* BITS_PER_UNIT
;
506 *alignp
= ptr_align
* BITS_PER_UNIT
;
507 /* We cannot really tell whether this result is an approximation. */
513 *alignp
= BITS_PER_UNIT
;
517 else if (TREE_CODE (exp
) == INTEGER_CST
)
519 *alignp
= BIGGEST_ALIGNMENT
;
520 *bitposp
= ((TREE_INT_CST_LOW (exp
) * BITS_PER_UNIT
)
521 & (BIGGEST_ALIGNMENT
- 1));
526 *alignp
= BITS_PER_UNIT
;
530 /* Return the alignment in bits of EXP, a pointer valued expression.
531 The alignment returned is, by default, the alignment of the thing that
532 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
534 Otherwise, look at the expression to see if we can do better, i.e., if the
535 expression is actually pointing at an object whose alignment is tighter. */
538 get_pointer_alignment (tree exp
)
540 unsigned HOST_WIDE_INT bitpos
= 0;
543 get_pointer_alignment_1 (exp
, &align
, &bitpos
);
545 /* align and bitpos now specify known low bits of the pointer.
546 ptr & (align - 1) == bitpos. */
549 align
= (bitpos
& -bitpos
);
554 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
555 way, because it could contain a zero byte in the middle.
556 TREE_STRING_LENGTH is the size of the character array, not the string.
558 ONLY_VALUE should be nonzero if the result is not going to be emitted
559 into the instruction stream and zero if it is going to be expanded.
560 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
561 is returned, otherwise NULL, since
562 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
563 evaluate the side-effects.
565 The value returned is of type `ssizetype'.
567 Unfortunately, string_constant can't access the values of const char
568 arrays with initializers, so neither can we do so here. */
571 c_strlen (tree src
, int only_value
)
574 HOST_WIDE_INT offset
;
580 if (TREE_CODE (src
) == COND_EXPR
581 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
585 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
);
586 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
);
587 if (tree_int_cst_equal (len1
, len2
))
591 if (TREE_CODE (src
) == COMPOUND_EXPR
592 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
593 return c_strlen (TREE_OPERAND (src
, 1), only_value
);
595 loc
= EXPR_LOC_OR_LOC (src
, input_location
);
597 src
= string_constant (src
, &offset_node
);
601 max
= TREE_STRING_LENGTH (src
) - 1;
602 ptr
= TREE_STRING_POINTER (src
);
604 if (offset_node
&& TREE_CODE (offset_node
) != INTEGER_CST
)
606 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
607 compute the offset to the following null if we don't know where to
608 start searching for it. */
611 for (i
= 0; i
< max
; i
++)
615 /* We don't know the starting offset, but we do know that the string
616 has no internal zero bytes. We can assume that the offset falls
617 within the bounds of the string; otherwise, the programmer deserves
618 what he gets. Subtract the offset from the length of the string,
619 and return that. This would perhaps not be valid if we were dealing
620 with named arrays in addition to literal string constants. */
622 return size_diffop_loc (loc
, size_int (max
), offset_node
);
625 /* We have a known offset into the string. Start searching there for
626 a null character if we can represent it as a single HOST_WIDE_INT. */
627 if (offset_node
== 0)
629 else if (! tree_fits_shwi_p (offset_node
))
632 offset
= tree_to_shwi (offset_node
);
634 /* If the offset is known to be out of bounds, warn, and call strlen at
636 if (offset
< 0 || offset
> max
)
638 /* Suppress multiple warnings for propagated constant strings. */
639 if (! TREE_NO_WARNING (src
))
641 warning_at (loc
, 0, "offset outside bounds of constant string");
642 TREE_NO_WARNING (src
) = 1;
647 /* Use strlen to search for the first zero byte. Since any strings
648 constructed with build_string will have nulls appended, we win even
649 if we get handed something like (char[4])"abcd".
651 Since OFFSET is our starting index into the string, no further
652 calculation is needed. */
653 return ssize_int (strlen (ptr
+ offset
));
656 /* Return a char pointer for a C string if it is a string constant
657 or sum of string constant and integer constant. */
664 src
= string_constant (src
, &offset_node
);
668 if (offset_node
== 0)
669 return TREE_STRING_POINTER (src
);
670 else if (!tree_fits_uhwi_p (offset_node
)
671 || compare_tree_int (offset_node
, TREE_STRING_LENGTH (src
) - 1) > 0)
674 return TREE_STRING_POINTER (src
) + tree_to_uhwi (offset_node
);
677 /* Return a CONST_INT or CONST_DOUBLE corresponding to target reading
678 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
681 c_readstr (const char *str
, enum machine_mode mode
)
687 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
692 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
695 if (WORDS_BIG_ENDIAN
)
696 j
= GET_MODE_SIZE (mode
) - i
- 1;
697 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
698 && GET_MODE_SIZE (mode
) >= UNITS_PER_WORD
)
699 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
701 gcc_assert (j
< HOST_BITS_PER_DOUBLE_INT
);
704 ch
= (unsigned char) str
[i
];
705 c
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
707 return immed_double_const (c
[0], c
[1], mode
);
710 /* Cast a target constant CST to target CHAR and if that value fits into
711 host char type, return zero and put that value into variable pointed to by
715 target_char_cast (tree cst
, char *p
)
717 unsigned HOST_WIDE_INT val
, hostval
;
719 if (TREE_CODE (cst
) != INTEGER_CST
720 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
723 val
= TREE_INT_CST_LOW (cst
);
724 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
725 val
&= (((unsigned HOST_WIDE_INT
) 1) << CHAR_TYPE_SIZE
) - 1;
728 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
729 hostval
&= (((unsigned HOST_WIDE_INT
) 1) << HOST_BITS_PER_CHAR
) - 1;
738 /* Similar to save_expr, but assumes that arbitrary code is not executed
739 in between the multiple evaluations. In particular, we assume that a
740 non-addressable local variable will not be modified. */
743 builtin_save_expr (tree exp
)
745 if (TREE_CODE (exp
) == SSA_NAME
746 || (TREE_ADDRESSABLE (exp
) == 0
747 && (TREE_CODE (exp
) == PARM_DECL
748 || (TREE_CODE (exp
) == VAR_DECL
&& !TREE_STATIC (exp
)))))
751 return save_expr (exp
);
754 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
755 times to get the address of either a higher stack frame, or a return
756 address located within it (depending on FNDECL_CODE). */
759 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
)
763 #ifdef INITIAL_FRAME_ADDRESS_RTX
764 rtx tem
= INITIAL_FRAME_ADDRESS_RTX
;
768 /* For a zero count with __builtin_return_address, we don't care what
769 frame address we return, because target-specific definitions will
770 override us. Therefore frame pointer elimination is OK, and using
771 the soft frame pointer is OK.
773 For a nonzero count, or a zero count with __builtin_frame_address,
774 we require a stable offset from the current frame pointer to the
775 previous one, so we must use the hard frame pointer, and
776 we must disable frame pointer elimination. */
777 if (count
== 0 && fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
778 tem
= frame_pointer_rtx
;
781 tem
= hard_frame_pointer_rtx
;
783 /* Tell reload not to eliminate the frame pointer. */
784 crtl
->accesses_prior_frames
= 1;
788 /* Some machines need special handling before we can access
789 arbitrary frames. For example, on the SPARC, we must first flush
790 all register windows to the stack. */
791 #ifdef SETUP_FRAME_ADDRESSES
793 SETUP_FRAME_ADDRESSES ();
796 /* On the SPARC, the return address is not in the frame, it is in a
797 register. There is no way to access it off of the current frame
798 pointer, but it can be accessed off the previous frame pointer by
799 reading the value from the register window save area. */
800 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
801 if (fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
805 /* Scan back COUNT frames to the specified frame. */
806 for (i
= 0; i
< count
; i
++)
808 /* Assume the dynamic chain pointer is in the word that the
809 frame address points to, unless otherwise specified. */
810 #ifdef DYNAMIC_CHAIN_ADDRESS
811 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
813 tem
= memory_address (Pmode
, tem
);
814 tem
= gen_frame_mem (Pmode
, tem
);
815 tem
= copy_to_reg (tem
);
818 /* For __builtin_frame_address, return what we've got. But, on
819 the SPARC for example, we may have to add a bias. */
820 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
821 #ifdef FRAME_ADDR_RTX
822 return FRAME_ADDR_RTX (tem
);
827 /* For __builtin_return_address, get the return address from that frame. */
828 #ifdef RETURN_ADDR_RTX
829 tem
= RETURN_ADDR_RTX (count
, tem
);
831 tem
= memory_address (Pmode
,
832 plus_constant (Pmode
, tem
, GET_MODE_SIZE (Pmode
)));
833 tem
= gen_frame_mem (Pmode
, tem
);
838 /* Alias set used for setjmp buffer. */
839 static alias_set_type setjmp_alias_set
= -1;
841 /* Construct the leading half of a __builtin_setjmp call. Control will
842 return to RECEIVER_LABEL. This is also called directly by the SJLJ
843 exception handling code. */
846 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
848 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
852 if (setjmp_alias_set
== -1)
853 setjmp_alias_set
= new_alias_set ();
855 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
857 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
859 /* We store the frame pointer and the address of receiver_label in
860 the buffer and use the rest of it for the stack save area, which
861 is machine-dependent. */
863 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
864 set_mem_alias_set (mem
, setjmp_alias_set
);
865 emit_move_insn (mem
, targetm
.builtin_setjmp_frame_value ());
867 mem
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
868 GET_MODE_SIZE (Pmode
))),
869 set_mem_alias_set (mem
, setjmp_alias_set
);
871 emit_move_insn (validize_mem (mem
),
872 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
874 stack_save
= gen_rtx_MEM (sa_mode
,
875 plus_constant (Pmode
, buf_addr
,
876 2 * GET_MODE_SIZE (Pmode
)));
877 set_mem_alias_set (stack_save
, setjmp_alias_set
);
878 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
880 /* If there is further processing to do, do it. */
881 #ifdef HAVE_builtin_setjmp_setup
882 if (HAVE_builtin_setjmp_setup
)
883 emit_insn (gen_builtin_setjmp_setup (buf_addr
));
886 /* We have a nonlocal label. */
887 cfun
->has_nonlocal_label
= 1;
890 /* Construct the trailing part of a __builtin_setjmp call. This is
891 also called directly by the SJLJ exception handling code.
892 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
895 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED
)
899 /* Mark the FP as used when we get here, so we have to make sure it's
900 marked as used by this function. */
901 emit_use (hard_frame_pointer_rtx
);
903 /* Mark the static chain as clobbered here so life information
904 doesn't get messed up for it. */
905 chain
= targetm
.calls
.static_chain (current_function_decl
, true);
906 if (chain
&& REG_P (chain
))
907 emit_clobber (chain
);
909 /* Now put in the code to restore the frame pointer, and argument
910 pointer, if needed. */
911 #ifdef HAVE_nonlocal_goto
912 if (! HAVE_nonlocal_goto
)
914 /* First adjust our frame pointer to its actual value. It was
915 previously set to the start of the virtual area corresponding to
916 the stacked variables when we branched here and now needs to be
917 adjusted to the actual hardware fp value.
919 Assignments to virtual registers are converted by
920 instantiate_virtual_regs into the corresponding assignment
921 to the underlying register (fp in this case) that makes
922 the original assignment true.
923 So the following insn will actually be decrementing fp by
924 STARTING_FRAME_OFFSET. */
925 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
927 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
928 if (fixed_regs
[ARG_POINTER_REGNUM
])
930 #ifdef ELIMINABLE_REGS
931 /* If the argument pointer can be eliminated in favor of the
932 frame pointer, we don't need to restore it. We assume here
933 that if such an elimination is present, it can always be used.
934 This is the case on all known machines; if we don't make this
935 assumption, we do unnecessary saving on many machines. */
937 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
939 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
940 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
941 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
944 if (i
== ARRAY_SIZE (elim_regs
))
947 /* Now restore our arg pointer from the address at which it
948 was saved in our stack frame. */
949 emit_move_insn (crtl
->args
.internal_arg_pointer
,
950 copy_to_reg (get_arg_pointer_save_area ()));
955 #ifdef HAVE_builtin_setjmp_receiver
956 if (receiver_label
!= NULL
&& HAVE_builtin_setjmp_receiver
)
957 emit_insn (gen_builtin_setjmp_receiver (receiver_label
));
960 #ifdef HAVE_nonlocal_goto_receiver
961 if (HAVE_nonlocal_goto_receiver
)
962 emit_insn (gen_nonlocal_goto_receiver ());
967 /* We must not allow the code we just generated to be reordered by
968 scheduling. Specifically, the update of the frame pointer must
969 happen immediately, not later. Similarly, we must block
970 (frame-related) register values to be used across this code. */
971 emit_insn (gen_blockage ());
974 /* __builtin_longjmp is passed a pointer to an array of five words (not
975 all will be used on all machines). It operates similarly to the C
976 library function of the same name, but is more efficient. Much of
977 the code below is copied from the handling of non-local gotos. */
980 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
982 rtx fp
, lab
, stack
, insn
, last
;
983 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
985 /* DRAP is needed for stack realign if longjmp is expanded to current
987 if (SUPPORTS_STACK_ALIGNMENT
)
988 crtl
->need_drap
= true;
990 if (setjmp_alias_set
== -1)
991 setjmp_alias_set
= new_alias_set ();
993 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
995 buf_addr
= force_reg (Pmode
, buf_addr
);
997 /* We require that the user must pass a second argument of 1, because
998 that is what builtin_setjmp will return. */
999 gcc_assert (value
== const1_rtx
);
1001 last
= get_last_insn ();
1002 #ifdef HAVE_builtin_longjmp
1003 if (HAVE_builtin_longjmp
)
1004 emit_insn (gen_builtin_longjmp (buf_addr
));
1008 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
1009 lab
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
1010 GET_MODE_SIZE (Pmode
)));
1012 stack
= gen_rtx_MEM (sa_mode
, plus_constant (Pmode
, buf_addr
,
1013 2 * GET_MODE_SIZE (Pmode
)));
1014 set_mem_alias_set (fp
, setjmp_alias_set
);
1015 set_mem_alias_set (lab
, setjmp_alias_set
);
1016 set_mem_alias_set (stack
, setjmp_alias_set
);
1018 /* Pick up FP, label, and SP from the block and jump. This code is
1019 from expand_goto in stmt.c; see there for detailed comments. */
1020 #ifdef HAVE_nonlocal_goto
1021 if (HAVE_nonlocal_goto
)
1022 /* We have to pass a value to the nonlocal_goto pattern that will
1023 get copied into the static_chain pointer, but it does not matter
1024 what that value is, because builtin_setjmp does not use it. */
1025 emit_insn (gen_nonlocal_goto (value
, lab
, stack
, fp
));
1029 lab
= copy_to_reg (lab
);
1031 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1032 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1034 emit_move_insn (hard_frame_pointer_rtx
, fp
);
1035 emit_stack_restore (SAVE_NONLOCAL
, stack
);
1037 emit_use (hard_frame_pointer_rtx
);
1038 emit_use (stack_pointer_rtx
);
1039 emit_indirect_jump (lab
);
1043 /* Search backwards and mark the jump insn as a non-local goto.
1044 Note that this precludes the use of __builtin_longjmp to a
1045 __builtin_setjmp target in the same function. However, we've
1046 already cautioned the user that these functions are for
1047 internal exception handling use only. */
1048 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1050 gcc_assert (insn
!= last
);
1054 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1057 else if (CALL_P (insn
))
1063 more_const_call_expr_args_p (const const_call_expr_arg_iterator
*iter
)
1065 return (iter
->i
< iter
->n
);
1068 /* This function validates the types of a function call argument list
1069 against a specified list of tree_codes. If the last specifier is a 0,
1070 that represents an ellipses, otherwise the last specifier must be a
1074 validate_arglist (const_tree callexpr
, ...)
1076 enum tree_code code
;
1079 const_call_expr_arg_iterator iter
;
1082 va_start (ap
, callexpr
);
1083 init_const_call_expr_arg_iterator (callexpr
, &iter
);
1087 code
= (enum tree_code
) va_arg (ap
, int);
1091 /* This signifies an ellipses, any further arguments are all ok. */
1095 /* This signifies an endlink, if no arguments remain, return
1096 true, otherwise return false. */
1097 res
= !more_const_call_expr_args_p (&iter
);
1100 /* If no parameters remain or the parameter's code does not
1101 match the specified code, return false. Otherwise continue
1102 checking any remaining arguments. */
1103 arg
= next_const_call_expr_arg (&iter
);
1104 if (!validate_arg (arg
, code
))
1111 /* We need gotos here since we can only have one VA_CLOSE in a
1119 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1120 and the address of the save area. */
1123 expand_builtin_nonlocal_goto (tree exp
)
1125 tree t_label
, t_save_area
;
1126 rtx r_label
, r_save_area
, r_fp
, r_sp
, insn
;
1128 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
1131 t_label
= CALL_EXPR_ARG (exp
, 0);
1132 t_save_area
= CALL_EXPR_ARG (exp
, 1);
1134 r_label
= expand_normal (t_label
);
1135 r_label
= convert_memory_address (Pmode
, r_label
);
1136 r_save_area
= expand_normal (t_save_area
);
1137 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
1138 /* Copy the address of the save location to a register just in case it was
1139 based on the frame pointer. */
1140 r_save_area
= copy_to_reg (r_save_area
);
1141 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
1142 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
1143 plus_constant (Pmode
, r_save_area
,
1144 GET_MODE_SIZE (Pmode
)));
1146 crtl
->has_nonlocal_goto
= 1;
1148 #ifdef HAVE_nonlocal_goto
1149 /* ??? We no longer need to pass the static chain value, afaik. */
1150 if (HAVE_nonlocal_goto
)
1151 emit_insn (gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
1155 r_label
= copy_to_reg (r_label
);
1157 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1158 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1160 /* Restore frame pointer for containing function. */
1161 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
1162 emit_stack_restore (SAVE_NONLOCAL
, r_sp
);
1164 /* USE of hard_frame_pointer_rtx added for consistency;
1165 not clear if really needed. */
1166 emit_use (hard_frame_pointer_rtx
);
1167 emit_use (stack_pointer_rtx
);
1169 /* If the architecture is using a GP register, we must
1170 conservatively assume that the target function makes use of it.
1171 The prologue of functions with nonlocal gotos must therefore
1172 initialize the GP register to the appropriate value, and we
1173 must then make sure that this value is live at the point
1174 of the jump. (Note that this doesn't necessarily apply
1175 to targets with a nonlocal_goto pattern; they are free
1176 to implement it in their own way. Note also that this is
1177 a no-op if the GP register is a global invariant.) */
1178 if ((unsigned) PIC_OFFSET_TABLE_REGNUM
!= INVALID_REGNUM
1179 && fixed_regs
[PIC_OFFSET_TABLE_REGNUM
])
1180 emit_use (pic_offset_table_rtx
);
1182 emit_indirect_jump (r_label
);
1185 /* Search backwards to the jump insn and mark it as a
1187 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1191 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1194 else if (CALL_P (insn
))
1201 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1202 (not all will be used on all machines) that was passed to __builtin_setjmp.
1203 It updates the stack pointer in that block to correspond to the current
1207 expand_builtin_update_setjmp_buf (rtx buf_addr
)
1209 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
1211 = gen_rtx_MEM (sa_mode
,
1214 plus_constant (Pmode
, buf_addr
,
1215 2 * GET_MODE_SIZE (Pmode
))));
1217 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
1220 /* Expand a call to __builtin_prefetch. For a target that does not support
1221 data prefetch, evaluate the memory address argument in case it has side
1225 expand_builtin_prefetch (tree exp
)
1227 tree arg0
, arg1
, arg2
;
1231 if (!validate_arglist (exp
, POINTER_TYPE
, 0))
1234 arg0
= CALL_EXPR_ARG (exp
, 0);
1236 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1237 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1239 nargs
= call_expr_nargs (exp
);
1241 arg1
= CALL_EXPR_ARG (exp
, 1);
1243 arg1
= integer_zero_node
;
1245 arg2
= CALL_EXPR_ARG (exp
, 2);
1247 arg2
= integer_three_node
;
1249 /* Argument 0 is an address. */
1250 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
1252 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1253 if (TREE_CODE (arg1
) != INTEGER_CST
)
1255 error ("second argument to %<__builtin_prefetch%> must be a constant");
1256 arg1
= integer_zero_node
;
1258 op1
= expand_normal (arg1
);
1259 /* Argument 1 must be either zero or one. */
1260 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
1262 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1267 /* Argument 2 (locality) must be a compile-time constant int. */
1268 if (TREE_CODE (arg2
) != INTEGER_CST
)
1270 error ("third argument to %<__builtin_prefetch%> must be a constant");
1271 arg2
= integer_zero_node
;
1273 op2
= expand_normal (arg2
);
1274 /* Argument 2 must be 0, 1, 2, or 3. */
1275 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
1277 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1281 #ifdef HAVE_prefetch
1284 struct expand_operand ops
[3];
1286 create_address_operand (&ops
[0], op0
);
1287 create_integer_operand (&ops
[1], INTVAL (op1
));
1288 create_integer_operand (&ops
[2], INTVAL (op2
));
1289 if (maybe_expand_insn (CODE_FOR_prefetch
, 3, ops
))
1294 /* Don't do anything with direct references to volatile memory, but
1295 generate code to handle other side effects. */
1296 if (!MEM_P (op0
) && side_effects_p (op0
))
1300 /* Get a MEM rtx for expression EXP which is the address of an operand
1301 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1302 the maximum length of the block of memory that might be accessed or
1306 get_memory_rtx (tree exp
, tree len
)
1308 tree orig_exp
= exp
;
1311 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1312 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1313 if (TREE_CODE (exp
) == SAVE_EXPR
&& !SAVE_EXPR_RESOLVED_P (exp
))
1314 exp
= TREE_OPERAND (exp
, 0);
1316 addr
= expand_expr (orig_exp
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
1317 mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
1319 /* Get an expression we can use to find the attributes to assign to MEM.
1320 First remove any nops. */
1321 while (CONVERT_EXPR_P (exp
)
1322 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
1323 exp
= TREE_OPERAND (exp
, 0);
1325 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1326 (as builtin stringops may alias with anything). */
1327 exp
= fold_build2 (MEM_REF
,
1328 build_array_type (char_type_node
,
1329 build_range_type (sizetype
,
1330 size_one_node
, len
)),
1331 exp
, build_int_cst (ptr_type_node
, 0));
1333 /* If the MEM_REF has no acceptable address, try to get the base object
1334 from the original address we got, and build an all-aliasing
1335 unknown-sized access to that one. */
1336 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp
, 0)))
1337 set_mem_attributes (mem
, exp
, 0);
1338 else if (TREE_CODE (TREE_OPERAND (exp
, 0)) == ADDR_EXPR
1339 && (exp
= get_base_address (TREE_OPERAND (TREE_OPERAND (exp
, 0),
1342 exp
= build_fold_addr_expr (exp
);
1343 exp
= fold_build2 (MEM_REF
,
1344 build_array_type (char_type_node
,
1345 build_range_type (sizetype
,
1348 exp
, build_int_cst (ptr_type_node
, 0));
1349 set_mem_attributes (mem
, exp
, 0);
1351 set_mem_alias_set (mem
, 0);
1355 /* Built-in functions to perform an untyped call and return. */
1357 #define apply_args_mode \
1358 (this_target_builtins->x_apply_args_mode)
1359 #define apply_result_mode \
1360 (this_target_builtins->x_apply_result_mode)
1362 /* Return the size required for the block returned by __builtin_apply_args,
1363 and initialize apply_args_mode. */
1366 apply_args_size (void)
1368 static int size
= -1;
1371 enum machine_mode mode
;
1373 /* The values computed by this function never change. */
1376 /* The first value is the incoming arg-pointer. */
1377 size
= GET_MODE_SIZE (Pmode
);
1379 /* The second value is the structure value address unless this is
1380 passed as an "invisible" first argument. */
1381 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1382 size
+= GET_MODE_SIZE (Pmode
);
1384 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1385 if (FUNCTION_ARG_REGNO_P (regno
))
1387 mode
= targetm
.calls
.get_raw_arg_mode (regno
);
1389 gcc_assert (mode
!= VOIDmode
);
1391 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1392 if (size
% align
!= 0)
1393 size
= CEIL (size
, align
) * align
;
1394 size
+= GET_MODE_SIZE (mode
);
1395 apply_args_mode
[regno
] = mode
;
1399 apply_args_mode
[regno
] = VOIDmode
;
1405 /* Return the size required for the block returned by __builtin_apply,
1406 and initialize apply_result_mode. */
1409 apply_result_size (void)
1411 static int size
= -1;
1413 enum machine_mode mode
;
1415 /* The values computed by this function never change. */
1420 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1421 if (targetm
.calls
.function_value_regno_p (regno
))
1423 mode
= targetm
.calls
.get_raw_result_mode (regno
);
1425 gcc_assert (mode
!= VOIDmode
);
1427 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1428 if (size
% align
!= 0)
1429 size
= CEIL (size
, align
) * align
;
1430 size
+= GET_MODE_SIZE (mode
);
1431 apply_result_mode
[regno
] = mode
;
1434 apply_result_mode
[regno
] = VOIDmode
;
1436 /* Allow targets that use untyped_call and untyped_return to override
1437 the size so that machine-specific information can be stored here. */
1438 #ifdef APPLY_RESULT_SIZE
1439 size
= APPLY_RESULT_SIZE
;
1445 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1446 /* Create a vector describing the result block RESULT. If SAVEP is true,
1447 the result block is used to save the values; otherwise it is used to
1448 restore the values. */
1451 result_vector (int savep
, rtx result
)
1453 int regno
, size
, align
, nelts
;
1454 enum machine_mode mode
;
1456 rtx
*savevec
= XALLOCAVEC (rtx
, FIRST_PSEUDO_REGISTER
);
1459 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1460 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1462 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1463 if (size
% align
!= 0)
1464 size
= CEIL (size
, align
) * align
;
1465 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
1466 mem
= adjust_address (result
, mode
, size
);
1467 savevec
[nelts
++] = (savep
1468 ? gen_rtx_SET (VOIDmode
, mem
, reg
)
1469 : gen_rtx_SET (VOIDmode
, reg
, mem
));
1470 size
+= GET_MODE_SIZE (mode
);
1472 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
1474 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1476 /* Save the state required to perform an untyped call with the same
1477 arguments as were passed to the current function. */
1480 expand_builtin_apply_args_1 (void)
1483 int size
, align
, regno
;
1484 enum machine_mode mode
;
1485 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
1487 /* Create a block where the arg-pointer, structure value address,
1488 and argument registers can be saved. */
1489 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
1491 /* Walk past the arg-pointer and structure value address. */
1492 size
= GET_MODE_SIZE (Pmode
);
1493 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1494 size
+= GET_MODE_SIZE (Pmode
);
1496 /* Save each register used in calling a function to the block. */
1497 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1498 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1500 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1501 if (size
% align
!= 0)
1502 size
= CEIL (size
, align
) * align
;
1504 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1506 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
1507 size
+= GET_MODE_SIZE (mode
);
1510 /* Save the arg pointer to the block. */
1511 tem
= copy_to_reg (crtl
->args
.internal_arg_pointer
);
1512 #ifdef STACK_GROWS_DOWNWARD
1513 /* We need the pointer as the caller actually passed them to us, not
1514 as we might have pretended they were passed. Make sure it's a valid
1515 operand, as emit_move_insn isn't expected to handle a PLUS. */
1517 = force_operand (plus_constant (Pmode
, tem
, crtl
->args
.pretend_args_size
),
1520 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
1522 size
= GET_MODE_SIZE (Pmode
);
1524 /* Save the structure value address unless this is passed as an
1525 "invisible" first argument. */
1526 if (struct_incoming_value
)
1528 emit_move_insn (adjust_address (registers
, Pmode
, size
),
1529 copy_to_reg (struct_incoming_value
));
1530 size
+= GET_MODE_SIZE (Pmode
);
1533 /* Return the address of the block. */
1534 return copy_addr_to_reg (XEXP (registers
, 0));
1537 /* __builtin_apply_args returns block of memory allocated on
1538 the stack into which is stored the arg pointer, structure
1539 value address, static chain, and all the registers that might
1540 possibly be used in performing a function call. The code is
1541 moved to the start of the function so the incoming values are
1545 expand_builtin_apply_args (void)
1547 /* Don't do __builtin_apply_args more than once in a function.
1548 Save the result of the first call and reuse it. */
1549 if (apply_args_value
!= 0)
1550 return apply_args_value
;
1552 /* When this function is called, it means that registers must be
1553 saved on entry to this function. So we migrate the
1554 call to the first insn of this function. */
1559 temp
= expand_builtin_apply_args_1 ();
1563 apply_args_value
= temp
;
1565 /* Put the insns after the NOTE that starts the function.
1566 If this is inside a start_sequence, make the outer-level insn
1567 chain current, so the code is placed at the start of the
1568 function. If internal_arg_pointer is a non-virtual pseudo,
1569 it needs to be placed after the function that initializes
1571 push_topmost_sequence ();
1572 if (REG_P (crtl
->args
.internal_arg_pointer
)
1573 && REGNO (crtl
->args
.internal_arg_pointer
) > LAST_VIRTUAL_REGISTER
)
1574 emit_insn_before (seq
, parm_birth_insn
);
1576 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
1577 pop_topmost_sequence ();
1582 /* Perform an untyped call and save the state required to perform an
1583 untyped return of whatever value was returned by the given function. */
1586 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
1588 int size
, align
, regno
;
1589 enum machine_mode mode
;
1590 rtx incoming_args
, result
, reg
, dest
, src
, call_insn
;
1591 rtx old_stack_level
= 0;
1592 rtx call_fusage
= 0;
1593 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
1595 arguments
= convert_memory_address (Pmode
, arguments
);
1597 /* Create a block where the return registers can be saved. */
1598 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
1600 /* Fetch the arg pointer from the ARGUMENTS block. */
1601 incoming_args
= gen_reg_rtx (Pmode
);
1602 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
1603 #ifndef STACK_GROWS_DOWNWARD
1604 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
1605 incoming_args
, 0, OPTAB_LIB_WIDEN
);
1608 /* Push a new argument block and copy the arguments. Do not allow
1609 the (potential) memcpy call below to interfere with our stack
1611 do_pending_stack_adjust ();
1614 /* Save the stack with nonlocal if available. */
1615 #ifdef HAVE_save_stack_nonlocal
1616 if (HAVE_save_stack_nonlocal
)
1617 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
);
1620 emit_stack_save (SAVE_BLOCK
, &old_stack_level
);
1622 /* Allocate a block of memory onto the stack and copy the memory
1623 arguments to the outgoing arguments address. We can pass TRUE
1624 as the 4th argument because we just saved the stack pointer
1625 and will restore it right after the call. */
1626 allocate_dynamic_stack_space (argsize
, 0, BIGGEST_ALIGNMENT
, true);
1628 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1629 may have already set current_function_calls_alloca to true.
1630 current_function_calls_alloca won't be set if argsize is zero,
1631 so we have to guarantee need_drap is true here. */
1632 if (SUPPORTS_STACK_ALIGNMENT
)
1633 crtl
->need_drap
= true;
1635 dest
= virtual_outgoing_args_rtx
;
1636 #ifndef STACK_GROWS_DOWNWARD
1637 if (CONST_INT_P (argsize
))
1638 dest
= plus_constant (Pmode
, dest
, -INTVAL (argsize
));
1640 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
1642 dest
= gen_rtx_MEM (BLKmode
, dest
);
1643 set_mem_align (dest
, PARM_BOUNDARY
);
1644 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
1645 set_mem_align (src
, PARM_BOUNDARY
);
1646 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
1648 /* Refer to the argument block. */
1650 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
1651 set_mem_align (arguments
, PARM_BOUNDARY
);
1653 /* Walk past the arg-pointer and structure value address. */
1654 size
= GET_MODE_SIZE (Pmode
);
1656 size
+= GET_MODE_SIZE (Pmode
);
1658 /* Restore each of the registers previously saved. Make USE insns
1659 for each of these registers for use in making the call. */
1660 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1661 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1663 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1664 if (size
% align
!= 0)
1665 size
= CEIL (size
, align
) * align
;
1666 reg
= gen_rtx_REG (mode
, regno
);
1667 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
1668 use_reg (&call_fusage
, reg
);
1669 size
+= GET_MODE_SIZE (mode
);
1672 /* Restore the structure value address unless this is passed as an
1673 "invisible" first argument. */
1674 size
= GET_MODE_SIZE (Pmode
);
1677 rtx value
= gen_reg_rtx (Pmode
);
1678 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
1679 emit_move_insn (struct_value
, value
);
1680 if (REG_P (struct_value
))
1681 use_reg (&call_fusage
, struct_value
);
1682 size
+= GET_MODE_SIZE (Pmode
);
1685 /* All arguments and registers used for the call are set up by now! */
1686 function
= prepare_call_address (NULL
, function
, NULL
, &call_fusage
, 0, 0);
1688 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1689 and we don't want to load it into a register as an optimization,
1690 because prepare_call_address already did it if it should be done. */
1691 if (GET_CODE (function
) != SYMBOL_REF
)
1692 function
= memory_address (FUNCTION_MODE
, function
);
1694 /* Generate the actual call instruction and save the return value. */
1695 #ifdef HAVE_untyped_call
1696 if (HAVE_untyped_call
)
1697 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE
, function
),
1698 result
, result_vector (1, result
)));
1701 #ifdef HAVE_call_value
1702 if (HAVE_call_value
)
1706 /* Locate the unique return register. It is not possible to
1707 express a call that sets more than one return register using
1708 call_value; use untyped_call for that. In fact, untyped_call
1709 only needs to save the return registers in the given block. */
1710 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1711 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1713 gcc_assert (!valreg
); /* HAVE_untyped_call required. */
1715 valreg
= gen_rtx_REG (mode
, regno
);
1718 emit_call_insn (GEN_CALL_VALUE (valreg
,
1719 gen_rtx_MEM (FUNCTION_MODE
, function
),
1720 const0_rtx
, NULL_RTX
, const0_rtx
));
1722 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
1728 /* Find the CALL insn we just emitted, and attach the register usage
1730 call_insn
= last_call_insn ();
1731 add_function_usage_to (call_insn
, call_fusage
);
1733 /* Restore the stack. */
1734 #ifdef HAVE_save_stack_nonlocal
1735 if (HAVE_save_stack_nonlocal
)
1736 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
);
1739 emit_stack_restore (SAVE_BLOCK
, old_stack_level
);
1740 fixup_args_size_notes (call_insn
, get_last_insn (), 0);
1744 /* Return the address of the result block. */
1745 result
= copy_addr_to_reg (XEXP (result
, 0));
1746 return convert_memory_address (ptr_mode
, result
);
1749 /* Perform an untyped return. */
1752 expand_builtin_return (rtx result
)
1754 int size
, align
, regno
;
1755 enum machine_mode mode
;
1757 rtx call_fusage
= 0;
1759 result
= convert_memory_address (Pmode
, result
);
1761 apply_result_size ();
1762 result
= gen_rtx_MEM (BLKmode
, result
);
1764 #ifdef HAVE_untyped_return
1765 if (HAVE_untyped_return
)
1767 emit_jump_insn (gen_untyped_return (result
, result_vector (0, result
)));
1773 /* Restore the return value and note that each value is used. */
1775 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1776 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1778 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1779 if (size
% align
!= 0)
1780 size
= CEIL (size
, align
) * align
;
1781 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1782 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
1784 push_to_sequence (call_fusage
);
1786 call_fusage
= get_insns ();
1788 size
+= GET_MODE_SIZE (mode
);
1791 /* Put the USE insns before the return. */
1792 emit_insn (call_fusage
);
1794 /* Return whatever values was restored by jumping directly to the end
1796 expand_naked_return ();
1799 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1801 static enum type_class
1802 type_to_class (tree type
)
1804 switch (TREE_CODE (type
))
1806 case VOID_TYPE
: return void_type_class
;
1807 case INTEGER_TYPE
: return integer_type_class
;
1808 case ENUMERAL_TYPE
: return enumeral_type_class
;
1809 case BOOLEAN_TYPE
: return boolean_type_class
;
1810 case POINTER_TYPE
: return pointer_type_class
;
1811 case REFERENCE_TYPE
: return reference_type_class
;
1812 case OFFSET_TYPE
: return offset_type_class
;
1813 case REAL_TYPE
: return real_type_class
;
1814 case COMPLEX_TYPE
: return complex_type_class
;
1815 case FUNCTION_TYPE
: return function_type_class
;
1816 case METHOD_TYPE
: return method_type_class
;
1817 case RECORD_TYPE
: return record_type_class
;
1819 case QUAL_UNION_TYPE
: return union_type_class
;
1820 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
1821 ? string_type_class
: array_type_class
);
1822 case LANG_TYPE
: return lang_type_class
;
1823 default: return no_type_class
;
1827 /* Expand a call EXP to __builtin_classify_type. */
1830 expand_builtin_classify_type (tree exp
)
1832 if (call_expr_nargs (exp
))
1833 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))));
1834 return GEN_INT (no_type_class
);
1837 /* This helper macro, meant to be used in mathfn_built_in below,
1838 determines which among a set of three builtin math functions is
1839 appropriate for a given type mode. The `F' and `L' cases are
1840 automatically generated from the `double' case. */
1841 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1842 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1843 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1844 fcodel = BUILT_IN_MATHFN##L ; break;
1845 /* Similar to above, but appends _R after any F/L suffix. */
1846 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1847 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1848 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1849 fcodel = BUILT_IN_MATHFN##L_R ; break;
1851 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1852 if available. If IMPLICIT is true use the implicit builtin declaration,
1853 otherwise use the explicit declaration. If we can't do the conversion,
1857 mathfn_built_in_1 (tree type
, enum built_in_function fn
, bool implicit_p
)
1859 enum built_in_function fcode
, fcodef
, fcodel
, fcode2
;
1863 CASE_MATHFN (BUILT_IN_ACOS
)
1864 CASE_MATHFN (BUILT_IN_ACOSH
)
1865 CASE_MATHFN (BUILT_IN_ASIN
)
1866 CASE_MATHFN (BUILT_IN_ASINH
)
1867 CASE_MATHFN (BUILT_IN_ATAN
)
1868 CASE_MATHFN (BUILT_IN_ATAN2
)
1869 CASE_MATHFN (BUILT_IN_ATANH
)
1870 CASE_MATHFN (BUILT_IN_CBRT
)
1871 CASE_MATHFN (BUILT_IN_CEIL
)
1872 CASE_MATHFN (BUILT_IN_CEXPI
)
1873 CASE_MATHFN (BUILT_IN_COPYSIGN
)
1874 CASE_MATHFN (BUILT_IN_COS
)
1875 CASE_MATHFN (BUILT_IN_COSH
)
1876 CASE_MATHFN (BUILT_IN_DREM
)
1877 CASE_MATHFN (BUILT_IN_ERF
)
1878 CASE_MATHFN (BUILT_IN_ERFC
)
1879 CASE_MATHFN (BUILT_IN_EXP
)
1880 CASE_MATHFN (BUILT_IN_EXP10
)
1881 CASE_MATHFN (BUILT_IN_EXP2
)
1882 CASE_MATHFN (BUILT_IN_EXPM1
)
1883 CASE_MATHFN (BUILT_IN_FABS
)
1884 CASE_MATHFN (BUILT_IN_FDIM
)
1885 CASE_MATHFN (BUILT_IN_FLOOR
)
1886 CASE_MATHFN (BUILT_IN_FMA
)
1887 CASE_MATHFN (BUILT_IN_FMAX
)
1888 CASE_MATHFN (BUILT_IN_FMIN
)
1889 CASE_MATHFN (BUILT_IN_FMOD
)
1890 CASE_MATHFN (BUILT_IN_FREXP
)
1891 CASE_MATHFN (BUILT_IN_GAMMA
)
1892 CASE_MATHFN_REENT (BUILT_IN_GAMMA
) /* GAMMA_R */
1893 CASE_MATHFN (BUILT_IN_HUGE_VAL
)
1894 CASE_MATHFN (BUILT_IN_HYPOT
)
1895 CASE_MATHFN (BUILT_IN_ILOGB
)
1896 CASE_MATHFN (BUILT_IN_ICEIL
)
1897 CASE_MATHFN (BUILT_IN_IFLOOR
)
1898 CASE_MATHFN (BUILT_IN_INF
)
1899 CASE_MATHFN (BUILT_IN_IRINT
)
1900 CASE_MATHFN (BUILT_IN_IROUND
)
1901 CASE_MATHFN (BUILT_IN_ISINF
)
1902 CASE_MATHFN (BUILT_IN_J0
)
1903 CASE_MATHFN (BUILT_IN_J1
)
1904 CASE_MATHFN (BUILT_IN_JN
)
1905 CASE_MATHFN (BUILT_IN_LCEIL
)
1906 CASE_MATHFN (BUILT_IN_LDEXP
)
1907 CASE_MATHFN (BUILT_IN_LFLOOR
)
1908 CASE_MATHFN (BUILT_IN_LGAMMA
)
1909 CASE_MATHFN_REENT (BUILT_IN_LGAMMA
) /* LGAMMA_R */
1910 CASE_MATHFN (BUILT_IN_LLCEIL
)
1911 CASE_MATHFN (BUILT_IN_LLFLOOR
)
1912 CASE_MATHFN (BUILT_IN_LLRINT
)
1913 CASE_MATHFN (BUILT_IN_LLROUND
)
1914 CASE_MATHFN (BUILT_IN_LOG
)
1915 CASE_MATHFN (BUILT_IN_LOG10
)
1916 CASE_MATHFN (BUILT_IN_LOG1P
)
1917 CASE_MATHFN (BUILT_IN_LOG2
)
1918 CASE_MATHFN (BUILT_IN_LOGB
)
1919 CASE_MATHFN (BUILT_IN_LRINT
)
1920 CASE_MATHFN (BUILT_IN_LROUND
)
1921 CASE_MATHFN (BUILT_IN_MODF
)
1922 CASE_MATHFN (BUILT_IN_NAN
)
1923 CASE_MATHFN (BUILT_IN_NANS
)
1924 CASE_MATHFN (BUILT_IN_NEARBYINT
)
1925 CASE_MATHFN (BUILT_IN_NEXTAFTER
)
1926 CASE_MATHFN (BUILT_IN_NEXTTOWARD
)
1927 CASE_MATHFN (BUILT_IN_POW
)
1928 CASE_MATHFN (BUILT_IN_POWI
)
1929 CASE_MATHFN (BUILT_IN_POW10
)
1930 CASE_MATHFN (BUILT_IN_REMAINDER
)
1931 CASE_MATHFN (BUILT_IN_REMQUO
)
1932 CASE_MATHFN (BUILT_IN_RINT
)
1933 CASE_MATHFN (BUILT_IN_ROUND
)
1934 CASE_MATHFN (BUILT_IN_SCALB
)
1935 CASE_MATHFN (BUILT_IN_SCALBLN
)
1936 CASE_MATHFN (BUILT_IN_SCALBN
)
1937 CASE_MATHFN (BUILT_IN_SIGNBIT
)
1938 CASE_MATHFN (BUILT_IN_SIGNIFICAND
)
1939 CASE_MATHFN (BUILT_IN_SIN
)
1940 CASE_MATHFN (BUILT_IN_SINCOS
)
1941 CASE_MATHFN (BUILT_IN_SINH
)
1942 CASE_MATHFN (BUILT_IN_SQRT
)
1943 CASE_MATHFN (BUILT_IN_TAN
)
1944 CASE_MATHFN (BUILT_IN_TANH
)
1945 CASE_MATHFN (BUILT_IN_TGAMMA
)
1946 CASE_MATHFN (BUILT_IN_TRUNC
)
1947 CASE_MATHFN (BUILT_IN_Y0
)
1948 CASE_MATHFN (BUILT_IN_Y1
)
1949 CASE_MATHFN (BUILT_IN_YN
)
1955 if (TYPE_MAIN_VARIANT (type
) == double_type_node
)
1957 else if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1959 else if (TYPE_MAIN_VARIANT (type
) == long_double_type_node
)
1964 if (implicit_p
&& !builtin_decl_implicit_p (fcode2
))
1967 return builtin_decl_explicit (fcode2
);
1970 /* Like mathfn_built_in_1(), but always use the implicit array. */
1973 mathfn_built_in (tree type
, enum built_in_function fn
)
1975 return mathfn_built_in_1 (type
, fn
, /*implicit=*/ 1);
1978 /* If errno must be maintained, expand the RTL to check if the result,
1979 TARGET, of a built-in function call, EXP, is NaN, and if so set
1983 expand_errno_check (tree exp
, rtx target
)
1985 rtx lab
= gen_label_rtx ();
1987 /* Test the result; if it is NaN, set errno=EDOM because
1988 the argument was not in the domain. */
1989 do_compare_rtx_and_jump (target
, target
, EQ
, 0, GET_MODE (target
),
1990 NULL_RTX
, NULL_RTX
, lab
,
1991 /* The jump is very likely. */
1992 REG_BR_PROB_BASE
- (REG_BR_PROB_BASE
/ 2000 - 1));
1995 /* If this built-in doesn't throw an exception, set errno directly. */
1996 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp
), 0)))
1998 #ifdef GEN_ERRNO_RTX
1999 rtx errno_rtx
= GEN_ERRNO_RTX
;
2002 = gen_rtx_MEM (word_mode
, gen_rtx_SYMBOL_REF (Pmode
, "errno"));
2004 emit_move_insn (errno_rtx
,
2005 gen_int_mode (TARGET_EDOM
, GET_MODE (errno_rtx
)));
2011 /* Make sure the library call isn't expanded as a tail call. */
2012 CALL_EXPR_TAILCALL (exp
) = 0;
2014 /* We can't set errno=EDOM directly; let the library call do it.
2015 Pop the arguments right away in case the call gets deleted. */
2017 expand_call (exp
, target
, 0);
2022 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2023 Return NULL_RTX if a normal call should be emitted rather than expanding
2024 the function in-line. EXP is the expression that is a call to the builtin
2025 function; if convenient, the result should be placed in TARGET.
2026 SUBTARGET may be used as the target for computing one of EXP's operands. */
2029 expand_builtin_mathfn (tree exp
, rtx target
, rtx subtarget
)
2031 optab builtin_optab
;
2033 tree fndecl
= get_callee_fndecl (exp
);
2034 enum machine_mode mode
;
2035 bool errno_set
= false;
2036 bool try_widening
= false;
2039 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2042 arg
= CALL_EXPR_ARG (exp
, 0);
2044 switch (DECL_FUNCTION_CODE (fndecl
))
2046 CASE_FLT_FN (BUILT_IN_SQRT
):
2047 errno_set
= ! tree_expr_nonnegative_p (arg
);
2048 try_widening
= true;
2049 builtin_optab
= sqrt_optab
;
2051 CASE_FLT_FN (BUILT_IN_EXP
):
2052 errno_set
= true; builtin_optab
= exp_optab
; break;
2053 CASE_FLT_FN (BUILT_IN_EXP10
):
2054 CASE_FLT_FN (BUILT_IN_POW10
):
2055 errno_set
= true; builtin_optab
= exp10_optab
; break;
2056 CASE_FLT_FN (BUILT_IN_EXP2
):
2057 errno_set
= true; builtin_optab
= exp2_optab
; break;
2058 CASE_FLT_FN (BUILT_IN_EXPM1
):
2059 errno_set
= true; builtin_optab
= expm1_optab
; break;
2060 CASE_FLT_FN (BUILT_IN_LOGB
):
2061 errno_set
= true; builtin_optab
= logb_optab
; break;
2062 CASE_FLT_FN (BUILT_IN_LOG
):
2063 errno_set
= true; builtin_optab
= log_optab
; break;
2064 CASE_FLT_FN (BUILT_IN_LOG10
):
2065 errno_set
= true; builtin_optab
= log10_optab
; break;
2066 CASE_FLT_FN (BUILT_IN_LOG2
):
2067 errno_set
= true; builtin_optab
= log2_optab
; break;
2068 CASE_FLT_FN (BUILT_IN_LOG1P
):
2069 errno_set
= true; builtin_optab
= log1p_optab
; break;
2070 CASE_FLT_FN (BUILT_IN_ASIN
):
2071 builtin_optab
= asin_optab
; break;
2072 CASE_FLT_FN (BUILT_IN_ACOS
):
2073 builtin_optab
= acos_optab
; break;
2074 CASE_FLT_FN (BUILT_IN_TAN
):
2075 builtin_optab
= tan_optab
; break;
2076 CASE_FLT_FN (BUILT_IN_ATAN
):
2077 builtin_optab
= atan_optab
; break;
2078 CASE_FLT_FN (BUILT_IN_FLOOR
):
2079 builtin_optab
= floor_optab
; break;
2080 CASE_FLT_FN (BUILT_IN_CEIL
):
2081 builtin_optab
= ceil_optab
; break;
2082 CASE_FLT_FN (BUILT_IN_TRUNC
):
2083 builtin_optab
= btrunc_optab
; break;
2084 CASE_FLT_FN (BUILT_IN_ROUND
):
2085 builtin_optab
= round_optab
; break;
2086 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
2087 builtin_optab
= nearbyint_optab
;
2088 if (flag_trapping_math
)
2090 /* Else fallthrough and expand as rint. */
2091 CASE_FLT_FN (BUILT_IN_RINT
):
2092 builtin_optab
= rint_optab
; break;
2093 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
2094 builtin_optab
= significand_optab
; break;
2099 /* Make a suitable register to place result in. */
2100 mode
= TYPE_MODE (TREE_TYPE (exp
));
2102 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2105 /* Before working hard, check whether the instruction is available, but try
2106 to widen the mode for specific operations. */
2107 if ((optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
2108 || (try_widening
&& !excess_precision_type (TREE_TYPE (exp
))))
2109 && (!errno_set
|| !optimize_insn_for_size_p ()))
2111 rtx result
= gen_reg_rtx (mode
);
2113 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2114 need to expand the argument again. This way, we will not perform
2115 side-effects more the once. */
2116 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2118 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2122 /* Compute into RESULT.
2123 Set RESULT to wherever the result comes back. */
2124 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2129 expand_errno_check (exp
, result
);
2131 /* Output the entire sequence. */
2132 insns
= get_insns ();
2138 /* If we were unable to expand via the builtin, stop the sequence
2139 (without outputting the insns) and call to the library function
2140 with the stabilized argument list. */
2144 return expand_call (exp
, target
, target
== const0_rtx
);
2147 /* Expand a call to the builtin binary math functions (pow and atan2).
2148 Return NULL_RTX if a normal call should be emitted rather than expanding the
2149 function in-line. EXP is the expression that is a call to the builtin
2150 function; if convenient, the result should be placed in TARGET.
2151 SUBTARGET may be used as the target for computing one of EXP's
2155 expand_builtin_mathfn_2 (tree exp
, rtx target
, rtx subtarget
)
2157 optab builtin_optab
;
2158 rtx op0
, op1
, insns
, result
;
2159 int op1_type
= REAL_TYPE
;
2160 tree fndecl
= get_callee_fndecl (exp
);
2162 enum machine_mode mode
;
2163 bool errno_set
= true;
2165 switch (DECL_FUNCTION_CODE (fndecl
))
2167 CASE_FLT_FN (BUILT_IN_SCALBN
):
2168 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2169 CASE_FLT_FN (BUILT_IN_LDEXP
):
2170 op1_type
= INTEGER_TYPE
;
2175 if (!validate_arglist (exp
, REAL_TYPE
, op1_type
, VOID_TYPE
))
2178 arg0
= CALL_EXPR_ARG (exp
, 0);
2179 arg1
= CALL_EXPR_ARG (exp
, 1);
2181 switch (DECL_FUNCTION_CODE (fndecl
))
2183 CASE_FLT_FN (BUILT_IN_POW
):
2184 builtin_optab
= pow_optab
; break;
2185 CASE_FLT_FN (BUILT_IN_ATAN2
):
2186 builtin_optab
= atan2_optab
; break;
2187 CASE_FLT_FN (BUILT_IN_SCALB
):
2188 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2190 builtin_optab
= scalb_optab
; break;
2191 CASE_FLT_FN (BUILT_IN_SCALBN
):
2192 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2193 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2195 /* Fall through... */
2196 CASE_FLT_FN (BUILT_IN_LDEXP
):
2197 builtin_optab
= ldexp_optab
; break;
2198 CASE_FLT_FN (BUILT_IN_FMOD
):
2199 builtin_optab
= fmod_optab
; break;
2200 CASE_FLT_FN (BUILT_IN_REMAINDER
):
2201 CASE_FLT_FN (BUILT_IN_DREM
):
2202 builtin_optab
= remainder_optab
; break;
2207 /* Make a suitable register to place result in. */
2208 mode
= TYPE_MODE (TREE_TYPE (exp
));
2210 /* Before working hard, check whether the instruction is available. */
2211 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2214 result
= gen_reg_rtx (mode
);
2216 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2219 if (errno_set
&& optimize_insn_for_size_p ())
2222 /* Always stabilize the argument list. */
2223 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2224 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2226 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2227 op1
= expand_normal (arg1
);
2231 /* Compute into RESULT.
2232 Set RESULT to wherever the result comes back. */
2233 result
= expand_binop (mode
, builtin_optab
, op0
, op1
,
2234 result
, 0, OPTAB_DIRECT
);
2236 /* If we were unable to expand via the builtin, stop the sequence
2237 (without outputting the insns) and call to the library function
2238 with the stabilized argument list. */
2242 return expand_call (exp
, target
, target
== const0_rtx
);
2246 expand_errno_check (exp
, result
);
2248 /* Output the entire sequence. */
2249 insns
= get_insns ();
2256 /* Expand a call to the builtin trinary math functions (fma).
2257 Return NULL_RTX if a normal call should be emitted rather than expanding the
2258 function in-line. EXP is the expression that is a call to the builtin
2259 function; if convenient, the result should be placed in TARGET.
2260 SUBTARGET may be used as the target for computing one of EXP's
2264 expand_builtin_mathfn_ternary (tree exp
, rtx target
, rtx subtarget
)
2266 optab builtin_optab
;
2267 rtx op0
, op1
, op2
, insns
, result
;
2268 tree fndecl
= get_callee_fndecl (exp
);
2269 tree arg0
, arg1
, arg2
;
2270 enum machine_mode mode
;
2272 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
2275 arg0
= CALL_EXPR_ARG (exp
, 0);
2276 arg1
= CALL_EXPR_ARG (exp
, 1);
2277 arg2
= CALL_EXPR_ARG (exp
, 2);
2279 switch (DECL_FUNCTION_CODE (fndecl
))
2281 CASE_FLT_FN (BUILT_IN_FMA
):
2282 builtin_optab
= fma_optab
; break;
2287 /* Make a suitable register to place result in. */
2288 mode
= TYPE_MODE (TREE_TYPE (exp
));
2290 /* Before working hard, check whether the instruction is available. */
2291 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2294 result
= gen_reg_rtx (mode
);
2296 /* Always stabilize the argument list. */
2297 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2298 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2299 CALL_EXPR_ARG (exp
, 2) = arg2
= builtin_save_expr (arg2
);
2301 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2302 op1
= expand_normal (arg1
);
2303 op2
= expand_normal (arg2
);
2307 /* Compute into RESULT.
2308 Set RESULT to wherever the result comes back. */
2309 result
= expand_ternary_op (mode
, builtin_optab
, op0
, op1
, op2
,
2312 /* If we were unable to expand via the builtin, stop the sequence
2313 (without outputting the insns) and call to the library function
2314 with the stabilized argument list. */
2318 return expand_call (exp
, target
, target
== const0_rtx
);
2321 /* Output the entire sequence. */
2322 insns
= get_insns ();
2329 /* Expand a call to the builtin sin and cos math functions.
2330 Return NULL_RTX if a normal call should be emitted rather than expanding the
2331 function in-line. EXP is the expression that is a call to the builtin
2332 function; if convenient, the result should be placed in TARGET.
2333 SUBTARGET may be used as the target for computing one of EXP's
2337 expand_builtin_mathfn_3 (tree exp
, rtx target
, rtx subtarget
)
2339 optab builtin_optab
;
2341 tree fndecl
= get_callee_fndecl (exp
);
2342 enum machine_mode mode
;
2345 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2348 arg
= CALL_EXPR_ARG (exp
, 0);
2350 switch (DECL_FUNCTION_CODE (fndecl
))
2352 CASE_FLT_FN (BUILT_IN_SIN
):
2353 CASE_FLT_FN (BUILT_IN_COS
):
2354 builtin_optab
= sincos_optab
; break;
2359 /* Make a suitable register to place result in. */
2360 mode
= TYPE_MODE (TREE_TYPE (exp
));
2362 /* Check if sincos insn is available, otherwise fallback
2363 to sin or cos insn. */
2364 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2365 switch (DECL_FUNCTION_CODE (fndecl
))
2367 CASE_FLT_FN (BUILT_IN_SIN
):
2368 builtin_optab
= sin_optab
; break;
2369 CASE_FLT_FN (BUILT_IN_COS
):
2370 builtin_optab
= cos_optab
; break;
2375 /* Before working hard, check whether the instruction is available. */
2376 if (optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
)
2378 rtx result
= gen_reg_rtx (mode
);
2380 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2381 need to expand the argument again. This way, we will not perform
2382 side-effects more the once. */
2383 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2385 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2389 /* Compute into RESULT.
2390 Set RESULT to wherever the result comes back. */
2391 if (builtin_optab
== sincos_optab
)
2395 switch (DECL_FUNCTION_CODE (fndecl
))
2397 CASE_FLT_FN (BUILT_IN_SIN
):
2398 ok
= expand_twoval_unop (builtin_optab
, op0
, 0, result
, 0);
2400 CASE_FLT_FN (BUILT_IN_COS
):
2401 ok
= expand_twoval_unop (builtin_optab
, op0
, result
, 0, 0);
2409 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2413 /* Output the entire sequence. */
2414 insns
= get_insns ();
2420 /* If we were unable to expand via the builtin, stop the sequence
2421 (without outputting the insns) and call to the library function
2422 with the stabilized argument list. */
2426 return expand_call (exp
, target
, target
== const0_rtx
);
2429 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2430 return an RTL instruction code that implements the functionality.
2431 If that isn't possible or available return CODE_FOR_nothing. */
2433 static enum insn_code
2434 interclass_mathfn_icode (tree arg
, tree fndecl
)
2436 bool errno_set
= false;
2437 optab builtin_optab
= unknown_optab
;
2438 enum machine_mode mode
;
2440 switch (DECL_FUNCTION_CODE (fndecl
))
2442 CASE_FLT_FN (BUILT_IN_ILOGB
):
2443 errno_set
= true; builtin_optab
= ilogb_optab
; break;
2444 CASE_FLT_FN (BUILT_IN_ISINF
):
2445 builtin_optab
= isinf_optab
; break;
2446 case BUILT_IN_ISNORMAL
:
2447 case BUILT_IN_ISFINITE
:
2448 CASE_FLT_FN (BUILT_IN_FINITE
):
2449 case BUILT_IN_FINITED32
:
2450 case BUILT_IN_FINITED64
:
2451 case BUILT_IN_FINITED128
:
2452 case BUILT_IN_ISINFD32
:
2453 case BUILT_IN_ISINFD64
:
2454 case BUILT_IN_ISINFD128
:
2455 /* These builtins have no optabs (yet). */
2461 /* There's no easy way to detect the case we need to set EDOM. */
2462 if (flag_errno_math
&& errno_set
)
2463 return CODE_FOR_nothing
;
2465 /* Optab mode depends on the mode of the input argument. */
2466 mode
= TYPE_MODE (TREE_TYPE (arg
));
2469 return optab_handler (builtin_optab
, mode
);
2470 return CODE_FOR_nothing
;
2473 /* Expand a call to one of the builtin math functions that operate on
2474 floating point argument and output an integer result (ilogb, isinf,
2476 Return 0 if a normal call should be emitted rather than expanding the
2477 function in-line. EXP is the expression that is a call to the builtin
2478 function; if convenient, the result should be placed in TARGET. */
2481 expand_builtin_interclass_mathfn (tree exp
, rtx target
)
2483 enum insn_code icode
= CODE_FOR_nothing
;
2485 tree fndecl
= get_callee_fndecl (exp
);
2486 enum machine_mode mode
;
2489 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2492 arg
= CALL_EXPR_ARG (exp
, 0);
2493 icode
= interclass_mathfn_icode (arg
, fndecl
);
2494 mode
= TYPE_MODE (TREE_TYPE (arg
));
2496 if (icode
!= CODE_FOR_nothing
)
2498 struct expand_operand ops
[1];
2499 rtx last
= get_last_insn ();
2500 tree orig_arg
= arg
;
2502 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2503 need to expand the argument again. This way, we will not perform
2504 side-effects more the once. */
2505 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2507 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2509 if (mode
!= GET_MODE (op0
))
2510 op0
= convert_to_mode (mode
, op0
, 0);
2512 create_output_operand (&ops
[0], target
, TYPE_MODE (TREE_TYPE (exp
)));
2513 if (maybe_legitimize_operands (icode
, 0, 1, ops
)
2514 && maybe_emit_unop_insn (icode
, ops
[0].value
, op0
, UNKNOWN
))
2515 return ops
[0].value
;
2517 delete_insns_since (last
);
2518 CALL_EXPR_ARG (exp
, 0) = orig_arg
;
2524 /* Expand a call to the builtin sincos math function.
2525 Return NULL_RTX if a normal call should be emitted rather than expanding the
2526 function in-line. EXP is the expression that is a call to the builtin
2530 expand_builtin_sincos (tree exp
)
2532 rtx op0
, op1
, op2
, target1
, target2
;
2533 enum machine_mode mode
;
2534 tree arg
, sinp
, cosp
;
2536 location_t loc
= EXPR_LOCATION (exp
);
2537 tree alias_type
, alias_off
;
2539 if (!validate_arglist (exp
, REAL_TYPE
,
2540 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2543 arg
= CALL_EXPR_ARG (exp
, 0);
2544 sinp
= CALL_EXPR_ARG (exp
, 1);
2545 cosp
= CALL_EXPR_ARG (exp
, 2);
2547 /* Make a suitable register to place result in. */
2548 mode
= TYPE_MODE (TREE_TYPE (arg
));
2550 /* Check if sincos insn is available, otherwise emit the call. */
2551 if (optab_handler (sincos_optab
, mode
) == CODE_FOR_nothing
)
2554 target1
= gen_reg_rtx (mode
);
2555 target2
= gen_reg_rtx (mode
);
2557 op0
= expand_normal (arg
);
2558 alias_type
= build_pointer_type_for_mode (TREE_TYPE (arg
), ptr_mode
, true);
2559 alias_off
= build_int_cst (alias_type
, 0);
2560 op1
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2562 op2
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2565 /* Compute into target1 and target2.
2566 Set TARGET to wherever the result comes back. */
2567 result
= expand_twoval_unop (sincos_optab
, op0
, target2
, target1
, 0);
2568 gcc_assert (result
);
2570 /* Move target1 and target2 to the memory locations indicated
2572 emit_move_insn (op1
, target1
);
2573 emit_move_insn (op2
, target2
);
2578 /* Expand a call to the internal cexpi builtin to the sincos math function.
2579 EXP is the expression that is a call to the builtin function; if convenient,
2580 the result should be placed in TARGET. */
2583 expand_builtin_cexpi (tree exp
, rtx target
)
2585 tree fndecl
= get_callee_fndecl (exp
);
2587 enum machine_mode mode
;
2589 location_t loc
= EXPR_LOCATION (exp
);
2591 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2594 arg
= CALL_EXPR_ARG (exp
, 0);
2595 type
= TREE_TYPE (arg
);
2596 mode
= TYPE_MODE (TREE_TYPE (arg
));
2598 /* Try expanding via a sincos optab, fall back to emitting a libcall
2599 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2600 is only generated from sincos, cexp or if we have either of them. */
2601 if (optab_handler (sincos_optab
, mode
) != CODE_FOR_nothing
)
2603 op1
= gen_reg_rtx (mode
);
2604 op2
= gen_reg_rtx (mode
);
2606 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2608 /* Compute into op1 and op2. */
2609 expand_twoval_unop (sincos_optab
, op0
, op2
, op1
, 0);
2611 else if (targetm
.libc_has_function (function_sincos
))
2613 tree call
, fn
= NULL_TREE
;
2617 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2618 fn
= builtin_decl_explicit (BUILT_IN_SINCOSF
);
2619 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2620 fn
= builtin_decl_explicit (BUILT_IN_SINCOS
);
2621 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2622 fn
= builtin_decl_explicit (BUILT_IN_SINCOSL
);
2626 op1
= assign_temp (TREE_TYPE (arg
), 1, 1);
2627 op2
= assign_temp (TREE_TYPE (arg
), 1, 1);
2628 op1a
= copy_addr_to_reg (XEXP (op1
, 0));
2629 op2a
= copy_addr_to_reg (XEXP (op2
, 0));
2630 top1
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op1a
);
2631 top2
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op2a
);
2633 /* Make sure not to fold the sincos call again. */
2634 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2635 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn
)),
2636 call
, 3, arg
, top1
, top2
));
2640 tree call
, fn
= NULL_TREE
, narg
;
2641 tree ctype
= build_complex_type (type
);
2643 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2644 fn
= builtin_decl_explicit (BUILT_IN_CEXPF
);
2645 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2646 fn
= builtin_decl_explicit (BUILT_IN_CEXP
);
2647 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2648 fn
= builtin_decl_explicit (BUILT_IN_CEXPL
);
2652 /* If we don't have a decl for cexp create one. This is the
2653 friendliest fallback if the user calls __builtin_cexpi
2654 without full target C99 function support. */
2655 if (fn
== NULL_TREE
)
2658 const char *name
= NULL
;
2660 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2662 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2664 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2667 fntype
= build_function_type_list (ctype
, ctype
, NULL_TREE
);
2668 fn
= build_fn_decl (name
, fntype
);
2671 narg
= fold_build2_loc (loc
, COMPLEX_EXPR
, ctype
,
2672 build_real (type
, dconst0
), arg
);
2674 /* Make sure not to fold the cexp call again. */
2675 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2676 return expand_expr (build_call_nary (ctype
, call
, 1, narg
),
2677 target
, VOIDmode
, EXPAND_NORMAL
);
2680 /* Now build the proper return type. */
2681 return expand_expr (build2 (COMPLEX_EXPR
, build_complex_type (type
),
2682 make_tree (TREE_TYPE (arg
), op2
),
2683 make_tree (TREE_TYPE (arg
), op1
)),
2684 target
, VOIDmode
, EXPAND_NORMAL
);
2687 /* Conveniently construct a function call expression. FNDECL names the
2688 function to be called, N is the number of arguments, and the "..."
2689 parameters are the argument expressions. Unlike build_call_exr
2690 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2693 build_call_nofold_loc (location_t loc
, tree fndecl
, int n
, ...)
2696 tree fntype
= TREE_TYPE (fndecl
);
2697 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), fndecl
);
2700 fn
= build_call_valist (TREE_TYPE (fntype
), fn
, n
, ap
);
2702 SET_EXPR_LOCATION (fn
, loc
);
2706 /* Expand a call to one of the builtin rounding functions gcc defines
2707 as an extension (lfloor and lceil). As these are gcc extensions we
2708 do not need to worry about setting errno to EDOM.
2709 If expanding via optab fails, lower expression to (int)(floor(x)).
2710 EXP is the expression that is a call to the builtin function;
2711 if convenient, the result should be placed in TARGET. */
2714 expand_builtin_int_roundingfn (tree exp
, rtx target
)
2716 convert_optab builtin_optab
;
2717 rtx op0
, insns
, tmp
;
2718 tree fndecl
= get_callee_fndecl (exp
);
2719 enum built_in_function fallback_fn
;
2720 tree fallback_fndecl
;
2721 enum machine_mode mode
;
2724 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2727 arg
= CALL_EXPR_ARG (exp
, 0);
2729 switch (DECL_FUNCTION_CODE (fndecl
))
2731 CASE_FLT_FN (BUILT_IN_ICEIL
):
2732 CASE_FLT_FN (BUILT_IN_LCEIL
):
2733 CASE_FLT_FN (BUILT_IN_LLCEIL
):
2734 builtin_optab
= lceil_optab
;
2735 fallback_fn
= BUILT_IN_CEIL
;
2738 CASE_FLT_FN (BUILT_IN_IFLOOR
):
2739 CASE_FLT_FN (BUILT_IN_LFLOOR
):
2740 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
2741 builtin_optab
= lfloor_optab
;
2742 fallback_fn
= BUILT_IN_FLOOR
;
2749 /* Make a suitable register to place result in. */
2750 mode
= TYPE_MODE (TREE_TYPE (exp
));
2752 target
= gen_reg_rtx (mode
);
2754 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2755 need to expand the argument again. This way, we will not perform
2756 side-effects more the once. */
2757 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2759 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2763 /* Compute into TARGET. */
2764 if (expand_sfix_optab (target
, op0
, builtin_optab
))
2766 /* Output the entire sequence. */
2767 insns
= get_insns ();
2773 /* If we were unable to expand via the builtin, stop the sequence
2774 (without outputting the insns). */
2777 /* Fall back to floating point rounding optab. */
2778 fallback_fndecl
= mathfn_built_in (TREE_TYPE (arg
), fallback_fn
);
2780 /* For non-C99 targets we may end up without a fallback fndecl here
2781 if the user called __builtin_lfloor directly. In this case emit
2782 a call to the floor/ceil variants nevertheless. This should result
2783 in the best user experience for not full C99 targets. */
2784 if (fallback_fndecl
== NULL_TREE
)
2787 const char *name
= NULL
;
2789 switch (DECL_FUNCTION_CODE (fndecl
))
2791 case BUILT_IN_ICEIL
:
2792 case BUILT_IN_LCEIL
:
2793 case BUILT_IN_LLCEIL
:
2796 case BUILT_IN_ICEILF
:
2797 case BUILT_IN_LCEILF
:
2798 case BUILT_IN_LLCEILF
:
2801 case BUILT_IN_ICEILL
:
2802 case BUILT_IN_LCEILL
:
2803 case BUILT_IN_LLCEILL
:
2806 case BUILT_IN_IFLOOR
:
2807 case BUILT_IN_LFLOOR
:
2808 case BUILT_IN_LLFLOOR
:
2811 case BUILT_IN_IFLOORF
:
2812 case BUILT_IN_LFLOORF
:
2813 case BUILT_IN_LLFLOORF
:
2816 case BUILT_IN_IFLOORL
:
2817 case BUILT_IN_LFLOORL
:
2818 case BUILT_IN_LLFLOORL
:
2825 fntype
= build_function_type_list (TREE_TYPE (arg
),
2826 TREE_TYPE (arg
), NULL_TREE
);
2827 fallback_fndecl
= build_fn_decl (name
, fntype
);
2830 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
), fallback_fndecl
, 1, arg
);
2832 tmp
= expand_normal (exp
);
2833 tmp
= maybe_emit_group_store (tmp
, TREE_TYPE (exp
));
2835 /* Truncate the result of floating point optab to integer
2836 via expand_fix (). */
2837 target
= gen_reg_rtx (mode
);
2838 expand_fix (target
, tmp
, 0);
2843 /* Expand a call to one of the builtin math functions doing integer
2845 Return 0 if a normal call should be emitted rather than expanding the
2846 function in-line. EXP is the expression that is a call to the builtin
2847 function; if convenient, the result should be placed in TARGET. */
2850 expand_builtin_int_roundingfn_2 (tree exp
, rtx target
)
2852 convert_optab builtin_optab
;
2854 tree fndecl
= get_callee_fndecl (exp
);
2856 enum machine_mode mode
;
2857 enum built_in_function fallback_fn
= BUILT_IN_NONE
;
2859 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2862 arg
= CALL_EXPR_ARG (exp
, 0);
2864 switch (DECL_FUNCTION_CODE (fndecl
))
2866 CASE_FLT_FN (BUILT_IN_IRINT
):
2867 fallback_fn
= BUILT_IN_LRINT
;
2869 CASE_FLT_FN (BUILT_IN_LRINT
):
2870 CASE_FLT_FN (BUILT_IN_LLRINT
):
2871 builtin_optab
= lrint_optab
;
2874 CASE_FLT_FN (BUILT_IN_IROUND
):
2875 fallback_fn
= BUILT_IN_LROUND
;
2877 CASE_FLT_FN (BUILT_IN_LROUND
):
2878 CASE_FLT_FN (BUILT_IN_LLROUND
):
2879 builtin_optab
= lround_optab
;
2886 /* There's no easy way to detect the case we need to set EDOM. */
2887 if (flag_errno_math
&& fallback_fn
== BUILT_IN_NONE
)
2890 /* Make a suitable register to place result in. */
2891 mode
= TYPE_MODE (TREE_TYPE (exp
));
2893 /* There's no easy way to detect the case we need to set EDOM. */
2894 if (!flag_errno_math
)
2896 rtx result
= gen_reg_rtx (mode
);
2898 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2899 need to expand the argument again. This way, we will not perform
2900 side-effects more the once. */
2901 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2903 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2907 if (expand_sfix_optab (result
, op0
, builtin_optab
))
2909 /* Output the entire sequence. */
2910 insns
= get_insns ();
2916 /* If we were unable to expand via the builtin, stop the sequence
2917 (without outputting the insns) and call to the library function
2918 with the stabilized argument list. */
2922 if (fallback_fn
!= BUILT_IN_NONE
)
2924 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2925 targets, (int) round (x) should never be transformed into
2926 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2927 a call to lround in the hope that the target provides at least some
2928 C99 functions. This should result in the best user experience for
2929 not full C99 targets. */
2930 tree fallback_fndecl
= mathfn_built_in_1 (TREE_TYPE (arg
),
2933 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
),
2934 fallback_fndecl
, 1, arg
);
2936 target
= expand_call (exp
, NULL_RTX
, target
== const0_rtx
);
2937 target
= maybe_emit_group_store (target
, TREE_TYPE (exp
));
2938 return convert_to_mode (mode
, target
, 0);
2941 return expand_call (exp
, target
, target
== const0_rtx
);
2944 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2945 a normal call should be emitted rather than expanding the function
2946 in-line. EXP is the expression that is a call to the builtin
2947 function; if convenient, the result should be placed in TARGET. */
2950 expand_builtin_powi (tree exp
, rtx target
)
2954 enum machine_mode mode
;
2955 enum machine_mode mode2
;
2957 if (! validate_arglist (exp
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2960 arg0
= CALL_EXPR_ARG (exp
, 0);
2961 arg1
= CALL_EXPR_ARG (exp
, 1);
2962 mode
= TYPE_MODE (TREE_TYPE (exp
));
2964 /* Emit a libcall to libgcc. */
2966 /* Mode of the 2nd argument must match that of an int. */
2967 mode2
= mode_for_size (INT_TYPE_SIZE
, MODE_INT
, 0);
2969 if (target
== NULL_RTX
)
2970 target
= gen_reg_rtx (mode
);
2972 op0
= expand_expr (arg0
, NULL_RTX
, mode
, EXPAND_NORMAL
);
2973 if (GET_MODE (op0
) != mode
)
2974 op0
= convert_to_mode (mode
, op0
, 0);
2975 op1
= expand_expr (arg1
, NULL_RTX
, mode2
, EXPAND_NORMAL
);
2976 if (GET_MODE (op1
) != mode2
)
2977 op1
= convert_to_mode (mode2
, op1
, 0);
2979 target
= emit_library_call_value (optab_libfunc (powi_optab
, mode
),
2980 target
, LCT_CONST
, mode
, 2,
2981 op0
, mode
, op1
, mode2
);
2986 /* Expand expression EXP which is a call to the strlen builtin. Return
2987 NULL_RTX if we failed the caller should emit a normal call, otherwise
2988 try to get the result in TARGET, if convenient. */
2991 expand_builtin_strlen (tree exp
, rtx target
,
2992 enum machine_mode target_mode
)
2994 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
2998 struct expand_operand ops
[4];
3001 tree src
= CALL_EXPR_ARG (exp
, 0);
3002 rtx src_reg
, before_strlen
;
3003 enum machine_mode insn_mode
= target_mode
;
3004 enum insn_code icode
= CODE_FOR_nothing
;
3007 /* If the length can be computed at compile-time, return it. */
3008 len
= c_strlen (src
, 0);
3010 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3012 /* If the length can be computed at compile-time and is constant
3013 integer, but there are side-effects in src, evaluate
3014 src for side-effects, then return len.
3015 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3016 can be optimized into: i++; x = 3; */
3017 len
= c_strlen (src
, 1);
3018 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
3020 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3021 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3024 align
= get_pointer_alignment (src
) / BITS_PER_UNIT
;
3026 /* If SRC is not a pointer type, don't do this operation inline. */
3030 /* Bail out if we can't compute strlen in the right mode. */
3031 while (insn_mode
!= VOIDmode
)
3033 icode
= optab_handler (strlen_optab
, insn_mode
);
3034 if (icode
!= CODE_FOR_nothing
)
3037 insn_mode
= GET_MODE_WIDER_MODE (insn_mode
);
3039 if (insn_mode
== VOIDmode
)
3042 /* Make a place to hold the source address. We will not expand
3043 the actual source until we are sure that the expansion will
3044 not fail -- there are trees that cannot be expanded twice. */
3045 src_reg
= gen_reg_rtx (Pmode
);
3047 /* Mark the beginning of the strlen sequence so we can emit the
3048 source operand later. */
3049 before_strlen
= get_last_insn ();
3051 create_output_operand (&ops
[0], target
, insn_mode
);
3052 create_fixed_operand (&ops
[1], gen_rtx_MEM (BLKmode
, src_reg
));
3053 create_integer_operand (&ops
[2], 0);
3054 create_integer_operand (&ops
[3], align
);
3055 if (!maybe_expand_insn (icode
, 4, ops
))
3058 /* Now that we are assured of success, expand the source. */
3060 pat
= expand_expr (src
, src_reg
, Pmode
, EXPAND_NORMAL
);
3063 #ifdef POINTERS_EXTEND_UNSIGNED
3064 if (GET_MODE (pat
) != Pmode
)
3065 pat
= convert_to_mode (Pmode
, pat
,
3066 POINTERS_EXTEND_UNSIGNED
);
3068 emit_move_insn (src_reg
, pat
);
3074 emit_insn_after (pat
, before_strlen
);
3076 emit_insn_before (pat
, get_insns ());
3078 /* Return the value in the proper mode for this function. */
3079 if (GET_MODE (ops
[0].value
) == target_mode
)
3080 target
= ops
[0].value
;
3081 else if (target
!= 0)
3082 convert_move (target
, ops
[0].value
, 0);
3084 target
= convert_to_mode (target_mode
, ops
[0].value
, 0);
3090 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3091 bytes from constant string DATA + OFFSET and return it as target
3095 builtin_memcpy_read_str (void *data
, HOST_WIDE_INT offset
,
3096 enum machine_mode mode
)
3098 const char *str
= (const char *) data
;
3100 gcc_assert (offset
>= 0
3101 && ((unsigned HOST_WIDE_INT
) offset
+ GET_MODE_SIZE (mode
)
3102 <= strlen (str
) + 1));
3104 return c_readstr (str
+ offset
, mode
);
3107 /* LEN specify length of the block of memcpy/memset operation.
3108 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3109 In some cases we can make very likely guess on max size, then we
3110 set it into PROBABLE_MAX_SIZE. */
3113 determine_block_size (tree len
, rtx len_rtx
,
3114 unsigned HOST_WIDE_INT
*min_size
,
3115 unsigned HOST_WIDE_INT
*max_size
,
3116 unsigned HOST_WIDE_INT
*probable_max_size
)
3118 if (CONST_INT_P (len_rtx
))
3120 *min_size
= *max_size
= UINTVAL (len_rtx
);
3125 double_int min
, max
;
3126 enum value_range_type range_type
= VR_UNDEFINED
;
3128 /* Determine bounds from the type. */
3129 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len
))))
3130 *min_size
= tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len
)));
3133 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len
))))
3134 *probable_max_size
= *max_size
= tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len
)));
3136 *probable_max_size
= *max_size
= GET_MODE_MASK (GET_MODE (len_rtx
));
3138 if (TREE_CODE (len
) == SSA_NAME
)
3139 range_type
= get_range_info (len
, &min
, &max
);
3140 if (range_type
== VR_RANGE
)
3142 if (min
.fits_uhwi () && *min_size
< min
.to_uhwi ())
3143 *min_size
= min
.to_uhwi ();
3144 if (max
.fits_uhwi () && *max_size
> max
.to_uhwi ())
3145 *probable_max_size
= *max_size
= max
.to_uhwi ();
3147 else if (range_type
== VR_ANTI_RANGE
)
3149 /* Anti range 0...N lets us to determine minimal size to N+1. */
3152 if ((max
+ double_int_one
).fits_uhwi ())
3153 *min_size
= (max
+ double_int_one
).to_uhwi ();
3161 Produce anti range allowing negative values of N. We still
3162 can use the information and make a guess that N is not negative.
3164 else if (!max
.ule (double_int_one
.lshift (30))
3165 && min
.fits_uhwi ())
3166 *probable_max_size
= min
.to_uhwi () - 1;
3169 gcc_checking_assert (*max_size
<=
3170 (unsigned HOST_WIDE_INT
)
3171 GET_MODE_MASK (GET_MODE (len_rtx
)));
3174 /* Expand a call EXP to the memcpy builtin.
3175 Return NULL_RTX if we failed, the caller should emit a normal call,
3176 otherwise try to get the result in TARGET, if convenient (and in
3177 mode MODE if that's convenient). */
3180 expand_builtin_memcpy (tree exp
, rtx target
)
3182 if (!validate_arglist (exp
,
3183 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3187 tree dest
= CALL_EXPR_ARG (exp
, 0);
3188 tree src
= CALL_EXPR_ARG (exp
, 1);
3189 tree len
= CALL_EXPR_ARG (exp
, 2);
3190 const char *src_str
;
3191 unsigned int src_align
= get_pointer_alignment (src
);
3192 unsigned int dest_align
= get_pointer_alignment (dest
);
3193 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
3194 HOST_WIDE_INT expected_size
= -1;
3195 unsigned int expected_align
= 0;
3196 unsigned HOST_WIDE_INT min_size
;
3197 unsigned HOST_WIDE_INT max_size
;
3198 unsigned HOST_WIDE_INT probable_max_size
;
3200 /* If DEST is not a pointer type, call the normal function. */
3201 if (dest_align
== 0)
3204 /* If either SRC is not a pointer type, don't do this
3205 operation in-line. */
3209 if (currently_expanding_gimple_stmt
)
3210 stringop_block_profile (currently_expanding_gimple_stmt
,
3211 &expected_align
, &expected_size
);
3213 if (expected_align
< dest_align
)
3214 expected_align
= dest_align
;
3215 dest_mem
= get_memory_rtx (dest
, len
);
3216 set_mem_align (dest_mem
, dest_align
);
3217 len_rtx
= expand_normal (len
);
3218 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3219 &probable_max_size
);
3220 src_str
= c_getstr (src
);
3222 /* If SRC is a string constant and block move would be done
3223 by pieces, we can avoid loading the string from memory
3224 and only stored the computed constants. */
3226 && CONST_INT_P (len_rtx
)
3227 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3228 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3229 CONST_CAST (char *, src_str
),
3232 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3233 builtin_memcpy_read_str
,
3234 CONST_CAST (char *, src_str
),
3235 dest_align
, false, 0);
3236 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3237 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3241 src_mem
= get_memory_rtx (src
, len
);
3242 set_mem_align (src_mem
, src_align
);
3244 /* Copy word part most expediently. */
3245 dest_addr
= emit_block_move_hints (dest_mem
, src_mem
, len_rtx
,
3246 CALL_EXPR_TAILCALL (exp
)
3247 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3248 expected_align
, expected_size
,
3249 min_size
, max_size
, probable_max_size
);
3253 dest_addr
= force_operand (XEXP (dest_mem
, 0), target
);
3254 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3260 /* Expand a call EXP to the mempcpy builtin.
3261 Return NULL_RTX if we failed; the caller should emit a normal call,
3262 otherwise try to get the result in TARGET, if convenient (and in
3263 mode MODE if that's convenient). If ENDP is 0 return the
3264 destination pointer, if ENDP is 1 return the end pointer ala
3265 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3269 expand_builtin_mempcpy (tree exp
, rtx target
, enum machine_mode mode
)
3271 if (!validate_arglist (exp
,
3272 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3276 tree dest
= CALL_EXPR_ARG (exp
, 0);
3277 tree src
= CALL_EXPR_ARG (exp
, 1);
3278 tree len
= CALL_EXPR_ARG (exp
, 2);
3279 return expand_builtin_mempcpy_args (dest
, src
, len
,
3280 target
, mode
, /*endp=*/ 1);
3284 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3285 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3286 so that this can also be called without constructing an actual CALL_EXPR.
3287 The other arguments and return value are the same as for
3288 expand_builtin_mempcpy. */
3291 expand_builtin_mempcpy_args (tree dest
, tree src
, tree len
,
3292 rtx target
, enum machine_mode mode
, int endp
)
3294 /* If return value is ignored, transform mempcpy into memcpy. */
3295 if (target
== const0_rtx
&& builtin_decl_implicit_p (BUILT_IN_MEMCPY
))
3297 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
3298 tree result
= build_call_nofold_loc (UNKNOWN_LOCATION
, fn
, 3,
3300 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3304 const char *src_str
;
3305 unsigned int src_align
= get_pointer_alignment (src
);
3306 unsigned int dest_align
= get_pointer_alignment (dest
);
3307 rtx dest_mem
, src_mem
, len_rtx
;
3309 /* If either SRC or DEST is not a pointer type, don't do this
3310 operation in-line. */
3311 if (dest_align
== 0 || src_align
== 0)
3314 /* If LEN is not constant, call the normal function. */
3315 if (! tree_fits_uhwi_p (len
))
3318 len_rtx
= expand_normal (len
);
3319 src_str
= c_getstr (src
);
3321 /* If SRC is a string constant and block move would be done
3322 by pieces, we can avoid loading the string from memory
3323 and only stored the computed constants. */
3325 && CONST_INT_P (len_rtx
)
3326 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3327 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3328 CONST_CAST (char *, src_str
),
3331 dest_mem
= get_memory_rtx (dest
, len
);
3332 set_mem_align (dest_mem
, dest_align
);
3333 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3334 builtin_memcpy_read_str
,
3335 CONST_CAST (char *, src_str
),
3336 dest_align
, false, endp
);
3337 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3338 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3342 if (CONST_INT_P (len_rtx
)
3343 && can_move_by_pieces (INTVAL (len_rtx
),
3344 MIN (dest_align
, src_align
)))
3346 dest_mem
= get_memory_rtx (dest
, len
);
3347 set_mem_align (dest_mem
, dest_align
);
3348 src_mem
= get_memory_rtx (src
, len
);
3349 set_mem_align (src_mem
, src_align
);
3350 dest_mem
= move_by_pieces (dest_mem
, src_mem
, INTVAL (len_rtx
),
3351 MIN (dest_align
, src_align
), endp
);
3352 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3353 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3362 # define HAVE_movstr 0
3363 # define CODE_FOR_movstr CODE_FOR_nothing
3366 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3367 we failed, the caller should emit a normal call, otherwise try to
3368 get the result in TARGET, if convenient. If ENDP is 0 return the
3369 destination pointer, if ENDP is 1 return the end pointer ala
3370 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3374 expand_movstr (tree dest
, tree src
, rtx target
, int endp
)
3376 struct expand_operand ops
[3];
3383 dest_mem
= get_memory_rtx (dest
, NULL
);
3384 src_mem
= get_memory_rtx (src
, NULL
);
3387 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
3388 dest_mem
= replace_equiv_address (dest_mem
, target
);
3391 create_output_operand (&ops
[0], endp
? target
: NULL_RTX
, Pmode
);
3392 create_fixed_operand (&ops
[1], dest_mem
);
3393 create_fixed_operand (&ops
[2], src_mem
);
3394 expand_insn (CODE_FOR_movstr
, 3, ops
);
3396 if (endp
&& target
!= const0_rtx
)
3398 target
= ops
[0].value
;
3399 /* movstr is supposed to set end to the address of the NUL
3400 terminator. If the caller requested a mempcpy-like return value,
3404 rtx tem
= plus_constant (GET_MODE (target
),
3405 gen_lowpart (GET_MODE (target
), target
), 1);
3406 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
3412 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3413 NULL_RTX if we failed the caller should emit a normal call, otherwise
3414 try to get the result in TARGET, if convenient (and in mode MODE if that's
3418 expand_builtin_strcpy (tree exp
, rtx target
)
3420 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3422 tree dest
= CALL_EXPR_ARG (exp
, 0);
3423 tree src
= CALL_EXPR_ARG (exp
, 1);
3424 return expand_builtin_strcpy_args (dest
, src
, target
);
3429 /* Helper function to do the actual work for expand_builtin_strcpy. The
3430 arguments to the builtin_strcpy call DEST and SRC are broken out
3431 so that this can also be called without constructing an actual CALL_EXPR.
3432 The other arguments and return value are the same as for
3433 expand_builtin_strcpy. */
3436 expand_builtin_strcpy_args (tree dest
, tree src
, rtx target
)
3438 return expand_movstr (dest
, src
, target
, /*endp=*/0);
3441 /* Expand a call EXP to the stpcpy builtin.
3442 Return NULL_RTX if we failed the caller should emit a normal call,
3443 otherwise try to get the result in TARGET, if convenient (and in
3444 mode MODE if that's convenient). */
3447 expand_builtin_stpcpy (tree exp
, rtx target
, enum machine_mode mode
)
3450 location_t loc
= EXPR_LOCATION (exp
);
3452 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3455 dst
= CALL_EXPR_ARG (exp
, 0);
3456 src
= CALL_EXPR_ARG (exp
, 1);
3458 /* If return value is ignored, transform stpcpy into strcpy. */
3459 if (target
== const0_rtx
&& builtin_decl_implicit (BUILT_IN_STRCPY
))
3461 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3462 tree result
= build_call_nofold_loc (loc
, fn
, 2, dst
, src
);
3463 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3470 /* Ensure we get an actual string whose length can be evaluated at
3471 compile-time, not an expression containing a string. This is
3472 because the latter will potentially produce pessimized code
3473 when used to produce the return value. */
3474 if (! c_getstr (src
) || ! (len
= c_strlen (src
, 0)))
3475 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3477 lenp1
= size_binop_loc (loc
, PLUS_EXPR
, len
, ssize_int (1));
3478 ret
= expand_builtin_mempcpy_args (dst
, src
, lenp1
,
3479 target
, mode
, /*endp=*/2);
3484 if (TREE_CODE (len
) == INTEGER_CST
)
3486 rtx len_rtx
= expand_normal (len
);
3488 if (CONST_INT_P (len_rtx
))
3490 ret
= expand_builtin_strcpy_args (dst
, src
, target
);
3496 if (mode
!= VOIDmode
)
3497 target
= gen_reg_rtx (mode
);
3499 target
= gen_reg_rtx (GET_MODE (ret
));
3501 if (GET_MODE (target
) != GET_MODE (ret
))
3502 ret
= gen_lowpart (GET_MODE (target
), ret
);
3504 ret
= plus_constant (GET_MODE (ret
), ret
, INTVAL (len_rtx
));
3505 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
3513 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3517 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3518 bytes from constant string DATA + OFFSET and return it as target
3522 builtin_strncpy_read_str (void *data
, HOST_WIDE_INT offset
,
3523 enum machine_mode mode
)
3525 const char *str
= (const char *) data
;
3527 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
3530 return c_readstr (str
+ offset
, mode
);
3533 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3534 NULL_RTX if we failed the caller should emit a normal call. */
3537 expand_builtin_strncpy (tree exp
, rtx target
)
3539 location_t loc
= EXPR_LOCATION (exp
);
3541 if (validate_arglist (exp
,
3542 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3544 tree dest
= CALL_EXPR_ARG (exp
, 0);
3545 tree src
= CALL_EXPR_ARG (exp
, 1);
3546 tree len
= CALL_EXPR_ARG (exp
, 2);
3547 tree slen
= c_strlen (src
, 1);
3549 /* We must be passed a constant len and src parameter. */
3550 if (!tree_fits_uhwi_p (len
) || !slen
|| !tree_fits_uhwi_p (slen
))
3553 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
3555 /* We're required to pad with trailing zeros if the requested
3556 len is greater than strlen(s2)+1. In that case try to
3557 use store_by_pieces, if it fails, punt. */
3558 if (tree_int_cst_lt (slen
, len
))
3560 unsigned int dest_align
= get_pointer_alignment (dest
);
3561 const char *p
= c_getstr (src
);
3564 if (!p
|| dest_align
== 0 || !tree_fits_uhwi_p (len
)
3565 || !can_store_by_pieces (tree_to_uhwi (len
),
3566 builtin_strncpy_read_str
,
3567 CONST_CAST (char *, p
),
3571 dest_mem
= get_memory_rtx (dest
, len
);
3572 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3573 builtin_strncpy_read_str
,
3574 CONST_CAST (char *, p
), dest_align
, false, 0);
3575 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3576 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3583 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3584 bytes from constant string DATA + OFFSET and return it as target
3588 builtin_memset_read_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3589 enum machine_mode mode
)
3591 const char *c
= (const char *) data
;
3592 char *p
= XALLOCAVEC (char, GET_MODE_SIZE (mode
));
3594 memset (p
, *c
, GET_MODE_SIZE (mode
));
3596 return c_readstr (p
, mode
);
3599 /* Callback routine for store_by_pieces. Return the RTL of a register
3600 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3601 char value given in the RTL register data. For example, if mode is
3602 4 bytes wide, return the RTL for 0x01010101*data. */
3605 builtin_memset_gen_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3606 enum machine_mode mode
)
3612 size
= GET_MODE_SIZE (mode
);
3616 p
= XALLOCAVEC (char, size
);
3617 memset (p
, 1, size
);
3618 coeff
= c_readstr (p
, mode
);
3620 target
= convert_to_mode (mode
, (rtx
) data
, 1);
3621 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
3622 return force_reg (mode
, target
);
3625 /* Expand expression EXP, which is a call to the memset builtin. Return
3626 NULL_RTX if we failed the caller should emit a normal call, otherwise
3627 try to get the result in TARGET, if convenient (and in mode MODE if that's
3631 expand_builtin_memset (tree exp
, rtx target
, enum machine_mode mode
)
3633 if (!validate_arglist (exp
,
3634 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3638 tree dest
= CALL_EXPR_ARG (exp
, 0);
3639 tree val
= CALL_EXPR_ARG (exp
, 1);
3640 tree len
= CALL_EXPR_ARG (exp
, 2);
3641 return expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
3645 /* Helper function to do the actual work for expand_builtin_memset. The
3646 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3647 so that this can also be called without constructing an actual CALL_EXPR.
3648 The other arguments and return value are the same as for
3649 expand_builtin_memset. */
3652 expand_builtin_memset_args (tree dest
, tree val
, tree len
,
3653 rtx target
, enum machine_mode mode
, tree orig_exp
)
3656 enum built_in_function fcode
;
3657 enum machine_mode val_mode
;
3659 unsigned int dest_align
;
3660 rtx dest_mem
, dest_addr
, len_rtx
;
3661 HOST_WIDE_INT expected_size
= -1;
3662 unsigned int expected_align
= 0;
3663 unsigned HOST_WIDE_INT min_size
;
3664 unsigned HOST_WIDE_INT max_size
;
3665 unsigned HOST_WIDE_INT probable_max_size
;
3667 dest_align
= get_pointer_alignment (dest
);
3669 /* If DEST is not a pointer type, don't do this operation in-line. */
3670 if (dest_align
== 0)
3673 if (currently_expanding_gimple_stmt
)
3674 stringop_block_profile (currently_expanding_gimple_stmt
,
3675 &expected_align
, &expected_size
);
3677 if (expected_align
< dest_align
)
3678 expected_align
= dest_align
;
3680 /* If the LEN parameter is zero, return DEST. */
3681 if (integer_zerop (len
))
3683 /* Evaluate and ignore VAL in case it has side-effects. */
3684 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3685 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
3688 /* Stabilize the arguments in case we fail. */
3689 dest
= builtin_save_expr (dest
);
3690 val
= builtin_save_expr (val
);
3691 len
= builtin_save_expr (len
);
3693 len_rtx
= expand_normal (len
);
3694 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3695 &probable_max_size
);
3696 dest_mem
= get_memory_rtx (dest
, len
);
3697 val_mode
= TYPE_MODE (unsigned_char_type_node
);
3699 if (TREE_CODE (val
) != INTEGER_CST
)
3703 val_rtx
= expand_normal (val
);
3704 val_rtx
= convert_to_mode (val_mode
, val_rtx
, 0);
3706 /* Assume that we can memset by pieces if we can store
3707 * the coefficients by pieces (in the required modes).
3708 * We can't pass builtin_memset_gen_str as that emits RTL. */
3710 if (tree_fits_uhwi_p (len
)
3711 && can_store_by_pieces (tree_to_uhwi (len
),
3712 builtin_memset_read_str
, &c
, dest_align
,
3715 val_rtx
= force_reg (val_mode
, val_rtx
);
3716 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3717 builtin_memset_gen_str
, val_rtx
, dest_align
,
3720 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, val_rtx
,
3721 dest_align
, expected_align
,
3722 expected_size
, min_size
, max_size
,
3726 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3727 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3731 if (target_char_cast (val
, &c
))
3736 if (tree_fits_uhwi_p (len
)
3737 && can_store_by_pieces (tree_to_uhwi (len
),
3738 builtin_memset_read_str
, &c
, dest_align
,
3740 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3741 builtin_memset_read_str
, &c
, dest_align
, true, 0);
3742 else if (!set_storage_via_setmem (dest_mem
, len_rtx
,
3743 gen_int_mode (c
, val_mode
),
3744 dest_align
, expected_align
,
3745 expected_size
, min_size
, max_size
,
3749 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3750 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3754 set_mem_align (dest_mem
, dest_align
);
3755 dest_addr
= clear_storage_hints (dest_mem
, len_rtx
,
3756 CALL_EXPR_TAILCALL (orig_exp
)
3757 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3758 expected_align
, expected_size
,
3764 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3765 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3771 fndecl
= get_callee_fndecl (orig_exp
);
3772 fcode
= DECL_FUNCTION_CODE (fndecl
);
3773 if (fcode
== BUILT_IN_MEMSET
)
3774 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 3,
3776 else if (fcode
== BUILT_IN_BZERO
)
3777 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 2,
3781 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
3782 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
3783 return expand_call (fn
, target
, target
== const0_rtx
);
3786 /* Expand expression EXP, which is a call to the bzero builtin. Return
3787 NULL_RTX if we failed the caller should emit a normal call. */
3790 expand_builtin_bzero (tree exp
)
3793 location_t loc
= EXPR_LOCATION (exp
);
3795 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3798 dest
= CALL_EXPR_ARG (exp
, 0);
3799 size
= CALL_EXPR_ARG (exp
, 1);
3801 /* New argument list transforming bzero(ptr x, int y) to
3802 memset(ptr x, int 0, size_t y). This is done this way
3803 so that if it isn't expanded inline, we fallback to
3804 calling bzero instead of memset. */
3806 return expand_builtin_memset_args (dest
, integer_zero_node
,
3807 fold_convert_loc (loc
,
3808 size_type_node
, size
),
3809 const0_rtx
, VOIDmode
, exp
);
3812 /* Expand expression EXP, which is a call to the memcmp built-in function.
3813 Return NULL_RTX if we failed and the caller should emit a normal call,
3814 otherwise try to get the result in TARGET, if convenient (and in mode
3815 MODE, if that's convenient). */
3818 expand_builtin_memcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
3819 ATTRIBUTE_UNUSED
enum machine_mode mode
)
3821 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
3823 if (!validate_arglist (exp
,
3824 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3827 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3828 implementing memcmp because it will stop if it encounters two
3830 #if defined HAVE_cmpmemsi
3832 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3835 tree arg1
= CALL_EXPR_ARG (exp
, 0);
3836 tree arg2
= CALL_EXPR_ARG (exp
, 1);
3837 tree len
= CALL_EXPR_ARG (exp
, 2);
3839 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
3840 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
3841 enum machine_mode insn_mode
;
3844 insn_mode
= insn_data
[(int) CODE_FOR_cmpmemsi
].operand
[0].mode
;
3848 /* If we don't have POINTER_TYPE, call the function. */
3849 if (arg1_align
== 0 || arg2_align
== 0)
3852 /* Make a place to write the result of the instruction. */
3855 && REG_P (result
) && GET_MODE (result
) == insn_mode
3856 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3857 result
= gen_reg_rtx (insn_mode
);
3859 arg1_rtx
= get_memory_rtx (arg1
, len
);
3860 arg2_rtx
= get_memory_rtx (arg2
, len
);
3861 arg3_rtx
= expand_normal (fold_convert_loc (loc
, sizetype
, len
));
3863 /* Set MEM_SIZE as appropriate. */
3864 if (CONST_INT_P (arg3_rtx
))
3866 set_mem_size (arg1_rtx
, INTVAL (arg3_rtx
));
3867 set_mem_size (arg2_rtx
, INTVAL (arg3_rtx
));
3871 insn
= gen_cmpmemsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3872 GEN_INT (MIN (arg1_align
, arg2_align
)));
3879 emit_library_call_value (memcmp_libfunc
, result
, LCT_PURE
,
3880 TYPE_MODE (integer_type_node
), 3,
3881 XEXP (arg1_rtx
, 0), Pmode
,
3882 XEXP (arg2_rtx
, 0), Pmode
,
3883 convert_to_mode (TYPE_MODE (sizetype
), arg3_rtx
,
3884 TYPE_UNSIGNED (sizetype
)),
3885 TYPE_MODE (sizetype
));
3887 /* Return the value in the proper mode for this function. */
3888 mode
= TYPE_MODE (TREE_TYPE (exp
));
3889 if (GET_MODE (result
) == mode
)
3891 else if (target
!= 0)
3893 convert_move (target
, result
, 0);
3897 return convert_to_mode (mode
, result
, 0);
3899 #endif /* HAVE_cmpmemsi. */
3904 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3905 if we failed the caller should emit a normal call, otherwise try to get
3906 the result in TARGET, if convenient. */
3909 expand_builtin_strcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
)
3911 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3914 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3915 if (direct_optab_handler (cmpstr_optab
, SImode
) != CODE_FOR_nothing
3916 || direct_optab_handler (cmpstrn_optab
, SImode
) != CODE_FOR_nothing
)
3918 rtx arg1_rtx
, arg2_rtx
;
3919 rtx result
, insn
= NULL_RTX
;
3921 tree arg1
= CALL_EXPR_ARG (exp
, 0);
3922 tree arg2
= CALL_EXPR_ARG (exp
, 1);
3924 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
3925 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
3927 /* If we don't have POINTER_TYPE, call the function. */
3928 if (arg1_align
== 0 || arg2_align
== 0)
3931 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3932 arg1
= builtin_save_expr (arg1
);
3933 arg2
= builtin_save_expr (arg2
);
3935 arg1_rtx
= get_memory_rtx (arg1
, NULL
);
3936 arg2_rtx
= get_memory_rtx (arg2
, NULL
);
3938 #ifdef HAVE_cmpstrsi
3939 /* Try to call cmpstrsi. */
3942 enum machine_mode insn_mode
3943 = insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
3945 /* Make a place to write the result of the instruction. */
3948 && REG_P (result
) && GET_MODE (result
) == insn_mode
3949 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3950 result
= gen_reg_rtx (insn_mode
);
3952 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
,
3953 GEN_INT (MIN (arg1_align
, arg2_align
)));
3956 #ifdef HAVE_cmpstrnsi
3957 /* Try to determine at least one length and call cmpstrnsi. */
3958 if (!insn
&& HAVE_cmpstrnsi
)
3963 enum machine_mode insn_mode
3964 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
3965 tree len1
= c_strlen (arg1
, 1);
3966 tree len2
= c_strlen (arg2
, 1);
3969 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
3971 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
3973 /* If we don't have a constant length for the first, use the length
3974 of the second, if we know it. We don't require a constant for
3975 this case; some cost analysis could be done if both are available
3976 but neither is constant. For now, assume they're equally cheap,
3977 unless one has side effects. If both strings have constant lengths,
3984 else if (TREE_SIDE_EFFECTS (len1
))
3986 else if (TREE_SIDE_EFFECTS (len2
))
3988 else if (TREE_CODE (len1
) != INTEGER_CST
)
3990 else if (TREE_CODE (len2
) != INTEGER_CST
)
3992 else if (tree_int_cst_lt (len1
, len2
))
3997 /* If both arguments have side effects, we cannot optimize. */
3998 if (!len
|| TREE_SIDE_EFFECTS (len
))
4001 arg3_rtx
= expand_normal (len
);
4003 /* Make a place to write the result of the instruction. */
4006 && REG_P (result
) && GET_MODE (result
) == insn_mode
4007 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
4008 result
= gen_reg_rtx (insn_mode
);
4010 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
4011 GEN_INT (MIN (arg1_align
, arg2_align
)));
4017 enum machine_mode mode
;
4020 /* Return the value in the proper mode for this function. */
4021 mode
= TYPE_MODE (TREE_TYPE (exp
));
4022 if (GET_MODE (result
) == mode
)
4025 return convert_to_mode (mode
, result
, 0);
4026 convert_move (target
, result
, 0);
4030 /* Expand the library call ourselves using a stabilized argument
4031 list to avoid re-evaluating the function's arguments twice. */
4032 #ifdef HAVE_cmpstrnsi
4035 fndecl
= get_callee_fndecl (exp
);
4036 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 2, arg1
, arg2
);
4037 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4038 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4039 return expand_call (fn
, target
, target
== const0_rtx
);
4045 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4046 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4047 the result in TARGET, if convenient. */
4050 expand_builtin_strncmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
4051 ATTRIBUTE_UNUSED
enum machine_mode mode
)
4053 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
4055 if (!validate_arglist (exp
,
4056 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4059 /* If c_strlen can determine an expression for one of the string
4060 lengths, and it doesn't have side effects, then emit cmpstrnsi
4061 using length MIN(strlen(string)+1, arg3). */
4062 #ifdef HAVE_cmpstrnsi
4065 tree len
, len1
, len2
;
4066 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
4069 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4070 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4071 tree arg3
= CALL_EXPR_ARG (exp
, 2);
4073 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4074 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4075 enum machine_mode insn_mode
4076 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
4078 len1
= c_strlen (arg1
, 1);
4079 len2
= c_strlen (arg2
, 1);
4082 len1
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len1
);
4084 len2
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len2
);
4086 /* If we don't have a constant length for the first, use the length
4087 of the second, if we know it. We don't require a constant for
4088 this case; some cost analysis could be done if both are available
4089 but neither is constant. For now, assume they're equally cheap,
4090 unless one has side effects. If both strings have constant lengths,
4097 else if (TREE_SIDE_EFFECTS (len1
))
4099 else if (TREE_SIDE_EFFECTS (len2
))
4101 else if (TREE_CODE (len1
) != INTEGER_CST
)
4103 else if (TREE_CODE (len2
) != INTEGER_CST
)
4105 else if (tree_int_cst_lt (len1
, len2
))
4110 /* If both arguments have side effects, we cannot optimize. */
4111 if (!len
|| TREE_SIDE_EFFECTS (len
))
4114 /* The actual new length parameter is MIN(len,arg3). */
4115 len
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (len
), len
,
4116 fold_convert_loc (loc
, TREE_TYPE (len
), arg3
));
4118 /* If we don't have POINTER_TYPE, call the function. */
4119 if (arg1_align
== 0 || arg2_align
== 0)
4122 /* Make a place to write the result of the instruction. */
4125 && REG_P (result
) && GET_MODE (result
) == insn_mode
4126 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
4127 result
= gen_reg_rtx (insn_mode
);
4129 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4130 arg1
= builtin_save_expr (arg1
);
4131 arg2
= builtin_save_expr (arg2
);
4132 len
= builtin_save_expr (len
);
4134 arg1_rtx
= get_memory_rtx (arg1
, len
);
4135 arg2_rtx
= get_memory_rtx (arg2
, len
);
4136 arg3_rtx
= expand_normal (len
);
4137 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
4138 GEN_INT (MIN (arg1_align
, arg2_align
)));
4143 /* Return the value in the proper mode for this function. */
4144 mode
= TYPE_MODE (TREE_TYPE (exp
));
4145 if (GET_MODE (result
) == mode
)
4148 return convert_to_mode (mode
, result
, 0);
4149 convert_move (target
, result
, 0);
4153 /* Expand the library call ourselves using a stabilized argument
4154 list to avoid re-evaluating the function's arguments twice. */
4155 fndecl
= get_callee_fndecl (exp
);
4156 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 3,
4158 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4159 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4160 return expand_call (fn
, target
, target
== const0_rtx
);
4166 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4167 if that's convenient. */
4170 expand_builtin_saveregs (void)
4174 /* Don't do __builtin_saveregs more than once in a function.
4175 Save the result of the first call and reuse it. */
4176 if (saveregs_value
!= 0)
4177 return saveregs_value
;
4179 /* When this function is called, it means that registers must be
4180 saved on entry to this function. So we migrate the call to the
4181 first insn of this function. */
4185 /* Do whatever the machine needs done in this case. */
4186 val
= targetm
.calls
.expand_builtin_saveregs ();
4191 saveregs_value
= val
;
4193 /* Put the insns after the NOTE that starts the function. If this
4194 is inside a start_sequence, make the outer-level insn chain current, so
4195 the code is placed at the start of the function. */
4196 push_topmost_sequence ();
4197 emit_insn_after (seq
, entry_of_function ());
4198 pop_topmost_sequence ();
4203 /* Expand a call to __builtin_next_arg. */
4206 expand_builtin_next_arg (void)
4208 /* Checking arguments is already done in fold_builtin_next_arg
4209 that must be called before this function. */
4210 return expand_binop (ptr_mode
, add_optab
,
4211 crtl
->args
.internal_arg_pointer
,
4212 crtl
->args
.arg_offset_rtx
,
4213 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
4216 /* Make it easier for the backends by protecting the valist argument
4217 from multiple evaluations. */
4220 stabilize_va_list_loc (location_t loc
, tree valist
, int needs_lvalue
)
4222 tree vatype
= targetm
.canonical_va_list_type (TREE_TYPE (valist
));
4224 /* The current way of determining the type of valist is completely
4225 bogus. We should have the information on the va builtin instead. */
4227 vatype
= targetm
.fn_abi_va_list (cfun
->decl
);
4229 if (TREE_CODE (vatype
) == ARRAY_TYPE
)
4231 if (TREE_SIDE_EFFECTS (valist
))
4232 valist
= save_expr (valist
);
4234 /* For this case, the backends will be expecting a pointer to
4235 vatype, but it's possible we've actually been given an array
4236 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4238 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4240 tree p1
= build_pointer_type (TREE_TYPE (vatype
));
4241 valist
= build_fold_addr_expr_with_type_loc (loc
, valist
, p1
);
4246 tree pt
= build_pointer_type (vatype
);
4250 if (! TREE_SIDE_EFFECTS (valist
))
4253 valist
= fold_build1_loc (loc
, ADDR_EXPR
, pt
, valist
);
4254 TREE_SIDE_EFFECTS (valist
) = 1;
4257 if (TREE_SIDE_EFFECTS (valist
))
4258 valist
= save_expr (valist
);
4259 valist
= fold_build2_loc (loc
, MEM_REF
,
4260 vatype
, valist
, build_int_cst (pt
, 0));
4266 /* The "standard" definition of va_list is void*. */
4269 std_build_builtin_va_list (void)
4271 return ptr_type_node
;
4274 /* The "standard" abi va_list is va_list_type_node. */
4277 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED
)
4279 return va_list_type_node
;
4282 /* The "standard" type of va_list is va_list_type_node. */
4285 std_canonical_va_list_type (tree type
)
4289 if (INDIRECT_REF_P (type
))
4290 type
= TREE_TYPE (type
);
4291 else if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (TREE_TYPE (type
)))
4292 type
= TREE_TYPE (type
);
4293 wtype
= va_list_type_node
;
4295 /* Treat structure va_list types. */
4296 if (TREE_CODE (wtype
) == RECORD_TYPE
&& POINTER_TYPE_P (htype
))
4297 htype
= TREE_TYPE (htype
);
4298 else if (TREE_CODE (wtype
) == ARRAY_TYPE
)
4300 /* If va_list is an array type, the argument may have decayed
4301 to a pointer type, e.g. by being passed to another function.
4302 In that case, unwrap both types so that we can compare the
4303 underlying records. */
4304 if (TREE_CODE (htype
) == ARRAY_TYPE
4305 || POINTER_TYPE_P (htype
))
4307 wtype
= TREE_TYPE (wtype
);
4308 htype
= TREE_TYPE (htype
);
4311 if (TYPE_MAIN_VARIANT (wtype
) == TYPE_MAIN_VARIANT (htype
))
4312 return va_list_type_node
;
4317 /* The "standard" implementation of va_start: just assign `nextarg' to
4321 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
4323 rtx va_r
= expand_expr (valist
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
4324 convert_move (va_r
, nextarg
, 0);
4327 /* Expand EXP, a call to __builtin_va_start. */
4330 expand_builtin_va_start (tree exp
)
4334 location_t loc
= EXPR_LOCATION (exp
);
4336 if (call_expr_nargs (exp
) < 2)
4338 error_at (loc
, "too few arguments to function %<va_start%>");
4342 if (fold_builtin_next_arg (exp
, true))
4345 nextarg
= expand_builtin_next_arg ();
4346 valist
= stabilize_va_list_loc (loc
, CALL_EXPR_ARG (exp
, 0), 1);
4348 if (targetm
.expand_builtin_va_start
)
4349 targetm
.expand_builtin_va_start (valist
, nextarg
);
4351 std_expand_builtin_va_start (valist
, nextarg
);
4356 /* Expand EXP, a call to __builtin_va_end. */
4359 expand_builtin_va_end (tree exp
)
4361 tree valist
= CALL_EXPR_ARG (exp
, 0);
4363 /* Evaluate for side effects, if needed. I hate macros that don't
4365 if (TREE_SIDE_EFFECTS (valist
))
4366 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4371 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4372 builtin rather than just as an assignment in stdarg.h because of the
4373 nastiness of array-type va_list types. */
4376 expand_builtin_va_copy (tree exp
)
4379 location_t loc
= EXPR_LOCATION (exp
);
4381 dst
= CALL_EXPR_ARG (exp
, 0);
4382 src
= CALL_EXPR_ARG (exp
, 1);
4384 dst
= stabilize_va_list_loc (loc
, dst
, 1);
4385 src
= stabilize_va_list_loc (loc
, src
, 0);
4387 gcc_assert (cfun
!= NULL
&& cfun
->decl
!= NULL_TREE
);
4389 if (TREE_CODE (targetm
.fn_abi_va_list (cfun
->decl
)) != ARRAY_TYPE
)
4391 t
= build2 (MODIFY_EXPR
, targetm
.fn_abi_va_list (cfun
->decl
), dst
, src
);
4392 TREE_SIDE_EFFECTS (t
) = 1;
4393 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4397 rtx dstb
, srcb
, size
;
4399 /* Evaluate to pointers. */
4400 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4401 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4402 size
= expand_expr (TYPE_SIZE_UNIT (targetm
.fn_abi_va_list (cfun
->decl
)),
4403 NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
4405 dstb
= convert_memory_address (Pmode
, dstb
);
4406 srcb
= convert_memory_address (Pmode
, srcb
);
4408 /* "Dereference" to BLKmode memories. */
4409 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
4410 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
4411 set_mem_align (dstb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4412 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
4413 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
4414 set_mem_align (srcb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4417 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
4423 /* Expand a call to one of the builtin functions __builtin_frame_address or
4424 __builtin_return_address. */
4427 expand_builtin_frame_address (tree fndecl
, tree exp
)
4429 /* The argument must be a nonnegative integer constant.
4430 It counts the number of frames to scan up the stack.
4431 The value is the return address saved in that frame. */
4432 if (call_expr_nargs (exp
) == 0)
4433 /* Warning about missing arg was already issued. */
4435 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp
, 0)))
4437 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4438 error ("invalid argument to %<__builtin_frame_address%>");
4440 error ("invalid argument to %<__builtin_return_address%>");
4446 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
),
4447 tree_to_uhwi (CALL_EXPR_ARG (exp
, 0)));
4449 /* Some ports cannot access arbitrary stack frames. */
4452 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4453 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4455 warning (0, "unsupported argument to %<__builtin_return_address%>");
4459 /* For __builtin_frame_address, return what we've got. */
4460 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4464 && ! CONSTANT_P (tem
))
4465 tem
= copy_addr_to_reg (tem
);
4470 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4471 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4472 is the same as for allocate_dynamic_stack_space. */
4475 expand_builtin_alloca (tree exp
, bool cannot_accumulate
)
4481 bool alloca_with_align
= (DECL_FUNCTION_CODE (get_callee_fndecl (exp
))
4482 == BUILT_IN_ALLOCA_WITH_ALIGN
);
4485 = (alloca_with_align
4486 ? validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
4487 : validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
));
4492 /* Compute the argument. */
4493 op0
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4495 /* Compute the alignment. */
4496 align
= (alloca_with_align
4497 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp
, 1))
4498 : BIGGEST_ALIGNMENT
);
4500 /* Allocate the desired space. */
4501 result
= allocate_dynamic_stack_space (op0
, 0, align
, cannot_accumulate
);
4502 result
= convert_memory_address (ptr_mode
, result
);
4507 /* Expand a call to bswap builtin in EXP.
4508 Return NULL_RTX if a normal call should be emitted rather than expanding the
4509 function in-line. If convenient, the result should be placed in TARGET.
4510 SUBTARGET may be used as the target for computing one of EXP's operands. */
4513 expand_builtin_bswap (enum machine_mode target_mode
, tree exp
, rtx target
,
4519 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4522 arg
= CALL_EXPR_ARG (exp
, 0);
4523 op0
= expand_expr (arg
,
4524 subtarget
&& GET_MODE (subtarget
) == target_mode
4525 ? subtarget
: NULL_RTX
,
4526 target_mode
, EXPAND_NORMAL
);
4527 if (GET_MODE (op0
) != target_mode
)
4528 op0
= convert_to_mode (target_mode
, op0
, 1);
4530 target
= expand_unop (target_mode
, bswap_optab
, op0
, target
, 1);
4532 gcc_assert (target
);
4534 return convert_to_mode (target_mode
, target
, 1);
4537 /* Expand a call to a unary builtin in EXP.
4538 Return NULL_RTX if a normal call should be emitted rather than expanding the
4539 function in-line. If convenient, the result should be placed in TARGET.
4540 SUBTARGET may be used as the target for computing one of EXP's operands. */
4543 expand_builtin_unop (enum machine_mode target_mode
, tree exp
, rtx target
,
4544 rtx subtarget
, optab op_optab
)
4548 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4551 /* Compute the argument. */
4552 op0
= expand_expr (CALL_EXPR_ARG (exp
, 0),
4554 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0)))
4555 == GET_MODE (subtarget
))) ? subtarget
: NULL_RTX
,
4556 VOIDmode
, EXPAND_NORMAL
);
4557 /* Compute op, into TARGET if possible.
4558 Set TARGET to wherever the result comes back. */
4559 target
= expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))),
4560 op_optab
, op0
, target
, op_optab
!= clrsb_optab
);
4561 gcc_assert (target
);
4563 return convert_to_mode (target_mode
, target
, 0);
4566 /* Expand a call to __builtin_expect. We just return our argument
4567 as the builtin_expect semantic should've been already executed by
4568 tree branch prediction pass. */
4571 expand_builtin_expect (tree exp
, rtx target
)
4575 if (call_expr_nargs (exp
) < 2)
4577 arg
= CALL_EXPR_ARG (exp
, 0);
4579 target
= expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4580 /* When guessing was done, the hints should be already stripped away. */
4581 gcc_assert (!flag_guess_branch_prob
4582 || optimize
== 0 || seen_error ());
4586 /* Expand a call to __builtin_assume_aligned. We just return our first
4587 argument as the builtin_assume_aligned semantic should've been already
4591 expand_builtin_assume_aligned (tree exp
, rtx target
)
4593 if (call_expr_nargs (exp
) < 2)
4595 target
= expand_expr (CALL_EXPR_ARG (exp
, 0), target
, VOIDmode
,
4597 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 1))
4598 && (call_expr_nargs (exp
) < 3
4599 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 2))));
4604 expand_builtin_trap (void)
4609 rtx insn
= emit_insn (gen_trap ());
4610 /* For trap insns when not accumulating outgoing args force
4611 REG_ARGS_SIZE note to prevent crossjumping of calls with
4612 different args sizes. */
4613 if (!ACCUMULATE_OUTGOING_ARGS
)
4614 add_reg_note (insn
, REG_ARGS_SIZE
, GEN_INT (stack_pointer_delta
));
4618 emit_library_call (abort_libfunc
, LCT_NORETURN
, VOIDmode
, 0);
4622 /* Expand a call to __builtin_unreachable. We do nothing except emit
4623 a barrier saying that control flow will not pass here.
4625 It is the responsibility of the program being compiled to ensure
4626 that control flow does never reach __builtin_unreachable. */
4628 expand_builtin_unreachable (void)
4633 /* Expand EXP, a call to fabs, fabsf or fabsl.
4634 Return NULL_RTX if a normal call should be emitted rather than expanding
4635 the function inline. If convenient, the result should be placed
4636 in TARGET. SUBTARGET may be used as the target for computing
4640 expand_builtin_fabs (tree exp
, rtx target
, rtx subtarget
)
4642 enum machine_mode mode
;
4646 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4649 arg
= CALL_EXPR_ARG (exp
, 0);
4650 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
4651 mode
= TYPE_MODE (TREE_TYPE (arg
));
4652 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4653 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
4656 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4657 Return NULL is a normal call should be emitted rather than expanding the
4658 function inline. If convenient, the result should be placed in TARGET.
4659 SUBTARGET may be used as the target for computing the operand. */
4662 expand_builtin_copysign (tree exp
, rtx target
, rtx subtarget
)
4667 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
4670 arg
= CALL_EXPR_ARG (exp
, 0);
4671 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4673 arg
= CALL_EXPR_ARG (exp
, 1);
4674 op1
= expand_normal (arg
);
4676 return expand_copysign (op0
, op1
, target
);
4679 /* Create a new constant string literal and return a char* pointer to it.
4680 The STRING_CST value is the LEN characters at STR. */
4682 build_string_literal (int len
, const char *str
)
4684 tree t
, elem
, index
, type
;
4686 t
= build_string (len
, str
);
4687 elem
= build_type_variant (char_type_node
, 1, 0);
4688 index
= build_index_type (size_int (len
- 1));
4689 type
= build_array_type (elem
, index
);
4690 TREE_TYPE (t
) = type
;
4691 TREE_CONSTANT (t
) = 1;
4692 TREE_READONLY (t
) = 1;
4693 TREE_STATIC (t
) = 1;
4695 type
= build_pointer_type (elem
);
4696 t
= build1 (ADDR_EXPR
, type
,
4697 build4 (ARRAY_REF
, elem
,
4698 t
, integer_zero_node
, NULL_TREE
, NULL_TREE
));
4702 /* Expand a call to __builtin___clear_cache. */
4705 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED
)
4707 #ifndef HAVE_clear_cache
4708 #ifdef CLEAR_INSN_CACHE
4709 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4710 does something. Just do the default expansion to a call to
4714 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4715 does nothing. There is no need to call it. Do nothing. */
4717 #endif /* CLEAR_INSN_CACHE */
4719 /* We have a "clear_cache" insn, and it will handle everything. */
4721 rtx begin_rtx
, end_rtx
;
4723 /* We must not expand to a library call. If we did, any
4724 fallback library function in libgcc that might contain a call to
4725 __builtin___clear_cache() would recurse infinitely. */
4726 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4728 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4732 if (HAVE_clear_cache
)
4734 struct expand_operand ops
[2];
4736 begin
= CALL_EXPR_ARG (exp
, 0);
4737 begin_rtx
= expand_expr (begin
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4739 end
= CALL_EXPR_ARG (exp
, 1);
4740 end_rtx
= expand_expr (end
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4742 create_address_operand (&ops
[0], begin_rtx
);
4743 create_address_operand (&ops
[1], end_rtx
);
4744 if (maybe_expand_insn (CODE_FOR_clear_cache
, 2, ops
))
4748 #endif /* HAVE_clear_cache */
4751 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4754 round_trampoline_addr (rtx tramp
)
4756 rtx temp
, addend
, mask
;
4758 /* If we don't need too much alignment, we'll have been guaranteed
4759 proper alignment by get_trampoline_type. */
4760 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
4763 /* Round address up to desired boundary. */
4764 temp
= gen_reg_rtx (Pmode
);
4765 addend
= gen_int_mode (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1, Pmode
);
4766 mask
= gen_int_mode (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
, Pmode
);
4768 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
4769 temp
, 0, OPTAB_LIB_WIDEN
);
4770 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
4771 temp
, 0, OPTAB_LIB_WIDEN
);
4777 expand_builtin_init_trampoline (tree exp
, bool onstack
)
4779 tree t_tramp
, t_func
, t_chain
;
4780 rtx m_tramp
, r_tramp
, r_chain
, tmp
;
4782 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
,
4783 POINTER_TYPE
, VOID_TYPE
))
4786 t_tramp
= CALL_EXPR_ARG (exp
, 0);
4787 t_func
= CALL_EXPR_ARG (exp
, 1);
4788 t_chain
= CALL_EXPR_ARG (exp
, 2);
4790 r_tramp
= expand_normal (t_tramp
);
4791 m_tramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
4792 MEM_NOTRAP_P (m_tramp
) = 1;
4794 /* If ONSTACK, the TRAMP argument should be the address of a field
4795 within the local function's FRAME decl. Either way, let's see if
4796 we can fill in the MEM_ATTRs for this memory. */
4797 if (TREE_CODE (t_tramp
) == ADDR_EXPR
)
4798 set_mem_attributes (m_tramp
, TREE_OPERAND (t_tramp
, 0), true);
4800 /* Creator of a heap trampoline is responsible for making sure the
4801 address is aligned to at least STACK_BOUNDARY. Normally malloc
4802 will ensure this anyhow. */
4803 tmp
= round_trampoline_addr (r_tramp
);
4806 m_tramp
= change_address (m_tramp
, BLKmode
, tmp
);
4807 set_mem_align (m_tramp
, TRAMPOLINE_ALIGNMENT
);
4808 set_mem_size (m_tramp
, TRAMPOLINE_SIZE
);
4811 /* The FUNC argument should be the address of the nested function.
4812 Extract the actual function decl to pass to the hook. */
4813 gcc_assert (TREE_CODE (t_func
) == ADDR_EXPR
);
4814 t_func
= TREE_OPERAND (t_func
, 0);
4815 gcc_assert (TREE_CODE (t_func
) == FUNCTION_DECL
);
4817 r_chain
= expand_normal (t_chain
);
4819 /* Generate insns to initialize the trampoline. */
4820 targetm
.calls
.trampoline_init (m_tramp
, t_func
, r_chain
);
4824 trampolines_created
= 1;
4826 warning_at (DECL_SOURCE_LOCATION (t_func
), OPT_Wtrampolines
,
4827 "trampoline generated for nested function %qD", t_func
);
4834 expand_builtin_adjust_trampoline (tree exp
)
4838 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
4841 tramp
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4842 tramp
= round_trampoline_addr (tramp
);
4843 if (targetm
.calls
.trampoline_adjust_address
)
4844 tramp
= targetm
.calls
.trampoline_adjust_address (tramp
);
4849 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4850 function. The function first checks whether the back end provides
4851 an insn to implement signbit for the respective mode. If not, it
4852 checks whether the floating point format of the value is such that
4853 the sign bit can be extracted. If that is not the case, the
4854 function returns NULL_RTX to indicate that a normal call should be
4855 emitted rather than expanding the function in-line. EXP is the
4856 expression that is a call to the builtin function; if convenient,
4857 the result should be placed in TARGET. */
4859 expand_builtin_signbit (tree exp
, rtx target
)
4861 const struct real_format
*fmt
;
4862 enum machine_mode fmode
, imode
, rmode
;
4865 enum insn_code icode
;
4867 location_t loc
= EXPR_LOCATION (exp
);
4869 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4872 arg
= CALL_EXPR_ARG (exp
, 0);
4873 fmode
= TYPE_MODE (TREE_TYPE (arg
));
4874 rmode
= TYPE_MODE (TREE_TYPE (exp
));
4875 fmt
= REAL_MODE_FORMAT (fmode
);
4877 arg
= builtin_save_expr (arg
);
4879 /* Expand the argument yielding a RTX expression. */
4880 temp
= expand_normal (arg
);
4882 /* Check if the back end provides an insn that handles signbit for the
4884 icode
= optab_handler (signbit_optab
, fmode
);
4885 if (icode
!= CODE_FOR_nothing
)
4887 rtx last
= get_last_insn ();
4888 target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
4889 if (maybe_emit_unop_insn (icode
, target
, temp
, UNKNOWN
))
4891 delete_insns_since (last
);
4894 /* For floating point formats without a sign bit, implement signbit
4896 bitpos
= fmt
->signbit_ro
;
4899 /* But we can't do this if the format supports signed zero. */
4900 if (fmt
->has_signed_zero
&& HONOR_SIGNED_ZEROS (fmode
))
4903 arg
= fold_build2_loc (loc
, LT_EXPR
, TREE_TYPE (exp
), arg
,
4904 build_real (TREE_TYPE (arg
), dconst0
));
4905 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4908 if (GET_MODE_SIZE (fmode
) <= UNITS_PER_WORD
)
4910 imode
= int_mode_for_mode (fmode
);
4911 if (imode
== BLKmode
)
4913 temp
= gen_lowpart (imode
, temp
);
4918 /* Handle targets with different FP word orders. */
4919 if (FLOAT_WORDS_BIG_ENDIAN
)
4920 word
= (GET_MODE_BITSIZE (fmode
) - bitpos
) / BITS_PER_WORD
;
4922 word
= bitpos
/ BITS_PER_WORD
;
4923 temp
= operand_subword_force (temp
, word
, fmode
);
4924 bitpos
= bitpos
% BITS_PER_WORD
;
4927 /* Force the intermediate word_mode (or narrower) result into a
4928 register. This avoids attempting to create paradoxical SUBREGs
4929 of floating point modes below. */
4930 temp
= force_reg (imode
, temp
);
4932 /* If the bitpos is within the "result mode" lowpart, the operation
4933 can be implement with a single bitwise AND. Otherwise, we need
4934 a right shift and an AND. */
4936 if (bitpos
< GET_MODE_BITSIZE (rmode
))
4938 double_int mask
= double_int_zero
.set_bit (bitpos
);
4940 if (GET_MODE_SIZE (imode
) > GET_MODE_SIZE (rmode
))
4941 temp
= gen_lowpart (rmode
, temp
);
4942 temp
= expand_binop (rmode
, and_optab
, temp
,
4943 immed_double_int_const (mask
, rmode
),
4944 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4948 /* Perform a logical right shift to place the signbit in the least
4949 significant bit, then truncate the result to the desired mode
4950 and mask just this bit. */
4951 temp
= expand_shift (RSHIFT_EXPR
, imode
, temp
, bitpos
, NULL_RTX
, 1);
4952 temp
= gen_lowpart (rmode
, temp
);
4953 temp
= expand_binop (rmode
, and_optab
, temp
, const1_rtx
,
4954 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4960 /* Expand fork or exec calls. TARGET is the desired target of the
4961 call. EXP is the call. FN is the
4962 identificator of the actual function. IGNORE is nonzero if the
4963 value is to be ignored. */
4966 expand_builtin_fork_or_exec (tree fn
, tree exp
, rtx target
, int ignore
)
4971 /* If we are not profiling, just call the function. */
4972 if (!profile_arc_flag
)
4975 /* Otherwise call the wrapper. This should be equivalent for the rest of
4976 compiler, so the code does not diverge, and the wrapper may run the
4977 code necessary for keeping the profiling sane. */
4979 switch (DECL_FUNCTION_CODE (fn
))
4982 id
= get_identifier ("__gcov_fork");
4985 case BUILT_IN_EXECL
:
4986 id
= get_identifier ("__gcov_execl");
4989 case BUILT_IN_EXECV
:
4990 id
= get_identifier ("__gcov_execv");
4993 case BUILT_IN_EXECLP
:
4994 id
= get_identifier ("__gcov_execlp");
4997 case BUILT_IN_EXECLE
:
4998 id
= get_identifier ("__gcov_execle");
5001 case BUILT_IN_EXECVP
:
5002 id
= get_identifier ("__gcov_execvp");
5005 case BUILT_IN_EXECVE
:
5006 id
= get_identifier ("__gcov_execve");
5013 decl
= build_decl (DECL_SOURCE_LOCATION (fn
),
5014 FUNCTION_DECL
, id
, TREE_TYPE (fn
));
5015 DECL_EXTERNAL (decl
) = 1;
5016 TREE_PUBLIC (decl
) = 1;
5017 DECL_ARTIFICIAL (decl
) = 1;
5018 TREE_NOTHROW (decl
) = 1;
5019 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
5020 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
5021 call
= rewrite_call_expr (EXPR_LOCATION (exp
), exp
, 0, decl
, 0);
5022 return expand_call (call
, target
, ignore
);
5027 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5028 the pointer in these functions is void*, the tree optimizers may remove
5029 casts. The mode computed in expand_builtin isn't reliable either, due
5030 to __sync_bool_compare_and_swap.
5032 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5033 group of builtins. This gives us log2 of the mode size. */
5035 static inline enum machine_mode
5036 get_builtin_sync_mode (int fcode_diff
)
5038 /* The size is not negotiable, so ask not to get BLKmode in return
5039 if the target indicates that a smaller size would be better. */
5040 return mode_for_size (BITS_PER_UNIT
<< fcode_diff
, MODE_INT
, 0);
5043 /* Expand the memory expression LOC and return the appropriate memory operand
5044 for the builtin_sync operations. */
5047 get_builtin_sync_mem (tree loc
, enum machine_mode mode
)
5051 addr
= expand_expr (loc
, NULL_RTX
, ptr_mode
, EXPAND_SUM
);
5052 addr
= convert_memory_address (Pmode
, addr
);
5054 /* Note that we explicitly do not want any alias information for this
5055 memory, so that we kill all other live memories. Otherwise we don't
5056 satisfy the full barrier semantics of the intrinsic. */
5057 mem
= validize_mem (gen_rtx_MEM (mode
, addr
));
5059 /* The alignment needs to be at least according to that of the mode. */
5060 set_mem_align (mem
, MAX (GET_MODE_ALIGNMENT (mode
),
5061 get_pointer_alignment (loc
)));
5062 set_mem_alias_set (mem
, ALIAS_SET_MEMORY_BARRIER
);
5063 MEM_VOLATILE_P (mem
) = 1;
5068 /* Make sure an argument is in the right mode.
5069 EXP is the tree argument.
5070 MODE is the mode it should be in. */
5073 expand_expr_force_mode (tree exp
, enum machine_mode mode
)
5076 enum machine_mode old_mode
;
5078 val
= expand_expr (exp
, NULL_RTX
, mode
, EXPAND_NORMAL
);
5079 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5080 of CONST_INTs, where we know the old_mode only from the call argument. */
5082 old_mode
= GET_MODE (val
);
5083 if (old_mode
== VOIDmode
)
5084 old_mode
= TYPE_MODE (TREE_TYPE (exp
));
5085 val
= convert_modes (mode
, old_mode
, val
, 1);
5090 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5091 EXP is the CALL_EXPR. CODE is the rtx code
5092 that corresponds to the arithmetic or logical operation from the name;
5093 an exception here is that NOT actually means NAND. TARGET is an optional
5094 place for us to store the results; AFTER is true if this is the
5095 fetch_and_xxx form. */
5098 expand_builtin_sync_operation (enum machine_mode mode
, tree exp
,
5099 enum rtx_code code
, bool after
,
5103 location_t loc
= EXPR_LOCATION (exp
);
5105 if (code
== NOT
&& warn_sync_nand
)
5107 tree fndecl
= get_callee_fndecl (exp
);
5108 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5110 static bool warned_f_a_n
, warned_n_a_f
;
5114 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
5115 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
5116 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
5117 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
5118 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
5122 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N
);
5123 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5124 warned_f_a_n
= true;
5127 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
5128 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
5129 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
5130 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
5131 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
5135 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N
);
5136 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5137 warned_n_a_f
= true;
5145 /* Expand the operands. */
5146 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5147 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5149 return expand_atomic_fetch_op (target
, mem
, val
, code
, MEMMODEL_SEQ_CST
,
5153 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5154 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5155 true if this is the boolean form. TARGET is a place for us to store the
5156 results; this is NOT optional if IS_BOOL is true. */
5159 expand_builtin_compare_and_swap (enum machine_mode mode
, tree exp
,
5160 bool is_bool
, rtx target
)
5162 rtx old_val
, new_val
, mem
;
5165 /* Expand the operands. */
5166 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5167 old_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5168 new_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5170 pbool
= poval
= NULL
;
5171 if (target
!= const0_rtx
)
5178 if (!expand_atomic_compare_and_swap (pbool
, poval
, mem
, old_val
, new_val
,
5179 false, MEMMODEL_SEQ_CST
,
5186 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5187 general form is actually an atomic exchange, and some targets only
5188 support a reduced form with the second argument being a constant 1.
5189 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5193 expand_builtin_sync_lock_test_and_set (enum machine_mode mode
, tree exp
,
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_sync_lock_test_and_set (target
, mem
, val
);
5205 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5208 expand_builtin_sync_lock_release (enum machine_mode mode
, tree exp
)
5212 /* Expand the operands. */
5213 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5215 expand_atomic_store (mem
, const0_rtx
, MEMMODEL_RELEASE
, true);
5218 /* Given an integer representing an ``enum memmodel'', verify its
5219 correctness and return the memory model enum. */
5221 static enum memmodel
5222 get_memmodel (tree exp
)
5225 unsigned HOST_WIDE_INT val
;
5227 /* If the parameter is not a constant, it's a run time value so we'll just
5228 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5229 if (TREE_CODE (exp
) != INTEGER_CST
)
5230 return MEMMODEL_SEQ_CST
;
5232 op
= expand_normal (exp
);
5235 if (targetm
.memmodel_check
)
5236 val
= targetm
.memmodel_check (val
);
5237 else if (val
& ~MEMMODEL_MASK
)
5239 warning (OPT_Winvalid_memory_model
,
5240 "Unknown architecture specifier in memory model to builtin.");
5241 return MEMMODEL_SEQ_CST
;
5244 if ((INTVAL (op
) & MEMMODEL_MASK
) >= MEMMODEL_LAST
)
5246 warning (OPT_Winvalid_memory_model
,
5247 "invalid memory model argument to builtin");
5248 return MEMMODEL_SEQ_CST
;
5251 return (enum memmodel
) val
;
5254 /* Expand the __atomic_exchange intrinsic:
5255 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5256 EXP is the CALL_EXPR.
5257 TARGET is an optional place for us to store the results. */
5260 expand_builtin_atomic_exchange (enum machine_mode mode
, tree exp
, rtx target
)
5263 enum memmodel model
;
5265 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5266 if ((model
& MEMMODEL_MASK
) == MEMMODEL_CONSUME
)
5268 error ("invalid memory model for %<__atomic_exchange%>");
5272 if (!flag_inline_atomics
)
5275 /* Expand the operands. */
5276 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5277 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5279 return expand_atomic_exchange (target
, mem
, val
, model
);
5282 /* Expand the __atomic_compare_exchange intrinsic:
5283 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5284 TYPE desired, BOOL weak,
5285 enum memmodel success,
5286 enum memmodel failure)
5287 EXP is the CALL_EXPR.
5288 TARGET is an optional place for us to store the results. */
5291 expand_builtin_atomic_compare_exchange (enum machine_mode mode
, tree exp
,
5294 rtx expect
, desired
, mem
, oldval
;
5295 enum memmodel success
, failure
;
5299 success
= get_memmodel (CALL_EXPR_ARG (exp
, 4));
5300 failure
= get_memmodel (CALL_EXPR_ARG (exp
, 5));
5302 if ((failure
& MEMMODEL_MASK
) == MEMMODEL_RELEASE
5303 || (failure
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5305 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5309 if (failure
> success
)
5311 error ("failure memory model cannot be stronger than success "
5312 "memory model for %<__atomic_compare_exchange%>");
5316 if (!flag_inline_atomics
)
5319 /* Expand the operands. */
5320 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5322 expect
= expand_normal (CALL_EXPR_ARG (exp
, 1));
5323 expect
= convert_memory_address (Pmode
, expect
);
5324 expect
= gen_rtx_MEM (mode
, expect
);
5325 desired
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5327 weak
= CALL_EXPR_ARG (exp
, 3);
5329 if (tree_fits_shwi_p (weak
) && tree_to_shwi (weak
) != 0)
5333 if (!expand_atomic_compare_and_swap ((target
== const0_rtx
? NULL
: &target
),
5334 &oldval
, mem
, oldval
, desired
,
5335 is_weak
, success
, failure
))
5338 if (oldval
!= expect
)
5339 emit_move_insn (expect
, oldval
);
5344 /* Expand the __atomic_load intrinsic:
5345 TYPE __atomic_load (TYPE *object, enum memmodel)
5346 EXP is the CALL_EXPR.
5347 TARGET is an optional place for us to store the results. */
5350 expand_builtin_atomic_load (enum machine_mode mode
, tree exp
, rtx target
)
5353 enum memmodel model
;
5355 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5356 if ((model
& MEMMODEL_MASK
) == MEMMODEL_RELEASE
5357 || (model
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5359 error ("invalid memory model for %<__atomic_load%>");
5363 if (!flag_inline_atomics
)
5366 /* Expand the operand. */
5367 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5369 return expand_atomic_load (target
, mem
, model
);
5373 /* Expand the __atomic_store intrinsic:
5374 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5375 EXP is the CALL_EXPR.
5376 TARGET is an optional place for us to store the results. */
5379 expand_builtin_atomic_store (enum machine_mode mode
, tree exp
)
5382 enum memmodel model
;
5384 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5385 if ((model
& MEMMODEL_MASK
) != MEMMODEL_RELAXED
5386 && (model
& MEMMODEL_MASK
) != MEMMODEL_SEQ_CST
5387 && (model
& MEMMODEL_MASK
) != MEMMODEL_RELEASE
)
5389 error ("invalid memory model for %<__atomic_store%>");
5393 if (!flag_inline_atomics
)
5396 /* Expand the operands. */
5397 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5398 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5400 return expand_atomic_store (mem
, val
, model
, false);
5403 /* Expand the __atomic_fetch_XXX intrinsic:
5404 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5405 EXP is the CALL_EXPR.
5406 TARGET is an optional place for us to store the results.
5407 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5408 FETCH_AFTER is true if returning the result of the operation.
5409 FETCH_AFTER is false if returning the value before the operation.
5410 IGNORE is true if the result is not used.
5411 EXT_CALL is the correct builtin for an external call if this cannot be
5412 resolved to an instruction sequence. */
5415 expand_builtin_atomic_fetch_op (enum machine_mode mode
, tree exp
, rtx target
,
5416 enum rtx_code code
, bool fetch_after
,
5417 bool ignore
, enum built_in_function ext_call
)
5420 enum memmodel model
;
5424 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5426 /* Expand the operands. */
5427 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5428 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5430 /* Only try generating instructions if inlining is turned on. */
5431 if (flag_inline_atomics
)
5433 ret
= expand_atomic_fetch_op (target
, mem
, val
, code
, model
, fetch_after
);
5438 /* Return if a different routine isn't needed for the library call. */
5439 if (ext_call
== BUILT_IN_NONE
)
5442 /* Change the call to the specified function. */
5443 fndecl
= get_callee_fndecl (exp
);
5444 addr
= CALL_EXPR_FN (exp
);
5447 gcc_assert (TREE_OPERAND (addr
, 0) == fndecl
);
5448 TREE_OPERAND (addr
, 0) = builtin_decl_explicit (ext_call
);
5450 /* Expand the call here so we can emit trailing code. */
5451 ret
= expand_call (exp
, target
, ignore
);
5453 /* Replace the original function just in case it matters. */
5454 TREE_OPERAND (addr
, 0) = fndecl
;
5456 /* Then issue the arithmetic correction to return the right result. */
5461 ret
= expand_simple_binop (mode
, AND
, ret
, val
, NULL_RTX
, true,
5463 ret
= expand_simple_unop (mode
, NOT
, ret
, target
, true);
5466 ret
= expand_simple_binop (mode
, code
, ret
, val
, target
, true,
5473 #ifndef HAVE_atomic_clear
5474 # define HAVE_atomic_clear 0
5475 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5478 /* Expand an atomic clear operation.
5479 void _atomic_clear (BOOL *obj, enum memmodel)
5480 EXP is the call expression. */
5483 expand_builtin_atomic_clear (tree exp
)
5485 enum machine_mode mode
;
5487 enum memmodel model
;
5489 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5490 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5491 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5493 if ((model
& MEMMODEL_MASK
) == MEMMODEL_ACQUIRE
5494 || (model
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5496 error ("invalid memory model for %<__atomic_store%>");
5500 if (HAVE_atomic_clear
)
5502 emit_insn (gen_atomic_clear (mem
, model
));
5506 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5507 Failing that, a store is issued by __atomic_store. The only way this can
5508 fail is if the bool type is larger than a word size. Unlikely, but
5509 handle it anyway for completeness. Assume a single threaded model since
5510 there is no atomic support in this case, and no barriers are required. */
5511 ret
= expand_atomic_store (mem
, const0_rtx
, model
, true);
5513 emit_move_insn (mem
, const0_rtx
);
5517 /* Expand an atomic test_and_set operation.
5518 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5519 EXP is the call expression. */
5522 expand_builtin_atomic_test_and_set (tree exp
, rtx target
)
5525 enum memmodel model
;
5526 enum machine_mode mode
;
5528 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5529 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5530 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5532 return expand_atomic_test_and_set (target
, mem
, model
);
5536 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5537 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5540 fold_builtin_atomic_always_lock_free (tree arg0
, tree arg1
)
5543 enum machine_mode mode
;
5544 unsigned int mode_align
, type_align
;
5546 if (TREE_CODE (arg0
) != INTEGER_CST
)
5549 size
= INTVAL (expand_normal (arg0
)) * BITS_PER_UNIT
;
5550 mode
= mode_for_size (size
, MODE_INT
, 0);
5551 mode_align
= GET_MODE_ALIGNMENT (mode
);
5553 if (TREE_CODE (arg1
) == INTEGER_CST
&& INTVAL (expand_normal (arg1
)) == 0)
5554 type_align
= mode_align
;
5557 tree ttype
= TREE_TYPE (arg1
);
5559 /* This function is usually invoked and folded immediately by the front
5560 end before anything else has a chance to look at it. The pointer
5561 parameter at this point is usually cast to a void *, so check for that
5562 and look past the cast. */
5563 if (TREE_CODE (arg1
) == NOP_EXPR
&& POINTER_TYPE_P (ttype
)
5564 && VOID_TYPE_P (TREE_TYPE (ttype
)))
5565 arg1
= TREE_OPERAND (arg1
, 0);
5567 ttype
= TREE_TYPE (arg1
);
5568 gcc_assert (POINTER_TYPE_P (ttype
));
5570 /* Get the underlying type of the object. */
5571 ttype
= TREE_TYPE (ttype
);
5572 type_align
= TYPE_ALIGN (ttype
);
5575 /* If the object has smaller alignment, the the lock free routines cannot
5577 if (type_align
< mode_align
)
5578 return boolean_false_node
;
5580 /* Check if a compare_and_swap pattern exists for the mode which represents
5581 the required size. The pattern is not allowed to fail, so the existence
5582 of the pattern indicates support is present. */
5583 if (can_compare_and_swap_p (mode
, true))
5584 return boolean_true_node
;
5586 return boolean_false_node
;
5589 /* Return true if the parameters to call EXP represent an object which will
5590 always generate lock free instructions. The first argument represents the
5591 size of the object, and the second parameter is a pointer to the object
5592 itself. If NULL is passed for the object, then the result is based on
5593 typical alignment for an object of the specified size. Otherwise return
5597 expand_builtin_atomic_always_lock_free (tree exp
)
5600 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5601 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5603 if (TREE_CODE (arg0
) != INTEGER_CST
)
5605 error ("non-constant argument 1 to __atomic_always_lock_free");
5609 size
= fold_builtin_atomic_always_lock_free (arg0
, arg1
);
5610 if (size
== boolean_true_node
)
5615 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5616 is lock free on this architecture. */
5619 fold_builtin_atomic_is_lock_free (tree arg0
, tree arg1
)
5621 if (!flag_inline_atomics
)
5624 /* If it isn't always lock free, don't generate a result. */
5625 if (fold_builtin_atomic_always_lock_free (arg0
, arg1
) == boolean_true_node
)
5626 return boolean_true_node
;
5631 /* Return true if the parameters to call EXP represent an object which will
5632 always generate lock free instructions. The first argument represents the
5633 size of the object, and the second parameter is a pointer to the object
5634 itself. If NULL is passed for the object, then the result is based on
5635 typical alignment for an object of the specified size. Otherwise return
5639 expand_builtin_atomic_is_lock_free (tree exp
)
5642 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5643 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5645 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
5647 error ("non-integer argument 1 to __atomic_is_lock_free");
5651 if (!flag_inline_atomics
)
5654 /* If the value is known at compile time, return the RTX for it. */
5655 size
= fold_builtin_atomic_is_lock_free (arg0
, arg1
);
5656 if (size
== boolean_true_node
)
5662 /* Expand the __atomic_thread_fence intrinsic:
5663 void __atomic_thread_fence (enum memmodel)
5664 EXP is the CALL_EXPR. */
5667 expand_builtin_atomic_thread_fence (tree exp
)
5669 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5670 expand_mem_thread_fence (model
);
5673 /* Expand the __atomic_signal_fence intrinsic:
5674 void __atomic_signal_fence (enum memmodel)
5675 EXP is the CALL_EXPR. */
5678 expand_builtin_atomic_signal_fence (tree exp
)
5680 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5681 expand_mem_signal_fence (model
);
5684 /* Expand the __sync_synchronize intrinsic. */
5687 expand_builtin_sync_synchronize (void)
5689 expand_mem_thread_fence (MEMMODEL_SEQ_CST
);
5693 expand_builtin_thread_pointer (tree exp
, rtx target
)
5695 enum insn_code icode
;
5696 if (!validate_arglist (exp
, VOID_TYPE
))
5698 icode
= direct_optab_handler (get_thread_pointer_optab
, Pmode
);
5699 if (icode
!= CODE_FOR_nothing
)
5701 struct expand_operand op
;
5702 if (!REG_P (target
) || GET_MODE (target
) != Pmode
)
5703 target
= gen_reg_rtx (Pmode
);
5704 create_output_operand (&op
, target
, Pmode
);
5705 expand_insn (icode
, 1, &op
);
5708 error ("__builtin_thread_pointer is not supported on this target");
5713 expand_builtin_set_thread_pointer (tree exp
)
5715 enum insn_code icode
;
5716 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
5718 icode
= direct_optab_handler (set_thread_pointer_optab
, Pmode
);
5719 if (icode
!= CODE_FOR_nothing
)
5721 struct expand_operand op
;
5722 rtx val
= expand_expr (CALL_EXPR_ARG (exp
, 0), NULL_RTX
,
5723 Pmode
, EXPAND_NORMAL
);
5724 create_input_operand (&op
, val
, Pmode
);
5725 expand_insn (icode
, 1, &op
);
5728 error ("__builtin_set_thread_pointer is not supported on this target");
5732 /* Emit code to restore the current value of stack. */
5735 expand_stack_restore (tree var
)
5737 rtx prev
, sa
= expand_normal (var
);
5739 sa
= convert_memory_address (Pmode
, sa
);
5741 prev
= get_last_insn ();
5742 emit_stack_restore (SAVE_BLOCK
, sa
);
5743 fixup_args_size_notes (prev
, get_last_insn (), 0);
5747 /* Emit code to save the current value of stack. */
5750 expand_stack_save (void)
5754 do_pending_stack_adjust ();
5755 emit_stack_save (SAVE_BLOCK
, &ret
);
5759 /* Expand an expression EXP that calls a built-in function,
5760 with result going to TARGET if that's convenient
5761 (and in mode MODE if that's convenient).
5762 SUBTARGET may be used as the target for computing one of EXP's operands.
5763 IGNORE is nonzero if the value is to be ignored. */
5766 expand_builtin (tree exp
, rtx target
, rtx subtarget
, enum machine_mode mode
,
5769 tree fndecl
= get_callee_fndecl (exp
);
5770 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5771 enum machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
5774 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5775 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
5777 /* When not optimizing, generate calls to library functions for a certain
5780 && !called_as_built_in (fndecl
)
5781 && fcode
!= BUILT_IN_FORK
5782 && fcode
!= BUILT_IN_EXECL
5783 && fcode
!= BUILT_IN_EXECV
5784 && fcode
!= BUILT_IN_EXECLP
5785 && fcode
!= BUILT_IN_EXECLE
5786 && fcode
!= BUILT_IN_EXECVP
5787 && fcode
!= BUILT_IN_EXECVE
5788 && fcode
!= BUILT_IN_ALLOCA
5789 && fcode
!= BUILT_IN_ALLOCA_WITH_ALIGN
5790 && fcode
!= BUILT_IN_FREE
5791 && fcode
!= BUILT_IN_CHKP_SET_PTR_BOUNDS
5792 && fcode
!= BUILT_IN_CHKP_INIT_PTR_BOUNDS
5793 && fcode
!= BUILT_IN_CHKP_NULL_PTR_BOUNDS
5794 && fcode
!= BUILT_IN_CHKP_COPY_PTR_BOUNDS
5795 && fcode
!= BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5796 && fcode
!= BUILT_IN_CHKP_STORE_PTR_BOUNDS
5797 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
5798 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
5799 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_BOUNDS
5800 && fcode
!= BUILT_IN_CHKP_GET_PTR_LBOUND
5801 && fcode
!= BUILT_IN_CHKP_GET_PTR_UBOUND
)
5802 return expand_call (exp
, target
, ignore
);
5804 /* The built-in function expanders test for target == const0_rtx
5805 to determine whether the function's result will be ignored. */
5807 target
= const0_rtx
;
5809 /* If the result of a pure or const built-in function is ignored, and
5810 none of its arguments are volatile, we can avoid expanding the
5811 built-in call and just evaluate the arguments for side-effects. */
5812 if (target
== const0_rtx
5813 && ((flags
= flags_from_decl_or_type (fndecl
)) & (ECF_CONST
| ECF_PURE
))
5814 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
5816 bool volatilep
= false;
5818 call_expr_arg_iterator iter
;
5820 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5821 if (TREE_THIS_VOLATILE (arg
))
5829 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5830 expand_expr (arg
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5837 CASE_FLT_FN (BUILT_IN_FABS
):
5838 case BUILT_IN_FABSD32
:
5839 case BUILT_IN_FABSD64
:
5840 case BUILT_IN_FABSD128
:
5841 target
= expand_builtin_fabs (exp
, target
, subtarget
);
5846 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
5847 target
= expand_builtin_copysign (exp
, target
, subtarget
);
5852 /* Just do a normal library call if we were unable to fold
5854 CASE_FLT_FN (BUILT_IN_CABS
):
5857 CASE_FLT_FN (BUILT_IN_EXP
):
5858 CASE_FLT_FN (BUILT_IN_EXP10
):
5859 CASE_FLT_FN (BUILT_IN_POW10
):
5860 CASE_FLT_FN (BUILT_IN_EXP2
):
5861 CASE_FLT_FN (BUILT_IN_EXPM1
):
5862 CASE_FLT_FN (BUILT_IN_LOGB
):
5863 CASE_FLT_FN (BUILT_IN_LOG
):
5864 CASE_FLT_FN (BUILT_IN_LOG10
):
5865 CASE_FLT_FN (BUILT_IN_LOG2
):
5866 CASE_FLT_FN (BUILT_IN_LOG1P
):
5867 CASE_FLT_FN (BUILT_IN_TAN
):
5868 CASE_FLT_FN (BUILT_IN_ASIN
):
5869 CASE_FLT_FN (BUILT_IN_ACOS
):
5870 CASE_FLT_FN (BUILT_IN_ATAN
):
5871 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
5872 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5873 because of possible accuracy problems. */
5874 if (! flag_unsafe_math_optimizations
)
5876 CASE_FLT_FN (BUILT_IN_SQRT
):
5877 CASE_FLT_FN (BUILT_IN_FLOOR
):
5878 CASE_FLT_FN (BUILT_IN_CEIL
):
5879 CASE_FLT_FN (BUILT_IN_TRUNC
):
5880 CASE_FLT_FN (BUILT_IN_ROUND
):
5881 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
5882 CASE_FLT_FN (BUILT_IN_RINT
):
5883 target
= expand_builtin_mathfn (exp
, target
, subtarget
);
5888 CASE_FLT_FN (BUILT_IN_FMA
):
5889 target
= expand_builtin_mathfn_ternary (exp
, target
, subtarget
);
5894 CASE_FLT_FN (BUILT_IN_ILOGB
):
5895 if (! flag_unsafe_math_optimizations
)
5897 CASE_FLT_FN (BUILT_IN_ISINF
):
5898 CASE_FLT_FN (BUILT_IN_FINITE
):
5899 case BUILT_IN_ISFINITE
:
5900 case BUILT_IN_ISNORMAL
:
5901 target
= expand_builtin_interclass_mathfn (exp
, target
);
5906 CASE_FLT_FN (BUILT_IN_ICEIL
):
5907 CASE_FLT_FN (BUILT_IN_LCEIL
):
5908 CASE_FLT_FN (BUILT_IN_LLCEIL
):
5909 CASE_FLT_FN (BUILT_IN_LFLOOR
):
5910 CASE_FLT_FN (BUILT_IN_IFLOOR
):
5911 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
5912 target
= expand_builtin_int_roundingfn (exp
, target
);
5917 CASE_FLT_FN (BUILT_IN_IRINT
):
5918 CASE_FLT_FN (BUILT_IN_LRINT
):
5919 CASE_FLT_FN (BUILT_IN_LLRINT
):
5920 CASE_FLT_FN (BUILT_IN_IROUND
):
5921 CASE_FLT_FN (BUILT_IN_LROUND
):
5922 CASE_FLT_FN (BUILT_IN_LLROUND
):
5923 target
= expand_builtin_int_roundingfn_2 (exp
, target
);
5928 CASE_FLT_FN (BUILT_IN_POWI
):
5929 target
= expand_builtin_powi (exp
, target
);
5934 CASE_FLT_FN (BUILT_IN_ATAN2
):
5935 CASE_FLT_FN (BUILT_IN_LDEXP
):
5936 CASE_FLT_FN (BUILT_IN_SCALB
):
5937 CASE_FLT_FN (BUILT_IN_SCALBN
):
5938 CASE_FLT_FN (BUILT_IN_SCALBLN
):
5939 if (! flag_unsafe_math_optimizations
)
5942 CASE_FLT_FN (BUILT_IN_FMOD
):
5943 CASE_FLT_FN (BUILT_IN_REMAINDER
):
5944 CASE_FLT_FN (BUILT_IN_DREM
):
5945 CASE_FLT_FN (BUILT_IN_POW
):
5946 target
= expand_builtin_mathfn_2 (exp
, target
, subtarget
);
5951 CASE_FLT_FN (BUILT_IN_CEXPI
):
5952 target
= expand_builtin_cexpi (exp
, target
);
5953 gcc_assert (target
);
5956 CASE_FLT_FN (BUILT_IN_SIN
):
5957 CASE_FLT_FN (BUILT_IN_COS
):
5958 if (! flag_unsafe_math_optimizations
)
5960 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
5965 CASE_FLT_FN (BUILT_IN_SINCOS
):
5966 if (! flag_unsafe_math_optimizations
)
5968 target
= expand_builtin_sincos (exp
);
5973 case BUILT_IN_APPLY_ARGS
:
5974 return expand_builtin_apply_args ();
5976 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5977 FUNCTION with a copy of the parameters described by
5978 ARGUMENTS, and ARGSIZE. It returns a block of memory
5979 allocated on the stack into which is stored all the registers
5980 that might possibly be used for returning the result of a
5981 function. ARGUMENTS is the value returned by
5982 __builtin_apply_args. ARGSIZE is the number of bytes of
5983 arguments that must be copied. ??? How should this value be
5984 computed? We'll also need a safe worst case value for varargs
5986 case BUILT_IN_APPLY
:
5987 if (!validate_arglist (exp
, POINTER_TYPE
,
5988 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
5989 && !validate_arglist (exp
, REFERENCE_TYPE
,
5990 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
5996 ops
[0] = expand_normal (CALL_EXPR_ARG (exp
, 0));
5997 ops
[1] = expand_normal (CALL_EXPR_ARG (exp
, 1));
5998 ops
[2] = expand_normal (CALL_EXPR_ARG (exp
, 2));
6000 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
6003 /* __builtin_return (RESULT) causes the function to return the
6004 value described by RESULT. RESULT is address of the block of
6005 memory returned by __builtin_apply. */
6006 case BUILT_IN_RETURN
:
6007 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6008 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp
, 0)));
6011 case BUILT_IN_SAVEREGS
:
6012 return expand_builtin_saveregs ();
6014 case BUILT_IN_VA_ARG_PACK
:
6015 /* All valid uses of __builtin_va_arg_pack () are removed during
6017 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp
);
6020 case BUILT_IN_VA_ARG_PACK_LEN
:
6021 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6023 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp
);
6026 /* Return the address of the first anonymous stack arg. */
6027 case BUILT_IN_NEXT_ARG
:
6028 if (fold_builtin_next_arg (exp
, false))
6030 return expand_builtin_next_arg ();
6032 case BUILT_IN_CLEAR_CACHE
:
6033 target
= expand_builtin___clear_cache (exp
);
6038 case BUILT_IN_CLASSIFY_TYPE
:
6039 return expand_builtin_classify_type (exp
);
6041 case BUILT_IN_CONSTANT_P
:
6044 case BUILT_IN_FRAME_ADDRESS
:
6045 case BUILT_IN_RETURN_ADDRESS
:
6046 return expand_builtin_frame_address (fndecl
, exp
);
6048 /* Returns the address of the area where the structure is returned.
6050 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
6051 if (call_expr_nargs (exp
) != 0
6052 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
6053 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
6056 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
6058 case BUILT_IN_ALLOCA
:
6059 case BUILT_IN_ALLOCA_WITH_ALIGN
:
6060 /* If the allocation stems from the declaration of a variable-sized
6061 object, it cannot accumulate. */
6062 target
= expand_builtin_alloca (exp
, CALL_ALLOCA_FOR_VAR_P (exp
));
6067 case BUILT_IN_STACK_SAVE
:
6068 return expand_stack_save ();
6070 case BUILT_IN_STACK_RESTORE
:
6071 expand_stack_restore (CALL_EXPR_ARG (exp
, 0));
6074 case BUILT_IN_BSWAP16
:
6075 case BUILT_IN_BSWAP32
:
6076 case BUILT_IN_BSWAP64
:
6077 target
= expand_builtin_bswap (target_mode
, exp
, target
, subtarget
);
6082 CASE_INT_FN (BUILT_IN_FFS
):
6083 target
= expand_builtin_unop (target_mode
, exp
, target
,
6084 subtarget
, ffs_optab
);
6089 CASE_INT_FN (BUILT_IN_CLZ
):
6090 target
= expand_builtin_unop (target_mode
, exp
, target
,
6091 subtarget
, clz_optab
);
6096 CASE_INT_FN (BUILT_IN_CTZ
):
6097 target
= expand_builtin_unop (target_mode
, exp
, target
,
6098 subtarget
, ctz_optab
);
6103 CASE_INT_FN (BUILT_IN_CLRSB
):
6104 target
= expand_builtin_unop (target_mode
, exp
, target
,
6105 subtarget
, clrsb_optab
);
6110 CASE_INT_FN (BUILT_IN_POPCOUNT
):
6111 target
= expand_builtin_unop (target_mode
, exp
, target
,
6112 subtarget
, popcount_optab
);
6117 CASE_INT_FN (BUILT_IN_PARITY
):
6118 target
= expand_builtin_unop (target_mode
, exp
, target
,
6119 subtarget
, parity_optab
);
6124 case BUILT_IN_STRLEN
:
6125 target
= expand_builtin_strlen (exp
, target
, target_mode
);
6130 case BUILT_IN_STRCPY
:
6131 target
= expand_builtin_strcpy (exp
, target
);
6136 case BUILT_IN_STRNCPY
:
6137 target
= expand_builtin_strncpy (exp
, target
);
6142 case BUILT_IN_STPCPY
:
6143 target
= expand_builtin_stpcpy (exp
, target
, mode
);
6148 case BUILT_IN_MEMCPY
:
6149 target
= expand_builtin_memcpy (exp
, target
);
6154 case BUILT_IN_MEMPCPY
:
6155 target
= expand_builtin_mempcpy (exp
, target
, mode
);
6160 case BUILT_IN_MEMSET
:
6161 target
= expand_builtin_memset (exp
, target
, mode
);
6166 case BUILT_IN_BZERO
:
6167 target
= expand_builtin_bzero (exp
);
6172 case BUILT_IN_STRCMP
:
6173 target
= expand_builtin_strcmp (exp
, target
);
6178 case BUILT_IN_STRNCMP
:
6179 target
= expand_builtin_strncmp (exp
, target
, mode
);
6185 case BUILT_IN_MEMCMP
:
6186 target
= expand_builtin_memcmp (exp
, target
, mode
);
6191 case BUILT_IN_SETJMP
:
6192 /* This should have been lowered to the builtins below. */
6195 case BUILT_IN_SETJMP_SETUP
:
6196 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6197 and the receiver label. */
6198 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
6200 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6201 VOIDmode
, EXPAND_NORMAL
);
6202 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 1), 0);
6203 rtx label_r
= label_rtx (label
);
6205 /* This is copied from the handling of non-local gotos. */
6206 expand_builtin_setjmp_setup (buf_addr
, label_r
);
6207 nonlocal_goto_handler_labels
6208 = gen_rtx_EXPR_LIST (VOIDmode
, label_r
,
6209 nonlocal_goto_handler_labels
);
6210 /* ??? Do not let expand_label treat us as such since we would
6211 not want to be both on the list of non-local labels and on
6212 the list of forced labels. */
6213 FORCED_LABEL (label
) = 0;
6218 case BUILT_IN_SETJMP_DISPATCHER
:
6219 /* __builtin_setjmp_dispatcher is passed the dispatcher label. */
6220 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6222 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 0), 0);
6223 rtx label_r
= label_rtx (label
);
6225 /* Remove the dispatcher label from the list of non-local labels
6226 since the receiver labels have been added to it above. */
6227 remove_node_from_expr_list (label_r
, &nonlocal_goto_handler_labels
);
6232 case BUILT_IN_SETJMP_RECEIVER
:
6233 /* __builtin_setjmp_receiver is passed the receiver label. */
6234 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6236 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 0), 0);
6237 rtx label_r
= label_rtx (label
);
6239 expand_builtin_setjmp_receiver (label_r
);
6244 /* __builtin_longjmp is passed a pointer to an array of five words.
6245 It's similar to the C library longjmp function but works with
6246 __builtin_setjmp above. */
6247 case BUILT_IN_LONGJMP
:
6248 if (validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6250 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6251 VOIDmode
, EXPAND_NORMAL
);
6252 rtx value
= expand_normal (CALL_EXPR_ARG (exp
, 1));
6254 if (value
!= const1_rtx
)
6256 error ("%<__builtin_longjmp%> second argument must be 1");
6260 expand_builtin_longjmp (buf_addr
, value
);
6265 case BUILT_IN_NONLOCAL_GOTO
:
6266 target
= expand_builtin_nonlocal_goto (exp
);
6271 /* This updates the setjmp buffer that is its argument with the value
6272 of the current stack pointer. */
6273 case BUILT_IN_UPDATE_SETJMP_BUF
:
6274 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6277 = expand_normal (CALL_EXPR_ARG (exp
, 0));
6279 expand_builtin_update_setjmp_buf (buf_addr
);
6285 expand_builtin_trap ();
6288 case BUILT_IN_UNREACHABLE
:
6289 expand_builtin_unreachable ();
6292 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
6293 case BUILT_IN_SIGNBITD32
:
6294 case BUILT_IN_SIGNBITD64
:
6295 case BUILT_IN_SIGNBITD128
:
6296 target
= expand_builtin_signbit (exp
, target
);
6301 /* Various hooks for the DWARF 2 __throw routine. */
6302 case BUILT_IN_UNWIND_INIT
:
6303 expand_builtin_unwind_init ();
6305 case BUILT_IN_DWARF_CFA
:
6306 return virtual_cfa_rtx
;
6307 #ifdef DWARF2_UNWIND_INFO
6308 case BUILT_IN_DWARF_SP_COLUMN
:
6309 return expand_builtin_dwarf_sp_column ();
6310 case BUILT_IN_INIT_DWARF_REG_SIZES
:
6311 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp
, 0));
6314 case BUILT_IN_FROB_RETURN_ADDR
:
6315 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp
, 0));
6316 case BUILT_IN_EXTRACT_RETURN_ADDR
:
6317 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp
, 0));
6318 case BUILT_IN_EH_RETURN
:
6319 expand_builtin_eh_return (CALL_EXPR_ARG (exp
, 0),
6320 CALL_EXPR_ARG (exp
, 1));
6322 #ifdef EH_RETURN_DATA_REGNO
6323 case BUILT_IN_EH_RETURN_DATA_REGNO
:
6324 return expand_builtin_eh_return_data_regno (exp
);
6326 case BUILT_IN_EXTEND_POINTER
:
6327 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp
, 0));
6328 case BUILT_IN_EH_POINTER
:
6329 return expand_builtin_eh_pointer (exp
);
6330 case BUILT_IN_EH_FILTER
:
6331 return expand_builtin_eh_filter (exp
);
6332 case BUILT_IN_EH_COPY_VALUES
:
6333 return expand_builtin_eh_copy_values (exp
);
6335 case BUILT_IN_VA_START
:
6336 return expand_builtin_va_start (exp
);
6337 case BUILT_IN_VA_END
:
6338 return expand_builtin_va_end (exp
);
6339 case BUILT_IN_VA_COPY
:
6340 return expand_builtin_va_copy (exp
);
6341 case BUILT_IN_EXPECT
:
6342 return expand_builtin_expect (exp
, target
);
6343 case BUILT_IN_ASSUME_ALIGNED
:
6344 return expand_builtin_assume_aligned (exp
, target
);
6345 case BUILT_IN_PREFETCH
:
6346 expand_builtin_prefetch (exp
);
6349 case BUILT_IN_INIT_TRAMPOLINE
:
6350 return expand_builtin_init_trampoline (exp
, true);
6351 case BUILT_IN_INIT_HEAP_TRAMPOLINE
:
6352 return expand_builtin_init_trampoline (exp
, false);
6353 case BUILT_IN_ADJUST_TRAMPOLINE
:
6354 return expand_builtin_adjust_trampoline (exp
);
6357 case BUILT_IN_EXECL
:
6358 case BUILT_IN_EXECV
:
6359 case BUILT_IN_EXECLP
:
6360 case BUILT_IN_EXECLE
:
6361 case BUILT_IN_EXECVP
:
6362 case BUILT_IN_EXECVE
:
6363 target
= expand_builtin_fork_or_exec (fndecl
, exp
, target
, ignore
);
6368 case BUILT_IN_SYNC_FETCH_AND_ADD_1
:
6369 case BUILT_IN_SYNC_FETCH_AND_ADD_2
:
6370 case BUILT_IN_SYNC_FETCH_AND_ADD_4
:
6371 case BUILT_IN_SYNC_FETCH_AND_ADD_8
:
6372 case BUILT_IN_SYNC_FETCH_AND_ADD_16
:
6373 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_ADD_1
);
6374 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, false, target
);
6379 case BUILT_IN_SYNC_FETCH_AND_SUB_1
:
6380 case BUILT_IN_SYNC_FETCH_AND_SUB_2
:
6381 case BUILT_IN_SYNC_FETCH_AND_SUB_4
:
6382 case BUILT_IN_SYNC_FETCH_AND_SUB_8
:
6383 case BUILT_IN_SYNC_FETCH_AND_SUB_16
:
6384 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_SUB_1
);
6385 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, false, target
);
6390 case BUILT_IN_SYNC_FETCH_AND_OR_1
:
6391 case BUILT_IN_SYNC_FETCH_AND_OR_2
:
6392 case BUILT_IN_SYNC_FETCH_AND_OR_4
:
6393 case BUILT_IN_SYNC_FETCH_AND_OR_8
:
6394 case BUILT_IN_SYNC_FETCH_AND_OR_16
:
6395 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_OR_1
);
6396 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, false, target
);
6401 case BUILT_IN_SYNC_FETCH_AND_AND_1
:
6402 case BUILT_IN_SYNC_FETCH_AND_AND_2
:
6403 case BUILT_IN_SYNC_FETCH_AND_AND_4
:
6404 case BUILT_IN_SYNC_FETCH_AND_AND_8
:
6405 case BUILT_IN_SYNC_FETCH_AND_AND_16
:
6406 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_AND_1
);
6407 target
= expand_builtin_sync_operation (mode
, exp
, AND
, false, target
);
6412 case BUILT_IN_SYNC_FETCH_AND_XOR_1
:
6413 case BUILT_IN_SYNC_FETCH_AND_XOR_2
:
6414 case BUILT_IN_SYNC_FETCH_AND_XOR_4
:
6415 case BUILT_IN_SYNC_FETCH_AND_XOR_8
:
6416 case BUILT_IN_SYNC_FETCH_AND_XOR_16
:
6417 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_XOR_1
);
6418 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, false, target
);
6423 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
6424 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
6425 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
6426 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
6427 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
6428 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_NAND_1
);
6429 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, false, target
);
6434 case BUILT_IN_SYNC_ADD_AND_FETCH_1
:
6435 case BUILT_IN_SYNC_ADD_AND_FETCH_2
:
6436 case BUILT_IN_SYNC_ADD_AND_FETCH_4
:
6437 case BUILT_IN_SYNC_ADD_AND_FETCH_8
:
6438 case BUILT_IN_SYNC_ADD_AND_FETCH_16
:
6439 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_ADD_AND_FETCH_1
);
6440 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, true, target
);
6445 case BUILT_IN_SYNC_SUB_AND_FETCH_1
:
6446 case BUILT_IN_SYNC_SUB_AND_FETCH_2
:
6447 case BUILT_IN_SYNC_SUB_AND_FETCH_4
:
6448 case BUILT_IN_SYNC_SUB_AND_FETCH_8
:
6449 case BUILT_IN_SYNC_SUB_AND_FETCH_16
:
6450 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_SUB_AND_FETCH_1
);
6451 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, true, target
);
6456 case BUILT_IN_SYNC_OR_AND_FETCH_1
:
6457 case BUILT_IN_SYNC_OR_AND_FETCH_2
:
6458 case BUILT_IN_SYNC_OR_AND_FETCH_4
:
6459 case BUILT_IN_SYNC_OR_AND_FETCH_8
:
6460 case BUILT_IN_SYNC_OR_AND_FETCH_16
:
6461 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_OR_AND_FETCH_1
);
6462 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, true, target
);
6467 case BUILT_IN_SYNC_AND_AND_FETCH_1
:
6468 case BUILT_IN_SYNC_AND_AND_FETCH_2
:
6469 case BUILT_IN_SYNC_AND_AND_FETCH_4
:
6470 case BUILT_IN_SYNC_AND_AND_FETCH_8
:
6471 case BUILT_IN_SYNC_AND_AND_FETCH_16
:
6472 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_AND_AND_FETCH_1
);
6473 target
= expand_builtin_sync_operation (mode
, exp
, AND
, true, target
);
6478 case BUILT_IN_SYNC_XOR_AND_FETCH_1
:
6479 case BUILT_IN_SYNC_XOR_AND_FETCH_2
:
6480 case BUILT_IN_SYNC_XOR_AND_FETCH_4
:
6481 case BUILT_IN_SYNC_XOR_AND_FETCH_8
:
6482 case BUILT_IN_SYNC_XOR_AND_FETCH_16
:
6483 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_XOR_AND_FETCH_1
);
6484 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, true, target
);
6489 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
6490 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
6491 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
6492 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
6493 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
6494 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_NAND_AND_FETCH_1
);
6495 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, true, target
);
6500 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
6501 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
6502 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
6503 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
6504 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
6505 if (mode
== VOIDmode
)
6506 mode
= TYPE_MODE (boolean_type_node
);
6507 if (!target
|| !register_operand (target
, mode
))
6508 target
= gen_reg_rtx (mode
);
6510 mode
= get_builtin_sync_mode
6511 (fcode
- BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
);
6512 target
= expand_builtin_compare_and_swap (mode
, exp
, true, target
);
6517 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
:
6518 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2
:
6519 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4
:
6520 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8
:
6521 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16
:
6522 mode
= get_builtin_sync_mode
6523 (fcode
- BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
);
6524 target
= expand_builtin_compare_and_swap (mode
, exp
, false, target
);
6529 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
:
6530 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2
:
6531 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4
:
6532 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8
:
6533 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16
:
6534 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
);
6535 target
= expand_builtin_sync_lock_test_and_set (mode
, exp
, target
);
6540 case BUILT_IN_SYNC_LOCK_RELEASE_1
:
6541 case BUILT_IN_SYNC_LOCK_RELEASE_2
:
6542 case BUILT_IN_SYNC_LOCK_RELEASE_4
:
6543 case BUILT_IN_SYNC_LOCK_RELEASE_8
:
6544 case BUILT_IN_SYNC_LOCK_RELEASE_16
:
6545 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_RELEASE_1
);
6546 expand_builtin_sync_lock_release (mode
, exp
);
6549 case BUILT_IN_SYNC_SYNCHRONIZE
:
6550 expand_builtin_sync_synchronize ();
6553 case BUILT_IN_ATOMIC_EXCHANGE_1
:
6554 case BUILT_IN_ATOMIC_EXCHANGE_2
:
6555 case BUILT_IN_ATOMIC_EXCHANGE_4
:
6556 case BUILT_IN_ATOMIC_EXCHANGE_8
:
6557 case BUILT_IN_ATOMIC_EXCHANGE_16
:
6558 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_EXCHANGE_1
);
6559 target
= expand_builtin_atomic_exchange (mode
, exp
, target
);
6564 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
6565 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
6566 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
6567 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
6568 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
6570 unsigned int nargs
, z
;
6571 vec
<tree
, va_gc
> *vec
;
6574 get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
);
6575 target
= expand_builtin_atomic_compare_exchange (mode
, exp
, target
);
6579 /* If this is turned into an external library call, the weak parameter
6580 must be dropped to match the expected parameter list. */
6581 nargs
= call_expr_nargs (exp
);
6582 vec_alloc (vec
, nargs
- 1);
6583 for (z
= 0; z
< 3; z
++)
6584 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
6585 /* Skip the boolean weak parameter. */
6586 for (z
= 4; z
< 6; z
++)
6587 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
6588 exp
= build_call_vec (TREE_TYPE (exp
), CALL_EXPR_FN (exp
), vec
);
6592 case BUILT_IN_ATOMIC_LOAD_1
:
6593 case BUILT_IN_ATOMIC_LOAD_2
:
6594 case BUILT_IN_ATOMIC_LOAD_4
:
6595 case BUILT_IN_ATOMIC_LOAD_8
:
6596 case BUILT_IN_ATOMIC_LOAD_16
:
6597 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_LOAD_1
);
6598 target
= expand_builtin_atomic_load (mode
, exp
, target
);
6603 case BUILT_IN_ATOMIC_STORE_1
:
6604 case BUILT_IN_ATOMIC_STORE_2
:
6605 case BUILT_IN_ATOMIC_STORE_4
:
6606 case BUILT_IN_ATOMIC_STORE_8
:
6607 case BUILT_IN_ATOMIC_STORE_16
:
6608 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_STORE_1
);
6609 target
= expand_builtin_atomic_store (mode
, exp
);
6614 case BUILT_IN_ATOMIC_ADD_FETCH_1
:
6615 case BUILT_IN_ATOMIC_ADD_FETCH_2
:
6616 case BUILT_IN_ATOMIC_ADD_FETCH_4
:
6617 case BUILT_IN_ATOMIC_ADD_FETCH_8
:
6618 case BUILT_IN_ATOMIC_ADD_FETCH_16
:
6620 enum built_in_function lib
;
6621 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
);
6622 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_ADD_1
+
6623 (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
));
6624 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, true,
6630 case BUILT_IN_ATOMIC_SUB_FETCH_1
:
6631 case BUILT_IN_ATOMIC_SUB_FETCH_2
:
6632 case BUILT_IN_ATOMIC_SUB_FETCH_4
:
6633 case BUILT_IN_ATOMIC_SUB_FETCH_8
:
6634 case BUILT_IN_ATOMIC_SUB_FETCH_16
:
6636 enum built_in_function lib
;
6637 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
);
6638 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_SUB_1
+
6639 (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
));
6640 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, true,
6646 case BUILT_IN_ATOMIC_AND_FETCH_1
:
6647 case BUILT_IN_ATOMIC_AND_FETCH_2
:
6648 case BUILT_IN_ATOMIC_AND_FETCH_4
:
6649 case BUILT_IN_ATOMIC_AND_FETCH_8
:
6650 case BUILT_IN_ATOMIC_AND_FETCH_16
:
6652 enum built_in_function lib
;
6653 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
);
6654 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_AND_1
+
6655 (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
));
6656 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, true,
6662 case BUILT_IN_ATOMIC_NAND_FETCH_1
:
6663 case BUILT_IN_ATOMIC_NAND_FETCH_2
:
6664 case BUILT_IN_ATOMIC_NAND_FETCH_4
:
6665 case BUILT_IN_ATOMIC_NAND_FETCH_8
:
6666 case BUILT_IN_ATOMIC_NAND_FETCH_16
:
6668 enum built_in_function lib
;
6669 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
);
6670 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_NAND_1
+
6671 (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
));
6672 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, true,
6678 case BUILT_IN_ATOMIC_XOR_FETCH_1
:
6679 case BUILT_IN_ATOMIC_XOR_FETCH_2
:
6680 case BUILT_IN_ATOMIC_XOR_FETCH_4
:
6681 case BUILT_IN_ATOMIC_XOR_FETCH_8
:
6682 case BUILT_IN_ATOMIC_XOR_FETCH_16
:
6684 enum built_in_function lib
;
6685 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
);
6686 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_XOR_1
+
6687 (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
));
6688 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, true,
6694 case BUILT_IN_ATOMIC_OR_FETCH_1
:
6695 case BUILT_IN_ATOMIC_OR_FETCH_2
:
6696 case BUILT_IN_ATOMIC_OR_FETCH_4
:
6697 case BUILT_IN_ATOMIC_OR_FETCH_8
:
6698 case BUILT_IN_ATOMIC_OR_FETCH_16
:
6700 enum built_in_function lib
;
6701 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
);
6702 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_OR_1
+
6703 (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
));
6704 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, true,
6710 case BUILT_IN_ATOMIC_FETCH_ADD_1
:
6711 case BUILT_IN_ATOMIC_FETCH_ADD_2
:
6712 case BUILT_IN_ATOMIC_FETCH_ADD_4
:
6713 case BUILT_IN_ATOMIC_FETCH_ADD_8
:
6714 case BUILT_IN_ATOMIC_FETCH_ADD_16
:
6715 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_ADD_1
);
6716 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, false,
6717 ignore
, BUILT_IN_NONE
);
6722 case BUILT_IN_ATOMIC_FETCH_SUB_1
:
6723 case BUILT_IN_ATOMIC_FETCH_SUB_2
:
6724 case BUILT_IN_ATOMIC_FETCH_SUB_4
:
6725 case BUILT_IN_ATOMIC_FETCH_SUB_8
:
6726 case BUILT_IN_ATOMIC_FETCH_SUB_16
:
6727 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_SUB_1
);
6728 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, false,
6729 ignore
, BUILT_IN_NONE
);
6734 case BUILT_IN_ATOMIC_FETCH_AND_1
:
6735 case BUILT_IN_ATOMIC_FETCH_AND_2
:
6736 case BUILT_IN_ATOMIC_FETCH_AND_4
:
6737 case BUILT_IN_ATOMIC_FETCH_AND_8
:
6738 case BUILT_IN_ATOMIC_FETCH_AND_16
:
6739 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_AND_1
);
6740 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, false,
6741 ignore
, BUILT_IN_NONE
);
6746 case BUILT_IN_ATOMIC_FETCH_NAND_1
:
6747 case BUILT_IN_ATOMIC_FETCH_NAND_2
:
6748 case BUILT_IN_ATOMIC_FETCH_NAND_4
:
6749 case BUILT_IN_ATOMIC_FETCH_NAND_8
:
6750 case BUILT_IN_ATOMIC_FETCH_NAND_16
:
6751 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_NAND_1
);
6752 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, false,
6753 ignore
, BUILT_IN_NONE
);
6758 case BUILT_IN_ATOMIC_FETCH_XOR_1
:
6759 case BUILT_IN_ATOMIC_FETCH_XOR_2
:
6760 case BUILT_IN_ATOMIC_FETCH_XOR_4
:
6761 case BUILT_IN_ATOMIC_FETCH_XOR_8
:
6762 case BUILT_IN_ATOMIC_FETCH_XOR_16
:
6763 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_XOR_1
);
6764 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, false,
6765 ignore
, BUILT_IN_NONE
);
6770 case BUILT_IN_ATOMIC_FETCH_OR_1
:
6771 case BUILT_IN_ATOMIC_FETCH_OR_2
:
6772 case BUILT_IN_ATOMIC_FETCH_OR_4
:
6773 case BUILT_IN_ATOMIC_FETCH_OR_8
:
6774 case BUILT_IN_ATOMIC_FETCH_OR_16
:
6775 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_OR_1
);
6776 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, false,
6777 ignore
, BUILT_IN_NONE
);
6782 case BUILT_IN_ATOMIC_TEST_AND_SET
:
6783 return expand_builtin_atomic_test_and_set (exp
, target
);
6785 case BUILT_IN_ATOMIC_CLEAR
:
6786 return expand_builtin_atomic_clear (exp
);
6788 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
6789 return expand_builtin_atomic_always_lock_free (exp
);
6791 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
6792 target
= expand_builtin_atomic_is_lock_free (exp
);
6797 case BUILT_IN_ATOMIC_THREAD_FENCE
:
6798 expand_builtin_atomic_thread_fence (exp
);
6801 case BUILT_IN_ATOMIC_SIGNAL_FENCE
:
6802 expand_builtin_atomic_signal_fence (exp
);
6805 case BUILT_IN_OBJECT_SIZE
:
6806 return expand_builtin_object_size (exp
);
6808 case BUILT_IN_MEMCPY_CHK
:
6809 case BUILT_IN_MEMPCPY_CHK
:
6810 case BUILT_IN_MEMMOVE_CHK
:
6811 case BUILT_IN_MEMSET_CHK
:
6812 target
= expand_builtin_memory_chk (exp
, target
, mode
, fcode
);
6817 case BUILT_IN_STRCPY_CHK
:
6818 case BUILT_IN_STPCPY_CHK
:
6819 case BUILT_IN_STRNCPY_CHK
:
6820 case BUILT_IN_STPNCPY_CHK
:
6821 case BUILT_IN_STRCAT_CHK
:
6822 case BUILT_IN_STRNCAT_CHK
:
6823 case BUILT_IN_SNPRINTF_CHK
:
6824 case BUILT_IN_VSNPRINTF_CHK
:
6825 maybe_emit_chk_warning (exp
, fcode
);
6828 case BUILT_IN_SPRINTF_CHK
:
6829 case BUILT_IN_VSPRINTF_CHK
:
6830 maybe_emit_sprintf_chk_warning (exp
, fcode
);
6834 if (warn_free_nonheap_object
)
6835 maybe_emit_free_warning (exp
);
6838 case BUILT_IN_THREAD_POINTER
:
6839 return expand_builtin_thread_pointer (exp
, target
);
6841 case BUILT_IN_SET_THREAD_POINTER
:
6842 expand_builtin_set_thread_pointer (exp
);
6845 case BUILT_IN_CILK_DETACH
:
6846 expand_builtin_cilk_detach (exp
);
6849 case BUILT_IN_CILK_POP_FRAME
:
6850 expand_builtin_cilk_pop_frame (exp
);
6853 case BUILT_IN_CHKP_INIT_PTR_BOUNDS
:
6854 case BUILT_IN_CHKP_NULL_PTR_BOUNDS
:
6855 case BUILT_IN_CHKP_COPY_PTR_BOUNDS
:
6856 return expand_normal (CALL_EXPR_ARG (exp
, 0));
6858 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
:
6859 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
:
6860 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS
:
6861 case BUILT_IN_CHKP_SET_PTR_BOUNDS
:
6862 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS
:
6863 case BUILT_IN_CHKP_STORE_PTR_BOUNDS
:
6864 case BUILT_IN_CHKP_GET_PTR_LBOUND
:
6865 case BUILT_IN_CHKP_GET_PTR_UBOUND
:
6866 /* We allow user CHKP builtins if Pointer Bounds
6868 if (!flag_check_pointer_bounds
)
6870 if (fcode
== BUILT_IN_CHKP_SET_PTR_BOUNDS
6871 || fcode
== BUILT_IN_CHKP_NARROW_PTR_BOUNDS
)
6872 return expand_normal (CALL_EXPR_ARG (exp
, 0));
6873 else if (fcode
== BUILT_IN_CHKP_GET_PTR_LBOUND
)
6874 return expand_normal (size_zero_node
);
6875 else if (fcode
== BUILT_IN_CHKP_GET_PTR_UBOUND
)
6876 return expand_normal (size_int (-1));
6882 case BUILT_IN_CHKP_BNDMK
:
6883 case BUILT_IN_CHKP_BNDSTX
:
6884 case BUILT_IN_CHKP_BNDCL
:
6885 case BUILT_IN_CHKP_BNDCU
:
6886 case BUILT_IN_CHKP_BNDLDX
:
6887 case BUILT_IN_CHKP_BNDRET
:
6888 case BUILT_IN_CHKP_INTERSECT
:
6889 case BUILT_IN_CHKP_ARG_BND
:
6890 case BUILT_IN_CHKP_NARROW
:
6891 case BUILT_IN_CHKP_EXTRACT_LOWER
:
6892 case BUILT_IN_CHKP_EXTRACT_UPPER
:
6893 /* Software implementation of pointers checker is NYI.
6894 Target support is required. */
6895 error ("Your target platform does not support -fcheck-pointers");
6898 default: /* just do library call, if unknown builtin */
6902 /* The switch statement above can drop through to cause the function
6903 to be called normally. */
6904 return expand_call (exp
, target
, ignore
);
6907 /* Determine whether a tree node represents a call to a built-in
6908 function. If the tree T is a call to a built-in function with
6909 the right number of arguments of the appropriate types, return
6910 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6911 Otherwise the return value is END_BUILTINS. */
6913 enum built_in_function
6914 builtin_mathfn_code (const_tree t
)
6916 const_tree fndecl
, arg
, parmlist
;
6917 const_tree argtype
, parmtype
;
6918 const_call_expr_arg_iterator iter
;
6920 if (TREE_CODE (t
) != CALL_EXPR
6921 || TREE_CODE (CALL_EXPR_FN (t
)) != ADDR_EXPR
)
6922 return END_BUILTINS
;
6924 fndecl
= get_callee_fndecl (t
);
6925 if (fndecl
== NULL_TREE
6926 || TREE_CODE (fndecl
) != FUNCTION_DECL
6927 || ! DECL_BUILT_IN (fndecl
)
6928 || DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
6929 return END_BUILTINS
;
6931 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
6932 init_const_call_expr_arg_iterator (t
, &iter
);
6933 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
6935 /* If a function doesn't take a variable number of arguments,
6936 the last element in the list will have type `void'. */
6937 parmtype
= TREE_VALUE (parmlist
);
6938 if (VOID_TYPE_P (parmtype
))
6940 if (more_const_call_expr_args_p (&iter
))
6941 return END_BUILTINS
;
6942 return DECL_FUNCTION_CODE (fndecl
);
6945 if (! more_const_call_expr_args_p (&iter
))
6946 return END_BUILTINS
;
6948 arg
= next_const_call_expr_arg (&iter
);
6949 argtype
= TREE_TYPE (arg
);
6951 if (SCALAR_FLOAT_TYPE_P (parmtype
))
6953 if (! SCALAR_FLOAT_TYPE_P (argtype
))
6954 return END_BUILTINS
;
6956 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
6958 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
6959 return END_BUILTINS
;
6961 else if (POINTER_TYPE_P (parmtype
))
6963 if (! POINTER_TYPE_P (argtype
))
6964 return END_BUILTINS
;
6966 else if (INTEGRAL_TYPE_P (parmtype
))
6968 if (! INTEGRAL_TYPE_P (argtype
))
6969 return END_BUILTINS
;
6972 return END_BUILTINS
;
6975 /* Variable-length argument list. */
6976 return DECL_FUNCTION_CODE (fndecl
);
6979 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6980 evaluate to a constant. */
6983 fold_builtin_constant_p (tree arg
)
6985 /* We return 1 for a numeric type that's known to be a constant
6986 value at compile-time or for an aggregate type that's a
6987 literal constant. */
6990 /* If we know this is a constant, emit the constant of one. */
6991 if (CONSTANT_CLASS_P (arg
)
6992 || (TREE_CODE (arg
) == CONSTRUCTOR
6993 && TREE_CONSTANT (arg
)))
6994 return integer_one_node
;
6995 if (TREE_CODE (arg
) == ADDR_EXPR
)
6997 tree op
= TREE_OPERAND (arg
, 0);
6998 if (TREE_CODE (op
) == STRING_CST
6999 || (TREE_CODE (op
) == ARRAY_REF
7000 && integer_zerop (TREE_OPERAND (op
, 1))
7001 && TREE_CODE (TREE_OPERAND (op
, 0)) == STRING_CST
))
7002 return integer_one_node
;
7005 /* If this expression has side effects, show we don't know it to be a
7006 constant. Likewise if it's a pointer or aggregate type since in
7007 those case we only want literals, since those are only optimized
7008 when generating RTL, not later.
7009 And finally, if we are compiling an initializer, not code, we
7010 need to return a definite result now; there's not going to be any
7011 more optimization done. */
7012 if (TREE_SIDE_EFFECTS (arg
)
7013 || AGGREGATE_TYPE_P (TREE_TYPE (arg
))
7014 || POINTER_TYPE_P (TREE_TYPE (arg
))
7016 || folding_initializer
7017 || force_folding_builtin_constant_p
)
7018 return integer_zero_node
;
7023 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7024 return it as a truthvalue. */
7027 build_builtin_expect_predicate (location_t loc
, tree pred
, tree expected
)
7029 tree fn
, arg_types
, pred_type
, expected_type
, call_expr
, ret_type
;
7031 fn
= builtin_decl_explicit (BUILT_IN_EXPECT
);
7032 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
7033 ret_type
= TREE_TYPE (TREE_TYPE (fn
));
7034 pred_type
= TREE_VALUE (arg_types
);
7035 expected_type
= TREE_VALUE (TREE_CHAIN (arg_types
));
7037 pred
= fold_convert_loc (loc
, pred_type
, pred
);
7038 expected
= fold_convert_loc (loc
, expected_type
, expected
);
7039 call_expr
= build_call_expr_loc (loc
, fn
, 2, pred
, expected
);
7041 return build2 (NE_EXPR
, TREE_TYPE (pred
), call_expr
,
7042 build_int_cst (ret_type
, 0));
7045 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7046 NULL_TREE if no simplification is possible. */
7049 fold_builtin_expect (location_t loc
, tree arg0
, tree arg1
)
7051 tree inner
, fndecl
, inner_arg0
;
7052 enum tree_code code
;
7054 /* Distribute the expected value over short-circuiting operators.
7055 See through the cast from truthvalue_type_node to long. */
7057 while (TREE_CODE (inner_arg0
) == NOP_EXPR
7058 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0
))
7059 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0
, 0))))
7060 inner_arg0
= TREE_OPERAND (inner_arg0
, 0);
7062 /* If this is a builtin_expect within a builtin_expect keep the
7063 inner one. See through a comparison against a constant. It
7064 might have been added to create a thruthvalue. */
7067 if (COMPARISON_CLASS_P (inner
)
7068 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
)
7069 inner
= TREE_OPERAND (inner
, 0);
7071 if (TREE_CODE (inner
) == CALL_EXPR
7072 && (fndecl
= get_callee_fndecl (inner
))
7073 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
7074 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
)
7078 code
= TREE_CODE (inner
);
7079 if (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
7081 tree op0
= TREE_OPERAND (inner
, 0);
7082 tree op1
= TREE_OPERAND (inner
, 1);
7084 op0
= build_builtin_expect_predicate (loc
, op0
, arg1
);
7085 op1
= build_builtin_expect_predicate (loc
, op1
, arg1
);
7086 inner
= build2 (code
, TREE_TYPE (inner
), op0
, op1
);
7088 return fold_convert_loc (loc
, TREE_TYPE (arg0
), inner
);
7091 /* If the argument isn't invariant then there's nothing else we can do. */
7092 if (!TREE_CONSTANT (inner_arg0
))
7095 /* If we expect that a comparison against the argument will fold to
7096 a constant return the constant. In practice, this means a true
7097 constant or the address of a non-weak symbol. */
7100 if (TREE_CODE (inner
) == ADDR_EXPR
)
7104 inner
= TREE_OPERAND (inner
, 0);
7106 while (TREE_CODE (inner
) == COMPONENT_REF
7107 || TREE_CODE (inner
) == ARRAY_REF
);
7108 if ((TREE_CODE (inner
) == VAR_DECL
7109 || TREE_CODE (inner
) == FUNCTION_DECL
)
7110 && DECL_WEAK (inner
))
7114 /* Otherwise, ARG0 already has the proper type for the return value. */
7118 /* Fold a call to __builtin_classify_type with argument ARG. */
7121 fold_builtin_classify_type (tree arg
)
7124 return build_int_cst (integer_type_node
, no_type_class
);
7126 return build_int_cst (integer_type_node
, type_to_class (TREE_TYPE (arg
)));
7129 /* Fold a call to __builtin_strlen with argument ARG. */
7132 fold_builtin_strlen (location_t loc
, tree type
, tree arg
)
7134 if (!validate_arg (arg
, POINTER_TYPE
))
7138 tree len
= c_strlen (arg
, 0);
7141 return fold_convert_loc (loc
, type
, len
);
7147 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7150 fold_builtin_inf (location_t loc
, tree type
, int warn
)
7152 REAL_VALUE_TYPE real
;
7154 /* __builtin_inff is intended to be usable to define INFINITY on all
7155 targets. If an infinity is not available, INFINITY expands "to a
7156 positive constant of type float that overflows at translation
7157 time", footnote "In this case, using INFINITY will violate the
7158 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7159 Thus we pedwarn to ensure this constraint violation is
7161 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
7162 pedwarn (loc
, 0, "target format does not support infinity");
7165 return build_real (type
, real
);
7168 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7171 fold_builtin_nan (tree arg
, tree type
, int quiet
)
7173 REAL_VALUE_TYPE real
;
7176 if (!validate_arg (arg
, POINTER_TYPE
))
7178 str
= c_getstr (arg
);
7182 if (!real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
7185 return build_real (type
, real
);
7188 /* Return true if the floating point expression T has an integer value.
7189 We also allow +Inf, -Inf and NaN to be considered integer values. */
7192 integer_valued_real_p (tree t
)
7194 switch (TREE_CODE (t
))
7201 return integer_valued_real_p (TREE_OPERAND (t
, 0));
7206 return integer_valued_real_p (TREE_OPERAND (t
, 1));
7213 return integer_valued_real_p (TREE_OPERAND (t
, 0))
7214 && integer_valued_real_p (TREE_OPERAND (t
, 1));
7217 return integer_valued_real_p (TREE_OPERAND (t
, 1))
7218 && integer_valued_real_p (TREE_OPERAND (t
, 2));
7221 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
7225 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
7226 if (TREE_CODE (type
) == INTEGER_TYPE
)
7228 if (TREE_CODE (type
) == REAL_TYPE
)
7229 return integer_valued_real_p (TREE_OPERAND (t
, 0));
7234 switch (builtin_mathfn_code (t
))
7236 CASE_FLT_FN (BUILT_IN_CEIL
):
7237 CASE_FLT_FN (BUILT_IN_FLOOR
):
7238 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
7239 CASE_FLT_FN (BUILT_IN_RINT
):
7240 CASE_FLT_FN (BUILT_IN_ROUND
):
7241 CASE_FLT_FN (BUILT_IN_TRUNC
):
7244 CASE_FLT_FN (BUILT_IN_FMIN
):
7245 CASE_FLT_FN (BUILT_IN_FMAX
):
7246 return integer_valued_real_p (CALL_EXPR_ARG (t
, 0))
7247 && integer_valued_real_p (CALL_EXPR_ARG (t
, 1));
7260 /* FNDECL is assumed to be a builtin where truncation can be propagated
7261 across (for instance floor((double)f) == (double)floorf (f).
7262 Do the transformation for a call with argument ARG. */
7265 fold_trunc_transparent_mathfn (location_t loc
, tree fndecl
, tree arg
)
7267 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7269 if (!validate_arg (arg
, REAL_TYPE
))
7272 /* Integer rounding functions are idempotent. */
7273 if (fcode
== builtin_mathfn_code (arg
))
7276 /* If argument is already integer valued, and we don't need to worry
7277 about setting errno, there's no need to perform rounding. */
7278 if (! flag_errno_math
&& integer_valued_real_p (arg
))
7283 tree arg0
= strip_float_extensions (arg
);
7284 tree ftype
= TREE_TYPE (TREE_TYPE (fndecl
));
7285 tree newtype
= TREE_TYPE (arg0
);
7288 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
7289 && (decl
= mathfn_built_in (newtype
, fcode
)))
7290 return fold_convert_loc (loc
, ftype
,
7291 build_call_expr_loc (loc
, decl
, 1,
7292 fold_convert_loc (loc
,
7299 /* FNDECL is assumed to be builtin which can narrow the FP type of
7300 the argument, for instance lround((double)f) -> lroundf (f).
7301 Do the transformation for a call with argument ARG. */
7304 fold_fixed_mathfn (location_t loc
, tree fndecl
, tree arg
)
7306 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7308 if (!validate_arg (arg
, REAL_TYPE
))
7311 /* If argument is already integer valued, and we don't need to worry
7312 about setting errno, there's no need to perform rounding. */
7313 if (! flag_errno_math
&& integer_valued_real_p (arg
))
7314 return fold_build1_loc (loc
, FIX_TRUNC_EXPR
,
7315 TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
7319 tree ftype
= TREE_TYPE (arg
);
7320 tree arg0
= strip_float_extensions (arg
);
7321 tree newtype
= TREE_TYPE (arg0
);
7324 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
7325 && (decl
= mathfn_built_in (newtype
, fcode
)))
7326 return build_call_expr_loc (loc
, decl
, 1,
7327 fold_convert_loc (loc
, newtype
, arg0
));
7330 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7331 sizeof (int) == sizeof (long). */
7332 if (TYPE_PRECISION (integer_type_node
)
7333 == TYPE_PRECISION (long_integer_type_node
))
7335 tree newfn
= NULL_TREE
;
7338 CASE_FLT_FN (BUILT_IN_ICEIL
):
7339 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
7342 CASE_FLT_FN (BUILT_IN_IFLOOR
):
7343 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
7346 CASE_FLT_FN (BUILT_IN_IROUND
):
7347 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
7350 CASE_FLT_FN (BUILT_IN_IRINT
):
7351 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
7360 tree newcall
= build_call_expr_loc (loc
, newfn
, 1, arg
);
7361 return fold_convert_loc (loc
,
7362 TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
7366 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7367 sizeof (long long) == sizeof (long). */
7368 if (TYPE_PRECISION (long_long_integer_type_node
)
7369 == TYPE_PRECISION (long_integer_type_node
))
7371 tree newfn
= NULL_TREE
;
7374 CASE_FLT_FN (BUILT_IN_LLCEIL
):
7375 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
7378 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7379 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
7382 CASE_FLT_FN (BUILT_IN_LLROUND
):
7383 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
7386 CASE_FLT_FN (BUILT_IN_LLRINT
):
7387 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
7396 tree newcall
= build_call_expr_loc (loc
, newfn
, 1, arg
);
7397 return fold_convert_loc (loc
,
7398 TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
7405 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7406 return type. Return NULL_TREE if no simplification can be made. */
7409 fold_builtin_cabs (location_t loc
, tree arg
, tree type
, tree fndecl
)
7413 if (!validate_arg (arg
, COMPLEX_TYPE
)
7414 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
7417 /* Calculate the result when the argument is a constant. */
7418 if (TREE_CODE (arg
) == COMPLEX_CST
7419 && (res
= do_mpfr_arg2 (TREE_REALPART (arg
), TREE_IMAGPART (arg
),
7423 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
7425 tree real
= TREE_OPERAND (arg
, 0);
7426 tree imag
= TREE_OPERAND (arg
, 1);
7428 /* If either part is zero, cabs is fabs of the other. */
7429 if (real_zerop (real
))
7430 return fold_build1_loc (loc
, ABS_EXPR
, type
, imag
);
7431 if (real_zerop (imag
))
7432 return fold_build1_loc (loc
, ABS_EXPR
, type
, real
);
7434 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7435 if (flag_unsafe_math_optimizations
7436 && operand_equal_p (real
, imag
, OEP_PURE_SAME
))
7438 const REAL_VALUE_TYPE sqrt2_trunc
7439 = real_value_truncate (TYPE_MODE (type
), dconst_sqrt2 ());
7441 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7442 fold_build1_loc (loc
, ABS_EXPR
, type
, real
),
7443 build_real (type
, sqrt2_trunc
));
7447 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7448 if (TREE_CODE (arg
) == NEGATE_EXPR
7449 || TREE_CODE (arg
) == CONJ_EXPR
)
7450 return build_call_expr_loc (loc
, fndecl
, 1, TREE_OPERAND (arg
, 0));
7452 /* Don't do this when optimizing for size. */
7453 if (flag_unsafe_math_optimizations
7454 && optimize
&& optimize_function_for_speed_p (cfun
))
7456 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
7458 if (sqrtfn
!= NULL_TREE
)
7460 tree rpart
, ipart
, result
;
7462 arg
= builtin_save_expr (arg
);
7464 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, type
, arg
);
7465 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg
);
7467 rpart
= builtin_save_expr (rpart
);
7468 ipart
= builtin_save_expr (ipart
);
7470 result
= fold_build2_loc (loc
, PLUS_EXPR
, type
,
7471 fold_build2_loc (loc
, MULT_EXPR
, type
,
7473 fold_build2_loc (loc
, MULT_EXPR
, type
,
7476 return build_call_expr_loc (loc
, sqrtfn
, 1, result
);
7483 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7484 complex tree type of the result. If NEG is true, the imaginary
7485 zero is negative. */
7488 build_complex_cproj (tree type
, bool neg
)
7490 REAL_VALUE_TYPE rinf
, rzero
= dconst0
;
7494 return build_complex (type
, build_real (TREE_TYPE (type
), rinf
),
7495 build_real (TREE_TYPE (type
), rzero
));
7498 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7499 return type. Return NULL_TREE if no simplification can be made. */
7502 fold_builtin_cproj (location_t loc
, tree arg
, tree type
)
7504 if (!validate_arg (arg
, COMPLEX_TYPE
)
7505 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
7508 /* If there are no infinities, return arg. */
7509 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type
))))
7510 return non_lvalue_loc (loc
, arg
);
7512 /* Calculate the result when the argument is a constant. */
7513 if (TREE_CODE (arg
) == COMPLEX_CST
)
7515 const REAL_VALUE_TYPE
*real
= TREE_REAL_CST_PTR (TREE_REALPART (arg
));
7516 const REAL_VALUE_TYPE
*imag
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg
));
7518 if (real_isinf (real
) || real_isinf (imag
))
7519 return build_complex_cproj (type
, imag
->sign
);
7523 else if (TREE_CODE (arg
) == COMPLEX_EXPR
)
7525 tree real
= TREE_OPERAND (arg
, 0);
7526 tree imag
= TREE_OPERAND (arg
, 1);
7531 /* If the real part is inf and the imag part is known to be
7532 nonnegative, return (inf + 0i). Remember side-effects are
7533 possible in the imag part. */
7534 if (TREE_CODE (real
) == REAL_CST
7535 && real_isinf (TREE_REAL_CST_PTR (real
))
7536 && tree_expr_nonnegative_p (imag
))
7537 return omit_one_operand_loc (loc
, type
,
7538 build_complex_cproj (type
, false),
7541 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7542 Remember side-effects are possible in the real part. */
7543 if (TREE_CODE (imag
) == REAL_CST
7544 && real_isinf (TREE_REAL_CST_PTR (imag
)))
7546 omit_one_operand_loc (loc
, type
,
7547 build_complex_cproj (type
, TREE_REAL_CST_PTR
7548 (imag
)->sign
), arg
);
7554 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7555 Return NULL_TREE if no simplification can be made. */
7558 fold_builtin_sqrt (location_t loc
, tree arg
, tree type
)
7561 enum built_in_function fcode
;
7564 if (!validate_arg (arg
, REAL_TYPE
))
7567 /* Calculate the result when the argument is a constant. */
7568 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_sqrt
, &dconst0
, NULL
, true)))
7571 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7572 fcode
= builtin_mathfn_code (arg
);
7573 if (flag_unsafe_math_optimizations
&& BUILTIN_EXPONENT_P (fcode
))
7575 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7576 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
7577 CALL_EXPR_ARG (arg
, 0),
7578 build_real (type
, dconsthalf
));
7579 return build_call_expr_loc (loc
, expfn
, 1, arg
);
7582 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7583 if (flag_unsafe_math_optimizations
&& BUILTIN_ROOT_P (fcode
))
7585 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7589 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7591 /* The inner root was either sqrt or cbrt. */
7592 /* This was a conditional expression but it triggered a bug
7594 REAL_VALUE_TYPE dconstroot
;
7595 if (BUILTIN_SQRT_P (fcode
))
7596 dconstroot
= dconsthalf
;
7598 dconstroot
= dconst_third ();
7600 /* Adjust for the outer root. */
7601 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7602 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7603 tree_root
= build_real (type
, dconstroot
);
7604 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7608 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7609 if (flag_unsafe_math_optimizations
7610 && (fcode
== BUILT_IN_POW
7611 || fcode
== BUILT_IN_POWF
7612 || fcode
== BUILT_IN_POWL
))
7614 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7615 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7616 tree arg1
= CALL_EXPR_ARG (arg
, 1);
7618 if (!tree_expr_nonnegative_p (arg0
))
7619 arg0
= build1 (ABS_EXPR
, type
, arg0
);
7620 narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
7621 build_real (type
, dconsthalf
));
7622 return build_call_expr_loc (loc
, powfn
, 2, arg0
, narg1
);
7628 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7629 Return NULL_TREE if no simplification can be made. */
7632 fold_builtin_cbrt (location_t loc
, tree arg
, tree type
)
7634 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
7637 if (!validate_arg (arg
, REAL_TYPE
))
7640 /* Calculate the result when the argument is a constant. */
7641 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cbrt
, NULL
, NULL
, 0)))
7644 if (flag_unsafe_math_optimizations
)
7646 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7647 if (BUILTIN_EXPONENT_P (fcode
))
7649 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7650 const REAL_VALUE_TYPE third_trunc
=
7651 real_value_truncate (TYPE_MODE (type
), dconst_third ());
7652 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
7653 CALL_EXPR_ARG (arg
, 0),
7654 build_real (type
, third_trunc
));
7655 return build_call_expr_loc (loc
, expfn
, 1, arg
);
7658 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7659 if (BUILTIN_SQRT_P (fcode
))
7661 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7665 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7667 REAL_VALUE_TYPE dconstroot
= dconst_third ();
7669 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7670 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7671 tree_root
= build_real (type
, dconstroot
);
7672 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7676 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7677 if (BUILTIN_CBRT_P (fcode
))
7679 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7680 if (tree_expr_nonnegative_p (arg0
))
7682 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7687 REAL_VALUE_TYPE dconstroot
;
7689 real_arithmetic (&dconstroot
, MULT_EXPR
,
7690 dconst_third_ptr (), dconst_third_ptr ());
7691 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7692 tree_root
= build_real (type
, dconstroot
);
7693 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7698 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7699 if (fcode
== BUILT_IN_POW
7700 || fcode
== BUILT_IN_POWF
7701 || fcode
== BUILT_IN_POWL
)
7703 tree arg00
= CALL_EXPR_ARG (arg
, 0);
7704 tree arg01
= CALL_EXPR_ARG (arg
, 1);
7705 if (tree_expr_nonnegative_p (arg00
))
7707 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7708 const REAL_VALUE_TYPE dconstroot
7709 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
7710 tree narg01
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg01
,
7711 build_real (type
, dconstroot
));
7712 return build_call_expr_loc (loc
, powfn
, 2, arg00
, narg01
);
7719 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7720 TYPE is the type of the return value. Return NULL_TREE if no
7721 simplification can be made. */
7724 fold_builtin_cos (location_t loc
,
7725 tree arg
, tree type
, tree fndecl
)
7729 if (!validate_arg (arg
, REAL_TYPE
))
7732 /* Calculate the result when the argument is a constant. */
7733 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cos
, NULL
, NULL
, 0)))
7736 /* Optimize cos(-x) into cos (x). */
7737 if ((narg
= fold_strip_sign_ops (arg
)))
7738 return build_call_expr_loc (loc
, fndecl
, 1, narg
);
7743 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7744 Return NULL_TREE if no simplification can be made. */
7747 fold_builtin_cosh (location_t loc
, tree arg
, tree type
, tree fndecl
)
7749 if (validate_arg (arg
, REAL_TYPE
))
7753 /* Calculate the result when the argument is a constant. */
7754 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cosh
, NULL
, NULL
, 0)))
7757 /* Optimize cosh(-x) into cosh (x). */
7758 if ((narg
= fold_strip_sign_ops (arg
)))
7759 return build_call_expr_loc (loc
, fndecl
, 1, narg
);
7765 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7766 argument ARG. TYPE is the type of the return value. Return
7767 NULL_TREE if no simplification can be made. */
7770 fold_builtin_ccos (location_t loc
, tree arg
, tree type
, tree fndecl
,
7773 if (validate_arg (arg
, COMPLEX_TYPE
)
7774 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
7778 /* Calculate the result when the argument is a constant. */
7779 if ((tmp
= do_mpc_arg1 (arg
, type
, (hyper
? mpc_cosh
: mpc_cos
))))
7782 /* Optimize fn(-x) into fn(x). */
7783 if ((tmp
= fold_strip_sign_ops (arg
)))
7784 return build_call_expr_loc (loc
, fndecl
, 1, tmp
);
7790 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7791 Return NULL_TREE if no simplification can be made. */
7794 fold_builtin_tan (tree arg
, tree type
)
7796 enum built_in_function fcode
;
7799 if (!validate_arg (arg
, REAL_TYPE
))
7802 /* Calculate the result when the argument is a constant. */
7803 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_tan
, NULL
, NULL
, 0)))
7806 /* Optimize tan(atan(x)) = x. */
7807 fcode
= builtin_mathfn_code (arg
);
7808 if (flag_unsafe_math_optimizations
7809 && (fcode
== BUILT_IN_ATAN
7810 || fcode
== BUILT_IN_ATANF
7811 || fcode
== BUILT_IN_ATANL
))
7812 return CALL_EXPR_ARG (arg
, 0);
7817 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7818 NULL_TREE if no simplification can be made. */
7821 fold_builtin_sincos (location_t loc
,
7822 tree arg0
, tree arg1
, tree arg2
)
7827 if (!validate_arg (arg0
, REAL_TYPE
)
7828 || !validate_arg (arg1
, POINTER_TYPE
)
7829 || !validate_arg (arg2
, POINTER_TYPE
))
7832 type
= TREE_TYPE (arg0
);
7834 /* Calculate the result when the argument is a constant. */
7835 if ((res
= do_mpfr_sincos (arg0
, arg1
, arg2
)))
7838 /* Canonicalize sincos to cexpi. */
7839 if (!targetm
.libc_has_function (function_c99_math_complex
))
7841 fn
= mathfn_built_in (type
, BUILT_IN_CEXPI
);
7845 call
= build_call_expr_loc (loc
, fn
, 1, arg0
);
7846 call
= builtin_save_expr (call
);
7848 return build2 (COMPOUND_EXPR
, void_type_node
,
7849 build2 (MODIFY_EXPR
, void_type_node
,
7850 build_fold_indirect_ref_loc (loc
, arg1
),
7851 build1 (IMAGPART_EXPR
, type
, call
)),
7852 build2 (MODIFY_EXPR
, void_type_node
,
7853 build_fold_indirect_ref_loc (loc
, arg2
),
7854 build1 (REALPART_EXPR
, type
, call
)));
7857 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7858 NULL_TREE if no simplification can be made. */
7861 fold_builtin_cexp (location_t loc
, tree arg0
, tree type
)
7864 tree realp
, imagp
, ifn
;
7867 if (!validate_arg (arg0
, COMPLEX_TYPE
)
7868 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) != REAL_TYPE
)
7871 /* Calculate the result when the argument is a constant. */
7872 if ((res
= do_mpc_arg1 (arg0
, type
, mpc_exp
)))
7875 rtype
= TREE_TYPE (TREE_TYPE (arg0
));
7877 /* In case we can figure out the real part of arg0 and it is constant zero
7879 if (!targetm
.libc_has_function (function_c99_math_complex
))
7881 ifn
= mathfn_built_in (rtype
, BUILT_IN_CEXPI
);
7885 if ((realp
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
))
7886 && real_zerop (realp
))
7888 tree narg
= fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
7889 return build_call_expr_loc (loc
, ifn
, 1, narg
);
7892 /* In case we can easily decompose real and imaginary parts split cexp
7893 to exp (r) * cexpi (i). */
7894 if (flag_unsafe_math_optimizations
7897 tree rfn
, rcall
, icall
;
7899 rfn
= mathfn_built_in (rtype
, BUILT_IN_EXP
);
7903 imagp
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
7907 icall
= build_call_expr_loc (loc
, ifn
, 1, imagp
);
7908 icall
= builtin_save_expr (icall
);
7909 rcall
= build_call_expr_loc (loc
, rfn
, 1, realp
);
7910 rcall
= builtin_save_expr (rcall
);
7911 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
7912 fold_build2_loc (loc
, MULT_EXPR
, rtype
,
7914 fold_build1_loc (loc
, REALPART_EXPR
,
7916 fold_build2_loc (loc
, MULT_EXPR
, rtype
,
7918 fold_build1_loc (loc
, IMAGPART_EXPR
,
7925 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7926 Return NULL_TREE if no simplification can be made. */
7929 fold_builtin_trunc (location_t loc
, tree fndecl
, tree arg
)
7931 if (!validate_arg (arg
, REAL_TYPE
))
7934 /* Optimize trunc of constant value. */
7935 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7937 REAL_VALUE_TYPE r
, x
;
7938 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7940 x
= TREE_REAL_CST (arg
);
7941 real_trunc (&r
, TYPE_MODE (type
), &x
);
7942 return build_real (type
, r
);
7945 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
7948 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7949 Return NULL_TREE if no simplification can be made. */
7952 fold_builtin_floor (location_t loc
, tree fndecl
, tree arg
)
7954 if (!validate_arg (arg
, REAL_TYPE
))
7957 /* Optimize floor of constant value. */
7958 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7962 x
= TREE_REAL_CST (arg
);
7963 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7965 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7968 real_floor (&r
, TYPE_MODE (type
), &x
);
7969 return build_real (type
, r
);
7973 /* Fold floor (x) where x is nonnegative to trunc (x). */
7974 if (tree_expr_nonnegative_p (arg
))
7976 tree truncfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_TRUNC
);
7978 return build_call_expr_loc (loc
, truncfn
, 1, arg
);
7981 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
7984 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7985 Return NULL_TREE if no simplification can be made. */
7988 fold_builtin_ceil (location_t loc
, tree fndecl
, tree arg
)
7990 if (!validate_arg (arg
, REAL_TYPE
))
7993 /* Optimize ceil of constant value. */
7994 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7998 x
= TREE_REAL_CST (arg
);
7999 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
8001 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8004 real_ceil (&r
, TYPE_MODE (type
), &x
);
8005 return build_real (type
, r
);
8009 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
8012 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8013 Return NULL_TREE if no simplification can be made. */
8016 fold_builtin_round (location_t loc
, tree fndecl
, tree arg
)
8018 if (!validate_arg (arg
, REAL_TYPE
))
8021 /* Optimize round of constant value. */
8022 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8026 x
= TREE_REAL_CST (arg
);
8027 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
8029 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8032 real_round (&r
, TYPE_MODE (type
), &x
);
8033 return build_real (type
, r
);
8037 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
8040 /* Fold function call to builtin lround, lroundf or lroundl (or the
8041 corresponding long long versions) and other rounding functions. ARG
8042 is the argument to the call. Return NULL_TREE if no simplification
8046 fold_builtin_int_roundingfn (location_t loc
, tree fndecl
, tree arg
)
8048 if (!validate_arg (arg
, REAL_TYPE
))
8051 /* Optimize lround of constant value. */
8052 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8054 const REAL_VALUE_TYPE x
= TREE_REAL_CST (arg
);
8056 if (real_isfinite (&x
))
8058 tree itype
= TREE_TYPE (TREE_TYPE (fndecl
));
8059 tree ftype
= TREE_TYPE (arg
);
8063 switch (DECL_FUNCTION_CODE (fndecl
))
8065 CASE_FLT_FN (BUILT_IN_IFLOOR
):
8066 CASE_FLT_FN (BUILT_IN_LFLOOR
):
8067 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
8068 real_floor (&r
, TYPE_MODE (ftype
), &x
);
8071 CASE_FLT_FN (BUILT_IN_ICEIL
):
8072 CASE_FLT_FN (BUILT_IN_LCEIL
):
8073 CASE_FLT_FN (BUILT_IN_LLCEIL
):
8074 real_ceil (&r
, TYPE_MODE (ftype
), &x
);
8077 CASE_FLT_FN (BUILT_IN_IROUND
):
8078 CASE_FLT_FN (BUILT_IN_LROUND
):
8079 CASE_FLT_FN (BUILT_IN_LLROUND
):
8080 real_round (&r
, TYPE_MODE (ftype
), &x
);
8087 real_to_integer2 ((HOST_WIDE_INT
*)&val
.low
, &val
.high
, &r
);
8088 if (double_int_fits_to_tree_p (itype
, val
))
8089 return double_int_to_tree (itype
, val
);
8093 switch (DECL_FUNCTION_CODE (fndecl
))
8095 CASE_FLT_FN (BUILT_IN_LFLOOR
):
8096 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
8097 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8098 if (tree_expr_nonnegative_p (arg
))
8099 return fold_build1_loc (loc
, FIX_TRUNC_EXPR
,
8100 TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
8105 return fold_fixed_mathfn (loc
, fndecl
, arg
);
8108 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8109 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8110 the argument to the call. Return NULL_TREE if no simplification can
8114 fold_builtin_bitop (tree fndecl
, tree arg
)
8116 if (!validate_arg (arg
, INTEGER_TYPE
))
8119 /* Optimize for constant argument. */
8120 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
8122 HOST_WIDE_INT hi
, width
, result
;
8123 unsigned HOST_WIDE_INT lo
;
8126 type
= TREE_TYPE (arg
);
8127 width
= TYPE_PRECISION (type
);
8128 lo
= TREE_INT_CST_LOW (arg
);
8130 /* Clear all the bits that are beyond the type's precision. */
8131 if (width
> HOST_BITS_PER_WIDE_INT
)
8133 hi
= TREE_INT_CST_HIGH (arg
);
8134 if (width
< HOST_BITS_PER_DOUBLE_INT
)
8135 hi
&= ~(HOST_WIDE_INT_M1U
<< (width
- HOST_BITS_PER_WIDE_INT
));
8140 if (width
< HOST_BITS_PER_WIDE_INT
)
8141 lo
&= ~(HOST_WIDE_INT_M1U
<< width
);
8144 switch (DECL_FUNCTION_CODE (fndecl
))
8146 CASE_INT_FN (BUILT_IN_FFS
):
8148 result
= ffs_hwi (lo
);
8150 result
= HOST_BITS_PER_WIDE_INT
+ ffs_hwi (hi
);
8155 CASE_INT_FN (BUILT_IN_CLZ
):
8157 result
= width
- floor_log2 (hi
) - 1 - HOST_BITS_PER_WIDE_INT
;
8159 result
= width
- floor_log2 (lo
) - 1;
8160 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
8164 CASE_INT_FN (BUILT_IN_CTZ
):
8166 result
= ctz_hwi (lo
);
8168 result
= HOST_BITS_PER_WIDE_INT
+ ctz_hwi (hi
);
8169 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
8173 CASE_INT_FN (BUILT_IN_CLRSB
):
8174 if (width
> 2 * HOST_BITS_PER_WIDE_INT
)
8176 if (width
> HOST_BITS_PER_WIDE_INT
8177 && (hi
& ((unsigned HOST_WIDE_INT
) 1
8178 << (width
- HOST_BITS_PER_WIDE_INT
- 1))) != 0)
8180 hi
= ~hi
& ~(HOST_WIDE_INT_M1U
8181 << (width
- HOST_BITS_PER_WIDE_INT
- 1));
8184 else if (width
<= HOST_BITS_PER_WIDE_INT
8185 && (lo
& ((unsigned HOST_WIDE_INT
) 1 << (width
- 1))) != 0)
8186 lo
= ~lo
& ~(HOST_WIDE_INT_M1U
<< (width
- 1));
8188 result
= width
- floor_log2 (hi
) - 2 - HOST_BITS_PER_WIDE_INT
;
8190 result
= width
- floor_log2 (lo
) - 2;
8195 CASE_INT_FN (BUILT_IN_POPCOUNT
):
8198 result
++, lo
&= lo
- 1;
8200 result
++, hi
&= (unsigned HOST_WIDE_INT
) hi
- 1;
8203 CASE_INT_FN (BUILT_IN_PARITY
):
8206 result
++, lo
&= lo
- 1;
8208 result
++, hi
&= (unsigned HOST_WIDE_INT
) hi
- 1;
8216 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), result
);
8222 /* Fold function call to builtin_bswap and the short, long and long long
8223 variants. Return NULL_TREE if no simplification can be made. */
8225 fold_builtin_bswap (tree fndecl
, tree arg
)
8227 if (! validate_arg (arg
, INTEGER_TYPE
))
8230 /* Optimize constant value. */
8231 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
8233 HOST_WIDE_INT hi
, width
, r_hi
= 0;
8234 unsigned HOST_WIDE_INT lo
, r_lo
= 0;
8235 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8237 width
= TYPE_PRECISION (type
);
8238 lo
= TREE_INT_CST_LOW (arg
);
8239 hi
= TREE_INT_CST_HIGH (arg
);
8241 switch (DECL_FUNCTION_CODE (fndecl
))
8243 case BUILT_IN_BSWAP16
:
8244 case BUILT_IN_BSWAP32
:
8245 case BUILT_IN_BSWAP64
:
8249 for (s
= 0; s
< width
; s
+= 8)
8251 int d
= width
- s
- 8;
8252 unsigned HOST_WIDE_INT byte
;
8254 if (s
< HOST_BITS_PER_WIDE_INT
)
8255 byte
= (lo
>> s
) & 0xff;
8257 byte
= (hi
>> (s
- HOST_BITS_PER_WIDE_INT
)) & 0xff;
8259 if (d
< HOST_BITS_PER_WIDE_INT
)
8262 r_hi
|= byte
<< (d
- HOST_BITS_PER_WIDE_INT
);
8272 if (width
< HOST_BITS_PER_WIDE_INT
)
8273 return build_int_cst (type
, r_lo
);
8275 return build_int_cst_wide (type
, r_lo
, r_hi
);
8281 /* A subroutine of fold_builtin to fold the various logarithmic
8282 functions. Return NULL_TREE if no simplification can me made.
8283 FUNC is the corresponding MPFR logarithm function. */
8286 fold_builtin_logarithm (location_t loc
, tree fndecl
, tree arg
,
8287 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
))
8289 if (validate_arg (arg
, REAL_TYPE
))
8291 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8293 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
8295 /* Calculate the result when the argument is a constant. */
8296 if ((res
= do_mpfr_arg1 (arg
, type
, func
, &dconst0
, NULL
, false)))
8299 /* Special case, optimize logN(expN(x)) = x. */
8300 if (flag_unsafe_math_optimizations
8301 && ((func
== mpfr_log
8302 && (fcode
== BUILT_IN_EXP
8303 || fcode
== BUILT_IN_EXPF
8304 || fcode
== BUILT_IN_EXPL
))
8305 || (func
== mpfr_log2
8306 && (fcode
== BUILT_IN_EXP2
8307 || fcode
== BUILT_IN_EXP2F
8308 || fcode
== BUILT_IN_EXP2L
))
8309 || (func
== mpfr_log10
&& (BUILTIN_EXP10_P (fcode
)))))
8310 return fold_convert_loc (loc
, type
, CALL_EXPR_ARG (arg
, 0));
8312 /* Optimize logN(func()) for various exponential functions. We
8313 want to determine the value "x" and the power "exponent" in
8314 order to transform logN(x**exponent) into exponent*logN(x). */
8315 if (flag_unsafe_math_optimizations
)
8317 tree exponent
= 0, x
= 0;
8321 CASE_FLT_FN (BUILT_IN_EXP
):
8322 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8323 x
= build_real (type
, real_value_truncate (TYPE_MODE (type
),
8325 exponent
= CALL_EXPR_ARG (arg
, 0);
8327 CASE_FLT_FN (BUILT_IN_EXP2
):
8328 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8329 x
= build_real (type
, dconst2
);
8330 exponent
= CALL_EXPR_ARG (arg
, 0);
8332 CASE_FLT_FN (BUILT_IN_EXP10
):
8333 CASE_FLT_FN (BUILT_IN_POW10
):
8334 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8336 REAL_VALUE_TYPE dconst10
;
8337 real_from_integer (&dconst10
, VOIDmode
, 10, 0, 0);
8338 x
= build_real (type
, dconst10
);
8340 exponent
= CALL_EXPR_ARG (arg
, 0);
8342 CASE_FLT_FN (BUILT_IN_SQRT
):
8343 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8344 x
= CALL_EXPR_ARG (arg
, 0);
8345 exponent
= build_real (type
, dconsthalf
);
8347 CASE_FLT_FN (BUILT_IN_CBRT
):
8348 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8349 x
= CALL_EXPR_ARG (arg
, 0);
8350 exponent
= build_real (type
, real_value_truncate (TYPE_MODE (type
),
8353 CASE_FLT_FN (BUILT_IN_POW
):
8354 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8355 x
= CALL_EXPR_ARG (arg
, 0);
8356 exponent
= CALL_EXPR_ARG (arg
, 1);
8362 /* Now perform the optimization. */
8365 tree logfn
= build_call_expr_loc (loc
, fndecl
, 1, x
);
8366 return fold_build2_loc (loc
, MULT_EXPR
, type
, exponent
, logfn
);
8374 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8375 NULL_TREE if no simplification can be made. */
8378 fold_builtin_hypot (location_t loc
, tree fndecl
,
8379 tree arg0
, tree arg1
, tree type
)
8381 tree res
, narg0
, narg1
;
8383 if (!validate_arg (arg0
, REAL_TYPE
)
8384 || !validate_arg (arg1
, REAL_TYPE
))
8387 /* Calculate the result when the argument is a constant. */
8388 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_hypot
)))
8391 /* If either argument to hypot has a negate or abs, strip that off.
8392 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8393 narg0
= fold_strip_sign_ops (arg0
);
8394 narg1
= fold_strip_sign_ops (arg1
);
8397 return build_call_expr_loc (loc
, fndecl
, 2, narg0
? narg0
: arg0
,
8398 narg1
? narg1
: arg1
);
8401 /* If either argument is zero, hypot is fabs of the other. */
8402 if (real_zerop (arg0
))
8403 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg1
);
8404 else if (real_zerop (arg1
))
8405 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg0
);
8407 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8408 if (flag_unsafe_math_optimizations
8409 && operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
8411 const REAL_VALUE_TYPE sqrt2_trunc
8412 = real_value_truncate (TYPE_MODE (type
), dconst_sqrt2 ());
8413 return fold_build2_loc (loc
, MULT_EXPR
, type
,
8414 fold_build1_loc (loc
, ABS_EXPR
, type
, arg0
),
8415 build_real (type
, sqrt2_trunc
));
8422 /* Fold a builtin function call to pow, powf, or powl. Return
8423 NULL_TREE if no simplification can be made. */
8425 fold_builtin_pow (location_t loc
, tree fndecl
, tree arg0
, tree arg1
, tree type
)
8429 if (!validate_arg (arg0
, REAL_TYPE
)
8430 || !validate_arg (arg1
, REAL_TYPE
))
8433 /* Calculate the result when the argument is a constant. */
8434 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_pow
)))
8437 /* Optimize pow(1.0,y) = 1.0. */
8438 if (real_onep (arg0
))
8439 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
), arg1
);
8441 if (TREE_CODE (arg1
) == REAL_CST
8442 && !TREE_OVERFLOW (arg1
))
8444 REAL_VALUE_TYPE cint
;
8448 c
= TREE_REAL_CST (arg1
);
8450 /* Optimize pow(x,0.0) = 1.0. */
8451 if (REAL_VALUES_EQUAL (c
, dconst0
))
8452 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
),
8455 /* Optimize pow(x,1.0) = x. */
8456 if (REAL_VALUES_EQUAL (c
, dconst1
))
8459 /* Optimize pow(x,-1.0) = 1.0/x. */
8460 if (REAL_VALUES_EQUAL (c
, dconstm1
))
8461 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
8462 build_real (type
, dconst1
), arg0
);
8464 /* Optimize pow(x,0.5) = sqrt(x). */
8465 if (flag_unsafe_math_optimizations
8466 && REAL_VALUES_EQUAL (c
, dconsthalf
))
8468 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
8470 if (sqrtfn
!= NULL_TREE
)
8471 return build_call_expr_loc (loc
, sqrtfn
, 1, arg0
);
8474 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8475 if (flag_unsafe_math_optimizations
)
8477 const REAL_VALUE_TYPE dconstroot
8478 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
8480 if (REAL_VALUES_EQUAL (c
, dconstroot
))
8482 tree cbrtfn
= mathfn_built_in (type
, BUILT_IN_CBRT
);
8483 if (cbrtfn
!= NULL_TREE
)
8484 return build_call_expr_loc (loc
, cbrtfn
, 1, arg0
);
8488 /* Check for an integer exponent. */
8489 n
= real_to_integer (&c
);
8490 real_from_integer (&cint
, VOIDmode
, n
, n
< 0 ? -1 : 0, 0);
8491 if (real_identical (&c
, &cint
))
8493 /* Attempt to evaluate pow at compile-time, unless this should
8494 raise an exception. */
8495 if (TREE_CODE (arg0
) == REAL_CST
8496 && !TREE_OVERFLOW (arg0
)
8498 || (!flag_trapping_math
&& !flag_errno_math
)
8499 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0
), dconst0
)))
8504 x
= TREE_REAL_CST (arg0
);
8505 inexact
= real_powi (&x
, TYPE_MODE (type
), &x
, n
);
8506 if (flag_unsafe_math_optimizations
|| !inexact
)
8507 return build_real (type
, x
);
8510 /* Strip sign ops from even integer powers. */
8511 if ((n
& 1) == 0 && flag_unsafe_math_optimizations
)
8513 tree narg0
= fold_strip_sign_ops (arg0
);
8515 return build_call_expr_loc (loc
, fndecl
, 2, narg0
, arg1
);
8520 if (flag_unsafe_math_optimizations
)
8522 const enum built_in_function fcode
= builtin_mathfn_code (arg0
);
8524 /* Optimize pow(expN(x),y) = expN(x*y). */
8525 if (BUILTIN_EXPONENT_P (fcode
))
8527 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
8528 tree arg
= CALL_EXPR_ARG (arg0
, 0);
8529 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg
, arg1
);
8530 return build_call_expr_loc (loc
, expfn
, 1, arg
);
8533 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8534 if (BUILTIN_SQRT_P (fcode
))
8536 tree narg0
= CALL_EXPR_ARG (arg0
, 0);
8537 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
8538 build_real (type
, dconsthalf
));
8539 return build_call_expr_loc (loc
, fndecl
, 2, narg0
, narg1
);
8542 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8543 if (BUILTIN_CBRT_P (fcode
))
8545 tree arg
= CALL_EXPR_ARG (arg0
, 0);
8546 if (tree_expr_nonnegative_p (arg
))
8548 const REAL_VALUE_TYPE dconstroot
8549 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
8550 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
8551 build_real (type
, dconstroot
));
8552 return build_call_expr_loc (loc
, fndecl
, 2, arg
, narg1
);
8556 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8557 if (fcode
== BUILT_IN_POW
8558 || fcode
== BUILT_IN_POWF
8559 || fcode
== BUILT_IN_POWL
)
8561 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
8562 if (tree_expr_nonnegative_p (arg00
))
8564 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
8565 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg01
, arg1
);
8566 return build_call_expr_loc (loc
, fndecl
, 2, arg00
, narg1
);
8574 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8575 Return NULL_TREE if no simplification can be made. */
8577 fold_builtin_powi (location_t loc
, tree fndecl ATTRIBUTE_UNUSED
,
8578 tree arg0
, tree arg1
, tree type
)
8580 if (!validate_arg (arg0
, REAL_TYPE
)
8581 || !validate_arg (arg1
, INTEGER_TYPE
))
8584 /* Optimize pow(1.0,y) = 1.0. */
8585 if (real_onep (arg0
))
8586 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
), arg1
);
8588 if (tree_fits_shwi_p (arg1
))
8590 HOST_WIDE_INT c
= tree_to_shwi (arg1
);
8592 /* Evaluate powi at compile-time. */
8593 if (TREE_CODE (arg0
) == REAL_CST
8594 && !TREE_OVERFLOW (arg0
))
8597 x
= TREE_REAL_CST (arg0
);
8598 real_powi (&x
, TYPE_MODE (type
), &x
, c
);
8599 return build_real (type
, x
);
8602 /* Optimize pow(x,0) = 1.0. */
8604 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
),
8607 /* Optimize pow(x,1) = x. */
8611 /* Optimize pow(x,-1) = 1.0/x. */
8613 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
8614 build_real (type
, dconst1
), arg0
);
8620 /* A subroutine of fold_builtin to fold the various exponent
8621 functions. Return NULL_TREE if no simplification can be made.
8622 FUNC is the corresponding MPFR exponent function. */
8625 fold_builtin_exponent (location_t loc
, tree fndecl
, tree arg
,
8626 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
))
8628 if (validate_arg (arg
, REAL_TYPE
))
8630 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8633 /* Calculate the result when the argument is a constant. */
8634 if ((res
= do_mpfr_arg1 (arg
, type
, func
, NULL
, NULL
, 0)))
8637 /* Optimize expN(logN(x)) = x. */
8638 if (flag_unsafe_math_optimizations
)
8640 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
8642 if ((func
== mpfr_exp
8643 && (fcode
== BUILT_IN_LOG
8644 || fcode
== BUILT_IN_LOGF
8645 || fcode
== BUILT_IN_LOGL
))
8646 || (func
== mpfr_exp2
8647 && (fcode
== BUILT_IN_LOG2
8648 || fcode
== BUILT_IN_LOG2F
8649 || fcode
== BUILT_IN_LOG2L
))
8650 || (func
== mpfr_exp10
8651 && (fcode
== BUILT_IN_LOG10
8652 || fcode
== BUILT_IN_LOG10F
8653 || fcode
== BUILT_IN_LOG10L
)))
8654 return fold_convert_loc (loc
, type
, CALL_EXPR_ARG (arg
, 0));
8661 /* Return true if VAR is a VAR_DECL or a component thereof. */
8664 var_decl_component_p (tree var
)
8667 while (handled_component_p (inner
))
8668 inner
= TREE_OPERAND (inner
, 0);
8669 return SSA_VAR_P (inner
);
8672 /* Fold function call to builtin memset. Return
8673 NULL_TREE if no simplification can be made. */
8676 fold_builtin_memset (location_t loc
, tree dest
, tree c
, tree len
,
8677 tree type
, bool ignore
)
8679 tree var
, ret
, etype
;
8680 unsigned HOST_WIDE_INT length
, cval
;
8682 if (! validate_arg (dest
, POINTER_TYPE
)
8683 || ! validate_arg (c
, INTEGER_TYPE
)
8684 || ! validate_arg (len
, INTEGER_TYPE
))
8687 if (! tree_fits_uhwi_p (len
))
8690 /* If the LEN parameter is zero, return DEST. */
8691 if (integer_zerop (len
))
8692 return omit_one_operand_loc (loc
, type
, dest
, c
);
8694 if (TREE_CODE (c
) != INTEGER_CST
|| TREE_SIDE_EFFECTS (dest
))
8699 if (TREE_CODE (var
) != ADDR_EXPR
)
8702 var
= TREE_OPERAND (var
, 0);
8703 if (TREE_THIS_VOLATILE (var
))
8706 etype
= TREE_TYPE (var
);
8707 if (TREE_CODE (etype
) == ARRAY_TYPE
)
8708 etype
= TREE_TYPE (etype
);
8710 if (!INTEGRAL_TYPE_P (etype
)
8711 && !POINTER_TYPE_P (etype
))
8714 if (! var_decl_component_p (var
))
8717 length
= tree_to_uhwi (len
);
8718 if (GET_MODE_SIZE (TYPE_MODE (etype
)) != length
8719 || get_pointer_alignment (dest
) / BITS_PER_UNIT
< length
)
8722 if (length
> HOST_BITS_PER_WIDE_INT
/ BITS_PER_UNIT
)
8725 if (integer_zerop (c
))
8729 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8 || HOST_BITS_PER_WIDE_INT
> 64)
8732 cval
= TREE_INT_CST_LOW (c
);
8736 cval
|= (cval
<< 31) << 1;
8739 ret
= build_int_cst_type (etype
, cval
);
8740 var
= build_fold_indirect_ref_loc (loc
,
8741 fold_convert_loc (loc
,
8742 build_pointer_type (etype
),
8744 ret
= build2 (MODIFY_EXPR
, etype
, var
, ret
);
8748 return omit_one_operand_loc (loc
, type
, dest
, ret
);
8751 /* Fold function call to builtin memset. Return
8752 NULL_TREE if no simplification can be made. */
8755 fold_builtin_bzero (location_t loc
, tree dest
, tree size
, bool ignore
)
8757 if (! validate_arg (dest
, POINTER_TYPE
)
8758 || ! validate_arg (size
, INTEGER_TYPE
))
8764 /* New argument list transforming bzero(ptr x, int y) to
8765 memset(ptr x, int 0, size_t y). This is done this way
8766 so that if it isn't expanded inline, we fallback to
8767 calling bzero instead of memset. */
8769 return fold_builtin_memset (loc
, dest
, integer_zero_node
,
8770 fold_convert_loc (loc
, size_type_node
, size
),
8771 void_type_node
, ignore
);
8774 /* Fold function call to builtin mem{{,p}cpy,move}. Return
8775 NULL_TREE if no simplification can be made.
8776 If ENDP is 0, return DEST (like memcpy).
8777 If ENDP is 1, return DEST+LEN (like mempcpy).
8778 If ENDP is 2, return DEST+LEN-1 (like stpcpy).
8779 If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap
8783 fold_builtin_memory_op (location_t loc
, tree dest
, tree src
,
8784 tree len
, tree type
, bool ignore
, int endp
)
8786 tree destvar
, srcvar
, expr
;
8788 if (! validate_arg (dest
, POINTER_TYPE
)
8789 || ! validate_arg (src
, POINTER_TYPE
)
8790 || ! validate_arg (len
, INTEGER_TYPE
))
8793 /* If the LEN parameter is zero, return DEST. */
8794 if (integer_zerop (len
))
8795 return omit_one_operand_loc (loc
, type
, dest
, src
);
8797 /* If SRC and DEST are the same (and not volatile), return
8798 DEST{,+LEN,+LEN-1}. */
8799 if (operand_equal_p (src
, dest
, 0))
8803 tree srctype
, desttype
;
8804 unsigned int src_align
, dest_align
;
8809 src_align
= get_pointer_alignment (src
);
8810 dest_align
= get_pointer_alignment (dest
);
8812 /* Both DEST and SRC must be pointer types.
8813 ??? This is what old code did. Is the testing for pointer types
8816 If either SRC is readonly or length is 1, we can use memcpy. */
8817 if (!dest_align
|| !src_align
)
8819 if (readonly_data_expr (src
)
8820 || (tree_fits_uhwi_p (len
)
8821 && (MIN (src_align
, dest_align
) / BITS_PER_UNIT
8822 >= tree_to_uhwi (len
))))
8824 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
8827 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
8830 /* If *src and *dest can't overlap, optimize into memcpy as well. */
8831 if (TREE_CODE (src
) == ADDR_EXPR
8832 && TREE_CODE (dest
) == ADDR_EXPR
)
8834 tree src_base
, dest_base
, fn
;
8835 HOST_WIDE_INT src_offset
= 0, dest_offset
= 0;
8836 HOST_WIDE_INT size
= -1;
8837 HOST_WIDE_INT maxsize
= -1;
8839 srcvar
= TREE_OPERAND (src
, 0);
8840 src_base
= get_ref_base_and_extent (srcvar
, &src_offset
,
8842 destvar
= TREE_OPERAND (dest
, 0);
8843 dest_base
= get_ref_base_and_extent (destvar
, &dest_offset
,
8845 if (tree_fits_uhwi_p (len
))
8846 maxsize
= tree_to_uhwi (len
);
8849 src_offset
/= BITS_PER_UNIT
;
8850 dest_offset
/= BITS_PER_UNIT
;
8851 if (SSA_VAR_P (src_base
)
8852 && SSA_VAR_P (dest_base
))
8854 if (operand_equal_p (src_base
, dest_base
, 0)
8855 && ranges_overlap_p (src_offset
, maxsize
,
8856 dest_offset
, maxsize
))
8859 else if (TREE_CODE (src_base
) == MEM_REF
8860 && TREE_CODE (dest_base
) == MEM_REF
)
8863 if (! operand_equal_p (TREE_OPERAND (src_base
, 0),
8864 TREE_OPERAND (dest_base
, 0), 0))
8866 off
= mem_ref_offset (src_base
) +
8867 double_int::from_shwi (src_offset
);
8868 if (!off
.fits_shwi ())
8870 src_offset
= off
.low
;
8871 off
= mem_ref_offset (dest_base
) +
8872 double_int::from_shwi (dest_offset
);
8873 if (!off
.fits_shwi ())
8875 dest_offset
= off
.low
;
8876 if (ranges_overlap_p (src_offset
, maxsize
,
8877 dest_offset
, maxsize
))
8883 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
8886 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
8889 /* If the destination and source do not alias optimize into
8891 if ((is_gimple_min_invariant (dest
)
8892 || TREE_CODE (dest
) == SSA_NAME
)
8893 && (is_gimple_min_invariant (src
)
8894 || TREE_CODE (src
) == SSA_NAME
))
8897 ao_ref_init_from_ptr_and_size (&destr
, dest
, len
);
8898 ao_ref_init_from_ptr_and_size (&srcr
, src
, len
);
8899 if (!refs_may_alias_p_1 (&destr
, &srcr
, false))
8902 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
8905 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
8912 if (!tree_fits_shwi_p (len
))
8915 This logic lose for arguments like (type *)malloc (sizeof (type)),
8916 since we strip the casts of up to VOID return value from malloc.
8917 Perhaps we ought to inherit type from non-VOID argument here? */
8920 if (!POINTER_TYPE_P (TREE_TYPE (src
))
8921 || !POINTER_TYPE_P (TREE_TYPE (dest
)))
8923 /* As we fold (void *)(p + CST) to (void *)p + CST undo this here. */
8924 if (TREE_CODE (src
) == POINTER_PLUS_EXPR
)
8926 tree tem
= TREE_OPERAND (src
, 0);
8928 if (tem
!= TREE_OPERAND (src
, 0))
8929 src
= build1 (NOP_EXPR
, TREE_TYPE (tem
), src
);
8931 if (TREE_CODE (dest
) == POINTER_PLUS_EXPR
)
8933 tree tem
= TREE_OPERAND (dest
, 0);
8935 if (tem
!= TREE_OPERAND (dest
, 0))
8936 dest
= build1 (NOP_EXPR
, TREE_TYPE (tem
), dest
);
8938 srctype
= TREE_TYPE (TREE_TYPE (src
));
8939 if (TREE_CODE (srctype
) == ARRAY_TYPE
8940 && !tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
8942 srctype
= TREE_TYPE (srctype
);
8944 src
= build1 (NOP_EXPR
, build_pointer_type (srctype
), src
);
8946 desttype
= TREE_TYPE (TREE_TYPE (dest
));
8947 if (TREE_CODE (desttype
) == ARRAY_TYPE
8948 && !tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
8950 desttype
= TREE_TYPE (desttype
);
8952 dest
= build1 (NOP_EXPR
, build_pointer_type (desttype
), dest
);
8954 if (TREE_ADDRESSABLE (srctype
)
8955 || TREE_ADDRESSABLE (desttype
))
8958 src_align
= get_pointer_alignment (src
);
8959 dest_align
= get_pointer_alignment (dest
);
8960 if (dest_align
< TYPE_ALIGN (desttype
)
8961 || src_align
< TYPE_ALIGN (srctype
))
8965 dest
= builtin_save_expr (dest
);
8967 /* Build accesses at offset zero with a ref-all character type. */
8968 off0
= build_int_cst (build_pointer_type_for_mode (char_type_node
,
8969 ptr_mode
, true), 0);
8972 STRIP_NOPS (destvar
);
8973 if (TREE_CODE (destvar
) == ADDR_EXPR
8974 && var_decl_component_p (TREE_OPERAND (destvar
, 0))
8975 && tree_int_cst_equal (TYPE_SIZE_UNIT (desttype
), len
))
8976 destvar
= fold_build2 (MEM_REF
, desttype
, destvar
, off0
);
8978 destvar
= NULL_TREE
;
8981 STRIP_NOPS (srcvar
);
8982 if (TREE_CODE (srcvar
) == ADDR_EXPR
8983 && var_decl_component_p (TREE_OPERAND (srcvar
, 0))
8984 && tree_int_cst_equal (TYPE_SIZE_UNIT (srctype
), len
))
8987 || src_align
>= TYPE_ALIGN (desttype
))
8988 srcvar
= fold_build2 (MEM_REF
, destvar
? desttype
: srctype
,
8990 else if (!STRICT_ALIGNMENT
)
8992 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
8994 srcvar
= fold_build2 (MEM_REF
, srctype
, srcvar
, off0
);
9002 if (srcvar
== NULL_TREE
&& destvar
== NULL_TREE
)
9005 if (srcvar
== NULL_TREE
)
9008 if (src_align
>= TYPE_ALIGN (desttype
))
9009 srcvar
= fold_build2 (MEM_REF
, desttype
, src
, off0
);
9012 if (STRICT_ALIGNMENT
)
9014 srctype
= build_aligned_type (TYPE_MAIN_VARIANT (desttype
),
9016 srcvar
= fold_build2 (MEM_REF
, srctype
, src
, off0
);
9019 else if (destvar
== NULL_TREE
)
9022 if (dest_align
>= TYPE_ALIGN (srctype
))
9023 destvar
= fold_build2 (MEM_REF
, srctype
, dest
, off0
);
9026 if (STRICT_ALIGNMENT
)
9028 desttype
= build_aligned_type (TYPE_MAIN_VARIANT (srctype
),
9030 destvar
= fold_build2 (MEM_REF
, desttype
, dest
, off0
);
9034 expr
= build2 (MODIFY_EXPR
, TREE_TYPE (destvar
), destvar
, srcvar
);
9040 if (endp
== 0 || endp
== 3)
9041 return omit_one_operand_loc (loc
, type
, dest
, expr
);
9047 len
= fold_build2_loc (loc
, MINUS_EXPR
, TREE_TYPE (len
), len
,
9050 dest
= fold_build_pointer_plus_loc (loc
, dest
, len
);
9051 dest
= fold_convert_loc (loc
, type
, dest
);
9053 dest
= omit_one_operand_loc (loc
, type
, dest
, expr
);
9057 /* Fold function call to builtin strcpy with arguments DEST and SRC.
9058 If LEN is not NULL, it represents the length of the string to be
9059 copied. Return NULL_TREE if no simplification can be made. */
9062 fold_builtin_strcpy (location_t loc
, tree fndecl
, tree dest
, tree src
, tree len
)
9066 if (!validate_arg (dest
, POINTER_TYPE
)
9067 || !validate_arg (src
, POINTER_TYPE
))
9070 /* If SRC and DEST are the same (and not volatile), return DEST. */
9071 if (operand_equal_p (src
, dest
, 0))
9072 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
);
9074 if (optimize_function_for_size_p (cfun
))
9077 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
9083 len
= c_strlen (src
, 1);
9084 if (! len
|| TREE_SIDE_EFFECTS (len
))
9088 len
= fold_convert_loc (loc
, size_type_node
, len
);
9089 len
= size_binop_loc (loc
, PLUS_EXPR
, len
, build_int_cst (size_type_node
, 1));
9090 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)),
9091 build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
));
9094 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
9095 Return NULL_TREE if no simplification can be made. */
9098 fold_builtin_stpcpy (location_t loc
, tree fndecl
, tree dest
, tree src
)
9100 tree fn
, len
, lenp1
, call
, type
;
9102 if (!validate_arg (dest
, POINTER_TYPE
)
9103 || !validate_arg (src
, POINTER_TYPE
))
9106 len
= c_strlen (src
, 1);
9108 || TREE_CODE (len
) != INTEGER_CST
)
9111 if (optimize_function_for_size_p (cfun
)
9112 /* If length is zero it's small enough. */
9113 && !integer_zerop (len
))
9116 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
9120 lenp1
= size_binop_loc (loc
, PLUS_EXPR
,
9121 fold_convert_loc (loc
, size_type_node
, len
),
9122 build_int_cst (size_type_node
, 1));
9123 /* We use dest twice in building our expression. Save it from
9124 multiple expansions. */
9125 dest
= builtin_save_expr (dest
);
9126 call
= build_call_expr_loc (loc
, fn
, 3, dest
, src
, lenp1
);
9128 type
= TREE_TYPE (TREE_TYPE (fndecl
));
9129 dest
= fold_build_pointer_plus_loc (loc
, dest
, len
);
9130 dest
= fold_convert_loc (loc
, type
, dest
);
9131 dest
= omit_one_operand_loc (loc
, type
, dest
, call
);
9135 /* Fold function call to builtin strncpy with arguments DEST, SRC, and LEN.
9136 If SLEN is not NULL, it represents the length of the source string.
9137 Return NULL_TREE if no simplification can be made. */
9140 fold_builtin_strncpy (location_t loc
, tree fndecl
, tree dest
,
9141 tree src
, tree len
, tree slen
)
9145 if (!validate_arg (dest
, POINTER_TYPE
)
9146 || !validate_arg (src
, POINTER_TYPE
)
9147 || !validate_arg (len
, INTEGER_TYPE
))
9150 /* If the LEN parameter is zero, return DEST. */
9151 if (integer_zerop (len
))
9152 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
9154 /* We can't compare slen with len as constants below if len is not a
9156 if (len
== 0 || TREE_CODE (len
) != INTEGER_CST
)
9160 slen
= c_strlen (src
, 1);
9162 /* Now, we must be passed a constant src ptr parameter. */
9163 if (slen
== 0 || TREE_CODE (slen
) != INTEGER_CST
)
9166 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
9168 /* We do not support simplification of this case, though we do
9169 support it when expanding trees into RTL. */
9170 /* FIXME: generate a call to __builtin_memset. */
9171 if (tree_int_cst_lt (slen
, len
))
9174 /* OK transform into builtin memcpy. */
9175 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
9179 len
= fold_convert_loc (loc
, size_type_node
, len
);
9180 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)),
9181 build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
));
9184 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
9185 arguments to the call, and TYPE is its return type.
9186 Return NULL_TREE if no simplification can be made. */
9189 fold_builtin_memchr (location_t loc
, tree arg1
, tree arg2
, tree len
, tree type
)
9191 if (!validate_arg (arg1
, POINTER_TYPE
)
9192 || !validate_arg (arg2
, INTEGER_TYPE
)
9193 || !validate_arg (len
, INTEGER_TYPE
))
9199 if (TREE_CODE (arg2
) != INTEGER_CST
9200 || !tree_fits_uhwi_p (len
))
9203 p1
= c_getstr (arg1
);
9204 if (p1
&& compare_tree_int (len
, strlen (p1
) + 1) <= 0)
9210 if (target_char_cast (arg2
, &c
))
9213 r
= (const char *) memchr (p1
, c
, tree_to_uhwi (len
));
9216 return build_int_cst (TREE_TYPE (arg1
), 0);
9218 tem
= fold_build_pointer_plus_hwi_loc (loc
, arg1
, r
- p1
);
9219 return fold_convert_loc (loc
, type
, tem
);
9225 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
9226 Return NULL_TREE if no simplification can be made. */
9229 fold_builtin_memcmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
9231 const char *p1
, *p2
;
9233 if (!validate_arg (arg1
, POINTER_TYPE
)
9234 || !validate_arg (arg2
, POINTER_TYPE
)
9235 || !validate_arg (len
, INTEGER_TYPE
))
9238 /* If the LEN parameter is zero, return zero. */
9239 if (integer_zerop (len
))
9240 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
9243 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9244 if (operand_equal_p (arg1
, arg2
, 0))
9245 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
9247 p1
= c_getstr (arg1
);
9248 p2
= c_getstr (arg2
);
9250 /* If all arguments are constant, and the value of len is not greater
9251 than the lengths of arg1 and arg2, evaluate at compile-time. */
9252 if (tree_fits_uhwi_p (len
) && p1
&& p2
9253 && compare_tree_int (len
, strlen (p1
) + 1) <= 0
9254 && compare_tree_int (len
, strlen (p2
) + 1) <= 0)
9256 const int r
= memcmp (p1
, p2
, tree_to_uhwi (len
));
9259 return integer_one_node
;
9261 return integer_minus_one_node
;
9263 return integer_zero_node
;
9266 /* If len parameter is one, return an expression corresponding to
9267 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9268 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
9270 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9271 tree cst_uchar_ptr_node
9272 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9275 = fold_convert_loc (loc
, integer_type_node
,
9276 build1 (INDIRECT_REF
, cst_uchar_node
,
9277 fold_convert_loc (loc
,
9281 = fold_convert_loc (loc
, integer_type_node
,
9282 build1 (INDIRECT_REF
, cst_uchar_node
,
9283 fold_convert_loc (loc
,
9286 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
9292 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
9293 Return NULL_TREE if no simplification can be made. */
9296 fold_builtin_strcmp (location_t loc
, tree arg1
, tree arg2
)
9298 const char *p1
, *p2
;
9300 if (!validate_arg (arg1
, POINTER_TYPE
)
9301 || !validate_arg (arg2
, POINTER_TYPE
))
9304 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9305 if (operand_equal_p (arg1
, arg2
, 0))
9306 return integer_zero_node
;
9308 p1
= c_getstr (arg1
);
9309 p2
= c_getstr (arg2
);
9313 const int i
= strcmp (p1
, p2
);
9315 return integer_minus_one_node
;
9317 return integer_one_node
;
9319 return integer_zero_node
;
9322 /* If the second arg is "", return *(const unsigned char*)arg1. */
9323 if (p2
&& *p2
== '\0')
9325 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9326 tree cst_uchar_ptr_node
9327 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9329 return fold_convert_loc (loc
, integer_type_node
,
9330 build1 (INDIRECT_REF
, cst_uchar_node
,
9331 fold_convert_loc (loc
,
9336 /* If the first arg is "", return -*(const unsigned char*)arg2. */
9337 if (p1
&& *p1
== '\0')
9339 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9340 tree cst_uchar_ptr_node
9341 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9344 = fold_convert_loc (loc
, integer_type_node
,
9345 build1 (INDIRECT_REF
, cst_uchar_node
,
9346 fold_convert_loc (loc
,
9349 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
9355 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
9356 Return NULL_TREE if no simplification can be made. */
9359 fold_builtin_strncmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
9361 const char *p1
, *p2
;
9363 if (!validate_arg (arg1
, POINTER_TYPE
)
9364 || !validate_arg (arg2
, POINTER_TYPE
)
9365 || !validate_arg (len
, INTEGER_TYPE
))
9368 /* If the LEN parameter is zero, return zero. */
9369 if (integer_zerop (len
))
9370 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
9373 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
9374 if (operand_equal_p (arg1
, arg2
, 0))
9375 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
9377 p1
= c_getstr (arg1
);
9378 p2
= c_getstr (arg2
);
9380 if (tree_fits_uhwi_p (len
) && p1
&& p2
)
9382 const int i
= strncmp (p1
, p2
, tree_to_uhwi (len
));
9384 return integer_one_node
;
9386 return integer_minus_one_node
;
9388 return integer_zero_node
;
9391 /* If the second arg is "", and the length is greater than zero,
9392 return *(const unsigned char*)arg1. */
9393 if (p2
&& *p2
== '\0'
9394 && TREE_CODE (len
) == INTEGER_CST
9395 && tree_int_cst_sgn (len
) == 1)
9397 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9398 tree cst_uchar_ptr_node
9399 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9401 return fold_convert_loc (loc
, integer_type_node
,
9402 build1 (INDIRECT_REF
, cst_uchar_node
,
9403 fold_convert_loc (loc
,
9408 /* If the first arg is "", and the length is greater than zero,
9409 return -*(const unsigned char*)arg2. */
9410 if (p1
&& *p1
== '\0'
9411 && TREE_CODE (len
) == INTEGER_CST
9412 && tree_int_cst_sgn (len
) == 1)
9414 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9415 tree cst_uchar_ptr_node
9416 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9418 tree temp
= fold_convert_loc (loc
, integer_type_node
,
9419 build1 (INDIRECT_REF
, cst_uchar_node
,
9420 fold_convert_loc (loc
,
9423 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
9426 /* If len parameter is one, return an expression corresponding to
9427 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
9428 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
9430 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
9431 tree cst_uchar_ptr_node
9432 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
9434 tree ind1
= fold_convert_loc (loc
, integer_type_node
,
9435 build1 (INDIRECT_REF
, cst_uchar_node
,
9436 fold_convert_loc (loc
,
9439 tree ind2
= fold_convert_loc (loc
, integer_type_node
,
9440 build1 (INDIRECT_REF
, cst_uchar_node
,
9441 fold_convert_loc (loc
,
9444 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
9450 /* Fold function call to builtin signbit, signbitf or signbitl with argument
9451 ARG. Return NULL_TREE if no simplification can be made. */
9454 fold_builtin_signbit (location_t loc
, tree arg
, tree type
)
9456 if (!validate_arg (arg
, REAL_TYPE
))
9459 /* If ARG is a compile-time constant, determine the result. */
9460 if (TREE_CODE (arg
) == REAL_CST
9461 && !TREE_OVERFLOW (arg
))
9465 c
= TREE_REAL_CST (arg
);
9466 return (REAL_VALUE_NEGATIVE (c
)
9467 ? build_one_cst (type
)
9468 : build_zero_cst (type
));
9471 /* If ARG is non-negative, the result is always zero. */
9472 if (tree_expr_nonnegative_p (arg
))
9473 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9475 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9476 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg
))))
9477 return fold_convert (type
,
9478 fold_build2_loc (loc
, LT_EXPR
, boolean_type_node
, arg
,
9479 build_real (TREE_TYPE (arg
), dconst0
)));
9484 /* Fold function call to builtin copysign, copysignf or copysignl with
9485 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9489 fold_builtin_copysign (location_t loc
, tree fndecl
,
9490 tree arg1
, tree arg2
, tree type
)
9494 if (!validate_arg (arg1
, REAL_TYPE
)
9495 || !validate_arg (arg2
, REAL_TYPE
))
9498 /* copysign(X,X) is X. */
9499 if (operand_equal_p (arg1
, arg2
, 0))
9500 return fold_convert_loc (loc
, type
, arg1
);
9502 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9503 if (TREE_CODE (arg1
) == REAL_CST
9504 && TREE_CODE (arg2
) == REAL_CST
9505 && !TREE_OVERFLOW (arg1
)
9506 && !TREE_OVERFLOW (arg2
))
9508 REAL_VALUE_TYPE c1
, c2
;
9510 c1
= TREE_REAL_CST (arg1
);
9511 c2
= TREE_REAL_CST (arg2
);
9512 /* c1.sign := c2.sign. */
9513 real_copysign (&c1
, &c2
);
9514 return build_real (type
, c1
);
9517 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9518 Remember to evaluate Y for side-effects. */
9519 if (tree_expr_nonnegative_p (arg2
))
9520 return omit_one_operand_loc (loc
, type
,
9521 fold_build1_loc (loc
, ABS_EXPR
, type
, arg1
),
9524 /* Strip sign changing operations for the first argument. */
9525 tem
= fold_strip_sign_ops (arg1
);
9527 return build_call_expr_loc (loc
, fndecl
, 2, tem
, arg2
);
9532 /* Fold a call to builtin isascii with argument ARG. */
9535 fold_builtin_isascii (location_t loc
, tree arg
)
9537 if (!validate_arg (arg
, INTEGER_TYPE
))
9541 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9542 arg
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
9543 build_int_cst (integer_type_node
,
9544 ~ (unsigned HOST_WIDE_INT
) 0x7f));
9545 return fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
,
9546 arg
, integer_zero_node
);
9550 /* Fold a call to builtin toascii with argument ARG. */
9553 fold_builtin_toascii (location_t loc
, tree arg
)
9555 if (!validate_arg (arg
, INTEGER_TYPE
))
9558 /* Transform toascii(c) -> (c & 0x7f). */
9559 return fold_build2_loc (loc
, BIT_AND_EXPR
, integer_type_node
, arg
,
9560 build_int_cst (integer_type_node
, 0x7f));
9563 /* Fold a call to builtin isdigit with argument ARG. */
9566 fold_builtin_isdigit (location_t loc
, tree arg
)
9568 if (!validate_arg (arg
, INTEGER_TYPE
))
9572 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9573 /* According to the C standard, isdigit is unaffected by locale.
9574 However, it definitely is affected by the target character set. */
9575 unsigned HOST_WIDE_INT target_digit0
9576 = lang_hooks
.to_target_charset ('0');
9578 if (target_digit0
== 0)
9581 arg
= fold_convert_loc (loc
, unsigned_type_node
, arg
);
9582 arg
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
9583 build_int_cst (unsigned_type_node
, target_digit0
));
9584 return fold_build2_loc (loc
, LE_EXPR
, integer_type_node
, arg
,
9585 build_int_cst (unsigned_type_node
, 9));
9589 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9592 fold_builtin_fabs (location_t loc
, tree arg
, tree type
)
9594 if (!validate_arg (arg
, REAL_TYPE
))
9597 arg
= fold_convert_loc (loc
, type
, arg
);
9598 if (TREE_CODE (arg
) == REAL_CST
)
9599 return fold_abs_const (arg
, type
);
9600 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
9603 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9606 fold_builtin_abs (location_t loc
, tree arg
, tree type
)
9608 if (!validate_arg (arg
, INTEGER_TYPE
))
9611 arg
= fold_convert_loc (loc
, type
, arg
);
9612 if (TREE_CODE (arg
) == INTEGER_CST
)
9613 return fold_abs_const (arg
, type
);
9614 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
9617 /* Fold a fma operation with arguments ARG[012]. */
9620 fold_fma (location_t loc ATTRIBUTE_UNUSED
,
9621 tree type
, tree arg0
, tree arg1
, tree arg2
)
9623 if (TREE_CODE (arg0
) == REAL_CST
9624 && TREE_CODE (arg1
) == REAL_CST
9625 && TREE_CODE (arg2
) == REAL_CST
)
9626 return do_mpfr_arg3 (arg0
, arg1
, arg2
, type
, mpfr_fma
);
9631 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9634 fold_builtin_fma (location_t loc
, tree arg0
, tree arg1
, tree arg2
, tree type
)
9636 if (validate_arg (arg0
, REAL_TYPE
)
9637 && validate_arg (arg1
, REAL_TYPE
)
9638 && validate_arg (arg2
, REAL_TYPE
))
9640 tree tem
= fold_fma (loc
, type
, arg0
, arg1
, arg2
);
9644 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9645 if (optab_handler (fma_optab
, TYPE_MODE (type
)) != CODE_FOR_nothing
)
9646 return fold_build3_loc (loc
, FMA_EXPR
, type
, arg0
, arg1
, arg2
);
9651 /* Fold a call to builtin fmin or fmax. */
9654 fold_builtin_fmin_fmax (location_t loc
, tree arg0
, tree arg1
,
9655 tree type
, bool max
)
9657 if (validate_arg (arg0
, REAL_TYPE
) && validate_arg (arg1
, REAL_TYPE
))
9659 /* Calculate the result when the argument is a constant. */
9660 tree res
= do_mpfr_arg2 (arg0
, arg1
, type
, (max
? mpfr_max
: mpfr_min
));
9665 /* If either argument is NaN, return the other one. Avoid the
9666 transformation if we get (and honor) a signalling NaN. Using
9667 omit_one_operand() ensures we create a non-lvalue. */
9668 if (TREE_CODE (arg0
) == REAL_CST
9669 && real_isnan (&TREE_REAL_CST (arg0
))
9670 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
9671 || ! TREE_REAL_CST (arg0
).signalling
))
9672 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
9673 if (TREE_CODE (arg1
) == REAL_CST
9674 && real_isnan (&TREE_REAL_CST (arg1
))
9675 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
9676 || ! TREE_REAL_CST (arg1
).signalling
))
9677 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9679 /* Transform fmin/fmax(x,x) -> x. */
9680 if (operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
9681 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9683 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9684 functions to return the numeric arg if the other one is NaN.
9685 These tree codes don't honor that, so only transform if
9686 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9687 handled, so we don't have to worry about it either. */
9688 if (flag_finite_math_only
)
9689 return fold_build2_loc (loc
, (max
? MAX_EXPR
: MIN_EXPR
), type
,
9690 fold_convert_loc (loc
, type
, arg0
),
9691 fold_convert_loc (loc
, type
, arg1
));
9696 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9699 fold_builtin_carg (location_t loc
, tree arg
, tree type
)
9701 if (validate_arg (arg
, COMPLEX_TYPE
)
9702 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
9704 tree atan2_fn
= mathfn_built_in (type
, BUILT_IN_ATAN2
);
9708 tree new_arg
= builtin_save_expr (arg
);
9709 tree r_arg
= fold_build1_loc (loc
, REALPART_EXPR
, type
, new_arg
);
9710 tree i_arg
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, new_arg
);
9711 return build_call_expr_loc (loc
, atan2_fn
, 2, i_arg
, r_arg
);
9718 /* Fold a call to builtin logb/ilogb. */
9721 fold_builtin_logb (location_t loc
, tree arg
, tree rettype
)
9723 if (! validate_arg (arg
, REAL_TYPE
))
9728 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_OVERFLOW (arg
))
9730 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg
);
9736 /* If arg is Inf or NaN and we're logb, return it. */
9737 if (TREE_CODE (rettype
) == REAL_TYPE
)
9739 /* For logb(-Inf) we have to return +Inf. */
9740 if (real_isinf (value
) && real_isneg (value
))
9742 REAL_VALUE_TYPE tem
;
9744 return build_real (rettype
, tem
);
9746 return fold_convert_loc (loc
, rettype
, arg
);
9748 /* Fall through... */
9750 /* Zero may set errno and/or raise an exception for logb, also
9751 for ilogb we don't know FP_ILOGB0. */
9754 /* For normal numbers, proceed iff radix == 2. In GCC,
9755 normalized significands are in the range [0.5, 1.0). We
9756 want the exponent as if they were [1.0, 2.0) so get the
9757 exponent and subtract 1. */
9758 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg
)))->b
== 2)
9759 return fold_convert_loc (loc
, rettype
,
9760 build_int_cst (integer_type_node
,
9761 REAL_EXP (value
)-1));
9769 /* Fold a call to builtin significand, if radix == 2. */
9772 fold_builtin_significand (location_t loc
, tree arg
, tree rettype
)
9774 if (! validate_arg (arg
, REAL_TYPE
))
9779 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_OVERFLOW (arg
))
9781 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg
);
9788 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9789 return fold_convert_loc (loc
, rettype
, arg
);
9791 /* For normal numbers, proceed iff radix == 2. */
9792 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg
)))->b
== 2)
9794 REAL_VALUE_TYPE result
= *value
;
9795 /* In GCC, normalized significands are in the range [0.5,
9796 1.0). We want them to be [1.0, 2.0) so set the
9798 SET_REAL_EXP (&result
, 1);
9799 return build_real (rettype
, result
);
9808 /* Fold a call to builtin frexp, we can assume the base is 2. */
9811 fold_builtin_frexp (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
)) == integer_type_node
)
9826 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9832 /* For +-0, return (*exp = 0, +-0). */
9833 exp
= integer_zero_node
;
9838 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9839 return omit_one_operand_loc (loc
, rettype
, arg0
, arg1
);
9842 /* Since the frexp function always expects base 2, and in
9843 GCC normalized significands are already in the range
9844 [0.5, 1.0), we have exactly what frexp wants. */
9845 REAL_VALUE_TYPE frac_rvt
= *value
;
9846 SET_REAL_EXP (&frac_rvt
, 0);
9847 frac
= build_real (rettype
, frac_rvt
);
9848 exp
= build_int_cst (integer_type_node
, REAL_EXP (value
));
9855 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9856 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
, exp
);
9857 TREE_SIDE_EFFECTS (arg1
) = 1;
9858 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
, frac
);
9864 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9865 then we can assume the base is two. If it's false, then we have to
9866 check the mode of the TYPE parameter in certain cases. */
9869 fold_builtin_load_exponent (location_t loc
, tree arg0
, tree arg1
,
9870 tree type
, bool ldexp
)
9872 if (validate_arg (arg0
, REAL_TYPE
) && validate_arg (arg1
, INTEGER_TYPE
))
9877 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9878 if (real_zerop (arg0
) || integer_zerop (arg1
)
9879 || (TREE_CODE (arg0
) == REAL_CST
9880 && !real_isfinite (&TREE_REAL_CST (arg0
))))
9881 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9883 /* If both arguments are constant, then try to evaluate it. */
9884 if ((ldexp
|| REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2)
9885 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
9886 && tree_fits_shwi_p (arg1
))
9888 /* Bound the maximum adjustment to twice the range of the
9889 mode's valid exponents. Use abs to ensure the range is
9890 positive as a sanity check. */
9891 const long max_exp_adj
= 2 *
9892 labs (REAL_MODE_FORMAT (TYPE_MODE (type
))->emax
9893 - REAL_MODE_FORMAT (TYPE_MODE (type
))->emin
);
9895 /* Get the user-requested adjustment. */
9896 const HOST_WIDE_INT req_exp_adj
= tree_to_shwi (arg1
);
9898 /* The requested adjustment must be inside this range. This
9899 is a preliminary cap to avoid things like overflow, we
9900 may still fail to compute the result for other reasons. */
9901 if (-max_exp_adj
< req_exp_adj
&& req_exp_adj
< max_exp_adj
)
9903 REAL_VALUE_TYPE initial_result
;
9905 real_ldexp (&initial_result
, &TREE_REAL_CST (arg0
), req_exp_adj
);
9907 /* Ensure we didn't overflow. */
9908 if (! real_isinf (&initial_result
))
9910 const REAL_VALUE_TYPE trunc_result
9911 = real_value_truncate (TYPE_MODE (type
), initial_result
);
9913 /* Only proceed if the target mode can hold the
9915 if (REAL_VALUES_EQUAL (initial_result
, trunc_result
))
9916 return build_real (type
, trunc_result
);
9925 /* Fold a call to builtin modf. */
9928 fold_builtin_modf (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
9930 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
9935 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
9938 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
9940 /* Proceed if a valid pointer type was passed in. */
9941 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == TYPE_MAIN_VARIANT (rettype
))
9943 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9944 REAL_VALUE_TYPE trunc
, frac
;
9950 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9951 trunc
= frac
= *value
;
9954 /* For +-Inf, return (*arg1 = arg0, +-0). */
9956 frac
.sign
= value
->sign
;
9960 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9961 real_trunc (&trunc
, VOIDmode
, value
);
9962 real_arithmetic (&frac
, MINUS_EXPR
, value
, &trunc
);
9963 /* If the original number was negative and already
9964 integral, then the fractional part is -0.0. */
9965 if (value
->sign
&& frac
.cl
== rvc_zero
)
9966 frac
.sign
= value
->sign
;
9970 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9971 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
,
9972 build_real (rettype
, trunc
));
9973 TREE_SIDE_EFFECTS (arg1
) = 1;
9974 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
,
9975 build_real (rettype
, frac
));
9981 /* Given a location LOC, an interclass builtin function decl FNDECL
9982 and its single argument ARG, return an folded expression computing
9983 the same, or NULL_TREE if we either couldn't or didn't want to fold
9984 (the latter happen if there's an RTL instruction available). */
9987 fold_builtin_interclass_mathfn (location_t loc
, tree fndecl
, tree arg
)
9989 enum machine_mode mode
;
9991 if (!validate_arg (arg
, REAL_TYPE
))
9994 if (interclass_mathfn_icode (arg
, fndecl
) != CODE_FOR_nothing
)
9997 mode
= TYPE_MODE (TREE_TYPE (arg
));
9999 /* If there is no optab, try generic code. */
10000 switch (DECL_FUNCTION_CODE (fndecl
))
10004 CASE_FLT_FN (BUILT_IN_ISINF
):
10006 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
10007 tree
const isgr_fn
= builtin_decl_explicit (BUILT_IN_ISGREATER
);
10008 tree
const type
= TREE_TYPE (arg
);
10012 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
10013 real_from_string (&r
, buf
);
10014 result
= build_call_expr (isgr_fn
, 2,
10015 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
10016 build_real (type
, r
));
10019 CASE_FLT_FN (BUILT_IN_FINITE
):
10020 case BUILT_IN_ISFINITE
:
10022 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
10023 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
10024 tree
const type
= TREE_TYPE (arg
);
10028 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
10029 real_from_string (&r
, buf
);
10030 result
= build_call_expr (isle_fn
, 2,
10031 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
10032 build_real (type
, r
));
10033 /*result = fold_build2_loc (loc, UNGT_EXPR,
10034 TREE_TYPE (TREE_TYPE (fndecl)),
10035 fold_build1_loc (loc, ABS_EXPR, type, arg),
10036 build_real (type, r));
10037 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
10038 TREE_TYPE (TREE_TYPE (fndecl)),
10042 case BUILT_IN_ISNORMAL
:
10044 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
10045 islessequal(fabs(x),DBL_MAX). */
10046 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
10047 tree
const isge_fn
= builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL
);
10048 tree
const type
= TREE_TYPE (arg
);
10049 REAL_VALUE_TYPE rmax
, rmin
;
10052 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
10053 real_from_string (&rmax
, buf
);
10054 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
10055 real_from_string (&rmin
, buf
);
10056 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
10057 result
= build_call_expr (isle_fn
, 2, arg
,
10058 build_real (type
, rmax
));
10059 result
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, result
,
10060 build_call_expr (isge_fn
, 2, arg
,
10061 build_real (type
, rmin
)));
10071 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
10072 ARG is the argument for the call. */
10075 fold_builtin_classify (location_t loc
, tree fndecl
, tree arg
, int builtin_index
)
10077 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10080 if (!validate_arg (arg
, REAL_TYPE
))
10083 switch (builtin_index
)
10085 case BUILT_IN_ISINF
:
10086 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
10087 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
10089 if (TREE_CODE (arg
) == REAL_CST
)
10091 r
= TREE_REAL_CST (arg
);
10092 if (real_isinf (&r
))
10093 return real_compare (GT_EXPR
, &r
, &dconst0
)
10094 ? integer_one_node
: integer_minus_one_node
;
10096 return integer_zero_node
;
10101 case BUILT_IN_ISINF_SIGN
:
10103 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
10104 /* In a boolean context, GCC will fold the inner COND_EXPR to
10105 1. So e.g. "if (isinf_sign(x))" would be folded to just
10106 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
10107 tree signbit_fn
= mathfn_built_in_1 (TREE_TYPE (arg
), BUILT_IN_SIGNBIT
, 0);
10108 tree isinf_fn
= builtin_decl_explicit (BUILT_IN_ISINF
);
10109 tree tmp
= NULL_TREE
;
10111 arg
= builtin_save_expr (arg
);
10113 if (signbit_fn
&& isinf_fn
)
10115 tree signbit_call
= build_call_expr_loc (loc
, signbit_fn
, 1, arg
);
10116 tree isinf_call
= build_call_expr_loc (loc
, isinf_fn
, 1, arg
);
10118 signbit_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
10119 signbit_call
, integer_zero_node
);
10120 isinf_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
10121 isinf_call
, integer_zero_node
);
10123 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, signbit_call
,
10124 integer_minus_one_node
, integer_one_node
);
10125 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
10127 integer_zero_node
);
10133 case BUILT_IN_ISFINITE
:
10134 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg
)))
10135 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
10136 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
10138 if (TREE_CODE (arg
) == REAL_CST
)
10140 r
= TREE_REAL_CST (arg
);
10141 return real_isfinite (&r
) ? integer_one_node
: integer_zero_node
;
10146 case BUILT_IN_ISNAN
:
10147 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg
))))
10148 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
10150 if (TREE_CODE (arg
) == REAL_CST
)
10152 r
= TREE_REAL_CST (arg
);
10153 return real_isnan (&r
) ? integer_one_node
: integer_zero_node
;
10156 arg
= builtin_save_expr (arg
);
10157 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg
, arg
);
10160 gcc_unreachable ();
10164 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
10165 This builtin will generate code to return the appropriate floating
10166 point classification depending on the value of the floating point
10167 number passed in. The possible return values must be supplied as
10168 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
10169 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
10170 one floating point argument which is "type generic". */
10173 fold_builtin_fpclassify (location_t loc
, tree exp
)
10175 tree fp_nan
, fp_infinite
, fp_normal
, fp_subnormal
, fp_zero
,
10176 arg
, type
, res
, tmp
;
10177 enum machine_mode mode
;
10181 /* Verify the required arguments in the original call. */
10182 if (!validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
,
10183 INTEGER_TYPE
, INTEGER_TYPE
,
10184 INTEGER_TYPE
, REAL_TYPE
, VOID_TYPE
))
10187 fp_nan
= CALL_EXPR_ARG (exp
, 0);
10188 fp_infinite
= CALL_EXPR_ARG (exp
, 1);
10189 fp_normal
= CALL_EXPR_ARG (exp
, 2);
10190 fp_subnormal
= CALL_EXPR_ARG (exp
, 3);
10191 fp_zero
= CALL_EXPR_ARG (exp
, 4);
10192 arg
= CALL_EXPR_ARG (exp
, 5);
10193 type
= TREE_TYPE (arg
);
10194 mode
= TYPE_MODE (type
);
10195 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
10197 /* fpclassify(x) ->
10198 isnan(x) ? FP_NAN :
10199 (fabs(x) == Inf ? FP_INFINITE :
10200 (fabs(x) >= DBL_MIN ? FP_NORMAL :
10201 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
10203 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
10204 build_real (type
, dconst0
));
10205 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
10206 tmp
, fp_zero
, fp_subnormal
);
10208 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
10209 real_from_string (&r
, buf
);
10210 tmp
= fold_build2_loc (loc
, GE_EXPR
, integer_type_node
,
10211 arg
, build_real (type
, r
));
10212 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, fp_normal
, res
);
10214 if (HONOR_INFINITIES (mode
))
10217 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
10218 build_real (type
, r
));
10219 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
,
10223 if (HONOR_NANS (mode
))
10225 tmp
= fold_build2_loc (loc
, ORDERED_EXPR
, integer_type_node
, arg
, arg
);
10226 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, res
, fp_nan
);
10232 /* Fold a call to an unordered comparison function such as
10233 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
10234 being called and ARG0 and ARG1 are the arguments for the call.
10235 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
10236 the opposite of the desired result. UNORDERED_CODE is used
10237 for modes that can hold NaNs and ORDERED_CODE is used for
10241 fold_builtin_unordered_cmp (location_t loc
, tree fndecl
, tree arg0
, tree arg1
,
10242 enum tree_code unordered_code
,
10243 enum tree_code ordered_code
)
10245 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10246 enum tree_code code
;
10248 enum tree_code code0
, code1
;
10249 tree cmp_type
= NULL_TREE
;
10251 type0
= TREE_TYPE (arg0
);
10252 type1
= TREE_TYPE (arg1
);
10254 code0
= TREE_CODE (type0
);
10255 code1
= TREE_CODE (type1
);
10257 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
10258 /* Choose the wider of two real types. */
10259 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
10261 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
10263 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
10266 arg0
= fold_convert_loc (loc
, cmp_type
, arg0
);
10267 arg1
= fold_convert_loc (loc
, cmp_type
, arg1
);
10269 if (unordered_code
== UNORDERED_EXPR
)
10271 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
10272 return omit_two_operands_loc (loc
, type
, integer_zero_node
, arg0
, arg1
);
10273 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg0
, arg1
);
10276 code
= HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))) ? unordered_code
10278 return fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
,
10279 fold_build2_loc (loc
, code
, type
, arg0
, arg1
));
10282 /* Fold a call to built-in function FNDECL with 0 arguments.
10283 IGNORE is true if the result of the function call is ignored. This
10284 function returns NULL_TREE if no simplification was possible. */
10287 fold_builtin_0 (location_t loc
, tree fndecl
, bool ignore ATTRIBUTE_UNUSED
)
10289 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10290 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10293 CASE_FLT_FN (BUILT_IN_INF
):
10294 case BUILT_IN_INFD32
:
10295 case BUILT_IN_INFD64
:
10296 case BUILT_IN_INFD128
:
10297 return fold_builtin_inf (loc
, type
, true);
10299 CASE_FLT_FN (BUILT_IN_HUGE_VAL
):
10300 return fold_builtin_inf (loc
, type
, false);
10302 case BUILT_IN_CLASSIFY_TYPE
:
10303 return fold_builtin_classify_type (NULL_TREE
);
10305 case BUILT_IN_UNREACHABLE
:
10306 if (flag_sanitize
& SANITIZE_UNREACHABLE
10307 && (current_function_decl
== NULL
10308 || !lookup_attribute ("no_sanitize_undefined",
10309 DECL_ATTRIBUTES (current_function_decl
))))
10310 return ubsan_instrument_unreachable (loc
);
10319 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
10320 IGNORE is true if the result of the function call is ignored. This
10321 function returns NULL_TREE if no simplification was possible. */
10324 fold_builtin_1 (location_t loc
, tree fndecl
, tree arg0
, bool ignore
)
10326 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10327 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10330 case BUILT_IN_CONSTANT_P
:
10332 tree val
= fold_builtin_constant_p (arg0
);
10334 /* Gimplification will pull the CALL_EXPR for the builtin out of
10335 an if condition. When not optimizing, we'll not CSE it back.
10336 To avoid link error types of regressions, return false now. */
10337 if (!val
&& !optimize
)
10338 val
= integer_zero_node
;
10343 case BUILT_IN_CLASSIFY_TYPE
:
10344 return fold_builtin_classify_type (arg0
);
10346 case BUILT_IN_STRLEN
:
10347 return fold_builtin_strlen (loc
, type
, arg0
);
10349 CASE_FLT_FN (BUILT_IN_FABS
):
10350 case BUILT_IN_FABSD32
:
10351 case BUILT_IN_FABSD64
:
10352 case BUILT_IN_FABSD128
:
10353 return fold_builtin_fabs (loc
, arg0
, type
);
10356 case BUILT_IN_LABS
:
10357 case BUILT_IN_LLABS
:
10358 case BUILT_IN_IMAXABS
:
10359 return fold_builtin_abs (loc
, arg0
, type
);
10361 CASE_FLT_FN (BUILT_IN_CONJ
):
10362 if (validate_arg (arg0
, COMPLEX_TYPE
)
10363 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10364 return fold_build1_loc (loc
, CONJ_EXPR
, type
, arg0
);
10367 CASE_FLT_FN (BUILT_IN_CREAL
):
10368 if (validate_arg (arg0
, COMPLEX_TYPE
)
10369 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10370 return non_lvalue_loc (loc
, fold_build1_loc (loc
, REALPART_EXPR
, type
, arg0
));;
10373 CASE_FLT_FN (BUILT_IN_CIMAG
):
10374 if (validate_arg (arg0
, COMPLEX_TYPE
)
10375 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10376 return non_lvalue_loc (loc
, fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg0
));
10379 CASE_FLT_FN (BUILT_IN_CCOS
):
10380 return fold_builtin_ccos (loc
, arg0
, type
, fndecl
, /*hyper=*/ false);
10382 CASE_FLT_FN (BUILT_IN_CCOSH
):
10383 return fold_builtin_ccos (loc
, arg0
, type
, fndecl
, /*hyper=*/ true);
10385 CASE_FLT_FN (BUILT_IN_CPROJ
):
10386 return fold_builtin_cproj (loc
, arg0
, type
);
10388 CASE_FLT_FN (BUILT_IN_CSIN
):
10389 if (validate_arg (arg0
, COMPLEX_TYPE
)
10390 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10391 return do_mpc_arg1 (arg0
, type
, mpc_sin
);
10394 CASE_FLT_FN (BUILT_IN_CSINH
):
10395 if (validate_arg (arg0
, COMPLEX_TYPE
)
10396 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10397 return do_mpc_arg1 (arg0
, type
, mpc_sinh
);
10400 CASE_FLT_FN (BUILT_IN_CTAN
):
10401 if (validate_arg (arg0
, COMPLEX_TYPE
)
10402 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10403 return do_mpc_arg1 (arg0
, type
, mpc_tan
);
10406 CASE_FLT_FN (BUILT_IN_CTANH
):
10407 if (validate_arg (arg0
, COMPLEX_TYPE
)
10408 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10409 return do_mpc_arg1 (arg0
, type
, mpc_tanh
);
10412 CASE_FLT_FN (BUILT_IN_CLOG
):
10413 if (validate_arg (arg0
, COMPLEX_TYPE
)
10414 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10415 return do_mpc_arg1 (arg0
, type
, mpc_log
);
10418 CASE_FLT_FN (BUILT_IN_CSQRT
):
10419 if (validate_arg (arg0
, COMPLEX_TYPE
)
10420 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10421 return do_mpc_arg1 (arg0
, type
, mpc_sqrt
);
10424 CASE_FLT_FN (BUILT_IN_CASIN
):
10425 if (validate_arg (arg0
, COMPLEX_TYPE
)
10426 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10427 return do_mpc_arg1 (arg0
, type
, mpc_asin
);
10430 CASE_FLT_FN (BUILT_IN_CACOS
):
10431 if (validate_arg (arg0
, COMPLEX_TYPE
)
10432 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10433 return do_mpc_arg1 (arg0
, type
, mpc_acos
);
10436 CASE_FLT_FN (BUILT_IN_CATAN
):
10437 if (validate_arg (arg0
, COMPLEX_TYPE
)
10438 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10439 return do_mpc_arg1 (arg0
, type
, mpc_atan
);
10442 CASE_FLT_FN (BUILT_IN_CASINH
):
10443 if (validate_arg (arg0
, COMPLEX_TYPE
)
10444 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10445 return do_mpc_arg1 (arg0
, type
, mpc_asinh
);
10448 CASE_FLT_FN (BUILT_IN_CACOSH
):
10449 if (validate_arg (arg0
, COMPLEX_TYPE
)
10450 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10451 return do_mpc_arg1 (arg0
, type
, mpc_acosh
);
10454 CASE_FLT_FN (BUILT_IN_CATANH
):
10455 if (validate_arg (arg0
, COMPLEX_TYPE
)
10456 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10457 return do_mpc_arg1 (arg0
, type
, mpc_atanh
);
10460 CASE_FLT_FN (BUILT_IN_CABS
):
10461 return fold_builtin_cabs (loc
, arg0
, type
, fndecl
);
10463 CASE_FLT_FN (BUILT_IN_CARG
):
10464 return fold_builtin_carg (loc
, arg0
, type
);
10466 CASE_FLT_FN (BUILT_IN_SQRT
):
10467 return fold_builtin_sqrt (loc
, arg0
, type
);
10469 CASE_FLT_FN (BUILT_IN_CBRT
):
10470 return fold_builtin_cbrt (loc
, arg0
, type
);
10472 CASE_FLT_FN (BUILT_IN_ASIN
):
10473 if (validate_arg (arg0
, REAL_TYPE
))
10474 return do_mpfr_arg1 (arg0
, type
, mpfr_asin
,
10475 &dconstm1
, &dconst1
, true);
10478 CASE_FLT_FN (BUILT_IN_ACOS
):
10479 if (validate_arg (arg0
, REAL_TYPE
))
10480 return do_mpfr_arg1 (arg0
, type
, mpfr_acos
,
10481 &dconstm1
, &dconst1
, true);
10484 CASE_FLT_FN (BUILT_IN_ATAN
):
10485 if (validate_arg (arg0
, REAL_TYPE
))
10486 return do_mpfr_arg1 (arg0
, type
, mpfr_atan
, NULL
, NULL
, 0);
10489 CASE_FLT_FN (BUILT_IN_ASINH
):
10490 if (validate_arg (arg0
, REAL_TYPE
))
10491 return do_mpfr_arg1 (arg0
, type
, mpfr_asinh
, NULL
, NULL
, 0);
10494 CASE_FLT_FN (BUILT_IN_ACOSH
):
10495 if (validate_arg (arg0
, REAL_TYPE
))
10496 return do_mpfr_arg1 (arg0
, type
, mpfr_acosh
,
10497 &dconst1
, NULL
, true);
10500 CASE_FLT_FN (BUILT_IN_ATANH
):
10501 if (validate_arg (arg0
, REAL_TYPE
))
10502 return do_mpfr_arg1 (arg0
, type
, mpfr_atanh
,
10503 &dconstm1
, &dconst1
, false);
10506 CASE_FLT_FN (BUILT_IN_SIN
):
10507 if (validate_arg (arg0
, REAL_TYPE
))
10508 return do_mpfr_arg1 (arg0
, type
, mpfr_sin
, NULL
, NULL
, 0);
10511 CASE_FLT_FN (BUILT_IN_COS
):
10512 return fold_builtin_cos (loc
, arg0
, type
, fndecl
);
10514 CASE_FLT_FN (BUILT_IN_TAN
):
10515 return fold_builtin_tan (arg0
, type
);
10517 CASE_FLT_FN (BUILT_IN_CEXP
):
10518 return fold_builtin_cexp (loc
, arg0
, type
);
10520 CASE_FLT_FN (BUILT_IN_CEXPI
):
10521 if (validate_arg (arg0
, REAL_TYPE
))
10522 return do_mpfr_sincos (arg0
, NULL_TREE
, NULL_TREE
);
10525 CASE_FLT_FN (BUILT_IN_SINH
):
10526 if (validate_arg (arg0
, REAL_TYPE
))
10527 return do_mpfr_arg1 (arg0
, type
, mpfr_sinh
, NULL
, NULL
, 0);
10530 CASE_FLT_FN (BUILT_IN_COSH
):
10531 return fold_builtin_cosh (loc
, arg0
, type
, fndecl
);
10533 CASE_FLT_FN (BUILT_IN_TANH
):
10534 if (validate_arg (arg0
, REAL_TYPE
))
10535 return do_mpfr_arg1 (arg0
, type
, mpfr_tanh
, NULL
, NULL
, 0);
10538 CASE_FLT_FN (BUILT_IN_ERF
):
10539 if (validate_arg (arg0
, REAL_TYPE
))
10540 return do_mpfr_arg1 (arg0
, type
, mpfr_erf
, NULL
, NULL
, 0);
10543 CASE_FLT_FN (BUILT_IN_ERFC
):
10544 if (validate_arg (arg0
, REAL_TYPE
))
10545 return do_mpfr_arg1 (arg0
, type
, mpfr_erfc
, NULL
, NULL
, 0);
10548 CASE_FLT_FN (BUILT_IN_TGAMMA
):
10549 if (validate_arg (arg0
, REAL_TYPE
))
10550 return do_mpfr_arg1 (arg0
, type
, mpfr_gamma
, NULL
, NULL
, 0);
10553 CASE_FLT_FN (BUILT_IN_EXP
):
10554 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp
);
10556 CASE_FLT_FN (BUILT_IN_EXP2
):
10557 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp2
);
10559 CASE_FLT_FN (BUILT_IN_EXP10
):
10560 CASE_FLT_FN (BUILT_IN_POW10
):
10561 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp10
);
10563 CASE_FLT_FN (BUILT_IN_EXPM1
):
10564 if (validate_arg (arg0
, REAL_TYPE
))
10565 return do_mpfr_arg1 (arg0
, type
, mpfr_expm1
, NULL
, NULL
, 0);
10568 CASE_FLT_FN (BUILT_IN_LOG
):
10569 return fold_builtin_logarithm (loc
, fndecl
, arg0
, mpfr_log
);
10571 CASE_FLT_FN (BUILT_IN_LOG2
):
10572 return fold_builtin_logarithm (loc
, fndecl
, arg0
, mpfr_log2
);
10574 CASE_FLT_FN (BUILT_IN_LOG10
):
10575 return fold_builtin_logarithm (loc
, fndecl
, arg0
, mpfr_log10
);
10577 CASE_FLT_FN (BUILT_IN_LOG1P
):
10578 if (validate_arg (arg0
, REAL_TYPE
))
10579 return do_mpfr_arg1 (arg0
, type
, mpfr_log1p
,
10580 &dconstm1
, NULL
, false);
10583 CASE_FLT_FN (BUILT_IN_J0
):
10584 if (validate_arg (arg0
, REAL_TYPE
))
10585 return do_mpfr_arg1 (arg0
, type
, mpfr_j0
,
10589 CASE_FLT_FN (BUILT_IN_J1
):
10590 if (validate_arg (arg0
, REAL_TYPE
))
10591 return do_mpfr_arg1 (arg0
, type
, mpfr_j1
,
10595 CASE_FLT_FN (BUILT_IN_Y0
):
10596 if (validate_arg (arg0
, REAL_TYPE
))
10597 return do_mpfr_arg1 (arg0
, type
, mpfr_y0
,
10598 &dconst0
, NULL
, false);
10601 CASE_FLT_FN (BUILT_IN_Y1
):
10602 if (validate_arg (arg0
, REAL_TYPE
))
10603 return do_mpfr_arg1 (arg0
, type
, mpfr_y1
,
10604 &dconst0
, NULL
, false);
10607 CASE_FLT_FN (BUILT_IN_NAN
):
10608 case BUILT_IN_NAND32
:
10609 case BUILT_IN_NAND64
:
10610 case BUILT_IN_NAND128
:
10611 return fold_builtin_nan (arg0
, type
, true);
10613 CASE_FLT_FN (BUILT_IN_NANS
):
10614 return fold_builtin_nan (arg0
, type
, false);
10616 CASE_FLT_FN (BUILT_IN_FLOOR
):
10617 return fold_builtin_floor (loc
, fndecl
, arg0
);
10619 CASE_FLT_FN (BUILT_IN_CEIL
):
10620 return fold_builtin_ceil (loc
, fndecl
, arg0
);
10622 CASE_FLT_FN (BUILT_IN_TRUNC
):
10623 return fold_builtin_trunc (loc
, fndecl
, arg0
);
10625 CASE_FLT_FN (BUILT_IN_ROUND
):
10626 return fold_builtin_round (loc
, fndecl
, arg0
);
10628 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
10629 CASE_FLT_FN (BUILT_IN_RINT
):
10630 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg0
);
10632 CASE_FLT_FN (BUILT_IN_ICEIL
):
10633 CASE_FLT_FN (BUILT_IN_LCEIL
):
10634 CASE_FLT_FN (BUILT_IN_LLCEIL
):
10635 CASE_FLT_FN (BUILT_IN_LFLOOR
):
10636 CASE_FLT_FN (BUILT_IN_IFLOOR
):
10637 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
10638 CASE_FLT_FN (BUILT_IN_IROUND
):
10639 CASE_FLT_FN (BUILT_IN_LROUND
):
10640 CASE_FLT_FN (BUILT_IN_LLROUND
):
10641 return fold_builtin_int_roundingfn (loc
, fndecl
, arg0
);
10643 CASE_FLT_FN (BUILT_IN_IRINT
):
10644 CASE_FLT_FN (BUILT_IN_LRINT
):
10645 CASE_FLT_FN (BUILT_IN_LLRINT
):
10646 return fold_fixed_mathfn (loc
, fndecl
, arg0
);
10648 case BUILT_IN_BSWAP16
:
10649 case BUILT_IN_BSWAP32
:
10650 case BUILT_IN_BSWAP64
:
10651 return fold_builtin_bswap (fndecl
, arg0
);
10653 CASE_INT_FN (BUILT_IN_FFS
):
10654 CASE_INT_FN (BUILT_IN_CLZ
):
10655 CASE_INT_FN (BUILT_IN_CTZ
):
10656 CASE_INT_FN (BUILT_IN_CLRSB
):
10657 CASE_INT_FN (BUILT_IN_POPCOUNT
):
10658 CASE_INT_FN (BUILT_IN_PARITY
):
10659 return fold_builtin_bitop (fndecl
, arg0
);
10661 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
10662 return fold_builtin_signbit (loc
, arg0
, type
);
10664 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
10665 return fold_builtin_significand (loc
, arg0
, type
);
10667 CASE_FLT_FN (BUILT_IN_ILOGB
):
10668 CASE_FLT_FN (BUILT_IN_LOGB
):
10669 return fold_builtin_logb (loc
, arg0
, type
);
10671 case BUILT_IN_ISASCII
:
10672 return fold_builtin_isascii (loc
, arg0
);
10674 case BUILT_IN_TOASCII
:
10675 return fold_builtin_toascii (loc
, arg0
);
10677 case BUILT_IN_ISDIGIT
:
10678 return fold_builtin_isdigit (loc
, arg0
);
10680 CASE_FLT_FN (BUILT_IN_FINITE
):
10681 case BUILT_IN_FINITED32
:
10682 case BUILT_IN_FINITED64
:
10683 case BUILT_IN_FINITED128
:
10684 case BUILT_IN_ISFINITE
:
10686 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISFINITE
);
10689 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10692 CASE_FLT_FN (BUILT_IN_ISINF
):
10693 case BUILT_IN_ISINFD32
:
10694 case BUILT_IN_ISINFD64
:
10695 case BUILT_IN_ISINFD128
:
10697 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF
);
10700 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10703 case BUILT_IN_ISNORMAL
:
10704 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10706 case BUILT_IN_ISINF_SIGN
:
10707 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF_SIGN
);
10709 CASE_FLT_FN (BUILT_IN_ISNAN
):
10710 case BUILT_IN_ISNAND32
:
10711 case BUILT_IN_ISNAND64
:
10712 case BUILT_IN_ISNAND128
:
10713 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISNAN
);
10715 case BUILT_IN_PRINTF
:
10716 case BUILT_IN_PRINTF_UNLOCKED
:
10717 case BUILT_IN_VPRINTF
:
10718 return fold_builtin_printf (loc
, fndecl
, arg0
, NULL_TREE
, ignore
, fcode
);
10720 case BUILT_IN_FREE
:
10721 if (integer_zerop (arg0
))
10722 return build_empty_stmt (loc
);
10733 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10734 IGNORE is true if the result of the function call is ignored. This
10735 function returns NULL_TREE if no simplification was possible. */
10738 fold_builtin_2 (location_t loc
, tree fndecl
, tree arg0
, tree arg1
, bool ignore
)
10740 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10741 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10745 CASE_FLT_FN (BUILT_IN_JN
):
10746 if (validate_arg (arg0
, INTEGER_TYPE
)
10747 && validate_arg (arg1
, REAL_TYPE
))
10748 return do_mpfr_bessel_n (arg0
, arg1
, type
, mpfr_jn
, NULL
, 0);
10751 CASE_FLT_FN (BUILT_IN_YN
):
10752 if (validate_arg (arg0
, INTEGER_TYPE
)
10753 && validate_arg (arg1
, REAL_TYPE
))
10754 return do_mpfr_bessel_n (arg0
, arg1
, type
, mpfr_yn
,
10758 CASE_FLT_FN (BUILT_IN_DREM
):
10759 CASE_FLT_FN (BUILT_IN_REMAINDER
):
10760 if (validate_arg (arg0
, REAL_TYPE
)
10761 && validate_arg (arg1
, REAL_TYPE
))
10762 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_remainder
);
10765 CASE_FLT_FN_REENT (BUILT_IN_GAMMA
): /* GAMMA_R */
10766 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA
): /* LGAMMA_R */
10767 if (validate_arg (arg0
, REAL_TYPE
)
10768 && validate_arg (arg1
, POINTER_TYPE
))
10769 return do_mpfr_lgamma_r (arg0
, arg1
, type
);
10772 CASE_FLT_FN (BUILT_IN_ATAN2
):
10773 if (validate_arg (arg0
, REAL_TYPE
)
10774 && validate_arg (arg1
, REAL_TYPE
))
10775 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_atan2
);
10778 CASE_FLT_FN (BUILT_IN_FDIM
):
10779 if (validate_arg (arg0
, REAL_TYPE
)
10780 && validate_arg (arg1
, REAL_TYPE
))
10781 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_dim
);
10784 CASE_FLT_FN (BUILT_IN_HYPOT
):
10785 return fold_builtin_hypot (loc
, fndecl
, arg0
, arg1
, type
);
10787 CASE_FLT_FN (BUILT_IN_CPOW
):
10788 if (validate_arg (arg0
, COMPLEX_TYPE
)
10789 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
10790 && validate_arg (arg1
, COMPLEX_TYPE
)
10791 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
)
10792 return do_mpc_arg2 (arg0
, arg1
, type
, /*do_nonfinite=*/ 0, mpc_pow
);
10795 CASE_FLT_FN (BUILT_IN_LDEXP
):
10796 return fold_builtin_load_exponent (loc
, arg0
, arg1
, type
, /*ldexp=*/true);
10797 CASE_FLT_FN (BUILT_IN_SCALBN
):
10798 CASE_FLT_FN (BUILT_IN_SCALBLN
):
10799 return fold_builtin_load_exponent (loc
, arg0
, arg1
,
10800 type
, /*ldexp=*/false);
10802 CASE_FLT_FN (BUILT_IN_FREXP
):
10803 return fold_builtin_frexp (loc
, arg0
, arg1
, type
);
10805 CASE_FLT_FN (BUILT_IN_MODF
):
10806 return fold_builtin_modf (loc
, arg0
, arg1
, type
);
10808 case BUILT_IN_BZERO
:
10809 return fold_builtin_bzero (loc
, arg0
, arg1
, ignore
);
10811 case BUILT_IN_FPUTS
:
10812 return fold_builtin_fputs (loc
, arg0
, arg1
, ignore
, false, NULL_TREE
);
10814 case BUILT_IN_FPUTS_UNLOCKED
:
10815 return fold_builtin_fputs (loc
, arg0
, arg1
, ignore
, true, NULL_TREE
);
10817 case BUILT_IN_STRSTR
:
10818 return fold_builtin_strstr (loc
, arg0
, arg1
, type
);
10820 case BUILT_IN_STRCAT
:
10821 return fold_builtin_strcat (loc
, arg0
, arg1
);
10823 case BUILT_IN_STRSPN
:
10824 return fold_builtin_strspn (loc
, arg0
, arg1
);
10826 case BUILT_IN_STRCSPN
:
10827 return fold_builtin_strcspn (loc
, arg0
, arg1
);
10829 case BUILT_IN_STRCHR
:
10830 case BUILT_IN_INDEX
:
10831 return fold_builtin_strchr (loc
, arg0
, arg1
, type
);
10833 case BUILT_IN_STRRCHR
:
10834 case BUILT_IN_RINDEX
:
10835 return fold_builtin_strrchr (loc
, arg0
, arg1
, type
);
10837 case BUILT_IN_STRCPY
:
10838 return fold_builtin_strcpy (loc
, fndecl
, arg0
, arg1
, NULL_TREE
);
10840 case BUILT_IN_STPCPY
:
10843 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
10847 return build_call_expr_loc (loc
, fn
, 2, arg0
, arg1
);
10850 return fold_builtin_stpcpy (loc
, fndecl
, arg0
, arg1
);
10853 case BUILT_IN_STRCMP
:
10854 return fold_builtin_strcmp (loc
, arg0
, arg1
);
10856 case BUILT_IN_STRPBRK
:
10857 return fold_builtin_strpbrk (loc
, arg0
, arg1
, type
);
10859 case BUILT_IN_EXPECT
:
10860 return fold_builtin_expect (loc
, arg0
, arg1
);
10862 CASE_FLT_FN (BUILT_IN_POW
):
10863 return fold_builtin_pow (loc
, fndecl
, arg0
, arg1
, type
);
10865 CASE_FLT_FN (BUILT_IN_POWI
):
10866 return fold_builtin_powi (loc
, fndecl
, arg0
, arg1
, type
);
10868 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
10869 return fold_builtin_copysign (loc
, fndecl
, arg0
, arg1
, type
);
10871 CASE_FLT_FN (BUILT_IN_FMIN
):
10872 return fold_builtin_fmin_fmax (loc
, arg0
, arg1
, type
, /*max=*/false);
10874 CASE_FLT_FN (BUILT_IN_FMAX
):
10875 return fold_builtin_fmin_fmax (loc
, arg0
, arg1
, type
, /*max=*/true);
10877 case BUILT_IN_ISGREATER
:
10878 return fold_builtin_unordered_cmp (loc
, fndecl
,
10879 arg0
, arg1
, UNLE_EXPR
, LE_EXPR
);
10880 case BUILT_IN_ISGREATEREQUAL
:
10881 return fold_builtin_unordered_cmp (loc
, fndecl
,
10882 arg0
, arg1
, UNLT_EXPR
, LT_EXPR
);
10883 case BUILT_IN_ISLESS
:
10884 return fold_builtin_unordered_cmp (loc
, fndecl
,
10885 arg0
, arg1
, UNGE_EXPR
, GE_EXPR
);
10886 case BUILT_IN_ISLESSEQUAL
:
10887 return fold_builtin_unordered_cmp (loc
, fndecl
,
10888 arg0
, arg1
, UNGT_EXPR
, GT_EXPR
);
10889 case BUILT_IN_ISLESSGREATER
:
10890 return fold_builtin_unordered_cmp (loc
, fndecl
,
10891 arg0
, arg1
, UNEQ_EXPR
, EQ_EXPR
);
10892 case BUILT_IN_ISUNORDERED
:
10893 return fold_builtin_unordered_cmp (loc
, fndecl
,
10894 arg0
, arg1
, UNORDERED_EXPR
,
10897 /* We do the folding for va_start in the expander. */
10898 case BUILT_IN_VA_START
:
10901 case BUILT_IN_SPRINTF
:
10902 return fold_builtin_sprintf (loc
, arg0
, arg1
, NULL_TREE
, ignore
);
10904 case BUILT_IN_OBJECT_SIZE
:
10905 return fold_builtin_object_size (arg0
, arg1
);
10907 case BUILT_IN_PRINTF
:
10908 case BUILT_IN_PRINTF_UNLOCKED
:
10909 case BUILT_IN_VPRINTF
:
10910 return fold_builtin_printf (loc
, fndecl
, arg0
, arg1
, ignore
, fcode
);
10912 case BUILT_IN_PRINTF_CHK
:
10913 case BUILT_IN_VPRINTF_CHK
:
10914 if (!validate_arg (arg0
, INTEGER_TYPE
)
10915 || TREE_SIDE_EFFECTS (arg0
))
10918 return fold_builtin_printf (loc
, fndecl
,
10919 arg1
, NULL_TREE
, ignore
, fcode
);
10922 case BUILT_IN_FPRINTF
:
10923 case BUILT_IN_FPRINTF_UNLOCKED
:
10924 case BUILT_IN_VFPRINTF
:
10925 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg1
, NULL_TREE
,
10928 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
10929 return fold_builtin_atomic_always_lock_free (arg0
, arg1
);
10931 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
10932 return fold_builtin_atomic_is_lock_free (arg0
, arg1
);
10940 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10941 and ARG2. IGNORE is true if the result of the function call is ignored.
10942 This function returns NULL_TREE if no simplification was possible. */
10945 fold_builtin_3 (location_t loc
, tree fndecl
,
10946 tree arg0
, tree arg1
, tree arg2
, bool ignore
)
10948 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10949 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10953 CASE_FLT_FN (BUILT_IN_SINCOS
):
10954 return fold_builtin_sincos (loc
, arg0
, arg1
, arg2
);
10956 CASE_FLT_FN (BUILT_IN_FMA
):
10957 return fold_builtin_fma (loc
, arg0
, arg1
, arg2
, type
);
10960 CASE_FLT_FN (BUILT_IN_REMQUO
):
10961 if (validate_arg (arg0
, REAL_TYPE
)
10962 && validate_arg (arg1
, REAL_TYPE
)
10963 && validate_arg (arg2
, POINTER_TYPE
))
10964 return do_mpfr_remquo (arg0
, arg1
, arg2
);
10967 case BUILT_IN_MEMSET
:
10968 return fold_builtin_memset (loc
, arg0
, arg1
, arg2
, type
, ignore
);
10970 case BUILT_IN_BCOPY
:
10971 return fold_builtin_memory_op (loc
, arg1
, arg0
, arg2
,
10972 void_type_node
, true, /*endp=*/3);
10974 case BUILT_IN_MEMCPY
:
10975 return fold_builtin_memory_op (loc
, arg0
, arg1
, arg2
,
10976 type
, ignore
, /*endp=*/0);
10978 case BUILT_IN_MEMPCPY
:
10979 return fold_builtin_memory_op (loc
, arg0
, arg1
, arg2
,
10980 type
, ignore
, /*endp=*/1);
10982 case BUILT_IN_MEMMOVE
:
10983 return fold_builtin_memory_op (loc
, arg0
, arg1
, arg2
,
10984 type
, ignore
, /*endp=*/3);
10986 case BUILT_IN_STRNCAT
:
10987 return fold_builtin_strncat (loc
, arg0
, arg1
, arg2
);
10989 case BUILT_IN_STRNCPY
:
10990 return fold_builtin_strncpy (loc
, fndecl
, arg0
, arg1
, arg2
, NULL_TREE
);
10992 case BUILT_IN_STRNCMP
:
10993 return fold_builtin_strncmp (loc
, arg0
, arg1
, arg2
);
10995 case BUILT_IN_MEMCHR
:
10996 return fold_builtin_memchr (loc
, arg0
, arg1
, arg2
, type
);
10998 case BUILT_IN_BCMP
:
10999 case BUILT_IN_MEMCMP
:
11000 return fold_builtin_memcmp (loc
, arg0
, arg1
, arg2
);;
11002 case BUILT_IN_SPRINTF
:
11003 return fold_builtin_sprintf (loc
, arg0
, arg1
, arg2
, ignore
);
11005 case BUILT_IN_SNPRINTF
:
11006 return fold_builtin_snprintf (loc
, arg0
, arg1
, arg2
, NULL_TREE
, ignore
);
11008 case BUILT_IN_STRCPY_CHK
:
11009 case BUILT_IN_STPCPY_CHK
:
11010 return fold_builtin_stxcpy_chk (loc
, fndecl
, arg0
, arg1
, arg2
, NULL_TREE
,
11013 case BUILT_IN_STRCAT_CHK
:
11014 return fold_builtin_strcat_chk (loc
, fndecl
, arg0
, arg1
, arg2
);
11016 case BUILT_IN_PRINTF_CHK
:
11017 case BUILT_IN_VPRINTF_CHK
:
11018 if (!validate_arg (arg0
, INTEGER_TYPE
)
11019 || TREE_SIDE_EFFECTS (arg0
))
11022 return fold_builtin_printf (loc
, fndecl
, arg1
, arg2
, ignore
, fcode
);
11025 case BUILT_IN_FPRINTF
:
11026 case BUILT_IN_FPRINTF_UNLOCKED
:
11027 case BUILT_IN_VFPRINTF
:
11028 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg1
, arg2
,
11031 case BUILT_IN_FPRINTF_CHK
:
11032 case BUILT_IN_VFPRINTF_CHK
:
11033 if (!validate_arg (arg1
, INTEGER_TYPE
)
11034 || TREE_SIDE_EFFECTS (arg1
))
11037 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg2
, NULL_TREE
,
11046 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
11047 ARG2, and ARG3. IGNORE is true if the result of the function call is
11048 ignored. This function returns NULL_TREE if no simplification was
11052 fold_builtin_4 (location_t loc
, tree fndecl
,
11053 tree arg0
, tree arg1
, tree arg2
, tree arg3
, bool ignore
)
11055 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
11059 case BUILT_IN_MEMCPY_CHK
:
11060 case BUILT_IN_MEMPCPY_CHK
:
11061 case BUILT_IN_MEMMOVE_CHK
:
11062 case BUILT_IN_MEMSET_CHK
:
11063 return fold_builtin_memory_chk (loc
, fndecl
, arg0
, arg1
, arg2
, arg3
,
11065 DECL_FUNCTION_CODE (fndecl
));
11067 case BUILT_IN_STRNCPY_CHK
:
11068 case BUILT_IN_STPNCPY_CHK
:
11069 return fold_builtin_stxncpy_chk (loc
, arg0
, arg1
, arg2
, arg3
, NULL_TREE
,
11072 case BUILT_IN_STRNCAT_CHK
:
11073 return fold_builtin_strncat_chk (loc
, fndecl
, arg0
, arg1
, arg2
, arg3
);
11075 case BUILT_IN_SNPRINTF
:
11076 return fold_builtin_snprintf (loc
, arg0
, arg1
, arg2
, arg3
, ignore
);
11078 case BUILT_IN_FPRINTF_CHK
:
11079 case BUILT_IN_VFPRINTF_CHK
:
11080 if (!validate_arg (arg1
, INTEGER_TYPE
)
11081 || TREE_SIDE_EFFECTS (arg1
))
11084 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg2
, arg3
,
11094 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
11095 arguments, where NARGS <= 4. IGNORE is true if the result of the
11096 function call is ignored. This function returns NULL_TREE if no
11097 simplification was possible. Note that this only folds builtins with
11098 fixed argument patterns. Foldings that do varargs-to-varargs
11099 transformations, or that match calls with more than 4 arguments,
11100 need to be handled with fold_builtin_varargs instead. */
11102 #define MAX_ARGS_TO_FOLD_BUILTIN 4
11105 fold_builtin_n (location_t loc
, tree fndecl
, tree
*args
, int nargs
, bool ignore
)
11107 tree ret
= NULL_TREE
;
11112 ret
= fold_builtin_0 (loc
, fndecl
, ignore
);
11115 ret
= fold_builtin_1 (loc
, fndecl
, args
[0], ignore
);
11118 ret
= fold_builtin_2 (loc
, fndecl
, args
[0], args
[1], ignore
);
11121 ret
= fold_builtin_3 (loc
, fndecl
, args
[0], args
[1], args
[2], ignore
);
11124 ret
= fold_builtin_4 (loc
, fndecl
, args
[0], args
[1], args
[2], args
[3],
11132 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
11133 SET_EXPR_LOCATION (ret
, loc
);
11134 TREE_NO_WARNING (ret
) = 1;
11140 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11141 list ARGS along with N new arguments in NEWARGS. SKIP is the number
11142 of arguments in ARGS to be omitted. OLDNARGS is the number of
11143 elements in ARGS. */
11146 rewrite_call_expr_valist (location_t loc
, int oldnargs
, tree
*args
,
11147 int skip
, tree fndecl
, int n
, va_list newargs
)
11149 int nargs
= oldnargs
- skip
+ n
;
11156 buffer
= XALLOCAVEC (tree
, nargs
);
11157 for (i
= 0; i
< n
; i
++)
11158 buffer
[i
] = va_arg (newargs
, tree
);
11159 for (j
= skip
; j
< oldnargs
; j
++, i
++)
11160 buffer
[i
] = args
[j
];
11163 buffer
= args
+ skip
;
11165 return build_call_expr_loc_array (loc
, fndecl
, nargs
, buffer
);
11168 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
11169 list ARGS along with N new arguments specified as the "..."
11170 parameters. SKIP is the number of arguments in ARGS to be omitted.
11171 OLDNARGS is the number of elements in ARGS. */
11174 rewrite_call_expr_array (location_t loc
, int oldnargs
, tree
*args
,
11175 int skip
, tree fndecl
, int n
, ...)
11181 t
= rewrite_call_expr_valist (loc
, oldnargs
, args
, skip
, fndecl
, n
, ap
);
11187 /* Return true if FNDECL shouldn't be folded right now.
11188 If a built-in function has an inline attribute always_inline
11189 wrapper, defer folding it after always_inline functions have
11190 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
11191 might not be performed. */
11194 avoid_folding_inline_builtin (tree fndecl
)
11196 return (DECL_DECLARED_INLINE_P (fndecl
)
11197 && DECL_DISREGARD_INLINE_LIMITS (fndecl
)
11199 && !cfun
->always_inline_functions_inlined
11200 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl
)));
11203 /* A wrapper function for builtin folding that prevents warnings for
11204 "statement without effect" and the like, caused by removing the
11205 call node earlier than the warning is generated. */
11208 fold_call_expr (location_t loc
, tree exp
, bool ignore
)
11210 tree ret
= NULL_TREE
;
11211 tree fndecl
= get_callee_fndecl (exp
);
11213 && TREE_CODE (fndecl
) == FUNCTION_DECL
11214 && DECL_BUILT_IN (fndecl
)
11215 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
11216 yet. Defer folding until we see all the arguments
11217 (after inlining). */
11218 && !CALL_EXPR_VA_ARG_PACK (exp
))
11220 int nargs
= call_expr_nargs (exp
);
11222 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
11223 instead last argument is __builtin_va_arg_pack (). Defer folding
11224 even in that case, until arguments are finalized. */
11225 if (nargs
&& TREE_CODE (CALL_EXPR_ARG (exp
, nargs
- 1)) == CALL_EXPR
)
11227 tree fndecl2
= get_callee_fndecl (CALL_EXPR_ARG (exp
, nargs
- 1));
11229 && TREE_CODE (fndecl2
) == FUNCTION_DECL
11230 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
11231 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
11235 if (avoid_folding_inline_builtin (fndecl
))
11238 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
11239 return targetm
.fold_builtin (fndecl
, call_expr_nargs (exp
),
11240 CALL_EXPR_ARGP (exp
), ignore
);
11243 if (nargs
<= MAX_ARGS_TO_FOLD_BUILTIN
)
11245 tree
*args
= CALL_EXPR_ARGP (exp
);
11246 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
11249 ret
= fold_builtin_varargs (loc
, fndecl
, exp
, ignore
);
11257 /* Conveniently construct a function call expression. FNDECL names the
11258 function to be called and N arguments are passed in the array
11262 build_call_expr_loc_array (location_t loc
, tree fndecl
, int n
, tree
*argarray
)
11264 tree fntype
= TREE_TYPE (fndecl
);
11265 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), fndecl
);
11267 return fold_builtin_call_array (loc
, TREE_TYPE (fntype
), fn
, n
, argarray
);
11270 /* Conveniently construct a function call expression. FNDECL names the
11271 function to be called and the arguments are passed in the vector
11275 build_call_expr_loc_vec (location_t loc
, tree fndecl
, vec
<tree
, va_gc
> *vec
)
11277 return build_call_expr_loc_array (loc
, fndecl
, vec_safe_length (vec
),
11278 vec_safe_address (vec
));
11282 /* Conveniently construct a function call expression. FNDECL names the
11283 function to be called, N is the number of arguments, and the "..."
11284 parameters are the argument expressions. */
11287 build_call_expr_loc (location_t loc
, tree fndecl
, int n
, ...)
11290 tree
*argarray
= XALLOCAVEC (tree
, n
);
11294 for (i
= 0; i
< n
; i
++)
11295 argarray
[i
] = va_arg (ap
, tree
);
11297 return build_call_expr_loc_array (loc
, fndecl
, n
, argarray
);
11300 /* Like build_call_expr_loc (UNKNOWN_LOCATION, ...). Duplicated because
11301 varargs macros aren't supported by all bootstrap compilers. */
11304 build_call_expr (tree fndecl
, int n
, ...)
11307 tree
*argarray
= XALLOCAVEC (tree
, n
);
11311 for (i
= 0; i
< n
; i
++)
11312 argarray
[i
] = va_arg (ap
, tree
);
11314 return build_call_expr_loc_array (UNKNOWN_LOCATION
, fndecl
, n
, argarray
);
11317 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
11318 N arguments are passed in the array ARGARRAY. */
11321 fold_builtin_call_array (location_t loc
, tree type
,
11326 tree ret
= NULL_TREE
;
11329 if (TREE_CODE (fn
) == ADDR_EXPR
)
11331 tree fndecl
= TREE_OPERAND (fn
, 0);
11332 if (TREE_CODE (fndecl
) == FUNCTION_DECL
11333 && DECL_BUILT_IN (fndecl
))
11335 /* If last argument is __builtin_va_arg_pack (), arguments to this
11336 function are not finalized yet. Defer folding until they are. */
11337 if (n
&& TREE_CODE (argarray
[n
- 1]) == CALL_EXPR
)
11339 tree fndecl2
= get_callee_fndecl (argarray
[n
- 1]);
11341 && TREE_CODE (fndecl2
) == FUNCTION_DECL
11342 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
11343 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
11344 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
11346 if (avoid_folding_inline_builtin (fndecl
))
11347 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
11348 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
11350 ret
= targetm
.fold_builtin (fndecl
, n
, argarray
, false);
11354 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
11356 else if (n
<= MAX_ARGS_TO_FOLD_BUILTIN
)
11358 /* First try the transformations that don't require consing up
11360 ret
= fold_builtin_n (loc
, fndecl
, argarray
, n
, false);
11365 /* If we got this far, we need to build an exp. */
11366 exp
= build_call_array_loc (loc
, type
, fn
, n
, argarray
);
11367 ret
= fold_builtin_varargs (loc
, fndecl
, exp
, false);
11368 return ret
? ret
: exp
;
11372 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
11375 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
11376 along with N new arguments specified as the "..." parameters. SKIP
11377 is the number of arguments in EXP to be omitted. This function is used
11378 to do varargs-to-varargs transformations. */
11381 rewrite_call_expr (location_t loc
, tree exp
, int skip
, tree fndecl
, int n
, ...)
11387 t
= rewrite_call_expr_valist (loc
, call_expr_nargs (exp
),
11388 CALL_EXPR_ARGP (exp
), skip
, fndecl
, n
, ap
);
11394 /* Validate a single argument ARG against a tree code CODE representing
11398 validate_arg (const_tree arg
, enum tree_code code
)
11402 else if (code
== POINTER_TYPE
)
11403 return POINTER_TYPE_P (TREE_TYPE (arg
));
11404 else if (code
== INTEGER_TYPE
)
11405 return INTEGRAL_TYPE_P (TREE_TYPE (arg
));
11406 return code
== TREE_CODE (TREE_TYPE (arg
));
11409 /* This function validates the types of a function call argument list
11410 against a specified list of tree_codes. If the last specifier is a 0,
11411 that represents an ellipses, otherwise the last specifier must be a
11414 This is the GIMPLE version of validate_arglist. Eventually we want to
11415 completely convert builtins.c to work from GIMPLEs and the tree based
11416 validate_arglist will then be removed. */
11419 validate_gimple_arglist (const_gimple call
, ...)
11421 enum tree_code code
;
11427 va_start (ap
, call
);
11432 code
= (enum tree_code
) va_arg (ap
, int);
11436 /* This signifies an ellipses, any further arguments are all ok. */
11440 /* This signifies an endlink, if no arguments remain, return
11441 true, otherwise return false. */
11442 res
= (i
== gimple_call_num_args (call
));
11445 /* If no parameters remain or the parameter's code does not
11446 match the specified code, return false. Otherwise continue
11447 checking any remaining arguments. */
11448 arg
= gimple_call_arg (call
, i
++);
11449 if (!validate_arg (arg
, code
))
11456 /* We need gotos here since we can only have one VA_CLOSE in a
11464 /* Default target-specific builtin expander that does nothing. */
11467 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
11468 rtx target ATTRIBUTE_UNUSED
,
11469 rtx subtarget ATTRIBUTE_UNUSED
,
11470 enum machine_mode mode ATTRIBUTE_UNUSED
,
11471 int ignore ATTRIBUTE_UNUSED
)
11476 /* Returns true is EXP represents data that would potentially reside
11477 in a readonly section. */
11480 readonly_data_expr (tree exp
)
11484 if (TREE_CODE (exp
) != ADDR_EXPR
)
11487 exp
= get_base_address (TREE_OPERAND (exp
, 0));
11491 /* Make sure we call decl_readonly_section only for trees it
11492 can handle (since it returns true for everything it doesn't
11494 if (TREE_CODE (exp
) == STRING_CST
11495 || TREE_CODE (exp
) == CONSTRUCTOR
11496 || (TREE_CODE (exp
) == VAR_DECL
&& TREE_STATIC (exp
)))
11497 return decl_readonly_section (exp
, 0);
11502 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
11503 to the call, and TYPE is its return type.
11505 Return NULL_TREE if no simplification was possible, otherwise return the
11506 simplified form of the call as a tree.
11508 The simplified form may be a constant or other expression which
11509 computes the same value, but in a more efficient manner (including
11510 calls to other builtin functions).
11512 The call may contain arguments which need to be evaluated, but
11513 which are not useful to determine the result of the call. In
11514 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11515 COMPOUND_EXPR will be an argument which must be evaluated.
11516 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11517 COMPOUND_EXPR in the chain will contain the tree for the simplified
11518 form of the builtin function call. */
11521 fold_builtin_strstr (location_t loc
, tree s1
, tree s2
, tree type
)
11523 if (!validate_arg (s1
, POINTER_TYPE
)
11524 || !validate_arg (s2
, POINTER_TYPE
))
11529 const char *p1
, *p2
;
11531 p2
= c_getstr (s2
);
11535 p1
= c_getstr (s1
);
11538 const char *r
= strstr (p1
, p2
);
11542 return build_int_cst (TREE_TYPE (s1
), 0);
11544 /* Return an offset into the constant string argument. */
11545 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11546 return fold_convert_loc (loc
, type
, tem
);
11549 /* The argument is const char *, and the result is char *, so we need
11550 a type conversion here to avoid a warning. */
11552 return fold_convert_loc (loc
, type
, s1
);
11557 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
11561 /* New argument list transforming strstr(s1, s2) to
11562 strchr(s1, s2[0]). */
11563 return build_call_expr_loc (loc
, fn
, 2, s1
,
11564 build_int_cst (integer_type_node
, p2
[0]));
11568 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
11569 the call, and TYPE is its return type.
11571 Return NULL_TREE if no simplification was possible, otherwise return the
11572 simplified form of the call as a tree.
11574 The simplified form may be a constant or other expression which
11575 computes the same value, but in a more efficient manner (including
11576 calls to other builtin functions).
11578 The call may contain arguments which need to be evaluated, but
11579 which are not useful to determine the result of the call. In
11580 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11581 COMPOUND_EXPR will be an argument which must be evaluated.
11582 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11583 COMPOUND_EXPR in the chain will contain the tree for the simplified
11584 form of the builtin function call. */
11587 fold_builtin_strchr (location_t loc
, tree s1
, tree s2
, tree type
)
11589 if (!validate_arg (s1
, POINTER_TYPE
)
11590 || !validate_arg (s2
, INTEGER_TYPE
))
11596 if (TREE_CODE (s2
) != INTEGER_CST
)
11599 p1
= c_getstr (s1
);
11606 if (target_char_cast (s2
, &c
))
11609 r
= strchr (p1
, c
);
11612 return build_int_cst (TREE_TYPE (s1
), 0);
11614 /* Return an offset into the constant string argument. */
11615 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11616 return fold_convert_loc (loc
, type
, tem
);
11622 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11623 the call, and TYPE is its return type.
11625 Return NULL_TREE if no simplification was possible, otherwise return the
11626 simplified form of the call as a tree.
11628 The simplified form may be a constant or other expression which
11629 computes the same value, but in a more efficient manner (including
11630 calls to other builtin functions).
11632 The call may contain arguments which need to be evaluated, but
11633 which are not useful to determine the result of the call. In
11634 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11635 COMPOUND_EXPR will be an argument which must be evaluated.
11636 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11637 COMPOUND_EXPR in the chain will contain the tree for the simplified
11638 form of the builtin function call. */
11641 fold_builtin_strrchr (location_t loc
, tree s1
, tree s2
, tree type
)
11643 if (!validate_arg (s1
, POINTER_TYPE
)
11644 || !validate_arg (s2
, INTEGER_TYPE
))
11651 if (TREE_CODE (s2
) != INTEGER_CST
)
11654 p1
= c_getstr (s1
);
11661 if (target_char_cast (s2
, &c
))
11664 r
= strrchr (p1
, c
);
11667 return build_int_cst (TREE_TYPE (s1
), 0);
11669 /* Return an offset into the constant string argument. */
11670 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11671 return fold_convert_loc (loc
, type
, tem
);
11674 if (! integer_zerop (s2
))
11677 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
11681 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11682 return build_call_expr_loc (loc
, fn
, 2, s1
, s2
);
11686 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11687 to the call, and TYPE is its return type.
11689 Return NULL_TREE if no simplification was possible, otherwise return the
11690 simplified form of the call as a tree.
11692 The simplified form may be a constant or other expression which
11693 computes the same value, but in a more efficient manner (including
11694 calls to other builtin functions).
11696 The call may contain arguments which need to be evaluated, but
11697 which are not useful to determine the result of the call. In
11698 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11699 COMPOUND_EXPR will be an argument which must be evaluated.
11700 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11701 COMPOUND_EXPR in the chain will contain the tree for the simplified
11702 form of the builtin function call. */
11705 fold_builtin_strpbrk (location_t loc
, tree s1
, tree s2
, tree type
)
11707 if (!validate_arg (s1
, POINTER_TYPE
)
11708 || !validate_arg (s2
, POINTER_TYPE
))
11713 const char *p1
, *p2
;
11715 p2
= c_getstr (s2
);
11719 p1
= c_getstr (s1
);
11722 const char *r
= strpbrk (p1
, p2
);
11726 return build_int_cst (TREE_TYPE (s1
), 0);
11728 /* Return an offset into the constant string argument. */
11729 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11730 return fold_convert_loc (loc
, type
, tem
);
11734 /* strpbrk(x, "") == NULL.
11735 Evaluate and ignore s1 in case it had side-effects. */
11736 return omit_one_operand_loc (loc
, TREE_TYPE (s1
), integer_zero_node
, s1
);
11739 return NULL_TREE
; /* Really call strpbrk. */
11741 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
11745 /* New argument list transforming strpbrk(s1, s2) to
11746 strchr(s1, s2[0]). */
11747 return build_call_expr_loc (loc
, fn
, 2, s1
,
11748 build_int_cst (integer_type_node
, p2
[0]));
11752 /* Simplify a call to the strcat builtin. DST and SRC are the arguments
11755 Return NULL_TREE if no simplification was possible, otherwise return the
11756 simplified form of the call as a tree.
11758 The simplified form may be a constant or other expression which
11759 computes the same value, but in a more efficient manner (including
11760 calls to other builtin functions).
11762 The call may contain arguments which need to be evaluated, but
11763 which are not useful to determine the result of the call. In
11764 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11765 COMPOUND_EXPR will be an argument which must be evaluated.
11766 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11767 COMPOUND_EXPR in the chain will contain the tree for the simplified
11768 form of the builtin function call. */
11771 fold_builtin_strcat (location_t loc ATTRIBUTE_UNUSED
, tree dst
, tree src
)
11773 if (!validate_arg (dst
, POINTER_TYPE
)
11774 || !validate_arg (src
, POINTER_TYPE
))
11778 const char *p
= c_getstr (src
);
11780 /* If the string length is zero, return the dst parameter. */
11781 if (p
&& *p
== '\0')
11784 if (optimize_insn_for_speed_p ())
11786 /* See if we can store by pieces into (dst + strlen(dst)). */
11788 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
11789 tree strcpy_fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
11791 if (!strlen_fn
|| !strcpy_fn
)
11794 /* If we don't have a movstr we don't want to emit an strcpy
11795 call. We have to do that if the length of the source string
11796 isn't computable (in that case we can use memcpy probably
11797 later expanding to a sequence of mov instructions). If we
11798 have movstr instructions we can emit strcpy calls. */
11801 tree len
= c_strlen (src
, 1);
11802 if (! len
|| TREE_SIDE_EFFECTS (len
))
11806 /* Stabilize the argument list. */
11807 dst
= builtin_save_expr (dst
);
11809 /* Create strlen (dst). */
11810 newdst
= build_call_expr_loc (loc
, strlen_fn
, 1, dst
);
11811 /* Create (dst p+ strlen (dst)). */
11813 newdst
= fold_build_pointer_plus_loc (loc
, dst
, newdst
);
11814 newdst
= builtin_save_expr (newdst
);
11816 call
= build_call_expr_loc (loc
, strcpy_fn
, 2, newdst
, src
);
11817 return build2 (COMPOUND_EXPR
, TREE_TYPE (dst
), call
, dst
);
11823 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11824 arguments to the call.
11826 Return NULL_TREE if no simplification was possible, otherwise return the
11827 simplified form of the call as a tree.
11829 The simplified form may be a constant or other expression which
11830 computes the same value, but in a more efficient manner (including
11831 calls to other builtin functions).
11833 The call may contain arguments which need to be evaluated, but
11834 which are not useful to determine the result of the call. In
11835 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11836 COMPOUND_EXPR will be an argument which must be evaluated.
11837 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11838 COMPOUND_EXPR in the chain will contain the tree for the simplified
11839 form of the builtin function call. */
11842 fold_builtin_strncat (location_t loc
, tree dst
, tree src
, tree len
)
11844 if (!validate_arg (dst
, POINTER_TYPE
)
11845 || !validate_arg (src
, POINTER_TYPE
)
11846 || !validate_arg (len
, INTEGER_TYPE
))
11850 const char *p
= c_getstr (src
);
11852 /* If the requested length is zero, or the src parameter string
11853 length is zero, return the dst parameter. */
11854 if (integer_zerop (len
) || (p
&& *p
== '\0'))
11855 return omit_two_operands_loc (loc
, TREE_TYPE (dst
), dst
, src
, len
);
11857 /* If the requested len is greater than or equal to the string
11858 length, call strcat. */
11859 if (TREE_CODE (len
) == INTEGER_CST
&& p
11860 && compare_tree_int (len
, strlen (p
)) >= 0)
11862 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
11864 /* If the replacement _DECL isn't initialized, don't do the
11869 return build_call_expr_loc (loc
, fn
, 2, dst
, src
);
11875 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11878 Return NULL_TREE if no simplification was possible, otherwise return the
11879 simplified form of the call as a tree.
11881 The simplified form may be a constant or other expression which
11882 computes the same value, but in a more efficient manner (including
11883 calls to other builtin functions).
11885 The call may contain arguments which need to be evaluated, but
11886 which are not useful to determine the result of the call. In
11887 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11888 COMPOUND_EXPR will be an argument which must be evaluated.
11889 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11890 COMPOUND_EXPR in the chain will contain the tree for the simplified
11891 form of the builtin function call. */
11894 fold_builtin_strspn (location_t loc
, tree s1
, tree s2
)
11896 if (!validate_arg (s1
, POINTER_TYPE
)
11897 || !validate_arg (s2
, POINTER_TYPE
))
11901 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
11903 /* If both arguments are constants, evaluate at compile-time. */
11906 const size_t r
= strspn (p1
, p2
);
11907 return build_int_cst (size_type_node
, r
);
11910 /* If either argument is "", return NULL_TREE. */
11911 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
11912 /* Evaluate and ignore both arguments in case either one has
11914 return omit_two_operands_loc (loc
, size_type_node
, size_zero_node
,
11920 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11923 Return NULL_TREE if no simplification was possible, otherwise return the
11924 simplified form of the call as a tree.
11926 The simplified form may be a constant or other expression which
11927 computes the same value, but in a more efficient manner (including
11928 calls to other builtin functions).
11930 The call may contain arguments which need to be evaluated, but
11931 which are not useful to determine the result of the call. In
11932 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11933 COMPOUND_EXPR will be an argument which must be evaluated.
11934 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11935 COMPOUND_EXPR in the chain will contain the tree for the simplified
11936 form of the builtin function call. */
11939 fold_builtin_strcspn (location_t loc
, tree s1
, tree s2
)
11941 if (!validate_arg (s1
, POINTER_TYPE
)
11942 || !validate_arg (s2
, POINTER_TYPE
))
11946 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
11948 /* If both arguments are constants, evaluate at compile-time. */
11951 const size_t r
= strcspn (p1
, p2
);
11952 return build_int_cst (size_type_node
, r
);
11955 /* If the first argument is "", return NULL_TREE. */
11956 if (p1
&& *p1
== '\0')
11958 /* Evaluate and ignore argument s2 in case it has
11960 return omit_one_operand_loc (loc
, size_type_node
,
11961 size_zero_node
, s2
);
11964 /* If the second argument is "", return __builtin_strlen(s1). */
11965 if (p2
&& *p2
== '\0')
11967 tree fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
11969 /* If the replacement _DECL isn't initialized, don't do the
11974 return build_call_expr_loc (loc
, fn
, 1, s1
);
11980 /* Fold a call to the fputs builtin. ARG0 and ARG1 are the arguments
11981 to the call. IGNORE is true if the value returned
11982 by the builtin will be ignored. UNLOCKED is true is true if this
11983 actually a call to fputs_unlocked. If LEN in non-NULL, it represents
11984 the known length of the string. Return NULL_TREE if no simplification
11988 fold_builtin_fputs (location_t loc
, tree arg0
, tree arg1
,
11989 bool ignore
, bool unlocked
, tree len
)
11991 /* If we're using an unlocked function, assume the other unlocked
11992 functions exist explicitly. */
11993 tree
const fn_fputc
= (unlocked
11994 ? builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
)
11995 : builtin_decl_implicit (BUILT_IN_FPUTC
));
11996 tree
const fn_fwrite
= (unlocked
11997 ? builtin_decl_explicit (BUILT_IN_FWRITE_UNLOCKED
)
11998 : builtin_decl_implicit (BUILT_IN_FWRITE
));
12000 /* If the return value is used, don't do the transformation. */
12004 /* Verify the arguments in the original call. */
12005 if (!validate_arg (arg0
, POINTER_TYPE
)
12006 || !validate_arg (arg1
, POINTER_TYPE
))
12010 len
= c_strlen (arg0
, 0);
12012 /* Get the length of the string passed to fputs. If the length
12013 can't be determined, punt. */
12015 || TREE_CODE (len
) != INTEGER_CST
)
12018 switch (compare_tree_int (len
, 1))
12020 case -1: /* length is 0, delete the call entirely . */
12021 return omit_one_operand_loc (loc
, integer_type_node
,
12022 integer_zero_node
, arg1
);;
12024 case 0: /* length is 1, call fputc. */
12026 const char *p
= c_getstr (arg0
);
12031 return build_call_expr_loc (loc
, fn_fputc
, 2,
12033 (integer_type_node
, p
[0]), arg1
);
12039 case 1: /* length is greater than 1, call fwrite. */
12041 /* If optimizing for size keep fputs. */
12042 if (optimize_function_for_size_p (cfun
))
12044 /* New argument list transforming fputs(string, stream) to
12045 fwrite(string, 1, len, stream). */
12047 return build_call_expr_loc (loc
, fn_fwrite
, 4, arg0
,
12048 size_one_node
, len
, arg1
);
12053 gcc_unreachable ();
12058 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
12059 produced. False otherwise. This is done so that we don't output the error
12060 or warning twice or three times. */
12063 fold_builtin_next_arg (tree exp
, bool va_start_p
)
12065 tree fntype
= TREE_TYPE (current_function_decl
);
12066 int nargs
= call_expr_nargs (exp
);
12068 /* There is good chance the current input_location points inside the
12069 definition of the va_start macro (perhaps on the token for
12070 builtin) in a system header, so warnings will not be emitted.
12071 Use the location in real source code. */
12072 source_location current_location
=
12073 linemap_unwind_to_first_non_reserved_loc (line_table
, input_location
,
12076 if (!stdarg_p (fntype
))
12078 error ("%<va_start%> used in function with fixed args");
12084 if (va_start_p
&& (nargs
!= 2))
12086 error ("wrong number of arguments to function %<va_start%>");
12089 arg
= CALL_EXPR_ARG (exp
, 1);
12091 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
12092 when we checked the arguments and if needed issued a warning. */
12097 /* Evidently an out of date version of <stdarg.h>; can't validate
12098 va_start's second argument, but can still work as intended. */
12099 warning_at (current_location
,
12101 "%<__builtin_next_arg%> called without an argument");
12104 else if (nargs
> 1)
12106 error ("wrong number of arguments to function %<__builtin_next_arg%>");
12109 arg
= CALL_EXPR_ARG (exp
, 0);
12112 if (TREE_CODE (arg
) == SSA_NAME
)
12113 arg
= SSA_NAME_VAR (arg
);
12115 /* We destructively modify the call to be __builtin_va_start (ap, 0)
12116 or __builtin_next_arg (0) the first time we see it, after checking
12117 the arguments and if needed issuing a warning. */
12118 if (!integer_zerop (arg
))
12120 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
12122 /* Strip off all nops for the sake of the comparison. This
12123 is not quite the same as STRIP_NOPS. It does more.
12124 We must also strip off INDIRECT_EXPR for C++ reference
12126 while (CONVERT_EXPR_P (arg
)
12127 || TREE_CODE (arg
) == INDIRECT_REF
)
12128 arg
= TREE_OPERAND (arg
, 0);
12129 if (arg
!= last_parm
)
12131 /* FIXME: Sometimes with the tree optimizers we can get the
12132 not the last argument even though the user used the last
12133 argument. We just warn and set the arg to be the last
12134 argument so that we will get wrong-code because of
12136 warning_at (current_location
,
12138 "second parameter of %<va_start%> not last named argument");
12141 /* Undefined by C99 7.15.1.4p4 (va_start):
12142 "If the parameter parmN is declared with the register storage
12143 class, with a function or array type, or with a type that is
12144 not compatible with the type that results after application of
12145 the default argument promotions, the behavior is undefined."
12147 else if (DECL_REGISTER (arg
))
12149 warning_at (current_location
,
12151 "undefined behaviour when second parameter of "
12152 "%<va_start%> is declared with %<register%> storage");
12155 /* We want to verify the second parameter just once before the tree
12156 optimizers are run and then avoid keeping it in the tree,
12157 as otherwise we could warn even for correct code like:
12158 void foo (int i, ...)
12159 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
12161 CALL_EXPR_ARG (exp
, 1) = integer_zero_node
;
12163 CALL_EXPR_ARG (exp
, 0) = integer_zero_node
;
12169 /* Simplify a call to the sprintf builtin with arguments DEST, FMT, and ORIG.
12170 ORIG may be null if this is a 2-argument call. We don't attempt to
12171 simplify calls with more than 3 arguments.
12173 Return NULL_TREE if no simplification was possible, otherwise return the
12174 simplified form of the call as a tree. If IGNORED is true, it means that
12175 the caller does not use the returned value of the function. */
12178 fold_builtin_sprintf (location_t loc
, tree dest
, tree fmt
,
12179 tree orig
, int ignored
)
12182 const char *fmt_str
= NULL
;
12184 /* Verify the required arguments in the original call. We deal with two
12185 types of sprintf() calls: 'sprintf (str, fmt)' and
12186 'sprintf (dest, "%s", orig)'. */
12187 if (!validate_arg (dest
, POINTER_TYPE
)
12188 || !validate_arg (fmt
, POINTER_TYPE
))
12190 if (orig
&& !validate_arg (orig
, POINTER_TYPE
))
12193 /* Check whether the format is a literal string constant. */
12194 fmt_str
= c_getstr (fmt
);
12195 if (fmt_str
== NULL
)
12199 retval
= NULL_TREE
;
12201 if (!init_target_chars ())
12204 /* If the format doesn't contain % args or %%, use strcpy. */
12205 if (strchr (fmt_str
, target_percent
) == NULL
)
12207 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
12212 /* Don't optimize sprintf (buf, "abc", ptr++). */
12216 /* Convert sprintf (str, fmt) into strcpy (str, fmt) when
12217 'format' is known to contain no % formats. */
12218 call
= build_call_expr_loc (loc
, fn
, 2, dest
, fmt
);
12220 retval
= build_int_cst (integer_type_node
, strlen (fmt_str
));
12223 /* If the format is "%s", use strcpy if the result isn't used. */
12224 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
12227 fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
12232 /* Don't crash on sprintf (str1, "%s"). */
12236 /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
12239 retval
= c_strlen (orig
, 1);
12240 if (!retval
|| TREE_CODE (retval
) != INTEGER_CST
)
12243 call
= build_call_expr_loc (loc
, fn
, 2, dest
, orig
);
12246 if (call
&& retval
)
12248 retval
= fold_convert_loc
12249 (loc
, TREE_TYPE (TREE_TYPE (builtin_decl_implicit (BUILT_IN_SPRINTF
))),
12251 return build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), call
, retval
);
12257 /* Simplify a call to the snprintf builtin with arguments DEST, DESTSIZE,
12258 FMT, and ORIG. ORIG may be null if this is a 3-argument call. We don't
12259 attempt to simplify calls with more than 4 arguments.
12261 Return NULL_TREE if no simplification was possible, otherwise return the
12262 simplified form of the call as a tree. If IGNORED is true, it means that
12263 the caller does not use the returned value of the function. */
12266 fold_builtin_snprintf (location_t loc
, tree dest
, tree destsize
, tree fmt
,
12267 tree orig
, int ignored
)
12270 const char *fmt_str
= NULL
;
12271 unsigned HOST_WIDE_INT destlen
;
12273 /* Verify the required arguments in the original call. We deal with two
12274 types of snprintf() calls: 'snprintf (str, cst, fmt)' and
12275 'snprintf (dest, cst, "%s", orig)'. */
12276 if (!validate_arg (dest
, POINTER_TYPE
)
12277 || !validate_arg (destsize
, INTEGER_TYPE
)
12278 || !validate_arg (fmt
, POINTER_TYPE
))
12280 if (orig
&& !validate_arg (orig
, POINTER_TYPE
))
12283 if (!tree_fits_uhwi_p (destsize
))
12286 /* Check whether the format is a literal string constant. */
12287 fmt_str
= c_getstr (fmt
);
12288 if (fmt_str
== NULL
)
12292 retval
= NULL_TREE
;
12294 if (!init_target_chars ())
12297 destlen
= tree_to_uhwi (destsize
);
12299 /* If the format doesn't contain % args or %%, use strcpy. */
12300 if (strchr (fmt_str
, target_percent
) == NULL
)
12302 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
12303 size_t len
= strlen (fmt_str
);
12305 /* Don't optimize snprintf (buf, 4, "abc", ptr++). */
12309 /* We could expand this as
12310 memcpy (str, fmt, cst - 1); str[cst - 1] = '\0';
12312 memcpy (str, fmt_with_nul_at_cstm1, cst);
12313 but in the former case that might increase code size
12314 and in the latter case grow .rodata section too much.
12315 So punt for now. */
12316 if (len
>= destlen
)
12322 /* Convert snprintf (str, cst, fmt) into strcpy (str, fmt) when
12323 'format' is known to contain no % formats and
12324 strlen (fmt) < cst. */
12325 call
= build_call_expr_loc (loc
, fn
, 2, dest
, fmt
);
12328 retval
= build_int_cst (integer_type_node
, strlen (fmt_str
));
12331 /* If the format is "%s", use strcpy if the result isn't used. */
12332 else if (fmt_str
&& strcmp (fmt_str
, target_percent_s
) == 0)
12334 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
12335 unsigned HOST_WIDE_INT origlen
;
12337 /* Don't crash on snprintf (str1, cst, "%s"). */
12341 retval
= c_strlen (orig
, 1);
12342 if (!retval
|| !tree_fits_uhwi_p (retval
))
12345 origlen
= tree_to_uhwi (retval
);
12346 /* We could expand this as
12347 memcpy (str1, str2, cst - 1); str1[cst - 1] = '\0';
12349 memcpy (str1, str2_with_nul_at_cstm1, cst);
12350 but in the former case that might increase code size
12351 and in the latter case grow .rodata section too much.
12352 So punt for now. */
12353 if (origlen
>= destlen
)
12356 /* Convert snprintf (str1, cst, "%s", str2) into
12357 strcpy (str1, str2) if strlen (str2) < cst. */
12361 call
= build_call_expr_loc (loc
, fn
, 2, dest
, orig
);
12364 retval
= NULL_TREE
;
12367 if (call
&& retval
)
12369 tree fn
= builtin_decl_explicit (BUILT_IN_SNPRINTF
);
12370 retval
= fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fn
)), retval
);
12371 return build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), call
, retval
);
12377 /* Expand a call EXP to __builtin_object_size. */
12380 expand_builtin_object_size (tree exp
)
12383 int object_size_type
;
12384 tree fndecl
= get_callee_fndecl (exp
);
12386 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
12388 error ("%Kfirst argument of %D must be a pointer, second integer constant",
12390 expand_builtin_trap ();
12394 ost
= CALL_EXPR_ARG (exp
, 1);
12397 if (TREE_CODE (ost
) != INTEGER_CST
12398 || tree_int_cst_sgn (ost
) < 0
12399 || compare_tree_int (ost
, 3) > 0)
12401 error ("%Klast argument of %D is not integer constant between 0 and 3",
12403 expand_builtin_trap ();
12407 object_size_type
= tree_to_shwi (ost
);
12409 return object_size_type
< 2 ? constm1_rtx
: const0_rtx
;
12412 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12413 FCODE is the BUILT_IN_* to use.
12414 Return NULL_RTX if we failed; the caller should emit a normal call,
12415 otherwise try to get the result in TARGET, if convenient (and in
12416 mode MODE if that's convenient). */
12419 expand_builtin_memory_chk (tree exp
, rtx target
, enum machine_mode mode
,
12420 enum built_in_function fcode
)
12422 tree dest
, src
, len
, size
;
12424 if (!validate_arglist (exp
,
12426 fcode
== BUILT_IN_MEMSET_CHK
12427 ? INTEGER_TYPE
: POINTER_TYPE
,
12428 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
12431 dest
= CALL_EXPR_ARG (exp
, 0);
12432 src
= CALL_EXPR_ARG (exp
, 1);
12433 len
= CALL_EXPR_ARG (exp
, 2);
12434 size
= CALL_EXPR_ARG (exp
, 3);
12436 if (! tree_fits_uhwi_p (size
))
12439 if (tree_fits_uhwi_p (len
) || integer_all_onesp (size
))
12443 if (! integer_all_onesp (size
) && tree_int_cst_lt (size
, len
))
12445 warning_at (tree_nonartificial_location (exp
),
12446 0, "%Kcall to %D will always overflow destination buffer",
12447 exp
, get_callee_fndecl (exp
));
12452 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12453 mem{cpy,pcpy,move,set} is available. */
12456 case BUILT_IN_MEMCPY_CHK
:
12457 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
12459 case BUILT_IN_MEMPCPY_CHK
:
12460 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
12462 case BUILT_IN_MEMMOVE_CHK
:
12463 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
12465 case BUILT_IN_MEMSET_CHK
:
12466 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
12475 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 3, dest
, src
, len
);
12476 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
12477 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
12478 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
12480 else if (fcode
== BUILT_IN_MEMSET_CHK
)
12484 unsigned int dest_align
= get_pointer_alignment (dest
);
12486 /* If DEST is not a pointer type, call the normal function. */
12487 if (dest_align
== 0)
12490 /* If SRC and DEST are the same (and not volatile), do nothing. */
12491 if (operand_equal_p (src
, dest
, 0))
12495 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
12497 /* Evaluate and ignore LEN in case it has side-effects. */
12498 expand_expr (len
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
12499 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
12502 expr
= fold_build_pointer_plus (dest
, len
);
12503 return expand_expr (expr
, target
, mode
, EXPAND_NORMAL
);
12506 /* __memmove_chk special case. */
12507 if (fcode
== BUILT_IN_MEMMOVE_CHK
)
12509 unsigned int src_align
= get_pointer_alignment (src
);
12511 if (src_align
== 0)
12514 /* If src is categorized for a readonly section we can use
12515 normal __memcpy_chk. */
12516 if (readonly_data_expr (src
))
12518 tree fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
12521 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 4,
12522 dest
, src
, len
, size
);
12523 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
12524 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
12525 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
12532 /* Emit warning if a buffer overflow is detected at compile time. */
12535 maybe_emit_chk_warning (tree exp
, enum built_in_function fcode
)
12539 location_t loc
= tree_nonartificial_location (exp
);
12543 case BUILT_IN_STRCPY_CHK
:
12544 case BUILT_IN_STPCPY_CHK
:
12545 /* For __strcat_chk the warning will be emitted only if overflowing
12546 by at least strlen (dest) + 1 bytes. */
12547 case BUILT_IN_STRCAT_CHK
:
12548 len
= CALL_EXPR_ARG (exp
, 1);
12549 size
= CALL_EXPR_ARG (exp
, 2);
12552 case BUILT_IN_STRNCAT_CHK
:
12553 case BUILT_IN_STRNCPY_CHK
:
12554 case BUILT_IN_STPNCPY_CHK
:
12555 len
= CALL_EXPR_ARG (exp
, 2);
12556 size
= CALL_EXPR_ARG (exp
, 3);
12558 case BUILT_IN_SNPRINTF_CHK
:
12559 case BUILT_IN_VSNPRINTF_CHK
:
12560 len
= CALL_EXPR_ARG (exp
, 1);
12561 size
= CALL_EXPR_ARG (exp
, 3);
12564 gcc_unreachable ();
12570 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
12575 len
= c_strlen (len
, 1);
12576 if (! len
|| ! tree_fits_uhwi_p (len
) || tree_int_cst_lt (len
, size
))
12579 else if (fcode
== BUILT_IN_STRNCAT_CHK
)
12581 tree src
= CALL_EXPR_ARG (exp
, 1);
12582 if (! src
|| ! tree_fits_uhwi_p (len
) || tree_int_cst_lt (len
, size
))
12584 src
= c_strlen (src
, 1);
12585 if (! src
|| ! tree_fits_uhwi_p (src
))
12587 warning_at (loc
, 0, "%Kcall to %D might overflow destination buffer",
12588 exp
, get_callee_fndecl (exp
));
12591 else if (tree_int_cst_lt (src
, size
))
12594 else if (! tree_fits_uhwi_p (len
) || ! tree_int_cst_lt (size
, len
))
12597 warning_at (loc
, 0, "%Kcall to %D will always overflow destination buffer",
12598 exp
, get_callee_fndecl (exp
));
12601 /* Emit warning if a buffer overflow is detected at compile time
12602 in __sprintf_chk/__vsprintf_chk calls. */
12605 maybe_emit_sprintf_chk_warning (tree exp
, enum built_in_function fcode
)
12607 tree size
, len
, fmt
;
12608 const char *fmt_str
;
12609 int nargs
= call_expr_nargs (exp
);
12611 /* Verify the required arguments in the original call. */
12615 size
= CALL_EXPR_ARG (exp
, 2);
12616 fmt
= CALL_EXPR_ARG (exp
, 3);
12618 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
12621 /* Check whether the format is a literal string constant. */
12622 fmt_str
= c_getstr (fmt
);
12623 if (fmt_str
== NULL
)
12626 if (!init_target_chars ())
12629 /* If the format doesn't contain % args or %%, we know its size. */
12630 if (strchr (fmt_str
, target_percent
) == 0)
12631 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
12632 /* If the format is "%s" and first ... argument is a string literal,
12634 else if (fcode
== BUILT_IN_SPRINTF_CHK
12635 && strcmp (fmt_str
, target_percent_s
) == 0)
12641 arg
= CALL_EXPR_ARG (exp
, 4);
12642 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
12645 len
= c_strlen (arg
, 1);
12646 if (!len
|| ! tree_fits_uhwi_p (len
))
12652 if (! tree_int_cst_lt (len
, size
))
12653 warning_at (tree_nonartificial_location (exp
),
12654 0, "%Kcall to %D will always overflow destination buffer",
12655 exp
, get_callee_fndecl (exp
));
12658 /* Emit warning if a free is called with address of a variable. */
12661 maybe_emit_free_warning (tree exp
)
12663 tree arg
= CALL_EXPR_ARG (exp
, 0);
12666 if (TREE_CODE (arg
) != ADDR_EXPR
)
12669 arg
= get_base_address (TREE_OPERAND (arg
, 0));
12670 if (arg
== NULL
|| INDIRECT_REF_P (arg
) || TREE_CODE (arg
) == MEM_REF
)
12673 if (SSA_VAR_P (arg
))
12674 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
12675 "%Kattempt to free a non-heap object %qD", exp
, arg
);
12677 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
12678 "%Kattempt to free a non-heap object", exp
);
12681 /* Fold a call to __builtin_object_size with arguments PTR and OST,
12685 fold_builtin_object_size (tree ptr
, tree ost
)
12687 unsigned HOST_WIDE_INT bytes
;
12688 int object_size_type
;
12690 if (!validate_arg (ptr
, POINTER_TYPE
)
12691 || !validate_arg (ost
, INTEGER_TYPE
))
12696 if (TREE_CODE (ost
) != INTEGER_CST
12697 || tree_int_cst_sgn (ost
) < 0
12698 || compare_tree_int (ost
, 3) > 0)
12701 object_size_type
= tree_to_shwi (ost
);
12703 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
12704 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
12705 and (size_t) 0 for types 2 and 3. */
12706 if (TREE_SIDE_EFFECTS (ptr
))
12707 return build_int_cst_type (size_type_node
, object_size_type
< 2 ? -1 : 0);
12709 if (TREE_CODE (ptr
) == ADDR_EXPR
)
12711 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
12712 if (double_int_fits_to_tree_p (size_type_node
,
12713 double_int::from_uhwi (bytes
)))
12714 return build_int_cstu (size_type_node
, bytes
);
12716 else if (TREE_CODE (ptr
) == SSA_NAME
)
12718 /* If object size is not known yet, delay folding until
12719 later. Maybe subsequent passes will help determining
12721 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
12722 if (bytes
!= (unsigned HOST_WIDE_INT
) (object_size_type
< 2 ? -1 : 0)
12723 && double_int_fits_to_tree_p (size_type_node
,
12724 double_int::from_uhwi (bytes
)))
12725 return build_int_cstu (size_type_node
, bytes
);
12731 /* Fold a call to the __mem{cpy,pcpy,move,set}_chk builtin.
12732 DEST, SRC, LEN, and SIZE are the arguments to the call.
12733 IGNORE is true, if return value can be ignored. FCODE is the BUILT_IN_*
12734 code of the builtin. If MAXLEN is not NULL, it is maximum length
12735 passed as third argument. */
12738 fold_builtin_memory_chk (location_t loc
, tree fndecl
,
12739 tree dest
, tree src
, tree len
, tree size
,
12740 tree maxlen
, bool ignore
,
12741 enum built_in_function fcode
)
12745 if (!validate_arg (dest
, POINTER_TYPE
)
12746 || !validate_arg (src
,
12747 (fcode
== BUILT_IN_MEMSET_CHK
12748 ? INTEGER_TYPE
: POINTER_TYPE
))
12749 || !validate_arg (len
, INTEGER_TYPE
)
12750 || !validate_arg (size
, INTEGER_TYPE
))
12753 /* If SRC and DEST are the same (and not volatile), return DEST
12754 (resp. DEST+LEN for __mempcpy_chk). */
12755 if (fcode
!= BUILT_IN_MEMSET_CHK
&& operand_equal_p (src
, dest
, 0))
12757 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
12758 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)),
12762 tree temp
= fold_build_pointer_plus_loc (loc
, dest
, len
);
12763 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), temp
);
12767 if (! tree_fits_uhwi_p (size
))
12770 if (! integer_all_onesp (size
))
12772 if (! tree_fits_uhwi_p (len
))
12774 /* If LEN is not constant, try MAXLEN too.
12775 For MAXLEN only allow optimizing into non-_ocs function
12776 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12777 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
12779 if (fcode
== BUILT_IN_MEMPCPY_CHK
&& ignore
)
12781 /* (void) __mempcpy_chk () can be optimized into
12782 (void) __memcpy_chk (). */
12783 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
12787 return build_call_expr_loc (loc
, fn
, 4, dest
, src
, len
, size
);
12795 if (tree_int_cst_lt (size
, maxlen
))
12800 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
12801 mem{cpy,pcpy,move,set} is available. */
12804 case BUILT_IN_MEMCPY_CHK
:
12805 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
12807 case BUILT_IN_MEMPCPY_CHK
:
12808 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
12810 case BUILT_IN_MEMMOVE_CHK
:
12811 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
12813 case BUILT_IN_MEMSET_CHK
:
12814 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
12823 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
12826 /* Fold a call to the __st[rp]cpy_chk builtin.
12827 DEST, SRC, and SIZE are the arguments to the call.
12828 IGNORE is true if return value can be ignored. FCODE is the BUILT_IN_*
12829 code of the builtin. If MAXLEN is not NULL, it is maximum length of
12830 strings passed as second argument. */
12833 fold_builtin_stxcpy_chk (location_t loc
, tree fndecl
, tree dest
,
12834 tree src
, tree size
,
12835 tree maxlen
, bool ignore
,
12836 enum built_in_function fcode
)
12840 if (!validate_arg (dest
, POINTER_TYPE
)
12841 || !validate_arg (src
, POINTER_TYPE
)
12842 || !validate_arg (size
, INTEGER_TYPE
))
12845 /* If SRC and DEST are the same (and not volatile), return DEST. */
12846 if (fcode
== BUILT_IN_STRCPY_CHK
&& operand_equal_p (src
, dest
, 0))
12847 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
);
12849 if (! tree_fits_uhwi_p (size
))
12852 if (! integer_all_onesp (size
))
12854 len
= c_strlen (src
, 1);
12855 if (! len
|| ! tree_fits_uhwi_p (len
))
12857 /* If LEN is not constant, try MAXLEN too.
12858 For MAXLEN only allow optimizing into non-_ocs function
12859 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12860 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
12862 if (fcode
== BUILT_IN_STPCPY_CHK
)
12867 /* If return value of __stpcpy_chk is ignored,
12868 optimize into __strcpy_chk. */
12869 fn
= builtin_decl_explicit (BUILT_IN_STRCPY_CHK
);
12873 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, size
);
12876 if (! len
|| TREE_SIDE_EFFECTS (len
))
12879 /* If c_strlen returned something, but not a constant,
12880 transform __strcpy_chk into __memcpy_chk. */
12881 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
12885 len
= fold_convert_loc (loc
, size_type_node
, len
);
12886 len
= size_binop_loc (loc
, PLUS_EXPR
, len
,
12887 build_int_cst (size_type_node
, 1));
12888 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)),
12889 build_call_expr_loc (loc
, fn
, 4,
12890 dest
, src
, len
, size
));
12896 if (! tree_int_cst_lt (maxlen
, size
))
12900 /* If __builtin_st{r,p}cpy_chk is used, assume st{r,p}cpy is available. */
12901 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPCPY_CHK
12902 ? BUILT_IN_STPCPY
: BUILT_IN_STRCPY
);
12906 return build_call_expr_loc (loc
, fn
, 2, dest
, src
);
12909 /* Fold a call to the __st{r,p}ncpy_chk builtin. DEST, SRC, LEN, and SIZE
12910 are the arguments to the call. If MAXLEN is not NULL, it is maximum
12911 length passed as third argument. IGNORE is true if return value can be
12912 ignored. FCODE is the BUILT_IN_* code of the builtin. */
12915 fold_builtin_stxncpy_chk (location_t loc
, tree dest
, tree src
,
12916 tree len
, tree size
, tree maxlen
, bool ignore
,
12917 enum built_in_function fcode
)
12921 if (!validate_arg (dest
, POINTER_TYPE
)
12922 || !validate_arg (src
, POINTER_TYPE
)
12923 || !validate_arg (len
, INTEGER_TYPE
)
12924 || !validate_arg (size
, INTEGER_TYPE
))
12927 if (fcode
== BUILT_IN_STPNCPY_CHK
&& ignore
)
12929 /* If return value of __stpncpy_chk is ignored,
12930 optimize into __strncpy_chk. */
12931 fn
= builtin_decl_explicit (BUILT_IN_STRNCPY_CHK
);
12933 return build_call_expr_loc (loc
, fn
, 4, dest
, src
, len
, size
);
12936 if (! tree_fits_uhwi_p (size
))
12939 if (! integer_all_onesp (size
))
12941 if (! tree_fits_uhwi_p (len
))
12943 /* If LEN is not constant, try MAXLEN too.
12944 For MAXLEN only allow optimizing into non-_ocs function
12945 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
12946 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
12952 if (tree_int_cst_lt (size
, maxlen
))
12956 /* If __builtin_st{r,p}ncpy_chk is used, assume st{r,p}ncpy is available. */
12957 fn
= builtin_decl_explicit (fcode
== BUILT_IN_STPNCPY_CHK
12958 ? BUILT_IN_STPNCPY
: BUILT_IN_STRNCPY
);
12962 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
12965 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
12966 are the arguments to the call. */
12969 fold_builtin_strcat_chk (location_t loc
, tree fndecl
, tree dest
,
12970 tree src
, tree size
)
12975 if (!validate_arg (dest
, POINTER_TYPE
)
12976 || !validate_arg (src
, POINTER_TYPE
)
12977 || !validate_arg (size
, INTEGER_TYPE
))
12980 p
= c_getstr (src
);
12981 /* If the SRC parameter is "", return DEST. */
12982 if (p
&& *p
== '\0')
12983 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
12985 if (! tree_fits_uhwi_p (size
) || ! integer_all_onesp (size
))
12988 /* If __builtin_strcat_chk is used, assume strcat is available. */
12989 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
12993 return build_call_expr_loc (loc
, fn
, 2, dest
, src
);
12996 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
13000 fold_builtin_strncat_chk (location_t loc
, tree fndecl
,
13001 tree dest
, tree src
, tree len
, tree size
)
13006 if (!validate_arg (dest
, POINTER_TYPE
)
13007 || !validate_arg (src
, POINTER_TYPE
)
13008 || !validate_arg (size
, INTEGER_TYPE
)
13009 || !validate_arg (size
, INTEGER_TYPE
))
13012 p
= c_getstr (src
);
13013 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
13014 if (p
&& *p
== '\0')
13015 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, len
);
13016 else if (integer_zerop (len
))
13017 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
13019 if (! tree_fits_uhwi_p (size
))
13022 if (! integer_all_onesp (size
))
13024 tree src_len
= c_strlen (src
, 1);
13026 && tree_fits_uhwi_p (src_len
)
13027 && tree_fits_uhwi_p (len
)
13028 && ! tree_int_cst_lt (len
, src_len
))
13030 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
13031 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
13035 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, size
);
13040 /* If __builtin_strncat_chk is used, assume strncat is available. */
13041 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
13045 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
13048 /* Fold a call EXP to __{,v}sprintf_chk having NARGS passed as ARGS.
13049 Return NULL_TREE if a normal call should be emitted rather than
13050 expanding the function inline. FCODE is either BUILT_IN_SPRINTF_CHK
13051 or BUILT_IN_VSPRINTF_CHK. */
13054 fold_builtin_sprintf_chk_1 (location_t loc
, int nargs
, tree
*args
,
13055 enum built_in_function fcode
)
13057 tree dest
, size
, len
, fn
, fmt
, flag
;
13058 const char *fmt_str
;
13060 /* Verify the required arguments in the original call. */
13064 if (!validate_arg (dest
, POINTER_TYPE
))
13067 if (!validate_arg (flag
, INTEGER_TYPE
))
13070 if (!validate_arg (size
, INTEGER_TYPE
))
13073 if (!validate_arg (fmt
, POINTER_TYPE
))
13076 if (! tree_fits_uhwi_p (size
))
13081 if (!init_target_chars ())
13084 /* Check whether the format is a literal string constant. */
13085 fmt_str
= c_getstr (fmt
);
13086 if (fmt_str
!= NULL
)
13088 /* If the format doesn't contain % args or %%, we know the size. */
13089 if (strchr (fmt_str
, target_percent
) == 0)
13091 if (fcode
!= BUILT_IN_SPRINTF_CHK
|| nargs
== 4)
13092 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
13094 /* If the format is "%s" and first ... argument is a string literal,
13095 we know the size too. */
13096 else if (fcode
== BUILT_IN_SPRINTF_CHK
13097 && strcmp (fmt_str
, target_percent_s
) == 0)
13104 if (validate_arg (arg
, POINTER_TYPE
))
13106 len
= c_strlen (arg
, 1);
13107 if (! len
|| ! tree_fits_uhwi_p (len
))
13114 if (! integer_all_onesp (size
))
13116 if (! len
|| ! tree_int_cst_lt (len
, size
))
13120 /* Only convert __{,v}sprintf_chk to {,v}sprintf if flag is 0
13121 or if format doesn't contain % chars or is "%s". */
13122 if (! integer_zerop (flag
))
13124 if (fmt_str
== NULL
)
13126 if (strchr (fmt_str
, target_percent
) != NULL
13127 && strcmp (fmt_str
, target_percent_s
))
13131 /* If __builtin_{,v}sprintf_chk is used, assume {,v}sprintf is available. */
13132 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSPRINTF_CHK
13133 ? BUILT_IN_VSPRINTF
: BUILT_IN_SPRINTF
);
13137 return rewrite_call_expr_array (loc
, nargs
, args
, 4, fn
, 2, dest
, fmt
);
13140 /* Fold a call EXP to __{,v}sprintf_chk. Return NULL_TREE if
13141 a normal call should be emitted rather than expanding the function
13142 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
13145 fold_builtin_sprintf_chk (location_t loc
, tree exp
,
13146 enum built_in_function fcode
)
13148 return fold_builtin_sprintf_chk_1 (loc
, call_expr_nargs (exp
),
13149 CALL_EXPR_ARGP (exp
), fcode
);
13152 /* Fold a call EXP to {,v}snprintf having NARGS passed as ARGS. Return
13153 NULL_TREE if a normal call should be emitted rather than expanding
13154 the function inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13155 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13156 passed as second argument. */
13159 fold_builtin_snprintf_chk_1 (location_t loc
, int nargs
, tree
*args
,
13160 tree maxlen
, enum built_in_function fcode
)
13162 tree dest
, size
, len
, fn
, fmt
, flag
;
13163 const char *fmt_str
;
13165 /* Verify the required arguments in the original call. */
13169 if (!validate_arg (dest
, POINTER_TYPE
))
13172 if (!validate_arg (len
, INTEGER_TYPE
))
13175 if (!validate_arg (flag
, INTEGER_TYPE
))
13178 if (!validate_arg (size
, INTEGER_TYPE
))
13181 if (!validate_arg (fmt
, POINTER_TYPE
))
13184 if (! tree_fits_uhwi_p (size
))
13187 if (! integer_all_onesp (size
))
13189 if (! tree_fits_uhwi_p (len
))
13191 /* If LEN is not constant, try MAXLEN too.
13192 For MAXLEN only allow optimizing into non-_ocs function
13193 if SIZE is >= MAXLEN, never convert to __ocs_fail (). */
13194 if (maxlen
== NULL_TREE
|| ! tree_fits_uhwi_p (maxlen
))
13200 if (tree_int_cst_lt (size
, maxlen
))
13204 if (!init_target_chars ())
13207 /* Only convert __{,v}snprintf_chk to {,v}snprintf if flag is 0
13208 or if format doesn't contain % chars or is "%s". */
13209 if (! integer_zerop (flag
))
13211 fmt_str
= c_getstr (fmt
);
13212 if (fmt_str
== NULL
)
13214 if (strchr (fmt_str
, target_percent
) != NULL
13215 && strcmp (fmt_str
, target_percent_s
))
13219 /* If __builtin_{,v}snprintf_chk is used, assume {,v}snprintf is
13221 fn
= builtin_decl_explicit (fcode
== BUILT_IN_VSNPRINTF_CHK
13222 ? BUILT_IN_VSNPRINTF
: BUILT_IN_SNPRINTF
);
13226 return rewrite_call_expr_array (loc
, nargs
, args
, 5, fn
, 3, dest
, len
, fmt
);
13229 /* Fold a call EXP to {,v}snprintf. Return NULL_TREE if
13230 a normal call should be emitted rather than expanding the function
13231 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
13232 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
13233 passed as second argument. */
13236 fold_builtin_snprintf_chk (location_t loc
, tree exp
, tree maxlen
,
13237 enum built_in_function fcode
)
13239 return fold_builtin_snprintf_chk_1 (loc
, call_expr_nargs (exp
),
13240 CALL_EXPR_ARGP (exp
), maxlen
, fcode
);
13243 /* Builtins with folding operations that operate on "..." arguments
13244 need special handling; we need to store the arguments in a convenient
13245 data structure before attempting any folding. Fortunately there are
13246 only a few builtins that fall into this category. FNDECL is the
13247 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
13248 result of the function call is ignored. */
13251 fold_builtin_varargs (location_t loc
, tree fndecl
, tree exp
,
13252 bool ignore ATTRIBUTE_UNUSED
)
13254 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
13255 tree ret
= NULL_TREE
;
13259 case BUILT_IN_SPRINTF_CHK
:
13260 case BUILT_IN_VSPRINTF_CHK
:
13261 ret
= fold_builtin_sprintf_chk (loc
, exp
, fcode
);
13264 case BUILT_IN_SNPRINTF_CHK
:
13265 case BUILT_IN_VSNPRINTF_CHK
:
13266 ret
= fold_builtin_snprintf_chk (loc
, exp
, NULL_TREE
, fcode
);
13269 case BUILT_IN_FPCLASSIFY
:
13270 ret
= fold_builtin_fpclassify (loc
, exp
);
13278 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
13279 SET_EXPR_LOCATION (ret
, loc
);
13280 TREE_NO_WARNING (ret
) = 1;
13286 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
13287 FMT and ARG are the arguments to the call; we don't fold cases with
13288 more than 2 arguments, and ARG may be null if this is a 1-argument case.
13290 Return NULL_TREE if no simplification was possible, otherwise return the
13291 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13292 code of the function to be simplified. */
13295 fold_builtin_printf (location_t loc
, tree fndecl
, tree fmt
,
13296 tree arg
, bool ignore
,
13297 enum built_in_function fcode
)
13299 tree fn_putchar
, fn_puts
, newarg
, call
= NULL_TREE
;
13300 const char *fmt_str
= NULL
;
13302 /* If the return value is used, don't do the transformation. */
13306 /* Verify the required arguments in the original call. */
13307 if (!validate_arg (fmt
, POINTER_TYPE
))
13310 /* Check whether the format is a literal string constant. */
13311 fmt_str
= c_getstr (fmt
);
13312 if (fmt_str
== NULL
)
13315 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
13317 /* If we're using an unlocked function, assume the other
13318 unlocked functions exist explicitly. */
13319 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
13320 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
13324 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
13325 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
13328 if (!init_target_chars ())
13331 if (strcmp (fmt_str
, target_percent_s
) == 0
13332 || strchr (fmt_str
, target_percent
) == NULL
)
13336 if (strcmp (fmt_str
, target_percent_s
) == 0)
13338 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
13341 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
13344 str
= c_getstr (arg
);
13350 /* The format specifier doesn't contain any '%' characters. */
13351 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
13357 /* If the string was "", printf does nothing. */
13358 if (str
[0] == '\0')
13359 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
13361 /* If the string has length of 1, call putchar. */
13362 if (str
[1] == '\0')
13364 /* Given printf("c"), (where c is any one character,)
13365 convert "c"[0] to an int and pass that to the replacement
13367 newarg
= build_int_cst (integer_type_node
, str
[0]);
13369 call
= build_call_expr_loc (loc
, fn_putchar
, 1, newarg
);
13373 /* If the string was "string\n", call puts("string"). */
13374 size_t len
= strlen (str
);
13375 if ((unsigned char)str
[len
- 1] == target_newline
13376 && (size_t) (int) len
== len
13380 tree offset_node
, string_cst
;
13382 /* Create a NUL-terminated string that's one char shorter
13383 than the original, stripping off the trailing '\n'. */
13384 newarg
= build_string_literal (len
, str
);
13385 string_cst
= string_constant (newarg
, &offset_node
);
13386 gcc_checking_assert (string_cst
13387 && (TREE_STRING_LENGTH (string_cst
)
13389 && integer_zerop (offset_node
)
13391 TREE_STRING_POINTER (string_cst
)[len
- 1]
13392 == target_newline
);
13393 /* build_string_literal creates a new STRING_CST,
13394 modify it in place to avoid double copying. */
13395 newstr
= CONST_CAST (char *, TREE_STRING_POINTER (string_cst
));
13396 newstr
[len
- 1] = '\0';
13398 call
= build_call_expr_loc (loc
, fn_puts
, 1, newarg
);
13401 /* We'd like to arrange to call fputs(string,stdout) here,
13402 but we need stdout and don't have a way to get it yet. */
13407 /* The other optimizations can be done only on the non-va_list variants. */
13408 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
13411 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
13412 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
13414 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
13417 call
= build_call_expr_loc (loc
, fn_puts
, 1, arg
);
13420 /* If the format specifier was "%c", call __builtin_putchar(arg). */
13421 else if (strcmp (fmt_str
, target_percent_c
) == 0)
13423 if (!arg
|| !validate_arg (arg
, INTEGER_TYPE
))
13426 call
= build_call_expr_loc (loc
, fn_putchar
, 1, arg
);
13432 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), call
);
13435 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
13436 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
13437 more than 3 arguments, and ARG may be null in the 2-argument case.
13439 Return NULL_TREE if no simplification was possible, otherwise return the
13440 simplified form of the call as a tree. FCODE is the BUILT_IN_*
13441 code of the function to be simplified. */
13444 fold_builtin_fprintf (location_t loc
, tree fndecl
, tree fp
,
13445 tree fmt
, tree arg
, bool ignore
,
13446 enum built_in_function fcode
)
13448 tree fn_fputc
, fn_fputs
, call
= NULL_TREE
;
13449 const char *fmt_str
= NULL
;
13451 /* If the return value is used, don't do the transformation. */
13455 /* Verify the required arguments in the original call. */
13456 if (!validate_arg (fp
, POINTER_TYPE
))
13458 if (!validate_arg (fmt
, POINTER_TYPE
))
13461 /* Check whether the format is a literal string constant. */
13462 fmt_str
= c_getstr (fmt
);
13463 if (fmt_str
== NULL
)
13466 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
13468 /* If we're using an unlocked function, assume the other
13469 unlocked functions exist explicitly. */
13470 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
13471 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
13475 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
13476 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
13479 if (!init_target_chars ())
13482 /* If the format doesn't contain % args or %%, use strcpy. */
13483 if (strchr (fmt_str
, target_percent
) == NULL
)
13485 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
13489 /* If the format specifier was "", fprintf does nothing. */
13490 if (fmt_str
[0] == '\0')
13492 /* If FP has side-effects, just wait until gimplification is
13494 if (TREE_SIDE_EFFECTS (fp
))
13497 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
13500 /* When "string" doesn't contain %, replace all cases of
13501 fprintf (fp, string) with fputs (string, fp). The fputs
13502 builtin will take care of special cases like length == 1. */
13504 call
= build_call_expr_loc (loc
, fn_fputs
, 2, fmt
, fp
);
13507 /* The other optimizations can be done only on the non-va_list variants. */
13508 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
13511 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
13512 else if (strcmp (fmt_str
, target_percent_s
) == 0)
13514 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
13517 call
= build_call_expr_loc (loc
, fn_fputs
, 2, arg
, fp
);
13520 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
13521 else if (strcmp (fmt_str
, target_percent_c
) == 0)
13523 if (!arg
|| !validate_arg (arg
, INTEGER_TYPE
))
13526 call
= build_call_expr_loc (loc
, fn_fputc
, 2, arg
, fp
);
13531 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), call
);
13534 /* Initialize format string characters in the target charset. */
13537 init_target_chars (void)
13542 target_newline
= lang_hooks
.to_target_charset ('\n');
13543 target_percent
= lang_hooks
.to_target_charset ('%');
13544 target_c
= lang_hooks
.to_target_charset ('c');
13545 target_s
= lang_hooks
.to_target_charset ('s');
13546 if (target_newline
== 0 || target_percent
== 0 || target_c
== 0
13550 target_percent_c
[0] = target_percent
;
13551 target_percent_c
[1] = target_c
;
13552 target_percent_c
[2] = '\0';
13554 target_percent_s
[0] = target_percent
;
13555 target_percent_s
[1] = target_s
;
13556 target_percent_s
[2] = '\0';
13558 target_percent_s_newline
[0] = target_percent
;
13559 target_percent_s_newline
[1] = target_s
;
13560 target_percent_s_newline
[2] = target_newline
;
13561 target_percent_s_newline
[3] = '\0';
13568 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
13569 and no overflow/underflow occurred. INEXACT is true if M was not
13570 exactly calculated. TYPE is the tree type for the result. This
13571 function assumes that you cleared the MPFR flags and then
13572 calculated M to see if anything subsequently set a flag prior to
13573 entering this function. Return NULL_TREE if any checks fail. */
13576 do_mpfr_ckconv (mpfr_srcptr m
, tree type
, int inexact
)
13578 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13579 overflow/underflow occurred. If -frounding-math, proceed iff the
13580 result of calling FUNC was exact. */
13581 if (mpfr_number_p (m
) && !mpfr_overflow_p () && !mpfr_underflow_p ()
13582 && (!flag_rounding_math
|| !inexact
))
13584 REAL_VALUE_TYPE rr
;
13586 real_from_mpfr (&rr
, m
, type
, GMP_RNDN
);
13587 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
13588 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13589 but the mpft_t is not, then we underflowed in the
13591 if (real_isfinite (&rr
)
13592 && (rr
.cl
== rvc_zero
) == (mpfr_zero_p (m
) != 0))
13594 REAL_VALUE_TYPE rmode
;
13596 real_convert (&rmode
, TYPE_MODE (type
), &rr
);
13597 /* Proceed iff the specified mode can hold the value. */
13598 if (real_identical (&rmode
, &rr
))
13599 return build_real (type
, rmode
);
13605 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
13606 number and no overflow/underflow occurred. INEXACT is true if M
13607 was not exactly calculated. TYPE is the tree type for the result.
13608 This function assumes that you cleared the MPFR flags and then
13609 calculated M to see if anything subsequently set a flag prior to
13610 entering this function. Return NULL_TREE if any checks fail, if
13611 FORCE_CONVERT is true, then bypass the checks. */
13614 do_mpc_ckconv (mpc_srcptr m
, tree type
, int inexact
, int force_convert
)
13616 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
13617 overflow/underflow occurred. If -frounding-math, proceed iff the
13618 result of calling FUNC was exact. */
13620 || (mpfr_number_p (mpc_realref (m
)) && mpfr_number_p (mpc_imagref (m
))
13621 && !mpfr_overflow_p () && !mpfr_underflow_p ()
13622 && (!flag_rounding_math
|| !inexact
)))
13624 REAL_VALUE_TYPE re
, im
;
13626 real_from_mpfr (&re
, mpc_realref (m
), TREE_TYPE (type
), GMP_RNDN
);
13627 real_from_mpfr (&im
, mpc_imagref (m
), TREE_TYPE (type
), GMP_RNDN
);
13628 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
13629 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
13630 but the mpft_t is not, then we underflowed in the
13633 || (real_isfinite (&re
) && real_isfinite (&im
)
13634 && (re
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_realref (m
)) != 0)
13635 && (im
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_imagref (m
)) != 0)))
13637 REAL_VALUE_TYPE re_mode
, im_mode
;
13639 real_convert (&re_mode
, TYPE_MODE (TREE_TYPE (type
)), &re
);
13640 real_convert (&im_mode
, TYPE_MODE (TREE_TYPE (type
)), &im
);
13641 /* Proceed iff the specified mode can hold the value. */
13643 || (real_identical (&re_mode
, &re
)
13644 && real_identical (&im_mode
, &im
)))
13645 return build_complex (type
, build_real (TREE_TYPE (type
), re_mode
),
13646 build_real (TREE_TYPE (type
), im_mode
));
13652 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
13653 FUNC on it and return the resulting value as a tree with type TYPE.
13654 If MIN and/or MAX are not NULL, then the supplied ARG must be
13655 within those bounds. If INCLUSIVE is true, then MIN/MAX are
13656 acceptable values, otherwise they are not. The mpfr precision is
13657 set to the precision of TYPE. We assume that function FUNC returns
13658 zero if the result could be calculated exactly within the requested
13662 do_mpfr_arg1 (tree arg
, tree type
, int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
13663 const REAL_VALUE_TYPE
*min
, const REAL_VALUE_TYPE
*max
,
13666 tree result
= NULL_TREE
;
13670 /* To proceed, MPFR must exactly represent the target floating point
13671 format, which only happens when the target base equals two. */
13672 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13673 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
13675 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
13677 if (real_isfinite (ra
)
13678 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
))
13679 && (!max
|| real_compare (inclusive
? LE_EXPR
: LT_EXPR
, ra
, max
)))
13681 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13682 const int prec
= fmt
->p
;
13683 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13687 mpfr_init2 (m
, prec
);
13688 mpfr_from_real (m
, ra
, GMP_RNDN
);
13689 mpfr_clear_flags ();
13690 inexact
= func (m
, m
, rnd
);
13691 result
= do_mpfr_ckconv (m
, type
, inexact
);
13699 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
13700 FUNC on it and return the resulting value as a tree with type TYPE.
13701 The mpfr precision is set to the precision of TYPE. We assume that
13702 function FUNC returns zero if the result could be calculated
13703 exactly within the requested precision. */
13706 do_mpfr_arg2 (tree arg1
, tree arg2
, tree type
,
13707 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
13709 tree result
= NULL_TREE
;
13714 /* To proceed, MPFR must exactly represent the target floating point
13715 format, which only happens when the target base equals two. */
13716 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13717 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
)
13718 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
))
13720 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
13721 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
13723 if (real_isfinite (ra1
) && real_isfinite (ra2
))
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
;
13731 mpfr_inits2 (prec
, m1
, m2
, NULL
);
13732 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
13733 mpfr_from_real (m2
, ra2
, GMP_RNDN
);
13734 mpfr_clear_flags ();
13735 inexact
= func (m1
, m1
, m2
, rnd
);
13736 result
= do_mpfr_ckconv (m1
, type
, inexact
);
13737 mpfr_clears (m1
, m2
, NULL
);
13744 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
13745 FUNC on it and return the resulting value as a tree with type TYPE.
13746 The mpfr precision is set to the precision of TYPE. We assume that
13747 function FUNC returns zero if the result could be calculated
13748 exactly within the requested precision. */
13751 do_mpfr_arg3 (tree arg1
, tree arg2
, tree arg3
, tree type
,
13752 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
13754 tree result
= NULL_TREE
;
13760 /* To proceed, MPFR must exactly represent the target floating point
13761 format, which only happens when the target base equals two. */
13762 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13763 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
)
13764 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
)
13765 && TREE_CODE (arg3
) == REAL_CST
&& !TREE_OVERFLOW (arg3
))
13767 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
13768 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
13769 const REAL_VALUE_TYPE
*const ra3
= &TREE_REAL_CST (arg3
);
13771 if (real_isfinite (ra1
) && real_isfinite (ra2
) && real_isfinite (ra3
))
13773 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13774 const int prec
= fmt
->p
;
13775 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13779 mpfr_inits2 (prec
, m1
, m2
, m3
, NULL
);
13780 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
13781 mpfr_from_real (m2
, ra2
, GMP_RNDN
);
13782 mpfr_from_real (m3
, ra3
, GMP_RNDN
);
13783 mpfr_clear_flags ();
13784 inexact
= func (m1
, m1
, m2
, m3
, rnd
);
13785 result
= do_mpfr_ckconv (m1
, type
, inexact
);
13786 mpfr_clears (m1
, m2
, m3
, NULL
);
13793 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
13794 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
13795 If ARG_SINP and ARG_COSP are NULL then the result is returned
13796 as a complex value.
13797 The type is taken from the type of ARG and is used for setting the
13798 precision of the calculation and results. */
13801 do_mpfr_sincos (tree arg
, tree arg_sinp
, tree arg_cosp
)
13803 tree
const type
= TREE_TYPE (arg
);
13804 tree result
= NULL_TREE
;
13808 /* To proceed, MPFR must exactly represent the target floating point
13809 format, which only happens when the target base equals two. */
13810 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13811 && TREE_CODE (arg
) == REAL_CST
13812 && !TREE_OVERFLOW (arg
))
13814 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
13816 if (real_isfinite (ra
))
13818 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13819 const int prec
= fmt
->p
;
13820 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13821 tree result_s
, result_c
;
13825 mpfr_inits2 (prec
, m
, ms
, mc
, NULL
);
13826 mpfr_from_real (m
, ra
, GMP_RNDN
);
13827 mpfr_clear_flags ();
13828 inexact
= mpfr_sin_cos (ms
, mc
, m
, rnd
);
13829 result_s
= do_mpfr_ckconv (ms
, type
, inexact
);
13830 result_c
= do_mpfr_ckconv (mc
, type
, inexact
);
13831 mpfr_clears (m
, ms
, mc
, NULL
);
13832 if (result_s
&& result_c
)
13834 /* If we are to return in a complex value do so. */
13835 if (!arg_sinp
&& !arg_cosp
)
13836 return build_complex (build_complex_type (type
),
13837 result_c
, result_s
);
13839 /* Dereference the sin/cos pointer arguments. */
13840 arg_sinp
= build_fold_indirect_ref (arg_sinp
);
13841 arg_cosp
= build_fold_indirect_ref (arg_cosp
);
13842 /* Proceed if valid pointer type were passed in. */
13843 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp
)) == TYPE_MAIN_VARIANT (type
)
13844 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp
)) == TYPE_MAIN_VARIANT (type
))
13846 /* Set the values. */
13847 result_s
= fold_build2 (MODIFY_EXPR
, type
, arg_sinp
,
13849 TREE_SIDE_EFFECTS (result_s
) = 1;
13850 result_c
= fold_build2 (MODIFY_EXPR
, type
, arg_cosp
,
13852 TREE_SIDE_EFFECTS (result_c
) = 1;
13853 /* Combine the assignments into a compound expr. */
13854 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
13855 result_s
, result_c
));
13863 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
13864 two-argument mpfr order N Bessel function FUNC on them and return
13865 the resulting value as a tree with type TYPE. The mpfr precision
13866 is set to the precision of TYPE. We assume that function FUNC
13867 returns zero if the result could be calculated exactly within the
13868 requested precision. */
13870 do_mpfr_bessel_n (tree arg1
, tree arg2
, tree type
,
13871 int (*func
)(mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
13872 const REAL_VALUE_TYPE
*min
, bool inclusive
)
13874 tree result
= NULL_TREE
;
13879 /* To proceed, MPFR must exactly represent the target floating point
13880 format, which only happens when the target base equals two. */
13881 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13882 && tree_fits_shwi_p (arg1
)
13883 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
))
13885 const HOST_WIDE_INT n
= tree_to_shwi (arg1
);
13886 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg2
);
13889 && real_isfinite (ra
)
13890 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
)))
13892 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13893 const int prec
= fmt
->p
;
13894 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13898 mpfr_init2 (m
, prec
);
13899 mpfr_from_real (m
, ra
, GMP_RNDN
);
13900 mpfr_clear_flags ();
13901 inexact
= func (m
, n
, m
, rnd
);
13902 result
= do_mpfr_ckconv (m
, type
, inexact
);
13910 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
13911 the pointer *(ARG_QUO) and return the result. The type is taken
13912 from the type of ARG0 and is used for setting the precision of the
13913 calculation and results. */
13916 do_mpfr_remquo (tree arg0
, tree arg1
, tree arg_quo
)
13918 tree
const type
= TREE_TYPE (arg0
);
13919 tree result
= NULL_TREE
;
13924 /* To proceed, MPFR must exactly represent the target floating point
13925 format, which only happens when the target base equals two. */
13926 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
13927 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
13928 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
))
13930 const REAL_VALUE_TYPE
*const ra0
= TREE_REAL_CST_PTR (arg0
);
13931 const REAL_VALUE_TYPE
*const ra1
= TREE_REAL_CST_PTR (arg1
);
13933 if (real_isfinite (ra0
) && real_isfinite (ra1
))
13935 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
13936 const int prec
= fmt
->p
;
13937 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
13942 mpfr_inits2 (prec
, m0
, m1
, NULL
);
13943 mpfr_from_real (m0
, ra0
, GMP_RNDN
);
13944 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
13945 mpfr_clear_flags ();
13946 mpfr_remquo (m0
, &integer_quo
, m0
, m1
, rnd
);
13947 /* Remquo is independent of the rounding mode, so pass
13948 inexact=0 to do_mpfr_ckconv(). */
13949 result_rem
= do_mpfr_ckconv (m0
, type
, /*inexact=*/ 0);
13950 mpfr_clears (m0
, m1
, NULL
);
13953 /* MPFR calculates quo in the host's long so it may
13954 return more bits in quo than the target int can hold
13955 if sizeof(host long) > sizeof(target int). This can
13956 happen even for native compilers in LP64 mode. In
13957 these cases, modulo the quo value with the largest
13958 number that the target int can hold while leaving one
13959 bit for the sign. */
13960 if (sizeof (integer_quo
) * CHAR_BIT
> INT_TYPE_SIZE
)
13961 integer_quo
%= (long)(1UL << (INT_TYPE_SIZE
- 1));
13963 /* Dereference the quo pointer argument. */
13964 arg_quo
= build_fold_indirect_ref (arg_quo
);
13965 /* Proceed iff a valid pointer type was passed in. */
13966 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo
)) == integer_type_node
)
13968 /* Set the value. */
13970 = fold_build2 (MODIFY_EXPR
, TREE_TYPE (arg_quo
), arg_quo
,
13971 build_int_cst (TREE_TYPE (arg_quo
),
13973 TREE_SIDE_EFFECTS (result_quo
) = 1;
13974 /* Combine the quo assignment with the rem. */
13975 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
13976 result_quo
, result_rem
));
13984 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
13985 resulting value as a tree with type TYPE. The mpfr precision is
13986 set to the precision of TYPE. We assume that this mpfr function
13987 returns zero if the result could be calculated exactly within the
13988 requested precision. In addition, the integer pointer represented
13989 by ARG_SG will be dereferenced and set to the appropriate signgam
13993 do_mpfr_lgamma_r (tree arg
, tree arg_sg
, tree type
)
13995 tree result
= NULL_TREE
;
13999 /* To proceed, MPFR must exactly represent the target floating point
14000 format, which only happens when the target base equals two. Also
14001 verify ARG is a constant and that ARG_SG is an int pointer. */
14002 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
14003 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
)
14004 && TREE_CODE (TREE_TYPE (arg_sg
)) == POINTER_TYPE
14005 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg
))) == integer_type_node
)
14007 const REAL_VALUE_TYPE
*const ra
= TREE_REAL_CST_PTR (arg
);
14009 /* In addition to NaN and Inf, the argument cannot be zero or a
14010 negative integer. */
14011 if (real_isfinite (ra
)
14012 && ra
->cl
!= rvc_zero
14013 && !(real_isneg (ra
) && real_isinteger (ra
, TYPE_MODE (type
))))
14015 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
14016 const int prec
= fmt
->p
;
14017 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
14022 mpfr_init2 (m
, prec
);
14023 mpfr_from_real (m
, ra
, GMP_RNDN
);
14024 mpfr_clear_flags ();
14025 inexact
= mpfr_lgamma (m
, &sg
, m
, rnd
);
14026 result_lg
= do_mpfr_ckconv (m
, type
, inexact
);
14032 /* Dereference the arg_sg pointer argument. */
14033 arg_sg
= build_fold_indirect_ref (arg_sg
);
14034 /* Assign the signgam value into *arg_sg. */
14035 result_sg
= fold_build2 (MODIFY_EXPR
,
14036 TREE_TYPE (arg_sg
), arg_sg
,
14037 build_int_cst (TREE_TYPE (arg_sg
), sg
));
14038 TREE_SIDE_EFFECTS (result_sg
) = 1;
14039 /* Combine the signgam assignment with the lgamma result. */
14040 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
14041 result_sg
, result_lg
));
14049 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
14050 function FUNC on it and return the resulting value as a tree with
14051 type TYPE. The mpfr precision is set to the precision of TYPE. We
14052 assume that function FUNC returns zero if the result could be
14053 calculated exactly within the requested precision. */
14056 do_mpc_arg1 (tree arg
, tree type
, int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_rnd_t
))
14058 tree result
= NULL_TREE
;
14062 /* To proceed, MPFR must exactly represent the target floating point
14063 format, which only happens when the target base equals two. */
14064 if (TREE_CODE (arg
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg
)
14065 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
14066 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg
))))->b
== 2)
14068 const REAL_VALUE_TYPE
*const re
= TREE_REAL_CST_PTR (TREE_REALPART (arg
));
14069 const REAL_VALUE_TYPE
*const im
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg
));
14071 if (real_isfinite (re
) && real_isfinite (im
))
14073 const struct real_format
*const fmt
=
14074 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
14075 const int prec
= fmt
->p
;
14076 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
14077 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
14081 mpc_init2 (m
, prec
);
14082 mpfr_from_real (mpc_realref (m
), re
, rnd
);
14083 mpfr_from_real (mpc_imagref (m
), im
, rnd
);
14084 mpfr_clear_flags ();
14085 inexact
= func (m
, m
, crnd
);
14086 result
= do_mpc_ckconv (m
, type
, inexact
, /*force_convert=*/ 0);
14094 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
14095 mpc function FUNC on it and return the resulting value as a tree
14096 with type TYPE. The mpfr precision is set to the precision of
14097 TYPE. We assume that function FUNC returns zero if the result
14098 could be calculated exactly within the requested precision. If
14099 DO_NONFINITE is true, then fold expressions containing Inf or NaN
14100 in the arguments and/or results. */
14103 do_mpc_arg2 (tree arg0
, tree arg1
, tree type
, int do_nonfinite
,
14104 int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_srcptr
, mpc_rnd_t
))
14106 tree result
= NULL_TREE
;
14111 /* To proceed, MPFR must exactly represent the target floating point
14112 format, which only happens when the target base equals two. */
14113 if (TREE_CODE (arg0
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg0
)
14114 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
14115 && TREE_CODE (arg1
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg1
)
14116 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
14117 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0
))))->b
== 2)
14119 const REAL_VALUE_TYPE
*const re0
= TREE_REAL_CST_PTR (TREE_REALPART (arg0
));
14120 const REAL_VALUE_TYPE
*const im0
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg0
));
14121 const REAL_VALUE_TYPE
*const re1
= TREE_REAL_CST_PTR (TREE_REALPART (arg1
));
14122 const REAL_VALUE_TYPE
*const im1
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg1
));
14125 || (real_isfinite (re0
) && real_isfinite (im0
)
14126 && real_isfinite (re1
) && real_isfinite (im1
)))
14128 const struct real_format
*const fmt
=
14129 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
14130 const int prec
= fmt
->p
;
14131 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
14132 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
14136 mpc_init2 (m0
, prec
);
14137 mpc_init2 (m1
, prec
);
14138 mpfr_from_real (mpc_realref (m0
), re0
, rnd
);
14139 mpfr_from_real (mpc_imagref (m0
), im0
, rnd
);
14140 mpfr_from_real (mpc_realref (m1
), re1
, rnd
);
14141 mpfr_from_real (mpc_imagref (m1
), im1
, rnd
);
14142 mpfr_clear_flags ();
14143 inexact
= func (m0
, m0
, m1
, crnd
);
14144 result
= do_mpc_ckconv (m0
, type
, inexact
, do_nonfinite
);
14153 /* Fold a call STMT to __{,v}sprintf_chk. Return NULL_TREE if
14154 a normal call should be emitted rather than expanding the function
14155 inline. FCODE is either BUILT_IN_SPRINTF_CHK or BUILT_IN_VSPRINTF_CHK. */
14158 gimple_fold_builtin_sprintf_chk (gimple stmt
, enum built_in_function fcode
)
14160 int nargs
= gimple_call_num_args (stmt
);
14162 return fold_builtin_sprintf_chk_1 (gimple_location (stmt
), nargs
,
14164 ? gimple_call_arg_ptr (stmt
, 0)
14165 : &error_mark_node
), fcode
);
14168 /* Fold a call STMT to {,v}snprintf. Return NULL_TREE if
14169 a normal call should be emitted rather than expanding the function
14170 inline. FCODE is either BUILT_IN_SNPRINTF_CHK or
14171 BUILT_IN_VSNPRINTF_CHK. If MAXLEN is not NULL, it is maximum length
14172 passed as second argument. */
14175 gimple_fold_builtin_snprintf_chk (gimple stmt
, tree maxlen
,
14176 enum built_in_function fcode
)
14178 int nargs
= gimple_call_num_args (stmt
);
14180 return fold_builtin_snprintf_chk_1 (gimple_location (stmt
), nargs
,
14182 ? gimple_call_arg_ptr (stmt
, 0)
14183 : &error_mark_node
), maxlen
, fcode
);
14186 /* Builtins with folding operations that operate on "..." arguments
14187 need special handling; we need to store the arguments in a convenient
14188 data structure before attempting any folding. Fortunately there are
14189 only a few builtins that fall into this category. FNDECL is the
14190 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
14191 result of the function call is ignored. */
14194 gimple_fold_builtin_varargs (tree fndecl
, gimple stmt
,
14195 bool ignore ATTRIBUTE_UNUSED
)
14197 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
14198 tree ret
= NULL_TREE
;
14202 case BUILT_IN_SPRINTF_CHK
:
14203 case BUILT_IN_VSPRINTF_CHK
:
14204 ret
= gimple_fold_builtin_sprintf_chk (stmt
, fcode
);
14207 case BUILT_IN_SNPRINTF_CHK
:
14208 case BUILT_IN_VSNPRINTF_CHK
:
14209 ret
= gimple_fold_builtin_snprintf_chk (stmt
, NULL_TREE
, fcode
);
14216 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
14217 TREE_NO_WARNING (ret
) = 1;
14223 /* A wrapper function for builtin folding that prevents warnings for
14224 "statement without effect" and the like, caused by removing the
14225 call node earlier than the warning is generated. */
14228 fold_call_stmt (gimple stmt
, bool ignore
)
14230 tree ret
= NULL_TREE
;
14231 tree fndecl
= gimple_call_fndecl (stmt
);
14232 location_t loc
= gimple_location (stmt
);
14234 && TREE_CODE (fndecl
) == FUNCTION_DECL
14235 && DECL_BUILT_IN (fndecl
)
14236 && !gimple_call_va_arg_pack_p (stmt
))
14238 int nargs
= gimple_call_num_args (stmt
);
14239 tree
*args
= (nargs
> 0
14240 ? gimple_call_arg_ptr (stmt
, 0)
14241 : &error_mark_node
);
14243 if (avoid_folding_inline_builtin (fndecl
))
14245 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
14247 return targetm
.fold_builtin (fndecl
, nargs
, args
, ignore
);
14251 if (nargs
<= MAX_ARGS_TO_FOLD_BUILTIN
)
14252 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
14254 ret
= gimple_fold_builtin_varargs (fndecl
, stmt
, ignore
);
14257 /* Propagate location information from original call to
14258 expansion of builtin. Otherwise things like
14259 maybe_emit_chk_warning, that operate on the expansion
14260 of a builtin, will use the wrong location information. */
14261 if (gimple_has_location (stmt
))
14263 tree realret
= ret
;
14264 if (TREE_CODE (ret
) == NOP_EXPR
)
14265 realret
= TREE_OPERAND (ret
, 0);
14266 if (CAN_HAVE_LOCATION_P (realret
)
14267 && !EXPR_HAS_LOCATION (realret
))
14268 SET_EXPR_LOCATION (realret
, loc
);
14278 /* Look up the function in builtin_decl that corresponds to DECL
14279 and set ASMSPEC as its user assembler name. DECL must be a
14280 function decl that declares a builtin. */
14283 set_builtin_user_assembler_name (tree decl
, const char *asmspec
)
14286 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
14287 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
14290 builtin
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
14291 set_user_assembler_name (builtin
, asmspec
);
14292 switch (DECL_FUNCTION_CODE (decl
))
14294 case BUILT_IN_MEMCPY
:
14295 init_block_move_fn (asmspec
);
14296 memcpy_libfunc
= set_user_assembler_libfunc ("memcpy", asmspec
);
14298 case BUILT_IN_MEMSET
:
14299 init_block_clear_fn (asmspec
);
14300 memset_libfunc
= set_user_assembler_libfunc ("memset", asmspec
);
14302 case BUILT_IN_MEMMOVE
:
14303 memmove_libfunc
= set_user_assembler_libfunc ("memmove", asmspec
);
14305 case BUILT_IN_MEMCMP
:
14306 memcmp_libfunc
= set_user_assembler_libfunc ("memcmp", asmspec
);
14308 case BUILT_IN_ABORT
:
14309 abort_libfunc
= set_user_assembler_libfunc ("abort", asmspec
);
14312 if (INT_TYPE_SIZE
< BITS_PER_WORD
)
14314 set_user_assembler_libfunc ("ffs", asmspec
);
14315 set_optab_libfunc (ffs_optab
, mode_for_size (INT_TYPE_SIZE
,
14316 MODE_INT
, 0), "ffs");
14324 /* Return true if DECL is a builtin that expands to a constant or similarly
14327 is_simple_builtin (tree decl
)
14329 if (decl
&& DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
14330 switch (DECL_FUNCTION_CODE (decl
))
14332 /* Builtins that expand to constants. */
14333 case BUILT_IN_CONSTANT_P
:
14334 case BUILT_IN_EXPECT
:
14335 case BUILT_IN_OBJECT_SIZE
:
14336 case BUILT_IN_UNREACHABLE
:
14337 /* Simple register moves or loads from stack. */
14338 case BUILT_IN_ASSUME_ALIGNED
:
14339 case BUILT_IN_RETURN_ADDRESS
:
14340 case BUILT_IN_EXTRACT_RETURN_ADDR
:
14341 case BUILT_IN_FROB_RETURN_ADDR
:
14342 case BUILT_IN_RETURN
:
14343 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
14344 case BUILT_IN_FRAME_ADDRESS
:
14345 case BUILT_IN_VA_END
:
14346 case BUILT_IN_STACK_SAVE
:
14347 case BUILT_IN_STACK_RESTORE
:
14348 /* Exception state returns or moves registers around. */
14349 case BUILT_IN_EH_FILTER
:
14350 case BUILT_IN_EH_POINTER
:
14351 case BUILT_IN_EH_COPY_VALUES
:
14361 /* Return true if DECL is a builtin that is not expensive, i.e., they are
14362 most probably expanded inline into reasonably simple code. This is a
14363 superset of is_simple_builtin. */
14365 is_inexpensive_builtin (tree decl
)
14369 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_MD
)
14371 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
14372 switch (DECL_FUNCTION_CODE (decl
))
14375 case BUILT_IN_ALLOCA
:
14376 case BUILT_IN_ALLOCA_WITH_ALIGN
:
14377 case BUILT_IN_BSWAP16
:
14378 case BUILT_IN_BSWAP32
:
14379 case BUILT_IN_BSWAP64
:
14381 case BUILT_IN_CLZIMAX
:
14382 case BUILT_IN_CLZL
:
14383 case BUILT_IN_CLZLL
:
14385 case BUILT_IN_CTZIMAX
:
14386 case BUILT_IN_CTZL
:
14387 case BUILT_IN_CTZLL
:
14389 case BUILT_IN_FFSIMAX
:
14390 case BUILT_IN_FFSL
:
14391 case BUILT_IN_FFSLL
:
14392 case BUILT_IN_IMAXABS
:
14393 case BUILT_IN_FINITE
:
14394 case BUILT_IN_FINITEF
:
14395 case BUILT_IN_FINITEL
:
14396 case BUILT_IN_FINITED32
:
14397 case BUILT_IN_FINITED64
:
14398 case BUILT_IN_FINITED128
:
14399 case BUILT_IN_FPCLASSIFY
:
14400 case BUILT_IN_ISFINITE
:
14401 case BUILT_IN_ISINF_SIGN
:
14402 case BUILT_IN_ISINF
:
14403 case BUILT_IN_ISINFF
:
14404 case BUILT_IN_ISINFL
:
14405 case BUILT_IN_ISINFD32
:
14406 case BUILT_IN_ISINFD64
:
14407 case BUILT_IN_ISINFD128
:
14408 case BUILT_IN_ISNAN
:
14409 case BUILT_IN_ISNANF
:
14410 case BUILT_IN_ISNANL
:
14411 case BUILT_IN_ISNAND32
:
14412 case BUILT_IN_ISNAND64
:
14413 case BUILT_IN_ISNAND128
:
14414 case BUILT_IN_ISNORMAL
:
14415 case BUILT_IN_ISGREATER
:
14416 case BUILT_IN_ISGREATEREQUAL
:
14417 case BUILT_IN_ISLESS
:
14418 case BUILT_IN_ISLESSEQUAL
:
14419 case BUILT_IN_ISLESSGREATER
:
14420 case BUILT_IN_ISUNORDERED
:
14421 case BUILT_IN_VA_ARG_PACK
:
14422 case BUILT_IN_VA_ARG_PACK_LEN
:
14423 case BUILT_IN_VA_COPY
:
14424 case BUILT_IN_TRAP
:
14425 case BUILT_IN_SAVEREGS
:
14426 case BUILT_IN_POPCOUNTL
:
14427 case BUILT_IN_POPCOUNTLL
:
14428 case BUILT_IN_POPCOUNTIMAX
:
14429 case BUILT_IN_POPCOUNT
:
14430 case BUILT_IN_PARITYL
:
14431 case BUILT_IN_PARITYLL
:
14432 case BUILT_IN_PARITYIMAX
:
14433 case BUILT_IN_PARITY
:
14434 case BUILT_IN_LABS
:
14435 case BUILT_IN_LLABS
:
14436 case BUILT_IN_PREFETCH
:
14440 return is_simple_builtin (decl
);