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"
75 static tree
do_mpc_arg1 (tree
, tree
, int (*)(mpc_ptr
, mpc_srcptr
, mpc_rnd_t
));
77 struct target_builtins default_target_builtins
;
79 struct target_builtins
*this_target_builtins
= &default_target_builtins
;
82 /* Define the names of the builtin function types and codes. */
83 const char *const built_in_class_names
[BUILT_IN_LAST
]
84 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
86 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
87 const char * built_in_names
[(int) END_BUILTINS
] =
89 #include "builtins.def"
93 /* Setup an array of _DECL trees, make sure each element is
94 initialized to NULL_TREE. */
95 builtin_info_type builtin_info
;
97 /* Non-zero if __builtin_constant_p should be folded right away. */
98 bool force_folding_builtin_constant_p
;
100 static rtx
c_readstr (const char *, machine_mode
);
101 static int target_char_cast (tree
, char *);
102 static rtx
get_memory_rtx (tree
, tree
);
103 static int apply_args_size (void);
104 static int apply_result_size (void);
105 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
106 static rtx
result_vector (int, rtx
);
108 static void expand_builtin_update_setjmp_buf (rtx
);
109 static void expand_builtin_prefetch (tree
);
110 static rtx
expand_builtin_apply_args (void);
111 static rtx
expand_builtin_apply_args_1 (void);
112 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
113 static void expand_builtin_return (rtx
);
114 static enum type_class
type_to_class (tree
);
115 static rtx
expand_builtin_classify_type (tree
);
116 static void expand_errno_check (tree
, rtx
);
117 static rtx
expand_builtin_mathfn (tree
, rtx
, rtx
);
118 static rtx
expand_builtin_mathfn_2 (tree
, rtx
, rtx
);
119 static rtx
expand_builtin_mathfn_3 (tree
, rtx
, rtx
);
120 static rtx
expand_builtin_mathfn_ternary (tree
, rtx
, rtx
);
121 static rtx
expand_builtin_interclass_mathfn (tree
, rtx
);
122 static rtx
expand_builtin_sincos (tree
);
123 static rtx
expand_builtin_cexpi (tree
, rtx
);
124 static rtx
expand_builtin_int_roundingfn (tree
, rtx
);
125 static rtx
expand_builtin_int_roundingfn_2 (tree
, rtx
);
126 static rtx
expand_builtin_next_arg (void);
127 static rtx
expand_builtin_va_start (tree
);
128 static rtx
expand_builtin_va_end (tree
);
129 static rtx
expand_builtin_va_copy (tree
);
130 static rtx
expand_builtin_memcmp (tree
, rtx
, machine_mode
);
131 static rtx
expand_builtin_strcmp (tree
, rtx
);
132 static rtx
expand_builtin_strncmp (tree
, rtx
, machine_mode
);
133 static rtx
builtin_memcpy_read_str (void *, HOST_WIDE_INT
, machine_mode
);
134 static rtx
expand_builtin_memcpy (tree
, rtx
);
135 static rtx
expand_builtin_memcpy_with_bounds (tree
, rtx
);
136 static rtx
expand_builtin_memcpy_args (tree
, tree
, tree
, rtx
, tree
);
137 static rtx
expand_builtin_mempcpy (tree
, rtx
, machine_mode
);
138 static rtx
expand_builtin_mempcpy_with_bounds (tree
, rtx
, machine_mode
);
139 static rtx
expand_builtin_mempcpy_args (tree
, tree
, tree
, rtx
,
140 machine_mode
, int, tree
);
141 static rtx
expand_builtin_strcpy (tree
, rtx
);
142 static rtx
expand_builtin_strcpy_args (tree
, tree
, rtx
);
143 static rtx
expand_builtin_stpcpy (tree
, rtx
, machine_mode
);
144 static rtx
expand_builtin_strncpy (tree
, rtx
);
145 static rtx
builtin_memset_gen_str (void *, HOST_WIDE_INT
, machine_mode
);
146 static rtx
expand_builtin_memset (tree
, rtx
, machine_mode
);
147 static rtx
expand_builtin_memset_with_bounds (tree
, rtx
, machine_mode
);
148 static rtx
expand_builtin_memset_args (tree
, tree
, tree
, rtx
, machine_mode
, tree
);
149 static rtx
expand_builtin_bzero (tree
);
150 static rtx
expand_builtin_strlen (tree
, rtx
, machine_mode
);
151 static rtx
expand_builtin_alloca (tree
, bool);
152 static rtx
expand_builtin_unop (machine_mode
, tree
, rtx
, rtx
, optab
);
153 static rtx
expand_builtin_frame_address (tree
, tree
);
154 static tree
stabilize_va_list_loc (location_t
, tree
, int);
155 static rtx
expand_builtin_expect (tree
, rtx
);
156 static tree
fold_builtin_constant_p (tree
);
157 static tree
fold_builtin_classify_type (tree
);
158 static tree
fold_builtin_strlen (location_t
, tree
, tree
);
159 static tree
fold_builtin_inf (location_t
, tree
, int);
160 static tree
fold_builtin_nan (tree
, tree
, int);
161 static tree
rewrite_call_expr (location_t
, tree
, int, tree
, int, ...);
162 static bool validate_arg (const_tree
, enum tree_code code
);
163 static bool integer_valued_real_p (tree
);
164 static tree
fold_trunc_transparent_mathfn (location_t
, tree
, tree
);
165 static rtx
expand_builtin_fabs (tree
, rtx
, rtx
);
166 static rtx
expand_builtin_signbit (tree
, rtx
);
167 static tree
fold_builtin_sqrt (location_t
, tree
, tree
);
168 static tree
fold_builtin_cbrt (location_t
, tree
, tree
);
169 static tree
fold_builtin_pow (location_t
, tree
, tree
, tree
, tree
);
170 static tree
fold_builtin_powi (location_t
, tree
, tree
, tree
, tree
);
171 static tree
fold_builtin_cos (location_t
, tree
, tree
, tree
);
172 static tree
fold_builtin_cosh (location_t
, tree
, tree
, tree
);
173 static tree
fold_builtin_tan (tree
, tree
);
174 static tree
fold_builtin_trunc (location_t
, tree
, tree
);
175 static tree
fold_builtin_floor (location_t
, tree
, tree
);
176 static tree
fold_builtin_ceil (location_t
, tree
, tree
);
177 static tree
fold_builtin_round (location_t
, tree
, tree
);
178 static tree
fold_builtin_int_roundingfn (location_t
, tree
, tree
);
179 static tree
fold_builtin_bitop (tree
, tree
);
180 static tree
fold_builtin_strchr (location_t
, tree
, tree
, tree
);
181 static tree
fold_builtin_memchr (location_t
, tree
, tree
, tree
, tree
);
182 static tree
fold_builtin_memcmp (location_t
, tree
, tree
, tree
);
183 static tree
fold_builtin_strcmp (location_t
, tree
, tree
);
184 static tree
fold_builtin_strncmp (location_t
, tree
, tree
, tree
);
185 static tree
fold_builtin_signbit (location_t
, tree
, tree
);
186 static tree
fold_builtin_copysign (location_t
, tree
, tree
, tree
, tree
);
187 static tree
fold_builtin_isascii (location_t
, tree
);
188 static tree
fold_builtin_toascii (location_t
, tree
);
189 static tree
fold_builtin_isdigit (location_t
, tree
);
190 static tree
fold_builtin_fabs (location_t
, tree
, tree
);
191 static tree
fold_builtin_abs (location_t
, tree
, tree
);
192 static tree
fold_builtin_unordered_cmp (location_t
, tree
, tree
, tree
, enum tree_code
,
194 static tree
fold_builtin_0 (location_t
, tree
, bool);
195 static tree
fold_builtin_1 (location_t
, tree
, tree
, bool);
196 static tree
fold_builtin_2 (location_t
, tree
, tree
, tree
, bool);
197 static tree
fold_builtin_3 (location_t
, tree
, tree
, tree
, tree
, bool);
198 static tree
fold_builtin_4 (location_t
, tree
, tree
, tree
, tree
, tree
, bool);
199 static tree
fold_builtin_varargs (location_t
, tree
, tree
*, int, bool);
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_strncat (location_t
, tree
, tree
, tree
);
205 static tree
fold_builtin_strspn (location_t
, tree
, tree
);
206 static tree
fold_builtin_strcspn (location_t
, tree
, tree
);
208 static rtx
expand_builtin_object_size (tree
);
209 static rtx
expand_builtin_memory_chk (tree
, rtx
, machine_mode
,
210 enum built_in_function
);
211 static void maybe_emit_chk_warning (tree
, enum built_in_function
);
212 static void maybe_emit_sprintf_chk_warning (tree
, enum built_in_function
);
213 static void maybe_emit_free_warning (tree
);
214 static tree
fold_builtin_object_size (tree
, tree
);
215 static tree
fold_builtin_printf (location_t
, tree
, tree
, tree
, bool, enum built_in_function
);
216 static tree
fold_builtin_fprintf (location_t
, tree
, tree
, tree
, tree
, bool,
217 enum built_in_function
);
219 static unsigned HOST_WIDE_INT target_newline
;
220 unsigned HOST_WIDE_INT target_percent
;
221 static unsigned HOST_WIDE_INT target_c
;
222 static unsigned HOST_WIDE_INT target_s
;
223 static char target_percent_c
[3];
224 char target_percent_s
[3];
225 static char target_percent_s_newline
[4];
226 static tree
do_mpfr_arg1 (tree
, tree
, int (*)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
227 const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, bool);
228 static tree
do_mpfr_arg2 (tree
, tree
, tree
,
229 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
230 static tree
do_mpfr_arg3 (tree
, tree
, tree
, tree
,
231 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
232 static tree
do_mpfr_sincos (tree
, tree
, tree
);
233 static tree
do_mpfr_bessel_n (tree
, tree
, tree
,
234 int (*)(mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
235 const REAL_VALUE_TYPE
*, bool);
236 static tree
do_mpfr_remquo (tree
, tree
, tree
);
237 static tree
do_mpfr_lgamma_r (tree
, tree
, tree
);
238 static void expand_builtin_sync_synchronize (void);
240 /* Return true if NAME starts with __builtin_ or __sync_. */
243 is_builtin_name (const char *name
)
245 if (strncmp (name
, "__builtin_", 10) == 0)
247 if (strncmp (name
, "__sync_", 7) == 0)
249 if (strncmp (name
, "__atomic_", 9) == 0)
252 && (!strcmp (name
, "__cilkrts_detach")
253 || !strcmp (name
, "__cilkrts_pop_frame")))
259 /* Return true if DECL is a function symbol representing a built-in. */
262 is_builtin_fn (tree decl
)
264 return TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
);
267 /* Return true if NODE should be considered for inline expansion regardless
268 of the optimization level. This means whenever a function is invoked with
269 its "internal" name, which normally contains the prefix "__builtin". */
272 called_as_built_in (tree node
)
274 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
275 we want the name used to call the function, not the name it
277 const char *name
= IDENTIFIER_POINTER (DECL_NAME (node
));
278 return is_builtin_name (name
);
281 /* Compute values M and N such that M divides (address of EXP - N) and such
282 that N < M. If these numbers can be determined, store M in alignp and N in
283 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
284 *alignp and any bit-offset to *bitposp.
286 Note that the address (and thus the alignment) computed here is based
287 on the address to which a symbol resolves, whereas DECL_ALIGN is based
288 on the address at which an object is actually located. These two
289 addresses are not always the same. For example, on ARM targets,
290 the address &foo of a Thumb function foo() has the lowest bit set,
291 whereas foo() itself starts on an even address.
293 If ADDR_P is true we are taking the address of the memory reference EXP
294 and thus cannot rely on the access taking place. */
297 get_object_alignment_2 (tree exp
, unsigned int *alignp
,
298 unsigned HOST_WIDE_INT
*bitposp
, bool addr_p
)
300 HOST_WIDE_INT bitsize
, bitpos
;
303 int unsignedp
, volatilep
;
304 unsigned int align
= BITS_PER_UNIT
;
305 bool known_alignment
= false;
307 /* Get the innermost object and the constant (bitpos) and possibly
308 variable (offset) offset of the access. */
309 exp
= get_inner_reference (exp
, &bitsize
, &bitpos
, &offset
,
310 &mode
, &unsignedp
, &volatilep
, true);
312 /* Extract alignment information from the innermost object and
313 possibly adjust bitpos and offset. */
314 if (TREE_CODE (exp
) == FUNCTION_DECL
)
316 /* Function addresses can encode extra information besides their
317 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
318 allows the low bit to be used as a virtual bit, we know
319 that the address itself must be at least 2-byte aligned. */
320 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_pfn
)
321 align
= 2 * BITS_PER_UNIT
;
323 else if (TREE_CODE (exp
) == LABEL_DECL
)
325 else if (TREE_CODE (exp
) == CONST_DECL
)
327 /* The alignment of a CONST_DECL is determined by its initializer. */
328 exp
= DECL_INITIAL (exp
);
329 align
= TYPE_ALIGN (TREE_TYPE (exp
));
330 #ifdef CONSTANT_ALIGNMENT
331 if (CONSTANT_CLASS_P (exp
))
332 align
= (unsigned) CONSTANT_ALIGNMENT (exp
, align
);
334 known_alignment
= true;
336 else if (DECL_P (exp
))
338 align
= DECL_ALIGN (exp
);
339 known_alignment
= true;
341 else if (TREE_CODE (exp
) == VIEW_CONVERT_EXPR
)
343 align
= TYPE_ALIGN (TREE_TYPE (exp
));
345 else if (TREE_CODE (exp
) == INDIRECT_REF
346 || TREE_CODE (exp
) == MEM_REF
347 || TREE_CODE (exp
) == TARGET_MEM_REF
)
349 tree addr
= TREE_OPERAND (exp
, 0);
351 unsigned HOST_WIDE_INT ptr_bitpos
;
353 if (TREE_CODE (addr
) == BIT_AND_EXPR
354 && TREE_CODE (TREE_OPERAND (addr
, 1)) == INTEGER_CST
)
356 align
= (TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1))
357 & -TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1)));
358 align
*= BITS_PER_UNIT
;
359 addr
= TREE_OPERAND (addr
, 0);
363 = get_pointer_alignment_1 (addr
, &ptr_align
, &ptr_bitpos
);
364 align
= MAX (ptr_align
, align
);
366 /* The alignment of the pointer operand in a TARGET_MEM_REF
367 has to take the variable offset parts into account. */
368 if (TREE_CODE (exp
) == TARGET_MEM_REF
)
372 unsigned HOST_WIDE_INT step
= 1;
374 step
= TREE_INT_CST_LOW (TMR_STEP (exp
));
375 align
= MIN (align
, (step
& -step
) * BITS_PER_UNIT
);
377 if (TMR_INDEX2 (exp
))
378 align
= BITS_PER_UNIT
;
379 known_alignment
= false;
382 /* When EXP is an actual memory reference then we can use
383 TYPE_ALIGN of a pointer indirection to derive alignment.
384 Do so only if get_pointer_alignment_1 did not reveal absolute
385 alignment knowledge and if using that alignment would
386 improve the situation. */
387 if (!addr_p
&& !known_alignment
388 && TYPE_ALIGN (TREE_TYPE (exp
)) > align
)
389 align
= TYPE_ALIGN (TREE_TYPE (exp
));
392 /* Else adjust bitpos accordingly. */
393 bitpos
+= ptr_bitpos
;
394 if (TREE_CODE (exp
) == MEM_REF
395 || TREE_CODE (exp
) == TARGET_MEM_REF
)
396 bitpos
+= mem_ref_offset (exp
).to_short_addr () * BITS_PER_UNIT
;
399 else if (TREE_CODE (exp
) == STRING_CST
)
401 /* STRING_CST are the only constant objects we allow to be not
402 wrapped inside a CONST_DECL. */
403 align
= TYPE_ALIGN (TREE_TYPE (exp
));
404 #ifdef CONSTANT_ALIGNMENT
405 if (CONSTANT_CLASS_P (exp
))
406 align
= (unsigned) CONSTANT_ALIGNMENT (exp
, align
);
408 known_alignment
= true;
411 /* If there is a non-constant offset part extract the maximum
412 alignment that can prevail. */
415 unsigned int trailing_zeros
= tree_ctz (offset
);
416 if (trailing_zeros
< HOST_BITS_PER_INT
)
418 unsigned int inner
= (1U << trailing_zeros
) * BITS_PER_UNIT
;
420 align
= MIN (align
, inner
);
425 *bitposp
= bitpos
& (*alignp
- 1);
426 return known_alignment
;
429 /* For a memory reference expression EXP compute values M and N such that M
430 divides (&EXP - N) and such that N < M. If these numbers can be determined,
431 store M in alignp and N in *BITPOSP and return true. Otherwise return false
432 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
435 get_object_alignment_1 (tree exp
, unsigned int *alignp
,
436 unsigned HOST_WIDE_INT
*bitposp
)
438 return get_object_alignment_2 (exp
, alignp
, bitposp
, false);
441 /* Return the alignment in bits of EXP, an object. */
444 get_object_alignment (tree exp
)
446 unsigned HOST_WIDE_INT bitpos
= 0;
449 get_object_alignment_1 (exp
, &align
, &bitpos
);
451 /* align and bitpos now specify known low bits of the pointer.
452 ptr & (align - 1) == bitpos. */
455 align
= (bitpos
& -bitpos
);
459 /* For a pointer valued expression EXP compute values M and N such that M
460 divides (EXP - N) and such that N < M. If these numbers can be determined,
461 store M in alignp and N in *BITPOSP and return true. Return false if
462 the results are just a conservative approximation.
464 If EXP is not a pointer, false is returned too. */
467 get_pointer_alignment_1 (tree exp
, unsigned int *alignp
,
468 unsigned HOST_WIDE_INT
*bitposp
)
472 if (TREE_CODE (exp
) == ADDR_EXPR
)
473 return get_object_alignment_2 (TREE_OPERAND (exp
, 0),
474 alignp
, bitposp
, true);
475 else if (TREE_CODE (exp
) == SSA_NAME
476 && POINTER_TYPE_P (TREE_TYPE (exp
)))
478 unsigned int ptr_align
, ptr_misalign
;
479 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (exp
);
481 if (pi
&& get_ptr_info_alignment (pi
, &ptr_align
, &ptr_misalign
))
483 *bitposp
= ptr_misalign
* BITS_PER_UNIT
;
484 *alignp
= ptr_align
* BITS_PER_UNIT
;
485 /* We cannot really tell whether this result is an approximation. */
491 *alignp
= BITS_PER_UNIT
;
495 else if (TREE_CODE (exp
) == INTEGER_CST
)
497 *alignp
= BIGGEST_ALIGNMENT
;
498 *bitposp
= ((TREE_INT_CST_LOW (exp
) * BITS_PER_UNIT
)
499 & (BIGGEST_ALIGNMENT
- 1));
504 *alignp
= BITS_PER_UNIT
;
508 /* Return the alignment in bits of EXP, a pointer valued expression.
509 The alignment returned is, by default, the alignment of the thing that
510 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
512 Otherwise, look at the expression to see if we can do better, i.e., if the
513 expression is actually pointing at an object whose alignment is tighter. */
516 get_pointer_alignment (tree exp
)
518 unsigned HOST_WIDE_INT bitpos
= 0;
521 get_pointer_alignment_1 (exp
, &align
, &bitpos
);
523 /* align and bitpos now specify known low bits of the pointer.
524 ptr & (align - 1) == bitpos. */
527 align
= (bitpos
& -bitpos
);
532 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
533 way, because it could contain a zero byte in the middle.
534 TREE_STRING_LENGTH is the size of the character array, not the string.
536 ONLY_VALUE should be nonzero if the result is not going to be emitted
537 into the instruction stream and zero if it is going to be expanded.
538 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
539 is returned, otherwise NULL, since
540 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
541 evaluate the side-effects.
543 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
544 accesses. Note that this implies the result is not going to be emitted
545 into the instruction stream.
547 The value returned is of type `ssizetype'.
549 Unfortunately, string_constant can't access the values of const char
550 arrays with initializers, so neither can we do so here. */
553 c_strlen (tree src
, int only_value
)
556 HOST_WIDE_INT offset
;
562 if (TREE_CODE (src
) == COND_EXPR
563 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
567 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
);
568 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
);
569 if (tree_int_cst_equal (len1
, len2
))
573 if (TREE_CODE (src
) == COMPOUND_EXPR
574 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
575 return c_strlen (TREE_OPERAND (src
, 1), only_value
);
577 loc
= EXPR_LOC_OR_LOC (src
, input_location
);
579 src
= string_constant (src
, &offset_node
);
583 max
= TREE_STRING_LENGTH (src
) - 1;
584 ptr
= TREE_STRING_POINTER (src
);
586 if (offset_node
&& TREE_CODE (offset_node
) != INTEGER_CST
)
588 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
589 compute the offset to the following null if we don't know where to
590 start searching for it. */
593 for (i
= 0; i
< max
; i
++)
597 /* We don't know the starting offset, but we do know that the string
598 has no internal zero bytes. We can assume that the offset falls
599 within the bounds of the string; otherwise, the programmer deserves
600 what he gets. Subtract the offset from the length of the string,
601 and return that. This would perhaps not be valid if we were dealing
602 with named arrays in addition to literal string constants. */
604 return size_diffop_loc (loc
, size_int (max
), offset_node
);
607 /* We have a known offset into the string. Start searching there for
608 a null character if we can represent it as a single HOST_WIDE_INT. */
609 if (offset_node
== 0)
611 else if (! tree_fits_shwi_p (offset_node
))
614 offset
= tree_to_shwi (offset_node
);
616 /* If the offset is known to be out of bounds, warn, and call strlen at
618 if (offset
< 0 || offset
> max
)
620 /* Suppress multiple warnings for propagated constant strings. */
622 && !TREE_NO_WARNING (src
))
624 warning_at (loc
, 0, "offset outside bounds of constant string");
625 TREE_NO_WARNING (src
) = 1;
630 /* Use strlen to search for the first zero byte. Since any strings
631 constructed with build_string will have nulls appended, we win even
632 if we get handed something like (char[4])"abcd".
634 Since OFFSET is our starting index into the string, no further
635 calculation is needed. */
636 return ssize_int (strlen (ptr
+ offset
));
639 /* Return a char pointer for a C string if it is a string constant
640 or sum of string constant and integer constant. */
647 src
= string_constant (src
, &offset_node
);
651 if (offset_node
== 0)
652 return TREE_STRING_POINTER (src
);
653 else if (!tree_fits_uhwi_p (offset_node
)
654 || compare_tree_int (offset_node
, TREE_STRING_LENGTH (src
) - 1) > 0)
657 return TREE_STRING_POINTER (src
) + tree_to_uhwi (offset_node
);
660 /* Return a constant integer corresponding to target reading
661 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
664 c_readstr (const char *str
, machine_mode mode
)
668 HOST_WIDE_INT tmp
[MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
];
670 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
671 unsigned int len
= (GET_MODE_PRECISION (mode
) + HOST_BITS_PER_WIDE_INT
- 1)
672 / HOST_BITS_PER_WIDE_INT
;
674 gcc_assert (len
<= MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
);
675 for (i
= 0; i
< len
; i
++)
679 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
682 if (WORDS_BIG_ENDIAN
)
683 j
= GET_MODE_SIZE (mode
) - i
- 1;
684 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
685 && GET_MODE_SIZE (mode
) >= UNITS_PER_WORD
)
686 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
690 ch
= (unsigned char) str
[i
];
691 tmp
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
694 wide_int c
= wide_int::from_array (tmp
, len
, GET_MODE_PRECISION (mode
));
695 return immed_wide_int_const (c
, mode
);
698 /* Cast a target constant CST to target CHAR and if that value fits into
699 host char type, return zero and put that value into variable pointed to by
703 target_char_cast (tree cst
, char *p
)
705 unsigned HOST_WIDE_INT val
, hostval
;
707 if (TREE_CODE (cst
) != INTEGER_CST
708 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
711 /* Do not care if it fits or not right here. */
712 val
= TREE_INT_CST_LOW (cst
);
714 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
715 val
&= (((unsigned HOST_WIDE_INT
) 1) << CHAR_TYPE_SIZE
) - 1;
718 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
719 hostval
&= (((unsigned HOST_WIDE_INT
) 1) << HOST_BITS_PER_CHAR
) - 1;
728 /* Similar to save_expr, but assumes that arbitrary code is not executed
729 in between the multiple evaluations. In particular, we assume that a
730 non-addressable local variable will not be modified. */
733 builtin_save_expr (tree exp
)
735 if (TREE_CODE (exp
) == SSA_NAME
736 || (TREE_ADDRESSABLE (exp
) == 0
737 && (TREE_CODE (exp
) == PARM_DECL
738 || (TREE_CODE (exp
) == VAR_DECL
&& !TREE_STATIC (exp
)))))
741 return save_expr (exp
);
744 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
745 times to get the address of either a higher stack frame, or a return
746 address located within it (depending on FNDECL_CODE). */
749 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
)
753 #ifdef INITIAL_FRAME_ADDRESS_RTX
754 rtx tem
= INITIAL_FRAME_ADDRESS_RTX
;
758 /* For a zero count with __builtin_return_address, we don't care what
759 frame address we return, because target-specific definitions will
760 override us. Therefore frame pointer elimination is OK, and using
761 the soft frame pointer is OK.
763 For a nonzero count, or a zero count with __builtin_frame_address,
764 we require a stable offset from the current frame pointer to the
765 previous one, so we must use the hard frame pointer, and
766 we must disable frame pointer elimination. */
767 if (count
== 0 && fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
768 tem
= frame_pointer_rtx
;
771 tem
= hard_frame_pointer_rtx
;
773 /* Tell reload not to eliminate the frame pointer. */
774 crtl
->accesses_prior_frames
= 1;
778 /* Some machines need special handling before we can access
779 arbitrary frames. For example, on the SPARC, we must first flush
780 all register windows to the stack. */
781 #ifdef SETUP_FRAME_ADDRESSES
783 SETUP_FRAME_ADDRESSES ();
786 /* On the SPARC, the return address is not in the frame, it is in a
787 register. There is no way to access it off of the current frame
788 pointer, but it can be accessed off the previous frame pointer by
789 reading the value from the register window save area. */
790 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
791 if (fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
795 /* Scan back COUNT frames to the specified frame. */
796 for (i
= 0; i
< count
; i
++)
798 /* Assume the dynamic chain pointer is in the word that the
799 frame address points to, unless otherwise specified. */
800 #ifdef DYNAMIC_CHAIN_ADDRESS
801 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
803 tem
= memory_address (Pmode
, tem
);
804 tem
= gen_frame_mem (Pmode
, tem
);
805 tem
= copy_to_reg (tem
);
808 /* For __builtin_frame_address, return what we've got. But, on
809 the SPARC for example, we may have to add a bias. */
810 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
811 #ifdef FRAME_ADDR_RTX
812 return FRAME_ADDR_RTX (tem
);
817 /* For __builtin_return_address, get the return address from that frame. */
818 #ifdef RETURN_ADDR_RTX
819 tem
= RETURN_ADDR_RTX (count
, tem
);
821 tem
= memory_address (Pmode
,
822 plus_constant (Pmode
, tem
, GET_MODE_SIZE (Pmode
)));
823 tem
= gen_frame_mem (Pmode
, tem
);
828 /* Alias set used for setjmp buffer. */
829 static alias_set_type setjmp_alias_set
= -1;
831 /* Construct the leading half of a __builtin_setjmp call. Control will
832 return to RECEIVER_LABEL. This is also called directly by the SJLJ
833 exception handling code. */
836 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
838 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
842 if (setjmp_alias_set
== -1)
843 setjmp_alias_set
= new_alias_set ();
845 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
847 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
849 /* We store the frame pointer and the address of receiver_label in
850 the buffer and use the rest of it for the stack save area, which
851 is machine-dependent. */
853 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
854 set_mem_alias_set (mem
, setjmp_alias_set
);
855 emit_move_insn (mem
, targetm
.builtin_setjmp_frame_value ());
857 mem
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
858 GET_MODE_SIZE (Pmode
))),
859 set_mem_alias_set (mem
, setjmp_alias_set
);
861 emit_move_insn (validize_mem (mem
),
862 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
864 stack_save
= gen_rtx_MEM (sa_mode
,
865 plus_constant (Pmode
, buf_addr
,
866 2 * GET_MODE_SIZE (Pmode
)));
867 set_mem_alias_set (stack_save
, setjmp_alias_set
);
868 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
870 /* If there is further processing to do, do it. */
871 #ifdef HAVE_builtin_setjmp_setup
872 if (HAVE_builtin_setjmp_setup
)
873 emit_insn (gen_builtin_setjmp_setup (buf_addr
));
876 /* We have a nonlocal label. */
877 cfun
->has_nonlocal_label
= 1;
880 /* Construct the trailing part of a __builtin_setjmp call. This is
881 also called directly by the SJLJ exception handling code.
882 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
885 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED
)
889 /* Mark the FP as used when we get here, so we have to make sure it's
890 marked as used by this function. */
891 emit_use (hard_frame_pointer_rtx
);
893 /* Mark the static chain as clobbered here so life information
894 doesn't get messed up for it. */
895 chain
= targetm
.calls
.static_chain (current_function_decl
, true);
896 if (chain
&& REG_P (chain
))
897 emit_clobber (chain
);
899 /* Now put in the code to restore the frame pointer, and argument
900 pointer, if needed. */
901 #ifdef HAVE_nonlocal_goto
902 if (! HAVE_nonlocal_goto
)
905 /* First adjust our frame pointer to its actual value. It was
906 previously set to the start of the virtual area corresponding to
907 the stacked variables when we branched here and now needs to be
908 adjusted to the actual hardware fp value.
910 Assignments to virtual registers are converted by
911 instantiate_virtual_regs into the corresponding assignment
912 to the underlying register (fp in this case) that makes
913 the original assignment true.
914 So the following insn will actually be decrementing fp by
915 STARTING_FRAME_OFFSET. */
916 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
918 /* Restoring the frame pointer also modifies the hard frame pointer.
919 Mark it used (so that the previous assignment remains live once
920 the frame pointer is eliminated) and clobbered (to represent the
921 implicit update from the assignment). */
922 emit_use (hard_frame_pointer_rtx
);
923 emit_clobber (hard_frame_pointer_rtx
);
926 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
927 if (fixed_regs
[ARG_POINTER_REGNUM
])
929 #ifdef ELIMINABLE_REGS
930 /* If the argument pointer can be eliminated in favor of the
931 frame pointer, we don't need to restore it. We assume here
932 that if such an elimination is present, it can always be used.
933 This is the case on all known machines; if we don't make this
934 assumption, we do unnecessary saving on many machines. */
936 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
938 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
939 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
940 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
943 if (i
== ARRAY_SIZE (elim_regs
))
946 /* Now restore our arg pointer from the address at which it
947 was saved in our stack frame. */
948 emit_move_insn (crtl
->args
.internal_arg_pointer
,
949 copy_to_reg (get_arg_pointer_save_area ()));
954 #ifdef HAVE_builtin_setjmp_receiver
955 if (receiver_label
!= NULL
&& HAVE_builtin_setjmp_receiver
)
956 emit_insn (gen_builtin_setjmp_receiver (receiver_label
));
959 #ifdef HAVE_nonlocal_goto_receiver
960 if (HAVE_nonlocal_goto_receiver
)
961 emit_insn (gen_nonlocal_goto_receiver ());
966 /* We must not allow the code we just generated to be reordered by
967 scheduling. Specifically, the update of the frame pointer must
968 happen immediately, not later. */
969 emit_insn (gen_blockage ());
972 /* __builtin_longjmp is passed a pointer to an array of five words (not
973 all will be used on all machines). It operates similarly to the C
974 library function of the same name, but is more efficient. Much of
975 the code below is copied from the handling of non-local gotos. */
978 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
981 rtx_insn
*insn
, *last
;
982 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
984 /* DRAP is needed for stack realign if longjmp is expanded to current
986 if (SUPPORTS_STACK_ALIGNMENT
)
987 crtl
->need_drap
= true;
989 if (setjmp_alias_set
== -1)
990 setjmp_alias_set
= new_alias_set ();
992 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
994 buf_addr
= force_reg (Pmode
, buf_addr
);
996 /* We require that the user must pass a second argument of 1, because
997 that is what builtin_setjmp will return. */
998 gcc_assert (value
== const1_rtx
);
1000 last
= get_last_insn ();
1001 #ifdef HAVE_builtin_longjmp
1002 if (HAVE_builtin_longjmp
)
1003 emit_insn (gen_builtin_longjmp (buf_addr
));
1007 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
1008 lab
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
1009 GET_MODE_SIZE (Pmode
)));
1011 stack
= gen_rtx_MEM (sa_mode
, plus_constant (Pmode
, buf_addr
,
1012 2 * GET_MODE_SIZE (Pmode
)));
1013 set_mem_alias_set (fp
, setjmp_alias_set
);
1014 set_mem_alias_set (lab
, setjmp_alias_set
);
1015 set_mem_alias_set (stack
, setjmp_alias_set
);
1017 /* Pick up FP, label, and SP from the block and jump. This code is
1018 from expand_goto in stmt.c; see there for detailed comments. */
1019 #ifdef HAVE_nonlocal_goto
1020 if (HAVE_nonlocal_goto
)
1021 /* We have to pass a value to the nonlocal_goto pattern that will
1022 get copied into the static_chain pointer, but it does not matter
1023 what that value is, because builtin_setjmp does not use it. */
1024 emit_insn (gen_nonlocal_goto (value
, lab
, stack
, fp
));
1028 lab
= copy_to_reg (lab
);
1030 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1031 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1033 emit_move_insn (hard_frame_pointer_rtx
, fp
);
1034 emit_stack_restore (SAVE_NONLOCAL
, stack
);
1036 emit_use (hard_frame_pointer_rtx
);
1037 emit_use (stack_pointer_rtx
);
1038 emit_indirect_jump (lab
);
1042 /* Search backwards and mark the jump insn as a non-local goto.
1043 Note that this precludes the use of __builtin_longjmp to a
1044 __builtin_setjmp target in the same function. However, we've
1045 already cautioned the user that these functions are for
1046 internal exception handling use only. */
1047 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1049 gcc_assert (insn
!= last
);
1053 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1056 else if (CALL_P (insn
))
1062 more_const_call_expr_args_p (const const_call_expr_arg_iterator
*iter
)
1064 return (iter
->i
< iter
->n
);
1067 /* This function validates the types of a function call argument list
1068 against a specified list of tree_codes. If the last specifier is a 0,
1069 that represents an ellipses, otherwise the last specifier must be a
1073 validate_arglist (const_tree callexpr
, ...)
1075 enum tree_code code
;
1078 const_call_expr_arg_iterator iter
;
1081 va_start (ap
, callexpr
);
1082 init_const_call_expr_arg_iterator (callexpr
, &iter
);
1086 code
= (enum tree_code
) va_arg (ap
, int);
1090 /* This signifies an ellipses, any further arguments are all ok. */
1094 /* This signifies an endlink, if no arguments remain, return
1095 true, otherwise return false. */
1096 res
= !more_const_call_expr_args_p (&iter
);
1099 /* If no parameters remain or the parameter's code does not
1100 match the specified code, return false. Otherwise continue
1101 checking any remaining arguments. */
1102 arg
= next_const_call_expr_arg (&iter
);
1103 if (!validate_arg (arg
, code
))
1110 /* We need gotos here since we can only have one VA_CLOSE in a
1118 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1119 and the address of the save area. */
1122 expand_builtin_nonlocal_goto (tree exp
)
1124 tree t_label
, t_save_area
;
1125 rtx r_label
, r_save_area
, r_fp
, r_sp
;
1128 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
1131 t_label
= CALL_EXPR_ARG (exp
, 0);
1132 t_save_area
= CALL_EXPR_ARG (exp
, 1);
1134 r_label
= expand_normal (t_label
);
1135 r_label
= convert_memory_address (Pmode
, r_label
);
1136 r_save_area
= expand_normal (t_save_area
);
1137 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
1138 /* Copy the address of the save location to a register just in case it was
1139 based on the frame pointer. */
1140 r_save_area
= copy_to_reg (r_save_area
);
1141 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
1142 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
1143 plus_constant (Pmode
, r_save_area
,
1144 GET_MODE_SIZE (Pmode
)));
1146 crtl
->has_nonlocal_goto
= 1;
1148 #ifdef HAVE_nonlocal_goto
1149 /* ??? We no longer need to pass the static chain value, afaik. */
1150 if (HAVE_nonlocal_goto
)
1151 emit_insn (gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
1155 r_label
= copy_to_reg (r_label
);
1157 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1158 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1160 /* Restore frame pointer for containing function. */
1161 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
1162 emit_stack_restore (SAVE_NONLOCAL
, r_sp
);
1164 /* USE of hard_frame_pointer_rtx added for consistency;
1165 not clear if really needed. */
1166 emit_use (hard_frame_pointer_rtx
);
1167 emit_use (stack_pointer_rtx
);
1169 /* If the architecture is using a GP register, we must
1170 conservatively assume that the target function makes use of it.
1171 The prologue of functions with nonlocal gotos must therefore
1172 initialize the GP register to the appropriate value, and we
1173 must then make sure that this value is live at the point
1174 of the jump. (Note that this doesn't necessarily apply
1175 to targets with a nonlocal_goto pattern; they are free
1176 to implement it in their own way. Note also that this is
1177 a no-op if the GP register is a global invariant.) */
1178 if ((unsigned) PIC_OFFSET_TABLE_REGNUM
!= INVALID_REGNUM
1179 && fixed_regs
[PIC_OFFSET_TABLE_REGNUM
])
1180 emit_use (pic_offset_table_rtx
);
1182 emit_indirect_jump (r_label
);
1185 /* Search backwards to the jump insn and mark it as a
1187 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1191 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1194 else if (CALL_P (insn
))
1201 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1202 (not all will be used on all machines) that was passed to __builtin_setjmp.
1203 It updates the stack pointer in that block to correspond to the current
1207 expand_builtin_update_setjmp_buf (rtx buf_addr
)
1209 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
1211 = gen_rtx_MEM (sa_mode
,
1214 plus_constant (Pmode
, buf_addr
,
1215 2 * GET_MODE_SIZE (Pmode
))));
1217 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
1220 /* Expand a call to __builtin_prefetch. For a target that does not support
1221 data prefetch, evaluate the memory address argument in case it has side
1225 expand_builtin_prefetch (tree exp
)
1227 tree arg0
, arg1
, arg2
;
1231 if (!validate_arglist (exp
, POINTER_TYPE
, 0))
1234 arg0
= CALL_EXPR_ARG (exp
, 0);
1236 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1237 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1239 nargs
= call_expr_nargs (exp
);
1241 arg1
= CALL_EXPR_ARG (exp
, 1);
1243 arg1
= integer_zero_node
;
1245 arg2
= CALL_EXPR_ARG (exp
, 2);
1247 arg2
= integer_three_node
;
1249 /* Argument 0 is an address. */
1250 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
1252 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1253 if (TREE_CODE (arg1
) != INTEGER_CST
)
1255 error ("second argument to %<__builtin_prefetch%> must be a constant");
1256 arg1
= integer_zero_node
;
1258 op1
= expand_normal (arg1
);
1259 /* Argument 1 must be either zero or one. */
1260 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
1262 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1267 /* Argument 2 (locality) must be a compile-time constant int. */
1268 if (TREE_CODE (arg2
) != INTEGER_CST
)
1270 error ("third argument to %<__builtin_prefetch%> must be a constant");
1271 arg2
= integer_zero_node
;
1273 op2
= expand_normal (arg2
);
1274 /* Argument 2 must be 0, 1, 2, or 3. */
1275 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
1277 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1281 #ifdef HAVE_prefetch
1284 struct expand_operand ops
[3];
1286 create_address_operand (&ops
[0], op0
);
1287 create_integer_operand (&ops
[1], INTVAL (op1
));
1288 create_integer_operand (&ops
[2], INTVAL (op2
));
1289 if (maybe_expand_insn (CODE_FOR_prefetch
, 3, ops
))
1294 /* Don't do anything with direct references to volatile memory, but
1295 generate code to handle other side effects. */
1296 if (!MEM_P (op0
) && side_effects_p (op0
))
1300 /* Get a MEM rtx for expression EXP which is the address of an operand
1301 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1302 the maximum length of the block of memory that might be accessed or
1306 get_memory_rtx (tree exp
, tree len
)
1308 tree orig_exp
= exp
;
1311 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1312 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1313 if (TREE_CODE (exp
) == SAVE_EXPR
&& !SAVE_EXPR_RESOLVED_P (exp
))
1314 exp
= TREE_OPERAND (exp
, 0);
1316 addr
= expand_expr (orig_exp
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
1317 mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
1319 /* Get an expression we can use to find the attributes to assign to MEM.
1320 First remove any nops. */
1321 while (CONVERT_EXPR_P (exp
)
1322 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
1323 exp
= TREE_OPERAND (exp
, 0);
1325 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1326 (as builtin stringops may alias with anything). */
1327 exp
= fold_build2 (MEM_REF
,
1328 build_array_type (char_type_node
,
1329 build_range_type (sizetype
,
1330 size_one_node
, len
)),
1331 exp
, build_int_cst (ptr_type_node
, 0));
1333 /* If the MEM_REF has no acceptable address, try to get the base object
1334 from the original address we got, and build an all-aliasing
1335 unknown-sized access to that one. */
1336 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp
, 0)))
1337 set_mem_attributes (mem
, exp
, 0);
1338 else if (TREE_CODE (TREE_OPERAND (exp
, 0)) == ADDR_EXPR
1339 && (exp
= get_base_address (TREE_OPERAND (TREE_OPERAND (exp
, 0),
1342 exp
= build_fold_addr_expr (exp
);
1343 exp
= fold_build2 (MEM_REF
,
1344 build_array_type (char_type_node
,
1345 build_range_type (sizetype
,
1348 exp
, build_int_cst (ptr_type_node
, 0));
1349 set_mem_attributes (mem
, exp
, 0);
1351 set_mem_alias_set (mem
, 0);
1355 /* Built-in functions to perform an untyped call and return. */
1357 #define apply_args_mode \
1358 (this_target_builtins->x_apply_args_mode)
1359 #define apply_result_mode \
1360 (this_target_builtins->x_apply_result_mode)
1362 /* Return the size required for the block returned by __builtin_apply_args,
1363 and initialize apply_args_mode. */
1366 apply_args_size (void)
1368 static int size
= -1;
1373 /* The values computed by this function never change. */
1376 /* The first value is the incoming arg-pointer. */
1377 size
= GET_MODE_SIZE (Pmode
);
1379 /* The second value is the structure value address unless this is
1380 passed as an "invisible" first argument. */
1381 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1382 size
+= GET_MODE_SIZE (Pmode
);
1384 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1385 if (FUNCTION_ARG_REGNO_P (regno
))
1387 mode
= targetm
.calls
.get_raw_arg_mode (regno
);
1389 gcc_assert (mode
!= VOIDmode
);
1391 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1392 if (size
% align
!= 0)
1393 size
= CEIL (size
, align
) * align
;
1394 size
+= GET_MODE_SIZE (mode
);
1395 apply_args_mode
[regno
] = mode
;
1399 apply_args_mode
[regno
] = VOIDmode
;
1405 /* Return the size required for the block returned by __builtin_apply,
1406 and initialize apply_result_mode. */
1409 apply_result_size (void)
1411 static int size
= -1;
1415 /* The values computed by this function never change. */
1420 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1421 if (targetm
.calls
.function_value_regno_p (regno
))
1423 mode
= targetm
.calls
.get_raw_result_mode (regno
);
1425 gcc_assert (mode
!= VOIDmode
);
1427 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1428 if (size
% align
!= 0)
1429 size
= CEIL (size
, align
) * align
;
1430 size
+= GET_MODE_SIZE (mode
);
1431 apply_result_mode
[regno
] = mode
;
1434 apply_result_mode
[regno
] = VOIDmode
;
1436 /* Allow targets that use untyped_call and untyped_return to override
1437 the size so that machine-specific information can be stored here. */
1438 #ifdef APPLY_RESULT_SIZE
1439 size
= APPLY_RESULT_SIZE
;
1445 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1446 /* Create a vector describing the result block RESULT. If SAVEP is true,
1447 the result block is used to save the values; otherwise it is used to
1448 restore the values. */
1451 result_vector (int savep
, rtx result
)
1453 int regno
, size
, align
, nelts
;
1456 rtx
*savevec
= XALLOCAVEC (rtx
, FIRST_PSEUDO_REGISTER
);
1459 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1460 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1462 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1463 if (size
% align
!= 0)
1464 size
= CEIL (size
, align
) * align
;
1465 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
1466 mem
= adjust_address (result
, mode
, size
);
1467 savevec
[nelts
++] = (savep
1468 ? gen_rtx_SET (VOIDmode
, mem
, reg
)
1469 : gen_rtx_SET (VOIDmode
, reg
, mem
));
1470 size
+= GET_MODE_SIZE (mode
);
1472 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
1474 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1476 /* Save the state required to perform an untyped call with the same
1477 arguments as were passed to the current function. */
1480 expand_builtin_apply_args_1 (void)
1483 int size
, align
, regno
;
1485 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
1487 /* Create a block where the arg-pointer, structure value address,
1488 and argument registers can be saved. */
1489 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
1491 /* Walk past the arg-pointer and structure value address. */
1492 size
= GET_MODE_SIZE (Pmode
);
1493 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1494 size
+= GET_MODE_SIZE (Pmode
);
1496 /* Save each register used in calling a function to the block. */
1497 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1498 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1500 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1501 if (size
% align
!= 0)
1502 size
= CEIL (size
, align
) * align
;
1504 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1506 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
1507 size
+= GET_MODE_SIZE (mode
);
1510 /* Save the arg pointer to the block. */
1511 tem
= copy_to_reg (crtl
->args
.internal_arg_pointer
);
1512 #ifdef STACK_GROWS_DOWNWARD
1513 /* We need the pointer as the caller actually passed them to us, not
1514 as we might have pretended they were passed. Make sure it's a valid
1515 operand, as emit_move_insn isn't expected to handle a PLUS. */
1517 = force_operand (plus_constant (Pmode
, tem
, crtl
->args
.pretend_args_size
),
1520 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
1522 size
= GET_MODE_SIZE (Pmode
);
1524 /* Save the structure value address unless this is passed as an
1525 "invisible" first argument. */
1526 if (struct_incoming_value
)
1528 emit_move_insn (adjust_address (registers
, Pmode
, size
),
1529 copy_to_reg (struct_incoming_value
));
1530 size
+= GET_MODE_SIZE (Pmode
);
1533 /* Return the address of the block. */
1534 return copy_addr_to_reg (XEXP (registers
, 0));
1537 /* __builtin_apply_args returns block of memory allocated on
1538 the stack into which is stored the arg pointer, structure
1539 value address, static chain, and all the registers that might
1540 possibly be used in performing a function call. The code is
1541 moved to the start of the function so the incoming values are
1545 expand_builtin_apply_args (void)
1547 /* Don't do __builtin_apply_args more than once in a function.
1548 Save the result of the first call and reuse it. */
1549 if (apply_args_value
!= 0)
1550 return apply_args_value
;
1552 /* When this function is called, it means that registers must be
1553 saved on entry to this function. So we migrate the
1554 call to the first insn of this function. */
1559 temp
= expand_builtin_apply_args_1 ();
1563 apply_args_value
= temp
;
1565 /* Put the insns after the NOTE that starts the function.
1566 If this is inside a start_sequence, make the outer-level insn
1567 chain current, so the code is placed at the start of the
1568 function. If internal_arg_pointer is a non-virtual pseudo,
1569 it needs to be placed after the function that initializes
1571 push_topmost_sequence ();
1572 if (REG_P (crtl
->args
.internal_arg_pointer
)
1573 && REGNO (crtl
->args
.internal_arg_pointer
) > LAST_VIRTUAL_REGISTER
)
1574 emit_insn_before (seq
, parm_birth_insn
);
1576 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
1577 pop_topmost_sequence ();
1582 /* Perform an untyped call and save the state required to perform an
1583 untyped return of whatever value was returned by the given function. */
1586 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
1588 int size
, align
, regno
;
1590 rtx incoming_args
, result
, reg
, dest
, src
;
1591 rtx_call_insn
*call_insn
;
1592 rtx old_stack_level
= 0;
1593 rtx call_fusage
= 0;
1594 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
1596 arguments
= convert_memory_address (Pmode
, arguments
);
1598 /* Create a block where the return registers can be saved. */
1599 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
1601 /* Fetch the arg pointer from the ARGUMENTS block. */
1602 incoming_args
= gen_reg_rtx (Pmode
);
1603 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
1604 #ifndef STACK_GROWS_DOWNWARD
1605 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
1606 incoming_args
, 0, OPTAB_LIB_WIDEN
);
1609 /* Push a new argument block and copy the arguments. Do not allow
1610 the (potential) memcpy call below to interfere with our stack
1612 do_pending_stack_adjust ();
1615 /* Save the stack with nonlocal if available. */
1616 #ifdef HAVE_save_stack_nonlocal
1617 if (HAVE_save_stack_nonlocal
)
1618 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
);
1621 emit_stack_save (SAVE_BLOCK
, &old_stack_level
);
1623 /* Allocate a block of memory onto the stack and copy the memory
1624 arguments to the outgoing arguments address. We can pass TRUE
1625 as the 4th argument because we just saved the stack pointer
1626 and will restore it right after the call. */
1627 allocate_dynamic_stack_space (argsize
, 0, BIGGEST_ALIGNMENT
, true);
1629 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1630 may have already set current_function_calls_alloca to true.
1631 current_function_calls_alloca won't be set if argsize is zero,
1632 so we have to guarantee need_drap is true here. */
1633 if (SUPPORTS_STACK_ALIGNMENT
)
1634 crtl
->need_drap
= true;
1636 dest
= virtual_outgoing_args_rtx
;
1637 #ifndef STACK_GROWS_DOWNWARD
1638 if (CONST_INT_P (argsize
))
1639 dest
= plus_constant (Pmode
, dest
, -INTVAL (argsize
));
1641 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
1643 dest
= gen_rtx_MEM (BLKmode
, dest
);
1644 set_mem_align (dest
, PARM_BOUNDARY
);
1645 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
1646 set_mem_align (src
, PARM_BOUNDARY
);
1647 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
1649 /* Refer to the argument block. */
1651 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
1652 set_mem_align (arguments
, PARM_BOUNDARY
);
1654 /* Walk past the arg-pointer and structure value address. */
1655 size
= GET_MODE_SIZE (Pmode
);
1657 size
+= GET_MODE_SIZE (Pmode
);
1659 /* Restore each of the registers previously saved. Make USE insns
1660 for each of these registers for use in making the call. */
1661 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1662 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1664 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1665 if (size
% align
!= 0)
1666 size
= CEIL (size
, align
) * align
;
1667 reg
= gen_rtx_REG (mode
, regno
);
1668 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
1669 use_reg (&call_fusage
, reg
);
1670 size
+= GET_MODE_SIZE (mode
);
1673 /* Restore the structure value address unless this is passed as an
1674 "invisible" first argument. */
1675 size
= GET_MODE_SIZE (Pmode
);
1678 rtx value
= gen_reg_rtx (Pmode
);
1679 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
1680 emit_move_insn (struct_value
, value
);
1681 if (REG_P (struct_value
))
1682 use_reg (&call_fusage
, struct_value
);
1683 size
+= GET_MODE_SIZE (Pmode
);
1686 /* All arguments and registers used for the call are set up by now! */
1687 function
= prepare_call_address (NULL
, function
, NULL
, &call_fusage
, 0, 0);
1689 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1690 and we don't want to load it into a register as an optimization,
1691 because prepare_call_address already did it if it should be done. */
1692 if (GET_CODE (function
) != SYMBOL_REF
)
1693 function
= memory_address (FUNCTION_MODE
, function
);
1695 /* Generate the actual call instruction and save the return value. */
1696 #ifdef HAVE_untyped_call
1697 if (HAVE_untyped_call
)
1698 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE
, function
),
1699 result
, result_vector (1, result
)));
1702 #ifdef HAVE_call_value
1703 if (HAVE_call_value
)
1707 /* Locate the unique return register. It is not possible to
1708 express a call that sets more than one return register using
1709 call_value; use untyped_call for that. In fact, untyped_call
1710 only needs to save the return registers in the given block. */
1711 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1712 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1714 gcc_assert (!valreg
); /* HAVE_untyped_call required. */
1716 valreg
= gen_rtx_REG (mode
, regno
);
1719 emit_call_insn (GEN_CALL_VALUE (valreg
,
1720 gen_rtx_MEM (FUNCTION_MODE
, function
),
1721 const0_rtx
, NULL_RTX
, const0_rtx
));
1723 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
1729 /* Find the CALL insn we just emitted, and attach the register usage
1731 call_insn
= last_call_insn ();
1732 add_function_usage_to (call_insn
, call_fusage
);
1734 /* Restore the stack. */
1735 #ifdef HAVE_save_stack_nonlocal
1736 if (HAVE_save_stack_nonlocal
)
1737 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
);
1740 emit_stack_restore (SAVE_BLOCK
, old_stack_level
);
1741 fixup_args_size_notes (call_insn
, get_last_insn (), 0);
1745 /* Return the address of the result block. */
1746 result
= copy_addr_to_reg (XEXP (result
, 0));
1747 return convert_memory_address (ptr_mode
, result
);
1750 /* Perform an untyped return. */
1753 expand_builtin_return (rtx result
)
1755 int size
, align
, regno
;
1758 rtx_insn
*call_fusage
= 0;
1760 result
= convert_memory_address (Pmode
, result
);
1762 apply_result_size ();
1763 result
= gen_rtx_MEM (BLKmode
, result
);
1765 #ifdef HAVE_untyped_return
1766 if (HAVE_untyped_return
)
1768 emit_jump_insn (gen_untyped_return (result
, result_vector (0, result
)));
1774 /* Restore the return value and note that each value is used. */
1776 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1777 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1779 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1780 if (size
% align
!= 0)
1781 size
= CEIL (size
, align
) * align
;
1782 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1783 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
1785 push_to_sequence (call_fusage
);
1787 call_fusage
= get_insns ();
1789 size
+= GET_MODE_SIZE (mode
);
1792 /* Put the USE insns before the return. */
1793 emit_insn (call_fusage
);
1795 /* Return whatever values was restored by jumping directly to the end
1797 expand_naked_return ();
1800 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1802 static enum type_class
1803 type_to_class (tree type
)
1805 switch (TREE_CODE (type
))
1807 case VOID_TYPE
: return void_type_class
;
1808 case INTEGER_TYPE
: return integer_type_class
;
1809 case ENUMERAL_TYPE
: return enumeral_type_class
;
1810 case BOOLEAN_TYPE
: return boolean_type_class
;
1811 case POINTER_TYPE
: return pointer_type_class
;
1812 case REFERENCE_TYPE
: return reference_type_class
;
1813 case OFFSET_TYPE
: return offset_type_class
;
1814 case REAL_TYPE
: return real_type_class
;
1815 case COMPLEX_TYPE
: return complex_type_class
;
1816 case FUNCTION_TYPE
: return function_type_class
;
1817 case METHOD_TYPE
: return method_type_class
;
1818 case RECORD_TYPE
: return record_type_class
;
1820 case QUAL_UNION_TYPE
: return union_type_class
;
1821 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
1822 ? string_type_class
: array_type_class
);
1823 case LANG_TYPE
: return lang_type_class
;
1824 default: return no_type_class
;
1828 /* Expand a call EXP to __builtin_classify_type. */
1831 expand_builtin_classify_type (tree exp
)
1833 if (call_expr_nargs (exp
))
1834 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))));
1835 return GEN_INT (no_type_class
);
1838 /* This helper macro, meant to be used in mathfn_built_in below,
1839 determines which among a set of three builtin math functions is
1840 appropriate for a given type mode. The `F' and `L' cases are
1841 automatically generated from the `double' case. */
1842 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1843 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1844 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1845 fcodel = BUILT_IN_MATHFN##L ; break;
1846 /* Similar to above, but appends _R after any F/L suffix. */
1847 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1848 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1849 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1850 fcodel = BUILT_IN_MATHFN##L_R ; break;
1852 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1853 if available. If IMPLICIT is true use the implicit builtin declaration,
1854 otherwise use the explicit declaration. If we can't do the conversion,
1858 mathfn_built_in_1 (tree type
, enum built_in_function fn
, bool implicit_p
)
1860 enum built_in_function fcode
, fcodef
, fcodel
, fcode2
;
1864 CASE_MATHFN (BUILT_IN_ACOS
)
1865 CASE_MATHFN (BUILT_IN_ACOSH
)
1866 CASE_MATHFN (BUILT_IN_ASIN
)
1867 CASE_MATHFN (BUILT_IN_ASINH
)
1868 CASE_MATHFN (BUILT_IN_ATAN
)
1869 CASE_MATHFN (BUILT_IN_ATAN2
)
1870 CASE_MATHFN (BUILT_IN_ATANH
)
1871 CASE_MATHFN (BUILT_IN_CBRT
)
1872 CASE_MATHFN (BUILT_IN_CEIL
)
1873 CASE_MATHFN (BUILT_IN_CEXPI
)
1874 CASE_MATHFN (BUILT_IN_COPYSIGN
)
1875 CASE_MATHFN (BUILT_IN_COS
)
1876 CASE_MATHFN (BUILT_IN_COSH
)
1877 CASE_MATHFN (BUILT_IN_DREM
)
1878 CASE_MATHFN (BUILT_IN_ERF
)
1879 CASE_MATHFN (BUILT_IN_ERFC
)
1880 CASE_MATHFN (BUILT_IN_EXP
)
1881 CASE_MATHFN (BUILT_IN_EXP10
)
1882 CASE_MATHFN (BUILT_IN_EXP2
)
1883 CASE_MATHFN (BUILT_IN_EXPM1
)
1884 CASE_MATHFN (BUILT_IN_FABS
)
1885 CASE_MATHFN (BUILT_IN_FDIM
)
1886 CASE_MATHFN (BUILT_IN_FLOOR
)
1887 CASE_MATHFN (BUILT_IN_FMA
)
1888 CASE_MATHFN (BUILT_IN_FMAX
)
1889 CASE_MATHFN (BUILT_IN_FMIN
)
1890 CASE_MATHFN (BUILT_IN_FMOD
)
1891 CASE_MATHFN (BUILT_IN_FREXP
)
1892 CASE_MATHFN (BUILT_IN_GAMMA
)
1893 CASE_MATHFN_REENT (BUILT_IN_GAMMA
) /* GAMMA_R */
1894 CASE_MATHFN (BUILT_IN_HUGE_VAL
)
1895 CASE_MATHFN (BUILT_IN_HYPOT
)
1896 CASE_MATHFN (BUILT_IN_ILOGB
)
1897 CASE_MATHFN (BUILT_IN_ICEIL
)
1898 CASE_MATHFN (BUILT_IN_IFLOOR
)
1899 CASE_MATHFN (BUILT_IN_INF
)
1900 CASE_MATHFN (BUILT_IN_IRINT
)
1901 CASE_MATHFN (BUILT_IN_IROUND
)
1902 CASE_MATHFN (BUILT_IN_ISINF
)
1903 CASE_MATHFN (BUILT_IN_J0
)
1904 CASE_MATHFN (BUILT_IN_J1
)
1905 CASE_MATHFN (BUILT_IN_JN
)
1906 CASE_MATHFN (BUILT_IN_LCEIL
)
1907 CASE_MATHFN (BUILT_IN_LDEXP
)
1908 CASE_MATHFN (BUILT_IN_LFLOOR
)
1909 CASE_MATHFN (BUILT_IN_LGAMMA
)
1910 CASE_MATHFN_REENT (BUILT_IN_LGAMMA
) /* LGAMMA_R */
1911 CASE_MATHFN (BUILT_IN_LLCEIL
)
1912 CASE_MATHFN (BUILT_IN_LLFLOOR
)
1913 CASE_MATHFN (BUILT_IN_LLRINT
)
1914 CASE_MATHFN (BUILT_IN_LLROUND
)
1915 CASE_MATHFN (BUILT_IN_LOG
)
1916 CASE_MATHFN (BUILT_IN_LOG10
)
1917 CASE_MATHFN (BUILT_IN_LOG1P
)
1918 CASE_MATHFN (BUILT_IN_LOG2
)
1919 CASE_MATHFN (BUILT_IN_LOGB
)
1920 CASE_MATHFN (BUILT_IN_LRINT
)
1921 CASE_MATHFN (BUILT_IN_LROUND
)
1922 CASE_MATHFN (BUILT_IN_MODF
)
1923 CASE_MATHFN (BUILT_IN_NAN
)
1924 CASE_MATHFN (BUILT_IN_NANS
)
1925 CASE_MATHFN (BUILT_IN_NEARBYINT
)
1926 CASE_MATHFN (BUILT_IN_NEXTAFTER
)
1927 CASE_MATHFN (BUILT_IN_NEXTTOWARD
)
1928 CASE_MATHFN (BUILT_IN_POW
)
1929 CASE_MATHFN (BUILT_IN_POWI
)
1930 CASE_MATHFN (BUILT_IN_POW10
)
1931 CASE_MATHFN (BUILT_IN_REMAINDER
)
1932 CASE_MATHFN (BUILT_IN_REMQUO
)
1933 CASE_MATHFN (BUILT_IN_RINT
)
1934 CASE_MATHFN (BUILT_IN_ROUND
)
1935 CASE_MATHFN (BUILT_IN_SCALB
)
1936 CASE_MATHFN (BUILT_IN_SCALBLN
)
1937 CASE_MATHFN (BUILT_IN_SCALBN
)
1938 CASE_MATHFN (BUILT_IN_SIGNBIT
)
1939 CASE_MATHFN (BUILT_IN_SIGNIFICAND
)
1940 CASE_MATHFN (BUILT_IN_SIN
)
1941 CASE_MATHFN (BUILT_IN_SINCOS
)
1942 CASE_MATHFN (BUILT_IN_SINH
)
1943 CASE_MATHFN (BUILT_IN_SQRT
)
1944 CASE_MATHFN (BUILT_IN_TAN
)
1945 CASE_MATHFN (BUILT_IN_TANH
)
1946 CASE_MATHFN (BUILT_IN_TGAMMA
)
1947 CASE_MATHFN (BUILT_IN_TRUNC
)
1948 CASE_MATHFN (BUILT_IN_Y0
)
1949 CASE_MATHFN (BUILT_IN_Y1
)
1950 CASE_MATHFN (BUILT_IN_YN
)
1956 if (TYPE_MAIN_VARIANT (type
) == double_type_node
)
1958 else if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1960 else if (TYPE_MAIN_VARIANT (type
) == long_double_type_node
)
1965 if (implicit_p
&& !builtin_decl_implicit_p (fcode2
))
1968 return builtin_decl_explicit (fcode2
);
1971 /* Like mathfn_built_in_1(), but always use the implicit array. */
1974 mathfn_built_in (tree type
, enum built_in_function fn
)
1976 return mathfn_built_in_1 (type
, fn
, /*implicit=*/ 1);
1979 /* If errno must be maintained, expand the RTL to check if the result,
1980 TARGET, of a built-in function call, EXP, is NaN, and if so set
1984 expand_errno_check (tree exp
, rtx target
)
1986 rtx_code_label
*lab
= gen_label_rtx ();
1988 /* Test the result; if it is NaN, set errno=EDOM because
1989 the argument was not in the domain. */
1990 do_compare_rtx_and_jump (target
, target
, EQ
, 0, GET_MODE (target
),
1991 NULL_RTX
, NULL_RTX
, lab
,
1992 /* The jump is very likely. */
1993 REG_BR_PROB_BASE
- (REG_BR_PROB_BASE
/ 2000 - 1));
1996 /* If this built-in doesn't throw an exception, set errno directly. */
1997 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp
), 0)))
1999 #ifdef GEN_ERRNO_RTX
2000 rtx errno_rtx
= GEN_ERRNO_RTX
;
2003 = gen_rtx_MEM (word_mode
, gen_rtx_SYMBOL_REF (Pmode
, "errno"));
2005 emit_move_insn (errno_rtx
,
2006 gen_int_mode (TARGET_EDOM
, GET_MODE (errno_rtx
)));
2012 /* Make sure the library call isn't expanded as a tail call. */
2013 CALL_EXPR_TAILCALL (exp
) = 0;
2015 /* We can't set errno=EDOM directly; let the library call do it.
2016 Pop the arguments right away in case the call gets deleted. */
2018 expand_call (exp
, target
, 0);
2023 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2024 Return NULL_RTX if a normal call should be emitted rather than expanding
2025 the function in-line. EXP is the expression that is a call to the builtin
2026 function; if convenient, the result should be placed in TARGET.
2027 SUBTARGET may be used as the target for computing one of EXP's operands. */
2030 expand_builtin_mathfn (tree exp
, rtx target
, rtx subtarget
)
2032 optab builtin_optab
;
2035 tree fndecl
= get_callee_fndecl (exp
);
2037 bool errno_set
= false;
2038 bool try_widening
= false;
2041 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2044 arg
= CALL_EXPR_ARG (exp
, 0);
2046 switch (DECL_FUNCTION_CODE (fndecl
))
2048 CASE_FLT_FN (BUILT_IN_SQRT
):
2049 errno_set
= ! tree_expr_nonnegative_p (arg
);
2050 try_widening
= true;
2051 builtin_optab
= sqrt_optab
;
2053 CASE_FLT_FN (BUILT_IN_EXP
):
2054 errno_set
= true; builtin_optab
= exp_optab
; break;
2055 CASE_FLT_FN (BUILT_IN_EXP10
):
2056 CASE_FLT_FN (BUILT_IN_POW10
):
2057 errno_set
= true; builtin_optab
= exp10_optab
; break;
2058 CASE_FLT_FN (BUILT_IN_EXP2
):
2059 errno_set
= true; builtin_optab
= exp2_optab
; break;
2060 CASE_FLT_FN (BUILT_IN_EXPM1
):
2061 errno_set
= true; builtin_optab
= expm1_optab
; break;
2062 CASE_FLT_FN (BUILT_IN_LOGB
):
2063 errno_set
= true; builtin_optab
= logb_optab
; break;
2064 CASE_FLT_FN (BUILT_IN_LOG
):
2065 errno_set
= true; builtin_optab
= log_optab
; break;
2066 CASE_FLT_FN (BUILT_IN_LOG10
):
2067 errno_set
= true; builtin_optab
= log10_optab
; break;
2068 CASE_FLT_FN (BUILT_IN_LOG2
):
2069 errno_set
= true; builtin_optab
= log2_optab
; break;
2070 CASE_FLT_FN (BUILT_IN_LOG1P
):
2071 errno_set
= true; builtin_optab
= log1p_optab
; break;
2072 CASE_FLT_FN (BUILT_IN_ASIN
):
2073 builtin_optab
= asin_optab
; break;
2074 CASE_FLT_FN (BUILT_IN_ACOS
):
2075 builtin_optab
= acos_optab
; break;
2076 CASE_FLT_FN (BUILT_IN_TAN
):
2077 builtin_optab
= tan_optab
; break;
2078 CASE_FLT_FN (BUILT_IN_ATAN
):
2079 builtin_optab
= atan_optab
; break;
2080 CASE_FLT_FN (BUILT_IN_FLOOR
):
2081 builtin_optab
= floor_optab
; break;
2082 CASE_FLT_FN (BUILT_IN_CEIL
):
2083 builtin_optab
= ceil_optab
; break;
2084 CASE_FLT_FN (BUILT_IN_TRUNC
):
2085 builtin_optab
= btrunc_optab
; break;
2086 CASE_FLT_FN (BUILT_IN_ROUND
):
2087 builtin_optab
= round_optab
; break;
2088 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
2089 builtin_optab
= nearbyint_optab
;
2090 if (flag_trapping_math
)
2092 /* Else fallthrough and expand as rint. */
2093 CASE_FLT_FN (BUILT_IN_RINT
):
2094 builtin_optab
= rint_optab
; break;
2095 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
2096 builtin_optab
= significand_optab
; break;
2101 /* Make a suitable register to place result in. */
2102 mode
= TYPE_MODE (TREE_TYPE (exp
));
2104 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2107 /* Before working hard, check whether the instruction is available, but try
2108 to widen the mode for specific operations. */
2109 if ((optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
2110 || (try_widening
&& !excess_precision_type (TREE_TYPE (exp
))))
2111 && (!errno_set
|| !optimize_insn_for_size_p ()))
2113 rtx result
= gen_reg_rtx (mode
);
2115 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2116 need to expand the argument again. This way, we will not perform
2117 side-effects more the once. */
2118 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2120 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2124 /* Compute into RESULT.
2125 Set RESULT to wherever the result comes back. */
2126 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2131 expand_errno_check (exp
, result
);
2133 /* Output the entire sequence. */
2134 insns
= get_insns ();
2140 /* If we were unable to expand via the builtin, stop the sequence
2141 (without outputting the insns) and call to the library function
2142 with the stabilized argument list. */
2146 return expand_call (exp
, target
, target
== const0_rtx
);
2149 /* Expand a call to the builtin binary math functions (pow and atan2).
2150 Return NULL_RTX if a normal call should be emitted rather than expanding the
2151 function in-line. EXP is the expression that is a call to the builtin
2152 function; if convenient, the result should be placed in TARGET.
2153 SUBTARGET may be used as the target for computing one of EXP's
2157 expand_builtin_mathfn_2 (tree exp
, rtx target
, rtx subtarget
)
2159 optab builtin_optab
;
2160 rtx op0
, op1
, result
;
2162 int op1_type
= REAL_TYPE
;
2163 tree fndecl
= get_callee_fndecl (exp
);
2166 bool errno_set
= true;
2168 switch (DECL_FUNCTION_CODE (fndecl
))
2170 CASE_FLT_FN (BUILT_IN_SCALBN
):
2171 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2172 CASE_FLT_FN (BUILT_IN_LDEXP
):
2173 op1_type
= INTEGER_TYPE
;
2178 if (!validate_arglist (exp
, REAL_TYPE
, op1_type
, VOID_TYPE
))
2181 arg0
= CALL_EXPR_ARG (exp
, 0);
2182 arg1
= CALL_EXPR_ARG (exp
, 1);
2184 switch (DECL_FUNCTION_CODE (fndecl
))
2186 CASE_FLT_FN (BUILT_IN_POW
):
2187 builtin_optab
= pow_optab
; break;
2188 CASE_FLT_FN (BUILT_IN_ATAN2
):
2189 builtin_optab
= atan2_optab
; break;
2190 CASE_FLT_FN (BUILT_IN_SCALB
):
2191 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2193 builtin_optab
= scalb_optab
; break;
2194 CASE_FLT_FN (BUILT_IN_SCALBN
):
2195 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2196 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2198 /* Fall through... */
2199 CASE_FLT_FN (BUILT_IN_LDEXP
):
2200 builtin_optab
= ldexp_optab
; break;
2201 CASE_FLT_FN (BUILT_IN_FMOD
):
2202 builtin_optab
= fmod_optab
; break;
2203 CASE_FLT_FN (BUILT_IN_REMAINDER
):
2204 CASE_FLT_FN (BUILT_IN_DREM
):
2205 builtin_optab
= remainder_optab
; break;
2210 /* Make a suitable register to place result in. */
2211 mode
= TYPE_MODE (TREE_TYPE (exp
));
2213 /* Before working hard, check whether the instruction is available. */
2214 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2217 result
= gen_reg_rtx (mode
);
2219 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2222 if (errno_set
&& optimize_insn_for_size_p ())
2225 /* Always stabilize the argument list. */
2226 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2227 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2229 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2230 op1
= expand_normal (arg1
);
2234 /* Compute into RESULT.
2235 Set RESULT to wherever the result comes back. */
2236 result
= expand_binop (mode
, builtin_optab
, op0
, op1
,
2237 result
, 0, OPTAB_DIRECT
);
2239 /* If we were unable to expand via the builtin, stop the sequence
2240 (without outputting the insns) and call to the library function
2241 with the stabilized argument list. */
2245 return expand_call (exp
, target
, target
== const0_rtx
);
2249 expand_errno_check (exp
, result
);
2251 /* Output the entire sequence. */
2252 insns
= get_insns ();
2259 /* Expand a call to the builtin trinary math functions (fma).
2260 Return NULL_RTX if a normal call should be emitted rather than expanding the
2261 function in-line. EXP is the expression that is a call to the builtin
2262 function; if convenient, the result should be placed in TARGET.
2263 SUBTARGET may be used as the target for computing one of EXP's
2267 expand_builtin_mathfn_ternary (tree exp
, rtx target
, rtx subtarget
)
2269 optab builtin_optab
;
2270 rtx op0
, op1
, op2
, result
;
2272 tree fndecl
= get_callee_fndecl (exp
);
2273 tree arg0
, arg1
, arg2
;
2276 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
2279 arg0
= CALL_EXPR_ARG (exp
, 0);
2280 arg1
= CALL_EXPR_ARG (exp
, 1);
2281 arg2
= CALL_EXPR_ARG (exp
, 2);
2283 switch (DECL_FUNCTION_CODE (fndecl
))
2285 CASE_FLT_FN (BUILT_IN_FMA
):
2286 builtin_optab
= fma_optab
; break;
2291 /* Make a suitable register to place result in. */
2292 mode
= TYPE_MODE (TREE_TYPE (exp
));
2294 /* Before working hard, check whether the instruction is available. */
2295 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2298 result
= gen_reg_rtx (mode
);
2300 /* Always stabilize the argument list. */
2301 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2302 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2303 CALL_EXPR_ARG (exp
, 2) = arg2
= builtin_save_expr (arg2
);
2305 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2306 op1
= expand_normal (arg1
);
2307 op2
= expand_normal (arg2
);
2311 /* Compute into RESULT.
2312 Set RESULT to wherever the result comes back. */
2313 result
= expand_ternary_op (mode
, builtin_optab
, op0
, op1
, op2
,
2316 /* If we were unable to expand via the builtin, stop the sequence
2317 (without outputting the insns) and call to the library function
2318 with the stabilized argument list. */
2322 return expand_call (exp
, target
, target
== const0_rtx
);
2325 /* Output the entire sequence. */
2326 insns
= get_insns ();
2333 /* Expand a call to the builtin sin and cos math functions.
2334 Return NULL_RTX if a normal call should be emitted rather than expanding the
2335 function in-line. EXP is the expression that is a call to the builtin
2336 function; if convenient, the result should be placed in TARGET.
2337 SUBTARGET may be used as the target for computing one of EXP's
2341 expand_builtin_mathfn_3 (tree exp
, rtx target
, rtx subtarget
)
2343 optab builtin_optab
;
2346 tree fndecl
= get_callee_fndecl (exp
);
2350 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2353 arg
= CALL_EXPR_ARG (exp
, 0);
2355 switch (DECL_FUNCTION_CODE (fndecl
))
2357 CASE_FLT_FN (BUILT_IN_SIN
):
2358 CASE_FLT_FN (BUILT_IN_COS
):
2359 builtin_optab
= sincos_optab
; break;
2364 /* Make a suitable register to place result in. */
2365 mode
= TYPE_MODE (TREE_TYPE (exp
));
2367 /* Check if sincos insn is available, otherwise fallback
2368 to sin or cos insn. */
2369 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2370 switch (DECL_FUNCTION_CODE (fndecl
))
2372 CASE_FLT_FN (BUILT_IN_SIN
):
2373 builtin_optab
= sin_optab
; break;
2374 CASE_FLT_FN (BUILT_IN_COS
):
2375 builtin_optab
= cos_optab
; break;
2380 /* Before working hard, check whether the instruction is available. */
2381 if (optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
)
2383 rtx result
= gen_reg_rtx (mode
);
2385 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2386 need to expand the argument again. This way, we will not perform
2387 side-effects more the once. */
2388 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2390 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2394 /* Compute into RESULT.
2395 Set RESULT to wherever the result comes back. */
2396 if (builtin_optab
== sincos_optab
)
2400 switch (DECL_FUNCTION_CODE (fndecl
))
2402 CASE_FLT_FN (BUILT_IN_SIN
):
2403 ok
= expand_twoval_unop (builtin_optab
, op0
, 0, result
, 0);
2405 CASE_FLT_FN (BUILT_IN_COS
):
2406 ok
= expand_twoval_unop (builtin_optab
, op0
, result
, 0, 0);
2414 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2418 /* Output the entire sequence. */
2419 insns
= get_insns ();
2425 /* If we were unable to expand via the builtin, stop the sequence
2426 (without outputting the insns) and call to the library function
2427 with the stabilized argument list. */
2431 return expand_call (exp
, target
, target
== const0_rtx
);
2434 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2435 return an RTL instruction code that implements the functionality.
2436 If that isn't possible or available return CODE_FOR_nothing. */
2438 static enum insn_code
2439 interclass_mathfn_icode (tree arg
, tree fndecl
)
2441 bool errno_set
= false;
2442 optab builtin_optab
= unknown_optab
;
2445 switch (DECL_FUNCTION_CODE (fndecl
))
2447 CASE_FLT_FN (BUILT_IN_ILOGB
):
2448 errno_set
= true; builtin_optab
= ilogb_optab
; break;
2449 CASE_FLT_FN (BUILT_IN_ISINF
):
2450 builtin_optab
= isinf_optab
; break;
2451 case BUILT_IN_ISNORMAL
:
2452 case BUILT_IN_ISFINITE
:
2453 CASE_FLT_FN (BUILT_IN_FINITE
):
2454 case BUILT_IN_FINITED32
:
2455 case BUILT_IN_FINITED64
:
2456 case BUILT_IN_FINITED128
:
2457 case BUILT_IN_ISINFD32
:
2458 case BUILT_IN_ISINFD64
:
2459 case BUILT_IN_ISINFD128
:
2460 /* These builtins have no optabs (yet). */
2466 /* There's no easy way to detect the case we need to set EDOM. */
2467 if (flag_errno_math
&& errno_set
)
2468 return CODE_FOR_nothing
;
2470 /* Optab mode depends on the mode of the input argument. */
2471 mode
= TYPE_MODE (TREE_TYPE (arg
));
2474 return optab_handler (builtin_optab
, mode
);
2475 return CODE_FOR_nothing
;
2478 /* Expand a call to one of the builtin math functions that operate on
2479 floating point argument and output an integer result (ilogb, isinf,
2481 Return 0 if a normal call should be emitted rather than expanding the
2482 function in-line. EXP is the expression that is a call to the builtin
2483 function; if convenient, the result should be placed in TARGET. */
2486 expand_builtin_interclass_mathfn (tree exp
, rtx target
)
2488 enum insn_code icode
= CODE_FOR_nothing
;
2490 tree fndecl
= get_callee_fndecl (exp
);
2494 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2497 arg
= CALL_EXPR_ARG (exp
, 0);
2498 icode
= interclass_mathfn_icode (arg
, fndecl
);
2499 mode
= TYPE_MODE (TREE_TYPE (arg
));
2501 if (icode
!= CODE_FOR_nothing
)
2503 struct expand_operand ops
[1];
2504 rtx_insn
*last
= get_last_insn ();
2505 tree orig_arg
= arg
;
2507 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2508 need to expand the argument again. This way, we will not perform
2509 side-effects more the once. */
2510 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2512 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2514 if (mode
!= GET_MODE (op0
))
2515 op0
= convert_to_mode (mode
, op0
, 0);
2517 create_output_operand (&ops
[0], target
, TYPE_MODE (TREE_TYPE (exp
)));
2518 if (maybe_legitimize_operands (icode
, 0, 1, ops
)
2519 && maybe_emit_unop_insn (icode
, ops
[0].value
, op0
, UNKNOWN
))
2520 return ops
[0].value
;
2522 delete_insns_since (last
);
2523 CALL_EXPR_ARG (exp
, 0) = orig_arg
;
2529 /* Expand a call to the builtin sincos math function.
2530 Return NULL_RTX if a normal call should be emitted rather than expanding the
2531 function in-line. EXP is the expression that is a call to the builtin
2535 expand_builtin_sincos (tree exp
)
2537 rtx op0
, op1
, op2
, target1
, target2
;
2539 tree arg
, sinp
, cosp
;
2541 location_t loc
= EXPR_LOCATION (exp
);
2542 tree alias_type
, alias_off
;
2544 if (!validate_arglist (exp
, REAL_TYPE
,
2545 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2548 arg
= CALL_EXPR_ARG (exp
, 0);
2549 sinp
= CALL_EXPR_ARG (exp
, 1);
2550 cosp
= CALL_EXPR_ARG (exp
, 2);
2552 /* Make a suitable register to place result in. */
2553 mode
= TYPE_MODE (TREE_TYPE (arg
));
2555 /* Check if sincos insn is available, otherwise emit the call. */
2556 if (optab_handler (sincos_optab
, mode
) == CODE_FOR_nothing
)
2559 target1
= gen_reg_rtx (mode
);
2560 target2
= gen_reg_rtx (mode
);
2562 op0
= expand_normal (arg
);
2563 alias_type
= build_pointer_type_for_mode (TREE_TYPE (arg
), ptr_mode
, true);
2564 alias_off
= build_int_cst (alias_type
, 0);
2565 op1
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2567 op2
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2570 /* Compute into target1 and target2.
2571 Set TARGET to wherever the result comes back. */
2572 result
= expand_twoval_unop (sincos_optab
, op0
, target2
, target1
, 0);
2573 gcc_assert (result
);
2575 /* Move target1 and target2 to the memory locations indicated
2577 emit_move_insn (op1
, target1
);
2578 emit_move_insn (op2
, target2
);
2583 /* Expand a call to the internal cexpi builtin to the sincos math function.
2584 EXP is the expression that is a call to the builtin function; if convenient,
2585 the result should be placed in TARGET. */
2588 expand_builtin_cexpi (tree exp
, rtx target
)
2590 tree fndecl
= get_callee_fndecl (exp
);
2594 location_t loc
= EXPR_LOCATION (exp
);
2596 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2599 arg
= CALL_EXPR_ARG (exp
, 0);
2600 type
= TREE_TYPE (arg
);
2601 mode
= TYPE_MODE (TREE_TYPE (arg
));
2603 /* Try expanding via a sincos optab, fall back to emitting a libcall
2604 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2605 is only generated from sincos, cexp or if we have either of them. */
2606 if (optab_handler (sincos_optab
, mode
) != CODE_FOR_nothing
)
2608 op1
= gen_reg_rtx (mode
);
2609 op2
= gen_reg_rtx (mode
);
2611 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2613 /* Compute into op1 and op2. */
2614 expand_twoval_unop (sincos_optab
, op0
, op2
, op1
, 0);
2616 else if (targetm
.libc_has_function (function_sincos
))
2618 tree call
, fn
= NULL_TREE
;
2622 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2623 fn
= builtin_decl_explicit (BUILT_IN_SINCOSF
);
2624 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2625 fn
= builtin_decl_explicit (BUILT_IN_SINCOS
);
2626 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2627 fn
= builtin_decl_explicit (BUILT_IN_SINCOSL
);
2631 op1
= assign_temp (TREE_TYPE (arg
), 1, 1);
2632 op2
= assign_temp (TREE_TYPE (arg
), 1, 1);
2633 op1a
= copy_addr_to_reg (XEXP (op1
, 0));
2634 op2a
= copy_addr_to_reg (XEXP (op2
, 0));
2635 top1
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op1a
);
2636 top2
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op2a
);
2638 /* Make sure not to fold the sincos call again. */
2639 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2640 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn
)),
2641 call
, 3, arg
, top1
, top2
));
2645 tree call
, fn
= NULL_TREE
, narg
;
2646 tree ctype
= build_complex_type (type
);
2648 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2649 fn
= builtin_decl_explicit (BUILT_IN_CEXPF
);
2650 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2651 fn
= builtin_decl_explicit (BUILT_IN_CEXP
);
2652 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2653 fn
= builtin_decl_explicit (BUILT_IN_CEXPL
);
2657 /* If we don't have a decl for cexp create one. This is the
2658 friendliest fallback if the user calls __builtin_cexpi
2659 without full target C99 function support. */
2660 if (fn
== NULL_TREE
)
2663 const char *name
= NULL
;
2665 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2667 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2669 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2672 fntype
= build_function_type_list (ctype
, ctype
, NULL_TREE
);
2673 fn
= build_fn_decl (name
, fntype
);
2676 narg
= fold_build2_loc (loc
, COMPLEX_EXPR
, ctype
,
2677 build_real (type
, dconst0
), arg
);
2679 /* Make sure not to fold the cexp call again. */
2680 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2681 return expand_expr (build_call_nary (ctype
, call
, 1, narg
),
2682 target
, VOIDmode
, EXPAND_NORMAL
);
2685 /* Now build the proper return type. */
2686 return expand_expr (build2 (COMPLEX_EXPR
, build_complex_type (type
),
2687 make_tree (TREE_TYPE (arg
), op2
),
2688 make_tree (TREE_TYPE (arg
), op1
)),
2689 target
, VOIDmode
, EXPAND_NORMAL
);
2692 /* Conveniently construct a function call expression. FNDECL names the
2693 function to be called, N is the number of arguments, and the "..."
2694 parameters are the argument expressions. Unlike build_call_exr
2695 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2698 build_call_nofold_loc (location_t loc
, tree fndecl
, int n
, ...)
2701 tree fntype
= TREE_TYPE (fndecl
);
2702 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), fndecl
);
2705 fn
= build_call_valist (TREE_TYPE (fntype
), fn
, n
, ap
);
2707 SET_EXPR_LOCATION (fn
, loc
);
2711 /* Expand a call to one of the builtin rounding functions gcc defines
2712 as an extension (lfloor and lceil). As these are gcc extensions we
2713 do not need to worry about setting errno to EDOM.
2714 If expanding via optab fails, lower expression to (int)(floor(x)).
2715 EXP is the expression that is a call to the builtin function;
2716 if convenient, the result should be placed in TARGET. */
2719 expand_builtin_int_roundingfn (tree exp
, rtx target
)
2721 convert_optab builtin_optab
;
2724 tree fndecl
= get_callee_fndecl (exp
);
2725 enum built_in_function fallback_fn
;
2726 tree fallback_fndecl
;
2730 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2733 arg
= CALL_EXPR_ARG (exp
, 0);
2735 switch (DECL_FUNCTION_CODE (fndecl
))
2737 CASE_FLT_FN (BUILT_IN_ICEIL
):
2738 CASE_FLT_FN (BUILT_IN_LCEIL
):
2739 CASE_FLT_FN (BUILT_IN_LLCEIL
):
2740 builtin_optab
= lceil_optab
;
2741 fallback_fn
= BUILT_IN_CEIL
;
2744 CASE_FLT_FN (BUILT_IN_IFLOOR
):
2745 CASE_FLT_FN (BUILT_IN_LFLOOR
):
2746 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
2747 builtin_optab
= lfloor_optab
;
2748 fallback_fn
= BUILT_IN_FLOOR
;
2755 /* Make a suitable register to place result in. */
2756 mode
= TYPE_MODE (TREE_TYPE (exp
));
2758 target
= gen_reg_rtx (mode
);
2760 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2761 need to expand the argument again. This way, we will not perform
2762 side-effects more the once. */
2763 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2765 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2769 /* Compute into TARGET. */
2770 if (expand_sfix_optab (target
, op0
, builtin_optab
))
2772 /* Output the entire sequence. */
2773 insns
= get_insns ();
2779 /* If we were unable to expand via the builtin, stop the sequence
2780 (without outputting the insns). */
2783 /* Fall back to floating point rounding optab. */
2784 fallback_fndecl
= mathfn_built_in (TREE_TYPE (arg
), fallback_fn
);
2786 /* For non-C99 targets we may end up without a fallback fndecl here
2787 if the user called __builtin_lfloor directly. In this case emit
2788 a call to the floor/ceil variants nevertheless. This should result
2789 in the best user experience for not full C99 targets. */
2790 if (fallback_fndecl
== NULL_TREE
)
2793 const char *name
= NULL
;
2795 switch (DECL_FUNCTION_CODE (fndecl
))
2797 case BUILT_IN_ICEIL
:
2798 case BUILT_IN_LCEIL
:
2799 case BUILT_IN_LLCEIL
:
2802 case BUILT_IN_ICEILF
:
2803 case BUILT_IN_LCEILF
:
2804 case BUILT_IN_LLCEILF
:
2807 case BUILT_IN_ICEILL
:
2808 case BUILT_IN_LCEILL
:
2809 case BUILT_IN_LLCEILL
:
2812 case BUILT_IN_IFLOOR
:
2813 case BUILT_IN_LFLOOR
:
2814 case BUILT_IN_LLFLOOR
:
2817 case BUILT_IN_IFLOORF
:
2818 case BUILT_IN_LFLOORF
:
2819 case BUILT_IN_LLFLOORF
:
2822 case BUILT_IN_IFLOORL
:
2823 case BUILT_IN_LFLOORL
:
2824 case BUILT_IN_LLFLOORL
:
2831 fntype
= build_function_type_list (TREE_TYPE (arg
),
2832 TREE_TYPE (arg
), NULL_TREE
);
2833 fallback_fndecl
= build_fn_decl (name
, fntype
);
2836 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
), fallback_fndecl
, 1, arg
);
2838 tmp
= expand_normal (exp
);
2839 tmp
= maybe_emit_group_store (tmp
, TREE_TYPE (exp
));
2841 /* Truncate the result of floating point optab to integer
2842 via expand_fix (). */
2843 target
= gen_reg_rtx (mode
);
2844 expand_fix (target
, tmp
, 0);
2849 /* Expand a call to one of the builtin math functions doing integer
2851 Return 0 if a normal call should be emitted rather than expanding the
2852 function in-line. EXP is the expression that is a call to the builtin
2853 function; if convenient, the result should be placed in TARGET. */
2856 expand_builtin_int_roundingfn_2 (tree exp
, rtx target
)
2858 convert_optab builtin_optab
;
2861 tree fndecl
= get_callee_fndecl (exp
);
2864 enum built_in_function fallback_fn
= BUILT_IN_NONE
;
2866 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2869 arg
= CALL_EXPR_ARG (exp
, 0);
2871 switch (DECL_FUNCTION_CODE (fndecl
))
2873 CASE_FLT_FN (BUILT_IN_IRINT
):
2874 fallback_fn
= BUILT_IN_LRINT
;
2876 CASE_FLT_FN (BUILT_IN_LRINT
):
2877 CASE_FLT_FN (BUILT_IN_LLRINT
):
2878 builtin_optab
= lrint_optab
;
2881 CASE_FLT_FN (BUILT_IN_IROUND
):
2882 fallback_fn
= BUILT_IN_LROUND
;
2884 CASE_FLT_FN (BUILT_IN_LROUND
):
2885 CASE_FLT_FN (BUILT_IN_LLROUND
):
2886 builtin_optab
= lround_optab
;
2893 /* There's no easy way to detect the case we need to set EDOM. */
2894 if (flag_errno_math
&& fallback_fn
== BUILT_IN_NONE
)
2897 /* Make a suitable register to place result in. */
2898 mode
= TYPE_MODE (TREE_TYPE (exp
));
2900 /* There's no easy way to detect the case we need to set EDOM. */
2901 if (!flag_errno_math
)
2903 rtx result
= gen_reg_rtx (mode
);
2905 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2906 need to expand the argument again. This way, we will not perform
2907 side-effects more the once. */
2908 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2910 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2914 if (expand_sfix_optab (result
, op0
, builtin_optab
))
2916 /* Output the entire sequence. */
2917 insns
= get_insns ();
2923 /* If we were unable to expand via the builtin, stop the sequence
2924 (without outputting the insns) and call to the library function
2925 with the stabilized argument list. */
2929 if (fallback_fn
!= BUILT_IN_NONE
)
2931 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2932 targets, (int) round (x) should never be transformed into
2933 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2934 a call to lround in the hope that the target provides at least some
2935 C99 functions. This should result in the best user experience for
2936 not full C99 targets. */
2937 tree fallback_fndecl
= mathfn_built_in_1 (TREE_TYPE (arg
),
2940 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
),
2941 fallback_fndecl
, 1, arg
);
2943 target
= expand_call (exp
, NULL_RTX
, target
== const0_rtx
);
2944 target
= maybe_emit_group_store (target
, TREE_TYPE (exp
));
2945 return convert_to_mode (mode
, target
, 0);
2948 return expand_call (exp
, target
, target
== const0_rtx
);
2951 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2952 a normal call should be emitted rather than expanding the function
2953 in-line. EXP is the expression that is a call to the builtin
2954 function; if convenient, the result should be placed in TARGET. */
2957 expand_builtin_powi (tree exp
, rtx target
)
2964 if (! validate_arglist (exp
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2967 arg0
= CALL_EXPR_ARG (exp
, 0);
2968 arg1
= CALL_EXPR_ARG (exp
, 1);
2969 mode
= TYPE_MODE (TREE_TYPE (exp
));
2971 /* Emit a libcall to libgcc. */
2973 /* Mode of the 2nd argument must match that of an int. */
2974 mode2
= mode_for_size (INT_TYPE_SIZE
, MODE_INT
, 0);
2976 if (target
== NULL_RTX
)
2977 target
= gen_reg_rtx (mode
);
2979 op0
= expand_expr (arg0
, NULL_RTX
, mode
, EXPAND_NORMAL
);
2980 if (GET_MODE (op0
) != mode
)
2981 op0
= convert_to_mode (mode
, op0
, 0);
2982 op1
= expand_expr (arg1
, NULL_RTX
, mode2
, EXPAND_NORMAL
);
2983 if (GET_MODE (op1
) != mode2
)
2984 op1
= convert_to_mode (mode2
, op1
, 0);
2986 target
= emit_library_call_value (optab_libfunc (powi_optab
, mode
),
2987 target
, LCT_CONST
, mode
, 2,
2988 op0
, mode
, op1
, mode2
);
2993 /* Expand expression EXP which is a call to the strlen builtin. Return
2994 NULL_RTX if we failed the caller should emit a normal call, otherwise
2995 try to get the result in TARGET, if convenient. */
2998 expand_builtin_strlen (tree exp
, rtx target
,
2999 machine_mode target_mode
)
3001 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
3005 struct expand_operand ops
[4];
3008 tree src
= CALL_EXPR_ARG (exp
, 0);
3010 rtx_insn
*before_strlen
;
3011 machine_mode insn_mode
= target_mode
;
3012 enum insn_code icode
= CODE_FOR_nothing
;
3015 /* If the length can be computed at compile-time, return it. */
3016 len
= c_strlen (src
, 0);
3018 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3020 /* If the length can be computed at compile-time and is constant
3021 integer, but there are side-effects in src, evaluate
3022 src for side-effects, then return len.
3023 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3024 can be optimized into: i++; x = 3; */
3025 len
= c_strlen (src
, 1);
3026 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
3028 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3029 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3032 align
= get_pointer_alignment (src
) / BITS_PER_UNIT
;
3034 /* If SRC is not a pointer type, don't do this operation inline. */
3038 /* Bail out if we can't compute strlen in the right mode. */
3039 while (insn_mode
!= VOIDmode
)
3041 icode
= optab_handler (strlen_optab
, insn_mode
);
3042 if (icode
!= CODE_FOR_nothing
)
3045 insn_mode
= GET_MODE_WIDER_MODE (insn_mode
);
3047 if (insn_mode
== VOIDmode
)
3050 /* Make a place to hold the source address. We will not expand
3051 the actual source until we are sure that the expansion will
3052 not fail -- there are trees that cannot be expanded twice. */
3053 src_reg
= gen_reg_rtx (Pmode
);
3055 /* Mark the beginning of the strlen sequence so we can emit the
3056 source operand later. */
3057 before_strlen
= get_last_insn ();
3059 create_output_operand (&ops
[0], target
, insn_mode
);
3060 create_fixed_operand (&ops
[1], gen_rtx_MEM (BLKmode
, src_reg
));
3061 create_integer_operand (&ops
[2], 0);
3062 create_integer_operand (&ops
[3], align
);
3063 if (!maybe_expand_insn (icode
, 4, ops
))
3066 /* Now that we are assured of success, expand the source. */
3068 pat
= expand_expr (src
, src_reg
, Pmode
, EXPAND_NORMAL
);
3071 #ifdef POINTERS_EXTEND_UNSIGNED
3072 if (GET_MODE (pat
) != Pmode
)
3073 pat
= convert_to_mode (Pmode
, pat
,
3074 POINTERS_EXTEND_UNSIGNED
);
3076 emit_move_insn (src_reg
, pat
);
3082 emit_insn_after (pat
, before_strlen
);
3084 emit_insn_before (pat
, get_insns ());
3086 /* Return the value in the proper mode for this function. */
3087 if (GET_MODE (ops
[0].value
) == target_mode
)
3088 target
= ops
[0].value
;
3089 else if (target
!= 0)
3090 convert_move (target
, ops
[0].value
, 0);
3092 target
= convert_to_mode (target_mode
, ops
[0].value
, 0);
3098 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3099 bytes from constant string DATA + OFFSET and return it as target
3103 builtin_memcpy_read_str (void *data
, HOST_WIDE_INT offset
,
3106 const char *str
= (const char *) data
;
3108 gcc_assert (offset
>= 0
3109 && ((unsigned HOST_WIDE_INT
) offset
+ GET_MODE_SIZE (mode
)
3110 <= strlen (str
) + 1));
3112 return c_readstr (str
+ offset
, mode
);
3115 /* LEN specify length of the block of memcpy/memset operation.
3116 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3117 In some cases we can make very likely guess on max size, then we
3118 set it into PROBABLE_MAX_SIZE. */
3121 determine_block_size (tree len
, rtx len_rtx
,
3122 unsigned HOST_WIDE_INT
*min_size
,
3123 unsigned HOST_WIDE_INT
*max_size
,
3124 unsigned HOST_WIDE_INT
*probable_max_size
)
3126 if (CONST_INT_P (len_rtx
))
3128 *min_size
= *max_size
= *probable_max_size
= UINTVAL (len_rtx
);
3134 enum value_range_type range_type
= VR_UNDEFINED
;
3136 /* Determine bounds from the type. */
3137 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len
))))
3138 *min_size
= tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len
)));
3141 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len
))))
3142 *probable_max_size
= *max_size
3143 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len
)));
3145 *probable_max_size
= *max_size
= GET_MODE_MASK (GET_MODE (len_rtx
));
3147 if (TREE_CODE (len
) == SSA_NAME
)
3148 range_type
= get_range_info (len
, &min
, &max
);
3149 if (range_type
== VR_RANGE
)
3151 if (wi::fits_uhwi_p (min
) && *min_size
< min
.to_uhwi ())
3152 *min_size
= min
.to_uhwi ();
3153 if (wi::fits_uhwi_p (max
) && *max_size
> max
.to_uhwi ())
3154 *probable_max_size
= *max_size
= max
.to_uhwi ();
3156 else if (range_type
== VR_ANTI_RANGE
)
3158 /* Anti range 0...N lets us to determine minimal size to N+1. */
3161 if (wi::fits_uhwi_p (max
) && max
.to_uhwi () + 1 != 0)
3162 *min_size
= max
.to_uhwi () + 1;
3170 Produce anti range allowing negative values of N. We still
3171 can use the information and make a guess that N is not negative.
3173 else if (!wi::leu_p (max
, 1 << 30) && wi::fits_uhwi_p (min
))
3174 *probable_max_size
= min
.to_uhwi () - 1;
3177 gcc_checking_assert (*max_size
<=
3178 (unsigned HOST_WIDE_INT
)
3179 GET_MODE_MASK (GET_MODE (len_rtx
)));
3182 /* Helper function to do the actual work for expand_builtin_memcpy. */
3185 expand_builtin_memcpy_args (tree dest
, tree src
, tree len
, rtx target
, tree exp
)
3187 const char *src_str
;
3188 unsigned int src_align
= get_pointer_alignment (src
);
3189 unsigned int dest_align
= get_pointer_alignment (dest
);
3190 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
3191 HOST_WIDE_INT expected_size
= -1;
3192 unsigned int expected_align
= 0;
3193 unsigned HOST_WIDE_INT min_size
;
3194 unsigned HOST_WIDE_INT max_size
;
3195 unsigned HOST_WIDE_INT probable_max_size
;
3197 /* If DEST is not a pointer type, call the normal function. */
3198 if (dest_align
== 0)
3201 /* If either SRC is not a pointer type, don't do this
3202 operation in-line. */
3206 if (currently_expanding_gimple_stmt
)
3207 stringop_block_profile (currently_expanding_gimple_stmt
,
3208 &expected_align
, &expected_size
);
3210 if (expected_align
< dest_align
)
3211 expected_align
= dest_align
;
3212 dest_mem
= get_memory_rtx (dest
, len
);
3213 set_mem_align (dest_mem
, dest_align
);
3214 len_rtx
= expand_normal (len
);
3215 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3216 &probable_max_size
);
3217 src_str
= c_getstr (src
);
3219 /* If SRC is a string constant and block move would be done
3220 by pieces, we can avoid loading the string from memory
3221 and only stored the computed constants. */
3223 && CONST_INT_P (len_rtx
)
3224 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3225 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3226 CONST_CAST (char *, src_str
),
3229 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3230 builtin_memcpy_read_str
,
3231 CONST_CAST (char *, src_str
),
3232 dest_align
, false, 0);
3233 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3234 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3238 src_mem
= get_memory_rtx (src
, len
);
3239 set_mem_align (src_mem
, src_align
);
3241 /* Copy word part most expediently. */
3242 dest_addr
= emit_block_move_hints (dest_mem
, src_mem
, len_rtx
,
3243 CALL_EXPR_TAILCALL (exp
)
3244 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3245 expected_align
, expected_size
,
3246 min_size
, max_size
, probable_max_size
);
3250 dest_addr
= force_operand (XEXP (dest_mem
, 0), target
);
3251 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3257 /* Expand a call EXP to the memcpy builtin.
3258 Return NULL_RTX if we failed, the caller should emit a normal call,
3259 otherwise try to get the result in TARGET, if convenient (and in
3260 mode MODE if that's convenient). */
3263 expand_builtin_memcpy (tree exp
, rtx target
)
3265 if (!validate_arglist (exp
,
3266 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3270 tree dest
= CALL_EXPR_ARG (exp
, 0);
3271 tree src
= CALL_EXPR_ARG (exp
, 1);
3272 tree len
= CALL_EXPR_ARG (exp
, 2);
3273 return expand_builtin_memcpy_args (dest
, src
, len
, target
, exp
);
3277 /* Expand an instrumented call EXP to the memcpy builtin.
3278 Return NULL_RTX if we failed, the caller should emit a normal call,
3279 otherwise try to get the result in TARGET, if convenient (and in
3280 mode MODE if that's convenient). */
3283 expand_builtin_memcpy_with_bounds (tree exp
, rtx target
)
3285 if (!validate_arglist (exp
,
3286 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3287 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3288 INTEGER_TYPE
, VOID_TYPE
))
3292 tree dest
= CALL_EXPR_ARG (exp
, 0);
3293 tree src
= CALL_EXPR_ARG (exp
, 2);
3294 tree len
= CALL_EXPR_ARG (exp
, 4);
3295 rtx res
= expand_builtin_memcpy_args (dest
, src
, len
, target
, exp
);
3297 /* Return src bounds with the result. */
3300 rtx bnd
= force_reg (targetm
.chkp_bound_mode (),
3301 expand_normal (CALL_EXPR_ARG (exp
, 1)));
3302 res
= chkp_join_splitted_slot (res
, bnd
);
3308 /* Expand a call EXP to the mempcpy builtin.
3309 Return NULL_RTX if we failed; the caller should emit a normal call,
3310 otherwise try to get the result in TARGET, if convenient (and in
3311 mode MODE if that's convenient). If ENDP is 0 return the
3312 destination pointer, if ENDP is 1 return the end pointer ala
3313 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3317 expand_builtin_mempcpy (tree exp
, rtx target
, machine_mode mode
)
3319 if (!validate_arglist (exp
,
3320 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3324 tree dest
= CALL_EXPR_ARG (exp
, 0);
3325 tree src
= CALL_EXPR_ARG (exp
, 1);
3326 tree len
= CALL_EXPR_ARG (exp
, 2);
3327 return expand_builtin_mempcpy_args (dest
, src
, len
,
3328 target
, mode
, /*endp=*/ 1,
3333 /* Expand an instrumented call EXP to the mempcpy builtin.
3334 Return NULL_RTX if we failed, the caller should emit a normal call,
3335 otherwise try to get the result in TARGET, if convenient (and in
3336 mode MODE if that's convenient). */
3339 expand_builtin_mempcpy_with_bounds (tree exp
, rtx target
, machine_mode mode
)
3341 if (!validate_arglist (exp
,
3342 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3343 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3344 INTEGER_TYPE
, VOID_TYPE
))
3348 tree dest
= CALL_EXPR_ARG (exp
, 0);
3349 tree src
= CALL_EXPR_ARG (exp
, 2);
3350 tree len
= CALL_EXPR_ARG (exp
, 4);
3351 rtx res
= expand_builtin_mempcpy_args (dest
, src
, len
, target
,
3354 /* Return src bounds with the result. */
3357 rtx bnd
= force_reg (targetm
.chkp_bound_mode (),
3358 expand_normal (CALL_EXPR_ARG (exp
, 1)));
3359 res
= chkp_join_splitted_slot (res
, bnd
);
3365 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3366 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3367 so that this can also be called without constructing an actual CALL_EXPR.
3368 The other arguments and return value are the same as for
3369 expand_builtin_mempcpy. */
3372 expand_builtin_mempcpy_args (tree dest
, tree src
, tree len
,
3373 rtx target
, machine_mode mode
, int endp
,
3376 tree fndecl
= get_callee_fndecl (orig_exp
);
3378 /* If return value is ignored, transform mempcpy into memcpy. */
3379 if (target
== const0_rtx
3380 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
3381 && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP
))
3383 tree fn
= builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP
);
3384 tree result
= build_call_nofold_loc (UNKNOWN_LOCATION
, fn
, 3,
3386 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3388 else if (target
== const0_rtx
3389 && builtin_decl_implicit_p (BUILT_IN_MEMCPY
))
3391 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
3392 tree result
= build_call_nofold_loc (UNKNOWN_LOCATION
, fn
, 3,
3394 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3398 const char *src_str
;
3399 unsigned int src_align
= get_pointer_alignment (src
);
3400 unsigned int dest_align
= get_pointer_alignment (dest
);
3401 rtx dest_mem
, src_mem
, len_rtx
;
3403 /* If either SRC or DEST is not a pointer type, don't do this
3404 operation in-line. */
3405 if (dest_align
== 0 || src_align
== 0)
3408 /* If LEN is not constant, call the normal function. */
3409 if (! tree_fits_uhwi_p (len
))
3412 len_rtx
= expand_normal (len
);
3413 src_str
= c_getstr (src
);
3415 /* If SRC is a string constant and block move would be done
3416 by pieces, we can avoid loading the string from memory
3417 and only stored the computed constants. */
3419 && CONST_INT_P (len_rtx
)
3420 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3421 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3422 CONST_CAST (char *, src_str
),
3425 dest_mem
= get_memory_rtx (dest
, len
);
3426 set_mem_align (dest_mem
, dest_align
);
3427 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3428 builtin_memcpy_read_str
,
3429 CONST_CAST (char *, src_str
),
3430 dest_align
, false, endp
);
3431 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3432 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3436 if (CONST_INT_P (len_rtx
)
3437 && can_move_by_pieces (INTVAL (len_rtx
),
3438 MIN (dest_align
, src_align
)))
3440 dest_mem
= get_memory_rtx (dest
, len
);
3441 set_mem_align (dest_mem
, dest_align
);
3442 src_mem
= get_memory_rtx (src
, len
);
3443 set_mem_align (src_mem
, src_align
);
3444 dest_mem
= move_by_pieces (dest_mem
, src_mem
, INTVAL (len_rtx
),
3445 MIN (dest_align
, src_align
), endp
);
3446 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3447 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3456 # define HAVE_movstr 0
3457 # define CODE_FOR_movstr CODE_FOR_nothing
3460 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3461 we failed, the caller should emit a normal call, otherwise try to
3462 get the result in TARGET, if convenient. If ENDP is 0 return the
3463 destination pointer, if ENDP is 1 return the end pointer ala
3464 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3468 expand_movstr (tree dest
, tree src
, rtx target
, int endp
)
3470 struct expand_operand ops
[3];
3477 dest_mem
= get_memory_rtx (dest
, NULL
);
3478 src_mem
= get_memory_rtx (src
, NULL
);
3481 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
3482 dest_mem
= replace_equiv_address (dest_mem
, target
);
3485 create_output_operand (&ops
[0], endp
? target
: NULL_RTX
, Pmode
);
3486 create_fixed_operand (&ops
[1], dest_mem
);
3487 create_fixed_operand (&ops
[2], src_mem
);
3488 if (!maybe_expand_insn (CODE_FOR_movstr
, 3, ops
))
3491 if (endp
&& target
!= const0_rtx
)
3493 target
= ops
[0].value
;
3494 /* movstr is supposed to set end to the address of the NUL
3495 terminator. If the caller requested a mempcpy-like return value,
3499 rtx tem
= plus_constant (GET_MODE (target
),
3500 gen_lowpart (GET_MODE (target
), target
), 1);
3501 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
3507 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3508 NULL_RTX if we failed the caller should emit a normal call, otherwise
3509 try to get the result in TARGET, if convenient (and in mode MODE if that's
3513 expand_builtin_strcpy (tree exp
, rtx target
)
3515 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3517 tree dest
= CALL_EXPR_ARG (exp
, 0);
3518 tree src
= CALL_EXPR_ARG (exp
, 1);
3519 return expand_builtin_strcpy_args (dest
, src
, target
);
3524 /* Helper function to do the actual work for expand_builtin_strcpy. The
3525 arguments to the builtin_strcpy call DEST and SRC are broken out
3526 so that this can also be called without constructing an actual CALL_EXPR.
3527 The other arguments and return value are the same as for
3528 expand_builtin_strcpy. */
3531 expand_builtin_strcpy_args (tree dest
, tree src
, rtx target
)
3533 return expand_movstr (dest
, src
, target
, /*endp=*/0);
3536 /* Expand a call EXP to the stpcpy builtin.
3537 Return NULL_RTX if we failed the caller should emit a normal call,
3538 otherwise try to get the result in TARGET, if convenient (and in
3539 mode MODE if that's convenient). */
3542 expand_builtin_stpcpy (tree exp
, rtx target
, machine_mode mode
)
3545 location_t loc
= EXPR_LOCATION (exp
);
3547 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3550 dst
= CALL_EXPR_ARG (exp
, 0);
3551 src
= CALL_EXPR_ARG (exp
, 1);
3553 /* If return value is ignored, transform stpcpy into strcpy. */
3554 if (target
== const0_rtx
&& builtin_decl_implicit (BUILT_IN_STRCPY
))
3556 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3557 tree result
= build_call_nofold_loc (loc
, fn
, 2, dst
, src
);
3558 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3565 /* Ensure we get an actual string whose length can be evaluated at
3566 compile-time, not an expression containing a string. This is
3567 because the latter will potentially produce pessimized code
3568 when used to produce the return value. */
3569 if (! c_getstr (src
) || ! (len
= c_strlen (src
, 0)))
3570 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3572 lenp1
= size_binop_loc (loc
, PLUS_EXPR
, len
, ssize_int (1));
3573 ret
= expand_builtin_mempcpy_args (dst
, src
, lenp1
,
3574 target
, mode
, /*endp=*/2,
3580 if (TREE_CODE (len
) == INTEGER_CST
)
3582 rtx len_rtx
= expand_normal (len
);
3584 if (CONST_INT_P (len_rtx
))
3586 ret
= expand_builtin_strcpy_args (dst
, src
, target
);
3592 if (mode
!= VOIDmode
)
3593 target
= gen_reg_rtx (mode
);
3595 target
= gen_reg_rtx (GET_MODE (ret
));
3597 if (GET_MODE (target
) != GET_MODE (ret
))
3598 ret
= gen_lowpart (GET_MODE (target
), ret
);
3600 ret
= plus_constant (GET_MODE (ret
), ret
, INTVAL (len_rtx
));
3601 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
3609 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3613 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3614 bytes from constant string DATA + OFFSET and return it as target
3618 builtin_strncpy_read_str (void *data
, HOST_WIDE_INT offset
,
3621 const char *str
= (const char *) data
;
3623 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
3626 return c_readstr (str
+ offset
, mode
);
3629 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3630 NULL_RTX if we failed the caller should emit a normal call. */
3633 expand_builtin_strncpy (tree exp
, rtx target
)
3635 location_t loc
= EXPR_LOCATION (exp
);
3637 if (validate_arglist (exp
,
3638 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3640 tree dest
= CALL_EXPR_ARG (exp
, 0);
3641 tree src
= CALL_EXPR_ARG (exp
, 1);
3642 tree len
= CALL_EXPR_ARG (exp
, 2);
3643 tree slen
= c_strlen (src
, 1);
3645 /* We must be passed a constant len and src parameter. */
3646 if (!tree_fits_uhwi_p (len
) || !slen
|| !tree_fits_uhwi_p (slen
))
3649 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
3651 /* We're required to pad with trailing zeros if the requested
3652 len is greater than strlen(s2)+1. In that case try to
3653 use store_by_pieces, if it fails, punt. */
3654 if (tree_int_cst_lt (slen
, len
))
3656 unsigned int dest_align
= get_pointer_alignment (dest
);
3657 const char *p
= c_getstr (src
);
3660 if (!p
|| dest_align
== 0 || !tree_fits_uhwi_p (len
)
3661 || !can_store_by_pieces (tree_to_uhwi (len
),
3662 builtin_strncpy_read_str
,
3663 CONST_CAST (char *, p
),
3667 dest_mem
= get_memory_rtx (dest
, len
);
3668 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3669 builtin_strncpy_read_str
,
3670 CONST_CAST (char *, p
), dest_align
, false, 0);
3671 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3672 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3679 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3680 bytes from constant string DATA + OFFSET and return it as target
3684 builtin_memset_read_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3687 const char *c
= (const char *) data
;
3688 char *p
= XALLOCAVEC (char, GET_MODE_SIZE (mode
));
3690 memset (p
, *c
, GET_MODE_SIZE (mode
));
3692 return c_readstr (p
, mode
);
3695 /* Callback routine for store_by_pieces. Return the RTL of a register
3696 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3697 char value given in the RTL register data. For example, if mode is
3698 4 bytes wide, return the RTL for 0x01010101*data. */
3701 builtin_memset_gen_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3708 size
= GET_MODE_SIZE (mode
);
3712 p
= XALLOCAVEC (char, size
);
3713 memset (p
, 1, size
);
3714 coeff
= c_readstr (p
, mode
);
3716 target
= convert_to_mode (mode
, (rtx
) data
, 1);
3717 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
3718 return force_reg (mode
, target
);
3721 /* Expand expression EXP, which is a call to the memset builtin. Return
3722 NULL_RTX if we failed the caller should emit a normal call, otherwise
3723 try to get the result in TARGET, if convenient (and in mode MODE if that's
3727 expand_builtin_memset (tree exp
, rtx target
, machine_mode mode
)
3729 if (!validate_arglist (exp
,
3730 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3734 tree dest
= CALL_EXPR_ARG (exp
, 0);
3735 tree val
= CALL_EXPR_ARG (exp
, 1);
3736 tree len
= CALL_EXPR_ARG (exp
, 2);
3737 return expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
3741 /* Expand expression EXP, which is an instrumented call to the memset builtin.
3742 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
3743 try to get the result in TARGET, if convenient (and in mode MODE if that's
3747 expand_builtin_memset_with_bounds (tree exp
, rtx target
, machine_mode mode
)
3749 if (!validate_arglist (exp
,
3750 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3751 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3755 tree dest
= CALL_EXPR_ARG (exp
, 0);
3756 tree val
= CALL_EXPR_ARG (exp
, 2);
3757 tree len
= CALL_EXPR_ARG (exp
, 3);
3758 rtx res
= expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
3760 /* Return src bounds with the result. */
3763 rtx bnd
= force_reg (targetm
.chkp_bound_mode (),
3764 expand_normal (CALL_EXPR_ARG (exp
, 1)));
3765 res
= chkp_join_splitted_slot (res
, bnd
);
3771 /* Helper function to do the actual work for expand_builtin_memset. The
3772 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3773 so that this can also be called without constructing an actual CALL_EXPR.
3774 The other arguments and return value are the same as for
3775 expand_builtin_memset. */
3778 expand_builtin_memset_args (tree dest
, tree val
, tree len
,
3779 rtx target
, machine_mode mode
, tree orig_exp
)
3782 enum built_in_function fcode
;
3783 machine_mode val_mode
;
3785 unsigned int dest_align
;
3786 rtx dest_mem
, dest_addr
, len_rtx
;
3787 HOST_WIDE_INT expected_size
= -1;
3788 unsigned int expected_align
= 0;
3789 unsigned HOST_WIDE_INT min_size
;
3790 unsigned HOST_WIDE_INT max_size
;
3791 unsigned HOST_WIDE_INT probable_max_size
;
3793 dest_align
= get_pointer_alignment (dest
);
3795 /* If DEST is not a pointer type, don't do this operation in-line. */
3796 if (dest_align
== 0)
3799 if (currently_expanding_gimple_stmt
)
3800 stringop_block_profile (currently_expanding_gimple_stmt
,
3801 &expected_align
, &expected_size
);
3803 if (expected_align
< dest_align
)
3804 expected_align
= dest_align
;
3806 /* If the LEN parameter is zero, return DEST. */
3807 if (integer_zerop (len
))
3809 /* Evaluate and ignore VAL in case it has side-effects. */
3810 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3811 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
3814 /* Stabilize the arguments in case we fail. */
3815 dest
= builtin_save_expr (dest
);
3816 val
= builtin_save_expr (val
);
3817 len
= builtin_save_expr (len
);
3819 len_rtx
= expand_normal (len
);
3820 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3821 &probable_max_size
);
3822 dest_mem
= get_memory_rtx (dest
, len
);
3823 val_mode
= TYPE_MODE (unsigned_char_type_node
);
3825 if (TREE_CODE (val
) != INTEGER_CST
)
3829 val_rtx
= expand_normal (val
);
3830 val_rtx
= convert_to_mode (val_mode
, val_rtx
, 0);
3832 /* Assume that we can memset by pieces if we can store
3833 * the coefficients by pieces (in the required modes).
3834 * We can't pass builtin_memset_gen_str as that emits RTL. */
3836 if (tree_fits_uhwi_p (len
)
3837 && can_store_by_pieces (tree_to_uhwi (len
),
3838 builtin_memset_read_str
, &c
, dest_align
,
3841 val_rtx
= force_reg (val_mode
, val_rtx
);
3842 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3843 builtin_memset_gen_str
, val_rtx
, dest_align
,
3846 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, val_rtx
,
3847 dest_align
, expected_align
,
3848 expected_size
, min_size
, max_size
,
3852 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3853 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3857 if (target_char_cast (val
, &c
))
3862 if (tree_fits_uhwi_p (len
)
3863 && can_store_by_pieces (tree_to_uhwi (len
),
3864 builtin_memset_read_str
, &c
, dest_align
,
3866 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3867 builtin_memset_read_str
, &c
, dest_align
, true, 0);
3868 else if (!set_storage_via_setmem (dest_mem
, len_rtx
,
3869 gen_int_mode (c
, val_mode
),
3870 dest_align
, expected_align
,
3871 expected_size
, min_size
, max_size
,
3875 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3876 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3880 set_mem_align (dest_mem
, dest_align
);
3881 dest_addr
= clear_storage_hints (dest_mem
, len_rtx
,
3882 CALL_EXPR_TAILCALL (orig_exp
)
3883 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3884 expected_align
, expected_size
,
3890 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3891 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3897 fndecl
= get_callee_fndecl (orig_exp
);
3898 fcode
= DECL_FUNCTION_CODE (fndecl
);
3899 if (fcode
== BUILT_IN_MEMSET
3900 || fcode
== BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP
)
3901 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 3,
3903 else if (fcode
== BUILT_IN_BZERO
)
3904 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 2,
3908 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
3909 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
3910 return expand_call (fn
, target
, target
== const0_rtx
);
3913 /* Expand expression EXP, which is a call to the bzero builtin. Return
3914 NULL_RTX if we failed the caller should emit a normal call. */
3917 expand_builtin_bzero (tree exp
)
3920 location_t loc
= EXPR_LOCATION (exp
);
3922 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3925 dest
= CALL_EXPR_ARG (exp
, 0);
3926 size
= CALL_EXPR_ARG (exp
, 1);
3928 /* New argument list transforming bzero(ptr x, int y) to
3929 memset(ptr x, int 0, size_t y). This is done this way
3930 so that if it isn't expanded inline, we fallback to
3931 calling bzero instead of memset. */
3933 return expand_builtin_memset_args (dest
, integer_zero_node
,
3934 fold_convert_loc (loc
,
3935 size_type_node
, size
),
3936 const0_rtx
, VOIDmode
, exp
);
3939 /* Expand expression EXP, which is a call to the memcmp built-in function.
3940 Return NULL_RTX if we failed and the caller should emit a normal call,
3941 otherwise try to get the result in TARGET, if convenient (and in mode
3942 MODE, if that's convenient). */
3945 expand_builtin_memcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
3946 ATTRIBUTE_UNUSED machine_mode mode
)
3948 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
3950 if (!validate_arglist (exp
,
3951 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3954 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3955 implementing memcmp because it will stop if it encounters two
3957 #if defined HAVE_cmpmemsi
3959 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3962 tree arg1
= CALL_EXPR_ARG (exp
, 0);
3963 tree arg2
= CALL_EXPR_ARG (exp
, 1);
3964 tree len
= CALL_EXPR_ARG (exp
, 2);
3966 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
3967 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
3968 machine_mode insn_mode
;
3971 insn_mode
= insn_data
[(int) CODE_FOR_cmpmemsi
].operand
[0].mode
;
3975 /* If we don't have POINTER_TYPE, call the function. */
3976 if (arg1_align
== 0 || arg2_align
== 0)
3979 /* Make a place to write the result of the instruction. */
3982 && REG_P (result
) && GET_MODE (result
) == insn_mode
3983 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3984 result
= gen_reg_rtx (insn_mode
);
3986 arg1_rtx
= get_memory_rtx (arg1
, len
);
3987 arg2_rtx
= get_memory_rtx (arg2
, len
);
3988 arg3_rtx
= expand_normal (fold_convert_loc (loc
, sizetype
, len
));
3990 /* Set MEM_SIZE as appropriate. */
3991 if (CONST_INT_P (arg3_rtx
))
3993 set_mem_size (arg1_rtx
, INTVAL (arg3_rtx
));
3994 set_mem_size (arg2_rtx
, INTVAL (arg3_rtx
));
3998 insn
= gen_cmpmemsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3999 GEN_INT (MIN (arg1_align
, arg2_align
)));
4006 emit_library_call_value (memcmp_libfunc
, result
, LCT_PURE
,
4007 TYPE_MODE (integer_type_node
), 3,
4008 XEXP (arg1_rtx
, 0), Pmode
,
4009 XEXP (arg2_rtx
, 0), Pmode
,
4010 convert_to_mode (TYPE_MODE (sizetype
), arg3_rtx
,
4011 TYPE_UNSIGNED (sizetype
)),
4012 TYPE_MODE (sizetype
));
4014 /* Return the value in the proper mode for this function. */
4015 mode
= TYPE_MODE (TREE_TYPE (exp
));
4016 if (GET_MODE (result
) == mode
)
4018 else if (target
!= 0)
4020 convert_move (target
, result
, 0);
4024 return convert_to_mode (mode
, result
, 0);
4026 #endif /* HAVE_cmpmemsi. */
4031 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4032 if we failed the caller should emit a normal call, otherwise try to get
4033 the result in TARGET, if convenient. */
4036 expand_builtin_strcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
)
4038 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4041 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
4042 if (direct_optab_handler (cmpstr_optab
, SImode
) != CODE_FOR_nothing
4043 || direct_optab_handler (cmpstrn_optab
, SImode
) != CODE_FOR_nothing
)
4045 rtx arg1_rtx
, arg2_rtx
;
4046 rtx result
, insn
= NULL_RTX
;
4048 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4049 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4051 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4052 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4054 /* If we don't have POINTER_TYPE, call the function. */
4055 if (arg1_align
== 0 || arg2_align
== 0)
4058 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4059 arg1
= builtin_save_expr (arg1
);
4060 arg2
= builtin_save_expr (arg2
);
4062 arg1_rtx
= get_memory_rtx (arg1
, NULL
);
4063 arg2_rtx
= get_memory_rtx (arg2
, NULL
);
4065 #ifdef HAVE_cmpstrsi
4066 /* Try to call cmpstrsi. */
4069 machine_mode insn_mode
4070 = insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
4072 /* Make a place to write the result of the instruction. */
4075 && REG_P (result
) && GET_MODE (result
) == insn_mode
4076 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
4077 result
= gen_reg_rtx (insn_mode
);
4079 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
,
4080 GEN_INT (MIN (arg1_align
, arg2_align
)));
4083 #ifdef HAVE_cmpstrnsi
4084 /* Try to determine at least one length and call cmpstrnsi. */
4085 if (!insn
&& HAVE_cmpstrnsi
)
4090 machine_mode insn_mode
4091 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
4092 tree len1
= c_strlen (arg1
, 1);
4093 tree len2
= c_strlen (arg2
, 1);
4096 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
4098 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
4100 /* If we don't have a constant length for the first, use the length
4101 of the second, if we know it. We don't require a constant for
4102 this case; some cost analysis could be done if both are available
4103 but neither is constant. For now, assume they're equally cheap,
4104 unless one has side effects. If both strings have constant lengths,
4111 else if (TREE_SIDE_EFFECTS (len1
))
4113 else if (TREE_SIDE_EFFECTS (len2
))
4115 else if (TREE_CODE (len1
) != INTEGER_CST
)
4117 else if (TREE_CODE (len2
) != INTEGER_CST
)
4119 else if (tree_int_cst_lt (len1
, len2
))
4124 /* If both arguments have side effects, we cannot optimize. */
4125 if (!len
|| TREE_SIDE_EFFECTS (len
))
4128 arg3_rtx
= expand_normal (len
);
4130 /* Make a place to write the result of the instruction. */
4133 && REG_P (result
) && GET_MODE (result
) == insn_mode
4134 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
4135 result
= gen_reg_rtx (insn_mode
);
4137 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
4138 GEN_INT (MIN (arg1_align
, arg2_align
)));
4147 /* Return the value in the proper mode for this function. */
4148 mode
= TYPE_MODE (TREE_TYPE (exp
));
4149 if (GET_MODE (result
) == mode
)
4152 return convert_to_mode (mode
, result
, 0);
4153 convert_move (target
, result
, 0);
4157 /* Expand the library call ourselves using a stabilized argument
4158 list to avoid re-evaluating the function's arguments twice. */
4159 #ifdef HAVE_cmpstrnsi
4162 fndecl
= get_callee_fndecl (exp
);
4163 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 2, arg1
, arg2
);
4164 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4165 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4166 return expand_call (fn
, target
, target
== const0_rtx
);
4172 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4173 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4174 the result in TARGET, if convenient. */
4177 expand_builtin_strncmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
4178 ATTRIBUTE_UNUSED machine_mode mode
)
4180 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
4182 if (!validate_arglist (exp
,
4183 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4186 /* If c_strlen can determine an expression for one of the string
4187 lengths, and it doesn't have side effects, then emit cmpstrnsi
4188 using length MIN(strlen(string)+1, arg3). */
4189 #ifdef HAVE_cmpstrnsi
4192 tree len
, len1
, len2
;
4193 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
4196 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4197 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4198 tree arg3
= CALL_EXPR_ARG (exp
, 2);
4200 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4201 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4202 machine_mode insn_mode
4203 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
4205 len1
= c_strlen (arg1
, 1);
4206 len2
= c_strlen (arg2
, 1);
4209 len1
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len1
);
4211 len2
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len2
);
4213 /* If we don't have a constant length for the first, use the length
4214 of the second, if we know it. We don't require a constant for
4215 this case; some cost analysis could be done if both are available
4216 but neither is constant. For now, assume they're equally cheap,
4217 unless one has side effects. If both strings have constant lengths,
4224 else if (TREE_SIDE_EFFECTS (len1
))
4226 else if (TREE_SIDE_EFFECTS (len2
))
4228 else if (TREE_CODE (len1
) != INTEGER_CST
)
4230 else if (TREE_CODE (len2
) != INTEGER_CST
)
4232 else if (tree_int_cst_lt (len1
, len2
))
4237 /* If both arguments have side effects, we cannot optimize. */
4238 if (!len
|| TREE_SIDE_EFFECTS (len
))
4241 /* The actual new length parameter is MIN(len,arg3). */
4242 len
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (len
), len
,
4243 fold_convert_loc (loc
, TREE_TYPE (len
), arg3
));
4245 /* If we don't have POINTER_TYPE, call the function. */
4246 if (arg1_align
== 0 || arg2_align
== 0)
4249 /* Make a place to write the result of the instruction. */
4252 && REG_P (result
) && GET_MODE (result
) == insn_mode
4253 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
4254 result
= gen_reg_rtx (insn_mode
);
4256 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4257 arg1
= builtin_save_expr (arg1
);
4258 arg2
= builtin_save_expr (arg2
);
4259 len
= builtin_save_expr (len
);
4261 arg1_rtx
= get_memory_rtx (arg1
, len
);
4262 arg2_rtx
= get_memory_rtx (arg2
, len
);
4263 arg3_rtx
= expand_normal (len
);
4264 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
4265 GEN_INT (MIN (arg1_align
, arg2_align
)));
4270 /* Return the value in the proper mode for this function. */
4271 mode
= TYPE_MODE (TREE_TYPE (exp
));
4272 if (GET_MODE (result
) == mode
)
4275 return convert_to_mode (mode
, result
, 0);
4276 convert_move (target
, result
, 0);
4280 /* Expand the library call ourselves using a stabilized argument
4281 list to avoid re-evaluating the function's arguments twice. */
4282 fndecl
= get_callee_fndecl (exp
);
4283 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 3,
4285 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4286 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4287 return expand_call (fn
, target
, target
== const0_rtx
);
4293 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4294 if that's convenient. */
4297 expand_builtin_saveregs (void)
4302 /* Don't do __builtin_saveregs more than once in a function.
4303 Save the result of the first call and reuse it. */
4304 if (saveregs_value
!= 0)
4305 return saveregs_value
;
4307 /* When this function is called, it means that registers must be
4308 saved on entry to this function. So we migrate the call to the
4309 first insn of this function. */
4313 /* Do whatever the machine needs done in this case. */
4314 val
= targetm
.calls
.expand_builtin_saveregs ();
4319 saveregs_value
= val
;
4321 /* Put the insns after the NOTE that starts the function. If this
4322 is inside a start_sequence, make the outer-level insn chain current, so
4323 the code is placed at the start of the function. */
4324 push_topmost_sequence ();
4325 emit_insn_after (seq
, entry_of_function ());
4326 pop_topmost_sequence ();
4331 /* Expand a call to __builtin_next_arg. */
4334 expand_builtin_next_arg (void)
4336 /* Checking arguments is already done in fold_builtin_next_arg
4337 that must be called before this function. */
4338 return expand_binop (ptr_mode
, add_optab
,
4339 crtl
->args
.internal_arg_pointer
,
4340 crtl
->args
.arg_offset_rtx
,
4341 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
4344 /* Make it easier for the backends by protecting the valist argument
4345 from multiple evaluations. */
4348 stabilize_va_list_loc (location_t loc
, tree valist
, int needs_lvalue
)
4350 tree vatype
= targetm
.canonical_va_list_type (TREE_TYPE (valist
));
4352 /* The current way of determining the type of valist is completely
4353 bogus. We should have the information on the va builtin instead. */
4355 vatype
= targetm
.fn_abi_va_list (cfun
->decl
);
4357 if (TREE_CODE (vatype
) == ARRAY_TYPE
)
4359 if (TREE_SIDE_EFFECTS (valist
))
4360 valist
= save_expr (valist
);
4362 /* For this case, the backends will be expecting a pointer to
4363 vatype, but it's possible we've actually been given an array
4364 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4366 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4368 tree p1
= build_pointer_type (TREE_TYPE (vatype
));
4369 valist
= build_fold_addr_expr_with_type_loc (loc
, valist
, p1
);
4374 tree pt
= build_pointer_type (vatype
);
4378 if (! TREE_SIDE_EFFECTS (valist
))
4381 valist
= fold_build1_loc (loc
, ADDR_EXPR
, pt
, valist
);
4382 TREE_SIDE_EFFECTS (valist
) = 1;
4385 if (TREE_SIDE_EFFECTS (valist
))
4386 valist
= save_expr (valist
);
4387 valist
= fold_build2_loc (loc
, MEM_REF
,
4388 vatype
, valist
, build_int_cst (pt
, 0));
4394 /* The "standard" definition of va_list is void*. */
4397 std_build_builtin_va_list (void)
4399 return ptr_type_node
;
4402 /* The "standard" abi va_list is va_list_type_node. */
4405 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED
)
4407 return va_list_type_node
;
4410 /* The "standard" type of va_list is va_list_type_node. */
4413 std_canonical_va_list_type (tree type
)
4417 if (INDIRECT_REF_P (type
))
4418 type
= TREE_TYPE (type
);
4419 else if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (TREE_TYPE (type
)))
4420 type
= TREE_TYPE (type
);
4421 wtype
= va_list_type_node
;
4423 /* Treat structure va_list types. */
4424 if (TREE_CODE (wtype
) == RECORD_TYPE
&& POINTER_TYPE_P (htype
))
4425 htype
= TREE_TYPE (htype
);
4426 else if (TREE_CODE (wtype
) == ARRAY_TYPE
)
4428 /* If va_list is an array type, the argument may have decayed
4429 to a pointer type, e.g. by being passed to another function.
4430 In that case, unwrap both types so that we can compare the
4431 underlying records. */
4432 if (TREE_CODE (htype
) == ARRAY_TYPE
4433 || POINTER_TYPE_P (htype
))
4435 wtype
= TREE_TYPE (wtype
);
4436 htype
= TREE_TYPE (htype
);
4439 if (TYPE_MAIN_VARIANT (wtype
) == TYPE_MAIN_VARIANT (htype
))
4440 return va_list_type_node
;
4445 /* The "standard" implementation of va_start: just assign `nextarg' to
4449 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
4451 rtx va_r
= expand_expr (valist
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
4452 convert_move (va_r
, nextarg
, 0);
4454 /* We do not have any valid bounds for the pointer, so
4455 just store zero bounds for it. */
4456 if (chkp_function_instrumented_p (current_function_decl
))
4457 chkp_expand_bounds_reset_for_mem (valist
,
4458 make_tree (TREE_TYPE (valist
),
4462 /* Expand EXP, a call to __builtin_va_start. */
4465 expand_builtin_va_start (tree exp
)
4469 location_t loc
= EXPR_LOCATION (exp
);
4471 if (call_expr_nargs (exp
) < 2)
4473 error_at (loc
, "too few arguments to function %<va_start%>");
4477 if (fold_builtin_next_arg (exp
, true))
4480 nextarg
= expand_builtin_next_arg ();
4481 valist
= stabilize_va_list_loc (loc
, CALL_EXPR_ARG (exp
, 0), 1);
4483 if (targetm
.expand_builtin_va_start
)
4484 targetm
.expand_builtin_va_start (valist
, nextarg
);
4486 std_expand_builtin_va_start (valist
, nextarg
);
4491 /* Expand EXP, a call to __builtin_va_end. */
4494 expand_builtin_va_end (tree exp
)
4496 tree valist
= CALL_EXPR_ARG (exp
, 0);
4498 /* Evaluate for side effects, if needed. I hate macros that don't
4500 if (TREE_SIDE_EFFECTS (valist
))
4501 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4506 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4507 builtin rather than just as an assignment in stdarg.h because of the
4508 nastiness of array-type va_list types. */
4511 expand_builtin_va_copy (tree exp
)
4514 location_t loc
= EXPR_LOCATION (exp
);
4516 dst
= CALL_EXPR_ARG (exp
, 0);
4517 src
= CALL_EXPR_ARG (exp
, 1);
4519 dst
= stabilize_va_list_loc (loc
, dst
, 1);
4520 src
= stabilize_va_list_loc (loc
, src
, 0);
4522 gcc_assert (cfun
!= NULL
&& cfun
->decl
!= NULL_TREE
);
4524 if (TREE_CODE (targetm
.fn_abi_va_list (cfun
->decl
)) != ARRAY_TYPE
)
4526 t
= build2 (MODIFY_EXPR
, targetm
.fn_abi_va_list (cfun
->decl
), dst
, src
);
4527 TREE_SIDE_EFFECTS (t
) = 1;
4528 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4532 rtx dstb
, srcb
, size
;
4534 /* Evaluate to pointers. */
4535 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4536 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4537 size
= expand_expr (TYPE_SIZE_UNIT (targetm
.fn_abi_va_list (cfun
->decl
)),
4538 NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
4540 dstb
= convert_memory_address (Pmode
, dstb
);
4541 srcb
= convert_memory_address (Pmode
, srcb
);
4543 /* "Dereference" to BLKmode memories. */
4544 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
4545 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
4546 set_mem_align (dstb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4547 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
4548 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
4549 set_mem_align (srcb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4552 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
4558 /* Expand a call to one of the builtin functions __builtin_frame_address or
4559 __builtin_return_address. */
4562 expand_builtin_frame_address (tree fndecl
, tree exp
)
4564 /* The argument must be a nonnegative integer constant.
4565 It counts the number of frames to scan up the stack.
4566 The value is the return address saved in that frame. */
4567 if (call_expr_nargs (exp
) == 0)
4568 /* Warning about missing arg was already issued. */
4570 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp
, 0)))
4572 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4573 error ("invalid argument to %<__builtin_frame_address%>");
4575 error ("invalid argument to %<__builtin_return_address%>");
4581 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
),
4582 tree_to_uhwi (CALL_EXPR_ARG (exp
, 0)));
4584 /* Some ports cannot access arbitrary stack frames. */
4587 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4588 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4590 warning (0, "unsupported argument to %<__builtin_return_address%>");
4594 /* For __builtin_frame_address, return what we've got. */
4595 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4599 && ! CONSTANT_P (tem
))
4600 tem
= copy_addr_to_reg (tem
);
4605 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4606 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4607 is the same as for allocate_dynamic_stack_space. */
4610 expand_builtin_alloca (tree exp
, bool cannot_accumulate
)
4616 bool alloca_with_align
= (DECL_FUNCTION_CODE (get_callee_fndecl (exp
))
4617 == BUILT_IN_ALLOCA_WITH_ALIGN
);
4620 = (alloca_with_align
4621 ? validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
4622 : validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
));
4627 /* Compute the argument. */
4628 op0
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4630 /* Compute the alignment. */
4631 align
= (alloca_with_align
4632 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp
, 1))
4633 : BIGGEST_ALIGNMENT
);
4635 /* Allocate the desired space. */
4636 result
= allocate_dynamic_stack_space (op0
, 0, align
, cannot_accumulate
);
4637 result
= convert_memory_address (ptr_mode
, result
);
4642 /* Expand a call to bswap builtin in EXP.
4643 Return NULL_RTX if a normal call should be emitted rather than expanding the
4644 function in-line. If convenient, the result should be placed in TARGET.
4645 SUBTARGET may be used as the target for computing one of EXP's operands. */
4648 expand_builtin_bswap (machine_mode target_mode
, tree exp
, rtx target
,
4654 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4657 arg
= CALL_EXPR_ARG (exp
, 0);
4658 op0
= expand_expr (arg
,
4659 subtarget
&& GET_MODE (subtarget
) == target_mode
4660 ? subtarget
: NULL_RTX
,
4661 target_mode
, EXPAND_NORMAL
);
4662 if (GET_MODE (op0
) != target_mode
)
4663 op0
= convert_to_mode (target_mode
, op0
, 1);
4665 target
= expand_unop (target_mode
, bswap_optab
, op0
, target
, 1);
4667 gcc_assert (target
);
4669 return convert_to_mode (target_mode
, target
, 1);
4672 /* Expand a call to a unary builtin in EXP.
4673 Return NULL_RTX if a normal call should be emitted rather than expanding the
4674 function in-line. If convenient, the result should be placed in TARGET.
4675 SUBTARGET may be used as the target for computing one of EXP's operands. */
4678 expand_builtin_unop (machine_mode target_mode
, tree exp
, rtx target
,
4679 rtx subtarget
, optab op_optab
)
4683 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4686 /* Compute the argument. */
4687 op0
= expand_expr (CALL_EXPR_ARG (exp
, 0),
4689 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0)))
4690 == GET_MODE (subtarget
))) ? subtarget
: NULL_RTX
,
4691 VOIDmode
, EXPAND_NORMAL
);
4692 /* Compute op, into TARGET if possible.
4693 Set TARGET to wherever the result comes back. */
4694 target
= expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))),
4695 op_optab
, op0
, target
, op_optab
!= clrsb_optab
);
4696 gcc_assert (target
);
4698 return convert_to_mode (target_mode
, target
, 0);
4701 /* Expand a call to __builtin_expect. We just return our argument
4702 as the builtin_expect semantic should've been already executed by
4703 tree branch prediction pass. */
4706 expand_builtin_expect (tree exp
, rtx target
)
4710 if (call_expr_nargs (exp
) < 2)
4712 arg
= CALL_EXPR_ARG (exp
, 0);
4714 target
= expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4715 /* When guessing was done, the hints should be already stripped away. */
4716 gcc_assert (!flag_guess_branch_prob
4717 || optimize
== 0 || seen_error ());
4721 /* Expand a call to __builtin_assume_aligned. We just return our first
4722 argument as the builtin_assume_aligned semantic should've been already
4726 expand_builtin_assume_aligned (tree exp
, rtx target
)
4728 if (call_expr_nargs (exp
) < 2)
4730 target
= expand_expr (CALL_EXPR_ARG (exp
, 0), target
, VOIDmode
,
4732 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 1))
4733 && (call_expr_nargs (exp
) < 3
4734 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 2))));
4739 expand_builtin_trap (void)
4744 rtx insn
= emit_insn (gen_trap ());
4745 /* For trap insns when not accumulating outgoing args force
4746 REG_ARGS_SIZE note to prevent crossjumping of calls with
4747 different args sizes. */
4748 if (!ACCUMULATE_OUTGOING_ARGS
)
4749 add_reg_note (insn
, REG_ARGS_SIZE
, GEN_INT (stack_pointer_delta
));
4753 emit_library_call (abort_libfunc
, LCT_NORETURN
, VOIDmode
, 0);
4757 /* Expand a call to __builtin_unreachable. We do nothing except emit
4758 a barrier saying that control flow will not pass here.
4760 It is the responsibility of the program being compiled to ensure
4761 that control flow does never reach __builtin_unreachable. */
4763 expand_builtin_unreachable (void)
4768 /* Expand EXP, a call to fabs, fabsf or fabsl.
4769 Return NULL_RTX if a normal call should be emitted rather than expanding
4770 the function inline. If convenient, the result should be placed
4771 in TARGET. SUBTARGET may be used as the target for computing
4775 expand_builtin_fabs (tree exp
, rtx target
, rtx subtarget
)
4781 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4784 arg
= CALL_EXPR_ARG (exp
, 0);
4785 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
4786 mode
= TYPE_MODE (TREE_TYPE (arg
));
4787 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4788 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
4791 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4792 Return NULL is a normal call should be emitted rather than expanding the
4793 function inline. If convenient, the result should be placed in TARGET.
4794 SUBTARGET may be used as the target for computing the operand. */
4797 expand_builtin_copysign (tree exp
, rtx target
, rtx subtarget
)
4802 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
4805 arg
= CALL_EXPR_ARG (exp
, 0);
4806 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4808 arg
= CALL_EXPR_ARG (exp
, 1);
4809 op1
= expand_normal (arg
);
4811 return expand_copysign (op0
, op1
, target
);
4814 /* Expand a call to __builtin___clear_cache. */
4817 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED
)
4819 #ifndef HAVE_clear_cache
4820 #ifdef CLEAR_INSN_CACHE
4821 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4822 does something. Just do the default expansion to a call to
4826 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4827 does nothing. There is no need to call it. Do nothing. */
4829 #endif /* CLEAR_INSN_CACHE */
4831 /* We have a "clear_cache" insn, and it will handle everything. */
4833 rtx begin_rtx
, end_rtx
;
4835 /* We must not expand to a library call. If we did, any
4836 fallback library function in libgcc that might contain a call to
4837 __builtin___clear_cache() would recurse infinitely. */
4838 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4840 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4844 if (HAVE_clear_cache
)
4846 struct expand_operand ops
[2];
4848 begin
= CALL_EXPR_ARG (exp
, 0);
4849 begin_rtx
= expand_expr (begin
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4851 end
= CALL_EXPR_ARG (exp
, 1);
4852 end_rtx
= expand_expr (end
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4854 create_address_operand (&ops
[0], begin_rtx
);
4855 create_address_operand (&ops
[1], end_rtx
);
4856 if (maybe_expand_insn (CODE_FOR_clear_cache
, 2, ops
))
4860 #endif /* HAVE_clear_cache */
4863 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4866 round_trampoline_addr (rtx tramp
)
4868 rtx temp
, addend
, mask
;
4870 /* If we don't need too much alignment, we'll have been guaranteed
4871 proper alignment by get_trampoline_type. */
4872 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
4875 /* Round address up to desired boundary. */
4876 temp
= gen_reg_rtx (Pmode
);
4877 addend
= gen_int_mode (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1, Pmode
);
4878 mask
= gen_int_mode (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
, Pmode
);
4880 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
4881 temp
, 0, OPTAB_LIB_WIDEN
);
4882 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
4883 temp
, 0, OPTAB_LIB_WIDEN
);
4889 expand_builtin_init_trampoline (tree exp
, bool onstack
)
4891 tree t_tramp
, t_func
, t_chain
;
4892 rtx m_tramp
, r_tramp
, r_chain
, tmp
;
4894 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
,
4895 POINTER_TYPE
, VOID_TYPE
))
4898 t_tramp
= CALL_EXPR_ARG (exp
, 0);
4899 t_func
= CALL_EXPR_ARG (exp
, 1);
4900 t_chain
= CALL_EXPR_ARG (exp
, 2);
4902 r_tramp
= expand_normal (t_tramp
);
4903 m_tramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
4904 MEM_NOTRAP_P (m_tramp
) = 1;
4906 /* If ONSTACK, the TRAMP argument should be the address of a field
4907 within the local function's FRAME decl. Either way, let's see if
4908 we can fill in the MEM_ATTRs for this memory. */
4909 if (TREE_CODE (t_tramp
) == ADDR_EXPR
)
4910 set_mem_attributes (m_tramp
, TREE_OPERAND (t_tramp
, 0), true);
4912 /* Creator of a heap trampoline is responsible for making sure the
4913 address is aligned to at least STACK_BOUNDARY. Normally malloc
4914 will ensure this anyhow. */
4915 tmp
= round_trampoline_addr (r_tramp
);
4918 m_tramp
= change_address (m_tramp
, BLKmode
, tmp
);
4919 set_mem_align (m_tramp
, TRAMPOLINE_ALIGNMENT
);
4920 set_mem_size (m_tramp
, TRAMPOLINE_SIZE
);
4923 /* The FUNC argument should be the address of the nested function.
4924 Extract the actual function decl to pass to the hook. */
4925 gcc_assert (TREE_CODE (t_func
) == ADDR_EXPR
);
4926 t_func
= TREE_OPERAND (t_func
, 0);
4927 gcc_assert (TREE_CODE (t_func
) == FUNCTION_DECL
);
4929 r_chain
= expand_normal (t_chain
);
4931 /* Generate insns to initialize the trampoline. */
4932 targetm
.calls
.trampoline_init (m_tramp
, t_func
, r_chain
);
4936 trampolines_created
= 1;
4938 warning_at (DECL_SOURCE_LOCATION (t_func
), OPT_Wtrampolines
,
4939 "trampoline generated for nested function %qD", t_func
);
4946 expand_builtin_adjust_trampoline (tree exp
)
4950 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
4953 tramp
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4954 tramp
= round_trampoline_addr (tramp
);
4955 if (targetm
.calls
.trampoline_adjust_address
)
4956 tramp
= targetm
.calls
.trampoline_adjust_address (tramp
);
4961 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4962 function. The function first checks whether the back end provides
4963 an insn to implement signbit for the respective mode. If not, it
4964 checks whether the floating point format of the value is such that
4965 the sign bit can be extracted. If that is not the case, the
4966 function returns NULL_RTX to indicate that a normal call should be
4967 emitted rather than expanding the function in-line. EXP is the
4968 expression that is a call to the builtin function; if convenient,
4969 the result should be placed in TARGET. */
4971 expand_builtin_signbit (tree exp
, rtx target
)
4973 const struct real_format
*fmt
;
4974 machine_mode fmode
, imode
, rmode
;
4977 enum insn_code icode
;
4979 location_t loc
= EXPR_LOCATION (exp
);
4981 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4984 arg
= CALL_EXPR_ARG (exp
, 0);
4985 fmode
= TYPE_MODE (TREE_TYPE (arg
));
4986 rmode
= TYPE_MODE (TREE_TYPE (exp
));
4987 fmt
= REAL_MODE_FORMAT (fmode
);
4989 arg
= builtin_save_expr (arg
);
4991 /* Expand the argument yielding a RTX expression. */
4992 temp
= expand_normal (arg
);
4994 /* Check if the back end provides an insn that handles signbit for the
4996 icode
= optab_handler (signbit_optab
, fmode
);
4997 if (icode
!= CODE_FOR_nothing
)
4999 rtx_insn
*last
= get_last_insn ();
5000 target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
5001 if (maybe_emit_unop_insn (icode
, target
, temp
, UNKNOWN
))
5003 delete_insns_since (last
);
5006 /* For floating point formats without a sign bit, implement signbit
5008 bitpos
= fmt
->signbit_ro
;
5011 /* But we can't do this if the format supports signed zero. */
5012 if (fmt
->has_signed_zero
&& HONOR_SIGNED_ZEROS (fmode
))
5015 arg
= fold_build2_loc (loc
, LT_EXPR
, TREE_TYPE (exp
), arg
,
5016 build_real (TREE_TYPE (arg
), dconst0
));
5017 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
5020 if (GET_MODE_SIZE (fmode
) <= UNITS_PER_WORD
)
5022 imode
= int_mode_for_mode (fmode
);
5023 if (imode
== BLKmode
)
5025 temp
= gen_lowpart (imode
, temp
);
5030 /* Handle targets with different FP word orders. */
5031 if (FLOAT_WORDS_BIG_ENDIAN
)
5032 word
= (GET_MODE_BITSIZE (fmode
) - bitpos
) / BITS_PER_WORD
;
5034 word
= bitpos
/ BITS_PER_WORD
;
5035 temp
= operand_subword_force (temp
, word
, fmode
);
5036 bitpos
= bitpos
% BITS_PER_WORD
;
5039 /* Force the intermediate word_mode (or narrower) result into a
5040 register. This avoids attempting to create paradoxical SUBREGs
5041 of floating point modes below. */
5042 temp
= force_reg (imode
, temp
);
5044 /* If the bitpos is within the "result mode" lowpart, the operation
5045 can be implement with a single bitwise AND. Otherwise, we need
5046 a right shift and an AND. */
5048 if (bitpos
< GET_MODE_BITSIZE (rmode
))
5050 wide_int mask
= wi::set_bit_in_zero (bitpos
, GET_MODE_PRECISION (rmode
));
5052 if (GET_MODE_SIZE (imode
) > GET_MODE_SIZE (rmode
))
5053 temp
= gen_lowpart (rmode
, temp
);
5054 temp
= expand_binop (rmode
, and_optab
, temp
,
5055 immed_wide_int_const (mask
, rmode
),
5056 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5060 /* Perform a logical right shift to place the signbit in the least
5061 significant bit, then truncate the result to the desired mode
5062 and mask just this bit. */
5063 temp
= expand_shift (RSHIFT_EXPR
, imode
, temp
, bitpos
, NULL_RTX
, 1);
5064 temp
= gen_lowpart (rmode
, temp
);
5065 temp
= expand_binop (rmode
, and_optab
, temp
, const1_rtx
,
5066 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5072 /* Expand fork or exec calls. TARGET is the desired target of the
5073 call. EXP is the call. FN is the
5074 identificator of the actual function. IGNORE is nonzero if the
5075 value is to be ignored. */
5078 expand_builtin_fork_or_exec (tree fn
, tree exp
, rtx target
, int ignore
)
5083 /* If we are not profiling, just call the function. */
5084 if (!profile_arc_flag
)
5087 /* Otherwise call the wrapper. This should be equivalent for the rest of
5088 compiler, so the code does not diverge, and the wrapper may run the
5089 code necessary for keeping the profiling sane. */
5091 switch (DECL_FUNCTION_CODE (fn
))
5094 id
= get_identifier ("__gcov_fork");
5097 case BUILT_IN_EXECL
:
5098 id
= get_identifier ("__gcov_execl");
5101 case BUILT_IN_EXECV
:
5102 id
= get_identifier ("__gcov_execv");
5105 case BUILT_IN_EXECLP
:
5106 id
= get_identifier ("__gcov_execlp");
5109 case BUILT_IN_EXECLE
:
5110 id
= get_identifier ("__gcov_execle");
5113 case BUILT_IN_EXECVP
:
5114 id
= get_identifier ("__gcov_execvp");
5117 case BUILT_IN_EXECVE
:
5118 id
= get_identifier ("__gcov_execve");
5125 decl
= build_decl (DECL_SOURCE_LOCATION (fn
),
5126 FUNCTION_DECL
, id
, TREE_TYPE (fn
));
5127 DECL_EXTERNAL (decl
) = 1;
5128 TREE_PUBLIC (decl
) = 1;
5129 DECL_ARTIFICIAL (decl
) = 1;
5130 TREE_NOTHROW (decl
) = 1;
5131 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
5132 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
5133 call
= rewrite_call_expr (EXPR_LOCATION (exp
), exp
, 0, decl
, 0);
5134 return expand_call (call
, target
, ignore
);
5139 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5140 the pointer in these functions is void*, the tree optimizers may remove
5141 casts. The mode computed in expand_builtin isn't reliable either, due
5142 to __sync_bool_compare_and_swap.
5144 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5145 group of builtins. This gives us log2 of the mode size. */
5147 static inline machine_mode
5148 get_builtin_sync_mode (int fcode_diff
)
5150 /* The size is not negotiable, so ask not to get BLKmode in return
5151 if the target indicates that a smaller size would be better. */
5152 return mode_for_size (BITS_PER_UNIT
<< fcode_diff
, MODE_INT
, 0);
5155 /* Expand the memory expression LOC and return the appropriate memory operand
5156 for the builtin_sync operations. */
5159 get_builtin_sync_mem (tree loc
, machine_mode mode
)
5163 addr
= expand_expr (loc
, NULL_RTX
, ptr_mode
, EXPAND_SUM
);
5164 addr
= convert_memory_address (Pmode
, addr
);
5166 /* Note that we explicitly do not want any alias information for this
5167 memory, so that we kill all other live memories. Otherwise we don't
5168 satisfy the full barrier semantics of the intrinsic. */
5169 mem
= validize_mem (gen_rtx_MEM (mode
, addr
));
5171 /* The alignment needs to be at least according to that of the mode. */
5172 set_mem_align (mem
, MAX (GET_MODE_ALIGNMENT (mode
),
5173 get_pointer_alignment (loc
)));
5174 set_mem_alias_set (mem
, ALIAS_SET_MEMORY_BARRIER
);
5175 MEM_VOLATILE_P (mem
) = 1;
5180 /* Make sure an argument is in the right mode.
5181 EXP is the tree argument.
5182 MODE is the mode it should be in. */
5185 expand_expr_force_mode (tree exp
, machine_mode mode
)
5188 machine_mode old_mode
;
5190 val
= expand_expr (exp
, NULL_RTX
, mode
, EXPAND_NORMAL
);
5191 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5192 of CONST_INTs, where we know the old_mode only from the call argument. */
5194 old_mode
= GET_MODE (val
);
5195 if (old_mode
== VOIDmode
)
5196 old_mode
= TYPE_MODE (TREE_TYPE (exp
));
5197 val
= convert_modes (mode
, old_mode
, val
, 1);
5202 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5203 EXP is the CALL_EXPR. CODE is the rtx code
5204 that corresponds to the arithmetic or logical operation from the name;
5205 an exception here is that NOT actually means NAND. TARGET is an optional
5206 place for us to store the results; AFTER is true if this is the
5207 fetch_and_xxx form. */
5210 expand_builtin_sync_operation (machine_mode mode
, tree exp
,
5211 enum rtx_code code
, bool after
,
5215 location_t loc
= EXPR_LOCATION (exp
);
5217 if (code
== NOT
&& warn_sync_nand
)
5219 tree fndecl
= get_callee_fndecl (exp
);
5220 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5222 static bool warned_f_a_n
, warned_n_a_f
;
5226 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
5227 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
5228 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
5229 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
5230 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
5234 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N
);
5235 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5236 warned_f_a_n
= true;
5239 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
5240 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
5241 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
5242 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
5243 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
5247 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N
);
5248 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5249 warned_n_a_f
= true;
5257 /* Expand the operands. */
5258 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5259 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5261 return expand_atomic_fetch_op (target
, mem
, val
, code
, MEMMODEL_SEQ_CST
,
5265 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5266 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5267 true if this is the boolean form. TARGET is a place for us to store the
5268 results; this is NOT optional if IS_BOOL is true. */
5271 expand_builtin_compare_and_swap (machine_mode mode
, tree exp
,
5272 bool is_bool
, rtx target
)
5274 rtx old_val
, new_val
, mem
;
5277 /* Expand the operands. */
5278 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5279 old_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5280 new_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5282 pbool
= poval
= NULL
;
5283 if (target
!= const0_rtx
)
5290 if (!expand_atomic_compare_and_swap (pbool
, poval
, mem
, old_val
, new_val
,
5291 false, MEMMODEL_SEQ_CST
,
5298 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5299 general form is actually an atomic exchange, and some targets only
5300 support a reduced form with the second argument being a constant 1.
5301 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5305 expand_builtin_sync_lock_test_and_set (machine_mode mode
, tree exp
,
5310 /* Expand the operands. */
5311 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5312 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5314 return expand_sync_lock_test_and_set (target
, mem
, val
);
5317 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5320 expand_builtin_sync_lock_release (machine_mode mode
, tree exp
)
5324 /* Expand the operands. */
5325 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5327 expand_atomic_store (mem
, const0_rtx
, MEMMODEL_RELEASE
, true);
5330 /* Given an integer representing an ``enum memmodel'', verify its
5331 correctness and return the memory model enum. */
5333 static enum memmodel
5334 get_memmodel (tree exp
)
5337 unsigned HOST_WIDE_INT val
;
5339 /* If the parameter is not a constant, it's a run time value so we'll just
5340 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5341 if (TREE_CODE (exp
) != INTEGER_CST
)
5342 return MEMMODEL_SEQ_CST
;
5344 op
= expand_normal (exp
);
5347 if (targetm
.memmodel_check
)
5348 val
= targetm
.memmodel_check (val
);
5349 else if (val
& ~MEMMODEL_MASK
)
5351 warning (OPT_Winvalid_memory_model
,
5352 "Unknown architecture specifier in memory model to builtin.");
5353 return MEMMODEL_SEQ_CST
;
5356 if ((INTVAL (op
) & MEMMODEL_MASK
) >= MEMMODEL_LAST
)
5358 warning (OPT_Winvalid_memory_model
,
5359 "invalid memory model argument to builtin");
5360 return MEMMODEL_SEQ_CST
;
5363 return (enum memmodel
) val
;
5366 /* Expand the __atomic_exchange intrinsic:
5367 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5368 EXP is the CALL_EXPR.
5369 TARGET is an optional place for us to store the results. */
5372 expand_builtin_atomic_exchange (machine_mode mode
, tree exp
, rtx target
)
5375 enum memmodel model
;
5377 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5378 if ((model
& MEMMODEL_MASK
) == MEMMODEL_CONSUME
)
5380 error ("invalid memory model for %<__atomic_exchange%>");
5384 if (!flag_inline_atomics
)
5387 /* Expand the operands. */
5388 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5389 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5391 return expand_atomic_exchange (target
, mem
, val
, model
);
5394 /* Expand the __atomic_compare_exchange intrinsic:
5395 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5396 TYPE desired, BOOL weak,
5397 enum memmodel success,
5398 enum memmodel failure)
5399 EXP is the CALL_EXPR.
5400 TARGET is an optional place for us to store the results. */
5403 expand_builtin_atomic_compare_exchange (machine_mode mode
, tree exp
,
5406 rtx expect
, desired
, mem
, oldval
;
5407 rtx_code_label
*label
;
5408 enum memmodel success
, failure
;
5412 success
= get_memmodel (CALL_EXPR_ARG (exp
, 4));
5413 failure
= get_memmodel (CALL_EXPR_ARG (exp
, 5));
5415 if ((failure
& MEMMODEL_MASK
) == MEMMODEL_RELEASE
5416 || (failure
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5418 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5422 if (failure
> success
)
5424 error ("failure memory model cannot be stronger than success "
5425 "memory model for %<__atomic_compare_exchange%>");
5429 if (!flag_inline_atomics
)
5432 /* Expand the operands. */
5433 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5435 expect
= expand_normal (CALL_EXPR_ARG (exp
, 1));
5436 expect
= convert_memory_address (Pmode
, expect
);
5437 expect
= gen_rtx_MEM (mode
, expect
);
5438 desired
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5440 weak
= CALL_EXPR_ARG (exp
, 3);
5442 if (tree_fits_shwi_p (weak
) && tree_to_shwi (weak
) != 0)
5445 if (target
== const0_rtx
)
5448 /* Lest the rtl backend create a race condition with an imporoper store
5449 to memory, always create a new pseudo for OLDVAL. */
5452 if (!expand_atomic_compare_and_swap (&target
, &oldval
, mem
, expect
, desired
,
5453 is_weak
, success
, failure
))
5456 /* Conditionally store back to EXPECT, lest we create a race condition
5457 with an improper store to memory. */
5458 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5459 the normal case where EXPECT is totally private, i.e. a register. At
5460 which point the store can be unconditional. */
5461 label
= gen_label_rtx ();
5462 emit_cmp_and_jump_insns (target
, const0_rtx
, NE
, NULL
, VOIDmode
, 1, label
);
5463 emit_move_insn (expect
, oldval
);
5469 /* Expand the __atomic_load intrinsic:
5470 TYPE __atomic_load (TYPE *object, enum memmodel)
5471 EXP is the CALL_EXPR.
5472 TARGET is an optional place for us to store the results. */
5475 expand_builtin_atomic_load (machine_mode mode
, tree exp
, rtx target
)
5478 enum memmodel model
;
5480 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5481 if ((model
& MEMMODEL_MASK
) == MEMMODEL_RELEASE
5482 || (model
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5484 error ("invalid memory model for %<__atomic_load%>");
5488 if (!flag_inline_atomics
)
5491 /* Expand the operand. */
5492 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5494 return expand_atomic_load (target
, mem
, model
);
5498 /* Expand the __atomic_store intrinsic:
5499 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5500 EXP is the CALL_EXPR.
5501 TARGET is an optional place for us to store the results. */
5504 expand_builtin_atomic_store (machine_mode mode
, tree exp
)
5507 enum memmodel model
;
5509 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5510 if ((model
& MEMMODEL_MASK
) != MEMMODEL_RELAXED
5511 && (model
& MEMMODEL_MASK
) != MEMMODEL_SEQ_CST
5512 && (model
& MEMMODEL_MASK
) != MEMMODEL_RELEASE
)
5514 error ("invalid memory model for %<__atomic_store%>");
5518 if (!flag_inline_atomics
)
5521 /* Expand the operands. */
5522 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5523 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5525 return expand_atomic_store (mem
, val
, model
, false);
5528 /* Expand the __atomic_fetch_XXX intrinsic:
5529 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5530 EXP is the CALL_EXPR.
5531 TARGET is an optional place for us to store the results.
5532 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5533 FETCH_AFTER is true if returning the result of the operation.
5534 FETCH_AFTER is false if returning the value before the operation.
5535 IGNORE is true if the result is not used.
5536 EXT_CALL is the correct builtin for an external call if this cannot be
5537 resolved to an instruction sequence. */
5540 expand_builtin_atomic_fetch_op (machine_mode mode
, tree exp
, rtx target
,
5541 enum rtx_code code
, bool fetch_after
,
5542 bool ignore
, enum built_in_function ext_call
)
5545 enum memmodel model
;
5549 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5551 /* Expand the operands. */
5552 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5553 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5555 /* Only try generating instructions if inlining is turned on. */
5556 if (flag_inline_atomics
)
5558 ret
= expand_atomic_fetch_op (target
, mem
, val
, code
, model
, fetch_after
);
5563 /* Return if a different routine isn't needed for the library call. */
5564 if (ext_call
== BUILT_IN_NONE
)
5567 /* Change the call to the specified function. */
5568 fndecl
= get_callee_fndecl (exp
);
5569 addr
= CALL_EXPR_FN (exp
);
5572 gcc_assert (TREE_OPERAND (addr
, 0) == fndecl
);
5573 TREE_OPERAND (addr
, 0) = builtin_decl_explicit (ext_call
);
5575 /* Expand the call here so we can emit trailing code. */
5576 ret
= expand_call (exp
, target
, ignore
);
5578 /* Replace the original function just in case it matters. */
5579 TREE_OPERAND (addr
, 0) = fndecl
;
5581 /* Then issue the arithmetic correction to return the right result. */
5586 ret
= expand_simple_binop (mode
, AND
, ret
, val
, NULL_RTX
, true,
5588 ret
= expand_simple_unop (mode
, NOT
, ret
, target
, true);
5591 ret
= expand_simple_binop (mode
, code
, ret
, val
, target
, true,
5598 #ifndef HAVE_atomic_clear
5599 # define HAVE_atomic_clear 0
5600 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5603 /* Expand an atomic clear operation.
5604 void _atomic_clear (BOOL *obj, enum memmodel)
5605 EXP is the call expression. */
5608 expand_builtin_atomic_clear (tree exp
)
5612 enum memmodel model
;
5614 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5615 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5616 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5618 if ((model
& MEMMODEL_MASK
) == MEMMODEL_ACQUIRE
5619 || (model
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5621 error ("invalid memory model for %<__atomic_store%>");
5625 if (HAVE_atomic_clear
)
5627 emit_insn (gen_atomic_clear (mem
, model
));
5631 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5632 Failing that, a store is issued by __atomic_store. The only way this can
5633 fail is if the bool type is larger than a word size. Unlikely, but
5634 handle it anyway for completeness. Assume a single threaded model since
5635 there is no atomic support in this case, and no barriers are required. */
5636 ret
= expand_atomic_store (mem
, const0_rtx
, model
, true);
5638 emit_move_insn (mem
, const0_rtx
);
5642 /* Expand an atomic test_and_set operation.
5643 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5644 EXP is the call expression. */
5647 expand_builtin_atomic_test_and_set (tree exp
, rtx target
)
5650 enum memmodel model
;
5653 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5654 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5655 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5657 return expand_atomic_test_and_set (target
, mem
, model
);
5661 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5662 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5665 fold_builtin_atomic_always_lock_free (tree arg0
, tree arg1
)
5669 unsigned int mode_align
, type_align
;
5671 if (TREE_CODE (arg0
) != INTEGER_CST
)
5674 size
= INTVAL (expand_normal (arg0
)) * BITS_PER_UNIT
;
5675 mode
= mode_for_size (size
, MODE_INT
, 0);
5676 mode_align
= GET_MODE_ALIGNMENT (mode
);
5678 if (TREE_CODE (arg1
) == INTEGER_CST
&& INTVAL (expand_normal (arg1
)) == 0)
5679 type_align
= mode_align
;
5682 tree ttype
= TREE_TYPE (arg1
);
5684 /* This function is usually invoked and folded immediately by the front
5685 end before anything else has a chance to look at it. The pointer
5686 parameter at this point is usually cast to a void *, so check for that
5687 and look past the cast. */
5688 if (CONVERT_EXPR_P (arg1
) && POINTER_TYPE_P (ttype
)
5689 && VOID_TYPE_P (TREE_TYPE (ttype
)))
5690 arg1
= TREE_OPERAND (arg1
, 0);
5692 ttype
= TREE_TYPE (arg1
);
5693 gcc_assert (POINTER_TYPE_P (ttype
));
5695 /* Get the underlying type of the object. */
5696 ttype
= TREE_TYPE (ttype
);
5697 type_align
= TYPE_ALIGN (ttype
);
5700 /* If the object has smaller alignment, the the lock free routines cannot
5702 if (type_align
< mode_align
)
5703 return boolean_false_node
;
5705 /* Check if a compare_and_swap pattern exists for the mode which represents
5706 the required size. The pattern is not allowed to fail, so the existence
5707 of the pattern indicates support is present. */
5708 if (can_compare_and_swap_p (mode
, true))
5709 return boolean_true_node
;
5711 return boolean_false_node
;
5714 /* Return true if the parameters to call EXP represent an object which will
5715 always generate lock free instructions. The first argument represents the
5716 size of the object, and the second parameter is a pointer to the object
5717 itself. If NULL is passed for the object, then the result is based on
5718 typical alignment for an object of the specified size. Otherwise return
5722 expand_builtin_atomic_always_lock_free (tree exp
)
5725 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5726 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5728 if (TREE_CODE (arg0
) != INTEGER_CST
)
5730 error ("non-constant argument 1 to __atomic_always_lock_free");
5734 size
= fold_builtin_atomic_always_lock_free (arg0
, arg1
);
5735 if (size
== boolean_true_node
)
5740 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5741 is lock free on this architecture. */
5744 fold_builtin_atomic_is_lock_free (tree arg0
, tree arg1
)
5746 if (!flag_inline_atomics
)
5749 /* If it isn't always lock free, don't generate a result. */
5750 if (fold_builtin_atomic_always_lock_free (arg0
, arg1
) == boolean_true_node
)
5751 return boolean_true_node
;
5756 /* Return true if the parameters to call EXP represent an object which will
5757 always generate lock free instructions. The first argument represents the
5758 size of the object, and the second parameter is a pointer to the object
5759 itself. If NULL is passed for the object, then the result is based on
5760 typical alignment for an object of the specified size. Otherwise return
5764 expand_builtin_atomic_is_lock_free (tree exp
)
5767 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5768 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5770 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
5772 error ("non-integer argument 1 to __atomic_is_lock_free");
5776 if (!flag_inline_atomics
)
5779 /* If the value is known at compile time, return the RTX for it. */
5780 size
= fold_builtin_atomic_is_lock_free (arg0
, arg1
);
5781 if (size
== boolean_true_node
)
5787 /* Expand the __atomic_thread_fence intrinsic:
5788 void __atomic_thread_fence (enum memmodel)
5789 EXP is the CALL_EXPR. */
5792 expand_builtin_atomic_thread_fence (tree exp
)
5794 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5795 expand_mem_thread_fence (model
);
5798 /* Expand the __atomic_signal_fence intrinsic:
5799 void __atomic_signal_fence (enum memmodel)
5800 EXP is the CALL_EXPR. */
5803 expand_builtin_atomic_signal_fence (tree exp
)
5805 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5806 expand_mem_signal_fence (model
);
5809 /* Expand the __sync_synchronize intrinsic. */
5812 expand_builtin_sync_synchronize (void)
5814 expand_mem_thread_fence (MEMMODEL_SEQ_CST
);
5818 expand_builtin_thread_pointer (tree exp
, rtx target
)
5820 enum insn_code icode
;
5821 if (!validate_arglist (exp
, VOID_TYPE
))
5823 icode
= direct_optab_handler (get_thread_pointer_optab
, Pmode
);
5824 if (icode
!= CODE_FOR_nothing
)
5826 struct expand_operand op
;
5827 /* If the target is not sutitable then create a new target. */
5828 if (target
== NULL_RTX
5830 || GET_MODE (target
) != Pmode
)
5831 target
= gen_reg_rtx (Pmode
);
5832 create_output_operand (&op
, target
, Pmode
);
5833 expand_insn (icode
, 1, &op
);
5836 error ("__builtin_thread_pointer is not supported on this target");
5841 expand_builtin_set_thread_pointer (tree exp
)
5843 enum insn_code icode
;
5844 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
5846 icode
= direct_optab_handler (set_thread_pointer_optab
, Pmode
);
5847 if (icode
!= CODE_FOR_nothing
)
5849 struct expand_operand op
;
5850 rtx val
= expand_expr (CALL_EXPR_ARG (exp
, 0), NULL_RTX
,
5851 Pmode
, EXPAND_NORMAL
);
5852 create_input_operand (&op
, val
, Pmode
);
5853 expand_insn (icode
, 1, &op
);
5856 error ("__builtin_set_thread_pointer is not supported on this target");
5860 /* Emit code to restore the current value of stack. */
5863 expand_stack_restore (tree var
)
5866 rtx sa
= expand_normal (var
);
5868 sa
= convert_memory_address (Pmode
, sa
);
5870 prev
= get_last_insn ();
5871 emit_stack_restore (SAVE_BLOCK
, sa
);
5872 fixup_args_size_notes (prev
, get_last_insn (), 0);
5876 /* Emit code to save the current value of stack. */
5879 expand_stack_save (void)
5883 do_pending_stack_adjust ();
5884 emit_stack_save (SAVE_BLOCK
, &ret
);
5888 /* Expand an expression EXP that calls a built-in function,
5889 with result going to TARGET if that's convenient
5890 (and in mode MODE if that's convenient).
5891 SUBTARGET may be used as the target for computing one of EXP's operands.
5892 IGNORE is nonzero if the value is to be ignored. */
5895 expand_builtin (tree exp
, rtx target
, rtx subtarget
, machine_mode mode
,
5898 tree fndecl
= get_callee_fndecl (exp
);
5899 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5900 machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
5903 /* When ASan is enabled, we don't want to expand some memory/string
5904 builtins and rely on libsanitizer's hooks. This allows us to avoid
5905 redundant checks and be sure, that possible overflow will be detected
5908 if ((flag_sanitize
& SANITIZE_ADDRESS
) && asan_intercepted_p (fcode
))
5909 return expand_call (exp
, target
, ignore
);
5911 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5912 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
5914 /* When not optimizing, generate calls to library functions for a certain
5917 && !called_as_built_in (fndecl
)
5918 && fcode
!= BUILT_IN_FORK
5919 && fcode
!= BUILT_IN_EXECL
5920 && fcode
!= BUILT_IN_EXECV
5921 && fcode
!= BUILT_IN_EXECLP
5922 && fcode
!= BUILT_IN_EXECLE
5923 && fcode
!= BUILT_IN_EXECVP
5924 && fcode
!= BUILT_IN_EXECVE
5925 && fcode
!= BUILT_IN_ALLOCA
5926 && fcode
!= BUILT_IN_ALLOCA_WITH_ALIGN
5927 && fcode
!= BUILT_IN_FREE
5928 && fcode
!= BUILT_IN_CHKP_SET_PTR_BOUNDS
5929 && fcode
!= BUILT_IN_CHKP_INIT_PTR_BOUNDS
5930 && fcode
!= BUILT_IN_CHKP_NULL_PTR_BOUNDS
5931 && fcode
!= BUILT_IN_CHKP_COPY_PTR_BOUNDS
5932 && fcode
!= BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5933 && fcode
!= BUILT_IN_CHKP_STORE_PTR_BOUNDS
5934 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
5935 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
5936 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_BOUNDS
5937 && fcode
!= BUILT_IN_CHKP_GET_PTR_LBOUND
5938 && fcode
!= BUILT_IN_CHKP_GET_PTR_UBOUND
5939 && fcode
!= BUILT_IN_CHKP_BNDRET
)
5940 return expand_call (exp
, target
, ignore
);
5942 /* The built-in function expanders test for target == const0_rtx
5943 to determine whether the function's result will be ignored. */
5945 target
= const0_rtx
;
5947 /* If the result of a pure or const built-in function is ignored, and
5948 none of its arguments are volatile, we can avoid expanding the
5949 built-in call and just evaluate the arguments for side-effects. */
5950 if (target
== const0_rtx
5951 && ((flags
= flags_from_decl_or_type (fndecl
)) & (ECF_CONST
| ECF_PURE
))
5952 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
5954 bool volatilep
= false;
5956 call_expr_arg_iterator iter
;
5958 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5959 if (TREE_THIS_VOLATILE (arg
))
5967 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5968 expand_expr (arg
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5973 /* expand_builtin_with_bounds is supposed to be used for
5974 instrumented builtin calls. */
5975 gcc_assert (!CALL_WITH_BOUNDS_P (exp
));
5979 CASE_FLT_FN (BUILT_IN_FABS
):
5980 case BUILT_IN_FABSD32
:
5981 case BUILT_IN_FABSD64
:
5982 case BUILT_IN_FABSD128
:
5983 target
= expand_builtin_fabs (exp
, target
, subtarget
);
5988 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
5989 target
= expand_builtin_copysign (exp
, target
, subtarget
);
5994 /* Just do a normal library call if we were unable to fold
5996 CASE_FLT_FN (BUILT_IN_CABS
):
5999 CASE_FLT_FN (BUILT_IN_EXP
):
6000 CASE_FLT_FN (BUILT_IN_EXP10
):
6001 CASE_FLT_FN (BUILT_IN_POW10
):
6002 CASE_FLT_FN (BUILT_IN_EXP2
):
6003 CASE_FLT_FN (BUILT_IN_EXPM1
):
6004 CASE_FLT_FN (BUILT_IN_LOGB
):
6005 CASE_FLT_FN (BUILT_IN_LOG
):
6006 CASE_FLT_FN (BUILT_IN_LOG10
):
6007 CASE_FLT_FN (BUILT_IN_LOG2
):
6008 CASE_FLT_FN (BUILT_IN_LOG1P
):
6009 CASE_FLT_FN (BUILT_IN_TAN
):
6010 CASE_FLT_FN (BUILT_IN_ASIN
):
6011 CASE_FLT_FN (BUILT_IN_ACOS
):
6012 CASE_FLT_FN (BUILT_IN_ATAN
):
6013 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
6014 /* Treat these like sqrt only if unsafe math optimizations are allowed,
6015 because of possible accuracy problems. */
6016 if (! flag_unsafe_math_optimizations
)
6018 CASE_FLT_FN (BUILT_IN_SQRT
):
6019 CASE_FLT_FN (BUILT_IN_FLOOR
):
6020 CASE_FLT_FN (BUILT_IN_CEIL
):
6021 CASE_FLT_FN (BUILT_IN_TRUNC
):
6022 CASE_FLT_FN (BUILT_IN_ROUND
):
6023 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
6024 CASE_FLT_FN (BUILT_IN_RINT
):
6025 target
= expand_builtin_mathfn (exp
, target
, subtarget
);
6030 CASE_FLT_FN (BUILT_IN_FMA
):
6031 target
= expand_builtin_mathfn_ternary (exp
, target
, subtarget
);
6036 CASE_FLT_FN (BUILT_IN_ILOGB
):
6037 if (! flag_unsafe_math_optimizations
)
6039 CASE_FLT_FN (BUILT_IN_ISINF
):
6040 CASE_FLT_FN (BUILT_IN_FINITE
):
6041 case BUILT_IN_ISFINITE
:
6042 case BUILT_IN_ISNORMAL
:
6043 target
= expand_builtin_interclass_mathfn (exp
, target
);
6048 CASE_FLT_FN (BUILT_IN_ICEIL
):
6049 CASE_FLT_FN (BUILT_IN_LCEIL
):
6050 CASE_FLT_FN (BUILT_IN_LLCEIL
):
6051 CASE_FLT_FN (BUILT_IN_LFLOOR
):
6052 CASE_FLT_FN (BUILT_IN_IFLOOR
):
6053 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
6054 target
= expand_builtin_int_roundingfn (exp
, target
);
6059 CASE_FLT_FN (BUILT_IN_IRINT
):
6060 CASE_FLT_FN (BUILT_IN_LRINT
):
6061 CASE_FLT_FN (BUILT_IN_LLRINT
):
6062 CASE_FLT_FN (BUILT_IN_IROUND
):
6063 CASE_FLT_FN (BUILT_IN_LROUND
):
6064 CASE_FLT_FN (BUILT_IN_LLROUND
):
6065 target
= expand_builtin_int_roundingfn_2 (exp
, target
);
6070 CASE_FLT_FN (BUILT_IN_POWI
):
6071 target
= expand_builtin_powi (exp
, target
);
6076 CASE_FLT_FN (BUILT_IN_ATAN2
):
6077 CASE_FLT_FN (BUILT_IN_LDEXP
):
6078 CASE_FLT_FN (BUILT_IN_SCALB
):
6079 CASE_FLT_FN (BUILT_IN_SCALBN
):
6080 CASE_FLT_FN (BUILT_IN_SCALBLN
):
6081 if (! flag_unsafe_math_optimizations
)
6084 CASE_FLT_FN (BUILT_IN_FMOD
):
6085 CASE_FLT_FN (BUILT_IN_REMAINDER
):
6086 CASE_FLT_FN (BUILT_IN_DREM
):
6087 CASE_FLT_FN (BUILT_IN_POW
):
6088 target
= expand_builtin_mathfn_2 (exp
, target
, subtarget
);
6093 CASE_FLT_FN (BUILT_IN_CEXPI
):
6094 target
= expand_builtin_cexpi (exp
, target
);
6095 gcc_assert (target
);
6098 CASE_FLT_FN (BUILT_IN_SIN
):
6099 CASE_FLT_FN (BUILT_IN_COS
):
6100 if (! flag_unsafe_math_optimizations
)
6102 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
6107 CASE_FLT_FN (BUILT_IN_SINCOS
):
6108 if (! flag_unsafe_math_optimizations
)
6110 target
= expand_builtin_sincos (exp
);
6115 case BUILT_IN_APPLY_ARGS
:
6116 return expand_builtin_apply_args ();
6118 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6119 FUNCTION with a copy of the parameters described by
6120 ARGUMENTS, and ARGSIZE. It returns a block of memory
6121 allocated on the stack into which is stored all the registers
6122 that might possibly be used for returning the result of a
6123 function. ARGUMENTS is the value returned by
6124 __builtin_apply_args. ARGSIZE is the number of bytes of
6125 arguments that must be copied. ??? How should this value be
6126 computed? We'll also need a safe worst case value for varargs
6128 case BUILT_IN_APPLY
:
6129 if (!validate_arglist (exp
, POINTER_TYPE
,
6130 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
6131 && !validate_arglist (exp
, REFERENCE_TYPE
,
6132 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6138 ops
[0] = expand_normal (CALL_EXPR_ARG (exp
, 0));
6139 ops
[1] = expand_normal (CALL_EXPR_ARG (exp
, 1));
6140 ops
[2] = expand_normal (CALL_EXPR_ARG (exp
, 2));
6142 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
6145 /* __builtin_return (RESULT) causes the function to return the
6146 value described by RESULT. RESULT is address of the block of
6147 memory returned by __builtin_apply. */
6148 case BUILT_IN_RETURN
:
6149 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6150 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp
, 0)));
6153 case BUILT_IN_SAVEREGS
:
6154 return expand_builtin_saveregs ();
6156 case BUILT_IN_VA_ARG_PACK
:
6157 /* All valid uses of __builtin_va_arg_pack () are removed during
6159 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp
);
6162 case BUILT_IN_VA_ARG_PACK_LEN
:
6163 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6165 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp
);
6168 /* Return the address of the first anonymous stack arg. */
6169 case BUILT_IN_NEXT_ARG
:
6170 if (fold_builtin_next_arg (exp
, false))
6172 return expand_builtin_next_arg ();
6174 case BUILT_IN_CLEAR_CACHE
:
6175 target
= expand_builtin___clear_cache (exp
);
6180 case BUILT_IN_CLASSIFY_TYPE
:
6181 return expand_builtin_classify_type (exp
);
6183 case BUILT_IN_CONSTANT_P
:
6186 case BUILT_IN_FRAME_ADDRESS
:
6187 case BUILT_IN_RETURN_ADDRESS
:
6188 return expand_builtin_frame_address (fndecl
, exp
);
6190 /* Returns the address of the area where the structure is returned.
6192 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
6193 if (call_expr_nargs (exp
) != 0
6194 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
6195 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
6198 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
6200 case BUILT_IN_ALLOCA
:
6201 case BUILT_IN_ALLOCA_WITH_ALIGN
:
6202 /* If the allocation stems from the declaration of a variable-sized
6203 object, it cannot accumulate. */
6204 target
= expand_builtin_alloca (exp
, CALL_ALLOCA_FOR_VAR_P (exp
));
6209 case BUILT_IN_STACK_SAVE
:
6210 return expand_stack_save ();
6212 case BUILT_IN_STACK_RESTORE
:
6213 expand_stack_restore (CALL_EXPR_ARG (exp
, 0));
6216 case BUILT_IN_BSWAP16
:
6217 case BUILT_IN_BSWAP32
:
6218 case BUILT_IN_BSWAP64
:
6219 target
= expand_builtin_bswap (target_mode
, exp
, target
, subtarget
);
6224 CASE_INT_FN (BUILT_IN_FFS
):
6225 target
= expand_builtin_unop (target_mode
, exp
, target
,
6226 subtarget
, ffs_optab
);
6231 CASE_INT_FN (BUILT_IN_CLZ
):
6232 target
= expand_builtin_unop (target_mode
, exp
, target
,
6233 subtarget
, clz_optab
);
6238 CASE_INT_FN (BUILT_IN_CTZ
):
6239 target
= expand_builtin_unop (target_mode
, exp
, target
,
6240 subtarget
, ctz_optab
);
6245 CASE_INT_FN (BUILT_IN_CLRSB
):
6246 target
= expand_builtin_unop (target_mode
, exp
, target
,
6247 subtarget
, clrsb_optab
);
6252 CASE_INT_FN (BUILT_IN_POPCOUNT
):
6253 target
= expand_builtin_unop (target_mode
, exp
, target
,
6254 subtarget
, popcount_optab
);
6259 CASE_INT_FN (BUILT_IN_PARITY
):
6260 target
= expand_builtin_unop (target_mode
, exp
, target
,
6261 subtarget
, parity_optab
);
6266 case BUILT_IN_STRLEN
:
6267 target
= expand_builtin_strlen (exp
, target
, target_mode
);
6272 case BUILT_IN_STRCPY
:
6273 target
= expand_builtin_strcpy (exp
, target
);
6278 case BUILT_IN_STRNCPY
:
6279 target
= expand_builtin_strncpy (exp
, target
);
6284 case BUILT_IN_STPCPY
:
6285 target
= expand_builtin_stpcpy (exp
, target
, mode
);
6290 case BUILT_IN_MEMCPY
:
6291 target
= expand_builtin_memcpy (exp
, target
);
6296 case BUILT_IN_MEMPCPY
:
6297 target
= expand_builtin_mempcpy (exp
, target
, mode
);
6302 case BUILT_IN_MEMSET
:
6303 target
= expand_builtin_memset (exp
, target
, mode
);
6308 case BUILT_IN_BZERO
:
6309 target
= expand_builtin_bzero (exp
);
6314 case BUILT_IN_STRCMP
:
6315 target
= expand_builtin_strcmp (exp
, target
);
6320 case BUILT_IN_STRNCMP
:
6321 target
= expand_builtin_strncmp (exp
, target
, mode
);
6327 case BUILT_IN_MEMCMP
:
6328 target
= expand_builtin_memcmp (exp
, target
, mode
);
6333 case BUILT_IN_SETJMP
:
6334 /* This should have been lowered to the builtins below. */
6337 case BUILT_IN_SETJMP_SETUP
:
6338 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6339 and the receiver label. */
6340 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
6342 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6343 VOIDmode
, EXPAND_NORMAL
);
6344 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 1), 0);
6345 rtx label_r
= label_rtx (label
);
6347 /* This is copied from the handling of non-local gotos. */
6348 expand_builtin_setjmp_setup (buf_addr
, label_r
);
6349 nonlocal_goto_handler_labels
6350 = gen_rtx_INSN_LIST (VOIDmode
, label_r
,
6351 nonlocal_goto_handler_labels
);
6352 /* ??? Do not let expand_label treat us as such since we would
6353 not want to be both on the list of non-local labels and on
6354 the list of forced labels. */
6355 FORCED_LABEL (label
) = 0;
6360 case BUILT_IN_SETJMP_RECEIVER
:
6361 /* __builtin_setjmp_receiver is passed the receiver label. */
6362 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6364 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 0), 0);
6365 rtx label_r
= label_rtx (label
);
6367 expand_builtin_setjmp_receiver (label_r
);
6372 /* __builtin_longjmp is passed a pointer to an array of five words.
6373 It's similar to the C library longjmp function but works with
6374 __builtin_setjmp above. */
6375 case BUILT_IN_LONGJMP
:
6376 if (validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6378 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6379 VOIDmode
, EXPAND_NORMAL
);
6380 rtx value
= expand_normal (CALL_EXPR_ARG (exp
, 1));
6382 if (value
!= const1_rtx
)
6384 error ("%<__builtin_longjmp%> second argument must be 1");
6388 expand_builtin_longjmp (buf_addr
, value
);
6393 case BUILT_IN_NONLOCAL_GOTO
:
6394 target
= expand_builtin_nonlocal_goto (exp
);
6399 /* This updates the setjmp buffer that is its argument with the value
6400 of the current stack pointer. */
6401 case BUILT_IN_UPDATE_SETJMP_BUF
:
6402 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6405 = expand_normal (CALL_EXPR_ARG (exp
, 0));
6407 expand_builtin_update_setjmp_buf (buf_addr
);
6413 expand_builtin_trap ();
6416 case BUILT_IN_UNREACHABLE
:
6417 expand_builtin_unreachable ();
6420 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
6421 case BUILT_IN_SIGNBITD32
:
6422 case BUILT_IN_SIGNBITD64
:
6423 case BUILT_IN_SIGNBITD128
:
6424 target
= expand_builtin_signbit (exp
, target
);
6429 /* Various hooks for the DWARF 2 __throw routine. */
6430 case BUILT_IN_UNWIND_INIT
:
6431 expand_builtin_unwind_init ();
6433 case BUILT_IN_DWARF_CFA
:
6434 return virtual_cfa_rtx
;
6435 #ifdef DWARF2_UNWIND_INFO
6436 case BUILT_IN_DWARF_SP_COLUMN
:
6437 return expand_builtin_dwarf_sp_column ();
6438 case BUILT_IN_INIT_DWARF_REG_SIZES
:
6439 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp
, 0));
6442 case BUILT_IN_FROB_RETURN_ADDR
:
6443 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp
, 0));
6444 case BUILT_IN_EXTRACT_RETURN_ADDR
:
6445 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp
, 0));
6446 case BUILT_IN_EH_RETURN
:
6447 expand_builtin_eh_return (CALL_EXPR_ARG (exp
, 0),
6448 CALL_EXPR_ARG (exp
, 1));
6450 #ifdef EH_RETURN_DATA_REGNO
6451 case BUILT_IN_EH_RETURN_DATA_REGNO
:
6452 return expand_builtin_eh_return_data_regno (exp
);
6454 case BUILT_IN_EXTEND_POINTER
:
6455 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp
, 0));
6456 case BUILT_IN_EH_POINTER
:
6457 return expand_builtin_eh_pointer (exp
);
6458 case BUILT_IN_EH_FILTER
:
6459 return expand_builtin_eh_filter (exp
);
6460 case BUILT_IN_EH_COPY_VALUES
:
6461 return expand_builtin_eh_copy_values (exp
);
6463 case BUILT_IN_VA_START
:
6464 return expand_builtin_va_start (exp
);
6465 case BUILT_IN_VA_END
:
6466 return expand_builtin_va_end (exp
);
6467 case BUILT_IN_VA_COPY
:
6468 return expand_builtin_va_copy (exp
);
6469 case BUILT_IN_EXPECT
:
6470 return expand_builtin_expect (exp
, target
);
6471 case BUILT_IN_ASSUME_ALIGNED
:
6472 return expand_builtin_assume_aligned (exp
, target
);
6473 case BUILT_IN_PREFETCH
:
6474 expand_builtin_prefetch (exp
);
6477 case BUILT_IN_INIT_TRAMPOLINE
:
6478 return expand_builtin_init_trampoline (exp
, true);
6479 case BUILT_IN_INIT_HEAP_TRAMPOLINE
:
6480 return expand_builtin_init_trampoline (exp
, false);
6481 case BUILT_IN_ADJUST_TRAMPOLINE
:
6482 return expand_builtin_adjust_trampoline (exp
);
6485 case BUILT_IN_EXECL
:
6486 case BUILT_IN_EXECV
:
6487 case BUILT_IN_EXECLP
:
6488 case BUILT_IN_EXECLE
:
6489 case BUILT_IN_EXECVP
:
6490 case BUILT_IN_EXECVE
:
6491 target
= expand_builtin_fork_or_exec (fndecl
, exp
, target
, ignore
);
6496 case BUILT_IN_SYNC_FETCH_AND_ADD_1
:
6497 case BUILT_IN_SYNC_FETCH_AND_ADD_2
:
6498 case BUILT_IN_SYNC_FETCH_AND_ADD_4
:
6499 case BUILT_IN_SYNC_FETCH_AND_ADD_8
:
6500 case BUILT_IN_SYNC_FETCH_AND_ADD_16
:
6501 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_ADD_1
);
6502 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, false, target
);
6507 case BUILT_IN_SYNC_FETCH_AND_SUB_1
:
6508 case BUILT_IN_SYNC_FETCH_AND_SUB_2
:
6509 case BUILT_IN_SYNC_FETCH_AND_SUB_4
:
6510 case BUILT_IN_SYNC_FETCH_AND_SUB_8
:
6511 case BUILT_IN_SYNC_FETCH_AND_SUB_16
:
6512 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_SUB_1
);
6513 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, false, target
);
6518 case BUILT_IN_SYNC_FETCH_AND_OR_1
:
6519 case BUILT_IN_SYNC_FETCH_AND_OR_2
:
6520 case BUILT_IN_SYNC_FETCH_AND_OR_4
:
6521 case BUILT_IN_SYNC_FETCH_AND_OR_8
:
6522 case BUILT_IN_SYNC_FETCH_AND_OR_16
:
6523 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_OR_1
);
6524 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, false, target
);
6529 case BUILT_IN_SYNC_FETCH_AND_AND_1
:
6530 case BUILT_IN_SYNC_FETCH_AND_AND_2
:
6531 case BUILT_IN_SYNC_FETCH_AND_AND_4
:
6532 case BUILT_IN_SYNC_FETCH_AND_AND_8
:
6533 case BUILT_IN_SYNC_FETCH_AND_AND_16
:
6534 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_AND_1
);
6535 target
= expand_builtin_sync_operation (mode
, exp
, AND
, false, target
);
6540 case BUILT_IN_SYNC_FETCH_AND_XOR_1
:
6541 case BUILT_IN_SYNC_FETCH_AND_XOR_2
:
6542 case BUILT_IN_SYNC_FETCH_AND_XOR_4
:
6543 case BUILT_IN_SYNC_FETCH_AND_XOR_8
:
6544 case BUILT_IN_SYNC_FETCH_AND_XOR_16
:
6545 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_XOR_1
);
6546 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, false, target
);
6551 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
6552 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
6553 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
6554 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
6555 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
6556 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_NAND_1
);
6557 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, false, target
);
6562 case BUILT_IN_SYNC_ADD_AND_FETCH_1
:
6563 case BUILT_IN_SYNC_ADD_AND_FETCH_2
:
6564 case BUILT_IN_SYNC_ADD_AND_FETCH_4
:
6565 case BUILT_IN_SYNC_ADD_AND_FETCH_8
:
6566 case BUILT_IN_SYNC_ADD_AND_FETCH_16
:
6567 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_ADD_AND_FETCH_1
);
6568 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, true, target
);
6573 case BUILT_IN_SYNC_SUB_AND_FETCH_1
:
6574 case BUILT_IN_SYNC_SUB_AND_FETCH_2
:
6575 case BUILT_IN_SYNC_SUB_AND_FETCH_4
:
6576 case BUILT_IN_SYNC_SUB_AND_FETCH_8
:
6577 case BUILT_IN_SYNC_SUB_AND_FETCH_16
:
6578 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_SUB_AND_FETCH_1
);
6579 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, true, target
);
6584 case BUILT_IN_SYNC_OR_AND_FETCH_1
:
6585 case BUILT_IN_SYNC_OR_AND_FETCH_2
:
6586 case BUILT_IN_SYNC_OR_AND_FETCH_4
:
6587 case BUILT_IN_SYNC_OR_AND_FETCH_8
:
6588 case BUILT_IN_SYNC_OR_AND_FETCH_16
:
6589 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_OR_AND_FETCH_1
);
6590 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, true, target
);
6595 case BUILT_IN_SYNC_AND_AND_FETCH_1
:
6596 case BUILT_IN_SYNC_AND_AND_FETCH_2
:
6597 case BUILT_IN_SYNC_AND_AND_FETCH_4
:
6598 case BUILT_IN_SYNC_AND_AND_FETCH_8
:
6599 case BUILT_IN_SYNC_AND_AND_FETCH_16
:
6600 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_AND_AND_FETCH_1
);
6601 target
= expand_builtin_sync_operation (mode
, exp
, AND
, true, target
);
6606 case BUILT_IN_SYNC_XOR_AND_FETCH_1
:
6607 case BUILT_IN_SYNC_XOR_AND_FETCH_2
:
6608 case BUILT_IN_SYNC_XOR_AND_FETCH_4
:
6609 case BUILT_IN_SYNC_XOR_AND_FETCH_8
:
6610 case BUILT_IN_SYNC_XOR_AND_FETCH_16
:
6611 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_XOR_AND_FETCH_1
);
6612 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, true, target
);
6617 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
6618 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
6619 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
6620 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
6621 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
6622 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_NAND_AND_FETCH_1
);
6623 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, true, target
);
6628 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
6629 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
6630 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
6631 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
6632 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
6633 if (mode
== VOIDmode
)
6634 mode
= TYPE_MODE (boolean_type_node
);
6635 if (!target
|| !register_operand (target
, mode
))
6636 target
= gen_reg_rtx (mode
);
6638 mode
= get_builtin_sync_mode
6639 (fcode
- BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
);
6640 target
= expand_builtin_compare_and_swap (mode
, exp
, true, target
);
6645 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
:
6646 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2
:
6647 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4
:
6648 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8
:
6649 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16
:
6650 mode
= get_builtin_sync_mode
6651 (fcode
- BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
);
6652 target
= expand_builtin_compare_and_swap (mode
, exp
, false, target
);
6657 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
:
6658 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2
:
6659 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4
:
6660 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8
:
6661 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16
:
6662 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
);
6663 target
= expand_builtin_sync_lock_test_and_set (mode
, exp
, target
);
6668 case BUILT_IN_SYNC_LOCK_RELEASE_1
:
6669 case BUILT_IN_SYNC_LOCK_RELEASE_2
:
6670 case BUILT_IN_SYNC_LOCK_RELEASE_4
:
6671 case BUILT_IN_SYNC_LOCK_RELEASE_8
:
6672 case BUILT_IN_SYNC_LOCK_RELEASE_16
:
6673 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_RELEASE_1
);
6674 expand_builtin_sync_lock_release (mode
, exp
);
6677 case BUILT_IN_SYNC_SYNCHRONIZE
:
6678 expand_builtin_sync_synchronize ();
6681 case BUILT_IN_ATOMIC_EXCHANGE_1
:
6682 case BUILT_IN_ATOMIC_EXCHANGE_2
:
6683 case BUILT_IN_ATOMIC_EXCHANGE_4
:
6684 case BUILT_IN_ATOMIC_EXCHANGE_8
:
6685 case BUILT_IN_ATOMIC_EXCHANGE_16
:
6686 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_EXCHANGE_1
);
6687 target
= expand_builtin_atomic_exchange (mode
, exp
, target
);
6692 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
6693 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
6694 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
6695 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
6696 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
6698 unsigned int nargs
, z
;
6699 vec
<tree
, va_gc
> *vec
;
6702 get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
);
6703 target
= expand_builtin_atomic_compare_exchange (mode
, exp
, target
);
6707 /* If this is turned into an external library call, the weak parameter
6708 must be dropped to match the expected parameter list. */
6709 nargs
= call_expr_nargs (exp
);
6710 vec_alloc (vec
, nargs
- 1);
6711 for (z
= 0; z
< 3; z
++)
6712 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
6713 /* Skip the boolean weak parameter. */
6714 for (z
= 4; z
< 6; z
++)
6715 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
6716 exp
= build_call_vec (TREE_TYPE (exp
), CALL_EXPR_FN (exp
), vec
);
6720 case BUILT_IN_ATOMIC_LOAD_1
:
6721 case BUILT_IN_ATOMIC_LOAD_2
:
6722 case BUILT_IN_ATOMIC_LOAD_4
:
6723 case BUILT_IN_ATOMIC_LOAD_8
:
6724 case BUILT_IN_ATOMIC_LOAD_16
:
6725 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_LOAD_1
);
6726 target
= expand_builtin_atomic_load (mode
, exp
, target
);
6731 case BUILT_IN_ATOMIC_STORE_1
:
6732 case BUILT_IN_ATOMIC_STORE_2
:
6733 case BUILT_IN_ATOMIC_STORE_4
:
6734 case BUILT_IN_ATOMIC_STORE_8
:
6735 case BUILT_IN_ATOMIC_STORE_16
:
6736 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_STORE_1
);
6737 target
= expand_builtin_atomic_store (mode
, exp
);
6742 case BUILT_IN_ATOMIC_ADD_FETCH_1
:
6743 case BUILT_IN_ATOMIC_ADD_FETCH_2
:
6744 case BUILT_IN_ATOMIC_ADD_FETCH_4
:
6745 case BUILT_IN_ATOMIC_ADD_FETCH_8
:
6746 case BUILT_IN_ATOMIC_ADD_FETCH_16
:
6748 enum built_in_function lib
;
6749 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
);
6750 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_ADD_1
+
6751 (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
));
6752 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, true,
6758 case BUILT_IN_ATOMIC_SUB_FETCH_1
:
6759 case BUILT_IN_ATOMIC_SUB_FETCH_2
:
6760 case BUILT_IN_ATOMIC_SUB_FETCH_4
:
6761 case BUILT_IN_ATOMIC_SUB_FETCH_8
:
6762 case BUILT_IN_ATOMIC_SUB_FETCH_16
:
6764 enum built_in_function lib
;
6765 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
);
6766 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_SUB_1
+
6767 (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
));
6768 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, true,
6774 case BUILT_IN_ATOMIC_AND_FETCH_1
:
6775 case BUILT_IN_ATOMIC_AND_FETCH_2
:
6776 case BUILT_IN_ATOMIC_AND_FETCH_4
:
6777 case BUILT_IN_ATOMIC_AND_FETCH_8
:
6778 case BUILT_IN_ATOMIC_AND_FETCH_16
:
6780 enum built_in_function lib
;
6781 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
);
6782 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_AND_1
+
6783 (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
));
6784 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, true,
6790 case BUILT_IN_ATOMIC_NAND_FETCH_1
:
6791 case BUILT_IN_ATOMIC_NAND_FETCH_2
:
6792 case BUILT_IN_ATOMIC_NAND_FETCH_4
:
6793 case BUILT_IN_ATOMIC_NAND_FETCH_8
:
6794 case BUILT_IN_ATOMIC_NAND_FETCH_16
:
6796 enum built_in_function lib
;
6797 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
);
6798 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_NAND_1
+
6799 (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
));
6800 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, true,
6806 case BUILT_IN_ATOMIC_XOR_FETCH_1
:
6807 case BUILT_IN_ATOMIC_XOR_FETCH_2
:
6808 case BUILT_IN_ATOMIC_XOR_FETCH_4
:
6809 case BUILT_IN_ATOMIC_XOR_FETCH_8
:
6810 case BUILT_IN_ATOMIC_XOR_FETCH_16
:
6812 enum built_in_function lib
;
6813 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
);
6814 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_XOR_1
+
6815 (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
));
6816 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, true,
6822 case BUILT_IN_ATOMIC_OR_FETCH_1
:
6823 case BUILT_IN_ATOMIC_OR_FETCH_2
:
6824 case BUILT_IN_ATOMIC_OR_FETCH_4
:
6825 case BUILT_IN_ATOMIC_OR_FETCH_8
:
6826 case BUILT_IN_ATOMIC_OR_FETCH_16
:
6828 enum built_in_function lib
;
6829 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
);
6830 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_OR_1
+
6831 (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
));
6832 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, true,
6838 case BUILT_IN_ATOMIC_FETCH_ADD_1
:
6839 case BUILT_IN_ATOMIC_FETCH_ADD_2
:
6840 case BUILT_IN_ATOMIC_FETCH_ADD_4
:
6841 case BUILT_IN_ATOMIC_FETCH_ADD_8
:
6842 case BUILT_IN_ATOMIC_FETCH_ADD_16
:
6843 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_ADD_1
);
6844 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, false,
6845 ignore
, BUILT_IN_NONE
);
6850 case BUILT_IN_ATOMIC_FETCH_SUB_1
:
6851 case BUILT_IN_ATOMIC_FETCH_SUB_2
:
6852 case BUILT_IN_ATOMIC_FETCH_SUB_4
:
6853 case BUILT_IN_ATOMIC_FETCH_SUB_8
:
6854 case BUILT_IN_ATOMIC_FETCH_SUB_16
:
6855 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_SUB_1
);
6856 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, false,
6857 ignore
, BUILT_IN_NONE
);
6862 case BUILT_IN_ATOMIC_FETCH_AND_1
:
6863 case BUILT_IN_ATOMIC_FETCH_AND_2
:
6864 case BUILT_IN_ATOMIC_FETCH_AND_4
:
6865 case BUILT_IN_ATOMIC_FETCH_AND_8
:
6866 case BUILT_IN_ATOMIC_FETCH_AND_16
:
6867 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_AND_1
);
6868 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, false,
6869 ignore
, BUILT_IN_NONE
);
6874 case BUILT_IN_ATOMIC_FETCH_NAND_1
:
6875 case BUILT_IN_ATOMIC_FETCH_NAND_2
:
6876 case BUILT_IN_ATOMIC_FETCH_NAND_4
:
6877 case BUILT_IN_ATOMIC_FETCH_NAND_8
:
6878 case BUILT_IN_ATOMIC_FETCH_NAND_16
:
6879 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_NAND_1
);
6880 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, false,
6881 ignore
, BUILT_IN_NONE
);
6886 case BUILT_IN_ATOMIC_FETCH_XOR_1
:
6887 case BUILT_IN_ATOMIC_FETCH_XOR_2
:
6888 case BUILT_IN_ATOMIC_FETCH_XOR_4
:
6889 case BUILT_IN_ATOMIC_FETCH_XOR_8
:
6890 case BUILT_IN_ATOMIC_FETCH_XOR_16
:
6891 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_XOR_1
);
6892 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, false,
6893 ignore
, BUILT_IN_NONE
);
6898 case BUILT_IN_ATOMIC_FETCH_OR_1
:
6899 case BUILT_IN_ATOMIC_FETCH_OR_2
:
6900 case BUILT_IN_ATOMIC_FETCH_OR_4
:
6901 case BUILT_IN_ATOMIC_FETCH_OR_8
:
6902 case BUILT_IN_ATOMIC_FETCH_OR_16
:
6903 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_OR_1
);
6904 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, false,
6905 ignore
, BUILT_IN_NONE
);
6910 case BUILT_IN_ATOMIC_TEST_AND_SET
:
6911 return expand_builtin_atomic_test_and_set (exp
, target
);
6913 case BUILT_IN_ATOMIC_CLEAR
:
6914 return expand_builtin_atomic_clear (exp
);
6916 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
6917 return expand_builtin_atomic_always_lock_free (exp
);
6919 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
6920 target
= expand_builtin_atomic_is_lock_free (exp
);
6925 case BUILT_IN_ATOMIC_THREAD_FENCE
:
6926 expand_builtin_atomic_thread_fence (exp
);
6929 case BUILT_IN_ATOMIC_SIGNAL_FENCE
:
6930 expand_builtin_atomic_signal_fence (exp
);
6933 case BUILT_IN_OBJECT_SIZE
:
6934 return expand_builtin_object_size (exp
);
6936 case BUILT_IN_MEMCPY_CHK
:
6937 case BUILT_IN_MEMPCPY_CHK
:
6938 case BUILT_IN_MEMMOVE_CHK
:
6939 case BUILT_IN_MEMSET_CHK
:
6940 target
= expand_builtin_memory_chk (exp
, target
, mode
, fcode
);
6945 case BUILT_IN_STRCPY_CHK
:
6946 case BUILT_IN_STPCPY_CHK
:
6947 case BUILT_IN_STRNCPY_CHK
:
6948 case BUILT_IN_STPNCPY_CHK
:
6949 case BUILT_IN_STRCAT_CHK
:
6950 case BUILT_IN_STRNCAT_CHK
:
6951 case BUILT_IN_SNPRINTF_CHK
:
6952 case BUILT_IN_VSNPRINTF_CHK
:
6953 maybe_emit_chk_warning (exp
, fcode
);
6956 case BUILT_IN_SPRINTF_CHK
:
6957 case BUILT_IN_VSPRINTF_CHK
:
6958 maybe_emit_sprintf_chk_warning (exp
, fcode
);
6962 if (warn_free_nonheap_object
)
6963 maybe_emit_free_warning (exp
);
6966 case BUILT_IN_THREAD_POINTER
:
6967 return expand_builtin_thread_pointer (exp
, target
);
6969 case BUILT_IN_SET_THREAD_POINTER
:
6970 expand_builtin_set_thread_pointer (exp
);
6973 case BUILT_IN_CILK_DETACH
:
6974 expand_builtin_cilk_detach (exp
);
6977 case BUILT_IN_CILK_POP_FRAME
:
6978 expand_builtin_cilk_pop_frame (exp
);
6981 case BUILT_IN_CHKP_INIT_PTR_BOUNDS
:
6982 case BUILT_IN_CHKP_NULL_PTR_BOUNDS
:
6983 case BUILT_IN_CHKP_COPY_PTR_BOUNDS
:
6984 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
:
6985 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
:
6986 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS
:
6987 case BUILT_IN_CHKP_SET_PTR_BOUNDS
:
6988 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS
:
6989 case BUILT_IN_CHKP_STORE_PTR_BOUNDS
:
6990 case BUILT_IN_CHKP_GET_PTR_LBOUND
:
6991 case BUILT_IN_CHKP_GET_PTR_UBOUND
:
6992 /* We allow user CHKP builtins if Pointer Bounds
6994 if (!chkp_function_instrumented_p (current_function_decl
))
6996 if (fcode
== BUILT_IN_CHKP_SET_PTR_BOUNDS
6997 || fcode
== BUILT_IN_CHKP_NARROW_PTR_BOUNDS
6998 || fcode
== BUILT_IN_CHKP_INIT_PTR_BOUNDS
6999 || fcode
== BUILT_IN_CHKP_NULL_PTR_BOUNDS
7000 || fcode
== BUILT_IN_CHKP_COPY_PTR_BOUNDS
)
7001 return expand_normal (CALL_EXPR_ARG (exp
, 0));
7002 else if (fcode
== BUILT_IN_CHKP_GET_PTR_LBOUND
)
7003 return expand_normal (size_zero_node
);
7004 else if (fcode
== BUILT_IN_CHKP_GET_PTR_UBOUND
)
7005 return expand_normal (size_int (-1));
7011 case BUILT_IN_CHKP_BNDMK
:
7012 case BUILT_IN_CHKP_BNDSTX
:
7013 case BUILT_IN_CHKP_BNDCL
:
7014 case BUILT_IN_CHKP_BNDCU
:
7015 case BUILT_IN_CHKP_BNDLDX
:
7016 case BUILT_IN_CHKP_BNDRET
:
7017 case BUILT_IN_CHKP_INTERSECT
:
7018 case BUILT_IN_CHKP_NARROW
:
7019 case BUILT_IN_CHKP_EXTRACT_LOWER
:
7020 case BUILT_IN_CHKP_EXTRACT_UPPER
:
7021 /* Software implementation of Pointer Bounds Checker is NYI.
7022 Target support is required. */
7023 error ("Your target platform does not support -fcheck-pointer-bounds");
7026 default: /* just do library call, if unknown builtin */
7030 /* The switch statement above can drop through to cause the function
7031 to be called normally. */
7032 return expand_call (exp
, target
, ignore
);
7035 /* Similar to expand_builtin but is used for instrumented calls. */
7038 expand_builtin_with_bounds (tree exp
, rtx target
,
7039 rtx subtarget ATTRIBUTE_UNUSED
,
7040 machine_mode mode
, int ignore
)
7042 tree fndecl
= get_callee_fndecl (exp
);
7043 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7045 gcc_assert (CALL_WITH_BOUNDS_P (exp
));
7047 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
7048 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
7050 gcc_assert (fcode
> BEGIN_CHKP_BUILTINS
7051 && fcode
< END_CHKP_BUILTINS
);
7055 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP
:
7056 target
= expand_builtin_memcpy_with_bounds (exp
, target
);
7061 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
:
7062 target
= expand_builtin_mempcpy_with_bounds (exp
, target
, mode
);
7067 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP
:
7068 target
= expand_builtin_memset_with_bounds (exp
, target
, mode
);
7077 /* The switch statement above can drop through to cause the function
7078 to be called normally. */
7079 return expand_call (exp
, target
, ignore
);
7082 /* Determine whether a tree node represents a call to a built-in
7083 function. If the tree T is a call to a built-in function with
7084 the right number of arguments of the appropriate types, return
7085 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7086 Otherwise the return value is END_BUILTINS. */
7088 enum built_in_function
7089 builtin_mathfn_code (const_tree t
)
7091 const_tree fndecl
, arg
, parmlist
;
7092 const_tree argtype
, parmtype
;
7093 const_call_expr_arg_iterator iter
;
7095 if (TREE_CODE (t
) != CALL_EXPR
7096 || TREE_CODE (CALL_EXPR_FN (t
)) != ADDR_EXPR
)
7097 return END_BUILTINS
;
7099 fndecl
= get_callee_fndecl (t
);
7100 if (fndecl
== NULL_TREE
7101 || TREE_CODE (fndecl
) != FUNCTION_DECL
7102 || ! DECL_BUILT_IN (fndecl
)
7103 || DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
7104 return END_BUILTINS
;
7106 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
7107 init_const_call_expr_arg_iterator (t
, &iter
);
7108 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
7110 /* If a function doesn't take a variable number of arguments,
7111 the last element in the list will have type `void'. */
7112 parmtype
= TREE_VALUE (parmlist
);
7113 if (VOID_TYPE_P (parmtype
))
7115 if (more_const_call_expr_args_p (&iter
))
7116 return END_BUILTINS
;
7117 return DECL_FUNCTION_CODE (fndecl
);
7120 if (! more_const_call_expr_args_p (&iter
))
7121 return END_BUILTINS
;
7123 arg
= next_const_call_expr_arg (&iter
);
7124 argtype
= TREE_TYPE (arg
);
7126 if (SCALAR_FLOAT_TYPE_P (parmtype
))
7128 if (! SCALAR_FLOAT_TYPE_P (argtype
))
7129 return END_BUILTINS
;
7131 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
7133 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
7134 return END_BUILTINS
;
7136 else if (POINTER_TYPE_P (parmtype
))
7138 if (! POINTER_TYPE_P (argtype
))
7139 return END_BUILTINS
;
7141 else if (INTEGRAL_TYPE_P (parmtype
))
7143 if (! INTEGRAL_TYPE_P (argtype
))
7144 return END_BUILTINS
;
7147 return END_BUILTINS
;
7150 /* Variable-length argument list. */
7151 return DECL_FUNCTION_CODE (fndecl
);
7154 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7155 evaluate to a constant. */
7158 fold_builtin_constant_p (tree arg
)
7160 /* We return 1 for a numeric type that's known to be a constant
7161 value at compile-time or for an aggregate type that's a
7162 literal constant. */
7165 /* If we know this is a constant, emit the constant of one. */
7166 if (CONSTANT_CLASS_P (arg
)
7167 || (TREE_CODE (arg
) == CONSTRUCTOR
7168 && TREE_CONSTANT (arg
)))
7169 return integer_one_node
;
7170 if (TREE_CODE (arg
) == ADDR_EXPR
)
7172 tree op
= TREE_OPERAND (arg
, 0);
7173 if (TREE_CODE (op
) == STRING_CST
7174 || (TREE_CODE (op
) == ARRAY_REF
7175 && integer_zerop (TREE_OPERAND (op
, 1))
7176 && TREE_CODE (TREE_OPERAND (op
, 0)) == STRING_CST
))
7177 return integer_one_node
;
7180 /* If this expression has side effects, show we don't know it to be a
7181 constant. Likewise if it's a pointer or aggregate type since in
7182 those case we only want literals, since those are only optimized
7183 when generating RTL, not later.
7184 And finally, if we are compiling an initializer, not code, we
7185 need to return a definite result now; there's not going to be any
7186 more optimization done. */
7187 if (TREE_SIDE_EFFECTS (arg
)
7188 || AGGREGATE_TYPE_P (TREE_TYPE (arg
))
7189 || POINTER_TYPE_P (TREE_TYPE (arg
))
7191 || folding_initializer
7192 || force_folding_builtin_constant_p
)
7193 return integer_zero_node
;
7198 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7199 return it as a truthvalue. */
7202 build_builtin_expect_predicate (location_t loc
, tree pred
, tree expected
,
7205 tree fn
, arg_types
, pred_type
, expected_type
, call_expr
, ret_type
;
7207 fn
= builtin_decl_explicit (BUILT_IN_EXPECT
);
7208 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
7209 ret_type
= TREE_TYPE (TREE_TYPE (fn
));
7210 pred_type
= TREE_VALUE (arg_types
);
7211 expected_type
= TREE_VALUE (TREE_CHAIN (arg_types
));
7213 pred
= fold_convert_loc (loc
, pred_type
, pred
);
7214 expected
= fold_convert_loc (loc
, expected_type
, expected
);
7215 call_expr
= build_call_expr_loc (loc
, fn
, predictor
? 3 : 2, pred
, expected
,
7218 return build2 (NE_EXPR
, TREE_TYPE (pred
), call_expr
,
7219 build_int_cst (ret_type
, 0));
7222 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7223 NULL_TREE if no simplification is possible. */
7226 fold_builtin_expect (location_t loc
, tree arg0
, tree arg1
, tree arg2
)
7228 tree inner
, fndecl
, inner_arg0
;
7229 enum tree_code code
;
7231 /* Distribute the expected value over short-circuiting operators.
7232 See through the cast from truthvalue_type_node to long. */
7234 while (CONVERT_EXPR_P (inner_arg0
)
7235 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0
))
7236 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0
, 0))))
7237 inner_arg0
= TREE_OPERAND (inner_arg0
, 0);
7239 /* If this is a builtin_expect within a builtin_expect keep the
7240 inner one. See through a comparison against a constant. It
7241 might have been added to create a thruthvalue. */
7244 if (COMPARISON_CLASS_P (inner
)
7245 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
)
7246 inner
= TREE_OPERAND (inner
, 0);
7248 if (TREE_CODE (inner
) == CALL_EXPR
7249 && (fndecl
= get_callee_fndecl (inner
))
7250 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
7251 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
)
7255 code
= TREE_CODE (inner
);
7256 if (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
7258 tree op0
= TREE_OPERAND (inner
, 0);
7259 tree op1
= TREE_OPERAND (inner
, 1);
7261 op0
= build_builtin_expect_predicate (loc
, op0
, arg1
, arg2
);
7262 op1
= build_builtin_expect_predicate (loc
, op1
, arg1
, arg2
);
7263 inner
= build2 (code
, TREE_TYPE (inner
), op0
, op1
);
7265 return fold_convert_loc (loc
, TREE_TYPE (arg0
), inner
);
7268 /* If the argument isn't invariant then there's nothing else we can do. */
7269 if (!TREE_CONSTANT (inner_arg0
))
7272 /* If we expect that a comparison against the argument will fold to
7273 a constant return the constant. In practice, this means a true
7274 constant or the address of a non-weak symbol. */
7277 if (TREE_CODE (inner
) == ADDR_EXPR
)
7281 inner
= TREE_OPERAND (inner
, 0);
7283 while (TREE_CODE (inner
) == COMPONENT_REF
7284 || TREE_CODE (inner
) == ARRAY_REF
);
7285 if ((TREE_CODE (inner
) == VAR_DECL
7286 || TREE_CODE (inner
) == FUNCTION_DECL
)
7287 && DECL_WEAK (inner
))
7291 /* Otherwise, ARG0 already has the proper type for the return value. */
7295 /* Fold a call to __builtin_classify_type with argument ARG. */
7298 fold_builtin_classify_type (tree arg
)
7301 return build_int_cst (integer_type_node
, no_type_class
);
7303 return build_int_cst (integer_type_node
, type_to_class (TREE_TYPE (arg
)));
7306 /* Fold a call to __builtin_strlen with argument ARG. */
7309 fold_builtin_strlen (location_t loc
, tree type
, tree arg
)
7311 if (!validate_arg (arg
, POINTER_TYPE
))
7315 tree len
= c_strlen (arg
, 0);
7318 return fold_convert_loc (loc
, type
, len
);
7324 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7327 fold_builtin_inf (location_t loc
, tree type
, int warn
)
7329 REAL_VALUE_TYPE real
;
7331 /* __builtin_inff is intended to be usable to define INFINITY on all
7332 targets. If an infinity is not available, INFINITY expands "to a
7333 positive constant of type float that overflows at translation
7334 time", footnote "In this case, using INFINITY will violate the
7335 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7336 Thus we pedwarn to ensure this constraint violation is
7338 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
7339 pedwarn (loc
, 0, "target format does not support infinity");
7342 return build_real (type
, real
);
7345 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7348 fold_builtin_nan (tree arg
, tree type
, int quiet
)
7350 REAL_VALUE_TYPE real
;
7353 if (!validate_arg (arg
, POINTER_TYPE
))
7355 str
= c_getstr (arg
);
7359 if (!real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
7362 return build_real (type
, real
);
7365 /* Return true if the floating point expression T has an integer value.
7366 We also allow +Inf, -Inf and NaN to be considered integer values. */
7369 integer_valued_real_p (tree t
)
7371 switch (TREE_CODE (t
))
7378 return integer_valued_real_p (TREE_OPERAND (t
, 0));
7383 return integer_valued_real_p (TREE_OPERAND (t
, 1));
7390 return integer_valued_real_p (TREE_OPERAND (t
, 0))
7391 && integer_valued_real_p (TREE_OPERAND (t
, 1));
7394 return integer_valued_real_p (TREE_OPERAND (t
, 1))
7395 && integer_valued_real_p (TREE_OPERAND (t
, 2));
7398 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
7402 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
7403 if (TREE_CODE (type
) == INTEGER_TYPE
)
7405 if (TREE_CODE (type
) == REAL_TYPE
)
7406 return integer_valued_real_p (TREE_OPERAND (t
, 0));
7411 switch (builtin_mathfn_code (t
))
7413 CASE_FLT_FN (BUILT_IN_CEIL
):
7414 CASE_FLT_FN (BUILT_IN_FLOOR
):
7415 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
7416 CASE_FLT_FN (BUILT_IN_RINT
):
7417 CASE_FLT_FN (BUILT_IN_ROUND
):
7418 CASE_FLT_FN (BUILT_IN_TRUNC
):
7421 CASE_FLT_FN (BUILT_IN_FMIN
):
7422 CASE_FLT_FN (BUILT_IN_FMAX
):
7423 return integer_valued_real_p (CALL_EXPR_ARG (t
, 0))
7424 && integer_valued_real_p (CALL_EXPR_ARG (t
, 1));
7437 /* FNDECL is assumed to be a builtin where truncation can be propagated
7438 across (for instance floor((double)f) == (double)floorf (f).
7439 Do the transformation for a call with argument ARG. */
7442 fold_trunc_transparent_mathfn (location_t loc
, tree fndecl
, tree arg
)
7444 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7446 if (!validate_arg (arg
, REAL_TYPE
))
7449 /* Integer rounding functions are idempotent. */
7450 if (fcode
== builtin_mathfn_code (arg
))
7453 /* If argument is already integer valued, and we don't need to worry
7454 about setting errno, there's no need to perform rounding. */
7455 if (! flag_errno_math
&& integer_valued_real_p (arg
))
7460 tree arg0
= strip_float_extensions (arg
);
7461 tree ftype
= TREE_TYPE (TREE_TYPE (fndecl
));
7462 tree newtype
= TREE_TYPE (arg0
);
7465 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
7466 && (decl
= mathfn_built_in (newtype
, fcode
)))
7467 return fold_convert_loc (loc
, ftype
,
7468 build_call_expr_loc (loc
, decl
, 1,
7469 fold_convert_loc (loc
,
7476 /* FNDECL is assumed to be builtin which can narrow the FP type of
7477 the argument, for instance lround((double)f) -> lroundf (f).
7478 Do the transformation for a call with argument ARG. */
7481 fold_fixed_mathfn (location_t loc
, tree fndecl
, tree arg
)
7483 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7485 if (!validate_arg (arg
, REAL_TYPE
))
7488 /* If argument is already integer valued, and we don't need to worry
7489 about setting errno, there's no need to perform rounding. */
7490 if (! flag_errno_math
&& integer_valued_real_p (arg
))
7491 return fold_build1_loc (loc
, FIX_TRUNC_EXPR
,
7492 TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
7496 tree ftype
= TREE_TYPE (arg
);
7497 tree arg0
= strip_float_extensions (arg
);
7498 tree newtype
= TREE_TYPE (arg0
);
7501 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
7502 && (decl
= mathfn_built_in (newtype
, fcode
)))
7503 return build_call_expr_loc (loc
, decl
, 1,
7504 fold_convert_loc (loc
, newtype
, arg0
));
7507 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7508 sizeof (int) == sizeof (long). */
7509 if (TYPE_PRECISION (integer_type_node
)
7510 == TYPE_PRECISION (long_integer_type_node
))
7512 tree newfn
= NULL_TREE
;
7515 CASE_FLT_FN (BUILT_IN_ICEIL
):
7516 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
7519 CASE_FLT_FN (BUILT_IN_IFLOOR
):
7520 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
7523 CASE_FLT_FN (BUILT_IN_IROUND
):
7524 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
7527 CASE_FLT_FN (BUILT_IN_IRINT
):
7528 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
7537 tree newcall
= build_call_expr_loc (loc
, newfn
, 1, arg
);
7538 return fold_convert_loc (loc
,
7539 TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
7543 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7544 sizeof (long long) == sizeof (long). */
7545 if (TYPE_PRECISION (long_long_integer_type_node
)
7546 == TYPE_PRECISION (long_integer_type_node
))
7548 tree newfn
= NULL_TREE
;
7551 CASE_FLT_FN (BUILT_IN_LLCEIL
):
7552 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
7555 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7556 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
7559 CASE_FLT_FN (BUILT_IN_LLROUND
):
7560 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
7563 CASE_FLT_FN (BUILT_IN_LLRINT
):
7564 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
7573 tree newcall
= build_call_expr_loc (loc
, newfn
, 1, arg
);
7574 return fold_convert_loc (loc
,
7575 TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
7582 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7583 return type. Return NULL_TREE if no simplification can be made. */
7586 fold_builtin_cabs (location_t loc
, tree arg
, tree type
, tree fndecl
)
7590 if (!validate_arg (arg
, COMPLEX_TYPE
)
7591 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
7594 /* Calculate the result when the argument is a constant. */
7595 if (TREE_CODE (arg
) == COMPLEX_CST
7596 && (res
= do_mpfr_arg2 (TREE_REALPART (arg
), TREE_IMAGPART (arg
),
7600 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
7602 tree real
= TREE_OPERAND (arg
, 0);
7603 tree imag
= TREE_OPERAND (arg
, 1);
7605 /* If either part is zero, cabs is fabs of the other. */
7606 if (real_zerop (real
))
7607 return fold_build1_loc (loc
, ABS_EXPR
, type
, imag
);
7608 if (real_zerop (imag
))
7609 return fold_build1_loc (loc
, ABS_EXPR
, type
, real
);
7611 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7612 if (flag_unsafe_math_optimizations
7613 && operand_equal_p (real
, imag
, OEP_PURE_SAME
))
7615 const REAL_VALUE_TYPE sqrt2_trunc
7616 = real_value_truncate (TYPE_MODE (type
), dconst_sqrt2 ());
7618 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7619 fold_build1_loc (loc
, ABS_EXPR
, type
, real
),
7620 build_real (type
, sqrt2_trunc
));
7624 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7625 if (TREE_CODE (arg
) == NEGATE_EXPR
7626 || TREE_CODE (arg
) == CONJ_EXPR
)
7627 return build_call_expr_loc (loc
, fndecl
, 1, TREE_OPERAND (arg
, 0));
7629 /* Don't do this when optimizing for size. */
7630 if (flag_unsafe_math_optimizations
7631 && optimize
&& optimize_function_for_speed_p (cfun
))
7633 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
7635 if (sqrtfn
!= NULL_TREE
)
7637 tree rpart
, ipart
, result
;
7639 arg
= builtin_save_expr (arg
);
7641 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, type
, arg
);
7642 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg
);
7644 rpart
= builtin_save_expr (rpart
);
7645 ipart
= builtin_save_expr (ipart
);
7647 result
= fold_build2_loc (loc
, PLUS_EXPR
, type
,
7648 fold_build2_loc (loc
, MULT_EXPR
, type
,
7650 fold_build2_loc (loc
, MULT_EXPR
, type
,
7653 return build_call_expr_loc (loc
, sqrtfn
, 1, result
);
7660 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7661 complex tree type of the result. If NEG is true, the imaginary
7662 zero is negative. */
7665 build_complex_cproj (tree type
, bool neg
)
7667 REAL_VALUE_TYPE rinf
, rzero
= dconst0
;
7671 return build_complex (type
, build_real (TREE_TYPE (type
), rinf
),
7672 build_real (TREE_TYPE (type
), rzero
));
7675 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7676 return type. Return NULL_TREE if no simplification can be made. */
7679 fold_builtin_cproj (location_t loc
, tree arg
, tree type
)
7681 if (!validate_arg (arg
, COMPLEX_TYPE
)
7682 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
7685 /* If there are no infinities, return arg. */
7686 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type
))))
7687 return non_lvalue_loc (loc
, arg
);
7689 /* Calculate the result when the argument is a constant. */
7690 if (TREE_CODE (arg
) == COMPLEX_CST
)
7692 const REAL_VALUE_TYPE
*real
= TREE_REAL_CST_PTR (TREE_REALPART (arg
));
7693 const REAL_VALUE_TYPE
*imag
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg
));
7695 if (real_isinf (real
) || real_isinf (imag
))
7696 return build_complex_cproj (type
, imag
->sign
);
7700 else if (TREE_CODE (arg
) == COMPLEX_EXPR
)
7702 tree real
= TREE_OPERAND (arg
, 0);
7703 tree imag
= TREE_OPERAND (arg
, 1);
7708 /* If the real part is inf and the imag part is known to be
7709 nonnegative, return (inf + 0i). Remember side-effects are
7710 possible in the imag part. */
7711 if (TREE_CODE (real
) == REAL_CST
7712 && real_isinf (TREE_REAL_CST_PTR (real
))
7713 && tree_expr_nonnegative_p (imag
))
7714 return omit_one_operand_loc (loc
, type
,
7715 build_complex_cproj (type
, false),
7718 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7719 Remember side-effects are possible in the real part. */
7720 if (TREE_CODE (imag
) == REAL_CST
7721 && real_isinf (TREE_REAL_CST_PTR (imag
)))
7723 omit_one_operand_loc (loc
, type
,
7724 build_complex_cproj (type
, TREE_REAL_CST_PTR
7725 (imag
)->sign
), arg
);
7731 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7732 Return NULL_TREE if no simplification can be made. */
7735 fold_builtin_sqrt (location_t loc
, tree arg
, tree type
)
7738 enum built_in_function fcode
;
7741 if (!validate_arg (arg
, REAL_TYPE
))
7744 /* Calculate the result when the argument is a constant. */
7745 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_sqrt
, &dconst0
, NULL
, true)))
7748 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7749 fcode
= builtin_mathfn_code (arg
);
7750 if (flag_unsafe_math_optimizations
&& BUILTIN_EXPONENT_P (fcode
))
7752 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7753 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
7754 CALL_EXPR_ARG (arg
, 0),
7755 build_real (type
, dconsthalf
));
7756 return build_call_expr_loc (loc
, expfn
, 1, arg
);
7759 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7760 if (flag_unsafe_math_optimizations
&& BUILTIN_ROOT_P (fcode
))
7762 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7766 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7768 /* The inner root was either sqrt or cbrt. */
7769 /* This was a conditional expression but it triggered a bug
7771 REAL_VALUE_TYPE dconstroot
;
7772 if (BUILTIN_SQRT_P (fcode
))
7773 dconstroot
= dconsthalf
;
7775 dconstroot
= dconst_third ();
7777 /* Adjust for the outer root. */
7778 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7779 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7780 tree_root
= build_real (type
, dconstroot
);
7781 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7785 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7786 if (flag_unsafe_math_optimizations
7787 && (fcode
== BUILT_IN_POW
7788 || fcode
== BUILT_IN_POWF
7789 || fcode
== BUILT_IN_POWL
))
7791 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7792 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7793 tree arg1
= CALL_EXPR_ARG (arg
, 1);
7795 if (!tree_expr_nonnegative_p (arg0
))
7796 arg0
= build1 (ABS_EXPR
, type
, arg0
);
7797 narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
7798 build_real (type
, dconsthalf
));
7799 return build_call_expr_loc (loc
, powfn
, 2, arg0
, narg1
);
7805 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7806 Return NULL_TREE if no simplification can be made. */
7809 fold_builtin_cbrt (location_t loc
, tree arg
, tree type
)
7811 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
7814 if (!validate_arg (arg
, REAL_TYPE
))
7817 /* Calculate the result when the argument is a constant. */
7818 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cbrt
, NULL
, NULL
, 0)))
7821 if (flag_unsafe_math_optimizations
)
7823 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7824 if (BUILTIN_EXPONENT_P (fcode
))
7826 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7827 const REAL_VALUE_TYPE third_trunc
=
7828 real_value_truncate (TYPE_MODE (type
), dconst_third ());
7829 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
7830 CALL_EXPR_ARG (arg
, 0),
7831 build_real (type
, third_trunc
));
7832 return build_call_expr_loc (loc
, expfn
, 1, arg
);
7835 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7836 if (BUILTIN_SQRT_P (fcode
))
7838 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7842 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7844 REAL_VALUE_TYPE dconstroot
= dconst_third ();
7846 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7847 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7848 tree_root
= build_real (type
, dconstroot
);
7849 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7853 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7854 if (BUILTIN_CBRT_P (fcode
))
7856 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7857 if (tree_expr_nonnegative_p (arg0
))
7859 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7864 REAL_VALUE_TYPE dconstroot
;
7866 real_arithmetic (&dconstroot
, MULT_EXPR
,
7867 dconst_third_ptr (), dconst_third_ptr ());
7868 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7869 tree_root
= build_real (type
, dconstroot
);
7870 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7875 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7876 if (fcode
== BUILT_IN_POW
7877 || fcode
== BUILT_IN_POWF
7878 || fcode
== BUILT_IN_POWL
)
7880 tree arg00
= CALL_EXPR_ARG (arg
, 0);
7881 tree arg01
= CALL_EXPR_ARG (arg
, 1);
7882 if (tree_expr_nonnegative_p (arg00
))
7884 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7885 const REAL_VALUE_TYPE dconstroot
7886 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
7887 tree narg01
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg01
,
7888 build_real (type
, dconstroot
));
7889 return build_call_expr_loc (loc
, powfn
, 2, arg00
, narg01
);
7896 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7897 TYPE is the type of the return value. Return NULL_TREE if no
7898 simplification can be made. */
7901 fold_builtin_cos (location_t loc
,
7902 tree arg
, tree type
, tree fndecl
)
7906 if (!validate_arg (arg
, REAL_TYPE
))
7909 /* Calculate the result when the argument is a constant. */
7910 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cos
, NULL
, NULL
, 0)))
7913 /* Optimize cos(-x) into cos (x). */
7914 if ((narg
= fold_strip_sign_ops (arg
)))
7915 return build_call_expr_loc (loc
, fndecl
, 1, narg
);
7920 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7921 Return NULL_TREE if no simplification can be made. */
7924 fold_builtin_cosh (location_t loc
, tree arg
, tree type
, tree fndecl
)
7926 if (validate_arg (arg
, REAL_TYPE
))
7930 /* Calculate the result when the argument is a constant. */
7931 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cosh
, NULL
, NULL
, 0)))
7934 /* Optimize cosh(-x) into cosh (x). */
7935 if ((narg
= fold_strip_sign_ops (arg
)))
7936 return build_call_expr_loc (loc
, fndecl
, 1, narg
);
7942 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7943 argument ARG. TYPE is the type of the return value. Return
7944 NULL_TREE if no simplification can be made. */
7947 fold_builtin_ccos (location_t loc
, tree arg
, tree type
, tree fndecl
,
7950 if (validate_arg (arg
, COMPLEX_TYPE
)
7951 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
7955 /* Calculate the result when the argument is a constant. */
7956 if ((tmp
= do_mpc_arg1 (arg
, type
, (hyper
? mpc_cosh
: mpc_cos
))))
7959 /* Optimize fn(-x) into fn(x). */
7960 if ((tmp
= fold_strip_sign_ops (arg
)))
7961 return build_call_expr_loc (loc
, fndecl
, 1, tmp
);
7967 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7968 Return NULL_TREE if no simplification can be made. */
7971 fold_builtin_tan (tree arg
, tree type
)
7973 enum built_in_function fcode
;
7976 if (!validate_arg (arg
, REAL_TYPE
))
7979 /* Calculate the result when the argument is a constant. */
7980 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_tan
, NULL
, NULL
, 0)))
7983 /* Optimize tan(atan(x)) = x. */
7984 fcode
= builtin_mathfn_code (arg
);
7985 if (flag_unsafe_math_optimizations
7986 && (fcode
== BUILT_IN_ATAN
7987 || fcode
== BUILT_IN_ATANF
7988 || fcode
== BUILT_IN_ATANL
))
7989 return CALL_EXPR_ARG (arg
, 0);
7994 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7995 NULL_TREE if no simplification can be made. */
7998 fold_builtin_sincos (location_t loc
,
7999 tree arg0
, tree arg1
, tree arg2
)
8004 if (!validate_arg (arg0
, REAL_TYPE
)
8005 || !validate_arg (arg1
, POINTER_TYPE
)
8006 || !validate_arg (arg2
, POINTER_TYPE
))
8009 type
= TREE_TYPE (arg0
);
8011 /* Calculate the result when the argument is a constant. */
8012 if ((res
= do_mpfr_sincos (arg0
, arg1
, arg2
)))
8015 /* Canonicalize sincos to cexpi. */
8016 if (!targetm
.libc_has_function (function_c99_math_complex
))
8018 fn
= mathfn_built_in (type
, BUILT_IN_CEXPI
);
8022 call
= build_call_expr_loc (loc
, fn
, 1, arg0
);
8023 call
= builtin_save_expr (call
);
8025 return build2 (COMPOUND_EXPR
, void_type_node
,
8026 build2 (MODIFY_EXPR
, void_type_node
,
8027 build_fold_indirect_ref_loc (loc
, arg1
),
8028 build1 (IMAGPART_EXPR
, type
, call
)),
8029 build2 (MODIFY_EXPR
, void_type_node
,
8030 build_fold_indirect_ref_loc (loc
, arg2
),
8031 build1 (REALPART_EXPR
, type
, call
)));
8034 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
8035 NULL_TREE if no simplification can be made. */
8038 fold_builtin_cexp (location_t loc
, tree arg0
, tree type
)
8041 tree realp
, imagp
, ifn
;
8044 if (!validate_arg (arg0
, COMPLEX_TYPE
)
8045 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) != REAL_TYPE
)
8048 /* Calculate the result when the argument is a constant. */
8049 if ((res
= do_mpc_arg1 (arg0
, type
, mpc_exp
)))
8052 rtype
= TREE_TYPE (TREE_TYPE (arg0
));
8054 /* In case we can figure out the real part of arg0 and it is constant zero
8056 if (!targetm
.libc_has_function (function_c99_math_complex
))
8058 ifn
= mathfn_built_in (rtype
, BUILT_IN_CEXPI
);
8062 if ((realp
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
))
8063 && real_zerop (realp
))
8065 tree narg
= fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
8066 return build_call_expr_loc (loc
, ifn
, 1, narg
);
8069 /* In case we can easily decompose real and imaginary parts split cexp
8070 to exp (r) * cexpi (i). */
8071 if (flag_unsafe_math_optimizations
8074 tree rfn
, rcall
, icall
;
8076 rfn
= mathfn_built_in (rtype
, BUILT_IN_EXP
);
8080 imagp
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
8084 icall
= build_call_expr_loc (loc
, ifn
, 1, imagp
);
8085 icall
= builtin_save_expr (icall
);
8086 rcall
= build_call_expr_loc (loc
, rfn
, 1, realp
);
8087 rcall
= builtin_save_expr (rcall
);
8088 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
8089 fold_build2_loc (loc
, MULT_EXPR
, rtype
,
8091 fold_build1_loc (loc
, REALPART_EXPR
,
8093 fold_build2_loc (loc
, MULT_EXPR
, rtype
,
8095 fold_build1_loc (loc
, IMAGPART_EXPR
,
8102 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
8103 Return NULL_TREE if no simplification can be made. */
8106 fold_builtin_trunc (location_t loc
, tree fndecl
, tree arg
)
8108 if (!validate_arg (arg
, REAL_TYPE
))
8111 /* Optimize trunc of constant value. */
8112 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8114 REAL_VALUE_TYPE r
, x
;
8115 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8117 x
= TREE_REAL_CST (arg
);
8118 real_trunc (&r
, TYPE_MODE (type
), &x
);
8119 return build_real (type
, r
);
8122 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
8125 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
8126 Return NULL_TREE if no simplification can be made. */
8129 fold_builtin_floor (location_t loc
, tree fndecl
, tree arg
)
8131 if (!validate_arg (arg
, REAL_TYPE
))
8134 /* Optimize floor of constant value. */
8135 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8139 x
= TREE_REAL_CST (arg
);
8140 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
8142 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8145 real_floor (&r
, TYPE_MODE (type
), &x
);
8146 return build_real (type
, r
);
8150 /* Fold floor (x) where x is nonnegative to trunc (x). */
8151 if (tree_expr_nonnegative_p (arg
))
8153 tree truncfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_TRUNC
);
8155 return build_call_expr_loc (loc
, truncfn
, 1, arg
);
8158 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
8161 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
8162 Return NULL_TREE if no simplification can be made. */
8165 fold_builtin_ceil (location_t loc
, tree fndecl
, tree arg
)
8167 if (!validate_arg (arg
, REAL_TYPE
))
8170 /* Optimize ceil of constant value. */
8171 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8175 x
= TREE_REAL_CST (arg
);
8176 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
8178 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8181 real_ceil (&r
, TYPE_MODE (type
), &x
);
8182 return build_real (type
, r
);
8186 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
8189 /* Fold function call to builtin round, roundf or roundl with argument ARG.
8190 Return NULL_TREE if no simplification can be made. */
8193 fold_builtin_round (location_t loc
, tree fndecl
, tree arg
)
8195 if (!validate_arg (arg
, REAL_TYPE
))
8198 /* Optimize round of constant value. */
8199 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8203 x
= TREE_REAL_CST (arg
);
8204 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
8206 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8209 real_round (&r
, TYPE_MODE (type
), &x
);
8210 return build_real (type
, r
);
8214 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
8217 /* Fold function call to builtin lround, lroundf or lroundl (or the
8218 corresponding long long versions) and other rounding functions. ARG
8219 is the argument to the call. Return NULL_TREE if no simplification
8223 fold_builtin_int_roundingfn (location_t loc
, tree fndecl
, tree arg
)
8225 if (!validate_arg (arg
, REAL_TYPE
))
8228 /* Optimize lround of constant value. */
8229 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
8231 const REAL_VALUE_TYPE x
= TREE_REAL_CST (arg
);
8233 if (real_isfinite (&x
))
8235 tree itype
= TREE_TYPE (TREE_TYPE (fndecl
));
8236 tree ftype
= TREE_TYPE (arg
);
8240 switch (DECL_FUNCTION_CODE (fndecl
))
8242 CASE_FLT_FN (BUILT_IN_IFLOOR
):
8243 CASE_FLT_FN (BUILT_IN_LFLOOR
):
8244 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
8245 real_floor (&r
, TYPE_MODE (ftype
), &x
);
8248 CASE_FLT_FN (BUILT_IN_ICEIL
):
8249 CASE_FLT_FN (BUILT_IN_LCEIL
):
8250 CASE_FLT_FN (BUILT_IN_LLCEIL
):
8251 real_ceil (&r
, TYPE_MODE (ftype
), &x
);
8254 CASE_FLT_FN (BUILT_IN_IROUND
):
8255 CASE_FLT_FN (BUILT_IN_LROUND
):
8256 CASE_FLT_FN (BUILT_IN_LLROUND
):
8257 real_round (&r
, TYPE_MODE (ftype
), &x
);
8264 wide_int val
= real_to_integer (&r
, &fail
, TYPE_PRECISION (itype
));
8266 return wide_int_to_tree (itype
, val
);
8270 switch (DECL_FUNCTION_CODE (fndecl
))
8272 CASE_FLT_FN (BUILT_IN_LFLOOR
):
8273 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
8274 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8275 if (tree_expr_nonnegative_p (arg
))
8276 return fold_build1_loc (loc
, FIX_TRUNC_EXPR
,
8277 TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
8282 return fold_fixed_mathfn (loc
, fndecl
, arg
);
8285 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8286 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8287 the argument to the call. Return NULL_TREE if no simplification can
8291 fold_builtin_bitop (tree fndecl
, tree arg
)
8293 if (!validate_arg (arg
, INTEGER_TYPE
))
8296 /* Optimize for constant argument. */
8297 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
8299 tree type
= TREE_TYPE (arg
);
8302 switch (DECL_FUNCTION_CODE (fndecl
))
8304 CASE_INT_FN (BUILT_IN_FFS
):
8305 result
= wi::ffs (arg
);
8308 CASE_INT_FN (BUILT_IN_CLZ
):
8309 if (wi::ne_p (arg
, 0))
8310 result
= wi::clz (arg
);
8311 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
8312 result
= TYPE_PRECISION (type
);
8315 CASE_INT_FN (BUILT_IN_CTZ
):
8316 if (wi::ne_p (arg
, 0))
8317 result
= wi::ctz (arg
);
8318 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
8319 result
= TYPE_PRECISION (type
);
8322 CASE_INT_FN (BUILT_IN_CLRSB
):
8323 result
= wi::clrsb (arg
);
8326 CASE_INT_FN (BUILT_IN_POPCOUNT
):
8327 result
= wi::popcount (arg
);
8330 CASE_INT_FN (BUILT_IN_PARITY
):
8331 result
= wi::parity (arg
);
8338 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), result
);
8344 /* Fold function call to builtin_bswap and the short, long and long long
8345 variants. Return NULL_TREE if no simplification can be made. */
8347 fold_builtin_bswap (tree fndecl
, tree arg
)
8349 if (! validate_arg (arg
, INTEGER_TYPE
))
8352 /* Optimize constant value. */
8353 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
8355 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8357 switch (DECL_FUNCTION_CODE (fndecl
))
8359 case BUILT_IN_BSWAP16
:
8360 case BUILT_IN_BSWAP32
:
8361 case BUILT_IN_BSWAP64
:
8363 signop sgn
= TYPE_SIGN (type
);
8365 wide_int_to_tree (type
,
8366 wide_int::from (arg
, TYPE_PRECISION (type
),
8378 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8379 NULL_TREE if no simplification can be made. */
8382 fold_builtin_hypot (location_t loc
, tree fndecl
,
8383 tree arg0
, tree arg1
, tree type
)
8385 tree res
, narg0
, narg1
;
8387 if (!validate_arg (arg0
, REAL_TYPE
)
8388 || !validate_arg (arg1
, REAL_TYPE
))
8391 /* Calculate the result when the argument is a constant. */
8392 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_hypot
)))
8395 /* If either argument to hypot has a negate or abs, strip that off.
8396 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8397 narg0
= fold_strip_sign_ops (arg0
);
8398 narg1
= fold_strip_sign_ops (arg1
);
8401 return build_call_expr_loc (loc
, fndecl
, 2, narg0
? narg0
: arg0
,
8402 narg1
? narg1
: arg1
);
8405 /* If either argument is zero, hypot is fabs of the other. */
8406 if (real_zerop (arg0
))
8407 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg1
);
8408 else if (real_zerop (arg1
))
8409 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg0
);
8411 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8412 if (flag_unsafe_math_optimizations
8413 && operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
8415 const REAL_VALUE_TYPE sqrt2_trunc
8416 = real_value_truncate (TYPE_MODE (type
), dconst_sqrt2 ());
8417 return fold_build2_loc (loc
, MULT_EXPR
, type
,
8418 fold_build1_loc (loc
, ABS_EXPR
, type
, arg0
),
8419 build_real (type
, sqrt2_trunc
));
8426 /* Fold a builtin function call to pow, powf, or powl. Return
8427 NULL_TREE if no simplification can be made. */
8429 fold_builtin_pow (location_t loc
, tree fndecl
, tree arg0
, tree arg1
, tree type
)
8433 if (!validate_arg (arg0
, REAL_TYPE
)
8434 || !validate_arg (arg1
, REAL_TYPE
))
8437 /* Calculate the result when the argument is a constant. */
8438 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_pow
)))
8441 /* Optimize pow(1.0,y) = 1.0. */
8442 if (real_onep (arg0
))
8443 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
), arg1
);
8445 if (TREE_CODE (arg1
) == REAL_CST
8446 && !TREE_OVERFLOW (arg1
))
8448 REAL_VALUE_TYPE cint
;
8452 c
= TREE_REAL_CST (arg1
);
8454 /* Optimize pow(x,0.0) = 1.0. */
8455 if (REAL_VALUES_EQUAL (c
, dconst0
))
8456 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
),
8459 /* Optimize pow(x,1.0) = x. */
8460 if (REAL_VALUES_EQUAL (c
, dconst1
))
8463 /* Optimize pow(x,-1.0) = 1.0/x. */
8464 if (REAL_VALUES_EQUAL (c
, dconstm1
))
8465 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
8466 build_real (type
, dconst1
), arg0
);
8468 /* Optimize pow(x,0.5) = sqrt(x). */
8469 if (flag_unsafe_math_optimizations
8470 && REAL_VALUES_EQUAL (c
, dconsthalf
))
8472 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
8474 if (sqrtfn
!= NULL_TREE
)
8475 return build_call_expr_loc (loc
, sqrtfn
, 1, arg0
);
8478 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8479 if (flag_unsafe_math_optimizations
)
8481 const REAL_VALUE_TYPE dconstroot
8482 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
8484 if (REAL_VALUES_EQUAL (c
, dconstroot
))
8486 tree cbrtfn
= mathfn_built_in (type
, BUILT_IN_CBRT
);
8487 if (cbrtfn
!= NULL_TREE
)
8488 return build_call_expr_loc (loc
, cbrtfn
, 1, arg0
);
8492 /* Check for an integer exponent. */
8493 n
= real_to_integer (&c
);
8494 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
8495 if (real_identical (&c
, &cint
))
8497 /* Attempt to evaluate pow at compile-time, unless this should
8498 raise an exception. */
8499 if (TREE_CODE (arg0
) == REAL_CST
8500 && !TREE_OVERFLOW (arg0
)
8502 || (!flag_trapping_math
&& !flag_errno_math
)
8503 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0
), dconst0
)))
8508 x
= TREE_REAL_CST (arg0
);
8509 inexact
= real_powi (&x
, TYPE_MODE (type
), &x
, n
);
8510 if (flag_unsafe_math_optimizations
|| !inexact
)
8511 return build_real (type
, x
);
8514 /* Strip sign ops from even integer powers. */
8515 if ((n
& 1) == 0 && flag_unsafe_math_optimizations
)
8517 tree narg0
= fold_strip_sign_ops (arg0
);
8519 return build_call_expr_loc (loc
, fndecl
, 2, narg0
, arg1
);
8524 if (flag_unsafe_math_optimizations
)
8526 const enum built_in_function fcode
= builtin_mathfn_code (arg0
);
8528 /* Optimize pow(expN(x),y) = expN(x*y). */
8529 if (BUILTIN_EXPONENT_P (fcode
))
8531 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
8532 tree arg
= CALL_EXPR_ARG (arg0
, 0);
8533 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg
, arg1
);
8534 return build_call_expr_loc (loc
, expfn
, 1, arg
);
8537 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8538 if (BUILTIN_SQRT_P (fcode
))
8540 tree narg0
= CALL_EXPR_ARG (arg0
, 0);
8541 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
8542 build_real (type
, dconsthalf
));
8543 return build_call_expr_loc (loc
, fndecl
, 2, narg0
, narg1
);
8546 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8547 if (BUILTIN_CBRT_P (fcode
))
8549 tree arg
= CALL_EXPR_ARG (arg0
, 0);
8550 if (tree_expr_nonnegative_p (arg
))
8552 const REAL_VALUE_TYPE dconstroot
8553 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
8554 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
8555 build_real (type
, dconstroot
));
8556 return build_call_expr_loc (loc
, fndecl
, 2, arg
, narg1
);
8560 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8561 if (fcode
== BUILT_IN_POW
8562 || fcode
== BUILT_IN_POWF
8563 || fcode
== BUILT_IN_POWL
)
8565 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
8566 if (tree_expr_nonnegative_p (arg00
))
8568 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
8569 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg01
, arg1
);
8570 return build_call_expr_loc (loc
, fndecl
, 2, arg00
, narg1
);
8578 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8579 Return NULL_TREE if no simplification can be made. */
8581 fold_builtin_powi (location_t loc
, tree fndecl ATTRIBUTE_UNUSED
,
8582 tree arg0
, tree arg1
, tree type
)
8584 if (!validate_arg (arg0
, REAL_TYPE
)
8585 || !validate_arg (arg1
, INTEGER_TYPE
))
8588 /* Optimize pow(1.0,y) = 1.0. */
8589 if (real_onep (arg0
))
8590 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
), arg1
);
8592 if (tree_fits_shwi_p (arg1
))
8594 HOST_WIDE_INT c
= tree_to_shwi (arg1
);
8596 /* Evaluate powi at compile-time. */
8597 if (TREE_CODE (arg0
) == REAL_CST
8598 && !TREE_OVERFLOW (arg0
))
8601 x
= TREE_REAL_CST (arg0
);
8602 real_powi (&x
, TYPE_MODE (type
), &x
, c
);
8603 return build_real (type
, x
);
8606 /* Optimize pow(x,0) = 1.0. */
8608 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
),
8611 /* Optimize pow(x,1) = x. */
8615 /* Optimize pow(x,-1) = 1.0/x. */
8617 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
8618 build_real (type
, dconst1
), arg0
);
8624 /* A subroutine of fold_builtin to fold the various exponent
8625 functions. Return NULL_TREE if no simplification can be made.
8626 FUNC is the corresponding MPFR exponent function. */
8629 fold_builtin_exponent (location_t loc
, tree fndecl
, tree arg
,
8630 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
))
8632 if (validate_arg (arg
, REAL_TYPE
))
8634 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8637 /* Calculate the result when the argument is a constant. */
8638 if ((res
= do_mpfr_arg1 (arg
, type
, func
, NULL
, NULL
, 0)))
8641 /* Optimize expN(logN(x)) = x. */
8642 if (flag_unsafe_math_optimizations
)
8644 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
8646 if ((func
== mpfr_exp
8647 && (fcode
== BUILT_IN_LOG
8648 || fcode
== BUILT_IN_LOGF
8649 || fcode
== BUILT_IN_LOGL
))
8650 || (func
== mpfr_exp2
8651 && (fcode
== BUILT_IN_LOG2
8652 || fcode
== BUILT_IN_LOG2F
8653 || fcode
== BUILT_IN_LOG2L
))
8654 || (func
== mpfr_exp10
8655 && (fcode
== BUILT_IN_LOG10
8656 || fcode
== BUILT_IN_LOG10F
8657 || fcode
== BUILT_IN_LOG10L
)))
8658 return fold_convert_loc (loc
, type
, CALL_EXPR_ARG (arg
, 0));
8665 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8666 Return NULL_TREE if no simplification can be made. */
8669 fold_builtin_stpcpy (location_t loc
, tree fndecl
, tree dest
, tree src
)
8671 tree fn
, len
, lenp1
, call
, type
;
8673 if (!validate_arg (dest
, POINTER_TYPE
)
8674 || !validate_arg (src
, POINTER_TYPE
))
8677 len
= c_strlen (src
, 1);
8679 || TREE_CODE (len
) != INTEGER_CST
)
8682 if (optimize_function_for_size_p (cfun
)
8683 /* If length is zero it's small enough. */
8684 && !integer_zerop (len
))
8687 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
8691 lenp1
= size_binop_loc (loc
, PLUS_EXPR
,
8692 fold_convert_loc (loc
, size_type_node
, len
),
8693 build_int_cst (size_type_node
, 1));
8694 /* We use dest twice in building our expression. Save it from
8695 multiple expansions. */
8696 dest
= builtin_save_expr (dest
);
8697 call
= build_call_expr_loc (loc
, fn
, 3, dest
, src
, lenp1
);
8699 type
= TREE_TYPE (TREE_TYPE (fndecl
));
8700 dest
= fold_build_pointer_plus_loc (loc
, dest
, len
);
8701 dest
= fold_convert_loc (loc
, type
, dest
);
8702 dest
= omit_one_operand_loc (loc
, type
, dest
, call
);
8706 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8707 arguments to the call, and TYPE is its return type.
8708 Return NULL_TREE if no simplification can be made. */
8711 fold_builtin_memchr (location_t loc
, tree arg1
, tree arg2
, tree len
, tree type
)
8713 if (!validate_arg (arg1
, POINTER_TYPE
)
8714 || !validate_arg (arg2
, INTEGER_TYPE
)
8715 || !validate_arg (len
, INTEGER_TYPE
))
8721 if (TREE_CODE (arg2
) != INTEGER_CST
8722 || !tree_fits_uhwi_p (len
))
8725 p1
= c_getstr (arg1
);
8726 if (p1
&& compare_tree_int (len
, strlen (p1
) + 1) <= 0)
8732 if (target_char_cast (arg2
, &c
))
8735 r
= (const char *) memchr (p1
, c
, tree_to_uhwi (len
));
8738 return build_int_cst (TREE_TYPE (arg1
), 0);
8740 tem
= fold_build_pointer_plus_hwi_loc (loc
, arg1
, r
- p1
);
8741 return fold_convert_loc (loc
, type
, tem
);
8747 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8748 Return NULL_TREE if no simplification can be made. */
8751 fold_builtin_memcmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
8753 const char *p1
, *p2
;
8755 if (!validate_arg (arg1
, POINTER_TYPE
)
8756 || !validate_arg (arg2
, POINTER_TYPE
)
8757 || !validate_arg (len
, INTEGER_TYPE
))
8760 /* If the LEN parameter is zero, return zero. */
8761 if (integer_zerop (len
))
8762 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
8765 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8766 if (operand_equal_p (arg1
, arg2
, 0))
8767 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
8769 p1
= c_getstr (arg1
);
8770 p2
= c_getstr (arg2
);
8772 /* If all arguments are constant, and the value of len is not greater
8773 than the lengths of arg1 and arg2, evaluate at compile-time. */
8774 if (tree_fits_uhwi_p (len
) && p1
&& p2
8775 && compare_tree_int (len
, strlen (p1
) + 1) <= 0
8776 && compare_tree_int (len
, strlen (p2
) + 1) <= 0)
8778 const int r
= memcmp (p1
, p2
, tree_to_uhwi (len
));
8781 return integer_one_node
;
8783 return integer_minus_one_node
;
8785 return integer_zero_node
;
8788 /* If len parameter is one, return an expression corresponding to
8789 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8790 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
8792 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8793 tree cst_uchar_ptr_node
8794 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8797 = fold_convert_loc (loc
, integer_type_node
,
8798 build1 (INDIRECT_REF
, cst_uchar_node
,
8799 fold_convert_loc (loc
,
8803 = fold_convert_loc (loc
, integer_type_node
,
8804 build1 (INDIRECT_REF
, cst_uchar_node
,
8805 fold_convert_loc (loc
,
8808 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8814 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8815 Return NULL_TREE if no simplification can be made. */
8818 fold_builtin_strcmp (location_t loc
, tree arg1
, tree arg2
)
8820 const char *p1
, *p2
;
8822 if (!validate_arg (arg1
, POINTER_TYPE
)
8823 || !validate_arg (arg2
, POINTER_TYPE
))
8826 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8827 if (operand_equal_p (arg1
, arg2
, 0))
8828 return integer_zero_node
;
8830 p1
= c_getstr (arg1
);
8831 p2
= c_getstr (arg2
);
8835 const int i
= strcmp (p1
, p2
);
8837 return integer_minus_one_node
;
8839 return integer_one_node
;
8841 return integer_zero_node
;
8844 /* If the second arg is "", return *(const unsigned char*)arg1. */
8845 if (p2
&& *p2
== '\0')
8847 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8848 tree cst_uchar_ptr_node
8849 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8851 return fold_convert_loc (loc
, integer_type_node
,
8852 build1 (INDIRECT_REF
, cst_uchar_node
,
8853 fold_convert_loc (loc
,
8858 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8859 if (p1
&& *p1
== '\0')
8861 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8862 tree cst_uchar_ptr_node
8863 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8866 = fold_convert_loc (loc
, integer_type_node
,
8867 build1 (INDIRECT_REF
, cst_uchar_node
,
8868 fold_convert_loc (loc
,
8871 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
8877 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8878 Return NULL_TREE if no simplification can be made. */
8881 fold_builtin_strncmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
8883 const char *p1
, *p2
;
8885 if (!validate_arg (arg1
, POINTER_TYPE
)
8886 || !validate_arg (arg2
, POINTER_TYPE
)
8887 || !validate_arg (len
, INTEGER_TYPE
))
8890 /* If the LEN parameter is zero, return zero. */
8891 if (integer_zerop (len
))
8892 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
8895 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8896 if (operand_equal_p (arg1
, arg2
, 0))
8897 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
8899 p1
= c_getstr (arg1
);
8900 p2
= c_getstr (arg2
);
8902 if (tree_fits_uhwi_p (len
) && p1
&& p2
)
8904 const int i
= strncmp (p1
, p2
, tree_to_uhwi (len
));
8906 return integer_one_node
;
8908 return integer_minus_one_node
;
8910 return integer_zero_node
;
8913 /* If the second arg is "", and the length is greater than zero,
8914 return *(const unsigned char*)arg1. */
8915 if (p2
&& *p2
== '\0'
8916 && TREE_CODE (len
) == INTEGER_CST
8917 && tree_int_cst_sgn (len
) == 1)
8919 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8920 tree cst_uchar_ptr_node
8921 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8923 return fold_convert_loc (loc
, integer_type_node
,
8924 build1 (INDIRECT_REF
, cst_uchar_node
,
8925 fold_convert_loc (loc
,
8930 /* If the first arg is "", and the length is greater than zero,
8931 return -*(const unsigned char*)arg2. */
8932 if (p1
&& *p1
== '\0'
8933 && TREE_CODE (len
) == INTEGER_CST
8934 && tree_int_cst_sgn (len
) == 1)
8936 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8937 tree cst_uchar_ptr_node
8938 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8940 tree temp
= fold_convert_loc (loc
, integer_type_node
,
8941 build1 (INDIRECT_REF
, cst_uchar_node
,
8942 fold_convert_loc (loc
,
8945 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
8948 /* If len parameter is one, return an expression corresponding to
8949 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8950 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
8952 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8953 tree cst_uchar_ptr_node
8954 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8956 tree ind1
= fold_convert_loc (loc
, integer_type_node
,
8957 build1 (INDIRECT_REF
, cst_uchar_node
,
8958 fold_convert_loc (loc
,
8961 tree ind2
= fold_convert_loc (loc
, integer_type_node
,
8962 build1 (INDIRECT_REF
, cst_uchar_node
,
8963 fold_convert_loc (loc
,
8966 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8972 /* Fold function call to builtin signbit, signbitf or signbitl with argument
8973 ARG. Return NULL_TREE if no simplification can be made. */
8976 fold_builtin_signbit (location_t loc
, tree arg
, tree type
)
8978 if (!validate_arg (arg
, REAL_TYPE
))
8981 /* If ARG is a compile-time constant, determine the result. */
8982 if (TREE_CODE (arg
) == REAL_CST
8983 && !TREE_OVERFLOW (arg
))
8987 c
= TREE_REAL_CST (arg
);
8988 return (REAL_VALUE_NEGATIVE (c
)
8989 ? build_one_cst (type
)
8990 : build_zero_cst (type
));
8993 /* If ARG is non-negative, the result is always zero. */
8994 if (tree_expr_nonnegative_p (arg
))
8995 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
8997 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8998 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg
))))
8999 return fold_convert (type
,
9000 fold_build2_loc (loc
, LT_EXPR
, boolean_type_node
, arg
,
9001 build_real (TREE_TYPE (arg
), dconst0
)));
9006 /* Fold function call to builtin copysign, copysignf or copysignl with
9007 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
9011 fold_builtin_copysign (location_t loc
, tree fndecl
,
9012 tree arg1
, tree arg2
, tree type
)
9016 if (!validate_arg (arg1
, REAL_TYPE
)
9017 || !validate_arg (arg2
, REAL_TYPE
))
9020 /* copysign(X,X) is X. */
9021 if (operand_equal_p (arg1
, arg2
, 0))
9022 return fold_convert_loc (loc
, type
, arg1
);
9024 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
9025 if (TREE_CODE (arg1
) == REAL_CST
9026 && TREE_CODE (arg2
) == REAL_CST
9027 && !TREE_OVERFLOW (arg1
)
9028 && !TREE_OVERFLOW (arg2
))
9030 REAL_VALUE_TYPE c1
, c2
;
9032 c1
= TREE_REAL_CST (arg1
);
9033 c2
= TREE_REAL_CST (arg2
);
9034 /* c1.sign := c2.sign. */
9035 real_copysign (&c1
, &c2
);
9036 return build_real (type
, c1
);
9039 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
9040 Remember to evaluate Y for side-effects. */
9041 if (tree_expr_nonnegative_p (arg2
))
9042 return omit_one_operand_loc (loc
, type
,
9043 fold_build1_loc (loc
, ABS_EXPR
, type
, arg1
),
9046 /* Strip sign changing operations for the first argument. */
9047 tem
= fold_strip_sign_ops (arg1
);
9049 return build_call_expr_loc (loc
, fndecl
, 2, tem
, arg2
);
9054 /* Fold a call to builtin isascii with argument ARG. */
9057 fold_builtin_isascii (location_t loc
, tree arg
)
9059 if (!validate_arg (arg
, INTEGER_TYPE
))
9063 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
9064 arg
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
9065 build_int_cst (integer_type_node
,
9066 ~ (unsigned HOST_WIDE_INT
) 0x7f));
9067 return fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
,
9068 arg
, integer_zero_node
);
9072 /* Fold a call to builtin toascii with argument ARG. */
9075 fold_builtin_toascii (location_t loc
, tree arg
)
9077 if (!validate_arg (arg
, INTEGER_TYPE
))
9080 /* Transform toascii(c) -> (c & 0x7f). */
9081 return fold_build2_loc (loc
, BIT_AND_EXPR
, integer_type_node
, arg
,
9082 build_int_cst (integer_type_node
, 0x7f));
9085 /* Fold a call to builtin isdigit with argument ARG. */
9088 fold_builtin_isdigit (location_t loc
, tree arg
)
9090 if (!validate_arg (arg
, INTEGER_TYPE
))
9094 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
9095 /* According to the C standard, isdigit is unaffected by locale.
9096 However, it definitely is affected by the target character set. */
9097 unsigned HOST_WIDE_INT target_digit0
9098 = lang_hooks
.to_target_charset ('0');
9100 if (target_digit0
== 0)
9103 arg
= fold_convert_loc (loc
, unsigned_type_node
, arg
);
9104 arg
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
9105 build_int_cst (unsigned_type_node
, target_digit0
));
9106 return fold_build2_loc (loc
, LE_EXPR
, integer_type_node
, arg
,
9107 build_int_cst (unsigned_type_node
, 9));
9111 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
9114 fold_builtin_fabs (location_t loc
, tree arg
, tree type
)
9116 if (!validate_arg (arg
, REAL_TYPE
))
9119 arg
= fold_convert_loc (loc
, type
, arg
);
9120 if (TREE_CODE (arg
) == REAL_CST
)
9121 return fold_abs_const (arg
, type
);
9122 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
9125 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
9128 fold_builtin_abs (location_t loc
, tree arg
, tree type
)
9130 if (!validate_arg (arg
, INTEGER_TYPE
))
9133 arg
= fold_convert_loc (loc
, type
, arg
);
9134 if (TREE_CODE (arg
) == INTEGER_CST
)
9135 return fold_abs_const (arg
, type
);
9136 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
9139 /* Fold a fma operation with arguments ARG[012]. */
9142 fold_fma (location_t loc ATTRIBUTE_UNUSED
,
9143 tree type
, tree arg0
, tree arg1
, tree arg2
)
9145 if (TREE_CODE (arg0
) == REAL_CST
9146 && TREE_CODE (arg1
) == REAL_CST
9147 && TREE_CODE (arg2
) == REAL_CST
)
9148 return do_mpfr_arg3 (arg0
, arg1
, arg2
, type
, mpfr_fma
);
9153 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
9156 fold_builtin_fma (location_t loc
, tree arg0
, tree arg1
, tree arg2
, tree type
)
9158 if (validate_arg (arg0
, REAL_TYPE
)
9159 && validate_arg (arg1
, REAL_TYPE
)
9160 && validate_arg (arg2
, REAL_TYPE
))
9162 tree tem
= fold_fma (loc
, type
, arg0
, arg1
, arg2
);
9166 /* ??? Only expand to FMA_EXPR if it's directly supported. */
9167 if (optab_handler (fma_optab
, TYPE_MODE (type
)) != CODE_FOR_nothing
)
9168 return fold_build3_loc (loc
, FMA_EXPR
, type
, arg0
, arg1
, arg2
);
9173 /* Fold a call to builtin fmin or fmax. */
9176 fold_builtin_fmin_fmax (location_t loc
, tree arg0
, tree arg1
,
9177 tree type
, bool max
)
9179 if (validate_arg (arg0
, REAL_TYPE
) && validate_arg (arg1
, REAL_TYPE
))
9181 /* Calculate the result when the argument is a constant. */
9182 tree res
= do_mpfr_arg2 (arg0
, arg1
, type
, (max
? mpfr_max
: mpfr_min
));
9187 /* If either argument is NaN, return the other one. Avoid the
9188 transformation if we get (and honor) a signalling NaN. Using
9189 omit_one_operand() ensures we create a non-lvalue. */
9190 if (TREE_CODE (arg0
) == REAL_CST
9191 && real_isnan (&TREE_REAL_CST (arg0
))
9192 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
9193 || ! TREE_REAL_CST (arg0
).signalling
))
9194 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
9195 if (TREE_CODE (arg1
) == REAL_CST
9196 && real_isnan (&TREE_REAL_CST (arg1
))
9197 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
9198 || ! TREE_REAL_CST (arg1
).signalling
))
9199 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9201 /* Transform fmin/fmax(x,x) -> x. */
9202 if (operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
9203 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9205 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9206 functions to return the numeric arg if the other one is NaN.
9207 These tree codes don't honor that, so only transform if
9208 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9209 handled, so we don't have to worry about it either. */
9210 if (flag_finite_math_only
)
9211 return fold_build2_loc (loc
, (max
? MAX_EXPR
: MIN_EXPR
), type
,
9212 fold_convert_loc (loc
, type
, arg0
),
9213 fold_convert_loc (loc
, type
, arg1
));
9218 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9221 fold_builtin_carg (location_t loc
, tree arg
, tree type
)
9223 if (validate_arg (arg
, COMPLEX_TYPE
)
9224 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
9226 tree atan2_fn
= mathfn_built_in (type
, BUILT_IN_ATAN2
);
9230 tree new_arg
= builtin_save_expr (arg
);
9231 tree r_arg
= fold_build1_loc (loc
, REALPART_EXPR
, type
, new_arg
);
9232 tree i_arg
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, new_arg
);
9233 return build_call_expr_loc (loc
, atan2_fn
, 2, i_arg
, r_arg
);
9240 /* Fold a call to builtin logb/ilogb. */
9243 fold_builtin_logb (location_t loc
, tree arg
, tree rettype
)
9245 if (! validate_arg (arg
, REAL_TYPE
))
9250 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_OVERFLOW (arg
))
9252 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg
);
9258 /* If arg is Inf or NaN and we're logb, return it. */
9259 if (TREE_CODE (rettype
) == REAL_TYPE
)
9261 /* For logb(-Inf) we have to return +Inf. */
9262 if (real_isinf (value
) && real_isneg (value
))
9264 REAL_VALUE_TYPE tem
;
9266 return build_real (rettype
, tem
);
9268 return fold_convert_loc (loc
, rettype
, arg
);
9270 /* Fall through... */
9272 /* Zero may set errno and/or raise an exception for logb, also
9273 for ilogb we don't know FP_ILOGB0. */
9276 /* For normal numbers, proceed iff radix == 2. In GCC,
9277 normalized significands are in the range [0.5, 1.0). We
9278 want the exponent as if they were [1.0, 2.0) so get the
9279 exponent and subtract 1. */
9280 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg
)))->b
== 2)
9281 return fold_convert_loc (loc
, rettype
,
9282 build_int_cst (integer_type_node
,
9283 REAL_EXP (value
)-1));
9291 /* Fold a call to builtin significand, if radix == 2. */
9294 fold_builtin_significand (location_t loc
, tree arg
, tree rettype
)
9296 if (! validate_arg (arg
, REAL_TYPE
))
9301 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_OVERFLOW (arg
))
9303 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg
);
9310 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9311 return fold_convert_loc (loc
, rettype
, arg
);
9313 /* For normal numbers, proceed iff radix == 2. */
9314 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg
)))->b
== 2)
9316 REAL_VALUE_TYPE result
= *value
;
9317 /* In GCC, normalized significands are in the range [0.5,
9318 1.0). We want them to be [1.0, 2.0) so set the
9320 SET_REAL_EXP (&result
, 1);
9321 return build_real (rettype
, result
);
9330 /* Fold a call to builtin frexp, we can assume the base is 2. */
9333 fold_builtin_frexp (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
9335 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
9340 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
9343 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
9345 /* Proceed if a valid pointer type was passed in. */
9346 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == integer_type_node
)
9348 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9354 /* For +-0, return (*exp = 0, +-0). */
9355 exp
= integer_zero_node
;
9360 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9361 return omit_one_operand_loc (loc
, rettype
, arg0
, arg1
);
9364 /* Since the frexp function always expects base 2, and in
9365 GCC normalized significands are already in the range
9366 [0.5, 1.0), we have exactly what frexp wants. */
9367 REAL_VALUE_TYPE frac_rvt
= *value
;
9368 SET_REAL_EXP (&frac_rvt
, 0);
9369 frac
= build_real (rettype
, frac_rvt
);
9370 exp
= build_int_cst (integer_type_node
, REAL_EXP (value
));
9377 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9378 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
, exp
);
9379 TREE_SIDE_EFFECTS (arg1
) = 1;
9380 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
, frac
);
9386 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9387 then we can assume the base is two. If it's false, then we have to
9388 check the mode of the TYPE parameter in certain cases. */
9391 fold_builtin_load_exponent (location_t loc
, tree arg0
, tree arg1
,
9392 tree type
, bool ldexp
)
9394 if (validate_arg (arg0
, REAL_TYPE
) && validate_arg (arg1
, INTEGER_TYPE
))
9399 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9400 if (real_zerop (arg0
) || integer_zerop (arg1
)
9401 || (TREE_CODE (arg0
) == REAL_CST
9402 && !real_isfinite (&TREE_REAL_CST (arg0
))))
9403 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9405 /* If both arguments are constant, then try to evaluate it. */
9406 if ((ldexp
|| REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2)
9407 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
9408 && tree_fits_shwi_p (arg1
))
9410 /* Bound the maximum adjustment to twice the range of the
9411 mode's valid exponents. Use abs to ensure the range is
9412 positive as a sanity check. */
9413 const long max_exp_adj
= 2 *
9414 labs (REAL_MODE_FORMAT (TYPE_MODE (type
))->emax
9415 - REAL_MODE_FORMAT (TYPE_MODE (type
))->emin
);
9417 /* Get the user-requested adjustment. */
9418 const HOST_WIDE_INT req_exp_adj
= tree_to_shwi (arg1
);
9420 /* The requested adjustment must be inside this range. This
9421 is a preliminary cap to avoid things like overflow, we
9422 may still fail to compute the result for other reasons. */
9423 if (-max_exp_adj
< req_exp_adj
&& req_exp_adj
< max_exp_adj
)
9425 REAL_VALUE_TYPE initial_result
;
9427 real_ldexp (&initial_result
, &TREE_REAL_CST (arg0
), req_exp_adj
);
9429 /* Ensure we didn't overflow. */
9430 if (! real_isinf (&initial_result
))
9432 const REAL_VALUE_TYPE trunc_result
9433 = real_value_truncate (TYPE_MODE (type
), initial_result
);
9435 /* Only proceed if the target mode can hold the
9437 if (REAL_VALUES_EQUAL (initial_result
, trunc_result
))
9438 return build_real (type
, trunc_result
);
9447 /* Fold a call to builtin modf. */
9450 fold_builtin_modf (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
9452 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
9457 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
9460 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
9462 /* Proceed if a valid pointer type was passed in. */
9463 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == TYPE_MAIN_VARIANT (rettype
))
9465 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9466 REAL_VALUE_TYPE trunc
, frac
;
9472 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9473 trunc
= frac
= *value
;
9476 /* For +-Inf, return (*arg1 = arg0, +-0). */
9478 frac
.sign
= value
->sign
;
9482 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9483 real_trunc (&trunc
, VOIDmode
, value
);
9484 real_arithmetic (&frac
, MINUS_EXPR
, value
, &trunc
);
9485 /* If the original number was negative and already
9486 integral, then the fractional part is -0.0. */
9487 if (value
->sign
&& frac
.cl
== rvc_zero
)
9488 frac
.sign
= value
->sign
;
9492 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9493 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
,
9494 build_real (rettype
, trunc
));
9495 TREE_SIDE_EFFECTS (arg1
) = 1;
9496 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
,
9497 build_real (rettype
, frac
));
9503 /* Given a location LOC, an interclass builtin function decl FNDECL
9504 and its single argument ARG, return an folded expression computing
9505 the same, or NULL_TREE if we either couldn't or didn't want to fold
9506 (the latter happen if there's an RTL instruction available). */
9509 fold_builtin_interclass_mathfn (location_t loc
, tree fndecl
, tree arg
)
9513 if (!validate_arg (arg
, REAL_TYPE
))
9516 if (interclass_mathfn_icode (arg
, fndecl
) != CODE_FOR_nothing
)
9519 mode
= TYPE_MODE (TREE_TYPE (arg
));
9521 /* If there is no optab, try generic code. */
9522 switch (DECL_FUNCTION_CODE (fndecl
))
9526 CASE_FLT_FN (BUILT_IN_ISINF
):
9528 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9529 tree
const isgr_fn
= builtin_decl_explicit (BUILT_IN_ISGREATER
);
9530 tree
const type
= TREE_TYPE (arg
);
9534 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9535 real_from_string (&r
, buf
);
9536 result
= build_call_expr (isgr_fn
, 2,
9537 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
9538 build_real (type
, r
));
9541 CASE_FLT_FN (BUILT_IN_FINITE
):
9542 case BUILT_IN_ISFINITE
:
9544 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9545 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
9546 tree
const type
= TREE_TYPE (arg
);
9550 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9551 real_from_string (&r
, buf
);
9552 result
= build_call_expr (isle_fn
, 2,
9553 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
9554 build_real (type
, r
));
9555 /*result = fold_build2_loc (loc, UNGT_EXPR,
9556 TREE_TYPE (TREE_TYPE (fndecl)),
9557 fold_build1_loc (loc, ABS_EXPR, type, arg),
9558 build_real (type, r));
9559 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9560 TREE_TYPE (TREE_TYPE (fndecl)),
9564 case BUILT_IN_ISNORMAL
:
9566 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9567 islessequal(fabs(x),DBL_MAX). */
9568 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
9569 tree
const isge_fn
= builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL
);
9570 tree
const type
= TREE_TYPE (arg
);
9571 REAL_VALUE_TYPE rmax
, rmin
;
9574 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9575 real_from_string (&rmax
, buf
);
9576 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
9577 real_from_string (&rmin
, buf
);
9578 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
9579 result
= build_call_expr (isle_fn
, 2, arg
,
9580 build_real (type
, rmax
));
9581 result
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, result
,
9582 build_call_expr (isge_fn
, 2, arg
,
9583 build_real (type
, rmin
)));
9593 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9594 ARG is the argument for the call. */
9597 fold_builtin_classify (location_t loc
, tree fndecl
, tree arg
, int builtin_index
)
9599 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9602 if (!validate_arg (arg
, REAL_TYPE
))
9605 switch (builtin_index
)
9607 case BUILT_IN_ISINF
:
9608 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
9609 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9611 if (TREE_CODE (arg
) == REAL_CST
)
9613 r
= TREE_REAL_CST (arg
);
9614 if (real_isinf (&r
))
9615 return real_compare (GT_EXPR
, &r
, &dconst0
)
9616 ? integer_one_node
: integer_minus_one_node
;
9618 return integer_zero_node
;
9623 case BUILT_IN_ISINF_SIGN
:
9625 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9626 /* In a boolean context, GCC will fold the inner COND_EXPR to
9627 1. So e.g. "if (isinf_sign(x))" would be folded to just
9628 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9629 tree signbit_fn
= mathfn_built_in_1 (TREE_TYPE (arg
), BUILT_IN_SIGNBIT
, 0);
9630 tree isinf_fn
= builtin_decl_explicit (BUILT_IN_ISINF
);
9631 tree tmp
= NULL_TREE
;
9633 arg
= builtin_save_expr (arg
);
9635 if (signbit_fn
&& isinf_fn
)
9637 tree signbit_call
= build_call_expr_loc (loc
, signbit_fn
, 1, arg
);
9638 tree isinf_call
= build_call_expr_loc (loc
, isinf_fn
, 1, arg
);
9640 signbit_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
9641 signbit_call
, integer_zero_node
);
9642 isinf_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
9643 isinf_call
, integer_zero_node
);
9645 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, signbit_call
,
9646 integer_minus_one_node
, integer_one_node
);
9647 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
9655 case BUILT_IN_ISFINITE
:
9656 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg
)))
9657 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
9658 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
9660 if (TREE_CODE (arg
) == REAL_CST
)
9662 r
= TREE_REAL_CST (arg
);
9663 return real_isfinite (&r
) ? integer_one_node
: integer_zero_node
;
9668 case BUILT_IN_ISNAN
:
9669 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg
))))
9670 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9672 if (TREE_CODE (arg
) == REAL_CST
)
9674 r
= TREE_REAL_CST (arg
);
9675 return real_isnan (&r
) ? integer_one_node
: integer_zero_node
;
9678 arg
= builtin_save_expr (arg
);
9679 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg
, arg
);
9686 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9687 This builtin will generate code to return the appropriate floating
9688 point classification depending on the value of the floating point
9689 number passed in. The possible return values must be supplied as
9690 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9691 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9692 one floating point argument which is "type generic". */
9695 fold_builtin_fpclassify (location_t loc
, tree
*args
, int nargs
)
9697 tree fp_nan
, fp_infinite
, fp_normal
, fp_subnormal
, fp_zero
,
9698 arg
, type
, res
, tmp
;
9703 /* Verify the required arguments in the original call. */
9705 || !validate_arg (args
[0], INTEGER_TYPE
)
9706 || !validate_arg (args
[1], INTEGER_TYPE
)
9707 || !validate_arg (args
[2], INTEGER_TYPE
)
9708 || !validate_arg (args
[3], INTEGER_TYPE
)
9709 || !validate_arg (args
[4], INTEGER_TYPE
)
9710 || !validate_arg (args
[5], REAL_TYPE
))
9714 fp_infinite
= args
[1];
9715 fp_normal
= args
[2];
9716 fp_subnormal
= args
[3];
9719 type
= TREE_TYPE (arg
);
9720 mode
= TYPE_MODE (type
);
9721 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
9725 (fabs(x) == Inf ? FP_INFINITE :
9726 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9727 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9729 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
9730 build_real (type
, dconst0
));
9731 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
9732 tmp
, fp_zero
, fp_subnormal
);
9734 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
9735 real_from_string (&r
, buf
);
9736 tmp
= fold_build2_loc (loc
, GE_EXPR
, integer_type_node
,
9737 arg
, build_real (type
, r
));
9738 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, fp_normal
, res
);
9740 if (HONOR_INFINITIES (mode
))
9743 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
9744 build_real (type
, r
));
9745 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
,
9749 if (HONOR_NANS (mode
))
9751 tmp
= fold_build2_loc (loc
, ORDERED_EXPR
, integer_type_node
, arg
, arg
);
9752 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, res
, fp_nan
);
9758 /* Fold a call to an unordered comparison function such as
9759 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9760 being called and ARG0 and ARG1 are the arguments for the call.
9761 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9762 the opposite of the desired result. UNORDERED_CODE is used
9763 for modes that can hold NaNs and ORDERED_CODE is used for
9767 fold_builtin_unordered_cmp (location_t loc
, tree fndecl
, tree arg0
, tree arg1
,
9768 enum tree_code unordered_code
,
9769 enum tree_code ordered_code
)
9771 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9772 enum tree_code code
;
9774 enum tree_code code0
, code1
;
9775 tree cmp_type
= NULL_TREE
;
9777 type0
= TREE_TYPE (arg0
);
9778 type1
= TREE_TYPE (arg1
);
9780 code0
= TREE_CODE (type0
);
9781 code1
= TREE_CODE (type1
);
9783 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
9784 /* Choose the wider of two real types. */
9785 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
9787 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
9789 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
9792 arg0
= fold_convert_loc (loc
, cmp_type
, arg0
);
9793 arg1
= fold_convert_loc (loc
, cmp_type
, arg1
);
9795 if (unordered_code
== UNORDERED_EXPR
)
9797 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
9798 return omit_two_operands_loc (loc
, type
, integer_zero_node
, arg0
, arg1
);
9799 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg0
, arg1
);
9802 code
= HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))) ? unordered_code
9804 return fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
,
9805 fold_build2_loc (loc
, code
, type
, arg0
, arg1
));
9808 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9809 arithmetics if it can never overflow, or into internal functions that
9810 return both result of arithmetics and overflowed boolean flag in
9811 a complex integer result, or some other check for overflow. */
9814 fold_builtin_arith_overflow (location_t loc
, enum built_in_function fcode
,
9815 tree arg0
, tree arg1
, tree arg2
)
9817 enum internal_fn ifn
= IFN_LAST
;
9818 tree type
= TREE_TYPE (TREE_TYPE (arg2
));
9819 tree mem_arg2
= build_fold_indirect_ref_loc (loc
, arg2
);
9822 case BUILT_IN_ADD_OVERFLOW
:
9823 case BUILT_IN_SADD_OVERFLOW
:
9824 case BUILT_IN_SADDL_OVERFLOW
:
9825 case BUILT_IN_SADDLL_OVERFLOW
:
9826 case BUILT_IN_UADD_OVERFLOW
:
9827 case BUILT_IN_UADDL_OVERFLOW
:
9828 case BUILT_IN_UADDLL_OVERFLOW
:
9829 ifn
= IFN_ADD_OVERFLOW
;
9831 case BUILT_IN_SUB_OVERFLOW
:
9832 case BUILT_IN_SSUB_OVERFLOW
:
9833 case BUILT_IN_SSUBL_OVERFLOW
:
9834 case BUILT_IN_SSUBLL_OVERFLOW
:
9835 case BUILT_IN_USUB_OVERFLOW
:
9836 case BUILT_IN_USUBL_OVERFLOW
:
9837 case BUILT_IN_USUBLL_OVERFLOW
:
9838 ifn
= IFN_SUB_OVERFLOW
;
9840 case BUILT_IN_MUL_OVERFLOW
:
9841 case BUILT_IN_SMUL_OVERFLOW
:
9842 case BUILT_IN_SMULL_OVERFLOW
:
9843 case BUILT_IN_SMULLL_OVERFLOW
:
9844 case BUILT_IN_UMUL_OVERFLOW
:
9845 case BUILT_IN_UMULL_OVERFLOW
:
9846 case BUILT_IN_UMULLL_OVERFLOW
:
9847 ifn
= IFN_MUL_OVERFLOW
;
9852 tree ctype
= build_complex_type (type
);
9853 tree call
= build_call_expr_internal_loc (loc
, ifn
, ctype
,
9855 tree tgt
= save_expr (call
);
9856 tree intres
= build1_loc (loc
, REALPART_EXPR
, type
, tgt
);
9857 tree ovfres
= build1_loc (loc
, IMAGPART_EXPR
, type
, tgt
);
9858 ovfres
= fold_convert_loc (loc
, boolean_type_node
, ovfres
);
9860 = fold_build2_loc (loc
, MODIFY_EXPR
, void_type_node
, mem_arg2
, intres
);
9861 return build2_loc (loc
, COMPOUND_EXPR
, boolean_type_node
, store
, ovfres
);
9864 /* Fold a call to built-in function FNDECL with 0 arguments.
9865 IGNORE is true if the result of the function call is ignored. This
9866 function returns NULL_TREE if no simplification was possible. */
9869 fold_builtin_0 (location_t loc
, tree fndecl
, bool ignore ATTRIBUTE_UNUSED
)
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 IGNORE is true if the result of the function call is ignored. This
9895 function returns NULL_TREE if no simplification was possible. */
9898 fold_builtin_1 (location_t loc
, tree fndecl
, tree arg0
, bool ignore
)
9900 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9901 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9904 case BUILT_IN_CONSTANT_P
:
9906 tree val
= fold_builtin_constant_p (arg0
);
9908 /* Gimplification will pull the CALL_EXPR for the builtin out of
9909 an if condition. When not optimizing, we'll not CSE it back.
9910 To avoid link error types of regressions, return false now. */
9911 if (!val
&& !optimize
)
9912 val
= integer_zero_node
;
9917 case BUILT_IN_CLASSIFY_TYPE
:
9918 return fold_builtin_classify_type (arg0
);
9920 case BUILT_IN_STRLEN
:
9921 return fold_builtin_strlen (loc
, type
, arg0
);
9923 CASE_FLT_FN (BUILT_IN_FABS
):
9924 case BUILT_IN_FABSD32
:
9925 case BUILT_IN_FABSD64
:
9926 case BUILT_IN_FABSD128
:
9927 return fold_builtin_fabs (loc
, arg0
, type
);
9931 case BUILT_IN_LLABS
:
9932 case BUILT_IN_IMAXABS
:
9933 return fold_builtin_abs (loc
, arg0
, type
);
9935 CASE_FLT_FN (BUILT_IN_CONJ
):
9936 if (validate_arg (arg0
, COMPLEX_TYPE
)
9937 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9938 return fold_build1_loc (loc
, CONJ_EXPR
, type
, arg0
);
9941 CASE_FLT_FN (BUILT_IN_CREAL
):
9942 if (validate_arg (arg0
, COMPLEX_TYPE
)
9943 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9944 return non_lvalue_loc (loc
, fold_build1_loc (loc
, REALPART_EXPR
, type
, arg0
));;
9947 CASE_FLT_FN (BUILT_IN_CIMAG
):
9948 if (validate_arg (arg0
, COMPLEX_TYPE
)
9949 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9950 return non_lvalue_loc (loc
, fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg0
));
9953 CASE_FLT_FN (BUILT_IN_CCOS
):
9954 return fold_builtin_ccos (loc
, arg0
, type
, fndecl
, /*hyper=*/ false);
9956 CASE_FLT_FN (BUILT_IN_CCOSH
):
9957 return fold_builtin_ccos (loc
, arg0
, type
, fndecl
, /*hyper=*/ true);
9959 CASE_FLT_FN (BUILT_IN_CPROJ
):
9960 return fold_builtin_cproj (loc
, arg0
, type
);
9962 CASE_FLT_FN (BUILT_IN_CSIN
):
9963 if (validate_arg (arg0
, COMPLEX_TYPE
)
9964 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9965 return do_mpc_arg1 (arg0
, type
, mpc_sin
);
9968 CASE_FLT_FN (BUILT_IN_CSINH
):
9969 if (validate_arg (arg0
, COMPLEX_TYPE
)
9970 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9971 return do_mpc_arg1 (arg0
, type
, mpc_sinh
);
9974 CASE_FLT_FN (BUILT_IN_CTAN
):
9975 if (validate_arg (arg0
, COMPLEX_TYPE
)
9976 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9977 return do_mpc_arg1 (arg0
, type
, mpc_tan
);
9980 CASE_FLT_FN (BUILT_IN_CTANH
):
9981 if (validate_arg (arg0
, COMPLEX_TYPE
)
9982 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9983 return do_mpc_arg1 (arg0
, type
, mpc_tanh
);
9986 CASE_FLT_FN (BUILT_IN_CLOG
):
9987 if (validate_arg (arg0
, COMPLEX_TYPE
)
9988 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9989 return do_mpc_arg1 (arg0
, type
, mpc_log
);
9992 CASE_FLT_FN (BUILT_IN_CSQRT
):
9993 if (validate_arg (arg0
, COMPLEX_TYPE
)
9994 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9995 return do_mpc_arg1 (arg0
, type
, mpc_sqrt
);
9998 CASE_FLT_FN (BUILT_IN_CASIN
):
9999 if (validate_arg (arg0
, COMPLEX_TYPE
)
10000 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10001 return do_mpc_arg1 (arg0
, type
, mpc_asin
);
10004 CASE_FLT_FN (BUILT_IN_CACOS
):
10005 if (validate_arg (arg0
, COMPLEX_TYPE
)
10006 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10007 return do_mpc_arg1 (arg0
, type
, mpc_acos
);
10010 CASE_FLT_FN (BUILT_IN_CATAN
):
10011 if (validate_arg (arg0
, COMPLEX_TYPE
)
10012 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10013 return do_mpc_arg1 (arg0
, type
, mpc_atan
);
10016 CASE_FLT_FN (BUILT_IN_CASINH
):
10017 if (validate_arg (arg0
, COMPLEX_TYPE
)
10018 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10019 return do_mpc_arg1 (arg0
, type
, mpc_asinh
);
10022 CASE_FLT_FN (BUILT_IN_CACOSH
):
10023 if (validate_arg (arg0
, COMPLEX_TYPE
)
10024 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10025 return do_mpc_arg1 (arg0
, type
, mpc_acosh
);
10028 CASE_FLT_FN (BUILT_IN_CATANH
):
10029 if (validate_arg (arg0
, COMPLEX_TYPE
)
10030 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
10031 return do_mpc_arg1 (arg0
, type
, mpc_atanh
);
10034 CASE_FLT_FN (BUILT_IN_CABS
):
10035 return fold_builtin_cabs (loc
, arg0
, type
, fndecl
);
10037 CASE_FLT_FN (BUILT_IN_CARG
):
10038 return fold_builtin_carg (loc
, arg0
, type
);
10040 CASE_FLT_FN (BUILT_IN_SQRT
):
10041 return fold_builtin_sqrt (loc
, arg0
, type
);
10043 CASE_FLT_FN (BUILT_IN_CBRT
):
10044 return fold_builtin_cbrt (loc
, arg0
, type
);
10046 CASE_FLT_FN (BUILT_IN_ASIN
):
10047 if (validate_arg (arg0
, REAL_TYPE
))
10048 return do_mpfr_arg1 (arg0
, type
, mpfr_asin
,
10049 &dconstm1
, &dconst1
, true);
10052 CASE_FLT_FN (BUILT_IN_ACOS
):
10053 if (validate_arg (arg0
, REAL_TYPE
))
10054 return do_mpfr_arg1 (arg0
, type
, mpfr_acos
,
10055 &dconstm1
, &dconst1
, true);
10058 CASE_FLT_FN (BUILT_IN_ATAN
):
10059 if (validate_arg (arg0
, REAL_TYPE
))
10060 return do_mpfr_arg1 (arg0
, type
, mpfr_atan
, NULL
, NULL
, 0);
10063 CASE_FLT_FN (BUILT_IN_ASINH
):
10064 if (validate_arg (arg0
, REAL_TYPE
))
10065 return do_mpfr_arg1 (arg0
, type
, mpfr_asinh
, NULL
, NULL
, 0);
10068 CASE_FLT_FN (BUILT_IN_ACOSH
):
10069 if (validate_arg (arg0
, REAL_TYPE
))
10070 return do_mpfr_arg1 (arg0
, type
, mpfr_acosh
,
10071 &dconst1
, NULL
, true);
10074 CASE_FLT_FN (BUILT_IN_ATANH
):
10075 if (validate_arg (arg0
, REAL_TYPE
))
10076 return do_mpfr_arg1 (arg0
, type
, mpfr_atanh
,
10077 &dconstm1
, &dconst1
, false);
10080 CASE_FLT_FN (BUILT_IN_SIN
):
10081 if (validate_arg (arg0
, REAL_TYPE
))
10082 return do_mpfr_arg1 (arg0
, type
, mpfr_sin
, NULL
, NULL
, 0);
10085 CASE_FLT_FN (BUILT_IN_COS
):
10086 return fold_builtin_cos (loc
, arg0
, type
, fndecl
);
10088 CASE_FLT_FN (BUILT_IN_TAN
):
10089 return fold_builtin_tan (arg0
, type
);
10091 CASE_FLT_FN (BUILT_IN_CEXP
):
10092 return fold_builtin_cexp (loc
, arg0
, type
);
10094 CASE_FLT_FN (BUILT_IN_CEXPI
):
10095 if (validate_arg (arg0
, REAL_TYPE
))
10096 return do_mpfr_sincos (arg0
, NULL_TREE
, NULL_TREE
);
10099 CASE_FLT_FN (BUILT_IN_SINH
):
10100 if (validate_arg (arg0
, REAL_TYPE
))
10101 return do_mpfr_arg1 (arg0
, type
, mpfr_sinh
, NULL
, NULL
, 0);
10104 CASE_FLT_FN (BUILT_IN_COSH
):
10105 return fold_builtin_cosh (loc
, arg0
, type
, fndecl
);
10107 CASE_FLT_FN (BUILT_IN_TANH
):
10108 if (validate_arg (arg0
, REAL_TYPE
))
10109 return do_mpfr_arg1 (arg0
, type
, mpfr_tanh
, NULL
, NULL
, 0);
10112 CASE_FLT_FN (BUILT_IN_ERF
):
10113 if (validate_arg (arg0
, REAL_TYPE
))
10114 return do_mpfr_arg1 (arg0
, type
, mpfr_erf
, NULL
, NULL
, 0);
10117 CASE_FLT_FN (BUILT_IN_ERFC
):
10118 if (validate_arg (arg0
, REAL_TYPE
))
10119 return do_mpfr_arg1 (arg0
, type
, mpfr_erfc
, NULL
, NULL
, 0);
10122 CASE_FLT_FN (BUILT_IN_TGAMMA
):
10123 if (validate_arg (arg0
, REAL_TYPE
))
10124 return do_mpfr_arg1 (arg0
, type
, mpfr_gamma
, NULL
, NULL
, 0);
10127 CASE_FLT_FN (BUILT_IN_EXP
):
10128 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp
);
10130 CASE_FLT_FN (BUILT_IN_EXP2
):
10131 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp2
);
10133 CASE_FLT_FN (BUILT_IN_EXP10
):
10134 CASE_FLT_FN (BUILT_IN_POW10
):
10135 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp10
);
10137 CASE_FLT_FN (BUILT_IN_EXPM1
):
10138 if (validate_arg (arg0
, REAL_TYPE
))
10139 return do_mpfr_arg1 (arg0
, type
, mpfr_expm1
, NULL
, NULL
, 0);
10142 CASE_FLT_FN (BUILT_IN_LOG
):
10143 if (validate_arg (arg0
, REAL_TYPE
))
10144 return do_mpfr_arg1 (arg0
, type
, mpfr_log
, &dconst0
, NULL
, false);
10147 CASE_FLT_FN (BUILT_IN_LOG2
):
10148 if (validate_arg (arg0
, REAL_TYPE
))
10149 return do_mpfr_arg1 (arg0
, type
, mpfr_log2
, &dconst0
, NULL
, false);
10152 CASE_FLT_FN (BUILT_IN_LOG10
):
10153 if (validate_arg (arg0
, REAL_TYPE
))
10154 return do_mpfr_arg1 (arg0
, type
, mpfr_log10
, &dconst0
, NULL
, false);
10157 CASE_FLT_FN (BUILT_IN_LOG1P
):
10158 if (validate_arg (arg0
, REAL_TYPE
))
10159 return do_mpfr_arg1 (arg0
, type
, mpfr_log1p
,
10160 &dconstm1
, NULL
, false);
10163 CASE_FLT_FN (BUILT_IN_J0
):
10164 if (validate_arg (arg0
, REAL_TYPE
))
10165 return do_mpfr_arg1 (arg0
, type
, mpfr_j0
,
10169 CASE_FLT_FN (BUILT_IN_J1
):
10170 if (validate_arg (arg0
, REAL_TYPE
))
10171 return do_mpfr_arg1 (arg0
, type
, mpfr_j1
,
10175 CASE_FLT_FN (BUILT_IN_Y0
):
10176 if (validate_arg (arg0
, REAL_TYPE
))
10177 return do_mpfr_arg1 (arg0
, type
, mpfr_y0
,
10178 &dconst0
, NULL
, false);
10181 CASE_FLT_FN (BUILT_IN_Y1
):
10182 if (validate_arg (arg0
, REAL_TYPE
))
10183 return do_mpfr_arg1 (arg0
, type
, mpfr_y1
,
10184 &dconst0
, NULL
, false);
10187 CASE_FLT_FN (BUILT_IN_NAN
):
10188 case BUILT_IN_NAND32
:
10189 case BUILT_IN_NAND64
:
10190 case BUILT_IN_NAND128
:
10191 return fold_builtin_nan (arg0
, type
, true);
10193 CASE_FLT_FN (BUILT_IN_NANS
):
10194 return fold_builtin_nan (arg0
, type
, false);
10196 CASE_FLT_FN (BUILT_IN_FLOOR
):
10197 return fold_builtin_floor (loc
, fndecl
, arg0
);
10199 CASE_FLT_FN (BUILT_IN_CEIL
):
10200 return fold_builtin_ceil (loc
, fndecl
, arg0
);
10202 CASE_FLT_FN (BUILT_IN_TRUNC
):
10203 return fold_builtin_trunc (loc
, fndecl
, arg0
);
10205 CASE_FLT_FN (BUILT_IN_ROUND
):
10206 return fold_builtin_round (loc
, fndecl
, arg0
);
10208 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
10209 CASE_FLT_FN (BUILT_IN_RINT
):
10210 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg0
);
10212 CASE_FLT_FN (BUILT_IN_ICEIL
):
10213 CASE_FLT_FN (BUILT_IN_LCEIL
):
10214 CASE_FLT_FN (BUILT_IN_LLCEIL
):
10215 CASE_FLT_FN (BUILT_IN_LFLOOR
):
10216 CASE_FLT_FN (BUILT_IN_IFLOOR
):
10217 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
10218 CASE_FLT_FN (BUILT_IN_IROUND
):
10219 CASE_FLT_FN (BUILT_IN_LROUND
):
10220 CASE_FLT_FN (BUILT_IN_LLROUND
):
10221 return fold_builtin_int_roundingfn (loc
, fndecl
, arg0
);
10223 CASE_FLT_FN (BUILT_IN_IRINT
):
10224 CASE_FLT_FN (BUILT_IN_LRINT
):
10225 CASE_FLT_FN (BUILT_IN_LLRINT
):
10226 return fold_fixed_mathfn (loc
, fndecl
, arg0
);
10228 case BUILT_IN_BSWAP16
:
10229 case BUILT_IN_BSWAP32
:
10230 case BUILT_IN_BSWAP64
:
10231 return fold_builtin_bswap (fndecl
, arg0
);
10233 CASE_INT_FN (BUILT_IN_FFS
):
10234 CASE_INT_FN (BUILT_IN_CLZ
):
10235 CASE_INT_FN (BUILT_IN_CTZ
):
10236 CASE_INT_FN (BUILT_IN_CLRSB
):
10237 CASE_INT_FN (BUILT_IN_POPCOUNT
):
10238 CASE_INT_FN (BUILT_IN_PARITY
):
10239 return fold_builtin_bitop (fndecl
, arg0
);
10241 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
10242 return fold_builtin_signbit (loc
, arg0
, type
);
10244 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
10245 return fold_builtin_significand (loc
, arg0
, type
);
10247 CASE_FLT_FN (BUILT_IN_ILOGB
):
10248 CASE_FLT_FN (BUILT_IN_LOGB
):
10249 return fold_builtin_logb (loc
, arg0
, type
);
10251 case BUILT_IN_ISASCII
:
10252 return fold_builtin_isascii (loc
, arg0
);
10254 case BUILT_IN_TOASCII
:
10255 return fold_builtin_toascii (loc
, arg0
);
10257 case BUILT_IN_ISDIGIT
:
10258 return fold_builtin_isdigit (loc
, arg0
);
10260 CASE_FLT_FN (BUILT_IN_FINITE
):
10261 case BUILT_IN_FINITED32
:
10262 case BUILT_IN_FINITED64
:
10263 case BUILT_IN_FINITED128
:
10264 case BUILT_IN_ISFINITE
:
10266 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISFINITE
);
10269 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10272 CASE_FLT_FN (BUILT_IN_ISINF
):
10273 case BUILT_IN_ISINFD32
:
10274 case BUILT_IN_ISINFD64
:
10275 case BUILT_IN_ISINFD128
:
10277 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF
);
10280 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10283 case BUILT_IN_ISNORMAL
:
10284 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10286 case BUILT_IN_ISINF_SIGN
:
10287 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF_SIGN
);
10289 CASE_FLT_FN (BUILT_IN_ISNAN
):
10290 case BUILT_IN_ISNAND32
:
10291 case BUILT_IN_ISNAND64
:
10292 case BUILT_IN_ISNAND128
:
10293 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISNAN
);
10295 case BUILT_IN_PRINTF
:
10296 case BUILT_IN_PRINTF_UNLOCKED
:
10297 case BUILT_IN_VPRINTF
:
10298 return fold_builtin_printf (loc
, fndecl
, arg0
, NULL_TREE
, ignore
, fcode
);
10300 case BUILT_IN_FREE
:
10301 if (integer_zerop (arg0
))
10302 return build_empty_stmt (loc
);
10313 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10314 IGNORE is true if the result of the function call is ignored. This
10315 function returns NULL_TREE if no simplification was possible. */
10318 fold_builtin_2 (location_t loc
, tree fndecl
, tree arg0
, tree arg1
, bool ignore
)
10320 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10321 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10325 CASE_FLT_FN (BUILT_IN_JN
):
10326 if (validate_arg (arg0
, INTEGER_TYPE
)
10327 && validate_arg (arg1
, REAL_TYPE
))
10328 return do_mpfr_bessel_n (arg0
, arg1
, type
, mpfr_jn
, NULL
, 0);
10331 CASE_FLT_FN (BUILT_IN_YN
):
10332 if (validate_arg (arg0
, INTEGER_TYPE
)
10333 && validate_arg (arg1
, REAL_TYPE
))
10334 return do_mpfr_bessel_n (arg0
, arg1
, type
, mpfr_yn
,
10338 CASE_FLT_FN (BUILT_IN_DREM
):
10339 CASE_FLT_FN (BUILT_IN_REMAINDER
):
10340 if (validate_arg (arg0
, REAL_TYPE
)
10341 && validate_arg (arg1
, REAL_TYPE
))
10342 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_remainder
);
10345 CASE_FLT_FN_REENT (BUILT_IN_GAMMA
): /* GAMMA_R */
10346 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA
): /* LGAMMA_R */
10347 if (validate_arg (arg0
, REAL_TYPE
)
10348 && validate_arg (arg1
, POINTER_TYPE
))
10349 return do_mpfr_lgamma_r (arg0
, arg1
, type
);
10352 CASE_FLT_FN (BUILT_IN_ATAN2
):
10353 if (validate_arg (arg0
, REAL_TYPE
)
10354 && validate_arg (arg1
, REAL_TYPE
))
10355 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_atan2
);
10358 CASE_FLT_FN (BUILT_IN_FDIM
):
10359 if (validate_arg (arg0
, REAL_TYPE
)
10360 && validate_arg (arg1
, REAL_TYPE
))
10361 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_dim
);
10364 CASE_FLT_FN (BUILT_IN_HYPOT
):
10365 return fold_builtin_hypot (loc
, fndecl
, arg0
, arg1
, type
);
10367 CASE_FLT_FN (BUILT_IN_CPOW
):
10368 if (validate_arg (arg0
, COMPLEX_TYPE
)
10369 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
10370 && validate_arg (arg1
, COMPLEX_TYPE
)
10371 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
)
10372 return do_mpc_arg2 (arg0
, arg1
, type
, /*do_nonfinite=*/ 0, mpc_pow
);
10375 CASE_FLT_FN (BUILT_IN_LDEXP
):
10376 return fold_builtin_load_exponent (loc
, arg0
, arg1
, type
, /*ldexp=*/true);
10377 CASE_FLT_FN (BUILT_IN_SCALBN
):
10378 CASE_FLT_FN (BUILT_IN_SCALBLN
):
10379 return fold_builtin_load_exponent (loc
, arg0
, arg1
,
10380 type
, /*ldexp=*/false);
10382 CASE_FLT_FN (BUILT_IN_FREXP
):
10383 return fold_builtin_frexp (loc
, arg0
, arg1
, type
);
10385 CASE_FLT_FN (BUILT_IN_MODF
):
10386 return fold_builtin_modf (loc
, arg0
, arg1
, type
);
10388 case BUILT_IN_STRSTR
:
10389 return fold_builtin_strstr (loc
, arg0
, arg1
, type
);
10391 case BUILT_IN_STRSPN
:
10392 return fold_builtin_strspn (loc
, arg0
, arg1
);
10394 case BUILT_IN_STRCSPN
:
10395 return fold_builtin_strcspn (loc
, arg0
, arg1
);
10397 case BUILT_IN_STRCHR
:
10398 case BUILT_IN_INDEX
:
10399 return fold_builtin_strchr (loc
, arg0
, arg1
, type
);
10401 case BUILT_IN_STRRCHR
:
10402 case BUILT_IN_RINDEX
:
10403 return fold_builtin_strrchr (loc
, arg0
, arg1
, type
);
10405 case BUILT_IN_STPCPY
:
10408 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
10412 return build_call_expr_loc (loc
, fn
, 2, arg0
, arg1
);
10415 return fold_builtin_stpcpy (loc
, fndecl
, arg0
, arg1
);
10418 case BUILT_IN_STRCMP
:
10419 return fold_builtin_strcmp (loc
, arg0
, arg1
);
10421 case BUILT_IN_STRPBRK
:
10422 return fold_builtin_strpbrk (loc
, arg0
, arg1
, type
);
10424 case BUILT_IN_EXPECT
:
10425 return fold_builtin_expect (loc
, arg0
, arg1
, NULL_TREE
);
10427 CASE_FLT_FN (BUILT_IN_POW
):
10428 return fold_builtin_pow (loc
, fndecl
, arg0
, arg1
, type
);
10430 CASE_FLT_FN (BUILT_IN_POWI
):
10431 return fold_builtin_powi (loc
, fndecl
, arg0
, arg1
, type
);
10433 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
10434 return fold_builtin_copysign (loc
, fndecl
, arg0
, arg1
, type
);
10436 CASE_FLT_FN (BUILT_IN_FMIN
):
10437 return fold_builtin_fmin_fmax (loc
, arg0
, arg1
, type
, /*max=*/false);
10439 CASE_FLT_FN (BUILT_IN_FMAX
):
10440 return fold_builtin_fmin_fmax (loc
, arg0
, arg1
, type
, /*max=*/true);
10442 case BUILT_IN_ISGREATER
:
10443 return fold_builtin_unordered_cmp (loc
, fndecl
,
10444 arg0
, arg1
, UNLE_EXPR
, LE_EXPR
);
10445 case BUILT_IN_ISGREATEREQUAL
:
10446 return fold_builtin_unordered_cmp (loc
, fndecl
,
10447 arg0
, arg1
, UNLT_EXPR
, LT_EXPR
);
10448 case BUILT_IN_ISLESS
:
10449 return fold_builtin_unordered_cmp (loc
, fndecl
,
10450 arg0
, arg1
, UNGE_EXPR
, GE_EXPR
);
10451 case BUILT_IN_ISLESSEQUAL
:
10452 return fold_builtin_unordered_cmp (loc
, fndecl
,
10453 arg0
, arg1
, UNGT_EXPR
, GT_EXPR
);
10454 case BUILT_IN_ISLESSGREATER
:
10455 return fold_builtin_unordered_cmp (loc
, fndecl
,
10456 arg0
, arg1
, UNEQ_EXPR
, EQ_EXPR
);
10457 case BUILT_IN_ISUNORDERED
:
10458 return fold_builtin_unordered_cmp (loc
, fndecl
,
10459 arg0
, arg1
, UNORDERED_EXPR
,
10462 /* We do the folding for va_start in the expander. */
10463 case BUILT_IN_VA_START
:
10466 case BUILT_IN_OBJECT_SIZE
:
10467 return fold_builtin_object_size (arg0
, arg1
);
10469 case BUILT_IN_PRINTF
:
10470 case BUILT_IN_PRINTF_UNLOCKED
:
10471 case BUILT_IN_VPRINTF
:
10472 return fold_builtin_printf (loc
, fndecl
, arg0
, arg1
, ignore
, fcode
);
10474 case BUILT_IN_PRINTF_CHK
:
10475 case BUILT_IN_VPRINTF_CHK
:
10476 if (!validate_arg (arg0
, INTEGER_TYPE
)
10477 || TREE_SIDE_EFFECTS (arg0
))
10480 return fold_builtin_printf (loc
, fndecl
,
10481 arg1
, NULL_TREE
, ignore
, fcode
);
10484 case BUILT_IN_FPRINTF
:
10485 case BUILT_IN_FPRINTF_UNLOCKED
:
10486 case BUILT_IN_VFPRINTF
:
10487 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg1
, NULL_TREE
,
10490 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
10491 return fold_builtin_atomic_always_lock_free (arg0
, arg1
);
10493 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
10494 return fold_builtin_atomic_is_lock_free (arg0
, arg1
);
10502 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10503 and ARG2. IGNORE is true if the result of the function call is ignored.
10504 This function returns NULL_TREE if no simplification was possible. */
10507 fold_builtin_3 (location_t loc
, tree fndecl
,
10508 tree arg0
, tree arg1
, tree arg2
, bool ignore
)
10510 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10511 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10515 CASE_FLT_FN (BUILT_IN_SINCOS
):
10516 return fold_builtin_sincos (loc
, arg0
, arg1
, arg2
);
10518 CASE_FLT_FN (BUILT_IN_FMA
):
10519 return fold_builtin_fma (loc
, arg0
, arg1
, arg2
, type
);
10522 CASE_FLT_FN (BUILT_IN_REMQUO
):
10523 if (validate_arg (arg0
, REAL_TYPE
)
10524 && validate_arg (arg1
, REAL_TYPE
)
10525 && validate_arg (arg2
, POINTER_TYPE
))
10526 return do_mpfr_remquo (arg0
, arg1
, arg2
);
10529 case BUILT_IN_STRNCAT
:
10530 return fold_builtin_strncat (loc
, arg0
, arg1
, arg2
);
10532 case BUILT_IN_STRNCMP
:
10533 return fold_builtin_strncmp (loc
, arg0
, arg1
, arg2
);
10535 case BUILT_IN_MEMCHR
:
10536 return fold_builtin_memchr (loc
, arg0
, arg1
, arg2
, type
);
10538 case BUILT_IN_BCMP
:
10539 case BUILT_IN_MEMCMP
:
10540 return fold_builtin_memcmp (loc
, arg0
, arg1
, arg2
);;
10542 case BUILT_IN_PRINTF_CHK
:
10543 case BUILT_IN_VPRINTF_CHK
:
10544 if (!validate_arg (arg0
, INTEGER_TYPE
)
10545 || TREE_SIDE_EFFECTS (arg0
))
10548 return fold_builtin_printf (loc
, fndecl
, arg1
, arg2
, ignore
, fcode
);
10551 case BUILT_IN_FPRINTF
:
10552 case BUILT_IN_FPRINTF_UNLOCKED
:
10553 case BUILT_IN_VFPRINTF
:
10554 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg1
, arg2
,
10557 case BUILT_IN_FPRINTF_CHK
:
10558 case BUILT_IN_VFPRINTF_CHK
:
10559 if (!validate_arg (arg1
, INTEGER_TYPE
)
10560 || TREE_SIDE_EFFECTS (arg1
))
10563 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg2
, NULL_TREE
,
10566 case BUILT_IN_EXPECT
:
10567 return fold_builtin_expect (loc
, arg0
, arg1
, arg2
);
10569 case BUILT_IN_ADD_OVERFLOW
:
10570 case BUILT_IN_SUB_OVERFLOW
:
10571 case BUILT_IN_MUL_OVERFLOW
:
10572 case BUILT_IN_SADD_OVERFLOW
:
10573 case BUILT_IN_SADDL_OVERFLOW
:
10574 case BUILT_IN_SADDLL_OVERFLOW
:
10575 case BUILT_IN_SSUB_OVERFLOW
:
10576 case BUILT_IN_SSUBL_OVERFLOW
:
10577 case BUILT_IN_SSUBLL_OVERFLOW
:
10578 case BUILT_IN_SMUL_OVERFLOW
:
10579 case BUILT_IN_SMULL_OVERFLOW
:
10580 case BUILT_IN_SMULLL_OVERFLOW
:
10581 case BUILT_IN_UADD_OVERFLOW
:
10582 case BUILT_IN_UADDL_OVERFLOW
:
10583 case BUILT_IN_UADDLL_OVERFLOW
:
10584 case BUILT_IN_USUB_OVERFLOW
:
10585 case BUILT_IN_USUBL_OVERFLOW
:
10586 case BUILT_IN_USUBLL_OVERFLOW
:
10587 case BUILT_IN_UMUL_OVERFLOW
:
10588 case BUILT_IN_UMULL_OVERFLOW
:
10589 case BUILT_IN_UMULLL_OVERFLOW
:
10590 return fold_builtin_arith_overflow (loc
, fcode
, arg0
, arg1
, arg2
);
10598 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10599 ARG2, and ARG3. IGNORE is true if the result of the function call is
10600 ignored. This function returns NULL_TREE if no simplification was
10604 fold_builtin_4 (location_t loc
, tree fndecl
,
10605 tree arg0
, tree arg1
, tree arg2
, tree arg3
, bool ignore
)
10607 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10611 case BUILT_IN_FPRINTF_CHK
:
10612 case BUILT_IN_VFPRINTF_CHK
:
10613 if (!validate_arg (arg1
, INTEGER_TYPE
)
10614 || TREE_SIDE_EFFECTS (arg1
))
10617 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg2
, arg3
,
10627 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10628 arguments. IGNORE is true if the result of the
10629 function call is ignored. This function returns NULL_TREE if no
10630 simplification was possible. */
10633 fold_builtin_n (location_t loc
, tree fndecl
, tree
*args
, int nargs
, bool ignore
)
10635 tree ret
= NULL_TREE
;
10640 ret
= fold_builtin_0 (loc
, fndecl
, ignore
);
10643 ret
= fold_builtin_1 (loc
, fndecl
, args
[0], ignore
);
10646 ret
= fold_builtin_2 (loc
, fndecl
, args
[0], args
[1], ignore
);
10649 ret
= fold_builtin_3 (loc
, fndecl
, args
[0], args
[1], args
[2], ignore
);
10652 ret
= fold_builtin_4 (loc
, fndecl
, args
[0], args
[1], args
[2], args
[3],
10656 ret
= fold_builtin_varargs (loc
, fndecl
, args
, nargs
, ignore
);
10661 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
10662 SET_EXPR_LOCATION (ret
, loc
);
10663 TREE_NO_WARNING (ret
) = 1;
10669 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10670 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10671 of arguments in ARGS to be omitted. OLDNARGS is the number of
10672 elements in ARGS. */
10675 rewrite_call_expr_valist (location_t loc
, int oldnargs
, tree
*args
,
10676 int skip
, tree fndecl
, int n
, va_list newargs
)
10678 int nargs
= oldnargs
- skip
+ n
;
10685 buffer
= XALLOCAVEC (tree
, nargs
);
10686 for (i
= 0; i
< n
; i
++)
10687 buffer
[i
] = va_arg (newargs
, tree
);
10688 for (j
= skip
; j
< oldnargs
; j
++, i
++)
10689 buffer
[i
] = args
[j
];
10692 buffer
= args
+ skip
;
10694 return build_call_expr_loc_array (loc
, fndecl
, nargs
, buffer
);
10697 /* Return true if FNDECL shouldn't be folded right now.
10698 If a built-in function has an inline attribute always_inline
10699 wrapper, defer folding it after always_inline functions have
10700 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10701 might not be performed. */
10704 avoid_folding_inline_builtin (tree fndecl
)
10706 return (DECL_DECLARED_INLINE_P (fndecl
)
10707 && DECL_DISREGARD_INLINE_LIMITS (fndecl
)
10709 && !cfun
->always_inline_functions_inlined
10710 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl
)));
10713 /* A wrapper function for builtin folding that prevents warnings for
10714 "statement without effect" and the like, caused by removing the
10715 call node earlier than the warning is generated. */
10718 fold_call_expr (location_t loc
, tree exp
, bool ignore
)
10720 tree ret
= NULL_TREE
;
10721 tree fndecl
= get_callee_fndecl (exp
);
10723 && TREE_CODE (fndecl
) == FUNCTION_DECL
10724 && DECL_BUILT_IN (fndecl
)
10725 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10726 yet. Defer folding until we see all the arguments
10727 (after inlining). */
10728 && !CALL_EXPR_VA_ARG_PACK (exp
))
10730 int nargs
= call_expr_nargs (exp
);
10732 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10733 instead last argument is __builtin_va_arg_pack (). Defer folding
10734 even in that case, until arguments are finalized. */
10735 if (nargs
&& TREE_CODE (CALL_EXPR_ARG (exp
, nargs
- 1)) == CALL_EXPR
)
10737 tree fndecl2
= get_callee_fndecl (CALL_EXPR_ARG (exp
, nargs
- 1));
10739 && TREE_CODE (fndecl2
) == FUNCTION_DECL
10740 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
10741 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
10745 if (avoid_folding_inline_builtin (fndecl
))
10748 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
10749 return targetm
.fold_builtin (fndecl
, call_expr_nargs (exp
),
10750 CALL_EXPR_ARGP (exp
), ignore
);
10753 tree
*args
= CALL_EXPR_ARGP (exp
);
10754 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
10762 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
10763 N arguments are passed in the array ARGARRAY. Return a folded
10764 expression or NULL_TREE if no simplification was possible. */
10767 fold_builtin_call_array (location_t loc
, tree
,
10772 if (TREE_CODE (fn
) != ADDR_EXPR
)
10775 tree fndecl
= TREE_OPERAND (fn
, 0);
10776 if (TREE_CODE (fndecl
) == FUNCTION_DECL
10777 && DECL_BUILT_IN (fndecl
))
10779 /* If last argument is __builtin_va_arg_pack (), arguments to this
10780 function are not finalized yet. Defer folding until they are. */
10781 if (n
&& TREE_CODE (argarray
[n
- 1]) == CALL_EXPR
)
10783 tree fndecl2
= get_callee_fndecl (argarray
[n
- 1]);
10785 && TREE_CODE (fndecl2
) == FUNCTION_DECL
10786 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
10787 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
10790 if (avoid_folding_inline_builtin (fndecl
))
10792 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
10793 return targetm
.fold_builtin (fndecl
, n
, argarray
, false);
10795 return fold_builtin_n (loc
, fndecl
, argarray
, n
, false);
10801 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10802 along with N new arguments specified as the "..." parameters. SKIP
10803 is the number of arguments in EXP to be omitted. This function is used
10804 to do varargs-to-varargs transformations. */
10807 rewrite_call_expr (location_t loc
, tree exp
, int skip
, tree fndecl
, int n
, ...)
10813 t
= rewrite_call_expr_valist (loc
, call_expr_nargs (exp
),
10814 CALL_EXPR_ARGP (exp
), skip
, fndecl
, n
, ap
);
10820 /* Validate a single argument ARG against a tree code CODE representing
10824 validate_arg (const_tree arg
, enum tree_code code
)
10828 else if (code
== POINTER_TYPE
)
10829 return POINTER_TYPE_P (TREE_TYPE (arg
));
10830 else if (code
== INTEGER_TYPE
)
10831 return INTEGRAL_TYPE_P (TREE_TYPE (arg
));
10832 return code
== TREE_CODE (TREE_TYPE (arg
));
10835 /* This function validates the types of a function call argument list
10836 against a specified list of tree_codes. If the last specifier is a 0,
10837 that represents an ellipses, otherwise the last specifier must be a
10840 This is the GIMPLE version of validate_arglist. Eventually we want to
10841 completely convert builtins.c to work from GIMPLEs and the tree based
10842 validate_arglist will then be removed. */
10845 validate_gimple_arglist (const gcall
*call
, ...)
10847 enum tree_code code
;
10853 va_start (ap
, call
);
10858 code
= (enum tree_code
) va_arg (ap
, int);
10862 /* This signifies an ellipses, any further arguments are all ok. */
10866 /* This signifies an endlink, if no arguments remain, return
10867 true, otherwise return false. */
10868 res
= (i
== gimple_call_num_args (call
));
10871 /* If no parameters remain or the parameter's code does not
10872 match the specified code, return false. Otherwise continue
10873 checking any remaining arguments. */
10874 arg
= gimple_call_arg (call
, i
++);
10875 if (!validate_arg (arg
, code
))
10882 /* We need gotos here since we can only have one VA_CLOSE in a
10890 /* Default target-specific builtin expander that does nothing. */
10893 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
10894 rtx target ATTRIBUTE_UNUSED
,
10895 rtx subtarget ATTRIBUTE_UNUSED
,
10896 machine_mode mode ATTRIBUTE_UNUSED
,
10897 int ignore ATTRIBUTE_UNUSED
)
10902 /* Returns true is EXP represents data that would potentially reside
10903 in a readonly section. */
10906 readonly_data_expr (tree exp
)
10910 if (TREE_CODE (exp
) != ADDR_EXPR
)
10913 exp
= get_base_address (TREE_OPERAND (exp
, 0));
10917 /* Make sure we call decl_readonly_section only for trees it
10918 can handle (since it returns true for everything it doesn't
10920 if (TREE_CODE (exp
) == STRING_CST
10921 || TREE_CODE (exp
) == CONSTRUCTOR
10922 || (TREE_CODE (exp
) == VAR_DECL
&& TREE_STATIC (exp
)))
10923 return decl_readonly_section (exp
, 0);
10928 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10929 to the call, and TYPE is its return type.
10931 Return NULL_TREE if no simplification was possible, otherwise return the
10932 simplified form of the call as a tree.
10934 The simplified form may be a constant or other expression which
10935 computes the same value, but in a more efficient manner (including
10936 calls to other builtin functions).
10938 The call may contain arguments which need to be evaluated, but
10939 which are not useful to determine the result of the call. In
10940 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10941 COMPOUND_EXPR will be an argument which must be evaluated.
10942 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10943 COMPOUND_EXPR in the chain will contain the tree for the simplified
10944 form of the builtin function call. */
10947 fold_builtin_strstr (location_t loc
, tree s1
, tree s2
, tree type
)
10949 if (!validate_arg (s1
, POINTER_TYPE
)
10950 || !validate_arg (s2
, POINTER_TYPE
))
10955 const char *p1
, *p2
;
10957 p2
= c_getstr (s2
);
10961 p1
= c_getstr (s1
);
10964 const char *r
= strstr (p1
, p2
);
10968 return build_int_cst (TREE_TYPE (s1
), 0);
10970 /* Return an offset into the constant string argument. */
10971 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
10972 return fold_convert_loc (loc
, type
, tem
);
10975 /* The argument is const char *, and the result is char *, so we need
10976 a type conversion here to avoid a warning. */
10978 return fold_convert_loc (loc
, type
, s1
);
10983 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
10987 /* New argument list transforming strstr(s1, s2) to
10988 strchr(s1, s2[0]). */
10989 return build_call_expr_loc (loc
, fn
, 2, s1
,
10990 build_int_cst (integer_type_node
, p2
[0]));
10994 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10995 the call, and TYPE is its return type.
10997 Return NULL_TREE if no simplification was possible, otherwise return the
10998 simplified form of the call as a tree.
11000 The simplified form may be a constant or other expression which
11001 computes the same value, but in a more efficient manner (including
11002 calls to other builtin functions).
11004 The call may contain arguments which need to be evaluated, but
11005 which are not useful to determine the result of the call. In
11006 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11007 COMPOUND_EXPR will be an argument which must be evaluated.
11008 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11009 COMPOUND_EXPR in the chain will contain the tree for the simplified
11010 form of the builtin function call. */
11013 fold_builtin_strchr (location_t loc
, tree s1
, tree s2
, tree type
)
11015 if (!validate_arg (s1
, POINTER_TYPE
)
11016 || !validate_arg (s2
, INTEGER_TYPE
))
11022 if (TREE_CODE (s2
) != INTEGER_CST
)
11025 p1
= c_getstr (s1
);
11032 if (target_char_cast (s2
, &c
))
11035 r
= strchr (p1
, c
);
11038 return build_int_cst (TREE_TYPE (s1
), 0);
11040 /* Return an offset into the constant string argument. */
11041 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11042 return fold_convert_loc (loc
, type
, tem
);
11048 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
11049 the call, and TYPE is its return type.
11051 Return NULL_TREE if no simplification was possible, otherwise return the
11052 simplified form of the call as a tree.
11054 The simplified form may be a constant or other expression which
11055 computes the same value, but in a more efficient manner (including
11056 calls to other builtin functions).
11058 The call may contain arguments which need to be evaluated, but
11059 which are not useful to determine the result of the call. In
11060 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11061 COMPOUND_EXPR will be an argument which must be evaluated.
11062 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11063 COMPOUND_EXPR in the chain will contain the tree for the simplified
11064 form of the builtin function call. */
11067 fold_builtin_strrchr (location_t loc
, tree s1
, tree s2
, tree type
)
11069 if (!validate_arg (s1
, POINTER_TYPE
)
11070 || !validate_arg (s2
, INTEGER_TYPE
))
11077 if (TREE_CODE (s2
) != INTEGER_CST
)
11080 p1
= c_getstr (s1
);
11087 if (target_char_cast (s2
, &c
))
11090 r
= strrchr (p1
, c
);
11093 return build_int_cst (TREE_TYPE (s1
), 0);
11095 /* Return an offset into the constant string argument. */
11096 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11097 return fold_convert_loc (loc
, type
, tem
);
11100 if (! integer_zerop (s2
))
11103 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
11107 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
11108 return build_call_expr_loc (loc
, fn
, 2, s1
, s2
);
11112 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
11113 to the call, and TYPE is its return type.
11115 Return NULL_TREE if no simplification was possible, otherwise return the
11116 simplified form of the call as a tree.
11118 The simplified form may be a constant or other expression which
11119 computes the same value, but in a more efficient manner (including
11120 calls to other builtin functions).
11122 The call may contain arguments which need to be evaluated, but
11123 which are not useful to determine the result of the call. In
11124 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11125 COMPOUND_EXPR will be an argument which must be evaluated.
11126 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11127 COMPOUND_EXPR in the chain will contain the tree for the simplified
11128 form of the builtin function call. */
11131 fold_builtin_strpbrk (location_t loc
, tree s1
, tree s2
, tree type
)
11133 if (!validate_arg (s1
, POINTER_TYPE
)
11134 || !validate_arg (s2
, POINTER_TYPE
))
11139 const char *p1
, *p2
;
11141 p2
= c_getstr (s2
);
11145 p1
= c_getstr (s1
);
11148 const char *r
= strpbrk (p1
, p2
);
11152 return build_int_cst (TREE_TYPE (s1
), 0);
11154 /* Return an offset into the constant string argument. */
11155 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
11156 return fold_convert_loc (loc
, type
, tem
);
11160 /* strpbrk(x, "") == NULL.
11161 Evaluate and ignore s1 in case it had side-effects. */
11162 return omit_one_operand_loc (loc
, TREE_TYPE (s1
), integer_zero_node
, s1
);
11165 return NULL_TREE
; /* Really call strpbrk. */
11167 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
11171 /* New argument list transforming strpbrk(s1, s2) to
11172 strchr(s1, s2[0]). */
11173 return build_call_expr_loc (loc
, fn
, 2, s1
,
11174 build_int_cst (integer_type_node
, p2
[0]));
11178 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
11179 arguments to the call.
11181 Return NULL_TREE if no simplification was possible, otherwise return the
11182 simplified form of the call as a tree.
11184 The simplified form may be a constant or other expression which
11185 computes the same value, but in a more efficient manner (including
11186 calls to other builtin functions).
11188 The call may contain arguments which need to be evaluated, but
11189 which are not useful to determine the result of the call. In
11190 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11191 COMPOUND_EXPR will be an argument which must be evaluated.
11192 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11193 COMPOUND_EXPR in the chain will contain the tree for the simplified
11194 form of the builtin function call. */
11197 fold_builtin_strncat (location_t loc
, tree dst
, tree src
, tree len
)
11199 if (!validate_arg (dst
, POINTER_TYPE
)
11200 || !validate_arg (src
, POINTER_TYPE
)
11201 || !validate_arg (len
, INTEGER_TYPE
))
11205 const char *p
= c_getstr (src
);
11207 /* If the requested length is zero, or the src parameter string
11208 length is zero, return the dst parameter. */
11209 if (integer_zerop (len
) || (p
&& *p
== '\0'))
11210 return omit_two_operands_loc (loc
, TREE_TYPE (dst
), dst
, src
, len
);
11212 /* If the requested len is greater than or equal to the string
11213 length, call strcat. */
11214 if (TREE_CODE (len
) == INTEGER_CST
&& p
11215 && compare_tree_int (len
, strlen (p
)) >= 0)
11217 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
11219 /* If the replacement _DECL isn't initialized, don't do the
11224 return build_call_expr_loc (loc
, fn
, 2, dst
, src
);
11230 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11233 Return NULL_TREE if no simplification was possible, otherwise return the
11234 simplified form of the call as a tree.
11236 The simplified form may be a constant or other expression which
11237 computes the same value, but in a more efficient manner (including
11238 calls to other builtin functions).
11240 The call may contain arguments which need to be evaluated, but
11241 which are not useful to determine the result of the call. In
11242 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11243 COMPOUND_EXPR will be an argument which must be evaluated.
11244 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11245 COMPOUND_EXPR in the chain will contain the tree for the simplified
11246 form of the builtin function call. */
11249 fold_builtin_strspn (location_t loc
, tree s1
, tree s2
)
11251 if (!validate_arg (s1
, POINTER_TYPE
)
11252 || !validate_arg (s2
, POINTER_TYPE
))
11256 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
11258 /* If both arguments are constants, evaluate at compile-time. */
11261 const size_t r
= strspn (p1
, p2
);
11262 return build_int_cst (size_type_node
, r
);
11265 /* If either argument is "", return NULL_TREE. */
11266 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
11267 /* Evaluate and ignore both arguments in case either one has
11269 return omit_two_operands_loc (loc
, size_type_node
, size_zero_node
,
11275 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11278 Return NULL_TREE if no simplification was possible, otherwise return the
11279 simplified form of the call as a tree.
11281 The simplified form may be a constant or other expression which
11282 computes the same value, but in a more efficient manner (including
11283 calls to other builtin functions).
11285 The call may contain arguments which need to be evaluated, but
11286 which are not useful to determine the result of the call. In
11287 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11288 COMPOUND_EXPR will be an argument which must be evaluated.
11289 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11290 COMPOUND_EXPR in the chain will contain the tree for the simplified
11291 form of the builtin function call. */
11294 fold_builtin_strcspn (location_t loc
, tree s1
, tree s2
)
11296 if (!validate_arg (s1
, POINTER_TYPE
)
11297 || !validate_arg (s2
, POINTER_TYPE
))
11301 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
11303 /* If both arguments are constants, evaluate at compile-time. */
11306 const size_t r
= strcspn (p1
, p2
);
11307 return build_int_cst (size_type_node
, r
);
11310 /* If the first argument is "", return NULL_TREE. */
11311 if (p1
&& *p1
== '\0')
11313 /* Evaluate and ignore argument s2 in case it has
11315 return omit_one_operand_loc (loc
, size_type_node
,
11316 size_zero_node
, s2
);
11319 /* If the second argument is "", return __builtin_strlen(s1). */
11320 if (p2
&& *p2
== '\0')
11322 tree fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
11324 /* If the replacement _DECL isn't initialized, don't do the
11329 return build_call_expr_loc (loc
, fn
, 1, s1
);
11335 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11336 produced. False otherwise. This is done so that we don't output the error
11337 or warning twice or three times. */
11340 fold_builtin_next_arg (tree exp
, bool va_start_p
)
11342 tree fntype
= TREE_TYPE (current_function_decl
);
11343 int nargs
= call_expr_nargs (exp
);
11345 /* There is good chance the current input_location points inside the
11346 definition of the va_start macro (perhaps on the token for
11347 builtin) in a system header, so warnings will not be emitted.
11348 Use the location in real source code. */
11349 source_location current_location
=
11350 linemap_unwind_to_first_non_reserved_loc (line_table
, input_location
,
11353 if (!stdarg_p (fntype
))
11355 error ("%<va_start%> used in function with fixed args");
11361 if (va_start_p
&& (nargs
!= 2))
11363 error ("wrong number of arguments to function %<va_start%>");
11366 arg
= CALL_EXPR_ARG (exp
, 1);
11368 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11369 when we checked the arguments and if needed issued a warning. */
11374 /* Evidently an out of date version of <stdarg.h>; can't validate
11375 va_start's second argument, but can still work as intended. */
11376 warning_at (current_location
,
11378 "%<__builtin_next_arg%> called without an argument");
11381 else if (nargs
> 1)
11383 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11386 arg
= CALL_EXPR_ARG (exp
, 0);
11389 if (TREE_CODE (arg
) == SSA_NAME
)
11390 arg
= SSA_NAME_VAR (arg
);
11392 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11393 or __builtin_next_arg (0) the first time we see it, after checking
11394 the arguments and if needed issuing a warning. */
11395 if (!integer_zerop (arg
))
11397 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
11399 /* Strip off all nops for the sake of the comparison. This
11400 is not quite the same as STRIP_NOPS. It does more.
11401 We must also strip off INDIRECT_EXPR for C++ reference
11403 while (CONVERT_EXPR_P (arg
)
11404 || TREE_CODE (arg
) == INDIRECT_REF
)
11405 arg
= TREE_OPERAND (arg
, 0);
11406 if (arg
!= last_parm
)
11408 /* FIXME: Sometimes with the tree optimizers we can get the
11409 not the last argument even though the user used the last
11410 argument. We just warn and set the arg to be the last
11411 argument so that we will get wrong-code because of
11413 warning_at (current_location
,
11415 "second parameter of %<va_start%> not last named argument");
11418 /* Undefined by C99 7.15.1.4p4 (va_start):
11419 "If the parameter parmN is declared with the register storage
11420 class, with a function or array type, or with a type that is
11421 not compatible with the type that results after application of
11422 the default argument promotions, the behavior is undefined."
11424 else if (DECL_REGISTER (arg
))
11426 warning_at (current_location
,
11428 "undefined behaviour when second parameter of "
11429 "%<va_start%> is declared with %<register%> storage");
11432 /* We want to verify the second parameter just once before the tree
11433 optimizers are run and then avoid keeping it in the tree,
11434 as otherwise we could warn even for correct code like:
11435 void foo (int i, ...)
11436 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11438 CALL_EXPR_ARG (exp
, 1) = integer_zero_node
;
11440 CALL_EXPR_ARG (exp
, 0) = integer_zero_node
;
11446 /* Expand a call EXP to __builtin_object_size. */
11449 expand_builtin_object_size (tree exp
)
11452 int object_size_type
;
11453 tree fndecl
= get_callee_fndecl (exp
);
11455 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
11457 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11459 expand_builtin_trap ();
11463 ost
= CALL_EXPR_ARG (exp
, 1);
11466 if (TREE_CODE (ost
) != INTEGER_CST
11467 || tree_int_cst_sgn (ost
) < 0
11468 || compare_tree_int (ost
, 3) > 0)
11470 error ("%Klast argument of %D is not integer constant between 0 and 3",
11472 expand_builtin_trap ();
11476 object_size_type
= tree_to_shwi (ost
);
11478 return object_size_type
< 2 ? constm1_rtx
: const0_rtx
;
11481 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11482 FCODE is the BUILT_IN_* to use.
11483 Return NULL_RTX if we failed; the caller should emit a normal call,
11484 otherwise try to get the result in TARGET, if convenient (and in
11485 mode MODE if that's convenient). */
11488 expand_builtin_memory_chk (tree exp
, rtx target
, machine_mode mode
,
11489 enum built_in_function fcode
)
11491 tree dest
, src
, len
, size
;
11493 if (!validate_arglist (exp
,
11495 fcode
== BUILT_IN_MEMSET_CHK
11496 ? INTEGER_TYPE
: POINTER_TYPE
,
11497 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
11500 dest
= CALL_EXPR_ARG (exp
, 0);
11501 src
= CALL_EXPR_ARG (exp
, 1);
11502 len
= CALL_EXPR_ARG (exp
, 2);
11503 size
= CALL_EXPR_ARG (exp
, 3);
11505 if (! tree_fits_uhwi_p (size
))
11508 if (tree_fits_uhwi_p (len
) || integer_all_onesp (size
))
11512 if (! integer_all_onesp (size
) && tree_int_cst_lt (size
, len
))
11514 warning_at (tree_nonartificial_location (exp
),
11515 0, "%Kcall to %D will always overflow destination buffer",
11516 exp
, get_callee_fndecl (exp
));
11521 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11522 mem{cpy,pcpy,move,set} is available. */
11525 case BUILT_IN_MEMCPY_CHK
:
11526 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
11528 case BUILT_IN_MEMPCPY_CHK
:
11529 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
11531 case BUILT_IN_MEMMOVE_CHK
:
11532 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
11534 case BUILT_IN_MEMSET_CHK
:
11535 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
11544 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 3, dest
, src
, len
);
11545 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
11546 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
11547 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
11549 else if (fcode
== BUILT_IN_MEMSET_CHK
)
11553 unsigned int dest_align
= get_pointer_alignment (dest
);
11555 /* If DEST is not a pointer type, call the normal function. */
11556 if (dest_align
== 0)
11559 /* If SRC and DEST are the same (and not volatile), do nothing. */
11560 if (operand_equal_p (src
, dest
, 0))
11564 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
11566 /* Evaluate and ignore LEN in case it has side-effects. */
11567 expand_expr (len
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
11568 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
11571 expr
= fold_build_pointer_plus (dest
, len
);
11572 return expand_expr (expr
, target
, mode
, EXPAND_NORMAL
);
11575 /* __memmove_chk special case. */
11576 if (fcode
== BUILT_IN_MEMMOVE_CHK
)
11578 unsigned int src_align
= get_pointer_alignment (src
);
11580 if (src_align
== 0)
11583 /* If src is categorized for a readonly section we can use
11584 normal __memcpy_chk. */
11585 if (readonly_data_expr (src
))
11587 tree fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
11590 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 4,
11591 dest
, src
, len
, size
);
11592 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
11593 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
11594 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
11601 /* Emit warning if a buffer overflow is detected at compile time. */
11604 maybe_emit_chk_warning (tree exp
, enum built_in_function fcode
)
11608 location_t loc
= tree_nonartificial_location (exp
);
11612 case BUILT_IN_STRCPY_CHK
:
11613 case BUILT_IN_STPCPY_CHK
:
11614 /* For __strcat_chk the warning will be emitted only if overflowing
11615 by at least strlen (dest) + 1 bytes. */
11616 case BUILT_IN_STRCAT_CHK
:
11617 len
= CALL_EXPR_ARG (exp
, 1);
11618 size
= CALL_EXPR_ARG (exp
, 2);
11621 case BUILT_IN_STRNCAT_CHK
:
11622 case BUILT_IN_STRNCPY_CHK
:
11623 case BUILT_IN_STPNCPY_CHK
:
11624 len
= CALL_EXPR_ARG (exp
, 2);
11625 size
= CALL_EXPR_ARG (exp
, 3);
11627 case BUILT_IN_SNPRINTF_CHK
:
11628 case BUILT_IN_VSNPRINTF_CHK
:
11629 len
= CALL_EXPR_ARG (exp
, 1);
11630 size
= CALL_EXPR_ARG (exp
, 3);
11633 gcc_unreachable ();
11639 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
11644 len
= c_strlen (len
, 1);
11645 if (! len
|| ! tree_fits_uhwi_p (len
) || tree_int_cst_lt (len
, size
))
11648 else if (fcode
== BUILT_IN_STRNCAT_CHK
)
11650 tree src
= CALL_EXPR_ARG (exp
, 1);
11651 if (! src
|| ! tree_fits_uhwi_p (len
) || tree_int_cst_lt (len
, size
))
11653 src
= c_strlen (src
, 1);
11654 if (! src
|| ! tree_fits_uhwi_p (src
))
11656 warning_at (loc
, 0, "%Kcall to %D might overflow destination buffer",
11657 exp
, get_callee_fndecl (exp
));
11660 else if (tree_int_cst_lt (src
, size
))
11663 else if (! tree_fits_uhwi_p (len
) || ! tree_int_cst_lt (size
, len
))
11666 warning_at (loc
, 0, "%Kcall to %D will always overflow destination buffer",
11667 exp
, get_callee_fndecl (exp
));
11670 /* Emit warning if a buffer overflow is detected at compile time
11671 in __sprintf_chk/__vsprintf_chk calls. */
11674 maybe_emit_sprintf_chk_warning (tree exp
, enum built_in_function fcode
)
11676 tree size
, len
, fmt
;
11677 const char *fmt_str
;
11678 int nargs
= call_expr_nargs (exp
);
11680 /* Verify the required arguments in the original call. */
11684 size
= CALL_EXPR_ARG (exp
, 2);
11685 fmt
= CALL_EXPR_ARG (exp
, 3);
11687 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
11690 /* Check whether the format is a literal string constant. */
11691 fmt_str
= c_getstr (fmt
);
11692 if (fmt_str
== NULL
)
11695 if (!init_target_chars ())
11698 /* If the format doesn't contain % args or %%, we know its size. */
11699 if (strchr (fmt_str
, target_percent
) == 0)
11700 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
11701 /* If the format is "%s" and first ... argument is a string literal,
11703 else if (fcode
== BUILT_IN_SPRINTF_CHK
11704 && strcmp (fmt_str
, target_percent_s
) == 0)
11710 arg
= CALL_EXPR_ARG (exp
, 4);
11711 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
11714 len
= c_strlen (arg
, 1);
11715 if (!len
|| ! tree_fits_uhwi_p (len
))
11721 if (! tree_int_cst_lt (len
, size
))
11722 warning_at (tree_nonartificial_location (exp
),
11723 0, "%Kcall to %D will always overflow destination buffer",
11724 exp
, get_callee_fndecl (exp
));
11727 /* Emit warning if a free is called with address of a variable. */
11730 maybe_emit_free_warning (tree exp
)
11732 tree arg
= CALL_EXPR_ARG (exp
, 0);
11735 if (TREE_CODE (arg
) != ADDR_EXPR
)
11738 arg
= get_base_address (TREE_OPERAND (arg
, 0));
11739 if (arg
== NULL
|| INDIRECT_REF_P (arg
) || TREE_CODE (arg
) == MEM_REF
)
11742 if (SSA_VAR_P (arg
))
11743 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
11744 "%Kattempt to free a non-heap object %qD", exp
, arg
);
11746 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
11747 "%Kattempt to free a non-heap object", exp
);
11750 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11754 fold_builtin_object_size (tree ptr
, tree ost
)
11756 unsigned HOST_WIDE_INT bytes
;
11757 int object_size_type
;
11759 if (!validate_arg (ptr
, POINTER_TYPE
)
11760 || !validate_arg (ost
, INTEGER_TYPE
))
11765 if (TREE_CODE (ost
) != INTEGER_CST
11766 || tree_int_cst_sgn (ost
) < 0
11767 || compare_tree_int (ost
, 3) > 0)
11770 object_size_type
= tree_to_shwi (ost
);
11772 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11773 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11774 and (size_t) 0 for types 2 and 3. */
11775 if (TREE_SIDE_EFFECTS (ptr
))
11776 return build_int_cst_type (size_type_node
, object_size_type
< 2 ? -1 : 0);
11778 if (TREE_CODE (ptr
) == ADDR_EXPR
)
11780 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
11781 if (wi::fits_to_tree_p (bytes
, size_type_node
))
11782 return build_int_cstu (size_type_node
, bytes
);
11784 else if (TREE_CODE (ptr
) == SSA_NAME
)
11786 /* If object size is not known yet, delay folding until
11787 later. Maybe subsequent passes will help determining
11789 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
11790 if (bytes
!= (unsigned HOST_WIDE_INT
) (object_size_type
< 2 ? -1 : 0)
11791 && wi::fits_to_tree_p (bytes
, size_type_node
))
11792 return build_int_cstu (size_type_node
, bytes
);
11798 /* Builtins with folding operations that operate on "..." arguments
11799 need special handling; we need to store the arguments in a convenient
11800 data structure before attempting any folding. Fortunately there are
11801 only a few builtins that fall into this category. FNDECL is the
11802 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11803 result of the function call is ignored. */
11806 fold_builtin_varargs (location_t loc
, tree fndecl
, tree
*args
, int nargs
,
11807 bool ignore ATTRIBUTE_UNUSED
)
11809 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
11810 tree ret
= NULL_TREE
;
11814 case BUILT_IN_FPCLASSIFY
:
11815 ret
= fold_builtin_fpclassify (loc
, args
, nargs
);
11823 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
11824 SET_EXPR_LOCATION (ret
, loc
);
11825 TREE_NO_WARNING (ret
) = 1;
11831 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11832 FMT and ARG are the arguments to the call; we don't fold cases with
11833 more than 2 arguments, and ARG may be null if this is a 1-argument case.
11835 Return NULL_TREE if no simplification was possible, otherwise return the
11836 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11837 code of the function to be simplified. */
11840 fold_builtin_printf (location_t loc
, tree fndecl
, tree fmt
,
11841 tree arg
, bool ignore
,
11842 enum built_in_function fcode
)
11844 tree fn_putchar
, fn_puts
, newarg
, call
= NULL_TREE
;
11845 const char *fmt_str
= NULL
;
11847 /* If the return value is used, don't do the transformation. */
11851 /* Verify the required arguments in the original call. */
11852 if (!validate_arg (fmt
, POINTER_TYPE
))
11855 /* Check whether the format is a literal string constant. */
11856 fmt_str
= c_getstr (fmt
);
11857 if (fmt_str
== NULL
)
11860 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
11862 /* If we're using an unlocked function, assume the other
11863 unlocked functions exist explicitly. */
11864 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
11865 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
11869 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
11870 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
11873 if (!init_target_chars ())
11876 if (strcmp (fmt_str
, target_percent_s
) == 0
11877 || strchr (fmt_str
, target_percent
) == NULL
)
11881 if (strcmp (fmt_str
, target_percent_s
) == 0)
11883 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
11886 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
11889 str
= c_getstr (arg
);
11895 /* The format specifier doesn't contain any '%' characters. */
11896 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
11902 /* If the string was "", printf does nothing. */
11903 if (str
[0] == '\0')
11904 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
11906 /* If the string has length of 1, call putchar. */
11907 if (str
[1] == '\0')
11909 /* Given printf("c"), (where c is any one character,)
11910 convert "c"[0] to an int and pass that to the replacement
11912 newarg
= build_int_cst (integer_type_node
, str
[0]);
11914 call
= build_call_expr_loc (loc
, fn_putchar
, 1, newarg
);
11918 /* If the string was "string\n", call puts("string"). */
11919 size_t len
= strlen (str
);
11920 if ((unsigned char)str
[len
- 1] == target_newline
11921 && (size_t) (int) len
== len
11925 tree offset_node
, string_cst
;
11927 /* Create a NUL-terminated string that's one char shorter
11928 than the original, stripping off the trailing '\n'. */
11929 newarg
= build_string_literal (len
, str
);
11930 string_cst
= string_constant (newarg
, &offset_node
);
11931 gcc_checking_assert (string_cst
11932 && (TREE_STRING_LENGTH (string_cst
)
11934 && integer_zerop (offset_node
)
11936 TREE_STRING_POINTER (string_cst
)[len
- 1]
11937 == target_newline
);
11938 /* build_string_literal creates a new STRING_CST,
11939 modify it in place to avoid double copying. */
11940 newstr
= CONST_CAST (char *, TREE_STRING_POINTER (string_cst
));
11941 newstr
[len
- 1] = '\0';
11943 call
= build_call_expr_loc (loc
, fn_puts
, 1, newarg
);
11946 /* We'd like to arrange to call fputs(string,stdout) here,
11947 but we need stdout and don't have a way to get it yet. */
11952 /* The other optimizations can be done only on the non-va_list variants. */
11953 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
11956 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11957 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
11959 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
11962 call
= build_call_expr_loc (loc
, fn_puts
, 1, arg
);
11965 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11966 else if (strcmp (fmt_str
, target_percent_c
) == 0)
11968 if (!arg
|| !validate_arg (arg
, INTEGER_TYPE
))
11971 call
= build_call_expr_loc (loc
, fn_putchar
, 1, arg
);
11977 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), call
);
11980 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11981 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
11982 more than 3 arguments, and ARG may be null in the 2-argument case.
11984 Return NULL_TREE if no simplification was possible, otherwise return the
11985 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11986 code of the function to be simplified. */
11989 fold_builtin_fprintf (location_t loc
, tree fndecl
, tree fp
,
11990 tree fmt
, tree arg
, bool ignore
,
11991 enum built_in_function fcode
)
11993 tree fn_fputc
, fn_fputs
, call
= NULL_TREE
;
11994 const char *fmt_str
= NULL
;
11996 /* If the return value is used, don't do the transformation. */
12000 /* Verify the required arguments in the original call. */
12001 if (!validate_arg (fp
, POINTER_TYPE
))
12003 if (!validate_arg (fmt
, POINTER_TYPE
))
12006 /* Check whether the format is a literal string constant. */
12007 fmt_str
= c_getstr (fmt
);
12008 if (fmt_str
== NULL
)
12011 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
12013 /* If we're using an unlocked function, assume the other
12014 unlocked functions exist explicitly. */
12015 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
12016 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
12020 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
12021 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
12024 if (!init_target_chars ())
12027 /* If the format doesn't contain % args or %%, use strcpy. */
12028 if (strchr (fmt_str
, target_percent
) == NULL
)
12030 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
12034 /* If the format specifier was "", fprintf does nothing. */
12035 if (fmt_str
[0] == '\0')
12037 /* If FP has side-effects, just wait until gimplification is
12039 if (TREE_SIDE_EFFECTS (fp
))
12042 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
12045 /* When "string" doesn't contain %, replace all cases of
12046 fprintf (fp, string) with fputs (string, fp). The fputs
12047 builtin will take care of special cases like length == 1. */
12049 call
= build_call_expr_loc (loc
, fn_fputs
, 2, fmt
, fp
);
12052 /* The other optimizations can be done only on the non-va_list variants. */
12053 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
12056 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
12057 else if (strcmp (fmt_str
, target_percent_s
) == 0)
12059 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
12062 call
= build_call_expr_loc (loc
, fn_fputs
, 2, arg
, fp
);
12065 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
12066 else if (strcmp (fmt_str
, target_percent_c
) == 0)
12068 if (!arg
|| !validate_arg (arg
, INTEGER_TYPE
))
12071 call
= build_call_expr_loc (loc
, fn_fputc
, 2, arg
, fp
);
12076 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), call
);
12079 /* Initialize format string characters in the target charset. */
12082 init_target_chars (void)
12087 target_newline
= lang_hooks
.to_target_charset ('\n');
12088 target_percent
= lang_hooks
.to_target_charset ('%');
12089 target_c
= lang_hooks
.to_target_charset ('c');
12090 target_s
= lang_hooks
.to_target_charset ('s');
12091 if (target_newline
== 0 || target_percent
== 0 || target_c
== 0
12095 target_percent_c
[0] = target_percent
;
12096 target_percent_c
[1] = target_c
;
12097 target_percent_c
[2] = '\0';
12099 target_percent_s
[0] = target_percent
;
12100 target_percent_s
[1] = target_s
;
12101 target_percent_s
[2] = '\0';
12103 target_percent_s_newline
[0] = target_percent
;
12104 target_percent_s_newline
[1] = target_s
;
12105 target_percent_s_newline
[2] = target_newline
;
12106 target_percent_s_newline
[3] = '\0';
12113 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
12114 and no overflow/underflow occurred. INEXACT is true if M was not
12115 exactly calculated. TYPE is the tree type for the result. This
12116 function assumes that you cleared the MPFR flags and then
12117 calculated M to see if anything subsequently set a flag prior to
12118 entering this function. Return NULL_TREE if any checks fail. */
12121 do_mpfr_ckconv (mpfr_srcptr m
, tree type
, int inexact
)
12123 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12124 overflow/underflow occurred. If -frounding-math, proceed iff the
12125 result of calling FUNC was exact. */
12126 if (mpfr_number_p (m
) && !mpfr_overflow_p () && !mpfr_underflow_p ()
12127 && (!flag_rounding_math
|| !inexact
))
12129 REAL_VALUE_TYPE rr
;
12131 real_from_mpfr (&rr
, m
, type
, GMP_RNDN
);
12132 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
12133 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12134 but the mpft_t is not, then we underflowed in the
12136 if (real_isfinite (&rr
)
12137 && (rr
.cl
== rvc_zero
) == (mpfr_zero_p (m
) != 0))
12139 REAL_VALUE_TYPE rmode
;
12141 real_convert (&rmode
, TYPE_MODE (type
), &rr
);
12142 /* Proceed iff the specified mode can hold the value. */
12143 if (real_identical (&rmode
, &rr
))
12144 return build_real (type
, rmode
);
12150 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
12151 number and no overflow/underflow occurred. INEXACT is true if M
12152 was not exactly calculated. TYPE is the tree type for the result.
12153 This function assumes that you cleared the MPFR flags and then
12154 calculated M to see if anything subsequently set a flag prior to
12155 entering this function. Return NULL_TREE if any checks fail, if
12156 FORCE_CONVERT is true, then bypass the checks. */
12159 do_mpc_ckconv (mpc_srcptr m
, tree type
, int inexact
, int force_convert
)
12161 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12162 overflow/underflow occurred. If -frounding-math, proceed iff the
12163 result of calling FUNC was exact. */
12165 || (mpfr_number_p (mpc_realref (m
)) && mpfr_number_p (mpc_imagref (m
))
12166 && !mpfr_overflow_p () && !mpfr_underflow_p ()
12167 && (!flag_rounding_math
|| !inexact
)))
12169 REAL_VALUE_TYPE re
, im
;
12171 real_from_mpfr (&re
, mpc_realref (m
), TREE_TYPE (type
), GMP_RNDN
);
12172 real_from_mpfr (&im
, mpc_imagref (m
), TREE_TYPE (type
), GMP_RNDN
);
12173 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
12174 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12175 but the mpft_t is not, then we underflowed in the
12178 || (real_isfinite (&re
) && real_isfinite (&im
)
12179 && (re
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_realref (m
)) != 0)
12180 && (im
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_imagref (m
)) != 0)))
12182 REAL_VALUE_TYPE re_mode
, im_mode
;
12184 real_convert (&re_mode
, TYPE_MODE (TREE_TYPE (type
)), &re
);
12185 real_convert (&im_mode
, TYPE_MODE (TREE_TYPE (type
)), &im
);
12186 /* Proceed iff the specified mode can hold the value. */
12188 || (real_identical (&re_mode
, &re
)
12189 && real_identical (&im_mode
, &im
)))
12190 return build_complex (type
, build_real (TREE_TYPE (type
), re_mode
),
12191 build_real (TREE_TYPE (type
), im_mode
));
12197 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
12198 FUNC on it and return the resulting value as a tree with type TYPE.
12199 If MIN and/or MAX are not NULL, then the supplied ARG must be
12200 within those bounds. If INCLUSIVE is true, then MIN/MAX are
12201 acceptable values, otherwise they are not. The mpfr precision is
12202 set to the precision of TYPE. We assume that function FUNC returns
12203 zero if the result could be calculated exactly within the requested
12207 do_mpfr_arg1 (tree arg
, tree type
, int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
12208 const REAL_VALUE_TYPE
*min
, const REAL_VALUE_TYPE
*max
,
12211 tree result
= NULL_TREE
;
12215 /* To proceed, MPFR must exactly represent the target floating point
12216 format, which only happens when the target base equals two. */
12217 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12218 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
12220 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
12222 if (real_isfinite (ra
)
12223 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
))
12224 && (!max
|| real_compare (inclusive
? LE_EXPR
: LT_EXPR
, ra
, max
)))
12226 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12227 const int prec
= fmt
->p
;
12228 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12232 mpfr_init2 (m
, prec
);
12233 mpfr_from_real (m
, ra
, GMP_RNDN
);
12234 mpfr_clear_flags ();
12235 inexact
= func (m
, m
, rnd
);
12236 result
= do_mpfr_ckconv (m
, type
, inexact
);
12244 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
12245 FUNC on it and return the resulting value as a tree with type TYPE.
12246 The mpfr precision is set to the precision of TYPE. We assume that
12247 function FUNC returns zero if the result could be calculated
12248 exactly within the requested precision. */
12251 do_mpfr_arg2 (tree arg1
, tree arg2
, tree type
,
12252 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
12254 tree result
= NULL_TREE
;
12259 /* To proceed, MPFR must exactly represent the target floating point
12260 format, which only happens when the target base equals two. */
12261 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12262 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
)
12263 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
))
12265 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
12266 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
12268 if (real_isfinite (ra1
) && real_isfinite (ra2
))
12270 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12271 const int prec
= fmt
->p
;
12272 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12276 mpfr_inits2 (prec
, m1
, m2
, NULL
);
12277 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
12278 mpfr_from_real (m2
, ra2
, GMP_RNDN
);
12279 mpfr_clear_flags ();
12280 inexact
= func (m1
, m1
, m2
, rnd
);
12281 result
= do_mpfr_ckconv (m1
, type
, inexact
);
12282 mpfr_clears (m1
, m2
, NULL
);
12289 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
12290 FUNC on it and return the resulting value as a tree with type TYPE.
12291 The mpfr precision is set to the precision of TYPE. We assume that
12292 function FUNC returns zero if the result could be calculated
12293 exactly within the requested precision. */
12296 do_mpfr_arg3 (tree arg1
, tree arg2
, tree arg3
, tree type
,
12297 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
12299 tree result
= NULL_TREE
;
12305 /* To proceed, MPFR must exactly represent the target floating point
12306 format, which only happens when the target base equals two. */
12307 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12308 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
)
12309 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
)
12310 && TREE_CODE (arg3
) == REAL_CST
&& !TREE_OVERFLOW (arg3
))
12312 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
12313 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
12314 const REAL_VALUE_TYPE
*const ra3
= &TREE_REAL_CST (arg3
);
12316 if (real_isfinite (ra1
) && real_isfinite (ra2
) && real_isfinite (ra3
))
12318 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12319 const int prec
= fmt
->p
;
12320 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12324 mpfr_inits2 (prec
, m1
, m2
, m3
, NULL
);
12325 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
12326 mpfr_from_real (m2
, ra2
, GMP_RNDN
);
12327 mpfr_from_real (m3
, ra3
, GMP_RNDN
);
12328 mpfr_clear_flags ();
12329 inexact
= func (m1
, m1
, m2
, m3
, rnd
);
12330 result
= do_mpfr_ckconv (m1
, type
, inexact
);
12331 mpfr_clears (m1
, m2
, m3
, NULL
);
12338 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
12339 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
12340 If ARG_SINP and ARG_COSP are NULL then the result is returned
12341 as a complex value.
12342 The type is taken from the type of ARG and is used for setting the
12343 precision of the calculation and results. */
12346 do_mpfr_sincos (tree arg
, tree arg_sinp
, tree arg_cosp
)
12348 tree
const type
= TREE_TYPE (arg
);
12349 tree result
= NULL_TREE
;
12353 /* To proceed, MPFR must exactly represent the target floating point
12354 format, which only happens when the target base equals two. */
12355 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12356 && TREE_CODE (arg
) == REAL_CST
12357 && !TREE_OVERFLOW (arg
))
12359 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
12361 if (real_isfinite (ra
))
12363 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12364 const int prec
= fmt
->p
;
12365 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12366 tree result_s
, result_c
;
12370 mpfr_inits2 (prec
, m
, ms
, mc
, NULL
);
12371 mpfr_from_real (m
, ra
, GMP_RNDN
);
12372 mpfr_clear_flags ();
12373 inexact
= mpfr_sin_cos (ms
, mc
, m
, rnd
);
12374 result_s
= do_mpfr_ckconv (ms
, type
, inexact
);
12375 result_c
= do_mpfr_ckconv (mc
, type
, inexact
);
12376 mpfr_clears (m
, ms
, mc
, NULL
);
12377 if (result_s
&& result_c
)
12379 /* If we are to return in a complex value do so. */
12380 if (!arg_sinp
&& !arg_cosp
)
12381 return build_complex (build_complex_type (type
),
12382 result_c
, result_s
);
12384 /* Dereference the sin/cos pointer arguments. */
12385 arg_sinp
= build_fold_indirect_ref (arg_sinp
);
12386 arg_cosp
= build_fold_indirect_ref (arg_cosp
);
12387 /* Proceed if valid pointer type were passed in. */
12388 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp
)) == TYPE_MAIN_VARIANT (type
)
12389 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp
)) == TYPE_MAIN_VARIANT (type
))
12391 /* Set the values. */
12392 result_s
= fold_build2 (MODIFY_EXPR
, type
, arg_sinp
,
12394 TREE_SIDE_EFFECTS (result_s
) = 1;
12395 result_c
= fold_build2 (MODIFY_EXPR
, type
, arg_cosp
,
12397 TREE_SIDE_EFFECTS (result_c
) = 1;
12398 /* Combine the assignments into a compound expr. */
12399 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
12400 result_s
, result_c
));
12408 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
12409 two-argument mpfr order N Bessel function FUNC on them and return
12410 the resulting value as a tree with type TYPE. The mpfr precision
12411 is set to the precision of TYPE. We assume that function FUNC
12412 returns zero if the result could be calculated exactly within the
12413 requested precision. */
12415 do_mpfr_bessel_n (tree arg1
, tree arg2
, tree type
,
12416 int (*func
)(mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
12417 const REAL_VALUE_TYPE
*min
, bool inclusive
)
12419 tree result
= NULL_TREE
;
12424 /* To proceed, MPFR must exactly represent the target floating point
12425 format, which only happens when the target base equals two. */
12426 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12427 && tree_fits_shwi_p (arg1
)
12428 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
))
12430 const HOST_WIDE_INT n
= tree_to_shwi (arg1
);
12431 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg2
);
12434 && real_isfinite (ra
)
12435 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
)))
12437 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12438 const int prec
= fmt
->p
;
12439 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12443 mpfr_init2 (m
, prec
);
12444 mpfr_from_real (m
, ra
, GMP_RNDN
);
12445 mpfr_clear_flags ();
12446 inexact
= func (m
, n
, m
, rnd
);
12447 result
= do_mpfr_ckconv (m
, type
, inexact
);
12455 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12456 the pointer *(ARG_QUO) and return the result. The type is taken
12457 from the type of ARG0 and is used for setting the precision of the
12458 calculation and results. */
12461 do_mpfr_remquo (tree arg0
, tree arg1
, tree arg_quo
)
12463 tree
const type
= TREE_TYPE (arg0
);
12464 tree result
= NULL_TREE
;
12469 /* To proceed, MPFR must exactly represent the target floating point
12470 format, which only happens when the target base equals two. */
12471 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12472 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
12473 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
))
12475 const REAL_VALUE_TYPE
*const ra0
= TREE_REAL_CST_PTR (arg0
);
12476 const REAL_VALUE_TYPE
*const ra1
= TREE_REAL_CST_PTR (arg1
);
12478 if (real_isfinite (ra0
) && real_isfinite (ra1
))
12480 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12481 const int prec
= fmt
->p
;
12482 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12487 mpfr_inits2 (prec
, m0
, m1
, NULL
);
12488 mpfr_from_real (m0
, ra0
, GMP_RNDN
);
12489 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
12490 mpfr_clear_flags ();
12491 mpfr_remquo (m0
, &integer_quo
, m0
, m1
, rnd
);
12492 /* Remquo is independent of the rounding mode, so pass
12493 inexact=0 to do_mpfr_ckconv(). */
12494 result_rem
= do_mpfr_ckconv (m0
, type
, /*inexact=*/ 0);
12495 mpfr_clears (m0
, m1
, NULL
);
12498 /* MPFR calculates quo in the host's long so it may
12499 return more bits in quo than the target int can hold
12500 if sizeof(host long) > sizeof(target int). This can
12501 happen even for native compilers in LP64 mode. In
12502 these cases, modulo the quo value with the largest
12503 number that the target int can hold while leaving one
12504 bit for the sign. */
12505 if (sizeof (integer_quo
) * CHAR_BIT
> INT_TYPE_SIZE
)
12506 integer_quo
%= (long)(1UL << (INT_TYPE_SIZE
- 1));
12508 /* Dereference the quo pointer argument. */
12509 arg_quo
= build_fold_indirect_ref (arg_quo
);
12510 /* Proceed iff a valid pointer type was passed in. */
12511 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo
)) == integer_type_node
)
12513 /* Set the value. */
12515 = fold_build2 (MODIFY_EXPR
, TREE_TYPE (arg_quo
), arg_quo
,
12516 build_int_cst (TREE_TYPE (arg_quo
),
12518 TREE_SIDE_EFFECTS (result_quo
) = 1;
12519 /* Combine the quo assignment with the rem. */
12520 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
12521 result_quo
, result_rem
));
12529 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12530 resulting value as a tree with type TYPE. The mpfr precision is
12531 set to the precision of TYPE. We assume that this mpfr function
12532 returns zero if the result could be calculated exactly within the
12533 requested precision. In addition, the integer pointer represented
12534 by ARG_SG will be dereferenced and set to the appropriate signgam
12538 do_mpfr_lgamma_r (tree arg
, tree arg_sg
, tree type
)
12540 tree result
= NULL_TREE
;
12544 /* To proceed, MPFR must exactly represent the target floating point
12545 format, which only happens when the target base equals two. Also
12546 verify ARG is a constant and that ARG_SG is an int pointer. */
12547 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12548 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
)
12549 && TREE_CODE (TREE_TYPE (arg_sg
)) == POINTER_TYPE
12550 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg
))) == integer_type_node
)
12552 const REAL_VALUE_TYPE
*const ra
= TREE_REAL_CST_PTR (arg
);
12554 /* In addition to NaN and Inf, the argument cannot be zero or a
12555 negative integer. */
12556 if (real_isfinite (ra
)
12557 && ra
->cl
!= rvc_zero
12558 && !(real_isneg (ra
) && real_isinteger (ra
, TYPE_MODE (type
))))
12560 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12561 const int prec
= fmt
->p
;
12562 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12567 mpfr_init2 (m
, prec
);
12568 mpfr_from_real (m
, ra
, GMP_RNDN
);
12569 mpfr_clear_flags ();
12570 inexact
= mpfr_lgamma (m
, &sg
, m
, rnd
);
12571 result_lg
= do_mpfr_ckconv (m
, type
, inexact
);
12577 /* Dereference the arg_sg pointer argument. */
12578 arg_sg
= build_fold_indirect_ref (arg_sg
);
12579 /* Assign the signgam value into *arg_sg. */
12580 result_sg
= fold_build2 (MODIFY_EXPR
,
12581 TREE_TYPE (arg_sg
), arg_sg
,
12582 build_int_cst (TREE_TYPE (arg_sg
), sg
));
12583 TREE_SIDE_EFFECTS (result_sg
) = 1;
12584 /* Combine the signgam assignment with the lgamma result. */
12585 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
12586 result_sg
, result_lg
));
12594 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
12595 function FUNC on it and return the resulting value as a tree with
12596 type TYPE. The mpfr precision is set to the precision of TYPE. We
12597 assume that function FUNC returns zero if the result could be
12598 calculated exactly within the requested precision. */
12601 do_mpc_arg1 (tree arg
, tree type
, int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_rnd_t
))
12603 tree result
= NULL_TREE
;
12607 /* To proceed, MPFR must exactly represent the target floating point
12608 format, which only happens when the target base equals two. */
12609 if (TREE_CODE (arg
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg
)
12610 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
12611 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg
))))->b
== 2)
12613 const REAL_VALUE_TYPE
*const re
= TREE_REAL_CST_PTR (TREE_REALPART (arg
));
12614 const REAL_VALUE_TYPE
*const im
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg
));
12616 if (real_isfinite (re
) && real_isfinite (im
))
12618 const struct real_format
*const fmt
=
12619 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
12620 const int prec
= fmt
->p
;
12621 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12622 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
12626 mpc_init2 (m
, prec
);
12627 mpfr_from_real (mpc_realref (m
), re
, rnd
);
12628 mpfr_from_real (mpc_imagref (m
), im
, rnd
);
12629 mpfr_clear_flags ();
12630 inexact
= func (m
, m
, crnd
);
12631 result
= do_mpc_ckconv (m
, type
, inexact
, /*force_convert=*/ 0);
12639 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12640 mpc function FUNC on it and return the resulting value as a tree
12641 with type TYPE. The mpfr precision is set to the precision of
12642 TYPE. We assume that function FUNC returns zero if the result
12643 could be calculated exactly within the requested precision. If
12644 DO_NONFINITE is true, then fold expressions containing Inf or NaN
12645 in the arguments and/or results. */
12648 do_mpc_arg2 (tree arg0
, tree arg1
, tree type
, int do_nonfinite
,
12649 int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_srcptr
, mpc_rnd_t
))
12651 tree result
= NULL_TREE
;
12656 /* To proceed, MPFR must exactly represent the target floating point
12657 format, which only happens when the target base equals two. */
12658 if (TREE_CODE (arg0
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg0
)
12659 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
12660 && TREE_CODE (arg1
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg1
)
12661 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
12662 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0
))))->b
== 2)
12664 const REAL_VALUE_TYPE
*const re0
= TREE_REAL_CST_PTR (TREE_REALPART (arg0
));
12665 const REAL_VALUE_TYPE
*const im0
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg0
));
12666 const REAL_VALUE_TYPE
*const re1
= TREE_REAL_CST_PTR (TREE_REALPART (arg1
));
12667 const REAL_VALUE_TYPE
*const im1
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg1
));
12670 || (real_isfinite (re0
) && real_isfinite (im0
)
12671 && real_isfinite (re1
) && real_isfinite (im1
)))
12673 const struct real_format
*const fmt
=
12674 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
12675 const int prec
= fmt
->p
;
12676 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12677 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
12681 mpc_init2 (m0
, prec
);
12682 mpc_init2 (m1
, prec
);
12683 mpfr_from_real (mpc_realref (m0
), re0
, rnd
);
12684 mpfr_from_real (mpc_imagref (m0
), im0
, rnd
);
12685 mpfr_from_real (mpc_realref (m1
), re1
, rnd
);
12686 mpfr_from_real (mpc_imagref (m1
), im1
, rnd
);
12687 mpfr_clear_flags ();
12688 inexact
= func (m0
, m0
, m1
, crnd
);
12689 result
= do_mpc_ckconv (m0
, type
, inexact
, do_nonfinite
);
12698 /* A wrapper function for builtin folding that prevents warnings for
12699 "statement without effect" and the like, caused by removing the
12700 call node earlier than the warning is generated. */
12703 fold_call_stmt (gcall
*stmt
, bool ignore
)
12705 tree ret
= NULL_TREE
;
12706 tree fndecl
= gimple_call_fndecl (stmt
);
12707 location_t loc
= gimple_location (stmt
);
12709 && TREE_CODE (fndecl
) == FUNCTION_DECL
12710 && DECL_BUILT_IN (fndecl
)
12711 && !gimple_call_va_arg_pack_p (stmt
))
12713 int nargs
= gimple_call_num_args (stmt
);
12714 tree
*args
= (nargs
> 0
12715 ? gimple_call_arg_ptr (stmt
, 0)
12716 : &error_mark_node
);
12718 if (avoid_folding_inline_builtin (fndecl
))
12720 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
12722 return targetm
.fold_builtin (fndecl
, nargs
, args
, ignore
);
12726 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
12729 /* Propagate location information from original call to
12730 expansion of builtin. Otherwise things like
12731 maybe_emit_chk_warning, that operate on the expansion
12732 of a builtin, will use the wrong location information. */
12733 if (gimple_has_location (stmt
))
12735 tree realret
= ret
;
12736 if (TREE_CODE (ret
) == NOP_EXPR
)
12737 realret
= TREE_OPERAND (ret
, 0);
12738 if (CAN_HAVE_LOCATION_P (realret
)
12739 && !EXPR_HAS_LOCATION (realret
))
12740 SET_EXPR_LOCATION (realret
, loc
);
12750 /* Look up the function in builtin_decl that corresponds to DECL
12751 and set ASMSPEC as its user assembler name. DECL must be a
12752 function decl that declares a builtin. */
12755 set_builtin_user_assembler_name (tree decl
, const char *asmspec
)
12758 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
12759 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
12762 builtin
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
12763 set_user_assembler_name (builtin
, asmspec
);
12764 switch (DECL_FUNCTION_CODE (decl
))
12766 case BUILT_IN_MEMCPY
:
12767 init_block_move_fn (asmspec
);
12768 memcpy_libfunc
= set_user_assembler_libfunc ("memcpy", asmspec
);
12770 case BUILT_IN_MEMSET
:
12771 init_block_clear_fn (asmspec
);
12772 memset_libfunc
= set_user_assembler_libfunc ("memset", asmspec
);
12774 case BUILT_IN_MEMMOVE
:
12775 memmove_libfunc
= set_user_assembler_libfunc ("memmove", asmspec
);
12777 case BUILT_IN_MEMCMP
:
12778 memcmp_libfunc
= set_user_assembler_libfunc ("memcmp", asmspec
);
12780 case BUILT_IN_ABORT
:
12781 abort_libfunc
= set_user_assembler_libfunc ("abort", asmspec
);
12784 if (INT_TYPE_SIZE
< BITS_PER_WORD
)
12786 set_user_assembler_libfunc ("ffs", asmspec
);
12787 set_optab_libfunc (ffs_optab
, mode_for_size (INT_TYPE_SIZE
,
12788 MODE_INT
, 0), "ffs");
12796 /* Return true if DECL is a builtin that expands to a constant or similarly
12799 is_simple_builtin (tree decl
)
12801 if (decl
&& DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
12802 switch (DECL_FUNCTION_CODE (decl
))
12804 /* Builtins that expand to constants. */
12805 case BUILT_IN_CONSTANT_P
:
12806 case BUILT_IN_EXPECT
:
12807 case BUILT_IN_OBJECT_SIZE
:
12808 case BUILT_IN_UNREACHABLE
:
12809 /* Simple register moves or loads from stack. */
12810 case BUILT_IN_ASSUME_ALIGNED
:
12811 case BUILT_IN_RETURN_ADDRESS
:
12812 case BUILT_IN_EXTRACT_RETURN_ADDR
:
12813 case BUILT_IN_FROB_RETURN_ADDR
:
12814 case BUILT_IN_RETURN
:
12815 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
12816 case BUILT_IN_FRAME_ADDRESS
:
12817 case BUILT_IN_VA_END
:
12818 case BUILT_IN_STACK_SAVE
:
12819 case BUILT_IN_STACK_RESTORE
:
12820 /* Exception state returns or moves registers around. */
12821 case BUILT_IN_EH_FILTER
:
12822 case BUILT_IN_EH_POINTER
:
12823 case BUILT_IN_EH_COPY_VALUES
:
12833 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12834 most probably expanded inline into reasonably simple code. This is a
12835 superset of is_simple_builtin. */
12837 is_inexpensive_builtin (tree decl
)
12841 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_MD
)
12843 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
12844 switch (DECL_FUNCTION_CODE (decl
))
12847 case BUILT_IN_ALLOCA
:
12848 case BUILT_IN_ALLOCA_WITH_ALIGN
:
12849 case BUILT_IN_BSWAP16
:
12850 case BUILT_IN_BSWAP32
:
12851 case BUILT_IN_BSWAP64
:
12853 case BUILT_IN_CLZIMAX
:
12854 case BUILT_IN_CLZL
:
12855 case BUILT_IN_CLZLL
:
12857 case BUILT_IN_CTZIMAX
:
12858 case BUILT_IN_CTZL
:
12859 case BUILT_IN_CTZLL
:
12861 case BUILT_IN_FFSIMAX
:
12862 case BUILT_IN_FFSL
:
12863 case BUILT_IN_FFSLL
:
12864 case BUILT_IN_IMAXABS
:
12865 case BUILT_IN_FINITE
:
12866 case BUILT_IN_FINITEF
:
12867 case BUILT_IN_FINITEL
:
12868 case BUILT_IN_FINITED32
:
12869 case BUILT_IN_FINITED64
:
12870 case BUILT_IN_FINITED128
:
12871 case BUILT_IN_FPCLASSIFY
:
12872 case BUILT_IN_ISFINITE
:
12873 case BUILT_IN_ISINF_SIGN
:
12874 case BUILT_IN_ISINF
:
12875 case BUILT_IN_ISINFF
:
12876 case BUILT_IN_ISINFL
:
12877 case BUILT_IN_ISINFD32
:
12878 case BUILT_IN_ISINFD64
:
12879 case BUILT_IN_ISINFD128
:
12880 case BUILT_IN_ISNAN
:
12881 case BUILT_IN_ISNANF
:
12882 case BUILT_IN_ISNANL
:
12883 case BUILT_IN_ISNAND32
:
12884 case BUILT_IN_ISNAND64
:
12885 case BUILT_IN_ISNAND128
:
12886 case BUILT_IN_ISNORMAL
:
12887 case BUILT_IN_ISGREATER
:
12888 case BUILT_IN_ISGREATEREQUAL
:
12889 case BUILT_IN_ISLESS
:
12890 case BUILT_IN_ISLESSEQUAL
:
12891 case BUILT_IN_ISLESSGREATER
:
12892 case BUILT_IN_ISUNORDERED
:
12893 case BUILT_IN_VA_ARG_PACK
:
12894 case BUILT_IN_VA_ARG_PACK_LEN
:
12895 case BUILT_IN_VA_COPY
:
12896 case BUILT_IN_TRAP
:
12897 case BUILT_IN_SAVEREGS
:
12898 case BUILT_IN_POPCOUNTL
:
12899 case BUILT_IN_POPCOUNTLL
:
12900 case BUILT_IN_POPCOUNTIMAX
:
12901 case BUILT_IN_POPCOUNT
:
12902 case BUILT_IN_PARITYL
:
12903 case BUILT_IN_PARITYLL
:
12904 case BUILT_IN_PARITYIMAX
:
12905 case BUILT_IN_PARITY
:
12906 case BUILT_IN_LABS
:
12907 case BUILT_IN_LLABS
:
12908 case BUILT_IN_PREFETCH
:
12912 return is_simple_builtin (decl
);