1 /* Expand builtin functions.
2 Copyright (C) 1988-2023 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/>. */
20 /* Legacy warning! Please add no further builtin simplifications here
21 (apart from pure constant folding) - builtin simplifications should go
22 to match.pd or gimple-fold.cc instead. */
26 #include "coretypes.h"
35 #include "stringpool.h"
37 #include "tree-ssanames.h"
42 #include "diagnostic-core.h"
44 #include "fold-const.h"
45 #include "fold-const-call.h"
46 #include "gimple-ssa-warn-access.h"
47 #include "stor-layout.h"
50 #include "tree-object-size.h"
51 #include "tree-ssa-strlen.h"
61 #include "typeclass.h"
62 #include "langhooks.h"
63 #include "value-prof.h"
65 #include "stringpool.h"
68 #include "internal-fn.h"
69 #include "case-cfn-macros.h"
70 #include "gimple-iterator.h"
71 #include "gimple-fold.h"
73 #include "file-prefix-map.h" /* remap_macro_filename() */
74 #include "gomp-constants.h"
75 #include "omp-general.h"
77 #include "gimple-ssa.h"
78 #include "tree-ssa-live.h"
79 #include "tree-outof-ssa.h"
80 #include "attr-fnspec.h"
82 #include "gimple-range.h"
83 #include "pointer-query.h"
85 struct target_builtins default_target_builtins
;
87 struct target_builtins
*this_target_builtins
= &default_target_builtins
;
90 /* Define the names of the builtin function types and codes. */
91 const char *const built_in_class_names
[BUILT_IN_LAST
]
92 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
94 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
95 const char * built_in_names
[(int) END_BUILTINS
] =
97 #include "builtins.def"
100 /* Setup an array of builtin_info_type, make sure each element decl is
101 initialized to NULL_TREE. */
102 builtin_info_type builtin_info
[(int)END_BUILTINS
];
104 /* Non-zero if __builtin_constant_p should be folded right away. */
105 bool force_folding_builtin_constant_p
;
107 static int target_char_cast (tree
, char *);
108 static int apply_args_size (void);
109 static int apply_result_size (void);
110 static rtx
result_vector (int, rtx
);
111 static void expand_builtin_prefetch (tree
);
112 static rtx
expand_builtin_apply_args (void);
113 static rtx
expand_builtin_apply_args_1 (void);
114 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
115 static void expand_builtin_return (rtx
);
116 static enum type_class
type_to_class (tree
);
117 static rtx
expand_builtin_classify_type (tree
);
118 static rtx
expand_builtin_mathfn_3 (tree
, rtx
, rtx
);
119 static rtx
expand_builtin_mathfn_ternary (tree
, rtx
, rtx
);
120 static rtx
expand_builtin_interclass_mathfn (tree
, rtx
);
121 static rtx
expand_builtin_sincos (tree
);
122 static rtx
expand_builtin_fegetround (tree
, rtx
, machine_mode
);
123 static rtx
expand_builtin_feclear_feraise_except (tree
, rtx
, machine_mode
,
125 static rtx
expand_builtin_cexpi (tree
, rtx
);
126 static rtx
expand_builtin_issignaling (tree
, rtx
);
127 static rtx
expand_builtin_int_roundingfn (tree
, rtx
);
128 static rtx
expand_builtin_int_roundingfn_2 (tree
, rtx
);
129 static rtx
expand_builtin_next_arg (void);
130 static rtx
expand_builtin_va_start (tree
);
131 static rtx
expand_builtin_va_end (tree
);
132 static rtx
expand_builtin_va_copy (tree
);
133 static rtx
inline_expand_builtin_bytecmp (tree
, rtx
);
134 static rtx
expand_builtin_strcmp (tree
, rtx
);
135 static rtx
expand_builtin_strncmp (tree
, rtx
, machine_mode
);
136 static rtx
expand_builtin_memcpy (tree
, rtx
);
137 static rtx
expand_builtin_memory_copy_args (tree dest
, tree src
, tree len
,
138 rtx target
, tree exp
,
141 static rtx
expand_builtin_memmove (tree
, rtx
);
142 static rtx
expand_builtin_mempcpy (tree
, rtx
);
143 static rtx
expand_builtin_mempcpy_args (tree
, tree
, tree
, rtx
, tree
, memop_ret
);
144 static rtx
expand_builtin_strcpy (tree
, rtx
);
145 static rtx
expand_builtin_strcpy_args (tree
, tree
, tree
, rtx
);
146 static rtx
expand_builtin_stpcpy (tree
, rtx
, machine_mode
);
147 static rtx
expand_builtin_strncpy (tree
, rtx
);
148 static rtx
expand_builtin_memset_args (tree
, tree
, tree
, rtx
, machine_mode
, tree
);
149 static rtx
expand_builtin_bzero (tree
);
150 static rtx
expand_builtin_strlen (tree
, rtx
, machine_mode
);
151 static rtx
expand_builtin_strnlen (tree
, rtx
, machine_mode
);
152 static rtx
expand_builtin_alloca (tree
);
153 static rtx
expand_builtin_unop (machine_mode
, tree
, rtx
, rtx
, optab
);
154 static rtx
expand_builtin_frame_address (tree
, tree
);
155 static tree
stabilize_va_list_loc (location_t
, tree
, int);
156 static rtx
expand_builtin_expect (tree
, rtx
);
157 static rtx
expand_builtin_expect_with_probability (tree
, rtx
);
158 static tree
fold_builtin_constant_p (tree
);
159 static tree
fold_builtin_classify_type (tree
);
160 static tree
fold_builtin_strlen (location_t
, tree
, tree
, tree
);
161 static tree
fold_builtin_inf (location_t
, tree
, int);
162 static tree
rewrite_call_expr (location_t
, tree
, int, tree
, int, ...);
163 static bool validate_arg (const_tree
, enum tree_code code
);
164 static rtx
expand_builtin_fabs (tree
, rtx
, rtx
);
165 static rtx
expand_builtin_signbit (tree
, rtx
);
166 static tree
fold_builtin_memcmp (location_t
, tree
, tree
, tree
);
167 static tree
fold_builtin_isascii (location_t
, tree
);
168 static tree
fold_builtin_toascii (location_t
, tree
);
169 static tree
fold_builtin_isdigit (location_t
, tree
);
170 static tree
fold_builtin_fabs (location_t
, tree
, tree
);
171 static tree
fold_builtin_abs (location_t
, tree
, tree
);
172 static tree
fold_builtin_unordered_cmp (location_t
, tree
, tree
, tree
, enum tree_code
,
174 static tree
fold_builtin_varargs (location_t
, tree
, tree
*, int);
176 static tree
fold_builtin_strpbrk (location_t
, tree
, tree
, tree
, tree
);
177 static tree
fold_builtin_strspn (location_t
, tree
, tree
, tree
);
178 static tree
fold_builtin_strcspn (location_t
, tree
, tree
, tree
);
180 static rtx
expand_builtin_object_size (tree
);
181 static rtx
expand_builtin_memory_chk (tree
, rtx
, machine_mode
,
182 enum built_in_function
);
183 static void maybe_emit_chk_warning (tree
, enum built_in_function
);
184 static void maybe_emit_sprintf_chk_warning (tree
, enum built_in_function
);
185 static tree
fold_builtin_object_size (tree
, tree
, enum built_in_function
);
187 unsigned HOST_WIDE_INT target_newline
;
188 unsigned HOST_WIDE_INT target_percent
;
189 static unsigned HOST_WIDE_INT target_c
;
190 static unsigned HOST_WIDE_INT target_s
;
191 char target_percent_c
[3];
192 char target_percent_s
[3];
193 char target_percent_s_newline
[4];
194 static tree
do_mpfr_remquo (tree
, tree
, tree
);
195 static tree
do_mpfr_lgamma_r (tree
, tree
, tree
);
196 static void expand_builtin_sync_synchronize (void);
198 /* Return true if NAME starts with __builtin_ or __sync_. */
201 is_builtin_name (const char *name
)
203 return (startswith (name
, "__builtin_")
204 || startswith (name
, "__sync_")
205 || startswith (name
, "__atomic_"));
208 /* Return true if NODE should be considered for inline expansion regardless
209 of the optimization level. This means whenever a function is invoked with
210 its "internal" name, which normally contains the prefix "__builtin". */
213 called_as_built_in (tree node
)
215 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
216 we want the name used to call the function, not the name it
218 const char *name
= IDENTIFIER_POINTER (DECL_NAME (node
));
219 return is_builtin_name (name
);
222 /* Compute values M and N such that M divides (address of EXP - N) and such
223 that N < M. If these numbers can be determined, store M in alignp and N in
224 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
225 *alignp and any bit-offset to *bitposp.
227 Note that the address (and thus the alignment) computed here is based
228 on the address to which a symbol resolves, whereas DECL_ALIGN is based
229 on the address at which an object is actually located. These two
230 addresses are not always the same. For example, on ARM targets,
231 the address &foo of a Thumb function foo() has the lowest bit set,
232 whereas foo() itself starts on an even address.
234 If ADDR_P is true we are taking the address of the memory reference EXP
235 and thus cannot rely on the access taking place. */
238 get_object_alignment_2 (tree exp
, unsigned int *alignp
,
239 unsigned HOST_WIDE_INT
*bitposp
, bool addr_p
)
241 poly_int64 bitsize
, bitpos
;
244 int unsignedp
, reversep
, volatilep
;
245 unsigned int align
= BITS_PER_UNIT
;
246 bool known_alignment
= false;
248 /* Get the innermost object and the constant (bitpos) and possibly
249 variable (offset) offset of the access. */
250 exp
= get_inner_reference (exp
, &bitsize
, &bitpos
, &offset
, &mode
,
251 &unsignedp
, &reversep
, &volatilep
);
253 /* Extract alignment information from the innermost object and
254 possibly adjust bitpos and offset. */
255 if (TREE_CODE (exp
) == FUNCTION_DECL
)
257 /* Function addresses can encode extra information besides their
258 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
259 allows the low bit to be used as a virtual bit, we know
260 that the address itself must be at least 2-byte aligned. */
261 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_pfn
)
262 align
= 2 * BITS_PER_UNIT
;
264 else if (TREE_CODE (exp
) == LABEL_DECL
)
266 else if (TREE_CODE (exp
) == CONST_DECL
)
268 /* The alignment of a CONST_DECL is determined by its initializer. */
269 exp
= DECL_INITIAL (exp
);
270 align
= TYPE_ALIGN (TREE_TYPE (exp
));
271 if (CONSTANT_CLASS_P (exp
))
272 align
= targetm
.constant_alignment (exp
, align
);
274 known_alignment
= true;
276 else if (DECL_P (exp
))
278 align
= DECL_ALIGN (exp
);
279 known_alignment
= true;
281 else if (TREE_CODE (exp
) == INDIRECT_REF
282 || TREE_CODE (exp
) == MEM_REF
283 || TREE_CODE (exp
) == TARGET_MEM_REF
)
285 tree addr
= TREE_OPERAND (exp
, 0);
287 unsigned HOST_WIDE_INT ptr_bitpos
;
288 unsigned HOST_WIDE_INT ptr_bitmask
= ~0;
290 /* If the address is explicitely aligned, handle that. */
291 if (TREE_CODE (addr
) == BIT_AND_EXPR
292 && TREE_CODE (TREE_OPERAND (addr
, 1)) == INTEGER_CST
)
294 ptr_bitmask
= TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1));
295 ptr_bitmask
*= BITS_PER_UNIT
;
296 align
= least_bit_hwi (ptr_bitmask
);
297 addr
= TREE_OPERAND (addr
, 0);
301 = get_pointer_alignment_1 (addr
, &ptr_align
, &ptr_bitpos
);
302 align
= MAX (ptr_align
, align
);
304 /* Re-apply explicit alignment to the bitpos. */
305 ptr_bitpos
&= ptr_bitmask
;
307 /* The alignment of the pointer operand in a TARGET_MEM_REF
308 has to take the variable offset parts into account. */
309 if (TREE_CODE (exp
) == TARGET_MEM_REF
)
313 unsigned HOST_WIDE_INT step
= 1;
315 step
= TREE_INT_CST_LOW (TMR_STEP (exp
));
316 align
= MIN (align
, least_bit_hwi (step
) * BITS_PER_UNIT
);
318 if (TMR_INDEX2 (exp
))
319 align
= BITS_PER_UNIT
;
320 known_alignment
= false;
323 /* When EXP is an actual memory reference then we can use
324 TYPE_ALIGN of a pointer indirection to derive alignment.
325 Do so only if get_pointer_alignment_1 did not reveal absolute
326 alignment knowledge and if using that alignment would
327 improve the situation. */
329 if (!addr_p
&& !known_alignment
330 && (talign
= min_align_of_type (TREE_TYPE (exp
)) * BITS_PER_UNIT
)
335 /* Else adjust bitpos accordingly. */
336 bitpos
+= ptr_bitpos
;
337 if (TREE_CODE (exp
) == MEM_REF
338 || TREE_CODE (exp
) == TARGET_MEM_REF
)
339 bitpos
+= mem_ref_offset (exp
).force_shwi () * BITS_PER_UNIT
;
342 else if (TREE_CODE (exp
) == STRING_CST
)
344 /* STRING_CST are the only constant objects we allow to be not
345 wrapped inside a CONST_DECL. */
346 align
= TYPE_ALIGN (TREE_TYPE (exp
));
347 if (CONSTANT_CLASS_P (exp
))
348 align
= targetm
.constant_alignment (exp
, align
);
350 known_alignment
= true;
353 /* If there is a non-constant offset part extract the maximum
354 alignment that can prevail. */
357 unsigned int trailing_zeros
= tree_ctz (offset
);
358 if (trailing_zeros
< HOST_BITS_PER_INT
)
360 unsigned int inner
= (1U << trailing_zeros
) * BITS_PER_UNIT
;
362 align
= MIN (align
, inner
);
366 /* Account for the alignment of runtime coefficients, so that the constant
367 bitpos is guaranteed to be accurate. */
368 unsigned int alt_align
= ::known_alignment (bitpos
- bitpos
.coeffs
[0]);
369 if (alt_align
!= 0 && alt_align
< align
)
372 known_alignment
= false;
376 *bitposp
= bitpos
.coeffs
[0] & (align
- 1);
377 return known_alignment
;
380 /* For a memory reference expression EXP compute values M and N such that M
381 divides (&EXP - N) and such that N < M. If these numbers can be determined,
382 store M in alignp and N in *BITPOSP and return true. Otherwise return false
383 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
386 get_object_alignment_1 (tree exp
, unsigned int *alignp
,
387 unsigned HOST_WIDE_INT
*bitposp
)
389 /* Strip a WITH_SIZE_EXPR, get_inner_reference doesn't know how to deal
391 if (TREE_CODE (exp
) == WITH_SIZE_EXPR
)
392 exp
= TREE_OPERAND (exp
, 0);
393 return get_object_alignment_2 (exp
, alignp
, bitposp
, false);
396 /* Return the alignment in bits of EXP, an object. */
399 get_object_alignment (tree exp
)
401 unsigned HOST_WIDE_INT bitpos
= 0;
404 get_object_alignment_1 (exp
, &align
, &bitpos
);
406 /* align and bitpos now specify known low bits of the pointer.
407 ptr & (align - 1) == bitpos. */
410 align
= least_bit_hwi (bitpos
);
414 /* For a pointer valued expression EXP compute values M and N such that M
415 divides (EXP - N) and such that N < M. If these numbers can be determined,
416 store M in alignp and N in *BITPOSP and return true. Return false if
417 the results are just a conservative approximation.
419 If EXP is not a pointer, false is returned too. */
422 get_pointer_alignment_1 (tree exp
, unsigned int *alignp
,
423 unsigned HOST_WIDE_INT
*bitposp
)
427 if (TREE_CODE (exp
) == ADDR_EXPR
)
428 return get_object_alignment_2 (TREE_OPERAND (exp
, 0),
429 alignp
, bitposp
, true);
430 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
433 unsigned HOST_WIDE_INT bitpos
;
434 bool res
= get_pointer_alignment_1 (TREE_OPERAND (exp
, 0),
436 if (TREE_CODE (TREE_OPERAND (exp
, 1)) == INTEGER_CST
)
437 bitpos
+= TREE_INT_CST_LOW (TREE_OPERAND (exp
, 1)) * BITS_PER_UNIT
;
440 unsigned int trailing_zeros
= tree_ctz (TREE_OPERAND (exp
, 1));
441 if (trailing_zeros
< HOST_BITS_PER_INT
)
443 unsigned int inner
= (1U << trailing_zeros
) * BITS_PER_UNIT
;
445 align
= MIN (align
, inner
);
449 *bitposp
= bitpos
& (align
- 1);
452 else if (TREE_CODE (exp
) == SSA_NAME
453 && POINTER_TYPE_P (TREE_TYPE (exp
)))
455 unsigned int ptr_align
, ptr_misalign
;
456 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (exp
);
458 if (pi
&& get_ptr_info_alignment (pi
, &ptr_align
, &ptr_misalign
))
460 *bitposp
= ptr_misalign
* BITS_PER_UNIT
;
461 *alignp
= ptr_align
* BITS_PER_UNIT
;
462 /* Make sure to return a sensible alignment when the multiplication
463 by BITS_PER_UNIT overflowed. */
465 *alignp
= 1u << (HOST_BITS_PER_INT
- 1);
466 /* We cannot really tell whether this result is an approximation. */
472 *alignp
= BITS_PER_UNIT
;
476 else if (TREE_CODE (exp
) == INTEGER_CST
)
478 *alignp
= BIGGEST_ALIGNMENT
;
479 *bitposp
= ((TREE_INT_CST_LOW (exp
) * BITS_PER_UNIT
)
480 & (BIGGEST_ALIGNMENT
- 1));
485 *alignp
= BITS_PER_UNIT
;
489 /* Return the alignment in bits of EXP, a pointer valued expression.
490 The alignment returned is, by default, the alignment of the thing that
491 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
493 Otherwise, look at the expression to see if we can do better, i.e., if the
494 expression is actually pointing at an object whose alignment is tighter. */
497 get_pointer_alignment (tree exp
)
499 unsigned HOST_WIDE_INT bitpos
= 0;
502 get_pointer_alignment_1 (exp
, &align
, &bitpos
);
504 /* align and bitpos now specify known low bits of the pointer.
505 ptr & (align - 1) == bitpos. */
508 align
= least_bit_hwi (bitpos
);
513 /* Return the number of leading non-zero elements in the sequence
514 [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
515 ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
518 string_length (const void *ptr
, unsigned eltsize
, unsigned maxelts
)
520 gcc_checking_assert (eltsize
== 1 || eltsize
== 2 || eltsize
== 4);
526 /* Optimize the common case of plain char. */
527 for (n
= 0; n
< maxelts
; n
++)
529 const char *elt
= (const char*) ptr
+ n
;
536 for (n
= 0; n
< maxelts
; n
++)
538 const char *elt
= (const char*) ptr
+ n
* eltsize
;
539 if (!memcmp (elt
, "\0\0\0\0", eltsize
))
546 /* Compute the length of a null-terminated character string or wide
547 character string handling character sizes of 1, 2, and 4 bytes.
548 TREE_STRING_LENGTH is not the right way because it evaluates to
549 the size of the character array in bytes (as opposed to characters)
550 and because it can contain a zero byte in the middle.
552 ONLY_VALUE should be nonzero if the result is not going to be emitted
553 into the instruction stream and zero if it is going to be expanded.
554 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
555 is returned, otherwise NULL, since
556 len = c_strlen (ARG, 1); if (len) expand_expr (len, ...); would not
557 evaluate the side-effects.
559 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
560 accesses. Note that this implies the result is not going to be emitted
561 into the instruction stream.
563 Additional information about the string accessed may be recorded
564 in DATA. For example, if ARG references an unterminated string,
565 then the declaration will be stored in the DECL field. If the
566 length of the unterminated string can be determined, it'll be
567 stored in the LEN field. Note this length could well be different
568 than what a C strlen call would return.
570 ELTSIZE is 1 for normal single byte character strings, and 2 or
571 4 for wide characer strings. ELTSIZE is by default 1.
573 The value returned is of type `ssizetype'. */
576 c_strlen (tree arg
, int only_value
, c_strlen_data
*data
, unsigned eltsize
)
578 /* If we were not passed a DATA pointer, then get one to a local
579 structure. That avoids having to check DATA for NULL before
580 each time we want to use it. */
581 c_strlen_data local_strlen_data
= { };
583 data
= &local_strlen_data
;
585 gcc_checking_assert (eltsize
== 1 || eltsize
== 2 || eltsize
== 4);
587 tree src
= STRIP_NOPS (arg
);
588 if (TREE_CODE (src
) == COND_EXPR
589 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
593 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
, data
, eltsize
);
594 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
, data
, eltsize
);
595 if (tree_int_cst_equal (len1
, len2
))
599 if (TREE_CODE (src
) == COMPOUND_EXPR
600 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
601 return c_strlen (TREE_OPERAND (src
, 1), only_value
, data
, eltsize
);
603 location_t loc
= EXPR_LOC_OR_LOC (src
, input_location
);
605 /* Offset from the beginning of the string in bytes. */
609 src
= string_constant (src
, &byteoff
, &memsize
, &decl
);
613 /* Determine the size of the string element. */
614 if (eltsize
!= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src
)))))
617 /* Set MAXELTS to ARRAY_SIZE (SRC) - 1, the maximum possible
618 length of SRC. Prefer TYPE_SIZE() to TREE_STRING_LENGTH() if possible
619 in case the latter is less than the size of the array, such as when
620 SRC refers to a short string literal used to initialize a large array.
621 In that case, the elements of the array after the terminating NUL are
623 HOST_WIDE_INT strelts
= TREE_STRING_LENGTH (src
);
624 strelts
= strelts
/ eltsize
;
626 if (!tree_fits_uhwi_p (memsize
))
629 HOST_WIDE_INT maxelts
= tree_to_uhwi (memsize
) / eltsize
;
631 /* PTR can point to the byte representation of any string type, including
632 char* and wchar_t*. */
633 const char *ptr
= TREE_STRING_POINTER (src
);
635 if (byteoff
&& TREE_CODE (byteoff
) != INTEGER_CST
)
637 /* The code below works only for single byte character types. */
641 /* If the string has an internal NUL character followed by any
642 non-NUL characters (e.g., "foo\0bar"), we can't compute
643 the offset to the following NUL if we don't know where to
644 start searching for it. */
645 unsigned len
= string_length (ptr
, eltsize
, strelts
);
647 /* Return when an embedded null character is found or none at all.
648 In the latter case, set the DECL/LEN field in the DATA structure
649 so that callers may examine them. */
650 if (len
+ 1 < strelts
)
652 else if (len
>= maxelts
)
656 data
->minlen
= ssize_int (len
);
660 /* For empty strings the result should be zero. */
662 return ssize_int (0);
664 /* We don't know the starting offset, but we do know that the string
665 has no internal zero bytes. If the offset falls within the bounds
666 of the string subtract the offset from the length of the string,
667 and return that. Otherwise the length is zero. Take care to
668 use SAVE_EXPR in case the OFFSET has side-effects. */
669 tree offsave
= TREE_SIDE_EFFECTS (byteoff
) ? save_expr (byteoff
)
671 offsave
= fold_convert_loc (loc
, sizetype
, offsave
);
672 tree condexp
= fold_build2_loc (loc
, LE_EXPR
, boolean_type_node
, offsave
,
674 tree lenexp
= fold_build2_loc (loc
, MINUS_EXPR
, sizetype
, size_int (len
),
676 lenexp
= fold_convert_loc (loc
, ssizetype
, lenexp
);
677 return fold_build3_loc (loc
, COND_EXPR
, ssizetype
, condexp
, lenexp
,
678 build_zero_cst (ssizetype
));
681 /* Offset from the beginning of the string in elements. */
682 HOST_WIDE_INT eltoff
;
684 /* We have a known offset into the string. Start searching there for
685 a null character if we can represent it as a single HOST_WIDE_INT. */
688 else if (! tree_fits_uhwi_p (byteoff
) || tree_to_uhwi (byteoff
) % eltsize
)
691 eltoff
= tree_to_uhwi (byteoff
) / eltsize
;
693 /* If the offset is known to be out of bounds, warn, and call strlen at
695 if (eltoff
< 0 || eltoff
>= maxelts
)
697 /* Suppress multiple warnings for propagated constant strings. */
699 && !warning_suppressed_p (arg
, OPT_Warray_bounds_
)
700 && warning_at (loc
, OPT_Warray_bounds_
,
701 "offset %qwi outside bounds of constant string",
705 inform (DECL_SOURCE_LOCATION (decl
), "%qE declared here", decl
);
706 suppress_warning (arg
, OPT_Warray_bounds_
);
711 /* If eltoff is larger than strelts but less than maxelts the
712 string length is zero, since the excess memory will be zero. */
713 if (eltoff
> strelts
)
714 return ssize_int (0);
716 /* Use strlen to search for the first zero byte. Since any strings
717 constructed with build_string will have nulls appended, we win even
718 if we get handed something like (char[4])"abcd".
720 Since ELTOFF is our starting index into the string, no further
721 calculation is needed. */
722 unsigned len
= string_length (ptr
+ eltoff
* eltsize
, eltsize
,
725 /* Don't know what to return if there was no zero termination.
726 Ideally this would turn into a gcc_checking_assert over time.
727 Set DECL/LEN so callers can examine them. */
728 if (len
>= maxelts
- eltoff
)
732 data
->minlen
= ssize_int (len
);
736 return ssize_int (len
);
739 /* Return a constant integer corresponding to target reading
740 GET_MODE_BITSIZE (MODE) bits from string constant STR. If
741 NULL_TERMINATED_P, reading stops after '\0' character, all further ones
742 are assumed to be zero, otherwise it reads as many characters
746 c_readstr (const char *str
, scalar_int_mode mode
,
747 bool null_terminated_p
/*=true*/)
751 HOST_WIDE_INT tmp
[MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
];
753 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
754 unsigned int len
= (GET_MODE_PRECISION (mode
) + HOST_BITS_PER_WIDE_INT
- 1)
755 / HOST_BITS_PER_WIDE_INT
;
757 gcc_assert (len
<= MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
);
758 for (i
= 0; i
< len
; i
++)
762 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
765 if (WORDS_BIG_ENDIAN
)
766 j
= GET_MODE_SIZE (mode
) - i
- 1;
767 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
768 && GET_MODE_SIZE (mode
) >= UNITS_PER_WORD
)
769 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
772 if (ch
|| !null_terminated_p
)
773 ch
= (unsigned char) str
[i
];
774 tmp
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
777 wide_int c
= wide_int::from_array (tmp
, len
, GET_MODE_PRECISION (mode
));
778 return immed_wide_int_const (c
, mode
);
781 /* Cast a target constant CST to target CHAR and if that value fits into
782 host char type, return zero and put that value into variable pointed to by
786 target_char_cast (tree cst
, char *p
)
788 unsigned HOST_WIDE_INT val
, hostval
;
790 if (TREE_CODE (cst
) != INTEGER_CST
791 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
794 /* Do not care if it fits or not right here. */
795 val
= TREE_INT_CST_LOW (cst
);
797 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
798 val
&= (HOST_WIDE_INT_1U
<< CHAR_TYPE_SIZE
) - 1;
801 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
802 hostval
&= (HOST_WIDE_INT_1U
<< HOST_BITS_PER_CHAR
) - 1;
811 /* Similar to save_expr, but assumes that arbitrary code is not executed
812 in between the multiple evaluations. In particular, we assume that a
813 non-addressable local variable will not be modified. */
816 builtin_save_expr (tree exp
)
818 if (TREE_CODE (exp
) == SSA_NAME
819 || (TREE_ADDRESSABLE (exp
) == 0
820 && (TREE_CODE (exp
) == PARM_DECL
821 || (VAR_P (exp
) && !TREE_STATIC (exp
)))))
824 return save_expr (exp
);
827 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
828 times to get the address of either a higher stack frame, or a return
829 address located within it (depending on FNDECL_CODE). */
832 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
)
835 rtx tem
= INITIAL_FRAME_ADDRESS_RTX
;
838 /* For a zero count with __builtin_return_address, we don't care what
839 frame address we return, because target-specific definitions will
840 override us. Therefore frame pointer elimination is OK, and using
841 the soft frame pointer is OK.
843 For a nonzero count, or a zero count with __builtin_frame_address,
844 we require a stable offset from the current frame pointer to the
845 previous one, so we must use the hard frame pointer, and
846 we must disable frame pointer elimination. */
847 if (count
== 0 && fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
848 tem
= frame_pointer_rtx
;
851 tem
= hard_frame_pointer_rtx
;
853 /* Tell reload not to eliminate the frame pointer. */
854 crtl
->accesses_prior_frames
= 1;
859 SETUP_FRAME_ADDRESSES ();
861 /* On the SPARC, the return address is not in the frame, it is in a
862 register. There is no way to access it off of the current frame
863 pointer, but it can be accessed off the previous frame pointer by
864 reading the value from the register window save area. */
865 if (RETURN_ADDR_IN_PREVIOUS_FRAME
&& fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
868 /* Scan back COUNT frames to the specified frame. */
869 for (i
= 0; i
< count
; i
++)
871 /* Assume the dynamic chain pointer is in the word that the
872 frame address points to, unless otherwise specified. */
873 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
874 tem
= memory_address (Pmode
, tem
);
875 tem
= gen_frame_mem (Pmode
, tem
);
876 tem
= copy_to_reg (tem
);
879 /* For __builtin_frame_address, return what we've got. But, on
880 the SPARC for example, we may have to add a bias. */
881 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
882 return FRAME_ADDR_RTX (tem
);
884 /* For __builtin_return_address, get the return address from that frame. */
885 #ifdef RETURN_ADDR_RTX
886 tem
= RETURN_ADDR_RTX (count
, tem
);
888 tem
= memory_address (Pmode
,
889 plus_constant (Pmode
, tem
, GET_MODE_SIZE (Pmode
)));
890 tem
= gen_frame_mem (Pmode
, tem
);
895 /* Alias set used for setjmp buffer. */
896 static alias_set_type setjmp_alias_set
= -1;
898 /* Construct the leading half of a __builtin_setjmp call. Control will
899 return to RECEIVER_LABEL. This is also called directly by the SJLJ
900 exception handling code. */
903 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
905 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
909 if (setjmp_alias_set
== -1)
910 setjmp_alias_set
= new_alias_set ();
912 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
914 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
916 /* We store the frame pointer and the address of receiver_label in
917 the buffer and use the rest of it for the stack save area, which
918 is machine-dependent. */
920 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
921 set_mem_alias_set (mem
, setjmp_alias_set
);
922 emit_move_insn (mem
, hard_frame_pointer_rtx
);
924 mem
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
925 GET_MODE_SIZE (Pmode
))),
926 set_mem_alias_set (mem
, setjmp_alias_set
);
928 emit_move_insn (validize_mem (mem
),
929 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
931 stack_save
= gen_rtx_MEM (sa_mode
,
932 plus_constant (Pmode
, buf_addr
,
933 2 * GET_MODE_SIZE (Pmode
)));
934 set_mem_alias_set (stack_save
, setjmp_alias_set
);
935 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
937 /* If there is further processing to do, do it. */
938 if (targetm
.have_builtin_setjmp_setup ())
939 emit_insn (targetm
.gen_builtin_setjmp_setup (buf_addr
));
941 /* We have a nonlocal label. */
942 cfun
->has_nonlocal_label
= 1;
945 /* Construct the trailing part of a __builtin_setjmp call. This is
946 also called directly by the SJLJ exception handling code.
947 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
950 expand_builtin_setjmp_receiver (rtx receiver_label
)
954 /* Mark the FP as used when we get here, so we have to make sure it's
955 marked as used by this function. */
956 emit_use (hard_frame_pointer_rtx
);
958 /* Mark the static chain as clobbered here so life information
959 doesn't get messed up for it. */
960 chain
= rtx_for_static_chain (current_function_decl
, true);
961 if (chain
&& REG_P (chain
))
962 emit_clobber (chain
);
964 if (!HARD_FRAME_POINTER_IS_ARG_POINTER
&& fixed_regs
[ARG_POINTER_REGNUM
])
966 /* If the argument pointer can be eliminated in favor of the
967 frame pointer, we don't need to restore it. We assume here
968 that if such an elimination is present, it can always be used.
969 This is the case on all known machines; if we don't make this
970 assumption, we do unnecessary saving on many machines. */
972 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
974 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
975 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
976 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
979 if (i
== ARRAY_SIZE (elim_regs
))
981 /* Now restore our arg pointer from the address at which it
982 was saved in our stack frame. */
983 emit_move_insn (crtl
->args
.internal_arg_pointer
,
984 copy_to_reg (get_arg_pointer_save_area ()));
988 if (receiver_label
!= NULL
&& targetm
.have_builtin_setjmp_receiver ())
989 emit_insn (targetm
.gen_builtin_setjmp_receiver (receiver_label
));
990 else if (targetm
.have_nonlocal_goto_receiver ())
991 emit_insn (targetm
.gen_nonlocal_goto_receiver ());
995 /* We must not allow the code we just generated to be reordered by
996 scheduling. Specifically, the update of the frame pointer must
997 happen immediately, not later. */
998 emit_insn (gen_blockage ());
1001 /* __builtin_longjmp is passed a pointer to an array of five words (not
1002 all will be used on all machines). It operates similarly to the C
1003 library function of the same name, but is more efficient. Much of
1004 the code below is copied from the handling of non-local gotos. */
1007 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
1010 rtx_insn
*insn
, *last
;
1011 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
1013 /* DRAP is needed for stack realign if longjmp is expanded to current
1015 if (SUPPORTS_STACK_ALIGNMENT
)
1016 crtl
->need_drap
= true;
1018 if (setjmp_alias_set
== -1)
1019 setjmp_alias_set
= new_alias_set ();
1021 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
1023 buf_addr
= force_reg (Pmode
, buf_addr
);
1025 /* We require that the user must pass a second argument of 1, because
1026 that is what builtin_setjmp will return. */
1027 gcc_assert (value
== const1_rtx
);
1029 last
= get_last_insn ();
1030 if (targetm
.have_builtin_longjmp ())
1031 emit_insn (targetm
.gen_builtin_longjmp (buf_addr
));
1034 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
1035 lab
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
1036 GET_MODE_SIZE (Pmode
)));
1038 stack
= gen_rtx_MEM (sa_mode
, plus_constant (Pmode
, buf_addr
,
1039 2 * GET_MODE_SIZE (Pmode
)));
1040 set_mem_alias_set (fp
, setjmp_alias_set
);
1041 set_mem_alias_set (lab
, setjmp_alias_set
);
1042 set_mem_alias_set (stack
, setjmp_alias_set
);
1044 /* Pick up FP, label, and SP from the block and jump. This code is
1045 from expand_goto in stmt.cc; see there for detailed comments. */
1046 if (targetm
.have_nonlocal_goto ())
1047 /* We have to pass a value to the nonlocal_goto pattern that will
1048 get copied into the static_chain pointer, but it does not matter
1049 what that value is, because builtin_setjmp does not use it. */
1050 emit_insn (targetm
.gen_nonlocal_goto (value
, lab
, stack
, fp
));
1053 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1054 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1056 lab
= copy_to_reg (lab
);
1058 /* Restore the frame pointer and stack pointer. We must use a
1059 temporary since the setjmp buffer may be a local. */
1060 fp
= copy_to_reg (fp
);
1061 emit_stack_restore (SAVE_NONLOCAL
, stack
);
1063 /* Ensure the frame pointer move is not optimized. */
1064 emit_insn (gen_blockage ());
1065 emit_clobber (hard_frame_pointer_rtx
);
1066 emit_clobber (frame_pointer_rtx
);
1067 emit_move_insn (hard_frame_pointer_rtx
, fp
);
1069 emit_use (hard_frame_pointer_rtx
);
1070 emit_use (stack_pointer_rtx
);
1071 emit_indirect_jump (lab
);
1075 /* Search backwards and mark the jump insn as a non-local goto.
1076 Note that this precludes the use of __builtin_longjmp to a
1077 __builtin_setjmp target in the same function. However, we've
1078 already cautioned the user that these functions are for
1079 internal exception handling use only. */
1080 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1082 gcc_assert (insn
!= last
);
1086 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1089 else if (CALL_P (insn
))
1095 more_const_call_expr_args_p (const const_call_expr_arg_iterator
*iter
)
1097 return (iter
->i
< iter
->n
);
1100 /* This function validates the types of a function call argument list
1101 against a specified list of tree_codes. If the last specifier is a 0,
1102 that represents an ellipsis, otherwise the last specifier must be a
1106 validate_arglist (const_tree callexpr
, ...)
1108 enum tree_code code
;
1111 const_call_expr_arg_iterator iter
;
1114 va_start (ap
, callexpr
);
1115 init_const_call_expr_arg_iterator (callexpr
, &iter
);
1117 /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1118 tree fn
= CALL_EXPR_FN (callexpr
);
1119 bitmap argmap
= get_nonnull_args (TREE_TYPE (TREE_TYPE (fn
)));
1121 for (unsigned argno
= 1; ; ++argno
)
1123 code
= (enum tree_code
) va_arg (ap
, int);
1128 /* This signifies an ellipses, any further arguments are all ok. */
1132 /* This signifies an endlink, if no arguments remain, return
1133 true, otherwise return false. */
1134 res
= !more_const_call_expr_args_p (&iter
);
1137 /* The actual argument must be nonnull when either the whole
1138 called function has been declared nonnull, or when the formal
1139 argument corresponding to the actual argument has been. */
1141 && (bitmap_empty_p (argmap
) || bitmap_bit_p (argmap
, argno
)))
1143 arg
= next_const_call_expr_arg (&iter
);
1144 if (!validate_arg (arg
, code
) || integer_zerop (arg
))
1150 /* If no parameters remain or the parameter's code does not
1151 match the specified code, return false. Otherwise continue
1152 checking any remaining arguments. */
1153 arg
= next_const_call_expr_arg (&iter
);
1154 if (!validate_arg (arg
, code
))
1160 /* We need gotos here since we can only have one VA_CLOSE in a
1165 BITMAP_FREE (argmap
);
1170 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1171 and the address of the save area. */
1174 expand_builtin_nonlocal_goto (tree exp
)
1176 tree t_label
, t_save_area
;
1177 rtx r_label
, r_save_area
, r_fp
, r_sp
;
1180 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
1183 t_label
= CALL_EXPR_ARG (exp
, 0);
1184 t_save_area
= CALL_EXPR_ARG (exp
, 1);
1186 r_label
= expand_normal (t_label
);
1187 r_label
= convert_memory_address (Pmode
, r_label
);
1188 r_save_area
= expand_normal (t_save_area
);
1189 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
1190 /* Copy the address of the save location to a register just in case it was
1191 based on the frame pointer. */
1192 r_save_area
= copy_to_reg (r_save_area
);
1193 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
1194 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
1195 plus_constant (Pmode
, r_save_area
,
1196 GET_MODE_SIZE (Pmode
)));
1198 crtl
->has_nonlocal_goto
= 1;
1200 /* ??? We no longer need to pass the static chain value, afaik. */
1201 if (targetm
.have_nonlocal_goto ())
1202 emit_insn (targetm
.gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
1205 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1206 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1208 r_label
= copy_to_reg (r_label
);
1210 /* Restore the frame pointer and stack pointer. We must use a
1211 temporary since the setjmp buffer may be a local. */
1212 r_fp
= copy_to_reg (r_fp
);
1213 emit_stack_restore (SAVE_NONLOCAL
, r_sp
);
1215 /* Ensure the frame pointer move is not optimized. */
1216 emit_insn (gen_blockage ());
1217 emit_clobber (hard_frame_pointer_rtx
);
1218 emit_clobber (frame_pointer_rtx
);
1219 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
1221 /* USE of hard_frame_pointer_rtx added for consistency;
1222 not clear if really needed. */
1223 emit_use (hard_frame_pointer_rtx
);
1224 emit_use (stack_pointer_rtx
);
1226 /* If the architecture is using a GP register, we must
1227 conservatively assume that the target function makes use of it.
1228 The prologue of functions with nonlocal gotos must therefore
1229 initialize the GP register to the appropriate value, and we
1230 must then make sure that this value is live at the point
1231 of the jump. (Note that this doesn't necessarily apply
1232 to targets with a nonlocal_goto pattern; they are free
1233 to implement it in their own way. Note also that this is
1234 a no-op if the GP register is a global invariant.) */
1235 unsigned regnum
= PIC_OFFSET_TABLE_REGNUM
;
1236 if (regnum
!= INVALID_REGNUM
&& fixed_regs
[regnum
])
1237 emit_use (pic_offset_table_rtx
);
1239 emit_indirect_jump (r_label
);
1242 /* Search backwards to the jump insn and mark it as a
1244 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1248 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1251 else if (CALL_P (insn
))
1258 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1259 (not all will be used on all machines) that was passed to __builtin_setjmp.
1260 It updates the stack pointer in that block to the current value. This is
1261 also called directly by the SJLJ exception handling code. */
1264 expand_builtin_update_setjmp_buf (rtx buf_addr
)
1266 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
1267 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
1269 = gen_rtx_MEM (sa_mode
,
1272 plus_constant (Pmode
, buf_addr
,
1273 2 * GET_MODE_SIZE (Pmode
))));
1275 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
1278 /* Expand a call to __builtin_prefetch. For a target that does not support
1279 data prefetch, evaluate the memory address argument in case it has side
1283 expand_builtin_prefetch (tree exp
)
1285 tree arg0
, arg1
, arg2
;
1289 if (!validate_arglist (exp
, POINTER_TYPE
, 0))
1292 arg0
= CALL_EXPR_ARG (exp
, 0);
1294 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1295 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1297 nargs
= call_expr_nargs (exp
);
1299 arg1
= CALL_EXPR_ARG (exp
, 1);
1301 arg1
= integer_zero_node
;
1303 arg2
= CALL_EXPR_ARG (exp
, 2);
1305 arg2
= integer_three_node
;
1307 /* Argument 0 is an address. */
1308 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
1310 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1311 if (TREE_CODE (arg1
) != INTEGER_CST
)
1313 error ("second argument to %<__builtin_prefetch%> must be a constant");
1314 arg1
= integer_zero_node
;
1316 op1
= expand_normal (arg1
);
1317 /* Argument 1 must be either zero or one. */
1318 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
1320 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1325 /* Argument 2 (locality) must be a compile-time constant int. */
1326 if (TREE_CODE (arg2
) != INTEGER_CST
)
1328 error ("third argument to %<__builtin_prefetch%> must be a constant");
1329 arg2
= integer_zero_node
;
1331 op2
= expand_normal (arg2
);
1332 /* Argument 2 must be 0, 1, 2, or 3. */
1333 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
1335 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1339 if (targetm
.have_prefetch ())
1341 class expand_operand ops
[3];
1343 create_address_operand (&ops
[0], op0
);
1344 create_integer_operand (&ops
[1], INTVAL (op1
));
1345 create_integer_operand (&ops
[2], INTVAL (op2
));
1346 if (maybe_expand_insn (targetm
.code_for_prefetch
, 3, ops
))
1350 /* Don't do anything with direct references to volatile memory, but
1351 generate code to handle other side effects. */
1352 if (!MEM_P (op0
) && side_effects_p (op0
))
1356 /* Get a MEM rtx for expression EXP which is the address of an operand
1357 to be used in a string instruction (cmpstrsi, cpymemsi, ..). LEN is
1358 the maximum length of the block of memory that might be accessed or
1362 get_memory_rtx (tree exp
, tree len
)
1364 tree orig_exp
= exp
, base
;
1367 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1368 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1369 if (TREE_CODE (exp
) == SAVE_EXPR
&& !SAVE_EXPR_RESOLVED_P (exp
))
1370 exp
= TREE_OPERAND (exp
, 0);
1372 addr
= expand_expr (orig_exp
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
1373 mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
1375 /* Get an expression we can use to find the attributes to assign to MEM.
1376 First remove any nops. */
1377 while (CONVERT_EXPR_P (exp
)
1378 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
1379 exp
= TREE_OPERAND (exp
, 0);
1381 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1382 (as builtin stringops may alias with anything). */
1383 exp
= fold_build2 (MEM_REF
,
1384 build_array_type (char_type_node
,
1385 build_range_type (sizetype
,
1386 size_one_node
, len
)),
1387 exp
, build_int_cst (ptr_type_node
, 0));
1389 /* If the MEM_REF has no acceptable address, try to get the base object
1390 from the original address we got, and build an all-aliasing
1391 unknown-sized access to that one. */
1392 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp
, 0)))
1393 set_mem_attributes (mem
, exp
, 0);
1394 else if (TREE_CODE (TREE_OPERAND (exp
, 0)) == ADDR_EXPR
1395 && (base
= get_base_address (TREE_OPERAND (TREE_OPERAND (exp
, 0),
1398 unsigned int align
= get_pointer_alignment (TREE_OPERAND (exp
, 0));
1399 exp
= build_fold_addr_expr (base
);
1400 exp
= fold_build2 (MEM_REF
,
1401 build_array_type (char_type_node
,
1402 build_range_type (sizetype
,
1405 exp
, build_int_cst (ptr_type_node
, 0));
1406 set_mem_attributes (mem
, exp
, 0);
1407 /* Since we stripped parts make sure the offset is unknown and the
1408 alignment is computed from the original address. */
1409 clear_mem_offset (mem
);
1410 set_mem_align (mem
, align
);
1412 set_mem_alias_set (mem
, 0);
1416 /* Built-in functions to perform an untyped call and return. */
1418 #define apply_args_mode \
1419 (this_target_builtins->x_apply_args_mode)
1420 #define apply_result_mode \
1421 (this_target_builtins->x_apply_result_mode)
1423 /* Return the size required for the block returned by __builtin_apply_args,
1424 and initialize apply_args_mode. */
1427 apply_args_size (void)
1429 static int size
= -1;
1433 /* The values computed by this function never change. */
1436 /* The first value is the incoming arg-pointer. */
1437 size
= GET_MODE_SIZE (Pmode
);
1439 /* The second value is the structure value address unless this is
1440 passed as an "invisible" first argument. */
1441 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1442 size
+= GET_MODE_SIZE (Pmode
);
1444 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1445 if (FUNCTION_ARG_REGNO_P (regno
))
1447 fixed_size_mode mode
= targetm
.calls
.get_raw_arg_mode (regno
);
1449 if (mode
!= VOIDmode
)
1451 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1452 if (size
% align
!= 0)
1453 size
= CEIL (size
, align
) * align
;
1454 size
+= GET_MODE_SIZE (mode
);
1455 apply_args_mode
[regno
] = mode
;
1458 apply_args_mode
[regno
] = as_a
<fixed_size_mode
> (VOIDmode
);
1461 apply_args_mode
[regno
] = as_a
<fixed_size_mode
> (VOIDmode
);
1466 /* Return the size required for the block returned by __builtin_apply,
1467 and initialize apply_result_mode. */
1470 apply_result_size (void)
1472 static int size
= -1;
1475 /* The values computed by this function never change. */
1480 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1481 if (targetm
.calls
.function_value_regno_p (regno
))
1483 fixed_size_mode mode
= targetm
.calls
.get_raw_result_mode (regno
);
1485 if (mode
!= VOIDmode
)
1487 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1488 if (size
% align
!= 0)
1489 size
= CEIL (size
, align
) * align
;
1490 size
+= GET_MODE_SIZE (mode
);
1491 apply_result_mode
[regno
] = mode
;
1494 apply_result_mode
[regno
] = as_a
<fixed_size_mode
> (VOIDmode
);
1497 apply_result_mode
[regno
] = as_a
<fixed_size_mode
> (VOIDmode
);
1499 /* Allow targets that use untyped_call and untyped_return to override
1500 the size so that machine-specific information can be stored here. */
1501 #ifdef APPLY_RESULT_SIZE
1502 size
= APPLY_RESULT_SIZE
;
1508 /* Create a vector describing the result block RESULT. If SAVEP is true,
1509 the result block is used to save the values; otherwise it is used to
1510 restore the values. */
1513 result_vector (int savep
, rtx result
)
1515 int regno
, size
, align
, nelts
;
1516 fixed_size_mode mode
;
1518 rtx
*savevec
= XALLOCAVEC (rtx
, FIRST_PSEUDO_REGISTER
);
1521 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1522 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1524 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1525 if (size
% align
!= 0)
1526 size
= CEIL (size
, align
) * align
;
1527 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
1528 mem
= adjust_address (result
, mode
, size
);
1529 savevec
[nelts
++] = (savep
1530 ? gen_rtx_SET (mem
, reg
)
1531 : gen_rtx_SET (reg
, mem
));
1532 size
+= GET_MODE_SIZE (mode
);
1534 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
1537 /* Save the state required to perform an untyped call with the same
1538 arguments as were passed to the current function. */
1541 expand_builtin_apply_args_1 (void)
1544 int size
, align
, regno
;
1545 fixed_size_mode mode
;
1546 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
1548 /* Create a block where the arg-pointer, structure value address,
1549 and argument registers can be saved. */
1550 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
1552 /* Walk past the arg-pointer and structure value address. */
1553 size
= GET_MODE_SIZE (Pmode
);
1554 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1555 size
+= GET_MODE_SIZE (Pmode
);
1557 /* Save each register used in calling a function to the block. */
1558 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1559 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1561 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1562 if (size
% align
!= 0)
1563 size
= CEIL (size
, align
) * align
;
1565 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1567 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
1568 size
+= GET_MODE_SIZE (mode
);
1571 /* Save the arg pointer to the block. */
1572 tem
= copy_to_reg (crtl
->args
.internal_arg_pointer
);
1573 /* We need the pointer as the caller actually passed them to us, not
1574 as we might have pretended they were passed. Make sure it's a valid
1575 operand, as emit_move_insn isn't expected to handle a PLUS. */
1576 if (STACK_GROWS_DOWNWARD
)
1578 = force_operand (plus_constant (Pmode
, tem
,
1579 crtl
->args
.pretend_args_size
),
1581 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
1583 size
= GET_MODE_SIZE (Pmode
);
1585 /* Save the structure value address unless this is passed as an
1586 "invisible" first argument. */
1587 if (struct_incoming_value
)
1588 emit_move_insn (adjust_address (registers
, Pmode
, size
),
1589 copy_to_reg (struct_incoming_value
));
1591 /* Return the address of the block. */
1592 return copy_addr_to_reg (XEXP (registers
, 0));
1595 /* __builtin_apply_args returns block of memory allocated on
1596 the stack into which is stored the arg pointer, structure
1597 value address, static chain, and all the registers that might
1598 possibly be used in performing a function call. The code is
1599 moved to the start of the function so the incoming values are
1603 expand_builtin_apply_args (void)
1605 /* Don't do __builtin_apply_args more than once in a function.
1606 Save the result of the first call and reuse it. */
1607 if (apply_args_value
!= 0)
1608 return apply_args_value
;
1610 /* When this function is called, it means that registers must be
1611 saved on entry to this function. So we migrate the
1612 call to the first insn of this function. */
1616 temp
= expand_builtin_apply_args_1 ();
1617 rtx_insn
*seq
= get_insns ();
1620 apply_args_value
= temp
;
1622 /* Put the insns after the NOTE that starts the function.
1623 If this is inside a start_sequence, make the outer-level insn
1624 chain current, so the code is placed at the start of the
1625 function. If internal_arg_pointer is a non-virtual pseudo,
1626 it needs to be placed after the function that initializes
1628 push_topmost_sequence ();
1629 if (REG_P (crtl
->args
.internal_arg_pointer
)
1630 && REGNO (crtl
->args
.internal_arg_pointer
) > LAST_VIRTUAL_REGISTER
)
1631 emit_insn_before (seq
, parm_birth_insn
);
1633 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
1634 pop_topmost_sequence ();
1639 /* Perform an untyped call and save the state required to perform an
1640 untyped return of whatever value was returned by the given function. */
1643 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
1645 int size
, align
, regno
;
1646 fixed_size_mode mode
;
1647 rtx incoming_args
, result
, reg
, dest
, src
;
1648 rtx_call_insn
*call_insn
;
1649 rtx old_stack_level
= 0;
1650 rtx call_fusage
= 0;
1651 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
1653 arguments
= convert_memory_address (Pmode
, arguments
);
1655 /* Create a block where the return registers can be saved. */
1656 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
1658 /* Fetch the arg pointer from the ARGUMENTS block. */
1659 incoming_args
= gen_reg_rtx (Pmode
);
1660 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
1661 if (!STACK_GROWS_DOWNWARD
)
1662 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
1663 incoming_args
, 0, OPTAB_LIB_WIDEN
);
1665 /* Push a new argument block and copy the arguments. Do not allow
1666 the (potential) memcpy call below to interfere with our stack
1668 do_pending_stack_adjust ();
1671 /* Save the stack with nonlocal if available. */
1672 if (targetm
.have_save_stack_nonlocal ())
1673 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
);
1675 emit_stack_save (SAVE_BLOCK
, &old_stack_level
);
1677 /* Allocate a block of memory onto the stack and copy the memory
1678 arguments to the outgoing arguments address. We can pass TRUE
1679 as the 4th argument because we just saved the stack pointer
1680 and will restore it right after the call. */
1681 allocate_dynamic_stack_space (argsize
, 0, BIGGEST_ALIGNMENT
, -1, true);
1683 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1684 may have already set current_function_calls_alloca to true.
1685 current_function_calls_alloca won't be set if argsize is zero,
1686 so we have to guarantee need_drap is true here. */
1687 if (SUPPORTS_STACK_ALIGNMENT
)
1688 crtl
->need_drap
= true;
1690 dest
= virtual_outgoing_args_rtx
;
1691 if (!STACK_GROWS_DOWNWARD
)
1693 if (CONST_INT_P (argsize
))
1694 dest
= plus_constant (Pmode
, dest
, -INTVAL (argsize
));
1696 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
1698 dest
= gen_rtx_MEM (BLKmode
, dest
);
1699 set_mem_align (dest
, PARM_BOUNDARY
);
1700 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
1701 set_mem_align (src
, PARM_BOUNDARY
);
1702 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
1704 /* Refer to the argument block. */
1706 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
1707 set_mem_align (arguments
, PARM_BOUNDARY
);
1709 /* Walk past the arg-pointer and structure value address. */
1710 size
= GET_MODE_SIZE (Pmode
);
1712 size
+= GET_MODE_SIZE (Pmode
);
1714 /* Restore each of the registers previously saved. Make USE insns
1715 for each of these registers for use in making the call. */
1716 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1717 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1719 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1720 if (size
% align
!= 0)
1721 size
= CEIL (size
, align
) * align
;
1722 reg
= gen_rtx_REG (mode
, regno
);
1723 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
1724 use_reg (&call_fusage
, reg
);
1725 size
+= GET_MODE_SIZE (mode
);
1728 /* Restore the structure value address unless this is passed as an
1729 "invisible" first argument. */
1730 size
= GET_MODE_SIZE (Pmode
);
1733 rtx value
= gen_reg_rtx (Pmode
);
1734 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
1735 emit_move_insn (struct_value
, value
);
1736 if (REG_P (struct_value
))
1737 use_reg (&call_fusage
, struct_value
);
1740 /* All arguments and registers used for the call are set up by now! */
1741 function
= prepare_call_address (NULL
, function
, NULL
, &call_fusage
, 0, 0);
1743 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1744 and we don't want to load it into a register as an optimization,
1745 because prepare_call_address already did it if it should be done. */
1746 if (GET_CODE (function
) != SYMBOL_REF
)
1747 function
= memory_address (FUNCTION_MODE
, function
);
1749 /* Generate the actual call instruction and save the return value. */
1750 if (targetm
.have_untyped_call ())
1752 rtx mem
= gen_rtx_MEM (FUNCTION_MODE
, function
);
1753 rtx_insn
*seq
= targetm
.gen_untyped_call (mem
, result
,
1754 result_vector (1, result
));
1755 for (rtx_insn
*insn
= seq
; insn
; insn
= NEXT_INSN (insn
))
1757 add_reg_note (insn
, REG_UNTYPED_CALL
, NULL_RTX
);
1760 else if (targetm
.have_call_value ())
1764 /* Locate the unique return register. It is not possible to
1765 express a call that sets more than one return register using
1766 call_value; use untyped_call for that. In fact, untyped_call
1767 only needs to save the return registers in the given block. */
1768 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1769 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1771 gcc_assert (!valreg
); /* have_untyped_call required. */
1773 valreg
= gen_rtx_REG (mode
, regno
);
1776 emit_insn (targetm
.gen_call_value (valreg
,
1777 gen_rtx_MEM (FUNCTION_MODE
, function
),
1778 const0_rtx
, NULL_RTX
, const0_rtx
));
1780 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
1785 /* Find the CALL insn we just emitted, and attach the register usage
1787 call_insn
= last_call_insn ();
1788 add_function_usage_to (call_insn
, call_fusage
);
1790 /* Restore the stack. */
1791 if (targetm
.have_save_stack_nonlocal ())
1792 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
);
1794 emit_stack_restore (SAVE_BLOCK
, old_stack_level
);
1795 fixup_args_size_notes (call_insn
, get_last_insn (), 0);
1799 /* Return the address of the result block. */
1800 result
= copy_addr_to_reg (XEXP (result
, 0));
1801 return convert_memory_address (ptr_mode
, result
);
1804 /* Perform an untyped return. */
1807 expand_builtin_return (rtx result
)
1809 int size
, align
, regno
;
1810 fixed_size_mode mode
;
1812 rtx_insn
*call_fusage
= 0;
1814 result
= convert_memory_address (Pmode
, result
);
1816 apply_result_size ();
1817 result
= gen_rtx_MEM (BLKmode
, result
);
1819 if (targetm
.have_untyped_return ())
1821 rtx vector
= result_vector (0, result
);
1822 emit_jump_insn (targetm
.gen_untyped_return (result
, vector
));
1827 /* Restore the return value and note that each value is used. */
1829 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1830 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1832 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1833 if (size
% align
!= 0)
1834 size
= CEIL (size
, align
) * align
;
1835 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1836 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
1838 push_to_sequence (call_fusage
);
1840 call_fusage
= get_insns ();
1842 size
+= GET_MODE_SIZE (mode
);
1845 /* Put the USE insns before the return. */
1846 emit_insn (call_fusage
);
1848 /* Return whatever values was restored by jumping directly to the end
1850 expand_naked_return ();
1853 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1855 static enum type_class
1856 type_to_class (tree type
)
1858 switch (TREE_CODE (type
))
1860 case VOID_TYPE
: return void_type_class
;
1861 case INTEGER_TYPE
: return integer_type_class
;
1862 case ENUMERAL_TYPE
: return enumeral_type_class
;
1863 case BOOLEAN_TYPE
: return boolean_type_class
;
1864 case POINTER_TYPE
: return pointer_type_class
;
1865 case REFERENCE_TYPE
: return reference_type_class
;
1866 case OFFSET_TYPE
: return offset_type_class
;
1867 case REAL_TYPE
: return real_type_class
;
1868 case COMPLEX_TYPE
: return complex_type_class
;
1869 case FUNCTION_TYPE
: return function_type_class
;
1870 case METHOD_TYPE
: return method_type_class
;
1871 case RECORD_TYPE
: return record_type_class
;
1873 case QUAL_UNION_TYPE
: return union_type_class
;
1874 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
1875 ? string_type_class
: array_type_class
);
1876 case LANG_TYPE
: return lang_type_class
;
1877 case OPAQUE_TYPE
: return opaque_type_class
;
1878 default: return no_type_class
;
1882 /* Expand a call EXP to __builtin_classify_type. */
1885 expand_builtin_classify_type (tree exp
)
1887 if (call_expr_nargs (exp
))
1888 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))));
1889 return GEN_INT (no_type_class
);
1892 /* This helper macro, meant to be used in mathfn_built_in below, determines
1893 which among a set of builtin math functions is appropriate for a given type
1894 mode. The `F' (float) and `L' (long double) are automatically generated
1895 from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1896 types, there are additional types that are considered with 'F32', 'F64',
1897 'F128', etc. suffixes. */
1898 #define CASE_MATHFN(MATHFN) \
1899 CASE_CFN_##MATHFN: \
1900 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1901 fcodel = BUILT_IN_##MATHFN##L ; break;
1902 /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1904 #define CASE_MATHFN_FLOATN(MATHFN) \
1905 CASE_CFN_##MATHFN: \
1906 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1907 fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1908 fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1909 fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1910 fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1912 /* Similar to above, but appends _R after any F/L suffix. */
1913 #define CASE_MATHFN_REENT(MATHFN) \
1914 case CFN_BUILT_IN_##MATHFN##_R: \
1915 case CFN_BUILT_IN_##MATHFN##F_R: \
1916 case CFN_BUILT_IN_##MATHFN##L_R: \
1917 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1918 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1920 /* Return a function equivalent to FN but operating on floating-point
1921 values of type TYPE, or END_BUILTINS if no such function exists.
1922 This is purely an operation on function codes; it does not guarantee
1923 that the target actually has an implementation of the function. */
1925 static built_in_function
1926 mathfn_built_in_2 (tree type
, combined_fn fn
)
1929 built_in_function fcode
, fcodef
, fcodel
;
1930 built_in_function fcodef16
= END_BUILTINS
;
1931 built_in_function fcodef32
= END_BUILTINS
;
1932 built_in_function fcodef64
= END_BUILTINS
;
1933 built_in_function fcodef128
= END_BUILTINS
;
1934 built_in_function fcodef32x
= END_BUILTINS
;
1935 built_in_function fcodef64x
= END_BUILTINS
;
1936 built_in_function fcodef128x
= END_BUILTINS
;
1938 /* If <math.h> has been included somehow, HUGE_VAL and NAN definitions
1939 break the uses below. */
1945 #define SEQ_OF_CASE_MATHFN \
1946 CASE_MATHFN_FLOATN (ACOS) \
1947 CASE_MATHFN_FLOATN (ACOSH) \
1948 CASE_MATHFN_FLOATN (ASIN) \
1949 CASE_MATHFN_FLOATN (ASINH) \
1950 CASE_MATHFN_FLOATN (ATAN) \
1951 CASE_MATHFN_FLOATN (ATAN2) \
1952 CASE_MATHFN_FLOATN (ATANH) \
1953 CASE_MATHFN_FLOATN (CBRT) \
1954 CASE_MATHFN_FLOATN (CEIL) \
1955 CASE_MATHFN (CEXPI) \
1956 CASE_MATHFN_FLOATN (COPYSIGN) \
1957 CASE_MATHFN_FLOATN (COS) \
1958 CASE_MATHFN_FLOATN (COSH) \
1959 CASE_MATHFN (DREM) \
1960 CASE_MATHFN_FLOATN (ERF) \
1961 CASE_MATHFN_FLOATN (ERFC) \
1962 CASE_MATHFN_FLOATN (EXP) \
1963 CASE_MATHFN (EXP10) \
1964 CASE_MATHFN_FLOATN (EXP2) \
1965 CASE_MATHFN_FLOATN (EXPM1) \
1966 CASE_MATHFN_FLOATN (FABS) \
1967 CASE_MATHFN_FLOATN (FDIM) \
1968 CASE_MATHFN_FLOATN (FLOOR) \
1969 CASE_MATHFN_FLOATN (FMA) \
1970 CASE_MATHFN_FLOATN (FMAX) \
1971 CASE_MATHFN_FLOATN (FMIN) \
1972 CASE_MATHFN_FLOATN (FMOD) \
1973 CASE_MATHFN_FLOATN (FREXP) \
1974 CASE_MATHFN (GAMMA) \
1975 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ \
1976 CASE_MATHFN_FLOATN (HUGE_VAL) \
1977 CASE_MATHFN_FLOATN (HYPOT) \
1978 CASE_MATHFN_FLOATN (ILOGB) \
1979 CASE_MATHFN (ICEIL) \
1980 CASE_MATHFN (IFLOOR) \
1981 CASE_MATHFN_FLOATN (INF) \
1982 CASE_MATHFN (IRINT) \
1983 CASE_MATHFN (IROUND) \
1984 CASE_MATHFN (ISINF) \
1988 CASE_MATHFN (LCEIL) \
1989 CASE_MATHFN_FLOATN (LDEXP) \
1990 CASE_MATHFN (LFLOOR) \
1991 CASE_MATHFN_FLOATN (LGAMMA) \
1992 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ \
1993 CASE_MATHFN (LLCEIL) \
1994 CASE_MATHFN (LLFLOOR) \
1995 CASE_MATHFN_FLOATN (LLRINT) \
1996 CASE_MATHFN_FLOATN (LLROUND) \
1997 CASE_MATHFN_FLOATN (LOG) \
1998 CASE_MATHFN_FLOATN (LOG10) \
1999 CASE_MATHFN_FLOATN (LOG1P) \
2000 CASE_MATHFN_FLOATN (LOG2) \
2001 CASE_MATHFN_FLOATN (LOGB) \
2002 CASE_MATHFN_FLOATN (LRINT) \
2003 CASE_MATHFN_FLOATN (LROUND) \
2004 CASE_MATHFN_FLOATN (MODF) \
2005 CASE_MATHFN_FLOATN (NAN) \
2006 CASE_MATHFN_FLOATN (NANS) \
2007 CASE_MATHFN_FLOATN (NEARBYINT) \
2008 CASE_MATHFN_FLOATN (NEXTAFTER) \
2009 CASE_MATHFN (NEXTTOWARD) \
2010 CASE_MATHFN_FLOATN (POW) \
2011 CASE_MATHFN (POWI) \
2012 CASE_MATHFN (POW10) \
2013 CASE_MATHFN_FLOATN (REMAINDER) \
2014 CASE_MATHFN_FLOATN (REMQUO) \
2015 CASE_MATHFN_FLOATN (RINT) \
2016 CASE_MATHFN_FLOATN (ROUND) \
2017 CASE_MATHFN_FLOATN (ROUNDEVEN) \
2018 CASE_MATHFN (SCALB) \
2019 CASE_MATHFN_FLOATN (SCALBLN) \
2020 CASE_MATHFN_FLOATN (SCALBN) \
2021 CASE_MATHFN (SIGNBIT) \
2022 CASE_MATHFN (SIGNIFICAND) \
2023 CASE_MATHFN_FLOATN (SIN) \
2024 CASE_MATHFN (SINCOS) \
2025 CASE_MATHFN_FLOATN (SINH) \
2026 CASE_MATHFN_FLOATN (SQRT) \
2027 CASE_MATHFN_FLOATN (TAN) \
2028 CASE_MATHFN_FLOATN (TANH) \
2029 CASE_MATHFN_FLOATN (TGAMMA) \
2030 CASE_MATHFN_FLOATN (TRUNC) \
2038 return END_BUILTINS
;
2041 mtype
= TYPE_MAIN_VARIANT (type
);
2042 if (mtype
== double_type_node
)
2044 else if (mtype
== float_type_node
)
2046 else if (mtype
== long_double_type_node
)
2048 else if (mtype
== float16_type_node
)
2050 else if (mtype
== float32_type_node
)
2052 else if (mtype
== float64_type_node
)
2054 else if (mtype
== float128_type_node
)
2056 else if (mtype
== float32x_type_node
)
2058 else if (mtype
== float64x_type_node
)
2060 else if (mtype
== float128x_type_node
)
2063 return END_BUILTINS
;
2067 #undef CASE_MATHFN_FLOATN
2068 #undef CASE_MATHFN_REENT
2070 /* Return mathematic function equivalent to FN but operating directly on TYPE,
2071 if available. If IMPLICIT_P is true use the implicit builtin declaration,
2072 otherwise use the explicit declaration. If we can't do the conversion,
2076 mathfn_built_in_1 (tree type
, combined_fn fn
, bool implicit_p
)
2078 built_in_function fcode2
= mathfn_built_in_2 (type
, fn
);
2079 if (fcode2
== END_BUILTINS
)
2082 if (implicit_p
&& !builtin_decl_implicit_p (fcode2
))
2085 return builtin_decl_explicit (fcode2
);
2088 /* Like mathfn_built_in_1, but always use the implicit array. */
2091 mathfn_built_in (tree type
, combined_fn fn
)
2093 return mathfn_built_in_1 (type
, fn
, /*implicit=*/ 1);
2096 /* Like mathfn_built_in_1, but always use the explicit array. */
2099 mathfn_built_in_explicit (tree type
, combined_fn fn
)
2101 return mathfn_built_in_1 (type
, fn
, /*implicit=*/ 0);
2104 /* Like mathfn_built_in_1, but take a built_in_function and
2105 always use the implicit array. */
2108 mathfn_built_in (tree type
, enum built_in_function fn
)
2110 return mathfn_built_in_1 (type
, as_combined_fn (fn
), /*implicit=*/ 1);
2113 /* Return the type associated with a built in function, i.e., the one
2114 to be passed to mathfn_built_in to get the type-specific
2118 mathfn_built_in_type (combined_fn fn
)
2120 #define CASE_MATHFN(MATHFN) \
2121 case CFN_BUILT_IN_##MATHFN: \
2122 return double_type_node; \
2123 case CFN_BUILT_IN_##MATHFN##F: \
2124 return float_type_node; \
2125 case CFN_BUILT_IN_##MATHFN##L: \
2126 return long_double_type_node;
2128 #define CASE_MATHFN_FLOATN(MATHFN) \
2129 CASE_MATHFN(MATHFN) \
2130 case CFN_BUILT_IN_##MATHFN##F16: \
2131 return float16_type_node; \
2132 case CFN_BUILT_IN_##MATHFN##F32: \
2133 return float32_type_node; \
2134 case CFN_BUILT_IN_##MATHFN##F64: \
2135 return float64_type_node; \
2136 case CFN_BUILT_IN_##MATHFN##F128: \
2137 return float128_type_node; \
2138 case CFN_BUILT_IN_##MATHFN##F32X: \
2139 return float32x_type_node; \
2140 case CFN_BUILT_IN_##MATHFN##F64X: \
2141 return float64x_type_node; \
2142 case CFN_BUILT_IN_##MATHFN##F128X: \
2143 return float128x_type_node;
2145 /* Similar to above, but appends _R after any F/L suffix. */
2146 #define CASE_MATHFN_REENT(MATHFN) \
2147 case CFN_BUILT_IN_##MATHFN##_R: \
2148 return double_type_node; \
2149 case CFN_BUILT_IN_##MATHFN##F_R: \
2150 return float_type_node; \
2151 case CFN_BUILT_IN_##MATHFN##L_R: \
2152 return long_double_type_node;
2163 #undef CASE_MATHFN_FLOATN
2164 #undef CASE_MATHFN_REENT
2165 #undef SEQ_OF_CASE_MATHFN
2168 /* Check whether there is an internal function associated with function FN
2169 and return type RETURN_TYPE. Return the function if so, otherwise return
2172 Note that this function only tests whether the function is defined in
2173 internals.def, not whether it is actually available on the target. */
2176 associated_internal_fn (built_in_function fn
, tree return_type
)
2180 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2181 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2182 #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2183 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2184 CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2185 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2186 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2187 #include "internal-fn.def"
2189 CASE_FLT_FN (BUILT_IN_POW10
):
2192 CASE_FLT_FN (BUILT_IN_DREM
):
2193 return IFN_REMAINDER
;
2195 CASE_FLT_FN (BUILT_IN_SCALBN
):
2196 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2197 if (REAL_MODE_FORMAT (TYPE_MODE (return_type
))->b
== 2)
2206 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2207 return its code, otherwise return IFN_LAST. Note that this function
2208 only tests whether the function is defined in internals.def, not whether
2209 it is actually available on the target. */
2212 associated_internal_fn (tree fndecl
)
2214 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
);
2215 return associated_internal_fn (DECL_FUNCTION_CODE (fndecl
),
2216 TREE_TYPE (TREE_TYPE (fndecl
)));
2219 /* Check whether there is an internal function associated with function CFN
2220 and return type RETURN_TYPE. Return the function if so, otherwise return
2223 Note that this function only tests whether the function is defined in
2224 internals.def, not whether it is actually available on the target. */
2227 associated_internal_fn (combined_fn cfn
, tree return_type
)
2229 if (internal_fn_p (cfn
))
2230 return as_internal_fn (cfn
);
2231 return associated_internal_fn (as_builtin_fn (cfn
), return_type
);
2234 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2235 on the current target by a call to an internal function, return the
2236 code of that internal function, otherwise return IFN_LAST. The caller
2237 is responsible for ensuring that any side-effects of the built-in
2238 call are dealt with correctly. E.g. if CALL sets errno, the caller
2239 must decide that the errno result isn't needed or make it available
2240 in some other way. */
2243 replacement_internal_fn (gcall
*call
)
2245 if (gimple_call_builtin_p (call
, BUILT_IN_NORMAL
))
2247 internal_fn ifn
= associated_internal_fn (gimple_call_fndecl (call
));
2248 if (ifn
!= IFN_LAST
)
2250 tree_pair types
= direct_internal_fn_types (ifn
, call
);
2251 optimization_type opt_type
= bb_optimization_type (gimple_bb (call
));
2252 if (direct_internal_fn_supported_p (ifn
, types
, opt_type
))
2259 /* Expand a call to the builtin trinary math functions (fma).
2260 Return NULL_RTX if a normal call should be emitted rather than expanding the
2261 function in-line. EXP is the expression that is a call to the builtin
2262 function; if convenient, the result should be placed in TARGET.
2263 SUBTARGET may be used as the target for computing one of EXP's
2267 expand_builtin_mathfn_ternary (tree exp
, rtx target
, rtx subtarget
)
2269 optab builtin_optab
;
2270 rtx op0
, op1
, op2
, result
;
2272 tree fndecl
= get_callee_fndecl (exp
);
2273 tree arg0
, arg1
, arg2
;
2276 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
2279 arg0
= CALL_EXPR_ARG (exp
, 0);
2280 arg1
= CALL_EXPR_ARG (exp
, 1);
2281 arg2
= CALL_EXPR_ARG (exp
, 2);
2283 switch (DECL_FUNCTION_CODE (fndecl
))
2285 CASE_FLT_FN (BUILT_IN_FMA
):
2286 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA
):
2287 builtin_optab
= fma_optab
; break;
2292 /* Make a suitable register to place result in. */
2293 mode
= TYPE_MODE (TREE_TYPE (exp
));
2295 /* Before working hard, check whether the instruction is available. */
2296 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2299 result
= gen_reg_rtx (mode
);
2301 /* Always stabilize the argument list. */
2302 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2303 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2304 CALL_EXPR_ARG (exp
, 2) = arg2
= builtin_save_expr (arg2
);
2306 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2307 op1
= expand_normal (arg1
);
2308 op2
= expand_normal (arg2
);
2312 /* Compute into RESULT.
2313 Set RESULT to wherever the result comes back. */
2314 result
= expand_ternary_op (mode
, builtin_optab
, op0
, op1
, op2
,
2317 /* If we were unable to expand via the builtin, stop the sequence
2318 (without outputting the insns) and call to the library function
2319 with the stabilized argument list. */
2323 return expand_call (exp
, target
, target
== const0_rtx
);
2326 /* Output the entire sequence. */
2327 insns
= get_insns ();
2334 /* Expand a call to the builtin sin and cos math functions.
2335 Return NULL_RTX if a normal call should be emitted rather than expanding the
2336 function in-line. EXP is the expression that is a call to the builtin
2337 function; if convenient, the result should be placed in TARGET.
2338 SUBTARGET may be used as the target for computing one of EXP's
2342 expand_builtin_mathfn_3 (tree exp
, rtx target
, rtx subtarget
)
2344 optab builtin_optab
;
2347 tree fndecl
= get_callee_fndecl (exp
);
2351 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2354 arg
= CALL_EXPR_ARG (exp
, 0);
2356 switch (DECL_FUNCTION_CODE (fndecl
))
2358 CASE_FLT_FN (BUILT_IN_SIN
):
2359 CASE_FLT_FN (BUILT_IN_COS
):
2360 builtin_optab
= sincos_optab
; break;
2365 /* Make a suitable register to place result in. */
2366 mode
= TYPE_MODE (TREE_TYPE (exp
));
2368 /* Check if sincos insn is available, otherwise fallback
2369 to sin or cos insn. */
2370 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2371 switch (DECL_FUNCTION_CODE (fndecl
))
2373 CASE_FLT_FN (BUILT_IN_SIN
):
2374 builtin_optab
= sin_optab
; break;
2375 CASE_FLT_FN (BUILT_IN_COS
):
2376 builtin_optab
= cos_optab
; break;
2381 /* Before working hard, check whether the instruction is available. */
2382 if (optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
)
2384 rtx result
= gen_reg_rtx (mode
);
2386 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2387 need to expand the argument again. This way, we will not perform
2388 side-effects more the once. */
2389 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2391 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2395 /* Compute into RESULT.
2396 Set RESULT to wherever the result comes back. */
2397 if (builtin_optab
== sincos_optab
)
2401 switch (DECL_FUNCTION_CODE (fndecl
))
2403 CASE_FLT_FN (BUILT_IN_SIN
):
2404 ok
= expand_twoval_unop (builtin_optab
, op0
, 0, result
, 0);
2406 CASE_FLT_FN (BUILT_IN_COS
):
2407 ok
= expand_twoval_unop (builtin_optab
, op0
, result
, 0, 0);
2415 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2419 /* Output the entire sequence. */
2420 insns
= get_insns ();
2426 /* If we were unable to expand via the builtin, stop the sequence
2427 (without outputting the insns) and call to the library function
2428 with the stabilized argument list. */
2432 return expand_call (exp
, target
, target
== const0_rtx
);
2435 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2436 return an RTL instruction code that implements the functionality.
2437 If that isn't possible or available return CODE_FOR_nothing. */
2439 static enum insn_code
2440 interclass_mathfn_icode (tree arg
, tree fndecl
)
2442 bool errno_set
= false;
2443 optab builtin_optab
= unknown_optab
;
2446 switch (DECL_FUNCTION_CODE (fndecl
))
2448 CASE_FLT_FN (BUILT_IN_ILOGB
):
2449 errno_set
= true; builtin_optab
= ilogb_optab
; break;
2450 CASE_FLT_FN (BUILT_IN_ISINF
):
2451 builtin_optab
= isinf_optab
; break;
2452 case BUILT_IN_ISNORMAL
:
2453 case BUILT_IN_ISFINITE
:
2454 CASE_FLT_FN (BUILT_IN_FINITE
):
2455 case BUILT_IN_FINITED32
:
2456 case BUILT_IN_FINITED64
:
2457 case BUILT_IN_FINITED128
:
2458 case BUILT_IN_ISINFD32
:
2459 case BUILT_IN_ISINFD64
:
2460 case BUILT_IN_ISINFD128
:
2461 /* These builtins have no optabs (yet). */
2467 /* There's no easy way to detect the case we need to set EDOM. */
2468 if (flag_errno_math
&& errno_set
)
2469 return CODE_FOR_nothing
;
2471 /* Optab mode depends on the mode of the input argument. */
2472 mode
= TYPE_MODE (TREE_TYPE (arg
));
2475 return optab_handler (builtin_optab
, mode
);
2476 return CODE_FOR_nothing
;
2479 /* Expand a call to one of the builtin math functions that operate on
2480 floating point argument and output an integer result (ilogb, isinf,
2482 Return 0 if a normal call should be emitted rather than expanding the
2483 function in-line. EXP is the expression that is a call to the builtin
2484 function; if convenient, the result should be placed in TARGET. */
2487 expand_builtin_interclass_mathfn (tree exp
, rtx target
)
2489 enum insn_code icode
= CODE_FOR_nothing
;
2491 tree fndecl
= get_callee_fndecl (exp
);
2495 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2498 arg
= CALL_EXPR_ARG (exp
, 0);
2499 icode
= interclass_mathfn_icode (arg
, fndecl
);
2500 mode
= TYPE_MODE (TREE_TYPE (arg
));
2502 if (icode
!= CODE_FOR_nothing
)
2504 class expand_operand ops
[1];
2505 rtx_insn
*last
= get_last_insn ();
2506 tree orig_arg
= arg
;
2508 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2509 need to expand the argument again. This way, we will not perform
2510 side-effects more the once. */
2511 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2513 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2515 if (mode
!= GET_MODE (op0
))
2516 op0
= convert_to_mode (mode
, op0
, 0);
2518 create_output_operand (&ops
[0], target
, TYPE_MODE (TREE_TYPE (exp
)));
2519 if (maybe_legitimize_operands (icode
, 0, 1, ops
)
2520 && maybe_emit_unop_insn (icode
, ops
[0].value
, op0
, UNKNOWN
))
2521 return ops
[0].value
;
2523 delete_insns_since (last
);
2524 CALL_EXPR_ARG (exp
, 0) = orig_arg
;
2530 /* Expand a call to the builtin sincos math function.
2531 Return NULL_RTX if a normal call should be emitted rather than expanding the
2532 function in-line. EXP is the expression that is a call to the builtin
2536 expand_builtin_sincos (tree exp
)
2538 rtx op0
, op1
, op2
, target1
, target2
;
2540 tree arg
, sinp
, cosp
;
2542 location_t loc
= EXPR_LOCATION (exp
);
2543 tree alias_type
, alias_off
;
2545 if (!validate_arglist (exp
, REAL_TYPE
,
2546 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2549 arg
= CALL_EXPR_ARG (exp
, 0);
2550 sinp
= CALL_EXPR_ARG (exp
, 1);
2551 cosp
= CALL_EXPR_ARG (exp
, 2);
2553 /* Make a suitable register to place result in. */
2554 mode
= TYPE_MODE (TREE_TYPE (arg
));
2556 /* Check if sincos insn is available, otherwise emit the call. */
2557 if (optab_handler (sincos_optab
, mode
) == CODE_FOR_nothing
)
2560 target1
= gen_reg_rtx (mode
);
2561 target2
= gen_reg_rtx (mode
);
2563 op0
= expand_normal (arg
);
2564 alias_type
= build_pointer_type_for_mode (TREE_TYPE (arg
), ptr_mode
, true);
2565 alias_off
= build_int_cst (alias_type
, 0);
2566 op1
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2568 op2
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2571 /* Compute into target1 and target2.
2572 Set TARGET to wherever the result comes back. */
2573 result
= expand_twoval_unop (sincos_optab
, op0
, target2
, target1
, 0);
2574 gcc_assert (result
);
2576 /* Move target1 and target2 to the memory locations indicated
2578 emit_move_insn (op1
, target1
);
2579 emit_move_insn (op2
, target2
);
2584 /* Expand call EXP to the fegetround builtin (from C99 fenv.h), returning the
2585 result and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2587 expand_builtin_fegetround (tree exp
, rtx target
, machine_mode target_mode
)
2589 if (!validate_arglist (exp
, VOID_TYPE
))
2592 insn_code icode
= direct_optab_handler (fegetround_optab
, SImode
);
2593 if (icode
== CODE_FOR_nothing
)
2597 || GET_MODE (target
) != target_mode
2598 || !(*insn_data
[icode
].operand
[0].predicate
) (target
, target_mode
))
2599 target
= gen_reg_rtx (target_mode
);
2601 rtx pat
= GEN_FCN (icode
) (target
);
2609 /* Expand call EXP to either feclearexcept or feraiseexcept builtins (from C99
2610 fenv.h), returning the result and setting it in TARGET. Otherwise return
2611 NULL_RTX on failure. */
2613 expand_builtin_feclear_feraise_except (tree exp
, rtx target
,
2614 machine_mode target_mode
, optab op_optab
)
2616 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
2618 rtx op0
= expand_normal (CALL_EXPR_ARG (exp
, 0));
2620 insn_code icode
= direct_optab_handler (op_optab
, SImode
);
2621 if (icode
== CODE_FOR_nothing
)
2624 if (!(*insn_data
[icode
].operand
[1].predicate
) (op0
, GET_MODE (op0
)))
2628 || GET_MODE (target
) != target_mode
2629 || !(*insn_data
[icode
].operand
[0].predicate
) (target
, target_mode
))
2630 target
= gen_reg_rtx (target_mode
);
2632 rtx pat
= GEN_FCN (icode
) (target
, op0
);
2640 /* Expand a call to the internal cexpi builtin to the sincos math function.
2641 EXP is the expression that is a call to the builtin function; if convenient,
2642 the result should be placed in TARGET. */
2645 expand_builtin_cexpi (tree exp
, rtx target
)
2647 tree fndecl
= get_callee_fndecl (exp
);
2651 location_t loc
= EXPR_LOCATION (exp
);
2653 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2656 arg
= CALL_EXPR_ARG (exp
, 0);
2657 type
= TREE_TYPE (arg
);
2658 mode
= TYPE_MODE (TREE_TYPE (arg
));
2660 /* Try expanding via a sincos optab, fall back to emitting a libcall
2661 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2662 is only generated from sincos, cexp or if we have either of them. */
2663 if (optab_handler (sincos_optab
, mode
) != CODE_FOR_nothing
)
2665 op1
= gen_reg_rtx (mode
);
2666 op2
= gen_reg_rtx (mode
);
2668 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2670 /* Compute into op1 and op2. */
2671 expand_twoval_unop (sincos_optab
, op0
, op2
, op1
, 0);
2673 else if (targetm
.libc_has_function (function_sincos
, type
))
2675 tree call
, fn
= NULL_TREE
;
2679 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2680 fn
= builtin_decl_explicit (BUILT_IN_SINCOSF
);
2681 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2682 fn
= builtin_decl_explicit (BUILT_IN_SINCOS
);
2683 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2684 fn
= builtin_decl_explicit (BUILT_IN_SINCOSL
);
2688 op1
= assign_temp (TREE_TYPE (arg
), 1, 1);
2689 op2
= assign_temp (TREE_TYPE (arg
), 1, 1);
2690 op1a
= copy_addr_to_reg (XEXP (op1
, 0));
2691 op2a
= copy_addr_to_reg (XEXP (op2
, 0));
2692 top1
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op1a
);
2693 top2
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op2a
);
2695 /* Make sure not to fold the sincos call again. */
2696 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2697 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn
)),
2698 call
, 3, arg
, top1
, top2
));
2702 tree call
, fn
= NULL_TREE
, narg
;
2703 tree ctype
= build_complex_type (type
);
2705 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2706 fn
= builtin_decl_explicit (BUILT_IN_CEXPF
);
2707 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2708 fn
= builtin_decl_explicit (BUILT_IN_CEXP
);
2709 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2710 fn
= builtin_decl_explicit (BUILT_IN_CEXPL
);
2714 /* If we don't have a decl for cexp create one. This is the
2715 friendliest fallback if the user calls __builtin_cexpi
2716 without full target C99 function support. */
2717 if (fn
== NULL_TREE
)
2720 const char *name
= NULL
;
2722 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2724 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2726 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2729 fntype
= build_function_type_list (ctype
, ctype
, NULL_TREE
);
2730 fn
= build_fn_decl (name
, fntype
);
2733 narg
= fold_build2_loc (loc
, COMPLEX_EXPR
, ctype
,
2734 build_real (type
, dconst0
), arg
);
2736 /* Make sure not to fold the cexp call again. */
2737 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2738 return expand_expr (build_call_nary (ctype
, call
, 1, narg
),
2739 target
, VOIDmode
, EXPAND_NORMAL
);
2742 /* Now build the proper return type. */
2743 return expand_expr (build2 (COMPLEX_EXPR
, build_complex_type (type
),
2744 make_tree (TREE_TYPE (arg
), op2
),
2745 make_tree (TREE_TYPE (arg
), op1
)),
2746 target
, VOIDmode
, EXPAND_NORMAL
);
2749 /* Conveniently construct a function call expression. FNDECL names the
2750 function to be called, N is the number of arguments, and the "..."
2751 parameters are the argument expressions. Unlike build_call_exr
2752 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2755 build_call_nofold_loc (location_t loc
, tree fndecl
, int n
, ...)
2758 tree fntype
= TREE_TYPE (fndecl
);
2759 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), fndecl
);
2762 fn
= build_call_valist (TREE_TYPE (fntype
), fn
, n
, ap
);
2764 SET_EXPR_LOCATION (fn
, loc
);
2768 /* Expand the __builtin_issignaling builtin. This needs to handle
2769 all floating point formats that do support NaNs (for those that
2770 don't it just sets target to 0). */
2773 expand_builtin_issignaling (tree exp
, rtx target
)
2775 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2778 tree arg
= CALL_EXPR_ARG (exp
, 0);
2779 scalar_float_mode fmode
= SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg
));
2780 const struct real_format
*fmt
= REAL_MODE_FORMAT (fmode
);
2782 /* Expand the argument yielding a RTX expression. */
2783 rtx temp
= expand_normal (arg
);
2785 /* If mode doesn't support NaN, always return 0.
2786 Don't use !HONOR_SNANS (fmode) here, so there is some possibility of
2787 __builtin_issignaling working without -fsignaling-nans. Especially
2788 when -fno-signaling-nans is the default.
2789 On the other side, MODE_HAS_NANS (fmode) is unnecessary, with
2790 -ffinite-math-only even __builtin_isnan or __builtin_fpclassify
2791 fold to 0 or non-NaN/Inf classification. */
2792 if (!HONOR_NANS (fmode
))
2794 emit_move_insn (target
, const0_rtx
);
2798 /* Check if the back end provides an insn that handles issignaling for the
2800 enum insn_code icode
= optab_handler (issignaling_optab
, fmode
);
2801 if (icode
!= CODE_FOR_nothing
)
2803 rtx_insn
*last
= get_last_insn ();
2804 rtx this_target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
2805 if (maybe_emit_unop_insn (icode
, this_target
, temp
, UNKNOWN
))
2807 delete_insns_since (last
);
2810 if (DECIMAL_FLOAT_MODE_P (fmode
))
2812 scalar_int_mode imode
;
2814 switch (fmt
->ieee_bits
)
2818 imode
= int_mode_for_mode (fmode
).require ();
2819 temp
= gen_lowpart (imode
, temp
);
2822 imode
= int_mode_for_size (64, 1).require ();
2824 /* For decimal128, TImode support isn't always there and even when
2825 it is, working on the DImode high part is usually better. */
2828 if (rtx t
= simplify_gen_subreg (imode
, temp
, fmode
,
2829 subreg_highpart_offset (imode
,
2834 scalar_int_mode imode2
;
2835 if (int_mode_for_mode (fmode
).exists (&imode2
))
2837 rtx temp2
= gen_lowpart (imode2
, temp
);
2838 poly_uint64 off
= subreg_highpart_offset (imode
, imode2
);
2839 if (rtx t
= simplify_gen_subreg (imode
, temp2
,
2846 rtx mem
= assign_stack_temp (fmode
, GET_MODE_SIZE (fmode
));
2847 emit_move_insn (mem
, temp
);
2854 = subreg_highpart_offset (imode
, GET_MODE (temp
));
2855 hi
= adjust_address (temp
, imode
, offset
);
2862 /* In all of decimal{32,64,128}, there is MSB sign bit and sNaN
2863 have 6 bits below it all set. */
2865 = GEN_INT (HOST_WIDE_INT_C (0x3f) << (GET_MODE_BITSIZE (imode
) - 7));
2866 temp
= expand_binop (imode
, and_optab
, temp
, val
,
2867 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
2868 temp
= emit_store_flag_force (target
, EQ
, temp
, val
, imode
, 1, 1);
2872 /* Only PDP11 has these defined differently but doesn't support NaNs. */
2873 gcc_assert (FLOAT_WORDS_BIG_ENDIAN
== WORDS_BIG_ENDIAN
);
2874 gcc_assert (fmt
->signbit_ro
> 0 && fmt
->b
== 2);
2875 gcc_assert (MODE_COMPOSITE_P (fmode
)
2876 || (fmt
->pnan
== fmt
->p
2877 && fmt
->signbit_ro
== fmt
->signbit_rw
));
2881 case 106: /* IBM double double */
2882 /* For IBM double double, recurse on the most significant double. */
2883 gcc_assert (MODE_COMPOSITE_P (fmode
));
2884 temp
= convert_modes (DFmode
, fmode
, temp
, 0);
2886 fmt
= REAL_MODE_FORMAT (DFmode
);
2888 case 8: /* bfloat */
2889 case 11: /* IEEE half */
2890 case 24: /* IEEE single */
2891 case 53: /* IEEE double or Intel extended with rounding to double */
2892 if (fmt
->p
== 53 && fmt
->signbit_ro
== 79)
2895 scalar_int_mode imode
= int_mode_for_mode (fmode
).require ();
2896 temp
= gen_lowpart (imode
, temp
);
2897 rtx val
= GEN_INT ((HOST_WIDE_INT_M1U
<< (fmt
->p
- 2))
2898 & ~(HOST_WIDE_INT_M1U
<< fmt
->signbit_ro
));
2899 if (fmt
->qnan_msb_set
)
2901 rtx mask
= GEN_INT (~(HOST_WIDE_INT_M1U
<< fmt
->signbit_ro
));
2902 rtx bit
= GEN_INT (HOST_WIDE_INT_1U
<< (fmt
->p
- 2));
2903 /* For non-MIPS/PA IEEE single/double/half or bfloat, expand to:
2904 ((temp ^ bit) & mask) > val. */
2905 temp
= expand_binop (imode
, xor_optab
, temp
, bit
,
2906 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
2907 temp
= expand_binop (imode
, and_optab
, temp
, mask
,
2908 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
2909 temp
= emit_store_flag_force (target
, GTU
, temp
, val
, imode
,
2914 /* For MIPS/PA IEEE single/double, expand to:
2915 (temp & val) == val. */
2916 temp
= expand_binop (imode
, and_optab
, temp
, val
,
2917 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
2918 temp
= emit_store_flag_force (target
, EQ
, temp
, val
, imode
,
2923 case 113: /* IEEE quad */
2925 rtx hi
= NULL_RTX
, lo
= NULL_RTX
;
2926 scalar_int_mode imode
= int_mode_for_size (64, 1).require ();
2927 /* For IEEE quad, TImode support isn't always there and even when
2928 it is, working on DImode parts is usually better. */
2931 hi
= simplify_gen_subreg (imode
, temp
, fmode
,
2932 subreg_highpart_offset (imode
, fmode
));
2933 lo
= simplify_gen_subreg (imode
, temp
, fmode
,
2934 subreg_lowpart_offset (imode
, fmode
));
2937 scalar_int_mode imode2
;
2938 if (int_mode_for_mode (fmode
).exists (&imode2
))
2940 rtx temp2
= gen_lowpart (imode2
, temp
);
2941 hi
= simplify_gen_subreg (imode
, temp2
, imode2
,
2942 subreg_highpart_offset (imode
,
2944 lo
= simplify_gen_subreg (imode
, temp2
, imode2
,
2945 subreg_lowpart_offset (imode
,
2951 rtx mem
= assign_stack_temp (fmode
, GET_MODE_SIZE (fmode
));
2952 emit_move_insn (mem
, temp
);
2959 = subreg_highpart_offset (imode
, GET_MODE (temp
));
2960 hi
= adjust_address (temp
, imode
, offset
);
2961 offset
= subreg_lowpart_offset (imode
, GET_MODE (temp
));
2962 lo
= adjust_address (temp
, imode
, offset
);
2964 rtx val
= GEN_INT ((HOST_WIDE_INT_M1U
<< (fmt
->p
- 2 - 64))
2965 & ~(HOST_WIDE_INT_M1U
<< (fmt
->signbit_ro
- 64)));
2966 if (fmt
->qnan_msb_set
)
2968 rtx mask
= GEN_INT (~(HOST_WIDE_INT_M1U
<< (fmt
->signbit_ro
2970 rtx bit
= GEN_INT (HOST_WIDE_INT_1U
<< (fmt
->p
- 2 - 64));
2971 /* For non-MIPS/PA IEEE quad, expand to:
2972 (((hi ^ bit) | ((lo | -lo) >> 63)) & mask) > val. */
2973 rtx nlo
= expand_unop (imode
, neg_optab
, lo
, NULL_RTX
, 0);
2974 lo
= expand_binop (imode
, ior_optab
, lo
, nlo
,
2975 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
2976 lo
= expand_shift (RSHIFT_EXPR
, imode
, lo
, 63, NULL_RTX
, 1);
2977 temp
= expand_binop (imode
, xor_optab
, hi
, bit
,
2978 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
2979 temp
= expand_binop (imode
, ior_optab
, temp
, lo
,
2980 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
2981 temp
= expand_binop (imode
, and_optab
, temp
, mask
,
2982 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
2983 temp
= emit_store_flag_force (target
, GTU
, temp
, val
, imode
,
2988 /* For MIPS/PA IEEE quad, expand to:
2989 (hi & val) == val. */
2990 temp
= expand_binop (imode
, and_optab
, hi
, val
,
2991 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
2992 temp
= emit_store_flag_force (target
, EQ
, temp
, val
, imode
,
2997 case 64: /* Intel or Motorola extended */
3001 scalar_int_mode imode
= int_mode_for_size (32, 1).require ();
3002 scalar_int_mode iemode
= int_mode_for_size (16, 1).require ();
3005 rtx mem
= assign_stack_temp (fmode
, GET_MODE_SIZE (fmode
));
3006 emit_move_insn (mem
, temp
);
3009 if (fmt
->signbit_ro
== 95)
3011 /* Motorola, always big endian, with 16-bit gap in between
3012 16-bit sign+exponent and 64-bit mantissa. */
3013 ex
= adjust_address (temp
, iemode
, 0);
3014 hi
= adjust_address (temp
, imode
, 4);
3015 lo
= adjust_address (temp
, imode
, 8);
3017 else if (!WORDS_BIG_ENDIAN
)
3019 /* Intel little endian, 64-bit mantissa followed by 16-bit
3020 sign+exponent and then either 16 or 48 bits of gap. */
3021 ex
= adjust_address (temp
, iemode
, 8);
3022 hi
= adjust_address (temp
, imode
, 4);
3023 lo
= adjust_address (temp
, imode
, 0);
3027 /* Big endian Itanium. */
3028 ex
= adjust_address (temp
, iemode
, 0);
3029 hi
= adjust_address (temp
, imode
, 2);
3030 lo
= adjust_address (temp
, imode
, 6);
3032 rtx val
= GEN_INT (HOST_WIDE_INT_M1U
<< 30);
3033 gcc_assert (fmt
->qnan_msb_set
);
3034 rtx mask
= GEN_INT (0x7fff);
3035 rtx bit
= GEN_INT (HOST_WIDE_INT_1U
<< 30);
3036 /* For Intel/Motorola extended format, expand to:
3037 (ex & mask) == mask && ((hi ^ bit) | ((lo | -lo) >> 31)) > val. */
3038 rtx nlo
= expand_unop (imode
, neg_optab
, lo
, NULL_RTX
, 0);
3039 lo
= expand_binop (imode
, ior_optab
, lo
, nlo
,
3040 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3041 lo
= expand_shift (RSHIFT_EXPR
, imode
, lo
, 31, NULL_RTX
, 1);
3042 temp
= expand_binop (imode
, xor_optab
, hi
, bit
,
3043 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3044 temp
= expand_binop (imode
, ior_optab
, temp
, lo
,
3045 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3046 temp
= emit_store_flag_force (target
, GTU
, temp
, val
, imode
, 1, 1);
3047 ex
= expand_binop (iemode
, and_optab
, ex
, mask
,
3048 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3049 ex
= emit_store_flag_force (gen_reg_rtx (GET_MODE (temp
)), EQ
,
3050 ex
, mask
, iemode
, 1, 1);
3051 temp
= expand_binop (GET_MODE (temp
), and_optab
, temp
, ex
,
3052 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
3062 /* Expand a call to one of the builtin rounding functions gcc defines
3063 as an extension (lfloor and lceil). As these are gcc extensions we
3064 do not need to worry about setting errno to EDOM.
3065 If expanding via optab fails, lower expression to (int)(floor(x)).
3066 EXP is the expression that is a call to the builtin function;
3067 if convenient, the result should be placed in TARGET. */
3070 expand_builtin_int_roundingfn (tree exp
, rtx target
)
3072 convert_optab builtin_optab
;
3075 tree fndecl
= get_callee_fndecl (exp
);
3076 enum built_in_function fallback_fn
;
3077 tree fallback_fndecl
;
3081 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
3084 arg
= CALL_EXPR_ARG (exp
, 0);
3086 switch (DECL_FUNCTION_CODE (fndecl
))
3088 CASE_FLT_FN (BUILT_IN_ICEIL
):
3089 CASE_FLT_FN (BUILT_IN_LCEIL
):
3090 CASE_FLT_FN (BUILT_IN_LLCEIL
):
3091 builtin_optab
= lceil_optab
;
3092 fallback_fn
= BUILT_IN_CEIL
;
3095 CASE_FLT_FN (BUILT_IN_IFLOOR
):
3096 CASE_FLT_FN (BUILT_IN_LFLOOR
):
3097 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
3098 builtin_optab
= lfloor_optab
;
3099 fallback_fn
= BUILT_IN_FLOOR
;
3106 /* Make a suitable register to place result in. */
3107 mode
= TYPE_MODE (TREE_TYPE (exp
));
3109 target
= gen_reg_rtx (mode
);
3111 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3112 need to expand the argument again. This way, we will not perform
3113 side-effects more the once. */
3114 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
3116 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
3120 /* Compute into TARGET. */
3121 if (expand_sfix_optab (target
, op0
, builtin_optab
))
3123 /* Output the entire sequence. */
3124 insns
= get_insns ();
3130 /* If we were unable to expand via the builtin, stop the sequence
3131 (without outputting the insns). */
3134 /* Fall back to floating point rounding optab. */
3135 fallback_fndecl
= mathfn_built_in (TREE_TYPE (arg
), fallback_fn
);
3137 /* For non-C99 targets we may end up without a fallback fndecl here
3138 if the user called __builtin_lfloor directly. In this case emit
3139 a call to the floor/ceil variants nevertheless. This should result
3140 in the best user experience for not full C99 targets. */
3141 if (fallback_fndecl
== NULL_TREE
)
3144 const char *name
= NULL
;
3146 switch (DECL_FUNCTION_CODE (fndecl
))
3148 case BUILT_IN_ICEIL
:
3149 case BUILT_IN_LCEIL
:
3150 case BUILT_IN_LLCEIL
:
3153 case BUILT_IN_ICEILF
:
3154 case BUILT_IN_LCEILF
:
3155 case BUILT_IN_LLCEILF
:
3158 case BUILT_IN_ICEILL
:
3159 case BUILT_IN_LCEILL
:
3160 case BUILT_IN_LLCEILL
:
3163 case BUILT_IN_IFLOOR
:
3164 case BUILT_IN_LFLOOR
:
3165 case BUILT_IN_LLFLOOR
:
3168 case BUILT_IN_IFLOORF
:
3169 case BUILT_IN_LFLOORF
:
3170 case BUILT_IN_LLFLOORF
:
3173 case BUILT_IN_IFLOORL
:
3174 case BUILT_IN_LFLOORL
:
3175 case BUILT_IN_LLFLOORL
:
3182 fntype
= build_function_type_list (TREE_TYPE (arg
),
3183 TREE_TYPE (arg
), NULL_TREE
);
3184 fallback_fndecl
= build_fn_decl (name
, fntype
);
3187 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
), fallback_fndecl
, 1, arg
);
3189 tmp
= expand_normal (exp
);
3190 tmp
= maybe_emit_group_store (tmp
, TREE_TYPE (exp
));
3192 /* Truncate the result of floating point optab to integer
3193 via expand_fix (). */
3194 target
= gen_reg_rtx (mode
);
3195 expand_fix (target
, tmp
, 0);
3200 /* Expand a call to one of the builtin math functions doing integer
3202 Return 0 if a normal call should be emitted rather than expanding the
3203 function in-line. EXP is the expression that is a call to the builtin
3204 function; if convenient, the result should be placed in TARGET. */
3207 expand_builtin_int_roundingfn_2 (tree exp
, rtx target
)
3209 convert_optab builtin_optab
;
3212 tree fndecl
= get_callee_fndecl (exp
);
3215 enum built_in_function fallback_fn
= BUILT_IN_NONE
;
3217 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
3220 arg
= CALL_EXPR_ARG (exp
, 0);
3222 switch (DECL_FUNCTION_CODE (fndecl
))
3224 CASE_FLT_FN (BUILT_IN_IRINT
):
3225 fallback_fn
= BUILT_IN_LRINT
;
3227 CASE_FLT_FN (BUILT_IN_LRINT
):
3228 CASE_FLT_FN (BUILT_IN_LLRINT
):
3229 builtin_optab
= lrint_optab
;
3232 CASE_FLT_FN (BUILT_IN_IROUND
):
3233 fallback_fn
= BUILT_IN_LROUND
;
3235 CASE_FLT_FN (BUILT_IN_LROUND
):
3236 CASE_FLT_FN (BUILT_IN_LLROUND
):
3237 builtin_optab
= lround_optab
;
3244 /* There's no easy way to detect the case we need to set EDOM. */
3245 if (flag_errno_math
&& fallback_fn
== BUILT_IN_NONE
)
3248 /* Make a suitable register to place result in. */
3249 mode
= TYPE_MODE (TREE_TYPE (exp
));
3251 /* There's no easy way to detect the case we need to set EDOM. */
3252 if (!flag_errno_math
)
3254 rtx result
= gen_reg_rtx (mode
);
3256 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3257 need to expand the argument again. This way, we will not perform
3258 side-effects more the once. */
3259 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
3261 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
3265 if (expand_sfix_optab (result
, op0
, builtin_optab
))
3267 /* Output the entire sequence. */
3268 insns
= get_insns ();
3274 /* If we were unable to expand via the builtin, stop the sequence
3275 (without outputting the insns) and call to the library function
3276 with the stabilized argument list. */
3280 if (fallback_fn
!= BUILT_IN_NONE
)
3282 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
3283 targets, (int) round (x) should never be transformed into
3284 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
3285 a call to lround in the hope that the target provides at least some
3286 C99 functions. This should result in the best user experience for
3287 not full C99 targets.
3288 As scalar float conversions with same mode are useless in GIMPLE,
3289 we can end up e.g. with _Float32 argument passed to float builtin,
3290 try to get the type from the builtin prototype first. */
3291 tree fallback_fndecl
= NULL_TREE
;
3292 if (tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
)))
3294 = mathfn_built_in_1 (TREE_VALUE (argtypes
),
3295 as_combined_fn (fallback_fn
), 0);
3296 if (fallback_fndecl
== NULL_TREE
)
3298 = mathfn_built_in_1 (TREE_TYPE (arg
),
3299 as_combined_fn (fallback_fn
), 0);
3300 if (fallback_fndecl
)
3302 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
),
3303 fallback_fndecl
, 1, arg
);
3305 target
= expand_call (exp
, NULL_RTX
, target
== const0_rtx
);
3306 target
= maybe_emit_group_store (target
, TREE_TYPE (exp
));
3307 return convert_to_mode (mode
, target
, 0);
3311 return expand_call (exp
, target
, target
== const0_rtx
);
3314 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3315 a normal call should be emitted rather than expanding the function
3316 in-line. EXP is the expression that is a call to the builtin
3317 function; if convenient, the result should be placed in TARGET. */
3320 expand_builtin_powi (tree exp
, rtx target
)
3327 if (! validate_arglist (exp
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3330 arg0
= CALL_EXPR_ARG (exp
, 0);
3331 arg1
= CALL_EXPR_ARG (exp
, 1);
3332 mode
= TYPE_MODE (TREE_TYPE (exp
));
3334 /* Emit a libcall to libgcc. */
3336 /* Mode of the 2nd argument must match that of an int. */
3337 mode2
= int_mode_for_size (INT_TYPE_SIZE
, 0).require ();
3339 if (target
== NULL_RTX
)
3340 target
= gen_reg_rtx (mode
);
3342 op0
= expand_expr (arg0
, NULL_RTX
, mode
, EXPAND_NORMAL
);
3343 if (GET_MODE (op0
) != mode
)
3344 op0
= convert_to_mode (mode
, op0
, 0);
3345 op1
= expand_expr (arg1
, NULL_RTX
, mode2
, EXPAND_NORMAL
);
3346 if (GET_MODE (op1
) != mode2
)
3347 op1
= convert_to_mode (mode2
, op1
, 0);
3349 target
= emit_library_call_value (optab_libfunc (powi_optab
, mode
),
3350 target
, LCT_CONST
, mode
,
3351 op0
, mode
, op1
, mode2
);
3356 /* Expand expression EXP which is a call to the strlen builtin. Return
3357 NULL_RTX if we failed and the caller should emit a normal call, otherwise
3358 try to get the result in TARGET, if convenient. */
3361 expand_builtin_strlen (tree exp
, rtx target
,
3362 machine_mode target_mode
)
3364 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
3367 tree src
= CALL_EXPR_ARG (exp
, 0);
3369 /* If the length can be computed at compile-time, return it. */
3370 if (tree len
= c_strlen (src
, 0))
3371 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3373 /* If the length can be computed at compile-time and is constant
3374 integer, but there are side-effects in src, evaluate
3375 src for side-effects, then return len.
3376 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3377 can be optimized into: i++; x = 3; */
3378 tree len
= c_strlen (src
, 1);
3379 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
3381 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3382 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3385 unsigned int align
= get_pointer_alignment (src
) / BITS_PER_UNIT
;
3387 /* If SRC is not a pointer type, don't do this operation inline. */
3391 /* Bail out if we can't compute strlen in the right mode. */
3392 machine_mode insn_mode
;
3393 enum insn_code icode
= CODE_FOR_nothing
;
3394 FOR_EACH_MODE_FROM (insn_mode
, target_mode
)
3396 icode
= optab_handler (strlen_optab
, insn_mode
);
3397 if (icode
!= CODE_FOR_nothing
)
3400 if (insn_mode
== VOIDmode
)
3403 /* Make a place to hold the source address. We will not expand
3404 the actual source until we are sure that the expansion will
3405 not fail -- there are trees that cannot be expanded twice. */
3406 rtx src_reg
= gen_reg_rtx (Pmode
);
3408 /* Mark the beginning of the strlen sequence so we can emit the
3409 source operand later. */
3410 rtx_insn
*before_strlen
= get_last_insn ();
3412 class expand_operand ops
[4];
3413 create_output_operand (&ops
[0], target
, insn_mode
);
3414 create_fixed_operand (&ops
[1], gen_rtx_MEM (BLKmode
, src_reg
));
3415 create_integer_operand (&ops
[2], 0);
3416 create_integer_operand (&ops
[3], align
);
3417 if (!maybe_expand_insn (icode
, 4, ops
))
3420 /* Check to see if the argument was declared attribute nonstring
3421 and if so, issue a warning since at this point it's not known
3422 to be nul-terminated. */
3423 maybe_warn_nonstring_arg (get_callee_fndecl (exp
), exp
);
3425 /* Now that we are assured of success, expand the source. */
3427 rtx pat
= expand_expr (src
, src_reg
, Pmode
, EXPAND_NORMAL
);
3430 #ifdef POINTERS_EXTEND_UNSIGNED
3431 if (GET_MODE (pat
) != Pmode
)
3432 pat
= convert_to_mode (Pmode
, pat
,
3433 POINTERS_EXTEND_UNSIGNED
);
3435 emit_move_insn (src_reg
, pat
);
3441 emit_insn_after (pat
, before_strlen
);
3443 emit_insn_before (pat
, get_insns ());
3445 /* Return the value in the proper mode for this function. */
3446 if (GET_MODE (ops
[0].value
) == target_mode
)
3447 target
= ops
[0].value
;
3448 else if (target
!= 0)
3449 convert_move (target
, ops
[0].value
, 0);
3451 target
= convert_to_mode (target_mode
, ops
[0].value
, 0);
3456 /* Expand call EXP to the strnlen built-in, returning the result
3457 and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3460 expand_builtin_strnlen (tree exp
, rtx target
, machine_mode target_mode
)
3462 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3465 tree src
= CALL_EXPR_ARG (exp
, 0);
3466 tree bound
= CALL_EXPR_ARG (exp
, 1);
3471 location_t loc
= UNKNOWN_LOCATION
;
3472 if (EXPR_HAS_LOCATION (exp
))
3473 loc
= EXPR_LOCATION (exp
);
3475 /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3476 so these conversions aren't necessary. */
3477 c_strlen_data lendata
= { };
3478 tree len
= c_strlen (src
, 0, &lendata
, 1);
3480 len
= fold_convert_loc (loc
, TREE_TYPE (bound
), len
);
3482 if (TREE_CODE (bound
) == INTEGER_CST
)
3487 len
= fold_build2_loc (loc
, MIN_EXPR
, size_type_node
, len
, bound
);
3488 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3491 if (TREE_CODE (bound
) != SSA_NAME
)
3496 get_global_range_query ()->range_of_expr (r
, bound
);
3497 if (r
.varying_p () || r
.undefined_p ())
3499 min
= r
.lower_bound ();
3500 max
= r
.upper_bound ();
3502 if (!len
|| TREE_CODE (len
) != INTEGER_CST
)
3505 lendata
.decl
= unterminated_array (src
, &len
, &exact
);
3513 if (wi::gtu_p (min
, wi::to_wide (len
)))
3514 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3516 len
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (len
), len
, bound
);
3517 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
3520 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3521 bytes from bytes at DATA + OFFSET and return it reinterpreted as
3522 a target constant. */
3525 builtin_memcpy_read_str (void *data
, void *, HOST_WIDE_INT offset
,
3526 fixed_size_mode mode
)
3528 /* The REPresentation pointed to by DATA need not be a nul-terminated
3529 string but the caller guarantees it's large enough for MODE. */
3530 const char *rep
= (const char *) data
;
3532 /* The by-pieces infrastructure does not try to pick a vector mode
3533 for memcpy expansion. */
3534 return c_readstr (rep
+ offset
, as_a
<scalar_int_mode
> (mode
),
3535 /*nul_terminated=*/false);
3538 /* LEN specify length of the block of memcpy/memset operation.
3539 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3540 In some cases we can make very likely guess on max size, then we
3541 set it into PROBABLE_MAX_SIZE. */
3544 determine_block_size (tree len
, rtx len_rtx
,
3545 unsigned HOST_WIDE_INT
*min_size
,
3546 unsigned HOST_WIDE_INT
*max_size
,
3547 unsigned HOST_WIDE_INT
*probable_max_size
)
3549 if (CONST_INT_P (len_rtx
))
3551 *min_size
= *max_size
= *probable_max_size
= UINTVAL (len_rtx
);
3557 enum value_range_kind range_type
= VR_UNDEFINED
;
3559 /* Determine bounds from the type. */
3560 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len
))))
3561 *min_size
= tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len
)));
3564 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len
))))
3565 *probable_max_size
= *max_size
3566 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len
)));
3568 *probable_max_size
= *max_size
= GET_MODE_MASK (GET_MODE (len_rtx
));
3570 if (TREE_CODE (len
) == SSA_NAME
)
3574 get_global_range_query ()->range_of_expr (r
, len
);
3575 range_type
= get_legacy_range (r
, tmin
, tmax
);
3576 if (range_type
!= VR_UNDEFINED
)
3578 min
= wi::to_wide (tmin
);
3579 max
= wi::to_wide (tmax
);
3582 if (range_type
== VR_RANGE
)
3584 if (wi::fits_uhwi_p (min
) && *min_size
< min
.to_uhwi ())
3585 *min_size
= min
.to_uhwi ();
3586 if (wi::fits_uhwi_p (max
) && *max_size
> max
.to_uhwi ())
3587 *probable_max_size
= *max_size
= max
.to_uhwi ();
3589 else if (range_type
== VR_ANTI_RANGE
)
3597 Produce anti range allowing negative values of N. We still
3598 can use the information and make a guess that N is not negative.
3600 if (!wi::leu_p (max
, 1 << 30) && wi::fits_uhwi_p (min
))
3601 *probable_max_size
= min
.to_uhwi () - 1;
3604 gcc_checking_assert (*max_size
<=
3605 (unsigned HOST_WIDE_INT
)
3606 GET_MODE_MASK (GET_MODE (len_rtx
)));
3609 /* Expand a call EXP to the memcpy builtin.
3610 Return NULL_RTX if we failed, the caller should emit a normal call,
3611 otherwise try to get the result in TARGET, if convenient (and in
3612 mode MODE if that's convenient). */
3615 expand_builtin_memcpy (tree exp
, rtx target
)
3617 if (!validate_arglist (exp
,
3618 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3621 tree dest
= CALL_EXPR_ARG (exp
, 0);
3622 tree src
= CALL_EXPR_ARG (exp
, 1);
3623 tree len
= CALL_EXPR_ARG (exp
, 2);
3625 return expand_builtin_memory_copy_args (dest
, src
, len
, target
, exp
,
3626 /*retmode=*/ RETURN_BEGIN
, false);
3629 /* Check a call EXP to the memmove built-in for validity.
3630 Return NULL_RTX on both success and failure. */
3633 expand_builtin_memmove (tree exp
, rtx target
)
3635 if (!validate_arglist (exp
,
3636 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3639 tree dest
= CALL_EXPR_ARG (exp
, 0);
3640 tree src
= CALL_EXPR_ARG (exp
, 1);
3641 tree len
= CALL_EXPR_ARG (exp
, 2);
3643 return expand_builtin_memory_copy_args (dest
, src
, len
, target
, exp
,
3644 /*retmode=*/ RETURN_BEGIN
, true);
3647 /* Expand a call EXP to the mempcpy builtin.
3648 Return NULL_RTX if we failed; the caller should emit a normal call,
3649 otherwise try to get the result in TARGET, if convenient (and in
3650 mode MODE if that's convenient). */
3653 expand_builtin_mempcpy (tree exp
, rtx target
)
3655 if (!validate_arglist (exp
,
3656 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3659 tree dest
= CALL_EXPR_ARG (exp
, 0);
3660 tree src
= CALL_EXPR_ARG (exp
, 1);
3661 tree len
= CALL_EXPR_ARG (exp
, 2);
3663 /* Policy does not generally allow using compute_objsize (which
3664 is used internally by check_memop_size) to change code generation
3665 or drive optimization decisions.
3667 In this instance it is safe because the code we generate has
3668 the same semantics regardless of the return value of
3669 check_memop_sizes. Exactly the same amount of data is copied
3670 and the return value is exactly the same in both cases.
3672 Furthermore, check_memop_size always uses mode 0 for the call to
3673 compute_objsize, so the imprecise nature of compute_objsize is
3676 /* Avoid expanding mempcpy into memcpy when the call is determined
3677 to overflow the buffer. This also prevents the same overflow
3678 from being diagnosed again when expanding memcpy. */
3680 return expand_builtin_mempcpy_args (dest
, src
, len
,
3681 target
, exp
, /*retmode=*/ RETURN_END
);
3684 /* Helper function to do the actual work for expand of memory copy family
3685 functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3686 of memory from SRC to DEST and assign to TARGET if convenient. Return
3687 value is based on RETMODE argument. */
3690 expand_builtin_memory_copy_args (tree dest
, tree src
, tree len
,
3691 rtx target
, tree exp
, memop_ret retmode
,
3694 unsigned int src_align
= get_pointer_alignment (src
);
3695 unsigned int dest_align
= get_pointer_alignment (dest
);
3696 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
3697 HOST_WIDE_INT expected_size
= -1;
3698 unsigned int expected_align
= 0;
3699 unsigned HOST_WIDE_INT min_size
;
3700 unsigned HOST_WIDE_INT max_size
;
3701 unsigned HOST_WIDE_INT probable_max_size
;
3705 /* If DEST is not a pointer type, call the normal function. */
3706 if (dest_align
== 0)
3709 /* If either SRC is not a pointer type, don't do this
3710 operation in-line. */
3714 if (currently_expanding_gimple_stmt
)
3715 stringop_block_profile (currently_expanding_gimple_stmt
,
3716 &expected_align
, &expected_size
);
3718 if (expected_align
< dest_align
)
3719 expected_align
= dest_align
;
3720 dest_mem
= get_memory_rtx (dest
, len
);
3721 set_mem_align (dest_mem
, dest_align
);
3722 len_rtx
= expand_normal (len
);
3723 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3724 &probable_max_size
);
3726 /* Try to get the byte representation of the constant SRC points to,
3727 with its byte size in NBYTES. */
3728 unsigned HOST_WIDE_INT nbytes
;
3729 const char *rep
= getbyterep (src
, &nbytes
);
3731 /* If the function's constant bound LEN_RTX is less than or equal
3732 to the byte size of the representation of the constant argument,
3733 and if block move would be done by pieces, we can avoid loading
3734 the bytes from memory and only store the computed constant.
3735 This works in the overlap (memmove) case as well because
3736 store_by_pieces just generates a series of stores of constants
3737 from the representation returned by getbyterep(). */
3739 && CONST_INT_P (len_rtx
)
3740 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= nbytes
3741 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3742 CONST_CAST (char *, rep
),
3745 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3746 builtin_memcpy_read_str
,
3747 CONST_CAST (char *, rep
),
3748 dest_align
, false, retmode
);
3749 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3750 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3754 src_mem
= get_memory_rtx (src
, len
);
3755 set_mem_align (src_mem
, src_align
);
3757 /* Copy word part most expediently. */
3758 enum block_op_methods method
= BLOCK_OP_NORMAL
;
3759 if (CALL_EXPR_TAILCALL (exp
)
3760 && (retmode
== RETURN_BEGIN
|| target
== const0_rtx
))
3761 method
= BLOCK_OP_TAILCALL
;
3762 bool use_mempcpy_call
= (targetm
.libc_has_fast_function (BUILT_IN_MEMPCPY
)
3763 && retmode
== RETURN_END
3765 && target
!= const0_rtx
);
3766 if (use_mempcpy_call
)
3767 method
= BLOCK_OP_NO_LIBCALL_RET
;
3768 dest_addr
= emit_block_move_hints (dest_mem
, src_mem
, len_rtx
, method
,
3769 expected_align
, expected_size
,
3770 min_size
, max_size
, probable_max_size
,
3771 use_mempcpy_call
, &is_move_done
,
3774 /* Bail out when a mempcpy call would be expanded as libcall and when
3775 we have a target that provides a fast implementation
3776 of mempcpy routine. */
3780 if (dest_addr
== pc_rtx
)
3785 dest_addr
= force_operand (XEXP (dest_mem
, 0), target
);
3786 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3789 if (retmode
!= RETURN_BEGIN
&& target
!= const0_rtx
)
3791 dest_addr
= gen_rtx_PLUS (ptr_mode
, dest_addr
, len_rtx
);
3792 /* stpcpy pointer to last byte. */
3793 if (retmode
== RETURN_END_MINUS_ONE
)
3794 dest_addr
= gen_rtx_MINUS (ptr_mode
, dest_addr
, const1_rtx
);
3801 expand_builtin_mempcpy_args (tree dest
, tree src
, tree len
,
3802 rtx target
, tree orig_exp
, memop_ret retmode
)
3804 return expand_builtin_memory_copy_args (dest
, src
, len
, target
, orig_exp
,
3808 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3809 we failed, the caller should emit a normal call, otherwise try to
3810 get the result in TARGET, if convenient.
3811 Return value is based on RETMODE argument. */
3814 expand_movstr (tree dest
, tree src
, rtx target
, memop_ret retmode
)
3816 class expand_operand ops
[3];
3820 if (!targetm
.have_movstr ())
3823 dest_mem
= get_memory_rtx (dest
, NULL
);
3824 src_mem
= get_memory_rtx (src
, NULL
);
3825 if (retmode
== RETURN_BEGIN
)
3827 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
3828 dest_mem
= replace_equiv_address (dest_mem
, target
);
3831 create_output_operand (&ops
[0],
3832 retmode
!= RETURN_BEGIN
? target
: NULL_RTX
, Pmode
);
3833 create_fixed_operand (&ops
[1], dest_mem
);
3834 create_fixed_operand (&ops
[2], src_mem
);
3835 if (!maybe_expand_insn (targetm
.code_for_movstr
, 3, ops
))
3838 if (retmode
!= RETURN_BEGIN
&& target
!= const0_rtx
)
3840 target
= ops
[0].value
;
3841 /* movstr is supposed to set end to the address of the NUL
3842 terminator. If the caller requested a mempcpy-like return value,
3844 if (retmode
== RETURN_END
)
3846 rtx tem
= plus_constant (GET_MODE (target
),
3847 gen_lowpart (GET_MODE (target
), target
), 1);
3848 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
3854 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3855 NULL_RTX if we failed the caller should emit a normal call, otherwise
3856 try to get the result in TARGET, if convenient (and in mode MODE if that's
3860 expand_builtin_strcpy (tree exp
, rtx target
)
3862 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3865 tree dest
= CALL_EXPR_ARG (exp
, 0);
3866 tree src
= CALL_EXPR_ARG (exp
, 1);
3868 return expand_builtin_strcpy_args (exp
, dest
, src
, target
);
3871 /* Helper function to do the actual work for expand_builtin_strcpy. The
3872 arguments to the builtin_strcpy call DEST and SRC are broken out
3873 so that this can also be called without constructing an actual CALL_EXPR.
3874 The other arguments and return value are the same as for
3875 expand_builtin_strcpy. */
3878 expand_builtin_strcpy_args (tree
, tree dest
, tree src
, rtx target
)
3880 return expand_movstr (dest
, src
, target
, /*retmode=*/ RETURN_BEGIN
);
3883 /* Expand a call EXP to the stpcpy builtin.
3884 Return NULL_RTX if we failed the caller should emit a normal call,
3885 otherwise try to get the result in TARGET, if convenient (and in
3886 mode MODE if that's convenient). */
3889 expand_builtin_stpcpy_1 (tree exp
, rtx target
, machine_mode mode
)
3892 location_t loc
= EXPR_LOCATION (exp
);
3894 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3897 dst
= CALL_EXPR_ARG (exp
, 0);
3898 src
= CALL_EXPR_ARG (exp
, 1);
3900 /* If return value is ignored, transform stpcpy into strcpy. */
3901 if (target
== const0_rtx
&& builtin_decl_implicit (BUILT_IN_STRCPY
))
3903 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3904 tree result
= build_call_nofold_loc (loc
, fn
, 2, dst
, src
);
3905 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3912 /* Ensure we get an actual string whose length can be evaluated at
3913 compile-time, not an expression containing a string. This is
3914 because the latter will potentially produce pessimized code
3915 when used to produce the return value. */
3916 c_strlen_data lendata
= { };
3918 || !(len
= c_strlen (src
, 0, &lendata
, 1)))
3919 return expand_movstr (dst
, src
, target
,
3920 /*retmode=*/ RETURN_END_MINUS_ONE
);
3922 lenp1
= size_binop_loc (loc
, PLUS_EXPR
, len
, ssize_int (1));
3923 ret
= expand_builtin_mempcpy_args (dst
, src
, lenp1
,
3925 /*retmode=*/ RETURN_END_MINUS_ONE
);
3930 if (TREE_CODE (len
) == INTEGER_CST
)
3932 rtx len_rtx
= expand_normal (len
);
3934 if (CONST_INT_P (len_rtx
))
3936 ret
= expand_builtin_strcpy_args (exp
, dst
, src
, target
);
3942 if (mode
!= VOIDmode
)
3943 target
= gen_reg_rtx (mode
);
3945 target
= gen_reg_rtx (GET_MODE (ret
));
3947 if (GET_MODE (target
) != GET_MODE (ret
))
3948 ret
= gen_lowpart (GET_MODE (target
), ret
);
3950 ret
= plus_constant (GET_MODE (ret
), ret
, INTVAL (len_rtx
));
3951 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
3959 return expand_movstr (dst
, src
, target
,
3960 /*retmode=*/ RETURN_END_MINUS_ONE
);
3964 /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
3965 arguments while being careful to avoid duplicate warnings (which could
3966 be issued if the expander were to expand the call, resulting in it
3967 being emitted in expand_call(). */
3970 expand_builtin_stpcpy (tree exp
, rtx target
, machine_mode mode
)
3972 if (rtx ret
= expand_builtin_stpcpy_1 (exp
, target
, mode
))
3974 /* The call has been successfully expanded. Check for nonstring
3975 arguments and issue warnings as appropriate. */
3976 maybe_warn_nonstring_arg (get_callee_fndecl (exp
), exp
);
3983 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3984 bytes from constant string DATA + OFFSET and return it as target
3988 builtin_strncpy_read_str (void *data
, void *, HOST_WIDE_INT offset
,
3989 fixed_size_mode mode
)
3991 const char *str
= (const char *) data
;
3993 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
3996 /* The by-pieces infrastructure does not try to pick a vector mode
3997 for strncpy expansion. */
3998 return c_readstr (str
+ offset
, as_a
<scalar_int_mode
> (mode
));
4001 /* Helper to check the sizes of sequences and the destination of calls
4002 to __builtin_strncat and __builtin___strncat_chk. Returns true on
4003 success (no overflow or invalid sizes), false otherwise. */
4006 check_strncat_sizes (tree exp
, tree objsize
)
4008 tree dest
= CALL_EXPR_ARG (exp
, 0);
4009 tree src
= CALL_EXPR_ARG (exp
, 1);
4010 tree maxread
= CALL_EXPR_ARG (exp
, 2);
4012 /* Try to determine the range of lengths that the source expression
4014 c_strlen_data lendata
= { };
4015 get_range_strlen (src
, &lendata
, /* eltsize = */ 1);
4017 /* Try to verify that the destination is big enough for the shortest
4020 access_data
data (nullptr, exp
, access_read_write
, maxread
, true);
4021 if (!objsize
&& warn_stringop_overflow
)
4023 /* If it hasn't been provided by __strncat_chk, try to determine
4024 the size of the destination object into which the source is
4026 objsize
= compute_objsize (dest
, warn_stringop_overflow
- 1, &data
.dst
);
4029 /* Add one for the terminating nul. */
4030 tree srclen
= (lendata
.minlen
4031 ? fold_build2 (PLUS_EXPR
, size_type_node
, lendata
.minlen
,
4035 /* The strncat function copies at most MAXREAD bytes and always appends
4036 the terminating nul so the specified upper bound should never be equal
4037 to (or greater than) the size of the destination. */
4038 if (tree_fits_uhwi_p (maxread
) && tree_fits_uhwi_p (objsize
)
4039 && tree_int_cst_equal (objsize
, maxread
))
4041 location_t loc
= EXPR_LOCATION (exp
);
4042 warning_at (loc
, OPT_Wstringop_overflow_
,
4043 "%qD specified bound %E equals destination size",
4044 get_callee_fndecl (exp
), maxread
);
4050 || (maxread
&& tree_fits_uhwi_p (maxread
)
4051 && tree_fits_uhwi_p (srclen
)
4052 && tree_int_cst_lt (maxread
, srclen
)))
4055 /* The number of bytes to write is LEN but check_access will alsoa
4056 check SRCLEN if LEN's value isn't known. */
4057 return check_access (exp
, /*dstwrite=*/NULL_TREE
, maxread
, srclen
,
4058 objsize
, data
.mode
, &data
);
4061 /* Expand expression EXP, which is a call to the strncpy builtin. Return
4062 NULL_RTX if we failed the caller should emit a normal call. */
4065 expand_builtin_strncpy (tree exp
, rtx target
)
4067 location_t loc
= EXPR_LOCATION (exp
);
4069 if (!validate_arglist (exp
,
4070 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4072 tree dest
= CALL_EXPR_ARG (exp
, 0);
4073 tree src
= CALL_EXPR_ARG (exp
, 1);
4074 /* The number of bytes to write (not the maximum). */
4075 tree len
= CALL_EXPR_ARG (exp
, 2);
4077 /* The length of the source sequence. */
4078 tree slen
= c_strlen (src
, 1);
4080 /* We must be passed a constant len and src parameter. */
4081 if (!tree_fits_uhwi_p (len
) || !slen
|| !tree_fits_uhwi_p (slen
))
4084 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
4086 /* We're required to pad with trailing zeros if the requested
4087 len is greater than strlen(s2)+1. In that case try to
4088 use store_by_pieces, if it fails, punt. */
4089 if (tree_int_cst_lt (slen
, len
))
4091 unsigned int dest_align
= get_pointer_alignment (dest
);
4092 const char *p
= c_getstr (src
);
4095 if (!p
|| dest_align
== 0 || !tree_fits_uhwi_p (len
)
4096 || !can_store_by_pieces (tree_to_uhwi (len
),
4097 builtin_strncpy_read_str
,
4098 CONST_CAST (char *, p
),
4102 dest_mem
= get_memory_rtx (dest
, len
);
4103 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
4104 builtin_strncpy_read_str
,
4105 CONST_CAST (char *, p
), dest_align
, false,
4107 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
4108 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
4115 /* Return the RTL of a register in MODE generated from PREV in the
4116 previous iteration. */
4119 gen_memset_value_from_prev (by_pieces_prev
*prev
, fixed_size_mode mode
)
4121 rtx target
= nullptr;
4122 if (prev
!= nullptr && prev
->data
!= nullptr)
4124 /* Use the previous data in the same mode. */
4125 if (prev
->mode
== mode
)
4128 fixed_size_mode prev_mode
= prev
->mode
;
4130 /* Don't use the previous data to write QImode if it is in a
4132 if (VECTOR_MODE_P (prev_mode
) && mode
== QImode
)
4135 rtx prev_rtx
= prev
->data
;
4137 if (REG_P (prev_rtx
)
4138 && HARD_REGISTER_P (prev_rtx
)
4139 && lowpart_subreg_regno (REGNO (prev_rtx
), prev_mode
, mode
) < 0)
4141 /* This case occurs when PREV_MODE is a vector and when
4142 MODE is too small to store using vector operations.
4143 After register allocation, the code will need to move the
4144 lowpart of the vector register into a non-vector register.
4146 Also, the target has chosen to use a hard register
4147 instead of going with the default choice of using a
4148 pseudo register. We should respect that choice and try to
4149 avoid creating a pseudo register with the same mode as the
4150 current hard register.
4152 In principle, we could just use a lowpart MODE subreg of
4153 the vector register. However, the vector register mode might
4154 be too wide for non-vector registers, and we already know
4155 that the non-vector mode is too small for vector registers.
4156 It's therefore likely that we'd need to spill to memory in
4157 the vector mode and reload the non-vector value from there.
4159 Try to avoid that by reducing the vector register to the
4160 smallest size that it can hold. This should increase the
4161 chances that non-vector registers can hold both the inner
4162 and outer modes of the subreg that we generate later. */
4164 fixed_size_mode candidate
;
4165 FOR_EACH_MODE_IN_CLASS (m
, GET_MODE_CLASS (mode
))
4166 if (is_a
<fixed_size_mode
> (m
, &candidate
))
4168 if (GET_MODE_SIZE (candidate
)
4169 >= GET_MODE_SIZE (prev_mode
))
4171 if (GET_MODE_SIZE (candidate
) >= GET_MODE_SIZE (mode
)
4172 && lowpart_subreg_regno (REGNO (prev_rtx
),
4173 prev_mode
, candidate
) >= 0)
4175 target
= lowpart_subreg (candidate
, prev_rtx
,
4178 prev_mode
= candidate
;
4182 if (target
== nullptr)
4183 prev_rtx
= copy_to_reg (prev_rtx
);
4186 target
= lowpart_subreg (mode
, prev_rtx
, prev_mode
);
4191 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4192 bytes from constant string DATA + OFFSET and return it as target
4193 constant. If PREV isn't nullptr, it has the RTL info from the
4194 previous iteration. */
4197 builtin_memset_read_str (void *data
, void *prev
,
4198 HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
4199 fixed_size_mode mode
)
4201 const char *c
= (const char *) data
;
4202 unsigned int size
= GET_MODE_SIZE (mode
);
4204 rtx target
= gen_memset_value_from_prev ((by_pieces_prev
*) prev
,
4206 if (target
!= nullptr)
4208 rtx src
= gen_int_mode (*c
, QImode
);
4210 if (VECTOR_MODE_P (mode
))
4212 gcc_assert (GET_MODE_INNER (mode
) == QImode
);
4214 rtx const_vec
= gen_const_vec_duplicate (mode
, src
);
4216 /* Return CONST_VECTOR when called by a query function. */
4219 /* Use the move expander with CONST_VECTOR. */
4220 target
= gen_reg_rtx (mode
);
4221 emit_move_insn (target
, const_vec
);
4225 char *p
= XALLOCAVEC (char, size
);
4227 memset (p
, *c
, size
);
4229 /* Vector modes should be handled above. */
4230 return c_readstr (p
, as_a
<scalar_int_mode
> (mode
));
4233 /* Callback routine for store_by_pieces. Return the RTL of a register
4234 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4235 char value given in the RTL register data. For example, if mode is
4236 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
4237 nullptr, it has the RTL info from the previous iteration. */
4240 builtin_memset_gen_str (void *data
, void *prev
,
4241 HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
4242 fixed_size_mode mode
)
4248 size
= GET_MODE_SIZE (mode
);
4252 target
= gen_memset_value_from_prev ((by_pieces_prev
*) prev
, mode
);
4253 if (target
!= nullptr)
4256 if (VECTOR_MODE_P (mode
))
4258 gcc_assert (GET_MODE_INNER (mode
) == QImode
);
4260 /* vec_duplicate_optab is a precondition to pick a vector mode for
4261 the memset expander. */
4262 insn_code icode
= optab_handler (vec_duplicate_optab
, mode
);
4264 target
= gen_reg_rtx (mode
);
4265 class expand_operand ops
[2];
4266 create_output_operand (&ops
[0], target
, mode
);
4267 create_input_operand (&ops
[1], (rtx
) data
, QImode
);
4268 expand_insn (icode
, 2, ops
);
4269 if (!rtx_equal_p (target
, ops
[0].value
))
4270 emit_move_insn (target
, ops
[0].value
);
4275 p
= XALLOCAVEC (char, size
);
4276 memset (p
, 1, size
);
4277 /* Vector modes should be handled above. */
4278 coeff
= c_readstr (p
, as_a
<scalar_int_mode
> (mode
));
4280 target
= convert_to_mode (mode
, (rtx
) data
, 1);
4281 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
4282 return force_reg (mode
, target
);
4285 /* Expand expression EXP, which is a call to the memset builtin. Return
4286 NULL_RTX if we failed the caller should emit a normal call, otherwise
4287 try to get the result in TARGET, if convenient (and in mode MODE if that's
4291 expand_builtin_memset (tree exp
, rtx target
, machine_mode mode
)
4293 if (!validate_arglist (exp
,
4294 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4297 tree dest
= CALL_EXPR_ARG (exp
, 0);
4298 tree val
= CALL_EXPR_ARG (exp
, 1);
4299 tree len
= CALL_EXPR_ARG (exp
, 2);
4301 return expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
4304 /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
4305 Return TRUE if successful, FALSE otherwise. TO is assumed to be
4306 aligned at an ALIGN-bits boundary. LEN must be a multiple of
4307 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
4309 The strategy is to issue one store_by_pieces for each power of two,
4310 from most to least significant, guarded by a test on whether there
4311 are at least that many bytes left to copy in LEN.
4313 ??? Should we skip some powers of two in favor of loops? Maybe start
4314 at the max of TO/LEN/word alignment, at least when optimizing for
4315 size, instead of ensuring O(log len) dynamic compares? */
4318 try_store_by_multiple_pieces (rtx to
, rtx len
, unsigned int ctz_len
,
4319 unsigned HOST_WIDE_INT min_len
,
4320 unsigned HOST_WIDE_INT max_len
,
4321 rtx val
, char valc
, unsigned int align
)
4323 int max_bits
= floor_log2 (max_len
);
4324 int min_bits
= floor_log2 (min_len
);
4325 int sctz_len
= ctz_len
;
4327 gcc_checking_assert (sctz_len
>= 0);
4332 /* Bits more significant than TST_BITS are part of the shared prefix
4333 in the binary representation of both min_len and max_len. Since
4334 they're identical, we don't need to test them in the loop. */
4335 int tst_bits
= (max_bits
!= min_bits
? max_bits
4336 : floor_log2 (max_len
^ min_len
));
4338 /* Check whether it's profitable to start by storing a fixed BLKSIZE
4339 bytes, to lower max_bits. In the unlikely case of a constant LEN
4340 (implied by identical MAX_LEN and MIN_LEN), we want to issue a
4341 single store_by_pieces, but otherwise, select the minimum multiple
4342 of the ALIGN (in bytes) and of the MCD of the possible LENs, that
4343 brings MAX_LEN below TST_BITS, if that's lower than min_len. */
4344 unsigned HOST_WIDE_INT blksize
;
4345 if (max_len
> min_len
)
4347 unsigned HOST_WIDE_INT alrng
= MAX (HOST_WIDE_INT_1U
<< ctz_len
,
4348 align
/ BITS_PER_UNIT
);
4349 blksize
= max_len
- (HOST_WIDE_INT_1U
<< tst_bits
) + alrng
;
4350 blksize
&= ~(alrng
- 1);
4352 else if (max_len
== min_len
)
4355 /* Huh, max_len < min_len? Punt. See pr100843.c. */
4357 if (min_len
>= blksize
)
4360 min_bits
= floor_log2 (min_len
);
4362 max_bits
= floor_log2 (max_len
);
4364 tst_bits
= (max_bits
!= min_bits
? max_bits
4365 : floor_log2 (max_len
^ min_len
));
4370 /* Check that we can use store by pieces for the maximum store count
4371 we may issue (initial fixed-size block, plus conditional
4372 power-of-two-sized from max_bits to ctz_len. */
4373 unsigned HOST_WIDE_INT xlenest
= blksize
;
4375 xlenest
+= ((HOST_WIDE_INT_1U
<< max_bits
) * 2
4376 - (HOST_WIDE_INT_1U
<< ctz_len
));
4377 if (!can_store_by_pieces (xlenest
, builtin_memset_read_str
,
4378 &valc
, align
, true))
4381 by_pieces_constfn constfun
;
4385 constfun
= builtin_memset_gen_str
;
4386 constfundata
= val
= force_reg (TYPE_MODE (unsigned_char_type_node
),
4391 constfun
= builtin_memset_read_str
;
4392 constfundata
= &valc
;
4395 rtx ptr
= copy_addr_to_reg (XEXP (to
, 0));
4396 rtx rem
= copy_to_mode_reg (ptr_mode
, convert_to_mode (ptr_mode
, len
, 0));
4397 to
= replace_equiv_address (to
, ptr
);
4398 set_mem_align (to
, align
);
4402 to
= store_by_pieces (to
, blksize
,
4403 constfun
, constfundata
,
4405 max_len
!= 0 ? RETURN_END
: RETURN_BEGIN
);
4409 /* Adjust PTR, TO and REM. Since TO's address is likely
4410 PTR+offset, we have to replace it. */
4411 emit_move_insn (ptr
, force_operand (XEXP (to
, 0), NULL_RTX
));
4412 to
= replace_equiv_address (to
, ptr
);
4413 rtx rem_minus_blksize
= plus_constant (ptr_mode
, rem
, -blksize
);
4414 emit_move_insn (rem
, force_operand (rem_minus_blksize
, NULL_RTX
));
4417 /* Iterate over power-of-two block sizes from the maximum length to
4418 the least significant bit possibly set in the length. */
4419 for (int i
= max_bits
; i
>= sctz_len
; i
--)
4421 rtx_code_label
*label
= NULL
;
4422 blksize
= HOST_WIDE_INT_1U
<< i
;
4424 /* If we're past the bits shared between min_ and max_len, expand
4425 a test on the dynamic length, comparing it with the
4429 label
= gen_label_rtx ();
4430 emit_cmp_and_jump_insns (rem
, GEN_INT (blksize
), LT
, NULL
,
4432 profile_probability::even ());
4434 /* If we are at a bit that is in the prefix shared by min_ and
4435 max_len, skip this BLKSIZE if the bit is clear. */
4436 else if ((max_len
& blksize
) == 0)
4439 /* Issue a store of BLKSIZE bytes. */
4440 to
= store_by_pieces (to
, blksize
,
4441 constfun
, constfundata
,
4443 i
!= sctz_len
? RETURN_END
: RETURN_BEGIN
);
4445 /* Adjust REM and PTR, unless this is the last iteration. */
4448 emit_move_insn (ptr
, force_operand (XEXP (to
, 0), NULL_RTX
));
4449 to
= replace_equiv_address (to
, ptr
);
4450 rtx rem_minus_blksize
= plus_constant (ptr_mode
, rem
, -blksize
);
4451 emit_move_insn (rem
, force_operand (rem_minus_blksize
, NULL_RTX
));
4458 /* Given conditional stores, the offset can no longer be
4459 known, so clear it. */
4460 clear_mem_offset (to
);
4467 /* Helper function to do the actual work for expand_builtin_memset. The
4468 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4469 so that this can also be called without constructing an actual CALL_EXPR.
4470 The other arguments and return value are the same as for
4471 expand_builtin_memset. */
4474 expand_builtin_memset_args (tree dest
, tree val
, tree len
,
4475 rtx target
, machine_mode mode
, tree orig_exp
)
4478 enum built_in_function fcode
;
4479 machine_mode val_mode
;
4481 unsigned int dest_align
;
4482 rtx dest_mem
, dest_addr
, len_rtx
;
4483 HOST_WIDE_INT expected_size
= -1;
4484 unsigned int expected_align
= 0;
4485 unsigned HOST_WIDE_INT min_size
;
4486 unsigned HOST_WIDE_INT max_size
;
4487 unsigned HOST_WIDE_INT probable_max_size
;
4489 dest_align
= get_pointer_alignment (dest
);
4491 /* If DEST is not a pointer type, don't do this operation in-line. */
4492 if (dest_align
== 0)
4495 if (currently_expanding_gimple_stmt
)
4496 stringop_block_profile (currently_expanding_gimple_stmt
,
4497 &expected_align
, &expected_size
);
4499 if (expected_align
< dest_align
)
4500 expected_align
= dest_align
;
4502 /* If the LEN parameter is zero, return DEST. */
4503 if (integer_zerop (len
))
4505 /* Evaluate and ignore VAL in case it has side-effects. */
4506 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4507 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
4510 /* Stabilize the arguments in case we fail. */
4511 dest
= builtin_save_expr (dest
);
4512 val
= builtin_save_expr (val
);
4513 len
= builtin_save_expr (len
);
4515 len_rtx
= expand_normal (len
);
4516 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
4517 &probable_max_size
);
4518 dest_mem
= get_memory_rtx (dest
, len
);
4519 val_mode
= TYPE_MODE (unsigned_char_type_node
);
4521 if (TREE_CODE (val
) != INTEGER_CST
4522 || target_char_cast (val
, &c
))
4526 val_rtx
= expand_normal (val
);
4527 val_rtx
= convert_to_mode (val_mode
, val_rtx
, 0);
4529 /* Assume that we can memset by pieces if we can store
4530 * the coefficients by pieces (in the required modes).
4531 * We can't pass builtin_memset_gen_str as that emits RTL. */
4533 if (tree_fits_uhwi_p (len
)
4534 && can_store_by_pieces (tree_to_uhwi (len
),
4535 builtin_memset_read_str
, &c
, dest_align
,
4538 val_rtx
= force_reg (val_mode
, val_rtx
);
4539 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
4540 builtin_memset_gen_str
, val_rtx
, dest_align
,
4541 true, RETURN_BEGIN
);
4543 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, val_rtx
,
4544 dest_align
, expected_align
,
4545 expected_size
, min_size
, max_size
,
4547 && !try_store_by_multiple_pieces (dest_mem
, len_rtx
,
4554 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
4555 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
4561 if (tree_fits_uhwi_p (len
)
4562 && can_store_by_pieces (tree_to_uhwi (len
),
4563 builtin_memset_read_str
, &c
, dest_align
,
4565 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
4566 builtin_memset_read_str
, &c
, dest_align
, true,
4568 else if (!set_storage_via_setmem (dest_mem
, len_rtx
,
4569 gen_int_mode (c
, val_mode
),
4570 dest_align
, expected_align
,
4571 expected_size
, min_size
, max_size
,
4573 && !try_store_by_multiple_pieces (dest_mem
, len_rtx
,
4580 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
4581 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
4585 set_mem_align (dest_mem
, dest_align
);
4586 dest_addr
= clear_storage_hints (dest_mem
, len_rtx
,
4587 CALL_EXPR_TAILCALL (orig_exp
)
4588 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
4589 expected_align
, expected_size
,
4591 probable_max_size
, tree_ctz (len
));
4595 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
4596 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
4602 fndecl
= get_callee_fndecl (orig_exp
);
4603 fcode
= DECL_FUNCTION_CODE (fndecl
);
4604 if (fcode
== BUILT_IN_MEMSET
)
4605 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 3,
4607 else if (fcode
== BUILT_IN_BZERO
)
4608 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 2,
4612 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4613 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
4614 return expand_call (fn
, target
, target
== const0_rtx
);
4617 /* Expand expression EXP, which is a call to the bzero builtin. Return
4618 NULL_RTX if we failed the caller should emit a normal call. */
4621 expand_builtin_bzero (tree exp
)
4623 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4626 tree dest
= CALL_EXPR_ARG (exp
, 0);
4627 tree size
= CALL_EXPR_ARG (exp
, 1);
4629 /* New argument list transforming bzero(ptr x, int y) to
4630 memset(ptr x, int 0, size_t y). This is done this way
4631 so that if it isn't expanded inline, we fallback to
4632 calling bzero instead of memset. */
4634 location_t loc
= EXPR_LOCATION (exp
);
4636 return expand_builtin_memset_args (dest
, integer_zero_node
,
4637 fold_convert_loc (loc
,
4638 size_type_node
, size
),
4639 const0_rtx
, VOIDmode
, exp
);
4642 /* Try to expand cmpstr operation ICODE with the given operands.
4643 Return the result rtx on success, otherwise return null. */
4646 expand_cmpstr (insn_code icode
, rtx target
, rtx arg1_rtx
, rtx arg2_rtx
,
4647 HOST_WIDE_INT align
)
4649 machine_mode insn_mode
= insn_data
[icode
].operand
[0].mode
;
4651 if (target
&& (!REG_P (target
) || HARD_REGISTER_P (target
)))
4654 class expand_operand ops
[4];
4655 create_output_operand (&ops
[0], target
, insn_mode
);
4656 create_fixed_operand (&ops
[1], arg1_rtx
);
4657 create_fixed_operand (&ops
[2], arg2_rtx
);
4658 create_integer_operand (&ops
[3], align
);
4659 if (maybe_expand_insn (icode
, 4, ops
))
4660 return ops
[0].value
;
4664 /* Expand expression EXP, which is a call to the memcmp built-in function.
4665 Return NULL_RTX if we failed and the caller should emit a normal call,
4666 otherwise try to get the result in TARGET, if convenient.
4667 RESULT_EQ is true if we can relax the returned value to be either zero
4668 or nonzero, without caring about the sign. */
4671 expand_builtin_memcmp (tree exp
, rtx target
, bool result_eq
)
4673 if (!validate_arglist (exp
,
4674 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4677 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4678 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4679 tree len
= CALL_EXPR_ARG (exp
, 2);
4681 /* Due to the performance benefit, always inline the calls first
4682 when result_eq is false. */
4683 rtx result
= NULL_RTX
;
4684 enum built_in_function fcode
= DECL_FUNCTION_CODE (get_callee_fndecl (exp
));
4685 if (!result_eq
&& fcode
!= BUILT_IN_BCMP
)
4687 result
= inline_expand_builtin_bytecmp (exp
, target
);
4692 machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
4693 location_t loc
= EXPR_LOCATION (exp
);
4695 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4696 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4698 /* If we don't have POINTER_TYPE, call the function. */
4699 if (arg1_align
== 0 || arg2_align
== 0)
4702 rtx arg1_rtx
= get_memory_rtx (arg1
, len
);
4703 rtx arg2_rtx
= get_memory_rtx (arg2
, len
);
4704 rtx len_rtx
= expand_normal (fold_convert_loc (loc
, sizetype
, len
));
4706 /* Set MEM_SIZE as appropriate. */
4707 if (CONST_INT_P (len_rtx
))
4709 set_mem_size (arg1_rtx
, INTVAL (len_rtx
));
4710 set_mem_size (arg2_rtx
, INTVAL (len_rtx
));
4713 by_pieces_constfn constfn
= NULL
;
4715 /* Try to get the byte representation of the constant ARG2 (or, only
4716 when the function's result is used for equality to zero, ARG1)
4717 points to, with its byte size in NBYTES. */
4718 unsigned HOST_WIDE_INT nbytes
;
4719 const char *rep
= getbyterep (arg2
, &nbytes
);
4720 if (result_eq
&& rep
== NULL
)
4722 /* For equality to zero the arguments are interchangeable. */
4723 rep
= getbyterep (arg1
, &nbytes
);
4725 std::swap (arg1_rtx
, arg2_rtx
);
4728 /* If the function's constant bound LEN_RTX is less than or equal
4729 to the byte size of the representation of the constant argument,
4730 and if block move would be done by pieces, we can avoid loading
4731 the bytes from memory and only store the computed constant result. */
4733 && CONST_INT_P (len_rtx
)
4734 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= nbytes
)
4735 constfn
= builtin_memcpy_read_str
;
4737 result
= emit_block_cmp_hints (arg1_rtx
, arg2_rtx
, len_rtx
,
4738 TREE_TYPE (len
), target
,
4740 CONST_CAST (char *, rep
));
4744 /* Return the value in the proper mode for this function. */
4745 if (GET_MODE (result
) == mode
)
4750 convert_move (target
, result
, 0);
4754 return convert_to_mode (mode
, result
, 0);
4760 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4761 if we failed the caller should emit a normal call, otherwise try to get
4762 the result in TARGET, if convenient. */
4765 expand_builtin_strcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
)
4767 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4770 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4771 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4773 /* Due to the performance benefit, always inline the calls first. */
4774 rtx result
= NULL_RTX
;
4775 result
= inline_expand_builtin_bytecmp (exp
, target
);
4779 insn_code cmpstr_icode
= direct_optab_handler (cmpstr_optab
, SImode
);
4780 insn_code cmpstrn_icode
= direct_optab_handler (cmpstrn_optab
, SImode
);
4781 if (cmpstr_icode
== CODE_FOR_nothing
&& cmpstrn_icode
== CODE_FOR_nothing
)
4784 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4785 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4787 /* If we don't have POINTER_TYPE, call the function. */
4788 if (arg1_align
== 0 || arg2_align
== 0)
4791 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4792 arg1
= builtin_save_expr (arg1
);
4793 arg2
= builtin_save_expr (arg2
);
4795 rtx arg1_rtx
= get_memory_rtx (arg1
, NULL
);
4796 rtx arg2_rtx
= get_memory_rtx (arg2
, NULL
);
4798 /* Try to call cmpstrsi. */
4799 if (cmpstr_icode
!= CODE_FOR_nothing
)
4800 result
= expand_cmpstr (cmpstr_icode
, target
, arg1_rtx
, arg2_rtx
,
4801 MIN (arg1_align
, arg2_align
));
4803 /* Try to determine at least one length and call cmpstrnsi. */
4804 if (!result
&& cmpstrn_icode
!= CODE_FOR_nothing
)
4809 tree len1
= c_strlen (arg1
, 1);
4810 tree len2
= c_strlen (arg2
, 1);
4813 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
4815 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
4817 /* If we don't have a constant length for the first, use the length
4818 of the second, if we know it. We don't require a constant for
4819 this case; some cost analysis could be done if both are available
4820 but neither is constant. For now, assume they're equally cheap,
4821 unless one has side effects. If both strings have constant lengths,
4828 else if (TREE_SIDE_EFFECTS (len1
))
4830 else if (TREE_SIDE_EFFECTS (len2
))
4832 else if (TREE_CODE (len1
) != INTEGER_CST
)
4834 else if (TREE_CODE (len2
) != INTEGER_CST
)
4836 else if (tree_int_cst_lt (len1
, len2
))
4841 /* If both arguments have side effects, we cannot optimize. */
4842 if (len
&& !TREE_SIDE_EFFECTS (len
))
4844 arg3_rtx
= expand_normal (len
);
4845 result
= expand_cmpstrn_or_cmpmem
4846 (cmpstrn_icode
, target
, arg1_rtx
, arg2_rtx
, TREE_TYPE (len
),
4847 arg3_rtx
, MIN (arg1_align
, arg2_align
));
4851 tree fndecl
= get_callee_fndecl (exp
);
4854 /* Return the value in the proper mode for this function. */
4855 machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
4856 if (GET_MODE (result
) == mode
)
4859 return convert_to_mode (mode
, result
, 0);
4860 convert_move (target
, result
, 0);
4864 /* Expand the library call ourselves using a stabilized argument
4865 list to avoid re-evaluating the function's arguments twice. */
4866 tree fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 2, arg1
, arg2
);
4867 copy_warning (fn
, exp
);
4868 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4869 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4870 return expand_call (fn
, target
, target
== const0_rtx
);
4873 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4874 NULL_RTX if we failed the caller should emit a normal call, otherwise
4875 try to get the result in TARGET, if convenient. */
4878 expand_builtin_strncmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
4879 ATTRIBUTE_UNUSED machine_mode mode
)
4881 if (!validate_arglist (exp
,
4882 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4885 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4886 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4887 tree arg3
= CALL_EXPR_ARG (exp
, 2);
4889 location_t loc
= EXPR_LOCATION (exp
);
4890 tree len1
= c_strlen (arg1
, 1);
4891 tree len2
= c_strlen (arg2
, 1);
4893 /* Due to the performance benefit, always inline the calls first. */
4894 rtx result
= NULL_RTX
;
4895 result
= inline_expand_builtin_bytecmp (exp
, target
);
4899 /* If c_strlen can determine an expression for one of the string
4900 lengths, and it doesn't have side effects, then emit cmpstrnsi
4901 using length MIN(strlen(string)+1, arg3). */
4902 insn_code cmpstrn_icode
= direct_optab_handler (cmpstrn_optab
, SImode
);
4903 if (cmpstrn_icode
== CODE_FOR_nothing
)
4908 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4909 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4912 len1
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len1
);
4914 len2
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len2
);
4916 tree len3
= fold_convert_loc (loc
, sizetype
, arg3
);
4918 /* If we don't have a constant length for the first, use the length
4919 of the second, if we know it. If neither string is constant length,
4920 use the given length argument. We don't require a constant for
4921 this case; some cost analysis could be done if both are available
4922 but neither is constant. For now, assume they're equally cheap,
4923 unless one has side effects. If both strings have constant lengths,
4932 else if (TREE_SIDE_EFFECTS (len1
))
4934 else if (TREE_SIDE_EFFECTS (len2
))
4936 else if (TREE_CODE (len1
) != INTEGER_CST
)
4938 else if (TREE_CODE (len2
) != INTEGER_CST
)
4940 else if (tree_int_cst_lt (len1
, len2
))
4945 /* If we are not using the given length, we must incorporate it here.
4946 The actual new length parameter will be MIN(len,arg3) in this case. */
4949 len
= fold_convert_loc (loc
, sizetype
, len
);
4950 len
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (len
), len
, len3
);
4952 rtx arg1_rtx
= get_memory_rtx (arg1
, len
);
4953 rtx arg2_rtx
= get_memory_rtx (arg2
, len
);
4954 rtx arg3_rtx
= expand_normal (len
);
4955 result
= expand_cmpstrn_or_cmpmem (cmpstrn_icode
, target
, arg1_rtx
,
4956 arg2_rtx
, TREE_TYPE (len
), arg3_rtx
,
4957 MIN (arg1_align
, arg2_align
));
4959 tree fndecl
= get_callee_fndecl (exp
);
4962 /* Return the value in the proper mode for this function. */
4963 mode
= TYPE_MODE (TREE_TYPE (exp
));
4964 if (GET_MODE (result
) == mode
)
4967 return convert_to_mode (mode
, result
, 0);
4968 convert_move (target
, result
, 0);
4972 /* Expand the library call ourselves using a stabilized argument
4973 list to avoid re-evaluating the function's arguments twice. */
4974 tree call
= build_call_nofold_loc (loc
, fndecl
, 3, arg1
, arg2
, len
);
4975 copy_warning (call
, exp
);
4976 gcc_assert (TREE_CODE (call
) == CALL_EXPR
);
4977 CALL_EXPR_TAILCALL (call
) = CALL_EXPR_TAILCALL (exp
);
4978 return expand_call (call
, target
, target
== const0_rtx
);
4981 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4982 if that's convenient. */
4985 expand_builtin_saveregs (void)
4990 /* Don't do __builtin_saveregs more than once in a function.
4991 Save the result of the first call and reuse it. */
4992 if (saveregs_value
!= 0)
4993 return saveregs_value
;
4995 /* When this function is called, it means that registers must be
4996 saved on entry to this function. So we migrate the call to the
4997 first insn of this function. */
5001 /* Do whatever the machine needs done in this case. */
5002 val
= targetm
.calls
.expand_builtin_saveregs ();
5007 saveregs_value
= val
;
5009 /* Put the insns after the NOTE that starts the function. If this
5010 is inside a start_sequence, make the outer-level insn chain current, so
5011 the code is placed at the start of the function. */
5012 push_topmost_sequence ();
5013 emit_insn_after (seq
, entry_of_function ());
5014 pop_topmost_sequence ();
5019 /* Expand a call to __builtin_next_arg. */
5022 expand_builtin_next_arg (void)
5024 /* Checking arguments is already done in fold_builtin_next_arg
5025 that must be called before this function. */
5026 return expand_binop (ptr_mode
, add_optab
,
5027 crtl
->args
.internal_arg_pointer
,
5028 crtl
->args
.arg_offset_rtx
,
5029 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
5032 /* Make it easier for the backends by protecting the valist argument
5033 from multiple evaluations. */
5036 stabilize_va_list_loc (location_t loc
, tree valist
, int needs_lvalue
)
5038 tree vatype
= targetm
.canonical_va_list_type (TREE_TYPE (valist
));
5040 /* The current way of determining the type of valist is completely
5041 bogus. We should have the information on the va builtin instead. */
5043 vatype
= targetm
.fn_abi_va_list (cfun
->decl
);
5045 if (TREE_CODE (vatype
) == ARRAY_TYPE
)
5047 if (TREE_SIDE_EFFECTS (valist
))
5048 valist
= save_expr (valist
);
5050 /* For this case, the backends will be expecting a pointer to
5051 vatype, but it's possible we've actually been given an array
5052 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
5054 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
5056 tree p1
= build_pointer_type (TREE_TYPE (vatype
));
5057 valist
= build_fold_addr_expr_with_type_loc (loc
, valist
, p1
);
5062 tree pt
= build_pointer_type (vatype
);
5066 if (! TREE_SIDE_EFFECTS (valist
))
5069 valist
= fold_build1_loc (loc
, ADDR_EXPR
, pt
, valist
);
5070 TREE_SIDE_EFFECTS (valist
) = 1;
5073 if (TREE_SIDE_EFFECTS (valist
))
5074 valist
= save_expr (valist
);
5075 valist
= fold_build2_loc (loc
, MEM_REF
,
5076 vatype
, valist
, build_int_cst (pt
, 0));
5082 /* The "standard" definition of va_list is void*. */
5085 std_build_builtin_va_list (void)
5087 return ptr_type_node
;
5090 /* The "standard" abi va_list is va_list_type_node. */
5093 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED
)
5095 return va_list_type_node
;
5098 /* The "standard" type of va_list is va_list_type_node. */
5101 std_canonical_va_list_type (tree type
)
5105 wtype
= va_list_type_node
;
5108 if (TREE_CODE (wtype
) == ARRAY_TYPE
)
5110 /* If va_list is an array type, the argument may have decayed
5111 to a pointer type, e.g. by being passed to another function.
5112 In that case, unwrap both types so that we can compare the
5113 underlying records. */
5114 if (TREE_CODE (htype
) == ARRAY_TYPE
5115 || POINTER_TYPE_P (htype
))
5117 wtype
= TREE_TYPE (wtype
);
5118 htype
= TREE_TYPE (htype
);
5121 if (TYPE_MAIN_VARIANT (wtype
) == TYPE_MAIN_VARIANT (htype
))
5122 return va_list_type_node
;
5127 /* The "standard" implementation of va_start: just assign `nextarg' to
5131 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
5133 rtx va_r
= expand_expr (valist
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
5134 convert_move (va_r
, nextarg
, 0);
5137 /* Expand EXP, a call to __builtin_va_start. */
5140 expand_builtin_va_start (tree exp
)
5144 location_t loc
= EXPR_LOCATION (exp
);
5146 if (call_expr_nargs (exp
) < 2)
5148 error_at (loc
, "too few arguments to function %<va_start%>");
5152 if (fold_builtin_next_arg (exp
, true))
5155 nextarg
= expand_builtin_next_arg ();
5156 valist
= stabilize_va_list_loc (loc
, CALL_EXPR_ARG (exp
, 0), 1);
5158 if (targetm
.expand_builtin_va_start
)
5159 targetm
.expand_builtin_va_start (valist
, nextarg
);
5161 std_expand_builtin_va_start (valist
, nextarg
);
5166 /* Expand EXP, a call to __builtin_va_end. */
5169 expand_builtin_va_end (tree exp
)
5171 tree valist
= CALL_EXPR_ARG (exp
, 0);
5173 /* Evaluate for side effects, if needed. I hate macros that don't
5175 if (TREE_SIDE_EFFECTS (valist
))
5176 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5181 /* Expand EXP, a call to __builtin_va_copy. We do this as a
5182 builtin rather than just as an assignment in stdarg.h because of the
5183 nastiness of array-type va_list types. */
5186 expand_builtin_va_copy (tree exp
)
5189 location_t loc
= EXPR_LOCATION (exp
);
5191 dst
= CALL_EXPR_ARG (exp
, 0);
5192 src
= CALL_EXPR_ARG (exp
, 1);
5194 dst
= stabilize_va_list_loc (loc
, dst
, 1);
5195 src
= stabilize_va_list_loc (loc
, src
, 0);
5197 gcc_assert (cfun
!= NULL
&& cfun
->decl
!= NULL_TREE
);
5199 if (TREE_CODE (targetm
.fn_abi_va_list (cfun
->decl
)) != ARRAY_TYPE
)
5201 t
= build2 (MODIFY_EXPR
, targetm
.fn_abi_va_list (cfun
->decl
), dst
, src
);
5202 TREE_SIDE_EFFECTS (t
) = 1;
5203 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5207 rtx dstb
, srcb
, size
;
5209 /* Evaluate to pointers. */
5210 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
5211 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
5212 size
= expand_expr (TYPE_SIZE_UNIT (targetm
.fn_abi_va_list (cfun
->decl
)),
5213 NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
5215 dstb
= convert_memory_address (Pmode
, dstb
);
5216 srcb
= convert_memory_address (Pmode
, srcb
);
5218 /* "Dereference" to BLKmode memories. */
5219 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
5220 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
5221 set_mem_align (dstb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
5222 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
5223 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
5224 set_mem_align (srcb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
5227 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
5233 /* Expand a call to one of the builtin functions __builtin_frame_address or
5234 __builtin_return_address. */
5237 expand_builtin_frame_address (tree fndecl
, tree exp
)
5239 /* The argument must be a nonnegative integer constant.
5240 It counts the number of frames to scan up the stack.
5241 The value is either the frame pointer value or the return
5242 address saved in that frame. */
5243 if (call_expr_nargs (exp
) == 0)
5244 /* Warning about missing arg was already issued. */
5246 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp
, 0)))
5248 error ("invalid argument to %qD", fndecl
);
5253 /* Number of frames to scan up the stack. */
5254 unsigned HOST_WIDE_INT count
= tree_to_uhwi (CALL_EXPR_ARG (exp
, 0));
5256 rtx tem
= expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
), count
);
5258 /* Some ports cannot access arbitrary stack frames. */
5261 warning (0, "unsupported argument to %qD", fndecl
);
5267 /* Warn since no effort is made to ensure that any frame
5268 beyond the current one exists or can be safely reached. */
5269 warning (OPT_Wframe_address
, "calling %qD with "
5270 "a nonzero argument is unsafe", fndecl
);
5273 /* For __builtin_frame_address, return what we've got. */
5274 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
5278 && ! CONSTANT_P (tem
))
5279 tem
= copy_addr_to_reg (tem
);
5284 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5285 failed and the caller should emit a normal call. */
5288 expand_builtin_alloca (tree exp
)
5293 tree fndecl
= get_callee_fndecl (exp
);
5294 HOST_WIDE_INT max_size
;
5295 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5296 bool alloca_for_var
= CALL_ALLOCA_FOR_VAR_P (exp
);
5298 = (fcode
== BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5299 ? validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
,
5301 : fcode
== BUILT_IN_ALLOCA_WITH_ALIGN
5302 ? validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
5303 : validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
));
5308 /* Compute the argument. */
5309 op0
= expand_normal (CALL_EXPR_ARG (exp
, 0));
5311 /* Compute the alignment. */
5312 align
= (fcode
== BUILT_IN_ALLOCA
5314 : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp
, 1)));
5316 /* Compute the maximum size. */
5317 max_size
= (fcode
== BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5318 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp
, 2))
5321 /* Allocate the desired space. If the allocation stems from the declaration
5322 of a variable-sized object, it cannot accumulate. */
5324 = allocate_dynamic_stack_space (op0
, 0, align
, max_size
, alloca_for_var
);
5325 result
= convert_memory_address (ptr_mode
, result
);
5327 /* Dynamic allocations for variables are recorded during gimplification. */
5328 if (!alloca_for_var
&& (flag_callgraph_info
& CALLGRAPH_INFO_DYNAMIC_ALLOC
))
5329 record_dynamic_alloc (exp
);
5334 /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
5335 of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
5336 STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
5337 handle_builtin_stack_restore function. */
5340 expand_asan_emit_allocas_unpoison (tree exp
)
5342 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5343 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5344 rtx top
= expand_expr (arg0
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
5345 rtx bot
= expand_expr (arg1
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
5346 rtx off
= expand_simple_binop (Pmode
, MINUS
, virtual_stack_dynamic_rtx
,
5347 stack_pointer_rtx
, NULL_RTX
, 0,
5349 off
= convert_modes (ptr_mode
, Pmode
, off
, 0);
5350 bot
= expand_simple_binop (ptr_mode
, PLUS
, bot
, off
, NULL_RTX
, 0,
5352 rtx ret
= init_one_libfunc ("__asan_allocas_unpoison");
5353 ret
= emit_library_call_value (ret
, NULL_RTX
, LCT_NORMAL
, ptr_mode
,
5354 top
, ptr_mode
, bot
, ptr_mode
);
5358 /* Expand a call to bswap builtin in EXP.
5359 Return NULL_RTX if a normal call should be emitted rather than expanding the
5360 function in-line. If convenient, the result should be placed in TARGET.
5361 SUBTARGET may be used as the target for computing one of EXP's operands. */
5364 expand_builtin_bswap (machine_mode target_mode
, tree exp
, rtx target
,
5370 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
5373 arg
= CALL_EXPR_ARG (exp
, 0);
5374 op0
= expand_expr (arg
,
5375 subtarget
&& GET_MODE (subtarget
) == target_mode
5376 ? subtarget
: NULL_RTX
,
5377 target_mode
, EXPAND_NORMAL
);
5378 if (GET_MODE (op0
) != target_mode
)
5379 op0
= convert_to_mode (target_mode
, op0
, 1);
5381 target
= expand_unop (target_mode
, bswap_optab
, op0
, target
, 1);
5383 gcc_assert (target
);
5385 return convert_to_mode (target_mode
, target
, 1);
5388 /* Expand a call to a unary builtin in EXP.
5389 Return NULL_RTX if a normal call should be emitted rather than expanding the
5390 function in-line. If convenient, the result should be placed in TARGET.
5391 SUBTARGET may be used as the target for computing one of EXP's operands. */
5394 expand_builtin_unop (machine_mode target_mode
, tree exp
, rtx target
,
5395 rtx subtarget
, optab op_optab
)
5399 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
5402 /* Compute the argument. */
5403 op0
= expand_expr (CALL_EXPR_ARG (exp
, 0),
5405 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0)))
5406 == GET_MODE (subtarget
))) ? subtarget
: NULL_RTX
,
5407 VOIDmode
, EXPAND_NORMAL
);
5408 /* Compute op, into TARGET if possible.
5409 Set TARGET to wherever the result comes back. */
5410 target
= expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))),
5411 op_optab
, op0
, target
, op_optab
!= clrsb_optab
);
5412 gcc_assert (target
);
5414 return convert_to_mode (target_mode
, target
, 0);
5417 /* Expand a call to __builtin_expect. We just return our argument
5418 as the builtin_expect semantic should've been already executed by
5419 tree branch prediction pass. */
5422 expand_builtin_expect (tree exp
, rtx target
)
5426 if (call_expr_nargs (exp
) < 2)
5428 arg
= CALL_EXPR_ARG (exp
, 0);
5430 target
= expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
5431 /* When guessing was done, the hints should be already stripped away. */
5432 gcc_assert (!flag_guess_branch_prob
5433 || optimize
== 0 || seen_error ());
5437 /* Expand a call to __builtin_expect_with_probability. We just return our
5438 argument as the builtin_expect semantic should've been already executed by
5439 tree branch prediction pass. */
5442 expand_builtin_expect_with_probability (tree exp
, rtx target
)
5446 if (call_expr_nargs (exp
) < 3)
5448 arg
= CALL_EXPR_ARG (exp
, 0);
5450 target
= expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
5451 /* When guessing was done, the hints should be already stripped away. */
5452 gcc_assert (!flag_guess_branch_prob
5453 || optimize
== 0 || seen_error ());
5458 /* Expand a call to __builtin_assume_aligned. We just return our first
5459 argument as the builtin_assume_aligned semantic should've been already
5463 expand_builtin_assume_aligned (tree exp
, rtx target
)
5465 if (call_expr_nargs (exp
) < 2)
5467 target
= expand_expr (CALL_EXPR_ARG (exp
, 0), target
, VOIDmode
,
5469 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 1))
5470 && (call_expr_nargs (exp
) < 3
5471 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 2))));
5476 expand_builtin_trap (void)
5478 if (targetm
.have_trap ())
5480 rtx_insn
*insn
= emit_insn (targetm
.gen_trap ());
5481 /* For trap insns when not accumulating outgoing args force
5482 REG_ARGS_SIZE note to prevent crossjumping of calls with
5483 different args sizes. */
5484 if (!ACCUMULATE_OUTGOING_ARGS
)
5485 add_args_size_note (insn
, stack_pointer_delta
);
5489 tree fn
= builtin_decl_implicit (BUILT_IN_ABORT
);
5490 tree call_expr
= build_call_expr (fn
, 0);
5491 expand_call (call_expr
, NULL_RTX
, false);
5497 /* Expand a call to __builtin_unreachable. We do nothing except emit
5498 a barrier saying that control flow will not pass here.
5500 It is the responsibility of the program being compiled to ensure
5501 that control flow does never reach __builtin_unreachable. */
5503 expand_builtin_unreachable (void)
5505 /* Use gimple_build_builtin_unreachable or builtin_decl_unreachable
5507 gcc_checking_assert (!sanitize_flags_p (SANITIZE_UNREACHABLE
));
5511 /* Expand EXP, a call to fabs, fabsf or fabsl.
5512 Return NULL_RTX if a normal call should be emitted rather than expanding
5513 the function inline. If convenient, the result should be placed
5514 in TARGET. SUBTARGET may be used as the target for computing
5518 expand_builtin_fabs (tree exp
, rtx target
, rtx subtarget
)
5524 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
5527 arg
= CALL_EXPR_ARG (exp
, 0);
5528 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
5529 mode
= TYPE_MODE (TREE_TYPE (arg
));
5530 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
5531 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
5534 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5535 Return NULL is a normal call should be emitted rather than expanding the
5536 function inline. If convenient, the result should be placed in TARGET.
5537 SUBTARGET may be used as the target for computing the operand. */
5540 expand_builtin_copysign (tree exp
, rtx target
, rtx subtarget
)
5545 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
5548 arg
= CALL_EXPR_ARG (exp
, 0);
5549 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
5551 arg
= CALL_EXPR_ARG (exp
, 1);
5552 op1
= expand_normal (arg
);
5554 return expand_copysign (op0
, op1
, target
);
5557 /* Emit a call to __builtin___clear_cache. */
5560 default_emit_call_builtin___clear_cache (rtx begin
, rtx end
)
5562 rtx callee
= gen_rtx_SYMBOL_REF (Pmode
,
5563 BUILTIN_ASM_NAME_PTR
5564 (BUILT_IN_CLEAR_CACHE
));
5566 emit_library_call (callee
,
5567 LCT_NORMAL
, VOIDmode
,
5568 convert_memory_address (ptr_mode
, begin
), ptr_mode
,
5569 convert_memory_address (ptr_mode
, end
), ptr_mode
);
5572 /* Emit a call to __builtin___clear_cache, unless the target specifies
5573 it as do-nothing. This function can be used by trampoline
5574 finalizers to duplicate the effects of expanding a call to the
5575 clear_cache builtin. */
5578 maybe_emit_call_builtin___clear_cache (rtx begin
, rtx end
)
5580 gcc_assert ((GET_MODE (begin
) == ptr_mode
|| GET_MODE (begin
) == Pmode
5581 || CONST_INT_P (begin
))
5582 && (GET_MODE (end
) == ptr_mode
|| GET_MODE (end
) == Pmode
5583 || CONST_INT_P (end
)));
5585 if (targetm
.have_clear_cache ())
5587 /* We have a "clear_cache" insn, and it will handle everything. */
5588 class expand_operand ops
[2];
5590 create_address_operand (&ops
[0], begin
);
5591 create_address_operand (&ops
[1], end
);
5593 if (maybe_expand_insn (targetm
.code_for_clear_cache
, 2, ops
))
5598 #ifndef CLEAR_INSN_CACHE
5599 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5600 does nothing. There is no need to call it. Do nothing. */
5602 #endif /* CLEAR_INSN_CACHE */
5605 targetm
.calls
.emit_call_builtin___clear_cache (begin
, end
);
5608 /* Expand a call to __builtin___clear_cache. */
5611 expand_builtin___clear_cache (tree exp
)
5614 rtx begin_rtx
, end_rtx
;
5616 /* We must not expand to a library call. If we did, any
5617 fallback library function in libgcc that might contain a call to
5618 __builtin___clear_cache() would recurse infinitely. */
5619 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
5621 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5625 begin
= CALL_EXPR_ARG (exp
, 0);
5626 begin_rtx
= expand_expr (begin
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
5628 end
= CALL_EXPR_ARG (exp
, 1);
5629 end_rtx
= expand_expr (end
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
5631 maybe_emit_call_builtin___clear_cache (begin_rtx
, end_rtx
);
5634 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5637 round_trampoline_addr (rtx tramp
)
5639 rtx temp
, addend
, mask
;
5641 /* If we don't need too much alignment, we'll have been guaranteed
5642 proper alignment by get_trampoline_type. */
5643 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
5646 /* Round address up to desired boundary. */
5647 temp
= gen_reg_rtx (Pmode
);
5648 addend
= gen_int_mode (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1, Pmode
);
5649 mask
= gen_int_mode (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
, Pmode
);
5651 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
5652 temp
, 0, OPTAB_LIB_WIDEN
);
5653 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
5654 temp
, 0, OPTAB_LIB_WIDEN
);
5660 expand_builtin_init_trampoline (tree exp
, bool onstack
)
5662 tree t_tramp
, t_func
, t_chain
;
5663 rtx m_tramp
, r_tramp
, r_chain
, tmp
;
5665 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
,
5666 POINTER_TYPE
, VOID_TYPE
))
5669 t_tramp
= CALL_EXPR_ARG (exp
, 0);
5670 t_func
= CALL_EXPR_ARG (exp
, 1);
5671 t_chain
= CALL_EXPR_ARG (exp
, 2);
5673 r_tramp
= expand_normal (t_tramp
);
5674 m_tramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
5675 MEM_NOTRAP_P (m_tramp
) = 1;
5677 /* If ONSTACK, the TRAMP argument should be the address of a field
5678 within the local function's FRAME decl. Either way, let's see if
5679 we can fill in the MEM_ATTRs for this memory. */
5680 if (TREE_CODE (t_tramp
) == ADDR_EXPR
)
5681 set_mem_attributes (m_tramp
, TREE_OPERAND (t_tramp
, 0), true);
5683 /* Creator of a heap trampoline is responsible for making sure the
5684 address is aligned to at least STACK_BOUNDARY. Normally malloc
5685 will ensure this anyhow. */
5686 tmp
= round_trampoline_addr (r_tramp
);
5689 m_tramp
= change_address (m_tramp
, BLKmode
, tmp
);
5690 set_mem_align (m_tramp
, TRAMPOLINE_ALIGNMENT
);
5691 set_mem_size (m_tramp
, TRAMPOLINE_SIZE
);
5694 /* The FUNC argument should be the address of the nested function.
5695 Extract the actual function decl to pass to the hook. */
5696 gcc_assert (TREE_CODE (t_func
) == ADDR_EXPR
);
5697 t_func
= TREE_OPERAND (t_func
, 0);
5698 gcc_assert (TREE_CODE (t_func
) == FUNCTION_DECL
);
5700 r_chain
= expand_normal (t_chain
);
5702 /* Generate insns to initialize the trampoline. */
5703 targetm
.calls
.trampoline_init (m_tramp
, t_func
, r_chain
);
5707 trampolines_created
= 1;
5709 if (targetm
.calls
.custom_function_descriptors
!= 0)
5710 warning_at (DECL_SOURCE_LOCATION (t_func
), OPT_Wtrampolines
,
5711 "trampoline generated for nested function %qD", t_func
);
5718 expand_builtin_adjust_trampoline (tree exp
)
5722 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
5725 tramp
= expand_normal (CALL_EXPR_ARG (exp
, 0));
5726 tramp
= round_trampoline_addr (tramp
);
5727 if (targetm
.calls
.trampoline_adjust_address
)
5728 tramp
= targetm
.calls
.trampoline_adjust_address (tramp
);
5733 /* Expand a call to the builtin descriptor initialization routine.
5734 A descriptor is made up of a couple of pointers to the static
5735 chain and the code entry in this order. */
5738 expand_builtin_init_descriptor (tree exp
)
5740 tree t_descr
, t_func
, t_chain
;
5741 rtx m_descr
, r_descr
, r_func
, r_chain
;
5743 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, POINTER_TYPE
,
5747 t_descr
= CALL_EXPR_ARG (exp
, 0);
5748 t_func
= CALL_EXPR_ARG (exp
, 1);
5749 t_chain
= CALL_EXPR_ARG (exp
, 2);
5751 r_descr
= expand_normal (t_descr
);
5752 m_descr
= gen_rtx_MEM (BLKmode
, r_descr
);
5753 MEM_NOTRAP_P (m_descr
) = 1;
5754 set_mem_align (m_descr
, GET_MODE_ALIGNMENT (ptr_mode
));
5756 r_func
= expand_normal (t_func
);
5757 r_chain
= expand_normal (t_chain
);
5759 /* Generate insns to initialize the descriptor. */
5760 emit_move_insn (adjust_address_nv (m_descr
, ptr_mode
, 0), r_chain
);
5761 emit_move_insn (adjust_address_nv (m_descr
, ptr_mode
,
5762 POINTER_SIZE
/ BITS_PER_UNIT
), r_func
);
5767 /* Expand a call to the builtin descriptor adjustment routine. */
5770 expand_builtin_adjust_descriptor (tree exp
)
5774 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
5777 tramp
= expand_normal (CALL_EXPR_ARG (exp
, 0));
5779 /* Unalign the descriptor to allow runtime identification. */
5780 tramp
= plus_constant (ptr_mode
, tramp
,
5781 targetm
.calls
.custom_function_descriptors
);
5783 return force_operand (tramp
, NULL_RTX
);
5786 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5787 function. The function first checks whether the back end provides
5788 an insn to implement signbit for the respective mode. If not, it
5789 checks whether the floating point format of the value is such that
5790 the sign bit can be extracted. If that is not the case, error out.
5791 EXP is the expression that is a call to the builtin function; if
5792 convenient, the result should be placed in TARGET. */
5794 expand_builtin_signbit (tree exp
, rtx target
)
5796 const struct real_format
*fmt
;
5797 scalar_float_mode fmode
;
5798 scalar_int_mode rmode
, imode
;
5801 enum insn_code icode
;
5803 location_t loc
= EXPR_LOCATION (exp
);
5805 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
5808 arg
= CALL_EXPR_ARG (exp
, 0);
5809 fmode
= SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg
));
5810 rmode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (exp
));
5811 fmt
= REAL_MODE_FORMAT (fmode
);
5813 arg
= builtin_save_expr (arg
);
5815 /* Expand the argument yielding a RTX expression. */
5816 temp
= expand_normal (arg
);
5818 /* Check if the back end provides an insn that handles signbit for the
5820 icode
= optab_handler (signbit_optab
, fmode
);
5821 if (icode
!= CODE_FOR_nothing
)
5823 rtx_insn
*last
= get_last_insn ();
5824 rtx this_target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
5825 if (maybe_emit_unop_insn (icode
, this_target
, temp
, UNKNOWN
))
5827 delete_insns_since (last
);
5830 /* For floating point formats without a sign bit, implement signbit
5832 bitpos
= fmt
->signbit_ro
;
5835 /* But we can't do this if the format supports signed zero. */
5836 gcc_assert (!fmt
->has_signed_zero
|| !HONOR_SIGNED_ZEROS (fmode
));
5838 arg
= fold_build2_loc (loc
, LT_EXPR
, TREE_TYPE (exp
), arg
,
5839 build_real (TREE_TYPE (arg
), dconst0
));
5840 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
5843 if (GET_MODE_SIZE (fmode
) <= UNITS_PER_WORD
)
5845 imode
= int_mode_for_mode (fmode
).require ();
5846 temp
= gen_lowpart (imode
, temp
);
5851 /* Handle targets with different FP word orders. */
5852 if (FLOAT_WORDS_BIG_ENDIAN
)
5853 word
= (GET_MODE_BITSIZE (fmode
) - bitpos
) / BITS_PER_WORD
;
5855 word
= bitpos
/ BITS_PER_WORD
;
5856 temp
= operand_subword_force (temp
, word
, fmode
);
5857 bitpos
= bitpos
% BITS_PER_WORD
;
5860 /* Force the intermediate word_mode (or narrower) result into a
5861 register. This avoids attempting to create paradoxical SUBREGs
5862 of floating point modes below. */
5863 temp
= force_reg (imode
, temp
);
5865 /* If the bitpos is within the "result mode" lowpart, the operation
5866 can be implement with a single bitwise AND. Otherwise, we need
5867 a right shift and an AND. */
5869 if (bitpos
< GET_MODE_BITSIZE (rmode
))
5871 wide_int mask
= wi::set_bit_in_zero (bitpos
, GET_MODE_PRECISION (rmode
));
5873 if (GET_MODE_SIZE (imode
) > GET_MODE_SIZE (rmode
))
5874 temp
= gen_lowpart (rmode
, temp
);
5875 temp
= expand_binop (rmode
, and_optab
, temp
,
5876 immed_wide_int_const (mask
, rmode
),
5877 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5881 /* Perform a logical right shift to place the signbit in the least
5882 significant bit, then truncate the result to the desired mode
5883 and mask just this bit. */
5884 temp
= expand_shift (RSHIFT_EXPR
, imode
, temp
, bitpos
, NULL_RTX
, 1);
5885 temp
= gen_lowpart (rmode
, temp
);
5886 temp
= expand_binop (rmode
, and_optab
, temp
, const1_rtx
,
5887 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
5893 /* Expand fork or exec calls. TARGET is the desired target of the
5894 call. EXP is the call. FN is the
5895 identificator of the actual function. IGNORE is nonzero if the
5896 value is to be ignored. */
5899 expand_builtin_fork_or_exec (tree fn
, tree exp
, rtx target
, int ignore
)
5904 /* If we are not profiling, just call the function. */
5905 if (!profile_arc_flag
)
5908 /* Otherwise call the wrapper. This should be equivalent for the rest of
5909 compiler, so the code does not diverge, and the wrapper may run the
5910 code necessary for keeping the profiling sane. */
5912 switch (DECL_FUNCTION_CODE (fn
))
5915 id
= get_identifier ("__gcov_fork");
5918 case BUILT_IN_EXECL
:
5919 id
= get_identifier ("__gcov_execl");
5922 case BUILT_IN_EXECV
:
5923 id
= get_identifier ("__gcov_execv");
5926 case BUILT_IN_EXECLP
:
5927 id
= get_identifier ("__gcov_execlp");
5930 case BUILT_IN_EXECLE
:
5931 id
= get_identifier ("__gcov_execle");
5934 case BUILT_IN_EXECVP
:
5935 id
= get_identifier ("__gcov_execvp");
5938 case BUILT_IN_EXECVE
:
5939 id
= get_identifier ("__gcov_execve");
5946 decl
= build_decl (DECL_SOURCE_LOCATION (fn
),
5947 FUNCTION_DECL
, id
, TREE_TYPE (fn
));
5948 DECL_EXTERNAL (decl
) = 1;
5949 TREE_PUBLIC (decl
) = 1;
5950 DECL_ARTIFICIAL (decl
) = 1;
5951 TREE_NOTHROW (decl
) = 1;
5952 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
5953 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
5954 call
= rewrite_call_expr (EXPR_LOCATION (exp
), exp
, 0, decl
, 0);
5955 return expand_call (call
, target
, ignore
);
5960 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5961 the pointer in these functions is void*, the tree optimizers may remove
5962 casts. The mode computed in expand_builtin isn't reliable either, due
5963 to __sync_bool_compare_and_swap.
5965 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5966 group of builtins. This gives us log2 of the mode size. */
5968 static inline machine_mode
5969 get_builtin_sync_mode (int fcode_diff
)
5971 /* The size is not negotiable, so ask not to get BLKmode in return
5972 if the target indicates that a smaller size would be better. */
5973 return int_mode_for_size (BITS_PER_UNIT
<< fcode_diff
, 0).require ();
5976 /* Expand the memory expression LOC and return the appropriate memory operand
5977 for the builtin_sync operations. */
5980 get_builtin_sync_mem (tree loc
, machine_mode mode
)
5983 int addr_space
= TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc
))
5984 ? TREE_TYPE (TREE_TYPE (loc
))
5986 scalar_int_mode addr_mode
= targetm
.addr_space
.address_mode (addr_space
);
5988 addr
= expand_expr (loc
, NULL_RTX
, addr_mode
, EXPAND_SUM
);
5989 addr
= convert_memory_address (addr_mode
, addr
);
5991 /* Note that we explicitly do not want any alias information for this
5992 memory, so that we kill all other live memories. Otherwise we don't
5993 satisfy the full barrier semantics of the intrinsic. */
5994 mem
= gen_rtx_MEM (mode
, addr
);
5996 set_mem_addr_space (mem
, addr_space
);
5998 mem
= validize_mem (mem
);
6000 /* The alignment needs to be at least according to that of the mode. */
6001 set_mem_align (mem
, MAX (GET_MODE_ALIGNMENT (mode
),
6002 get_pointer_alignment (loc
)));
6003 set_mem_alias_set (mem
, ALIAS_SET_MEMORY_BARRIER
);
6004 MEM_VOLATILE_P (mem
) = 1;
6009 /* Make sure an argument is in the right mode.
6010 EXP is the tree argument.
6011 MODE is the mode it should be in. */
6014 expand_expr_force_mode (tree exp
, machine_mode mode
)
6017 machine_mode old_mode
;
6019 if (TREE_CODE (exp
) == SSA_NAME
6020 && TYPE_MODE (TREE_TYPE (exp
)) != mode
)
6022 /* Undo argument promotion if possible, as combine might not
6023 be able to do it later due to MEM_VOLATILE_P uses in the
6025 gimple
*g
= get_gimple_for_ssa_name (exp
);
6026 if (g
&& gimple_assign_cast_p (g
))
6028 tree rhs
= gimple_assign_rhs1 (g
);
6029 tree_code code
= gimple_assign_rhs_code (g
);
6030 if (CONVERT_EXPR_CODE_P (code
)
6031 && TYPE_MODE (TREE_TYPE (rhs
)) == mode
6032 && INTEGRAL_TYPE_P (TREE_TYPE (exp
))
6033 && INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
6034 && (TYPE_PRECISION (TREE_TYPE (exp
))
6035 > TYPE_PRECISION (TREE_TYPE (rhs
))))
6040 val
= expand_expr (exp
, NULL_RTX
, mode
, EXPAND_NORMAL
);
6041 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6042 of CONST_INTs, where we know the old_mode only from the call argument. */
6044 old_mode
= GET_MODE (val
);
6045 if (old_mode
== VOIDmode
)
6046 old_mode
= TYPE_MODE (TREE_TYPE (exp
));
6047 val
= convert_modes (mode
, old_mode
, val
, 1);
6052 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6053 EXP is the CALL_EXPR. CODE is the rtx code
6054 that corresponds to the arithmetic or logical operation from the name;
6055 an exception here is that NOT actually means NAND. TARGET is an optional
6056 place for us to store the results; AFTER is true if this is the
6057 fetch_and_xxx form. */
6060 expand_builtin_sync_operation (machine_mode mode
, tree exp
,
6061 enum rtx_code code
, bool after
,
6065 location_t loc
= EXPR_LOCATION (exp
);
6067 if (code
== NOT
&& warn_sync_nand
)
6069 tree fndecl
= get_callee_fndecl (exp
);
6070 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
6072 static bool warned_f_a_n
, warned_n_a_f
;
6076 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
6077 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
6078 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
6079 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
6080 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
6084 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N
);
6085 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
6086 warned_f_a_n
= true;
6089 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
6090 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
6091 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
6092 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
6093 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
6097 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N
);
6098 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
6099 warned_n_a_f
= true;
6107 /* Expand the operands. */
6108 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6109 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
6111 return expand_atomic_fetch_op (target
, mem
, val
, code
, MEMMODEL_SYNC_SEQ_CST
,
6115 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6116 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6117 true if this is the boolean form. TARGET is a place for us to store the
6118 results; this is NOT optional if IS_BOOL is true. */
6121 expand_builtin_compare_and_swap (machine_mode mode
, tree exp
,
6122 bool is_bool
, rtx target
)
6124 rtx old_val
, new_val
, mem
;
6127 /* Expand the operands. */
6128 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6129 old_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
6130 new_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
6132 pbool
= poval
= NULL
;
6133 if (target
!= const0_rtx
)
6140 if (!expand_atomic_compare_and_swap (pbool
, poval
, mem
, old_val
, new_val
,
6141 false, MEMMODEL_SYNC_SEQ_CST
,
6142 MEMMODEL_SYNC_SEQ_CST
))
6148 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6149 general form is actually an atomic exchange, and some targets only
6150 support a reduced form with the second argument being a constant 1.
6151 EXP is the CALL_EXPR; TARGET is an optional place for us to store
6155 expand_builtin_sync_lock_test_and_set (machine_mode mode
, tree exp
,
6160 /* Expand the operands. */
6161 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6162 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
6164 return expand_sync_lock_test_and_set (target
, mem
, val
);
6167 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6170 expand_builtin_sync_lock_release (machine_mode mode
, tree exp
)
6174 /* Expand the operands. */
6175 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6177 expand_atomic_store (mem
, const0_rtx
, MEMMODEL_SYNC_RELEASE
, true);
6180 /* Given an integer representing an ``enum memmodel'', verify its
6181 correctness and return the memory model enum. */
6183 static enum memmodel
6184 get_memmodel (tree exp
)
6186 /* If the parameter is not a constant, it's a run time value so we'll just
6187 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
6188 if (TREE_CODE (exp
) != INTEGER_CST
)
6189 return MEMMODEL_SEQ_CST
;
6191 rtx op
= expand_normal (exp
);
6193 unsigned HOST_WIDE_INT val
= INTVAL (op
);
6194 if (targetm
.memmodel_check
)
6195 val
= targetm
.memmodel_check (val
);
6196 else if (val
& ~MEMMODEL_MASK
)
6197 return MEMMODEL_SEQ_CST
;
6199 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
6200 if (memmodel_base (val
) >= MEMMODEL_LAST
)
6201 return MEMMODEL_SEQ_CST
;
6203 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
6204 be conservative and promote consume to acquire. */
6205 if (val
== MEMMODEL_CONSUME
)
6206 val
= MEMMODEL_ACQUIRE
;
6208 return (enum memmodel
) val
;
6211 /* Expand the __atomic_exchange intrinsic:
6212 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
6213 EXP is the CALL_EXPR.
6214 TARGET is an optional place for us to store the results. */
6217 expand_builtin_atomic_exchange (machine_mode mode
, tree exp
, rtx target
)
6220 enum memmodel model
;
6222 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
6224 if (!flag_inline_atomics
)
6227 /* Expand the operands. */
6228 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6229 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
6231 return expand_atomic_exchange (target
, mem
, val
, model
);
6234 /* Expand the __atomic_compare_exchange intrinsic:
6235 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
6236 TYPE desired, BOOL weak,
6237 enum memmodel success,
6238 enum memmodel failure)
6239 EXP is the CALL_EXPR.
6240 TARGET is an optional place for us to store the results. */
6243 expand_builtin_atomic_compare_exchange (machine_mode mode
, tree exp
,
6246 rtx expect
, desired
, mem
, oldval
;
6247 rtx_code_label
*label
;
6251 memmodel success
= get_memmodel (CALL_EXPR_ARG (exp
, 4));
6252 memmodel failure
= get_memmodel (CALL_EXPR_ARG (exp
, 5));
6254 if (failure
> success
)
6255 success
= MEMMODEL_SEQ_CST
;
6257 if (is_mm_release (failure
) || is_mm_acq_rel (failure
))
6259 failure
= MEMMODEL_SEQ_CST
;
6260 success
= MEMMODEL_SEQ_CST
;
6264 if (!flag_inline_atomics
)
6267 /* Expand the operands. */
6268 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6270 expect
= expand_normal (CALL_EXPR_ARG (exp
, 1));
6271 expect
= convert_memory_address (Pmode
, expect
);
6272 expect
= gen_rtx_MEM (mode
, expect
);
6273 desired
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
6275 weak
= CALL_EXPR_ARG (exp
, 3);
6277 if (tree_fits_shwi_p (weak
) && tree_to_shwi (weak
) != 0)
6280 if (target
== const0_rtx
)
6283 /* Lest the rtl backend create a race condition with an imporoper store
6284 to memory, always create a new pseudo for OLDVAL. */
6287 if (!expand_atomic_compare_and_swap (&target
, &oldval
, mem
, expect
, desired
,
6288 is_weak
, success
, failure
))
6291 /* Conditionally store back to EXPECT, lest we create a race condition
6292 with an improper store to memory. */
6293 /* ??? With a rearrangement of atomics at the gimple level, we can handle
6294 the normal case where EXPECT is totally private, i.e. a register. At
6295 which point the store can be unconditional. */
6296 label
= gen_label_rtx ();
6297 emit_cmp_and_jump_insns (target
, const0_rtx
, NE
, NULL
,
6298 GET_MODE (target
), 1, label
);
6299 emit_move_insn (expect
, oldval
);
6305 /* Helper function for expand_ifn_atomic_compare_exchange - expand
6306 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
6307 call. The weak parameter must be dropped to match the expected parameter
6308 list and the expected argument changed from value to pointer to memory
6312 expand_ifn_atomic_compare_exchange_into_call (gcall
*call
, machine_mode mode
)
6315 vec
<tree
, va_gc
> *vec
;
6318 vec
->quick_push (gimple_call_arg (call
, 0));
6319 tree expected
= gimple_call_arg (call
, 1);
6320 rtx x
= assign_stack_temp_for_type (mode
, GET_MODE_SIZE (mode
),
6321 TREE_TYPE (expected
));
6322 rtx expd
= expand_expr (expected
, x
, mode
, EXPAND_NORMAL
);
6324 emit_move_insn (x
, expd
);
6325 tree v
= make_tree (TREE_TYPE (expected
), x
);
6326 vec
->quick_push (build1 (ADDR_EXPR
,
6327 build_pointer_type (TREE_TYPE (expected
)), v
));
6328 vec
->quick_push (gimple_call_arg (call
, 2));
6329 /* Skip the boolean weak parameter. */
6330 for (z
= 4; z
< 6; z
++)
6331 vec
->quick_push (gimple_call_arg (call
, z
));
6332 /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6333 unsigned int bytes_log2
= exact_log2 (GET_MODE_SIZE (mode
).to_constant ());
6334 gcc_assert (bytes_log2
< 5);
6335 built_in_function fncode
6336 = (built_in_function
) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6338 tree fndecl
= builtin_decl_explicit (fncode
);
6339 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fndecl
)),
6341 tree exp
= build_call_vec (boolean_type_node
, fn
, vec
);
6342 tree lhs
= gimple_call_lhs (call
);
6343 rtx boolret
= expand_call (exp
, NULL_RTX
, lhs
== NULL_TREE
);
6346 rtx target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
6347 if (GET_MODE (boolret
) != mode
)
6348 boolret
= convert_modes (mode
, GET_MODE (boolret
), boolret
, 1);
6349 x
= force_reg (mode
, x
);
6350 write_complex_part (target
, boolret
, true, true);
6351 write_complex_part (target
, x
, false, false);
6355 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6358 expand_ifn_atomic_compare_exchange (gcall
*call
)
6360 int size
= tree_to_shwi (gimple_call_arg (call
, 3)) & 255;
6361 gcc_assert (size
== 1 || size
== 2 || size
== 4 || size
== 8 || size
== 16);
6362 machine_mode mode
= int_mode_for_size (BITS_PER_UNIT
* size
, 0).require ();
6364 memmodel success
= get_memmodel (gimple_call_arg (call
, 4));
6365 memmodel failure
= get_memmodel (gimple_call_arg (call
, 5));
6367 if (failure
> success
)
6368 success
= MEMMODEL_SEQ_CST
;
6370 if (is_mm_release (failure
) || is_mm_acq_rel (failure
))
6372 failure
= MEMMODEL_SEQ_CST
;
6373 success
= MEMMODEL_SEQ_CST
;
6376 if (!flag_inline_atomics
)
6378 expand_ifn_atomic_compare_exchange_into_call (call
, mode
);
6382 /* Expand the operands. */
6383 rtx mem
= get_builtin_sync_mem (gimple_call_arg (call
, 0), mode
);
6385 rtx expect
= expand_expr_force_mode (gimple_call_arg (call
, 1), mode
);
6386 rtx desired
= expand_expr_force_mode (gimple_call_arg (call
, 2), mode
);
6388 bool is_weak
= (tree_to_shwi (gimple_call_arg (call
, 3)) & 256) != 0;
6393 if (!expand_atomic_compare_and_swap (&boolret
, &oldval
, mem
, expect
, desired
,
6394 is_weak
, success
, failure
))
6396 expand_ifn_atomic_compare_exchange_into_call (call
, mode
);
6400 tree lhs
= gimple_call_lhs (call
);
6403 rtx target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
6404 if (GET_MODE (boolret
) != mode
)
6405 boolret
= convert_modes (mode
, GET_MODE (boolret
), boolret
, 1);
6406 write_complex_part (target
, boolret
, true, true);
6407 write_complex_part (target
, oldval
, false, false);
6411 /* Expand the __atomic_load intrinsic:
6412 TYPE __atomic_load (TYPE *object, enum memmodel)
6413 EXP is the CALL_EXPR.
6414 TARGET is an optional place for us to store the results. */
6417 expand_builtin_atomic_load (machine_mode mode
, tree exp
, rtx target
)
6419 memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
6420 if (is_mm_release (model
) || is_mm_acq_rel (model
))
6421 model
= MEMMODEL_SEQ_CST
;
6423 if (!flag_inline_atomics
)
6426 /* Expand the operand. */
6427 rtx mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6429 return expand_atomic_load (target
, mem
, model
);
6433 /* Expand the __atomic_store intrinsic:
6434 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6435 EXP is the CALL_EXPR.
6436 TARGET is an optional place for us to store the results. */
6439 expand_builtin_atomic_store (machine_mode mode
, tree exp
)
6441 memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
6442 if (!(is_mm_relaxed (model
) || is_mm_seq_cst (model
)
6443 || is_mm_release (model
)))
6444 model
= MEMMODEL_SEQ_CST
;
6446 if (!flag_inline_atomics
)
6449 /* Expand the operands. */
6450 rtx mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6451 rtx val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
6453 return expand_atomic_store (mem
, val
, model
, false);
6456 /* Expand the __atomic_fetch_XXX intrinsic:
6457 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6458 EXP is the CALL_EXPR.
6459 TARGET is an optional place for us to store the results.
6460 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6461 FETCH_AFTER is true if returning the result of the operation.
6462 FETCH_AFTER is false if returning the value before the operation.
6463 IGNORE is true if the result is not used.
6464 EXT_CALL is the correct builtin for an external call if this cannot be
6465 resolved to an instruction sequence. */
6468 expand_builtin_atomic_fetch_op (machine_mode mode
, tree exp
, rtx target
,
6469 enum rtx_code code
, bool fetch_after
,
6470 bool ignore
, enum built_in_function ext_call
)
6473 enum memmodel model
;
6477 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
6479 /* Expand the operands. */
6480 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6481 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
6483 /* Only try generating instructions if inlining is turned on. */
6484 if (flag_inline_atomics
)
6486 ret
= expand_atomic_fetch_op (target
, mem
, val
, code
, model
, fetch_after
);
6491 /* Return if a different routine isn't needed for the library call. */
6492 if (ext_call
== BUILT_IN_NONE
)
6495 /* Change the call to the specified function. */
6496 fndecl
= get_callee_fndecl (exp
);
6497 addr
= CALL_EXPR_FN (exp
);
6500 gcc_assert (TREE_OPERAND (addr
, 0) == fndecl
);
6501 TREE_OPERAND (addr
, 0) = builtin_decl_explicit (ext_call
);
6503 /* If we will emit code after the call, the call cannot be a tail call.
6504 If it is emitted as a tail call, a barrier is emitted after it, and
6505 then all trailing code is removed. */
6507 CALL_EXPR_TAILCALL (exp
) = 0;
6509 /* Expand the call here so we can emit trailing code. */
6510 ret
= expand_call (exp
, target
, ignore
);
6512 /* Replace the original function just in case it matters. */
6513 TREE_OPERAND (addr
, 0) = fndecl
;
6515 /* Then issue the arithmetic correction to return the right result. */
6520 ret
= expand_simple_binop (mode
, AND
, ret
, val
, NULL_RTX
, true,
6522 ret
= expand_simple_unop (mode
, NOT
, ret
, target
, true);
6525 ret
= expand_simple_binop (mode
, code
, ret
, val
, target
, true,
6531 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6534 expand_ifn_atomic_bit_test_and (gcall
*call
)
6536 tree ptr
= gimple_call_arg (call
, 0);
6537 tree bit
= gimple_call_arg (call
, 1);
6538 tree flag
= gimple_call_arg (call
, 2);
6539 tree lhs
= gimple_call_lhs (call
);
6540 enum memmodel model
= MEMMODEL_SYNC_SEQ_CST
;
6541 machine_mode mode
= TYPE_MODE (TREE_TYPE (flag
));
6544 class expand_operand ops
[5];
6546 gcc_assert (flag_inline_atomics
);
6548 if (gimple_call_num_args (call
) == 5)
6549 model
= get_memmodel (gimple_call_arg (call
, 3));
6551 rtx mem
= get_builtin_sync_mem (ptr
, mode
);
6552 rtx val
= expand_expr_force_mode (bit
, mode
);
6554 switch (gimple_call_internal_fn (call
))
6556 case IFN_ATOMIC_BIT_TEST_AND_SET
:
6558 optab
= atomic_bit_test_and_set_optab
;
6560 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT
:
6562 optab
= atomic_bit_test_and_complement_optab
;
6564 case IFN_ATOMIC_BIT_TEST_AND_RESET
:
6566 optab
= atomic_bit_test_and_reset_optab
;
6572 if (lhs
== NULL_TREE
)
6574 rtx val2
= expand_simple_binop (mode
, ASHIFT
, const1_rtx
,
6575 val
, NULL_RTX
, true, OPTAB_DIRECT
);
6577 val2
= expand_simple_unop (mode
, NOT
, val2
, NULL_RTX
, true);
6578 if (expand_atomic_fetch_op (const0_rtx
, mem
, val2
, code
, model
, false))
6584 target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
6586 target
= gen_reg_rtx (mode
);
6587 enum insn_code icode
= direct_optab_handler (optab
, mode
);
6588 gcc_assert (icode
!= CODE_FOR_nothing
);
6589 create_output_operand (&ops
[0], target
, mode
);
6590 create_fixed_operand (&ops
[1], mem
);
6591 create_convert_operand_to (&ops
[2], val
, mode
, true);
6592 create_integer_operand (&ops
[3], model
);
6593 create_integer_operand (&ops
[4], integer_onep (flag
));
6594 if (maybe_expand_insn (icode
, 5, ops
))
6598 val
= expand_simple_binop (mode
, ASHIFT
, const1_rtx
,
6599 val
, NULL_RTX
, true, OPTAB_DIRECT
);
6602 val
= expand_simple_unop (mode
, NOT
, val
, NULL_RTX
, true);
6603 rtx result
= expand_atomic_fetch_op (gen_reg_rtx (mode
), mem
, val
,
6604 code
, model
, false);
6607 bool is_atomic
= gimple_call_num_args (call
) == 5;
6608 tree tcall
= gimple_call_arg (call
, 3 + is_atomic
);
6609 tree fndecl
= gimple_call_addr_fndecl (tcall
);
6610 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
6611 tree exp
= build_call_nary (type
, tcall
, 2 + is_atomic
, ptr
,
6612 make_tree (type
, val
),
6614 ? gimple_call_arg (call
, 3)
6615 : integer_zero_node
);
6616 result
= expand_builtin (exp
, gen_reg_rtx (mode
), NULL_RTX
,
6621 if (integer_onep (flag
))
6623 result
= expand_simple_binop (mode
, ASHIFTRT
, result
, bitval
,
6624 NULL_RTX
, true, OPTAB_DIRECT
);
6625 result
= expand_simple_binop (mode
, AND
, result
, const1_rtx
, target
,
6626 true, OPTAB_DIRECT
);
6629 result
= expand_simple_binop (mode
, AND
, result
, maskval
, target
, true,
6631 if (result
!= target
)
6632 emit_move_insn (target
, result
);
6635 /* Expand IFN_ATOMIC_*_FETCH_CMP_0 internal function. */
6638 expand_ifn_atomic_op_fetch_cmp_0 (gcall
*call
)
6640 tree cmp
= gimple_call_arg (call
, 0);
6641 tree ptr
= gimple_call_arg (call
, 1);
6642 tree arg
= gimple_call_arg (call
, 2);
6643 tree lhs
= gimple_call_lhs (call
);
6644 enum memmodel model
= MEMMODEL_SYNC_SEQ_CST
;
6645 machine_mode mode
= TYPE_MODE (TREE_TYPE (cmp
));
6648 class expand_operand ops
[5];
6650 gcc_assert (flag_inline_atomics
);
6652 if (gimple_call_num_args (call
) == 5)
6653 model
= get_memmodel (gimple_call_arg (call
, 3));
6655 rtx mem
= get_builtin_sync_mem (ptr
, mode
);
6656 rtx op
= expand_expr_force_mode (arg
, mode
);
6658 switch (gimple_call_internal_fn (call
))
6660 case IFN_ATOMIC_ADD_FETCH_CMP_0
:
6662 optab
= atomic_add_fetch_cmp_0_optab
;
6664 case IFN_ATOMIC_SUB_FETCH_CMP_0
:
6666 optab
= atomic_sub_fetch_cmp_0_optab
;
6668 case IFN_ATOMIC_AND_FETCH_CMP_0
:
6670 optab
= atomic_and_fetch_cmp_0_optab
;
6672 case IFN_ATOMIC_OR_FETCH_CMP_0
:
6674 optab
= atomic_or_fetch_cmp_0_optab
;
6676 case IFN_ATOMIC_XOR_FETCH_CMP_0
:
6678 optab
= atomic_xor_fetch_cmp_0_optab
;
6684 enum rtx_code comp
= UNKNOWN
;
6685 switch (tree_to_uhwi (cmp
))
6687 case ATOMIC_OP_FETCH_CMP_0_EQ
: comp
= EQ
; break;
6688 case ATOMIC_OP_FETCH_CMP_0_NE
: comp
= NE
; break;
6689 case ATOMIC_OP_FETCH_CMP_0_GT
: comp
= GT
; break;
6690 case ATOMIC_OP_FETCH_CMP_0_GE
: comp
= GE
; break;
6691 case ATOMIC_OP_FETCH_CMP_0_LT
: comp
= LT
; break;
6692 case ATOMIC_OP_FETCH_CMP_0_LE
: comp
= LE
; break;
6693 default: gcc_unreachable ();
6697 if (lhs
== NULL_TREE
)
6698 target
= gen_reg_rtx (TYPE_MODE (boolean_type_node
));
6700 target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
6701 enum insn_code icode
= direct_optab_handler (optab
, mode
);
6702 gcc_assert (icode
!= CODE_FOR_nothing
);
6703 create_output_operand (&ops
[0], target
, TYPE_MODE (boolean_type_node
));
6704 create_fixed_operand (&ops
[1], mem
);
6705 create_convert_operand_to (&ops
[2], op
, mode
, true);
6706 create_integer_operand (&ops
[3], model
);
6707 create_integer_operand (&ops
[4], comp
);
6708 if (maybe_expand_insn (icode
, 5, ops
))
6711 rtx result
= expand_atomic_fetch_op (gen_reg_rtx (mode
), mem
, op
,
6715 bool is_atomic
= gimple_call_num_args (call
) == 5;
6716 tree tcall
= gimple_call_arg (call
, 3 + is_atomic
);
6717 tree fndecl
= gimple_call_addr_fndecl (tcall
);
6718 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
6719 tree exp
= build_call_nary (type
, tcall
,
6720 2 + is_atomic
, ptr
, arg
,
6722 ? gimple_call_arg (call
, 3)
6723 : integer_zero_node
);
6724 result
= expand_builtin (exp
, gen_reg_rtx (mode
), NULL_RTX
,
6730 result
= emit_store_flag_force (target
, comp
, result
, const0_rtx
, mode
,
6732 if (result
!= target
)
6733 emit_move_insn (target
, result
);
6737 /* Expand an atomic clear operation.
6738 void _atomic_clear (BOOL *obj, enum memmodel)
6739 EXP is the call expression. */
6742 expand_builtin_atomic_clear (tree exp
)
6744 machine_mode mode
= int_mode_for_size (BOOL_TYPE_SIZE
, 0).require ();
6745 rtx mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6746 memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
6748 if (is_mm_consume (model
) || is_mm_acquire (model
) || is_mm_acq_rel (model
))
6749 model
= MEMMODEL_SEQ_CST
;
6751 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6752 Failing that, a store is issued by __atomic_store. The only way this can
6753 fail is if the bool type is larger than a word size. Unlikely, but
6754 handle it anyway for completeness. Assume a single threaded model since
6755 there is no atomic support in this case, and no barriers are required. */
6756 rtx ret
= expand_atomic_store (mem
, const0_rtx
, model
, true);
6758 emit_move_insn (mem
, const0_rtx
);
6762 /* Expand an atomic test_and_set operation.
6763 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6764 EXP is the call expression. */
6767 expand_builtin_atomic_test_and_set (tree exp
, rtx target
)
6770 enum memmodel model
;
6773 mode
= int_mode_for_size (BOOL_TYPE_SIZE
, 0).require ();
6774 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
6775 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
6777 return expand_atomic_test_and_set (target
, mem
, model
);
6781 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6782 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6785 fold_builtin_atomic_always_lock_free (tree arg0
, tree arg1
)
6789 unsigned int mode_align
, type_align
;
6791 if (TREE_CODE (arg0
) != INTEGER_CST
)
6794 /* We need a corresponding integer mode for the access to be lock-free. */
6795 size
= INTVAL (expand_normal (arg0
)) * BITS_PER_UNIT
;
6796 if (!int_mode_for_size (size
, 0).exists (&mode
))
6797 return boolean_false_node
;
6799 mode_align
= GET_MODE_ALIGNMENT (mode
);
6801 if (TREE_CODE (arg1
) == INTEGER_CST
)
6803 unsigned HOST_WIDE_INT val
= UINTVAL (expand_normal (arg1
));
6805 /* Either this argument is null, or it's a fake pointer encoding
6806 the alignment of the object. */
6807 val
= least_bit_hwi (val
);
6808 val
*= BITS_PER_UNIT
;
6810 if (val
== 0 || mode_align
< val
)
6811 type_align
= mode_align
;
6817 tree ttype
= TREE_TYPE (arg1
);
6819 /* This function is usually invoked and folded immediately by the front
6820 end before anything else has a chance to look at it. The pointer
6821 parameter at this point is usually cast to a void *, so check for that
6822 and look past the cast. */
6823 if (CONVERT_EXPR_P (arg1
)
6824 && POINTER_TYPE_P (ttype
)
6825 && VOID_TYPE_P (TREE_TYPE (ttype
))
6826 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
6827 arg1
= TREE_OPERAND (arg1
, 0);
6829 ttype
= TREE_TYPE (arg1
);
6830 gcc_assert (POINTER_TYPE_P (ttype
));
6832 /* Get the underlying type of the object. */
6833 ttype
= TREE_TYPE (ttype
);
6834 type_align
= TYPE_ALIGN (ttype
);
6837 /* If the object has smaller alignment, the lock free routines cannot
6839 if (type_align
< mode_align
)
6840 return boolean_false_node
;
6842 /* Check if a compare_and_swap pattern exists for the mode which represents
6843 the required size. The pattern is not allowed to fail, so the existence
6844 of the pattern indicates support is present. Also require that an
6845 atomic load exists for the required size. */
6846 if (can_compare_and_swap_p (mode
, true) && can_atomic_load_p (mode
))
6847 return boolean_true_node
;
6849 return boolean_false_node
;
6852 /* Return true if the parameters to call EXP represent an object which will
6853 always generate lock free instructions. The first argument represents the
6854 size of the object, and the second parameter is a pointer to the object
6855 itself. If NULL is passed for the object, then the result is based on
6856 typical alignment for an object of the specified size. Otherwise return
6860 expand_builtin_atomic_always_lock_free (tree exp
)
6863 tree arg0
= CALL_EXPR_ARG (exp
, 0);
6864 tree arg1
= CALL_EXPR_ARG (exp
, 1);
6866 if (TREE_CODE (arg0
) != INTEGER_CST
)
6868 error ("non-constant argument 1 to %qs", "__atomic_always_lock_free");
6872 size
= fold_builtin_atomic_always_lock_free (arg0
, arg1
);
6873 if (size
== boolean_true_node
)
6878 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6879 is lock free on this architecture. */
6882 fold_builtin_atomic_is_lock_free (tree arg0
, tree arg1
)
6884 if (!flag_inline_atomics
)
6887 /* If it isn't always lock free, don't generate a result. */
6888 if (fold_builtin_atomic_always_lock_free (arg0
, arg1
) == boolean_true_node
)
6889 return boolean_true_node
;
6894 /* Return true if the parameters to call EXP represent an object which will
6895 always generate lock free instructions. The first argument represents the
6896 size of the object, and the second parameter is a pointer to the object
6897 itself. If NULL is passed for the object, then the result is based on
6898 typical alignment for an object of the specified size. Otherwise return
6902 expand_builtin_atomic_is_lock_free (tree exp
)
6905 tree arg0
= CALL_EXPR_ARG (exp
, 0);
6906 tree arg1
= CALL_EXPR_ARG (exp
, 1);
6908 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
6910 error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
6914 if (!flag_inline_atomics
)
6917 /* If the value is known at compile time, return the RTX for it. */
6918 size
= fold_builtin_atomic_is_lock_free (arg0
, arg1
);
6919 if (size
== boolean_true_node
)
6925 /* Expand the __atomic_thread_fence intrinsic:
6926 void __atomic_thread_fence (enum memmodel)
6927 EXP is the CALL_EXPR. */
6930 expand_builtin_atomic_thread_fence (tree exp
)
6932 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
6933 expand_mem_thread_fence (model
);
6936 /* Expand the __atomic_signal_fence intrinsic:
6937 void __atomic_signal_fence (enum memmodel)
6938 EXP is the CALL_EXPR. */
6941 expand_builtin_atomic_signal_fence (tree exp
)
6943 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
6944 expand_mem_signal_fence (model
);
6947 /* Expand the __sync_synchronize intrinsic. */
6950 expand_builtin_sync_synchronize (void)
6952 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST
);
6956 expand_builtin_thread_pointer (tree exp
, rtx target
)
6958 enum insn_code icode
;
6959 if (!validate_arglist (exp
, VOID_TYPE
))
6961 icode
= direct_optab_handler (get_thread_pointer_optab
, Pmode
);
6962 if (icode
!= CODE_FOR_nothing
)
6964 class expand_operand op
;
6965 /* If the target is not sutitable then create a new target. */
6966 if (target
== NULL_RTX
6968 || GET_MODE (target
) != Pmode
)
6969 target
= gen_reg_rtx (Pmode
);
6970 create_output_operand (&op
, target
, Pmode
);
6971 expand_insn (icode
, 1, &op
);
6974 error ("%<__builtin_thread_pointer%> is not supported on this target");
6979 expand_builtin_set_thread_pointer (tree exp
)
6981 enum insn_code icode
;
6982 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6984 icode
= direct_optab_handler (set_thread_pointer_optab
, Pmode
);
6985 if (icode
!= CODE_FOR_nothing
)
6987 class expand_operand op
;
6988 rtx val
= expand_expr (CALL_EXPR_ARG (exp
, 0), NULL_RTX
,
6989 Pmode
, EXPAND_NORMAL
);
6990 create_input_operand (&op
, val
, Pmode
);
6991 expand_insn (icode
, 1, &op
);
6994 error ("%<__builtin_set_thread_pointer%> is not supported on this target");
6998 /* Emit code to restore the current value of stack. */
7001 expand_stack_restore (tree var
)
7004 rtx sa
= expand_normal (var
);
7006 sa
= convert_memory_address (Pmode
, sa
);
7008 prev
= get_last_insn ();
7009 emit_stack_restore (SAVE_BLOCK
, sa
);
7011 record_new_stack_level ();
7013 fixup_args_size_notes (prev
, get_last_insn (), 0);
7016 /* Emit code to save the current value of stack. */
7019 expand_stack_save (void)
7023 emit_stack_save (SAVE_BLOCK
, &ret
);
7027 /* Emit code to get the openacc gang, worker or vector id or size. */
7030 expand_builtin_goacc_parlevel_id_size (tree exp
, rtx target
, int ignore
)
7033 rtx fallback_retval
;
7034 rtx_insn
*(*gen_fn
) (rtx
, rtx
);
7035 switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp
)))
7037 case BUILT_IN_GOACC_PARLEVEL_ID
:
7038 name
= "__builtin_goacc_parlevel_id";
7039 fallback_retval
= const0_rtx
;
7040 gen_fn
= targetm
.gen_oacc_dim_pos
;
7042 case BUILT_IN_GOACC_PARLEVEL_SIZE
:
7043 name
= "__builtin_goacc_parlevel_size";
7044 fallback_retval
= const1_rtx
;
7045 gen_fn
= targetm
.gen_oacc_dim_size
;
7051 if (oacc_get_fn_attrib (current_function_decl
) == NULL_TREE
)
7053 error ("%qs only supported in OpenACC code", name
);
7057 tree arg
= CALL_EXPR_ARG (exp
, 0);
7058 if (TREE_CODE (arg
) != INTEGER_CST
)
7060 error ("non-constant argument 0 to %qs", name
);
7064 int dim
= TREE_INT_CST_LOW (arg
);
7068 case GOMP_DIM_WORKER
:
7069 case GOMP_DIM_VECTOR
:
7072 error ("illegal argument 0 to %qs", name
);
7079 if (target
== NULL_RTX
)
7080 target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
7082 if (!targetm
.have_oacc_dim_size ())
7084 emit_move_insn (target
, fallback_retval
);
7088 rtx reg
= MEM_P (target
) ? gen_reg_rtx (GET_MODE (target
)) : target
;
7089 emit_insn (gen_fn (reg
, GEN_INT (dim
)));
7091 emit_move_insn (target
, reg
);
7096 /* Expand a string compare operation using a sequence of char comparison
7097 to get rid of the calling overhead, with result going to TARGET if
7100 VAR_STR is the variable string source;
7101 CONST_STR is the constant string source;
7102 LENGTH is the number of chars to compare;
7103 CONST_STR_N indicates which source string is the constant string;
7104 IS_MEMCMP indicates whether it's a memcmp or strcmp.
7106 to: (assume const_str_n is 2, i.e., arg2 is a constant string)
7108 target = (int) (unsigned char) var_str[0]
7109 - (int) (unsigned char) const_str[0];
7113 target = (int) (unsigned char) var_str[length - 2]
7114 - (int) (unsigned char) const_str[length - 2];
7117 target = (int) (unsigned char) var_str[length - 1]
7118 - (int) (unsigned char) const_str[length - 1];
7123 inline_string_cmp (rtx target
, tree var_str
, const char *const_str
,
7124 unsigned HOST_WIDE_INT length
,
7125 int const_str_n
, machine_mode mode
)
7127 HOST_WIDE_INT offset
= 0;
7129 = get_memory_rtx (var_str
, build_int_cst (unsigned_type_node
,length
));
7130 rtx var_rtx
= NULL_RTX
;
7131 rtx const_rtx
= NULL_RTX
;
7132 rtx result
= target
? target
: gen_reg_rtx (mode
);
7133 rtx_code_label
*ne_label
= gen_label_rtx ();
7134 tree unit_type_node
= unsigned_char_type_node
;
7135 scalar_int_mode unit_mode
7136 = as_a
<scalar_int_mode
> TYPE_MODE (unit_type_node
);
7140 for (unsigned HOST_WIDE_INT i
= 0; i
< length
; i
++)
7143 = adjust_address (var_rtx_array
, TYPE_MODE (unit_type_node
), offset
);
7144 const_rtx
= c_readstr (const_str
+ offset
, unit_mode
);
7145 rtx op0
= (const_str_n
== 1) ? const_rtx
: var_rtx
;
7146 rtx op1
= (const_str_n
== 1) ? var_rtx
: const_rtx
;
7148 op0
= convert_modes (mode
, unit_mode
, op0
, 1);
7149 op1
= convert_modes (mode
, unit_mode
, op1
, 1);
7150 rtx diff
= expand_simple_binop (mode
, MINUS
, op0
, op1
,
7151 result
, 1, OPTAB_WIDEN
);
7153 /* Force the difference into result register. We cannot reassign
7154 result here ("result = diff") or we may end up returning
7155 uninitialized result when expand_simple_binop allocates a new
7156 pseudo-register for returning. */
7158 emit_move_insn (result
, diff
);
7161 emit_cmp_and_jump_insns (result
, CONST0_RTX (mode
), NE
, NULL_RTX
,
7162 mode
, true, ne_label
);
7163 offset
+= GET_MODE_SIZE (unit_mode
);
7166 emit_label (ne_label
);
7167 rtx_insn
*insns
= get_insns ();
7174 /* Inline expansion of a call to str(n)cmp and memcmp, with result going
7175 to TARGET if that's convenient.
7176 If the call is not been inlined, return NULL_RTX. */
7179 inline_expand_builtin_bytecmp (tree exp
, rtx target
)
7181 tree fndecl
= get_callee_fndecl (exp
);
7182 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7183 bool is_ncmp
= (fcode
== BUILT_IN_STRNCMP
|| fcode
== BUILT_IN_MEMCMP
);
7185 /* Do NOT apply this inlining expansion when optimizing for size or
7186 optimization level below 2 or if unused *cmp hasn't been DCEd. */
7187 if (optimize
< 2 || optimize_insn_for_size_p () || target
== const0_rtx
)
7190 gcc_checking_assert (fcode
== BUILT_IN_STRCMP
7191 || fcode
== BUILT_IN_STRNCMP
7192 || fcode
== BUILT_IN_MEMCMP
);
7194 /* On a target where the type of the call (int) has same or narrower presicion
7195 than unsigned char, give up the inlining expansion. */
7196 if (TYPE_PRECISION (unsigned_char_type_node
)
7197 >= TYPE_PRECISION (TREE_TYPE (exp
)))
7200 tree arg1
= CALL_EXPR_ARG (exp
, 0);
7201 tree arg2
= CALL_EXPR_ARG (exp
, 1);
7202 tree len3_tree
= is_ncmp
? CALL_EXPR_ARG (exp
, 2) : NULL_TREE
;
7204 unsigned HOST_WIDE_INT len1
= 0;
7205 unsigned HOST_WIDE_INT len2
= 0;
7206 unsigned HOST_WIDE_INT len3
= 0;
7208 /* Get the object representation of the initializers of ARG1 and ARG2
7209 as strings, provided they refer to constant objects, with their byte
7210 sizes in LEN1 and LEN2, respectively. */
7211 const char *bytes1
= getbyterep (arg1
, &len1
);
7212 const char *bytes2
= getbyterep (arg2
, &len2
);
7214 /* Fail if neither argument refers to an initialized constant. */
7215 if (!bytes1
&& !bytes2
)
7220 /* Fail if the memcmp/strncmp bound is not a constant. */
7221 if (!tree_fits_uhwi_p (len3_tree
))
7224 len3
= tree_to_uhwi (len3_tree
);
7226 if (fcode
== BUILT_IN_MEMCMP
)
7228 /* Fail if the memcmp bound is greater than the size of either
7229 of the two constant objects. */
7230 if ((bytes1
&& len1
< len3
)
7231 || (bytes2
&& len2
< len3
))
7236 if (fcode
!= BUILT_IN_MEMCMP
)
7238 /* For string functions (i.e., strcmp and strncmp) reduce LEN1
7239 and LEN2 to the length of the nul-terminated string stored
7242 len1
= strnlen (bytes1
, len1
) + 1;
7244 len2
= strnlen (bytes2
, len2
) + 1;
7247 /* See inline_string_cmp. */
7253 else if (len2
> len1
)
7258 /* For strncmp only, compute the new bound as the smallest of
7259 the lengths of the two strings (plus 1) and the bound provided
7261 unsigned HOST_WIDE_INT bound
= (const_str_n
== 1) ? len1
: len2
;
7262 if (is_ncmp
&& len3
< bound
)
7265 /* If the bound of the comparison is larger than the threshold,
7267 if (bound
> (unsigned HOST_WIDE_INT
) param_builtin_string_cmp_inline_length
)
7270 machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
7272 /* Now, start inline expansion the call. */
7273 return inline_string_cmp (target
, (const_str_n
== 1) ? arg2
: arg1
,
7274 (const_str_n
== 1) ? bytes1
: bytes2
, bound
,
7278 /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
7279 represents the size of the first argument to that call, or VOIDmode
7280 if the argument is a pointer. IGNORE will be true if the result
7283 expand_speculation_safe_value (machine_mode mode
, tree exp
, rtx target
,
7287 unsigned nargs
= call_expr_nargs (exp
);
7289 tree arg0
= CALL_EXPR_ARG (exp
, 0);
7291 if (mode
== VOIDmode
)
7293 mode
= TYPE_MODE (TREE_TYPE (arg0
));
7294 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
7297 val
= expand_expr (arg0
, NULL_RTX
, mode
, EXPAND_NORMAL
);
7299 /* An optional second argument can be used as a failsafe value on
7300 some machines. If it isn't present, then the failsafe value is
7304 tree arg1
= CALL_EXPR_ARG (exp
, 1);
7305 failsafe
= expand_expr (arg1
, NULL_RTX
, mode
, EXPAND_NORMAL
);
7308 failsafe
= const0_rtx
;
7310 /* If the result isn't used, the behavior is undefined. It would be
7311 nice to emit a warning here, but path splitting means this might
7312 happen with legitimate code. So simply drop the builtin
7313 expansion in that case; we've handled any side-effects above. */
7317 /* If we don't have a suitable target, create one to hold the result. */
7318 if (target
== NULL
|| GET_MODE (target
) != mode
)
7319 target
= gen_reg_rtx (mode
);
7321 if (GET_MODE (val
) != mode
&& GET_MODE (val
) != VOIDmode
)
7322 val
= convert_modes (mode
, VOIDmode
, val
, false);
7324 return targetm
.speculation_safe_value (mode
, target
, val
, failsafe
);
7327 /* Expand an expression EXP that calls a built-in function,
7328 with result going to TARGET if that's convenient
7329 (and in mode MODE if that's convenient).
7330 SUBTARGET may be used as the target for computing one of EXP's operands.
7331 IGNORE is nonzero if the value is to be ignored. */
7334 expand_builtin (tree exp
, rtx target
, rtx subtarget
, machine_mode mode
,
7337 tree fndecl
= get_callee_fndecl (exp
);
7338 machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
7341 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
7342 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
7344 /* When ASan is enabled, we don't want to expand some memory/string
7345 builtins and rely on libsanitizer's hooks. This allows us to avoid
7346 redundant checks and be sure, that possible overflow will be detected
7349 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
7350 if (param_asan_kernel_mem_intrinsic_prefix
7351 && sanitize_flags_p (SANITIZE_KERNEL_ADDRESS
7352 | SANITIZE_KERNEL_HWADDRESS
))
7355 rtx save_decl_rtl
, ret
;
7356 case BUILT_IN_MEMCPY
:
7357 case BUILT_IN_MEMMOVE
:
7358 case BUILT_IN_MEMSET
:
7359 save_decl_rtl
= DECL_RTL (fndecl
);
7360 DECL_RTL (fndecl
) = asan_memfn_rtl (fndecl
);
7361 ret
= expand_call (exp
, target
, ignore
);
7362 DECL_RTL (fndecl
) = save_decl_rtl
;
7367 if (sanitize_flags_p (SANITIZE_ADDRESS
) && asan_intercepted_p (fcode
))
7368 return expand_call (exp
, target
, ignore
);
7370 /* When not optimizing, generate calls to library functions for a certain
7373 && !called_as_built_in (fndecl
)
7374 && fcode
!= BUILT_IN_FORK
7375 && fcode
!= BUILT_IN_EXECL
7376 && fcode
!= BUILT_IN_EXECV
7377 && fcode
!= BUILT_IN_EXECLP
7378 && fcode
!= BUILT_IN_EXECLE
7379 && fcode
!= BUILT_IN_EXECVP
7380 && fcode
!= BUILT_IN_EXECVE
7381 && fcode
!= BUILT_IN_CLEAR_CACHE
7382 && !ALLOCA_FUNCTION_CODE_P (fcode
)
7383 && fcode
!= BUILT_IN_FREE
)
7384 return expand_call (exp
, target
, ignore
);
7386 /* The built-in function expanders test for target == const0_rtx
7387 to determine whether the function's result will be ignored. */
7389 target
= const0_rtx
;
7391 /* If the result of a pure or const built-in function is ignored, and
7392 none of its arguments are volatile, we can avoid expanding the
7393 built-in call and just evaluate the arguments for side-effects. */
7394 if (target
== const0_rtx
7395 && ((flags
= flags_from_decl_or_type (fndecl
)) & (ECF_CONST
| ECF_PURE
))
7396 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
7398 bool volatilep
= false;
7400 call_expr_arg_iterator iter
;
7402 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
7403 if (TREE_THIS_VOLATILE (arg
))
7411 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
7412 expand_expr (arg
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
7419 CASE_FLT_FN (BUILT_IN_FABS
):
7420 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS
):
7421 case BUILT_IN_FABSD32
:
7422 case BUILT_IN_FABSD64
:
7423 case BUILT_IN_FABSD128
:
7424 target
= expand_builtin_fabs (exp
, target
, subtarget
);
7429 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
7430 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN
):
7431 target
= expand_builtin_copysign (exp
, target
, subtarget
);
7436 /* Just do a normal library call if we were unable to fold
7438 CASE_FLT_FN (BUILT_IN_CABS
):
7439 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CABS
):
7442 CASE_FLT_FN (BUILT_IN_FMA
):
7443 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA
):
7444 target
= expand_builtin_mathfn_ternary (exp
, target
, subtarget
);
7449 CASE_FLT_FN (BUILT_IN_ILOGB
):
7450 if (! flag_unsafe_math_optimizations
)
7453 CASE_FLT_FN (BUILT_IN_ISINF
):
7454 CASE_FLT_FN (BUILT_IN_FINITE
):
7455 case BUILT_IN_ISFINITE
:
7456 case BUILT_IN_ISNORMAL
:
7457 target
= expand_builtin_interclass_mathfn (exp
, target
);
7462 case BUILT_IN_ISSIGNALING
:
7463 target
= expand_builtin_issignaling (exp
, target
);
7468 CASE_FLT_FN (BUILT_IN_ICEIL
):
7469 CASE_FLT_FN (BUILT_IN_LCEIL
):
7470 CASE_FLT_FN (BUILT_IN_LLCEIL
):
7471 CASE_FLT_FN (BUILT_IN_LFLOOR
):
7472 CASE_FLT_FN (BUILT_IN_IFLOOR
):
7473 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
7474 target
= expand_builtin_int_roundingfn (exp
, target
);
7479 CASE_FLT_FN (BUILT_IN_IRINT
):
7480 CASE_FLT_FN (BUILT_IN_LRINT
):
7481 CASE_FLT_FN (BUILT_IN_LLRINT
):
7482 CASE_FLT_FN (BUILT_IN_IROUND
):
7483 CASE_FLT_FN (BUILT_IN_LROUND
):
7484 CASE_FLT_FN (BUILT_IN_LLROUND
):
7485 target
= expand_builtin_int_roundingfn_2 (exp
, target
);
7490 CASE_FLT_FN (BUILT_IN_POWI
):
7491 target
= expand_builtin_powi (exp
, target
);
7496 CASE_FLT_FN (BUILT_IN_CEXPI
):
7497 target
= expand_builtin_cexpi (exp
, target
);
7498 gcc_assert (target
);
7501 CASE_FLT_FN (BUILT_IN_SIN
):
7502 CASE_FLT_FN (BUILT_IN_COS
):
7503 if (! flag_unsafe_math_optimizations
)
7505 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
7510 CASE_FLT_FN (BUILT_IN_SINCOS
):
7511 if (! flag_unsafe_math_optimizations
)
7513 target
= expand_builtin_sincos (exp
);
7518 case BUILT_IN_FEGETROUND
:
7519 target
= expand_builtin_fegetround (exp
, target
, target_mode
);
7524 case BUILT_IN_FECLEAREXCEPT
:
7525 target
= expand_builtin_feclear_feraise_except (exp
, target
, target_mode
,
7526 feclearexcept_optab
);
7531 case BUILT_IN_FERAISEEXCEPT
:
7532 target
= expand_builtin_feclear_feraise_except (exp
, target
, target_mode
,
7533 feraiseexcept_optab
);
7538 case BUILT_IN_APPLY_ARGS
:
7539 return expand_builtin_apply_args ();
7541 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
7542 FUNCTION with a copy of the parameters described by
7543 ARGUMENTS, and ARGSIZE. It returns a block of memory
7544 allocated on the stack into which is stored all the registers
7545 that might possibly be used for returning the result of a
7546 function. ARGUMENTS is the value returned by
7547 __builtin_apply_args. ARGSIZE is the number of bytes of
7548 arguments that must be copied. ??? How should this value be
7549 computed? We'll also need a safe worst case value for varargs
7551 case BUILT_IN_APPLY
:
7552 if (!validate_arglist (exp
, POINTER_TYPE
,
7553 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
7554 && !validate_arglist (exp
, REFERENCE_TYPE
,
7555 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7561 ops
[0] = expand_normal (CALL_EXPR_ARG (exp
, 0));
7562 ops
[1] = expand_normal (CALL_EXPR_ARG (exp
, 1));
7563 ops
[2] = expand_normal (CALL_EXPR_ARG (exp
, 2));
7565 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
7568 /* __builtin_return (RESULT) causes the function to return the
7569 value described by RESULT. RESULT is address of the block of
7570 memory returned by __builtin_apply. */
7571 case BUILT_IN_RETURN
:
7572 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
7573 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp
, 0)));
7576 case BUILT_IN_SAVEREGS
:
7577 return expand_builtin_saveregs ();
7579 case BUILT_IN_VA_ARG_PACK
:
7580 /* All valid uses of __builtin_va_arg_pack () are removed during
7582 error ("invalid use of %<__builtin_va_arg_pack ()%>");
7585 case BUILT_IN_VA_ARG_PACK_LEN
:
7586 /* All valid uses of __builtin_va_arg_pack_len () are removed during
7588 error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
7591 /* Return the address of the first anonymous stack arg. */
7592 case BUILT_IN_NEXT_ARG
:
7593 if (fold_builtin_next_arg (exp
, false))
7595 return expand_builtin_next_arg ();
7597 case BUILT_IN_CLEAR_CACHE
:
7598 expand_builtin___clear_cache (exp
);
7601 case BUILT_IN_CLASSIFY_TYPE
:
7602 return expand_builtin_classify_type (exp
);
7604 case BUILT_IN_CONSTANT_P
:
7607 case BUILT_IN_FRAME_ADDRESS
:
7608 case BUILT_IN_RETURN_ADDRESS
:
7609 return expand_builtin_frame_address (fndecl
, exp
);
7611 /* Returns the address of the area where the structure is returned.
7613 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
7614 if (call_expr_nargs (exp
) != 0
7615 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
7616 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
7619 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
7621 CASE_BUILT_IN_ALLOCA
:
7622 target
= expand_builtin_alloca (exp
);
7627 case BUILT_IN_ASAN_ALLOCAS_UNPOISON
:
7628 return expand_asan_emit_allocas_unpoison (exp
);
7630 case BUILT_IN_STACK_SAVE
:
7631 return expand_stack_save ();
7633 case BUILT_IN_STACK_RESTORE
:
7634 expand_stack_restore (CALL_EXPR_ARG (exp
, 0));
7637 case BUILT_IN_BSWAP16
:
7638 case BUILT_IN_BSWAP32
:
7639 case BUILT_IN_BSWAP64
:
7640 case BUILT_IN_BSWAP128
:
7641 target
= expand_builtin_bswap (target_mode
, exp
, target
, subtarget
);
7646 CASE_INT_FN (BUILT_IN_FFS
):
7647 target
= expand_builtin_unop (target_mode
, exp
, target
,
7648 subtarget
, ffs_optab
);
7653 CASE_INT_FN (BUILT_IN_CLZ
):
7654 target
= expand_builtin_unop (target_mode
, exp
, target
,
7655 subtarget
, clz_optab
);
7660 CASE_INT_FN (BUILT_IN_CTZ
):
7661 target
= expand_builtin_unop (target_mode
, exp
, target
,
7662 subtarget
, ctz_optab
);
7667 CASE_INT_FN (BUILT_IN_CLRSB
):
7668 target
= expand_builtin_unop (target_mode
, exp
, target
,
7669 subtarget
, clrsb_optab
);
7674 CASE_INT_FN (BUILT_IN_POPCOUNT
):
7675 target
= expand_builtin_unop (target_mode
, exp
, target
,
7676 subtarget
, popcount_optab
);
7681 CASE_INT_FN (BUILT_IN_PARITY
):
7682 target
= expand_builtin_unop (target_mode
, exp
, target
,
7683 subtarget
, parity_optab
);
7688 case BUILT_IN_STRLEN
:
7689 target
= expand_builtin_strlen (exp
, target
, target_mode
);
7694 case BUILT_IN_STRNLEN
:
7695 target
= expand_builtin_strnlen (exp
, target
, target_mode
);
7700 case BUILT_IN_STRCPY
:
7701 target
= expand_builtin_strcpy (exp
, target
);
7706 case BUILT_IN_STRNCPY
:
7707 target
= expand_builtin_strncpy (exp
, target
);
7712 case BUILT_IN_STPCPY
:
7713 target
= expand_builtin_stpcpy (exp
, target
, mode
);
7718 case BUILT_IN_MEMCPY
:
7719 target
= expand_builtin_memcpy (exp
, target
);
7724 case BUILT_IN_MEMMOVE
:
7725 target
= expand_builtin_memmove (exp
, target
);
7730 case BUILT_IN_MEMPCPY
:
7731 target
= expand_builtin_mempcpy (exp
, target
);
7736 case BUILT_IN_MEMSET
:
7737 target
= expand_builtin_memset (exp
, target
, mode
);
7742 case BUILT_IN_BZERO
:
7743 target
= expand_builtin_bzero (exp
);
7748 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7749 back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
7750 when changing it to a strcmp call. */
7751 case BUILT_IN_STRCMP_EQ
:
7752 target
= expand_builtin_memcmp (exp
, target
, true);
7756 /* Change this call back to a BUILT_IN_STRCMP. */
7757 TREE_OPERAND (exp
, 1)
7758 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP
));
7760 /* Delete the last parameter. */
7762 vec
<tree
, va_gc
> *arg_vec
;
7763 vec_alloc (arg_vec
, 2);
7764 for (i
= 0; i
< 2; i
++)
7765 arg_vec
->quick_push (CALL_EXPR_ARG (exp
, i
));
7766 exp
= build_call_vec (TREE_TYPE (exp
), CALL_EXPR_FN (exp
), arg_vec
);
7769 case BUILT_IN_STRCMP
:
7770 target
= expand_builtin_strcmp (exp
, target
);
7775 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7776 back to a BUILT_IN_STRNCMP. */
7777 case BUILT_IN_STRNCMP_EQ
:
7778 target
= expand_builtin_memcmp (exp
, target
, true);
7782 /* Change it back to a BUILT_IN_STRNCMP. */
7783 TREE_OPERAND (exp
, 1)
7784 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP
));
7787 case BUILT_IN_STRNCMP
:
7788 target
= expand_builtin_strncmp (exp
, target
, mode
);
7794 case BUILT_IN_MEMCMP
:
7795 case BUILT_IN_MEMCMP_EQ
:
7796 target
= expand_builtin_memcmp (exp
, target
, fcode
== BUILT_IN_MEMCMP_EQ
);
7799 if (fcode
== BUILT_IN_MEMCMP_EQ
)
7801 tree newdecl
= builtin_decl_explicit (BUILT_IN_MEMCMP
);
7802 TREE_OPERAND (exp
, 1) = build_fold_addr_expr (newdecl
);
7806 case BUILT_IN_SETJMP
:
7807 /* This should have been lowered to the builtins below. */
7810 case BUILT_IN_SETJMP_SETUP
:
7811 /* __builtin_setjmp_setup is passed a pointer to an array of five words
7812 and the receiver label. */
7813 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
7815 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
7816 VOIDmode
, EXPAND_NORMAL
);
7817 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 1), 0);
7818 rtx_insn
*label_r
= label_rtx (label
);
7820 expand_builtin_setjmp_setup (buf_addr
, label_r
);
7825 case BUILT_IN_SETJMP_RECEIVER
:
7826 /* __builtin_setjmp_receiver is passed the receiver label. */
7827 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
7829 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 0), 0);
7830 rtx_insn
*label_r
= label_rtx (label
);
7832 expand_builtin_setjmp_receiver (label_r
);
7833 nonlocal_goto_handler_labels
7834 = gen_rtx_INSN_LIST (VOIDmode
, label_r
,
7835 nonlocal_goto_handler_labels
);
7836 /* ??? Do not let expand_label treat us as such since we would
7837 not want to be both on the list of non-local labels and on
7838 the list of forced labels. */
7839 FORCED_LABEL (label
) = 0;
7844 /* __builtin_longjmp is passed a pointer to an array of five words.
7845 It's similar to the C library longjmp function but works with
7846 __builtin_setjmp above. */
7847 case BUILT_IN_LONGJMP
:
7848 if (validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
7850 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
7851 VOIDmode
, EXPAND_NORMAL
);
7852 rtx value
= expand_normal (CALL_EXPR_ARG (exp
, 1));
7854 if (value
!= const1_rtx
)
7856 error ("%<__builtin_longjmp%> second argument must be 1");
7860 expand_builtin_longjmp (buf_addr
, value
);
7865 case BUILT_IN_NONLOCAL_GOTO
:
7866 target
= expand_builtin_nonlocal_goto (exp
);
7871 /* This updates the setjmp buffer that is its argument with the value
7872 of the current stack pointer. */
7873 case BUILT_IN_UPDATE_SETJMP_BUF
:
7874 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
7877 = expand_normal (CALL_EXPR_ARG (exp
, 0));
7879 expand_builtin_update_setjmp_buf (buf_addr
);
7885 case BUILT_IN_UNREACHABLE_TRAP
:
7886 expand_builtin_trap ();
7889 case BUILT_IN_UNREACHABLE
:
7890 expand_builtin_unreachable ();
7893 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
7894 case BUILT_IN_SIGNBITD32
:
7895 case BUILT_IN_SIGNBITD64
:
7896 case BUILT_IN_SIGNBITD128
:
7897 target
= expand_builtin_signbit (exp
, target
);
7902 /* Various hooks for the DWARF 2 __throw routine. */
7903 case BUILT_IN_UNWIND_INIT
:
7904 expand_builtin_unwind_init ();
7906 case BUILT_IN_DWARF_CFA
:
7907 return virtual_cfa_rtx
;
7908 #ifdef DWARF2_UNWIND_INFO
7909 case BUILT_IN_DWARF_SP_COLUMN
:
7910 return expand_builtin_dwarf_sp_column ();
7911 case BUILT_IN_INIT_DWARF_REG_SIZES
:
7912 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp
, 0));
7915 case BUILT_IN_FROB_RETURN_ADDR
:
7916 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp
, 0));
7917 case BUILT_IN_EXTRACT_RETURN_ADDR
:
7918 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp
, 0));
7919 case BUILT_IN_EH_RETURN
:
7920 expand_builtin_eh_return (CALL_EXPR_ARG (exp
, 0),
7921 CALL_EXPR_ARG (exp
, 1));
7923 case BUILT_IN_EH_RETURN_DATA_REGNO
:
7924 return expand_builtin_eh_return_data_regno (exp
);
7925 case BUILT_IN_EXTEND_POINTER
:
7926 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp
, 0));
7927 case BUILT_IN_EH_POINTER
:
7928 return expand_builtin_eh_pointer (exp
);
7929 case BUILT_IN_EH_FILTER
:
7930 return expand_builtin_eh_filter (exp
);
7931 case BUILT_IN_EH_COPY_VALUES
:
7932 return expand_builtin_eh_copy_values (exp
);
7934 case BUILT_IN_VA_START
:
7935 return expand_builtin_va_start (exp
);
7936 case BUILT_IN_VA_END
:
7937 return expand_builtin_va_end (exp
);
7938 case BUILT_IN_VA_COPY
:
7939 return expand_builtin_va_copy (exp
);
7940 case BUILT_IN_EXPECT
:
7941 return expand_builtin_expect (exp
, target
);
7942 case BUILT_IN_EXPECT_WITH_PROBABILITY
:
7943 return expand_builtin_expect_with_probability (exp
, target
);
7944 case BUILT_IN_ASSUME_ALIGNED
:
7945 return expand_builtin_assume_aligned (exp
, target
);
7946 case BUILT_IN_PREFETCH
:
7947 expand_builtin_prefetch (exp
);
7950 case BUILT_IN_INIT_TRAMPOLINE
:
7951 return expand_builtin_init_trampoline (exp
, true);
7952 case BUILT_IN_INIT_HEAP_TRAMPOLINE
:
7953 return expand_builtin_init_trampoline (exp
, false);
7954 case BUILT_IN_ADJUST_TRAMPOLINE
:
7955 return expand_builtin_adjust_trampoline (exp
);
7957 case BUILT_IN_INIT_DESCRIPTOR
:
7958 return expand_builtin_init_descriptor (exp
);
7959 case BUILT_IN_ADJUST_DESCRIPTOR
:
7960 return expand_builtin_adjust_descriptor (exp
);
7963 case BUILT_IN_EXECL
:
7964 case BUILT_IN_EXECV
:
7965 case BUILT_IN_EXECLP
:
7966 case BUILT_IN_EXECLE
:
7967 case BUILT_IN_EXECVP
:
7968 case BUILT_IN_EXECVE
:
7969 target
= expand_builtin_fork_or_exec (fndecl
, exp
, target
, ignore
);
7974 case BUILT_IN_SYNC_FETCH_AND_ADD_1
:
7975 case BUILT_IN_SYNC_FETCH_AND_ADD_2
:
7976 case BUILT_IN_SYNC_FETCH_AND_ADD_4
:
7977 case BUILT_IN_SYNC_FETCH_AND_ADD_8
:
7978 case BUILT_IN_SYNC_FETCH_AND_ADD_16
:
7979 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_ADD_1
);
7980 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, false, target
);
7985 case BUILT_IN_SYNC_FETCH_AND_SUB_1
:
7986 case BUILT_IN_SYNC_FETCH_AND_SUB_2
:
7987 case BUILT_IN_SYNC_FETCH_AND_SUB_4
:
7988 case BUILT_IN_SYNC_FETCH_AND_SUB_8
:
7989 case BUILT_IN_SYNC_FETCH_AND_SUB_16
:
7990 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_SUB_1
);
7991 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, false, target
);
7996 case BUILT_IN_SYNC_FETCH_AND_OR_1
:
7997 case BUILT_IN_SYNC_FETCH_AND_OR_2
:
7998 case BUILT_IN_SYNC_FETCH_AND_OR_4
:
7999 case BUILT_IN_SYNC_FETCH_AND_OR_8
:
8000 case BUILT_IN_SYNC_FETCH_AND_OR_16
:
8001 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_OR_1
);
8002 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, false, target
);
8007 case BUILT_IN_SYNC_FETCH_AND_AND_1
:
8008 case BUILT_IN_SYNC_FETCH_AND_AND_2
:
8009 case BUILT_IN_SYNC_FETCH_AND_AND_4
:
8010 case BUILT_IN_SYNC_FETCH_AND_AND_8
:
8011 case BUILT_IN_SYNC_FETCH_AND_AND_16
:
8012 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_AND_1
);
8013 target
= expand_builtin_sync_operation (mode
, exp
, AND
, false, target
);
8018 case BUILT_IN_SYNC_FETCH_AND_XOR_1
:
8019 case BUILT_IN_SYNC_FETCH_AND_XOR_2
:
8020 case BUILT_IN_SYNC_FETCH_AND_XOR_4
:
8021 case BUILT_IN_SYNC_FETCH_AND_XOR_8
:
8022 case BUILT_IN_SYNC_FETCH_AND_XOR_16
:
8023 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_XOR_1
);
8024 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, false, target
);
8029 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
8030 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
8031 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
8032 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
8033 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
8034 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_NAND_1
);
8035 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, false, target
);
8040 case BUILT_IN_SYNC_ADD_AND_FETCH_1
:
8041 case BUILT_IN_SYNC_ADD_AND_FETCH_2
:
8042 case BUILT_IN_SYNC_ADD_AND_FETCH_4
:
8043 case BUILT_IN_SYNC_ADD_AND_FETCH_8
:
8044 case BUILT_IN_SYNC_ADD_AND_FETCH_16
:
8045 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_ADD_AND_FETCH_1
);
8046 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, true, target
);
8051 case BUILT_IN_SYNC_SUB_AND_FETCH_1
:
8052 case BUILT_IN_SYNC_SUB_AND_FETCH_2
:
8053 case BUILT_IN_SYNC_SUB_AND_FETCH_4
:
8054 case BUILT_IN_SYNC_SUB_AND_FETCH_8
:
8055 case BUILT_IN_SYNC_SUB_AND_FETCH_16
:
8056 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_SUB_AND_FETCH_1
);
8057 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, true, target
);
8062 case BUILT_IN_SYNC_OR_AND_FETCH_1
:
8063 case BUILT_IN_SYNC_OR_AND_FETCH_2
:
8064 case BUILT_IN_SYNC_OR_AND_FETCH_4
:
8065 case BUILT_IN_SYNC_OR_AND_FETCH_8
:
8066 case BUILT_IN_SYNC_OR_AND_FETCH_16
:
8067 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_OR_AND_FETCH_1
);
8068 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, true, target
);
8073 case BUILT_IN_SYNC_AND_AND_FETCH_1
:
8074 case BUILT_IN_SYNC_AND_AND_FETCH_2
:
8075 case BUILT_IN_SYNC_AND_AND_FETCH_4
:
8076 case BUILT_IN_SYNC_AND_AND_FETCH_8
:
8077 case BUILT_IN_SYNC_AND_AND_FETCH_16
:
8078 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_AND_AND_FETCH_1
);
8079 target
= expand_builtin_sync_operation (mode
, exp
, AND
, true, target
);
8084 case BUILT_IN_SYNC_XOR_AND_FETCH_1
:
8085 case BUILT_IN_SYNC_XOR_AND_FETCH_2
:
8086 case BUILT_IN_SYNC_XOR_AND_FETCH_4
:
8087 case BUILT_IN_SYNC_XOR_AND_FETCH_8
:
8088 case BUILT_IN_SYNC_XOR_AND_FETCH_16
:
8089 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_XOR_AND_FETCH_1
);
8090 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, true, target
);
8095 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
8096 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
8097 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
8098 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
8099 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
8100 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_NAND_AND_FETCH_1
);
8101 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, true, target
);
8106 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
8107 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
8108 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
8109 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
8110 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
8111 if (mode
== VOIDmode
)
8112 mode
= TYPE_MODE (boolean_type_node
);
8113 if (!target
|| !register_operand (target
, mode
))
8114 target
= gen_reg_rtx (mode
);
8116 mode
= get_builtin_sync_mode
8117 (fcode
- BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
);
8118 target
= expand_builtin_compare_and_swap (mode
, exp
, true, target
);
8123 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
:
8124 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2
:
8125 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4
:
8126 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8
:
8127 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16
:
8128 mode
= get_builtin_sync_mode
8129 (fcode
- BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
);
8130 target
= expand_builtin_compare_and_swap (mode
, exp
, false, target
);
8135 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
:
8136 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2
:
8137 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4
:
8138 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8
:
8139 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16
:
8140 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
);
8141 target
= expand_builtin_sync_lock_test_and_set (mode
, exp
, target
);
8146 case BUILT_IN_SYNC_LOCK_RELEASE_1
:
8147 case BUILT_IN_SYNC_LOCK_RELEASE_2
:
8148 case BUILT_IN_SYNC_LOCK_RELEASE_4
:
8149 case BUILT_IN_SYNC_LOCK_RELEASE_8
:
8150 case BUILT_IN_SYNC_LOCK_RELEASE_16
:
8151 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_RELEASE_1
);
8152 expand_builtin_sync_lock_release (mode
, exp
);
8155 case BUILT_IN_SYNC_SYNCHRONIZE
:
8156 expand_builtin_sync_synchronize ();
8159 case BUILT_IN_ATOMIC_EXCHANGE_1
:
8160 case BUILT_IN_ATOMIC_EXCHANGE_2
:
8161 case BUILT_IN_ATOMIC_EXCHANGE_4
:
8162 case BUILT_IN_ATOMIC_EXCHANGE_8
:
8163 case BUILT_IN_ATOMIC_EXCHANGE_16
:
8164 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_EXCHANGE_1
);
8165 target
= expand_builtin_atomic_exchange (mode
, exp
, target
);
8170 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
8171 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
8172 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
8173 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
8174 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
8176 unsigned int nargs
, z
;
8177 vec
<tree
, va_gc
> *vec
;
8180 get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
);
8181 target
= expand_builtin_atomic_compare_exchange (mode
, exp
, target
);
8185 /* If this is turned into an external library call, the weak parameter
8186 must be dropped to match the expected parameter list. */
8187 nargs
= call_expr_nargs (exp
);
8188 vec_alloc (vec
, nargs
- 1);
8189 for (z
= 0; z
< 3; z
++)
8190 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
8191 /* Skip the boolean weak parameter. */
8192 for (z
= 4; z
< 6; z
++)
8193 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
8194 exp
= build_call_vec (TREE_TYPE (exp
), CALL_EXPR_FN (exp
), vec
);
8198 case BUILT_IN_ATOMIC_LOAD_1
:
8199 case BUILT_IN_ATOMIC_LOAD_2
:
8200 case BUILT_IN_ATOMIC_LOAD_4
:
8201 case BUILT_IN_ATOMIC_LOAD_8
:
8202 case BUILT_IN_ATOMIC_LOAD_16
:
8203 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_LOAD_1
);
8204 target
= expand_builtin_atomic_load (mode
, exp
, target
);
8209 case BUILT_IN_ATOMIC_STORE_1
:
8210 case BUILT_IN_ATOMIC_STORE_2
:
8211 case BUILT_IN_ATOMIC_STORE_4
:
8212 case BUILT_IN_ATOMIC_STORE_8
:
8213 case BUILT_IN_ATOMIC_STORE_16
:
8214 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_STORE_1
);
8215 target
= expand_builtin_atomic_store (mode
, exp
);
8220 case BUILT_IN_ATOMIC_ADD_FETCH_1
:
8221 case BUILT_IN_ATOMIC_ADD_FETCH_2
:
8222 case BUILT_IN_ATOMIC_ADD_FETCH_4
:
8223 case BUILT_IN_ATOMIC_ADD_FETCH_8
:
8224 case BUILT_IN_ATOMIC_ADD_FETCH_16
:
8226 enum built_in_function lib
;
8227 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
);
8228 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_ADD_1
+
8229 (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
));
8230 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, true,
8236 case BUILT_IN_ATOMIC_SUB_FETCH_1
:
8237 case BUILT_IN_ATOMIC_SUB_FETCH_2
:
8238 case BUILT_IN_ATOMIC_SUB_FETCH_4
:
8239 case BUILT_IN_ATOMIC_SUB_FETCH_8
:
8240 case BUILT_IN_ATOMIC_SUB_FETCH_16
:
8242 enum built_in_function lib
;
8243 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
);
8244 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_SUB_1
+
8245 (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
));
8246 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, true,
8252 case BUILT_IN_ATOMIC_AND_FETCH_1
:
8253 case BUILT_IN_ATOMIC_AND_FETCH_2
:
8254 case BUILT_IN_ATOMIC_AND_FETCH_4
:
8255 case BUILT_IN_ATOMIC_AND_FETCH_8
:
8256 case BUILT_IN_ATOMIC_AND_FETCH_16
:
8258 enum built_in_function lib
;
8259 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
);
8260 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_AND_1
+
8261 (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
));
8262 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, true,
8268 case BUILT_IN_ATOMIC_NAND_FETCH_1
:
8269 case BUILT_IN_ATOMIC_NAND_FETCH_2
:
8270 case BUILT_IN_ATOMIC_NAND_FETCH_4
:
8271 case BUILT_IN_ATOMIC_NAND_FETCH_8
:
8272 case BUILT_IN_ATOMIC_NAND_FETCH_16
:
8274 enum built_in_function lib
;
8275 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
);
8276 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_NAND_1
+
8277 (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
));
8278 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, true,
8284 case BUILT_IN_ATOMIC_XOR_FETCH_1
:
8285 case BUILT_IN_ATOMIC_XOR_FETCH_2
:
8286 case BUILT_IN_ATOMIC_XOR_FETCH_4
:
8287 case BUILT_IN_ATOMIC_XOR_FETCH_8
:
8288 case BUILT_IN_ATOMIC_XOR_FETCH_16
:
8290 enum built_in_function lib
;
8291 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
);
8292 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_XOR_1
+
8293 (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
));
8294 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, true,
8300 case BUILT_IN_ATOMIC_OR_FETCH_1
:
8301 case BUILT_IN_ATOMIC_OR_FETCH_2
:
8302 case BUILT_IN_ATOMIC_OR_FETCH_4
:
8303 case BUILT_IN_ATOMIC_OR_FETCH_8
:
8304 case BUILT_IN_ATOMIC_OR_FETCH_16
:
8306 enum built_in_function lib
;
8307 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
);
8308 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_OR_1
+
8309 (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
));
8310 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, true,
8316 case BUILT_IN_ATOMIC_FETCH_ADD_1
:
8317 case BUILT_IN_ATOMIC_FETCH_ADD_2
:
8318 case BUILT_IN_ATOMIC_FETCH_ADD_4
:
8319 case BUILT_IN_ATOMIC_FETCH_ADD_8
:
8320 case BUILT_IN_ATOMIC_FETCH_ADD_16
:
8321 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_ADD_1
);
8322 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, false,
8323 ignore
, BUILT_IN_NONE
);
8328 case BUILT_IN_ATOMIC_FETCH_SUB_1
:
8329 case BUILT_IN_ATOMIC_FETCH_SUB_2
:
8330 case BUILT_IN_ATOMIC_FETCH_SUB_4
:
8331 case BUILT_IN_ATOMIC_FETCH_SUB_8
:
8332 case BUILT_IN_ATOMIC_FETCH_SUB_16
:
8333 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_SUB_1
);
8334 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, false,
8335 ignore
, BUILT_IN_NONE
);
8340 case BUILT_IN_ATOMIC_FETCH_AND_1
:
8341 case BUILT_IN_ATOMIC_FETCH_AND_2
:
8342 case BUILT_IN_ATOMIC_FETCH_AND_4
:
8343 case BUILT_IN_ATOMIC_FETCH_AND_8
:
8344 case BUILT_IN_ATOMIC_FETCH_AND_16
:
8345 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_AND_1
);
8346 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, false,
8347 ignore
, BUILT_IN_NONE
);
8352 case BUILT_IN_ATOMIC_FETCH_NAND_1
:
8353 case BUILT_IN_ATOMIC_FETCH_NAND_2
:
8354 case BUILT_IN_ATOMIC_FETCH_NAND_4
:
8355 case BUILT_IN_ATOMIC_FETCH_NAND_8
:
8356 case BUILT_IN_ATOMIC_FETCH_NAND_16
:
8357 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_NAND_1
);
8358 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, false,
8359 ignore
, BUILT_IN_NONE
);
8364 case BUILT_IN_ATOMIC_FETCH_XOR_1
:
8365 case BUILT_IN_ATOMIC_FETCH_XOR_2
:
8366 case BUILT_IN_ATOMIC_FETCH_XOR_4
:
8367 case BUILT_IN_ATOMIC_FETCH_XOR_8
:
8368 case BUILT_IN_ATOMIC_FETCH_XOR_16
:
8369 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_XOR_1
);
8370 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, false,
8371 ignore
, BUILT_IN_NONE
);
8376 case BUILT_IN_ATOMIC_FETCH_OR_1
:
8377 case BUILT_IN_ATOMIC_FETCH_OR_2
:
8378 case BUILT_IN_ATOMIC_FETCH_OR_4
:
8379 case BUILT_IN_ATOMIC_FETCH_OR_8
:
8380 case BUILT_IN_ATOMIC_FETCH_OR_16
:
8381 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_OR_1
);
8382 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, false,
8383 ignore
, BUILT_IN_NONE
);
8388 case BUILT_IN_ATOMIC_TEST_AND_SET
:
8389 return expand_builtin_atomic_test_and_set (exp
, target
);
8391 case BUILT_IN_ATOMIC_CLEAR
:
8392 return expand_builtin_atomic_clear (exp
);
8394 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
8395 return expand_builtin_atomic_always_lock_free (exp
);
8397 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
8398 target
= expand_builtin_atomic_is_lock_free (exp
);
8403 case BUILT_IN_ATOMIC_THREAD_FENCE
:
8404 expand_builtin_atomic_thread_fence (exp
);
8407 case BUILT_IN_ATOMIC_SIGNAL_FENCE
:
8408 expand_builtin_atomic_signal_fence (exp
);
8411 case BUILT_IN_OBJECT_SIZE
:
8412 case BUILT_IN_DYNAMIC_OBJECT_SIZE
:
8413 return expand_builtin_object_size (exp
);
8415 case BUILT_IN_MEMCPY_CHK
:
8416 case BUILT_IN_MEMPCPY_CHK
:
8417 case BUILT_IN_MEMMOVE_CHK
:
8418 case BUILT_IN_MEMSET_CHK
:
8419 target
= expand_builtin_memory_chk (exp
, target
, mode
, fcode
);
8424 case BUILT_IN_STRCPY_CHK
:
8425 case BUILT_IN_STPCPY_CHK
:
8426 case BUILT_IN_STRNCPY_CHK
:
8427 case BUILT_IN_STPNCPY_CHK
:
8428 case BUILT_IN_STRCAT_CHK
:
8429 case BUILT_IN_STRNCAT_CHK
:
8430 case BUILT_IN_SNPRINTF_CHK
:
8431 case BUILT_IN_VSNPRINTF_CHK
:
8432 maybe_emit_chk_warning (exp
, fcode
);
8435 case BUILT_IN_SPRINTF_CHK
:
8436 case BUILT_IN_VSPRINTF_CHK
:
8437 maybe_emit_sprintf_chk_warning (exp
, fcode
);
8440 case BUILT_IN_THREAD_POINTER
:
8441 return expand_builtin_thread_pointer (exp
, target
);
8443 case BUILT_IN_SET_THREAD_POINTER
:
8444 expand_builtin_set_thread_pointer (exp
);
8447 case BUILT_IN_ACC_ON_DEVICE
:
8448 /* Do library call, if we failed to expand the builtin when
8452 case BUILT_IN_GOACC_PARLEVEL_ID
:
8453 case BUILT_IN_GOACC_PARLEVEL_SIZE
:
8454 return expand_builtin_goacc_parlevel_id_size (exp
, target
, ignore
);
8456 case BUILT_IN_SPECULATION_SAFE_VALUE_PTR
:
8457 return expand_speculation_safe_value (VOIDmode
, exp
, target
, ignore
);
8459 case BUILT_IN_SPECULATION_SAFE_VALUE_1
:
8460 case BUILT_IN_SPECULATION_SAFE_VALUE_2
:
8461 case BUILT_IN_SPECULATION_SAFE_VALUE_4
:
8462 case BUILT_IN_SPECULATION_SAFE_VALUE_8
:
8463 case BUILT_IN_SPECULATION_SAFE_VALUE_16
:
8464 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SPECULATION_SAFE_VALUE_1
);
8465 return expand_speculation_safe_value (mode
, exp
, target
, ignore
);
8467 default: /* just do library call, if unknown builtin */
8471 /* The switch statement above can drop through to cause the function
8472 to be called normally. */
8473 return expand_call (exp
, target
, ignore
);
8476 /* Determine whether a tree node represents a call to a built-in
8477 function. If the tree T is a call to a built-in function with
8478 the right number of arguments of the appropriate types, return
8479 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
8480 Otherwise the return value is END_BUILTINS. */
8482 enum built_in_function
8483 builtin_mathfn_code (const_tree t
)
8485 const_tree fndecl
, arg
, parmlist
;
8486 const_tree argtype
, parmtype
;
8487 const_call_expr_arg_iterator iter
;
8489 if (TREE_CODE (t
) != CALL_EXPR
)
8490 return END_BUILTINS
;
8492 fndecl
= get_callee_fndecl (t
);
8493 if (fndecl
== NULL_TREE
|| !fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
))
8494 return END_BUILTINS
;
8496 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
8497 init_const_call_expr_arg_iterator (t
, &iter
);
8498 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
8500 /* If a function doesn't take a variable number of arguments,
8501 the last element in the list will have type `void'. */
8502 parmtype
= TREE_VALUE (parmlist
);
8503 if (VOID_TYPE_P (parmtype
))
8505 if (more_const_call_expr_args_p (&iter
))
8506 return END_BUILTINS
;
8507 return DECL_FUNCTION_CODE (fndecl
);
8510 if (! more_const_call_expr_args_p (&iter
))
8511 return END_BUILTINS
;
8513 arg
= next_const_call_expr_arg (&iter
);
8514 argtype
= TREE_TYPE (arg
);
8516 if (SCALAR_FLOAT_TYPE_P (parmtype
))
8518 if (! SCALAR_FLOAT_TYPE_P (argtype
))
8519 return END_BUILTINS
;
8521 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
8523 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
8524 return END_BUILTINS
;
8526 else if (POINTER_TYPE_P (parmtype
))
8528 if (! POINTER_TYPE_P (argtype
))
8529 return END_BUILTINS
;
8531 else if (INTEGRAL_TYPE_P (parmtype
))
8533 if (! INTEGRAL_TYPE_P (argtype
))
8534 return END_BUILTINS
;
8537 return END_BUILTINS
;
8540 /* Variable-length argument list. */
8541 return DECL_FUNCTION_CODE (fndecl
);
8544 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
8545 evaluate to a constant. */
8548 fold_builtin_constant_p (tree arg
)
8550 /* We return 1 for a numeric type that's known to be a constant
8551 value at compile-time or for an aggregate type that's a
8552 literal constant. */
8555 /* If we know this is a constant, emit the constant of one. */
8556 if (CONSTANT_CLASS_P (arg
)
8557 || (TREE_CODE (arg
) == CONSTRUCTOR
8558 && TREE_CONSTANT (arg
)))
8559 return integer_one_node
;
8560 if (TREE_CODE (arg
) == ADDR_EXPR
)
8562 tree op
= TREE_OPERAND (arg
, 0);
8563 if (TREE_CODE (op
) == STRING_CST
8564 || (TREE_CODE (op
) == ARRAY_REF
8565 && integer_zerop (TREE_OPERAND (op
, 1))
8566 && TREE_CODE (TREE_OPERAND (op
, 0)) == STRING_CST
))
8567 return integer_one_node
;
8570 /* If this expression has side effects, show we don't know it to be a
8571 constant. Likewise if it's a pointer or aggregate type since in
8572 those case we only want literals, since those are only optimized
8573 when generating RTL, not later.
8574 And finally, if we are compiling an initializer, not code, we
8575 need to return a definite result now; there's not going to be any
8576 more optimization done. */
8577 if (TREE_SIDE_EFFECTS (arg
)
8578 || AGGREGATE_TYPE_P (TREE_TYPE (arg
))
8579 || POINTER_TYPE_P (TREE_TYPE (arg
))
8581 || folding_initializer
8582 || force_folding_builtin_constant_p
)
8583 return integer_zero_node
;
8588 /* Create builtin_expect or builtin_expect_with_probability
8589 with PRED and EXPECTED as its arguments and return it as a truthvalue.
8590 Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
8591 builtin_expect_with_probability instead uses third argument as PROBABILITY
8595 build_builtin_expect_predicate (location_t loc
, tree pred
, tree expected
,
8596 tree predictor
, tree probability
)
8598 tree fn
, arg_types
, pred_type
, expected_type
, call_expr
, ret_type
;
8600 fn
= builtin_decl_explicit (probability
== NULL_TREE
? BUILT_IN_EXPECT
8601 : BUILT_IN_EXPECT_WITH_PROBABILITY
);
8602 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
8603 ret_type
= TREE_TYPE (TREE_TYPE (fn
));
8604 pred_type
= TREE_VALUE (arg_types
);
8605 expected_type
= TREE_VALUE (TREE_CHAIN (arg_types
));
8607 pred
= fold_convert_loc (loc
, pred_type
, pred
);
8608 expected
= fold_convert_loc (loc
, expected_type
, expected
);
8611 call_expr
= build_call_expr_loc (loc
, fn
, 3, pred
, expected
, probability
);
8613 call_expr
= build_call_expr_loc (loc
, fn
, predictor
? 3 : 2, pred
, expected
,
8616 return build2 (NE_EXPR
, TREE_TYPE (pred
), call_expr
,
8617 build_int_cst (ret_type
, 0));
8620 /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
8621 NULL_TREE if no simplification is possible. */
8624 fold_builtin_expect (location_t loc
, tree arg0
, tree arg1
, tree arg2
,
8627 tree inner
, fndecl
, inner_arg0
;
8628 enum tree_code code
;
8630 /* Distribute the expected value over short-circuiting operators.
8631 See through the cast from truthvalue_type_node to long. */
8633 while (CONVERT_EXPR_P (inner_arg0
)
8634 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0
))
8635 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0
, 0))))
8636 inner_arg0
= TREE_OPERAND (inner_arg0
, 0);
8638 /* If this is a builtin_expect within a builtin_expect keep the
8639 inner one. See through a comparison against a constant. It
8640 might have been added to create a thruthvalue. */
8643 if (COMPARISON_CLASS_P (inner
)
8644 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
)
8645 inner
= TREE_OPERAND (inner
, 0);
8647 if (TREE_CODE (inner
) == CALL_EXPR
8648 && (fndecl
= get_callee_fndecl (inner
))
8649 && fndecl_built_in_p (fndecl
, BUILT_IN_EXPECT
,
8650 BUILT_IN_EXPECT_WITH_PROBABILITY
))
8654 code
= TREE_CODE (inner
);
8655 if (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
8657 tree op0
= TREE_OPERAND (inner
, 0);
8658 tree op1
= TREE_OPERAND (inner
, 1);
8659 arg1
= save_expr (arg1
);
8661 op0
= build_builtin_expect_predicate (loc
, op0
, arg1
, arg2
, arg3
);
8662 op1
= build_builtin_expect_predicate (loc
, op1
, arg1
, arg2
, arg3
);
8663 inner
= build2 (code
, TREE_TYPE (inner
), op0
, op1
);
8665 return fold_convert_loc (loc
, TREE_TYPE (arg0
), inner
);
8668 /* If the argument isn't invariant then there's nothing else we can do. */
8669 if (!TREE_CONSTANT (inner_arg0
))
8672 /* If we expect that a comparison against the argument will fold to
8673 a constant return the constant. In practice, this means a true
8674 constant or the address of a non-weak symbol. */
8677 if (TREE_CODE (inner
) == ADDR_EXPR
)
8681 inner
= TREE_OPERAND (inner
, 0);
8683 while (TREE_CODE (inner
) == COMPONENT_REF
8684 || TREE_CODE (inner
) == ARRAY_REF
);
8685 if (VAR_OR_FUNCTION_DECL_P (inner
) && DECL_WEAK (inner
))
8689 /* Otherwise, ARG0 already has the proper type for the return value. */
8693 /* Fold a call to __builtin_classify_type with argument ARG. */
8696 fold_builtin_classify_type (tree arg
)
8699 return build_int_cst (integer_type_node
, no_type_class
);
8701 return build_int_cst (integer_type_node
, type_to_class (TREE_TYPE (arg
)));
8704 /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
8708 fold_builtin_strlen (location_t loc
, tree expr
, tree type
, tree arg
)
8710 if (!validate_arg (arg
, POINTER_TYPE
))
8714 c_strlen_data lendata
= { };
8715 tree len
= c_strlen (arg
, 0, &lendata
);
8718 return fold_convert_loc (loc
, type
, len
);
8720 /* TODO: Move this to gimple-ssa-warn-access once the pass runs
8721 also early enough to detect invalid reads in multimensional
8722 arrays and struct members. */
8724 c_strlen (arg
, 1, &lendata
);
8728 if (EXPR_HAS_LOCATION (arg
))
8729 loc
= EXPR_LOCATION (arg
);
8730 else if (loc
== UNKNOWN_LOCATION
)
8731 loc
= input_location
;
8732 warn_string_no_nul (loc
, expr
, "strlen", arg
, lendata
.decl
);
8739 /* Fold a call to __builtin_inf or __builtin_huge_val. */
8742 fold_builtin_inf (location_t loc
, tree type
, int warn
)
8744 /* __builtin_inff is intended to be usable to define INFINITY on all
8745 targets. If an infinity is not available, INFINITY expands "to a
8746 positive constant of type float that overflows at translation
8747 time", footnote "In this case, using INFINITY will violate the
8748 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
8749 Thus we pedwarn to ensure this constraint violation is
8751 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
8752 pedwarn (loc
, 0, "target format does not support infinity");
8754 return build_real (type
, dconstinf
);
8757 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8758 NULL_TREE if no simplification can be made. */
8761 fold_builtin_sincos (location_t loc
,
8762 tree arg0
, tree arg1
, tree arg2
)
8765 tree fndecl
, call
= NULL_TREE
;
8767 if (!validate_arg (arg0
, REAL_TYPE
)
8768 || !validate_arg (arg1
, POINTER_TYPE
)
8769 || !validate_arg (arg2
, POINTER_TYPE
))
8772 type
= TREE_TYPE (arg0
);
8774 /* Calculate the result when the argument is a constant. */
8775 built_in_function fn
= mathfn_built_in_2 (type
, CFN_BUILT_IN_CEXPI
);
8776 if (fn
== END_BUILTINS
)
8779 /* Canonicalize sincos to cexpi. */
8780 if (TREE_CODE (arg0
) == REAL_CST
)
8782 tree complex_type
= build_complex_type (type
);
8783 call
= fold_const_call (as_combined_fn (fn
), complex_type
, arg0
);
8787 if (!targetm
.libc_has_function (function_c99_math_complex
, type
)
8788 || !builtin_decl_implicit_p (fn
))
8790 fndecl
= builtin_decl_explicit (fn
);
8791 call
= build_call_expr_loc (loc
, fndecl
, 1, arg0
);
8792 call
= builtin_save_expr (call
);
8795 tree ptype
= build_pointer_type (type
);
8796 arg1
= fold_convert (ptype
, arg1
);
8797 arg2
= fold_convert (ptype
, arg2
);
8798 return build2 (COMPOUND_EXPR
, void_type_node
,
8799 build2 (MODIFY_EXPR
, void_type_node
,
8800 build_fold_indirect_ref_loc (loc
, arg1
),
8801 fold_build1_loc (loc
, IMAGPART_EXPR
, type
, call
)),
8802 build2 (MODIFY_EXPR
, void_type_node
,
8803 build_fold_indirect_ref_loc (loc
, arg2
),
8804 fold_build1_loc (loc
, REALPART_EXPR
, type
, call
)));
8807 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8808 Return NULL_TREE if no simplification can be made. */
8811 fold_builtin_memcmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
8813 if (!validate_arg (arg1
, POINTER_TYPE
)
8814 || !validate_arg (arg2
, POINTER_TYPE
)
8815 || !validate_arg (len
, INTEGER_TYPE
))
8818 /* If the LEN parameter is zero, return zero. */
8819 if (integer_zerop (len
))
8820 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
8823 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8824 if (operand_equal_p (arg1
, arg2
, 0))
8825 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
8827 /* If len parameter is one, return an expression corresponding to
8828 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8829 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
8831 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
8832 tree cst_uchar_ptr_node
8833 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
8836 = fold_convert_loc (loc
, integer_type_node
,
8837 build1 (INDIRECT_REF
, cst_uchar_node
,
8838 fold_convert_loc (loc
,
8842 = fold_convert_loc (loc
, integer_type_node
,
8843 build1 (INDIRECT_REF
, cst_uchar_node
,
8844 fold_convert_loc (loc
,
8847 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
8853 /* Fold a call to builtin isascii with argument ARG. */
8856 fold_builtin_isascii (location_t loc
, tree arg
)
8858 if (!validate_arg (arg
, INTEGER_TYPE
))
8862 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8863 arg
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
8864 build_int_cst (integer_type_node
,
8865 ~ (unsigned HOST_WIDE_INT
) 0x7f));
8866 return fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
,
8867 arg
, integer_zero_node
);
8871 /* Fold a call to builtin toascii with argument ARG. */
8874 fold_builtin_toascii (location_t loc
, tree arg
)
8876 if (!validate_arg (arg
, INTEGER_TYPE
))
8879 /* Transform toascii(c) -> (c & 0x7f). */
8880 return fold_build2_loc (loc
, BIT_AND_EXPR
, integer_type_node
, arg
,
8881 build_int_cst (integer_type_node
, 0x7f));
8884 /* Fold a call to builtin isdigit with argument ARG. */
8887 fold_builtin_isdigit (location_t loc
, tree arg
)
8889 if (!validate_arg (arg
, INTEGER_TYPE
))
8893 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8894 /* According to the C standard, isdigit is unaffected by locale.
8895 However, it definitely is affected by the target character set. */
8896 unsigned HOST_WIDE_INT target_digit0
8897 = lang_hooks
.to_target_charset ('0');
8899 if (target_digit0
== 0)
8902 arg
= fold_convert_loc (loc
, unsigned_type_node
, arg
);
8903 arg
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
8904 build_int_cst (unsigned_type_node
, target_digit0
));
8905 return fold_build2_loc (loc
, LE_EXPR
, integer_type_node
, arg
,
8906 build_int_cst (unsigned_type_node
, 9));
8910 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8913 fold_builtin_fabs (location_t loc
, tree arg
, tree type
)
8915 if (!validate_arg (arg
, REAL_TYPE
))
8918 arg
= fold_convert_loc (loc
, type
, arg
);
8919 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
8922 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8925 fold_builtin_abs (location_t loc
, tree arg
, tree type
)
8927 if (!validate_arg (arg
, INTEGER_TYPE
))
8930 arg
= fold_convert_loc (loc
, type
, arg
);
8931 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
8934 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8937 fold_builtin_carg (location_t loc
, tree arg
, tree type
)
8939 if (validate_arg (arg
, COMPLEX_TYPE
)
8940 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg
))))
8942 tree atan2_fn
= mathfn_built_in (type
, BUILT_IN_ATAN2
);
8946 tree new_arg
= builtin_save_expr (arg
);
8947 tree r_arg
= fold_build1_loc (loc
, REALPART_EXPR
, type
, new_arg
);
8948 tree i_arg
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, new_arg
);
8949 return build_call_expr_loc (loc
, atan2_fn
, 2, i_arg
, r_arg
);
8956 /* Fold a call to builtin frexp, we can assume the base is 2. */
8959 fold_builtin_frexp (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
8961 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
8966 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
8969 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
8971 /* Proceed if a valid pointer type was passed in. */
8972 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == integer_type_node
)
8974 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
8975 tree frac
, exp
, res
;
8980 /* For +-0, return (*exp = 0, +-0). */
8981 exp
= integer_zero_node
;
8986 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8987 return omit_one_operand_loc (loc
, rettype
, arg0
, arg1
);
8990 /* Since the frexp function always expects base 2, and in
8991 GCC normalized significands are already in the range
8992 [0.5, 1.0), we have exactly what frexp wants. */
8993 REAL_VALUE_TYPE frac_rvt
= *value
;
8994 SET_REAL_EXP (&frac_rvt
, 0);
8995 frac
= build_real (rettype
, frac_rvt
);
8996 exp
= build_int_cst (integer_type_node
, REAL_EXP (value
));
9003 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9004 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
, exp
);
9005 TREE_SIDE_EFFECTS (arg1
) = 1;
9006 res
= fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
, frac
);
9007 suppress_warning (res
, OPT_Wunused_value
);
9014 /* Fold a call to builtin modf. */
9017 fold_builtin_modf (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
9019 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
9024 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
9027 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
9029 /* Proceed if a valid pointer type was passed in. */
9030 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == TYPE_MAIN_VARIANT (rettype
))
9032 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
9033 REAL_VALUE_TYPE trunc
, frac
;
9040 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9041 trunc
= frac
= *value
;
9044 /* For +-Inf, return (*arg1 = arg0, +-0). */
9046 frac
.sign
= value
->sign
;
9050 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9051 real_trunc (&trunc
, VOIDmode
, value
);
9052 real_arithmetic (&frac
, MINUS_EXPR
, value
, &trunc
);
9053 /* If the original number was negative and already
9054 integral, then the fractional part is -0.0. */
9055 if (value
->sign
&& frac
.cl
== rvc_zero
)
9056 frac
.sign
= value
->sign
;
9060 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9061 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
,
9062 build_real (rettype
, trunc
));
9063 TREE_SIDE_EFFECTS (arg1
) = 1;
9064 res
= fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
,
9065 build_real (rettype
, frac
));
9066 suppress_warning (res
, OPT_Wunused_value
);
9073 /* Given a location LOC, an interclass builtin function decl FNDECL
9074 and its single argument ARG, return an folded expression computing
9075 the same, or NULL_TREE if we either couldn't or didn't want to fold
9076 (the latter happen if there's an RTL instruction available). */
9079 fold_builtin_interclass_mathfn (location_t loc
, tree fndecl
, tree arg
)
9083 if (!validate_arg (arg
, REAL_TYPE
))
9086 if (interclass_mathfn_icode (arg
, fndecl
) != CODE_FOR_nothing
)
9089 mode
= TYPE_MODE (TREE_TYPE (arg
));
9091 bool is_ibm_extended
= MODE_COMPOSITE_P (mode
);
9093 /* If there is no optab, try generic code. */
9094 switch (DECL_FUNCTION_CODE (fndecl
))
9098 CASE_FLT_FN (BUILT_IN_ISINF
):
9100 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9101 tree
const isgr_fn
= builtin_decl_explicit (BUILT_IN_ISGREATER
);
9102 tree type
= TREE_TYPE (arg
);
9106 if (is_ibm_extended
)
9108 /* NaN and Inf are encoded in the high-order double value
9109 only. The low-order value is not significant. */
9110 type
= double_type_node
;
9112 arg
= fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
9114 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
), false);
9115 real_from_string (&r
, buf
);
9116 result
= build_call_expr (isgr_fn
, 2,
9117 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
9118 build_real (type
, r
));
9121 CASE_FLT_FN (BUILT_IN_FINITE
):
9122 case BUILT_IN_ISFINITE
:
9124 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9125 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
9126 tree type
= TREE_TYPE (arg
);
9130 if (is_ibm_extended
)
9132 /* NaN and Inf are encoded in the high-order double value
9133 only. The low-order value is not significant. */
9134 type
= double_type_node
;
9136 arg
= fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
9138 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
), false);
9139 real_from_string (&r
, buf
);
9140 result
= build_call_expr (isle_fn
, 2,
9141 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
9142 build_real (type
, r
));
9143 /*result = fold_build2_loc (loc, UNGT_EXPR,
9144 TREE_TYPE (TREE_TYPE (fndecl)),
9145 fold_build1_loc (loc, ABS_EXPR, type, arg),
9146 build_real (type, r));
9147 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9148 TREE_TYPE (TREE_TYPE (fndecl)),
9152 case BUILT_IN_ISNORMAL
:
9154 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9155 islessequal(fabs(x),DBL_MAX). */
9156 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
9157 tree type
= TREE_TYPE (arg
);
9158 tree orig_arg
, max_exp
, min_exp
;
9159 machine_mode orig_mode
= mode
;
9160 REAL_VALUE_TYPE rmax
, rmin
;
9163 orig_arg
= arg
= builtin_save_expr (arg
);
9164 if (is_ibm_extended
)
9166 /* Use double to test the normal range of IBM extended
9167 precision. Emin for IBM extended precision is
9168 different to emin for IEEE double, being 53 higher
9169 since the low double exponent is at least 53 lower
9170 than the high double exponent. */
9171 type
= double_type_node
;
9173 arg
= fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
9175 arg
= fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
9177 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
), false);
9178 real_from_string (&rmax
, buf
);
9179 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (orig_mode
)->emin
- 1);
9180 real_from_string (&rmin
, buf
);
9181 max_exp
= build_real (type
, rmax
);
9182 min_exp
= build_real (type
, rmin
);
9184 max_exp
= build_call_expr (isle_fn
, 2, arg
, max_exp
);
9185 if (is_ibm_extended
)
9187 /* Testing the high end of the range is done just using
9188 the high double, using the same test as isfinite().
9189 For the subnormal end of the range we first test the
9190 high double, then if its magnitude is equal to the
9191 limit of 0x1p-969, we test whether the low double is
9192 non-zero and opposite sign to the high double. */
9193 tree
const islt_fn
= builtin_decl_explicit (BUILT_IN_ISLESS
);
9194 tree
const isgt_fn
= builtin_decl_explicit (BUILT_IN_ISGREATER
);
9195 tree gt_min
= build_call_expr (isgt_fn
, 2, arg
, min_exp
);
9196 tree eq_min
= fold_build2 (EQ_EXPR
, integer_type_node
,
9198 tree as_complex
= build1 (VIEW_CONVERT_EXPR
,
9199 complex_double_type_node
, orig_arg
);
9200 tree hi_dbl
= build1 (REALPART_EXPR
, type
, as_complex
);
9201 tree lo_dbl
= build1 (IMAGPART_EXPR
, type
, as_complex
);
9202 tree zero
= build_real (type
, dconst0
);
9203 tree hilt
= build_call_expr (islt_fn
, 2, hi_dbl
, zero
);
9204 tree lolt
= build_call_expr (islt_fn
, 2, lo_dbl
, zero
);
9205 tree logt
= build_call_expr (isgt_fn
, 2, lo_dbl
, zero
);
9206 tree ok_lo
= fold_build1 (TRUTH_NOT_EXPR
, integer_type_node
,
9207 fold_build3 (COND_EXPR
,
9210 eq_min
= fold_build2 (TRUTH_ANDIF_EXPR
, integer_type_node
,
9212 min_exp
= fold_build2 (TRUTH_ORIF_EXPR
, integer_type_node
,
9218 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL
);
9219 min_exp
= build_call_expr (isge_fn
, 2, arg
, min_exp
);
9221 result
= fold_build2 (BIT_AND_EXPR
, integer_type_node
,
9232 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9233 ARG is the argument for the call. */
9236 fold_builtin_classify (location_t loc
, tree fndecl
, tree arg
, int builtin_index
)
9238 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9240 if (!validate_arg (arg
, REAL_TYPE
))
9243 switch (builtin_index
)
9245 case BUILT_IN_ISINF
:
9246 if (tree_expr_infinite_p (arg
))
9247 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
9248 if (!tree_expr_maybe_infinite_p (arg
))
9249 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9252 case BUILT_IN_ISINF_SIGN
:
9254 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9255 /* In a boolean context, GCC will fold the inner COND_EXPR to
9256 1. So e.g. "if (isinf_sign(x))" would be folded to just
9257 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9258 tree signbit_fn
= builtin_decl_explicit (BUILT_IN_SIGNBIT
);
9259 tree isinf_fn
= builtin_decl_explicit (BUILT_IN_ISINF
);
9260 tree tmp
= NULL_TREE
;
9262 arg
= builtin_save_expr (arg
);
9264 if (signbit_fn
&& isinf_fn
)
9266 tree signbit_call
= build_call_expr_loc (loc
, signbit_fn
, 1, arg
);
9267 tree isinf_call
= build_call_expr_loc (loc
, isinf_fn
, 1, arg
);
9269 signbit_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
9270 signbit_call
, integer_zero_node
);
9271 isinf_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
9272 isinf_call
, integer_zero_node
);
9274 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, signbit_call
,
9275 integer_minus_one_node
, integer_one_node
);
9276 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
9284 case BUILT_IN_ISFINITE
:
9285 if (tree_expr_finite_p (arg
))
9286 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
9287 if (tree_expr_nan_p (arg
) || tree_expr_infinite_p (arg
))
9288 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9291 case BUILT_IN_ISNAN
:
9292 if (tree_expr_nan_p (arg
))
9293 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
9294 if (!tree_expr_maybe_nan_p (arg
))
9295 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9298 bool is_ibm_extended
= MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg
)));
9299 if (is_ibm_extended
)
9301 /* NaN and Inf are encoded in the high-order double value
9302 only. The low-order value is not significant. */
9303 arg
= fold_build1_loc (loc
, NOP_EXPR
, double_type_node
, arg
);
9306 arg
= builtin_save_expr (arg
);
9307 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg
, arg
);
9309 case BUILT_IN_ISSIGNALING
:
9310 /* Folding to true for REAL_CST is done in fold_const_call_ss.
9311 Don't use tree_expr_signaling_nan_p (arg) -> integer_one_node
9312 and !tree_expr_maybe_signaling_nan_p (arg) -> integer_zero_node
9313 here, so there is some possibility of __builtin_issignaling working
9314 without -fsignaling-nans. Especially when -fno-signaling-nans is
9316 if (!tree_expr_maybe_nan_p (arg
))
9317 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
9325 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9326 This builtin will generate code to return the appropriate floating
9327 point classification depending on the value of the floating point
9328 number passed in. The possible return values must be supplied as
9329 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9330 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9331 one floating point argument which is "type generic". */
9334 fold_builtin_fpclassify (location_t loc
, tree
*args
, int nargs
)
9336 tree fp_nan
, fp_infinite
, fp_normal
, fp_subnormal
, fp_zero
,
9337 arg
, type
, res
, tmp
;
9342 /* Verify the required arguments in the original call. */
9344 || !validate_arg (args
[0], INTEGER_TYPE
)
9345 || !validate_arg (args
[1], INTEGER_TYPE
)
9346 || !validate_arg (args
[2], INTEGER_TYPE
)
9347 || !validate_arg (args
[3], INTEGER_TYPE
)
9348 || !validate_arg (args
[4], INTEGER_TYPE
)
9349 || !validate_arg (args
[5], REAL_TYPE
))
9353 fp_infinite
= args
[1];
9354 fp_normal
= args
[2];
9355 fp_subnormal
= args
[3];
9358 type
= TREE_TYPE (arg
);
9359 mode
= TYPE_MODE (type
);
9360 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
9364 (fabs(x) == Inf ? FP_INFINITE :
9365 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9366 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9368 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
9369 build_real (type
, dconst0
));
9370 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
9371 tmp
, fp_zero
, fp_subnormal
);
9373 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
9374 real_from_string (&r
, buf
);
9375 tmp
= fold_build2_loc (loc
, GE_EXPR
, integer_type_node
,
9376 arg
, build_real (type
, r
));
9377 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, fp_normal
, res
);
9379 if (tree_expr_maybe_infinite_p (arg
))
9381 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
9382 build_real (type
, dconstinf
));
9383 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
,
9387 if (tree_expr_maybe_nan_p (arg
))
9389 tmp
= fold_build2_loc (loc
, ORDERED_EXPR
, integer_type_node
, arg
, arg
);
9390 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, res
, fp_nan
);
9396 /* Fold a call to an unordered comparison function such as
9397 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9398 being called and ARG0 and ARG1 are the arguments for the call.
9399 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9400 the opposite of the desired result. UNORDERED_CODE is used
9401 for modes that can hold NaNs and ORDERED_CODE is used for
9405 fold_builtin_unordered_cmp (location_t loc
, tree fndecl
, tree arg0
, tree arg1
,
9406 enum tree_code unordered_code
,
9407 enum tree_code ordered_code
)
9409 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9410 enum tree_code code
;
9412 enum tree_code code0
, code1
;
9413 tree cmp_type
= NULL_TREE
;
9415 type0
= TREE_TYPE (arg0
);
9416 type1
= TREE_TYPE (arg1
);
9418 code0
= TREE_CODE (type0
);
9419 code1
= TREE_CODE (type1
);
9421 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
9422 /* Choose the wider of two real types. */
9423 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
9425 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
9427 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
9430 arg0
= fold_convert_loc (loc
, cmp_type
, arg0
);
9431 arg1
= fold_convert_loc (loc
, cmp_type
, arg1
);
9433 if (unordered_code
== UNORDERED_EXPR
)
9435 if (tree_expr_nan_p (arg0
) || tree_expr_nan_p (arg1
))
9436 return omit_two_operands_loc (loc
, type
, integer_one_node
, arg0
, arg1
);
9437 if (!tree_expr_maybe_nan_p (arg0
) && !tree_expr_maybe_nan_p (arg1
))
9438 return omit_two_operands_loc (loc
, type
, integer_zero_node
, arg0
, arg1
);
9439 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg0
, arg1
);
9442 code
= (tree_expr_maybe_nan_p (arg0
) || tree_expr_maybe_nan_p (arg1
))
9443 ? unordered_code
: ordered_code
;
9444 return fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
,
9445 fold_build2_loc (loc
, code
, type
, arg0
, arg1
));
9448 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9449 arithmetics if it can never overflow, or into internal functions that
9450 return both result of arithmetics and overflowed boolean flag in
9451 a complex integer result, or some other check for overflow.
9452 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
9453 checking part of that. */
9456 fold_builtin_arith_overflow (location_t loc
, enum built_in_function fcode
,
9457 tree arg0
, tree arg1
, tree arg2
)
9459 enum internal_fn ifn
= IFN_LAST
;
9460 /* The code of the expression corresponding to the built-in. */
9461 enum tree_code opcode
= ERROR_MARK
;
9462 bool ovf_only
= false;
9466 case BUILT_IN_ADD_OVERFLOW_P
:
9469 case BUILT_IN_ADD_OVERFLOW
:
9470 case BUILT_IN_SADD_OVERFLOW
:
9471 case BUILT_IN_SADDL_OVERFLOW
:
9472 case BUILT_IN_SADDLL_OVERFLOW
:
9473 case BUILT_IN_UADD_OVERFLOW
:
9474 case BUILT_IN_UADDL_OVERFLOW
:
9475 case BUILT_IN_UADDLL_OVERFLOW
:
9477 ifn
= IFN_ADD_OVERFLOW
;
9479 case BUILT_IN_SUB_OVERFLOW_P
:
9482 case BUILT_IN_SUB_OVERFLOW
:
9483 case BUILT_IN_SSUB_OVERFLOW
:
9484 case BUILT_IN_SSUBL_OVERFLOW
:
9485 case BUILT_IN_SSUBLL_OVERFLOW
:
9486 case BUILT_IN_USUB_OVERFLOW
:
9487 case BUILT_IN_USUBL_OVERFLOW
:
9488 case BUILT_IN_USUBLL_OVERFLOW
:
9489 opcode
= MINUS_EXPR
;
9490 ifn
= IFN_SUB_OVERFLOW
;
9492 case BUILT_IN_MUL_OVERFLOW_P
:
9495 case BUILT_IN_MUL_OVERFLOW
:
9496 case BUILT_IN_SMUL_OVERFLOW
:
9497 case BUILT_IN_SMULL_OVERFLOW
:
9498 case BUILT_IN_SMULLL_OVERFLOW
:
9499 case BUILT_IN_UMUL_OVERFLOW
:
9500 case BUILT_IN_UMULL_OVERFLOW
:
9501 case BUILT_IN_UMULLL_OVERFLOW
:
9503 ifn
= IFN_MUL_OVERFLOW
;
9509 /* For the "generic" overloads, the first two arguments can have different
9510 types and the last argument determines the target type to use to check
9511 for overflow. The arguments of the other overloads all have the same
9513 tree type
= ovf_only
? TREE_TYPE (arg2
) : TREE_TYPE (TREE_TYPE (arg2
));
9515 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
9516 arguments are constant, attempt to fold the built-in call into a constant
9517 expression indicating whether or not it detected an overflow. */
9519 && TREE_CODE (arg0
) == INTEGER_CST
9520 && TREE_CODE (arg1
) == INTEGER_CST
)
9521 /* Perform the computation in the target type and check for overflow. */
9522 return omit_one_operand_loc (loc
, boolean_type_node
,
9523 arith_overflowed_p (opcode
, type
, arg0
, arg1
)
9524 ? boolean_true_node
: boolean_false_node
,
9527 tree intres
, ovfres
;
9528 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
9530 intres
= fold_binary_loc (loc
, opcode
, type
,
9531 fold_convert_loc (loc
, type
, arg0
),
9532 fold_convert_loc (loc
, type
, arg1
));
9533 if (TREE_OVERFLOW (intres
))
9534 intres
= drop_tree_overflow (intres
);
9535 ovfres
= (arith_overflowed_p (opcode
, type
, arg0
, arg1
)
9536 ? boolean_true_node
: boolean_false_node
);
9540 tree ctype
= build_complex_type (type
);
9541 tree call
= build_call_expr_internal_loc (loc
, ifn
, ctype
, 2,
9543 tree tgt
= save_expr (call
);
9544 intres
= build1_loc (loc
, REALPART_EXPR
, type
, tgt
);
9545 ovfres
= build1_loc (loc
, IMAGPART_EXPR
, type
, tgt
);
9546 ovfres
= fold_convert_loc (loc
, boolean_type_node
, ovfres
);
9550 return omit_one_operand_loc (loc
, boolean_type_node
, ovfres
, arg2
);
9552 tree mem_arg2
= build_fold_indirect_ref_loc (loc
, arg2
);
9554 = fold_build2_loc (loc
, MODIFY_EXPR
, void_type_node
, mem_arg2
, intres
);
9555 return build2_loc (loc
, COMPOUND_EXPR
, boolean_type_node
, store
, ovfres
);
9558 /* Fold a call to __builtin_FILE to a constant string. */
9561 fold_builtin_FILE (location_t loc
)
9563 if (const char *fname
= LOCATION_FILE (loc
))
9565 /* The documentation says this builtin is equivalent to the preprocessor
9566 __FILE__ macro so it appears appropriate to use the same file prefix
9568 fname
= remap_macro_filename (fname
);
9569 return build_string_literal (fname
);
9572 return build_string_literal ("");
9575 /* Fold a call to __builtin_FUNCTION to a constant string. */
9578 fold_builtin_FUNCTION ()
9580 const char *name
= "";
9582 if (current_function_decl
)
9583 name
= lang_hooks
.decl_printable_name (current_function_decl
, 0);
9585 return build_string_literal (name
);
9588 /* Fold a call to __builtin_LINE to an integer constant. */
9591 fold_builtin_LINE (location_t loc
, tree type
)
9593 return build_int_cst (type
, LOCATION_LINE (loc
));
9596 /* Fold a call to built-in function FNDECL with 0 arguments.
9597 This function returns NULL_TREE if no simplification was possible. */
9600 fold_builtin_0 (location_t loc
, tree fndecl
)
9602 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9603 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9607 return fold_builtin_FILE (loc
);
9609 case BUILT_IN_FUNCTION
:
9610 return fold_builtin_FUNCTION ();
9613 return fold_builtin_LINE (loc
, type
);
9615 CASE_FLT_FN (BUILT_IN_INF
):
9616 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF
):
9617 case BUILT_IN_INFD32
:
9618 case BUILT_IN_INFD64
:
9619 case BUILT_IN_INFD128
:
9620 return fold_builtin_inf (loc
, type
, true);
9622 CASE_FLT_FN (BUILT_IN_HUGE_VAL
):
9623 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL
):
9624 return fold_builtin_inf (loc
, type
, false);
9626 case BUILT_IN_CLASSIFY_TYPE
:
9627 return fold_builtin_classify_type (NULL_TREE
);
9629 case BUILT_IN_UNREACHABLE
:
9630 /* Rewrite any explicit calls to __builtin_unreachable. */
9631 if (sanitize_flags_p (SANITIZE_UNREACHABLE
))
9632 return build_builtin_unreachable (loc
);
9641 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9642 This function returns NULL_TREE if no simplification was possible. */
9645 fold_builtin_1 (location_t loc
, tree expr
, tree fndecl
, tree arg0
)
9647 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9648 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9650 if (TREE_CODE (arg0
) == ERROR_MARK
)
9653 if (tree ret
= fold_const_call (as_combined_fn (fcode
), type
, arg0
))
9658 case BUILT_IN_CONSTANT_P
:
9660 tree val
= fold_builtin_constant_p (arg0
);
9662 /* Gimplification will pull the CALL_EXPR for the builtin out of
9663 an if condition. When not optimizing, we'll not CSE it back.
9664 To avoid link error types of regressions, return false now. */
9665 if (!val
&& !optimize
)
9666 val
= integer_zero_node
;
9671 case BUILT_IN_CLASSIFY_TYPE
:
9672 return fold_builtin_classify_type (arg0
);
9674 case BUILT_IN_STRLEN
:
9675 return fold_builtin_strlen (loc
, expr
, type
, arg0
);
9677 CASE_FLT_FN (BUILT_IN_FABS
):
9678 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS
):
9679 case BUILT_IN_FABSD32
:
9680 case BUILT_IN_FABSD64
:
9681 case BUILT_IN_FABSD128
:
9682 return fold_builtin_fabs (loc
, arg0
, type
);
9686 case BUILT_IN_LLABS
:
9687 case BUILT_IN_IMAXABS
:
9688 return fold_builtin_abs (loc
, arg0
, type
);
9690 CASE_FLT_FN (BUILT_IN_CONJ
):
9691 if (validate_arg (arg0
, COMPLEX_TYPE
)
9692 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9693 return fold_build1_loc (loc
, CONJ_EXPR
, type
, arg0
);
9696 CASE_FLT_FN (BUILT_IN_CREAL
):
9697 if (validate_arg (arg0
, COMPLEX_TYPE
)
9698 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9699 return non_lvalue_loc (loc
, fold_build1_loc (loc
, REALPART_EXPR
, type
, arg0
));
9702 CASE_FLT_FN (BUILT_IN_CIMAG
):
9703 if (validate_arg (arg0
, COMPLEX_TYPE
)
9704 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
9705 return non_lvalue_loc (loc
, fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg0
));
9708 CASE_FLT_FN (BUILT_IN_CARG
):
9709 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CARG
):
9710 return fold_builtin_carg (loc
, arg0
, type
);
9712 case BUILT_IN_ISASCII
:
9713 return fold_builtin_isascii (loc
, arg0
);
9715 case BUILT_IN_TOASCII
:
9716 return fold_builtin_toascii (loc
, arg0
);
9718 case BUILT_IN_ISDIGIT
:
9719 return fold_builtin_isdigit (loc
, arg0
);
9721 CASE_FLT_FN (BUILT_IN_FINITE
):
9722 case BUILT_IN_FINITED32
:
9723 case BUILT_IN_FINITED64
:
9724 case BUILT_IN_FINITED128
:
9725 case BUILT_IN_ISFINITE
:
9727 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISFINITE
);
9730 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
9733 CASE_FLT_FN (BUILT_IN_ISINF
):
9734 case BUILT_IN_ISINFD32
:
9735 case BUILT_IN_ISINFD64
:
9736 case BUILT_IN_ISINFD128
:
9738 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF
);
9741 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
9744 case BUILT_IN_ISNORMAL
:
9745 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
9747 case BUILT_IN_ISINF_SIGN
:
9748 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF_SIGN
);
9750 CASE_FLT_FN (BUILT_IN_ISNAN
):
9751 case BUILT_IN_ISNAND32
:
9752 case BUILT_IN_ISNAND64
:
9753 case BUILT_IN_ISNAND128
:
9754 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISNAN
);
9756 case BUILT_IN_ISSIGNALING
:
9757 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISSIGNALING
);
9760 if (integer_zerop (arg0
))
9761 return build_empty_stmt (loc
);
9772 /* Folds a call EXPR (which may be null) to built-in function FNDECL
9773 with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
9774 if no simplification was possible. */
9777 fold_builtin_2 (location_t loc
, tree expr
, tree fndecl
, tree arg0
, tree arg1
)
9779 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9780 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9782 if (TREE_CODE (arg0
) == ERROR_MARK
9783 || TREE_CODE (arg1
) == ERROR_MARK
)
9786 if (tree ret
= fold_const_call (as_combined_fn (fcode
), type
, arg0
, arg1
))
9791 CASE_FLT_FN_REENT (BUILT_IN_GAMMA
): /* GAMMA_R */
9792 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA
): /* LGAMMA_R */
9793 if (validate_arg (arg0
, REAL_TYPE
)
9794 && validate_arg (arg1
, POINTER_TYPE
))
9795 return do_mpfr_lgamma_r (arg0
, arg1
, type
);
9798 CASE_FLT_FN (BUILT_IN_FREXP
):
9799 return fold_builtin_frexp (loc
, arg0
, arg1
, type
);
9801 CASE_FLT_FN (BUILT_IN_MODF
):
9802 return fold_builtin_modf (loc
, arg0
, arg1
, type
);
9804 case BUILT_IN_STRSPN
:
9805 return fold_builtin_strspn (loc
, expr
, arg0
, arg1
);
9807 case BUILT_IN_STRCSPN
:
9808 return fold_builtin_strcspn (loc
, expr
, arg0
, arg1
);
9810 case BUILT_IN_STRPBRK
:
9811 return fold_builtin_strpbrk (loc
, expr
, arg0
, arg1
, type
);
9813 case BUILT_IN_EXPECT
:
9814 return fold_builtin_expect (loc
, arg0
, arg1
, NULL_TREE
, NULL_TREE
);
9816 case BUILT_IN_ISGREATER
:
9817 return fold_builtin_unordered_cmp (loc
, fndecl
,
9818 arg0
, arg1
, UNLE_EXPR
, LE_EXPR
);
9819 case BUILT_IN_ISGREATEREQUAL
:
9820 return fold_builtin_unordered_cmp (loc
, fndecl
,
9821 arg0
, arg1
, UNLT_EXPR
, LT_EXPR
);
9822 case BUILT_IN_ISLESS
:
9823 return fold_builtin_unordered_cmp (loc
, fndecl
,
9824 arg0
, arg1
, UNGE_EXPR
, GE_EXPR
);
9825 case BUILT_IN_ISLESSEQUAL
:
9826 return fold_builtin_unordered_cmp (loc
, fndecl
,
9827 arg0
, arg1
, UNGT_EXPR
, GT_EXPR
);
9828 case BUILT_IN_ISLESSGREATER
:
9829 return fold_builtin_unordered_cmp (loc
, fndecl
,
9830 arg0
, arg1
, UNEQ_EXPR
, EQ_EXPR
);
9831 case BUILT_IN_ISUNORDERED
:
9832 return fold_builtin_unordered_cmp (loc
, fndecl
,
9833 arg0
, arg1
, UNORDERED_EXPR
,
9836 /* We do the folding for va_start in the expander. */
9837 case BUILT_IN_VA_START
:
9840 case BUILT_IN_OBJECT_SIZE
:
9841 case BUILT_IN_DYNAMIC_OBJECT_SIZE
:
9842 return fold_builtin_object_size (arg0
, arg1
, fcode
);
9844 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
9845 return fold_builtin_atomic_always_lock_free (arg0
, arg1
);
9847 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
9848 return fold_builtin_atomic_is_lock_free (arg0
, arg1
);
9856 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
9858 This function returns NULL_TREE if no simplification was possible. */
9861 fold_builtin_3 (location_t loc
, tree fndecl
,
9862 tree arg0
, tree arg1
, tree arg2
)
9864 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
9865 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9867 if (TREE_CODE (arg0
) == ERROR_MARK
9868 || TREE_CODE (arg1
) == ERROR_MARK
9869 || TREE_CODE (arg2
) == ERROR_MARK
)
9872 if (tree ret
= fold_const_call (as_combined_fn (fcode
), type
,
9879 CASE_FLT_FN (BUILT_IN_SINCOS
):
9880 return fold_builtin_sincos (loc
, arg0
, arg1
, arg2
);
9882 CASE_FLT_FN (BUILT_IN_REMQUO
):
9883 if (validate_arg (arg0
, REAL_TYPE
)
9884 && validate_arg (arg1
, REAL_TYPE
)
9885 && validate_arg (arg2
, POINTER_TYPE
))
9886 return do_mpfr_remquo (arg0
, arg1
, arg2
);
9889 case BUILT_IN_MEMCMP
:
9890 return fold_builtin_memcmp (loc
, arg0
, arg1
, arg2
);
9892 case BUILT_IN_EXPECT
:
9893 return fold_builtin_expect (loc
, arg0
, arg1
, arg2
, NULL_TREE
);
9895 case BUILT_IN_EXPECT_WITH_PROBABILITY
:
9896 return fold_builtin_expect (loc
, arg0
, arg1
, NULL_TREE
, arg2
);
9898 case BUILT_IN_ADD_OVERFLOW
:
9899 case BUILT_IN_SUB_OVERFLOW
:
9900 case BUILT_IN_MUL_OVERFLOW
:
9901 case BUILT_IN_ADD_OVERFLOW_P
:
9902 case BUILT_IN_SUB_OVERFLOW_P
:
9903 case BUILT_IN_MUL_OVERFLOW_P
:
9904 case BUILT_IN_SADD_OVERFLOW
:
9905 case BUILT_IN_SADDL_OVERFLOW
:
9906 case BUILT_IN_SADDLL_OVERFLOW
:
9907 case BUILT_IN_SSUB_OVERFLOW
:
9908 case BUILT_IN_SSUBL_OVERFLOW
:
9909 case BUILT_IN_SSUBLL_OVERFLOW
:
9910 case BUILT_IN_SMUL_OVERFLOW
:
9911 case BUILT_IN_SMULL_OVERFLOW
:
9912 case BUILT_IN_SMULLL_OVERFLOW
:
9913 case BUILT_IN_UADD_OVERFLOW
:
9914 case BUILT_IN_UADDL_OVERFLOW
:
9915 case BUILT_IN_UADDLL_OVERFLOW
:
9916 case BUILT_IN_USUB_OVERFLOW
:
9917 case BUILT_IN_USUBL_OVERFLOW
:
9918 case BUILT_IN_USUBLL_OVERFLOW
:
9919 case BUILT_IN_UMUL_OVERFLOW
:
9920 case BUILT_IN_UMULL_OVERFLOW
:
9921 case BUILT_IN_UMULLL_OVERFLOW
:
9922 return fold_builtin_arith_overflow (loc
, fcode
, arg0
, arg1
, arg2
);
9930 /* Folds a call EXPR (which may be null) to built-in function FNDECL.
9931 ARGS is an array of NARGS arguments. IGNORE is true if the result
9932 of the function call is ignored. This function returns NULL_TREE
9933 if no simplification was possible. */
9936 fold_builtin_n (location_t loc
, tree expr
, tree fndecl
, tree
*args
,
9939 tree ret
= NULL_TREE
;
9944 ret
= fold_builtin_0 (loc
, fndecl
);
9947 ret
= fold_builtin_1 (loc
, expr
, fndecl
, args
[0]);
9950 ret
= fold_builtin_2 (loc
, expr
, fndecl
, args
[0], args
[1]);
9953 ret
= fold_builtin_3 (loc
, fndecl
, args
[0], args
[1], args
[2]);
9956 ret
= fold_builtin_varargs (loc
, fndecl
, args
, nargs
);
9961 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
9962 SET_EXPR_LOCATION (ret
, loc
);
9968 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
9969 list ARGS along with N new arguments in NEWARGS. SKIP is the number
9970 of arguments in ARGS to be omitted. OLDNARGS is the number of
9971 elements in ARGS. */
9974 rewrite_call_expr_valist (location_t loc
, int oldnargs
, tree
*args
,
9975 int skip
, tree fndecl
, int n
, va_list newargs
)
9977 int nargs
= oldnargs
- skip
+ n
;
9984 buffer
= XALLOCAVEC (tree
, nargs
);
9985 for (i
= 0; i
< n
; i
++)
9986 buffer
[i
] = va_arg (newargs
, tree
);
9987 for (j
= skip
; j
< oldnargs
; j
++, i
++)
9988 buffer
[i
] = args
[j
];
9991 buffer
= args
+ skip
;
9993 return build_call_expr_loc_array (loc
, fndecl
, nargs
, buffer
);
9996 /* Return true if FNDECL shouldn't be folded right now.
9997 If a built-in function has an inline attribute always_inline
9998 wrapper, defer folding it after always_inline functions have
9999 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10000 might not be performed. */
10003 avoid_folding_inline_builtin (tree fndecl
)
10005 return (DECL_DECLARED_INLINE_P (fndecl
)
10006 && DECL_DISREGARD_INLINE_LIMITS (fndecl
)
10008 && !cfun
->always_inline_functions_inlined
10009 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl
)));
10012 /* A wrapper function for builtin folding that prevents warnings for
10013 "statement without effect" and the like, caused by removing the
10014 call node earlier than the warning is generated. */
10017 fold_call_expr (location_t loc
, tree exp
, bool ignore
)
10019 tree ret
= NULL_TREE
;
10020 tree fndecl
= get_callee_fndecl (exp
);
10021 if (fndecl
&& fndecl_built_in_p (fndecl
)
10022 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10023 yet. Defer folding until we see all the arguments
10024 (after inlining). */
10025 && !CALL_EXPR_VA_ARG_PACK (exp
))
10027 int nargs
= call_expr_nargs (exp
);
10029 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10030 instead last argument is __builtin_va_arg_pack (). Defer folding
10031 even in that case, until arguments are finalized. */
10032 if (nargs
&& TREE_CODE (CALL_EXPR_ARG (exp
, nargs
- 1)) == CALL_EXPR
)
10034 tree fndecl2
= get_callee_fndecl (CALL_EXPR_ARG (exp
, nargs
- 1));
10035 if (fndecl2
&& fndecl_built_in_p (fndecl2
, BUILT_IN_VA_ARG_PACK
))
10039 if (avoid_folding_inline_builtin (fndecl
))
10042 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
10043 return targetm
.fold_builtin (fndecl
, call_expr_nargs (exp
),
10044 CALL_EXPR_ARGP (exp
), ignore
);
10047 tree
*args
= CALL_EXPR_ARGP (exp
);
10048 ret
= fold_builtin_n (loc
, exp
, fndecl
, args
, nargs
, ignore
);
10056 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
10057 N arguments are passed in the array ARGARRAY. Return a folded
10058 expression or NULL_TREE if no simplification was possible. */
10061 fold_builtin_call_array (location_t loc
, tree
,
10066 if (TREE_CODE (fn
) != ADDR_EXPR
)
10069 tree fndecl
= TREE_OPERAND (fn
, 0);
10070 if (TREE_CODE (fndecl
) == FUNCTION_DECL
10071 && fndecl_built_in_p (fndecl
))
10073 /* If last argument is __builtin_va_arg_pack (), arguments to this
10074 function are not finalized yet. Defer folding until they are. */
10075 if (n
&& TREE_CODE (argarray
[n
- 1]) == CALL_EXPR
)
10077 tree fndecl2
= get_callee_fndecl (argarray
[n
- 1]);
10078 if (fndecl2
&& fndecl_built_in_p (fndecl2
, BUILT_IN_VA_ARG_PACK
))
10081 if (avoid_folding_inline_builtin (fndecl
))
10083 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
10084 return targetm
.fold_builtin (fndecl
, n
, argarray
, false);
10086 return fold_builtin_n (loc
, NULL_TREE
, fndecl
, argarray
, n
, false);
10092 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10093 along with N new arguments specified as the "..." parameters. SKIP
10094 is the number of arguments in EXP to be omitted. This function is used
10095 to do varargs-to-varargs transformations. */
10098 rewrite_call_expr (location_t loc
, tree exp
, int skip
, tree fndecl
, int n
, ...)
10104 t
= rewrite_call_expr_valist (loc
, call_expr_nargs (exp
),
10105 CALL_EXPR_ARGP (exp
), skip
, fndecl
, n
, ap
);
10111 /* Validate a single argument ARG against a tree code CODE representing
10112 a type. Return true when argument is valid. */
10115 validate_arg (const_tree arg
, enum tree_code code
)
10119 else if (code
== POINTER_TYPE
)
10120 return POINTER_TYPE_P (TREE_TYPE (arg
));
10121 else if (code
== INTEGER_TYPE
)
10122 return INTEGRAL_TYPE_P (TREE_TYPE (arg
));
10123 return code
== TREE_CODE (TREE_TYPE (arg
));
10126 /* This function validates the types of a function call argument list
10127 against a specified list of tree_codes. If the last specifier is a 0,
10128 that represents an ellipses, otherwise the last specifier must be a
10131 This is the GIMPLE version of validate_arglist. Eventually we want to
10132 completely convert builtins.cc to work from GIMPLEs and the tree based
10133 validate_arglist will then be removed. */
10136 validate_gimple_arglist (const gcall
*call
, ...)
10138 enum tree_code code
;
10144 va_start (ap
, call
);
10149 code
= (enum tree_code
) va_arg (ap
, int);
10153 /* This signifies an ellipses, any further arguments are all ok. */
10157 /* This signifies an endlink, if no arguments remain, return
10158 true, otherwise return false. */
10159 res
= (i
== gimple_call_num_args (call
));
10162 /* If no parameters remain or the parameter's code does not
10163 match the specified code, return false. Otherwise continue
10164 checking any remaining arguments. */
10165 arg
= gimple_call_arg (call
, i
++);
10166 if (!validate_arg (arg
, code
))
10173 /* We need gotos here since we can only have one VA_CLOSE in a
10181 /* Default target-specific builtin expander that does nothing. */
10184 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
10185 rtx target ATTRIBUTE_UNUSED
,
10186 rtx subtarget ATTRIBUTE_UNUSED
,
10187 machine_mode mode ATTRIBUTE_UNUSED
,
10188 int ignore ATTRIBUTE_UNUSED
)
10193 /* Returns true is EXP represents data that would potentially reside
10194 in a readonly section. */
10197 readonly_data_expr (tree exp
)
10201 if (TREE_CODE (exp
) != ADDR_EXPR
)
10204 exp
= get_base_address (TREE_OPERAND (exp
, 0));
10208 /* Make sure we call decl_readonly_section only for trees it
10209 can handle (since it returns true for everything it doesn't
10211 if (TREE_CODE (exp
) == STRING_CST
10212 || TREE_CODE (exp
) == CONSTRUCTOR
10213 || (VAR_P (exp
) && TREE_STATIC (exp
)))
10214 return decl_readonly_section (exp
, 0);
10219 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
10220 to the call, and TYPE is its return type.
10222 Return NULL_TREE if no simplification was possible, otherwise return the
10223 simplified form of the call as a tree.
10225 The simplified form may be a constant or other expression which
10226 computes the same value, but in a more efficient manner (including
10227 calls to other builtin functions).
10229 The call may contain arguments which need to be evaluated, but
10230 which are not useful to determine the result of the call. In
10231 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10232 COMPOUND_EXPR will be an argument which must be evaluated.
10233 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10234 COMPOUND_EXPR in the chain will contain the tree for the simplified
10235 form of the builtin function call. */
10238 fold_builtin_strpbrk (location_t loc
, tree
, tree s1
, tree s2
, tree type
)
10240 if (!validate_arg (s1
, POINTER_TYPE
)
10241 || !validate_arg (s2
, POINTER_TYPE
))
10245 const char *p1
, *p2
;
10247 p2
= c_getstr (s2
);
10251 p1
= c_getstr (s1
);
10254 const char *r
= strpbrk (p1
, p2
);
10258 return build_int_cst (TREE_TYPE (s1
), 0);
10260 /* Return an offset into the constant string argument. */
10261 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
10262 return fold_convert_loc (loc
, type
, tem
);
10266 /* strpbrk(x, "") == NULL.
10267 Evaluate and ignore s1 in case it had side-effects. */
10268 return omit_one_operand_loc (loc
, type
, integer_zero_node
, s1
);
10271 return NULL_TREE
; /* Really call strpbrk. */
10273 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
10277 /* New argument list transforming strpbrk(s1, s2) to
10278 strchr(s1, s2[0]). */
10279 return build_call_expr_loc (loc
, fn
, 2, s1
,
10280 build_int_cst (integer_type_node
, p2
[0]));
10283 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
10286 Return NULL_TREE if no simplification was possible, otherwise return the
10287 simplified form of the call as a tree.
10289 The simplified form may be a constant or other expression which
10290 computes the same value, but in a more efficient manner (including
10291 calls to other builtin functions).
10293 The call may contain arguments which need to be evaluated, but
10294 which are not useful to determine the result of the call. In
10295 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10296 COMPOUND_EXPR will be an argument which must be evaluated.
10297 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10298 COMPOUND_EXPR in the chain will contain the tree for the simplified
10299 form of the builtin function call. */
10302 fold_builtin_strspn (location_t loc
, tree expr
, tree s1
, tree s2
)
10304 if (!validate_arg (s1
, POINTER_TYPE
)
10305 || !validate_arg (s2
, POINTER_TYPE
))
10308 if (!check_nul_terminated_array (expr
, s1
)
10309 || !check_nul_terminated_array (expr
, s2
))
10312 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
10314 /* If either argument is "", return NULL_TREE. */
10315 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
10316 /* Evaluate and ignore both arguments in case either one has
10318 return omit_two_operands_loc (loc
, size_type_node
, size_zero_node
,
10323 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
10326 Return NULL_TREE if no simplification was possible, otherwise return the
10327 simplified form of the call as a tree.
10329 The simplified form may be a constant or other expression which
10330 computes the same value, but in a more efficient manner (including
10331 calls to other builtin functions).
10333 The call may contain arguments which need to be evaluated, but
10334 which are not useful to determine the result of the call. In
10335 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10336 COMPOUND_EXPR will be an argument which must be evaluated.
10337 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10338 COMPOUND_EXPR in the chain will contain the tree for the simplified
10339 form of the builtin function call. */
10342 fold_builtin_strcspn (location_t loc
, tree expr
, tree s1
, tree s2
)
10344 if (!validate_arg (s1
, POINTER_TYPE
)
10345 || !validate_arg (s2
, POINTER_TYPE
))
10348 if (!check_nul_terminated_array (expr
, s1
)
10349 || !check_nul_terminated_array (expr
, s2
))
10352 /* If the first argument is "", return NULL_TREE. */
10353 const char *p1
= c_getstr (s1
);
10354 if (p1
&& *p1
== '\0')
10356 /* Evaluate and ignore argument s2 in case it has
10358 return omit_one_operand_loc (loc
, size_type_node
,
10359 size_zero_node
, s2
);
10362 /* If the second argument is "", return __builtin_strlen(s1). */
10363 const char *p2
= c_getstr (s2
);
10364 if (p2
&& *p2
== '\0')
10366 tree fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
10368 /* If the replacement _DECL isn't initialized, don't do the
10373 return build_call_expr_loc (loc
, fn
, 1, s1
);
10378 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
10379 produced. False otherwise. This is done so that we don't output the error
10380 or warning twice or three times. */
10383 fold_builtin_next_arg (tree exp
, bool va_start_p
)
10385 tree fntype
= TREE_TYPE (current_function_decl
);
10386 int nargs
= call_expr_nargs (exp
);
10388 /* There is good chance the current input_location points inside the
10389 definition of the va_start macro (perhaps on the token for
10390 builtin) in a system header, so warnings will not be emitted.
10391 Use the location in real source code. */
10392 location_t current_location
=
10393 linemap_unwind_to_first_non_reserved_loc (line_table
, input_location
,
10396 if (!stdarg_p (fntype
))
10398 error ("%<va_start%> used in function with fixed arguments");
10404 if (va_start_p
&& (nargs
!= 2))
10406 error ("wrong number of arguments to function %<va_start%>");
10409 arg
= CALL_EXPR_ARG (exp
, 1);
10411 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10412 when we checked the arguments and if needed issued a warning. */
10417 /* Evidently an out of date version of <stdarg.h>; can't validate
10418 va_start's second argument, but can still work as intended. */
10419 warning_at (current_location
,
10421 "%<__builtin_next_arg%> called without an argument");
10424 else if (nargs
> 1)
10426 error ("wrong number of arguments to function %<__builtin_next_arg%>");
10429 arg
= CALL_EXPR_ARG (exp
, 0);
10432 if (TREE_CODE (arg
) == SSA_NAME
10433 && SSA_NAME_VAR (arg
))
10434 arg
= SSA_NAME_VAR (arg
);
10436 /* We destructively modify the call to be __builtin_va_start (ap, 0)
10437 or __builtin_next_arg (0) the first time we see it, after checking
10438 the arguments and if needed issuing a warning. */
10439 if (!integer_zerop (arg
))
10441 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
10443 /* Strip off all nops for the sake of the comparison. This
10444 is not quite the same as STRIP_NOPS. It does more.
10445 We must also strip off INDIRECT_EXPR for C++ reference
10447 while (CONVERT_EXPR_P (arg
)
10448 || INDIRECT_REF_P (arg
))
10449 arg
= TREE_OPERAND (arg
, 0);
10450 if (arg
!= last_parm
)
10452 /* FIXME: Sometimes with the tree optimizers we can get the
10453 not the last argument even though the user used the last
10454 argument. We just warn and set the arg to be the last
10455 argument so that we will get wrong-code because of
10457 warning_at (current_location
,
10459 "second parameter of %<va_start%> not last named argument");
10462 /* Undefined by C99 7.15.1.4p4 (va_start):
10463 "If the parameter parmN is declared with the register storage
10464 class, with a function or array type, or with a type that is
10465 not compatible with the type that results after application of
10466 the default argument promotions, the behavior is undefined."
10468 else if (DECL_REGISTER (arg
))
10470 warning_at (current_location
,
10472 "undefined behavior when second parameter of "
10473 "%<va_start%> is declared with %<register%> storage");
10476 /* We want to verify the second parameter just once before the tree
10477 optimizers are run and then avoid keeping it in the tree,
10478 as otherwise we could warn even for correct code like:
10479 void foo (int i, ...)
10480 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10482 CALL_EXPR_ARG (exp
, 1) = integer_zero_node
;
10484 CALL_EXPR_ARG (exp
, 0) = integer_zero_node
;
10490 /* Expand a call EXP to __builtin_object_size. */
10493 expand_builtin_object_size (tree exp
)
10496 int object_size_type
;
10497 tree fndecl
= get_callee_fndecl (exp
);
10499 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10501 error ("first argument of %qD must be a pointer, second integer constant",
10503 expand_builtin_trap ();
10507 ost
= CALL_EXPR_ARG (exp
, 1);
10510 if (TREE_CODE (ost
) != INTEGER_CST
10511 || tree_int_cst_sgn (ost
) < 0
10512 || compare_tree_int (ost
, 3) > 0)
10514 error ("last argument of %qD is not integer constant between 0 and 3",
10516 expand_builtin_trap ();
10520 object_size_type
= tree_to_shwi (ost
);
10522 return object_size_type
< 2 ? constm1_rtx
: const0_rtx
;
10525 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10526 FCODE is the BUILT_IN_* to use.
10527 Return NULL_RTX if we failed; the caller should emit a normal call,
10528 otherwise try to get the result in TARGET, if convenient (and in
10529 mode MODE if that's convenient). */
10532 expand_builtin_memory_chk (tree exp
, rtx target
, machine_mode mode
,
10533 enum built_in_function fcode
)
10535 if (!validate_arglist (exp
,
10537 fcode
== BUILT_IN_MEMSET_CHK
10538 ? INTEGER_TYPE
: POINTER_TYPE
,
10539 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
10542 tree dest
= CALL_EXPR_ARG (exp
, 0);
10543 tree src
= CALL_EXPR_ARG (exp
, 1);
10544 tree len
= CALL_EXPR_ARG (exp
, 2);
10545 tree size
= CALL_EXPR_ARG (exp
, 3);
10547 /* FIXME: Set access mode to write only for memset et al. */
10548 bool sizes_ok
= check_access (exp
, len
, /*maxread=*/NULL_TREE
,
10549 /*srcstr=*/NULL_TREE
, size
, access_read_write
);
10551 if (!tree_fits_uhwi_p (size
))
10554 if (tree_fits_uhwi_p (len
) || integer_all_onesp (size
))
10556 /* Avoid transforming the checking call to an ordinary one when
10557 an overflow has been detected or when the call couldn't be
10558 validated because the size is not constant. */
10559 if (!sizes_ok
&& !integer_all_onesp (size
) && tree_int_cst_lt (size
, len
))
10562 tree fn
= NULL_TREE
;
10563 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10564 mem{cpy,pcpy,move,set} is available. */
10567 case BUILT_IN_MEMCPY_CHK
:
10568 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
10570 case BUILT_IN_MEMPCPY_CHK
:
10571 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
10573 case BUILT_IN_MEMMOVE_CHK
:
10574 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
10576 case BUILT_IN_MEMSET_CHK
:
10577 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
10586 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 3, dest
, src
, len
);
10587 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
10588 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
10589 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
10591 else if (fcode
== BUILT_IN_MEMSET_CHK
)
10595 unsigned int dest_align
= get_pointer_alignment (dest
);
10597 /* If DEST is not a pointer type, call the normal function. */
10598 if (dest_align
== 0)
10601 /* If SRC and DEST are the same (and not volatile), do nothing. */
10602 if (operand_equal_p (src
, dest
, 0))
10606 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
10608 /* Evaluate and ignore LEN in case it has side-effects. */
10609 expand_expr (len
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
10610 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
10613 expr
= fold_build_pointer_plus (dest
, len
);
10614 return expand_expr (expr
, target
, mode
, EXPAND_NORMAL
);
10617 /* __memmove_chk special case. */
10618 if (fcode
== BUILT_IN_MEMMOVE_CHK
)
10620 unsigned int src_align
= get_pointer_alignment (src
);
10622 if (src_align
== 0)
10625 /* If src is categorized for a readonly section we can use
10626 normal __memcpy_chk. */
10627 if (readonly_data_expr (src
))
10629 tree fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
10632 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 4,
10633 dest
, src
, len
, size
);
10634 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
10635 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
10636 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
10643 /* Emit warning if a buffer overflow is detected at compile time. */
10646 maybe_emit_chk_warning (tree exp
, enum built_in_function fcode
)
10648 /* The source string. */
10649 tree srcstr
= NULL_TREE
;
10650 /* The size of the destination object returned by __builtin_object_size. */
10651 tree objsize
= NULL_TREE
;
10652 /* The string that is being concatenated with (as in __strcat_chk)
10653 or null if it isn't. */
10654 tree catstr
= NULL_TREE
;
10655 /* The maximum length of the source sequence in a bounded operation
10656 (such as __strncat_chk) or null if the operation isn't bounded
10657 (such as __strcat_chk). */
10658 tree maxread
= NULL_TREE
;
10659 /* The exact size of the access (such as in __strncpy_chk). */
10660 tree size
= NULL_TREE
;
10661 /* The access by the function that's checked. Except for snprintf
10662 both writing and reading is checked. */
10663 access_mode mode
= access_read_write
;
10667 case BUILT_IN_STRCPY_CHK
:
10668 case BUILT_IN_STPCPY_CHK
:
10669 srcstr
= CALL_EXPR_ARG (exp
, 1);
10670 objsize
= CALL_EXPR_ARG (exp
, 2);
10673 case BUILT_IN_STRCAT_CHK
:
10674 /* For __strcat_chk the warning will be emitted only if overflowing
10675 by at least strlen (dest) + 1 bytes. */
10676 catstr
= CALL_EXPR_ARG (exp
, 0);
10677 srcstr
= CALL_EXPR_ARG (exp
, 1);
10678 objsize
= CALL_EXPR_ARG (exp
, 2);
10681 case BUILT_IN_STRNCAT_CHK
:
10682 catstr
= CALL_EXPR_ARG (exp
, 0);
10683 srcstr
= CALL_EXPR_ARG (exp
, 1);
10684 maxread
= CALL_EXPR_ARG (exp
, 2);
10685 objsize
= CALL_EXPR_ARG (exp
, 3);
10688 case BUILT_IN_STRNCPY_CHK
:
10689 case BUILT_IN_STPNCPY_CHK
:
10690 srcstr
= CALL_EXPR_ARG (exp
, 1);
10691 size
= CALL_EXPR_ARG (exp
, 2);
10692 objsize
= CALL_EXPR_ARG (exp
, 3);
10695 case BUILT_IN_SNPRINTF_CHK
:
10696 case BUILT_IN_VSNPRINTF_CHK
:
10697 maxread
= CALL_EXPR_ARG (exp
, 1);
10698 objsize
= CALL_EXPR_ARG (exp
, 3);
10699 /* The only checked access the write to the destination. */
10700 mode
= access_write_only
;
10703 gcc_unreachable ();
10706 if (catstr
&& maxread
)
10708 /* Check __strncat_chk. There is no way to determine the length
10709 of the string to which the source string is being appended so
10710 just warn when the length of the source string is not known. */
10711 check_strncat_sizes (exp
, objsize
);
10715 check_access (exp
, size
, maxread
, srcstr
, objsize
, mode
);
10718 /* Emit warning if a buffer overflow is detected at compile time
10719 in __sprintf_chk/__vsprintf_chk calls. */
10722 maybe_emit_sprintf_chk_warning (tree exp
, enum built_in_function fcode
)
10724 tree size
, len
, fmt
;
10725 const char *fmt_str
;
10726 int nargs
= call_expr_nargs (exp
);
10728 /* Verify the required arguments in the original call. */
10732 size
= CALL_EXPR_ARG (exp
, 2);
10733 fmt
= CALL_EXPR_ARG (exp
, 3);
10735 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
10738 /* Check whether the format is a literal string constant. */
10739 fmt_str
= c_getstr (fmt
);
10740 if (fmt_str
== NULL
)
10743 if (!init_target_chars ())
10746 /* If the format doesn't contain % args or %%, we know its size. */
10747 if (strchr (fmt_str
, target_percent
) == 0)
10748 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
10749 /* If the format is "%s" and first ... argument is a string literal,
10751 else if (fcode
== BUILT_IN_SPRINTF_CHK
10752 && strcmp (fmt_str
, target_percent_s
) == 0)
10758 arg
= CALL_EXPR_ARG (exp
, 4);
10759 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
10762 len
= c_strlen (arg
, 1);
10763 if (!len
|| ! tree_fits_uhwi_p (len
))
10769 /* Add one for the terminating nul. */
10770 len
= fold_build2 (PLUS_EXPR
, TREE_TYPE (len
), len
, size_one_node
);
10772 check_access (exp
, /*size=*/NULL_TREE
, /*maxread=*/NULL_TREE
, len
, size
,
10773 access_write_only
);
10776 /* Fold a call to __builtin_object_size with arguments PTR and OST,
10780 fold_builtin_object_size (tree ptr
, tree ost
, enum built_in_function fcode
)
10783 int object_size_type
;
10785 if (!validate_arg (ptr
, POINTER_TYPE
)
10786 || !validate_arg (ost
, INTEGER_TYPE
))
10791 if (TREE_CODE (ost
) != INTEGER_CST
10792 || tree_int_cst_sgn (ost
) < 0
10793 || compare_tree_int (ost
, 3) > 0)
10796 object_size_type
= tree_to_shwi (ost
);
10798 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10799 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10800 and (size_t) 0 for types 2 and 3. */
10801 if (TREE_SIDE_EFFECTS (ptr
))
10802 return build_int_cst_type (size_type_node
, object_size_type
< 2 ? -1 : 0);
10804 if (fcode
== BUILT_IN_DYNAMIC_OBJECT_SIZE
)
10805 object_size_type
|= OST_DYNAMIC
;
10807 if (TREE_CODE (ptr
) == ADDR_EXPR
)
10809 compute_builtin_object_size (ptr
, object_size_type
, &bytes
);
10810 if ((object_size_type
& OST_DYNAMIC
)
10811 || int_fits_type_p (bytes
, size_type_node
))
10812 return fold_convert (size_type_node
, bytes
);
10814 else if (TREE_CODE (ptr
) == SSA_NAME
)
10816 /* If object size is not known yet, delay folding until
10817 later. Maybe subsequent passes will help determining
10819 if (compute_builtin_object_size (ptr
, object_size_type
, &bytes
)
10820 && ((object_size_type
& OST_DYNAMIC
)
10821 || int_fits_type_p (bytes
, size_type_node
)))
10822 return fold_convert (size_type_node
, bytes
);
10828 /* Builtins with folding operations that operate on "..." arguments
10829 need special handling; we need to store the arguments in a convenient
10830 data structure before attempting any folding. Fortunately there are
10831 only a few builtins that fall into this category. FNDECL is the
10832 function, EXP is the CALL_EXPR for the call. */
10835 fold_builtin_varargs (location_t loc
, tree fndecl
, tree
*args
, int nargs
)
10837 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
10838 tree ret
= NULL_TREE
;
10842 case BUILT_IN_FPCLASSIFY
:
10843 ret
= fold_builtin_fpclassify (loc
, args
, nargs
);
10851 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
10852 SET_EXPR_LOCATION (ret
, loc
);
10853 suppress_warning (ret
);
10859 /* Initialize format string characters in the target charset. */
10862 init_target_chars (void)
10867 target_newline
= lang_hooks
.to_target_charset ('\n');
10868 target_percent
= lang_hooks
.to_target_charset ('%');
10869 target_c
= lang_hooks
.to_target_charset ('c');
10870 target_s
= lang_hooks
.to_target_charset ('s');
10871 if (target_newline
== 0 || target_percent
== 0 || target_c
== 0
10875 target_percent_c
[0] = target_percent
;
10876 target_percent_c
[1] = target_c
;
10877 target_percent_c
[2] = '\0';
10879 target_percent_s
[0] = target_percent
;
10880 target_percent_s
[1] = target_s
;
10881 target_percent_s
[2] = '\0';
10883 target_percent_s_newline
[0] = target_percent
;
10884 target_percent_s_newline
[1] = target_s
;
10885 target_percent_s_newline
[2] = target_newline
;
10886 target_percent_s_newline
[3] = '\0';
10893 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
10894 and no overflow/underflow occurred. INEXACT is true if M was not
10895 exactly calculated. TYPE is the tree type for the result. This
10896 function assumes that you cleared the MPFR flags and then
10897 calculated M to see if anything subsequently set a flag prior to
10898 entering this function. Return NULL_TREE if any checks fail. */
10901 do_mpfr_ckconv (mpfr_srcptr m
, tree type
, int inexact
)
10903 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10904 overflow/underflow occurred. If -frounding-math, proceed iff the
10905 result of calling FUNC was exact. */
10906 if (mpfr_number_p (m
) && !mpfr_overflow_p () && !mpfr_underflow_p ()
10907 && (!flag_rounding_math
|| !inexact
))
10909 REAL_VALUE_TYPE rr
;
10911 real_from_mpfr (&rr
, m
, type
, MPFR_RNDN
);
10912 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
10913 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10914 but the mpfr_t is not, then we underflowed in the
10916 if (real_isfinite (&rr
)
10917 && (rr
.cl
== rvc_zero
) == (mpfr_zero_p (m
) != 0))
10919 REAL_VALUE_TYPE rmode
;
10921 real_convert (&rmode
, TYPE_MODE (type
), &rr
);
10922 /* Proceed iff the specified mode can hold the value. */
10923 if (real_identical (&rmode
, &rr
))
10924 return build_real (type
, rmode
);
10930 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
10931 number and no overflow/underflow occurred. INEXACT is true if M
10932 was not exactly calculated. TYPE is the tree type for the result.
10933 This function assumes that you cleared the MPFR flags and then
10934 calculated M to see if anything subsequently set a flag prior to
10935 entering this function. Return NULL_TREE if any checks fail, if
10936 FORCE_CONVERT is true, then bypass the checks. */
10939 do_mpc_ckconv (mpc_srcptr m
, tree type
, int inexact
, int force_convert
)
10941 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10942 overflow/underflow occurred. If -frounding-math, proceed iff the
10943 result of calling FUNC was exact. */
10945 || (mpfr_number_p (mpc_realref (m
)) && mpfr_number_p (mpc_imagref (m
))
10946 && !mpfr_overflow_p () && !mpfr_underflow_p ()
10947 && (!flag_rounding_math
|| !inexact
)))
10949 REAL_VALUE_TYPE re
, im
;
10951 real_from_mpfr (&re
, mpc_realref (m
), TREE_TYPE (type
), MPFR_RNDN
);
10952 real_from_mpfr (&im
, mpc_imagref (m
), TREE_TYPE (type
), MPFR_RNDN
);
10953 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
10954 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10955 but the mpfr_t is not, then we underflowed in the
10958 || (real_isfinite (&re
) && real_isfinite (&im
)
10959 && (re
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_realref (m
)) != 0)
10960 && (im
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_imagref (m
)) != 0)))
10962 REAL_VALUE_TYPE re_mode
, im_mode
;
10964 real_convert (&re_mode
, TYPE_MODE (TREE_TYPE (type
)), &re
);
10965 real_convert (&im_mode
, TYPE_MODE (TREE_TYPE (type
)), &im
);
10966 /* Proceed iff the specified mode can hold the value. */
10968 || (real_identical (&re_mode
, &re
)
10969 && real_identical (&im_mode
, &im
)))
10970 return build_complex (type
, build_real (TREE_TYPE (type
), re_mode
),
10971 build_real (TREE_TYPE (type
), im_mode
));
10977 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
10978 the pointer *(ARG_QUO) and return the result. The type is taken
10979 from the type of ARG0 and is used for setting the precision of the
10980 calculation and results. */
10983 do_mpfr_remquo (tree arg0
, tree arg1
, tree arg_quo
)
10985 tree
const type
= TREE_TYPE (arg0
);
10986 tree result
= NULL_TREE
;
10991 /* To proceed, MPFR must exactly represent the target floating point
10992 format, which only happens when the target base equals two. */
10993 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
10994 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
10995 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
))
10997 const REAL_VALUE_TYPE
*const ra0
= TREE_REAL_CST_PTR (arg0
);
10998 const REAL_VALUE_TYPE
*const ra1
= TREE_REAL_CST_PTR (arg1
);
11000 if (real_isfinite (ra0
) && real_isfinite (ra1
))
11002 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
11003 const int prec
= fmt
->p
;
11004 const mpfr_rnd_t rnd
= fmt
->round_towards_zero
? MPFR_RNDZ
: MPFR_RNDN
;
11009 mpfr_inits2 (prec
, m0
, m1
, NULL
);
11010 mpfr_from_real (m0
, ra0
, MPFR_RNDN
);
11011 mpfr_from_real (m1
, ra1
, MPFR_RNDN
);
11012 mpfr_clear_flags ();
11013 mpfr_remquo (m0
, &integer_quo
, m0
, m1
, rnd
);
11014 /* Remquo is independent of the rounding mode, so pass
11015 inexact=0 to do_mpfr_ckconv(). */
11016 result_rem
= do_mpfr_ckconv (m0
, type
, /*inexact=*/ 0);
11017 mpfr_clears (m0
, m1
, NULL
);
11020 /* MPFR calculates quo in the host's long so it may
11021 return more bits in quo than the target int can hold
11022 if sizeof(host long) > sizeof(target int). This can
11023 happen even for native compilers in LP64 mode. In
11024 these cases, modulo the quo value with the largest
11025 number that the target int can hold while leaving one
11026 bit for the sign. */
11027 if (sizeof (integer_quo
) * CHAR_BIT
> INT_TYPE_SIZE
)
11028 integer_quo
%= (long)(1UL << (INT_TYPE_SIZE
- 1));
11030 /* Dereference the quo pointer argument. */
11031 arg_quo
= build_fold_indirect_ref (arg_quo
);
11032 /* Proceed iff a valid pointer type was passed in. */
11033 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo
)) == integer_type_node
)
11035 /* Set the value. */
11037 = fold_build2 (MODIFY_EXPR
, TREE_TYPE (arg_quo
), arg_quo
,
11038 build_int_cst (TREE_TYPE (arg_quo
),
11040 TREE_SIDE_EFFECTS (result_quo
) = 1;
11041 /* Combine the quo assignment with the rem. */
11042 result
= fold_build2 (COMPOUND_EXPR
, type
,
11043 result_quo
, result_rem
);
11044 suppress_warning (result
, OPT_Wunused_value
);
11045 result
= non_lvalue (result
);
11053 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
11054 resulting value as a tree with type TYPE. The mpfr precision is
11055 set to the precision of TYPE. We assume that this mpfr function
11056 returns zero if the result could be calculated exactly within the
11057 requested precision. In addition, the integer pointer represented
11058 by ARG_SG will be dereferenced and set to the appropriate signgam
11062 do_mpfr_lgamma_r (tree arg
, tree arg_sg
, tree type
)
11064 tree result
= NULL_TREE
;
11068 /* To proceed, MPFR must exactly represent the target floating point
11069 format, which only happens when the target base equals two. Also
11070 verify ARG is a constant and that ARG_SG is an int pointer. */
11071 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
11072 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
)
11073 && TREE_CODE (TREE_TYPE (arg_sg
)) == POINTER_TYPE
11074 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg
))) == integer_type_node
)
11076 const REAL_VALUE_TYPE
*const ra
= TREE_REAL_CST_PTR (arg
);
11078 /* In addition to NaN and Inf, the argument cannot be zero or a
11079 negative integer. */
11080 if (real_isfinite (ra
)
11081 && ra
->cl
!= rvc_zero
11082 && !(real_isneg (ra
) && real_isinteger (ra
, TYPE_MODE (type
))))
11084 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
11085 const int prec
= fmt
->p
;
11086 const mpfr_rnd_t rnd
= fmt
->round_towards_zero
? MPFR_RNDZ
: MPFR_RNDN
;
11090 auto_mpfr
m (prec
);
11091 mpfr_from_real (m
, ra
, MPFR_RNDN
);
11092 mpfr_clear_flags ();
11093 inexact
= mpfr_lgamma (m
, &sg
, m
, rnd
);
11094 result_lg
= do_mpfr_ckconv (m
, type
, inexact
);
11099 /* Dereference the arg_sg pointer argument. */
11100 arg_sg
= build_fold_indirect_ref (arg_sg
);
11101 /* Assign the signgam value into *arg_sg. */
11102 result_sg
= fold_build2 (MODIFY_EXPR
,
11103 TREE_TYPE (arg_sg
), arg_sg
,
11104 build_int_cst (TREE_TYPE (arg_sg
), sg
));
11105 TREE_SIDE_EFFECTS (result_sg
) = 1;
11106 /* Combine the signgam assignment with the lgamma result. */
11107 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
11108 result_sg
, result_lg
));
11116 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
11117 mpc function FUNC on it and return the resulting value as a tree
11118 with type TYPE. The mpfr precision is set to the precision of
11119 TYPE. We assume that function FUNC returns zero if the result
11120 could be calculated exactly within the requested precision. If
11121 DO_NONFINITE is true, then fold expressions containing Inf or NaN
11122 in the arguments and/or results. */
11125 do_mpc_arg2 (tree arg0
, tree arg1
, tree type
, int do_nonfinite
,
11126 int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_srcptr
, mpc_rnd_t
))
11128 tree result
= NULL_TREE
;
11133 /* To proceed, MPFR must exactly represent the target floating point
11134 format, which only happens when the target base equals two. */
11135 if (TREE_CODE (arg0
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg0
)
11136 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg0
)))
11137 && TREE_CODE (arg1
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg1
)
11138 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg1
)))
11139 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0
))))->b
== 2)
11141 const REAL_VALUE_TYPE
*const re0
= TREE_REAL_CST_PTR (TREE_REALPART (arg0
));
11142 const REAL_VALUE_TYPE
*const im0
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg0
));
11143 const REAL_VALUE_TYPE
*const re1
= TREE_REAL_CST_PTR (TREE_REALPART (arg1
));
11144 const REAL_VALUE_TYPE
*const im1
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg1
));
11147 || (real_isfinite (re0
) && real_isfinite (im0
)
11148 && real_isfinite (re1
) && real_isfinite (im1
)))
11150 const struct real_format
*const fmt
=
11151 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
11152 const int prec
= fmt
->p
;
11153 const mpfr_rnd_t rnd
= fmt
->round_towards_zero
11154 ? MPFR_RNDZ
: MPFR_RNDN
;
11155 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
11159 mpc_init2 (m0
, prec
);
11160 mpc_init2 (m1
, prec
);
11161 mpfr_from_real (mpc_realref (m0
), re0
, rnd
);
11162 mpfr_from_real (mpc_imagref (m0
), im0
, rnd
);
11163 mpfr_from_real (mpc_realref (m1
), re1
, rnd
);
11164 mpfr_from_real (mpc_imagref (m1
), im1
, rnd
);
11165 mpfr_clear_flags ();
11166 inexact
= func (m0
, m0
, m1
, crnd
);
11167 result
= do_mpc_ckconv (m0
, type
, inexact
, do_nonfinite
);
11176 /* A wrapper function for builtin folding that prevents warnings for
11177 "statement without effect" and the like, caused by removing the
11178 call node earlier than the warning is generated. */
11181 fold_call_stmt (gcall
*stmt
, bool ignore
)
11183 tree ret
= NULL_TREE
;
11184 tree fndecl
= gimple_call_fndecl (stmt
);
11185 location_t loc
= gimple_location (stmt
);
11186 if (fndecl
&& fndecl_built_in_p (fndecl
)
11187 && !gimple_call_va_arg_pack_p (stmt
))
11189 int nargs
= gimple_call_num_args (stmt
);
11190 tree
*args
= (nargs
> 0
11191 ? gimple_call_arg_ptr (stmt
, 0)
11192 : &error_mark_node
);
11194 if (avoid_folding_inline_builtin (fndecl
))
11196 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
11198 return targetm
.fold_builtin (fndecl
, nargs
, args
, ignore
);
11202 ret
= fold_builtin_n (loc
, NULL_TREE
, fndecl
, args
, nargs
, ignore
);
11205 /* Propagate location information from original call to
11206 expansion of builtin. Otherwise things like
11207 maybe_emit_chk_warning, that operate on the expansion
11208 of a builtin, will use the wrong location information. */
11209 if (gimple_has_location (stmt
))
11211 tree realret
= ret
;
11212 if (TREE_CODE (ret
) == NOP_EXPR
)
11213 realret
= TREE_OPERAND (ret
, 0);
11214 if (CAN_HAVE_LOCATION_P (realret
)
11215 && !EXPR_HAS_LOCATION (realret
))
11216 SET_EXPR_LOCATION (realret
, loc
);
11226 /* Look up the function in builtin_decl that corresponds to DECL
11227 and set ASMSPEC as its user assembler name. DECL must be a
11228 function decl that declares a builtin. */
11231 set_builtin_user_assembler_name (tree decl
, const char *asmspec
)
11233 gcc_assert (fndecl_built_in_p (decl
, BUILT_IN_NORMAL
)
11236 tree builtin
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
11237 set_user_assembler_name (builtin
, asmspec
);
11239 if (DECL_FUNCTION_CODE (decl
) == BUILT_IN_FFS
11240 && INT_TYPE_SIZE
< BITS_PER_WORD
)
11242 scalar_int_mode mode
= int_mode_for_size (INT_TYPE_SIZE
, 0).require ();
11243 set_user_assembler_libfunc ("ffs", asmspec
);
11244 set_optab_libfunc (ffs_optab
, mode
, "ffs");
11248 /* Return true if DECL is a builtin that expands to a constant or similarly
11251 is_simple_builtin (tree decl
)
11253 if (decl
&& fndecl_built_in_p (decl
, BUILT_IN_NORMAL
))
11254 switch (DECL_FUNCTION_CODE (decl
))
11256 /* Builtins that expand to constants. */
11257 case BUILT_IN_CONSTANT_P
:
11258 case BUILT_IN_EXPECT
:
11259 case BUILT_IN_OBJECT_SIZE
:
11260 case BUILT_IN_UNREACHABLE
:
11261 /* Simple register moves or loads from stack. */
11262 case BUILT_IN_ASSUME_ALIGNED
:
11263 case BUILT_IN_RETURN_ADDRESS
:
11264 case BUILT_IN_EXTRACT_RETURN_ADDR
:
11265 case BUILT_IN_FROB_RETURN_ADDR
:
11266 case BUILT_IN_RETURN
:
11267 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
11268 case BUILT_IN_FRAME_ADDRESS
:
11269 case BUILT_IN_VA_END
:
11270 case BUILT_IN_STACK_SAVE
:
11271 case BUILT_IN_STACK_RESTORE
:
11272 case BUILT_IN_DWARF_CFA
:
11273 /* Exception state returns or moves registers around. */
11274 case BUILT_IN_EH_FILTER
:
11275 case BUILT_IN_EH_POINTER
:
11276 case BUILT_IN_EH_COPY_VALUES
:
11286 /* Return true if DECL is a builtin that is not expensive, i.e., they are
11287 most probably expanded inline into reasonably simple code. This is a
11288 superset of is_simple_builtin. */
11290 is_inexpensive_builtin (tree decl
)
11294 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_MD
)
11296 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
11297 switch (DECL_FUNCTION_CODE (decl
))
11300 CASE_BUILT_IN_ALLOCA
:
11301 case BUILT_IN_BSWAP16
:
11302 case BUILT_IN_BSWAP32
:
11303 case BUILT_IN_BSWAP64
:
11304 case BUILT_IN_BSWAP128
:
11306 case BUILT_IN_CLZIMAX
:
11307 case BUILT_IN_CLZL
:
11308 case BUILT_IN_CLZLL
:
11310 case BUILT_IN_CTZIMAX
:
11311 case BUILT_IN_CTZL
:
11312 case BUILT_IN_CTZLL
:
11314 case BUILT_IN_FFSIMAX
:
11315 case BUILT_IN_FFSL
:
11316 case BUILT_IN_FFSLL
:
11317 case BUILT_IN_IMAXABS
:
11318 case BUILT_IN_FINITE
:
11319 case BUILT_IN_FINITEF
:
11320 case BUILT_IN_FINITEL
:
11321 case BUILT_IN_FINITED32
:
11322 case BUILT_IN_FINITED64
:
11323 case BUILT_IN_FINITED128
:
11324 case BUILT_IN_FPCLASSIFY
:
11325 case BUILT_IN_ISFINITE
:
11326 case BUILT_IN_ISINF_SIGN
:
11327 case BUILT_IN_ISINF
:
11328 case BUILT_IN_ISINFF
:
11329 case BUILT_IN_ISINFL
:
11330 case BUILT_IN_ISINFD32
:
11331 case BUILT_IN_ISINFD64
:
11332 case BUILT_IN_ISINFD128
:
11333 case BUILT_IN_ISNAN
:
11334 case BUILT_IN_ISNANF
:
11335 case BUILT_IN_ISNANL
:
11336 case BUILT_IN_ISNAND32
:
11337 case BUILT_IN_ISNAND64
:
11338 case BUILT_IN_ISNAND128
:
11339 case BUILT_IN_ISNORMAL
:
11340 case BUILT_IN_ISGREATER
:
11341 case BUILT_IN_ISGREATEREQUAL
:
11342 case BUILT_IN_ISLESS
:
11343 case BUILT_IN_ISLESSEQUAL
:
11344 case BUILT_IN_ISLESSGREATER
:
11345 case BUILT_IN_ISUNORDERED
:
11346 case BUILT_IN_VA_ARG_PACK
:
11347 case BUILT_IN_VA_ARG_PACK_LEN
:
11348 case BUILT_IN_VA_COPY
:
11349 case BUILT_IN_TRAP
:
11350 case BUILT_IN_UNREACHABLE_TRAP
:
11351 case BUILT_IN_SAVEREGS
:
11352 case BUILT_IN_POPCOUNTL
:
11353 case BUILT_IN_POPCOUNTLL
:
11354 case BUILT_IN_POPCOUNTIMAX
:
11355 case BUILT_IN_POPCOUNT
:
11356 case BUILT_IN_PARITYL
:
11357 case BUILT_IN_PARITYLL
:
11358 case BUILT_IN_PARITYIMAX
:
11359 case BUILT_IN_PARITY
:
11360 case BUILT_IN_LABS
:
11361 case BUILT_IN_LLABS
:
11362 case BUILT_IN_PREFETCH
:
11363 case BUILT_IN_ACC_ON_DEVICE
:
11367 return is_simple_builtin (decl
);
11373 /* Return true if T is a constant and the value cast to a target char
11374 can be represented by a host char.
11375 Store the casted char constant in *P if so. */
11378 target_char_cst_p (tree t
, char *p
)
11380 if (!tree_fits_uhwi_p (t
) || CHAR_TYPE_SIZE
!= HOST_BITS_PER_CHAR
)
11383 *p
= (char)tree_to_uhwi (t
);
11387 /* Return true if the builtin DECL is implemented in a standard library.
11388 Otherwise return false which doesn't guarantee it is not (thus the list
11389 of handled builtins below may be incomplete). */
11392 builtin_with_linkage_p (tree decl
)
11394 if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
11395 switch (DECL_FUNCTION_CODE (decl
))
11397 CASE_FLT_FN (BUILT_IN_ACOS
):
11398 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS
):
11399 CASE_FLT_FN (BUILT_IN_ACOSH
):
11400 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH
):
11401 CASE_FLT_FN (BUILT_IN_ASIN
):
11402 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN
):
11403 CASE_FLT_FN (BUILT_IN_ASINH
):
11404 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINH
):
11405 CASE_FLT_FN (BUILT_IN_ATAN
):
11406 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN
):
11407 CASE_FLT_FN (BUILT_IN_ATANH
):
11408 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH
):
11409 CASE_FLT_FN (BUILT_IN_ATAN2
):
11410 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN2
):
11411 CASE_FLT_FN (BUILT_IN_CBRT
):
11412 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CBRT
):
11413 CASE_FLT_FN (BUILT_IN_CEIL
):
11414 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL
):
11415 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
11416 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN
):
11417 CASE_FLT_FN (BUILT_IN_COS
):
11418 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COS
):
11419 CASE_FLT_FN (BUILT_IN_COSH
):
11420 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSH
):
11421 CASE_FLT_FN (BUILT_IN_ERF
):
11422 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERF
):
11423 CASE_FLT_FN (BUILT_IN_ERFC
):
11424 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERFC
):
11425 CASE_FLT_FN (BUILT_IN_EXP
):
11426 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP
):
11427 CASE_FLT_FN (BUILT_IN_EXP2
):
11428 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP2
):
11429 CASE_FLT_FN (BUILT_IN_EXPM1
):
11430 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXPM1
):
11431 CASE_FLT_FN (BUILT_IN_FABS
):
11432 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS
):
11433 CASE_FLT_FN (BUILT_IN_FDIM
):
11434 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FDIM
):
11435 CASE_FLT_FN (BUILT_IN_FLOOR
):
11436 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR
):
11437 CASE_FLT_FN (BUILT_IN_FMA
):
11438 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA
):
11439 CASE_FLT_FN (BUILT_IN_FMAX
):
11440 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX
):
11441 CASE_FLT_FN (BUILT_IN_FMIN
):
11442 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN
):
11443 CASE_FLT_FN (BUILT_IN_FMOD
):
11444 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMOD
):
11445 CASE_FLT_FN (BUILT_IN_FREXP
):
11446 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FREXP
):
11447 CASE_FLT_FN (BUILT_IN_HYPOT
):
11448 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HYPOT
):
11449 CASE_FLT_FN (BUILT_IN_ILOGB
):
11450 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ILOGB
):
11451 CASE_FLT_FN (BUILT_IN_LDEXP
):
11452 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LDEXP
):
11453 CASE_FLT_FN (BUILT_IN_LGAMMA
):
11454 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LGAMMA
):
11455 CASE_FLT_FN (BUILT_IN_LLRINT
):
11456 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLRINT
):
11457 CASE_FLT_FN (BUILT_IN_LLROUND
):
11458 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLROUND
):
11459 CASE_FLT_FN (BUILT_IN_LOG
):
11460 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG
):
11461 CASE_FLT_FN (BUILT_IN_LOG10
):
11462 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10
):
11463 CASE_FLT_FN (BUILT_IN_LOG1P
):
11464 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P
):
11465 CASE_FLT_FN (BUILT_IN_LOG2
):
11466 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2
):
11467 CASE_FLT_FN (BUILT_IN_LOGB
):
11468 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOGB
):
11469 CASE_FLT_FN (BUILT_IN_LRINT
):
11470 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LRINT
):
11471 CASE_FLT_FN (BUILT_IN_LROUND
):
11472 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LROUND
):
11473 CASE_FLT_FN (BUILT_IN_MODF
):
11474 CASE_FLT_FN_FLOATN_NX (BUILT_IN_MODF
):
11475 CASE_FLT_FN (BUILT_IN_NAN
):
11476 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NAN
):
11477 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
11478 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT
):
11479 CASE_FLT_FN (BUILT_IN_NEXTAFTER
):
11480 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEXTAFTER
):
11481 CASE_FLT_FN (BUILT_IN_NEXTTOWARD
):
11482 CASE_FLT_FN (BUILT_IN_POW
):
11483 CASE_FLT_FN_FLOATN_NX (BUILT_IN_POW
):
11484 CASE_FLT_FN (BUILT_IN_REMAINDER
):
11485 CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMAINDER
):
11486 CASE_FLT_FN (BUILT_IN_REMQUO
):
11487 CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMQUO
):
11488 CASE_FLT_FN (BUILT_IN_RINT
):
11489 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT
):
11490 CASE_FLT_FN (BUILT_IN_ROUND
):
11491 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND
):
11492 CASE_FLT_FN (BUILT_IN_SCALBLN
):
11493 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBLN
):
11494 CASE_FLT_FN (BUILT_IN_SCALBN
):
11495 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBN
):
11496 CASE_FLT_FN (BUILT_IN_SIN
):
11497 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SIN
):
11498 CASE_FLT_FN (BUILT_IN_SINH
):
11499 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINH
):
11500 CASE_FLT_FN (BUILT_IN_SINCOS
):
11501 CASE_FLT_FN (BUILT_IN_SQRT
):
11502 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT
):
11503 CASE_FLT_FN (BUILT_IN_TAN
):
11504 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TAN
):
11505 CASE_FLT_FN (BUILT_IN_TANH
):
11506 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TANH
):
11507 CASE_FLT_FN (BUILT_IN_TGAMMA
):
11508 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TGAMMA
):
11509 CASE_FLT_FN (BUILT_IN_TRUNC
):
11510 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC
):
11513 case BUILT_IN_STPCPY
:
11514 case BUILT_IN_STPNCPY
:
11515 /* stpcpy is both referenced in libiberty's pex-win32.c and provided
11516 by libiberty's stpcpy.c for MinGW targets so we need to return true
11517 in order to be able to build libiberty in LTO mode for them. */
11526 /* Return true if OFFRNG is bounded to a subrange of offset values
11527 valid for the largest possible object. */
11530 access_ref::offset_bounded () const
11532 tree min
= TYPE_MIN_VALUE (ptrdiff_type_node
);
11533 tree max
= TYPE_MAX_VALUE (ptrdiff_type_node
);
11534 return wi::to_offset (min
) <= offrng
[0] && offrng
[1] <= wi::to_offset (max
);
11537 /* If CALLEE has known side effects, fill in INFO and return true.
11538 See tree-ssa-structalias.cc:find_func_aliases
11539 for the list of builtins we might need to handle here. */
11542 builtin_fnspec (tree callee
)
11544 built_in_function code
= DECL_FUNCTION_CODE (callee
);
11548 /* All the following functions read memory pointed to by
11549 their second argument and write memory pointed to by first
11551 strcat/strncat additionally reads memory pointed to by the first
11553 case BUILT_IN_STRCAT
:
11554 case BUILT_IN_STRCAT_CHK
:
11556 case BUILT_IN_STRNCAT
:
11557 case BUILT_IN_STRNCAT_CHK
:
11559 case BUILT_IN_STRCPY
:
11560 case BUILT_IN_STRCPY_CHK
:
11562 case BUILT_IN_STPCPY
:
11563 case BUILT_IN_STPCPY_CHK
:
11565 case BUILT_IN_STRNCPY
:
11566 case BUILT_IN_MEMCPY
:
11567 case BUILT_IN_MEMMOVE
:
11568 case BUILT_IN_TM_MEMCPY
:
11569 case BUILT_IN_TM_MEMMOVE
:
11570 case BUILT_IN_STRNCPY_CHK
:
11571 case BUILT_IN_MEMCPY_CHK
:
11572 case BUILT_IN_MEMMOVE_CHK
:
11574 case BUILT_IN_MEMPCPY
:
11575 case BUILT_IN_MEMPCPY_CHK
:
11577 case BUILT_IN_STPNCPY
:
11578 case BUILT_IN_STPNCPY_CHK
:
11580 case BUILT_IN_BCOPY
:
11582 case BUILT_IN_BZERO
:
11584 case BUILT_IN_MEMCMP
:
11585 case BUILT_IN_MEMCMP_EQ
:
11586 case BUILT_IN_BCMP
:
11587 case BUILT_IN_STRNCMP
:
11588 case BUILT_IN_STRNCMP_EQ
:
11589 case BUILT_IN_STRNCASECMP
:
11592 /* The following functions read memory pointed to by their
11594 CASE_BUILT_IN_TM_LOAD (1):
11595 CASE_BUILT_IN_TM_LOAD (2):
11596 CASE_BUILT_IN_TM_LOAD (4):
11597 CASE_BUILT_IN_TM_LOAD (8):
11598 CASE_BUILT_IN_TM_LOAD (FLOAT
):
11599 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
11600 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
11601 CASE_BUILT_IN_TM_LOAD (M64
):
11602 CASE_BUILT_IN_TM_LOAD (M128
):
11603 CASE_BUILT_IN_TM_LOAD (M256
):
11604 case BUILT_IN_TM_LOG
:
11605 case BUILT_IN_TM_LOG_1
:
11606 case BUILT_IN_TM_LOG_2
:
11607 case BUILT_IN_TM_LOG_4
:
11608 case BUILT_IN_TM_LOG_8
:
11609 case BUILT_IN_TM_LOG_FLOAT
:
11610 case BUILT_IN_TM_LOG_DOUBLE
:
11611 case BUILT_IN_TM_LOG_LDOUBLE
:
11612 case BUILT_IN_TM_LOG_M64
:
11613 case BUILT_IN_TM_LOG_M128
:
11614 case BUILT_IN_TM_LOG_M256
:
11617 case BUILT_IN_INDEX
:
11618 case BUILT_IN_RINDEX
:
11619 case BUILT_IN_STRCHR
:
11620 case BUILT_IN_STRLEN
:
11621 case BUILT_IN_STRRCHR
:
11623 case BUILT_IN_STRNLEN
:
11626 /* These read memory pointed to by the first argument.
11627 Allocating memory does not have any side-effects apart from
11628 being the definition point for the pointer.
11629 Unix98 specifies that errno is set on allocation failure. */
11630 case BUILT_IN_STRDUP
:
11632 case BUILT_IN_STRNDUP
:
11634 /* Allocating memory does not have any side-effects apart from
11635 being the definition point for the pointer. */
11636 case BUILT_IN_MALLOC
:
11637 case BUILT_IN_ALIGNED_ALLOC
:
11638 case BUILT_IN_CALLOC
:
11639 case BUILT_IN_GOMP_ALLOC
:
11641 CASE_BUILT_IN_ALLOCA
:
11643 /* These read memory pointed to by the first argument with size
11644 in the third argument. */
11645 case BUILT_IN_MEMCHR
:
11647 /* These read memory pointed to by the first and second arguments. */
11648 case BUILT_IN_STRSTR
:
11649 case BUILT_IN_STRPBRK
:
11650 case BUILT_IN_STRCASECMP
:
11651 case BUILT_IN_STRCSPN
:
11652 case BUILT_IN_STRSPN
:
11653 case BUILT_IN_STRCMP
:
11654 case BUILT_IN_STRCMP_EQ
:
11656 /* Freeing memory kills the pointed-to memory. More importantly
11657 the call has to serve as a barrier for moving loads and stores
11659 case BUILT_IN_STACK_RESTORE
:
11660 case BUILT_IN_FREE
:
11661 case BUILT_IN_GOMP_FREE
:
11663 case BUILT_IN_VA_END
:
11665 /* Realloc serves both as allocation point and deallocation point. */
11666 case BUILT_IN_REALLOC
:
11668 case BUILT_IN_GAMMA_R
:
11669 case BUILT_IN_GAMMAF_R
:
11670 case BUILT_IN_GAMMAL_R
:
11671 case BUILT_IN_LGAMMA_R
:
11672 case BUILT_IN_LGAMMAF_R
:
11673 case BUILT_IN_LGAMMAL_R
:
11675 case BUILT_IN_FREXP
:
11676 case BUILT_IN_FREXPF
:
11677 case BUILT_IN_FREXPL
:
11678 case BUILT_IN_MODF
:
11679 case BUILT_IN_MODFF
:
11680 case BUILT_IN_MODFL
:
11682 case BUILT_IN_REMQUO
:
11683 case BUILT_IN_REMQUOF
:
11684 case BUILT_IN_REMQUOL
:
11686 case BUILT_IN_SINCOS
:
11687 case BUILT_IN_SINCOSF
:
11688 case BUILT_IN_SINCOSL
:
11690 case BUILT_IN_MEMSET
:
11691 case BUILT_IN_MEMSET_CHK
:
11692 case BUILT_IN_TM_MEMSET
:
11694 CASE_BUILT_IN_TM_STORE (1):
11695 CASE_BUILT_IN_TM_STORE (2):
11696 CASE_BUILT_IN_TM_STORE (4):
11697 CASE_BUILT_IN_TM_STORE (8):
11698 CASE_BUILT_IN_TM_STORE (FLOAT
):
11699 CASE_BUILT_IN_TM_STORE (DOUBLE
):
11700 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
11701 CASE_BUILT_IN_TM_STORE (M64
):
11702 CASE_BUILT_IN_TM_STORE (M128
):
11703 CASE_BUILT_IN_TM_STORE (M256
):
11705 case BUILT_IN_STACK_SAVE
:
11706 case BUILT_IN_RETURN
:
11707 case BUILT_IN_EH_POINTER
:
11708 case BUILT_IN_EH_FILTER
:
11709 case BUILT_IN_UNWIND_RESUME
:
11710 case BUILT_IN_CXA_END_CLEANUP
:
11711 case BUILT_IN_EH_COPY_VALUES
:
11712 case BUILT_IN_FRAME_ADDRESS
:
11713 case BUILT_IN_APPLY_ARGS
:
11714 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT
:
11715 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT
:
11716 case BUILT_IN_PREFETCH
:
11717 case BUILT_IN_DWARF_CFA
:
11718 case BUILT_IN_RETURN_ADDRESS
:
11720 case BUILT_IN_ASSUME_ALIGNED
:
11721 case BUILT_IN_EXPECT
:
11722 case BUILT_IN_EXPECT_WITH_PROBABILITY
:
11724 /* But posix_memalign stores a pointer into the memory pointed to
11725 by its first argument. */
11726 case BUILT_IN_POSIX_MEMALIGN
: