1 /* Expand builtin functions.
2 Copyright (C) 1988-2014 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"
37 #include "hard-reg-set.h"
41 #include "basic-block.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "gimple-expr.h"
50 #include "insn-config.h"
52 #include "insn-codes.h"
57 #include "typeclass.h"
60 #include "langhooks.h"
61 #include "tree-ssanames.h"
63 #include "value-prof.h"
64 #include "diagnostic-core.h"
69 #include "lto-streamer.h"
71 #include "tree-chkp.h"
73 #include "gomp-constants.h"
76 static tree
do_mpc_arg1 (tree
, tree
, int (*)(mpc_ptr
, mpc_srcptr
, mpc_rnd_t
));
78 struct target_builtins default_target_builtins
;
80 struct target_builtins
*this_target_builtins
= &default_target_builtins
;
83 /* Define the names of the builtin function types and codes. */
84 const char *const built_in_class_names
[BUILT_IN_LAST
]
85 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
87 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
88 const char * built_in_names
[(int) END_BUILTINS
] =
90 #include "builtins.def"
94 /* Setup an array of _DECL trees, make sure each element is
95 initialized to NULL_TREE. */
96 builtin_info_type builtin_info
;
98 /* Non-zero if __builtin_constant_p should be folded right away. */
99 bool force_folding_builtin_constant_p
;
101 static rtx
c_readstr (const char *, machine_mode
);
102 static int target_char_cast (tree
, char *);
103 static rtx
get_memory_rtx (tree
, tree
);
104 static int apply_args_size (void);
105 static int apply_result_size (void);
106 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
107 static rtx
result_vector (int, rtx
);
109 static void expand_builtin_update_setjmp_buf (rtx
);
110 static void expand_builtin_prefetch (tree
);
111 static rtx
expand_builtin_apply_args (void);
112 static rtx
expand_builtin_apply_args_1 (void);
113 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
114 static void expand_builtin_return (rtx
);
115 static enum type_class
type_to_class (tree
);
116 static rtx
expand_builtin_classify_type (tree
);
117 static void expand_errno_check (tree
, rtx
);
118 static rtx
expand_builtin_mathfn (tree
, rtx
, rtx
);
119 static rtx
expand_builtin_mathfn_2 (tree
, rtx
, rtx
);
120 static rtx
expand_builtin_mathfn_3 (tree
, rtx
, rtx
);
121 static rtx
expand_builtin_mathfn_ternary (tree
, rtx
, rtx
);
122 static rtx
expand_builtin_interclass_mathfn (tree
, rtx
);
123 static rtx
expand_builtin_sincos (tree
);
124 static rtx
expand_builtin_cexpi (tree
, rtx
);
125 static rtx
expand_builtin_int_roundingfn (tree
, rtx
);
126 static rtx
expand_builtin_int_roundingfn_2 (tree
, rtx
);
127 static rtx
expand_builtin_next_arg (void);
128 static rtx
expand_builtin_va_start (tree
);
129 static rtx
expand_builtin_va_end (tree
);
130 static rtx
expand_builtin_va_copy (tree
);
131 static rtx
expand_builtin_memcmp (tree
, rtx
, machine_mode
);
132 static rtx
expand_builtin_strcmp (tree
, rtx
);
133 static rtx
expand_builtin_strncmp (tree
, rtx
, machine_mode
);
134 static rtx
builtin_memcpy_read_str (void *, HOST_WIDE_INT
, machine_mode
);
135 static rtx
expand_builtin_memcpy (tree
, rtx
);
136 static rtx
expand_builtin_memcpy_with_bounds (tree
, rtx
);
137 static rtx
expand_builtin_memcpy_args (tree
, tree
, tree
, rtx
, tree
);
138 static rtx
expand_builtin_mempcpy (tree
, rtx
, machine_mode
);
139 static rtx
expand_builtin_mempcpy_with_bounds (tree
, rtx
, machine_mode
);
140 static rtx
expand_builtin_mempcpy_args (tree
, tree
, tree
, rtx
,
141 machine_mode
, int, tree
);
142 static rtx
expand_builtin_strcpy (tree
, rtx
);
143 static rtx
expand_builtin_strcpy_args (tree
, tree
, rtx
);
144 static rtx
expand_builtin_stpcpy (tree
, rtx
, machine_mode
);
145 static rtx
expand_builtin_strncpy (tree
, rtx
);
146 static rtx
builtin_memset_gen_str (void *, HOST_WIDE_INT
, machine_mode
);
147 static rtx
expand_builtin_memset (tree
, rtx
, machine_mode
);
148 static rtx
expand_builtin_memset_with_bounds (tree
, rtx
, machine_mode
);
149 static rtx
expand_builtin_memset_args (tree
, tree
, tree
, rtx
, machine_mode
, tree
);
150 static rtx
expand_builtin_bzero (tree
);
151 static rtx
expand_builtin_strlen (tree
, rtx
, machine_mode
);
152 static rtx
expand_builtin_alloca (tree
, bool);
153 static rtx
expand_builtin_unop (machine_mode
, tree
, rtx
, rtx
, optab
);
154 static rtx
expand_builtin_frame_address (tree
, tree
);
155 static tree
stabilize_va_list_loc (location_t
, tree
, int);
156 static rtx
expand_builtin_expect (tree
, rtx
);
157 static tree
fold_builtin_constant_p (tree
);
158 static tree
fold_builtin_classify_type (tree
);
159 static tree
fold_builtin_strlen (location_t
, tree
, tree
);
160 static tree
fold_builtin_inf (location_t
, tree
, int);
161 static tree
fold_builtin_nan (tree
, tree
, int);
162 static tree
rewrite_call_expr (location_t
, tree
, int, tree
, int, ...);
163 static bool validate_arg (const_tree
, enum tree_code code
);
164 static bool integer_valued_real_p (tree
);
165 static tree
fold_trunc_transparent_mathfn (location_t
, tree
, tree
);
166 static rtx
expand_builtin_fabs (tree
, rtx
, rtx
);
167 static rtx
expand_builtin_signbit (tree
, rtx
);
168 static tree
fold_builtin_sqrt (location_t
, tree
, tree
);
169 static tree
fold_builtin_cbrt (location_t
, tree
, tree
);
170 static tree
fold_builtin_pow (location_t
, tree
, tree
, tree
, tree
);
171 static tree
fold_builtin_powi (location_t
, tree
, tree
, tree
, tree
);
172 static tree
fold_builtin_cos (location_t
, tree
, tree
, tree
);
173 static tree
fold_builtin_cosh (location_t
, tree
, tree
, tree
);
174 static tree
fold_builtin_tan (tree
, tree
);
175 static tree
fold_builtin_trunc (location_t
, tree
, tree
);
176 static tree
fold_builtin_floor (location_t
, tree
, tree
);
177 static tree
fold_builtin_ceil (location_t
, tree
, tree
);
178 static tree
fold_builtin_round (location_t
, tree
, tree
);
179 static tree
fold_builtin_int_roundingfn (location_t
, tree
, tree
);
180 static tree
fold_builtin_bitop (tree
, tree
);
181 static tree
fold_builtin_strchr (location_t
, tree
, tree
, tree
);
182 static tree
fold_builtin_memchr (location_t
, tree
, tree
, tree
, tree
);
183 static tree
fold_builtin_memcmp (location_t
, tree
, tree
, tree
);
184 static tree
fold_builtin_strcmp (location_t
, tree
, tree
);
185 static tree
fold_builtin_strncmp (location_t
, tree
, tree
, tree
);
186 static tree
fold_builtin_signbit (location_t
, tree
, tree
);
187 static tree
fold_builtin_copysign (location_t
, tree
, tree
, tree
, tree
);
188 static tree
fold_builtin_isascii (location_t
, tree
);
189 static tree
fold_builtin_toascii (location_t
, tree
);
190 static tree
fold_builtin_isdigit (location_t
, tree
);
191 static tree
fold_builtin_fabs (location_t
, tree
, tree
);
192 static tree
fold_builtin_abs (location_t
, tree
, tree
);
193 static tree
fold_builtin_unordered_cmp (location_t
, tree
, tree
, tree
, enum tree_code
,
195 static tree
fold_builtin_0 (location_t
, tree
);
196 static tree
fold_builtin_1 (location_t
, tree
, tree
);
197 static tree
fold_builtin_2 (location_t
, tree
, tree
, tree
);
198 static tree
fold_builtin_3 (location_t
, tree
, tree
, tree
, tree
);
199 static tree
fold_builtin_varargs (location_t
, tree
, tree
*, int);
201 static tree
fold_builtin_strpbrk (location_t
, tree
, tree
, tree
);
202 static tree
fold_builtin_strstr (location_t
, tree
, tree
, tree
);
203 static tree
fold_builtin_strrchr (location_t
, tree
, tree
, tree
);
204 static tree
fold_builtin_strspn (location_t
, tree
, tree
);
205 static tree
fold_builtin_strcspn (location_t
, tree
, tree
);
207 static rtx
expand_builtin_object_size (tree
);
208 static rtx
expand_builtin_memory_chk (tree
, rtx
, machine_mode
,
209 enum built_in_function
);
210 static void maybe_emit_chk_warning (tree
, enum built_in_function
);
211 static void maybe_emit_sprintf_chk_warning (tree
, enum built_in_function
);
212 static void maybe_emit_free_warning (tree
);
213 static tree
fold_builtin_object_size (tree
, tree
);
215 unsigned HOST_WIDE_INT target_newline
;
216 unsigned HOST_WIDE_INT target_percent
;
217 static unsigned HOST_WIDE_INT target_c
;
218 static unsigned HOST_WIDE_INT target_s
;
219 char target_percent_c
[3];
220 char target_percent_s
[3];
221 char target_percent_s_newline
[4];
222 static tree
do_mpfr_arg1 (tree
, tree
, int (*)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
223 const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, bool);
224 static tree
do_mpfr_arg2 (tree
, tree
, tree
,
225 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
226 static tree
do_mpfr_arg3 (tree
, tree
, tree
, tree
,
227 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
228 static tree
do_mpfr_sincos (tree
, tree
, tree
);
229 static tree
do_mpfr_bessel_n (tree
, tree
, tree
,
230 int (*)(mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
231 const REAL_VALUE_TYPE
*, bool);
232 static tree
do_mpfr_remquo (tree
, tree
, tree
);
233 static tree
do_mpfr_lgamma_r (tree
, tree
, tree
);
234 static void expand_builtin_sync_synchronize (void);
236 /* Return true if NAME starts with __builtin_ or __sync_. */
239 is_builtin_name (const char *name
)
241 if (strncmp (name
, "__builtin_", 10) == 0)
243 if (strncmp (name
, "__sync_", 7) == 0)
245 if (strncmp (name
, "__atomic_", 9) == 0)
248 && (!strcmp (name
, "__cilkrts_detach")
249 || !strcmp (name
, "__cilkrts_pop_frame")))
255 /* Return true if DECL is a function symbol representing a built-in. */
258 is_builtin_fn (tree decl
)
260 return TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
);
263 /* Return true if NODE should be considered for inline expansion regardless
264 of the optimization level. This means whenever a function is invoked with
265 its "internal" name, which normally contains the prefix "__builtin". */
268 called_as_built_in (tree node
)
270 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
271 we want the name used to call the function, not the name it
273 const char *name
= IDENTIFIER_POINTER (DECL_NAME (node
));
274 return is_builtin_name (name
);
277 /* Compute values M and N such that M divides (address of EXP - N) and such
278 that N < M. If these numbers can be determined, store M in alignp and N in
279 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
280 *alignp and any bit-offset to *bitposp.
282 Note that the address (and thus the alignment) computed here is based
283 on the address to which a symbol resolves, whereas DECL_ALIGN is based
284 on the address at which an object is actually located. These two
285 addresses are not always the same. For example, on ARM targets,
286 the address &foo of a Thumb function foo() has the lowest bit set,
287 whereas foo() itself starts on an even address.
289 If ADDR_P is true we are taking the address of the memory reference EXP
290 and thus cannot rely on the access taking place. */
293 get_object_alignment_2 (tree exp
, unsigned int *alignp
,
294 unsigned HOST_WIDE_INT
*bitposp
, bool addr_p
)
296 HOST_WIDE_INT bitsize
, bitpos
;
299 int unsignedp
, volatilep
;
300 unsigned int align
= BITS_PER_UNIT
;
301 bool known_alignment
= false;
303 /* Get the innermost object and the constant (bitpos) and possibly
304 variable (offset) offset of the access. */
305 exp
= get_inner_reference (exp
, &bitsize
, &bitpos
, &offset
,
306 &mode
, &unsignedp
, &volatilep
, true);
308 /* Extract alignment information from the innermost object and
309 possibly adjust bitpos and offset. */
310 if (TREE_CODE (exp
) == FUNCTION_DECL
)
312 /* Function addresses can encode extra information besides their
313 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
314 allows the low bit to be used as a virtual bit, we know
315 that the address itself must be at least 2-byte aligned. */
316 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_pfn
)
317 align
= 2 * BITS_PER_UNIT
;
319 else if (TREE_CODE (exp
) == LABEL_DECL
)
321 else if (TREE_CODE (exp
) == CONST_DECL
)
323 /* The alignment of a CONST_DECL is determined by its initializer. */
324 exp
= DECL_INITIAL (exp
);
325 align
= TYPE_ALIGN (TREE_TYPE (exp
));
326 #ifdef CONSTANT_ALIGNMENT
327 if (CONSTANT_CLASS_P (exp
))
328 align
= (unsigned) CONSTANT_ALIGNMENT (exp
, align
);
330 known_alignment
= true;
332 else if (DECL_P (exp
))
334 align
= DECL_ALIGN (exp
);
335 known_alignment
= true;
337 else if (TREE_CODE (exp
) == VIEW_CONVERT_EXPR
)
339 align
= TYPE_ALIGN (TREE_TYPE (exp
));
341 else if (TREE_CODE (exp
) == INDIRECT_REF
342 || TREE_CODE (exp
) == MEM_REF
343 || TREE_CODE (exp
) == TARGET_MEM_REF
)
345 tree addr
= TREE_OPERAND (exp
, 0);
347 unsigned HOST_WIDE_INT ptr_bitpos
;
349 if (TREE_CODE (addr
) == BIT_AND_EXPR
350 && TREE_CODE (TREE_OPERAND (addr
, 1)) == INTEGER_CST
)
352 align
= (TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1))
353 & -TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1)));
354 align
*= BITS_PER_UNIT
;
355 addr
= TREE_OPERAND (addr
, 0);
359 = get_pointer_alignment_1 (addr
, &ptr_align
, &ptr_bitpos
);
360 align
= MAX (ptr_align
, align
);
362 /* The alignment of the pointer operand in a TARGET_MEM_REF
363 has to take the variable offset parts into account. */
364 if (TREE_CODE (exp
) == TARGET_MEM_REF
)
368 unsigned HOST_WIDE_INT step
= 1;
370 step
= TREE_INT_CST_LOW (TMR_STEP (exp
));
371 align
= MIN (align
, (step
& -step
) * BITS_PER_UNIT
);
373 if (TMR_INDEX2 (exp
))
374 align
= BITS_PER_UNIT
;
375 known_alignment
= false;
378 /* When EXP is an actual memory reference then we can use
379 TYPE_ALIGN of a pointer indirection to derive alignment.
380 Do so only if get_pointer_alignment_1 did not reveal absolute
381 alignment knowledge and if using that alignment would
382 improve the situation. */
383 if (!addr_p
&& !known_alignment
384 && TYPE_ALIGN (TREE_TYPE (exp
)) > align
)
385 align
= TYPE_ALIGN (TREE_TYPE (exp
));
388 /* Else adjust bitpos accordingly. */
389 bitpos
+= ptr_bitpos
;
390 if (TREE_CODE (exp
) == MEM_REF
391 || TREE_CODE (exp
) == TARGET_MEM_REF
)
392 bitpos
+= mem_ref_offset (exp
).to_short_addr () * BITS_PER_UNIT
;
395 else if (TREE_CODE (exp
) == STRING_CST
)
397 /* STRING_CST are the only constant objects we allow to be not
398 wrapped inside a CONST_DECL. */
399 align
= TYPE_ALIGN (TREE_TYPE (exp
));
400 #ifdef CONSTANT_ALIGNMENT
401 if (CONSTANT_CLASS_P (exp
))
402 align
= (unsigned) CONSTANT_ALIGNMENT (exp
, align
);
404 known_alignment
= true;
407 /* If there is a non-constant offset part extract the maximum
408 alignment that can prevail. */
411 unsigned int trailing_zeros
= tree_ctz (offset
);
412 if (trailing_zeros
< HOST_BITS_PER_INT
)
414 unsigned int inner
= (1U << trailing_zeros
) * BITS_PER_UNIT
;
416 align
= MIN (align
, inner
);
421 *bitposp
= bitpos
& (*alignp
- 1);
422 return known_alignment
;
425 /* For a memory reference expression EXP compute values M and N such that M
426 divides (&EXP - N) and such that N < M. If these numbers can be determined,
427 store M in alignp and N in *BITPOSP and return true. Otherwise return false
428 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
431 get_object_alignment_1 (tree exp
, unsigned int *alignp
,
432 unsigned HOST_WIDE_INT
*bitposp
)
434 return get_object_alignment_2 (exp
, alignp
, bitposp
, false);
437 /* Return the alignment in bits of EXP, an object. */
440 get_object_alignment (tree exp
)
442 unsigned HOST_WIDE_INT bitpos
= 0;
445 get_object_alignment_1 (exp
, &align
, &bitpos
);
447 /* align and bitpos now specify known low bits of the pointer.
448 ptr & (align - 1) == bitpos. */
451 align
= (bitpos
& -bitpos
);
455 /* For a pointer valued expression EXP compute values M and N such that M
456 divides (EXP - N) and such that N < M. If these numbers can be determined,
457 store M in alignp and N in *BITPOSP and return true. Return false if
458 the results are just a conservative approximation.
460 If EXP is not a pointer, false is returned too. */
463 get_pointer_alignment_1 (tree exp
, unsigned int *alignp
,
464 unsigned HOST_WIDE_INT
*bitposp
)
468 if (TREE_CODE (exp
) == ADDR_EXPR
)
469 return get_object_alignment_2 (TREE_OPERAND (exp
, 0),
470 alignp
, bitposp
, true);
471 else if (TREE_CODE (exp
) == SSA_NAME
472 && POINTER_TYPE_P (TREE_TYPE (exp
)))
474 unsigned int ptr_align
, ptr_misalign
;
475 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (exp
);
477 if (pi
&& get_ptr_info_alignment (pi
, &ptr_align
, &ptr_misalign
))
479 *bitposp
= ptr_misalign
* BITS_PER_UNIT
;
480 *alignp
= ptr_align
* BITS_PER_UNIT
;
481 /* We cannot really tell whether this result is an approximation. */
487 *alignp
= BITS_PER_UNIT
;
491 else if (TREE_CODE (exp
) == INTEGER_CST
)
493 *alignp
= BIGGEST_ALIGNMENT
;
494 *bitposp
= ((TREE_INT_CST_LOW (exp
) * BITS_PER_UNIT
)
495 & (BIGGEST_ALIGNMENT
- 1));
500 *alignp
= BITS_PER_UNIT
;
504 /* Return the alignment in bits of EXP, a pointer valued expression.
505 The alignment returned is, by default, the alignment of the thing that
506 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
508 Otherwise, look at the expression to see if we can do better, i.e., if the
509 expression is actually pointing at an object whose alignment is tighter. */
512 get_pointer_alignment (tree exp
)
514 unsigned HOST_WIDE_INT bitpos
= 0;
517 get_pointer_alignment_1 (exp
, &align
, &bitpos
);
519 /* align and bitpos now specify known low bits of the pointer.
520 ptr & (align - 1) == bitpos. */
523 align
= (bitpos
& -bitpos
);
528 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
529 way, because it could contain a zero byte in the middle.
530 TREE_STRING_LENGTH is the size of the character array, not the string.
532 ONLY_VALUE should be nonzero if the result is not going to be emitted
533 into the instruction stream and zero if it is going to be expanded.
534 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
535 is returned, otherwise NULL, since
536 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
537 evaluate the side-effects.
539 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
540 accesses. Note that this implies the result is not going to be emitted
541 into the instruction stream.
543 The value returned is of type `ssizetype'.
545 Unfortunately, string_constant can't access the values of const char
546 arrays with initializers, so neither can we do so here. */
549 c_strlen (tree src
, int only_value
)
552 HOST_WIDE_INT offset
;
558 if (TREE_CODE (src
) == COND_EXPR
559 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
563 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
);
564 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
);
565 if (tree_int_cst_equal (len1
, len2
))
569 if (TREE_CODE (src
) == COMPOUND_EXPR
570 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
571 return c_strlen (TREE_OPERAND (src
, 1), only_value
);
573 loc
= EXPR_LOC_OR_LOC (src
, input_location
);
575 src
= string_constant (src
, &offset_node
);
579 max
= TREE_STRING_LENGTH (src
) - 1;
580 ptr
= TREE_STRING_POINTER (src
);
582 if (offset_node
&& TREE_CODE (offset_node
) != INTEGER_CST
)
584 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
585 compute the offset to the following null if we don't know where to
586 start searching for it. */
589 for (i
= 0; i
< max
; i
++)
593 /* We don't know the starting offset, but we do know that the string
594 has no internal zero bytes. We can assume that the offset falls
595 within the bounds of the string; otherwise, the programmer deserves
596 what he gets. Subtract the offset from the length of the string,
597 and return that. This would perhaps not be valid if we were dealing
598 with named arrays in addition to literal string constants. */
600 return size_diffop_loc (loc
, size_int (max
), offset_node
);
603 /* We have a known offset into the string. Start searching there for
604 a null character if we can represent it as a single HOST_WIDE_INT. */
605 if (offset_node
== 0)
607 else if (! tree_fits_shwi_p (offset_node
))
610 offset
= tree_to_shwi (offset_node
);
612 /* If the offset is known to be out of bounds, warn, and call strlen at
614 if (offset
< 0 || offset
> max
)
616 /* Suppress multiple warnings for propagated constant strings. */
618 && !TREE_NO_WARNING (src
))
620 warning_at (loc
, 0, "offset outside bounds of constant string");
621 TREE_NO_WARNING (src
) = 1;
626 /* Use strlen to search for the first zero byte. Since any strings
627 constructed with build_string will have nulls appended, we win even
628 if we get handed something like (char[4])"abcd".
630 Since OFFSET is our starting index into the string, no further
631 calculation is needed. */
632 return ssize_int (strlen (ptr
+ offset
));
635 /* Return a char pointer for a C string if it is a string constant
636 or sum of string constant and integer constant. */
643 src
= string_constant (src
, &offset_node
);
647 if (offset_node
== 0)
648 return TREE_STRING_POINTER (src
);
649 else if (!tree_fits_uhwi_p (offset_node
)
650 || compare_tree_int (offset_node
, TREE_STRING_LENGTH (src
) - 1) > 0)
653 return TREE_STRING_POINTER (src
) + tree_to_uhwi (offset_node
);
656 /* Return a constant integer corresponding to target reading
657 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
660 c_readstr (const char *str
, machine_mode mode
)
664 HOST_WIDE_INT tmp
[MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
];
666 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
667 unsigned int len
= (GET_MODE_PRECISION (mode
) + HOST_BITS_PER_WIDE_INT
- 1)
668 / HOST_BITS_PER_WIDE_INT
;
670 gcc_assert (len
<= MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
);
671 for (i
= 0; i
< len
; i
++)
675 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
678 if (WORDS_BIG_ENDIAN
)
679 j
= GET_MODE_SIZE (mode
) - i
- 1;
680 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
681 && GET_MODE_SIZE (mode
) >= UNITS_PER_WORD
)
682 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
686 ch
= (unsigned char) str
[i
];
687 tmp
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
690 wide_int c
= wide_int::from_array (tmp
, len
, GET_MODE_PRECISION (mode
));
691 return immed_wide_int_const (c
, mode
);
694 /* Cast a target constant CST to target CHAR and if that value fits into
695 host char type, return zero and put that value into variable pointed to by
699 target_char_cast (tree cst
, char *p
)
701 unsigned HOST_WIDE_INT val
, hostval
;
703 if (TREE_CODE (cst
) != INTEGER_CST
704 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
707 /* Do not care if it fits or not right here. */
708 val
= TREE_INT_CST_LOW (cst
);
710 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
711 val
&= (((unsigned HOST_WIDE_INT
) 1) << CHAR_TYPE_SIZE
) - 1;
714 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
715 hostval
&= (((unsigned HOST_WIDE_INT
) 1) << HOST_BITS_PER_CHAR
) - 1;
724 /* Similar to save_expr, but assumes that arbitrary code is not executed
725 in between the multiple evaluations. In particular, we assume that a
726 non-addressable local variable will not be modified. */
729 builtin_save_expr (tree exp
)
731 if (TREE_CODE (exp
) == SSA_NAME
732 || (TREE_ADDRESSABLE (exp
) == 0
733 && (TREE_CODE (exp
) == PARM_DECL
734 || (TREE_CODE (exp
) == VAR_DECL
&& !TREE_STATIC (exp
)))))
737 return save_expr (exp
);
740 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
741 times to get the address of either a higher stack frame, or a return
742 address located within it (depending on FNDECL_CODE). */
745 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
)
749 #ifdef INITIAL_FRAME_ADDRESS_RTX
750 rtx tem
= INITIAL_FRAME_ADDRESS_RTX
;
754 /* For a zero count with __builtin_return_address, we don't care what
755 frame address we return, because target-specific definitions will
756 override us. Therefore frame pointer elimination is OK, and using
757 the soft frame pointer is OK.
759 For a nonzero count, or a zero count with __builtin_frame_address,
760 we require a stable offset from the current frame pointer to the
761 previous one, so we must use the hard frame pointer, and
762 we must disable frame pointer elimination. */
763 if (count
== 0 && fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
764 tem
= frame_pointer_rtx
;
767 tem
= hard_frame_pointer_rtx
;
769 /* Tell reload not to eliminate the frame pointer. */
770 crtl
->accesses_prior_frames
= 1;
774 /* Some machines need special handling before we can access
775 arbitrary frames. For example, on the SPARC, we must first flush
776 all register windows to the stack. */
777 #ifdef SETUP_FRAME_ADDRESSES
779 SETUP_FRAME_ADDRESSES ();
782 /* On the SPARC, the return address is not in the frame, it is in a
783 register. There is no way to access it off of the current frame
784 pointer, but it can be accessed off the previous frame pointer by
785 reading the value from the register window save area. */
786 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
787 if (fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
791 /* Scan back COUNT frames to the specified frame. */
792 for (i
= 0; i
< count
; i
++)
794 /* Assume the dynamic chain pointer is in the word that the
795 frame address points to, unless otherwise specified. */
796 #ifdef DYNAMIC_CHAIN_ADDRESS
797 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
799 tem
= memory_address (Pmode
, tem
);
800 tem
= gen_frame_mem (Pmode
, tem
);
801 tem
= copy_to_reg (tem
);
804 /* For __builtin_frame_address, return what we've got. But, on
805 the SPARC for example, we may have to add a bias. */
806 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
807 #ifdef FRAME_ADDR_RTX
808 return FRAME_ADDR_RTX (tem
);
813 /* For __builtin_return_address, get the return address from that frame. */
814 #ifdef RETURN_ADDR_RTX
815 tem
= RETURN_ADDR_RTX (count
, tem
);
817 tem
= memory_address (Pmode
,
818 plus_constant (Pmode
, tem
, GET_MODE_SIZE (Pmode
)));
819 tem
= gen_frame_mem (Pmode
, tem
);
824 /* Alias set used for setjmp buffer. */
825 static alias_set_type setjmp_alias_set
= -1;
827 /* Construct the leading half of a __builtin_setjmp call. Control will
828 return to RECEIVER_LABEL. This is also called directly by the SJLJ
829 exception handling code. */
832 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
834 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
838 if (setjmp_alias_set
== -1)
839 setjmp_alias_set
= new_alias_set ();
841 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
843 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
845 /* We store the frame pointer and the address of receiver_label in
846 the buffer and use the rest of it for the stack save area, which
847 is machine-dependent. */
849 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
850 set_mem_alias_set (mem
, setjmp_alias_set
);
851 emit_move_insn (mem
, targetm
.builtin_setjmp_frame_value ());
853 mem
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
854 GET_MODE_SIZE (Pmode
))),
855 set_mem_alias_set (mem
, setjmp_alias_set
);
857 emit_move_insn (validize_mem (mem
),
858 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
860 stack_save
= gen_rtx_MEM (sa_mode
,
861 plus_constant (Pmode
, buf_addr
,
862 2 * GET_MODE_SIZE (Pmode
)));
863 set_mem_alias_set (stack_save
, setjmp_alias_set
);
864 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
866 /* If there is further processing to do, do it. */
867 #ifdef HAVE_builtin_setjmp_setup
868 if (HAVE_builtin_setjmp_setup
)
869 emit_insn (gen_builtin_setjmp_setup (buf_addr
));
872 /* We have a nonlocal label. */
873 cfun
->has_nonlocal_label
= 1;
876 /* Construct the trailing part of a __builtin_setjmp call. This is
877 also called directly by the SJLJ exception handling code.
878 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
881 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED
)
885 /* Mark the FP as used when we get here, so we have to make sure it's
886 marked as used by this function. */
887 emit_use (hard_frame_pointer_rtx
);
889 /* Mark the static chain as clobbered here so life information
890 doesn't get messed up for it. */
891 chain
= targetm
.calls
.static_chain (current_function_decl
, true);
892 if (chain
&& REG_P (chain
))
893 emit_clobber (chain
);
895 /* Now put in the code to restore the frame pointer, and argument
896 pointer, if needed. */
897 #ifdef HAVE_nonlocal_goto
898 if (! HAVE_nonlocal_goto
)
901 /* First adjust our frame pointer to its actual value. It was
902 previously set to the start of the virtual area corresponding to
903 the stacked variables when we branched here and now needs to be
904 adjusted to the actual hardware fp value.
906 Assignments to virtual registers are converted by
907 instantiate_virtual_regs into the corresponding assignment
908 to the underlying register (fp in this case) that makes
909 the original assignment true.
910 So the following insn will actually be decrementing fp by
911 STARTING_FRAME_OFFSET. */
912 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
914 /* Restoring the frame pointer also modifies the hard frame pointer.
915 Mark it used (so that the previous assignment remains live once
916 the frame pointer is eliminated) and clobbered (to represent the
917 implicit update from the assignment). */
918 emit_use (hard_frame_pointer_rtx
);
919 emit_clobber (hard_frame_pointer_rtx
);
922 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
923 if (fixed_regs
[ARG_POINTER_REGNUM
])
925 #ifdef ELIMINABLE_REGS
926 /* If the argument pointer can be eliminated in favor of the
927 frame pointer, we don't need to restore it. We assume here
928 that if such an elimination is present, it can always be used.
929 This is the case on all known machines; if we don't make this
930 assumption, we do unnecessary saving on many machines. */
932 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
934 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
935 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
936 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
939 if (i
== ARRAY_SIZE (elim_regs
))
942 /* Now restore our arg pointer from the address at which it
943 was saved in our stack frame. */
944 emit_move_insn (crtl
->args
.internal_arg_pointer
,
945 copy_to_reg (get_arg_pointer_save_area ()));
950 #ifdef HAVE_builtin_setjmp_receiver
951 if (receiver_label
!= NULL
&& HAVE_builtin_setjmp_receiver
)
952 emit_insn (gen_builtin_setjmp_receiver (receiver_label
));
955 #ifdef HAVE_nonlocal_goto_receiver
956 if (HAVE_nonlocal_goto_receiver
)
957 emit_insn (gen_nonlocal_goto_receiver ());
962 /* We must not allow the code we just generated to be reordered by
963 scheduling. Specifically, the update of the frame pointer must
964 happen immediately, not later. */
965 emit_insn (gen_blockage ());
968 /* __builtin_longjmp is passed a pointer to an array of five words (not
969 all will be used on all machines). It operates similarly to the C
970 library function of the same name, but is more efficient. Much of
971 the code below is copied from the handling of non-local gotos. */
974 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
977 rtx_insn
*insn
, *last
;
978 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
980 /* DRAP is needed for stack realign if longjmp is expanded to current
982 if (SUPPORTS_STACK_ALIGNMENT
)
983 crtl
->need_drap
= true;
985 if (setjmp_alias_set
== -1)
986 setjmp_alias_set
= new_alias_set ();
988 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
990 buf_addr
= force_reg (Pmode
, buf_addr
);
992 /* We require that the user must pass a second argument of 1, because
993 that is what builtin_setjmp will return. */
994 gcc_assert (value
== const1_rtx
);
996 last
= get_last_insn ();
997 #ifdef HAVE_builtin_longjmp
998 if (HAVE_builtin_longjmp
)
999 emit_insn (gen_builtin_longjmp (buf_addr
));
1003 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
1004 lab
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
1005 GET_MODE_SIZE (Pmode
)));
1007 stack
= gen_rtx_MEM (sa_mode
, plus_constant (Pmode
, buf_addr
,
1008 2 * GET_MODE_SIZE (Pmode
)));
1009 set_mem_alias_set (fp
, setjmp_alias_set
);
1010 set_mem_alias_set (lab
, setjmp_alias_set
);
1011 set_mem_alias_set (stack
, setjmp_alias_set
);
1013 /* Pick up FP, label, and SP from the block and jump. This code is
1014 from expand_goto in stmt.c; see there for detailed comments. */
1015 #ifdef HAVE_nonlocal_goto
1016 if (HAVE_nonlocal_goto
)
1017 /* We have to pass a value to the nonlocal_goto pattern that will
1018 get copied into the static_chain pointer, but it does not matter
1019 what that value is, because builtin_setjmp does not use it. */
1020 emit_insn (gen_nonlocal_goto (value
, lab
, stack
, fp
));
1024 lab
= copy_to_reg (lab
);
1026 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1027 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1029 emit_move_insn (hard_frame_pointer_rtx
, fp
);
1030 emit_stack_restore (SAVE_NONLOCAL
, stack
);
1032 emit_use (hard_frame_pointer_rtx
);
1033 emit_use (stack_pointer_rtx
);
1034 emit_indirect_jump (lab
);
1038 /* Search backwards and mark the jump insn as a non-local goto.
1039 Note that this precludes the use of __builtin_longjmp to a
1040 __builtin_setjmp target in the same function. However, we've
1041 already cautioned the user that these functions are for
1042 internal exception handling use only. */
1043 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1045 gcc_assert (insn
!= last
);
1049 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1052 else if (CALL_P (insn
))
1058 more_const_call_expr_args_p (const const_call_expr_arg_iterator
*iter
)
1060 return (iter
->i
< iter
->n
);
1063 /* This function validates the types of a function call argument list
1064 against a specified list of tree_codes. If the last specifier is a 0,
1065 that represents an ellipses, otherwise the last specifier must be a
1069 validate_arglist (const_tree callexpr
, ...)
1071 enum tree_code code
;
1074 const_call_expr_arg_iterator iter
;
1077 va_start (ap
, callexpr
);
1078 init_const_call_expr_arg_iterator (callexpr
, &iter
);
1082 code
= (enum tree_code
) va_arg (ap
, int);
1086 /* This signifies an ellipses, any further arguments are all ok. */
1090 /* This signifies an endlink, if no arguments remain, return
1091 true, otherwise return false. */
1092 res
= !more_const_call_expr_args_p (&iter
);
1095 /* If no parameters remain or the parameter's code does not
1096 match the specified code, return false. Otherwise continue
1097 checking any remaining arguments. */
1098 arg
= next_const_call_expr_arg (&iter
);
1099 if (!validate_arg (arg
, code
))
1106 /* We need gotos here since we can only have one VA_CLOSE in a
1114 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1115 and the address of the save area. */
1118 expand_builtin_nonlocal_goto (tree exp
)
1120 tree t_label
, t_save_area
;
1121 rtx r_label
, r_save_area
, r_fp
, r_sp
;
1124 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
1127 t_label
= CALL_EXPR_ARG (exp
, 0);
1128 t_save_area
= CALL_EXPR_ARG (exp
, 1);
1130 r_label
= expand_normal (t_label
);
1131 r_label
= convert_memory_address (Pmode
, r_label
);
1132 r_save_area
= expand_normal (t_save_area
);
1133 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
1134 /* Copy the address of the save location to a register just in case it was
1135 based on the frame pointer. */
1136 r_save_area
= copy_to_reg (r_save_area
);
1137 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
1138 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
1139 plus_constant (Pmode
, r_save_area
,
1140 GET_MODE_SIZE (Pmode
)));
1142 crtl
->has_nonlocal_goto
= 1;
1144 #ifdef HAVE_nonlocal_goto
1145 /* ??? We no longer need to pass the static chain value, afaik. */
1146 if (HAVE_nonlocal_goto
)
1147 emit_insn (gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
1151 r_label
= copy_to_reg (r_label
);
1153 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1154 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1156 /* Restore frame pointer for containing function. */
1157 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
1158 emit_stack_restore (SAVE_NONLOCAL
, r_sp
);
1160 /* USE of hard_frame_pointer_rtx added for consistency;
1161 not clear if really needed. */
1162 emit_use (hard_frame_pointer_rtx
);
1163 emit_use (stack_pointer_rtx
);
1165 /* If the architecture is using a GP register, we must
1166 conservatively assume that the target function makes use of it.
1167 The prologue of functions with nonlocal gotos must therefore
1168 initialize the GP register to the appropriate value, and we
1169 must then make sure that this value is live at the point
1170 of the jump. (Note that this doesn't necessarily apply
1171 to targets with a nonlocal_goto pattern; they are free
1172 to implement it in their own way. Note also that this is
1173 a no-op if the GP register is a global invariant.) */
1174 if ((unsigned) PIC_OFFSET_TABLE_REGNUM
!= INVALID_REGNUM
1175 && fixed_regs
[PIC_OFFSET_TABLE_REGNUM
])
1176 emit_use (pic_offset_table_rtx
);
1178 emit_indirect_jump (r_label
);
1181 /* Search backwards to the jump insn and mark it as a
1183 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1187 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1190 else if (CALL_P (insn
))
1197 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1198 (not all will be used on all machines) that was passed to __builtin_setjmp.
1199 It updates the stack pointer in that block to correspond to the current
1203 expand_builtin_update_setjmp_buf (rtx buf_addr
)
1205 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
1207 = gen_rtx_MEM (sa_mode
,
1210 plus_constant (Pmode
, buf_addr
,
1211 2 * GET_MODE_SIZE (Pmode
))));
1213 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
1216 /* Expand a call to __builtin_prefetch. For a target that does not support
1217 data prefetch, evaluate the memory address argument in case it has side
1221 expand_builtin_prefetch (tree exp
)
1223 tree arg0
, arg1
, arg2
;
1227 if (!validate_arglist (exp
, POINTER_TYPE
, 0))
1230 arg0
= CALL_EXPR_ARG (exp
, 0);
1232 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1233 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1235 nargs
= call_expr_nargs (exp
);
1237 arg1
= CALL_EXPR_ARG (exp
, 1);
1239 arg1
= integer_zero_node
;
1241 arg2
= CALL_EXPR_ARG (exp
, 2);
1243 arg2
= integer_three_node
;
1245 /* Argument 0 is an address. */
1246 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
1248 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1249 if (TREE_CODE (arg1
) != INTEGER_CST
)
1251 error ("second argument to %<__builtin_prefetch%> must be a constant");
1252 arg1
= integer_zero_node
;
1254 op1
= expand_normal (arg1
);
1255 /* Argument 1 must be either zero or one. */
1256 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
1258 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1263 /* Argument 2 (locality) must be a compile-time constant int. */
1264 if (TREE_CODE (arg2
) != INTEGER_CST
)
1266 error ("third argument to %<__builtin_prefetch%> must be a constant");
1267 arg2
= integer_zero_node
;
1269 op2
= expand_normal (arg2
);
1270 /* Argument 2 must be 0, 1, 2, or 3. */
1271 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
1273 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1277 #ifdef HAVE_prefetch
1280 struct expand_operand ops
[3];
1282 create_address_operand (&ops
[0], op0
);
1283 create_integer_operand (&ops
[1], INTVAL (op1
));
1284 create_integer_operand (&ops
[2], INTVAL (op2
));
1285 if (maybe_expand_insn (CODE_FOR_prefetch
, 3, ops
))
1290 /* Don't do anything with direct references to volatile memory, but
1291 generate code to handle other side effects. */
1292 if (!MEM_P (op0
) && side_effects_p (op0
))
1296 /* Get a MEM rtx for expression EXP which is the address of an operand
1297 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1298 the maximum length of the block of memory that might be accessed or
1302 get_memory_rtx (tree exp
, tree len
)
1304 tree orig_exp
= exp
;
1307 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1308 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1309 if (TREE_CODE (exp
) == SAVE_EXPR
&& !SAVE_EXPR_RESOLVED_P (exp
))
1310 exp
= TREE_OPERAND (exp
, 0);
1312 addr
= expand_expr (orig_exp
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
1313 mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
1315 /* Get an expression we can use to find the attributes to assign to MEM.
1316 First remove any nops. */
1317 while (CONVERT_EXPR_P (exp
)
1318 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
1319 exp
= TREE_OPERAND (exp
, 0);
1321 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1322 (as builtin stringops may alias with anything). */
1323 exp
= fold_build2 (MEM_REF
,
1324 build_array_type (char_type_node
,
1325 build_range_type (sizetype
,
1326 size_one_node
, len
)),
1327 exp
, build_int_cst (ptr_type_node
, 0));
1329 /* If the MEM_REF has no acceptable address, try to get the base object
1330 from the original address we got, and build an all-aliasing
1331 unknown-sized access to that one. */
1332 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp
, 0)))
1333 set_mem_attributes (mem
, exp
, 0);
1334 else if (TREE_CODE (TREE_OPERAND (exp
, 0)) == ADDR_EXPR
1335 && (exp
= get_base_address (TREE_OPERAND (TREE_OPERAND (exp
, 0),
1338 exp
= build_fold_addr_expr (exp
);
1339 exp
= fold_build2 (MEM_REF
,
1340 build_array_type (char_type_node
,
1341 build_range_type (sizetype
,
1344 exp
, build_int_cst (ptr_type_node
, 0));
1345 set_mem_attributes (mem
, exp
, 0);
1347 set_mem_alias_set (mem
, 0);
1351 /* Built-in functions to perform an untyped call and return. */
1353 #define apply_args_mode \
1354 (this_target_builtins->x_apply_args_mode)
1355 #define apply_result_mode \
1356 (this_target_builtins->x_apply_result_mode)
1358 /* Return the size required for the block returned by __builtin_apply_args,
1359 and initialize apply_args_mode. */
1362 apply_args_size (void)
1364 static int size
= -1;
1369 /* The values computed by this function never change. */
1372 /* The first value is the incoming arg-pointer. */
1373 size
= GET_MODE_SIZE (Pmode
);
1375 /* The second value is the structure value address unless this is
1376 passed as an "invisible" first argument. */
1377 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1378 size
+= GET_MODE_SIZE (Pmode
);
1380 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1381 if (FUNCTION_ARG_REGNO_P (regno
))
1383 mode
= targetm
.calls
.get_raw_arg_mode (regno
);
1385 gcc_assert (mode
!= VOIDmode
);
1387 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1388 if (size
% align
!= 0)
1389 size
= CEIL (size
, align
) * align
;
1390 size
+= GET_MODE_SIZE (mode
);
1391 apply_args_mode
[regno
] = mode
;
1395 apply_args_mode
[regno
] = VOIDmode
;
1401 /* Return the size required for the block returned by __builtin_apply,
1402 and initialize apply_result_mode. */
1405 apply_result_size (void)
1407 static int size
= -1;
1411 /* The values computed by this function never change. */
1416 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1417 if (targetm
.calls
.function_value_regno_p (regno
))
1419 mode
= targetm
.calls
.get_raw_result_mode (regno
);
1421 gcc_assert (mode
!= VOIDmode
);
1423 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1424 if (size
% align
!= 0)
1425 size
= CEIL (size
, align
) * align
;
1426 size
+= GET_MODE_SIZE (mode
);
1427 apply_result_mode
[regno
] = mode
;
1430 apply_result_mode
[regno
] = VOIDmode
;
1432 /* Allow targets that use untyped_call and untyped_return to override
1433 the size so that machine-specific information can be stored here. */
1434 #ifdef APPLY_RESULT_SIZE
1435 size
= APPLY_RESULT_SIZE
;
1441 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1442 /* Create a vector describing the result block RESULT. If SAVEP is true,
1443 the result block is used to save the values; otherwise it is used to
1444 restore the values. */
1447 result_vector (int savep
, rtx result
)
1449 int regno
, size
, align
, nelts
;
1452 rtx
*savevec
= XALLOCAVEC (rtx
, FIRST_PSEUDO_REGISTER
);
1455 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1456 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1458 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1459 if (size
% align
!= 0)
1460 size
= CEIL (size
, align
) * align
;
1461 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
1462 mem
= adjust_address (result
, mode
, size
);
1463 savevec
[nelts
++] = (savep
1464 ? gen_rtx_SET (VOIDmode
, mem
, reg
)
1465 : gen_rtx_SET (VOIDmode
, reg
, mem
));
1466 size
+= GET_MODE_SIZE (mode
);
1468 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
1470 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1472 /* Save the state required to perform an untyped call with the same
1473 arguments as were passed to the current function. */
1476 expand_builtin_apply_args_1 (void)
1479 int size
, align
, regno
;
1481 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
1483 /* Create a block where the arg-pointer, structure value address,
1484 and argument registers can be saved. */
1485 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
1487 /* Walk past the arg-pointer and structure value address. */
1488 size
= GET_MODE_SIZE (Pmode
);
1489 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1490 size
+= GET_MODE_SIZE (Pmode
);
1492 /* Save each register used in calling a function to the block. */
1493 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1494 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1496 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1497 if (size
% align
!= 0)
1498 size
= CEIL (size
, align
) * align
;
1500 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1502 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
1503 size
+= GET_MODE_SIZE (mode
);
1506 /* Save the arg pointer to the block. */
1507 tem
= copy_to_reg (crtl
->args
.internal_arg_pointer
);
1508 #ifdef STACK_GROWS_DOWNWARD
1509 /* We need the pointer as the caller actually passed them to us, not
1510 as we might have pretended they were passed. Make sure it's a valid
1511 operand, as emit_move_insn isn't expected to handle a PLUS. */
1513 = force_operand (plus_constant (Pmode
, tem
, crtl
->args
.pretend_args_size
),
1516 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
1518 size
= GET_MODE_SIZE (Pmode
);
1520 /* Save the structure value address unless this is passed as an
1521 "invisible" first argument. */
1522 if (struct_incoming_value
)
1524 emit_move_insn (adjust_address (registers
, Pmode
, size
),
1525 copy_to_reg (struct_incoming_value
));
1526 size
+= GET_MODE_SIZE (Pmode
);
1529 /* Return the address of the block. */
1530 return copy_addr_to_reg (XEXP (registers
, 0));
1533 /* __builtin_apply_args returns block of memory allocated on
1534 the stack into which is stored the arg pointer, structure
1535 value address, static chain, and all the registers that might
1536 possibly be used in performing a function call. The code is
1537 moved to the start of the function so the incoming values are
1541 expand_builtin_apply_args (void)
1543 /* Don't do __builtin_apply_args more than once in a function.
1544 Save the result of the first call and reuse it. */
1545 if (apply_args_value
!= 0)
1546 return apply_args_value
;
1548 /* When this function is called, it means that registers must be
1549 saved on entry to this function. So we migrate the
1550 call to the first insn of this function. */
1555 temp
= expand_builtin_apply_args_1 ();
1559 apply_args_value
= temp
;
1561 /* Put the insns after the NOTE that starts the function.
1562 If this is inside a start_sequence, make the outer-level insn
1563 chain current, so the code is placed at the start of the
1564 function. If internal_arg_pointer is a non-virtual pseudo,
1565 it needs to be placed after the function that initializes
1567 push_topmost_sequence ();
1568 if (REG_P (crtl
->args
.internal_arg_pointer
)
1569 && REGNO (crtl
->args
.internal_arg_pointer
) > LAST_VIRTUAL_REGISTER
)
1570 emit_insn_before (seq
, parm_birth_insn
);
1572 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
1573 pop_topmost_sequence ();
1578 /* Perform an untyped call and save the state required to perform an
1579 untyped return of whatever value was returned by the given function. */
1582 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
1584 int size
, align
, regno
;
1586 rtx incoming_args
, result
, reg
, dest
, src
;
1587 rtx_call_insn
*call_insn
;
1588 rtx old_stack_level
= 0;
1589 rtx call_fusage
= 0;
1590 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
1592 arguments
= convert_memory_address (Pmode
, arguments
);
1594 /* Create a block where the return registers can be saved. */
1595 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
1597 /* Fetch the arg pointer from the ARGUMENTS block. */
1598 incoming_args
= gen_reg_rtx (Pmode
);
1599 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
1600 #ifndef STACK_GROWS_DOWNWARD
1601 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
1602 incoming_args
, 0, OPTAB_LIB_WIDEN
);
1605 /* Push a new argument block and copy the arguments. Do not allow
1606 the (potential) memcpy call below to interfere with our stack
1608 do_pending_stack_adjust ();
1611 /* Save the stack with nonlocal if available. */
1612 #ifdef HAVE_save_stack_nonlocal
1613 if (HAVE_save_stack_nonlocal
)
1614 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
);
1617 emit_stack_save (SAVE_BLOCK
, &old_stack_level
);
1619 /* Allocate a block of memory onto the stack and copy the memory
1620 arguments to the outgoing arguments address. We can pass TRUE
1621 as the 4th argument because we just saved the stack pointer
1622 and will restore it right after the call. */
1623 allocate_dynamic_stack_space (argsize
, 0, BIGGEST_ALIGNMENT
, true);
1625 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1626 may have already set current_function_calls_alloca to true.
1627 current_function_calls_alloca won't be set if argsize is zero,
1628 so we have to guarantee need_drap is true here. */
1629 if (SUPPORTS_STACK_ALIGNMENT
)
1630 crtl
->need_drap
= true;
1632 dest
= virtual_outgoing_args_rtx
;
1633 #ifndef STACK_GROWS_DOWNWARD
1634 if (CONST_INT_P (argsize
))
1635 dest
= plus_constant (Pmode
, dest
, -INTVAL (argsize
));
1637 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
1639 dest
= gen_rtx_MEM (BLKmode
, dest
);
1640 set_mem_align (dest
, PARM_BOUNDARY
);
1641 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
1642 set_mem_align (src
, PARM_BOUNDARY
);
1643 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
1645 /* Refer to the argument block. */
1647 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
1648 set_mem_align (arguments
, PARM_BOUNDARY
);
1650 /* Walk past the arg-pointer and structure value address. */
1651 size
= GET_MODE_SIZE (Pmode
);
1653 size
+= GET_MODE_SIZE (Pmode
);
1655 /* Restore each of the registers previously saved. Make USE insns
1656 for each of these registers for use in making the call. */
1657 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1658 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1660 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1661 if (size
% align
!= 0)
1662 size
= CEIL (size
, align
) * align
;
1663 reg
= gen_rtx_REG (mode
, regno
);
1664 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
1665 use_reg (&call_fusage
, reg
);
1666 size
+= GET_MODE_SIZE (mode
);
1669 /* Restore the structure value address unless this is passed as an
1670 "invisible" first argument. */
1671 size
= GET_MODE_SIZE (Pmode
);
1674 rtx value
= gen_reg_rtx (Pmode
);
1675 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
1676 emit_move_insn (struct_value
, value
);
1677 if (REG_P (struct_value
))
1678 use_reg (&call_fusage
, struct_value
);
1679 size
+= GET_MODE_SIZE (Pmode
);
1682 /* All arguments and registers used for the call are set up by now! */
1683 function
= prepare_call_address (NULL
, function
, NULL
, &call_fusage
, 0, 0);
1685 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1686 and we don't want to load it into a register as an optimization,
1687 because prepare_call_address already did it if it should be done. */
1688 if (GET_CODE (function
) != SYMBOL_REF
)
1689 function
= memory_address (FUNCTION_MODE
, function
);
1691 /* Generate the actual call instruction and save the return value. */
1692 #ifdef HAVE_untyped_call
1693 if (HAVE_untyped_call
)
1694 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE
, function
),
1695 result
, result_vector (1, result
)));
1698 #ifdef HAVE_call_value
1699 if (HAVE_call_value
)
1703 /* Locate the unique return register. It is not possible to
1704 express a call that sets more than one return register using
1705 call_value; use untyped_call for that. In fact, untyped_call
1706 only needs to save the return registers in the given block. */
1707 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1708 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1710 gcc_assert (!valreg
); /* HAVE_untyped_call required. */
1712 valreg
= gen_rtx_REG (mode
, regno
);
1715 emit_call_insn (GEN_CALL_VALUE (valreg
,
1716 gen_rtx_MEM (FUNCTION_MODE
, function
),
1717 const0_rtx
, NULL_RTX
, const0_rtx
));
1719 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
1725 /* Find the CALL insn we just emitted, and attach the register usage
1727 call_insn
= last_call_insn ();
1728 add_function_usage_to (call_insn
, call_fusage
);
1730 /* Restore the stack. */
1731 #ifdef HAVE_save_stack_nonlocal
1732 if (HAVE_save_stack_nonlocal
)
1733 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
);
1736 emit_stack_restore (SAVE_BLOCK
, old_stack_level
);
1737 fixup_args_size_notes (call_insn
, get_last_insn (), 0);
1741 /* Return the address of the result block. */
1742 result
= copy_addr_to_reg (XEXP (result
, 0));
1743 return convert_memory_address (ptr_mode
, result
);
1746 /* Perform an untyped return. */
1749 expand_builtin_return (rtx result
)
1751 int size
, align
, regno
;
1754 rtx_insn
*call_fusage
= 0;
1756 result
= convert_memory_address (Pmode
, result
);
1758 apply_result_size ();
1759 result
= gen_rtx_MEM (BLKmode
, result
);
1761 #ifdef HAVE_untyped_return
1762 if (HAVE_untyped_return
)
1764 emit_jump_insn (gen_untyped_return (result
, result_vector (0, result
)));
1770 /* Restore the return value and note that each value is used. */
1772 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1773 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1775 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1776 if (size
% align
!= 0)
1777 size
= CEIL (size
, align
) * align
;
1778 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1779 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
1781 push_to_sequence (call_fusage
);
1783 call_fusage
= get_insns ();
1785 size
+= GET_MODE_SIZE (mode
);
1788 /* Put the USE insns before the return. */
1789 emit_insn (call_fusage
);
1791 /* Return whatever values was restored by jumping directly to the end
1793 expand_naked_return ();
1796 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1798 static enum type_class
1799 type_to_class (tree type
)
1801 switch (TREE_CODE (type
))
1803 case VOID_TYPE
: return void_type_class
;
1804 case INTEGER_TYPE
: return integer_type_class
;
1805 case ENUMERAL_TYPE
: return enumeral_type_class
;
1806 case BOOLEAN_TYPE
: return boolean_type_class
;
1807 case POINTER_TYPE
: return pointer_type_class
;
1808 case REFERENCE_TYPE
: return reference_type_class
;
1809 case OFFSET_TYPE
: return offset_type_class
;
1810 case REAL_TYPE
: return real_type_class
;
1811 case COMPLEX_TYPE
: return complex_type_class
;
1812 case FUNCTION_TYPE
: return function_type_class
;
1813 case METHOD_TYPE
: return method_type_class
;
1814 case RECORD_TYPE
: return record_type_class
;
1816 case QUAL_UNION_TYPE
: return union_type_class
;
1817 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
1818 ? string_type_class
: array_type_class
);
1819 case LANG_TYPE
: return lang_type_class
;
1820 default: return no_type_class
;
1824 /* Expand a call EXP to __builtin_classify_type. */
1827 expand_builtin_classify_type (tree exp
)
1829 if (call_expr_nargs (exp
))
1830 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))));
1831 return GEN_INT (no_type_class
);
1834 /* This helper macro, meant to be used in mathfn_built_in below,
1835 determines which among a set of three builtin math functions is
1836 appropriate for a given type mode. The `F' and `L' cases are
1837 automatically generated from the `double' case. */
1838 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1839 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1840 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1841 fcodel = BUILT_IN_MATHFN##L ; break;
1842 /* Similar to above, but appends _R after any F/L suffix. */
1843 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1844 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1845 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1846 fcodel = BUILT_IN_MATHFN##L_R ; break;
1848 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1849 if available. If IMPLICIT is true use the implicit builtin declaration,
1850 otherwise use the explicit declaration. If we can't do the conversion,
1854 mathfn_built_in_1 (tree type
, enum built_in_function fn
, bool implicit_p
)
1856 enum built_in_function fcode
, fcodef
, fcodel
, fcode2
;
1860 CASE_MATHFN (BUILT_IN_ACOS
)
1861 CASE_MATHFN (BUILT_IN_ACOSH
)
1862 CASE_MATHFN (BUILT_IN_ASIN
)
1863 CASE_MATHFN (BUILT_IN_ASINH
)
1864 CASE_MATHFN (BUILT_IN_ATAN
)
1865 CASE_MATHFN (BUILT_IN_ATAN2
)
1866 CASE_MATHFN (BUILT_IN_ATANH
)
1867 CASE_MATHFN (BUILT_IN_CBRT
)
1868 CASE_MATHFN (BUILT_IN_CEIL
)
1869 CASE_MATHFN (BUILT_IN_CEXPI
)
1870 CASE_MATHFN (BUILT_IN_COPYSIGN
)
1871 CASE_MATHFN (BUILT_IN_COS
)
1872 CASE_MATHFN (BUILT_IN_COSH
)
1873 CASE_MATHFN (BUILT_IN_DREM
)
1874 CASE_MATHFN (BUILT_IN_ERF
)
1875 CASE_MATHFN (BUILT_IN_ERFC
)
1876 CASE_MATHFN (BUILT_IN_EXP
)
1877 CASE_MATHFN (BUILT_IN_EXP10
)
1878 CASE_MATHFN (BUILT_IN_EXP2
)
1879 CASE_MATHFN (BUILT_IN_EXPM1
)
1880 CASE_MATHFN (BUILT_IN_FABS
)
1881 CASE_MATHFN (BUILT_IN_FDIM
)
1882 CASE_MATHFN (BUILT_IN_FLOOR
)
1883 CASE_MATHFN (BUILT_IN_FMA
)
1884 CASE_MATHFN (BUILT_IN_FMAX
)
1885 CASE_MATHFN (BUILT_IN_FMIN
)
1886 CASE_MATHFN (BUILT_IN_FMOD
)
1887 CASE_MATHFN (BUILT_IN_FREXP
)
1888 CASE_MATHFN (BUILT_IN_GAMMA
)
1889 CASE_MATHFN_REENT (BUILT_IN_GAMMA
) /* GAMMA_R */
1890 CASE_MATHFN (BUILT_IN_HUGE_VAL
)
1891 CASE_MATHFN (BUILT_IN_HYPOT
)
1892 CASE_MATHFN (BUILT_IN_ILOGB
)
1893 CASE_MATHFN (BUILT_IN_ICEIL
)
1894 CASE_MATHFN (BUILT_IN_IFLOOR
)
1895 CASE_MATHFN (BUILT_IN_INF
)
1896 CASE_MATHFN (BUILT_IN_IRINT
)
1897 CASE_MATHFN (BUILT_IN_IROUND
)
1898 CASE_MATHFN (BUILT_IN_ISINF
)
1899 CASE_MATHFN (BUILT_IN_J0
)
1900 CASE_MATHFN (BUILT_IN_J1
)
1901 CASE_MATHFN (BUILT_IN_JN
)
1902 CASE_MATHFN (BUILT_IN_LCEIL
)
1903 CASE_MATHFN (BUILT_IN_LDEXP
)
1904 CASE_MATHFN (BUILT_IN_LFLOOR
)
1905 CASE_MATHFN (BUILT_IN_LGAMMA
)
1906 CASE_MATHFN_REENT (BUILT_IN_LGAMMA
) /* LGAMMA_R */
1907 CASE_MATHFN (BUILT_IN_LLCEIL
)
1908 CASE_MATHFN (BUILT_IN_LLFLOOR
)
1909 CASE_MATHFN (BUILT_IN_LLRINT
)
1910 CASE_MATHFN (BUILT_IN_LLROUND
)
1911 CASE_MATHFN (BUILT_IN_LOG
)
1912 CASE_MATHFN (BUILT_IN_LOG10
)
1913 CASE_MATHFN (BUILT_IN_LOG1P
)
1914 CASE_MATHFN (BUILT_IN_LOG2
)
1915 CASE_MATHFN (BUILT_IN_LOGB
)
1916 CASE_MATHFN (BUILT_IN_LRINT
)
1917 CASE_MATHFN (BUILT_IN_LROUND
)
1918 CASE_MATHFN (BUILT_IN_MODF
)
1919 CASE_MATHFN (BUILT_IN_NAN
)
1920 CASE_MATHFN (BUILT_IN_NANS
)
1921 CASE_MATHFN (BUILT_IN_NEARBYINT
)
1922 CASE_MATHFN (BUILT_IN_NEXTAFTER
)
1923 CASE_MATHFN (BUILT_IN_NEXTTOWARD
)
1924 CASE_MATHFN (BUILT_IN_POW
)
1925 CASE_MATHFN (BUILT_IN_POWI
)
1926 CASE_MATHFN (BUILT_IN_POW10
)
1927 CASE_MATHFN (BUILT_IN_REMAINDER
)
1928 CASE_MATHFN (BUILT_IN_REMQUO
)
1929 CASE_MATHFN (BUILT_IN_RINT
)
1930 CASE_MATHFN (BUILT_IN_ROUND
)
1931 CASE_MATHFN (BUILT_IN_SCALB
)
1932 CASE_MATHFN (BUILT_IN_SCALBLN
)
1933 CASE_MATHFN (BUILT_IN_SCALBN
)
1934 CASE_MATHFN (BUILT_IN_SIGNBIT
)
1935 CASE_MATHFN (BUILT_IN_SIGNIFICAND
)
1936 CASE_MATHFN (BUILT_IN_SIN
)
1937 CASE_MATHFN (BUILT_IN_SINCOS
)
1938 CASE_MATHFN (BUILT_IN_SINH
)
1939 CASE_MATHFN (BUILT_IN_SQRT
)
1940 CASE_MATHFN (BUILT_IN_TAN
)
1941 CASE_MATHFN (BUILT_IN_TANH
)
1942 CASE_MATHFN (BUILT_IN_TGAMMA
)
1943 CASE_MATHFN (BUILT_IN_TRUNC
)
1944 CASE_MATHFN (BUILT_IN_Y0
)
1945 CASE_MATHFN (BUILT_IN_Y1
)
1946 CASE_MATHFN (BUILT_IN_YN
)
1952 if (TYPE_MAIN_VARIANT (type
) == double_type_node
)
1954 else if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1956 else if (TYPE_MAIN_VARIANT (type
) == long_double_type_node
)
1961 if (implicit_p
&& !builtin_decl_implicit_p (fcode2
))
1964 return builtin_decl_explicit (fcode2
);
1967 /* Like mathfn_built_in_1(), but always use the implicit array. */
1970 mathfn_built_in (tree type
, enum built_in_function fn
)
1972 return mathfn_built_in_1 (type
, fn
, /*implicit=*/ 1);
1975 /* If errno must be maintained, expand the RTL to check if the result,
1976 TARGET, of a built-in function call, EXP, is NaN, and if so set
1980 expand_errno_check (tree exp
, rtx target
)
1982 rtx_code_label
*lab
= gen_label_rtx ();
1984 /* Test the result; if it is NaN, set errno=EDOM because
1985 the argument was not in the domain. */
1986 do_compare_rtx_and_jump (target
, target
, EQ
, 0, GET_MODE (target
),
1987 NULL_RTX
, NULL_RTX
, lab
,
1988 /* The jump is very likely. */
1989 REG_BR_PROB_BASE
- (REG_BR_PROB_BASE
/ 2000 - 1));
1992 /* If this built-in doesn't throw an exception, set errno directly. */
1993 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp
), 0)))
1995 #ifdef GEN_ERRNO_RTX
1996 rtx errno_rtx
= GEN_ERRNO_RTX
;
1999 = gen_rtx_MEM (word_mode
, gen_rtx_SYMBOL_REF (Pmode
, "errno"));
2001 emit_move_insn (errno_rtx
,
2002 gen_int_mode (TARGET_EDOM
, GET_MODE (errno_rtx
)));
2008 /* Make sure the library call isn't expanded as a tail call. */
2009 CALL_EXPR_TAILCALL (exp
) = 0;
2011 /* We can't set errno=EDOM directly; let the library call do it.
2012 Pop the arguments right away in case the call gets deleted. */
2014 expand_call (exp
, target
, 0);
2019 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2020 Return NULL_RTX if a normal call should be emitted rather than expanding
2021 the function in-line. EXP is the expression that is a call to the builtin
2022 function; if convenient, the result should be placed in TARGET.
2023 SUBTARGET may be used as the target for computing one of EXP's operands. */
2026 expand_builtin_mathfn (tree exp
, rtx target
, rtx subtarget
)
2028 optab builtin_optab
;
2031 tree fndecl
= get_callee_fndecl (exp
);
2033 bool errno_set
= false;
2034 bool try_widening
= false;
2037 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2040 arg
= CALL_EXPR_ARG (exp
, 0);
2042 switch (DECL_FUNCTION_CODE (fndecl
))
2044 CASE_FLT_FN (BUILT_IN_SQRT
):
2045 errno_set
= ! tree_expr_nonnegative_p (arg
);
2046 try_widening
= true;
2047 builtin_optab
= sqrt_optab
;
2049 CASE_FLT_FN (BUILT_IN_EXP
):
2050 errno_set
= true; builtin_optab
= exp_optab
; break;
2051 CASE_FLT_FN (BUILT_IN_EXP10
):
2052 CASE_FLT_FN (BUILT_IN_POW10
):
2053 errno_set
= true; builtin_optab
= exp10_optab
; break;
2054 CASE_FLT_FN (BUILT_IN_EXP2
):
2055 errno_set
= true; builtin_optab
= exp2_optab
; break;
2056 CASE_FLT_FN (BUILT_IN_EXPM1
):
2057 errno_set
= true; builtin_optab
= expm1_optab
; break;
2058 CASE_FLT_FN (BUILT_IN_LOGB
):
2059 errno_set
= true; builtin_optab
= logb_optab
; break;
2060 CASE_FLT_FN (BUILT_IN_LOG
):
2061 errno_set
= true; builtin_optab
= log_optab
; break;
2062 CASE_FLT_FN (BUILT_IN_LOG10
):
2063 errno_set
= true; builtin_optab
= log10_optab
; break;
2064 CASE_FLT_FN (BUILT_IN_LOG2
):
2065 errno_set
= true; builtin_optab
= log2_optab
; break;
2066 CASE_FLT_FN (BUILT_IN_LOG1P
):
2067 errno_set
= true; builtin_optab
= log1p_optab
; break;
2068 CASE_FLT_FN (BUILT_IN_ASIN
):
2069 builtin_optab
= asin_optab
; break;
2070 CASE_FLT_FN (BUILT_IN_ACOS
):
2071 builtin_optab
= acos_optab
; break;
2072 CASE_FLT_FN (BUILT_IN_TAN
):
2073 builtin_optab
= tan_optab
; break;
2074 CASE_FLT_FN (BUILT_IN_ATAN
):
2075 builtin_optab
= atan_optab
; break;
2076 CASE_FLT_FN (BUILT_IN_FLOOR
):
2077 builtin_optab
= floor_optab
; break;
2078 CASE_FLT_FN (BUILT_IN_CEIL
):
2079 builtin_optab
= ceil_optab
; break;
2080 CASE_FLT_FN (BUILT_IN_TRUNC
):
2081 builtin_optab
= btrunc_optab
; break;
2082 CASE_FLT_FN (BUILT_IN_ROUND
):
2083 builtin_optab
= round_optab
; break;
2084 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
2085 builtin_optab
= nearbyint_optab
;
2086 if (flag_trapping_math
)
2088 /* Else fallthrough and expand as rint. */
2089 CASE_FLT_FN (BUILT_IN_RINT
):
2090 builtin_optab
= rint_optab
; break;
2091 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
2092 builtin_optab
= significand_optab
; break;
2097 /* Make a suitable register to place result in. */
2098 mode
= TYPE_MODE (TREE_TYPE (exp
));
2100 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2103 /* Before working hard, check whether the instruction is available, but try
2104 to widen the mode for specific operations. */
2105 if ((optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
2106 || (try_widening
&& !excess_precision_type (TREE_TYPE (exp
))))
2107 && (!errno_set
|| !optimize_insn_for_size_p ()))
2109 rtx result
= gen_reg_rtx (mode
);
2111 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2112 need to expand the argument again. This way, we will not perform
2113 side-effects more the once. */
2114 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2116 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2120 /* Compute into RESULT.
2121 Set RESULT to wherever the result comes back. */
2122 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2127 expand_errno_check (exp
, result
);
2129 /* Output the entire sequence. */
2130 insns
= get_insns ();
2136 /* If we were unable to expand via the builtin, stop the sequence
2137 (without outputting the insns) and call to the library function
2138 with the stabilized argument list. */
2142 return expand_call (exp
, target
, target
== const0_rtx
);
2145 /* Expand a call to the builtin binary math functions (pow and atan2).
2146 Return NULL_RTX if a normal call should be emitted rather than expanding the
2147 function in-line. EXP is the expression that is a call to the builtin
2148 function; if convenient, the result should be placed in TARGET.
2149 SUBTARGET may be used as the target for computing one of EXP's
2153 expand_builtin_mathfn_2 (tree exp
, rtx target
, rtx subtarget
)
2155 optab builtin_optab
;
2156 rtx op0
, op1
, result
;
2158 int op1_type
= REAL_TYPE
;
2159 tree fndecl
= get_callee_fndecl (exp
);
2162 bool errno_set
= true;
2164 switch (DECL_FUNCTION_CODE (fndecl
))
2166 CASE_FLT_FN (BUILT_IN_SCALBN
):
2167 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2168 CASE_FLT_FN (BUILT_IN_LDEXP
):
2169 op1_type
= INTEGER_TYPE
;
2174 if (!validate_arglist (exp
, REAL_TYPE
, op1_type
, VOID_TYPE
))
2177 arg0
= CALL_EXPR_ARG (exp
, 0);
2178 arg1
= CALL_EXPR_ARG (exp
, 1);
2180 switch (DECL_FUNCTION_CODE (fndecl
))
2182 CASE_FLT_FN (BUILT_IN_POW
):
2183 builtin_optab
= pow_optab
; break;
2184 CASE_FLT_FN (BUILT_IN_ATAN2
):
2185 builtin_optab
= atan2_optab
; break;
2186 CASE_FLT_FN (BUILT_IN_SCALB
):
2187 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2189 builtin_optab
= scalb_optab
; break;
2190 CASE_FLT_FN (BUILT_IN_SCALBN
):
2191 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2192 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2194 /* Fall through... */
2195 CASE_FLT_FN (BUILT_IN_LDEXP
):
2196 builtin_optab
= ldexp_optab
; break;
2197 CASE_FLT_FN (BUILT_IN_FMOD
):
2198 builtin_optab
= fmod_optab
; break;
2199 CASE_FLT_FN (BUILT_IN_REMAINDER
):
2200 CASE_FLT_FN (BUILT_IN_DREM
):
2201 builtin_optab
= remainder_optab
; break;
2206 /* Make a suitable register to place result in. */
2207 mode
= TYPE_MODE (TREE_TYPE (exp
));
2209 /* Before working hard, check whether the instruction is available. */
2210 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2213 result
= gen_reg_rtx (mode
);
2215 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2218 if (errno_set
&& optimize_insn_for_size_p ())
2221 /* Always stabilize the argument list. */
2222 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2223 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2225 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2226 op1
= expand_normal (arg1
);
2230 /* Compute into RESULT.
2231 Set RESULT to wherever the result comes back. */
2232 result
= expand_binop (mode
, builtin_optab
, op0
, op1
,
2233 result
, 0, OPTAB_DIRECT
);
2235 /* If we were unable to expand via the builtin, stop the sequence
2236 (without outputting the insns) and call to the library function
2237 with the stabilized argument list. */
2241 return expand_call (exp
, target
, target
== const0_rtx
);
2245 expand_errno_check (exp
, result
);
2247 /* Output the entire sequence. */
2248 insns
= get_insns ();
2255 /* Expand a call to the builtin trinary math functions (fma).
2256 Return NULL_RTX if a normal call should be emitted rather than expanding the
2257 function in-line. EXP is the expression that is a call to the builtin
2258 function; if convenient, the result should be placed in TARGET.
2259 SUBTARGET may be used as the target for computing one of EXP's
2263 expand_builtin_mathfn_ternary (tree exp
, rtx target
, rtx subtarget
)
2265 optab builtin_optab
;
2266 rtx op0
, op1
, op2
, result
;
2268 tree fndecl
= get_callee_fndecl (exp
);
2269 tree arg0
, arg1
, arg2
;
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
;
2342 tree fndecl
= get_callee_fndecl (exp
);
2346 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2349 arg
= CALL_EXPR_ARG (exp
, 0);
2351 switch (DECL_FUNCTION_CODE (fndecl
))
2353 CASE_FLT_FN (BUILT_IN_SIN
):
2354 CASE_FLT_FN (BUILT_IN_COS
):
2355 builtin_optab
= sincos_optab
; break;
2360 /* Make a suitable register to place result in. */
2361 mode
= TYPE_MODE (TREE_TYPE (exp
));
2363 /* Check if sincos insn is available, otherwise fallback
2364 to sin or cos insn. */
2365 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2366 switch (DECL_FUNCTION_CODE (fndecl
))
2368 CASE_FLT_FN (BUILT_IN_SIN
):
2369 builtin_optab
= sin_optab
; break;
2370 CASE_FLT_FN (BUILT_IN_COS
):
2371 builtin_optab
= cos_optab
; break;
2376 /* Before working hard, check whether the instruction is available. */
2377 if (optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
)
2379 rtx result
= gen_reg_rtx (mode
);
2381 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2382 need to expand the argument again. This way, we will not perform
2383 side-effects more the once. */
2384 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2386 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2390 /* Compute into RESULT.
2391 Set RESULT to wherever the result comes back. */
2392 if (builtin_optab
== sincos_optab
)
2396 switch (DECL_FUNCTION_CODE (fndecl
))
2398 CASE_FLT_FN (BUILT_IN_SIN
):
2399 ok
= expand_twoval_unop (builtin_optab
, op0
, 0, result
, 0);
2401 CASE_FLT_FN (BUILT_IN_COS
):
2402 ok
= expand_twoval_unop (builtin_optab
, op0
, result
, 0, 0);
2410 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2414 /* Output the entire sequence. */
2415 insns
= get_insns ();
2421 /* If we were unable to expand via the builtin, stop the sequence
2422 (without outputting the insns) and call to the library function
2423 with the stabilized argument list. */
2427 return expand_call (exp
, target
, target
== const0_rtx
);
2430 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2431 return an RTL instruction code that implements the functionality.
2432 If that isn't possible or available return CODE_FOR_nothing. */
2434 static enum insn_code
2435 interclass_mathfn_icode (tree arg
, tree fndecl
)
2437 bool errno_set
= false;
2438 optab builtin_optab
= unknown_optab
;
2441 switch (DECL_FUNCTION_CODE (fndecl
))
2443 CASE_FLT_FN (BUILT_IN_ILOGB
):
2444 errno_set
= true; builtin_optab
= ilogb_optab
; break;
2445 CASE_FLT_FN (BUILT_IN_ISINF
):
2446 builtin_optab
= isinf_optab
; break;
2447 case BUILT_IN_ISNORMAL
:
2448 case BUILT_IN_ISFINITE
:
2449 CASE_FLT_FN (BUILT_IN_FINITE
):
2450 case BUILT_IN_FINITED32
:
2451 case BUILT_IN_FINITED64
:
2452 case BUILT_IN_FINITED128
:
2453 case BUILT_IN_ISINFD32
:
2454 case BUILT_IN_ISINFD64
:
2455 case BUILT_IN_ISINFD128
:
2456 /* These builtins have no optabs (yet). */
2462 /* There's no easy way to detect the case we need to set EDOM. */
2463 if (flag_errno_math
&& errno_set
)
2464 return CODE_FOR_nothing
;
2466 /* Optab mode depends on the mode of the input argument. */
2467 mode
= TYPE_MODE (TREE_TYPE (arg
));
2470 return optab_handler (builtin_optab
, mode
);
2471 return CODE_FOR_nothing
;
2474 /* Expand a call to one of the builtin math functions that operate on
2475 floating point argument and output an integer result (ilogb, isinf,
2477 Return 0 if a normal call should be emitted rather than expanding the
2478 function in-line. EXP is the expression that is a call to the builtin
2479 function; if convenient, the result should be placed in TARGET. */
2482 expand_builtin_interclass_mathfn (tree exp
, rtx target
)
2484 enum insn_code icode
= CODE_FOR_nothing
;
2486 tree fndecl
= get_callee_fndecl (exp
);
2490 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2493 arg
= CALL_EXPR_ARG (exp
, 0);
2494 icode
= interclass_mathfn_icode (arg
, fndecl
);
2495 mode
= TYPE_MODE (TREE_TYPE (arg
));
2497 if (icode
!= CODE_FOR_nothing
)
2499 struct expand_operand ops
[1];
2500 rtx_insn
*last
= get_last_insn ();
2501 tree orig_arg
= arg
;
2503 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2504 need to expand the argument again. This way, we will not perform
2505 side-effects more the once. */
2506 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2508 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2510 if (mode
!= GET_MODE (op0
))
2511 op0
= convert_to_mode (mode
, op0
, 0);
2513 create_output_operand (&ops
[0], target
, TYPE_MODE (TREE_TYPE (exp
)));
2514 if (maybe_legitimize_operands (icode
, 0, 1, ops
)
2515 && maybe_emit_unop_insn (icode
, ops
[0].value
, op0
, UNKNOWN
))
2516 return ops
[0].value
;
2518 delete_insns_since (last
);
2519 CALL_EXPR_ARG (exp
, 0) = orig_arg
;
2525 /* Expand a call to the builtin sincos math function.
2526 Return NULL_RTX if a normal call should be emitted rather than expanding the
2527 function in-line. EXP is the expression that is a call to the builtin
2531 expand_builtin_sincos (tree exp
)
2533 rtx op0
, op1
, op2
, target1
, target2
;
2535 tree arg
, sinp
, cosp
;
2537 location_t loc
= EXPR_LOCATION (exp
);
2538 tree alias_type
, alias_off
;
2540 if (!validate_arglist (exp
, REAL_TYPE
,
2541 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2544 arg
= CALL_EXPR_ARG (exp
, 0);
2545 sinp
= CALL_EXPR_ARG (exp
, 1);
2546 cosp
= CALL_EXPR_ARG (exp
, 2);
2548 /* Make a suitable register to place result in. */
2549 mode
= TYPE_MODE (TREE_TYPE (arg
));
2551 /* Check if sincos insn is available, otherwise emit the call. */
2552 if (optab_handler (sincos_optab
, mode
) == CODE_FOR_nothing
)
2555 target1
= gen_reg_rtx (mode
);
2556 target2
= gen_reg_rtx (mode
);
2558 op0
= expand_normal (arg
);
2559 alias_type
= build_pointer_type_for_mode (TREE_TYPE (arg
), ptr_mode
, true);
2560 alias_off
= build_int_cst (alias_type
, 0);
2561 op1
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2563 op2
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2566 /* Compute into target1 and target2.
2567 Set TARGET to wherever the result comes back. */
2568 result
= expand_twoval_unop (sincos_optab
, op0
, target2
, target1
, 0);
2569 gcc_assert (result
);
2571 /* Move target1 and target2 to the memory locations indicated
2573 emit_move_insn (op1
, target1
);
2574 emit_move_insn (op2
, target2
);
2579 /* Expand a call to the internal cexpi builtin to the sincos math function.
2580 EXP is the expression that is a call to the builtin function; if convenient,
2581 the result should be placed in TARGET. */
2584 expand_builtin_cexpi (tree exp
, rtx target
)
2586 tree fndecl
= get_callee_fndecl (exp
);
2590 location_t loc
= EXPR_LOCATION (exp
);
2592 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2595 arg
= CALL_EXPR_ARG (exp
, 0);
2596 type
= TREE_TYPE (arg
);
2597 mode
= TYPE_MODE (TREE_TYPE (arg
));
2599 /* Try expanding via a sincos optab, fall back to emitting a libcall
2600 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2601 is only generated from sincos, cexp or if we have either of them. */
2602 if (optab_handler (sincos_optab
, mode
) != CODE_FOR_nothing
)
2604 op1
= gen_reg_rtx (mode
);
2605 op2
= gen_reg_rtx (mode
);
2607 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2609 /* Compute into op1 and op2. */
2610 expand_twoval_unop (sincos_optab
, op0
, op2
, op1
, 0);
2612 else if (targetm
.libc_has_function (function_sincos
))
2614 tree call
, fn
= NULL_TREE
;
2618 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2619 fn
= builtin_decl_explicit (BUILT_IN_SINCOSF
);
2620 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2621 fn
= builtin_decl_explicit (BUILT_IN_SINCOS
);
2622 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2623 fn
= builtin_decl_explicit (BUILT_IN_SINCOSL
);
2627 op1
= assign_temp (TREE_TYPE (arg
), 1, 1);
2628 op2
= assign_temp (TREE_TYPE (arg
), 1, 1);
2629 op1a
= copy_addr_to_reg (XEXP (op1
, 0));
2630 op2a
= copy_addr_to_reg (XEXP (op2
, 0));
2631 top1
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op1a
);
2632 top2
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op2a
);
2634 /* Make sure not to fold the sincos call again. */
2635 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2636 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn
)),
2637 call
, 3, arg
, top1
, top2
));
2641 tree call
, fn
= NULL_TREE
, narg
;
2642 tree ctype
= build_complex_type (type
);
2644 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2645 fn
= builtin_decl_explicit (BUILT_IN_CEXPF
);
2646 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2647 fn
= builtin_decl_explicit (BUILT_IN_CEXP
);
2648 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2649 fn
= builtin_decl_explicit (BUILT_IN_CEXPL
);
2653 /* If we don't have a decl for cexp create one. This is the
2654 friendliest fallback if the user calls __builtin_cexpi
2655 without full target C99 function support. */
2656 if (fn
== NULL_TREE
)
2659 const char *name
= NULL
;
2661 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2663 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2665 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2668 fntype
= build_function_type_list (ctype
, ctype
, NULL_TREE
);
2669 fn
= build_fn_decl (name
, fntype
);
2672 narg
= fold_build2_loc (loc
, COMPLEX_EXPR
, ctype
,
2673 build_real (type
, dconst0
), arg
);
2675 /* Make sure not to fold the cexp call again. */
2676 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2677 return expand_expr (build_call_nary (ctype
, call
, 1, narg
),
2678 target
, VOIDmode
, EXPAND_NORMAL
);
2681 /* Now build the proper return type. */
2682 return expand_expr (build2 (COMPLEX_EXPR
, build_complex_type (type
),
2683 make_tree (TREE_TYPE (arg
), op2
),
2684 make_tree (TREE_TYPE (arg
), op1
)),
2685 target
, VOIDmode
, EXPAND_NORMAL
);
2688 /* Conveniently construct a function call expression. FNDECL names the
2689 function to be called, N is the number of arguments, and the "..."
2690 parameters are the argument expressions. Unlike build_call_exr
2691 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2694 build_call_nofold_loc (location_t loc
, tree fndecl
, int n
, ...)
2697 tree fntype
= TREE_TYPE (fndecl
);
2698 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), fndecl
);
2701 fn
= build_call_valist (TREE_TYPE (fntype
), fn
, n
, ap
);
2703 SET_EXPR_LOCATION (fn
, loc
);
2707 /* Expand a call to one of the builtin rounding functions gcc defines
2708 as an extension (lfloor and lceil). As these are gcc extensions we
2709 do not need to worry about setting errno to EDOM.
2710 If expanding via optab fails, lower expression to (int)(floor(x)).
2711 EXP is the expression that is a call to the builtin function;
2712 if convenient, the result should be placed in TARGET. */
2715 expand_builtin_int_roundingfn (tree exp
, rtx target
)
2717 convert_optab builtin_optab
;
2720 tree fndecl
= get_callee_fndecl (exp
);
2721 enum built_in_function fallback_fn
;
2722 tree fallback_fndecl
;
2726 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2729 arg
= CALL_EXPR_ARG (exp
, 0);
2731 switch (DECL_FUNCTION_CODE (fndecl
))
2733 CASE_FLT_FN (BUILT_IN_ICEIL
):
2734 CASE_FLT_FN (BUILT_IN_LCEIL
):
2735 CASE_FLT_FN (BUILT_IN_LLCEIL
):
2736 builtin_optab
= lceil_optab
;
2737 fallback_fn
= BUILT_IN_CEIL
;
2740 CASE_FLT_FN (BUILT_IN_IFLOOR
):
2741 CASE_FLT_FN (BUILT_IN_LFLOOR
):
2742 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
2743 builtin_optab
= lfloor_optab
;
2744 fallback_fn
= BUILT_IN_FLOOR
;
2751 /* Make a suitable register to place result in. */
2752 mode
= TYPE_MODE (TREE_TYPE (exp
));
2754 target
= gen_reg_rtx (mode
);
2756 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2757 need to expand the argument again. This way, we will not perform
2758 side-effects more the once. */
2759 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2761 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2765 /* Compute into TARGET. */
2766 if (expand_sfix_optab (target
, op0
, builtin_optab
))
2768 /* Output the entire sequence. */
2769 insns
= get_insns ();
2775 /* If we were unable to expand via the builtin, stop the sequence
2776 (without outputting the insns). */
2779 /* Fall back to floating point rounding optab. */
2780 fallback_fndecl
= mathfn_built_in (TREE_TYPE (arg
), fallback_fn
);
2782 /* For non-C99 targets we may end up without a fallback fndecl here
2783 if the user called __builtin_lfloor directly. In this case emit
2784 a call to the floor/ceil variants nevertheless. This should result
2785 in the best user experience for not full C99 targets. */
2786 if (fallback_fndecl
== NULL_TREE
)
2789 const char *name
= NULL
;
2791 switch (DECL_FUNCTION_CODE (fndecl
))
2793 case BUILT_IN_ICEIL
:
2794 case BUILT_IN_LCEIL
:
2795 case BUILT_IN_LLCEIL
:
2798 case BUILT_IN_ICEILF
:
2799 case BUILT_IN_LCEILF
:
2800 case BUILT_IN_LLCEILF
:
2803 case BUILT_IN_ICEILL
:
2804 case BUILT_IN_LCEILL
:
2805 case BUILT_IN_LLCEILL
:
2808 case BUILT_IN_IFLOOR
:
2809 case BUILT_IN_LFLOOR
:
2810 case BUILT_IN_LLFLOOR
:
2813 case BUILT_IN_IFLOORF
:
2814 case BUILT_IN_LFLOORF
:
2815 case BUILT_IN_LLFLOORF
:
2818 case BUILT_IN_IFLOORL
:
2819 case BUILT_IN_LFLOORL
:
2820 case BUILT_IN_LLFLOORL
:
2827 fntype
= build_function_type_list (TREE_TYPE (arg
),
2828 TREE_TYPE (arg
), NULL_TREE
);
2829 fallback_fndecl
= build_fn_decl (name
, fntype
);
2832 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
), fallback_fndecl
, 1, arg
);
2834 tmp
= expand_normal (exp
);
2835 tmp
= maybe_emit_group_store (tmp
, TREE_TYPE (exp
));
2837 /* Truncate the result of floating point optab to integer
2838 via expand_fix (). */
2839 target
= gen_reg_rtx (mode
);
2840 expand_fix (target
, tmp
, 0);
2845 /* Expand a call to one of the builtin math functions doing integer
2847 Return 0 if a normal call should be emitted rather than expanding the
2848 function in-line. EXP is the expression that is a call to the builtin
2849 function; if convenient, the result should be placed in TARGET. */
2852 expand_builtin_int_roundingfn_2 (tree exp
, rtx target
)
2854 convert_optab builtin_optab
;
2857 tree fndecl
= get_callee_fndecl (exp
);
2860 enum built_in_function fallback_fn
= BUILT_IN_NONE
;
2862 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2865 arg
= CALL_EXPR_ARG (exp
, 0);
2867 switch (DECL_FUNCTION_CODE (fndecl
))
2869 CASE_FLT_FN (BUILT_IN_IRINT
):
2870 fallback_fn
= BUILT_IN_LRINT
;
2872 CASE_FLT_FN (BUILT_IN_LRINT
):
2873 CASE_FLT_FN (BUILT_IN_LLRINT
):
2874 builtin_optab
= lrint_optab
;
2877 CASE_FLT_FN (BUILT_IN_IROUND
):
2878 fallback_fn
= BUILT_IN_LROUND
;
2880 CASE_FLT_FN (BUILT_IN_LROUND
):
2881 CASE_FLT_FN (BUILT_IN_LLROUND
):
2882 builtin_optab
= lround_optab
;
2889 /* There's no easy way to detect the case we need to set EDOM. */
2890 if (flag_errno_math
&& fallback_fn
== BUILT_IN_NONE
)
2893 /* Make a suitable register to place result in. */
2894 mode
= TYPE_MODE (TREE_TYPE (exp
));
2896 /* There's no easy way to detect the case we need to set EDOM. */
2897 if (!flag_errno_math
)
2899 rtx result
= gen_reg_rtx (mode
);
2901 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2902 need to expand the argument again. This way, we will not perform
2903 side-effects more the once. */
2904 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2906 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2910 if (expand_sfix_optab (result
, op0
, builtin_optab
))
2912 /* Output the entire sequence. */
2913 insns
= get_insns ();
2919 /* If we were unable to expand via the builtin, stop the sequence
2920 (without outputting the insns) and call to the library function
2921 with the stabilized argument list. */
2925 if (fallback_fn
!= BUILT_IN_NONE
)
2927 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2928 targets, (int) round (x) should never be transformed into
2929 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2930 a call to lround in the hope that the target provides at least some
2931 C99 functions. This should result in the best user experience for
2932 not full C99 targets. */
2933 tree fallback_fndecl
= mathfn_built_in_1 (TREE_TYPE (arg
),
2936 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
),
2937 fallback_fndecl
, 1, arg
);
2939 target
= expand_call (exp
, NULL_RTX
, target
== const0_rtx
);
2940 target
= maybe_emit_group_store (target
, TREE_TYPE (exp
));
2941 return convert_to_mode (mode
, target
, 0);
2944 return expand_call (exp
, target
, target
== const0_rtx
);
2947 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2948 a normal call should be emitted rather than expanding the function
2949 in-line. EXP is the expression that is a call to the builtin
2950 function; if convenient, the result should be placed in TARGET. */
2953 expand_builtin_powi (tree exp
, rtx target
)
2960 if (! validate_arglist (exp
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2963 arg0
= CALL_EXPR_ARG (exp
, 0);
2964 arg1
= CALL_EXPR_ARG (exp
, 1);
2965 mode
= TYPE_MODE (TREE_TYPE (exp
));
2967 /* Emit a libcall to libgcc. */
2969 /* Mode of the 2nd argument must match that of an int. */
2970 mode2
= mode_for_size (INT_TYPE_SIZE
, MODE_INT
, 0);
2972 if (target
== NULL_RTX
)
2973 target
= gen_reg_rtx (mode
);
2975 op0
= expand_expr (arg0
, NULL_RTX
, mode
, EXPAND_NORMAL
);
2976 if (GET_MODE (op0
) != mode
)
2977 op0
= convert_to_mode (mode
, op0
, 0);
2978 op1
= expand_expr (arg1
, NULL_RTX
, mode2
, EXPAND_NORMAL
);
2979 if (GET_MODE (op1
) != mode2
)
2980 op1
= convert_to_mode (mode2
, op1
, 0);
2982 target
= emit_library_call_value (optab_libfunc (powi_optab
, mode
),
2983 target
, LCT_CONST
, mode
, 2,
2984 op0
, mode
, op1
, mode2
);
2989 /* Expand expression EXP which is a call to the strlen builtin. Return
2990 NULL_RTX if we failed the caller should emit a normal call, otherwise
2991 try to get the result in TARGET, if convenient. */
2994 expand_builtin_strlen (tree exp
, rtx target
,
2995 machine_mode target_mode
)
2997 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
3001 struct expand_operand ops
[4];
3004 tree src
= CALL_EXPR_ARG (exp
, 0);
3006 rtx_insn
*before_strlen
;
3007 machine_mode insn_mode
= target_mode
;
3008 enum insn_code icode
= CODE_FOR_nothing
;
3011 /* If the length can be computed at compile-time, return it. */
3012 len
= c_strlen (src
, 0);
3014 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3016 /* If the length can be computed at compile-time and is constant
3017 integer, but there are side-effects in src, evaluate
3018 src for side-effects, then return len.
3019 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3020 can be optimized into: i++; x = 3; */
3021 len
= c_strlen (src
, 1);
3022 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
3024 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3025 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3028 align
= get_pointer_alignment (src
) / BITS_PER_UNIT
;
3030 /* If SRC is not a pointer type, don't do this operation inline. */
3034 /* Bail out if we can't compute strlen in the right mode. */
3035 while (insn_mode
!= VOIDmode
)
3037 icode
= optab_handler (strlen_optab
, insn_mode
);
3038 if (icode
!= CODE_FOR_nothing
)
3041 insn_mode
= GET_MODE_WIDER_MODE (insn_mode
);
3043 if (insn_mode
== VOIDmode
)
3046 /* Make a place to hold the source address. We will not expand
3047 the actual source until we are sure that the expansion will
3048 not fail -- there are trees that cannot be expanded twice. */
3049 src_reg
= gen_reg_rtx (Pmode
);
3051 /* Mark the beginning of the strlen sequence so we can emit the
3052 source operand later. */
3053 before_strlen
= get_last_insn ();
3055 create_output_operand (&ops
[0], target
, insn_mode
);
3056 create_fixed_operand (&ops
[1], gen_rtx_MEM (BLKmode
, src_reg
));
3057 create_integer_operand (&ops
[2], 0);
3058 create_integer_operand (&ops
[3], align
);
3059 if (!maybe_expand_insn (icode
, 4, ops
))
3062 /* Now that we are assured of success, expand the source. */
3064 pat
= expand_expr (src
, src_reg
, Pmode
, EXPAND_NORMAL
);
3067 #ifdef POINTERS_EXTEND_UNSIGNED
3068 if (GET_MODE (pat
) != Pmode
)
3069 pat
= convert_to_mode (Pmode
, pat
,
3070 POINTERS_EXTEND_UNSIGNED
);
3072 emit_move_insn (src_reg
, pat
);
3078 emit_insn_after (pat
, before_strlen
);
3080 emit_insn_before (pat
, get_insns ());
3082 /* Return the value in the proper mode for this function. */
3083 if (GET_MODE (ops
[0].value
) == target_mode
)
3084 target
= ops
[0].value
;
3085 else if (target
!= 0)
3086 convert_move (target
, ops
[0].value
, 0);
3088 target
= convert_to_mode (target_mode
, ops
[0].value
, 0);
3094 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3095 bytes from constant string DATA + OFFSET and return it as target
3099 builtin_memcpy_read_str (void *data
, HOST_WIDE_INT offset
,
3102 const char *str
= (const char *) data
;
3104 gcc_assert (offset
>= 0
3105 && ((unsigned HOST_WIDE_INT
) offset
+ GET_MODE_SIZE (mode
)
3106 <= strlen (str
) + 1));
3108 return c_readstr (str
+ offset
, mode
);
3111 /* LEN specify length of the block of memcpy/memset operation.
3112 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3113 In some cases we can make very likely guess on max size, then we
3114 set it into PROBABLE_MAX_SIZE. */
3117 determine_block_size (tree len
, rtx len_rtx
,
3118 unsigned HOST_WIDE_INT
*min_size
,
3119 unsigned HOST_WIDE_INT
*max_size
,
3120 unsigned HOST_WIDE_INT
*probable_max_size
)
3122 if (CONST_INT_P (len_rtx
))
3124 *min_size
= *max_size
= *probable_max_size
= UINTVAL (len_rtx
);
3130 enum value_range_type range_type
= VR_UNDEFINED
;
3132 /* Determine bounds from the type. */
3133 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len
))))
3134 *min_size
= tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len
)));
3137 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len
))))
3138 *probable_max_size
= *max_size
3139 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len
)));
3141 *probable_max_size
= *max_size
= GET_MODE_MASK (GET_MODE (len_rtx
));
3143 if (TREE_CODE (len
) == SSA_NAME
)
3144 range_type
= get_range_info (len
, &min
, &max
);
3145 if (range_type
== VR_RANGE
)
3147 if (wi::fits_uhwi_p (min
) && *min_size
< min
.to_uhwi ())
3148 *min_size
= min
.to_uhwi ();
3149 if (wi::fits_uhwi_p (max
) && *max_size
> max
.to_uhwi ())
3150 *probable_max_size
= *max_size
= max
.to_uhwi ();
3152 else if (range_type
== VR_ANTI_RANGE
)
3154 /* Anti range 0...N lets us to determine minimal size to N+1. */
3157 if (wi::fits_uhwi_p (max
) && max
.to_uhwi () + 1 != 0)
3158 *min_size
= max
.to_uhwi () + 1;
3166 Produce anti range allowing negative values of N. We still
3167 can use the information and make a guess that N is not negative.
3169 else if (!wi::leu_p (max
, 1 << 30) && wi::fits_uhwi_p (min
))
3170 *probable_max_size
= min
.to_uhwi () - 1;
3173 gcc_checking_assert (*max_size
<=
3174 (unsigned HOST_WIDE_INT
)
3175 GET_MODE_MASK (GET_MODE (len_rtx
)));
3178 /* Helper function to do the actual work for expand_builtin_memcpy. */
3181 expand_builtin_memcpy_args (tree dest
, tree src
, tree len
, rtx target
, tree exp
)
3183 const char *src_str
;
3184 unsigned int src_align
= get_pointer_alignment (src
);
3185 unsigned int dest_align
= get_pointer_alignment (dest
);
3186 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
3187 HOST_WIDE_INT expected_size
= -1;
3188 unsigned int expected_align
= 0;
3189 unsigned HOST_WIDE_INT min_size
;
3190 unsigned HOST_WIDE_INT max_size
;
3191 unsigned HOST_WIDE_INT probable_max_size
;
3193 /* If DEST is not a pointer type, call the normal function. */
3194 if (dest_align
== 0)
3197 /* If either SRC is not a pointer type, don't do this
3198 operation in-line. */
3202 if (currently_expanding_gimple_stmt
)
3203 stringop_block_profile (currently_expanding_gimple_stmt
,
3204 &expected_align
, &expected_size
);
3206 if (expected_align
< dest_align
)
3207 expected_align
= dest_align
;
3208 dest_mem
= get_memory_rtx (dest
, len
);
3209 set_mem_align (dest_mem
, dest_align
);
3210 len_rtx
= expand_normal (len
);
3211 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3212 &probable_max_size
);
3213 src_str
= c_getstr (src
);
3215 /* If SRC is a string constant and block move would be done
3216 by pieces, we can avoid loading the string from memory
3217 and only stored the computed constants. */
3219 && CONST_INT_P (len_rtx
)
3220 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3221 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3222 CONST_CAST (char *, src_str
),
3225 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3226 builtin_memcpy_read_str
,
3227 CONST_CAST (char *, src_str
),
3228 dest_align
, false, 0);
3229 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3230 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3234 src_mem
= get_memory_rtx (src
, len
);
3235 set_mem_align (src_mem
, src_align
);
3237 /* Copy word part most expediently. */
3238 dest_addr
= emit_block_move_hints (dest_mem
, src_mem
, len_rtx
,
3239 CALL_EXPR_TAILCALL (exp
)
3240 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3241 expected_align
, expected_size
,
3242 min_size
, max_size
, probable_max_size
);
3246 dest_addr
= force_operand (XEXP (dest_mem
, 0), target
);
3247 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3253 /* Expand a call EXP to the memcpy builtin.
3254 Return NULL_RTX if we failed, the caller should emit a normal call,
3255 otherwise try to get the result in TARGET, if convenient (and in
3256 mode MODE if that's convenient). */
3259 expand_builtin_memcpy (tree exp
, rtx target
)
3261 if (!validate_arglist (exp
,
3262 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3266 tree dest
= CALL_EXPR_ARG (exp
, 0);
3267 tree src
= CALL_EXPR_ARG (exp
, 1);
3268 tree len
= CALL_EXPR_ARG (exp
, 2);
3269 return expand_builtin_memcpy_args (dest
, src
, len
, target
, exp
);
3273 /* Expand an instrumented call EXP to the memcpy builtin.
3274 Return NULL_RTX if we failed, the caller should emit a normal call,
3275 otherwise try to get the result in TARGET, if convenient (and in
3276 mode MODE if that's convenient). */
3279 expand_builtin_memcpy_with_bounds (tree exp
, rtx target
)
3281 if (!validate_arglist (exp
,
3282 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3283 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3284 INTEGER_TYPE
, VOID_TYPE
))
3288 tree dest
= CALL_EXPR_ARG (exp
, 0);
3289 tree src
= CALL_EXPR_ARG (exp
, 2);
3290 tree len
= CALL_EXPR_ARG (exp
, 4);
3291 rtx res
= expand_builtin_memcpy_args (dest
, src
, len
, target
, exp
);
3293 /* Return src bounds with the result. */
3296 rtx bnd
= force_reg (targetm
.chkp_bound_mode (),
3297 expand_normal (CALL_EXPR_ARG (exp
, 1)));
3298 res
= chkp_join_splitted_slot (res
, bnd
);
3304 /* Expand a call EXP to the mempcpy builtin.
3305 Return NULL_RTX if we failed; the caller should emit a normal call,
3306 otherwise try to get the result in TARGET, if convenient (and in
3307 mode MODE if that's convenient). If ENDP is 0 return the
3308 destination pointer, if ENDP is 1 return the end pointer ala
3309 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3313 expand_builtin_mempcpy (tree exp
, rtx target
, machine_mode mode
)
3315 if (!validate_arglist (exp
,
3316 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3320 tree dest
= CALL_EXPR_ARG (exp
, 0);
3321 tree src
= CALL_EXPR_ARG (exp
, 1);
3322 tree len
= CALL_EXPR_ARG (exp
, 2);
3323 return expand_builtin_mempcpy_args (dest
, src
, len
,
3324 target
, mode
, /*endp=*/ 1,
3329 /* Expand an instrumented call EXP to the mempcpy builtin.
3330 Return NULL_RTX if we failed, the caller should emit a normal call,
3331 otherwise try to get the result in TARGET, if convenient (and in
3332 mode MODE if that's convenient). */
3335 expand_builtin_mempcpy_with_bounds (tree exp
, rtx target
, machine_mode mode
)
3337 if (!validate_arglist (exp
,
3338 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3339 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3340 INTEGER_TYPE
, VOID_TYPE
))
3344 tree dest
= CALL_EXPR_ARG (exp
, 0);
3345 tree src
= CALL_EXPR_ARG (exp
, 2);
3346 tree len
= CALL_EXPR_ARG (exp
, 4);
3347 rtx res
= expand_builtin_mempcpy_args (dest
, src
, len
, target
,
3350 /* Return src bounds with the result. */
3353 rtx bnd
= force_reg (targetm
.chkp_bound_mode (),
3354 expand_normal (CALL_EXPR_ARG (exp
, 1)));
3355 res
= chkp_join_splitted_slot (res
, bnd
);
3361 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3362 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3363 so that this can also be called without constructing an actual CALL_EXPR.
3364 The other arguments and return value are the same as for
3365 expand_builtin_mempcpy. */
3368 expand_builtin_mempcpy_args (tree dest
, tree src
, tree len
,
3369 rtx target
, machine_mode mode
, int endp
,
3372 tree fndecl
= get_callee_fndecl (orig_exp
);
3374 /* If return value is ignored, transform mempcpy into memcpy. */
3375 if (target
== const0_rtx
3376 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
3377 && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP
))
3379 tree fn
= builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP
);
3380 tree result
= build_call_nofold_loc (UNKNOWN_LOCATION
, fn
, 3,
3382 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3384 else if (target
== const0_rtx
3385 && builtin_decl_implicit_p (BUILT_IN_MEMCPY
))
3387 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
3388 tree result
= build_call_nofold_loc (UNKNOWN_LOCATION
, fn
, 3,
3390 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3394 const char *src_str
;
3395 unsigned int src_align
= get_pointer_alignment (src
);
3396 unsigned int dest_align
= get_pointer_alignment (dest
);
3397 rtx dest_mem
, src_mem
, len_rtx
;
3399 /* If either SRC or DEST is not a pointer type, don't do this
3400 operation in-line. */
3401 if (dest_align
== 0 || src_align
== 0)
3404 /* If LEN is not constant, call the normal function. */
3405 if (! tree_fits_uhwi_p (len
))
3408 len_rtx
= expand_normal (len
);
3409 src_str
= c_getstr (src
);
3411 /* If SRC is a string constant and block move would be done
3412 by pieces, we can avoid loading the string from memory
3413 and only stored the computed constants. */
3415 && CONST_INT_P (len_rtx
)
3416 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3417 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3418 CONST_CAST (char *, src_str
),
3421 dest_mem
= get_memory_rtx (dest
, len
);
3422 set_mem_align (dest_mem
, dest_align
);
3423 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3424 builtin_memcpy_read_str
,
3425 CONST_CAST (char *, src_str
),
3426 dest_align
, false, endp
);
3427 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3428 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3432 if (CONST_INT_P (len_rtx
)
3433 && can_move_by_pieces (INTVAL (len_rtx
),
3434 MIN (dest_align
, src_align
)))
3436 dest_mem
= get_memory_rtx (dest
, len
);
3437 set_mem_align (dest_mem
, dest_align
);
3438 src_mem
= get_memory_rtx (src
, len
);
3439 set_mem_align (src_mem
, src_align
);
3440 dest_mem
= move_by_pieces (dest_mem
, src_mem
, INTVAL (len_rtx
),
3441 MIN (dest_align
, src_align
), endp
);
3442 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3443 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3452 # define HAVE_movstr 0
3453 # define CODE_FOR_movstr CODE_FOR_nothing
3456 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3457 we failed, the caller should emit a normal call, otherwise try to
3458 get the result in TARGET, if convenient. If ENDP is 0 return the
3459 destination pointer, if ENDP is 1 return the end pointer ala
3460 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3464 expand_movstr (tree dest
, tree src
, rtx target
, int endp
)
3466 struct expand_operand ops
[3];
3473 dest_mem
= get_memory_rtx (dest
, NULL
);
3474 src_mem
= get_memory_rtx (src
, NULL
);
3477 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
3478 dest_mem
= replace_equiv_address (dest_mem
, target
);
3481 create_output_operand (&ops
[0], endp
? target
: NULL_RTX
, Pmode
);
3482 create_fixed_operand (&ops
[1], dest_mem
);
3483 create_fixed_operand (&ops
[2], src_mem
);
3484 if (!maybe_expand_insn (CODE_FOR_movstr
, 3, ops
))
3487 if (endp
&& target
!= const0_rtx
)
3489 target
= ops
[0].value
;
3490 /* movstr is supposed to set end to the address of the NUL
3491 terminator. If the caller requested a mempcpy-like return value,
3495 rtx tem
= plus_constant (GET_MODE (target
),
3496 gen_lowpart (GET_MODE (target
), target
), 1);
3497 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
3503 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3504 NULL_RTX if we failed the caller should emit a normal call, otherwise
3505 try to get the result in TARGET, if convenient (and in mode MODE if that's
3509 expand_builtin_strcpy (tree exp
, rtx target
)
3511 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3513 tree dest
= CALL_EXPR_ARG (exp
, 0);
3514 tree src
= CALL_EXPR_ARG (exp
, 1);
3515 return expand_builtin_strcpy_args (dest
, src
, target
);
3520 /* Helper function to do the actual work for expand_builtin_strcpy. The
3521 arguments to the builtin_strcpy call DEST and SRC are broken out
3522 so that this can also be called without constructing an actual CALL_EXPR.
3523 The other arguments and return value are the same as for
3524 expand_builtin_strcpy. */
3527 expand_builtin_strcpy_args (tree dest
, tree src
, rtx target
)
3529 return expand_movstr (dest
, src
, target
, /*endp=*/0);
3532 /* Expand a call EXP to the stpcpy builtin.
3533 Return NULL_RTX if we failed the caller should emit a normal call,
3534 otherwise try to get the result in TARGET, if convenient (and in
3535 mode MODE if that's convenient). */
3538 expand_builtin_stpcpy (tree exp
, rtx target
, machine_mode mode
)
3541 location_t loc
= EXPR_LOCATION (exp
);
3543 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3546 dst
= CALL_EXPR_ARG (exp
, 0);
3547 src
= CALL_EXPR_ARG (exp
, 1);
3549 /* If return value is ignored, transform stpcpy into strcpy. */
3550 if (target
== const0_rtx
&& builtin_decl_implicit (BUILT_IN_STRCPY
))
3552 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3553 tree result
= build_call_nofold_loc (loc
, fn
, 2, dst
, src
);
3554 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3561 /* Ensure we get an actual string whose length can be evaluated at
3562 compile-time, not an expression containing a string. This is
3563 because the latter will potentially produce pessimized code
3564 when used to produce the return value. */
3565 if (! c_getstr (src
) || ! (len
= c_strlen (src
, 0)))
3566 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3568 lenp1
= size_binop_loc (loc
, PLUS_EXPR
, len
, ssize_int (1));
3569 ret
= expand_builtin_mempcpy_args (dst
, src
, lenp1
,
3570 target
, mode
, /*endp=*/2,
3576 if (TREE_CODE (len
) == INTEGER_CST
)
3578 rtx len_rtx
= expand_normal (len
);
3580 if (CONST_INT_P (len_rtx
))
3582 ret
= expand_builtin_strcpy_args (dst
, src
, target
);
3588 if (mode
!= VOIDmode
)
3589 target
= gen_reg_rtx (mode
);
3591 target
= gen_reg_rtx (GET_MODE (ret
));
3593 if (GET_MODE (target
) != GET_MODE (ret
))
3594 ret
= gen_lowpart (GET_MODE (target
), ret
);
3596 ret
= plus_constant (GET_MODE (ret
), ret
, INTVAL (len_rtx
));
3597 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
3605 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3609 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3610 bytes from constant string DATA + OFFSET and return it as target
3614 builtin_strncpy_read_str (void *data
, HOST_WIDE_INT offset
,
3617 const char *str
= (const char *) data
;
3619 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
3622 return c_readstr (str
+ offset
, mode
);
3625 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3626 NULL_RTX if we failed the caller should emit a normal call. */
3629 expand_builtin_strncpy (tree exp
, rtx target
)
3631 location_t loc
= EXPR_LOCATION (exp
);
3633 if (validate_arglist (exp
,
3634 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3636 tree dest
= CALL_EXPR_ARG (exp
, 0);
3637 tree src
= CALL_EXPR_ARG (exp
, 1);
3638 tree len
= CALL_EXPR_ARG (exp
, 2);
3639 tree slen
= c_strlen (src
, 1);
3641 /* We must be passed a constant len and src parameter. */
3642 if (!tree_fits_uhwi_p (len
) || !slen
|| !tree_fits_uhwi_p (slen
))
3645 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
3647 /* We're required to pad with trailing zeros if the requested
3648 len is greater than strlen(s2)+1. In that case try to
3649 use store_by_pieces, if it fails, punt. */
3650 if (tree_int_cst_lt (slen
, len
))
3652 unsigned int dest_align
= get_pointer_alignment (dest
);
3653 const char *p
= c_getstr (src
);
3656 if (!p
|| dest_align
== 0 || !tree_fits_uhwi_p (len
)
3657 || !can_store_by_pieces (tree_to_uhwi (len
),
3658 builtin_strncpy_read_str
,
3659 CONST_CAST (char *, p
),
3663 dest_mem
= get_memory_rtx (dest
, len
);
3664 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3665 builtin_strncpy_read_str
,
3666 CONST_CAST (char *, p
), dest_align
, false, 0);
3667 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3668 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3675 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3676 bytes from constant string DATA + OFFSET and return it as target
3680 builtin_memset_read_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3683 const char *c
= (const char *) data
;
3684 char *p
= XALLOCAVEC (char, GET_MODE_SIZE (mode
));
3686 memset (p
, *c
, GET_MODE_SIZE (mode
));
3688 return c_readstr (p
, mode
);
3691 /* Callback routine for store_by_pieces. Return the RTL of a register
3692 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3693 char value given in the RTL register data. For example, if mode is
3694 4 bytes wide, return the RTL for 0x01010101*data. */
3697 builtin_memset_gen_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3704 size
= GET_MODE_SIZE (mode
);
3708 p
= XALLOCAVEC (char, size
);
3709 memset (p
, 1, size
);
3710 coeff
= c_readstr (p
, mode
);
3712 target
= convert_to_mode (mode
, (rtx
) data
, 1);
3713 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
3714 return force_reg (mode
, target
);
3717 /* Expand expression EXP, which is a call to the memset builtin. Return
3718 NULL_RTX if we failed the caller should emit a normal call, otherwise
3719 try to get the result in TARGET, if convenient (and in mode MODE if that's
3723 expand_builtin_memset (tree exp
, rtx target
, machine_mode mode
)
3725 if (!validate_arglist (exp
,
3726 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3730 tree dest
= CALL_EXPR_ARG (exp
, 0);
3731 tree val
= CALL_EXPR_ARG (exp
, 1);
3732 tree len
= CALL_EXPR_ARG (exp
, 2);
3733 return expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
3737 /* Expand expression EXP, which is an instrumented call to the memset builtin.
3738 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
3739 try to get the result in TARGET, if convenient (and in mode MODE if that's
3743 expand_builtin_memset_with_bounds (tree exp
, rtx target
, machine_mode mode
)
3745 if (!validate_arglist (exp
,
3746 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3747 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3751 tree dest
= CALL_EXPR_ARG (exp
, 0);
3752 tree val
= CALL_EXPR_ARG (exp
, 2);
3753 tree len
= CALL_EXPR_ARG (exp
, 3);
3754 rtx res
= expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
3756 /* Return src bounds with the result. */
3759 rtx bnd
= force_reg (targetm
.chkp_bound_mode (),
3760 expand_normal (CALL_EXPR_ARG (exp
, 1)));
3761 res
= chkp_join_splitted_slot (res
, bnd
);
3767 /* Helper function to do the actual work for expand_builtin_memset. The
3768 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3769 so that this can also be called without constructing an actual CALL_EXPR.
3770 The other arguments and return value are the same as for
3771 expand_builtin_memset. */
3774 expand_builtin_memset_args (tree dest
, tree val
, tree len
,
3775 rtx target
, machine_mode mode
, tree orig_exp
)
3778 enum built_in_function fcode
;
3779 machine_mode val_mode
;
3781 unsigned int dest_align
;
3782 rtx dest_mem
, dest_addr
, len_rtx
;
3783 HOST_WIDE_INT expected_size
= -1;
3784 unsigned int expected_align
= 0;
3785 unsigned HOST_WIDE_INT min_size
;
3786 unsigned HOST_WIDE_INT max_size
;
3787 unsigned HOST_WIDE_INT probable_max_size
;
3789 dest_align
= get_pointer_alignment (dest
);
3791 /* If DEST is not a pointer type, don't do this operation in-line. */
3792 if (dest_align
== 0)
3795 if (currently_expanding_gimple_stmt
)
3796 stringop_block_profile (currently_expanding_gimple_stmt
,
3797 &expected_align
, &expected_size
);
3799 if (expected_align
< dest_align
)
3800 expected_align
= dest_align
;
3802 /* If the LEN parameter is zero, return DEST. */
3803 if (integer_zerop (len
))
3805 /* Evaluate and ignore VAL in case it has side-effects. */
3806 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3807 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
3810 /* Stabilize the arguments in case we fail. */
3811 dest
= builtin_save_expr (dest
);
3812 val
= builtin_save_expr (val
);
3813 len
= builtin_save_expr (len
);
3815 len_rtx
= expand_normal (len
);
3816 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3817 &probable_max_size
);
3818 dest_mem
= get_memory_rtx (dest
, len
);
3819 val_mode
= TYPE_MODE (unsigned_char_type_node
);
3821 if (TREE_CODE (val
) != INTEGER_CST
)
3825 val_rtx
= expand_normal (val
);
3826 val_rtx
= convert_to_mode (val_mode
, val_rtx
, 0);
3828 /* Assume that we can memset by pieces if we can store
3829 * the coefficients by pieces (in the required modes).
3830 * We can't pass builtin_memset_gen_str as that emits RTL. */
3832 if (tree_fits_uhwi_p (len
)
3833 && can_store_by_pieces (tree_to_uhwi (len
),
3834 builtin_memset_read_str
, &c
, dest_align
,
3837 val_rtx
= force_reg (val_mode
, val_rtx
);
3838 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3839 builtin_memset_gen_str
, val_rtx
, dest_align
,
3842 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, val_rtx
,
3843 dest_align
, expected_align
,
3844 expected_size
, min_size
, max_size
,
3848 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3849 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3853 if (target_char_cast (val
, &c
))
3858 if (tree_fits_uhwi_p (len
)
3859 && can_store_by_pieces (tree_to_uhwi (len
),
3860 builtin_memset_read_str
, &c
, dest_align
,
3862 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3863 builtin_memset_read_str
, &c
, dest_align
, true, 0);
3864 else if (!set_storage_via_setmem (dest_mem
, len_rtx
,
3865 gen_int_mode (c
, val_mode
),
3866 dest_align
, expected_align
,
3867 expected_size
, min_size
, max_size
,
3871 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3872 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3876 set_mem_align (dest_mem
, dest_align
);
3877 dest_addr
= clear_storage_hints (dest_mem
, len_rtx
,
3878 CALL_EXPR_TAILCALL (orig_exp
)
3879 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3880 expected_align
, expected_size
,
3886 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3887 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3893 fndecl
= get_callee_fndecl (orig_exp
);
3894 fcode
= DECL_FUNCTION_CODE (fndecl
);
3895 if (fcode
== BUILT_IN_MEMSET
3896 || fcode
== BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP
)
3897 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 3,
3899 else if (fcode
== BUILT_IN_BZERO
)
3900 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 2,
3904 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
3905 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
3906 return expand_call (fn
, target
, target
== const0_rtx
);
3909 /* Expand expression EXP, which is a call to the bzero builtin. Return
3910 NULL_RTX if we failed the caller should emit a normal call. */
3913 expand_builtin_bzero (tree exp
)
3916 location_t loc
= EXPR_LOCATION (exp
);
3918 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3921 dest
= CALL_EXPR_ARG (exp
, 0);
3922 size
= CALL_EXPR_ARG (exp
, 1);
3924 /* New argument list transforming bzero(ptr x, int y) to
3925 memset(ptr x, int 0, size_t y). This is done this way
3926 so that if it isn't expanded inline, we fallback to
3927 calling bzero instead of memset. */
3929 return expand_builtin_memset_args (dest
, integer_zero_node
,
3930 fold_convert_loc (loc
,
3931 size_type_node
, size
),
3932 const0_rtx
, VOIDmode
, exp
);
3935 /* Expand expression EXP, which is a call to the memcmp built-in function.
3936 Return NULL_RTX if we failed and the caller should emit a normal call,
3937 otherwise try to get the result in TARGET, if convenient (and in mode
3938 MODE, if that's convenient). */
3941 expand_builtin_memcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
3942 ATTRIBUTE_UNUSED machine_mode mode
)
3944 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
3946 if (!validate_arglist (exp
,
3947 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3950 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3951 implementing memcmp because it will stop if it encounters two
3953 #if defined HAVE_cmpmemsi
3955 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3958 tree arg1
= CALL_EXPR_ARG (exp
, 0);
3959 tree arg2
= CALL_EXPR_ARG (exp
, 1);
3960 tree len
= CALL_EXPR_ARG (exp
, 2);
3962 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
3963 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
3964 machine_mode insn_mode
;
3967 insn_mode
= insn_data
[(int) CODE_FOR_cmpmemsi
].operand
[0].mode
;
3971 /* If we don't have POINTER_TYPE, call the function. */
3972 if (arg1_align
== 0 || arg2_align
== 0)
3975 /* Make a place to write the result of the instruction. */
3978 && REG_P (result
) && GET_MODE (result
) == insn_mode
3979 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3980 result
= gen_reg_rtx (insn_mode
);
3982 arg1_rtx
= get_memory_rtx (arg1
, len
);
3983 arg2_rtx
= get_memory_rtx (arg2
, len
);
3984 arg3_rtx
= expand_normal (fold_convert_loc (loc
, sizetype
, len
));
3986 /* Set MEM_SIZE as appropriate. */
3987 if (CONST_INT_P (arg3_rtx
))
3989 set_mem_size (arg1_rtx
, INTVAL (arg3_rtx
));
3990 set_mem_size (arg2_rtx
, INTVAL (arg3_rtx
));
3994 insn
= gen_cmpmemsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3995 GEN_INT (MIN (arg1_align
, arg2_align
)));
4002 emit_library_call_value (memcmp_libfunc
, result
, LCT_PURE
,
4003 TYPE_MODE (integer_type_node
), 3,
4004 XEXP (arg1_rtx
, 0), Pmode
,
4005 XEXP (arg2_rtx
, 0), Pmode
,
4006 convert_to_mode (TYPE_MODE (sizetype
), arg3_rtx
,
4007 TYPE_UNSIGNED (sizetype
)),
4008 TYPE_MODE (sizetype
));
4010 /* Return the value in the proper mode for this function. */
4011 mode
= TYPE_MODE (TREE_TYPE (exp
));
4012 if (GET_MODE (result
) == mode
)
4014 else if (target
!= 0)
4016 convert_move (target
, result
, 0);
4020 return convert_to_mode (mode
, result
, 0);
4022 #endif /* HAVE_cmpmemsi. */
4027 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4028 if we failed the caller should emit a normal call, otherwise try to get
4029 the result in TARGET, if convenient. */
4032 expand_builtin_strcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
)
4034 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4037 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4038 if (direct_optab_handler (cmpstr_optab
, SImode
) != CODE_FOR_nothing
4039 || direct_optab_handler (cmpstrn_optab
, SImode
) != CODE_FOR_nothing
)
4041 rtx arg1_rtx
, arg2_rtx
;
4042 rtx result
, insn
= NULL_RTX
;
4044 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4045 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4047 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4048 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4050 /* If we don't have POINTER_TYPE, call the function. */
4051 if (arg1_align
== 0 || arg2_align
== 0)
4054 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4055 arg1
= builtin_save_expr (arg1
);
4056 arg2
= builtin_save_expr (arg2
);
4058 arg1_rtx
= get_memory_rtx (arg1
, NULL
);
4059 arg2_rtx
= get_memory_rtx (arg2
, NULL
);
4061 #ifdef HAVE_cmpstrsi
4062 /* Try to call cmpstrsi. */
4065 machine_mode insn_mode
4066 = insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
4068 /* Make a place to write the result of the instruction. */
4071 && REG_P (result
) && GET_MODE (result
) == insn_mode
4072 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
4073 result
= gen_reg_rtx (insn_mode
);
4075 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
,
4076 GEN_INT (MIN (arg1_align
, arg2_align
)));
4079 #ifdef HAVE_cmpstrnsi
4080 /* Try to determine at least one length and call cmpstrnsi. */
4081 if (!insn
&& HAVE_cmpstrnsi
)
4086 machine_mode insn_mode
4087 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
4088 tree len1
= c_strlen (arg1
, 1);
4089 tree len2
= c_strlen (arg2
, 1);
4092 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
4094 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
4096 /* If we don't have a constant length for the first, use the length
4097 of the second, if we know it. We don't require a constant for
4098 this case; some cost analysis could be done if both are available
4099 but neither is constant. For now, assume they're equally cheap,
4100 unless one has side effects. If both strings have constant lengths,
4107 else if (TREE_SIDE_EFFECTS (len1
))
4109 else if (TREE_SIDE_EFFECTS (len2
))
4111 else if (TREE_CODE (len1
) != INTEGER_CST
)
4113 else if (TREE_CODE (len2
) != INTEGER_CST
)
4115 else if (tree_int_cst_lt (len1
, len2
))
4120 /* If both arguments have side effects, we cannot optimize. */
4121 if (!len
|| TREE_SIDE_EFFECTS (len
))
4124 arg3_rtx
= expand_normal (len
);
4126 /* Make a place to write the result of the instruction. */
4129 && REG_P (result
) && GET_MODE (result
) == insn_mode
4130 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
4131 result
= gen_reg_rtx (insn_mode
);
4133 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
4134 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 #ifdef HAVE_cmpstrnsi
4158 fndecl
= get_callee_fndecl (exp
);
4159 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 2, arg1
, arg2
);
4160 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4161 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4162 return expand_call (fn
, target
, target
== const0_rtx
);
4168 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4169 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4170 the result in TARGET, if convenient. */
4173 expand_builtin_strncmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
4174 ATTRIBUTE_UNUSED machine_mode mode
)
4176 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
4178 if (!validate_arglist (exp
,
4179 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4182 /* If c_strlen can determine an expression for one of the string
4183 lengths, and it doesn't have side effects, then emit cmpstrnsi
4184 using length MIN(strlen(string)+1, arg3). */
4185 #ifdef HAVE_cmpstrnsi
4188 tree len
, len1
, len2
;
4189 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
4192 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4193 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4194 tree arg3
= CALL_EXPR_ARG (exp
, 2);
4196 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4197 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4198 machine_mode insn_mode
4199 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
4201 len1
= c_strlen (arg1
, 1);
4202 len2
= c_strlen (arg2
, 1);
4205 len1
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len1
);
4207 len2
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len2
);
4209 /* If we don't have a constant length for the first, use the length
4210 of the second, if we know it. We don't require a constant for
4211 this case; some cost analysis could be done if both are available
4212 but neither is constant. For now, assume they're equally cheap,
4213 unless one has side effects. If both strings have constant lengths,
4220 else if (TREE_SIDE_EFFECTS (len1
))
4222 else if (TREE_SIDE_EFFECTS (len2
))
4224 else if (TREE_CODE (len1
) != INTEGER_CST
)
4226 else if (TREE_CODE (len2
) != INTEGER_CST
)
4228 else if (tree_int_cst_lt (len1
, len2
))
4233 /* If both arguments have side effects, we cannot optimize. */
4234 if (!len
|| TREE_SIDE_EFFECTS (len
))
4237 /* The actual new length parameter is MIN(len,arg3). */
4238 len
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (len
), len
,
4239 fold_convert_loc (loc
, TREE_TYPE (len
), arg3
));
4241 /* If we don't have POINTER_TYPE, call the function. */
4242 if (arg1_align
== 0 || arg2_align
== 0)
4245 /* Make a place to write the result of the instruction. */
4248 && REG_P (result
) && GET_MODE (result
) == insn_mode
4249 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
4250 result
= gen_reg_rtx (insn_mode
);
4252 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4253 arg1
= builtin_save_expr (arg1
);
4254 arg2
= builtin_save_expr (arg2
);
4255 len
= builtin_save_expr (len
);
4257 arg1_rtx
= get_memory_rtx (arg1
, len
);
4258 arg2_rtx
= get_memory_rtx (arg2
, len
);
4259 arg3_rtx
= expand_normal (len
);
4260 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
4261 GEN_INT (MIN (arg1_align
, arg2_align
)));
4266 /* Return the value in the proper mode for this function. */
4267 mode
= TYPE_MODE (TREE_TYPE (exp
));
4268 if (GET_MODE (result
) == mode
)
4271 return convert_to_mode (mode
, result
, 0);
4272 convert_move (target
, result
, 0);
4276 /* Expand the library call ourselves using a stabilized argument
4277 list to avoid re-evaluating the function's arguments twice. */
4278 fndecl
= get_callee_fndecl (exp
);
4279 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 3,
4281 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4282 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4283 return expand_call (fn
, target
, target
== const0_rtx
);
4289 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4290 if that's convenient. */
4293 expand_builtin_saveregs (void)
4298 /* Don't do __builtin_saveregs more than once in a function.
4299 Save the result of the first call and reuse it. */
4300 if (saveregs_value
!= 0)
4301 return saveregs_value
;
4303 /* When this function is called, it means that registers must be
4304 saved on entry to this function. So we migrate the call to the
4305 first insn of this function. */
4309 /* Do whatever the machine needs done in this case. */
4310 val
= targetm
.calls
.expand_builtin_saveregs ();
4315 saveregs_value
= val
;
4317 /* Put the insns after the NOTE that starts the function. If this
4318 is inside a start_sequence, make the outer-level insn chain current, so
4319 the code is placed at the start of the function. */
4320 push_topmost_sequence ();
4321 emit_insn_after (seq
, entry_of_function ());
4322 pop_topmost_sequence ();
4327 /* Expand a call to __builtin_next_arg. */
4330 expand_builtin_next_arg (void)
4332 /* Checking arguments is already done in fold_builtin_next_arg
4333 that must be called before this function. */
4334 return expand_binop (ptr_mode
, add_optab
,
4335 crtl
->args
.internal_arg_pointer
,
4336 crtl
->args
.arg_offset_rtx
,
4337 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
4340 /* Make it easier for the backends by protecting the valist argument
4341 from multiple evaluations. */
4344 stabilize_va_list_loc (location_t loc
, tree valist
, int needs_lvalue
)
4346 tree vatype
= targetm
.canonical_va_list_type (TREE_TYPE (valist
));
4348 /* The current way of determining the type of valist is completely
4349 bogus. We should have the information on the va builtin instead. */
4351 vatype
= targetm
.fn_abi_va_list (cfun
->decl
);
4353 if (TREE_CODE (vatype
) == ARRAY_TYPE
)
4355 if (TREE_SIDE_EFFECTS (valist
))
4356 valist
= save_expr (valist
);
4358 /* For this case, the backends will be expecting a pointer to
4359 vatype, but it's possible we've actually been given an array
4360 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4362 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4364 tree p1
= build_pointer_type (TREE_TYPE (vatype
));
4365 valist
= build_fold_addr_expr_with_type_loc (loc
, valist
, p1
);
4370 tree pt
= build_pointer_type (vatype
);
4374 if (! TREE_SIDE_EFFECTS (valist
))
4377 valist
= fold_build1_loc (loc
, ADDR_EXPR
, pt
, valist
);
4378 TREE_SIDE_EFFECTS (valist
) = 1;
4381 if (TREE_SIDE_EFFECTS (valist
))
4382 valist
= save_expr (valist
);
4383 valist
= fold_build2_loc (loc
, MEM_REF
,
4384 vatype
, valist
, build_int_cst (pt
, 0));
4390 /* The "standard" definition of va_list is void*. */
4393 std_build_builtin_va_list (void)
4395 return ptr_type_node
;
4398 /* The "standard" abi va_list is va_list_type_node. */
4401 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED
)
4403 return va_list_type_node
;
4406 /* The "standard" type of va_list is va_list_type_node. */
4409 std_canonical_va_list_type (tree type
)
4413 if (INDIRECT_REF_P (type
))
4414 type
= TREE_TYPE (type
);
4415 else if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (TREE_TYPE (type
)))
4416 type
= TREE_TYPE (type
);
4417 wtype
= va_list_type_node
;
4419 /* Treat structure va_list types. */
4420 if (TREE_CODE (wtype
) == RECORD_TYPE
&& POINTER_TYPE_P (htype
))
4421 htype
= TREE_TYPE (htype
);
4422 else if (TREE_CODE (wtype
) == ARRAY_TYPE
)
4424 /* If va_list is an array type, the argument may have decayed
4425 to a pointer type, e.g. by being passed to another function.
4426 In that case, unwrap both types so that we can compare the
4427 underlying records. */
4428 if (TREE_CODE (htype
) == ARRAY_TYPE
4429 || POINTER_TYPE_P (htype
))
4431 wtype
= TREE_TYPE (wtype
);
4432 htype
= TREE_TYPE (htype
);
4435 if (TYPE_MAIN_VARIANT (wtype
) == TYPE_MAIN_VARIANT (htype
))
4436 return va_list_type_node
;
4441 /* The "standard" implementation of va_start: just assign `nextarg' to
4445 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
4447 rtx va_r
= expand_expr (valist
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
4448 convert_move (va_r
, nextarg
, 0);
4450 /* We do not have any valid bounds for the pointer, so
4451 just store zero bounds for it. */
4452 if (chkp_function_instrumented_p (current_function_decl
))
4453 chkp_expand_bounds_reset_for_mem (valist
,
4454 make_tree (TREE_TYPE (valist
),
4458 /* Expand EXP, a call to __builtin_va_start. */
4461 expand_builtin_va_start (tree exp
)
4465 location_t loc
= EXPR_LOCATION (exp
);
4467 if (call_expr_nargs (exp
) < 2)
4469 error_at (loc
, "too few arguments to function %<va_start%>");
4473 if (fold_builtin_next_arg (exp
, true))
4476 nextarg
= expand_builtin_next_arg ();
4477 valist
= stabilize_va_list_loc (loc
, CALL_EXPR_ARG (exp
, 0), 1);
4479 if (targetm
.expand_builtin_va_start
)
4480 targetm
.expand_builtin_va_start (valist
, nextarg
);
4482 std_expand_builtin_va_start (valist
, nextarg
);
4487 /* Expand EXP, a call to __builtin_va_end. */
4490 expand_builtin_va_end (tree exp
)
4492 tree valist
= CALL_EXPR_ARG (exp
, 0);
4494 /* Evaluate for side effects, if needed. I hate macros that don't
4496 if (TREE_SIDE_EFFECTS (valist
))
4497 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4502 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4503 builtin rather than just as an assignment in stdarg.h because of the
4504 nastiness of array-type va_list types. */
4507 expand_builtin_va_copy (tree exp
)
4510 location_t loc
= EXPR_LOCATION (exp
);
4512 dst
= CALL_EXPR_ARG (exp
, 0);
4513 src
= CALL_EXPR_ARG (exp
, 1);
4515 dst
= stabilize_va_list_loc (loc
, dst
, 1);
4516 src
= stabilize_va_list_loc (loc
, src
, 0);
4518 gcc_assert (cfun
!= NULL
&& cfun
->decl
!= NULL_TREE
);
4520 if (TREE_CODE (targetm
.fn_abi_va_list (cfun
->decl
)) != ARRAY_TYPE
)
4522 t
= build2 (MODIFY_EXPR
, targetm
.fn_abi_va_list (cfun
->decl
), dst
, src
);
4523 TREE_SIDE_EFFECTS (t
) = 1;
4524 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4528 rtx dstb
, srcb
, size
;
4530 /* Evaluate to pointers. */
4531 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4532 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4533 size
= expand_expr (TYPE_SIZE_UNIT (targetm
.fn_abi_va_list (cfun
->decl
)),
4534 NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
4536 dstb
= convert_memory_address (Pmode
, dstb
);
4537 srcb
= convert_memory_address (Pmode
, srcb
);
4539 /* "Dereference" to BLKmode memories. */
4540 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
4541 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
4542 set_mem_align (dstb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4543 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
4544 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
4545 set_mem_align (srcb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4548 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
4554 /* Expand a call to one of the builtin functions __builtin_frame_address or
4555 __builtin_return_address. */
4558 expand_builtin_frame_address (tree fndecl
, tree exp
)
4560 /* The argument must be a nonnegative integer constant.
4561 It counts the number of frames to scan up the stack.
4562 The value is the return address saved in that frame. */
4563 if (call_expr_nargs (exp
) == 0)
4564 /* Warning about missing arg was already issued. */
4566 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp
, 0)))
4568 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4569 error ("invalid argument to %<__builtin_frame_address%>");
4571 error ("invalid argument to %<__builtin_return_address%>");
4577 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
),
4578 tree_to_uhwi (CALL_EXPR_ARG (exp
, 0)));
4580 /* Some ports cannot access arbitrary stack frames. */
4583 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4584 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4586 warning (0, "unsupported argument to %<__builtin_return_address%>");
4590 /* For __builtin_frame_address, return what we've got. */
4591 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4595 && ! CONSTANT_P (tem
))
4596 tem
= copy_addr_to_reg (tem
);
4601 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4602 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4603 is the same as for allocate_dynamic_stack_space. */
4606 expand_builtin_alloca (tree exp
, bool cannot_accumulate
)
4612 bool alloca_with_align
= (DECL_FUNCTION_CODE (get_callee_fndecl (exp
))
4613 == BUILT_IN_ALLOCA_WITH_ALIGN
);
4616 = (alloca_with_align
4617 ? validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
4618 : validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
));
4623 /* Compute the argument. */
4624 op0
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4626 /* Compute the alignment. */
4627 align
= (alloca_with_align
4628 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp
, 1))
4629 : BIGGEST_ALIGNMENT
);
4631 /* Allocate the desired space. */
4632 result
= allocate_dynamic_stack_space (op0
, 0, align
, cannot_accumulate
);
4633 result
= convert_memory_address (ptr_mode
, result
);
4638 /* Expand a call to bswap builtin in EXP.
4639 Return NULL_RTX if a normal call should be emitted rather than expanding the
4640 function in-line. If convenient, the result should be placed in TARGET.
4641 SUBTARGET may be used as the target for computing one of EXP's operands. */
4644 expand_builtin_bswap (machine_mode target_mode
, tree exp
, rtx target
,
4650 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4653 arg
= CALL_EXPR_ARG (exp
, 0);
4654 op0
= expand_expr (arg
,
4655 subtarget
&& GET_MODE (subtarget
) == target_mode
4656 ? subtarget
: NULL_RTX
,
4657 target_mode
, EXPAND_NORMAL
);
4658 if (GET_MODE (op0
) != target_mode
)
4659 op0
= convert_to_mode (target_mode
, op0
, 1);
4661 target
= expand_unop (target_mode
, bswap_optab
, op0
, target
, 1);
4663 gcc_assert (target
);
4665 return convert_to_mode (target_mode
, target
, 1);
4668 /* Expand a call to a unary builtin in EXP.
4669 Return NULL_RTX if a normal call should be emitted rather than expanding the
4670 function in-line. If convenient, the result should be placed in TARGET.
4671 SUBTARGET may be used as the target for computing one of EXP's operands. */
4674 expand_builtin_unop (machine_mode target_mode
, tree exp
, rtx target
,
4675 rtx subtarget
, optab op_optab
)
4679 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4682 /* Compute the argument. */
4683 op0
= expand_expr (CALL_EXPR_ARG (exp
, 0),
4685 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0)))
4686 == GET_MODE (subtarget
))) ? subtarget
: NULL_RTX
,
4687 VOIDmode
, EXPAND_NORMAL
);
4688 /* Compute op, into TARGET if possible.
4689 Set TARGET to wherever the result comes back. */
4690 target
= expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))),
4691 op_optab
, op0
, target
, op_optab
!= clrsb_optab
);
4692 gcc_assert (target
);
4694 return convert_to_mode (target_mode
, target
, 0);
4697 /* Expand a call to __builtin_expect. We just return our argument
4698 as the builtin_expect semantic should've been already executed by
4699 tree branch prediction pass. */
4702 expand_builtin_expect (tree exp
, rtx target
)
4706 if (call_expr_nargs (exp
) < 2)
4708 arg
= CALL_EXPR_ARG (exp
, 0);
4710 target
= expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4711 /* When guessing was done, the hints should be already stripped away. */
4712 gcc_assert (!flag_guess_branch_prob
4713 || optimize
== 0 || seen_error ());
4717 /* Expand a call to __builtin_assume_aligned. We just return our first
4718 argument as the builtin_assume_aligned semantic should've been already
4722 expand_builtin_assume_aligned (tree exp
, rtx target
)
4724 if (call_expr_nargs (exp
) < 2)
4726 target
= expand_expr (CALL_EXPR_ARG (exp
, 0), target
, VOIDmode
,
4728 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 1))
4729 && (call_expr_nargs (exp
) < 3
4730 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 2))));
4735 expand_builtin_trap (void)
4740 rtx insn
= emit_insn (gen_trap ());
4741 /* For trap insns when not accumulating outgoing args force
4742 REG_ARGS_SIZE note to prevent crossjumping of calls with
4743 different args sizes. */
4744 if (!ACCUMULATE_OUTGOING_ARGS
)
4745 add_reg_note (insn
, REG_ARGS_SIZE
, GEN_INT (stack_pointer_delta
));
4749 emit_library_call (abort_libfunc
, LCT_NORETURN
, VOIDmode
, 0);
4753 /* Expand a call to __builtin_unreachable. We do nothing except emit
4754 a barrier saying that control flow will not pass here.
4756 It is the responsibility of the program being compiled to ensure
4757 that control flow does never reach __builtin_unreachable. */
4759 expand_builtin_unreachable (void)
4764 /* Expand EXP, a call to fabs, fabsf or fabsl.
4765 Return NULL_RTX if a normal call should be emitted rather than expanding
4766 the function inline. If convenient, the result should be placed
4767 in TARGET. SUBTARGET may be used as the target for computing
4771 expand_builtin_fabs (tree exp
, rtx target
, rtx subtarget
)
4777 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4780 arg
= CALL_EXPR_ARG (exp
, 0);
4781 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
4782 mode
= TYPE_MODE (TREE_TYPE (arg
));
4783 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4784 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
4787 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4788 Return NULL is a normal call should be emitted rather than expanding the
4789 function inline. If convenient, the result should be placed in TARGET.
4790 SUBTARGET may be used as the target for computing the operand. */
4793 expand_builtin_copysign (tree exp
, rtx target
, rtx subtarget
)
4798 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
4801 arg
= CALL_EXPR_ARG (exp
, 0);
4802 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4804 arg
= CALL_EXPR_ARG (exp
, 1);
4805 op1
= expand_normal (arg
);
4807 return expand_copysign (op0
, op1
, target
);
4810 /* Expand a call to __builtin___clear_cache. */
4813 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED
)
4815 #ifndef HAVE_clear_cache
4816 #ifdef CLEAR_INSN_CACHE
4817 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4818 does something. Just do the default expansion to a call to
4822 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4823 does nothing. There is no need to call it. Do nothing. */
4825 #endif /* CLEAR_INSN_CACHE */
4827 /* We have a "clear_cache" insn, and it will handle everything. */
4829 rtx begin_rtx
, end_rtx
;
4831 /* We must not expand to a library call. If we did, any
4832 fallback library function in libgcc that might contain a call to
4833 __builtin___clear_cache() would recurse infinitely. */
4834 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4836 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4840 if (HAVE_clear_cache
)
4842 struct expand_operand ops
[2];
4844 begin
= CALL_EXPR_ARG (exp
, 0);
4845 begin_rtx
= expand_expr (begin
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4847 end
= CALL_EXPR_ARG (exp
, 1);
4848 end_rtx
= expand_expr (end
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4850 create_address_operand (&ops
[0], begin_rtx
);
4851 create_address_operand (&ops
[1], end_rtx
);
4852 if (maybe_expand_insn (CODE_FOR_clear_cache
, 2, ops
))
4856 #endif /* HAVE_clear_cache */
4859 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4862 round_trampoline_addr (rtx tramp
)
4864 rtx temp
, addend
, mask
;
4866 /* If we don't need too much alignment, we'll have been guaranteed
4867 proper alignment by get_trampoline_type. */
4868 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
4871 /* Round address up to desired boundary. */
4872 temp
= gen_reg_rtx (Pmode
);
4873 addend
= gen_int_mode (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1, Pmode
);
4874 mask
= gen_int_mode (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
, Pmode
);
4876 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
4877 temp
, 0, OPTAB_LIB_WIDEN
);
4878 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
4879 temp
, 0, OPTAB_LIB_WIDEN
);
4885 expand_builtin_init_trampoline (tree exp
, bool onstack
)
4887 tree t_tramp
, t_func
, t_chain
;
4888 rtx m_tramp
, r_tramp
, r_chain
, tmp
;
4890 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
,
4891 POINTER_TYPE
, VOID_TYPE
))
4894 t_tramp
= CALL_EXPR_ARG (exp
, 0);
4895 t_func
= CALL_EXPR_ARG (exp
, 1);
4896 t_chain
= CALL_EXPR_ARG (exp
, 2);
4898 r_tramp
= expand_normal (t_tramp
);
4899 m_tramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
4900 MEM_NOTRAP_P (m_tramp
) = 1;
4902 /* If ONSTACK, the TRAMP argument should be the address of a field
4903 within the local function's FRAME decl. Either way, let's see if
4904 we can fill in the MEM_ATTRs for this memory. */
4905 if (TREE_CODE (t_tramp
) == ADDR_EXPR
)
4906 set_mem_attributes (m_tramp
, TREE_OPERAND (t_tramp
, 0), true);
4908 /* Creator of a heap trampoline is responsible for making sure the
4909 address is aligned to at least STACK_BOUNDARY. Normally malloc
4910 will ensure this anyhow. */
4911 tmp
= round_trampoline_addr (r_tramp
);
4914 m_tramp
= change_address (m_tramp
, BLKmode
, tmp
);
4915 set_mem_align (m_tramp
, TRAMPOLINE_ALIGNMENT
);
4916 set_mem_size (m_tramp
, TRAMPOLINE_SIZE
);
4919 /* The FUNC argument should be the address of the nested function.
4920 Extract the actual function decl to pass to the hook. */
4921 gcc_assert (TREE_CODE (t_func
) == ADDR_EXPR
);
4922 t_func
= TREE_OPERAND (t_func
, 0);
4923 gcc_assert (TREE_CODE (t_func
) == FUNCTION_DECL
);
4925 r_chain
= expand_normal (t_chain
);
4927 /* Generate insns to initialize the trampoline. */
4928 targetm
.calls
.trampoline_init (m_tramp
, t_func
, r_chain
);
4932 trampolines_created
= 1;
4934 warning_at (DECL_SOURCE_LOCATION (t_func
), OPT_Wtrampolines
,
4935 "trampoline generated for nested function %qD", t_func
);
4942 expand_builtin_adjust_trampoline (tree exp
)
4946 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
4949 tramp
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4950 tramp
= round_trampoline_addr (tramp
);
4951 if (targetm
.calls
.trampoline_adjust_address
)
4952 tramp
= targetm
.calls
.trampoline_adjust_address (tramp
);
4957 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4958 function. The function first checks whether the back end provides
4959 an insn to implement signbit for the respective mode. If not, it
4960 checks whether the floating point format of the value is such that
4961 the sign bit can be extracted. If that is not the case, the
4962 function returns NULL_RTX to indicate that a normal call should be
4963 emitted rather than expanding the function in-line. EXP is the
4964 expression that is a call to the builtin function; if convenient,
4965 the result should be placed in TARGET. */
4967 expand_builtin_signbit (tree exp
, rtx target
)
4969 const struct real_format
*fmt
;
4970 machine_mode fmode
, imode
, rmode
;
4973 enum insn_code icode
;
4975 location_t loc
= EXPR_LOCATION (exp
);
4977 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4980 arg
= CALL_EXPR_ARG (exp
, 0);
4981 fmode
= TYPE_MODE (TREE_TYPE (arg
));
4982 rmode
= TYPE_MODE (TREE_TYPE (exp
));
4983 fmt
= REAL_MODE_FORMAT (fmode
);
4985 arg
= builtin_save_expr (arg
);
4987 /* Expand the argument yielding a RTX expression. */
4988 temp
= expand_normal (arg
);
4990 /* Check if the back end provides an insn that handles signbit for the
4992 icode
= optab_handler (signbit_optab
, fmode
);
4993 if (icode
!= CODE_FOR_nothing
)
4995 rtx_insn
*last
= get_last_insn ();
4996 target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
4997 if (maybe_emit_unop_insn (icode
, target
, temp
, UNKNOWN
))
4999 delete_insns_since (last
);
5002 /* For floating point formats without a sign bit, implement signbit
5004 bitpos
= fmt
->signbit_ro
;
5007 /* But we can't do this if the format supports signed zero. */
5008 if (fmt
->has_signed_zero
&& HONOR_SIGNED_ZEROS (fmode
))
5011 arg
= fold_build2_loc (loc
, LT_EXPR
, TREE_TYPE (exp
), arg
,
5012 build_real (TREE_TYPE (arg
), dconst0
));
5013 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
5016 if (GET_MODE_SIZE (fmode
) <= UNITS_PER_WORD
)
5018 imode
= int_mode_for_mode (fmode
);
5019 if (imode
== BLKmode
)
5021 temp
= gen_lowpart (imode
, temp
);
5026 /* Handle targets with different FP word orders. */
5027 if (FLOAT_WORDS_BIG_ENDIAN
)
5028 word
= (GET_MODE_BITSIZE (fmode
) - bitpos
) / BITS_PER_WORD
;
5030 word
= bitpos
/ BITS_PER_WORD
;
5031 temp
= operand_subword_force (temp
, word
, fmode
);
5032 bitpos
= bitpos
% BITS_PER_WORD
;
5035 /* Force the intermediate word_mode (or narrower) result into a
5036 register. This avoids attempting to create paradoxical SUBREGs
5037 of floating point modes below. */
5038 temp
= force_reg (imode
, temp
);
5040 /* If the bitpos is within the "result mode" lowpart, the operation
5041 can be implement with a single bitwise AND. Otherwise, we need
5042 a right shift and an AND. */
5044 if (bitpos
< GET_MODE_BITSIZE (rmode
))
5046 wide_int mask
= wi::set_bit_in_zero (bitpos
, GET_MODE_PRECISION (rmode
));
5048 if (GET_MODE_SIZE (imode
) > GET_MODE_SIZE (rmode
))
5049 temp
= gen_lowpart (rmode
, temp
);
5050 temp
= expand_binop (rmode
, and_optab
, temp
,
5051 immed_wide_int_const (mask
, rmode
),
5052 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5056 /* Perform a logical right shift to place the signbit in the least
5057 significant bit, then truncate the result to the desired mode
5058 and mask just this bit. */
5059 temp
= expand_shift (RSHIFT_EXPR
, imode
, temp
, bitpos
, NULL_RTX
, 1);
5060 temp
= gen_lowpart (rmode
, temp
);
5061 temp
= expand_binop (rmode
, and_optab
, temp
, const1_rtx
,
5062 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5068 /* Expand fork or exec calls. TARGET is the desired target of the
5069 call. EXP is the call. FN is the
5070 identificator of the actual function. IGNORE is nonzero if the
5071 value is to be ignored. */
5074 expand_builtin_fork_or_exec (tree fn
, tree exp
, rtx target
, int ignore
)
5079 /* If we are not profiling, just call the function. */
5080 if (!profile_arc_flag
)
5083 /* Otherwise call the wrapper. This should be equivalent for the rest of
5084 compiler, so the code does not diverge, and the wrapper may run the
5085 code necessary for keeping the profiling sane. */
5087 switch (DECL_FUNCTION_CODE (fn
))
5090 id
= get_identifier ("__gcov_fork");
5093 case BUILT_IN_EXECL
:
5094 id
= get_identifier ("__gcov_execl");
5097 case BUILT_IN_EXECV
:
5098 id
= get_identifier ("__gcov_execv");
5101 case BUILT_IN_EXECLP
:
5102 id
= get_identifier ("__gcov_execlp");
5105 case BUILT_IN_EXECLE
:
5106 id
= get_identifier ("__gcov_execle");
5109 case BUILT_IN_EXECVP
:
5110 id
= get_identifier ("__gcov_execvp");
5113 case BUILT_IN_EXECVE
:
5114 id
= get_identifier ("__gcov_execve");
5121 decl
= build_decl (DECL_SOURCE_LOCATION (fn
),
5122 FUNCTION_DECL
, id
, TREE_TYPE (fn
));
5123 DECL_EXTERNAL (decl
) = 1;
5124 TREE_PUBLIC (decl
) = 1;
5125 DECL_ARTIFICIAL (decl
) = 1;
5126 TREE_NOTHROW (decl
) = 1;
5127 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
5128 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
5129 call
= rewrite_call_expr (EXPR_LOCATION (exp
), exp
, 0, decl
, 0);
5130 return expand_call (call
, target
, ignore
);
5135 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5136 the pointer in these functions is void*, the tree optimizers may remove
5137 casts. The mode computed in expand_builtin isn't reliable either, due
5138 to __sync_bool_compare_and_swap.
5140 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5141 group of builtins. This gives us log2 of the mode size. */
5143 static inline machine_mode
5144 get_builtin_sync_mode (int fcode_diff
)
5146 /* The size is not negotiable, so ask not to get BLKmode in return
5147 if the target indicates that a smaller size would be better. */
5148 return mode_for_size (BITS_PER_UNIT
<< fcode_diff
, MODE_INT
, 0);
5151 /* Expand the memory expression LOC and return the appropriate memory operand
5152 for the builtin_sync operations. */
5155 get_builtin_sync_mem (tree loc
, machine_mode mode
)
5159 addr
= expand_expr (loc
, NULL_RTX
, ptr_mode
, EXPAND_SUM
);
5160 addr
= convert_memory_address (Pmode
, addr
);
5162 /* Note that we explicitly do not want any alias information for this
5163 memory, so that we kill all other live memories. Otherwise we don't
5164 satisfy the full barrier semantics of the intrinsic. */
5165 mem
= validize_mem (gen_rtx_MEM (mode
, addr
));
5167 /* The alignment needs to be at least according to that of the mode. */
5168 set_mem_align (mem
, MAX (GET_MODE_ALIGNMENT (mode
),
5169 get_pointer_alignment (loc
)));
5170 set_mem_alias_set (mem
, ALIAS_SET_MEMORY_BARRIER
);
5171 MEM_VOLATILE_P (mem
) = 1;
5176 /* Make sure an argument is in the right mode.
5177 EXP is the tree argument.
5178 MODE is the mode it should be in. */
5181 expand_expr_force_mode (tree exp
, machine_mode mode
)
5184 machine_mode old_mode
;
5186 val
= expand_expr (exp
, NULL_RTX
, mode
, EXPAND_NORMAL
);
5187 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5188 of CONST_INTs, where we know the old_mode only from the call argument. */
5190 old_mode
= GET_MODE (val
);
5191 if (old_mode
== VOIDmode
)
5192 old_mode
= TYPE_MODE (TREE_TYPE (exp
));
5193 val
= convert_modes (mode
, old_mode
, val
, 1);
5198 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5199 EXP is the CALL_EXPR. CODE is the rtx code
5200 that corresponds to the arithmetic or logical operation from the name;
5201 an exception here is that NOT actually means NAND. TARGET is an optional
5202 place for us to store the results; AFTER is true if this is the
5203 fetch_and_xxx form. */
5206 expand_builtin_sync_operation (machine_mode mode
, tree exp
,
5207 enum rtx_code code
, bool after
,
5211 location_t loc
= EXPR_LOCATION (exp
);
5213 if (code
== NOT
&& warn_sync_nand
)
5215 tree fndecl
= get_callee_fndecl (exp
);
5216 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5218 static bool warned_f_a_n
, warned_n_a_f
;
5222 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
5223 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
5224 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
5225 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
5226 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
5230 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N
);
5231 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5232 warned_f_a_n
= true;
5235 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
5236 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
5237 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
5238 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
5239 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
5243 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N
);
5244 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5245 warned_n_a_f
= true;
5253 /* Expand the operands. */
5254 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5255 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5257 return expand_atomic_fetch_op (target
, mem
, val
, code
, MEMMODEL_SEQ_CST
,
5261 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5262 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5263 true if this is the boolean form. TARGET is a place for us to store the
5264 results; this is NOT optional if IS_BOOL is true. */
5267 expand_builtin_compare_and_swap (machine_mode mode
, tree exp
,
5268 bool is_bool
, rtx target
)
5270 rtx old_val
, new_val
, mem
;
5273 /* Expand the operands. */
5274 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5275 old_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5276 new_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5278 pbool
= poval
= NULL
;
5279 if (target
!= const0_rtx
)
5286 if (!expand_atomic_compare_and_swap (pbool
, poval
, mem
, old_val
, new_val
,
5287 false, MEMMODEL_SEQ_CST
,
5294 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5295 general form is actually an atomic exchange, and some targets only
5296 support a reduced form with the second argument being a constant 1.
5297 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5301 expand_builtin_sync_lock_test_and_set (machine_mode mode
, tree exp
,
5306 /* Expand the operands. */
5307 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5308 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5310 return expand_sync_lock_test_and_set (target
, mem
, val
);
5313 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5316 expand_builtin_sync_lock_release (machine_mode mode
, tree exp
)
5320 /* Expand the operands. */
5321 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5323 expand_atomic_store (mem
, const0_rtx
, MEMMODEL_RELEASE
, true);
5326 /* Given an integer representing an ``enum memmodel'', verify its
5327 correctness and return the memory model enum. */
5329 static enum memmodel
5330 get_memmodel (tree exp
)
5333 unsigned HOST_WIDE_INT val
;
5335 /* If the parameter is not a constant, it's a run time value so we'll just
5336 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5337 if (TREE_CODE (exp
) != INTEGER_CST
)
5338 return MEMMODEL_SEQ_CST
;
5340 op
= expand_normal (exp
);
5343 if (targetm
.memmodel_check
)
5344 val
= targetm
.memmodel_check (val
);
5345 else if (val
& ~MEMMODEL_MASK
)
5347 warning (OPT_Winvalid_memory_model
,
5348 "Unknown architecture specifier in memory model to builtin.");
5349 return MEMMODEL_SEQ_CST
;
5352 if ((INTVAL (op
) & MEMMODEL_MASK
) >= MEMMODEL_LAST
)
5354 warning (OPT_Winvalid_memory_model
,
5355 "invalid memory model argument to builtin");
5356 return MEMMODEL_SEQ_CST
;
5359 return (enum memmodel
) val
;
5362 /* Expand the __atomic_exchange intrinsic:
5363 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5364 EXP is the CALL_EXPR.
5365 TARGET is an optional place for us to store the results. */
5368 expand_builtin_atomic_exchange (machine_mode mode
, tree exp
, rtx target
)
5371 enum memmodel model
;
5373 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5374 if ((model
& MEMMODEL_MASK
) == MEMMODEL_CONSUME
)
5376 error ("invalid memory model for %<__atomic_exchange%>");
5380 if (!flag_inline_atomics
)
5383 /* Expand the operands. */
5384 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5385 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5387 return expand_atomic_exchange (target
, mem
, val
, model
);
5390 /* Expand the __atomic_compare_exchange intrinsic:
5391 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5392 TYPE desired, BOOL weak,
5393 enum memmodel success,
5394 enum memmodel failure)
5395 EXP is the CALL_EXPR.
5396 TARGET is an optional place for us to store the results. */
5399 expand_builtin_atomic_compare_exchange (machine_mode mode
, tree exp
,
5402 rtx expect
, desired
, mem
, oldval
;
5403 rtx_code_label
*label
;
5404 enum memmodel success
, failure
;
5408 success
= get_memmodel (CALL_EXPR_ARG (exp
, 4));
5409 failure
= get_memmodel (CALL_EXPR_ARG (exp
, 5));
5411 if ((failure
& MEMMODEL_MASK
) == MEMMODEL_RELEASE
5412 || (failure
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5414 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5418 if (failure
> success
)
5420 error ("failure memory model cannot be stronger than success "
5421 "memory model for %<__atomic_compare_exchange%>");
5425 if (!flag_inline_atomics
)
5428 /* Expand the operands. */
5429 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5431 expect
= expand_normal (CALL_EXPR_ARG (exp
, 1));
5432 expect
= convert_memory_address (Pmode
, expect
);
5433 expect
= gen_rtx_MEM (mode
, expect
);
5434 desired
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5436 weak
= CALL_EXPR_ARG (exp
, 3);
5438 if (tree_fits_shwi_p (weak
) && tree_to_shwi (weak
) != 0)
5441 if (target
== const0_rtx
)
5444 /* Lest the rtl backend create a race condition with an imporoper store
5445 to memory, always create a new pseudo for OLDVAL. */
5448 if (!expand_atomic_compare_and_swap (&target
, &oldval
, mem
, expect
, desired
,
5449 is_weak
, success
, failure
))
5452 /* Conditionally store back to EXPECT, lest we create a race condition
5453 with an improper store to memory. */
5454 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5455 the normal case where EXPECT is totally private, i.e. a register. At
5456 which point the store can be unconditional. */
5457 label
= gen_label_rtx ();
5458 emit_cmp_and_jump_insns (target
, const0_rtx
, NE
, NULL
, VOIDmode
, 1, label
);
5459 emit_move_insn (expect
, oldval
);
5465 /* Expand the __atomic_load intrinsic:
5466 TYPE __atomic_load (TYPE *object, enum memmodel)
5467 EXP is the CALL_EXPR.
5468 TARGET is an optional place for us to store the results. */
5471 expand_builtin_atomic_load (machine_mode mode
, tree exp
, rtx target
)
5474 enum memmodel model
;
5476 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5477 if ((model
& MEMMODEL_MASK
) == MEMMODEL_RELEASE
5478 || (model
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5480 error ("invalid memory model for %<__atomic_load%>");
5484 if (!flag_inline_atomics
)
5487 /* Expand the operand. */
5488 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5490 return expand_atomic_load (target
, mem
, model
);
5494 /* Expand the __atomic_store intrinsic:
5495 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5496 EXP is the CALL_EXPR.
5497 TARGET is an optional place for us to store the results. */
5500 expand_builtin_atomic_store (machine_mode mode
, tree exp
)
5503 enum memmodel model
;
5505 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5506 if ((model
& MEMMODEL_MASK
) != MEMMODEL_RELAXED
5507 && (model
& MEMMODEL_MASK
) != MEMMODEL_SEQ_CST
5508 && (model
& MEMMODEL_MASK
) != MEMMODEL_RELEASE
)
5510 error ("invalid memory model for %<__atomic_store%>");
5514 if (!flag_inline_atomics
)
5517 /* Expand the operands. */
5518 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5519 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5521 return expand_atomic_store (mem
, val
, model
, false);
5524 /* Expand the __atomic_fetch_XXX intrinsic:
5525 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5526 EXP is the CALL_EXPR.
5527 TARGET is an optional place for us to store the results.
5528 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5529 FETCH_AFTER is true if returning the result of the operation.
5530 FETCH_AFTER is false if returning the value before the operation.
5531 IGNORE is true if the result is not used.
5532 EXT_CALL is the correct builtin for an external call if this cannot be
5533 resolved to an instruction sequence. */
5536 expand_builtin_atomic_fetch_op (machine_mode mode
, tree exp
, rtx target
,
5537 enum rtx_code code
, bool fetch_after
,
5538 bool ignore
, enum built_in_function ext_call
)
5541 enum memmodel model
;
5545 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5547 /* Expand the operands. */
5548 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5549 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5551 /* Only try generating instructions if inlining is turned on. */
5552 if (flag_inline_atomics
)
5554 ret
= expand_atomic_fetch_op (target
, mem
, val
, code
, model
, fetch_after
);
5559 /* Return if a different routine isn't needed for the library call. */
5560 if (ext_call
== BUILT_IN_NONE
)
5563 /* Change the call to the specified function. */
5564 fndecl
= get_callee_fndecl (exp
);
5565 addr
= CALL_EXPR_FN (exp
);
5568 gcc_assert (TREE_OPERAND (addr
, 0) == fndecl
);
5569 TREE_OPERAND (addr
, 0) = builtin_decl_explicit (ext_call
);
5571 /* Expand the call here so we can emit trailing code. */
5572 ret
= expand_call (exp
, target
, ignore
);
5574 /* Replace the original function just in case it matters. */
5575 TREE_OPERAND (addr
, 0) = fndecl
;
5577 /* Then issue the arithmetic correction to return the right result. */
5582 ret
= expand_simple_binop (mode
, AND
, ret
, val
, NULL_RTX
, true,
5584 ret
= expand_simple_unop (mode
, NOT
, ret
, target
, true);
5587 ret
= expand_simple_binop (mode
, code
, ret
, val
, target
, true,
5594 #ifndef HAVE_atomic_clear
5595 # define HAVE_atomic_clear 0
5596 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5599 /* Expand an atomic clear operation.
5600 void _atomic_clear (BOOL *obj, enum memmodel)
5601 EXP is the call expression. */
5604 expand_builtin_atomic_clear (tree exp
)
5608 enum memmodel model
;
5610 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5611 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5612 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5614 if ((model
& MEMMODEL_MASK
) == MEMMODEL_ACQUIRE
5615 || (model
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5617 error ("invalid memory model for %<__atomic_store%>");
5621 if (HAVE_atomic_clear
)
5623 emit_insn (gen_atomic_clear (mem
, model
));
5627 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5628 Failing that, a store is issued by __atomic_store. The only way this can
5629 fail is if the bool type is larger than a word size. Unlikely, but
5630 handle it anyway for completeness. Assume a single threaded model since
5631 there is no atomic support in this case, and no barriers are required. */
5632 ret
= expand_atomic_store (mem
, const0_rtx
, model
, true);
5634 emit_move_insn (mem
, const0_rtx
);
5638 /* Expand an atomic test_and_set operation.
5639 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5640 EXP is the call expression. */
5643 expand_builtin_atomic_test_and_set (tree exp
, rtx target
)
5646 enum memmodel model
;
5649 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5650 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5651 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5653 return expand_atomic_test_and_set (target
, mem
, model
);
5657 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5658 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5661 fold_builtin_atomic_always_lock_free (tree arg0
, tree arg1
)
5665 unsigned int mode_align
, type_align
;
5667 if (TREE_CODE (arg0
) != INTEGER_CST
)
5670 size
= INTVAL (expand_normal (arg0
)) * BITS_PER_UNIT
;
5671 mode
= mode_for_size (size
, MODE_INT
, 0);
5672 mode_align
= GET_MODE_ALIGNMENT (mode
);
5674 if (TREE_CODE (arg1
) == INTEGER_CST
&& INTVAL (expand_normal (arg1
)) == 0)
5675 type_align
= mode_align
;
5678 tree ttype
= TREE_TYPE (arg1
);
5680 /* This function is usually invoked and folded immediately by the front
5681 end before anything else has a chance to look at it. The pointer
5682 parameter at this point is usually cast to a void *, so check for that
5683 and look past the cast. */
5684 if (CONVERT_EXPR_P (arg1
) && POINTER_TYPE_P (ttype
)
5685 && VOID_TYPE_P (TREE_TYPE (ttype
)))
5686 arg1
= TREE_OPERAND (arg1
, 0);
5688 ttype
= TREE_TYPE (arg1
);
5689 gcc_assert (POINTER_TYPE_P (ttype
));
5691 /* Get the underlying type of the object. */
5692 ttype
= TREE_TYPE (ttype
);
5693 type_align
= TYPE_ALIGN (ttype
);
5696 /* If the object has smaller alignment, the the lock free routines cannot
5698 if (type_align
< mode_align
)
5699 return boolean_false_node
;
5701 /* Check if a compare_and_swap pattern exists for the mode which represents
5702 the required size. The pattern is not allowed to fail, so the existence
5703 of the pattern indicates support is present. */
5704 if (can_compare_and_swap_p (mode
, true))
5705 return boolean_true_node
;
5707 return boolean_false_node
;
5710 /* Return true if the parameters to call EXP represent an object which will
5711 always generate lock free instructions. The first argument represents the
5712 size of the object, and the second parameter is a pointer to the object
5713 itself. If NULL is passed for the object, then the result is based on
5714 typical alignment for an object of the specified size. Otherwise return
5718 expand_builtin_atomic_always_lock_free (tree exp
)
5721 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5722 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5724 if (TREE_CODE (arg0
) != INTEGER_CST
)
5726 error ("non-constant argument 1 to __atomic_always_lock_free");
5730 size
= fold_builtin_atomic_always_lock_free (arg0
, arg1
);
5731 if (size
== boolean_true_node
)
5736 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5737 is lock free on this architecture. */
5740 fold_builtin_atomic_is_lock_free (tree arg0
, tree arg1
)
5742 if (!flag_inline_atomics
)
5745 /* If it isn't always lock free, don't generate a result. */
5746 if (fold_builtin_atomic_always_lock_free (arg0
, arg1
) == boolean_true_node
)
5747 return boolean_true_node
;
5752 /* Return true if the parameters to call EXP represent an object which will
5753 always generate lock free instructions. The first argument represents the
5754 size of the object, and the second parameter is a pointer to the object
5755 itself. If NULL is passed for the object, then the result is based on
5756 typical alignment for an object of the specified size. Otherwise return
5760 expand_builtin_atomic_is_lock_free (tree exp
)
5763 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5764 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5766 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
5768 error ("non-integer argument 1 to __atomic_is_lock_free");
5772 if (!flag_inline_atomics
)
5775 /* If the value is known at compile time, return the RTX for it. */
5776 size
= fold_builtin_atomic_is_lock_free (arg0
, arg1
);
5777 if (size
== boolean_true_node
)
5783 /* Expand the __atomic_thread_fence intrinsic:
5784 void __atomic_thread_fence (enum memmodel)
5785 EXP is the CALL_EXPR. */
5788 expand_builtin_atomic_thread_fence (tree exp
)
5790 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5791 expand_mem_thread_fence (model
);
5794 /* Expand the __atomic_signal_fence intrinsic:
5795 void __atomic_signal_fence (enum memmodel)
5796 EXP is the CALL_EXPR. */
5799 expand_builtin_atomic_signal_fence (tree exp
)
5801 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5802 expand_mem_signal_fence (model
);
5805 /* Expand the __sync_synchronize intrinsic. */
5808 expand_builtin_sync_synchronize (void)
5810 expand_mem_thread_fence (MEMMODEL_SEQ_CST
);
5814 expand_builtin_thread_pointer (tree exp
, rtx target
)
5816 enum insn_code icode
;
5817 if (!validate_arglist (exp
, VOID_TYPE
))
5819 icode
= direct_optab_handler (get_thread_pointer_optab
, Pmode
);
5820 if (icode
!= CODE_FOR_nothing
)
5822 struct expand_operand op
;
5823 /* If the target is not sutitable then create a new target. */
5824 if (target
== NULL_RTX
5826 || GET_MODE (target
) != Pmode
)
5827 target
= gen_reg_rtx (Pmode
);
5828 create_output_operand (&op
, target
, Pmode
);
5829 expand_insn (icode
, 1, &op
);
5832 error ("__builtin_thread_pointer is not supported on this target");
5837 expand_builtin_set_thread_pointer (tree exp
)
5839 enum insn_code icode
;
5840 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
5842 icode
= direct_optab_handler (set_thread_pointer_optab
, Pmode
);
5843 if (icode
!= CODE_FOR_nothing
)
5845 struct expand_operand op
;
5846 rtx val
= expand_expr (CALL_EXPR_ARG (exp
, 0), NULL_RTX
,
5847 Pmode
, EXPAND_NORMAL
);
5848 create_input_operand (&op
, val
, Pmode
);
5849 expand_insn (icode
, 1, &op
);
5852 error ("__builtin_set_thread_pointer is not supported on this target");
5856 /* Emit code to restore the current value of stack. */
5859 expand_stack_restore (tree var
)
5862 rtx sa
= expand_normal (var
);
5864 sa
= convert_memory_address (Pmode
, sa
);
5866 prev
= get_last_insn ();
5867 emit_stack_restore (SAVE_BLOCK
, sa
);
5868 fixup_args_size_notes (prev
, get_last_insn (), 0);
5872 /* Emit code to save the current value of stack. */
5875 expand_stack_save (void)
5879 do_pending_stack_adjust ();
5880 emit_stack_save (SAVE_BLOCK
, &ret
);
5885 /* Expand OpenACC acc_on_device.
5887 This has to happen late (that is, not in early folding; expand_builtin_*,
5888 rather than fold_builtin_*), as we have to act differently for host and
5889 acceleration device (ACCEL_COMPILER conditional). */
5892 expand_builtin_acc_on_device (tree exp
, rtx target
)
5894 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
5897 tree arg
= CALL_EXPR_ARG (exp
, 0);
5899 /* Return (arg == v1 || arg == v2) ? 1 : 0. */
5900 machine_mode v_mode
= TYPE_MODE (TREE_TYPE (arg
));
5901 rtx v
= expand_normal (arg
), v1
, v2
;
5902 #ifdef ACCEL_COMPILER
5903 v1
= GEN_INT (GOMP_DEVICE_NOT_HOST
);
5904 v2
= GEN_INT (ACCEL_COMPILER_acc_device
);
5906 v1
= GEN_INT (GOMP_DEVICE_NONE
);
5907 v2
= GEN_INT (GOMP_DEVICE_HOST
);
5909 machine_mode target_mode
= TYPE_MODE (integer_type_node
);
5910 if (!REG_P (target
) || GET_MODE (target
) != target_mode
)
5911 target
= gen_reg_rtx (target_mode
);
5912 emit_move_insn (target
, const1_rtx
);
5913 rtx_code_label
*done_label
= gen_label_rtx ();
5914 do_compare_rtx_and_jump (v
, v1
, EQ
, false, v_mode
, NULL_RTX
,
5915 NULL_RTX
, done_label
, PROB_EVEN
);
5916 do_compare_rtx_and_jump (v
, v2
, EQ
, false, v_mode
, NULL_RTX
,
5917 NULL_RTX
, done_label
, PROB_EVEN
);
5918 emit_move_insn (target
, const0_rtx
);
5919 emit_label (done_label
);
5925 /* Expand an expression EXP that calls a built-in function,
5926 with result going to TARGET if that's convenient
5927 (and in mode MODE if that's convenient).
5928 SUBTARGET may be used as the target for computing one of EXP's operands.
5929 IGNORE is nonzero if the value is to be ignored. */
5932 expand_builtin (tree exp
, rtx target
, rtx subtarget
, machine_mode mode
,
5935 tree fndecl
= get_callee_fndecl (exp
);
5936 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5937 machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
5940 /* When ASan is enabled, we don't want to expand some memory/string
5941 builtins and rely on libsanitizer's hooks. This allows us to avoid
5942 redundant checks and be sure, that possible overflow will be detected
5945 if ((flag_sanitize
& SANITIZE_ADDRESS
) && asan_intercepted_p (fcode
))
5946 return expand_call (exp
, target
, ignore
);
5948 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5949 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
5951 /* When not optimizing, generate calls to library functions for a certain
5954 && !called_as_built_in (fndecl
)
5955 && fcode
!= BUILT_IN_FORK
5956 && fcode
!= BUILT_IN_EXECL
5957 && fcode
!= BUILT_IN_EXECV
5958 && fcode
!= BUILT_IN_EXECLP
5959 && fcode
!= BUILT_IN_EXECLE
5960 && fcode
!= BUILT_IN_EXECVP
5961 && fcode
!= BUILT_IN_EXECVE
5962 && fcode
!= BUILT_IN_ALLOCA
5963 && fcode
!= BUILT_IN_ALLOCA_WITH_ALIGN
5964 && fcode
!= BUILT_IN_FREE
5965 && fcode
!= BUILT_IN_CHKP_SET_PTR_BOUNDS
5966 && fcode
!= BUILT_IN_CHKP_INIT_PTR_BOUNDS
5967 && fcode
!= BUILT_IN_CHKP_NULL_PTR_BOUNDS
5968 && fcode
!= BUILT_IN_CHKP_COPY_PTR_BOUNDS
5969 && fcode
!= BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5970 && fcode
!= BUILT_IN_CHKP_STORE_PTR_BOUNDS
5971 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
5972 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
5973 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_BOUNDS
5974 && fcode
!= BUILT_IN_CHKP_GET_PTR_LBOUND
5975 && fcode
!= BUILT_IN_CHKP_GET_PTR_UBOUND
5976 && fcode
!= BUILT_IN_CHKP_BNDRET
)
5977 return expand_call (exp
, target
, ignore
);
5979 /* The built-in function expanders test for target == const0_rtx
5980 to determine whether the function's result will be ignored. */
5982 target
= const0_rtx
;
5984 /* If the result of a pure or const built-in function is ignored, and
5985 none of its arguments are volatile, we can avoid expanding the
5986 built-in call and just evaluate the arguments for side-effects. */
5987 if (target
== const0_rtx
5988 && ((flags
= flags_from_decl_or_type (fndecl
)) & (ECF_CONST
| ECF_PURE
))
5989 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
5991 bool volatilep
= false;
5993 call_expr_arg_iterator iter
;
5995 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5996 if (TREE_THIS_VOLATILE (arg
))
6004 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
6005 expand_expr (arg
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
6010 /* expand_builtin_with_bounds is supposed to be used for
6011 instrumented builtin calls. */
6012 gcc_assert (!CALL_WITH_BOUNDS_P (exp
));
6016 CASE_FLT_FN (BUILT_IN_FABS
):
6017 case BUILT_IN_FABSD32
:
6018 case BUILT_IN_FABSD64
:
6019 case BUILT_IN_FABSD128
:
6020 target
= expand_builtin_fabs (exp
, target
, subtarget
);
6025 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
6026 target
= expand_builtin_copysign (exp
, target
, subtarget
);
6031 /* Just do a normal library call if we were unable to fold
6033 CASE_FLT_FN (BUILT_IN_CABS
):
6036 CASE_FLT_FN (BUILT_IN_EXP
):
6037 CASE_FLT_FN (BUILT_IN_EXP10
):
6038 CASE_FLT_FN (BUILT_IN_POW10
):
6039 CASE_FLT_FN (BUILT_IN_EXP2
):
6040 CASE_FLT_FN (BUILT_IN_EXPM1
):
6041 CASE_FLT_FN (BUILT_IN_LOGB
):
6042 CASE_FLT_FN (BUILT_IN_LOG
):
6043 CASE_FLT_FN (BUILT_IN_LOG10
):
6044 CASE_FLT_FN (BUILT_IN_LOG2
):
6045 CASE_FLT_FN (BUILT_IN_LOG1P
):
6046 CASE_FLT_FN (BUILT_IN_TAN
):
6047 CASE_FLT_FN (BUILT_IN_ASIN
):
6048 CASE_FLT_FN (BUILT_IN_ACOS
):
6049 CASE_FLT_FN (BUILT_IN_ATAN
):
6050 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
6051 /* Treat these like sqrt only if unsafe math optimizations are allowed,
6052 because of possible accuracy problems. */
6053 if (! flag_unsafe_math_optimizations
)
6055 CASE_FLT_FN (BUILT_IN_SQRT
):
6056 CASE_FLT_FN (BUILT_IN_FLOOR
):
6057 CASE_FLT_FN (BUILT_IN_CEIL
):
6058 CASE_FLT_FN (BUILT_IN_TRUNC
):
6059 CASE_FLT_FN (BUILT_IN_ROUND
):
6060 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
6061 CASE_FLT_FN (BUILT_IN_RINT
):
6062 target
= expand_builtin_mathfn (exp
, target
, subtarget
);
6067 CASE_FLT_FN (BUILT_IN_FMA
):
6068 target
= expand_builtin_mathfn_ternary (exp
, target
, subtarget
);
6073 CASE_FLT_FN (BUILT_IN_ILOGB
):
6074 if (! flag_unsafe_math_optimizations
)
6076 CASE_FLT_FN (BUILT_IN_ISINF
):
6077 CASE_FLT_FN (BUILT_IN_FINITE
):
6078 case BUILT_IN_ISFINITE
:
6079 case BUILT_IN_ISNORMAL
:
6080 target
= expand_builtin_interclass_mathfn (exp
, target
);
6085 CASE_FLT_FN (BUILT_IN_ICEIL
):
6086 CASE_FLT_FN (BUILT_IN_LCEIL
):
6087 CASE_FLT_FN (BUILT_IN_LLCEIL
):
6088 CASE_FLT_FN (BUILT_IN_LFLOOR
):
6089 CASE_FLT_FN (BUILT_IN_IFLOOR
):
6090 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
6091 target
= expand_builtin_int_roundingfn (exp
, target
);
6096 CASE_FLT_FN (BUILT_IN_IRINT
):
6097 CASE_FLT_FN (BUILT_IN_LRINT
):
6098 CASE_FLT_FN (BUILT_IN_LLRINT
):
6099 CASE_FLT_FN (BUILT_IN_IROUND
):
6100 CASE_FLT_FN (BUILT_IN_LROUND
):
6101 CASE_FLT_FN (BUILT_IN_LLROUND
):
6102 target
= expand_builtin_int_roundingfn_2 (exp
, target
);
6107 CASE_FLT_FN (BUILT_IN_POWI
):
6108 target
= expand_builtin_powi (exp
, target
);
6113 CASE_FLT_FN (BUILT_IN_ATAN2
):
6114 CASE_FLT_FN (BUILT_IN_LDEXP
):
6115 CASE_FLT_FN (BUILT_IN_SCALB
):
6116 CASE_FLT_FN (BUILT_IN_SCALBN
):
6117 CASE_FLT_FN (BUILT_IN_SCALBLN
):
6118 if (! flag_unsafe_math_optimizations
)
6121 CASE_FLT_FN (BUILT_IN_FMOD
):
6122 CASE_FLT_FN (BUILT_IN_REMAINDER
):
6123 CASE_FLT_FN (BUILT_IN_DREM
):
6124 CASE_FLT_FN (BUILT_IN_POW
):
6125 target
= expand_builtin_mathfn_2 (exp
, target
, subtarget
);
6130 CASE_FLT_FN (BUILT_IN_CEXPI
):
6131 target
= expand_builtin_cexpi (exp
, target
);
6132 gcc_assert (target
);
6135 CASE_FLT_FN (BUILT_IN_SIN
):
6136 CASE_FLT_FN (BUILT_IN_COS
):
6137 if (! flag_unsafe_math_optimizations
)
6139 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
6144 CASE_FLT_FN (BUILT_IN_SINCOS
):
6145 if (! flag_unsafe_math_optimizations
)
6147 target
= expand_builtin_sincos (exp
);
6152 case BUILT_IN_APPLY_ARGS
:
6153 return expand_builtin_apply_args ();
6155 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6156 FUNCTION with a copy of the parameters described by
6157 ARGUMENTS, and ARGSIZE. It returns a block of memory
6158 allocated on the stack into which is stored all the registers
6159 that might possibly be used for returning the result of a
6160 function. ARGUMENTS is the value returned by
6161 __builtin_apply_args. ARGSIZE is the number of bytes of
6162 arguments that must be copied. ??? How should this value be
6163 computed? We'll also need a safe worst case value for varargs
6165 case BUILT_IN_APPLY
:
6166 if (!validate_arglist (exp
, POINTER_TYPE
,
6167 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
6168 && !validate_arglist (exp
, REFERENCE_TYPE
,
6169 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6175 ops
[0] = expand_normal (CALL_EXPR_ARG (exp
, 0));
6176 ops
[1] = expand_normal (CALL_EXPR_ARG (exp
, 1));
6177 ops
[2] = expand_normal (CALL_EXPR_ARG (exp
, 2));
6179 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
6182 /* __builtin_return (RESULT) causes the function to return the
6183 value described by RESULT. RESULT is address of the block of
6184 memory returned by __builtin_apply. */
6185 case BUILT_IN_RETURN
:
6186 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6187 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp
, 0)));
6190 case BUILT_IN_SAVEREGS
:
6191 return expand_builtin_saveregs ();
6193 case BUILT_IN_VA_ARG_PACK
:
6194 /* All valid uses of __builtin_va_arg_pack () are removed during
6196 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp
);
6199 case BUILT_IN_VA_ARG_PACK_LEN
:
6200 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6202 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp
);
6205 /* Return the address of the first anonymous stack arg. */
6206 case BUILT_IN_NEXT_ARG
:
6207 if (fold_builtin_next_arg (exp
, false))
6209 return expand_builtin_next_arg ();
6211 case BUILT_IN_CLEAR_CACHE
:
6212 target
= expand_builtin___clear_cache (exp
);
6217 case BUILT_IN_CLASSIFY_TYPE
:
6218 return expand_builtin_classify_type (exp
);
6220 case BUILT_IN_CONSTANT_P
:
6223 case BUILT_IN_FRAME_ADDRESS
:
6224 case BUILT_IN_RETURN_ADDRESS
:
6225 return expand_builtin_frame_address (fndecl
, exp
);
6227 /* Returns the address of the area where the structure is returned.
6229 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
6230 if (call_expr_nargs (exp
) != 0
6231 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
6232 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
6235 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
6237 case BUILT_IN_ALLOCA
:
6238 case BUILT_IN_ALLOCA_WITH_ALIGN
:
6239 /* If the allocation stems from the declaration of a variable-sized
6240 object, it cannot accumulate. */
6241 target
= expand_builtin_alloca (exp
, CALL_ALLOCA_FOR_VAR_P (exp
));
6246 case BUILT_IN_STACK_SAVE
:
6247 return expand_stack_save ();
6249 case BUILT_IN_STACK_RESTORE
:
6250 expand_stack_restore (CALL_EXPR_ARG (exp
, 0));
6253 case BUILT_IN_BSWAP16
:
6254 case BUILT_IN_BSWAP32
:
6255 case BUILT_IN_BSWAP64
:
6256 target
= expand_builtin_bswap (target_mode
, exp
, target
, subtarget
);
6261 CASE_INT_FN (BUILT_IN_FFS
):
6262 target
= expand_builtin_unop (target_mode
, exp
, target
,
6263 subtarget
, ffs_optab
);
6268 CASE_INT_FN (BUILT_IN_CLZ
):
6269 target
= expand_builtin_unop (target_mode
, exp
, target
,
6270 subtarget
, clz_optab
);
6275 CASE_INT_FN (BUILT_IN_CTZ
):
6276 target
= expand_builtin_unop (target_mode
, exp
, target
,
6277 subtarget
, ctz_optab
);
6282 CASE_INT_FN (BUILT_IN_CLRSB
):
6283 target
= expand_builtin_unop (target_mode
, exp
, target
,
6284 subtarget
, clrsb_optab
);
6289 CASE_INT_FN (BUILT_IN_POPCOUNT
):
6290 target
= expand_builtin_unop (target_mode
, exp
, target
,
6291 subtarget
, popcount_optab
);
6296 CASE_INT_FN (BUILT_IN_PARITY
):
6297 target
= expand_builtin_unop (target_mode
, exp
, target
,
6298 subtarget
, parity_optab
);
6303 case BUILT_IN_STRLEN
:
6304 target
= expand_builtin_strlen (exp
, target
, target_mode
);
6309 case BUILT_IN_STRCPY
:
6310 target
= expand_builtin_strcpy (exp
, target
);
6315 case BUILT_IN_STRNCPY
:
6316 target
= expand_builtin_strncpy (exp
, target
);
6321 case BUILT_IN_STPCPY
:
6322 target
= expand_builtin_stpcpy (exp
, target
, mode
);
6327 case BUILT_IN_MEMCPY
:
6328 target
= expand_builtin_memcpy (exp
, target
);
6333 case BUILT_IN_MEMPCPY
:
6334 target
= expand_builtin_mempcpy (exp
, target
, mode
);
6339 case BUILT_IN_MEMSET
:
6340 target
= expand_builtin_memset (exp
, target
, mode
);
6345 case BUILT_IN_BZERO
:
6346 target
= expand_builtin_bzero (exp
);
6351 case BUILT_IN_STRCMP
:
6352 target
= expand_builtin_strcmp (exp
, target
);
6357 case BUILT_IN_STRNCMP
:
6358 target
= expand_builtin_strncmp (exp
, target
, mode
);
6364 case BUILT_IN_MEMCMP
:
6365 target
= expand_builtin_memcmp (exp
, target
, mode
);
6370 case BUILT_IN_SETJMP
:
6371 /* This should have been lowered to the builtins below. */
6374 case BUILT_IN_SETJMP_SETUP
:
6375 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6376 and the receiver label. */
6377 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
6379 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6380 VOIDmode
, EXPAND_NORMAL
);
6381 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 1), 0);
6382 rtx label_r
= label_rtx (label
);
6384 /* This is copied from the handling of non-local gotos. */
6385 expand_builtin_setjmp_setup (buf_addr
, label_r
);
6386 nonlocal_goto_handler_labels
6387 = gen_rtx_INSN_LIST (VOIDmode
, label_r
,
6388 nonlocal_goto_handler_labels
);
6389 /* ??? Do not let expand_label treat us as such since we would
6390 not want to be both on the list of non-local labels and on
6391 the list of forced labels. */
6392 FORCED_LABEL (label
) = 0;
6397 case BUILT_IN_SETJMP_RECEIVER
:
6398 /* __builtin_setjmp_receiver is passed the receiver label. */
6399 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6401 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 0), 0);
6402 rtx label_r
= label_rtx (label
);
6404 expand_builtin_setjmp_receiver (label_r
);
6409 /* __builtin_longjmp is passed a pointer to an array of five words.
6410 It's similar to the C library longjmp function but works with
6411 __builtin_setjmp above. */
6412 case BUILT_IN_LONGJMP
:
6413 if (validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6415 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6416 VOIDmode
, EXPAND_NORMAL
);
6417 rtx value
= expand_normal (CALL_EXPR_ARG (exp
, 1));
6419 if (value
!= const1_rtx
)
6421 error ("%<__builtin_longjmp%> second argument must be 1");
6425 expand_builtin_longjmp (buf_addr
, value
);
6430 case BUILT_IN_NONLOCAL_GOTO
:
6431 target
= expand_builtin_nonlocal_goto (exp
);
6436 /* This updates the setjmp buffer that is its argument with the value
6437 of the current stack pointer. */
6438 case BUILT_IN_UPDATE_SETJMP_BUF
:
6439 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6442 = expand_normal (CALL_EXPR_ARG (exp
, 0));
6444 expand_builtin_update_setjmp_buf (buf_addr
);
6450 expand_builtin_trap ();
6453 case BUILT_IN_UNREACHABLE
:
6454 expand_builtin_unreachable ();
6457 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
6458 case BUILT_IN_SIGNBITD32
:
6459 case BUILT_IN_SIGNBITD64
:
6460 case BUILT_IN_SIGNBITD128
:
6461 target
= expand_builtin_signbit (exp
, target
);
6466 /* Various hooks for the DWARF 2 __throw routine. */
6467 case BUILT_IN_UNWIND_INIT
:
6468 expand_builtin_unwind_init ();
6470 case BUILT_IN_DWARF_CFA
:
6471 return virtual_cfa_rtx
;
6472 #ifdef DWARF2_UNWIND_INFO
6473 case BUILT_IN_DWARF_SP_COLUMN
:
6474 return expand_builtin_dwarf_sp_column ();
6475 case BUILT_IN_INIT_DWARF_REG_SIZES
:
6476 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp
, 0));
6479 case BUILT_IN_FROB_RETURN_ADDR
:
6480 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp
, 0));
6481 case BUILT_IN_EXTRACT_RETURN_ADDR
:
6482 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp
, 0));
6483 case BUILT_IN_EH_RETURN
:
6484 expand_builtin_eh_return (CALL_EXPR_ARG (exp
, 0),
6485 CALL_EXPR_ARG (exp
, 1));
6487 #ifdef EH_RETURN_DATA_REGNO
6488 case BUILT_IN_EH_RETURN_DATA_REGNO
:
6489 return expand_builtin_eh_return_data_regno (exp
);
6491 case BUILT_IN_EXTEND_POINTER
:
6492 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp
, 0));
6493 case BUILT_IN_EH_POINTER
:
6494 return expand_builtin_eh_pointer (exp
);
6495 case BUILT_IN_EH_FILTER
:
6496 return expand_builtin_eh_filter (exp
);
6497 case BUILT_IN_EH_COPY_VALUES
:
6498 return expand_builtin_eh_copy_values (exp
);
6500 case BUILT_IN_VA_START
:
6501 return expand_builtin_va_start (exp
);
6502 case BUILT_IN_VA_END
:
6503 return expand_builtin_va_end (exp
);
6504 case BUILT_IN_VA_COPY
:
6505 return expand_builtin_va_copy (exp
);
6506 case BUILT_IN_EXPECT
:
6507 return expand_builtin_expect (exp
, target
);
6508 case BUILT_IN_ASSUME_ALIGNED
:
6509 return expand_builtin_assume_aligned (exp
, target
);
6510 case BUILT_IN_PREFETCH
:
6511 expand_builtin_prefetch (exp
);
6514 case BUILT_IN_INIT_TRAMPOLINE
:
6515 return expand_builtin_init_trampoline (exp
, true);
6516 case BUILT_IN_INIT_HEAP_TRAMPOLINE
:
6517 return expand_builtin_init_trampoline (exp
, false);
6518 case BUILT_IN_ADJUST_TRAMPOLINE
:
6519 return expand_builtin_adjust_trampoline (exp
);
6522 case BUILT_IN_EXECL
:
6523 case BUILT_IN_EXECV
:
6524 case BUILT_IN_EXECLP
:
6525 case BUILT_IN_EXECLE
:
6526 case BUILT_IN_EXECVP
:
6527 case BUILT_IN_EXECVE
:
6528 target
= expand_builtin_fork_or_exec (fndecl
, exp
, target
, ignore
);
6533 case BUILT_IN_SYNC_FETCH_AND_ADD_1
:
6534 case BUILT_IN_SYNC_FETCH_AND_ADD_2
:
6535 case BUILT_IN_SYNC_FETCH_AND_ADD_4
:
6536 case BUILT_IN_SYNC_FETCH_AND_ADD_8
:
6537 case BUILT_IN_SYNC_FETCH_AND_ADD_16
:
6538 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_ADD_1
);
6539 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, false, target
);
6544 case BUILT_IN_SYNC_FETCH_AND_SUB_1
:
6545 case BUILT_IN_SYNC_FETCH_AND_SUB_2
:
6546 case BUILT_IN_SYNC_FETCH_AND_SUB_4
:
6547 case BUILT_IN_SYNC_FETCH_AND_SUB_8
:
6548 case BUILT_IN_SYNC_FETCH_AND_SUB_16
:
6549 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_SUB_1
);
6550 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, false, target
);
6555 case BUILT_IN_SYNC_FETCH_AND_OR_1
:
6556 case BUILT_IN_SYNC_FETCH_AND_OR_2
:
6557 case BUILT_IN_SYNC_FETCH_AND_OR_4
:
6558 case BUILT_IN_SYNC_FETCH_AND_OR_8
:
6559 case BUILT_IN_SYNC_FETCH_AND_OR_16
:
6560 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_OR_1
);
6561 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, false, target
);
6566 case BUILT_IN_SYNC_FETCH_AND_AND_1
:
6567 case BUILT_IN_SYNC_FETCH_AND_AND_2
:
6568 case BUILT_IN_SYNC_FETCH_AND_AND_4
:
6569 case BUILT_IN_SYNC_FETCH_AND_AND_8
:
6570 case BUILT_IN_SYNC_FETCH_AND_AND_16
:
6571 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_AND_1
);
6572 target
= expand_builtin_sync_operation (mode
, exp
, AND
, false, target
);
6577 case BUILT_IN_SYNC_FETCH_AND_XOR_1
:
6578 case BUILT_IN_SYNC_FETCH_AND_XOR_2
:
6579 case BUILT_IN_SYNC_FETCH_AND_XOR_4
:
6580 case BUILT_IN_SYNC_FETCH_AND_XOR_8
:
6581 case BUILT_IN_SYNC_FETCH_AND_XOR_16
:
6582 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_XOR_1
);
6583 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, false, target
);
6588 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
6589 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
6590 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
6591 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
6592 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
6593 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_NAND_1
);
6594 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, false, target
);
6599 case BUILT_IN_SYNC_ADD_AND_FETCH_1
:
6600 case BUILT_IN_SYNC_ADD_AND_FETCH_2
:
6601 case BUILT_IN_SYNC_ADD_AND_FETCH_4
:
6602 case BUILT_IN_SYNC_ADD_AND_FETCH_8
:
6603 case BUILT_IN_SYNC_ADD_AND_FETCH_16
:
6604 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_ADD_AND_FETCH_1
);
6605 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, true, target
);
6610 case BUILT_IN_SYNC_SUB_AND_FETCH_1
:
6611 case BUILT_IN_SYNC_SUB_AND_FETCH_2
:
6612 case BUILT_IN_SYNC_SUB_AND_FETCH_4
:
6613 case BUILT_IN_SYNC_SUB_AND_FETCH_8
:
6614 case BUILT_IN_SYNC_SUB_AND_FETCH_16
:
6615 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_SUB_AND_FETCH_1
);
6616 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, true, target
);
6621 case BUILT_IN_SYNC_OR_AND_FETCH_1
:
6622 case BUILT_IN_SYNC_OR_AND_FETCH_2
:
6623 case BUILT_IN_SYNC_OR_AND_FETCH_4
:
6624 case BUILT_IN_SYNC_OR_AND_FETCH_8
:
6625 case BUILT_IN_SYNC_OR_AND_FETCH_16
:
6626 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_OR_AND_FETCH_1
);
6627 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, true, target
);
6632 case BUILT_IN_SYNC_AND_AND_FETCH_1
:
6633 case BUILT_IN_SYNC_AND_AND_FETCH_2
:
6634 case BUILT_IN_SYNC_AND_AND_FETCH_4
:
6635 case BUILT_IN_SYNC_AND_AND_FETCH_8
:
6636 case BUILT_IN_SYNC_AND_AND_FETCH_16
:
6637 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_AND_AND_FETCH_1
);
6638 target
= expand_builtin_sync_operation (mode
, exp
, AND
, true, target
);
6643 case BUILT_IN_SYNC_XOR_AND_FETCH_1
:
6644 case BUILT_IN_SYNC_XOR_AND_FETCH_2
:
6645 case BUILT_IN_SYNC_XOR_AND_FETCH_4
:
6646 case BUILT_IN_SYNC_XOR_AND_FETCH_8
:
6647 case BUILT_IN_SYNC_XOR_AND_FETCH_16
:
6648 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_XOR_AND_FETCH_1
);
6649 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, true, target
);
6654 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
6655 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
6656 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
6657 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
6658 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
6659 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_NAND_AND_FETCH_1
);
6660 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, true, target
);
6665 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
6666 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
6667 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
6668 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
6669 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
6670 if (mode
== VOIDmode
)
6671 mode
= TYPE_MODE (boolean_type_node
);
6672 if (!target
|| !register_operand (target
, mode
))
6673 target
= gen_reg_rtx (mode
);
6675 mode
= get_builtin_sync_mode
6676 (fcode
- BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
);
6677 target
= expand_builtin_compare_and_swap (mode
, exp
, true, target
);
6682 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
:
6683 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2
:
6684 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4
:
6685 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8
:
6686 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16
:
6687 mode
= get_builtin_sync_mode
6688 (fcode
- BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
);
6689 target
= expand_builtin_compare_and_swap (mode
, exp
, false, target
);
6694 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
:
6695 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2
:
6696 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4
:
6697 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8
:
6698 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16
:
6699 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
);
6700 target
= expand_builtin_sync_lock_test_and_set (mode
, exp
, target
);
6705 case BUILT_IN_SYNC_LOCK_RELEASE_1
:
6706 case BUILT_IN_SYNC_LOCK_RELEASE_2
:
6707 case BUILT_IN_SYNC_LOCK_RELEASE_4
:
6708 case BUILT_IN_SYNC_LOCK_RELEASE_8
:
6709 case BUILT_IN_SYNC_LOCK_RELEASE_16
:
6710 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_RELEASE_1
);
6711 expand_builtin_sync_lock_release (mode
, exp
);
6714 case BUILT_IN_SYNC_SYNCHRONIZE
:
6715 expand_builtin_sync_synchronize ();
6718 case BUILT_IN_ATOMIC_EXCHANGE_1
:
6719 case BUILT_IN_ATOMIC_EXCHANGE_2
:
6720 case BUILT_IN_ATOMIC_EXCHANGE_4
:
6721 case BUILT_IN_ATOMIC_EXCHANGE_8
:
6722 case BUILT_IN_ATOMIC_EXCHANGE_16
:
6723 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_EXCHANGE_1
);
6724 target
= expand_builtin_atomic_exchange (mode
, exp
, target
);
6729 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
6730 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
6731 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
6732 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
6733 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
6735 unsigned int nargs
, z
;
6736 vec
<tree
, va_gc
> *vec
;
6739 get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
);
6740 target
= expand_builtin_atomic_compare_exchange (mode
, exp
, target
);
6744 /* If this is turned into an external library call, the weak parameter
6745 must be dropped to match the expected parameter list. */
6746 nargs
= call_expr_nargs (exp
);
6747 vec_alloc (vec
, nargs
- 1);
6748 for (z
= 0; z
< 3; z
++)
6749 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
6750 /* Skip the boolean weak parameter. */
6751 for (z
= 4; z
< 6; z
++)
6752 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
6753 exp
= build_call_vec (TREE_TYPE (exp
), CALL_EXPR_FN (exp
), vec
);
6757 case BUILT_IN_ATOMIC_LOAD_1
:
6758 case BUILT_IN_ATOMIC_LOAD_2
:
6759 case BUILT_IN_ATOMIC_LOAD_4
:
6760 case BUILT_IN_ATOMIC_LOAD_8
:
6761 case BUILT_IN_ATOMIC_LOAD_16
:
6762 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_LOAD_1
);
6763 target
= expand_builtin_atomic_load (mode
, exp
, target
);
6768 case BUILT_IN_ATOMIC_STORE_1
:
6769 case BUILT_IN_ATOMIC_STORE_2
:
6770 case BUILT_IN_ATOMIC_STORE_4
:
6771 case BUILT_IN_ATOMIC_STORE_8
:
6772 case BUILT_IN_ATOMIC_STORE_16
:
6773 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_STORE_1
);
6774 target
= expand_builtin_atomic_store (mode
, exp
);
6779 case BUILT_IN_ATOMIC_ADD_FETCH_1
:
6780 case BUILT_IN_ATOMIC_ADD_FETCH_2
:
6781 case BUILT_IN_ATOMIC_ADD_FETCH_4
:
6782 case BUILT_IN_ATOMIC_ADD_FETCH_8
:
6783 case BUILT_IN_ATOMIC_ADD_FETCH_16
:
6785 enum built_in_function lib
;
6786 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
);
6787 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_ADD_1
+
6788 (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
));
6789 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, true,
6795 case BUILT_IN_ATOMIC_SUB_FETCH_1
:
6796 case BUILT_IN_ATOMIC_SUB_FETCH_2
:
6797 case BUILT_IN_ATOMIC_SUB_FETCH_4
:
6798 case BUILT_IN_ATOMIC_SUB_FETCH_8
:
6799 case BUILT_IN_ATOMIC_SUB_FETCH_16
:
6801 enum built_in_function lib
;
6802 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
);
6803 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_SUB_1
+
6804 (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
));
6805 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, true,
6811 case BUILT_IN_ATOMIC_AND_FETCH_1
:
6812 case BUILT_IN_ATOMIC_AND_FETCH_2
:
6813 case BUILT_IN_ATOMIC_AND_FETCH_4
:
6814 case BUILT_IN_ATOMIC_AND_FETCH_8
:
6815 case BUILT_IN_ATOMIC_AND_FETCH_16
:
6817 enum built_in_function lib
;
6818 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
);
6819 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_AND_1
+
6820 (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
));
6821 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, true,
6827 case BUILT_IN_ATOMIC_NAND_FETCH_1
:
6828 case BUILT_IN_ATOMIC_NAND_FETCH_2
:
6829 case BUILT_IN_ATOMIC_NAND_FETCH_4
:
6830 case BUILT_IN_ATOMIC_NAND_FETCH_8
:
6831 case BUILT_IN_ATOMIC_NAND_FETCH_16
:
6833 enum built_in_function lib
;
6834 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
);
6835 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_NAND_1
+
6836 (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
));
6837 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, true,
6843 case BUILT_IN_ATOMIC_XOR_FETCH_1
:
6844 case BUILT_IN_ATOMIC_XOR_FETCH_2
:
6845 case BUILT_IN_ATOMIC_XOR_FETCH_4
:
6846 case BUILT_IN_ATOMIC_XOR_FETCH_8
:
6847 case BUILT_IN_ATOMIC_XOR_FETCH_16
:
6849 enum built_in_function lib
;
6850 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
);
6851 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_XOR_1
+
6852 (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
));
6853 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, true,
6859 case BUILT_IN_ATOMIC_OR_FETCH_1
:
6860 case BUILT_IN_ATOMIC_OR_FETCH_2
:
6861 case BUILT_IN_ATOMIC_OR_FETCH_4
:
6862 case BUILT_IN_ATOMIC_OR_FETCH_8
:
6863 case BUILT_IN_ATOMIC_OR_FETCH_16
:
6865 enum built_in_function lib
;
6866 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
);
6867 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_OR_1
+
6868 (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
));
6869 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, true,
6875 case BUILT_IN_ATOMIC_FETCH_ADD_1
:
6876 case BUILT_IN_ATOMIC_FETCH_ADD_2
:
6877 case BUILT_IN_ATOMIC_FETCH_ADD_4
:
6878 case BUILT_IN_ATOMIC_FETCH_ADD_8
:
6879 case BUILT_IN_ATOMIC_FETCH_ADD_16
:
6880 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_ADD_1
);
6881 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, false,
6882 ignore
, BUILT_IN_NONE
);
6887 case BUILT_IN_ATOMIC_FETCH_SUB_1
:
6888 case BUILT_IN_ATOMIC_FETCH_SUB_2
:
6889 case BUILT_IN_ATOMIC_FETCH_SUB_4
:
6890 case BUILT_IN_ATOMIC_FETCH_SUB_8
:
6891 case BUILT_IN_ATOMIC_FETCH_SUB_16
:
6892 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_SUB_1
);
6893 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, false,
6894 ignore
, BUILT_IN_NONE
);
6899 case BUILT_IN_ATOMIC_FETCH_AND_1
:
6900 case BUILT_IN_ATOMIC_FETCH_AND_2
:
6901 case BUILT_IN_ATOMIC_FETCH_AND_4
:
6902 case BUILT_IN_ATOMIC_FETCH_AND_8
:
6903 case BUILT_IN_ATOMIC_FETCH_AND_16
:
6904 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_AND_1
);
6905 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, false,
6906 ignore
, BUILT_IN_NONE
);
6911 case BUILT_IN_ATOMIC_FETCH_NAND_1
:
6912 case BUILT_IN_ATOMIC_FETCH_NAND_2
:
6913 case BUILT_IN_ATOMIC_FETCH_NAND_4
:
6914 case BUILT_IN_ATOMIC_FETCH_NAND_8
:
6915 case BUILT_IN_ATOMIC_FETCH_NAND_16
:
6916 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_NAND_1
);
6917 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, false,
6918 ignore
, BUILT_IN_NONE
);
6923 case BUILT_IN_ATOMIC_FETCH_XOR_1
:
6924 case BUILT_IN_ATOMIC_FETCH_XOR_2
:
6925 case BUILT_IN_ATOMIC_FETCH_XOR_4
:
6926 case BUILT_IN_ATOMIC_FETCH_XOR_8
:
6927 case BUILT_IN_ATOMIC_FETCH_XOR_16
:
6928 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_XOR_1
);
6929 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, false,
6930 ignore
, BUILT_IN_NONE
);
6935 case BUILT_IN_ATOMIC_FETCH_OR_1
:
6936 case BUILT_IN_ATOMIC_FETCH_OR_2
:
6937 case BUILT_IN_ATOMIC_FETCH_OR_4
:
6938 case BUILT_IN_ATOMIC_FETCH_OR_8
:
6939 case BUILT_IN_ATOMIC_FETCH_OR_16
:
6940 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_OR_1
);
6941 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, false,
6942 ignore
, BUILT_IN_NONE
);
6947 case BUILT_IN_ATOMIC_TEST_AND_SET
:
6948 return expand_builtin_atomic_test_and_set (exp
, target
);
6950 case BUILT_IN_ATOMIC_CLEAR
:
6951 return expand_builtin_atomic_clear (exp
);
6953 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
6954 return expand_builtin_atomic_always_lock_free (exp
);
6956 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
6957 target
= expand_builtin_atomic_is_lock_free (exp
);
6962 case BUILT_IN_ATOMIC_THREAD_FENCE
:
6963 expand_builtin_atomic_thread_fence (exp
);
6966 case BUILT_IN_ATOMIC_SIGNAL_FENCE
:
6967 expand_builtin_atomic_signal_fence (exp
);
6970 case BUILT_IN_OBJECT_SIZE
:
6971 return expand_builtin_object_size (exp
);
6973 case BUILT_IN_MEMCPY_CHK
:
6974 case BUILT_IN_MEMPCPY_CHK
:
6975 case BUILT_IN_MEMMOVE_CHK
:
6976 case BUILT_IN_MEMSET_CHK
:
6977 target
= expand_builtin_memory_chk (exp
, target
, mode
, fcode
);
6982 case BUILT_IN_STRCPY_CHK
:
6983 case BUILT_IN_STPCPY_CHK
:
6984 case BUILT_IN_STRNCPY_CHK
:
6985 case BUILT_IN_STPNCPY_CHK
:
6986 case BUILT_IN_STRCAT_CHK
:
6987 case BUILT_IN_STRNCAT_CHK
:
6988 case BUILT_IN_SNPRINTF_CHK
:
6989 case BUILT_IN_VSNPRINTF_CHK
:
6990 maybe_emit_chk_warning (exp
, fcode
);
6993 case BUILT_IN_SPRINTF_CHK
:
6994 case BUILT_IN_VSPRINTF_CHK
:
6995 maybe_emit_sprintf_chk_warning (exp
, fcode
);
6999 if (warn_free_nonheap_object
)
7000 maybe_emit_free_warning (exp
);
7003 case BUILT_IN_THREAD_POINTER
:
7004 return expand_builtin_thread_pointer (exp
, target
);
7006 case BUILT_IN_SET_THREAD_POINTER
:
7007 expand_builtin_set_thread_pointer (exp
);
7010 case BUILT_IN_CILK_DETACH
:
7011 expand_builtin_cilk_detach (exp
);
7014 case BUILT_IN_CILK_POP_FRAME
:
7015 expand_builtin_cilk_pop_frame (exp
);
7018 case BUILT_IN_CHKP_INIT_PTR_BOUNDS
:
7019 case BUILT_IN_CHKP_NULL_PTR_BOUNDS
:
7020 case BUILT_IN_CHKP_COPY_PTR_BOUNDS
:
7021 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
:
7022 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
:
7023 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS
:
7024 case BUILT_IN_CHKP_SET_PTR_BOUNDS
:
7025 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS
:
7026 case BUILT_IN_CHKP_STORE_PTR_BOUNDS
:
7027 case BUILT_IN_CHKP_GET_PTR_LBOUND
:
7028 case BUILT_IN_CHKP_GET_PTR_UBOUND
:
7029 /* We allow user CHKP builtins if Pointer Bounds
7031 if (!chkp_function_instrumented_p (current_function_decl
))
7033 if (fcode
== BUILT_IN_CHKP_SET_PTR_BOUNDS
7034 || fcode
== BUILT_IN_CHKP_NARROW_PTR_BOUNDS
7035 || fcode
== BUILT_IN_CHKP_INIT_PTR_BOUNDS
7036 || fcode
== BUILT_IN_CHKP_NULL_PTR_BOUNDS
7037 || fcode
== BUILT_IN_CHKP_COPY_PTR_BOUNDS
)
7038 return expand_normal (CALL_EXPR_ARG (exp
, 0));
7039 else if (fcode
== BUILT_IN_CHKP_GET_PTR_LBOUND
)
7040 return expand_normal (size_zero_node
);
7041 else if (fcode
== BUILT_IN_CHKP_GET_PTR_UBOUND
)
7042 return expand_normal (size_int (-1));
7048 case BUILT_IN_CHKP_BNDMK
:
7049 case BUILT_IN_CHKP_BNDSTX
:
7050 case BUILT_IN_CHKP_BNDCL
:
7051 case BUILT_IN_CHKP_BNDCU
:
7052 case BUILT_IN_CHKP_BNDLDX
:
7053 case BUILT_IN_CHKP_BNDRET
:
7054 case BUILT_IN_CHKP_INTERSECT
:
7055 case BUILT_IN_CHKP_NARROW
:
7056 case BUILT_IN_CHKP_EXTRACT_LOWER
:
7057 case BUILT_IN_CHKP_EXTRACT_UPPER
:
7058 /* Software implementation of Pointer Bounds Checker is NYI.
7059 Target support is required. */
7060 error ("Your target platform does not support -fcheck-pointer-bounds");
7063 case BUILT_IN_ACC_ON_DEVICE
:
7064 target
= expand_builtin_acc_on_device (exp
, target
);
7069 default: /* just do library call, if unknown builtin */
7073 /* The switch statement above can drop through to cause the function
7074 to be called normally. */
7075 return expand_call (exp
, target
, ignore
);
7078 /* Similar to expand_builtin but is used for instrumented calls. */
7081 expand_builtin_with_bounds (tree exp
, rtx target
,
7082 rtx subtarget ATTRIBUTE_UNUSED
,
7083 machine_mode mode
, int ignore
)
7085 tree fndecl
= get_callee_fndecl (exp
);
7086 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7088 gcc_assert (CALL_WITH_BOUNDS_P (exp
));
7090 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
7091 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
7093 gcc_assert (fcode
> BEGIN_CHKP_BUILTINS
7094 && fcode
< END_CHKP_BUILTINS
);
7098 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP
:
7099 target
= expand_builtin_memcpy_with_bounds (exp
, target
);
7104 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
:
7105 target
= expand_builtin_mempcpy_with_bounds (exp
, target
, mode
);
7110 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP
:
7111 target
= expand_builtin_memset_with_bounds (exp
, target
, mode
);
7120 /* The switch statement above can drop through to cause the function
7121 to be called normally. */
7122 return expand_call (exp
, target
, ignore
);
7125 /* Determine whether a tree node represents a call to a built-in
7126 function. If the tree T is a call to a built-in function with
7127 the right number of arguments of the appropriate types, return
7128 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7129 Otherwise the return value is END_BUILTINS. */
7131 enum built_in_function
7132 builtin_mathfn_code (const_tree t
)
7134 const_tree fndecl
, arg
, parmlist
;
7135 const_tree argtype
, parmtype
;
7136 const_call_expr_arg_iterator iter
;
7138 if (TREE_CODE (t
) != CALL_EXPR
7139 || TREE_CODE (CALL_EXPR_FN (t
)) != ADDR_EXPR
)
7140 return END_BUILTINS
;
7142 fndecl
= get_callee_fndecl (t
);
7143 if (fndecl
== NULL_TREE
7144 || TREE_CODE (fndecl
) != FUNCTION_DECL
7145 || ! DECL_BUILT_IN (fndecl
)
7146 || DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
7147 return END_BUILTINS
;
7149 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
7150 init_const_call_expr_arg_iterator (t
, &iter
);
7151 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
7153 /* If a function doesn't take a variable number of arguments,
7154 the last element in the list will have type `void'. */
7155 parmtype
= TREE_VALUE (parmlist
);
7156 if (VOID_TYPE_P (parmtype
))
7158 if (more_const_call_expr_args_p (&iter
))
7159 return END_BUILTINS
;
7160 return DECL_FUNCTION_CODE (fndecl
);
7163 if (! more_const_call_expr_args_p (&iter
))
7164 return END_BUILTINS
;
7166 arg
= next_const_call_expr_arg (&iter
);
7167 argtype
= TREE_TYPE (arg
);
7169 if (SCALAR_FLOAT_TYPE_P (parmtype
))
7171 if (! SCALAR_FLOAT_TYPE_P (argtype
))
7172 return END_BUILTINS
;
7174 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
7176 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
7177 return END_BUILTINS
;
7179 else if (POINTER_TYPE_P (parmtype
))
7181 if (! POINTER_TYPE_P (argtype
))
7182 return END_BUILTINS
;
7184 else if (INTEGRAL_TYPE_P (parmtype
))
7186 if (! INTEGRAL_TYPE_P (argtype
))
7187 return END_BUILTINS
;
7190 return END_BUILTINS
;
7193 /* Variable-length argument list. */
7194 return DECL_FUNCTION_CODE (fndecl
);
7197 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7198 evaluate to a constant. */
7201 fold_builtin_constant_p (tree arg
)
7203 /* We return 1 for a numeric type that's known to be a constant
7204 value at compile-time or for an aggregate type that's a
7205 literal constant. */
7208 /* If we know this is a constant, emit the constant of one. */
7209 if (CONSTANT_CLASS_P (arg
)
7210 || (TREE_CODE (arg
) == CONSTRUCTOR
7211 && TREE_CONSTANT (arg
)))
7212 return integer_one_node
;
7213 if (TREE_CODE (arg
) == ADDR_EXPR
)
7215 tree op
= TREE_OPERAND (arg
, 0);
7216 if (TREE_CODE (op
) == STRING_CST
7217 || (TREE_CODE (op
) == ARRAY_REF
7218 && integer_zerop (TREE_OPERAND (op
, 1))
7219 && TREE_CODE (TREE_OPERAND (op
, 0)) == STRING_CST
))
7220 return integer_one_node
;
7223 /* If this expression has side effects, show we don't know it to be a
7224 constant. Likewise if it's a pointer or aggregate type since in
7225 those case we only want literals, since those are only optimized
7226 when generating RTL, not later.
7227 And finally, if we are compiling an initializer, not code, we
7228 need to return a definite result now; there's not going to be any
7229 more optimization done. */
7230 if (TREE_SIDE_EFFECTS (arg
)
7231 || AGGREGATE_TYPE_P (TREE_TYPE (arg
))
7232 || POINTER_TYPE_P (TREE_TYPE (arg
))
7234 || folding_initializer
7235 || force_folding_builtin_constant_p
)
7236 return integer_zero_node
;
7241 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7242 return it as a truthvalue. */
7245 build_builtin_expect_predicate (location_t loc
, tree pred
, tree expected
,
7248 tree fn
, arg_types
, pred_type
, expected_type
, call_expr
, ret_type
;
7250 fn
= builtin_decl_explicit (BUILT_IN_EXPECT
);
7251 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
7252 ret_type
= TREE_TYPE (TREE_TYPE (fn
));
7253 pred_type
= TREE_VALUE (arg_types
);
7254 expected_type
= TREE_VALUE (TREE_CHAIN (arg_types
));
7256 pred
= fold_convert_loc (loc
, pred_type
, pred
);
7257 expected
= fold_convert_loc (loc
, expected_type
, expected
);
7258 call_expr
= build_call_expr_loc (loc
, fn
, predictor
? 3 : 2, pred
, expected
,
7261 return build2 (NE_EXPR
, TREE_TYPE (pred
), call_expr
,
7262 build_int_cst (ret_type
, 0));
7265 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7266 NULL_TREE if no simplification is possible. */
7269 fold_builtin_expect (location_t loc
, tree arg0
, tree arg1
, tree arg2
)
7271 tree inner
, fndecl
, inner_arg0
;
7272 enum tree_code code
;
7274 /* Distribute the expected value over short-circuiting operators.
7275 See through the cast from truthvalue_type_node to long. */
7277 while (CONVERT_EXPR_P (inner_arg0
)
7278 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0
))
7279 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0
, 0))))
7280 inner_arg0
= TREE_OPERAND (inner_arg0
, 0);
7282 /* If this is a builtin_expect within a builtin_expect keep the
7283 inner one. See through a comparison against a constant. It
7284 might have been added to create a thruthvalue. */
7287 if (COMPARISON_CLASS_P (inner
)
7288 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
)
7289 inner
= TREE_OPERAND (inner
, 0);
7291 if (TREE_CODE (inner
) == CALL_EXPR
7292 && (fndecl
= get_callee_fndecl (inner
))
7293 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
7294 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
)
7298 code
= TREE_CODE (inner
);
7299 if (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
7301 tree op0
= TREE_OPERAND (inner
, 0);
7302 tree op1
= TREE_OPERAND (inner
, 1);
7304 op0
= build_builtin_expect_predicate (loc
, op0
, arg1
, arg2
);
7305 op1
= build_builtin_expect_predicate (loc
, op1
, arg1
, arg2
);
7306 inner
= build2 (code
, TREE_TYPE (inner
), op0
, op1
);
7308 return fold_convert_loc (loc
, TREE_TYPE (arg0
), inner
);
7311 /* If the argument isn't invariant then there's nothing else we can do. */
7312 if (!TREE_CONSTANT (inner_arg0
))
7315 /* If we expect that a comparison against the argument will fold to
7316 a constant return the constant. In practice, this means a true
7317 constant or the address of a non-weak symbol. */
7320 if (TREE_CODE (inner
) == ADDR_EXPR
)
7324 inner
= TREE_OPERAND (inner
, 0);
7326 while (TREE_CODE (inner
) == COMPONENT_REF
7327 || TREE_CODE (inner
) == ARRAY_REF
);
7328 if ((TREE_CODE (inner
) == VAR_DECL
7329 || TREE_CODE (inner
) == FUNCTION_DECL
)
7330 && DECL_WEAK (inner
))
7334 /* Otherwise, ARG0 already has the proper type for the return value. */
7338 /* Fold a call to __builtin_classify_type with argument ARG. */
7341 fold_builtin_classify_type (tree arg
)
7344 return build_int_cst (integer_type_node
, no_type_class
);
7346 return build_int_cst (integer_type_node
, type_to_class (TREE_TYPE (arg
)));
7349 /* Fold a call to __builtin_strlen with argument ARG. */
7352 fold_builtin_strlen (location_t loc
, tree type
, tree arg
)
7354 if (!validate_arg (arg
, POINTER_TYPE
))
7358 tree len
= c_strlen (arg
, 0);
7361 return fold_convert_loc (loc
, type
, len
);
7367 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7370 fold_builtin_inf (location_t loc
, tree type
, int warn
)
7372 REAL_VALUE_TYPE real
;
7374 /* __builtin_inff is intended to be usable to define INFINITY on all
7375 targets. If an infinity is not available, INFINITY expands "to a
7376 positive constant of type float that overflows at translation
7377 time", footnote "In this case, using INFINITY will violate the
7378 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7379 Thus we pedwarn to ensure this constraint violation is
7381 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
7382 pedwarn (loc
, 0, "target format does not support infinity");
7385 return build_real (type
, real
);
7388 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7391 fold_builtin_nan (tree arg
, tree type
, int quiet
)
7393 REAL_VALUE_TYPE real
;
7396 if (!validate_arg (arg
, POINTER_TYPE
))
7398 str
= c_getstr (arg
);
7402 if (!real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
7405 return build_real (type
, real
);
7408 /* Return true if the floating point expression T has an integer value.
7409 We also allow +Inf, -Inf and NaN to be considered integer values. */
7412 integer_valued_real_p (tree t
)
7414 switch (TREE_CODE (t
))
7421 return integer_valued_real_p (TREE_OPERAND (t
, 0));
7426 return integer_valued_real_p (TREE_OPERAND (t
, 1));
7433 return integer_valued_real_p (TREE_OPERAND (t
, 0))
7434 && integer_valued_real_p (TREE_OPERAND (t
, 1));
7437 return integer_valued_real_p (TREE_OPERAND (t
, 1))
7438 && integer_valued_real_p (TREE_OPERAND (t
, 2));
7441 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
7445 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
7446 if (TREE_CODE (type
) == INTEGER_TYPE
)
7448 if (TREE_CODE (type
) == REAL_TYPE
)
7449 return integer_valued_real_p (TREE_OPERAND (t
, 0));
7454 switch (builtin_mathfn_code (t
))
7456 CASE_FLT_FN (BUILT_IN_CEIL
):
7457 CASE_FLT_FN (BUILT_IN_FLOOR
):
7458 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
7459 CASE_FLT_FN (BUILT_IN_RINT
):
7460 CASE_FLT_FN (BUILT_IN_ROUND
):
7461 CASE_FLT_FN (BUILT_IN_TRUNC
):
7464 CASE_FLT_FN (BUILT_IN_FMIN
):
7465 CASE_FLT_FN (BUILT_IN_FMAX
):
7466 return integer_valued_real_p (CALL_EXPR_ARG (t
, 0))
7467 && integer_valued_real_p (CALL_EXPR_ARG (t
, 1));
7480 /* FNDECL is assumed to be a builtin where truncation can be propagated
7481 across (for instance floor((double)f) == (double)floorf (f).
7482 Do the transformation for a call with argument ARG. */
7485 fold_trunc_transparent_mathfn (location_t loc
, tree fndecl
, tree arg
)
7487 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7489 if (!validate_arg (arg
, REAL_TYPE
))
7492 /* Integer rounding functions are idempotent. */
7493 if (fcode
== builtin_mathfn_code (arg
))
7496 /* If argument is already integer valued, and we don't need to worry
7497 about setting errno, there's no need to perform rounding. */
7498 if (! flag_errno_math
&& integer_valued_real_p (arg
))
7503 tree arg0
= strip_float_extensions (arg
);
7504 tree ftype
= TREE_TYPE (TREE_TYPE (fndecl
));
7505 tree newtype
= TREE_TYPE (arg0
);
7508 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
7509 && (decl
= mathfn_built_in (newtype
, fcode
)))
7510 return fold_convert_loc (loc
, ftype
,
7511 build_call_expr_loc (loc
, decl
, 1,
7512 fold_convert_loc (loc
,
7519 /* FNDECL is assumed to be builtin which can narrow the FP type of
7520 the argument, for instance lround((double)f) -> lroundf (f).
7521 Do the transformation for a call with argument ARG. */
7524 fold_fixed_mathfn (location_t loc
, tree fndecl
, tree arg
)
7526 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7528 if (!validate_arg (arg
, REAL_TYPE
))
7531 /* If argument is already integer valued, and we don't need to worry
7532 about setting errno, there's no need to perform rounding. */
7533 if (! flag_errno_math
&& integer_valued_real_p (arg
))
7534 return fold_build1_loc (loc
, FIX_TRUNC_EXPR
,
7535 TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
7539 tree ftype
= TREE_TYPE (arg
);
7540 tree arg0
= strip_float_extensions (arg
);
7541 tree newtype
= TREE_TYPE (arg0
);
7544 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
7545 && (decl
= mathfn_built_in (newtype
, fcode
)))
7546 return build_call_expr_loc (loc
, decl
, 1,
7547 fold_convert_loc (loc
, newtype
, arg0
));
7550 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7551 sizeof (int) == sizeof (long). */
7552 if (TYPE_PRECISION (integer_type_node
)
7553 == TYPE_PRECISION (long_integer_type_node
))
7555 tree newfn
= NULL_TREE
;
7558 CASE_FLT_FN (BUILT_IN_ICEIL
):
7559 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
7562 CASE_FLT_FN (BUILT_IN_IFLOOR
):
7563 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
7566 CASE_FLT_FN (BUILT_IN_IROUND
):
7567 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
7570 CASE_FLT_FN (BUILT_IN_IRINT
):
7571 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
7580 tree newcall
= build_call_expr_loc (loc
, newfn
, 1, arg
);
7581 return fold_convert_loc (loc
,
7582 TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
7586 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7587 sizeof (long long) == sizeof (long). */
7588 if (TYPE_PRECISION (long_long_integer_type_node
)
7589 == TYPE_PRECISION (long_integer_type_node
))
7591 tree newfn
= NULL_TREE
;
7594 CASE_FLT_FN (BUILT_IN_LLCEIL
):
7595 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
7598 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7599 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
7602 CASE_FLT_FN (BUILT_IN_LLROUND
):
7603 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
7606 CASE_FLT_FN (BUILT_IN_LLRINT
):
7607 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
7616 tree newcall
= build_call_expr_loc (loc
, newfn
, 1, arg
);
7617 return fold_convert_loc (loc
,
7618 TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
7625 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7626 return type. Return NULL_TREE if no simplification can be made. */
7629 fold_builtin_cabs (location_t loc
, tree arg
, tree type
, tree fndecl
)
7633 if (!validate_arg (arg
, COMPLEX_TYPE
)
7634 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
7637 /* Calculate the result when the argument is a constant. */
7638 if (TREE_CODE (arg
) == COMPLEX_CST
7639 && (res
= do_mpfr_arg2 (TREE_REALPART (arg
), TREE_IMAGPART (arg
),
7643 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
7645 tree real
= TREE_OPERAND (arg
, 0);
7646 tree imag
= TREE_OPERAND (arg
, 1);
7648 /* If either part is zero, cabs is fabs of the other. */
7649 if (real_zerop (real
))
7650 return fold_build1_loc (loc
, ABS_EXPR
, type
, imag
);
7651 if (real_zerop (imag
))
7652 return fold_build1_loc (loc
, ABS_EXPR
, type
, real
);
7654 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7655 if (flag_unsafe_math_optimizations
7656 && operand_equal_p (real
, imag
, OEP_PURE_SAME
))
7658 const REAL_VALUE_TYPE sqrt2_trunc
7659 = real_value_truncate (TYPE_MODE (type
), dconst_sqrt2 ());
7661 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7662 fold_build1_loc (loc
, ABS_EXPR
, type
, real
),
7663 build_real (type
, sqrt2_trunc
));
7667 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7668 if (TREE_CODE (arg
) == NEGATE_EXPR
7669 || TREE_CODE (arg
) == CONJ_EXPR
)
7670 return build_call_expr_loc (loc
, fndecl
, 1, TREE_OPERAND (arg
, 0));
7672 /* Don't do this when optimizing for size. */
7673 if (flag_unsafe_math_optimizations
7674 && optimize
&& optimize_function_for_speed_p (cfun
))
7676 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
7678 if (sqrtfn
!= NULL_TREE
)
7680 tree rpart
, ipart
, result
;
7682 arg
= builtin_save_expr (arg
);
7684 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, type
, arg
);
7685 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg
);
7687 rpart
= builtin_save_expr (rpart
);
7688 ipart
= builtin_save_expr (ipart
);
7690 result
= fold_build2_loc (loc
, PLUS_EXPR
, type
,
7691 fold_build2_loc (loc
, MULT_EXPR
, type
,
7693 fold_build2_loc (loc
, MULT_EXPR
, type
,
7696 return build_call_expr_loc (loc
, sqrtfn
, 1, result
);
7703 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7704 complex tree type of the result. If NEG is true, the imaginary
7705 zero is negative. */
7708 build_complex_cproj (tree type
, bool neg
)
7710 REAL_VALUE_TYPE rinf
, rzero
= dconst0
;
7714 return build_complex (type
, build_real (TREE_TYPE (type
), rinf
),
7715 build_real (TREE_TYPE (type
), rzero
));
7718 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7719 return type. Return NULL_TREE if no simplification can be made. */
7722 fold_builtin_cproj (location_t loc
, tree arg
, tree type
)
7724 if (!validate_arg (arg
, COMPLEX_TYPE
)
7725 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
7728 /* If there are no infinities, return arg. */
7729 if (! HONOR_INFINITIES (type
))
7730 return non_lvalue_loc (loc
, arg
);
7732 /* Calculate the result when the argument is a constant. */
7733 if (TREE_CODE (arg
) == COMPLEX_CST
)
7735 const REAL_VALUE_TYPE
*real
= TREE_REAL_CST_PTR (TREE_REALPART (arg
));
7736 const REAL_VALUE_TYPE
*imag
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg
));
7738 if (real_isinf (real
) || real_isinf (imag
))
7739 return build_complex_cproj (type
, imag
->sign
);
7743 else if (TREE_CODE (arg
) == COMPLEX_EXPR
)
7745 tree real
= TREE_OPERAND (arg
, 0);
7746 tree imag
= TREE_OPERAND (arg
, 1);
7751 /* If the real part is inf and the imag part is known to be
7752 nonnegative, return (inf + 0i). Remember side-effects are
7753 possible in the imag part. */
7754 if (TREE_CODE (real
) == REAL_CST
7755 && real_isinf (TREE_REAL_CST_PTR (real
))
7756 && tree_expr_nonnegative_p (imag
))
7757 return omit_one_operand_loc (loc
, type
,
7758 build_complex_cproj (type
, false),
7761 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7762 Remember side-effects are possible in the real part. */
7763 if (TREE_CODE (imag
) == REAL_CST
7764 && real_isinf (TREE_REAL_CST_PTR (imag
)))
7766 omit_one_operand_loc (loc
, type
,
7767 build_complex_cproj (type
, TREE_REAL_CST_PTR
7768 (imag
)->sign
), arg
);
7774 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7775 Return NULL_TREE if no simplification can be made. */
7778 fold_builtin_sqrt (location_t loc
, tree arg
, tree type
)
7781 enum built_in_function fcode
;
7784 if (!validate_arg (arg
, REAL_TYPE
))
7787 /* Calculate the result when the argument is a constant. */
7788 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_sqrt
, &dconst0
, NULL
, true)))
7791 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7792 fcode
= builtin_mathfn_code (arg
);
7793 if (flag_unsafe_math_optimizations
&& BUILTIN_EXPONENT_P (fcode
))
7795 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7796 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
7797 CALL_EXPR_ARG (arg
, 0),
7798 build_real (type
, dconsthalf
));
7799 return build_call_expr_loc (loc
, expfn
, 1, arg
);
7802 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7803 if (flag_unsafe_math_optimizations
&& BUILTIN_ROOT_P (fcode
))
7805 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7809 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7811 /* The inner root was either sqrt or cbrt. */
7812 /* This was a conditional expression but it triggered a bug
7814 REAL_VALUE_TYPE dconstroot
;
7815 if (BUILTIN_SQRT_P (fcode
))
7816 dconstroot
= dconsthalf
;
7818 dconstroot
= dconst_third ();
7820 /* Adjust for the outer root. */
7821 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7822 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7823 tree_root
= build_real (type
, dconstroot
);
7824 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7828 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7829 if (flag_unsafe_math_optimizations
7830 && (fcode
== BUILT_IN_POW
7831 || fcode
== BUILT_IN_POWF
7832 || fcode
== BUILT_IN_POWL
))
7834 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7835 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7836 tree arg1
= CALL_EXPR_ARG (arg
, 1);
7838 if (!tree_expr_nonnegative_p (arg0
))
7839 arg0
= build1 (ABS_EXPR
, type
, arg0
);
7840 narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
7841 build_real (type
, dconsthalf
));
7842 return build_call_expr_loc (loc
, powfn
, 2, arg0
, narg1
);
7848 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7849 Return NULL_TREE if no simplification can be made. */
7852 fold_builtin_cbrt (location_t loc
, tree arg
, tree type
)
7854 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
7857 if (!validate_arg (arg
, REAL_TYPE
))
7860 /* Calculate the result when the argument is a constant. */
7861 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cbrt
, NULL
, NULL
, 0)))
7864 if (flag_unsafe_math_optimizations
)
7866 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7867 if (BUILTIN_EXPONENT_P (fcode
))
7869 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7870 const REAL_VALUE_TYPE third_trunc
=
7871 real_value_truncate (TYPE_MODE (type
), dconst_third ());
7872 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
7873 CALL_EXPR_ARG (arg
, 0),
7874 build_real (type
, third_trunc
));
7875 return build_call_expr_loc (loc
, expfn
, 1, arg
);
7878 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7879 if (BUILTIN_SQRT_P (fcode
))
7881 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7885 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7887 REAL_VALUE_TYPE dconstroot
= dconst_third ();
7889 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7890 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7891 tree_root
= build_real (type
, dconstroot
);
7892 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7896 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7897 if (BUILTIN_CBRT_P (fcode
))
7899 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7900 if (tree_expr_nonnegative_p (arg0
))
7902 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7907 REAL_VALUE_TYPE dconstroot
;
7909 real_arithmetic (&dconstroot
, MULT_EXPR
,
7910 dconst_third_ptr (), dconst_third_ptr ());
7911 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7912 tree_root
= build_real (type
, dconstroot
);
7913 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7918 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7919 if (fcode
== BUILT_IN_POW
7920 || fcode
== BUILT_IN_POWF
7921 || fcode
== BUILT_IN_POWL
)
7923 tree arg00
= CALL_EXPR_ARG (arg
, 0);
7924 tree arg01
= CALL_EXPR_ARG (arg
, 1);
7925 if (tree_expr_nonnegative_p (arg00
))
7927 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7928 const REAL_VALUE_TYPE dconstroot
7929 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
7930 tree narg01
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg01
,
7931 build_real (type
, dconstroot
));
7932 return build_call_expr_loc (loc
, powfn
, 2, arg00
, narg01
);
7939 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7940 TYPE is the type of the return value. Return NULL_TREE if no
7941 simplification can be made. */
7944 fold_builtin_cos (location_t loc
,
7945 tree arg
, tree type
, tree fndecl
)
7949 if (!validate_arg (arg
, REAL_TYPE
))
7952 /* Calculate the result when the argument is a constant. */
7953 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cos
, NULL
, NULL
, 0)))
7956 /* Optimize cos(-x) into cos (x). */
7957 if ((narg
= fold_strip_sign_ops (arg
)))
7958 return build_call_expr_loc (loc
, fndecl
, 1, narg
);
7963 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7964 Return NULL_TREE if no simplification can be made. */
7967 fold_builtin_cosh (location_t loc
, tree arg
, tree type
, tree fndecl
)
7969 if (validate_arg (arg
, REAL_TYPE
))
7973 /* Calculate the result when the argument is a constant. */
7974 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cosh
, NULL
, NULL
, 0)))
7977 /* Optimize cosh(-x) into cosh (x). */
7978 if ((narg
= fold_strip_sign_ops (arg
)))
7979 return build_call_expr_loc (loc
, fndecl
, 1, narg
);
7985 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7986 argument ARG. TYPE is the type of the return value. Return
7987 NULL_TREE if no simplification can be made. */
7990 fold_builtin_ccos (location_t loc
, tree arg
, tree type
, tree fndecl
,
7993 if (validate_arg (arg
, COMPLEX_TYPE
)
7994 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
7998 /* Calculate the result when the argument is a constant. */
7999 if ((tmp
= do_mpc_arg1 (arg
, type
, (hyper
? mpc_cosh
: mpc_cos
))))
8002 /* Optimize fn(-x) into fn(x). */
8003 if ((tmp
= fold_strip_sign_ops (arg
)))
8004 return build_call_expr_loc (loc
, fndecl
, 1, tmp
);
8010 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
8011 Return NULL_TREE if no simplification can be made. */
8014 fold_builtin_tan (tree arg
, tree type
)
8016 enum built_in_function fcode
;
8019 if (!validate_arg (arg
, REAL_TYPE
))
8022 /* Calculate the result when the argument is a constant. */
8023 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_tan
, NULL
, NULL
, 0)))
8026 /* Optimize tan(atan(x)) = x. */
8027 fcode
= builtin_mathfn_code (arg
);
8028 if (flag_unsafe_math_optimizations
8029 && (fcode
== BUILT_IN_ATAN
8030 || fcode
== BUILT_IN_ATANF
8031 || fcode
== BUILT_IN_ATANL
))
8032 return CALL_EXPR_ARG (arg
, 0);
8037 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8038 NULL_TREE if no simplification can be made. */
8041 fold_builtin_sincos (location_t loc
,
8042 tree arg0
, tree arg1
, tree arg2
)
8047 if (!validate_arg (arg0
, REAL_TYPE
)
8048 || !validate_arg (arg1
, POINTER_TYPE
)
8049 || !validate_arg (arg2
, POINTER_TYPE
))
8052 type
= TREE_TYPE (arg0
);
8054 /* Calculate the result when the argument is a constant. */
8055 if ((res
= do_mpfr_sincos (arg0
, arg1
, arg2
)))
8058 /* Canonicalize sincos to cexpi. */
8059 if (!targetm
.libc_has_function (function_c99_math_complex
))
8061 fn
= mathfn_built_in (type
, BUILT_IN_CEXPI
);
8065 call
= build_call_expr_loc (loc
, fn
, 1, arg0
);
8066 call
= builtin_save_expr (call
);
8068 return build2 (COMPOUND_EXPR
, void_type_node
,
8069 build2 (MODIFY_EXPR
, void_type_node
,
8070 build_fold_indirect_ref_loc (loc
, arg1
),
8071 build1 (IMAGPART_EXPR
, type
, call
)),
8072 build2 (MODIFY_EXPR
, void_type_node
,
8073 build_fold_indirect_ref_loc (loc
, arg2
),
8074 build1 (REALPART_EXPR
, type
, call
)));
8077 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
8078 NULL_TREE if no simplification can be made. */
8081 fold_builtin_cexp (location_t loc
, tree arg0
, tree type
)
8084 tree realp
, imagp
, ifn
;
8087 if (!validate_arg (arg0
, COMPLEX_TYPE
)
8088 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) != REAL_TYPE
)
8091 /* Calculate the result when the argument is a constant. */
8092 if ((res
= do_mpc_arg1 (arg0
, type
, mpc_exp
)))
8095 rtype
= TREE_TYPE (TREE_TYPE (arg0
));
8097 /* In case we can figure out the real part of arg0 and it is constant zero
8099 if (!targetm
.libc_has_function (function_c99_math_complex
))
8101 ifn
= mathfn_built_in (rtype
, BUILT_IN_CEXPI
);
8105 if ((realp
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
))
8106 && real_zerop (realp
))
8108 tree narg
= fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
8109 return build_call_expr_loc (loc
, ifn
, 1, narg
);
8112 /* In case we can easily decompose real and imaginary parts split cexp
8113 to exp (r) * cexpi (i). */
8114 if (flag_unsafe_math_optimizations
8117 tree rfn
, rcall
, icall
;
8119 rfn
= mathfn_built_in (rtype
, BUILT_IN_EXP
);
8123 imagp
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
8127 icall
= build_call_expr_loc (loc
, ifn
, 1, imagp
);
8128 icall
= builtin_save_expr (icall
);
8129 rcall
= build_call_expr_loc (loc
, rfn
, 1, realp
);
8130 rcall
= builtin_save_expr (rcall
);
8131 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
8132 fold_build2_loc (loc
, MULT_EXPR
, rtype
,
8134 fold_build1_loc (loc
, REALPART_EXPR
,
8136 fold_build2_loc (loc
, MULT_EXPR
, rtype
,
8138 fold_build1_loc (loc
, IMAGPART_EXPR
,
8145 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
8146 Return NULL_TREE if no simplification can be made. */
8149 fold_builtin_trunc (location_t loc
, tree fndecl
, tree arg
)
8151 if (!validate_arg (arg
, REAL_TYPE
))
8154 /* Optimize trunc of constant value. */
8155 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8157 REAL_VALUE_TYPE r
, x
;
8158 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8160 x
= TREE_REAL_CST (arg
);
8161 real_trunc (&r
, TYPE_MODE (type
), &x
);
8162 return build_real (type
, r
);
8165 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
8168 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
8169 Return NULL_TREE if no simplification can be made. */
8172 fold_builtin_floor (location_t loc
, tree fndecl
, tree arg
)
8174 if (!validate_arg (arg
, REAL_TYPE
))
8177 /* Optimize floor of constant value. */
8178 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8182 x
= TREE_REAL_CST (arg
);
8183 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
8185 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8188 real_floor (&r
, TYPE_MODE (type
), &x
);
8189 return build_real (type
, r
);
8193 /* Fold floor (x) where x is nonnegative to trunc (x). */
8194 if (tree_expr_nonnegative_p (arg
))
8196 tree truncfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_TRUNC
);
8198 return build_call_expr_loc (loc
, truncfn
, 1, arg
);
8201 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
8204 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
8205 Return NULL_TREE if no simplification can be made. */
8208 fold_builtin_ceil (location_t loc
, tree fndecl
, tree arg
)
8210 if (!validate_arg (arg
, REAL_TYPE
))
8213 /* Optimize ceil of constant value. */
8214 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8218 x
= TREE_REAL_CST (arg
);
8219 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
8221 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8224 real_ceil (&r
, TYPE_MODE (type
), &x
);
8225 return build_real (type
, r
);
8229 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
8232 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8233 Return NULL_TREE if no simplification can be made. */
8236 fold_builtin_round (location_t loc
, tree fndecl
, tree arg
)
8238 if (!validate_arg (arg
, REAL_TYPE
))
8241 /* Optimize round of constant value. */
8242 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8246 x
= TREE_REAL_CST (arg
);
8247 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
8249 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8252 real_round (&r
, TYPE_MODE (type
), &x
);
8253 return build_real (type
, r
);
8257 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
8260 /* Fold function call to builtin lround, lroundf or lroundl (or the
8261 corresponding long long versions) and other rounding functions. ARG
8262 is the argument to the call. Return NULL_TREE if no simplification
8266 fold_builtin_int_roundingfn (location_t loc
, tree fndecl
, tree arg
)
8268 if (!validate_arg (arg
, REAL_TYPE
))
8271 /* Optimize lround of constant value. */
8272 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8274 const REAL_VALUE_TYPE x
= TREE_REAL_CST (arg
);
8276 if (real_isfinite (&x
))
8278 tree itype
= TREE_TYPE (TREE_TYPE (fndecl
));
8279 tree ftype
= TREE_TYPE (arg
);
8283 switch (DECL_FUNCTION_CODE (fndecl
))
8285 CASE_FLT_FN (BUILT_IN_IFLOOR
):
8286 CASE_FLT_FN (BUILT_IN_LFLOOR
):
8287 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
8288 real_floor (&r
, TYPE_MODE (ftype
), &x
);
8291 CASE_FLT_FN (BUILT_IN_ICEIL
):
8292 CASE_FLT_FN (BUILT_IN_LCEIL
):
8293 CASE_FLT_FN (BUILT_IN_LLCEIL
):
8294 real_ceil (&r
, TYPE_MODE (ftype
), &x
);
8297 CASE_FLT_FN (BUILT_IN_IROUND
):
8298 CASE_FLT_FN (BUILT_IN_LROUND
):
8299 CASE_FLT_FN (BUILT_IN_LLROUND
):
8300 real_round (&r
, TYPE_MODE (ftype
), &x
);
8307 wide_int val
= real_to_integer (&r
, &fail
, TYPE_PRECISION (itype
));
8309 return wide_int_to_tree (itype
, val
);
8313 switch (DECL_FUNCTION_CODE (fndecl
))
8315 CASE_FLT_FN (BUILT_IN_LFLOOR
):
8316 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
8317 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8318 if (tree_expr_nonnegative_p (arg
))
8319 return fold_build1_loc (loc
, FIX_TRUNC_EXPR
,
8320 TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
8325 return fold_fixed_mathfn (loc
, fndecl
, arg
);
8328 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8329 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8330 the argument to the call. Return NULL_TREE if no simplification can
8334 fold_builtin_bitop (tree fndecl
, tree arg
)
8336 if (!validate_arg (arg
, INTEGER_TYPE
))
8339 /* Optimize for constant argument. */
8340 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
8342 tree type
= TREE_TYPE (arg
);
8345 switch (DECL_FUNCTION_CODE (fndecl
))
8347 CASE_INT_FN (BUILT_IN_FFS
):
8348 result
= wi::ffs (arg
);
8351 CASE_INT_FN (BUILT_IN_CLZ
):
8352 if (wi::ne_p (arg
, 0))
8353 result
= wi::clz (arg
);
8354 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
8355 result
= TYPE_PRECISION (type
);
8358 CASE_INT_FN (BUILT_IN_CTZ
):
8359 if (wi::ne_p (arg
, 0))
8360 result
= wi::ctz (arg
);
8361 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
8362 result
= TYPE_PRECISION (type
);
8365 CASE_INT_FN (BUILT_IN_CLRSB
):
8366 result
= wi::clrsb (arg
);
8369 CASE_INT_FN (BUILT_IN_POPCOUNT
):
8370 result
= wi::popcount (arg
);
8373 CASE_INT_FN (BUILT_IN_PARITY
):
8374 result
= wi::parity (arg
);
8381 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), result
);
8387 /* Fold function call to builtin_bswap and the short, long and long long
8388 variants. Return NULL_TREE if no simplification can be made. */
8390 fold_builtin_bswap (tree fndecl
, tree arg
)
8392 if (! validate_arg (arg
, INTEGER_TYPE
))
8395 /* Optimize constant value. */
8396 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
8398 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8400 switch (DECL_FUNCTION_CODE (fndecl
))
8402 case BUILT_IN_BSWAP16
:
8403 case BUILT_IN_BSWAP32
:
8404 case BUILT_IN_BSWAP64
:
8406 signop sgn
= TYPE_SIGN (type
);
8408 wide_int_to_tree (type
,
8409 wide_int::from (arg
, TYPE_PRECISION (type
),
8421 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8422 NULL_TREE if no simplification can be made. */
8425 fold_builtin_hypot (location_t loc
, tree fndecl
,
8426 tree arg0
, tree arg1
, tree type
)
8428 tree res
, narg0
, narg1
;
8430 if (!validate_arg (arg0
, REAL_TYPE
)
8431 || !validate_arg (arg1
, REAL_TYPE
))
8434 /* Calculate the result when the argument is a constant. */
8435 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_hypot
)))
8438 /* If either argument to hypot has a negate or abs, strip that off.
8439 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8440 narg0
= fold_strip_sign_ops (arg0
);
8441 narg1
= fold_strip_sign_ops (arg1
);
8444 return build_call_expr_loc (loc
, fndecl
, 2, narg0
? narg0
: arg0
,
8445 narg1
? narg1
: arg1
);
8448 /* If either argument is zero, hypot is fabs of the other. */
8449 if (real_zerop (arg0
))
8450 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg1
);
8451 else if (real_zerop (arg1
))
8452 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg0
);
8454 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8455 if (flag_unsafe_math_optimizations
8456 && operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
8458 const REAL_VALUE_TYPE sqrt2_trunc
8459 = real_value_truncate (TYPE_MODE (type
), dconst_sqrt2 ());
8460 return fold_build2_loc (loc
, MULT_EXPR
, type
,
8461 fold_build1_loc (loc
, ABS_EXPR
, type
, arg0
),
8462 build_real (type
, sqrt2_trunc
));
8469 /* Fold a builtin function call to pow, powf, or powl. Return
8470 NULL_TREE if no simplification can be made. */
8472 fold_builtin_pow (location_t loc
, tree fndecl
, tree arg0
, tree arg1
, tree type
)
8476 if (!validate_arg (arg0
, REAL_TYPE
)
8477 || !validate_arg (arg1
, REAL_TYPE
))
8480 /* Calculate the result when the argument is a constant. */
8481 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_pow
)))
8484 /* Optimize pow(1.0,y) = 1.0. */
8485 if (real_onep (arg0
))
8486 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
), arg1
);
8488 if (TREE_CODE (arg1
) == REAL_CST
8489 && !TREE_OVERFLOW (arg1
))
8491 REAL_VALUE_TYPE cint
;
8495 c
= TREE_REAL_CST (arg1
);
8497 /* Optimize pow(x,0.0) = 1.0. */
8498 if (REAL_VALUES_EQUAL (c
, dconst0
))
8499 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
),
8502 /* Optimize pow(x,1.0) = x. */
8503 if (REAL_VALUES_EQUAL (c
, dconst1
))
8506 /* Optimize pow(x,-1.0) = 1.0/x. */
8507 if (REAL_VALUES_EQUAL (c
, dconstm1
))
8508 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
8509 build_real (type
, dconst1
), arg0
);
8511 /* Optimize pow(x,0.5) = sqrt(x). */
8512 if (flag_unsafe_math_optimizations
8513 && REAL_VALUES_EQUAL (c
, dconsthalf
))
8515 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
8517 if (sqrtfn
!= NULL_TREE
)
8518 return build_call_expr_loc (loc
, sqrtfn
, 1, arg0
);
8521 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8522 if (flag_unsafe_math_optimizations
)
8524 const REAL_VALUE_TYPE dconstroot
8525 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
8527 if (REAL_VALUES_EQUAL (c
, dconstroot
))
8529 tree cbrtfn
= mathfn_built_in (type
, BUILT_IN_CBRT
);
8530 if (cbrtfn
!= NULL_TREE
)
8531 return build_call_expr_loc (loc
, cbrtfn
, 1, arg0
);
8535 /* Check for an integer exponent. */
8536 n
= real_to_integer (&c
);
8537 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
8538 if (real_identical (&c
, &cint
))
8540 /* Attempt to evaluate pow at compile-time, unless this should
8541 raise an exception. */
8542 if (TREE_CODE (arg0
) == REAL_CST
8543 && !TREE_OVERFLOW (arg0
)
8545 || (!flag_trapping_math
&& !flag_errno_math
)
8546 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0
), dconst0
)))
8551 x
= TREE_REAL_CST (arg0
);
8552 inexact
= real_powi (&x
, TYPE_MODE (type
), &x
, n
);
8553 if (flag_unsafe_math_optimizations
|| !inexact
)
8554 return build_real (type
, x
);
8557 /* Strip sign ops from even integer powers. */
8558 if ((n
& 1) == 0 && flag_unsafe_math_optimizations
)
8560 tree narg0
= fold_strip_sign_ops (arg0
);
8562 return build_call_expr_loc (loc
, fndecl
, 2, narg0
, arg1
);
8567 if (flag_unsafe_math_optimizations
)
8569 const enum built_in_function fcode
= builtin_mathfn_code (arg0
);
8571 /* Optimize pow(expN(x),y) = expN(x*y). */
8572 if (BUILTIN_EXPONENT_P (fcode
))
8574 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
8575 tree arg
= CALL_EXPR_ARG (arg0
, 0);
8576 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg
, arg1
);
8577 return build_call_expr_loc (loc
, expfn
, 1, arg
);
8580 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8581 if (BUILTIN_SQRT_P (fcode
))
8583 tree narg0
= CALL_EXPR_ARG (arg0
, 0);
8584 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
8585 build_real (type
, dconsthalf
));
8586 return build_call_expr_loc (loc
, fndecl
, 2, narg0
, narg1
);
8589 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8590 if (BUILTIN_CBRT_P (fcode
))
8592 tree arg
= CALL_EXPR_ARG (arg0
, 0);
8593 if (tree_expr_nonnegative_p (arg
))
8595 const REAL_VALUE_TYPE dconstroot
8596 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
8597 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
8598 build_real (type
, dconstroot
));
8599 return build_call_expr_loc (loc
, fndecl
, 2, arg
, narg1
);
8603 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8604 if (fcode
== BUILT_IN_POW
8605 || fcode
== BUILT_IN_POWF
8606 || fcode
== BUILT_IN_POWL
)
8608 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
8609 if (tree_expr_nonnegative_p (arg00
))
8611 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
8612 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg01
, arg1
);
8613 return build_call_expr_loc (loc
, fndecl
, 2, arg00
, narg1
);
8621 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8622 Return NULL_TREE if no simplification can be made. */
8624 fold_builtin_powi (location_t loc
, tree fndecl ATTRIBUTE_UNUSED
,
8625 tree arg0
, tree arg1
, tree type
)
8627 if (!validate_arg (arg0
, REAL_TYPE
)
8628 || !validate_arg (arg1
, INTEGER_TYPE
))
8631 /* Optimize pow(1.0,y) = 1.0. */
8632 if (real_onep (arg0
))
8633 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
), arg1
);
8635 if (tree_fits_shwi_p (arg1
))
8637 HOST_WIDE_INT c
= tree_to_shwi (arg1
);
8639 /* Evaluate powi at compile-time. */
8640 if (TREE_CODE (arg0
) == REAL_CST
8641 && !TREE_OVERFLOW (arg0
))
8644 x
= TREE_REAL_CST (arg0
);
8645 real_powi (&x
, TYPE_MODE (type
), &x
, c
);
8646 return build_real (type
, x
);
8649 /* Optimize pow(x,0) = 1.0. */
8651 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
),
8654 /* Optimize pow(x,1) = x. */
8658 /* Optimize pow(x,-1) = 1.0/x. */
8660 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
8661 build_real (type
, dconst1
), arg0
);
8667 /* A subroutine of fold_builtin to fold the various exponent
8668 functions. Return NULL_TREE if no simplification can be made.
8669 FUNC is the corresponding MPFR exponent function. */
8672 fold_builtin_exponent (location_t loc
, tree fndecl
, tree arg
,
8673 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
))
8675 if (validate_arg (arg
, REAL_TYPE
))
8677 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8680 /* Calculate the result when the argument is a constant. */
8681 if ((res
= do_mpfr_arg1 (arg
, type
, func
, NULL
, NULL
, 0)))
8684 /* Optimize expN(logN(x)) = x. */
8685 if (flag_unsafe_math_optimizations
)
8687 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
8689 if ((func
== mpfr_exp
8690 && (fcode
== BUILT_IN_LOG
8691 || fcode
== BUILT_IN_LOGF
8692 || fcode
== BUILT_IN_LOGL
))
8693 || (func
== mpfr_exp2
8694 && (fcode
== BUILT_IN_LOG2
8695 || fcode
== BUILT_IN_LOG2F
8696 || fcode
== BUILT_IN_LOG2L
))
8697 || (func
== mpfr_exp10
8698 && (fcode
== BUILT_IN_LOG10
8699 || fcode
== BUILT_IN_LOG10F
8700 || fcode
== BUILT_IN_LOG10L
)))
8701 return fold_convert_loc (loc
, type
, CALL_EXPR_ARG (arg
, 0));
8708 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8709 arguments to the call, and TYPE is its return type.
8710 Return NULL_TREE if no simplification can be made. */
8713 fold_builtin_memchr (location_t loc
, tree arg1
, tree arg2
, tree len
, tree type
)
8715 if (!validate_arg (arg1
, POINTER_TYPE
)
8716 || !validate_arg (arg2
, INTEGER_TYPE
)
8717 || !validate_arg (len
, INTEGER_TYPE
))
8723 if (TREE_CODE (arg2
) != INTEGER_CST
8724 || !tree_fits_uhwi_p (len
))
8727 p1
= c_getstr (arg1
);
8728 if (p1
&& compare_tree_int (len
, strlen (p1
) + 1) <= 0)
8734 if (target_char_cast (arg2
, &c
))
8737 r
= (const char *) memchr (p1
, c
, tree_to_uhwi (len
));
8740 return build_int_cst (TREE_TYPE (arg1
), 0);
8742 tem
= fold_build_pointer_plus_hwi_loc (loc
, arg1
, r
- p1
);
8743 return fold_convert_loc (loc
, type
, tem
);
8749 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8750 Return NULL_TREE if no simplification can be made. */
8753 fold_builtin_memcmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
8755 const char *p1
, *p2
;
8757 if (!validate_arg (arg1
, POINTER_TYPE
)
8758 || !validate_arg (arg2
, POINTER_TYPE
)
8759 || !validate_arg (len
, INTEGER_TYPE
))
8762 /* If the LEN parameter is zero, return zero. */
8763 if (integer_zerop (len
))
8764 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
8767 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8768 if (operand_equal_p (arg1
, arg2
, 0))
8769 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
8771 p1
= c_getstr (arg1
);
8772 p2
= c_getstr (arg2
);
8774 /* If all arguments are constant, and the value of len is not greater
8775 than the lengths of arg1 and arg2, evaluate at compile-time. */
8776 if (tree_fits_uhwi_p (len
) && p1
&& p2
8777 && compare_tree_int (len
, strlen (p1
) + 1) <= 0
8778 && compare_tree_int (len
, strlen (p2
) + 1) <= 0)
8780 const int r
= memcmp (p1
, p2
, tree_to_uhwi (len
));
8783 return integer_one_node
;
8785 return integer_minus_one_node
;
8787 return integer_zero_node
;
8790 /* If len parameter is one, return an expression corresponding to
8791 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8792 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
8794 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8795 tree cst_uchar_ptr_node
8796 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8799 = fold_convert_loc (loc
, integer_type_node
,
8800 build1 (INDIRECT_REF
, cst_uchar_node
,
8801 fold_convert_loc (loc
,
8805 = fold_convert_loc (loc
, integer_type_node
,
8806 build1 (INDIRECT_REF
, cst_uchar_node
,
8807 fold_convert_loc (loc
,
8810 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8816 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8817 Return NULL_TREE if no simplification can be made. */
8820 fold_builtin_strcmp (location_t loc
, tree arg1
, tree arg2
)
8822 const char *p1
, *p2
;
8824 if (!validate_arg (arg1
, POINTER_TYPE
)
8825 || !validate_arg (arg2
, POINTER_TYPE
))
8828 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8829 if (operand_equal_p (arg1
, arg2
, 0))
8830 return integer_zero_node
;
8832 p1
= c_getstr (arg1
);
8833 p2
= c_getstr (arg2
);
8837 const int i
= strcmp (p1
, p2
);
8839 return integer_minus_one_node
;
8841 return integer_one_node
;
8843 return integer_zero_node
;
8846 /* If the second arg is "", return *(const unsigned char*)arg1. */
8847 if (p2
&& *p2
== '\0')
8849 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8850 tree cst_uchar_ptr_node
8851 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8853 return fold_convert_loc (loc
, integer_type_node
,
8854 build1 (INDIRECT_REF
, cst_uchar_node
,
8855 fold_convert_loc (loc
,
8860 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8861 if (p1
&& *p1
== '\0')
8863 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8864 tree cst_uchar_ptr_node
8865 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8868 = fold_convert_loc (loc
, integer_type_node
,
8869 build1 (INDIRECT_REF
, cst_uchar_node
,
8870 fold_convert_loc (loc
,
8873 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
8879 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8880 Return NULL_TREE if no simplification can be made. */
8883 fold_builtin_strncmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
8885 const char *p1
, *p2
;
8887 if (!validate_arg (arg1
, POINTER_TYPE
)
8888 || !validate_arg (arg2
, POINTER_TYPE
)
8889 || !validate_arg (len
, INTEGER_TYPE
))
8892 /* If the LEN parameter is zero, return zero. */
8893 if (integer_zerop (len
))
8894 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
8897 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8898 if (operand_equal_p (arg1
, arg2
, 0))
8899 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
8901 p1
= c_getstr (arg1
);
8902 p2
= c_getstr (arg2
);
8904 if (tree_fits_uhwi_p (len
) && p1
&& p2
)
8906 const int i
= strncmp (p1
, p2
, tree_to_uhwi (len
));
8908 return integer_one_node
;
8910 return integer_minus_one_node
;
8912 return integer_zero_node
;
8915 /* If the second arg is "", and the length is greater than zero,
8916 return *(const unsigned char*)arg1. */
8917 if (p2
&& *p2
== '\0'
8918 && TREE_CODE (len
) == INTEGER_CST
8919 && tree_int_cst_sgn (len
) == 1)
8921 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8922 tree cst_uchar_ptr_node
8923 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8925 return fold_convert_loc (loc
, integer_type_node
,
8926 build1 (INDIRECT_REF
, cst_uchar_node
,
8927 fold_convert_loc (loc
,
8932 /* If the first arg is "", and the length is greater than zero,
8933 return -*(const unsigned char*)arg2. */
8934 if (p1
&& *p1
== '\0'
8935 && TREE_CODE (len
) == INTEGER_CST
8936 && tree_int_cst_sgn (len
) == 1)
8938 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8939 tree cst_uchar_ptr_node
8940 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8942 tree temp
= fold_convert_loc (loc
, integer_type_node
,
8943 build1 (INDIRECT_REF
, cst_uchar_node
,
8944 fold_convert_loc (loc
,
8947 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
8950 /* If len parameter is one, return an expression corresponding to
8951 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8952 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
8954 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8955 tree cst_uchar_ptr_node
8956 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8958 tree ind1
= fold_convert_loc (loc
, integer_type_node
,
8959 build1 (INDIRECT_REF
, cst_uchar_node
,
8960 fold_convert_loc (loc
,
8963 tree ind2
= fold_convert_loc (loc
, integer_type_node
,
8964 build1 (INDIRECT_REF
, cst_uchar_node
,
8965 fold_convert_loc (loc
,
8968 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8974 /* Fold function call to builtin signbit, signbitf or signbitl with argument
8975 ARG. Return NULL_TREE if no simplification can be made. */
8978 fold_builtin_signbit (location_t loc
, tree arg
, tree type
)
8980 if (!validate_arg (arg
, REAL_TYPE
))
8983 /* If ARG is a compile-time constant, determine the result. */
8984 if (TREE_CODE (arg
) == REAL_CST
8985 && !TREE_OVERFLOW (arg
))
8989 c
= TREE_REAL_CST (arg
);
8990 return (REAL_VALUE_NEGATIVE (c
)
8991 ? build_one_cst (type
)
8992 : build_zero_cst (type
));
8995 /* If ARG is non-negative, the result is always zero. */
8996 if (tree_expr_nonnegative_p (arg
))
8997 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
8999 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
9000 if (!HONOR_SIGNED_ZEROS (arg
))
9001 return fold_convert (type
,
9002 fold_build2_loc (loc
, LT_EXPR
, boolean_type_node
, arg
,
9003 build_real (TREE_TYPE (arg
), dconst0
)));
9008 /* Fold function call to builtin copysign, copysignf or copysignl with
9009 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9013 fold_builtin_copysign (location_t loc
, tree fndecl
,
9014 tree arg1
, tree arg2
, tree type
)
9018 if (!validate_arg (arg1
, REAL_TYPE
)
9019 || !validate_arg (arg2
, REAL_TYPE
))
9022 /* copysign(X,X) is X. */
9023 if (operand_equal_p (arg1
, arg2
, 0))
9024 return fold_convert_loc (loc
, type
, arg1
);
9026 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9027 if (TREE_CODE (arg1
) == REAL_CST
9028 && TREE_CODE (arg2
) == REAL_CST
9029 && !TREE_OVERFLOW (arg1
)
9030 && !TREE_OVERFLOW (arg2
))
9032 REAL_VALUE_TYPE c1
, c2
;
9034 c1
= TREE_REAL_CST (arg1
);
9035 c2
= TREE_REAL_CST (arg2
);
9036 /* c1.sign := c2.sign. */
9037 real_copysign (&c1
, &c2
);
9038 return build_real (type
, c1
);
9041 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9042 Remember to evaluate Y for side-effects. */
9043 if (tree_expr_nonnegative_p (arg2
))
9044 return omit_one_operand_loc (loc
, type
,
9045 fold_build1_loc (loc
, ABS_EXPR
, type
, arg1
),
9048 /* Strip sign changing operations for the first argument. */
9049 tem
= fold_strip_sign_ops (arg1
);
9051 return build_call_expr_loc (loc
, fndecl
, 2, tem
, arg2
);
9056 /* Fold a call to builtin isascii with argument ARG. */
9059 fold_builtin_isascii (location_t loc
, tree arg
)
9061 if (!validate_arg (arg
, INTEGER_TYPE
))
9065 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9066 arg
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
9067 build_int_cst (integer_type_node
,
9068 ~ (unsigned HOST_WIDE_INT
) 0x7f));
9069 return fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
,
9070 arg
, integer_zero_node
);
9074 /* Fold a call to builtin toascii with argument ARG. */
9077 fold_builtin_toascii (location_t loc
, tree arg
)
9079 if (!validate_arg (arg
, INTEGER_TYPE
))
9082 /* Transform toascii(c) -> (c & 0x7f). */
9083 return fold_build2_loc (loc
, BIT_AND_EXPR
, integer_type_node
, arg
,
9084 build_int_cst (integer_type_node
, 0x7f));
9087 /* Fold a call to builtin isdigit with argument ARG. */
9090 fold_builtin_isdigit (location_t loc
, tree arg
)
9092 if (!validate_arg (arg
, INTEGER_TYPE
))
9096 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9097 /* According to the C standard, isdigit is unaffected by locale.
9098 However, it definitely is affected by the target character set. */
9099 unsigned HOST_WIDE_INT target_digit0
9100 = lang_hooks
.to_target_charset ('0');
9102 if (target_digit0
== 0)
9105 arg
= fold_convert_loc (loc
, unsigned_type_node
, arg
);
9106 arg
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
9107 build_int_cst (unsigned_type_node
, target_digit0
));
9108 return fold_build2_loc (loc
, LE_EXPR
, integer_type_node
, arg
,
9109 build_int_cst (unsigned_type_node
, 9));
9113 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9116 fold_builtin_fabs (location_t loc
, tree arg
, tree type
)
9118 if (!validate_arg (arg
, REAL_TYPE
))
9121 arg
= fold_convert_loc (loc
, type
, arg
);
9122 if (TREE_CODE (arg
) == REAL_CST
)
9123 return fold_abs_const (arg
, type
);
9124 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
9127 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9130 fold_builtin_abs (location_t loc
, tree arg
, tree type
)
9132 if (!validate_arg (arg
, INTEGER_TYPE
))
9135 arg
= fold_convert_loc (loc
, type
, arg
);
9136 if (TREE_CODE (arg
) == INTEGER_CST
)
9137 return fold_abs_const (arg
, type
);
9138 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
9141 /* Fold a fma operation with arguments ARG[012]. */
9144 fold_fma (location_t loc ATTRIBUTE_UNUSED
,
9145 tree type
, tree arg0
, tree arg1
, tree arg2
)
9147 if (TREE_CODE (arg0
) == REAL_CST
9148 && TREE_CODE (arg1
) == REAL_CST
9149 && TREE_CODE (arg2
) == REAL_CST
)
9150 return do_mpfr_arg3 (arg0
, arg1
, arg2
, type
, mpfr_fma
);
9155 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9158 fold_builtin_fma (location_t loc
, tree arg0
, tree arg1
, tree arg2
, tree type
)
9160 if (validate_arg (arg0
, REAL_TYPE
)
9161 && validate_arg (arg1
, REAL_TYPE
)
9162 && validate_arg (arg2
, REAL_TYPE
))
9164 tree tem
= fold_fma (loc
, type
, arg0
, arg1
, arg2
);
9168 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9169 if (optab_handler (fma_optab
, TYPE_MODE (type
)) != CODE_FOR_nothing
)
9170 return fold_build3_loc (loc
, FMA_EXPR
, type
, arg0
, arg1
, arg2
);
9175 /* Fold a call to builtin fmin or fmax. */
9178 fold_builtin_fmin_fmax (location_t loc
, tree arg0
, tree arg1
,
9179 tree type
, bool max
)
9181 if (validate_arg (arg0
, REAL_TYPE
) && validate_arg (arg1
, REAL_TYPE
))
9183 /* Calculate the result when the argument is a constant. */
9184 tree res
= do_mpfr_arg2 (arg0
, arg1
, type
, (max
? mpfr_max
: mpfr_min
));
9189 /* If either argument is NaN, return the other one. Avoid the
9190 transformation if we get (and honor) a signalling NaN. Using
9191 omit_one_operand() ensures we create a non-lvalue. */
9192 if (TREE_CODE (arg0
) == REAL_CST
9193 && real_isnan (&TREE_REAL_CST (arg0
))
9194 && (! HONOR_SNANS (arg0
)
9195 || ! TREE_REAL_CST (arg0
).signalling
))
9196 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
9197 if (TREE_CODE (arg1
) == REAL_CST
9198 && real_isnan (&TREE_REAL_CST (arg1
))
9199 && (! HONOR_SNANS (arg1
)
9200 || ! TREE_REAL_CST (arg1
).signalling
))
9201 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9203 /* Transform fmin/fmax(x,x) -> x. */
9204 if (operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
9205 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9207 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9208 functions to return the numeric arg if the other one is NaN.
9209 These tree codes don't honor that, so only transform if
9210 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9211 handled, so we don't have to worry about it either. */
9212 if (flag_finite_math_only
)
9213 return fold_build2_loc (loc
, (max
? MAX_EXPR
: MIN_EXPR
), type
,
9214 fold_convert_loc (loc
, type
, arg0
),
9215 fold_convert_loc (loc
, type
, arg1
));
9220 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9223 fold_builtin_carg (location_t loc
, tree arg
, tree type
)
9225 if (validate_arg (arg
, COMPLEX_TYPE
)
9226 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
9228 tree atan2_fn
= mathfn_built_in (type
, BUILT_IN_ATAN2
);
9232 tree new_arg
= builtin_save_expr (arg
);
9233 tree r_arg
= fold_build1_loc (loc
, REALPART_EXPR
, type
, new_arg
);
9234 tree i_arg
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, new_arg
);
9235 return build_call_expr_loc (loc
, atan2_fn
, 2, i_arg
, r_arg
);
9242 /* Fold a call to builtin logb/ilogb. */
9245 fold_builtin_logb (location_t loc
, tree arg
, tree rettype
)
9247 if (! validate_arg (arg
, REAL_TYPE
))
9252 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_OVERFLOW (arg
))
9254 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg
);
9260 /* If arg is Inf or NaN and we're logb, return it. */
9261 if (TREE_CODE (rettype
) == REAL_TYPE
)
9263 /* For logb(-Inf) we have to return +Inf. */
9264 if (real_isinf (value
) && real_isneg (value
))
9266 REAL_VALUE_TYPE tem
;
9268 return build_real (rettype
, tem
);
9270 return fold_convert_loc (loc
, rettype
, arg
);
9272 /* Fall through... */
9274 /* Zero may set errno and/or raise an exception for logb, also
9275 for ilogb we don't know FP_ILOGB0. */
9278 /* For normal numbers, proceed iff radix == 2. In GCC,
9279 normalized significands are in the range [0.5, 1.0). We
9280 want the exponent as if they were [1.0, 2.0) so get the
9281 exponent and subtract 1. */
9282 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg
)))->b
== 2)
9283 return fold_convert_loc (loc
, rettype
,
9284 build_int_cst (integer_type_node
,
9285 REAL_EXP (value
)-1));
9293 /* Fold a call to builtin significand, if radix == 2. */
9296 fold_builtin_significand (location_t loc
, tree arg
, tree rettype
)
9298 if (! validate_arg (arg
, REAL_TYPE
))
9303 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_OVERFLOW (arg
))
9305 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg
);
9312 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9313 return fold_convert_loc (loc
, rettype
, arg
);
9315 /* For normal numbers, proceed iff radix == 2. */
9316 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg
)))->b
== 2)
9318 REAL_VALUE_TYPE result
= *value
;
9319 /* In GCC, normalized significands are in the range [0.5,
9320 1.0). We want them to be [1.0, 2.0) so set the
9322 SET_REAL_EXP (&result
, 1);
9323 return build_real (rettype
, result
);
9332 /* Fold a call to builtin frexp, we can assume the base is 2. */
9335 fold_builtin_frexp (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
9337 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
9342 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
9345 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
9347 /* Proceed if a valid pointer type was passed in. */
9348 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == integer_type_node
)
9350 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9356 /* For +-0, return (*exp = 0, +-0). */
9357 exp
= integer_zero_node
;
9362 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9363 return omit_one_operand_loc (loc
, rettype
, arg0
, arg1
);
9366 /* Since the frexp function always expects base 2, and in
9367 GCC normalized significands are already in the range
9368 [0.5, 1.0), we have exactly what frexp wants. */
9369 REAL_VALUE_TYPE frac_rvt
= *value
;
9370 SET_REAL_EXP (&frac_rvt
, 0);
9371 frac
= build_real (rettype
, frac_rvt
);
9372 exp
= build_int_cst (integer_type_node
, REAL_EXP (value
));
9379 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9380 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
, exp
);
9381 TREE_SIDE_EFFECTS (arg1
) = 1;
9382 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
, frac
);
9388 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9389 then we can assume the base is two. If it's false, then we have to
9390 check the mode of the TYPE parameter in certain cases. */
9393 fold_builtin_load_exponent (location_t loc
, tree arg0
, tree arg1
,
9394 tree type
, bool ldexp
)
9396 if (validate_arg (arg0
, REAL_TYPE
) && validate_arg (arg1
, INTEGER_TYPE
))
9401 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9402 if (real_zerop (arg0
) || integer_zerop (arg1
)
9403 || (TREE_CODE (arg0
) == REAL_CST
9404 && !real_isfinite (&TREE_REAL_CST (arg0
))))
9405 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9407 /* If both arguments are constant, then try to evaluate it. */
9408 if ((ldexp
|| REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2)
9409 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
9410 && tree_fits_shwi_p (arg1
))
9412 /* Bound the maximum adjustment to twice the range of the
9413 mode's valid exponents. Use abs to ensure the range is
9414 positive as a sanity check. */
9415 const long max_exp_adj
= 2 *
9416 labs (REAL_MODE_FORMAT (TYPE_MODE (type
))->emax
9417 - REAL_MODE_FORMAT (TYPE_MODE (type
))->emin
);
9419 /* Get the user-requested adjustment. */
9420 const HOST_WIDE_INT req_exp_adj
= tree_to_shwi (arg1
);
9422 /* The requested adjustment must be inside this range. This
9423 is a preliminary cap to avoid things like overflow, we
9424 may still fail to compute the result for other reasons. */
9425 if (-max_exp_adj
< req_exp_adj
&& req_exp_adj
< max_exp_adj
)
9427 REAL_VALUE_TYPE initial_result
;
9429 real_ldexp (&initial_result
, &TREE_REAL_CST (arg0
), req_exp_adj
);
9431 /* Ensure we didn't overflow. */
9432 if (! real_isinf (&initial_result
))
9434 const REAL_VALUE_TYPE trunc_result
9435 = real_value_truncate (TYPE_MODE (type
), initial_result
);
9437 /* Only proceed if the target mode can hold the
9439 if (REAL_VALUES_EQUAL (initial_result
, trunc_result
))
9440 return build_real (type
, trunc_result
);
9449 /* Fold a call to builtin modf. */
9452 fold_builtin_modf (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
9454 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
9459 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
9462 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
9464 /* Proceed if a valid pointer type was passed in. */
9465 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == TYPE_MAIN_VARIANT (rettype
))
9467 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9468 REAL_VALUE_TYPE trunc
, frac
;
9474 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9475 trunc
= frac
= *value
;
9478 /* For +-Inf, return (*arg1 = arg0, +-0). */
9480 frac
.sign
= value
->sign
;
9484 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9485 real_trunc (&trunc
, VOIDmode
, value
);
9486 real_arithmetic (&frac
, MINUS_EXPR
, value
, &trunc
);
9487 /* If the original number was negative and already
9488 integral, then the fractional part is -0.0. */
9489 if (value
->sign
&& frac
.cl
== rvc_zero
)
9490 frac
.sign
= value
->sign
;
9494 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9495 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
,
9496 build_real (rettype
, trunc
));
9497 TREE_SIDE_EFFECTS (arg1
) = 1;
9498 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
,
9499 build_real (rettype
, frac
));
9505 /* Given a location LOC, an interclass builtin function decl FNDECL
9506 and its single argument ARG, return an folded expression computing
9507 the same, or NULL_TREE if we either couldn't or didn't want to fold
9508 (the latter happen if there's an RTL instruction available). */
9511 fold_builtin_interclass_mathfn (location_t loc
, tree fndecl
, tree arg
)
9515 if (!validate_arg (arg
, REAL_TYPE
))
9518 if (interclass_mathfn_icode (arg
, fndecl
) != CODE_FOR_nothing
)
9521 mode
= TYPE_MODE (TREE_TYPE (arg
));
9523 /* If there is no optab, try generic code. */
9524 switch (DECL_FUNCTION_CODE (fndecl
))
9528 CASE_FLT_FN (BUILT_IN_ISINF
):
9530 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9531 tree
const isgr_fn
= builtin_decl_explicit (BUILT_IN_ISGREATER
);
9532 tree
const type
= TREE_TYPE (arg
);
9536 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9537 real_from_string (&r
, buf
);
9538 result
= build_call_expr (isgr_fn
, 2,
9539 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
9540 build_real (type
, r
));
9543 CASE_FLT_FN (BUILT_IN_FINITE
):
9544 case BUILT_IN_ISFINITE
:
9546 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9547 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
9548 tree
const type
= TREE_TYPE (arg
);
9552 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9553 real_from_string (&r
, buf
);
9554 result
= build_call_expr (isle_fn
, 2,
9555 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
9556 build_real (type
, r
));
9557 /*result = fold_build2_loc (loc, UNGT_EXPR,
9558 TREE_TYPE (TREE_TYPE (fndecl)),
9559 fold_build1_loc (loc, ABS_EXPR, type, arg),
9560 build_real (type, r));
9561 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9562 TREE_TYPE (TREE_TYPE (fndecl)),
9566 case BUILT_IN_ISNORMAL
:
9568 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9569 islessequal(fabs(x),DBL_MAX). */
9570 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
9571 tree
const isge_fn
= builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL
);
9572 tree
const type
= TREE_TYPE (arg
);
9573 REAL_VALUE_TYPE rmax
, rmin
;
9576 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9577 real_from_string (&rmax
, buf
);
9578 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
9579 real_from_string (&rmin
, buf
);
9580 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
9581 result
= build_call_expr (isle_fn
, 2, arg
,
9582 build_real (type
, rmax
));
9583 result
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, result
,
9584 build_call_expr (isge_fn
, 2, arg
,
9585 build_real (type
, rmin
)));
9595 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9596 ARG is the argument for the call. */
9599 fold_builtin_classify (location_t loc
, tree fndecl
, tree arg
, int builtin_index
)
9601 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9604 if (!validate_arg (arg
, REAL_TYPE
))
9607 switch (builtin_index
)
9609 case BUILT_IN_ISINF
:
9610 if (!HONOR_INFINITIES (arg
))
9611 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9613 if (TREE_CODE (arg
) == REAL_CST
)
9615 r
= TREE_REAL_CST (arg
);
9616 if (real_isinf (&r
))
9617 return real_compare (GT_EXPR
, &r
, &dconst0
)
9618 ? integer_one_node
: integer_minus_one_node
;
9620 return integer_zero_node
;
9625 case BUILT_IN_ISINF_SIGN
:
9627 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9628 /* In a boolean context, GCC will fold the inner COND_EXPR to
9629 1. So e.g. "if (isinf_sign(x))" would be folded to just
9630 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9631 tree signbit_fn
= mathfn_built_in_1 (TREE_TYPE (arg
), BUILT_IN_SIGNBIT
, 0);
9632 tree isinf_fn
= builtin_decl_explicit (BUILT_IN_ISINF
);
9633 tree tmp
= NULL_TREE
;
9635 arg
= builtin_save_expr (arg
);
9637 if (signbit_fn
&& isinf_fn
)
9639 tree signbit_call
= build_call_expr_loc (loc
, signbit_fn
, 1, arg
);
9640 tree isinf_call
= build_call_expr_loc (loc
, isinf_fn
, 1, arg
);
9642 signbit_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
9643 signbit_call
, integer_zero_node
);
9644 isinf_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
9645 isinf_call
, integer_zero_node
);
9647 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, signbit_call
,
9648 integer_minus_one_node
, integer_one_node
);
9649 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
9657 case BUILT_IN_ISFINITE
:
9658 if (!HONOR_NANS (arg
)
9659 && !HONOR_INFINITIES (arg
))
9660 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
9662 if (TREE_CODE (arg
) == REAL_CST
)
9664 r
= TREE_REAL_CST (arg
);
9665 return real_isfinite (&r
) ? integer_one_node
: integer_zero_node
;
9670 case BUILT_IN_ISNAN
:
9671 if (!HONOR_NANS (arg
))
9672 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9674 if (TREE_CODE (arg
) == REAL_CST
)
9676 r
= TREE_REAL_CST (arg
);
9677 return real_isnan (&r
) ? integer_one_node
: integer_zero_node
;
9680 arg
= builtin_save_expr (arg
);
9681 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg
, arg
);
9688 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9689 This builtin will generate code to return the appropriate floating
9690 point classification depending on the value of the floating point
9691 number passed in. The possible return values must be supplied as
9692 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9693 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9694 one floating point argument which is "type generic". */
9697 fold_builtin_fpclassify (location_t loc
, tree
*args
, int nargs
)
9699 tree fp_nan
, fp_infinite
, fp_normal
, fp_subnormal
, fp_zero
,
9700 arg
, type
, res
, tmp
;
9705 /* Verify the required arguments in the original call. */
9707 || !validate_arg (args
[0], INTEGER_TYPE
)
9708 || !validate_arg (args
[1], INTEGER_TYPE
)
9709 || !validate_arg (args
[2], INTEGER_TYPE
)
9710 || !validate_arg (args
[3], INTEGER_TYPE
)
9711 || !validate_arg (args
[4], INTEGER_TYPE
)
9712 || !validate_arg (args
[5], REAL_TYPE
))
9716 fp_infinite
= args
[1];
9717 fp_normal
= args
[2];
9718 fp_subnormal
= args
[3];
9721 type
= TREE_TYPE (arg
);
9722 mode
= TYPE_MODE (type
);
9723 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
9727 (fabs(x) == Inf ? FP_INFINITE :
9728 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9729 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9731 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
9732 build_real (type
, dconst0
));
9733 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
9734 tmp
, fp_zero
, fp_subnormal
);
9736 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
9737 real_from_string (&r
, buf
);
9738 tmp
= fold_build2_loc (loc
, GE_EXPR
, integer_type_node
,
9739 arg
, build_real (type
, r
));
9740 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, fp_normal
, res
);
9742 if (HONOR_INFINITIES (mode
))
9745 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
9746 build_real (type
, r
));
9747 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
,
9751 if (HONOR_NANS (mode
))
9753 tmp
= fold_build2_loc (loc
, ORDERED_EXPR
, integer_type_node
, arg
, arg
);
9754 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, res
, fp_nan
);
9760 /* Fold a call to an unordered comparison function such as
9761 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9762 being called and ARG0 and ARG1 are the arguments for the call.
9763 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9764 the opposite of the desired result. UNORDERED_CODE is used
9765 for modes that can hold NaNs and ORDERED_CODE is used for
9769 fold_builtin_unordered_cmp (location_t loc
, tree fndecl
, tree arg0
, tree arg1
,
9770 enum tree_code unordered_code
,
9771 enum tree_code ordered_code
)
9773 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9774 enum tree_code code
;
9776 enum tree_code code0
, code1
;
9777 tree cmp_type
= NULL_TREE
;
9779 type0
= TREE_TYPE (arg0
);
9780 type1
= TREE_TYPE (arg1
);
9782 code0
= TREE_CODE (type0
);
9783 code1
= TREE_CODE (type1
);
9785 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
9786 /* Choose the wider of two real types. */
9787 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
9789 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
9791 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
9794 arg0
= fold_convert_loc (loc
, cmp_type
, arg0
);
9795 arg1
= fold_convert_loc (loc
, cmp_type
, arg1
);
9797 if (unordered_code
== UNORDERED_EXPR
)
9799 if (!HONOR_NANS (arg0
))
9800 return omit_two_operands_loc (loc
, type
, integer_zero_node
, arg0
, arg1
);
9801 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg0
, arg1
);
9804 code
= HONOR_NANS (arg0
) ? unordered_code
: ordered_code
;
9805 return fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
,
9806 fold_build2_loc (loc
, code
, type
, arg0
, arg1
));
9809 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9810 arithmetics if it can never overflow, or into internal functions that
9811 return both result of arithmetics and overflowed boolean flag in
9812 a complex integer result, or some other check for overflow. */
9815 fold_builtin_arith_overflow (location_t loc
, enum built_in_function fcode
,
9816 tree arg0
, tree arg1
, tree arg2
)
9818 enum internal_fn ifn
= IFN_LAST
;
9819 tree type
= TREE_TYPE (TREE_TYPE (arg2
));
9820 tree mem_arg2
= build_fold_indirect_ref_loc (loc
, arg2
);
9823 case BUILT_IN_ADD_OVERFLOW
:
9824 case BUILT_IN_SADD_OVERFLOW
:
9825 case BUILT_IN_SADDL_OVERFLOW
:
9826 case BUILT_IN_SADDLL_OVERFLOW
:
9827 case BUILT_IN_UADD_OVERFLOW
:
9828 case BUILT_IN_UADDL_OVERFLOW
:
9829 case BUILT_IN_UADDLL_OVERFLOW
:
9830 ifn
= IFN_ADD_OVERFLOW
;
9832 case BUILT_IN_SUB_OVERFLOW
:
9833 case BUILT_IN_SSUB_OVERFLOW
:
9834 case BUILT_IN_SSUBL_OVERFLOW
:
9835 case BUILT_IN_SSUBLL_OVERFLOW
:
9836 case BUILT_IN_USUB_OVERFLOW
:
9837 case BUILT_IN_USUBL_OVERFLOW
:
9838 case BUILT_IN_USUBLL_OVERFLOW
:
9839 ifn
= IFN_SUB_OVERFLOW
;
9841 case BUILT_IN_MUL_OVERFLOW
:
9842 case BUILT_IN_SMUL_OVERFLOW
:
9843 case BUILT_IN_SMULL_OVERFLOW
:
9844 case BUILT_IN_SMULLL_OVERFLOW
:
9845 case BUILT_IN_UMUL_OVERFLOW
:
9846 case BUILT_IN_UMULL_OVERFLOW
:
9847 case BUILT_IN_UMULLL_OVERFLOW
:
9848 ifn
= IFN_MUL_OVERFLOW
;
9853 tree ctype
= build_complex_type (type
);
9854 tree call
= build_call_expr_internal_loc (loc
, ifn
, ctype
,
9856 tree tgt
= save_expr (call
);
9857 tree intres
= build1_loc (loc
, REALPART_EXPR
, type
, tgt
);
9858 tree ovfres
= build1_loc (loc
, IMAGPART_EXPR
, type
, tgt
);
9859 ovfres
= fold_convert_loc (loc
, boolean_type_node
, ovfres
);
9861 = fold_build2_loc (loc
, MODIFY_EXPR
, void_type_node
, mem_arg2
, intres
);
9862 return build2_loc (loc
, COMPOUND_EXPR
, boolean_type_node
, store
, ovfres
);
9865 /* Fold a call to built-in function FNDECL with 0 arguments.
9866 This function returns NULL_TREE if no simplification was possible. */
9869 fold_builtin_0 (location_t loc
, tree fndecl
)
9871 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9872 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9875 CASE_FLT_FN (BUILT_IN_INF
):
9876 case BUILT_IN_INFD32
:
9877 case BUILT_IN_INFD64
:
9878 case BUILT_IN_INFD128
:
9879 return fold_builtin_inf (loc
, type
, true);
9881 CASE_FLT_FN (BUILT_IN_HUGE_VAL
):
9882 return fold_builtin_inf (loc
, type
, false);
9884 case BUILT_IN_CLASSIFY_TYPE
:
9885 return fold_builtin_classify_type (NULL_TREE
);
9893 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9894 This function returns NULL_TREE if no simplification was possible. */
9897 fold_builtin_1 (location_t loc
, tree fndecl
, tree arg0
)
9899 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9900 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9903 case BUILT_IN_CONSTANT_P
:
9905 tree val
= fold_builtin_constant_p (arg0
);
9907 /* Gimplification will pull the CALL_EXPR for the builtin out of
9908 an if condition. When not optimizing, we'll not CSE it back.
9909 To avoid link error types of regressions, return false now. */
9910 if (!val
&& !optimize
)
9911 val
= integer_zero_node
;
9916 case BUILT_IN_CLASSIFY_TYPE
:
9917 return fold_builtin_classify_type (arg0
);
9919 case BUILT_IN_STRLEN
:
9920 return fold_builtin_strlen (loc
, type
, arg0
);
9922 CASE_FLT_FN (BUILT_IN_FABS
):
9923 case BUILT_IN_FABSD32
:
9924 case BUILT_IN_FABSD64
:
9925 case BUILT_IN_FABSD128
:
9926 return fold_builtin_fabs (loc
, arg0
, type
);
9930 case BUILT_IN_LLABS
:
9931 case BUILT_IN_IMAXABS
:
9932 return fold_builtin_abs (loc
, arg0
, type
);
9934 CASE_FLT_FN (BUILT_IN_CONJ
):
9935 if (validate_arg (arg0
, COMPLEX_TYPE
)
9936 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9937 return fold_build1_loc (loc
, CONJ_EXPR
, type
, arg0
);
9940 CASE_FLT_FN (BUILT_IN_CREAL
):
9941 if (validate_arg (arg0
, COMPLEX_TYPE
)
9942 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9943 return non_lvalue_loc (loc
, fold_build1_loc (loc
, REALPART_EXPR
, type
, arg0
));;
9946 CASE_FLT_FN (BUILT_IN_CIMAG
):
9947 if (validate_arg (arg0
, COMPLEX_TYPE
)
9948 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9949 return non_lvalue_loc (loc
, fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg0
));
9952 CASE_FLT_FN (BUILT_IN_CCOS
):
9953 return fold_builtin_ccos (loc
, arg0
, type
, fndecl
, /*hyper=*/ false);
9955 CASE_FLT_FN (BUILT_IN_CCOSH
):
9956 return fold_builtin_ccos (loc
, arg0
, type
, fndecl
, /*hyper=*/ true);
9958 CASE_FLT_FN (BUILT_IN_CPROJ
):
9959 return fold_builtin_cproj (loc
, arg0
, type
);
9961 CASE_FLT_FN (BUILT_IN_CSIN
):
9962 if (validate_arg (arg0
, COMPLEX_TYPE
)
9963 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9964 return do_mpc_arg1 (arg0
, type
, mpc_sin
);
9967 CASE_FLT_FN (BUILT_IN_CSINH
):
9968 if (validate_arg (arg0
, COMPLEX_TYPE
)
9969 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9970 return do_mpc_arg1 (arg0
, type
, mpc_sinh
);
9973 CASE_FLT_FN (BUILT_IN_CTAN
):
9974 if (validate_arg (arg0
, COMPLEX_TYPE
)
9975 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9976 return do_mpc_arg1 (arg0
, type
, mpc_tan
);
9979 CASE_FLT_FN (BUILT_IN_CTANH
):
9980 if (validate_arg (arg0
, COMPLEX_TYPE
)
9981 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9982 return do_mpc_arg1 (arg0
, type
, mpc_tanh
);
9985 CASE_FLT_FN (BUILT_IN_CLOG
):
9986 if (validate_arg (arg0
, COMPLEX_TYPE
)
9987 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9988 return do_mpc_arg1 (arg0
, type
, mpc_log
);
9991 CASE_FLT_FN (BUILT_IN_CSQRT
):
9992 if (validate_arg (arg0
, COMPLEX_TYPE
)
9993 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9994 return do_mpc_arg1 (arg0
, type
, mpc_sqrt
);
9997 CASE_FLT_FN (BUILT_IN_CASIN
):
9998 if (validate_arg (arg0
, COMPLEX_TYPE
)
9999 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10000 return do_mpc_arg1 (arg0
, type
, mpc_asin
);
10003 CASE_FLT_FN (BUILT_IN_CACOS
):
10004 if (validate_arg (arg0
, COMPLEX_TYPE
)
10005 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10006 return do_mpc_arg1 (arg0
, type
, mpc_acos
);
10009 CASE_FLT_FN (BUILT_IN_CATAN
):
10010 if (validate_arg (arg0
, COMPLEX_TYPE
)
10011 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10012 return do_mpc_arg1 (arg0
, type
, mpc_atan
);
10015 CASE_FLT_FN (BUILT_IN_CASINH
):
10016 if (validate_arg (arg0
, COMPLEX_TYPE
)
10017 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10018 return do_mpc_arg1 (arg0
, type
, mpc_asinh
);
10021 CASE_FLT_FN (BUILT_IN_CACOSH
):
10022 if (validate_arg (arg0
, COMPLEX_TYPE
)
10023 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10024 return do_mpc_arg1 (arg0
, type
, mpc_acosh
);
10027 CASE_FLT_FN (BUILT_IN_CATANH
):
10028 if (validate_arg (arg0
, COMPLEX_TYPE
)
10029 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10030 return do_mpc_arg1 (arg0
, type
, mpc_atanh
);
10033 CASE_FLT_FN (BUILT_IN_CABS
):
10034 return fold_builtin_cabs (loc
, arg0
, type
, fndecl
);
10036 CASE_FLT_FN (BUILT_IN_CARG
):
10037 return fold_builtin_carg (loc
, arg0
, type
);
10039 CASE_FLT_FN (BUILT_IN_SQRT
):
10040 return fold_builtin_sqrt (loc
, arg0
, type
);
10042 CASE_FLT_FN (BUILT_IN_CBRT
):
10043 return fold_builtin_cbrt (loc
, arg0
, type
);
10045 CASE_FLT_FN (BUILT_IN_ASIN
):
10046 if (validate_arg (arg0
, REAL_TYPE
))
10047 return do_mpfr_arg1 (arg0
, type
, mpfr_asin
,
10048 &dconstm1
, &dconst1
, true);
10051 CASE_FLT_FN (BUILT_IN_ACOS
):
10052 if (validate_arg (arg0
, REAL_TYPE
))
10053 return do_mpfr_arg1 (arg0
, type
, mpfr_acos
,
10054 &dconstm1
, &dconst1
, true);
10057 CASE_FLT_FN (BUILT_IN_ATAN
):
10058 if (validate_arg (arg0
, REAL_TYPE
))
10059 return do_mpfr_arg1 (arg0
, type
, mpfr_atan
, NULL
, NULL
, 0);
10062 CASE_FLT_FN (BUILT_IN_ASINH
):
10063 if (validate_arg (arg0
, REAL_TYPE
))
10064 return do_mpfr_arg1 (arg0
, type
, mpfr_asinh
, NULL
, NULL
, 0);
10067 CASE_FLT_FN (BUILT_IN_ACOSH
):
10068 if (validate_arg (arg0
, REAL_TYPE
))
10069 return do_mpfr_arg1 (arg0
, type
, mpfr_acosh
,
10070 &dconst1
, NULL
, true);
10073 CASE_FLT_FN (BUILT_IN_ATANH
):
10074 if (validate_arg (arg0
, REAL_TYPE
))
10075 return do_mpfr_arg1 (arg0
, type
, mpfr_atanh
,
10076 &dconstm1
, &dconst1
, false);
10079 CASE_FLT_FN (BUILT_IN_SIN
):
10080 if (validate_arg (arg0
, REAL_TYPE
))
10081 return do_mpfr_arg1 (arg0
, type
, mpfr_sin
, NULL
, NULL
, 0);
10084 CASE_FLT_FN (BUILT_IN_COS
):
10085 return fold_builtin_cos (loc
, arg0
, type
, fndecl
);
10087 CASE_FLT_FN (BUILT_IN_TAN
):
10088 return fold_builtin_tan (arg0
, type
);
10090 CASE_FLT_FN (BUILT_IN_CEXP
):
10091 return fold_builtin_cexp (loc
, arg0
, type
);
10093 CASE_FLT_FN (BUILT_IN_CEXPI
):
10094 if (validate_arg (arg0
, REAL_TYPE
))
10095 return do_mpfr_sincos (arg0
, NULL_TREE
, NULL_TREE
);
10098 CASE_FLT_FN (BUILT_IN_SINH
):
10099 if (validate_arg (arg0
, REAL_TYPE
))
10100 return do_mpfr_arg1 (arg0
, type
, mpfr_sinh
, NULL
, NULL
, 0);
10103 CASE_FLT_FN (BUILT_IN_COSH
):
10104 return fold_builtin_cosh (loc
, arg0
, type
, fndecl
);
10106 CASE_FLT_FN (BUILT_IN_TANH
):
10107 if (validate_arg (arg0
, REAL_TYPE
))
10108 return do_mpfr_arg1 (arg0
, type
, mpfr_tanh
, NULL
, NULL
, 0);
10111 CASE_FLT_FN (BUILT_IN_ERF
):
10112 if (validate_arg (arg0
, REAL_TYPE
))
10113 return do_mpfr_arg1 (arg0
, type
, mpfr_erf
, NULL
, NULL
, 0);
10116 CASE_FLT_FN (BUILT_IN_ERFC
):
10117 if (validate_arg (arg0
, REAL_TYPE
))
10118 return do_mpfr_arg1 (arg0
, type
, mpfr_erfc
, NULL
, NULL
, 0);
10121 CASE_FLT_FN (BUILT_IN_TGAMMA
):
10122 if (validate_arg (arg0
, REAL_TYPE
))
10123 return do_mpfr_arg1 (arg0
, type
, mpfr_gamma
, NULL
, NULL
, 0);
10126 CASE_FLT_FN (BUILT_IN_EXP
):
10127 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp
);
10129 CASE_FLT_FN (BUILT_IN_EXP2
):
10130 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp2
);
10132 CASE_FLT_FN (BUILT_IN_EXP10
):
10133 CASE_FLT_FN (BUILT_IN_POW10
):
10134 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp10
);
10136 CASE_FLT_FN (BUILT_IN_EXPM1
):
10137 if (validate_arg (arg0
, REAL_TYPE
))
10138 return do_mpfr_arg1 (arg0
, type
, mpfr_expm1
, NULL
, NULL
, 0);
10141 CASE_FLT_FN (BUILT_IN_LOG
):
10142 if (validate_arg (arg0
, REAL_TYPE
))
10143 return do_mpfr_arg1 (arg0
, type
, mpfr_log
, &dconst0
, NULL
, false);
10146 CASE_FLT_FN (BUILT_IN_LOG2
):
10147 if (validate_arg (arg0
, REAL_TYPE
))
10148 return do_mpfr_arg1 (arg0
, type
, mpfr_log2
, &dconst0
, NULL
, false);
10151 CASE_FLT_FN (BUILT_IN_LOG10
):
10152 if (validate_arg (arg0
, REAL_TYPE
))
10153 return do_mpfr_arg1 (arg0
, type
, mpfr_log10
, &dconst0
, NULL
, false);
10156 CASE_FLT_FN (BUILT_IN_LOG1P
):
10157 if (validate_arg (arg0
, REAL_TYPE
))
10158 return do_mpfr_arg1 (arg0
, type
, mpfr_log1p
,
10159 &dconstm1
, NULL
, false);
10162 CASE_FLT_FN (BUILT_IN_J0
):
10163 if (validate_arg (arg0
, REAL_TYPE
))
10164 return do_mpfr_arg1 (arg0
, type
, mpfr_j0
,
10168 CASE_FLT_FN (BUILT_IN_J1
):
10169 if (validate_arg (arg0
, REAL_TYPE
))
10170 return do_mpfr_arg1 (arg0
, type
, mpfr_j1
,
10174 CASE_FLT_FN (BUILT_IN_Y0
):
10175 if (validate_arg (arg0
, REAL_TYPE
))
10176 return do_mpfr_arg1 (arg0
, type
, mpfr_y0
,
10177 &dconst0
, NULL
, false);
10180 CASE_FLT_FN (BUILT_IN_Y1
):
10181 if (validate_arg (arg0
, REAL_TYPE
))
10182 return do_mpfr_arg1 (arg0
, type
, mpfr_y1
,
10183 &dconst0
, NULL
, false);
10186 CASE_FLT_FN (BUILT_IN_NAN
):
10187 case BUILT_IN_NAND32
:
10188 case BUILT_IN_NAND64
:
10189 case BUILT_IN_NAND128
:
10190 return fold_builtin_nan (arg0
, type
, true);
10192 CASE_FLT_FN (BUILT_IN_NANS
):
10193 return fold_builtin_nan (arg0
, type
, false);
10195 CASE_FLT_FN (BUILT_IN_FLOOR
):
10196 return fold_builtin_floor (loc
, fndecl
, arg0
);
10198 CASE_FLT_FN (BUILT_IN_CEIL
):
10199 return fold_builtin_ceil (loc
, fndecl
, arg0
);
10201 CASE_FLT_FN (BUILT_IN_TRUNC
):
10202 return fold_builtin_trunc (loc
, fndecl
, arg0
);
10204 CASE_FLT_FN (BUILT_IN_ROUND
):
10205 return fold_builtin_round (loc
, fndecl
, arg0
);
10207 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
10208 CASE_FLT_FN (BUILT_IN_RINT
):
10209 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg0
);
10211 CASE_FLT_FN (BUILT_IN_ICEIL
):
10212 CASE_FLT_FN (BUILT_IN_LCEIL
):
10213 CASE_FLT_FN (BUILT_IN_LLCEIL
):
10214 CASE_FLT_FN (BUILT_IN_LFLOOR
):
10215 CASE_FLT_FN (BUILT_IN_IFLOOR
):
10216 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
10217 CASE_FLT_FN (BUILT_IN_IROUND
):
10218 CASE_FLT_FN (BUILT_IN_LROUND
):
10219 CASE_FLT_FN (BUILT_IN_LLROUND
):
10220 return fold_builtin_int_roundingfn (loc
, fndecl
, arg0
);
10222 CASE_FLT_FN (BUILT_IN_IRINT
):
10223 CASE_FLT_FN (BUILT_IN_LRINT
):
10224 CASE_FLT_FN (BUILT_IN_LLRINT
):
10225 return fold_fixed_mathfn (loc
, fndecl
, arg0
);
10227 case BUILT_IN_BSWAP16
:
10228 case BUILT_IN_BSWAP32
:
10229 case BUILT_IN_BSWAP64
:
10230 return fold_builtin_bswap (fndecl
, arg0
);
10232 CASE_INT_FN (BUILT_IN_FFS
):
10233 CASE_INT_FN (BUILT_IN_CLZ
):
10234 CASE_INT_FN (BUILT_IN_CTZ
):
10235 CASE_INT_FN (BUILT_IN_CLRSB
):
10236 CASE_INT_FN (BUILT_IN_POPCOUNT
):
10237 CASE_INT_FN (BUILT_IN_PARITY
):
10238 return fold_builtin_bitop (fndecl
, arg0
);
10240 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
10241 return fold_builtin_signbit (loc
, arg0
, type
);
10243 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
10244 return fold_builtin_significand (loc
, arg0
, type
);
10246 CASE_FLT_FN (BUILT_IN_ILOGB
):
10247 CASE_FLT_FN (BUILT_IN_LOGB
):
10248 return fold_builtin_logb (loc
, arg0
, type
);
10250 case BUILT_IN_ISASCII
:
10251 return fold_builtin_isascii (loc
, arg0
);
10253 case BUILT_IN_TOASCII
:
10254 return fold_builtin_toascii (loc
, arg0
);
10256 case BUILT_IN_ISDIGIT
:
10257 return fold_builtin_isdigit (loc
, arg0
);
10259 CASE_FLT_FN (BUILT_IN_FINITE
):
10260 case BUILT_IN_FINITED32
:
10261 case BUILT_IN_FINITED64
:
10262 case BUILT_IN_FINITED128
:
10263 case BUILT_IN_ISFINITE
:
10265 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISFINITE
);
10268 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10271 CASE_FLT_FN (BUILT_IN_ISINF
):
10272 case BUILT_IN_ISINFD32
:
10273 case BUILT_IN_ISINFD64
:
10274 case BUILT_IN_ISINFD128
:
10276 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF
);
10279 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10282 case BUILT_IN_ISNORMAL
:
10283 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10285 case BUILT_IN_ISINF_SIGN
:
10286 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF_SIGN
);
10288 CASE_FLT_FN (BUILT_IN_ISNAN
):
10289 case BUILT_IN_ISNAND32
:
10290 case BUILT_IN_ISNAND64
:
10291 case BUILT_IN_ISNAND128
:
10292 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISNAN
);
10294 case BUILT_IN_FREE
:
10295 if (integer_zerop (arg0
))
10296 return build_empty_stmt (loc
);
10307 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10308 This function returns NULL_TREE if no simplification was possible. */
10311 fold_builtin_2 (location_t loc
, tree fndecl
, tree arg0
, tree arg1
)
10313 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10314 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10318 CASE_FLT_FN (BUILT_IN_JN
):
10319 if (validate_arg (arg0
, INTEGER_TYPE
)
10320 && validate_arg (arg1
, REAL_TYPE
))
10321 return do_mpfr_bessel_n (arg0
, arg1
, type
, mpfr_jn
, NULL
, 0);
10324 CASE_FLT_FN (BUILT_IN_YN
):
10325 if (validate_arg (arg0
, INTEGER_TYPE
)
10326 && validate_arg (arg1
, REAL_TYPE
))
10327 return do_mpfr_bessel_n (arg0
, arg1
, type
, mpfr_yn
,
10331 CASE_FLT_FN (BUILT_IN_DREM
):
10332 CASE_FLT_FN (BUILT_IN_REMAINDER
):
10333 if (validate_arg (arg0
, REAL_TYPE
)
10334 && validate_arg (arg1
, REAL_TYPE
))
10335 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_remainder
);
10338 CASE_FLT_FN_REENT (BUILT_IN_GAMMA
): /* GAMMA_R */
10339 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA
): /* LGAMMA_R */
10340 if (validate_arg (arg0
, REAL_TYPE
)
10341 && validate_arg (arg1
, POINTER_TYPE
))
10342 return do_mpfr_lgamma_r (arg0
, arg1
, type
);
10345 CASE_FLT_FN (BUILT_IN_ATAN2
):
10346 if (validate_arg (arg0
, REAL_TYPE
)
10347 && validate_arg (arg1
, REAL_TYPE
))
10348 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_atan2
);
10351 CASE_FLT_FN (BUILT_IN_FDIM
):
10352 if (validate_arg (arg0
, REAL_TYPE
)
10353 && validate_arg (arg1
, REAL_TYPE
))
10354 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_dim
);
10357 CASE_FLT_FN (BUILT_IN_HYPOT
):
10358 return fold_builtin_hypot (loc
, fndecl
, arg0
, arg1
, type
);
10360 CASE_FLT_FN (BUILT_IN_CPOW
):
10361 if (validate_arg (arg0
, COMPLEX_TYPE
)
10362 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
10363 && validate_arg (arg1
, COMPLEX_TYPE
)
10364 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
)
10365 return do_mpc_arg2 (arg0
, arg1
, type
, /*do_nonfinite=*/ 0, mpc_pow
);
10368 CASE_FLT_FN (BUILT_IN_LDEXP
):
10369 return fold_builtin_load_exponent (loc
, arg0
, arg1
, type
, /*ldexp=*/true);
10370 CASE_FLT_FN (BUILT_IN_SCALBN
):
10371 CASE_FLT_FN (BUILT_IN_SCALBLN
):
10372 return fold_builtin_load_exponent (loc
, arg0
, arg1
,
10373 type
, /*ldexp=*/false);
10375 CASE_FLT_FN (BUILT_IN_FREXP
):
10376 return fold_builtin_frexp (loc
, arg0
, arg1
, type
);
10378 CASE_FLT_FN (BUILT_IN_MODF
):
10379 return fold_builtin_modf (loc
, arg0
, arg1
, type
);
10381 case BUILT_IN_STRSTR
:
10382 return fold_builtin_strstr (loc
, arg0
, arg1
, type
);
10384 case BUILT_IN_STRSPN
:
10385 return fold_builtin_strspn (loc
, arg0
, arg1
);
10387 case BUILT_IN_STRCSPN
:
10388 return fold_builtin_strcspn (loc
, arg0
, arg1
);
10390 case BUILT_IN_STRCHR
:
10391 case BUILT_IN_INDEX
:
10392 return fold_builtin_strchr (loc
, arg0
, arg1
, type
);
10394 case BUILT_IN_STRRCHR
:
10395 case BUILT_IN_RINDEX
:
10396 return fold_builtin_strrchr (loc
, arg0
, arg1
, type
);
10398 case BUILT_IN_STRCMP
:
10399 return fold_builtin_strcmp (loc
, arg0
, arg1
);
10401 case BUILT_IN_STRPBRK
:
10402 return fold_builtin_strpbrk (loc
, arg0
, arg1
, type
);
10404 case BUILT_IN_EXPECT
:
10405 return fold_builtin_expect (loc
, arg0
, arg1
, NULL_TREE
);
10407 CASE_FLT_FN (BUILT_IN_POW
):
10408 return fold_builtin_pow (loc
, fndecl
, arg0
, arg1
, type
);
10410 CASE_FLT_FN (BUILT_IN_POWI
):
10411 return fold_builtin_powi (loc
, fndecl
, arg0
, arg1
, type
);
10413 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
10414 return fold_builtin_copysign (loc
, fndecl
, arg0
, arg1
, type
);
10416 CASE_FLT_FN (BUILT_IN_FMIN
):
10417 return fold_builtin_fmin_fmax (loc
, arg0
, arg1
, type
, /*max=*/false);
10419 CASE_FLT_FN (BUILT_IN_FMAX
):
10420 return fold_builtin_fmin_fmax (loc
, arg0
, arg1
, type
, /*max=*/true);
10422 case BUILT_IN_ISGREATER
:
10423 return fold_builtin_unordered_cmp (loc
, fndecl
,
10424 arg0
, arg1
, UNLE_EXPR
, LE_EXPR
);
10425 case BUILT_IN_ISGREATEREQUAL
:
10426 return fold_builtin_unordered_cmp (loc
, fndecl
,
10427 arg0
, arg1
, UNLT_EXPR
, LT_EXPR
);
10428 case BUILT_IN_ISLESS
:
10429 return fold_builtin_unordered_cmp (loc
, fndecl
,
10430 arg0
, arg1
, UNGE_EXPR
, GE_EXPR
);
10431 case BUILT_IN_ISLESSEQUAL
:
10432 return fold_builtin_unordered_cmp (loc
, fndecl
,
10433 arg0
, arg1
, UNGT_EXPR
, GT_EXPR
);
10434 case BUILT_IN_ISLESSGREATER
:
10435 return fold_builtin_unordered_cmp (loc
, fndecl
,
10436 arg0
, arg1
, UNEQ_EXPR
, EQ_EXPR
);
10437 case BUILT_IN_ISUNORDERED
:
10438 return fold_builtin_unordered_cmp (loc
, fndecl
,
10439 arg0
, arg1
, UNORDERED_EXPR
,
10442 /* We do the folding for va_start in the expander. */
10443 case BUILT_IN_VA_START
:
10446 case BUILT_IN_OBJECT_SIZE
:
10447 return fold_builtin_object_size (arg0
, arg1
);
10449 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
10450 return fold_builtin_atomic_always_lock_free (arg0
, arg1
);
10452 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
10453 return fold_builtin_atomic_is_lock_free (arg0
, arg1
);
10461 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10463 This function returns NULL_TREE if no simplification was possible. */
10466 fold_builtin_3 (location_t loc
, tree fndecl
,
10467 tree arg0
, tree arg1
, tree arg2
)
10469 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10470 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10474 CASE_FLT_FN (BUILT_IN_SINCOS
):
10475 return fold_builtin_sincos (loc
, arg0
, arg1
, arg2
);
10477 CASE_FLT_FN (BUILT_IN_FMA
):
10478 return fold_builtin_fma (loc
, arg0
, arg1
, arg2
, type
);
10481 CASE_FLT_FN (BUILT_IN_REMQUO
):
10482 if (validate_arg (arg0
, REAL_TYPE
)
10483 && validate_arg (arg1
, REAL_TYPE
)
10484 && validate_arg (arg2
, POINTER_TYPE
))
10485 return do_mpfr_remquo (arg0
, arg1
, arg2
);
10488 case BUILT_IN_STRNCMP
:
10489 return fold_builtin_strncmp (loc
, arg0
, arg1
, arg2
);
10491 case BUILT_IN_MEMCHR
:
10492 return fold_builtin_memchr (loc
, arg0
, arg1
, arg2
, type
);
10494 case BUILT_IN_BCMP
:
10495 case BUILT_IN_MEMCMP
:
10496 return fold_builtin_memcmp (loc
, arg0
, arg1
, arg2
);;
10498 case BUILT_IN_EXPECT
:
10499 return fold_builtin_expect (loc
, arg0
, arg1
, arg2
);
10501 case BUILT_IN_ADD_OVERFLOW
:
10502 case BUILT_IN_SUB_OVERFLOW
:
10503 case BUILT_IN_MUL_OVERFLOW
:
10504 case BUILT_IN_SADD_OVERFLOW
:
10505 case BUILT_IN_SADDL_OVERFLOW
:
10506 case BUILT_IN_SADDLL_OVERFLOW
:
10507 case BUILT_IN_SSUB_OVERFLOW
:
10508 case BUILT_IN_SSUBL_OVERFLOW
:
10509 case BUILT_IN_SSUBLL_OVERFLOW
:
10510 case BUILT_IN_SMUL_OVERFLOW
:
10511 case BUILT_IN_SMULL_OVERFLOW
:
10512 case BUILT_IN_SMULLL_OVERFLOW
:
10513 case BUILT_IN_UADD_OVERFLOW
:
10514 case BUILT_IN_UADDL_OVERFLOW
:
10515 case BUILT_IN_UADDLL_OVERFLOW
:
10516 case BUILT_IN_USUB_OVERFLOW
:
10517 case BUILT_IN_USUBL_OVERFLOW
:
10518 case BUILT_IN_USUBLL_OVERFLOW
:
10519 case BUILT_IN_UMUL_OVERFLOW
:
10520 case BUILT_IN_UMULL_OVERFLOW
:
10521 case BUILT_IN_UMULLL_OVERFLOW
:
10522 return fold_builtin_arith_overflow (loc
, fcode
, arg0
, arg1
, arg2
);
10530 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10531 arguments. IGNORE is true if the result of the
10532 function call is ignored. This function returns NULL_TREE if no
10533 simplification was possible. */
10536 fold_builtin_n (location_t loc
, tree fndecl
, tree
*args
, int nargs
, bool)
10538 tree ret
= NULL_TREE
;
10543 ret
= fold_builtin_0 (loc
, fndecl
);
10546 ret
= fold_builtin_1 (loc
, fndecl
, args
[0]);
10549 ret
= fold_builtin_2 (loc
, fndecl
, args
[0], args
[1]);
10552 ret
= fold_builtin_3 (loc
, fndecl
, args
[0], args
[1], args
[2]);
10555 ret
= fold_builtin_varargs (loc
, fndecl
, args
, nargs
);
10560 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
10561 SET_EXPR_LOCATION (ret
, loc
);
10562 TREE_NO_WARNING (ret
) = 1;
10568 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10569 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10570 of arguments in ARGS to be omitted. OLDNARGS is the number of
10571 elements in ARGS. */
10574 rewrite_call_expr_valist (location_t loc
, int oldnargs
, tree
*args
,
10575 int skip
, tree fndecl
, int n
, va_list newargs
)
10577 int nargs
= oldnargs
- skip
+ n
;
10584 buffer
= XALLOCAVEC (tree
, nargs
);
10585 for (i
= 0; i
< n
; i
++)
10586 buffer
[i
] = va_arg (newargs
, tree
);
10587 for (j
= skip
; j
< oldnargs
; j
++, i
++)
10588 buffer
[i
] = args
[j
];
10591 buffer
= args
+ skip
;
10593 return build_call_expr_loc_array (loc
, fndecl
, nargs
, buffer
);
10596 /* Return true if FNDECL shouldn't be folded right now.
10597 If a built-in function has an inline attribute always_inline
10598 wrapper, defer folding it after always_inline functions have
10599 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10600 might not be performed. */
10603 avoid_folding_inline_builtin (tree fndecl
)
10605 return (DECL_DECLARED_INLINE_P (fndecl
)
10606 && DECL_DISREGARD_INLINE_LIMITS (fndecl
)
10608 && !cfun
->always_inline_functions_inlined
10609 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl
)));
10612 /* A wrapper function for builtin folding that prevents warnings for
10613 "statement without effect" and the like, caused by removing the
10614 call node earlier than the warning is generated. */
10617 fold_call_expr (location_t loc
, tree exp
, bool ignore
)
10619 tree ret
= NULL_TREE
;
10620 tree fndecl
= get_callee_fndecl (exp
);
10622 && TREE_CODE (fndecl
) == FUNCTION_DECL
10623 && DECL_BUILT_IN (fndecl
)
10624 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10625 yet. Defer folding until we see all the arguments
10626 (after inlining). */
10627 && !CALL_EXPR_VA_ARG_PACK (exp
))
10629 int nargs
= call_expr_nargs (exp
);
10631 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10632 instead last argument is __builtin_va_arg_pack (). Defer folding
10633 even in that case, until arguments are finalized. */
10634 if (nargs
&& TREE_CODE (CALL_EXPR_ARG (exp
, nargs
- 1)) == CALL_EXPR
)
10636 tree fndecl2
= get_callee_fndecl (CALL_EXPR_ARG (exp
, nargs
- 1));
10638 && TREE_CODE (fndecl2
) == FUNCTION_DECL
10639 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
10640 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
10644 if (avoid_folding_inline_builtin (fndecl
))
10647 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
10648 return targetm
.fold_builtin (fndecl
, call_expr_nargs (exp
),
10649 CALL_EXPR_ARGP (exp
), ignore
);
10652 tree
*args
= CALL_EXPR_ARGP (exp
);
10653 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
10661 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
10662 N arguments are passed in the array ARGARRAY. Return a folded
10663 expression or NULL_TREE if no simplification was possible. */
10666 fold_builtin_call_array (location_t loc
, tree
,
10671 if (TREE_CODE (fn
) != ADDR_EXPR
)
10674 tree fndecl
= TREE_OPERAND (fn
, 0);
10675 if (TREE_CODE (fndecl
) == FUNCTION_DECL
10676 && DECL_BUILT_IN (fndecl
))
10678 /* If last argument is __builtin_va_arg_pack (), arguments to this
10679 function are not finalized yet. Defer folding until they are. */
10680 if (n
&& TREE_CODE (argarray
[n
- 1]) == CALL_EXPR
)
10682 tree fndecl2
= get_callee_fndecl (argarray
[n
- 1]);
10684 && TREE_CODE (fndecl2
) == FUNCTION_DECL
10685 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
10686 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
10689 if (avoid_folding_inline_builtin (fndecl
))
10691 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
10692 return targetm
.fold_builtin (fndecl
, n
, argarray
, false);
10694 return fold_builtin_n (loc
, fndecl
, argarray
, n
, false);
10700 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10701 along with N new arguments specified as the "..." parameters. SKIP
10702 is the number of arguments in EXP to be omitted. This function is used
10703 to do varargs-to-varargs transformations. */
10706 rewrite_call_expr (location_t loc
, tree exp
, int skip
, tree fndecl
, int n
, ...)
10712 t
= rewrite_call_expr_valist (loc
, call_expr_nargs (exp
),
10713 CALL_EXPR_ARGP (exp
), skip
, fndecl
, n
, ap
);
10719 /* Validate a single argument ARG against a tree code CODE representing
10723 validate_arg (const_tree arg
, enum tree_code code
)
10727 else if (code
== POINTER_TYPE
)
10728 return POINTER_TYPE_P (TREE_TYPE (arg
));
10729 else if (code
== INTEGER_TYPE
)
10730 return INTEGRAL_TYPE_P (TREE_TYPE (arg
));
10731 return code
== TREE_CODE (TREE_TYPE (arg
));
10734 /* This function validates the types of a function call argument list
10735 against a specified list of tree_codes. If the last specifier is a 0,
10736 that represents an ellipses, otherwise the last specifier must be a
10739 This is the GIMPLE version of validate_arglist. Eventually we want to
10740 completely convert builtins.c to work from GIMPLEs and the tree based
10741 validate_arglist will then be removed. */
10744 validate_gimple_arglist (const gcall
*call
, ...)
10746 enum tree_code code
;
10752 va_start (ap
, call
);
10757 code
= (enum tree_code
) va_arg (ap
, int);
10761 /* This signifies an ellipses, any further arguments are all ok. */
10765 /* This signifies an endlink, if no arguments remain, return
10766 true, otherwise return false. */
10767 res
= (i
== gimple_call_num_args (call
));
10770 /* If no parameters remain or the parameter's code does not
10771 match the specified code, return false. Otherwise continue
10772 checking any remaining arguments. */
10773 arg
= gimple_call_arg (call
, i
++);
10774 if (!validate_arg (arg
, code
))
10781 /* We need gotos here since we can only have one VA_CLOSE in a
10789 /* Default target-specific builtin expander that does nothing. */
10792 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
10793 rtx target ATTRIBUTE_UNUSED
,
10794 rtx subtarget ATTRIBUTE_UNUSED
,
10795 machine_mode mode ATTRIBUTE_UNUSED
,
10796 int ignore ATTRIBUTE_UNUSED
)
10801 /* Returns true is EXP represents data that would potentially reside
10802 in a readonly section. */
10805 readonly_data_expr (tree exp
)
10809 if (TREE_CODE (exp
) != ADDR_EXPR
)
10812 exp
= get_base_address (TREE_OPERAND (exp
, 0));
10816 /* Make sure we call decl_readonly_section only for trees it
10817 can handle (since it returns true for everything it doesn't
10819 if (TREE_CODE (exp
) == STRING_CST
10820 || TREE_CODE (exp
) == CONSTRUCTOR
10821 || (TREE_CODE (exp
) == VAR_DECL
&& TREE_STATIC (exp
)))
10822 return decl_readonly_section (exp
, 0);
10827 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10828 to the call, and TYPE is its return type.
10830 Return NULL_TREE if no simplification was possible, otherwise return the
10831 simplified form of the call as a tree.
10833 The simplified form may be a constant or other expression which
10834 computes the same value, but in a more efficient manner (including
10835 calls to other builtin functions).
10837 The call may contain arguments which need to be evaluated, but
10838 which are not useful to determine the result of the call. In
10839 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10840 COMPOUND_EXPR will be an argument which must be evaluated.
10841 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10842 COMPOUND_EXPR in the chain will contain the tree for the simplified
10843 form of the builtin function call. */
10846 fold_builtin_strstr (location_t loc
, tree s1
, tree s2
, tree type
)
10848 if (!validate_arg (s1
, POINTER_TYPE
)
10849 || !validate_arg (s2
, POINTER_TYPE
))
10854 const char *p1
, *p2
;
10856 p2
= c_getstr (s2
);
10860 p1
= c_getstr (s1
);
10863 const char *r
= strstr (p1
, p2
);
10867 return build_int_cst (TREE_TYPE (s1
), 0);
10869 /* Return an offset into the constant string argument. */
10870 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
10871 return fold_convert_loc (loc
, type
, tem
);
10874 /* The argument is const char *, and the result is char *, so we need
10875 a type conversion here to avoid a warning. */
10877 return fold_convert_loc (loc
, type
, s1
);
10882 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
10886 /* New argument list transforming strstr(s1, s2) to
10887 strchr(s1, s2[0]). */
10888 return build_call_expr_loc (loc
, fn
, 2, s1
,
10889 build_int_cst (integer_type_node
, p2
[0]));
10893 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10894 the call, and TYPE is its return type.
10896 Return NULL_TREE if no simplification was possible, otherwise return the
10897 simplified form of the call as a tree.
10899 The simplified form may be a constant or other expression which
10900 computes the same value, but in a more efficient manner (including
10901 calls to other builtin functions).
10903 The call may contain arguments which need to be evaluated, but
10904 which are not useful to determine the result of the call. In
10905 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10906 COMPOUND_EXPR will be an argument which must be evaluated.
10907 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10908 COMPOUND_EXPR in the chain will contain the tree for the simplified
10909 form of the builtin function call. */
10912 fold_builtin_strchr (location_t loc
, tree s1
, tree s2
, tree type
)
10914 if (!validate_arg (s1
, POINTER_TYPE
)
10915 || !validate_arg (s2
, INTEGER_TYPE
))
10921 if (TREE_CODE (s2
) != INTEGER_CST
)
10924 p1
= c_getstr (s1
);
10931 if (target_char_cast (s2
, &c
))
10934 r
= strchr (p1
, c
);
10937 return build_int_cst (TREE_TYPE (s1
), 0);
10939 /* Return an offset into the constant string argument. */
10940 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
10941 return fold_convert_loc (loc
, type
, tem
);
10947 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10948 the call, and TYPE is its return type.
10950 Return NULL_TREE if no simplification was possible, otherwise return the
10951 simplified form of the call as a tree.
10953 The simplified form may be a constant or other expression which
10954 computes the same value, but in a more efficient manner (including
10955 calls to other builtin functions).
10957 The call may contain arguments which need to be evaluated, but
10958 which are not useful to determine the result of the call. In
10959 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10960 COMPOUND_EXPR will be an argument which must be evaluated.
10961 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10962 COMPOUND_EXPR in the chain will contain the tree for the simplified
10963 form of the builtin function call. */
10966 fold_builtin_strrchr (location_t loc
, tree s1
, tree s2
, tree type
)
10968 if (!validate_arg (s1
, POINTER_TYPE
)
10969 || !validate_arg (s2
, INTEGER_TYPE
))
10976 if (TREE_CODE (s2
) != INTEGER_CST
)
10979 p1
= c_getstr (s1
);
10986 if (target_char_cast (s2
, &c
))
10989 r
= strrchr (p1
, c
);
10992 return build_int_cst (TREE_TYPE (s1
), 0);
10994 /* Return an offset into the constant string argument. */
10995 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
10996 return fold_convert_loc (loc
, type
, tem
);
10999 if (! integer_zerop (s2
))
11002 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
11006 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11007 return build_call_expr_loc (loc
, fn
, 2, s1
, s2
);
11011 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11012 to the call, and TYPE is its return type.
11014 Return NULL_TREE if no simplification was possible, otherwise return the
11015 simplified form of the call as a tree.
11017 The simplified form may be a constant or other expression which
11018 computes the same value, but in a more efficient manner (including
11019 calls to other builtin functions).
11021 The call may contain arguments which need to be evaluated, but
11022 which are not useful to determine the result of the call. In
11023 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11024 COMPOUND_EXPR will be an argument which must be evaluated.
11025 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11026 COMPOUND_EXPR in the chain will contain the tree for the simplified
11027 form of the builtin function call. */
11030 fold_builtin_strpbrk (location_t loc
, tree s1
, tree s2
, tree type
)
11032 if (!validate_arg (s1
, POINTER_TYPE
)
11033 || !validate_arg (s2
, POINTER_TYPE
))
11038 const char *p1
, *p2
;
11040 p2
= c_getstr (s2
);
11044 p1
= c_getstr (s1
);
11047 const char *r
= strpbrk (p1
, p2
);
11051 return build_int_cst (TREE_TYPE (s1
), 0);
11053 /* Return an offset into the constant string argument. */
11054 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11055 return fold_convert_loc (loc
, type
, tem
);
11059 /* strpbrk(x, "") == NULL.
11060 Evaluate and ignore s1 in case it had side-effects. */
11061 return omit_one_operand_loc (loc
, TREE_TYPE (s1
), integer_zero_node
, s1
);
11064 return NULL_TREE
; /* Really call strpbrk. */
11066 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
11070 /* New argument list transforming strpbrk(s1, s2) to
11071 strchr(s1, s2[0]). */
11072 return build_call_expr_loc (loc
, fn
, 2, s1
,
11073 build_int_cst (integer_type_node
, p2
[0]));
11077 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11080 Return NULL_TREE if no simplification was possible, otherwise return the
11081 simplified form of the call as a tree.
11083 The simplified form may be a constant or other expression which
11084 computes the same value, but in a more efficient manner (including
11085 calls to other builtin functions).
11087 The call may contain arguments which need to be evaluated, but
11088 which are not useful to determine the result of the call. In
11089 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11090 COMPOUND_EXPR will be an argument which must be evaluated.
11091 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11092 COMPOUND_EXPR in the chain will contain the tree for the simplified
11093 form of the builtin function call. */
11096 fold_builtin_strspn (location_t loc
, tree s1
, tree s2
)
11098 if (!validate_arg (s1
, POINTER_TYPE
)
11099 || !validate_arg (s2
, POINTER_TYPE
))
11103 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
11105 /* If both arguments are constants, evaluate at compile-time. */
11108 const size_t r
= strspn (p1
, p2
);
11109 return build_int_cst (size_type_node
, r
);
11112 /* If either argument is "", return NULL_TREE. */
11113 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
11114 /* Evaluate and ignore both arguments in case either one has
11116 return omit_two_operands_loc (loc
, size_type_node
, size_zero_node
,
11122 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11125 Return NULL_TREE if no simplification was possible, otherwise return the
11126 simplified form of the call as a tree.
11128 The simplified form may be a constant or other expression which
11129 computes the same value, but in a more efficient manner (including
11130 calls to other builtin functions).
11132 The call may contain arguments which need to be evaluated, but
11133 which are not useful to determine the result of the call. In
11134 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11135 COMPOUND_EXPR will be an argument which must be evaluated.
11136 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11137 COMPOUND_EXPR in the chain will contain the tree for the simplified
11138 form of the builtin function call. */
11141 fold_builtin_strcspn (location_t loc
, tree s1
, tree s2
)
11143 if (!validate_arg (s1
, POINTER_TYPE
)
11144 || !validate_arg (s2
, POINTER_TYPE
))
11148 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
11150 /* If both arguments are constants, evaluate at compile-time. */
11153 const size_t r
= strcspn (p1
, p2
);
11154 return build_int_cst (size_type_node
, r
);
11157 /* If the first argument is "", return NULL_TREE. */
11158 if (p1
&& *p1
== '\0')
11160 /* Evaluate and ignore argument s2 in case it has
11162 return omit_one_operand_loc (loc
, size_type_node
,
11163 size_zero_node
, s2
);
11166 /* If the second argument is "", return __builtin_strlen(s1). */
11167 if (p2
&& *p2
== '\0')
11169 tree fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
11171 /* If the replacement _DECL isn't initialized, don't do the
11176 return build_call_expr_loc (loc
, fn
, 1, s1
);
11182 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11183 produced. False otherwise. This is done so that we don't output the error
11184 or warning twice or three times. */
11187 fold_builtin_next_arg (tree exp
, bool va_start_p
)
11189 tree fntype
= TREE_TYPE (current_function_decl
);
11190 int nargs
= call_expr_nargs (exp
);
11192 /* There is good chance the current input_location points inside the
11193 definition of the va_start macro (perhaps on the token for
11194 builtin) in a system header, so warnings will not be emitted.
11195 Use the location in real source code. */
11196 source_location current_location
=
11197 linemap_unwind_to_first_non_reserved_loc (line_table
, input_location
,
11200 if (!stdarg_p (fntype
))
11202 error ("%<va_start%> used in function with fixed args");
11208 if (va_start_p
&& (nargs
!= 2))
11210 error ("wrong number of arguments to function %<va_start%>");
11213 arg
= CALL_EXPR_ARG (exp
, 1);
11215 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11216 when we checked the arguments and if needed issued a warning. */
11221 /* Evidently an out of date version of <stdarg.h>; can't validate
11222 va_start's second argument, but can still work as intended. */
11223 warning_at (current_location
,
11225 "%<__builtin_next_arg%> called without an argument");
11228 else if (nargs
> 1)
11230 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11233 arg
= CALL_EXPR_ARG (exp
, 0);
11236 if (TREE_CODE (arg
) == SSA_NAME
)
11237 arg
= SSA_NAME_VAR (arg
);
11239 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11240 or __builtin_next_arg (0) the first time we see it, after checking
11241 the arguments and if needed issuing a warning. */
11242 if (!integer_zerop (arg
))
11244 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
11246 /* Strip off all nops for the sake of the comparison. This
11247 is not quite the same as STRIP_NOPS. It does more.
11248 We must also strip off INDIRECT_EXPR for C++ reference
11250 while (CONVERT_EXPR_P (arg
)
11251 || TREE_CODE (arg
) == INDIRECT_REF
)
11252 arg
= TREE_OPERAND (arg
, 0);
11253 if (arg
!= last_parm
)
11255 /* FIXME: Sometimes with the tree optimizers we can get the
11256 not the last argument even though the user used the last
11257 argument. We just warn and set the arg to be the last
11258 argument so that we will get wrong-code because of
11260 warning_at (current_location
,
11262 "second parameter of %<va_start%> not last named argument");
11265 /* Undefined by C99 7.15.1.4p4 (va_start):
11266 "If the parameter parmN is declared with the register storage
11267 class, with a function or array type, or with a type that is
11268 not compatible with the type that results after application of
11269 the default argument promotions, the behavior is undefined."
11271 else if (DECL_REGISTER (arg
))
11273 warning_at (current_location
,
11275 "undefined behaviour when second parameter of "
11276 "%<va_start%> is declared with %<register%> storage");
11279 /* We want to verify the second parameter just once before the tree
11280 optimizers are run and then avoid keeping it in the tree,
11281 as otherwise we could warn even for correct code like:
11282 void foo (int i, ...)
11283 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11285 CALL_EXPR_ARG (exp
, 1) = integer_zero_node
;
11287 CALL_EXPR_ARG (exp
, 0) = integer_zero_node
;
11293 /* Expand a call EXP to __builtin_object_size. */
11296 expand_builtin_object_size (tree exp
)
11299 int object_size_type
;
11300 tree fndecl
= get_callee_fndecl (exp
);
11302 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
11304 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11306 expand_builtin_trap ();
11310 ost
= CALL_EXPR_ARG (exp
, 1);
11313 if (TREE_CODE (ost
) != INTEGER_CST
11314 || tree_int_cst_sgn (ost
) < 0
11315 || compare_tree_int (ost
, 3) > 0)
11317 error ("%Klast argument of %D is not integer constant between 0 and 3",
11319 expand_builtin_trap ();
11323 object_size_type
= tree_to_shwi (ost
);
11325 return object_size_type
< 2 ? constm1_rtx
: const0_rtx
;
11328 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11329 FCODE is the BUILT_IN_* to use.
11330 Return NULL_RTX if we failed; the caller should emit a normal call,
11331 otherwise try to get the result in TARGET, if convenient (and in
11332 mode MODE if that's convenient). */
11335 expand_builtin_memory_chk (tree exp
, rtx target
, machine_mode mode
,
11336 enum built_in_function fcode
)
11338 tree dest
, src
, len
, size
;
11340 if (!validate_arglist (exp
,
11342 fcode
== BUILT_IN_MEMSET_CHK
11343 ? INTEGER_TYPE
: POINTER_TYPE
,
11344 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
11347 dest
= CALL_EXPR_ARG (exp
, 0);
11348 src
= CALL_EXPR_ARG (exp
, 1);
11349 len
= CALL_EXPR_ARG (exp
, 2);
11350 size
= CALL_EXPR_ARG (exp
, 3);
11352 if (! tree_fits_uhwi_p (size
))
11355 if (tree_fits_uhwi_p (len
) || integer_all_onesp (size
))
11359 if (! integer_all_onesp (size
) && tree_int_cst_lt (size
, len
))
11361 warning_at (tree_nonartificial_location (exp
),
11362 0, "%Kcall to %D will always overflow destination buffer",
11363 exp
, get_callee_fndecl (exp
));
11368 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11369 mem{cpy,pcpy,move,set} is available. */
11372 case BUILT_IN_MEMCPY_CHK
:
11373 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
11375 case BUILT_IN_MEMPCPY_CHK
:
11376 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
11378 case BUILT_IN_MEMMOVE_CHK
:
11379 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
11381 case BUILT_IN_MEMSET_CHK
:
11382 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
11391 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 3, dest
, src
, len
);
11392 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
11393 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
11394 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
11396 else if (fcode
== BUILT_IN_MEMSET_CHK
)
11400 unsigned int dest_align
= get_pointer_alignment (dest
);
11402 /* If DEST is not a pointer type, call the normal function. */
11403 if (dest_align
== 0)
11406 /* If SRC and DEST are the same (and not volatile), do nothing. */
11407 if (operand_equal_p (src
, dest
, 0))
11411 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
11413 /* Evaluate and ignore LEN in case it has side-effects. */
11414 expand_expr (len
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
11415 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
11418 expr
= fold_build_pointer_plus (dest
, len
);
11419 return expand_expr (expr
, target
, mode
, EXPAND_NORMAL
);
11422 /* __memmove_chk special case. */
11423 if (fcode
== BUILT_IN_MEMMOVE_CHK
)
11425 unsigned int src_align
= get_pointer_alignment (src
);
11427 if (src_align
== 0)
11430 /* If src is categorized for a readonly section we can use
11431 normal __memcpy_chk. */
11432 if (readonly_data_expr (src
))
11434 tree fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
11437 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 4,
11438 dest
, src
, len
, size
);
11439 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
11440 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
11441 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
11448 /* Emit warning if a buffer overflow is detected at compile time. */
11451 maybe_emit_chk_warning (tree exp
, enum built_in_function fcode
)
11455 location_t loc
= tree_nonartificial_location (exp
);
11459 case BUILT_IN_STRCPY_CHK
:
11460 case BUILT_IN_STPCPY_CHK
:
11461 /* For __strcat_chk the warning will be emitted only if overflowing
11462 by at least strlen (dest) + 1 bytes. */
11463 case BUILT_IN_STRCAT_CHK
:
11464 len
= CALL_EXPR_ARG (exp
, 1);
11465 size
= CALL_EXPR_ARG (exp
, 2);
11468 case BUILT_IN_STRNCAT_CHK
:
11469 case BUILT_IN_STRNCPY_CHK
:
11470 case BUILT_IN_STPNCPY_CHK
:
11471 len
= CALL_EXPR_ARG (exp
, 2);
11472 size
= CALL_EXPR_ARG (exp
, 3);
11474 case BUILT_IN_SNPRINTF_CHK
:
11475 case BUILT_IN_VSNPRINTF_CHK
:
11476 len
= CALL_EXPR_ARG (exp
, 1);
11477 size
= CALL_EXPR_ARG (exp
, 3);
11480 gcc_unreachable ();
11486 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
11491 len
= c_strlen (len
, 1);
11492 if (! len
|| ! tree_fits_uhwi_p (len
) || tree_int_cst_lt (len
, size
))
11495 else if (fcode
== BUILT_IN_STRNCAT_CHK
)
11497 tree src
= CALL_EXPR_ARG (exp
, 1);
11498 if (! src
|| ! tree_fits_uhwi_p (len
) || tree_int_cst_lt (len
, size
))
11500 src
= c_strlen (src
, 1);
11501 if (! src
|| ! tree_fits_uhwi_p (src
))
11503 warning_at (loc
, 0, "%Kcall to %D might overflow destination buffer",
11504 exp
, get_callee_fndecl (exp
));
11507 else if (tree_int_cst_lt (src
, size
))
11510 else if (! tree_fits_uhwi_p (len
) || ! tree_int_cst_lt (size
, len
))
11513 warning_at (loc
, 0, "%Kcall to %D will always overflow destination buffer",
11514 exp
, get_callee_fndecl (exp
));
11517 /* Emit warning if a buffer overflow is detected at compile time
11518 in __sprintf_chk/__vsprintf_chk calls. */
11521 maybe_emit_sprintf_chk_warning (tree exp
, enum built_in_function fcode
)
11523 tree size
, len
, fmt
;
11524 const char *fmt_str
;
11525 int nargs
= call_expr_nargs (exp
);
11527 /* Verify the required arguments in the original call. */
11531 size
= CALL_EXPR_ARG (exp
, 2);
11532 fmt
= CALL_EXPR_ARG (exp
, 3);
11534 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
11537 /* Check whether the format is a literal string constant. */
11538 fmt_str
= c_getstr (fmt
);
11539 if (fmt_str
== NULL
)
11542 if (!init_target_chars ())
11545 /* If the format doesn't contain % args or %%, we know its size. */
11546 if (strchr (fmt_str
, target_percent
) == 0)
11547 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
11548 /* If the format is "%s" and first ... argument is a string literal,
11550 else if (fcode
== BUILT_IN_SPRINTF_CHK
11551 && strcmp (fmt_str
, target_percent_s
) == 0)
11557 arg
= CALL_EXPR_ARG (exp
, 4);
11558 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
11561 len
= c_strlen (arg
, 1);
11562 if (!len
|| ! tree_fits_uhwi_p (len
))
11568 if (! tree_int_cst_lt (len
, size
))
11569 warning_at (tree_nonartificial_location (exp
),
11570 0, "%Kcall to %D will always overflow destination buffer",
11571 exp
, get_callee_fndecl (exp
));
11574 /* Emit warning if a free is called with address of a variable. */
11577 maybe_emit_free_warning (tree exp
)
11579 tree arg
= CALL_EXPR_ARG (exp
, 0);
11582 if (TREE_CODE (arg
) != ADDR_EXPR
)
11585 arg
= get_base_address (TREE_OPERAND (arg
, 0));
11586 if (arg
== NULL
|| INDIRECT_REF_P (arg
) || TREE_CODE (arg
) == MEM_REF
)
11589 if (SSA_VAR_P (arg
))
11590 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
11591 "%Kattempt to free a non-heap object %qD", exp
, arg
);
11593 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
11594 "%Kattempt to free a non-heap object", exp
);
11597 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11601 fold_builtin_object_size (tree ptr
, tree ost
)
11603 unsigned HOST_WIDE_INT bytes
;
11604 int object_size_type
;
11606 if (!validate_arg (ptr
, POINTER_TYPE
)
11607 || !validate_arg (ost
, INTEGER_TYPE
))
11612 if (TREE_CODE (ost
) != INTEGER_CST
11613 || tree_int_cst_sgn (ost
) < 0
11614 || compare_tree_int (ost
, 3) > 0)
11617 object_size_type
= tree_to_shwi (ost
);
11619 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11620 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11621 and (size_t) 0 for types 2 and 3. */
11622 if (TREE_SIDE_EFFECTS (ptr
))
11623 return build_int_cst_type (size_type_node
, object_size_type
< 2 ? -1 : 0);
11625 if (TREE_CODE (ptr
) == ADDR_EXPR
)
11627 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
11628 if (wi::fits_to_tree_p (bytes
, size_type_node
))
11629 return build_int_cstu (size_type_node
, bytes
);
11631 else if (TREE_CODE (ptr
) == SSA_NAME
)
11633 /* If object size is not known yet, delay folding until
11634 later. Maybe subsequent passes will help determining
11636 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
11637 if (bytes
!= (unsigned HOST_WIDE_INT
) (object_size_type
< 2 ? -1 : 0)
11638 && wi::fits_to_tree_p (bytes
, size_type_node
))
11639 return build_int_cstu (size_type_node
, bytes
);
11645 /* Builtins with folding operations that operate on "..." arguments
11646 need special handling; we need to store the arguments in a convenient
11647 data structure before attempting any folding. Fortunately there are
11648 only a few builtins that fall into this category. FNDECL is the
11649 function, EXP is the CALL_EXPR for the call. */
11652 fold_builtin_varargs (location_t loc
, tree fndecl
, tree
*args
, int nargs
)
11654 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
11655 tree ret
= NULL_TREE
;
11659 case BUILT_IN_FPCLASSIFY
:
11660 ret
= fold_builtin_fpclassify (loc
, args
, nargs
);
11668 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
11669 SET_EXPR_LOCATION (ret
, loc
);
11670 TREE_NO_WARNING (ret
) = 1;
11676 /* Initialize format string characters in the target charset. */
11679 init_target_chars (void)
11684 target_newline
= lang_hooks
.to_target_charset ('\n');
11685 target_percent
= lang_hooks
.to_target_charset ('%');
11686 target_c
= lang_hooks
.to_target_charset ('c');
11687 target_s
= lang_hooks
.to_target_charset ('s');
11688 if (target_newline
== 0 || target_percent
== 0 || target_c
== 0
11692 target_percent_c
[0] = target_percent
;
11693 target_percent_c
[1] = target_c
;
11694 target_percent_c
[2] = '\0';
11696 target_percent_s
[0] = target_percent
;
11697 target_percent_s
[1] = target_s
;
11698 target_percent_s
[2] = '\0';
11700 target_percent_s_newline
[0] = target_percent
;
11701 target_percent_s_newline
[1] = target_s
;
11702 target_percent_s_newline
[2] = target_newline
;
11703 target_percent_s_newline
[3] = '\0';
11710 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11711 and no overflow/underflow occurred. INEXACT is true if M was not
11712 exactly calculated. TYPE is the tree type for the result. This
11713 function assumes that you cleared the MPFR flags and then
11714 calculated M to see if anything subsequently set a flag prior to
11715 entering this function. Return NULL_TREE if any checks fail. */
11718 do_mpfr_ckconv (mpfr_srcptr m
, tree type
, int inexact
)
11720 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11721 overflow/underflow occurred. If -frounding-math, proceed iff the
11722 result of calling FUNC was exact. */
11723 if (mpfr_number_p (m
) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11724 && (!flag_rounding_math
|| !inexact
))
11726 REAL_VALUE_TYPE rr
;
11728 real_from_mpfr (&rr
, m
, type
, GMP_RNDN
);
11729 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11730 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11731 but the mpft_t is not, then we underflowed in the
11733 if (real_isfinite (&rr
)
11734 && (rr
.cl
== rvc_zero
) == (mpfr_zero_p (m
) != 0))
11736 REAL_VALUE_TYPE rmode
;
11738 real_convert (&rmode
, TYPE_MODE (type
), &rr
);
11739 /* Proceed iff the specified mode can hold the value. */
11740 if (real_identical (&rmode
, &rr
))
11741 return build_real (type
, rmode
);
11747 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
11748 number and no overflow/underflow occurred. INEXACT is true if M
11749 was not exactly calculated. TYPE is the tree type for the result.
11750 This function assumes that you cleared the MPFR flags and then
11751 calculated M to see if anything subsequently set a flag prior to
11752 entering this function. Return NULL_TREE if any checks fail, if
11753 FORCE_CONVERT is true, then bypass the checks. */
11756 do_mpc_ckconv (mpc_srcptr m
, tree type
, int inexact
, int force_convert
)
11758 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11759 overflow/underflow occurred. If -frounding-math, proceed iff the
11760 result of calling FUNC was exact. */
11762 || (mpfr_number_p (mpc_realref (m
)) && mpfr_number_p (mpc_imagref (m
))
11763 && !mpfr_overflow_p () && !mpfr_underflow_p ()
11764 && (!flag_rounding_math
|| !inexact
)))
11766 REAL_VALUE_TYPE re
, im
;
11768 real_from_mpfr (&re
, mpc_realref (m
), TREE_TYPE (type
), GMP_RNDN
);
11769 real_from_mpfr (&im
, mpc_imagref (m
), TREE_TYPE (type
), GMP_RNDN
);
11770 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11771 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11772 but the mpft_t is not, then we underflowed in the
11775 || (real_isfinite (&re
) && real_isfinite (&im
)
11776 && (re
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_realref (m
)) != 0)
11777 && (im
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_imagref (m
)) != 0)))
11779 REAL_VALUE_TYPE re_mode
, im_mode
;
11781 real_convert (&re_mode
, TYPE_MODE (TREE_TYPE (type
)), &re
);
11782 real_convert (&im_mode
, TYPE_MODE (TREE_TYPE (type
)), &im
);
11783 /* Proceed iff the specified mode can hold the value. */
11785 || (real_identical (&re_mode
, &re
)
11786 && real_identical (&im_mode
, &im
)))
11787 return build_complex (type
, build_real (TREE_TYPE (type
), re_mode
),
11788 build_real (TREE_TYPE (type
), im_mode
));
11794 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
11795 FUNC on it and return the resulting value as a tree with type TYPE.
11796 If MIN and/or MAX are not NULL, then the supplied ARG must be
11797 within those bounds. If INCLUSIVE is true, then MIN/MAX are
11798 acceptable values, otherwise they are not. The mpfr precision is
11799 set to the precision of TYPE. We assume that function FUNC returns
11800 zero if the result could be calculated exactly within the requested
11804 do_mpfr_arg1 (tree arg
, tree type
, int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
11805 const REAL_VALUE_TYPE
*min
, const REAL_VALUE_TYPE
*max
,
11808 tree result
= NULL_TREE
;
11812 /* To proceed, MPFR must exactly represent the target floating point
11813 format, which only happens when the target base equals two. */
11814 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
11815 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
11817 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
11819 if (real_isfinite (ra
)
11820 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
))
11821 && (!max
|| real_compare (inclusive
? LE_EXPR
: LT_EXPR
, ra
, max
)))
11823 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
11824 const int prec
= fmt
->p
;
11825 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
11829 mpfr_init2 (m
, prec
);
11830 mpfr_from_real (m
, ra
, GMP_RNDN
);
11831 mpfr_clear_flags ();
11832 inexact
= func (m
, m
, rnd
);
11833 result
= do_mpfr_ckconv (m
, type
, inexact
);
11841 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
11842 FUNC on it and return the resulting value as a tree with type TYPE.
11843 The mpfr precision is set to the precision of TYPE. We assume that
11844 function FUNC returns zero if the result could be calculated
11845 exactly within the requested precision. */
11848 do_mpfr_arg2 (tree arg1
, tree arg2
, tree type
,
11849 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
11851 tree result
= NULL_TREE
;
11856 /* To proceed, MPFR must exactly represent the target floating point
11857 format, which only happens when the target base equals two. */
11858 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
11859 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
)
11860 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
))
11862 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
11863 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
11865 if (real_isfinite (ra1
) && real_isfinite (ra2
))
11867 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
11868 const int prec
= fmt
->p
;
11869 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
11873 mpfr_inits2 (prec
, m1
, m2
, NULL
);
11874 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
11875 mpfr_from_real (m2
, ra2
, GMP_RNDN
);
11876 mpfr_clear_flags ();
11877 inexact
= func (m1
, m1
, m2
, rnd
);
11878 result
= do_mpfr_ckconv (m1
, type
, inexact
);
11879 mpfr_clears (m1
, m2
, NULL
);
11886 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
11887 FUNC on it and return the resulting value as a tree with type TYPE.
11888 The mpfr precision is set to the precision of TYPE. We assume that
11889 function FUNC returns zero if the result could be calculated
11890 exactly within the requested precision. */
11893 do_mpfr_arg3 (tree arg1
, tree arg2
, tree arg3
, tree type
,
11894 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
11896 tree result
= NULL_TREE
;
11902 /* To proceed, MPFR must exactly represent the target floating point
11903 format, which only happens when the target base equals two. */
11904 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
11905 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
)
11906 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
)
11907 && TREE_CODE (arg3
) == REAL_CST
&& !TREE_OVERFLOW (arg3
))
11909 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
11910 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
11911 const REAL_VALUE_TYPE
*const ra3
= &TREE_REAL_CST (arg3
);
11913 if (real_isfinite (ra1
) && real_isfinite (ra2
) && real_isfinite (ra3
))
11915 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
11916 const int prec
= fmt
->p
;
11917 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
11921 mpfr_inits2 (prec
, m1
, m2
, m3
, NULL
);
11922 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
11923 mpfr_from_real (m2
, ra2
, GMP_RNDN
);
11924 mpfr_from_real (m3
, ra3
, GMP_RNDN
);
11925 mpfr_clear_flags ();
11926 inexact
= func (m1
, m1
, m2
, m3
, rnd
);
11927 result
= do_mpfr_ckconv (m1
, type
, inexact
);
11928 mpfr_clears (m1
, m2
, m3
, NULL
);
11935 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
11936 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
11937 If ARG_SINP and ARG_COSP are NULL then the result is returned
11938 as a complex value.
11939 The type is taken from the type of ARG and is used for setting the
11940 precision of the calculation and results. */
11943 do_mpfr_sincos (tree arg
, tree arg_sinp
, tree arg_cosp
)
11945 tree
const type
= TREE_TYPE (arg
);
11946 tree result
= NULL_TREE
;
11950 /* To proceed, MPFR must exactly represent the target floating point
11951 format, which only happens when the target base equals two. */
11952 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
11953 && TREE_CODE (arg
) == REAL_CST
11954 && !TREE_OVERFLOW (arg
))
11956 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
11958 if (real_isfinite (ra
))
11960 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
11961 const int prec
= fmt
->p
;
11962 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
11963 tree result_s
, result_c
;
11967 mpfr_inits2 (prec
, m
, ms
, mc
, NULL
);
11968 mpfr_from_real (m
, ra
, GMP_RNDN
);
11969 mpfr_clear_flags ();
11970 inexact
= mpfr_sin_cos (ms
, mc
, m
, rnd
);
11971 result_s
= do_mpfr_ckconv (ms
, type
, inexact
);
11972 result_c
= do_mpfr_ckconv (mc
, type
, inexact
);
11973 mpfr_clears (m
, ms
, mc
, NULL
);
11974 if (result_s
&& result_c
)
11976 /* If we are to return in a complex value do so. */
11977 if (!arg_sinp
&& !arg_cosp
)
11978 return build_complex (build_complex_type (type
),
11979 result_c
, result_s
);
11981 /* Dereference the sin/cos pointer arguments. */
11982 arg_sinp
= build_fold_indirect_ref (arg_sinp
);
11983 arg_cosp
= build_fold_indirect_ref (arg_cosp
);
11984 /* Proceed if valid pointer type were passed in. */
11985 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp
)) == TYPE_MAIN_VARIANT (type
)
11986 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp
)) == TYPE_MAIN_VARIANT (type
))
11988 /* Set the values. */
11989 result_s
= fold_build2 (MODIFY_EXPR
, type
, arg_sinp
,
11991 TREE_SIDE_EFFECTS (result_s
) = 1;
11992 result_c
= fold_build2 (MODIFY_EXPR
, type
, arg_cosp
,
11994 TREE_SIDE_EFFECTS (result_c
) = 1;
11995 /* Combine the assignments into a compound expr. */
11996 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
11997 result_s
, result_c
));
12005 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
12006 two-argument mpfr order N Bessel function FUNC on them and return
12007 the resulting value as a tree with type TYPE. The mpfr precision
12008 is set to the precision of TYPE. We assume that function FUNC
12009 returns zero if the result could be calculated exactly within the
12010 requested precision. */
12012 do_mpfr_bessel_n (tree arg1
, tree arg2
, tree type
,
12013 int (*func
)(mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
12014 const REAL_VALUE_TYPE
*min
, bool inclusive
)
12016 tree result
= NULL_TREE
;
12021 /* To proceed, MPFR must exactly represent the target floating point
12022 format, which only happens when the target base equals two. */
12023 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12024 && tree_fits_shwi_p (arg1
)
12025 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
))
12027 const HOST_WIDE_INT n
= tree_to_shwi (arg1
);
12028 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg2
);
12031 && real_isfinite (ra
)
12032 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
)))
12034 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12035 const int prec
= fmt
->p
;
12036 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12040 mpfr_init2 (m
, prec
);
12041 mpfr_from_real (m
, ra
, GMP_RNDN
);
12042 mpfr_clear_flags ();
12043 inexact
= func (m
, n
, m
, rnd
);
12044 result
= do_mpfr_ckconv (m
, type
, inexact
);
12052 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12053 the pointer *(ARG_QUO) and return the result. The type is taken
12054 from the type of ARG0 and is used for setting the precision of the
12055 calculation and results. */
12058 do_mpfr_remquo (tree arg0
, tree arg1
, tree arg_quo
)
12060 tree
const type
= TREE_TYPE (arg0
);
12061 tree result
= NULL_TREE
;
12066 /* To proceed, MPFR must exactly represent the target floating point
12067 format, which only happens when the target base equals two. */
12068 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12069 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
12070 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
))
12072 const REAL_VALUE_TYPE
*const ra0
= TREE_REAL_CST_PTR (arg0
);
12073 const REAL_VALUE_TYPE
*const ra1
= TREE_REAL_CST_PTR (arg1
);
12075 if (real_isfinite (ra0
) && real_isfinite (ra1
))
12077 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12078 const int prec
= fmt
->p
;
12079 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12084 mpfr_inits2 (prec
, m0
, m1
, NULL
);
12085 mpfr_from_real (m0
, ra0
, GMP_RNDN
);
12086 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
12087 mpfr_clear_flags ();
12088 mpfr_remquo (m0
, &integer_quo
, m0
, m1
, rnd
);
12089 /* Remquo is independent of the rounding mode, so pass
12090 inexact=0 to do_mpfr_ckconv(). */
12091 result_rem
= do_mpfr_ckconv (m0
, type
, /*inexact=*/ 0);
12092 mpfr_clears (m0
, m1
, NULL
);
12095 /* MPFR calculates quo in the host's long so it may
12096 return more bits in quo than the target int can hold
12097 if sizeof(host long) > sizeof(target int). This can
12098 happen even for native compilers in LP64 mode. In
12099 these cases, modulo the quo value with the largest
12100 number that the target int can hold while leaving one
12101 bit for the sign. */
12102 if (sizeof (integer_quo
) * CHAR_BIT
> INT_TYPE_SIZE
)
12103 integer_quo
%= (long)(1UL << (INT_TYPE_SIZE
- 1));
12105 /* Dereference the quo pointer argument. */
12106 arg_quo
= build_fold_indirect_ref (arg_quo
);
12107 /* Proceed iff a valid pointer type was passed in. */
12108 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo
)) == integer_type_node
)
12110 /* Set the value. */
12112 = fold_build2 (MODIFY_EXPR
, TREE_TYPE (arg_quo
), arg_quo
,
12113 build_int_cst (TREE_TYPE (arg_quo
),
12115 TREE_SIDE_EFFECTS (result_quo
) = 1;
12116 /* Combine the quo assignment with the rem. */
12117 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
12118 result_quo
, result_rem
));
12126 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12127 resulting value as a tree with type TYPE. The mpfr precision is
12128 set to the precision of TYPE. We assume that this mpfr function
12129 returns zero if the result could be calculated exactly within the
12130 requested precision. In addition, the integer pointer represented
12131 by ARG_SG will be dereferenced and set to the appropriate signgam
12135 do_mpfr_lgamma_r (tree arg
, tree arg_sg
, tree type
)
12137 tree result
= NULL_TREE
;
12141 /* To proceed, MPFR must exactly represent the target floating point
12142 format, which only happens when the target base equals two. Also
12143 verify ARG is a constant and that ARG_SG is an int pointer. */
12144 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12145 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
)
12146 && TREE_CODE (TREE_TYPE (arg_sg
)) == POINTER_TYPE
12147 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg
))) == integer_type_node
)
12149 const REAL_VALUE_TYPE
*const ra
= TREE_REAL_CST_PTR (arg
);
12151 /* In addition to NaN and Inf, the argument cannot be zero or a
12152 negative integer. */
12153 if (real_isfinite (ra
)
12154 && ra
->cl
!= rvc_zero
12155 && !(real_isneg (ra
) && real_isinteger (ra
, TYPE_MODE (type
))))
12157 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12158 const int prec
= fmt
->p
;
12159 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12164 mpfr_init2 (m
, prec
);
12165 mpfr_from_real (m
, ra
, GMP_RNDN
);
12166 mpfr_clear_flags ();
12167 inexact
= mpfr_lgamma (m
, &sg
, m
, rnd
);
12168 result_lg
= do_mpfr_ckconv (m
, type
, inexact
);
12174 /* Dereference the arg_sg pointer argument. */
12175 arg_sg
= build_fold_indirect_ref (arg_sg
);
12176 /* Assign the signgam value into *arg_sg. */
12177 result_sg
= fold_build2 (MODIFY_EXPR
,
12178 TREE_TYPE (arg_sg
), arg_sg
,
12179 build_int_cst (TREE_TYPE (arg_sg
), sg
));
12180 TREE_SIDE_EFFECTS (result_sg
) = 1;
12181 /* Combine the signgam assignment with the lgamma result. */
12182 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
12183 result_sg
, result_lg
));
12191 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
12192 function FUNC on it and return the resulting value as a tree with
12193 type TYPE. The mpfr precision is set to the precision of TYPE. We
12194 assume that function FUNC returns zero if the result could be
12195 calculated exactly within the requested precision. */
12198 do_mpc_arg1 (tree arg
, tree type
, int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_rnd_t
))
12200 tree result
= NULL_TREE
;
12204 /* To proceed, MPFR must exactly represent the target floating point
12205 format, which only happens when the target base equals two. */
12206 if (TREE_CODE (arg
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg
)
12207 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
12208 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg
))))->b
== 2)
12210 const REAL_VALUE_TYPE
*const re
= TREE_REAL_CST_PTR (TREE_REALPART (arg
));
12211 const REAL_VALUE_TYPE
*const im
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg
));
12213 if (real_isfinite (re
) && real_isfinite (im
))
12215 const struct real_format
*const fmt
=
12216 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
12217 const int prec
= fmt
->p
;
12218 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12219 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
12223 mpc_init2 (m
, prec
);
12224 mpfr_from_real (mpc_realref (m
), re
, rnd
);
12225 mpfr_from_real (mpc_imagref (m
), im
, rnd
);
12226 mpfr_clear_flags ();
12227 inexact
= func (m
, m
, crnd
);
12228 result
= do_mpc_ckconv (m
, type
, inexact
, /*force_convert=*/ 0);
12236 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12237 mpc function FUNC on it and return the resulting value as a tree
12238 with type TYPE. The mpfr precision is set to the precision of
12239 TYPE. We assume that function FUNC returns zero if the result
12240 could be calculated exactly within the requested precision. If
12241 DO_NONFINITE is true, then fold expressions containing Inf or NaN
12242 in the arguments and/or results. */
12245 do_mpc_arg2 (tree arg0
, tree arg1
, tree type
, int do_nonfinite
,
12246 int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_srcptr
, mpc_rnd_t
))
12248 tree result
= NULL_TREE
;
12253 /* To proceed, MPFR must exactly represent the target floating point
12254 format, which only happens when the target base equals two. */
12255 if (TREE_CODE (arg0
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg0
)
12256 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
12257 && TREE_CODE (arg1
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg1
)
12258 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
12259 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0
))))->b
== 2)
12261 const REAL_VALUE_TYPE
*const re0
= TREE_REAL_CST_PTR (TREE_REALPART (arg0
));
12262 const REAL_VALUE_TYPE
*const im0
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg0
));
12263 const REAL_VALUE_TYPE
*const re1
= TREE_REAL_CST_PTR (TREE_REALPART (arg1
));
12264 const REAL_VALUE_TYPE
*const im1
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg1
));
12267 || (real_isfinite (re0
) && real_isfinite (im0
)
12268 && real_isfinite (re1
) && real_isfinite (im1
)))
12270 const struct real_format
*const fmt
=
12271 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
12272 const int prec
= fmt
->p
;
12273 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12274 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
12278 mpc_init2 (m0
, prec
);
12279 mpc_init2 (m1
, prec
);
12280 mpfr_from_real (mpc_realref (m0
), re0
, rnd
);
12281 mpfr_from_real (mpc_imagref (m0
), im0
, rnd
);
12282 mpfr_from_real (mpc_realref (m1
), re1
, rnd
);
12283 mpfr_from_real (mpc_imagref (m1
), im1
, rnd
);
12284 mpfr_clear_flags ();
12285 inexact
= func (m0
, m0
, m1
, crnd
);
12286 result
= do_mpc_ckconv (m0
, type
, inexact
, do_nonfinite
);
12295 /* A wrapper function for builtin folding that prevents warnings for
12296 "statement without effect" and the like, caused by removing the
12297 call node earlier than the warning is generated. */
12300 fold_call_stmt (gcall
*stmt
, bool ignore
)
12302 tree ret
= NULL_TREE
;
12303 tree fndecl
= gimple_call_fndecl (stmt
);
12304 location_t loc
= gimple_location (stmt
);
12306 && TREE_CODE (fndecl
) == FUNCTION_DECL
12307 && DECL_BUILT_IN (fndecl
)
12308 && !gimple_call_va_arg_pack_p (stmt
))
12310 int nargs
= gimple_call_num_args (stmt
);
12311 tree
*args
= (nargs
> 0
12312 ? gimple_call_arg_ptr (stmt
, 0)
12313 : &error_mark_node
);
12315 if (avoid_folding_inline_builtin (fndecl
))
12317 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
12319 return targetm
.fold_builtin (fndecl
, nargs
, args
, ignore
);
12323 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
12326 /* Propagate location information from original call to
12327 expansion of builtin. Otherwise things like
12328 maybe_emit_chk_warning, that operate on the expansion
12329 of a builtin, will use the wrong location information. */
12330 if (gimple_has_location (stmt
))
12332 tree realret
= ret
;
12333 if (TREE_CODE (ret
) == NOP_EXPR
)
12334 realret
= TREE_OPERAND (ret
, 0);
12335 if (CAN_HAVE_LOCATION_P (realret
)
12336 && !EXPR_HAS_LOCATION (realret
))
12337 SET_EXPR_LOCATION (realret
, loc
);
12347 /* Look up the function in builtin_decl that corresponds to DECL
12348 and set ASMSPEC as its user assembler name. DECL must be a
12349 function decl that declares a builtin. */
12352 set_builtin_user_assembler_name (tree decl
, const char *asmspec
)
12355 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
12356 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
12359 builtin
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
12360 set_user_assembler_name (builtin
, asmspec
);
12361 switch (DECL_FUNCTION_CODE (decl
))
12363 case BUILT_IN_MEMCPY
:
12364 init_block_move_fn (asmspec
);
12365 memcpy_libfunc
= set_user_assembler_libfunc ("memcpy", asmspec
);
12367 case BUILT_IN_MEMSET
:
12368 init_block_clear_fn (asmspec
);
12369 memset_libfunc
= set_user_assembler_libfunc ("memset", asmspec
);
12371 case BUILT_IN_MEMMOVE
:
12372 memmove_libfunc
= set_user_assembler_libfunc ("memmove", asmspec
);
12374 case BUILT_IN_MEMCMP
:
12375 memcmp_libfunc
= set_user_assembler_libfunc ("memcmp", asmspec
);
12377 case BUILT_IN_ABORT
:
12378 abort_libfunc
= set_user_assembler_libfunc ("abort", asmspec
);
12381 if (INT_TYPE_SIZE
< BITS_PER_WORD
)
12383 set_user_assembler_libfunc ("ffs", asmspec
);
12384 set_optab_libfunc (ffs_optab
, mode_for_size (INT_TYPE_SIZE
,
12385 MODE_INT
, 0), "ffs");
12393 /* Return true if DECL is a builtin that expands to a constant or similarly
12396 is_simple_builtin (tree decl
)
12398 if (decl
&& DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
12399 switch (DECL_FUNCTION_CODE (decl
))
12401 /* Builtins that expand to constants. */
12402 case BUILT_IN_CONSTANT_P
:
12403 case BUILT_IN_EXPECT
:
12404 case BUILT_IN_OBJECT_SIZE
:
12405 case BUILT_IN_UNREACHABLE
:
12406 /* Simple register moves or loads from stack. */
12407 case BUILT_IN_ASSUME_ALIGNED
:
12408 case BUILT_IN_RETURN_ADDRESS
:
12409 case BUILT_IN_EXTRACT_RETURN_ADDR
:
12410 case BUILT_IN_FROB_RETURN_ADDR
:
12411 case BUILT_IN_RETURN
:
12412 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
12413 case BUILT_IN_FRAME_ADDRESS
:
12414 case BUILT_IN_VA_END
:
12415 case BUILT_IN_STACK_SAVE
:
12416 case BUILT_IN_STACK_RESTORE
:
12417 /* Exception state returns or moves registers around. */
12418 case BUILT_IN_EH_FILTER
:
12419 case BUILT_IN_EH_POINTER
:
12420 case BUILT_IN_EH_COPY_VALUES
:
12430 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12431 most probably expanded inline into reasonably simple code. This is a
12432 superset of is_simple_builtin. */
12434 is_inexpensive_builtin (tree decl
)
12438 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_MD
)
12440 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
12441 switch (DECL_FUNCTION_CODE (decl
))
12444 case BUILT_IN_ALLOCA
:
12445 case BUILT_IN_ALLOCA_WITH_ALIGN
:
12446 case BUILT_IN_BSWAP16
:
12447 case BUILT_IN_BSWAP32
:
12448 case BUILT_IN_BSWAP64
:
12450 case BUILT_IN_CLZIMAX
:
12451 case BUILT_IN_CLZL
:
12452 case BUILT_IN_CLZLL
:
12454 case BUILT_IN_CTZIMAX
:
12455 case BUILT_IN_CTZL
:
12456 case BUILT_IN_CTZLL
:
12458 case BUILT_IN_FFSIMAX
:
12459 case BUILT_IN_FFSL
:
12460 case BUILT_IN_FFSLL
:
12461 case BUILT_IN_IMAXABS
:
12462 case BUILT_IN_FINITE
:
12463 case BUILT_IN_FINITEF
:
12464 case BUILT_IN_FINITEL
:
12465 case BUILT_IN_FINITED32
:
12466 case BUILT_IN_FINITED64
:
12467 case BUILT_IN_FINITED128
:
12468 case BUILT_IN_FPCLASSIFY
:
12469 case BUILT_IN_ISFINITE
:
12470 case BUILT_IN_ISINF_SIGN
:
12471 case BUILT_IN_ISINF
:
12472 case BUILT_IN_ISINFF
:
12473 case BUILT_IN_ISINFL
:
12474 case BUILT_IN_ISINFD32
:
12475 case BUILT_IN_ISINFD64
:
12476 case BUILT_IN_ISINFD128
:
12477 case BUILT_IN_ISNAN
:
12478 case BUILT_IN_ISNANF
:
12479 case BUILT_IN_ISNANL
:
12480 case BUILT_IN_ISNAND32
:
12481 case BUILT_IN_ISNAND64
:
12482 case BUILT_IN_ISNAND128
:
12483 case BUILT_IN_ISNORMAL
:
12484 case BUILT_IN_ISGREATER
:
12485 case BUILT_IN_ISGREATEREQUAL
:
12486 case BUILT_IN_ISLESS
:
12487 case BUILT_IN_ISLESSEQUAL
:
12488 case BUILT_IN_ISLESSGREATER
:
12489 case BUILT_IN_ISUNORDERED
:
12490 case BUILT_IN_VA_ARG_PACK
:
12491 case BUILT_IN_VA_ARG_PACK_LEN
:
12492 case BUILT_IN_VA_COPY
:
12493 case BUILT_IN_TRAP
:
12494 case BUILT_IN_SAVEREGS
:
12495 case BUILT_IN_POPCOUNTL
:
12496 case BUILT_IN_POPCOUNTLL
:
12497 case BUILT_IN_POPCOUNTIMAX
:
12498 case BUILT_IN_POPCOUNT
:
12499 case BUILT_IN_PARITYL
:
12500 case BUILT_IN_PARITYLL
:
12501 case BUILT_IN_PARITYIMAX
:
12502 case BUILT_IN_PARITY
:
12503 case BUILT_IN_LABS
:
12504 case BUILT_IN_LLABS
:
12505 case BUILT_IN_PREFETCH
:
12506 case BUILT_IN_ACC_ON_DEVICE
:
12510 return is_simple_builtin (decl
);