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"
33 #include "basic-block.h"
34 #include "tree-ssa-alias.h"
35 #include "internal-fn.h"
36 #include "gimple-expr.h"
41 #include "hard-reg-set.h"
44 #include "insn-config.h"
50 #include "typeclass.h"
54 #include "langhooks.h"
55 #include "tree-ssanames.h"
57 #include "value-prof.h"
58 #include "diagnostic-core.h"
64 static tree
do_mpc_arg1 (tree
, tree
, int (*)(mpc_ptr
, mpc_srcptr
, mpc_rnd_t
));
66 struct target_builtins default_target_builtins
;
68 struct target_builtins
*this_target_builtins
= &default_target_builtins
;
71 /* Define the names of the builtin function types and codes. */
72 const char *const built_in_class_names
[BUILT_IN_LAST
]
73 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
75 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
76 const char * built_in_names
[(int) END_BUILTINS
] =
78 #include "builtins.def"
82 /* Setup an array of _DECL trees, make sure each element is
83 initialized to NULL_TREE. */
84 builtin_info_type builtin_info
;
86 /* Non-zero if __builtin_constant_p should be folded right away. */
87 bool force_folding_builtin_constant_p
;
89 static rtx
c_readstr (const char *, enum machine_mode
);
90 static int target_char_cast (tree
, char *);
91 static rtx
get_memory_rtx (tree
, tree
);
92 static int apply_args_size (void);
93 static int apply_result_size (void);
94 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
95 static rtx
result_vector (int, rtx
);
97 static void expand_builtin_update_setjmp_buf (rtx
);
98 static void expand_builtin_prefetch (tree
);
99 static rtx
expand_builtin_apply_args (void);
100 static rtx
expand_builtin_apply_args_1 (void);
101 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
102 static void expand_builtin_return (rtx
);
103 static enum type_class
type_to_class (tree
);
104 static rtx
expand_builtin_classify_type (tree
);
105 static void expand_errno_check (tree
, rtx
);
106 static rtx
expand_builtin_mathfn (tree
, rtx
, rtx
);
107 static rtx
expand_builtin_mathfn_2 (tree
, rtx
, rtx
);
108 static rtx
expand_builtin_mathfn_3 (tree
, rtx
, rtx
);
109 static rtx
expand_builtin_mathfn_ternary (tree
, rtx
, rtx
);
110 static rtx
expand_builtin_interclass_mathfn (tree
, rtx
);
111 static rtx
expand_builtin_sincos (tree
);
112 static rtx
expand_builtin_cexpi (tree
, rtx
);
113 static rtx
expand_builtin_int_roundingfn (tree
, rtx
);
114 static rtx
expand_builtin_int_roundingfn_2 (tree
, rtx
);
115 static rtx
expand_builtin_next_arg (void);
116 static rtx
expand_builtin_va_start (tree
);
117 static rtx
expand_builtin_va_end (tree
);
118 static rtx
expand_builtin_va_copy (tree
);
119 static rtx
expand_builtin_memcmp (tree
, rtx
, enum machine_mode
);
120 static rtx
expand_builtin_strcmp (tree
, rtx
);
121 static rtx
expand_builtin_strncmp (tree
, rtx
, enum machine_mode
);
122 static rtx
builtin_memcpy_read_str (void *, HOST_WIDE_INT
, enum machine_mode
);
123 static rtx
expand_builtin_memcpy (tree
, rtx
);
124 static rtx
expand_builtin_mempcpy (tree
, rtx
, enum machine_mode
);
125 static rtx
expand_builtin_mempcpy_args (tree
, tree
, tree
, rtx
,
126 enum machine_mode
, int);
127 static rtx
expand_builtin_strcpy (tree
, rtx
);
128 static rtx
expand_builtin_strcpy_args (tree
, tree
, rtx
);
129 static rtx
expand_builtin_stpcpy (tree
, rtx
, enum machine_mode
);
130 static rtx
expand_builtin_strncpy (tree
, rtx
);
131 static rtx
builtin_memset_gen_str (void *, HOST_WIDE_INT
, enum machine_mode
);
132 static rtx
expand_builtin_memset (tree
, rtx
, enum machine_mode
);
133 static rtx
expand_builtin_memset_args (tree
, tree
, tree
, rtx
, enum machine_mode
, tree
);
134 static rtx
expand_builtin_bzero (tree
);
135 static rtx
expand_builtin_strlen (tree
, rtx
, enum machine_mode
);
136 static rtx
expand_builtin_alloca (tree
, bool);
137 static rtx
expand_builtin_unop (enum machine_mode
, tree
, rtx
, rtx
, optab
);
138 static rtx
expand_builtin_frame_address (tree
, tree
);
139 static tree
stabilize_va_list_loc (location_t
, tree
, int);
140 static rtx
expand_builtin_expect (tree
, rtx
);
141 static tree
fold_builtin_constant_p (tree
);
142 static tree
fold_builtin_classify_type (tree
);
143 static tree
fold_builtin_strlen (location_t
, tree
, tree
);
144 static tree
fold_builtin_inf (location_t
, tree
, int);
145 static tree
fold_builtin_nan (tree
, tree
, int);
146 static tree
rewrite_call_expr (location_t
, tree
, int, tree
, int, ...);
147 static bool validate_arg (const_tree
, enum tree_code code
);
148 static bool integer_valued_real_p (tree
);
149 static tree
fold_trunc_transparent_mathfn (location_t
, tree
, tree
);
150 static rtx
expand_builtin_fabs (tree
, rtx
, rtx
);
151 static rtx
expand_builtin_signbit (tree
, rtx
);
152 static tree
fold_builtin_sqrt (location_t
, tree
, tree
);
153 static tree
fold_builtin_cbrt (location_t
, tree
, tree
);
154 static tree
fold_builtin_pow (location_t
, tree
, tree
, tree
, tree
);
155 static tree
fold_builtin_powi (location_t
, tree
, tree
, tree
, tree
);
156 static tree
fold_builtin_cos (location_t
, tree
, tree
, tree
);
157 static tree
fold_builtin_cosh (location_t
, tree
, tree
, tree
);
158 static tree
fold_builtin_tan (tree
, tree
);
159 static tree
fold_builtin_trunc (location_t
, tree
, tree
);
160 static tree
fold_builtin_floor (location_t
, tree
, tree
);
161 static tree
fold_builtin_ceil (location_t
, tree
, tree
);
162 static tree
fold_builtin_round (location_t
, tree
, tree
);
163 static tree
fold_builtin_int_roundingfn (location_t
, tree
, tree
);
164 static tree
fold_builtin_bitop (tree
, tree
);
165 static tree
fold_builtin_strchr (location_t
, tree
, tree
, tree
);
166 static tree
fold_builtin_memchr (location_t
, tree
, tree
, tree
, tree
);
167 static tree
fold_builtin_memcmp (location_t
, tree
, tree
, tree
);
168 static tree
fold_builtin_strcmp (location_t
, tree
, tree
);
169 static tree
fold_builtin_strncmp (location_t
, tree
, tree
, tree
);
170 static tree
fold_builtin_signbit (location_t
, tree
, tree
);
171 static tree
fold_builtin_copysign (location_t
, tree
, tree
, tree
, tree
);
172 static tree
fold_builtin_isascii (location_t
, tree
);
173 static tree
fold_builtin_toascii (location_t
, tree
);
174 static tree
fold_builtin_isdigit (location_t
, tree
);
175 static tree
fold_builtin_fabs (location_t
, tree
, tree
);
176 static tree
fold_builtin_abs (location_t
, tree
, tree
);
177 static tree
fold_builtin_unordered_cmp (location_t
, tree
, tree
, tree
, enum tree_code
,
179 static tree
fold_builtin_n (location_t
, tree
, tree
*, int, bool);
180 static tree
fold_builtin_0 (location_t
, tree
, bool);
181 static tree
fold_builtin_1 (location_t
, tree
, tree
, bool);
182 static tree
fold_builtin_2 (location_t
, tree
, tree
, tree
, bool);
183 static tree
fold_builtin_3 (location_t
, tree
, tree
, tree
, tree
, bool);
184 static tree
fold_builtin_4 (location_t
, tree
, tree
, tree
, tree
, tree
, bool);
185 static tree
fold_builtin_varargs (location_t
, tree
, tree
, bool);
187 static tree
fold_builtin_strpbrk (location_t
, tree
, tree
, tree
);
188 static tree
fold_builtin_strstr (location_t
, tree
, tree
, tree
);
189 static tree
fold_builtin_strrchr (location_t
, tree
, tree
, tree
);
190 static tree
fold_builtin_strncat (location_t
, tree
, tree
, tree
);
191 static tree
fold_builtin_strspn (location_t
, tree
, tree
);
192 static tree
fold_builtin_strcspn (location_t
, tree
, tree
);
194 static rtx
expand_builtin_object_size (tree
);
195 static rtx
expand_builtin_memory_chk (tree
, rtx
, enum machine_mode
,
196 enum built_in_function
);
197 static void maybe_emit_chk_warning (tree
, enum built_in_function
);
198 static void maybe_emit_sprintf_chk_warning (tree
, enum built_in_function
);
199 static void maybe_emit_free_warning (tree
);
200 static tree
fold_builtin_object_size (tree
, tree
);
201 static tree
fold_builtin_strcat_chk (location_t
, tree
, tree
, tree
, tree
);
202 static tree
fold_builtin_strncat_chk (location_t
, tree
, tree
, tree
, tree
, tree
);
203 static tree
fold_builtin_printf (location_t
, tree
, tree
, tree
, bool, enum built_in_function
);
204 static tree
fold_builtin_fprintf (location_t
, tree
, tree
, tree
, tree
, bool,
205 enum built_in_function
);
207 static unsigned HOST_WIDE_INT target_newline
;
208 unsigned HOST_WIDE_INT target_percent
;
209 static unsigned HOST_WIDE_INT target_c
;
210 static unsigned HOST_WIDE_INT target_s
;
211 static char target_percent_c
[3];
212 char target_percent_s
[3];
213 static char target_percent_s_newline
[4];
214 static tree
do_mpfr_arg1 (tree
, tree
, int (*)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
215 const REAL_VALUE_TYPE
*, const REAL_VALUE_TYPE
*, bool);
216 static tree
do_mpfr_arg2 (tree
, tree
, tree
,
217 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
218 static tree
do_mpfr_arg3 (tree
, tree
, tree
, tree
,
219 int (*)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
));
220 static tree
do_mpfr_sincos (tree
, tree
, tree
);
221 static tree
do_mpfr_bessel_n (tree
, tree
, tree
,
222 int (*)(mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
223 const REAL_VALUE_TYPE
*, bool);
224 static tree
do_mpfr_remquo (tree
, tree
, tree
);
225 static tree
do_mpfr_lgamma_r (tree
, tree
, tree
);
226 static void expand_builtin_sync_synchronize (void);
228 /* Return true if NAME starts with __builtin_ or __sync_. */
231 is_builtin_name (const char *name
)
233 if (strncmp (name
, "__builtin_", 10) == 0)
235 if (strncmp (name
, "__sync_", 7) == 0)
237 if (strncmp (name
, "__atomic_", 9) == 0)
240 && (!strcmp (name
, "__cilkrts_detach")
241 || !strcmp (name
, "__cilkrts_pop_frame")))
247 /* Return true if DECL is a function symbol representing a built-in. */
250 is_builtin_fn (tree decl
)
252 return TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
);
255 /* Return true if NODE should be considered for inline expansion regardless
256 of the optimization level. This means whenever a function is invoked with
257 its "internal" name, which normally contains the prefix "__builtin". */
260 called_as_built_in (tree node
)
262 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
263 we want the name used to call the function, not the name it
265 const char *name
= IDENTIFIER_POINTER (DECL_NAME (node
));
266 return is_builtin_name (name
);
269 /* Compute values M and N such that M divides (address of EXP - N) and such
270 that N < M. If these numbers can be determined, store M in alignp and N in
271 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
272 *alignp and any bit-offset to *bitposp.
274 Note that the address (and thus the alignment) computed here is based
275 on the address to which a symbol resolves, whereas DECL_ALIGN is based
276 on the address at which an object is actually located. These two
277 addresses are not always the same. For example, on ARM targets,
278 the address &foo of a Thumb function foo() has the lowest bit set,
279 whereas foo() itself starts on an even address.
281 If ADDR_P is true we are taking the address of the memory reference EXP
282 and thus cannot rely on the access taking place. */
285 get_object_alignment_2 (tree exp
, unsigned int *alignp
,
286 unsigned HOST_WIDE_INT
*bitposp
, bool addr_p
)
288 HOST_WIDE_INT bitsize
, bitpos
;
290 enum machine_mode mode
;
291 int unsignedp
, volatilep
;
292 unsigned int align
= BITS_PER_UNIT
;
293 bool known_alignment
= false;
295 /* Get the innermost object and the constant (bitpos) and possibly
296 variable (offset) offset of the access. */
297 exp
= get_inner_reference (exp
, &bitsize
, &bitpos
, &offset
,
298 &mode
, &unsignedp
, &volatilep
, true);
300 /* Extract alignment information from the innermost object and
301 possibly adjust bitpos and offset. */
302 if (TREE_CODE (exp
) == FUNCTION_DECL
)
304 /* Function addresses can encode extra information besides their
305 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
306 allows the low bit to be used as a virtual bit, we know
307 that the address itself must be at least 2-byte aligned. */
308 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_pfn
)
309 align
= 2 * BITS_PER_UNIT
;
311 else if (TREE_CODE (exp
) == LABEL_DECL
)
313 else if (TREE_CODE (exp
) == CONST_DECL
)
315 /* The alignment of a CONST_DECL is determined by its initializer. */
316 exp
= DECL_INITIAL (exp
);
317 align
= TYPE_ALIGN (TREE_TYPE (exp
));
318 #ifdef CONSTANT_ALIGNMENT
319 if (CONSTANT_CLASS_P (exp
))
320 align
= (unsigned) CONSTANT_ALIGNMENT (exp
, align
);
322 known_alignment
= true;
324 else if (DECL_P (exp
))
326 align
= DECL_ALIGN (exp
);
327 known_alignment
= true;
329 else if (TREE_CODE (exp
) == VIEW_CONVERT_EXPR
)
331 align
= TYPE_ALIGN (TREE_TYPE (exp
));
333 else if (TREE_CODE (exp
) == INDIRECT_REF
334 || TREE_CODE (exp
) == MEM_REF
335 || TREE_CODE (exp
) == TARGET_MEM_REF
)
337 tree addr
= TREE_OPERAND (exp
, 0);
339 unsigned HOST_WIDE_INT ptr_bitpos
;
341 if (TREE_CODE (addr
) == BIT_AND_EXPR
342 && TREE_CODE (TREE_OPERAND (addr
, 1)) == INTEGER_CST
)
344 align
= (TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1))
345 & -TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1)));
346 align
*= BITS_PER_UNIT
;
347 addr
= TREE_OPERAND (addr
, 0);
351 = get_pointer_alignment_1 (addr
, &ptr_align
, &ptr_bitpos
);
352 align
= MAX (ptr_align
, align
);
354 /* The alignment of the pointer operand in a TARGET_MEM_REF
355 has to take the variable offset parts into account. */
356 if (TREE_CODE (exp
) == TARGET_MEM_REF
)
360 unsigned HOST_WIDE_INT step
= 1;
362 step
= TREE_INT_CST_LOW (TMR_STEP (exp
));
363 align
= MIN (align
, (step
& -step
) * BITS_PER_UNIT
);
365 if (TMR_INDEX2 (exp
))
366 align
= BITS_PER_UNIT
;
367 known_alignment
= false;
370 /* When EXP is an actual memory reference then we can use
371 TYPE_ALIGN of a pointer indirection to derive alignment.
372 Do so only if get_pointer_alignment_1 did not reveal absolute
373 alignment knowledge and if using that alignment would
374 improve the situation. */
375 if (!addr_p
&& !known_alignment
376 && TYPE_ALIGN (TREE_TYPE (exp
)) > align
)
377 align
= TYPE_ALIGN (TREE_TYPE (exp
));
380 /* Else adjust bitpos accordingly. */
381 bitpos
+= ptr_bitpos
;
382 if (TREE_CODE (exp
) == MEM_REF
383 || TREE_CODE (exp
) == TARGET_MEM_REF
)
384 bitpos
+= mem_ref_offset (exp
).to_short_addr () * BITS_PER_UNIT
;
387 else if (TREE_CODE (exp
) == STRING_CST
)
389 /* STRING_CST are the only constant objects we allow to be not
390 wrapped inside a CONST_DECL. */
391 align
= TYPE_ALIGN (TREE_TYPE (exp
));
392 #ifdef CONSTANT_ALIGNMENT
393 if (CONSTANT_CLASS_P (exp
))
394 align
= (unsigned) CONSTANT_ALIGNMENT (exp
, align
);
396 known_alignment
= true;
399 /* If there is a non-constant offset part extract the maximum
400 alignment that can prevail. */
403 unsigned int trailing_zeros
= tree_ctz (offset
);
404 if (trailing_zeros
< HOST_BITS_PER_INT
)
406 unsigned int inner
= (1U << trailing_zeros
) * BITS_PER_UNIT
;
408 align
= MIN (align
, inner
);
413 *bitposp
= bitpos
& (*alignp
- 1);
414 return known_alignment
;
417 /* For a memory reference expression EXP compute values M and N such that M
418 divides (&EXP - N) and such that N < M. If these numbers can be determined,
419 store M in alignp and N in *BITPOSP and return true. Otherwise return false
420 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
423 get_object_alignment_1 (tree exp
, unsigned int *alignp
,
424 unsigned HOST_WIDE_INT
*bitposp
)
426 return get_object_alignment_2 (exp
, alignp
, bitposp
, false);
429 /* Return the alignment in bits of EXP, an object. */
432 get_object_alignment (tree exp
)
434 unsigned HOST_WIDE_INT bitpos
= 0;
437 get_object_alignment_1 (exp
, &align
, &bitpos
);
439 /* align and bitpos now specify known low bits of the pointer.
440 ptr & (align - 1) == bitpos. */
443 align
= (bitpos
& -bitpos
);
447 /* For a pointer valued expression EXP compute values M and N such that M
448 divides (EXP - N) and such that N < M. If these numbers can be determined,
449 store M in alignp and N in *BITPOSP and return true. Return false if
450 the results are just a conservative approximation.
452 If EXP is not a pointer, false is returned too. */
455 get_pointer_alignment_1 (tree exp
, unsigned int *alignp
,
456 unsigned HOST_WIDE_INT
*bitposp
)
460 if (TREE_CODE (exp
) == ADDR_EXPR
)
461 return get_object_alignment_2 (TREE_OPERAND (exp
, 0),
462 alignp
, bitposp
, true);
463 else if (TREE_CODE (exp
) == SSA_NAME
464 && POINTER_TYPE_P (TREE_TYPE (exp
)))
466 unsigned int ptr_align
, ptr_misalign
;
467 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (exp
);
469 if (pi
&& get_ptr_info_alignment (pi
, &ptr_align
, &ptr_misalign
))
471 *bitposp
= ptr_misalign
* BITS_PER_UNIT
;
472 *alignp
= ptr_align
* BITS_PER_UNIT
;
473 /* We cannot really tell whether this result is an approximation. */
479 *alignp
= BITS_PER_UNIT
;
483 else if (TREE_CODE (exp
) == INTEGER_CST
)
485 *alignp
= BIGGEST_ALIGNMENT
;
486 *bitposp
= ((TREE_INT_CST_LOW (exp
) * BITS_PER_UNIT
)
487 & (BIGGEST_ALIGNMENT
- 1));
492 *alignp
= BITS_PER_UNIT
;
496 /* Return the alignment in bits of EXP, a pointer valued expression.
497 The alignment returned is, by default, the alignment of the thing that
498 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
500 Otherwise, look at the expression to see if we can do better, i.e., if the
501 expression is actually pointing at an object whose alignment is tighter. */
504 get_pointer_alignment (tree exp
)
506 unsigned HOST_WIDE_INT bitpos
= 0;
509 get_pointer_alignment_1 (exp
, &align
, &bitpos
);
511 /* align and bitpos now specify known low bits of the pointer.
512 ptr & (align - 1) == bitpos. */
515 align
= (bitpos
& -bitpos
);
520 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
521 way, because it could contain a zero byte in the middle.
522 TREE_STRING_LENGTH is the size of the character array, not the string.
524 ONLY_VALUE should be nonzero if the result is not going to be emitted
525 into the instruction stream and zero if it is going to be expanded.
526 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
527 is returned, otherwise NULL, since
528 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
529 evaluate the side-effects.
531 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
532 accesses. Note that this implies the result is not going to be emitted
533 into the instruction stream.
535 The value returned is of type `ssizetype'.
537 Unfortunately, string_constant can't access the values of const char
538 arrays with initializers, so neither can we do so here. */
541 c_strlen (tree src
, int only_value
)
544 HOST_WIDE_INT offset
;
550 if (TREE_CODE (src
) == COND_EXPR
551 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
555 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
);
556 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
);
557 if (tree_int_cst_equal (len1
, len2
))
561 if (TREE_CODE (src
) == COMPOUND_EXPR
562 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
563 return c_strlen (TREE_OPERAND (src
, 1), only_value
);
565 loc
= EXPR_LOC_OR_LOC (src
, input_location
);
567 src
= string_constant (src
, &offset_node
);
571 max
= TREE_STRING_LENGTH (src
) - 1;
572 ptr
= TREE_STRING_POINTER (src
);
574 if (offset_node
&& TREE_CODE (offset_node
) != INTEGER_CST
)
576 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
577 compute the offset to the following null if we don't know where to
578 start searching for it. */
581 for (i
= 0; i
< max
; i
++)
585 /* We don't know the starting offset, but we do know that the string
586 has no internal zero bytes. We can assume that the offset falls
587 within the bounds of the string; otherwise, the programmer deserves
588 what he gets. Subtract the offset from the length of the string,
589 and return that. This would perhaps not be valid if we were dealing
590 with named arrays in addition to literal string constants. */
592 return size_diffop_loc (loc
, size_int (max
), offset_node
);
595 /* We have a known offset into the string. Start searching there for
596 a null character if we can represent it as a single HOST_WIDE_INT. */
597 if (offset_node
== 0)
599 else if (! tree_fits_shwi_p (offset_node
))
602 offset
= tree_to_shwi (offset_node
);
604 /* If the offset is known to be out of bounds, warn, and call strlen at
606 if (offset
< 0 || offset
> max
)
608 /* Suppress multiple warnings for propagated constant strings. */
610 && !TREE_NO_WARNING (src
))
612 warning_at (loc
, 0, "offset outside bounds of constant string");
613 TREE_NO_WARNING (src
) = 1;
618 /* Use strlen to search for the first zero byte. Since any strings
619 constructed with build_string will have nulls appended, we win even
620 if we get handed something like (char[4])"abcd".
622 Since OFFSET is our starting index into the string, no further
623 calculation is needed. */
624 return ssize_int (strlen (ptr
+ offset
));
627 /* Return a char pointer for a C string if it is a string constant
628 or sum of string constant and integer constant. */
635 src
= string_constant (src
, &offset_node
);
639 if (offset_node
== 0)
640 return TREE_STRING_POINTER (src
);
641 else if (!tree_fits_uhwi_p (offset_node
)
642 || compare_tree_int (offset_node
, TREE_STRING_LENGTH (src
) - 1) > 0)
645 return TREE_STRING_POINTER (src
) + tree_to_uhwi (offset_node
);
648 /* Return a constant integer corresponding to target reading
649 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
652 c_readstr (const char *str
, enum machine_mode mode
)
656 HOST_WIDE_INT tmp
[MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
];
658 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
659 unsigned int len
= (GET_MODE_PRECISION (mode
) + HOST_BITS_PER_WIDE_INT
- 1)
660 / HOST_BITS_PER_WIDE_INT
;
662 gcc_assert (len
<= MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
);
663 for (i
= 0; i
< len
; i
++)
667 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
670 if (WORDS_BIG_ENDIAN
)
671 j
= GET_MODE_SIZE (mode
) - i
- 1;
672 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
673 && GET_MODE_SIZE (mode
) >= UNITS_PER_WORD
)
674 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
678 ch
= (unsigned char) str
[i
];
679 tmp
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
682 wide_int c
= wide_int::from_array (tmp
, len
, GET_MODE_PRECISION (mode
));
683 return immed_wide_int_const (c
, mode
);
686 /* Cast a target constant CST to target CHAR and if that value fits into
687 host char type, return zero and put that value into variable pointed to by
691 target_char_cast (tree cst
, char *p
)
693 unsigned HOST_WIDE_INT val
, hostval
;
695 if (TREE_CODE (cst
) != INTEGER_CST
696 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
699 /* Do not care if it fits or not right here. */
700 val
= TREE_INT_CST_LOW (cst
);
702 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
703 val
&= (((unsigned HOST_WIDE_INT
) 1) << CHAR_TYPE_SIZE
) - 1;
706 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
707 hostval
&= (((unsigned HOST_WIDE_INT
) 1) << HOST_BITS_PER_CHAR
) - 1;
716 /* Similar to save_expr, but assumes that arbitrary code is not executed
717 in between the multiple evaluations. In particular, we assume that a
718 non-addressable local variable will not be modified. */
721 builtin_save_expr (tree exp
)
723 if (TREE_CODE (exp
) == SSA_NAME
724 || (TREE_ADDRESSABLE (exp
) == 0
725 && (TREE_CODE (exp
) == PARM_DECL
726 || (TREE_CODE (exp
) == VAR_DECL
&& !TREE_STATIC (exp
)))))
729 return save_expr (exp
);
732 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
733 times to get the address of either a higher stack frame, or a return
734 address located within it (depending on FNDECL_CODE). */
737 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
)
741 #ifdef INITIAL_FRAME_ADDRESS_RTX
742 rtx tem
= INITIAL_FRAME_ADDRESS_RTX
;
746 /* For a zero count with __builtin_return_address, we don't care what
747 frame address we return, because target-specific definitions will
748 override us. Therefore frame pointer elimination is OK, and using
749 the soft frame pointer is OK.
751 For a nonzero count, or a zero count with __builtin_frame_address,
752 we require a stable offset from the current frame pointer to the
753 previous one, so we must use the hard frame pointer, and
754 we must disable frame pointer elimination. */
755 if (count
== 0 && fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
756 tem
= frame_pointer_rtx
;
759 tem
= hard_frame_pointer_rtx
;
761 /* Tell reload not to eliminate the frame pointer. */
762 crtl
->accesses_prior_frames
= 1;
766 /* Some machines need special handling before we can access
767 arbitrary frames. For example, on the SPARC, we must first flush
768 all register windows to the stack. */
769 #ifdef SETUP_FRAME_ADDRESSES
771 SETUP_FRAME_ADDRESSES ();
774 /* On the SPARC, the return address is not in the frame, it is in a
775 register. There is no way to access it off of the current frame
776 pointer, but it can be accessed off the previous frame pointer by
777 reading the value from the register window save area. */
778 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
779 if (fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
783 /* Scan back COUNT frames to the specified frame. */
784 for (i
= 0; i
< count
; i
++)
786 /* Assume the dynamic chain pointer is in the word that the
787 frame address points to, unless otherwise specified. */
788 #ifdef DYNAMIC_CHAIN_ADDRESS
789 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
791 tem
= memory_address (Pmode
, tem
);
792 tem
= gen_frame_mem (Pmode
, tem
);
793 tem
= copy_to_reg (tem
);
796 /* For __builtin_frame_address, return what we've got. But, on
797 the SPARC for example, we may have to add a bias. */
798 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
799 #ifdef FRAME_ADDR_RTX
800 return FRAME_ADDR_RTX (tem
);
805 /* For __builtin_return_address, get the return address from that frame. */
806 #ifdef RETURN_ADDR_RTX
807 tem
= RETURN_ADDR_RTX (count
, tem
);
809 tem
= memory_address (Pmode
,
810 plus_constant (Pmode
, tem
, GET_MODE_SIZE (Pmode
)));
811 tem
= gen_frame_mem (Pmode
, tem
);
816 /* Alias set used for setjmp buffer. */
817 static alias_set_type setjmp_alias_set
= -1;
819 /* Construct the leading half of a __builtin_setjmp call. Control will
820 return to RECEIVER_LABEL. This is also called directly by the SJLJ
821 exception handling code. */
824 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
826 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
830 if (setjmp_alias_set
== -1)
831 setjmp_alias_set
= new_alias_set ();
833 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
835 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
837 /* We store the frame pointer and the address of receiver_label in
838 the buffer and use the rest of it for the stack save area, which
839 is machine-dependent. */
841 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
842 set_mem_alias_set (mem
, setjmp_alias_set
);
843 emit_move_insn (mem
, targetm
.builtin_setjmp_frame_value ());
845 mem
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
846 GET_MODE_SIZE (Pmode
))),
847 set_mem_alias_set (mem
, setjmp_alias_set
);
849 emit_move_insn (validize_mem (mem
),
850 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
852 stack_save
= gen_rtx_MEM (sa_mode
,
853 plus_constant (Pmode
, buf_addr
,
854 2 * GET_MODE_SIZE (Pmode
)));
855 set_mem_alias_set (stack_save
, setjmp_alias_set
);
856 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
858 /* If there is further processing to do, do it. */
859 #ifdef HAVE_builtin_setjmp_setup
860 if (HAVE_builtin_setjmp_setup
)
861 emit_insn (gen_builtin_setjmp_setup (buf_addr
));
864 /* We have a nonlocal label. */
865 cfun
->has_nonlocal_label
= 1;
868 /* Construct the trailing part of a __builtin_setjmp call. This is
869 also called directly by the SJLJ exception handling code.
870 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
873 expand_builtin_setjmp_receiver (rtx receiver_label ATTRIBUTE_UNUSED
)
877 /* Mark the FP as used when we get here, so we have to make sure it's
878 marked as used by this function. */
879 emit_use (hard_frame_pointer_rtx
);
881 /* Mark the static chain as clobbered here so life information
882 doesn't get messed up for it. */
883 chain
= targetm
.calls
.static_chain (current_function_decl
, true);
884 if (chain
&& REG_P (chain
))
885 emit_clobber (chain
);
887 /* Now put in the code to restore the frame pointer, and argument
888 pointer, if needed. */
889 #ifdef HAVE_nonlocal_goto
890 if (! HAVE_nonlocal_goto
)
893 /* First adjust our frame pointer to its actual value. It was
894 previously set to the start of the virtual area corresponding to
895 the stacked variables when we branched here and now needs to be
896 adjusted to the actual hardware fp value.
898 Assignments to virtual registers are converted by
899 instantiate_virtual_regs into the corresponding assignment
900 to the underlying register (fp in this case) that makes
901 the original assignment true.
902 So the following insn will actually be decrementing fp by
903 STARTING_FRAME_OFFSET. */
904 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
906 /* Restoring the frame pointer also modifies the hard frame pointer.
907 Mark it used (so that the previous assignment remains live once
908 the frame pointer is eliminated) and clobbered (to represent the
909 implicit update from the assignment). */
910 emit_use (hard_frame_pointer_rtx
);
911 emit_clobber (hard_frame_pointer_rtx
);
914 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
915 if (fixed_regs
[ARG_POINTER_REGNUM
])
917 #ifdef ELIMINABLE_REGS
918 /* If the argument pointer can be eliminated in favor of the
919 frame pointer, we don't need to restore it. We assume here
920 that if such an elimination is present, it can always be used.
921 This is the case on all known machines; if we don't make this
922 assumption, we do unnecessary saving on many machines. */
924 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
926 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
927 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
928 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
931 if (i
== ARRAY_SIZE (elim_regs
))
934 /* Now restore our arg pointer from the address at which it
935 was saved in our stack frame. */
936 emit_move_insn (crtl
->args
.internal_arg_pointer
,
937 copy_to_reg (get_arg_pointer_save_area ()));
942 #ifdef HAVE_builtin_setjmp_receiver
943 if (receiver_label
!= NULL
&& HAVE_builtin_setjmp_receiver
)
944 emit_insn (gen_builtin_setjmp_receiver (receiver_label
));
947 #ifdef HAVE_nonlocal_goto_receiver
948 if (HAVE_nonlocal_goto_receiver
)
949 emit_insn (gen_nonlocal_goto_receiver ());
954 /* We must not allow the code we just generated to be reordered by
955 scheduling. Specifically, the update of the frame pointer must
956 happen immediately, not later. */
957 emit_insn (gen_blockage ());
960 /* __builtin_longjmp is passed a pointer to an array of five words (not
961 all will be used on all machines). It operates similarly to the C
962 library function of the same name, but is more efficient. Much of
963 the code below is copied from the handling of non-local gotos. */
966 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
968 rtx fp
, lab
, stack
, insn
, last
;
969 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
971 /* DRAP is needed for stack realign if longjmp is expanded to current
973 if (SUPPORTS_STACK_ALIGNMENT
)
974 crtl
->need_drap
= true;
976 if (setjmp_alias_set
== -1)
977 setjmp_alias_set
= new_alias_set ();
979 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
981 buf_addr
= force_reg (Pmode
, buf_addr
);
983 /* We require that the user must pass a second argument of 1, because
984 that is what builtin_setjmp will return. */
985 gcc_assert (value
== const1_rtx
);
987 last
= get_last_insn ();
988 #ifdef HAVE_builtin_longjmp
989 if (HAVE_builtin_longjmp
)
990 emit_insn (gen_builtin_longjmp (buf_addr
));
994 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
995 lab
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
996 GET_MODE_SIZE (Pmode
)));
998 stack
= gen_rtx_MEM (sa_mode
, plus_constant (Pmode
, buf_addr
,
999 2 * GET_MODE_SIZE (Pmode
)));
1000 set_mem_alias_set (fp
, setjmp_alias_set
);
1001 set_mem_alias_set (lab
, setjmp_alias_set
);
1002 set_mem_alias_set (stack
, setjmp_alias_set
);
1004 /* Pick up FP, label, and SP from the block and jump. This code is
1005 from expand_goto in stmt.c; see there for detailed comments. */
1006 #ifdef HAVE_nonlocal_goto
1007 if (HAVE_nonlocal_goto
)
1008 /* We have to pass a value to the nonlocal_goto pattern that will
1009 get copied into the static_chain pointer, but it does not matter
1010 what that value is, because builtin_setjmp does not use it. */
1011 emit_insn (gen_nonlocal_goto (value
, lab
, stack
, fp
));
1015 lab
= copy_to_reg (lab
);
1017 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1018 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1020 emit_move_insn (hard_frame_pointer_rtx
, fp
);
1021 emit_stack_restore (SAVE_NONLOCAL
, stack
);
1023 emit_use (hard_frame_pointer_rtx
);
1024 emit_use (stack_pointer_rtx
);
1025 emit_indirect_jump (lab
);
1029 /* Search backwards and mark the jump insn as a non-local goto.
1030 Note that this precludes the use of __builtin_longjmp to a
1031 __builtin_setjmp target in the same function. However, we've
1032 already cautioned the user that these functions are for
1033 internal exception handling use only. */
1034 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1036 gcc_assert (insn
!= last
);
1040 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1043 else if (CALL_P (insn
))
1049 more_const_call_expr_args_p (const const_call_expr_arg_iterator
*iter
)
1051 return (iter
->i
< iter
->n
);
1054 /* This function validates the types of a function call argument list
1055 against a specified list of tree_codes. If the last specifier is a 0,
1056 that represents an ellipses, otherwise the last specifier must be a
1060 validate_arglist (const_tree callexpr
, ...)
1062 enum tree_code code
;
1065 const_call_expr_arg_iterator iter
;
1068 va_start (ap
, callexpr
);
1069 init_const_call_expr_arg_iterator (callexpr
, &iter
);
1073 code
= (enum tree_code
) va_arg (ap
, int);
1077 /* This signifies an ellipses, any further arguments are all ok. */
1081 /* This signifies an endlink, if no arguments remain, return
1082 true, otherwise return false. */
1083 res
= !more_const_call_expr_args_p (&iter
);
1086 /* If no parameters remain or the parameter's code does not
1087 match the specified code, return false. Otherwise continue
1088 checking any remaining arguments. */
1089 arg
= next_const_call_expr_arg (&iter
);
1090 if (!validate_arg (arg
, code
))
1097 /* We need gotos here since we can only have one VA_CLOSE in a
1105 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1106 and the address of the save area. */
1109 expand_builtin_nonlocal_goto (tree exp
)
1111 tree t_label
, t_save_area
;
1112 rtx r_label
, r_save_area
, r_fp
, r_sp
, insn
;
1114 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
1117 t_label
= CALL_EXPR_ARG (exp
, 0);
1118 t_save_area
= CALL_EXPR_ARG (exp
, 1);
1120 r_label
= expand_normal (t_label
);
1121 r_label
= convert_memory_address (Pmode
, r_label
);
1122 r_save_area
= expand_normal (t_save_area
);
1123 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
1124 /* Copy the address of the save location to a register just in case it was
1125 based on the frame pointer. */
1126 r_save_area
= copy_to_reg (r_save_area
);
1127 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
1128 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
1129 plus_constant (Pmode
, r_save_area
,
1130 GET_MODE_SIZE (Pmode
)));
1132 crtl
->has_nonlocal_goto
= 1;
1134 #ifdef HAVE_nonlocal_goto
1135 /* ??? We no longer need to pass the static chain value, afaik. */
1136 if (HAVE_nonlocal_goto
)
1137 emit_insn (gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
1141 r_label
= copy_to_reg (r_label
);
1143 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1144 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1146 /* Restore frame pointer for containing function. */
1147 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
1148 emit_stack_restore (SAVE_NONLOCAL
, r_sp
);
1150 /* USE of hard_frame_pointer_rtx added for consistency;
1151 not clear if really needed. */
1152 emit_use (hard_frame_pointer_rtx
);
1153 emit_use (stack_pointer_rtx
);
1155 /* If the architecture is using a GP register, we must
1156 conservatively assume that the target function makes use of it.
1157 The prologue of functions with nonlocal gotos must therefore
1158 initialize the GP register to the appropriate value, and we
1159 must then make sure that this value is live at the point
1160 of the jump. (Note that this doesn't necessarily apply
1161 to targets with a nonlocal_goto pattern; they are free
1162 to implement it in their own way. Note also that this is
1163 a no-op if the GP register is a global invariant.) */
1164 if ((unsigned) PIC_OFFSET_TABLE_REGNUM
!= INVALID_REGNUM
1165 && fixed_regs
[PIC_OFFSET_TABLE_REGNUM
])
1166 emit_use (pic_offset_table_rtx
);
1168 emit_indirect_jump (r_label
);
1171 /* Search backwards to the jump insn and mark it as a
1173 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1177 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1180 else if (CALL_P (insn
))
1187 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1188 (not all will be used on all machines) that was passed to __builtin_setjmp.
1189 It updates the stack pointer in that block to correspond to the current
1193 expand_builtin_update_setjmp_buf (rtx buf_addr
)
1195 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
1197 = gen_rtx_MEM (sa_mode
,
1200 plus_constant (Pmode
, buf_addr
,
1201 2 * GET_MODE_SIZE (Pmode
))));
1203 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
1206 /* Expand a call to __builtin_prefetch. For a target that does not support
1207 data prefetch, evaluate the memory address argument in case it has side
1211 expand_builtin_prefetch (tree exp
)
1213 tree arg0
, arg1
, arg2
;
1217 if (!validate_arglist (exp
, POINTER_TYPE
, 0))
1220 arg0
= CALL_EXPR_ARG (exp
, 0);
1222 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1223 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1225 nargs
= call_expr_nargs (exp
);
1227 arg1
= CALL_EXPR_ARG (exp
, 1);
1229 arg1
= integer_zero_node
;
1231 arg2
= CALL_EXPR_ARG (exp
, 2);
1233 arg2
= integer_three_node
;
1235 /* Argument 0 is an address. */
1236 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
1238 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1239 if (TREE_CODE (arg1
) != INTEGER_CST
)
1241 error ("second argument to %<__builtin_prefetch%> must be a constant");
1242 arg1
= integer_zero_node
;
1244 op1
= expand_normal (arg1
);
1245 /* Argument 1 must be either zero or one. */
1246 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
1248 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1253 /* Argument 2 (locality) must be a compile-time constant int. */
1254 if (TREE_CODE (arg2
) != INTEGER_CST
)
1256 error ("third argument to %<__builtin_prefetch%> must be a constant");
1257 arg2
= integer_zero_node
;
1259 op2
= expand_normal (arg2
);
1260 /* Argument 2 must be 0, 1, 2, or 3. */
1261 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
1263 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1267 #ifdef HAVE_prefetch
1270 struct expand_operand ops
[3];
1272 create_address_operand (&ops
[0], op0
);
1273 create_integer_operand (&ops
[1], INTVAL (op1
));
1274 create_integer_operand (&ops
[2], INTVAL (op2
));
1275 if (maybe_expand_insn (CODE_FOR_prefetch
, 3, ops
))
1280 /* Don't do anything with direct references to volatile memory, but
1281 generate code to handle other side effects. */
1282 if (!MEM_P (op0
) && side_effects_p (op0
))
1286 /* Get a MEM rtx for expression EXP which is the address of an operand
1287 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1288 the maximum length of the block of memory that might be accessed or
1292 get_memory_rtx (tree exp
, tree len
)
1294 tree orig_exp
= exp
;
1297 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1298 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1299 if (TREE_CODE (exp
) == SAVE_EXPR
&& !SAVE_EXPR_RESOLVED_P (exp
))
1300 exp
= TREE_OPERAND (exp
, 0);
1302 addr
= expand_expr (orig_exp
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
1303 mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
1305 /* Get an expression we can use to find the attributes to assign to MEM.
1306 First remove any nops. */
1307 while (CONVERT_EXPR_P (exp
)
1308 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
1309 exp
= TREE_OPERAND (exp
, 0);
1311 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1312 (as builtin stringops may alias with anything). */
1313 exp
= fold_build2 (MEM_REF
,
1314 build_array_type (char_type_node
,
1315 build_range_type (sizetype
,
1316 size_one_node
, len
)),
1317 exp
, build_int_cst (ptr_type_node
, 0));
1319 /* If the MEM_REF has no acceptable address, try to get the base object
1320 from the original address we got, and build an all-aliasing
1321 unknown-sized access to that one. */
1322 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp
, 0)))
1323 set_mem_attributes (mem
, exp
, 0);
1324 else if (TREE_CODE (TREE_OPERAND (exp
, 0)) == ADDR_EXPR
1325 && (exp
= get_base_address (TREE_OPERAND (TREE_OPERAND (exp
, 0),
1328 exp
= build_fold_addr_expr (exp
);
1329 exp
= fold_build2 (MEM_REF
,
1330 build_array_type (char_type_node
,
1331 build_range_type (sizetype
,
1334 exp
, build_int_cst (ptr_type_node
, 0));
1335 set_mem_attributes (mem
, exp
, 0);
1337 set_mem_alias_set (mem
, 0);
1341 /* Built-in functions to perform an untyped call and return. */
1343 #define apply_args_mode \
1344 (this_target_builtins->x_apply_args_mode)
1345 #define apply_result_mode \
1346 (this_target_builtins->x_apply_result_mode)
1348 /* Return the size required for the block returned by __builtin_apply_args,
1349 and initialize apply_args_mode. */
1352 apply_args_size (void)
1354 static int size
= -1;
1357 enum machine_mode mode
;
1359 /* The values computed by this function never change. */
1362 /* The first value is the incoming arg-pointer. */
1363 size
= GET_MODE_SIZE (Pmode
);
1365 /* The second value is the structure value address unless this is
1366 passed as an "invisible" first argument. */
1367 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1368 size
+= GET_MODE_SIZE (Pmode
);
1370 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1371 if (FUNCTION_ARG_REGNO_P (regno
))
1373 mode
= targetm
.calls
.get_raw_arg_mode (regno
);
1375 gcc_assert (mode
!= VOIDmode
);
1377 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1378 if (size
% align
!= 0)
1379 size
= CEIL (size
, align
) * align
;
1380 size
+= GET_MODE_SIZE (mode
);
1381 apply_args_mode
[regno
] = mode
;
1385 apply_args_mode
[regno
] = VOIDmode
;
1391 /* Return the size required for the block returned by __builtin_apply,
1392 and initialize apply_result_mode. */
1395 apply_result_size (void)
1397 static int size
= -1;
1399 enum machine_mode mode
;
1401 /* The values computed by this function never change. */
1406 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1407 if (targetm
.calls
.function_value_regno_p (regno
))
1409 mode
= targetm
.calls
.get_raw_result_mode (regno
);
1411 gcc_assert (mode
!= VOIDmode
);
1413 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1414 if (size
% align
!= 0)
1415 size
= CEIL (size
, align
) * align
;
1416 size
+= GET_MODE_SIZE (mode
);
1417 apply_result_mode
[regno
] = mode
;
1420 apply_result_mode
[regno
] = VOIDmode
;
1422 /* Allow targets that use untyped_call and untyped_return to override
1423 the size so that machine-specific information can be stored here. */
1424 #ifdef APPLY_RESULT_SIZE
1425 size
= APPLY_RESULT_SIZE
;
1431 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
1432 /* Create a vector describing the result block RESULT. If SAVEP is true,
1433 the result block is used to save the values; otherwise it is used to
1434 restore the values. */
1437 result_vector (int savep
, rtx result
)
1439 int regno
, size
, align
, nelts
;
1440 enum machine_mode mode
;
1442 rtx
*savevec
= XALLOCAVEC (rtx
, FIRST_PSEUDO_REGISTER
);
1445 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1446 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1448 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1449 if (size
% align
!= 0)
1450 size
= CEIL (size
, align
) * align
;
1451 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
1452 mem
= adjust_address (result
, mode
, size
);
1453 savevec
[nelts
++] = (savep
1454 ? gen_rtx_SET (VOIDmode
, mem
, reg
)
1455 : gen_rtx_SET (VOIDmode
, reg
, mem
));
1456 size
+= GET_MODE_SIZE (mode
);
1458 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
1460 #endif /* HAVE_untyped_call or HAVE_untyped_return */
1462 /* Save the state required to perform an untyped call with the same
1463 arguments as were passed to the current function. */
1466 expand_builtin_apply_args_1 (void)
1469 int size
, align
, regno
;
1470 enum machine_mode mode
;
1471 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
1473 /* Create a block where the arg-pointer, structure value address,
1474 and argument registers can be saved. */
1475 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
1477 /* Walk past the arg-pointer and structure value address. */
1478 size
= GET_MODE_SIZE (Pmode
);
1479 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1480 size
+= GET_MODE_SIZE (Pmode
);
1482 /* Save each register used in calling a function to the block. */
1483 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1484 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1486 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1487 if (size
% align
!= 0)
1488 size
= CEIL (size
, align
) * align
;
1490 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1492 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
1493 size
+= GET_MODE_SIZE (mode
);
1496 /* Save the arg pointer to the block. */
1497 tem
= copy_to_reg (crtl
->args
.internal_arg_pointer
);
1498 #ifdef STACK_GROWS_DOWNWARD
1499 /* We need the pointer as the caller actually passed them to us, not
1500 as we might have pretended they were passed. Make sure it's a valid
1501 operand, as emit_move_insn isn't expected to handle a PLUS. */
1503 = force_operand (plus_constant (Pmode
, tem
, crtl
->args
.pretend_args_size
),
1506 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
1508 size
= GET_MODE_SIZE (Pmode
);
1510 /* Save the structure value address unless this is passed as an
1511 "invisible" first argument. */
1512 if (struct_incoming_value
)
1514 emit_move_insn (adjust_address (registers
, Pmode
, size
),
1515 copy_to_reg (struct_incoming_value
));
1516 size
+= GET_MODE_SIZE (Pmode
);
1519 /* Return the address of the block. */
1520 return copy_addr_to_reg (XEXP (registers
, 0));
1523 /* __builtin_apply_args returns block of memory allocated on
1524 the stack into which is stored the arg pointer, structure
1525 value address, static chain, and all the registers that might
1526 possibly be used in performing a function call. The code is
1527 moved to the start of the function so the incoming values are
1531 expand_builtin_apply_args (void)
1533 /* Don't do __builtin_apply_args more than once in a function.
1534 Save the result of the first call and reuse it. */
1535 if (apply_args_value
!= 0)
1536 return apply_args_value
;
1538 /* When this function is called, it means that registers must be
1539 saved on entry to this function. So we migrate the
1540 call to the first insn of this function. */
1545 temp
= expand_builtin_apply_args_1 ();
1549 apply_args_value
= temp
;
1551 /* Put the insns after the NOTE that starts the function.
1552 If this is inside a start_sequence, make the outer-level insn
1553 chain current, so the code is placed at the start of the
1554 function. If internal_arg_pointer is a non-virtual pseudo,
1555 it needs to be placed after the function that initializes
1557 push_topmost_sequence ();
1558 if (REG_P (crtl
->args
.internal_arg_pointer
)
1559 && REGNO (crtl
->args
.internal_arg_pointer
) > LAST_VIRTUAL_REGISTER
)
1560 emit_insn_before (seq
, parm_birth_insn
);
1562 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
1563 pop_topmost_sequence ();
1568 /* Perform an untyped call and save the state required to perform an
1569 untyped return of whatever value was returned by the given function. */
1572 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
1574 int size
, align
, regno
;
1575 enum machine_mode mode
;
1576 rtx incoming_args
, result
, reg
, dest
, src
, call_insn
;
1577 rtx old_stack_level
= 0;
1578 rtx call_fusage
= 0;
1579 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
1581 arguments
= convert_memory_address (Pmode
, arguments
);
1583 /* Create a block where the return registers can be saved. */
1584 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
1586 /* Fetch the arg pointer from the ARGUMENTS block. */
1587 incoming_args
= gen_reg_rtx (Pmode
);
1588 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
1589 #ifndef STACK_GROWS_DOWNWARD
1590 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
1591 incoming_args
, 0, OPTAB_LIB_WIDEN
);
1594 /* Push a new argument block and copy the arguments. Do not allow
1595 the (potential) memcpy call below to interfere with our stack
1597 do_pending_stack_adjust ();
1600 /* Save the stack with nonlocal if available. */
1601 #ifdef HAVE_save_stack_nonlocal
1602 if (HAVE_save_stack_nonlocal
)
1603 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
);
1606 emit_stack_save (SAVE_BLOCK
, &old_stack_level
);
1608 /* Allocate a block of memory onto the stack and copy the memory
1609 arguments to the outgoing arguments address. We can pass TRUE
1610 as the 4th argument because we just saved the stack pointer
1611 and will restore it right after the call. */
1612 allocate_dynamic_stack_space (argsize
, 0, BIGGEST_ALIGNMENT
, true);
1614 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1615 may have already set current_function_calls_alloca to true.
1616 current_function_calls_alloca won't be set if argsize is zero,
1617 so we have to guarantee need_drap is true here. */
1618 if (SUPPORTS_STACK_ALIGNMENT
)
1619 crtl
->need_drap
= true;
1621 dest
= virtual_outgoing_args_rtx
;
1622 #ifndef STACK_GROWS_DOWNWARD
1623 if (CONST_INT_P (argsize
))
1624 dest
= plus_constant (Pmode
, dest
, -INTVAL (argsize
));
1626 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
1628 dest
= gen_rtx_MEM (BLKmode
, dest
);
1629 set_mem_align (dest
, PARM_BOUNDARY
);
1630 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
1631 set_mem_align (src
, PARM_BOUNDARY
);
1632 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
1634 /* Refer to the argument block. */
1636 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
1637 set_mem_align (arguments
, PARM_BOUNDARY
);
1639 /* Walk past the arg-pointer and structure value address. */
1640 size
= GET_MODE_SIZE (Pmode
);
1642 size
+= GET_MODE_SIZE (Pmode
);
1644 /* Restore each of the registers previously saved. Make USE insns
1645 for each of these registers for use in making the call. */
1646 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1647 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1649 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1650 if (size
% align
!= 0)
1651 size
= CEIL (size
, align
) * align
;
1652 reg
= gen_rtx_REG (mode
, regno
);
1653 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
1654 use_reg (&call_fusage
, reg
);
1655 size
+= GET_MODE_SIZE (mode
);
1658 /* Restore the structure value address unless this is passed as an
1659 "invisible" first argument. */
1660 size
= GET_MODE_SIZE (Pmode
);
1663 rtx value
= gen_reg_rtx (Pmode
);
1664 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
1665 emit_move_insn (struct_value
, value
);
1666 if (REG_P (struct_value
))
1667 use_reg (&call_fusage
, struct_value
);
1668 size
+= GET_MODE_SIZE (Pmode
);
1671 /* All arguments and registers used for the call are set up by now! */
1672 function
= prepare_call_address (NULL
, function
, NULL
, &call_fusage
, 0, 0);
1674 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1675 and we don't want to load it into a register as an optimization,
1676 because prepare_call_address already did it if it should be done. */
1677 if (GET_CODE (function
) != SYMBOL_REF
)
1678 function
= memory_address (FUNCTION_MODE
, function
);
1680 /* Generate the actual call instruction and save the return value. */
1681 #ifdef HAVE_untyped_call
1682 if (HAVE_untyped_call
)
1683 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE
, function
),
1684 result
, result_vector (1, result
)));
1687 #ifdef HAVE_call_value
1688 if (HAVE_call_value
)
1692 /* Locate the unique return register. It is not possible to
1693 express a call that sets more than one return register using
1694 call_value; use untyped_call for that. In fact, untyped_call
1695 only needs to save the return registers in the given block. */
1696 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1697 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1699 gcc_assert (!valreg
); /* HAVE_untyped_call required. */
1701 valreg
= gen_rtx_REG (mode
, regno
);
1704 emit_call_insn (GEN_CALL_VALUE (valreg
,
1705 gen_rtx_MEM (FUNCTION_MODE
, function
),
1706 const0_rtx
, NULL_RTX
, const0_rtx
));
1708 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
1714 /* Find the CALL insn we just emitted, and attach the register usage
1716 call_insn
= last_call_insn ();
1717 add_function_usage_to (call_insn
, call_fusage
);
1719 /* Restore the stack. */
1720 #ifdef HAVE_save_stack_nonlocal
1721 if (HAVE_save_stack_nonlocal
)
1722 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
);
1725 emit_stack_restore (SAVE_BLOCK
, old_stack_level
);
1726 fixup_args_size_notes (call_insn
, get_last_insn (), 0);
1730 /* Return the address of the result block. */
1731 result
= copy_addr_to_reg (XEXP (result
, 0));
1732 return convert_memory_address (ptr_mode
, result
);
1735 /* Perform an untyped return. */
1738 expand_builtin_return (rtx result
)
1740 int size
, align
, regno
;
1741 enum machine_mode mode
;
1743 rtx call_fusage
= 0;
1745 result
= convert_memory_address (Pmode
, result
);
1747 apply_result_size ();
1748 result
= gen_rtx_MEM (BLKmode
, result
);
1750 #ifdef HAVE_untyped_return
1751 if (HAVE_untyped_return
)
1753 emit_jump_insn (gen_untyped_return (result
, result_vector (0, result
)));
1759 /* Restore the return value and note that each value is used. */
1761 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1762 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1764 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1765 if (size
% align
!= 0)
1766 size
= CEIL (size
, align
) * align
;
1767 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1768 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
1770 push_to_sequence (call_fusage
);
1772 call_fusage
= get_insns ();
1774 size
+= GET_MODE_SIZE (mode
);
1777 /* Put the USE insns before the return. */
1778 emit_insn (call_fusage
);
1780 /* Return whatever values was restored by jumping directly to the end
1782 expand_naked_return ();
1785 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1787 static enum type_class
1788 type_to_class (tree type
)
1790 switch (TREE_CODE (type
))
1792 case VOID_TYPE
: return void_type_class
;
1793 case INTEGER_TYPE
: return integer_type_class
;
1794 case ENUMERAL_TYPE
: return enumeral_type_class
;
1795 case BOOLEAN_TYPE
: return boolean_type_class
;
1796 case POINTER_TYPE
: return pointer_type_class
;
1797 case REFERENCE_TYPE
: return reference_type_class
;
1798 case OFFSET_TYPE
: return offset_type_class
;
1799 case REAL_TYPE
: return real_type_class
;
1800 case COMPLEX_TYPE
: return complex_type_class
;
1801 case FUNCTION_TYPE
: return function_type_class
;
1802 case METHOD_TYPE
: return method_type_class
;
1803 case RECORD_TYPE
: return record_type_class
;
1805 case QUAL_UNION_TYPE
: return union_type_class
;
1806 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
1807 ? string_type_class
: array_type_class
);
1808 case LANG_TYPE
: return lang_type_class
;
1809 default: return no_type_class
;
1813 /* Expand a call EXP to __builtin_classify_type. */
1816 expand_builtin_classify_type (tree exp
)
1818 if (call_expr_nargs (exp
))
1819 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))));
1820 return GEN_INT (no_type_class
);
1823 /* This helper macro, meant to be used in mathfn_built_in below,
1824 determines which among a set of three builtin math functions is
1825 appropriate for a given type mode. The `F' and `L' cases are
1826 automatically generated from the `double' case. */
1827 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1828 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1829 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1830 fcodel = BUILT_IN_MATHFN##L ; break;
1831 /* Similar to above, but appends _R after any F/L suffix. */
1832 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1833 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1834 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1835 fcodel = BUILT_IN_MATHFN##L_R ; break;
1837 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1838 if available. If IMPLICIT is true use the implicit builtin declaration,
1839 otherwise use the explicit declaration. If we can't do the conversion,
1843 mathfn_built_in_1 (tree type
, enum built_in_function fn
, bool implicit_p
)
1845 enum built_in_function fcode
, fcodef
, fcodel
, fcode2
;
1849 CASE_MATHFN (BUILT_IN_ACOS
)
1850 CASE_MATHFN (BUILT_IN_ACOSH
)
1851 CASE_MATHFN (BUILT_IN_ASIN
)
1852 CASE_MATHFN (BUILT_IN_ASINH
)
1853 CASE_MATHFN (BUILT_IN_ATAN
)
1854 CASE_MATHFN (BUILT_IN_ATAN2
)
1855 CASE_MATHFN (BUILT_IN_ATANH
)
1856 CASE_MATHFN (BUILT_IN_CBRT
)
1857 CASE_MATHFN (BUILT_IN_CEIL
)
1858 CASE_MATHFN (BUILT_IN_CEXPI
)
1859 CASE_MATHFN (BUILT_IN_COPYSIGN
)
1860 CASE_MATHFN (BUILT_IN_COS
)
1861 CASE_MATHFN (BUILT_IN_COSH
)
1862 CASE_MATHFN (BUILT_IN_DREM
)
1863 CASE_MATHFN (BUILT_IN_ERF
)
1864 CASE_MATHFN (BUILT_IN_ERFC
)
1865 CASE_MATHFN (BUILT_IN_EXP
)
1866 CASE_MATHFN (BUILT_IN_EXP10
)
1867 CASE_MATHFN (BUILT_IN_EXP2
)
1868 CASE_MATHFN (BUILT_IN_EXPM1
)
1869 CASE_MATHFN (BUILT_IN_FABS
)
1870 CASE_MATHFN (BUILT_IN_FDIM
)
1871 CASE_MATHFN (BUILT_IN_FLOOR
)
1872 CASE_MATHFN (BUILT_IN_FMA
)
1873 CASE_MATHFN (BUILT_IN_FMAX
)
1874 CASE_MATHFN (BUILT_IN_FMIN
)
1875 CASE_MATHFN (BUILT_IN_FMOD
)
1876 CASE_MATHFN (BUILT_IN_FREXP
)
1877 CASE_MATHFN (BUILT_IN_GAMMA
)
1878 CASE_MATHFN_REENT (BUILT_IN_GAMMA
) /* GAMMA_R */
1879 CASE_MATHFN (BUILT_IN_HUGE_VAL
)
1880 CASE_MATHFN (BUILT_IN_HYPOT
)
1881 CASE_MATHFN (BUILT_IN_ILOGB
)
1882 CASE_MATHFN (BUILT_IN_ICEIL
)
1883 CASE_MATHFN (BUILT_IN_IFLOOR
)
1884 CASE_MATHFN (BUILT_IN_INF
)
1885 CASE_MATHFN (BUILT_IN_IRINT
)
1886 CASE_MATHFN (BUILT_IN_IROUND
)
1887 CASE_MATHFN (BUILT_IN_ISINF
)
1888 CASE_MATHFN (BUILT_IN_J0
)
1889 CASE_MATHFN (BUILT_IN_J1
)
1890 CASE_MATHFN (BUILT_IN_JN
)
1891 CASE_MATHFN (BUILT_IN_LCEIL
)
1892 CASE_MATHFN (BUILT_IN_LDEXP
)
1893 CASE_MATHFN (BUILT_IN_LFLOOR
)
1894 CASE_MATHFN (BUILT_IN_LGAMMA
)
1895 CASE_MATHFN_REENT (BUILT_IN_LGAMMA
) /* LGAMMA_R */
1896 CASE_MATHFN (BUILT_IN_LLCEIL
)
1897 CASE_MATHFN (BUILT_IN_LLFLOOR
)
1898 CASE_MATHFN (BUILT_IN_LLRINT
)
1899 CASE_MATHFN (BUILT_IN_LLROUND
)
1900 CASE_MATHFN (BUILT_IN_LOG
)
1901 CASE_MATHFN (BUILT_IN_LOG10
)
1902 CASE_MATHFN (BUILT_IN_LOG1P
)
1903 CASE_MATHFN (BUILT_IN_LOG2
)
1904 CASE_MATHFN (BUILT_IN_LOGB
)
1905 CASE_MATHFN (BUILT_IN_LRINT
)
1906 CASE_MATHFN (BUILT_IN_LROUND
)
1907 CASE_MATHFN (BUILT_IN_MODF
)
1908 CASE_MATHFN (BUILT_IN_NAN
)
1909 CASE_MATHFN (BUILT_IN_NANS
)
1910 CASE_MATHFN (BUILT_IN_NEARBYINT
)
1911 CASE_MATHFN (BUILT_IN_NEXTAFTER
)
1912 CASE_MATHFN (BUILT_IN_NEXTTOWARD
)
1913 CASE_MATHFN (BUILT_IN_POW
)
1914 CASE_MATHFN (BUILT_IN_POWI
)
1915 CASE_MATHFN (BUILT_IN_POW10
)
1916 CASE_MATHFN (BUILT_IN_REMAINDER
)
1917 CASE_MATHFN (BUILT_IN_REMQUO
)
1918 CASE_MATHFN (BUILT_IN_RINT
)
1919 CASE_MATHFN (BUILT_IN_ROUND
)
1920 CASE_MATHFN (BUILT_IN_SCALB
)
1921 CASE_MATHFN (BUILT_IN_SCALBLN
)
1922 CASE_MATHFN (BUILT_IN_SCALBN
)
1923 CASE_MATHFN (BUILT_IN_SIGNBIT
)
1924 CASE_MATHFN (BUILT_IN_SIGNIFICAND
)
1925 CASE_MATHFN (BUILT_IN_SIN
)
1926 CASE_MATHFN (BUILT_IN_SINCOS
)
1927 CASE_MATHFN (BUILT_IN_SINH
)
1928 CASE_MATHFN (BUILT_IN_SQRT
)
1929 CASE_MATHFN (BUILT_IN_TAN
)
1930 CASE_MATHFN (BUILT_IN_TANH
)
1931 CASE_MATHFN (BUILT_IN_TGAMMA
)
1932 CASE_MATHFN (BUILT_IN_TRUNC
)
1933 CASE_MATHFN (BUILT_IN_Y0
)
1934 CASE_MATHFN (BUILT_IN_Y1
)
1935 CASE_MATHFN (BUILT_IN_YN
)
1941 if (TYPE_MAIN_VARIANT (type
) == double_type_node
)
1943 else if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1945 else if (TYPE_MAIN_VARIANT (type
) == long_double_type_node
)
1950 if (implicit_p
&& !builtin_decl_implicit_p (fcode2
))
1953 return builtin_decl_explicit (fcode2
);
1956 /* Like mathfn_built_in_1(), but always use the implicit array. */
1959 mathfn_built_in (tree type
, enum built_in_function fn
)
1961 return mathfn_built_in_1 (type
, fn
, /*implicit=*/ 1);
1964 /* If errno must be maintained, expand the RTL to check if the result,
1965 TARGET, of a built-in function call, EXP, is NaN, and if so set
1969 expand_errno_check (tree exp
, rtx target
)
1971 rtx lab
= gen_label_rtx ();
1973 /* Test the result; if it is NaN, set errno=EDOM because
1974 the argument was not in the domain. */
1975 do_compare_rtx_and_jump (target
, target
, EQ
, 0, GET_MODE (target
),
1976 NULL_RTX
, NULL_RTX
, lab
,
1977 /* The jump is very likely. */
1978 REG_BR_PROB_BASE
- (REG_BR_PROB_BASE
/ 2000 - 1));
1981 /* If this built-in doesn't throw an exception, set errno directly. */
1982 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp
), 0)))
1984 #ifdef GEN_ERRNO_RTX
1985 rtx errno_rtx
= GEN_ERRNO_RTX
;
1988 = gen_rtx_MEM (word_mode
, gen_rtx_SYMBOL_REF (Pmode
, "errno"));
1990 emit_move_insn (errno_rtx
,
1991 gen_int_mode (TARGET_EDOM
, GET_MODE (errno_rtx
)));
1997 /* Make sure the library call isn't expanded as a tail call. */
1998 CALL_EXPR_TAILCALL (exp
) = 0;
2000 /* We can't set errno=EDOM directly; let the library call do it.
2001 Pop the arguments right away in case the call gets deleted. */
2003 expand_call (exp
, target
, 0);
2008 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
2009 Return NULL_RTX if a normal call should be emitted rather than expanding
2010 the function in-line. EXP is the expression that is a call to the builtin
2011 function; if convenient, the result should be placed in TARGET.
2012 SUBTARGET may be used as the target for computing one of EXP's operands. */
2015 expand_builtin_mathfn (tree exp
, rtx target
, rtx subtarget
)
2017 optab builtin_optab
;
2019 tree fndecl
= get_callee_fndecl (exp
);
2020 enum machine_mode mode
;
2021 bool errno_set
= false;
2022 bool try_widening
= false;
2025 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2028 arg
= CALL_EXPR_ARG (exp
, 0);
2030 switch (DECL_FUNCTION_CODE (fndecl
))
2032 CASE_FLT_FN (BUILT_IN_SQRT
):
2033 errno_set
= ! tree_expr_nonnegative_p (arg
);
2034 try_widening
= true;
2035 builtin_optab
= sqrt_optab
;
2037 CASE_FLT_FN (BUILT_IN_EXP
):
2038 errno_set
= true; builtin_optab
= exp_optab
; break;
2039 CASE_FLT_FN (BUILT_IN_EXP10
):
2040 CASE_FLT_FN (BUILT_IN_POW10
):
2041 errno_set
= true; builtin_optab
= exp10_optab
; break;
2042 CASE_FLT_FN (BUILT_IN_EXP2
):
2043 errno_set
= true; builtin_optab
= exp2_optab
; break;
2044 CASE_FLT_FN (BUILT_IN_EXPM1
):
2045 errno_set
= true; builtin_optab
= expm1_optab
; break;
2046 CASE_FLT_FN (BUILT_IN_LOGB
):
2047 errno_set
= true; builtin_optab
= logb_optab
; break;
2048 CASE_FLT_FN (BUILT_IN_LOG
):
2049 errno_set
= true; builtin_optab
= log_optab
; break;
2050 CASE_FLT_FN (BUILT_IN_LOG10
):
2051 errno_set
= true; builtin_optab
= log10_optab
; break;
2052 CASE_FLT_FN (BUILT_IN_LOG2
):
2053 errno_set
= true; builtin_optab
= log2_optab
; break;
2054 CASE_FLT_FN (BUILT_IN_LOG1P
):
2055 errno_set
= true; builtin_optab
= log1p_optab
; break;
2056 CASE_FLT_FN (BUILT_IN_ASIN
):
2057 builtin_optab
= asin_optab
; break;
2058 CASE_FLT_FN (BUILT_IN_ACOS
):
2059 builtin_optab
= acos_optab
; break;
2060 CASE_FLT_FN (BUILT_IN_TAN
):
2061 builtin_optab
= tan_optab
; break;
2062 CASE_FLT_FN (BUILT_IN_ATAN
):
2063 builtin_optab
= atan_optab
; break;
2064 CASE_FLT_FN (BUILT_IN_FLOOR
):
2065 builtin_optab
= floor_optab
; break;
2066 CASE_FLT_FN (BUILT_IN_CEIL
):
2067 builtin_optab
= ceil_optab
; break;
2068 CASE_FLT_FN (BUILT_IN_TRUNC
):
2069 builtin_optab
= btrunc_optab
; break;
2070 CASE_FLT_FN (BUILT_IN_ROUND
):
2071 builtin_optab
= round_optab
; break;
2072 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
2073 builtin_optab
= nearbyint_optab
;
2074 if (flag_trapping_math
)
2076 /* Else fallthrough and expand as rint. */
2077 CASE_FLT_FN (BUILT_IN_RINT
):
2078 builtin_optab
= rint_optab
; break;
2079 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
2080 builtin_optab
= significand_optab
; break;
2085 /* Make a suitable register to place result in. */
2086 mode
= TYPE_MODE (TREE_TYPE (exp
));
2088 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2091 /* Before working hard, check whether the instruction is available, but try
2092 to widen the mode for specific operations. */
2093 if ((optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
2094 || (try_widening
&& !excess_precision_type (TREE_TYPE (exp
))))
2095 && (!errno_set
|| !optimize_insn_for_size_p ()))
2097 rtx result
= gen_reg_rtx (mode
);
2099 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2100 need to expand the argument again. This way, we will not perform
2101 side-effects more the once. */
2102 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2104 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2108 /* Compute into RESULT.
2109 Set RESULT to wherever the result comes back. */
2110 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2115 expand_errno_check (exp
, result
);
2117 /* Output the entire sequence. */
2118 insns
= get_insns ();
2124 /* If we were unable to expand via the builtin, stop the sequence
2125 (without outputting the insns) and call to the library function
2126 with the stabilized argument list. */
2130 return expand_call (exp
, target
, target
== const0_rtx
);
2133 /* Expand a call to the builtin binary math functions (pow and atan2).
2134 Return NULL_RTX if a normal call should be emitted rather than expanding the
2135 function in-line. EXP is the expression that is a call to the builtin
2136 function; if convenient, the result should be placed in TARGET.
2137 SUBTARGET may be used as the target for computing one of EXP's
2141 expand_builtin_mathfn_2 (tree exp
, rtx target
, rtx subtarget
)
2143 optab builtin_optab
;
2144 rtx op0
, op1
, insns
, result
;
2145 int op1_type
= REAL_TYPE
;
2146 tree fndecl
= get_callee_fndecl (exp
);
2148 enum machine_mode mode
;
2149 bool errno_set
= true;
2151 switch (DECL_FUNCTION_CODE (fndecl
))
2153 CASE_FLT_FN (BUILT_IN_SCALBN
):
2154 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2155 CASE_FLT_FN (BUILT_IN_LDEXP
):
2156 op1_type
= INTEGER_TYPE
;
2161 if (!validate_arglist (exp
, REAL_TYPE
, op1_type
, VOID_TYPE
))
2164 arg0
= CALL_EXPR_ARG (exp
, 0);
2165 arg1
= CALL_EXPR_ARG (exp
, 1);
2167 switch (DECL_FUNCTION_CODE (fndecl
))
2169 CASE_FLT_FN (BUILT_IN_POW
):
2170 builtin_optab
= pow_optab
; break;
2171 CASE_FLT_FN (BUILT_IN_ATAN2
):
2172 builtin_optab
= atan2_optab
; break;
2173 CASE_FLT_FN (BUILT_IN_SCALB
):
2174 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2176 builtin_optab
= scalb_optab
; break;
2177 CASE_FLT_FN (BUILT_IN_SCALBN
):
2178 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2179 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2181 /* Fall through... */
2182 CASE_FLT_FN (BUILT_IN_LDEXP
):
2183 builtin_optab
= ldexp_optab
; break;
2184 CASE_FLT_FN (BUILT_IN_FMOD
):
2185 builtin_optab
= fmod_optab
; break;
2186 CASE_FLT_FN (BUILT_IN_REMAINDER
):
2187 CASE_FLT_FN (BUILT_IN_DREM
):
2188 builtin_optab
= remainder_optab
; break;
2193 /* Make a suitable register to place result in. */
2194 mode
= TYPE_MODE (TREE_TYPE (exp
));
2196 /* Before working hard, check whether the instruction is available. */
2197 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2200 result
= gen_reg_rtx (mode
);
2202 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2205 if (errno_set
&& optimize_insn_for_size_p ())
2208 /* Always stabilize the argument list. */
2209 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2210 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2212 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2213 op1
= expand_normal (arg1
);
2217 /* Compute into RESULT.
2218 Set RESULT to wherever the result comes back. */
2219 result
= expand_binop (mode
, builtin_optab
, op0
, op1
,
2220 result
, 0, OPTAB_DIRECT
);
2222 /* If we were unable to expand via the builtin, stop the sequence
2223 (without outputting the insns) and call to the library function
2224 with the stabilized argument list. */
2228 return expand_call (exp
, target
, target
== const0_rtx
);
2232 expand_errno_check (exp
, result
);
2234 /* Output the entire sequence. */
2235 insns
= get_insns ();
2242 /* Expand a call to the builtin trinary math functions (fma).
2243 Return NULL_RTX if a normal call should be emitted rather than expanding the
2244 function in-line. EXP is the expression that is a call to the builtin
2245 function; if convenient, the result should be placed in TARGET.
2246 SUBTARGET may be used as the target for computing one of EXP's
2250 expand_builtin_mathfn_ternary (tree exp
, rtx target
, rtx subtarget
)
2252 optab builtin_optab
;
2253 rtx op0
, op1
, op2
, insns
, result
;
2254 tree fndecl
= get_callee_fndecl (exp
);
2255 tree arg0
, arg1
, arg2
;
2256 enum machine_mode mode
;
2258 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
2261 arg0
= CALL_EXPR_ARG (exp
, 0);
2262 arg1
= CALL_EXPR_ARG (exp
, 1);
2263 arg2
= CALL_EXPR_ARG (exp
, 2);
2265 switch (DECL_FUNCTION_CODE (fndecl
))
2267 CASE_FLT_FN (BUILT_IN_FMA
):
2268 builtin_optab
= fma_optab
; break;
2273 /* Make a suitable register to place result in. */
2274 mode
= TYPE_MODE (TREE_TYPE (exp
));
2276 /* Before working hard, check whether the instruction is available. */
2277 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2280 result
= gen_reg_rtx (mode
);
2282 /* Always stabilize the argument list. */
2283 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2284 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2285 CALL_EXPR_ARG (exp
, 2) = arg2
= builtin_save_expr (arg2
);
2287 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2288 op1
= expand_normal (arg1
);
2289 op2
= expand_normal (arg2
);
2293 /* Compute into RESULT.
2294 Set RESULT to wherever the result comes back. */
2295 result
= expand_ternary_op (mode
, builtin_optab
, op0
, op1
, op2
,
2298 /* If we were unable to expand via the builtin, stop the sequence
2299 (without outputting the insns) and call to the library function
2300 with the stabilized argument list. */
2304 return expand_call (exp
, target
, target
== const0_rtx
);
2307 /* Output the entire sequence. */
2308 insns
= get_insns ();
2315 /* Expand a call to the builtin sin and cos math functions.
2316 Return NULL_RTX if a normal call should be emitted rather than expanding the
2317 function in-line. EXP is the expression that is a call to the builtin
2318 function; if convenient, the result should be placed in TARGET.
2319 SUBTARGET may be used as the target for computing one of EXP's
2323 expand_builtin_mathfn_3 (tree exp
, rtx target
, rtx subtarget
)
2325 optab builtin_optab
;
2327 tree fndecl
= get_callee_fndecl (exp
);
2328 enum machine_mode mode
;
2331 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2334 arg
= CALL_EXPR_ARG (exp
, 0);
2336 switch (DECL_FUNCTION_CODE (fndecl
))
2338 CASE_FLT_FN (BUILT_IN_SIN
):
2339 CASE_FLT_FN (BUILT_IN_COS
):
2340 builtin_optab
= sincos_optab
; break;
2345 /* Make a suitable register to place result in. */
2346 mode
= TYPE_MODE (TREE_TYPE (exp
));
2348 /* Check if sincos insn is available, otherwise fallback
2349 to sin or cos insn. */
2350 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2351 switch (DECL_FUNCTION_CODE (fndecl
))
2353 CASE_FLT_FN (BUILT_IN_SIN
):
2354 builtin_optab
= sin_optab
; break;
2355 CASE_FLT_FN (BUILT_IN_COS
):
2356 builtin_optab
= cos_optab
; break;
2361 /* Before working hard, check whether the instruction is available. */
2362 if (optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
)
2364 rtx result
= gen_reg_rtx (mode
);
2366 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2367 need to expand the argument again. This way, we will not perform
2368 side-effects more the once. */
2369 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2371 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2375 /* Compute into RESULT.
2376 Set RESULT to wherever the result comes back. */
2377 if (builtin_optab
== sincos_optab
)
2381 switch (DECL_FUNCTION_CODE (fndecl
))
2383 CASE_FLT_FN (BUILT_IN_SIN
):
2384 ok
= expand_twoval_unop (builtin_optab
, op0
, 0, result
, 0);
2386 CASE_FLT_FN (BUILT_IN_COS
):
2387 ok
= expand_twoval_unop (builtin_optab
, op0
, result
, 0, 0);
2395 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2399 /* Output the entire sequence. */
2400 insns
= get_insns ();
2406 /* If we were unable to expand via the builtin, stop the sequence
2407 (without outputting the insns) and call to the library function
2408 with the stabilized argument list. */
2412 return expand_call (exp
, target
, target
== const0_rtx
);
2415 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2416 return an RTL instruction code that implements the functionality.
2417 If that isn't possible or available return CODE_FOR_nothing. */
2419 static enum insn_code
2420 interclass_mathfn_icode (tree arg
, tree fndecl
)
2422 bool errno_set
= false;
2423 optab builtin_optab
= unknown_optab
;
2424 enum machine_mode mode
;
2426 switch (DECL_FUNCTION_CODE (fndecl
))
2428 CASE_FLT_FN (BUILT_IN_ILOGB
):
2429 errno_set
= true; builtin_optab
= ilogb_optab
; break;
2430 CASE_FLT_FN (BUILT_IN_ISINF
):
2431 builtin_optab
= isinf_optab
; break;
2432 case BUILT_IN_ISNORMAL
:
2433 case BUILT_IN_ISFINITE
:
2434 CASE_FLT_FN (BUILT_IN_FINITE
):
2435 case BUILT_IN_FINITED32
:
2436 case BUILT_IN_FINITED64
:
2437 case BUILT_IN_FINITED128
:
2438 case BUILT_IN_ISINFD32
:
2439 case BUILT_IN_ISINFD64
:
2440 case BUILT_IN_ISINFD128
:
2441 /* These builtins have no optabs (yet). */
2447 /* There's no easy way to detect the case we need to set EDOM. */
2448 if (flag_errno_math
&& errno_set
)
2449 return CODE_FOR_nothing
;
2451 /* Optab mode depends on the mode of the input argument. */
2452 mode
= TYPE_MODE (TREE_TYPE (arg
));
2455 return optab_handler (builtin_optab
, mode
);
2456 return CODE_FOR_nothing
;
2459 /* Expand a call to one of the builtin math functions that operate on
2460 floating point argument and output an integer result (ilogb, isinf,
2462 Return 0 if a normal call should be emitted rather than expanding the
2463 function in-line. EXP is the expression that is a call to the builtin
2464 function; if convenient, the result should be placed in TARGET. */
2467 expand_builtin_interclass_mathfn (tree exp
, rtx target
)
2469 enum insn_code icode
= CODE_FOR_nothing
;
2471 tree fndecl
= get_callee_fndecl (exp
);
2472 enum machine_mode mode
;
2475 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2478 arg
= CALL_EXPR_ARG (exp
, 0);
2479 icode
= interclass_mathfn_icode (arg
, fndecl
);
2480 mode
= TYPE_MODE (TREE_TYPE (arg
));
2482 if (icode
!= CODE_FOR_nothing
)
2484 struct expand_operand ops
[1];
2485 rtx last
= get_last_insn ();
2486 tree orig_arg
= arg
;
2488 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2489 need to expand the argument again. This way, we will not perform
2490 side-effects more the once. */
2491 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2493 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2495 if (mode
!= GET_MODE (op0
))
2496 op0
= convert_to_mode (mode
, op0
, 0);
2498 create_output_operand (&ops
[0], target
, TYPE_MODE (TREE_TYPE (exp
)));
2499 if (maybe_legitimize_operands (icode
, 0, 1, ops
)
2500 && maybe_emit_unop_insn (icode
, ops
[0].value
, op0
, UNKNOWN
))
2501 return ops
[0].value
;
2503 delete_insns_since (last
);
2504 CALL_EXPR_ARG (exp
, 0) = orig_arg
;
2510 /* Expand a call to the builtin sincos math function.
2511 Return NULL_RTX if a normal call should be emitted rather than expanding the
2512 function in-line. EXP is the expression that is a call to the builtin
2516 expand_builtin_sincos (tree exp
)
2518 rtx op0
, op1
, op2
, target1
, target2
;
2519 enum machine_mode mode
;
2520 tree arg
, sinp
, cosp
;
2522 location_t loc
= EXPR_LOCATION (exp
);
2523 tree alias_type
, alias_off
;
2525 if (!validate_arglist (exp
, REAL_TYPE
,
2526 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2529 arg
= CALL_EXPR_ARG (exp
, 0);
2530 sinp
= CALL_EXPR_ARG (exp
, 1);
2531 cosp
= CALL_EXPR_ARG (exp
, 2);
2533 /* Make a suitable register to place result in. */
2534 mode
= TYPE_MODE (TREE_TYPE (arg
));
2536 /* Check if sincos insn is available, otherwise emit the call. */
2537 if (optab_handler (sincos_optab
, mode
) == CODE_FOR_nothing
)
2540 target1
= gen_reg_rtx (mode
);
2541 target2
= gen_reg_rtx (mode
);
2543 op0
= expand_normal (arg
);
2544 alias_type
= build_pointer_type_for_mode (TREE_TYPE (arg
), ptr_mode
, true);
2545 alias_off
= build_int_cst (alias_type
, 0);
2546 op1
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2548 op2
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2551 /* Compute into target1 and target2.
2552 Set TARGET to wherever the result comes back. */
2553 result
= expand_twoval_unop (sincos_optab
, op0
, target2
, target1
, 0);
2554 gcc_assert (result
);
2556 /* Move target1 and target2 to the memory locations indicated
2558 emit_move_insn (op1
, target1
);
2559 emit_move_insn (op2
, target2
);
2564 /* Expand a call to the internal cexpi builtin to the sincos math function.
2565 EXP is the expression that is a call to the builtin function; if convenient,
2566 the result should be placed in TARGET. */
2569 expand_builtin_cexpi (tree exp
, rtx target
)
2571 tree fndecl
= get_callee_fndecl (exp
);
2573 enum machine_mode mode
;
2575 location_t loc
= EXPR_LOCATION (exp
);
2577 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2580 arg
= CALL_EXPR_ARG (exp
, 0);
2581 type
= TREE_TYPE (arg
);
2582 mode
= TYPE_MODE (TREE_TYPE (arg
));
2584 /* Try expanding via a sincos optab, fall back to emitting a libcall
2585 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2586 is only generated from sincos, cexp or if we have either of them. */
2587 if (optab_handler (sincos_optab
, mode
) != CODE_FOR_nothing
)
2589 op1
= gen_reg_rtx (mode
);
2590 op2
= gen_reg_rtx (mode
);
2592 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2594 /* Compute into op1 and op2. */
2595 expand_twoval_unop (sincos_optab
, op0
, op2
, op1
, 0);
2597 else if (targetm
.libc_has_function (function_sincos
))
2599 tree call
, fn
= NULL_TREE
;
2603 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2604 fn
= builtin_decl_explicit (BUILT_IN_SINCOSF
);
2605 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2606 fn
= builtin_decl_explicit (BUILT_IN_SINCOS
);
2607 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2608 fn
= builtin_decl_explicit (BUILT_IN_SINCOSL
);
2612 op1
= assign_temp (TREE_TYPE (arg
), 1, 1);
2613 op2
= assign_temp (TREE_TYPE (arg
), 1, 1);
2614 op1a
= copy_addr_to_reg (XEXP (op1
, 0));
2615 op2a
= copy_addr_to_reg (XEXP (op2
, 0));
2616 top1
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op1a
);
2617 top2
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op2a
);
2619 /* Make sure not to fold the sincos call again. */
2620 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2621 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn
)),
2622 call
, 3, arg
, top1
, top2
));
2626 tree call
, fn
= NULL_TREE
, narg
;
2627 tree ctype
= build_complex_type (type
);
2629 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2630 fn
= builtin_decl_explicit (BUILT_IN_CEXPF
);
2631 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2632 fn
= builtin_decl_explicit (BUILT_IN_CEXP
);
2633 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2634 fn
= builtin_decl_explicit (BUILT_IN_CEXPL
);
2638 /* If we don't have a decl for cexp create one. This is the
2639 friendliest fallback if the user calls __builtin_cexpi
2640 without full target C99 function support. */
2641 if (fn
== NULL_TREE
)
2644 const char *name
= NULL
;
2646 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2648 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2650 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2653 fntype
= build_function_type_list (ctype
, ctype
, NULL_TREE
);
2654 fn
= build_fn_decl (name
, fntype
);
2657 narg
= fold_build2_loc (loc
, COMPLEX_EXPR
, ctype
,
2658 build_real (type
, dconst0
), arg
);
2660 /* Make sure not to fold the cexp call again. */
2661 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2662 return expand_expr (build_call_nary (ctype
, call
, 1, narg
),
2663 target
, VOIDmode
, EXPAND_NORMAL
);
2666 /* Now build the proper return type. */
2667 return expand_expr (build2 (COMPLEX_EXPR
, build_complex_type (type
),
2668 make_tree (TREE_TYPE (arg
), op2
),
2669 make_tree (TREE_TYPE (arg
), op1
)),
2670 target
, VOIDmode
, EXPAND_NORMAL
);
2673 /* Conveniently construct a function call expression. FNDECL names the
2674 function to be called, N is the number of arguments, and the "..."
2675 parameters are the argument expressions. Unlike build_call_exr
2676 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2679 build_call_nofold_loc (location_t loc
, tree fndecl
, int n
, ...)
2682 tree fntype
= TREE_TYPE (fndecl
);
2683 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), fndecl
);
2686 fn
= build_call_valist (TREE_TYPE (fntype
), fn
, n
, ap
);
2688 SET_EXPR_LOCATION (fn
, loc
);
2692 /* Expand a call to one of the builtin rounding functions gcc defines
2693 as an extension (lfloor and lceil). As these are gcc extensions we
2694 do not need to worry about setting errno to EDOM.
2695 If expanding via optab fails, lower expression to (int)(floor(x)).
2696 EXP is the expression that is a call to the builtin function;
2697 if convenient, the result should be placed in TARGET. */
2700 expand_builtin_int_roundingfn (tree exp
, rtx target
)
2702 convert_optab builtin_optab
;
2703 rtx op0
, insns
, tmp
;
2704 tree fndecl
= get_callee_fndecl (exp
);
2705 enum built_in_function fallback_fn
;
2706 tree fallback_fndecl
;
2707 enum machine_mode mode
;
2710 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2713 arg
= CALL_EXPR_ARG (exp
, 0);
2715 switch (DECL_FUNCTION_CODE (fndecl
))
2717 CASE_FLT_FN (BUILT_IN_ICEIL
):
2718 CASE_FLT_FN (BUILT_IN_LCEIL
):
2719 CASE_FLT_FN (BUILT_IN_LLCEIL
):
2720 builtin_optab
= lceil_optab
;
2721 fallback_fn
= BUILT_IN_CEIL
;
2724 CASE_FLT_FN (BUILT_IN_IFLOOR
):
2725 CASE_FLT_FN (BUILT_IN_LFLOOR
):
2726 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
2727 builtin_optab
= lfloor_optab
;
2728 fallback_fn
= BUILT_IN_FLOOR
;
2735 /* Make a suitable register to place result in. */
2736 mode
= TYPE_MODE (TREE_TYPE (exp
));
2738 target
= gen_reg_rtx (mode
);
2740 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2741 need to expand the argument again. This way, we will not perform
2742 side-effects more the once. */
2743 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2745 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2749 /* Compute into TARGET. */
2750 if (expand_sfix_optab (target
, op0
, builtin_optab
))
2752 /* Output the entire sequence. */
2753 insns
= get_insns ();
2759 /* If we were unable to expand via the builtin, stop the sequence
2760 (without outputting the insns). */
2763 /* Fall back to floating point rounding optab. */
2764 fallback_fndecl
= mathfn_built_in (TREE_TYPE (arg
), fallback_fn
);
2766 /* For non-C99 targets we may end up without a fallback fndecl here
2767 if the user called __builtin_lfloor directly. In this case emit
2768 a call to the floor/ceil variants nevertheless. This should result
2769 in the best user experience for not full C99 targets. */
2770 if (fallback_fndecl
== NULL_TREE
)
2773 const char *name
= NULL
;
2775 switch (DECL_FUNCTION_CODE (fndecl
))
2777 case BUILT_IN_ICEIL
:
2778 case BUILT_IN_LCEIL
:
2779 case BUILT_IN_LLCEIL
:
2782 case BUILT_IN_ICEILF
:
2783 case BUILT_IN_LCEILF
:
2784 case BUILT_IN_LLCEILF
:
2787 case BUILT_IN_ICEILL
:
2788 case BUILT_IN_LCEILL
:
2789 case BUILT_IN_LLCEILL
:
2792 case BUILT_IN_IFLOOR
:
2793 case BUILT_IN_LFLOOR
:
2794 case BUILT_IN_LLFLOOR
:
2797 case BUILT_IN_IFLOORF
:
2798 case BUILT_IN_LFLOORF
:
2799 case BUILT_IN_LLFLOORF
:
2802 case BUILT_IN_IFLOORL
:
2803 case BUILT_IN_LFLOORL
:
2804 case BUILT_IN_LLFLOORL
:
2811 fntype
= build_function_type_list (TREE_TYPE (arg
),
2812 TREE_TYPE (arg
), NULL_TREE
);
2813 fallback_fndecl
= build_fn_decl (name
, fntype
);
2816 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
), fallback_fndecl
, 1, arg
);
2818 tmp
= expand_normal (exp
);
2819 tmp
= maybe_emit_group_store (tmp
, TREE_TYPE (exp
));
2821 /* Truncate the result of floating point optab to integer
2822 via expand_fix (). */
2823 target
= gen_reg_rtx (mode
);
2824 expand_fix (target
, tmp
, 0);
2829 /* Expand a call to one of the builtin math functions doing integer
2831 Return 0 if a normal call should be emitted rather than expanding the
2832 function in-line. EXP is the expression that is a call to the builtin
2833 function; if convenient, the result should be placed in TARGET. */
2836 expand_builtin_int_roundingfn_2 (tree exp
, rtx target
)
2838 convert_optab builtin_optab
;
2840 tree fndecl
= get_callee_fndecl (exp
);
2842 enum machine_mode mode
;
2843 enum built_in_function fallback_fn
= BUILT_IN_NONE
;
2845 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2848 arg
= CALL_EXPR_ARG (exp
, 0);
2850 switch (DECL_FUNCTION_CODE (fndecl
))
2852 CASE_FLT_FN (BUILT_IN_IRINT
):
2853 fallback_fn
= BUILT_IN_LRINT
;
2855 CASE_FLT_FN (BUILT_IN_LRINT
):
2856 CASE_FLT_FN (BUILT_IN_LLRINT
):
2857 builtin_optab
= lrint_optab
;
2860 CASE_FLT_FN (BUILT_IN_IROUND
):
2861 fallback_fn
= BUILT_IN_LROUND
;
2863 CASE_FLT_FN (BUILT_IN_LROUND
):
2864 CASE_FLT_FN (BUILT_IN_LLROUND
):
2865 builtin_optab
= lround_optab
;
2872 /* There's no easy way to detect the case we need to set EDOM. */
2873 if (flag_errno_math
&& fallback_fn
== BUILT_IN_NONE
)
2876 /* Make a suitable register to place result in. */
2877 mode
= TYPE_MODE (TREE_TYPE (exp
));
2879 /* There's no easy way to detect the case we need to set EDOM. */
2880 if (!flag_errno_math
)
2882 rtx result
= gen_reg_rtx (mode
);
2884 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2885 need to expand the argument again. This way, we will not perform
2886 side-effects more the once. */
2887 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2889 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2893 if (expand_sfix_optab (result
, op0
, builtin_optab
))
2895 /* Output the entire sequence. */
2896 insns
= get_insns ();
2902 /* If we were unable to expand via the builtin, stop the sequence
2903 (without outputting the insns) and call to the library function
2904 with the stabilized argument list. */
2908 if (fallback_fn
!= BUILT_IN_NONE
)
2910 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2911 targets, (int) round (x) should never be transformed into
2912 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2913 a call to lround in the hope that the target provides at least some
2914 C99 functions. This should result in the best user experience for
2915 not full C99 targets. */
2916 tree fallback_fndecl
= mathfn_built_in_1 (TREE_TYPE (arg
),
2919 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
),
2920 fallback_fndecl
, 1, arg
);
2922 target
= expand_call (exp
, NULL_RTX
, target
== const0_rtx
);
2923 target
= maybe_emit_group_store (target
, TREE_TYPE (exp
));
2924 return convert_to_mode (mode
, target
, 0);
2927 return expand_call (exp
, target
, target
== const0_rtx
);
2930 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2931 a normal call should be emitted rather than expanding the function
2932 in-line. EXP is the expression that is a call to the builtin
2933 function; if convenient, the result should be placed in TARGET. */
2936 expand_builtin_powi (tree exp
, rtx target
)
2940 enum machine_mode mode
;
2941 enum machine_mode mode2
;
2943 if (! validate_arglist (exp
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2946 arg0
= CALL_EXPR_ARG (exp
, 0);
2947 arg1
= CALL_EXPR_ARG (exp
, 1);
2948 mode
= TYPE_MODE (TREE_TYPE (exp
));
2950 /* Emit a libcall to libgcc. */
2952 /* Mode of the 2nd argument must match that of an int. */
2953 mode2
= mode_for_size (INT_TYPE_SIZE
, MODE_INT
, 0);
2955 if (target
== NULL_RTX
)
2956 target
= gen_reg_rtx (mode
);
2958 op0
= expand_expr (arg0
, NULL_RTX
, mode
, EXPAND_NORMAL
);
2959 if (GET_MODE (op0
) != mode
)
2960 op0
= convert_to_mode (mode
, op0
, 0);
2961 op1
= expand_expr (arg1
, NULL_RTX
, mode2
, EXPAND_NORMAL
);
2962 if (GET_MODE (op1
) != mode2
)
2963 op1
= convert_to_mode (mode2
, op1
, 0);
2965 target
= emit_library_call_value (optab_libfunc (powi_optab
, mode
),
2966 target
, LCT_CONST
, mode
, 2,
2967 op0
, mode
, op1
, mode2
);
2972 /* Expand expression EXP which is a call to the strlen builtin. Return
2973 NULL_RTX if we failed the caller should emit a normal call, otherwise
2974 try to get the result in TARGET, if convenient. */
2977 expand_builtin_strlen (tree exp
, rtx target
,
2978 enum machine_mode target_mode
)
2980 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
2984 struct expand_operand ops
[4];
2987 tree src
= CALL_EXPR_ARG (exp
, 0);
2988 rtx src_reg
, before_strlen
;
2989 enum machine_mode insn_mode
= target_mode
;
2990 enum insn_code icode
= CODE_FOR_nothing
;
2993 /* If the length can be computed at compile-time, return it. */
2994 len
= c_strlen (src
, 0);
2996 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2998 /* If the length can be computed at compile-time and is constant
2999 integer, but there are side-effects in src, evaluate
3000 src for side-effects, then return len.
3001 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3002 can be optimized into: i++; x = 3; */
3003 len
= c_strlen (src
, 1);
3004 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
3006 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3007 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3010 align
= get_pointer_alignment (src
) / BITS_PER_UNIT
;
3012 /* If SRC is not a pointer type, don't do this operation inline. */
3016 /* Bail out if we can't compute strlen in the right mode. */
3017 while (insn_mode
!= VOIDmode
)
3019 icode
= optab_handler (strlen_optab
, insn_mode
);
3020 if (icode
!= CODE_FOR_nothing
)
3023 insn_mode
= GET_MODE_WIDER_MODE (insn_mode
);
3025 if (insn_mode
== VOIDmode
)
3028 /* Make a place to hold the source address. We will not expand
3029 the actual source until we are sure that the expansion will
3030 not fail -- there are trees that cannot be expanded twice. */
3031 src_reg
= gen_reg_rtx (Pmode
);
3033 /* Mark the beginning of the strlen sequence so we can emit the
3034 source operand later. */
3035 before_strlen
= get_last_insn ();
3037 create_output_operand (&ops
[0], target
, insn_mode
);
3038 create_fixed_operand (&ops
[1], gen_rtx_MEM (BLKmode
, src_reg
));
3039 create_integer_operand (&ops
[2], 0);
3040 create_integer_operand (&ops
[3], align
);
3041 if (!maybe_expand_insn (icode
, 4, ops
))
3044 /* Now that we are assured of success, expand the source. */
3046 pat
= expand_expr (src
, src_reg
, Pmode
, EXPAND_NORMAL
);
3049 #ifdef POINTERS_EXTEND_UNSIGNED
3050 if (GET_MODE (pat
) != Pmode
)
3051 pat
= convert_to_mode (Pmode
, pat
,
3052 POINTERS_EXTEND_UNSIGNED
);
3054 emit_move_insn (src_reg
, pat
);
3060 emit_insn_after (pat
, before_strlen
);
3062 emit_insn_before (pat
, get_insns ());
3064 /* Return the value in the proper mode for this function. */
3065 if (GET_MODE (ops
[0].value
) == target_mode
)
3066 target
= ops
[0].value
;
3067 else if (target
!= 0)
3068 convert_move (target
, ops
[0].value
, 0);
3070 target
= convert_to_mode (target_mode
, ops
[0].value
, 0);
3076 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3077 bytes from constant string DATA + OFFSET and return it as target
3081 builtin_memcpy_read_str (void *data
, HOST_WIDE_INT offset
,
3082 enum machine_mode mode
)
3084 const char *str
= (const char *) data
;
3086 gcc_assert (offset
>= 0
3087 && ((unsigned HOST_WIDE_INT
) offset
+ GET_MODE_SIZE (mode
)
3088 <= strlen (str
) + 1));
3090 return c_readstr (str
+ offset
, mode
);
3093 /* LEN specify length of the block of memcpy/memset operation.
3094 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3095 In some cases we can make very likely guess on max size, then we
3096 set it into PROBABLE_MAX_SIZE. */
3099 determine_block_size (tree len
, rtx len_rtx
,
3100 unsigned HOST_WIDE_INT
*min_size
,
3101 unsigned HOST_WIDE_INT
*max_size
,
3102 unsigned HOST_WIDE_INT
*probable_max_size
)
3104 if (CONST_INT_P (len_rtx
))
3106 *min_size
= *max_size
= *probable_max_size
= UINTVAL (len_rtx
);
3112 enum value_range_type range_type
= VR_UNDEFINED
;
3114 /* Determine bounds from the type. */
3115 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len
))))
3116 *min_size
= tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len
)));
3119 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len
))))
3120 *probable_max_size
= *max_size
3121 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len
)));
3123 *probable_max_size
= *max_size
= GET_MODE_MASK (GET_MODE (len_rtx
));
3125 if (TREE_CODE (len
) == SSA_NAME
)
3126 range_type
= get_range_info (len
, &min
, &max
);
3127 if (range_type
== VR_RANGE
)
3129 if (wi::fits_uhwi_p (min
) && *min_size
< min
.to_uhwi ())
3130 *min_size
= min
.to_uhwi ();
3131 if (wi::fits_uhwi_p (max
) && *max_size
> max
.to_uhwi ())
3132 *probable_max_size
= *max_size
= max
.to_uhwi ();
3134 else if (range_type
== VR_ANTI_RANGE
)
3136 /* Anti range 0...N lets us to determine minimal size to N+1. */
3139 if (wi::fits_uhwi_p (max
) && max
.to_uhwi () + 1 != 0)
3140 *min_size
= max
.to_uhwi () + 1;
3148 Produce anti range allowing negative values of N. We still
3149 can use the information and make a guess that N is not negative.
3151 else if (!wi::leu_p (max
, 1 << 30) && wi::fits_uhwi_p (min
))
3152 *probable_max_size
= min
.to_uhwi () - 1;
3155 gcc_checking_assert (*max_size
<=
3156 (unsigned HOST_WIDE_INT
)
3157 GET_MODE_MASK (GET_MODE (len_rtx
)));
3160 /* Expand a call EXP to the memcpy builtin.
3161 Return NULL_RTX if we failed, the caller should emit a normal call,
3162 otherwise try to get the result in TARGET, if convenient (and in
3163 mode MODE if that's convenient). */
3166 expand_builtin_memcpy (tree exp
, rtx target
)
3168 if (!validate_arglist (exp
,
3169 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3173 tree dest
= CALL_EXPR_ARG (exp
, 0);
3174 tree src
= CALL_EXPR_ARG (exp
, 1);
3175 tree len
= CALL_EXPR_ARG (exp
, 2);
3176 const char *src_str
;
3177 unsigned int src_align
= get_pointer_alignment (src
);
3178 unsigned int dest_align
= get_pointer_alignment (dest
);
3179 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
3180 HOST_WIDE_INT expected_size
= -1;
3181 unsigned int expected_align
= 0;
3182 unsigned HOST_WIDE_INT min_size
;
3183 unsigned HOST_WIDE_INT max_size
;
3184 unsigned HOST_WIDE_INT probable_max_size
;
3186 /* If DEST is not a pointer type, call the normal function. */
3187 if (dest_align
== 0)
3190 /* If either SRC is not a pointer type, don't do this
3191 operation in-line. */
3195 if (currently_expanding_gimple_stmt
)
3196 stringop_block_profile (currently_expanding_gimple_stmt
,
3197 &expected_align
, &expected_size
);
3199 if (expected_align
< dest_align
)
3200 expected_align
= dest_align
;
3201 dest_mem
= get_memory_rtx (dest
, len
);
3202 set_mem_align (dest_mem
, dest_align
);
3203 len_rtx
= expand_normal (len
);
3204 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3205 &probable_max_size
);
3206 src_str
= c_getstr (src
);
3208 /* If SRC is a string constant and block move would be done
3209 by pieces, we can avoid loading the string from memory
3210 and only stored the computed constants. */
3212 && CONST_INT_P (len_rtx
)
3213 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3214 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3215 CONST_CAST (char *, src_str
),
3218 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3219 builtin_memcpy_read_str
,
3220 CONST_CAST (char *, src_str
),
3221 dest_align
, false, 0);
3222 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3223 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3227 src_mem
= get_memory_rtx (src
, len
);
3228 set_mem_align (src_mem
, src_align
);
3230 /* Copy word part most expediently. */
3231 dest_addr
= emit_block_move_hints (dest_mem
, src_mem
, len_rtx
,
3232 CALL_EXPR_TAILCALL (exp
)
3233 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3234 expected_align
, expected_size
,
3235 min_size
, max_size
, probable_max_size
);
3239 dest_addr
= force_operand (XEXP (dest_mem
, 0), target
);
3240 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3246 /* Expand a call EXP to the mempcpy builtin.
3247 Return NULL_RTX if we failed; the caller should emit a normal call,
3248 otherwise try to get the result in TARGET, if convenient (and in
3249 mode MODE if that's convenient). If ENDP is 0 return the
3250 destination pointer, if ENDP is 1 return the end pointer ala
3251 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3255 expand_builtin_mempcpy (tree exp
, rtx target
, enum machine_mode mode
)
3257 if (!validate_arglist (exp
,
3258 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3262 tree dest
= CALL_EXPR_ARG (exp
, 0);
3263 tree src
= CALL_EXPR_ARG (exp
, 1);
3264 tree len
= CALL_EXPR_ARG (exp
, 2);
3265 return expand_builtin_mempcpy_args (dest
, src
, len
,
3266 target
, mode
, /*endp=*/ 1);
3270 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3271 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3272 so that this can also be called without constructing an actual CALL_EXPR.
3273 The other arguments and return value are the same as for
3274 expand_builtin_mempcpy. */
3277 expand_builtin_mempcpy_args (tree dest
, tree src
, tree len
,
3278 rtx target
, enum machine_mode mode
, int endp
)
3280 /* If return value is ignored, transform mempcpy into memcpy. */
3281 if (target
== const0_rtx
&& builtin_decl_implicit_p (BUILT_IN_MEMCPY
))
3283 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
3284 tree result
= build_call_nofold_loc (UNKNOWN_LOCATION
, fn
, 3,
3286 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3290 const char *src_str
;
3291 unsigned int src_align
= get_pointer_alignment (src
);
3292 unsigned int dest_align
= get_pointer_alignment (dest
);
3293 rtx dest_mem
, src_mem
, len_rtx
;
3295 /* If either SRC or DEST is not a pointer type, don't do this
3296 operation in-line. */
3297 if (dest_align
== 0 || src_align
== 0)
3300 /* If LEN is not constant, call the normal function. */
3301 if (! tree_fits_uhwi_p (len
))
3304 len_rtx
= expand_normal (len
);
3305 src_str
= c_getstr (src
);
3307 /* If SRC is a string constant and block move would be done
3308 by pieces, we can avoid loading the string from memory
3309 and only stored the computed constants. */
3311 && CONST_INT_P (len_rtx
)
3312 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3313 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3314 CONST_CAST (char *, src_str
),
3317 dest_mem
= get_memory_rtx (dest
, len
);
3318 set_mem_align (dest_mem
, dest_align
);
3319 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3320 builtin_memcpy_read_str
,
3321 CONST_CAST (char *, src_str
),
3322 dest_align
, false, endp
);
3323 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3324 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3328 if (CONST_INT_P (len_rtx
)
3329 && can_move_by_pieces (INTVAL (len_rtx
),
3330 MIN (dest_align
, src_align
)))
3332 dest_mem
= get_memory_rtx (dest
, len
);
3333 set_mem_align (dest_mem
, dest_align
);
3334 src_mem
= get_memory_rtx (src
, len
);
3335 set_mem_align (src_mem
, src_align
);
3336 dest_mem
= move_by_pieces (dest_mem
, src_mem
, INTVAL (len_rtx
),
3337 MIN (dest_align
, src_align
), endp
);
3338 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3339 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3348 # define HAVE_movstr 0
3349 # define CODE_FOR_movstr CODE_FOR_nothing
3352 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3353 we failed, the caller should emit a normal call, otherwise try to
3354 get the result in TARGET, if convenient. If ENDP is 0 return the
3355 destination pointer, if ENDP is 1 return the end pointer ala
3356 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3360 expand_movstr (tree dest
, tree src
, rtx target
, int endp
)
3362 struct expand_operand ops
[3];
3369 dest_mem
= get_memory_rtx (dest
, NULL
);
3370 src_mem
= get_memory_rtx (src
, NULL
);
3373 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
3374 dest_mem
= replace_equiv_address (dest_mem
, target
);
3377 create_output_operand (&ops
[0], endp
? target
: NULL_RTX
, Pmode
);
3378 create_fixed_operand (&ops
[1], dest_mem
);
3379 create_fixed_operand (&ops
[2], src_mem
);
3380 if (!maybe_expand_insn (CODE_FOR_movstr
, 3, ops
))
3383 if (endp
&& target
!= const0_rtx
)
3385 target
= ops
[0].value
;
3386 /* movstr is supposed to set end to the address of the NUL
3387 terminator. If the caller requested a mempcpy-like return value,
3391 rtx tem
= plus_constant (GET_MODE (target
),
3392 gen_lowpart (GET_MODE (target
), target
), 1);
3393 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
3399 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3400 NULL_RTX if we failed the caller should emit a normal call, otherwise
3401 try to get the result in TARGET, if convenient (and in mode MODE if that's
3405 expand_builtin_strcpy (tree exp
, rtx target
)
3407 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3409 tree dest
= CALL_EXPR_ARG (exp
, 0);
3410 tree src
= CALL_EXPR_ARG (exp
, 1);
3411 return expand_builtin_strcpy_args (dest
, src
, target
);
3416 /* Helper function to do the actual work for expand_builtin_strcpy. The
3417 arguments to the builtin_strcpy call DEST and SRC are broken out
3418 so that this can also be called without constructing an actual CALL_EXPR.
3419 The other arguments and return value are the same as for
3420 expand_builtin_strcpy. */
3423 expand_builtin_strcpy_args (tree dest
, tree src
, rtx target
)
3425 return expand_movstr (dest
, src
, target
, /*endp=*/0);
3428 /* Expand a call EXP to the stpcpy builtin.
3429 Return NULL_RTX if we failed the caller should emit a normal call,
3430 otherwise try to get the result in TARGET, if convenient (and in
3431 mode MODE if that's convenient). */
3434 expand_builtin_stpcpy (tree exp
, rtx target
, enum machine_mode mode
)
3437 location_t loc
= EXPR_LOCATION (exp
);
3439 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3442 dst
= CALL_EXPR_ARG (exp
, 0);
3443 src
= CALL_EXPR_ARG (exp
, 1);
3445 /* If return value is ignored, transform stpcpy into strcpy. */
3446 if (target
== const0_rtx
&& builtin_decl_implicit (BUILT_IN_STRCPY
))
3448 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3449 tree result
= build_call_nofold_loc (loc
, fn
, 2, dst
, src
);
3450 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3457 /* Ensure we get an actual string whose length can be evaluated at
3458 compile-time, not an expression containing a string. This is
3459 because the latter will potentially produce pessimized code
3460 when used to produce the return value. */
3461 if (! c_getstr (src
) || ! (len
= c_strlen (src
, 0)))
3462 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3464 lenp1
= size_binop_loc (loc
, PLUS_EXPR
, len
, ssize_int (1));
3465 ret
= expand_builtin_mempcpy_args (dst
, src
, lenp1
,
3466 target
, mode
, /*endp=*/2);
3471 if (TREE_CODE (len
) == INTEGER_CST
)
3473 rtx len_rtx
= expand_normal (len
);
3475 if (CONST_INT_P (len_rtx
))
3477 ret
= expand_builtin_strcpy_args (dst
, src
, target
);
3483 if (mode
!= VOIDmode
)
3484 target
= gen_reg_rtx (mode
);
3486 target
= gen_reg_rtx (GET_MODE (ret
));
3488 if (GET_MODE (target
) != GET_MODE (ret
))
3489 ret
= gen_lowpart (GET_MODE (target
), ret
);
3491 ret
= plus_constant (GET_MODE (ret
), ret
, INTVAL (len_rtx
));
3492 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
3500 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3504 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3505 bytes from constant string DATA + OFFSET and return it as target
3509 builtin_strncpy_read_str (void *data
, HOST_WIDE_INT offset
,
3510 enum machine_mode mode
)
3512 const char *str
= (const char *) data
;
3514 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
3517 return c_readstr (str
+ offset
, mode
);
3520 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3521 NULL_RTX if we failed the caller should emit a normal call. */
3524 expand_builtin_strncpy (tree exp
, rtx target
)
3526 location_t loc
= EXPR_LOCATION (exp
);
3528 if (validate_arglist (exp
,
3529 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3531 tree dest
= CALL_EXPR_ARG (exp
, 0);
3532 tree src
= CALL_EXPR_ARG (exp
, 1);
3533 tree len
= CALL_EXPR_ARG (exp
, 2);
3534 tree slen
= c_strlen (src
, 1);
3536 /* We must be passed a constant len and src parameter. */
3537 if (!tree_fits_uhwi_p (len
) || !slen
|| !tree_fits_uhwi_p (slen
))
3540 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
3542 /* We're required to pad with trailing zeros if the requested
3543 len is greater than strlen(s2)+1. In that case try to
3544 use store_by_pieces, if it fails, punt. */
3545 if (tree_int_cst_lt (slen
, len
))
3547 unsigned int dest_align
= get_pointer_alignment (dest
);
3548 const char *p
= c_getstr (src
);
3551 if (!p
|| dest_align
== 0 || !tree_fits_uhwi_p (len
)
3552 || !can_store_by_pieces (tree_to_uhwi (len
),
3553 builtin_strncpy_read_str
,
3554 CONST_CAST (char *, p
),
3558 dest_mem
= get_memory_rtx (dest
, len
);
3559 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3560 builtin_strncpy_read_str
,
3561 CONST_CAST (char *, p
), dest_align
, false, 0);
3562 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3563 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3570 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3571 bytes from constant string DATA + OFFSET and return it as target
3575 builtin_memset_read_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3576 enum machine_mode mode
)
3578 const char *c
= (const char *) data
;
3579 char *p
= XALLOCAVEC (char, GET_MODE_SIZE (mode
));
3581 memset (p
, *c
, GET_MODE_SIZE (mode
));
3583 return c_readstr (p
, mode
);
3586 /* Callback routine for store_by_pieces. Return the RTL of a register
3587 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3588 char value given in the RTL register data. For example, if mode is
3589 4 bytes wide, return the RTL for 0x01010101*data. */
3592 builtin_memset_gen_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3593 enum machine_mode mode
)
3599 size
= GET_MODE_SIZE (mode
);
3603 p
= XALLOCAVEC (char, size
);
3604 memset (p
, 1, size
);
3605 coeff
= c_readstr (p
, mode
);
3607 target
= convert_to_mode (mode
, (rtx
) data
, 1);
3608 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
3609 return force_reg (mode
, target
);
3612 /* Expand expression EXP, which is a call to the memset builtin. Return
3613 NULL_RTX if we failed the caller should emit a normal call, otherwise
3614 try to get the result in TARGET, if convenient (and in mode MODE if that's
3618 expand_builtin_memset (tree exp
, rtx target
, enum machine_mode mode
)
3620 if (!validate_arglist (exp
,
3621 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3625 tree dest
= CALL_EXPR_ARG (exp
, 0);
3626 tree val
= CALL_EXPR_ARG (exp
, 1);
3627 tree len
= CALL_EXPR_ARG (exp
, 2);
3628 return expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
3632 /* Helper function to do the actual work for expand_builtin_memset. The
3633 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3634 so that this can also be called without constructing an actual CALL_EXPR.
3635 The other arguments and return value are the same as for
3636 expand_builtin_memset. */
3639 expand_builtin_memset_args (tree dest
, tree val
, tree len
,
3640 rtx target
, enum machine_mode mode
, tree orig_exp
)
3643 enum built_in_function fcode
;
3644 enum machine_mode val_mode
;
3646 unsigned int dest_align
;
3647 rtx dest_mem
, dest_addr
, len_rtx
;
3648 HOST_WIDE_INT expected_size
= -1;
3649 unsigned int expected_align
= 0;
3650 unsigned HOST_WIDE_INT min_size
;
3651 unsigned HOST_WIDE_INT max_size
;
3652 unsigned HOST_WIDE_INT probable_max_size
;
3654 dest_align
= get_pointer_alignment (dest
);
3656 /* If DEST is not a pointer type, don't do this operation in-line. */
3657 if (dest_align
== 0)
3660 if (currently_expanding_gimple_stmt
)
3661 stringop_block_profile (currently_expanding_gimple_stmt
,
3662 &expected_align
, &expected_size
);
3664 if (expected_align
< dest_align
)
3665 expected_align
= dest_align
;
3667 /* If the LEN parameter is zero, return DEST. */
3668 if (integer_zerop (len
))
3670 /* Evaluate and ignore VAL in case it has side-effects. */
3671 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3672 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
3675 /* Stabilize the arguments in case we fail. */
3676 dest
= builtin_save_expr (dest
);
3677 val
= builtin_save_expr (val
);
3678 len
= builtin_save_expr (len
);
3680 len_rtx
= expand_normal (len
);
3681 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3682 &probable_max_size
);
3683 dest_mem
= get_memory_rtx (dest
, len
);
3684 val_mode
= TYPE_MODE (unsigned_char_type_node
);
3686 if (TREE_CODE (val
) != INTEGER_CST
)
3690 val_rtx
= expand_normal (val
);
3691 val_rtx
= convert_to_mode (val_mode
, val_rtx
, 0);
3693 /* Assume that we can memset by pieces if we can store
3694 * the coefficients by pieces (in the required modes).
3695 * We can't pass builtin_memset_gen_str as that emits RTL. */
3697 if (tree_fits_uhwi_p (len
)
3698 && can_store_by_pieces (tree_to_uhwi (len
),
3699 builtin_memset_read_str
, &c
, dest_align
,
3702 val_rtx
= force_reg (val_mode
, val_rtx
);
3703 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3704 builtin_memset_gen_str
, val_rtx
, dest_align
,
3707 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, val_rtx
,
3708 dest_align
, expected_align
,
3709 expected_size
, min_size
, max_size
,
3713 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3714 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3718 if (target_char_cast (val
, &c
))
3723 if (tree_fits_uhwi_p (len
)
3724 && can_store_by_pieces (tree_to_uhwi (len
),
3725 builtin_memset_read_str
, &c
, dest_align
,
3727 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3728 builtin_memset_read_str
, &c
, dest_align
, true, 0);
3729 else if (!set_storage_via_setmem (dest_mem
, len_rtx
,
3730 gen_int_mode (c
, val_mode
),
3731 dest_align
, expected_align
,
3732 expected_size
, min_size
, max_size
,
3736 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3737 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3741 set_mem_align (dest_mem
, dest_align
);
3742 dest_addr
= clear_storage_hints (dest_mem
, len_rtx
,
3743 CALL_EXPR_TAILCALL (orig_exp
)
3744 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3745 expected_align
, expected_size
,
3751 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3752 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3758 fndecl
= get_callee_fndecl (orig_exp
);
3759 fcode
= DECL_FUNCTION_CODE (fndecl
);
3760 if (fcode
== BUILT_IN_MEMSET
)
3761 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 3,
3763 else if (fcode
== BUILT_IN_BZERO
)
3764 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 2,
3768 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
3769 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
3770 return expand_call (fn
, target
, target
== const0_rtx
);
3773 /* Expand expression EXP, which is a call to the bzero builtin. Return
3774 NULL_RTX if we failed the caller should emit a normal call. */
3777 expand_builtin_bzero (tree exp
)
3780 location_t loc
= EXPR_LOCATION (exp
);
3782 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3785 dest
= CALL_EXPR_ARG (exp
, 0);
3786 size
= CALL_EXPR_ARG (exp
, 1);
3788 /* New argument list transforming bzero(ptr x, int y) to
3789 memset(ptr x, int 0, size_t y). This is done this way
3790 so that if it isn't expanded inline, we fallback to
3791 calling bzero instead of memset. */
3793 return expand_builtin_memset_args (dest
, integer_zero_node
,
3794 fold_convert_loc (loc
,
3795 size_type_node
, size
),
3796 const0_rtx
, VOIDmode
, exp
);
3799 /* Expand expression EXP, which is a call to the memcmp built-in function.
3800 Return NULL_RTX if we failed and the caller should emit a normal call,
3801 otherwise try to get the result in TARGET, if convenient (and in mode
3802 MODE, if that's convenient). */
3805 expand_builtin_memcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
3806 ATTRIBUTE_UNUSED
enum machine_mode mode
)
3808 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
3810 if (!validate_arglist (exp
,
3811 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3814 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3815 implementing memcmp because it will stop if it encounters two
3817 #if defined HAVE_cmpmemsi
3819 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
3822 tree arg1
= CALL_EXPR_ARG (exp
, 0);
3823 tree arg2
= CALL_EXPR_ARG (exp
, 1);
3824 tree len
= CALL_EXPR_ARG (exp
, 2);
3826 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
3827 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
3828 enum machine_mode insn_mode
;
3831 insn_mode
= insn_data
[(int) CODE_FOR_cmpmemsi
].operand
[0].mode
;
3835 /* If we don't have POINTER_TYPE, call the function. */
3836 if (arg1_align
== 0 || arg2_align
== 0)
3839 /* Make a place to write the result of the instruction. */
3842 && REG_P (result
) && GET_MODE (result
) == insn_mode
3843 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3844 result
= gen_reg_rtx (insn_mode
);
3846 arg1_rtx
= get_memory_rtx (arg1
, len
);
3847 arg2_rtx
= get_memory_rtx (arg2
, len
);
3848 arg3_rtx
= expand_normal (fold_convert_loc (loc
, sizetype
, len
));
3850 /* Set MEM_SIZE as appropriate. */
3851 if (CONST_INT_P (arg3_rtx
))
3853 set_mem_size (arg1_rtx
, INTVAL (arg3_rtx
));
3854 set_mem_size (arg2_rtx
, INTVAL (arg3_rtx
));
3858 insn
= gen_cmpmemsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3859 GEN_INT (MIN (arg1_align
, arg2_align
)));
3866 emit_library_call_value (memcmp_libfunc
, result
, LCT_PURE
,
3867 TYPE_MODE (integer_type_node
), 3,
3868 XEXP (arg1_rtx
, 0), Pmode
,
3869 XEXP (arg2_rtx
, 0), Pmode
,
3870 convert_to_mode (TYPE_MODE (sizetype
), arg3_rtx
,
3871 TYPE_UNSIGNED (sizetype
)),
3872 TYPE_MODE (sizetype
));
3874 /* Return the value in the proper mode for this function. */
3875 mode
= TYPE_MODE (TREE_TYPE (exp
));
3876 if (GET_MODE (result
) == mode
)
3878 else if (target
!= 0)
3880 convert_move (target
, result
, 0);
3884 return convert_to_mode (mode
, result
, 0);
3886 #endif /* HAVE_cmpmemsi. */
3891 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3892 if we failed the caller should emit a normal call, otherwise try to get
3893 the result in TARGET, if convenient. */
3896 expand_builtin_strcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
)
3898 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3901 #if defined HAVE_cmpstrsi || defined HAVE_cmpstrnsi
3902 if (direct_optab_handler (cmpstr_optab
, SImode
) != CODE_FOR_nothing
3903 || direct_optab_handler (cmpstrn_optab
, SImode
) != CODE_FOR_nothing
)
3905 rtx arg1_rtx
, arg2_rtx
;
3906 rtx result
, insn
= NULL_RTX
;
3908 tree arg1
= CALL_EXPR_ARG (exp
, 0);
3909 tree arg2
= CALL_EXPR_ARG (exp
, 1);
3911 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
3912 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
3914 /* If we don't have POINTER_TYPE, call the function. */
3915 if (arg1_align
== 0 || arg2_align
== 0)
3918 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
3919 arg1
= builtin_save_expr (arg1
);
3920 arg2
= builtin_save_expr (arg2
);
3922 arg1_rtx
= get_memory_rtx (arg1
, NULL
);
3923 arg2_rtx
= get_memory_rtx (arg2
, NULL
);
3925 #ifdef HAVE_cmpstrsi
3926 /* Try to call cmpstrsi. */
3929 enum machine_mode insn_mode
3930 = insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
3932 /* Make a place to write the result of the instruction. */
3935 && REG_P (result
) && GET_MODE (result
) == insn_mode
3936 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3937 result
= gen_reg_rtx (insn_mode
);
3939 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
,
3940 GEN_INT (MIN (arg1_align
, arg2_align
)));
3943 #ifdef HAVE_cmpstrnsi
3944 /* Try to determine at least one length and call cmpstrnsi. */
3945 if (!insn
&& HAVE_cmpstrnsi
)
3950 enum machine_mode insn_mode
3951 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
3952 tree len1
= c_strlen (arg1
, 1);
3953 tree len2
= c_strlen (arg2
, 1);
3956 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
3958 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
3960 /* If we don't have a constant length for the first, use the length
3961 of the second, if we know it. We don't require a constant for
3962 this case; some cost analysis could be done if both are available
3963 but neither is constant. For now, assume they're equally cheap,
3964 unless one has side effects. If both strings have constant lengths,
3971 else if (TREE_SIDE_EFFECTS (len1
))
3973 else if (TREE_SIDE_EFFECTS (len2
))
3975 else if (TREE_CODE (len1
) != INTEGER_CST
)
3977 else if (TREE_CODE (len2
) != INTEGER_CST
)
3979 else if (tree_int_cst_lt (len1
, len2
))
3984 /* If both arguments have side effects, we cannot optimize. */
3985 if (!len
|| TREE_SIDE_EFFECTS (len
))
3988 arg3_rtx
= expand_normal (len
);
3990 /* Make a place to write the result of the instruction. */
3993 && REG_P (result
) && GET_MODE (result
) == insn_mode
3994 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3995 result
= gen_reg_rtx (insn_mode
);
3997 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
3998 GEN_INT (MIN (arg1_align
, arg2_align
)));
4004 enum machine_mode mode
;
4007 /* Return the value in the proper mode for this function. */
4008 mode
= TYPE_MODE (TREE_TYPE (exp
));
4009 if (GET_MODE (result
) == mode
)
4012 return convert_to_mode (mode
, result
, 0);
4013 convert_move (target
, result
, 0);
4017 /* Expand the library call ourselves using a stabilized argument
4018 list to avoid re-evaluating the function's arguments twice. */
4019 #ifdef HAVE_cmpstrnsi
4022 fndecl
= get_callee_fndecl (exp
);
4023 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 2, arg1
, arg2
);
4024 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4025 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4026 return expand_call (fn
, target
, target
== const0_rtx
);
4032 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4033 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4034 the result in TARGET, if convenient. */
4037 expand_builtin_strncmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
4038 ATTRIBUTE_UNUSED
enum machine_mode mode
)
4040 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
4042 if (!validate_arglist (exp
,
4043 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4046 /* If c_strlen can determine an expression for one of the string
4047 lengths, and it doesn't have side effects, then emit cmpstrnsi
4048 using length MIN(strlen(string)+1, arg3). */
4049 #ifdef HAVE_cmpstrnsi
4052 tree len
, len1
, len2
;
4053 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
4056 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4057 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4058 tree arg3
= CALL_EXPR_ARG (exp
, 2);
4060 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4061 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4062 enum machine_mode insn_mode
4063 = insn_data
[(int) CODE_FOR_cmpstrnsi
].operand
[0].mode
;
4065 len1
= c_strlen (arg1
, 1);
4066 len2
= c_strlen (arg2
, 1);
4069 len1
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len1
);
4071 len2
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len2
);
4073 /* If we don't have a constant length for the first, use the length
4074 of the second, if we know it. We don't require a constant for
4075 this case; some cost analysis could be done if both are available
4076 but neither is constant. For now, assume they're equally cheap,
4077 unless one has side effects. If both strings have constant lengths,
4084 else if (TREE_SIDE_EFFECTS (len1
))
4086 else if (TREE_SIDE_EFFECTS (len2
))
4088 else if (TREE_CODE (len1
) != INTEGER_CST
)
4090 else if (TREE_CODE (len2
) != INTEGER_CST
)
4092 else if (tree_int_cst_lt (len1
, len2
))
4097 /* If both arguments have side effects, we cannot optimize. */
4098 if (!len
|| TREE_SIDE_EFFECTS (len
))
4101 /* The actual new length parameter is MIN(len,arg3). */
4102 len
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (len
), len
,
4103 fold_convert_loc (loc
, TREE_TYPE (len
), arg3
));
4105 /* If we don't have POINTER_TYPE, call the function. */
4106 if (arg1_align
== 0 || arg2_align
== 0)
4109 /* Make a place to write the result of the instruction. */
4112 && REG_P (result
) && GET_MODE (result
) == insn_mode
4113 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
4114 result
= gen_reg_rtx (insn_mode
);
4116 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4117 arg1
= builtin_save_expr (arg1
);
4118 arg2
= builtin_save_expr (arg2
);
4119 len
= builtin_save_expr (len
);
4121 arg1_rtx
= get_memory_rtx (arg1
, len
);
4122 arg2_rtx
= get_memory_rtx (arg2
, len
);
4123 arg3_rtx
= expand_normal (len
);
4124 insn
= gen_cmpstrnsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
4125 GEN_INT (MIN (arg1_align
, arg2_align
)));
4130 /* Return the value in the proper mode for this function. */
4131 mode
= TYPE_MODE (TREE_TYPE (exp
));
4132 if (GET_MODE (result
) == mode
)
4135 return convert_to_mode (mode
, result
, 0);
4136 convert_move (target
, result
, 0);
4140 /* Expand the library call ourselves using a stabilized argument
4141 list to avoid re-evaluating the function's arguments twice. */
4142 fndecl
= get_callee_fndecl (exp
);
4143 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 3,
4145 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4146 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4147 return expand_call (fn
, target
, target
== const0_rtx
);
4153 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4154 if that's convenient. */
4157 expand_builtin_saveregs (void)
4161 /* Don't do __builtin_saveregs more than once in a function.
4162 Save the result of the first call and reuse it. */
4163 if (saveregs_value
!= 0)
4164 return saveregs_value
;
4166 /* When this function is called, it means that registers must be
4167 saved on entry to this function. So we migrate the call to the
4168 first insn of this function. */
4172 /* Do whatever the machine needs done in this case. */
4173 val
= targetm
.calls
.expand_builtin_saveregs ();
4178 saveregs_value
= val
;
4180 /* Put the insns after the NOTE that starts the function. If this
4181 is inside a start_sequence, make the outer-level insn chain current, so
4182 the code is placed at the start of the function. */
4183 push_topmost_sequence ();
4184 emit_insn_after (seq
, entry_of_function ());
4185 pop_topmost_sequence ();
4190 /* Expand a call to __builtin_next_arg. */
4193 expand_builtin_next_arg (void)
4195 /* Checking arguments is already done in fold_builtin_next_arg
4196 that must be called before this function. */
4197 return expand_binop (ptr_mode
, add_optab
,
4198 crtl
->args
.internal_arg_pointer
,
4199 crtl
->args
.arg_offset_rtx
,
4200 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
4203 /* Make it easier for the backends by protecting the valist argument
4204 from multiple evaluations. */
4207 stabilize_va_list_loc (location_t loc
, tree valist
, int needs_lvalue
)
4209 tree vatype
= targetm
.canonical_va_list_type (TREE_TYPE (valist
));
4211 /* The current way of determining the type of valist is completely
4212 bogus. We should have the information on the va builtin instead. */
4214 vatype
= targetm
.fn_abi_va_list (cfun
->decl
);
4216 if (TREE_CODE (vatype
) == ARRAY_TYPE
)
4218 if (TREE_SIDE_EFFECTS (valist
))
4219 valist
= save_expr (valist
);
4221 /* For this case, the backends will be expecting a pointer to
4222 vatype, but it's possible we've actually been given an array
4223 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4225 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4227 tree p1
= build_pointer_type (TREE_TYPE (vatype
));
4228 valist
= build_fold_addr_expr_with_type_loc (loc
, valist
, p1
);
4233 tree pt
= build_pointer_type (vatype
);
4237 if (! TREE_SIDE_EFFECTS (valist
))
4240 valist
= fold_build1_loc (loc
, ADDR_EXPR
, pt
, valist
);
4241 TREE_SIDE_EFFECTS (valist
) = 1;
4244 if (TREE_SIDE_EFFECTS (valist
))
4245 valist
= save_expr (valist
);
4246 valist
= fold_build2_loc (loc
, MEM_REF
,
4247 vatype
, valist
, build_int_cst (pt
, 0));
4253 /* The "standard" definition of va_list is void*. */
4256 std_build_builtin_va_list (void)
4258 return ptr_type_node
;
4261 /* The "standard" abi va_list is va_list_type_node. */
4264 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED
)
4266 return va_list_type_node
;
4269 /* The "standard" type of va_list is va_list_type_node. */
4272 std_canonical_va_list_type (tree type
)
4276 if (INDIRECT_REF_P (type
))
4277 type
= TREE_TYPE (type
);
4278 else if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (TREE_TYPE (type
)))
4279 type
= TREE_TYPE (type
);
4280 wtype
= va_list_type_node
;
4282 /* Treat structure va_list types. */
4283 if (TREE_CODE (wtype
) == RECORD_TYPE
&& POINTER_TYPE_P (htype
))
4284 htype
= TREE_TYPE (htype
);
4285 else if (TREE_CODE (wtype
) == ARRAY_TYPE
)
4287 /* If va_list is an array type, the argument may have decayed
4288 to a pointer type, e.g. by being passed to another function.
4289 In that case, unwrap both types so that we can compare the
4290 underlying records. */
4291 if (TREE_CODE (htype
) == ARRAY_TYPE
4292 || POINTER_TYPE_P (htype
))
4294 wtype
= TREE_TYPE (wtype
);
4295 htype
= TREE_TYPE (htype
);
4298 if (TYPE_MAIN_VARIANT (wtype
) == TYPE_MAIN_VARIANT (htype
))
4299 return va_list_type_node
;
4304 /* The "standard" implementation of va_start: just assign `nextarg' to
4308 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
4310 rtx va_r
= expand_expr (valist
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
4311 convert_move (va_r
, nextarg
, 0);
4314 /* Expand EXP, a call to __builtin_va_start. */
4317 expand_builtin_va_start (tree exp
)
4321 location_t loc
= EXPR_LOCATION (exp
);
4323 if (call_expr_nargs (exp
) < 2)
4325 error_at (loc
, "too few arguments to function %<va_start%>");
4329 if (fold_builtin_next_arg (exp
, true))
4332 nextarg
= expand_builtin_next_arg ();
4333 valist
= stabilize_va_list_loc (loc
, CALL_EXPR_ARG (exp
, 0), 1);
4335 if (targetm
.expand_builtin_va_start
)
4336 targetm
.expand_builtin_va_start (valist
, nextarg
);
4338 std_expand_builtin_va_start (valist
, nextarg
);
4343 /* Expand EXP, a call to __builtin_va_end. */
4346 expand_builtin_va_end (tree exp
)
4348 tree valist
= CALL_EXPR_ARG (exp
, 0);
4350 /* Evaluate for side effects, if needed. I hate macros that don't
4352 if (TREE_SIDE_EFFECTS (valist
))
4353 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4358 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4359 builtin rather than just as an assignment in stdarg.h because of the
4360 nastiness of array-type va_list types. */
4363 expand_builtin_va_copy (tree exp
)
4366 location_t loc
= EXPR_LOCATION (exp
);
4368 dst
= CALL_EXPR_ARG (exp
, 0);
4369 src
= CALL_EXPR_ARG (exp
, 1);
4371 dst
= stabilize_va_list_loc (loc
, dst
, 1);
4372 src
= stabilize_va_list_loc (loc
, src
, 0);
4374 gcc_assert (cfun
!= NULL
&& cfun
->decl
!= NULL_TREE
);
4376 if (TREE_CODE (targetm
.fn_abi_va_list (cfun
->decl
)) != ARRAY_TYPE
)
4378 t
= build2 (MODIFY_EXPR
, targetm
.fn_abi_va_list (cfun
->decl
), dst
, src
);
4379 TREE_SIDE_EFFECTS (t
) = 1;
4380 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4384 rtx dstb
, srcb
, size
;
4386 /* Evaluate to pointers. */
4387 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4388 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4389 size
= expand_expr (TYPE_SIZE_UNIT (targetm
.fn_abi_va_list (cfun
->decl
)),
4390 NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
4392 dstb
= convert_memory_address (Pmode
, dstb
);
4393 srcb
= convert_memory_address (Pmode
, srcb
);
4395 /* "Dereference" to BLKmode memories. */
4396 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
4397 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
4398 set_mem_align (dstb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4399 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
4400 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
4401 set_mem_align (srcb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4404 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
4410 /* Expand a call to one of the builtin functions __builtin_frame_address or
4411 __builtin_return_address. */
4414 expand_builtin_frame_address (tree fndecl
, tree exp
)
4416 /* The argument must be a nonnegative integer constant.
4417 It counts the number of frames to scan up the stack.
4418 The value is the return address saved in that frame. */
4419 if (call_expr_nargs (exp
) == 0)
4420 /* Warning about missing arg was already issued. */
4422 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp
, 0)))
4424 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4425 error ("invalid argument to %<__builtin_frame_address%>");
4427 error ("invalid argument to %<__builtin_return_address%>");
4433 = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
),
4434 tree_to_uhwi (CALL_EXPR_ARG (exp
, 0)));
4436 /* Some ports cannot access arbitrary stack frames. */
4439 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4440 warning (0, "unsupported argument to %<__builtin_frame_address%>");
4442 warning (0, "unsupported argument to %<__builtin_return_address%>");
4446 /* For __builtin_frame_address, return what we've got. */
4447 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4451 && ! CONSTANT_P (tem
))
4452 tem
= copy_addr_to_reg (tem
);
4457 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4458 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4459 is the same as for allocate_dynamic_stack_space. */
4462 expand_builtin_alloca (tree exp
, bool cannot_accumulate
)
4468 bool alloca_with_align
= (DECL_FUNCTION_CODE (get_callee_fndecl (exp
))
4469 == BUILT_IN_ALLOCA_WITH_ALIGN
);
4472 = (alloca_with_align
4473 ? validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
4474 : validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
));
4479 /* Compute the argument. */
4480 op0
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4482 /* Compute the alignment. */
4483 align
= (alloca_with_align
4484 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp
, 1))
4485 : BIGGEST_ALIGNMENT
);
4487 /* Allocate the desired space. */
4488 result
= allocate_dynamic_stack_space (op0
, 0, align
, cannot_accumulate
);
4489 result
= convert_memory_address (ptr_mode
, result
);
4494 /* Expand a call to bswap builtin in EXP.
4495 Return NULL_RTX if a normal call should be emitted rather than expanding the
4496 function in-line. If convenient, the result should be placed in TARGET.
4497 SUBTARGET may be used as the target for computing one of EXP's operands. */
4500 expand_builtin_bswap (enum machine_mode target_mode
, tree exp
, rtx target
,
4506 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4509 arg
= CALL_EXPR_ARG (exp
, 0);
4510 op0
= expand_expr (arg
,
4511 subtarget
&& GET_MODE (subtarget
) == target_mode
4512 ? subtarget
: NULL_RTX
,
4513 target_mode
, EXPAND_NORMAL
);
4514 if (GET_MODE (op0
) != target_mode
)
4515 op0
= convert_to_mode (target_mode
, op0
, 1);
4517 target
= expand_unop (target_mode
, bswap_optab
, op0
, target
, 1);
4519 gcc_assert (target
);
4521 return convert_to_mode (target_mode
, target
, 1);
4524 /* Expand a call to a unary builtin in EXP.
4525 Return NULL_RTX if a normal call should be emitted rather than expanding the
4526 function in-line. If convenient, the result should be placed in TARGET.
4527 SUBTARGET may be used as the target for computing one of EXP's operands. */
4530 expand_builtin_unop (enum machine_mode target_mode
, tree exp
, rtx target
,
4531 rtx subtarget
, optab op_optab
)
4535 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4538 /* Compute the argument. */
4539 op0
= expand_expr (CALL_EXPR_ARG (exp
, 0),
4541 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0)))
4542 == GET_MODE (subtarget
))) ? subtarget
: NULL_RTX
,
4543 VOIDmode
, EXPAND_NORMAL
);
4544 /* Compute op, into TARGET if possible.
4545 Set TARGET to wherever the result comes back. */
4546 target
= expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))),
4547 op_optab
, op0
, target
, op_optab
!= clrsb_optab
);
4548 gcc_assert (target
);
4550 return convert_to_mode (target_mode
, target
, 0);
4553 /* Expand a call to __builtin_expect. We just return our argument
4554 as the builtin_expect semantic should've been already executed by
4555 tree branch prediction pass. */
4558 expand_builtin_expect (tree exp
, rtx target
)
4562 if (call_expr_nargs (exp
) < 2)
4564 arg
= CALL_EXPR_ARG (exp
, 0);
4566 target
= expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4567 /* When guessing was done, the hints should be already stripped away. */
4568 gcc_assert (!flag_guess_branch_prob
4569 || optimize
== 0 || seen_error ());
4573 /* Expand a call to __builtin_assume_aligned. We just return our first
4574 argument as the builtin_assume_aligned semantic should've been already
4578 expand_builtin_assume_aligned (tree exp
, rtx target
)
4580 if (call_expr_nargs (exp
) < 2)
4582 target
= expand_expr (CALL_EXPR_ARG (exp
, 0), target
, VOIDmode
,
4584 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 1))
4585 && (call_expr_nargs (exp
) < 3
4586 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 2))));
4591 expand_builtin_trap (void)
4596 rtx insn
= emit_insn (gen_trap ());
4597 /* For trap insns when not accumulating outgoing args force
4598 REG_ARGS_SIZE note to prevent crossjumping of calls with
4599 different args sizes. */
4600 if (!ACCUMULATE_OUTGOING_ARGS
)
4601 add_reg_note (insn
, REG_ARGS_SIZE
, GEN_INT (stack_pointer_delta
));
4605 emit_library_call (abort_libfunc
, LCT_NORETURN
, VOIDmode
, 0);
4609 /* Expand a call to __builtin_unreachable. We do nothing except emit
4610 a barrier saying that control flow will not pass here.
4612 It is the responsibility of the program being compiled to ensure
4613 that control flow does never reach __builtin_unreachable. */
4615 expand_builtin_unreachable (void)
4620 /* Expand EXP, a call to fabs, fabsf or fabsl.
4621 Return NULL_RTX if a normal call should be emitted rather than expanding
4622 the function inline. If convenient, the result should be placed
4623 in TARGET. SUBTARGET may be used as the target for computing
4627 expand_builtin_fabs (tree exp
, rtx target
, rtx subtarget
)
4629 enum machine_mode mode
;
4633 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4636 arg
= CALL_EXPR_ARG (exp
, 0);
4637 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
4638 mode
= TYPE_MODE (TREE_TYPE (arg
));
4639 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4640 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
4643 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4644 Return NULL is a normal call should be emitted rather than expanding the
4645 function inline. If convenient, the result should be placed in TARGET.
4646 SUBTARGET may be used as the target for computing the operand. */
4649 expand_builtin_copysign (tree exp
, rtx target
, rtx subtarget
)
4654 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
4657 arg
= CALL_EXPR_ARG (exp
, 0);
4658 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4660 arg
= CALL_EXPR_ARG (exp
, 1);
4661 op1
= expand_normal (arg
);
4663 return expand_copysign (op0
, op1
, target
);
4666 /* Expand a call to __builtin___clear_cache. */
4669 expand_builtin___clear_cache (tree exp ATTRIBUTE_UNUSED
)
4671 #ifndef HAVE_clear_cache
4672 #ifdef CLEAR_INSN_CACHE
4673 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4674 does something. Just do the default expansion to a call to
4678 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4679 does nothing. There is no need to call it. Do nothing. */
4681 #endif /* CLEAR_INSN_CACHE */
4683 /* We have a "clear_cache" insn, and it will handle everything. */
4685 rtx begin_rtx
, end_rtx
;
4687 /* We must not expand to a library call. If we did, any
4688 fallback library function in libgcc that might contain a call to
4689 __builtin___clear_cache() would recurse infinitely. */
4690 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4692 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4696 if (HAVE_clear_cache
)
4698 struct expand_operand ops
[2];
4700 begin
= CALL_EXPR_ARG (exp
, 0);
4701 begin_rtx
= expand_expr (begin
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4703 end
= CALL_EXPR_ARG (exp
, 1);
4704 end_rtx
= expand_expr (end
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4706 create_address_operand (&ops
[0], begin_rtx
);
4707 create_address_operand (&ops
[1], end_rtx
);
4708 if (maybe_expand_insn (CODE_FOR_clear_cache
, 2, ops
))
4712 #endif /* HAVE_clear_cache */
4715 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4718 round_trampoline_addr (rtx tramp
)
4720 rtx temp
, addend
, mask
;
4722 /* If we don't need too much alignment, we'll have been guaranteed
4723 proper alignment by get_trampoline_type. */
4724 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
4727 /* Round address up to desired boundary. */
4728 temp
= gen_reg_rtx (Pmode
);
4729 addend
= gen_int_mode (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1, Pmode
);
4730 mask
= gen_int_mode (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
, Pmode
);
4732 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
4733 temp
, 0, OPTAB_LIB_WIDEN
);
4734 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
4735 temp
, 0, OPTAB_LIB_WIDEN
);
4741 expand_builtin_init_trampoline (tree exp
, bool onstack
)
4743 tree t_tramp
, t_func
, t_chain
;
4744 rtx m_tramp
, r_tramp
, r_chain
, tmp
;
4746 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
,
4747 POINTER_TYPE
, VOID_TYPE
))
4750 t_tramp
= CALL_EXPR_ARG (exp
, 0);
4751 t_func
= CALL_EXPR_ARG (exp
, 1);
4752 t_chain
= CALL_EXPR_ARG (exp
, 2);
4754 r_tramp
= expand_normal (t_tramp
);
4755 m_tramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
4756 MEM_NOTRAP_P (m_tramp
) = 1;
4758 /* If ONSTACK, the TRAMP argument should be the address of a field
4759 within the local function's FRAME decl. Either way, let's see if
4760 we can fill in the MEM_ATTRs for this memory. */
4761 if (TREE_CODE (t_tramp
) == ADDR_EXPR
)
4762 set_mem_attributes (m_tramp
, TREE_OPERAND (t_tramp
, 0), true);
4764 /* Creator of a heap trampoline is responsible for making sure the
4765 address is aligned to at least STACK_BOUNDARY. Normally malloc
4766 will ensure this anyhow. */
4767 tmp
= round_trampoline_addr (r_tramp
);
4770 m_tramp
= change_address (m_tramp
, BLKmode
, tmp
);
4771 set_mem_align (m_tramp
, TRAMPOLINE_ALIGNMENT
);
4772 set_mem_size (m_tramp
, TRAMPOLINE_SIZE
);
4775 /* The FUNC argument should be the address of the nested function.
4776 Extract the actual function decl to pass to the hook. */
4777 gcc_assert (TREE_CODE (t_func
) == ADDR_EXPR
);
4778 t_func
= TREE_OPERAND (t_func
, 0);
4779 gcc_assert (TREE_CODE (t_func
) == FUNCTION_DECL
);
4781 r_chain
= expand_normal (t_chain
);
4783 /* Generate insns to initialize the trampoline. */
4784 targetm
.calls
.trampoline_init (m_tramp
, t_func
, r_chain
);
4788 trampolines_created
= 1;
4790 warning_at (DECL_SOURCE_LOCATION (t_func
), OPT_Wtrampolines
,
4791 "trampoline generated for nested function %qD", t_func
);
4798 expand_builtin_adjust_trampoline (tree exp
)
4802 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
4805 tramp
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4806 tramp
= round_trampoline_addr (tramp
);
4807 if (targetm
.calls
.trampoline_adjust_address
)
4808 tramp
= targetm
.calls
.trampoline_adjust_address (tramp
);
4813 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4814 function. The function first checks whether the back end provides
4815 an insn to implement signbit for the respective mode. If not, it
4816 checks whether the floating point format of the value is such that
4817 the sign bit can be extracted. If that is not the case, the
4818 function returns NULL_RTX to indicate that a normal call should be
4819 emitted rather than expanding the function in-line. EXP is the
4820 expression that is a call to the builtin function; if convenient,
4821 the result should be placed in TARGET. */
4823 expand_builtin_signbit (tree exp
, rtx target
)
4825 const struct real_format
*fmt
;
4826 enum machine_mode fmode
, imode
, rmode
;
4829 enum insn_code icode
;
4831 location_t loc
= EXPR_LOCATION (exp
);
4833 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4836 arg
= CALL_EXPR_ARG (exp
, 0);
4837 fmode
= TYPE_MODE (TREE_TYPE (arg
));
4838 rmode
= TYPE_MODE (TREE_TYPE (exp
));
4839 fmt
= REAL_MODE_FORMAT (fmode
);
4841 arg
= builtin_save_expr (arg
);
4843 /* Expand the argument yielding a RTX expression. */
4844 temp
= expand_normal (arg
);
4846 /* Check if the back end provides an insn that handles signbit for the
4848 icode
= optab_handler (signbit_optab
, fmode
);
4849 if (icode
!= CODE_FOR_nothing
)
4851 rtx last
= get_last_insn ();
4852 target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
4853 if (maybe_emit_unop_insn (icode
, target
, temp
, UNKNOWN
))
4855 delete_insns_since (last
);
4858 /* For floating point formats without a sign bit, implement signbit
4860 bitpos
= fmt
->signbit_ro
;
4863 /* But we can't do this if the format supports signed zero. */
4864 if (fmt
->has_signed_zero
&& HONOR_SIGNED_ZEROS (fmode
))
4867 arg
= fold_build2_loc (loc
, LT_EXPR
, TREE_TYPE (exp
), arg
,
4868 build_real (TREE_TYPE (arg
), dconst0
));
4869 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4872 if (GET_MODE_SIZE (fmode
) <= UNITS_PER_WORD
)
4874 imode
= int_mode_for_mode (fmode
);
4875 if (imode
== BLKmode
)
4877 temp
= gen_lowpart (imode
, temp
);
4882 /* Handle targets with different FP word orders. */
4883 if (FLOAT_WORDS_BIG_ENDIAN
)
4884 word
= (GET_MODE_BITSIZE (fmode
) - bitpos
) / BITS_PER_WORD
;
4886 word
= bitpos
/ BITS_PER_WORD
;
4887 temp
= operand_subword_force (temp
, word
, fmode
);
4888 bitpos
= bitpos
% BITS_PER_WORD
;
4891 /* Force the intermediate word_mode (or narrower) result into a
4892 register. This avoids attempting to create paradoxical SUBREGs
4893 of floating point modes below. */
4894 temp
= force_reg (imode
, temp
);
4896 /* If the bitpos is within the "result mode" lowpart, the operation
4897 can be implement with a single bitwise AND. Otherwise, we need
4898 a right shift and an AND. */
4900 if (bitpos
< GET_MODE_BITSIZE (rmode
))
4902 wide_int mask
= wi::set_bit_in_zero (bitpos
, GET_MODE_PRECISION (rmode
));
4904 if (GET_MODE_SIZE (imode
) > GET_MODE_SIZE (rmode
))
4905 temp
= gen_lowpart (rmode
, temp
);
4906 temp
= expand_binop (rmode
, and_optab
, temp
,
4907 immed_wide_int_const (mask
, rmode
),
4908 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4912 /* Perform a logical right shift to place the signbit in the least
4913 significant bit, then truncate the result to the desired mode
4914 and mask just this bit. */
4915 temp
= expand_shift (RSHIFT_EXPR
, imode
, temp
, bitpos
, NULL_RTX
, 1);
4916 temp
= gen_lowpart (rmode
, temp
);
4917 temp
= expand_binop (rmode
, and_optab
, temp
, const1_rtx
,
4918 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4924 /* Expand fork or exec calls. TARGET is the desired target of the
4925 call. EXP is the call. FN is the
4926 identificator of the actual function. IGNORE is nonzero if the
4927 value is to be ignored. */
4930 expand_builtin_fork_or_exec (tree fn
, tree exp
, rtx target
, int ignore
)
4935 /* If we are not profiling, just call the function. */
4936 if (!profile_arc_flag
)
4939 /* Otherwise call the wrapper. This should be equivalent for the rest of
4940 compiler, so the code does not diverge, and the wrapper may run the
4941 code necessary for keeping the profiling sane. */
4943 switch (DECL_FUNCTION_CODE (fn
))
4946 id
= get_identifier ("__gcov_fork");
4949 case BUILT_IN_EXECL
:
4950 id
= get_identifier ("__gcov_execl");
4953 case BUILT_IN_EXECV
:
4954 id
= get_identifier ("__gcov_execv");
4957 case BUILT_IN_EXECLP
:
4958 id
= get_identifier ("__gcov_execlp");
4961 case BUILT_IN_EXECLE
:
4962 id
= get_identifier ("__gcov_execle");
4965 case BUILT_IN_EXECVP
:
4966 id
= get_identifier ("__gcov_execvp");
4969 case BUILT_IN_EXECVE
:
4970 id
= get_identifier ("__gcov_execve");
4977 decl
= build_decl (DECL_SOURCE_LOCATION (fn
),
4978 FUNCTION_DECL
, id
, TREE_TYPE (fn
));
4979 DECL_EXTERNAL (decl
) = 1;
4980 TREE_PUBLIC (decl
) = 1;
4981 DECL_ARTIFICIAL (decl
) = 1;
4982 TREE_NOTHROW (decl
) = 1;
4983 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
4984 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
4985 call
= rewrite_call_expr (EXPR_LOCATION (exp
), exp
, 0, decl
, 0);
4986 return expand_call (call
, target
, ignore
);
4991 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
4992 the pointer in these functions is void*, the tree optimizers may remove
4993 casts. The mode computed in expand_builtin isn't reliable either, due
4994 to __sync_bool_compare_and_swap.
4996 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
4997 group of builtins. This gives us log2 of the mode size. */
4999 static inline enum machine_mode
5000 get_builtin_sync_mode (int fcode_diff
)
5002 /* The size is not negotiable, so ask not to get BLKmode in return
5003 if the target indicates that a smaller size would be better. */
5004 return mode_for_size (BITS_PER_UNIT
<< fcode_diff
, MODE_INT
, 0);
5007 /* Expand the memory expression LOC and return the appropriate memory operand
5008 for the builtin_sync operations. */
5011 get_builtin_sync_mem (tree loc
, enum machine_mode mode
)
5015 addr
= expand_expr (loc
, NULL_RTX
, ptr_mode
, EXPAND_SUM
);
5016 addr
= convert_memory_address (Pmode
, addr
);
5018 /* Note that we explicitly do not want any alias information for this
5019 memory, so that we kill all other live memories. Otherwise we don't
5020 satisfy the full barrier semantics of the intrinsic. */
5021 mem
= validize_mem (gen_rtx_MEM (mode
, addr
));
5023 /* The alignment needs to be at least according to that of the mode. */
5024 set_mem_align (mem
, MAX (GET_MODE_ALIGNMENT (mode
),
5025 get_pointer_alignment (loc
)));
5026 set_mem_alias_set (mem
, ALIAS_SET_MEMORY_BARRIER
);
5027 MEM_VOLATILE_P (mem
) = 1;
5032 /* Make sure an argument is in the right mode.
5033 EXP is the tree argument.
5034 MODE is the mode it should be in. */
5037 expand_expr_force_mode (tree exp
, enum machine_mode mode
)
5040 enum machine_mode old_mode
;
5042 val
= expand_expr (exp
, NULL_RTX
, mode
, EXPAND_NORMAL
);
5043 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5044 of CONST_INTs, where we know the old_mode only from the call argument. */
5046 old_mode
= GET_MODE (val
);
5047 if (old_mode
== VOIDmode
)
5048 old_mode
= TYPE_MODE (TREE_TYPE (exp
));
5049 val
= convert_modes (mode
, old_mode
, val
, 1);
5054 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5055 EXP is the CALL_EXPR. CODE is the rtx code
5056 that corresponds to the arithmetic or logical operation from the name;
5057 an exception here is that NOT actually means NAND. TARGET is an optional
5058 place for us to store the results; AFTER is true if this is the
5059 fetch_and_xxx form. */
5062 expand_builtin_sync_operation (enum machine_mode mode
, tree exp
,
5063 enum rtx_code code
, bool after
,
5067 location_t loc
= EXPR_LOCATION (exp
);
5069 if (code
== NOT
&& warn_sync_nand
)
5071 tree fndecl
= get_callee_fndecl (exp
);
5072 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5074 static bool warned_f_a_n
, warned_n_a_f
;
5078 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
5079 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
5080 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
5081 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
5082 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
5086 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N
);
5087 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5088 warned_f_a_n
= true;
5091 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
5092 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
5093 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
5094 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
5095 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
5099 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N
);
5100 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5101 warned_n_a_f
= true;
5109 /* Expand the operands. */
5110 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5111 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5113 return expand_atomic_fetch_op (target
, mem
, val
, code
, MEMMODEL_SEQ_CST
,
5117 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5118 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5119 true if this is the boolean form. TARGET is a place for us to store the
5120 results; this is NOT optional if IS_BOOL is true. */
5123 expand_builtin_compare_and_swap (enum machine_mode mode
, tree exp
,
5124 bool is_bool
, rtx target
)
5126 rtx old_val
, new_val
, mem
;
5129 /* Expand the operands. */
5130 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5131 old_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5132 new_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5134 pbool
= poval
= NULL
;
5135 if (target
!= const0_rtx
)
5142 if (!expand_atomic_compare_and_swap (pbool
, poval
, mem
, old_val
, new_val
,
5143 false, MEMMODEL_SEQ_CST
,
5150 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5151 general form is actually an atomic exchange, and some targets only
5152 support a reduced form with the second argument being a constant 1.
5153 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5157 expand_builtin_sync_lock_test_and_set (enum machine_mode mode
, tree exp
,
5162 /* Expand the operands. */
5163 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5164 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5166 return expand_sync_lock_test_and_set (target
, mem
, val
);
5169 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5172 expand_builtin_sync_lock_release (enum machine_mode mode
, tree exp
)
5176 /* Expand the operands. */
5177 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5179 expand_atomic_store (mem
, const0_rtx
, MEMMODEL_RELEASE
, true);
5182 /* Given an integer representing an ``enum memmodel'', verify its
5183 correctness and return the memory model enum. */
5185 static enum memmodel
5186 get_memmodel (tree exp
)
5189 unsigned HOST_WIDE_INT val
;
5191 /* If the parameter is not a constant, it's a run time value so we'll just
5192 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5193 if (TREE_CODE (exp
) != INTEGER_CST
)
5194 return MEMMODEL_SEQ_CST
;
5196 op
= expand_normal (exp
);
5199 if (targetm
.memmodel_check
)
5200 val
= targetm
.memmodel_check (val
);
5201 else if (val
& ~MEMMODEL_MASK
)
5203 warning (OPT_Winvalid_memory_model
,
5204 "Unknown architecture specifier in memory model to builtin.");
5205 return MEMMODEL_SEQ_CST
;
5208 if ((INTVAL (op
) & MEMMODEL_MASK
) >= MEMMODEL_LAST
)
5210 warning (OPT_Winvalid_memory_model
,
5211 "invalid memory model argument to builtin");
5212 return MEMMODEL_SEQ_CST
;
5215 return (enum memmodel
) val
;
5218 /* Expand the __atomic_exchange intrinsic:
5219 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5220 EXP is the CALL_EXPR.
5221 TARGET is an optional place for us to store the results. */
5224 expand_builtin_atomic_exchange (enum machine_mode mode
, tree exp
, rtx target
)
5227 enum memmodel model
;
5229 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5230 if ((model
& MEMMODEL_MASK
) == MEMMODEL_CONSUME
)
5232 error ("invalid memory model for %<__atomic_exchange%>");
5236 if (!flag_inline_atomics
)
5239 /* Expand the operands. */
5240 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5241 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5243 return expand_atomic_exchange (target
, mem
, val
, model
);
5246 /* Expand the __atomic_compare_exchange intrinsic:
5247 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5248 TYPE desired, BOOL weak,
5249 enum memmodel success,
5250 enum memmodel failure)
5251 EXP is the CALL_EXPR.
5252 TARGET is an optional place for us to store the results. */
5255 expand_builtin_atomic_compare_exchange (enum machine_mode mode
, tree exp
,
5258 rtx expect
, desired
, mem
, oldval
, label
;
5259 enum memmodel success
, failure
;
5263 success
= get_memmodel (CALL_EXPR_ARG (exp
, 4));
5264 failure
= get_memmodel (CALL_EXPR_ARG (exp
, 5));
5266 if ((failure
& MEMMODEL_MASK
) == MEMMODEL_RELEASE
5267 || (failure
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5269 error ("invalid failure memory model for %<__atomic_compare_exchange%>");
5273 if (failure
> success
)
5275 error ("failure memory model cannot be stronger than success "
5276 "memory model for %<__atomic_compare_exchange%>");
5280 if (!flag_inline_atomics
)
5283 /* Expand the operands. */
5284 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5286 expect
= expand_normal (CALL_EXPR_ARG (exp
, 1));
5287 expect
= convert_memory_address (Pmode
, expect
);
5288 expect
= gen_rtx_MEM (mode
, expect
);
5289 desired
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5291 weak
= CALL_EXPR_ARG (exp
, 3);
5293 if (tree_fits_shwi_p (weak
) && tree_to_shwi (weak
) != 0)
5296 if (target
== const0_rtx
)
5299 /* Lest the rtl backend create a race condition with an imporoper store
5300 to memory, always create a new pseudo for OLDVAL. */
5303 if (!expand_atomic_compare_and_swap (&target
, &oldval
, mem
, expect
, desired
,
5304 is_weak
, success
, failure
))
5307 /* Conditionally store back to EXPECT, lest we create a race condition
5308 with an improper store to memory. */
5309 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5310 the normal case where EXPECT is totally private, i.e. a register. At
5311 which point the store can be unconditional. */
5312 label
= gen_label_rtx ();
5313 emit_cmp_and_jump_insns (target
, const0_rtx
, NE
, NULL
, VOIDmode
, 1, label
);
5314 emit_move_insn (expect
, oldval
);
5320 /* Expand the __atomic_load intrinsic:
5321 TYPE __atomic_load (TYPE *object, enum memmodel)
5322 EXP is the CALL_EXPR.
5323 TARGET is an optional place for us to store the results. */
5326 expand_builtin_atomic_load (enum machine_mode mode
, tree exp
, rtx target
)
5329 enum memmodel model
;
5331 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5332 if ((model
& MEMMODEL_MASK
) == MEMMODEL_RELEASE
5333 || (model
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5335 error ("invalid memory model for %<__atomic_load%>");
5339 if (!flag_inline_atomics
)
5342 /* Expand the operand. */
5343 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5345 return expand_atomic_load (target
, mem
, model
);
5349 /* Expand the __atomic_store intrinsic:
5350 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5351 EXP is the CALL_EXPR.
5352 TARGET is an optional place for us to store the results. */
5355 expand_builtin_atomic_store (enum machine_mode mode
, tree exp
)
5358 enum memmodel model
;
5360 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5361 if ((model
& MEMMODEL_MASK
) != MEMMODEL_RELAXED
5362 && (model
& MEMMODEL_MASK
) != MEMMODEL_SEQ_CST
5363 && (model
& MEMMODEL_MASK
) != MEMMODEL_RELEASE
)
5365 error ("invalid memory model for %<__atomic_store%>");
5369 if (!flag_inline_atomics
)
5372 /* Expand the operands. */
5373 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5374 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5376 return expand_atomic_store (mem
, val
, model
, false);
5379 /* Expand the __atomic_fetch_XXX intrinsic:
5380 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5381 EXP is the CALL_EXPR.
5382 TARGET is an optional place for us to store the results.
5383 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5384 FETCH_AFTER is true if returning the result of the operation.
5385 FETCH_AFTER is false if returning the value before the operation.
5386 IGNORE is true if the result is not used.
5387 EXT_CALL is the correct builtin for an external call if this cannot be
5388 resolved to an instruction sequence. */
5391 expand_builtin_atomic_fetch_op (enum machine_mode mode
, tree exp
, rtx target
,
5392 enum rtx_code code
, bool fetch_after
,
5393 bool ignore
, enum built_in_function ext_call
)
5396 enum memmodel model
;
5400 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5402 /* Expand the operands. */
5403 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5404 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5406 /* Only try generating instructions if inlining is turned on. */
5407 if (flag_inline_atomics
)
5409 ret
= expand_atomic_fetch_op (target
, mem
, val
, code
, model
, fetch_after
);
5414 /* Return if a different routine isn't needed for the library call. */
5415 if (ext_call
== BUILT_IN_NONE
)
5418 /* Change the call to the specified function. */
5419 fndecl
= get_callee_fndecl (exp
);
5420 addr
= CALL_EXPR_FN (exp
);
5423 gcc_assert (TREE_OPERAND (addr
, 0) == fndecl
);
5424 TREE_OPERAND (addr
, 0) = builtin_decl_explicit (ext_call
);
5426 /* Expand the call here so we can emit trailing code. */
5427 ret
= expand_call (exp
, target
, ignore
);
5429 /* Replace the original function just in case it matters. */
5430 TREE_OPERAND (addr
, 0) = fndecl
;
5432 /* Then issue the arithmetic correction to return the right result. */
5437 ret
= expand_simple_binop (mode
, AND
, ret
, val
, NULL_RTX
, true,
5439 ret
= expand_simple_unop (mode
, NOT
, ret
, target
, true);
5442 ret
= expand_simple_binop (mode
, code
, ret
, val
, target
, true,
5449 #ifndef HAVE_atomic_clear
5450 # define HAVE_atomic_clear 0
5451 # define gen_atomic_clear(x,y) (gcc_unreachable (), NULL_RTX)
5454 /* Expand an atomic clear operation.
5455 void _atomic_clear (BOOL *obj, enum memmodel)
5456 EXP is the call expression. */
5459 expand_builtin_atomic_clear (tree exp
)
5461 enum machine_mode mode
;
5463 enum memmodel model
;
5465 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5466 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5467 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5469 if ((model
& MEMMODEL_MASK
) == MEMMODEL_ACQUIRE
5470 || (model
& MEMMODEL_MASK
) == MEMMODEL_ACQ_REL
)
5472 error ("invalid memory model for %<__atomic_store%>");
5476 if (HAVE_atomic_clear
)
5478 emit_insn (gen_atomic_clear (mem
, model
));
5482 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5483 Failing that, a store is issued by __atomic_store. The only way this can
5484 fail is if the bool type is larger than a word size. Unlikely, but
5485 handle it anyway for completeness. Assume a single threaded model since
5486 there is no atomic support in this case, and no barriers are required. */
5487 ret
= expand_atomic_store (mem
, const0_rtx
, model
, true);
5489 emit_move_insn (mem
, const0_rtx
);
5493 /* Expand an atomic test_and_set operation.
5494 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5495 EXP is the call expression. */
5498 expand_builtin_atomic_test_and_set (tree exp
, rtx target
)
5501 enum memmodel model
;
5502 enum machine_mode mode
;
5504 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5505 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5506 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5508 return expand_atomic_test_and_set (target
, mem
, model
);
5512 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5513 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5516 fold_builtin_atomic_always_lock_free (tree arg0
, tree arg1
)
5519 enum machine_mode mode
;
5520 unsigned int mode_align
, type_align
;
5522 if (TREE_CODE (arg0
) != INTEGER_CST
)
5525 size
= INTVAL (expand_normal (arg0
)) * BITS_PER_UNIT
;
5526 mode
= mode_for_size (size
, MODE_INT
, 0);
5527 mode_align
= GET_MODE_ALIGNMENT (mode
);
5529 if (TREE_CODE (arg1
) == INTEGER_CST
&& INTVAL (expand_normal (arg1
)) == 0)
5530 type_align
= mode_align
;
5533 tree ttype
= TREE_TYPE (arg1
);
5535 /* This function is usually invoked and folded immediately by the front
5536 end before anything else has a chance to look at it. The pointer
5537 parameter at this point is usually cast to a void *, so check for that
5538 and look past the cast. */
5539 if (TREE_CODE (arg1
) == NOP_EXPR
&& POINTER_TYPE_P (ttype
)
5540 && VOID_TYPE_P (TREE_TYPE (ttype
)))
5541 arg1
= TREE_OPERAND (arg1
, 0);
5543 ttype
= TREE_TYPE (arg1
);
5544 gcc_assert (POINTER_TYPE_P (ttype
));
5546 /* Get the underlying type of the object. */
5547 ttype
= TREE_TYPE (ttype
);
5548 type_align
= TYPE_ALIGN (ttype
);
5551 /* If the object has smaller alignment, the the lock free routines cannot
5553 if (type_align
< mode_align
)
5554 return boolean_false_node
;
5556 /* Check if a compare_and_swap pattern exists for the mode which represents
5557 the required size. The pattern is not allowed to fail, so the existence
5558 of the pattern indicates support is present. */
5559 if (can_compare_and_swap_p (mode
, true))
5560 return boolean_true_node
;
5562 return boolean_false_node
;
5565 /* Return true if the parameters to call EXP represent an object which will
5566 always generate lock free instructions. The first argument represents the
5567 size of the object, and the second parameter is a pointer to the object
5568 itself. If NULL is passed for the object, then the result is based on
5569 typical alignment for an object of the specified size. Otherwise return
5573 expand_builtin_atomic_always_lock_free (tree exp
)
5576 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5577 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5579 if (TREE_CODE (arg0
) != INTEGER_CST
)
5581 error ("non-constant argument 1 to __atomic_always_lock_free");
5585 size
= fold_builtin_atomic_always_lock_free (arg0
, arg1
);
5586 if (size
== boolean_true_node
)
5591 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5592 is lock free on this architecture. */
5595 fold_builtin_atomic_is_lock_free (tree arg0
, tree arg1
)
5597 if (!flag_inline_atomics
)
5600 /* If it isn't always lock free, don't generate a result. */
5601 if (fold_builtin_atomic_always_lock_free (arg0
, arg1
) == boolean_true_node
)
5602 return boolean_true_node
;
5607 /* Return true if the parameters to call EXP represent an object which will
5608 always generate lock free instructions. The first argument represents the
5609 size of the object, and the second parameter is a pointer to the object
5610 itself. If NULL is passed for the object, then the result is based on
5611 typical alignment for an object of the specified size. Otherwise return
5615 expand_builtin_atomic_is_lock_free (tree exp
)
5618 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5619 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5621 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
5623 error ("non-integer argument 1 to __atomic_is_lock_free");
5627 if (!flag_inline_atomics
)
5630 /* If the value is known at compile time, return the RTX for it. */
5631 size
= fold_builtin_atomic_is_lock_free (arg0
, arg1
);
5632 if (size
== boolean_true_node
)
5638 /* Expand the __atomic_thread_fence intrinsic:
5639 void __atomic_thread_fence (enum memmodel)
5640 EXP is the CALL_EXPR. */
5643 expand_builtin_atomic_thread_fence (tree exp
)
5645 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5646 expand_mem_thread_fence (model
);
5649 /* Expand the __atomic_signal_fence intrinsic:
5650 void __atomic_signal_fence (enum memmodel)
5651 EXP is the CALL_EXPR. */
5654 expand_builtin_atomic_signal_fence (tree exp
)
5656 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5657 expand_mem_signal_fence (model
);
5660 /* Expand the __sync_synchronize intrinsic. */
5663 expand_builtin_sync_synchronize (void)
5665 expand_mem_thread_fence (MEMMODEL_SEQ_CST
);
5669 expand_builtin_thread_pointer (tree exp
, rtx target
)
5671 enum insn_code icode
;
5672 if (!validate_arglist (exp
, VOID_TYPE
))
5674 icode
= direct_optab_handler (get_thread_pointer_optab
, Pmode
);
5675 if (icode
!= CODE_FOR_nothing
)
5677 struct expand_operand op
;
5678 /* If the target is not sutitable then create a new target. */
5679 if (target
== NULL_RTX
5681 || GET_MODE (target
) != Pmode
)
5682 target
= gen_reg_rtx (Pmode
);
5683 create_output_operand (&op
, target
, Pmode
);
5684 expand_insn (icode
, 1, &op
);
5687 error ("__builtin_thread_pointer is not supported on this target");
5692 expand_builtin_set_thread_pointer (tree exp
)
5694 enum insn_code icode
;
5695 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
5697 icode
= direct_optab_handler (set_thread_pointer_optab
, Pmode
);
5698 if (icode
!= CODE_FOR_nothing
)
5700 struct expand_operand op
;
5701 rtx val
= expand_expr (CALL_EXPR_ARG (exp
, 0), NULL_RTX
,
5702 Pmode
, EXPAND_NORMAL
);
5703 create_input_operand (&op
, val
, Pmode
);
5704 expand_insn (icode
, 1, &op
);
5707 error ("__builtin_set_thread_pointer is not supported on this target");
5711 /* Emit code to restore the current value of stack. */
5714 expand_stack_restore (tree var
)
5716 rtx prev
, sa
= expand_normal (var
);
5718 sa
= convert_memory_address (Pmode
, sa
);
5720 prev
= get_last_insn ();
5721 emit_stack_restore (SAVE_BLOCK
, sa
);
5722 fixup_args_size_notes (prev
, get_last_insn (), 0);
5726 /* Emit code to save the current value of stack. */
5729 expand_stack_save (void)
5733 do_pending_stack_adjust ();
5734 emit_stack_save (SAVE_BLOCK
, &ret
);
5738 /* Expand an expression EXP that calls a built-in function,
5739 with result going to TARGET if that's convenient
5740 (and in mode MODE if that's convenient).
5741 SUBTARGET may be used as the target for computing one of EXP's operands.
5742 IGNORE is nonzero if the value is to be ignored. */
5745 expand_builtin (tree exp
, rtx target
, rtx subtarget
, enum machine_mode mode
,
5748 tree fndecl
= get_callee_fndecl (exp
);
5749 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5750 enum machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
5753 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5754 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
5756 /* When not optimizing, generate calls to library functions for a certain
5759 && !called_as_built_in (fndecl
)
5760 && fcode
!= BUILT_IN_FORK
5761 && fcode
!= BUILT_IN_EXECL
5762 && fcode
!= BUILT_IN_EXECV
5763 && fcode
!= BUILT_IN_EXECLP
5764 && fcode
!= BUILT_IN_EXECLE
5765 && fcode
!= BUILT_IN_EXECVP
5766 && fcode
!= BUILT_IN_EXECVE
5767 && fcode
!= BUILT_IN_ALLOCA
5768 && fcode
!= BUILT_IN_ALLOCA_WITH_ALIGN
5769 && fcode
!= BUILT_IN_FREE
)
5770 return expand_call (exp
, target
, ignore
);
5772 /* The built-in function expanders test for target == const0_rtx
5773 to determine whether the function's result will be ignored. */
5775 target
= const0_rtx
;
5777 /* If the result of a pure or const built-in function is ignored, and
5778 none of its arguments are volatile, we can avoid expanding the
5779 built-in call and just evaluate the arguments for side-effects. */
5780 if (target
== const0_rtx
5781 && ((flags
= flags_from_decl_or_type (fndecl
)) & (ECF_CONST
| ECF_PURE
))
5782 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
5784 bool volatilep
= false;
5786 call_expr_arg_iterator iter
;
5788 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5789 if (TREE_THIS_VOLATILE (arg
))
5797 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5798 expand_expr (arg
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5805 CASE_FLT_FN (BUILT_IN_FABS
):
5806 case BUILT_IN_FABSD32
:
5807 case BUILT_IN_FABSD64
:
5808 case BUILT_IN_FABSD128
:
5809 target
= expand_builtin_fabs (exp
, target
, subtarget
);
5814 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
5815 target
= expand_builtin_copysign (exp
, target
, subtarget
);
5820 /* Just do a normal library call if we were unable to fold
5822 CASE_FLT_FN (BUILT_IN_CABS
):
5825 CASE_FLT_FN (BUILT_IN_EXP
):
5826 CASE_FLT_FN (BUILT_IN_EXP10
):
5827 CASE_FLT_FN (BUILT_IN_POW10
):
5828 CASE_FLT_FN (BUILT_IN_EXP2
):
5829 CASE_FLT_FN (BUILT_IN_EXPM1
):
5830 CASE_FLT_FN (BUILT_IN_LOGB
):
5831 CASE_FLT_FN (BUILT_IN_LOG
):
5832 CASE_FLT_FN (BUILT_IN_LOG10
):
5833 CASE_FLT_FN (BUILT_IN_LOG2
):
5834 CASE_FLT_FN (BUILT_IN_LOG1P
):
5835 CASE_FLT_FN (BUILT_IN_TAN
):
5836 CASE_FLT_FN (BUILT_IN_ASIN
):
5837 CASE_FLT_FN (BUILT_IN_ACOS
):
5838 CASE_FLT_FN (BUILT_IN_ATAN
):
5839 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
5840 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5841 because of possible accuracy problems. */
5842 if (! flag_unsafe_math_optimizations
)
5844 CASE_FLT_FN (BUILT_IN_SQRT
):
5845 CASE_FLT_FN (BUILT_IN_FLOOR
):
5846 CASE_FLT_FN (BUILT_IN_CEIL
):
5847 CASE_FLT_FN (BUILT_IN_TRUNC
):
5848 CASE_FLT_FN (BUILT_IN_ROUND
):
5849 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
5850 CASE_FLT_FN (BUILT_IN_RINT
):
5851 target
= expand_builtin_mathfn (exp
, target
, subtarget
);
5856 CASE_FLT_FN (BUILT_IN_FMA
):
5857 target
= expand_builtin_mathfn_ternary (exp
, target
, subtarget
);
5862 CASE_FLT_FN (BUILT_IN_ILOGB
):
5863 if (! flag_unsafe_math_optimizations
)
5865 CASE_FLT_FN (BUILT_IN_ISINF
):
5866 CASE_FLT_FN (BUILT_IN_FINITE
):
5867 case BUILT_IN_ISFINITE
:
5868 case BUILT_IN_ISNORMAL
:
5869 target
= expand_builtin_interclass_mathfn (exp
, target
);
5874 CASE_FLT_FN (BUILT_IN_ICEIL
):
5875 CASE_FLT_FN (BUILT_IN_LCEIL
):
5876 CASE_FLT_FN (BUILT_IN_LLCEIL
):
5877 CASE_FLT_FN (BUILT_IN_LFLOOR
):
5878 CASE_FLT_FN (BUILT_IN_IFLOOR
):
5879 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
5880 target
= expand_builtin_int_roundingfn (exp
, target
);
5885 CASE_FLT_FN (BUILT_IN_IRINT
):
5886 CASE_FLT_FN (BUILT_IN_LRINT
):
5887 CASE_FLT_FN (BUILT_IN_LLRINT
):
5888 CASE_FLT_FN (BUILT_IN_IROUND
):
5889 CASE_FLT_FN (BUILT_IN_LROUND
):
5890 CASE_FLT_FN (BUILT_IN_LLROUND
):
5891 target
= expand_builtin_int_roundingfn_2 (exp
, target
);
5896 CASE_FLT_FN (BUILT_IN_POWI
):
5897 target
= expand_builtin_powi (exp
, target
);
5902 CASE_FLT_FN (BUILT_IN_ATAN2
):
5903 CASE_FLT_FN (BUILT_IN_LDEXP
):
5904 CASE_FLT_FN (BUILT_IN_SCALB
):
5905 CASE_FLT_FN (BUILT_IN_SCALBN
):
5906 CASE_FLT_FN (BUILT_IN_SCALBLN
):
5907 if (! flag_unsafe_math_optimizations
)
5910 CASE_FLT_FN (BUILT_IN_FMOD
):
5911 CASE_FLT_FN (BUILT_IN_REMAINDER
):
5912 CASE_FLT_FN (BUILT_IN_DREM
):
5913 CASE_FLT_FN (BUILT_IN_POW
):
5914 target
= expand_builtin_mathfn_2 (exp
, target
, subtarget
);
5919 CASE_FLT_FN (BUILT_IN_CEXPI
):
5920 target
= expand_builtin_cexpi (exp
, target
);
5921 gcc_assert (target
);
5924 CASE_FLT_FN (BUILT_IN_SIN
):
5925 CASE_FLT_FN (BUILT_IN_COS
):
5926 if (! flag_unsafe_math_optimizations
)
5928 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
5933 CASE_FLT_FN (BUILT_IN_SINCOS
):
5934 if (! flag_unsafe_math_optimizations
)
5936 target
= expand_builtin_sincos (exp
);
5941 case BUILT_IN_APPLY_ARGS
:
5942 return expand_builtin_apply_args ();
5944 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
5945 FUNCTION with a copy of the parameters described by
5946 ARGUMENTS, and ARGSIZE. It returns a block of memory
5947 allocated on the stack into which is stored all the registers
5948 that might possibly be used for returning the result of a
5949 function. ARGUMENTS is the value returned by
5950 __builtin_apply_args. ARGSIZE is the number of bytes of
5951 arguments that must be copied. ??? How should this value be
5952 computed? We'll also need a safe worst case value for varargs
5954 case BUILT_IN_APPLY
:
5955 if (!validate_arglist (exp
, POINTER_TYPE
,
5956 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
5957 && !validate_arglist (exp
, REFERENCE_TYPE
,
5958 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
5964 ops
[0] = expand_normal (CALL_EXPR_ARG (exp
, 0));
5965 ops
[1] = expand_normal (CALL_EXPR_ARG (exp
, 1));
5966 ops
[2] = expand_normal (CALL_EXPR_ARG (exp
, 2));
5968 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
5971 /* __builtin_return (RESULT) causes the function to return the
5972 value described by RESULT. RESULT is address of the block of
5973 memory returned by __builtin_apply. */
5974 case BUILT_IN_RETURN
:
5975 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
5976 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp
, 0)));
5979 case BUILT_IN_SAVEREGS
:
5980 return expand_builtin_saveregs ();
5982 case BUILT_IN_VA_ARG_PACK
:
5983 /* All valid uses of __builtin_va_arg_pack () are removed during
5985 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp
);
5988 case BUILT_IN_VA_ARG_PACK_LEN
:
5989 /* All valid uses of __builtin_va_arg_pack_len () are removed during
5991 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp
);
5994 /* Return the address of the first anonymous stack arg. */
5995 case BUILT_IN_NEXT_ARG
:
5996 if (fold_builtin_next_arg (exp
, false))
5998 return expand_builtin_next_arg ();
6000 case BUILT_IN_CLEAR_CACHE
:
6001 target
= expand_builtin___clear_cache (exp
);
6006 case BUILT_IN_CLASSIFY_TYPE
:
6007 return expand_builtin_classify_type (exp
);
6009 case BUILT_IN_CONSTANT_P
:
6012 case BUILT_IN_FRAME_ADDRESS
:
6013 case BUILT_IN_RETURN_ADDRESS
:
6014 return expand_builtin_frame_address (fndecl
, exp
);
6016 /* Returns the address of the area where the structure is returned.
6018 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
6019 if (call_expr_nargs (exp
) != 0
6020 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
6021 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
6024 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
6026 case BUILT_IN_ALLOCA
:
6027 case BUILT_IN_ALLOCA_WITH_ALIGN
:
6028 /* If the allocation stems from the declaration of a variable-sized
6029 object, it cannot accumulate. */
6030 target
= expand_builtin_alloca (exp
, CALL_ALLOCA_FOR_VAR_P (exp
));
6035 case BUILT_IN_STACK_SAVE
:
6036 return expand_stack_save ();
6038 case BUILT_IN_STACK_RESTORE
:
6039 expand_stack_restore (CALL_EXPR_ARG (exp
, 0));
6042 case BUILT_IN_BSWAP16
:
6043 case BUILT_IN_BSWAP32
:
6044 case BUILT_IN_BSWAP64
:
6045 target
= expand_builtin_bswap (target_mode
, exp
, target
, subtarget
);
6050 CASE_INT_FN (BUILT_IN_FFS
):
6051 target
= expand_builtin_unop (target_mode
, exp
, target
,
6052 subtarget
, ffs_optab
);
6057 CASE_INT_FN (BUILT_IN_CLZ
):
6058 target
= expand_builtin_unop (target_mode
, exp
, target
,
6059 subtarget
, clz_optab
);
6064 CASE_INT_FN (BUILT_IN_CTZ
):
6065 target
= expand_builtin_unop (target_mode
, exp
, target
,
6066 subtarget
, ctz_optab
);
6071 CASE_INT_FN (BUILT_IN_CLRSB
):
6072 target
= expand_builtin_unop (target_mode
, exp
, target
,
6073 subtarget
, clrsb_optab
);
6078 CASE_INT_FN (BUILT_IN_POPCOUNT
):
6079 target
= expand_builtin_unop (target_mode
, exp
, target
,
6080 subtarget
, popcount_optab
);
6085 CASE_INT_FN (BUILT_IN_PARITY
):
6086 target
= expand_builtin_unop (target_mode
, exp
, target
,
6087 subtarget
, parity_optab
);
6092 case BUILT_IN_STRLEN
:
6093 target
= expand_builtin_strlen (exp
, target
, target_mode
);
6098 case BUILT_IN_STRCPY
:
6099 target
= expand_builtin_strcpy (exp
, target
);
6104 case BUILT_IN_STRNCPY
:
6105 target
= expand_builtin_strncpy (exp
, target
);
6110 case BUILT_IN_STPCPY
:
6111 target
= expand_builtin_stpcpy (exp
, target
, mode
);
6116 case BUILT_IN_MEMCPY
:
6117 target
= expand_builtin_memcpy (exp
, target
);
6122 case BUILT_IN_MEMPCPY
:
6123 target
= expand_builtin_mempcpy (exp
, target
, mode
);
6128 case BUILT_IN_MEMSET
:
6129 target
= expand_builtin_memset (exp
, target
, mode
);
6134 case BUILT_IN_BZERO
:
6135 target
= expand_builtin_bzero (exp
);
6140 case BUILT_IN_STRCMP
:
6141 target
= expand_builtin_strcmp (exp
, target
);
6146 case BUILT_IN_STRNCMP
:
6147 target
= expand_builtin_strncmp (exp
, target
, mode
);
6153 case BUILT_IN_MEMCMP
:
6154 target
= expand_builtin_memcmp (exp
, target
, mode
);
6159 case BUILT_IN_SETJMP
:
6160 /* This should have been lowered to the builtins below. */
6163 case BUILT_IN_SETJMP_SETUP
:
6164 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6165 and the receiver label. */
6166 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
6168 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6169 VOIDmode
, EXPAND_NORMAL
);
6170 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 1), 0);
6171 rtx label_r
= label_rtx (label
);
6173 /* This is copied from the handling of non-local gotos. */
6174 expand_builtin_setjmp_setup (buf_addr
, label_r
);
6175 nonlocal_goto_handler_labels
6176 = gen_rtx_EXPR_LIST (VOIDmode
, label_r
,
6177 nonlocal_goto_handler_labels
);
6178 /* ??? Do not let expand_label treat us as such since we would
6179 not want to be both on the list of non-local labels and on
6180 the list of forced labels. */
6181 FORCED_LABEL (label
) = 0;
6186 case BUILT_IN_SETJMP_RECEIVER
:
6187 /* __builtin_setjmp_receiver is passed the receiver label. */
6188 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6190 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 0), 0);
6191 rtx label_r
= label_rtx (label
);
6193 expand_builtin_setjmp_receiver (label_r
);
6198 /* __builtin_longjmp is passed a pointer to an array of five words.
6199 It's similar to the C library longjmp function but works with
6200 __builtin_setjmp above. */
6201 case BUILT_IN_LONGJMP
:
6202 if (validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6204 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6205 VOIDmode
, EXPAND_NORMAL
);
6206 rtx value
= expand_normal (CALL_EXPR_ARG (exp
, 1));
6208 if (value
!= const1_rtx
)
6210 error ("%<__builtin_longjmp%> second argument must be 1");
6214 expand_builtin_longjmp (buf_addr
, value
);
6219 case BUILT_IN_NONLOCAL_GOTO
:
6220 target
= expand_builtin_nonlocal_goto (exp
);
6225 /* This updates the setjmp buffer that is its argument with the value
6226 of the current stack pointer. */
6227 case BUILT_IN_UPDATE_SETJMP_BUF
:
6228 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6231 = expand_normal (CALL_EXPR_ARG (exp
, 0));
6233 expand_builtin_update_setjmp_buf (buf_addr
);
6239 expand_builtin_trap ();
6242 case BUILT_IN_UNREACHABLE
:
6243 expand_builtin_unreachable ();
6246 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
6247 case BUILT_IN_SIGNBITD32
:
6248 case BUILT_IN_SIGNBITD64
:
6249 case BUILT_IN_SIGNBITD128
:
6250 target
= expand_builtin_signbit (exp
, target
);
6255 /* Various hooks for the DWARF 2 __throw routine. */
6256 case BUILT_IN_UNWIND_INIT
:
6257 expand_builtin_unwind_init ();
6259 case BUILT_IN_DWARF_CFA
:
6260 return virtual_cfa_rtx
;
6261 #ifdef DWARF2_UNWIND_INFO
6262 case BUILT_IN_DWARF_SP_COLUMN
:
6263 return expand_builtin_dwarf_sp_column ();
6264 case BUILT_IN_INIT_DWARF_REG_SIZES
:
6265 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp
, 0));
6268 case BUILT_IN_FROB_RETURN_ADDR
:
6269 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp
, 0));
6270 case BUILT_IN_EXTRACT_RETURN_ADDR
:
6271 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp
, 0));
6272 case BUILT_IN_EH_RETURN
:
6273 expand_builtin_eh_return (CALL_EXPR_ARG (exp
, 0),
6274 CALL_EXPR_ARG (exp
, 1));
6276 #ifdef EH_RETURN_DATA_REGNO
6277 case BUILT_IN_EH_RETURN_DATA_REGNO
:
6278 return expand_builtin_eh_return_data_regno (exp
);
6280 case BUILT_IN_EXTEND_POINTER
:
6281 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp
, 0));
6282 case BUILT_IN_EH_POINTER
:
6283 return expand_builtin_eh_pointer (exp
);
6284 case BUILT_IN_EH_FILTER
:
6285 return expand_builtin_eh_filter (exp
);
6286 case BUILT_IN_EH_COPY_VALUES
:
6287 return expand_builtin_eh_copy_values (exp
);
6289 case BUILT_IN_VA_START
:
6290 return expand_builtin_va_start (exp
);
6291 case BUILT_IN_VA_END
:
6292 return expand_builtin_va_end (exp
);
6293 case BUILT_IN_VA_COPY
:
6294 return expand_builtin_va_copy (exp
);
6295 case BUILT_IN_EXPECT
:
6296 return expand_builtin_expect (exp
, target
);
6297 case BUILT_IN_ASSUME_ALIGNED
:
6298 return expand_builtin_assume_aligned (exp
, target
);
6299 case BUILT_IN_PREFETCH
:
6300 expand_builtin_prefetch (exp
);
6303 case BUILT_IN_INIT_TRAMPOLINE
:
6304 return expand_builtin_init_trampoline (exp
, true);
6305 case BUILT_IN_INIT_HEAP_TRAMPOLINE
:
6306 return expand_builtin_init_trampoline (exp
, false);
6307 case BUILT_IN_ADJUST_TRAMPOLINE
:
6308 return expand_builtin_adjust_trampoline (exp
);
6311 case BUILT_IN_EXECL
:
6312 case BUILT_IN_EXECV
:
6313 case BUILT_IN_EXECLP
:
6314 case BUILT_IN_EXECLE
:
6315 case BUILT_IN_EXECVP
:
6316 case BUILT_IN_EXECVE
:
6317 target
= expand_builtin_fork_or_exec (fndecl
, exp
, target
, ignore
);
6322 case BUILT_IN_SYNC_FETCH_AND_ADD_1
:
6323 case BUILT_IN_SYNC_FETCH_AND_ADD_2
:
6324 case BUILT_IN_SYNC_FETCH_AND_ADD_4
:
6325 case BUILT_IN_SYNC_FETCH_AND_ADD_8
:
6326 case BUILT_IN_SYNC_FETCH_AND_ADD_16
:
6327 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_ADD_1
);
6328 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, false, target
);
6333 case BUILT_IN_SYNC_FETCH_AND_SUB_1
:
6334 case BUILT_IN_SYNC_FETCH_AND_SUB_2
:
6335 case BUILT_IN_SYNC_FETCH_AND_SUB_4
:
6336 case BUILT_IN_SYNC_FETCH_AND_SUB_8
:
6337 case BUILT_IN_SYNC_FETCH_AND_SUB_16
:
6338 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_SUB_1
);
6339 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, false, target
);
6344 case BUILT_IN_SYNC_FETCH_AND_OR_1
:
6345 case BUILT_IN_SYNC_FETCH_AND_OR_2
:
6346 case BUILT_IN_SYNC_FETCH_AND_OR_4
:
6347 case BUILT_IN_SYNC_FETCH_AND_OR_8
:
6348 case BUILT_IN_SYNC_FETCH_AND_OR_16
:
6349 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_OR_1
);
6350 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, false, target
);
6355 case BUILT_IN_SYNC_FETCH_AND_AND_1
:
6356 case BUILT_IN_SYNC_FETCH_AND_AND_2
:
6357 case BUILT_IN_SYNC_FETCH_AND_AND_4
:
6358 case BUILT_IN_SYNC_FETCH_AND_AND_8
:
6359 case BUILT_IN_SYNC_FETCH_AND_AND_16
:
6360 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_AND_1
);
6361 target
= expand_builtin_sync_operation (mode
, exp
, AND
, false, target
);
6366 case BUILT_IN_SYNC_FETCH_AND_XOR_1
:
6367 case BUILT_IN_SYNC_FETCH_AND_XOR_2
:
6368 case BUILT_IN_SYNC_FETCH_AND_XOR_4
:
6369 case BUILT_IN_SYNC_FETCH_AND_XOR_8
:
6370 case BUILT_IN_SYNC_FETCH_AND_XOR_16
:
6371 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_XOR_1
);
6372 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, false, target
);
6377 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
6378 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
6379 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
6380 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
6381 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
6382 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_NAND_1
);
6383 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, false, target
);
6388 case BUILT_IN_SYNC_ADD_AND_FETCH_1
:
6389 case BUILT_IN_SYNC_ADD_AND_FETCH_2
:
6390 case BUILT_IN_SYNC_ADD_AND_FETCH_4
:
6391 case BUILT_IN_SYNC_ADD_AND_FETCH_8
:
6392 case BUILT_IN_SYNC_ADD_AND_FETCH_16
:
6393 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_ADD_AND_FETCH_1
);
6394 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, true, target
);
6399 case BUILT_IN_SYNC_SUB_AND_FETCH_1
:
6400 case BUILT_IN_SYNC_SUB_AND_FETCH_2
:
6401 case BUILT_IN_SYNC_SUB_AND_FETCH_4
:
6402 case BUILT_IN_SYNC_SUB_AND_FETCH_8
:
6403 case BUILT_IN_SYNC_SUB_AND_FETCH_16
:
6404 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_SUB_AND_FETCH_1
);
6405 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, true, target
);
6410 case BUILT_IN_SYNC_OR_AND_FETCH_1
:
6411 case BUILT_IN_SYNC_OR_AND_FETCH_2
:
6412 case BUILT_IN_SYNC_OR_AND_FETCH_4
:
6413 case BUILT_IN_SYNC_OR_AND_FETCH_8
:
6414 case BUILT_IN_SYNC_OR_AND_FETCH_16
:
6415 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_OR_AND_FETCH_1
);
6416 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, true, target
);
6421 case BUILT_IN_SYNC_AND_AND_FETCH_1
:
6422 case BUILT_IN_SYNC_AND_AND_FETCH_2
:
6423 case BUILT_IN_SYNC_AND_AND_FETCH_4
:
6424 case BUILT_IN_SYNC_AND_AND_FETCH_8
:
6425 case BUILT_IN_SYNC_AND_AND_FETCH_16
:
6426 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_AND_AND_FETCH_1
);
6427 target
= expand_builtin_sync_operation (mode
, exp
, AND
, true, target
);
6432 case BUILT_IN_SYNC_XOR_AND_FETCH_1
:
6433 case BUILT_IN_SYNC_XOR_AND_FETCH_2
:
6434 case BUILT_IN_SYNC_XOR_AND_FETCH_4
:
6435 case BUILT_IN_SYNC_XOR_AND_FETCH_8
:
6436 case BUILT_IN_SYNC_XOR_AND_FETCH_16
:
6437 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_XOR_AND_FETCH_1
);
6438 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, true, target
);
6443 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
6444 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
6445 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
6446 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
6447 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
6448 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_NAND_AND_FETCH_1
);
6449 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, true, target
);
6454 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
6455 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
6456 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
6457 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
6458 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
6459 if (mode
== VOIDmode
)
6460 mode
= TYPE_MODE (boolean_type_node
);
6461 if (!target
|| !register_operand (target
, mode
))
6462 target
= gen_reg_rtx (mode
);
6464 mode
= get_builtin_sync_mode
6465 (fcode
- BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
);
6466 target
= expand_builtin_compare_and_swap (mode
, exp
, true, target
);
6471 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
:
6472 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2
:
6473 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4
:
6474 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8
:
6475 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16
:
6476 mode
= get_builtin_sync_mode
6477 (fcode
- BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
);
6478 target
= expand_builtin_compare_and_swap (mode
, exp
, false, target
);
6483 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
:
6484 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2
:
6485 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4
:
6486 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8
:
6487 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16
:
6488 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
);
6489 target
= expand_builtin_sync_lock_test_and_set (mode
, exp
, target
);
6494 case BUILT_IN_SYNC_LOCK_RELEASE_1
:
6495 case BUILT_IN_SYNC_LOCK_RELEASE_2
:
6496 case BUILT_IN_SYNC_LOCK_RELEASE_4
:
6497 case BUILT_IN_SYNC_LOCK_RELEASE_8
:
6498 case BUILT_IN_SYNC_LOCK_RELEASE_16
:
6499 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_RELEASE_1
);
6500 expand_builtin_sync_lock_release (mode
, exp
);
6503 case BUILT_IN_SYNC_SYNCHRONIZE
:
6504 expand_builtin_sync_synchronize ();
6507 case BUILT_IN_ATOMIC_EXCHANGE_1
:
6508 case BUILT_IN_ATOMIC_EXCHANGE_2
:
6509 case BUILT_IN_ATOMIC_EXCHANGE_4
:
6510 case BUILT_IN_ATOMIC_EXCHANGE_8
:
6511 case BUILT_IN_ATOMIC_EXCHANGE_16
:
6512 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_EXCHANGE_1
);
6513 target
= expand_builtin_atomic_exchange (mode
, exp
, target
);
6518 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
6519 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
6520 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
6521 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
6522 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
6524 unsigned int nargs
, z
;
6525 vec
<tree
, va_gc
> *vec
;
6528 get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
);
6529 target
= expand_builtin_atomic_compare_exchange (mode
, exp
, target
);
6533 /* If this is turned into an external library call, the weak parameter
6534 must be dropped to match the expected parameter list. */
6535 nargs
= call_expr_nargs (exp
);
6536 vec_alloc (vec
, nargs
- 1);
6537 for (z
= 0; z
< 3; z
++)
6538 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
6539 /* Skip the boolean weak parameter. */
6540 for (z
= 4; z
< 6; z
++)
6541 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
6542 exp
= build_call_vec (TREE_TYPE (exp
), CALL_EXPR_FN (exp
), vec
);
6546 case BUILT_IN_ATOMIC_LOAD_1
:
6547 case BUILT_IN_ATOMIC_LOAD_2
:
6548 case BUILT_IN_ATOMIC_LOAD_4
:
6549 case BUILT_IN_ATOMIC_LOAD_8
:
6550 case BUILT_IN_ATOMIC_LOAD_16
:
6551 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_LOAD_1
);
6552 target
= expand_builtin_atomic_load (mode
, exp
, target
);
6557 case BUILT_IN_ATOMIC_STORE_1
:
6558 case BUILT_IN_ATOMIC_STORE_2
:
6559 case BUILT_IN_ATOMIC_STORE_4
:
6560 case BUILT_IN_ATOMIC_STORE_8
:
6561 case BUILT_IN_ATOMIC_STORE_16
:
6562 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_STORE_1
);
6563 target
= expand_builtin_atomic_store (mode
, exp
);
6568 case BUILT_IN_ATOMIC_ADD_FETCH_1
:
6569 case BUILT_IN_ATOMIC_ADD_FETCH_2
:
6570 case BUILT_IN_ATOMIC_ADD_FETCH_4
:
6571 case BUILT_IN_ATOMIC_ADD_FETCH_8
:
6572 case BUILT_IN_ATOMIC_ADD_FETCH_16
:
6574 enum built_in_function lib
;
6575 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
);
6576 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_ADD_1
+
6577 (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
));
6578 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, true,
6584 case BUILT_IN_ATOMIC_SUB_FETCH_1
:
6585 case BUILT_IN_ATOMIC_SUB_FETCH_2
:
6586 case BUILT_IN_ATOMIC_SUB_FETCH_4
:
6587 case BUILT_IN_ATOMIC_SUB_FETCH_8
:
6588 case BUILT_IN_ATOMIC_SUB_FETCH_16
:
6590 enum built_in_function lib
;
6591 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
);
6592 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_SUB_1
+
6593 (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
));
6594 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, true,
6600 case BUILT_IN_ATOMIC_AND_FETCH_1
:
6601 case BUILT_IN_ATOMIC_AND_FETCH_2
:
6602 case BUILT_IN_ATOMIC_AND_FETCH_4
:
6603 case BUILT_IN_ATOMIC_AND_FETCH_8
:
6604 case BUILT_IN_ATOMIC_AND_FETCH_16
:
6606 enum built_in_function lib
;
6607 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
);
6608 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_AND_1
+
6609 (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
));
6610 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, true,
6616 case BUILT_IN_ATOMIC_NAND_FETCH_1
:
6617 case BUILT_IN_ATOMIC_NAND_FETCH_2
:
6618 case BUILT_IN_ATOMIC_NAND_FETCH_4
:
6619 case BUILT_IN_ATOMIC_NAND_FETCH_8
:
6620 case BUILT_IN_ATOMIC_NAND_FETCH_16
:
6622 enum built_in_function lib
;
6623 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
);
6624 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_NAND_1
+
6625 (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
));
6626 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, true,
6632 case BUILT_IN_ATOMIC_XOR_FETCH_1
:
6633 case BUILT_IN_ATOMIC_XOR_FETCH_2
:
6634 case BUILT_IN_ATOMIC_XOR_FETCH_4
:
6635 case BUILT_IN_ATOMIC_XOR_FETCH_8
:
6636 case BUILT_IN_ATOMIC_XOR_FETCH_16
:
6638 enum built_in_function lib
;
6639 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
);
6640 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_XOR_1
+
6641 (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
));
6642 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, true,
6648 case BUILT_IN_ATOMIC_OR_FETCH_1
:
6649 case BUILT_IN_ATOMIC_OR_FETCH_2
:
6650 case BUILT_IN_ATOMIC_OR_FETCH_4
:
6651 case BUILT_IN_ATOMIC_OR_FETCH_8
:
6652 case BUILT_IN_ATOMIC_OR_FETCH_16
:
6654 enum built_in_function lib
;
6655 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
);
6656 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_OR_1
+
6657 (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
));
6658 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, true,
6664 case BUILT_IN_ATOMIC_FETCH_ADD_1
:
6665 case BUILT_IN_ATOMIC_FETCH_ADD_2
:
6666 case BUILT_IN_ATOMIC_FETCH_ADD_4
:
6667 case BUILT_IN_ATOMIC_FETCH_ADD_8
:
6668 case BUILT_IN_ATOMIC_FETCH_ADD_16
:
6669 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_ADD_1
);
6670 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, false,
6671 ignore
, BUILT_IN_NONE
);
6676 case BUILT_IN_ATOMIC_FETCH_SUB_1
:
6677 case BUILT_IN_ATOMIC_FETCH_SUB_2
:
6678 case BUILT_IN_ATOMIC_FETCH_SUB_4
:
6679 case BUILT_IN_ATOMIC_FETCH_SUB_8
:
6680 case BUILT_IN_ATOMIC_FETCH_SUB_16
:
6681 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_SUB_1
);
6682 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, false,
6683 ignore
, BUILT_IN_NONE
);
6688 case BUILT_IN_ATOMIC_FETCH_AND_1
:
6689 case BUILT_IN_ATOMIC_FETCH_AND_2
:
6690 case BUILT_IN_ATOMIC_FETCH_AND_4
:
6691 case BUILT_IN_ATOMIC_FETCH_AND_8
:
6692 case BUILT_IN_ATOMIC_FETCH_AND_16
:
6693 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_AND_1
);
6694 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, false,
6695 ignore
, BUILT_IN_NONE
);
6700 case BUILT_IN_ATOMIC_FETCH_NAND_1
:
6701 case BUILT_IN_ATOMIC_FETCH_NAND_2
:
6702 case BUILT_IN_ATOMIC_FETCH_NAND_4
:
6703 case BUILT_IN_ATOMIC_FETCH_NAND_8
:
6704 case BUILT_IN_ATOMIC_FETCH_NAND_16
:
6705 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_NAND_1
);
6706 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, false,
6707 ignore
, BUILT_IN_NONE
);
6712 case BUILT_IN_ATOMIC_FETCH_XOR_1
:
6713 case BUILT_IN_ATOMIC_FETCH_XOR_2
:
6714 case BUILT_IN_ATOMIC_FETCH_XOR_4
:
6715 case BUILT_IN_ATOMIC_FETCH_XOR_8
:
6716 case BUILT_IN_ATOMIC_FETCH_XOR_16
:
6717 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_XOR_1
);
6718 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, false,
6719 ignore
, BUILT_IN_NONE
);
6724 case BUILT_IN_ATOMIC_FETCH_OR_1
:
6725 case BUILT_IN_ATOMIC_FETCH_OR_2
:
6726 case BUILT_IN_ATOMIC_FETCH_OR_4
:
6727 case BUILT_IN_ATOMIC_FETCH_OR_8
:
6728 case BUILT_IN_ATOMIC_FETCH_OR_16
:
6729 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_OR_1
);
6730 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, false,
6731 ignore
, BUILT_IN_NONE
);
6736 case BUILT_IN_ATOMIC_TEST_AND_SET
:
6737 return expand_builtin_atomic_test_and_set (exp
, target
);
6739 case BUILT_IN_ATOMIC_CLEAR
:
6740 return expand_builtin_atomic_clear (exp
);
6742 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
6743 return expand_builtin_atomic_always_lock_free (exp
);
6745 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
6746 target
= expand_builtin_atomic_is_lock_free (exp
);
6751 case BUILT_IN_ATOMIC_THREAD_FENCE
:
6752 expand_builtin_atomic_thread_fence (exp
);
6755 case BUILT_IN_ATOMIC_SIGNAL_FENCE
:
6756 expand_builtin_atomic_signal_fence (exp
);
6759 case BUILT_IN_OBJECT_SIZE
:
6760 return expand_builtin_object_size (exp
);
6762 case BUILT_IN_MEMCPY_CHK
:
6763 case BUILT_IN_MEMPCPY_CHK
:
6764 case BUILT_IN_MEMMOVE_CHK
:
6765 case BUILT_IN_MEMSET_CHK
:
6766 target
= expand_builtin_memory_chk (exp
, target
, mode
, fcode
);
6771 case BUILT_IN_STRCPY_CHK
:
6772 case BUILT_IN_STPCPY_CHK
:
6773 case BUILT_IN_STRNCPY_CHK
:
6774 case BUILT_IN_STPNCPY_CHK
:
6775 case BUILT_IN_STRCAT_CHK
:
6776 case BUILT_IN_STRNCAT_CHK
:
6777 case BUILT_IN_SNPRINTF_CHK
:
6778 case BUILT_IN_VSNPRINTF_CHK
:
6779 maybe_emit_chk_warning (exp
, fcode
);
6782 case BUILT_IN_SPRINTF_CHK
:
6783 case BUILT_IN_VSPRINTF_CHK
:
6784 maybe_emit_sprintf_chk_warning (exp
, fcode
);
6788 if (warn_free_nonheap_object
)
6789 maybe_emit_free_warning (exp
);
6792 case BUILT_IN_THREAD_POINTER
:
6793 return expand_builtin_thread_pointer (exp
, target
);
6795 case BUILT_IN_SET_THREAD_POINTER
:
6796 expand_builtin_set_thread_pointer (exp
);
6799 case BUILT_IN_CILK_DETACH
:
6800 expand_builtin_cilk_detach (exp
);
6803 case BUILT_IN_CILK_POP_FRAME
:
6804 expand_builtin_cilk_pop_frame (exp
);
6807 default: /* just do library call, if unknown builtin */
6811 /* The switch statement above can drop through to cause the function
6812 to be called normally. */
6813 return expand_call (exp
, target
, ignore
);
6816 /* Determine whether a tree node represents a call to a built-in
6817 function. If the tree T is a call to a built-in function with
6818 the right number of arguments of the appropriate types, return
6819 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
6820 Otherwise the return value is END_BUILTINS. */
6822 enum built_in_function
6823 builtin_mathfn_code (const_tree t
)
6825 const_tree fndecl
, arg
, parmlist
;
6826 const_tree argtype
, parmtype
;
6827 const_call_expr_arg_iterator iter
;
6829 if (TREE_CODE (t
) != CALL_EXPR
6830 || TREE_CODE (CALL_EXPR_FN (t
)) != ADDR_EXPR
)
6831 return END_BUILTINS
;
6833 fndecl
= get_callee_fndecl (t
);
6834 if (fndecl
== NULL_TREE
6835 || TREE_CODE (fndecl
) != FUNCTION_DECL
6836 || ! DECL_BUILT_IN (fndecl
)
6837 || DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
6838 return END_BUILTINS
;
6840 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
6841 init_const_call_expr_arg_iterator (t
, &iter
);
6842 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
6844 /* If a function doesn't take a variable number of arguments,
6845 the last element in the list will have type `void'. */
6846 parmtype
= TREE_VALUE (parmlist
);
6847 if (VOID_TYPE_P (parmtype
))
6849 if (more_const_call_expr_args_p (&iter
))
6850 return END_BUILTINS
;
6851 return DECL_FUNCTION_CODE (fndecl
);
6854 if (! more_const_call_expr_args_p (&iter
))
6855 return END_BUILTINS
;
6857 arg
= next_const_call_expr_arg (&iter
);
6858 argtype
= TREE_TYPE (arg
);
6860 if (SCALAR_FLOAT_TYPE_P (parmtype
))
6862 if (! SCALAR_FLOAT_TYPE_P (argtype
))
6863 return END_BUILTINS
;
6865 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
6867 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
6868 return END_BUILTINS
;
6870 else if (POINTER_TYPE_P (parmtype
))
6872 if (! POINTER_TYPE_P (argtype
))
6873 return END_BUILTINS
;
6875 else if (INTEGRAL_TYPE_P (parmtype
))
6877 if (! INTEGRAL_TYPE_P (argtype
))
6878 return END_BUILTINS
;
6881 return END_BUILTINS
;
6884 /* Variable-length argument list. */
6885 return DECL_FUNCTION_CODE (fndecl
);
6888 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
6889 evaluate to a constant. */
6892 fold_builtin_constant_p (tree arg
)
6894 /* We return 1 for a numeric type that's known to be a constant
6895 value at compile-time or for an aggregate type that's a
6896 literal constant. */
6899 /* If we know this is a constant, emit the constant of one. */
6900 if (CONSTANT_CLASS_P (arg
)
6901 || (TREE_CODE (arg
) == CONSTRUCTOR
6902 && TREE_CONSTANT (arg
)))
6903 return integer_one_node
;
6904 if (TREE_CODE (arg
) == ADDR_EXPR
)
6906 tree op
= TREE_OPERAND (arg
, 0);
6907 if (TREE_CODE (op
) == STRING_CST
6908 || (TREE_CODE (op
) == ARRAY_REF
6909 && integer_zerop (TREE_OPERAND (op
, 1))
6910 && TREE_CODE (TREE_OPERAND (op
, 0)) == STRING_CST
))
6911 return integer_one_node
;
6914 /* If this expression has side effects, show we don't know it to be a
6915 constant. Likewise if it's a pointer or aggregate type since in
6916 those case we only want literals, since those are only optimized
6917 when generating RTL, not later.
6918 And finally, if we are compiling an initializer, not code, we
6919 need to return a definite result now; there's not going to be any
6920 more optimization done. */
6921 if (TREE_SIDE_EFFECTS (arg
)
6922 || AGGREGATE_TYPE_P (TREE_TYPE (arg
))
6923 || POINTER_TYPE_P (TREE_TYPE (arg
))
6925 || folding_initializer
6926 || force_folding_builtin_constant_p
)
6927 return integer_zero_node
;
6932 /* Create builtin_expect with PRED and EXPECTED as its arguments and
6933 return it as a truthvalue. */
6936 build_builtin_expect_predicate (location_t loc
, tree pred
, tree expected
,
6939 tree fn
, arg_types
, pred_type
, expected_type
, call_expr
, ret_type
;
6941 fn
= builtin_decl_explicit (BUILT_IN_EXPECT
);
6942 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
6943 ret_type
= TREE_TYPE (TREE_TYPE (fn
));
6944 pred_type
= TREE_VALUE (arg_types
);
6945 expected_type
= TREE_VALUE (TREE_CHAIN (arg_types
));
6947 pred
= fold_convert_loc (loc
, pred_type
, pred
);
6948 expected
= fold_convert_loc (loc
, expected_type
, expected
);
6949 call_expr
= build_call_expr_loc (loc
, fn
, predictor
? 3 : 2, pred
, expected
,
6952 return build2 (NE_EXPR
, TREE_TYPE (pred
), call_expr
,
6953 build_int_cst (ret_type
, 0));
6956 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
6957 NULL_TREE if no simplification is possible. */
6960 fold_builtin_expect (location_t loc
, tree arg0
, tree arg1
, tree arg2
)
6962 tree inner
, fndecl
, inner_arg0
;
6963 enum tree_code code
;
6965 /* Distribute the expected value over short-circuiting operators.
6966 See through the cast from truthvalue_type_node to long. */
6968 while (TREE_CODE (inner_arg0
) == NOP_EXPR
6969 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0
))
6970 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0
, 0))))
6971 inner_arg0
= TREE_OPERAND (inner_arg0
, 0);
6973 /* If this is a builtin_expect within a builtin_expect keep the
6974 inner one. See through a comparison against a constant. It
6975 might have been added to create a thruthvalue. */
6978 if (COMPARISON_CLASS_P (inner
)
6979 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
)
6980 inner
= TREE_OPERAND (inner
, 0);
6982 if (TREE_CODE (inner
) == CALL_EXPR
6983 && (fndecl
= get_callee_fndecl (inner
))
6984 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
6985 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
)
6989 code
= TREE_CODE (inner
);
6990 if (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
6992 tree op0
= TREE_OPERAND (inner
, 0);
6993 tree op1
= TREE_OPERAND (inner
, 1);
6995 op0
= build_builtin_expect_predicate (loc
, op0
, arg1
, arg2
);
6996 op1
= build_builtin_expect_predicate (loc
, op1
, arg1
, arg2
);
6997 inner
= build2 (code
, TREE_TYPE (inner
), op0
, op1
);
6999 return fold_convert_loc (loc
, TREE_TYPE (arg0
), inner
);
7002 /* If the argument isn't invariant then there's nothing else we can do. */
7003 if (!TREE_CONSTANT (inner_arg0
))
7006 /* If we expect that a comparison against the argument will fold to
7007 a constant return the constant. In practice, this means a true
7008 constant or the address of a non-weak symbol. */
7011 if (TREE_CODE (inner
) == ADDR_EXPR
)
7015 inner
= TREE_OPERAND (inner
, 0);
7017 while (TREE_CODE (inner
) == COMPONENT_REF
7018 || TREE_CODE (inner
) == ARRAY_REF
);
7019 if ((TREE_CODE (inner
) == VAR_DECL
7020 || TREE_CODE (inner
) == FUNCTION_DECL
)
7021 && DECL_WEAK (inner
))
7025 /* Otherwise, ARG0 already has the proper type for the return value. */
7029 /* Fold a call to __builtin_classify_type with argument ARG. */
7032 fold_builtin_classify_type (tree arg
)
7035 return build_int_cst (integer_type_node
, no_type_class
);
7037 return build_int_cst (integer_type_node
, type_to_class (TREE_TYPE (arg
)));
7040 /* Fold a call to __builtin_strlen with argument ARG. */
7043 fold_builtin_strlen (location_t loc
, tree type
, tree arg
)
7045 if (!validate_arg (arg
, POINTER_TYPE
))
7049 tree len
= c_strlen (arg
, 0);
7052 return fold_convert_loc (loc
, type
, len
);
7058 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7061 fold_builtin_inf (location_t loc
, tree type
, int warn
)
7063 REAL_VALUE_TYPE real
;
7065 /* __builtin_inff is intended to be usable to define INFINITY on all
7066 targets. If an infinity is not available, INFINITY expands "to a
7067 positive constant of type float that overflows at translation
7068 time", footnote "In this case, using INFINITY will violate the
7069 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7070 Thus we pedwarn to ensure this constraint violation is
7072 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
7073 pedwarn (loc
, 0, "target format does not support infinity");
7076 return build_real (type
, real
);
7079 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7082 fold_builtin_nan (tree arg
, tree type
, int quiet
)
7084 REAL_VALUE_TYPE real
;
7087 if (!validate_arg (arg
, POINTER_TYPE
))
7089 str
= c_getstr (arg
);
7093 if (!real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
7096 return build_real (type
, real
);
7099 /* Return true if the floating point expression T has an integer value.
7100 We also allow +Inf, -Inf and NaN to be considered integer values. */
7103 integer_valued_real_p (tree t
)
7105 switch (TREE_CODE (t
))
7112 return integer_valued_real_p (TREE_OPERAND (t
, 0));
7117 return integer_valued_real_p (TREE_OPERAND (t
, 1));
7124 return integer_valued_real_p (TREE_OPERAND (t
, 0))
7125 && integer_valued_real_p (TREE_OPERAND (t
, 1));
7128 return integer_valued_real_p (TREE_OPERAND (t
, 1))
7129 && integer_valued_real_p (TREE_OPERAND (t
, 2));
7132 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
7136 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
7137 if (TREE_CODE (type
) == INTEGER_TYPE
)
7139 if (TREE_CODE (type
) == REAL_TYPE
)
7140 return integer_valued_real_p (TREE_OPERAND (t
, 0));
7145 switch (builtin_mathfn_code (t
))
7147 CASE_FLT_FN (BUILT_IN_CEIL
):
7148 CASE_FLT_FN (BUILT_IN_FLOOR
):
7149 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
7150 CASE_FLT_FN (BUILT_IN_RINT
):
7151 CASE_FLT_FN (BUILT_IN_ROUND
):
7152 CASE_FLT_FN (BUILT_IN_TRUNC
):
7155 CASE_FLT_FN (BUILT_IN_FMIN
):
7156 CASE_FLT_FN (BUILT_IN_FMAX
):
7157 return integer_valued_real_p (CALL_EXPR_ARG (t
, 0))
7158 && integer_valued_real_p (CALL_EXPR_ARG (t
, 1));
7171 /* FNDECL is assumed to be a builtin where truncation can be propagated
7172 across (for instance floor((double)f) == (double)floorf (f).
7173 Do the transformation for a call with argument ARG. */
7176 fold_trunc_transparent_mathfn (location_t loc
, tree fndecl
, tree arg
)
7178 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7180 if (!validate_arg (arg
, REAL_TYPE
))
7183 /* Integer rounding functions are idempotent. */
7184 if (fcode
== builtin_mathfn_code (arg
))
7187 /* If argument is already integer valued, and we don't need to worry
7188 about setting errno, there's no need to perform rounding. */
7189 if (! flag_errno_math
&& integer_valued_real_p (arg
))
7194 tree arg0
= strip_float_extensions (arg
);
7195 tree ftype
= TREE_TYPE (TREE_TYPE (fndecl
));
7196 tree newtype
= TREE_TYPE (arg0
);
7199 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
7200 && (decl
= mathfn_built_in (newtype
, fcode
)))
7201 return fold_convert_loc (loc
, ftype
,
7202 build_call_expr_loc (loc
, decl
, 1,
7203 fold_convert_loc (loc
,
7210 /* FNDECL is assumed to be builtin which can narrow the FP type of
7211 the argument, for instance lround((double)f) -> lroundf (f).
7212 Do the transformation for a call with argument ARG. */
7215 fold_fixed_mathfn (location_t loc
, tree fndecl
, tree arg
)
7217 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7219 if (!validate_arg (arg
, REAL_TYPE
))
7222 /* If argument is already integer valued, and we don't need to worry
7223 about setting errno, there's no need to perform rounding. */
7224 if (! flag_errno_math
&& integer_valued_real_p (arg
))
7225 return fold_build1_loc (loc
, FIX_TRUNC_EXPR
,
7226 TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
7230 tree ftype
= TREE_TYPE (arg
);
7231 tree arg0
= strip_float_extensions (arg
);
7232 tree newtype
= TREE_TYPE (arg0
);
7235 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (ftype
)
7236 && (decl
= mathfn_built_in (newtype
, fcode
)))
7237 return build_call_expr_loc (loc
, decl
, 1,
7238 fold_convert_loc (loc
, newtype
, arg0
));
7241 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7242 sizeof (int) == sizeof (long). */
7243 if (TYPE_PRECISION (integer_type_node
)
7244 == TYPE_PRECISION (long_integer_type_node
))
7246 tree newfn
= NULL_TREE
;
7249 CASE_FLT_FN (BUILT_IN_ICEIL
):
7250 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
7253 CASE_FLT_FN (BUILT_IN_IFLOOR
):
7254 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
7257 CASE_FLT_FN (BUILT_IN_IROUND
):
7258 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
7261 CASE_FLT_FN (BUILT_IN_IRINT
):
7262 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
7271 tree newcall
= build_call_expr_loc (loc
, newfn
, 1, arg
);
7272 return fold_convert_loc (loc
,
7273 TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
7277 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7278 sizeof (long long) == sizeof (long). */
7279 if (TYPE_PRECISION (long_long_integer_type_node
)
7280 == TYPE_PRECISION (long_integer_type_node
))
7282 tree newfn
= NULL_TREE
;
7285 CASE_FLT_FN (BUILT_IN_LLCEIL
):
7286 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LCEIL
);
7289 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7290 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LFLOOR
);
7293 CASE_FLT_FN (BUILT_IN_LLROUND
):
7294 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LROUND
);
7297 CASE_FLT_FN (BUILT_IN_LLRINT
):
7298 newfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_LRINT
);
7307 tree newcall
= build_call_expr_loc (loc
, newfn
, 1, arg
);
7308 return fold_convert_loc (loc
,
7309 TREE_TYPE (TREE_TYPE (fndecl
)), newcall
);
7316 /* Fold call to builtin cabs, cabsf or cabsl with argument ARG. TYPE is the
7317 return type. Return NULL_TREE if no simplification can be made. */
7320 fold_builtin_cabs (location_t loc
, tree arg
, tree type
, tree fndecl
)
7324 if (!validate_arg (arg
, COMPLEX_TYPE
)
7325 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
7328 /* Calculate the result when the argument is a constant. */
7329 if (TREE_CODE (arg
) == COMPLEX_CST
7330 && (res
= do_mpfr_arg2 (TREE_REALPART (arg
), TREE_IMAGPART (arg
),
7334 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
7336 tree real
= TREE_OPERAND (arg
, 0);
7337 tree imag
= TREE_OPERAND (arg
, 1);
7339 /* If either part is zero, cabs is fabs of the other. */
7340 if (real_zerop (real
))
7341 return fold_build1_loc (loc
, ABS_EXPR
, type
, imag
);
7342 if (real_zerop (imag
))
7343 return fold_build1_loc (loc
, ABS_EXPR
, type
, real
);
7345 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7346 if (flag_unsafe_math_optimizations
7347 && operand_equal_p (real
, imag
, OEP_PURE_SAME
))
7349 const REAL_VALUE_TYPE sqrt2_trunc
7350 = real_value_truncate (TYPE_MODE (type
), dconst_sqrt2 ());
7352 return fold_build2_loc (loc
, MULT_EXPR
, type
,
7353 fold_build1_loc (loc
, ABS_EXPR
, type
, real
),
7354 build_real (type
, sqrt2_trunc
));
7358 /* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */
7359 if (TREE_CODE (arg
) == NEGATE_EXPR
7360 || TREE_CODE (arg
) == CONJ_EXPR
)
7361 return build_call_expr_loc (loc
, fndecl
, 1, TREE_OPERAND (arg
, 0));
7363 /* Don't do this when optimizing for size. */
7364 if (flag_unsafe_math_optimizations
7365 && optimize
&& optimize_function_for_speed_p (cfun
))
7367 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
7369 if (sqrtfn
!= NULL_TREE
)
7371 tree rpart
, ipart
, result
;
7373 arg
= builtin_save_expr (arg
);
7375 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, type
, arg
);
7376 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg
);
7378 rpart
= builtin_save_expr (rpart
);
7379 ipart
= builtin_save_expr (ipart
);
7381 result
= fold_build2_loc (loc
, PLUS_EXPR
, type
,
7382 fold_build2_loc (loc
, MULT_EXPR
, type
,
7384 fold_build2_loc (loc
, MULT_EXPR
, type
,
7387 return build_call_expr_loc (loc
, sqrtfn
, 1, result
);
7394 /* Build a complex (inf +- 0i) for the result of cproj. TYPE is the
7395 complex tree type of the result. If NEG is true, the imaginary
7396 zero is negative. */
7399 build_complex_cproj (tree type
, bool neg
)
7401 REAL_VALUE_TYPE rinf
, rzero
= dconst0
;
7405 return build_complex (type
, build_real (TREE_TYPE (type
), rinf
),
7406 build_real (TREE_TYPE (type
), rzero
));
7409 /* Fold call to builtin cproj, cprojf or cprojl with argument ARG. TYPE is the
7410 return type. Return NULL_TREE if no simplification can be made. */
7413 fold_builtin_cproj (location_t loc
, tree arg
, tree type
)
7415 if (!validate_arg (arg
, COMPLEX_TYPE
)
7416 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) != REAL_TYPE
)
7419 /* If there are no infinities, return arg. */
7420 if (! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (type
))))
7421 return non_lvalue_loc (loc
, arg
);
7423 /* Calculate the result when the argument is a constant. */
7424 if (TREE_CODE (arg
) == COMPLEX_CST
)
7426 const REAL_VALUE_TYPE
*real
= TREE_REAL_CST_PTR (TREE_REALPART (arg
));
7427 const REAL_VALUE_TYPE
*imag
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg
));
7429 if (real_isinf (real
) || real_isinf (imag
))
7430 return build_complex_cproj (type
, imag
->sign
);
7434 else if (TREE_CODE (arg
) == COMPLEX_EXPR
)
7436 tree real
= TREE_OPERAND (arg
, 0);
7437 tree imag
= TREE_OPERAND (arg
, 1);
7442 /* If the real part is inf and the imag part is known to be
7443 nonnegative, return (inf + 0i). Remember side-effects are
7444 possible in the imag part. */
7445 if (TREE_CODE (real
) == REAL_CST
7446 && real_isinf (TREE_REAL_CST_PTR (real
))
7447 && tree_expr_nonnegative_p (imag
))
7448 return omit_one_operand_loc (loc
, type
,
7449 build_complex_cproj (type
, false),
7452 /* If the imag part is inf, return (inf+I*copysign(0,imag)).
7453 Remember side-effects are possible in the real part. */
7454 if (TREE_CODE (imag
) == REAL_CST
7455 && real_isinf (TREE_REAL_CST_PTR (imag
)))
7457 omit_one_operand_loc (loc
, type
,
7458 build_complex_cproj (type
, TREE_REAL_CST_PTR
7459 (imag
)->sign
), arg
);
7465 /* Fold a builtin function call to sqrt, sqrtf, or sqrtl with argument ARG.
7466 Return NULL_TREE if no simplification can be made. */
7469 fold_builtin_sqrt (location_t loc
, tree arg
, tree type
)
7472 enum built_in_function fcode
;
7475 if (!validate_arg (arg
, REAL_TYPE
))
7478 /* Calculate the result when the argument is a constant. */
7479 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_sqrt
, &dconst0
, NULL
, true)))
7482 /* Optimize sqrt(expN(x)) = expN(x*0.5). */
7483 fcode
= builtin_mathfn_code (arg
);
7484 if (flag_unsafe_math_optimizations
&& BUILTIN_EXPONENT_P (fcode
))
7486 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7487 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
7488 CALL_EXPR_ARG (arg
, 0),
7489 build_real (type
, dconsthalf
));
7490 return build_call_expr_loc (loc
, expfn
, 1, arg
);
7493 /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)). */
7494 if (flag_unsafe_math_optimizations
&& BUILTIN_ROOT_P (fcode
))
7496 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7500 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7502 /* The inner root was either sqrt or cbrt. */
7503 /* This was a conditional expression but it triggered a bug
7505 REAL_VALUE_TYPE dconstroot
;
7506 if (BUILTIN_SQRT_P (fcode
))
7507 dconstroot
= dconsthalf
;
7509 dconstroot
= dconst_third ();
7511 /* Adjust for the outer root. */
7512 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7513 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7514 tree_root
= build_real (type
, dconstroot
);
7515 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7519 /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5). */
7520 if (flag_unsafe_math_optimizations
7521 && (fcode
== BUILT_IN_POW
7522 || fcode
== BUILT_IN_POWF
7523 || fcode
== BUILT_IN_POWL
))
7525 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7526 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7527 tree arg1
= CALL_EXPR_ARG (arg
, 1);
7529 if (!tree_expr_nonnegative_p (arg0
))
7530 arg0
= build1 (ABS_EXPR
, type
, arg0
);
7531 narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
7532 build_real (type
, dconsthalf
));
7533 return build_call_expr_loc (loc
, powfn
, 2, arg0
, narg1
);
7539 /* Fold a builtin function call to cbrt, cbrtf, or cbrtl with argument ARG.
7540 Return NULL_TREE if no simplification can be made. */
7543 fold_builtin_cbrt (location_t loc
, tree arg
, tree type
)
7545 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
7548 if (!validate_arg (arg
, REAL_TYPE
))
7551 /* Calculate the result when the argument is a constant. */
7552 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cbrt
, NULL
, NULL
, 0)))
7555 if (flag_unsafe_math_optimizations
)
7557 /* Optimize cbrt(expN(x)) -> expN(x/3). */
7558 if (BUILTIN_EXPONENT_P (fcode
))
7560 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7561 const REAL_VALUE_TYPE third_trunc
=
7562 real_value_truncate (TYPE_MODE (type
), dconst_third ());
7563 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
,
7564 CALL_EXPR_ARG (arg
, 0),
7565 build_real (type
, third_trunc
));
7566 return build_call_expr_loc (loc
, expfn
, 1, arg
);
7569 /* Optimize cbrt(sqrt(x)) -> pow(x,1/6). */
7570 if (BUILTIN_SQRT_P (fcode
))
7572 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7576 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7578 REAL_VALUE_TYPE dconstroot
= dconst_third ();
7580 SET_REAL_EXP (&dconstroot
, REAL_EXP (&dconstroot
) - 1);
7581 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7582 tree_root
= build_real (type
, dconstroot
);
7583 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7587 /* Optimize cbrt(cbrt(x)) -> pow(x,1/9) iff x is nonnegative. */
7588 if (BUILTIN_CBRT_P (fcode
))
7590 tree arg0
= CALL_EXPR_ARG (arg
, 0);
7591 if (tree_expr_nonnegative_p (arg0
))
7593 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
7598 REAL_VALUE_TYPE dconstroot
;
7600 real_arithmetic (&dconstroot
, MULT_EXPR
,
7601 dconst_third_ptr (), dconst_third_ptr ());
7602 dconstroot
= real_value_truncate (TYPE_MODE (type
), dconstroot
);
7603 tree_root
= build_real (type
, dconstroot
);
7604 return build_call_expr_loc (loc
, powfn
, 2, arg0
, tree_root
);
7609 /* Optimize cbrt(pow(x,y)) -> pow(x,y/3) iff x is nonnegative. */
7610 if (fcode
== BUILT_IN_POW
7611 || fcode
== BUILT_IN_POWF
7612 || fcode
== BUILT_IN_POWL
)
7614 tree arg00
= CALL_EXPR_ARG (arg
, 0);
7615 tree arg01
= CALL_EXPR_ARG (arg
, 1);
7616 if (tree_expr_nonnegative_p (arg00
))
7618 tree powfn
= TREE_OPERAND (CALL_EXPR_FN (arg
), 0);
7619 const REAL_VALUE_TYPE dconstroot
7620 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
7621 tree narg01
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg01
,
7622 build_real (type
, dconstroot
));
7623 return build_call_expr_loc (loc
, powfn
, 2, arg00
, narg01
);
7630 /* Fold function call to builtin cos, cosf, or cosl with argument ARG.
7631 TYPE is the type of the return value. Return NULL_TREE if no
7632 simplification can be made. */
7635 fold_builtin_cos (location_t loc
,
7636 tree arg
, tree type
, tree fndecl
)
7640 if (!validate_arg (arg
, REAL_TYPE
))
7643 /* Calculate the result when the argument is a constant. */
7644 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cos
, NULL
, NULL
, 0)))
7647 /* Optimize cos(-x) into cos (x). */
7648 if ((narg
= fold_strip_sign_ops (arg
)))
7649 return build_call_expr_loc (loc
, fndecl
, 1, narg
);
7654 /* Fold function call to builtin cosh, coshf, or coshl with argument ARG.
7655 Return NULL_TREE if no simplification can be made. */
7658 fold_builtin_cosh (location_t loc
, tree arg
, tree type
, tree fndecl
)
7660 if (validate_arg (arg
, REAL_TYPE
))
7664 /* Calculate the result when the argument is a constant. */
7665 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_cosh
, NULL
, NULL
, 0)))
7668 /* Optimize cosh(-x) into cosh (x). */
7669 if ((narg
= fold_strip_sign_ops (arg
)))
7670 return build_call_expr_loc (loc
, fndecl
, 1, narg
);
7676 /* Fold function call to builtin ccos (or ccosh if HYPER is TRUE) with
7677 argument ARG. TYPE is the type of the return value. Return
7678 NULL_TREE if no simplification can be made. */
7681 fold_builtin_ccos (location_t loc
, tree arg
, tree type
, tree fndecl
,
7684 if (validate_arg (arg
, COMPLEX_TYPE
)
7685 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
7689 /* Calculate the result when the argument is a constant. */
7690 if ((tmp
= do_mpc_arg1 (arg
, type
, (hyper
? mpc_cosh
: mpc_cos
))))
7693 /* Optimize fn(-x) into fn(x). */
7694 if ((tmp
= fold_strip_sign_ops (arg
)))
7695 return build_call_expr_loc (loc
, fndecl
, 1, tmp
);
7701 /* Fold function call to builtin tan, tanf, or tanl with argument ARG.
7702 Return NULL_TREE if no simplification can be made. */
7705 fold_builtin_tan (tree arg
, tree type
)
7707 enum built_in_function fcode
;
7710 if (!validate_arg (arg
, REAL_TYPE
))
7713 /* Calculate the result when the argument is a constant. */
7714 if ((res
= do_mpfr_arg1 (arg
, type
, mpfr_tan
, NULL
, NULL
, 0)))
7717 /* Optimize tan(atan(x)) = x. */
7718 fcode
= builtin_mathfn_code (arg
);
7719 if (flag_unsafe_math_optimizations
7720 && (fcode
== BUILT_IN_ATAN
7721 || fcode
== BUILT_IN_ATANF
7722 || fcode
== BUILT_IN_ATANL
))
7723 return CALL_EXPR_ARG (arg
, 0);
7728 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7729 NULL_TREE if no simplification can be made. */
7732 fold_builtin_sincos (location_t loc
,
7733 tree arg0
, tree arg1
, tree arg2
)
7738 if (!validate_arg (arg0
, REAL_TYPE
)
7739 || !validate_arg (arg1
, POINTER_TYPE
)
7740 || !validate_arg (arg2
, POINTER_TYPE
))
7743 type
= TREE_TYPE (arg0
);
7745 /* Calculate the result when the argument is a constant. */
7746 if ((res
= do_mpfr_sincos (arg0
, arg1
, arg2
)))
7749 /* Canonicalize sincos to cexpi. */
7750 if (!targetm
.libc_has_function (function_c99_math_complex
))
7752 fn
= mathfn_built_in (type
, BUILT_IN_CEXPI
);
7756 call
= build_call_expr_loc (loc
, fn
, 1, arg0
);
7757 call
= builtin_save_expr (call
);
7759 return build2 (COMPOUND_EXPR
, void_type_node
,
7760 build2 (MODIFY_EXPR
, void_type_node
,
7761 build_fold_indirect_ref_loc (loc
, arg1
),
7762 build1 (IMAGPART_EXPR
, type
, call
)),
7763 build2 (MODIFY_EXPR
, void_type_node
,
7764 build_fold_indirect_ref_loc (loc
, arg2
),
7765 build1 (REALPART_EXPR
, type
, call
)));
7768 /* Fold function call to builtin cexp, cexpf, or cexpl. Return
7769 NULL_TREE if no simplification can be made. */
7772 fold_builtin_cexp (location_t loc
, tree arg0
, tree type
)
7775 tree realp
, imagp
, ifn
;
7778 if (!validate_arg (arg0
, COMPLEX_TYPE
)
7779 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) != REAL_TYPE
)
7782 /* Calculate the result when the argument is a constant. */
7783 if ((res
= do_mpc_arg1 (arg0
, type
, mpc_exp
)))
7786 rtype
= TREE_TYPE (TREE_TYPE (arg0
));
7788 /* In case we can figure out the real part of arg0 and it is constant zero
7790 if (!targetm
.libc_has_function (function_c99_math_complex
))
7792 ifn
= mathfn_built_in (rtype
, BUILT_IN_CEXPI
);
7796 if ((realp
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
))
7797 && real_zerop (realp
))
7799 tree narg
= fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
7800 return build_call_expr_loc (loc
, ifn
, 1, narg
);
7803 /* In case we can easily decompose real and imaginary parts split cexp
7804 to exp (r) * cexpi (i). */
7805 if (flag_unsafe_math_optimizations
7808 tree rfn
, rcall
, icall
;
7810 rfn
= mathfn_built_in (rtype
, BUILT_IN_EXP
);
7814 imagp
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
7818 icall
= build_call_expr_loc (loc
, ifn
, 1, imagp
);
7819 icall
= builtin_save_expr (icall
);
7820 rcall
= build_call_expr_loc (loc
, rfn
, 1, realp
);
7821 rcall
= builtin_save_expr (rcall
);
7822 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
7823 fold_build2_loc (loc
, MULT_EXPR
, rtype
,
7825 fold_build1_loc (loc
, REALPART_EXPR
,
7827 fold_build2_loc (loc
, MULT_EXPR
, rtype
,
7829 fold_build1_loc (loc
, IMAGPART_EXPR
,
7836 /* Fold function call to builtin trunc, truncf or truncl with argument ARG.
7837 Return NULL_TREE if no simplification can be made. */
7840 fold_builtin_trunc (location_t loc
, tree fndecl
, tree arg
)
7842 if (!validate_arg (arg
, REAL_TYPE
))
7845 /* Optimize trunc of constant value. */
7846 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7848 REAL_VALUE_TYPE r
, x
;
7849 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7851 x
= TREE_REAL_CST (arg
);
7852 real_trunc (&r
, TYPE_MODE (type
), &x
);
7853 return build_real (type
, r
);
7856 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
7859 /* Fold function call to builtin floor, floorf or floorl with argument ARG.
7860 Return NULL_TREE if no simplification can be made. */
7863 fold_builtin_floor (location_t loc
, tree fndecl
, tree arg
)
7865 if (!validate_arg (arg
, REAL_TYPE
))
7868 /* Optimize floor of constant value. */
7869 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7873 x
= TREE_REAL_CST (arg
);
7874 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7876 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7879 real_floor (&r
, TYPE_MODE (type
), &x
);
7880 return build_real (type
, r
);
7884 /* Fold floor (x) where x is nonnegative to trunc (x). */
7885 if (tree_expr_nonnegative_p (arg
))
7887 tree truncfn
= mathfn_built_in (TREE_TYPE (arg
), BUILT_IN_TRUNC
);
7889 return build_call_expr_loc (loc
, truncfn
, 1, arg
);
7892 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
7895 /* Fold function call to builtin ceil, ceilf or ceill with argument ARG.
7896 Return NULL_TREE if no simplification can be made. */
7899 fold_builtin_ceil (location_t loc
, tree fndecl
, tree arg
)
7901 if (!validate_arg (arg
, REAL_TYPE
))
7904 /* Optimize ceil of constant value. */
7905 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7909 x
= TREE_REAL_CST (arg
);
7910 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7912 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7915 real_ceil (&r
, TYPE_MODE (type
), &x
);
7916 return build_real (type
, r
);
7920 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
7923 /* Fold function call to builtin round, roundf or roundl with argument ARG.
7924 Return NULL_TREE if no simplification can be made. */
7927 fold_builtin_round (location_t loc
, tree fndecl
, tree arg
)
7929 if (!validate_arg (arg
, REAL_TYPE
))
7932 /* Optimize round of constant value. */
7933 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7937 x
= TREE_REAL_CST (arg
);
7938 if (! REAL_VALUE_ISNAN (x
) || ! flag_errno_math
)
7940 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7943 real_round (&r
, TYPE_MODE (type
), &x
);
7944 return build_real (type
, r
);
7948 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg
);
7951 /* Fold function call to builtin lround, lroundf or lroundl (or the
7952 corresponding long long versions) and other rounding functions. ARG
7953 is the argument to the call. Return NULL_TREE if no simplification
7957 fold_builtin_int_roundingfn (location_t loc
, tree fndecl
, tree arg
)
7959 if (!validate_arg (arg
, REAL_TYPE
))
7962 /* Optimize lround of constant value. */
7963 if (TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
7965 const REAL_VALUE_TYPE x
= TREE_REAL_CST (arg
);
7967 if (real_isfinite (&x
))
7969 tree itype
= TREE_TYPE (TREE_TYPE (fndecl
));
7970 tree ftype
= TREE_TYPE (arg
);
7974 switch (DECL_FUNCTION_CODE (fndecl
))
7976 CASE_FLT_FN (BUILT_IN_IFLOOR
):
7977 CASE_FLT_FN (BUILT_IN_LFLOOR
):
7978 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7979 real_floor (&r
, TYPE_MODE (ftype
), &x
);
7982 CASE_FLT_FN (BUILT_IN_ICEIL
):
7983 CASE_FLT_FN (BUILT_IN_LCEIL
):
7984 CASE_FLT_FN (BUILT_IN_LLCEIL
):
7985 real_ceil (&r
, TYPE_MODE (ftype
), &x
);
7988 CASE_FLT_FN (BUILT_IN_IROUND
):
7989 CASE_FLT_FN (BUILT_IN_LROUND
):
7990 CASE_FLT_FN (BUILT_IN_LLROUND
):
7991 real_round (&r
, TYPE_MODE (ftype
), &x
);
7998 wide_int val
= real_to_integer (&r
, &fail
, TYPE_PRECISION (itype
));
8000 return wide_int_to_tree (itype
, val
);
8004 switch (DECL_FUNCTION_CODE (fndecl
))
8006 CASE_FLT_FN (BUILT_IN_LFLOOR
):
8007 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
8008 /* Fold lfloor (x) where x is nonnegative to FIX_TRUNC (x). */
8009 if (tree_expr_nonnegative_p (arg
))
8010 return fold_build1_loc (loc
, FIX_TRUNC_EXPR
,
8011 TREE_TYPE (TREE_TYPE (fndecl
)), arg
);
8016 return fold_fixed_mathfn (loc
, fndecl
, arg
);
8019 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
8020 and their long and long long variants (i.e. ffsl and ffsll). ARG is
8021 the argument to the call. Return NULL_TREE if no simplification can
8025 fold_builtin_bitop (tree fndecl
, tree arg
)
8027 if (!validate_arg (arg
, INTEGER_TYPE
))
8030 /* Optimize for constant argument. */
8031 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
8033 tree type
= TREE_TYPE (arg
);
8036 switch (DECL_FUNCTION_CODE (fndecl
))
8038 CASE_INT_FN (BUILT_IN_FFS
):
8039 result
= wi::ffs (arg
);
8042 CASE_INT_FN (BUILT_IN_CLZ
):
8043 if (wi::ne_p (arg
, 0))
8044 result
= wi::clz (arg
);
8045 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
8046 result
= TYPE_PRECISION (type
);
8049 CASE_INT_FN (BUILT_IN_CTZ
):
8050 if (wi::ne_p (arg
, 0))
8051 result
= wi::ctz (arg
);
8052 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
8053 result
= TYPE_PRECISION (type
);
8056 CASE_INT_FN (BUILT_IN_CLRSB
):
8057 result
= wi::clrsb (arg
);
8060 CASE_INT_FN (BUILT_IN_POPCOUNT
):
8061 result
= wi::popcount (arg
);
8064 CASE_INT_FN (BUILT_IN_PARITY
):
8065 result
= wi::parity (arg
);
8072 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), result
);
8078 /* Fold function call to builtin_bswap and the short, long and long long
8079 variants. Return NULL_TREE if no simplification can be made. */
8081 fold_builtin_bswap (tree fndecl
, tree arg
)
8083 if (! validate_arg (arg
, INTEGER_TYPE
))
8086 /* Optimize constant value. */
8087 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
8089 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8091 switch (DECL_FUNCTION_CODE (fndecl
))
8093 case BUILT_IN_BSWAP16
:
8094 case BUILT_IN_BSWAP32
:
8095 case BUILT_IN_BSWAP64
:
8097 signop sgn
= TYPE_SIGN (type
);
8099 wide_int_to_tree (type
,
8100 wide_int::from (arg
, TYPE_PRECISION (type
),
8112 /* A subroutine of fold_builtin to fold the various logarithmic
8113 functions. Return NULL_TREE if no simplification can me made.
8114 FUNC is the corresponding MPFR logarithm function. */
8117 fold_builtin_logarithm (location_t loc
, tree fndecl
, tree arg
,
8118 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
))
8120 if (validate_arg (arg
, REAL_TYPE
))
8122 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8124 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
8126 /* Calculate the result when the argument is a constant. */
8127 if ((res
= do_mpfr_arg1 (arg
, type
, func
, &dconst0
, NULL
, false)))
8130 /* Special case, optimize logN(expN(x)) = x. */
8131 if (flag_unsafe_math_optimizations
8132 && ((func
== mpfr_log
8133 && (fcode
== BUILT_IN_EXP
8134 || fcode
== BUILT_IN_EXPF
8135 || fcode
== BUILT_IN_EXPL
))
8136 || (func
== mpfr_log2
8137 && (fcode
== BUILT_IN_EXP2
8138 || fcode
== BUILT_IN_EXP2F
8139 || fcode
== BUILT_IN_EXP2L
))
8140 || (func
== mpfr_log10
&& (BUILTIN_EXP10_P (fcode
)))))
8141 return fold_convert_loc (loc
, type
, CALL_EXPR_ARG (arg
, 0));
8143 /* Optimize logN(func()) for various exponential functions. We
8144 want to determine the value "x" and the power "exponent" in
8145 order to transform logN(x**exponent) into exponent*logN(x). */
8146 if (flag_unsafe_math_optimizations
)
8148 tree exponent
= 0, x
= 0;
8152 CASE_FLT_FN (BUILT_IN_EXP
):
8153 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
8154 x
= build_real (type
, real_value_truncate (TYPE_MODE (type
),
8156 exponent
= CALL_EXPR_ARG (arg
, 0);
8158 CASE_FLT_FN (BUILT_IN_EXP2
):
8159 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
8160 x
= build_real (type
, dconst2
);
8161 exponent
= CALL_EXPR_ARG (arg
, 0);
8163 CASE_FLT_FN (BUILT_IN_EXP10
):
8164 CASE_FLT_FN (BUILT_IN_POW10
):
8165 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
8167 REAL_VALUE_TYPE dconst10
;
8168 real_from_integer (&dconst10
, VOIDmode
, 10, SIGNED
);
8169 x
= build_real (type
, dconst10
);
8171 exponent
= CALL_EXPR_ARG (arg
, 0);
8173 CASE_FLT_FN (BUILT_IN_SQRT
):
8174 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
8175 x
= CALL_EXPR_ARG (arg
, 0);
8176 exponent
= build_real (type
, dconsthalf
);
8178 CASE_FLT_FN (BUILT_IN_CBRT
):
8179 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
8180 x
= CALL_EXPR_ARG (arg
, 0);
8181 exponent
= build_real (type
, real_value_truncate (TYPE_MODE (type
),
8184 CASE_FLT_FN (BUILT_IN_POW
):
8185 /* Prepare to do logN(pow(x,exponent) -> exponent*logN(x). */
8186 x
= CALL_EXPR_ARG (arg
, 0);
8187 exponent
= CALL_EXPR_ARG (arg
, 1);
8193 /* Now perform the optimization. */
8196 tree logfn
= build_call_expr_loc (loc
, fndecl
, 1, x
);
8197 return fold_build2_loc (loc
, MULT_EXPR
, type
, exponent
, logfn
);
8205 /* Fold a builtin function call to hypot, hypotf, or hypotl. Return
8206 NULL_TREE if no simplification can be made. */
8209 fold_builtin_hypot (location_t loc
, tree fndecl
,
8210 tree arg0
, tree arg1
, tree type
)
8212 tree res
, narg0
, narg1
;
8214 if (!validate_arg (arg0
, REAL_TYPE
)
8215 || !validate_arg (arg1
, REAL_TYPE
))
8218 /* Calculate the result when the argument is a constant. */
8219 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_hypot
)))
8222 /* If either argument to hypot has a negate or abs, strip that off.
8223 E.g. hypot(-x,fabs(y)) -> hypot(x,y). */
8224 narg0
= fold_strip_sign_ops (arg0
);
8225 narg1
= fold_strip_sign_ops (arg1
);
8228 return build_call_expr_loc (loc
, fndecl
, 2, narg0
? narg0
: arg0
,
8229 narg1
? narg1
: arg1
);
8232 /* If either argument is zero, hypot is fabs of the other. */
8233 if (real_zerop (arg0
))
8234 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg1
);
8235 else if (real_zerop (arg1
))
8236 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg0
);
8238 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8239 if (flag_unsafe_math_optimizations
8240 && operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
8242 const REAL_VALUE_TYPE sqrt2_trunc
8243 = real_value_truncate (TYPE_MODE (type
), dconst_sqrt2 ());
8244 return fold_build2_loc (loc
, MULT_EXPR
, type
,
8245 fold_build1_loc (loc
, ABS_EXPR
, type
, arg0
),
8246 build_real (type
, sqrt2_trunc
));
8253 /* Fold a builtin function call to pow, powf, or powl. Return
8254 NULL_TREE if no simplification can be made. */
8256 fold_builtin_pow (location_t loc
, tree fndecl
, tree arg0
, tree arg1
, tree type
)
8260 if (!validate_arg (arg0
, REAL_TYPE
)
8261 || !validate_arg (arg1
, REAL_TYPE
))
8264 /* Calculate the result when the argument is a constant. */
8265 if ((res
= do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_pow
)))
8268 /* Optimize pow(1.0,y) = 1.0. */
8269 if (real_onep (arg0
))
8270 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
), arg1
);
8272 if (TREE_CODE (arg1
) == REAL_CST
8273 && !TREE_OVERFLOW (arg1
))
8275 REAL_VALUE_TYPE cint
;
8279 c
= TREE_REAL_CST (arg1
);
8281 /* Optimize pow(x,0.0) = 1.0. */
8282 if (REAL_VALUES_EQUAL (c
, dconst0
))
8283 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
),
8286 /* Optimize pow(x,1.0) = x. */
8287 if (REAL_VALUES_EQUAL (c
, dconst1
))
8290 /* Optimize pow(x,-1.0) = 1.0/x. */
8291 if (REAL_VALUES_EQUAL (c
, dconstm1
))
8292 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
8293 build_real (type
, dconst1
), arg0
);
8295 /* Optimize pow(x,0.5) = sqrt(x). */
8296 if (flag_unsafe_math_optimizations
8297 && REAL_VALUES_EQUAL (c
, dconsthalf
))
8299 tree sqrtfn
= mathfn_built_in (type
, BUILT_IN_SQRT
);
8301 if (sqrtfn
!= NULL_TREE
)
8302 return build_call_expr_loc (loc
, sqrtfn
, 1, arg0
);
8305 /* Optimize pow(x,1.0/3.0) = cbrt(x). */
8306 if (flag_unsafe_math_optimizations
)
8308 const REAL_VALUE_TYPE dconstroot
8309 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
8311 if (REAL_VALUES_EQUAL (c
, dconstroot
))
8313 tree cbrtfn
= mathfn_built_in (type
, BUILT_IN_CBRT
);
8314 if (cbrtfn
!= NULL_TREE
)
8315 return build_call_expr_loc (loc
, cbrtfn
, 1, arg0
);
8319 /* Check for an integer exponent. */
8320 n
= real_to_integer (&c
);
8321 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
8322 if (real_identical (&c
, &cint
))
8324 /* Attempt to evaluate pow at compile-time, unless this should
8325 raise an exception. */
8326 if (TREE_CODE (arg0
) == REAL_CST
8327 && !TREE_OVERFLOW (arg0
)
8329 || (!flag_trapping_math
&& !flag_errno_math
)
8330 || !REAL_VALUES_EQUAL (TREE_REAL_CST (arg0
), dconst0
)))
8335 x
= TREE_REAL_CST (arg0
);
8336 inexact
= real_powi (&x
, TYPE_MODE (type
), &x
, n
);
8337 if (flag_unsafe_math_optimizations
|| !inexact
)
8338 return build_real (type
, x
);
8341 /* Strip sign ops from even integer powers. */
8342 if ((n
& 1) == 0 && flag_unsafe_math_optimizations
)
8344 tree narg0
= fold_strip_sign_ops (arg0
);
8346 return build_call_expr_loc (loc
, fndecl
, 2, narg0
, arg1
);
8351 if (flag_unsafe_math_optimizations
)
8353 const enum built_in_function fcode
= builtin_mathfn_code (arg0
);
8355 /* Optimize pow(expN(x),y) = expN(x*y). */
8356 if (BUILTIN_EXPONENT_P (fcode
))
8358 tree expfn
= TREE_OPERAND (CALL_EXPR_FN (arg0
), 0);
8359 tree arg
= CALL_EXPR_ARG (arg0
, 0);
8360 arg
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg
, arg1
);
8361 return build_call_expr_loc (loc
, expfn
, 1, arg
);
8364 /* Optimize pow(sqrt(x),y) = pow(x,y*0.5). */
8365 if (BUILTIN_SQRT_P (fcode
))
8367 tree narg0
= CALL_EXPR_ARG (arg0
, 0);
8368 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
8369 build_real (type
, dconsthalf
));
8370 return build_call_expr_loc (loc
, fndecl
, 2, narg0
, narg1
);
8373 /* Optimize pow(cbrt(x),y) = pow(x,y/3) iff x is nonnegative. */
8374 if (BUILTIN_CBRT_P (fcode
))
8376 tree arg
= CALL_EXPR_ARG (arg0
, 0);
8377 if (tree_expr_nonnegative_p (arg
))
8379 const REAL_VALUE_TYPE dconstroot
8380 = real_value_truncate (TYPE_MODE (type
), dconst_third ());
8381 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg1
,
8382 build_real (type
, dconstroot
));
8383 return build_call_expr_loc (loc
, fndecl
, 2, arg
, narg1
);
8387 /* Optimize pow(pow(x,y),z) = pow(x,y*z) iff x is nonnegative. */
8388 if (fcode
== BUILT_IN_POW
8389 || fcode
== BUILT_IN_POWF
8390 || fcode
== BUILT_IN_POWL
)
8392 tree arg00
= CALL_EXPR_ARG (arg0
, 0);
8393 if (tree_expr_nonnegative_p (arg00
))
8395 tree arg01
= CALL_EXPR_ARG (arg0
, 1);
8396 tree narg1
= fold_build2_loc (loc
, MULT_EXPR
, type
, arg01
, arg1
);
8397 return build_call_expr_loc (loc
, fndecl
, 2, arg00
, narg1
);
8405 /* Fold a builtin function call to powi, powif, or powil with argument ARG.
8406 Return NULL_TREE if no simplification can be made. */
8408 fold_builtin_powi (location_t loc
, tree fndecl ATTRIBUTE_UNUSED
,
8409 tree arg0
, tree arg1
, tree type
)
8411 if (!validate_arg (arg0
, REAL_TYPE
)
8412 || !validate_arg (arg1
, INTEGER_TYPE
))
8415 /* Optimize pow(1.0,y) = 1.0. */
8416 if (real_onep (arg0
))
8417 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
), arg1
);
8419 if (tree_fits_shwi_p (arg1
))
8421 HOST_WIDE_INT c
= tree_to_shwi (arg1
);
8423 /* Evaluate powi at compile-time. */
8424 if (TREE_CODE (arg0
) == REAL_CST
8425 && !TREE_OVERFLOW (arg0
))
8428 x
= TREE_REAL_CST (arg0
);
8429 real_powi (&x
, TYPE_MODE (type
), &x
, c
);
8430 return build_real (type
, x
);
8433 /* Optimize pow(x,0) = 1.0. */
8435 return omit_one_operand_loc (loc
, type
, build_real (type
, dconst1
),
8438 /* Optimize pow(x,1) = x. */
8442 /* Optimize pow(x,-1) = 1.0/x. */
8444 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
8445 build_real (type
, dconst1
), arg0
);
8451 /* A subroutine of fold_builtin to fold the various exponent
8452 functions. Return NULL_TREE if no simplification can be made.
8453 FUNC is the corresponding MPFR exponent function. */
8456 fold_builtin_exponent (location_t loc
, tree fndecl
, tree arg
,
8457 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
))
8459 if (validate_arg (arg
, REAL_TYPE
))
8461 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8464 /* Calculate the result when the argument is a constant. */
8465 if ((res
= do_mpfr_arg1 (arg
, type
, func
, NULL
, NULL
, 0)))
8468 /* Optimize expN(logN(x)) = x. */
8469 if (flag_unsafe_math_optimizations
)
8471 const enum built_in_function fcode
= builtin_mathfn_code (arg
);
8473 if ((func
== mpfr_exp
8474 && (fcode
== BUILT_IN_LOG
8475 || fcode
== BUILT_IN_LOGF
8476 || fcode
== BUILT_IN_LOGL
))
8477 || (func
== mpfr_exp2
8478 && (fcode
== BUILT_IN_LOG2
8479 || fcode
== BUILT_IN_LOG2F
8480 || fcode
== BUILT_IN_LOG2L
))
8481 || (func
== mpfr_exp10
8482 && (fcode
== BUILT_IN_LOG10
8483 || fcode
== BUILT_IN_LOG10F
8484 || fcode
== BUILT_IN_LOG10L
)))
8485 return fold_convert_loc (loc
, type
, CALL_EXPR_ARG (arg
, 0));
8492 /* Fold function call to builtin stpcpy with arguments DEST and SRC.
8493 Return NULL_TREE if no simplification can be made. */
8496 fold_builtin_stpcpy (location_t loc
, tree fndecl
, tree dest
, tree src
)
8498 tree fn
, len
, lenp1
, call
, type
;
8500 if (!validate_arg (dest
, POINTER_TYPE
)
8501 || !validate_arg (src
, POINTER_TYPE
))
8504 len
= c_strlen (src
, 1);
8506 || TREE_CODE (len
) != INTEGER_CST
)
8509 if (optimize_function_for_size_p (cfun
)
8510 /* If length is zero it's small enough. */
8511 && !integer_zerop (len
))
8514 fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
8518 lenp1
= size_binop_loc (loc
, PLUS_EXPR
,
8519 fold_convert_loc (loc
, size_type_node
, len
),
8520 build_int_cst (size_type_node
, 1));
8521 /* We use dest twice in building our expression. Save it from
8522 multiple expansions. */
8523 dest
= builtin_save_expr (dest
);
8524 call
= build_call_expr_loc (loc
, fn
, 3, dest
, src
, lenp1
);
8526 type
= TREE_TYPE (TREE_TYPE (fndecl
));
8527 dest
= fold_build_pointer_plus_loc (loc
, dest
, len
);
8528 dest
= fold_convert_loc (loc
, type
, dest
);
8529 dest
= omit_one_operand_loc (loc
, type
, dest
, call
);
8533 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
8534 arguments to the call, and TYPE is its return type.
8535 Return NULL_TREE if no simplification can be made. */
8538 fold_builtin_memchr (location_t loc
, tree arg1
, tree arg2
, tree len
, tree type
)
8540 if (!validate_arg (arg1
, POINTER_TYPE
)
8541 || !validate_arg (arg2
, INTEGER_TYPE
)
8542 || !validate_arg (len
, INTEGER_TYPE
))
8548 if (TREE_CODE (arg2
) != INTEGER_CST
8549 || !tree_fits_uhwi_p (len
))
8552 p1
= c_getstr (arg1
);
8553 if (p1
&& compare_tree_int (len
, strlen (p1
) + 1) <= 0)
8559 if (target_char_cast (arg2
, &c
))
8562 r
= (const char *) memchr (p1
, c
, tree_to_uhwi (len
));
8565 return build_int_cst (TREE_TYPE (arg1
), 0);
8567 tem
= fold_build_pointer_plus_hwi_loc (loc
, arg1
, r
- p1
);
8568 return fold_convert_loc (loc
, type
, tem
);
8574 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8575 Return NULL_TREE if no simplification can be made. */
8578 fold_builtin_memcmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
8580 const char *p1
, *p2
;
8582 if (!validate_arg (arg1
, POINTER_TYPE
)
8583 || !validate_arg (arg2
, POINTER_TYPE
)
8584 || !validate_arg (len
, INTEGER_TYPE
))
8587 /* If the LEN parameter is zero, return zero. */
8588 if (integer_zerop (len
))
8589 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
8592 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8593 if (operand_equal_p (arg1
, arg2
, 0))
8594 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
8596 p1
= c_getstr (arg1
);
8597 p2
= c_getstr (arg2
);
8599 /* If all arguments are constant, and the value of len is not greater
8600 than the lengths of arg1 and arg2, evaluate at compile-time. */
8601 if (tree_fits_uhwi_p (len
) && p1
&& p2
8602 && compare_tree_int (len
, strlen (p1
) + 1) <= 0
8603 && compare_tree_int (len
, strlen (p2
) + 1) <= 0)
8605 const int r
= memcmp (p1
, p2
, tree_to_uhwi (len
));
8608 return integer_one_node
;
8610 return integer_minus_one_node
;
8612 return integer_zero_node
;
8615 /* If len parameter is one, return an expression corresponding to
8616 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8617 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
8619 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8620 tree cst_uchar_ptr_node
8621 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8624 = fold_convert_loc (loc
, integer_type_node
,
8625 build1 (INDIRECT_REF
, cst_uchar_node
,
8626 fold_convert_loc (loc
,
8630 = fold_convert_loc (loc
, integer_type_node
,
8631 build1 (INDIRECT_REF
, cst_uchar_node
,
8632 fold_convert_loc (loc
,
8635 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8641 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
8642 Return NULL_TREE if no simplification can be made. */
8645 fold_builtin_strcmp (location_t loc
, tree arg1
, tree arg2
)
8647 const char *p1
, *p2
;
8649 if (!validate_arg (arg1
, POINTER_TYPE
)
8650 || !validate_arg (arg2
, POINTER_TYPE
))
8653 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8654 if (operand_equal_p (arg1
, arg2
, 0))
8655 return integer_zero_node
;
8657 p1
= c_getstr (arg1
);
8658 p2
= c_getstr (arg2
);
8662 const int i
= strcmp (p1
, p2
);
8664 return integer_minus_one_node
;
8666 return integer_one_node
;
8668 return integer_zero_node
;
8671 /* If the second arg is "", return *(const unsigned char*)arg1. */
8672 if (p2
&& *p2
== '\0')
8674 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8675 tree cst_uchar_ptr_node
8676 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8678 return fold_convert_loc (loc
, integer_type_node
,
8679 build1 (INDIRECT_REF
, cst_uchar_node
,
8680 fold_convert_loc (loc
,
8685 /* If the first arg is "", return -*(const unsigned char*)arg2. */
8686 if (p1
&& *p1
== '\0')
8688 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8689 tree cst_uchar_ptr_node
8690 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8693 = fold_convert_loc (loc
, integer_type_node
,
8694 build1 (INDIRECT_REF
, cst_uchar_node
,
8695 fold_convert_loc (loc
,
8698 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
8704 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
8705 Return NULL_TREE if no simplification can be made. */
8708 fold_builtin_strncmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
8710 const char *p1
, *p2
;
8712 if (!validate_arg (arg1
, POINTER_TYPE
)
8713 || !validate_arg (arg2
, POINTER_TYPE
)
8714 || !validate_arg (len
, INTEGER_TYPE
))
8717 /* If the LEN parameter is zero, return zero. */
8718 if (integer_zerop (len
))
8719 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
8722 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8723 if (operand_equal_p (arg1
, arg2
, 0))
8724 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
8726 p1
= c_getstr (arg1
);
8727 p2
= c_getstr (arg2
);
8729 if (tree_fits_uhwi_p (len
) && p1
&& p2
)
8731 const int i
= strncmp (p1
, p2
, tree_to_uhwi (len
));
8733 return integer_one_node
;
8735 return integer_minus_one_node
;
8737 return integer_zero_node
;
8740 /* If the second arg is "", and the length is greater than zero,
8741 return *(const unsigned char*)arg1. */
8742 if (p2
&& *p2
== '\0'
8743 && TREE_CODE (len
) == INTEGER_CST
8744 && tree_int_cst_sgn (len
) == 1)
8746 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8747 tree cst_uchar_ptr_node
8748 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8750 return fold_convert_loc (loc
, integer_type_node
,
8751 build1 (INDIRECT_REF
, cst_uchar_node
,
8752 fold_convert_loc (loc
,
8757 /* If the first arg is "", and the length is greater than zero,
8758 return -*(const unsigned char*)arg2. */
8759 if (p1
&& *p1
== '\0'
8760 && TREE_CODE (len
) == INTEGER_CST
8761 && tree_int_cst_sgn (len
) == 1)
8763 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8764 tree cst_uchar_ptr_node
8765 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8767 tree temp
= fold_convert_loc (loc
, integer_type_node
,
8768 build1 (INDIRECT_REF
, cst_uchar_node
,
8769 fold_convert_loc (loc
,
8772 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
8775 /* If len parameter is one, return an expression corresponding to
8776 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8777 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
8779 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8780 tree cst_uchar_ptr_node
8781 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8783 tree ind1
= fold_convert_loc (loc
, integer_type_node
,
8784 build1 (INDIRECT_REF
, cst_uchar_node
,
8785 fold_convert_loc (loc
,
8788 tree ind2
= fold_convert_loc (loc
, integer_type_node
,
8789 build1 (INDIRECT_REF
, cst_uchar_node
,
8790 fold_convert_loc (loc
,
8793 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8799 /* Fold function call to builtin signbit, signbitf or signbitl with argument
8800 ARG. Return NULL_TREE if no simplification can be made. */
8803 fold_builtin_signbit (location_t loc
, tree arg
, tree type
)
8805 if (!validate_arg (arg
, REAL_TYPE
))
8808 /* If ARG is a compile-time constant, determine the result. */
8809 if (TREE_CODE (arg
) == REAL_CST
8810 && !TREE_OVERFLOW (arg
))
8814 c
= TREE_REAL_CST (arg
);
8815 return (REAL_VALUE_NEGATIVE (c
)
8816 ? build_one_cst (type
)
8817 : build_zero_cst (type
));
8820 /* If ARG is non-negative, the result is always zero. */
8821 if (tree_expr_nonnegative_p (arg
))
8822 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
8824 /* If ARG's format doesn't have signed zeros, return "arg < 0.0". */
8825 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg
))))
8826 return fold_convert (type
,
8827 fold_build2_loc (loc
, LT_EXPR
, boolean_type_node
, arg
,
8828 build_real (TREE_TYPE (arg
), dconst0
)));
8833 /* Fold function call to builtin copysign, copysignf or copysignl with
8834 arguments ARG1 and ARG2. Return NULL_TREE if no simplification can
8838 fold_builtin_copysign (location_t loc
, tree fndecl
,
8839 tree arg1
, tree arg2
, tree type
)
8843 if (!validate_arg (arg1
, REAL_TYPE
)
8844 || !validate_arg (arg2
, REAL_TYPE
))
8847 /* copysign(X,X) is X. */
8848 if (operand_equal_p (arg1
, arg2
, 0))
8849 return fold_convert_loc (loc
, type
, arg1
);
8851 /* If ARG1 and ARG2 are compile-time constants, determine the result. */
8852 if (TREE_CODE (arg1
) == REAL_CST
8853 && TREE_CODE (arg2
) == REAL_CST
8854 && !TREE_OVERFLOW (arg1
)
8855 && !TREE_OVERFLOW (arg2
))
8857 REAL_VALUE_TYPE c1
, c2
;
8859 c1
= TREE_REAL_CST (arg1
);
8860 c2
= TREE_REAL_CST (arg2
);
8861 /* c1.sign := c2.sign. */
8862 real_copysign (&c1
, &c2
);
8863 return build_real (type
, c1
);
8866 /* copysign(X, Y) is fabs(X) when Y is always non-negative.
8867 Remember to evaluate Y for side-effects. */
8868 if (tree_expr_nonnegative_p (arg2
))
8869 return omit_one_operand_loc (loc
, type
,
8870 fold_build1_loc (loc
, ABS_EXPR
, type
, arg1
),
8873 /* Strip sign changing operations for the first argument. */
8874 tem
= fold_strip_sign_ops (arg1
);
8876 return build_call_expr_loc (loc
, fndecl
, 2, tem
, arg2
);
8881 /* Fold a call to builtin isascii with argument ARG. */
8884 fold_builtin_isascii (location_t loc
, tree arg
)
8886 if (!validate_arg (arg
, INTEGER_TYPE
))
8890 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8891 arg
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
8892 build_int_cst (integer_type_node
,
8893 ~ (unsigned HOST_WIDE_INT
) 0x7f));
8894 return fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
,
8895 arg
, integer_zero_node
);
8899 /* Fold a call to builtin toascii with argument ARG. */
8902 fold_builtin_toascii (location_t loc
, tree arg
)
8904 if (!validate_arg (arg
, INTEGER_TYPE
))
8907 /* Transform toascii(c) -> (c & 0x7f). */
8908 return fold_build2_loc (loc
, BIT_AND_EXPR
, integer_type_node
, arg
,
8909 build_int_cst (integer_type_node
, 0x7f));
8912 /* Fold a call to builtin isdigit with argument ARG. */
8915 fold_builtin_isdigit (location_t loc
, tree arg
)
8917 if (!validate_arg (arg
, INTEGER_TYPE
))
8921 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8922 /* According to the C standard, isdigit is unaffected by locale.
8923 However, it definitely is affected by the target character set. */
8924 unsigned HOST_WIDE_INT target_digit0
8925 = lang_hooks
.to_target_charset ('0');
8927 if (target_digit0
== 0)
8930 arg
= fold_convert_loc (loc
, unsigned_type_node
, arg
);
8931 arg
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
8932 build_int_cst (unsigned_type_node
, target_digit0
));
8933 return fold_build2_loc (loc
, LE_EXPR
, integer_type_node
, arg
,
8934 build_int_cst (unsigned_type_node
, 9));
8938 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8941 fold_builtin_fabs (location_t loc
, tree arg
, tree type
)
8943 if (!validate_arg (arg
, REAL_TYPE
))
8946 arg
= fold_convert_loc (loc
, type
, arg
);
8947 if (TREE_CODE (arg
) == REAL_CST
)
8948 return fold_abs_const (arg
, type
);
8949 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
8952 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8955 fold_builtin_abs (location_t loc
, tree arg
, tree type
)
8957 if (!validate_arg (arg
, INTEGER_TYPE
))
8960 arg
= fold_convert_loc (loc
, type
, arg
);
8961 if (TREE_CODE (arg
) == INTEGER_CST
)
8962 return fold_abs_const (arg
, type
);
8963 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
8966 /* Fold a fma operation with arguments ARG[012]. */
8969 fold_fma (location_t loc ATTRIBUTE_UNUSED
,
8970 tree type
, tree arg0
, tree arg1
, tree arg2
)
8972 if (TREE_CODE (arg0
) == REAL_CST
8973 && TREE_CODE (arg1
) == REAL_CST
8974 && TREE_CODE (arg2
) == REAL_CST
)
8975 return do_mpfr_arg3 (arg0
, arg1
, arg2
, type
, mpfr_fma
);
8980 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
8983 fold_builtin_fma (location_t loc
, tree arg0
, tree arg1
, tree arg2
, tree type
)
8985 if (validate_arg (arg0
, REAL_TYPE
)
8986 && validate_arg (arg1
, REAL_TYPE
)
8987 && validate_arg (arg2
, REAL_TYPE
))
8989 tree tem
= fold_fma (loc
, type
, arg0
, arg1
, arg2
);
8993 /* ??? Only expand to FMA_EXPR if it's directly supported. */
8994 if (optab_handler (fma_optab
, TYPE_MODE (type
)) != CODE_FOR_nothing
)
8995 return fold_build3_loc (loc
, FMA_EXPR
, type
, arg0
, arg1
, arg2
);
9000 /* Fold a call to builtin fmin or fmax. */
9003 fold_builtin_fmin_fmax (location_t loc
, tree arg0
, tree arg1
,
9004 tree type
, bool max
)
9006 if (validate_arg (arg0
, REAL_TYPE
) && validate_arg (arg1
, REAL_TYPE
))
9008 /* Calculate the result when the argument is a constant. */
9009 tree res
= do_mpfr_arg2 (arg0
, arg1
, type
, (max
? mpfr_max
: mpfr_min
));
9014 /* If either argument is NaN, return the other one. Avoid the
9015 transformation if we get (and honor) a signalling NaN. Using
9016 omit_one_operand() ensures we create a non-lvalue. */
9017 if (TREE_CODE (arg0
) == REAL_CST
9018 && real_isnan (&TREE_REAL_CST (arg0
))
9019 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0
)))
9020 || ! TREE_REAL_CST (arg0
).signalling
))
9021 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
9022 if (TREE_CODE (arg1
) == REAL_CST
9023 && real_isnan (&TREE_REAL_CST (arg1
))
9024 && (! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1
)))
9025 || ! TREE_REAL_CST (arg1
).signalling
))
9026 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9028 /* Transform fmin/fmax(x,x) -> x. */
9029 if (operand_equal_p (arg0
, arg1
, OEP_PURE_SAME
))
9030 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9032 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
9033 functions to return the numeric arg if the other one is NaN.
9034 These tree codes don't honor that, so only transform if
9035 -ffinite-math-only is set. C99 doesn't require -0.0 to be
9036 handled, so we don't have to worry about it either. */
9037 if (flag_finite_math_only
)
9038 return fold_build2_loc (loc
, (max
? MAX_EXPR
: MIN_EXPR
), type
,
9039 fold_convert_loc (loc
, type
, arg0
),
9040 fold_convert_loc (loc
, type
, arg1
));
9045 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
9048 fold_builtin_carg (location_t loc
, tree arg
, tree type
)
9050 if (validate_arg (arg
, COMPLEX_TYPE
)
9051 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
9053 tree atan2_fn
= mathfn_built_in (type
, BUILT_IN_ATAN2
);
9057 tree new_arg
= builtin_save_expr (arg
);
9058 tree r_arg
= fold_build1_loc (loc
, REALPART_EXPR
, type
, new_arg
);
9059 tree i_arg
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, new_arg
);
9060 return build_call_expr_loc (loc
, atan2_fn
, 2, i_arg
, r_arg
);
9067 /* Fold a call to builtin logb/ilogb. */
9070 fold_builtin_logb (location_t loc
, tree arg
, tree rettype
)
9072 if (! validate_arg (arg
, REAL_TYPE
))
9077 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_OVERFLOW (arg
))
9079 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg
);
9085 /* If arg is Inf or NaN and we're logb, return it. */
9086 if (TREE_CODE (rettype
) == REAL_TYPE
)
9088 /* For logb(-Inf) we have to return +Inf. */
9089 if (real_isinf (value
) && real_isneg (value
))
9091 REAL_VALUE_TYPE tem
;
9093 return build_real (rettype
, tem
);
9095 return fold_convert_loc (loc
, rettype
, arg
);
9097 /* Fall through... */
9099 /* Zero may set errno and/or raise an exception for logb, also
9100 for ilogb we don't know FP_ILOGB0. */
9103 /* For normal numbers, proceed iff radix == 2. In GCC,
9104 normalized significands are in the range [0.5, 1.0). We
9105 want the exponent as if they were [1.0, 2.0) so get the
9106 exponent and subtract 1. */
9107 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg
)))->b
== 2)
9108 return fold_convert_loc (loc
, rettype
,
9109 build_int_cst (integer_type_node
,
9110 REAL_EXP (value
)-1));
9118 /* Fold a call to builtin significand, if radix == 2. */
9121 fold_builtin_significand (location_t loc
, tree arg
, tree rettype
)
9123 if (! validate_arg (arg
, REAL_TYPE
))
9128 if (TREE_CODE (arg
) == REAL_CST
&& ! TREE_OVERFLOW (arg
))
9130 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg
);
9137 /* If arg is +-0, +-Inf or +-NaN, then return it. */
9138 return fold_convert_loc (loc
, rettype
, arg
);
9140 /* For normal numbers, proceed iff radix == 2. */
9141 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg
)))->b
== 2)
9143 REAL_VALUE_TYPE result
= *value
;
9144 /* In GCC, normalized significands are in the range [0.5,
9145 1.0). We want them to be [1.0, 2.0) so set the
9147 SET_REAL_EXP (&result
, 1);
9148 return build_real (rettype
, result
);
9157 /* Fold a call to builtin frexp, we can assume the base is 2. */
9160 fold_builtin_frexp (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
9162 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
9167 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
9170 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
9172 /* Proceed if a valid pointer type was passed in. */
9173 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == integer_type_node
)
9175 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9181 /* For +-0, return (*exp = 0, +-0). */
9182 exp
= integer_zero_node
;
9187 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
9188 return omit_one_operand_loc (loc
, rettype
, arg0
, arg1
);
9191 /* Since the frexp function always expects base 2, and in
9192 GCC normalized significands are already in the range
9193 [0.5, 1.0), we have exactly what frexp wants. */
9194 REAL_VALUE_TYPE frac_rvt
= *value
;
9195 SET_REAL_EXP (&frac_rvt
, 0);
9196 frac
= build_real (rettype
, frac_rvt
);
9197 exp
= build_int_cst (integer_type_node
, REAL_EXP (value
));
9204 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9205 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
, exp
);
9206 TREE_SIDE_EFFECTS (arg1
) = 1;
9207 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
, frac
);
9213 /* Fold a call to builtin ldexp or scalbn/scalbln. If LDEXP is true
9214 then we can assume the base is two. If it's false, then we have to
9215 check the mode of the TYPE parameter in certain cases. */
9218 fold_builtin_load_exponent (location_t loc
, tree arg0
, tree arg1
,
9219 tree type
, bool ldexp
)
9221 if (validate_arg (arg0
, REAL_TYPE
) && validate_arg (arg1
, INTEGER_TYPE
))
9226 /* If arg0 is 0, Inf or NaN, or if arg1 is 0, then return arg0. */
9227 if (real_zerop (arg0
) || integer_zerop (arg1
)
9228 || (TREE_CODE (arg0
) == REAL_CST
9229 && !real_isfinite (&TREE_REAL_CST (arg0
))))
9230 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
9232 /* If both arguments are constant, then try to evaluate it. */
9233 if ((ldexp
|| REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2)
9234 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
9235 && tree_fits_shwi_p (arg1
))
9237 /* Bound the maximum adjustment to twice the range of the
9238 mode's valid exponents. Use abs to ensure the range is
9239 positive as a sanity check. */
9240 const long max_exp_adj
= 2 *
9241 labs (REAL_MODE_FORMAT (TYPE_MODE (type
))->emax
9242 - REAL_MODE_FORMAT (TYPE_MODE (type
))->emin
);
9244 /* Get the user-requested adjustment. */
9245 const HOST_WIDE_INT req_exp_adj
= tree_to_shwi (arg1
);
9247 /* The requested adjustment must be inside this range. This
9248 is a preliminary cap to avoid things like overflow, we
9249 may still fail to compute the result for other reasons. */
9250 if (-max_exp_adj
< req_exp_adj
&& req_exp_adj
< max_exp_adj
)
9252 REAL_VALUE_TYPE initial_result
;
9254 real_ldexp (&initial_result
, &TREE_REAL_CST (arg0
), req_exp_adj
);
9256 /* Ensure we didn't overflow. */
9257 if (! real_isinf (&initial_result
))
9259 const REAL_VALUE_TYPE trunc_result
9260 = real_value_truncate (TYPE_MODE (type
), initial_result
);
9262 /* Only proceed if the target mode can hold the
9264 if (REAL_VALUES_EQUAL (initial_result
, trunc_result
))
9265 return build_real (type
, trunc_result
);
9274 /* Fold a call to builtin modf. */
9277 fold_builtin_modf (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
9279 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
9284 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
9287 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
9289 /* Proceed if a valid pointer type was passed in. */
9290 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == TYPE_MAIN_VARIANT (rettype
))
9292 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9293 REAL_VALUE_TYPE trunc
, frac
;
9299 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9300 trunc
= frac
= *value
;
9303 /* For +-Inf, return (*arg1 = arg0, +-0). */
9305 frac
.sign
= value
->sign
;
9309 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9310 real_trunc (&trunc
, VOIDmode
, value
);
9311 real_arithmetic (&frac
, MINUS_EXPR
, value
, &trunc
);
9312 /* If the original number was negative and already
9313 integral, then the fractional part is -0.0. */
9314 if (value
->sign
&& frac
.cl
== rvc_zero
)
9315 frac
.sign
= value
->sign
;
9319 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9320 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
,
9321 build_real (rettype
, trunc
));
9322 TREE_SIDE_EFFECTS (arg1
) = 1;
9323 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
,
9324 build_real (rettype
, frac
));
9330 /* Given a location LOC, an interclass builtin function decl FNDECL
9331 and its single argument ARG, return an folded expression computing
9332 the same, or NULL_TREE if we either couldn't or didn't want to fold
9333 (the latter happen if there's an RTL instruction available). */
9336 fold_builtin_interclass_mathfn (location_t loc
, tree fndecl
, tree arg
)
9338 enum machine_mode mode
;
9340 if (!validate_arg (arg
, REAL_TYPE
))
9343 if (interclass_mathfn_icode (arg
, fndecl
) != CODE_FOR_nothing
)
9346 mode
= TYPE_MODE (TREE_TYPE (arg
));
9348 /* If there is no optab, try generic code. */
9349 switch (DECL_FUNCTION_CODE (fndecl
))
9353 CASE_FLT_FN (BUILT_IN_ISINF
):
9355 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9356 tree
const isgr_fn
= builtin_decl_explicit (BUILT_IN_ISGREATER
);
9357 tree
const type
= TREE_TYPE (arg
);
9361 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9362 real_from_string (&r
, buf
);
9363 result
= build_call_expr (isgr_fn
, 2,
9364 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
9365 build_real (type
, r
));
9368 CASE_FLT_FN (BUILT_IN_FINITE
):
9369 case BUILT_IN_ISFINITE
:
9371 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9372 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
9373 tree
const type
= TREE_TYPE (arg
);
9377 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9378 real_from_string (&r
, buf
);
9379 result
= build_call_expr (isle_fn
, 2,
9380 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
9381 build_real (type
, r
));
9382 /*result = fold_build2_loc (loc, UNGT_EXPR,
9383 TREE_TYPE (TREE_TYPE (fndecl)),
9384 fold_build1_loc (loc, ABS_EXPR, type, arg),
9385 build_real (type, r));
9386 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9387 TREE_TYPE (TREE_TYPE (fndecl)),
9391 case BUILT_IN_ISNORMAL
:
9393 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9394 islessequal(fabs(x),DBL_MAX). */
9395 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
9396 tree
const isge_fn
= builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL
);
9397 tree
const type
= TREE_TYPE (arg
);
9398 REAL_VALUE_TYPE rmax
, rmin
;
9401 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
9402 real_from_string (&rmax
, buf
);
9403 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
9404 real_from_string (&rmin
, buf
);
9405 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
9406 result
= build_call_expr (isle_fn
, 2, arg
,
9407 build_real (type
, rmax
));
9408 result
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, result
,
9409 build_call_expr (isge_fn
, 2, arg
,
9410 build_real (type
, rmin
)));
9420 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9421 ARG is the argument for the call. */
9424 fold_builtin_classify (location_t loc
, tree fndecl
, tree arg
, int builtin_index
)
9426 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9429 if (!validate_arg (arg
, REAL_TYPE
))
9432 switch (builtin_index
)
9434 case BUILT_IN_ISINF
:
9435 if (!HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
9436 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9438 if (TREE_CODE (arg
) == REAL_CST
)
9440 r
= TREE_REAL_CST (arg
);
9441 if (real_isinf (&r
))
9442 return real_compare (GT_EXPR
, &r
, &dconst0
)
9443 ? integer_one_node
: integer_minus_one_node
;
9445 return integer_zero_node
;
9450 case BUILT_IN_ISINF_SIGN
:
9452 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9453 /* In a boolean context, GCC will fold the inner COND_EXPR to
9454 1. So e.g. "if (isinf_sign(x))" would be folded to just
9455 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9456 tree signbit_fn
= mathfn_built_in_1 (TREE_TYPE (arg
), BUILT_IN_SIGNBIT
, 0);
9457 tree isinf_fn
= builtin_decl_explicit (BUILT_IN_ISINF
);
9458 tree tmp
= NULL_TREE
;
9460 arg
= builtin_save_expr (arg
);
9462 if (signbit_fn
&& isinf_fn
)
9464 tree signbit_call
= build_call_expr_loc (loc
, signbit_fn
, 1, arg
);
9465 tree isinf_call
= build_call_expr_loc (loc
, isinf_fn
, 1, arg
);
9467 signbit_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
9468 signbit_call
, integer_zero_node
);
9469 isinf_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
9470 isinf_call
, integer_zero_node
);
9472 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, signbit_call
,
9473 integer_minus_one_node
, integer_one_node
);
9474 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
9482 case BUILT_IN_ISFINITE
:
9483 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg
)))
9484 && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg
))))
9485 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
9487 if (TREE_CODE (arg
) == REAL_CST
)
9489 r
= TREE_REAL_CST (arg
);
9490 return real_isfinite (&r
) ? integer_one_node
: integer_zero_node
;
9495 case BUILT_IN_ISNAN
:
9496 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg
))))
9497 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9499 if (TREE_CODE (arg
) == REAL_CST
)
9501 r
= TREE_REAL_CST (arg
);
9502 return real_isnan (&r
) ? integer_one_node
: integer_zero_node
;
9505 arg
= builtin_save_expr (arg
);
9506 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg
, arg
);
9513 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9514 This builtin will generate code to return the appropriate floating
9515 point classification depending on the value of the floating point
9516 number passed in. The possible return values must be supplied as
9517 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9518 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9519 one floating point argument which is "type generic". */
9522 fold_builtin_fpclassify (location_t loc
, tree exp
)
9524 tree fp_nan
, fp_infinite
, fp_normal
, fp_subnormal
, fp_zero
,
9525 arg
, type
, res
, tmp
;
9526 enum machine_mode mode
;
9530 /* Verify the required arguments in the original call. */
9531 if (!validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
,
9532 INTEGER_TYPE
, INTEGER_TYPE
,
9533 INTEGER_TYPE
, REAL_TYPE
, VOID_TYPE
))
9536 fp_nan
= CALL_EXPR_ARG (exp
, 0);
9537 fp_infinite
= CALL_EXPR_ARG (exp
, 1);
9538 fp_normal
= CALL_EXPR_ARG (exp
, 2);
9539 fp_subnormal
= CALL_EXPR_ARG (exp
, 3);
9540 fp_zero
= CALL_EXPR_ARG (exp
, 4);
9541 arg
= CALL_EXPR_ARG (exp
, 5);
9542 type
= TREE_TYPE (arg
);
9543 mode
= TYPE_MODE (type
);
9544 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
9548 (fabs(x) == Inf ? FP_INFINITE :
9549 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9550 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9552 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
9553 build_real (type
, dconst0
));
9554 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
9555 tmp
, fp_zero
, fp_subnormal
);
9557 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
9558 real_from_string (&r
, buf
);
9559 tmp
= fold_build2_loc (loc
, GE_EXPR
, integer_type_node
,
9560 arg
, build_real (type
, r
));
9561 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, fp_normal
, res
);
9563 if (HONOR_INFINITIES (mode
))
9566 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
9567 build_real (type
, r
));
9568 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
,
9572 if (HONOR_NANS (mode
))
9574 tmp
= fold_build2_loc (loc
, ORDERED_EXPR
, integer_type_node
, arg
, arg
);
9575 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, res
, fp_nan
);
9581 /* Fold a call to an unordered comparison function such as
9582 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9583 being called and ARG0 and ARG1 are the arguments for the call.
9584 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9585 the opposite of the desired result. UNORDERED_CODE is used
9586 for modes that can hold NaNs and ORDERED_CODE is used for
9590 fold_builtin_unordered_cmp (location_t loc
, tree fndecl
, tree arg0
, tree arg1
,
9591 enum tree_code unordered_code
,
9592 enum tree_code ordered_code
)
9594 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9595 enum tree_code code
;
9597 enum tree_code code0
, code1
;
9598 tree cmp_type
= NULL_TREE
;
9600 type0
= TREE_TYPE (arg0
);
9601 type1
= TREE_TYPE (arg1
);
9603 code0
= TREE_CODE (type0
);
9604 code1
= TREE_CODE (type1
);
9606 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
9607 /* Choose the wider of two real types. */
9608 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
9610 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
9612 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
9615 arg0
= fold_convert_loc (loc
, cmp_type
, arg0
);
9616 arg1
= fold_convert_loc (loc
, cmp_type
, arg1
);
9618 if (unordered_code
== UNORDERED_EXPR
)
9620 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))))
9621 return omit_two_operands_loc (loc
, type
, integer_zero_node
, arg0
, arg1
);
9622 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg0
, arg1
);
9625 code
= HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0
))) ? unordered_code
9627 return fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
,
9628 fold_build2_loc (loc
, code
, type
, arg0
, arg1
));
9631 /* Fold a call to built-in function FNDECL with 0 arguments.
9632 IGNORE is true if the result of the function call is ignored. This
9633 function returns NULL_TREE if no simplification was possible. */
9636 fold_builtin_0 (location_t loc
, tree fndecl
, bool ignore ATTRIBUTE_UNUSED
)
9638 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9639 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9642 CASE_FLT_FN (BUILT_IN_INF
):
9643 case BUILT_IN_INFD32
:
9644 case BUILT_IN_INFD64
:
9645 case BUILT_IN_INFD128
:
9646 return fold_builtin_inf (loc
, type
, true);
9648 CASE_FLT_FN (BUILT_IN_HUGE_VAL
):
9649 return fold_builtin_inf (loc
, type
, false);
9651 case BUILT_IN_CLASSIFY_TYPE
:
9652 return fold_builtin_classify_type (NULL_TREE
);
9654 case BUILT_IN_UNREACHABLE
:
9655 if (flag_sanitize
& SANITIZE_UNREACHABLE
9656 && (current_function_decl
== NULL
9657 || !lookup_attribute ("no_sanitize_undefined",
9658 DECL_ATTRIBUTES (current_function_decl
))))
9659 return ubsan_instrument_unreachable (loc
);
9668 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9669 IGNORE is true if the result of the function call is ignored. This
9670 function returns NULL_TREE if no simplification was possible. */
9673 fold_builtin_1 (location_t loc
, tree fndecl
, tree arg0
, bool ignore
)
9675 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9676 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9679 case BUILT_IN_CONSTANT_P
:
9681 tree val
= fold_builtin_constant_p (arg0
);
9683 /* Gimplification will pull the CALL_EXPR for the builtin out of
9684 an if condition. When not optimizing, we'll not CSE it back.
9685 To avoid link error types of regressions, return false now. */
9686 if (!val
&& !optimize
)
9687 val
= integer_zero_node
;
9692 case BUILT_IN_CLASSIFY_TYPE
:
9693 return fold_builtin_classify_type (arg0
);
9695 case BUILT_IN_STRLEN
:
9696 return fold_builtin_strlen (loc
, type
, arg0
);
9698 CASE_FLT_FN (BUILT_IN_FABS
):
9699 case BUILT_IN_FABSD32
:
9700 case BUILT_IN_FABSD64
:
9701 case BUILT_IN_FABSD128
:
9702 return fold_builtin_fabs (loc
, arg0
, type
);
9706 case BUILT_IN_LLABS
:
9707 case BUILT_IN_IMAXABS
:
9708 return fold_builtin_abs (loc
, arg0
, type
);
9710 CASE_FLT_FN (BUILT_IN_CONJ
):
9711 if (validate_arg (arg0
, COMPLEX_TYPE
)
9712 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9713 return fold_build1_loc (loc
, CONJ_EXPR
, type
, arg0
);
9716 CASE_FLT_FN (BUILT_IN_CREAL
):
9717 if (validate_arg (arg0
, COMPLEX_TYPE
)
9718 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9719 return non_lvalue_loc (loc
, fold_build1_loc (loc
, REALPART_EXPR
, type
, arg0
));;
9722 CASE_FLT_FN (BUILT_IN_CIMAG
):
9723 if (validate_arg (arg0
, COMPLEX_TYPE
)
9724 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9725 return non_lvalue_loc (loc
, fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg0
));
9728 CASE_FLT_FN (BUILT_IN_CCOS
):
9729 return fold_builtin_ccos (loc
, arg0
, type
, fndecl
, /*hyper=*/ false);
9731 CASE_FLT_FN (BUILT_IN_CCOSH
):
9732 return fold_builtin_ccos (loc
, arg0
, type
, fndecl
, /*hyper=*/ true);
9734 CASE_FLT_FN (BUILT_IN_CPROJ
):
9735 return fold_builtin_cproj (loc
, arg0
, type
);
9737 CASE_FLT_FN (BUILT_IN_CSIN
):
9738 if (validate_arg (arg0
, COMPLEX_TYPE
)
9739 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9740 return do_mpc_arg1 (arg0
, type
, mpc_sin
);
9743 CASE_FLT_FN (BUILT_IN_CSINH
):
9744 if (validate_arg (arg0
, COMPLEX_TYPE
)
9745 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9746 return do_mpc_arg1 (arg0
, type
, mpc_sinh
);
9749 CASE_FLT_FN (BUILT_IN_CTAN
):
9750 if (validate_arg (arg0
, COMPLEX_TYPE
)
9751 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9752 return do_mpc_arg1 (arg0
, type
, mpc_tan
);
9755 CASE_FLT_FN (BUILT_IN_CTANH
):
9756 if (validate_arg (arg0
, COMPLEX_TYPE
)
9757 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9758 return do_mpc_arg1 (arg0
, type
, mpc_tanh
);
9761 CASE_FLT_FN (BUILT_IN_CLOG
):
9762 if (validate_arg (arg0
, COMPLEX_TYPE
)
9763 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9764 return do_mpc_arg1 (arg0
, type
, mpc_log
);
9767 CASE_FLT_FN (BUILT_IN_CSQRT
):
9768 if (validate_arg (arg0
, COMPLEX_TYPE
)
9769 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9770 return do_mpc_arg1 (arg0
, type
, mpc_sqrt
);
9773 CASE_FLT_FN (BUILT_IN_CASIN
):
9774 if (validate_arg (arg0
, COMPLEX_TYPE
)
9775 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9776 return do_mpc_arg1 (arg0
, type
, mpc_asin
);
9779 CASE_FLT_FN (BUILT_IN_CACOS
):
9780 if (validate_arg (arg0
, COMPLEX_TYPE
)
9781 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9782 return do_mpc_arg1 (arg0
, type
, mpc_acos
);
9785 CASE_FLT_FN (BUILT_IN_CATAN
):
9786 if (validate_arg (arg0
, COMPLEX_TYPE
)
9787 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9788 return do_mpc_arg1 (arg0
, type
, mpc_atan
);
9791 CASE_FLT_FN (BUILT_IN_CASINH
):
9792 if (validate_arg (arg0
, COMPLEX_TYPE
)
9793 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9794 return do_mpc_arg1 (arg0
, type
, mpc_asinh
);
9797 CASE_FLT_FN (BUILT_IN_CACOSH
):
9798 if (validate_arg (arg0
, COMPLEX_TYPE
)
9799 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9800 return do_mpc_arg1 (arg0
, type
, mpc_acosh
);
9803 CASE_FLT_FN (BUILT_IN_CATANH
):
9804 if (validate_arg (arg0
, COMPLEX_TYPE
)
9805 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9806 return do_mpc_arg1 (arg0
, type
, mpc_atanh
);
9809 CASE_FLT_FN (BUILT_IN_CABS
):
9810 return fold_builtin_cabs (loc
, arg0
, type
, fndecl
);
9812 CASE_FLT_FN (BUILT_IN_CARG
):
9813 return fold_builtin_carg (loc
, arg0
, type
);
9815 CASE_FLT_FN (BUILT_IN_SQRT
):
9816 return fold_builtin_sqrt (loc
, arg0
, type
);
9818 CASE_FLT_FN (BUILT_IN_CBRT
):
9819 return fold_builtin_cbrt (loc
, arg0
, type
);
9821 CASE_FLT_FN (BUILT_IN_ASIN
):
9822 if (validate_arg (arg0
, REAL_TYPE
))
9823 return do_mpfr_arg1 (arg0
, type
, mpfr_asin
,
9824 &dconstm1
, &dconst1
, true);
9827 CASE_FLT_FN (BUILT_IN_ACOS
):
9828 if (validate_arg (arg0
, REAL_TYPE
))
9829 return do_mpfr_arg1 (arg0
, type
, mpfr_acos
,
9830 &dconstm1
, &dconst1
, true);
9833 CASE_FLT_FN (BUILT_IN_ATAN
):
9834 if (validate_arg (arg0
, REAL_TYPE
))
9835 return do_mpfr_arg1 (arg0
, type
, mpfr_atan
, NULL
, NULL
, 0);
9838 CASE_FLT_FN (BUILT_IN_ASINH
):
9839 if (validate_arg (arg0
, REAL_TYPE
))
9840 return do_mpfr_arg1 (arg0
, type
, mpfr_asinh
, NULL
, NULL
, 0);
9843 CASE_FLT_FN (BUILT_IN_ACOSH
):
9844 if (validate_arg (arg0
, REAL_TYPE
))
9845 return do_mpfr_arg1 (arg0
, type
, mpfr_acosh
,
9846 &dconst1
, NULL
, true);
9849 CASE_FLT_FN (BUILT_IN_ATANH
):
9850 if (validate_arg (arg0
, REAL_TYPE
))
9851 return do_mpfr_arg1 (arg0
, type
, mpfr_atanh
,
9852 &dconstm1
, &dconst1
, false);
9855 CASE_FLT_FN (BUILT_IN_SIN
):
9856 if (validate_arg (arg0
, REAL_TYPE
))
9857 return do_mpfr_arg1 (arg0
, type
, mpfr_sin
, NULL
, NULL
, 0);
9860 CASE_FLT_FN (BUILT_IN_COS
):
9861 return fold_builtin_cos (loc
, arg0
, type
, fndecl
);
9863 CASE_FLT_FN (BUILT_IN_TAN
):
9864 return fold_builtin_tan (arg0
, type
);
9866 CASE_FLT_FN (BUILT_IN_CEXP
):
9867 return fold_builtin_cexp (loc
, arg0
, type
);
9869 CASE_FLT_FN (BUILT_IN_CEXPI
):
9870 if (validate_arg (arg0
, REAL_TYPE
))
9871 return do_mpfr_sincos (arg0
, NULL_TREE
, NULL_TREE
);
9874 CASE_FLT_FN (BUILT_IN_SINH
):
9875 if (validate_arg (arg0
, REAL_TYPE
))
9876 return do_mpfr_arg1 (arg0
, type
, mpfr_sinh
, NULL
, NULL
, 0);
9879 CASE_FLT_FN (BUILT_IN_COSH
):
9880 return fold_builtin_cosh (loc
, arg0
, type
, fndecl
);
9882 CASE_FLT_FN (BUILT_IN_TANH
):
9883 if (validate_arg (arg0
, REAL_TYPE
))
9884 return do_mpfr_arg1 (arg0
, type
, mpfr_tanh
, NULL
, NULL
, 0);
9887 CASE_FLT_FN (BUILT_IN_ERF
):
9888 if (validate_arg (arg0
, REAL_TYPE
))
9889 return do_mpfr_arg1 (arg0
, type
, mpfr_erf
, NULL
, NULL
, 0);
9892 CASE_FLT_FN (BUILT_IN_ERFC
):
9893 if (validate_arg (arg0
, REAL_TYPE
))
9894 return do_mpfr_arg1 (arg0
, type
, mpfr_erfc
, NULL
, NULL
, 0);
9897 CASE_FLT_FN (BUILT_IN_TGAMMA
):
9898 if (validate_arg (arg0
, REAL_TYPE
))
9899 return do_mpfr_arg1 (arg0
, type
, mpfr_gamma
, NULL
, NULL
, 0);
9902 CASE_FLT_FN (BUILT_IN_EXP
):
9903 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp
);
9905 CASE_FLT_FN (BUILT_IN_EXP2
):
9906 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp2
);
9908 CASE_FLT_FN (BUILT_IN_EXP10
):
9909 CASE_FLT_FN (BUILT_IN_POW10
):
9910 return fold_builtin_exponent (loc
, fndecl
, arg0
, mpfr_exp10
);
9912 CASE_FLT_FN (BUILT_IN_EXPM1
):
9913 if (validate_arg (arg0
, REAL_TYPE
))
9914 return do_mpfr_arg1 (arg0
, type
, mpfr_expm1
, NULL
, NULL
, 0);
9917 CASE_FLT_FN (BUILT_IN_LOG
):
9918 return fold_builtin_logarithm (loc
, fndecl
, arg0
, mpfr_log
);
9920 CASE_FLT_FN (BUILT_IN_LOG2
):
9921 return fold_builtin_logarithm (loc
, fndecl
, arg0
, mpfr_log2
);
9923 CASE_FLT_FN (BUILT_IN_LOG10
):
9924 return fold_builtin_logarithm (loc
, fndecl
, arg0
, mpfr_log10
);
9926 CASE_FLT_FN (BUILT_IN_LOG1P
):
9927 if (validate_arg (arg0
, REAL_TYPE
))
9928 return do_mpfr_arg1 (arg0
, type
, mpfr_log1p
,
9929 &dconstm1
, NULL
, false);
9932 CASE_FLT_FN (BUILT_IN_J0
):
9933 if (validate_arg (arg0
, REAL_TYPE
))
9934 return do_mpfr_arg1 (arg0
, type
, mpfr_j0
,
9938 CASE_FLT_FN (BUILT_IN_J1
):
9939 if (validate_arg (arg0
, REAL_TYPE
))
9940 return do_mpfr_arg1 (arg0
, type
, mpfr_j1
,
9944 CASE_FLT_FN (BUILT_IN_Y0
):
9945 if (validate_arg (arg0
, REAL_TYPE
))
9946 return do_mpfr_arg1 (arg0
, type
, mpfr_y0
,
9947 &dconst0
, NULL
, false);
9950 CASE_FLT_FN (BUILT_IN_Y1
):
9951 if (validate_arg (arg0
, REAL_TYPE
))
9952 return do_mpfr_arg1 (arg0
, type
, mpfr_y1
,
9953 &dconst0
, NULL
, false);
9956 CASE_FLT_FN (BUILT_IN_NAN
):
9957 case BUILT_IN_NAND32
:
9958 case BUILT_IN_NAND64
:
9959 case BUILT_IN_NAND128
:
9960 return fold_builtin_nan (arg0
, type
, true);
9962 CASE_FLT_FN (BUILT_IN_NANS
):
9963 return fold_builtin_nan (arg0
, type
, false);
9965 CASE_FLT_FN (BUILT_IN_FLOOR
):
9966 return fold_builtin_floor (loc
, fndecl
, arg0
);
9968 CASE_FLT_FN (BUILT_IN_CEIL
):
9969 return fold_builtin_ceil (loc
, fndecl
, arg0
);
9971 CASE_FLT_FN (BUILT_IN_TRUNC
):
9972 return fold_builtin_trunc (loc
, fndecl
, arg0
);
9974 CASE_FLT_FN (BUILT_IN_ROUND
):
9975 return fold_builtin_round (loc
, fndecl
, arg0
);
9977 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
9978 CASE_FLT_FN (BUILT_IN_RINT
):
9979 return fold_trunc_transparent_mathfn (loc
, fndecl
, arg0
);
9981 CASE_FLT_FN (BUILT_IN_ICEIL
):
9982 CASE_FLT_FN (BUILT_IN_LCEIL
):
9983 CASE_FLT_FN (BUILT_IN_LLCEIL
):
9984 CASE_FLT_FN (BUILT_IN_LFLOOR
):
9985 CASE_FLT_FN (BUILT_IN_IFLOOR
):
9986 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
9987 CASE_FLT_FN (BUILT_IN_IROUND
):
9988 CASE_FLT_FN (BUILT_IN_LROUND
):
9989 CASE_FLT_FN (BUILT_IN_LLROUND
):
9990 return fold_builtin_int_roundingfn (loc
, fndecl
, arg0
);
9992 CASE_FLT_FN (BUILT_IN_IRINT
):
9993 CASE_FLT_FN (BUILT_IN_LRINT
):
9994 CASE_FLT_FN (BUILT_IN_LLRINT
):
9995 return fold_fixed_mathfn (loc
, fndecl
, arg0
);
9997 case BUILT_IN_BSWAP16
:
9998 case BUILT_IN_BSWAP32
:
9999 case BUILT_IN_BSWAP64
:
10000 return fold_builtin_bswap (fndecl
, arg0
);
10002 CASE_INT_FN (BUILT_IN_FFS
):
10003 CASE_INT_FN (BUILT_IN_CLZ
):
10004 CASE_INT_FN (BUILT_IN_CTZ
):
10005 CASE_INT_FN (BUILT_IN_CLRSB
):
10006 CASE_INT_FN (BUILT_IN_POPCOUNT
):
10007 CASE_INT_FN (BUILT_IN_PARITY
):
10008 return fold_builtin_bitop (fndecl
, arg0
);
10010 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
10011 return fold_builtin_signbit (loc
, arg0
, type
);
10013 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
10014 return fold_builtin_significand (loc
, arg0
, type
);
10016 CASE_FLT_FN (BUILT_IN_ILOGB
):
10017 CASE_FLT_FN (BUILT_IN_LOGB
):
10018 return fold_builtin_logb (loc
, arg0
, type
);
10020 case BUILT_IN_ISASCII
:
10021 return fold_builtin_isascii (loc
, arg0
);
10023 case BUILT_IN_TOASCII
:
10024 return fold_builtin_toascii (loc
, arg0
);
10026 case BUILT_IN_ISDIGIT
:
10027 return fold_builtin_isdigit (loc
, arg0
);
10029 CASE_FLT_FN (BUILT_IN_FINITE
):
10030 case BUILT_IN_FINITED32
:
10031 case BUILT_IN_FINITED64
:
10032 case BUILT_IN_FINITED128
:
10033 case BUILT_IN_ISFINITE
:
10035 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISFINITE
);
10038 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10041 CASE_FLT_FN (BUILT_IN_ISINF
):
10042 case BUILT_IN_ISINFD32
:
10043 case BUILT_IN_ISINFD64
:
10044 case BUILT_IN_ISINFD128
:
10046 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF
);
10049 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10052 case BUILT_IN_ISNORMAL
:
10053 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
10055 case BUILT_IN_ISINF_SIGN
:
10056 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF_SIGN
);
10058 CASE_FLT_FN (BUILT_IN_ISNAN
):
10059 case BUILT_IN_ISNAND32
:
10060 case BUILT_IN_ISNAND64
:
10061 case BUILT_IN_ISNAND128
:
10062 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISNAN
);
10064 case BUILT_IN_PRINTF
:
10065 case BUILT_IN_PRINTF_UNLOCKED
:
10066 case BUILT_IN_VPRINTF
:
10067 return fold_builtin_printf (loc
, fndecl
, arg0
, NULL_TREE
, ignore
, fcode
);
10069 case BUILT_IN_FREE
:
10070 if (integer_zerop (arg0
))
10071 return build_empty_stmt (loc
);
10082 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
10083 IGNORE is true if the result of the function call is ignored. This
10084 function returns NULL_TREE if no simplification was possible. */
10087 fold_builtin_2 (location_t loc
, tree fndecl
, tree arg0
, tree arg1
, bool ignore
)
10089 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10090 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10094 CASE_FLT_FN (BUILT_IN_JN
):
10095 if (validate_arg (arg0
, INTEGER_TYPE
)
10096 && validate_arg (arg1
, REAL_TYPE
))
10097 return do_mpfr_bessel_n (arg0
, arg1
, type
, mpfr_jn
, NULL
, 0);
10100 CASE_FLT_FN (BUILT_IN_YN
):
10101 if (validate_arg (arg0
, INTEGER_TYPE
)
10102 && validate_arg (arg1
, REAL_TYPE
))
10103 return do_mpfr_bessel_n (arg0
, arg1
, type
, mpfr_yn
,
10107 CASE_FLT_FN (BUILT_IN_DREM
):
10108 CASE_FLT_FN (BUILT_IN_REMAINDER
):
10109 if (validate_arg (arg0
, REAL_TYPE
)
10110 && validate_arg (arg1
, REAL_TYPE
))
10111 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_remainder
);
10114 CASE_FLT_FN_REENT (BUILT_IN_GAMMA
): /* GAMMA_R */
10115 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA
): /* LGAMMA_R */
10116 if (validate_arg (arg0
, REAL_TYPE
)
10117 && validate_arg (arg1
, POINTER_TYPE
))
10118 return do_mpfr_lgamma_r (arg0
, arg1
, type
);
10121 CASE_FLT_FN (BUILT_IN_ATAN2
):
10122 if (validate_arg (arg0
, REAL_TYPE
)
10123 && validate_arg (arg1
, REAL_TYPE
))
10124 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_atan2
);
10127 CASE_FLT_FN (BUILT_IN_FDIM
):
10128 if (validate_arg (arg0
, REAL_TYPE
)
10129 && validate_arg (arg1
, REAL_TYPE
))
10130 return do_mpfr_arg2 (arg0
, arg1
, type
, mpfr_dim
);
10133 CASE_FLT_FN (BUILT_IN_HYPOT
):
10134 return fold_builtin_hypot (loc
, fndecl
, arg0
, arg1
, type
);
10136 CASE_FLT_FN (BUILT_IN_CPOW
):
10137 if (validate_arg (arg0
, COMPLEX_TYPE
)
10138 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
10139 && validate_arg (arg1
, COMPLEX_TYPE
)
10140 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
)
10141 return do_mpc_arg2 (arg0
, arg1
, type
, /*do_nonfinite=*/ 0, mpc_pow
);
10144 CASE_FLT_FN (BUILT_IN_LDEXP
):
10145 return fold_builtin_load_exponent (loc
, arg0
, arg1
, type
, /*ldexp=*/true);
10146 CASE_FLT_FN (BUILT_IN_SCALBN
):
10147 CASE_FLT_FN (BUILT_IN_SCALBLN
):
10148 return fold_builtin_load_exponent (loc
, arg0
, arg1
,
10149 type
, /*ldexp=*/false);
10151 CASE_FLT_FN (BUILT_IN_FREXP
):
10152 return fold_builtin_frexp (loc
, arg0
, arg1
, type
);
10154 CASE_FLT_FN (BUILT_IN_MODF
):
10155 return fold_builtin_modf (loc
, arg0
, arg1
, type
);
10157 case BUILT_IN_STRSTR
:
10158 return fold_builtin_strstr (loc
, arg0
, arg1
, type
);
10160 case BUILT_IN_STRSPN
:
10161 return fold_builtin_strspn (loc
, arg0
, arg1
);
10163 case BUILT_IN_STRCSPN
:
10164 return fold_builtin_strcspn (loc
, arg0
, arg1
);
10166 case BUILT_IN_STRCHR
:
10167 case BUILT_IN_INDEX
:
10168 return fold_builtin_strchr (loc
, arg0
, arg1
, type
);
10170 case BUILT_IN_STRRCHR
:
10171 case BUILT_IN_RINDEX
:
10172 return fold_builtin_strrchr (loc
, arg0
, arg1
, type
);
10174 case BUILT_IN_STPCPY
:
10177 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
10181 return build_call_expr_loc (loc
, fn
, 2, arg0
, arg1
);
10184 return fold_builtin_stpcpy (loc
, fndecl
, arg0
, arg1
);
10187 case BUILT_IN_STRCMP
:
10188 return fold_builtin_strcmp (loc
, arg0
, arg1
);
10190 case BUILT_IN_STRPBRK
:
10191 return fold_builtin_strpbrk (loc
, arg0
, arg1
, type
);
10193 case BUILT_IN_EXPECT
:
10194 return fold_builtin_expect (loc
, arg0
, arg1
, NULL_TREE
);
10196 CASE_FLT_FN (BUILT_IN_POW
):
10197 return fold_builtin_pow (loc
, fndecl
, arg0
, arg1
, type
);
10199 CASE_FLT_FN (BUILT_IN_POWI
):
10200 return fold_builtin_powi (loc
, fndecl
, arg0
, arg1
, type
);
10202 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
10203 return fold_builtin_copysign (loc
, fndecl
, arg0
, arg1
, type
);
10205 CASE_FLT_FN (BUILT_IN_FMIN
):
10206 return fold_builtin_fmin_fmax (loc
, arg0
, arg1
, type
, /*max=*/false);
10208 CASE_FLT_FN (BUILT_IN_FMAX
):
10209 return fold_builtin_fmin_fmax (loc
, arg0
, arg1
, type
, /*max=*/true);
10211 case BUILT_IN_ISGREATER
:
10212 return fold_builtin_unordered_cmp (loc
, fndecl
,
10213 arg0
, arg1
, UNLE_EXPR
, LE_EXPR
);
10214 case BUILT_IN_ISGREATEREQUAL
:
10215 return fold_builtin_unordered_cmp (loc
, fndecl
,
10216 arg0
, arg1
, UNLT_EXPR
, LT_EXPR
);
10217 case BUILT_IN_ISLESS
:
10218 return fold_builtin_unordered_cmp (loc
, fndecl
,
10219 arg0
, arg1
, UNGE_EXPR
, GE_EXPR
);
10220 case BUILT_IN_ISLESSEQUAL
:
10221 return fold_builtin_unordered_cmp (loc
, fndecl
,
10222 arg0
, arg1
, UNGT_EXPR
, GT_EXPR
);
10223 case BUILT_IN_ISLESSGREATER
:
10224 return fold_builtin_unordered_cmp (loc
, fndecl
,
10225 arg0
, arg1
, UNEQ_EXPR
, EQ_EXPR
);
10226 case BUILT_IN_ISUNORDERED
:
10227 return fold_builtin_unordered_cmp (loc
, fndecl
,
10228 arg0
, arg1
, UNORDERED_EXPR
,
10231 /* We do the folding for va_start in the expander. */
10232 case BUILT_IN_VA_START
:
10235 case BUILT_IN_OBJECT_SIZE
:
10236 return fold_builtin_object_size (arg0
, arg1
);
10238 case BUILT_IN_PRINTF
:
10239 case BUILT_IN_PRINTF_UNLOCKED
:
10240 case BUILT_IN_VPRINTF
:
10241 return fold_builtin_printf (loc
, fndecl
, arg0
, arg1
, ignore
, fcode
);
10243 case BUILT_IN_PRINTF_CHK
:
10244 case BUILT_IN_VPRINTF_CHK
:
10245 if (!validate_arg (arg0
, INTEGER_TYPE
)
10246 || TREE_SIDE_EFFECTS (arg0
))
10249 return fold_builtin_printf (loc
, fndecl
,
10250 arg1
, NULL_TREE
, ignore
, fcode
);
10253 case BUILT_IN_FPRINTF
:
10254 case BUILT_IN_FPRINTF_UNLOCKED
:
10255 case BUILT_IN_VFPRINTF
:
10256 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg1
, NULL_TREE
,
10259 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
10260 return fold_builtin_atomic_always_lock_free (arg0
, arg1
);
10262 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
10263 return fold_builtin_atomic_is_lock_free (arg0
, arg1
);
10271 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
10272 and ARG2. IGNORE is true if the result of the function call is ignored.
10273 This function returns NULL_TREE if no simplification was possible. */
10276 fold_builtin_3 (location_t loc
, tree fndecl
,
10277 tree arg0
, tree arg1
, tree arg2
, bool ignore
)
10279 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
10280 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10284 CASE_FLT_FN (BUILT_IN_SINCOS
):
10285 return fold_builtin_sincos (loc
, arg0
, arg1
, arg2
);
10287 CASE_FLT_FN (BUILT_IN_FMA
):
10288 return fold_builtin_fma (loc
, arg0
, arg1
, arg2
, type
);
10291 CASE_FLT_FN (BUILT_IN_REMQUO
):
10292 if (validate_arg (arg0
, REAL_TYPE
)
10293 && validate_arg (arg1
, REAL_TYPE
)
10294 && validate_arg (arg2
, POINTER_TYPE
))
10295 return do_mpfr_remquo (arg0
, arg1
, arg2
);
10298 case BUILT_IN_STRNCAT
:
10299 return fold_builtin_strncat (loc
, arg0
, arg1
, arg2
);
10301 case BUILT_IN_STRNCMP
:
10302 return fold_builtin_strncmp (loc
, arg0
, arg1
, arg2
);
10304 case BUILT_IN_MEMCHR
:
10305 return fold_builtin_memchr (loc
, arg0
, arg1
, arg2
, type
);
10307 case BUILT_IN_BCMP
:
10308 case BUILT_IN_MEMCMP
:
10309 return fold_builtin_memcmp (loc
, arg0
, arg1
, arg2
);;
10311 case BUILT_IN_STRCAT_CHK
:
10312 return fold_builtin_strcat_chk (loc
, fndecl
, arg0
, arg1
, arg2
);
10314 case BUILT_IN_PRINTF_CHK
:
10315 case BUILT_IN_VPRINTF_CHK
:
10316 if (!validate_arg (arg0
, INTEGER_TYPE
)
10317 || TREE_SIDE_EFFECTS (arg0
))
10320 return fold_builtin_printf (loc
, fndecl
, arg1
, arg2
, ignore
, fcode
);
10323 case BUILT_IN_FPRINTF
:
10324 case BUILT_IN_FPRINTF_UNLOCKED
:
10325 case BUILT_IN_VFPRINTF
:
10326 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg1
, arg2
,
10329 case BUILT_IN_FPRINTF_CHK
:
10330 case BUILT_IN_VFPRINTF_CHK
:
10331 if (!validate_arg (arg1
, INTEGER_TYPE
)
10332 || TREE_SIDE_EFFECTS (arg1
))
10335 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg2
, NULL_TREE
,
10338 case BUILT_IN_EXPECT
:
10339 return fold_builtin_expect (loc
, arg0
, arg1
, arg2
);
10347 /* Fold a call to built-in function FNDECL with 4 arguments, ARG0, ARG1,
10348 ARG2, and ARG3. IGNORE is true if the result of the function call is
10349 ignored. This function returns NULL_TREE if no simplification was
10353 fold_builtin_4 (location_t loc
, tree fndecl
,
10354 tree arg0
, tree arg1
, tree arg2
, tree arg3
, bool ignore
)
10356 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10360 case BUILT_IN_STRNCAT_CHK
:
10361 return fold_builtin_strncat_chk (loc
, fndecl
, arg0
, arg1
, arg2
, arg3
);
10363 case BUILT_IN_FPRINTF_CHK
:
10364 case BUILT_IN_VFPRINTF_CHK
:
10365 if (!validate_arg (arg1
, INTEGER_TYPE
)
10366 || TREE_SIDE_EFFECTS (arg1
))
10369 return fold_builtin_fprintf (loc
, fndecl
, arg0
, arg2
, arg3
,
10379 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
10380 arguments, where NARGS <= 4. IGNORE is true if the result of the
10381 function call is ignored. This function returns NULL_TREE if no
10382 simplification was possible. Note that this only folds builtins with
10383 fixed argument patterns. Foldings that do varargs-to-varargs
10384 transformations, or that match calls with more than 4 arguments,
10385 need to be handled with fold_builtin_varargs instead. */
10387 #define MAX_ARGS_TO_FOLD_BUILTIN 4
10390 fold_builtin_n (location_t loc
, tree fndecl
, tree
*args
, int nargs
, bool ignore
)
10392 tree ret
= NULL_TREE
;
10397 ret
= fold_builtin_0 (loc
, fndecl
, ignore
);
10400 ret
= fold_builtin_1 (loc
, fndecl
, args
[0], ignore
);
10403 ret
= fold_builtin_2 (loc
, fndecl
, args
[0], args
[1], ignore
);
10406 ret
= fold_builtin_3 (loc
, fndecl
, args
[0], args
[1], args
[2], ignore
);
10409 ret
= fold_builtin_4 (loc
, fndecl
, args
[0], args
[1], args
[2], args
[3],
10417 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
10418 SET_EXPR_LOCATION (ret
, loc
);
10419 TREE_NO_WARNING (ret
) = 1;
10425 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10426 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10427 of arguments in ARGS to be omitted. OLDNARGS is the number of
10428 elements in ARGS. */
10431 rewrite_call_expr_valist (location_t loc
, int oldnargs
, tree
*args
,
10432 int skip
, tree fndecl
, int n
, va_list newargs
)
10434 int nargs
= oldnargs
- skip
+ n
;
10441 buffer
= XALLOCAVEC (tree
, nargs
);
10442 for (i
= 0; i
< n
; i
++)
10443 buffer
[i
] = va_arg (newargs
, tree
);
10444 for (j
= skip
; j
< oldnargs
; j
++, i
++)
10445 buffer
[i
] = args
[j
];
10448 buffer
= args
+ skip
;
10450 return build_call_expr_loc_array (loc
, fndecl
, nargs
, buffer
);
10453 /* Return true if FNDECL shouldn't be folded right now.
10454 If a built-in function has an inline attribute always_inline
10455 wrapper, defer folding it after always_inline functions have
10456 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10457 might not be performed. */
10460 avoid_folding_inline_builtin (tree fndecl
)
10462 return (DECL_DECLARED_INLINE_P (fndecl
)
10463 && DECL_DISREGARD_INLINE_LIMITS (fndecl
)
10465 && !cfun
->always_inline_functions_inlined
10466 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl
)));
10469 /* A wrapper function for builtin folding that prevents warnings for
10470 "statement without effect" and the like, caused by removing the
10471 call node earlier than the warning is generated. */
10474 fold_call_expr (location_t loc
, tree exp
, bool ignore
)
10476 tree ret
= NULL_TREE
;
10477 tree fndecl
= get_callee_fndecl (exp
);
10479 && TREE_CODE (fndecl
) == FUNCTION_DECL
10480 && DECL_BUILT_IN (fndecl
)
10481 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10482 yet. Defer folding until we see all the arguments
10483 (after inlining). */
10484 && !CALL_EXPR_VA_ARG_PACK (exp
))
10486 int nargs
= call_expr_nargs (exp
);
10488 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10489 instead last argument is __builtin_va_arg_pack (). Defer folding
10490 even in that case, until arguments are finalized. */
10491 if (nargs
&& TREE_CODE (CALL_EXPR_ARG (exp
, nargs
- 1)) == CALL_EXPR
)
10493 tree fndecl2
= get_callee_fndecl (CALL_EXPR_ARG (exp
, nargs
- 1));
10495 && TREE_CODE (fndecl2
) == FUNCTION_DECL
10496 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
10497 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
10501 if (avoid_folding_inline_builtin (fndecl
))
10504 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
10505 return targetm
.fold_builtin (fndecl
, call_expr_nargs (exp
),
10506 CALL_EXPR_ARGP (exp
), ignore
);
10509 if (nargs
<= MAX_ARGS_TO_FOLD_BUILTIN
)
10511 tree
*args
= CALL_EXPR_ARGP (exp
);
10512 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
10515 ret
= fold_builtin_varargs (loc
, fndecl
, exp
, ignore
);
10523 /* Construct a CALL_EXPR with type TYPE with FN as the function expression.
10524 N arguments are passed in the array ARGARRAY. */
10527 fold_builtin_call_array (location_t loc
, tree type
,
10532 tree ret
= NULL_TREE
;
10535 if (TREE_CODE (fn
) == ADDR_EXPR
)
10537 tree fndecl
= TREE_OPERAND (fn
, 0);
10538 if (TREE_CODE (fndecl
) == FUNCTION_DECL
10539 && DECL_BUILT_IN (fndecl
))
10541 /* If last argument is __builtin_va_arg_pack (), arguments to this
10542 function are not finalized yet. Defer folding until they are. */
10543 if (n
&& TREE_CODE (argarray
[n
- 1]) == CALL_EXPR
)
10545 tree fndecl2
= get_callee_fndecl (argarray
[n
- 1]);
10547 && TREE_CODE (fndecl2
) == FUNCTION_DECL
10548 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
10549 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
10550 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
10552 if (avoid_folding_inline_builtin (fndecl
))
10553 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
10554 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
10556 ret
= targetm
.fold_builtin (fndecl
, n
, argarray
, false);
10560 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
10562 else if (n
<= MAX_ARGS_TO_FOLD_BUILTIN
)
10564 /* First try the transformations that don't require consing up
10566 ret
= fold_builtin_n (loc
, fndecl
, argarray
, n
, false);
10571 /* If we got this far, we need to build an exp. */
10572 exp
= build_call_array_loc (loc
, type
, fn
, n
, argarray
);
10573 ret
= fold_builtin_varargs (loc
, fndecl
, exp
, false);
10574 return ret
? ret
: exp
;
10578 return build_call_array_loc (loc
, type
, fn
, n
, argarray
);
10581 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10582 along with N new arguments specified as the "..." parameters. SKIP
10583 is the number of arguments in EXP to be omitted. This function is used
10584 to do varargs-to-varargs transformations. */
10587 rewrite_call_expr (location_t loc
, tree exp
, int skip
, tree fndecl
, int n
, ...)
10593 t
= rewrite_call_expr_valist (loc
, call_expr_nargs (exp
),
10594 CALL_EXPR_ARGP (exp
), skip
, fndecl
, n
, ap
);
10600 /* Validate a single argument ARG against a tree code CODE representing
10604 validate_arg (const_tree arg
, enum tree_code code
)
10608 else if (code
== POINTER_TYPE
)
10609 return POINTER_TYPE_P (TREE_TYPE (arg
));
10610 else if (code
== INTEGER_TYPE
)
10611 return INTEGRAL_TYPE_P (TREE_TYPE (arg
));
10612 return code
== TREE_CODE (TREE_TYPE (arg
));
10615 /* This function validates the types of a function call argument list
10616 against a specified list of tree_codes. If the last specifier is a 0,
10617 that represents an ellipses, otherwise the last specifier must be a
10620 This is the GIMPLE version of validate_arglist. Eventually we want to
10621 completely convert builtins.c to work from GIMPLEs and the tree based
10622 validate_arglist will then be removed. */
10625 validate_gimple_arglist (const_gimple call
, ...)
10627 enum tree_code code
;
10633 va_start (ap
, call
);
10638 code
= (enum tree_code
) va_arg (ap
, int);
10642 /* This signifies an ellipses, any further arguments are all ok. */
10646 /* This signifies an endlink, if no arguments remain, return
10647 true, otherwise return false. */
10648 res
= (i
== gimple_call_num_args (call
));
10651 /* If no parameters remain or the parameter's code does not
10652 match the specified code, return false. Otherwise continue
10653 checking any remaining arguments. */
10654 arg
= gimple_call_arg (call
, i
++);
10655 if (!validate_arg (arg
, code
))
10662 /* We need gotos here since we can only have one VA_CLOSE in a
10670 /* Default target-specific builtin expander that does nothing. */
10673 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
10674 rtx target ATTRIBUTE_UNUSED
,
10675 rtx subtarget ATTRIBUTE_UNUSED
,
10676 enum machine_mode mode ATTRIBUTE_UNUSED
,
10677 int ignore ATTRIBUTE_UNUSED
)
10682 /* Returns true is EXP represents data that would potentially reside
10683 in a readonly section. */
10686 readonly_data_expr (tree exp
)
10690 if (TREE_CODE (exp
) != ADDR_EXPR
)
10693 exp
= get_base_address (TREE_OPERAND (exp
, 0));
10697 /* Make sure we call decl_readonly_section only for trees it
10698 can handle (since it returns true for everything it doesn't
10700 if (TREE_CODE (exp
) == STRING_CST
10701 || TREE_CODE (exp
) == CONSTRUCTOR
10702 || (TREE_CODE (exp
) == VAR_DECL
&& TREE_STATIC (exp
)))
10703 return decl_readonly_section (exp
, 0);
10708 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
10709 to the call, and TYPE is its return type.
10711 Return NULL_TREE if no simplification was possible, otherwise return the
10712 simplified form of the call as a tree.
10714 The simplified form may be a constant or other expression which
10715 computes the same value, but in a more efficient manner (including
10716 calls to other builtin functions).
10718 The call may contain arguments which need to be evaluated, but
10719 which are not useful to determine the result of the call. In
10720 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10721 COMPOUND_EXPR will be an argument which must be evaluated.
10722 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10723 COMPOUND_EXPR in the chain will contain the tree for the simplified
10724 form of the builtin function call. */
10727 fold_builtin_strstr (location_t loc
, tree s1
, tree s2
, tree type
)
10729 if (!validate_arg (s1
, POINTER_TYPE
)
10730 || !validate_arg (s2
, POINTER_TYPE
))
10735 const char *p1
, *p2
;
10737 p2
= c_getstr (s2
);
10741 p1
= c_getstr (s1
);
10744 const char *r
= strstr (p1
, p2
);
10748 return build_int_cst (TREE_TYPE (s1
), 0);
10750 /* Return an offset into the constant string argument. */
10751 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
10752 return fold_convert_loc (loc
, type
, tem
);
10755 /* The argument is const char *, and the result is char *, so we need
10756 a type conversion here to avoid a warning. */
10758 return fold_convert_loc (loc
, type
, s1
);
10763 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
10767 /* New argument list transforming strstr(s1, s2) to
10768 strchr(s1, s2[0]). */
10769 return build_call_expr_loc (loc
, fn
, 2, s1
,
10770 build_int_cst (integer_type_node
, p2
[0]));
10774 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
10775 the call, and TYPE is its return type.
10777 Return NULL_TREE if no simplification was possible, otherwise return the
10778 simplified form of the call as a tree.
10780 The simplified form may be a constant or other expression which
10781 computes the same value, but in a more efficient manner (including
10782 calls to other builtin functions).
10784 The call may contain arguments which need to be evaluated, but
10785 which are not useful to determine the result of the call. In
10786 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10787 COMPOUND_EXPR will be an argument which must be evaluated.
10788 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10789 COMPOUND_EXPR in the chain will contain the tree for the simplified
10790 form of the builtin function call. */
10793 fold_builtin_strchr (location_t loc
, tree s1
, tree s2
, tree type
)
10795 if (!validate_arg (s1
, POINTER_TYPE
)
10796 || !validate_arg (s2
, INTEGER_TYPE
))
10802 if (TREE_CODE (s2
) != INTEGER_CST
)
10805 p1
= c_getstr (s1
);
10812 if (target_char_cast (s2
, &c
))
10815 r
= strchr (p1
, c
);
10818 return build_int_cst (TREE_TYPE (s1
), 0);
10820 /* Return an offset into the constant string argument. */
10821 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
10822 return fold_convert_loc (loc
, type
, tem
);
10828 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
10829 the call, and TYPE is its return type.
10831 Return NULL_TREE if no simplification was possible, otherwise return the
10832 simplified form of the call as a tree.
10834 The simplified form may be a constant or other expression which
10835 computes the same value, but in a more efficient manner (including
10836 calls to other builtin functions).
10838 The call may contain arguments which need to be evaluated, but
10839 which are not useful to determine the result of the call. In
10840 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10841 COMPOUND_EXPR will be an argument which must be evaluated.
10842 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10843 COMPOUND_EXPR in the chain will contain the tree for the simplified
10844 form of the builtin function call. */
10847 fold_builtin_strrchr (location_t loc
, tree s1
, tree s2
, tree type
)
10849 if (!validate_arg (s1
, POINTER_TYPE
)
10850 || !validate_arg (s2
, INTEGER_TYPE
))
10857 if (TREE_CODE (s2
) != INTEGER_CST
)
10860 p1
= c_getstr (s1
);
10867 if (target_char_cast (s2
, &c
))
10870 r
= strrchr (p1
, c
);
10873 return build_int_cst (TREE_TYPE (s1
), 0);
10875 /* Return an offset into the constant string argument. */
10876 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
10877 return fold_convert_loc (loc
, type
, tem
);
10880 if (! integer_zerop (s2
))
10883 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
10887 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
10888 return build_call_expr_loc (loc
, fn
, 2, s1
, s2
);
10892 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
10893 to the call, and TYPE is its return type.
10895 Return NULL_TREE if no simplification was possible, otherwise return the
10896 simplified form of the call as a tree.
10898 The simplified form may be a constant or other expression which
10899 computes the same value, but in a more efficient manner (including
10900 calls to other builtin functions).
10902 The call may contain arguments which need to be evaluated, but
10903 which are not useful to determine the result of the call. In
10904 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10905 COMPOUND_EXPR will be an argument which must be evaluated.
10906 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10907 COMPOUND_EXPR in the chain will contain the tree for the simplified
10908 form of the builtin function call. */
10911 fold_builtin_strpbrk (location_t loc
, tree s1
, tree s2
, tree type
)
10913 if (!validate_arg (s1
, POINTER_TYPE
)
10914 || !validate_arg (s2
, POINTER_TYPE
))
10919 const char *p1
, *p2
;
10921 p2
= c_getstr (s2
);
10925 p1
= c_getstr (s1
);
10928 const char *r
= strpbrk (p1
, p2
);
10932 return build_int_cst (TREE_TYPE (s1
), 0);
10934 /* Return an offset into the constant string argument. */
10935 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
10936 return fold_convert_loc (loc
, type
, tem
);
10940 /* strpbrk(x, "") == NULL.
10941 Evaluate and ignore s1 in case it had side-effects. */
10942 return omit_one_operand_loc (loc
, TREE_TYPE (s1
), integer_zero_node
, s1
);
10945 return NULL_TREE
; /* Really call strpbrk. */
10947 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
10951 /* New argument list transforming strpbrk(s1, s2) to
10952 strchr(s1, s2[0]). */
10953 return build_call_expr_loc (loc
, fn
, 2, s1
,
10954 build_int_cst (integer_type_node
, p2
[0]));
10958 /* Simplify a call to the strncat builtin. DST, SRC, and LEN are the
10959 arguments to the call.
10961 Return NULL_TREE if no simplification was possible, otherwise return the
10962 simplified form of the call as a tree.
10964 The simplified form may be a constant or other expression which
10965 computes the same value, but in a more efficient manner (including
10966 calls to other builtin functions).
10968 The call may contain arguments which need to be evaluated, but
10969 which are not useful to determine the result of the call. In
10970 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10971 COMPOUND_EXPR will be an argument which must be evaluated.
10972 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10973 COMPOUND_EXPR in the chain will contain the tree for the simplified
10974 form of the builtin function call. */
10977 fold_builtin_strncat (location_t loc
, tree dst
, tree src
, tree len
)
10979 if (!validate_arg (dst
, POINTER_TYPE
)
10980 || !validate_arg (src
, POINTER_TYPE
)
10981 || !validate_arg (len
, INTEGER_TYPE
))
10985 const char *p
= c_getstr (src
);
10987 /* If the requested length is zero, or the src parameter string
10988 length is zero, return the dst parameter. */
10989 if (integer_zerop (len
) || (p
&& *p
== '\0'))
10990 return omit_two_operands_loc (loc
, TREE_TYPE (dst
), dst
, src
, len
);
10992 /* If the requested len is greater than or equal to the string
10993 length, call strcat. */
10994 if (TREE_CODE (len
) == INTEGER_CST
&& p
10995 && compare_tree_int (len
, strlen (p
)) >= 0)
10997 tree fn
= builtin_decl_implicit (BUILT_IN_STRCAT
);
10999 /* If the replacement _DECL isn't initialized, don't do the
11004 return build_call_expr_loc (loc
, fn
, 2, dst
, src
);
11010 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
11013 Return NULL_TREE if no simplification was possible, otherwise return the
11014 simplified form of the call as a tree.
11016 The simplified form may be a constant or other expression which
11017 computes the same value, but in a more efficient manner (including
11018 calls to other builtin functions).
11020 The call may contain arguments which need to be evaluated, but
11021 which are not useful to determine the result of the call. In
11022 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11023 COMPOUND_EXPR will be an argument which must be evaluated.
11024 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11025 COMPOUND_EXPR in the chain will contain the tree for the simplified
11026 form of the builtin function call. */
11029 fold_builtin_strspn (location_t loc
, tree s1
, tree s2
)
11031 if (!validate_arg (s1
, POINTER_TYPE
)
11032 || !validate_arg (s2
, POINTER_TYPE
))
11036 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
11038 /* If both arguments are constants, evaluate at compile-time. */
11041 const size_t r
= strspn (p1
, p2
);
11042 return build_int_cst (size_type_node
, r
);
11045 /* If either argument is "", return NULL_TREE. */
11046 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
11047 /* Evaluate and ignore both arguments in case either one has
11049 return omit_two_operands_loc (loc
, size_type_node
, size_zero_node
,
11055 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
11058 Return NULL_TREE if no simplification was possible, otherwise return the
11059 simplified form of the call as a tree.
11061 The simplified form may be a constant or other expression which
11062 computes the same value, but in a more efficient manner (including
11063 calls to other builtin functions).
11065 The call may contain arguments which need to be evaluated, but
11066 which are not useful to determine the result of the call. In
11067 this case we return a chain of COMPOUND_EXPRs. The LHS of each
11068 COMPOUND_EXPR will be an argument which must be evaluated.
11069 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
11070 COMPOUND_EXPR in the chain will contain the tree for the simplified
11071 form of the builtin function call. */
11074 fold_builtin_strcspn (location_t loc
, tree s1
, tree s2
)
11076 if (!validate_arg (s1
, POINTER_TYPE
)
11077 || !validate_arg (s2
, POINTER_TYPE
))
11081 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
11083 /* If both arguments are constants, evaluate at compile-time. */
11086 const size_t r
= strcspn (p1
, p2
);
11087 return build_int_cst (size_type_node
, r
);
11090 /* If the first argument is "", return NULL_TREE. */
11091 if (p1
&& *p1
== '\0')
11093 /* Evaluate and ignore argument s2 in case it has
11095 return omit_one_operand_loc (loc
, size_type_node
,
11096 size_zero_node
, s2
);
11099 /* If the second argument is "", return __builtin_strlen(s1). */
11100 if (p2
&& *p2
== '\0')
11102 tree fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
11104 /* If the replacement _DECL isn't initialized, don't do the
11109 return build_call_expr_loc (loc
, fn
, 1, s1
);
11115 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
11116 produced. False otherwise. This is done so that we don't output the error
11117 or warning twice or three times. */
11120 fold_builtin_next_arg (tree exp
, bool va_start_p
)
11122 tree fntype
= TREE_TYPE (current_function_decl
);
11123 int nargs
= call_expr_nargs (exp
);
11125 /* There is good chance the current input_location points inside the
11126 definition of the va_start macro (perhaps on the token for
11127 builtin) in a system header, so warnings will not be emitted.
11128 Use the location in real source code. */
11129 source_location current_location
=
11130 linemap_unwind_to_first_non_reserved_loc (line_table
, input_location
,
11133 if (!stdarg_p (fntype
))
11135 error ("%<va_start%> used in function with fixed args");
11141 if (va_start_p
&& (nargs
!= 2))
11143 error ("wrong number of arguments to function %<va_start%>");
11146 arg
= CALL_EXPR_ARG (exp
, 1);
11148 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
11149 when we checked the arguments and if needed issued a warning. */
11154 /* Evidently an out of date version of <stdarg.h>; can't validate
11155 va_start's second argument, but can still work as intended. */
11156 warning_at (current_location
,
11158 "%<__builtin_next_arg%> called without an argument");
11161 else if (nargs
> 1)
11163 error ("wrong number of arguments to function %<__builtin_next_arg%>");
11166 arg
= CALL_EXPR_ARG (exp
, 0);
11169 if (TREE_CODE (arg
) == SSA_NAME
)
11170 arg
= SSA_NAME_VAR (arg
);
11172 /* We destructively modify the call to be __builtin_va_start (ap, 0)
11173 or __builtin_next_arg (0) the first time we see it, after checking
11174 the arguments and if needed issuing a warning. */
11175 if (!integer_zerop (arg
))
11177 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
11179 /* Strip off all nops for the sake of the comparison. This
11180 is not quite the same as STRIP_NOPS. It does more.
11181 We must also strip off INDIRECT_EXPR for C++ reference
11183 while (CONVERT_EXPR_P (arg
)
11184 || TREE_CODE (arg
) == INDIRECT_REF
)
11185 arg
= TREE_OPERAND (arg
, 0);
11186 if (arg
!= last_parm
)
11188 /* FIXME: Sometimes with the tree optimizers we can get the
11189 not the last argument even though the user used the last
11190 argument. We just warn and set the arg to be the last
11191 argument so that we will get wrong-code because of
11193 warning_at (current_location
,
11195 "second parameter of %<va_start%> not last named argument");
11198 /* Undefined by C99 7.15.1.4p4 (va_start):
11199 "If the parameter parmN is declared with the register storage
11200 class, with a function or array type, or with a type that is
11201 not compatible with the type that results after application of
11202 the default argument promotions, the behavior is undefined."
11204 else if (DECL_REGISTER (arg
))
11206 warning_at (current_location
,
11208 "undefined behaviour when second parameter of "
11209 "%<va_start%> is declared with %<register%> storage");
11212 /* We want to verify the second parameter just once before the tree
11213 optimizers are run and then avoid keeping it in the tree,
11214 as otherwise we could warn even for correct code like:
11215 void foo (int i, ...)
11216 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
11218 CALL_EXPR_ARG (exp
, 1) = integer_zero_node
;
11220 CALL_EXPR_ARG (exp
, 0) = integer_zero_node
;
11226 /* Expand a call EXP to __builtin_object_size. */
11229 expand_builtin_object_size (tree exp
)
11232 int object_size_type
;
11233 tree fndecl
= get_callee_fndecl (exp
);
11235 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
11237 error ("%Kfirst argument of %D must be a pointer, second integer constant",
11239 expand_builtin_trap ();
11243 ost
= CALL_EXPR_ARG (exp
, 1);
11246 if (TREE_CODE (ost
) != INTEGER_CST
11247 || tree_int_cst_sgn (ost
) < 0
11248 || compare_tree_int (ost
, 3) > 0)
11250 error ("%Klast argument of %D is not integer constant between 0 and 3",
11252 expand_builtin_trap ();
11256 object_size_type
= tree_to_shwi (ost
);
11258 return object_size_type
< 2 ? constm1_rtx
: const0_rtx
;
11261 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
11262 FCODE is the BUILT_IN_* to use.
11263 Return NULL_RTX if we failed; the caller should emit a normal call,
11264 otherwise try to get the result in TARGET, if convenient (and in
11265 mode MODE if that's convenient). */
11268 expand_builtin_memory_chk (tree exp
, rtx target
, enum machine_mode mode
,
11269 enum built_in_function fcode
)
11271 tree dest
, src
, len
, size
;
11273 if (!validate_arglist (exp
,
11275 fcode
== BUILT_IN_MEMSET_CHK
11276 ? INTEGER_TYPE
: POINTER_TYPE
,
11277 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
11280 dest
= CALL_EXPR_ARG (exp
, 0);
11281 src
= CALL_EXPR_ARG (exp
, 1);
11282 len
= CALL_EXPR_ARG (exp
, 2);
11283 size
= CALL_EXPR_ARG (exp
, 3);
11285 if (! tree_fits_uhwi_p (size
))
11288 if (tree_fits_uhwi_p (len
) || integer_all_onesp (size
))
11292 if (! integer_all_onesp (size
) && tree_int_cst_lt (size
, len
))
11294 warning_at (tree_nonartificial_location (exp
),
11295 0, "%Kcall to %D will always overflow destination buffer",
11296 exp
, get_callee_fndecl (exp
));
11301 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
11302 mem{cpy,pcpy,move,set} is available. */
11305 case BUILT_IN_MEMCPY_CHK
:
11306 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
11308 case BUILT_IN_MEMPCPY_CHK
:
11309 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
11311 case BUILT_IN_MEMMOVE_CHK
:
11312 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
11314 case BUILT_IN_MEMSET_CHK
:
11315 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
11324 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 3, dest
, src
, len
);
11325 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
11326 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
11327 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
11329 else if (fcode
== BUILT_IN_MEMSET_CHK
)
11333 unsigned int dest_align
= get_pointer_alignment (dest
);
11335 /* If DEST is not a pointer type, call the normal function. */
11336 if (dest_align
== 0)
11339 /* If SRC and DEST are the same (and not volatile), do nothing. */
11340 if (operand_equal_p (src
, dest
, 0))
11344 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
11346 /* Evaluate and ignore LEN in case it has side-effects. */
11347 expand_expr (len
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
11348 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
11351 expr
= fold_build_pointer_plus (dest
, len
);
11352 return expand_expr (expr
, target
, mode
, EXPAND_NORMAL
);
11355 /* __memmove_chk special case. */
11356 if (fcode
== BUILT_IN_MEMMOVE_CHK
)
11358 unsigned int src_align
= get_pointer_alignment (src
);
11360 if (src_align
== 0)
11363 /* If src is categorized for a readonly section we can use
11364 normal __memcpy_chk. */
11365 if (readonly_data_expr (src
))
11367 tree fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
11370 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 4,
11371 dest
, src
, len
, size
);
11372 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
11373 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
11374 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
11381 /* Emit warning if a buffer overflow is detected at compile time. */
11384 maybe_emit_chk_warning (tree exp
, enum built_in_function fcode
)
11388 location_t loc
= tree_nonartificial_location (exp
);
11392 case BUILT_IN_STRCPY_CHK
:
11393 case BUILT_IN_STPCPY_CHK
:
11394 /* For __strcat_chk the warning will be emitted only if overflowing
11395 by at least strlen (dest) + 1 bytes. */
11396 case BUILT_IN_STRCAT_CHK
:
11397 len
= CALL_EXPR_ARG (exp
, 1);
11398 size
= CALL_EXPR_ARG (exp
, 2);
11401 case BUILT_IN_STRNCAT_CHK
:
11402 case BUILT_IN_STRNCPY_CHK
:
11403 case BUILT_IN_STPNCPY_CHK
:
11404 len
= CALL_EXPR_ARG (exp
, 2);
11405 size
= CALL_EXPR_ARG (exp
, 3);
11407 case BUILT_IN_SNPRINTF_CHK
:
11408 case BUILT_IN_VSNPRINTF_CHK
:
11409 len
= CALL_EXPR_ARG (exp
, 1);
11410 size
= CALL_EXPR_ARG (exp
, 3);
11413 gcc_unreachable ();
11419 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
11424 len
= c_strlen (len
, 1);
11425 if (! len
|| ! tree_fits_uhwi_p (len
) || tree_int_cst_lt (len
, size
))
11428 else if (fcode
== BUILT_IN_STRNCAT_CHK
)
11430 tree src
= CALL_EXPR_ARG (exp
, 1);
11431 if (! src
|| ! tree_fits_uhwi_p (len
) || tree_int_cst_lt (len
, size
))
11433 src
= c_strlen (src
, 1);
11434 if (! src
|| ! tree_fits_uhwi_p (src
))
11436 warning_at (loc
, 0, "%Kcall to %D might overflow destination buffer",
11437 exp
, get_callee_fndecl (exp
));
11440 else if (tree_int_cst_lt (src
, size
))
11443 else if (! tree_fits_uhwi_p (len
) || ! tree_int_cst_lt (size
, len
))
11446 warning_at (loc
, 0, "%Kcall to %D will always overflow destination buffer",
11447 exp
, get_callee_fndecl (exp
));
11450 /* Emit warning if a buffer overflow is detected at compile time
11451 in __sprintf_chk/__vsprintf_chk calls. */
11454 maybe_emit_sprintf_chk_warning (tree exp
, enum built_in_function fcode
)
11456 tree size
, len
, fmt
;
11457 const char *fmt_str
;
11458 int nargs
= call_expr_nargs (exp
);
11460 /* Verify the required arguments in the original call. */
11464 size
= CALL_EXPR_ARG (exp
, 2);
11465 fmt
= CALL_EXPR_ARG (exp
, 3);
11467 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
11470 /* Check whether the format is a literal string constant. */
11471 fmt_str
= c_getstr (fmt
);
11472 if (fmt_str
== NULL
)
11475 if (!init_target_chars ())
11478 /* If the format doesn't contain % args or %%, we know its size. */
11479 if (strchr (fmt_str
, target_percent
) == 0)
11480 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
11481 /* If the format is "%s" and first ... argument is a string literal,
11483 else if (fcode
== BUILT_IN_SPRINTF_CHK
11484 && strcmp (fmt_str
, target_percent_s
) == 0)
11490 arg
= CALL_EXPR_ARG (exp
, 4);
11491 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
11494 len
= c_strlen (arg
, 1);
11495 if (!len
|| ! tree_fits_uhwi_p (len
))
11501 if (! tree_int_cst_lt (len
, size
))
11502 warning_at (tree_nonartificial_location (exp
),
11503 0, "%Kcall to %D will always overflow destination buffer",
11504 exp
, get_callee_fndecl (exp
));
11507 /* Emit warning if a free is called with address of a variable. */
11510 maybe_emit_free_warning (tree exp
)
11512 tree arg
= CALL_EXPR_ARG (exp
, 0);
11515 if (TREE_CODE (arg
) != ADDR_EXPR
)
11518 arg
= get_base_address (TREE_OPERAND (arg
, 0));
11519 if (arg
== NULL
|| INDIRECT_REF_P (arg
) || TREE_CODE (arg
) == MEM_REF
)
11522 if (SSA_VAR_P (arg
))
11523 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
11524 "%Kattempt to free a non-heap object %qD", exp
, arg
);
11526 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
11527 "%Kattempt to free a non-heap object", exp
);
11530 /* Fold a call to __builtin_object_size with arguments PTR and OST,
11534 fold_builtin_object_size (tree ptr
, tree ost
)
11536 unsigned HOST_WIDE_INT bytes
;
11537 int object_size_type
;
11539 if (!validate_arg (ptr
, POINTER_TYPE
)
11540 || !validate_arg (ost
, INTEGER_TYPE
))
11545 if (TREE_CODE (ost
) != INTEGER_CST
11546 || tree_int_cst_sgn (ost
) < 0
11547 || compare_tree_int (ost
, 3) > 0)
11550 object_size_type
= tree_to_shwi (ost
);
11552 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
11553 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
11554 and (size_t) 0 for types 2 and 3. */
11555 if (TREE_SIDE_EFFECTS (ptr
))
11556 return build_int_cst_type (size_type_node
, object_size_type
< 2 ? -1 : 0);
11558 if (TREE_CODE (ptr
) == ADDR_EXPR
)
11560 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
11561 if (wi::fits_to_tree_p (bytes
, size_type_node
))
11562 return build_int_cstu (size_type_node
, bytes
);
11564 else if (TREE_CODE (ptr
) == SSA_NAME
)
11566 /* If object size is not known yet, delay folding until
11567 later. Maybe subsequent passes will help determining
11569 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
11570 if (bytes
!= (unsigned HOST_WIDE_INT
) (object_size_type
< 2 ? -1 : 0)
11571 && wi::fits_to_tree_p (bytes
, size_type_node
))
11572 return build_int_cstu (size_type_node
, bytes
);
11578 /* Fold a call to the __strcat_chk builtin FNDECL. DEST, SRC, and SIZE
11579 are the arguments to the call. */
11582 fold_builtin_strcat_chk (location_t loc
, tree fndecl
, tree dest
,
11583 tree src
, tree size
)
11588 if (!validate_arg (dest
, POINTER_TYPE
)
11589 || !validate_arg (src
, POINTER_TYPE
)
11590 || !validate_arg (size
, INTEGER_TYPE
))
11593 p
= c_getstr (src
);
11594 /* If the SRC parameter is "", return DEST. */
11595 if (p
&& *p
== '\0')
11596 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
11598 if (! tree_fits_uhwi_p (size
) || ! integer_all_onesp (size
))
11601 /* If __builtin_strcat_chk is used, assume strcat is available. */
11602 fn
= builtin_decl_explicit (BUILT_IN_STRCAT
);
11606 return build_call_expr_loc (loc
, fn
, 2, dest
, src
);
11609 /* Fold a call to the __strncat_chk builtin with arguments DEST, SRC,
11613 fold_builtin_strncat_chk (location_t loc
, tree fndecl
,
11614 tree dest
, tree src
, tree len
, tree size
)
11619 if (!validate_arg (dest
, POINTER_TYPE
)
11620 || !validate_arg (src
, POINTER_TYPE
)
11621 || !validate_arg (size
, INTEGER_TYPE
)
11622 || !validate_arg (size
, INTEGER_TYPE
))
11625 p
= c_getstr (src
);
11626 /* If the SRC parameter is "" or if LEN is 0, return DEST. */
11627 if (p
&& *p
== '\0')
11628 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, len
);
11629 else if (integer_zerop (len
))
11630 return omit_one_operand_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), dest
, src
);
11632 if (! tree_fits_uhwi_p (size
))
11635 if (! integer_all_onesp (size
))
11637 tree src_len
= c_strlen (src
, 1);
11639 && tree_fits_uhwi_p (src_len
)
11640 && tree_fits_uhwi_p (len
)
11641 && ! tree_int_cst_lt (len
, src_len
))
11643 /* If LEN >= strlen (SRC), optimize into __strcat_chk. */
11644 fn
= builtin_decl_explicit (BUILT_IN_STRCAT_CHK
);
11648 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, size
);
11653 /* If __builtin_strncat_chk is used, assume strncat is available. */
11654 fn
= builtin_decl_explicit (BUILT_IN_STRNCAT
);
11658 return build_call_expr_loc (loc
, fn
, 3, dest
, src
, len
);
11661 /* Builtins with folding operations that operate on "..." arguments
11662 need special handling; we need to store the arguments in a convenient
11663 data structure before attempting any folding. Fortunately there are
11664 only a few builtins that fall into this category. FNDECL is the
11665 function, EXP is the CALL_EXPR for the call, and IGNORE is true if the
11666 result of the function call is ignored. */
11669 fold_builtin_varargs (location_t loc
, tree fndecl
, tree exp
,
11670 bool ignore ATTRIBUTE_UNUSED
)
11672 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
11673 tree ret
= NULL_TREE
;
11677 case BUILT_IN_FPCLASSIFY
:
11678 ret
= fold_builtin_fpclassify (loc
, exp
);
11686 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
11687 SET_EXPR_LOCATION (ret
, loc
);
11688 TREE_NO_WARNING (ret
) = 1;
11694 /* Fold a call to the {,v}printf{,_unlocked} and __{,v}printf_chk builtins.
11695 FMT and ARG are the arguments to the call; we don't fold cases with
11696 more than 2 arguments, and ARG may be null if this is a 1-argument case.
11698 Return NULL_TREE if no simplification was possible, otherwise return the
11699 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11700 code of the function to be simplified. */
11703 fold_builtin_printf (location_t loc
, tree fndecl
, tree fmt
,
11704 tree arg
, bool ignore
,
11705 enum built_in_function fcode
)
11707 tree fn_putchar
, fn_puts
, newarg
, call
= NULL_TREE
;
11708 const char *fmt_str
= NULL
;
11710 /* If the return value is used, don't do the transformation. */
11714 /* Verify the required arguments in the original call. */
11715 if (!validate_arg (fmt
, POINTER_TYPE
))
11718 /* Check whether the format is a literal string constant. */
11719 fmt_str
= c_getstr (fmt
);
11720 if (fmt_str
== NULL
)
11723 if (fcode
== BUILT_IN_PRINTF_UNLOCKED
)
11725 /* If we're using an unlocked function, assume the other
11726 unlocked functions exist explicitly. */
11727 fn_putchar
= builtin_decl_explicit (BUILT_IN_PUTCHAR_UNLOCKED
);
11728 fn_puts
= builtin_decl_explicit (BUILT_IN_PUTS_UNLOCKED
);
11732 fn_putchar
= builtin_decl_implicit (BUILT_IN_PUTCHAR
);
11733 fn_puts
= builtin_decl_implicit (BUILT_IN_PUTS
);
11736 if (!init_target_chars ())
11739 if (strcmp (fmt_str
, target_percent_s
) == 0
11740 || strchr (fmt_str
, target_percent
) == NULL
)
11744 if (strcmp (fmt_str
, target_percent_s
) == 0)
11746 if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
11749 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
11752 str
= c_getstr (arg
);
11758 /* The format specifier doesn't contain any '%' characters. */
11759 if (fcode
!= BUILT_IN_VPRINTF
&& fcode
!= BUILT_IN_VPRINTF_CHK
11765 /* If the string was "", printf does nothing. */
11766 if (str
[0] == '\0')
11767 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
11769 /* If the string has length of 1, call putchar. */
11770 if (str
[1] == '\0')
11772 /* Given printf("c"), (where c is any one character,)
11773 convert "c"[0] to an int and pass that to the replacement
11775 newarg
= build_int_cst (integer_type_node
, str
[0]);
11777 call
= build_call_expr_loc (loc
, fn_putchar
, 1, newarg
);
11781 /* If the string was "string\n", call puts("string"). */
11782 size_t len
= strlen (str
);
11783 if ((unsigned char)str
[len
- 1] == target_newline
11784 && (size_t) (int) len
== len
11788 tree offset_node
, string_cst
;
11790 /* Create a NUL-terminated string that's one char shorter
11791 than the original, stripping off the trailing '\n'. */
11792 newarg
= build_string_literal (len
, str
);
11793 string_cst
= string_constant (newarg
, &offset_node
);
11794 gcc_checking_assert (string_cst
11795 && (TREE_STRING_LENGTH (string_cst
)
11797 && integer_zerop (offset_node
)
11799 TREE_STRING_POINTER (string_cst
)[len
- 1]
11800 == target_newline
);
11801 /* build_string_literal creates a new STRING_CST,
11802 modify it in place to avoid double copying. */
11803 newstr
= CONST_CAST (char *, TREE_STRING_POINTER (string_cst
));
11804 newstr
[len
- 1] = '\0';
11806 call
= build_call_expr_loc (loc
, fn_puts
, 1, newarg
);
11809 /* We'd like to arrange to call fputs(string,stdout) here,
11810 but we need stdout and don't have a way to get it yet. */
11815 /* The other optimizations can be done only on the non-va_list variants. */
11816 else if (fcode
== BUILT_IN_VPRINTF
|| fcode
== BUILT_IN_VPRINTF_CHK
)
11819 /* If the format specifier was "%s\n", call __builtin_puts(arg). */
11820 else if (strcmp (fmt_str
, target_percent_s_newline
) == 0)
11822 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
11825 call
= build_call_expr_loc (loc
, fn_puts
, 1, arg
);
11828 /* If the format specifier was "%c", call __builtin_putchar(arg). */
11829 else if (strcmp (fmt_str
, target_percent_c
) == 0)
11831 if (!arg
|| !validate_arg (arg
, INTEGER_TYPE
))
11834 call
= build_call_expr_loc (loc
, fn_putchar
, 1, arg
);
11840 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), call
);
11843 /* Fold a call to the {,v}fprintf{,_unlocked} and __{,v}printf_chk builtins.
11844 FP, FMT, and ARG are the arguments to the call. We don't fold calls with
11845 more than 3 arguments, and ARG may be null in the 2-argument case.
11847 Return NULL_TREE if no simplification was possible, otherwise return the
11848 simplified form of the call as a tree. FCODE is the BUILT_IN_*
11849 code of the function to be simplified. */
11852 fold_builtin_fprintf (location_t loc
, tree fndecl
, tree fp
,
11853 tree fmt
, tree arg
, bool ignore
,
11854 enum built_in_function fcode
)
11856 tree fn_fputc
, fn_fputs
, call
= NULL_TREE
;
11857 const char *fmt_str
= NULL
;
11859 /* If the return value is used, don't do the transformation. */
11863 /* Verify the required arguments in the original call. */
11864 if (!validate_arg (fp
, POINTER_TYPE
))
11866 if (!validate_arg (fmt
, POINTER_TYPE
))
11869 /* Check whether the format is a literal string constant. */
11870 fmt_str
= c_getstr (fmt
);
11871 if (fmt_str
== NULL
)
11874 if (fcode
== BUILT_IN_FPRINTF_UNLOCKED
)
11876 /* If we're using an unlocked function, assume the other
11877 unlocked functions exist explicitly. */
11878 fn_fputc
= builtin_decl_explicit (BUILT_IN_FPUTC_UNLOCKED
);
11879 fn_fputs
= builtin_decl_explicit (BUILT_IN_FPUTS_UNLOCKED
);
11883 fn_fputc
= builtin_decl_implicit (BUILT_IN_FPUTC
);
11884 fn_fputs
= builtin_decl_implicit (BUILT_IN_FPUTS
);
11887 if (!init_target_chars ())
11890 /* If the format doesn't contain % args or %%, use strcpy. */
11891 if (strchr (fmt_str
, target_percent
) == NULL
)
11893 if (fcode
!= BUILT_IN_VFPRINTF
&& fcode
!= BUILT_IN_VFPRINTF_CHK
11897 /* If the format specifier was "", fprintf does nothing. */
11898 if (fmt_str
[0] == '\0')
11900 /* If FP has side-effects, just wait until gimplification is
11902 if (TREE_SIDE_EFFECTS (fp
))
11905 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
11908 /* When "string" doesn't contain %, replace all cases of
11909 fprintf (fp, string) with fputs (string, fp). The fputs
11910 builtin will take care of special cases like length == 1. */
11912 call
= build_call_expr_loc (loc
, fn_fputs
, 2, fmt
, fp
);
11915 /* The other optimizations can be done only on the non-va_list variants. */
11916 else if (fcode
== BUILT_IN_VFPRINTF
|| fcode
== BUILT_IN_VFPRINTF_CHK
)
11919 /* If the format specifier was "%s", call __builtin_fputs (arg, fp). */
11920 else if (strcmp (fmt_str
, target_percent_s
) == 0)
11922 if (!arg
|| !validate_arg (arg
, POINTER_TYPE
))
11925 call
= build_call_expr_loc (loc
, fn_fputs
, 2, arg
, fp
);
11928 /* If the format specifier was "%c", call __builtin_fputc (arg, fp). */
11929 else if (strcmp (fmt_str
, target_percent_c
) == 0)
11931 if (!arg
|| !validate_arg (arg
, INTEGER_TYPE
))
11934 call
= build_call_expr_loc (loc
, fn_fputc
, 2, arg
, fp
);
11939 return fold_convert_loc (loc
, TREE_TYPE (TREE_TYPE (fndecl
)), call
);
11942 /* Initialize format string characters in the target charset. */
11945 init_target_chars (void)
11950 target_newline
= lang_hooks
.to_target_charset ('\n');
11951 target_percent
= lang_hooks
.to_target_charset ('%');
11952 target_c
= lang_hooks
.to_target_charset ('c');
11953 target_s
= lang_hooks
.to_target_charset ('s');
11954 if (target_newline
== 0 || target_percent
== 0 || target_c
== 0
11958 target_percent_c
[0] = target_percent
;
11959 target_percent_c
[1] = target_c
;
11960 target_percent_c
[2] = '\0';
11962 target_percent_s
[0] = target_percent
;
11963 target_percent_s
[1] = target_s
;
11964 target_percent_s
[2] = '\0';
11966 target_percent_s_newline
[0] = target_percent
;
11967 target_percent_s_newline
[1] = target_s
;
11968 target_percent_s_newline
[2] = target_newline
;
11969 target_percent_s_newline
[3] = '\0';
11976 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
11977 and no overflow/underflow occurred. INEXACT is true if M was not
11978 exactly calculated. TYPE is the tree type for the result. This
11979 function assumes that you cleared the MPFR flags and then
11980 calculated M to see if anything subsequently set a flag prior to
11981 entering this function. Return NULL_TREE if any checks fail. */
11984 do_mpfr_ckconv (mpfr_srcptr m
, tree type
, int inexact
)
11986 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
11987 overflow/underflow occurred. If -frounding-math, proceed iff the
11988 result of calling FUNC was exact. */
11989 if (mpfr_number_p (m
) && !mpfr_overflow_p () && !mpfr_underflow_p ()
11990 && (!flag_rounding_math
|| !inexact
))
11992 REAL_VALUE_TYPE rr
;
11994 real_from_mpfr (&rr
, m
, type
, GMP_RNDN
);
11995 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
11996 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11997 but the mpft_t is not, then we underflowed in the
11999 if (real_isfinite (&rr
)
12000 && (rr
.cl
== rvc_zero
) == (mpfr_zero_p (m
) != 0))
12002 REAL_VALUE_TYPE rmode
;
12004 real_convert (&rmode
, TYPE_MODE (type
), &rr
);
12005 /* Proceed iff the specified mode can hold the value. */
12006 if (real_identical (&rmode
, &rr
))
12007 return build_real (type
, rmode
);
12013 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
12014 number and no overflow/underflow occurred. INEXACT is true if M
12015 was not exactly calculated. TYPE is the tree type for the result.
12016 This function assumes that you cleared the MPFR flags and then
12017 calculated M to see if anything subsequently set a flag prior to
12018 entering this function. Return NULL_TREE if any checks fail, if
12019 FORCE_CONVERT is true, then bypass the checks. */
12022 do_mpc_ckconv (mpc_srcptr m
, tree type
, int inexact
, int force_convert
)
12024 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
12025 overflow/underflow occurred. If -frounding-math, proceed iff the
12026 result of calling FUNC was exact. */
12028 || (mpfr_number_p (mpc_realref (m
)) && mpfr_number_p (mpc_imagref (m
))
12029 && !mpfr_overflow_p () && !mpfr_underflow_p ()
12030 && (!flag_rounding_math
|| !inexact
)))
12032 REAL_VALUE_TYPE re
, im
;
12034 real_from_mpfr (&re
, mpc_realref (m
), TREE_TYPE (type
), GMP_RNDN
);
12035 real_from_mpfr (&im
, mpc_imagref (m
), TREE_TYPE (type
), GMP_RNDN
);
12036 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
12037 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
12038 but the mpft_t is not, then we underflowed in the
12041 || (real_isfinite (&re
) && real_isfinite (&im
)
12042 && (re
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_realref (m
)) != 0)
12043 && (im
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_imagref (m
)) != 0)))
12045 REAL_VALUE_TYPE re_mode
, im_mode
;
12047 real_convert (&re_mode
, TYPE_MODE (TREE_TYPE (type
)), &re
);
12048 real_convert (&im_mode
, TYPE_MODE (TREE_TYPE (type
)), &im
);
12049 /* Proceed iff the specified mode can hold the value. */
12051 || (real_identical (&re_mode
, &re
)
12052 && real_identical (&im_mode
, &im
)))
12053 return build_complex (type
, build_real (TREE_TYPE (type
), re_mode
),
12054 build_real (TREE_TYPE (type
), im_mode
));
12060 /* If argument ARG is a REAL_CST, call the one-argument mpfr function
12061 FUNC on it and return the resulting value as a tree with type TYPE.
12062 If MIN and/or MAX are not NULL, then the supplied ARG must be
12063 within those bounds. If INCLUSIVE is true, then MIN/MAX are
12064 acceptable values, otherwise they are not. The mpfr precision is
12065 set to the precision of TYPE. We assume that function FUNC returns
12066 zero if the result could be calculated exactly within the requested
12070 do_mpfr_arg1 (tree arg
, tree type
, int (*func
)(mpfr_ptr
, mpfr_srcptr
, mp_rnd_t
),
12071 const REAL_VALUE_TYPE
*min
, const REAL_VALUE_TYPE
*max
,
12074 tree result
= NULL_TREE
;
12078 /* To proceed, MPFR must exactly represent the target floating point
12079 format, which only happens when the target base equals two. */
12080 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12081 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
))
12083 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
12085 if (real_isfinite (ra
)
12086 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
))
12087 && (!max
|| real_compare (inclusive
? LE_EXPR
: LT_EXPR
, ra
, max
)))
12089 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12090 const int prec
= fmt
->p
;
12091 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12095 mpfr_init2 (m
, prec
);
12096 mpfr_from_real (m
, ra
, GMP_RNDN
);
12097 mpfr_clear_flags ();
12098 inexact
= func (m
, m
, rnd
);
12099 result
= do_mpfr_ckconv (m
, type
, inexact
);
12107 /* If argument ARG is a REAL_CST, call the two-argument mpfr function
12108 FUNC on it and return the resulting value as a tree with type TYPE.
12109 The mpfr precision is set to the precision of TYPE. We assume that
12110 function FUNC returns zero if the result could be calculated
12111 exactly within the requested precision. */
12114 do_mpfr_arg2 (tree arg1
, tree arg2
, tree type
,
12115 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
12117 tree result
= NULL_TREE
;
12122 /* To proceed, MPFR must exactly represent the target floating point
12123 format, which only happens when the target base equals two. */
12124 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12125 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
)
12126 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
))
12128 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
12129 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
12131 if (real_isfinite (ra1
) && real_isfinite (ra2
))
12133 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12134 const int prec
= fmt
->p
;
12135 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12139 mpfr_inits2 (prec
, m1
, m2
, NULL
);
12140 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
12141 mpfr_from_real (m2
, ra2
, GMP_RNDN
);
12142 mpfr_clear_flags ();
12143 inexact
= func (m1
, m1
, m2
, rnd
);
12144 result
= do_mpfr_ckconv (m1
, type
, inexact
);
12145 mpfr_clears (m1
, m2
, NULL
);
12152 /* If argument ARG is a REAL_CST, call the three-argument mpfr function
12153 FUNC on it and return the resulting value as a tree with type TYPE.
12154 The mpfr precision is set to the precision of TYPE. We assume that
12155 function FUNC returns zero if the result could be calculated
12156 exactly within the requested precision. */
12159 do_mpfr_arg3 (tree arg1
, tree arg2
, tree arg3
, tree type
,
12160 int (*func
)(mpfr_ptr
, mpfr_srcptr
, mpfr_srcptr
, mpfr_srcptr
, mp_rnd_t
))
12162 tree result
= NULL_TREE
;
12168 /* To proceed, MPFR must exactly represent the target floating point
12169 format, which only happens when the target base equals two. */
12170 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12171 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
)
12172 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
)
12173 && TREE_CODE (arg3
) == REAL_CST
&& !TREE_OVERFLOW (arg3
))
12175 const REAL_VALUE_TYPE
*const ra1
= &TREE_REAL_CST (arg1
);
12176 const REAL_VALUE_TYPE
*const ra2
= &TREE_REAL_CST (arg2
);
12177 const REAL_VALUE_TYPE
*const ra3
= &TREE_REAL_CST (arg3
);
12179 if (real_isfinite (ra1
) && real_isfinite (ra2
) && real_isfinite (ra3
))
12181 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12182 const int prec
= fmt
->p
;
12183 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12187 mpfr_inits2 (prec
, m1
, m2
, m3
, NULL
);
12188 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
12189 mpfr_from_real (m2
, ra2
, GMP_RNDN
);
12190 mpfr_from_real (m3
, ra3
, GMP_RNDN
);
12191 mpfr_clear_flags ();
12192 inexact
= func (m1
, m1
, m2
, m3
, rnd
);
12193 result
= do_mpfr_ckconv (m1
, type
, inexact
);
12194 mpfr_clears (m1
, m2
, m3
, NULL
);
12201 /* If argument ARG is a REAL_CST, call mpfr_sin_cos() on it and set
12202 the pointers *(ARG_SINP) and *(ARG_COSP) to the resulting values.
12203 If ARG_SINP and ARG_COSP are NULL then the result is returned
12204 as a complex value.
12205 The type is taken from the type of ARG and is used for setting the
12206 precision of the calculation and results. */
12209 do_mpfr_sincos (tree arg
, tree arg_sinp
, tree arg_cosp
)
12211 tree
const type
= TREE_TYPE (arg
);
12212 tree result
= NULL_TREE
;
12216 /* To proceed, MPFR must exactly represent the target floating point
12217 format, which only happens when the target base equals two. */
12218 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12219 && TREE_CODE (arg
) == REAL_CST
12220 && !TREE_OVERFLOW (arg
))
12222 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg
);
12224 if (real_isfinite (ra
))
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
;
12229 tree result_s
, result_c
;
12233 mpfr_inits2 (prec
, m
, ms
, mc
, NULL
);
12234 mpfr_from_real (m
, ra
, GMP_RNDN
);
12235 mpfr_clear_flags ();
12236 inexact
= mpfr_sin_cos (ms
, mc
, m
, rnd
);
12237 result_s
= do_mpfr_ckconv (ms
, type
, inexact
);
12238 result_c
= do_mpfr_ckconv (mc
, type
, inexact
);
12239 mpfr_clears (m
, ms
, mc
, NULL
);
12240 if (result_s
&& result_c
)
12242 /* If we are to return in a complex value do so. */
12243 if (!arg_sinp
&& !arg_cosp
)
12244 return build_complex (build_complex_type (type
),
12245 result_c
, result_s
);
12247 /* Dereference the sin/cos pointer arguments. */
12248 arg_sinp
= build_fold_indirect_ref (arg_sinp
);
12249 arg_cosp
= build_fold_indirect_ref (arg_cosp
);
12250 /* Proceed if valid pointer type were passed in. */
12251 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_sinp
)) == TYPE_MAIN_VARIANT (type
)
12252 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_cosp
)) == TYPE_MAIN_VARIANT (type
))
12254 /* Set the values. */
12255 result_s
= fold_build2 (MODIFY_EXPR
, type
, arg_sinp
,
12257 TREE_SIDE_EFFECTS (result_s
) = 1;
12258 result_c
= fold_build2 (MODIFY_EXPR
, type
, arg_cosp
,
12260 TREE_SIDE_EFFECTS (result_c
) = 1;
12261 /* Combine the assignments into a compound expr. */
12262 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
12263 result_s
, result_c
));
12271 /* If argument ARG1 is an INTEGER_CST and ARG2 is a REAL_CST, call the
12272 two-argument mpfr order N Bessel function FUNC on them and return
12273 the resulting value as a tree with type TYPE. The mpfr precision
12274 is set to the precision of TYPE. We assume that function FUNC
12275 returns zero if the result could be calculated exactly within the
12276 requested precision. */
12278 do_mpfr_bessel_n (tree arg1
, tree arg2
, tree type
,
12279 int (*func
)(mpfr_ptr
, long, mpfr_srcptr
, mp_rnd_t
),
12280 const REAL_VALUE_TYPE
*min
, bool inclusive
)
12282 tree result
= NULL_TREE
;
12287 /* To proceed, MPFR must exactly represent the target floating point
12288 format, which only happens when the target base equals two. */
12289 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12290 && tree_fits_shwi_p (arg1
)
12291 && TREE_CODE (arg2
) == REAL_CST
&& !TREE_OVERFLOW (arg2
))
12293 const HOST_WIDE_INT n
= tree_to_shwi (arg1
);
12294 const REAL_VALUE_TYPE
*const ra
= &TREE_REAL_CST (arg2
);
12297 && real_isfinite (ra
)
12298 && (!min
|| real_compare (inclusive
? GE_EXPR
: GT_EXPR
, ra
, min
)))
12300 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12301 const int prec
= fmt
->p
;
12302 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12306 mpfr_init2 (m
, prec
);
12307 mpfr_from_real (m
, ra
, GMP_RNDN
);
12308 mpfr_clear_flags ();
12309 inexact
= func (m
, n
, m
, rnd
);
12310 result
= do_mpfr_ckconv (m
, type
, inexact
);
12318 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
12319 the pointer *(ARG_QUO) and return the result. The type is taken
12320 from the type of ARG0 and is used for setting the precision of the
12321 calculation and results. */
12324 do_mpfr_remquo (tree arg0
, tree arg1
, tree arg_quo
)
12326 tree
const type
= TREE_TYPE (arg0
);
12327 tree result
= NULL_TREE
;
12332 /* To proceed, MPFR must exactly represent the target floating point
12333 format, which only happens when the target base equals two. */
12334 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12335 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
12336 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
))
12338 const REAL_VALUE_TYPE
*const ra0
= TREE_REAL_CST_PTR (arg0
);
12339 const REAL_VALUE_TYPE
*const ra1
= TREE_REAL_CST_PTR (arg1
);
12341 if (real_isfinite (ra0
) && real_isfinite (ra1
))
12343 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12344 const int prec
= fmt
->p
;
12345 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12350 mpfr_inits2 (prec
, m0
, m1
, NULL
);
12351 mpfr_from_real (m0
, ra0
, GMP_RNDN
);
12352 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
12353 mpfr_clear_flags ();
12354 mpfr_remquo (m0
, &integer_quo
, m0
, m1
, rnd
);
12355 /* Remquo is independent of the rounding mode, so pass
12356 inexact=0 to do_mpfr_ckconv(). */
12357 result_rem
= do_mpfr_ckconv (m0
, type
, /*inexact=*/ 0);
12358 mpfr_clears (m0
, m1
, NULL
);
12361 /* MPFR calculates quo in the host's long so it may
12362 return more bits in quo than the target int can hold
12363 if sizeof(host long) > sizeof(target int). This can
12364 happen even for native compilers in LP64 mode. In
12365 these cases, modulo the quo value with the largest
12366 number that the target int can hold while leaving one
12367 bit for the sign. */
12368 if (sizeof (integer_quo
) * CHAR_BIT
> INT_TYPE_SIZE
)
12369 integer_quo
%= (long)(1UL << (INT_TYPE_SIZE
- 1));
12371 /* Dereference the quo pointer argument. */
12372 arg_quo
= build_fold_indirect_ref (arg_quo
);
12373 /* Proceed iff a valid pointer type was passed in. */
12374 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo
)) == integer_type_node
)
12376 /* Set the value. */
12378 = fold_build2 (MODIFY_EXPR
, TREE_TYPE (arg_quo
), arg_quo
,
12379 build_int_cst (TREE_TYPE (arg_quo
),
12381 TREE_SIDE_EFFECTS (result_quo
) = 1;
12382 /* Combine the quo assignment with the rem. */
12383 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
12384 result_quo
, result_rem
));
12392 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
12393 resulting value as a tree with type TYPE. The mpfr precision is
12394 set to the precision of TYPE. We assume that this mpfr function
12395 returns zero if the result could be calculated exactly within the
12396 requested precision. In addition, the integer pointer represented
12397 by ARG_SG will be dereferenced and set to the appropriate signgam
12401 do_mpfr_lgamma_r (tree arg
, tree arg_sg
, tree type
)
12403 tree result
= NULL_TREE
;
12407 /* To proceed, MPFR must exactly represent the target floating point
12408 format, which only happens when the target base equals two. Also
12409 verify ARG is a constant and that ARG_SG is an int pointer. */
12410 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
12411 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
)
12412 && TREE_CODE (TREE_TYPE (arg_sg
)) == POINTER_TYPE
12413 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg
))) == integer_type_node
)
12415 const REAL_VALUE_TYPE
*const ra
= TREE_REAL_CST_PTR (arg
);
12417 /* In addition to NaN and Inf, the argument cannot be zero or a
12418 negative integer. */
12419 if (real_isfinite (ra
)
12420 && ra
->cl
!= rvc_zero
12421 && !(real_isneg (ra
) && real_isinteger (ra
, TYPE_MODE (type
))))
12423 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
12424 const int prec
= fmt
->p
;
12425 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12430 mpfr_init2 (m
, prec
);
12431 mpfr_from_real (m
, ra
, GMP_RNDN
);
12432 mpfr_clear_flags ();
12433 inexact
= mpfr_lgamma (m
, &sg
, m
, rnd
);
12434 result_lg
= do_mpfr_ckconv (m
, type
, inexact
);
12440 /* Dereference the arg_sg pointer argument. */
12441 arg_sg
= build_fold_indirect_ref (arg_sg
);
12442 /* Assign the signgam value into *arg_sg. */
12443 result_sg
= fold_build2 (MODIFY_EXPR
,
12444 TREE_TYPE (arg_sg
), arg_sg
,
12445 build_int_cst (TREE_TYPE (arg_sg
), sg
));
12446 TREE_SIDE_EFFECTS (result_sg
) = 1;
12447 /* Combine the signgam assignment with the lgamma result. */
12448 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
12449 result_sg
, result_lg
));
12457 /* If argument ARG is a COMPLEX_CST, call the one-argument mpc
12458 function FUNC on it and return the resulting value as a tree with
12459 type TYPE. The mpfr precision is set to the precision of TYPE. We
12460 assume that function FUNC returns zero if the result could be
12461 calculated exactly within the requested precision. */
12464 do_mpc_arg1 (tree arg
, tree type
, int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_rnd_t
))
12466 tree result
= NULL_TREE
;
12470 /* To proceed, MPFR must exactly represent the target floating point
12471 format, which only happens when the target base equals two. */
12472 if (TREE_CODE (arg
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg
)
12473 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
12474 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg
))))->b
== 2)
12476 const REAL_VALUE_TYPE
*const re
= TREE_REAL_CST_PTR (TREE_REALPART (arg
));
12477 const REAL_VALUE_TYPE
*const im
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg
));
12479 if (real_isfinite (re
) && real_isfinite (im
))
12481 const struct real_format
*const fmt
=
12482 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
12483 const int prec
= fmt
->p
;
12484 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12485 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
12489 mpc_init2 (m
, prec
);
12490 mpfr_from_real (mpc_realref (m
), re
, rnd
);
12491 mpfr_from_real (mpc_imagref (m
), im
, rnd
);
12492 mpfr_clear_flags ();
12493 inexact
= func (m
, m
, crnd
);
12494 result
= do_mpc_ckconv (m
, type
, inexact
, /*force_convert=*/ 0);
12502 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
12503 mpc function FUNC on it and return the resulting value as a tree
12504 with type TYPE. The mpfr precision is set to the precision of
12505 TYPE. We assume that function FUNC returns zero if the result
12506 could be calculated exactly within the requested precision. If
12507 DO_NONFINITE is true, then fold expressions containing Inf or NaN
12508 in the arguments and/or results. */
12511 do_mpc_arg2 (tree arg0
, tree arg1
, tree type
, int do_nonfinite
,
12512 int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_srcptr
, mpc_rnd_t
))
12514 tree result
= NULL_TREE
;
12519 /* To proceed, MPFR must exactly represent the target floating point
12520 format, which only happens when the target base equals two. */
12521 if (TREE_CODE (arg0
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg0
)
12522 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
12523 && TREE_CODE (arg1
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg1
)
12524 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
12525 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0
))))->b
== 2)
12527 const REAL_VALUE_TYPE
*const re0
= TREE_REAL_CST_PTR (TREE_REALPART (arg0
));
12528 const REAL_VALUE_TYPE
*const im0
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg0
));
12529 const REAL_VALUE_TYPE
*const re1
= TREE_REAL_CST_PTR (TREE_REALPART (arg1
));
12530 const REAL_VALUE_TYPE
*const im1
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg1
));
12533 || (real_isfinite (re0
) && real_isfinite (im0
)
12534 && real_isfinite (re1
) && real_isfinite (im1
)))
12536 const struct real_format
*const fmt
=
12537 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
12538 const int prec
= fmt
->p
;
12539 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
12540 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
12544 mpc_init2 (m0
, prec
);
12545 mpc_init2 (m1
, prec
);
12546 mpfr_from_real (mpc_realref (m0
), re0
, rnd
);
12547 mpfr_from_real (mpc_imagref (m0
), im0
, rnd
);
12548 mpfr_from_real (mpc_realref (m1
), re1
, rnd
);
12549 mpfr_from_real (mpc_imagref (m1
), im1
, rnd
);
12550 mpfr_clear_flags ();
12551 inexact
= func (m0
, m0
, m1
, crnd
);
12552 result
= do_mpc_ckconv (m0
, type
, inexact
, do_nonfinite
);
12561 /* A wrapper function for builtin folding that prevents warnings for
12562 "statement without effect" and the like, caused by removing the
12563 call node earlier than the warning is generated. */
12566 fold_call_stmt (gimple stmt
, bool ignore
)
12568 tree ret
= NULL_TREE
;
12569 tree fndecl
= gimple_call_fndecl (stmt
);
12570 location_t loc
= gimple_location (stmt
);
12572 && TREE_CODE (fndecl
) == FUNCTION_DECL
12573 && DECL_BUILT_IN (fndecl
)
12574 && !gimple_call_va_arg_pack_p (stmt
))
12576 int nargs
= gimple_call_num_args (stmt
);
12577 tree
*args
= (nargs
> 0
12578 ? gimple_call_arg_ptr (stmt
, 0)
12579 : &error_mark_node
);
12581 if (avoid_folding_inline_builtin (fndecl
))
12583 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
12585 return targetm
.fold_builtin (fndecl
, nargs
, args
, ignore
);
12589 if (nargs
<= MAX_ARGS_TO_FOLD_BUILTIN
)
12590 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
12593 /* Propagate location information from original call to
12594 expansion of builtin. Otherwise things like
12595 maybe_emit_chk_warning, that operate on the expansion
12596 of a builtin, will use the wrong location information. */
12597 if (gimple_has_location (stmt
))
12599 tree realret
= ret
;
12600 if (TREE_CODE (ret
) == NOP_EXPR
)
12601 realret
= TREE_OPERAND (ret
, 0);
12602 if (CAN_HAVE_LOCATION_P (realret
)
12603 && !EXPR_HAS_LOCATION (realret
))
12604 SET_EXPR_LOCATION (realret
, loc
);
12614 /* Look up the function in builtin_decl that corresponds to DECL
12615 and set ASMSPEC as its user assembler name. DECL must be a
12616 function decl that declares a builtin. */
12619 set_builtin_user_assembler_name (tree decl
, const char *asmspec
)
12622 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
12623 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
12626 builtin
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
12627 set_user_assembler_name (builtin
, asmspec
);
12628 switch (DECL_FUNCTION_CODE (decl
))
12630 case BUILT_IN_MEMCPY
:
12631 init_block_move_fn (asmspec
);
12632 memcpy_libfunc
= set_user_assembler_libfunc ("memcpy", asmspec
);
12634 case BUILT_IN_MEMSET
:
12635 init_block_clear_fn (asmspec
);
12636 memset_libfunc
= set_user_assembler_libfunc ("memset", asmspec
);
12638 case BUILT_IN_MEMMOVE
:
12639 memmove_libfunc
= set_user_assembler_libfunc ("memmove", asmspec
);
12641 case BUILT_IN_MEMCMP
:
12642 memcmp_libfunc
= set_user_assembler_libfunc ("memcmp", asmspec
);
12644 case BUILT_IN_ABORT
:
12645 abort_libfunc
= set_user_assembler_libfunc ("abort", asmspec
);
12648 if (INT_TYPE_SIZE
< BITS_PER_WORD
)
12650 set_user_assembler_libfunc ("ffs", asmspec
);
12651 set_optab_libfunc (ffs_optab
, mode_for_size (INT_TYPE_SIZE
,
12652 MODE_INT
, 0), "ffs");
12660 /* Return true if DECL is a builtin that expands to a constant or similarly
12663 is_simple_builtin (tree decl
)
12665 if (decl
&& DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
12666 switch (DECL_FUNCTION_CODE (decl
))
12668 /* Builtins that expand to constants. */
12669 case BUILT_IN_CONSTANT_P
:
12670 case BUILT_IN_EXPECT
:
12671 case BUILT_IN_OBJECT_SIZE
:
12672 case BUILT_IN_UNREACHABLE
:
12673 /* Simple register moves or loads from stack. */
12674 case BUILT_IN_ASSUME_ALIGNED
:
12675 case BUILT_IN_RETURN_ADDRESS
:
12676 case BUILT_IN_EXTRACT_RETURN_ADDR
:
12677 case BUILT_IN_FROB_RETURN_ADDR
:
12678 case BUILT_IN_RETURN
:
12679 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
12680 case BUILT_IN_FRAME_ADDRESS
:
12681 case BUILT_IN_VA_END
:
12682 case BUILT_IN_STACK_SAVE
:
12683 case BUILT_IN_STACK_RESTORE
:
12684 /* Exception state returns or moves registers around. */
12685 case BUILT_IN_EH_FILTER
:
12686 case BUILT_IN_EH_POINTER
:
12687 case BUILT_IN_EH_COPY_VALUES
:
12697 /* Return true if DECL is a builtin that is not expensive, i.e., they are
12698 most probably expanded inline into reasonably simple code. This is a
12699 superset of is_simple_builtin. */
12701 is_inexpensive_builtin (tree decl
)
12705 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_MD
)
12707 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
12708 switch (DECL_FUNCTION_CODE (decl
))
12711 case BUILT_IN_ALLOCA
:
12712 case BUILT_IN_ALLOCA_WITH_ALIGN
:
12713 case BUILT_IN_BSWAP16
:
12714 case BUILT_IN_BSWAP32
:
12715 case BUILT_IN_BSWAP64
:
12717 case BUILT_IN_CLZIMAX
:
12718 case BUILT_IN_CLZL
:
12719 case BUILT_IN_CLZLL
:
12721 case BUILT_IN_CTZIMAX
:
12722 case BUILT_IN_CTZL
:
12723 case BUILT_IN_CTZLL
:
12725 case BUILT_IN_FFSIMAX
:
12726 case BUILT_IN_FFSL
:
12727 case BUILT_IN_FFSLL
:
12728 case BUILT_IN_IMAXABS
:
12729 case BUILT_IN_FINITE
:
12730 case BUILT_IN_FINITEF
:
12731 case BUILT_IN_FINITEL
:
12732 case BUILT_IN_FINITED32
:
12733 case BUILT_IN_FINITED64
:
12734 case BUILT_IN_FINITED128
:
12735 case BUILT_IN_FPCLASSIFY
:
12736 case BUILT_IN_ISFINITE
:
12737 case BUILT_IN_ISINF_SIGN
:
12738 case BUILT_IN_ISINF
:
12739 case BUILT_IN_ISINFF
:
12740 case BUILT_IN_ISINFL
:
12741 case BUILT_IN_ISINFD32
:
12742 case BUILT_IN_ISINFD64
:
12743 case BUILT_IN_ISINFD128
:
12744 case BUILT_IN_ISNAN
:
12745 case BUILT_IN_ISNANF
:
12746 case BUILT_IN_ISNANL
:
12747 case BUILT_IN_ISNAND32
:
12748 case BUILT_IN_ISNAND64
:
12749 case BUILT_IN_ISNAND128
:
12750 case BUILT_IN_ISNORMAL
:
12751 case BUILT_IN_ISGREATER
:
12752 case BUILT_IN_ISGREATEREQUAL
:
12753 case BUILT_IN_ISLESS
:
12754 case BUILT_IN_ISLESSEQUAL
:
12755 case BUILT_IN_ISLESSGREATER
:
12756 case BUILT_IN_ISUNORDERED
:
12757 case BUILT_IN_VA_ARG_PACK
:
12758 case BUILT_IN_VA_ARG_PACK_LEN
:
12759 case BUILT_IN_VA_COPY
:
12760 case BUILT_IN_TRAP
:
12761 case BUILT_IN_SAVEREGS
:
12762 case BUILT_IN_POPCOUNTL
:
12763 case BUILT_IN_POPCOUNTLL
:
12764 case BUILT_IN_POPCOUNTIMAX
:
12765 case BUILT_IN_POPCOUNT
:
12766 case BUILT_IN_PARITYL
:
12767 case BUILT_IN_PARITYLL
:
12768 case BUILT_IN_PARITYIMAX
:
12769 case BUILT_IN_PARITY
:
12770 case BUILT_IN_LABS
:
12771 case BUILT_IN_LLABS
:
12772 case BUILT_IN_PREFETCH
:
12776 return is_simple_builtin (decl
);