1 /* Expand builtin functions.
2 Copyright (C) 1988-2015 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.c instead. */
26 #include "coretypes.h"
34 #include "stringpool.h"
35 #include "tree-ssanames.h"
40 #include "diagnostic-core.h"
42 #include "fold-const.h"
43 #include "fold-const-call.h"
44 #include "stor-layout.h"
47 #include "tree-object-size.h"
57 #include "typeclass.h"
58 #include "langhooks.h"
59 #include "value-prof.h"
63 #include "tree-chkp.h"
67 struct target_builtins default_target_builtins
;
69 struct target_builtins
*this_target_builtins
= &default_target_builtins
;
72 /* Define the names of the builtin function types and codes. */
73 const char *const built_in_class_names
[BUILT_IN_LAST
]
74 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
76 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
77 const char * built_in_names
[(int) END_BUILTINS
] =
79 #include "builtins.def"
83 /* Setup an array of builtin_info_type, make sure each element decl is
84 initialized to NULL_TREE. */
85 builtin_info_type builtin_info
[(int)END_BUILTINS
];
87 /* Non-zero if __builtin_constant_p should be folded right away. */
88 bool force_folding_builtin_constant_p
;
90 static rtx
c_readstr (const char *, machine_mode
);
91 static int target_char_cast (tree
, char *);
92 static rtx
get_memory_rtx (tree
, tree
);
93 static int apply_args_size (void);
94 static int apply_result_size (void);
95 static rtx
result_vector (int, rtx
);
96 static void expand_builtin_prefetch (tree
);
97 static rtx
expand_builtin_apply_args (void);
98 static rtx
expand_builtin_apply_args_1 (void);
99 static rtx
expand_builtin_apply (rtx
, rtx
, rtx
);
100 static void expand_builtin_return (rtx
);
101 static enum type_class
type_to_class (tree
);
102 static rtx
expand_builtin_classify_type (tree
);
103 static void expand_errno_check (tree
, rtx
);
104 static rtx
expand_builtin_mathfn (tree
, rtx
, rtx
);
105 static rtx
expand_builtin_mathfn_2 (tree
, rtx
, rtx
);
106 static rtx
expand_builtin_mathfn_3 (tree
, rtx
, rtx
);
107 static rtx
expand_builtin_mathfn_ternary (tree
, rtx
, rtx
);
108 static rtx
expand_builtin_interclass_mathfn (tree
, rtx
);
109 static rtx
expand_builtin_sincos (tree
);
110 static rtx
expand_builtin_cexpi (tree
, rtx
);
111 static rtx
expand_builtin_int_roundingfn (tree
, rtx
);
112 static rtx
expand_builtin_int_roundingfn_2 (tree
, rtx
);
113 static rtx
expand_builtin_next_arg (void);
114 static rtx
expand_builtin_va_start (tree
);
115 static rtx
expand_builtin_va_end (tree
);
116 static rtx
expand_builtin_va_copy (tree
);
117 static rtx
expand_builtin_strcmp (tree
, rtx
);
118 static rtx
expand_builtin_strncmp (tree
, rtx
, machine_mode
);
119 static rtx
builtin_memcpy_read_str (void *, HOST_WIDE_INT
, machine_mode
);
120 static rtx
expand_builtin_memcpy (tree
, rtx
);
121 static rtx
expand_builtin_memcpy_with_bounds (tree
, rtx
);
122 static rtx
expand_builtin_memcpy_args (tree
, tree
, tree
, rtx
, tree
);
123 static rtx
expand_builtin_mempcpy (tree
, rtx
, machine_mode
);
124 static rtx
expand_builtin_mempcpy_with_bounds (tree
, rtx
, machine_mode
);
125 static rtx
expand_builtin_mempcpy_args (tree
, tree
, tree
, rtx
,
126 machine_mode
, int, tree
);
127 static rtx
expand_builtin_strcpy (tree
, rtx
);
128 static rtx
expand_builtin_strcpy_args (tree
, tree
, rtx
);
129 static rtx
expand_builtin_stpcpy (tree
, rtx
, machine_mode
);
130 static rtx
expand_builtin_strncpy (tree
, rtx
);
131 static rtx
builtin_memset_gen_str (void *, HOST_WIDE_INT
, machine_mode
);
132 static rtx
expand_builtin_memset (tree
, rtx
, machine_mode
);
133 static rtx
expand_builtin_memset_with_bounds (tree
, rtx
, machine_mode
);
134 static rtx
expand_builtin_memset_args (tree
, tree
, tree
, rtx
, machine_mode
, tree
);
135 static rtx
expand_builtin_bzero (tree
);
136 static rtx
expand_builtin_strlen (tree
, rtx
, machine_mode
);
137 static rtx
expand_builtin_alloca (tree
, bool);
138 static rtx
expand_builtin_unop (machine_mode
, tree
, rtx
, rtx
, optab
);
139 static rtx
expand_builtin_frame_address (tree
, tree
);
140 static tree
stabilize_va_list_loc (location_t
, tree
, int);
141 static rtx
expand_builtin_expect (tree
, rtx
);
142 static tree
fold_builtin_constant_p (tree
);
143 static tree
fold_builtin_classify_type (tree
);
144 static tree
fold_builtin_strlen (location_t
, tree
, tree
);
145 static tree
fold_builtin_inf (location_t
, tree
, int);
146 static tree
fold_builtin_nan (tree
, tree
, int);
147 static tree
rewrite_call_expr (location_t
, tree
, int, tree
, int, ...);
148 static bool validate_arg (const_tree
, enum tree_code code
);
149 static rtx
expand_builtin_fabs (tree
, rtx
, rtx
);
150 static rtx
expand_builtin_signbit (tree
, rtx
);
151 static tree
fold_builtin_bitop (tree
, tree
);
152 static tree
fold_builtin_strchr (location_t
, tree
, tree
, tree
);
153 static tree
fold_builtin_memchr (location_t
, tree
, tree
, tree
, tree
);
154 static tree
fold_builtin_memcmp (location_t
, tree
, tree
, tree
);
155 static tree
fold_builtin_strcmp (location_t
, tree
, tree
);
156 static tree
fold_builtin_strncmp (location_t
, tree
, tree
, tree
);
157 static tree
fold_builtin_isascii (location_t
, tree
);
158 static tree
fold_builtin_toascii (location_t
, tree
);
159 static tree
fold_builtin_isdigit (location_t
, tree
);
160 static tree
fold_builtin_fabs (location_t
, tree
, tree
);
161 static tree
fold_builtin_abs (location_t
, tree
, tree
);
162 static tree
fold_builtin_unordered_cmp (location_t
, tree
, tree
, tree
, enum tree_code
,
164 static tree
fold_builtin_0 (location_t
, tree
);
165 static tree
fold_builtin_1 (location_t
, tree
, tree
);
166 static tree
fold_builtin_2 (location_t
, tree
, tree
, tree
);
167 static tree
fold_builtin_3 (location_t
, tree
, tree
, tree
, tree
);
168 static tree
fold_builtin_varargs (location_t
, tree
, tree
*, int);
170 static tree
fold_builtin_strpbrk (location_t
, tree
, tree
, tree
);
171 static tree
fold_builtin_strstr (location_t
, tree
, tree
, tree
);
172 static tree
fold_builtin_strrchr (location_t
, tree
, tree
, tree
);
173 static tree
fold_builtin_strspn (location_t
, tree
, tree
);
174 static tree
fold_builtin_strcspn (location_t
, tree
, tree
);
176 static rtx
expand_builtin_object_size (tree
);
177 static rtx
expand_builtin_memory_chk (tree
, rtx
, machine_mode
,
178 enum built_in_function
);
179 static void maybe_emit_chk_warning (tree
, enum built_in_function
);
180 static void maybe_emit_sprintf_chk_warning (tree
, enum built_in_function
);
181 static void maybe_emit_free_warning (tree
);
182 static tree
fold_builtin_object_size (tree
, tree
);
184 unsigned HOST_WIDE_INT target_newline
;
185 unsigned HOST_WIDE_INT target_percent
;
186 static unsigned HOST_WIDE_INT target_c
;
187 static unsigned HOST_WIDE_INT target_s
;
188 char target_percent_c
[3];
189 char target_percent_s
[3];
190 char target_percent_s_newline
[4];
191 static tree
do_mpfr_remquo (tree
, tree
, tree
);
192 static tree
do_mpfr_lgamma_r (tree
, tree
, tree
);
193 static void expand_builtin_sync_synchronize (void);
195 /* Return true if NAME starts with __builtin_ or __sync_. */
198 is_builtin_name (const char *name
)
200 if (strncmp (name
, "__builtin_", 10) == 0)
202 if (strncmp (name
, "__sync_", 7) == 0)
204 if (strncmp (name
, "__atomic_", 9) == 0)
207 && (!strcmp (name
, "__cilkrts_detach")
208 || !strcmp (name
, "__cilkrts_pop_frame")))
214 /* Return true if DECL is a function symbol representing a built-in. */
217 is_builtin_fn (tree decl
)
219 return TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_BUILT_IN (decl
);
222 /* Return true if NODE should be considered for inline expansion regardless
223 of the optimization level. This means whenever a function is invoked with
224 its "internal" name, which normally contains the prefix "__builtin". */
227 called_as_built_in (tree node
)
229 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
230 we want the name used to call the function, not the name it
232 const char *name
= IDENTIFIER_POINTER (DECL_NAME (node
));
233 return is_builtin_name (name
);
236 /* Compute values M and N such that M divides (address of EXP - N) and such
237 that N < M. If these numbers can be determined, store M in alignp and N in
238 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
239 *alignp and any bit-offset to *bitposp.
241 Note that the address (and thus the alignment) computed here is based
242 on the address to which a symbol resolves, whereas DECL_ALIGN is based
243 on the address at which an object is actually located. These two
244 addresses are not always the same. For example, on ARM targets,
245 the address &foo of a Thumb function foo() has the lowest bit set,
246 whereas foo() itself starts on an even address.
248 If ADDR_P is true we are taking the address of the memory reference EXP
249 and thus cannot rely on the access taking place. */
252 get_object_alignment_2 (tree exp
, unsigned int *alignp
,
253 unsigned HOST_WIDE_INT
*bitposp
, bool addr_p
)
255 HOST_WIDE_INT bitsize
, bitpos
;
258 int unsignedp
, volatilep
;
259 unsigned int align
= BITS_PER_UNIT
;
260 bool known_alignment
= false;
262 /* Get the innermost object and the constant (bitpos) and possibly
263 variable (offset) offset of the access. */
264 exp
= get_inner_reference (exp
, &bitsize
, &bitpos
, &offset
,
265 &mode
, &unsignedp
, &volatilep
, true);
267 /* Extract alignment information from the innermost object and
268 possibly adjust bitpos and offset. */
269 if (TREE_CODE (exp
) == FUNCTION_DECL
)
271 /* Function addresses can encode extra information besides their
272 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
273 allows the low bit to be used as a virtual bit, we know
274 that the address itself must be at least 2-byte aligned. */
275 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_pfn
)
276 align
= 2 * BITS_PER_UNIT
;
278 else if (TREE_CODE (exp
) == LABEL_DECL
)
280 else if (TREE_CODE (exp
) == CONST_DECL
)
282 /* The alignment of a CONST_DECL is determined by its initializer. */
283 exp
= DECL_INITIAL (exp
);
284 align
= TYPE_ALIGN (TREE_TYPE (exp
));
285 if (CONSTANT_CLASS_P (exp
))
286 align
= (unsigned) CONSTANT_ALIGNMENT (exp
, align
);
288 known_alignment
= true;
290 else if (DECL_P (exp
))
292 align
= DECL_ALIGN (exp
);
293 known_alignment
= true;
295 else if (TREE_CODE (exp
) == VIEW_CONVERT_EXPR
)
297 align
= TYPE_ALIGN (TREE_TYPE (exp
));
299 else if (TREE_CODE (exp
) == INDIRECT_REF
300 || TREE_CODE (exp
) == MEM_REF
301 || TREE_CODE (exp
) == TARGET_MEM_REF
)
303 tree addr
= TREE_OPERAND (exp
, 0);
305 unsigned HOST_WIDE_INT ptr_bitpos
;
306 unsigned HOST_WIDE_INT ptr_bitmask
= ~0;
308 /* If the address is explicitely aligned, handle that. */
309 if (TREE_CODE (addr
) == BIT_AND_EXPR
310 && TREE_CODE (TREE_OPERAND (addr
, 1)) == INTEGER_CST
)
312 ptr_bitmask
= TREE_INT_CST_LOW (TREE_OPERAND (addr
, 1));
313 ptr_bitmask
*= BITS_PER_UNIT
;
314 align
= ptr_bitmask
& -ptr_bitmask
;
315 addr
= TREE_OPERAND (addr
, 0);
319 = get_pointer_alignment_1 (addr
, &ptr_align
, &ptr_bitpos
);
320 align
= MAX (ptr_align
, align
);
322 /* Re-apply explicit alignment to the bitpos. */
323 ptr_bitpos
&= ptr_bitmask
;
325 /* The alignment of the pointer operand in a TARGET_MEM_REF
326 has to take the variable offset parts into account. */
327 if (TREE_CODE (exp
) == TARGET_MEM_REF
)
331 unsigned HOST_WIDE_INT step
= 1;
333 step
= TREE_INT_CST_LOW (TMR_STEP (exp
));
334 align
= MIN (align
, (step
& -step
) * BITS_PER_UNIT
);
336 if (TMR_INDEX2 (exp
))
337 align
= BITS_PER_UNIT
;
338 known_alignment
= false;
341 /* When EXP is an actual memory reference then we can use
342 TYPE_ALIGN of a pointer indirection to derive alignment.
343 Do so only if get_pointer_alignment_1 did not reveal absolute
344 alignment knowledge and if using that alignment would
345 improve the situation. */
346 if (!addr_p
&& !known_alignment
347 && TYPE_ALIGN (TREE_TYPE (exp
)) > align
)
348 align
= TYPE_ALIGN (TREE_TYPE (exp
));
351 /* Else adjust bitpos accordingly. */
352 bitpos
+= ptr_bitpos
;
353 if (TREE_CODE (exp
) == MEM_REF
354 || TREE_CODE (exp
) == TARGET_MEM_REF
)
355 bitpos
+= mem_ref_offset (exp
).to_short_addr () * BITS_PER_UNIT
;
358 else if (TREE_CODE (exp
) == STRING_CST
)
360 /* STRING_CST are the only constant objects we allow to be not
361 wrapped inside a CONST_DECL. */
362 align
= TYPE_ALIGN (TREE_TYPE (exp
));
363 if (CONSTANT_CLASS_P (exp
))
364 align
= (unsigned) CONSTANT_ALIGNMENT (exp
, align
);
366 known_alignment
= true;
369 /* If there is a non-constant offset part extract the maximum
370 alignment that can prevail. */
373 unsigned int trailing_zeros
= tree_ctz (offset
);
374 if (trailing_zeros
< HOST_BITS_PER_INT
)
376 unsigned int inner
= (1U << trailing_zeros
) * BITS_PER_UNIT
;
378 align
= MIN (align
, inner
);
383 *bitposp
= bitpos
& (*alignp
- 1);
384 return known_alignment
;
387 /* For a memory reference expression EXP compute values M and N such that M
388 divides (&EXP - N) and such that N < M. If these numbers can be determined,
389 store M in alignp and N in *BITPOSP and return true. Otherwise return false
390 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
393 get_object_alignment_1 (tree exp
, unsigned int *alignp
,
394 unsigned HOST_WIDE_INT
*bitposp
)
396 return get_object_alignment_2 (exp
, alignp
, bitposp
, false);
399 /* Return the alignment in bits of EXP, an object. */
402 get_object_alignment (tree exp
)
404 unsigned HOST_WIDE_INT bitpos
= 0;
407 get_object_alignment_1 (exp
, &align
, &bitpos
);
409 /* align and bitpos now specify known low bits of the pointer.
410 ptr & (align - 1) == bitpos. */
413 align
= (bitpos
& -bitpos
);
417 /* For a pointer valued expression EXP compute values M and N such that M
418 divides (EXP - N) and such that N < M. If these numbers can be determined,
419 store M in alignp and N in *BITPOSP and return true. Return false if
420 the results are just a conservative approximation.
422 If EXP is not a pointer, false is returned too. */
425 get_pointer_alignment_1 (tree exp
, unsigned int *alignp
,
426 unsigned HOST_WIDE_INT
*bitposp
)
430 if (TREE_CODE (exp
) == ADDR_EXPR
)
431 return get_object_alignment_2 (TREE_OPERAND (exp
, 0),
432 alignp
, bitposp
, true);
433 else if (TREE_CODE (exp
) == POINTER_PLUS_EXPR
)
436 unsigned HOST_WIDE_INT bitpos
;
437 bool res
= get_pointer_alignment_1 (TREE_OPERAND (exp
, 0),
439 if (TREE_CODE (TREE_OPERAND (exp
, 1)) == INTEGER_CST
)
440 bitpos
+= TREE_INT_CST_LOW (TREE_OPERAND (exp
, 1)) * BITS_PER_UNIT
;
443 unsigned int trailing_zeros
= tree_ctz (TREE_OPERAND (exp
, 1));
444 if (trailing_zeros
< HOST_BITS_PER_INT
)
446 unsigned int inner
= (1U << trailing_zeros
) * BITS_PER_UNIT
;
448 align
= MIN (align
, inner
);
452 *bitposp
= bitpos
& (align
- 1);
455 else if (TREE_CODE (exp
) == SSA_NAME
456 && POINTER_TYPE_P (TREE_TYPE (exp
)))
458 unsigned int ptr_align
, ptr_misalign
;
459 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (exp
);
461 if (pi
&& get_ptr_info_alignment (pi
, &ptr_align
, &ptr_misalign
))
463 *bitposp
= ptr_misalign
* BITS_PER_UNIT
;
464 *alignp
= ptr_align
* BITS_PER_UNIT
;
465 /* We cannot really tell whether this result is an approximation. */
471 *alignp
= BITS_PER_UNIT
;
475 else if (TREE_CODE (exp
) == INTEGER_CST
)
477 *alignp
= BIGGEST_ALIGNMENT
;
478 *bitposp
= ((TREE_INT_CST_LOW (exp
) * BITS_PER_UNIT
)
479 & (BIGGEST_ALIGNMENT
- 1));
484 *alignp
= BITS_PER_UNIT
;
488 /* Return the alignment in bits of EXP, a pointer valued expression.
489 The alignment returned is, by default, the alignment of the thing that
490 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
492 Otherwise, look at the expression to see if we can do better, i.e., if the
493 expression is actually pointing at an object whose alignment is tighter. */
496 get_pointer_alignment (tree exp
)
498 unsigned HOST_WIDE_INT bitpos
= 0;
501 get_pointer_alignment_1 (exp
, &align
, &bitpos
);
503 /* align and bitpos now specify known low bits of the pointer.
504 ptr & (align - 1) == bitpos. */
507 align
= (bitpos
& -bitpos
);
512 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
513 way, because it could contain a zero byte in the middle.
514 TREE_STRING_LENGTH is the size of the character array, not the string.
516 ONLY_VALUE should be nonzero if the result is not going to be emitted
517 into the instruction stream and zero if it is going to be expanded.
518 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
519 is returned, otherwise NULL, since
520 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
521 evaluate the side-effects.
523 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
524 accesses. Note that this implies the result is not going to be emitted
525 into the instruction stream.
527 The value returned is of type `ssizetype'.
529 Unfortunately, string_constant can't access the values of const char
530 arrays with initializers, so neither can we do so here. */
533 c_strlen (tree src
, int only_value
)
536 HOST_WIDE_INT offset
;
542 if (TREE_CODE (src
) == COND_EXPR
543 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
547 len1
= c_strlen (TREE_OPERAND (src
, 1), only_value
);
548 len2
= c_strlen (TREE_OPERAND (src
, 2), only_value
);
549 if (tree_int_cst_equal (len1
, len2
))
553 if (TREE_CODE (src
) == COMPOUND_EXPR
554 && (only_value
|| !TREE_SIDE_EFFECTS (TREE_OPERAND (src
, 0))))
555 return c_strlen (TREE_OPERAND (src
, 1), only_value
);
557 loc
= EXPR_LOC_OR_LOC (src
, input_location
);
559 src
= string_constant (src
, &offset_node
);
563 max
= TREE_STRING_LENGTH (src
) - 1;
564 ptr
= TREE_STRING_POINTER (src
);
566 if (offset_node
&& TREE_CODE (offset_node
) != INTEGER_CST
)
568 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
569 compute the offset to the following null if we don't know where to
570 start searching for it. */
573 for (i
= 0; i
< max
; i
++)
577 /* We don't know the starting offset, but we do know that the string
578 has no internal zero bytes. We can assume that the offset falls
579 within the bounds of the string; otherwise, the programmer deserves
580 what he gets. Subtract the offset from the length of the string,
581 and return that. This would perhaps not be valid if we were dealing
582 with named arrays in addition to literal string constants. */
584 return size_diffop_loc (loc
, size_int (max
), offset_node
);
587 /* We have a known offset into the string. Start searching there for
588 a null character if we can represent it as a single HOST_WIDE_INT. */
589 if (offset_node
== 0)
591 else if (! tree_fits_shwi_p (offset_node
))
594 offset
= tree_to_shwi (offset_node
);
596 /* If the offset is known to be out of bounds, warn, and call strlen at
598 if (offset
< 0 || offset
> max
)
600 /* Suppress multiple warnings for propagated constant strings. */
602 && !TREE_NO_WARNING (src
))
604 warning_at (loc
, 0, "offset outside bounds of constant string");
605 TREE_NO_WARNING (src
) = 1;
610 /* Use strlen to search for the first zero byte. Since any strings
611 constructed with build_string will have nulls appended, we win even
612 if we get handed something like (char[4])"abcd".
614 Since OFFSET is our starting index into the string, no further
615 calculation is needed. */
616 return ssize_int (strlen (ptr
+ offset
));
619 /* Return a constant integer corresponding to target reading
620 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
623 c_readstr (const char *str
, machine_mode mode
)
627 HOST_WIDE_INT tmp
[MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
];
629 gcc_assert (GET_MODE_CLASS (mode
) == MODE_INT
);
630 unsigned int len
= (GET_MODE_PRECISION (mode
) + HOST_BITS_PER_WIDE_INT
- 1)
631 / HOST_BITS_PER_WIDE_INT
;
633 gcc_assert (len
<= MAX_BITSIZE_MODE_ANY_INT
/ HOST_BITS_PER_WIDE_INT
);
634 for (i
= 0; i
< len
; i
++)
638 for (i
= 0; i
< GET_MODE_SIZE (mode
); i
++)
641 if (WORDS_BIG_ENDIAN
)
642 j
= GET_MODE_SIZE (mode
) - i
- 1;
643 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
644 && GET_MODE_SIZE (mode
) >= UNITS_PER_WORD
)
645 j
= j
+ UNITS_PER_WORD
- 2 * (j
% UNITS_PER_WORD
) - 1;
649 ch
= (unsigned char) str
[i
];
650 tmp
[j
/ HOST_BITS_PER_WIDE_INT
] |= ch
<< (j
% HOST_BITS_PER_WIDE_INT
);
653 wide_int c
= wide_int::from_array (tmp
, len
, GET_MODE_PRECISION (mode
));
654 return immed_wide_int_const (c
, mode
);
657 /* Cast a target constant CST to target CHAR and if that value fits into
658 host char type, return zero and put that value into variable pointed to by
662 target_char_cast (tree cst
, char *p
)
664 unsigned HOST_WIDE_INT val
, hostval
;
666 if (TREE_CODE (cst
) != INTEGER_CST
667 || CHAR_TYPE_SIZE
> HOST_BITS_PER_WIDE_INT
)
670 /* Do not care if it fits or not right here. */
671 val
= TREE_INT_CST_LOW (cst
);
673 if (CHAR_TYPE_SIZE
< HOST_BITS_PER_WIDE_INT
)
674 val
&= (((unsigned HOST_WIDE_INT
) 1) << CHAR_TYPE_SIZE
) - 1;
677 if (HOST_BITS_PER_CHAR
< HOST_BITS_PER_WIDE_INT
)
678 hostval
&= (((unsigned HOST_WIDE_INT
) 1) << HOST_BITS_PER_CHAR
) - 1;
687 /* Similar to save_expr, but assumes that arbitrary code is not executed
688 in between the multiple evaluations. In particular, we assume that a
689 non-addressable local variable will not be modified. */
692 builtin_save_expr (tree exp
)
694 if (TREE_CODE (exp
) == SSA_NAME
695 || (TREE_ADDRESSABLE (exp
) == 0
696 && (TREE_CODE (exp
) == PARM_DECL
697 || (TREE_CODE (exp
) == VAR_DECL
&& !TREE_STATIC (exp
)))))
700 return save_expr (exp
);
703 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
704 times to get the address of either a higher stack frame, or a return
705 address located within it (depending on FNDECL_CODE). */
708 expand_builtin_return_addr (enum built_in_function fndecl_code
, int count
)
711 rtx tem
= INITIAL_FRAME_ADDRESS_RTX
;
714 /* For a zero count with __builtin_return_address, we don't care what
715 frame address we return, because target-specific definitions will
716 override us. Therefore frame pointer elimination is OK, and using
717 the soft frame pointer is OK.
719 For a nonzero count, or a zero count with __builtin_frame_address,
720 we require a stable offset from the current frame pointer to the
721 previous one, so we must use the hard frame pointer, and
722 we must disable frame pointer elimination. */
723 if (count
== 0 && fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
724 tem
= frame_pointer_rtx
;
727 tem
= hard_frame_pointer_rtx
;
729 /* Tell reload not to eliminate the frame pointer. */
730 crtl
->accesses_prior_frames
= 1;
735 SETUP_FRAME_ADDRESSES ();
737 /* On the SPARC, the return address is not in the frame, it is in a
738 register. There is no way to access it off of the current frame
739 pointer, but it can be accessed off the previous frame pointer by
740 reading the value from the register window save area. */
741 if (RETURN_ADDR_IN_PREVIOUS_FRAME
&& fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
744 /* Scan back COUNT frames to the specified frame. */
745 for (i
= 0; i
< count
; i
++)
747 /* Assume the dynamic chain pointer is in the word that the
748 frame address points to, unless otherwise specified. */
749 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
750 tem
= memory_address (Pmode
, tem
);
751 tem
= gen_frame_mem (Pmode
, tem
);
752 tem
= copy_to_reg (tem
);
755 /* For __builtin_frame_address, return what we've got. But, on
756 the SPARC for example, we may have to add a bias. */
757 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
758 return FRAME_ADDR_RTX (tem
);
760 /* For __builtin_return_address, get the return address from that frame. */
761 #ifdef RETURN_ADDR_RTX
762 tem
= RETURN_ADDR_RTX (count
, tem
);
764 tem
= memory_address (Pmode
,
765 plus_constant (Pmode
, tem
, GET_MODE_SIZE (Pmode
)));
766 tem
= gen_frame_mem (Pmode
, tem
);
771 /* Alias set used for setjmp buffer. */
772 static alias_set_type setjmp_alias_set
= -1;
774 /* Construct the leading half of a __builtin_setjmp call. Control will
775 return to RECEIVER_LABEL. This is also called directly by the SJLJ
776 exception handling code. */
779 expand_builtin_setjmp_setup (rtx buf_addr
, rtx receiver_label
)
781 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
785 if (setjmp_alias_set
== -1)
786 setjmp_alias_set
= new_alias_set ();
788 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
790 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
792 /* We store the frame pointer and the address of receiver_label in
793 the buffer and use the rest of it for the stack save area, which
794 is machine-dependent. */
796 mem
= gen_rtx_MEM (Pmode
, buf_addr
);
797 set_mem_alias_set (mem
, setjmp_alias_set
);
798 emit_move_insn (mem
, targetm
.builtin_setjmp_frame_value ());
800 mem
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
801 GET_MODE_SIZE (Pmode
))),
802 set_mem_alias_set (mem
, setjmp_alias_set
);
804 emit_move_insn (validize_mem (mem
),
805 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, receiver_label
)));
807 stack_save
= gen_rtx_MEM (sa_mode
,
808 plus_constant (Pmode
, buf_addr
,
809 2 * GET_MODE_SIZE (Pmode
)));
810 set_mem_alias_set (stack_save
, setjmp_alias_set
);
811 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
813 /* If there is further processing to do, do it. */
814 if (targetm
.have_builtin_setjmp_setup ())
815 emit_insn (targetm
.gen_builtin_setjmp_setup (buf_addr
));
817 /* We have a nonlocal label. */
818 cfun
->has_nonlocal_label
= 1;
821 /* Construct the trailing part of a __builtin_setjmp call. This is
822 also called directly by the SJLJ exception handling code.
823 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
826 expand_builtin_setjmp_receiver (rtx receiver_label
)
830 /* Mark the FP as used when we get here, so we have to make sure it's
831 marked as used by this function. */
832 emit_use (hard_frame_pointer_rtx
);
834 /* Mark the static chain as clobbered here so life information
835 doesn't get messed up for it. */
836 chain
= targetm
.calls
.static_chain (current_function_decl
, true);
837 if (chain
&& REG_P (chain
))
838 emit_clobber (chain
);
840 /* Now put in the code to restore the frame pointer, and argument
841 pointer, if needed. */
842 if (! targetm
.have_nonlocal_goto ())
844 /* First adjust our frame pointer to its actual value. It was
845 previously set to the start of the virtual area corresponding to
846 the stacked variables when we branched here and now needs to be
847 adjusted to the actual hardware fp value.
849 Assignments to virtual registers are converted by
850 instantiate_virtual_regs into the corresponding assignment
851 to the underlying register (fp in this case) that makes
852 the original assignment true.
853 So the following insn will actually be decrementing fp by
854 STARTING_FRAME_OFFSET. */
855 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
857 /* Restoring the frame pointer also modifies the hard frame pointer.
858 Mark it used (so that the previous assignment remains live once
859 the frame pointer is eliminated) and clobbered (to represent the
860 implicit update from the assignment). */
861 emit_use (hard_frame_pointer_rtx
);
862 emit_clobber (hard_frame_pointer_rtx
);
865 if (!HARD_FRAME_POINTER_IS_ARG_POINTER
&& fixed_regs
[ARG_POINTER_REGNUM
])
867 #ifdef ELIMINABLE_REGS
868 /* If the argument pointer can be eliminated in favor of the
869 frame pointer, we don't need to restore it. We assume here
870 that if such an elimination is present, it can always be used.
871 This is the case on all known machines; if we don't make this
872 assumption, we do unnecessary saving on many machines. */
874 static const struct elims
{const int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
876 for (i
= 0; i
< ARRAY_SIZE (elim_regs
); i
++)
877 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
878 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
881 if (i
== ARRAY_SIZE (elim_regs
))
884 /* Now restore our arg pointer from the address at which it
885 was saved in our stack frame. */
886 emit_move_insn (crtl
->args
.internal_arg_pointer
,
887 copy_to_reg (get_arg_pointer_save_area ()));
891 if (receiver_label
!= NULL
&& targetm
.have_builtin_setjmp_receiver ())
892 emit_insn (targetm
.gen_builtin_setjmp_receiver (receiver_label
));
893 else if (targetm
.have_nonlocal_goto_receiver ())
894 emit_insn (targetm
.gen_nonlocal_goto_receiver ());
898 /* We must not allow the code we just generated to be reordered by
899 scheduling. Specifically, the update of the frame pointer must
900 happen immediately, not later. */
901 emit_insn (gen_blockage ());
904 /* __builtin_longjmp is passed a pointer to an array of five words (not
905 all will be used on all machines). It operates similarly to the C
906 library function of the same name, but is more efficient. Much of
907 the code below is copied from the handling of non-local gotos. */
910 expand_builtin_longjmp (rtx buf_addr
, rtx value
)
913 rtx_insn
*insn
, *last
;
914 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
916 /* DRAP is needed for stack realign if longjmp is expanded to current
918 if (SUPPORTS_STACK_ALIGNMENT
)
919 crtl
->need_drap
= true;
921 if (setjmp_alias_set
== -1)
922 setjmp_alias_set
= new_alias_set ();
924 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
926 buf_addr
= force_reg (Pmode
, buf_addr
);
928 /* We require that the user must pass a second argument of 1, because
929 that is what builtin_setjmp will return. */
930 gcc_assert (value
== const1_rtx
);
932 last
= get_last_insn ();
933 if (targetm
.have_builtin_longjmp ())
934 emit_insn (targetm
.gen_builtin_longjmp (buf_addr
));
937 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
938 lab
= gen_rtx_MEM (Pmode
, plus_constant (Pmode
, buf_addr
,
939 GET_MODE_SIZE (Pmode
)));
941 stack
= gen_rtx_MEM (sa_mode
, plus_constant (Pmode
, buf_addr
,
942 2 * GET_MODE_SIZE (Pmode
)));
943 set_mem_alias_set (fp
, setjmp_alias_set
);
944 set_mem_alias_set (lab
, setjmp_alias_set
);
945 set_mem_alias_set (stack
, setjmp_alias_set
);
947 /* Pick up FP, label, and SP from the block and jump. This code is
948 from expand_goto in stmt.c; see there for detailed comments. */
949 if (targetm
.have_nonlocal_goto ())
950 /* We have to pass a value to the nonlocal_goto pattern that will
951 get copied into the static_chain pointer, but it does not matter
952 what that value is, because builtin_setjmp does not use it. */
953 emit_insn (targetm
.gen_nonlocal_goto (value
, lab
, stack
, fp
));
956 lab
= copy_to_reg (lab
);
958 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
959 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
961 emit_move_insn (hard_frame_pointer_rtx
, fp
);
962 emit_stack_restore (SAVE_NONLOCAL
, stack
);
964 emit_use (hard_frame_pointer_rtx
);
965 emit_use (stack_pointer_rtx
);
966 emit_indirect_jump (lab
);
970 /* Search backwards and mark the jump insn as a non-local goto.
971 Note that this precludes the use of __builtin_longjmp to a
972 __builtin_setjmp target in the same function. However, we've
973 already cautioned the user that these functions are for
974 internal exception handling use only. */
975 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
977 gcc_assert (insn
!= last
);
981 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
984 else if (CALL_P (insn
))
990 more_const_call_expr_args_p (const const_call_expr_arg_iterator
*iter
)
992 return (iter
->i
< iter
->n
);
995 /* This function validates the types of a function call argument list
996 against a specified list of tree_codes. If the last specifier is a 0,
997 that represents an ellipses, otherwise the last specifier must be a
1001 validate_arglist (const_tree callexpr
, ...)
1003 enum tree_code code
;
1006 const_call_expr_arg_iterator iter
;
1009 va_start (ap
, callexpr
);
1010 init_const_call_expr_arg_iterator (callexpr
, &iter
);
1014 code
= (enum tree_code
) va_arg (ap
, int);
1018 /* This signifies an ellipses, any further arguments are all ok. */
1022 /* This signifies an endlink, if no arguments remain, return
1023 true, otherwise return false. */
1024 res
= !more_const_call_expr_args_p (&iter
);
1027 /* If no parameters remain or the parameter's code does not
1028 match the specified code, return false. Otherwise continue
1029 checking any remaining arguments. */
1030 arg
= next_const_call_expr_arg (&iter
);
1031 if (!validate_arg (arg
, code
))
1038 /* We need gotos here since we can only have one VA_CLOSE in a
1046 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1047 and the address of the save area. */
1050 expand_builtin_nonlocal_goto (tree exp
)
1052 tree t_label
, t_save_area
;
1053 rtx r_label
, r_save_area
, r_fp
, r_sp
;
1056 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
1059 t_label
= CALL_EXPR_ARG (exp
, 0);
1060 t_save_area
= CALL_EXPR_ARG (exp
, 1);
1062 r_label
= expand_normal (t_label
);
1063 r_label
= convert_memory_address (Pmode
, r_label
);
1064 r_save_area
= expand_normal (t_save_area
);
1065 r_save_area
= convert_memory_address (Pmode
, r_save_area
);
1066 /* Copy the address of the save location to a register just in case it was
1067 based on the frame pointer. */
1068 r_save_area
= copy_to_reg (r_save_area
);
1069 r_fp
= gen_rtx_MEM (Pmode
, r_save_area
);
1070 r_sp
= gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
),
1071 plus_constant (Pmode
, r_save_area
,
1072 GET_MODE_SIZE (Pmode
)));
1074 crtl
->has_nonlocal_goto
= 1;
1076 /* ??? We no longer need to pass the static chain value, afaik. */
1077 if (targetm
.have_nonlocal_goto ())
1078 emit_insn (targetm
.gen_nonlocal_goto (const0_rtx
, r_label
, r_sp
, r_fp
));
1081 r_label
= copy_to_reg (r_label
);
1083 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1084 emit_clobber (gen_rtx_MEM (BLKmode
, hard_frame_pointer_rtx
));
1086 /* Restore frame pointer for containing function. */
1087 emit_move_insn (hard_frame_pointer_rtx
, r_fp
);
1088 emit_stack_restore (SAVE_NONLOCAL
, r_sp
);
1090 /* USE of hard_frame_pointer_rtx added for consistency;
1091 not clear if really needed. */
1092 emit_use (hard_frame_pointer_rtx
);
1093 emit_use (stack_pointer_rtx
);
1095 /* If the architecture is using a GP register, we must
1096 conservatively assume that the target function makes use of it.
1097 The prologue of functions with nonlocal gotos must therefore
1098 initialize the GP register to the appropriate value, and we
1099 must then make sure that this value is live at the point
1100 of the jump. (Note that this doesn't necessarily apply
1101 to targets with a nonlocal_goto pattern; they are free
1102 to implement it in their own way. Note also that this is
1103 a no-op if the GP register is a global invariant.) */
1104 if ((unsigned) PIC_OFFSET_TABLE_REGNUM
!= INVALID_REGNUM
1105 && fixed_regs
[PIC_OFFSET_TABLE_REGNUM
])
1106 emit_use (pic_offset_table_rtx
);
1108 emit_indirect_jump (r_label
);
1111 /* Search backwards to the jump insn and mark it as a
1113 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
1117 add_reg_note (insn
, REG_NON_LOCAL_GOTO
, const0_rtx
);
1120 else if (CALL_P (insn
))
1127 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1128 (not all will be used on all machines) that was passed to __builtin_setjmp.
1129 It updates the stack pointer in that block to the current value. This is
1130 also called directly by the SJLJ exception handling code. */
1133 expand_builtin_update_setjmp_buf (rtx buf_addr
)
1135 machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
1137 = gen_rtx_MEM (sa_mode
,
1140 plus_constant (Pmode
, buf_addr
,
1141 2 * GET_MODE_SIZE (Pmode
))));
1143 emit_stack_save (SAVE_NONLOCAL
, &stack_save
);
1146 /* Expand a call to __builtin_prefetch. For a target that does not support
1147 data prefetch, evaluate the memory address argument in case it has side
1151 expand_builtin_prefetch (tree exp
)
1153 tree arg0
, arg1
, arg2
;
1157 if (!validate_arglist (exp
, POINTER_TYPE
, 0))
1160 arg0
= CALL_EXPR_ARG (exp
, 0);
1162 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1163 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1165 nargs
= call_expr_nargs (exp
);
1167 arg1
= CALL_EXPR_ARG (exp
, 1);
1169 arg1
= integer_zero_node
;
1171 arg2
= CALL_EXPR_ARG (exp
, 2);
1173 arg2
= integer_three_node
;
1175 /* Argument 0 is an address. */
1176 op0
= expand_expr (arg0
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
1178 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1179 if (TREE_CODE (arg1
) != INTEGER_CST
)
1181 error ("second argument to %<__builtin_prefetch%> must be a constant");
1182 arg1
= integer_zero_node
;
1184 op1
= expand_normal (arg1
);
1185 /* Argument 1 must be either zero or one. */
1186 if (INTVAL (op1
) != 0 && INTVAL (op1
) != 1)
1188 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1193 /* Argument 2 (locality) must be a compile-time constant int. */
1194 if (TREE_CODE (arg2
) != INTEGER_CST
)
1196 error ("third argument to %<__builtin_prefetch%> must be a constant");
1197 arg2
= integer_zero_node
;
1199 op2
= expand_normal (arg2
);
1200 /* Argument 2 must be 0, 1, 2, or 3. */
1201 if (INTVAL (op2
) < 0 || INTVAL (op2
) > 3)
1203 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1207 if (targetm
.have_prefetch ())
1209 struct expand_operand ops
[3];
1211 create_address_operand (&ops
[0], op0
);
1212 create_integer_operand (&ops
[1], INTVAL (op1
));
1213 create_integer_operand (&ops
[2], INTVAL (op2
));
1214 if (maybe_expand_insn (targetm
.code_for_prefetch
, 3, ops
))
1218 /* Don't do anything with direct references to volatile memory, but
1219 generate code to handle other side effects. */
1220 if (!MEM_P (op0
) && side_effects_p (op0
))
1224 /* Get a MEM rtx for expression EXP which is the address of an operand
1225 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1226 the maximum length of the block of memory that might be accessed or
1230 get_memory_rtx (tree exp
, tree len
)
1232 tree orig_exp
= exp
;
1235 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1236 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1237 if (TREE_CODE (exp
) == SAVE_EXPR
&& !SAVE_EXPR_RESOLVED_P (exp
))
1238 exp
= TREE_OPERAND (exp
, 0);
1240 addr
= expand_expr (orig_exp
, NULL_RTX
, ptr_mode
, EXPAND_NORMAL
);
1241 mem
= gen_rtx_MEM (BLKmode
, memory_address (BLKmode
, addr
));
1243 /* Get an expression we can use to find the attributes to assign to MEM.
1244 First remove any nops. */
1245 while (CONVERT_EXPR_P (exp
)
1246 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp
, 0))))
1247 exp
= TREE_OPERAND (exp
, 0);
1249 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1250 (as builtin stringops may alias with anything). */
1251 exp
= fold_build2 (MEM_REF
,
1252 build_array_type (char_type_node
,
1253 build_range_type (sizetype
,
1254 size_one_node
, len
)),
1255 exp
, build_int_cst (ptr_type_node
, 0));
1257 /* If the MEM_REF has no acceptable address, try to get the base object
1258 from the original address we got, and build an all-aliasing
1259 unknown-sized access to that one. */
1260 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp
, 0)))
1261 set_mem_attributes (mem
, exp
, 0);
1262 else if (TREE_CODE (TREE_OPERAND (exp
, 0)) == ADDR_EXPR
1263 && (exp
= get_base_address (TREE_OPERAND (TREE_OPERAND (exp
, 0),
1266 exp
= build_fold_addr_expr (exp
);
1267 exp
= fold_build2 (MEM_REF
,
1268 build_array_type (char_type_node
,
1269 build_range_type (sizetype
,
1272 exp
, build_int_cst (ptr_type_node
, 0));
1273 set_mem_attributes (mem
, exp
, 0);
1275 set_mem_alias_set (mem
, 0);
1279 /* Built-in functions to perform an untyped call and return. */
1281 #define apply_args_mode \
1282 (this_target_builtins->x_apply_args_mode)
1283 #define apply_result_mode \
1284 (this_target_builtins->x_apply_result_mode)
1286 /* Return the size required for the block returned by __builtin_apply_args,
1287 and initialize apply_args_mode. */
1290 apply_args_size (void)
1292 static int size
= -1;
1297 /* The values computed by this function never change. */
1300 /* The first value is the incoming arg-pointer. */
1301 size
= GET_MODE_SIZE (Pmode
);
1303 /* The second value is the structure value address unless this is
1304 passed as an "invisible" first argument. */
1305 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1306 size
+= GET_MODE_SIZE (Pmode
);
1308 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1309 if (FUNCTION_ARG_REGNO_P (regno
))
1311 mode
= targetm
.calls
.get_raw_arg_mode (regno
);
1313 gcc_assert (mode
!= VOIDmode
);
1315 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1316 if (size
% align
!= 0)
1317 size
= CEIL (size
, align
) * align
;
1318 size
+= GET_MODE_SIZE (mode
);
1319 apply_args_mode
[regno
] = mode
;
1323 apply_args_mode
[regno
] = VOIDmode
;
1329 /* Return the size required for the block returned by __builtin_apply,
1330 and initialize apply_result_mode. */
1333 apply_result_size (void)
1335 static int size
= -1;
1339 /* The values computed by this function never change. */
1344 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1345 if (targetm
.calls
.function_value_regno_p (regno
))
1347 mode
= targetm
.calls
.get_raw_result_mode (regno
);
1349 gcc_assert (mode
!= VOIDmode
);
1351 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1352 if (size
% align
!= 0)
1353 size
= CEIL (size
, align
) * align
;
1354 size
+= GET_MODE_SIZE (mode
);
1355 apply_result_mode
[regno
] = mode
;
1358 apply_result_mode
[regno
] = VOIDmode
;
1360 /* Allow targets that use untyped_call and untyped_return to override
1361 the size so that machine-specific information can be stored here. */
1362 #ifdef APPLY_RESULT_SIZE
1363 size
= APPLY_RESULT_SIZE
;
1369 /* Create a vector describing the result block RESULT. If SAVEP is true,
1370 the result block is used to save the values; otherwise it is used to
1371 restore the values. */
1374 result_vector (int savep
, rtx result
)
1376 int regno
, size
, align
, nelts
;
1379 rtx
*savevec
= XALLOCAVEC (rtx
, FIRST_PSEUDO_REGISTER
);
1382 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1383 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1385 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1386 if (size
% align
!= 0)
1387 size
= CEIL (size
, align
) * align
;
1388 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
1389 mem
= adjust_address (result
, mode
, size
);
1390 savevec
[nelts
++] = (savep
1391 ? gen_rtx_SET (mem
, reg
)
1392 : gen_rtx_SET (reg
, mem
));
1393 size
+= GET_MODE_SIZE (mode
);
1395 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
1398 /* Save the state required to perform an untyped call with the same
1399 arguments as were passed to the current function. */
1402 expand_builtin_apply_args_1 (void)
1405 int size
, align
, regno
;
1407 rtx struct_incoming_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 1);
1409 /* Create a block where the arg-pointer, structure value address,
1410 and argument registers can be saved. */
1411 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
1413 /* Walk past the arg-pointer and structure value address. */
1414 size
= GET_MODE_SIZE (Pmode
);
1415 if (targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0))
1416 size
+= GET_MODE_SIZE (Pmode
);
1418 /* Save each register used in calling a function to the block. */
1419 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1420 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1422 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1423 if (size
% align
!= 0)
1424 size
= CEIL (size
, align
) * align
;
1426 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1428 emit_move_insn (adjust_address (registers
, mode
, size
), tem
);
1429 size
+= GET_MODE_SIZE (mode
);
1432 /* Save the arg pointer to the block. */
1433 tem
= copy_to_reg (crtl
->args
.internal_arg_pointer
);
1434 /* We need the pointer as the caller actually passed them to us, not
1435 as we might have pretended they were passed. Make sure it's a valid
1436 operand, as emit_move_insn isn't expected to handle a PLUS. */
1437 if (STACK_GROWS_DOWNWARD
)
1439 = force_operand (plus_constant (Pmode
, tem
,
1440 crtl
->args
.pretend_args_size
),
1442 emit_move_insn (adjust_address (registers
, Pmode
, 0), tem
);
1444 size
= GET_MODE_SIZE (Pmode
);
1446 /* Save the structure value address unless this is passed as an
1447 "invisible" first argument. */
1448 if (struct_incoming_value
)
1450 emit_move_insn (adjust_address (registers
, Pmode
, size
),
1451 copy_to_reg (struct_incoming_value
));
1452 size
+= GET_MODE_SIZE (Pmode
);
1455 /* Return the address of the block. */
1456 return copy_addr_to_reg (XEXP (registers
, 0));
1459 /* __builtin_apply_args returns block of memory allocated on
1460 the stack into which is stored the arg pointer, structure
1461 value address, static chain, and all the registers that might
1462 possibly be used in performing a function call. The code is
1463 moved to the start of the function so the incoming values are
1467 expand_builtin_apply_args (void)
1469 /* Don't do __builtin_apply_args more than once in a function.
1470 Save the result of the first call and reuse it. */
1471 if (apply_args_value
!= 0)
1472 return apply_args_value
;
1474 /* When this function is called, it means that registers must be
1475 saved on entry to this function. So we migrate the
1476 call to the first insn of this function. */
1480 temp
= expand_builtin_apply_args_1 ();
1481 rtx_insn
*seq
= get_insns ();
1484 apply_args_value
= temp
;
1486 /* Put the insns after the NOTE that starts the function.
1487 If this is inside a start_sequence, make the outer-level insn
1488 chain current, so the code is placed at the start of the
1489 function. If internal_arg_pointer is a non-virtual pseudo,
1490 it needs to be placed after the function that initializes
1492 push_topmost_sequence ();
1493 if (REG_P (crtl
->args
.internal_arg_pointer
)
1494 && REGNO (crtl
->args
.internal_arg_pointer
) > LAST_VIRTUAL_REGISTER
)
1495 emit_insn_before (seq
, parm_birth_insn
);
1497 emit_insn_before (seq
, NEXT_INSN (entry_of_function ()));
1498 pop_topmost_sequence ();
1503 /* Perform an untyped call and save the state required to perform an
1504 untyped return of whatever value was returned by the given function. */
1507 expand_builtin_apply (rtx function
, rtx arguments
, rtx argsize
)
1509 int size
, align
, regno
;
1511 rtx incoming_args
, result
, reg
, dest
, src
;
1512 rtx_call_insn
*call_insn
;
1513 rtx old_stack_level
= 0;
1514 rtx call_fusage
= 0;
1515 rtx struct_value
= targetm
.calls
.struct_value_rtx (cfun
? TREE_TYPE (cfun
->decl
) : 0, 0);
1517 arguments
= convert_memory_address (Pmode
, arguments
);
1519 /* Create a block where the return registers can be saved. */
1520 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
1522 /* Fetch the arg pointer from the ARGUMENTS block. */
1523 incoming_args
= gen_reg_rtx (Pmode
);
1524 emit_move_insn (incoming_args
, gen_rtx_MEM (Pmode
, arguments
));
1525 if (!STACK_GROWS_DOWNWARD
)
1526 incoming_args
= expand_simple_binop (Pmode
, MINUS
, incoming_args
, argsize
,
1527 incoming_args
, 0, OPTAB_LIB_WIDEN
);
1529 /* Push a new argument block and copy the arguments. Do not allow
1530 the (potential) memcpy call below to interfere with our stack
1532 do_pending_stack_adjust ();
1535 /* Save the stack with nonlocal if available. */
1536 if (targetm
.have_save_stack_nonlocal ())
1537 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
);
1539 emit_stack_save (SAVE_BLOCK
, &old_stack_level
);
1541 /* Allocate a block of memory onto the stack and copy the memory
1542 arguments to the outgoing arguments address. We can pass TRUE
1543 as the 4th argument because we just saved the stack pointer
1544 and will restore it right after the call. */
1545 allocate_dynamic_stack_space (argsize
, 0, BIGGEST_ALIGNMENT
, true);
1547 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1548 may have already set current_function_calls_alloca to true.
1549 current_function_calls_alloca won't be set if argsize is zero,
1550 so we have to guarantee need_drap is true here. */
1551 if (SUPPORTS_STACK_ALIGNMENT
)
1552 crtl
->need_drap
= true;
1554 dest
= virtual_outgoing_args_rtx
;
1555 if (!STACK_GROWS_DOWNWARD
)
1557 if (CONST_INT_P (argsize
))
1558 dest
= plus_constant (Pmode
, dest
, -INTVAL (argsize
));
1560 dest
= gen_rtx_PLUS (Pmode
, dest
, negate_rtx (Pmode
, argsize
));
1562 dest
= gen_rtx_MEM (BLKmode
, dest
);
1563 set_mem_align (dest
, PARM_BOUNDARY
);
1564 src
= gen_rtx_MEM (BLKmode
, incoming_args
);
1565 set_mem_align (src
, PARM_BOUNDARY
);
1566 emit_block_move (dest
, src
, argsize
, BLOCK_OP_NORMAL
);
1568 /* Refer to the argument block. */
1570 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
1571 set_mem_align (arguments
, PARM_BOUNDARY
);
1573 /* Walk past the arg-pointer and structure value address. */
1574 size
= GET_MODE_SIZE (Pmode
);
1576 size
+= GET_MODE_SIZE (Pmode
);
1578 /* Restore each of the registers previously saved. Make USE insns
1579 for each of these registers for use in making the call. */
1580 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1581 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
1583 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1584 if (size
% align
!= 0)
1585 size
= CEIL (size
, align
) * align
;
1586 reg
= gen_rtx_REG (mode
, regno
);
1587 emit_move_insn (reg
, adjust_address (arguments
, mode
, size
));
1588 use_reg (&call_fusage
, reg
);
1589 size
+= GET_MODE_SIZE (mode
);
1592 /* Restore the structure value address unless this is passed as an
1593 "invisible" first argument. */
1594 size
= GET_MODE_SIZE (Pmode
);
1597 rtx value
= gen_reg_rtx (Pmode
);
1598 emit_move_insn (value
, adjust_address (arguments
, Pmode
, size
));
1599 emit_move_insn (struct_value
, value
);
1600 if (REG_P (struct_value
))
1601 use_reg (&call_fusage
, struct_value
);
1602 size
+= GET_MODE_SIZE (Pmode
);
1605 /* All arguments and registers used for the call are set up by now! */
1606 function
= prepare_call_address (NULL
, function
, NULL
, &call_fusage
, 0, 0);
1608 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1609 and we don't want to load it into a register as an optimization,
1610 because prepare_call_address already did it if it should be done. */
1611 if (GET_CODE (function
) != SYMBOL_REF
)
1612 function
= memory_address (FUNCTION_MODE
, function
);
1614 /* Generate the actual call instruction and save the return value. */
1615 if (targetm
.have_untyped_call ())
1617 rtx mem
= gen_rtx_MEM (FUNCTION_MODE
, function
);
1618 emit_call_insn (targetm
.gen_untyped_call (mem
, result
,
1619 result_vector (1, result
)));
1621 else if (targetm
.have_call_value ())
1625 /* Locate the unique return register. It is not possible to
1626 express a call that sets more than one return register using
1627 call_value; use untyped_call for that. In fact, untyped_call
1628 only needs to save the return registers in the given block. */
1629 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1630 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1632 gcc_assert (!valreg
); /* have_untyped_call required. */
1634 valreg
= gen_rtx_REG (mode
, regno
);
1637 emit_insn (targetm
.gen_call_value (valreg
,
1638 gen_rtx_MEM (FUNCTION_MODE
, function
),
1639 const0_rtx
, NULL_RTX
, const0_rtx
));
1641 emit_move_insn (adjust_address (result
, GET_MODE (valreg
), 0), valreg
);
1646 /* Find the CALL insn we just emitted, and attach the register usage
1648 call_insn
= last_call_insn ();
1649 add_function_usage_to (call_insn
, call_fusage
);
1651 /* Restore the stack. */
1652 if (targetm
.have_save_stack_nonlocal ())
1653 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
);
1655 emit_stack_restore (SAVE_BLOCK
, old_stack_level
);
1656 fixup_args_size_notes (call_insn
, get_last_insn (), 0);
1660 /* Return the address of the result block. */
1661 result
= copy_addr_to_reg (XEXP (result
, 0));
1662 return convert_memory_address (ptr_mode
, result
);
1665 /* Perform an untyped return. */
1668 expand_builtin_return (rtx result
)
1670 int size
, align
, regno
;
1673 rtx_insn
*call_fusage
= 0;
1675 result
= convert_memory_address (Pmode
, result
);
1677 apply_result_size ();
1678 result
= gen_rtx_MEM (BLKmode
, result
);
1680 if (targetm
.have_untyped_return ())
1682 rtx vector
= result_vector (0, result
);
1683 emit_jump_insn (targetm
.gen_untyped_return (result
, vector
));
1688 /* Restore the return value and note that each value is used. */
1690 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1691 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1693 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1694 if (size
% align
!= 0)
1695 size
= CEIL (size
, align
) * align
;
1696 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1697 emit_move_insn (reg
, adjust_address (result
, mode
, size
));
1699 push_to_sequence (call_fusage
);
1701 call_fusage
= get_insns ();
1703 size
+= GET_MODE_SIZE (mode
);
1706 /* Put the USE insns before the return. */
1707 emit_insn (call_fusage
);
1709 /* Return whatever values was restored by jumping directly to the end
1711 expand_naked_return ();
1714 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1716 static enum type_class
1717 type_to_class (tree type
)
1719 switch (TREE_CODE (type
))
1721 case VOID_TYPE
: return void_type_class
;
1722 case INTEGER_TYPE
: return integer_type_class
;
1723 case ENUMERAL_TYPE
: return enumeral_type_class
;
1724 case BOOLEAN_TYPE
: return boolean_type_class
;
1725 case POINTER_TYPE
: return pointer_type_class
;
1726 case REFERENCE_TYPE
: return reference_type_class
;
1727 case OFFSET_TYPE
: return offset_type_class
;
1728 case REAL_TYPE
: return real_type_class
;
1729 case COMPLEX_TYPE
: return complex_type_class
;
1730 case FUNCTION_TYPE
: return function_type_class
;
1731 case METHOD_TYPE
: return method_type_class
;
1732 case RECORD_TYPE
: return record_type_class
;
1734 case QUAL_UNION_TYPE
: return union_type_class
;
1735 case ARRAY_TYPE
: return (TYPE_STRING_FLAG (type
)
1736 ? string_type_class
: array_type_class
);
1737 case LANG_TYPE
: return lang_type_class
;
1738 default: return no_type_class
;
1742 /* Expand a call EXP to __builtin_classify_type. */
1745 expand_builtin_classify_type (tree exp
)
1747 if (call_expr_nargs (exp
))
1748 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))));
1749 return GEN_INT (no_type_class
);
1752 /* This helper macro, meant to be used in mathfn_built_in below,
1753 determines which among a set of three builtin math functions is
1754 appropriate for a given type mode. The `F' and `L' cases are
1755 automatically generated from the `double' case. */
1756 #define CASE_MATHFN(BUILT_IN_MATHFN) \
1757 case BUILT_IN_MATHFN: case BUILT_IN_MATHFN##F: case BUILT_IN_MATHFN##L: \
1758 fcode = BUILT_IN_MATHFN; fcodef = BUILT_IN_MATHFN##F ; \
1759 fcodel = BUILT_IN_MATHFN##L ; break;
1760 /* Similar to above, but appends _R after any F/L suffix. */
1761 #define CASE_MATHFN_REENT(BUILT_IN_MATHFN) \
1762 case BUILT_IN_MATHFN##_R: case BUILT_IN_MATHFN##F_R: case BUILT_IN_MATHFN##L_R: \
1763 fcode = BUILT_IN_MATHFN##_R; fcodef = BUILT_IN_MATHFN##F_R ; \
1764 fcodel = BUILT_IN_MATHFN##L_R ; break;
1766 /* Return a function equivalent to FN but operating on floating-point
1767 values of type TYPE, or END_BUILTINS if no such function exists.
1768 This is purely an operation on built-in function codes; it does not
1769 guarantee that the target actually has an implementation of the
1772 static built_in_function
1773 mathfn_built_in_2 (tree type
, built_in_function fn
)
1775 built_in_function fcode
, fcodef
, fcodel
;
1779 CASE_MATHFN (BUILT_IN_ACOS
)
1780 CASE_MATHFN (BUILT_IN_ACOSH
)
1781 CASE_MATHFN (BUILT_IN_ASIN
)
1782 CASE_MATHFN (BUILT_IN_ASINH
)
1783 CASE_MATHFN (BUILT_IN_ATAN
)
1784 CASE_MATHFN (BUILT_IN_ATAN2
)
1785 CASE_MATHFN (BUILT_IN_ATANH
)
1786 CASE_MATHFN (BUILT_IN_CBRT
)
1787 CASE_MATHFN (BUILT_IN_CEIL
)
1788 CASE_MATHFN (BUILT_IN_CEXPI
)
1789 CASE_MATHFN (BUILT_IN_COPYSIGN
)
1790 CASE_MATHFN (BUILT_IN_COS
)
1791 CASE_MATHFN (BUILT_IN_COSH
)
1792 CASE_MATHFN (BUILT_IN_DREM
)
1793 CASE_MATHFN (BUILT_IN_ERF
)
1794 CASE_MATHFN (BUILT_IN_ERFC
)
1795 CASE_MATHFN (BUILT_IN_EXP
)
1796 CASE_MATHFN (BUILT_IN_EXP10
)
1797 CASE_MATHFN (BUILT_IN_EXP2
)
1798 CASE_MATHFN (BUILT_IN_EXPM1
)
1799 CASE_MATHFN (BUILT_IN_FABS
)
1800 CASE_MATHFN (BUILT_IN_FDIM
)
1801 CASE_MATHFN (BUILT_IN_FLOOR
)
1802 CASE_MATHFN (BUILT_IN_FMA
)
1803 CASE_MATHFN (BUILT_IN_FMAX
)
1804 CASE_MATHFN (BUILT_IN_FMIN
)
1805 CASE_MATHFN (BUILT_IN_FMOD
)
1806 CASE_MATHFN (BUILT_IN_FREXP
)
1807 CASE_MATHFN (BUILT_IN_GAMMA
)
1808 CASE_MATHFN_REENT (BUILT_IN_GAMMA
) /* GAMMA_R */
1809 CASE_MATHFN (BUILT_IN_HUGE_VAL
)
1810 CASE_MATHFN (BUILT_IN_HYPOT
)
1811 CASE_MATHFN (BUILT_IN_ILOGB
)
1812 CASE_MATHFN (BUILT_IN_ICEIL
)
1813 CASE_MATHFN (BUILT_IN_IFLOOR
)
1814 CASE_MATHFN (BUILT_IN_INF
)
1815 CASE_MATHFN (BUILT_IN_IRINT
)
1816 CASE_MATHFN (BUILT_IN_IROUND
)
1817 CASE_MATHFN (BUILT_IN_ISINF
)
1818 CASE_MATHFN (BUILT_IN_J0
)
1819 CASE_MATHFN (BUILT_IN_J1
)
1820 CASE_MATHFN (BUILT_IN_JN
)
1821 CASE_MATHFN (BUILT_IN_LCEIL
)
1822 CASE_MATHFN (BUILT_IN_LDEXP
)
1823 CASE_MATHFN (BUILT_IN_LFLOOR
)
1824 CASE_MATHFN (BUILT_IN_LGAMMA
)
1825 CASE_MATHFN_REENT (BUILT_IN_LGAMMA
) /* LGAMMA_R */
1826 CASE_MATHFN (BUILT_IN_LLCEIL
)
1827 CASE_MATHFN (BUILT_IN_LLFLOOR
)
1828 CASE_MATHFN (BUILT_IN_LLRINT
)
1829 CASE_MATHFN (BUILT_IN_LLROUND
)
1830 CASE_MATHFN (BUILT_IN_LOG
)
1831 CASE_MATHFN (BUILT_IN_LOG10
)
1832 CASE_MATHFN (BUILT_IN_LOG1P
)
1833 CASE_MATHFN (BUILT_IN_LOG2
)
1834 CASE_MATHFN (BUILT_IN_LOGB
)
1835 CASE_MATHFN (BUILT_IN_LRINT
)
1836 CASE_MATHFN (BUILT_IN_LROUND
)
1837 CASE_MATHFN (BUILT_IN_MODF
)
1838 CASE_MATHFN (BUILT_IN_NAN
)
1839 CASE_MATHFN (BUILT_IN_NANS
)
1840 CASE_MATHFN (BUILT_IN_NEARBYINT
)
1841 CASE_MATHFN (BUILT_IN_NEXTAFTER
)
1842 CASE_MATHFN (BUILT_IN_NEXTTOWARD
)
1843 CASE_MATHFN (BUILT_IN_POW
)
1844 CASE_MATHFN (BUILT_IN_POWI
)
1845 CASE_MATHFN (BUILT_IN_POW10
)
1846 CASE_MATHFN (BUILT_IN_REMAINDER
)
1847 CASE_MATHFN (BUILT_IN_REMQUO
)
1848 CASE_MATHFN (BUILT_IN_RINT
)
1849 CASE_MATHFN (BUILT_IN_ROUND
)
1850 CASE_MATHFN (BUILT_IN_SCALB
)
1851 CASE_MATHFN (BUILT_IN_SCALBLN
)
1852 CASE_MATHFN (BUILT_IN_SCALBN
)
1853 CASE_MATHFN (BUILT_IN_SIGNBIT
)
1854 CASE_MATHFN (BUILT_IN_SIGNIFICAND
)
1855 CASE_MATHFN (BUILT_IN_SIN
)
1856 CASE_MATHFN (BUILT_IN_SINCOS
)
1857 CASE_MATHFN (BUILT_IN_SINH
)
1858 CASE_MATHFN (BUILT_IN_SQRT
)
1859 CASE_MATHFN (BUILT_IN_TAN
)
1860 CASE_MATHFN (BUILT_IN_TANH
)
1861 CASE_MATHFN (BUILT_IN_TGAMMA
)
1862 CASE_MATHFN (BUILT_IN_TRUNC
)
1863 CASE_MATHFN (BUILT_IN_Y0
)
1864 CASE_MATHFN (BUILT_IN_Y1
)
1865 CASE_MATHFN (BUILT_IN_YN
)
1868 return END_BUILTINS
;
1871 if (TYPE_MAIN_VARIANT (type
) == double_type_node
)
1873 else if (TYPE_MAIN_VARIANT (type
) == float_type_node
)
1875 else if (TYPE_MAIN_VARIANT (type
) == long_double_type_node
)
1878 return END_BUILTINS
;
1881 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1882 if available. If IMPLICIT_P is true use the implicit builtin declaration,
1883 otherwise use the explicit declaration. If we can't do the conversion,
1887 mathfn_built_in_1 (tree type
, enum built_in_function fn
, bool implicit_p
)
1889 built_in_function fcode2
= mathfn_built_in_2 (type
, fn
);
1890 if (fcode2
== END_BUILTINS
)
1893 if (implicit_p
&& !builtin_decl_implicit_p (fcode2
))
1896 return builtin_decl_explicit (fcode2
);
1899 /* Like mathfn_built_in_1(), but always use the implicit array. */
1902 mathfn_built_in (tree type
, enum built_in_function fn
)
1904 return mathfn_built_in_1 (type
, fn
, /*implicit=*/ 1);
1907 /* If errno must be maintained, expand the RTL to check if the result,
1908 TARGET, of a built-in function call, EXP, is NaN, and if so set
1912 expand_errno_check (tree exp
, rtx target
)
1914 rtx_code_label
*lab
= gen_label_rtx ();
1916 /* Test the result; if it is NaN, set errno=EDOM because
1917 the argument was not in the domain. */
1918 do_compare_rtx_and_jump (target
, target
, EQ
, 0, GET_MODE (target
),
1919 NULL_RTX
, NULL
, lab
,
1920 /* The jump is very likely. */
1921 REG_BR_PROB_BASE
- (REG_BR_PROB_BASE
/ 2000 - 1));
1924 /* If this built-in doesn't throw an exception, set errno directly. */
1925 if (TREE_NOTHROW (TREE_OPERAND (CALL_EXPR_FN (exp
), 0)))
1927 #ifdef GEN_ERRNO_RTX
1928 rtx errno_rtx
= GEN_ERRNO_RTX
;
1931 = gen_rtx_MEM (word_mode
, gen_rtx_SYMBOL_REF (Pmode
, "errno"));
1933 emit_move_insn (errno_rtx
,
1934 gen_int_mode (TARGET_EDOM
, GET_MODE (errno_rtx
)));
1940 /* Make sure the library call isn't expanded as a tail call. */
1941 CALL_EXPR_TAILCALL (exp
) = 0;
1943 /* We can't set errno=EDOM directly; let the library call do it.
1944 Pop the arguments right away in case the call gets deleted. */
1946 expand_call (exp
, target
, 0);
1951 /* Expand a call to one of the builtin math functions (sqrt, exp, or log).
1952 Return NULL_RTX if a normal call should be emitted rather than expanding
1953 the function in-line. EXP is the expression that is a call to the builtin
1954 function; if convenient, the result should be placed in TARGET.
1955 SUBTARGET may be used as the target for computing one of EXP's operands. */
1958 expand_builtin_mathfn (tree exp
, rtx target
, rtx subtarget
)
1960 optab builtin_optab
;
1963 tree fndecl
= get_callee_fndecl (exp
);
1965 bool errno_set
= false;
1966 bool try_widening
= false;
1969 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
1972 arg
= CALL_EXPR_ARG (exp
, 0);
1974 switch (DECL_FUNCTION_CODE (fndecl
))
1976 CASE_FLT_FN (BUILT_IN_SQRT
):
1977 errno_set
= ! tree_expr_nonnegative_p (arg
);
1978 try_widening
= true;
1979 builtin_optab
= sqrt_optab
;
1981 CASE_FLT_FN (BUILT_IN_EXP
):
1982 errno_set
= true; builtin_optab
= exp_optab
; break;
1983 CASE_FLT_FN (BUILT_IN_EXP10
):
1984 CASE_FLT_FN (BUILT_IN_POW10
):
1985 errno_set
= true; builtin_optab
= exp10_optab
; break;
1986 CASE_FLT_FN (BUILT_IN_EXP2
):
1987 errno_set
= true; builtin_optab
= exp2_optab
; break;
1988 CASE_FLT_FN (BUILT_IN_EXPM1
):
1989 errno_set
= true; builtin_optab
= expm1_optab
; break;
1990 CASE_FLT_FN (BUILT_IN_LOGB
):
1991 errno_set
= true; builtin_optab
= logb_optab
; break;
1992 CASE_FLT_FN (BUILT_IN_LOG
):
1993 errno_set
= true; builtin_optab
= log_optab
; break;
1994 CASE_FLT_FN (BUILT_IN_LOG10
):
1995 errno_set
= true; builtin_optab
= log10_optab
; break;
1996 CASE_FLT_FN (BUILT_IN_LOG2
):
1997 errno_set
= true; builtin_optab
= log2_optab
; break;
1998 CASE_FLT_FN (BUILT_IN_LOG1P
):
1999 errno_set
= true; builtin_optab
= log1p_optab
; break;
2000 CASE_FLT_FN (BUILT_IN_ASIN
):
2001 builtin_optab
= asin_optab
; break;
2002 CASE_FLT_FN (BUILT_IN_ACOS
):
2003 builtin_optab
= acos_optab
; break;
2004 CASE_FLT_FN (BUILT_IN_TAN
):
2005 builtin_optab
= tan_optab
; break;
2006 CASE_FLT_FN (BUILT_IN_ATAN
):
2007 builtin_optab
= atan_optab
; break;
2008 CASE_FLT_FN (BUILT_IN_FLOOR
):
2009 builtin_optab
= floor_optab
; break;
2010 CASE_FLT_FN (BUILT_IN_CEIL
):
2011 builtin_optab
= ceil_optab
; break;
2012 CASE_FLT_FN (BUILT_IN_TRUNC
):
2013 builtin_optab
= btrunc_optab
; break;
2014 CASE_FLT_FN (BUILT_IN_ROUND
):
2015 builtin_optab
= round_optab
; break;
2016 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
2017 builtin_optab
= nearbyint_optab
;
2018 if (flag_trapping_math
)
2020 /* Else fallthrough and expand as rint. */
2021 CASE_FLT_FN (BUILT_IN_RINT
):
2022 builtin_optab
= rint_optab
; break;
2023 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
2024 builtin_optab
= significand_optab
; break;
2029 /* Make a suitable register to place result in. */
2030 mode
= TYPE_MODE (TREE_TYPE (exp
));
2032 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2035 /* Before working hard, check whether the instruction is available, but try
2036 to widen the mode for specific operations. */
2037 if ((optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
2038 || (try_widening
&& !excess_precision_type (TREE_TYPE (exp
))))
2039 && (!errno_set
|| !optimize_insn_for_size_p ()))
2041 rtx result
= gen_reg_rtx (mode
);
2043 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2044 need to expand the argument again. This way, we will not perform
2045 side-effects more the once. */
2046 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2048 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2052 /* Compute into RESULT.
2053 Set RESULT to wherever the result comes back. */
2054 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2059 expand_errno_check (exp
, result
);
2061 /* Output the entire sequence. */
2062 insns
= get_insns ();
2068 /* If we were unable to expand via the builtin, stop the sequence
2069 (without outputting the insns) and call to the library function
2070 with the stabilized argument list. */
2074 return expand_call (exp
, target
, target
== const0_rtx
);
2077 /* Expand a call to the builtin binary math functions (pow and atan2).
2078 Return NULL_RTX if a normal call should be emitted rather than expanding the
2079 function in-line. EXP is the expression that is a call to the builtin
2080 function; if convenient, the result should be placed in TARGET.
2081 SUBTARGET may be used as the target for computing one of EXP's
2085 expand_builtin_mathfn_2 (tree exp
, rtx target
, rtx subtarget
)
2087 optab builtin_optab
;
2088 rtx op0
, op1
, result
;
2090 int op1_type
= REAL_TYPE
;
2091 tree fndecl
= get_callee_fndecl (exp
);
2094 bool errno_set
= true;
2096 switch (DECL_FUNCTION_CODE (fndecl
))
2098 CASE_FLT_FN (BUILT_IN_SCALBN
):
2099 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2100 CASE_FLT_FN (BUILT_IN_LDEXP
):
2101 op1_type
= INTEGER_TYPE
;
2106 if (!validate_arglist (exp
, REAL_TYPE
, op1_type
, VOID_TYPE
))
2109 arg0
= CALL_EXPR_ARG (exp
, 0);
2110 arg1
= CALL_EXPR_ARG (exp
, 1);
2112 switch (DECL_FUNCTION_CODE (fndecl
))
2114 CASE_FLT_FN (BUILT_IN_POW
):
2115 builtin_optab
= pow_optab
; break;
2116 CASE_FLT_FN (BUILT_IN_ATAN2
):
2117 builtin_optab
= atan2_optab
; break;
2118 CASE_FLT_FN (BUILT_IN_SCALB
):
2119 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2121 builtin_optab
= scalb_optab
; break;
2122 CASE_FLT_FN (BUILT_IN_SCALBN
):
2123 CASE_FLT_FN (BUILT_IN_SCALBLN
):
2124 if (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (exp
)))->b
!= 2)
2126 /* Fall through... */
2127 CASE_FLT_FN (BUILT_IN_LDEXP
):
2128 builtin_optab
= ldexp_optab
; break;
2129 CASE_FLT_FN (BUILT_IN_FMOD
):
2130 builtin_optab
= fmod_optab
; break;
2131 CASE_FLT_FN (BUILT_IN_REMAINDER
):
2132 CASE_FLT_FN (BUILT_IN_DREM
):
2133 builtin_optab
= remainder_optab
; break;
2138 /* Make a suitable register to place result in. */
2139 mode
= TYPE_MODE (TREE_TYPE (exp
));
2141 /* Before working hard, check whether the instruction is available. */
2142 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2145 result
= gen_reg_rtx (mode
);
2147 if (! flag_errno_math
|| ! HONOR_NANS (mode
))
2150 if (errno_set
&& optimize_insn_for_size_p ())
2153 /* Always stabilize the argument list. */
2154 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2155 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2157 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2158 op1
= expand_normal (arg1
);
2162 /* Compute into RESULT.
2163 Set RESULT to wherever the result comes back. */
2164 result
= expand_binop (mode
, builtin_optab
, op0
, op1
,
2165 result
, 0, OPTAB_DIRECT
);
2167 /* If we were unable to expand via the builtin, stop the sequence
2168 (without outputting the insns) and call to the library function
2169 with the stabilized argument list. */
2173 return expand_call (exp
, target
, target
== const0_rtx
);
2177 expand_errno_check (exp
, result
);
2179 /* Output the entire sequence. */
2180 insns
= get_insns ();
2187 /* Expand a call to the builtin trinary math functions (fma).
2188 Return NULL_RTX if a normal call should be emitted rather than expanding the
2189 function in-line. EXP is the expression that is a call to the builtin
2190 function; if convenient, the result should be placed in TARGET.
2191 SUBTARGET may be used as the target for computing one of EXP's
2195 expand_builtin_mathfn_ternary (tree exp
, rtx target
, rtx subtarget
)
2197 optab builtin_optab
;
2198 rtx op0
, op1
, op2
, result
;
2200 tree fndecl
= get_callee_fndecl (exp
);
2201 tree arg0
, arg1
, arg2
;
2204 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
2207 arg0
= CALL_EXPR_ARG (exp
, 0);
2208 arg1
= CALL_EXPR_ARG (exp
, 1);
2209 arg2
= CALL_EXPR_ARG (exp
, 2);
2211 switch (DECL_FUNCTION_CODE (fndecl
))
2213 CASE_FLT_FN (BUILT_IN_FMA
):
2214 builtin_optab
= fma_optab
; break;
2219 /* Make a suitable register to place result in. */
2220 mode
= TYPE_MODE (TREE_TYPE (exp
));
2222 /* Before working hard, check whether the instruction is available. */
2223 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2226 result
= gen_reg_rtx (mode
);
2228 /* Always stabilize the argument list. */
2229 CALL_EXPR_ARG (exp
, 0) = arg0
= builtin_save_expr (arg0
);
2230 CALL_EXPR_ARG (exp
, 1) = arg1
= builtin_save_expr (arg1
);
2231 CALL_EXPR_ARG (exp
, 2) = arg2
= builtin_save_expr (arg2
);
2233 op0
= expand_expr (arg0
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2234 op1
= expand_normal (arg1
);
2235 op2
= expand_normal (arg2
);
2239 /* Compute into RESULT.
2240 Set RESULT to wherever the result comes back. */
2241 result
= expand_ternary_op (mode
, builtin_optab
, op0
, op1
, op2
,
2244 /* If we were unable to expand via the builtin, stop the sequence
2245 (without outputting the insns) and call to the library function
2246 with the stabilized argument list. */
2250 return expand_call (exp
, target
, target
== const0_rtx
);
2253 /* Output the entire sequence. */
2254 insns
= get_insns ();
2261 /* Expand a call to the builtin sin and cos math functions.
2262 Return NULL_RTX if a normal call should be emitted rather than expanding the
2263 function in-line. EXP is the expression that is a call to the builtin
2264 function; if convenient, the result should be placed in TARGET.
2265 SUBTARGET may be used as the target for computing one of EXP's
2269 expand_builtin_mathfn_3 (tree exp
, rtx target
, rtx subtarget
)
2271 optab builtin_optab
;
2274 tree fndecl
= get_callee_fndecl (exp
);
2278 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2281 arg
= CALL_EXPR_ARG (exp
, 0);
2283 switch (DECL_FUNCTION_CODE (fndecl
))
2285 CASE_FLT_FN (BUILT_IN_SIN
):
2286 CASE_FLT_FN (BUILT_IN_COS
):
2287 builtin_optab
= sincos_optab
; break;
2292 /* Make a suitable register to place result in. */
2293 mode
= TYPE_MODE (TREE_TYPE (exp
));
2295 /* Check if sincos insn is available, otherwise fallback
2296 to sin or cos insn. */
2297 if (optab_handler (builtin_optab
, mode
) == CODE_FOR_nothing
)
2298 switch (DECL_FUNCTION_CODE (fndecl
))
2300 CASE_FLT_FN (BUILT_IN_SIN
):
2301 builtin_optab
= sin_optab
; break;
2302 CASE_FLT_FN (BUILT_IN_COS
):
2303 builtin_optab
= cos_optab
; break;
2308 /* Before working hard, check whether the instruction is available. */
2309 if (optab_handler (builtin_optab
, mode
) != CODE_FOR_nothing
)
2311 rtx result
= gen_reg_rtx (mode
);
2313 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2314 need to expand the argument again. This way, we will not perform
2315 side-effects more the once. */
2316 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2318 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
2322 /* Compute into RESULT.
2323 Set RESULT to wherever the result comes back. */
2324 if (builtin_optab
== sincos_optab
)
2328 switch (DECL_FUNCTION_CODE (fndecl
))
2330 CASE_FLT_FN (BUILT_IN_SIN
):
2331 ok
= expand_twoval_unop (builtin_optab
, op0
, 0, result
, 0);
2333 CASE_FLT_FN (BUILT_IN_COS
):
2334 ok
= expand_twoval_unop (builtin_optab
, op0
, result
, 0, 0);
2342 result
= expand_unop (mode
, builtin_optab
, op0
, result
, 0);
2346 /* Output the entire sequence. */
2347 insns
= get_insns ();
2353 /* If we were unable to expand via the builtin, stop the sequence
2354 (without outputting the insns) and call to the library function
2355 with the stabilized argument list. */
2359 return expand_call (exp
, target
, target
== const0_rtx
);
2362 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2363 return an RTL instruction code that implements the functionality.
2364 If that isn't possible or available return CODE_FOR_nothing. */
2366 static enum insn_code
2367 interclass_mathfn_icode (tree arg
, tree fndecl
)
2369 bool errno_set
= false;
2370 optab builtin_optab
= unknown_optab
;
2373 switch (DECL_FUNCTION_CODE (fndecl
))
2375 CASE_FLT_FN (BUILT_IN_ILOGB
):
2376 errno_set
= true; builtin_optab
= ilogb_optab
; break;
2377 CASE_FLT_FN (BUILT_IN_ISINF
):
2378 builtin_optab
= isinf_optab
; break;
2379 case BUILT_IN_ISNORMAL
:
2380 case BUILT_IN_ISFINITE
:
2381 CASE_FLT_FN (BUILT_IN_FINITE
):
2382 case BUILT_IN_FINITED32
:
2383 case BUILT_IN_FINITED64
:
2384 case BUILT_IN_FINITED128
:
2385 case BUILT_IN_ISINFD32
:
2386 case BUILT_IN_ISINFD64
:
2387 case BUILT_IN_ISINFD128
:
2388 /* These builtins have no optabs (yet). */
2394 /* There's no easy way to detect the case we need to set EDOM. */
2395 if (flag_errno_math
&& errno_set
)
2396 return CODE_FOR_nothing
;
2398 /* Optab mode depends on the mode of the input argument. */
2399 mode
= TYPE_MODE (TREE_TYPE (arg
));
2402 return optab_handler (builtin_optab
, mode
);
2403 return CODE_FOR_nothing
;
2406 /* Expand a call to one of the builtin math functions that operate on
2407 floating point argument and output an integer result (ilogb, isinf,
2409 Return 0 if a normal call should be emitted rather than expanding the
2410 function in-line. EXP is the expression that is a call to the builtin
2411 function; if convenient, the result should be placed in TARGET. */
2414 expand_builtin_interclass_mathfn (tree exp
, rtx target
)
2416 enum insn_code icode
= CODE_FOR_nothing
;
2418 tree fndecl
= get_callee_fndecl (exp
);
2422 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2425 arg
= CALL_EXPR_ARG (exp
, 0);
2426 icode
= interclass_mathfn_icode (arg
, fndecl
);
2427 mode
= TYPE_MODE (TREE_TYPE (arg
));
2429 if (icode
!= CODE_FOR_nothing
)
2431 struct expand_operand ops
[1];
2432 rtx_insn
*last
= get_last_insn ();
2433 tree orig_arg
= arg
;
2435 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2436 need to expand the argument again. This way, we will not perform
2437 side-effects more the once. */
2438 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2440 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2442 if (mode
!= GET_MODE (op0
))
2443 op0
= convert_to_mode (mode
, op0
, 0);
2445 create_output_operand (&ops
[0], target
, TYPE_MODE (TREE_TYPE (exp
)));
2446 if (maybe_legitimize_operands (icode
, 0, 1, ops
)
2447 && maybe_emit_unop_insn (icode
, ops
[0].value
, op0
, UNKNOWN
))
2448 return ops
[0].value
;
2450 delete_insns_since (last
);
2451 CALL_EXPR_ARG (exp
, 0) = orig_arg
;
2457 /* Expand a call to the builtin sincos math function.
2458 Return NULL_RTX if a normal call should be emitted rather than expanding the
2459 function in-line. EXP is the expression that is a call to the builtin
2463 expand_builtin_sincos (tree exp
)
2465 rtx op0
, op1
, op2
, target1
, target2
;
2467 tree arg
, sinp
, cosp
;
2469 location_t loc
= EXPR_LOCATION (exp
);
2470 tree alias_type
, alias_off
;
2472 if (!validate_arglist (exp
, REAL_TYPE
,
2473 POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
2476 arg
= CALL_EXPR_ARG (exp
, 0);
2477 sinp
= CALL_EXPR_ARG (exp
, 1);
2478 cosp
= CALL_EXPR_ARG (exp
, 2);
2480 /* Make a suitable register to place result in. */
2481 mode
= TYPE_MODE (TREE_TYPE (arg
));
2483 /* Check if sincos insn is available, otherwise emit the call. */
2484 if (optab_handler (sincos_optab
, mode
) == CODE_FOR_nothing
)
2487 target1
= gen_reg_rtx (mode
);
2488 target2
= gen_reg_rtx (mode
);
2490 op0
= expand_normal (arg
);
2491 alias_type
= build_pointer_type_for_mode (TREE_TYPE (arg
), ptr_mode
, true);
2492 alias_off
= build_int_cst (alias_type
, 0);
2493 op1
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2495 op2
= expand_normal (fold_build2_loc (loc
, MEM_REF
, TREE_TYPE (arg
),
2498 /* Compute into target1 and target2.
2499 Set TARGET to wherever the result comes back. */
2500 result
= expand_twoval_unop (sincos_optab
, op0
, target2
, target1
, 0);
2501 gcc_assert (result
);
2503 /* Move target1 and target2 to the memory locations indicated
2505 emit_move_insn (op1
, target1
);
2506 emit_move_insn (op2
, target2
);
2511 /* Expand a call to the internal cexpi builtin to the sincos math function.
2512 EXP is the expression that is a call to the builtin function; if convenient,
2513 the result should be placed in TARGET. */
2516 expand_builtin_cexpi (tree exp
, rtx target
)
2518 tree fndecl
= get_callee_fndecl (exp
);
2522 location_t loc
= EXPR_LOCATION (exp
);
2524 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2527 arg
= CALL_EXPR_ARG (exp
, 0);
2528 type
= TREE_TYPE (arg
);
2529 mode
= TYPE_MODE (TREE_TYPE (arg
));
2531 /* Try expanding via a sincos optab, fall back to emitting a libcall
2532 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2533 is only generated from sincos, cexp or if we have either of them. */
2534 if (optab_handler (sincos_optab
, mode
) != CODE_FOR_nothing
)
2536 op1
= gen_reg_rtx (mode
);
2537 op2
= gen_reg_rtx (mode
);
2539 op0
= expand_expr (arg
, NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
2541 /* Compute into op1 and op2. */
2542 expand_twoval_unop (sincos_optab
, op0
, op2
, op1
, 0);
2544 else if (targetm
.libc_has_function (function_sincos
))
2546 tree call
, fn
= NULL_TREE
;
2550 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2551 fn
= builtin_decl_explicit (BUILT_IN_SINCOSF
);
2552 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2553 fn
= builtin_decl_explicit (BUILT_IN_SINCOS
);
2554 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2555 fn
= builtin_decl_explicit (BUILT_IN_SINCOSL
);
2559 op1
= assign_temp (TREE_TYPE (arg
), 1, 1);
2560 op2
= assign_temp (TREE_TYPE (arg
), 1, 1);
2561 op1a
= copy_addr_to_reg (XEXP (op1
, 0));
2562 op2a
= copy_addr_to_reg (XEXP (op2
, 0));
2563 top1
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op1a
);
2564 top2
= make_tree (build_pointer_type (TREE_TYPE (arg
)), op2a
);
2566 /* Make sure not to fold the sincos call again. */
2567 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2568 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn
)),
2569 call
, 3, arg
, top1
, top2
));
2573 tree call
, fn
= NULL_TREE
, narg
;
2574 tree ctype
= build_complex_type (type
);
2576 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2577 fn
= builtin_decl_explicit (BUILT_IN_CEXPF
);
2578 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2579 fn
= builtin_decl_explicit (BUILT_IN_CEXP
);
2580 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2581 fn
= builtin_decl_explicit (BUILT_IN_CEXPL
);
2585 /* If we don't have a decl for cexp create one. This is the
2586 friendliest fallback if the user calls __builtin_cexpi
2587 without full target C99 function support. */
2588 if (fn
== NULL_TREE
)
2591 const char *name
= NULL
;
2593 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIF
)
2595 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPI
)
2597 else if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CEXPIL
)
2600 fntype
= build_function_type_list (ctype
, ctype
, NULL_TREE
);
2601 fn
= build_fn_decl (name
, fntype
);
2604 narg
= fold_build2_loc (loc
, COMPLEX_EXPR
, ctype
,
2605 build_real (type
, dconst0
), arg
);
2607 /* Make sure not to fold the cexp call again. */
2608 call
= build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (fn
)), fn
);
2609 return expand_expr (build_call_nary (ctype
, call
, 1, narg
),
2610 target
, VOIDmode
, EXPAND_NORMAL
);
2613 /* Now build the proper return type. */
2614 return expand_expr (build2 (COMPLEX_EXPR
, build_complex_type (type
),
2615 make_tree (TREE_TYPE (arg
), op2
),
2616 make_tree (TREE_TYPE (arg
), op1
)),
2617 target
, VOIDmode
, EXPAND_NORMAL
);
2620 /* Conveniently construct a function call expression. FNDECL names the
2621 function to be called, N is the number of arguments, and the "..."
2622 parameters are the argument expressions. Unlike build_call_exr
2623 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2626 build_call_nofold_loc (location_t loc
, tree fndecl
, int n
, ...)
2629 tree fntype
= TREE_TYPE (fndecl
);
2630 tree fn
= build1 (ADDR_EXPR
, build_pointer_type (fntype
), fndecl
);
2633 fn
= build_call_valist (TREE_TYPE (fntype
), fn
, n
, ap
);
2635 SET_EXPR_LOCATION (fn
, loc
);
2639 /* Expand a call to one of the builtin rounding functions gcc defines
2640 as an extension (lfloor and lceil). As these are gcc extensions we
2641 do not need to worry about setting errno to EDOM.
2642 If expanding via optab fails, lower expression to (int)(floor(x)).
2643 EXP is the expression that is a call to the builtin function;
2644 if convenient, the result should be placed in TARGET. */
2647 expand_builtin_int_roundingfn (tree exp
, rtx target
)
2649 convert_optab builtin_optab
;
2652 tree fndecl
= get_callee_fndecl (exp
);
2653 enum built_in_function fallback_fn
;
2654 tree fallback_fndecl
;
2658 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2661 arg
= CALL_EXPR_ARG (exp
, 0);
2663 switch (DECL_FUNCTION_CODE (fndecl
))
2665 CASE_FLT_FN (BUILT_IN_ICEIL
):
2666 CASE_FLT_FN (BUILT_IN_LCEIL
):
2667 CASE_FLT_FN (BUILT_IN_LLCEIL
):
2668 builtin_optab
= lceil_optab
;
2669 fallback_fn
= BUILT_IN_CEIL
;
2672 CASE_FLT_FN (BUILT_IN_IFLOOR
):
2673 CASE_FLT_FN (BUILT_IN_LFLOOR
):
2674 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
2675 builtin_optab
= lfloor_optab
;
2676 fallback_fn
= BUILT_IN_FLOOR
;
2683 /* Make a suitable register to place result in. */
2684 mode
= TYPE_MODE (TREE_TYPE (exp
));
2686 target
= gen_reg_rtx (mode
);
2688 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2689 need to expand the argument again. This way, we will not perform
2690 side-effects more the once. */
2691 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2693 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2697 /* Compute into TARGET. */
2698 if (expand_sfix_optab (target
, op0
, builtin_optab
))
2700 /* Output the entire sequence. */
2701 insns
= get_insns ();
2707 /* If we were unable to expand via the builtin, stop the sequence
2708 (without outputting the insns). */
2711 /* Fall back to floating point rounding optab. */
2712 fallback_fndecl
= mathfn_built_in (TREE_TYPE (arg
), fallback_fn
);
2714 /* For non-C99 targets we may end up without a fallback fndecl here
2715 if the user called __builtin_lfloor directly. In this case emit
2716 a call to the floor/ceil variants nevertheless. This should result
2717 in the best user experience for not full C99 targets. */
2718 if (fallback_fndecl
== NULL_TREE
)
2721 const char *name
= NULL
;
2723 switch (DECL_FUNCTION_CODE (fndecl
))
2725 case BUILT_IN_ICEIL
:
2726 case BUILT_IN_LCEIL
:
2727 case BUILT_IN_LLCEIL
:
2730 case BUILT_IN_ICEILF
:
2731 case BUILT_IN_LCEILF
:
2732 case BUILT_IN_LLCEILF
:
2735 case BUILT_IN_ICEILL
:
2736 case BUILT_IN_LCEILL
:
2737 case BUILT_IN_LLCEILL
:
2740 case BUILT_IN_IFLOOR
:
2741 case BUILT_IN_LFLOOR
:
2742 case BUILT_IN_LLFLOOR
:
2745 case BUILT_IN_IFLOORF
:
2746 case BUILT_IN_LFLOORF
:
2747 case BUILT_IN_LLFLOORF
:
2750 case BUILT_IN_IFLOORL
:
2751 case BUILT_IN_LFLOORL
:
2752 case BUILT_IN_LLFLOORL
:
2759 fntype
= build_function_type_list (TREE_TYPE (arg
),
2760 TREE_TYPE (arg
), NULL_TREE
);
2761 fallback_fndecl
= build_fn_decl (name
, fntype
);
2764 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
), fallback_fndecl
, 1, arg
);
2766 tmp
= expand_normal (exp
);
2767 tmp
= maybe_emit_group_store (tmp
, TREE_TYPE (exp
));
2769 /* Truncate the result of floating point optab to integer
2770 via expand_fix (). */
2771 target
= gen_reg_rtx (mode
);
2772 expand_fix (target
, tmp
, 0);
2777 /* Expand a call to one of the builtin math functions doing integer
2779 Return 0 if a normal call should be emitted rather than expanding the
2780 function in-line. EXP is the expression that is a call to the builtin
2781 function; if convenient, the result should be placed in TARGET. */
2784 expand_builtin_int_roundingfn_2 (tree exp
, rtx target
)
2786 convert_optab builtin_optab
;
2789 tree fndecl
= get_callee_fndecl (exp
);
2792 enum built_in_function fallback_fn
= BUILT_IN_NONE
;
2794 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
2797 arg
= CALL_EXPR_ARG (exp
, 0);
2799 switch (DECL_FUNCTION_CODE (fndecl
))
2801 CASE_FLT_FN (BUILT_IN_IRINT
):
2802 fallback_fn
= BUILT_IN_LRINT
;
2804 CASE_FLT_FN (BUILT_IN_LRINT
):
2805 CASE_FLT_FN (BUILT_IN_LLRINT
):
2806 builtin_optab
= lrint_optab
;
2809 CASE_FLT_FN (BUILT_IN_IROUND
):
2810 fallback_fn
= BUILT_IN_LROUND
;
2812 CASE_FLT_FN (BUILT_IN_LROUND
):
2813 CASE_FLT_FN (BUILT_IN_LLROUND
):
2814 builtin_optab
= lround_optab
;
2821 /* There's no easy way to detect the case we need to set EDOM. */
2822 if (flag_errno_math
&& fallback_fn
== BUILT_IN_NONE
)
2825 /* Make a suitable register to place result in. */
2826 mode
= TYPE_MODE (TREE_TYPE (exp
));
2828 /* There's no easy way to detect the case we need to set EDOM. */
2829 if (!flag_errno_math
)
2831 rtx result
= gen_reg_rtx (mode
);
2833 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2834 need to expand the argument again. This way, we will not perform
2835 side-effects more the once. */
2836 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
2838 op0
= expand_expr (arg
, NULL
, VOIDmode
, EXPAND_NORMAL
);
2842 if (expand_sfix_optab (result
, op0
, builtin_optab
))
2844 /* Output the entire sequence. */
2845 insns
= get_insns ();
2851 /* If we were unable to expand via the builtin, stop the sequence
2852 (without outputting the insns) and call to the library function
2853 with the stabilized argument list. */
2857 if (fallback_fn
!= BUILT_IN_NONE
)
2859 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2860 targets, (int) round (x) should never be transformed into
2861 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2862 a call to lround in the hope that the target provides at least some
2863 C99 functions. This should result in the best user experience for
2864 not full C99 targets. */
2865 tree fallback_fndecl
= mathfn_built_in_1 (TREE_TYPE (arg
),
2868 exp
= build_call_nofold_loc (EXPR_LOCATION (exp
),
2869 fallback_fndecl
, 1, arg
);
2871 target
= expand_call (exp
, NULL_RTX
, target
== const0_rtx
);
2872 target
= maybe_emit_group_store (target
, TREE_TYPE (exp
));
2873 return convert_to_mode (mode
, target
, 0);
2876 return expand_call (exp
, target
, target
== const0_rtx
);
2879 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2880 a normal call should be emitted rather than expanding the function
2881 in-line. EXP is the expression that is a call to the builtin
2882 function; if convenient, the result should be placed in TARGET. */
2885 expand_builtin_powi (tree exp
, rtx target
)
2892 if (! validate_arglist (exp
, REAL_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
2895 arg0
= CALL_EXPR_ARG (exp
, 0);
2896 arg1
= CALL_EXPR_ARG (exp
, 1);
2897 mode
= TYPE_MODE (TREE_TYPE (exp
));
2899 /* Emit a libcall to libgcc. */
2901 /* Mode of the 2nd argument must match that of an int. */
2902 mode2
= mode_for_size (INT_TYPE_SIZE
, MODE_INT
, 0);
2904 if (target
== NULL_RTX
)
2905 target
= gen_reg_rtx (mode
);
2907 op0
= expand_expr (arg0
, NULL_RTX
, mode
, EXPAND_NORMAL
);
2908 if (GET_MODE (op0
) != mode
)
2909 op0
= convert_to_mode (mode
, op0
, 0);
2910 op1
= expand_expr (arg1
, NULL_RTX
, mode2
, EXPAND_NORMAL
);
2911 if (GET_MODE (op1
) != mode2
)
2912 op1
= convert_to_mode (mode2
, op1
, 0);
2914 target
= emit_library_call_value (optab_libfunc (powi_optab
, mode
),
2915 target
, LCT_CONST
, mode
, 2,
2916 op0
, mode
, op1
, mode2
);
2921 /* Expand expression EXP which is a call to the strlen builtin. Return
2922 NULL_RTX if we failed the caller should emit a normal call, otherwise
2923 try to get the result in TARGET, if convenient. */
2926 expand_builtin_strlen (tree exp
, rtx target
,
2927 machine_mode target_mode
)
2929 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
2933 struct expand_operand ops
[4];
2936 tree src
= CALL_EXPR_ARG (exp
, 0);
2938 rtx_insn
*before_strlen
;
2939 machine_mode insn_mode
= target_mode
;
2940 enum insn_code icode
= CODE_FOR_nothing
;
2943 /* If the length can be computed at compile-time, return it. */
2944 len
= c_strlen (src
, 0);
2946 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2948 /* If the length can be computed at compile-time and is constant
2949 integer, but there are side-effects in src, evaluate
2950 src for side-effects, then return len.
2951 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2952 can be optimized into: i++; x = 3; */
2953 len
= c_strlen (src
, 1);
2954 if (len
&& TREE_CODE (len
) == INTEGER_CST
)
2956 expand_expr (src
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
2957 return expand_expr (len
, target
, target_mode
, EXPAND_NORMAL
);
2960 align
= get_pointer_alignment (src
) / BITS_PER_UNIT
;
2962 /* If SRC is not a pointer type, don't do this operation inline. */
2966 /* Bail out if we can't compute strlen in the right mode. */
2967 while (insn_mode
!= VOIDmode
)
2969 icode
= optab_handler (strlen_optab
, insn_mode
);
2970 if (icode
!= CODE_FOR_nothing
)
2973 insn_mode
= GET_MODE_WIDER_MODE (insn_mode
);
2975 if (insn_mode
== VOIDmode
)
2978 /* Make a place to hold the source address. We will not expand
2979 the actual source until we are sure that the expansion will
2980 not fail -- there are trees that cannot be expanded twice. */
2981 src_reg
= gen_reg_rtx (Pmode
);
2983 /* Mark the beginning of the strlen sequence so we can emit the
2984 source operand later. */
2985 before_strlen
= get_last_insn ();
2987 create_output_operand (&ops
[0], target
, insn_mode
);
2988 create_fixed_operand (&ops
[1], gen_rtx_MEM (BLKmode
, src_reg
));
2989 create_integer_operand (&ops
[2], 0);
2990 create_integer_operand (&ops
[3], align
);
2991 if (!maybe_expand_insn (icode
, 4, ops
))
2994 /* Now that we are assured of success, expand the source. */
2996 pat
= expand_expr (src
, src_reg
, Pmode
, EXPAND_NORMAL
);
2999 #ifdef POINTERS_EXTEND_UNSIGNED
3000 if (GET_MODE (pat
) != Pmode
)
3001 pat
= convert_to_mode (Pmode
, pat
,
3002 POINTERS_EXTEND_UNSIGNED
);
3004 emit_move_insn (src_reg
, pat
);
3010 emit_insn_after (pat
, before_strlen
);
3012 emit_insn_before (pat
, get_insns ());
3014 /* Return the value in the proper mode for this function. */
3015 if (GET_MODE (ops
[0].value
) == target_mode
)
3016 target
= ops
[0].value
;
3017 else if (target
!= 0)
3018 convert_move (target
, ops
[0].value
, 0);
3020 target
= convert_to_mode (target_mode
, ops
[0].value
, 0);
3026 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3027 bytes from constant string DATA + OFFSET and return it as target
3031 builtin_memcpy_read_str (void *data
, HOST_WIDE_INT offset
,
3034 const char *str
= (const char *) data
;
3036 gcc_assert (offset
>= 0
3037 && ((unsigned HOST_WIDE_INT
) offset
+ GET_MODE_SIZE (mode
)
3038 <= strlen (str
) + 1));
3040 return c_readstr (str
+ offset
, mode
);
3043 /* LEN specify length of the block of memcpy/memset operation.
3044 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3045 In some cases we can make very likely guess on max size, then we
3046 set it into PROBABLE_MAX_SIZE. */
3049 determine_block_size (tree len
, rtx len_rtx
,
3050 unsigned HOST_WIDE_INT
*min_size
,
3051 unsigned HOST_WIDE_INT
*max_size
,
3052 unsigned HOST_WIDE_INT
*probable_max_size
)
3054 if (CONST_INT_P (len_rtx
))
3056 *min_size
= *max_size
= *probable_max_size
= UINTVAL (len_rtx
);
3062 enum value_range_type range_type
= VR_UNDEFINED
;
3064 /* Determine bounds from the type. */
3065 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len
))))
3066 *min_size
= tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len
)));
3069 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len
))))
3070 *probable_max_size
= *max_size
3071 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len
)));
3073 *probable_max_size
= *max_size
= GET_MODE_MASK (GET_MODE (len_rtx
));
3075 if (TREE_CODE (len
) == SSA_NAME
)
3076 range_type
= get_range_info (len
, &min
, &max
);
3077 if (range_type
== VR_RANGE
)
3079 if (wi::fits_uhwi_p (min
) && *min_size
< min
.to_uhwi ())
3080 *min_size
= min
.to_uhwi ();
3081 if (wi::fits_uhwi_p (max
) && *max_size
> max
.to_uhwi ())
3082 *probable_max_size
= *max_size
= max
.to_uhwi ();
3084 else if (range_type
== VR_ANTI_RANGE
)
3086 /* Anti range 0...N lets us to determine minimal size to N+1. */
3089 if (wi::fits_uhwi_p (max
) && max
.to_uhwi () + 1 != 0)
3090 *min_size
= max
.to_uhwi () + 1;
3098 Produce anti range allowing negative values of N. We still
3099 can use the information and make a guess that N is not negative.
3101 else if (!wi::leu_p (max
, 1 << 30) && wi::fits_uhwi_p (min
))
3102 *probable_max_size
= min
.to_uhwi () - 1;
3105 gcc_checking_assert (*max_size
<=
3106 (unsigned HOST_WIDE_INT
)
3107 GET_MODE_MASK (GET_MODE (len_rtx
)));
3110 /* Helper function to do the actual work for expand_builtin_memcpy. */
3113 expand_builtin_memcpy_args (tree dest
, tree src
, tree len
, rtx target
, tree exp
)
3115 const char *src_str
;
3116 unsigned int src_align
= get_pointer_alignment (src
);
3117 unsigned int dest_align
= get_pointer_alignment (dest
);
3118 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
3119 HOST_WIDE_INT expected_size
= -1;
3120 unsigned int expected_align
= 0;
3121 unsigned HOST_WIDE_INT min_size
;
3122 unsigned HOST_WIDE_INT max_size
;
3123 unsigned HOST_WIDE_INT probable_max_size
;
3125 /* If DEST is not a pointer type, call the normal function. */
3126 if (dest_align
== 0)
3129 /* If either SRC is not a pointer type, don't do this
3130 operation in-line. */
3134 if (currently_expanding_gimple_stmt
)
3135 stringop_block_profile (currently_expanding_gimple_stmt
,
3136 &expected_align
, &expected_size
);
3138 if (expected_align
< dest_align
)
3139 expected_align
= dest_align
;
3140 dest_mem
= get_memory_rtx (dest
, len
);
3141 set_mem_align (dest_mem
, dest_align
);
3142 len_rtx
= expand_normal (len
);
3143 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3144 &probable_max_size
);
3145 src_str
= c_getstr (src
);
3147 /* If SRC is a string constant and block move would be done
3148 by pieces, we can avoid loading the string from memory
3149 and only stored the computed constants. */
3151 && CONST_INT_P (len_rtx
)
3152 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3153 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3154 CONST_CAST (char *, src_str
),
3157 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3158 builtin_memcpy_read_str
,
3159 CONST_CAST (char *, src_str
),
3160 dest_align
, false, 0);
3161 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3162 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3166 src_mem
= get_memory_rtx (src
, len
);
3167 set_mem_align (src_mem
, src_align
);
3169 /* Copy word part most expediently. */
3170 dest_addr
= emit_block_move_hints (dest_mem
, src_mem
, len_rtx
,
3171 CALL_EXPR_TAILCALL (exp
)
3172 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3173 expected_align
, expected_size
,
3174 min_size
, max_size
, probable_max_size
);
3178 dest_addr
= force_operand (XEXP (dest_mem
, 0), target
);
3179 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3185 /* Expand a call EXP to the memcpy builtin.
3186 Return NULL_RTX if we failed, the caller should emit a normal call,
3187 otherwise try to get the result in TARGET, if convenient (and in
3188 mode MODE if that's convenient). */
3191 expand_builtin_memcpy (tree exp
, rtx target
)
3193 if (!validate_arglist (exp
,
3194 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3198 tree dest
= CALL_EXPR_ARG (exp
, 0);
3199 tree src
= CALL_EXPR_ARG (exp
, 1);
3200 tree len
= CALL_EXPR_ARG (exp
, 2);
3201 return expand_builtin_memcpy_args (dest
, src
, len
, target
, exp
);
3205 /* Expand an instrumented call EXP to the memcpy builtin.
3206 Return NULL_RTX if we failed, the caller should emit a normal call,
3207 otherwise try to get the result in TARGET, if convenient (and in
3208 mode MODE if that's convenient). */
3211 expand_builtin_memcpy_with_bounds (tree exp
, rtx target
)
3213 if (!validate_arglist (exp
,
3214 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3215 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3216 INTEGER_TYPE
, VOID_TYPE
))
3220 tree dest
= CALL_EXPR_ARG (exp
, 0);
3221 tree src
= CALL_EXPR_ARG (exp
, 2);
3222 tree len
= CALL_EXPR_ARG (exp
, 4);
3223 rtx res
= expand_builtin_memcpy_args (dest
, src
, len
, target
, exp
);
3225 /* Return src bounds with the result. */
3228 rtx bnd
= force_reg (targetm
.chkp_bound_mode (),
3229 expand_normal (CALL_EXPR_ARG (exp
, 1)));
3230 res
= chkp_join_splitted_slot (res
, bnd
);
3236 /* Expand a call EXP to the mempcpy builtin.
3237 Return NULL_RTX if we failed; the caller should emit a normal call,
3238 otherwise try to get the result in TARGET, if convenient (and in
3239 mode MODE if that's convenient). If ENDP is 0 return the
3240 destination pointer, if ENDP is 1 return the end pointer ala
3241 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3245 expand_builtin_mempcpy (tree exp
, rtx target
, machine_mode mode
)
3247 if (!validate_arglist (exp
,
3248 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3252 tree dest
= CALL_EXPR_ARG (exp
, 0);
3253 tree src
= CALL_EXPR_ARG (exp
, 1);
3254 tree len
= CALL_EXPR_ARG (exp
, 2);
3255 return expand_builtin_mempcpy_args (dest
, src
, len
,
3256 target
, mode
, /*endp=*/ 1,
3261 /* Expand an instrumented call EXP to the mempcpy builtin.
3262 Return NULL_RTX if we failed, the caller should emit a normal call,
3263 otherwise try to get the result in TARGET, if convenient (and in
3264 mode MODE if that's convenient). */
3267 expand_builtin_mempcpy_with_bounds (tree exp
, rtx target
, machine_mode mode
)
3269 if (!validate_arglist (exp
,
3270 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3271 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3272 INTEGER_TYPE
, VOID_TYPE
))
3276 tree dest
= CALL_EXPR_ARG (exp
, 0);
3277 tree src
= CALL_EXPR_ARG (exp
, 2);
3278 tree len
= CALL_EXPR_ARG (exp
, 4);
3279 rtx res
= expand_builtin_mempcpy_args (dest
, src
, len
, target
,
3282 /* Return src bounds with the result. */
3285 rtx bnd
= force_reg (targetm
.chkp_bound_mode (),
3286 expand_normal (CALL_EXPR_ARG (exp
, 1)));
3287 res
= chkp_join_splitted_slot (res
, bnd
);
3293 /* Helper function to do the actual work for expand_builtin_mempcpy. The
3294 arguments to the builtin_mempcpy call DEST, SRC, and LEN are broken out
3295 so that this can also be called without constructing an actual CALL_EXPR.
3296 The other arguments and return value are the same as for
3297 expand_builtin_mempcpy. */
3300 expand_builtin_mempcpy_args (tree dest
, tree src
, tree len
,
3301 rtx target
, machine_mode mode
, int endp
,
3304 tree fndecl
= get_callee_fndecl (orig_exp
);
3306 /* If return value is ignored, transform mempcpy into memcpy. */
3307 if (target
== const0_rtx
3308 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
3309 && builtin_decl_implicit_p (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP
))
3311 tree fn
= builtin_decl_implicit (BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP
);
3312 tree result
= build_call_nofold_loc (UNKNOWN_LOCATION
, fn
, 3,
3314 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3316 else if (target
== const0_rtx
3317 && builtin_decl_implicit_p (BUILT_IN_MEMCPY
))
3319 tree fn
= builtin_decl_implicit (BUILT_IN_MEMCPY
);
3320 tree result
= build_call_nofold_loc (UNKNOWN_LOCATION
, fn
, 3,
3322 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3326 const char *src_str
;
3327 unsigned int src_align
= get_pointer_alignment (src
);
3328 unsigned int dest_align
= get_pointer_alignment (dest
);
3329 rtx dest_mem
, src_mem
, len_rtx
;
3331 /* If either SRC or DEST is not a pointer type, don't do this
3332 operation in-line. */
3333 if (dest_align
== 0 || src_align
== 0)
3336 /* If LEN is not constant, call the normal function. */
3337 if (! tree_fits_uhwi_p (len
))
3340 len_rtx
= expand_normal (len
);
3341 src_str
= c_getstr (src
);
3343 /* If SRC is a string constant and block move would be done
3344 by pieces, we can avoid loading the string from memory
3345 and only stored the computed constants. */
3347 && CONST_INT_P (len_rtx
)
3348 && (unsigned HOST_WIDE_INT
) INTVAL (len_rtx
) <= strlen (src_str
) + 1
3349 && can_store_by_pieces (INTVAL (len_rtx
), builtin_memcpy_read_str
,
3350 CONST_CAST (char *, src_str
),
3353 dest_mem
= get_memory_rtx (dest
, len
);
3354 set_mem_align (dest_mem
, dest_align
);
3355 dest_mem
= store_by_pieces (dest_mem
, INTVAL (len_rtx
),
3356 builtin_memcpy_read_str
,
3357 CONST_CAST (char *, src_str
),
3358 dest_align
, false, endp
);
3359 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3360 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3364 if (CONST_INT_P (len_rtx
)
3365 && can_move_by_pieces (INTVAL (len_rtx
),
3366 MIN (dest_align
, src_align
)))
3368 dest_mem
= get_memory_rtx (dest
, len
);
3369 set_mem_align (dest_mem
, dest_align
);
3370 src_mem
= get_memory_rtx (src
, len
);
3371 set_mem_align (src_mem
, src_align
);
3372 dest_mem
= move_by_pieces (dest_mem
, src_mem
, INTVAL (len_rtx
),
3373 MIN (dest_align
, src_align
), endp
);
3374 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3375 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3383 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3384 we failed, the caller should emit a normal call, otherwise try to
3385 get the result in TARGET, if convenient. If ENDP is 0 return the
3386 destination pointer, if ENDP is 1 return the end pointer ala
3387 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3391 expand_movstr (tree dest
, tree src
, rtx target
, int endp
)
3393 struct expand_operand ops
[3];
3397 if (!targetm
.have_movstr ())
3400 dest_mem
= get_memory_rtx (dest
, NULL
);
3401 src_mem
= get_memory_rtx (src
, NULL
);
3404 target
= force_reg (Pmode
, XEXP (dest_mem
, 0));
3405 dest_mem
= replace_equiv_address (dest_mem
, target
);
3408 create_output_operand (&ops
[0], endp
? target
: NULL_RTX
, Pmode
);
3409 create_fixed_operand (&ops
[1], dest_mem
);
3410 create_fixed_operand (&ops
[2], src_mem
);
3411 if (!maybe_expand_insn (targetm
.code_for_movstr
, 3, ops
))
3414 if (endp
&& target
!= const0_rtx
)
3416 target
= ops
[0].value
;
3417 /* movstr is supposed to set end to the address of the NUL
3418 terminator. If the caller requested a mempcpy-like return value,
3422 rtx tem
= plus_constant (GET_MODE (target
),
3423 gen_lowpart (GET_MODE (target
), target
), 1);
3424 emit_move_insn (target
, force_operand (tem
, NULL_RTX
));
3430 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3431 NULL_RTX if we failed the caller should emit a normal call, otherwise
3432 try to get the result in TARGET, if convenient (and in mode MODE if that's
3436 expand_builtin_strcpy (tree exp
, rtx target
)
3438 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3440 tree dest
= CALL_EXPR_ARG (exp
, 0);
3441 tree src
= CALL_EXPR_ARG (exp
, 1);
3442 return expand_builtin_strcpy_args (dest
, src
, target
);
3447 /* Helper function to do the actual work for expand_builtin_strcpy. The
3448 arguments to the builtin_strcpy call DEST and SRC are broken out
3449 so that this can also be called without constructing an actual CALL_EXPR.
3450 The other arguments and return value are the same as for
3451 expand_builtin_strcpy. */
3454 expand_builtin_strcpy_args (tree dest
, tree src
, rtx target
)
3456 return expand_movstr (dest
, src
, target
, /*endp=*/0);
3459 /* Expand a call EXP to the stpcpy builtin.
3460 Return NULL_RTX if we failed the caller should emit a normal call,
3461 otherwise try to get the result in TARGET, if convenient (and in
3462 mode MODE if that's convenient). */
3465 expand_builtin_stpcpy (tree exp
, rtx target
, machine_mode mode
)
3468 location_t loc
= EXPR_LOCATION (exp
);
3470 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3473 dst
= CALL_EXPR_ARG (exp
, 0);
3474 src
= CALL_EXPR_ARG (exp
, 1);
3476 /* If return value is ignored, transform stpcpy into strcpy. */
3477 if (target
== const0_rtx
&& builtin_decl_implicit (BUILT_IN_STRCPY
))
3479 tree fn
= builtin_decl_implicit (BUILT_IN_STRCPY
);
3480 tree result
= build_call_nofold_loc (loc
, fn
, 2, dst
, src
);
3481 return expand_expr (result
, target
, mode
, EXPAND_NORMAL
);
3488 /* Ensure we get an actual string whose length can be evaluated at
3489 compile-time, not an expression containing a string. This is
3490 because the latter will potentially produce pessimized code
3491 when used to produce the return value. */
3492 if (! c_getstr (src
) || ! (len
= c_strlen (src
, 0)))
3493 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3495 lenp1
= size_binop_loc (loc
, PLUS_EXPR
, len
, ssize_int (1));
3496 ret
= expand_builtin_mempcpy_args (dst
, src
, lenp1
,
3497 target
, mode
, /*endp=*/2,
3503 if (TREE_CODE (len
) == INTEGER_CST
)
3505 rtx len_rtx
= expand_normal (len
);
3507 if (CONST_INT_P (len_rtx
))
3509 ret
= expand_builtin_strcpy_args (dst
, src
, target
);
3515 if (mode
!= VOIDmode
)
3516 target
= gen_reg_rtx (mode
);
3518 target
= gen_reg_rtx (GET_MODE (ret
));
3520 if (GET_MODE (target
) != GET_MODE (ret
))
3521 ret
= gen_lowpart (GET_MODE (target
), ret
);
3523 ret
= plus_constant (GET_MODE (ret
), ret
, INTVAL (len_rtx
));
3524 ret
= emit_move_insn (target
, force_operand (ret
, NULL_RTX
));
3532 return expand_movstr (dst
, src
, target
, /*endp=*/2);
3536 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3537 bytes from constant string DATA + OFFSET and return it as target
3541 builtin_strncpy_read_str (void *data
, HOST_WIDE_INT offset
,
3544 const char *str
= (const char *) data
;
3546 if ((unsigned HOST_WIDE_INT
) offset
> strlen (str
))
3549 return c_readstr (str
+ offset
, mode
);
3552 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3553 NULL_RTX if we failed the caller should emit a normal call. */
3556 expand_builtin_strncpy (tree exp
, rtx target
)
3558 location_t loc
= EXPR_LOCATION (exp
);
3560 if (validate_arglist (exp
,
3561 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3563 tree dest
= CALL_EXPR_ARG (exp
, 0);
3564 tree src
= CALL_EXPR_ARG (exp
, 1);
3565 tree len
= CALL_EXPR_ARG (exp
, 2);
3566 tree slen
= c_strlen (src
, 1);
3568 /* We must be passed a constant len and src parameter. */
3569 if (!tree_fits_uhwi_p (len
) || !slen
|| !tree_fits_uhwi_p (slen
))
3572 slen
= size_binop_loc (loc
, PLUS_EXPR
, slen
, ssize_int (1));
3574 /* We're required to pad with trailing zeros if the requested
3575 len is greater than strlen(s2)+1. In that case try to
3576 use store_by_pieces, if it fails, punt. */
3577 if (tree_int_cst_lt (slen
, len
))
3579 unsigned int dest_align
= get_pointer_alignment (dest
);
3580 const char *p
= c_getstr (src
);
3583 if (!p
|| dest_align
== 0 || !tree_fits_uhwi_p (len
)
3584 || !can_store_by_pieces (tree_to_uhwi (len
),
3585 builtin_strncpy_read_str
,
3586 CONST_CAST (char *, p
),
3590 dest_mem
= get_memory_rtx (dest
, len
);
3591 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3592 builtin_strncpy_read_str
,
3593 CONST_CAST (char *, p
), dest_align
, false, 0);
3594 dest_mem
= force_operand (XEXP (dest_mem
, 0), target
);
3595 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3602 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3603 bytes from constant string DATA + OFFSET and return it as target
3607 builtin_memset_read_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3610 const char *c
= (const char *) data
;
3611 char *p
= XALLOCAVEC (char, GET_MODE_SIZE (mode
));
3613 memset (p
, *c
, GET_MODE_SIZE (mode
));
3615 return c_readstr (p
, mode
);
3618 /* Callback routine for store_by_pieces. Return the RTL of a register
3619 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3620 char value given in the RTL register data. For example, if mode is
3621 4 bytes wide, return the RTL for 0x01010101*data. */
3624 builtin_memset_gen_str (void *data
, HOST_WIDE_INT offset ATTRIBUTE_UNUSED
,
3631 size
= GET_MODE_SIZE (mode
);
3635 p
= XALLOCAVEC (char, size
);
3636 memset (p
, 1, size
);
3637 coeff
= c_readstr (p
, mode
);
3639 target
= convert_to_mode (mode
, (rtx
) data
, 1);
3640 target
= expand_mult (mode
, target
, coeff
, NULL_RTX
, 1);
3641 return force_reg (mode
, target
);
3644 /* Expand expression EXP, which is a call to the memset builtin. Return
3645 NULL_RTX if we failed the caller should emit a normal call, otherwise
3646 try to get the result in TARGET, if convenient (and in mode MODE if that's
3650 expand_builtin_memset (tree exp
, rtx target
, machine_mode mode
)
3652 if (!validate_arglist (exp
,
3653 POINTER_TYPE
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3657 tree dest
= CALL_EXPR_ARG (exp
, 0);
3658 tree val
= CALL_EXPR_ARG (exp
, 1);
3659 tree len
= CALL_EXPR_ARG (exp
, 2);
3660 return expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
3664 /* Expand expression EXP, which is an instrumented call to the memset builtin.
3665 Return NULL_RTX if we failed the caller should emit a normal call, otherwise
3666 try to get the result in TARGET, if convenient (and in mode MODE if that's
3670 expand_builtin_memset_with_bounds (tree exp
, rtx target
, machine_mode mode
)
3672 if (!validate_arglist (exp
,
3673 POINTER_TYPE
, POINTER_BOUNDS_TYPE
,
3674 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3678 tree dest
= CALL_EXPR_ARG (exp
, 0);
3679 tree val
= CALL_EXPR_ARG (exp
, 2);
3680 tree len
= CALL_EXPR_ARG (exp
, 3);
3681 rtx res
= expand_builtin_memset_args (dest
, val
, len
, target
, mode
, exp
);
3683 /* Return src bounds with the result. */
3686 rtx bnd
= force_reg (targetm
.chkp_bound_mode (),
3687 expand_normal (CALL_EXPR_ARG (exp
, 1)));
3688 res
= chkp_join_splitted_slot (res
, bnd
);
3694 /* Helper function to do the actual work for expand_builtin_memset. The
3695 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
3696 so that this can also be called without constructing an actual CALL_EXPR.
3697 The other arguments and return value are the same as for
3698 expand_builtin_memset. */
3701 expand_builtin_memset_args (tree dest
, tree val
, tree len
,
3702 rtx target
, machine_mode mode
, tree orig_exp
)
3705 enum built_in_function fcode
;
3706 machine_mode val_mode
;
3708 unsigned int dest_align
;
3709 rtx dest_mem
, dest_addr
, len_rtx
;
3710 HOST_WIDE_INT expected_size
= -1;
3711 unsigned int expected_align
= 0;
3712 unsigned HOST_WIDE_INT min_size
;
3713 unsigned HOST_WIDE_INT max_size
;
3714 unsigned HOST_WIDE_INT probable_max_size
;
3716 dest_align
= get_pointer_alignment (dest
);
3718 /* If DEST is not a pointer type, don't do this operation in-line. */
3719 if (dest_align
== 0)
3722 if (currently_expanding_gimple_stmt
)
3723 stringop_block_profile (currently_expanding_gimple_stmt
,
3724 &expected_align
, &expected_size
);
3726 if (expected_align
< dest_align
)
3727 expected_align
= dest_align
;
3729 /* If the LEN parameter is zero, return DEST. */
3730 if (integer_zerop (len
))
3732 /* Evaluate and ignore VAL in case it has side-effects. */
3733 expand_expr (val
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
3734 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
3737 /* Stabilize the arguments in case we fail. */
3738 dest
= builtin_save_expr (dest
);
3739 val
= builtin_save_expr (val
);
3740 len
= builtin_save_expr (len
);
3742 len_rtx
= expand_normal (len
);
3743 determine_block_size (len
, len_rtx
, &min_size
, &max_size
,
3744 &probable_max_size
);
3745 dest_mem
= get_memory_rtx (dest
, len
);
3746 val_mode
= TYPE_MODE (unsigned_char_type_node
);
3748 if (TREE_CODE (val
) != INTEGER_CST
)
3752 val_rtx
= expand_normal (val
);
3753 val_rtx
= convert_to_mode (val_mode
, val_rtx
, 0);
3755 /* Assume that we can memset by pieces if we can store
3756 * the coefficients by pieces (in the required modes).
3757 * We can't pass builtin_memset_gen_str as that emits RTL. */
3759 if (tree_fits_uhwi_p (len
)
3760 && can_store_by_pieces (tree_to_uhwi (len
),
3761 builtin_memset_read_str
, &c
, dest_align
,
3764 val_rtx
= force_reg (val_mode
, val_rtx
);
3765 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3766 builtin_memset_gen_str
, val_rtx
, dest_align
,
3769 else if (!set_storage_via_setmem (dest_mem
, len_rtx
, val_rtx
,
3770 dest_align
, expected_align
,
3771 expected_size
, min_size
, max_size
,
3775 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3776 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3780 if (target_char_cast (val
, &c
))
3785 if (tree_fits_uhwi_p (len
)
3786 && can_store_by_pieces (tree_to_uhwi (len
),
3787 builtin_memset_read_str
, &c
, dest_align
,
3789 store_by_pieces (dest_mem
, tree_to_uhwi (len
),
3790 builtin_memset_read_str
, &c
, dest_align
, true, 0);
3791 else if (!set_storage_via_setmem (dest_mem
, len_rtx
,
3792 gen_int_mode (c
, val_mode
),
3793 dest_align
, expected_align
,
3794 expected_size
, min_size
, max_size
,
3798 dest_mem
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3799 dest_mem
= convert_memory_address (ptr_mode
, dest_mem
);
3803 set_mem_align (dest_mem
, dest_align
);
3804 dest_addr
= clear_storage_hints (dest_mem
, len_rtx
,
3805 CALL_EXPR_TAILCALL (orig_exp
)
3806 ? BLOCK_OP_TAILCALL
: BLOCK_OP_NORMAL
,
3807 expected_align
, expected_size
,
3813 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
3814 dest_addr
= convert_memory_address (ptr_mode
, dest_addr
);
3820 fndecl
= get_callee_fndecl (orig_exp
);
3821 fcode
= DECL_FUNCTION_CODE (fndecl
);
3822 if (fcode
== BUILT_IN_MEMSET
3823 || fcode
== BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP
)
3824 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 3,
3826 else if (fcode
== BUILT_IN_BZERO
)
3827 fn
= build_call_nofold_loc (EXPR_LOCATION (orig_exp
), fndecl
, 2,
3831 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
3832 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (orig_exp
);
3833 return expand_call (fn
, target
, target
== const0_rtx
);
3836 /* Expand expression EXP, which is a call to the bzero builtin. Return
3837 NULL_RTX if we failed the caller should emit a normal call. */
3840 expand_builtin_bzero (tree exp
)
3843 location_t loc
= EXPR_LOCATION (exp
);
3845 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3848 dest
= CALL_EXPR_ARG (exp
, 0);
3849 size
= CALL_EXPR_ARG (exp
, 1);
3851 /* New argument list transforming bzero(ptr x, int y) to
3852 memset(ptr x, int 0, size_t y). This is done this way
3853 so that if it isn't expanded inline, we fallback to
3854 calling bzero instead of memset. */
3856 return expand_builtin_memset_args (dest
, integer_zero_node
,
3857 fold_convert_loc (loc
,
3858 size_type_node
, size
),
3859 const0_rtx
, VOIDmode
, exp
);
3862 /* Try to expand cmpstr operation ICODE with the given operands.
3863 Return the result rtx on success, otherwise return null. */
3866 expand_cmpstr (insn_code icode
, rtx target
, rtx arg1_rtx
, rtx arg2_rtx
,
3867 HOST_WIDE_INT align
)
3869 machine_mode insn_mode
= insn_data
[icode
].operand
[0].mode
;
3871 if (target
&& (!REG_P (target
) || HARD_REGISTER_P (target
)))
3874 struct expand_operand ops
[4];
3875 create_output_operand (&ops
[0], target
, insn_mode
);
3876 create_fixed_operand (&ops
[1], arg1_rtx
);
3877 create_fixed_operand (&ops
[2], arg2_rtx
);
3878 create_integer_operand (&ops
[3], align
);
3879 if (maybe_expand_insn (icode
, 4, ops
))
3880 return ops
[0].value
;
3884 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
3885 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
3886 otherwise return null. */
3889 expand_cmpstrn_or_cmpmem (insn_code icode
, rtx target
, rtx arg1_rtx
,
3890 rtx arg2_rtx
, tree arg3_type
, rtx arg3_rtx
,
3891 HOST_WIDE_INT align
)
3893 machine_mode insn_mode
= insn_data
[icode
].operand
[0].mode
;
3895 if (target
&& (!REG_P (target
) || HARD_REGISTER_P (target
)))
3898 struct expand_operand ops
[5];
3899 create_output_operand (&ops
[0], target
, insn_mode
);
3900 create_fixed_operand (&ops
[1], arg1_rtx
);
3901 create_fixed_operand (&ops
[2], arg2_rtx
);
3902 create_convert_operand_from (&ops
[3], arg3_rtx
, TYPE_MODE (arg3_type
),
3903 TYPE_UNSIGNED (arg3_type
));
3904 create_integer_operand (&ops
[4], align
);
3905 if (maybe_expand_insn (icode
, 5, ops
))
3906 return ops
[0].value
;
3910 /* Expand expression EXP, which is a call to the memcmp built-in function.
3911 Return NULL_RTX if we failed and the caller should emit a normal call,
3912 otherwise try to get the result in TARGET, if convenient. */
3915 expand_builtin_memcmp (tree exp
, rtx target
)
3917 if (!validate_arglist (exp
,
3918 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
3921 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
3922 implementing memcmp because it will stop if it encounters two
3924 insn_code icode
= direct_optab_handler (cmpmem_optab
, SImode
);
3925 if (icode
== CODE_FOR_nothing
)
3928 tree arg1
= CALL_EXPR_ARG (exp
, 0);
3929 tree arg2
= CALL_EXPR_ARG (exp
, 1);
3930 tree len
= CALL_EXPR_ARG (exp
, 2);
3932 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
3933 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
3935 /* If we don't have POINTER_TYPE, call the function. */
3936 if (arg1_align
== 0 || arg2_align
== 0)
3939 machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
3940 location_t loc
= EXPR_LOCATION (exp
);
3941 rtx arg1_rtx
= get_memory_rtx (arg1
, len
);
3942 rtx arg2_rtx
= get_memory_rtx (arg2
, len
);
3943 rtx arg3_rtx
= expand_normal (fold_convert_loc (loc
, sizetype
, len
));
3945 /* Set MEM_SIZE as appropriate. */
3946 if (CONST_INT_P (arg3_rtx
))
3948 set_mem_size (arg1_rtx
, INTVAL (arg3_rtx
));
3949 set_mem_size (arg2_rtx
, INTVAL (arg3_rtx
));
3952 rtx result
= expand_cmpstrn_or_cmpmem (icode
, target
, arg1_rtx
, arg2_rtx
,
3953 TREE_TYPE (len
), arg3_rtx
,
3954 MIN (arg1_align
, arg2_align
));
3957 /* Return the value in the proper mode for this function. */
3958 if (GET_MODE (result
) == mode
)
3963 convert_move (target
, result
, 0);
3967 return convert_to_mode (mode
, result
, 0);
3972 && REG_P (result
) && GET_MODE (result
) == mode
3973 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
3974 result
= gen_reg_rtx (mode
);
3976 emit_library_call_value (memcmp_libfunc
, result
, LCT_PURE
,
3977 TYPE_MODE (integer_type_node
), 3,
3978 XEXP (arg1_rtx
, 0), Pmode
,
3979 XEXP (arg2_rtx
, 0), Pmode
,
3980 convert_to_mode (TYPE_MODE (sizetype
), arg3_rtx
,
3981 TYPE_UNSIGNED (sizetype
)),
3982 TYPE_MODE (sizetype
));
3986 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
3987 if we failed the caller should emit a normal call, otherwise try to get
3988 the result in TARGET, if convenient. */
3991 expand_builtin_strcmp (tree exp
, ATTRIBUTE_UNUSED rtx target
)
3993 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
3996 insn_code cmpstr_icode
= direct_optab_handler (cmpstr_optab
, SImode
);
3997 insn_code cmpstrn_icode
= direct_optab_handler (cmpstrn_optab
, SImode
);
3998 if (cmpstr_icode
!= CODE_FOR_nothing
|| cmpstrn_icode
!= CODE_FOR_nothing
)
4000 rtx arg1_rtx
, arg2_rtx
;
4002 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4003 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4004 rtx result
= NULL_RTX
;
4006 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4007 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4009 /* If we don't have POINTER_TYPE, call the function. */
4010 if (arg1_align
== 0 || arg2_align
== 0)
4013 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4014 arg1
= builtin_save_expr (arg1
);
4015 arg2
= builtin_save_expr (arg2
);
4017 arg1_rtx
= get_memory_rtx (arg1
, NULL
);
4018 arg2_rtx
= get_memory_rtx (arg2
, NULL
);
4020 /* Try to call cmpstrsi. */
4021 if (cmpstr_icode
!= CODE_FOR_nothing
)
4022 result
= expand_cmpstr (cmpstr_icode
, target
, arg1_rtx
, arg2_rtx
,
4023 MIN (arg1_align
, arg2_align
));
4025 /* Try to determine at least one length and call cmpstrnsi. */
4026 if (!result
&& cmpstrn_icode
!= CODE_FOR_nothing
)
4031 tree len1
= c_strlen (arg1
, 1);
4032 tree len2
= c_strlen (arg2
, 1);
4035 len1
= size_binop (PLUS_EXPR
, ssize_int (1), len1
);
4037 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
4039 /* If we don't have a constant length for the first, use the length
4040 of the second, if we know it. We don't require a constant for
4041 this case; some cost analysis could be done if both are available
4042 but neither is constant. For now, assume they're equally cheap,
4043 unless one has side effects. If both strings have constant lengths,
4050 else if (TREE_SIDE_EFFECTS (len1
))
4052 else if (TREE_SIDE_EFFECTS (len2
))
4054 else if (TREE_CODE (len1
) != INTEGER_CST
)
4056 else if (TREE_CODE (len2
) != INTEGER_CST
)
4058 else if (tree_int_cst_lt (len1
, len2
))
4063 /* If both arguments have side effects, we cannot optimize. */
4064 if (len
&& !TREE_SIDE_EFFECTS (len
))
4066 arg3_rtx
= expand_normal (len
);
4067 result
= expand_cmpstrn_or_cmpmem
4068 (cmpstrn_icode
, target
, arg1_rtx
, arg2_rtx
, TREE_TYPE (len
),
4069 arg3_rtx
, MIN (arg1_align
, arg2_align
));
4075 /* Return the value in the proper mode for this function. */
4076 machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
4077 if (GET_MODE (result
) == mode
)
4080 return convert_to_mode (mode
, result
, 0);
4081 convert_move (target
, result
, 0);
4085 /* Expand the library call ourselves using a stabilized argument
4086 list to avoid re-evaluating the function's arguments twice. */
4087 fndecl
= get_callee_fndecl (exp
);
4088 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 2, arg1
, arg2
);
4089 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4090 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4091 return expand_call (fn
, target
, target
== const0_rtx
);
4096 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4097 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4098 the result in TARGET, if convenient. */
4101 expand_builtin_strncmp (tree exp
, ATTRIBUTE_UNUSED rtx target
,
4102 ATTRIBUTE_UNUSED machine_mode mode
)
4104 location_t loc ATTRIBUTE_UNUSED
= EXPR_LOCATION (exp
);
4106 if (!validate_arglist (exp
,
4107 POINTER_TYPE
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
4110 /* If c_strlen can determine an expression for one of the string
4111 lengths, and it doesn't have side effects, then emit cmpstrnsi
4112 using length MIN(strlen(string)+1, arg3). */
4113 insn_code cmpstrn_icode
= direct_optab_handler (cmpstrn_optab
, SImode
);
4114 if (cmpstrn_icode
!= CODE_FOR_nothing
)
4116 tree len
, len1
, len2
;
4117 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
4120 tree arg1
= CALL_EXPR_ARG (exp
, 0);
4121 tree arg2
= CALL_EXPR_ARG (exp
, 1);
4122 tree arg3
= CALL_EXPR_ARG (exp
, 2);
4124 unsigned int arg1_align
= get_pointer_alignment (arg1
) / BITS_PER_UNIT
;
4125 unsigned int arg2_align
= get_pointer_alignment (arg2
) / BITS_PER_UNIT
;
4127 len1
= c_strlen (arg1
, 1);
4128 len2
= c_strlen (arg2
, 1);
4131 len1
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len1
);
4133 len2
= size_binop_loc (loc
, PLUS_EXPR
, ssize_int (1), len2
);
4135 /* If we don't have a constant length for the first, use the length
4136 of the second, if we know it. We don't require a constant for
4137 this case; some cost analysis could be done if both are available
4138 but neither is constant. For now, assume they're equally cheap,
4139 unless one has side effects. If both strings have constant lengths,
4146 else if (TREE_SIDE_EFFECTS (len1
))
4148 else if (TREE_SIDE_EFFECTS (len2
))
4150 else if (TREE_CODE (len1
) != INTEGER_CST
)
4152 else if (TREE_CODE (len2
) != INTEGER_CST
)
4154 else if (tree_int_cst_lt (len1
, len2
))
4159 /* If both arguments have side effects, we cannot optimize. */
4160 if (!len
|| TREE_SIDE_EFFECTS (len
))
4163 /* The actual new length parameter is MIN(len,arg3). */
4164 len
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (len
), len
,
4165 fold_convert_loc (loc
, TREE_TYPE (len
), arg3
));
4167 /* If we don't have POINTER_TYPE, call the function. */
4168 if (arg1_align
== 0 || arg2_align
== 0)
4171 /* Stabilize the arguments in case gen_cmpstrnsi fails. */
4172 arg1
= builtin_save_expr (arg1
);
4173 arg2
= builtin_save_expr (arg2
);
4174 len
= builtin_save_expr (len
);
4176 arg1_rtx
= get_memory_rtx (arg1
, len
);
4177 arg2_rtx
= get_memory_rtx (arg2
, len
);
4178 arg3_rtx
= expand_normal (len
);
4179 result
= expand_cmpstrn_or_cmpmem (cmpstrn_icode
, target
, arg1_rtx
,
4180 arg2_rtx
, TREE_TYPE (len
), arg3_rtx
,
4181 MIN (arg1_align
, arg2_align
));
4184 /* Return the value in the proper mode for this function. */
4185 mode
= TYPE_MODE (TREE_TYPE (exp
));
4186 if (GET_MODE (result
) == mode
)
4189 return convert_to_mode (mode
, result
, 0);
4190 convert_move (target
, result
, 0);
4194 /* Expand the library call ourselves using a stabilized argument
4195 list to avoid re-evaluating the function's arguments twice. */
4196 fndecl
= get_callee_fndecl (exp
);
4197 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fndecl
, 3,
4199 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
4200 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
4201 return expand_call (fn
, target
, target
== const0_rtx
);
4206 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4207 if that's convenient. */
4210 expand_builtin_saveregs (void)
4215 /* Don't do __builtin_saveregs more than once in a function.
4216 Save the result of the first call and reuse it. */
4217 if (saveregs_value
!= 0)
4218 return saveregs_value
;
4220 /* When this function is called, it means that registers must be
4221 saved on entry to this function. So we migrate the call to the
4222 first insn of this function. */
4226 /* Do whatever the machine needs done in this case. */
4227 val
= targetm
.calls
.expand_builtin_saveregs ();
4232 saveregs_value
= val
;
4234 /* Put the insns after the NOTE that starts the function. If this
4235 is inside a start_sequence, make the outer-level insn chain current, so
4236 the code is placed at the start of the function. */
4237 push_topmost_sequence ();
4238 emit_insn_after (seq
, entry_of_function ());
4239 pop_topmost_sequence ();
4244 /* Expand a call to __builtin_next_arg. */
4247 expand_builtin_next_arg (void)
4249 /* Checking arguments is already done in fold_builtin_next_arg
4250 that must be called before this function. */
4251 return expand_binop (ptr_mode
, add_optab
,
4252 crtl
->args
.internal_arg_pointer
,
4253 crtl
->args
.arg_offset_rtx
,
4254 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
4257 /* Make it easier for the backends by protecting the valist argument
4258 from multiple evaluations. */
4261 stabilize_va_list_loc (location_t loc
, tree valist
, int needs_lvalue
)
4263 tree vatype
= targetm
.canonical_va_list_type (TREE_TYPE (valist
));
4265 /* The current way of determining the type of valist is completely
4266 bogus. We should have the information on the va builtin instead. */
4268 vatype
= targetm
.fn_abi_va_list (cfun
->decl
);
4270 if (TREE_CODE (vatype
) == ARRAY_TYPE
)
4272 if (TREE_SIDE_EFFECTS (valist
))
4273 valist
= save_expr (valist
);
4275 /* For this case, the backends will be expecting a pointer to
4276 vatype, but it's possible we've actually been given an array
4277 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4279 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
4281 tree p1
= build_pointer_type (TREE_TYPE (vatype
));
4282 valist
= build_fold_addr_expr_with_type_loc (loc
, valist
, p1
);
4287 tree pt
= build_pointer_type (vatype
);
4291 if (! TREE_SIDE_EFFECTS (valist
))
4294 valist
= fold_build1_loc (loc
, ADDR_EXPR
, pt
, valist
);
4295 TREE_SIDE_EFFECTS (valist
) = 1;
4298 if (TREE_SIDE_EFFECTS (valist
))
4299 valist
= save_expr (valist
);
4300 valist
= fold_build2_loc (loc
, MEM_REF
,
4301 vatype
, valist
, build_int_cst (pt
, 0));
4307 /* The "standard" definition of va_list is void*. */
4310 std_build_builtin_va_list (void)
4312 return ptr_type_node
;
4315 /* The "standard" abi va_list is va_list_type_node. */
4318 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED
)
4320 return va_list_type_node
;
4323 /* The "standard" type of va_list is va_list_type_node. */
4326 std_canonical_va_list_type (tree type
)
4330 if (INDIRECT_REF_P (type
))
4331 type
= TREE_TYPE (type
);
4332 else if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (TREE_TYPE (type
)))
4333 type
= TREE_TYPE (type
);
4334 wtype
= va_list_type_node
;
4336 /* Treat structure va_list types. */
4337 if (TREE_CODE (wtype
) == RECORD_TYPE
&& POINTER_TYPE_P (htype
))
4338 htype
= TREE_TYPE (htype
);
4339 else if (TREE_CODE (wtype
) == ARRAY_TYPE
)
4341 /* If va_list is an array type, the argument may have decayed
4342 to a pointer type, e.g. by being passed to another function.
4343 In that case, unwrap both types so that we can compare the
4344 underlying records. */
4345 if (TREE_CODE (htype
) == ARRAY_TYPE
4346 || POINTER_TYPE_P (htype
))
4348 wtype
= TREE_TYPE (wtype
);
4349 htype
= TREE_TYPE (htype
);
4352 if (TYPE_MAIN_VARIANT (wtype
) == TYPE_MAIN_VARIANT (htype
))
4353 return va_list_type_node
;
4358 /* The "standard" implementation of va_start: just assign `nextarg' to
4362 std_expand_builtin_va_start (tree valist
, rtx nextarg
)
4364 rtx va_r
= expand_expr (valist
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
4365 convert_move (va_r
, nextarg
, 0);
4367 /* We do not have any valid bounds for the pointer, so
4368 just store zero bounds for it. */
4369 if (chkp_function_instrumented_p (current_function_decl
))
4370 chkp_expand_bounds_reset_for_mem (valist
,
4371 make_tree (TREE_TYPE (valist
),
4375 /* Expand EXP, a call to __builtin_va_start. */
4378 expand_builtin_va_start (tree exp
)
4382 location_t loc
= EXPR_LOCATION (exp
);
4384 if (call_expr_nargs (exp
) < 2)
4386 error_at (loc
, "too few arguments to function %<va_start%>");
4390 if (fold_builtin_next_arg (exp
, true))
4393 nextarg
= expand_builtin_next_arg ();
4394 valist
= stabilize_va_list_loc (loc
, CALL_EXPR_ARG (exp
, 0), 1);
4396 if (targetm
.expand_builtin_va_start
)
4397 targetm
.expand_builtin_va_start (valist
, nextarg
);
4399 std_expand_builtin_va_start (valist
, nextarg
);
4404 /* Expand EXP, a call to __builtin_va_end. */
4407 expand_builtin_va_end (tree exp
)
4409 tree valist
= CALL_EXPR_ARG (exp
, 0);
4411 /* Evaluate for side effects, if needed. I hate macros that don't
4413 if (TREE_SIDE_EFFECTS (valist
))
4414 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4419 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4420 builtin rather than just as an assignment in stdarg.h because of the
4421 nastiness of array-type va_list types. */
4424 expand_builtin_va_copy (tree exp
)
4427 location_t loc
= EXPR_LOCATION (exp
);
4429 dst
= CALL_EXPR_ARG (exp
, 0);
4430 src
= CALL_EXPR_ARG (exp
, 1);
4432 dst
= stabilize_va_list_loc (loc
, dst
, 1);
4433 src
= stabilize_va_list_loc (loc
, src
, 0);
4435 gcc_assert (cfun
!= NULL
&& cfun
->decl
!= NULL_TREE
);
4437 if (TREE_CODE (targetm
.fn_abi_va_list (cfun
->decl
)) != ARRAY_TYPE
)
4439 t
= build2 (MODIFY_EXPR
, targetm
.fn_abi_va_list (cfun
->decl
), dst
, src
);
4440 TREE_SIDE_EFFECTS (t
) = 1;
4441 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
4445 rtx dstb
, srcb
, size
;
4447 /* Evaluate to pointers. */
4448 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4449 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4450 size
= expand_expr (TYPE_SIZE_UNIT (targetm
.fn_abi_va_list (cfun
->decl
)),
4451 NULL_RTX
, VOIDmode
, EXPAND_NORMAL
);
4453 dstb
= convert_memory_address (Pmode
, dstb
);
4454 srcb
= convert_memory_address (Pmode
, srcb
);
4456 /* "Dereference" to BLKmode memories. */
4457 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
4458 set_mem_alias_set (dstb
, get_alias_set (TREE_TYPE (TREE_TYPE (dst
))));
4459 set_mem_align (dstb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4460 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
4461 set_mem_alias_set (srcb
, get_alias_set (TREE_TYPE (TREE_TYPE (src
))));
4462 set_mem_align (srcb
, TYPE_ALIGN (targetm
.fn_abi_va_list (cfun
->decl
)));
4465 emit_block_move (dstb
, srcb
, size
, BLOCK_OP_NORMAL
);
4471 /* Expand a call to one of the builtin functions __builtin_frame_address or
4472 __builtin_return_address. */
4475 expand_builtin_frame_address (tree fndecl
, tree exp
)
4477 /* The argument must be a nonnegative integer constant.
4478 It counts the number of frames to scan up the stack.
4479 The value is either the frame pointer value or the return
4480 address saved in that frame. */
4481 if (call_expr_nargs (exp
) == 0)
4482 /* Warning about missing arg was already issued. */
4484 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp
, 0)))
4486 error ("invalid argument to %qD", fndecl
);
4491 /* Number of frames to scan up the stack. */
4492 unsigned HOST_WIDE_INT count
= tree_to_uhwi (CALL_EXPR_ARG (exp
, 0));
4494 rtx tem
= expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
), count
);
4496 /* Some ports cannot access arbitrary stack frames. */
4499 warning (0, "unsupported argument to %qD", fndecl
);
4505 /* Warn since no effort is made to ensure that any frame
4506 beyond the current one exists or can be safely reached. */
4507 warning (OPT_Wframe_address
, "calling %qD with "
4508 "a nonzero argument is unsafe", fndecl
);
4511 /* For __builtin_frame_address, return what we've got. */
4512 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
4516 && ! CONSTANT_P (tem
))
4517 tem
= copy_addr_to_reg (tem
);
4522 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4523 failed and the caller should emit a normal call. CANNOT_ACCUMULATE
4524 is the same as for allocate_dynamic_stack_space. */
4527 expand_builtin_alloca (tree exp
, bool cannot_accumulate
)
4533 bool alloca_with_align
= (DECL_FUNCTION_CODE (get_callee_fndecl (exp
))
4534 == BUILT_IN_ALLOCA_WITH_ALIGN
);
4537 = (alloca_with_align
4538 ? validate_arglist (exp
, INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
4539 : validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
));
4544 /* Compute the argument. */
4545 op0
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4547 /* Compute the alignment. */
4548 align
= (alloca_with_align
4549 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp
, 1))
4550 : BIGGEST_ALIGNMENT
);
4552 /* Allocate the desired space. */
4553 result
= allocate_dynamic_stack_space (op0
, 0, align
, cannot_accumulate
);
4554 result
= convert_memory_address (ptr_mode
, result
);
4559 /* Expand a call to bswap builtin in EXP.
4560 Return NULL_RTX if a normal call should be emitted rather than expanding the
4561 function in-line. If convenient, the result should be placed in TARGET.
4562 SUBTARGET may be used as the target for computing one of EXP's operands. */
4565 expand_builtin_bswap (machine_mode target_mode
, tree exp
, rtx target
,
4571 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4574 arg
= CALL_EXPR_ARG (exp
, 0);
4575 op0
= expand_expr (arg
,
4576 subtarget
&& GET_MODE (subtarget
) == target_mode
4577 ? subtarget
: NULL_RTX
,
4578 target_mode
, EXPAND_NORMAL
);
4579 if (GET_MODE (op0
) != target_mode
)
4580 op0
= convert_to_mode (target_mode
, op0
, 1);
4582 target
= expand_unop (target_mode
, bswap_optab
, op0
, target
, 1);
4584 gcc_assert (target
);
4586 return convert_to_mode (target_mode
, target
, 1);
4589 /* Expand a call to a unary builtin in EXP.
4590 Return NULL_RTX if a normal call should be emitted rather than expanding the
4591 function in-line. If convenient, the result should be placed in TARGET.
4592 SUBTARGET may be used as the target for computing one of EXP's operands. */
4595 expand_builtin_unop (machine_mode target_mode
, tree exp
, rtx target
,
4596 rtx subtarget
, optab op_optab
)
4600 if (!validate_arglist (exp
, INTEGER_TYPE
, VOID_TYPE
))
4603 /* Compute the argument. */
4604 op0
= expand_expr (CALL_EXPR_ARG (exp
, 0),
4606 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0)))
4607 == GET_MODE (subtarget
))) ? subtarget
: NULL_RTX
,
4608 VOIDmode
, EXPAND_NORMAL
);
4609 /* Compute op, into TARGET if possible.
4610 Set TARGET to wherever the result comes back. */
4611 target
= expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp
, 0))),
4612 op_optab
, op0
, target
, op_optab
!= clrsb_optab
);
4613 gcc_assert (target
);
4615 return convert_to_mode (target_mode
, target
, 0);
4618 /* Expand a call to __builtin_expect. We just return our argument
4619 as the builtin_expect semantic should've been already executed by
4620 tree branch prediction pass. */
4623 expand_builtin_expect (tree exp
, rtx target
)
4627 if (call_expr_nargs (exp
) < 2)
4629 arg
= CALL_EXPR_ARG (exp
, 0);
4631 target
= expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4632 /* When guessing was done, the hints should be already stripped away. */
4633 gcc_assert (!flag_guess_branch_prob
4634 || optimize
== 0 || seen_error ());
4638 /* Expand a call to __builtin_assume_aligned. We just return our first
4639 argument as the builtin_assume_aligned semantic should've been already
4643 expand_builtin_assume_aligned (tree exp
, rtx target
)
4645 if (call_expr_nargs (exp
) < 2)
4647 target
= expand_expr (CALL_EXPR_ARG (exp
, 0), target
, VOIDmode
,
4649 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 1))
4650 && (call_expr_nargs (exp
) < 3
4651 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp
, 2))));
4656 expand_builtin_trap (void)
4658 if (targetm
.have_trap ())
4660 rtx_insn
*insn
= emit_insn (targetm
.gen_trap ());
4661 /* For trap insns when not accumulating outgoing args force
4662 REG_ARGS_SIZE note to prevent crossjumping of calls with
4663 different args sizes. */
4664 if (!ACCUMULATE_OUTGOING_ARGS
)
4665 add_reg_note (insn
, REG_ARGS_SIZE
, GEN_INT (stack_pointer_delta
));
4668 emit_library_call (abort_libfunc
, LCT_NORETURN
, VOIDmode
, 0);
4672 /* Expand a call to __builtin_unreachable. We do nothing except emit
4673 a barrier saying that control flow will not pass here.
4675 It is the responsibility of the program being compiled to ensure
4676 that control flow does never reach __builtin_unreachable. */
4678 expand_builtin_unreachable (void)
4683 /* Expand EXP, a call to fabs, fabsf or fabsl.
4684 Return NULL_RTX if a normal call should be emitted rather than expanding
4685 the function inline. If convenient, the result should be placed
4686 in TARGET. SUBTARGET may be used as the target for computing
4690 expand_builtin_fabs (tree exp
, rtx target
, rtx subtarget
)
4696 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4699 arg
= CALL_EXPR_ARG (exp
, 0);
4700 CALL_EXPR_ARG (exp
, 0) = arg
= builtin_save_expr (arg
);
4701 mode
= TYPE_MODE (TREE_TYPE (arg
));
4702 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4703 return expand_abs (mode
, op0
, target
, 0, safe_from_p (target
, arg
, 1));
4706 /* Expand EXP, a call to copysign, copysignf, or copysignl.
4707 Return NULL is a normal call should be emitted rather than expanding the
4708 function inline. If convenient, the result should be placed in TARGET.
4709 SUBTARGET may be used as the target for computing the operand. */
4712 expand_builtin_copysign (tree exp
, rtx target
, rtx subtarget
)
4717 if (!validate_arglist (exp
, REAL_TYPE
, REAL_TYPE
, VOID_TYPE
))
4720 arg
= CALL_EXPR_ARG (exp
, 0);
4721 op0
= expand_expr (arg
, subtarget
, VOIDmode
, EXPAND_NORMAL
);
4723 arg
= CALL_EXPR_ARG (exp
, 1);
4724 op1
= expand_normal (arg
);
4726 return expand_copysign (op0
, op1
, target
);
4729 /* Expand a call to __builtin___clear_cache. */
4732 expand_builtin___clear_cache (tree exp
)
4734 if (!targetm
.code_for_clear_cache
)
4736 #ifdef CLEAR_INSN_CACHE
4737 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4738 does something. Just do the default expansion to a call to
4742 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
4743 does nothing. There is no need to call it. Do nothing. */
4745 #endif /* CLEAR_INSN_CACHE */
4748 /* We have a "clear_cache" insn, and it will handle everything. */
4750 rtx begin_rtx
, end_rtx
;
4752 /* We must not expand to a library call. If we did, any
4753 fallback library function in libgcc that might contain a call to
4754 __builtin___clear_cache() would recurse infinitely. */
4755 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
4757 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
4761 if (targetm
.have_clear_cache ())
4763 struct expand_operand ops
[2];
4765 begin
= CALL_EXPR_ARG (exp
, 0);
4766 begin_rtx
= expand_expr (begin
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4768 end
= CALL_EXPR_ARG (exp
, 1);
4769 end_rtx
= expand_expr (end
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
4771 create_address_operand (&ops
[0], begin_rtx
);
4772 create_address_operand (&ops
[1], end_rtx
);
4773 if (maybe_expand_insn (targetm
.code_for_clear_cache
, 2, ops
))
4779 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
4782 round_trampoline_addr (rtx tramp
)
4784 rtx temp
, addend
, mask
;
4786 /* If we don't need too much alignment, we'll have been guaranteed
4787 proper alignment by get_trampoline_type. */
4788 if (TRAMPOLINE_ALIGNMENT
<= STACK_BOUNDARY
)
4791 /* Round address up to desired boundary. */
4792 temp
= gen_reg_rtx (Pmode
);
4793 addend
= gen_int_mode (TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
- 1, Pmode
);
4794 mask
= gen_int_mode (-TRAMPOLINE_ALIGNMENT
/ BITS_PER_UNIT
, Pmode
);
4796 temp
= expand_simple_binop (Pmode
, PLUS
, tramp
, addend
,
4797 temp
, 0, OPTAB_LIB_WIDEN
);
4798 tramp
= expand_simple_binop (Pmode
, AND
, temp
, mask
,
4799 temp
, 0, OPTAB_LIB_WIDEN
);
4805 expand_builtin_init_trampoline (tree exp
, bool onstack
)
4807 tree t_tramp
, t_func
, t_chain
;
4808 rtx m_tramp
, r_tramp
, r_chain
, tmp
;
4810 if (!validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
,
4811 POINTER_TYPE
, VOID_TYPE
))
4814 t_tramp
= CALL_EXPR_ARG (exp
, 0);
4815 t_func
= CALL_EXPR_ARG (exp
, 1);
4816 t_chain
= CALL_EXPR_ARG (exp
, 2);
4818 r_tramp
= expand_normal (t_tramp
);
4819 m_tramp
= gen_rtx_MEM (BLKmode
, r_tramp
);
4820 MEM_NOTRAP_P (m_tramp
) = 1;
4822 /* If ONSTACK, the TRAMP argument should be the address of a field
4823 within the local function's FRAME decl. Either way, let's see if
4824 we can fill in the MEM_ATTRs for this memory. */
4825 if (TREE_CODE (t_tramp
) == ADDR_EXPR
)
4826 set_mem_attributes (m_tramp
, TREE_OPERAND (t_tramp
, 0), true);
4828 /* Creator of a heap trampoline is responsible for making sure the
4829 address is aligned to at least STACK_BOUNDARY. Normally malloc
4830 will ensure this anyhow. */
4831 tmp
= round_trampoline_addr (r_tramp
);
4834 m_tramp
= change_address (m_tramp
, BLKmode
, tmp
);
4835 set_mem_align (m_tramp
, TRAMPOLINE_ALIGNMENT
);
4836 set_mem_size (m_tramp
, TRAMPOLINE_SIZE
);
4839 /* The FUNC argument should be the address of the nested function.
4840 Extract the actual function decl to pass to the hook. */
4841 gcc_assert (TREE_CODE (t_func
) == ADDR_EXPR
);
4842 t_func
= TREE_OPERAND (t_func
, 0);
4843 gcc_assert (TREE_CODE (t_func
) == FUNCTION_DECL
);
4845 r_chain
= expand_normal (t_chain
);
4847 /* Generate insns to initialize the trampoline. */
4848 targetm
.calls
.trampoline_init (m_tramp
, t_func
, r_chain
);
4852 trampolines_created
= 1;
4854 warning_at (DECL_SOURCE_LOCATION (t_func
), OPT_Wtrampolines
,
4855 "trampoline generated for nested function %qD", t_func
);
4862 expand_builtin_adjust_trampoline (tree exp
)
4866 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
4869 tramp
= expand_normal (CALL_EXPR_ARG (exp
, 0));
4870 tramp
= round_trampoline_addr (tramp
);
4871 if (targetm
.calls
.trampoline_adjust_address
)
4872 tramp
= targetm
.calls
.trampoline_adjust_address (tramp
);
4877 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
4878 function. The function first checks whether the back end provides
4879 an insn to implement signbit for the respective mode. If not, it
4880 checks whether the floating point format of the value is such that
4881 the sign bit can be extracted. If that is not the case, error out.
4882 EXP is the expression that is a call to the builtin function; if
4883 convenient, the result should be placed in TARGET. */
4885 expand_builtin_signbit (tree exp
, rtx target
)
4887 const struct real_format
*fmt
;
4888 machine_mode fmode
, imode
, rmode
;
4891 enum insn_code icode
;
4893 location_t loc
= EXPR_LOCATION (exp
);
4895 if (!validate_arglist (exp
, REAL_TYPE
, VOID_TYPE
))
4898 arg
= CALL_EXPR_ARG (exp
, 0);
4899 fmode
= TYPE_MODE (TREE_TYPE (arg
));
4900 rmode
= TYPE_MODE (TREE_TYPE (exp
));
4901 fmt
= REAL_MODE_FORMAT (fmode
);
4903 arg
= builtin_save_expr (arg
);
4905 /* Expand the argument yielding a RTX expression. */
4906 temp
= expand_normal (arg
);
4908 /* Check if the back end provides an insn that handles signbit for the
4910 icode
= optab_handler (signbit_optab
, fmode
);
4911 if (icode
!= CODE_FOR_nothing
)
4913 rtx_insn
*last
= get_last_insn ();
4914 target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
4915 if (maybe_emit_unop_insn (icode
, target
, temp
, UNKNOWN
))
4917 delete_insns_since (last
);
4920 /* For floating point formats without a sign bit, implement signbit
4922 bitpos
= fmt
->signbit_ro
;
4925 /* But we can't do this if the format supports signed zero. */
4926 gcc_assert (!fmt
->has_signed_zero
|| !HONOR_SIGNED_ZEROS (fmode
));
4928 arg
= fold_build2_loc (loc
, LT_EXPR
, TREE_TYPE (exp
), arg
,
4929 build_real (TREE_TYPE (arg
), dconst0
));
4930 return expand_expr (arg
, target
, VOIDmode
, EXPAND_NORMAL
);
4933 if (GET_MODE_SIZE (fmode
) <= UNITS_PER_WORD
)
4935 imode
= int_mode_for_mode (fmode
);
4936 gcc_assert (imode
!= BLKmode
);
4937 temp
= gen_lowpart (imode
, temp
);
4942 /* Handle targets with different FP word orders. */
4943 if (FLOAT_WORDS_BIG_ENDIAN
)
4944 word
= (GET_MODE_BITSIZE (fmode
) - bitpos
) / BITS_PER_WORD
;
4946 word
= bitpos
/ BITS_PER_WORD
;
4947 temp
= operand_subword_force (temp
, word
, fmode
);
4948 bitpos
= bitpos
% BITS_PER_WORD
;
4951 /* Force the intermediate word_mode (or narrower) result into a
4952 register. This avoids attempting to create paradoxical SUBREGs
4953 of floating point modes below. */
4954 temp
= force_reg (imode
, temp
);
4956 /* If the bitpos is within the "result mode" lowpart, the operation
4957 can be implement with a single bitwise AND. Otherwise, we need
4958 a right shift and an AND. */
4960 if (bitpos
< GET_MODE_BITSIZE (rmode
))
4962 wide_int mask
= wi::set_bit_in_zero (bitpos
, GET_MODE_PRECISION (rmode
));
4964 if (GET_MODE_SIZE (imode
) > GET_MODE_SIZE (rmode
))
4965 temp
= gen_lowpart (rmode
, temp
);
4966 temp
= expand_binop (rmode
, and_optab
, temp
,
4967 immed_wide_int_const (mask
, rmode
),
4968 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4972 /* Perform a logical right shift to place the signbit in the least
4973 significant bit, then truncate the result to the desired mode
4974 and mask just this bit. */
4975 temp
= expand_shift (RSHIFT_EXPR
, imode
, temp
, bitpos
, NULL_RTX
, 1);
4976 temp
= gen_lowpart (rmode
, temp
);
4977 temp
= expand_binop (rmode
, and_optab
, temp
, const1_rtx
,
4978 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
4984 /* Expand fork or exec calls. TARGET is the desired target of the
4985 call. EXP is the call. FN is the
4986 identificator of the actual function. IGNORE is nonzero if the
4987 value is to be ignored. */
4990 expand_builtin_fork_or_exec (tree fn
, tree exp
, rtx target
, int ignore
)
4995 /* If we are not profiling, just call the function. */
4996 if (!profile_arc_flag
)
4999 /* Otherwise call the wrapper. This should be equivalent for the rest of
5000 compiler, so the code does not diverge, and the wrapper may run the
5001 code necessary for keeping the profiling sane. */
5003 switch (DECL_FUNCTION_CODE (fn
))
5006 id
= get_identifier ("__gcov_fork");
5009 case BUILT_IN_EXECL
:
5010 id
= get_identifier ("__gcov_execl");
5013 case BUILT_IN_EXECV
:
5014 id
= get_identifier ("__gcov_execv");
5017 case BUILT_IN_EXECLP
:
5018 id
= get_identifier ("__gcov_execlp");
5021 case BUILT_IN_EXECLE
:
5022 id
= get_identifier ("__gcov_execle");
5025 case BUILT_IN_EXECVP
:
5026 id
= get_identifier ("__gcov_execvp");
5029 case BUILT_IN_EXECVE
:
5030 id
= get_identifier ("__gcov_execve");
5037 decl
= build_decl (DECL_SOURCE_LOCATION (fn
),
5038 FUNCTION_DECL
, id
, TREE_TYPE (fn
));
5039 DECL_EXTERNAL (decl
) = 1;
5040 TREE_PUBLIC (decl
) = 1;
5041 DECL_ARTIFICIAL (decl
) = 1;
5042 TREE_NOTHROW (decl
) = 1;
5043 DECL_VISIBILITY (decl
) = VISIBILITY_DEFAULT
;
5044 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
5045 call
= rewrite_call_expr (EXPR_LOCATION (exp
), exp
, 0, decl
, 0);
5046 return expand_call (call
, target
, ignore
);
5051 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5052 the pointer in these functions is void*, the tree optimizers may remove
5053 casts. The mode computed in expand_builtin isn't reliable either, due
5054 to __sync_bool_compare_and_swap.
5056 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5057 group of builtins. This gives us log2 of the mode size. */
5059 static inline machine_mode
5060 get_builtin_sync_mode (int fcode_diff
)
5062 /* The size is not negotiable, so ask not to get BLKmode in return
5063 if the target indicates that a smaller size would be better. */
5064 return mode_for_size (BITS_PER_UNIT
<< fcode_diff
, MODE_INT
, 0);
5067 /* Expand the memory expression LOC and return the appropriate memory operand
5068 for the builtin_sync operations. */
5071 get_builtin_sync_mem (tree loc
, machine_mode mode
)
5075 addr
= expand_expr (loc
, NULL_RTX
, ptr_mode
, EXPAND_SUM
);
5076 addr
= convert_memory_address (Pmode
, addr
);
5078 /* Note that we explicitly do not want any alias information for this
5079 memory, so that we kill all other live memories. Otherwise we don't
5080 satisfy the full barrier semantics of the intrinsic. */
5081 mem
= validize_mem (gen_rtx_MEM (mode
, addr
));
5083 /* The alignment needs to be at least according to that of the mode. */
5084 set_mem_align (mem
, MAX (GET_MODE_ALIGNMENT (mode
),
5085 get_pointer_alignment (loc
)));
5086 set_mem_alias_set (mem
, ALIAS_SET_MEMORY_BARRIER
);
5087 MEM_VOLATILE_P (mem
) = 1;
5092 /* Make sure an argument is in the right mode.
5093 EXP is the tree argument.
5094 MODE is the mode it should be in. */
5097 expand_expr_force_mode (tree exp
, machine_mode mode
)
5100 machine_mode old_mode
;
5102 val
= expand_expr (exp
, NULL_RTX
, mode
, EXPAND_NORMAL
);
5103 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5104 of CONST_INTs, where we know the old_mode only from the call argument. */
5106 old_mode
= GET_MODE (val
);
5107 if (old_mode
== VOIDmode
)
5108 old_mode
= TYPE_MODE (TREE_TYPE (exp
));
5109 val
= convert_modes (mode
, old_mode
, val
, 1);
5114 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5115 EXP is the CALL_EXPR. CODE is the rtx code
5116 that corresponds to the arithmetic or logical operation from the name;
5117 an exception here is that NOT actually means NAND. TARGET is an optional
5118 place for us to store the results; AFTER is true if this is the
5119 fetch_and_xxx form. */
5122 expand_builtin_sync_operation (machine_mode mode
, tree exp
,
5123 enum rtx_code code
, bool after
,
5127 location_t loc
= EXPR_LOCATION (exp
);
5129 if (code
== NOT
&& warn_sync_nand
)
5131 tree fndecl
= get_callee_fndecl (exp
);
5132 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5134 static bool warned_f_a_n
, warned_n_a_f
;
5138 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
5139 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
5140 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
5141 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
5142 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
5146 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N
);
5147 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5148 warned_f_a_n
= true;
5151 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
5152 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
5153 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
5154 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
5155 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
5159 fndecl
= builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N
);
5160 inform (loc
, "%qD changed semantics in GCC 4.4", fndecl
);
5161 warned_n_a_f
= true;
5169 /* Expand the operands. */
5170 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5171 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5173 return expand_atomic_fetch_op (target
, mem
, val
, code
, MEMMODEL_SYNC_SEQ_CST
,
5177 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5178 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5179 true if this is the boolean form. TARGET is a place for us to store the
5180 results; this is NOT optional if IS_BOOL is true. */
5183 expand_builtin_compare_and_swap (machine_mode mode
, tree exp
,
5184 bool is_bool
, rtx target
)
5186 rtx old_val
, new_val
, mem
;
5189 /* Expand the operands. */
5190 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5191 old_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5192 new_val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5194 pbool
= poval
= NULL
;
5195 if (target
!= const0_rtx
)
5202 if (!expand_atomic_compare_and_swap (pbool
, poval
, mem
, old_val
, new_val
,
5203 false, MEMMODEL_SYNC_SEQ_CST
,
5204 MEMMODEL_SYNC_SEQ_CST
))
5210 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5211 general form is actually an atomic exchange, and some targets only
5212 support a reduced form with the second argument being a constant 1.
5213 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5217 expand_builtin_sync_lock_test_and_set (machine_mode mode
, tree exp
,
5222 /* Expand the operands. */
5223 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5224 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5226 return expand_sync_lock_test_and_set (target
, mem
, val
);
5229 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5232 expand_builtin_sync_lock_release (machine_mode mode
, tree exp
)
5236 /* Expand the operands. */
5237 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5239 expand_atomic_store (mem
, const0_rtx
, MEMMODEL_SYNC_RELEASE
, true);
5242 /* Given an integer representing an ``enum memmodel'', verify its
5243 correctness and return the memory model enum. */
5245 static enum memmodel
5246 get_memmodel (tree exp
)
5249 unsigned HOST_WIDE_INT val
;
5251 /* If the parameter is not a constant, it's a run time value so we'll just
5252 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5253 if (TREE_CODE (exp
) != INTEGER_CST
)
5254 return MEMMODEL_SEQ_CST
;
5256 op
= expand_normal (exp
);
5259 if (targetm
.memmodel_check
)
5260 val
= targetm
.memmodel_check (val
);
5261 else if (val
& ~MEMMODEL_MASK
)
5263 warning (OPT_Winvalid_memory_model
,
5264 "Unknown architecture specifier in memory model to builtin.");
5265 return MEMMODEL_SEQ_CST
;
5268 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5269 if (memmodel_base (val
) >= MEMMODEL_LAST
)
5271 warning (OPT_Winvalid_memory_model
,
5272 "invalid memory model argument to builtin");
5273 return MEMMODEL_SEQ_CST
;
5276 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5277 be conservative and promote consume to acquire. */
5278 if (val
== MEMMODEL_CONSUME
)
5279 val
= MEMMODEL_ACQUIRE
;
5281 return (enum memmodel
) val
;
5284 /* Expand the __atomic_exchange intrinsic:
5285 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5286 EXP is the CALL_EXPR.
5287 TARGET is an optional place for us to store the results. */
5290 expand_builtin_atomic_exchange (machine_mode mode
, tree exp
, rtx target
)
5293 enum memmodel model
;
5295 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5297 if (!flag_inline_atomics
)
5300 /* Expand the operands. */
5301 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5302 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5304 return expand_atomic_exchange (target
, mem
, val
, model
);
5307 /* Expand the __atomic_compare_exchange intrinsic:
5308 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5309 TYPE desired, BOOL weak,
5310 enum memmodel success,
5311 enum memmodel failure)
5312 EXP is the CALL_EXPR.
5313 TARGET is an optional place for us to store the results. */
5316 expand_builtin_atomic_compare_exchange (machine_mode mode
, tree exp
,
5319 rtx expect
, desired
, mem
, oldval
;
5320 rtx_code_label
*label
;
5321 enum memmodel success
, failure
;
5325 success
= get_memmodel (CALL_EXPR_ARG (exp
, 4));
5326 failure
= get_memmodel (CALL_EXPR_ARG (exp
, 5));
5328 if (failure
> success
)
5330 warning (OPT_Winvalid_memory_model
,
5331 "failure memory model cannot be stronger than success memory "
5332 "model for %<__atomic_compare_exchange%>");
5333 success
= MEMMODEL_SEQ_CST
;
5336 if (is_mm_release (failure
) || is_mm_acq_rel (failure
))
5338 warning (OPT_Winvalid_memory_model
,
5339 "invalid failure memory model for "
5340 "%<__atomic_compare_exchange%>");
5341 failure
= MEMMODEL_SEQ_CST
;
5342 success
= MEMMODEL_SEQ_CST
;
5346 if (!flag_inline_atomics
)
5349 /* Expand the operands. */
5350 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5352 expect
= expand_normal (CALL_EXPR_ARG (exp
, 1));
5353 expect
= convert_memory_address (Pmode
, expect
);
5354 expect
= gen_rtx_MEM (mode
, expect
);
5355 desired
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 2), mode
);
5357 weak
= CALL_EXPR_ARG (exp
, 3);
5359 if (tree_fits_shwi_p (weak
) && tree_to_shwi (weak
) != 0)
5362 if (target
== const0_rtx
)
5365 /* Lest the rtl backend create a race condition with an imporoper store
5366 to memory, always create a new pseudo for OLDVAL. */
5369 if (!expand_atomic_compare_and_swap (&target
, &oldval
, mem
, expect
, desired
,
5370 is_weak
, success
, failure
))
5373 /* Conditionally store back to EXPECT, lest we create a race condition
5374 with an improper store to memory. */
5375 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5376 the normal case where EXPECT is totally private, i.e. a register. At
5377 which point the store can be unconditional. */
5378 label
= gen_label_rtx ();
5379 emit_cmp_and_jump_insns (target
, const0_rtx
, NE
, NULL
,
5380 GET_MODE (target
), 1, label
);
5381 emit_move_insn (expect
, oldval
);
5387 /* Expand the __atomic_load intrinsic:
5388 TYPE __atomic_load (TYPE *object, enum memmodel)
5389 EXP is the CALL_EXPR.
5390 TARGET is an optional place for us to store the results. */
5393 expand_builtin_atomic_load (machine_mode mode
, tree exp
, rtx target
)
5396 enum memmodel model
;
5398 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5399 if (is_mm_release (model
) || is_mm_acq_rel (model
))
5401 warning (OPT_Winvalid_memory_model
,
5402 "invalid memory model for %<__atomic_load%>");
5403 model
= MEMMODEL_SEQ_CST
;
5406 if (!flag_inline_atomics
)
5409 /* Expand the operand. */
5410 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5412 return expand_atomic_load (target
, mem
, model
);
5416 /* Expand the __atomic_store intrinsic:
5417 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
5418 EXP is the CALL_EXPR.
5419 TARGET is an optional place for us to store the results. */
5422 expand_builtin_atomic_store (machine_mode mode
, tree exp
)
5425 enum memmodel model
;
5427 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5428 if (!(is_mm_relaxed (model
) || is_mm_seq_cst (model
)
5429 || is_mm_release (model
)))
5431 warning (OPT_Winvalid_memory_model
,
5432 "invalid memory model for %<__atomic_store%>");
5433 model
= MEMMODEL_SEQ_CST
;
5436 if (!flag_inline_atomics
)
5439 /* Expand the operands. */
5440 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5441 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5443 return expand_atomic_store (mem
, val
, model
, false);
5446 /* Expand the __atomic_fetch_XXX intrinsic:
5447 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
5448 EXP is the CALL_EXPR.
5449 TARGET is an optional place for us to store the results.
5450 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
5451 FETCH_AFTER is true if returning the result of the operation.
5452 FETCH_AFTER is false if returning the value before the operation.
5453 IGNORE is true if the result is not used.
5454 EXT_CALL is the correct builtin for an external call if this cannot be
5455 resolved to an instruction sequence. */
5458 expand_builtin_atomic_fetch_op (machine_mode mode
, tree exp
, rtx target
,
5459 enum rtx_code code
, bool fetch_after
,
5460 bool ignore
, enum built_in_function ext_call
)
5463 enum memmodel model
;
5467 model
= get_memmodel (CALL_EXPR_ARG (exp
, 2));
5469 /* Expand the operands. */
5470 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5471 val
= expand_expr_force_mode (CALL_EXPR_ARG (exp
, 1), mode
);
5473 /* Only try generating instructions if inlining is turned on. */
5474 if (flag_inline_atomics
)
5476 ret
= expand_atomic_fetch_op (target
, mem
, val
, code
, model
, fetch_after
);
5481 /* Return if a different routine isn't needed for the library call. */
5482 if (ext_call
== BUILT_IN_NONE
)
5485 /* Change the call to the specified function. */
5486 fndecl
= get_callee_fndecl (exp
);
5487 addr
= CALL_EXPR_FN (exp
);
5490 gcc_assert (TREE_OPERAND (addr
, 0) == fndecl
);
5491 TREE_OPERAND (addr
, 0) = builtin_decl_explicit (ext_call
);
5493 /* Expand the call here so we can emit trailing code. */
5494 ret
= expand_call (exp
, target
, ignore
);
5496 /* Replace the original function just in case it matters. */
5497 TREE_OPERAND (addr
, 0) = fndecl
;
5499 /* Then issue the arithmetic correction to return the right result. */
5504 ret
= expand_simple_binop (mode
, AND
, ret
, val
, NULL_RTX
, true,
5506 ret
= expand_simple_unop (mode
, NOT
, ret
, target
, true);
5509 ret
= expand_simple_binop (mode
, code
, ret
, val
, target
, true,
5515 /* Expand an atomic clear operation.
5516 void _atomic_clear (BOOL *obj, enum memmodel)
5517 EXP is the call expression. */
5520 expand_builtin_atomic_clear (tree exp
)
5524 enum memmodel model
;
5526 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5527 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5528 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5530 if (is_mm_consume (model
) || is_mm_acquire (model
) || is_mm_acq_rel (model
))
5532 warning (OPT_Winvalid_memory_model
,
5533 "invalid memory model for %<__atomic_store%>");
5534 model
= MEMMODEL_SEQ_CST
;
5537 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
5538 Failing that, a store is issued by __atomic_store. The only way this can
5539 fail is if the bool type is larger than a word size. Unlikely, but
5540 handle it anyway for completeness. Assume a single threaded model since
5541 there is no atomic support in this case, and no barriers are required. */
5542 ret
= expand_atomic_store (mem
, const0_rtx
, model
, true);
5544 emit_move_insn (mem
, const0_rtx
);
5548 /* Expand an atomic test_and_set operation.
5549 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
5550 EXP is the call expression. */
5553 expand_builtin_atomic_test_and_set (tree exp
, rtx target
)
5556 enum memmodel model
;
5559 mode
= mode_for_size (BOOL_TYPE_SIZE
, MODE_INT
, 0);
5560 mem
= get_builtin_sync_mem (CALL_EXPR_ARG (exp
, 0), mode
);
5561 model
= get_memmodel (CALL_EXPR_ARG (exp
, 1));
5563 return expand_atomic_test_and_set (target
, mem
, model
);
5567 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
5568 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
5571 fold_builtin_atomic_always_lock_free (tree arg0
, tree arg1
)
5575 unsigned int mode_align
, type_align
;
5577 if (TREE_CODE (arg0
) != INTEGER_CST
)
5580 size
= INTVAL (expand_normal (arg0
)) * BITS_PER_UNIT
;
5581 mode
= mode_for_size (size
, MODE_INT
, 0);
5582 mode_align
= GET_MODE_ALIGNMENT (mode
);
5584 if (TREE_CODE (arg1
) == INTEGER_CST
)
5586 unsigned HOST_WIDE_INT val
= UINTVAL (expand_normal (arg1
));
5588 /* Either this argument is null, or it's a fake pointer encoding
5589 the alignment of the object. */
5591 val
*= BITS_PER_UNIT
;
5593 if (val
== 0 || mode_align
< val
)
5594 type_align
= mode_align
;
5600 tree ttype
= TREE_TYPE (arg1
);
5602 /* This function is usually invoked and folded immediately by the front
5603 end before anything else has a chance to look at it. The pointer
5604 parameter at this point is usually cast to a void *, so check for that
5605 and look past the cast. */
5606 if (CONVERT_EXPR_P (arg1
) && POINTER_TYPE_P (ttype
)
5607 && VOID_TYPE_P (TREE_TYPE (ttype
)))
5608 arg1
= TREE_OPERAND (arg1
, 0);
5610 ttype
= TREE_TYPE (arg1
);
5611 gcc_assert (POINTER_TYPE_P (ttype
));
5613 /* Get the underlying type of the object. */
5614 ttype
= TREE_TYPE (ttype
);
5615 type_align
= TYPE_ALIGN (ttype
);
5618 /* If the object has smaller alignment, the lock free routines cannot
5620 if (type_align
< mode_align
)
5621 return boolean_false_node
;
5623 /* Check if a compare_and_swap pattern exists for the mode which represents
5624 the required size. The pattern is not allowed to fail, so the existence
5625 of the pattern indicates support is present. */
5626 if (can_compare_and_swap_p (mode
, true))
5627 return boolean_true_node
;
5629 return boolean_false_node
;
5632 /* Return true if the parameters to call EXP represent an object which will
5633 always generate lock free instructions. The first argument represents the
5634 size of the object, and the second parameter is a pointer to the object
5635 itself. If NULL is passed for the object, then the result is based on
5636 typical alignment for an object of the specified size. Otherwise return
5640 expand_builtin_atomic_always_lock_free (tree exp
)
5643 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5644 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5646 if (TREE_CODE (arg0
) != INTEGER_CST
)
5648 error ("non-constant argument 1 to __atomic_always_lock_free");
5652 size
= fold_builtin_atomic_always_lock_free (arg0
, arg1
);
5653 if (size
== boolean_true_node
)
5658 /* Return a one or zero if it can be determined that object ARG1 of size ARG
5659 is lock free on this architecture. */
5662 fold_builtin_atomic_is_lock_free (tree arg0
, tree arg1
)
5664 if (!flag_inline_atomics
)
5667 /* If it isn't always lock free, don't generate a result. */
5668 if (fold_builtin_atomic_always_lock_free (arg0
, arg1
) == boolean_true_node
)
5669 return boolean_true_node
;
5674 /* Return true if the parameters to call EXP represent an object which will
5675 always generate lock free instructions. The first argument represents the
5676 size of the object, and the second parameter is a pointer to the object
5677 itself. If NULL is passed for the object, then the result is based on
5678 typical alignment for an object of the specified size. Otherwise return
5682 expand_builtin_atomic_is_lock_free (tree exp
)
5685 tree arg0
= CALL_EXPR_ARG (exp
, 0);
5686 tree arg1
= CALL_EXPR_ARG (exp
, 1);
5688 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
5690 error ("non-integer argument 1 to __atomic_is_lock_free");
5694 if (!flag_inline_atomics
)
5697 /* If the value is known at compile time, return the RTX for it. */
5698 size
= fold_builtin_atomic_is_lock_free (arg0
, arg1
);
5699 if (size
== boolean_true_node
)
5705 /* Expand the __atomic_thread_fence intrinsic:
5706 void __atomic_thread_fence (enum memmodel)
5707 EXP is the CALL_EXPR. */
5710 expand_builtin_atomic_thread_fence (tree exp
)
5712 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5713 expand_mem_thread_fence (model
);
5716 /* Expand the __atomic_signal_fence intrinsic:
5717 void __atomic_signal_fence (enum memmodel)
5718 EXP is the CALL_EXPR. */
5721 expand_builtin_atomic_signal_fence (tree exp
)
5723 enum memmodel model
= get_memmodel (CALL_EXPR_ARG (exp
, 0));
5724 expand_mem_signal_fence (model
);
5727 /* Expand the __sync_synchronize intrinsic. */
5730 expand_builtin_sync_synchronize (void)
5732 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST
);
5736 expand_builtin_thread_pointer (tree exp
, rtx target
)
5738 enum insn_code icode
;
5739 if (!validate_arglist (exp
, VOID_TYPE
))
5741 icode
= direct_optab_handler (get_thread_pointer_optab
, Pmode
);
5742 if (icode
!= CODE_FOR_nothing
)
5744 struct expand_operand op
;
5745 /* If the target is not sutitable then create a new target. */
5746 if (target
== NULL_RTX
5748 || GET_MODE (target
) != Pmode
)
5749 target
= gen_reg_rtx (Pmode
);
5750 create_output_operand (&op
, target
, Pmode
);
5751 expand_insn (icode
, 1, &op
);
5754 error ("__builtin_thread_pointer is not supported on this target");
5759 expand_builtin_set_thread_pointer (tree exp
)
5761 enum insn_code icode
;
5762 if (!validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
5764 icode
= direct_optab_handler (set_thread_pointer_optab
, Pmode
);
5765 if (icode
!= CODE_FOR_nothing
)
5767 struct expand_operand op
;
5768 rtx val
= expand_expr (CALL_EXPR_ARG (exp
, 0), NULL_RTX
,
5769 Pmode
, EXPAND_NORMAL
);
5770 create_input_operand (&op
, val
, Pmode
);
5771 expand_insn (icode
, 1, &op
);
5774 error ("__builtin_set_thread_pointer is not supported on this target");
5778 /* Emit code to restore the current value of stack. */
5781 expand_stack_restore (tree var
)
5784 rtx sa
= expand_normal (var
);
5786 sa
= convert_memory_address (Pmode
, sa
);
5788 prev
= get_last_insn ();
5789 emit_stack_restore (SAVE_BLOCK
, sa
);
5791 record_new_stack_level ();
5793 fixup_args_size_notes (prev
, get_last_insn (), 0);
5796 /* Emit code to save the current value of stack. */
5799 expand_stack_save (void)
5803 emit_stack_save (SAVE_BLOCK
, &ret
);
5808 /* Expand an expression EXP that calls a built-in function,
5809 with result going to TARGET if that's convenient
5810 (and in mode MODE if that's convenient).
5811 SUBTARGET may be used as the target for computing one of EXP's operands.
5812 IGNORE is nonzero if the value is to be ignored. */
5815 expand_builtin (tree exp
, rtx target
, rtx subtarget
, machine_mode mode
,
5818 tree fndecl
= get_callee_fndecl (exp
);
5819 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
5820 machine_mode target_mode
= TYPE_MODE (TREE_TYPE (exp
));
5823 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
5824 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
5826 /* When ASan is enabled, we don't want to expand some memory/string
5827 builtins and rely on libsanitizer's hooks. This allows us to avoid
5828 redundant checks and be sure, that possible overflow will be detected
5831 if ((flag_sanitize
& SANITIZE_ADDRESS
) && asan_intercepted_p (fcode
))
5832 return expand_call (exp
, target
, ignore
);
5834 /* When not optimizing, generate calls to library functions for a certain
5837 && !called_as_built_in (fndecl
)
5838 && fcode
!= BUILT_IN_FORK
5839 && fcode
!= BUILT_IN_EXECL
5840 && fcode
!= BUILT_IN_EXECV
5841 && fcode
!= BUILT_IN_EXECLP
5842 && fcode
!= BUILT_IN_EXECLE
5843 && fcode
!= BUILT_IN_EXECVP
5844 && fcode
!= BUILT_IN_EXECVE
5845 && fcode
!= BUILT_IN_ALLOCA
5846 && fcode
!= BUILT_IN_ALLOCA_WITH_ALIGN
5847 && fcode
!= BUILT_IN_FREE
5848 && fcode
!= BUILT_IN_CHKP_SET_PTR_BOUNDS
5849 && fcode
!= BUILT_IN_CHKP_INIT_PTR_BOUNDS
5850 && fcode
!= BUILT_IN_CHKP_NULL_PTR_BOUNDS
5851 && fcode
!= BUILT_IN_CHKP_COPY_PTR_BOUNDS
5852 && fcode
!= BUILT_IN_CHKP_NARROW_PTR_BOUNDS
5853 && fcode
!= BUILT_IN_CHKP_STORE_PTR_BOUNDS
5854 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
5855 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
5856 && fcode
!= BUILT_IN_CHKP_CHECK_PTR_BOUNDS
5857 && fcode
!= BUILT_IN_CHKP_GET_PTR_LBOUND
5858 && fcode
!= BUILT_IN_CHKP_GET_PTR_UBOUND
5859 && fcode
!= BUILT_IN_CHKP_BNDRET
)
5860 return expand_call (exp
, target
, ignore
);
5862 /* The built-in function expanders test for target == const0_rtx
5863 to determine whether the function's result will be ignored. */
5865 target
= const0_rtx
;
5867 /* If the result of a pure or const built-in function is ignored, and
5868 none of its arguments are volatile, we can avoid expanding the
5869 built-in call and just evaluate the arguments for side-effects. */
5870 if (target
== const0_rtx
5871 && ((flags
= flags_from_decl_or_type (fndecl
)) & (ECF_CONST
| ECF_PURE
))
5872 && !(flags
& ECF_LOOPING_CONST_OR_PURE
))
5874 bool volatilep
= false;
5876 call_expr_arg_iterator iter
;
5878 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5879 if (TREE_THIS_VOLATILE (arg
))
5887 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, exp
)
5888 expand_expr (arg
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
5893 /* expand_builtin_with_bounds is supposed to be used for
5894 instrumented builtin calls. */
5895 gcc_assert (!CALL_WITH_BOUNDS_P (exp
));
5899 CASE_FLT_FN (BUILT_IN_FABS
):
5900 case BUILT_IN_FABSD32
:
5901 case BUILT_IN_FABSD64
:
5902 case BUILT_IN_FABSD128
:
5903 target
= expand_builtin_fabs (exp
, target
, subtarget
);
5908 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
5909 target
= expand_builtin_copysign (exp
, target
, subtarget
);
5914 /* Just do a normal library call if we were unable to fold
5916 CASE_FLT_FN (BUILT_IN_CABS
):
5919 CASE_FLT_FN (BUILT_IN_EXP
):
5920 CASE_FLT_FN (BUILT_IN_EXP10
):
5921 CASE_FLT_FN (BUILT_IN_POW10
):
5922 CASE_FLT_FN (BUILT_IN_EXP2
):
5923 CASE_FLT_FN (BUILT_IN_EXPM1
):
5924 CASE_FLT_FN (BUILT_IN_LOGB
):
5925 CASE_FLT_FN (BUILT_IN_LOG
):
5926 CASE_FLT_FN (BUILT_IN_LOG10
):
5927 CASE_FLT_FN (BUILT_IN_LOG2
):
5928 CASE_FLT_FN (BUILT_IN_LOG1P
):
5929 CASE_FLT_FN (BUILT_IN_TAN
):
5930 CASE_FLT_FN (BUILT_IN_ASIN
):
5931 CASE_FLT_FN (BUILT_IN_ACOS
):
5932 CASE_FLT_FN (BUILT_IN_ATAN
):
5933 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
5934 /* Treat these like sqrt only if unsafe math optimizations are allowed,
5935 because of possible accuracy problems. */
5936 if (! flag_unsafe_math_optimizations
)
5938 CASE_FLT_FN (BUILT_IN_SQRT
):
5939 CASE_FLT_FN (BUILT_IN_FLOOR
):
5940 CASE_FLT_FN (BUILT_IN_CEIL
):
5941 CASE_FLT_FN (BUILT_IN_TRUNC
):
5942 CASE_FLT_FN (BUILT_IN_ROUND
):
5943 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
5944 CASE_FLT_FN (BUILT_IN_RINT
):
5945 target
= expand_builtin_mathfn (exp
, target
, subtarget
);
5950 CASE_FLT_FN (BUILT_IN_FMA
):
5951 target
= expand_builtin_mathfn_ternary (exp
, target
, subtarget
);
5956 CASE_FLT_FN (BUILT_IN_ILOGB
):
5957 if (! flag_unsafe_math_optimizations
)
5959 CASE_FLT_FN (BUILT_IN_ISINF
):
5960 CASE_FLT_FN (BUILT_IN_FINITE
):
5961 case BUILT_IN_ISFINITE
:
5962 case BUILT_IN_ISNORMAL
:
5963 target
= expand_builtin_interclass_mathfn (exp
, target
);
5968 CASE_FLT_FN (BUILT_IN_ICEIL
):
5969 CASE_FLT_FN (BUILT_IN_LCEIL
):
5970 CASE_FLT_FN (BUILT_IN_LLCEIL
):
5971 CASE_FLT_FN (BUILT_IN_LFLOOR
):
5972 CASE_FLT_FN (BUILT_IN_IFLOOR
):
5973 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
5974 target
= expand_builtin_int_roundingfn (exp
, target
);
5979 CASE_FLT_FN (BUILT_IN_IRINT
):
5980 CASE_FLT_FN (BUILT_IN_LRINT
):
5981 CASE_FLT_FN (BUILT_IN_LLRINT
):
5982 CASE_FLT_FN (BUILT_IN_IROUND
):
5983 CASE_FLT_FN (BUILT_IN_LROUND
):
5984 CASE_FLT_FN (BUILT_IN_LLROUND
):
5985 target
= expand_builtin_int_roundingfn_2 (exp
, target
);
5990 CASE_FLT_FN (BUILT_IN_POWI
):
5991 target
= expand_builtin_powi (exp
, target
);
5996 CASE_FLT_FN (BUILT_IN_ATAN2
):
5997 CASE_FLT_FN (BUILT_IN_LDEXP
):
5998 CASE_FLT_FN (BUILT_IN_SCALB
):
5999 CASE_FLT_FN (BUILT_IN_SCALBN
):
6000 CASE_FLT_FN (BUILT_IN_SCALBLN
):
6001 if (! flag_unsafe_math_optimizations
)
6004 CASE_FLT_FN (BUILT_IN_FMOD
):
6005 CASE_FLT_FN (BUILT_IN_REMAINDER
):
6006 CASE_FLT_FN (BUILT_IN_DREM
):
6007 CASE_FLT_FN (BUILT_IN_POW
):
6008 target
= expand_builtin_mathfn_2 (exp
, target
, subtarget
);
6013 CASE_FLT_FN (BUILT_IN_CEXPI
):
6014 target
= expand_builtin_cexpi (exp
, target
);
6015 gcc_assert (target
);
6018 CASE_FLT_FN (BUILT_IN_SIN
):
6019 CASE_FLT_FN (BUILT_IN_COS
):
6020 if (! flag_unsafe_math_optimizations
)
6022 target
= expand_builtin_mathfn_3 (exp
, target
, subtarget
);
6027 CASE_FLT_FN (BUILT_IN_SINCOS
):
6028 if (! flag_unsafe_math_optimizations
)
6030 target
= expand_builtin_sincos (exp
);
6035 case BUILT_IN_APPLY_ARGS
:
6036 return expand_builtin_apply_args ();
6038 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6039 FUNCTION with a copy of the parameters described by
6040 ARGUMENTS, and ARGSIZE. It returns a block of memory
6041 allocated on the stack into which is stored all the registers
6042 that might possibly be used for returning the result of a
6043 function. ARGUMENTS is the value returned by
6044 __builtin_apply_args. ARGSIZE is the number of bytes of
6045 arguments that must be copied. ??? How should this value be
6046 computed? We'll also need a safe worst case value for varargs
6048 case BUILT_IN_APPLY
:
6049 if (!validate_arglist (exp
, POINTER_TYPE
,
6050 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
)
6051 && !validate_arglist (exp
, REFERENCE_TYPE
,
6052 POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6058 ops
[0] = expand_normal (CALL_EXPR_ARG (exp
, 0));
6059 ops
[1] = expand_normal (CALL_EXPR_ARG (exp
, 1));
6060 ops
[2] = expand_normal (CALL_EXPR_ARG (exp
, 2));
6062 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
6065 /* __builtin_return (RESULT) causes the function to return the
6066 value described by RESULT. RESULT is address of the block of
6067 memory returned by __builtin_apply. */
6068 case BUILT_IN_RETURN
:
6069 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6070 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp
, 0)));
6073 case BUILT_IN_SAVEREGS
:
6074 return expand_builtin_saveregs ();
6076 case BUILT_IN_VA_ARG_PACK
:
6077 /* All valid uses of __builtin_va_arg_pack () are removed during
6079 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp
);
6082 case BUILT_IN_VA_ARG_PACK_LEN
:
6083 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6085 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp
);
6088 /* Return the address of the first anonymous stack arg. */
6089 case BUILT_IN_NEXT_ARG
:
6090 if (fold_builtin_next_arg (exp
, false))
6092 return expand_builtin_next_arg ();
6094 case BUILT_IN_CLEAR_CACHE
:
6095 target
= expand_builtin___clear_cache (exp
);
6100 case BUILT_IN_CLASSIFY_TYPE
:
6101 return expand_builtin_classify_type (exp
);
6103 case BUILT_IN_CONSTANT_P
:
6106 case BUILT_IN_FRAME_ADDRESS
:
6107 case BUILT_IN_RETURN_ADDRESS
:
6108 return expand_builtin_frame_address (fndecl
, exp
);
6110 /* Returns the address of the area where the structure is returned.
6112 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
6113 if (call_expr_nargs (exp
) != 0
6114 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
6115 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl
))))
6118 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
6120 case BUILT_IN_ALLOCA
:
6121 case BUILT_IN_ALLOCA_WITH_ALIGN
:
6122 /* If the allocation stems from the declaration of a variable-sized
6123 object, it cannot accumulate. */
6124 target
= expand_builtin_alloca (exp
, CALL_ALLOCA_FOR_VAR_P (exp
));
6129 case BUILT_IN_STACK_SAVE
:
6130 return expand_stack_save ();
6132 case BUILT_IN_STACK_RESTORE
:
6133 expand_stack_restore (CALL_EXPR_ARG (exp
, 0));
6136 case BUILT_IN_BSWAP16
:
6137 case BUILT_IN_BSWAP32
:
6138 case BUILT_IN_BSWAP64
:
6139 target
= expand_builtin_bswap (target_mode
, exp
, target
, subtarget
);
6144 CASE_INT_FN (BUILT_IN_FFS
):
6145 target
= expand_builtin_unop (target_mode
, exp
, target
,
6146 subtarget
, ffs_optab
);
6151 CASE_INT_FN (BUILT_IN_CLZ
):
6152 target
= expand_builtin_unop (target_mode
, exp
, target
,
6153 subtarget
, clz_optab
);
6158 CASE_INT_FN (BUILT_IN_CTZ
):
6159 target
= expand_builtin_unop (target_mode
, exp
, target
,
6160 subtarget
, ctz_optab
);
6165 CASE_INT_FN (BUILT_IN_CLRSB
):
6166 target
= expand_builtin_unop (target_mode
, exp
, target
,
6167 subtarget
, clrsb_optab
);
6172 CASE_INT_FN (BUILT_IN_POPCOUNT
):
6173 target
= expand_builtin_unop (target_mode
, exp
, target
,
6174 subtarget
, popcount_optab
);
6179 CASE_INT_FN (BUILT_IN_PARITY
):
6180 target
= expand_builtin_unop (target_mode
, exp
, target
,
6181 subtarget
, parity_optab
);
6186 case BUILT_IN_STRLEN
:
6187 target
= expand_builtin_strlen (exp
, target
, target_mode
);
6192 case BUILT_IN_STRCPY
:
6193 target
= expand_builtin_strcpy (exp
, target
);
6198 case BUILT_IN_STRNCPY
:
6199 target
= expand_builtin_strncpy (exp
, target
);
6204 case BUILT_IN_STPCPY
:
6205 target
= expand_builtin_stpcpy (exp
, target
, mode
);
6210 case BUILT_IN_MEMCPY
:
6211 target
= expand_builtin_memcpy (exp
, target
);
6216 case BUILT_IN_MEMPCPY
:
6217 target
= expand_builtin_mempcpy (exp
, target
, mode
);
6222 case BUILT_IN_MEMSET
:
6223 target
= expand_builtin_memset (exp
, target
, mode
);
6228 case BUILT_IN_BZERO
:
6229 target
= expand_builtin_bzero (exp
);
6234 case BUILT_IN_STRCMP
:
6235 target
= expand_builtin_strcmp (exp
, target
);
6240 case BUILT_IN_STRNCMP
:
6241 target
= expand_builtin_strncmp (exp
, target
, mode
);
6247 case BUILT_IN_MEMCMP
:
6248 target
= expand_builtin_memcmp (exp
, target
);
6253 case BUILT_IN_SETJMP
:
6254 /* This should have been lowered to the builtins below. */
6257 case BUILT_IN_SETJMP_SETUP
:
6258 /* __builtin_setjmp_setup is passed a pointer to an array of five words
6259 and the receiver label. */
6260 if (validate_arglist (exp
, POINTER_TYPE
, POINTER_TYPE
, VOID_TYPE
))
6262 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6263 VOIDmode
, EXPAND_NORMAL
);
6264 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 1), 0);
6265 rtx_insn
*label_r
= label_rtx (label
);
6267 /* This is copied from the handling of non-local gotos. */
6268 expand_builtin_setjmp_setup (buf_addr
, label_r
);
6269 nonlocal_goto_handler_labels
6270 = gen_rtx_INSN_LIST (VOIDmode
, label_r
,
6271 nonlocal_goto_handler_labels
);
6272 /* ??? Do not let expand_label treat us as such since we would
6273 not want to be both on the list of non-local labels and on
6274 the list of forced labels. */
6275 FORCED_LABEL (label
) = 0;
6280 case BUILT_IN_SETJMP_RECEIVER
:
6281 /* __builtin_setjmp_receiver is passed the receiver label. */
6282 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6284 tree label
= TREE_OPERAND (CALL_EXPR_ARG (exp
, 0), 0);
6285 rtx_insn
*label_r
= label_rtx (label
);
6287 expand_builtin_setjmp_receiver (label_r
);
6292 /* __builtin_longjmp is passed a pointer to an array of five words.
6293 It's similar to the C library longjmp function but works with
6294 __builtin_setjmp above. */
6295 case BUILT_IN_LONGJMP
:
6296 if (validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
6298 rtx buf_addr
= expand_expr (CALL_EXPR_ARG (exp
, 0), subtarget
,
6299 VOIDmode
, EXPAND_NORMAL
);
6300 rtx value
= expand_normal (CALL_EXPR_ARG (exp
, 1));
6302 if (value
!= const1_rtx
)
6304 error ("%<__builtin_longjmp%> second argument must be 1");
6308 expand_builtin_longjmp (buf_addr
, value
);
6313 case BUILT_IN_NONLOCAL_GOTO
:
6314 target
= expand_builtin_nonlocal_goto (exp
);
6319 /* This updates the setjmp buffer that is its argument with the value
6320 of the current stack pointer. */
6321 case BUILT_IN_UPDATE_SETJMP_BUF
:
6322 if (validate_arglist (exp
, POINTER_TYPE
, VOID_TYPE
))
6325 = expand_normal (CALL_EXPR_ARG (exp
, 0));
6327 expand_builtin_update_setjmp_buf (buf_addr
);
6333 expand_builtin_trap ();
6336 case BUILT_IN_UNREACHABLE
:
6337 expand_builtin_unreachable ();
6340 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
6341 case BUILT_IN_SIGNBITD32
:
6342 case BUILT_IN_SIGNBITD64
:
6343 case BUILT_IN_SIGNBITD128
:
6344 target
= expand_builtin_signbit (exp
, target
);
6349 /* Various hooks for the DWARF 2 __throw routine. */
6350 case BUILT_IN_UNWIND_INIT
:
6351 expand_builtin_unwind_init ();
6353 case BUILT_IN_DWARF_CFA
:
6354 return virtual_cfa_rtx
;
6355 #ifdef DWARF2_UNWIND_INFO
6356 case BUILT_IN_DWARF_SP_COLUMN
:
6357 return expand_builtin_dwarf_sp_column ();
6358 case BUILT_IN_INIT_DWARF_REG_SIZES
:
6359 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp
, 0));
6362 case BUILT_IN_FROB_RETURN_ADDR
:
6363 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp
, 0));
6364 case BUILT_IN_EXTRACT_RETURN_ADDR
:
6365 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp
, 0));
6366 case BUILT_IN_EH_RETURN
:
6367 expand_builtin_eh_return (CALL_EXPR_ARG (exp
, 0),
6368 CALL_EXPR_ARG (exp
, 1));
6370 case BUILT_IN_EH_RETURN_DATA_REGNO
:
6371 return expand_builtin_eh_return_data_regno (exp
);
6372 case BUILT_IN_EXTEND_POINTER
:
6373 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp
, 0));
6374 case BUILT_IN_EH_POINTER
:
6375 return expand_builtin_eh_pointer (exp
);
6376 case BUILT_IN_EH_FILTER
:
6377 return expand_builtin_eh_filter (exp
);
6378 case BUILT_IN_EH_COPY_VALUES
:
6379 return expand_builtin_eh_copy_values (exp
);
6381 case BUILT_IN_VA_START
:
6382 return expand_builtin_va_start (exp
);
6383 case BUILT_IN_VA_END
:
6384 return expand_builtin_va_end (exp
);
6385 case BUILT_IN_VA_COPY
:
6386 return expand_builtin_va_copy (exp
);
6387 case BUILT_IN_EXPECT
:
6388 return expand_builtin_expect (exp
, target
);
6389 case BUILT_IN_ASSUME_ALIGNED
:
6390 return expand_builtin_assume_aligned (exp
, target
);
6391 case BUILT_IN_PREFETCH
:
6392 expand_builtin_prefetch (exp
);
6395 case BUILT_IN_INIT_TRAMPOLINE
:
6396 return expand_builtin_init_trampoline (exp
, true);
6397 case BUILT_IN_INIT_HEAP_TRAMPOLINE
:
6398 return expand_builtin_init_trampoline (exp
, false);
6399 case BUILT_IN_ADJUST_TRAMPOLINE
:
6400 return expand_builtin_adjust_trampoline (exp
);
6403 case BUILT_IN_EXECL
:
6404 case BUILT_IN_EXECV
:
6405 case BUILT_IN_EXECLP
:
6406 case BUILT_IN_EXECLE
:
6407 case BUILT_IN_EXECVP
:
6408 case BUILT_IN_EXECVE
:
6409 target
= expand_builtin_fork_or_exec (fndecl
, exp
, target
, ignore
);
6414 case BUILT_IN_SYNC_FETCH_AND_ADD_1
:
6415 case BUILT_IN_SYNC_FETCH_AND_ADD_2
:
6416 case BUILT_IN_SYNC_FETCH_AND_ADD_4
:
6417 case BUILT_IN_SYNC_FETCH_AND_ADD_8
:
6418 case BUILT_IN_SYNC_FETCH_AND_ADD_16
:
6419 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_ADD_1
);
6420 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, false, target
);
6425 case BUILT_IN_SYNC_FETCH_AND_SUB_1
:
6426 case BUILT_IN_SYNC_FETCH_AND_SUB_2
:
6427 case BUILT_IN_SYNC_FETCH_AND_SUB_4
:
6428 case BUILT_IN_SYNC_FETCH_AND_SUB_8
:
6429 case BUILT_IN_SYNC_FETCH_AND_SUB_16
:
6430 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_SUB_1
);
6431 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, false, target
);
6436 case BUILT_IN_SYNC_FETCH_AND_OR_1
:
6437 case BUILT_IN_SYNC_FETCH_AND_OR_2
:
6438 case BUILT_IN_SYNC_FETCH_AND_OR_4
:
6439 case BUILT_IN_SYNC_FETCH_AND_OR_8
:
6440 case BUILT_IN_SYNC_FETCH_AND_OR_16
:
6441 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_OR_1
);
6442 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, false, target
);
6447 case BUILT_IN_SYNC_FETCH_AND_AND_1
:
6448 case BUILT_IN_SYNC_FETCH_AND_AND_2
:
6449 case BUILT_IN_SYNC_FETCH_AND_AND_4
:
6450 case BUILT_IN_SYNC_FETCH_AND_AND_8
:
6451 case BUILT_IN_SYNC_FETCH_AND_AND_16
:
6452 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_AND_1
);
6453 target
= expand_builtin_sync_operation (mode
, exp
, AND
, false, target
);
6458 case BUILT_IN_SYNC_FETCH_AND_XOR_1
:
6459 case BUILT_IN_SYNC_FETCH_AND_XOR_2
:
6460 case BUILT_IN_SYNC_FETCH_AND_XOR_4
:
6461 case BUILT_IN_SYNC_FETCH_AND_XOR_8
:
6462 case BUILT_IN_SYNC_FETCH_AND_XOR_16
:
6463 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_XOR_1
);
6464 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, false, target
);
6469 case BUILT_IN_SYNC_FETCH_AND_NAND_1
:
6470 case BUILT_IN_SYNC_FETCH_AND_NAND_2
:
6471 case BUILT_IN_SYNC_FETCH_AND_NAND_4
:
6472 case BUILT_IN_SYNC_FETCH_AND_NAND_8
:
6473 case BUILT_IN_SYNC_FETCH_AND_NAND_16
:
6474 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_FETCH_AND_NAND_1
);
6475 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, false, target
);
6480 case BUILT_IN_SYNC_ADD_AND_FETCH_1
:
6481 case BUILT_IN_SYNC_ADD_AND_FETCH_2
:
6482 case BUILT_IN_SYNC_ADD_AND_FETCH_4
:
6483 case BUILT_IN_SYNC_ADD_AND_FETCH_8
:
6484 case BUILT_IN_SYNC_ADD_AND_FETCH_16
:
6485 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_ADD_AND_FETCH_1
);
6486 target
= expand_builtin_sync_operation (mode
, exp
, PLUS
, true, target
);
6491 case BUILT_IN_SYNC_SUB_AND_FETCH_1
:
6492 case BUILT_IN_SYNC_SUB_AND_FETCH_2
:
6493 case BUILT_IN_SYNC_SUB_AND_FETCH_4
:
6494 case BUILT_IN_SYNC_SUB_AND_FETCH_8
:
6495 case BUILT_IN_SYNC_SUB_AND_FETCH_16
:
6496 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_SUB_AND_FETCH_1
);
6497 target
= expand_builtin_sync_operation (mode
, exp
, MINUS
, true, target
);
6502 case BUILT_IN_SYNC_OR_AND_FETCH_1
:
6503 case BUILT_IN_SYNC_OR_AND_FETCH_2
:
6504 case BUILT_IN_SYNC_OR_AND_FETCH_4
:
6505 case BUILT_IN_SYNC_OR_AND_FETCH_8
:
6506 case BUILT_IN_SYNC_OR_AND_FETCH_16
:
6507 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_OR_AND_FETCH_1
);
6508 target
= expand_builtin_sync_operation (mode
, exp
, IOR
, true, target
);
6513 case BUILT_IN_SYNC_AND_AND_FETCH_1
:
6514 case BUILT_IN_SYNC_AND_AND_FETCH_2
:
6515 case BUILT_IN_SYNC_AND_AND_FETCH_4
:
6516 case BUILT_IN_SYNC_AND_AND_FETCH_8
:
6517 case BUILT_IN_SYNC_AND_AND_FETCH_16
:
6518 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_AND_AND_FETCH_1
);
6519 target
= expand_builtin_sync_operation (mode
, exp
, AND
, true, target
);
6524 case BUILT_IN_SYNC_XOR_AND_FETCH_1
:
6525 case BUILT_IN_SYNC_XOR_AND_FETCH_2
:
6526 case BUILT_IN_SYNC_XOR_AND_FETCH_4
:
6527 case BUILT_IN_SYNC_XOR_AND_FETCH_8
:
6528 case BUILT_IN_SYNC_XOR_AND_FETCH_16
:
6529 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_XOR_AND_FETCH_1
);
6530 target
= expand_builtin_sync_operation (mode
, exp
, XOR
, true, target
);
6535 case BUILT_IN_SYNC_NAND_AND_FETCH_1
:
6536 case BUILT_IN_SYNC_NAND_AND_FETCH_2
:
6537 case BUILT_IN_SYNC_NAND_AND_FETCH_4
:
6538 case BUILT_IN_SYNC_NAND_AND_FETCH_8
:
6539 case BUILT_IN_SYNC_NAND_AND_FETCH_16
:
6540 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_NAND_AND_FETCH_1
);
6541 target
= expand_builtin_sync_operation (mode
, exp
, NOT
, true, target
);
6546 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
:
6547 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2
:
6548 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4
:
6549 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8
:
6550 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16
:
6551 if (mode
== VOIDmode
)
6552 mode
= TYPE_MODE (boolean_type_node
);
6553 if (!target
|| !register_operand (target
, mode
))
6554 target
= gen_reg_rtx (mode
);
6556 mode
= get_builtin_sync_mode
6557 (fcode
- BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1
);
6558 target
= expand_builtin_compare_and_swap (mode
, exp
, true, target
);
6563 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
:
6564 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2
:
6565 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4
:
6566 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8
:
6567 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16
:
6568 mode
= get_builtin_sync_mode
6569 (fcode
- BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1
);
6570 target
= expand_builtin_compare_and_swap (mode
, exp
, false, target
);
6575 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
:
6576 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2
:
6577 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4
:
6578 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8
:
6579 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16
:
6580 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_TEST_AND_SET_1
);
6581 target
= expand_builtin_sync_lock_test_and_set (mode
, exp
, target
);
6586 case BUILT_IN_SYNC_LOCK_RELEASE_1
:
6587 case BUILT_IN_SYNC_LOCK_RELEASE_2
:
6588 case BUILT_IN_SYNC_LOCK_RELEASE_4
:
6589 case BUILT_IN_SYNC_LOCK_RELEASE_8
:
6590 case BUILT_IN_SYNC_LOCK_RELEASE_16
:
6591 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_SYNC_LOCK_RELEASE_1
);
6592 expand_builtin_sync_lock_release (mode
, exp
);
6595 case BUILT_IN_SYNC_SYNCHRONIZE
:
6596 expand_builtin_sync_synchronize ();
6599 case BUILT_IN_ATOMIC_EXCHANGE_1
:
6600 case BUILT_IN_ATOMIC_EXCHANGE_2
:
6601 case BUILT_IN_ATOMIC_EXCHANGE_4
:
6602 case BUILT_IN_ATOMIC_EXCHANGE_8
:
6603 case BUILT_IN_ATOMIC_EXCHANGE_16
:
6604 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_EXCHANGE_1
);
6605 target
= expand_builtin_atomic_exchange (mode
, exp
, target
);
6610 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
:
6611 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2
:
6612 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
:
6613 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
:
6614 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16
:
6616 unsigned int nargs
, z
;
6617 vec
<tree
, va_gc
> *vec
;
6620 get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
);
6621 target
= expand_builtin_atomic_compare_exchange (mode
, exp
, target
);
6625 /* If this is turned into an external library call, the weak parameter
6626 must be dropped to match the expected parameter list. */
6627 nargs
= call_expr_nargs (exp
);
6628 vec_alloc (vec
, nargs
- 1);
6629 for (z
= 0; z
< 3; z
++)
6630 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
6631 /* Skip the boolean weak parameter. */
6632 for (z
= 4; z
< 6; z
++)
6633 vec
->quick_push (CALL_EXPR_ARG (exp
, z
));
6634 exp
= build_call_vec (TREE_TYPE (exp
), CALL_EXPR_FN (exp
), vec
);
6638 case BUILT_IN_ATOMIC_LOAD_1
:
6639 case BUILT_IN_ATOMIC_LOAD_2
:
6640 case BUILT_IN_ATOMIC_LOAD_4
:
6641 case BUILT_IN_ATOMIC_LOAD_8
:
6642 case BUILT_IN_ATOMIC_LOAD_16
:
6643 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_LOAD_1
);
6644 target
= expand_builtin_atomic_load (mode
, exp
, target
);
6649 case BUILT_IN_ATOMIC_STORE_1
:
6650 case BUILT_IN_ATOMIC_STORE_2
:
6651 case BUILT_IN_ATOMIC_STORE_4
:
6652 case BUILT_IN_ATOMIC_STORE_8
:
6653 case BUILT_IN_ATOMIC_STORE_16
:
6654 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_STORE_1
);
6655 target
= expand_builtin_atomic_store (mode
, exp
);
6660 case BUILT_IN_ATOMIC_ADD_FETCH_1
:
6661 case BUILT_IN_ATOMIC_ADD_FETCH_2
:
6662 case BUILT_IN_ATOMIC_ADD_FETCH_4
:
6663 case BUILT_IN_ATOMIC_ADD_FETCH_8
:
6664 case BUILT_IN_ATOMIC_ADD_FETCH_16
:
6666 enum built_in_function lib
;
6667 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
);
6668 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_ADD_1
+
6669 (fcode
- BUILT_IN_ATOMIC_ADD_FETCH_1
));
6670 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, true,
6676 case BUILT_IN_ATOMIC_SUB_FETCH_1
:
6677 case BUILT_IN_ATOMIC_SUB_FETCH_2
:
6678 case BUILT_IN_ATOMIC_SUB_FETCH_4
:
6679 case BUILT_IN_ATOMIC_SUB_FETCH_8
:
6680 case BUILT_IN_ATOMIC_SUB_FETCH_16
:
6682 enum built_in_function lib
;
6683 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
);
6684 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_SUB_1
+
6685 (fcode
- BUILT_IN_ATOMIC_SUB_FETCH_1
));
6686 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, true,
6692 case BUILT_IN_ATOMIC_AND_FETCH_1
:
6693 case BUILT_IN_ATOMIC_AND_FETCH_2
:
6694 case BUILT_IN_ATOMIC_AND_FETCH_4
:
6695 case BUILT_IN_ATOMIC_AND_FETCH_8
:
6696 case BUILT_IN_ATOMIC_AND_FETCH_16
:
6698 enum built_in_function lib
;
6699 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
);
6700 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_AND_1
+
6701 (fcode
- BUILT_IN_ATOMIC_AND_FETCH_1
));
6702 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, true,
6708 case BUILT_IN_ATOMIC_NAND_FETCH_1
:
6709 case BUILT_IN_ATOMIC_NAND_FETCH_2
:
6710 case BUILT_IN_ATOMIC_NAND_FETCH_4
:
6711 case BUILT_IN_ATOMIC_NAND_FETCH_8
:
6712 case BUILT_IN_ATOMIC_NAND_FETCH_16
:
6714 enum built_in_function lib
;
6715 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
);
6716 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_NAND_1
+
6717 (fcode
- BUILT_IN_ATOMIC_NAND_FETCH_1
));
6718 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, true,
6724 case BUILT_IN_ATOMIC_XOR_FETCH_1
:
6725 case BUILT_IN_ATOMIC_XOR_FETCH_2
:
6726 case BUILT_IN_ATOMIC_XOR_FETCH_4
:
6727 case BUILT_IN_ATOMIC_XOR_FETCH_8
:
6728 case BUILT_IN_ATOMIC_XOR_FETCH_16
:
6730 enum built_in_function lib
;
6731 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
);
6732 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_XOR_1
+
6733 (fcode
- BUILT_IN_ATOMIC_XOR_FETCH_1
));
6734 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, true,
6740 case BUILT_IN_ATOMIC_OR_FETCH_1
:
6741 case BUILT_IN_ATOMIC_OR_FETCH_2
:
6742 case BUILT_IN_ATOMIC_OR_FETCH_4
:
6743 case BUILT_IN_ATOMIC_OR_FETCH_8
:
6744 case BUILT_IN_ATOMIC_OR_FETCH_16
:
6746 enum built_in_function lib
;
6747 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
);
6748 lib
= (enum built_in_function
)((int)BUILT_IN_ATOMIC_FETCH_OR_1
+
6749 (fcode
- BUILT_IN_ATOMIC_OR_FETCH_1
));
6750 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, true,
6756 case BUILT_IN_ATOMIC_FETCH_ADD_1
:
6757 case BUILT_IN_ATOMIC_FETCH_ADD_2
:
6758 case BUILT_IN_ATOMIC_FETCH_ADD_4
:
6759 case BUILT_IN_ATOMIC_FETCH_ADD_8
:
6760 case BUILT_IN_ATOMIC_FETCH_ADD_16
:
6761 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_ADD_1
);
6762 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, PLUS
, false,
6763 ignore
, BUILT_IN_NONE
);
6768 case BUILT_IN_ATOMIC_FETCH_SUB_1
:
6769 case BUILT_IN_ATOMIC_FETCH_SUB_2
:
6770 case BUILT_IN_ATOMIC_FETCH_SUB_4
:
6771 case BUILT_IN_ATOMIC_FETCH_SUB_8
:
6772 case BUILT_IN_ATOMIC_FETCH_SUB_16
:
6773 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_SUB_1
);
6774 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, MINUS
, false,
6775 ignore
, BUILT_IN_NONE
);
6780 case BUILT_IN_ATOMIC_FETCH_AND_1
:
6781 case BUILT_IN_ATOMIC_FETCH_AND_2
:
6782 case BUILT_IN_ATOMIC_FETCH_AND_4
:
6783 case BUILT_IN_ATOMIC_FETCH_AND_8
:
6784 case BUILT_IN_ATOMIC_FETCH_AND_16
:
6785 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_AND_1
);
6786 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, AND
, false,
6787 ignore
, BUILT_IN_NONE
);
6792 case BUILT_IN_ATOMIC_FETCH_NAND_1
:
6793 case BUILT_IN_ATOMIC_FETCH_NAND_2
:
6794 case BUILT_IN_ATOMIC_FETCH_NAND_4
:
6795 case BUILT_IN_ATOMIC_FETCH_NAND_8
:
6796 case BUILT_IN_ATOMIC_FETCH_NAND_16
:
6797 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_NAND_1
);
6798 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, NOT
, false,
6799 ignore
, BUILT_IN_NONE
);
6804 case BUILT_IN_ATOMIC_FETCH_XOR_1
:
6805 case BUILT_IN_ATOMIC_FETCH_XOR_2
:
6806 case BUILT_IN_ATOMIC_FETCH_XOR_4
:
6807 case BUILT_IN_ATOMIC_FETCH_XOR_8
:
6808 case BUILT_IN_ATOMIC_FETCH_XOR_16
:
6809 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_XOR_1
);
6810 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, XOR
, false,
6811 ignore
, BUILT_IN_NONE
);
6816 case BUILT_IN_ATOMIC_FETCH_OR_1
:
6817 case BUILT_IN_ATOMIC_FETCH_OR_2
:
6818 case BUILT_IN_ATOMIC_FETCH_OR_4
:
6819 case BUILT_IN_ATOMIC_FETCH_OR_8
:
6820 case BUILT_IN_ATOMIC_FETCH_OR_16
:
6821 mode
= get_builtin_sync_mode (fcode
- BUILT_IN_ATOMIC_FETCH_OR_1
);
6822 target
= expand_builtin_atomic_fetch_op (mode
, exp
, target
, IOR
, false,
6823 ignore
, BUILT_IN_NONE
);
6828 case BUILT_IN_ATOMIC_TEST_AND_SET
:
6829 return expand_builtin_atomic_test_and_set (exp
, target
);
6831 case BUILT_IN_ATOMIC_CLEAR
:
6832 return expand_builtin_atomic_clear (exp
);
6834 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
6835 return expand_builtin_atomic_always_lock_free (exp
);
6837 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
6838 target
= expand_builtin_atomic_is_lock_free (exp
);
6843 case BUILT_IN_ATOMIC_THREAD_FENCE
:
6844 expand_builtin_atomic_thread_fence (exp
);
6847 case BUILT_IN_ATOMIC_SIGNAL_FENCE
:
6848 expand_builtin_atomic_signal_fence (exp
);
6851 case BUILT_IN_OBJECT_SIZE
:
6852 return expand_builtin_object_size (exp
);
6854 case BUILT_IN_MEMCPY_CHK
:
6855 case BUILT_IN_MEMPCPY_CHK
:
6856 case BUILT_IN_MEMMOVE_CHK
:
6857 case BUILT_IN_MEMSET_CHK
:
6858 target
= expand_builtin_memory_chk (exp
, target
, mode
, fcode
);
6863 case BUILT_IN_STRCPY_CHK
:
6864 case BUILT_IN_STPCPY_CHK
:
6865 case BUILT_IN_STRNCPY_CHK
:
6866 case BUILT_IN_STPNCPY_CHK
:
6867 case BUILT_IN_STRCAT_CHK
:
6868 case BUILT_IN_STRNCAT_CHK
:
6869 case BUILT_IN_SNPRINTF_CHK
:
6870 case BUILT_IN_VSNPRINTF_CHK
:
6871 maybe_emit_chk_warning (exp
, fcode
);
6874 case BUILT_IN_SPRINTF_CHK
:
6875 case BUILT_IN_VSPRINTF_CHK
:
6876 maybe_emit_sprintf_chk_warning (exp
, fcode
);
6880 if (warn_free_nonheap_object
)
6881 maybe_emit_free_warning (exp
);
6884 case BUILT_IN_THREAD_POINTER
:
6885 return expand_builtin_thread_pointer (exp
, target
);
6887 case BUILT_IN_SET_THREAD_POINTER
:
6888 expand_builtin_set_thread_pointer (exp
);
6891 case BUILT_IN_CILK_DETACH
:
6892 expand_builtin_cilk_detach (exp
);
6895 case BUILT_IN_CILK_POP_FRAME
:
6896 expand_builtin_cilk_pop_frame (exp
);
6899 case BUILT_IN_CHKP_INIT_PTR_BOUNDS
:
6900 case BUILT_IN_CHKP_NULL_PTR_BOUNDS
:
6901 case BUILT_IN_CHKP_COPY_PTR_BOUNDS
:
6902 case BUILT_IN_CHKP_CHECK_PTR_LBOUNDS
:
6903 case BUILT_IN_CHKP_CHECK_PTR_UBOUNDS
:
6904 case BUILT_IN_CHKP_CHECK_PTR_BOUNDS
:
6905 case BUILT_IN_CHKP_SET_PTR_BOUNDS
:
6906 case BUILT_IN_CHKP_NARROW_PTR_BOUNDS
:
6907 case BUILT_IN_CHKP_STORE_PTR_BOUNDS
:
6908 case BUILT_IN_CHKP_GET_PTR_LBOUND
:
6909 case BUILT_IN_CHKP_GET_PTR_UBOUND
:
6910 /* We allow user CHKP builtins if Pointer Bounds
6912 if (!chkp_function_instrumented_p (current_function_decl
))
6914 if (fcode
== BUILT_IN_CHKP_SET_PTR_BOUNDS
6915 || fcode
== BUILT_IN_CHKP_NARROW_PTR_BOUNDS
6916 || fcode
== BUILT_IN_CHKP_INIT_PTR_BOUNDS
6917 || fcode
== BUILT_IN_CHKP_NULL_PTR_BOUNDS
6918 || fcode
== BUILT_IN_CHKP_COPY_PTR_BOUNDS
)
6919 return expand_normal (CALL_EXPR_ARG (exp
, 0));
6920 else if (fcode
== BUILT_IN_CHKP_GET_PTR_LBOUND
)
6921 return expand_normal (size_zero_node
);
6922 else if (fcode
== BUILT_IN_CHKP_GET_PTR_UBOUND
)
6923 return expand_normal (size_int (-1));
6929 case BUILT_IN_CHKP_BNDMK
:
6930 case BUILT_IN_CHKP_BNDSTX
:
6931 case BUILT_IN_CHKP_BNDCL
:
6932 case BUILT_IN_CHKP_BNDCU
:
6933 case BUILT_IN_CHKP_BNDLDX
:
6934 case BUILT_IN_CHKP_BNDRET
:
6935 case BUILT_IN_CHKP_INTERSECT
:
6936 case BUILT_IN_CHKP_NARROW
:
6937 case BUILT_IN_CHKP_EXTRACT_LOWER
:
6938 case BUILT_IN_CHKP_EXTRACT_UPPER
:
6939 /* Software implementation of Pointer Bounds Checker is NYI.
6940 Target support is required. */
6941 error ("Your target platform does not support -fcheck-pointer-bounds");
6944 case BUILT_IN_ACC_ON_DEVICE
:
6945 /* Do library call, if we failed to expand the builtin when
6949 default: /* just do library call, if unknown builtin */
6953 /* The switch statement above can drop through to cause the function
6954 to be called normally. */
6955 return expand_call (exp
, target
, ignore
);
6958 /* Similar to expand_builtin but is used for instrumented calls. */
6961 expand_builtin_with_bounds (tree exp
, rtx target
,
6962 rtx subtarget ATTRIBUTE_UNUSED
,
6963 machine_mode mode
, int ignore
)
6965 tree fndecl
= get_callee_fndecl (exp
);
6966 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
6968 gcc_assert (CALL_WITH_BOUNDS_P (exp
));
6970 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
6971 return targetm
.expand_builtin (exp
, target
, subtarget
, mode
, ignore
);
6973 gcc_assert (fcode
> BEGIN_CHKP_BUILTINS
6974 && fcode
< END_CHKP_BUILTINS
);
6978 case BUILT_IN_CHKP_MEMCPY_NOBND_NOCHK_CHKP
:
6979 target
= expand_builtin_memcpy_with_bounds (exp
, target
);
6984 case BUILT_IN_CHKP_MEMPCPY_NOBND_NOCHK_CHKP
:
6985 target
= expand_builtin_mempcpy_with_bounds (exp
, target
, mode
);
6990 case BUILT_IN_CHKP_MEMSET_NOBND_NOCHK_CHKP
:
6991 target
= expand_builtin_memset_with_bounds (exp
, target
, mode
);
7000 /* The switch statement above can drop through to cause the function
7001 to be called normally. */
7002 return expand_call (exp
, target
, ignore
);
7005 /* Determine whether a tree node represents a call to a built-in
7006 function. If the tree T is a call to a built-in function with
7007 the right number of arguments of the appropriate types, return
7008 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7009 Otherwise the return value is END_BUILTINS. */
7011 enum built_in_function
7012 builtin_mathfn_code (const_tree t
)
7014 const_tree fndecl
, arg
, parmlist
;
7015 const_tree argtype
, parmtype
;
7016 const_call_expr_arg_iterator iter
;
7018 if (TREE_CODE (t
) != CALL_EXPR
7019 || TREE_CODE (CALL_EXPR_FN (t
)) != ADDR_EXPR
)
7020 return END_BUILTINS
;
7022 fndecl
= get_callee_fndecl (t
);
7023 if (fndecl
== NULL_TREE
7024 || TREE_CODE (fndecl
) != FUNCTION_DECL
7025 || ! DECL_BUILT_IN (fndecl
)
7026 || DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
7027 return END_BUILTINS
;
7029 parmlist
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
7030 init_const_call_expr_arg_iterator (t
, &iter
);
7031 for (; parmlist
; parmlist
= TREE_CHAIN (parmlist
))
7033 /* If a function doesn't take a variable number of arguments,
7034 the last element in the list will have type `void'. */
7035 parmtype
= TREE_VALUE (parmlist
);
7036 if (VOID_TYPE_P (parmtype
))
7038 if (more_const_call_expr_args_p (&iter
))
7039 return END_BUILTINS
;
7040 return DECL_FUNCTION_CODE (fndecl
);
7043 if (! more_const_call_expr_args_p (&iter
))
7044 return END_BUILTINS
;
7046 arg
= next_const_call_expr_arg (&iter
);
7047 argtype
= TREE_TYPE (arg
);
7049 if (SCALAR_FLOAT_TYPE_P (parmtype
))
7051 if (! SCALAR_FLOAT_TYPE_P (argtype
))
7052 return END_BUILTINS
;
7054 else if (COMPLEX_FLOAT_TYPE_P (parmtype
))
7056 if (! COMPLEX_FLOAT_TYPE_P (argtype
))
7057 return END_BUILTINS
;
7059 else if (POINTER_TYPE_P (parmtype
))
7061 if (! POINTER_TYPE_P (argtype
))
7062 return END_BUILTINS
;
7064 else if (INTEGRAL_TYPE_P (parmtype
))
7066 if (! INTEGRAL_TYPE_P (argtype
))
7067 return END_BUILTINS
;
7070 return END_BUILTINS
;
7073 /* Variable-length argument list. */
7074 return DECL_FUNCTION_CODE (fndecl
);
7077 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7078 evaluate to a constant. */
7081 fold_builtin_constant_p (tree arg
)
7083 /* We return 1 for a numeric type that's known to be a constant
7084 value at compile-time or for an aggregate type that's a
7085 literal constant. */
7088 /* If we know this is a constant, emit the constant of one. */
7089 if (CONSTANT_CLASS_P (arg
)
7090 || (TREE_CODE (arg
) == CONSTRUCTOR
7091 && TREE_CONSTANT (arg
)))
7092 return integer_one_node
;
7093 if (TREE_CODE (arg
) == ADDR_EXPR
)
7095 tree op
= TREE_OPERAND (arg
, 0);
7096 if (TREE_CODE (op
) == STRING_CST
7097 || (TREE_CODE (op
) == ARRAY_REF
7098 && integer_zerop (TREE_OPERAND (op
, 1))
7099 && TREE_CODE (TREE_OPERAND (op
, 0)) == STRING_CST
))
7100 return integer_one_node
;
7103 /* If this expression has side effects, show we don't know it to be a
7104 constant. Likewise if it's a pointer or aggregate type since in
7105 those case we only want literals, since those are only optimized
7106 when generating RTL, not later.
7107 And finally, if we are compiling an initializer, not code, we
7108 need to return a definite result now; there's not going to be any
7109 more optimization done. */
7110 if (TREE_SIDE_EFFECTS (arg
)
7111 || AGGREGATE_TYPE_P (TREE_TYPE (arg
))
7112 || POINTER_TYPE_P (TREE_TYPE (arg
))
7114 || folding_initializer
7115 || force_folding_builtin_constant_p
)
7116 return integer_zero_node
;
7121 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7122 return it as a truthvalue. */
7125 build_builtin_expect_predicate (location_t loc
, tree pred
, tree expected
,
7128 tree fn
, arg_types
, pred_type
, expected_type
, call_expr
, ret_type
;
7130 fn
= builtin_decl_explicit (BUILT_IN_EXPECT
);
7131 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
7132 ret_type
= TREE_TYPE (TREE_TYPE (fn
));
7133 pred_type
= TREE_VALUE (arg_types
);
7134 expected_type
= TREE_VALUE (TREE_CHAIN (arg_types
));
7136 pred
= fold_convert_loc (loc
, pred_type
, pred
);
7137 expected
= fold_convert_loc (loc
, expected_type
, expected
);
7138 call_expr
= build_call_expr_loc (loc
, fn
, predictor
? 3 : 2, pred
, expected
,
7141 return build2 (NE_EXPR
, TREE_TYPE (pred
), call_expr
,
7142 build_int_cst (ret_type
, 0));
7145 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7146 NULL_TREE if no simplification is possible. */
7149 fold_builtin_expect (location_t loc
, tree arg0
, tree arg1
, tree arg2
)
7151 tree inner
, fndecl
, inner_arg0
;
7152 enum tree_code code
;
7154 /* Distribute the expected value over short-circuiting operators.
7155 See through the cast from truthvalue_type_node to long. */
7157 while (CONVERT_EXPR_P (inner_arg0
)
7158 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0
))
7159 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0
, 0))))
7160 inner_arg0
= TREE_OPERAND (inner_arg0
, 0);
7162 /* If this is a builtin_expect within a builtin_expect keep the
7163 inner one. See through a comparison against a constant. It
7164 might have been added to create a thruthvalue. */
7167 if (COMPARISON_CLASS_P (inner
)
7168 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
)
7169 inner
= TREE_OPERAND (inner
, 0);
7171 if (TREE_CODE (inner
) == CALL_EXPR
7172 && (fndecl
= get_callee_fndecl (inner
))
7173 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
7174 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_EXPECT
)
7178 code
= TREE_CODE (inner
);
7179 if (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
)
7181 tree op0
= TREE_OPERAND (inner
, 0);
7182 tree op1
= TREE_OPERAND (inner
, 1);
7184 op0
= build_builtin_expect_predicate (loc
, op0
, arg1
, arg2
);
7185 op1
= build_builtin_expect_predicate (loc
, op1
, arg1
, arg2
);
7186 inner
= build2 (code
, TREE_TYPE (inner
), op0
, op1
);
7188 return fold_convert_loc (loc
, TREE_TYPE (arg0
), inner
);
7191 /* If the argument isn't invariant then there's nothing else we can do. */
7192 if (!TREE_CONSTANT (inner_arg0
))
7195 /* If we expect that a comparison against the argument will fold to
7196 a constant return the constant. In practice, this means a true
7197 constant or the address of a non-weak symbol. */
7200 if (TREE_CODE (inner
) == ADDR_EXPR
)
7204 inner
= TREE_OPERAND (inner
, 0);
7206 while (TREE_CODE (inner
) == COMPONENT_REF
7207 || TREE_CODE (inner
) == ARRAY_REF
);
7208 if ((TREE_CODE (inner
) == VAR_DECL
7209 || TREE_CODE (inner
) == FUNCTION_DECL
)
7210 && DECL_WEAK (inner
))
7214 /* Otherwise, ARG0 already has the proper type for the return value. */
7218 /* Fold a call to __builtin_classify_type with argument ARG. */
7221 fold_builtin_classify_type (tree arg
)
7224 return build_int_cst (integer_type_node
, no_type_class
);
7226 return build_int_cst (integer_type_node
, type_to_class (TREE_TYPE (arg
)));
7229 /* Fold a call to __builtin_strlen with argument ARG. */
7232 fold_builtin_strlen (location_t loc
, tree type
, tree arg
)
7234 if (!validate_arg (arg
, POINTER_TYPE
))
7238 tree len
= c_strlen (arg
, 0);
7241 return fold_convert_loc (loc
, type
, len
);
7247 /* Fold a call to __builtin_inf or __builtin_huge_val. */
7250 fold_builtin_inf (location_t loc
, tree type
, int warn
)
7252 REAL_VALUE_TYPE real
;
7254 /* __builtin_inff is intended to be usable to define INFINITY on all
7255 targets. If an infinity is not available, INFINITY expands "to a
7256 positive constant of type float that overflows at translation
7257 time", footnote "In this case, using INFINITY will violate the
7258 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
7259 Thus we pedwarn to ensure this constraint violation is
7261 if (!MODE_HAS_INFINITIES (TYPE_MODE (type
)) && warn
)
7262 pedwarn (loc
, 0, "target format does not support infinity");
7265 return build_real (type
, real
);
7268 /* Fold a call to __builtin_nan or __builtin_nans with argument ARG. */
7271 fold_builtin_nan (tree arg
, tree type
, int quiet
)
7273 REAL_VALUE_TYPE real
;
7276 if (!validate_arg (arg
, POINTER_TYPE
))
7278 str
= c_getstr (arg
);
7282 if (!real_nan (&real
, str
, quiet
, TYPE_MODE (type
)))
7285 return build_real (type
, real
);
7288 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
7289 NULL_TREE if no simplification can be made. */
7292 fold_builtin_sincos (location_t loc
,
7293 tree arg0
, tree arg1
, tree arg2
)
7296 tree fndecl
, call
= NULL_TREE
;
7298 if (!validate_arg (arg0
, REAL_TYPE
)
7299 || !validate_arg (arg1
, POINTER_TYPE
)
7300 || !validate_arg (arg2
, POINTER_TYPE
))
7303 type
= TREE_TYPE (arg0
);
7305 /* Calculate the result when the argument is a constant. */
7306 built_in_function fn
= mathfn_built_in_2 (type
, BUILT_IN_CEXPI
);
7307 if (fn
== END_BUILTINS
)
7310 /* Canonicalize sincos to cexpi. */
7311 if (TREE_CODE (arg0
) == REAL_CST
)
7313 tree complex_type
= build_complex_type (type
);
7314 call
= fold_const_call (fn
, complex_type
, arg0
);
7318 if (!targetm
.libc_has_function (function_c99_math_complex
)
7319 || !builtin_decl_implicit_p (fn
))
7321 fndecl
= builtin_decl_explicit (fn
);
7322 call
= build_call_expr_loc (loc
, fndecl
, 1, arg0
);
7323 call
= builtin_save_expr (call
);
7326 return build2 (COMPOUND_EXPR
, void_type_node
,
7327 build2 (MODIFY_EXPR
, void_type_node
,
7328 build_fold_indirect_ref_loc (loc
, arg1
),
7329 fold_build1_loc (loc
, IMAGPART_EXPR
, type
, call
)),
7330 build2 (MODIFY_EXPR
, void_type_node
,
7331 build_fold_indirect_ref_loc (loc
, arg2
),
7332 fold_build1_loc (loc
, REALPART_EXPR
, type
, call
)));
7335 /* Fold function call to builtin ffs, clz, ctz, popcount and parity
7336 and their long and long long variants (i.e. ffsl and ffsll). ARG is
7337 the argument to the call. Return NULL_TREE if no simplification can
7341 fold_builtin_bitop (tree fndecl
, tree arg
)
7343 if (!validate_arg (arg
, INTEGER_TYPE
))
7346 /* Optimize for constant argument. */
7347 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
7349 tree type
= TREE_TYPE (arg
);
7352 switch (DECL_FUNCTION_CODE (fndecl
))
7354 CASE_INT_FN (BUILT_IN_FFS
):
7355 result
= wi::ffs (arg
);
7358 CASE_INT_FN (BUILT_IN_CLZ
):
7359 if (wi::ne_p (arg
, 0))
7360 result
= wi::clz (arg
);
7361 else if (! CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
7362 result
= TYPE_PRECISION (type
);
7365 CASE_INT_FN (BUILT_IN_CTZ
):
7366 if (wi::ne_p (arg
, 0))
7367 result
= wi::ctz (arg
);
7368 else if (! CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (type
), result
))
7369 result
= TYPE_PRECISION (type
);
7372 CASE_INT_FN (BUILT_IN_CLRSB
):
7373 result
= wi::clrsb (arg
);
7376 CASE_INT_FN (BUILT_IN_POPCOUNT
):
7377 result
= wi::popcount (arg
);
7380 CASE_INT_FN (BUILT_IN_PARITY
):
7381 result
= wi::parity (arg
);
7388 return build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), result
);
7394 /* Fold function call to builtin_bswap and the short, long and long long
7395 variants. Return NULL_TREE if no simplification can be made. */
7397 fold_builtin_bswap (tree fndecl
, tree arg
)
7399 if (! validate_arg (arg
, INTEGER_TYPE
))
7402 /* Optimize constant value. */
7403 if (TREE_CODE (arg
) == INTEGER_CST
&& !TREE_OVERFLOW (arg
))
7405 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
7407 switch (DECL_FUNCTION_CODE (fndecl
))
7409 case BUILT_IN_BSWAP16
:
7410 case BUILT_IN_BSWAP32
:
7411 case BUILT_IN_BSWAP64
:
7413 signop sgn
= TYPE_SIGN (type
);
7415 wide_int_to_tree (type
,
7416 wide_int::from (arg
, TYPE_PRECISION (type
),
7428 /* Fold function call to builtin memchr. ARG1, ARG2 and LEN are the
7429 arguments to the call, and TYPE is its return type.
7430 Return NULL_TREE if no simplification can be made. */
7433 fold_builtin_memchr (location_t loc
, tree arg1
, tree arg2
, tree len
, tree type
)
7435 if (!validate_arg (arg1
, POINTER_TYPE
)
7436 || !validate_arg (arg2
, INTEGER_TYPE
)
7437 || !validate_arg (len
, INTEGER_TYPE
))
7443 if (TREE_CODE (arg2
) != INTEGER_CST
7444 || !tree_fits_uhwi_p (len
))
7447 p1
= c_getstr (arg1
);
7448 if (p1
&& compare_tree_int (len
, strlen (p1
) + 1) <= 0)
7454 if (target_char_cast (arg2
, &c
))
7457 r
= (const char *) memchr (p1
, c
, tree_to_uhwi (len
));
7460 return build_int_cst (TREE_TYPE (arg1
), 0);
7462 tem
= fold_build_pointer_plus_hwi_loc (loc
, arg1
, r
- p1
);
7463 return fold_convert_loc (loc
, type
, tem
);
7469 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
7470 Return NULL_TREE if no simplification can be made. */
7473 fold_builtin_memcmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
7475 const char *p1
, *p2
;
7477 if (!validate_arg (arg1
, POINTER_TYPE
)
7478 || !validate_arg (arg2
, POINTER_TYPE
)
7479 || !validate_arg (len
, INTEGER_TYPE
))
7482 /* If the LEN parameter is zero, return zero. */
7483 if (integer_zerop (len
))
7484 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
7487 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7488 if (operand_equal_p (arg1
, arg2
, 0))
7489 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
7491 p1
= c_getstr (arg1
);
7492 p2
= c_getstr (arg2
);
7494 /* If all arguments are constant, and the value of len is not greater
7495 than the lengths of arg1 and arg2, evaluate at compile-time. */
7496 if (tree_fits_uhwi_p (len
) && p1
&& p2
7497 && compare_tree_int (len
, strlen (p1
) + 1) <= 0
7498 && compare_tree_int (len
, strlen (p2
) + 1) <= 0)
7500 const int r
= memcmp (p1
, p2
, tree_to_uhwi (len
));
7503 return integer_one_node
;
7505 return integer_minus_one_node
;
7507 return integer_zero_node
;
7510 /* If len parameter is one, return an expression corresponding to
7511 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7512 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
7514 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7515 tree cst_uchar_ptr_node
7516 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
7519 = fold_convert_loc (loc
, integer_type_node
,
7520 build1 (INDIRECT_REF
, cst_uchar_node
,
7521 fold_convert_loc (loc
,
7525 = fold_convert_loc (loc
, integer_type_node
,
7526 build1 (INDIRECT_REF
, cst_uchar_node
,
7527 fold_convert_loc (loc
,
7530 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
7536 /* Fold function call to builtin strcmp with arguments ARG1 and ARG2.
7537 Return NULL_TREE if no simplification can be made. */
7540 fold_builtin_strcmp (location_t loc
, tree arg1
, tree arg2
)
7542 const char *p1
, *p2
;
7544 if (!validate_arg (arg1
, POINTER_TYPE
)
7545 || !validate_arg (arg2
, POINTER_TYPE
))
7548 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7549 if (operand_equal_p (arg1
, arg2
, 0))
7550 return integer_zero_node
;
7552 p1
= c_getstr (arg1
);
7553 p2
= c_getstr (arg2
);
7557 const int i
= strcmp (p1
, p2
);
7559 return integer_minus_one_node
;
7561 return integer_one_node
;
7563 return integer_zero_node
;
7566 /* If the second arg is "", return *(const unsigned char*)arg1. */
7567 if (p2
&& *p2
== '\0')
7569 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7570 tree cst_uchar_ptr_node
7571 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
7573 return fold_convert_loc (loc
, integer_type_node
,
7574 build1 (INDIRECT_REF
, cst_uchar_node
,
7575 fold_convert_loc (loc
,
7580 /* If the first arg is "", return -*(const unsigned char*)arg2. */
7581 if (p1
&& *p1
== '\0')
7583 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7584 tree cst_uchar_ptr_node
7585 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
7588 = fold_convert_loc (loc
, integer_type_node
,
7589 build1 (INDIRECT_REF
, cst_uchar_node
,
7590 fold_convert_loc (loc
,
7593 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
7599 /* Fold function call to builtin strncmp with arguments ARG1, ARG2, and LEN.
7600 Return NULL_TREE if no simplification can be made. */
7603 fold_builtin_strncmp (location_t loc
, tree arg1
, tree arg2
, tree len
)
7605 const char *p1
, *p2
;
7607 if (!validate_arg (arg1
, POINTER_TYPE
)
7608 || !validate_arg (arg2
, POINTER_TYPE
)
7609 || !validate_arg (len
, INTEGER_TYPE
))
7612 /* If the LEN parameter is zero, return zero. */
7613 if (integer_zerop (len
))
7614 return omit_two_operands_loc (loc
, integer_type_node
, integer_zero_node
,
7617 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
7618 if (operand_equal_p (arg1
, arg2
, 0))
7619 return omit_one_operand_loc (loc
, integer_type_node
, integer_zero_node
, len
);
7621 p1
= c_getstr (arg1
);
7622 p2
= c_getstr (arg2
);
7624 if (tree_fits_uhwi_p (len
) && p1
&& p2
)
7626 const int i
= strncmp (p1
, p2
, tree_to_uhwi (len
));
7628 return integer_one_node
;
7630 return integer_minus_one_node
;
7632 return integer_zero_node
;
7635 /* If the second arg is "", and the length is greater than zero,
7636 return *(const unsigned char*)arg1. */
7637 if (p2
&& *p2
== '\0'
7638 && TREE_CODE (len
) == INTEGER_CST
7639 && tree_int_cst_sgn (len
) == 1)
7641 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7642 tree cst_uchar_ptr_node
7643 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
7645 return fold_convert_loc (loc
, integer_type_node
,
7646 build1 (INDIRECT_REF
, cst_uchar_node
,
7647 fold_convert_loc (loc
,
7652 /* If the first arg is "", and the length is greater than zero,
7653 return -*(const unsigned char*)arg2. */
7654 if (p1
&& *p1
== '\0'
7655 && TREE_CODE (len
) == INTEGER_CST
7656 && tree_int_cst_sgn (len
) == 1)
7658 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7659 tree cst_uchar_ptr_node
7660 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
7662 tree temp
= fold_convert_loc (loc
, integer_type_node
,
7663 build1 (INDIRECT_REF
, cst_uchar_node
,
7664 fold_convert_loc (loc
,
7667 return fold_build1_loc (loc
, NEGATE_EXPR
, integer_type_node
, temp
);
7670 /* If len parameter is one, return an expression corresponding to
7671 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
7672 if (tree_fits_uhwi_p (len
) && tree_to_uhwi (len
) == 1)
7674 tree cst_uchar_node
= build_type_variant (unsigned_char_type_node
, 1, 0);
7675 tree cst_uchar_ptr_node
7676 = build_pointer_type_for_mode (cst_uchar_node
, ptr_mode
, true);
7678 tree ind1
= fold_convert_loc (loc
, integer_type_node
,
7679 build1 (INDIRECT_REF
, cst_uchar_node
,
7680 fold_convert_loc (loc
,
7683 tree ind2
= fold_convert_loc (loc
, integer_type_node
,
7684 build1 (INDIRECT_REF
, cst_uchar_node
,
7685 fold_convert_loc (loc
,
7688 return fold_build2_loc (loc
, MINUS_EXPR
, integer_type_node
, ind1
, ind2
);
7694 /* Fold a call to builtin isascii with argument ARG. */
7697 fold_builtin_isascii (location_t loc
, tree arg
)
7699 if (!validate_arg (arg
, INTEGER_TYPE
))
7703 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
7704 arg
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, arg
,
7705 build_int_cst (integer_type_node
,
7706 ~ (unsigned HOST_WIDE_INT
) 0x7f));
7707 return fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
,
7708 arg
, integer_zero_node
);
7712 /* Fold a call to builtin toascii with argument ARG. */
7715 fold_builtin_toascii (location_t loc
, tree arg
)
7717 if (!validate_arg (arg
, INTEGER_TYPE
))
7720 /* Transform toascii(c) -> (c & 0x7f). */
7721 return fold_build2_loc (loc
, BIT_AND_EXPR
, integer_type_node
, arg
,
7722 build_int_cst (integer_type_node
, 0x7f));
7725 /* Fold a call to builtin isdigit with argument ARG. */
7728 fold_builtin_isdigit (location_t loc
, tree arg
)
7730 if (!validate_arg (arg
, INTEGER_TYPE
))
7734 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
7735 /* According to the C standard, isdigit is unaffected by locale.
7736 However, it definitely is affected by the target character set. */
7737 unsigned HOST_WIDE_INT target_digit0
7738 = lang_hooks
.to_target_charset ('0');
7740 if (target_digit0
== 0)
7743 arg
= fold_convert_loc (loc
, unsigned_type_node
, arg
);
7744 arg
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, arg
,
7745 build_int_cst (unsigned_type_node
, target_digit0
));
7746 return fold_build2_loc (loc
, LE_EXPR
, integer_type_node
, arg
,
7747 build_int_cst (unsigned_type_node
, 9));
7751 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
7754 fold_builtin_fabs (location_t loc
, tree arg
, tree type
)
7756 if (!validate_arg (arg
, REAL_TYPE
))
7759 arg
= fold_convert_loc (loc
, type
, arg
);
7760 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
7763 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
7766 fold_builtin_abs (location_t loc
, tree arg
, tree type
)
7768 if (!validate_arg (arg
, INTEGER_TYPE
))
7771 arg
= fold_convert_loc (loc
, type
, arg
);
7772 return fold_build1_loc (loc
, ABS_EXPR
, type
, arg
);
7775 /* Fold a call to fma, fmaf, or fmal with arguments ARG[012]. */
7778 fold_builtin_fma (location_t loc
, tree arg0
, tree arg1
, tree arg2
, tree type
)
7780 /* ??? Only expand to FMA_EXPR if it's directly supported. */
7781 if (validate_arg (arg0
, REAL_TYPE
)
7782 && validate_arg (arg1
, REAL_TYPE
)
7783 && validate_arg (arg2
, REAL_TYPE
)
7784 && optab_handler (fma_optab
, TYPE_MODE (type
)) != CODE_FOR_nothing
)
7785 return fold_build3_loc (loc
, FMA_EXPR
, type
, arg0
, arg1
, arg2
);
7790 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
7793 fold_builtin_carg (location_t loc
, tree arg
, tree type
)
7795 if (validate_arg (arg
, COMPLEX_TYPE
)
7796 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg
))) == REAL_TYPE
)
7798 tree atan2_fn
= mathfn_built_in (type
, BUILT_IN_ATAN2
);
7802 tree new_arg
= builtin_save_expr (arg
);
7803 tree r_arg
= fold_build1_loc (loc
, REALPART_EXPR
, type
, new_arg
);
7804 tree i_arg
= fold_build1_loc (loc
, IMAGPART_EXPR
, type
, new_arg
);
7805 return build_call_expr_loc (loc
, atan2_fn
, 2, i_arg
, r_arg
);
7812 /* Fold a call to builtin frexp, we can assume the base is 2. */
7815 fold_builtin_frexp (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
7817 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
7822 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
7825 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
7827 /* Proceed if a valid pointer type was passed in. */
7828 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == integer_type_node
)
7830 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
7836 /* For +-0, return (*exp = 0, +-0). */
7837 exp
= integer_zero_node
;
7842 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
7843 return omit_one_operand_loc (loc
, rettype
, arg0
, arg1
);
7846 /* Since the frexp function always expects base 2, and in
7847 GCC normalized significands are already in the range
7848 [0.5, 1.0), we have exactly what frexp wants. */
7849 REAL_VALUE_TYPE frac_rvt
= *value
;
7850 SET_REAL_EXP (&frac_rvt
, 0);
7851 frac
= build_real (rettype
, frac_rvt
);
7852 exp
= build_int_cst (integer_type_node
, REAL_EXP (value
));
7859 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
7860 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
, exp
);
7861 TREE_SIDE_EFFECTS (arg1
) = 1;
7862 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
, frac
);
7868 /* Fold a call to builtin modf. */
7871 fold_builtin_modf (location_t loc
, tree arg0
, tree arg1
, tree rettype
)
7873 if (! validate_arg (arg0
, REAL_TYPE
) || ! validate_arg (arg1
, POINTER_TYPE
))
7878 if (!(TREE_CODE (arg0
) == REAL_CST
&& ! TREE_OVERFLOW (arg0
)))
7881 arg1
= build_fold_indirect_ref_loc (loc
, arg1
);
7883 /* Proceed if a valid pointer type was passed in. */
7884 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1
)) == TYPE_MAIN_VARIANT (rettype
))
7886 const REAL_VALUE_TYPE
*const value
= TREE_REAL_CST_PTR (arg0
);
7887 REAL_VALUE_TYPE trunc
, frac
;
7893 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
7894 trunc
= frac
= *value
;
7897 /* For +-Inf, return (*arg1 = arg0, +-0). */
7899 frac
.sign
= value
->sign
;
7903 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
7904 real_trunc (&trunc
, VOIDmode
, value
);
7905 real_arithmetic (&frac
, MINUS_EXPR
, value
, &trunc
);
7906 /* If the original number was negative and already
7907 integral, then the fractional part is -0.0. */
7908 if (value
->sign
&& frac
.cl
== rvc_zero
)
7909 frac
.sign
= value
->sign
;
7913 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
7914 arg1
= fold_build2_loc (loc
, MODIFY_EXPR
, rettype
, arg1
,
7915 build_real (rettype
, trunc
));
7916 TREE_SIDE_EFFECTS (arg1
) = 1;
7917 return fold_build2_loc (loc
, COMPOUND_EXPR
, rettype
, arg1
,
7918 build_real (rettype
, frac
));
7924 /* Given a location LOC, an interclass builtin function decl FNDECL
7925 and its single argument ARG, return an folded expression computing
7926 the same, or NULL_TREE if we either couldn't or didn't want to fold
7927 (the latter happen if there's an RTL instruction available). */
7930 fold_builtin_interclass_mathfn (location_t loc
, tree fndecl
, tree arg
)
7934 if (!validate_arg (arg
, REAL_TYPE
))
7937 if (interclass_mathfn_icode (arg
, fndecl
) != CODE_FOR_nothing
)
7940 mode
= TYPE_MODE (TREE_TYPE (arg
));
7942 /* If there is no optab, try generic code. */
7943 switch (DECL_FUNCTION_CODE (fndecl
))
7947 CASE_FLT_FN (BUILT_IN_ISINF
):
7949 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
7950 tree
const isgr_fn
= builtin_decl_explicit (BUILT_IN_ISGREATER
);
7951 tree
const type
= TREE_TYPE (arg
);
7955 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
7956 real_from_string (&r
, buf
);
7957 result
= build_call_expr (isgr_fn
, 2,
7958 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
7959 build_real (type
, r
));
7962 CASE_FLT_FN (BUILT_IN_FINITE
):
7963 case BUILT_IN_ISFINITE
:
7965 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
7966 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
7967 tree
const type
= TREE_TYPE (arg
);
7971 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
7972 real_from_string (&r
, buf
);
7973 result
= build_call_expr (isle_fn
, 2,
7974 fold_build1_loc (loc
, ABS_EXPR
, type
, arg
),
7975 build_real (type
, r
));
7976 /*result = fold_build2_loc (loc, UNGT_EXPR,
7977 TREE_TYPE (TREE_TYPE (fndecl)),
7978 fold_build1_loc (loc, ABS_EXPR, type, arg),
7979 build_real (type, r));
7980 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
7981 TREE_TYPE (TREE_TYPE (fndecl)),
7985 case BUILT_IN_ISNORMAL
:
7987 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
7988 islessequal(fabs(x),DBL_MAX). */
7989 tree
const isle_fn
= builtin_decl_explicit (BUILT_IN_ISLESSEQUAL
);
7990 tree
const isge_fn
= builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL
);
7991 tree
const type
= TREE_TYPE (arg
);
7992 REAL_VALUE_TYPE rmax
, rmin
;
7995 get_max_float (REAL_MODE_FORMAT (mode
), buf
, sizeof (buf
));
7996 real_from_string (&rmax
, buf
);
7997 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
7998 real_from_string (&rmin
, buf
);
7999 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
8000 result
= build_call_expr (isle_fn
, 2, arg
,
8001 build_real (type
, rmax
));
8002 result
= fold_build2 (BIT_AND_EXPR
, integer_type_node
, result
,
8003 build_call_expr (isge_fn
, 2, arg
,
8004 build_real (type
, rmin
)));
8014 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8015 ARG is the argument for the call. */
8018 fold_builtin_classify (location_t loc
, tree fndecl
, tree arg
, int builtin_index
)
8020 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8023 if (!validate_arg (arg
, REAL_TYPE
))
8026 switch (builtin_index
)
8028 case BUILT_IN_ISINF
:
8029 if (!HONOR_INFINITIES (arg
))
8030 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
8032 if (TREE_CODE (arg
) == REAL_CST
)
8034 r
= TREE_REAL_CST (arg
);
8035 if (real_isinf (&r
))
8036 return real_compare (GT_EXPR
, &r
, &dconst0
)
8037 ? integer_one_node
: integer_minus_one_node
;
8039 return integer_zero_node
;
8044 case BUILT_IN_ISINF_SIGN
:
8046 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
8047 /* In a boolean context, GCC will fold the inner COND_EXPR to
8048 1. So e.g. "if (isinf_sign(x))" would be folded to just
8049 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
8050 tree signbit_fn
= mathfn_built_in_1 (TREE_TYPE (arg
), BUILT_IN_SIGNBIT
, 0);
8051 tree isinf_fn
= builtin_decl_explicit (BUILT_IN_ISINF
);
8052 tree tmp
= NULL_TREE
;
8054 arg
= builtin_save_expr (arg
);
8056 if (signbit_fn
&& isinf_fn
)
8058 tree signbit_call
= build_call_expr_loc (loc
, signbit_fn
, 1, arg
);
8059 tree isinf_call
= build_call_expr_loc (loc
, isinf_fn
, 1, arg
);
8061 signbit_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
8062 signbit_call
, integer_zero_node
);
8063 isinf_call
= fold_build2_loc (loc
, NE_EXPR
, integer_type_node
,
8064 isinf_call
, integer_zero_node
);
8066 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, signbit_call
,
8067 integer_minus_one_node
, integer_one_node
);
8068 tmp
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
8076 case BUILT_IN_ISFINITE
:
8077 if (!HONOR_NANS (arg
)
8078 && !HONOR_INFINITIES (arg
))
8079 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg
);
8081 if (TREE_CODE (arg
) == REAL_CST
)
8083 r
= TREE_REAL_CST (arg
);
8084 return real_isfinite (&r
) ? integer_one_node
: integer_zero_node
;
8089 case BUILT_IN_ISNAN
:
8090 if (!HONOR_NANS (arg
))
8091 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg
);
8093 if (TREE_CODE (arg
) == REAL_CST
)
8095 r
= TREE_REAL_CST (arg
);
8096 return real_isnan (&r
) ? integer_one_node
: integer_zero_node
;
8099 arg
= builtin_save_expr (arg
);
8100 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg
, arg
);
8107 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
8108 This builtin will generate code to return the appropriate floating
8109 point classification depending on the value of the floating point
8110 number passed in. The possible return values must be supplied as
8111 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
8112 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
8113 one floating point argument which is "type generic". */
8116 fold_builtin_fpclassify (location_t loc
, tree
*args
, int nargs
)
8118 tree fp_nan
, fp_infinite
, fp_normal
, fp_subnormal
, fp_zero
,
8119 arg
, type
, res
, tmp
;
8124 /* Verify the required arguments in the original call. */
8126 || !validate_arg (args
[0], INTEGER_TYPE
)
8127 || !validate_arg (args
[1], INTEGER_TYPE
)
8128 || !validate_arg (args
[2], INTEGER_TYPE
)
8129 || !validate_arg (args
[3], INTEGER_TYPE
)
8130 || !validate_arg (args
[4], INTEGER_TYPE
)
8131 || !validate_arg (args
[5], REAL_TYPE
))
8135 fp_infinite
= args
[1];
8136 fp_normal
= args
[2];
8137 fp_subnormal
= args
[3];
8140 type
= TREE_TYPE (arg
);
8141 mode
= TYPE_MODE (type
);
8142 arg
= builtin_save_expr (fold_build1_loc (loc
, ABS_EXPR
, type
, arg
));
8146 (fabs(x) == Inf ? FP_INFINITE :
8147 (fabs(x) >= DBL_MIN ? FP_NORMAL :
8148 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
8150 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
8151 build_real (type
, dconst0
));
8152 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
,
8153 tmp
, fp_zero
, fp_subnormal
);
8155 sprintf (buf
, "0x1p%d", REAL_MODE_FORMAT (mode
)->emin
- 1);
8156 real_from_string (&r
, buf
);
8157 tmp
= fold_build2_loc (loc
, GE_EXPR
, integer_type_node
,
8158 arg
, build_real (type
, r
));
8159 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, fp_normal
, res
);
8161 if (HONOR_INFINITIES (mode
))
8164 tmp
= fold_build2_loc (loc
, EQ_EXPR
, integer_type_node
, arg
,
8165 build_real (type
, r
));
8166 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
,
8170 if (HONOR_NANS (mode
))
8172 tmp
= fold_build2_loc (loc
, ORDERED_EXPR
, integer_type_node
, arg
, arg
);
8173 res
= fold_build3_loc (loc
, COND_EXPR
, integer_type_node
, tmp
, res
, fp_nan
);
8179 /* Fold a call to an unordered comparison function such as
8180 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8181 being called and ARG0 and ARG1 are the arguments for the call.
8182 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8183 the opposite of the desired result. UNORDERED_CODE is used
8184 for modes that can hold NaNs and ORDERED_CODE is used for
8188 fold_builtin_unordered_cmp (location_t loc
, tree fndecl
, tree arg0
, tree arg1
,
8189 enum tree_code unordered_code
,
8190 enum tree_code ordered_code
)
8192 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8193 enum tree_code code
;
8195 enum tree_code code0
, code1
;
8196 tree cmp_type
= NULL_TREE
;
8198 type0
= TREE_TYPE (arg0
);
8199 type1
= TREE_TYPE (arg1
);
8201 code0
= TREE_CODE (type0
);
8202 code1
= TREE_CODE (type1
);
8204 if (code0
== REAL_TYPE
&& code1
== REAL_TYPE
)
8205 /* Choose the wider of two real types. */
8206 cmp_type
= TYPE_PRECISION (type0
) >= TYPE_PRECISION (type1
)
8208 else if (code0
== REAL_TYPE
&& code1
== INTEGER_TYPE
)
8210 else if (code0
== INTEGER_TYPE
&& code1
== REAL_TYPE
)
8213 arg0
= fold_convert_loc (loc
, cmp_type
, arg0
);
8214 arg1
= fold_convert_loc (loc
, cmp_type
, arg1
);
8216 if (unordered_code
== UNORDERED_EXPR
)
8218 if (!HONOR_NANS (arg0
))
8219 return omit_two_operands_loc (loc
, type
, integer_zero_node
, arg0
, arg1
);
8220 return fold_build2_loc (loc
, UNORDERED_EXPR
, type
, arg0
, arg1
);
8223 code
= HONOR_NANS (arg0
) ? unordered_code
: ordered_code
;
8224 return fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
,
8225 fold_build2_loc (loc
, code
, type
, arg0
, arg1
));
8228 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
8229 arithmetics if it can never overflow, or into internal functions that
8230 return both result of arithmetics and overflowed boolean flag in
8231 a complex integer result, or some other check for overflow. */
8234 fold_builtin_arith_overflow (location_t loc
, enum built_in_function fcode
,
8235 tree arg0
, tree arg1
, tree arg2
)
8237 enum internal_fn ifn
= IFN_LAST
;
8238 tree type
= TREE_TYPE (TREE_TYPE (arg2
));
8239 tree mem_arg2
= build_fold_indirect_ref_loc (loc
, arg2
);
8242 case BUILT_IN_ADD_OVERFLOW
:
8243 case BUILT_IN_SADD_OVERFLOW
:
8244 case BUILT_IN_SADDL_OVERFLOW
:
8245 case BUILT_IN_SADDLL_OVERFLOW
:
8246 case BUILT_IN_UADD_OVERFLOW
:
8247 case BUILT_IN_UADDL_OVERFLOW
:
8248 case BUILT_IN_UADDLL_OVERFLOW
:
8249 ifn
= IFN_ADD_OVERFLOW
;
8251 case BUILT_IN_SUB_OVERFLOW
:
8252 case BUILT_IN_SSUB_OVERFLOW
:
8253 case BUILT_IN_SSUBL_OVERFLOW
:
8254 case BUILT_IN_SSUBLL_OVERFLOW
:
8255 case BUILT_IN_USUB_OVERFLOW
:
8256 case BUILT_IN_USUBL_OVERFLOW
:
8257 case BUILT_IN_USUBLL_OVERFLOW
:
8258 ifn
= IFN_SUB_OVERFLOW
;
8260 case BUILT_IN_MUL_OVERFLOW
:
8261 case BUILT_IN_SMUL_OVERFLOW
:
8262 case BUILT_IN_SMULL_OVERFLOW
:
8263 case BUILT_IN_SMULLL_OVERFLOW
:
8264 case BUILT_IN_UMUL_OVERFLOW
:
8265 case BUILT_IN_UMULL_OVERFLOW
:
8266 case BUILT_IN_UMULLL_OVERFLOW
:
8267 ifn
= IFN_MUL_OVERFLOW
;
8272 tree ctype
= build_complex_type (type
);
8273 tree call
= build_call_expr_internal_loc (loc
, ifn
, ctype
,
8275 tree tgt
= save_expr (call
);
8276 tree intres
= build1_loc (loc
, REALPART_EXPR
, type
, tgt
);
8277 tree ovfres
= build1_loc (loc
, IMAGPART_EXPR
, type
, tgt
);
8278 ovfres
= fold_convert_loc (loc
, boolean_type_node
, ovfres
);
8280 = fold_build2_loc (loc
, MODIFY_EXPR
, void_type_node
, mem_arg2
, intres
);
8281 return build2_loc (loc
, COMPOUND_EXPR
, boolean_type_node
, store
, ovfres
);
8284 /* Fold a call to built-in function FNDECL with 0 arguments.
8285 This function returns NULL_TREE if no simplification was possible. */
8288 fold_builtin_0 (location_t loc
, tree fndecl
)
8290 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8291 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
8294 CASE_FLT_FN (BUILT_IN_INF
):
8295 case BUILT_IN_INFD32
:
8296 case BUILT_IN_INFD64
:
8297 case BUILT_IN_INFD128
:
8298 return fold_builtin_inf (loc
, type
, true);
8300 CASE_FLT_FN (BUILT_IN_HUGE_VAL
):
8301 return fold_builtin_inf (loc
, type
, false);
8303 case BUILT_IN_CLASSIFY_TYPE
:
8304 return fold_builtin_classify_type (NULL_TREE
);
8312 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
8313 This function returns NULL_TREE if no simplification was possible. */
8316 fold_builtin_1 (location_t loc
, tree fndecl
, tree arg0
)
8318 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8319 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
8321 if (TREE_CODE (arg0
) == ERROR_MARK
)
8324 if (tree ret
= fold_const_call (fcode
, type
, arg0
))
8329 case BUILT_IN_CONSTANT_P
:
8331 tree val
= fold_builtin_constant_p (arg0
);
8333 /* Gimplification will pull the CALL_EXPR for the builtin out of
8334 an if condition. When not optimizing, we'll not CSE it back.
8335 To avoid link error types of regressions, return false now. */
8336 if (!val
&& !optimize
)
8337 val
= integer_zero_node
;
8342 case BUILT_IN_CLASSIFY_TYPE
:
8343 return fold_builtin_classify_type (arg0
);
8345 case BUILT_IN_STRLEN
:
8346 return fold_builtin_strlen (loc
, type
, arg0
);
8348 CASE_FLT_FN (BUILT_IN_FABS
):
8349 case BUILT_IN_FABSD32
:
8350 case BUILT_IN_FABSD64
:
8351 case BUILT_IN_FABSD128
:
8352 return fold_builtin_fabs (loc
, arg0
, type
);
8356 case BUILT_IN_LLABS
:
8357 case BUILT_IN_IMAXABS
:
8358 return fold_builtin_abs (loc
, arg0
, type
);
8360 CASE_FLT_FN (BUILT_IN_CONJ
):
8361 if (validate_arg (arg0
, COMPLEX_TYPE
)
8362 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
8363 return fold_build1_loc (loc
, CONJ_EXPR
, type
, arg0
);
8366 CASE_FLT_FN (BUILT_IN_CREAL
):
8367 if (validate_arg (arg0
, COMPLEX_TYPE
)
8368 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
8369 return non_lvalue_loc (loc
, fold_build1_loc (loc
, REALPART_EXPR
, type
, arg0
));
8372 CASE_FLT_FN (BUILT_IN_CIMAG
):
8373 if (validate_arg (arg0
, COMPLEX_TYPE
)
8374 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
)
8375 return non_lvalue_loc (loc
, fold_build1_loc (loc
, IMAGPART_EXPR
, type
, arg0
));
8378 CASE_FLT_FN (BUILT_IN_CARG
):
8379 return fold_builtin_carg (loc
, arg0
, type
);
8381 CASE_FLT_FN (BUILT_IN_NAN
):
8382 case BUILT_IN_NAND32
:
8383 case BUILT_IN_NAND64
:
8384 case BUILT_IN_NAND128
:
8385 return fold_builtin_nan (arg0
, type
, true);
8387 CASE_FLT_FN (BUILT_IN_NANS
):
8388 return fold_builtin_nan (arg0
, type
, false);
8390 case BUILT_IN_BSWAP16
:
8391 case BUILT_IN_BSWAP32
:
8392 case BUILT_IN_BSWAP64
:
8393 return fold_builtin_bswap (fndecl
, arg0
);
8395 CASE_INT_FN (BUILT_IN_FFS
):
8396 CASE_INT_FN (BUILT_IN_CLZ
):
8397 CASE_INT_FN (BUILT_IN_CTZ
):
8398 CASE_INT_FN (BUILT_IN_CLRSB
):
8399 CASE_INT_FN (BUILT_IN_POPCOUNT
):
8400 CASE_INT_FN (BUILT_IN_PARITY
):
8401 return fold_builtin_bitop (fndecl
, arg0
);
8403 case BUILT_IN_ISASCII
:
8404 return fold_builtin_isascii (loc
, arg0
);
8406 case BUILT_IN_TOASCII
:
8407 return fold_builtin_toascii (loc
, arg0
);
8409 case BUILT_IN_ISDIGIT
:
8410 return fold_builtin_isdigit (loc
, arg0
);
8412 CASE_FLT_FN (BUILT_IN_FINITE
):
8413 case BUILT_IN_FINITED32
:
8414 case BUILT_IN_FINITED64
:
8415 case BUILT_IN_FINITED128
:
8416 case BUILT_IN_ISFINITE
:
8418 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISFINITE
);
8421 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
8424 CASE_FLT_FN (BUILT_IN_ISINF
):
8425 case BUILT_IN_ISINFD32
:
8426 case BUILT_IN_ISINFD64
:
8427 case BUILT_IN_ISINFD128
:
8429 tree ret
= fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF
);
8432 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
8435 case BUILT_IN_ISNORMAL
:
8436 return fold_builtin_interclass_mathfn (loc
, fndecl
, arg0
);
8438 case BUILT_IN_ISINF_SIGN
:
8439 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISINF_SIGN
);
8441 CASE_FLT_FN (BUILT_IN_ISNAN
):
8442 case BUILT_IN_ISNAND32
:
8443 case BUILT_IN_ISNAND64
:
8444 case BUILT_IN_ISNAND128
:
8445 return fold_builtin_classify (loc
, fndecl
, arg0
, BUILT_IN_ISNAN
);
8448 if (integer_zerop (arg0
))
8449 return build_empty_stmt (loc
);
8460 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
8461 This function returns NULL_TREE if no simplification was possible. */
8464 fold_builtin_2 (location_t loc
, tree fndecl
, tree arg0
, tree arg1
)
8466 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8467 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
8469 if (TREE_CODE (arg0
) == ERROR_MARK
8470 || TREE_CODE (arg1
) == ERROR_MARK
)
8473 if (tree ret
= fold_const_call (fcode
, type
, arg0
, arg1
))
8478 CASE_FLT_FN_REENT (BUILT_IN_GAMMA
): /* GAMMA_R */
8479 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA
): /* LGAMMA_R */
8480 if (validate_arg (arg0
, REAL_TYPE
)
8481 && validate_arg (arg1
, POINTER_TYPE
))
8482 return do_mpfr_lgamma_r (arg0
, arg1
, type
);
8485 CASE_FLT_FN (BUILT_IN_FREXP
):
8486 return fold_builtin_frexp (loc
, arg0
, arg1
, type
);
8488 CASE_FLT_FN (BUILT_IN_MODF
):
8489 return fold_builtin_modf (loc
, arg0
, arg1
, type
);
8491 case BUILT_IN_STRSTR
:
8492 return fold_builtin_strstr (loc
, arg0
, arg1
, type
);
8494 case BUILT_IN_STRSPN
:
8495 return fold_builtin_strspn (loc
, arg0
, arg1
);
8497 case BUILT_IN_STRCSPN
:
8498 return fold_builtin_strcspn (loc
, arg0
, arg1
);
8500 case BUILT_IN_STRCHR
:
8501 case BUILT_IN_INDEX
:
8502 return fold_builtin_strchr (loc
, arg0
, arg1
, type
);
8504 case BUILT_IN_STRRCHR
:
8505 case BUILT_IN_RINDEX
:
8506 return fold_builtin_strrchr (loc
, arg0
, arg1
, type
);
8508 case BUILT_IN_STRCMP
:
8509 return fold_builtin_strcmp (loc
, arg0
, arg1
);
8511 case BUILT_IN_STRPBRK
:
8512 return fold_builtin_strpbrk (loc
, arg0
, arg1
, type
);
8514 case BUILT_IN_EXPECT
:
8515 return fold_builtin_expect (loc
, arg0
, arg1
, NULL_TREE
);
8517 case BUILT_IN_ISGREATER
:
8518 return fold_builtin_unordered_cmp (loc
, fndecl
,
8519 arg0
, arg1
, UNLE_EXPR
, LE_EXPR
);
8520 case BUILT_IN_ISGREATEREQUAL
:
8521 return fold_builtin_unordered_cmp (loc
, fndecl
,
8522 arg0
, arg1
, UNLT_EXPR
, LT_EXPR
);
8523 case BUILT_IN_ISLESS
:
8524 return fold_builtin_unordered_cmp (loc
, fndecl
,
8525 arg0
, arg1
, UNGE_EXPR
, GE_EXPR
);
8526 case BUILT_IN_ISLESSEQUAL
:
8527 return fold_builtin_unordered_cmp (loc
, fndecl
,
8528 arg0
, arg1
, UNGT_EXPR
, GT_EXPR
);
8529 case BUILT_IN_ISLESSGREATER
:
8530 return fold_builtin_unordered_cmp (loc
, fndecl
,
8531 arg0
, arg1
, UNEQ_EXPR
, EQ_EXPR
);
8532 case BUILT_IN_ISUNORDERED
:
8533 return fold_builtin_unordered_cmp (loc
, fndecl
,
8534 arg0
, arg1
, UNORDERED_EXPR
,
8537 /* We do the folding for va_start in the expander. */
8538 case BUILT_IN_VA_START
:
8541 case BUILT_IN_OBJECT_SIZE
:
8542 return fold_builtin_object_size (arg0
, arg1
);
8544 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE
:
8545 return fold_builtin_atomic_always_lock_free (arg0
, arg1
);
8547 case BUILT_IN_ATOMIC_IS_LOCK_FREE
:
8548 return fold_builtin_atomic_is_lock_free (arg0
, arg1
);
8556 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
8558 This function returns NULL_TREE if no simplification was possible. */
8561 fold_builtin_3 (location_t loc
, tree fndecl
,
8562 tree arg0
, tree arg1
, tree arg2
)
8564 tree type
= TREE_TYPE (TREE_TYPE (fndecl
));
8565 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
8567 if (TREE_CODE (arg0
) == ERROR_MARK
8568 || TREE_CODE (arg1
) == ERROR_MARK
8569 || TREE_CODE (arg2
) == ERROR_MARK
)
8572 if (tree ret
= fold_const_call (fcode
, type
, arg0
, arg1
, arg2
))
8578 CASE_FLT_FN (BUILT_IN_SINCOS
):
8579 return fold_builtin_sincos (loc
, arg0
, arg1
, arg2
);
8581 CASE_FLT_FN (BUILT_IN_FMA
):
8582 return fold_builtin_fma (loc
, arg0
, arg1
, arg2
, type
);
8584 CASE_FLT_FN (BUILT_IN_REMQUO
):
8585 if (validate_arg (arg0
, REAL_TYPE
)
8586 && validate_arg (arg1
, REAL_TYPE
)
8587 && validate_arg (arg2
, POINTER_TYPE
))
8588 return do_mpfr_remquo (arg0
, arg1
, arg2
);
8591 case BUILT_IN_STRNCMP
:
8592 return fold_builtin_strncmp (loc
, arg0
, arg1
, arg2
);
8594 case BUILT_IN_MEMCHR
:
8595 return fold_builtin_memchr (loc
, arg0
, arg1
, arg2
, type
);
8598 case BUILT_IN_MEMCMP
:
8599 return fold_builtin_memcmp (loc
, arg0
, arg1
, arg2
);;
8601 case BUILT_IN_EXPECT
:
8602 return fold_builtin_expect (loc
, arg0
, arg1
, arg2
);
8604 case BUILT_IN_ADD_OVERFLOW
:
8605 case BUILT_IN_SUB_OVERFLOW
:
8606 case BUILT_IN_MUL_OVERFLOW
:
8607 case BUILT_IN_SADD_OVERFLOW
:
8608 case BUILT_IN_SADDL_OVERFLOW
:
8609 case BUILT_IN_SADDLL_OVERFLOW
:
8610 case BUILT_IN_SSUB_OVERFLOW
:
8611 case BUILT_IN_SSUBL_OVERFLOW
:
8612 case BUILT_IN_SSUBLL_OVERFLOW
:
8613 case BUILT_IN_SMUL_OVERFLOW
:
8614 case BUILT_IN_SMULL_OVERFLOW
:
8615 case BUILT_IN_SMULLL_OVERFLOW
:
8616 case BUILT_IN_UADD_OVERFLOW
:
8617 case BUILT_IN_UADDL_OVERFLOW
:
8618 case BUILT_IN_UADDLL_OVERFLOW
:
8619 case BUILT_IN_USUB_OVERFLOW
:
8620 case BUILT_IN_USUBL_OVERFLOW
:
8621 case BUILT_IN_USUBLL_OVERFLOW
:
8622 case BUILT_IN_UMUL_OVERFLOW
:
8623 case BUILT_IN_UMULL_OVERFLOW
:
8624 case BUILT_IN_UMULLL_OVERFLOW
:
8625 return fold_builtin_arith_overflow (loc
, fcode
, arg0
, arg1
, arg2
);
8633 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
8634 arguments. IGNORE is true if the result of the
8635 function call is ignored. This function returns NULL_TREE if no
8636 simplification was possible. */
8639 fold_builtin_n (location_t loc
, tree fndecl
, tree
*args
, int nargs
, bool)
8641 tree ret
= NULL_TREE
;
8646 ret
= fold_builtin_0 (loc
, fndecl
);
8649 ret
= fold_builtin_1 (loc
, fndecl
, args
[0]);
8652 ret
= fold_builtin_2 (loc
, fndecl
, args
[0], args
[1]);
8655 ret
= fold_builtin_3 (loc
, fndecl
, args
[0], args
[1], args
[2]);
8658 ret
= fold_builtin_varargs (loc
, fndecl
, args
, nargs
);
8663 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
8664 SET_EXPR_LOCATION (ret
, loc
);
8665 TREE_NO_WARNING (ret
) = 1;
8671 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
8672 list ARGS along with N new arguments in NEWARGS. SKIP is the number
8673 of arguments in ARGS to be omitted. OLDNARGS is the number of
8674 elements in ARGS. */
8677 rewrite_call_expr_valist (location_t loc
, int oldnargs
, tree
*args
,
8678 int skip
, tree fndecl
, int n
, va_list newargs
)
8680 int nargs
= oldnargs
- skip
+ n
;
8687 buffer
= XALLOCAVEC (tree
, nargs
);
8688 for (i
= 0; i
< n
; i
++)
8689 buffer
[i
] = va_arg (newargs
, tree
);
8690 for (j
= skip
; j
< oldnargs
; j
++, i
++)
8691 buffer
[i
] = args
[j
];
8694 buffer
= args
+ skip
;
8696 return build_call_expr_loc_array (loc
, fndecl
, nargs
, buffer
);
8699 /* Return true if FNDECL shouldn't be folded right now.
8700 If a built-in function has an inline attribute always_inline
8701 wrapper, defer folding it after always_inline functions have
8702 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
8703 might not be performed. */
8706 avoid_folding_inline_builtin (tree fndecl
)
8708 return (DECL_DECLARED_INLINE_P (fndecl
)
8709 && DECL_DISREGARD_INLINE_LIMITS (fndecl
)
8711 && !cfun
->always_inline_functions_inlined
8712 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl
)));
8715 /* A wrapper function for builtin folding that prevents warnings for
8716 "statement without effect" and the like, caused by removing the
8717 call node earlier than the warning is generated. */
8720 fold_call_expr (location_t loc
, tree exp
, bool ignore
)
8722 tree ret
= NULL_TREE
;
8723 tree fndecl
= get_callee_fndecl (exp
);
8725 && TREE_CODE (fndecl
) == FUNCTION_DECL
8726 && DECL_BUILT_IN (fndecl
)
8727 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
8728 yet. Defer folding until we see all the arguments
8729 (after inlining). */
8730 && !CALL_EXPR_VA_ARG_PACK (exp
))
8732 int nargs
= call_expr_nargs (exp
);
8734 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
8735 instead last argument is __builtin_va_arg_pack (). Defer folding
8736 even in that case, until arguments are finalized. */
8737 if (nargs
&& TREE_CODE (CALL_EXPR_ARG (exp
, nargs
- 1)) == CALL_EXPR
)
8739 tree fndecl2
= get_callee_fndecl (CALL_EXPR_ARG (exp
, nargs
- 1));
8741 && TREE_CODE (fndecl2
) == FUNCTION_DECL
8742 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
8743 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
8747 if (avoid_folding_inline_builtin (fndecl
))
8750 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
8751 return targetm
.fold_builtin (fndecl
, call_expr_nargs (exp
),
8752 CALL_EXPR_ARGP (exp
), ignore
);
8755 tree
*args
= CALL_EXPR_ARGP (exp
);
8756 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
8764 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
8765 N arguments are passed in the array ARGARRAY. Return a folded
8766 expression or NULL_TREE if no simplification was possible. */
8769 fold_builtin_call_array (location_t loc
, tree
,
8774 if (TREE_CODE (fn
) != ADDR_EXPR
)
8777 tree fndecl
= TREE_OPERAND (fn
, 0);
8778 if (TREE_CODE (fndecl
) == FUNCTION_DECL
8779 && DECL_BUILT_IN (fndecl
))
8781 /* If last argument is __builtin_va_arg_pack (), arguments to this
8782 function are not finalized yet. Defer folding until they are. */
8783 if (n
&& TREE_CODE (argarray
[n
- 1]) == CALL_EXPR
)
8785 tree fndecl2
= get_callee_fndecl (argarray
[n
- 1]);
8787 && TREE_CODE (fndecl2
) == FUNCTION_DECL
8788 && DECL_BUILT_IN_CLASS (fndecl2
) == BUILT_IN_NORMAL
8789 && DECL_FUNCTION_CODE (fndecl2
) == BUILT_IN_VA_ARG_PACK
)
8792 if (avoid_folding_inline_builtin (fndecl
))
8794 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
8795 return targetm
.fold_builtin (fndecl
, n
, argarray
, false);
8797 return fold_builtin_n (loc
, fndecl
, argarray
, n
, false);
8803 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
8804 along with N new arguments specified as the "..." parameters. SKIP
8805 is the number of arguments in EXP to be omitted. This function is used
8806 to do varargs-to-varargs transformations. */
8809 rewrite_call_expr (location_t loc
, tree exp
, int skip
, tree fndecl
, int n
, ...)
8815 t
= rewrite_call_expr_valist (loc
, call_expr_nargs (exp
),
8816 CALL_EXPR_ARGP (exp
), skip
, fndecl
, n
, ap
);
8822 /* Validate a single argument ARG against a tree code CODE representing
8826 validate_arg (const_tree arg
, enum tree_code code
)
8830 else if (code
== POINTER_TYPE
)
8831 return POINTER_TYPE_P (TREE_TYPE (arg
));
8832 else if (code
== INTEGER_TYPE
)
8833 return INTEGRAL_TYPE_P (TREE_TYPE (arg
));
8834 return code
== TREE_CODE (TREE_TYPE (arg
));
8837 /* This function validates the types of a function call argument list
8838 against a specified list of tree_codes. If the last specifier is a 0,
8839 that represents an ellipses, otherwise the last specifier must be a
8842 This is the GIMPLE version of validate_arglist. Eventually we want to
8843 completely convert builtins.c to work from GIMPLEs and the tree based
8844 validate_arglist will then be removed. */
8847 validate_gimple_arglist (const gcall
*call
, ...)
8849 enum tree_code code
;
8855 va_start (ap
, call
);
8860 code
= (enum tree_code
) va_arg (ap
, int);
8864 /* This signifies an ellipses, any further arguments are all ok. */
8868 /* This signifies an endlink, if no arguments remain, return
8869 true, otherwise return false. */
8870 res
= (i
== gimple_call_num_args (call
));
8873 /* If no parameters remain or the parameter's code does not
8874 match the specified code, return false. Otherwise continue
8875 checking any remaining arguments. */
8876 arg
= gimple_call_arg (call
, i
++);
8877 if (!validate_arg (arg
, code
))
8884 /* We need gotos here since we can only have one VA_CLOSE in a
8892 /* Default target-specific builtin expander that does nothing. */
8895 default_expand_builtin (tree exp ATTRIBUTE_UNUSED
,
8896 rtx target ATTRIBUTE_UNUSED
,
8897 rtx subtarget ATTRIBUTE_UNUSED
,
8898 machine_mode mode ATTRIBUTE_UNUSED
,
8899 int ignore ATTRIBUTE_UNUSED
)
8904 /* Returns true is EXP represents data that would potentially reside
8905 in a readonly section. */
8908 readonly_data_expr (tree exp
)
8912 if (TREE_CODE (exp
) != ADDR_EXPR
)
8915 exp
= get_base_address (TREE_OPERAND (exp
, 0));
8919 /* Make sure we call decl_readonly_section only for trees it
8920 can handle (since it returns true for everything it doesn't
8922 if (TREE_CODE (exp
) == STRING_CST
8923 || TREE_CODE (exp
) == CONSTRUCTOR
8924 || (TREE_CODE (exp
) == VAR_DECL
&& TREE_STATIC (exp
)))
8925 return decl_readonly_section (exp
, 0);
8930 /* Simplify a call to the strstr builtin. S1 and S2 are the arguments
8931 to the call, and TYPE is its return type.
8933 Return NULL_TREE if no simplification was possible, otherwise return the
8934 simplified form of the call as a tree.
8936 The simplified form may be a constant or other expression which
8937 computes the same value, but in a more efficient manner (including
8938 calls to other builtin functions).
8940 The call may contain arguments which need to be evaluated, but
8941 which are not useful to determine the result of the call. In
8942 this case we return a chain of COMPOUND_EXPRs. The LHS of each
8943 COMPOUND_EXPR will be an argument which must be evaluated.
8944 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
8945 COMPOUND_EXPR in the chain will contain the tree for the simplified
8946 form of the builtin function call. */
8949 fold_builtin_strstr (location_t loc
, tree s1
, tree s2
, tree type
)
8951 if (!validate_arg (s1
, POINTER_TYPE
)
8952 || !validate_arg (s2
, POINTER_TYPE
))
8957 const char *p1
, *p2
;
8966 const char *r
= strstr (p1
, p2
);
8970 return build_int_cst (TREE_TYPE (s1
), 0);
8972 /* Return an offset into the constant string argument. */
8973 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
8974 return fold_convert_loc (loc
, type
, tem
);
8977 /* The argument is const char *, and the result is char *, so we need
8978 a type conversion here to avoid a warning. */
8980 return fold_convert_loc (loc
, type
, s1
);
8985 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
8989 /* New argument list transforming strstr(s1, s2) to
8990 strchr(s1, s2[0]). */
8991 return build_call_expr_loc (loc
, fn
, 2, s1
,
8992 build_int_cst (integer_type_node
, p2
[0]));
8996 /* Simplify a call to the strchr builtin. S1 and S2 are the arguments to
8997 the call, and TYPE is its return type.
8999 Return NULL_TREE if no simplification was possible, otherwise return the
9000 simplified form of the call as a tree.
9002 The simplified form may be a constant or other expression which
9003 computes the same value, but in a more efficient manner (including
9004 calls to other builtin functions).
9006 The call may contain arguments which need to be evaluated, but
9007 which are not useful to determine the result of the call. In
9008 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9009 COMPOUND_EXPR will be an argument which must be evaluated.
9010 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9011 COMPOUND_EXPR in the chain will contain the tree for the simplified
9012 form of the builtin function call. */
9015 fold_builtin_strchr (location_t loc
, tree s1
, tree s2
, tree type
)
9017 if (!validate_arg (s1
, POINTER_TYPE
)
9018 || !validate_arg (s2
, INTEGER_TYPE
))
9024 if (TREE_CODE (s2
) != INTEGER_CST
)
9034 if (target_char_cast (s2
, &c
))
9040 return build_int_cst (TREE_TYPE (s1
), 0);
9042 /* Return an offset into the constant string argument. */
9043 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
9044 return fold_convert_loc (loc
, type
, tem
);
9050 /* Simplify a call to the strrchr builtin. S1 and S2 are the arguments to
9051 the call, and TYPE is its return type.
9053 Return NULL_TREE if no simplification was possible, otherwise return the
9054 simplified form of the call as a tree.
9056 The simplified form may be a constant or other expression which
9057 computes the same value, but in a more efficient manner (including
9058 calls to other builtin functions).
9060 The call may contain arguments which need to be evaluated, but
9061 which are not useful to determine the result of the call. In
9062 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9063 COMPOUND_EXPR will be an argument which must be evaluated.
9064 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9065 COMPOUND_EXPR in the chain will contain the tree for the simplified
9066 form of the builtin function call. */
9069 fold_builtin_strrchr (location_t loc
, tree s1
, tree s2
, tree type
)
9071 if (!validate_arg (s1
, POINTER_TYPE
)
9072 || !validate_arg (s2
, INTEGER_TYPE
))
9079 if (TREE_CODE (s2
) != INTEGER_CST
)
9089 if (target_char_cast (s2
, &c
))
9092 r
= strrchr (p1
, c
);
9095 return build_int_cst (TREE_TYPE (s1
), 0);
9097 /* Return an offset into the constant string argument. */
9098 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
9099 return fold_convert_loc (loc
, type
, tem
);
9102 if (! integer_zerop (s2
))
9105 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
9109 /* Transform strrchr(s1, '\0') to strchr(s1, '\0'). */
9110 return build_call_expr_loc (loc
, fn
, 2, s1
, s2
);
9114 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
9115 to the call, and TYPE is its return type.
9117 Return NULL_TREE if no simplification was possible, otherwise return the
9118 simplified form of the call as a tree.
9120 The simplified form may be a constant or other expression which
9121 computes the same value, but in a more efficient manner (including
9122 calls to other builtin functions).
9124 The call may contain arguments which need to be evaluated, but
9125 which are not useful to determine the result of the call. In
9126 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9127 COMPOUND_EXPR will be an argument which must be evaluated.
9128 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9129 COMPOUND_EXPR in the chain will contain the tree for the simplified
9130 form of the builtin function call. */
9133 fold_builtin_strpbrk (location_t loc
, tree s1
, tree s2
, tree type
)
9135 if (!validate_arg (s1
, POINTER_TYPE
)
9136 || !validate_arg (s2
, POINTER_TYPE
))
9141 const char *p1
, *p2
;
9150 const char *r
= strpbrk (p1
, p2
);
9154 return build_int_cst (TREE_TYPE (s1
), 0);
9156 /* Return an offset into the constant string argument. */
9157 tem
= fold_build_pointer_plus_hwi_loc (loc
, s1
, r
- p1
);
9158 return fold_convert_loc (loc
, type
, tem
);
9162 /* strpbrk(x, "") == NULL.
9163 Evaluate and ignore s1 in case it had side-effects. */
9164 return omit_one_operand_loc (loc
, TREE_TYPE (s1
), integer_zero_node
, s1
);
9167 return NULL_TREE
; /* Really call strpbrk. */
9169 fn
= builtin_decl_implicit (BUILT_IN_STRCHR
);
9173 /* New argument list transforming strpbrk(s1, s2) to
9174 strchr(s1, s2[0]). */
9175 return build_call_expr_loc (loc
, fn
, 2, s1
,
9176 build_int_cst (integer_type_node
, p2
[0]));
9180 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
9183 Return NULL_TREE if no simplification was possible, otherwise return the
9184 simplified form of the call as a tree.
9186 The simplified form may be a constant or other expression which
9187 computes the same value, but in a more efficient manner (including
9188 calls to other builtin functions).
9190 The call may contain arguments which need to be evaluated, but
9191 which are not useful to determine the result of the call. In
9192 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9193 COMPOUND_EXPR will be an argument which must be evaluated.
9194 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9195 COMPOUND_EXPR in the chain will contain the tree for the simplified
9196 form of the builtin function call. */
9199 fold_builtin_strspn (location_t loc
, tree s1
, tree s2
)
9201 if (!validate_arg (s1
, POINTER_TYPE
)
9202 || !validate_arg (s2
, POINTER_TYPE
))
9206 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
9208 /* If both arguments are constants, evaluate at compile-time. */
9211 const size_t r
= strspn (p1
, p2
);
9212 return build_int_cst (size_type_node
, r
);
9215 /* If either argument is "", return NULL_TREE. */
9216 if ((p1
&& *p1
== '\0') || (p2
&& *p2
== '\0'))
9217 /* Evaluate and ignore both arguments in case either one has
9219 return omit_two_operands_loc (loc
, size_type_node
, size_zero_node
,
9225 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
9228 Return NULL_TREE if no simplification was possible, otherwise return the
9229 simplified form of the call as a tree.
9231 The simplified form may be a constant or other expression which
9232 computes the same value, but in a more efficient manner (including
9233 calls to other builtin functions).
9235 The call may contain arguments which need to be evaluated, but
9236 which are not useful to determine the result of the call. In
9237 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9238 COMPOUND_EXPR will be an argument which must be evaluated.
9239 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9240 COMPOUND_EXPR in the chain will contain the tree for the simplified
9241 form of the builtin function call. */
9244 fold_builtin_strcspn (location_t loc
, tree s1
, tree s2
)
9246 if (!validate_arg (s1
, POINTER_TYPE
)
9247 || !validate_arg (s2
, POINTER_TYPE
))
9251 const char *p1
= c_getstr (s1
), *p2
= c_getstr (s2
);
9253 /* If both arguments are constants, evaluate at compile-time. */
9256 const size_t r
= strcspn (p1
, p2
);
9257 return build_int_cst (size_type_node
, r
);
9260 /* If the first argument is "", return NULL_TREE. */
9261 if (p1
&& *p1
== '\0')
9263 /* Evaluate and ignore argument s2 in case it has
9265 return omit_one_operand_loc (loc
, size_type_node
,
9266 size_zero_node
, s2
);
9269 /* If the second argument is "", return __builtin_strlen(s1). */
9270 if (p2
&& *p2
== '\0')
9272 tree fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
9274 /* If the replacement _DECL isn't initialized, don't do the
9279 return build_call_expr_loc (loc
, fn
, 1, s1
);
9285 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
9286 produced. False otherwise. This is done so that we don't output the error
9287 or warning twice or three times. */
9290 fold_builtin_next_arg (tree exp
, bool va_start_p
)
9292 tree fntype
= TREE_TYPE (current_function_decl
);
9293 int nargs
= call_expr_nargs (exp
);
9295 /* There is good chance the current input_location points inside the
9296 definition of the va_start macro (perhaps on the token for
9297 builtin) in a system header, so warnings will not be emitted.
9298 Use the location in real source code. */
9299 source_location current_location
=
9300 linemap_unwind_to_first_non_reserved_loc (line_table
, input_location
,
9303 if (!stdarg_p (fntype
))
9305 error ("%<va_start%> used in function with fixed args");
9311 if (va_start_p
&& (nargs
!= 2))
9313 error ("wrong number of arguments to function %<va_start%>");
9316 arg
= CALL_EXPR_ARG (exp
, 1);
9318 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9319 when we checked the arguments and if needed issued a warning. */
9324 /* Evidently an out of date version of <stdarg.h>; can't validate
9325 va_start's second argument, but can still work as intended. */
9326 warning_at (current_location
,
9328 "%<__builtin_next_arg%> called without an argument");
9333 error ("wrong number of arguments to function %<__builtin_next_arg%>");
9336 arg
= CALL_EXPR_ARG (exp
, 0);
9339 if (TREE_CODE (arg
) == SSA_NAME
)
9340 arg
= SSA_NAME_VAR (arg
);
9342 /* We destructively modify the call to be __builtin_va_start (ap, 0)
9343 or __builtin_next_arg (0) the first time we see it, after checking
9344 the arguments and if needed issuing a warning. */
9345 if (!integer_zerop (arg
))
9347 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
9349 /* Strip off all nops for the sake of the comparison. This
9350 is not quite the same as STRIP_NOPS. It does more.
9351 We must also strip off INDIRECT_EXPR for C++ reference
9353 while (CONVERT_EXPR_P (arg
)
9354 || TREE_CODE (arg
) == INDIRECT_REF
)
9355 arg
= TREE_OPERAND (arg
, 0);
9356 if (arg
!= last_parm
)
9358 /* FIXME: Sometimes with the tree optimizers we can get the
9359 not the last argument even though the user used the last
9360 argument. We just warn and set the arg to be the last
9361 argument so that we will get wrong-code because of
9363 warning_at (current_location
,
9365 "second parameter of %<va_start%> not last named argument");
9368 /* Undefined by C99 7.15.1.4p4 (va_start):
9369 "If the parameter parmN is declared with the register storage
9370 class, with a function or array type, or with a type that is
9371 not compatible with the type that results after application of
9372 the default argument promotions, the behavior is undefined."
9374 else if (DECL_REGISTER (arg
))
9376 warning_at (current_location
,
9378 "undefined behaviour when second parameter of "
9379 "%<va_start%> is declared with %<register%> storage");
9382 /* We want to verify the second parameter just once before the tree
9383 optimizers are run and then avoid keeping it in the tree,
9384 as otherwise we could warn even for correct code like:
9385 void foo (int i, ...)
9386 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9388 CALL_EXPR_ARG (exp
, 1) = integer_zero_node
;
9390 CALL_EXPR_ARG (exp
, 0) = integer_zero_node
;
9396 /* Expand a call EXP to __builtin_object_size. */
9399 expand_builtin_object_size (tree exp
)
9402 int object_size_type
;
9403 tree fndecl
= get_callee_fndecl (exp
);
9405 if (!validate_arglist (exp
, POINTER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
9407 error ("%Kfirst argument of %D must be a pointer, second integer constant",
9409 expand_builtin_trap ();
9413 ost
= CALL_EXPR_ARG (exp
, 1);
9416 if (TREE_CODE (ost
) != INTEGER_CST
9417 || tree_int_cst_sgn (ost
) < 0
9418 || compare_tree_int (ost
, 3) > 0)
9420 error ("%Klast argument of %D is not integer constant between 0 and 3",
9422 expand_builtin_trap ();
9426 object_size_type
= tree_to_shwi (ost
);
9428 return object_size_type
< 2 ? constm1_rtx
: const0_rtx
;
9431 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
9432 FCODE is the BUILT_IN_* to use.
9433 Return NULL_RTX if we failed; the caller should emit a normal call,
9434 otherwise try to get the result in TARGET, if convenient (and in
9435 mode MODE if that's convenient). */
9438 expand_builtin_memory_chk (tree exp
, rtx target
, machine_mode mode
,
9439 enum built_in_function fcode
)
9441 tree dest
, src
, len
, size
;
9443 if (!validate_arglist (exp
,
9445 fcode
== BUILT_IN_MEMSET_CHK
9446 ? INTEGER_TYPE
: POINTER_TYPE
,
9447 INTEGER_TYPE
, INTEGER_TYPE
, VOID_TYPE
))
9450 dest
= CALL_EXPR_ARG (exp
, 0);
9451 src
= CALL_EXPR_ARG (exp
, 1);
9452 len
= CALL_EXPR_ARG (exp
, 2);
9453 size
= CALL_EXPR_ARG (exp
, 3);
9455 if (! tree_fits_uhwi_p (size
))
9458 if (tree_fits_uhwi_p (len
) || integer_all_onesp (size
))
9462 if (! integer_all_onesp (size
) && tree_int_cst_lt (size
, len
))
9464 warning_at (tree_nonartificial_location (exp
),
9465 0, "%Kcall to %D will always overflow destination buffer",
9466 exp
, get_callee_fndecl (exp
));
9471 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
9472 mem{cpy,pcpy,move,set} is available. */
9475 case BUILT_IN_MEMCPY_CHK
:
9476 fn
= builtin_decl_explicit (BUILT_IN_MEMCPY
);
9478 case BUILT_IN_MEMPCPY_CHK
:
9479 fn
= builtin_decl_explicit (BUILT_IN_MEMPCPY
);
9481 case BUILT_IN_MEMMOVE_CHK
:
9482 fn
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
9484 case BUILT_IN_MEMSET_CHK
:
9485 fn
= builtin_decl_explicit (BUILT_IN_MEMSET
);
9494 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 3, dest
, src
, len
);
9495 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
9496 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
9497 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
9499 else if (fcode
== BUILT_IN_MEMSET_CHK
)
9503 unsigned int dest_align
= get_pointer_alignment (dest
);
9505 /* If DEST is not a pointer type, call the normal function. */
9506 if (dest_align
== 0)
9509 /* If SRC and DEST are the same (and not volatile), do nothing. */
9510 if (operand_equal_p (src
, dest
, 0))
9514 if (fcode
!= BUILT_IN_MEMPCPY_CHK
)
9516 /* Evaluate and ignore LEN in case it has side-effects. */
9517 expand_expr (len
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
9518 return expand_expr (dest
, target
, mode
, EXPAND_NORMAL
);
9521 expr
= fold_build_pointer_plus (dest
, len
);
9522 return expand_expr (expr
, target
, mode
, EXPAND_NORMAL
);
9525 /* __memmove_chk special case. */
9526 if (fcode
== BUILT_IN_MEMMOVE_CHK
)
9528 unsigned int src_align
= get_pointer_alignment (src
);
9533 /* If src is categorized for a readonly section we can use
9534 normal __memcpy_chk. */
9535 if (readonly_data_expr (src
))
9537 tree fn
= builtin_decl_explicit (BUILT_IN_MEMCPY_CHK
);
9540 fn
= build_call_nofold_loc (EXPR_LOCATION (exp
), fn
, 4,
9541 dest
, src
, len
, size
);
9542 gcc_assert (TREE_CODE (fn
) == CALL_EXPR
);
9543 CALL_EXPR_TAILCALL (fn
) = CALL_EXPR_TAILCALL (exp
);
9544 return expand_expr (fn
, target
, mode
, EXPAND_NORMAL
);
9551 /* Emit warning if a buffer overflow is detected at compile time. */
9554 maybe_emit_chk_warning (tree exp
, enum built_in_function fcode
)
9558 location_t loc
= tree_nonartificial_location (exp
);
9562 case BUILT_IN_STRCPY_CHK
:
9563 case BUILT_IN_STPCPY_CHK
:
9564 /* For __strcat_chk the warning will be emitted only if overflowing
9565 by at least strlen (dest) + 1 bytes. */
9566 case BUILT_IN_STRCAT_CHK
:
9567 len
= CALL_EXPR_ARG (exp
, 1);
9568 size
= CALL_EXPR_ARG (exp
, 2);
9571 case BUILT_IN_STRNCAT_CHK
:
9572 case BUILT_IN_STRNCPY_CHK
:
9573 case BUILT_IN_STPNCPY_CHK
:
9574 len
= CALL_EXPR_ARG (exp
, 2);
9575 size
= CALL_EXPR_ARG (exp
, 3);
9577 case BUILT_IN_SNPRINTF_CHK
:
9578 case BUILT_IN_VSNPRINTF_CHK
:
9579 len
= CALL_EXPR_ARG (exp
, 1);
9580 size
= CALL_EXPR_ARG (exp
, 3);
9589 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
9594 len
= c_strlen (len
, 1);
9595 if (! len
|| ! tree_fits_uhwi_p (len
) || tree_int_cst_lt (len
, size
))
9598 else if (fcode
== BUILT_IN_STRNCAT_CHK
)
9600 tree src
= CALL_EXPR_ARG (exp
, 1);
9601 if (! src
|| ! tree_fits_uhwi_p (len
) || tree_int_cst_lt (len
, size
))
9603 src
= c_strlen (src
, 1);
9604 if (! src
|| ! tree_fits_uhwi_p (src
))
9606 warning_at (loc
, 0, "%Kcall to %D might overflow destination buffer",
9607 exp
, get_callee_fndecl (exp
));
9610 else if (tree_int_cst_lt (src
, size
))
9613 else if (! tree_fits_uhwi_p (len
) || ! tree_int_cst_lt (size
, len
))
9616 warning_at (loc
, 0, "%Kcall to %D will always overflow destination buffer",
9617 exp
, get_callee_fndecl (exp
));
9620 /* Emit warning if a buffer overflow is detected at compile time
9621 in __sprintf_chk/__vsprintf_chk calls. */
9624 maybe_emit_sprintf_chk_warning (tree exp
, enum built_in_function fcode
)
9626 tree size
, len
, fmt
;
9627 const char *fmt_str
;
9628 int nargs
= call_expr_nargs (exp
);
9630 /* Verify the required arguments in the original call. */
9634 size
= CALL_EXPR_ARG (exp
, 2);
9635 fmt
= CALL_EXPR_ARG (exp
, 3);
9637 if (! tree_fits_uhwi_p (size
) || integer_all_onesp (size
))
9640 /* Check whether the format is a literal string constant. */
9641 fmt_str
= c_getstr (fmt
);
9642 if (fmt_str
== NULL
)
9645 if (!init_target_chars ())
9648 /* If the format doesn't contain % args or %%, we know its size. */
9649 if (strchr (fmt_str
, target_percent
) == 0)
9650 len
= build_int_cstu (size_type_node
, strlen (fmt_str
));
9651 /* If the format is "%s" and first ... argument is a string literal,
9653 else if (fcode
== BUILT_IN_SPRINTF_CHK
9654 && strcmp (fmt_str
, target_percent_s
) == 0)
9660 arg
= CALL_EXPR_ARG (exp
, 4);
9661 if (! POINTER_TYPE_P (TREE_TYPE (arg
)))
9664 len
= c_strlen (arg
, 1);
9665 if (!len
|| ! tree_fits_uhwi_p (len
))
9671 if (! tree_int_cst_lt (len
, size
))
9672 warning_at (tree_nonartificial_location (exp
),
9673 0, "%Kcall to %D will always overflow destination buffer",
9674 exp
, get_callee_fndecl (exp
));
9677 /* Emit warning if a free is called with address of a variable. */
9680 maybe_emit_free_warning (tree exp
)
9682 tree arg
= CALL_EXPR_ARG (exp
, 0);
9685 if (TREE_CODE (arg
) != ADDR_EXPR
)
9688 arg
= get_base_address (TREE_OPERAND (arg
, 0));
9689 if (arg
== NULL
|| INDIRECT_REF_P (arg
) || TREE_CODE (arg
) == MEM_REF
)
9692 if (SSA_VAR_P (arg
))
9693 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
9694 "%Kattempt to free a non-heap object %qD", exp
, arg
);
9696 warning_at (tree_nonartificial_location (exp
), OPT_Wfree_nonheap_object
,
9697 "%Kattempt to free a non-heap object", exp
);
9700 /* Fold a call to __builtin_object_size with arguments PTR and OST,
9704 fold_builtin_object_size (tree ptr
, tree ost
)
9706 unsigned HOST_WIDE_INT bytes
;
9707 int object_size_type
;
9709 if (!validate_arg (ptr
, POINTER_TYPE
)
9710 || !validate_arg (ost
, INTEGER_TYPE
))
9715 if (TREE_CODE (ost
) != INTEGER_CST
9716 || tree_int_cst_sgn (ost
) < 0
9717 || compare_tree_int (ost
, 3) > 0)
9720 object_size_type
= tree_to_shwi (ost
);
9722 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
9723 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
9724 and (size_t) 0 for types 2 and 3. */
9725 if (TREE_SIDE_EFFECTS (ptr
))
9726 return build_int_cst_type (size_type_node
, object_size_type
< 2 ? -1 : 0);
9728 if (TREE_CODE (ptr
) == ADDR_EXPR
)
9730 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
9731 if (wi::fits_to_tree_p (bytes
, size_type_node
))
9732 return build_int_cstu (size_type_node
, bytes
);
9734 else if (TREE_CODE (ptr
) == SSA_NAME
)
9736 /* If object size is not known yet, delay folding until
9737 later. Maybe subsequent passes will help determining
9739 bytes
= compute_builtin_object_size (ptr
, object_size_type
);
9740 if (bytes
!= (unsigned HOST_WIDE_INT
) (object_size_type
< 2 ? -1 : 0)
9741 && wi::fits_to_tree_p (bytes
, size_type_node
))
9742 return build_int_cstu (size_type_node
, bytes
);
9748 /* Builtins with folding operations that operate on "..." arguments
9749 need special handling; we need to store the arguments in a convenient
9750 data structure before attempting any folding. Fortunately there are
9751 only a few builtins that fall into this category. FNDECL is the
9752 function, EXP is the CALL_EXPR for the call. */
9755 fold_builtin_varargs (location_t loc
, tree fndecl
, tree
*args
, int nargs
)
9757 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
9758 tree ret
= NULL_TREE
;
9762 case BUILT_IN_FPCLASSIFY
:
9763 ret
= fold_builtin_fpclassify (loc
, args
, nargs
);
9771 ret
= build1 (NOP_EXPR
, TREE_TYPE (ret
), ret
);
9772 SET_EXPR_LOCATION (ret
, loc
);
9773 TREE_NO_WARNING (ret
) = 1;
9779 /* Initialize format string characters in the target charset. */
9782 init_target_chars (void)
9787 target_newline
= lang_hooks
.to_target_charset ('\n');
9788 target_percent
= lang_hooks
.to_target_charset ('%');
9789 target_c
= lang_hooks
.to_target_charset ('c');
9790 target_s
= lang_hooks
.to_target_charset ('s');
9791 if (target_newline
== 0 || target_percent
== 0 || target_c
== 0
9795 target_percent_c
[0] = target_percent
;
9796 target_percent_c
[1] = target_c
;
9797 target_percent_c
[2] = '\0';
9799 target_percent_s
[0] = target_percent
;
9800 target_percent_s
[1] = target_s
;
9801 target_percent_s
[2] = '\0';
9803 target_percent_s_newline
[0] = target_percent
;
9804 target_percent_s_newline
[1] = target_s
;
9805 target_percent_s_newline
[2] = target_newline
;
9806 target_percent_s_newline
[3] = '\0';
9813 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
9814 and no overflow/underflow occurred. INEXACT is true if M was not
9815 exactly calculated. TYPE is the tree type for the result. This
9816 function assumes that you cleared the MPFR flags and then
9817 calculated M to see if anything subsequently set a flag prior to
9818 entering this function. Return NULL_TREE if any checks fail. */
9821 do_mpfr_ckconv (mpfr_srcptr m
, tree type
, int inexact
)
9823 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
9824 overflow/underflow occurred. If -frounding-math, proceed iff the
9825 result of calling FUNC was exact. */
9826 if (mpfr_number_p (m
) && !mpfr_overflow_p () && !mpfr_underflow_p ()
9827 && (!flag_rounding_math
|| !inexact
))
9831 real_from_mpfr (&rr
, m
, type
, GMP_RNDN
);
9832 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
9833 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
9834 but the mpft_t is not, then we underflowed in the
9836 if (real_isfinite (&rr
)
9837 && (rr
.cl
== rvc_zero
) == (mpfr_zero_p (m
) != 0))
9839 REAL_VALUE_TYPE rmode
;
9841 real_convert (&rmode
, TYPE_MODE (type
), &rr
);
9842 /* Proceed iff the specified mode can hold the value. */
9843 if (real_identical (&rmode
, &rr
))
9844 return build_real (type
, rmode
);
9850 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
9851 number and no overflow/underflow occurred. INEXACT is true if M
9852 was not exactly calculated. TYPE is the tree type for the result.
9853 This function assumes that you cleared the MPFR flags and then
9854 calculated M to see if anything subsequently set a flag prior to
9855 entering this function. Return NULL_TREE if any checks fail, if
9856 FORCE_CONVERT is true, then bypass the checks. */
9859 do_mpc_ckconv (mpc_srcptr m
, tree type
, int inexact
, int force_convert
)
9861 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
9862 overflow/underflow occurred. If -frounding-math, proceed iff the
9863 result of calling FUNC was exact. */
9865 || (mpfr_number_p (mpc_realref (m
)) && mpfr_number_p (mpc_imagref (m
))
9866 && !mpfr_overflow_p () && !mpfr_underflow_p ()
9867 && (!flag_rounding_math
|| !inexact
)))
9869 REAL_VALUE_TYPE re
, im
;
9871 real_from_mpfr (&re
, mpc_realref (m
), TREE_TYPE (type
), GMP_RNDN
);
9872 real_from_mpfr (&im
, mpc_imagref (m
), TREE_TYPE (type
), GMP_RNDN
);
9873 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
9874 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
9875 but the mpft_t is not, then we underflowed in the
9878 || (real_isfinite (&re
) && real_isfinite (&im
)
9879 && (re
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_realref (m
)) != 0)
9880 && (im
.cl
== rvc_zero
) == (mpfr_zero_p (mpc_imagref (m
)) != 0)))
9882 REAL_VALUE_TYPE re_mode
, im_mode
;
9884 real_convert (&re_mode
, TYPE_MODE (TREE_TYPE (type
)), &re
);
9885 real_convert (&im_mode
, TYPE_MODE (TREE_TYPE (type
)), &im
);
9886 /* Proceed iff the specified mode can hold the value. */
9888 || (real_identical (&re_mode
, &re
)
9889 && real_identical (&im_mode
, &im
)))
9890 return build_complex (type
, build_real (TREE_TYPE (type
), re_mode
),
9891 build_real (TREE_TYPE (type
), im_mode
));
9897 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
9898 the pointer *(ARG_QUO) and return the result. The type is taken
9899 from the type of ARG0 and is used for setting the precision of the
9900 calculation and results. */
9903 do_mpfr_remquo (tree arg0
, tree arg1
, tree arg_quo
)
9905 tree
const type
= TREE_TYPE (arg0
);
9906 tree result
= NULL_TREE
;
9911 /* To proceed, MPFR must exactly represent the target floating point
9912 format, which only happens when the target base equals two. */
9913 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
9914 && TREE_CODE (arg0
) == REAL_CST
&& !TREE_OVERFLOW (arg0
)
9915 && TREE_CODE (arg1
) == REAL_CST
&& !TREE_OVERFLOW (arg1
))
9917 const REAL_VALUE_TYPE
*const ra0
= TREE_REAL_CST_PTR (arg0
);
9918 const REAL_VALUE_TYPE
*const ra1
= TREE_REAL_CST_PTR (arg1
);
9920 if (real_isfinite (ra0
) && real_isfinite (ra1
))
9922 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
9923 const int prec
= fmt
->p
;
9924 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
9929 mpfr_inits2 (prec
, m0
, m1
, NULL
);
9930 mpfr_from_real (m0
, ra0
, GMP_RNDN
);
9931 mpfr_from_real (m1
, ra1
, GMP_RNDN
);
9932 mpfr_clear_flags ();
9933 mpfr_remquo (m0
, &integer_quo
, m0
, m1
, rnd
);
9934 /* Remquo is independent of the rounding mode, so pass
9935 inexact=0 to do_mpfr_ckconv(). */
9936 result_rem
= do_mpfr_ckconv (m0
, type
, /*inexact=*/ 0);
9937 mpfr_clears (m0
, m1
, NULL
);
9940 /* MPFR calculates quo in the host's long so it may
9941 return more bits in quo than the target int can hold
9942 if sizeof(host long) > sizeof(target int). This can
9943 happen even for native compilers in LP64 mode. In
9944 these cases, modulo the quo value with the largest
9945 number that the target int can hold while leaving one
9946 bit for the sign. */
9947 if (sizeof (integer_quo
) * CHAR_BIT
> INT_TYPE_SIZE
)
9948 integer_quo
%= (long)(1UL << (INT_TYPE_SIZE
- 1));
9950 /* Dereference the quo pointer argument. */
9951 arg_quo
= build_fold_indirect_ref (arg_quo
);
9952 /* Proceed iff a valid pointer type was passed in. */
9953 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo
)) == integer_type_node
)
9955 /* Set the value. */
9957 = fold_build2 (MODIFY_EXPR
, TREE_TYPE (arg_quo
), arg_quo
,
9958 build_int_cst (TREE_TYPE (arg_quo
),
9960 TREE_SIDE_EFFECTS (result_quo
) = 1;
9961 /* Combine the quo assignment with the rem. */
9962 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
9963 result_quo
, result_rem
));
9971 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
9972 resulting value as a tree with type TYPE. The mpfr precision is
9973 set to the precision of TYPE. We assume that this mpfr function
9974 returns zero if the result could be calculated exactly within the
9975 requested precision. In addition, the integer pointer represented
9976 by ARG_SG will be dereferenced and set to the appropriate signgam
9980 do_mpfr_lgamma_r (tree arg
, tree arg_sg
, tree type
)
9982 tree result
= NULL_TREE
;
9986 /* To proceed, MPFR must exactly represent the target floating point
9987 format, which only happens when the target base equals two. Also
9988 verify ARG is a constant and that ARG_SG is an int pointer. */
9989 if (REAL_MODE_FORMAT (TYPE_MODE (type
))->b
== 2
9990 && TREE_CODE (arg
) == REAL_CST
&& !TREE_OVERFLOW (arg
)
9991 && TREE_CODE (TREE_TYPE (arg_sg
)) == POINTER_TYPE
9992 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg
))) == integer_type_node
)
9994 const REAL_VALUE_TYPE
*const ra
= TREE_REAL_CST_PTR (arg
);
9996 /* In addition to NaN and Inf, the argument cannot be zero or a
9997 negative integer. */
9998 if (real_isfinite (ra
)
9999 && ra
->cl
!= rvc_zero
10000 && !(real_isneg (ra
) && real_isinteger (ra
, TYPE_MODE (type
))))
10002 const struct real_format
*fmt
= REAL_MODE_FORMAT (TYPE_MODE (type
));
10003 const int prec
= fmt
->p
;
10004 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
10009 mpfr_init2 (m
, prec
);
10010 mpfr_from_real (m
, ra
, GMP_RNDN
);
10011 mpfr_clear_flags ();
10012 inexact
= mpfr_lgamma (m
, &sg
, m
, rnd
);
10013 result_lg
= do_mpfr_ckconv (m
, type
, inexact
);
10019 /* Dereference the arg_sg pointer argument. */
10020 arg_sg
= build_fold_indirect_ref (arg_sg
);
10021 /* Assign the signgam value into *arg_sg. */
10022 result_sg
= fold_build2 (MODIFY_EXPR
,
10023 TREE_TYPE (arg_sg
), arg_sg
,
10024 build_int_cst (TREE_TYPE (arg_sg
), sg
));
10025 TREE_SIDE_EFFECTS (result_sg
) = 1;
10026 /* Combine the signgam assignment with the lgamma result. */
10027 result
= non_lvalue (fold_build2 (COMPOUND_EXPR
, type
,
10028 result_sg
, result_lg
));
10036 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
10037 mpc function FUNC on it and return the resulting value as a tree
10038 with type TYPE. The mpfr precision is set to the precision of
10039 TYPE. We assume that function FUNC returns zero if the result
10040 could be calculated exactly within the requested precision. If
10041 DO_NONFINITE is true, then fold expressions containing Inf or NaN
10042 in the arguments and/or results. */
10045 do_mpc_arg2 (tree arg0
, tree arg1
, tree type
, int do_nonfinite
,
10046 int (*func
)(mpc_ptr
, mpc_srcptr
, mpc_srcptr
, mpc_rnd_t
))
10048 tree result
= NULL_TREE
;
10053 /* To proceed, MPFR must exactly represent the target floating point
10054 format, which only happens when the target base equals two. */
10055 if (TREE_CODE (arg0
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg0
)
10056 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0
))) == REAL_TYPE
10057 && TREE_CODE (arg1
) == COMPLEX_CST
&& !TREE_OVERFLOW (arg1
)
10058 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1
))) == REAL_TYPE
10059 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0
))))->b
== 2)
10061 const REAL_VALUE_TYPE
*const re0
= TREE_REAL_CST_PTR (TREE_REALPART (arg0
));
10062 const REAL_VALUE_TYPE
*const im0
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg0
));
10063 const REAL_VALUE_TYPE
*const re1
= TREE_REAL_CST_PTR (TREE_REALPART (arg1
));
10064 const REAL_VALUE_TYPE
*const im1
= TREE_REAL_CST_PTR (TREE_IMAGPART (arg1
));
10067 || (real_isfinite (re0
) && real_isfinite (im0
)
10068 && real_isfinite (re1
) && real_isfinite (im1
)))
10070 const struct real_format
*const fmt
=
10071 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type
)));
10072 const int prec
= fmt
->p
;
10073 const mp_rnd_t rnd
= fmt
->round_towards_zero
? GMP_RNDZ
: GMP_RNDN
;
10074 const mpc_rnd_t crnd
= fmt
->round_towards_zero
? MPC_RNDZZ
: MPC_RNDNN
;
10078 mpc_init2 (m0
, prec
);
10079 mpc_init2 (m1
, prec
);
10080 mpfr_from_real (mpc_realref (m0
), re0
, rnd
);
10081 mpfr_from_real (mpc_imagref (m0
), im0
, rnd
);
10082 mpfr_from_real (mpc_realref (m1
), re1
, rnd
);
10083 mpfr_from_real (mpc_imagref (m1
), im1
, rnd
);
10084 mpfr_clear_flags ();
10085 inexact
= func (m0
, m0
, m1
, crnd
);
10086 result
= do_mpc_ckconv (m0
, type
, inexact
, do_nonfinite
);
10095 /* A wrapper function for builtin folding that prevents warnings for
10096 "statement without effect" and the like, caused by removing the
10097 call node earlier than the warning is generated. */
10100 fold_call_stmt (gcall
*stmt
, bool ignore
)
10102 tree ret
= NULL_TREE
;
10103 tree fndecl
= gimple_call_fndecl (stmt
);
10104 location_t loc
= gimple_location (stmt
);
10106 && TREE_CODE (fndecl
) == FUNCTION_DECL
10107 && DECL_BUILT_IN (fndecl
)
10108 && !gimple_call_va_arg_pack_p (stmt
))
10110 int nargs
= gimple_call_num_args (stmt
);
10111 tree
*args
= (nargs
> 0
10112 ? gimple_call_arg_ptr (stmt
, 0)
10113 : &error_mark_node
);
10115 if (avoid_folding_inline_builtin (fndecl
))
10117 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
10119 return targetm
.fold_builtin (fndecl
, nargs
, args
, ignore
);
10123 ret
= fold_builtin_n (loc
, fndecl
, args
, nargs
, ignore
);
10126 /* Propagate location information from original call to
10127 expansion of builtin. Otherwise things like
10128 maybe_emit_chk_warning, that operate on the expansion
10129 of a builtin, will use the wrong location information. */
10130 if (gimple_has_location (stmt
))
10132 tree realret
= ret
;
10133 if (TREE_CODE (ret
) == NOP_EXPR
)
10134 realret
= TREE_OPERAND (ret
, 0);
10135 if (CAN_HAVE_LOCATION_P (realret
)
10136 && !EXPR_HAS_LOCATION (realret
))
10137 SET_EXPR_LOCATION (realret
, loc
);
10147 /* Look up the function in builtin_decl that corresponds to DECL
10148 and set ASMSPEC as its user assembler name. DECL must be a
10149 function decl that declares a builtin. */
10152 set_builtin_user_assembler_name (tree decl
, const char *asmspec
)
10155 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
10156 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
10159 builtin
= builtin_decl_explicit (DECL_FUNCTION_CODE (decl
));
10160 set_user_assembler_name (builtin
, asmspec
);
10161 switch (DECL_FUNCTION_CODE (decl
))
10163 case BUILT_IN_MEMCPY
:
10164 init_block_move_fn (asmspec
);
10165 memcpy_libfunc
= set_user_assembler_libfunc ("memcpy", asmspec
);
10167 case BUILT_IN_MEMSET
:
10168 init_block_clear_fn (asmspec
);
10169 memset_libfunc
= set_user_assembler_libfunc ("memset", asmspec
);
10171 case BUILT_IN_MEMMOVE
:
10172 memmove_libfunc
= set_user_assembler_libfunc ("memmove", asmspec
);
10174 case BUILT_IN_MEMCMP
:
10175 memcmp_libfunc
= set_user_assembler_libfunc ("memcmp", asmspec
);
10177 case BUILT_IN_ABORT
:
10178 abort_libfunc
= set_user_assembler_libfunc ("abort", asmspec
);
10181 if (INT_TYPE_SIZE
< BITS_PER_WORD
)
10183 set_user_assembler_libfunc ("ffs", asmspec
);
10184 set_optab_libfunc (ffs_optab
, mode_for_size (INT_TYPE_SIZE
,
10185 MODE_INT
, 0), "ffs");
10193 /* Return true if DECL is a builtin that expands to a constant or similarly
10196 is_simple_builtin (tree decl
)
10198 if (decl
&& DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
10199 switch (DECL_FUNCTION_CODE (decl
))
10201 /* Builtins that expand to constants. */
10202 case BUILT_IN_CONSTANT_P
:
10203 case BUILT_IN_EXPECT
:
10204 case BUILT_IN_OBJECT_SIZE
:
10205 case BUILT_IN_UNREACHABLE
:
10206 /* Simple register moves or loads from stack. */
10207 case BUILT_IN_ASSUME_ALIGNED
:
10208 case BUILT_IN_RETURN_ADDRESS
:
10209 case BUILT_IN_EXTRACT_RETURN_ADDR
:
10210 case BUILT_IN_FROB_RETURN_ADDR
:
10211 case BUILT_IN_RETURN
:
10212 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
10213 case BUILT_IN_FRAME_ADDRESS
:
10214 case BUILT_IN_VA_END
:
10215 case BUILT_IN_STACK_SAVE
:
10216 case BUILT_IN_STACK_RESTORE
:
10217 /* Exception state returns or moves registers around. */
10218 case BUILT_IN_EH_FILTER
:
10219 case BUILT_IN_EH_POINTER
:
10220 case BUILT_IN_EH_COPY_VALUES
:
10230 /* Return true if DECL is a builtin that is not expensive, i.e., they are
10231 most probably expanded inline into reasonably simple code. This is a
10232 superset of is_simple_builtin. */
10234 is_inexpensive_builtin (tree decl
)
10238 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_MD
)
10240 else if (DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
10241 switch (DECL_FUNCTION_CODE (decl
))
10244 case BUILT_IN_ALLOCA
:
10245 case BUILT_IN_ALLOCA_WITH_ALIGN
:
10246 case BUILT_IN_BSWAP16
:
10247 case BUILT_IN_BSWAP32
:
10248 case BUILT_IN_BSWAP64
:
10250 case BUILT_IN_CLZIMAX
:
10251 case BUILT_IN_CLZL
:
10252 case BUILT_IN_CLZLL
:
10254 case BUILT_IN_CTZIMAX
:
10255 case BUILT_IN_CTZL
:
10256 case BUILT_IN_CTZLL
:
10258 case BUILT_IN_FFSIMAX
:
10259 case BUILT_IN_FFSL
:
10260 case BUILT_IN_FFSLL
:
10261 case BUILT_IN_IMAXABS
:
10262 case BUILT_IN_FINITE
:
10263 case BUILT_IN_FINITEF
:
10264 case BUILT_IN_FINITEL
:
10265 case BUILT_IN_FINITED32
:
10266 case BUILT_IN_FINITED64
:
10267 case BUILT_IN_FINITED128
:
10268 case BUILT_IN_FPCLASSIFY
:
10269 case BUILT_IN_ISFINITE
:
10270 case BUILT_IN_ISINF_SIGN
:
10271 case BUILT_IN_ISINF
:
10272 case BUILT_IN_ISINFF
:
10273 case BUILT_IN_ISINFL
:
10274 case BUILT_IN_ISINFD32
:
10275 case BUILT_IN_ISINFD64
:
10276 case BUILT_IN_ISINFD128
:
10277 case BUILT_IN_ISNAN
:
10278 case BUILT_IN_ISNANF
:
10279 case BUILT_IN_ISNANL
:
10280 case BUILT_IN_ISNAND32
:
10281 case BUILT_IN_ISNAND64
:
10282 case BUILT_IN_ISNAND128
:
10283 case BUILT_IN_ISNORMAL
:
10284 case BUILT_IN_ISGREATER
:
10285 case BUILT_IN_ISGREATEREQUAL
:
10286 case BUILT_IN_ISLESS
:
10287 case BUILT_IN_ISLESSEQUAL
:
10288 case BUILT_IN_ISLESSGREATER
:
10289 case BUILT_IN_ISUNORDERED
:
10290 case BUILT_IN_VA_ARG_PACK
:
10291 case BUILT_IN_VA_ARG_PACK_LEN
:
10292 case BUILT_IN_VA_COPY
:
10293 case BUILT_IN_TRAP
:
10294 case BUILT_IN_SAVEREGS
:
10295 case BUILT_IN_POPCOUNTL
:
10296 case BUILT_IN_POPCOUNTLL
:
10297 case BUILT_IN_POPCOUNTIMAX
:
10298 case BUILT_IN_POPCOUNT
:
10299 case BUILT_IN_PARITYL
:
10300 case BUILT_IN_PARITYLL
:
10301 case BUILT_IN_PARITYIMAX
:
10302 case BUILT_IN_PARITY
:
10303 case BUILT_IN_LABS
:
10304 case BUILT_IN_LLABS
:
10305 case BUILT_IN_PREFETCH
:
10306 case BUILT_IN_ACC_ON_DEVICE
:
10310 return is_simple_builtin (decl
);