d: Add test for PR d/108167 to the testsuite [PR108167]
[official-gcc.git] / gcc / builtins.cc
blob305c65c29be58c838e6cdb4918aedb6096d2c38a
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 gcc_assert (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
1459 apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1462 return size;
1465 /* Return the size required for the block returned by __builtin_apply,
1466 and initialize apply_result_mode. */
1468 static int
1469 apply_result_size (void)
1471 static int size = -1;
1472 int align, regno;
1474 /* The values computed by this function never change. */
1475 if (size < 0)
1477 size = 0;
1479 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1480 if (targetm.calls.function_value_regno_p (regno))
1482 fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1484 gcc_assert (mode != VOIDmode);
1486 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1487 if (size % align != 0)
1488 size = CEIL (size, align) * align;
1489 size += GET_MODE_SIZE (mode);
1490 apply_result_mode[regno] = mode;
1492 else
1493 apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1495 /* Allow targets that use untyped_call and untyped_return to override
1496 the size so that machine-specific information can be stored here. */
1497 #ifdef APPLY_RESULT_SIZE
1498 size = APPLY_RESULT_SIZE;
1499 #endif
1501 return size;
1504 /* Create a vector describing the result block RESULT. If SAVEP is true,
1505 the result block is used to save the values; otherwise it is used to
1506 restore the values. */
1508 static rtx
1509 result_vector (int savep, rtx result)
1511 int regno, size, align, nelts;
1512 fixed_size_mode mode;
1513 rtx reg, mem;
1514 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1516 size = nelts = 0;
1517 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1518 if ((mode = apply_result_mode[regno]) != VOIDmode)
1520 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1521 if (size % align != 0)
1522 size = CEIL (size, align) * align;
1523 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1524 mem = adjust_address (result, mode, size);
1525 savevec[nelts++] = (savep
1526 ? gen_rtx_SET (mem, reg)
1527 : gen_rtx_SET (reg, mem));
1528 size += GET_MODE_SIZE (mode);
1530 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1533 /* Save the state required to perform an untyped call with the same
1534 arguments as were passed to the current function. */
1536 static rtx
1537 expand_builtin_apply_args_1 (void)
1539 rtx registers, tem;
1540 int size, align, regno;
1541 fixed_size_mode mode;
1542 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1544 /* Create a block where the arg-pointer, structure value address,
1545 and argument registers can be saved. */
1546 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1548 /* Walk past the arg-pointer and structure value address. */
1549 size = GET_MODE_SIZE (Pmode);
1550 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1551 size += GET_MODE_SIZE (Pmode);
1553 /* Save each register used in calling a function to the block. */
1554 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1555 if ((mode = apply_args_mode[regno]) != VOIDmode)
1557 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1558 if (size % align != 0)
1559 size = CEIL (size, align) * align;
1561 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1563 emit_move_insn (adjust_address (registers, mode, size), tem);
1564 size += GET_MODE_SIZE (mode);
1567 /* Save the arg pointer to the block. */
1568 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1569 /* We need the pointer as the caller actually passed them to us, not
1570 as we might have pretended they were passed. Make sure it's a valid
1571 operand, as emit_move_insn isn't expected to handle a PLUS. */
1572 if (STACK_GROWS_DOWNWARD)
1574 = force_operand (plus_constant (Pmode, tem,
1575 crtl->args.pretend_args_size),
1576 NULL_RTX);
1577 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1579 size = GET_MODE_SIZE (Pmode);
1581 /* Save the structure value address unless this is passed as an
1582 "invisible" first argument. */
1583 if (struct_incoming_value)
1584 emit_move_insn (adjust_address (registers, Pmode, size),
1585 copy_to_reg (struct_incoming_value));
1587 /* Return the address of the block. */
1588 return copy_addr_to_reg (XEXP (registers, 0));
1591 /* __builtin_apply_args returns block of memory allocated on
1592 the stack into which is stored the arg pointer, structure
1593 value address, static chain, and all the registers that might
1594 possibly be used in performing a function call. The code is
1595 moved to the start of the function so the incoming values are
1596 saved. */
1598 static rtx
1599 expand_builtin_apply_args (void)
1601 /* Don't do __builtin_apply_args more than once in a function.
1602 Save the result of the first call and reuse it. */
1603 if (apply_args_value != 0)
1604 return apply_args_value;
1606 /* When this function is called, it means that registers must be
1607 saved on entry to this function. So we migrate the
1608 call to the first insn of this function. */
1609 rtx temp;
1611 start_sequence ();
1612 temp = expand_builtin_apply_args_1 ();
1613 rtx_insn *seq = get_insns ();
1614 end_sequence ();
1616 apply_args_value = temp;
1618 /* Put the insns after the NOTE that starts the function.
1619 If this is inside a start_sequence, make the outer-level insn
1620 chain current, so the code is placed at the start of the
1621 function. If internal_arg_pointer is a non-virtual pseudo,
1622 it needs to be placed after the function that initializes
1623 that pseudo. */
1624 push_topmost_sequence ();
1625 if (REG_P (crtl->args.internal_arg_pointer)
1626 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1627 emit_insn_before (seq, parm_birth_insn);
1628 else
1629 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1630 pop_topmost_sequence ();
1631 return temp;
1635 /* Perform an untyped call and save the state required to perform an
1636 untyped return of whatever value was returned by the given function. */
1638 static rtx
1639 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1641 int size, align, regno;
1642 fixed_size_mode mode;
1643 rtx incoming_args, result, reg, dest, src;
1644 rtx_call_insn *call_insn;
1645 rtx old_stack_level = 0;
1646 rtx call_fusage = 0;
1647 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1649 arguments = convert_memory_address (Pmode, arguments);
1651 /* Create a block where the return registers can be saved. */
1652 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1654 /* Fetch the arg pointer from the ARGUMENTS block. */
1655 incoming_args = gen_reg_rtx (Pmode);
1656 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1657 if (!STACK_GROWS_DOWNWARD)
1658 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1659 incoming_args, 0, OPTAB_LIB_WIDEN);
1661 /* Push a new argument block and copy the arguments. Do not allow
1662 the (potential) memcpy call below to interfere with our stack
1663 manipulations. */
1664 do_pending_stack_adjust ();
1665 NO_DEFER_POP;
1667 /* Save the stack with nonlocal if available. */
1668 if (targetm.have_save_stack_nonlocal ())
1669 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1670 else
1671 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1673 /* Allocate a block of memory onto the stack and copy the memory
1674 arguments to the outgoing arguments address. We can pass TRUE
1675 as the 4th argument because we just saved the stack pointer
1676 and will restore it right after the call. */
1677 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1679 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1680 may have already set current_function_calls_alloca to true.
1681 current_function_calls_alloca won't be set if argsize is zero,
1682 so we have to guarantee need_drap is true here. */
1683 if (SUPPORTS_STACK_ALIGNMENT)
1684 crtl->need_drap = true;
1686 dest = virtual_outgoing_args_rtx;
1687 if (!STACK_GROWS_DOWNWARD)
1689 if (CONST_INT_P (argsize))
1690 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1691 else
1692 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1694 dest = gen_rtx_MEM (BLKmode, dest);
1695 set_mem_align (dest, PARM_BOUNDARY);
1696 src = gen_rtx_MEM (BLKmode, incoming_args);
1697 set_mem_align (src, PARM_BOUNDARY);
1698 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1700 /* Refer to the argument block. */
1701 apply_args_size ();
1702 arguments = gen_rtx_MEM (BLKmode, arguments);
1703 set_mem_align (arguments, PARM_BOUNDARY);
1705 /* Walk past the arg-pointer and structure value address. */
1706 size = GET_MODE_SIZE (Pmode);
1707 if (struct_value)
1708 size += GET_MODE_SIZE (Pmode);
1710 /* Restore each of the registers previously saved. Make USE insns
1711 for each of these registers for use in making the call. */
1712 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1713 if ((mode = apply_args_mode[regno]) != VOIDmode)
1715 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1716 if (size % align != 0)
1717 size = CEIL (size, align) * align;
1718 reg = gen_rtx_REG (mode, regno);
1719 emit_move_insn (reg, adjust_address (arguments, mode, size));
1720 use_reg (&call_fusage, reg);
1721 size += GET_MODE_SIZE (mode);
1724 /* Restore the structure value address unless this is passed as an
1725 "invisible" first argument. */
1726 size = GET_MODE_SIZE (Pmode);
1727 if (struct_value)
1729 rtx value = gen_reg_rtx (Pmode);
1730 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1731 emit_move_insn (struct_value, value);
1732 if (REG_P (struct_value))
1733 use_reg (&call_fusage, struct_value);
1736 /* All arguments and registers used for the call are set up by now! */
1737 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1739 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1740 and we don't want to load it into a register as an optimization,
1741 because prepare_call_address already did it if it should be done. */
1742 if (GET_CODE (function) != SYMBOL_REF)
1743 function = memory_address (FUNCTION_MODE, function);
1745 /* Generate the actual call instruction and save the return value. */
1746 if (targetm.have_untyped_call ())
1748 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1749 rtx_insn *seq = targetm.gen_untyped_call (mem, result,
1750 result_vector (1, result));
1751 for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
1752 if (CALL_P (insn))
1753 add_reg_note (insn, REG_UNTYPED_CALL, NULL_RTX);
1754 emit_insn (seq);
1756 else if (targetm.have_call_value ())
1758 rtx valreg = 0;
1760 /* Locate the unique return register. It is not possible to
1761 express a call that sets more than one return register using
1762 call_value; use untyped_call for that. In fact, untyped_call
1763 only needs to save the return registers in the given block. */
1764 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1765 if ((mode = apply_result_mode[regno]) != VOIDmode)
1767 gcc_assert (!valreg); /* have_untyped_call required. */
1769 valreg = gen_rtx_REG (mode, regno);
1772 emit_insn (targetm.gen_call_value (valreg,
1773 gen_rtx_MEM (FUNCTION_MODE, function),
1774 const0_rtx, NULL_RTX, const0_rtx));
1776 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1778 else
1779 gcc_unreachable ();
1781 /* Find the CALL insn we just emitted, and attach the register usage
1782 information. */
1783 call_insn = last_call_insn ();
1784 add_function_usage_to (call_insn, call_fusage);
1786 /* Restore the stack. */
1787 if (targetm.have_save_stack_nonlocal ())
1788 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1789 else
1790 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1791 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1793 OK_DEFER_POP;
1795 /* Return the address of the result block. */
1796 result = copy_addr_to_reg (XEXP (result, 0));
1797 return convert_memory_address (ptr_mode, result);
1800 /* Perform an untyped return. */
1802 static void
1803 expand_builtin_return (rtx result)
1805 int size, align, regno;
1806 fixed_size_mode mode;
1807 rtx reg;
1808 rtx_insn *call_fusage = 0;
1810 result = convert_memory_address (Pmode, result);
1812 apply_result_size ();
1813 result = gen_rtx_MEM (BLKmode, result);
1815 if (targetm.have_untyped_return ())
1817 rtx vector = result_vector (0, result);
1818 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1819 emit_barrier ();
1820 return;
1823 /* Restore the return value and note that each value is used. */
1824 size = 0;
1825 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1826 if ((mode = apply_result_mode[regno]) != VOIDmode)
1828 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1829 if (size % align != 0)
1830 size = CEIL (size, align) * align;
1831 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1832 emit_move_insn (reg, adjust_address (result, mode, size));
1834 push_to_sequence (call_fusage);
1835 emit_use (reg);
1836 call_fusage = get_insns ();
1837 end_sequence ();
1838 size += GET_MODE_SIZE (mode);
1841 /* Put the USE insns before the return. */
1842 emit_insn (call_fusage);
1844 /* Return whatever values was restored by jumping directly to the end
1845 of the function. */
1846 expand_naked_return ();
1849 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1851 static enum type_class
1852 type_to_class (tree type)
1854 switch (TREE_CODE (type))
1856 case VOID_TYPE: return void_type_class;
1857 case INTEGER_TYPE: return integer_type_class;
1858 case ENUMERAL_TYPE: return enumeral_type_class;
1859 case BOOLEAN_TYPE: return boolean_type_class;
1860 case POINTER_TYPE: return pointer_type_class;
1861 case REFERENCE_TYPE: return reference_type_class;
1862 case OFFSET_TYPE: return offset_type_class;
1863 case REAL_TYPE: return real_type_class;
1864 case COMPLEX_TYPE: return complex_type_class;
1865 case FUNCTION_TYPE: return function_type_class;
1866 case METHOD_TYPE: return method_type_class;
1867 case RECORD_TYPE: return record_type_class;
1868 case UNION_TYPE:
1869 case QUAL_UNION_TYPE: return union_type_class;
1870 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1871 ? string_type_class : array_type_class);
1872 case LANG_TYPE: return lang_type_class;
1873 case OPAQUE_TYPE: return opaque_type_class;
1874 default: return no_type_class;
1878 /* Expand a call EXP to __builtin_classify_type. */
1880 static rtx
1881 expand_builtin_classify_type (tree exp)
1883 if (call_expr_nargs (exp))
1884 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1885 return GEN_INT (no_type_class);
1888 /* This helper macro, meant to be used in mathfn_built_in below, determines
1889 which among a set of builtin math functions is appropriate for a given type
1890 mode. The `F' (float) and `L' (long double) are automatically generated
1891 from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1892 types, there are additional types that are considered with 'F32', 'F64',
1893 'F128', etc. suffixes. */
1894 #define CASE_MATHFN(MATHFN) \
1895 CASE_CFN_##MATHFN: \
1896 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1897 fcodel = BUILT_IN_##MATHFN##L ; break;
1898 /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1899 types. */
1900 #define CASE_MATHFN_FLOATN(MATHFN) \
1901 CASE_CFN_##MATHFN: \
1902 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1903 fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1904 fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1905 fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1906 fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1907 break;
1908 /* Similar to above, but appends _R after any F/L suffix. */
1909 #define CASE_MATHFN_REENT(MATHFN) \
1910 case CFN_BUILT_IN_##MATHFN##_R: \
1911 case CFN_BUILT_IN_##MATHFN##F_R: \
1912 case CFN_BUILT_IN_##MATHFN##L_R: \
1913 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1914 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1916 /* Return a function equivalent to FN but operating on floating-point
1917 values of type TYPE, or END_BUILTINS if no such function exists.
1918 This is purely an operation on function codes; it does not guarantee
1919 that the target actually has an implementation of the function. */
1921 static built_in_function
1922 mathfn_built_in_2 (tree type, combined_fn fn)
1924 tree mtype;
1925 built_in_function fcode, fcodef, fcodel;
1926 built_in_function fcodef16 = END_BUILTINS;
1927 built_in_function fcodef32 = END_BUILTINS;
1928 built_in_function fcodef64 = END_BUILTINS;
1929 built_in_function fcodef128 = END_BUILTINS;
1930 built_in_function fcodef32x = END_BUILTINS;
1931 built_in_function fcodef64x = END_BUILTINS;
1932 built_in_function fcodef128x = END_BUILTINS;
1934 /* If <math.h> has been included somehow, HUGE_VAL and NAN definitions
1935 break the uses below. */
1936 #undef HUGE_VAL
1937 #undef NAN
1939 switch (fn)
1941 #define SEQ_OF_CASE_MATHFN \
1942 CASE_MATHFN_FLOATN (ACOS) \
1943 CASE_MATHFN_FLOATN (ACOSH) \
1944 CASE_MATHFN_FLOATN (ASIN) \
1945 CASE_MATHFN_FLOATN (ASINH) \
1946 CASE_MATHFN_FLOATN (ATAN) \
1947 CASE_MATHFN_FLOATN (ATAN2) \
1948 CASE_MATHFN_FLOATN (ATANH) \
1949 CASE_MATHFN_FLOATN (CBRT) \
1950 CASE_MATHFN_FLOATN (CEIL) \
1951 CASE_MATHFN (CEXPI) \
1952 CASE_MATHFN_FLOATN (COPYSIGN) \
1953 CASE_MATHFN_FLOATN (COS) \
1954 CASE_MATHFN_FLOATN (COSH) \
1955 CASE_MATHFN (DREM) \
1956 CASE_MATHFN_FLOATN (ERF) \
1957 CASE_MATHFN_FLOATN (ERFC) \
1958 CASE_MATHFN_FLOATN (EXP) \
1959 CASE_MATHFN (EXP10) \
1960 CASE_MATHFN_FLOATN (EXP2) \
1961 CASE_MATHFN_FLOATN (EXPM1) \
1962 CASE_MATHFN_FLOATN (FABS) \
1963 CASE_MATHFN_FLOATN (FDIM) \
1964 CASE_MATHFN_FLOATN (FLOOR) \
1965 CASE_MATHFN_FLOATN (FMA) \
1966 CASE_MATHFN_FLOATN (FMAX) \
1967 CASE_MATHFN_FLOATN (FMIN) \
1968 CASE_MATHFN_FLOATN (FMOD) \
1969 CASE_MATHFN_FLOATN (FREXP) \
1970 CASE_MATHFN (GAMMA) \
1971 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ \
1972 CASE_MATHFN_FLOATN (HUGE_VAL) \
1973 CASE_MATHFN_FLOATN (HYPOT) \
1974 CASE_MATHFN_FLOATN (ILOGB) \
1975 CASE_MATHFN (ICEIL) \
1976 CASE_MATHFN (IFLOOR) \
1977 CASE_MATHFN_FLOATN (INF) \
1978 CASE_MATHFN (IRINT) \
1979 CASE_MATHFN (IROUND) \
1980 CASE_MATHFN (ISINF) \
1981 CASE_MATHFN (J0) \
1982 CASE_MATHFN (J1) \
1983 CASE_MATHFN (JN) \
1984 CASE_MATHFN (LCEIL) \
1985 CASE_MATHFN_FLOATN (LDEXP) \
1986 CASE_MATHFN (LFLOOR) \
1987 CASE_MATHFN_FLOATN (LGAMMA) \
1988 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ \
1989 CASE_MATHFN (LLCEIL) \
1990 CASE_MATHFN (LLFLOOR) \
1991 CASE_MATHFN_FLOATN (LLRINT) \
1992 CASE_MATHFN_FLOATN (LLROUND) \
1993 CASE_MATHFN_FLOATN (LOG) \
1994 CASE_MATHFN_FLOATN (LOG10) \
1995 CASE_MATHFN_FLOATN (LOG1P) \
1996 CASE_MATHFN_FLOATN (LOG2) \
1997 CASE_MATHFN_FLOATN (LOGB) \
1998 CASE_MATHFN_FLOATN (LRINT) \
1999 CASE_MATHFN_FLOATN (LROUND) \
2000 CASE_MATHFN_FLOATN (MODF) \
2001 CASE_MATHFN_FLOATN (NAN) \
2002 CASE_MATHFN_FLOATN (NANS) \
2003 CASE_MATHFN_FLOATN (NEARBYINT) \
2004 CASE_MATHFN_FLOATN (NEXTAFTER) \
2005 CASE_MATHFN (NEXTTOWARD) \
2006 CASE_MATHFN_FLOATN (POW) \
2007 CASE_MATHFN (POWI) \
2008 CASE_MATHFN (POW10) \
2009 CASE_MATHFN_FLOATN (REMAINDER) \
2010 CASE_MATHFN_FLOATN (REMQUO) \
2011 CASE_MATHFN_FLOATN (RINT) \
2012 CASE_MATHFN_FLOATN (ROUND) \
2013 CASE_MATHFN_FLOATN (ROUNDEVEN) \
2014 CASE_MATHFN (SCALB) \
2015 CASE_MATHFN_FLOATN (SCALBLN) \
2016 CASE_MATHFN_FLOATN (SCALBN) \
2017 CASE_MATHFN (SIGNBIT) \
2018 CASE_MATHFN (SIGNIFICAND) \
2019 CASE_MATHFN_FLOATN (SIN) \
2020 CASE_MATHFN (SINCOS) \
2021 CASE_MATHFN_FLOATN (SINH) \
2022 CASE_MATHFN_FLOATN (SQRT) \
2023 CASE_MATHFN_FLOATN (TAN) \
2024 CASE_MATHFN_FLOATN (TANH) \
2025 CASE_MATHFN_FLOATN (TGAMMA) \
2026 CASE_MATHFN_FLOATN (TRUNC) \
2027 CASE_MATHFN (Y0) \
2028 CASE_MATHFN (Y1) \
2029 CASE_MATHFN (YN)
2031 SEQ_OF_CASE_MATHFN
2033 default:
2034 return END_BUILTINS;
2037 mtype = TYPE_MAIN_VARIANT (type);
2038 if (mtype == double_type_node)
2039 return fcode;
2040 else if (mtype == float_type_node)
2041 return fcodef;
2042 else if (mtype == long_double_type_node)
2043 return fcodel;
2044 else if (mtype == float16_type_node)
2045 return fcodef16;
2046 else if (mtype == float32_type_node)
2047 return fcodef32;
2048 else if (mtype == float64_type_node)
2049 return fcodef64;
2050 else if (mtype == float128_type_node)
2051 return fcodef128;
2052 else if (mtype == float32x_type_node)
2053 return fcodef32x;
2054 else if (mtype == float64x_type_node)
2055 return fcodef64x;
2056 else if (mtype == float128x_type_node)
2057 return fcodef128x;
2058 else
2059 return END_BUILTINS;
2062 #undef CASE_MATHFN
2063 #undef CASE_MATHFN_FLOATN
2064 #undef CASE_MATHFN_REENT
2066 /* Return mathematic function equivalent to FN but operating directly on TYPE,
2067 if available. If IMPLICIT_P is true use the implicit builtin declaration,
2068 otherwise use the explicit declaration. If we can't do the conversion,
2069 return null. */
2071 static tree
2072 mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2074 built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2075 if (fcode2 == END_BUILTINS)
2076 return NULL_TREE;
2078 if (implicit_p && !builtin_decl_implicit_p (fcode2))
2079 return NULL_TREE;
2081 return builtin_decl_explicit (fcode2);
2084 /* Like mathfn_built_in_1, but always use the implicit array. */
2086 tree
2087 mathfn_built_in (tree type, combined_fn fn)
2089 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2092 /* Like mathfn_built_in_1, but always use the explicit array. */
2094 tree
2095 mathfn_built_in_explicit (tree type, combined_fn fn)
2097 return mathfn_built_in_1 (type, fn, /*implicit=*/ 0);
2100 /* Like mathfn_built_in_1, but take a built_in_function and
2101 always use the implicit array. */
2103 tree
2104 mathfn_built_in (tree type, enum built_in_function fn)
2106 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2109 /* Return the type associated with a built in function, i.e., the one
2110 to be passed to mathfn_built_in to get the type-specific
2111 function. */
2113 tree
2114 mathfn_built_in_type (combined_fn fn)
2116 #define CASE_MATHFN(MATHFN) \
2117 case CFN_BUILT_IN_##MATHFN: \
2118 return double_type_node; \
2119 case CFN_BUILT_IN_##MATHFN##F: \
2120 return float_type_node; \
2121 case CFN_BUILT_IN_##MATHFN##L: \
2122 return long_double_type_node;
2124 #define CASE_MATHFN_FLOATN(MATHFN) \
2125 CASE_MATHFN(MATHFN) \
2126 case CFN_BUILT_IN_##MATHFN##F16: \
2127 return float16_type_node; \
2128 case CFN_BUILT_IN_##MATHFN##F32: \
2129 return float32_type_node; \
2130 case CFN_BUILT_IN_##MATHFN##F64: \
2131 return float64_type_node; \
2132 case CFN_BUILT_IN_##MATHFN##F128: \
2133 return float128_type_node; \
2134 case CFN_BUILT_IN_##MATHFN##F32X: \
2135 return float32x_type_node; \
2136 case CFN_BUILT_IN_##MATHFN##F64X: \
2137 return float64x_type_node; \
2138 case CFN_BUILT_IN_##MATHFN##F128X: \
2139 return float128x_type_node;
2141 /* Similar to above, but appends _R after any F/L suffix. */
2142 #define CASE_MATHFN_REENT(MATHFN) \
2143 case CFN_BUILT_IN_##MATHFN##_R: \
2144 return double_type_node; \
2145 case CFN_BUILT_IN_##MATHFN##F_R: \
2146 return float_type_node; \
2147 case CFN_BUILT_IN_##MATHFN##L_R: \
2148 return long_double_type_node;
2150 switch (fn)
2152 SEQ_OF_CASE_MATHFN
2154 default:
2155 return NULL_TREE;
2158 #undef CASE_MATHFN
2159 #undef CASE_MATHFN_FLOATN
2160 #undef CASE_MATHFN_REENT
2161 #undef SEQ_OF_CASE_MATHFN
2164 /* Check whether there is an internal function associated with function FN
2165 and return type RETURN_TYPE. Return the function if so, otherwise return
2166 IFN_LAST.
2168 Note that this function only tests whether the function is defined in
2169 internals.def, not whether it is actually available on the target. */
2171 static internal_fn
2172 associated_internal_fn (built_in_function fn, tree return_type)
2174 switch (fn)
2176 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2177 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2178 #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2179 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2180 CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2181 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2182 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2183 #include "internal-fn.def"
2185 CASE_FLT_FN (BUILT_IN_POW10):
2186 return IFN_EXP10;
2188 CASE_FLT_FN (BUILT_IN_DREM):
2189 return IFN_REMAINDER;
2191 CASE_FLT_FN (BUILT_IN_SCALBN):
2192 CASE_FLT_FN (BUILT_IN_SCALBLN):
2193 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2194 return IFN_LDEXP;
2195 return IFN_LAST;
2197 default:
2198 return IFN_LAST;
2202 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2203 return its code, otherwise return IFN_LAST. Note that this function
2204 only tests whether the function is defined in internals.def, not whether
2205 it is actually available on the target. */
2207 internal_fn
2208 associated_internal_fn (tree fndecl)
2210 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2211 return associated_internal_fn (DECL_FUNCTION_CODE (fndecl),
2212 TREE_TYPE (TREE_TYPE (fndecl)));
2215 /* Check whether there is an internal function associated with function CFN
2216 and return type RETURN_TYPE. Return the function if so, otherwise return
2217 IFN_LAST.
2219 Note that this function only tests whether the function is defined in
2220 internals.def, not whether it is actually available on the target. */
2222 internal_fn
2223 associated_internal_fn (combined_fn cfn, tree return_type)
2225 if (internal_fn_p (cfn))
2226 return as_internal_fn (cfn);
2227 return associated_internal_fn (as_builtin_fn (cfn), return_type);
2230 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2231 on the current target by a call to an internal function, return the
2232 code of that internal function, otherwise return IFN_LAST. The caller
2233 is responsible for ensuring that any side-effects of the built-in
2234 call are dealt with correctly. E.g. if CALL sets errno, the caller
2235 must decide that the errno result isn't needed or make it available
2236 in some other way. */
2238 internal_fn
2239 replacement_internal_fn (gcall *call)
2241 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2243 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2244 if (ifn != IFN_LAST)
2246 tree_pair types = direct_internal_fn_types (ifn, call);
2247 optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2248 if (direct_internal_fn_supported_p (ifn, types, opt_type))
2249 return ifn;
2252 return IFN_LAST;
2255 /* Expand a call to the builtin trinary math functions (fma).
2256 Return NULL_RTX if a normal call should be emitted rather than expanding the
2257 function in-line. EXP is the expression that is a call to the builtin
2258 function; if convenient, the result should be placed in TARGET.
2259 SUBTARGET may be used as the target for computing one of EXP's
2260 operands. */
2262 static rtx
2263 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2265 optab builtin_optab;
2266 rtx op0, op1, op2, result;
2267 rtx_insn *insns;
2268 tree fndecl = get_callee_fndecl (exp);
2269 tree arg0, arg1, arg2;
2270 machine_mode mode;
2272 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2273 return NULL_RTX;
2275 arg0 = CALL_EXPR_ARG (exp, 0);
2276 arg1 = CALL_EXPR_ARG (exp, 1);
2277 arg2 = CALL_EXPR_ARG (exp, 2);
2279 switch (DECL_FUNCTION_CODE (fndecl))
2281 CASE_FLT_FN (BUILT_IN_FMA):
2282 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2283 builtin_optab = fma_optab; break;
2284 default:
2285 gcc_unreachable ();
2288 /* Make a suitable register to place result in. */
2289 mode = TYPE_MODE (TREE_TYPE (exp));
2291 /* Before working hard, check whether the instruction is available. */
2292 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2293 return NULL_RTX;
2295 result = gen_reg_rtx (mode);
2297 /* Always stabilize the argument list. */
2298 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2299 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2300 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2302 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2303 op1 = expand_normal (arg1);
2304 op2 = expand_normal (arg2);
2306 start_sequence ();
2308 /* Compute into RESULT.
2309 Set RESULT to wherever the result comes back. */
2310 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2311 result, 0);
2313 /* If we were unable to expand via the builtin, stop the sequence
2314 (without outputting the insns) and call to the library function
2315 with the stabilized argument list. */
2316 if (result == 0)
2318 end_sequence ();
2319 return expand_call (exp, target, target == const0_rtx);
2322 /* Output the entire sequence. */
2323 insns = get_insns ();
2324 end_sequence ();
2325 emit_insn (insns);
2327 return result;
2330 /* Expand a call to the builtin sin and cos math functions.
2331 Return NULL_RTX if a normal call should be emitted rather than expanding the
2332 function in-line. EXP is the expression that is a call to the builtin
2333 function; if convenient, the result should be placed in TARGET.
2334 SUBTARGET may be used as the target for computing one of EXP's
2335 operands. */
2337 static rtx
2338 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2340 optab builtin_optab;
2341 rtx op0;
2342 rtx_insn *insns;
2343 tree fndecl = get_callee_fndecl (exp);
2344 machine_mode mode;
2345 tree arg;
2347 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2348 return NULL_RTX;
2350 arg = CALL_EXPR_ARG (exp, 0);
2352 switch (DECL_FUNCTION_CODE (fndecl))
2354 CASE_FLT_FN (BUILT_IN_SIN):
2355 CASE_FLT_FN (BUILT_IN_COS):
2356 builtin_optab = sincos_optab; break;
2357 default:
2358 gcc_unreachable ();
2361 /* Make a suitable register to place result in. */
2362 mode = TYPE_MODE (TREE_TYPE (exp));
2364 /* Check if sincos insn is available, otherwise fallback
2365 to sin or cos insn. */
2366 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2367 switch (DECL_FUNCTION_CODE (fndecl))
2369 CASE_FLT_FN (BUILT_IN_SIN):
2370 builtin_optab = sin_optab; break;
2371 CASE_FLT_FN (BUILT_IN_COS):
2372 builtin_optab = cos_optab; break;
2373 default:
2374 gcc_unreachable ();
2377 /* Before working hard, check whether the instruction is available. */
2378 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2380 rtx result = gen_reg_rtx (mode);
2382 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2383 need to expand the argument again. This way, we will not perform
2384 side-effects more the once. */
2385 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2387 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2389 start_sequence ();
2391 /* Compute into RESULT.
2392 Set RESULT to wherever the result comes back. */
2393 if (builtin_optab == sincos_optab)
2395 int ok;
2397 switch (DECL_FUNCTION_CODE (fndecl))
2399 CASE_FLT_FN (BUILT_IN_SIN):
2400 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2401 break;
2402 CASE_FLT_FN (BUILT_IN_COS):
2403 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2404 break;
2405 default:
2406 gcc_unreachable ();
2408 gcc_assert (ok);
2410 else
2411 result = expand_unop (mode, builtin_optab, op0, result, 0);
2413 if (result != 0)
2415 /* Output the entire sequence. */
2416 insns = get_insns ();
2417 end_sequence ();
2418 emit_insn (insns);
2419 return result;
2422 /* If we were unable to expand via the builtin, stop the sequence
2423 (without outputting the insns) and call to the library function
2424 with the stabilized argument list. */
2425 end_sequence ();
2428 return expand_call (exp, target, target == const0_rtx);
2431 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2432 return an RTL instruction code that implements the functionality.
2433 If that isn't possible or available return CODE_FOR_nothing. */
2435 static enum insn_code
2436 interclass_mathfn_icode (tree arg, tree fndecl)
2438 bool errno_set = false;
2439 optab builtin_optab = unknown_optab;
2440 machine_mode mode;
2442 switch (DECL_FUNCTION_CODE (fndecl))
2444 CASE_FLT_FN (BUILT_IN_ILOGB):
2445 errno_set = true; builtin_optab = ilogb_optab; break;
2446 CASE_FLT_FN (BUILT_IN_ISINF):
2447 builtin_optab = isinf_optab; break;
2448 case BUILT_IN_ISNORMAL:
2449 case BUILT_IN_ISFINITE:
2450 CASE_FLT_FN (BUILT_IN_FINITE):
2451 case BUILT_IN_FINITED32:
2452 case BUILT_IN_FINITED64:
2453 case BUILT_IN_FINITED128:
2454 case BUILT_IN_ISINFD32:
2455 case BUILT_IN_ISINFD64:
2456 case BUILT_IN_ISINFD128:
2457 /* These builtins have no optabs (yet). */
2458 break;
2459 default:
2460 gcc_unreachable ();
2463 /* There's no easy way to detect the case we need to set EDOM. */
2464 if (flag_errno_math && errno_set)
2465 return CODE_FOR_nothing;
2467 /* Optab mode depends on the mode of the input argument. */
2468 mode = TYPE_MODE (TREE_TYPE (arg));
2470 if (builtin_optab)
2471 return optab_handler (builtin_optab, mode);
2472 return CODE_FOR_nothing;
2475 /* Expand a call to one of the builtin math functions that operate on
2476 floating point argument and output an integer result (ilogb, isinf,
2477 isnan, etc).
2478 Return 0 if a normal call should be emitted rather than expanding the
2479 function in-line. EXP is the expression that is a call to the builtin
2480 function; if convenient, the result should be placed in TARGET. */
2482 static rtx
2483 expand_builtin_interclass_mathfn (tree exp, rtx target)
2485 enum insn_code icode = CODE_FOR_nothing;
2486 rtx op0;
2487 tree fndecl = get_callee_fndecl (exp);
2488 machine_mode mode;
2489 tree arg;
2491 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2492 return NULL_RTX;
2494 arg = CALL_EXPR_ARG (exp, 0);
2495 icode = interclass_mathfn_icode (arg, fndecl);
2496 mode = TYPE_MODE (TREE_TYPE (arg));
2498 if (icode != CODE_FOR_nothing)
2500 class expand_operand ops[1];
2501 rtx_insn *last = get_last_insn ();
2502 tree orig_arg = arg;
2504 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2505 need to expand the argument again. This way, we will not perform
2506 side-effects more the once. */
2507 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2509 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2511 if (mode != GET_MODE (op0))
2512 op0 = convert_to_mode (mode, op0, 0);
2514 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2515 if (maybe_legitimize_operands (icode, 0, 1, ops)
2516 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2517 return ops[0].value;
2519 delete_insns_since (last);
2520 CALL_EXPR_ARG (exp, 0) = orig_arg;
2523 return NULL_RTX;
2526 /* Expand a call to the builtin sincos math function.
2527 Return NULL_RTX if a normal call should be emitted rather than expanding the
2528 function in-line. EXP is the expression that is a call to the builtin
2529 function. */
2531 static rtx
2532 expand_builtin_sincos (tree exp)
2534 rtx op0, op1, op2, target1, target2;
2535 machine_mode mode;
2536 tree arg, sinp, cosp;
2537 int result;
2538 location_t loc = EXPR_LOCATION (exp);
2539 tree alias_type, alias_off;
2541 if (!validate_arglist (exp, REAL_TYPE,
2542 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2543 return NULL_RTX;
2545 arg = CALL_EXPR_ARG (exp, 0);
2546 sinp = CALL_EXPR_ARG (exp, 1);
2547 cosp = CALL_EXPR_ARG (exp, 2);
2549 /* Make a suitable register to place result in. */
2550 mode = TYPE_MODE (TREE_TYPE (arg));
2552 /* Check if sincos insn is available, otherwise emit the call. */
2553 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2554 return NULL_RTX;
2556 target1 = gen_reg_rtx (mode);
2557 target2 = gen_reg_rtx (mode);
2559 op0 = expand_normal (arg);
2560 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2561 alias_off = build_int_cst (alias_type, 0);
2562 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2563 sinp, alias_off));
2564 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2565 cosp, alias_off));
2567 /* Compute into target1 and target2.
2568 Set TARGET to wherever the result comes back. */
2569 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2570 gcc_assert (result);
2572 /* Move target1 and target2 to the memory locations indicated
2573 by op1 and op2. */
2574 emit_move_insn (op1, target1);
2575 emit_move_insn (op2, target2);
2577 return const0_rtx;
2580 /* Expand call EXP to the fegetround builtin (from C99 fenv.h), returning the
2581 result and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2582 static rtx
2583 expand_builtin_fegetround (tree exp, rtx target, machine_mode target_mode)
2585 if (!validate_arglist (exp, VOID_TYPE))
2586 return NULL_RTX;
2588 insn_code icode = direct_optab_handler (fegetround_optab, SImode);
2589 if (icode == CODE_FOR_nothing)
2590 return NULL_RTX;
2592 if (target == 0
2593 || GET_MODE (target) != target_mode
2594 || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2595 target = gen_reg_rtx (target_mode);
2597 rtx pat = GEN_FCN (icode) (target);
2598 if (!pat)
2599 return NULL_RTX;
2600 emit_insn (pat);
2602 return target;
2605 /* Expand call EXP to either feclearexcept or feraiseexcept builtins (from C99
2606 fenv.h), returning the result and setting it in TARGET. Otherwise return
2607 NULL_RTX on failure. */
2608 static rtx
2609 expand_builtin_feclear_feraise_except (tree exp, rtx target,
2610 machine_mode target_mode, optab op_optab)
2612 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
2613 return NULL_RTX;
2614 rtx op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
2616 insn_code icode = direct_optab_handler (op_optab, SImode);
2617 if (icode == CODE_FOR_nothing)
2618 return NULL_RTX;
2620 if (!(*insn_data[icode].operand[1].predicate) (op0, GET_MODE (op0)))
2621 return NULL_RTX;
2623 if (target == 0
2624 || GET_MODE (target) != target_mode
2625 || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2626 target = gen_reg_rtx (target_mode);
2628 rtx pat = GEN_FCN (icode) (target, op0);
2629 if (!pat)
2630 return NULL_RTX;
2631 emit_insn (pat);
2633 return target;
2636 /* Expand a call to the internal cexpi builtin to the sincos math function.
2637 EXP is the expression that is a call to the builtin function; if convenient,
2638 the result should be placed in TARGET. */
2640 static rtx
2641 expand_builtin_cexpi (tree exp, rtx target)
2643 tree fndecl = get_callee_fndecl (exp);
2644 tree arg, type;
2645 machine_mode mode;
2646 rtx op0, op1, op2;
2647 location_t loc = EXPR_LOCATION (exp);
2649 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2650 return NULL_RTX;
2652 arg = CALL_EXPR_ARG (exp, 0);
2653 type = TREE_TYPE (arg);
2654 mode = TYPE_MODE (TREE_TYPE (arg));
2656 /* Try expanding via a sincos optab, fall back to emitting a libcall
2657 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2658 is only generated from sincos, cexp or if we have either of them. */
2659 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2661 op1 = gen_reg_rtx (mode);
2662 op2 = gen_reg_rtx (mode);
2664 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2666 /* Compute into op1 and op2. */
2667 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2669 else if (targetm.libc_has_function (function_sincos, type))
2671 tree call, fn = NULL_TREE;
2672 tree top1, top2;
2673 rtx op1a, op2a;
2675 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2676 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2677 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2678 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2679 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2680 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2681 else
2682 gcc_unreachable ();
2684 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2685 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2686 op1a = copy_addr_to_reg (XEXP (op1, 0));
2687 op2a = copy_addr_to_reg (XEXP (op2, 0));
2688 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2689 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2691 /* Make sure not to fold the sincos call again. */
2692 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2693 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2694 call, 3, arg, top1, top2));
2696 else
2698 tree call, fn = NULL_TREE, narg;
2699 tree ctype = build_complex_type (type);
2701 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2702 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2703 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2704 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2705 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2706 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2707 else
2708 gcc_unreachable ();
2710 /* If we don't have a decl for cexp create one. This is the
2711 friendliest fallback if the user calls __builtin_cexpi
2712 without full target C99 function support. */
2713 if (fn == NULL_TREE)
2715 tree fntype;
2716 const char *name = NULL;
2718 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2719 name = "cexpf";
2720 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2721 name = "cexp";
2722 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2723 name = "cexpl";
2725 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2726 fn = build_fn_decl (name, fntype);
2729 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2730 build_real (type, dconst0), arg);
2732 /* Make sure not to fold the cexp call again. */
2733 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2734 return expand_expr (build_call_nary (ctype, call, 1, narg),
2735 target, VOIDmode, EXPAND_NORMAL);
2738 /* Now build the proper return type. */
2739 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2740 make_tree (TREE_TYPE (arg), op2),
2741 make_tree (TREE_TYPE (arg), op1)),
2742 target, VOIDmode, EXPAND_NORMAL);
2745 /* Conveniently construct a function call expression. FNDECL names the
2746 function to be called, N is the number of arguments, and the "..."
2747 parameters are the argument expressions. Unlike build_call_exr
2748 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2750 static tree
2751 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2753 va_list ap;
2754 tree fntype = TREE_TYPE (fndecl);
2755 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2757 va_start (ap, n);
2758 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2759 va_end (ap);
2760 SET_EXPR_LOCATION (fn, loc);
2761 return fn;
2764 /* Expand the __builtin_issignaling builtin. This needs to handle
2765 all floating point formats that do support NaNs (for those that
2766 don't it just sets target to 0). */
2768 static rtx
2769 expand_builtin_issignaling (tree exp, rtx target)
2771 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2772 return NULL_RTX;
2774 tree arg = CALL_EXPR_ARG (exp, 0);
2775 scalar_float_mode fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
2776 const struct real_format *fmt = REAL_MODE_FORMAT (fmode);
2778 /* Expand the argument yielding a RTX expression. */
2779 rtx temp = expand_normal (arg);
2781 /* If mode doesn't support NaN, always return 0.
2782 Don't use !HONOR_SNANS (fmode) here, so there is some possibility of
2783 __builtin_issignaling working without -fsignaling-nans. Especially
2784 when -fno-signaling-nans is the default.
2785 On the other side, MODE_HAS_NANS (fmode) is unnecessary, with
2786 -ffinite-math-only even __builtin_isnan or __builtin_fpclassify
2787 fold to 0 or non-NaN/Inf classification. */
2788 if (!HONOR_NANS (fmode))
2790 emit_move_insn (target, const0_rtx);
2791 return target;
2794 /* Check if the back end provides an insn that handles issignaling for the
2795 argument's mode. */
2796 enum insn_code icode = optab_handler (issignaling_optab, fmode);
2797 if (icode != CODE_FOR_nothing)
2799 rtx_insn *last = get_last_insn ();
2800 rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2801 if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
2802 return this_target;
2803 delete_insns_since (last);
2806 if (DECIMAL_FLOAT_MODE_P (fmode))
2808 scalar_int_mode imode;
2809 rtx hi;
2810 switch (fmt->ieee_bits)
2812 case 32:
2813 case 64:
2814 imode = int_mode_for_mode (fmode).require ();
2815 temp = gen_lowpart (imode, temp);
2816 break;
2817 case 128:
2818 imode = int_mode_for_size (64, 1).require ();
2819 hi = NULL_RTX;
2820 /* For decimal128, TImode support isn't always there and even when
2821 it is, working on the DImode high part is usually better. */
2822 if (!MEM_P (temp))
2824 if (rtx t = simplify_gen_subreg (imode, temp, fmode,
2825 subreg_highpart_offset (imode,
2826 fmode)))
2827 hi = t;
2828 else
2830 scalar_int_mode imode2;
2831 if (int_mode_for_mode (fmode).exists (&imode2))
2833 rtx temp2 = gen_lowpart (imode2, temp);
2834 poly_uint64 off = subreg_highpart_offset (imode, imode2);
2835 if (rtx t = simplify_gen_subreg (imode, temp2,
2836 imode2, off))
2837 hi = t;
2840 if (!hi)
2842 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2843 emit_move_insn (mem, temp);
2844 temp = mem;
2847 if (!hi)
2849 poly_int64 offset
2850 = subreg_highpart_offset (imode, GET_MODE (temp));
2851 hi = adjust_address (temp, imode, offset);
2853 temp = hi;
2854 break;
2855 default:
2856 gcc_unreachable ();
2858 /* In all of decimal{32,64,128}, there is MSB sign bit and sNaN
2859 have 6 bits below it all set. */
2860 rtx val
2861 = GEN_INT (HOST_WIDE_INT_C (0x3f) << (GET_MODE_BITSIZE (imode) - 7));
2862 temp = expand_binop (imode, and_optab, temp, val,
2863 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2864 temp = emit_store_flag_force (target, EQ, temp, val, imode, 1, 1);
2865 return temp;
2868 /* Only PDP11 has these defined differently but doesn't support NaNs. */
2869 gcc_assert (FLOAT_WORDS_BIG_ENDIAN == WORDS_BIG_ENDIAN);
2870 gcc_assert (fmt->signbit_ro > 0 && fmt->b == 2);
2871 gcc_assert (MODE_COMPOSITE_P (fmode)
2872 || (fmt->pnan == fmt->p
2873 && fmt->signbit_ro == fmt->signbit_rw));
2875 switch (fmt->p)
2877 case 106: /* IBM double double */
2878 /* For IBM double double, recurse on the most significant double. */
2879 gcc_assert (MODE_COMPOSITE_P (fmode));
2880 temp = convert_modes (DFmode, fmode, temp, 0);
2881 fmode = DFmode;
2882 fmt = REAL_MODE_FORMAT (DFmode);
2883 /* FALLTHRU */
2884 case 8: /* bfloat */
2885 case 11: /* IEEE half */
2886 case 24: /* IEEE single */
2887 case 53: /* IEEE double or Intel extended with rounding to double */
2888 if (fmt->p == 53 && fmt->signbit_ro == 79)
2889 goto extended;
2891 scalar_int_mode imode = int_mode_for_mode (fmode).require ();
2892 temp = gen_lowpart (imode, temp);
2893 rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2))
2894 & ~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2895 if (fmt->qnan_msb_set)
2897 rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2898 rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2));
2899 /* For non-MIPS/PA IEEE single/double/half or bfloat, expand to:
2900 ((temp ^ bit) & mask) > val. */
2901 temp = expand_binop (imode, xor_optab, temp, bit,
2902 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2903 temp = expand_binop (imode, and_optab, temp, mask,
2904 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2905 temp = emit_store_flag_force (target, GTU, temp, val, imode,
2906 1, 1);
2908 else
2910 /* For MIPS/PA IEEE single/double, expand to:
2911 (temp & val) == val. */
2912 temp = expand_binop (imode, and_optab, temp, val,
2913 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2914 temp = emit_store_flag_force (target, EQ, temp, val, imode,
2915 1, 1);
2918 break;
2919 case 113: /* IEEE quad */
2921 rtx hi = NULL_RTX, lo = NULL_RTX;
2922 scalar_int_mode imode = int_mode_for_size (64, 1).require ();
2923 /* For IEEE quad, TImode support isn't always there and even when
2924 it is, working on DImode parts is usually better. */
2925 if (!MEM_P (temp))
2927 hi = simplify_gen_subreg (imode, temp, fmode,
2928 subreg_highpart_offset (imode, fmode));
2929 lo = simplify_gen_subreg (imode, temp, fmode,
2930 subreg_lowpart_offset (imode, fmode));
2931 if (!hi || !lo)
2933 scalar_int_mode imode2;
2934 if (int_mode_for_mode (fmode).exists (&imode2))
2936 rtx temp2 = gen_lowpart (imode2, temp);
2937 hi = simplify_gen_subreg (imode, temp2, imode2,
2938 subreg_highpart_offset (imode,
2939 imode2));
2940 lo = simplify_gen_subreg (imode, temp2, imode2,
2941 subreg_lowpart_offset (imode,
2942 imode2));
2945 if (!hi || !lo)
2947 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2948 emit_move_insn (mem, temp);
2949 temp = mem;
2952 if (!hi || !lo)
2954 poly_int64 offset
2955 = subreg_highpart_offset (imode, GET_MODE (temp));
2956 hi = adjust_address (temp, imode, offset);
2957 offset = subreg_lowpart_offset (imode, GET_MODE (temp));
2958 lo = adjust_address (temp, imode, offset);
2960 rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2 - 64))
2961 & ~(HOST_WIDE_INT_M1U << (fmt->signbit_ro - 64)));
2962 if (fmt->qnan_msb_set)
2964 rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << (fmt->signbit_ro
2965 - 64)));
2966 rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2 - 64));
2967 /* For non-MIPS/PA IEEE quad, expand to:
2968 (((hi ^ bit) | ((lo | -lo) >> 63)) & mask) > val. */
2969 rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
2970 lo = expand_binop (imode, ior_optab, lo, nlo,
2971 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2972 lo = expand_shift (RSHIFT_EXPR, imode, lo, 63, NULL_RTX, 1);
2973 temp = expand_binop (imode, xor_optab, hi, bit,
2974 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2975 temp = expand_binop (imode, ior_optab, temp, lo,
2976 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2977 temp = expand_binop (imode, and_optab, temp, mask,
2978 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2979 temp = emit_store_flag_force (target, GTU, temp, val, imode,
2980 1, 1);
2982 else
2984 /* For MIPS/PA IEEE quad, expand to:
2985 (hi & val) == val. */
2986 temp = expand_binop (imode, and_optab, hi, val,
2987 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2988 temp = emit_store_flag_force (target, EQ, temp, val, imode,
2989 1, 1);
2992 break;
2993 case 64: /* Intel or Motorola extended */
2994 extended:
2996 rtx ex, hi, lo;
2997 scalar_int_mode imode = int_mode_for_size (32, 1).require ();
2998 scalar_int_mode iemode = int_mode_for_size (16, 1).require ();
2999 if (!MEM_P (temp))
3001 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
3002 emit_move_insn (mem, temp);
3003 temp = mem;
3005 if (fmt->signbit_ro == 95)
3007 /* Motorola, always big endian, with 16-bit gap in between
3008 16-bit sign+exponent and 64-bit mantissa. */
3009 ex = adjust_address (temp, iemode, 0);
3010 hi = adjust_address (temp, imode, 4);
3011 lo = adjust_address (temp, imode, 8);
3013 else if (!WORDS_BIG_ENDIAN)
3015 /* Intel little endian, 64-bit mantissa followed by 16-bit
3016 sign+exponent and then either 16 or 48 bits of gap. */
3017 ex = adjust_address (temp, iemode, 8);
3018 hi = adjust_address (temp, imode, 4);
3019 lo = adjust_address (temp, imode, 0);
3021 else
3023 /* Big endian Itanium. */
3024 ex = adjust_address (temp, iemode, 0);
3025 hi = adjust_address (temp, imode, 2);
3026 lo = adjust_address (temp, imode, 6);
3028 rtx val = GEN_INT (HOST_WIDE_INT_M1U << 30);
3029 gcc_assert (fmt->qnan_msb_set);
3030 rtx mask = GEN_INT (0x7fff);
3031 rtx bit = GEN_INT (HOST_WIDE_INT_1U << 30);
3032 /* For Intel/Motorola extended format, expand to:
3033 (ex & mask) == mask && ((hi ^ bit) | ((lo | -lo) >> 31)) > val. */
3034 rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3035 lo = expand_binop (imode, ior_optab, lo, nlo,
3036 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3037 lo = expand_shift (RSHIFT_EXPR, imode, lo, 31, NULL_RTX, 1);
3038 temp = expand_binop (imode, xor_optab, hi, bit,
3039 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3040 temp = expand_binop (imode, ior_optab, temp, lo,
3041 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3042 temp = emit_store_flag_force (target, GTU, temp, val, imode, 1, 1);
3043 ex = expand_binop (iemode, and_optab, ex, mask,
3044 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3045 ex = emit_store_flag_force (gen_reg_rtx (GET_MODE (temp)), EQ,
3046 ex, mask, iemode, 1, 1);
3047 temp = expand_binop (GET_MODE (temp), and_optab, temp, ex,
3048 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3050 break;
3051 default:
3052 gcc_unreachable ();
3055 return temp;
3058 /* Expand a call to one of the builtin rounding functions gcc defines
3059 as an extension (lfloor and lceil). As these are gcc extensions we
3060 do not need to worry about setting errno to EDOM.
3061 If expanding via optab fails, lower expression to (int)(floor(x)).
3062 EXP is the expression that is a call to the builtin function;
3063 if convenient, the result should be placed in TARGET. */
3065 static rtx
3066 expand_builtin_int_roundingfn (tree exp, rtx target)
3068 convert_optab builtin_optab;
3069 rtx op0, tmp;
3070 rtx_insn *insns;
3071 tree fndecl = get_callee_fndecl (exp);
3072 enum built_in_function fallback_fn;
3073 tree fallback_fndecl;
3074 machine_mode mode;
3075 tree arg;
3077 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3078 return NULL_RTX;
3080 arg = CALL_EXPR_ARG (exp, 0);
3082 switch (DECL_FUNCTION_CODE (fndecl))
3084 CASE_FLT_FN (BUILT_IN_ICEIL):
3085 CASE_FLT_FN (BUILT_IN_LCEIL):
3086 CASE_FLT_FN (BUILT_IN_LLCEIL):
3087 builtin_optab = lceil_optab;
3088 fallback_fn = BUILT_IN_CEIL;
3089 break;
3091 CASE_FLT_FN (BUILT_IN_IFLOOR):
3092 CASE_FLT_FN (BUILT_IN_LFLOOR):
3093 CASE_FLT_FN (BUILT_IN_LLFLOOR):
3094 builtin_optab = lfloor_optab;
3095 fallback_fn = BUILT_IN_FLOOR;
3096 break;
3098 default:
3099 gcc_unreachable ();
3102 /* Make a suitable register to place result in. */
3103 mode = TYPE_MODE (TREE_TYPE (exp));
3105 target = gen_reg_rtx (mode);
3107 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3108 need to expand the argument again. This way, we will not perform
3109 side-effects more the once. */
3110 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3112 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3114 start_sequence ();
3116 /* Compute into TARGET. */
3117 if (expand_sfix_optab (target, op0, builtin_optab))
3119 /* Output the entire sequence. */
3120 insns = get_insns ();
3121 end_sequence ();
3122 emit_insn (insns);
3123 return target;
3126 /* If we were unable to expand via the builtin, stop the sequence
3127 (without outputting the insns). */
3128 end_sequence ();
3130 /* Fall back to floating point rounding optab. */
3131 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
3133 /* For non-C99 targets we may end up without a fallback fndecl here
3134 if the user called __builtin_lfloor directly. In this case emit
3135 a call to the floor/ceil variants nevertheless. This should result
3136 in the best user experience for not full C99 targets. */
3137 if (fallback_fndecl == NULL_TREE)
3139 tree fntype;
3140 const char *name = NULL;
3142 switch (DECL_FUNCTION_CODE (fndecl))
3144 case BUILT_IN_ICEIL:
3145 case BUILT_IN_LCEIL:
3146 case BUILT_IN_LLCEIL:
3147 name = "ceil";
3148 break;
3149 case BUILT_IN_ICEILF:
3150 case BUILT_IN_LCEILF:
3151 case BUILT_IN_LLCEILF:
3152 name = "ceilf";
3153 break;
3154 case BUILT_IN_ICEILL:
3155 case BUILT_IN_LCEILL:
3156 case BUILT_IN_LLCEILL:
3157 name = "ceill";
3158 break;
3159 case BUILT_IN_IFLOOR:
3160 case BUILT_IN_LFLOOR:
3161 case BUILT_IN_LLFLOOR:
3162 name = "floor";
3163 break;
3164 case BUILT_IN_IFLOORF:
3165 case BUILT_IN_LFLOORF:
3166 case BUILT_IN_LLFLOORF:
3167 name = "floorf";
3168 break;
3169 case BUILT_IN_IFLOORL:
3170 case BUILT_IN_LFLOORL:
3171 case BUILT_IN_LLFLOORL:
3172 name = "floorl";
3173 break;
3174 default:
3175 gcc_unreachable ();
3178 fntype = build_function_type_list (TREE_TYPE (arg),
3179 TREE_TYPE (arg), NULL_TREE);
3180 fallback_fndecl = build_fn_decl (name, fntype);
3183 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
3185 tmp = expand_normal (exp);
3186 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
3188 /* Truncate the result of floating point optab to integer
3189 via expand_fix (). */
3190 target = gen_reg_rtx (mode);
3191 expand_fix (target, tmp, 0);
3193 return target;
3196 /* Expand a call to one of the builtin math functions doing integer
3197 conversion (lrint).
3198 Return 0 if a normal call should be emitted rather than expanding the
3199 function in-line. EXP is the expression that is a call to the builtin
3200 function; if convenient, the result should be placed in TARGET. */
3202 static rtx
3203 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
3205 convert_optab builtin_optab;
3206 rtx op0;
3207 rtx_insn *insns;
3208 tree fndecl = get_callee_fndecl (exp);
3209 tree arg;
3210 machine_mode mode;
3211 enum built_in_function fallback_fn = BUILT_IN_NONE;
3213 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3214 return NULL_RTX;
3216 arg = CALL_EXPR_ARG (exp, 0);
3218 switch (DECL_FUNCTION_CODE (fndecl))
3220 CASE_FLT_FN (BUILT_IN_IRINT):
3221 fallback_fn = BUILT_IN_LRINT;
3222 gcc_fallthrough ();
3223 CASE_FLT_FN (BUILT_IN_LRINT):
3224 CASE_FLT_FN (BUILT_IN_LLRINT):
3225 builtin_optab = lrint_optab;
3226 break;
3228 CASE_FLT_FN (BUILT_IN_IROUND):
3229 fallback_fn = BUILT_IN_LROUND;
3230 gcc_fallthrough ();
3231 CASE_FLT_FN (BUILT_IN_LROUND):
3232 CASE_FLT_FN (BUILT_IN_LLROUND):
3233 builtin_optab = lround_optab;
3234 break;
3236 default:
3237 gcc_unreachable ();
3240 /* There's no easy way to detect the case we need to set EDOM. */
3241 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
3242 return NULL_RTX;
3244 /* Make a suitable register to place result in. */
3245 mode = TYPE_MODE (TREE_TYPE (exp));
3247 /* There's no easy way to detect the case we need to set EDOM. */
3248 if (!flag_errno_math)
3250 rtx result = gen_reg_rtx (mode);
3252 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3253 need to expand the argument again. This way, we will not perform
3254 side-effects more the once. */
3255 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3257 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3259 start_sequence ();
3261 if (expand_sfix_optab (result, op0, builtin_optab))
3263 /* Output the entire sequence. */
3264 insns = get_insns ();
3265 end_sequence ();
3266 emit_insn (insns);
3267 return result;
3270 /* If we were unable to expand via the builtin, stop the sequence
3271 (without outputting the insns) and call to the library function
3272 with the stabilized argument list. */
3273 end_sequence ();
3276 if (fallback_fn != BUILT_IN_NONE)
3278 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
3279 targets, (int) round (x) should never be transformed into
3280 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
3281 a call to lround in the hope that the target provides at least some
3282 C99 functions. This should result in the best user experience for
3283 not full C99 targets.
3284 As scalar float conversions with same mode are useless in GIMPLE,
3285 we can end up e.g. with _Float32 argument passed to float builtin,
3286 try to get the type from the builtin prototype first. */
3287 tree fallback_fndecl = NULL_TREE;
3288 if (tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
3289 fallback_fndecl
3290 = mathfn_built_in_1 (TREE_VALUE (argtypes),
3291 as_combined_fn (fallback_fn), 0);
3292 if (fallback_fndecl == NULL_TREE)
3293 fallback_fndecl
3294 = mathfn_built_in_1 (TREE_TYPE (arg),
3295 as_combined_fn (fallback_fn), 0);
3296 if (fallback_fndecl)
3298 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
3299 fallback_fndecl, 1, arg);
3301 target = expand_call (exp, NULL_RTX, target == const0_rtx);
3302 target = maybe_emit_group_store (target, TREE_TYPE (exp));
3303 return convert_to_mode (mode, target, 0);
3307 return expand_call (exp, target, target == const0_rtx);
3310 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3311 a normal call should be emitted rather than expanding the function
3312 in-line. EXP is the expression that is a call to the builtin
3313 function; if convenient, the result should be placed in TARGET. */
3315 static rtx
3316 expand_builtin_powi (tree exp, rtx target)
3318 tree arg0, arg1;
3319 rtx op0, op1;
3320 machine_mode mode;
3321 machine_mode mode2;
3323 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3324 return NULL_RTX;
3326 arg0 = CALL_EXPR_ARG (exp, 0);
3327 arg1 = CALL_EXPR_ARG (exp, 1);
3328 mode = TYPE_MODE (TREE_TYPE (exp));
3330 /* Emit a libcall to libgcc. */
3332 /* Mode of the 2nd argument must match that of an int. */
3333 mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
3335 if (target == NULL_RTX)
3336 target = gen_reg_rtx (mode);
3338 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3339 if (GET_MODE (op0) != mode)
3340 op0 = convert_to_mode (mode, op0, 0);
3341 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3342 if (GET_MODE (op1) != mode2)
3343 op1 = convert_to_mode (mode2, op1, 0);
3345 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3346 target, LCT_CONST, mode,
3347 op0, mode, op1, mode2);
3349 return target;
3352 /* Expand expression EXP which is a call to the strlen builtin. Return
3353 NULL_RTX if we failed and the caller should emit a normal call, otherwise
3354 try to get the result in TARGET, if convenient. */
3356 static rtx
3357 expand_builtin_strlen (tree exp, rtx target,
3358 machine_mode target_mode)
3360 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3361 return NULL_RTX;
3363 tree src = CALL_EXPR_ARG (exp, 0);
3365 /* If the length can be computed at compile-time, return it. */
3366 if (tree len = c_strlen (src, 0))
3367 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3369 /* If the length can be computed at compile-time and is constant
3370 integer, but there are side-effects in src, evaluate
3371 src for side-effects, then return len.
3372 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3373 can be optimized into: i++; x = 3; */
3374 tree len = c_strlen (src, 1);
3375 if (len && TREE_CODE (len) == INTEGER_CST)
3377 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3378 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3381 unsigned int align = get_pointer_alignment (src) / BITS_PER_UNIT;
3383 /* If SRC is not a pointer type, don't do this operation inline. */
3384 if (align == 0)
3385 return NULL_RTX;
3387 /* Bail out if we can't compute strlen in the right mode. */
3388 machine_mode insn_mode;
3389 enum insn_code icode = CODE_FOR_nothing;
3390 FOR_EACH_MODE_FROM (insn_mode, target_mode)
3392 icode = optab_handler (strlen_optab, insn_mode);
3393 if (icode != CODE_FOR_nothing)
3394 break;
3396 if (insn_mode == VOIDmode)
3397 return NULL_RTX;
3399 /* Make a place to hold the source address. We will not expand
3400 the actual source until we are sure that the expansion will
3401 not fail -- there are trees that cannot be expanded twice. */
3402 rtx src_reg = gen_reg_rtx (Pmode);
3404 /* Mark the beginning of the strlen sequence so we can emit the
3405 source operand later. */
3406 rtx_insn *before_strlen = get_last_insn ();
3408 class expand_operand ops[4];
3409 create_output_operand (&ops[0], target, insn_mode);
3410 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3411 create_integer_operand (&ops[2], 0);
3412 create_integer_operand (&ops[3], align);
3413 if (!maybe_expand_insn (icode, 4, ops))
3414 return NULL_RTX;
3416 /* Check to see if the argument was declared attribute nonstring
3417 and if so, issue a warning since at this point it's not known
3418 to be nul-terminated. */
3419 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3421 /* Now that we are assured of success, expand the source. */
3422 start_sequence ();
3423 rtx pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3424 if (pat != src_reg)
3426 #ifdef POINTERS_EXTEND_UNSIGNED
3427 if (GET_MODE (pat) != Pmode)
3428 pat = convert_to_mode (Pmode, pat,
3429 POINTERS_EXTEND_UNSIGNED);
3430 #endif
3431 emit_move_insn (src_reg, pat);
3433 pat = get_insns ();
3434 end_sequence ();
3436 if (before_strlen)
3437 emit_insn_after (pat, before_strlen);
3438 else
3439 emit_insn_before (pat, get_insns ());
3441 /* Return the value in the proper mode for this function. */
3442 if (GET_MODE (ops[0].value) == target_mode)
3443 target = ops[0].value;
3444 else if (target != 0)
3445 convert_move (target, ops[0].value, 0);
3446 else
3447 target = convert_to_mode (target_mode, ops[0].value, 0);
3449 return target;
3452 /* Expand call EXP to the strnlen built-in, returning the result
3453 and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3455 static rtx
3456 expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
3458 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3459 return NULL_RTX;
3461 tree src = CALL_EXPR_ARG (exp, 0);
3462 tree bound = CALL_EXPR_ARG (exp, 1);
3464 if (!bound)
3465 return NULL_RTX;
3467 location_t loc = UNKNOWN_LOCATION;
3468 if (EXPR_HAS_LOCATION (exp))
3469 loc = EXPR_LOCATION (exp);
3471 /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3472 so these conversions aren't necessary. */
3473 c_strlen_data lendata = { };
3474 tree len = c_strlen (src, 0, &lendata, 1);
3475 if (len)
3476 len = fold_convert_loc (loc, TREE_TYPE (bound), len);
3478 if (TREE_CODE (bound) == INTEGER_CST)
3480 if (!len)
3481 return NULL_RTX;
3483 len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
3484 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3487 if (TREE_CODE (bound) != SSA_NAME)
3488 return NULL_RTX;
3490 wide_int min, max;
3491 value_range r;
3492 get_global_range_query ()->range_of_expr (r, bound);
3493 if (r.kind () != VR_RANGE)
3494 return NULL_RTX;
3495 min = r.lower_bound ();
3496 max = r.upper_bound ();
3498 if (!len || TREE_CODE (len) != INTEGER_CST)
3500 bool exact;
3501 lendata.decl = unterminated_array (src, &len, &exact);
3502 if (!lendata.decl)
3503 return NULL_RTX;
3506 if (lendata.decl)
3507 return NULL_RTX;
3509 if (wi::gtu_p (min, wi::to_wide (len)))
3510 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3512 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound);
3513 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3516 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3517 bytes from bytes at DATA + OFFSET and return it reinterpreted as
3518 a target constant. */
3520 static rtx
3521 builtin_memcpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3522 fixed_size_mode mode)
3524 /* The REPresentation pointed to by DATA need not be a nul-terminated
3525 string but the caller guarantees it's large enough for MODE. */
3526 const char *rep = (const char *) data;
3528 /* The by-pieces infrastructure does not try to pick a vector mode
3529 for memcpy expansion. */
3530 return c_readstr (rep + offset, as_a <scalar_int_mode> (mode),
3531 /*nul_terminated=*/false);
3534 /* LEN specify length of the block of memcpy/memset operation.
3535 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3536 In some cases we can make very likely guess on max size, then we
3537 set it into PROBABLE_MAX_SIZE. */
3539 static void
3540 determine_block_size (tree len, rtx len_rtx,
3541 unsigned HOST_WIDE_INT *min_size,
3542 unsigned HOST_WIDE_INT *max_size,
3543 unsigned HOST_WIDE_INT *probable_max_size)
3545 if (CONST_INT_P (len_rtx))
3547 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3548 return;
3550 else
3552 wide_int min, max;
3553 enum value_range_kind range_type = VR_UNDEFINED;
3555 /* Determine bounds from the type. */
3556 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3557 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3558 else
3559 *min_size = 0;
3560 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3561 *probable_max_size = *max_size
3562 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3563 else
3564 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3566 if (TREE_CODE (len) == SSA_NAME)
3568 value_range r;
3569 get_global_range_query ()->range_of_expr (r, len);
3570 range_type = r.kind ();
3571 if (range_type != VR_UNDEFINED)
3573 min = wi::to_wide (r.min ());
3574 max = wi::to_wide (r.max ());
3577 if (range_type == VR_RANGE)
3579 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3580 *min_size = min.to_uhwi ();
3581 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3582 *probable_max_size = *max_size = max.to_uhwi ();
3584 else if (range_type == VR_ANTI_RANGE)
3586 /* Code like
3588 int n;
3589 if (n < 100)
3590 memcpy (a, b, n)
3592 Produce anti range allowing negative values of N. We still
3593 can use the information and make a guess that N is not negative.
3595 if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3596 *probable_max_size = min.to_uhwi () - 1;
3599 gcc_checking_assert (*max_size <=
3600 (unsigned HOST_WIDE_INT)
3601 GET_MODE_MASK (GET_MODE (len_rtx)));
3604 /* Expand a call EXP to the memcpy builtin.
3605 Return NULL_RTX if we failed, the caller should emit a normal call,
3606 otherwise try to get the result in TARGET, if convenient (and in
3607 mode MODE if that's convenient). */
3609 static rtx
3610 expand_builtin_memcpy (tree exp, rtx target)
3612 if (!validate_arglist (exp,
3613 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3614 return NULL_RTX;
3616 tree dest = CALL_EXPR_ARG (exp, 0);
3617 tree src = CALL_EXPR_ARG (exp, 1);
3618 tree len = CALL_EXPR_ARG (exp, 2);
3620 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3621 /*retmode=*/ RETURN_BEGIN, false);
3624 /* Check a call EXP to the memmove built-in for validity.
3625 Return NULL_RTX on both success and failure. */
3627 static rtx
3628 expand_builtin_memmove (tree exp, rtx target)
3630 if (!validate_arglist (exp,
3631 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3632 return NULL_RTX;
3634 tree dest = CALL_EXPR_ARG (exp, 0);
3635 tree src = CALL_EXPR_ARG (exp, 1);
3636 tree len = CALL_EXPR_ARG (exp, 2);
3638 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3639 /*retmode=*/ RETURN_BEGIN, true);
3642 /* Expand a call EXP to the mempcpy builtin.
3643 Return NULL_RTX if we failed; the caller should emit a normal call,
3644 otherwise try to get the result in TARGET, if convenient (and in
3645 mode MODE if that's convenient). */
3647 static rtx
3648 expand_builtin_mempcpy (tree exp, rtx target)
3650 if (!validate_arglist (exp,
3651 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3652 return NULL_RTX;
3654 tree dest = CALL_EXPR_ARG (exp, 0);
3655 tree src = CALL_EXPR_ARG (exp, 1);
3656 tree len = CALL_EXPR_ARG (exp, 2);
3658 /* Policy does not generally allow using compute_objsize (which
3659 is used internally by check_memop_size) to change code generation
3660 or drive optimization decisions.
3662 In this instance it is safe because the code we generate has
3663 the same semantics regardless of the return value of
3664 check_memop_sizes. Exactly the same amount of data is copied
3665 and the return value is exactly the same in both cases.
3667 Furthermore, check_memop_size always uses mode 0 for the call to
3668 compute_objsize, so the imprecise nature of compute_objsize is
3669 avoided. */
3671 /* Avoid expanding mempcpy into memcpy when the call is determined
3672 to overflow the buffer. This also prevents the same overflow
3673 from being diagnosed again when expanding memcpy. */
3675 return expand_builtin_mempcpy_args (dest, src, len,
3676 target, exp, /*retmode=*/ RETURN_END);
3679 /* Helper function to do the actual work for expand of memory copy family
3680 functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3681 of memory from SRC to DEST and assign to TARGET if convenient. Return
3682 value is based on RETMODE argument. */
3684 static rtx
3685 expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3686 rtx target, tree exp, memop_ret retmode,
3687 bool might_overlap)
3689 unsigned int src_align = get_pointer_alignment (src);
3690 unsigned int dest_align = get_pointer_alignment (dest);
3691 rtx dest_mem, src_mem, dest_addr, len_rtx;
3692 HOST_WIDE_INT expected_size = -1;
3693 unsigned int expected_align = 0;
3694 unsigned HOST_WIDE_INT min_size;
3695 unsigned HOST_WIDE_INT max_size;
3696 unsigned HOST_WIDE_INT probable_max_size;
3698 bool is_move_done;
3700 /* If DEST is not a pointer type, call the normal function. */
3701 if (dest_align == 0)
3702 return NULL_RTX;
3704 /* If either SRC is not a pointer type, don't do this
3705 operation in-line. */
3706 if (src_align == 0)
3707 return NULL_RTX;
3709 if (currently_expanding_gimple_stmt)
3710 stringop_block_profile (currently_expanding_gimple_stmt,
3711 &expected_align, &expected_size);
3713 if (expected_align < dest_align)
3714 expected_align = dest_align;
3715 dest_mem = get_memory_rtx (dest, len);
3716 set_mem_align (dest_mem, dest_align);
3717 len_rtx = expand_normal (len);
3718 determine_block_size (len, len_rtx, &min_size, &max_size,
3719 &probable_max_size);
3721 /* Try to get the byte representation of the constant SRC points to,
3722 with its byte size in NBYTES. */
3723 unsigned HOST_WIDE_INT nbytes;
3724 const char *rep = getbyterep (src, &nbytes);
3726 /* If the function's constant bound LEN_RTX is less than or equal
3727 to the byte size of the representation of the constant argument,
3728 and if block move would be done by pieces, we can avoid loading
3729 the bytes from memory and only store the computed constant.
3730 This works in the overlap (memmove) case as well because
3731 store_by_pieces just generates a series of stores of constants
3732 from the representation returned by getbyterep(). */
3733 if (rep
3734 && CONST_INT_P (len_rtx)
3735 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes
3736 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3737 CONST_CAST (char *, rep),
3738 dest_align, false))
3740 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3741 builtin_memcpy_read_str,
3742 CONST_CAST (char *, rep),
3743 dest_align, false, retmode);
3744 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3745 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3746 return dest_mem;
3749 src_mem = get_memory_rtx (src, len);
3750 set_mem_align (src_mem, src_align);
3752 /* Copy word part most expediently. */
3753 enum block_op_methods method = BLOCK_OP_NORMAL;
3754 if (CALL_EXPR_TAILCALL (exp)
3755 && (retmode == RETURN_BEGIN || target == const0_rtx))
3756 method = BLOCK_OP_TAILCALL;
3757 bool use_mempcpy_call = (targetm.libc_has_fast_function (BUILT_IN_MEMPCPY)
3758 && retmode == RETURN_END
3759 && !might_overlap
3760 && target != const0_rtx);
3761 if (use_mempcpy_call)
3762 method = BLOCK_OP_NO_LIBCALL_RET;
3763 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3764 expected_align, expected_size,
3765 min_size, max_size, probable_max_size,
3766 use_mempcpy_call, &is_move_done,
3767 might_overlap);
3769 /* Bail out when a mempcpy call would be expanded as libcall and when
3770 we have a target that provides a fast implementation
3771 of mempcpy routine. */
3772 if (!is_move_done)
3773 return NULL_RTX;
3775 if (dest_addr == pc_rtx)
3776 return NULL_RTX;
3778 if (dest_addr == 0)
3780 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3781 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3784 if (retmode != RETURN_BEGIN && target != const0_rtx)
3786 dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3787 /* stpcpy pointer to last byte. */
3788 if (retmode == RETURN_END_MINUS_ONE)
3789 dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3792 return dest_addr;
3795 static rtx
3796 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3797 rtx target, tree orig_exp, memop_ret retmode)
3799 return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3800 retmode, false);
3803 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3804 we failed, the caller should emit a normal call, otherwise try to
3805 get the result in TARGET, if convenient.
3806 Return value is based on RETMODE argument. */
3808 static rtx
3809 expand_movstr (tree dest, tree src, rtx target, memop_ret retmode)
3811 class expand_operand ops[3];
3812 rtx dest_mem;
3813 rtx src_mem;
3815 if (!targetm.have_movstr ())
3816 return NULL_RTX;
3818 dest_mem = get_memory_rtx (dest, NULL);
3819 src_mem = get_memory_rtx (src, NULL);
3820 if (retmode == RETURN_BEGIN)
3822 target = force_reg (Pmode, XEXP (dest_mem, 0));
3823 dest_mem = replace_equiv_address (dest_mem, target);
3826 create_output_operand (&ops[0],
3827 retmode != RETURN_BEGIN ? target : NULL_RTX, Pmode);
3828 create_fixed_operand (&ops[1], dest_mem);
3829 create_fixed_operand (&ops[2], src_mem);
3830 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3831 return NULL_RTX;
3833 if (retmode != RETURN_BEGIN && target != const0_rtx)
3835 target = ops[0].value;
3836 /* movstr is supposed to set end to the address of the NUL
3837 terminator. If the caller requested a mempcpy-like return value,
3838 adjust it. */
3839 if (retmode == RETURN_END)
3841 rtx tem = plus_constant (GET_MODE (target),
3842 gen_lowpart (GET_MODE (target), target), 1);
3843 emit_move_insn (target, force_operand (tem, NULL_RTX));
3846 return target;
3849 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3850 NULL_RTX if we failed the caller should emit a normal call, otherwise
3851 try to get the result in TARGET, if convenient (and in mode MODE if that's
3852 convenient). */
3854 static rtx
3855 expand_builtin_strcpy (tree exp, rtx target)
3857 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3858 return NULL_RTX;
3860 tree dest = CALL_EXPR_ARG (exp, 0);
3861 tree src = CALL_EXPR_ARG (exp, 1);
3863 return expand_builtin_strcpy_args (exp, dest, src, target);
3866 /* Helper function to do the actual work for expand_builtin_strcpy. The
3867 arguments to the builtin_strcpy call DEST and SRC are broken out
3868 so that this can also be called without constructing an actual CALL_EXPR.
3869 The other arguments and return value are the same as for
3870 expand_builtin_strcpy. */
3872 static rtx
3873 expand_builtin_strcpy_args (tree, tree dest, tree src, rtx target)
3875 return expand_movstr (dest, src, target, /*retmode=*/ RETURN_BEGIN);
3878 /* Expand a call EXP to the stpcpy builtin.
3879 Return NULL_RTX if we failed the caller should emit a normal call,
3880 otherwise try to get the result in TARGET, if convenient (and in
3881 mode MODE if that's convenient). */
3883 static rtx
3884 expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
3886 tree dst, src;
3887 location_t loc = EXPR_LOCATION (exp);
3889 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3890 return NULL_RTX;
3892 dst = CALL_EXPR_ARG (exp, 0);
3893 src = CALL_EXPR_ARG (exp, 1);
3895 /* If return value is ignored, transform stpcpy into strcpy. */
3896 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3898 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3899 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3900 return expand_expr (result, target, mode, EXPAND_NORMAL);
3902 else
3904 tree len, lenp1;
3905 rtx ret;
3907 /* Ensure we get an actual string whose length can be evaluated at
3908 compile-time, not an expression containing a string. This is
3909 because the latter will potentially produce pessimized code
3910 when used to produce the return value. */
3911 c_strlen_data lendata = { };
3912 if (!c_getstr (src)
3913 || !(len = c_strlen (src, 0, &lendata, 1)))
3914 return expand_movstr (dst, src, target,
3915 /*retmode=*/ RETURN_END_MINUS_ONE);
3917 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3918 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3919 target, exp,
3920 /*retmode=*/ RETURN_END_MINUS_ONE);
3922 if (ret)
3923 return ret;
3925 if (TREE_CODE (len) == INTEGER_CST)
3927 rtx len_rtx = expand_normal (len);
3929 if (CONST_INT_P (len_rtx))
3931 ret = expand_builtin_strcpy_args (exp, dst, src, target);
3933 if (ret)
3935 if (! target)
3937 if (mode != VOIDmode)
3938 target = gen_reg_rtx (mode);
3939 else
3940 target = gen_reg_rtx (GET_MODE (ret));
3942 if (GET_MODE (target) != GET_MODE (ret))
3943 ret = gen_lowpart (GET_MODE (target), ret);
3945 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3946 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3947 gcc_assert (ret);
3949 return target;
3954 return expand_movstr (dst, src, target,
3955 /*retmode=*/ RETURN_END_MINUS_ONE);
3959 /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
3960 arguments while being careful to avoid duplicate warnings (which could
3961 be issued if the expander were to expand the call, resulting in it
3962 being emitted in expand_call(). */
3964 static rtx
3965 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3967 if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode))
3969 /* The call has been successfully expanded. Check for nonstring
3970 arguments and issue warnings as appropriate. */
3971 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3972 return ret;
3975 return NULL_RTX;
3978 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3979 bytes from constant string DATA + OFFSET and return it as target
3980 constant. */
3983 builtin_strncpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3984 fixed_size_mode mode)
3986 const char *str = (const char *) data;
3988 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3989 return const0_rtx;
3991 /* The by-pieces infrastructure does not try to pick a vector mode
3992 for strncpy expansion. */
3993 return c_readstr (str + offset, as_a <scalar_int_mode> (mode));
3996 /* Helper to check the sizes of sequences and the destination of calls
3997 to __builtin_strncat and __builtin___strncat_chk. Returns true on
3998 success (no overflow or invalid sizes), false otherwise. */
4000 static bool
4001 check_strncat_sizes (tree exp, tree objsize)
4003 tree dest = CALL_EXPR_ARG (exp, 0);
4004 tree src = CALL_EXPR_ARG (exp, 1);
4005 tree maxread = CALL_EXPR_ARG (exp, 2);
4007 /* Try to determine the range of lengths that the source expression
4008 refers to. */
4009 c_strlen_data lendata = { };
4010 get_range_strlen (src, &lendata, /* eltsize = */ 1);
4012 /* Try to verify that the destination is big enough for the shortest
4013 string. */
4015 access_data data (nullptr, exp, access_read_write, maxread, true);
4016 if (!objsize && warn_stringop_overflow)
4018 /* If it hasn't been provided by __strncat_chk, try to determine
4019 the size of the destination object into which the source is
4020 being copied. */
4021 objsize = compute_objsize (dest, warn_stringop_overflow - 1, &data.dst);
4024 /* Add one for the terminating nul. */
4025 tree srclen = (lendata.minlen
4026 ? fold_build2 (PLUS_EXPR, size_type_node, lendata.minlen,
4027 size_one_node)
4028 : NULL_TREE);
4030 /* The strncat function copies at most MAXREAD bytes and always appends
4031 the terminating nul so the specified upper bound should never be equal
4032 to (or greater than) the size of the destination. */
4033 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
4034 && tree_int_cst_equal (objsize, maxread))
4036 location_t loc = EXPR_LOCATION (exp);
4037 warning_at (loc, OPT_Wstringop_overflow_,
4038 "%qD specified bound %E equals destination size",
4039 get_callee_fndecl (exp), maxread);
4041 return false;
4044 if (!srclen
4045 || (maxread && tree_fits_uhwi_p (maxread)
4046 && tree_fits_uhwi_p (srclen)
4047 && tree_int_cst_lt (maxread, srclen)))
4048 srclen = maxread;
4050 /* The number of bytes to write is LEN but check_access will alsoa
4051 check SRCLEN if LEN's value isn't known. */
4052 return check_access (exp, /*dstwrite=*/NULL_TREE, maxread, srclen,
4053 objsize, data.mode, &data);
4056 /* Expand expression EXP, which is a call to the strncpy builtin. Return
4057 NULL_RTX if we failed the caller should emit a normal call. */
4059 static rtx
4060 expand_builtin_strncpy (tree exp, rtx target)
4062 location_t loc = EXPR_LOCATION (exp);
4064 if (!validate_arglist (exp,
4065 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4066 return NULL_RTX;
4067 tree dest = CALL_EXPR_ARG (exp, 0);
4068 tree src = CALL_EXPR_ARG (exp, 1);
4069 /* The number of bytes to write (not the maximum). */
4070 tree len = CALL_EXPR_ARG (exp, 2);
4072 /* The length of the source sequence. */
4073 tree slen = c_strlen (src, 1);
4075 /* We must be passed a constant len and src parameter. */
4076 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4077 return NULL_RTX;
4079 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4081 /* We're required to pad with trailing zeros if the requested
4082 len is greater than strlen(s2)+1. In that case try to
4083 use store_by_pieces, if it fails, punt. */
4084 if (tree_int_cst_lt (slen, len))
4086 unsigned int dest_align = get_pointer_alignment (dest);
4087 const char *p = c_getstr (src);
4088 rtx dest_mem;
4090 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4091 || !can_store_by_pieces (tree_to_uhwi (len),
4092 builtin_strncpy_read_str,
4093 CONST_CAST (char *, p),
4094 dest_align, false))
4095 return NULL_RTX;
4097 dest_mem = get_memory_rtx (dest, len);
4098 store_by_pieces (dest_mem, tree_to_uhwi (len),
4099 builtin_strncpy_read_str,
4100 CONST_CAST (char *, p), dest_align, false,
4101 RETURN_BEGIN);
4102 dest_mem = force_operand (XEXP (dest_mem, 0), target);
4103 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4104 return dest_mem;
4107 return NULL_RTX;
4110 /* Return the RTL of a register in MODE generated from PREV in the
4111 previous iteration. */
4113 static rtx
4114 gen_memset_value_from_prev (by_pieces_prev *prev, fixed_size_mode mode)
4116 rtx target = nullptr;
4117 if (prev != nullptr && prev->data != nullptr)
4119 /* Use the previous data in the same mode. */
4120 if (prev->mode == mode)
4121 return prev->data;
4123 fixed_size_mode prev_mode = prev->mode;
4125 /* Don't use the previous data to write QImode if it is in a
4126 vector mode. */
4127 if (VECTOR_MODE_P (prev_mode) && mode == QImode)
4128 return target;
4130 rtx prev_rtx = prev->data;
4132 if (REG_P (prev_rtx)
4133 && HARD_REGISTER_P (prev_rtx)
4134 && lowpart_subreg_regno (REGNO (prev_rtx), prev_mode, mode) < 0)
4136 /* This case occurs when PREV_MODE is a vector and when
4137 MODE is too small to store using vector operations.
4138 After register allocation, the code will need to move the
4139 lowpart of the vector register into a non-vector register.
4141 Also, the target has chosen to use a hard register
4142 instead of going with the default choice of using a
4143 pseudo register. We should respect that choice and try to
4144 avoid creating a pseudo register with the same mode as the
4145 current hard register.
4147 In principle, we could just use a lowpart MODE subreg of
4148 the vector register. However, the vector register mode might
4149 be too wide for non-vector registers, and we already know
4150 that the non-vector mode is too small for vector registers.
4151 It's therefore likely that we'd need to spill to memory in
4152 the vector mode and reload the non-vector value from there.
4154 Try to avoid that by reducing the vector register to the
4155 smallest size that it can hold. This should increase the
4156 chances that non-vector registers can hold both the inner
4157 and outer modes of the subreg that we generate later. */
4158 machine_mode m;
4159 fixed_size_mode candidate;
4160 FOR_EACH_MODE_IN_CLASS (m, GET_MODE_CLASS (mode))
4161 if (is_a<fixed_size_mode> (m, &candidate))
4163 if (GET_MODE_SIZE (candidate)
4164 >= GET_MODE_SIZE (prev_mode))
4165 break;
4166 if (GET_MODE_SIZE (candidate) >= GET_MODE_SIZE (mode)
4167 && lowpart_subreg_regno (REGNO (prev_rtx),
4168 prev_mode, candidate) >= 0)
4170 target = lowpart_subreg (candidate, prev_rtx,
4171 prev_mode);
4172 prev_rtx = target;
4173 prev_mode = candidate;
4174 break;
4177 if (target == nullptr)
4178 prev_rtx = copy_to_reg (prev_rtx);
4181 target = lowpart_subreg (mode, prev_rtx, prev_mode);
4183 return target;
4186 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4187 bytes from constant string DATA + OFFSET and return it as target
4188 constant. If PREV isn't nullptr, it has the RTL info from the
4189 previous iteration. */
4192 builtin_memset_read_str (void *data, void *prev,
4193 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4194 fixed_size_mode mode)
4196 const char *c = (const char *) data;
4197 unsigned int size = GET_MODE_SIZE (mode);
4199 rtx target = gen_memset_value_from_prev ((by_pieces_prev *) prev,
4200 mode);
4201 if (target != nullptr)
4202 return target;
4203 rtx src = gen_int_mode (*c, QImode);
4205 if (VECTOR_MODE_P (mode))
4207 gcc_assert (GET_MODE_INNER (mode) == QImode);
4209 rtx const_vec = gen_const_vec_duplicate (mode, src);
4210 if (prev == NULL)
4211 /* Return CONST_VECTOR when called by a query function. */
4212 return const_vec;
4214 /* Use the move expander with CONST_VECTOR. */
4215 target = targetm.gen_memset_scratch_rtx (mode);
4216 emit_move_insn (target, const_vec);
4217 return target;
4220 char *p = XALLOCAVEC (char, size);
4222 memset (p, *c, size);
4224 /* Vector modes should be handled above. */
4225 return c_readstr (p, as_a <scalar_int_mode> (mode));
4228 /* Callback routine for store_by_pieces. Return the RTL of a register
4229 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4230 char value given in the RTL register data. For example, if mode is
4231 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
4232 nullptr, it has the RTL info from the previous iteration. */
4234 static rtx
4235 builtin_memset_gen_str (void *data, void *prev,
4236 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4237 fixed_size_mode mode)
4239 rtx target, coeff;
4240 size_t size;
4241 char *p;
4243 size = GET_MODE_SIZE (mode);
4244 if (size == 1)
4245 return (rtx) data;
4247 target = gen_memset_value_from_prev ((by_pieces_prev *) prev, mode);
4248 if (target != nullptr)
4249 return target;
4251 if (VECTOR_MODE_P (mode))
4253 gcc_assert (GET_MODE_INNER (mode) == QImode);
4255 /* vec_duplicate_optab is a precondition to pick a vector mode for
4256 the memset expander. */
4257 insn_code icode = optab_handler (vec_duplicate_optab, mode);
4259 target = targetm.gen_memset_scratch_rtx (mode);
4260 class expand_operand ops[2];
4261 create_output_operand (&ops[0], target, mode);
4262 create_input_operand (&ops[1], (rtx) data, QImode);
4263 expand_insn (icode, 2, ops);
4264 if (!rtx_equal_p (target, ops[0].value))
4265 emit_move_insn (target, ops[0].value);
4267 return target;
4270 p = XALLOCAVEC (char, size);
4271 memset (p, 1, size);
4272 /* Vector modes should be handled above. */
4273 coeff = c_readstr (p, as_a <scalar_int_mode> (mode));
4275 target = convert_to_mode (mode, (rtx) data, 1);
4276 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4277 return force_reg (mode, target);
4280 /* Expand expression EXP, which is a call to the memset builtin. Return
4281 NULL_RTX if we failed the caller should emit a normal call, otherwise
4282 try to get the result in TARGET, if convenient (and in mode MODE if that's
4283 convenient). */
4286 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4288 if (!validate_arglist (exp,
4289 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4290 return NULL_RTX;
4292 tree dest = CALL_EXPR_ARG (exp, 0);
4293 tree val = CALL_EXPR_ARG (exp, 1);
4294 tree len = CALL_EXPR_ARG (exp, 2);
4296 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4299 /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
4300 Return TRUE if successful, FALSE otherwise. TO is assumed to be
4301 aligned at an ALIGN-bits boundary. LEN must be a multiple of
4302 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
4304 The strategy is to issue one store_by_pieces for each power of two,
4305 from most to least significant, guarded by a test on whether there
4306 are at least that many bytes left to copy in LEN.
4308 ??? Should we skip some powers of two in favor of loops? Maybe start
4309 at the max of TO/LEN/word alignment, at least when optimizing for
4310 size, instead of ensuring O(log len) dynamic compares? */
4312 bool
4313 try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
4314 unsigned HOST_WIDE_INT min_len,
4315 unsigned HOST_WIDE_INT max_len,
4316 rtx val, char valc, unsigned int align)
4318 int max_bits = floor_log2 (max_len);
4319 int min_bits = floor_log2 (min_len);
4320 int sctz_len = ctz_len;
4322 gcc_checking_assert (sctz_len >= 0);
4324 if (val)
4325 valc = 1;
4327 /* Bits more significant than TST_BITS are part of the shared prefix
4328 in the binary representation of both min_len and max_len. Since
4329 they're identical, we don't need to test them in the loop. */
4330 int tst_bits = (max_bits != min_bits ? max_bits
4331 : floor_log2 (max_len ^ min_len));
4333 /* Check whether it's profitable to start by storing a fixed BLKSIZE
4334 bytes, to lower max_bits. In the unlikely case of a constant LEN
4335 (implied by identical MAX_LEN and MIN_LEN), we want to issue a
4336 single store_by_pieces, but otherwise, select the minimum multiple
4337 of the ALIGN (in bytes) and of the MCD of the possible LENs, that
4338 brings MAX_LEN below TST_BITS, if that's lower than min_len. */
4339 unsigned HOST_WIDE_INT blksize;
4340 if (max_len > min_len)
4342 unsigned HOST_WIDE_INT alrng = MAX (HOST_WIDE_INT_1U << ctz_len,
4343 align / BITS_PER_UNIT);
4344 blksize = max_len - (HOST_WIDE_INT_1U << tst_bits) + alrng;
4345 blksize &= ~(alrng - 1);
4347 else if (max_len == min_len)
4348 blksize = max_len;
4349 else
4350 /* Huh, max_len < min_len? Punt. See pr100843.c. */
4351 return false;
4352 if (min_len >= blksize)
4354 min_len -= blksize;
4355 min_bits = floor_log2 (min_len);
4356 max_len -= blksize;
4357 max_bits = floor_log2 (max_len);
4359 tst_bits = (max_bits != min_bits ? max_bits
4360 : floor_log2 (max_len ^ min_len));
4362 else
4363 blksize = 0;
4365 /* Check that we can use store by pieces for the maximum store count
4366 we may issue (initial fixed-size block, plus conditional
4367 power-of-two-sized from max_bits to ctz_len. */
4368 unsigned HOST_WIDE_INT xlenest = blksize;
4369 if (max_bits >= 0)
4370 xlenest += ((HOST_WIDE_INT_1U << max_bits) * 2
4371 - (HOST_WIDE_INT_1U << ctz_len));
4372 if (!can_store_by_pieces (xlenest, builtin_memset_read_str,
4373 &valc, align, true))
4374 return false;
4376 by_pieces_constfn constfun;
4377 void *constfundata;
4378 if (val)
4380 constfun = builtin_memset_gen_str;
4381 constfundata = val = force_reg (TYPE_MODE (unsigned_char_type_node),
4382 val);
4384 else
4386 constfun = builtin_memset_read_str;
4387 constfundata = &valc;
4390 rtx ptr = copy_addr_to_reg (XEXP (to, 0));
4391 rtx rem = copy_to_mode_reg (ptr_mode, convert_to_mode (ptr_mode, len, 0));
4392 to = replace_equiv_address (to, ptr);
4393 set_mem_align (to, align);
4395 if (blksize)
4397 to = store_by_pieces (to, blksize,
4398 constfun, constfundata,
4399 align, true,
4400 max_len != 0 ? RETURN_END : RETURN_BEGIN);
4401 if (max_len == 0)
4402 return true;
4404 /* Adjust PTR, TO and REM. Since TO's address is likely
4405 PTR+offset, we have to replace it. */
4406 emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4407 to = replace_equiv_address (to, ptr);
4408 rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4409 emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4412 /* Iterate over power-of-two block sizes from the maximum length to
4413 the least significant bit possibly set in the length. */
4414 for (int i = max_bits; i >= sctz_len; i--)
4416 rtx_code_label *label = NULL;
4417 blksize = HOST_WIDE_INT_1U << i;
4419 /* If we're past the bits shared between min_ and max_len, expand
4420 a test on the dynamic length, comparing it with the
4421 BLKSIZE. */
4422 if (i <= tst_bits)
4424 label = gen_label_rtx ();
4425 emit_cmp_and_jump_insns (rem, GEN_INT (blksize), LT, NULL,
4426 ptr_mode, 1, label,
4427 profile_probability::even ());
4429 /* If we are at a bit that is in the prefix shared by min_ and
4430 max_len, skip this BLKSIZE if the bit is clear. */
4431 else if ((max_len & blksize) == 0)
4432 continue;
4434 /* Issue a store of BLKSIZE bytes. */
4435 to = store_by_pieces (to, blksize,
4436 constfun, constfundata,
4437 align, true,
4438 i != sctz_len ? RETURN_END : RETURN_BEGIN);
4440 /* Adjust REM and PTR, unless this is the last iteration. */
4441 if (i != sctz_len)
4443 emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4444 to = replace_equiv_address (to, ptr);
4445 rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4446 emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4449 if (label)
4451 emit_label (label);
4453 /* Given conditional stores, the offset can no longer be
4454 known, so clear it. */
4455 clear_mem_offset (to);
4459 return true;
4462 /* Helper function to do the actual work for expand_builtin_memset. The
4463 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4464 so that this can also be called without constructing an actual CALL_EXPR.
4465 The other arguments and return value are the same as for
4466 expand_builtin_memset. */
4468 static rtx
4469 expand_builtin_memset_args (tree dest, tree val, tree len,
4470 rtx target, machine_mode mode, tree orig_exp)
4472 tree fndecl, fn;
4473 enum built_in_function fcode;
4474 machine_mode val_mode;
4475 char c;
4476 unsigned int dest_align;
4477 rtx dest_mem, dest_addr, len_rtx;
4478 HOST_WIDE_INT expected_size = -1;
4479 unsigned int expected_align = 0;
4480 unsigned HOST_WIDE_INT min_size;
4481 unsigned HOST_WIDE_INT max_size;
4482 unsigned HOST_WIDE_INT probable_max_size;
4484 dest_align = get_pointer_alignment (dest);
4486 /* If DEST is not a pointer type, don't do this operation in-line. */
4487 if (dest_align == 0)
4488 return NULL_RTX;
4490 if (currently_expanding_gimple_stmt)
4491 stringop_block_profile (currently_expanding_gimple_stmt,
4492 &expected_align, &expected_size);
4494 if (expected_align < dest_align)
4495 expected_align = dest_align;
4497 /* If the LEN parameter is zero, return DEST. */
4498 if (integer_zerop (len))
4500 /* Evaluate and ignore VAL in case it has side-effects. */
4501 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4502 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4505 /* Stabilize the arguments in case we fail. */
4506 dest = builtin_save_expr (dest);
4507 val = builtin_save_expr (val);
4508 len = builtin_save_expr (len);
4510 len_rtx = expand_normal (len);
4511 determine_block_size (len, len_rtx, &min_size, &max_size,
4512 &probable_max_size);
4513 dest_mem = get_memory_rtx (dest, len);
4514 val_mode = TYPE_MODE (unsigned_char_type_node);
4516 if (TREE_CODE (val) != INTEGER_CST
4517 || target_char_cast (val, &c))
4519 rtx val_rtx;
4521 val_rtx = expand_normal (val);
4522 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4524 /* Assume that we can memset by pieces if we can store
4525 * the coefficients by pieces (in the required modes).
4526 * We can't pass builtin_memset_gen_str as that emits RTL. */
4527 c = 1;
4528 if (tree_fits_uhwi_p (len)
4529 && can_store_by_pieces (tree_to_uhwi (len),
4530 builtin_memset_read_str, &c, dest_align,
4531 true))
4533 val_rtx = force_reg (val_mode, val_rtx);
4534 store_by_pieces (dest_mem, tree_to_uhwi (len),
4535 builtin_memset_gen_str, val_rtx, dest_align,
4536 true, RETURN_BEGIN);
4538 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4539 dest_align, expected_align,
4540 expected_size, min_size, max_size,
4541 probable_max_size)
4542 && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4543 tree_ctz (len),
4544 min_size, max_size,
4545 val_rtx, 0,
4546 dest_align))
4547 goto do_libcall;
4549 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4550 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4551 return dest_mem;
4554 if (c)
4556 if (tree_fits_uhwi_p (len)
4557 && can_store_by_pieces (tree_to_uhwi (len),
4558 builtin_memset_read_str, &c, dest_align,
4559 true))
4560 store_by_pieces (dest_mem, tree_to_uhwi (len),
4561 builtin_memset_read_str, &c, dest_align, true,
4562 RETURN_BEGIN);
4563 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4564 gen_int_mode (c, val_mode),
4565 dest_align, expected_align,
4566 expected_size, min_size, max_size,
4567 probable_max_size)
4568 && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4569 tree_ctz (len),
4570 min_size, max_size,
4571 NULL_RTX, c,
4572 dest_align))
4573 goto do_libcall;
4575 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4576 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4577 return dest_mem;
4580 set_mem_align (dest_mem, dest_align);
4581 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4582 CALL_EXPR_TAILCALL (orig_exp)
4583 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4584 expected_align, expected_size,
4585 min_size, max_size,
4586 probable_max_size, tree_ctz (len));
4588 if (dest_addr == 0)
4590 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4591 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4594 return dest_addr;
4596 do_libcall:
4597 fndecl = get_callee_fndecl (orig_exp);
4598 fcode = DECL_FUNCTION_CODE (fndecl);
4599 if (fcode == BUILT_IN_MEMSET)
4600 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4601 dest, val, len);
4602 else if (fcode == BUILT_IN_BZERO)
4603 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4604 dest, len);
4605 else
4606 gcc_unreachable ();
4607 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4608 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4609 return expand_call (fn, target, target == const0_rtx);
4612 /* Expand expression EXP, which is a call to the bzero builtin. Return
4613 NULL_RTX if we failed the caller should emit a normal call. */
4615 static rtx
4616 expand_builtin_bzero (tree exp)
4618 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4619 return NULL_RTX;
4621 tree dest = CALL_EXPR_ARG (exp, 0);
4622 tree size = CALL_EXPR_ARG (exp, 1);
4624 /* New argument list transforming bzero(ptr x, int y) to
4625 memset(ptr x, int 0, size_t y). This is done this way
4626 so that if it isn't expanded inline, we fallback to
4627 calling bzero instead of memset. */
4629 location_t loc = EXPR_LOCATION (exp);
4631 return expand_builtin_memset_args (dest, integer_zero_node,
4632 fold_convert_loc (loc,
4633 size_type_node, size),
4634 const0_rtx, VOIDmode, exp);
4637 /* Try to expand cmpstr operation ICODE with the given operands.
4638 Return the result rtx on success, otherwise return null. */
4640 static rtx
4641 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4642 HOST_WIDE_INT align)
4644 machine_mode insn_mode = insn_data[icode].operand[0].mode;
4646 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4647 target = NULL_RTX;
4649 class expand_operand ops[4];
4650 create_output_operand (&ops[0], target, insn_mode);
4651 create_fixed_operand (&ops[1], arg1_rtx);
4652 create_fixed_operand (&ops[2], arg2_rtx);
4653 create_integer_operand (&ops[3], align);
4654 if (maybe_expand_insn (icode, 4, ops))
4655 return ops[0].value;
4656 return NULL_RTX;
4659 /* Expand expression EXP, which is a call to the memcmp built-in function.
4660 Return NULL_RTX if we failed and the caller should emit a normal call,
4661 otherwise try to get the result in TARGET, if convenient.
4662 RESULT_EQ is true if we can relax the returned value to be either zero
4663 or nonzero, without caring about the sign. */
4665 static rtx
4666 expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4668 if (!validate_arglist (exp,
4669 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4670 return NULL_RTX;
4672 tree arg1 = CALL_EXPR_ARG (exp, 0);
4673 tree arg2 = CALL_EXPR_ARG (exp, 1);
4674 tree len = CALL_EXPR_ARG (exp, 2);
4676 /* Due to the performance benefit, always inline the calls first
4677 when result_eq is false. */
4678 rtx result = NULL_RTX;
4679 enum built_in_function fcode = DECL_FUNCTION_CODE (get_callee_fndecl (exp));
4680 if (!result_eq && fcode != BUILT_IN_BCMP)
4682 result = inline_expand_builtin_bytecmp (exp, target);
4683 if (result)
4684 return result;
4687 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4688 location_t loc = EXPR_LOCATION (exp);
4690 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4691 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4693 /* If we don't have POINTER_TYPE, call the function. */
4694 if (arg1_align == 0 || arg2_align == 0)
4695 return NULL_RTX;
4697 rtx arg1_rtx = get_memory_rtx (arg1, len);
4698 rtx arg2_rtx = get_memory_rtx (arg2, len);
4699 rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4701 /* Set MEM_SIZE as appropriate. */
4702 if (CONST_INT_P (len_rtx))
4704 set_mem_size (arg1_rtx, INTVAL (len_rtx));
4705 set_mem_size (arg2_rtx, INTVAL (len_rtx));
4708 by_pieces_constfn constfn = NULL;
4710 /* Try to get the byte representation of the constant ARG2 (or, only
4711 when the function's result is used for equality to zero, ARG1)
4712 points to, with its byte size in NBYTES. */
4713 unsigned HOST_WIDE_INT nbytes;
4714 const char *rep = getbyterep (arg2, &nbytes);
4715 if (result_eq && rep == NULL)
4717 /* For equality to zero the arguments are interchangeable. */
4718 rep = getbyterep (arg1, &nbytes);
4719 if (rep != NULL)
4720 std::swap (arg1_rtx, arg2_rtx);
4723 /* If the function's constant bound LEN_RTX is less than or equal
4724 to the byte size of the representation of the constant argument,
4725 and if block move would be done by pieces, we can avoid loading
4726 the bytes from memory and only store the computed constant result. */
4727 if (rep
4728 && CONST_INT_P (len_rtx)
4729 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes)
4730 constfn = builtin_memcpy_read_str;
4732 result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4733 TREE_TYPE (len), target,
4734 result_eq, constfn,
4735 CONST_CAST (char *, rep));
4737 if (result)
4739 /* Return the value in the proper mode for this function. */
4740 if (GET_MODE (result) == mode)
4741 return result;
4743 if (target != 0)
4745 convert_move (target, result, 0);
4746 return target;
4749 return convert_to_mode (mode, result, 0);
4752 return NULL_RTX;
4755 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4756 if we failed the caller should emit a normal call, otherwise try to get
4757 the result in TARGET, if convenient. */
4759 static rtx
4760 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4762 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4763 return NULL_RTX;
4765 tree arg1 = CALL_EXPR_ARG (exp, 0);
4766 tree arg2 = CALL_EXPR_ARG (exp, 1);
4768 /* Due to the performance benefit, always inline the calls first. */
4769 rtx result = NULL_RTX;
4770 result = inline_expand_builtin_bytecmp (exp, target);
4771 if (result)
4772 return result;
4774 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4775 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4776 if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4777 return NULL_RTX;
4779 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4780 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4782 /* If we don't have POINTER_TYPE, call the function. */
4783 if (arg1_align == 0 || arg2_align == 0)
4784 return NULL_RTX;
4786 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4787 arg1 = builtin_save_expr (arg1);
4788 arg2 = builtin_save_expr (arg2);
4790 rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4791 rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4793 /* Try to call cmpstrsi. */
4794 if (cmpstr_icode != CODE_FOR_nothing)
4795 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4796 MIN (arg1_align, arg2_align));
4798 /* Try to determine at least one length and call cmpstrnsi. */
4799 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4801 tree len;
4802 rtx arg3_rtx;
4804 tree len1 = c_strlen (arg1, 1);
4805 tree len2 = c_strlen (arg2, 1);
4807 if (len1)
4808 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4809 if (len2)
4810 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4812 /* If we don't have a constant length for the first, use the length
4813 of the second, if we know it. We don't require a constant for
4814 this case; some cost analysis could be done if both are available
4815 but neither is constant. For now, assume they're equally cheap,
4816 unless one has side effects. If both strings have constant lengths,
4817 use the smaller. */
4819 if (!len1)
4820 len = len2;
4821 else if (!len2)
4822 len = len1;
4823 else if (TREE_SIDE_EFFECTS (len1))
4824 len = len2;
4825 else if (TREE_SIDE_EFFECTS (len2))
4826 len = len1;
4827 else if (TREE_CODE (len1) != INTEGER_CST)
4828 len = len2;
4829 else if (TREE_CODE (len2) != INTEGER_CST)
4830 len = len1;
4831 else if (tree_int_cst_lt (len1, len2))
4832 len = len1;
4833 else
4834 len = len2;
4836 /* If both arguments have side effects, we cannot optimize. */
4837 if (len && !TREE_SIDE_EFFECTS (len))
4839 arg3_rtx = expand_normal (len);
4840 result = expand_cmpstrn_or_cmpmem
4841 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4842 arg3_rtx, MIN (arg1_align, arg2_align));
4846 tree fndecl = get_callee_fndecl (exp);
4847 if (result)
4849 /* Return the value in the proper mode for this function. */
4850 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4851 if (GET_MODE (result) == mode)
4852 return result;
4853 if (target == 0)
4854 return convert_to_mode (mode, result, 0);
4855 convert_move (target, result, 0);
4856 return target;
4859 /* Expand the library call ourselves using a stabilized argument
4860 list to avoid re-evaluating the function's arguments twice. */
4861 tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4862 copy_warning (fn, exp);
4863 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4864 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4865 return expand_call (fn, target, target == const0_rtx);
4868 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4869 NULL_RTX if we failed the caller should emit a normal call, otherwise
4870 try to get the result in TARGET, if convenient. */
4872 static rtx
4873 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4874 ATTRIBUTE_UNUSED machine_mode mode)
4876 if (!validate_arglist (exp,
4877 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4878 return NULL_RTX;
4880 tree arg1 = CALL_EXPR_ARG (exp, 0);
4881 tree arg2 = CALL_EXPR_ARG (exp, 1);
4882 tree arg3 = CALL_EXPR_ARG (exp, 2);
4884 location_t loc = EXPR_LOCATION (exp);
4885 tree len1 = c_strlen (arg1, 1);
4886 tree len2 = c_strlen (arg2, 1);
4888 /* Due to the performance benefit, always inline the calls first. */
4889 rtx result = NULL_RTX;
4890 result = inline_expand_builtin_bytecmp (exp, target);
4891 if (result)
4892 return result;
4894 /* If c_strlen can determine an expression for one of the string
4895 lengths, and it doesn't have side effects, then emit cmpstrnsi
4896 using length MIN(strlen(string)+1, arg3). */
4897 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4898 if (cmpstrn_icode == CODE_FOR_nothing)
4899 return NULL_RTX;
4901 tree len;
4903 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4904 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4906 if (len1)
4907 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4908 if (len2)
4909 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4911 tree len3 = fold_convert_loc (loc, sizetype, arg3);
4913 /* If we don't have a constant length for the first, use the length
4914 of the second, if we know it. If neither string is constant length,
4915 use the given length argument. We don't require a constant for
4916 this case; some cost analysis could be done if both are available
4917 but neither is constant. For now, assume they're equally cheap,
4918 unless one has side effects. If both strings have constant lengths,
4919 use the smaller. */
4921 if (!len1 && !len2)
4922 len = len3;
4923 else if (!len1)
4924 len = len2;
4925 else if (!len2)
4926 len = len1;
4927 else if (TREE_SIDE_EFFECTS (len1))
4928 len = len2;
4929 else if (TREE_SIDE_EFFECTS (len2))
4930 len = len1;
4931 else if (TREE_CODE (len1) != INTEGER_CST)
4932 len = len2;
4933 else if (TREE_CODE (len2) != INTEGER_CST)
4934 len = len1;
4935 else if (tree_int_cst_lt (len1, len2))
4936 len = len1;
4937 else
4938 len = len2;
4940 /* If we are not using the given length, we must incorporate it here.
4941 The actual new length parameter will be MIN(len,arg3) in this case. */
4942 if (len != len3)
4944 len = fold_convert_loc (loc, sizetype, len);
4945 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
4947 rtx arg1_rtx = get_memory_rtx (arg1, len);
4948 rtx arg2_rtx = get_memory_rtx (arg2, len);
4949 rtx arg3_rtx = expand_normal (len);
4950 result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4951 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4952 MIN (arg1_align, arg2_align));
4954 tree fndecl = get_callee_fndecl (exp);
4955 if (result)
4957 /* Return the value in the proper mode for this function. */
4958 mode = TYPE_MODE (TREE_TYPE (exp));
4959 if (GET_MODE (result) == mode)
4960 return result;
4961 if (target == 0)
4962 return convert_to_mode (mode, result, 0);
4963 convert_move (target, result, 0);
4964 return target;
4967 /* Expand the library call ourselves using a stabilized argument
4968 list to avoid re-evaluating the function's arguments twice. */
4969 tree call = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
4970 copy_warning (call, exp);
4971 gcc_assert (TREE_CODE (call) == CALL_EXPR);
4972 CALL_EXPR_TAILCALL (call) = CALL_EXPR_TAILCALL (exp);
4973 return expand_call (call, target, target == const0_rtx);
4976 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4977 if that's convenient. */
4980 expand_builtin_saveregs (void)
4982 rtx val;
4983 rtx_insn *seq;
4985 /* Don't do __builtin_saveregs more than once in a function.
4986 Save the result of the first call and reuse it. */
4987 if (saveregs_value != 0)
4988 return saveregs_value;
4990 /* When this function is called, it means that registers must be
4991 saved on entry to this function. So we migrate the call to the
4992 first insn of this function. */
4994 start_sequence ();
4996 /* Do whatever the machine needs done in this case. */
4997 val = targetm.calls.expand_builtin_saveregs ();
4999 seq = get_insns ();
5000 end_sequence ();
5002 saveregs_value = val;
5004 /* Put the insns after the NOTE that starts the function. If this
5005 is inside a start_sequence, make the outer-level insn chain current, so
5006 the code is placed at the start of the function. */
5007 push_topmost_sequence ();
5008 emit_insn_after (seq, entry_of_function ());
5009 pop_topmost_sequence ();
5011 return val;
5014 /* Expand a call to __builtin_next_arg. */
5016 static rtx
5017 expand_builtin_next_arg (void)
5019 /* Checking arguments is already done in fold_builtin_next_arg
5020 that must be called before this function. */
5021 return expand_binop (ptr_mode, add_optab,
5022 crtl->args.internal_arg_pointer,
5023 crtl->args.arg_offset_rtx,
5024 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5027 /* Make it easier for the backends by protecting the valist argument
5028 from multiple evaluations. */
5030 static tree
5031 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
5033 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
5035 /* The current way of determining the type of valist is completely
5036 bogus. We should have the information on the va builtin instead. */
5037 if (!vatype)
5038 vatype = targetm.fn_abi_va_list (cfun->decl);
5040 if (TREE_CODE (vatype) == ARRAY_TYPE)
5042 if (TREE_SIDE_EFFECTS (valist))
5043 valist = save_expr (valist);
5045 /* For this case, the backends will be expecting a pointer to
5046 vatype, but it's possible we've actually been given an array
5047 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
5048 So fix it. */
5049 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
5051 tree p1 = build_pointer_type (TREE_TYPE (vatype));
5052 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
5055 else
5057 tree pt = build_pointer_type (vatype);
5059 if (! needs_lvalue)
5061 if (! TREE_SIDE_EFFECTS (valist))
5062 return valist;
5064 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
5065 TREE_SIDE_EFFECTS (valist) = 1;
5068 if (TREE_SIDE_EFFECTS (valist))
5069 valist = save_expr (valist);
5070 valist = fold_build2_loc (loc, MEM_REF,
5071 vatype, valist, build_int_cst (pt, 0));
5074 return valist;
5077 /* The "standard" definition of va_list is void*. */
5079 tree
5080 std_build_builtin_va_list (void)
5082 return ptr_type_node;
5085 /* The "standard" abi va_list is va_list_type_node. */
5087 tree
5088 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
5090 return va_list_type_node;
5093 /* The "standard" type of va_list is va_list_type_node. */
5095 tree
5096 std_canonical_va_list_type (tree type)
5098 tree wtype, htype;
5100 wtype = va_list_type_node;
5101 htype = type;
5103 if (TREE_CODE (wtype) == ARRAY_TYPE)
5105 /* If va_list is an array type, the argument may have decayed
5106 to a pointer type, e.g. by being passed to another function.
5107 In that case, unwrap both types so that we can compare the
5108 underlying records. */
5109 if (TREE_CODE (htype) == ARRAY_TYPE
5110 || POINTER_TYPE_P (htype))
5112 wtype = TREE_TYPE (wtype);
5113 htype = TREE_TYPE (htype);
5116 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
5117 return va_list_type_node;
5119 return NULL_TREE;
5122 /* The "standard" implementation of va_start: just assign `nextarg' to
5123 the variable. */
5125 void
5126 std_expand_builtin_va_start (tree valist, rtx nextarg)
5128 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
5129 convert_move (va_r, nextarg, 0);
5132 /* Expand EXP, a call to __builtin_va_start. */
5134 static rtx
5135 expand_builtin_va_start (tree exp)
5137 rtx nextarg;
5138 tree valist;
5139 location_t loc = EXPR_LOCATION (exp);
5141 if (call_expr_nargs (exp) < 2)
5143 error_at (loc, "too few arguments to function %<va_start%>");
5144 return const0_rtx;
5147 if (fold_builtin_next_arg (exp, true))
5148 return const0_rtx;
5150 nextarg = expand_builtin_next_arg ();
5151 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
5153 if (targetm.expand_builtin_va_start)
5154 targetm.expand_builtin_va_start (valist, nextarg);
5155 else
5156 std_expand_builtin_va_start (valist, nextarg);
5158 return const0_rtx;
5161 /* Expand EXP, a call to __builtin_va_end. */
5163 static rtx
5164 expand_builtin_va_end (tree exp)
5166 tree valist = CALL_EXPR_ARG (exp, 0);
5168 /* Evaluate for side effects, if needed. I hate macros that don't
5169 do that. */
5170 if (TREE_SIDE_EFFECTS (valist))
5171 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
5173 return const0_rtx;
5176 /* Expand EXP, a call to __builtin_va_copy. We do this as a
5177 builtin rather than just as an assignment in stdarg.h because of the
5178 nastiness of array-type va_list types. */
5180 static rtx
5181 expand_builtin_va_copy (tree exp)
5183 tree dst, src, t;
5184 location_t loc = EXPR_LOCATION (exp);
5186 dst = CALL_EXPR_ARG (exp, 0);
5187 src = CALL_EXPR_ARG (exp, 1);
5189 dst = stabilize_va_list_loc (loc, dst, 1);
5190 src = stabilize_va_list_loc (loc, src, 0);
5192 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5194 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
5196 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
5197 TREE_SIDE_EFFECTS (t) = 1;
5198 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5200 else
5202 rtx dstb, srcb, size;
5204 /* Evaluate to pointers. */
5205 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5206 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5207 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5208 NULL_RTX, VOIDmode, EXPAND_NORMAL);
5210 dstb = convert_memory_address (Pmode, dstb);
5211 srcb = convert_memory_address (Pmode, srcb);
5213 /* "Dereference" to BLKmode memories. */
5214 dstb = gen_rtx_MEM (BLKmode, dstb);
5215 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5216 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5217 srcb = gen_rtx_MEM (BLKmode, srcb);
5218 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5219 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5221 /* Copy. */
5222 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5225 return const0_rtx;
5228 /* Expand a call to one of the builtin functions __builtin_frame_address or
5229 __builtin_return_address. */
5231 static rtx
5232 expand_builtin_frame_address (tree fndecl, tree exp)
5234 /* The argument must be a nonnegative integer constant.
5235 It counts the number of frames to scan up the stack.
5236 The value is either the frame pointer value or the return
5237 address saved in that frame. */
5238 if (call_expr_nargs (exp) == 0)
5239 /* Warning about missing arg was already issued. */
5240 return const0_rtx;
5241 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
5243 error ("invalid argument to %qD", fndecl);
5244 return const0_rtx;
5246 else
5248 /* Number of frames to scan up the stack. */
5249 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
5251 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
5253 /* Some ports cannot access arbitrary stack frames. */
5254 if (tem == NULL)
5256 warning (0, "unsupported argument to %qD", fndecl);
5257 return const0_rtx;
5260 if (count)
5262 /* Warn since no effort is made to ensure that any frame
5263 beyond the current one exists or can be safely reached. */
5264 warning (OPT_Wframe_address, "calling %qD with "
5265 "a nonzero argument is unsafe", fndecl);
5268 /* For __builtin_frame_address, return what we've got. */
5269 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5270 return tem;
5272 if (!REG_P (tem)
5273 && ! CONSTANT_P (tem))
5274 tem = copy_addr_to_reg (tem);
5275 return tem;
5279 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5280 failed and the caller should emit a normal call. */
5282 static rtx
5283 expand_builtin_alloca (tree exp)
5285 rtx op0;
5286 rtx result;
5287 unsigned int align;
5288 tree fndecl = get_callee_fndecl (exp);
5289 HOST_WIDE_INT max_size;
5290 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5291 bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5292 bool valid_arglist
5293 = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5294 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5295 VOID_TYPE)
5296 : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5297 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5298 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5300 if (!valid_arglist)
5301 return NULL_RTX;
5303 /* Compute the argument. */
5304 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5306 /* Compute the alignment. */
5307 align = (fcode == BUILT_IN_ALLOCA
5308 ? BIGGEST_ALIGNMENT
5309 : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5311 /* Compute the maximum size. */
5312 max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5313 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2))
5314 : -1);
5316 /* Allocate the desired space. If the allocation stems from the declaration
5317 of a variable-sized object, it cannot accumulate. */
5318 result
5319 = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5320 result = convert_memory_address (ptr_mode, result);
5322 /* Dynamic allocations for variables are recorded during gimplification. */
5323 if (!alloca_for_var && (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC))
5324 record_dynamic_alloc (exp);
5326 return result;
5329 /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
5330 of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
5331 STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
5332 handle_builtin_stack_restore function. */
5334 static rtx
5335 expand_asan_emit_allocas_unpoison (tree exp)
5337 tree arg0 = CALL_EXPR_ARG (exp, 0);
5338 tree arg1 = CALL_EXPR_ARG (exp, 1);
5339 rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5340 rtx bot = expand_expr (arg1, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5341 rtx off = expand_simple_binop (Pmode, MINUS, virtual_stack_dynamic_rtx,
5342 stack_pointer_rtx, NULL_RTX, 0,
5343 OPTAB_LIB_WIDEN);
5344 off = convert_modes (ptr_mode, Pmode, off, 0);
5345 bot = expand_simple_binop (ptr_mode, PLUS, bot, off, NULL_RTX, 0,
5346 OPTAB_LIB_WIDEN);
5347 rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
5348 ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
5349 top, ptr_mode, bot, ptr_mode);
5350 return ret;
5353 /* Expand a call to bswap builtin in EXP.
5354 Return NULL_RTX if a normal call should be emitted rather than expanding the
5355 function in-line. If convenient, the result should be placed in TARGET.
5356 SUBTARGET may be used as the target for computing one of EXP's operands. */
5358 static rtx
5359 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5360 rtx subtarget)
5362 tree arg;
5363 rtx op0;
5365 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5366 return NULL_RTX;
5368 arg = CALL_EXPR_ARG (exp, 0);
5369 op0 = expand_expr (arg,
5370 subtarget && GET_MODE (subtarget) == target_mode
5371 ? subtarget : NULL_RTX,
5372 target_mode, EXPAND_NORMAL);
5373 if (GET_MODE (op0) != target_mode)
5374 op0 = convert_to_mode (target_mode, op0, 1);
5376 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5378 gcc_assert (target);
5380 return convert_to_mode (target_mode, target, 1);
5383 /* Expand a call to a unary builtin in EXP.
5384 Return NULL_RTX if a normal call should be emitted rather than expanding the
5385 function in-line. If convenient, the result should be placed in TARGET.
5386 SUBTARGET may be used as the target for computing one of EXP's operands. */
5388 static rtx
5389 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5390 rtx subtarget, optab op_optab)
5392 rtx op0;
5394 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5395 return NULL_RTX;
5397 /* Compute the argument. */
5398 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5399 (subtarget
5400 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5401 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5402 VOIDmode, EXPAND_NORMAL);
5403 /* Compute op, into TARGET if possible.
5404 Set TARGET to wherever the result comes back. */
5405 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5406 op_optab, op0, target, op_optab != clrsb_optab);
5407 gcc_assert (target);
5409 return convert_to_mode (target_mode, target, 0);
5412 /* Expand a call to __builtin_expect. We just return our argument
5413 as the builtin_expect semantic should've been already executed by
5414 tree branch prediction pass. */
5416 static rtx
5417 expand_builtin_expect (tree exp, rtx target)
5419 tree arg;
5421 if (call_expr_nargs (exp) < 2)
5422 return const0_rtx;
5423 arg = CALL_EXPR_ARG (exp, 0);
5425 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5426 /* When guessing was done, the hints should be already stripped away. */
5427 gcc_assert (!flag_guess_branch_prob
5428 || optimize == 0 || seen_error ());
5429 return target;
5432 /* Expand a call to __builtin_expect_with_probability. We just return our
5433 argument as the builtin_expect semantic should've been already executed by
5434 tree branch prediction pass. */
5436 static rtx
5437 expand_builtin_expect_with_probability (tree exp, rtx target)
5439 tree arg;
5441 if (call_expr_nargs (exp) < 3)
5442 return const0_rtx;
5443 arg = CALL_EXPR_ARG (exp, 0);
5445 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5446 /* When guessing was done, the hints should be already stripped away. */
5447 gcc_assert (!flag_guess_branch_prob
5448 || optimize == 0 || seen_error ());
5449 return target;
5453 /* Expand a call to __builtin_assume_aligned. We just return our first
5454 argument as the builtin_assume_aligned semantic should've been already
5455 executed by CCP. */
5457 static rtx
5458 expand_builtin_assume_aligned (tree exp, rtx target)
5460 if (call_expr_nargs (exp) < 2)
5461 return const0_rtx;
5462 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
5463 EXPAND_NORMAL);
5464 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
5465 && (call_expr_nargs (exp) < 3
5466 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
5467 return target;
5470 void
5471 expand_builtin_trap (void)
5473 if (targetm.have_trap ())
5475 rtx_insn *insn = emit_insn (targetm.gen_trap ());
5476 /* For trap insns when not accumulating outgoing args force
5477 REG_ARGS_SIZE note to prevent crossjumping of calls with
5478 different args sizes. */
5479 if (!ACCUMULATE_OUTGOING_ARGS)
5480 add_args_size_note (insn, stack_pointer_delta);
5482 else
5484 tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
5485 tree call_expr = build_call_expr (fn, 0);
5486 expand_call (call_expr, NULL_RTX, false);
5489 emit_barrier ();
5492 /* Expand a call to __builtin_unreachable. We do nothing except emit
5493 a barrier saying that control flow will not pass here.
5495 It is the responsibility of the program being compiled to ensure
5496 that control flow does never reach __builtin_unreachable. */
5497 static void
5498 expand_builtin_unreachable (void)
5500 /* Use gimple_build_builtin_unreachable or builtin_decl_unreachable
5501 to avoid this. */
5502 gcc_checking_assert (!sanitize_flags_p (SANITIZE_UNREACHABLE));
5503 emit_barrier ();
5506 /* Expand EXP, a call to fabs, fabsf or fabsl.
5507 Return NULL_RTX if a normal call should be emitted rather than expanding
5508 the function inline. If convenient, the result should be placed
5509 in TARGET. SUBTARGET may be used as the target for computing
5510 the operand. */
5512 static rtx
5513 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5515 machine_mode mode;
5516 tree arg;
5517 rtx op0;
5519 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5520 return NULL_RTX;
5522 arg = CALL_EXPR_ARG (exp, 0);
5523 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5524 mode = TYPE_MODE (TREE_TYPE (arg));
5525 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5526 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5529 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5530 Return NULL is a normal call should be emitted rather than expanding the
5531 function inline. If convenient, the result should be placed in TARGET.
5532 SUBTARGET may be used as the target for computing the operand. */
5534 static rtx
5535 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5537 rtx op0, op1;
5538 tree arg;
5540 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5541 return NULL_RTX;
5543 arg = CALL_EXPR_ARG (exp, 0);
5544 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5546 arg = CALL_EXPR_ARG (exp, 1);
5547 op1 = expand_normal (arg);
5549 return expand_copysign (op0, op1, target);
5552 /* Emit a call to __builtin___clear_cache. */
5554 void
5555 default_emit_call_builtin___clear_cache (rtx begin, rtx end)
5557 rtx callee = gen_rtx_SYMBOL_REF (Pmode,
5558 BUILTIN_ASM_NAME_PTR
5559 (BUILT_IN_CLEAR_CACHE));
5561 emit_library_call (callee,
5562 LCT_NORMAL, VOIDmode,
5563 convert_memory_address (ptr_mode, begin), ptr_mode,
5564 convert_memory_address (ptr_mode, end), ptr_mode);
5567 /* Emit a call to __builtin___clear_cache, unless the target specifies
5568 it as do-nothing. This function can be used by trampoline
5569 finalizers to duplicate the effects of expanding a call to the
5570 clear_cache builtin. */
5572 void
5573 maybe_emit_call_builtin___clear_cache (rtx begin, rtx end)
5575 gcc_assert ((GET_MODE (begin) == ptr_mode || GET_MODE (begin) == Pmode
5576 || CONST_INT_P (begin))
5577 && (GET_MODE (end) == ptr_mode || GET_MODE (end) == Pmode
5578 || CONST_INT_P (end)));
5580 if (targetm.have_clear_cache ())
5582 /* We have a "clear_cache" insn, and it will handle everything. */
5583 class expand_operand ops[2];
5585 create_address_operand (&ops[0], begin);
5586 create_address_operand (&ops[1], end);
5588 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
5589 return;
5591 else
5593 #ifndef CLEAR_INSN_CACHE
5594 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5595 does nothing. There is no need to call it. Do nothing. */
5596 return;
5597 #endif /* CLEAR_INSN_CACHE */
5600 targetm.calls.emit_call_builtin___clear_cache (begin, end);
5603 /* Expand a call to __builtin___clear_cache. */
5605 static void
5606 expand_builtin___clear_cache (tree exp)
5608 tree begin, end;
5609 rtx begin_rtx, end_rtx;
5611 /* We must not expand to a library call. If we did, any
5612 fallback library function in libgcc that might contain a call to
5613 __builtin___clear_cache() would recurse infinitely. */
5614 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5616 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5617 return;
5620 begin = CALL_EXPR_ARG (exp, 0);
5621 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5623 end = CALL_EXPR_ARG (exp, 1);
5624 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5626 maybe_emit_call_builtin___clear_cache (begin_rtx, end_rtx);
5629 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5631 static rtx
5632 round_trampoline_addr (rtx tramp)
5634 rtx temp, addend, mask;
5636 /* If we don't need too much alignment, we'll have been guaranteed
5637 proper alignment by get_trampoline_type. */
5638 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5639 return tramp;
5641 /* Round address up to desired boundary. */
5642 temp = gen_reg_rtx (Pmode);
5643 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
5644 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
5646 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5647 temp, 0, OPTAB_LIB_WIDEN);
5648 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5649 temp, 0, OPTAB_LIB_WIDEN);
5651 return tramp;
5654 static rtx
5655 expand_builtin_init_trampoline (tree exp, bool onstack)
5657 tree t_tramp, t_func, t_chain;
5658 rtx m_tramp, r_tramp, r_chain, tmp;
5660 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5661 POINTER_TYPE, VOID_TYPE))
5662 return NULL_RTX;
5664 t_tramp = CALL_EXPR_ARG (exp, 0);
5665 t_func = CALL_EXPR_ARG (exp, 1);
5666 t_chain = CALL_EXPR_ARG (exp, 2);
5668 r_tramp = expand_normal (t_tramp);
5669 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5670 MEM_NOTRAP_P (m_tramp) = 1;
5672 /* If ONSTACK, the TRAMP argument should be the address of a field
5673 within the local function's FRAME decl. Either way, let's see if
5674 we can fill in the MEM_ATTRs for this memory. */
5675 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5676 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
5678 /* Creator of a heap trampoline is responsible for making sure the
5679 address is aligned to at least STACK_BOUNDARY. Normally malloc
5680 will ensure this anyhow. */
5681 tmp = round_trampoline_addr (r_tramp);
5682 if (tmp != r_tramp)
5684 m_tramp = change_address (m_tramp, BLKmode, tmp);
5685 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5686 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
5689 /* The FUNC argument should be the address of the nested function.
5690 Extract the actual function decl to pass to the hook. */
5691 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5692 t_func = TREE_OPERAND (t_func, 0);
5693 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5695 r_chain = expand_normal (t_chain);
5697 /* Generate insns to initialize the trampoline. */
5698 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5700 if (onstack)
5702 trampolines_created = 1;
5704 if (targetm.calls.custom_function_descriptors != 0)
5705 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5706 "trampoline generated for nested function %qD", t_func);
5709 return const0_rtx;
5712 static rtx
5713 expand_builtin_adjust_trampoline (tree exp)
5715 rtx tramp;
5717 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5718 return NULL_RTX;
5720 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5721 tramp = round_trampoline_addr (tramp);
5722 if (targetm.calls.trampoline_adjust_address)
5723 tramp = targetm.calls.trampoline_adjust_address (tramp);
5725 return tramp;
5728 /* Expand a call to the builtin descriptor initialization routine.
5729 A descriptor is made up of a couple of pointers to the static
5730 chain and the code entry in this order. */
5732 static rtx
5733 expand_builtin_init_descriptor (tree exp)
5735 tree t_descr, t_func, t_chain;
5736 rtx m_descr, r_descr, r_func, r_chain;
5738 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
5739 VOID_TYPE))
5740 return NULL_RTX;
5742 t_descr = CALL_EXPR_ARG (exp, 0);
5743 t_func = CALL_EXPR_ARG (exp, 1);
5744 t_chain = CALL_EXPR_ARG (exp, 2);
5746 r_descr = expand_normal (t_descr);
5747 m_descr = gen_rtx_MEM (BLKmode, r_descr);
5748 MEM_NOTRAP_P (m_descr) = 1;
5749 set_mem_align (m_descr, GET_MODE_ALIGNMENT (ptr_mode));
5751 r_func = expand_normal (t_func);
5752 r_chain = expand_normal (t_chain);
5754 /* Generate insns to initialize the descriptor. */
5755 emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
5756 emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
5757 POINTER_SIZE / BITS_PER_UNIT), r_func);
5759 return const0_rtx;
5762 /* Expand a call to the builtin descriptor adjustment routine. */
5764 static rtx
5765 expand_builtin_adjust_descriptor (tree exp)
5767 rtx tramp;
5769 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5770 return NULL_RTX;
5772 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5774 /* Unalign the descriptor to allow runtime identification. */
5775 tramp = plus_constant (ptr_mode, tramp,
5776 targetm.calls.custom_function_descriptors);
5778 return force_operand (tramp, NULL_RTX);
5781 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5782 function. The function first checks whether the back end provides
5783 an insn to implement signbit for the respective mode. If not, it
5784 checks whether the floating point format of the value is such that
5785 the sign bit can be extracted. If that is not the case, error out.
5786 EXP is the expression that is a call to the builtin function; if
5787 convenient, the result should be placed in TARGET. */
5788 static rtx
5789 expand_builtin_signbit (tree exp, rtx target)
5791 const struct real_format *fmt;
5792 scalar_float_mode fmode;
5793 scalar_int_mode rmode, imode;
5794 tree arg;
5795 int word, bitpos;
5796 enum insn_code icode;
5797 rtx temp;
5798 location_t loc = EXPR_LOCATION (exp);
5800 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5801 return NULL_RTX;
5803 arg = CALL_EXPR_ARG (exp, 0);
5804 fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
5805 rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5806 fmt = REAL_MODE_FORMAT (fmode);
5808 arg = builtin_save_expr (arg);
5810 /* Expand the argument yielding a RTX expression. */
5811 temp = expand_normal (arg);
5813 /* Check if the back end provides an insn that handles signbit for the
5814 argument's mode. */
5815 icode = optab_handler (signbit_optab, fmode);
5816 if (icode != CODE_FOR_nothing)
5818 rtx_insn *last = get_last_insn ();
5819 rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5820 if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
5821 return this_target;
5822 delete_insns_since (last);
5825 /* For floating point formats without a sign bit, implement signbit
5826 as "ARG < 0.0". */
5827 bitpos = fmt->signbit_ro;
5828 if (bitpos < 0)
5830 /* But we can't do this if the format supports signed zero. */
5831 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
5833 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5834 build_real (TREE_TYPE (arg), dconst0));
5835 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5838 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5840 imode = int_mode_for_mode (fmode).require ();
5841 temp = gen_lowpart (imode, temp);
5843 else
5845 imode = word_mode;
5846 /* Handle targets with different FP word orders. */
5847 if (FLOAT_WORDS_BIG_ENDIAN)
5848 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5849 else
5850 word = bitpos / BITS_PER_WORD;
5851 temp = operand_subword_force (temp, word, fmode);
5852 bitpos = bitpos % BITS_PER_WORD;
5855 /* Force the intermediate word_mode (or narrower) result into a
5856 register. This avoids attempting to create paradoxical SUBREGs
5857 of floating point modes below. */
5858 temp = force_reg (imode, temp);
5860 /* If the bitpos is within the "result mode" lowpart, the operation
5861 can be implement with a single bitwise AND. Otherwise, we need
5862 a right shift and an AND. */
5864 if (bitpos < GET_MODE_BITSIZE (rmode))
5866 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5868 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5869 temp = gen_lowpart (rmode, temp);
5870 temp = expand_binop (rmode, and_optab, temp,
5871 immed_wide_int_const (mask, rmode),
5872 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5874 else
5876 /* Perform a logical right shift to place the signbit in the least
5877 significant bit, then truncate the result to the desired mode
5878 and mask just this bit. */
5879 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5880 temp = gen_lowpart (rmode, temp);
5881 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5882 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5885 return temp;
5888 /* Expand fork or exec calls. TARGET is the desired target of the
5889 call. EXP is the call. FN is the
5890 identificator of the actual function. IGNORE is nonzero if the
5891 value is to be ignored. */
5893 static rtx
5894 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5896 tree id, decl;
5897 tree call;
5899 /* If we are not profiling, just call the function. */
5900 if (!profile_arc_flag)
5901 return NULL_RTX;
5903 /* Otherwise call the wrapper. This should be equivalent for the rest of
5904 compiler, so the code does not diverge, and the wrapper may run the
5905 code necessary for keeping the profiling sane. */
5907 switch (DECL_FUNCTION_CODE (fn))
5909 case BUILT_IN_FORK:
5910 id = get_identifier ("__gcov_fork");
5911 break;
5913 case BUILT_IN_EXECL:
5914 id = get_identifier ("__gcov_execl");
5915 break;
5917 case BUILT_IN_EXECV:
5918 id = get_identifier ("__gcov_execv");
5919 break;
5921 case BUILT_IN_EXECLP:
5922 id = get_identifier ("__gcov_execlp");
5923 break;
5925 case BUILT_IN_EXECLE:
5926 id = get_identifier ("__gcov_execle");
5927 break;
5929 case BUILT_IN_EXECVP:
5930 id = get_identifier ("__gcov_execvp");
5931 break;
5933 case BUILT_IN_EXECVE:
5934 id = get_identifier ("__gcov_execve");
5935 break;
5937 default:
5938 gcc_unreachable ();
5941 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5942 FUNCTION_DECL, id, TREE_TYPE (fn));
5943 DECL_EXTERNAL (decl) = 1;
5944 TREE_PUBLIC (decl) = 1;
5945 DECL_ARTIFICIAL (decl) = 1;
5946 TREE_NOTHROW (decl) = 1;
5947 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5948 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5949 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5950 return expand_call (call, target, ignore);
5955 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5956 the pointer in these functions is void*, the tree optimizers may remove
5957 casts. The mode computed in expand_builtin isn't reliable either, due
5958 to __sync_bool_compare_and_swap.
5960 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5961 group of builtins. This gives us log2 of the mode size. */
5963 static inline machine_mode
5964 get_builtin_sync_mode (int fcode_diff)
5966 /* The size is not negotiable, so ask not to get BLKmode in return
5967 if the target indicates that a smaller size would be better. */
5968 return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require ();
5971 /* Expand the memory expression LOC and return the appropriate memory operand
5972 for the builtin_sync operations. */
5974 static rtx
5975 get_builtin_sync_mem (tree loc, machine_mode mode)
5977 rtx addr, mem;
5978 int addr_space = TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc))
5979 ? TREE_TYPE (TREE_TYPE (loc))
5980 : TREE_TYPE (loc));
5981 scalar_int_mode addr_mode = targetm.addr_space.address_mode (addr_space);
5983 addr = expand_expr (loc, NULL_RTX, addr_mode, EXPAND_SUM);
5984 addr = convert_memory_address (addr_mode, addr);
5986 /* Note that we explicitly do not want any alias information for this
5987 memory, so that we kill all other live memories. Otherwise we don't
5988 satisfy the full barrier semantics of the intrinsic. */
5989 mem = gen_rtx_MEM (mode, addr);
5991 set_mem_addr_space (mem, addr_space);
5993 mem = validize_mem (mem);
5995 /* The alignment needs to be at least according to that of the mode. */
5996 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5997 get_pointer_alignment (loc)));
5998 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5999 MEM_VOLATILE_P (mem) = 1;
6001 return mem;
6004 /* Make sure an argument is in the right mode.
6005 EXP is the tree argument.
6006 MODE is the mode it should be in. */
6008 static rtx
6009 expand_expr_force_mode (tree exp, machine_mode mode)
6011 rtx val;
6012 machine_mode old_mode;
6014 if (TREE_CODE (exp) == SSA_NAME
6015 && TYPE_MODE (TREE_TYPE (exp)) != mode)
6017 /* Undo argument promotion if possible, as combine might not
6018 be able to do it later due to MEM_VOLATILE_P uses in the
6019 patterns. */
6020 gimple *g = get_gimple_for_ssa_name (exp);
6021 if (g && gimple_assign_cast_p (g))
6023 tree rhs = gimple_assign_rhs1 (g);
6024 tree_code code = gimple_assign_rhs_code (g);
6025 if (CONVERT_EXPR_CODE_P (code)
6026 && TYPE_MODE (TREE_TYPE (rhs)) == mode
6027 && INTEGRAL_TYPE_P (TREE_TYPE (exp))
6028 && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
6029 && (TYPE_PRECISION (TREE_TYPE (exp))
6030 > TYPE_PRECISION (TREE_TYPE (rhs))))
6031 exp = rhs;
6035 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
6036 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6037 of CONST_INTs, where we know the old_mode only from the call argument. */
6039 old_mode = GET_MODE (val);
6040 if (old_mode == VOIDmode)
6041 old_mode = TYPE_MODE (TREE_TYPE (exp));
6042 val = convert_modes (mode, old_mode, val, 1);
6043 return val;
6047 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6048 EXP is the CALL_EXPR. CODE is the rtx code
6049 that corresponds to the arithmetic or logical operation from the name;
6050 an exception here is that NOT actually means NAND. TARGET is an optional
6051 place for us to store the results; AFTER is true if this is the
6052 fetch_and_xxx form. */
6054 static rtx
6055 expand_builtin_sync_operation (machine_mode mode, tree exp,
6056 enum rtx_code code, bool after,
6057 rtx target)
6059 rtx val, mem;
6060 location_t loc = EXPR_LOCATION (exp);
6062 if (code == NOT && warn_sync_nand)
6064 tree fndecl = get_callee_fndecl (exp);
6065 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6067 static bool warned_f_a_n, warned_n_a_f;
6069 switch (fcode)
6071 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6072 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6073 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6074 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6075 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6076 if (warned_f_a_n)
6077 break;
6079 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
6080 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6081 warned_f_a_n = true;
6082 break;
6084 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6085 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6086 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6087 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6088 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6089 if (warned_n_a_f)
6090 break;
6092 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
6093 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6094 warned_n_a_f = true;
6095 break;
6097 default:
6098 gcc_unreachable ();
6102 /* Expand the operands. */
6103 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6104 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6106 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
6107 after);
6110 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6111 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6112 true if this is the boolean form. TARGET is a place for us to store the
6113 results; this is NOT optional if IS_BOOL is true. */
6115 static rtx
6116 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
6117 bool is_bool, rtx target)
6119 rtx old_val, new_val, mem;
6120 rtx *pbool, *poval;
6122 /* Expand the operands. */
6123 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6124 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6125 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6127 pbool = poval = NULL;
6128 if (target != const0_rtx)
6130 if (is_bool)
6131 pbool = &target;
6132 else
6133 poval = &target;
6135 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
6136 false, MEMMODEL_SYNC_SEQ_CST,
6137 MEMMODEL_SYNC_SEQ_CST))
6138 return NULL_RTX;
6140 return target;
6143 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6144 general form is actually an atomic exchange, and some targets only
6145 support a reduced form with the second argument being a constant 1.
6146 EXP is the CALL_EXPR; TARGET is an optional place for us to store
6147 the results. */
6149 static rtx
6150 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
6151 rtx target)
6153 rtx val, mem;
6155 /* Expand the operands. */
6156 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6157 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6159 return expand_sync_lock_test_and_set (target, mem, val);
6162 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6164 static void
6165 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
6167 rtx mem;
6169 /* Expand the operands. */
6170 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6172 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
6175 /* Given an integer representing an ``enum memmodel'', verify its
6176 correctness and return the memory model enum. */
6178 static enum memmodel
6179 get_memmodel (tree exp)
6181 /* If the parameter is not a constant, it's a run time value so we'll just
6182 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
6183 if (TREE_CODE (exp) != INTEGER_CST)
6184 return MEMMODEL_SEQ_CST;
6186 rtx op = expand_normal (exp);
6188 unsigned HOST_WIDE_INT val = INTVAL (op);
6189 if (targetm.memmodel_check)
6190 val = targetm.memmodel_check (val);
6191 else if (val & ~MEMMODEL_MASK)
6192 return MEMMODEL_SEQ_CST;
6194 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
6195 if (memmodel_base (val) >= MEMMODEL_LAST)
6196 return MEMMODEL_SEQ_CST;
6198 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
6199 be conservative and promote consume to acquire. */
6200 if (val == MEMMODEL_CONSUME)
6201 val = MEMMODEL_ACQUIRE;
6203 return (enum memmodel) val;
6206 /* Expand the __atomic_exchange intrinsic:
6207 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
6208 EXP is the CALL_EXPR.
6209 TARGET is an optional place for us to store the results. */
6211 static rtx
6212 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
6214 rtx val, mem;
6215 enum memmodel model;
6217 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6219 if (!flag_inline_atomics)
6220 return NULL_RTX;
6222 /* Expand the operands. */
6223 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6224 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6226 return expand_atomic_exchange (target, mem, val, model);
6229 /* Expand the __atomic_compare_exchange intrinsic:
6230 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
6231 TYPE desired, BOOL weak,
6232 enum memmodel success,
6233 enum memmodel failure)
6234 EXP is the CALL_EXPR.
6235 TARGET is an optional place for us to store the results. */
6237 static rtx
6238 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
6239 rtx target)
6241 rtx expect, desired, mem, oldval;
6242 rtx_code_label *label;
6243 tree weak;
6244 bool is_weak;
6246 memmodel success = get_memmodel (CALL_EXPR_ARG (exp, 4));
6247 memmodel failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
6249 if (failure > success)
6250 success = MEMMODEL_SEQ_CST;
6252 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6254 failure = MEMMODEL_SEQ_CST;
6255 success = MEMMODEL_SEQ_CST;
6259 if (!flag_inline_atomics)
6260 return NULL_RTX;
6262 /* Expand the operands. */
6263 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6265 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
6266 expect = convert_memory_address (Pmode, expect);
6267 expect = gen_rtx_MEM (mode, expect);
6268 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6270 weak = CALL_EXPR_ARG (exp, 3);
6271 is_weak = false;
6272 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
6273 is_weak = true;
6275 if (target == const0_rtx)
6276 target = NULL;
6278 /* Lest the rtl backend create a race condition with an imporoper store
6279 to memory, always create a new pseudo for OLDVAL. */
6280 oldval = NULL;
6282 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
6283 is_weak, success, failure))
6284 return NULL_RTX;
6286 /* Conditionally store back to EXPECT, lest we create a race condition
6287 with an improper store to memory. */
6288 /* ??? With a rearrangement of atomics at the gimple level, we can handle
6289 the normal case where EXPECT is totally private, i.e. a register. At
6290 which point the store can be unconditional. */
6291 label = gen_label_rtx ();
6292 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
6293 GET_MODE (target), 1, label);
6294 emit_move_insn (expect, oldval);
6295 emit_label (label);
6297 return target;
6300 /* Helper function for expand_ifn_atomic_compare_exchange - expand
6301 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
6302 call. The weak parameter must be dropped to match the expected parameter
6303 list and the expected argument changed from value to pointer to memory
6304 slot. */
6306 static void
6307 expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
6309 unsigned int z;
6310 vec<tree, va_gc> *vec;
6312 vec_alloc (vec, 5);
6313 vec->quick_push (gimple_call_arg (call, 0));
6314 tree expected = gimple_call_arg (call, 1);
6315 rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
6316 TREE_TYPE (expected));
6317 rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
6318 if (expd != x)
6319 emit_move_insn (x, expd);
6320 tree v = make_tree (TREE_TYPE (expected), x);
6321 vec->quick_push (build1 (ADDR_EXPR,
6322 build_pointer_type (TREE_TYPE (expected)), v));
6323 vec->quick_push (gimple_call_arg (call, 2));
6324 /* Skip the boolean weak parameter. */
6325 for (z = 4; z < 6; z++)
6326 vec->quick_push (gimple_call_arg (call, z));
6327 /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6328 unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
6329 gcc_assert (bytes_log2 < 5);
6330 built_in_function fncode
6331 = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6332 + bytes_log2);
6333 tree fndecl = builtin_decl_explicit (fncode);
6334 tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
6335 fndecl);
6336 tree exp = build_call_vec (boolean_type_node, fn, vec);
6337 tree lhs = gimple_call_lhs (call);
6338 rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
6339 if (lhs)
6341 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6342 if (GET_MODE (boolret) != mode)
6343 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6344 x = force_reg (mode, x);
6345 write_complex_part (target, boolret, true, true);
6346 write_complex_part (target, x, false, false);
6350 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6352 void
6353 expand_ifn_atomic_compare_exchange (gcall *call)
6355 int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6356 gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6357 machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6359 memmodel success = get_memmodel (gimple_call_arg (call, 4));
6360 memmodel failure = get_memmodel (gimple_call_arg (call, 5));
6362 if (failure > success)
6363 success = MEMMODEL_SEQ_CST;
6365 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6367 failure = MEMMODEL_SEQ_CST;
6368 success = MEMMODEL_SEQ_CST;
6371 if (!flag_inline_atomics)
6373 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6374 return;
6377 /* Expand the operands. */
6378 rtx mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6380 rtx expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6381 rtx desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6383 bool is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6385 rtx boolret = NULL;
6386 rtx oldval = NULL;
6388 if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
6389 is_weak, success, failure))
6391 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6392 return;
6395 tree lhs = gimple_call_lhs (call);
6396 if (lhs)
6398 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6399 if (GET_MODE (boolret) != mode)
6400 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6401 write_complex_part (target, boolret, true, true);
6402 write_complex_part (target, oldval, false, false);
6406 /* Expand the __atomic_load intrinsic:
6407 TYPE __atomic_load (TYPE *object, enum memmodel)
6408 EXP is the CALL_EXPR.
6409 TARGET is an optional place for us to store the results. */
6411 static rtx
6412 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6414 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6415 if (is_mm_release (model) || is_mm_acq_rel (model))
6416 model = MEMMODEL_SEQ_CST;
6418 if (!flag_inline_atomics)
6419 return NULL_RTX;
6421 /* Expand the operand. */
6422 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6424 return expand_atomic_load (target, mem, model);
6428 /* Expand the __atomic_store intrinsic:
6429 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6430 EXP is the CALL_EXPR.
6431 TARGET is an optional place for us to store the results. */
6433 static rtx
6434 expand_builtin_atomic_store (machine_mode mode, tree exp)
6436 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6437 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6438 || is_mm_release (model)))
6439 model = MEMMODEL_SEQ_CST;
6441 if (!flag_inline_atomics)
6442 return NULL_RTX;
6444 /* Expand the operands. */
6445 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6446 rtx val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6448 return expand_atomic_store (mem, val, model, false);
6451 /* Expand the __atomic_fetch_XXX intrinsic:
6452 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6453 EXP is the CALL_EXPR.
6454 TARGET is an optional place for us to store the results.
6455 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6456 FETCH_AFTER is true if returning the result of the operation.
6457 FETCH_AFTER is false if returning the value before the operation.
6458 IGNORE is true if the result is not used.
6459 EXT_CALL is the correct builtin for an external call if this cannot be
6460 resolved to an instruction sequence. */
6462 static rtx
6463 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
6464 enum rtx_code code, bool fetch_after,
6465 bool ignore, enum built_in_function ext_call)
6467 rtx val, mem, ret;
6468 enum memmodel model;
6469 tree fndecl;
6470 tree addr;
6472 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6474 /* Expand the operands. */
6475 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6476 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6478 /* Only try generating instructions if inlining is turned on. */
6479 if (flag_inline_atomics)
6481 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6482 if (ret)
6483 return ret;
6486 /* Return if a different routine isn't needed for the library call. */
6487 if (ext_call == BUILT_IN_NONE)
6488 return NULL_RTX;
6490 /* Change the call to the specified function. */
6491 fndecl = get_callee_fndecl (exp);
6492 addr = CALL_EXPR_FN (exp);
6493 STRIP_NOPS (addr);
6495 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
6496 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
6498 /* If we will emit code after the call, the call cannot be a tail call.
6499 If it is emitted as a tail call, a barrier is emitted after it, and
6500 then all trailing code is removed. */
6501 if (!ignore)
6502 CALL_EXPR_TAILCALL (exp) = 0;
6504 /* Expand the call here so we can emit trailing code. */
6505 ret = expand_call (exp, target, ignore);
6507 /* Replace the original function just in case it matters. */
6508 TREE_OPERAND (addr, 0) = fndecl;
6510 /* Then issue the arithmetic correction to return the right result. */
6511 if (!ignore)
6513 if (code == NOT)
6515 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
6516 OPTAB_LIB_WIDEN);
6517 ret = expand_simple_unop (mode, NOT, ret, target, true);
6519 else
6520 ret = expand_simple_binop (mode, code, ret, val, target, true,
6521 OPTAB_LIB_WIDEN);
6523 return ret;
6526 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6528 void
6529 expand_ifn_atomic_bit_test_and (gcall *call)
6531 tree ptr = gimple_call_arg (call, 0);
6532 tree bit = gimple_call_arg (call, 1);
6533 tree flag = gimple_call_arg (call, 2);
6534 tree lhs = gimple_call_lhs (call);
6535 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6536 machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
6537 enum rtx_code code;
6538 optab optab;
6539 class expand_operand ops[5];
6541 gcc_assert (flag_inline_atomics);
6543 if (gimple_call_num_args (call) == 5)
6544 model = get_memmodel (gimple_call_arg (call, 3));
6546 rtx mem = get_builtin_sync_mem (ptr, mode);
6547 rtx val = expand_expr_force_mode (bit, mode);
6549 switch (gimple_call_internal_fn (call))
6551 case IFN_ATOMIC_BIT_TEST_AND_SET:
6552 code = IOR;
6553 optab = atomic_bit_test_and_set_optab;
6554 break;
6555 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
6556 code = XOR;
6557 optab = atomic_bit_test_and_complement_optab;
6558 break;
6559 case IFN_ATOMIC_BIT_TEST_AND_RESET:
6560 code = AND;
6561 optab = atomic_bit_test_and_reset_optab;
6562 break;
6563 default:
6564 gcc_unreachable ();
6567 if (lhs == NULL_TREE)
6569 rtx val2 = expand_simple_binop (mode, ASHIFT, const1_rtx,
6570 val, NULL_RTX, true, OPTAB_DIRECT);
6571 if (code == AND)
6572 val2 = expand_simple_unop (mode, NOT, val2, NULL_RTX, true);
6573 if (expand_atomic_fetch_op (const0_rtx, mem, val2, code, model, false))
6574 return;
6577 rtx target;
6578 if (lhs)
6579 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6580 else
6581 target = gen_reg_rtx (mode);
6582 enum insn_code icode = direct_optab_handler (optab, mode);
6583 gcc_assert (icode != CODE_FOR_nothing);
6584 create_output_operand (&ops[0], target, mode);
6585 create_fixed_operand (&ops[1], mem);
6586 create_convert_operand_to (&ops[2], val, mode, true);
6587 create_integer_operand (&ops[3], model);
6588 create_integer_operand (&ops[4], integer_onep (flag));
6589 if (maybe_expand_insn (icode, 5, ops))
6590 return;
6592 rtx bitval = val;
6593 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6594 val, NULL_RTX, true, OPTAB_DIRECT);
6595 rtx maskval = val;
6596 if (code == AND)
6597 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6598 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
6599 code, model, false);
6600 if (!result)
6602 bool is_atomic = gimple_call_num_args (call) == 5;
6603 tree tcall = gimple_call_arg (call, 3 + is_atomic);
6604 tree fndecl = gimple_call_addr_fndecl (tcall);
6605 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6606 tree exp = build_call_nary (type, tcall, 2 + is_atomic, ptr,
6607 make_tree (type, val),
6608 is_atomic
6609 ? gimple_call_arg (call, 3)
6610 : integer_zero_node);
6611 result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
6612 mode, !lhs);
6614 if (!lhs)
6615 return;
6616 if (integer_onep (flag))
6618 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
6619 NULL_RTX, true, OPTAB_DIRECT);
6620 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
6621 true, OPTAB_DIRECT);
6623 else
6624 result = expand_simple_binop (mode, AND, result, maskval, target, true,
6625 OPTAB_DIRECT);
6626 if (result != target)
6627 emit_move_insn (target, result);
6630 /* Expand IFN_ATOMIC_*_FETCH_CMP_0 internal function. */
6632 void
6633 expand_ifn_atomic_op_fetch_cmp_0 (gcall *call)
6635 tree cmp = gimple_call_arg (call, 0);
6636 tree ptr = gimple_call_arg (call, 1);
6637 tree arg = gimple_call_arg (call, 2);
6638 tree lhs = gimple_call_lhs (call);
6639 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6640 machine_mode mode = TYPE_MODE (TREE_TYPE (cmp));
6641 optab optab;
6642 rtx_code code;
6643 class expand_operand ops[5];
6645 gcc_assert (flag_inline_atomics);
6647 if (gimple_call_num_args (call) == 5)
6648 model = get_memmodel (gimple_call_arg (call, 3));
6650 rtx mem = get_builtin_sync_mem (ptr, mode);
6651 rtx op = expand_expr_force_mode (arg, mode);
6653 switch (gimple_call_internal_fn (call))
6655 case IFN_ATOMIC_ADD_FETCH_CMP_0:
6656 code = PLUS;
6657 optab = atomic_add_fetch_cmp_0_optab;
6658 break;
6659 case IFN_ATOMIC_SUB_FETCH_CMP_0:
6660 code = MINUS;
6661 optab = atomic_sub_fetch_cmp_0_optab;
6662 break;
6663 case IFN_ATOMIC_AND_FETCH_CMP_0:
6664 code = AND;
6665 optab = atomic_and_fetch_cmp_0_optab;
6666 break;
6667 case IFN_ATOMIC_OR_FETCH_CMP_0:
6668 code = IOR;
6669 optab = atomic_or_fetch_cmp_0_optab;
6670 break;
6671 case IFN_ATOMIC_XOR_FETCH_CMP_0:
6672 code = XOR;
6673 optab = atomic_xor_fetch_cmp_0_optab;
6674 break;
6675 default:
6676 gcc_unreachable ();
6679 enum rtx_code comp = UNKNOWN;
6680 switch (tree_to_uhwi (cmp))
6682 case ATOMIC_OP_FETCH_CMP_0_EQ: comp = EQ; break;
6683 case ATOMIC_OP_FETCH_CMP_0_NE: comp = NE; break;
6684 case ATOMIC_OP_FETCH_CMP_0_GT: comp = GT; break;
6685 case ATOMIC_OP_FETCH_CMP_0_GE: comp = GE; break;
6686 case ATOMIC_OP_FETCH_CMP_0_LT: comp = LT; break;
6687 case ATOMIC_OP_FETCH_CMP_0_LE: comp = LE; break;
6688 default: gcc_unreachable ();
6691 rtx target;
6692 if (lhs == NULL_TREE)
6693 target = gen_reg_rtx (TYPE_MODE (boolean_type_node));
6694 else
6695 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6696 enum insn_code icode = direct_optab_handler (optab, mode);
6697 gcc_assert (icode != CODE_FOR_nothing);
6698 create_output_operand (&ops[0], target, TYPE_MODE (boolean_type_node));
6699 create_fixed_operand (&ops[1], mem);
6700 create_convert_operand_to (&ops[2], op, mode, true);
6701 create_integer_operand (&ops[3], model);
6702 create_integer_operand (&ops[4], comp);
6703 if (maybe_expand_insn (icode, 5, ops))
6704 return;
6706 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, op,
6707 code, model, true);
6708 if (!result)
6710 bool is_atomic = gimple_call_num_args (call) == 5;
6711 tree tcall = gimple_call_arg (call, 3 + is_atomic);
6712 tree fndecl = gimple_call_addr_fndecl (tcall);
6713 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6714 tree exp = build_call_nary (type, tcall,
6715 2 + is_atomic, ptr, arg,
6716 is_atomic
6717 ? gimple_call_arg (call, 3)
6718 : integer_zero_node);
6719 result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
6720 mode, !lhs);
6723 if (lhs)
6725 result = emit_store_flag_force (target, comp, result, const0_rtx, mode,
6726 0, 1);
6727 if (result != target)
6728 emit_move_insn (target, result);
6732 /* Expand an atomic clear operation.
6733 void _atomic_clear (BOOL *obj, enum memmodel)
6734 EXP is the call expression. */
6736 static rtx
6737 expand_builtin_atomic_clear (tree exp)
6739 machine_mode mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6740 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6741 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6743 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
6744 model = MEMMODEL_SEQ_CST;
6746 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6747 Failing that, a store is issued by __atomic_store. The only way this can
6748 fail is if the bool type is larger than a word size. Unlikely, but
6749 handle it anyway for completeness. Assume a single threaded model since
6750 there is no atomic support in this case, and no barriers are required. */
6751 rtx ret = expand_atomic_store (mem, const0_rtx, model, true);
6752 if (!ret)
6753 emit_move_insn (mem, const0_rtx);
6754 return const0_rtx;
6757 /* Expand an atomic test_and_set operation.
6758 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6759 EXP is the call expression. */
6761 static rtx
6762 expand_builtin_atomic_test_and_set (tree exp, rtx target)
6764 rtx mem;
6765 enum memmodel model;
6766 machine_mode mode;
6768 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6769 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6770 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6772 return expand_atomic_test_and_set (target, mem, model);
6776 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6777 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6779 static tree
6780 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
6782 int size;
6783 machine_mode mode;
6784 unsigned int mode_align, type_align;
6786 if (TREE_CODE (arg0) != INTEGER_CST)
6787 return NULL_TREE;
6789 /* We need a corresponding integer mode for the access to be lock-free. */
6790 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
6791 if (!int_mode_for_size (size, 0).exists (&mode))
6792 return boolean_false_node;
6794 mode_align = GET_MODE_ALIGNMENT (mode);
6796 if (TREE_CODE (arg1) == INTEGER_CST)
6798 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
6800 /* Either this argument is null, or it's a fake pointer encoding
6801 the alignment of the object. */
6802 val = least_bit_hwi (val);
6803 val *= BITS_PER_UNIT;
6805 if (val == 0 || mode_align < val)
6806 type_align = mode_align;
6807 else
6808 type_align = val;
6810 else
6812 tree ttype = TREE_TYPE (arg1);
6814 /* This function is usually invoked and folded immediately by the front
6815 end before anything else has a chance to look at it. The pointer
6816 parameter at this point is usually cast to a void *, so check for that
6817 and look past the cast. */
6818 if (CONVERT_EXPR_P (arg1)
6819 && POINTER_TYPE_P (ttype)
6820 && VOID_TYPE_P (TREE_TYPE (ttype))
6821 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
6822 arg1 = TREE_OPERAND (arg1, 0);
6824 ttype = TREE_TYPE (arg1);
6825 gcc_assert (POINTER_TYPE_P (ttype));
6827 /* Get the underlying type of the object. */
6828 ttype = TREE_TYPE (ttype);
6829 type_align = TYPE_ALIGN (ttype);
6832 /* If the object has smaller alignment, the lock free routines cannot
6833 be used. */
6834 if (type_align < mode_align)
6835 return boolean_false_node;
6837 /* Check if a compare_and_swap pattern exists for the mode which represents
6838 the required size. The pattern is not allowed to fail, so the existence
6839 of the pattern indicates support is present. Also require that an
6840 atomic load exists for the required size. */
6841 if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
6842 return boolean_true_node;
6843 else
6844 return boolean_false_node;
6847 /* Return true if the parameters to call EXP represent an object which will
6848 always generate lock free instructions. The first argument represents the
6849 size of the object, and the second parameter is a pointer to the object
6850 itself. If NULL is passed for the object, then the result is based on
6851 typical alignment for an object of the specified size. Otherwise return
6852 false. */
6854 static rtx
6855 expand_builtin_atomic_always_lock_free (tree exp)
6857 tree size;
6858 tree arg0 = CALL_EXPR_ARG (exp, 0);
6859 tree arg1 = CALL_EXPR_ARG (exp, 1);
6861 if (TREE_CODE (arg0) != INTEGER_CST)
6863 error ("non-constant argument 1 to %qs", "__atomic_always_lock_free");
6864 return const0_rtx;
6867 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
6868 if (size == boolean_true_node)
6869 return const1_rtx;
6870 return const0_rtx;
6873 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6874 is lock free on this architecture. */
6876 static tree
6877 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
6879 if (!flag_inline_atomics)
6880 return NULL_TREE;
6882 /* If it isn't always lock free, don't generate a result. */
6883 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
6884 return boolean_true_node;
6886 return NULL_TREE;
6889 /* Return true if the parameters to call EXP represent an object which will
6890 always generate lock free instructions. The first argument represents the
6891 size of the object, and the second parameter is a pointer to the object
6892 itself. If NULL is passed for the object, then the result is based on
6893 typical alignment for an object of the specified size. Otherwise return
6894 NULL*/
6896 static rtx
6897 expand_builtin_atomic_is_lock_free (tree exp)
6899 tree size;
6900 tree arg0 = CALL_EXPR_ARG (exp, 0);
6901 tree arg1 = CALL_EXPR_ARG (exp, 1);
6903 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6905 error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
6906 return NULL_RTX;
6909 if (!flag_inline_atomics)
6910 return NULL_RTX;
6912 /* If the value is known at compile time, return the RTX for it. */
6913 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
6914 if (size == boolean_true_node)
6915 return const1_rtx;
6917 return NULL_RTX;
6920 /* Expand the __atomic_thread_fence intrinsic:
6921 void __atomic_thread_fence (enum memmodel)
6922 EXP is the CALL_EXPR. */
6924 static void
6925 expand_builtin_atomic_thread_fence (tree exp)
6927 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6928 expand_mem_thread_fence (model);
6931 /* Expand the __atomic_signal_fence intrinsic:
6932 void __atomic_signal_fence (enum memmodel)
6933 EXP is the CALL_EXPR. */
6935 static void
6936 expand_builtin_atomic_signal_fence (tree exp)
6938 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6939 expand_mem_signal_fence (model);
6942 /* Expand the __sync_synchronize intrinsic. */
6944 static void
6945 expand_builtin_sync_synchronize (void)
6947 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
6950 static rtx
6951 expand_builtin_thread_pointer (tree exp, rtx target)
6953 enum insn_code icode;
6954 if (!validate_arglist (exp, VOID_TYPE))
6955 return const0_rtx;
6956 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
6957 if (icode != CODE_FOR_nothing)
6959 class expand_operand op;
6960 /* If the target is not sutitable then create a new target. */
6961 if (target == NULL_RTX
6962 || !REG_P (target)
6963 || GET_MODE (target) != Pmode)
6964 target = gen_reg_rtx (Pmode);
6965 create_output_operand (&op, target, Pmode);
6966 expand_insn (icode, 1, &op);
6967 return target;
6969 error ("%<__builtin_thread_pointer%> is not supported on this target");
6970 return const0_rtx;
6973 static void
6974 expand_builtin_set_thread_pointer (tree exp)
6976 enum insn_code icode;
6977 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6978 return;
6979 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
6980 if (icode != CODE_FOR_nothing)
6982 class expand_operand op;
6983 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
6984 Pmode, EXPAND_NORMAL);
6985 create_input_operand (&op, val, Pmode);
6986 expand_insn (icode, 1, &op);
6987 return;
6989 error ("%<__builtin_set_thread_pointer%> is not supported on this target");
6993 /* Emit code to restore the current value of stack. */
6995 static void
6996 expand_stack_restore (tree var)
6998 rtx_insn *prev;
6999 rtx sa = expand_normal (var);
7001 sa = convert_memory_address (Pmode, sa);
7003 prev = get_last_insn ();
7004 emit_stack_restore (SAVE_BLOCK, sa);
7006 record_new_stack_level ();
7008 fixup_args_size_notes (prev, get_last_insn (), 0);
7011 /* Emit code to save the current value of stack. */
7013 static rtx
7014 expand_stack_save (void)
7016 rtx ret = NULL_RTX;
7018 emit_stack_save (SAVE_BLOCK, &ret);
7019 return ret;
7022 /* Emit code to get the openacc gang, worker or vector id or size. */
7024 static rtx
7025 expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
7027 const char *name;
7028 rtx fallback_retval;
7029 rtx_insn *(*gen_fn) (rtx, rtx);
7030 switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
7032 case BUILT_IN_GOACC_PARLEVEL_ID:
7033 name = "__builtin_goacc_parlevel_id";
7034 fallback_retval = const0_rtx;
7035 gen_fn = targetm.gen_oacc_dim_pos;
7036 break;
7037 case BUILT_IN_GOACC_PARLEVEL_SIZE:
7038 name = "__builtin_goacc_parlevel_size";
7039 fallback_retval = const1_rtx;
7040 gen_fn = targetm.gen_oacc_dim_size;
7041 break;
7042 default:
7043 gcc_unreachable ();
7046 if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
7048 error ("%qs only supported in OpenACC code", name);
7049 return const0_rtx;
7052 tree arg = CALL_EXPR_ARG (exp, 0);
7053 if (TREE_CODE (arg) != INTEGER_CST)
7055 error ("non-constant argument 0 to %qs", name);
7056 return const0_rtx;
7059 int dim = TREE_INT_CST_LOW (arg);
7060 switch (dim)
7062 case GOMP_DIM_GANG:
7063 case GOMP_DIM_WORKER:
7064 case GOMP_DIM_VECTOR:
7065 break;
7066 default:
7067 error ("illegal argument 0 to %qs", name);
7068 return const0_rtx;
7071 if (ignore)
7072 return target;
7074 if (target == NULL_RTX)
7075 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
7077 if (!targetm.have_oacc_dim_size ())
7079 emit_move_insn (target, fallback_retval);
7080 return target;
7083 rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
7084 emit_insn (gen_fn (reg, GEN_INT (dim)));
7085 if (reg != target)
7086 emit_move_insn (target, reg);
7088 return target;
7091 /* Expand a string compare operation using a sequence of char comparison
7092 to get rid of the calling overhead, with result going to TARGET if
7093 that's convenient.
7095 VAR_STR is the variable string source;
7096 CONST_STR is the constant string source;
7097 LENGTH is the number of chars to compare;
7098 CONST_STR_N indicates which source string is the constant string;
7099 IS_MEMCMP indicates whether it's a memcmp or strcmp.
7101 to: (assume const_str_n is 2, i.e., arg2 is a constant string)
7103 target = (int) (unsigned char) var_str[0]
7104 - (int) (unsigned char) const_str[0];
7105 if (target != 0)
7106 goto ne_label;
7108 target = (int) (unsigned char) var_str[length - 2]
7109 - (int) (unsigned char) const_str[length - 2];
7110 if (target != 0)
7111 goto ne_label;
7112 target = (int) (unsigned char) var_str[length - 1]
7113 - (int) (unsigned char) const_str[length - 1];
7114 ne_label:
7117 static rtx
7118 inline_string_cmp (rtx target, tree var_str, const char *const_str,
7119 unsigned HOST_WIDE_INT length,
7120 int const_str_n, machine_mode mode)
7122 HOST_WIDE_INT offset = 0;
7123 rtx var_rtx_array
7124 = get_memory_rtx (var_str, build_int_cst (unsigned_type_node,length));
7125 rtx var_rtx = NULL_RTX;
7126 rtx const_rtx = NULL_RTX;
7127 rtx result = target ? target : gen_reg_rtx (mode);
7128 rtx_code_label *ne_label = gen_label_rtx ();
7129 tree unit_type_node = unsigned_char_type_node;
7130 scalar_int_mode unit_mode
7131 = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
7133 start_sequence ();
7135 for (unsigned HOST_WIDE_INT i = 0; i < length; i++)
7137 var_rtx
7138 = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset);
7139 const_rtx = c_readstr (const_str + offset, unit_mode);
7140 rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx;
7141 rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
7143 op0 = convert_modes (mode, unit_mode, op0, 1);
7144 op1 = convert_modes (mode, unit_mode, op1, 1);
7145 result = expand_simple_binop (mode, MINUS, op0, op1,
7146 result, 1, OPTAB_WIDEN);
7147 if (i < length - 1)
7148 emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
7149 mode, true, ne_label);
7150 offset += GET_MODE_SIZE (unit_mode);
7153 emit_label (ne_label);
7154 rtx_insn *insns = get_insns ();
7155 end_sequence ();
7156 emit_insn (insns);
7158 return result;
7161 /* Inline expansion of a call to str(n)cmp and memcmp, with result going
7162 to TARGET if that's convenient.
7163 If the call is not been inlined, return NULL_RTX. */
7165 static rtx
7166 inline_expand_builtin_bytecmp (tree exp, rtx target)
7168 tree fndecl = get_callee_fndecl (exp);
7169 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7170 bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP);
7172 /* Do NOT apply this inlining expansion when optimizing for size or
7173 optimization level below 2. */
7174 if (optimize < 2 || optimize_insn_for_size_p ())
7175 return NULL_RTX;
7177 gcc_checking_assert (fcode == BUILT_IN_STRCMP
7178 || fcode == BUILT_IN_STRNCMP
7179 || fcode == BUILT_IN_MEMCMP);
7181 /* On a target where the type of the call (int) has same or narrower presicion
7182 than unsigned char, give up the inlining expansion. */
7183 if (TYPE_PRECISION (unsigned_char_type_node)
7184 >= TYPE_PRECISION (TREE_TYPE (exp)))
7185 return NULL_RTX;
7187 tree arg1 = CALL_EXPR_ARG (exp, 0);
7188 tree arg2 = CALL_EXPR_ARG (exp, 1);
7189 tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
7191 unsigned HOST_WIDE_INT len1 = 0;
7192 unsigned HOST_WIDE_INT len2 = 0;
7193 unsigned HOST_WIDE_INT len3 = 0;
7195 /* Get the object representation of the initializers of ARG1 and ARG2
7196 as strings, provided they refer to constant objects, with their byte
7197 sizes in LEN1 and LEN2, respectively. */
7198 const char *bytes1 = getbyterep (arg1, &len1);
7199 const char *bytes2 = getbyterep (arg2, &len2);
7201 /* Fail if neither argument refers to an initialized constant. */
7202 if (!bytes1 && !bytes2)
7203 return NULL_RTX;
7205 if (is_ncmp)
7207 /* Fail if the memcmp/strncmp bound is not a constant. */
7208 if (!tree_fits_uhwi_p (len3_tree))
7209 return NULL_RTX;
7211 len3 = tree_to_uhwi (len3_tree);
7213 if (fcode == BUILT_IN_MEMCMP)
7215 /* Fail if the memcmp bound is greater than the size of either
7216 of the two constant objects. */
7217 if ((bytes1 && len1 < len3)
7218 || (bytes2 && len2 < len3))
7219 return NULL_RTX;
7223 if (fcode != BUILT_IN_MEMCMP)
7225 /* For string functions (i.e., strcmp and strncmp) reduce LEN1
7226 and LEN2 to the length of the nul-terminated string stored
7227 in each. */
7228 if (bytes1 != NULL)
7229 len1 = strnlen (bytes1, len1) + 1;
7230 if (bytes2 != NULL)
7231 len2 = strnlen (bytes2, len2) + 1;
7234 /* See inline_string_cmp. */
7235 int const_str_n;
7236 if (!len1)
7237 const_str_n = 2;
7238 else if (!len2)
7239 const_str_n = 1;
7240 else if (len2 > len1)
7241 const_str_n = 1;
7242 else
7243 const_str_n = 2;
7245 /* For strncmp only, compute the new bound as the smallest of
7246 the lengths of the two strings (plus 1) and the bound provided
7247 to the function. */
7248 unsigned HOST_WIDE_INT bound = (const_str_n == 1) ? len1 : len2;
7249 if (is_ncmp && len3 < bound)
7250 bound = len3;
7252 /* If the bound of the comparison is larger than the threshold,
7253 do nothing. */
7254 if (bound > (unsigned HOST_WIDE_INT) param_builtin_string_cmp_inline_length)
7255 return NULL_RTX;
7257 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
7259 /* Now, start inline expansion the call. */
7260 return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
7261 (const_str_n == 1) ? bytes1 : bytes2, bound,
7262 const_str_n, mode);
7265 /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
7266 represents the size of the first argument to that call, or VOIDmode
7267 if the argument is a pointer. IGNORE will be true if the result
7268 isn't used. */
7269 static rtx
7270 expand_speculation_safe_value (machine_mode mode, tree exp, rtx target,
7271 bool ignore)
7273 rtx val, failsafe;
7274 unsigned nargs = call_expr_nargs (exp);
7276 tree arg0 = CALL_EXPR_ARG (exp, 0);
7278 if (mode == VOIDmode)
7280 mode = TYPE_MODE (TREE_TYPE (arg0));
7281 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
7284 val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
7286 /* An optional second argument can be used as a failsafe value on
7287 some machines. If it isn't present, then the failsafe value is
7288 assumed to be 0. */
7289 if (nargs > 1)
7291 tree arg1 = CALL_EXPR_ARG (exp, 1);
7292 failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL);
7294 else
7295 failsafe = const0_rtx;
7297 /* If the result isn't used, the behavior is undefined. It would be
7298 nice to emit a warning here, but path splitting means this might
7299 happen with legitimate code. So simply drop the builtin
7300 expansion in that case; we've handled any side-effects above. */
7301 if (ignore)
7302 return const0_rtx;
7304 /* If we don't have a suitable target, create one to hold the result. */
7305 if (target == NULL || GET_MODE (target) != mode)
7306 target = gen_reg_rtx (mode);
7308 if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode)
7309 val = convert_modes (mode, VOIDmode, val, false);
7311 return targetm.speculation_safe_value (mode, target, val, failsafe);
7314 /* Expand an expression EXP that calls a built-in function,
7315 with result going to TARGET if that's convenient
7316 (and in mode MODE if that's convenient).
7317 SUBTARGET may be used as the target for computing one of EXP's operands.
7318 IGNORE is nonzero if the value is to be ignored. */
7321 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
7322 int ignore)
7324 tree fndecl = get_callee_fndecl (exp);
7325 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
7326 int flags;
7328 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7329 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7331 /* When ASan is enabled, we don't want to expand some memory/string
7332 builtins and rely on libsanitizer's hooks. This allows us to avoid
7333 redundant checks and be sure, that possible overflow will be detected
7334 by ASan. */
7336 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7337 if (param_asan_kernel_mem_intrinsic_prefix
7338 && sanitize_flags_p (SANITIZE_KERNEL_ADDRESS
7339 | SANITIZE_KERNEL_HWADDRESS))
7340 switch (fcode)
7342 rtx save_decl_rtl, ret;
7343 case BUILT_IN_MEMCPY:
7344 case BUILT_IN_MEMMOVE:
7345 case BUILT_IN_MEMSET:
7346 save_decl_rtl = DECL_RTL (fndecl);
7347 DECL_RTL (fndecl) = asan_memfn_rtl (fndecl);
7348 ret = expand_call (exp, target, ignore);
7349 DECL_RTL (fndecl) = save_decl_rtl;
7350 return ret;
7351 default:
7352 break;
7354 if (sanitize_flags_p (SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
7355 return expand_call (exp, target, ignore);
7357 /* When not optimizing, generate calls to library functions for a certain
7358 set of builtins. */
7359 if (!optimize
7360 && !called_as_built_in (fndecl)
7361 && fcode != BUILT_IN_FORK
7362 && fcode != BUILT_IN_EXECL
7363 && fcode != BUILT_IN_EXECV
7364 && fcode != BUILT_IN_EXECLP
7365 && fcode != BUILT_IN_EXECLE
7366 && fcode != BUILT_IN_EXECVP
7367 && fcode != BUILT_IN_EXECVE
7368 && fcode != BUILT_IN_CLEAR_CACHE
7369 && !ALLOCA_FUNCTION_CODE_P (fcode)
7370 && fcode != BUILT_IN_FREE)
7371 return expand_call (exp, target, ignore);
7373 /* The built-in function expanders test for target == const0_rtx
7374 to determine whether the function's result will be ignored. */
7375 if (ignore)
7376 target = const0_rtx;
7378 /* If the result of a pure or const built-in function is ignored, and
7379 none of its arguments are volatile, we can avoid expanding the
7380 built-in call and just evaluate the arguments for side-effects. */
7381 if (target == const0_rtx
7382 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
7383 && !(flags & ECF_LOOPING_CONST_OR_PURE))
7385 bool volatilep = false;
7386 tree arg;
7387 call_expr_arg_iterator iter;
7389 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7390 if (TREE_THIS_VOLATILE (arg))
7392 volatilep = true;
7393 break;
7396 if (! volatilep)
7398 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7399 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
7400 return const0_rtx;
7404 switch (fcode)
7406 CASE_FLT_FN (BUILT_IN_FABS):
7407 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
7408 case BUILT_IN_FABSD32:
7409 case BUILT_IN_FABSD64:
7410 case BUILT_IN_FABSD128:
7411 target = expand_builtin_fabs (exp, target, subtarget);
7412 if (target)
7413 return target;
7414 break;
7416 CASE_FLT_FN (BUILT_IN_COPYSIGN):
7417 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
7418 target = expand_builtin_copysign (exp, target, subtarget);
7419 if (target)
7420 return target;
7421 break;
7423 /* Just do a normal library call if we were unable to fold
7424 the values. */
7425 CASE_FLT_FN (BUILT_IN_CABS):
7426 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CABS):
7427 break;
7429 CASE_FLT_FN (BUILT_IN_FMA):
7430 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
7431 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
7432 if (target)
7433 return target;
7434 break;
7436 CASE_FLT_FN (BUILT_IN_ILOGB):
7437 if (! flag_unsafe_math_optimizations)
7438 break;
7439 gcc_fallthrough ();
7440 CASE_FLT_FN (BUILT_IN_ISINF):
7441 CASE_FLT_FN (BUILT_IN_FINITE):
7442 case BUILT_IN_ISFINITE:
7443 case BUILT_IN_ISNORMAL:
7444 target = expand_builtin_interclass_mathfn (exp, target);
7445 if (target)
7446 return target;
7447 break;
7449 case BUILT_IN_ISSIGNALING:
7450 target = expand_builtin_issignaling (exp, target);
7451 if (target)
7452 return target;
7453 break;
7455 CASE_FLT_FN (BUILT_IN_ICEIL):
7456 CASE_FLT_FN (BUILT_IN_LCEIL):
7457 CASE_FLT_FN (BUILT_IN_LLCEIL):
7458 CASE_FLT_FN (BUILT_IN_LFLOOR):
7459 CASE_FLT_FN (BUILT_IN_IFLOOR):
7460 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7461 target = expand_builtin_int_roundingfn (exp, target);
7462 if (target)
7463 return target;
7464 break;
7466 CASE_FLT_FN (BUILT_IN_IRINT):
7467 CASE_FLT_FN (BUILT_IN_LRINT):
7468 CASE_FLT_FN (BUILT_IN_LLRINT):
7469 CASE_FLT_FN (BUILT_IN_IROUND):
7470 CASE_FLT_FN (BUILT_IN_LROUND):
7471 CASE_FLT_FN (BUILT_IN_LLROUND):
7472 target = expand_builtin_int_roundingfn_2 (exp, target);
7473 if (target)
7474 return target;
7475 break;
7477 CASE_FLT_FN (BUILT_IN_POWI):
7478 target = expand_builtin_powi (exp, target);
7479 if (target)
7480 return target;
7481 break;
7483 CASE_FLT_FN (BUILT_IN_CEXPI):
7484 target = expand_builtin_cexpi (exp, target);
7485 gcc_assert (target);
7486 return target;
7488 CASE_FLT_FN (BUILT_IN_SIN):
7489 CASE_FLT_FN (BUILT_IN_COS):
7490 if (! flag_unsafe_math_optimizations)
7491 break;
7492 target = expand_builtin_mathfn_3 (exp, target, subtarget);
7493 if (target)
7494 return target;
7495 break;
7497 CASE_FLT_FN (BUILT_IN_SINCOS):
7498 if (! flag_unsafe_math_optimizations)
7499 break;
7500 target = expand_builtin_sincos (exp);
7501 if (target)
7502 return target;
7503 break;
7505 case BUILT_IN_FEGETROUND:
7506 target = expand_builtin_fegetround (exp, target, target_mode);
7507 if (target)
7508 return target;
7509 break;
7511 case BUILT_IN_FECLEAREXCEPT:
7512 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
7513 feclearexcept_optab);
7514 if (target)
7515 return target;
7516 break;
7518 case BUILT_IN_FERAISEEXCEPT:
7519 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
7520 feraiseexcept_optab);
7521 if (target)
7522 return target;
7523 break;
7525 case BUILT_IN_APPLY_ARGS:
7526 return expand_builtin_apply_args ();
7528 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
7529 FUNCTION with a copy of the parameters described by
7530 ARGUMENTS, and ARGSIZE. It returns a block of memory
7531 allocated on the stack into which is stored all the registers
7532 that might possibly be used for returning the result of a
7533 function. ARGUMENTS is the value returned by
7534 __builtin_apply_args. ARGSIZE is the number of bytes of
7535 arguments that must be copied. ??? How should this value be
7536 computed? We'll also need a safe worst case value for varargs
7537 functions. */
7538 case BUILT_IN_APPLY:
7539 if (!validate_arglist (exp, POINTER_TYPE,
7540 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
7541 && !validate_arglist (exp, REFERENCE_TYPE,
7542 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7543 return const0_rtx;
7544 else
7546 rtx ops[3];
7548 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
7549 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
7550 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
7552 return expand_builtin_apply (ops[0], ops[1], ops[2]);
7555 /* __builtin_return (RESULT) causes the function to return the
7556 value described by RESULT. RESULT is address of the block of
7557 memory returned by __builtin_apply. */
7558 case BUILT_IN_RETURN:
7559 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7560 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
7561 return const0_rtx;
7563 case BUILT_IN_SAVEREGS:
7564 return expand_builtin_saveregs ();
7566 case BUILT_IN_VA_ARG_PACK:
7567 /* All valid uses of __builtin_va_arg_pack () are removed during
7568 inlining. */
7569 error ("invalid use of %<__builtin_va_arg_pack ()%>");
7570 return const0_rtx;
7572 case BUILT_IN_VA_ARG_PACK_LEN:
7573 /* All valid uses of __builtin_va_arg_pack_len () are removed during
7574 inlining. */
7575 error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
7576 return const0_rtx;
7578 /* Return the address of the first anonymous stack arg. */
7579 case BUILT_IN_NEXT_ARG:
7580 if (fold_builtin_next_arg (exp, false))
7581 return const0_rtx;
7582 return expand_builtin_next_arg ();
7584 case BUILT_IN_CLEAR_CACHE:
7585 expand_builtin___clear_cache (exp);
7586 return const0_rtx;
7588 case BUILT_IN_CLASSIFY_TYPE:
7589 return expand_builtin_classify_type (exp);
7591 case BUILT_IN_CONSTANT_P:
7592 return const0_rtx;
7594 case BUILT_IN_FRAME_ADDRESS:
7595 case BUILT_IN_RETURN_ADDRESS:
7596 return expand_builtin_frame_address (fndecl, exp);
7598 /* Returns the address of the area where the structure is returned.
7599 0 otherwise. */
7600 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
7601 if (call_expr_nargs (exp) != 0
7602 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
7603 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
7604 return const0_rtx;
7605 else
7606 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
7608 CASE_BUILT_IN_ALLOCA:
7609 target = expand_builtin_alloca (exp);
7610 if (target)
7611 return target;
7612 break;
7614 case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
7615 return expand_asan_emit_allocas_unpoison (exp);
7617 case BUILT_IN_STACK_SAVE:
7618 return expand_stack_save ();
7620 case BUILT_IN_STACK_RESTORE:
7621 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
7622 return const0_rtx;
7624 case BUILT_IN_BSWAP16:
7625 case BUILT_IN_BSWAP32:
7626 case BUILT_IN_BSWAP64:
7627 case BUILT_IN_BSWAP128:
7628 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
7629 if (target)
7630 return target;
7631 break;
7633 CASE_INT_FN (BUILT_IN_FFS):
7634 target = expand_builtin_unop (target_mode, exp, target,
7635 subtarget, ffs_optab);
7636 if (target)
7637 return target;
7638 break;
7640 CASE_INT_FN (BUILT_IN_CLZ):
7641 target = expand_builtin_unop (target_mode, exp, target,
7642 subtarget, clz_optab);
7643 if (target)
7644 return target;
7645 break;
7647 CASE_INT_FN (BUILT_IN_CTZ):
7648 target = expand_builtin_unop (target_mode, exp, target,
7649 subtarget, ctz_optab);
7650 if (target)
7651 return target;
7652 break;
7654 CASE_INT_FN (BUILT_IN_CLRSB):
7655 target = expand_builtin_unop (target_mode, exp, target,
7656 subtarget, clrsb_optab);
7657 if (target)
7658 return target;
7659 break;
7661 CASE_INT_FN (BUILT_IN_POPCOUNT):
7662 target = expand_builtin_unop (target_mode, exp, target,
7663 subtarget, popcount_optab);
7664 if (target)
7665 return target;
7666 break;
7668 CASE_INT_FN (BUILT_IN_PARITY):
7669 target = expand_builtin_unop (target_mode, exp, target,
7670 subtarget, parity_optab);
7671 if (target)
7672 return target;
7673 break;
7675 case BUILT_IN_STRLEN:
7676 target = expand_builtin_strlen (exp, target, target_mode);
7677 if (target)
7678 return target;
7679 break;
7681 case BUILT_IN_STRNLEN:
7682 target = expand_builtin_strnlen (exp, target, target_mode);
7683 if (target)
7684 return target;
7685 break;
7687 case BUILT_IN_STRCPY:
7688 target = expand_builtin_strcpy (exp, target);
7689 if (target)
7690 return target;
7691 break;
7693 case BUILT_IN_STRNCPY:
7694 target = expand_builtin_strncpy (exp, target);
7695 if (target)
7696 return target;
7697 break;
7699 case BUILT_IN_STPCPY:
7700 target = expand_builtin_stpcpy (exp, target, mode);
7701 if (target)
7702 return target;
7703 break;
7705 case BUILT_IN_MEMCPY:
7706 target = expand_builtin_memcpy (exp, target);
7707 if (target)
7708 return target;
7709 break;
7711 case BUILT_IN_MEMMOVE:
7712 target = expand_builtin_memmove (exp, target);
7713 if (target)
7714 return target;
7715 break;
7717 case BUILT_IN_MEMPCPY:
7718 target = expand_builtin_mempcpy (exp, target);
7719 if (target)
7720 return target;
7721 break;
7723 case BUILT_IN_MEMSET:
7724 target = expand_builtin_memset (exp, target, mode);
7725 if (target)
7726 return target;
7727 break;
7729 case BUILT_IN_BZERO:
7730 target = expand_builtin_bzero (exp);
7731 if (target)
7732 return target;
7733 break;
7735 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7736 back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
7737 when changing it to a strcmp call. */
7738 case BUILT_IN_STRCMP_EQ:
7739 target = expand_builtin_memcmp (exp, target, true);
7740 if (target)
7741 return target;
7743 /* Change this call back to a BUILT_IN_STRCMP. */
7744 TREE_OPERAND (exp, 1)
7745 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
7747 /* Delete the last parameter. */
7748 unsigned int i;
7749 vec<tree, va_gc> *arg_vec;
7750 vec_alloc (arg_vec, 2);
7751 for (i = 0; i < 2; i++)
7752 arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
7753 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
7754 /* FALLTHROUGH */
7756 case BUILT_IN_STRCMP:
7757 target = expand_builtin_strcmp (exp, target);
7758 if (target)
7759 return target;
7760 break;
7762 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7763 back to a BUILT_IN_STRNCMP. */
7764 case BUILT_IN_STRNCMP_EQ:
7765 target = expand_builtin_memcmp (exp, target, true);
7766 if (target)
7767 return target;
7769 /* Change it back to a BUILT_IN_STRNCMP. */
7770 TREE_OPERAND (exp, 1)
7771 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
7772 /* FALLTHROUGH */
7774 case BUILT_IN_STRNCMP:
7775 target = expand_builtin_strncmp (exp, target, mode);
7776 if (target)
7777 return target;
7778 break;
7780 case BUILT_IN_BCMP:
7781 case BUILT_IN_MEMCMP:
7782 case BUILT_IN_MEMCMP_EQ:
7783 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
7784 if (target)
7785 return target;
7786 if (fcode == BUILT_IN_MEMCMP_EQ)
7788 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
7789 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
7791 break;
7793 case BUILT_IN_SETJMP:
7794 /* This should have been lowered to the builtins below. */
7795 gcc_unreachable ();
7797 case BUILT_IN_SETJMP_SETUP:
7798 /* __builtin_setjmp_setup is passed a pointer to an array of five words
7799 and the receiver label. */
7800 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7802 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7803 VOIDmode, EXPAND_NORMAL);
7804 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
7805 rtx_insn *label_r = label_rtx (label);
7807 expand_builtin_setjmp_setup (buf_addr, label_r);
7808 return const0_rtx;
7810 break;
7812 case BUILT_IN_SETJMP_RECEIVER:
7813 /* __builtin_setjmp_receiver is passed the receiver label. */
7814 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7816 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
7817 rtx_insn *label_r = label_rtx (label);
7819 expand_builtin_setjmp_receiver (label_r);
7820 nonlocal_goto_handler_labels
7821 = gen_rtx_INSN_LIST (VOIDmode, label_r,
7822 nonlocal_goto_handler_labels);
7823 /* ??? Do not let expand_label treat us as such since we would
7824 not want to be both on the list of non-local labels and on
7825 the list of forced labels. */
7826 FORCED_LABEL (label) = 0;
7827 return const0_rtx;
7829 break;
7831 /* __builtin_longjmp is passed a pointer to an array of five words.
7832 It's similar to the C library longjmp function but works with
7833 __builtin_setjmp above. */
7834 case BUILT_IN_LONGJMP:
7835 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7837 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7838 VOIDmode, EXPAND_NORMAL);
7839 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
7841 if (value != const1_rtx)
7843 error ("%<__builtin_longjmp%> second argument must be 1");
7844 return const0_rtx;
7847 expand_builtin_longjmp (buf_addr, value);
7848 return const0_rtx;
7850 break;
7852 case BUILT_IN_NONLOCAL_GOTO:
7853 target = expand_builtin_nonlocal_goto (exp);
7854 if (target)
7855 return target;
7856 break;
7858 /* This updates the setjmp buffer that is its argument with the value
7859 of the current stack pointer. */
7860 case BUILT_IN_UPDATE_SETJMP_BUF:
7861 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7863 rtx buf_addr
7864 = expand_normal (CALL_EXPR_ARG (exp, 0));
7866 expand_builtin_update_setjmp_buf (buf_addr);
7867 return const0_rtx;
7869 break;
7871 case BUILT_IN_TRAP:
7872 case BUILT_IN_UNREACHABLE_TRAP:
7873 expand_builtin_trap ();
7874 return const0_rtx;
7876 case BUILT_IN_UNREACHABLE:
7877 expand_builtin_unreachable ();
7878 return const0_rtx;
7880 CASE_FLT_FN (BUILT_IN_SIGNBIT):
7881 case BUILT_IN_SIGNBITD32:
7882 case BUILT_IN_SIGNBITD64:
7883 case BUILT_IN_SIGNBITD128:
7884 target = expand_builtin_signbit (exp, target);
7885 if (target)
7886 return target;
7887 break;
7889 /* Various hooks for the DWARF 2 __throw routine. */
7890 case BUILT_IN_UNWIND_INIT:
7891 expand_builtin_unwind_init ();
7892 return const0_rtx;
7893 case BUILT_IN_DWARF_CFA:
7894 return virtual_cfa_rtx;
7895 #ifdef DWARF2_UNWIND_INFO
7896 case BUILT_IN_DWARF_SP_COLUMN:
7897 return expand_builtin_dwarf_sp_column ();
7898 case BUILT_IN_INIT_DWARF_REG_SIZES:
7899 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
7900 return const0_rtx;
7901 #endif
7902 case BUILT_IN_FROB_RETURN_ADDR:
7903 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
7904 case BUILT_IN_EXTRACT_RETURN_ADDR:
7905 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
7906 case BUILT_IN_EH_RETURN:
7907 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
7908 CALL_EXPR_ARG (exp, 1));
7909 return const0_rtx;
7910 case BUILT_IN_EH_RETURN_DATA_REGNO:
7911 return expand_builtin_eh_return_data_regno (exp);
7912 case BUILT_IN_EXTEND_POINTER:
7913 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
7914 case BUILT_IN_EH_POINTER:
7915 return expand_builtin_eh_pointer (exp);
7916 case BUILT_IN_EH_FILTER:
7917 return expand_builtin_eh_filter (exp);
7918 case BUILT_IN_EH_COPY_VALUES:
7919 return expand_builtin_eh_copy_values (exp);
7921 case BUILT_IN_VA_START:
7922 return expand_builtin_va_start (exp);
7923 case BUILT_IN_VA_END:
7924 return expand_builtin_va_end (exp);
7925 case BUILT_IN_VA_COPY:
7926 return expand_builtin_va_copy (exp);
7927 case BUILT_IN_EXPECT:
7928 return expand_builtin_expect (exp, target);
7929 case BUILT_IN_EXPECT_WITH_PROBABILITY:
7930 return expand_builtin_expect_with_probability (exp, target);
7931 case BUILT_IN_ASSUME_ALIGNED:
7932 return expand_builtin_assume_aligned (exp, target);
7933 case BUILT_IN_PREFETCH:
7934 expand_builtin_prefetch (exp);
7935 return const0_rtx;
7937 case BUILT_IN_INIT_TRAMPOLINE:
7938 return expand_builtin_init_trampoline (exp, true);
7939 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
7940 return expand_builtin_init_trampoline (exp, false);
7941 case BUILT_IN_ADJUST_TRAMPOLINE:
7942 return expand_builtin_adjust_trampoline (exp);
7944 case BUILT_IN_INIT_DESCRIPTOR:
7945 return expand_builtin_init_descriptor (exp);
7946 case BUILT_IN_ADJUST_DESCRIPTOR:
7947 return expand_builtin_adjust_descriptor (exp);
7949 case BUILT_IN_FORK:
7950 case BUILT_IN_EXECL:
7951 case BUILT_IN_EXECV:
7952 case BUILT_IN_EXECLP:
7953 case BUILT_IN_EXECLE:
7954 case BUILT_IN_EXECVP:
7955 case BUILT_IN_EXECVE:
7956 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
7957 if (target)
7958 return target;
7959 break;
7961 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
7962 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
7963 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
7964 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
7965 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
7966 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
7967 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
7968 if (target)
7969 return target;
7970 break;
7972 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
7973 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
7974 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
7975 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
7976 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
7977 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
7978 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
7979 if (target)
7980 return target;
7981 break;
7983 case BUILT_IN_SYNC_FETCH_AND_OR_1:
7984 case BUILT_IN_SYNC_FETCH_AND_OR_2:
7985 case BUILT_IN_SYNC_FETCH_AND_OR_4:
7986 case BUILT_IN_SYNC_FETCH_AND_OR_8:
7987 case BUILT_IN_SYNC_FETCH_AND_OR_16:
7988 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
7989 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
7990 if (target)
7991 return target;
7992 break;
7994 case BUILT_IN_SYNC_FETCH_AND_AND_1:
7995 case BUILT_IN_SYNC_FETCH_AND_AND_2:
7996 case BUILT_IN_SYNC_FETCH_AND_AND_4:
7997 case BUILT_IN_SYNC_FETCH_AND_AND_8:
7998 case BUILT_IN_SYNC_FETCH_AND_AND_16:
7999 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
8000 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
8001 if (target)
8002 return target;
8003 break;
8005 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
8006 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
8007 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
8008 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
8009 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
8010 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
8011 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
8012 if (target)
8013 return target;
8014 break;
8016 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
8017 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
8018 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
8019 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
8020 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
8021 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
8022 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
8023 if (target)
8024 return target;
8025 break;
8027 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
8028 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
8029 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
8030 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
8031 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
8032 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
8033 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
8034 if (target)
8035 return target;
8036 break;
8038 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
8039 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
8040 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
8041 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
8042 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
8043 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
8044 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
8045 if (target)
8046 return target;
8047 break;
8049 case BUILT_IN_SYNC_OR_AND_FETCH_1:
8050 case BUILT_IN_SYNC_OR_AND_FETCH_2:
8051 case BUILT_IN_SYNC_OR_AND_FETCH_4:
8052 case BUILT_IN_SYNC_OR_AND_FETCH_8:
8053 case BUILT_IN_SYNC_OR_AND_FETCH_16:
8054 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
8055 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
8056 if (target)
8057 return target;
8058 break;
8060 case BUILT_IN_SYNC_AND_AND_FETCH_1:
8061 case BUILT_IN_SYNC_AND_AND_FETCH_2:
8062 case BUILT_IN_SYNC_AND_AND_FETCH_4:
8063 case BUILT_IN_SYNC_AND_AND_FETCH_8:
8064 case BUILT_IN_SYNC_AND_AND_FETCH_16:
8065 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
8066 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
8067 if (target)
8068 return target;
8069 break;
8071 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
8072 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
8073 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
8074 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
8075 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
8076 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
8077 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
8078 if (target)
8079 return target;
8080 break;
8082 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
8083 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
8084 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
8085 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
8086 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
8087 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
8088 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
8089 if (target)
8090 return target;
8091 break;
8093 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
8094 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
8095 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
8096 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
8097 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
8098 if (mode == VOIDmode)
8099 mode = TYPE_MODE (boolean_type_node);
8100 if (!target || !register_operand (target, mode))
8101 target = gen_reg_rtx (mode);
8103 mode = get_builtin_sync_mode
8104 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
8105 target = expand_builtin_compare_and_swap (mode, exp, true, target);
8106 if (target)
8107 return target;
8108 break;
8110 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
8111 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
8112 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
8113 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
8114 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
8115 mode = get_builtin_sync_mode
8116 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
8117 target = expand_builtin_compare_and_swap (mode, exp, false, target);
8118 if (target)
8119 return target;
8120 break;
8122 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
8123 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
8124 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
8125 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
8126 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
8127 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
8128 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
8129 if (target)
8130 return target;
8131 break;
8133 case BUILT_IN_SYNC_LOCK_RELEASE_1:
8134 case BUILT_IN_SYNC_LOCK_RELEASE_2:
8135 case BUILT_IN_SYNC_LOCK_RELEASE_4:
8136 case BUILT_IN_SYNC_LOCK_RELEASE_8:
8137 case BUILT_IN_SYNC_LOCK_RELEASE_16:
8138 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
8139 expand_builtin_sync_lock_release (mode, exp);
8140 return const0_rtx;
8142 case BUILT_IN_SYNC_SYNCHRONIZE:
8143 expand_builtin_sync_synchronize ();
8144 return const0_rtx;
8146 case BUILT_IN_ATOMIC_EXCHANGE_1:
8147 case BUILT_IN_ATOMIC_EXCHANGE_2:
8148 case BUILT_IN_ATOMIC_EXCHANGE_4:
8149 case BUILT_IN_ATOMIC_EXCHANGE_8:
8150 case BUILT_IN_ATOMIC_EXCHANGE_16:
8151 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
8152 target = expand_builtin_atomic_exchange (mode, exp, target);
8153 if (target)
8154 return target;
8155 break;
8157 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
8158 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
8159 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
8160 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
8161 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
8163 unsigned int nargs, z;
8164 vec<tree, va_gc> *vec;
8166 mode =
8167 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
8168 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
8169 if (target)
8170 return target;
8172 /* If this is turned into an external library call, the weak parameter
8173 must be dropped to match the expected parameter list. */
8174 nargs = call_expr_nargs (exp);
8175 vec_alloc (vec, nargs - 1);
8176 for (z = 0; z < 3; z++)
8177 vec->quick_push (CALL_EXPR_ARG (exp, z));
8178 /* Skip the boolean weak parameter. */
8179 for (z = 4; z < 6; z++)
8180 vec->quick_push (CALL_EXPR_ARG (exp, z));
8181 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
8182 break;
8185 case BUILT_IN_ATOMIC_LOAD_1:
8186 case BUILT_IN_ATOMIC_LOAD_2:
8187 case BUILT_IN_ATOMIC_LOAD_4:
8188 case BUILT_IN_ATOMIC_LOAD_8:
8189 case BUILT_IN_ATOMIC_LOAD_16:
8190 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
8191 target = expand_builtin_atomic_load (mode, exp, target);
8192 if (target)
8193 return target;
8194 break;
8196 case BUILT_IN_ATOMIC_STORE_1:
8197 case BUILT_IN_ATOMIC_STORE_2:
8198 case BUILT_IN_ATOMIC_STORE_4:
8199 case BUILT_IN_ATOMIC_STORE_8:
8200 case BUILT_IN_ATOMIC_STORE_16:
8201 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
8202 target = expand_builtin_atomic_store (mode, exp);
8203 if (target)
8204 return const0_rtx;
8205 break;
8207 case BUILT_IN_ATOMIC_ADD_FETCH_1:
8208 case BUILT_IN_ATOMIC_ADD_FETCH_2:
8209 case BUILT_IN_ATOMIC_ADD_FETCH_4:
8210 case BUILT_IN_ATOMIC_ADD_FETCH_8:
8211 case BUILT_IN_ATOMIC_ADD_FETCH_16:
8213 enum built_in_function lib;
8214 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
8215 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
8216 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
8217 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
8218 ignore, lib);
8219 if (target)
8220 return target;
8221 break;
8223 case BUILT_IN_ATOMIC_SUB_FETCH_1:
8224 case BUILT_IN_ATOMIC_SUB_FETCH_2:
8225 case BUILT_IN_ATOMIC_SUB_FETCH_4:
8226 case BUILT_IN_ATOMIC_SUB_FETCH_8:
8227 case BUILT_IN_ATOMIC_SUB_FETCH_16:
8229 enum built_in_function lib;
8230 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
8231 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
8232 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
8233 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
8234 ignore, lib);
8235 if (target)
8236 return target;
8237 break;
8239 case BUILT_IN_ATOMIC_AND_FETCH_1:
8240 case BUILT_IN_ATOMIC_AND_FETCH_2:
8241 case BUILT_IN_ATOMIC_AND_FETCH_4:
8242 case BUILT_IN_ATOMIC_AND_FETCH_8:
8243 case BUILT_IN_ATOMIC_AND_FETCH_16:
8245 enum built_in_function lib;
8246 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
8247 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
8248 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
8249 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
8250 ignore, lib);
8251 if (target)
8252 return target;
8253 break;
8255 case BUILT_IN_ATOMIC_NAND_FETCH_1:
8256 case BUILT_IN_ATOMIC_NAND_FETCH_2:
8257 case BUILT_IN_ATOMIC_NAND_FETCH_4:
8258 case BUILT_IN_ATOMIC_NAND_FETCH_8:
8259 case BUILT_IN_ATOMIC_NAND_FETCH_16:
8261 enum built_in_function lib;
8262 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
8263 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
8264 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
8265 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
8266 ignore, lib);
8267 if (target)
8268 return target;
8269 break;
8271 case BUILT_IN_ATOMIC_XOR_FETCH_1:
8272 case BUILT_IN_ATOMIC_XOR_FETCH_2:
8273 case BUILT_IN_ATOMIC_XOR_FETCH_4:
8274 case BUILT_IN_ATOMIC_XOR_FETCH_8:
8275 case BUILT_IN_ATOMIC_XOR_FETCH_16:
8277 enum built_in_function lib;
8278 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
8279 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
8280 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
8281 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
8282 ignore, lib);
8283 if (target)
8284 return target;
8285 break;
8287 case BUILT_IN_ATOMIC_OR_FETCH_1:
8288 case BUILT_IN_ATOMIC_OR_FETCH_2:
8289 case BUILT_IN_ATOMIC_OR_FETCH_4:
8290 case BUILT_IN_ATOMIC_OR_FETCH_8:
8291 case BUILT_IN_ATOMIC_OR_FETCH_16:
8293 enum built_in_function lib;
8294 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
8295 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
8296 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
8297 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
8298 ignore, lib);
8299 if (target)
8300 return target;
8301 break;
8303 case BUILT_IN_ATOMIC_FETCH_ADD_1:
8304 case BUILT_IN_ATOMIC_FETCH_ADD_2:
8305 case BUILT_IN_ATOMIC_FETCH_ADD_4:
8306 case BUILT_IN_ATOMIC_FETCH_ADD_8:
8307 case BUILT_IN_ATOMIC_FETCH_ADD_16:
8308 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
8309 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
8310 ignore, BUILT_IN_NONE);
8311 if (target)
8312 return target;
8313 break;
8315 case BUILT_IN_ATOMIC_FETCH_SUB_1:
8316 case BUILT_IN_ATOMIC_FETCH_SUB_2:
8317 case BUILT_IN_ATOMIC_FETCH_SUB_4:
8318 case BUILT_IN_ATOMIC_FETCH_SUB_8:
8319 case BUILT_IN_ATOMIC_FETCH_SUB_16:
8320 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
8321 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
8322 ignore, BUILT_IN_NONE);
8323 if (target)
8324 return target;
8325 break;
8327 case BUILT_IN_ATOMIC_FETCH_AND_1:
8328 case BUILT_IN_ATOMIC_FETCH_AND_2:
8329 case BUILT_IN_ATOMIC_FETCH_AND_4:
8330 case BUILT_IN_ATOMIC_FETCH_AND_8:
8331 case BUILT_IN_ATOMIC_FETCH_AND_16:
8332 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
8333 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
8334 ignore, BUILT_IN_NONE);
8335 if (target)
8336 return target;
8337 break;
8339 case BUILT_IN_ATOMIC_FETCH_NAND_1:
8340 case BUILT_IN_ATOMIC_FETCH_NAND_2:
8341 case BUILT_IN_ATOMIC_FETCH_NAND_4:
8342 case BUILT_IN_ATOMIC_FETCH_NAND_8:
8343 case BUILT_IN_ATOMIC_FETCH_NAND_16:
8344 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
8345 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
8346 ignore, BUILT_IN_NONE);
8347 if (target)
8348 return target;
8349 break;
8351 case BUILT_IN_ATOMIC_FETCH_XOR_1:
8352 case BUILT_IN_ATOMIC_FETCH_XOR_2:
8353 case BUILT_IN_ATOMIC_FETCH_XOR_4:
8354 case BUILT_IN_ATOMIC_FETCH_XOR_8:
8355 case BUILT_IN_ATOMIC_FETCH_XOR_16:
8356 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
8357 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
8358 ignore, BUILT_IN_NONE);
8359 if (target)
8360 return target;
8361 break;
8363 case BUILT_IN_ATOMIC_FETCH_OR_1:
8364 case BUILT_IN_ATOMIC_FETCH_OR_2:
8365 case BUILT_IN_ATOMIC_FETCH_OR_4:
8366 case BUILT_IN_ATOMIC_FETCH_OR_8:
8367 case BUILT_IN_ATOMIC_FETCH_OR_16:
8368 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
8369 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
8370 ignore, BUILT_IN_NONE);
8371 if (target)
8372 return target;
8373 break;
8375 case BUILT_IN_ATOMIC_TEST_AND_SET:
8376 return expand_builtin_atomic_test_and_set (exp, target);
8378 case BUILT_IN_ATOMIC_CLEAR:
8379 return expand_builtin_atomic_clear (exp);
8381 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8382 return expand_builtin_atomic_always_lock_free (exp);
8384 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8385 target = expand_builtin_atomic_is_lock_free (exp);
8386 if (target)
8387 return target;
8388 break;
8390 case BUILT_IN_ATOMIC_THREAD_FENCE:
8391 expand_builtin_atomic_thread_fence (exp);
8392 return const0_rtx;
8394 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
8395 expand_builtin_atomic_signal_fence (exp);
8396 return const0_rtx;
8398 case BUILT_IN_OBJECT_SIZE:
8399 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
8400 return expand_builtin_object_size (exp);
8402 case BUILT_IN_MEMCPY_CHK:
8403 case BUILT_IN_MEMPCPY_CHK:
8404 case BUILT_IN_MEMMOVE_CHK:
8405 case BUILT_IN_MEMSET_CHK:
8406 target = expand_builtin_memory_chk (exp, target, mode, fcode);
8407 if (target)
8408 return target;
8409 break;
8411 case BUILT_IN_STRCPY_CHK:
8412 case BUILT_IN_STPCPY_CHK:
8413 case BUILT_IN_STRNCPY_CHK:
8414 case BUILT_IN_STPNCPY_CHK:
8415 case BUILT_IN_STRCAT_CHK:
8416 case BUILT_IN_STRNCAT_CHK:
8417 case BUILT_IN_SNPRINTF_CHK:
8418 case BUILT_IN_VSNPRINTF_CHK:
8419 maybe_emit_chk_warning (exp, fcode);
8420 break;
8422 case BUILT_IN_SPRINTF_CHK:
8423 case BUILT_IN_VSPRINTF_CHK:
8424 maybe_emit_sprintf_chk_warning (exp, fcode);
8425 break;
8427 case BUILT_IN_THREAD_POINTER:
8428 return expand_builtin_thread_pointer (exp, target);
8430 case BUILT_IN_SET_THREAD_POINTER:
8431 expand_builtin_set_thread_pointer (exp);
8432 return const0_rtx;
8434 case BUILT_IN_ACC_ON_DEVICE:
8435 /* Do library call, if we failed to expand the builtin when
8436 folding. */
8437 break;
8439 case BUILT_IN_GOACC_PARLEVEL_ID:
8440 case BUILT_IN_GOACC_PARLEVEL_SIZE:
8441 return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
8443 case BUILT_IN_SPECULATION_SAFE_VALUE_PTR:
8444 return expand_speculation_safe_value (VOIDmode, exp, target, ignore);
8446 case BUILT_IN_SPECULATION_SAFE_VALUE_1:
8447 case BUILT_IN_SPECULATION_SAFE_VALUE_2:
8448 case BUILT_IN_SPECULATION_SAFE_VALUE_4:
8449 case BUILT_IN_SPECULATION_SAFE_VALUE_8:
8450 case BUILT_IN_SPECULATION_SAFE_VALUE_16:
8451 mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1);
8452 return expand_speculation_safe_value (mode, exp, target, ignore);
8454 default: /* just do library call, if unknown builtin */
8455 break;
8458 /* The switch statement above can drop through to cause the function
8459 to be called normally. */
8460 return expand_call (exp, target, ignore);
8463 /* Determine whether a tree node represents a call to a built-in
8464 function. If the tree T is a call to a built-in function with
8465 the right number of arguments of the appropriate types, return
8466 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
8467 Otherwise the return value is END_BUILTINS. */
8469 enum built_in_function
8470 builtin_mathfn_code (const_tree t)
8472 const_tree fndecl, arg, parmlist;
8473 const_tree argtype, parmtype;
8474 const_call_expr_arg_iterator iter;
8476 if (TREE_CODE (t) != CALL_EXPR)
8477 return END_BUILTINS;
8479 fndecl = get_callee_fndecl (t);
8480 if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
8481 return END_BUILTINS;
8483 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
8484 init_const_call_expr_arg_iterator (t, &iter);
8485 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
8487 /* If a function doesn't take a variable number of arguments,
8488 the last element in the list will have type `void'. */
8489 parmtype = TREE_VALUE (parmlist);
8490 if (VOID_TYPE_P (parmtype))
8492 if (more_const_call_expr_args_p (&iter))
8493 return END_BUILTINS;
8494 return DECL_FUNCTION_CODE (fndecl);
8497 if (! more_const_call_expr_args_p (&iter))
8498 return END_BUILTINS;
8500 arg = next_const_call_expr_arg (&iter);
8501 argtype = TREE_TYPE (arg);
8503 if (SCALAR_FLOAT_TYPE_P (parmtype))
8505 if (! SCALAR_FLOAT_TYPE_P (argtype))
8506 return END_BUILTINS;
8508 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
8510 if (! COMPLEX_FLOAT_TYPE_P (argtype))
8511 return END_BUILTINS;
8513 else if (POINTER_TYPE_P (parmtype))
8515 if (! POINTER_TYPE_P (argtype))
8516 return END_BUILTINS;
8518 else if (INTEGRAL_TYPE_P (parmtype))
8520 if (! INTEGRAL_TYPE_P (argtype))
8521 return END_BUILTINS;
8523 else
8524 return END_BUILTINS;
8527 /* Variable-length argument list. */
8528 return DECL_FUNCTION_CODE (fndecl);
8531 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
8532 evaluate to a constant. */
8534 static tree
8535 fold_builtin_constant_p (tree arg)
8537 /* We return 1 for a numeric type that's known to be a constant
8538 value at compile-time or for an aggregate type that's a
8539 literal constant. */
8540 STRIP_NOPS (arg);
8542 /* If we know this is a constant, emit the constant of one. */
8543 if (CONSTANT_CLASS_P (arg)
8544 || (TREE_CODE (arg) == CONSTRUCTOR
8545 && TREE_CONSTANT (arg)))
8546 return integer_one_node;
8547 if (TREE_CODE (arg) == ADDR_EXPR)
8549 tree op = TREE_OPERAND (arg, 0);
8550 if (TREE_CODE (op) == STRING_CST
8551 || (TREE_CODE (op) == ARRAY_REF
8552 && integer_zerop (TREE_OPERAND (op, 1))
8553 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
8554 return integer_one_node;
8557 /* If this expression has side effects, show we don't know it to be a
8558 constant. Likewise if it's a pointer or aggregate type since in
8559 those case we only want literals, since those are only optimized
8560 when generating RTL, not later.
8561 And finally, if we are compiling an initializer, not code, we
8562 need to return a definite result now; there's not going to be any
8563 more optimization done. */
8564 if (TREE_SIDE_EFFECTS (arg)
8565 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
8566 || POINTER_TYPE_P (TREE_TYPE (arg))
8567 || cfun == 0
8568 || folding_initializer
8569 || force_folding_builtin_constant_p)
8570 return integer_zero_node;
8572 return NULL_TREE;
8575 /* Create builtin_expect or builtin_expect_with_probability
8576 with PRED and EXPECTED as its arguments and return it as a truthvalue.
8577 Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
8578 builtin_expect_with_probability instead uses third argument as PROBABILITY
8579 value. */
8581 static tree
8582 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
8583 tree predictor, tree probability)
8585 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
8587 fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT
8588 : BUILT_IN_EXPECT_WITH_PROBABILITY);
8589 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
8590 ret_type = TREE_TYPE (TREE_TYPE (fn));
8591 pred_type = TREE_VALUE (arg_types);
8592 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
8594 pred = fold_convert_loc (loc, pred_type, pred);
8595 expected = fold_convert_loc (loc, expected_type, expected);
8597 if (probability)
8598 call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability);
8599 else
8600 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
8601 predictor);
8603 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
8604 build_int_cst (ret_type, 0));
8607 /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
8608 NULL_TREE if no simplification is possible. */
8610 tree
8611 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2,
8612 tree arg3)
8614 tree inner, fndecl, inner_arg0;
8615 enum tree_code code;
8617 /* Distribute the expected value over short-circuiting operators.
8618 See through the cast from truthvalue_type_node to long. */
8619 inner_arg0 = arg0;
8620 while (CONVERT_EXPR_P (inner_arg0)
8621 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
8622 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
8623 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
8625 /* If this is a builtin_expect within a builtin_expect keep the
8626 inner one. See through a comparison against a constant. It
8627 might have been added to create a thruthvalue. */
8628 inner = inner_arg0;
8630 if (COMPARISON_CLASS_P (inner)
8631 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
8632 inner = TREE_OPERAND (inner, 0);
8634 if (TREE_CODE (inner) == CALL_EXPR
8635 && (fndecl = get_callee_fndecl (inner))
8636 && (fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
8637 || fndecl_built_in_p (fndecl, BUILT_IN_EXPECT_WITH_PROBABILITY)))
8638 return arg0;
8640 inner = inner_arg0;
8641 code = TREE_CODE (inner);
8642 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
8644 tree op0 = TREE_OPERAND (inner, 0);
8645 tree op1 = TREE_OPERAND (inner, 1);
8646 arg1 = save_expr (arg1);
8648 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3);
8649 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3);
8650 inner = build2 (code, TREE_TYPE (inner), op0, op1);
8652 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
8655 /* If the argument isn't invariant then there's nothing else we can do. */
8656 if (!TREE_CONSTANT (inner_arg0))
8657 return NULL_TREE;
8659 /* If we expect that a comparison against the argument will fold to
8660 a constant return the constant. In practice, this means a true
8661 constant or the address of a non-weak symbol. */
8662 inner = inner_arg0;
8663 STRIP_NOPS (inner);
8664 if (TREE_CODE (inner) == ADDR_EXPR)
8668 inner = TREE_OPERAND (inner, 0);
8670 while (TREE_CODE (inner) == COMPONENT_REF
8671 || TREE_CODE (inner) == ARRAY_REF);
8672 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
8673 return NULL_TREE;
8676 /* Otherwise, ARG0 already has the proper type for the return value. */
8677 return arg0;
8680 /* Fold a call to __builtin_classify_type with argument ARG. */
8682 static tree
8683 fold_builtin_classify_type (tree arg)
8685 if (arg == 0)
8686 return build_int_cst (integer_type_node, no_type_class);
8688 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
8691 /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
8692 ARG. */
8694 static tree
8695 fold_builtin_strlen (location_t loc, tree expr, tree type, tree arg)
8697 if (!validate_arg (arg, POINTER_TYPE))
8698 return NULL_TREE;
8699 else
8701 c_strlen_data lendata = { };
8702 tree len = c_strlen (arg, 0, &lendata);
8704 if (len)
8705 return fold_convert_loc (loc, type, len);
8707 /* TODO: Move this to gimple-ssa-warn-access once the pass runs
8708 also early enough to detect invalid reads in multimensional
8709 arrays and struct members. */
8710 if (!lendata.decl)
8711 c_strlen (arg, 1, &lendata);
8713 if (lendata.decl)
8715 if (EXPR_HAS_LOCATION (arg))
8716 loc = EXPR_LOCATION (arg);
8717 else if (loc == UNKNOWN_LOCATION)
8718 loc = input_location;
8719 warn_string_no_nul (loc, expr, "strlen", arg, lendata.decl);
8722 return NULL_TREE;
8726 /* Fold a call to __builtin_inf or __builtin_huge_val. */
8728 static tree
8729 fold_builtin_inf (location_t loc, tree type, int warn)
8731 /* __builtin_inff is intended to be usable to define INFINITY on all
8732 targets. If an infinity is not available, INFINITY expands "to a
8733 positive constant of type float that overflows at translation
8734 time", footnote "In this case, using INFINITY will violate the
8735 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
8736 Thus we pedwarn to ensure this constraint violation is
8737 diagnosed. */
8738 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
8739 pedwarn (loc, 0, "target format does not support infinity");
8741 return build_real (type, dconstinf);
8744 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8745 NULL_TREE if no simplification can be made. */
8747 static tree
8748 fold_builtin_sincos (location_t loc,
8749 tree arg0, tree arg1, tree arg2)
8751 tree type;
8752 tree fndecl, call = NULL_TREE;
8754 if (!validate_arg (arg0, REAL_TYPE)
8755 || !validate_arg (arg1, POINTER_TYPE)
8756 || !validate_arg (arg2, POINTER_TYPE))
8757 return NULL_TREE;
8759 type = TREE_TYPE (arg0);
8761 /* Calculate the result when the argument is a constant. */
8762 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
8763 if (fn == END_BUILTINS)
8764 return NULL_TREE;
8766 /* Canonicalize sincos to cexpi. */
8767 if (TREE_CODE (arg0) == REAL_CST)
8769 tree complex_type = build_complex_type (type);
8770 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
8772 if (!call)
8774 if (!targetm.libc_has_function (function_c99_math_complex, type)
8775 || !builtin_decl_implicit_p (fn))
8776 return NULL_TREE;
8777 fndecl = builtin_decl_explicit (fn);
8778 call = build_call_expr_loc (loc, fndecl, 1, arg0);
8779 call = builtin_save_expr (call);
8782 tree ptype = build_pointer_type (type);
8783 arg1 = fold_convert (ptype, arg1);
8784 arg2 = fold_convert (ptype, arg2);
8785 return build2 (COMPOUND_EXPR, void_type_node,
8786 build2 (MODIFY_EXPR, void_type_node,
8787 build_fold_indirect_ref_loc (loc, arg1),
8788 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
8789 build2 (MODIFY_EXPR, void_type_node,
8790 build_fold_indirect_ref_loc (loc, arg2),
8791 fold_build1_loc (loc, REALPART_EXPR, type, call)));
8794 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8795 Return NULL_TREE if no simplification can be made. */
8797 static tree
8798 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8800 if (!validate_arg (arg1, POINTER_TYPE)
8801 || !validate_arg (arg2, POINTER_TYPE)
8802 || !validate_arg (len, INTEGER_TYPE))
8803 return NULL_TREE;
8805 /* If the LEN parameter is zero, return zero. */
8806 if (integer_zerop (len))
8807 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8808 arg1, arg2);
8810 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8811 if (operand_equal_p (arg1, arg2, 0))
8812 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8814 /* If len parameter is one, return an expression corresponding to
8815 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8816 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8818 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8819 tree cst_uchar_ptr_node
8820 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8822 tree ind1
8823 = fold_convert_loc (loc, integer_type_node,
8824 build1 (INDIRECT_REF, cst_uchar_node,
8825 fold_convert_loc (loc,
8826 cst_uchar_ptr_node,
8827 arg1)));
8828 tree ind2
8829 = fold_convert_loc (loc, integer_type_node,
8830 build1 (INDIRECT_REF, cst_uchar_node,
8831 fold_convert_loc (loc,
8832 cst_uchar_ptr_node,
8833 arg2)));
8834 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8837 return NULL_TREE;
8840 /* Fold a call to builtin isascii with argument ARG. */
8842 static tree
8843 fold_builtin_isascii (location_t loc, tree arg)
8845 if (!validate_arg (arg, INTEGER_TYPE))
8846 return NULL_TREE;
8847 else
8849 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8850 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8851 build_int_cst (integer_type_node,
8852 ~ (unsigned HOST_WIDE_INT) 0x7f));
8853 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8854 arg, integer_zero_node);
8858 /* Fold a call to builtin toascii with argument ARG. */
8860 static tree
8861 fold_builtin_toascii (location_t loc, tree arg)
8863 if (!validate_arg (arg, INTEGER_TYPE))
8864 return NULL_TREE;
8866 /* Transform toascii(c) -> (c & 0x7f). */
8867 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8868 build_int_cst (integer_type_node, 0x7f));
8871 /* Fold a call to builtin isdigit with argument ARG. */
8873 static tree
8874 fold_builtin_isdigit (location_t loc, tree arg)
8876 if (!validate_arg (arg, INTEGER_TYPE))
8877 return NULL_TREE;
8878 else
8880 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8881 /* According to the C standard, isdigit is unaffected by locale.
8882 However, it definitely is affected by the target character set. */
8883 unsigned HOST_WIDE_INT target_digit0
8884 = lang_hooks.to_target_charset ('0');
8886 if (target_digit0 == 0)
8887 return NULL_TREE;
8889 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8890 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8891 build_int_cst (unsigned_type_node, target_digit0));
8892 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8893 build_int_cst (unsigned_type_node, 9));
8897 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8899 static tree
8900 fold_builtin_fabs (location_t loc, tree arg, tree type)
8902 if (!validate_arg (arg, REAL_TYPE))
8903 return NULL_TREE;
8905 arg = fold_convert_loc (loc, type, arg);
8906 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8909 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8911 static tree
8912 fold_builtin_abs (location_t loc, tree arg, tree type)
8914 if (!validate_arg (arg, INTEGER_TYPE))
8915 return NULL_TREE;
8917 arg = fold_convert_loc (loc, type, arg);
8918 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8921 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8923 static tree
8924 fold_builtin_carg (location_t loc, tree arg, tree type)
8926 if (validate_arg (arg, COMPLEX_TYPE)
8927 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8929 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8931 if (atan2_fn)
8933 tree new_arg = builtin_save_expr (arg);
8934 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8935 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8936 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8940 return NULL_TREE;
8943 /* Fold a call to builtin frexp, we can assume the base is 2. */
8945 static tree
8946 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
8948 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8949 return NULL_TREE;
8951 STRIP_NOPS (arg0);
8953 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8954 return NULL_TREE;
8956 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8958 /* Proceed if a valid pointer type was passed in. */
8959 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
8961 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8962 tree frac, exp, res;
8964 switch (value->cl)
8966 case rvc_zero:
8967 /* For +-0, return (*exp = 0, +-0). */
8968 exp = integer_zero_node;
8969 frac = arg0;
8970 break;
8971 case rvc_nan:
8972 case rvc_inf:
8973 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8974 return omit_one_operand_loc (loc, rettype, arg0, arg1);
8975 case rvc_normal:
8977 /* Since the frexp function always expects base 2, and in
8978 GCC normalized significands are already in the range
8979 [0.5, 1.0), we have exactly what frexp wants. */
8980 REAL_VALUE_TYPE frac_rvt = *value;
8981 SET_REAL_EXP (&frac_rvt, 0);
8982 frac = build_real (rettype, frac_rvt);
8983 exp = build_int_cst (integer_type_node, REAL_EXP (value));
8985 break;
8986 default:
8987 gcc_unreachable ();
8990 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8991 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
8992 TREE_SIDE_EFFECTS (arg1) = 1;
8993 res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
8994 suppress_warning (res, OPT_Wunused_value);
8995 return res;
8998 return NULL_TREE;
9001 /* Fold a call to builtin modf. */
9003 static tree
9004 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
9006 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
9007 return NULL_TREE;
9009 STRIP_NOPS (arg0);
9011 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
9012 return NULL_TREE;
9014 arg1 = build_fold_indirect_ref_loc (loc, arg1);
9016 /* Proceed if a valid pointer type was passed in. */
9017 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
9019 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
9020 REAL_VALUE_TYPE trunc, frac;
9021 tree res;
9023 switch (value->cl)
9025 case rvc_nan:
9026 case rvc_zero:
9027 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9028 trunc = frac = *value;
9029 break;
9030 case rvc_inf:
9031 /* For +-Inf, return (*arg1 = arg0, +-0). */
9032 frac = dconst0;
9033 frac.sign = value->sign;
9034 trunc = *value;
9035 break;
9036 case rvc_normal:
9037 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9038 real_trunc (&trunc, VOIDmode, value);
9039 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9040 /* If the original number was negative and already
9041 integral, then the fractional part is -0.0. */
9042 if (value->sign && frac.cl == rvc_zero)
9043 frac.sign = value->sign;
9044 break;
9047 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9048 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9049 build_real (rettype, trunc));
9050 TREE_SIDE_EFFECTS (arg1) = 1;
9051 res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9052 build_real (rettype, frac));
9053 suppress_warning (res, OPT_Wunused_value);
9054 return res;
9057 return NULL_TREE;
9060 /* Given a location LOC, an interclass builtin function decl FNDECL
9061 and its single argument ARG, return an folded expression computing
9062 the same, or NULL_TREE if we either couldn't or didn't want to fold
9063 (the latter happen if there's an RTL instruction available). */
9065 static tree
9066 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9068 machine_mode mode;
9070 if (!validate_arg (arg, REAL_TYPE))
9071 return NULL_TREE;
9073 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9074 return NULL_TREE;
9076 mode = TYPE_MODE (TREE_TYPE (arg));
9078 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
9080 /* If there is no optab, try generic code. */
9081 switch (DECL_FUNCTION_CODE (fndecl))
9083 tree result;
9085 CASE_FLT_FN (BUILT_IN_ISINF):
9087 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9088 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9089 tree type = TREE_TYPE (arg);
9090 REAL_VALUE_TYPE r;
9091 char buf[128];
9093 if (is_ibm_extended)
9095 /* NaN and Inf are encoded in the high-order double value
9096 only. The low-order value is not significant. */
9097 type = double_type_node;
9098 mode = DFmode;
9099 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9101 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9102 real_from_string (&r, buf);
9103 result = build_call_expr (isgr_fn, 2,
9104 fold_build1_loc (loc, ABS_EXPR, type, arg),
9105 build_real (type, r));
9106 return result;
9108 CASE_FLT_FN (BUILT_IN_FINITE):
9109 case BUILT_IN_ISFINITE:
9111 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9112 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9113 tree type = TREE_TYPE (arg);
9114 REAL_VALUE_TYPE r;
9115 char buf[128];
9117 if (is_ibm_extended)
9119 /* NaN and Inf are encoded in the high-order double value
9120 only. The low-order value is not significant. */
9121 type = double_type_node;
9122 mode = DFmode;
9123 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9125 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9126 real_from_string (&r, buf);
9127 result = build_call_expr (isle_fn, 2,
9128 fold_build1_loc (loc, ABS_EXPR, type, arg),
9129 build_real (type, r));
9130 /*result = fold_build2_loc (loc, UNGT_EXPR,
9131 TREE_TYPE (TREE_TYPE (fndecl)),
9132 fold_build1_loc (loc, ABS_EXPR, type, arg),
9133 build_real (type, r));
9134 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9135 TREE_TYPE (TREE_TYPE (fndecl)),
9136 result);*/
9137 return result;
9139 case BUILT_IN_ISNORMAL:
9141 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9142 islessequal(fabs(x),DBL_MAX). */
9143 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9144 tree type = TREE_TYPE (arg);
9145 tree orig_arg, max_exp, min_exp;
9146 machine_mode orig_mode = mode;
9147 REAL_VALUE_TYPE rmax, rmin;
9148 char buf[128];
9150 orig_arg = arg = builtin_save_expr (arg);
9151 if (is_ibm_extended)
9153 /* Use double to test the normal range of IBM extended
9154 precision. Emin for IBM extended precision is
9155 different to emin for IEEE double, being 53 higher
9156 since the low double exponent is at least 53 lower
9157 than the high double exponent. */
9158 type = double_type_node;
9159 mode = DFmode;
9160 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9162 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
9164 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9165 real_from_string (&rmax, buf);
9166 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9167 real_from_string (&rmin, buf);
9168 max_exp = build_real (type, rmax);
9169 min_exp = build_real (type, rmin);
9171 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
9172 if (is_ibm_extended)
9174 /* Testing the high end of the range is done just using
9175 the high double, using the same test as isfinite().
9176 For the subnormal end of the range we first test the
9177 high double, then if its magnitude is equal to the
9178 limit of 0x1p-969, we test whether the low double is
9179 non-zero and opposite sign to the high double. */
9180 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
9181 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9182 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
9183 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
9184 arg, min_exp);
9185 tree as_complex = build1 (VIEW_CONVERT_EXPR,
9186 complex_double_type_node, orig_arg);
9187 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
9188 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
9189 tree zero = build_real (type, dconst0);
9190 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
9191 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
9192 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
9193 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
9194 fold_build3 (COND_EXPR,
9195 integer_type_node,
9196 hilt, logt, lolt));
9197 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
9198 eq_min, ok_lo);
9199 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
9200 gt_min, eq_min);
9202 else
9204 tree const isge_fn
9205 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9206 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
9208 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
9209 max_exp, min_exp);
9210 return result;
9212 default:
9213 break;
9216 return NULL_TREE;
9219 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9220 ARG is the argument for the call. */
9222 static tree
9223 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9225 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9227 if (!validate_arg (arg, REAL_TYPE))
9228 return NULL_TREE;
9230 switch (builtin_index)
9232 case BUILT_IN_ISINF:
9233 if (tree_expr_infinite_p (arg))
9234 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9235 if (!tree_expr_maybe_infinite_p (arg))
9236 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9237 return NULL_TREE;
9239 case BUILT_IN_ISINF_SIGN:
9241 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9242 /* In a boolean context, GCC will fold the inner COND_EXPR to
9243 1. So e.g. "if (isinf_sign(x))" would be folded to just
9244 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9245 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
9246 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9247 tree tmp = NULL_TREE;
9249 arg = builtin_save_expr (arg);
9251 if (signbit_fn && isinf_fn)
9253 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9254 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9256 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9257 signbit_call, integer_zero_node);
9258 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9259 isinf_call, integer_zero_node);
9261 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9262 integer_minus_one_node, integer_one_node);
9263 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9264 isinf_call, tmp,
9265 integer_zero_node);
9268 return tmp;
9271 case BUILT_IN_ISFINITE:
9272 if (tree_expr_finite_p (arg))
9273 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9274 if (tree_expr_nan_p (arg) || tree_expr_infinite_p (arg))
9275 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9276 return NULL_TREE;
9278 case BUILT_IN_ISNAN:
9279 if (tree_expr_nan_p (arg))
9280 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9281 if (!tree_expr_maybe_nan_p (arg))
9282 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9285 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
9286 if (is_ibm_extended)
9288 /* NaN and Inf are encoded in the high-order double value
9289 only. The low-order value is not significant. */
9290 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
9293 arg = builtin_save_expr (arg);
9294 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9296 case BUILT_IN_ISSIGNALING:
9297 /* Folding to true for REAL_CST is done in fold_const_call_ss.
9298 Don't use tree_expr_signaling_nan_p (arg) -> integer_one_node
9299 and !tree_expr_maybe_signaling_nan_p (arg) -> integer_zero_node
9300 here, so there is some possibility of __builtin_issignaling working
9301 without -fsignaling-nans. Especially when -fno-signaling-nans is
9302 the default. */
9303 if (!tree_expr_maybe_nan_p (arg))
9304 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9305 return NULL_TREE;
9307 default:
9308 gcc_unreachable ();
9312 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9313 This builtin will generate code to return the appropriate floating
9314 point classification depending on the value of the floating point
9315 number passed in. The possible return values must be supplied as
9316 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9317 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9318 one floating point argument which is "type generic". */
9320 static tree
9321 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9323 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9324 arg, type, res, tmp;
9325 machine_mode mode;
9326 REAL_VALUE_TYPE r;
9327 char buf[128];
9329 /* Verify the required arguments in the original call. */
9330 if (nargs != 6
9331 || !validate_arg (args[0], INTEGER_TYPE)
9332 || !validate_arg (args[1], INTEGER_TYPE)
9333 || !validate_arg (args[2], INTEGER_TYPE)
9334 || !validate_arg (args[3], INTEGER_TYPE)
9335 || !validate_arg (args[4], INTEGER_TYPE)
9336 || !validate_arg (args[5], REAL_TYPE))
9337 return NULL_TREE;
9339 fp_nan = args[0];
9340 fp_infinite = args[1];
9341 fp_normal = args[2];
9342 fp_subnormal = args[3];
9343 fp_zero = args[4];
9344 arg = args[5];
9345 type = TREE_TYPE (arg);
9346 mode = TYPE_MODE (type);
9347 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9349 /* fpclassify(x) ->
9350 isnan(x) ? FP_NAN :
9351 (fabs(x) == Inf ? FP_INFINITE :
9352 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9353 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9355 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9356 build_real (type, dconst0));
9357 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9358 tmp, fp_zero, fp_subnormal);
9360 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9361 real_from_string (&r, buf);
9362 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9363 arg, build_real (type, r));
9364 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9366 if (tree_expr_maybe_infinite_p (arg))
9368 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9369 build_real (type, dconstinf));
9370 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9371 fp_infinite, res);
9374 if (tree_expr_maybe_nan_p (arg))
9376 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9377 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9380 return res;
9383 /* Fold a call to an unordered comparison function such as
9384 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9385 being called and ARG0 and ARG1 are the arguments for the call.
9386 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9387 the opposite of the desired result. UNORDERED_CODE is used
9388 for modes that can hold NaNs and ORDERED_CODE is used for
9389 the rest. */
9391 static tree
9392 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9393 enum tree_code unordered_code,
9394 enum tree_code ordered_code)
9396 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9397 enum tree_code code;
9398 tree type0, type1;
9399 enum tree_code code0, code1;
9400 tree cmp_type = NULL_TREE;
9402 type0 = TREE_TYPE (arg0);
9403 type1 = TREE_TYPE (arg1);
9405 code0 = TREE_CODE (type0);
9406 code1 = TREE_CODE (type1);
9408 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9409 /* Choose the wider of two real types. */
9410 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9411 ? type0 : type1;
9412 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9413 cmp_type = type0;
9414 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9415 cmp_type = type1;
9417 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9418 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9420 if (unordered_code == UNORDERED_EXPR)
9422 if (tree_expr_nan_p (arg0) || tree_expr_nan_p (arg1))
9423 return omit_two_operands_loc (loc, type, integer_one_node, arg0, arg1);
9424 if (!tree_expr_maybe_nan_p (arg0) && !tree_expr_maybe_nan_p (arg1))
9425 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9426 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9429 code = (tree_expr_maybe_nan_p (arg0) || tree_expr_maybe_nan_p (arg1))
9430 ? unordered_code : ordered_code;
9431 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9432 fold_build2_loc (loc, code, type, arg0, arg1));
9435 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9436 arithmetics if it can never overflow, or into internal functions that
9437 return both result of arithmetics and overflowed boolean flag in
9438 a complex integer result, or some other check for overflow.
9439 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
9440 checking part of that. */
9442 static tree
9443 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
9444 tree arg0, tree arg1, tree arg2)
9446 enum internal_fn ifn = IFN_LAST;
9447 /* The code of the expression corresponding to the built-in. */
9448 enum tree_code opcode = ERROR_MARK;
9449 bool ovf_only = false;
9451 switch (fcode)
9453 case BUILT_IN_ADD_OVERFLOW_P:
9454 ovf_only = true;
9455 /* FALLTHRU */
9456 case BUILT_IN_ADD_OVERFLOW:
9457 case BUILT_IN_SADD_OVERFLOW:
9458 case BUILT_IN_SADDL_OVERFLOW:
9459 case BUILT_IN_SADDLL_OVERFLOW:
9460 case BUILT_IN_UADD_OVERFLOW:
9461 case BUILT_IN_UADDL_OVERFLOW:
9462 case BUILT_IN_UADDLL_OVERFLOW:
9463 opcode = PLUS_EXPR;
9464 ifn = IFN_ADD_OVERFLOW;
9465 break;
9466 case BUILT_IN_SUB_OVERFLOW_P:
9467 ovf_only = true;
9468 /* FALLTHRU */
9469 case BUILT_IN_SUB_OVERFLOW:
9470 case BUILT_IN_SSUB_OVERFLOW:
9471 case BUILT_IN_SSUBL_OVERFLOW:
9472 case BUILT_IN_SSUBLL_OVERFLOW:
9473 case BUILT_IN_USUB_OVERFLOW:
9474 case BUILT_IN_USUBL_OVERFLOW:
9475 case BUILT_IN_USUBLL_OVERFLOW:
9476 opcode = MINUS_EXPR;
9477 ifn = IFN_SUB_OVERFLOW;
9478 break;
9479 case BUILT_IN_MUL_OVERFLOW_P:
9480 ovf_only = true;
9481 /* FALLTHRU */
9482 case BUILT_IN_MUL_OVERFLOW:
9483 case BUILT_IN_SMUL_OVERFLOW:
9484 case BUILT_IN_SMULL_OVERFLOW:
9485 case BUILT_IN_SMULLL_OVERFLOW:
9486 case BUILT_IN_UMUL_OVERFLOW:
9487 case BUILT_IN_UMULL_OVERFLOW:
9488 case BUILT_IN_UMULLL_OVERFLOW:
9489 opcode = MULT_EXPR;
9490 ifn = IFN_MUL_OVERFLOW;
9491 break;
9492 default:
9493 gcc_unreachable ();
9496 /* For the "generic" overloads, the first two arguments can have different
9497 types and the last argument determines the target type to use to check
9498 for overflow. The arguments of the other overloads all have the same
9499 type. */
9500 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
9502 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
9503 arguments are constant, attempt to fold the built-in call into a constant
9504 expression indicating whether or not it detected an overflow. */
9505 if (ovf_only
9506 && TREE_CODE (arg0) == INTEGER_CST
9507 && TREE_CODE (arg1) == INTEGER_CST)
9508 /* Perform the computation in the target type and check for overflow. */
9509 return omit_one_operand_loc (loc, boolean_type_node,
9510 arith_overflowed_p (opcode, type, arg0, arg1)
9511 ? boolean_true_node : boolean_false_node,
9512 arg2);
9514 tree intres, ovfres;
9515 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9517 intres = fold_binary_loc (loc, opcode, type,
9518 fold_convert_loc (loc, type, arg0),
9519 fold_convert_loc (loc, type, arg1));
9520 if (TREE_OVERFLOW (intres))
9521 intres = drop_tree_overflow (intres);
9522 ovfres = (arith_overflowed_p (opcode, type, arg0, arg1)
9523 ? boolean_true_node : boolean_false_node);
9525 else
9527 tree ctype = build_complex_type (type);
9528 tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
9529 arg0, arg1);
9530 tree tgt = save_expr (call);
9531 intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9532 ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9533 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
9536 if (ovf_only)
9537 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
9539 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
9540 tree store
9541 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
9542 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
9545 /* Fold a call to __builtin_FILE to a constant string. */
9547 static inline tree
9548 fold_builtin_FILE (location_t loc)
9550 if (const char *fname = LOCATION_FILE (loc))
9552 /* The documentation says this builtin is equivalent to the preprocessor
9553 __FILE__ macro so it appears appropriate to use the same file prefix
9554 mappings. */
9555 fname = remap_macro_filename (fname);
9556 return build_string_literal (fname);
9559 return build_string_literal ("");
9562 /* Fold a call to __builtin_FUNCTION to a constant string. */
9564 static inline tree
9565 fold_builtin_FUNCTION ()
9567 const char *name = "";
9569 if (current_function_decl)
9570 name = lang_hooks.decl_printable_name (current_function_decl, 0);
9572 return build_string_literal (name);
9575 /* Fold a call to __builtin_LINE to an integer constant. */
9577 static inline tree
9578 fold_builtin_LINE (location_t loc, tree type)
9580 return build_int_cst (type, LOCATION_LINE (loc));
9583 /* Fold a call to built-in function FNDECL with 0 arguments.
9584 This function returns NULL_TREE if no simplification was possible. */
9586 static tree
9587 fold_builtin_0 (location_t loc, tree fndecl)
9589 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9590 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9591 switch (fcode)
9593 case BUILT_IN_FILE:
9594 return fold_builtin_FILE (loc);
9596 case BUILT_IN_FUNCTION:
9597 return fold_builtin_FUNCTION ();
9599 case BUILT_IN_LINE:
9600 return fold_builtin_LINE (loc, type);
9602 CASE_FLT_FN (BUILT_IN_INF):
9603 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
9604 case BUILT_IN_INFD32:
9605 case BUILT_IN_INFD64:
9606 case BUILT_IN_INFD128:
9607 return fold_builtin_inf (loc, type, true);
9609 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9610 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
9611 return fold_builtin_inf (loc, type, false);
9613 case BUILT_IN_CLASSIFY_TYPE:
9614 return fold_builtin_classify_type (NULL_TREE);
9616 case BUILT_IN_UNREACHABLE:
9617 /* Rewrite any explicit calls to __builtin_unreachable. */
9618 if (sanitize_flags_p (SANITIZE_UNREACHABLE))
9619 return build_builtin_unreachable (loc);
9620 break;
9622 default:
9623 break;
9625 return NULL_TREE;
9628 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9629 This function returns NULL_TREE if no simplification was possible. */
9631 static tree
9632 fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
9634 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9635 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9637 if (TREE_CODE (arg0) == ERROR_MARK)
9638 return NULL_TREE;
9640 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
9641 return ret;
9643 switch (fcode)
9645 case BUILT_IN_CONSTANT_P:
9647 tree val = fold_builtin_constant_p (arg0);
9649 /* Gimplification will pull the CALL_EXPR for the builtin out of
9650 an if condition. When not optimizing, we'll not CSE it back.
9651 To avoid link error types of regressions, return false now. */
9652 if (!val && !optimize)
9653 val = integer_zero_node;
9655 return val;
9658 case BUILT_IN_CLASSIFY_TYPE:
9659 return fold_builtin_classify_type (arg0);
9661 case BUILT_IN_STRLEN:
9662 return fold_builtin_strlen (loc, expr, type, arg0);
9664 CASE_FLT_FN (BUILT_IN_FABS):
9665 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
9666 case BUILT_IN_FABSD32:
9667 case BUILT_IN_FABSD64:
9668 case BUILT_IN_FABSD128:
9669 return fold_builtin_fabs (loc, arg0, type);
9671 case BUILT_IN_ABS:
9672 case BUILT_IN_LABS:
9673 case BUILT_IN_LLABS:
9674 case BUILT_IN_IMAXABS:
9675 return fold_builtin_abs (loc, arg0, type);
9677 CASE_FLT_FN (BUILT_IN_CONJ):
9678 if (validate_arg (arg0, COMPLEX_TYPE)
9679 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9680 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9681 break;
9683 CASE_FLT_FN (BUILT_IN_CREAL):
9684 if (validate_arg (arg0, COMPLEX_TYPE)
9685 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9686 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9687 break;
9689 CASE_FLT_FN (BUILT_IN_CIMAG):
9690 if (validate_arg (arg0, COMPLEX_TYPE)
9691 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9692 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9693 break;
9695 CASE_FLT_FN (BUILT_IN_CARG):
9696 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CARG):
9697 return fold_builtin_carg (loc, arg0, type);
9699 case BUILT_IN_ISASCII:
9700 return fold_builtin_isascii (loc, arg0);
9702 case BUILT_IN_TOASCII:
9703 return fold_builtin_toascii (loc, arg0);
9705 case BUILT_IN_ISDIGIT:
9706 return fold_builtin_isdigit (loc, arg0);
9708 CASE_FLT_FN (BUILT_IN_FINITE):
9709 case BUILT_IN_FINITED32:
9710 case BUILT_IN_FINITED64:
9711 case BUILT_IN_FINITED128:
9712 case BUILT_IN_ISFINITE:
9714 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
9715 if (ret)
9716 return ret;
9717 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9720 CASE_FLT_FN (BUILT_IN_ISINF):
9721 case BUILT_IN_ISINFD32:
9722 case BUILT_IN_ISINFD64:
9723 case BUILT_IN_ISINFD128:
9725 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
9726 if (ret)
9727 return ret;
9728 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9731 case BUILT_IN_ISNORMAL:
9732 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9734 case BUILT_IN_ISINF_SIGN:
9735 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
9737 CASE_FLT_FN (BUILT_IN_ISNAN):
9738 case BUILT_IN_ISNAND32:
9739 case BUILT_IN_ISNAND64:
9740 case BUILT_IN_ISNAND128:
9741 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
9743 case BUILT_IN_ISSIGNALING:
9744 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISSIGNALING);
9746 case BUILT_IN_FREE:
9747 if (integer_zerop (arg0))
9748 return build_empty_stmt (loc);
9749 break;
9751 default:
9752 break;
9755 return NULL_TREE;
9759 /* Folds a call EXPR (which may be null) to built-in function FNDECL
9760 with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
9761 if no simplification was possible. */
9763 static tree
9764 fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
9766 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9767 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9769 if (TREE_CODE (arg0) == ERROR_MARK
9770 || TREE_CODE (arg1) == ERROR_MARK)
9771 return NULL_TREE;
9773 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
9774 return ret;
9776 switch (fcode)
9778 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
9779 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
9780 if (validate_arg (arg0, REAL_TYPE)
9781 && validate_arg (arg1, POINTER_TYPE))
9782 return do_mpfr_lgamma_r (arg0, arg1, type);
9783 break;
9785 CASE_FLT_FN (BUILT_IN_FREXP):
9786 return fold_builtin_frexp (loc, arg0, arg1, type);
9788 CASE_FLT_FN (BUILT_IN_MODF):
9789 return fold_builtin_modf (loc, arg0, arg1, type);
9791 case BUILT_IN_STRSPN:
9792 return fold_builtin_strspn (loc, expr, arg0, arg1);
9794 case BUILT_IN_STRCSPN:
9795 return fold_builtin_strcspn (loc, expr, arg0, arg1);
9797 case BUILT_IN_STRPBRK:
9798 return fold_builtin_strpbrk (loc, expr, arg0, arg1, type);
9800 case BUILT_IN_EXPECT:
9801 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE);
9803 case BUILT_IN_ISGREATER:
9804 return fold_builtin_unordered_cmp (loc, fndecl,
9805 arg0, arg1, UNLE_EXPR, LE_EXPR);
9806 case BUILT_IN_ISGREATEREQUAL:
9807 return fold_builtin_unordered_cmp (loc, fndecl,
9808 arg0, arg1, UNLT_EXPR, LT_EXPR);
9809 case BUILT_IN_ISLESS:
9810 return fold_builtin_unordered_cmp (loc, fndecl,
9811 arg0, arg1, UNGE_EXPR, GE_EXPR);
9812 case BUILT_IN_ISLESSEQUAL:
9813 return fold_builtin_unordered_cmp (loc, fndecl,
9814 arg0, arg1, UNGT_EXPR, GT_EXPR);
9815 case BUILT_IN_ISLESSGREATER:
9816 return fold_builtin_unordered_cmp (loc, fndecl,
9817 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
9818 case BUILT_IN_ISUNORDERED:
9819 return fold_builtin_unordered_cmp (loc, fndecl,
9820 arg0, arg1, UNORDERED_EXPR,
9821 NOP_EXPR);
9823 /* We do the folding for va_start in the expander. */
9824 case BUILT_IN_VA_START:
9825 break;
9827 case BUILT_IN_OBJECT_SIZE:
9828 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
9829 return fold_builtin_object_size (arg0, arg1, fcode);
9831 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
9832 return fold_builtin_atomic_always_lock_free (arg0, arg1);
9834 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
9835 return fold_builtin_atomic_is_lock_free (arg0, arg1);
9837 default:
9838 break;
9840 return NULL_TREE;
9843 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
9844 and ARG2.
9845 This function returns NULL_TREE if no simplification was possible. */
9847 static tree
9848 fold_builtin_3 (location_t loc, tree fndecl,
9849 tree arg0, tree arg1, tree arg2)
9851 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9852 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9854 if (TREE_CODE (arg0) == ERROR_MARK
9855 || TREE_CODE (arg1) == ERROR_MARK
9856 || TREE_CODE (arg2) == ERROR_MARK)
9857 return NULL_TREE;
9859 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
9860 arg0, arg1, arg2))
9861 return ret;
9863 switch (fcode)
9866 CASE_FLT_FN (BUILT_IN_SINCOS):
9867 return fold_builtin_sincos (loc, arg0, arg1, arg2);
9869 CASE_FLT_FN (BUILT_IN_REMQUO):
9870 if (validate_arg (arg0, REAL_TYPE)
9871 && validate_arg (arg1, REAL_TYPE)
9872 && validate_arg (arg2, POINTER_TYPE))
9873 return do_mpfr_remquo (arg0, arg1, arg2);
9874 break;
9876 case BUILT_IN_MEMCMP:
9877 return fold_builtin_memcmp (loc, arg0, arg1, arg2);
9879 case BUILT_IN_EXPECT:
9880 return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE);
9882 case BUILT_IN_EXPECT_WITH_PROBABILITY:
9883 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2);
9885 case BUILT_IN_ADD_OVERFLOW:
9886 case BUILT_IN_SUB_OVERFLOW:
9887 case BUILT_IN_MUL_OVERFLOW:
9888 case BUILT_IN_ADD_OVERFLOW_P:
9889 case BUILT_IN_SUB_OVERFLOW_P:
9890 case BUILT_IN_MUL_OVERFLOW_P:
9891 case BUILT_IN_SADD_OVERFLOW:
9892 case BUILT_IN_SADDL_OVERFLOW:
9893 case BUILT_IN_SADDLL_OVERFLOW:
9894 case BUILT_IN_SSUB_OVERFLOW:
9895 case BUILT_IN_SSUBL_OVERFLOW:
9896 case BUILT_IN_SSUBLL_OVERFLOW:
9897 case BUILT_IN_SMUL_OVERFLOW:
9898 case BUILT_IN_SMULL_OVERFLOW:
9899 case BUILT_IN_SMULLL_OVERFLOW:
9900 case BUILT_IN_UADD_OVERFLOW:
9901 case BUILT_IN_UADDL_OVERFLOW:
9902 case BUILT_IN_UADDLL_OVERFLOW:
9903 case BUILT_IN_USUB_OVERFLOW:
9904 case BUILT_IN_USUBL_OVERFLOW:
9905 case BUILT_IN_USUBLL_OVERFLOW:
9906 case BUILT_IN_UMUL_OVERFLOW:
9907 case BUILT_IN_UMULL_OVERFLOW:
9908 case BUILT_IN_UMULLL_OVERFLOW:
9909 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
9911 default:
9912 break;
9914 return NULL_TREE;
9917 /* Folds a call EXPR (which may be null) to built-in function FNDECL.
9918 ARGS is an array of NARGS arguments. IGNORE is true if the result
9919 of the function call is ignored. This function returns NULL_TREE
9920 if no simplification was possible. */
9922 static tree
9923 fold_builtin_n (location_t loc, tree expr, tree fndecl, tree *args,
9924 int nargs, bool)
9926 tree ret = NULL_TREE;
9928 switch (nargs)
9930 case 0:
9931 ret = fold_builtin_0 (loc, fndecl);
9932 break;
9933 case 1:
9934 ret = fold_builtin_1 (loc, expr, fndecl, args[0]);
9935 break;
9936 case 2:
9937 ret = fold_builtin_2 (loc, expr, fndecl, args[0], args[1]);
9938 break;
9939 case 3:
9940 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
9941 break;
9942 default:
9943 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
9944 break;
9946 if (ret)
9948 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
9949 SET_EXPR_LOCATION (ret, loc);
9950 return ret;
9952 return NULL_TREE;
9955 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
9956 list ARGS along with N new arguments in NEWARGS. SKIP is the number
9957 of arguments in ARGS to be omitted. OLDNARGS is the number of
9958 elements in ARGS. */
9960 static tree
9961 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
9962 int skip, tree fndecl, int n, va_list newargs)
9964 int nargs = oldnargs - skip + n;
9965 tree *buffer;
9967 if (n > 0)
9969 int i, j;
9971 buffer = XALLOCAVEC (tree, nargs);
9972 for (i = 0; i < n; i++)
9973 buffer[i] = va_arg (newargs, tree);
9974 for (j = skip; j < oldnargs; j++, i++)
9975 buffer[i] = args[j];
9977 else
9978 buffer = args + skip;
9980 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
9983 /* Return true if FNDECL shouldn't be folded right now.
9984 If a built-in function has an inline attribute always_inline
9985 wrapper, defer folding it after always_inline functions have
9986 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
9987 might not be performed. */
9989 bool
9990 avoid_folding_inline_builtin (tree fndecl)
9992 return (DECL_DECLARED_INLINE_P (fndecl)
9993 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
9994 && cfun
9995 && !cfun->always_inline_functions_inlined
9996 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
9999 /* A wrapper function for builtin folding that prevents warnings for
10000 "statement without effect" and the like, caused by removing the
10001 call node earlier than the warning is generated. */
10003 tree
10004 fold_call_expr (location_t loc, tree exp, bool ignore)
10006 tree ret = NULL_TREE;
10007 tree fndecl = get_callee_fndecl (exp);
10008 if (fndecl && fndecl_built_in_p (fndecl)
10009 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
10010 yet. Defer folding until we see all the arguments
10011 (after inlining). */
10012 && !CALL_EXPR_VA_ARG_PACK (exp))
10014 int nargs = call_expr_nargs (exp);
10016 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
10017 instead last argument is __builtin_va_arg_pack (). Defer folding
10018 even in that case, until arguments are finalized. */
10019 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
10021 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
10022 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
10023 return NULL_TREE;
10026 if (avoid_folding_inline_builtin (fndecl))
10027 return NULL_TREE;
10029 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10030 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10031 CALL_EXPR_ARGP (exp), ignore);
10032 else
10034 tree *args = CALL_EXPR_ARGP (exp);
10035 ret = fold_builtin_n (loc, exp, fndecl, args, nargs, ignore);
10036 if (ret)
10037 return ret;
10040 return NULL_TREE;
10043 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
10044 N arguments are passed in the array ARGARRAY. Return a folded
10045 expression or NULL_TREE if no simplification was possible. */
10047 tree
10048 fold_builtin_call_array (location_t loc, tree,
10049 tree fn,
10050 int n,
10051 tree *argarray)
10053 if (TREE_CODE (fn) != ADDR_EXPR)
10054 return NULL_TREE;
10056 tree fndecl = TREE_OPERAND (fn, 0);
10057 if (TREE_CODE (fndecl) == FUNCTION_DECL
10058 && fndecl_built_in_p (fndecl))
10060 /* If last argument is __builtin_va_arg_pack (), arguments to this
10061 function are not finalized yet. Defer folding until they are. */
10062 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10064 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10065 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
10066 return NULL_TREE;
10068 if (avoid_folding_inline_builtin (fndecl))
10069 return NULL_TREE;
10070 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10071 return targetm.fold_builtin (fndecl, n, argarray, false);
10072 else
10073 return fold_builtin_n (loc, NULL_TREE, fndecl, argarray, n, false);
10076 return NULL_TREE;
10079 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10080 along with N new arguments specified as the "..." parameters. SKIP
10081 is the number of arguments in EXP to be omitted. This function is used
10082 to do varargs-to-varargs transformations. */
10084 static tree
10085 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10087 va_list ap;
10088 tree t;
10090 va_start (ap, n);
10091 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10092 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10093 va_end (ap);
10095 return t;
10098 /* Validate a single argument ARG against a tree code CODE representing
10099 a type. Return true when argument is valid. */
10101 static bool
10102 validate_arg (const_tree arg, enum tree_code code)
10104 if (!arg)
10105 return false;
10106 else if (code == POINTER_TYPE)
10107 return POINTER_TYPE_P (TREE_TYPE (arg));
10108 else if (code == INTEGER_TYPE)
10109 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10110 return code == TREE_CODE (TREE_TYPE (arg));
10113 /* This function validates the types of a function call argument list
10114 against a specified list of tree_codes. If the last specifier is a 0,
10115 that represents an ellipses, otherwise the last specifier must be a
10116 VOID_TYPE.
10118 This is the GIMPLE version of validate_arglist. Eventually we want to
10119 completely convert builtins.cc to work from GIMPLEs and the tree based
10120 validate_arglist will then be removed. */
10122 bool
10123 validate_gimple_arglist (const gcall *call, ...)
10125 enum tree_code code;
10126 bool res = 0;
10127 va_list ap;
10128 const_tree arg;
10129 size_t i;
10131 va_start (ap, call);
10132 i = 0;
10136 code = (enum tree_code) va_arg (ap, int);
10137 switch (code)
10139 case 0:
10140 /* This signifies an ellipses, any further arguments are all ok. */
10141 res = true;
10142 goto end;
10143 case VOID_TYPE:
10144 /* This signifies an endlink, if no arguments remain, return
10145 true, otherwise return false. */
10146 res = (i == gimple_call_num_args (call));
10147 goto end;
10148 default:
10149 /* If no parameters remain or the parameter's code does not
10150 match the specified code, return false. Otherwise continue
10151 checking any remaining arguments. */
10152 arg = gimple_call_arg (call, i++);
10153 if (!validate_arg (arg, code))
10154 goto end;
10155 break;
10158 while (1);
10160 /* We need gotos here since we can only have one VA_CLOSE in a
10161 function. */
10162 end: ;
10163 va_end (ap);
10165 return res;
10168 /* Default target-specific builtin expander that does nothing. */
10171 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10172 rtx target ATTRIBUTE_UNUSED,
10173 rtx subtarget ATTRIBUTE_UNUSED,
10174 machine_mode mode ATTRIBUTE_UNUSED,
10175 int ignore ATTRIBUTE_UNUSED)
10177 return NULL_RTX;
10180 /* Returns true is EXP represents data that would potentially reside
10181 in a readonly section. */
10183 bool
10184 readonly_data_expr (tree exp)
10186 STRIP_NOPS (exp);
10188 if (TREE_CODE (exp) != ADDR_EXPR)
10189 return false;
10191 exp = get_base_address (TREE_OPERAND (exp, 0));
10192 if (!exp)
10193 return false;
10195 /* Make sure we call decl_readonly_section only for trees it
10196 can handle (since it returns true for everything it doesn't
10197 understand). */
10198 if (TREE_CODE (exp) == STRING_CST
10199 || TREE_CODE (exp) == CONSTRUCTOR
10200 || (VAR_P (exp) && TREE_STATIC (exp)))
10201 return decl_readonly_section (exp, 0);
10202 else
10203 return false;
10206 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
10207 to the call, and TYPE is its return type.
10209 Return NULL_TREE if no simplification was possible, otherwise return the
10210 simplified form of the call as a tree.
10212 The simplified form may be a constant or other expression which
10213 computes the same value, but in a more efficient manner (including
10214 calls to other builtin functions).
10216 The call may contain arguments which need to be evaluated, but
10217 which are not useful to determine the result of the call. In
10218 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10219 COMPOUND_EXPR will be an argument which must be evaluated.
10220 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10221 COMPOUND_EXPR in the chain will contain the tree for the simplified
10222 form of the builtin function call. */
10224 static tree
10225 fold_builtin_strpbrk (location_t loc, tree, tree s1, tree s2, tree type)
10227 if (!validate_arg (s1, POINTER_TYPE)
10228 || !validate_arg (s2, POINTER_TYPE))
10229 return NULL_TREE;
10231 tree fn;
10232 const char *p1, *p2;
10234 p2 = c_getstr (s2);
10235 if (p2 == NULL)
10236 return NULL_TREE;
10238 p1 = c_getstr (s1);
10239 if (p1 != NULL)
10241 const char *r = strpbrk (p1, p2);
10242 tree tem;
10244 if (r == NULL)
10245 return build_int_cst (TREE_TYPE (s1), 0);
10247 /* Return an offset into the constant string argument. */
10248 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10249 return fold_convert_loc (loc, type, tem);
10252 if (p2[0] == '\0')
10253 /* strpbrk(x, "") == NULL.
10254 Evaluate and ignore s1 in case it had side-effects. */
10255 return omit_one_operand_loc (loc, type, integer_zero_node, s1);
10257 if (p2[1] != '\0')
10258 return NULL_TREE; /* Really call strpbrk. */
10260 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10261 if (!fn)
10262 return NULL_TREE;
10264 /* New argument list transforming strpbrk(s1, s2) to
10265 strchr(s1, s2[0]). */
10266 return build_call_expr_loc (loc, fn, 2, s1,
10267 build_int_cst (integer_type_node, p2[0]));
10270 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
10271 to the call.
10273 Return NULL_TREE if no simplification was possible, otherwise return the
10274 simplified form of the call as a tree.
10276 The simplified form may be a constant or other expression which
10277 computes the same value, but in a more efficient manner (including
10278 calls to other builtin functions).
10280 The call may contain arguments which need to be evaluated, but
10281 which are not useful to determine the result of the call. In
10282 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10283 COMPOUND_EXPR will be an argument which must be evaluated.
10284 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10285 COMPOUND_EXPR in the chain will contain the tree for the simplified
10286 form of the builtin function call. */
10288 static tree
10289 fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2)
10291 if (!validate_arg (s1, POINTER_TYPE)
10292 || !validate_arg (s2, POINTER_TYPE))
10293 return NULL_TREE;
10295 if (!check_nul_terminated_array (expr, s1)
10296 || !check_nul_terminated_array (expr, s2))
10297 return NULL_TREE;
10299 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10301 /* If either argument is "", return NULL_TREE. */
10302 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
10303 /* Evaluate and ignore both arguments in case either one has
10304 side-effects. */
10305 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
10306 s1, s2);
10307 return NULL_TREE;
10310 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
10311 to the call.
10313 Return NULL_TREE if no simplification was possible, otherwise return the
10314 simplified form of the call as a tree.
10316 The simplified form may be a constant or other expression which
10317 computes the same value, but in a more efficient manner (including
10318 calls to other builtin functions).
10320 The call may contain arguments which need to be evaluated, but
10321 which are not useful to determine the result of the call. In
10322 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10323 COMPOUND_EXPR will be an argument which must be evaluated.
10324 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10325 COMPOUND_EXPR in the chain will contain the tree for the simplified
10326 form of the builtin function call. */
10328 static tree
10329 fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2)
10331 if (!validate_arg (s1, POINTER_TYPE)
10332 || !validate_arg (s2, POINTER_TYPE))
10333 return NULL_TREE;
10335 if (!check_nul_terminated_array (expr, s1)
10336 || !check_nul_terminated_array (expr, s2))
10337 return NULL_TREE;
10339 /* If the first argument is "", return NULL_TREE. */
10340 const char *p1 = c_getstr (s1);
10341 if (p1 && *p1 == '\0')
10343 /* Evaluate and ignore argument s2 in case it has
10344 side-effects. */
10345 return omit_one_operand_loc (loc, size_type_node,
10346 size_zero_node, s2);
10349 /* If the second argument is "", return __builtin_strlen(s1). */
10350 const char *p2 = c_getstr (s2);
10351 if (p2 && *p2 == '\0')
10353 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
10355 /* If the replacement _DECL isn't initialized, don't do the
10356 transformation. */
10357 if (!fn)
10358 return NULL_TREE;
10360 return build_call_expr_loc (loc, fn, 1, s1);
10362 return NULL_TREE;
10365 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
10366 produced. False otherwise. This is done so that we don't output the error
10367 or warning twice or three times. */
10369 bool
10370 fold_builtin_next_arg (tree exp, bool va_start_p)
10372 tree fntype = TREE_TYPE (current_function_decl);
10373 int nargs = call_expr_nargs (exp);
10374 tree arg;
10375 /* There is good chance the current input_location points inside the
10376 definition of the va_start macro (perhaps on the token for
10377 builtin) in a system header, so warnings will not be emitted.
10378 Use the location in real source code. */
10379 location_t current_location =
10380 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
10381 NULL);
10383 if (!stdarg_p (fntype))
10385 error ("%<va_start%> used in function with fixed arguments");
10386 return true;
10389 if (va_start_p)
10391 if (va_start_p && (nargs != 2))
10393 error ("wrong number of arguments to function %<va_start%>");
10394 return true;
10396 arg = CALL_EXPR_ARG (exp, 1);
10398 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10399 when we checked the arguments and if needed issued a warning. */
10400 else
10402 if (nargs == 0)
10404 /* Evidently an out of date version of <stdarg.h>; can't validate
10405 va_start's second argument, but can still work as intended. */
10406 warning_at (current_location,
10407 OPT_Wvarargs,
10408 "%<__builtin_next_arg%> called without an argument");
10409 return true;
10411 else if (nargs > 1)
10413 error ("wrong number of arguments to function %<__builtin_next_arg%>");
10414 return true;
10416 arg = CALL_EXPR_ARG (exp, 0);
10419 if (TREE_CODE (arg) == SSA_NAME
10420 && SSA_NAME_VAR (arg))
10421 arg = SSA_NAME_VAR (arg);
10423 /* We destructively modify the call to be __builtin_va_start (ap, 0)
10424 or __builtin_next_arg (0) the first time we see it, after checking
10425 the arguments and if needed issuing a warning. */
10426 if (!integer_zerop (arg))
10428 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10430 /* Strip off all nops for the sake of the comparison. This
10431 is not quite the same as STRIP_NOPS. It does more.
10432 We must also strip off INDIRECT_EXPR for C++ reference
10433 parameters. */
10434 while (CONVERT_EXPR_P (arg)
10435 || TREE_CODE (arg) == INDIRECT_REF)
10436 arg = TREE_OPERAND (arg, 0);
10437 if (arg != last_parm)
10439 /* FIXME: Sometimes with the tree optimizers we can get the
10440 not the last argument even though the user used the last
10441 argument. We just warn and set the arg to be the last
10442 argument so that we will get wrong-code because of
10443 it. */
10444 warning_at (current_location,
10445 OPT_Wvarargs,
10446 "second parameter of %<va_start%> not last named argument");
10449 /* Undefined by C99 7.15.1.4p4 (va_start):
10450 "If the parameter parmN is declared with the register storage
10451 class, with a function or array type, or with a type that is
10452 not compatible with the type that results after application of
10453 the default argument promotions, the behavior is undefined."
10455 else if (DECL_REGISTER (arg))
10457 warning_at (current_location,
10458 OPT_Wvarargs,
10459 "undefined behavior when second parameter of "
10460 "%<va_start%> is declared with %<register%> storage");
10463 /* We want to verify the second parameter just once before the tree
10464 optimizers are run and then avoid keeping it in the tree,
10465 as otherwise we could warn even for correct code like:
10466 void foo (int i, ...)
10467 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10468 if (va_start_p)
10469 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
10470 else
10471 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
10473 return false;
10477 /* Expand a call EXP to __builtin_object_size. */
10479 static rtx
10480 expand_builtin_object_size (tree exp)
10482 tree ost;
10483 int object_size_type;
10484 tree fndecl = get_callee_fndecl (exp);
10486 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10488 error ("first argument of %qD must be a pointer, second integer constant",
10489 fndecl);
10490 expand_builtin_trap ();
10491 return const0_rtx;
10494 ost = CALL_EXPR_ARG (exp, 1);
10495 STRIP_NOPS (ost);
10497 if (TREE_CODE (ost) != INTEGER_CST
10498 || tree_int_cst_sgn (ost) < 0
10499 || compare_tree_int (ost, 3) > 0)
10501 error ("last argument of %qD is not integer constant between 0 and 3",
10502 fndecl);
10503 expand_builtin_trap ();
10504 return const0_rtx;
10507 object_size_type = tree_to_shwi (ost);
10509 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10512 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10513 FCODE is the BUILT_IN_* to use.
10514 Return NULL_RTX if we failed; the caller should emit a normal call,
10515 otherwise try to get the result in TARGET, if convenient (and in
10516 mode MODE if that's convenient). */
10518 static rtx
10519 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
10520 enum built_in_function fcode)
10522 if (!validate_arglist (exp,
10523 POINTER_TYPE,
10524 fcode == BUILT_IN_MEMSET_CHK
10525 ? INTEGER_TYPE : POINTER_TYPE,
10526 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10527 return NULL_RTX;
10529 tree dest = CALL_EXPR_ARG (exp, 0);
10530 tree src = CALL_EXPR_ARG (exp, 1);
10531 tree len = CALL_EXPR_ARG (exp, 2);
10532 tree size = CALL_EXPR_ARG (exp, 3);
10534 /* FIXME: Set access mode to write only for memset et al. */
10535 bool sizes_ok = check_access (exp, len, /*maxread=*/NULL_TREE,
10536 /*srcstr=*/NULL_TREE, size, access_read_write);
10538 if (!tree_fits_uhwi_p (size))
10539 return NULL_RTX;
10541 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
10543 /* Avoid transforming the checking call to an ordinary one when
10544 an overflow has been detected or when the call couldn't be
10545 validated because the size is not constant. */
10546 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
10547 return NULL_RTX;
10549 tree fn = NULL_TREE;
10550 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10551 mem{cpy,pcpy,move,set} is available. */
10552 switch (fcode)
10554 case BUILT_IN_MEMCPY_CHK:
10555 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
10556 break;
10557 case BUILT_IN_MEMPCPY_CHK:
10558 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
10559 break;
10560 case BUILT_IN_MEMMOVE_CHK:
10561 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
10562 break;
10563 case BUILT_IN_MEMSET_CHK:
10564 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
10565 break;
10566 default:
10567 break;
10570 if (! fn)
10571 return NULL_RTX;
10573 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
10574 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10575 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10576 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10578 else if (fcode == BUILT_IN_MEMSET_CHK)
10579 return NULL_RTX;
10580 else
10582 unsigned int dest_align = get_pointer_alignment (dest);
10584 /* If DEST is not a pointer type, call the normal function. */
10585 if (dest_align == 0)
10586 return NULL_RTX;
10588 /* If SRC and DEST are the same (and not volatile), do nothing. */
10589 if (operand_equal_p (src, dest, 0))
10591 tree expr;
10593 if (fcode != BUILT_IN_MEMPCPY_CHK)
10595 /* Evaluate and ignore LEN in case it has side-effects. */
10596 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10597 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10600 expr = fold_build_pointer_plus (dest, len);
10601 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10604 /* __memmove_chk special case. */
10605 if (fcode == BUILT_IN_MEMMOVE_CHK)
10607 unsigned int src_align = get_pointer_alignment (src);
10609 if (src_align == 0)
10610 return NULL_RTX;
10612 /* If src is categorized for a readonly section we can use
10613 normal __memcpy_chk. */
10614 if (readonly_data_expr (src))
10616 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
10617 if (!fn)
10618 return NULL_RTX;
10619 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
10620 dest, src, len, size);
10621 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10622 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10623 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10626 return NULL_RTX;
10630 /* Emit warning if a buffer overflow is detected at compile time. */
10632 static void
10633 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10635 /* The source string. */
10636 tree srcstr = NULL_TREE;
10637 /* The size of the destination object returned by __builtin_object_size. */
10638 tree objsize = NULL_TREE;
10639 /* The string that is being concatenated with (as in __strcat_chk)
10640 or null if it isn't. */
10641 tree catstr = NULL_TREE;
10642 /* The maximum length of the source sequence in a bounded operation
10643 (such as __strncat_chk) or null if the operation isn't bounded
10644 (such as __strcat_chk). */
10645 tree maxread = NULL_TREE;
10646 /* The exact size of the access (such as in __strncpy_chk). */
10647 tree size = NULL_TREE;
10648 /* The access by the function that's checked. Except for snprintf
10649 both writing and reading is checked. */
10650 access_mode mode = access_read_write;
10652 switch (fcode)
10654 case BUILT_IN_STRCPY_CHK:
10655 case BUILT_IN_STPCPY_CHK:
10656 srcstr = CALL_EXPR_ARG (exp, 1);
10657 objsize = CALL_EXPR_ARG (exp, 2);
10658 break;
10660 case BUILT_IN_STRCAT_CHK:
10661 /* For __strcat_chk the warning will be emitted only if overflowing
10662 by at least strlen (dest) + 1 bytes. */
10663 catstr = CALL_EXPR_ARG (exp, 0);
10664 srcstr = CALL_EXPR_ARG (exp, 1);
10665 objsize = CALL_EXPR_ARG (exp, 2);
10666 break;
10668 case BUILT_IN_STRNCAT_CHK:
10669 catstr = CALL_EXPR_ARG (exp, 0);
10670 srcstr = CALL_EXPR_ARG (exp, 1);
10671 maxread = CALL_EXPR_ARG (exp, 2);
10672 objsize = CALL_EXPR_ARG (exp, 3);
10673 break;
10675 case BUILT_IN_STRNCPY_CHK:
10676 case BUILT_IN_STPNCPY_CHK:
10677 srcstr = CALL_EXPR_ARG (exp, 1);
10678 size = CALL_EXPR_ARG (exp, 2);
10679 objsize = CALL_EXPR_ARG (exp, 3);
10680 break;
10682 case BUILT_IN_SNPRINTF_CHK:
10683 case BUILT_IN_VSNPRINTF_CHK:
10684 maxread = CALL_EXPR_ARG (exp, 1);
10685 objsize = CALL_EXPR_ARG (exp, 3);
10686 /* The only checked access the write to the destination. */
10687 mode = access_write_only;
10688 break;
10689 default:
10690 gcc_unreachable ();
10693 if (catstr && maxread)
10695 /* Check __strncat_chk. There is no way to determine the length
10696 of the string to which the source string is being appended so
10697 just warn when the length of the source string is not known. */
10698 check_strncat_sizes (exp, objsize);
10699 return;
10702 check_access (exp, size, maxread, srcstr, objsize, mode);
10705 /* Emit warning if a buffer overflow is detected at compile time
10706 in __sprintf_chk/__vsprintf_chk calls. */
10708 static void
10709 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10711 tree size, len, fmt;
10712 const char *fmt_str;
10713 int nargs = call_expr_nargs (exp);
10715 /* Verify the required arguments in the original call. */
10717 if (nargs < 4)
10718 return;
10719 size = CALL_EXPR_ARG (exp, 2);
10720 fmt = CALL_EXPR_ARG (exp, 3);
10722 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
10723 return;
10725 /* Check whether the format is a literal string constant. */
10726 fmt_str = c_getstr (fmt);
10727 if (fmt_str == NULL)
10728 return;
10730 if (!init_target_chars ())
10731 return;
10733 /* If the format doesn't contain % args or %%, we know its size. */
10734 if (strchr (fmt_str, target_percent) == 0)
10735 len = build_int_cstu (size_type_node, strlen (fmt_str));
10736 /* If the format is "%s" and first ... argument is a string literal,
10737 we know it too. */
10738 else if (fcode == BUILT_IN_SPRINTF_CHK
10739 && strcmp (fmt_str, target_percent_s) == 0)
10741 tree arg;
10743 if (nargs < 5)
10744 return;
10745 arg = CALL_EXPR_ARG (exp, 4);
10746 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10747 return;
10749 len = c_strlen (arg, 1);
10750 if (!len || ! tree_fits_uhwi_p (len))
10751 return;
10753 else
10754 return;
10756 /* Add one for the terminating nul. */
10757 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
10759 check_access (exp, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, len, size,
10760 access_write_only);
10763 /* Fold a call to __builtin_object_size with arguments PTR and OST,
10764 if possible. */
10766 static tree
10767 fold_builtin_object_size (tree ptr, tree ost, enum built_in_function fcode)
10769 tree bytes;
10770 int object_size_type;
10772 if (!validate_arg (ptr, POINTER_TYPE)
10773 || !validate_arg (ost, INTEGER_TYPE))
10774 return NULL_TREE;
10776 STRIP_NOPS (ost);
10778 if (TREE_CODE (ost) != INTEGER_CST
10779 || tree_int_cst_sgn (ost) < 0
10780 || compare_tree_int (ost, 3) > 0)
10781 return NULL_TREE;
10783 object_size_type = tree_to_shwi (ost);
10785 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10786 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10787 and (size_t) 0 for types 2 and 3. */
10788 if (TREE_SIDE_EFFECTS (ptr))
10789 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10791 if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
10792 object_size_type |= OST_DYNAMIC;
10794 if (TREE_CODE (ptr) == ADDR_EXPR)
10796 compute_builtin_object_size (ptr, object_size_type, &bytes);
10797 if ((object_size_type & OST_DYNAMIC)
10798 || int_fits_type_p (bytes, size_type_node))
10799 return fold_convert (size_type_node, bytes);
10801 else if (TREE_CODE (ptr) == SSA_NAME)
10803 /* If object size is not known yet, delay folding until
10804 later. Maybe subsequent passes will help determining
10805 it. */
10806 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
10807 && ((object_size_type & OST_DYNAMIC)
10808 || int_fits_type_p (bytes, size_type_node)))
10809 return fold_convert (size_type_node, bytes);
10812 return NULL_TREE;
10815 /* Builtins with folding operations that operate on "..." arguments
10816 need special handling; we need to store the arguments in a convenient
10817 data structure before attempting any folding. Fortunately there are
10818 only a few builtins that fall into this category. FNDECL is the
10819 function, EXP is the CALL_EXPR for the call. */
10821 static tree
10822 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
10824 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10825 tree ret = NULL_TREE;
10827 switch (fcode)
10829 case BUILT_IN_FPCLASSIFY:
10830 ret = fold_builtin_fpclassify (loc, args, nargs);
10831 break;
10833 default:
10834 break;
10836 if (ret)
10838 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10839 SET_EXPR_LOCATION (ret, loc);
10840 suppress_warning (ret);
10841 return ret;
10843 return NULL_TREE;
10846 /* Initialize format string characters in the target charset. */
10848 bool
10849 init_target_chars (void)
10851 static bool init;
10852 if (!init)
10854 target_newline = lang_hooks.to_target_charset ('\n');
10855 target_percent = lang_hooks.to_target_charset ('%');
10856 target_c = lang_hooks.to_target_charset ('c');
10857 target_s = lang_hooks.to_target_charset ('s');
10858 if (target_newline == 0 || target_percent == 0 || target_c == 0
10859 || target_s == 0)
10860 return false;
10862 target_percent_c[0] = target_percent;
10863 target_percent_c[1] = target_c;
10864 target_percent_c[2] = '\0';
10866 target_percent_s[0] = target_percent;
10867 target_percent_s[1] = target_s;
10868 target_percent_s[2] = '\0';
10870 target_percent_s_newline[0] = target_percent;
10871 target_percent_s_newline[1] = target_s;
10872 target_percent_s_newline[2] = target_newline;
10873 target_percent_s_newline[3] = '\0';
10875 init = true;
10877 return true;
10880 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
10881 and no overflow/underflow occurred. INEXACT is true if M was not
10882 exactly calculated. TYPE is the tree type for the result. This
10883 function assumes that you cleared the MPFR flags and then
10884 calculated M to see if anything subsequently set a flag prior to
10885 entering this function. Return NULL_TREE if any checks fail. */
10887 static tree
10888 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
10890 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10891 overflow/underflow occurred. If -frounding-math, proceed iff the
10892 result of calling FUNC was exact. */
10893 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
10894 && (!flag_rounding_math || !inexact))
10896 REAL_VALUE_TYPE rr;
10898 real_from_mpfr (&rr, m, type, MPFR_RNDN);
10899 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
10900 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10901 but the mpft_t is not, then we underflowed in the
10902 conversion. */
10903 if (real_isfinite (&rr)
10904 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
10906 REAL_VALUE_TYPE rmode;
10908 real_convert (&rmode, TYPE_MODE (type), &rr);
10909 /* Proceed iff the specified mode can hold the value. */
10910 if (real_identical (&rmode, &rr))
10911 return build_real (type, rmode);
10914 return NULL_TREE;
10917 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
10918 number and no overflow/underflow occurred. INEXACT is true if M
10919 was not exactly calculated. TYPE is the tree type for the result.
10920 This function assumes that you cleared the MPFR flags and then
10921 calculated M to see if anything subsequently set a flag prior to
10922 entering this function. Return NULL_TREE if any checks fail, if
10923 FORCE_CONVERT is true, then bypass the checks. */
10925 static tree
10926 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
10928 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10929 overflow/underflow occurred. If -frounding-math, proceed iff the
10930 result of calling FUNC was exact. */
10931 if (force_convert
10932 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
10933 && !mpfr_overflow_p () && !mpfr_underflow_p ()
10934 && (!flag_rounding_math || !inexact)))
10936 REAL_VALUE_TYPE re, im;
10938 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), MPFR_RNDN);
10939 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), MPFR_RNDN);
10940 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
10941 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10942 but the mpft_t is not, then we underflowed in the
10943 conversion. */
10944 if (force_convert
10945 || (real_isfinite (&re) && real_isfinite (&im)
10946 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
10947 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
10949 REAL_VALUE_TYPE re_mode, im_mode;
10951 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
10952 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
10953 /* Proceed iff the specified mode can hold the value. */
10954 if (force_convert
10955 || (real_identical (&re_mode, &re)
10956 && real_identical (&im_mode, &im)))
10957 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
10958 build_real (TREE_TYPE (type), im_mode));
10961 return NULL_TREE;
10964 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
10965 the pointer *(ARG_QUO) and return the result. The type is taken
10966 from the type of ARG0 and is used for setting the precision of the
10967 calculation and results. */
10969 static tree
10970 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
10972 tree const type = TREE_TYPE (arg0);
10973 tree result = NULL_TREE;
10975 STRIP_NOPS (arg0);
10976 STRIP_NOPS (arg1);
10978 /* To proceed, MPFR must exactly represent the target floating point
10979 format, which only happens when the target base equals two. */
10980 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10981 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
10982 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
10984 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
10985 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
10987 if (real_isfinite (ra0) && real_isfinite (ra1))
10989 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10990 const int prec = fmt->p;
10991 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
10992 tree result_rem;
10993 long integer_quo;
10994 mpfr_t m0, m1;
10996 mpfr_inits2 (prec, m0, m1, NULL);
10997 mpfr_from_real (m0, ra0, MPFR_RNDN);
10998 mpfr_from_real (m1, ra1, MPFR_RNDN);
10999 mpfr_clear_flags ();
11000 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
11001 /* Remquo is independent of the rounding mode, so pass
11002 inexact=0 to do_mpfr_ckconv(). */
11003 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
11004 mpfr_clears (m0, m1, NULL);
11005 if (result_rem)
11007 /* MPFR calculates quo in the host's long so it may
11008 return more bits in quo than the target int can hold
11009 if sizeof(host long) > sizeof(target int). This can
11010 happen even for native compilers in LP64 mode. In
11011 these cases, modulo the quo value with the largest
11012 number that the target int can hold while leaving one
11013 bit for the sign. */
11014 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
11015 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
11017 /* Dereference the quo pointer argument. */
11018 arg_quo = build_fold_indirect_ref (arg_quo);
11019 /* Proceed iff a valid pointer type was passed in. */
11020 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
11022 /* Set the value. */
11023 tree result_quo
11024 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
11025 build_int_cst (TREE_TYPE (arg_quo),
11026 integer_quo));
11027 TREE_SIDE_EFFECTS (result_quo) = 1;
11028 /* Combine the quo assignment with the rem. */
11029 result = fold_build2 (COMPOUND_EXPR, type,
11030 result_quo, result_rem);
11031 suppress_warning (result, OPT_Wunused_value);
11032 result = non_lvalue (result);
11037 return result;
11040 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
11041 resulting value as a tree with type TYPE. The mpfr precision is
11042 set to the precision of TYPE. We assume that this mpfr function
11043 returns zero if the result could be calculated exactly within the
11044 requested precision. In addition, the integer pointer represented
11045 by ARG_SG will be dereferenced and set to the appropriate signgam
11046 (-1,1) value. */
11048 static tree
11049 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
11051 tree result = NULL_TREE;
11053 STRIP_NOPS (arg);
11055 /* To proceed, MPFR must exactly represent the target floating point
11056 format, which only happens when the target base equals two. Also
11057 verify ARG is a constant and that ARG_SG is an int pointer. */
11058 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11059 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
11060 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
11061 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
11063 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
11065 /* In addition to NaN and Inf, the argument cannot be zero or a
11066 negative integer. */
11067 if (real_isfinite (ra)
11068 && ra->cl != rvc_zero
11069 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
11071 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11072 const int prec = fmt->p;
11073 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
11074 int inexact, sg;
11075 mpfr_t m;
11076 tree result_lg;
11078 mpfr_init2 (m, prec);
11079 mpfr_from_real (m, ra, MPFR_RNDN);
11080 mpfr_clear_flags ();
11081 inexact = mpfr_lgamma (m, &sg, m, rnd);
11082 result_lg = do_mpfr_ckconv (m, type, inexact);
11083 mpfr_clear (m);
11084 if (result_lg)
11086 tree result_sg;
11088 /* Dereference the arg_sg pointer argument. */
11089 arg_sg = build_fold_indirect_ref (arg_sg);
11090 /* Assign the signgam value into *arg_sg. */
11091 result_sg = fold_build2 (MODIFY_EXPR,
11092 TREE_TYPE (arg_sg), arg_sg,
11093 build_int_cst (TREE_TYPE (arg_sg), sg));
11094 TREE_SIDE_EFFECTS (result_sg) = 1;
11095 /* Combine the signgam assignment with the lgamma result. */
11096 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11097 result_sg, result_lg));
11102 return result;
11105 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
11106 mpc function FUNC on it and return the resulting value as a tree
11107 with type TYPE. The mpfr precision is set to the precision of
11108 TYPE. We assume that function FUNC returns zero if the result
11109 could be calculated exactly within the requested precision. If
11110 DO_NONFINITE is true, then fold expressions containing Inf or NaN
11111 in the arguments and/or results. */
11113 tree
11114 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
11115 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
11117 tree result = NULL_TREE;
11119 STRIP_NOPS (arg0);
11120 STRIP_NOPS (arg1);
11122 /* To proceed, MPFR must exactly represent the target floating point
11123 format, which only happens when the target base equals two. */
11124 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
11125 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
11126 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
11127 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
11128 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
11130 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
11131 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
11132 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
11133 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
11135 if (do_nonfinite
11136 || (real_isfinite (re0) && real_isfinite (im0)
11137 && real_isfinite (re1) && real_isfinite (im1)))
11139 const struct real_format *const fmt =
11140 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
11141 const int prec = fmt->p;
11142 const mpfr_rnd_t rnd = fmt->round_towards_zero
11143 ? MPFR_RNDZ : MPFR_RNDN;
11144 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
11145 int inexact;
11146 mpc_t m0, m1;
11148 mpc_init2 (m0, prec);
11149 mpc_init2 (m1, prec);
11150 mpfr_from_real (mpc_realref (m0), re0, rnd);
11151 mpfr_from_real (mpc_imagref (m0), im0, rnd);
11152 mpfr_from_real (mpc_realref (m1), re1, rnd);
11153 mpfr_from_real (mpc_imagref (m1), im1, rnd);
11154 mpfr_clear_flags ();
11155 inexact = func (m0, m0, m1, crnd);
11156 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
11157 mpc_clear (m0);
11158 mpc_clear (m1);
11162 return result;
11165 /* A wrapper function for builtin folding that prevents warnings for
11166 "statement without effect" and the like, caused by removing the
11167 call node earlier than the warning is generated. */
11169 tree
11170 fold_call_stmt (gcall *stmt, bool ignore)
11172 tree ret = NULL_TREE;
11173 tree fndecl = gimple_call_fndecl (stmt);
11174 location_t loc = gimple_location (stmt);
11175 if (fndecl && fndecl_built_in_p (fndecl)
11176 && !gimple_call_va_arg_pack_p (stmt))
11178 int nargs = gimple_call_num_args (stmt);
11179 tree *args = (nargs > 0
11180 ? gimple_call_arg_ptr (stmt, 0)
11181 : &error_mark_node);
11183 if (avoid_folding_inline_builtin (fndecl))
11184 return NULL_TREE;
11185 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11187 return targetm.fold_builtin (fndecl, nargs, args, ignore);
11189 else
11191 ret = fold_builtin_n (loc, NULL_TREE, fndecl, args, nargs, ignore);
11192 if (ret)
11194 /* Propagate location information from original call to
11195 expansion of builtin. Otherwise things like
11196 maybe_emit_chk_warning, that operate on the expansion
11197 of a builtin, will use the wrong location information. */
11198 if (gimple_has_location (stmt))
11200 tree realret = ret;
11201 if (TREE_CODE (ret) == NOP_EXPR)
11202 realret = TREE_OPERAND (ret, 0);
11203 if (CAN_HAVE_LOCATION_P (realret)
11204 && !EXPR_HAS_LOCATION (realret))
11205 SET_EXPR_LOCATION (realret, loc);
11206 return realret;
11208 return ret;
11212 return NULL_TREE;
11215 /* Look up the function in builtin_decl that corresponds to DECL
11216 and set ASMSPEC as its user assembler name. DECL must be a
11217 function decl that declares a builtin. */
11219 void
11220 set_builtin_user_assembler_name (tree decl, const char *asmspec)
11222 gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL)
11223 && asmspec != 0);
11225 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
11226 set_user_assembler_name (builtin, asmspec);
11228 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
11229 && INT_TYPE_SIZE < BITS_PER_WORD)
11231 scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
11232 set_user_assembler_libfunc ("ffs", asmspec);
11233 set_optab_libfunc (ffs_optab, mode, "ffs");
11237 /* Return true if DECL is a builtin that expands to a constant or similarly
11238 simple code. */
11239 bool
11240 is_simple_builtin (tree decl)
11242 if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
11243 switch (DECL_FUNCTION_CODE (decl))
11245 /* Builtins that expand to constants. */
11246 case BUILT_IN_CONSTANT_P:
11247 case BUILT_IN_EXPECT:
11248 case BUILT_IN_OBJECT_SIZE:
11249 case BUILT_IN_UNREACHABLE:
11250 /* Simple register moves or loads from stack. */
11251 case BUILT_IN_ASSUME_ALIGNED:
11252 case BUILT_IN_RETURN_ADDRESS:
11253 case BUILT_IN_EXTRACT_RETURN_ADDR:
11254 case BUILT_IN_FROB_RETURN_ADDR:
11255 case BUILT_IN_RETURN:
11256 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
11257 case BUILT_IN_FRAME_ADDRESS:
11258 case BUILT_IN_VA_END:
11259 case BUILT_IN_STACK_SAVE:
11260 case BUILT_IN_STACK_RESTORE:
11261 case BUILT_IN_DWARF_CFA:
11262 /* Exception state returns or moves registers around. */
11263 case BUILT_IN_EH_FILTER:
11264 case BUILT_IN_EH_POINTER:
11265 case BUILT_IN_EH_COPY_VALUES:
11266 return true;
11268 default:
11269 return false;
11272 return false;
11275 /* Return true if DECL is a builtin that is not expensive, i.e., they are
11276 most probably expanded inline into reasonably simple code. This is a
11277 superset of is_simple_builtin. */
11278 bool
11279 is_inexpensive_builtin (tree decl)
11281 if (!decl)
11282 return false;
11283 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
11284 return true;
11285 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
11286 switch (DECL_FUNCTION_CODE (decl))
11288 case BUILT_IN_ABS:
11289 CASE_BUILT_IN_ALLOCA:
11290 case BUILT_IN_BSWAP16:
11291 case BUILT_IN_BSWAP32:
11292 case BUILT_IN_BSWAP64:
11293 case BUILT_IN_BSWAP128:
11294 case BUILT_IN_CLZ:
11295 case BUILT_IN_CLZIMAX:
11296 case BUILT_IN_CLZL:
11297 case BUILT_IN_CLZLL:
11298 case BUILT_IN_CTZ:
11299 case BUILT_IN_CTZIMAX:
11300 case BUILT_IN_CTZL:
11301 case BUILT_IN_CTZLL:
11302 case BUILT_IN_FFS:
11303 case BUILT_IN_FFSIMAX:
11304 case BUILT_IN_FFSL:
11305 case BUILT_IN_FFSLL:
11306 case BUILT_IN_IMAXABS:
11307 case BUILT_IN_FINITE:
11308 case BUILT_IN_FINITEF:
11309 case BUILT_IN_FINITEL:
11310 case BUILT_IN_FINITED32:
11311 case BUILT_IN_FINITED64:
11312 case BUILT_IN_FINITED128:
11313 case BUILT_IN_FPCLASSIFY:
11314 case BUILT_IN_ISFINITE:
11315 case BUILT_IN_ISINF_SIGN:
11316 case BUILT_IN_ISINF:
11317 case BUILT_IN_ISINFF:
11318 case BUILT_IN_ISINFL:
11319 case BUILT_IN_ISINFD32:
11320 case BUILT_IN_ISINFD64:
11321 case BUILT_IN_ISINFD128:
11322 case BUILT_IN_ISNAN:
11323 case BUILT_IN_ISNANF:
11324 case BUILT_IN_ISNANL:
11325 case BUILT_IN_ISNAND32:
11326 case BUILT_IN_ISNAND64:
11327 case BUILT_IN_ISNAND128:
11328 case BUILT_IN_ISNORMAL:
11329 case BUILT_IN_ISGREATER:
11330 case BUILT_IN_ISGREATEREQUAL:
11331 case BUILT_IN_ISLESS:
11332 case BUILT_IN_ISLESSEQUAL:
11333 case BUILT_IN_ISLESSGREATER:
11334 case BUILT_IN_ISUNORDERED:
11335 case BUILT_IN_VA_ARG_PACK:
11336 case BUILT_IN_VA_ARG_PACK_LEN:
11337 case BUILT_IN_VA_COPY:
11338 case BUILT_IN_TRAP:
11339 case BUILT_IN_UNREACHABLE_TRAP:
11340 case BUILT_IN_SAVEREGS:
11341 case BUILT_IN_POPCOUNTL:
11342 case BUILT_IN_POPCOUNTLL:
11343 case BUILT_IN_POPCOUNTIMAX:
11344 case BUILT_IN_POPCOUNT:
11345 case BUILT_IN_PARITYL:
11346 case BUILT_IN_PARITYLL:
11347 case BUILT_IN_PARITYIMAX:
11348 case BUILT_IN_PARITY:
11349 case BUILT_IN_LABS:
11350 case BUILT_IN_LLABS:
11351 case BUILT_IN_PREFETCH:
11352 case BUILT_IN_ACC_ON_DEVICE:
11353 return true;
11355 default:
11356 return is_simple_builtin (decl);
11359 return false;
11362 /* Return true if T is a constant and the value cast to a target char
11363 can be represented by a host char.
11364 Store the casted char constant in *P if so. */
11366 bool
11367 target_char_cst_p (tree t, char *p)
11369 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
11370 return false;
11372 *p = (char)tree_to_uhwi (t);
11373 return true;
11376 /* Return true if the builtin DECL is implemented in a standard library.
11377 Otherwise return false which doesn't guarantee it is not (thus the list
11378 of handled builtins below may be incomplete). */
11380 bool
11381 builtin_with_linkage_p (tree decl)
11383 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
11384 switch (DECL_FUNCTION_CODE (decl))
11386 CASE_FLT_FN (BUILT_IN_ACOS):
11387 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
11388 CASE_FLT_FN (BUILT_IN_ACOSH):
11389 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH):
11390 CASE_FLT_FN (BUILT_IN_ASIN):
11391 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
11392 CASE_FLT_FN (BUILT_IN_ASINH):
11393 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINH):
11394 CASE_FLT_FN (BUILT_IN_ATAN):
11395 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN):
11396 CASE_FLT_FN (BUILT_IN_ATANH):
11397 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH):
11398 CASE_FLT_FN (BUILT_IN_ATAN2):
11399 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN2):
11400 CASE_FLT_FN (BUILT_IN_CBRT):
11401 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CBRT):
11402 CASE_FLT_FN (BUILT_IN_CEIL):
11403 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
11404 CASE_FLT_FN (BUILT_IN_COPYSIGN):
11405 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
11406 CASE_FLT_FN (BUILT_IN_COS):
11407 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COS):
11408 CASE_FLT_FN (BUILT_IN_COSH):
11409 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSH):
11410 CASE_FLT_FN (BUILT_IN_ERF):
11411 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERF):
11412 CASE_FLT_FN (BUILT_IN_ERFC):
11413 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERFC):
11414 CASE_FLT_FN (BUILT_IN_EXP):
11415 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP):
11416 CASE_FLT_FN (BUILT_IN_EXP2):
11417 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP2):
11418 CASE_FLT_FN (BUILT_IN_EXPM1):
11419 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXPM1):
11420 CASE_FLT_FN (BUILT_IN_FABS):
11421 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
11422 CASE_FLT_FN (BUILT_IN_FDIM):
11423 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FDIM):
11424 CASE_FLT_FN (BUILT_IN_FLOOR):
11425 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
11426 CASE_FLT_FN (BUILT_IN_FMA):
11427 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
11428 CASE_FLT_FN (BUILT_IN_FMAX):
11429 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
11430 CASE_FLT_FN (BUILT_IN_FMIN):
11431 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
11432 CASE_FLT_FN (BUILT_IN_FMOD):
11433 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMOD):
11434 CASE_FLT_FN (BUILT_IN_FREXP):
11435 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FREXP):
11436 CASE_FLT_FN (BUILT_IN_HYPOT):
11437 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HYPOT):
11438 CASE_FLT_FN (BUILT_IN_ILOGB):
11439 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ILOGB):
11440 CASE_FLT_FN (BUILT_IN_LDEXP):
11441 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LDEXP):
11442 CASE_FLT_FN (BUILT_IN_LGAMMA):
11443 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LGAMMA):
11444 CASE_FLT_FN (BUILT_IN_LLRINT):
11445 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLRINT):
11446 CASE_FLT_FN (BUILT_IN_LLROUND):
11447 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLROUND):
11448 CASE_FLT_FN (BUILT_IN_LOG):
11449 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG):
11450 CASE_FLT_FN (BUILT_IN_LOG10):
11451 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10):
11452 CASE_FLT_FN (BUILT_IN_LOG1P):
11453 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P):
11454 CASE_FLT_FN (BUILT_IN_LOG2):
11455 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2):
11456 CASE_FLT_FN (BUILT_IN_LOGB):
11457 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOGB):
11458 CASE_FLT_FN (BUILT_IN_LRINT):
11459 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LRINT):
11460 CASE_FLT_FN (BUILT_IN_LROUND):
11461 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LROUND):
11462 CASE_FLT_FN (BUILT_IN_MODF):
11463 CASE_FLT_FN_FLOATN_NX (BUILT_IN_MODF):
11464 CASE_FLT_FN (BUILT_IN_NAN):
11465 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NAN):
11466 CASE_FLT_FN (BUILT_IN_NEARBYINT):
11467 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
11468 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
11469 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEXTAFTER):
11470 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
11471 CASE_FLT_FN (BUILT_IN_POW):
11472 CASE_FLT_FN_FLOATN_NX (BUILT_IN_POW):
11473 CASE_FLT_FN (BUILT_IN_REMAINDER):
11474 CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMAINDER):
11475 CASE_FLT_FN (BUILT_IN_REMQUO):
11476 CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMQUO):
11477 CASE_FLT_FN (BUILT_IN_RINT):
11478 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
11479 CASE_FLT_FN (BUILT_IN_ROUND):
11480 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
11481 CASE_FLT_FN (BUILT_IN_SCALBLN):
11482 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBLN):
11483 CASE_FLT_FN (BUILT_IN_SCALBN):
11484 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBN):
11485 CASE_FLT_FN (BUILT_IN_SIN):
11486 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SIN):
11487 CASE_FLT_FN (BUILT_IN_SINH):
11488 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINH):
11489 CASE_FLT_FN (BUILT_IN_SINCOS):
11490 CASE_FLT_FN (BUILT_IN_SQRT):
11491 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
11492 CASE_FLT_FN (BUILT_IN_TAN):
11493 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TAN):
11494 CASE_FLT_FN (BUILT_IN_TANH):
11495 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TANH):
11496 CASE_FLT_FN (BUILT_IN_TGAMMA):
11497 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TGAMMA):
11498 CASE_FLT_FN (BUILT_IN_TRUNC):
11499 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
11500 return true;
11502 case BUILT_IN_STPCPY:
11503 case BUILT_IN_STPNCPY:
11504 /* stpcpy is both referenced in libiberty's pex-win32.c and provided
11505 by libiberty's stpcpy.c for MinGW targets so we need to return true
11506 in order to be able to build libiberty in LTO mode for them. */
11507 return true;
11509 default:
11510 break;
11512 return false;
11515 /* Return true if OFFRNG is bounded to a subrange of offset values
11516 valid for the largest possible object. */
11518 bool
11519 access_ref::offset_bounded () const
11521 tree min = TYPE_MIN_VALUE (ptrdiff_type_node);
11522 tree max = TYPE_MAX_VALUE (ptrdiff_type_node);
11523 return wi::to_offset (min) <= offrng[0] && offrng[1] <= wi::to_offset (max);
11526 /* If CALLEE has known side effects, fill in INFO and return true.
11527 See tree-ssa-structalias.cc:find_func_aliases
11528 for the list of builtins we might need to handle here. */
11530 attr_fnspec
11531 builtin_fnspec (tree callee)
11533 built_in_function code = DECL_FUNCTION_CODE (callee);
11535 switch (code)
11537 /* All the following functions read memory pointed to by
11538 their second argument and write memory pointed to by first
11539 argument.
11540 strcat/strncat additionally reads memory pointed to by the first
11541 argument. */
11542 case BUILT_IN_STRCAT:
11543 case BUILT_IN_STRCAT_CHK:
11544 return "1cW 1 ";
11545 case BUILT_IN_STRNCAT:
11546 case BUILT_IN_STRNCAT_CHK:
11547 return "1cW 13";
11548 case BUILT_IN_STRCPY:
11549 case BUILT_IN_STRCPY_CHK:
11550 return "1cO 1 ";
11551 case BUILT_IN_STPCPY:
11552 case BUILT_IN_STPCPY_CHK:
11553 return ".cO 1 ";
11554 case BUILT_IN_STRNCPY:
11555 case BUILT_IN_MEMCPY:
11556 case BUILT_IN_MEMMOVE:
11557 case BUILT_IN_TM_MEMCPY:
11558 case BUILT_IN_TM_MEMMOVE:
11559 case BUILT_IN_STRNCPY_CHK:
11560 case BUILT_IN_MEMCPY_CHK:
11561 case BUILT_IN_MEMMOVE_CHK:
11562 return "1cO313";
11563 case BUILT_IN_MEMPCPY:
11564 case BUILT_IN_MEMPCPY_CHK:
11565 return ".cO313";
11566 case BUILT_IN_STPNCPY:
11567 case BUILT_IN_STPNCPY_CHK:
11568 return ".cO313";
11569 case BUILT_IN_BCOPY:
11570 return ".c23O3";
11571 case BUILT_IN_BZERO:
11572 return ".cO2";
11573 case BUILT_IN_MEMCMP:
11574 case BUILT_IN_MEMCMP_EQ:
11575 case BUILT_IN_BCMP:
11576 case BUILT_IN_STRNCMP:
11577 case BUILT_IN_STRNCMP_EQ:
11578 case BUILT_IN_STRNCASECMP:
11579 return ".cR3R3";
11581 /* The following functions read memory pointed to by their
11582 first argument. */
11583 CASE_BUILT_IN_TM_LOAD (1):
11584 CASE_BUILT_IN_TM_LOAD (2):
11585 CASE_BUILT_IN_TM_LOAD (4):
11586 CASE_BUILT_IN_TM_LOAD (8):
11587 CASE_BUILT_IN_TM_LOAD (FLOAT):
11588 CASE_BUILT_IN_TM_LOAD (DOUBLE):
11589 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
11590 CASE_BUILT_IN_TM_LOAD (M64):
11591 CASE_BUILT_IN_TM_LOAD (M128):
11592 CASE_BUILT_IN_TM_LOAD (M256):
11593 case BUILT_IN_TM_LOG:
11594 case BUILT_IN_TM_LOG_1:
11595 case BUILT_IN_TM_LOG_2:
11596 case BUILT_IN_TM_LOG_4:
11597 case BUILT_IN_TM_LOG_8:
11598 case BUILT_IN_TM_LOG_FLOAT:
11599 case BUILT_IN_TM_LOG_DOUBLE:
11600 case BUILT_IN_TM_LOG_LDOUBLE:
11601 case BUILT_IN_TM_LOG_M64:
11602 case BUILT_IN_TM_LOG_M128:
11603 case BUILT_IN_TM_LOG_M256:
11604 return ".cR ";
11606 case BUILT_IN_INDEX:
11607 case BUILT_IN_RINDEX:
11608 case BUILT_IN_STRCHR:
11609 case BUILT_IN_STRLEN:
11610 case BUILT_IN_STRRCHR:
11611 return ".cR ";
11612 case BUILT_IN_STRNLEN:
11613 return ".cR2";
11615 /* These read memory pointed to by the first argument.
11616 Allocating memory does not have any side-effects apart from
11617 being the definition point for the pointer.
11618 Unix98 specifies that errno is set on allocation failure. */
11619 case BUILT_IN_STRDUP:
11620 return "mCR ";
11621 case BUILT_IN_STRNDUP:
11622 return "mCR2";
11623 /* Allocating memory does not have any side-effects apart from
11624 being the definition point for the pointer. */
11625 case BUILT_IN_MALLOC:
11626 case BUILT_IN_ALIGNED_ALLOC:
11627 case BUILT_IN_CALLOC:
11628 case BUILT_IN_GOMP_ALLOC:
11629 return "mC";
11630 CASE_BUILT_IN_ALLOCA:
11631 return "mc";
11632 /* These read memory pointed to by the first argument with size
11633 in the third argument. */
11634 case BUILT_IN_MEMCHR:
11635 return ".cR3";
11636 /* These read memory pointed to by the first and second arguments. */
11637 case BUILT_IN_STRSTR:
11638 case BUILT_IN_STRPBRK:
11639 case BUILT_IN_STRCASECMP:
11640 case BUILT_IN_STRCSPN:
11641 case BUILT_IN_STRSPN:
11642 case BUILT_IN_STRCMP:
11643 case BUILT_IN_STRCMP_EQ:
11644 return ".cR R ";
11645 /* Freeing memory kills the pointed-to memory. More importantly
11646 the call has to serve as a barrier for moving loads and stores
11647 across it. */
11648 case BUILT_IN_STACK_RESTORE:
11649 case BUILT_IN_FREE:
11650 case BUILT_IN_GOMP_FREE:
11651 return ".co ";
11652 case BUILT_IN_VA_END:
11653 return ".cO ";
11654 /* Realloc serves both as allocation point and deallocation point. */
11655 case BUILT_IN_REALLOC:
11656 return ".Cw ";
11657 case BUILT_IN_GAMMA_R:
11658 case BUILT_IN_GAMMAF_R:
11659 case BUILT_IN_GAMMAL_R:
11660 case BUILT_IN_LGAMMA_R:
11661 case BUILT_IN_LGAMMAF_R:
11662 case BUILT_IN_LGAMMAL_R:
11663 return ".C. Ot";
11664 case BUILT_IN_FREXP:
11665 case BUILT_IN_FREXPF:
11666 case BUILT_IN_FREXPL:
11667 case BUILT_IN_MODF:
11668 case BUILT_IN_MODFF:
11669 case BUILT_IN_MODFL:
11670 return ".c. Ot";
11671 case BUILT_IN_REMQUO:
11672 case BUILT_IN_REMQUOF:
11673 case BUILT_IN_REMQUOL:
11674 return ".c. . Ot";
11675 case BUILT_IN_SINCOS:
11676 case BUILT_IN_SINCOSF:
11677 case BUILT_IN_SINCOSL:
11678 return ".c. OtOt";
11679 case BUILT_IN_MEMSET:
11680 case BUILT_IN_MEMSET_CHK:
11681 case BUILT_IN_TM_MEMSET:
11682 return "1cO3";
11683 CASE_BUILT_IN_TM_STORE (1):
11684 CASE_BUILT_IN_TM_STORE (2):
11685 CASE_BUILT_IN_TM_STORE (4):
11686 CASE_BUILT_IN_TM_STORE (8):
11687 CASE_BUILT_IN_TM_STORE (FLOAT):
11688 CASE_BUILT_IN_TM_STORE (DOUBLE):
11689 CASE_BUILT_IN_TM_STORE (LDOUBLE):
11690 CASE_BUILT_IN_TM_STORE (M64):
11691 CASE_BUILT_IN_TM_STORE (M128):
11692 CASE_BUILT_IN_TM_STORE (M256):
11693 return ".cO ";
11694 case BUILT_IN_STACK_SAVE:
11695 case BUILT_IN_RETURN:
11696 case BUILT_IN_EH_POINTER:
11697 case BUILT_IN_EH_FILTER:
11698 case BUILT_IN_UNWIND_RESUME:
11699 case BUILT_IN_CXA_END_CLEANUP:
11700 case BUILT_IN_EH_COPY_VALUES:
11701 case BUILT_IN_FRAME_ADDRESS:
11702 case BUILT_IN_APPLY_ARGS:
11703 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
11704 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
11705 case BUILT_IN_PREFETCH:
11706 case BUILT_IN_DWARF_CFA:
11707 case BUILT_IN_RETURN_ADDRESS:
11708 return ".c";
11709 case BUILT_IN_ASSUME_ALIGNED:
11710 return "1cX ";
11711 /* But posix_memalign stores a pointer into the memory pointed to
11712 by its first argument. */
11713 case BUILT_IN_POSIX_MEMALIGN:
11714 return ".cOt";
11716 default:
11717 return "";