c++: only cache constexpr calls that are constant exprs
[official-gcc.git] / gcc / builtins.cc
blob6dff5214ff8d1d427d0315a668dd8382f2619e76
1 /* Expand builtin functions.
2 Copyright (C) 1988-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
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
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Legacy warning! Please add no further builtin simplifications here
21 (apart from pure constant folding) - builtin simplifications should go
22 to match.pd or gimple-fold.cc instead. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "memmodel.h"
32 #include "gimple.h"
33 #include "predict.h"
34 #include "tm_p.h"
35 #include "stringpool.h"
36 #include "tree-vrp.h"
37 #include "tree-ssanames.h"
38 #include "expmed.h"
39 #include "optabs.h"
40 #include "emit-rtl.h"
41 #include "recog.h"
42 #include "diagnostic-core.h"
43 #include "alias.h"
44 #include "fold-const.h"
45 #include "fold-const-call.h"
46 #include "gimple-ssa-warn-access.h"
47 #include "stor-layout.h"
48 #include "calls.h"
49 #include "varasm.h"
50 #include "tree-object-size.h"
51 #include "tree-ssa-strlen.h"
52 #include "realmpfr.h"
53 #include "cfgrtl.h"
54 #include "except.h"
55 #include "dojump.h"
56 #include "explow.h"
57 #include "stmt.h"
58 #include "expr.h"
59 #include "libfuncs.h"
60 #include "output.h"
61 #include "typeclass.h"
62 #include "langhooks.h"
63 #include "value-prof.h"
64 #include "builtins.h"
65 #include "stringpool.h"
66 #include "attribs.h"
67 #include "asan.h"
68 #include "internal-fn.h"
69 #include "case-cfn-macros.h"
70 #include "gimple-iterator.h"
71 #include "gimple-fold.h"
72 #include "intl.h"
73 #include "file-prefix-map.h" /* remap_macro_filename() */
74 #include "gomp-constants.h"
75 #include "omp-general.h"
76 #include "tree-dfa.h"
77 #include "gimple-ssa.h"
78 #include "tree-ssa-live.h"
79 #include "tree-outof-ssa.h"
80 #include "attr-fnspec.h"
81 #include "demangle.h"
82 #include "gimple-range.h"
83 #include "pointer-query.h"
85 struct target_builtins default_target_builtins;
86 #if SWITCHABLE_TARGET
87 struct target_builtins *this_target_builtins = &default_target_builtins;
88 #endif
90 /* Define the names of the builtin function types and codes. */
91 const char *const built_in_class_names[BUILT_IN_LAST]
92 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
94 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
95 const char * built_in_names[(int) END_BUILTINS] =
97 #include "builtins.def"
100 /* Setup an array of builtin_info_type, make sure each element decl is
101 initialized to NULL_TREE. */
102 builtin_info_type builtin_info[(int)END_BUILTINS];
104 /* Non-zero if __builtin_constant_p should be folded right away. */
105 bool force_folding_builtin_constant_p;
107 static int target_char_cast (tree, char *);
108 static int apply_args_size (void);
109 static int apply_result_size (void);
110 static rtx result_vector (int, rtx);
111 static void expand_builtin_prefetch (tree);
112 static rtx expand_builtin_apply_args (void);
113 static rtx expand_builtin_apply_args_1 (void);
114 static rtx expand_builtin_apply (rtx, rtx, rtx);
115 static void expand_builtin_return (rtx);
116 static enum type_class type_to_class (tree);
117 static rtx expand_builtin_classify_type (tree);
118 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
119 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
120 static rtx expand_builtin_interclass_mathfn (tree, rtx);
121 static rtx expand_builtin_sincos (tree);
122 static rtx expand_builtin_fegetround (tree, rtx, machine_mode);
123 static rtx expand_builtin_feclear_feraise_except (tree, rtx, machine_mode,
124 optab);
125 static rtx expand_builtin_cexpi (tree, rtx);
126 static rtx expand_builtin_issignaling (tree, rtx);
127 static rtx expand_builtin_int_roundingfn (tree, rtx);
128 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
129 static rtx expand_builtin_next_arg (void);
130 static rtx expand_builtin_va_start (tree);
131 static rtx expand_builtin_va_end (tree);
132 static rtx expand_builtin_va_copy (tree);
133 static rtx inline_expand_builtin_bytecmp (tree, rtx);
134 static rtx expand_builtin_strcmp (tree, rtx);
135 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
136 static rtx expand_builtin_memcpy (tree, rtx);
137 static rtx expand_builtin_memory_copy_args (tree dest, tree src, tree len,
138 rtx target, tree exp,
139 memop_ret retmode,
140 bool might_overlap);
141 static rtx expand_builtin_memmove (tree, rtx);
142 static rtx expand_builtin_mempcpy (tree, rtx);
143 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, tree, memop_ret);
144 static rtx expand_builtin_strcpy (tree, rtx);
145 static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx);
146 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
147 static rtx expand_builtin_strncpy (tree, rtx);
148 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
149 static rtx expand_builtin_bzero (tree);
150 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
151 static rtx expand_builtin_strnlen (tree, rtx, machine_mode);
152 static rtx expand_builtin_alloca (tree);
153 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
154 static rtx expand_builtin_frame_address (tree, tree);
155 static tree stabilize_va_list_loc (location_t, tree, int);
156 static rtx expand_builtin_expect (tree, rtx);
157 static rtx expand_builtin_expect_with_probability (tree, rtx);
158 static tree fold_builtin_constant_p (tree);
159 static tree fold_builtin_classify_type (tree);
160 static tree fold_builtin_strlen (location_t, tree, tree, tree);
161 static tree fold_builtin_inf (location_t, tree, int);
162 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
163 static bool validate_arg (const_tree, enum tree_code code);
164 static rtx expand_builtin_fabs (tree, rtx, rtx);
165 static rtx expand_builtin_signbit (tree, rtx);
166 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
167 static tree fold_builtin_isascii (location_t, tree);
168 static tree fold_builtin_toascii (location_t, tree);
169 static tree fold_builtin_isdigit (location_t, tree);
170 static tree fold_builtin_fabs (location_t, tree, tree);
171 static tree fold_builtin_abs (location_t, tree, tree);
172 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
173 enum tree_code);
174 static tree fold_builtin_varargs (location_t, tree, tree*, int);
176 static tree fold_builtin_strpbrk (location_t, tree, tree, tree, tree);
177 static tree fold_builtin_strspn (location_t, tree, tree, tree);
178 static tree fold_builtin_strcspn (location_t, tree, tree, tree);
180 static rtx expand_builtin_object_size (tree);
181 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
182 enum built_in_function);
183 static void maybe_emit_chk_warning (tree, enum built_in_function);
184 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
185 static tree fold_builtin_object_size (tree, tree, enum built_in_function);
187 unsigned HOST_WIDE_INT target_newline;
188 unsigned HOST_WIDE_INT target_percent;
189 static unsigned HOST_WIDE_INT target_c;
190 static unsigned HOST_WIDE_INT target_s;
191 char target_percent_c[3];
192 char target_percent_s[3];
193 char target_percent_s_newline[4];
194 static tree do_mpfr_remquo (tree, tree, tree);
195 static tree do_mpfr_lgamma_r (tree, tree, tree);
196 static void expand_builtin_sync_synchronize (void);
198 /* Return true if NAME starts with __builtin_ or __sync_. */
200 static bool
201 is_builtin_name (const char *name)
203 return (startswith (name, "__builtin_")
204 || startswith (name, "__sync_")
205 || startswith (name, "__atomic_"));
208 /* Return true if NODE should be considered for inline expansion regardless
209 of the optimization level. This means whenever a function is invoked with
210 its "internal" name, which normally contains the prefix "__builtin". */
212 bool
213 called_as_built_in (tree node)
215 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
216 we want the name used to call the function, not the name it
217 will have. */
218 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
219 return is_builtin_name (name);
222 /* Compute values M and N such that M divides (address of EXP - N) and such
223 that N < M. If these numbers can be determined, store M in alignp and N in
224 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
225 *alignp and any bit-offset to *bitposp.
227 Note that the address (and thus the alignment) computed here is based
228 on the address to which a symbol resolves, whereas DECL_ALIGN is based
229 on the address at which an object is actually located. These two
230 addresses are not always the same. For example, on ARM targets,
231 the address &foo of a Thumb function foo() has the lowest bit set,
232 whereas foo() itself starts on an even address.
234 If ADDR_P is true we are taking the address of the memory reference EXP
235 and thus cannot rely on the access taking place. */
237 bool
238 get_object_alignment_2 (tree exp, unsigned int *alignp,
239 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
241 poly_int64 bitsize, bitpos;
242 tree offset;
243 machine_mode mode;
244 int unsignedp, reversep, volatilep;
245 unsigned int align = BITS_PER_UNIT;
246 bool known_alignment = false;
248 /* Get the innermost object and the constant (bitpos) and possibly
249 variable (offset) offset of the access. */
250 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
251 &unsignedp, &reversep, &volatilep);
253 /* Extract alignment information from the innermost object and
254 possibly adjust bitpos and offset. */
255 if (TREE_CODE (exp) == FUNCTION_DECL)
257 /* Function addresses can encode extra information besides their
258 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
259 allows the low bit to be used as a virtual bit, we know
260 that the address itself must be at least 2-byte aligned. */
261 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
262 align = 2 * BITS_PER_UNIT;
264 else if (TREE_CODE (exp) == LABEL_DECL)
266 else if (TREE_CODE (exp) == CONST_DECL)
268 /* The alignment of a CONST_DECL is determined by its initializer. */
269 exp = DECL_INITIAL (exp);
270 align = TYPE_ALIGN (TREE_TYPE (exp));
271 if (CONSTANT_CLASS_P (exp))
272 align = targetm.constant_alignment (exp, align);
274 known_alignment = true;
276 else if (DECL_P (exp))
278 align = DECL_ALIGN (exp);
279 known_alignment = true;
281 else if (TREE_CODE (exp) == INDIRECT_REF
282 || TREE_CODE (exp) == MEM_REF
283 || TREE_CODE (exp) == TARGET_MEM_REF)
285 tree addr = TREE_OPERAND (exp, 0);
286 unsigned ptr_align;
287 unsigned HOST_WIDE_INT ptr_bitpos;
288 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
290 /* If the address is explicitely aligned, handle that. */
291 if (TREE_CODE (addr) == BIT_AND_EXPR
292 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
294 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
295 ptr_bitmask *= BITS_PER_UNIT;
296 align = least_bit_hwi (ptr_bitmask);
297 addr = TREE_OPERAND (addr, 0);
300 known_alignment
301 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
302 align = MAX (ptr_align, align);
304 /* Re-apply explicit alignment to the bitpos. */
305 ptr_bitpos &= ptr_bitmask;
307 /* The alignment of the pointer operand in a TARGET_MEM_REF
308 has to take the variable offset parts into account. */
309 if (TREE_CODE (exp) == TARGET_MEM_REF)
311 if (TMR_INDEX (exp))
313 unsigned HOST_WIDE_INT step = 1;
314 if (TMR_STEP (exp))
315 step = TREE_INT_CST_LOW (TMR_STEP (exp));
316 align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
318 if (TMR_INDEX2 (exp))
319 align = BITS_PER_UNIT;
320 known_alignment = false;
323 /* When EXP is an actual memory reference then we can use
324 TYPE_ALIGN of a pointer indirection to derive alignment.
325 Do so only if get_pointer_alignment_1 did not reveal absolute
326 alignment knowledge and if using that alignment would
327 improve the situation. */
328 unsigned int talign;
329 if (!addr_p && !known_alignment
330 && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT)
331 && talign > align)
332 align = talign;
333 else
335 /* Else adjust bitpos accordingly. */
336 bitpos += ptr_bitpos;
337 if (TREE_CODE (exp) == MEM_REF
338 || TREE_CODE (exp) == TARGET_MEM_REF)
339 bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT;
342 else if (TREE_CODE (exp) == STRING_CST)
344 /* STRING_CST are the only constant objects we allow to be not
345 wrapped inside a CONST_DECL. */
346 align = TYPE_ALIGN (TREE_TYPE (exp));
347 if (CONSTANT_CLASS_P (exp))
348 align = targetm.constant_alignment (exp, align);
350 known_alignment = true;
353 /* If there is a non-constant offset part extract the maximum
354 alignment that can prevail. */
355 if (offset)
357 unsigned int trailing_zeros = tree_ctz (offset);
358 if (trailing_zeros < HOST_BITS_PER_INT)
360 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
361 if (inner)
362 align = MIN (align, inner);
366 /* Account for the alignment of runtime coefficients, so that the constant
367 bitpos is guaranteed to be accurate. */
368 unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]);
369 if (alt_align != 0 && alt_align < align)
371 align = alt_align;
372 known_alignment = false;
375 *alignp = align;
376 *bitposp = bitpos.coeffs[0] & (align - 1);
377 return known_alignment;
380 /* For a memory reference expression EXP compute values M and N such that M
381 divides (&EXP - N) and such that N < M. If these numbers can be determined,
382 store M in alignp and N in *BITPOSP and return true. Otherwise return false
383 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
385 bool
386 get_object_alignment_1 (tree exp, unsigned int *alignp,
387 unsigned HOST_WIDE_INT *bitposp)
389 /* Strip a WITH_SIZE_EXPR, get_inner_reference doesn't know how to deal
390 with it. */
391 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
392 exp = TREE_OPERAND (exp, 0);
393 return get_object_alignment_2 (exp, alignp, bitposp, false);
396 /* Return the alignment in bits of EXP, an object. */
398 unsigned int
399 get_object_alignment (tree exp)
401 unsigned HOST_WIDE_INT bitpos = 0;
402 unsigned int align;
404 get_object_alignment_1 (exp, &align, &bitpos);
406 /* align and bitpos now specify known low bits of the pointer.
407 ptr & (align - 1) == bitpos. */
409 if (bitpos != 0)
410 align = least_bit_hwi (bitpos);
411 return align;
414 /* For a pointer valued expression EXP compute values M and N such that M
415 divides (EXP - N) and such that N < M. If these numbers can be determined,
416 store M in alignp and N in *BITPOSP and return true. Return false if
417 the results are just a conservative approximation.
419 If EXP is not a pointer, false is returned too. */
421 bool
422 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
423 unsigned HOST_WIDE_INT *bitposp)
425 STRIP_NOPS (exp);
427 if (TREE_CODE (exp) == ADDR_EXPR)
428 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
429 alignp, bitposp, true);
430 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
432 unsigned int align;
433 unsigned HOST_WIDE_INT bitpos;
434 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
435 &align, &bitpos);
436 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
437 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
438 else
440 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
441 if (trailing_zeros < HOST_BITS_PER_INT)
443 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
444 if (inner)
445 align = MIN (align, inner);
448 *alignp = align;
449 *bitposp = bitpos & (align - 1);
450 return res;
452 else if (TREE_CODE (exp) == SSA_NAME
453 && POINTER_TYPE_P (TREE_TYPE (exp)))
455 unsigned int ptr_align, ptr_misalign;
456 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
458 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
460 *bitposp = ptr_misalign * BITS_PER_UNIT;
461 *alignp = ptr_align * BITS_PER_UNIT;
462 /* Make sure to return a sensible alignment when the multiplication
463 by BITS_PER_UNIT overflowed. */
464 if (*alignp == 0)
465 *alignp = 1u << (HOST_BITS_PER_INT - 1);
466 /* We cannot really tell whether this result is an approximation. */
467 return false;
469 else
471 *bitposp = 0;
472 *alignp = BITS_PER_UNIT;
473 return false;
476 else if (TREE_CODE (exp) == INTEGER_CST)
478 *alignp = BIGGEST_ALIGNMENT;
479 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
480 & (BIGGEST_ALIGNMENT - 1));
481 return true;
484 *bitposp = 0;
485 *alignp = BITS_PER_UNIT;
486 return false;
489 /* Return the alignment in bits of EXP, a pointer valued expression.
490 The alignment returned is, by default, the alignment of the thing that
491 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
493 Otherwise, look at the expression to see if we can do better, i.e., if the
494 expression is actually pointing at an object whose alignment is tighter. */
496 unsigned int
497 get_pointer_alignment (tree exp)
499 unsigned HOST_WIDE_INT bitpos = 0;
500 unsigned int align;
502 get_pointer_alignment_1 (exp, &align, &bitpos);
504 /* align and bitpos now specify known low bits of the pointer.
505 ptr & (align - 1) == bitpos. */
507 if (bitpos != 0)
508 align = least_bit_hwi (bitpos);
510 return align;
513 /* Return the number of leading non-zero elements in the sequence
514 [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
515 ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
517 unsigned
518 string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
520 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
522 unsigned n;
524 if (eltsize == 1)
526 /* Optimize the common case of plain char. */
527 for (n = 0; n < maxelts; n++)
529 const char *elt = (const char*) ptr + n;
530 if (!*elt)
531 break;
534 else
536 for (n = 0; n < maxelts; n++)
538 const char *elt = (const char*) ptr + n * eltsize;
539 if (!memcmp (elt, "\0\0\0\0", eltsize))
540 break;
543 return n;
546 /* Compute the length of a null-terminated character string or wide
547 character string handling character sizes of 1, 2, and 4 bytes.
548 TREE_STRING_LENGTH is not the right way because it evaluates to
549 the size of the character array in bytes (as opposed to characters)
550 and because it can contain a zero byte in the middle.
552 ONLY_VALUE should be nonzero if the result is not going to be emitted
553 into the instruction stream and zero if it is going to be expanded.
554 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
555 is returned, otherwise NULL, since
556 len = c_strlen (ARG, 1); if (len) expand_expr (len, ...); would not
557 evaluate the side-effects.
559 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
560 accesses. Note that this implies the result is not going to be emitted
561 into the instruction stream.
563 Additional information about the string accessed may be recorded
564 in DATA. For example, if ARG references an unterminated string,
565 then the declaration will be stored in the DECL field. If the
566 length of the unterminated string can be determined, it'll be
567 stored in the LEN field. Note this length could well be different
568 than what a C strlen call would return.
570 ELTSIZE is 1 for normal single byte character strings, and 2 or
571 4 for wide characer strings. ELTSIZE is by default 1.
573 The value returned is of type `ssizetype'. */
575 tree
576 c_strlen (tree arg, int only_value, c_strlen_data *data, unsigned eltsize)
578 /* If we were not passed a DATA pointer, then get one to a local
579 structure. That avoids having to check DATA for NULL before
580 each time we want to use it. */
581 c_strlen_data local_strlen_data = { };
582 if (!data)
583 data = &local_strlen_data;
585 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
587 tree src = STRIP_NOPS (arg);
588 if (TREE_CODE (src) == COND_EXPR
589 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
591 tree len1, len2;
593 len1 = c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
594 len2 = c_strlen (TREE_OPERAND (src, 2), only_value, data, eltsize);
595 if (tree_int_cst_equal (len1, len2))
596 return len1;
599 if (TREE_CODE (src) == COMPOUND_EXPR
600 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
601 return c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
603 location_t loc = EXPR_LOC_OR_LOC (src, input_location);
605 /* Offset from the beginning of the string in bytes. */
606 tree byteoff;
607 tree memsize;
608 tree decl;
609 src = string_constant (src, &byteoff, &memsize, &decl);
610 if (src == 0)
611 return NULL_TREE;
613 /* Determine the size of the string element. */
614 if (eltsize != tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src)))))
615 return NULL_TREE;
617 /* Set MAXELTS to ARRAY_SIZE (SRC) - 1, the maximum possible
618 length of SRC. Prefer TYPE_SIZE() to TREE_STRING_LENGTH() if possible
619 in case the latter is less than the size of the array, such as when
620 SRC refers to a short string literal used to initialize a large array.
621 In that case, the elements of the array after the terminating NUL are
622 all NUL. */
623 HOST_WIDE_INT strelts = TREE_STRING_LENGTH (src);
624 strelts = strelts / eltsize;
626 if (!tree_fits_uhwi_p (memsize))
627 return NULL_TREE;
629 HOST_WIDE_INT maxelts = tree_to_uhwi (memsize) / eltsize;
631 /* PTR can point to the byte representation of any string type, including
632 char* and wchar_t*. */
633 const char *ptr = TREE_STRING_POINTER (src);
635 if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
637 /* The code below works only for single byte character types. */
638 if (eltsize != 1)
639 return NULL_TREE;
641 /* If the string has an internal NUL character followed by any
642 non-NUL characters (e.g., "foo\0bar"), we can't compute
643 the offset to the following NUL if we don't know where to
644 start searching for it. */
645 unsigned len = string_length (ptr, eltsize, strelts);
647 /* Return when an embedded null character is found or none at all.
648 In the latter case, set the DECL/LEN field in the DATA structure
649 so that callers may examine them. */
650 if (len + 1 < strelts)
651 return NULL_TREE;
652 else if (len >= maxelts)
654 data->decl = decl;
655 data->off = byteoff;
656 data->minlen = ssize_int (len);
657 return NULL_TREE;
660 /* For empty strings the result should be zero. */
661 if (len == 0)
662 return ssize_int (0);
664 /* We don't know the starting offset, but we do know that the string
665 has no internal zero bytes. If the offset falls within the bounds
666 of the string subtract the offset from the length of the string,
667 and return that. Otherwise the length is zero. Take care to
668 use SAVE_EXPR in case the OFFSET has side-effects. */
669 tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff)
670 : byteoff;
671 offsave = fold_convert_loc (loc, sizetype, offsave);
672 tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave,
673 size_int (len));
674 tree lenexp = fold_build2_loc (loc, MINUS_EXPR, sizetype, size_int (len),
675 offsave);
676 lenexp = fold_convert_loc (loc, ssizetype, lenexp);
677 return fold_build3_loc (loc, COND_EXPR, ssizetype, condexp, lenexp,
678 build_zero_cst (ssizetype));
681 /* Offset from the beginning of the string in elements. */
682 HOST_WIDE_INT eltoff;
684 /* We have a known offset into the string. Start searching there for
685 a null character if we can represent it as a single HOST_WIDE_INT. */
686 if (byteoff == 0)
687 eltoff = 0;
688 else if (! tree_fits_uhwi_p (byteoff) || tree_to_uhwi (byteoff) % eltsize)
689 eltoff = -1;
690 else
691 eltoff = tree_to_uhwi (byteoff) / eltsize;
693 /* If the offset is known to be out of bounds, warn, and call strlen at
694 runtime. */
695 if (eltoff < 0 || eltoff >= maxelts)
697 /* Suppress multiple warnings for propagated constant strings. */
698 if (only_value != 2
699 && !warning_suppressed_p (arg, OPT_Warray_bounds_)
700 && warning_at (loc, OPT_Warray_bounds_,
701 "offset %qwi outside bounds of constant string",
702 eltoff))
704 if (decl)
705 inform (DECL_SOURCE_LOCATION (decl), "%qE declared here", decl);
706 suppress_warning (arg, OPT_Warray_bounds_);
708 return NULL_TREE;
711 /* If eltoff is larger than strelts but less than maxelts the
712 string length is zero, since the excess memory will be zero. */
713 if (eltoff > strelts)
714 return ssize_int (0);
716 /* Use strlen to search for the first zero byte. Since any strings
717 constructed with build_string will have nulls appended, we win even
718 if we get handed something like (char[4])"abcd".
720 Since ELTOFF is our starting index into the string, no further
721 calculation is needed. */
722 unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
723 strelts - eltoff);
725 /* Don't know what to return if there was no zero termination.
726 Ideally this would turn into a gcc_checking_assert over time.
727 Set DECL/LEN so callers can examine them. */
728 if (len >= maxelts - eltoff)
730 data->decl = decl;
731 data->off = byteoff;
732 data->minlen = ssize_int (len);
733 return NULL_TREE;
736 return ssize_int (len);
739 /* Return a constant integer corresponding to target reading
740 GET_MODE_BITSIZE (MODE) bits from string constant STR. If
741 NULL_TERMINATED_P, reading stops after '\0' character, all further ones
742 are assumed to be zero, otherwise it reads as many characters
743 as needed. */
746 c_readstr (const char *str, scalar_int_mode mode,
747 bool null_terminated_p/*=true*/)
749 HOST_WIDE_INT ch;
750 unsigned int i, j;
751 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
753 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
754 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
755 / HOST_BITS_PER_WIDE_INT;
757 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
758 for (i = 0; i < len; i++)
759 tmp[i] = 0;
761 ch = 1;
762 for (i = 0; i < GET_MODE_SIZE (mode); i++)
764 j = i;
765 if (WORDS_BIG_ENDIAN)
766 j = GET_MODE_SIZE (mode) - i - 1;
767 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
768 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
769 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
770 j *= BITS_PER_UNIT;
772 if (ch || !null_terminated_p)
773 ch = (unsigned char) str[i];
774 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
777 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
778 return immed_wide_int_const (c, mode);
781 /* Cast a target constant CST to target CHAR and if that value fits into
782 host char type, return zero and put that value into variable pointed to by
783 P. */
785 static int
786 target_char_cast (tree cst, char *p)
788 unsigned HOST_WIDE_INT val, hostval;
790 if (TREE_CODE (cst) != INTEGER_CST
791 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
792 return 1;
794 /* Do not care if it fits or not right here. */
795 val = TREE_INT_CST_LOW (cst);
797 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
798 val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
800 hostval = val;
801 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
802 hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
804 if (val != hostval)
805 return 1;
807 *p = hostval;
808 return 0;
811 /* Similar to save_expr, but assumes that arbitrary code is not executed
812 in between the multiple evaluations. In particular, we assume that a
813 non-addressable local variable will not be modified. */
815 static tree
816 builtin_save_expr (tree exp)
818 if (TREE_CODE (exp) == SSA_NAME
819 || (TREE_ADDRESSABLE (exp) == 0
820 && (TREE_CODE (exp) == PARM_DECL
821 || (VAR_P (exp) && !TREE_STATIC (exp)))))
822 return exp;
824 return save_expr (exp);
827 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
828 times to get the address of either a higher stack frame, or a return
829 address located within it (depending on FNDECL_CODE). */
831 static rtx
832 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
834 int i;
835 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
836 if (tem == NULL_RTX)
838 /* For a zero count with __builtin_return_address, we don't care what
839 frame address we return, because target-specific definitions will
840 override us. Therefore frame pointer elimination is OK, and using
841 the soft frame pointer is OK.
843 For a nonzero count, or a zero count with __builtin_frame_address,
844 we require a stable offset from the current frame pointer to the
845 previous one, so we must use the hard frame pointer, and
846 we must disable frame pointer elimination. */
847 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
848 tem = frame_pointer_rtx;
849 else
851 tem = hard_frame_pointer_rtx;
853 /* Tell reload not to eliminate the frame pointer. */
854 crtl->accesses_prior_frames = 1;
858 if (count > 0)
859 SETUP_FRAME_ADDRESSES ();
861 /* On the SPARC, the return address is not in the frame, it is in a
862 register. There is no way to access it off of the current frame
863 pointer, but it can be accessed off the previous frame pointer by
864 reading the value from the register window save area. */
865 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
866 count--;
868 /* Scan back COUNT frames to the specified frame. */
869 for (i = 0; i < count; i++)
871 /* Assume the dynamic chain pointer is in the word that the
872 frame address points to, unless otherwise specified. */
873 tem = DYNAMIC_CHAIN_ADDRESS (tem);
874 tem = memory_address (Pmode, tem);
875 tem = gen_frame_mem (Pmode, tem);
876 tem = copy_to_reg (tem);
879 /* For __builtin_frame_address, return what we've got. But, on
880 the SPARC for example, we may have to add a bias. */
881 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
882 return FRAME_ADDR_RTX (tem);
884 /* For __builtin_return_address, get the return address from that frame. */
885 #ifdef RETURN_ADDR_RTX
886 tem = RETURN_ADDR_RTX (count, tem);
887 #else
888 tem = memory_address (Pmode,
889 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
890 tem = gen_frame_mem (Pmode, tem);
891 #endif
892 return tem;
895 /* Alias set used for setjmp buffer. */
896 static alias_set_type setjmp_alias_set = -1;
898 /* Construct the leading half of a __builtin_setjmp call. Control will
899 return to RECEIVER_LABEL. This is also called directly by the SJLJ
900 exception handling code. */
902 void
903 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
905 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
906 rtx stack_save;
907 rtx mem;
909 if (setjmp_alias_set == -1)
910 setjmp_alias_set = new_alias_set ();
912 buf_addr = convert_memory_address (Pmode, buf_addr);
914 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
916 /* We store the frame pointer and the address of receiver_label in
917 the buffer and use the rest of it for the stack save area, which
918 is machine-dependent. */
920 mem = gen_rtx_MEM (Pmode, buf_addr);
921 set_mem_alias_set (mem, setjmp_alias_set);
922 emit_move_insn (mem, hard_frame_pointer_rtx);
924 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
925 GET_MODE_SIZE (Pmode))),
926 set_mem_alias_set (mem, setjmp_alias_set);
928 emit_move_insn (validize_mem (mem),
929 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
931 stack_save = gen_rtx_MEM (sa_mode,
932 plus_constant (Pmode, buf_addr,
933 2 * GET_MODE_SIZE (Pmode)));
934 set_mem_alias_set (stack_save, setjmp_alias_set);
935 emit_stack_save (SAVE_NONLOCAL, &stack_save);
937 /* If there is further processing to do, do it. */
938 if (targetm.have_builtin_setjmp_setup ())
939 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
941 /* We have a nonlocal label. */
942 cfun->has_nonlocal_label = 1;
945 /* Construct the trailing part of a __builtin_setjmp call. This is
946 also called directly by the SJLJ exception handling code.
947 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
949 void
950 expand_builtin_setjmp_receiver (rtx receiver_label)
952 rtx chain;
954 /* Mark the FP as used when we get here, so we have to make sure it's
955 marked as used by this function. */
956 emit_use (hard_frame_pointer_rtx);
958 /* Mark the static chain as clobbered here so life information
959 doesn't get messed up for it. */
960 chain = rtx_for_static_chain (current_function_decl, true);
961 if (chain && REG_P (chain))
962 emit_clobber (chain);
964 if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
966 /* If the argument pointer can be eliminated in favor of the
967 frame pointer, we don't need to restore it. We assume here
968 that if such an elimination is present, it can always be used.
969 This is the case on all known machines; if we don't make this
970 assumption, we do unnecessary saving on many machines. */
971 size_t i;
972 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
974 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
975 if (elim_regs[i].from == ARG_POINTER_REGNUM
976 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
977 break;
979 if (i == ARRAY_SIZE (elim_regs))
981 /* Now restore our arg pointer from the address at which it
982 was saved in our stack frame. */
983 emit_move_insn (crtl->args.internal_arg_pointer,
984 copy_to_reg (get_arg_pointer_save_area ()));
988 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
989 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
990 else if (targetm.have_nonlocal_goto_receiver ())
991 emit_insn (targetm.gen_nonlocal_goto_receiver ());
992 else
993 { /* Nothing */ }
995 /* We must not allow the code we just generated to be reordered by
996 scheduling. Specifically, the update of the frame pointer must
997 happen immediately, not later. */
998 emit_insn (gen_blockage ());
1001 /* __builtin_longjmp is passed a pointer to an array of five words (not
1002 all will be used on all machines). It operates similarly to the C
1003 library function of the same name, but is more efficient. Much of
1004 the code below is copied from the handling of non-local gotos. */
1006 static void
1007 expand_builtin_longjmp (rtx buf_addr, rtx value)
1009 rtx fp, lab, stack;
1010 rtx_insn *insn, *last;
1011 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1013 /* DRAP is needed for stack realign if longjmp is expanded to current
1014 function */
1015 if (SUPPORTS_STACK_ALIGNMENT)
1016 crtl->need_drap = true;
1018 if (setjmp_alias_set == -1)
1019 setjmp_alias_set = new_alias_set ();
1021 buf_addr = convert_memory_address (Pmode, buf_addr);
1023 buf_addr = force_reg (Pmode, buf_addr);
1025 /* We require that the user must pass a second argument of 1, because
1026 that is what builtin_setjmp will return. */
1027 gcc_assert (value == const1_rtx);
1029 last = get_last_insn ();
1030 if (targetm.have_builtin_longjmp ())
1031 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
1032 else
1034 fp = gen_rtx_MEM (Pmode, buf_addr);
1035 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1036 GET_MODE_SIZE (Pmode)));
1038 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1039 2 * GET_MODE_SIZE (Pmode)));
1040 set_mem_alias_set (fp, setjmp_alias_set);
1041 set_mem_alias_set (lab, setjmp_alias_set);
1042 set_mem_alias_set (stack, setjmp_alias_set);
1044 /* Pick up FP, label, and SP from the block and jump. This code is
1045 from expand_goto in stmt.cc; see there for detailed comments. */
1046 if (targetm.have_nonlocal_goto ())
1047 /* We have to pass a value to the nonlocal_goto pattern that will
1048 get copied into the static_chain pointer, but it does not matter
1049 what that value is, because builtin_setjmp does not use it. */
1050 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1051 else
1053 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1054 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1056 lab = copy_to_reg (lab);
1058 /* Restore the frame pointer and stack pointer. We must use a
1059 temporary since the setjmp buffer may be a local. */
1060 fp = copy_to_reg (fp);
1061 emit_stack_restore (SAVE_NONLOCAL, stack);
1063 /* Ensure the frame pointer move is not optimized. */
1064 emit_insn (gen_blockage ());
1065 emit_clobber (hard_frame_pointer_rtx);
1066 emit_clobber (frame_pointer_rtx);
1067 emit_move_insn (hard_frame_pointer_rtx, fp);
1069 emit_use (hard_frame_pointer_rtx);
1070 emit_use (stack_pointer_rtx);
1071 emit_indirect_jump (lab);
1075 /* Search backwards and mark the jump insn as a non-local goto.
1076 Note that this precludes the use of __builtin_longjmp to a
1077 __builtin_setjmp target in the same function. However, we've
1078 already cautioned the user that these functions are for
1079 internal exception handling use only. */
1080 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1082 gcc_assert (insn != last);
1084 if (JUMP_P (insn))
1086 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1087 break;
1089 else if (CALL_P (insn))
1090 break;
1094 static inline bool
1095 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1097 return (iter->i < iter->n);
1100 /* This function validates the types of a function call argument list
1101 against a specified list of tree_codes. If the last specifier is a 0,
1102 that represents an ellipsis, otherwise the last specifier must be a
1103 VOID_TYPE. */
1105 static bool
1106 validate_arglist (const_tree callexpr, ...)
1108 enum tree_code code;
1109 bool res = 0;
1110 va_list ap;
1111 const_call_expr_arg_iterator iter;
1112 const_tree arg;
1114 va_start (ap, callexpr);
1115 init_const_call_expr_arg_iterator (callexpr, &iter);
1117 /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1118 tree fn = CALL_EXPR_FN (callexpr);
1119 bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1121 for (unsigned argno = 1; ; ++argno)
1123 code = (enum tree_code) va_arg (ap, int);
1125 switch (code)
1127 case 0:
1128 /* This signifies an ellipses, any further arguments are all ok. */
1129 res = true;
1130 goto end;
1131 case VOID_TYPE:
1132 /* This signifies an endlink, if no arguments remain, return
1133 true, otherwise return false. */
1134 res = !more_const_call_expr_args_p (&iter);
1135 goto end;
1136 case POINTER_TYPE:
1137 /* The actual argument must be nonnull when either the whole
1138 called function has been declared nonnull, or when the formal
1139 argument corresponding to the actual argument has been. */
1140 if (argmap
1141 && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1143 arg = next_const_call_expr_arg (&iter);
1144 if (!validate_arg (arg, code) || integer_zerop (arg))
1145 goto end;
1146 break;
1148 /* FALLTHRU */
1149 default:
1150 /* If no parameters remain or the parameter's code does not
1151 match the specified code, return false. Otherwise continue
1152 checking any remaining arguments. */
1153 arg = next_const_call_expr_arg (&iter);
1154 if (!validate_arg (arg, code))
1155 goto end;
1156 break;
1160 /* We need gotos here since we can only have one VA_CLOSE in a
1161 function. */
1162 end: ;
1163 va_end (ap);
1165 BITMAP_FREE (argmap);
1167 return res;
1170 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1171 and the address of the save area. */
1173 static rtx
1174 expand_builtin_nonlocal_goto (tree exp)
1176 tree t_label, t_save_area;
1177 rtx r_label, r_save_area, r_fp, r_sp;
1178 rtx_insn *insn;
1180 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1181 return NULL_RTX;
1183 t_label = CALL_EXPR_ARG (exp, 0);
1184 t_save_area = CALL_EXPR_ARG (exp, 1);
1186 r_label = expand_normal (t_label);
1187 r_label = convert_memory_address (Pmode, r_label);
1188 r_save_area = expand_normal (t_save_area);
1189 r_save_area = convert_memory_address (Pmode, r_save_area);
1190 /* Copy the address of the save location to a register just in case it was
1191 based on the frame pointer. */
1192 r_save_area = copy_to_reg (r_save_area);
1193 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1194 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1195 plus_constant (Pmode, r_save_area,
1196 GET_MODE_SIZE (Pmode)));
1198 crtl->has_nonlocal_goto = 1;
1200 /* ??? We no longer need to pass the static chain value, afaik. */
1201 if (targetm.have_nonlocal_goto ())
1202 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1203 else
1205 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1206 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1208 r_label = copy_to_reg (r_label);
1210 /* Restore the frame pointer and stack pointer. We must use a
1211 temporary since the setjmp buffer may be a local. */
1212 r_fp = copy_to_reg (r_fp);
1213 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1215 /* Ensure the frame pointer move is not optimized. */
1216 emit_insn (gen_blockage ());
1217 emit_clobber (hard_frame_pointer_rtx);
1218 emit_clobber (frame_pointer_rtx);
1219 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1221 /* USE of hard_frame_pointer_rtx added for consistency;
1222 not clear if really needed. */
1223 emit_use (hard_frame_pointer_rtx);
1224 emit_use (stack_pointer_rtx);
1226 /* If the architecture is using a GP register, we must
1227 conservatively assume that the target function makes use of it.
1228 The prologue of functions with nonlocal gotos must therefore
1229 initialize the GP register to the appropriate value, and we
1230 must then make sure that this value is live at the point
1231 of the jump. (Note that this doesn't necessarily apply
1232 to targets with a nonlocal_goto pattern; they are free
1233 to implement it in their own way. Note also that this is
1234 a no-op if the GP register is a global invariant.) */
1235 unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1236 if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1237 emit_use (pic_offset_table_rtx);
1239 emit_indirect_jump (r_label);
1242 /* Search backwards to the jump insn and mark it as a
1243 non-local goto. */
1244 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1246 if (JUMP_P (insn))
1248 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1249 break;
1251 else if (CALL_P (insn))
1252 break;
1255 return const0_rtx;
1258 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1259 (not all will be used on all machines) that was passed to __builtin_setjmp.
1260 It updates the stack pointer in that block to the current value. This is
1261 also called directly by the SJLJ exception handling code. */
1263 void
1264 expand_builtin_update_setjmp_buf (rtx buf_addr)
1266 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1267 buf_addr = convert_memory_address (Pmode, buf_addr);
1268 rtx stack_save
1269 = gen_rtx_MEM (sa_mode,
1270 memory_address
1271 (sa_mode,
1272 plus_constant (Pmode, buf_addr,
1273 2 * GET_MODE_SIZE (Pmode))));
1275 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1278 /* Expand a call to __builtin_prefetch. For a target that does not support
1279 data prefetch, evaluate the memory address argument in case it has side
1280 effects. */
1282 static void
1283 expand_builtin_prefetch (tree exp)
1285 tree arg0, arg1, arg2;
1286 int nargs;
1287 rtx op0, op1, op2;
1289 if (!validate_arglist (exp, POINTER_TYPE, 0))
1290 return;
1292 arg0 = CALL_EXPR_ARG (exp, 0);
1294 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1295 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1296 locality). */
1297 nargs = call_expr_nargs (exp);
1298 if (nargs > 1)
1299 arg1 = CALL_EXPR_ARG (exp, 1);
1300 else
1301 arg1 = integer_zero_node;
1302 if (nargs > 2)
1303 arg2 = CALL_EXPR_ARG (exp, 2);
1304 else
1305 arg2 = integer_three_node;
1307 /* Argument 0 is an address. */
1308 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1310 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1311 if (TREE_CODE (arg1) != INTEGER_CST)
1313 error ("second argument to %<__builtin_prefetch%> must be a constant");
1314 arg1 = integer_zero_node;
1316 op1 = expand_normal (arg1);
1317 /* Argument 1 must be either zero or one. */
1318 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1320 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1321 " using zero");
1322 op1 = const0_rtx;
1325 /* Argument 2 (locality) must be a compile-time constant int. */
1326 if (TREE_CODE (arg2) != INTEGER_CST)
1328 error ("third argument to %<__builtin_prefetch%> must be a constant");
1329 arg2 = integer_zero_node;
1331 op2 = expand_normal (arg2);
1332 /* Argument 2 must be 0, 1, 2, or 3. */
1333 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1335 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1336 op2 = const0_rtx;
1339 if (targetm.have_prefetch ())
1341 class expand_operand ops[3];
1343 create_address_operand (&ops[0], op0);
1344 create_integer_operand (&ops[1], INTVAL (op1));
1345 create_integer_operand (&ops[2], INTVAL (op2));
1346 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1347 return;
1350 /* Don't do anything with direct references to volatile memory, but
1351 generate code to handle other side effects. */
1352 if (!MEM_P (op0) && side_effects_p (op0))
1353 emit_insn (op0);
1356 /* Get a MEM rtx for expression EXP which is the address of an operand
1357 to be used in a string instruction (cmpstrsi, cpymemsi, ..). LEN is
1358 the maximum length of the block of memory that might be accessed or
1359 NULL if unknown. */
1362 get_memory_rtx (tree exp, tree len)
1364 tree orig_exp = exp, base;
1365 rtx addr, mem;
1367 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1368 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1369 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1370 exp = TREE_OPERAND (exp, 0);
1372 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1373 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1375 /* Get an expression we can use to find the attributes to assign to MEM.
1376 First remove any nops. */
1377 while (CONVERT_EXPR_P (exp)
1378 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1379 exp = TREE_OPERAND (exp, 0);
1381 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1382 (as builtin stringops may alias with anything). */
1383 exp = fold_build2 (MEM_REF,
1384 build_array_type (char_type_node,
1385 build_range_type (sizetype,
1386 size_one_node, len)),
1387 exp, build_int_cst (ptr_type_node, 0));
1389 /* If the MEM_REF has no acceptable address, try to get the base object
1390 from the original address we got, and build an all-aliasing
1391 unknown-sized access to that one. */
1392 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1393 set_mem_attributes (mem, exp, 0);
1394 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1395 && (base = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1396 0))))
1398 unsigned int align = get_pointer_alignment (TREE_OPERAND (exp, 0));
1399 exp = build_fold_addr_expr (base);
1400 exp = fold_build2 (MEM_REF,
1401 build_array_type (char_type_node,
1402 build_range_type (sizetype,
1403 size_zero_node,
1404 NULL)),
1405 exp, build_int_cst (ptr_type_node, 0));
1406 set_mem_attributes (mem, exp, 0);
1407 /* Since we stripped parts make sure the offset is unknown and the
1408 alignment is computed from the original address. */
1409 clear_mem_offset (mem);
1410 set_mem_align (mem, align);
1412 set_mem_alias_set (mem, 0);
1413 return mem;
1416 /* Built-in functions to perform an untyped call and return. */
1418 #define apply_args_mode \
1419 (this_target_builtins->x_apply_args_mode)
1420 #define apply_result_mode \
1421 (this_target_builtins->x_apply_result_mode)
1423 /* Return the size required for the block returned by __builtin_apply_args,
1424 and initialize apply_args_mode. */
1426 static int
1427 apply_args_size (void)
1429 static int size = -1;
1430 int align;
1431 unsigned int regno;
1433 /* The values computed by this function never change. */
1434 if (size < 0)
1436 /* The first value is the incoming arg-pointer. */
1437 size = GET_MODE_SIZE (Pmode);
1439 /* The second value is the structure value address unless this is
1440 passed as an "invisible" first argument. */
1441 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1442 size += GET_MODE_SIZE (Pmode);
1444 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1445 if (FUNCTION_ARG_REGNO_P (regno))
1447 fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno);
1449 if (mode != VOIDmode)
1451 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1452 if (size % align != 0)
1453 size = CEIL (size, align) * align;
1454 size += GET_MODE_SIZE (mode);
1455 apply_args_mode[regno] = mode;
1457 else
1458 apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1460 else
1461 apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1463 return size;
1466 /* Return the size required for the block returned by __builtin_apply,
1467 and initialize apply_result_mode. */
1469 static int
1470 apply_result_size (void)
1472 static int size = -1;
1473 int align, regno;
1475 /* The values computed by this function never change. */
1476 if (size < 0)
1478 size = 0;
1480 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1481 if (targetm.calls.function_value_regno_p (regno))
1483 fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1485 if (mode != VOIDmode)
1487 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1488 if (size % align != 0)
1489 size = CEIL (size, align) * align;
1490 size += GET_MODE_SIZE (mode);
1491 apply_result_mode[regno] = mode;
1493 else
1494 apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1496 else
1497 apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1499 /* Allow targets that use untyped_call and untyped_return to override
1500 the size so that machine-specific information can be stored here. */
1501 #ifdef APPLY_RESULT_SIZE
1502 size = APPLY_RESULT_SIZE;
1503 #endif
1505 return size;
1508 /* Create a vector describing the result block RESULT. If SAVEP is true,
1509 the result block is used to save the values; otherwise it is used to
1510 restore the values. */
1512 static rtx
1513 result_vector (int savep, rtx result)
1515 int regno, size, align, nelts;
1516 fixed_size_mode mode;
1517 rtx reg, mem;
1518 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1520 size = nelts = 0;
1521 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1522 if ((mode = apply_result_mode[regno]) != VOIDmode)
1524 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1525 if (size % align != 0)
1526 size = CEIL (size, align) * align;
1527 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1528 mem = adjust_address (result, mode, size);
1529 savevec[nelts++] = (savep
1530 ? gen_rtx_SET (mem, reg)
1531 : gen_rtx_SET (reg, mem));
1532 size += GET_MODE_SIZE (mode);
1534 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1537 /* Save the state required to perform an untyped call with the same
1538 arguments as were passed to the current function. */
1540 static rtx
1541 expand_builtin_apply_args_1 (void)
1543 rtx registers, tem;
1544 int size, align, regno;
1545 fixed_size_mode mode;
1546 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1548 /* Create a block where the arg-pointer, structure value address,
1549 and argument registers can be saved. */
1550 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1552 /* Walk past the arg-pointer and structure value address. */
1553 size = GET_MODE_SIZE (Pmode);
1554 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1555 size += GET_MODE_SIZE (Pmode);
1557 /* Save each register used in calling a function to the block. */
1558 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1559 if ((mode = apply_args_mode[regno]) != VOIDmode)
1561 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1562 if (size % align != 0)
1563 size = CEIL (size, align) * align;
1565 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1567 emit_move_insn (adjust_address (registers, mode, size), tem);
1568 size += GET_MODE_SIZE (mode);
1571 /* Save the arg pointer to the block. */
1572 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1573 /* We need the pointer as the caller actually passed them to us, not
1574 as we might have pretended they were passed. Make sure it's a valid
1575 operand, as emit_move_insn isn't expected to handle a PLUS. */
1576 if (STACK_GROWS_DOWNWARD)
1578 = force_operand (plus_constant (Pmode, tem,
1579 crtl->args.pretend_args_size),
1580 NULL_RTX);
1581 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1583 size = GET_MODE_SIZE (Pmode);
1585 /* Save the structure value address unless this is passed as an
1586 "invisible" first argument. */
1587 if (struct_incoming_value)
1588 emit_move_insn (adjust_address (registers, Pmode, size),
1589 copy_to_reg (struct_incoming_value));
1591 /* Return the address of the block. */
1592 return copy_addr_to_reg (XEXP (registers, 0));
1595 /* __builtin_apply_args returns block of memory allocated on
1596 the stack into which is stored the arg pointer, structure
1597 value address, static chain, and all the registers that might
1598 possibly be used in performing a function call. The code is
1599 moved to the start of the function so the incoming values are
1600 saved. */
1602 static rtx
1603 expand_builtin_apply_args (void)
1605 /* Don't do __builtin_apply_args more than once in a function.
1606 Save the result of the first call and reuse it. */
1607 if (apply_args_value != 0)
1608 return apply_args_value;
1610 /* When this function is called, it means that registers must be
1611 saved on entry to this function. So we migrate the
1612 call to the first insn of this function. */
1613 rtx temp;
1615 start_sequence ();
1616 temp = expand_builtin_apply_args_1 ();
1617 rtx_insn *seq = get_insns ();
1618 end_sequence ();
1620 apply_args_value = temp;
1622 /* Put the insns after the NOTE that starts the function.
1623 If this is inside a start_sequence, make the outer-level insn
1624 chain current, so the code is placed at the start of the
1625 function. If internal_arg_pointer is a non-virtual pseudo,
1626 it needs to be placed after the function that initializes
1627 that pseudo. */
1628 push_topmost_sequence ();
1629 if (REG_P (crtl->args.internal_arg_pointer)
1630 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1631 emit_insn_before (seq, parm_birth_insn);
1632 else
1633 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1634 pop_topmost_sequence ();
1635 return temp;
1639 /* Perform an untyped call and save the state required to perform an
1640 untyped return of whatever value was returned by the given function. */
1642 static rtx
1643 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1645 int size, align, regno;
1646 fixed_size_mode mode;
1647 rtx incoming_args, result, reg, dest, src;
1648 rtx_call_insn *call_insn;
1649 rtx old_stack_level = 0;
1650 rtx call_fusage = 0;
1651 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1653 arguments = convert_memory_address (Pmode, arguments);
1655 /* Create a block where the return registers can be saved. */
1656 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1658 /* Fetch the arg pointer from the ARGUMENTS block. */
1659 incoming_args = gen_reg_rtx (Pmode);
1660 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1661 if (!STACK_GROWS_DOWNWARD)
1662 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1663 incoming_args, 0, OPTAB_LIB_WIDEN);
1665 /* Push a new argument block and copy the arguments. Do not allow
1666 the (potential) memcpy call below to interfere with our stack
1667 manipulations. */
1668 do_pending_stack_adjust ();
1669 NO_DEFER_POP;
1671 /* Save the stack with nonlocal if available. */
1672 if (targetm.have_save_stack_nonlocal ())
1673 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1674 else
1675 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1677 /* Allocate a block of memory onto the stack and copy the memory
1678 arguments to the outgoing arguments address. We can pass TRUE
1679 as the 4th argument because we just saved the stack pointer
1680 and will restore it right after the call. */
1681 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1683 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1684 may have already set current_function_calls_alloca to true.
1685 current_function_calls_alloca won't be set if argsize is zero,
1686 so we have to guarantee need_drap is true here. */
1687 if (SUPPORTS_STACK_ALIGNMENT)
1688 crtl->need_drap = true;
1690 dest = virtual_outgoing_args_rtx;
1691 if (!STACK_GROWS_DOWNWARD)
1693 if (CONST_INT_P (argsize))
1694 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1695 else
1696 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1698 dest = gen_rtx_MEM (BLKmode, dest);
1699 set_mem_align (dest, PARM_BOUNDARY);
1700 src = gen_rtx_MEM (BLKmode, incoming_args);
1701 set_mem_align (src, PARM_BOUNDARY);
1702 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1704 /* Refer to the argument block. */
1705 apply_args_size ();
1706 arguments = gen_rtx_MEM (BLKmode, arguments);
1707 set_mem_align (arguments, PARM_BOUNDARY);
1709 /* Walk past the arg-pointer and structure value address. */
1710 size = GET_MODE_SIZE (Pmode);
1711 if (struct_value)
1712 size += GET_MODE_SIZE (Pmode);
1714 /* Restore each of the registers previously saved. Make USE insns
1715 for each of these registers for use in making the call. */
1716 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1717 if ((mode = apply_args_mode[regno]) != VOIDmode)
1719 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1720 if (size % align != 0)
1721 size = CEIL (size, align) * align;
1722 reg = gen_rtx_REG (mode, regno);
1723 emit_move_insn (reg, adjust_address (arguments, mode, size));
1724 use_reg (&call_fusage, reg);
1725 size += GET_MODE_SIZE (mode);
1728 /* Restore the structure value address unless this is passed as an
1729 "invisible" first argument. */
1730 size = GET_MODE_SIZE (Pmode);
1731 if (struct_value)
1733 rtx value = gen_reg_rtx (Pmode);
1734 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1735 emit_move_insn (struct_value, value);
1736 if (REG_P (struct_value))
1737 use_reg (&call_fusage, struct_value);
1740 /* All arguments and registers used for the call are set up by now! */
1741 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1743 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1744 and we don't want to load it into a register as an optimization,
1745 because prepare_call_address already did it if it should be done. */
1746 if (GET_CODE (function) != SYMBOL_REF)
1747 function = memory_address (FUNCTION_MODE, function);
1749 /* Generate the actual call instruction and save the return value. */
1750 if (targetm.have_untyped_call ())
1752 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1753 rtx_insn *seq = targetm.gen_untyped_call (mem, result,
1754 result_vector (1, result));
1755 for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
1756 if (CALL_P (insn))
1757 add_reg_note (insn, REG_UNTYPED_CALL, NULL_RTX);
1758 emit_insn (seq);
1760 else if (targetm.have_call_value ())
1762 rtx valreg = 0;
1764 /* Locate the unique return register. It is not possible to
1765 express a call that sets more than one return register using
1766 call_value; use untyped_call for that. In fact, untyped_call
1767 only needs to save the return registers in the given block. */
1768 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1769 if ((mode = apply_result_mode[regno]) != VOIDmode)
1771 gcc_assert (!valreg); /* have_untyped_call required. */
1773 valreg = gen_rtx_REG (mode, regno);
1776 emit_insn (targetm.gen_call_value (valreg,
1777 gen_rtx_MEM (FUNCTION_MODE, function),
1778 const0_rtx, NULL_RTX, const0_rtx));
1780 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1782 else
1783 gcc_unreachable ();
1785 /* Find the CALL insn we just emitted, and attach the register usage
1786 information. */
1787 call_insn = last_call_insn ();
1788 add_function_usage_to (call_insn, call_fusage);
1790 /* Restore the stack. */
1791 if (targetm.have_save_stack_nonlocal ())
1792 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1793 else
1794 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1795 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1797 OK_DEFER_POP;
1799 /* Return the address of the result block. */
1800 result = copy_addr_to_reg (XEXP (result, 0));
1801 return convert_memory_address (ptr_mode, result);
1804 /* Perform an untyped return. */
1806 static void
1807 expand_builtin_return (rtx result)
1809 int size, align, regno;
1810 fixed_size_mode mode;
1811 rtx reg;
1812 rtx_insn *call_fusage = 0;
1814 result = convert_memory_address (Pmode, result);
1816 apply_result_size ();
1817 result = gen_rtx_MEM (BLKmode, result);
1819 if (targetm.have_untyped_return ())
1821 rtx vector = result_vector (0, result);
1822 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1823 emit_barrier ();
1824 return;
1827 /* Restore the return value and note that each value is used. */
1828 size = 0;
1829 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1830 if ((mode = apply_result_mode[regno]) != VOIDmode)
1832 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1833 if (size % align != 0)
1834 size = CEIL (size, align) * align;
1835 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1836 emit_move_insn (reg, adjust_address (result, mode, size));
1838 push_to_sequence (call_fusage);
1839 emit_use (reg);
1840 call_fusage = get_insns ();
1841 end_sequence ();
1842 size += GET_MODE_SIZE (mode);
1845 /* Put the USE insns before the return. */
1846 emit_insn (call_fusage);
1848 /* Return whatever values was restored by jumping directly to the end
1849 of the function. */
1850 expand_naked_return ();
1853 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1855 static enum type_class
1856 type_to_class (tree type)
1858 switch (TREE_CODE (type))
1860 case VOID_TYPE: return void_type_class;
1861 case INTEGER_TYPE: return integer_type_class;
1862 case ENUMERAL_TYPE: return enumeral_type_class;
1863 case BOOLEAN_TYPE: return boolean_type_class;
1864 case POINTER_TYPE: return pointer_type_class;
1865 case REFERENCE_TYPE: return reference_type_class;
1866 case OFFSET_TYPE: return offset_type_class;
1867 case REAL_TYPE: return real_type_class;
1868 case COMPLEX_TYPE: return complex_type_class;
1869 case FUNCTION_TYPE: return function_type_class;
1870 case METHOD_TYPE: return method_type_class;
1871 case RECORD_TYPE: return record_type_class;
1872 case UNION_TYPE:
1873 case QUAL_UNION_TYPE: return union_type_class;
1874 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1875 ? string_type_class : array_type_class);
1876 case LANG_TYPE: return lang_type_class;
1877 case OPAQUE_TYPE: return opaque_type_class;
1878 default: return no_type_class;
1882 /* Expand a call EXP to __builtin_classify_type. */
1884 static rtx
1885 expand_builtin_classify_type (tree exp)
1887 if (call_expr_nargs (exp))
1888 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1889 return GEN_INT (no_type_class);
1892 /* This helper macro, meant to be used in mathfn_built_in below, determines
1893 which among a set of builtin math functions is appropriate for a given type
1894 mode. The `F' (float) and `L' (long double) are automatically generated
1895 from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1896 types, there are additional types that are considered with 'F32', 'F64',
1897 'F128', etc. suffixes. */
1898 #define CASE_MATHFN(MATHFN) \
1899 CASE_CFN_##MATHFN: \
1900 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1901 fcodel = BUILT_IN_##MATHFN##L ; break;
1902 /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1903 types. */
1904 #define CASE_MATHFN_FLOATN(MATHFN) \
1905 CASE_CFN_##MATHFN: \
1906 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1907 fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1908 fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1909 fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1910 fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1911 break;
1912 /* Similar to above, but appends _R after any F/L suffix. */
1913 #define CASE_MATHFN_REENT(MATHFN) \
1914 case CFN_BUILT_IN_##MATHFN##_R: \
1915 case CFN_BUILT_IN_##MATHFN##F_R: \
1916 case CFN_BUILT_IN_##MATHFN##L_R: \
1917 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1918 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1920 /* Return a function equivalent to FN but operating on floating-point
1921 values of type TYPE, or END_BUILTINS if no such function exists.
1922 This is purely an operation on function codes; it does not guarantee
1923 that the target actually has an implementation of the function. */
1925 static built_in_function
1926 mathfn_built_in_2 (tree type, combined_fn fn)
1928 tree mtype;
1929 built_in_function fcode, fcodef, fcodel;
1930 built_in_function fcodef16 = END_BUILTINS;
1931 built_in_function fcodef32 = END_BUILTINS;
1932 built_in_function fcodef64 = END_BUILTINS;
1933 built_in_function fcodef128 = END_BUILTINS;
1934 built_in_function fcodef32x = END_BUILTINS;
1935 built_in_function fcodef64x = END_BUILTINS;
1936 built_in_function fcodef128x = END_BUILTINS;
1938 /* If <math.h> has been included somehow, HUGE_VAL and NAN definitions
1939 break the uses below. */
1940 #undef HUGE_VAL
1941 #undef NAN
1943 switch (fn)
1945 #define SEQ_OF_CASE_MATHFN \
1946 CASE_MATHFN_FLOATN (ACOS) \
1947 CASE_MATHFN_FLOATN (ACOSH) \
1948 CASE_MATHFN_FLOATN (ASIN) \
1949 CASE_MATHFN_FLOATN (ASINH) \
1950 CASE_MATHFN_FLOATN (ATAN) \
1951 CASE_MATHFN_FLOATN (ATAN2) \
1952 CASE_MATHFN_FLOATN (ATANH) \
1953 CASE_MATHFN_FLOATN (CBRT) \
1954 CASE_MATHFN_FLOATN (CEIL) \
1955 CASE_MATHFN (CEXPI) \
1956 CASE_MATHFN_FLOATN (COPYSIGN) \
1957 CASE_MATHFN_FLOATN (COS) \
1958 CASE_MATHFN_FLOATN (COSH) \
1959 CASE_MATHFN (DREM) \
1960 CASE_MATHFN_FLOATN (ERF) \
1961 CASE_MATHFN_FLOATN (ERFC) \
1962 CASE_MATHFN_FLOATN (EXP) \
1963 CASE_MATHFN (EXP10) \
1964 CASE_MATHFN_FLOATN (EXP2) \
1965 CASE_MATHFN_FLOATN (EXPM1) \
1966 CASE_MATHFN_FLOATN (FABS) \
1967 CASE_MATHFN_FLOATN (FDIM) \
1968 CASE_MATHFN_FLOATN (FLOOR) \
1969 CASE_MATHFN_FLOATN (FMA) \
1970 CASE_MATHFN_FLOATN (FMAX) \
1971 CASE_MATHFN_FLOATN (FMIN) \
1972 CASE_MATHFN_FLOATN (FMOD) \
1973 CASE_MATHFN_FLOATN (FREXP) \
1974 CASE_MATHFN (GAMMA) \
1975 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ \
1976 CASE_MATHFN_FLOATN (HUGE_VAL) \
1977 CASE_MATHFN_FLOATN (HYPOT) \
1978 CASE_MATHFN_FLOATN (ILOGB) \
1979 CASE_MATHFN (ICEIL) \
1980 CASE_MATHFN (IFLOOR) \
1981 CASE_MATHFN_FLOATN (INF) \
1982 CASE_MATHFN (IRINT) \
1983 CASE_MATHFN (IROUND) \
1984 CASE_MATHFN (ISINF) \
1985 CASE_MATHFN (J0) \
1986 CASE_MATHFN (J1) \
1987 CASE_MATHFN (JN) \
1988 CASE_MATHFN (LCEIL) \
1989 CASE_MATHFN_FLOATN (LDEXP) \
1990 CASE_MATHFN (LFLOOR) \
1991 CASE_MATHFN_FLOATN (LGAMMA) \
1992 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ \
1993 CASE_MATHFN (LLCEIL) \
1994 CASE_MATHFN (LLFLOOR) \
1995 CASE_MATHFN_FLOATN (LLRINT) \
1996 CASE_MATHFN_FLOATN (LLROUND) \
1997 CASE_MATHFN_FLOATN (LOG) \
1998 CASE_MATHFN_FLOATN (LOG10) \
1999 CASE_MATHFN_FLOATN (LOG1P) \
2000 CASE_MATHFN_FLOATN (LOG2) \
2001 CASE_MATHFN_FLOATN (LOGB) \
2002 CASE_MATHFN_FLOATN (LRINT) \
2003 CASE_MATHFN_FLOATN (LROUND) \
2004 CASE_MATHFN_FLOATN (MODF) \
2005 CASE_MATHFN_FLOATN (NAN) \
2006 CASE_MATHFN_FLOATN (NANS) \
2007 CASE_MATHFN_FLOATN (NEARBYINT) \
2008 CASE_MATHFN_FLOATN (NEXTAFTER) \
2009 CASE_MATHFN (NEXTTOWARD) \
2010 CASE_MATHFN_FLOATN (POW) \
2011 CASE_MATHFN (POWI) \
2012 CASE_MATHFN (POW10) \
2013 CASE_MATHFN_FLOATN (REMAINDER) \
2014 CASE_MATHFN_FLOATN (REMQUO) \
2015 CASE_MATHFN_FLOATN (RINT) \
2016 CASE_MATHFN_FLOATN (ROUND) \
2017 CASE_MATHFN_FLOATN (ROUNDEVEN) \
2018 CASE_MATHFN (SCALB) \
2019 CASE_MATHFN_FLOATN (SCALBLN) \
2020 CASE_MATHFN_FLOATN (SCALBN) \
2021 CASE_MATHFN (SIGNBIT) \
2022 CASE_MATHFN (SIGNIFICAND) \
2023 CASE_MATHFN_FLOATN (SIN) \
2024 CASE_MATHFN (SINCOS) \
2025 CASE_MATHFN_FLOATN (SINH) \
2026 CASE_MATHFN_FLOATN (SQRT) \
2027 CASE_MATHFN_FLOATN (TAN) \
2028 CASE_MATHFN_FLOATN (TANH) \
2029 CASE_MATHFN_FLOATN (TGAMMA) \
2030 CASE_MATHFN_FLOATN (TRUNC) \
2031 CASE_MATHFN (Y0) \
2032 CASE_MATHFN (Y1) \
2033 CASE_MATHFN (YN)
2035 SEQ_OF_CASE_MATHFN
2037 default:
2038 return END_BUILTINS;
2041 mtype = TYPE_MAIN_VARIANT (type);
2042 if (mtype == double_type_node)
2043 return fcode;
2044 else if (mtype == float_type_node)
2045 return fcodef;
2046 else if (mtype == long_double_type_node)
2047 return fcodel;
2048 else if (mtype == float16_type_node)
2049 return fcodef16;
2050 else if (mtype == float32_type_node)
2051 return fcodef32;
2052 else if (mtype == float64_type_node)
2053 return fcodef64;
2054 else if (mtype == float128_type_node)
2055 return fcodef128;
2056 else if (mtype == float32x_type_node)
2057 return fcodef32x;
2058 else if (mtype == float64x_type_node)
2059 return fcodef64x;
2060 else if (mtype == float128x_type_node)
2061 return fcodef128x;
2062 else
2063 return END_BUILTINS;
2066 #undef CASE_MATHFN
2067 #undef CASE_MATHFN_FLOATN
2068 #undef CASE_MATHFN_REENT
2070 /* Return mathematic function equivalent to FN but operating directly on TYPE,
2071 if available. If IMPLICIT_P is true use the implicit builtin declaration,
2072 otherwise use the explicit declaration. If we can't do the conversion,
2073 return null. */
2075 static tree
2076 mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2078 built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2079 if (fcode2 == END_BUILTINS)
2080 return NULL_TREE;
2082 if (implicit_p && !builtin_decl_implicit_p (fcode2))
2083 return NULL_TREE;
2085 return builtin_decl_explicit (fcode2);
2088 /* Like mathfn_built_in_1, but always use the implicit array. */
2090 tree
2091 mathfn_built_in (tree type, combined_fn fn)
2093 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2096 /* Like mathfn_built_in_1, but always use the explicit array. */
2098 tree
2099 mathfn_built_in_explicit (tree type, combined_fn fn)
2101 return mathfn_built_in_1 (type, fn, /*implicit=*/ 0);
2104 /* Like mathfn_built_in_1, but take a built_in_function and
2105 always use the implicit array. */
2107 tree
2108 mathfn_built_in (tree type, enum built_in_function fn)
2110 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2113 /* Return the type associated with a built in function, i.e., the one
2114 to be passed to mathfn_built_in to get the type-specific
2115 function. */
2117 tree
2118 mathfn_built_in_type (combined_fn fn)
2120 #define CASE_MATHFN(MATHFN) \
2121 case CFN_BUILT_IN_##MATHFN: \
2122 return double_type_node; \
2123 case CFN_BUILT_IN_##MATHFN##F: \
2124 return float_type_node; \
2125 case CFN_BUILT_IN_##MATHFN##L: \
2126 return long_double_type_node;
2128 #define CASE_MATHFN_FLOATN(MATHFN) \
2129 CASE_MATHFN(MATHFN) \
2130 case CFN_BUILT_IN_##MATHFN##F16: \
2131 return float16_type_node; \
2132 case CFN_BUILT_IN_##MATHFN##F32: \
2133 return float32_type_node; \
2134 case CFN_BUILT_IN_##MATHFN##F64: \
2135 return float64_type_node; \
2136 case CFN_BUILT_IN_##MATHFN##F128: \
2137 return float128_type_node; \
2138 case CFN_BUILT_IN_##MATHFN##F32X: \
2139 return float32x_type_node; \
2140 case CFN_BUILT_IN_##MATHFN##F64X: \
2141 return float64x_type_node; \
2142 case CFN_BUILT_IN_##MATHFN##F128X: \
2143 return float128x_type_node;
2145 /* Similar to above, but appends _R after any F/L suffix. */
2146 #define CASE_MATHFN_REENT(MATHFN) \
2147 case CFN_BUILT_IN_##MATHFN##_R: \
2148 return double_type_node; \
2149 case CFN_BUILT_IN_##MATHFN##F_R: \
2150 return float_type_node; \
2151 case CFN_BUILT_IN_##MATHFN##L_R: \
2152 return long_double_type_node;
2154 switch (fn)
2156 SEQ_OF_CASE_MATHFN
2158 default:
2159 return NULL_TREE;
2162 #undef CASE_MATHFN
2163 #undef CASE_MATHFN_FLOATN
2164 #undef CASE_MATHFN_REENT
2165 #undef SEQ_OF_CASE_MATHFN
2168 /* Check whether there is an internal function associated with function FN
2169 and return type RETURN_TYPE. Return the function if so, otherwise return
2170 IFN_LAST.
2172 Note that this function only tests whether the function is defined in
2173 internals.def, not whether it is actually available on the target. */
2175 static internal_fn
2176 associated_internal_fn (built_in_function fn, tree return_type)
2178 switch (fn)
2180 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2181 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2182 #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2183 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2184 CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2185 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2186 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2187 #include "internal-fn.def"
2189 CASE_FLT_FN (BUILT_IN_POW10):
2190 return IFN_EXP10;
2192 CASE_FLT_FN (BUILT_IN_DREM):
2193 return IFN_REMAINDER;
2195 CASE_FLT_FN (BUILT_IN_SCALBN):
2196 CASE_FLT_FN (BUILT_IN_SCALBLN):
2197 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2198 return IFN_LDEXP;
2199 return IFN_LAST;
2201 default:
2202 return IFN_LAST;
2206 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2207 return its code, otherwise return IFN_LAST. Note that this function
2208 only tests whether the function is defined in internals.def, not whether
2209 it is actually available on the target. */
2211 internal_fn
2212 associated_internal_fn (tree fndecl)
2214 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2215 return associated_internal_fn (DECL_FUNCTION_CODE (fndecl),
2216 TREE_TYPE (TREE_TYPE (fndecl)));
2219 /* Check whether there is an internal function associated with function CFN
2220 and return type RETURN_TYPE. Return the function if so, otherwise return
2221 IFN_LAST.
2223 Note that this function only tests whether the function is defined in
2224 internals.def, not whether it is actually available on the target. */
2226 internal_fn
2227 associated_internal_fn (combined_fn cfn, tree return_type)
2229 if (internal_fn_p (cfn))
2230 return as_internal_fn (cfn);
2231 return associated_internal_fn (as_builtin_fn (cfn), return_type);
2234 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2235 on the current target by a call to an internal function, return the
2236 code of that internal function, otherwise return IFN_LAST. The caller
2237 is responsible for ensuring that any side-effects of the built-in
2238 call are dealt with correctly. E.g. if CALL sets errno, the caller
2239 must decide that the errno result isn't needed or make it available
2240 in some other way. */
2242 internal_fn
2243 replacement_internal_fn (gcall *call)
2245 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2247 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2248 if (ifn != IFN_LAST)
2250 tree_pair types = direct_internal_fn_types (ifn, call);
2251 optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2252 if (direct_internal_fn_supported_p (ifn, types, opt_type))
2253 return ifn;
2256 return IFN_LAST;
2259 /* Expand a call to the builtin trinary math functions (fma).
2260 Return NULL_RTX if a normal call should be emitted rather than expanding the
2261 function in-line. EXP is the expression that is a call to the builtin
2262 function; if convenient, the result should be placed in TARGET.
2263 SUBTARGET may be used as the target for computing one of EXP's
2264 operands. */
2266 static rtx
2267 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2269 optab builtin_optab;
2270 rtx op0, op1, op2, result;
2271 rtx_insn *insns;
2272 tree fndecl = get_callee_fndecl (exp);
2273 tree arg0, arg1, arg2;
2274 machine_mode mode;
2276 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2277 return NULL_RTX;
2279 arg0 = CALL_EXPR_ARG (exp, 0);
2280 arg1 = CALL_EXPR_ARG (exp, 1);
2281 arg2 = CALL_EXPR_ARG (exp, 2);
2283 switch (DECL_FUNCTION_CODE (fndecl))
2285 CASE_FLT_FN (BUILT_IN_FMA):
2286 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2287 builtin_optab = fma_optab; break;
2288 default:
2289 gcc_unreachable ();
2292 /* Make a suitable register to place result in. */
2293 mode = TYPE_MODE (TREE_TYPE (exp));
2295 /* Before working hard, check whether the instruction is available. */
2296 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2297 return NULL_RTX;
2299 result = gen_reg_rtx (mode);
2301 /* Always stabilize the argument list. */
2302 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2303 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2304 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2306 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2307 op1 = expand_normal (arg1);
2308 op2 = expand_normal (arg2);
2310 start_sequence ();
2312 /* Compute into RESULT.
2313 Set RESULT to wherever the result comes back. */
2314 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2315 result, 0);
2317 /* If we were unable to expand via the builtin, stop the sequence
2318 (without outputting the insns) and call to the library function
2319 with the stabilized argument list. */
2320 if (result == 0)
2322 end_sequence ();
2323 return expand_call (exp, target, target == const0_rtx);
2326 /* Output the entire sequence. */
2327 insns = get_insns ();
2328 end_sequence ();
2329 emit_insn (insns);
2331 return result;
2334 /* Expand a call to the builtin sin and cos math functions.
2335 Return NULL_RTX if a normal call should be emitted rather than expanding the
2336 function in-line. EXP is the expression that is a call to the builtin
2337 function; if convenient, the result should be placed in TARGET.
2338 SUBTARGET may be used as the target for computing one of EXP's
2339 operands. */
2341 static rtx
2342 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2344 optab builtin_optab;
2345 rtx op0;
2346 rtx_insn *insns;
2347 tree fndecl = get_callee_fndecl (exp);
2348 machine_mode mode;
2349 tree arg;
2351 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2352 return NULL_RTX;
2354 arg = CALL_EXPR_ARG (exp, 0);
2356 switch (DECL_FUNCTION_CODE (fndecl))
2358 CASE_FLT_FN (BUILT_IN_SIN):
2359 CASE_FLT_FN (BUILT_IN_COS):
2360 builtin_optab = sincos_optab; break;
2361 default:
2362 gcc_unreachable ();
2365 /* Make a suitable register to place result in. */
2366 mode = TYPE_MODE (TREE_TYPE (exp));
2368 /* Check if sincos insn is available, otherwise fallback
2369 to sin or cos insn. */
2370 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2371 switch (DECL_FUNCTION_CODE (fndecl))
2373 CASE_FLT_FN (BUILT_IN_SIN):
2374 builtin_optab = sin_optab; break;
2375 CASE_FLT_FN (BUILT_IN_COS):
2376 builtin_optab = cos_optab; break;
2377 default:
2378 gcc_unreachable ();
2381 /* Before working hard, check whether the instruction is available. */
2382 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2384 rtx result = gen_reg_rtx (mode);
2386 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2387 need to expand the argument again. This way, we will not perform
2388 side-effects more the once. */
2389 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2391 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2393 start_sequence ();
2395 /* Compute into RESULT.
2396 Set RESULT to wherever the result comes back. */
2397 if (builtin_optab == sincos_optab)
2399 int ok;
2401 switch (DECL_FUNCTION_CODE (fndecl))
2403 CASE_FLT_FN (BUILT_IN_SIN):
2404 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2405 break;
2406 CASE_FLT_FN (BUILT_IN_COS):
2407 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2408 break;
2409 default:
2410 gcc_unreachable ();
2412 gcc_assert (ok);
2414 else
2415 result = expand_unop (mode, builtin_optab, op0, result, 0);
2417 if (result != 0)
2419 /* Output the entire sequence. */
2420 insns = get_insns ();
2421 end_sequence ();
2422 emit_insn (insns);
2423 return result;
2426 /* If we were unable to expand via the builtin, stop the sequence
2427 (without outputting the insns) and call to the library function
2428 with the stabilized argument list. */
2429 end_sequence ();
2432 return expand_call (exp, target, target == const0_rtx);
2435 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2436 return an RTL instruction code that implements the functionality.
2437 If that isn't possible or available return CODE_FOR_nothing. */
2439 static enum insn_code
2440 interclass_mathfn_icode (tree arg, tree fndecl)
2442 bool errno_set = false;
2443 optab builtin_optab = unknown_optab;
2444 machine_mode mode;
2446 switch (DECL_FUNCTION_CODE (fndecl))
2448 CASE_FLT_FN (BUILT_IN_ILOGB):
2449 errno_set = true; builtin_optab = ilogb_optab; break;
2450 CASE_FLT_FN (BUILT_IN_ISINF):
2451 builtin_optab = isinf_optab; break;
2452 case BUILT_IN_ISNORMAL:
2453 case BUILT_IN_ISFINITE:
2454 CASE_FLT_FN (BUILT_IN_FINITE):
2455 case BUILT_IN_FINITED32:
2456 case BUILT_IN_FINITED64:
2457 case BUILT_IN_FINITED128:
2458 case BUILT_IN_ISINFD32:
2459 case BUILT_IN_ISINFD64:
2460 case BUILT_IN_ISINFD128:
2461 /* These builtins have no optabs (yet). */
2462 break;
2463 default:
2464 gcc_unreachable ();
2467 /* There's no easy way to detect the case we need to set EDOM. */
2468 if (flag_errno_math && errno_set)
2469 return CODE_FOR_nothing;
2471 /* Optab mode depends on the mode of the input argument. */
2472 mode = TYPE_MODE (TREE_TYPE (arg));
2474 if (builtin_optab)
2475 return optab_handler (builtin_optab, mode);
2476 return CODE_FOR_nothing;
2479 /* Expand a call to one of the builtin math functions that operate on
2480 floating point argument and output an integer result (ilogb, isinf,
2481 isnan, etc).
2482 Return 0 if a normal call should be emitted rather than expanding the
2483 function in-line. EXP is the expression that is a call to the builtin
2484 function; if convenient, the result should be placed in TARGET. */
2486 static rtx
2487 expand_builtin_interclass_mathfn (tree exp, rtx target)
2489 enum insn_code icode = CODE_FOR_nothing;
2490 rtx op0;
2491 tree fndecl = get_callee_fndecl (exp);
2492 machine_mode mode;
2493 tree arg;
2495 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2496 return NULL_RTX;
2498 arg = CALL_EXPR_ARG (exp, 0);
2499 icode = interclass_mathfn_icode (arg, fndecl);
2500 mode = TYPE_MODE (TREE_TYPE (arg));
2502 if (icode != CODE_FOR_nothing)
2504 class expand_operand ops[1];
2505 rtx_insn *last = get_last_insn ();
2506 tree orig_arg = arg;
2508 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2509 need to expand the argument again. This way, we will not perform
2510 side-effects more the once. */
2511 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2513 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2515 if (mode != GET_MODE (op0))
2516 op0 = convert_to_mode (mode, op0, 0);
2518 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2519 if (maybe_legitimize_operands (icode, 0, 1, ops)
2520 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2521 return ops[0].value;
2523 delete_insns_since (last);
2524 CALL_EXPR_ARG (exp, 0) = orig_arg;
2527 return NULL_RTX;
2530 /* Expand a call to the builtin sincos math function.
2531 Return NULL_RTX if a normal call should be emitted rather than expanding the
2532 function in-line. EXP is the expression that is a call to the builtin
2533 function. */
2535 static rtx
2536 expand_builtin_sincos (tree exp)
2538 rtx op0, op1, op2, target1, target2;
2539 machine_mode mode;
2540 tree arg, sinp, cosp;
2541 int result;
2542 location_t loc = EXPR_LOCATION (exp);
2543 tree alias_type, alias_off;
2545 if (!validate_arglist (exp, REAL_TYPE,
2546 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2547 return NULL_RTX;
2549 arg = CALL_EXPR_ARG (exp, 0);
2550 sinp = CALL_EXPR_ARG (exp, 1);
2551 cosp = CALL_EXPR_ARG (exp, 2);
2553 /* Make a suitable register to place result in. */
2554 mode = TYPE_MODE (TREE_TYPE (arg));
2556 /* Check if sincos insn is available, otherwise emit the call. */
2557 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2558 return NULL_RTX;
2560 target1 = gen_reg_rtx (mode);
2561 target2 = gen_reg_rtx (mode);
2563 op0 = expand_normal (arg);
2564 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2565 alias_off = build_int_cst (alias_type, 0);
2566 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2567 sinp, alias_off));
2568 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2569 cosp, alias_off));
2571 /* Compute into target1 and target2.
2572 Set TARGET to wherever the result comes back. */
2573 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2574 gcc_assert (result);
2576 /* Move target1 and target2 to the memory locations indicated
2577 by op1 and op2. */
2578 emit_move_insn (op1, target1);
2579 emit_move_insn (op2, target2);
2581 return const0_rtx;
2584 /* Expand call EXP to the fegetround builtin (from C99 fenv.h), returning the
2585 result and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2586 static rtx
2587 expand_builtin_fegetround (tree exp, rtx target, machine_mode target_mode)
2589 if (!validate_arglist (exp, VOID_TYPE))
2590 return NULL_RTX;
2592 insn_code icode = direct_optab_handler (fegetround_optab, SImode);
2593 if (icode == CODE_FOR_nothing)
2594 return NULL_RTX;
2596 if (target == 0
2597 || GET_MODE (target) != target_mode
2598 || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2599 target = gen_reg_rtx (target_mode);
2601 rtx pat = GEN_FCN (icode) (target);
2602 if (!pat)
2603 return NULL_RTX;
2604 emit_insn (pat);
2606 return target;
2609 /* Expand call EXP to either feclearexcept or feraiseexcept builtins (from C99
2610 fenv.h), returning the result and setting it in TARGET. Otherwise return
2611 NULL_RTX on failure. */
2612 static rtx
2613 expand_builtin_feclear_feraise_except (tree exp, rtx target,
2614 machine_mode target_mode, optab op_optab)
2616 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
2617 return NULL_RTX;
2618 rtx op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
2620 insn_code icode = direct_optab_handler (op_optab, SImode);
2621 if (icode == CODE_FOR_nothing)
2622 return NULL_RTX;
2624 if (!(*insn_data[icode].operand[1].predicate) (op0, GET_MODE (op0)))
2625 return NULL_RTX;
2627 if (target == 0
2628 || GET_MODE (target) != target_mode
2629 || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2630 target = gen_reg_rtx (target_mode);
2632 rtx pat = GEN_FCN (icode) (target, op0);
2633 if (!pat)
2634 return NULL_RTX;
2635 emit_insn (pat);
2637 return target;
2640 /* Expand a call to the internal cexpi builtin to the sincos math function.
2641 EXP is the expression that is a call to the builtin function; if convenient,
2642 the result should be placed in TARGET. */
2644 static rtx
2645 expand_builtin_cexpi (tree exp, rtx target)
2647 tree fndecl = get_callee_fndecl (exp);
2648 tree arg, type;
2649 machine_mode mode;
2650 rtx op0, op1, op2;
2651 location_t loc = EXPR_LOCATION (exp);
2653 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2654 return NULL_RTX;
2656 arg = CALL_EXPR_ARG (exp, 0);
2657 type = TREE_TYPE (arg);
2658 mode = TYPE_MODE (TREE_TYPE (arg));
2660 /* Try expanding via a sincos optab, fall back to emitting a libcall
2661 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2662 is only generated from sincos, cexp or if we have either of them. */
2663 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2665 op1 = gen_reg_rtx (mode);
2666 op2 = gen_reg_rtx (mode);
2668 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2670 /* Compute into op1 and op2. */
2671 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2673 else if (targetm.libc_has_function (function_sincos, type))
2675 tree call, fn = NULL_TREE;
2676 tree top1, top2;
2677 rtx op1a, op2a;
2679 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2680 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2681 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2682 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2683 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2684 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2685 else
2686 gcc_unreachable ();
2688 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2689 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2690 op1a = copy_addr_to_reg (XEXP (op1, 0));
2691 op2a = copy_addr_to_reg (XEXP (op2, 0));
2692 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2693 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2695 /* Make sure not to fold the sincos call again. */
2696 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2697 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2698 call, 3, arg, top1, top2));
2700 else
2702 tree call, fn = NULL_TREE, narg;
2703 tree ctype = build_complex_type (type);
2705 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2706 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2707 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2708 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2709 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2710 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2711 else
2712 gcc_unreachable ();
2714 /* If we don't have a decl for cexp create one. This is the
2715 friendliest fallback if the user calls __builtin_cexpi
2716 without full target C99 function support. */
2717 if (fn == NULL_TREE)
2719 tree fntype;
2720 const char *name = NULL;
2722 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2723 name = "cexpf";
2724 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2725 name = "cexp";
2726 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2727 name = "cexpl";
2729 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2730 fn = build_fn_decl (name, fntype);
2733 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2734 build_real (type, dconst0), arg);
2736 /* Make sure not to fold the cexp call again. */
2737 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2738 return expand_expr (build_call_nary (ctype, call, 1, narg),
2739 target, VOIDmode, EXPAND_NORMAL);
2742 /* Now build the proper return type. */
2743 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2744 make_tree (TREE_TYPE (arg), op2),
2745 make_tree (TREE_TYPE (arg), op1)),
2746 target, VOIDmode, EXPAND_NORMAL);
2749 /* Conveniently construct a function call expression. FNDECL names the
2750 function to be called, N is the number of arguments, and the "..."
2751 parameters are the argument expressions. Unlike build_call_exr
2752 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2754 static tree
2755 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2757 va_list ap;
2758 tree fntype = TREE_TYPE (fndecl);
2759 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2761 va_start (ap, n);
2762 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2763 va_end (ap);
2764 SET_EXPR_LOCATION (fn, loc);
2765 return fn;
2768 /* Expand the __builtin_issignaling builtin. This needs to handle
2769 all floating point formats that do support NaNs (for those that
2770 don't it just sets target to 0). */
2772 static rtx
2773 expand_builtin_issignaling (tree exp, rtx target)
2775 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2776 return NULL_RTX;
2778 tree arg = CALL_EXPR_ARG (exp, 0);
2779 scalar_float_mode fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
2780 const struct real_format *fmt = REAL_MODE_FORMAT (fmode);
2782 /* Expand the argument yielding a RTX expression. */
2783 rtx temp = expand_normal (arg);
2785 /* If mode doesn't support NaN, always return 0.
2786 Don't use !HONOR_SNANS (fmode) here, so there is some possibility of
2787 __builtin_issignaling working without -fsignaling-nans. Especially
2788 when -fno-signaling-nans is the default.
2789 On the other side, MODE_HAS_NANS (fmode) is unnecessary, with
2790 -ffinite-math-only even __builtin_isnan or __builtin_fpclassify
2791 fold to 0 or non-NaN/Inf classification. */
2792 if (!HONOR_NANS (fmode))
2794 emit_move_insn (target, const0_rtx);
2795 return target;
2798 /* Check if the back end provides an insn that handles issignaling for the
2799 argument's mode. */
2800 enum insn_code icode = optab_handler (issignaling_optab, fmode);
2801 if (icode != CODE_FOR_nothing)
2803 rtx_insn *last = get_last_insn ();
2804 rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2805 if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
2806 return this_target;
2807 delete_insns_since (last);
2810 if (DECIMAL_FLOAT_MODE_P (fmode))
2812 scalar_int_mode imode;
2813 rtx hi;
2814 switch (fmt->ieee_bits)
2816 case 32:
2817 case 64:
2818 imode = int_mode_for_mode (fmode).require ();
2819 temp = gen_lowpart (imode, temp);
2820 break;
2821 case 128:
2822 imode = int_mode_for_size (64, 1).require ();
2823 hi = NULL_RTX;
2824 /* For decimal128, TImode support isn't always there and even when
2825 it is, working on the DImode high part is usually better. */
2826 if (!MEM_P (temp))
2828 if (rtx t = simplify_gen_subreg (imode, temp, fmode,
2829 subreg_highpart_offset (imode,
2830 fmode)))
2831 hi = t;
2832 else
2834 scalar_int_mode imode2;
2835 if (int_mode_for_mode (fmode).exists (&imode2))
2837 rtx temp2 = gen_lowpart (imode2, temp);
2838 poly_uint64 off = subreg_highpart_offset (imode, imode2);
2839 if (rtx t = simplify_gen_subreg (imode, temp2,
2840 imode2, off))
2841 hi = t;
2844 if (!hi)
2846 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2847 emit_move_insn (mem, temp);
2848 temp = mem;
2851 if (!hi)
2853 poly_int64 offset
2854 = subreg_highpart_offset (imode, GET_MODE (temp));
2855 hi = adjust_address (temp, imode, offset);
2857 temp = hi;
2858 break;
2859 default:
2860 gcc_unreachable ();
2862 /* In all of decimal{32,64,128}, there is MSB sign bit and sNaN
2863 have 6 bits below it all set. */
2864 rtx val
2865 = GEN_INT (HOST_WIDE_INT_C (0x3f) << (GET_MODE_BITSIZE (imode) - 7));
2866 temp = expand_binop (imode, and_optab, temp, val,
2867 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2868 temp = emit_store_flag_force (target, EQ, temp, val, imode, 1, 1);
2869 return temp;
2872 /* Only PDP11 has these defined differently but doesn't support NaNs. */
2873 gcc_assert (FLOAT_WORDS_BIG_ENDIAN == WORDS_BIG_ENDIAN);
2874 gcc_assert (fmt->signbit_ro > 0 && fmt->b == 2);
2875 gcc_assert (MODE_COMPOSITE_P (fmode)
2876 || (fmt->pnan == fmt->p
2877 && fmt->signbit_ro == fmt->signbit_rw));
2879 switch (fmt->p)
2881 case 106: /* IBM double double */
2882 /* For IBM double double, recurse on the most significant double. */
2883 gcc_assert (MODE_COMPOSITE_P (fmode));
2884 temp = convert_modes (DFmode, fmode, temp, 0);
2885 fmode = DFmode;
2886 fmt = REAL_MODE_FORMAT (DFmode);
2887 /* FALLTHRU */
2888 case 8: /* bfloat */
2889 case 11: /* IEEE half */
2890 case 24: /* IEEE single */
2891 case 53: /* IEEE double or Intel extended with rounding to double */
2892 if (fmt->p == 53 && fmt->signbit_ro == 79)
2893 goto extended;
2895 scalar_int_mode imode = int_mode_for_mode (fmode).require ();
2896 temp = gen_lowpart (imode, temp);
2897 rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2))
2898 & ~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2899 if (fmt->qnan_msb_set)
2901 rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2902 rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2));
2903 /* For non-MIPS/PA IEEE single/double/half or bfloat, expand to:
2904 ((temp ^ bit) & mask) > val. */
2905 temp = expand_binop (imode, xor_optab, temp, bit,
2906 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2907 temp = expand_binop (imode, and_optab, temp, mask,
2908 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2909 temp = emit_store_flag_force (target, GTU, temp, val, imode,
2910 1, 1);
2912 else
2914 /* For MIPS/PA IEEE single/double, expand to:
2915 (temp & val) == val. */
2916 temp = expand_binop (imode, and_optab, temp, val,
2917 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2918 temp = emit_store_flag_force (target, EQ, temp, val, imode,
2919 1, 1);
2922 break;
2923 case 113: /* IEEE quad */
2925 rtx hi = NULL_RTX, lo = NULL_RTX;
2926 scalar_int_mode imode = int_mode_for_size (64, 1).require ();
2927 /* For IEEE quad, TImode support isn't always there and even when
2928 it is, working on DImode parts is usually better. */
2929 if (!MEM_P (temp))
2931 hi = simplify_gen_subreg (imode, temp, fmode,
2932 subreg_highpart_offset (imode, fmode));
2933 lo = simplify_gen_subreg (imode, temp, fmode,
2934 subreg_lowpart_offset (imode, fmode));
2935 if (!hi || !lo)
2937 scalar_int_mode imode2;
2938 if (int_mode_for_mode (fmode).exists (&imode2))
2940 rtx temp2 = gen_lowpart (imode2, temp);
2941 hi = simplify_gen_subreg (imode, temp2, imode2,
2942 subreg_highpart_offset (imode,
2943 imode2));
2944 lo = simplify_gen_subreg (imode, temp2, imode2,
2945 subreg_lowpart_offset (imode,
2946 imode2));
2949 if (!hi || !lo)
2951 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2952 emit_move_insn (mem, temp);
2953 temp = mem;
2956 if (!hi || !lo)
2958 poly_int64 offset
2959 = subreg_highpart_offset (imode, GET_MODE (temp));
2960 hi = adjust_address (temp, imode, offset);
2961 offset = subreg_lowpart_offset (imode, GET_MODE (temp));
2962 lo = adjust_address (temp, imode, offset);
2964 rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2 - 64))
2965 & ~(HOST_WIDE_INT_M1U << (fmt->signbit_ro - 64)));
2966 if (fmt->qnan_msb_set)
2968 rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << (fmt->signbit_ro
2969 - 64)));
2970 rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2 - 64));
2971 /* For non-MIPS/PA IEEE quad, expand to:
2972 (((hi ^ bit) | ((lo | -lo) >> 63)) & mask) > val. */
2973 rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
2974 lo = expand_binop (imode, ior_optab, lo, nlo,
2975 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2976 lo = expand_shift (RSHIFT_EXPR, imode, lo, 63, NULL_RTX, 1);
2977 temp = expand_binop (imode, xor_optab, hi, bit,
2978 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2979 temp = expand_binop (imode, ior_optab, temp, lo,
2980 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2981 temp = expand_binop (imode, and_optab, temp, mask,
2982 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2983 temp = emit_store_flag_force (target, GTU, temp, val, imode,
2984 1, 1);
2986 else
2988 /* For MIPS/PA IEEE quad, expand to:
2989 (hi & val) == val. */
2990 temp = expand_binop (imode, and_optab, hi, val,
2991 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2992 temp = emit_store_flag_force (target, EQ, temp, val, imode,
2993 1, 1);
2996 break;
2997 case 64: /* Intel or Motorola extended */
2998 extended:
3000 rtx ex, hi, lo;
3001 scalar_int_mode imode = int_mode_for_size (32, 1).require ();
3002 scalar_int_mode iemode = int_mode_for_size (16, 1).require ();
3003 if (!MEM_P (temp))
3005 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
3006 emit_move_insn (mem, temp);
3007 temp = mem;
3009 if (fmt->signbit_ro == 95)
3011 /* Motorola, always big endian, with 16-bit gap in between
3012 16-bit sign+exponent and 64-bit mantissa. */
3013 ex = adjust_address (temp, iemode, 0);
3014 hi = adjust_address (temp, imode, 4);
3015 lo = adjust_address (temp, imode, 8);
3017 else if (!WORDS_BIG_ENDIAN)
3019 /* Intel little endian, 64-bit mantissa followed by 16-bit
3020 sign+exponent and then either 16 or 48 bits of gap. */
3021 ex = adjust_address (temp, iemode, 8);
3022 hi = adjust_address (temp, imode, 4);
3023 lo = adjust_address (temp, imode, 0);
3025 else
3027 /* Big endian Itanium. */
3028 ex = adjust_address (temp, iemode, 0);
3029 hi = adjust_address (temp, imode, 2);
3030 lo = adjust_address (temp, imode, 6);
3032 rtx val = GEN_INT (HOST_WIDE_INT_M1U << 30);
3033 gcc_assert (fmt->qnan_msb_set);
3034 rtx mask = GEN_INT (0x7fff);
3035 rtx bit = GEN_INT (HOST_WIDE_INT_1U << 30);
3036 /* For Intel/Motorola extended format, expand to:
3037 (ex & mask) == mask && ((hi ^ bit) | ((lo | -lo) >> 31)) > val. */
3038 rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3039 lo = expand_binop (imode, ior_optab, lo, nlo,
3040 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3041 lo = expand_shift (RSHIFT_EXPR, imode, lo, 31, NULL_RTX, 1);
3042 temp = expand_binop (imode, xor_optab, hi, bit,
3043 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3044 temp = expand_binop (imode, ior_optab, temp, lo,
3045 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3046 temp = emit_store_flag_force (target, GTU, temp, val, imode, 1, 1);
3047 ex = expand_binop (iemode, and_optab, ex, mask,
3048 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3049 ex = emit_store_flag_force (gen_reg_rtx (GET_MODE (temp)), EQ,
3050 ex, mask, iemode, 1, 1);
3051 temp = expand_binop (GET_MODE (temp), and_optab, temp, ex,
3052 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3054 break;
3055 default:
3056 gcc_unreachable ();
3059 return temp;
3062 /* Expand a call to one of the builtin rounding functions gcc defines
3063 as an extension (lfloor and lceil). As these are gcc extensions we
3064 do not need to worry about setting errno to EDOM.
3065 If expanding via optab fails, lower expression to (int)(floor(x)).
3066 EXP is the expression that is a call to the builtin function;
3067 if convenient, the result should be placed in TARGET. */
3069 static rtx
3070 expand_builtin_int_roundingfn (tree exp, rtx target)
3072 convert_optab builtin_optab;
3073 rtx op0, tmp;
3074 rtx_insn *insns;
3075 tree fndecl = get_callee_fndecl (exp);
3076 enum built_in_function fallback_fn;
3077 tree fallback_fndecl;
3078 machine_mode mode;
3079 tree arg;
3081 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3082 return NULL_RTX;
3084 arg = CALL_EXPR_ARG (exp, 0);
3086 switch (DECL_FUNCTION_CODE (fndecl))
3088 CASE_FLT_FN (BUILT_IN_ICEIL):
3089 CASE_FLT_FN (BUILT_IN_LCEIL):
3090 CASE_FLT_FN (BUILT_IN_LLCEIL):
3091 builtin_optab = lceil_optab;
3092 fallback_fn = BUILT_IN_CEIL;
3093 break;
3095 CASE_FLT_FN (BUILT_IN_IFLOOR):
3096 CASE_FLT_FN (BUILT_IN_LFLOOR):
3097 CASE_FLT_FN (BUILT_IN_LLFLOOR):
3098 builtin_optab = lfloor_optab;
3099 fallback_fn = BUILT_IN_FLOOR;
3100 break;
3102 default:
3103 gcc_unreachable ();
3106 /* Make a suitable register to place result in. */
3107 mode = TYPE_MODE (TREE_TYPE (exp));
3109 target = gen_reg_rtx (mode);
3111 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3112 need to expand the argument again. This way, we will not perform
3113 side-effects more the once. */
3114 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3116 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3118 start_sequence ();
3120 /* Compute into TARGET. */
3121 if (expand_sfix_optab (target, op0, builtin_optab))
3123 /* Output the entire sequence. */
3124 insns = get_insns ();
3125 end_sequence ();
3126 emit_insn (insns);
3127 return target;
3130 /* If we were unable to expand via the builtin, stop the sequence
3131 (without outputting the insns). */
3132 end_sequence ();
3134 /* Fall back to floating point rounding optab. */
3135 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
3137 /* For non-C99 targets we may end up without a fallback fndecl here
3138 if the user called __builtin_lfloor directly. In this case emit
3139 a call to the floor/ceil variants nevertheless. This should result
3140 in the best user experience for not full C99 targets. */
3141 if (fallback_fndecl == NULL_TREE)
3143 tree fntype;
3144 const char *name = NULL;
3146 switch (DECL_FUNCTION_CODE (fndecl))
3148 case BUILT_IN_ICEIL:
3149 case BUILT_IN_LCEIL:
3150 case BUILT_IN_LLCEIL:
3151 name = "ceil";
3152 break;
3153 case BUILT_IN_ICEILF:
3154 case BUILT_IN_LCEILF:
3155 case BUILT_IN_LLCEILF:
3156 name = "ceilf";
3157 break;
3158 case BUILT_IN_ICEILL:
3159 case BUILT_IN_LCEILL:
3160 case BUILT_IN_LLCEILL:
3161 name = "ceill";
3162 break;
3163 case BUILT_IN_IFLOOR:
3164 case BUILT_IN_LFLOOR:
3165 case BUILT_IN_LLFLOOR:
3166 name = "floor";
3167 break;
3168 case BUILT_IN_IFLOORF:
3169 case BUILT_IN_LFLOORF:
3170 case BUILT_IN_LLFLOORF:
3171 name = "floorf";
3172 break;
3173 case BUILT_IN_IFLOORL:
3174 case BUILT_IN_LFLOORL:
3175 case BUILT_IN_LLFLOORL:
3176 name = "floorl";
3177 break;
3178 default:
3179 gcc_unreachable ();
3182 fntype = build_function_type_list (TREE_TYPE (arg),
3183 TREE_TYPE (arg), NULL_TREE);
3184 fallback_fndecl = build_fn_decl (name, fntype);
3187 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
3189 tmp = expand_normal (exp);
3190 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
3192 /* Truncate the result of floating point optab to integer
3193 via expand_fix (). */
3194 target = gen_reg_rtx (mode);
3195 expand_fix (target, tmp, 0);
3197 return target;
3200 /* Expand a call to one of the builtin math functions doing integer
3201 conversion (lrint).
3202 Return 0 if a normal call should be emitted rather than expanding the
3203 function in-line. EXP is the expression that is a call to the builtin
3204 function; if convenient, the result should be placed in TARGET. */
3206 static rtx
3207 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
3209 convert_optab builtin_optab;
3210 rtx op0;
3211 rtx_insn *insns;
3212 tree fndecl = get_callee_fndecl (exp);
3213 tree arg;
3214 machine_mode mode;
3215 enum built_in_function fallback_fn = BUILT_IN_NONE;
3217 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3218 return NULL_RTX;
3220 arg = CALL_EXPR_ARG (exp, 0);
3222 switch (DECL_FUNCTION_CODE (fndecl))
3224 CASE_FLT_FN (BUILT_IN_IRINT):
3225 fallback_fn = BUILT_IN_LRINT;
3226 gcc_fallthrough ();
3227 CASE_FLT_FN (BUILT_IN_LRINT):
3228 CASE_FLT_FN (BUILT_IN_LLRINT):
3229 builtin_optab = lrint_optab;
3230 break;
3232 CASE_FLT_FN (BUILT_IN_IROUND):
3233 fallback_fn = BUILT_IN_LROUND;
3234 gcc_fallthrough ();
3235 CASE_FLT_FN (BUILT_IN_LROUND):
3236 CASE_FLT_FN (BUILT_IN_LLROUND):
3237 builtin_optab = lround_optab;
3238 break;
3240 default:
3241 gcc_unreachable ();
3244 /* There's no easy way to detect the case we need to set EDOM. */
3245 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
3246 return NULL_RTX;
3248 /* Make a suitable register to place result in. */
3249 mode = TYPE_MODE (TREE_TYPE (exp));
3251 /* There's no easy way to detect the case we need to set EDOM. */
3252 if (!flag_errno_math)
3254 rtx result = gen_reg_rtx (mode);
3256 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3257 need to expand the argument again. This way, we will not perform
3258 side-effects more the once. */
3259 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3261 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3263 start_sequence ();
3265 if (expand_sfix_optab (result, op0, builtin_optab))
3267 /* Output the entire sequence. */
3268 insns = get_insns ();
3269 end_sequence ();
3270 emit_insn (insns);
3271 return result;
3274 /* If we were unable to expand via the builtin, stop the sequence
3275 (without outputting the insns) and call to the library function
3276 with the stabilized argument list. */
3277 end_sequence ();
3280 if (fallback_fn != BUILT_IN_NONE)
3282 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
3283 targets, (int) round (x) should never be transformed into
3284 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
3285 a call to lround in the hope that the target provides at least some
3286 C99 functions. This should result in the best user experience for
3287 not full C99 targets.
3288 As scalar float conversions with same mode are useless in GIMPLE,
3289 we can end up e.g. with _Float32 argument passed to float builtin,
3290 try to get the type from the builtin prototype first. */
3291 tree fallback_fndecl = NULL_TREE;
3292 if (tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
3293 fallback_fndecl
3294 = mathfn_built_in_1 (TREE_VALUE (argtypes),
3295 as_combined_fn (fallback_fn), 0);
3296 if (fallback_fndecl == NULL_TREE)
3297 fallback_fndecl
3298 = mathfn_built_in_1 (TREE_TYPE (arg),
3299 as_combined_fn (fallback_fn), 0);
3300 if (fallback_fndecl)
3302 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
3303 fallback_fndecl, 1, arg);
3305 target = expand_call (exp, NULL_RTX, target == const0_rtx);
3306 target = maybe_emit_group_store (target, TREE_TYPE (exp));
3307 return convert_to_mode (mode, target, 0);
3311 return expand_call (exp, target, target == const0_rtx);
3314 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3315 a normal call should be emitted rather than expanding the function
3316 in-line. EXP is the expression that is a call to the builtin
3317 function; if convenient, the result should be placed in TARGET. */
3319 static rtx
3320 expand_builtin_powi (tree exp, rtx target)
3322 tree arg0, arg1;
3323 rtx op0, op1;
3324 machine_mode mode;
3325 machine_mode mode2;
3327 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3328 return NULL_RTX;
3330 arg0 = CALL_EXPR_ARG (exp, 0);
3331 arg1 = CALL_EXPR_ARG (exp, 1);
3332 mode = TYPE_MODE (TREE_TYPE (exp));
3334 /* Emit a libcall to libgcc. */
3336 /* Mode of the 2nd argument must match that of an int. */
3337 mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
3339 if (target == NULL_RTX)
3340 target = gen_reg_rtx (mode);
3342 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3343 if (GET_MODE (op0) != mode)
3344 op0 = convert_to_mode (mode, op0, 0);
3345 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3346 if (GET_MODE (op1) != mode2)
3347 op1 = convert_to_mode (mode2, op1, 0);
3349 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3350 target, LCT_CONST, mode,
3351 op0, mode, op1, mode2);
3353 return target;
3356 /* Expand expression EXP which is a call to the strlen builtin. Return
3357 NULL_RTX if we failed and the caller should emit a normal call, otherwise
3358 try to get the result in TARGET, if convenient. */
3360 static rtx
3361 expand_builtin_strlen (tree exp, rtx target,
3362 machine_mode target_mode)
3364 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3365 return NULL_RTX;
3367 tree src = CALL_EXPR_ARG (exp, 0);
3369 /* If the length can be computed at compile-time, return it. */
3370 if (tree len = c_strlen (src, 0))
3371 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3373 /* If the length can be computed at compile-time and is constant
3374 integer, but there are side-effects in src, evaluate
3375 src for side-effects, then return len.
3376 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3377 can be optimized into: i++; x = 3; */
3378 tree len = c_strlen (src, 1);
3379 if (len && TREE_CODE (len) == INTEGER_CST)
3381 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3382 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3385 unsigned int align = get_pointer_alignment (src) / BITS_PER_UNIT;
3387 /* If SRC is not a pointer type, don't do this operation inline. */
3388 if (align == 0)
3389 return NULL_RTX;
3391 /* Bail out if we can't compute strlen in the right mode. */
3392 machine_mode insn_mode;
3393 enum insn_code icode = CODE_FOR_nothing;
3394 FOR_EACH_MODE_FROM (insn_mode, target_mode)
3396 icode = optab_handler (strlen_optab, insn_mode);
3397 if (icode != CODE_FOR_nothing)
3398 break;
3400 if (insn_mode == VOIDmode)
3401 return NULL_RTX;
3403 /* Make a place to hold the source address. We will not expand
3404 the actual source until we are sure that the expansion will
3405 not fail -- there are trees that cannot be expanded twice. */
3406 rtx src_reg = gen_reg_rtx (Pmode);
3408 /* Mark the beginning of the strlen sequence so we can emit the
3409 source operand later. */
3410 rtx_insn *before_strlen = get_last_insn ();
3412 class expand_operand ops[4];
3413 create_output_operand (&ops[0], target, insn_mode);
3414 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3415 create_integer_operand (&ops[2], 0);
3416 create_integer_operand (&ops[3], align);
3417 if (!maybe_expand_insn (icode, 4, ops))
3418 return NULL_RTX;
3420 /* Check to see if the argument was declared attribute nonstring
3421 and if so, issue a warning since at this point it's not known
3422 to be nul-terminated. */
3423 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3425 /* Now that we are assured of success, expand the source. */
3426 start_sequence ();
3427 rtx pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3428 if (pat != src_reg)
3430 #ifdef POINTERS_EXTEND_UNSIGNED
3431 if (GET_MODE (pat) != Pmode)
3432 pat = convert_to_mode (Pmode, pat,
3433 POINTERS_EXTEND_UNSIGNED);
3434 #endif
3435 emit_move_insn (src_reg, pat);
3437 pat = get_insns ();
3438 end_sequence ();
3440 if (before_strlen)
3441 emit_insn_after (pat, before_strlen);
3442 else
3443 emit_insn_before (pat, get_insns ());
3445 /* Return the value in the proper mode for this function. */
3446 if (GET_MODE (ops[0].value) == target_mode)
3447 target = ops[0].value;
3448 else if (target != 0)
3449 convert_move (target, ops[0].value, 0);
3450 else
3451 target = convert_to_mode (target_mode, ops[0].value, 0);
3453 return target;
3456 /* Expand call EXP to the strnlen built-in, returning the result
3457 and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3459 static rtx
3460 expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
3462 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3463 return NULL_RTX;
3465 tree src = CALL_EXPR_ARG (exp, 0);
3466 tree bound = CALL_EXPR_ARG (exp, 1);
3468 if (!bound)
3469 return NULL_RTX;
3471 location_t loc = UNKNOWN_LOCATION;
3472 if (EXPR_HAS_LOCATION (exp))
3473 loc = EXPR_LOCATION (exp);
3475 /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3476 so these conversions aren't necessary. */
3477 c_strlen_data lendata = { };
3478 tree len = c_strlen (src, 0, &lendata, 1);
3479 if (len)
3480 len = fold_convert_loc (loc, TREE_TYPE (bound), len);
3482 if (TREE_CODE (bound) == INTEGER_CST)
3484 if (!len)
3485 return NULL_RTX;
3487 len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
3488 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3491 if (TREE_CODE (bound) != SSA_NAME)
3492 return NULL_RTX;
3494 wide_int min, max;
3495 value_range r;
3496 get_global_range_query ()->range_of_expr (r, bound);
3497 if (r.varying_p () || r.undefined_p ())
3498 return NULL_RTX;
3499 min = r.lower_bound ();
3500 max = r.upper_bound ();
3502 if (!len || TREE_CODE (len) != INTEGER_CST)
3504 bool exact;
3505 lendata.decl = unterminated_array (src, &len, &exact);
3506 if (!lendata.decl)
3507 return NULL_RTX;
3510 if (lendata.decl)
3511 return NULL_RTX;
3513 if (wi::gtu_p (min, wi::to_wide (len)))
3514 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3516 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound);
3517 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3520 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3521 bytes from bytes at DATA + OFFSET and return it reinterpreted as
3522 a target constant. */
3524 static rtx
3525 builtin_memcpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3526 fixed_size_mode mode)
3528 /* The REPresentation pointed to by DATA need not be a nul-terminated
3529 string but the caller guarantees it's large enough for MODE. */
3530 const char *rep = (const char *) data;
3532 /* The by-pieces infrastructure does not try to pick a vector mode
3533 for memcpy expansion. */
3534 return c_readstr (rep + offset, as_a <scalar_int_mode> (mode),
3535 /*nul_terminated=*/false);
3538 /* LEN specify length of the block of memcpy/memset operation.
3539 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3540 In some cases we can make very likely guess on max size, then we
3541 set it into PROBABLE_MAX_SIZE. */
3543 static void
3544 determine_block_size (tree len, rtx len_rtx,
3545 unsigned HOST_WIDE_INT *min_size,
3546 unsigned HOST_WIDE_INT *max_size,
3547 unsigned HOST_WIDE_INT *probable_max_size)
3549 if (CONST_INT_P (len_rtx))
3551 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3552 return;
3554 else
3556 wide_int min, max;
3557 enum value_range_kind range_type = VR_UNDEFINED;
3559 /* Determine bounds from the type. */
3560 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3561 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3562 else
3563 *min_size = 0;
3564 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3565 *probable_max_size = *max_size
3566 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3567 else
3568 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3570 if (TREE_CODE (len) == SSA_NAME)
3572 value_range r;
3573 tree tmin, tmax;
3574 get_global_range_query ()->range_of_expr (r, len);
3575 range_type = get_legacy_range (r, tmin, tmax);
3576 if (range_type != VR_UNDEFINED)
3578 min = wi::to_wide (tmin);
3579 max = wi::to_wide (tmax);
3582 if (range_type == VR_RANGE)
3584 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3585 *min_size = min.to_uhwi ();
3586 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3587 *probable_max_size = *max_size = max.to_uhwi ();
3589 else if (range_type == VR_ANTI_RANGE)
3591 /* Code like
3593 int n;
3594 if (n < 100)
3595 memcpy (a, b, n)
3597 Produce anti range allowing negative values of N. We still
3598 can use the information and make a guess that N is not negative.
3600 if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3601 *probable_max_size = min.to_uhwi () - 1;
3604 gcc_checking_assert (*max_size <=
3605 (unsigned HOST_WIDE_INT)
3606 GET_MODE_MASK (GET_MODE (len_rtx)));
3609 /* Expand a call EXP to the memcpy builtin.
3610 Return NULL_RTX if we failed, the caller should emit a normal call,
3611 otherwise try to get the result in TARGET, if convenient (and in
3612 mode MODE if that's convenient). */
3614 static rtx
3615 expand_builtin_memcpy (tree exp, rtx target)
3617 if (!validate_arglist (exp,
3618 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3619 return NULL_RTX;
3621 tree dest = CALL_EXPR_ARG (exp, 0);
3622 tree src = CALL_EXPR_ARG (exp, 1);
3623 tree len = CALL_EXPR_ARG (exp, 2);
3625 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3626 /*retmode=*/ RETURN_BEGIN, false);
3629 /* Check a call EXP to the memmove built-in for validity.
3630 Return NULL_RTX on both success and failure. */
3632 static rtx
3633 expand_builtin_memmove (tree exp, rtx target)
3635 if (!validate_arglist (exp,
3636 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3637 return NULL_RTX;
3639 tree dest = CALL_EXPR_ARG (exp, 0);
3640 tree src = CALL_EXPR_ARG (exp, 1);
3641 tree len = CALL_EXPR_ARG (exp, 2);
3643 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3644 /*retmode=*/ RETURN_BEGIN, true);
3647 /* Expand a call EXP to the mempcpy builtin.
3648 Return NULL_RTX if we failed; the caller should emit a normal call,
3649 otherwise try to get the result in TARGET, if convenient (and in
3650 mode MODE if that's convenient). */
3652 static rtx
3653 expand_builtin_mempcpy (tree exp, rtx target)
3655 if (!validate_arglist (exp,
3656 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3657 return NULL_RTX;
3659 tree dest = CALL_EXPR_ARG (exp, 0);
3660 tree src = CALL_EXPR_ARG (exp, 1);
3661 tree len = CALL_EXPR_ARG (exp, 2);
3663 /* Policy does not generally allow using compute_objsize (which
3664 is used internally by check_memop_size) to change code generation
3665 or drive optimization decisions.
3667 In this instance it is safe because the code we generate has
3668 the same semantics regardless of the return value of
3669 check_memop_sizes. Exactly the same amount of data is copied
3670 and the return value is exactly the same in both cases.
3672 Furthermore, check_memop_size always uses mode 0 for the call to
3673 compute_objsize, so the imprecise nature of compute_objsize is
3674 avoided. */
3676 /* Avoid expanding mempcpy into memcpy when the call is determined
3677 to overflow the buffer. This also prevents the same overflow
3678 from being diagnosed again when expanding memcpy. */
3680 return expand_builtin_mempcpy_args (dest, src, len,
3681 target, exp, /*retmode=*/ RETURN_END);
3684 /* Helper function to do the actual work for expand of memory copy family
3685 functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3686 of memory from SRC to DEST and assign to TARGET if convenient. Return
3687 value is based on RETMODE argument. */
3689 static rtx
3690 expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3691 rtx target, tree exp, memop_ret retmode,
3692 bool might_overlap)
3694 unsigned int src_align = get_pointer_alignment (src);
3695 unsigned int dest_align = get_pointer_alignment (dest);
3696 rtx dest_mem, src_mem, dest_addr, len_rtx;
3697 HOST_WIDE_INT expected_size = -1;
3698 unsigned int expected_align = 0;
3699 unsigned HOST_WIDE_INT min_size;
3700 unsigned HOST_WIDE_INT max_size;
3701 unsigned HOST_WIDE_INT probable_max_size;
3703 bool is_move_done;
3705 /* If DEST is not a pointer type, call the normal function. */
3706 if (dest_align == 0)
3707 return NULL_RTX;
3709 /* If either SRC is not a pointer type, don't do this
3710 operation in-line. */
3711 if (src_align == 0)
3712 return NULL_RTX;
3714 if (currently_expanding_gimple_stmt)
3715 stringop_block_profile (currently_expanding_gimple_stmt,
3716 &expected_align, &expected_size);
3718 if (expected_align < dest_align)
3719 expected_align = dest_align;
3720 dest_mem = get_memory_rtx (dest, len);
3721 set_mem_align (dest_mem, dest_align);
3722 len_rtx = expand_normal (len);
3723 determine_block_size (len, len_rtx, &min_size, &max_size,
3724 &probable_max_size);
3726 /* Try to get the byte representation of the constant SRC points to,
3727 with its byte size in NBYTES. */
3728 unsigned HOST_WIDE_INT nbytes;
3729 const char *rep = getbyterep (src, &nbytes);
3731 /* If the function's constant bound LEN_RTX is less than or equal
3732 to the byte size of the representation of the constant argument,
3733 and if block move would be done by pieces, we can avoid loading
3734 the bytes from memory and only store the computed constant.
3735 This works in the overlap (memmove) case as well because
3736 store_by_pieces just generates a series of stores of constants
3737 from the representation returned by getbyterep(). */
3738 if (rep
3739 && CONST_INT_P (len_rtx)
3740 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes
3741 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3742 CONST_CAST (char *, rep),
3743 dest_align, false))
3745 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3746 builtin_memcpy_read_str,
3747 CONST_CAST (char *, rep),
3748 dest_align, false, retmode);
3749 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3750 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3751 return dest_mem;
3754 src_mem = get_memory_rtx (src, len);
3755 set_mem_align (src_mem, src_align);
3757 /* Copy word part most expediently. */
3758 enum block_op_methods method = BLOCK_OP_NORMAL;
3759 if (CALL_EXPR_TAILCALL (exp)
3760 && (retmode == RETURN_BEGIN || target == const0_rtx))
3761 method = BLOCK_OP_TAILCALL;
3762 bool use_mempcpy_call = (targetm.libc_has_fast_function (BUILT_IN_MEMPCPY)
3763 && retmode == RETURN_END
3764 && !might_overlap
3765 && target != const0_rtx);
3766 if (use_mempcpy_call)
3767 method = BLOCK_OP_NO_LIBCALL_RET;
3768 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3769 expected_align, expected_size,
3770 min_size, max_size, probable_max_size,
3771 use_mempcpy_call, &is_move_done,
3772 might_overlap);
3774 /* Bail out when a mempcpy call would be expanded as libcall and when
3775 we have a target that provides a fast implementation
3776 of mempcpy routine. */
3777 if (!is_move_done)
3778 return NULL_RTX;
3780 if (dest_addr == pc_rtx)
3781 return NULL_RTX;
3783 if (dest_addr == 0)
3785 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3786 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3789 if (retmode != RETURN_BEGIN && target != const0_rtx)
3791 dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3792 /* stpcpy pointer to last byte. */
3793 if (retmode == RETURN_END_MINUS_ONE)
3794 dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3797 return dest_addr;
3800 static rtx
3801 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3802 rtx target, tree orig_exp, memop_ret retmode)
3804 return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3805 retmode, false);
3808 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3809 we failed, the caller should emit a normal call, otherwise try to
3810 get the result in TARGET, if convenient.
3811 Return value is based on RETMODE argument. */
3813 static rtx
3814 expand_movstr (tree dest, tree src, rtx target, memop_ret retmode)
3816 class expand_operand ops[3];
3817 rtx dest_mem;
3818 rtx src_mem;
3820 if (!targetm.have_movstr ())
3821 return NULL_RTX;
3823 dest_mem = get_memory_rtx (dest, NULL);
3824 src_mem = get_memory_rtx (src, NULL);
3825 if (retmode == RETURN_BEGIN)
3827 target = force_reg (Pmode, XEXP (dest_mem, 0));
3828 dest_mem = replace_equiv_address (dest_mem, target);
3831 create_output_operand (&ops[0],
3832 retmode != RETURN_BEGIN ? target : NULL_RTX, Pmode);
3833 create_fixed_operand (&ops[1], dest_mem);
3834 create_fixed_operand (&ops[2], src_mem);
3835 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3836 return NULL_RTX;
3838 if (retmode != RETURN_BEGIN && target != const0_rtx)
3840 target = ops[0].value;
3841 /* movstr is supposed to set end to the address of the NUL
3842 terminator. If the caller requested a mempcpy-like return value,
3843 adjust it. */
3844 if (retmode == RETURN_END)
3846 rtx tem = plus_constant (GET_MODE (target),
3847 gen_lowpart (GET_MODE (target), target), 1);
3848 emit_move_insn (target, force_operand (tem, NULL_RTX));
3851 return target;
3854 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3855 NULL_RTX if we failed the caller should emit a normal call, otherwise
3856 try to get the result in TARGET, if convenient (and in mode MODE if that's
3857 convenient). */
3859 static rtx
3860 expand_builtin_strcpy (tree exp, rtx target)
3862 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3863 return NULL_RTX;
3865 tree dest = CALL_EXPR_ARG (exp, 0);
3866 tree src = CALL_EXPR_ARG (exp, 1);
3868 return expand_builtin_strcpy_args (exp, dest, src, target);
3871 /* Helper function to do the actual work for expand_builtin_strcpy. The
3872 arguments to the builtin_strcpy call DEST and SRC are broken out
3873 so that this can also be called without constructing an actual CALL_EXPR.
3874 The other arguments and return value are the same as for
3875 expand_builtin_strcpy. */
3877 static rtx
3878 expand_builtin_strcpy_args (tree, tree dest, tree src, rtx target)
3880 return expand_movstr (dest, src, target, /*retmode=*/ RETURN_BEGIN);
3883 /* Expand a call EXP to the stpcpy builtin.
3884 Return NULL_RTX if we failed the caller should emit a normal call,
3885 otherwise try to get the result in TARGET, if convenient (and in
3886 mode MODE if that's convenient). */
3888 static rtx
3889 expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
3891 tree dst, src;
3892 location_t loc = EXPR_LOCATION (exp);
3894 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3895 return NULL_RTX;
3897 dst = CALL_EXPR_ARG (exp, 0);
3898 src = CALL_EXPR_ARG (exp, 1);
3900 /* If return value is ignored, transform stpcpy into strcpy. */
3901 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3903 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3904 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3905 return expand_expr (result, target, mode, EXPAND_NORMAL);
3907 else
3909 tree len, lenp1;
3910 rtx ret;
3912 /* Ensure we get an actual string whose length can be evaluated at
3913 compile-time, not an expression containing a string. This is
3914 because the latter will potentially produce pessimized code
3915 when used to produce the return value. */
3916 c_strlen_data lendata = { };
3917 if (!c_getstr (src)
3918 || !(len = c_strlen (src, 0, &lendata, 1)))
3919 return expand_movstr (dst, src, target,
3920 /*retmode=*/ RETURN_END_MINUS_ONE);
3922 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3923 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3924 target, exp,
3925 /*retmode=*/ RETURN_END_MINUS_ONE);
3927 if (ret)
3928 return ret;
3930 if (TREE_CODE (len) == INTEGER_CST)
3932 rtx len_rtx = expand_normal (len);
3934 if (CONST_INT_P (len_rtx))
3936 ret = expand_builtin_strcpy_args (exp, dst, src, target);
3938 if (ret)
3940 if (! target)
3942 if (mode != VOIDmode)
3943 target = gen_reg_rtx (mode);
3944 else
3945 target = gen_reg_rtx (GET_MODE (ret));
3947 if (GET_MODE (target) != GET_MODE (ret))
3948 ret = gen_lowpart (GET_MODE (target), ret);
3950 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3951 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3952 gcc_assert (ret);
3954 return target;
3959 return expand_movstr (dst, src, target,
3960 /*retmode=*/ RETURN_END_MINUS_ONE);
3964 /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
3965 arguments while being careful to avoid duplicate warnings (which could
3966 be issued if the expander were to expand the call, resulting in it
3967 being emitted in expand_call(). */
3969 static rtx
3970 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3972 if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode))
3974 /* The call has been successfully expanded. Check for nonstring
3975 arguments and issue warnings as appropriate. */
3976 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3977 return ret;
3980 return NULL_RTX;
3983 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3984 bytes from constant string DATA + OFFSET and return it as target
3985 constant. */
3988 builtin_strncpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3989 fixed_size_mode mode)
3991 const char *str = (const char *) data;
3993 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3994 return const0_rtx;
3996 /* The by-pieces infrastructure does not try to pick a vector mode
3997 for strncpy expansion. */
3998 return c_readstr (str + offset, as_a <scalar_int_mode> (mode));
4001 /* Helper to check the sizes of sequences and the destination of calls
4002 to __builtin_strncat and __builtin___strncat_chk. Returns true on
4003 success (no overflow or invalid sizes), false otherwise. */
4005 static bool
4006 check_strncat_sizes (tree exp, tree objsize)
4008 tree dest = CALL_EXPR_ARG (exp, 0);
4009 tree src = CALL_EXPR_ARG (exp, 1);
4010 tree maxread = CALL_EXPR_ARG (exp, 2);
4012 /* Try to determine the range of lengths that the source expression
4013 refers to. */
4014 c_strlen_data lendata = { };
4015 get_range_strlen (src, &lendata, /* eltsize = */ 1);
4017 /* Try to verify that the destination is big enough for the shortest
4018 string. */
4020 access_data data (nullptr, exp, access_read_write, maxread, true);
4021 if (!objsize && warn_stringop_overflow)
4023 /* If it hasn't been provided by __strncat_chk, try to determine
4024 the size of the destination object into which the source is
4025 being copied. */
4026 objsize = compute_objsize (dest, warn_stringop_overflow - 1, &data.dst);
4029 /* Add one for the terminating nul. */
4030 tree srclen = (lendata.minlen
4031 ? fold_build2 (PLUS_EXPR, size_type_node, lendata.minlen,
4032 size_one_node)
4033 : NULL_TREE);
4035 /* The strncat function copies at most MAXREAD bytes and always appends
4036 the terminating nul so the specified upper bound should never be equal
4037 to (or greater than) the size of the destination. */
4038 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
4039 && tree_int_cst_equal (objsize, maxread))
4041 location_t loc = EXPR_LOCATION (exp);
4042 warning_at (loc, OPT_Wstringop_overflow_,
4043 "%qD specified bound %E equals destination size",
4044 get_callee_fndecl (exp), maxread);
4046 return false;
4049 if (!srclen
4050 || (maxread && tree_fits_uhwi_p (maxread)
4051 && tree_fits_uhwi_p (srclen)
4052 && tree_int_cst_lt (maxread, srclen)))
4053 srclen = maxread;
4055 /* The number of bytes to write is LEN but check_access will alsoa
4056 check SRCLEN if LEN's value isn't known. */
4057 return check_access (exp, /*dstwrite=*/NULL_TREE, maxread, srclen,
4058 objsize, data.mode, &data);
4061 /* Expand expression EXP, which is a call to the strncpy builtin. Return
4062 NULL_RTX if we failed the caller should emit a normal call. */
4064 static rtx
4065 expand_builtin_strncpy (tree exp, rtx target)
4067 location_t loc = EXPR_LOCATION (exp);
4069 if (!validate_arglist (exp,
4070 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4071 return NULL_RTX;
4072 tree dest = CALL_EXPR_ARG (exp, 0);
4073 tree src = CALL_EXPR_ARG (exp, 1);
4074 /* The number of bytes to write (not the maximum). */
4075 tree len = CALL_EXPR_ARG (exp, 2);
4077 /* The length of the source sequence. */
4078 tree slen = c_strlen (src, 1);
4080 /* We must be passed a constant len and src parameter. */
4081 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4082 return NULL_RTX;
4084 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4086 /* We're required to pad with trailing zeros if the requested
4087 len is greater than strlen(s2)+1. In that case try to
4088 use store_by_pieces, if it fails, punt. */
4089 if (tree_int_cst_lt (slen, len))
4091 unsigned int dest_align = get_pointer_alignment (dest);
4092 const char *p = c_getstr (src);
4093 rtx dest_mem;
4095 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4096 || !can_store_by_pieces (tree_to_uhwi (len),
4097 builtin_strncpy_read_str,
4098 CONST_CAST (char *, p),
4099 dest_align, false))
4100 return NULL_RTX;
4102 dest_mem = get_memory_rtx (dest, len);
4103 store_by_pieces (dest_mem, tree_to_uhwi (len),
4104 builtin_strncpy_read_str,
4105 CONST_CAST (char *, p), dest_align, false,
4106 RETURN_BEGIN);
4107 dest_mem = force_operand (XEXP (dest_mem, 0), target);
4108 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4109 return dest_mem;
4112 return NULL_RTX;
4115 /* Return the RTL of a register in MODE generated from PREV in the
4116 previous iteration. */
4118 static rtx
4119 gen_memset_value_from_prev (by_pieces_prev *prev, fixed_size_mode mode)
4121 rtx target = nullptr;
4122 if (prev != nullptr && prev->data != nullptr)
4124 /* Use the previous data in the same mode. */
4125 if (prev->mode == mode)
4126 return prev->data;
4128 fixed_size_mode prev_mode = prev->mode;
4130 /* Don't use the previous data to write QImode if it is in a
4131 vector mode. */
4132 if (VECTOR_MODE_P (prev_mode) && mode == QImode)
4133 return target;
4135 rtx prev_rtx = prev->data;
4137 if (REG_P (prev_rtx)
4138 && HARD_REGISTER_P (prev_rtx)
4139 && lowpart_subreg_regno (REGNO (prev_rtx), prev_mode, mode) < 0)
4141 /* This case occurs when PREV_MODE is a vector and when
4142 MODE is too small to store using vector operations.
4143 After register allocation, the code will need to move the
4144 lowpart of the vector register into a non-vector register.
4146 Also, the target has chosen to use a hard register
4147 instead of going with the default choice of using a
4148 pseudo register. We should respect that choice and try to
4149 avoid creating a pseudo register with the same mode as the
4150 current hard register.
4152 In principle, we could just use a lowpart MODE subreg of
4153 the vector register. However, the vector register mode might
4154 be too wide for non-vector registers, and we already know
4155 that the non-vector mode is too small for vector registers.
4156 It's therefore likely that we'd need to spill to memory in
4157 the vector mode and reload the non-vector value from there.
4159 Try to avoid that by reducing the vector register to the
4160 smallest size that it can hold. This should increase the
4161 chances that non-vector registers can hold both the inner
4162 and outer modes of the subreg that we generate later. */
4163 machine_mode m;
4164 fixed_size_mode candidate;
4165 FOR_EACH_MODE_IN_CLASS (m, GET_MODE_CLASS (mode))
4166 if (is_a<fixed_size_mode> (m, &candidate))
4168 if (GET_MODE_SIZE (candidate)
4169 >= GET_MODE_SIZE (prev_mode))
4170 break;
4171 if (GET_MODE_SIZE (candidate) >= GET_MODE_SIZE (mode)
4172 && lowpart_subreg_regno (REGNO (prev_rtx),
4173 prev_mode, candidate) >= 0)
4175 target = lowpart_subreg (candidate, prev_rtx,
4176 prev_mode);
4177 prev_rtx = target;
4178 prev_mode = candidate;
4179 break;
4182 if (target == nullptr)
4183 prev_rtx = copy_to_reg (prev_rtx);
4186 target = lowpart_subreg (mode, prev_rtx, prev_mode);
4188 return target;
4191 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4192 bytes from constant string DATA + OFFSET and return it as target
4193 constant. If PREV isn't nullptr, it has the RTL info from the
4194 previous iteration. */
4197 builtin_memset_read_str (void *data, void *prev,
4198 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4199 fixed_size_mode mode)
4201 const char *c = (const char *) data;
4202 unsigned int size = GET_MODE_SIZE (mode);
4204 rtx target = gen_memset_value_from_prev ((by_pieces_prev *) prev,
4205 mode);
4206 if (target != nullptr)
4207 return target;
4208 rtx src = gen_int_mode (*c, QImode);
4210 if (VECTOR_MODE_P (mode))
4212 gcc_assert (GET_MODE_INNER (mode) == QImode);
4214 rtx const_vec = gen_const_vec_duplicate (mode, src);
4215 if (prev == NULL)
4216 /* Return CONST_VECTOR when called by a query function. */
4217 return const_vec;
4219 /* Use the move expander with CONST_VECTOR. */
4220 target = gen_reg_rtx (mode);
4221 emit_move_insn (target, const_vec);
4222 return target;
4225 char *p = XALLOCAVEC (char, size);
4227 memset (p, *c, size);
4229 /* Vector modes should be handled above. */
4230 return c_readstr (p, as_a <scalar_int_mode> (mode));
4233 /* Callback routine for store_by_pieces. Return the RTL of a register
4234 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4235 char value given in the RTL register data. For example, if mode is
4236 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
4237 nullptr, it has the RTL info from the previous iteration. */
4239 static rtx
4240 builtin_memset_gen_str (void *data, void *prev,
4241 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4242 fixed_size_mode mode)
4244 rtx target, coeff;
4245 size_t size;
4246 char *p;
4248 size = GET_MODE_SIZE (mode);
4249 if (size == 1)
4250 return (rtx) data;
4252 target = gen_memset_value_from_prev ((by_pieces_prev *) prev, mode);
4253 if (target != nullptr)
4254 return target;
4256 if (VECTOR_MODE_P (mode))
4258 gcc_assert (GET_MODE_INNER (mode) == QImode);
4260 /* vec_duplicate_optab is a precondition to pick a vector mode for
4261 the memset expander. */
4262 insn_code icode = optab_handler (vec_duplicate_optab, mode);
4264 target = gen_reg_rtx (mode);
4265 class expand_operand ops[2];
4266 create_output_operand (&ops[0], target, mode);
4267 create_input_operand (&ops[1], (rtx) data, QImode);
4268 expand_insn (icode, 2, ops);
4269 if (!rtx_equal_p (target, ops[0].value))
4270 emit_move_insn (target, ops[0].value);
4272 return target;
4275 p = XALLOCAVEC (char, size);
4276 memset (p, 1, size);
4277 /* Vector modes should be handled above. */
4278 coeff = c_readstr (p, as_a <scalar_int_mode> (mode));
4280 target = convert_to_mode (mode, (rtx) data, 1);
4281 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4282 return force_reg (mode, target);
4285 /* Expand expression EXP, which is a call to the memset builtin. Return
4286 NULL_RTX if we failed the caller should emit a normal call, otherwise
4287 try to get the result in TARGET, if convenient (and in mode MODE if that's
4288 convenient). */
4291 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4293 if (!validate_arglist (exp,
4294 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4295 return NULL_RTX;
4297 tree dest = CALL_EXPR_ARG (exp, 0);
4298 tree val = CALL_EXPR_ARG (exp, 1);
4299 tree len = CALL_EXPR_ARG (exp, 2);
4301 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4304 /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
4305 Return TRUE if successful, FALSE otherwise. TO is assumed to be
4306 aligned at an ALIGN-bits boundary. LEN must be a multiple of
4307 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
4309 The strategy is to issue one store_by_pieces for each power of two,
4310 from most to least significant, guarded by a test on whether there
4311 are at least that many bytes left to copy in LEN.
4313 ??? Should we skip some powers of two in favor of loops? Maybe start
4314 at the max of TO/LEN/word alignment, at least when optimizing for
4315 size, instead of ensuring O(log len) dynamic compares? */
4317 bool
4318 try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
4319 unsigned HOST_WIDE_INT min_len,
4320 unsigned HOST_WIDE_INT max_len,
4321 rtx val, char valc, unsigned int align)
4323 int max_bits = floor_log2 (max_len);
4324 int min_bits = floor_log2 (min_len);
4325 int sctz_len = ctz_len;
4327 gcc_checking_assert (sctz_len >= 0);
4329 if (val)
4330 valc = 1;
4332 /* Bits more significant than TST_BITS are part of the shared prefix
4333 in the binary representation of both min_len and max_len. Since
4334 they're identical, we don't need to test them in the loop. */
4335 int tst_bits = (max_bits != min_bits ? max_bits
4336 : floor_log2 (max_len ^ min_len));
4338 /* Check whether it's profitable to start by storing a fixed BLKSIZE
4339 bytes, to lower max_bits. In the unlikely case of a constant LEN
4340 (implied by identical MAX_LEN and MIN_LEN), we want to issue a
4341 single store_by_pieces, but otherwise, select the minimum multiple
4342 of the ALIGN (in bytes) and of the MCD of the possible LENs, that
4343 brings MAX_LEN below TST_BITS, if that's lower than min_len. */
4344 unsigned HOST_WIDE_INT blksize;
4345 if (max_len > min_len)
4347 unsigned HOST_WIDE_INT alrng = MAX (HOST_WIDE_INT_1U << ctz_len,
4348 align / BITS_PER_UNIT);
4349 blksize = max_len - (HOST_WIDE_INT_1U << tst_bits) + alrng;
4350 blksize &= ~(alrng - 1);
4352 else if (max_len == min_len)
4353 blksize = max_len;
4354 else
4355 /* Huh, max_len < min_len? Punt. See pr100843.c. */
4356 return false;
4357 if (min_len >= blksize)
4359 min_len -= blksize;
4360 min_bits = floor_log2 (min_len);
4361 max_len -= blksize;
4362 max_bits = floor_log2 (max_len);
4364 tst_bits = (max_bits != min_bits ? max_bits
4365 : floor_log2 (max_len ^ min_len));
4367 else
4368 blksize = 0;
4370 /* Check that we can use store by pieces for the maximum store count
4371 we may issue (initial fixed-size block, plus conditional
4372 power-of-two-sized from max_bits to ctz_len. */
4373 unsigned HOST_WIDE_INT xlenest = blksize;
4374 if (max_bits >= 0)
4375 xlenest += ((HOST_WIDE_INT_1U << max_bits) * 2
4376 - (HOST_WIDE_INT_1U << ctz_len));
4377 if (!can_store_by_pieces (xlenest, builtin_memset_read_str,
4378 &valc, align, true))
4379 return false;
4381 by_pieces_constfn constfun;
4382 void *constfundata;
4383 if (val)
4385 constfun = builtin_memset_gen_str;
4386 constfundata = val = force_reg (TYPE_MODE (unsigned_char_type_node),
4387 val);
4389 else
4391 constfun = builtin_memset_read_str;
4392 constfundata = &valc;
4395 rtx ptr = copy_addr_to_reg (XEXP (to, 0));
4396 rtx rem = copy_to_mode_reg (ptr_mode, convert_to_mode (ptr_mode, len, 0));
4397 to = replace_equiv_address (to, ptr);
4398 set_mem_align (to, align);
4400 if (blksize)
4402 to = store_by_pieces (to, blksize,
4403 constfun, constfundata,
4404 align, true,
4405 max_len != 0 ? RETURN_END : RETURN_BEGIN);
4406 if (max_len == 0)
4407 return true;
4409 /* Adjust PTR, TO and REM. Since TO's address is likely
4410 PTR+offset, we have to replace it. */
4411 emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4412 to = replace_equiv_address (to, ptr);
4413 rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4414 emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4417 /* Iterate over power-of-two block sizes from the maximum length to
4418 the least significant bit possibly set in the length. */
4419 for (int i = max_bits; i >= sctz_len; i--)
4421 rtx_code_label *label = NULL;
4422 blksize = HOST_WIDE_INT_1U << i;
4424 /* If we're past the bits shared between min_ and max_len, expand
4425 a test on the dynamic length, comparing it with the
4426 BLKSIZE. */
4427 if (i <= tst_bits)
4429 label = gen_label_rtx ();
4430 emit_cmp_and_jump_insns (rem, GEN_INT (blksize), LT, NULL,
4431 ptr_mode, 1, label,
4432 profile_probability::even ());
4434 /* If we are at a bit that is in the prefix shared by min_ and
4435 max_len, skip this BLKSIZE if the bit is clear. */
4436 else if ((max_len & blksize) == 0)
4437 continue;
4439 /* Issue a store of BLKSIZE bytes. */
4440 to = store_by_pieces (to, blksize,
4441 constfun, constfundata,
4442 align, true,
4443 i != sctz_len ? RETURN_END : RETURN_BEGIN);
4445 /* Adjust REM and PTR, unless this is the last iteration. */
4446 if (i != sctz_len)
4448 emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4449 to = replace_equiv_address (to, ptr);
4450 rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4451 emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4454 if (label)
4456 emit_label (label);
4458 /* Given conditional stores, the offset can no longer be
4459 known, so clear it. */
4460 clear_mem_offset (to);
4464 return true;
4467 /* Helper function to do the actual work for expand_builtin_memset. The
4468 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4469 so that this can also be called without constructing an actual CALL_EXPR.
4470 The other arguments and return value are the same as for
4471 expand_builtin_memset. */
4473 static rtx
4474 expand_builtin_memset_args (tree dest, tree val, tree len,
4475 rtx target, machine_mode mode, tree orig_exp)
4477 tree fndecl, fn;
4478 enum built_in_function fcode;
4479 machine_mode val_mode;
4480 char c;
4481 unsigned int dest_align;
4482 rtx dest_mem, dest_addr, len_rtx;
4483 HOST_WIDE_INT expected_size = -1;
4484 unsigned int expected_align = 0;
4485 unsigned HOST_WIDE_INT min_size;
4486 unsigned HOST_WIDE_INT max_size;
4487 unsigned HOST_WIDE_INT probable_max_size;
4489 dest_align = get_pointer_alignment (dest);
4491 /* If DEST is not a pointer type, don't do this operation in-line. */
4492 if (dest_align == 0)
4493 return NULL_RTX;
4495 if (currently_expanding_gimple_stmt)
4496 stringop_block_profile (currently_expanding_gimple_stmt,
4497 &expected_align, &expected_size);
4499 if (expected_align < dest_align)
4500 expected_align = dest_align;
4502 /* If the LEN parameter is zero, return DEST. */
4503 if (integer_zerop (len))
4505 /* Evaluate and ignore VAL in case it has side-effects. */
4506 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4507 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4510 /* Stabilize the arguments in case we fail. */
4511 dest = builtin_save_expr (dest);
4512 val = builtin_save_expr (val);
4513 len = builtin_save_expr (len);
4515 len_rtx = expand_normal (len);
4516 determine_block_size (len, len_rtx, &min_size, &max_size,
4517 &probable_max_size);
4518 dest_mem = get_memory_rtx (dest, len);
4519 val_mode = TYPE_MODE (unsigned_char_type_node);
4521 if (TREE_CODE (val) != INTEGER_CST
4522 || target_char_cast (val, &c))
4524 rtx val_rtx;
4526 val_rtx = expand_normal (val);
4527 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4529 /* Assume that we can memset by pieces if we can store
4530 * the coefficients by pieces (in the required modes).
4531 * We can't pass builtin_memset_gen_str as that emits RTL. */
4532 c = 1;
4533 if (tree_fits_uhwi_p (len)
4534 && can_store_by_pieces (tree_to_uhwi (len),
4535 builtin_memset_read_str, &c, dest_align,
4536 true))
4538 val_rtx = force_reg (val_mode, val_rtx);
4539 store_by_pieces (dest_mem, tree_to_uhwi (len),
4540 builtin_memset_gen_str, val_rtx, dest_align,
4541 true, RETURN_BEGIN);
4543 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4544 dest_align, expected_align,
4545 expected_size, min_size, max_size,
4546 probable_max_size)
4547 && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4548 tree_ctz (len),
4549 min_size, max_size,
4550 val_rtx, 0,
4551 dest_align))
4552 goto do_libcall;
4554 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4555 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4556 return dest_mem;
4559 if (c)
4561 if (tree_fits_uhwi_p (len)
4562 && can_store_by_pieces (tree_to_uhwi (len),
4563 builtin_memset_read_str, &c, dest_align,
4564 true))
4565 store_by_pieces (dest_mem, tree_to_uhwi (len),
4566 builtin_memset_read_str, &c, dest_align, true,
4567 RETURN_BEGIN);
4568 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4569 gen_int_mode (c, val_mode),
4570 dest_align, expected_align,
4571 expected_size, min_size, max_size,
4572 probable_max_size)
4573 && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4574 tree_ctz (len),
4575 min_size, max_size,
4576 NULL_RTX, c,
4577 dest_align))
4578 goto do_libcall;
4580 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4581 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4582 return dest_mem;
4585 set_mem_align (dest_mem, dest_align);
4586 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4587 CALL_EXPR_TAILCALL (orig_exp)
4588 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4589 expected_align, expected_size,
4590 min_size, max_size,
4591 probable_max_size, tree_ctz (len));
4593 if (dest_addr == 0)
4595 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4596 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4599 return dest_addr;
4601 do_libcall:
4602 fndecl = get_callee_fndecl (orig_exp);
4603 fcode = DECL_FUNCTION_CODE (fndecl);
4604 if (fcode == BUILT_IN_MEMSET)
4605 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4606 dest, val, len);
4607 else if (fcode == BUILT_IN_BZERO)
4608 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4609 dest, len);
4610 else
4611 gcc_unreachable ();
4612 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4613 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4614 return expand_call (fn, target, target == const0_rtx);
4617 /* Expand expression EXP, which is a call to the bzero builtin. Return
4618 NULL_RTX if we failed the caller should emit a normal call. */
4620 static rtx
4621 expand_builtin_bzero (tree exp)
4623 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4624 return NULL_RTX;
4626 tree dest = CALL_EXPR_ARG (exp, 0);
4627 tree size = CALL_EXPR_ARG (exp, 1);
4629 /* New argument list transforming bzero(ptr x, int y) to
4630 memset(ptr x, int 0, size_t y). This is done this way
4631 so that if it isn't expanded inline, we fallback to
4632 calling bzero instead of memset. */
4634 location_t loc = EXPR_LOCATION (exp);
4636 return expand_builtin_memset_args (dest, integer_zero_node,
4637 fold_convert_loc (loc,
4638 size_type_node, size),
4639 const0_rtx, VOIDmode, exp);
4642 /* Try to expand cmpstr operation ICODE with the given operands.
4643 Return the result rtx on success, otherwise return null. */
4645 static rtx
4646 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4647 HOST_WIDE_INT align)
4649 machine_mode insn_mode = insn_data[icode].operand[0].mode;
4651 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4652 target = NULL_RTX;
4654 class expand_operand ops[4];
4655 create_output_operand (&ops[0], target, insn_mode);
4656 create_fixed_operand (&ops[1], arg1_rtx);
4657 create_fixed_operand (&ops[2], arg2_rtx);
4658 create_integer_operand (&ops[3], align);
4659 if (maybe_expand_insn (icode, 4, ops))
4660 return ops[0].value;
4661 return NULL_RTX;
4664 /* Expand expression EXP, which is a call to the memcmp built-in function.
4665 Return NULL_RTX if we failed and the caller should emit a normal call,
4666 otherwise try to get the result in TARGET, if convenient.
4667 RESULT_EQ is true if we can relax the returned value to be either zero
4668 or nonzero, without caring about the sign. */
4670 static rtx
4671 expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4673 if (!validate_arglist (exp,
4674 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4675 return NULL_RTX;
4677 tree arg1 = CALL_EXPR_ARG (exp, 0);
4678 tree arg2 = CALL_EXPR_ARG (exp, 1);
4679 tree len = CALL_EXPR_ARG (exp, 2);
4681 /* Due to the performance benefit, always inline the calls first
4682 when result_eq is false. */
4683 rtx result = NULL_RTX;
4684 enum built_in_function fcode = DECL_FUNCTION_CODE (get_callee_fndecl (exp));
4685 if (!result_eq && fcode != BUILT_IN_BCMP)
4687 result = inline_expand_builtin_bytecmp (exp, target);
4688 if (result)
4689 return result;
4692 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4693 location_t loc = EXPR_LOCATION (exp);
4695 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4696 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4698 /* If we don't have POINTER_TYPE, call the function. */
4699 if (arg1_align == 0 || arg2_align == 0)
4700 return NULL_RTX;
4702 rtx arg1_rtx = get_memory_rtx (arg1, len);
4703 rtx arg2_rtx = get_memory_rtx (arg2, len);
4704 rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4706 /* Set MEM_SIZE as appropriate. */
4707 if (CONST_INT_P (len_rtx))
4709 set_mem_size (arg1_rtx, INTVAL (len_rtx));
4710 set_mem_size (arg2_rtx, INTVAL (len_rtx));
4713 by_pieces_constfn constfn = NULL;
4715 /* Try to get the byte representation of the constant ARG2 (or, only
4716 when the function's result is used for equality to zero, ARG1)
4717 points to, with its byte size in NBYTES. */
4718 unsigned HOST_WIDE_INT nbytes;
4719 const char *rep = getbyterep (arg2, &nbytes);
4720 if (result_eq && rep == NULL)
4722 /* For equality to zero the arguments are interchangeable. */
4723 rep = getbyterep (arg1, &nbytes);
4724 if (rep != NULL)
4725 std::swap (arg1_rtx, arg2_rtx);
4728 /* If the function's constant bound LEN_RTX is less than or equal
4729 to the byte size of the representation of the constant argument,
4730 and if block move would be done by pieces, we can avoid loading
4731 the bytes from memory and only store the computed constant result. */
4732 if (rep
4733 && CONST_INT_P (len_rtx)
4734 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes)
4735 constfn = builtin_memcpy_read_str;
4737 result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4738 TREE_TYPE (len), target,
4739 result_eq, constfn,
4740 CONST_CAST (char *, rep));
4742 if (result)
4744 /* Return the value in the proper mode for this function. */
4745 if (GET_MODE (result) == mode)
4746 return result;
4748 if (target != 0)
4750 convert_move (target, result, 0);
4751 return target;
4754 return convert_to_mode (mode, result, 0);
4757 return NULL_RTX;
4760 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4761 if we failed the caller should emit a normal call, otherwise try to get
4762 the result in TARGET, if convenient. */
4764 static rtx
4765 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4767 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4768 return NULL_RTX;
4770 tree arg1 = CALL_EXPR_ARG (exp, 0);
4771 tree arg2 = CALL_EXPR_ARG (exp, 1);
4773 /* Due to the performance benefit, always inline the calls first. */
4774 rtx result = NULL_RTX;
4775 result = inline_expand_builtin_bytecmp (exp, target);
4776 if (result)
4777 return result;
4779 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4780 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4781 if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4782 return NULL_RTX;
4784 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4785 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4787 /* If we don't have POINTER_TYPE, call the function. */
4788 if (arg1_align == 0 || arg2_align == 0)
4789 return NULL_RTX;
4791 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4792 arg1 = builtin_save_expr (arg1);
4793 arg2 = builtin_save_expr (arg2);
4795 rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4796 rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4798 /* Try to call cmpstrsi. */
4799 if (cmpstr_icode != CODE_FOR_nothing)
4800 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4801 MIN (arg1_align, arg2_align));
4803 /* Try to determine at least one length and call cmpstrnsi. */
4804 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4806 tree len;
4807 rtx arg3_rtx;
4809 tree len1 = c_strlen (arg1, 1);
4810 tree len2 = c_strlen (arg2, 1);
4812 if (len1)
4813 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4814 if (len2)
4815 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4817 /* If we don't have a constant length for the first, use the length
4818 of the second, if we know it. We don't require a constant for
4819 this case; some cost analysis could be done if both are available
4820 but neither is constant. For now, assume they're equally cheap,
4821 unless one has side effects. If both strings have constant lengths,
4822 use the smaller. */
4824 if (!len1)
4825 len = len2;
4826 else if (!len2)
4827 len = len1;
4828 else if (TREE_SIDE_EFFECTS (len1))
4829 len = len2;
4830 else if (TREE_SIDE_EFFECTS (len2))
4831 len = len1;
4832 else if (TREE_CODE (len1) != INTEGER_CST)
4833 len = len2;
4834 else if (TREE_CODE (len2) != INTEGER_CST)
4835 len = len1;
4836 else if (tree_int_cst_lt (len1, len2))
4837 len = len1;
4838 else
4839 len = len2;
4841 /* If both arguments have side effects, we cannot optimize. */
4842 if (len && !TREE_SIDE_EFFECTS (len))
4844 arg3_rtx = expand_normal (len);
4845 result = expand_cmpstrn_or_cmpmem
4846 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4847 arg3_rtx, MIN (arg1_align, arg2_align));
4851 tree fndecl = get_callee_fndecl (exp);
4852 if (result)
4854 /* Return the value in the proper mode for this function. */
4855 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4856 if (GET_MODE (result) == mode)
4857 return result;
4858 if (target == 0)
4859 return convert_to_mode (mode, result, 0);
4860 convert_move (target, result, 0);
4861 return target;
4864 /* Expand the library call ourselves using a stabilized argument
4865 list to avoid re-evaluating the function's arguments twice. */
4866 tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4867 copy_warning (fn, exp);
4868 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4869 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4870 return expand_call (fn, target, target == const0_rtx);
4873 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4874 NULL_RTX if we failed the caller should emit a normal call, otherwise
4875 try to get the result in TARGET, if convenient. */
4877 static rtx
4878 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4879 ATTRIBUTE_UNUSED machine_mode mode)
4881 if (!validate_arglist (exp,
4882 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4883 return NULL_RTX;
4885 tree arg1 = CALL_EXPR_ARG (exp, 0);
4886 tree arg2 = CALL_EXPR_ARG (exp, 1);
4887 tree arg3 = CALL_EXPR_ARG (exp, 2);
4889 location_t loc = EXPR_LOCATION (exp);
4890 tree len1 = c_strlen (arg1, 1);
4891 tree len2 = c_strlen (arg2, 1);
4893 /* Due to the performance benefit, always inline the calls first. */
4894 rtx result = NULL_RTX;
4895 result = inline_expand_builtin_bytecmp (exp, target);
4896 if (result)
4897 return result;
4899 /* If c_strlen can determine an expression for one of the string
4900 lengths, and it doesn't have side effects, then emit cmpstrnsi
4901 using length MIN(strlen(string)+1, arg3). */
4902 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4903 if (cmpstrn_icode == CODE_FOR_nothing)
4904 return NULL_RTX;
4906 tree len;
4908 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4909 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4911 if (len1)
4912 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4913 if (len2)
4914 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4916 tree len3 = fold_convert_loc (loc, sizetype, arg3);
4918 /* If we don't have a constant length for the first, use the length
4919 of the second, if we know it. If neither string is constant length,
4920 use the given length argument. We don't require a constant for
4921 this case; some cost analysis could be done if both are available
4922 but neither is constant. For now, assume they're equally cheap,
4923 unless one has side effects. If both strings have constant lengths,
4924 use the smaller. */
4926 if (!len1 && !len2)
4927 len = len3;
4928 else if (!len1)
4929 len = len2;
4930 else if (!len2)
4931 len = len1;
4932 else if (TREE_SIDE_EFFECTS (len1))
4933 len = len2;
4934 else if (TREE_SIDE_EFFECTS (len2))
4935 len = len1;
4936 else if (TREE_CODE (len1) != INTEGER_CST)
4937 len = len2;
4938 else if (TREE_CODE (len2) != INTEGER_CST)
4939 len = len1;
4940 else if (tree_int_cst_lt (len1, len2))
4941 len = len1;
4942 else
4943 len = len2;
4945 /* If we are not using the given length, we must incorporate it here.
4946 The actual new length parameter will be MIN(len,arg3) in this case. */
4947 if (len != len3)
4949 len = fold_convert_loc (loc, sizetype, len);
4950 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
4952 rtx arg1_rtx = get_memory_rtx (arg1, len);
4953 rtx arg2_rtx = get_memory_rtx (arg2, len);
4954 rtx arg3_rtx = expand_normal (len);
4955 result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4956 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4957 MIN (arg1_align, arg2_align));
4959 tree fndecl = get_callee_fndecl (exp);
4960 if (result)
4962 /* Return the value in the proper mode for this function. */
4963 mode = TYPE_MODE (TREE_TYPE (exp));
4964 if (GET_MODE (result) == mode)
4965 return result;
4966 if (target == 0)
4967 return convert_to_mode (mode, result, 0);
4968 convert_move (target, result, 0);
4969 return target;
4972 /* Expand the library call ourselves using a stabilized argument
4973 list to avoid re-evaluating the function's arguments twice. */
4974 tree call = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
4975 copy_warning (call, exp);
4976 gcc_assert (TREE_CODE (call) == CALL_EXPR);
4977 CALL_EXPR_TAILCALL (call) = CALL_EXPR_TAILCALL (exp);
4978 return expand_call (call, target, target == const0_rtx);
4981 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4982 if that's convenient. */
4985 expand_builtin_saveregs (void)
4987 rtx val;
4988 rtx_insn *seq;
4990 /* Don't do __builtin_saveregs more than once in a function.
4991 Save the result of the first call and reuse it. */
4992 if (saveregs_value != 0)
4993 return saveregs_value;
4995 /* When this function is called, it means that registers must be
4996 saved on entry to this function. So we migrate the call to the
4997 first insn of this function. */
4999 start_sequence ();
5001 /* Do whatever the machine needs done in this case. */
5002 val = targetm.calls.expand_builtin_saveregs ();
5004 seq = get_insns ();
5005 end_sequence ();
5007 saveregs_value = val;
5009 /* Put the insns after the NOTE that starts the function. If this
5010 is inside a start_sequence, make the outer-level insn chain current, so
5011 the code is placed at the start of the function. */
5012 push_topmost_sequence ();
5013 emit_insn_after (seq, entry_of_function ());
5014 pop_topmost_sequence ();
5016 return val;
5019 /* Expand a call to __builtin_next_arg. */
5021 static rtx
5022 expand_builtin_next_arg (void)
5024 /* Checking arguments is already done in fold_builtin_next_arg
5025 that must be called before this function. */
5026 return expand_binop (ptr_mode, add_optab,
5027 crtl->args.internal_arg_pointer,
5028 crtl->args.arg_offset_rtx,
5029 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5032 /* Make it easier for the backends by protecting the valist argument
5033 from multiple evaluations. */
5035 static tree
5036 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
5038 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
5040 /* The current way of determining the type of valist is completely
5041 bogus. We should have the information on the va builtin instead. */
5042 if (!vatype)
5043 vatype = targetm.fn_abi_va_list (cfun->decl);
5045 if (TREE_CODE (vatype) == ARRAY_TYPE)
5047 if (TREE_SIDE_EFFECTS (valist))
5048 valist = save_expr (valist);
5050 /* For this case, the backends will be expecting a pointer to
5051 vatype, but it's possible we've actually been given an array
5052 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
5053 So fix it. */
5054 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
5056 tree p1 = build_pointer_type (TREE_TYPE (vatype));
5057 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
5060 else
5062 tree pt = build_pointer_type (vatype);
5064 if (! needs_lvalue)
5066 if (! TREE_SIDE_EFFECTS (valist))
5067 return valist;
5069 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
5070 TREE_SIDE_EFFECTS (valist) = 1;
5073 if (TREE_SIDE_EFFECTS (valist))
5074 valist = save_expr (valist);
5075 valist = fold_build2_loc (loc, MEM_REF,
5076 vatype, valist, build_int_cst (pt, 0));
5079 return valist;
5082 /* The "standard" definition of va_list is void*. */
5084 tree
5085 std_build_builtin_va_list (void)
5087 return ptr_type_node;
5090 /* The "standard" abi va_list is va_list_type_node. */
5092 tree
5093 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
5095 return va_list_type_node;
5098 /* The "standard" type of va_list is va_list_type_node. */
5100 tree
5101 std_canonical_va_list_type (tree type)
5103 tree wtype, htype;
5105 wtype = va_list_type_node;
5106 htype = type;
5108 if (TREE_CODE (wtype) == ARRAY_TYPE)
5110 /* If va_list is an array type, the argument may have decayed
5111 to a pointer type, e.g. by being passed to another function.
5112 In that case, unwrap both types so that we can compare the
5113 underlying records. */
5114 if (TREE_CODE (htype) == ARRAY_TYPE
5115 || POINTER_TYPE_P (htype))
5117 wtype = TREE_TYPE (wtype);
5118 htype = TREE_TYPE (htype);
5121 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
5122 return va_list_type_node;
5124 return NULL_TREE;
5127 /* The "standard" implementation of va_start: just assign `nextarg' to
5128 the variable. */
5130 void
5131 std_expand_builtin_va_start (tree valist, rtx nextarg)
5133 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
5134 convert_move (va_r, nextarg, 0);
5137 /* Expand EXP, a call to __builtin_va_start. */
5139 static rtx
5140 expand_builtin_va_start (tree exp)
5142 rtx nextarg;
5143 tree valist;
5144 location_t loc = EXPR_LOCATION (exp);
5146 if (call_expr_nargs (exp) < 2)
5148 error_at (loc, "too few arguments to function %<va_start%>");
5149 return const0_rtx;
5152 if (fold_builtin_next_arg (exp, true))
5153 return const0_rtx;
5155 nextarg = expand_builtin_next_arg ();
5156 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
5158 if (targetm.expand_builtin_va_start)
5159 targetm.expand_builtin_va_start (valist, nextarg);
5160 else
5161 std_expand_builtin_va_start (valist, nextarg);
5163 return const0_rtx;
5166 /* Expand EXP, a call to __builtin_va_end. */
5168 static rtx
5169 expand_builtin_va_end (tree exp)
5171 tree valist = CALL_EXPR_ARG (exp, 0);
5173 /* Evaluate for side effects, if needed. I hate macros that don't
5174 do that. */
5175 if (TREE_SIDE_EFFECTS (valist))
5176 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
5178 return const0_rtx;
5181 /* Expand EXP, a call to __builtin_va_copy. We do this as a
5182 builtin rather than just as an assignment in stdarg.h because of the
5183 nastiness of array-type va_list types. */
5185 static rtx
5186 expand_builtin_va_copy (tree exp)
5188 tree dst, src, t;
5189 location_t loc = EXPR_LOCATION (exp);
5191 dst = CALL_EXPR_ARG (exp, 0);
5192 src = CALL_EXPR_ARG (exp, 1);
5194 dst = stabilize_va_list_loc (loc, dst, 1);
5195 src = stabilize_va_list_loc (loc, src, 0);
5197 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5199 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
5201 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
5202 TREE_SIDE_EFFECTS (t) = 1;
5203 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5205 else
5207 rtx dstb, srcb, size;
5209 /* Evaluate to pointers. */
5210 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5211 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5212 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5213 NULL_RTX, VOIDmode, EXPAND_NORMAL);
5215 dstb = convert_memory_address (Pmode, dstb);
5216 srcb = convert_memory_address (Pmode, srcb);
5218 /* "Dereference" to BLKmode memories. */
5219 dstb = gen_rtx_MEM (BLKmode, dstb);
5220 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5221 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5222 srcb = gen_rtx_MEM (BLKmode, srcb);
5223 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5224 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5226 /* Copy. */
5227 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5230 return const0_rtx;
5233 /* Expand a call to one of the builtin functions __builtin_frame_address or
5234 __builtin_return_address. */
5236 static rtx
5237 expand_builtin_frame_address (tree fndecl, tree exp)
5239 /* The argument must be a nonnegative integer constant.
5240 It counts the number of frames to scan up the stack.
5241 The value is either the frame pointer value or the return
5242 address saved in that frame. */
5243 if (call_expr_nargs (exp) == 0)
5244 /* Warning about missing arg was already issued. */
5245 return const0_rtx;
5246 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
5248 error ("invalid argument to %qD", fndecl);
5249 return const0_rtx;
5251 else
5253 /* Number of frames to scan up the stack. */
5254 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
5256 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
5258 /* Some ports cannot access arbitrary stack frames. */
5259 if (tem == NULL)
5261 warning (0, "unsupported argument to %qD", fndecl);
5262 return const0_rtx;
5265 if (count)
5267 /* Warn since no effort is made to ensure that any frame
5268 beyond the current one exists or can be safely reached. */
5269 warning (OPT_Wframe_address, "calling %qD with "
5270 "a nonzero argument is unsafe", fndecl);
5273 /* For __builtin_frame_address, return what we've got. */
5274 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5275 return tem;
5277 if (!REG_P (tem)
5278 && ! CONSTANT_P (tem))
5279 tem = copy_addr_to_reg (tem);
5280 return tem;
5284 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5285 failed and the caller should emit a normal call. */
5287 static rtx
5288 expand_builtin_alloca (tree exp)
5290 rtx op0;
5291 rtx result;
5292 unsigned int align;
5293 tree fndecl = get_callee_fndecl (exp);
5294 HOST_WIDE_INT max_size;
5295 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5296 bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5297 bool valid_arglist
5298 = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5299 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5300 VOID_TYPE)
5301 : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5302 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5303 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5305 if (!valid_arglist)
5306 return NULL_RTX;
5308 /* Compute the argument. */
5309 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5311 /* Compute the alignment. */
5312 align = (fcode == BUILT_IN_ALLOCA
5313 ? BIGGEST_ALIGNMENT
5314 : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5316 /* Compute the maximum size. */
5317 max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5318 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2))
5319 : -1);
5321 /* Allocate the desired space. If the allocation stems from the declaration
5322 of a variable-sized object, it cannot accumulate. */
5323 result
5324 = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5325 result = convert_memory_address (ptr_mode, result);
5327 /* Dynamic allocations for variables are recorded during gimplification. */
5328 if (!alloca_for_var && (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC))
5329 record_dynamic_alloc (exp);
5331 return result;
5334 /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
5335 of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
5336 STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
5337 handle_builtin_stack_restore function. */
5339 static rtx
5340 expand_asan_emit_allocas_unpoison (tree exp)
5342 tree arg0 = CALL_EXPR_ARG (exp, 0);
5343 tree arg1 = CALL_EXPR_ARG (exp, 1);
5344 rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5345 rtx bot = expand_expr (arg1, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5346 rtx off = expand_simple_binop (Pmode, MINUS, virtual_stack_dynamic_rtx,
5347 stack_pointer_rtx, NULL_RTX, 0,
5348 OPTAB_LIB_WIDEN);
5349 off = convert_modes (ptr_mode, Pmode, off, 0);
5350 bot = expand_simple_binop (ptr_mode, PLUS, bot, off, NULL_RTX, 0,
5351 OPTAB_LIB_WIDEN);
5352 rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
5353 ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
5354 top, ptr_mode, bot, ptr_mode);
5355 return ret;
5358 /* Expand a call to bswap builtin in EXP.
5359 Return NULL_RTX if a normal call should be emitted rather than expanding the
5360 function in-line. If convenient, the result should be placed in TARGET.
5361 SUBTARGET may be used as the target for computing one of EXP's operands. */
5363 static rtx
5364 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5365 rtx subtarget)
5367 tree arg;
5368 rtx op0;
5370 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5371 return NULL_RTX;
5373 arg = CALL_EXPR_ARG (exp, 0);
5374 op0 = expand_expr (arg,
5375 subtarget && GET_MODE (subtarget) == target_mode
5376 ? subtarget : NULL_RTX,
5377 target_mode, EXPAND_NORMAL);
5378 if (GET_MODE (op0) != target_mode)
5379 op0 = convert_to_mode (target_mode, op0, 1);
5381 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5383 gcc_assert (target);
5385 return convert_to_mode (target_mode, target, 1);
5388 /* Expand a call to a unary builtin in EXP.
5389 Return NULL_RTX if a normal call should be emitted rather than expanding the
5390 function in-line. If convenient, the result should be placed in TARGET.
5391 SUBTARGET may be used as the target for computing one of EXP's operands. */
5393 static rtx
5394 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5395 rtx subtarget, optab op_optab)
5397 rtx op0;
5399 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5400 return NULL_RTX;
5402 /* Compute the argument. */
5403 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5404 (subtarget
5405 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5406 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5407 VOIDmode, EXPAND_NORMAL);
5408 /* Compute op, into TARGET if possible.
5409 Set TARGET to wherever the result comes back. */
5410 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5411 op_optab, op0, target, op_optab != clrsb_optab);
5412 gcc_assert (target);
5414 return convert_to_mode (target_mode, target, 0);
5417 /* Expand a call to __builtin_expect. We just return our argument
5418 as the builtin_expect semantic should've been already executed by
5419 tree branch prediction pass. */
5421 static rtx
5422 expand_builtin_expect (tree exp, rtx target)
5424 tree arg;
5426 if (call_expr_nargs (exp) < 2)
5427 return const0_rtx;
5428 arg = CALL_EXPR_ARG (exp, 0);
5430 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5431 /* When guessing was done, the hints should be already stripped away. */
5432 gcc_assert (!flag_guess_branch_prob
5433 || optimize == 0 || seen_error ());
5434 return target;
5437 /* Expand a call to __builtin_expect_with_probability. We just return our
5438 argument as the builtin_expect semantic should've been already executed by
5439 tree branch prediction pass. */
5441 static rtx
5442 expand_builtin_expect_with_probability (tree exp, rtx target)
5444 tree arg;
5446 if (call_expr_nargs (exp) < 3)
5447 return const0_rtx;
5448 arg = CALL_EXPR_ARG (exp, 0);
5450 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5451 /* When guessing was done, the hints should be already stripped away. */
5452 gcc_assert (!flag_guess_branch_prob
5453 || optimize == 0 || seen_error ());
5454 return target;
5458 /* Expand a call to __builtin_assume_aligned. We just return our first
5459 argument as the builtin_assume_aligned semantic should've been already
5460 executed by CCP. */
5462 static rtx
5463 expand_builtin_assume_aligned (tree exp, rtx target)
5465 if (call_expr_nargs (exp) < 2)
5466 return const0_rtx;
5467 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
5468 EXPAND_NORMAL);
5469 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
5470 && (call_expr_nargs (exp) < 3
5471 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
5472 return target;
5475 void
5476 expand_builtin_trap (void)
5478 if (targetm.have_trap ())
5480 rtx_insn *insn = emit_insn (targetm.gen_trap ());
5481 /* For trap insns when not accumulating outgoing args force
5482 REG_ARGS_SIZE note to prevent crossjumping of calls with
5483 different args sizes. */
5484 if (!ACCUMULATE_OUTGOING_ARGS)
5485 add_args_size_note (insn, stack_pointer_delta);
5487 else
5489 tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
5490 tree call_expr = build_call_expr (fn, 0);
5491 expand_call (call_expr, NULL_RTX, false);
5494 emit_barrier ();
5497 /* Expand a call to __builtin_unreachable. We do nothing except emit
5498 a barrier saying that control flow will not pass here.
5500 It is the responsibility of the program being compiled to ensure
5501 that control flow does never reach __builtin_unreachable. */
5502 static void
5503 expand_builtin_unreachable (void)
5505 /* Use gimple_build_builtin_unreachable or builtin_decl_unreachable
5506 to avoid this. */
5507 gcc_checking_assert (!sanitize_flags_p (SANITIZE_UNREACHABLE));
5508 emit_barrier ();
5511 /* Expand EXP, a call to fabs, fabsf or fabsl.
5512 Return NULL_RTX if a normal call should be emitted rather than expanding
5513 the function inline. If convenient, the result should be placed
5514 in TARGET. SUBTARGET may be used as the target for computing
5515 the operand. */
5517 static rtx
5518 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5520 machine_mode mode;
5521 tree arg;
5522 rtx op0;
5524 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5525 return NULL_RTX;
5527 arg = CALL_EXPR_ARG (exp, 0);
5528 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5529 mode = TYPE_MODE (TREE_TYPE (arg));
5530 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5531 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5534 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5535 Return NULL is a normal call should be emitted rather than expanding the
5536 function inline. If convenient, the result should be placed in TARGET.
5537 SUBTARGET may be used as the target for computing the operand. */
5539 static rtx
5540 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5542 rtx op0, op1;
5543 tree arg;
5545 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5546 return NULL_RTX;
5548 arg = CALL_EXPR_ARG (exp, 0);
5549 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5551 arg = CALL_EXPR_ARG (exp, 1);
5552 op1 = expand_normal (arg);
5554 return expand_copysign (op0, op1, target);
5557 /* Emit a call to __builtin___clear_cache. */
5559 void
5560 default_emit_call_builtin___clear_cache (rtx begin, rtx end)
5562 rtx callee = gen_rtx_SYMBOL_REF (Pmode,
5563 BUILTIN_ASM_NAME_PTR
5564 (BUILT_IN_CLEAR_CACHE));
5566 emit_library_call (callee,
5567 LCT_NORMAL, VOIDmode,
5568 convert_memory_address (ptr_mode, begin), ptr_mode,
5569 convert_memory_address (ptr_mode, end), ptr_mode);
5572 /* Emit a call to __builtin___clear_cache, unless the target specifies
5573 it as do-nothing. This function can be used by trampoline
5574 finalizers to duplicate the effects of expanding a call to the
5575 clear_cache builtin. */
5577 void
5578 maybe_emit_call_builtin___clear_cache (rtx begin, rtx end)
5580 gcc_assert ((GET_MODE (begin) == ptr_mode || GET_MODE (begin) == Pmode
5581 || CONST_INT_P (begin))
5582 && (GET_MODE (end) == ptr_mode || GET_MODE (end) == Pmode
5583 || CONST_INT_P (end)));
5585 if (targetm.have_clear_cache ())
5587 /* We have a "clear_cache" insn, and it will handle everything. */
5588 class expand_operand ops[2];
5590 create_address_operand (&ops[0], begin);
5591 create_address_operand (&ops[1], end);
5593 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
5594 return;
5596 else
5598 #ifndef CLEAR_INSN_CACHE
5599 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5600 does nothing. There is no need to call it. Do nothing. */
5601 return;
5602 #endif /* CLEAR_INSN_CACHE */
5605 targetm.calls.emit_call_builtin___clear_cache (begin, end);
5608 /* Expand a call to __builtin___clear_cache. */
5610 static void
5611 expand_builtin___clear_cache (tree exp)
5613 tree begin, end;
5614 rtx begin_rtx, end_rtx;
5616 /* We must not expand to a library call. If we did, any
5617 fallback library function in libgcc that might contain a call to
5618 __builtin___clear_cache() would recurse infinitely. */
5619 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5621 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5622 return;
5625 begin = CALL_EXPR_ARG (exp, 0);
5626 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5628 end = CALL_EXPR_ARG (exp, 1);
5629 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5631 maybe_emit_call_builtin___clear_cache (begin_rtx, end_rtx);
5634 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5636 static rtx
5637 round_trampoline_addr (rtx tramp)
5639 rtx temp, addend, mask;
5641 /* If we don't need too much alignment, we'll have been guaranteed
5642 proper alignment by get_trampoline_type. */
5643 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5644 return tramp;
5646 /* Round address up to desired boundary. */
5647 temp = gen_reg_rtx (Pmode);
5648 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
5649 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
5651 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5652 temp, 0, OPTAB_LIB_WIDEN);
5653 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5654 temp, 0, OPTAB_LIB_WIDEN);
5656 return tramp;
5659 static rtx
5660 expand_builtin_init_trampoline (tree exp, bool onstack)
5662 tree t_tramp, t_func, t_chain;
5663 rtx m_tramp, r_tramp, r_chain, tmp;
5665 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5666 POINTER_TYPE, VOID_TYPE))
5667 return NULL_RTX;
5669 t_tramp = CALL_EXPR_ARG (exp, 0);
5670 t_func = CALL_EXPR_ARG (exp, 1);
5671 t_chain = CALL_EXPR_ARG (exp, 2);
5673 r_tramp = expand_normal (t_tramp);
5674 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5675 MEM_NOTRAP_P (m_tramp) = 1;
5677 /* If ONSTACK, the TRAMP argument should be the address of a field
5678 within the local function's FRAME decl. Either way, let's see if
5679 we can fill in the MEM_ATTRs for this memory. */
5680 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5681 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
5683 /* Creator of a heap trampoline is responsible for making sure the
5684 address is aligned to at least STACK_BOUNDARY. Normally malloc
5685 will ensure this anyhow. */
5686 tmp = round_trampoline_addr (r_tramp);
5687 if (tmp != r_tramp)
5689 m_tramp = change_address (m_tramp, BLKmode, tmp);
5690 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5691 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
5694 /* The FUNC argument should be the address of the nested function.
5695 Extract the actual function decl to pass to the hook. */
5696 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5697 t_func = TREE_OPERAND (t_func, 0);
5698 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5700 r_chain = expand_normal (t_chain);
5702 /* Generate insns to initialize the trampoline. */
5703 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5705 if (onstack)
5707 trampolines_created = 1;
5709 if (targetm.calls.custom_function_descriptors != 0)
5710 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5711 "trampoline generated for nested function %qD", t_func);
5714 return const0_rtx;
5717 static rtx
5718 expand_builtin_adjust_trampoline (tree exp)
5720 rtx tramp;
5722 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5723 return NULL_RTX;
5725 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5726 tramp = round_trampoline_addr (tramp);
5727 if (targetm.calls.trampoline_adjust_address)
5728 tramp = targetm.calls.trampoline_adjust_address (tramp);
5730 return tramp;
5733 /* Expand a call to the builtin descriptor initialization routine.
5734 A descriptor is made up of a couple of pointers to the static
5735 chain and the code entry in this order. */
5737 static rtx
5738 expand_builtin_init_descriptor (tree exp)
5740 tree t_descr, t_func, t_chain;
5741 rtx m_descr, r_descr, r_func, r_chain;
5743 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
5744 VOID_TYPE))
5745 return NULL_RTX;
5747 t_descr = CALL_EXPR_ARG (exp, 0);
5748 t_func = CALL_EXPR_ARG (exp, 1);
5749 t_chain = CALL_EXPR_ARG (exp, 2);
5751 r_descr = expand_normal (t_descr);
5752 m_descr = gen_rtx_MEM (BLKmode, r_descr);
5753 MEM_NOTRAP_P (m_descr) = 1;
5754 set_mem_align (m_descr, GET_MODE_ALIGNMENT (ptr_mode));
5756 r_func = expand_normal (t_func);
5757 r_chain = expand_normal (t_chain);
5759 /* Generate insns to initialize the descriptor. */
5760 emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
5761 emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
5762 POINTER_SIZE / BITS_PER_UNIT), r_func);
5764 return const0_rtx;
5767 /* Expand a call to the builtin descriptor adjustment routine. */
5769 static rtx
5770 expand_builtin_adjust_descriptor (tree exp)
5772 rtx tramp;
5774 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5775 return NULL_RTX;
5777 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5779 /* Unalign the descriptor to allow runtime identification. */
5780 tramp = plus_constant (ptr_mode, tramp,
5781 targetm.calls.custom_function_descriptors);
5783 return force_operand (tramp, NULL_RTX);
5786 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5787 function. The function first checks whether the back end provides
5788 an insn to implement signbit for the respective mode. If not, it
5789 checks whether the floating point format of the value is such that
5790 the sign bit can be extracted. If that is not the case, error out.
5791 EXP is the expression that is a call to the builtin function; if
5792 convenient, the result should be placed in TARGET. */
5793 static rtx
5794 expand_builtin_signbit (tree exp, rtx target)
5796 const struct real_format *fmt;
5797 scalar_float_mode fmode;
5798 scalar_int_mode rmode, imode;
5799 tree arg;
5800 int word, bitpos;
5801 enum insn_code icode;
5802 rtx temp;
5803 location_t loc = EXPR_LOCATION (exp);
5805 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5806 return NULL_RTX;
5808 arg = CALL_EXPR_ARG (exp, 0);
5809 fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
5810 rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5811 fmt = REAL_MODE_FORMAT (fmode);
5813 arg = builtin_save_expr (arg);
5815 /* Expand the argument yielding a RTX expression. */
5816 temp = expand_normal (arg);
5818 /* Check if the back end provides an insn that handles signbit for the
5819 argument's mode. */
5820 icode = optab_handler (signbit_optab, fmode);
5821 if (icode != CODE_FOR_nothing)
5823 rtx_insn *last = get_last_insn ();
5824 rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5825 if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
5826 return this_target;
5827 delete_insns_since (last);
5830 /* For floating point formats without a sign bit, implement signbit
5831 as "ARG < 0.0". */
5832 bitpos = fmt->signbit_ro;
5833 if (bitpos < 0)
5835 /* But we can't do this if the format supports signed zero. */
5836 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
5838 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5839 build_real (TREE_TYPE (arg), dconst0));
5840 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5843 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5845 imode = int_mode_for_mode (fmode).require ();
5846 temp = gen_lowpart (imode, temp);
5848 else
5850 imode = word_mode;
5851 /* Handle targets with different FP word orders. */
5852 if (FLOAT_WORDS_BIG_ENDIAN)
5853 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5854 else
5855 word = bitpos / BITS_PER_WORD;
5856 temp = operand_subword_force (temp, word, fmode);
5857 bitpos = bitpos % BITS_PER_WORD;
5860 /* Force the intermediate word_mode (or narrower) result into a
5861 register. This avoids attempting to create paradoxical SUBREGs
5862 of floating point modes below. */
5863 temp = force_reg (imode, temp);
5865 /* If the bitpos is within the "result mode" lowpart, the operation
5866 can be implement with a single bitwise AND. Otherwise, we need
5867 a right shift and an AND. */
5869 if (bitpos < GET_MODE_BITSIZE (rmode))
5871 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5873 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5874 temp = gen_lowpart (rmode, temp);
5875 temp = expand_binop (rmode, and_optab, temp,
5876 immed_wide_int_const (mask, rmode),
5877 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5879 else
5881 /* Perform a logical right shift to place the signbit in the least
5882 significant bit, then truncate the result to the desired mode
5883 and mask just this bit. */
5884 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5885 temp = gen_lowpart (rmode, temp);
5886 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5887 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5890 return temp;
5893 /* Expand fork or exec calls. TARGET is the desired target of the
5894 call. EXP is the call. FN is the
5895 identificator of the actual function. IGNORE is nonzero if the
5896 value is to be ignored. */
5898 static rtx
5899 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5901 tree id, decl;
5902 tree call;
5904 /* If we are not profiling, just call the function. */
5905 if (!profile_arc_flag)
5906 return NULL_RTX;
5908 /* Otherwise call the wrapper. This should be equivalent for the rest of
5909 compiler, so the code does not diverge, and the wrapper may run the
5910 code necessary for keeping the profiling sane. */
5912 switch (DECL_FUNCTION_CODE (fn))
5914 case BUILT_IN_FORK:
5915 id = get_identifier ("__gcov_fork");
5916 break;
5918 case BUILT_IN_EXECL:
5919 id = get_identifier ("__gcov_execl");
5920 break;
5922 case BUILT_IN_EXECV:
5923 id = get_identifier ("__gcov_execv");
5924 break;
5926 case BUILT_IN_EXECLP:
5927 id = get_identifier ("__gcov_execlp");
5928 break;
5930 case BUILT_IN_EXECLE:
5931 id = get_identifier ("__gcov_execle");
5932 break;
5934 case BUILT_IN_EXECVP:
5935 id = get_identifier ("__gcov_execvp");
5936 break;
5938 case BUILT_IN_EXECVE:
5939 id = get_identifier ("__gcov_execve");
5940 break;
5942 default:
5943 gcc_unreachable ();
5946 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5947 FUNCTION_DECL, id, TREE_TYPE (fn));
5948 DECL_EXTERNAL (decl) = 1;
5949 TREE_PUBLIC (decl) = 1;
5950 DECL_ARTIFICIAL (decl) = 1;
5951 TREE_NOTHROW (decl) = 1;
5952 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5953 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5954 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5955 return expand_call (call, target, ignore);
5960 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5961 the pointer in these functions is void*, the tree optimizers may remove
5962 casts. The mode computed in expand_builtin isn't reliable either, due
5963 to __sync_bool_compare_and_swap.
5965 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5966 group of builtins. This gives us log2 of the mode size. */
5968 static inline machine_mode
5969 get_builtin_sync_mode (int fcode_diff)
5971 /* The size is not negotiable, so ask not to get BLKmode in return
5972 if the target indicates that a smaller size would be better. */
5973 return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require ();
5976 /* Expand the memory expression LOC and return the appropriate memory operand
5977 for the builtin_sync operations. */
5979 static rtx
5980 get_builtin_sync_mem (tree loc, machine_mode mode)
5982 rtx addr, mem;
5983 int addr_space = TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc))
5984 ? TREE_TYPE (TREE_TYPE (loc))
5985 : TREE_TYPE (loc));
5986 scalar_int_mode addr_mode = targetm.addr_space.address_mode (addr_space);
5988 addr = expand_expr (loc, NULL_RTX, addr_mode, EXPAND_SUM);
5989 addr = convert_memory_address (addr_mode, addr);
5991 /* Note that we explicitly do not want any alias information for this
5992 memory, so that we kill all other live memories. Otherwise we don't
5993 satisfy the full barrier semantics of the intrinsic. */
5994 mem = gen_rtx_MEM (mode, addr);
5996 set_mem_addr_space (mem, addr_space);
5998 mem = validize_mem (mem);
6000 /* The alignment needs to be at least according to that of the mode. */
6001 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
6002 get_pointer_alignment (loc)));
6003 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
6004 MEM_VOLATILE_P (mem) = 1;
6006 return mem;
6009 /* Make sure an argument is in the right mode.
6010 EXP is the tree argument.
6011 MODE is the mode it should be in. */
6013 static rtx
6014 expand_expr_force_mode (tree exp, machine_mode mode)
6016 rtx val;
6017 machine_mode old_mode;
6019 if (TREE_CODE (exp) == SSA_NAME
6020 && TYPE_MODE (TREE_TYPE (exp)) != mode)
6022 /* Undo argument promotion if possible, as combine might not
6023 be able to do it later due to MEM_VOLATILE_P uses in the
6024 patterns. */
6025 gimple *g = get_gimple_for_ssa_name (exp);
6026 if (g && gimple_assign_cast_p (g))
6028 tree rhs = gimple_assign_rhs1 (g);
6029 tree_code code = gimple_assign_rhs_code (g);
6030 if (CONVERT_EXPR_CODE_P (code)
6031 && TYPE_MODE (TREE_TYPE (rhs)) == mode
6032 && INTEGRAL_TYPE_P (TREE_TYPE (exp))
6033 && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
6034 && (TYPE_PRECISION (TREE_TYPE (exp))
6035 > TYPE_PRECISION (TREE_TYPE (rhs))))
6036 exp = rhs;
6040 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
6041 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6042 of CONST_INTs, where we know the old_mode only from the call argument. */
6044 old_mode = GET_MODE (val);
6045 if (old_mode == VOIDmode)
6046 old_mode = TYPE_MODE (TREE_TYPE (exp));
6047 val = convert_modes (mode, old_mode, val, 1);
6048 return val;
6052 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6053 EXP is the CALL_EXPR. CODE is the rtx code
6054 that corresponds to the arithmetic or logical operation from the name;
6055 an exception here is that NOT actually means NAND. TARGET is an optional
6056 place for us to store the results; AFTER is true if this is the
6057 fetch_and_xxx form. */
6059 static rtx
6060 expand_builtin_sync_operation (machine_mode mode, tree exp,
6061 enum rtx_code code, bool after,
6062 rtx target)
6064 rtx val, mem;
6065 location_t loc = EXPR_LOCATION (exp);
6067 if (code == NOT && warn_sync_nand)
6069 tree fndecl = get_callee_fndecl (exp);
6070 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6072 static bool warned_f_a_n, warned_n_a_f;
6074 switch (fcode)
6076 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6077 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6078 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6079 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6080 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6081 if (warned_f_a_n)
6082 break;
6084 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
6085 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6086 warned_f_a_n = true;
6087 break;
6089 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6090 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6091 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6092 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6093 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6094 if (warned_n_a_f)
6095 break;
6097 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
6098 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6099 warned_n_a_f = true;
6100 break;
6102 default:
6103 gcc_unreachable ();
6107 /* Expand the operands. */
6108 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6109 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6111 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
6112 after);
6115 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6116 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6117 true if this is the boolean form. TARGET is a place for us to store the
6118 results; this is NOT optional if IS_BOOL is true. */
6120 static rtx
6121 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
6122 bool is_bool, rtx target)
6124 rtx old_val, new_val, mem;
6125 rtx *pbool, *poval;
6127 /* Expand the operands. */
6128 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6129 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6130 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6132 pbool = poval = NULL;
6133 if (target != const0_rtx)
6135 if (is_bool)
6136 pbool = &target;
6137 else
6138 poval = &target;
6140 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
6141 false, MEMMODEL_SYNC_SEQ_CST,
6142 MEMMODEL_SYNC_SEQ_CST))
6143 return NULL_RTX;
6145 return target;
6148 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6149 general form is actually an atomic exchange, and some targets only
6150 support a reduced form with the second argument being a constant 1.
6151 EXP is the CALL_EXPR; TARGET is an optional place for us to store
6152 the results. */
6154 static rtx
6155 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
6156 rtx target)
6158 rtx val, mem;
6160 /* Expand the operands. */
6161 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6162 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6164 return expand_sync_lock_test_and_set (target, mem, val);
6167 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6169 static void
6170 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
6172 rtx mem;
6174 /* Expand the operands. */
6175 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6177 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
6180 /* Given an integer representing an ``enum memmodel'', verify its
6181 correctness and return the memory model enum. */
6183 static enum memmodel
6184 get_memmodel (tree exp)
6186 /* If the parameter is not a constant, it's a run time value so we'll just
6187 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
6188 if (TREE_CODE (exp) != INTEGER_CST)
6189 return MEMMODEL_SEQ_CST;
6191 rtx op = expand_normal (exp);
6193 unsigned HOST_WIDE_INT val = INTVAL (op);
6194 if (targetm.memmodel_check)
6195 val = targetm.memmodel_check (val);
6196 else if (val & ~MEMMODEL_MASK)
6197 return MEMMODEL_SEQ_CST;
6199 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
6200 if (memmodel_base (val) >= MEMMODEL_LAST)
6201 return MEMMODEL_SEQ_CST;
6203 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
6204 be conservative and promote consume to acquire. */
6205 if (val == MEMMODEL_CONSUME)
6206 val = MEMMODEL_ACQUIRE;
6208 return (enum memmodel) val;
6211 /* Expand the __atomic_exchange intrinsic:
6212 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
6213 EXP is the CALL_EXPR.
6214 TARGET is an optional place for us to store the results. */
6216 static rtx
6217 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
6219 rtx val, mem;
6220 enum memmodel model;
6222 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6224 if (!flag_inline_atomics)
6225 return NULL_RTX;
6227 /* Expand the operands. */
6228 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6229 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6231 return expand_atomic_exchange (target, mem, val, model);
6234 /* Expand the __atomic_compare_exchange intrinsic:
6235 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
6236 TYPE desired, BOOL weak,
6237 enum memmodel success,
6238 enum memmodel failure)
6239 EXP is the CALL_EXPR.
6240 TARGET is an optional place for us to store the results. */
6242 static rtx
6243 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
6244 rtx target)
6246 rtx expect, desired, mem, oldval;
6247 rtx_code_label *label;
6248 tree weak;
6249 bool is_weak;
6251 memmodel success = get_memmodel (CALL_EXPR_ARG (exp, 4));
6252 memmodel failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
6254 if (failure > success)
6255 success = MEMMODEL_SEQ_CST;
6257 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6259 failure = MEMMODEL_SEQ_CST;
6260 success = MEMMODEL_SEQ_CST;
6264 if (!flag_inline_atomics)
6265 return NULL_RTX;
6267 /* Expand the operands. */
6268 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6270 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
6271 expect = convert_memory_address (Pmode, expect);
6272 expect = gen_rtx_MEM (mode, expect);
6273 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6275 weak = CALL_EXPR_ARG (exp, 3);
6276 is_weak = false;
6277 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
6278 is_weak = true;
6280 if (target == const0_rtx)
6281 target = NULL;
6283 /* Lest the rtl backend create a race condition with an imporoper store
6284 to memory, always create a new pseudo for OLDVAL. */
6285 oldval = NULL;
6287 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
6288 is_weak, success, failure))
6289 return NULL_RTX;
6291 /* Conditionally store back to EXPECT, lest we create a race condition
6292 with an improper store to memory. */
6293 /* ??? With a rearrangement of atomics at the gimple level, we can handle
6294 the normal case where EXPECT is totally private, i.e. a register. At
6295 which point the store can be unconditional. */
6296 label = gen_label_rtx ();
6297 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
6298 GET_MODE (target), 1, label);
6299 emit_move_insn (expect, oldval);
6300 emit_label (label);
6302 return target;
6305 /* Helper function for expand_ifn_atomic_compare_exchange - expand
6306 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
6307 call. The weak parameter must be dropped to match the expected parameter
6308 list and the expected argument changed from value to pointer to memory
6309 slot. */
6311 static void
6312 expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
6314 unsigned int z;
6315 vec<tree, va_gc> *vec;
6317 vec_alloc (vec, 5);
6318 vec->quick_push (gimple_call_arg (call, 0));
6319 tree expected = gimple_call_arg (call, 1);
6320 rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
6321 TREE_TYPE (expected));
6322 rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
6323 if (expd != x)
6324 emit_move_insn (x, expd);
6325 tree v = make_tree (TREE_TYPE (expected), x);
6326 vec->quick_push (build1 (ADDR_EXPR,
6327 build_pointer_type (TREE_TYPE (expected)), v));
6328 vec->quick_push (gimple_call_arg (call, 2));
6329 /* Skip the boolean weak parameter. */
6330 for (z = 4; z < 6; z++)
6331 vec->quick_push (gimple_call_arg (call, z));
6332 /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6333 unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
6334 gcc_assert (bytes_log2 < 5);
6335 built_in_function fncode
6336 = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6337 + bytes_log2);
6338 tree fndecl = builtin_decl_explicit (fncode);
6339 tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
6340 fndecl);
6341 tree exp = build_call_vec (boolean_type_node, fn, vec);
6342 tree lhs = gimple_call_lhs (call);
6343 rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
6344 if (lhs)
6346 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6347 if (GET_MODE (boolret) != mode)
6348 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6349 x = force_reg (mode, x);
6350 write_complex_part (target, boolret, true, true);
6351 write_complex_part (target, x, false, false);
6355 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6357 void
6358 expand_ifn_atomic_compare_exchange (gcall *call)
6360 int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6361 gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6362 machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6364 memmodel success = get_memmodel (gimple_call_arg (call, 4));
6365 memmodel failure = get_memmodel (gimple_call_arg (call, 5));
6367 if (failure > success)
6368 success = MEMMODEL_SEQ_CST;
6370 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6372 failure = MEMMODEL_SEQ_CST;
6373 success = MEMMODEL_SEQ_CST;
6376 if (!flag_inline_atomics)
6378 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6379 return;
6382 /* Expand the operands. */
6383 rtx mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6385 rtx expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6386 rtx desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6388 bool is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6390 rtx boolret = NULL;
6391 rtx oldval = NULL;
6393 if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
6394 is_weak, success, failure))
6396 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6397 return;
6400 tree lhs = gimple_call_lhs (call);
6401 if (lhs)
6403 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6404 if (GET_MODE (boolret) != mode)
6405 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6406 write_complex_part (target, boolret, true, true);
6407 write_complex_part (target, oldval, false, false);
6411 /* Expand the __atomic_load intrinsic:
6412 TYPE __atomic_load (TYPE *object, enum memmodel)
6413 EXP is the CALL_EXPR.
6414 TARGET is an optional place for us to store the results. */
6416 static rtx
6417 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6419 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6420 if (is_mm_release (model) || is_mm_acq_rel (model))
6421 model = MEMMODEL_SEQ_CST;
6423 if (!flag_inline_atomics)
6424 return NULL_RTX;
6426 /* Expand the operand. */
6427 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6429 return expand_atomic_load (target, mem, model);
6433 /* Expand the __atomic_store intrinsic:
6434 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6435 EXP is the CALL_EXPR.
6436 TARGET is an optional place for us to store the results. */
6438 static rtx
6439 expand_builtin_atomic_store (machine_mode mode, tree exp)
6441 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6442 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6443 || is_mm_release (model)))
6444 model = MEMMODEL_SEQ_CST;
6446 if (!flag_inline_atomics)
6447 return NULL_RTX;
6449 /* Expand the operands. */
6450 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6451 rtx val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6453 return expand_atomic_store (mem, val, model, false);
6456 /* Expand the __atomic_fetch_XXX intrinsic:
6457 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6458 EXP is the CALL_EXPR.
6459 TARGET is an optional place for us to store the results.
6460 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6461 FETCH_AFTER is true if returning the result of the operation.
6462 FETCH_AFTER is false if returning the value before the operation.
6463 IGNORE is true if the result is not used.
6464 EXT_CALL is the correct builtin for an external call if this cannot be
6465 resolved to an instruction sequence. */
6467 static rtx
6468 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
6469 enum rtx_code code, bool fetch_after,
6470 bool ignore, enum built_in_function ext_call)
6472 rtx val, mem, ret;
6473 enum memmodel model;
6474 tree fndecl;
6475 tree addr;
6477 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6479 /* Expand the operands. */
6480 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6481 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6483 /* Only try generating instructions if inlining is turned on. */
6484 if (flag_inline_atomics)
6486 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6487 if (ret)
6488 return ret;
6491 /* Return if a different routine isn't needed for the library call. */
6492 if (ext_call == BUILT_IN_NONE)
6493 return NULL_RTX;
6495 /* Change the call to the specified function. */
6496 fndecl = get_callee_fndecl (exp);
6497 addr = CALL_EXPR_FN (exp);
6498 STRIP_NOPS (addr);
6500 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
6501 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
6503 /* If we will emit code after the call, the call cannot be a tail call.
6504 If it is emitted as a tail call, a barrier is emitted after it, and
6505 then all trailing code is removed. */
6506 if (!ignore)
6507 CALL_EXPR_TAILCALL (exp) = 0;
6509 /* Expand the call here so we can emit trailing code. */
6510 ret = expand_call (exp, target, ignore);
6512 /* Replace the original function just in case it matters. */
6513 TREE_OPERAND (addr, 0) = fndecl;
6515 /* Then issue the arithmetic correction to return the right result. */
6516 if (!ignore)
6518 if (code == NOT)
6520 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
6521 OPTAB_LIB_WIDEN);
6522 ret = expand_simple_unop (mode, NOT, ret, target, true);
6524 else
6525 ret = expand_simple_binop (mode, code, ret, val, target, true,
6526 OPTAB_LIB_WIDEN);
6528 return ret;
6531 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6533 void
6534 expand_ifn_atomic_bit_test_and (gcall *call)
6536 tree ptr = gimple_call_arg (call, 0);
6537 tree bit = gimple_call_arg (call, 1);
6538 tree flag = gimple_call_arg (call, 2);
6539 tree lhs = gimple_call_lhs (call);
6540 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6541 machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
6542 enum rtx_code code;
6543 optab optab;
6544 class expand_operand ops[5];
6546 gcc_assert (flag_inline_atomics);
6548 if (gimple_call_num_args (call) == 5)
6549 model = get_memmodel (gimple_call_arg (call, 3));
6551 rtx mem = get_builtin_sync_mem (ptr, mode);
6552 rtx val = expand_expr_force_mode (bit, mode);
6554 switch (gimple_call_internal_fn (call))
6556 case IFN_ATOMIC_BIT_TEST_AND_SET:
6557 code = IOR;
6558 optab = atomic_bit_test_and_set_optab;
6559 break;
6560 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
6561 code = XOR;
6562 optab = atomic_bit_test_and_complement_optab;
6563 break;
6564 case IFN_ATOMIC_BIT_TEST_AND_RESET:
6565 code = AND;
6566 optab = atomic_bit_test_and_reset_optab;
6567 break;
6568 default:
6569 gcc_unreachable ();
6572 if (lhs == NULL_TREE)
6574 rtx val2 = expand_simple_binop (mode, ASHIFT, const1_rtx,
6575 val, NULL_RTX, true, OPTAB_DIRECT);
6576 if (code == AND)
6577 val2 = expand_simple_unop (mode, NOT, val2, NULL_RTX, true);
6578 if (expand_atomic_fetch_op (const0_rtx, mem, val2, code, model, false))
6579 return;
6582 rtx target;
6583 if (lhs)
6584 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6585 else
6586 target = gen_reg_rtx (mode);
6587 enum insn_code icode = direct_optab_handler (optab, mode);
6588 gcc_assert (icode != CODE_FOR_nothing);
6589 create_output_operand (&ops[0], target, mode);
6590 create_fixed_operand (&ops[1], mem);
6591 create_convert_operand_to (&ops[2], val, mode, true);
6592 create_integer_operand (&ops[3], model);
6593 create_integer_operand (&ops[4], integer_onep (flag));
6594 if (maybe_expand_insn (icode, 5, ops))
6595 return;
6597 rtx bitval = val;
6598 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6599 val, NULL_RTX, true, OPTAB_DIRECT);
6600 rtx maskval = val;
6601 if (code == AND)
6602 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6603 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
6604 code, model, false);
6605 if (!result)
6607 bool is_atomic = gimple_call_num_args (call) == 5;
6608 tree tcall = gimple_call_arg (call, 3 + is_atomic);
6609 tree fndecl = gimple_call_addr_fndecl (tcall);
6610 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6611 tree exp = build_call_nary (type, tcall, 2 + is_atomic, ptr,
6612 make_tree (type, val),
6613 is_atomic
6614 ? gimple_call_arg (call, 3)
6615 : integer_zero_node);
6616 result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
6617 mode, !lhs);
6619 if (!lhs)
6620 return;
6621 if (integer_onep (flag))
6623 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
6624 NULL_RTX, true, OPTAB_DIRECT);
6625 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
6626 true, OPTAB_DIRECT);
6628 else
6629 result = expand_simple_binop (mode, AND, result, maskval, target, true,
6630 OPTAB_DIRECT);
6631 if (result != target)
6632 emit_move_insn (target, result);
6635 /* Expand IFN_ATOMIC_*_FETCH_CMP_0 internal function. */
6637 void
6638 expand_ifn_atomic_op_fetch_cmp_0 (gcall *call)
6640 tree cmp = gimple_call_arg (call, 0);
6641 tree ptr = gimple_call_arg (call, 1);
6642 tree arg = gimple_call_arg (call, 2);
6643 tree lhs = gimple_call_lhs (call);
6644 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6645 machine_mode mode = TYPE_MODE (TREE_TYPE (cmp));
6646 optab optab;
6647 rtx_code code;
6648 class expand_operand ops[5];
6650 gcc_assert (flag_inline_atomics);
6652 if (gimple_call_num_args (call) == 5)
6653 model = get_memmodel (gimple_call_arg (call, 3));
6655 rtx mem = get_builtin_sync_mem (ptr, mode);
6656 rtx op = expand_expr_force_mode (arg, mode);
6658 switch (gimple_call_internal_fn (call))
6660 case IFN_ATOMIC_ADD_FETCH_CMP_0:
6661 code = PLUS;
6662 optab = atomic_add_fetch_cmp_0_optab;
6663 break;
6664 case IFN_ATOMIC_SUB_FETCH_CMP_0:
6665 code = MINUS;
6666 optab = atomic_sub_fetch_cmp_0_optab;
6667 break;
6668 case IFN_ATOMIC_AND_FETCH_CMP_0:
6669 code = AND;
6670 optab = atomic_and_fetch_cmp_0_optab;
6671 break;
6672 case IFN_ATOMIC_OR_FETCH_CMP_0:
6673 code = IOR;
6674 optab = atomic_or_fetch_cmp_0_optab;
6675 break;
6676 case IFN_ATOMIC_XOR_FETCH_CMP_0:
6677 code = XOR;
6678 optab = atomic_xor_fetch_cmp_0_optab;
6679 break;
6680 default:
6681 gcc_unreachable ();
6684 enum rtx_code comp = UNKNOWN;
6685 switch (tree_to_uhwi (cmp))
6687 case ATOMIC_OP_FETCH_CMP_0_EQ: comp = EQ; break;
6688 case ATOMIC_OP_FETCH_CMP_0_NE: comp = NE; break;
6689 case ATOMIC_OP_FETCH_CMP_0_GT: comp = GT; break;
6690 case ATOMIC_OP_FETCH_CMP_0_GE: comp = GE; break;
6691 case ATOMIC_OP_FETCH_CMP_0_LT: comp = LT; break;
6692 case ATOMIC_OP_FETCH_CMP_0_LE: comp = LE; break;
6693 default: gcc_unreachable ();
6696 rtx target;
6697 if (lhs == NULL_TREE)
6698 target = gen_reg_rtx (TYPE_MODE (boolean_type_node));
6699 else
6700 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6701 enum insn_code icode = direct_optab_handler (optab, mode);
6702 gcc_assert (icode != CODE_FOR_nothing);
6703 create_output_operand (&ops[0], target, TYPE_MODE (boolean_type_node));
6704 create_fixed_operand (&ops[1], mem);
6705 create_convert_operand_to (&ops[2], op, mode, true);
6706 create_integer_operand (&ops[3], model);
6707 create_integer_operand (&ops[4], comp);
6708 if (maybe_expand_insn (icode, 5, ops))
6709 return;
6711 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, op,
6712 code, model, true);
6713 if (!result)
6715 bool is_atomic = gimple_call_num_args (call) == 5;
6716 tree tcall = gimple_call_arg (call, 3 + is_atomic);
6717 tree fndecl = gimple_call_addr_fndecl (tcall);
6718 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6719 tree exp = build_call_nary (type, tcall,
6720 2 + is_atomic, ptr, arg,
6721 is_atomic
6722 ? gimple_call_arg (call, 3)
6723 : integer_zero_node);
6724 result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
6725 mode, !lhs);
6728 if (lhs)
6730 result = emit_store_flag_force (target, comp, result, const0_rtx, mode,
6731 0, 1);
6732 if (result != target)
6733 emit_move_insn (target, result);
6737 /* Expand an atomic clear operation.
6738 void _atomic_clear (BOOL *obj, enum memmodel)
6739 EXP is the call expression. */
6741 static rtx
6742 expand_builtin_atomic_clear (tree exp)
6744 machine_mode mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6745 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6746 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6748 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
6749 model = MEMMODEL_SEQ_CST;
6751 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6752 Failing that, a store is issued by __atomic_store. The only way this can
6753 fail is if the bool type is larger than a word size. Unlikely, but
6754 handle it anyway for completeness. Assume a single threaded model since
6755 there is no atomic support in this case, and no barriers are required. */
6756 rtx ret = expand_atomic_store (mem, const0_rtx, model, true);
6757 if (!ret)
6758 emit_move_insn (mem, const0_rtx);
6759 return const0_rtx;
6762 /* Expand an atomic test_and_set operation.
6763 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6764 EXP is the call expression. */
6766 static rtx
6767 expand_builtin_atomic_test_and_set (tree exp, rtx target)
6769 rtx mem;
6770 enum memmodel model;
6771 machine_mode mode;
6773 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6774 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6775 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6777 return expand_atomic_test_and_set (target, mem, model);
6781 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6782 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6784 static tree
6785 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
6787 int size;
6788 machine_mode mode;
6789 unsigned int mode_align, type_align;
6791 if (TREE_CODE (arg0) != INTEGER_CST)
6792 return NULL_TREE;
6794 /* We need a corresponding integer mode for the access to be lock-free. */
6795 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
6796 if (!int_mode_for_size (size, 0).exists (&mode))
6797 return boolean_false_node;
6799 mode_align = GET_MODE_ALIGNMENT (mode);
6801 if (TREE_CODE (arg1) == INTEGER_CST)
6803 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
6805 /* Either this argument is null, or it's a fake pointer encoding
6806 the alignment of the object. */
6807 val = least_bit_hwi (val);
6808 val *= BITS_PER_UNIT;
6810 if (val == 0 || mode_align < val)
6811 type_align = mode_align;
6812 else
6813 type_align = val;
6815 else
6817 tree ttype = TREE_TYPE (arg1);
6819 /* This function is usually invoked and folded immediately by the front
6820 end before anything else has a chance to look at it. The pointer
6821 parameter at this point is usually cast to a void *, so check for that
6822 and look past the cast. */
6823 if (CONVERT_EXPR_P (arg1)
6824 && POINTER_TYPE_P (ttype)
6825 && VOID_TYPE_P (TREE_TYPE (ttype))
6826 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
6827 arg1 = TREE_OPERAND (arg1, 0);
6829 ttype = TREE_TYPE (arg1);
6830 gcc_assert (POINTER_TYPE_P (ttype));
6832 /* Get the underlying type of the object. */
6833 ttype = TREE_TYPE (ttype);
6834 type_align = TYPE_ALIGN (ttype);
6837 /* If the object has smaller alignment, the lock free routines cannot
6838 be used. */
6839 if (type_align < mode_align)
6840 return boolean_false_node;
6842 /* Check if a compare_and_swap pattern exists for the mode which represents
6843 the required size. The pattern is not allowed to fail, so the existence
6844 of the pattern indicates support is present. Also require that an
6845 atomic load exists for the required size. */
6846 if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
6847 return boolean_true_node;
6848 else
6849 return boolean_false_node;
6852 /* Return true if the parameters to call EXP represent an object which will
6853 always generate lock free instructions. The first argument represents the
6854 size of the object, and the second parameter is a pointer to the object
6855 itself. If NULL is passed for the object, then the result is based on
6856 typical alignment for an object of the specified size. Otherwise return
6857 false. */
6859 static rtx
6860 expand_builtin_atomic_always_lock_free (tree exp)
6862 tree size;
6863 tree arg0 = CALL_EXPR_ARG (exp, 0);
6864 tree arg1 = CALL_EXPR_ARG (exp, 1);
6866 if (TREE_CODE (arg0) != INTEGER_CST)
6868 error ("non-constant argument 1 to %qs", "__atomic_always_lock_free");
6869 return const0_rtx;
6872 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
6873 if (size == boolean_true_node)
6874 return const1_rtx;
6875 return const0_rtx;
6878 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6879 is lock free on this architecture. */
6881 static tree
6882 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
6884 if (!flag_inline_atomics)
6885 return NULL_TREE;
6887 /* If it isn't always lock free, don't generate a result. */
6888 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
6889 return boolean_true_node;
6891 return NULL_TREE;
6894 /* Return true if the parameters to call EXP represent an object which will
6895 always generate lock free instructions. The first argument represents the
6896 size of the object, and the second parameter is a pointer to the object
6897 itself. If NULL is passed for the object, then the result is based on
6898 typical alignment for an object of the specified size. Otherwise return
6899 NULL*/
6901 static rtx
6902 expand_builtin_atomic_is_lock_free (tree exp)
6904 tree size;
6905 tree arg0 = CALL_EXPR_ARG (exp, 0);
6906 tree arg1 = CALL_EXPR_ARG (exp, 1);
6908 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6910 error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
6911 return NULL_RTX;
6914 if (!flag_inline_atomics)
6915 return NULL_RTX;
6917 /* If the value is known at compile time, return the RTX for it. */
6918 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
6919 if (size == boolean_true_node)
6920 return const1_rtx;
6922 return NULL_RTX;
6925 /* Expand the __atomic_thread_fence intrinsic:
6926 void __atomic_thread_fence (enum memmodel)
6927 EXP is the CALL_EXPR. */
6929 static void
6930 expand_builtin_atomic_thread_fence (tree exp)
6932 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6933 expand_mem_thread_fence (model);
6936 /* Expand the __atomic_signal_fence intrinsic:
6937 void __atomic_signal_fence (enum memmodel)
6938 EXP is the CALL_EXPR. */
6940 static void
6941 expand_builtin_atomic_signal_fence (tree exp)
6943 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6944 expand_mem_signal_fence (model);
6947 /* Expand the __sync_synchronize intrinsic. */
6949 static void
6950 expand_builtin_sync_synchronize (void)
6952 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
6955 static rtx
6956 expand_builtin_thread_pointer (tree exp, rtx target)
6958 enum insn_code icode;
6959 if (!validate_arglist (exp, VOID_TYPE))
6960 return const0_rtx;
6961 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
6962 if (icode != CODE_FOR_nothing)
6964 class expand_operand op;
6965 /* If the target is not sutitable then create a new target. */
6966 if (target == NULL_RTX
6967 || !REG_P (target)
6968 || GET_MODE (target) != Pmode)
6969 target = gen_reg_rtx (Pmode);
6970 create_output_operand (&op, target, Pmode);
6971 expand_insn (icode, 1, &op);
6972 return target;
6974 error ("%<__builtin_thread_pointer%> is not supported on this target");
6975 return const0_rtx;
6978 static void
6979 expand_builtin_set_thread_pointer (tree exp)
6981 enum insn_code icode;
6982 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6983 return;
6984 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
6985 if (icode != CODE_FOR_nothing)
6987 class expand_operand op;
6988 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
6989 Pmode, EXPAND_NORMAL);
6990 create_input_operand (&op, val, Pmode);
6991 expand_insn (icode, 1, &op);
6992 return;
6994 error ("%<__builtin_set_thread_pointer%> is not supported on this target");
6998 /* Emit code to restore the current value of stack. */
7000 static void
7001 expand_stack_restore (tree var)
7003 rtx_insn *prev;
7004 rtx sa = expand_normal (var);
7006 sa = convert_memory_address (Pmode, sa);
7008 prev = get_last_insn ();
7009 emit_stack_restore (SAVE_BLOCK, sa);
7011 record_new_stack_level ();
7013 fixup_args_size_notes (prev, get_last_insn (), 0);
7016 /* Emit code to save the current value of stack. */
7018 static rtx
7019 expand_stack_save (void)
7021 rtx ret = NULL_RTX;
7023 emit_stack_save (SAVE_BLOCK, &ret);
7024 return ret;
7027 /* Emit code to get the openacc gang, worker or vector id or size. */
7029 static rtx
7030 expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
7032 const char *name;
7033 rtx fallback_retval;
7034 rtx_insn *(*gen_fn) (rtx, rtx);
7035 switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
7037 case BUILT_IN_GOACC_PARLEVEL_ID:
7038 name = "__builtin_goacc_parlevel_id";
7039 fallback_retval = const0_rtx;
7040 gen_fn = targetm.gen_oacc_dim_pos;
7041 break;
7042 case BUILT_IN_GOACC_PARLEVEL_SIZE:
7043 name = "__builtin_goacc_parlevel_size";
7044 fallback_retval = const1_rtx;
7045 gen_fn = targetm.gen_oacc_dim_size;
7046 break;
7047 default:
7048 gcc_unreachable ();
7051 if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
7053 error ("%qs only supported in OpenACC code", name);
7054 return const0_rtx;
7057 tree arg = CALL_EXPR_ARG (exp, 0);
7058 if (TREE_CODE (arg) != INTEGER_CST)
7060 error ("non-constant argument 0 to %qs", name);
7061 return const0_rtx;
7064 int dim = TREE_INT_CST_LOW (arg);
7065 switch (dim)
7067 case GOMP_DIM_GANG:
7068 case GOMP_DIM_WORKER:
7069 case GOMP_DIM_VECTOR:
7070 break;
7071 default:
7072 error ("illegal argument 0 to %qs", name);
7073 return const0_rtx;
7076 if (ignore)
7077 return target;
7079 if (target == NULL_RTX)
7080 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
7082 if (!targetm.have_oacc_dim_size ())
7084 emit_move_insn (target, fallback_retval);
7085 return target;
7088 rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
7089 emit_insn (gen_fn (reg, GEN_INT (dim)));
7090 if (reg != target)
7091 emit_move_insn (target, reg);
7093 return target;
7096 /* Expand a string compare operation using a sequence of char comparison
7097 to get rid of the calling overhead, with result going to TARGET if
7098 that's convenient.
7100 VAR_STR is the variable string source;
7101 CONST_STR is the constant string source;
7102 LENGTH is the number of chars to compare;
7103 CONST_STR_N indicates which source string is the constant string;
7104 IS_MEMCMP indicates whether it's a memcmp or strcmp.
7106 to: (assume const_str_n is 2, i.e., arg2 is a constant string)
7108 target = (int) (unsigned char) var_str[0]
7109 - (int) (unsigned char) const_str[0];
7110 if (target != 0)
7111 goto ne_label;
7113 target = (int) (unsigned char) var_str[length - 2]
7114 - (int) (unsigned char) const_str[length - 2];
7115 if (target != 0)
7116 goto ne_label;
7117 target = (int) (unsigned char) var_str[length - 1]
7118 - (int) (unsigned char) const_str[length - 1];
7119 ne_label:
7122 static rtx
7123 inline_string_cmp (rtx target, tree var_str, const char *const_str,
7124 unsigned HOST_WIDE_INT length,
7125 int const_str_n, machine_mode mode)
7127 HOST_WIDE_INT offset = 0;
7128 rtx var_rtx_array
7129 = get_memory_rtx (var_str, build_int_cst (unsigned_type_node,length));
7130 rtx var_rtx = NULL_RTX;
7131 rtx const_rtx = NULL_RTX;
7132 rtx result = target ? target : gen_reg_rtx (mode);
7133 rtx_code_label *ne_label = gen_label_rtx ();
7134 tree unit_type_node = unsigned_char_type_node;
7135 scalar_int_mode unit_mode
7136 = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
7138 start_sequence ();
7140 for (unsigned HOST_WIDE_INT i = 0; i < length; i++)
7142 var_rtx
7143 = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset);
7144 const_rtx = c_readstr (const_str + offset, unit_mode);
7145 rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx;
7146 rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
7148 op0 = convert_modes (mode, unit_mode, op0, 1);
7149 op1 = convert_modes (mode, unit_mode, op1, 1);
7150 rtx diff = expand_simple_binop (mode, MINUS, op0, op1,
7151 result, 1, OPTAB_WIDEN);
7153 /* Force the difference into result register. We cannot reassign
7154 result here ("result = diff") or we may end up returning
7155 uninitialized result when expand_simple_binop allocates a new
7156 pseudo-register for returning. */
7157 if (diff != result)
7158 emit_move_insn (result, diff);
7160 if (i < length - 1)
7161 emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
7162 mode, true, ne_label);
7163 offset += GET_MODE_SIZE (unit_mode);
7166 emit_label (ne_label);
7167 rtx_insn *insns = get_insns ();
7168 end_sequence ();
7169 emit_insn (insns);
7171 return result;
7174 /* Inline expansion of a call to str(n)cmp and memcmp, with result going
7175 to TARGET if that's convenient.
7176 If the call is not been inlined, return NULL_RTX. */
7178 static rtx
7179 inline_expand_builtin_bytecmp (tree exp, rtx target)
7181 tree fndecl = get_callee_fndecl (exp);
7182 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7183 bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP);
7185 /* Do NOT apply this inlining expansion when optimizing for size or
7186 optimization level below 2 or if unused *cmp hasn't been DCEd. */
7187 if (optimize < 2 || optimize_insn_for_size_p () || target == const0_rtx)
7188 return NULL_RTX;
7190 gcc_checking_assert (fcode == BUILT_IN_STRCMP
7191 || fcode == BUILT_IN_STRNCMP
7192 || fcode == BUILT_IN_MEMCMP);
7194 /* On a target where the type of the call (int) has same or narrower presicion
7195 than unsigned char, give up the inlining expansion. */
7196 if (TYPE_PRECISION (unsigned_char_type_node)
7197 >= TYPE_PRECISION (TREE_TYPE (exp)))
7198 return NULL_RTX;
7200 tree arg1 = CALL_EXPR_ARG (exp, 0);
7201 tree arg2 = CALL_EXPR_ARG (exp, 1);
7202 tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
7204 unsigned HOST_WIDE_INT len1 = 0;
7205 unsigned HOST_WIDE_INT len2 = 0;
7206 unsigned HOST_WIDE_INT len3 = 0;
7208 /* Get the object representation of the initializers of ARG1 and ARG2
7209 as strings, provided they refer to constant objects, with their byte
7210 sizes in LEN1 and LEN2, respectively. */
7211 const char *bytes1 = getbyterep (arg1, &len1);
7212 const char *bytes2 = getbyterep (arg2, &len2);
7214 /* Fail if neither argument refers to an initialized constant. */
7215 if (!bytes1 && !bytes2)
7216 return NULL_RTX;
7218 if (is_ncmp)
7220 /* Fail if the memcmp/strncmp bound is not a constant. */
7221 if (!tree_fits_uhwi_p (len3_tree))
7222 return NULL_RTX;
7224 len3 = tree_to_uhwi (len3_tree);
7226 if (fcode == BUILT_IN_MEMCMP)
7228 /* Fail if the memcmp bound is greater than the size of either
7229 of the two constant objects. */
7230 if ((bytes1 && len1 < len3)
7231 || (bytes2 && len2 < len3))
7232 return NULL_RTX;
7236 if (fcode != BUILT_IN_MEMCMP)
7238 /* For string functions (i.e., strcmp and strncmp) reduce LEN1
7239 and LEN2 to the length of the nul-terminated string stored
7240 in each. */
7241 if (bytes1 != NULL)
7242 len1 = strnlen (bytes1, len1) + 1;
7243 if (bytes2 != NULL)
7244 len2 = strnlen (bytes2, len2) + 1;
7247 /* See inline_string_cmp. */
7248 int const_str_n;
7249 if (!len1)
7250 const_str_n = 2;
7251 else if (!len2)
7252 const_str_n = 1;
7253 else if (len2 > len1)
7254 const_str_n = 1;
7255 else
7256 const_str_n = 2;
7258 /* For strncmp only, compute the new bound as the smallest of
7259 the lengths of the two strings (plus 1) and the bound provided
7260 to the function. */
7261 unsigned HOST_WIDE_INT bound = (const_str_n == 1) ? len1 : len2;
7262 if (is_ncmp && len3 < bound)
7263 bound = len3;
7265 /* If the bound of the comparison is larger than the threshold,
7266 do nothing. */
7267 if (bound > (unsigned HOST_WIDE_INT) param_builtin_string_cmp_inline_length)
7268 return NULL_RTX;
7270 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
7272 /* Now, start inline expansion the call. */
7273 return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
7274 (const_str_n == 1) ? bytes1 : bytes2, bound,
7275 const_str_n, mode);
7278 /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
7279 represents the size of the first argument to that call, or VOIDmode
7280 if the argument is a pointer. IGNORE will be true if the result
7281 isn't used. */
7282 static rtx
7283 expand_speculation_safe_value (machine_mode mode, tree exp, rtx target,
7284 bool ignore)
7286 rtx val, failsafe;
7287 unsigned nargs = call_expr_nargs (exp);
7289 tree arg0 = CALL_EXPR_ARG (exp, 0);
7291 if (mode == VOIDmode)
7293 mode = TYPE_MODE (TREE_TYPE (arg0));
7294 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
7297 val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
7299 /* An optional second argument can be used as a failsafe value on
7300 some machines. If it isn't present, then the failsafe value is
7301 assumed to be 0. */
7302 if (nargs > 1)
7304 tree arg1 = CALL_EXPR_ARG (exp, 1);
7305 failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL);
7307 else
7308 failsafe = const0_rtx;
7310 /* If the result isn't used, the behavior is undefined. It would be
7311 nice to emit a warning here, but path splitting means this might
7312 happen with legitimate code. So simply drop the builtin
7313 expansion in that case; we've handled any side-effects above. */
7314 if (ignore)
7315 return const0_rtx;
7317 /* If we don't have a suitable target, create one to hold the result. */
7318 if (target == NULL || GET_MODE (target) != mode)
7319 target = gen_reg_rtx (mode);
7321 if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode)
7322 val = convert_modes (mode, VOIDmode, val, false);
7324 return targetm.speculation_safe_value (mode, target, val, failsafe);
7327 /* Expand an expression EXP that calls a built-in function,
7328 with result going to TARGET if that's convenient
7329 (and in mode MODE if that's convenient).
7330 SUBTARGET may be used as the target for computing one of EXP's operands.
7331 IGNORE is nonzero if the value is to be ignored. */
7334 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
7335 int ignore)
7337 tree fndecl = get_callee_fndecl (exp);
7338 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
7339 int flags;
7341 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7342 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7344 /* When ASan is enabled, we don't want to expand some memory/string
7345 builtins and rely on libsanitizer's hooks. This allows us to avoid
7346 redundant checks and be sure, that possible overflow will be detected
7347 by ASan. */
7349 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7350 if (param_asan_kernel_mem_intrinsic_prefix
7351 && sanitize_flags_p (SANITIZE_KERNEL_ADDRESS
7352 | SANITIZE_KERNEL_HWADDRESS))
7353 switch (fcode)
7355 rtx save_decl_rtl, ret;
7356 case BUILT_IN_MEMCPY:
7357 case BUILT_IN_MEMMOVE:
7358 case BUILT_IN_MEMSET:
7359 save_decl_rtl = DECL_RTL (fndecl);
7360 DECL_RTL (fndecl) = asan_memfn_rtl (fndecl);
7361 ret = expand_call (exp, target, ignore);
7362 DECL_RTL (fndecl) = save_decl_rtl;
7363 return ret;
7364 default:
7365 break;
7367 if (sanitize_flags_p (SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
7368 return expand_call (exp, target, ignore);
7370 /* When not optimizing, generate calls to library functions for a certain
7371 set of builtins. */
7372 if (!optimize
7373 && !called_as_built_in (fndecl)
7374 && fcode != BUILT_IN_FORK
7375 && fcode != BUILT_IN_EXECL
7376 && fcode != BUILT_IN_EXECV
7377 && fcode != BUILT_IN_EXECLP
7378 && fcode != BUILT_IN_EXECLE
7379 && fcode != BUILT_IN_EXECVP
7380 && fcode != BUILT_IN_EXECVE
7381 && fcode != BUILT_IN_CLEAR_CACHE
7382 && !ALLOCA_FUNCTION_CODE_P (fcode)
7383 && fcode != BUILT_IN_FREE)
7384 return expand_call (exp, target, ignore);
7386 /* The built-in function expanders test for target == const0_rtx
7387 to determine whether the function's result will be ignored. */
7388 if (ignore)
7389 target = const0_rtx;
7391 /* If the result of a pure or const built-in function is ignored, and
7392 none of its arguments are volatile, we can avoid expanding the
7393 built-in call and just evaluate the arguments for side-effects. */
7394 if (target == const0_rtx
7395 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
7396 && !(flags & ECF_LOOPING_CONST_OR_PURE))
7398 bool volatilep = false;
7399 tree arg;
7400 call_expr_arg_iterator iter;
7402 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7403 if (TREE_THIS_VOLATILE (arg))
7405 volatilep = true;
7406 break;
7409 if (! volatilep)
7411 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7412 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
7413 return const0_rtx;
7417 switch (fcode)
7419 CASE_FLT_FN (BUILT_IN_FABS):
7420 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
7421 case BUILT_IN_FABSD32:
7422 case BUILT_IN_FABSD64:
7423 case BUILT_IN_FABSD128:
7424 target = expand_builtin_fabs (exp, target, subtarget);
7425 if (target)
7426 return target;
7427 break;
7429 CASE_FLT_FN (BUILT_IN_COPYSIGN):
7430 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
7431 target = expand_builtin_copysign (exp, target, subtarget);
7432 if (target)
7433 return target;
7434 break;
7436 /* Just do a normal library call if we were unable to fold
7437 the values. */
7438 CASE_FLT_FN (BUILT_IN_CABS):
7439 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CABS):
7440 break;
7442 CASE_FLT_FN (BUILT_IN_FMA):
7443 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
7444 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
7445 if (target)
7446 return target;
7447 break;
7449 CASE_FLT_FN (BUILT_IN_ILOGB):
7450 if (! flag_unsafe_math_optimizations)
7451 break;
7452 gcc_fallthrough ();
7453 CASE_FLT_FN (BUILT_IN_ISINF):
7454 CASE_FLT_FN (BUILT_IN_FINITE):
7455 case BUILT_IN_ISFINITE:
7456 case BUILT_IN_ISNORMAL:
7457 target = expand_builtin_interclass_mathfn (exp, target);
7458 if (target)
7459 return target;
7460 break;
7462 case BUILT_IN_ISSIGNALING:
7463 target = expand_builtin_issignaling (exp, target);
7464 if (target)
7465 return target;
7466 break;
7468 CASE_FLT_FN (BUILT_IN_ICEIL):
7469 CASE_FLT_FN (BUILT_IN_LCEIL):
7470 CASE_FLT_FN (BUILT_IN_LLCEIL):
7471 CASE_FLT_FN (BUILT_IN_LFLOOR):
7472 CASE_FLT_FN (BUILT_IN_IFLOOR):
7473 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7474 target = expand_builtin_int_roundingfn (exp, target);
7475 if (target)
7476 return target;
7477 break;
7479 CASE_FLT_FN (BUILT_IN_IRINT):
7480 CASE_FLT_FN (BUILT_IN_LRINT):
7481 CASE_FLT_FN (BUILT_IN_LLRINT):
7482 CASE_FLT_FN (BUILT_IN_IROUND):
7483 CASE_FLT_FN (BUILT_IN_LROUND):
7484 CASE_FLT_FN (BUILT_IN_LLROUND):
7485 target = expand_builtin_int_roundingfn_2 (exp, target);
7486 if (target)
7487 return target;
7488 break;
7490 CASE_FLT_FN (BUILT_IN_POWI):
7491 target = expand_builtin_powi (exp, target);
7492 if (target)
7493 return target;
7494 break;
7496 CASE_FLT_FN (BUILT_IN_CEXPI):
7497 target = expand_builtin_cexpi (exp, target);
7498 gcc_assert (target);
7499 return target;
7501 CASE_FLT_FN (BUILT_IN_SIN):
7502 CASE_FLT_FN (BUILT_IN_COS):
7503 if (! flag_unsafe_math_optimizations)
7504 break;
7505 target = expand_builtin_mathfn_3 (exp, target, subtarget);
7506 if (target)
7507 return target;
7508 break;
7510 CASE_FLT_FN (BUILT_IN_SINCOS):
7511 if (! flag_unsafe_math_optimizations)
7512 break;
7513 target = expand_builtin_sincos (exp);
7514 if (target)
7515 return target;
7516 break;
7518 case BUILT_IN_FEGETROUND:
7519 target = expand_builtin_fegetround (exp, target, target_mode);
7520 if (target)
7521 return target;
7522 break;
7524 case BUILT_IN_FECLEAREXCEPT:
7525 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
7526 feclearexcept_optab);
7527 if (target)
7528 return target;
7529 break;
7531 case BUILT_IN_FERAISEEXCEPT:
7532 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
7533 feraiseexcept_optab);
7534 if (target)
7535 return target;
7536 break;
7538 case BUILT_IN_APPLY_ARGS:
7539 return expand_builtin_apply_args ();
7541 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
7542 FUNCTION with a copy of the parameters described by
7543 ARGUMENTS, and ARGSIZE. It returns a block of memory
7544 allocated on the stack into which is stored all the registers
7545 that might possibly be used for returning the result of a
7546 function. ARGUMENTS is the value returned by
7547 __builtin_apply_args. ARGSIZE is the number of bytes of
7548 arguments that must be copied. ??? How should this value be
7549 computed? We'll also need a safe worst case value for varargs
7550 functions. */
7551 case BUILT_IN_APPLY:
7552 if (!validate_arglist (exp, POINTER_TYPE,
7553 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
7554 && !validate_arglist (exp, REFERENCE_TYPE,
7555 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7556 return const0_rtx;
7557 else
7559 rtx ops[3];
7561 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
7562 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
7563 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
7565 return expand_builtin_apply (ops[0], ops[1], ops[2]);
7568 /* __builtin_return (RESULT) causes the function to return the
7569 value described by RESULT. RESULT is address of the block of
7570 memory returned by __builtin_apply. */
7571 case BUILT_IN_RETURN:
7572 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7573 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
7574 return const0_rtx;
7576 case BUILT_IN_SAVEREGS:
7577 return expand_builtin_saveregs ();
7579 case BUILT_IN_VA_ARG_PACK:
7580 /* All valid uses of __builtin_va_arg_pack () are removed during
7581 inlining. */
7582 error ("invalid use of %<__builtin_va_arg_pack ()%>");
7583 return const0_rtx;
7585 case BUILT_IN_VA_ARG_PACK_LEN:
7586 /* All valid uses of __builtin_va_arg_pack_len () are removed during
7587 inlining. */
7588 error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
7589 return const0_rtx;
7591 /* Return the address of the first anonymous stack arg. */
7592 case BUILT_IN_NEXT_ARG:
7593 if (fold_builtin_next_arg (exp, false))
7594 return const0_rtx;
7595 return expand_builtin_next_arg ();
7597 case BUILT_IN_CLEAR_CACHE:
7598 expand_builtin___clear_cache (exp);
7599 return const0_rtx;
7601 case BUILT_IN_CLASSIFY_TYPE:
7602 return expand_builtin_classify_type (exp);
7604 case BUILT_IN_CONSTANT_P:
7605 return const0_rtx;
7607 case BUILT_IN_FRAME_ADDRESS:
7608 case BUILT_IN_RETURN_ADDRESS:
7609 return expand_builtin_frame_address (fndecl, exp);
7611 /* Returns the address of the area where the structure is returned.
7612 0 otherwise. */
7613 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
7614 if (call_expr_nargs (exp) != 0
7615 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
7616 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
7617 return const0_rtx;
7618 else
7619 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
7621 CASE_BUILT_IN_ALLOCA:
7622 target = expand_builtin_alloca (exp);
7623 if (target)
7624 return target;
7625 break;
7627 case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
7628 return expand_asan_emit_allocas_unpoison (exp);
7630 case BUILT_IN_STACK_SAVE:
7631 return expand_stack_save ();
7633 case BUILT_IN_STACK_RESTORE:
7634 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
7635 return const0_rtx;
7637 case BUILT_IN_BSWAP16:
7638 case BUILT_IN_BSWAP32:
7639 case BUILT_IN_BSWAP64:
7640 case BUILT_IN_BSWAP128:
7641 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
7642 if (target)
7643 return target;
7644 break;
7646 CASE_INT_FN (BUILT_IN_FFS):
7647 target = expand_builtin_unop (target_mode, exp, target,
7648 subtarget, ffs_optab);
7649 if (target)
7650 return target;
7651 break;
7653 CASE_INT_FN (BUILT_IN_CLZ):
7654 target = expand_builtin_unop (target_mode, exp, target,
7655 subtarget, clz_optab);
7656 if (target)
7657 return target;
7658 break;
7660 CASE_INT_FN (BUILT_IN_CTZ):
7661 target = expand_builtin_unop (target_mode, exp, target,
7662 subtarget, ctz_optab);
7663 if (target)
7664 return target;
7665 break;
7667 CASE_INT_FN (BUILT_IN_CLRSB):
7668 target = expand_builtin_unop (target_mode, exp, target,
7669 subtarget, clrsb_optab);
7670 if (target)
7671 return target;
7672 break;
7674 CASE_INT_FN (BUILT_IN_POPCOUNT):
7675 target = expand_builtin_unop (target_mode, exp, target,
7676 subtarget, popcount_optab);
7677 if (target)
7678 return target;
7679 break;
7681 CASE_INT_FN (BUILT_IN_PARITY):
7682 target = expand_builtin_unop (target_mode, exp, target,
7683 subtarget, parity_optab);
7684 if (target)
7685 return target;
7686 break;
7688 case BUILT_IN_STRLEN:
7689 target = expand_builtin_strlen (exp, target, target_mode);
7690 if (target)
7691 return target;
7692 break;
7694 case BUILT_IN_STRNLEN:
7695 target = expand_builtin_strnlen (exp, target, target_mode);
7696 if (target)
7697 return target;
7698 break;
7700 case BUILT_IN_STRCPY:
7701 target = expand_builtin_strcpy (exp, target);
7702 if (target)
7703 return target;
7704 break;
7706 case BUILT_IN_STRNCPY:
7707 target = expand_builtin_strncpy (exp, target);
7708 if (target)
7709 return target;
7710 break;
7712 case BUILT_IN_STPCPY:
7713 target = expand_builtin_stpcpy (exp, target, mode);
7714 if (target)
7715 return target;
7716 break;
7718 case BUILT_IN_MEMCPY:
7719 target = expand_builtin_memcpy (exp, target);
7720 if (target)
7721 return target;
7722 break;
7724 case BUILT_IN_MEMMOVE:
7725 target = expand_builtin_memmove (exp, target);
7726 if (target)
7727 return target;
7728 break;
7730 case BUILT_IN_MEMPCPY:
7731 target = expand_builtin_mempcpy (exp, target);
7732 if (target)
7733 return target;
7734 break;
7736 case BUILT_IN_MEMSET:
7737 target = expand_builtin_memset (exp, target, mode);
7738 if (target)
7739 return target;
7740 break;
7742 case BUILT_IN_BZERO:
7743 target = expand_builtin_bzero (exp);
7744 if (target)
7745 return target;
7746 break;
7748 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7749 back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
7750 when changing it to a strcmp call. */
7751 case BUILT_IN_STRCMP_EQ:
7752 target = expand_builtin_memcmp (exp, target, true);
7753 if (target)
7754 return target;
7756 /* Change this call back to a BUILT_IN_STRCMP. */
7757 TREE_OPERAND (exp, 1)
7758 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
7760 /* Delete the last parameter. */
7761 unsigned int i;
7762 vec<tree, va_gc> *arg_vec;
7763 vec_alloc (arg_vec, 2);
7764 for (i = 0; i < 2; i++)
7765 arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
7766 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
7767 /* FALLTHROUGH */
7769 case BUILT_IN_STRCMP:
7770 target = expand_builtin_strcmp (exp, target);
7771 if (target)
7772 return target;
7773 break;
7775 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7776 back to a BUILT_IN_STRNCMP. */
7777 case BUILT_IN_STRNCMP_EQ:
7778 target = expand_builtin_memcmp (exp, target, true);
7779 if (target)
7780 return target;
7782 /* Change it back to a BUILT_IN_STRNCMP. */
7783 TREE_OPERAND (exp, 1)
7784 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
7785 /* FALLTHROUGH */
7787 case BUILT_IN_STRNCMP:
7788 target = expand_builtin_strncmp (exp, target, mode);
7789 if (target)
7790 return target;
7791 break;
7793 case BUILT_IN_BCMP:
7794 case BUILT_IN_MEMCMP:
7795 case BUILT_IN_MEMCMP_EQ:
7796 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
7797 if (target)
7798 return target;
7799 if (fcode == BUILT_IN_MEMCMP_EQ)
7801 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
7802 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
7804 break;
7806 case BUILT_IN_SETJMP:
7807 /* This should have been lowered to the builtins below. */
7808 gcc_unreachable ();
7810 case BUILT_IN_SETJMP_SETUP:
7811 /* __builtin_setjmp_setup is passed a pointer to an array of five words
7812 and the receiver label. */
7813 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7815 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7816 VOIDmode, EXPAND_NORMAL);
7817 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
7818 rtx_insn *label_r = label_rtx (label);
7820 expand_builtin_setjmp_setup (buf_addr, label_r);
7821 return const0_rtx;
7823 break;
7825 case BUILT_IN_SETJMP_RECEIVER:
7826 /* __builtin_setjmp_receiver is passed the receiver label. */
7827 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7829 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
7830 rtx_insn *label_r = label_rtx (label);
7832 expand_builtin_setjmp_receiver (label_r);
7833 nonlocal_goto_handler_labels
7834 = gen_rtx_INSN_LIST (VOIDmode, label_r,
7835 nonlocal_goto_handler_labels);
7836 /* ??? Do not let expand_label treat us as such since we would
7837 not want to be both on the list of non-local labels and on
7838 the list of forced labels. */
7839 FORCED_LABEL (label) = 0;
7840 return const0_rtx;
7842 break;
7844 /* __builtin_longjmp is passed a pointer to an array of five words.
7845 It's similar to the C library longjmp function but works with
7846 __builtin_setjmp above. */
7847 case BUILT_IN_LONGJMP:
7848 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7850 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7851 VOIDmode, EXPAND_NORMAL);
7852 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
7854 if (value != const1_rtx)
7856 error ("%<__builtin_longjmp%> second argument must be 1");
7857 return const0_rtx;
7860 expand_builtin_longjmp (buf_addr, value);
7861 return const0_rtx;
7863 break;
7865 case BUILT_IN_NONLOCAL_GOTO:
7866 target = expand_builtin_nonlocal_goto (exp);
7867 if (target)
7868 return target;
7869 break;
7871 /* This updates the setjmp buffer that is its argument with the value
7872 of the current stack pointer. */
7873 case BUILT_IN_UPDATE_SETJMP_BUF:
7874 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7876 rtx buf_addr
7877 = expand_normal (CALL_EXPR_ARG (exp, 0));
7879 expand_builtin_update_setjmp_buf (buf_addr);
7880 return const0_rtx;
7882 break;
7884 case BUILT_IN_TRAP:
7885 case BUILT_IN_UNREACHABLE_TRAP:
7886 expand_builtin_trap ();
7887 return const0_rtx;
7889 case BUILT_IN_UNREACHABLE:
7890 expand_builtin_unreachable ();
7891 return const0_rtx;
7893 CASE_FLT_FN (BUILT_IN_SIGNBIT):
7894 case BUILT_IN_SIGNBITD32:
7895 case BUILT_IN_SIGNBITD64:
7896 case BUILT_IN_SIGNBITD128:
7897 target = expand_builtin_signbit (exp, target);
7898 if (target)
7899 return target;
7900 break;
7902 /* Various hooks for the DWARF 2 __throw routine. */
7903 case BUILT_IN_UNWIND_INIT:
7904 expand_builtin_unwind_init ();
7905 return const0_rtx;
7906 case BUILT_IN_DWARF_CFA:
7907 return virtual_cfa_rtx;
7908 #ifdef DWARF2_UNWIND_INFO
7909 case BUILT_IN_DWARF_SP_COLUMN:
7910 return expand_builtin_dwarf_sp_column ();
7911 case BUILT_IN_INIT_DWARF_REG_SIZES:
7912 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
7913 return const0_rtx;
7914 #endif
7915 case BUILT_IN_FROB_RETURN_ADDR:
7916 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
7917 case BUILT_IN_EXTRACT_RETURN_ADDR:
7918 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
7919 case BUILT_IN_EH_RETURN:
7920 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
7921 CALL_EXPR_ARG (exp, 1));
7922 return const0_rtx;
7923 case BUILT_IN_EH_RETURN_DATA_REGNO:
7924 return expand_builtin_eh_return_data_regno (exp);
7925 case BUILT_IN_EXTEND_POINTER:
7926 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
7927 case BUILT_IN_EH_POINTER:
7928 return expand_builtin_eh_pointer (exp);
7929 case BUILT_IN_EH_FILTER:
7930 return expand_builtin_eh_filter (exp);
7931 case BUILT_IN_EH_COPY_VALUES:
7932 return expand_builtin_eh_copy_values (exp);
7934 case BUILT_IN_VA_START:
7935 return expand_builtin_va_start (exp);
7936 case BUILT_IN_VA_END:
7937 return expand_builtin_va_end (exp);
7938 case BUILT_IN_VA_COPY:
7939 return expand_builtin_va_copy (exp);
7940 case BUILT_IN_EXPECT:
7941 return expand_builtin_expect (exp, target);
7942 case BUILT_IN_EXPECT_WITH_PROBABILITY:
7943 return expand_builtin_expect_with_probability (exp, target);
7944 case BUILT_IN_ASSUME_ALIGNED:
7945 return expand_builtin_assume_aligned (exp, target);
7946 case BUILT_IN_PREFETCH:
7947 expand_builtin_prefetch (exp);
7948 return const0_rtx;
7950 case BUILT_IN_INIT_TRAMPOLINE:
7951 return expand_builtin_init_trampoline (exp, true);
7952 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
7953 return expand_builtin_init_trampoline (exp, false);
7954 case BUILT_IN_ADJUST_TRAMPOLINE:
7955 return expand_builtin_adjust_trampoline (exp);
7957 case BUILT_IN_INIT_DESCRIPTOR:
7958 return expand_builtin_init_descriptor (exp);
7959 case BUILT_IN_ADJUST_DESCRIPTOR:
7960 return expand_builtin_adjust_descriptor (exp);
7962 case BUILT_IN_FORK:
7963 case BUILT_IN_EXECL:
7964 case BUILT_IN_EXECV:
7965 case BUILT_IN_EXECLP:
7966 case BUILT_IN_EXECLE:
7967 case BUILT_IN_EXECVP:
7968 case BUILT_IN_EXECVE:
7969 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
7970 if (target)
7971 return target;
7972 break;
7974 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
7975 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
7976 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
7977 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
7978 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
7979 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
7980 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
7981 if (target)
7982 return target;
7983 break;
7985 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
7986 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
7987 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
7988 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
7989 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
7990 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
7991 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
7992 if (target)
7993 return target;
7994 break;
7996 case BUILT_IN_SYNC_FETCH_AND_OR_1:
7997 case BUILT_IN_SYNC_FETCH_AND_OR_2:
7998 case BUILT_IN_SYNC_FETCH_AND_OR_4:
7999 case BUILT_IN_SYNC_FETCH_AND_OR_8:
8000 case BUILT_IN_SYNC_FETCH_AND_OR_16:
8001 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
8002 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
8003 if (target)
8004 return target;
8005 break;
8007 case BUILT_IN_SYNC_FETCH_AND_AND_1:
8008 case BUILT_IN_SYNC_FETCH_AND_AND_2:
8009 case BUILT_IN_SYNC_FETCH_AND_AND_4:
8010 case BUILT_IN_SYNC_FETCH_AND_AND_8:
8011 case BUILT_IN_SYNC_FETCH_AND_AND_16:
8012 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
8013 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
8014 if (target)
8015 return target;
8016 break;
8018 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
8019 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
8020 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
8021 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
8022 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
8023 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
8024 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
8025 if (target)
8026 return target;
8027 break;
8029 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
8030 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
8031 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
8032 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
8033 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
8034 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
8035 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
8036 if (target)
8037 return target;
8038 break;
8040 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
8041 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
8042 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
8043 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
8044 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
8045 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
8046 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
8047 if (target)
8048 return target;
8049 break;
8051 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
8052 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
8053 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
8054 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
8055 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
8056 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
8057 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
8058 if (target)
8059 return target;
8060 break;
8062 case BUILT_IN_SYNC_OR_AND_FETCH_1:
8063 case BUILT_IN_SYNC_OR_AND_FETCH_2:
8064 case BUILT_IN_SYNC_OR_AND_FETCH_4:
8065 case BUILT_IN_SYNC_OR_AND_FETCH_8:
8066 case BUILT_IN_SYNC_OR_AND_FETCH_16:
8067 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
8068 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
8069 if (target)
8070 return target;
8071 break;
8073 case BUILT_IN_SYNC_AND_AND_FETCH_1:
8074 case BUILT_IN_SYNC_AND_AND_FETCH_2:
8075 case BUILT_IN_SYNC_AND_AND_FETCH_4:
8076 case BUILT_IN_SYNC_AND_AND_FETCH_8:
8077 case BUILT_IN_SYNC_AND_AND_FETCH_16:
8078 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
8079 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
8080 if (target)
8081 return target;
8082 break;
8084 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
8085 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
8086 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
8087 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
8088 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
8089 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
8090 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
8091 if (target)
8092 return target;
8093 break;
8095 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
8096 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
8097 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
8098 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
8099 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
8100 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
8101 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
8102 if (target)
8103 return target;
8104 break;
8106 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
8107 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
8108 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
8109 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
8110 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
8111 if (mode == VOIDmode)
8112 mode = TYPE_MODE (boolean_type_node);
8113 if (!target || !register_operand (target, mode))
8114 target = gen_reg_rtx (mode);
8116 mode = get_builtin_sync_mode
8117 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
8118 target = expand_builtin_compare_and_swap (mode, exp, true, target);
8119 if (target)
8120 return target;
8121 break;
8123 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
8124 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
8125 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
8126 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
8127 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
8128 mode = get_builtin_sync_mode
8129 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
8130 target = expand_builtin_compare_and_swap (mode, exp, false, target);
8131 if (target)
8132 return target;
8133 break;
8135 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
8136 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
8137 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
8138 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
8139 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
8140 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
8141 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
8142 if (target)
8143 return target;
8144 break;
8146 case BUILT_IN_SYNC_LOCK_RELEASE_1:
8147 case BUILT_IN_SYNC_LOCK_RELEASE_2:
8148 case BUILT_IN_SYNC_LOCK_RELEASE_4:
8149 case BUILT_IN_SYNC_LOCK_RELEASE_8:
8150 case BUILT_IN_SYNC_LOCK_RELEASE_16:
8151 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
8152 expand_builtin_sync_lock_release (mode, exp);
8153 return const0_rtx;
8155 case BUILT_IN_SYNC_SYNCHRONIZE:
8156 expand_builtin_sync_synchronize ();
8157 return const0_rtx;
8159 case BUILT_IN_ATOMIC_EXCHANGE_1:
8160 case BUILT_IN_ATOMIC_EXCHANGE_2:
8161 case BUILT_IN_ATOMIC_EXCHANGE_4:
8162 case BUILT_IN_ATOMIC_EXCHANGE_8:
8163 case BUILT_IN_ATOMIC_EXCHANGE_16:
8164 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
8165 target = expand_builtin_atomic_exchange (mode, exp, target);
8166 if (target)
8167 return target;
8168 break;
8170 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
8171 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
8172 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
8173 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
8174 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
8176 unsigned int nargs, z;
8177 vec<tree, va_gc> *vec;
8179 mode =
8180 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
8181 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
8182 if (target)
8183 return target;
8185 /* If this is turned into an external library call, the weak parameter
8186 must be dropped to match the expected parameter list. */
8187 nargs = call_expr_nargs (exp);
8188 vec_alloc (vec, nargs - 1);
8189 for (z = 0; z < 3; z++)
8190 vec->quick_push (CALL_EXPR_ARG (exp, z));
8191 /* Skip the boolean weak parameter. */
8192 for (z = 4; z < 6; z++)
8193 vec->quick_push (CALL_EXPR_ARG (exp, z));
8194 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
8195 break;
8198 case BUILT_IN_ATOMIC_LOAD_1:
8199 case BUILT_IN_ATOMIC_LOAD_2:
8200 case BUILT_IN_ATOMIC_LOAD_4:
8201 case BUILT_IN_ATOMIC_LOAD_8:
8202 case BUILT_IN_ATOMIC_LOAD_16:
8203 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
8204 target = expand_builtin_atomic_load (mode, exp, target);
8205 if (target)
8206 return target;
8207 break;
8209 case BUILT_IN_ATOMIC_STORE_1:
8210 case BUILT_IN_ATOMIC_STORE_2:
8211 case BUILT_IN_ATOMIC_STORE_4:
8212 case BUILT_IN_ATOMIC_STORE_8:
8213 case BUILT_IN_ATOMIC_STORE_16:
8214 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
8215 target = expand_builtin_atomic_store (mode, exp);
8216 if (target)
8217 return const0_rtx;
8218 break;
8220 case BUILT_IN_ATOMIC_ADD_FETCH_1:
8221 case BUILT_IN_ATOMIC_ADD_FETCH_2:
8222 case BUILT_IN_ATOMIC_ADD_FETCH_4:
8223 case BUILT_IN_ATOMIC_ADD_FETCH_8:
8224 case BUILT_IN_ATOMIC_ADD_FETCH_16:
8226 enum built_in_function lib;
8227 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
8228 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
8229 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
8230 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
8231 ignore, lib);
8232 if (target)
8233 return target;
8234 break;
8236 case BUILT_IN_ATOMIC_SUB_FETCH_1:
8237 case BUILT_IN_ATOMIC_SUB_FETCH_2:
8238 case BUILT_IN_ATOMIC_SUB_FETCH_4:
8239 case BUILT_IN_ATOMIC_SUB_FETCH_8:
8240 case BUILT_IN_ATOMIC_SUB_FETCH_16:
8242 enum built_in_function lib;
8243 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
8244 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
8245 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
8246 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
8247 ignore, lib);
8248 if (target)
8249 return target;
8250 break;
8252 case BUILT_IN_ATOMIC_AND_FETCH_1:
8253 case BUILT_IN_ATOMIC_AND_FETCH_2:
8254 case BUILT_IN_ATOMIC_AND_FETCH_4:
8255 case BUILT_IN_ATOMIC_AND_FETCH_8:
8256 case BUILT_IN_ATOMIC_AND_FETCH_16:
8258 enum built_in_function lib;
8259 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
8260 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
8261 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
8262 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
8263 ignore, lib);
8264 if (target)
8265 return target;
8266 break;
8268 case BUILT_IN_ATOMIC_NAND_FETCH_1:
8269 case BUILT_IN_ATOMIC_NAND_FETCH_2:
8270 case BUILT_IN_ATOMIC_NAND_FETCH_4:
8271 case BUILT_IN_ATOMIC_NAND_FETCH_8:
8272 case BUILT_IN_ATOMIC_NAND_FETCH_16:
8274 enum built_in_function lib;
8275 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
8276 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
8277 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
8278 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
8279 ignore, lib);
8280 if (target)
8281 return target;
8282 break;
8284 case BUILT_IN_ATOMIC_XOR_FETCH_1:
8285 case BUILT_IN_ATOMIC_XOR_FETCH_2:
8286 case BUILT_IN_ATOMIC_XOR_FETCH_4:
8287 case BUILT_IN_ATOMIC_XOR_FETCH_8:
8288 case BUILT_IN_ATOMIC_XOR_FETCH_16:
8290 enum built_in_function lib;
8291 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
8292 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
8293 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
8294 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
8295 ignore, lib);
8296 if (target)
8297 return target;
8298 break;
8300 case BUILT_IN_ATOMIC_OR_FETCH_1:
8301 case BUILT_IN_ATOMIC_OR_FETCH_2:
8302 case BUILT_IN_ATOMIC_OR_FETCH_4:
8303 case BUILT_IN_ATOMIC_OR_FETCH_8:
8304 case BUILT_IN_ATOMIC_OR_FETCH_16:
8306 enum built_in_function lib;
8307 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
8308 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
8309 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
8310 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
8311 ignore, lib);
8312 if (target)
8313 return target;
8314 break;
8316 case BUILT_IN_ATOMIC_FETCH_ADD_1:
8317 case BUILT_IN_ATOMIC_FETCH_ADD_2:
8318 case BUILT_IN_ATOMIC_FETCH_ADD_4:
8319 case BUILT_IN_ATOMIC_FETCH_ADD_8:
8320 case BUILT_IN_ATOMIC_FETCH_ADD_16:
8321 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
8322 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
8323 ignore, BUILT_IN_NONE);
8324 if (target)
8325 return target;
8326 break;
8328 case BUILT_IN_ATOMIC_FETCH_SUB_1:
8329 case BUILT_IN_ATOMIC_FETCH_SUB_2:
8330 case BUILT_IN_ATOMIC_FETCH_SUB_4:
8331 case BUILT_IN_ATOMIC_FETCH_SUB_8:
8332 case BUILT_IN_ATOMIC_FETCH_SUB_16:
8333 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
8334 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
8335 ignore, BUILT_IN_NONE);
8336 if (target)
8337 return target;
8338 break;
8340 case BUILT_IN_ATOMIC_FETCH_AND_1:
8341 case BUILT_IN_ATOMIC_FETCH_AND_2:
8342 case BUILT_IN_ATOMIC_FETCH_AND_4:
8343 case BUILT_IN_ATOMIC_FETCH_AND_8:
8344 case BUILT_IN_ATOMIC_FETCH_AND_16:
8345 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
8346 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
8347 ignore, BUILT_IN_NONE);
8348 if (target)
8349 return target;
8350 break;
8352 case BUILT_IN_ATOMIC_FETCH_NAND_1:
8353 case BUILT_IN_ATOMIC_FETCH_NAND_2:
8354 case BUILT_IN_ATOMIC_FETCH_NAND_4:
8355 case BUILT_IN_ATOMIC_FETCH_NAND_8:
8356 case BUILT_IN_ATOMIC_FETCH_NAND_16:
8357 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
8358 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
8359 ignore, BUILT_IN_NONE);
8360 if (target)
8361 return target;
8362 break;
8364 case BUILT_IN_ATOMIC_FETCH_XOR_1:
8365 case BUILT_IN_ATOMIC_FETCH_XOR_2:
8366 case BUILT_IN_ATOMIC_FETCH_XOR_4:
8367 case BUILT_IN_ATOMIC_FETCH_XOR_8:
8368 case BUILT_IN_ATOMIC_FETCH_XOR_16:
8369 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
8370 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
8371 ignore, BUILT_IN_NONE);
8372 if (target)
8373 return target;
8374 break;
8376 case BUILT_IN_ATOMIC_FETCH_OR_1:
8377 case BUILT_IN_ATOMIC_FETCH_OR_2:
8378 case BUILT_IN_ATOMIC_FETCH_OR_4:
8379 case BUILT_IN_ATOMIC_FETCH_OR_8:
8380 case BUILT_IN_ATOMIC_FETCH_OR_16:
8381 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
8382 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
8383 ignore, BUILT_IN_NONE);
8384 if (target)
8385 return target;
8386 break;
8388 case BUILT_IN_ATOMIC_TEST_AND_SET:
8389 return expand_builtin_atomic_test_and_set (exp, target);
8391 case BUILT_IN_ATOMIC_CLEAR:
8392 return expand_builtin_atomic_clear (exp);
8394 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8395 return expand_builtin_atomic_always_lock_free (exp);
8397 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8398 target = expand_builtin_atomic_is_lock_free (exp);
8399 if (target)
8400 return target;
8401 break;
8403 case BUILT_IN_ATOMIC_THREAD_FENCE:
8404 expand_builtin_atomic_thread_fence (exp);
8405 return const0_rtx;
8407 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
8408 expand_builtin_atomic_signal_fence (exp);
8409 return const0_rtx;
8411 case BUILT_IN_OBJECT_SIZE:
8412 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
8413 return expand_builtin_object_size (exp);
8415 case BUILT_IN_MEMCPY_CHK:
8416 case BUILT_IN_MEMPCPY_CHK:
8417 case BUILT_IN_MEMMOVE_CHK:
8418 case BUILT_IN_MEMSET_CHK:
8419 target = expand_builtin_memory_chk (exp, target, mode, fcode);
8420 if (target)
8421 return target;
8422 break;
8424 case BUILT_IN_STRCPY_CHK:
8425 case BUILT_IN_STPCPY_CHK:
8426 case BUILT_IN_STRNCPY_CHK:
8427 case BUILT_IN_STPNCPY_CHK:
8428 case BUILT_IN_STRCAT_CHK:
8429 case BUILT_IN_STRNCAT_CHK:
8430 case BUILT_IN_SNPRINTF_CHK:
8431 case BUILT_IN_VSNPRINTF_CHK:
8432 maybe_emit_chk_warning (exp, fcode);
8433 break;
8435 case BUILT_IN_SPRINTF_CHK:
8436 case BUILT_IN_VSPRINTF_CHK:
8437 maybe_emit_sprintf_chk_warning (exp, fcode);
8438 break;
8440 case BUILT_IN_THREAD_POINTER:
8441 return expand_builtin_thread_pointer (exp, target);
8443 case BUILT_IN_SET_THREAD_POINTER:
8444 expand_builtin_set_thread_pointer (exp);
8445 return const0_rtx;
8447 case BUILT_IN_ACC_ON_DEVICE:
8448 /* Do library call, if we failed to expand the builtin when
8449 folding. */
8450 break;
8452 case BUILT_IN_GOACC_PARLEVEL_ID:
8453 case BUILT_IN_GOACC_PARLEVEL_SIZE:
8454 return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
8456 case BUILT_IN_SPECULATION_SAFE_VALUE_PTR:
8457 return expand_speculation_safe_value (VOIDmode, exp, target, ignore);
8459 case BUILT_IN_SPECULATION_SAFE_VALUE_1:
8460 case BUILT_IN_SPECULATION_SAFE_VALUE_2:
8461 case BUILT_IN_SPECULATION_SAFE_VALUE_4:
8462 case BUILT_IN_SPECULATION_SAFE_VALUE_8:
8463 case BUILT_IN_SPECULATION_SAFE_VALUE_16:
8464 mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1);
8465 return expand_speculation_safe_value (mode, exp, target, ignore);
8467 default: /* just do library call, if unknown builtin */
8468 break;
8471 /* The switch statement above can drop through to cause the function
8472 to be called normally. */
8473 return expand_call (exp, target, ignore);
8476 /* Determine whether a tree node represents a call to a built-in
8477 function. If the tree T is a call to a built-in function with
8478 the right number of arguments of the appropriate types, return
8479 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
8480 Otherwise the return value is END_BUILTINS. */
8482 enum built_in_function
8483 builtin_mathfn_code (const_tree t)
8485 const_tree fndecl, arg, parmlist;
8486 const_tree argtype, parmtype;
8487 const_call_expr_arg_iterator iter;
8489 if (TREE_CODE (t) != CALL_EXPR)
8490 return END_BUILTINS;
8492 fndecl = get_callee_fndecl (t);
8493 if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
8494 return END_BUILTINS;
8496 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
8497 init_const_call_expr_arg_iterator (t, &iter);
8498 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
8500 /* If a function doesn't take a variable number of arguments,
8501 the last element in the list will have type `void'. */
8502 parmtype = TREE_VALUE (parmlist);
8503 if (VOID_TYPE_P (parmtype))
8505 if (more_const_call_expr_args_p (&iter))
8506 return END_BUILTINS;
8507 return DECL_FUNCTION_CODE (fndecl);
8510 if (! more_const_call_expr_args_p (&iter))
8511 return END_BUILTINS;
8513 arg = next_const_call_expr_arg (&iter);
8514 argtype = TREE_TYPE (arg);
8516 if (SCALAR_FLOAT_TYPE_P (parmtype))
8518 if (! SCALAR_FLOAT_TYPE_P (argtype))
8519 return END_BUILTINS;
8521 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
8523 if (! COMPLEX_FLOAT_TYPE_P (argtype))
8524 return END_BUILTINS;
8526 else if (POINTER_TYPE_P (parmtype))
8528 if (! POINTER_TYPE_P (argtype))
8529 return END_BUILTINS;
8531 else if (INTEGRAL_TYPE_P (parmtype))
8533 if (! INTEGRAL_TYPE_P (argtype))
8534 return END_BUILTINS;
8536 else
8537 return END_BUILTINS;
8540 /* Variable-length argument list. */
8541 return DECL_FUNCTION_CODE (fndecl);
8544 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
8545 evaluate to a constant. */
8547 static tree
8548 fold_builtin_constant_p (tree arg)
8550 /* We return 1 for a numeric type that's known to be a constant
8551 value at compile-time or for an aggregate type that's a
8552 literal constant. */
8553 STRIP_NOPS (arg);
8555 /* If we know this is a constant, emit the constant of one. */
8556 if (CONSTANT_CLASS_P (arg)
8557 || (TREE_CODE (arg) == CONSTRUCTOR
8558 && TREE_CONSTANT (arg)))
8559 return integer_one_node;
8560 if (TREE_CODE (arg) == ADDR_EXPR)
8562 tree op = TREE_OPERAND (arg, 0);
8563 if (TREE_CODE (op) == STRING_CST
8564 || (TREE_CODE (op) == ARRAY_REF
8565 && integer_zerop (TREE_OPERAND (op, 1))
8566 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
8567 return integer_one_node;
8570 /* If this expression has side effects, show we don't know it to be a
8571 constant. Likewise if it's a pointer or aggregate type since in
8572 those case we only want literals, since those are only optimized
8573 when generating RTL, not later.
8574 And finally, if we are compiling an initializer, not code, we
8575 need to return a definite result now; there's not going to be any
8576 more optimization done. */
8577 if (TREE_SIDE_EFFECTS (arg)
8578 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
8579 || POINTER_TYPE_P (TREE_TYPE (arg))
8580 || cfun == 0
8581 || folding_initializer
8582 || force_folding_builtin_constant_p)
8583 return integer_zero_node;
8585 return NULL_TREE;
8588 /* Create builtin_expect or builtin_expect_with_probability
8589 with PRED and EXPECTED as its arguments and return it as a truthvalue.
8590 Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
8591 builtin_expect_with_probability instead uses third argument as PROBABILITY
8592 value. */
8594 static tree
8595 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
8596 tree predictor, tree probability)
8598 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
8600 fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT
8601 : BUILT_IN_EXPECT_WITH_PROBABILITY);
8602 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
8603 ret_type = TREE_TYPE (TREE_TYPE (fn));
8604 pred_type = TREE_VALUE (arg_types);
8605 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
8607 pred = fold_convert_loc (loc, pred_type, pred);
8608 expected = fold_convert_loc (loc, expected_type, expected);
8610 if (probability)
8611 call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability);
8612 else
8613 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
8614 predictor);
8616 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
8617 build_int_cst (ret_type, 0));
8620 /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
8621 NULL_TREE if no simplification is possible. */
8623 tree
8624 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2,
8625 tree arg3)
8627 tree inner, fndecl, inner_arg0;
8628 enum tree_code code;
8630 /* Distribute the expected value over short-circuiting operators.
8631 See through the cast from truthvalue_type_node to long. */
8632 inner_arg0 = arg0;
8633 while (CONVERT_EXPR_P (inner_arg0)
8634 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
8635 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
8636 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
8638 /* If this is a builtin_expect within a builtin_expect keep the
8639 inner one. See through a comparison against a constant. It
8640 might have been added to create a thruthvalue. */
8641 inner = inner_arg0;
8643 if (COMPARISON_CLASS_P (inner)
8644 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
8645 inner = TREE_OPERAND (inner, 0);
8647 if (TREE_CODE (inner) == CALL_EXPR
8648 && (fndecl = get_callee_fndecl (inner))
8649 && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT,
8650 BUILT_IN_EXPECT_WITH_PROBABILITY))
8651 return arg0;
8653 inner = inner_arg0;
8654 code = TREE_CODE (inner);
8655 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
8657 tree op0 = TREE_OPERAND (inner, 0);
8658 tree op1 = TREE_OPERAND (inner, 1);
8659 arg1 = save_expr (arg1);
8661 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3);
8662 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3);
8663 inner = build2 (code, TREE_TYPE (inner), op0, op1);
8665 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
8668 /* If the argument isn't invariant then there's nothing else we can do. */
8669 if (!TREE_CONSTANT (inner_arg0))
8670 return NULL_TREE;
8672 /* If we expect that a comparison against the argument will fold to
8673 a constant return the constant. In practice, this means a true
8674 constant or the address of a non-weak symbol. */
8675 inner = inner_arg0;
8676 STRIP_NOPS (inner);
8677 if (TREE_CODE (inner) == ADDR_EXPR)
8681 inner = TREE_OPERAND (inner, 0);
8683 while (TREE_CODE (inner) == COMPONENT_REF
8684 || TREE_CODE (inner) == ARRAY_REF);
8685 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
8686 return NULL_TREE;
8689 /* Otherwise, ARG0 already has the proper type for the return value. */
8690 return arg0;
8693 /* Fold a call to __builtin_classify_type with argument ARG. */
8695 static tree
8696 fold_builtin_classify_type (tree arg)
8698 if (arg == 0)
8699 return build_int_cst (integer_type_node, no_type_class);
8701 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
8704 /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
8705 ARG. */
8707 static tree
8708 fold_builtin_strlen (location_t loc, tree expr, tree type, tree arg)
8710 if (!validate_arg (arg, POINTER_TYPE))
8711 return NULL_TREE;
8712 else
8714 c_strlen_data lendata = { };
8715 tree len = c_strlen (arg, 0, &lendata);
8717 if (len)
8718 return fold_convert_loc (loc, type, len);
8720 /* TODO: Move this to gimple-ssa-warn-access once the pass runs
8721 also early enough to detect invalid reads in multimensional
8722 arrays and struct members. */
8723 if (!lendata.decl)
8724 c_strlen (arg, 1, &lendata);
8726 if (lendata.decl)
8728 if (EXPR_HAS_LOCATION (arg))
8729 loc = EXPR_LOCATION (arg);
8730 else if (loc == UNKNOWN_LOCATION)
8731 loc = input_location;
8732 warn_string_no_nul (loc, expr, "strlen", arg, lendata.decl);
8735 return NULL_TREE;
8739 /* Fold a call to __builtin_inf or __builtin_huge_val. */
8741 static tree
8742 fold_builtin_inf (location_t loc, tree type, int warn)
8744 /* __builtin_inff is intended to be usable to define INFINITY on all
8745 targets. If an infinity is not available, INFINITY expands "to a
8746 positive constant of type float that overflows at translation
8747 time", footnote "In this case, using INFINITY will violate the
8748 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
8749 Thus we pedwarn to ensure this constraint violation is
8750 diagnosed. */
8751 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
8752 pedwarn (loc, 0, "target format does not support infinity");
8754 return build_real (type, dconstinf);
8757 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8758 NULL_TREE if no simplification can be made. */
8760 static tree
8761 fold_builtin_sincos (location_t loc,
8762 tree arg0, tree arg1, tree arg2)
8764 tree type;
8765 tree fndecl, call = NULL_TREE;
8767 if (!validate_arg (arg0, REAL_TYPE)
8768 || !validate_arg (arg1, POINTER_TYPE)
8769 || !validate_arg (arg2, POINTER_TYPE))
8770 return NULL_TREE;
8772 type = TREE_TYPE (arg0);
8774 /* Calculate the result when the argument is a constant. */
8775 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
8776 if (fn == END_BUILTINS)
8777 return NULL_TREE;
8779 /* Canonicalize sincos to cexpi. */
8780 if (TREE_CODE (arg0) == REAL_CST)
8782 tree complex_type = build_complex_type (type);
8783 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
8785 if (!call)
8787 if (!targetm.libc_has_function (function_c99_math_complex, type)
8788 || !builtin_decl_implicit_p (fn))
8789 return NULL_TREE;
8790 fndecl = builtin_decl_explicit (fn);
8791 call = build_call_expr_loc (loc, fndecl, 1, arg0);
8792 call = builtin_save_expr (call);
8795 tree ptype = build_pointer_type (type);
8796 arg1 = fold_convert (ptype, arg1);
8797 arg2 = fold_convert (ptype, arg2);
8798 return build2 (COMPOUND_EXPR, void_type_node,
8799 build2 (MODIFY_EXPR, void_type_node,
8800 build_fold_indirect_ref_loc (loc, arg1),
8801 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
8802 build2 (MODIFY_EXPR, void_type_node,
8803 build_fold_indirect_ref_loc (loc, arg2),
8804 fold_build1_loc (loc, REALPART_EXPR, type, call)));
8807 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8808 Return NULL_TREE if no simplification can be made. */
8810 static tree
8811 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8813 if (!validate_arg (arg1, POINTER_TYPE)
8814 || !validate_arg (arg2, POINTER_TYPE)
8815 || !validate_arg (len, INTEGER_TYPE))
8816 return NULL_TREE;
8818 /* If the LEN parameter is zero, return zero. */
8819 if (integer_zerop (len))
8820 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8821 arg1, arg2);
8823 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8824 if (operand_equal_p (arg1, arg2, 0))
8825 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8827 /* If len parameter is one, return an expression corresponding to
8828 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8829 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8831 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8832 tree cst_uchar_ptr_node
8833 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8835 tree ind1
8836 = fold_convert_loc (loc, integer_type_node,
8837 build1 (INDIRECT_REF, cst_uchar_node,
8838 fold_convert_loc (loc,
8839 cst_uchar_ptr_node,
8840 arg1)));
8841 tree ind2
8842 = fold_convert_loc (loc, integer_type_node,
8843 build1 (INDIRECT_REF, cst_uchar_node,
8844 fold_convert_loc (loc,
8845 cst_uchar_ptr_node,
8846 arg2)));
8847 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8850 return NULL_TREE;
8853 /* Fold a call to builtin isascii with argument ARG. */
8855 static tree
8856 fold_builtin_isascii (location_t loc, tree arg)
8858 if (!validate_arg (arg, INTEGER_TYPE))
8859 return NULL_TREE;
8860 else
8862 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8863 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8864 build_int_cst (integer_type_node,
8865 ~ (unsigned HOST_WIDE_INT) 0x7f));
8866 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8867 arg, integer_zero_node);
8871 /* Fold a call to builtin toascii with argument ARG. */
8873 static tree
8874 fold_builtin_toascii (location_t loc, tree arg)
8876 if (!validate_arg (arg, INTEGER_TYPE))
8877 return NULL_TREE;
8879 /* Transform toascii(c) -> (c & 0x7f). */
8880 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8881 build_int_cst (integer_type_node, 0x7f));
8884 /* Fold a call to builtin isdigit with argument ARG. */
8886 static tree
8887 fold_builtin_isdigit (location_t loc, tree arg)
8889 if (!validate_arg (arg, INTEGER_TYPE))
8890 return NULL_TREE;
8891 else
8893 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8894 /* According to the C standard, isdigit is unaffected by locale.
8895 However, it definitely is affected by the target character set. */
8896 unsigned HOST_WIDE_INT target_digit0
8897 = lang_hooks.to_target_charset ('0');
8899 if (target_digit0 == 0)
8900 return NULL_TREE;
8902 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8903 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8904 build_int_cst (unsigned_type_node, target_digit0));
8905 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8906 build_int_cst (unsigned_type_node, 9));
8910 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8912 static tree
8913 fold_builtin_fabs (location_t loc, tree arg, tree type)
8915 if (!validate_arg (arg, REAL_TYPE))
8916 return NULL_TREE;
8918 arg = fold_convert_loc (loc, type, arg);
8919 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8922 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8924 static tree
8925 fold_builtin_abs (location_t loc, tree arg, tree type)
8927 if (!validate_arg (arg, INTEGER_TYPE))
8928 return NULL_TREE;
8930 arg = fold_convert_loc (loc, type, arg);
8931 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8934 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8936 static tree
8937 fold_builtin_carg (location_t loc, tree arg, tree type)
8939 if (validate_arg (arg, COMPLEX_TYPE)
8940 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg))))
8942 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8944 if (atan2_fn)
8946 tree new_arg = builtin_save_expr (arg);
8947 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8948 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8949 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8953 return NULL_TREE;
8956 /* Fold a call to builtin frexp, we can assume the base is 2. */
8958 static tree
8959 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
8961 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8962 return NULL_TREE;
8964 STRIP_NOPS (arg0);
8966 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8967 return NULL_TREE;
8969 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8971 /* Proceed if a valid pointer type was passed in. */
8972 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
8974 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8975 tree frac, exp, res;
8977 switch (value->cl)
8979 case rvc_zero:
8980 /* For +-0, return (*exp = 0, +-0). */
8981 exp = integer_zero_node;
8982 frac = arg0;
8983 break;
8984 case rvc_nan:
8985 case rvc_inf:
8986 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8987 return omit_one_operand_loc (loc, rettype, arg0, arg1);
8988 case rvc_normal:
8990 /* Since the frexp function always expects base 2, and in
8991 GCC normalized significands are already in the range
8992 [0.5, 1.0), we have exactly what frexp wants. */
8993 REAL_VALUE_TYPE frac_rvt = *value;
8994 SET_REAL_EXP (&frac_rvt, 0);
8995 frac = build_real (rettype, frac_rvt);
8996 exp = build_int_cst (integer_type_node, REAL_EXP (value));
8998 break;
8999 default:
9000 gcc_unreachable ();
9003 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9004 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
9005 TREE_SIDE_EFFECTS (arg1) = 1;
9006 res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
9007 suppress_warning (res, OPT_Wunused_value);
9008 return res;
9011 return NULL_TREE;
9014 /* Fold a call to builtin modf. */
9016 static tree
9017 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9019 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9020 return NULL_TREE;
9022 STRIP_NOPS (arg0);
9024 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9025 return NULL_TREE;
9027 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9029 /* Proceed if a valid pointer type was passed in. */
9030 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9032 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9033 REAL_VALUE_TYPE trunc, frac;
9034 tree res;
9036 switch (value->cl)
9038 case rvc_nan:
9039 case rvc_zero:
9040 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9041 trunc = frac = *value;
9042 break;
9043 case rvc_inf:
9044 /* For +-Inf, return (*arg1 = arg0, +-0). */
9045 frac = dconst0;
9046 frac.sign = value->sign;
9047 trunc = *value;
9048 break;
9049 case rvc_normal:
9050 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9051 real_trunc (&trunc, VOIDmode, value);
9052 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9053 /* If the original number was negative and already
9054 integral, then the fractional part is -0.0. */
9055 if (value->sign && frac.cl == rvc_zero)
9056 frac.sign = value->sign;
9057 break;
9060 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9061 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9062 build_real (rettype, trunc));
9063 TREE_SIDE_EFFECTS (arg1) = 1;
9064 res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9065 build_real (rettype, frac));
9066 suppress_warning (res, OPT_Wunused_value);
9067 return res;
9070 return NULL_TREE;
9073 /* Given a location LOC, an interclass builtin function decl FNDECL
9074 and its single argument ARG, return an folded expression computing
9075 the same, or NULL_TREE if we either couldn't or didn't want to fold
9076 (the latter happen if there's an RTL instruction available). */
9078 static tree
9079 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9081 machine_mode mode;
9083 if (!validate_arg (arg, REAL_TYPE))
9084 return NULL_TREE;
9086 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9087 return NULL_TREE;
9089 mode = TYPE_MODE (TREE_TYPE (arg));
9091 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
9093 /* If there is no optab, try generic code. */
9094 switch (DECL_FUNCTION_CODE (fndecl))
9096 tree result;
9098 CASE_FLT_FN (BUILT_IN_ISINF):
9100 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9101 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9102 tree type = TREE_TYPE (arg);
9103 REAL_VALUE_TYPE r;
9104 char buf[128];
9106 if (is_ibm_extended)
9108 /* NaN and Inf are encoded in the high-order double value
9109 only. The low-order value is not significant. */
9110 type = double_type_node;
9111 mode = DFmode;
9112 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9114 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9115 real_from_string (&r, buf);
9116 result = build_call_expr (isgr_fn, 2,
9117 fold_build1_loc (loc, ABS_EXPR, type, arg),
9118 build_real (type, r));
9119 return result;
9121 CASE_FLT_FN (BUILT_IN_FINITE):
9122 case BUILT_IN_ISFINITE:
9124 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9125 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9126 tree type = TREE_TYPE (arg);
9127 REAL_VALUE_TYPE r;
9128 char buf[128];
9130 if (is_ibm_extended)
9132 /* NaN and Inf are encoded in the high-order double value
9133 only. The low-order value is not significant. */
9134 type = double_type_node;
9135 mode = DFmode;
9136 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9138 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9139 real_from_string (&r, buf);
9140 result = build_call_expr (isle_fn, 2,
9141 fold_build1_loc (loc, ABS_EXPR, type, arg),
9142 build_real (type, r));
9143 /*result = fold_build2_loc (loc, UNGT_EXPR,
9144 TREE_TYPE (TREE_TYPE (fndecl)),
9145 fold_build1_loc (loc, ABS_EXPR, type, arg),
9146 build_real (type, r));
9147 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9148 TREE_TYPE (TREE_TYPE (fndecl)),
9149 result);*/
9150 return result;
9152 case BUILT_IN_ISNORMAL:
9154 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9155 islessequal(fabs(x),DBL_MAX). */
9156 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9157 tree type = TREE_TYPE (arg);
9158 tree orig_arg, max_exp, min_exp;
9159 machine_mode orig_mode = mode;
9160 REAL_VALUE_TYPE rmax, rmin;
9161 char buf[128];
9163 orig_arg = arg = builtin_save_expr (arg);
9164 if (is_ibm_extended)
9166 /* Use double to test the normal range of IBM extended
9167 precision. Emin for IBM extended precision is
9168 different to emin for IEEE double, being 53 higher
9169 since the low double exponent is at least 53 lower
9170 than the high double exponent. */
9171 type = double_type_node;
9172 mode = DFmode;
9173 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9175 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
9177 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9178 real_from_string (&rmax, buf);
9179 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9180 real_from_string (&rmin, buf);
9181 max_exp = build_real (type, rmax);
9182 min_exp = build_real (type, rmin);
9184 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
9185 if (is_ibm_extended)
9187 /* Testing the high end of the range is done just using
9188 the high double, using the same test as isfinite().
9189 For the subnormal end of the range we first test the
9190 high double, then if its magnitude is equal to the
9191 limit of 0x1p-969, we test whether the low double is
9192 non-zero and opposite sign to the high double. */
9193 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
9194 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9195 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
9196 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
9197 arg, min_exp);
9198 tree as_complex = build1 (VIEW_CONVERT_EXPR,
9199 complex_double_type_node, orig_arg);
9200 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
9201 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
9202 tree zero = build_real (type, dconst0);
9203 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
9204 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
9205 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
9206 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
9207 fold_build3 (COND_EXPR,
9208 integer_type_node,
9209 hilt, logt, lolt));
9210 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
9211 eq_min, ok_lo);
9212 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
9213 gt_min, eq_min);
9215 else
9217 tree const isge_fn
9218 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9219 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
9221 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
9222 max_exp, min_exp);
9223 return result;
9225 default:
9226 break;
9229 return NULL_TREE;
9232 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9233 ARG is the argument for the call. */
9235 static tree
9236 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9238 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9240 if (!validate_arg (arg, REAL_TYPE))
9241 return NULL_TREE;
9243 switch (builtin_index)
9245 case BUILT_IN_ISINF:
9246 if (tree_expr_infinite_p (arg))
9247 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9248 if (!tree_expr_maybe_infinite_p (arg))
9249 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9250 return NULL_TREE;
9252 case BUILT_IN_ISINF_SIGN:
9254 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9255 /* In a boolean context, GCC will fold the inner COND_EXPR to
9256 1. So e.g. "if (isinf_sign(x))" would be folded to just
9257 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9258 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
9259 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9260 tree tmp = NULL_TREE;
9262 arg = builtin_save_expr (arg);
9264 if (signbit_fn && isinf_fn)
9266 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9267 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9269 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9270 signbit_call, integer_zero_node);
9271 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9272 isinf_call, integer_zero_node);
9274 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9275 integer_minus_one_node, integer_one_node);
9276 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9277 isinf_call, tmp,
9278 integer_zero_node);
9281 return tmp;
9284 case BUILT_IN_ISFINITE:
9285 if (tree_expr_finite_p (arg))
9286 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9287 if (tree_expr_nan_p (arg) || tree_expr_infinite_p (arg))
9288 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9289 return NULL_TREE;
9291 case BUILT_IN_ISNAN:
9292 if (tree_expr_nan_p (arg))
9293 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9294 if (!tree_expr_maybe_nan_p (arg))
9295 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9298 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
9299 if (is_ibm_extended)
9301 /* NaN and Inf are encoded in the high-order double value
9302 only. The low-order value is not significant. */
9303 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
9306 arg = builtin_save_expr (arg);
9307 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9309 case BUILT_IN_ISSIGNALING:
9310 /* Folding to true for REAL_CST is done in fold_const_call_ss.
9311 Don't use tree_expr_signaling_nan_p (arg) -> integer_one_node
9312 and !tree_expr_maybe_signaling_nan_p (arg) -> integer_zero_node
9313 here, so there is some possibility of __builtin_issignaling working
9314 without -fsignaling-nans. Especially when -fno-signaling-nans is
9315 the default. */
9316 if (!tree_expr_maybe_nan_p (arg))
9317 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9318 return NULL_TREE;
9320 default:
9321 gcc_unreachable ();
9325 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9326 This builtin will generate code to return the appropriate floating
9327 point classification depending on the value of the floating point
9328 number passed in. The possible return values must be supplied as
9329 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9330 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9331 one floating point argument which is "type generic". */
9333 static tree
9334 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9336 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9337 arg, type, res, tmp;
9338 machine_mode mode;
9339 REAL_VALUE_TYPE r;
9340 char buf[128];
9342 /* Verify the required arguments in the original call. */
9343 if (nargs != 6
9344 || !validate_arg (args[0], INTEGER_TYPE)
9345 || !validate_arg (args[1], INTEGER_TYPE)
9346 || !validate_arg (args[2], INTEGER_TYPE)
9347 || !validate_arg (args[3], INTEGER_TYPE)
9348 || !validate_arg (args[4], INTEGER_TYPE)
9349 || !validate_arg (args[5], REAL_TYPE))
9350 return NULL_TREE;
9352 fp_nan = args[0];
9353 fp_infinite = args[1];
9354 fp_normal = args[2];
9355 fp_subnormal = args[3];
9356 fp_zero = args[4];
9357 arg = args[5];
9358 type = TREE_TYPE (arg);
9359 mode = TYPE_MODE (type);
9360 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9362 /* fpclassify(x) ->
9363 isnan(x) ? FP_NAN :
9364 (fabs(x) == Inf ? FP_INFINITE :
9365 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9366 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9368 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9369 build_real (type, dconst0));
9370 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9371 tmp, fp_zero, fp_subnormal);
9373 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9374 real_from_string (&r, buf);
9375 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9376 arg, build_real (type, r));
9377 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9379 if (tree_expr_maybe_infinite_p (arg))
9381 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9382 build_real (type, dconstinf));
9383 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9384 fp_infinite, res);
9387 if (tree_expr_maybe_nan_p (arg))
9389 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9390 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9393 return res;
9396 /* Fold a call to an unordered comparison function such as
9397 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9398 being called and ARG0 and ARG1 are the arguments for the call.
9399 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9400 the opposite of the desired result. UNORDERED_CODE is used
9401 for modes that can hold NaNs and ORDERED_CODE is used for
9402 the rest. */
9404 static tree
9405 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9406 enum tree_code unordered_code,
9407 enum tree_code ordered_code)
9409 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9410 enum tree_code code;
9411 tree type0, type1;
9412 enum tree_code code0, code1;
9413 tree cmp_type = NULL_TREE;
9415 type0 = TREE_TYPE (arg0);
9416 type1 = TREE_TYPE (arg1);
9418 code0 = TREE_CODE (type0);
9419 code1 = TREE_CODE (type1);
9421 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9422 /* Choose the wider of two real types. */
9423 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9424 ? type0 : type1;
9425 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9426 cmp_type = type0;
9427 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9428 cmp_type = type1;
9430 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9431 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9433 if (unordered_code == UNORDERED_EXPR)
9435 if (tree_expr_nan_p (arg0) || tree_expr_nan_p (arg1))
9436 return omit_two_operands_loc (loc, type, integer_one_node, arg0, arg1);
9437 if (!tree_expr_maybe_nan_p (arg0) && !tree_expr_maybe_nan_p (arg1))
9438 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9439 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9442 code = (tree_expr_maybe_nan_p (arg0) || tree_expr_maybe_nan_p (arg1))
9443 ? unordered_code : ordered_code;
9444 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9445 fold_build2_loc (loc, code, type, arg0, arg1));
9448 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9449 arithmetics if it can never overflow, or into internal functions that
9450 return both result of arithmetics and overflowed boolean flag in
9451 a complex integer result, or some other check for overflow.
9452 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
9453 checking part of that. */
9455 static tree
9456 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
9457 tree arg0, tree arg1, tree arg2)
9459 enum internal_fn ifn = IFN_LAST;
9460 /* The code of the expression corresponding to the built-in. */
9461 enum tree_code opcode = ERROR_MARK;
9462 bool ovf_only = false;
9464 switch (fcode)
9466 case BUILT_IN_ADD_OVERFLOW_P:
9467 ovf_only = true;
9468 /* FALLTHRU */
9469 case BUILT_IN_ADD_OVERFLOW:
9470 case BUILT_IN_SADD_OVERFLOW:
9471 case BUILT_IN_SADDL_OVERFLOW:
9472 case BUILT_IN_SADDLL_OVERFLOW:
9473 case BUILT_IN_UADD_OVERFLOW:
9474 case BUILT_IN_UADDL_OVERFLOW:
9475 case BUILT_IN_UADDLL_OVERFLOW:
9476 opcode = PLUS_EXPR;
9477 ifn = IFN_ADD_OVERFLOW;
9478 break;
9479 case BUILT_IN_SUB_OVERFLOW_P:
9480 ovf_only = true;
9481 /* FALLTHRU */
9482 case BUILT_IN_SUB_OVERFLOW:
9483 case BUILT_IN_SSUB_OVERFLOW:
9484 case BUILT_IN_SSUBL_OVERFLOW:
9485 case BUILT_IN_SSUBLL_OVERFLOW:
9486 case BUILT_IN_USUB_OVERFLOW:
9487 case BUILT_IN_USUBL_OVERFLOW:
9488 case BUILT_IN_USUBLL_OVERFLOW:
9489 opcode = MINUS_EXPR;
9490 ifn = IFN_SUB_OVERFLOW;
9491 break;
9492 case BUILT_IN_MUL_OVERFLOW_P:
9493 ovf_only = true;
9494 /* FALLTHRU */
9495 case BUILT_IN_MUL_OVERFLOW:
9496 case BUILT_IN_SMUL_OVERFLOW:
9497 case BUILT_IN_SMULL_OVERFLOW:
9498 case BUILT_IN_SMULLL_OVERFLOW:
9499 case BUILT_IN_UMUL_OVERFLOW:
9500 case BUILT_IN_UMULL_OVERFLOW:
9501 case BUILT_IN_UMULLL_OVERFLOW:
9502 opcode = MULT_EXPR;
9503 ifn = IFN_MUL_OVERFLOW;
9504 break;
9505 default:
9506 gcc_unreachable ();
9509 /* For the "generic" overloads, the first two arguments can have different
9510 types and the last argument determines the target type to use to check
9511 for overflow. The arguments of the other overloads all have the same
9512 type. */
9513 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
9515 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
9516 arguments are constant, attempt to fold the built-in call into a constant
9517 expression indicating whether or not it detected an overflow. */
9518 if (ovf_only
9519 && TREE_CODE (arg0) == INTEGER_CST
9520 && TREE_CODE (arg1) == INTEGER_CST)
9521 /* Perform the computation in the target type and check for overflow. */
9522 return omit_one_operand_loc (loc, boolean_type_node,
9523 arith_overflowed_p (opcode, type, arg0, arg1)
9524 ? boolean_true_node : boolean_false_node,
9525 arg2);
9527 tree intres, ovfres;
9528 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9530 intres = fold_binary_loc (loc, opcode, type,
9531 fold_convert_loc (loc, type, arg0),
9532 fold_convert_loc (loc, type, arg1));
9533 if (TREE_OVERFLOW (intres))
9534 intres = drop_tree_overflow (intres);
9535 ovfres = (arith_overflowed_p (opcode, type, arg0, arg1)
9536 ? boolean_true_node : boolean_false_node);
9538 else
9540 tree ctype = build_complex_type (type);
9541 tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
9542 arg0, arg1);
9543 tree tgt = save_expr (call);
9544 intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9545 ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9546 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
9549 if (ovf_only)
9550 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
9552 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
9553 tree store
9554 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
9555 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
9558 /* Fold __builtin_{add,sub}c{,l,ll} into pair of internal functions
9559 that return both result of arithmetics and overflowed boolean
9560 flag in a complex integer result. */
9562 static tree
9563 fold_builtin_addc_subc (location_t loc, enum built_in_function fcode,
9564 tree *args)
9566 enum internal_fn ifn;
9568 switch (fcode)
9570 case BUILT_IN_ADDC:
9571 case BUILT_IN_ADDCL:
9572 case BUILT_IN_ADDCLL:
9573 ifn = IFN_ADD_OVERFLOW;
9574 break;
9575 case BUILT_IN_SUBC:
9576 case BUILT_IN_SUBCL:
9577 case BUILT_IN_SUBCLL:
9578 ifn = IFN_SUB_OVERFLOW;
9579 break;
9580 default:
9581 gcc_unreachable ();
9584 tree type = TREE_TYPE (args[0]);
9585 tree ctype = build_complex_type (type);
9586 tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
9587 args[0], args[1]);
9588 tree tgt = save_expr (call);
9589 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9590 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9591 call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
9592 intres, args[2]);
9593 tgt = save_expr (call);
9594 intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9595 tree ovfres2 = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9596 ovfres = build2_loc (loc, BIT_IOR_EXPR, type, ovfres, ovfres2);
9597 tree mem_arg3 = build_fold_indirect_ref_loc (loc, args[3]);
9598 tree store
9599 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg3, ovfres);
9600 return build2_loc (loc, COMPOUND_EXPR, type, store, intres);
9603 /* Fold a call to __builtin_FILE to a constant string. */
9605 static inline tree
9606 fold_builtin_FILE (location_t loc)
9608 if (const char *fname = LOCATION_FILE (loc))
9610 /* The documentation says this builtin is equivalent to the preprocessor
9611 __FILE__ macro so it appears appropriate to use the same file prefix
9612 mappings. */
9613 fname = remap_macro_filename (fname);
9614 return build_string_literal (fname);
9617 return build_string_literal ("");
9620 /* Fold a call to __builtin_FUNCTION to a constant string. */
9622 static inline tree
9623 fold_builtin_FUNCTION ()
9625 const char *name = "";
9627 if (current_function_decl)
9628 name = lang_hooks.decl_printable_name (current_function_decl, 0);
9630 return build_string_literal (name);
9633 /* Fold a call to __builtin_LINE to an integer constant. */
9635 static inline tree
9636 fold_builtin_LINE (location_t loc, tree type)
9638 return build_int_cst (type, LOCATION_LINE (loc));
9641 /* Fold a call to built-in function FNDECL with 0 arguments.
9642 This function returns NULL_TREE if no simplification was possible. */
9644 static tree
9645 fold_builtin_0 (location_t loc, tree fndecl)
9647 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9648 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9649 switch (fcode)
9651 case BUILT_IN_FILE:
9652 return fold_builtin_FILE (loc);
9654 case BUILT_IN_FUNCTION:
9655 return fold_builtin_FUNCTION ();
9657 case BUILT_IN_LINE:
9658 return fold_builtin_LINE (loc, type);
9660 CASE_FLT_FN (BUILT_IN_INF):
9661 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
9662 case BUILT_IN_INFD32:
9663 case BUILT_IN_INFD64:
9664 case BUILT_IN_INFD128:
9665 return fold_builtin_inf (loc, type, true);
9667 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9668 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
9669 return fold_builtin_inf (loc, type, false);
9671 case BUILT_IN_CLASSIFY_TYPE:
9672 return fold_builtin_classify_type (NULL_TREE);
9674 case BUILT_IN_UNREACHABLE:
9675 /* Rewrite any explicit calls to __builtin_unreachable. */
9676 if (sanitize_flags_p (SANITIZE_UNREACHABLE))
9677 return build_builtin_unreachable (loc);
9678 break;
9680 default:
9681 break;
9683 return NULL_TREE;
9686 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9687 This function returns NULL_TREE if no simplification was possible. */
9689 static tree
9690 fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
9692 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9693 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9695 if (TREE_CODE (arg0) == ERROR_MARK)
9696 return NULL_TREE;
9698 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
9699 return ret;
9701 switch (fcode)
9703 case BUILT_IN_CONSTANT_P:
9705 tree val = fold_builtin_constant_p (arg0);
9707 /* Gimplification will pull the CALL_EXPR for the builtin out of
9708 an if condition. When not optimizing, we'll not CSE it back.
9709 To avoid link error types of regressions, return false now. */
9710 if (!val && !optimize)
9711 val = integer_zero_node;
9713 return val;
9716 case BUILT_IN_CLASSIFY_TYPE:
9717 return fold_builtin_classify_type (arg0);
9719 case BUILT_IN_STRLEN:
9720 return fold_builtin_strlen (loc, expr, type, arg0);
9722 CASE_FLT_FN (BUILT_IN_FABS):
9723 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
9724 case BUILT_IN_FABSD32:
9725 case BUILT_IN_FABSD64:
9726 case BUILT_IN_FABSD128:
9727 return fold_builtin_fabs (loc, arg0, type);
9729 case BUILT_IN_ABS:
9730 case BUILT_IN_LABS:
9731 case BUILT_IN_LLABS:
9732 case BUILT_IN_IMAXABS:
9733 return fold_builtin_abs (loc, arg0, type);
9735 CASE_FLT_FN (BUILT_IN_CONJ):
9736 if (validate_arg (arg0, COMPLEX_TYPE)
9737 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9738 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9739 break;
9741 CASE_FLT_FN (BUILT_IN_CREAL):
9742 if (validate_arg (arg0, COMPLEX_TYPE)
9743 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9744 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9745 break;
9747 CASE_FLT_FN (BUILT_IN_CIMAG):
9748 if (validate_arg (arg0, COMPLEX_TYPE)
9749 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9750 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9751 break;
9753 CASE_FLT_FN (BUILT_IN_CARG):
9754 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CARG):
9755 return fold_builtin_carg (loc, arg0, type);
9757 case BUILT_IN_ISASCII:
9758 return fold_builtin_isascii (loc, arg0);
9760 case BUILT_IN_TOASCII:
9761 return fold_builtin_toascii (loc, arg0);
9763 case BUILT_IN_ISDIGIT:
9764 return fold_builtin_isdigit (loc, arg0);
9766 CASE_FLT_FN (BUILT_IN_FINITE):
9767 case BUILT_IN_FINITED32:
9768 case BUILT_IN_FINITED64:
9769 case BUILT_IN_FINITED128:
9770 case BUILT_IN_ISFINITE:
9772 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
9773 if (ret)
9774 return ret;
9775 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9778 CASE_FLT_FN (BUILT_IN_ISINF):
9779 case BUILT_IN_ISINFD32:
9780 case BUILT_IN_ISINFD64:
9781 case BUILT_IN_ISINFD128:
9783 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
9784 if (ret)
9785 return ret;
9786 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9789 case BUILT_IN_ISNORMAL:
9790 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9792 case BUILT_IN_ISINF_SIGN:
9793 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
9795 CASE_FLT_FN (BUILT_IN_ISNAN):
9796 case BUILT_IN_ISNAND32:
9797 case BUILT_IN_ISNAND64:
9798 case BUILT_IN_ISNAND128:
9799 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
9801 case BUILT_IN_ISSIGNALING:
9802 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISSIGNALING);
9804 case BUILT_IN_FREE:
9805 if (integer_zerop (arg0))
9806 return build_empty_stmt (loc);
9807 break;
9809 default:
9810 break;
9813 return NULL_TREE;
9817 /* Folds a call EXPR (which may be null) to built-in function FNDECL
9818 with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
9819 if no simplification was possible. */
9821 static tree
9822 fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
9824 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9825 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9827 if (TREE_CODE (arg0) == ERROR_MARK
9828 || TREE_CODE (arg1) == ERROR_MARK)
9829 return NULL_TREE;
9831 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
9832 return ret;
9834 switch (fcode)
9836 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
9837 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
9838 if (validate_arg (arg0, REAL_TYPE)
9839 && validate_arg (arg1, POINTER_TYPE))
9840 return do_mpfr_lgamma_r (arg0, arg1, type);
9841 break;
9843 CASE_FLT_FN (BUILT_IN_FREXP):
9844 return fold_builtin_frexp (loc, arg0, arg1, type);
9846 CASE_FLT_FN (BUILT_IN_MODF):
9847 return fold_builtin_modf (loc, arg0, arg1, type);
9849 case BUILT_IN_STRSPN:
9850 return fold_builtin_strspn (loc, expr, arg0, arg1);
9852 case BUILT_IN_STRCSPN:
9853 return fold_builtin_strcspn (loc, expr, arg0, arg1);
9855 case BUILT_IN_STRPBRK:
9856 return fold_builtin_strpbrk (loc, expr, arg0, arg1, type);
9858 case BUILT_IN_EXPECT:
9859 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE);
9861 case BUILT_IN_ISGREATER:
9862 return fold_builtin_unordered_cmp (loc, fndecl,
9863 arg0, arg1, UNLE_EXPR, LE_EXPR);
9864 case BUILT_IN_ISGREATEREQUAL:
9865 return fold_builtin_unordered_cmp (loc, fndecl,
9866 arg0, arg1, UNLT_EXPR, LT_EXPR);
9867 case BUILT_IN_ISLESS:
9868 return fold_builtin_unordered_cmp (loc, fndecl,
9869 arg0, arg1, UNGE_EXPR, GE_EXPR);
9870 case BUILT_IN_ISLESSEQUAL:
9871 return fold_builtin_unordered_cmp (loc, fndecl,
9872 arg0, arg1, UNGT_EXPR, GT_EXPR);
9873 case BUILT_IN_ISLESSGREATER:
9874 return fold_builtin_unordered_cmp (loc, fndecl,
9875 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
9876 case BUILT_IN_ISUNORDERED:
9877 return fold_builtin_unordered_cmp (loc, fndecl,
9878 arg0, arg1, UNORDERED_EXPR,
9879 NOP_EXPR);
9881 /* We do the folding for va_start in the expander. */
9882 case BUILT_IN_VA_START:
9883 break;
9885 case BUILT_IN_OBJECT_SIZE:
9886 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
9887 return fold_builtin_object_size (arg0, arg1, fcode);
9889 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
9890 return fold_builtin_atomic_always_lock_free (arg0, arg1);
9892 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
9893 return fold_builtin_atomic_is_lock_free (arg0, arg1);
9895 default:
9896 break;
9898 return NULL_TREE;
9901 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
9902 and ARG2.
9903 This function returns NULL_TREE if no simplification was possible. */
9905 static tree
9906 fold_builtin_3 (location_t loc, tree fndecl,
9907 tree arg0, tree arg1, tree arg2)
9909 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9910 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9912 if (TREE_CODE (arg0) == ERROR_MARK
9913 || TREE_CODE (arg1) == ERROR_MARK
9914 || TREE_CODE (arg2) == ERROR_MARK)
9915 return NULL_TREE;
9917 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
9918 arg0, arg1, arg2))
9919 return ret;
9921 switch (fcode)
9924 CASE_FLT_FN (BUILT_IN_SINCOS):
9925 return fold_builtin_sincos (loc, arg0, arg1, arg2);
9927 CASE_FLT_FN (BUILT_IN_REMQUO):
9928 if (validate_arg (arg0, REAL_TYPE)
9929 && validate_arg (arg1, REAL_TYPE)
9930 && validate_arg (arg2, POINTER_TYPE))
9931 return do_mpfr_remquo (arg0, arg1, arg2);
9932 break;
9934 case BUILT_IN_MEMCMP:
9935 return fold_builtin_memcmp (loc, arg0, arg1, arg2);
9937 case BUILT_IN_EXPECT:
9938 return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE);
9940 case BUILT_IN_EXPECT_WITH_PROBABILITY:
9941 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2);
9943 case BUILT_IN_ADD_OVERFLOW:
9944 case BUILT_IN_SUB_OVERFLOW:
9945 case BUILT_IN_MUL_OVERFLOW:
9946 case BUILT_IN_ADD_OVERFLOW_P:
9947 case BUILT_IN_SUB_OVERFLOW_P:
9948 case BUILT_IN_MUL_OVERFLOW_P:
9949 case BUILT_IN_SADD_OVERFLOW:
9950 case BUILT_IN_SADDL_OVERFLOW:
9951 case BUILT_IN_SADDLL_OVERFLOW:
9952 case BUILT_IN_SSUB_OVERFLOW:
9953 case BUILT_IN_SSUBL_OVERFLOW:
9954 case BUILT_IN_SSUBLL_OVERFLOW:
9955 case BUILT_IN_SMUL_OVERFLOW:
9956 case BUILT_IN_SMULL_OVERFLOW:
9957 case BUILT_IN_SMULLL_OVERFLOW:
9958 case BUILT_IN_UADD_OVERFLOW:
9959 case BUILT_IN_UADDL_OVERFLOW:
9960 case BUILT_IN_UADDLL_OVERFLOW:
9961 case BUILT_IN_USUB_OVERFLOW:
9962 case BUILT_IN_USUBL_OVERFLOW:
9963 case BUILT_IN_USUBLL_OVERFLOW:
9964 case BUILT_IN_UMUL_OVERFLOW:
9965 case BUILT_IN_UMULL_OVERFLOW:
9966 case BUILT_IN_UMULLL_OVERFLOW:
9967 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
9969 default:
9970 break;
9972 return NULL_TREE;
9975 /* Folds a call EXPR (which may be null) to built-in function FNDECL.
9976 ARGS is an array of NARGS arguments. IGNORE is true if the result
9977 of the function call is ignored. This function returns NULL_TREE
9978 if no simplification was possible. */
9980 static tree
9981 fold_builtin_n (location_t loc, tree expr, tree fndecl, tree *args,
9982 int nargs, bool)
9984 tree ret = NULL_TREE;
9986 switch (nargs)
9988 case 0:
9989 ret = fold_builtin_0 (loc, fndecl);
9990 break;
9991 case 1:
9992 ret = fold_builtin_1 (loc, expr, fndecl, args[0]);
9993 break;
9994 case 2:
9995 ret = fold_builtin_2 (loc, expr, fndecl, args[0], args[1]);
9996 break;
9997 case 3:
9998 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
9999 break;
10000 default:
10001 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
10002 break;
10004 if (ret)
10006 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10007 SET_EXPR_LOCATION (ret, loc);
10008 return ret;
10010 return NULL_TREE;
10013 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
10014 list ARGS along with N new arguments in NEWARGS. SKIP is the number
10015 of arguments in ARGS to be omitted. OLDNARGS is the number of
10016 elements in ARGS. */
10018 static tree
10019 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
10020 int skip, tree fndecl, int n, va_list newargs)
10022 int nargs = oldnargs - skip + n;
10023 tree *buffer;
10025 if (n > 0)
10027 int i, j;
10029 buffer = XALLOCAVEC (tree, nargs);
10030 for (i = 0; i < n; i++)
10031 buffer[i] = va_arg (newargs, tree);
10032 for (j = skip; j < oldnargs; j++, i++)
10033 buffer[i] = args[j];
10035 else
10036 buffer = args + skip;
10038 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
10041 /* Return true if FNDECL shouldn't be folded right now.
10042 If a built-in function has an inline attribute always_inline
10043 wrapper, defer folding it after always_inline functions have
10044 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
10045 might not be performed. */
10047 bool
10048 avoid_folding_inline_builtin (tree fndecl)
10050 return (DECL_DECLARED_INLINE_P (fndecl)
10051 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
10052 && cfun
10053 && !cfun->always_inline_functions_inlined
10054 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
10057 /* A wrapper function for builtin folding that prevents warnings for
10058 "statement without effect" and the like, caused by removing the
10059 call node earlier than the warning is generated. */
10061 tree
10062 fold_call_expr (location_t loc, tree exp, bool ignore)
10064 tree ret = NULL_TREE;
10065 tree fndecl = get_callee_fndecl (exp);
10066 if (fndecl && fndecl_built_in_p (fndecl)
10067 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10068 yet. Defer folding until we see all the arguments
10069 (after inlining). */
10070 && !CALL_EXPR_VA_ARG_PACK (exp))
10072 int nargs = call_expr_nargs (exp);
10074 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10075 instead last argument is __builtin_va_arg_pack (). Defer folding
10076 even in that case, until arguments are finalized. */
10077 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10079 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10080 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
10081 return NULL_TREE;
10084 if (avoid_folding_inline_builtin (fndecl))
10085 return NULL_TREE;
10087 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10088 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10089 CALL_EXPR_ARGP (exp), ignore);
10090 else
10092 tree *args = CALL_EXPR_ARGP (exp);
10093 ret = fold_builtin_n (loc, exp, fndecl, args, nargs, ignore);
10094 if (ret)
10095 return ret;
10098 return NULL_TREE;
10101 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
10102 N arguments are passed in the array ARGARRAY. Return a folded
10103 expression or NULL_TREE if no simplification was possible. */
10105 tree
10106 fold_builtin_call_array (location_t loc, tree,
10107 tree fn,
10108 int n,
10109 tree *argarray)
10111 if (TREE_CODE (fn) != ADDR_EXPR)
10112 return NULL_TREE;
10114 tree fndecl = TREE_OPERAND (fn, 0);
10115 if (TREE_CODE (fndecl) == FUNCTION_DECL
10116 && fndecl_built_in_p (fndecl))
10118 /* If last argument is __builtin_va_arg_pack (), arguments to this
10119 function are not finalized yet. Defer folding until they are. */
10120 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10122 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10123 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
10124 return NULL_TREE;
10126 if (avoid_folding_inline_builtin (fndecl))
10127 return NULL_TREE;
10128 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10129 return targetm.fold_builtin (fndecl, n, argarray, false);
10130 else
10131 return fold_builtin_n (loc, NULL_TREE, fndecl, argarray, n, false);
10134 return NULL_TREE;
10137 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10138 along with N new arguments specified as the "..." parameters. SKIP
10139 is the number of arguments in EXP to be omitted. This function is used
10140 to do varargs-to-varargs transformations. */
10142 static tree
10143 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10145 va_list ap;
10146 tree t;
10148 va_start (ap, n);
10149 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10150 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10151 va_end (ap);
10153 return t;
10156 /* Validate a single argument ARG against a tree code CODE representing
10157 a type. Return true when argument is valid. */
10159 static bool
10160 validate_arg (const_tree arg, enum tree_code code)
10162 if (!arg)
10163 return false;
10164 else if (code == POINTER_TYPE)
10165 return POINTER_TYPE_P (TREE_TYPE (arg));
10166 else if (code == INTEGER_TYPE)
10167 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10168 return code == TREE_CODE (TREE_TYPE (arg));
10171 /* This function validates the types of a function call argument list
10172 against a specified list of tree_codes. If the last specifier is a 0,
10173 that represents an ellipses, otherwise the last specifier must be a
10174 VOID_TYPE.
10176 This is the GIMPLE version of validate_arglist. Eventually we want to
10177 completely convert builtins.cc to work from GIMPLEs and the tree based
10178 validate_arglist will then be removed. */
10180 bool
10181 validate_gimple_arglist (const gcall *call, ...)
10183 enum tree_code code;
10184 bool res = 0;
10185 va_list ap;
10186 const_tree arg;
10187 size_t i;
10189 va_start (ap, call);
10190 i = 0;
10194 code = (enum tree_code) va_arg (ap, int);
10195 switch (code)
10197 case 0:
10198 /* This signifies an ellipses, any further arguments are all ok. */
10199 res = true;
10200 goto end;
10201 case VOID_TYPE:
10202 /* This signifies an endlink, if no arguments remain, return
10203 true, otherwise return false. */
10204 res = (i == gimple_call_num_args (call));
10205 goto end;
10206 default:
10207 /* If no parameters remain or the parameter's code does not
10208 match the specified code, return false. Otherwise continue
10209 checking any remaining arguments. */
10210 arg = gimple_call_arg (call, i++);
10211 if (!validate_arg (arg, code))
10212 goto end;
10213 break;
10216 while (1);
10218 /* We need gotos here since we can only have one VA_CLOSE in a
10219 function. */
10220 end: ;
10221 va_end (ap);
10223 return res;
10226 /* Default target-specific builtin expander that does nothing. */
10229 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10230 rtx target ATTRIBUTE_UNUSED,
10231 rtx subtarget ATTRIBUTE_UNUSED,
10232 machine_mode mode ATTRIBUTE_UNUSED,
10233 int ignore ATTRIBUTE_UNUSED)
10235 return NULL_RTX;
10238 /* Returns true is EXP represents data that would potentially reside
10239 in a readonly section. */
10241 bool
10242 readonly_data_expr (tree exp)
10244 STRIP_NOPS (exp);
10246 if (TREE_CODE (exp) != ADDR_EXPR)
10247 return false;
10249 exp = get_base_address (TREE_OPERAND (exp, 0));
10250 if (!exp)
10251 return false;
10253 /* Make sure we call decl_readonly_section only for trees it
10254 can handle (since it returns true for everything it doesn't
10255 understand). */
10256 if (TREE_CODE (exp) == STRING_CST
10257 || TREE_CODE (exp) == CONSTRUCTOR
10258 || (VAR_P (exp) && TREE_STATIC (exp)))
10259 return decl_readonly_section (exp, 0);
10260 else
10261 return false;
10264 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
10265 to the call, and TYPE is its return type.
10267 Return NULL_TREE if no simplification was possible, otherwise return the
10268 simplified form of the call as a tree.
10270 The simplified form may be a constant or other expression which
10271 computes the same value, but in a more efficient manner (including
10272 calls to other builtin functions).
10274 The call may contain arguments which need to be evaluated, but
10275 which are not useful to determine the result of the call. In
10276 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10277 COMPOUND_EXPR will be an argument which must be evaluated.
10278 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10279 COMPOUND_EXPR in the chain will contain the tree for the simplified
10280 form of the builtin function call. */
10282 static tree
10283 fold_builtin_strpbrk (location_t loc, tree, tree s1, tree s2, tree type)
10285 if (!validate_arg (s1, POINTER_TYPE)
10286 || !validate_arg (s2, POINTER_TYPE))
10287 return NULL_TREE;
10289 tree fn;
10290 const char *p1, *p2;
10292 p2 = c_getstr (s2);
10293 if (p2 == NULL)
10294 return NULL_TREE;
10296 p1 = c_getstr (s1);
10297 if (p1 != NULL)
10299 const char *r = strpbrk (p1, p2);
10300 tree tem;
10302 if (r == NULL)
10303 return build_int_cst (TREE_TYPE (s1), 0);
10305 /* Return an offset into the constant string argument. */
10306 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10307 return fold_convert_loc (loc, type, tem);
10310 if (p2[0] == '\0')
10311 /* strpbrk(x, "") == NULL.
10312 Evaluate and ignore s1 in case it had side-effects. */
10313 return omit_one_operand_loc (loc, type, integer_zero_node, s1);
10315 if (p2[1] != '\0')
10316 return NULL_TREE; /* Really call strpbrk. */
10318 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10319 if (!fn)
10320 return NULL_TREE;
10322 /* New argument list transforming strpbrk(s1, s2) to
10323 strchr(s1, s2[0]). */
10324 return build_call_expr_loc (loc, fn, 2, s1,
10325 build_int_cst (integer_type_node, p2[0]));
10328 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
10329 to the call.
10331 Return NULL_TREE if no simplification was possible, otherwise return the
10332 simplified form of the call as a tree.
10334 The simplified form may be a constant or other expression which
10335 computes the same value, but in a more efficient manner (including
10336 calls to other builtin functions).
10338 The call may contain arguments which need to be evaluated, but
10339 which are not useful to determine the result of the call. In
10340 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10341 COMPOUND_EXPR will be an argument which must be evaluated.
10342 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10343 COMPOUND_EXPR in the chain will contain the tree for the simplified
10344 form of the builtin function call. */
10346 static tree
10347 fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2)
10349 if (!validate_arg (s1, POINTER_TYPE)
10350 || !validate_arg (s2, POINTER_TYPE))
10351 return NULL_TREE;
10353 if (!check_nul_terminated_array (expr, s1)
10354 || !check_nul_terminated_array (expr, s2))
10355 return NULL_TREE;
10357 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10359 /* If either argument is "", return NULL_TREE. */
10360 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
10361 /* Evaluate and ignore both arguments in case either one has
10362 side-effects. */
10363 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
10364 s1, s2);
10365 return NULL_TREE;
10368 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
10369 to the call.
10371 Return NULL_TREE if no simplification was possible, otherwise return the
10372 simplified form of the call as a tree.
10374 The simplified form may be a constant or other expression which
10375 computes the same value, but in a more efficient manner (including
10376 calls to other builtin functions).
10378 The call may contain arguments which need to be evaluated, but
10379 which are not useful to determine the result of the call. In
10380 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10381 COMPOUND_EXPR will be an argument which must be evaluated.
10382 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10383 COMPOUND_EXPR in the chain will contain the tree for the simplified
10384 form of the builtin function call. */
10386 static tree
10387 fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2)
10389 if (!validate_arg (s1, POINTER_TYPE)
10390 || !validate_arg (s2, POINTER_TYPE))
10391 return NULL_TREE;
10393 if (!check_nul_terminated_array (expr, s1)
10394 || !check_nul_terminated_array (expr, s2))
10395 return NULL_TREE;
10397 /* If the first argument is "", return NULL_TREE. */
10398 const char *p1 = c_getstr (s1);
10399 if (p1 && *p1 == '\0')
10401 /* Evaluate and ignore argument s2 in case it has
10402 side-effects. */
10403 return omit_one_operand_loc (loc, size_type_node,
10404 size_zero_node, s2);
10407 /* If the second argument is "", return __builtin_strlen(s1). */
10408 const char *p2 = c_getstr (s2);
10409 if (p2 && *p2 == '\0')
10411 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
10413 /* If the replacement _DECL isn't initialized, don't do the
10414 transformation. */
10415 if (!fn)
10416 return NULL_TREE;
10418 return build_call_expr_loc (loc, fn, 1, s1);
10420 return NULL_TREE;
10423 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
10424 produced. False otherwise. This is done so that we don't output the error
10425 or warning twice or three times. */
10427 bool
10428 fold_builtin_next_arg (tree exp, bool va_start_p)
10430 tree fntype = TREE_TYPE (current_function_decl);
10431 int nargs = call_expr_nargs (exp);
10432 tree arg;
10433 /* There is good chance the current input_location points inside the
10434 definition of the va_start macro (perhaps on the token for
10435 builtin) in a system header, so warnings will not be emitted.
10436 Use the location in real source code. */
10437 location_t current_location =
10438 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
10439 NULL);
10441 if (!stdarg_p (fntype))
10443 error ("%<va_start%> used in function with fixed arguments");
10444 return true;
10447 if (va_start_p)
10449 if (va_start_p && (nargs != 2))
10451 error ("wrong number of arguments to function %<va_start%>");
10452 return true;
10454 arg = CALL_EXPR_ARG (exp, 1);
10456 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10457 when we checked the arguments and if needed issued a warning. */
10458 else
10460 if (nargs == 0)
10462 /* Evidently an out of date version of <stdarg.h>; can't validate
10463 va_start's second argument, but can still work as intended. */
10464 warning_at (current_location,
10465 OPT_Wvarargs,
10466 "%<__builtin_next_arg%> called without an argument");
10467 return true;
10469 else if (nargs > 1)
10471 error ("wrong number of arguments to function %<__builtin_next_arg%>");
10472 return true;
10474 arg = CALL_EXPR_ARG (exp, 0);
10477 if (TREE_CODE (arg) == SSA_NAME
10478 && SSA_NAME_VAR (arg))
10479 arg = SSA_NAME_VAR (arg);
10481 /* We destructively modify the call to be __builtin_va_start (ap, 0)
10482 or __builtin_next_arg (0) the first time we see it, after checking
10483 the arguments and if needed issuing a warning. */
10484 if (!integer_zerop (arg))
10486 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10488 /* Strip off all nops for the sake of the comparison. This
10489 is not quite the same as STRIP_NOPS. It does more.
10490 We must also strip off INDIRECT_EXPR for C++ reference
10491 parameters. */
10492 while (CONVERT_EXPR_P (arg)
10493 || INDIRECT_REF_P (arg))
10494 arg = TREE_OPERAND (arg, 0);
10495 if (arg != last_parm)
10497 /* FIXME: Sometimes with the tree optimizers we can get the
10498 not the last argument even though the user used the last
10499 argument. We just warn and set the arg to be the last
10500 argument so that we will get wrong-code because of
10501 it. */
10502 warning_at (current_location,
10503 OPT_Wvarargs,
10504 "second parameter of %<va_start%> not last named argument");
10507 /* Undefined by C99 7.15.1.4p4 (va_start):
10508 "If the parameter parmN is declared with the register storage
10509 class, with a function or array type, or with a type that is
10510 not compatible with the type that results after application of
10511 the default argument promotions, the behavior is undefined."
10513 else if (DECL_REGISTER (arg))
10515 warning_at (current_location,
10516 OPT_Wvarargs,
10517 "undefined behavior when second parameter of "
10518 "%<va_start%> is declared with %<register%> storage");
10521 /* We want to verify the second parameter just once before the tree
10522 optimizers are run and then avoid keeping it in the tree,
10523 as otherwise we could warn even for correct code like:
10524 void foo (int i, ...)
10525 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10526 if (va_start_p)
10527 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
10528 else
10529 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
10531 return false;
10535 /* Expand a call EXP to __builtin_object_size. */
10537 static rtx
10538 expand_builtin_object_size (tree exp)
10540 tree ost;
10541 int object_size_type;
10542 tree fndecl = get_callee_fndecl (exp);
10544 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10546 error ("first argument of %qD must be a pointer, second integer constant",
10547 fndecl);
10548 expand_builtin_trap ();
10549 return const0_rtx;
10552 ost = CALL_EXPR_ARG (exp, 1);
10553 STRIP_NOPS (ost);
10555 if (TREE_CODE (ost) != INTEGER_CST
10556 || tree_int_cst_sgn (ost) < 0
10557 || compare_tree_int (ost, 3) > 0)
10559 error ("last argument of %qD is not integer constant between 0 and 3",
10560 fndecl);
10561 expand_builtin_trap ();
10562 return const0_rtx;
10565 object_size_type = tree_to_shwi (ost);
10567 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10570 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10571 FCODE is the BUILT_IN_* to use.
10572 Return NULL_RTX if we failed; the caller should emit a normal call,
10573 otherwise try to get the result in TARGET, if convenient (and in
10574 mode MODE if that's convenient). */
10576 static rtx
10577 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
10578 enum built_in_function fcode)
10580 if (!validate_arglist (exp,
10581 POINTER_TYPE,
10582 fcode == BUILT_IN_MEMSET_CHK
10583 ? INTEGER_TYPE : POINTER_TYPE,
10584 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10585 return NULL_RTX;
10587 tree dest = CALL_EXPR_ARG (exp, 0);
10588 tree src = CALL_EXPR_ARG (exp, 1);
10589 tree len = CALL_EXPR_ARG (exp, 2);
10590 tree size = CALL_EXPR_ARG (exp, 3);
10592 /* FIXME: Set access mode to write only for memset et al. */
10593 bool sizes_ok = check_access (exp, len, /*maxread=*/NULL_TREE,
10594 /*srcstr=*/NULL_TREE, size, access_read_write);
10596 if (!tree_fits_uhwi_p (size))
10597 return NULL_RTX;
10599 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
10601 /* Avoid transforming the checking call to an ordinary one when
10602 an overflow has been detected or when the call couldn't be
10603 validated because the size is not constant. */
10604 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
10605 return NULL_RTX;
10607 tree fn = NULL_TREE;
10608 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10609 mem{cpy,pcpy,move,set} is available. */
10610 switch (fcode)
10612 case BUILT_IN_MEMCPY_CHK:
10613 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
10614 break;
10615 case BUILT_IN_MEMPCPY_CHK:
10616 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
10617 break;
10618 case BUILT_IN_MEMMOVE_CHK:
10619 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
10620 break;
10621 case BUILT_IN_MEMSET_CHK:
10622 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
10623 break;
10624 default:
10625 break;
10628 if (! fn)
10629 return NULL_RTX;
10631 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
10632 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10633 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10634 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10636 else if (fcode == BUILT_IN_MEMSET_CHK)
10637 return NULL_RTX;
10638 else
10640 unsigned int dest_align = get_pointer_alignment (dest);
10642 /* If DEST is not a pointer type, call the normal function. */
10643 if (dest_align == 0)
10644 return NULL_RTX;
10646 /* If SRC and DEST are the same (and not volatile), do nothing. */
10647 if (operand_equal_p (src, dest, 0))
10649 tree expr;
10651 if (fcode != BUILT_IN_MEMPCPY_CHK)
10653 /* Evaluate and ignore LEN in case it has side-effects. */
10654 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10655 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10658 expr = fold_build_pointer_plus (dest, len);
10659 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10662 /* __memmove_chk special case. */
10663 if (fcode == BUILT_IN_MEMMOVE_CHK)
10665 unsigned int src_align = get_pointer_alignment (src);
10667 if (src_align == 0)
10668 return NULL_RTX;
10670 /* If src is categorized for a readonly section we can use
10671 normal __memcpy_chk. */
10672 if (readonly_data_expr (src))
10674 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
10675 if (!fn)
10676 return NULL_RTX;
10677 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
10678 dest, src, len, size);
10679 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10680 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10681 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10684 return NULL_RTX;
10688 /* Emit warning if a buffer overflow is detected at compile time. */
10690 static void
10691 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10693 /* The source string. */
10694 tree srcstr = NULL_TREE;
10695 /* The size of the destination object returned by __builtin_object_size. */
10696 tree objsize = NULL_TREE;
10697 /* The string that is being concatenated with (as in __strcat_chk)
10698 or null if it isn't. */
10699 tree catstr = NULL_TREE;
10700 /* The maximum length of the source sequence in a bounded operation
10701 (such as __strncat_chk) or null if the operation isn't bounded
10702 (such as __strcat_chk). */
10703 tree maxread = NULL_TREE;
10704 /* The exact size of the access (such as in __strncpy_chk). */
10705 tree size = NULL_TREE;
10706 /* The access by the function that's checked. Except for snprintf
10707 both writing and reading is checked. */
10708 access_mode mode = access_read_write;
10710 switch (fcode)
10712 case BUILT_IN_STRCPY_CHK:
10713 case BUILT_IN_STPCPY_CHK:
10714 srcstr = CALL_EXPR_ARG (exp, 1);
10715 objsize = CALL_EXPR_ARG (exp, 2);
10716 break;
10718 case BUILT_IN_STRCAT_CHK:
10719 /* For __strcat_chk the warning will be emitted only if overflowing
10720 by at least strlen (dest) + 1 bytes. */
10721 catstr = CALL_EXPR_ARG (exp, 0);
10722 srcstr = CALL_EXPR_ARG (exp, 1);
10723 objsize = CALL_EXPR_ARG (exp, 2);
10724 break;
10726 case BUILT_IN_STRNCAT_CHK:
10727 catstr = CALL_EXPR_ARG (exp, 0);
10728 srcstr = CALL_EXPR_ARG (exp, 1);
10729 maxread = CALL_EXPR_ARG (exp, 2);
10730 objsize = CALL_EXPR_ARG (exp, 3);
10731 break;
10733 case BUILT_IN_STRNCPY_CHK:
10734 case BUILT_IN_STPNCPY_CHK:
10735 srcstr = CALL_EXPR_ARG (exp, 1);
10736 size = CALL_EXPR_ARG (exp, 2);
10737 objsize = CALL_EXPR_ARG (exp, 3);
10738 break;
10740 case BUILT_IN_SNPRINTF_CHK:
10741 case BUILT_IN_VSNPRINTF_CHK:
10742 maxread = CALL_EXPR_ARG (exp, 1);
10743 objsize = CALL_EXPR_ARG (exp, 3);
10744 /* The only checked access the write to the destination. */
10745 mode = access_write_only;
10746 break;
10747 default:
10748 gcc_unreachable ();
10751 if (catstr && maxread)
10753 /* Check __strncat_chk. There is no way to determine the length
10754 of the string to which the source string is being appended so
10755 just warn when the length of the source string is not known. */
10756 check_strncat_sizes (exp, objsize);
10757 return;
10760 check_access (exp, size, maxread, srcstr, objsize, mode);
10763 /* Emit warning if a buffer overflow is detected at compile time
10764 in __sprintf_chk/__vsprintf_chk calls. */
10766 static void
10767 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10769 tree size, len, fmt;
10770 const char *fmt_str;
10771 int nargs = call_expr_nargs (exp);
10773 /* Verify the required arguments in the original call. */
10775 if (nargs < 4)
10776 return;
10777 size = CALL_EXPR_ARG (exp, 2);
10778 fmt = CALL_EXPR_ARG (exp, 3);
10780 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
10781 return;
10783 /* Check whether the format is a literal string constant. */
10784 fmt_str = c_getstr (fmt);
10785 if (fmt_str == NULL)
10786 return;
10788 if (!init_target_chars ())
10789 return;
10791 /* If the format doesn't contain % args or %%, we know its size. */
10792 if (strchr (fmt_str, target_percent) == 0)
10793 len = build_int_cstu (size_type_node, strlen (fmt_str));
10794 /* If the format is "%s" and first ... argument is a string literal,
10795 we know it too. */
10796 else if (fcode == BUILT_IN_SPRINTF_CHK
10797 && strcmp (fmt_str, target_percent_s) == 0)
10799 tree arg;
10801 if (nargs < 5)
10802 return;
10803 arg = CALL_EXPR_ARG (exp, 4);
10804 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10805 return;
10807 len = c_strlen (arg, 1);
10808 if (!len || ! tree_fits_uhwi_p (len))
10809 return;
10811 else
10812 return;
10814 /* Add one for the terminating nul. */
10815 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
10817 check_access (exp, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, len, size,
10818 access_write_only);
10821 /* Fold a call to __builtin_object_size with arguments PTR and OST,
10822 if possible. */
10824 static tree
10825 fold_builtin_object_size (tree ptr, tree ost, enum built_in_function fcode)
10827 tree bytes;
10828 int object_size_type;
10830 if (!validate_arg (ptr, POINTER_TYPE)
10831 || !validate_arg (ost, INTEGER_TYPE))
10832 return NULL_TREE;
10834 STRIP_NOPS (ost);
10836 if (TREE_CODE (ost) != INTEGER_CST
10837 || tree_int_cst_sgn (ost) < 0
10838 || compare_tree_int (ost, 3) > 0)
10839 return NULL_TREE;
10841 object_size_type = tree_to_shwi (ost);
10843 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10844 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10845 and (size_t) 0 for types 2 and 3. */
10846 if (TREE_SIDE_EFFECTS (ptr))
10847 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10849 if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
10850 object_size_type |= OST_DYNAMIC;
10852 if (TREE_CODE (ptr) == ADDR_EXPR)
10854 compute_builtin_object_size (ptr, object_size_type, &bytes);
10855 if ((object_size_type & OST_DYNAMIC)
10856 || int_fits_type_p (bytes, size_type_node))
10857 return fold_convert (size_type_node, bytes);
10859 else if (TREE_CODE (ptr) == SSA_NAME)
10861 /* If object size is not known yet, delay folding until
10862 later. Maybe subsequent passes will help determining
10863 it. */
10864 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
10865 && ((object_size_type & OST_DYNAMIC)
10866 || int_fits_type_p (bytes, size_type_node)))
10867 return fold_convert (size_type_node, bytes);
10870 return NULL_TREE;
10873 /* Builtins with folding operations that operate on "..." arguments
10874 need special handling; we need to store the arguments in a convenient
10875 data structure before attempting any folding. Fortunately there are
10876 only a few builtins that fall into this category. FNDECL is the
10877 function, EXP is the CALL_EXPR for the call. */
10879 static tree
10880 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
10882 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10883 tree ret = NULL_TREE;
10885 switch (fcode)
10887 case BUILT_IN_FPCLASSIFY:
10888 ret = fold_builtin_fpclassify (loc, args, nargs);
10889 break;
10891 case BUILT_IN_ADDC:
10892 case BUILT_IN_ADDCL:
10893 case BUILT_IN_ADDCLL:
10894 case BUILT_IN_SUBC:
10895 case BUILT_IN_SUBCL:
10896 case BUILT_IN_SUBCLL:
10897 return fold_builtin_addc_subc (loc, fcode, args);
10899 default:
10900 break;
10902 if (ret)
10904 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10905 SET_EXPR_LOCATION (ret, loc);
10906 suppress_warning (ret);
10907 return ret;
10909 return NULL_TREE;
10912 /* Initialize format string characters in the target charset. */
10914 bool
10915 init_target_chars (void)
10917 static bool init;
10918 if (!init)
10920 target_newline = lang_hooks.to_target_charset ('\n');
10921 target_percent = lang_hooks.to_target_charset ('%');
10922 target_c = lang_hooks.to_target_charset ('c');
10923 target_s = lang_hooks.to_target_charset ('s');
10924 if (target_newline == 0 || target_percent == 0 || target_c == 0
10925 || target_s == 0)
10926 return false;
10928 target_percent_c[0] = target_percent;
10929 target_percent_c[1] = target_c;
10930 target_percent_c[2] = '\0';
10932 target_percent_s[0] = target_percent;
10933 target_percent_s[1] = target_s;
10934 target_percent_s[2] = '\0';
10936 target_percent_s_newline[0] = target_percent;
10937 target_percent_s_newline[1] = target_s;
10938 target_percent_s_newline[2] = target_newline;
10939 target_percent_s_newline[3] = '\0';
10941 init = true;
10943 return true;
10946 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
10947 and no overflow/underflow occurred. INEXACT is true if M was not
10948 exactly calculated. TYPE is the tree type for the result. This
10949 function assumes that you cleared the MPFR flags and then
10950 calculated M to see if anything subsequently set a flag prior to
10951 entering this function. Return NULL_TREE if any checks fail. */
10953 static tree
10954 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
10956 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10957 overflow/underflow occurred. If -frounding-math, proceed iff the
10958 result of calling FUNC was exact. */
10959 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
10960 && (!flag_rounding_math || !inexact))
10962 REAL_VALUE_TYPE rr;
10964 real_from_mpfr (&rr, m, type, MPFR_RNDN);
10965 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
10966 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10967 but the mpfr_t is not, then we underflowed in the
10968 conversion. */
10969 if (real_isfinite (&rr)
10970 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
10972 REAL_VALUE_TYPE rmode;
10974 real_convert (&rmode, TYPE_MODE (type), &rr);
10975 /* Proceed iff the specified mode can hold the value. */
10976 if (real_identical (&rmode, &rr))
10977 return build_real (type, rmode);
10980 return NULL_TREE;
10983 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
10984 number and no overflow/underflow occurred. INEXACT is true if M
10985 was not exactly calculated. TYPE is the tree type for the result.
10986 This function assumes that you cleared the MPFR flags and then
10987 calculated M to see if anything subsequently set a flag prior to
10988 entering this function. Return NULL_TREE if any checks fail, if
10989 FORCE_CONVERT is true, then bypass the checks. */
10991 static tree
10992 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
10994 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10995 overflow/underflow occurred. If -frounding-math, proceed iff the
10996 result of calling FUNC was exact. */
10997 if (force_convert
10998 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
10999 && !mpfr_overflow_p () && !mpfr_underflow_p ()
11000 && (!flag_rounding_math || !inexact)))
11002 REAL_VALUE_TYPE re, im;
11004 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), MPFR_RNDN);
11005 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), MPFR_RNDN);
11006 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
11007 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
11008 but the mpfr_t is not, then we underflowed in the
11009 conversion. */
11010 if (force_convert
11011 || (real_isfinite (&re) && real_isfinite (&im)
11012 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
11013 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
11015 REAL_VALUE_TYPE re_mode, im_mode;
11017 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
11018 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
11019 /* Proceed iff the specified mode can hold the value. */
11020 if (force_convert
11021 || (real_identical (&re_mode, &re)
11022 && real_identical (&im_mode, &im)))
11023 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
11024 build_real (TREE_TYPE (type), im_mode));
11027 return NULL_TREE;
11030 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
11031 the pointer *(ARG_QUO) and return the result. The type is taken
11032 from the type of ARG0 and is used for setting the precision of the
11033 calculation and results. */
11035 static tree
11036 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
11038 tree const type = TREE_TYPE (arg0);
11039 tree result = NULL_TREE;
11041 STRIP_NOPS (arg0);
11042 STRIP_NOPS (arg1);
11044 /* To proceed, MPFR must exactly represent the target floating point
11045 format, which only happens when the target base equals two. */
11046 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11047 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
11048 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
11050 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
11051 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
11053 if (real_isfinite (ra0) && real_isfinite (ra1))
11055 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11056 const int prec = fmt->p;
11057 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
11058 tree result_rem;
11059 long integer_quo;
11060 mpfr_t m0, m1;
11062 mpfr_inits2 (prec, m0, m1, NULL);
11063 mpfr_from_real (m0, ra0, MPFR_RNDN);
11064 mpfr_from_real (m1, ra1, MPFR_RNDN);
11065 mpfr_clear_flags ();
11066 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
11067 /* Remquo is independent of the rounding mode, so pass
11068 inexact=0 to do_mpfr_ckconv(). */
11069 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
11070 mpfr_clears (m0, m1, NULL);
11071 if (result_rem)
11073 /* MPFR calculates quo in the host's long so it may
11074 return more bits in quo than the target int can hold
11075 if sizeof(host long) > sizeof(target int). This can
11076 happen even for native compilers in LP64 mode. In
11077 these cases, modulo the quo value with the largest
11078 number that the target int can hold while leaving one
11079 bit for the sign. */
11080 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
11081 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
11083 /* Dereference the quo pointer argument. */
11084 arg_quo = build_fold_indirect_ref (arg_quo);
11085 /* Proceed iff a valid pointer type was passed in. */
11086 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
11088 /* Set the value. */
11089 tree result_quo
11090 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
11091 build_int_cst (TREE_TYPE (arg_quo),
11092 integer_quo));
11093 TREE_SIDE_EFFECTS (result_quo) = 1;
11094 /* Combine the quo assignment with the rem. */
11095 result = fold_build2 (COMPOUND_EXPR, type,
11096 result_quo, result_rem);
11097 suppress_warning (result, OPT_Wunused_value);
11098 result = non_lvalue (result);
11103 return result;
11106 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
11107 resulting value as a tree with type TYPE. The mpfr precision is
11108 set to the precision of TYPE. We assume that this mpfr function
11109 returns zero if the result could be calculated exactly within the
11110 requested precision. In addition, the integer pointer represented
11111 by ARG_SG will be dereferenced and set to the appropriate signgam
11112 (-1,1) value. */
11114 static tree
11115 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
11117 tree result = NULL_TREE;
11119 STRIP_NOPS (arg);
11121 /* To proceed, MPFR must exactly represent the target floating point
11122 format, which only happens when the target base equals two. Also
11123 verify ARG is a constant and that ARG_SG is an int pointer. */
11124 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11125 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
11126 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
11127 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
11129 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
11131 /* In addition to NaN and Inf, the argument cannot be zero or a
11132 negative integer. */
11133 if (real_isfinite (ra)
11134 && ra->cl != rvc_zero
11135 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
11137 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11138 const int prec = fmt->p;
11139 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
11140 int inexact, sg;
11141 tree result_lg;
11143 auto_mpfr m (prec);
11144 mpfr_from_real (m, ra, MPFR_RNDN);
11145 mpfr_clear_flags ();
11146 inexact = mpfr_lgamma (m, &sg, m, rnd);
11147 result_lg = do_mpfr_ckconv (m, type, inexact);
11148 if (result_lg)
11150 tree result_sg;
11152 /* Dereference the arg_sg pointer argument. */
11153 arg_sg = build_fold_indirect_ref (arg_sg);
11154 /* Assign the signgam value into *arg_sg. */
11155 result_sg = fold_build2 (MODIFY_EXPR,
11156 TREE_TYPE (arg_sg), arg_sg,
11157 build_int_cst (TREE_TYPE (arg_sg), sg));
11158 TREE_SIDE_EFFECTS (result_sg) = 1;
11159 /* Combine the signgam assignment with the lgamma result. */
11160 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11161 result_sg, result_lg));
11166 return result;
11169 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
11170 mpc function FUNC on it and return the resulting value as a tree
11171 with type TYPE. The mpfr precision is set to the precision of
11172 TYPE. We assume that function FUNC returns zero if the result
11173 could be calculated exactly within the requested precision. If
11174 DO_NONFINITE is true, then fold expressions containing Inf or NaN
11175 in the arguments and/or results. */
11177 tree
11178 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
11179 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
11181 tree result = NULL_TREE;
11183 STRIP_NOPS (arg0);
11184 STRIP_NOPS (arg1);
11186 /* To proceed, MPFR must exactly represent the target floating point
11187 format, which only happens when the target base equals two. */
11188 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
11189 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg0)))
11190 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
11191 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (TREE_TYPE (arg1)))
11192 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
11194 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
11195 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
11196 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
11197 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
11199 if (do_nonfinite
11200 || (real_isfinite (re0) && real_isfinite (im0)
11201 && real_isfinite (re1) && real_isfinite (im1)))
11203 const struct real_format *const fmt =
11204 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
11205 const int prec = fmt->p;
11206 const mpfr_rnd_t rnd = fmt->round_towards_zero
11207 ? MPFR_RNDZ : MPFR_RNDN;
11208 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
11209 int inexact;
11210 mpc_t m0, m1;
11212 mpc_init2 (m0, prec);
11213 mpc_init2 (m1, prec);
11214 mpfr_from_real (mpc_realref (m0), re0, rnd);
11215 mpfr_from_real (mpc_imagref (m0), im0, rnd);
11216 mpfr_from_real (mpc_realref (m1), re1, rnd);
11217 mpfr_from_real (mpc_imagref (m1), im1, rnd);
11218 mpfr_clear_flags ();
11219 inexact = func (m0, m0, m1, crnd);
11220 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
11221 mpc_clear (m0);
11222 mpc_clear (m1);
11226 return result;
11229 /* A wrapper function for builtin folding that prevents warnings for
11230 "statement without effect" and the like, caused by removing the
11231 call node earlier than the warning is generated. */
11233 tree
11234 fold_call_stmt (gcall *stmt, bool ignore)
11236 tree ret = NULL_TREE;
11237 tree fndecl = gimple_call_fndecl (stmt);
11238 location_t loc = gimple_location (stmt);
11239 if (fndecl && fndecl_built_in_p (fndecl)
11240 && !gimple_call_va_arg_pack_p (stmt))
11242 int nargs = gimple_call_num_args (stmt);
11243 tree *args = (nargs > 0
11244 ? gimple_call_arg_ptr (stmt, 0)
11245 : &error_mark_node);
11247 if (avoid_folding_inline_builtin (fndecl))
11248 return NULL_TREE;
11249 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11251 return targetm.fold_builtin (fndecl, nargs, args, ignore);
11253 else
11255 ret = fold_builtin_n (loc, NULL_TREE, fndecl, args, nargs, ignore);
11256 if (ret)
11258 /* Propagate location information from original call to
11259 expansion of builtin. Otherwise things like
11260 maybe_emit_chk_warning, that operate on the expansion
11261 of a builtin, will use the wrong location information. */
11262 if (gimple_has_location (stmt))
11264 tree realret = ret;
11265 if (TREE_CODE (ret) == NOP_EXPR)
11266 realret = TREE_OPERAND (ret, 0);
11267 if (CAN_HAVE_LOCATION_P (realret)
11268 && !EXPR_HAS_LOCATION (realret))
11269 SET_EXPR_LOCATION (realret, loc);
11270 return realret;
11272 return ret;
11276 return NULL_TREE;
11279 /* Look up the function in builtin_decl that corresponds to DECL
11280 and set ASMSPEC as its user assembler name. DECL must be a
11281 function decl that declares a builtin. */
11283 void
11284 set_builtin_user_assembler_name (tree decl, const char *asmspec)
11286 gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL)
11287 && asmspec != 0);
11289 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
11290 set_user_assembler_name (builtin, asmspec);
11292 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
11293 && INT_TYPE_SIZE < BITS_PER_WORD)
11295 scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
11296 set_user_assembler_libfunc ("ffs", asmspec);
11297 set_optab_libfunc (ffs_optab, mode, "ffs");
11301 /* Return true if DECL is a builtin that expands to a constant or similarly
11302 simple code. */
11303 bool
11304 is_simple_builtin (tree decl)
11306 if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
11307 switch (DECL_FUNCTION_CODE (decl))
11309 /* Builtins that expand to constants. */
11310 case BUILT_IN_CONSTANT_P:
11311 case BUILT_IN_EXPECT:
11312 case BUILT_IN_OBJECT_SIZE:
11313 case BUILT_IN_UNREACHABLE:
11314 /* Simple register moves or loads from stack. */
11315 case BUILT_IN_ASSUME_ALIGNED:
11316 case BUILT_IN_RETURN_ADDRESS:
11317 case BUILT_IN_EXTRACT_RETURN_ADDR:
11318 case BUILT_IN_FROB_RETURN_ADDR:
11319 case BUILT_IN_RETURN:
11320 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
11321 case BUILT_IN_FRAME_ADDRESS:
11322 case BUILT_IN_VA_END:
11323 case BUILT_IN_STACK_SAVE:
11324 case BUILT_IN_STACK_RESTORE:
11325 case BUILT_IN_DWARF_CFA:
11326 /* Exception state returns or moves registers around. */
11327 case BUILT_IN_EH_FILTER:
11328 case BUILT_IN_EH_POINTER:
11329 case BUILT_IN_EH_COPY_VALUES:
11330 return true;
11332 default:
11333 return false;
11336 return false;
11339 /* Return true if DECL is a builtin that is not expensive, i.e., they are
11340 most probably expanded inline into reasonably simple code. This is a
11341 superset of is_simple_builtin. */
11342 bool
11343 is_inexpensive_builtin (tree decl)
11345 if (!decl)
11346 return false;
11347 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
11348 return true;
11349 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
11350 switch (DECL_FUNCTION_CODE (decl))
11352 case BUILT_IN_ABS:
11353 CASE_BUILT_IN_ALLOCA:
11354 case BUILT_IN_BSWAP16:
11355 case BUILT_IN_BSWAP32:
11356 case BUILT_IN_BSWAP64:
11357 case BUILT_IN_BSWAP128:
11358 case BUILT_IN_CLZ:
11359 case BUILT_IN_CLZIMAX:
11360 case BUILT_IN_CLZL:
11361 case BUILT_IN_CLZLL:
11362 case BUILT_IN_CTZ:
11363 case BUILT_IN_CTZIMAX:
11364 case BUILT_IN_CTZL:
11365 case BUILT_IN_CTZLL:
11366 case BUILT_IN_FFS:
11367 case BUILT_IN_FFSIMAX:
11368 case BUILT_IN_FFSL:
11369 case BUILT_IN_FFSLL:
11370 case BUILT_IN_IMAXABS:
11371 case BUILT_IN_FINITE:
11372 case BUILT_IN_FINITEF:
11373 case BUILT_IN_FINITEL:
11374 case BUILT_IN_FINITED32:
11375 case BUILT_IN_FINITED64:
11376 case BUILT_IN_FINITED128:
11377 case BUILT_IN_FPCLASSIFY:
11378 case BUILT_IN_ISFINITE:
11379 case BUILT_IN_ISINF_SIGN:
11380 case BUILT_IN_ISINF:
11381 case BUILT_IN_ISINFF:
11382 case BUILT_IN_ISINFL:
11383 case BUILT_IN_ISINFD32:
11384 case BUILT_IN_ISINFD64:
11385 case BUILT_IN_ISINFD128:
11386 case BUILT_IN_ISNAN:
11387 case BUILT_IN_ISNANF:
11388 case BUILT_IN_ISNANL:
11389 case BUILT_IN_ISNAND32:
11390 case BUILT_IN_ISNAND64:
11391 case BUILT_IN_ISNAND128:
11392 case BUILT_IN_ISNORMAL:
11393 case BUILT_IN_ISGREATER:
11394 case BUILT_IN_ISGREATEREQUAL:
11395 case BUILT_IN_ISLESS:
11396 case BUILT_IN_ISLESSEQUAL:
11397 case BUILT_IN_ISLESSGREATER:
11398 case BUILT_IN_ISUNORDERED:
11399 case BUILT_IN_VA_ARG_PACK:
11400 case BUILT_IN_VA_ARG_PACK_LEN:
11401 case BUILT_IN_VA_COPY:
11402 case BUILT_IN_TRAP:
11403 case BUILT_IN_UNREACHABLE_TRAP:
11404 case BUILT_IN_SAVEREGS:
11405 case BUILT_IN_POPCOUNTL:
11406 case BUILT_IN_POPCOUNTLL:
11407 case BUILT_IN_POPCOUNTIMAX:
11408 case BUILT_IN_POPCOUNT:
11409 case BUILT_IN_PARITYL:
11410 case BUILT_IN_PARITYLL:
11411 case BUILT_IN_PARITYIMAX:
11412 case BUILT_IN_PARITY:
11413 case BUILT_IN_LABS:
11414 case BUILT_IN_LLABS:
11415 case BUILT_IN_PREFETCH:
11416 case BUILT_IN_ACC_ON_DEVICE:
11417 return true;
11419 default:
11420 return is_simple_builtin (decl);
11423 return false;
11426 /* Return true if T is a constant and the value cast to a target char
11427 can be represented by a host char.
11428 Store the casted char constant in *P if so. */
11430 bool
11431 target_char_cst_p (tree t, char *p)
11433 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
11434 return false;
11436 *p = (char)tree_to_uhwi (t);
11437 return true;
11440 /* Return true if the builtin DECL is implemented in a standard library.
11441 Otherwise return false which doesn't guarantee it is not (thus the list
11442 of handled builtins below may be incomplete). */
11444 bool
11445 builtin_with_linkage_p (tree decl)
11447 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
11448 switch (DECL_FUNCTION_CODE (decl))
11450 CASE_FLT_FN (BUILT_IN_ACOS):
11451 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
11452 CASE_FLT_FN (BUILT_IN_ACOSH):
11453 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH):
11454 CASE_FLT_FN (BUILT_IN_ASIN):
11455 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
11456 CASE_FLT_FN (BUILT_IN_ASINH):
11457 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINH):
11458 CASE_FLT_FN (BUILT_IN_ATAN):
11459 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN):
11460 CASE_FLT_FN (BUILT_IN_ATANH):
11461 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH):
11462 CASE_FLT_FN (BUILT_IN_ATAN2):
11463 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN2):
11464 CASE_FLT_FN (BUILT_IN_CBRT):
11465 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CBRT):
11466 CASE_FLT_FN (BUILT_IN_CEIL):
11467 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
11468 CASE_FLT_FN (BUILT_IN_COPYSIGN):
11469 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
11470 CASE_FLT_FN (BUILT_IN_COS):
11471 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COS):
11472 CASE_FLT_FN (BUILT_IN_COSH):
11473 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSH):
11474 CASE_FLT_FN (BUILT_IN_ERF):
11475 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERF):
11476 CASE_FLT_FN (BUILT_IN_ERFC):
11477 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERFC):
11478 CASE_FLT_FN (BUILT_IN_EXP):
11479 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP):
11480 CASE_FLT_FN (BUILT_IN_EXP2):
11481 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP2):
11482 CASE_FLT_FN (BUILT_IN_EXPM1):
11483 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXPM1):
11484 CASE_FLT_FN (BUILT_IN_FABS):
11485 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
11486 CASE_FLT_FN (BUILT_IN_FDIM):
11487 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FDIM):
11488 CASE_FLT_FN (BUILT_IN_FLOOR):
11489 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
11490 CASE_FLT_FN (BUILT_IN_FMA):
11491 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
11492 CASE_FLT_FN (BUILT_IN_FMAX):
11493 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
11494 CASE_FLT_FN (BUILT_IN_FMIN):
11495 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
11496 CASE_FLT_FN (BUILT_IN_FMOD):
11497 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMOD):
11498 CASE_FLT_FN (BUILT_IN_FREXP):
11499 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FREXP):
11500 CASE_FLT_FN (BUILT_IN_HYPOT):
11501 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HYPOT):
11502 CASE_FLT_FN (BUILT_IN_ILOGB):
11503 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ILOGB):
11504 CASE_FLT_FN (BUILT_IN_LDEXP):
11505 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LDEXP):
11506 CASE_FLT_FN (BUILT_IN_LGAMMA):
11507 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LGAMMA):
11508 CASE_FLT_FN (BUILT_IN_LLRINT):
11509 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLRINT):
11510 CASE_FLT_FN (BUILT_IN_LLROUND):
11511 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLROUND):
11512 CASE_FLT_FN (BUILT_IN_LOG):
11513 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG):
11514 CASE_FLT_FN (BUILT_IN_LOG10):
11515 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10):
11516 CASE_FLT_FN (BUILT_IN_LOG1P):
11517 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P):
11518 CASE_FLT_FN (BUILT_IN_LOG2):
11519 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2):
11520 CASE_FLT_FN (BUILT_IN_LOGB):
11521 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOGB):
11522 CASE_FLT_FN (BUILT_IN_LRINT):
11523 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LRINT):
11524 CASE_FLT_FN (BUILT_IN_LROUND):
11525 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LROUND):
11526 CASE_FLT_FN (BUILT_IN_MODF):
11527 CASE_FLT_FN_FLOATN_NX (BUILT_IN_MODF):
11528 CASE_FLT_FN (BUILT_IN_NAN):
11529 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NAN):
11530 CASE_FLT_FN (BUILT_IN_NEARBYINT):
11531 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
11532 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
11533 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEXTAFTER):
11534 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
11535 CASE_FLT_FN (BUILT_IN_POW):
11536 CASE_FLT_FN_FLOATN_NX (BUILT_IN_POW):
11537 CASE_FLT_FN (BUILT_IN_REMAINDER):
11538 CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMAINDER):
11539 CASE_FLT_FN (BUILT_IN_REMQUO):
11540 CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMQUO):
11541 CASE_FLT_FN (BUILT_IN_RINT):
11542 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
11543 CASE_FLT_FN (BUILT_IN_ROUND):
11544 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
11545 CASE_FLT_FN (BUILT_IN_SCALBLN):
11546 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBLN):
11547 CASE_FLT_FN (BUILT_IN_SCALBN):
11548 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBN):
11549 CASE_FLT_FN (BUILT_IN_SIN):
11550 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SIN):
11551 CASE_FLT_FN (BUILT_IN_SINH):
11552 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINH):
11553 CASE_FLT_FN (BUILT_IN_SINCOS):
11554 CASE_FLT_FN (BUILT_IN_SQRT):
11555 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
11556 CASE_FLT_FN (BUILT_IN_TAN):
11557 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TAN):
11558 CASE_FLT_FN (BUILT_IN_TANH):
11559 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TANH):
11560 CASE_FLT_FN (BUILT_IN_TGAMMA):
11561 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TGAMMA):
11562 CASE_FLT_FN (BUILT_IN_TRUNC):
11563 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
11564 return true;
11566 case BUILT_IN_STPCPY:
11567 case BUILT_IN_STPNCPY:
11568 /* stpcpy is both referenced in libiberty's pex-win32.c and provided
11569 by libiberty's stpcpy.c for MinGW targets so we need to return true
11570 in order to be able to build libiberty in LTO mode for them. */
11571 return true;
11573 default:
11574 break;
11576 return false;
11579 /* Return true if OFFRNG is bounded to a subrange of offset values
11580 valid for the largest possible object. */
11582 bool
11583 access_ref::offset_bounded () const
11585 tree min = TYPE_MIN_VALUE (ptrdiff_type_node);
11586 tree max = TYPE_MAX_VALUE (ptrdiff_type_node);
11587 return wi::to_offset (min) <= offrng[0] && offrng[1] <= wi::to_offset (max);
11590 /* If CALLEE has known side effects, fill in INFO and return true.
11591 See tree-ssa-structalias.cc:find_func_aliases
11592 for the list of builtins we might need to handle here. */
11594 attr_fnspec
11595 builtin_fnspec (tree callee)
11597 built_in_function code = DECL_FUNCTION_CODE (callee);
11599 switch (code)
11601 /* All the following functions read memory pointed to by
11602 their second argument and write memory pointed to by first
11603 argument.
11604 strcat/strncat additionally reads memory pointed to by the first
11605 argument. */
11606 case BUILT_IN_STRCAT:
11607 case BUILT_IN_STRCAT_CHK:
11608 return "1cW 1 ";
11609 case BUILT_IN_STRNCAT:
11610 case BUILT_IN_STRNCAT_CHK:
11611 return "1cW 13";
11612 case BUILT_IN_STRCPY:
11613 case BUILT_IN_STRCPY_CHK:
11614 return "1cO 1 ";
11615 case BUILT_IN_STPCPY:
11616 case BUILT_IN_STPCPY_CHK:
11617 return ".cO 1 ";
11618 case BUILT_IN_STRNCPY:
11619 case BUILT_IN_MEMCPY:
11620 case BUILT_IN_MEMMOVE:
11621 case BUILT_IN_TM_MEMCPY:
11622 case BUILT_IN_TM_MEMMOVE:
11623 case BUILT_IN_STRNCPY_CHK:
11624 case BUILT_IN_MEMCPY_CHK:
11625 case BUILT_IN_MEMMOVE_CHK:
11626 return "1cO313";
11627 case BUILT_IN_MEMPCPY:
11628 case BUILT_IN_MEMPCPY_CHK:
11629 return ".cO313";
11630 case BUILT_IN_STPNCPY:
11631 case BUILT_IN_STPNCPY_CHK:
11632 return ".cO313";
11633 case BUILT_IN_BCOPY:
11634 return ".c23O3";
11635 case BUILT_IN_BZERO:
11636 return ".cO2";
11637 case BUILT_IN_MEMCMP:
11638 case BUILT_IN_MEMCMP_EQ:
11639 case BUILT_IN_BCMP:
11640 case BUILT_IN_STRNCMP:
11641 case BUILT_IN_STRNCMP_EQ:
11642 case BUILT_IN_STRNCASECMP:
11643 return ".cR3R3";
11645 /* The following functions read memory pointed to by their
11646 first argument. */
11647 CASE_BUILT_IN_TM_LOAD (1):
11648 CASE_BUILT_IN_TM_LOAD (2):
11649 CASE_BUILT_IN_TM_LOAD (4):
11650 CASE_BUILT_IN_TM_LOAD (8):
11651 CASE_BUILT_IN_TM_LOAD (FLOAT):
11652 CASE_BUILT_IN_TM_LOAD (DOUBLE):
11653 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
11654 CASE_BUILT_IN_TM_LOAD (M64):
11655 CASE_BUILT_IN_TM_LOAD (M128):
11656 CASE_BUILT_IN_TM_LOAD (M256):
11657 case BUILT_IN_TM_LOG:
11658 case BUILT_IN_TM_LOG_1:
11659 case BUILT_IN_TM_LOG_2:
11660 case BUILT_IN_TM_LOG_4:
11661 case BUILT_IN_TM_LOG_8:
11662 case BUILT_IN_TM_LOG_FLOAT:
11663 case BUILT_IN_TM_LOG_DOUBLE:
11664 case BUILT_IN_TM_LOG_LDOUBLE:
11665 case BUILT_IN_TM_LOG_M64:
11666 case BUILT_IN_TM_LOG_M128:
11667 case BUILT_IN_TM_LOG_M256:
11668 return ".cR ";
11670 case BUILT_IN_INDEX:
11671 case BUILT_IN_RINDEX:
11672 case BUILT_IN_STRCHR:
11673 case BUILT_IN_STRLEN:
11674 case BUILT_IN_STRRCHR:
11675 return ".cR ";
11676 case BUILT_IN_STRNLEN:
11677 return ".cR2";
11679 /* These read memory pointed to by the first argument.
11680 Allocating memory does not have any side-effects apart from
11681 being the definition point for the pointer.
11682 Unix98 specifies that errno is set on allocation failure. */
11683 case BUILT_IN_STRDUP:
11684 return "mCR ";
11685 case BUILT_IN_STRNDUP:
11686 return "mCR2";
11687 /* Allocating memory does not have any side-effects apart from
11688 being the definition point for the pointer. */
11689 case BUILT_IN_MALLOC:
11690 case BUILT_IN_ALIGNED_ALLOC:
11691 case BUILT_IN_CALLOC:
11692 case BUILT_IN_GOMP_ALLOC:
11693 return "mC";
11694 CASE_BUILT_IN_ALLOCA:
11695 return "mc";
11696 /* These read memory pointed to by the first argument with size
11697 in the third argument. */
11698 case BUILT_IN_MEMCHR:
11699 return ".cR3";
11700 /* These read memory pointed to by the first and second arguments. */
11701 case BUILT_IN_STRSTR:
11702 case BUILT_IN_STRPBRK:
11703 case BUILT_IN_STRCASECMP:
11704 case BUILT_IN_STRCSPN:
11705 case BUILT_IN_STRSPN:
11706 case BUILT_IN_STRCMP:
11707 case BUILT_IN_STRCMP_EQ:
11708 return ".cR R ";
11709 /* Freeing memory kills the pointed-to memory. More importantly
11710 the call has to serve as a barrier for moving loads and stores
11711 across it. */
11712 case BUILT_IN_STACK_RESTORE:
11713 case BUILT_IN_FREE:
11714 case BUILT_IN_GOMP_FREE:
11715 return ".co ";
11716 case BUILT_IN_VA_END:
11717 return ".cO ";
11718 /* Realloc serves both as allocation point and deallocation point. */
11719 case BUILT_IN_REALLOC:
11720 return ".Cw ";
11721 case BUILT_IN_GAMMA_R:
11722 case BUILT_IN_GAMMAF_R:
11723 case BUILT_IN_GAMMAL_R:
11724 case BUILT_IN_LGAMMA_R:
11725 case BUILT_IN_LGAMMAF_R:
11726 case BUILT_IN_LGAMMAL_R:
11727 return ".C. Ot";
11728 case BUILT_IN_FREXP:
11729 case BUILT_IN_FREXPF:
11730 case BUILT_IN_FREXPL:
11731 case BUILT_IN_MODF:
11732 case BUILT_IN_MODFF:
11733 case BUILT_IN_MODFL:
11734 return ".c. Ot";
11735 case BUILT_IN_REMQUO:
11736 case BUILT_IN_REMQUOF:
11737 case BUILT_IN_REMQUOL:
11738 return ".c. . Ot";
11739 case BUILT_IN_SINCOS:
11740 case BUILT_IN_SINCOSF:
11741 case BUILT_IN_SINCOSL:
11742 return ".c. OtOt";
11743 case BUILT_IN_MEMSET:
11744 case BUILT_IN_MEMSET_CHK:
11745 case BUILT_IN_TM_MEMSET:
11746 return "1cO3";
11747 CASE_BUILT_IN_TM_STORE (1):
11748 CASE_BUILT_IN_TM_STORE (2):
11749 CASE_BUILT_IN_TM_STORE (4):
11750 CASE_BUILT_IN_TM_STORE (8):
11751 CASE_BUILT_IN_TM_STORE (FLOAT):
11752 CASE_BUILT_IN_TM_STORE (DOUBLE):
11753 CASE_BUILT_IN_TM_STORE (LDOUBLE):
11754 CASE_BUILT_IN_TM_STORE (M64):
11755 CASE_BUILT_IN_TM_STORE (M128):
11756 CASE_BUILT_IN_TM_STORE (M256):
11757 return ".cO ";
11758 case BUILT_IN_STACK_SAVE:
11759 case BUILT_IN_RETURN:
11760 case BUILT_IN_EH_POINTER:
11761 case BUILT_IN_EH_FILTER:
11762 case BUILT_IN_UNWIND_RESUME:
11763 case BUILT_IN_CXA_END_CLEANUP:
11764 case BUILT_IN_EH_COPY_VALUES:
11765 case BUILT_IN_FRAME_ADDRESS:
11766 case BUILT_IN_APPLY_ARGS:
11767 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
11768 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
11769 case BUILT_IN_PREFETCH:
11770 case BUILT_IN_DWARF_CFA:
11771 case BUILT_IN_RETURN_ADDRESS:
11772 return ".c";
11773 case BUILT_IN_ASSUME_ALIGNED:
11774 case BUILT_IN_EXPECT:
11775 case BUILT_IN_EXPECT_WITH_PROBABILITY:
11776 return "1cX ";
11777 /* But posix_memalign stores a pointer into the memory pointed to
11778 by its first argument. */
11779 case BUILT_IN_POSIX_MEMALIGN:
11780 return ".cOt";
11782 default:
11783 return "";