Small fix for -fdump-ada-spec
[official-gcc.git] / gcc / builtins.cc
blobaf45829875e6cc16569222758aa574893ec4e361
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 take a built_in_function and
2093 always use the implicit array. */
2095 tree
2096 mathfn_built_in (tree type, enum built_in_function fn)
2098 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2101 /* Return the type associated with a built in function, i.e., the one
2102 to be passed to mathfn_built_in to get the type-specific
2103 function. */
2105 tree
2106 mathfn_built_in_type (combined_fn fn)
2108 #define CASE_MATHFN(MATHFN) \
2109 case CFN_BUILT_IN_##MATHFN: \
2110 return double_type_node; \
2111 case CFN_BUILT_IN_##MATHFN##F: \
2112 return float_type_node; \
2113 case CFN_BUILT_IN_##MATHFN##L: \
2114 return long_double_type_node;
2116 #define CASE_MATHFN_FLOATN(MATHFN) \
2117 CASE_MATHFN(MATHFN) \
2118 case CFN_BUILT_IN_##MATHFN##F16: \
2119 return float16_type_node; \
2120 case CFN_BUILT_IN_##MATHFN##F32: \
2121 return float32_type_node; \
2122 case CFN_BUILT_IN_##MATHFN##F64: \
2123 return float64_type_node; \
2124 case CFN_BUILT_IN_##MATHFN##F128: \
2125 return float128_type_node; \
2126 case CFN_BUILT_IN_##MATHFN##F32X: \
2127 return float32x_type_node; \
2128 case CFN_BUILT_IN_##MATHFN##F64X: \
2129 return float64x_type_node; \
2130 case CFN_BUILT_IN_##MATHFN##F128X: \
2131 return float128x_type_node;
2133 /* Similar to above, but appends _R after any F/L suffix. */
2134 #define CASE_MATHFN_REENT(MATHFN) \
2135 case CFN_BUILT_IN_##MATHFN##_R: \
2136 return double_type_node; \
2137 case CFN_BUILT_IN_##MATHFN##F_R: \
2138 return float_type_node; \
2139 case CFN_BUILT_IN_##MATHFN##L_R: \
2140 return long_double_type_node;
2142 switch (fn)
2144 SEQ_OF_CASE_MATHFN
2146 default:
2147 return NULL_TREE;
2150 #undef CASE_MATHFN
2151 #undef CASE_MATHFN_FLOATN
2152 #undef CASE_MATHFN_REENT
2153 #undef SEQ_OF_CASE_MATHFN
2156 /* Check whether there is an internal function associated with function FN
2157 and return type RETURN_TYPE. Return the function if so, otherwise return
2158 IFN_LAST.
2160 Note that this function only tests whether the function is defined in
2161 internals.def, not whether it is actually available on the target. */
2163 static internal_fn
2164 associated_internal_fn (built_in_function fn, tree return_type)
2166 switch (fn)
2168 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2169 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2170 #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2171 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2172 CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2173 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2174 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2175 #include "internal-fn.def"
2177 CASE_FLT_FN (BUILT_IN_POW10):
2178 return IFN_EXP10;
2180 CASE_FLT_FN (BUILT_IN_DREM):
2181 return IFN_REMAINDER;
2183 CASE_FLT_FN (BUILT_IN_SCALBN):
2184 CASE_FLT_FN (BUILT_IN_SCALBLN):
2185 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2186 return IFN_LDEXP;
2187 return IFN_LAST;
2189 default:
2190 return IFN_LAST;
2194 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2195 return its code, otherwise return IFN_LAST. Note that this function
2196 only tests whether the function is defined in internals.def, not whether
2197 it is actually available on the target. */
2199 internal_fn
2200 associated_internal_fn (tree fndecl)
2202 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2203 return associated_internal_fn (DECL_FUNCTION_CODE (fndecl),
2204 TREE_TYPE (TREE_TYPE (fndecl)));
2207 /* Check whether there is an internal function associated with function CFN
2208 and return type RETURN_TYPE. Return the function if so, otherwise return
2209 IFN_LAST.
2211 Note that this function only tests whether the function is defined in
2212 internals.def, not whether it is actually available on the target. */
2214 internal_fn
2215 associated_internal_fn (combined_fn cfn, tree return_type)
2217 if (internal_fn_p (cfn))
2218 return as_internal_fn (cfn);
2219 return associated_internal_fn (as_builtin_fn (cfn), return_type);
2222 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2223 on the current target by a call to an internal function, return the
2224 code of that internal function, otherwise return IFN_LAST. The caller
2225 is responsible for ensuring that any side-effects of the built-in
2226 call are dealt with correctly. E.g. if CALL sets errno, the caller
2227 must decide that the errno result isn't needed or make it available
2228 in some other way. */
2230 internal_fn
2231 replacement_internal_fn (gcall *call)
2233 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2235 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2236 if (ifn != IFN_LAST)
2238 tree_pair types = direct_internal_fn_types (ifn, call);
2239 optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2240 if (direct_internal_fn_supported_p (ifn, types, opt_type))
2241 return ifn;
2244 return IFN_LAST;
2247 /* Expand a call to the builtin trinary math functions (fma).
2248 Return NULL_RTX if a normal call should be emitted rather than expanding the
2249 function in-line. EXP is the expression that is a call to the builtin
2250 function; if convenient, the result should be placed in TARGET.
2251 SUBTARGET may be used as the target for computing one of EXP's
2252 operands. */
2254 static rtx
2255 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2257 optab builtin_optab;
2258 rtx op0, op1, op2, result;
2259 rtx_insn *insns;
2260 tree fndecl = get_callee_fndecl (exp);
2261 tree arg0, arg1, arg2;
2262 machine_mode mode;
2264 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2265 return NULL_RTX;
2267 arg0 = CALL_EXPR_ARG (exp, 0);
2268 arg1 = CALL_EXPR_ARG (exp, 1);
2269 arg2 = CALL_EXPR_ARG (exp, 2);
2271 switch (DECL_FUNCTION_CODE (fndecl))
2273 CASE_FLT_FN (BUILT_IN_FMA):
2274 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2275 builtin_optab = fma_optab; break;
2276 default:
2277 gcc_unreachable ();
2280 /* Make a suitable register to place result in. */
2281 mode = TYPE_MODE (TREE_TYPE (exp));
2283 /* Before working hard, check whether the instruction is available. */
2284 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2285 return NULL_RTX;
2287 result = gen_reg_rtx (mode);
2289 /* Always stabilize the argument list. */
2290 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2291 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2292 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2294 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2295 op1 = expand_normal (arg1);
2296 op2 = expand_normal (arg2);
2298 start_sequence ();
2300 /* Compute into RESULT.
2301 Set RESULT to wherever the result comes back. */
2302 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2303 result, 0);
2305 /* If we were unable to expand via the builtin, stop the sequence
2306 (without outputting the insns) and call to the library function
2307 with the stabilized argument list. */
2308 if (result == 0)
2310 end_sequence ();
2311 return expand_call (exp, target, target == const0_rtx);
2314 /* Output the entire sequence. */
2315 insns = get_insns ();
2316 end_sequence ();
2317 emit_insn (insns);
2319 return result;
2322 /* Expand a call to the builtin sin and cos math functions.
2323 Return NULL_RTX if a normal call should be emitted rather than expanding the
2324 function in-line. EXP is the expression that is a call to the builtin
2325 function; if convenient, the result should be placed in TARGET.
2326 SUBTARGET may be used as the target for computing one of EXP's
2327 operands. */
2329 static rtx
2330 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2332 optab builtin_optab;
2333 rtx op0;
2334 rtx_insn *insns;
2335 tree fndecl = get_callee_fndecl (exp);
2336 machine_mode mode;
2337 tree arg;
2339 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2340 return NULL_RTX;
2342 arg = CALL_EXPR_ARG (exp, 0);
2344 switch (DECL_FUNCTION_CODE (fndecl))
2346 CASE_FLT_FN (BUILT_IN_SIN):
2347 CASE_FLT_FN (BUILT_IN_COS):
2348 builtin_optab = sincos_optab; break;
2349 default:
2350 gcc_unreachable ();
2353 /* Make a suitable register to place result in. */
2354 mode = TYPE_MODE (TREE_TYPE (exp));
2356 /* Check if sincos insn is available, otherwise fallback
2357 to sin or cos insn. */
2358 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2359 switch (DECL_FUNCTION_CODE (fndecl))
2361 CASE_FLT_FN (BUILT_IN_SIN):
2362 builtin_optab = sin_optab; break;
2363 CASE_FLT_FN (BUILT_IN_COS):
2364 builtin_optab = cos_optab; break;
2365 default:
2366 gcc_unreachable ();
2369 /* Before working hard, check whether the instruction is available. */
2370 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2372 rtx result = gen_reg_rtx (mode);
2374 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2375 need to expand the argument again. This way, we will not perform
2376 side-effects more the once. */
2377 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2379 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2381 start_sequence ();
2383 /* Compute into RESULT.
2384 Set RESULT to wherever the result comes back. */
2385 if (builtin_optab == sincos_optab)
2387 int ok;
2389 switch (DECL_FUNCTION_CODE (fndecl))
2391 CASE_FLT_FN (BUILT_IN_SIN):
2392 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2393 break;
2394 CASE_FLT_FN (BUILT_IN_COS):
2395 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2396 break;
2397 default:
2398 gcc_unreachable ();
2400 gcc_assert (ok);
2402 else
2403 result = expand_unop (mode, builtin_optab, op0, result, 0);
2405 if (result != 0)
2407 /* Output the entire sequence. */
2408 insns = get_insns ();
2409 end_sequence ();
2410 emit_insn (insns);
2411 return result;
2414 /* If we were unable to expand via the builtin, stop the sequence
2415 (without outputting the insns) and call to the library function
2416 with the stabilized argument list. */
2417 end_sequence ();
2420 return expand_call (exp, target, target == const0_rtx);
2423 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2424 return an RTL instruction code that implements the functionality.
2425 If that isn't possible or available return CODE_FOR_nothing. */
2427 static enum insn_code
2428 interclass_mathfn_icode (tree arg, tree fndecl)
2430 bool errno_set = false;
2431 optab builtin_optab = unknown_optab;
2432 machine_mode mode;
2434 switch (DECL_FUNCTION_CODE (fndecl))
2436 CASE_FLT_FN (BUILT_IN_ILOGB):
2437 errno_set = true; builtin_optab = ilogb_optab; break;
2438 CASE_FLT_FN (BUILT_IN_ISINF):
2439 builtin_optab = isinf_optab; break;
2440 case BUILT_IN_ISNORMAL:
2441 case BUILT_IN_ISFINITE:
2442 CASE_FLT_FN (BUILT_IN_FINITE):
2443 case BUILT_IN_FINITED32:
2444 case BUILT_IN_FINITED64:
2445 case BUILT_IN_FINITED128:
2446 case BUILT_IN_ISINFD32:
2447 case BUILT_IN_ISINFD64:
2448 case BUILT_IN_ISINFD128:
2449 /* These builtins have no optabs (yet). */
2450 break;
2451 default:
2452 gcc_unreachable ();
2455 /* There's no easy way to detect the case we need to set EDOM. */
2456 if (flag_errno_math && errno_set)
2457 return CODE_FOR_nothing;
2459 /* Optab mode depends on the mode of the input argument. */
2460 mode = TYPE_MODE (TREE_TYPE (arg));
2462 if (builtin_optab)
2463 return optab_handler (builtin_optab, mode);
2464 return CODE_FOR_nothing;
2467 /* Expand a call to one of the builtin math functions that operate on
2468 floating point argument and output an integer result (ilogb, isinf,
2469 isnan, etc).
2470 Return 0 if a normal call should be emitted rather than expanding the
2471 function in-line. EXP is the expression that is a call to the builtin
2472 function; if convenient, the result should be placed in TARGET. */
2474 static rtx
2475 expand_builtin_interclass_mathfn (tree exp, rtx target)
2477 enum insn_code icode = CODE_FOR_nothing;
2478 rtx op0;
2479 tree fndecl = get_callee_fndecl (exp);
2480 machine_mode mode;
2481 tree arg;
2483 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2484 return NULL_RTX;
2486 arg = CALL_EXPR_ARG (exp, 0);
2487 icode = interclass_mathfn_icode (arg, fndecl);
2488 mode = TYPE_MODE (TREE_TYPE (arg));
2490 if (icode != CODE_FOR_nothing)
2492 class expand_operand ops[1];
2493 rtx_insn *last = get_last_insn ();
2494 tree orig_arg = arg;
2496 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2497 need to expand the argument again. This way, we will not perform
2498 side-effects more the once. */
2499 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2501 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2503 if (mode != GET_MODE (op0))
2504 op0 = convert_to_mode (mode, op0, 0);
2506 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2507 if (maybe_legitimize_operands (icode, 0, 1, ops)
2508 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2509 return ops[0].value;
2511 delete_insns_since (last);
2512 CALL_EXPR_ARG (exp, 0) = orig_arg;
2515 return NULL_RTX;
2518 /* Expand a call to the builtin sincos math function.
2519 Return NULL_RTX if a normal call should be emitted rather than expanding the
2520 function in-line. EXP is the expression that is a call to the builtin
2521 function. */
2523 static rtx
2524 expand_builtin_sincos (tree exp)
2526 rtx op0, op1, op2, target1, target2;
2527 machine_mode mode;
2528 tree arg, sinp, cosp;
2529 int result;
2530 location_t loc = EXPR_LOCATION (exp);
2531 tree alias_type, alias_off;
2533 if (!validate_arglist (exp, REAL_TYPE,
2534 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2535 return NULL_RTX;
2537 arg = CALL_EXPR_ARG (exp, 0);
2538 sinp = CALL_EXPR_ARG (exp, 1);
2539 cosp = CALL_EXPR_ARG (exp, 2);
2541 /* Make a suitable register to place result in. */
2542 mode = TYPE_MODE (TREE_TYPE (arg));
2544 /* Check if sincos insn is available, otherwise emit the call. */
2545 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2546 return NULL_RTX;
2548 target1 = gen_reg_rtx (mode);
2549 target2 = gen_reg_rtx (mode);
2551 op0 = expand_normal (arg);
2552 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2553 alias_off = build_int_cst (alias_type, 0);
2554 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2555 sinp, alias_off));
2556 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2557 cosp, alias_off));
2559 /* Compute into target1 and target2.
2560 Set TARGET to wherever the result comes back. */
2561 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2562 gcc_assert (result);
2564 /* Move target1 and target2 to the memory locations indicated
2565 by op1 and op2. */
2566 emit_move_insn (op1, target1);
2567 emit_move_insn (op2, target2);
2569 return const0_rtx;
2572 /* Expand call EXP to the fegetround builtin (from C99 fenv.h), returning the
2573 result and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2574 static rtx
2575 expand_builtin_fegetround (tree exp, rtx target, machine_mode target_mode)
2577 if (!validate_arglist (exp, VOID_TYPE))
2578 return NULL_RTX;
2580 insn_code icode = direct_optab_handler (fegetround_optab, SImode);
2581 if (icode == CODE_FOR_nothing)
2582 return NULL_RTX;
2584 if (target == 0
2585 || GET_MODE (target) != target_mode
2586 || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2587 target = gen_reg_rtx (target_mode);
2589 rtx pat = GEN_FCN (icode) (target);
2590 if (!pat)
2591 return NULL_RTX;
2592 emit_insn (pat);
2594 return target;
2597 /* Expand call EXP to either feclearexcept or feraiseexcept builtins (from C99
2598 fenv.h), returning the result and setting it in TARGET. Otherwise return
2599 NULL_RTX on failure. */
2600 static rtx
2601 expand_builtin_feclear_feraise_except (tree exp, rtx target,
2602 machine_mode target_mode, optab op_optab)
2604 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
2605 return NULL_RTX;
2606 rtx op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
2608 insn_code icode = direct_optab_handler (op_optab, SImode);
2609 if (icode == CODE_FOR_nothing)
2610 return NULL_RTX;
2612 if (!(*insn_data[icode].operand[1].predicate) (op0, GET_MODE (op0)))
2613 return NULL_RTX;
2615 if (target == 0
2616 || GET_MODE (target) != target_mode
2617 || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2618 target = gen_reg_rtx (target_mode);
2620 rtx pat = GEN_FCN (icode) (target, op0);
2621 if (!pat)
2622 return NULL_RTX;
2623 emit_insn (pat);
2625 return target;
2628 /* Expand a call to the internal cexpi builtin to the sincos math function.
2629 EXP is the expression that is a call to the builtin function; if convenient,
2630 the result should be placed in TARGET. */
2632 static rtx
2633 expand_builtin_cexpi (tree exp, rtx target)
2635 tree fndecl = get_callee_fndecl (exp);
2636 tree arg, type;
2637 machine_mode mode;
2638 rtx op0, op1, op2;
2639 location_t loc = EXPR_LOCATION (exp);
2641 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2642 return NULL_RTX;
2644 arg = CALL_EXPR_ARG (exp, 0);
2645 type = TREE_TYPE (arg);
2646 mode = TYPE_MODE (TREE_TYPE (arg));
2648 /* Try expanding via a sincos optab, fall back to emitting a libcall
2649 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2650 is only generated from sincos, cexp or if we have either of them. */
2651 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2653 op1 = gen_reg_rtx (mode);
2654 op2 = gen_reg_rtx (mode);
2656 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2658 /* Compute into op1 and op2. */
2659 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2661 else if (targetm.libc_has_function (function_sincos, type))
2663 tree call, fn = NULL_TREE;
2664 tree top1, top2;
2665 rtx op1a, op2a;
2667 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2668 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2669 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2670 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2671 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2672 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2673 else
2674 gcc_unreachable ();
2676 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2677 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2678 op1a = copy_addr_to_reg (XEXP (op1, 0));
2679 op2a = copy_addr_to_reg (XEXP (op2, 0));
2680 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2681 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2683 /* Make sure not to fold the sincos call again. */
2684 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2685 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2686 call, 3, arg, top1, top2));
2688 else
2690 tree call, fn = NULL_TREE, narg;
2691 tree ctype = build_complex_type (type);
2693 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2694 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2695 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2696 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2697 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2698 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2699 else
2700 gcc_unreachable ();
2702 /* If we don't have a decl for cexp create one. This is the
2703 friendliest fallback if the user calls __builtin_cexpi
2704 without full target C99 function support. */
2705 if (fn == NULL_TREE)
2707 tree fntype;
2708 const char *name = NULL;
2710 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2711 name = "cexpf";
2712 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2713 name = "cexp";
2714 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2715 name = "cexpl";
2717 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2718 fn = build_fn_decl (name, fntype);
2721 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2722 build_real (type, dconst0), arg);
2724 /* Make sure not to fold the cexp call again. */
2725 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2726 return expand_expr (build_call_nary (ctype, call, 1, narg),
2727 target, VOIDmode, EXPAND_NORMAL);
2730 /* Now build the proper return type. */
2731 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2732 make_tree (TREE_TYPE (arg), op2),
2733 make_tree (TREE_TYPE (arg), op1)),
2734 target, VOIDmode, EXPAND_NORMAL);
2737 /* Conveniently construct a function call expression. FNDECL names the
2738 function to be called, N is the number of arguments, and the "..."
2739 parameters are the argument expressions. Unlike build_call_exr
2740 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2742 static tree
2743 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2745 va_list ap;
2746 tree fntype = TREE_TYPE (fndecl);
2747 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2749 va_start (ap, n);
2750 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2751 va_end (ap);
2752 SET_EXPR_LOCATION (fn, loc);
2753 return fn;
2756 /* Expand the __builtin_issignaling builtin. This needs to handle
2757 all floating point formats that do support NaNs (for those that
2758 don't it just sets target to 0). */
2760 static rtx
2761 expand_builtin_issignaling (tree exp, rtx target)
2763 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2764 return NULL_RTX;
2766 tree arg = CALL_EXPR_ARG (exp, 0);
2767 scalar_float_mode fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
2768 const struct real_format *fmt = REAL_MODE_FORMAT (fmode);
2770 /* Expand the argument yielding a RTX expression. */
2771 rtx temp = expand_normal (arg);
2773 /* If mode doesn't support NaN, always return 0.
2774 Don't use !HONOR_SNANS (fmode) here, so there is some possibility of
2775 __builtin_issignaling working without -fsignaling-nans. Especially
2776 when -fno-signaling-nans is the default.
2777 On the other side, MODE_HAS_NANS (fmode) is unnecessary, with
2778 -ffinite-math-only even __builtin_isnan or __builtin_fpclassify
2779 fold to 0 or non-NaN/Inf classification. */
2780 if (!HONOR_NANS (fmode))
2782 emit_move_insn (target, const0_rtx);
2783 return target;
2786 /* Check if the back end provides an insn that handles issignaling for the
2787 argument's mode. */
2788 enum insn_code icode = optab_handler (issignaling_optab, fmode);
2789 if (icode != CODE_FOR_nothing)
2791 rtx_insn *last = get_last_insn ();
2792 rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
2793 if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
2794 return this_target;
2795 delete_insns_since (last);
2798 if (DECIMAL_FLOAT_MODE_P (fmode))
2800 scalar_int_mode imode;
2801 rtx hi;
2802 switch (fmt->ieee_bits)
2804 case 32:
2805 case 64:
2806 imode = int_mode_for_mode (fmode).require ();
2807 temp = gen_lowpart (imode, temp);
2808 break;
2809 case 128:
2810 imode = int_mode_for_size (64, 1).require ();
2811 hi = NULL_RTX;
2812 /* For decimal128, TImode support isn't always there and even when
2813 it is, working on the DImode high part is usually better. */
2814 if (!MEM_P (temp))
2816 if (rtx t = simplify_gen_subreg (imode, temp, fmode,
2817 subreg_highpart_offset (imode,
2818 fmode)))
2819 hi = t;
2820 else
2822 scalar_int_mode imode2;
2823 if (int_mode_for_mode (fmode).exists (&imode2))
2825 rtx temp2 = gen_lowpart (imode2, temp);
2826 poly_uint64 off = subreg_highpart_offset (imode, imode2);
2827 if (rtx t = simplify_gen_subreg (imode, temp2,
2828 imode2, off))
2829 hi = t;
2832 if (!hi)
2834 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2835 emit_move_insn (mem, temp);
2836 temp = mem;
2839 if (!hi)
2841 poly_int64 offset
2842 = subreg_highpart_offset (imode, GET_MODE (temp));
2843 hi = adjust_address (temp, imode, offset);
2845 temp = hi;
2846 break;
2847 default:
2848 gcc_unreachable ();
2850 /* In all of decimal{32,64,128}, there is MSB sign bit and sNaN
2851 have 6 bits below it all set. */
2852 rtx val
2853 = GEN_INT (HOST_WIDE_INT_C (0x3f) << (GET_MODE_BITSIZE (imode) - 7));
2854 temp = expand_binop (imode, and_optab, temp, val,
2855 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2856 temp = emit_store_flag_force (target, EQ, temp, val, imode, 1, 1);
2857 return temp;
2860 /* Only PDP11 has these defined differently but doesn't support NaNs. */
2861 gcc_assert (FLOAT_WORDS_BIG_ENDIAN == WORDS_BIG_ENDIAN);
2862 gcc_assert (fmt->signbit_ro > 0 && fmt->b == 2);
2863 gcc_assert (MODE_COMPOSITE_P (fmode)
2864 || (fmt->pnan == fmt->p
2865 && fmt->signbit_ro == fmt->signbit_rw));
2867 switch (fmt->p)
2869 case 106: /* IBM double double */
2870 /* For IBM double double, recurse on the most significant double. */
2871 gcc_assert (MODE_COMPOSITE_P (fmode));
2872 temp = convert_modes (DFmode, fmode, temp, 0);
2873 fmode = DFmode;
2874 fmt = REAL_MODE_FORMAT (DFmode);
2875 /* FALLTHRU */
2876 case 8: /* bfloat */
2877 case 11: /* IEEE half */
2878 case 24: /* IEEE single */
2879 case 53: /* IEEE double or Intel extended with rounding to double */
2880 if (fmt->p == 53 && fmt->signbit_ro == 79)
2881 goto extended;
2883 scalar_int_mode imode = int_mode_for_mode (fmode).require ();
2884 temp = gen_lowpart (imode, temp);
2885 rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2))
2886 & ~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2887 if (fmt->qnan_msb_set)
2889 rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << fmt->signbit_ro));
2890 rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2));
2891 /* For non-MIPS/PA IEEE single/double/half or bfloat, expand to:
2892 ((temp ^ bit) & mask) > val. */
2893 temp = expand_binop (imode, xor_optab, temp, bit,
2894 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2895 temp = expand_binop (imode, and_optab, temp, mask,
2896 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2897 temp = emit_store_flag_force (target, GTU, temp, val, imode,
2898 1, 1);
2900 else
2902 /* For MIPS/PA IEEE single/double, expand to:
2903 (temp & val) == val. */
2904 temp = expand_binop (imode, and_optab, temp, val,
2905 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2906 temp = emit_store_flag_force (target, EQ, temp, val, imode,
2907 1, 1);
2910 break;
2911 case 113: /* IEEE quad */
2913 rtx hi = NULL_RTX, lo = NULL_RTX;
2914 scalar_int_mode imode = int_mode_for_size (64, 1).require ();
2915 /* For IEEE quad, TImode support isn't always there and even when
2916 it is, working on DImode parts is usually better. */
2917 if (!MEM_P (temp))
2919 hi = simplify_gen_subreg (imode, temp, fmode,
2920 subreg_highpart_offset (imode, fmode));
2921 lo = simplify_gen_subreg (imode, temp, fmode,
2922 subreg_lowpart_offset (imode, fmode));
2923 if (!hi || !lo)
2925 scalar_int_mode imode2;
2926 if (int_mode_for_mode (fmode).exists (&imode2))
2928 rtx temp2 = gen_lowpart (imode2, temp);
2929 hi = simplify_gen_subreg (imode, temp2, imode2,
2930 subreg_highpart_offset (imode,
2931 imode2));
2932 lo = simplify_gen_subreg (imode, temp2, imode2,
2933 subreg_lowpart_offset (imode,
2934 imode2));
2937 if (!hi || !lo)
2939 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2940 emit_move_insn (mem, temp);
2941 temp = mem;
2944 if (!hi || !lo)
2946 poly_int64 offset
2947 = subreg_highpart_offset (imode, GET_MODE (temp));
2948 hi = adjust_address (temp, imode, offset);
2949 offset = subreg_lowpart_offset (imode, GET_MODE (temp));
2950 lo = adjust_address (temp, imode, offset);
2952 rtx val = GEN_INT ((HOST_WIDE_INT_M1U << (fmt->p - 2 - 64))
2953 & ~(HOST_WIDE_INT_M1U << (fmt->signbit_ro - 64)));
2954 if (fmt->qnan_msb_set)
2956 rtx mask = GEN_INT (~(HOST_WIDE_INT_M1U << (fmt->signbit_ro
2957 - 64)));
2958 rtx bit = GEN_INT (HOST_WIDE_INT_1U << (fmt->p - 2 - 64));
2959 /* For non-MIPS/PA IEEE quad, expand to:
2960 (((hi ^ bit) | ((lo | -lo) >> 63)) & mask) > val. */
2961 rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
2962 lo = expand_binop (imode, ior_optab, lo, nlo,
2963 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2964 lo = expand_shift (RSHIFT_EXPR, imode, lo, 63, NULL_RTX, 1);
2965 temp = expand_binop (imode, xor_optab, hi, bit,
2966 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2967 temp = expand_binop (imode, ior_optab, temp, lo,
2968 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2969 temp = expand_binop (imode, and_optab, temp, mask,
2970 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2971 temp = emit_store_flag_force (target, GTU, temp, val, imode,
2972 1, 1);
2974 else
2976 /* For MIPS/PA IEEE quad, expand to:
2977 (hi & val) == val. */
2978 temp = expand_binop (imode, and_optab, hi, val,
2979 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2980 temp = emit_store_flag_force (target, EQ, temp, val, imode,
2981 1, 1);
2984 break;
2985 case 64: /* Intel or Motorola extended */
2986 extended:
2988 rtx ex, hi, lo;
2989 scalar_int_mode imode = int_mode_for_size (32, 1).require ();
2990 scalar_int_mode iemode = int_mode_for_size (16, 1).require ();
2991 if (!MEM_P (temp))
2993 rtx mem = assign_stack_temp (fmode, GET_MODE_SIZE (fmode));
2994 emit_move_insn (mem, temp);
2995 temp = mem;
2997 if (fmt->signbit_ro == 95)
2999 /* Motorola, always big endian, with 16-bit gap in between
3000 16-bit sign+exponent and 64-bit mantissa. */
3001 ex = adjust_address (temp, iemode, 0);
3002 hi = adjust_address (temp, imode, 4);
3003 lo = adjust_address (temp, imode, 8);
3005 else if (!WORDS_BIG_ENDIAN)
3007 /* Intel little endian, 64-bit mantissa followed by 16-bit
3008 sign+exponent and then either 16 or 48 bits of gap. */
3009 ex = adjust_address (temp, iemode, 8);
3010 hi = adjust_address (temp, imode, 4);
3011 lo = adjust_address (temp, imode, 0);
3013 else
3015 /* Big endian Itanium. */
3016 ex = adjust_address (temp, iemode, 0);
3017 hi = adjust_address (temp, imode, 2);
3018 lo = adjust_address (temp, imode, 6);
3020 rtx val = GEN_INT (HOST_WIDE_INT_M1U << 30);
3021 gcc_assert (fmt->qnan_msb_set);
3022 rtx mask = GEN_INT (0x7fff);
3023 rtx bit = GEN_INT (HOST_WIDE_INT_1U << 30);
3024 /* For Intel/Motorola extended format, expand to:
3025 (ex & mask) == mask && ((hi ^ bit) | ((lo | -lo) >> 31)) > val. */
3026 rtx nlo = expand_unop (imode, neg_optab, lo, NULL_RTX, 0);
3027 lo = expand_binop (imode, ior_optab, lo, nlo,
3028 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3029 lo = expand_shift (RSHIFT_EXPR, imode, lo, 31, NULL_RTX, 1);
3030 temp = expand_binop (imode, xor_optab, hi, bit,
3031 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3032 temp = expand_binop (imode, ior_optab, temp, lo,
3033 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3034 temp = emit_store_flag_force (target, GTU, temp, val, imode, 1, 1);
3035 ex = expand_binop (iemode, and_optab, ex, mask,
3036 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3037 ex = emit_store_flag_force (gen_reg_rtx (GET_MODE (temp)), EQ,
3038 ex, mask, iemode, 1, 1);
3039 temp = expand_binop (GET_MODE (temp), and_optab, temp, ex,
3040 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3042 break;
3043 default:
3044 gcc_unreachable ();
3047 return temp;
3050 /* Expand a call to one of the builtin rounding functions gcc defines
3051 as an extension (lfloor and lceil). As these are gcc extensions we
3052 do not need to worry about setting errno to EDOM.
3053 If expanding via optab fails, lower expression to (int)(floor(x)).
3054 EXP is the expression that is a call to the builtin function;
3055 if convenient, the result should be placed in TARGET. */
3057 static rtx
3058 expand_builtin_int_roundingfn (tree exp, rtx target)
3060 convert_optab builtin_optab;
3061 rtx op0, tmp;
3062 rtx_insn *insns;
3063 tree fndecl = get_callee_fndecl (exp);
3064 enum built_in_function fallback_fn;
3065 tree fallback_fndecl;
3066 machine_mode mode;
3067 tree arg;
3069 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3070 return NULL_RTX;
3072 arg = CALL_EXPR_ARG (exp, 0);
3074 switch (DECL_FUNCTION_CODE (fndecl))
3076 CASE_FLT_FN (BUILT_IN_ICEIL):
3077 CASE_FLT_FN (BUILT_IN_LCEIL):
3078 CASE_FLT_FN (BUILT_IN_LLCEIL):
3079 builtin_optab = lceil_optab;
3080 fallback_fn = BUILT_IN_CEIL;
3081 break;
3083 CASE_FLT_FN (BUILT_IN_IFLOOR):
3084 CASE_FLT_FN (BUILT_IN_LFLOOR):
3085 CASE_FLT_FN (BUILT_IN_LLFLOOR):
3086 builtin_optab = lfloor_optab;
3087 fallback_fn = BUILT_IN_FLOOR;
3088 break;
3090 default:
3091 gcc_unreachable ();
3094 /* Make a suitable register to place result in. */
3095 mode = TYPE_MODE (TREE_TYPE (exp));
3097 target = gen_reg_rtx (mode);
3099 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3100 need to expand the argument again. This way, we will not perform
3101 side-effects more the once. */
3102 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3104 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3106 start_sequence ();
3108 /* Compute into TARGET. */
3109 if (expand_sfix_optab (target, op0, builtin_optab))
3111 /* Output the entire sequence. */
3112 insns = get_insns ();
3113 end_sequence ();
3114 emit_insn (insns);
3115 return target;
3118 /* If we were unable to expand via the builtin, stop the sequence
3119 (without outputting the insns). */
3120 end_sequence ();
3122 /* Fall back to floating point rounding optab. */
3123 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
3125 /* For non-C99 targets we may end up without a fallback fndecl here
3126 if the user called __builtin_lfloor directly. In this case emit
3127 a call to the floor/ceil variants nevertheless. This should result
3128 in the best user experience for not full C99 targets. */
3129 if (fallback_fndecl == NULL_TREE)
3131 tree fntype;
3132 const char *name = NULL;
3134 switch (DECL_FUNCTION_CODE (fndecl))
3136 case BUILT_IN_ICEIL:
3137 case BUILT_IN_LCEIL:
3138 case BUILT_IN_LLCEIL:
3139 name = "ceil";
3140 break;
3141 case BUILT_IN_ICEILF:
3142 case BUILT_IN_LCEILF:
3143 case BUILT_IN_LLCEILF:
3144 name = "ceilf";
3145 break;
3146 case BUILT_IN_ICEILL:
3147 case BUILT_IN_LCEILL:
3148 case BUILT_IN_LLCEILL:
3149 name = "ceill";
3150 break;
3151 case BUILT_IN_IFLOOR:
3152 case BUILT_IN_LFLOOR:
3153 case BUILT_IN_LLFLOOR:
3154 name = "floor";
3155 break;
3156 case BUILT_IN_IFLOORF:
3157 case BUILT_IN_LFLOORF:
3158 case BUILT_IN_LLFLOORF:
3159 name = "floorf";
3160 break;
3161 case BUILT_IN_IFLOORL:
3162 case BUILT_IN_LFLOORL:
3163 case BUILT_IN_LLFLOORL:
3164 name = "floorl";
3165 break;
3166 default:
3167 gcc_unreachable ();
3170 fntype = build_function_type_list (TREE_TYPE (arg),
3171 TREE_TYPE (arg), NULL_TREE);
3172 fallback_fndecl = build_fn_decl (name, fntype);
3175 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
3177 tmp = expand_normal (exp);
3178 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
3180 /* Truncate the result of floating point optab to integer
3181 via expand_fix (). */
3182 target = gen_reg_rtx (mode);
3183 expand_fix (target, tmp, 0);
3185 return target;
3188 /* Expand a call to one of the builtin math functions doing integer
3189 conversion (lrint).
3190 Return 0 if a normal call should be emitted rather than expanding the
3191 function in-line. EXP is the expression that is a call to the builtin
3192 function; if convenient, the result should be placed in TARGET. */
3194 static rtx
3195 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
3197 convert_optab builtin_optab;
3198 rtx op0;
3199 rtx_insn *insns;
3200 tree fndecl = get_callee_fndecl (exp);
3201 tree arg;
3202 machine_mode mode;
3203 enum built_in_function fallback_fn = BUILT_IN_NONE;
3205 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
3206 return NULL_RTX;
3208 arg = CALL_EXPR_ARG (exp, 0);
3210 switch (DECL_FUNCTION_CODE (fndecl))
3212 CASE_FLT_FN (BUILT_IN_IRINT):
3213 fallback_fn = BUILT_IN_LRINT;
3214 gcc_fallthrough ();
3215 CASE_FLT_FN (BUILT_IN_LRINT):
3216 CASE_FLT_FN (BUILT_IN_LLRINT):
3217 builtin_optab = lrint_optab;
3218 break;
3220 CASE_FLT_FN (BUILT_IN_IROUND):
3221 fallback_fn = BUILT_IN_LROUND;
3222 gcc_fallthrough ();
3223 CASE_FLT_FN (BUILT_IN_LROUND):
3224 CASE_FLT_FN (BUILT_IN_LLROUND):
3225 builtin_optab = lround_optab;
3226 break;
3228 default:
3229 gcc_unreachable ();
3232 /* There's no easy way to detect the case we need to set EDOM. */
3233 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
3234 return NULL_RTX;
3236 /* Make a suitable register to place result in. */
3237 mode = TYPE_MODE (TREE_TYPE (exp));
3239 /* There's no easy way to detect the case we need to set EDOM. */
3240 if (!flag_errno_math)
3242 rtx result = gen_reg_rtx (mode);
3244 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
3245 need to expand the argument again. This way, we will not perform
3246 side-effects more the once. */
3247 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
3249 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
3251 start_sequence ();
3253 if (expand_sfix_optab (result, op0, builtin_optab))
3255 /* Output the entire sequence. */
3256 insns = get_insns ();
3257 end_sequence ();
3258 emit_insn (insns);
3259 return result;
3262 /* If we were unable to expand via the builtin, stop the sequence
3263 (without outputting the insns) and call to the library function
3264 with the stabilized argument list. */
3265 end_sequence ();
3268 if (fallback_fn != BUILT_IN_NONE)
3270 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
3271 targets, (int) round (x) should never be transformed into
3272 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
3273 a call to lround in the hope that the target provides at least some
3274 C99 functions. This should result in the best user experience for
3275 not full C99 targets.
3276 As scalar float conversions with same mode are useless in GIMPLE,
3277 we can end up e.g. with _Float32 argument passed to float builtin,
3278 try to get the type from the builtin prototype first. */
3279 tree fallback_fndecl = NULL_TREE;
3280 if (tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
3281 fallback_fndecl
3282 = mathfn_built_in_1 (TREE_VALUE (argtypes),
3283 as_combined_fn (fallback_fn), 0);
3284 if (fallback_fndecl == NULL_TREE)
3285 fallback_fndecl
3286 = mathfn_built_in_1 (TREE_TYPE (arg),
3287 as_combined_fn (fallback_fn), 0);
3288 if (fallback_fndecl)
3290 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
3291 fallback_fndecl, 1, arg);
3293 target = expand_call (exp, NULL_RTX, target == const0_rtx);
3294 target = maybe_emit_group_store (target, TREE_TYPE (exp));
3295 return convert_to_mode (mode, target, 0);
3299 return expand_call (exp, target, target == const0_rtx);
3302 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
3303 a normal call should be emitted rather than expanding the function
3304 in-line. EXP is the expression that is a call to the builtin
3305 function; if convenient, the result should be placed in TARGET. */
3307 static rtx
3308 expand_builtin_powi (tree exp, rtx target)
3310 tree arg0, arg1;
3311 rtx op0, op1;
3312 machine_mode mode;
3313 machine_mode mode2;
3315 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3316 return NULL_RTX;
3318 arg0 = CALL_EXPR_ARG (exp, 0);
3319 arg1 = CALL_EXPR_ARG (exp, 1);
3320 mode = TYPE_MODE (TREE_TYPE (exp));
3322 /* Emit a libcall to libgcc. */
3324 /* Mode of the 2nd argument must match that of an int. */
3325 mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
3327 if (target == NULL_RTX)
3328 target = gen_reg_rtx (mode);
3330 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3331 if (GET_MODE (op0) != mode)
3332 op0 = convert_to_mode (mode, op0, 0);
3333 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3334 if (GET_MODE (op1) != mode2)
3335 op1 = convert_to_mode (mode2, op1, 0);
3337 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3338 target, LCT_CONST, mode,
3339 op0, mode, op1, mode2);
3341 return target;
3344 /* Expand expression EXP which is a call to the strlen builtin. Return
3345 NULL_RTX if we failed and the caller should emit a normal call, otherwise
3346 try to get the result in TARGET, if convenient. */
3348 static rtx
3349 expand_builtin_strlen (tree exp, rtx target,
3350 machine_mode target_mode)
3352 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3353 return NULL_RTX;
3355 tree src = CALL_EXPR_ARG (exp, 0);
3357 /* If the length can be computed at compile-time, return it. */
3358 if (tree len = c_strlen (src, 0))
3359 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3361 /* If the length can be computed at compile-time and is constant
3362 integer, but there are side-effects in src, evaluate
3363 src for side-effects, then return len.
3364 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3365 can be optimized into: i++; x = 3; */
3366 tree len = c_strlen (src, 1);
3367 if (len && TREE_CODE (len) == INTEGER_CST)
3369 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3370 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3373 unsigned int align = get_pointer_alignment (src) / BITS_PER_UNIT;
3375 /* If SRC is not a pointer type, don't do this operation inline. */
3376 if (align == 0)
3377 return NULL_RTX;
3379 /* Bail out if we can't compute strlen in the right mode. */
3380 machine_mode insn_mode;
3381 enum insn_code icode = CODE_FOR_nothing;
3382 FOR_EACH_MODE_FROM (insn_mode, target_mode)
3384 icode = optab_handler (strlen_optab, insn_mode);
3385 if (icode != CODE_FOR_nothing)
3386 break;
3388 if (insn_mode == VOIDmode)
3389 return NULL_RTX;
3391 /* Make a place to hold the source address. We will not expand
3392 the actual source until we are sure that the expansion will
3393 not fail -- there are trees that cannot be expanded twice. */
3394 rtx src_reg = gen_reg_rtx (Pmode);
3396 /* Mark the beginning of the strlen sequence so we can emit the
3397 source operand later. */
3398 rtx_insn *before_strlen = get_last_insn ();
3400 class expand_operand ops[4];
3401 create_output_operand (&ops[0], target, insn_mode);
3402 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3403 create_integer_operand (&ops[2], 0);
3404 create_integer_operand (&ops[3], align);
3405 if (!maybe_expand_insn (icode, 4, ops))
3406 return NULL_RTX;
3408 /* Check to see if the argument was declared attribute nonstring
3409 and if so, issue a warning since at this point it's not known
3410 to be nul-terminated. */
3411 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3413 /* Now that we are assured of success, expand the source. */
3414 start_sequence ();
3415 rtx pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3416 if (pat != src_reg)
3418 #ifdef POINTERS_EXTEND_UNSIGNED
3419 if (GET_MODE (pat) != Pmode)
3420 pat = convert_to_mode (Pmode, pat,
3421 POINTERS_EXTEND_UNSIGNED);
3422 #endif
3423 emit_move_insn (src_reg, pat);
3425 pat = get_insns ();
3426 end_sequence ();
3428 if (before_strlen)
3429 emit_insn_after (pat, before_strlen);
3430 else
3431 emit_insn_before (pat, get_insns ());
3433 /* Return the value in the proper mode for this function. */
3434 if (GET_MODE (ops[0].value) == target_mode)
3435 target = ops[0].value;
3436 else if (target != 0)
3437 convert_move (target, ops[0].value, 0);
3438 else
3439 target = convert_to_mode (target_mode, ops[0].value, 0);
3441 return target;
3444 /* Expand call EXP to the strnlen built-in, returning the result
3445 and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3447 static rtx
3448 expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
3450 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3451 return NULL_RTX;
3453 tree src = CALL_EXPR_ARG (exp, 0);
3454 tree bound = CALL_EXPR_ARG (exp, 1);
3456 if (!bound)
3457 return NULL_RTX;
3459 location_t loc = UNKNOWN_LOCATION;
3460 if (EXPR_HAS_LOCATION (exp))
3461 loc = EXPR_LOCATION (exp);
3463 /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3464 so these conversions aren't necessary. */
3465 c_strlen_data lendata = { };
3466 tree len = c_strlen (src, 0, &lendata, 1);
3467 if (len)
3468 len = fold_convert_loc (loc, TREE_TYPE (bound), len);
3470 if (TREE_CODE (bound) == INTEGER_CST)
3472 if (!len)
3473 return NULL_RTX;
3475 len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
3476 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3479 if (TREE_CODE (bound) != SSA_NAME)
3480 return NULL_RTX;
3482 wide_int min, max;
3483 value_range r;
3484 get_global_range_query ()->range_of_expr (r, bound);
3485 if (r.kind () != VR_RANGE)
3486 return NULL_RTX;
3487 min = r.lower_bound ();
3488 max = r.upper_bound ();
3490 if (!len || TREE_CODE (len) != INTEGER_CST)
3492 bool exact;
3493 lendata.decl = unterminated_array (src, &len, &exact);
3494 if (!lendata.decl)
3495 return NULL_RTX;
3498 if (lendata.decl)
3499 return NULL_RTX;
3501 if (wi::gtu_p (min, wi::to_wide (len)))
3502 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3504 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound);
3505 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3508 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3509 bytes from bytes at DATA + OFFSET and return it reinterpreted as
3510 a target constant. */
3512 static rtx
3513 builtin_memcpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3514 fixed_size_mode mode)
3516 /* The REPresentation pointed to by DATA need not be a nul-terminated
3517 string but the caller guarantees it's large enough for MODE. */
3518 const char *rep = (const char *) data;
3520 /* The by-pieces infrastructure does not try to pick a vector mode
3521 for memcpy expansion. */
3522 return c_readstr (rep + offset, as_a <scalar_int_mode> (mode),
3523 /*nul_terminated=*/false);
3526 /* LEN specify length of the block of memcpy/memset operation.
3527 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3528 In some cases we can make very likely guess on max size, then we
3529 set it into PROBABLE_MAX_SIZE. */
3531 static void
3532 determine_block_size (tree len, rtx len_rtx,
3533 unsigned HOST_WIDE_INT *min_size,
3534 unsigned HOST_WIDE_INT *max_size,
3535 unsigned HOST_WIDE_INT *probable_max_size)
3537 if (CONST_INT_P (len_rtx))
3539 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3540 return;
3542 else
3544 wide_int min, max;
3545 enum value_range_kind range_type = VR_UNDEFINED;
3547 /* Determine bounds from the type. */
3548 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3549 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3550 else
3551 *min_size = 0;
3552 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3553 *probable_max_size = *max_size
3554 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3555 else
3556 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3558 if (TREE_CODE (len) == SSA_NAME)
3560 value_range r;
3561 get_global_range_query ()->range_of_expr (r, len);
3562 range_type = r.kind ();
3563 if (range_type != VR_UNDEFINED)
3565 min = wi::to_wide (r.min ());
3566 max = wi::to_wide (r.max ());
3569 if (range_type == VR_RANGE)
3571 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3572 *min_size = min.to_uhwi ();
3573 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3574 *probable_max_size = *max_size = max.to_uhwi ();
3576 else if (range_type == VR_ANTI_RANGE)
3578 /* Code like
3580 int n;
3581 if (n < 100)
3582 memcpy (a, b, n)
3584 Produce anti range allowing negative values of N. We still
3585 can use the information and make a guess that N is not negative.
3587 if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3588 *probable_max_size = min.to_uhwi () - 1;
3591 gcc_checking_assert (*max_size <=
3592 (unsigned HOST_WIDE_INT)
3593 GET_MODE_MASK (GET_MODE (len_rtx)));
3596 /* Expand a call EXP to the memcpy builtin.
3597 Return NULL_RTX if we failed, the caller should emit a normal call,
3598 otherwise try to get the result in TARGET, if convenient (and in
3599 mode MODE if that's convenient). */
3601 static rtx
3602 expand_builtin_memcpy (tree exp, rtx target)
3604 if (!validate_arglist (exp,
3605 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3606 return NULL_RTX;
3608 tree dest = CALL_EXPR_ARG (exp, 0);
3609 tree src = CALL_EXPR_ARG (exp, 1);
3610 tree len = CALL_EXPR_ARG (exp, 2);
3612 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3613 /*retmode=*/ RETURN_BEGIN, false);
3616 /* Check a call EXP to the memmove built-in for validity.
3617 Return NULL_RTX on both success and failure. */
3619 static rtx
3620 expand_builtin_memmove (tree exp, rtx target)
3622 if (!validate_arglist (exp,
3623 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3624 return NULL_RTX;
3626 tree dest = CALL_EXPR_ARG (exp, 0);
3627 tree src = CALL_EXPR_ARG (exp, 1);
3628 tree len = CALL_EXPR_ARG (exp, 2);
3630 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3631 /*retmode=*/ RETURN_BEGIN, true);
3634 /* Expand a call EXP to the mempcpy builtin.
3635 Return NULL_RTX if we failed; the caller should emit a normal call,
3636 otherwise try to get the result in TARGET, if convenient (and in
3637 mode MODE if that's convenient). */
3639 static rtx
3640 expand_builtin_mempcpy (tree exp, rtx target)
3642 if (!validate_arglist (exp,
3643 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3644 return NULL_RTX;
3646 tree dest = CALL_EXPR_ARG (exp, 0);
3647 tree src = CALL_EXPR_ARG (exp, 1);
3648 tree len = CALL_EXPR_ARG (exp, 2);
3650 /* Policy does not generally allow using compute_objsize (which
3651 is used internally by check_memop_size) to change code generation
3652 or drive optimization decisions.
3654 In this instance it is safe because the code we generate has
3655 the same semantics regardless of the return value of
3656 check_memop_sizes. Exactly the same amount of data is copied
3657 and the return value is exactly the same in both cases.
3659 Furthermore, check_memop_size always uses mode 0 for the call to
3660 compute_objsize, so the imprecise nature of compute_objsize is
3661 avoided. */
3663 /* Avoid expanding mempcpy into memcpy when the call is determined
3664 to overflow the buffer. This also prevents the same overflow
3665 from being diagnosed again when expanding memcpy. */
3667 return expand_builtin_mempcpy_args (dest, src, len,
3668 target, exp, /*retmode=*/ RETURN_END);
3671 /* Helper function to do the actual work for expand of memory copy family
3672 functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3673 of memory from SRC to DEST and assign to TARGET if convenient. Return
3674 value is based on RETMODE argument. */
3676 static rtx
3677 expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3678 rtx target, tree exp, memop_ret retmode,
3679 bool might_overlap)
3681 unsigned int src_align = get_pointer_alignment (src);
3682 unsigned int dest_align = get_pointer_alignment (dest);
3683 rtx dest_mem, src_mem, dest_addr, len_rtx;
3684 HOST_WIDE_INT expected_size = -1;
3685 unsigned int expected_align = 0;
3686 unsigned HOST_WIDE_INT min_size;
3687 unsigned HOST_WIDE_INT max_size;
3688 unsigned HOST_WIDE_INT probable_max_size;
3690 bool is_move_done;
3692 /* If DEST is not a pointer type, call the normal function. */
3693 if (dest_align == 0)
3694 return NULL_RTX;
3696 /* If either SRC is not a pointer type, don't do this
3697 operation in-line. */
3698 if (src_align == 0)
3699 return NULL_RTX;
3701 if (currently_expanding_gimple_stmt)
3702 stringop_block_profile (currently_expanding_gimple_stmt,
3703 &expected_align, &expected_size);
3705 if (expected_align < dest_align)
3706 expected_align = dest_align;
3707 dest_mem = get_memory_rtx (dest, len);
3708 set_mem_align (dest_mem, dest_align);
3709 len_rtx = expand_normal (len);
3710 determine_block_size (len, len_rtx, &min_size, &max_size,
3711 &probable_max_size);
3713 /* Try to get the byte representation of the constant SRC points to,
3714 with its byte size in NBYTES. */
3715 unsigned HOST_WIDE_INT nbytes;
3716 const char *rep = getbyterep (src, &nbytes);
3718 /* If the function's constant bound LEN_RTX is less than or equal
3719 to the byte size of the representation of the constant argument,
3720 and if block move would be done by pieces, we can avoid loading
3721 the bytes from memory and only store the computed constant.
3722 This works in the overlap (memmove) case as well because
3723 store_by_pieces just generates a series of stores of constants
3724 from the representation returned by getbyterep(). */
3725 if (rep
3726 && CONST_INT_P (len_rtx)
3727 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes
3728 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3729 CONST_CAST (char *, rep),
3730 dest_align, false))
3732 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3733 builtin_memcpy_read_str,
3734 CONST_CAST (char *, rep),
3735 dest_align, false, retmode);
3736 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3737 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3738 return dest_mem;
3741 src_mem = get_memory_rtx (src, len);
3742 set_mem_align (src_mem, src_align);
3744 /* Copy word part most expediently. */
3745 enum block_op_methods method = BLOCK_OP_NORMAL;
3746 if (CALL_EXPR_TAILCALL (exp)
3747 && (retmode == RETURN_BEGIN || target == const0_rtx))
3748 method = BLOCK_OP_TAILCALL;
3749 bool use_mempcpy_call = (targetm.libc_has_fast_function (BUILT_IN_MEMPCPY)
3750 && retmode == RETURN_END
3751 && !might_overlap
3752 && target != const0_rtx);
3753 if (use_mempcpy_call)
3754 method = BLOCK_OP_NO_LIBCALL_RET;
3755 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3756 expected_align, expected_size,
3757 min_size, max_size, probable_max_size,
3758 use_mempcpy_call, &is_move_done,
3759 might_overlap);
3761 /* Bail out when a mempcpy call would be expanded as libcall and when
3762 we have a target that provides a fast implementation
3763 of mempcpy routine. */
3764 if (!is_move_done)
3765 return NULL_RTX;
3767 if (dest_addr == pc_rtx)
3768 return NULL_RTX;
3770 if (dest_addr == 0)
3772 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3773 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3776 if (retmode != RETURN_BEGIN && target != const0_rtx)
3778 dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3779 /* stpcpy pointer to last byte. */
3780 if (retmode == RETURN_END_MINUS_ONE)
3781 dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3784 return dest_addr;
3787 static rtx
3788 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3789 rtx target, tree orig_exp, memop_ret retmode)
3791 return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3792 retmode, false);
3795 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3796 we failed, the caller should emit a normal call, otherwise try to
3797 get the result in TARGET, if convenient.
3798 Return value is based on RETMODE argument. */
3800 static rtx
3801 expand_movstr (tree dest, tree src, rtx target, memop_ret retmode)
3803 class expand_operand ops[3];
3804 rtx dest_mem;
3805 rtx src_mem;
3807 if (!targetm.have_movstr ())
3808 return NULL_RTX;
3810 dest_mem = get_memory_rtx (dest, NULL);
3811 src_mem = get_memory_rtx (src, NULL);
3812 if (retmode == RETURN_BEGIN)
3814 target = force_reg (Pmode, XEXP (dest_mem, 0));
3815 dest_mem = replace_equiv_address (dest_mem, target);
3818 create_output_operand (&ops[0],
3819 retmode != RETURN_BEGIN ? target : NULL_RTX, Pmode);
3820 create_fixed_operand (&ops[1], dest_mem);
3821 create_fixed_operand (&ops[2], src_mem);
3822 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3823 return NULL_RTX;
3825 if (retmode != RETURN_BEGIN && target != const0_rtx)
3827 target = ops[0].value;
3828 /* movstr is supposed to set end to the address of the NUL
3829 terminator. If the caller requested a mempcpy-like return value,
3830 adjust it. */
3831 if (retmode == RETURN_END)
3833 rtx tem = plus_constant (GET_MODE (target),
3834 gen_lowpart (GET_MODE (target), target), 1);
3835 emit_move_insn (target, force_operand (tem, NULL_RTX));
3838 return target;
3841 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3842 NULL_RTX if we failed the caller should emit a normal call, otherwise
3843 try to get the result in TARGET, if convenient (and in mode MODE if that's
3844 convenient). */
3846 static rtx
3847 expand_builtin_strcpy (tree exp, rtx target)
3849 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3850 return NULL_RTX;
3852 tree dest = CALL_EXPR_ARG (exp, 0);
3853 tree src = CALL_EXPR_ARG (exp, 1);
3855 return expand_builtin_strcpy_args (exp, dest, src, target);
3858 /* Helper function to do the actual work for expand_builtin_strcpy. The
3859 arguments to the builtin_strcpy call DEST and SRC are broken out
3860 so that this can also be called without constructing an actual CALL_EXPR.
3861 The other arguments and return value are the same as for
3862 expand_builtin_strcpy. */
3864 static rtx
3865 expand_builtin_strcpy_args (tree, tree dest, tree src, rtx target)
3867 return expand_movstr (dest, src, target, /*retmode=*/ RETURN_BEGIN);
3870 /* Expand a call EXP to the stpcpy builtin.
3871 Return NULL_RTX if we failed the caller should emit a normal call,
3872 otherwise try to get the result in TARGET, if convenient (and in
3873 mode MODE if that's convenient). */
3875 static rtx
3876 expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
3878 tree dst, src;
3879 location_t loc = EXPR_LOCATION (exp);
3881 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3882 return NULL_RTX;
3884 dst = CALL_EXPR_ARG (exp, 0);
3885 src = CALL_EXPR_ARG (exp, 1);
3887 /* If return value is ignored, transform stpcpy into strcpy. */
3888 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3890 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3891 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3892 return expand_expr (result, target, mode, EXPAND_NORMAL);
3894 else
3896 tree len, lenp1;
3897 rtx ret;
3899 /* Ensure we get an actual string whose length can be evaluated at
3900 compile-time, not an expression containing a string. This is
3901 because the latter will potentially produce pessimized code
3902 when used to produce the return value. */
3903 c_strlen_data lendata = { };
3904 if (!c_getstr (src)
3905 || !(len = c_strlen (src, 0, &lendata, 1)))
3906 return expand_movstr (dst, src, target,
3907 /*retmode=*/ RETURN_END_MINUS_ONE);
3909 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3910 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3911 target, exp,
3912 /*retmode=*/ RETURN_END_MINUS_ONE);
3914 if (ret)
3915 return ret;
3917 if (TREE_CODE (len) == INTEGER_CST)
3919 rtx len_rtx = expand_normal (len);
3921 if (CONST_INT_P (len_rtx))
3923 ret = expand_builtin_strcpy_args (exp, dst, src, target);
3925 if (ret)
3927 if (! target)
3929 if (mode != VOIDmode)
3930 target = gen_reg_rtx (mode);
3931 else
3932 target = gen_reg_rtx (GET_MODE (ret));
3934 if (GET_MODE (target) != GET_MODE (ret))
3935 ret = gen_lowpart (GET_MODE (target), ret);
3937 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3938 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3939 gcc_assert (ret);
3941 return target;
3946 return expand_movstr (dst, src, target,
3947 /*retmode=*/ RETURN_END_MINUS_ONE);
3951 /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
3952 arguments while being careful to avoid duplicate warnings (which could
3953 be issued if the expander were to expand the call, resulting in it
3954 being emitted in expand_call(). */
3956 static rtx
3957 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3959 if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode))
3961 /* The call has been successfully expanded. Check for nonstring
3962 arguments and issue warnings as appropriate. */
3963 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3964 return ret;
3967 return NULL_RTX;
3970 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3971 bytes from constant string DATA + OFFSET and return it as target
3972 constant. */
3975 builtin_strncpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3976 fixed_size_mode mode)
3978 const char *str = (const char *) data;
3980 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3981 return const0_rtx;
3983 /* The by-pieces infrastructure does not try to pick a vector mode
3984 for strncpy expansion. */
3985 return c_readstr (str + offset, as_a <scalar_int_mode> (mode));
3988 /* Helper to check the sizes of sequences and the destination of calls
3989 to __builtin_strncat and __builtin___strncat_chk. Returns true on
3990 success (no overflow or invalid sizes), false otherwise. */
3992 static bool
3993 check_strncat_sizes (tree exp, tree objsize)
3995 tree dest = CALL_EXPR_ARG (exp, 0);
3996 tree src = CALL_EXPR_ARG (exp, 1);
3997 tree maxread = CALL_EXPR_ARG (exp, 2);
3999 /* Try to determine the range of lengths that the source expression
4000 refers to. */
4001 c_strlen_data lendata = { };
4002 get_range_strlen (src, &lendata, /* eltsize = */ 1);
4004 /* Try to verify that the destination is big enough for the shortest
4005 string. */
4007 access_data data (nullptr, exp, access_read_write, maxread, true);
4008 if (!objsize && warn_stringop_overflow)
4010 /* If it hasn't been provided by __strncat_chk, try to determine
4011 the size of the destination object into which the source is
4012 being copied. */
4013 objsize = compute_objsize (dest, warn_stringop_overflow - 1, &data.dst);
4016 /* Add one for the terminating nul. */
4017 tree srclen = (lendata.minlen
4018 ? fold_build2 (PLUS_EXPR, size_type_node, lendata.minlen,
4019 size_one_node)
4020 : NULL_TREE);
4022 /* The strncat function copies at most MAXREAD bytes and always appends
4023 the terminating nul so the specified upper bound should never be equal
4024 to (or greater than) the size of the destination. */
4025 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
4026 && tree_int_cst_equal (objsize, maxread))
4028 location_t loc = EXPR_LOCATION (exp);
4029 warning_at (loc, OPT_Wstringop_overflow_,
4030 "%qD specified bound %E equals destination size",
4031 get_callee_fndecl (exp), maxread);
4033 return false;
4036 if (!srclen
4037 || (maxread && tree_fits_uhwi_p (maxread)
4038 && tree_fits_uhwi_p (srclen)
4039 && tree_int_cst_lt (maxread, srclen)))
4040 srclen = maxread;
4042 /* The number of bytes to write is LEN but check_access will alsoa
4043 check SRCLEN if LEN's value isn't known. */
4044 return check_access (exp, /*dstwrite=*/NULL_TREE, maxread, srclen,
4045 objsize, data.mode, &data);
4048 /* Expand expression EXP, which is a call to the strncpy builtin. Return
4049 NULL_RTX if we failed the caller should emit a normal call. */
4051 static rtx
4052 expand_builtin_strncpy (tree exp, rtx target)
4054 location_t loc = EXPR_LOCATION (exp);
4056 if (!validate_arglist (exp,
4057 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4058 return NULL_RTX;
4059 tree dest = CALL_EXPR_ARG (exp, 0);
4060 tree src = CALL_EXPR_ARG (exp, 1);
4061 /* The number of bytes to write (not the maximum). */
4062 tree len = CALL_EXPR_ARG (exp, 2);
4064 /* The length of the source sequence. */
4065 tree slen = c_strlen (src, 1);
4067 /* We must be passed a constant len and src parameter. */
4068 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4069 return NULL_RTX;
4071 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4073 /* We're required to pad with trailing zeros if the requested
4074 len is greater than strlen(s2)+1. In that case try to
4075 use store_by_pieces, if it fails, punt. */
4076 if (tree_int_cst_lt (slen, len))
4078 unsigned int dest_align = get_pointer_alignment (dest);
4079 const char *p = c_getstr (src);
4080 rtx dest_mem;
4082 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4083 || !can_store_by_pieces (tree_to_uhwi (len),
4084 builtin_strncpy_read_str,
4085 CONST_CAST (char *, p),
4086 dest_align, false))
4087 return NULL_RTX;
4089 dest_mem = get_memory_rtx (dest, len);
4090 store_by_pieces (dest_mem, tree_to_uhwi (len),
4091 builtin_strncpy_read_str,
4092 CONST_CAST (char *, p), dest_align, false,
4093 RETURN_BEGIN);
4094 dest_mem = force_operand (XEXP (dest_mem, 0), target);
4095 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4096 return dest_mem;
4099 return NULL_RTX;
4102 /* Return the RTL of a register in MODE generated from PREV in the
4103 previous iteration. */
4105 static rtx
4106 gen_memset_value_from_prev (by_pieces_prev *prev, fixed_size_mode mode)
4108 rtx target = nullptr;
4109 if (prev != nullptr && prev->data != nullptr)
4111 /* Use the previous data in the same mode. */
4112 if (prev->mode == mode)
4113 return prev->data;
4115 fixed_size_mode prev_mode = prev->mode;
4117 /* Don't use the previous data to write QImode if it is in a
4118 vector mode. */
4119 if (VECTOR_MODE_P (prev_mode) && mode == QImode)
4120 return target;
4122 rtx prev_rtx = prev->data;
4124 if (REG_P (prev_rtx)
4125 && HARD_REGISTER_P (prev_rtx)
4126 && lowpart_subreg_regno (REGNO (prev_rtx), prev_mode, mode) < 0)
4128 /* This case occurs when PREV_MODE is a vector and when
4129 MODE is too small to store using vector operations.
4130 After register allocation, the code will need to move the
4131 lowpart of the vector register into a non-vector register.
4133 Also, the target has chosen to use a hard register
4134 instead of going with the default choice of using a
4135 pseudo register. We should respect that choice and try to
4136 avoid creating a pseudo register with the same mode as the
4137 current hard register.
4139 In principle, we could just use a lowpart MODE subreg of
4140 the vector register. However, the vector register mode might
4141 be too wide for non-vector registers, and we already know
4142 that the non-vector mode is too small for vector registers.
4143 It's therefore likely that we'd need to spill to memory in
4144 the vector mode and reload the non-vector value from there.
4146 Try to avoid that by reducing the vector register to the
4147 smallest size that it can hold. This should increase the
4148 chances that non-vector registers can hold both the inner
4149 and outer modes of the subreg that we generate later. */
4150 machine_mode m;
4151 fixed_size_mode candidate;
4152 FOR_EACH_MODE_IN_CLASS (m, GET_MODE_CLASS (mode))
4153 if (is_a<fixed_size_mode> (m, &candidate))
4155 if (GET_MODE_SIZE (candidate)
4156 >= GET_MODE_SIZE (prev_mode))
4157 break;
4158 if (GET_MODE_SIZE (candidate) >= GET_MODE_SIZE (mode)
4159 && lowpart_subreg_regno (REGNO (prev_rtx),
4160 prev_mode, candidate) >= 0)
4162 target = lowpart_subreg (candidate, prev_rtx,
4163 prev_mode);
4164 prev_rtx = target;
4165 prev_mode = candidate;
4166 break;
4169 if (target == nullptr)
4170 prev_rtx = copy_to_reg (prev_rtx);
4173 target = lowpart_subreg (mode, prev_rtx, prev_mode);
4175 return target;
4178 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4179 bytes from constant string DATA + OFFSET and return it as target
4180 constant. If PREV isn't nullptr, it has the RTL info from the
4181 previous iteration. */
4184 builtin_memset_read_str (void *data, void *prev,
4185 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4186 fixed_size_mode mode)
4188 const char *c = (const char *) data;
4189 unsigned int size = GET_MODE_SIZE (mode);
4191 rtx target = gen_memset_value_from_prev ((by_pieces_prev *) prev,
4192 mode);
4193 if (target != nullptr)
4194 return target;
4195 rtx src = gen_int_mode (*c, QImode);
4197 if (VECTOR_MODE_P (mode))
4199 gcc_assert (GET_MODE_INNER (mode) == QImode);
4201 rtx const_vec = gen_const_vec_duplicate (mode, src);
4202 if (prev == NULL)
4203 /* Return CONST_VECTOR when called by a query function. */
4204 return const_vec;
4206 /* Use the move expander with CONST_VECTOR. */
4207 target = targetm.gen_memset_scratch_rtx (mode);
4208 emit_move_insn (target, const_vec);
4209 return target;
4212 char *p = XALLOCAVEC (char, size);
4214 memset (p, *c, size);
4216 /* Vector modes should be handled above. */
4217 return c_readstr (p, as_a <scalar_int_mode> (mode));
4220 /* Callback routine for store_by_pieces. Return the RTL of a register
4221 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4222 char value given in the RTL register data. For example, if mode is
4223 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
4224 nullptr, it has the RTL info from the previous iteration. */
4226 static rtx
4227 builtin_memset_gen_str (void *data, void *prev,
4228 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4229 fixed_size_mode mode)
4231 rtx target, coeff;
4232 size_t size;
4233 char *p;
4235 size = GET_MODE_SIZE (mode);
4236 if (size == 1)
4237 return (rtx) data;
4239 target = gen_memset_value_from_prev ((by_pieces_prev *) prev, mode);
4240 if (target != nullptr)
4241 return target;
4243 if (VECTOR_MODE_P (mode))
4245 gcc_assert (GET_MODE_INNER (mode) == QImode);
4247 /* vec_duplicate_optab is a precondition to pick a vector mode for
4248 the memset expander. */
4249 insn_code icode = optab_handler (vec_duplicate_optab, mode);
4251 target = targetm.gen_memset_scratch_rtx (mode);
4252 class expand_operand ops[2];
4253 create_output_operand (&ops[0], target, mode);
4254 create_input_operand (&ops[1], (rtx) data, QImode);
4255 expand_insn (icode, 2, ops);
4256 if (!rtx_equal_p (target, ops[0].value))
4257 emit_move_insn (target, ops[0].value);
4259 return target;
4262 p = XALLOCAVEC (char, size);
4263 memset (p, 1, size);
4264 /* Vector modes should be handled above. */
4265 coeff = c_readstr (p, as_a <scalar_int_mode> (mode));
4267 target = convert_to_mode (mode, (rtx) data, 1);
4268 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4269 return force_reg (mode, target);
4272 /* Expand expression EXP, which is a call to the memset builtin. Return
4273 NULL_RTX if we failed the caller should emit a normal call, otherwise
4274 try to get the result in TARGET, if convenient (and in mode MODE if that's
4275 convenient). */
4278 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4280 if (!validate_arglist (exp,
4281 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4282 return NULL_RTX;
4284 tree dest = CALL_EXPR_ARG (exp, 0);
4285 tree val = CALL_EXPR_ARG (exp, 1);
4286 tree len = CALL_EXPR_ARG (exp, 2);
4288 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4291 /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
4292 Return TRUE if successful, FALSE otherwise. TO is assumed to be
4293 aligned at an ALIGN-bits boundary. LEN must be a multiple of
4294 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
4296 The strategy is to issue one store_by_pieces for each power of two,
4297 from most to least significant, guarded by a test on whether there
4298 are at least that many bytes left to copy in LEN.
4300 ??? Should we skip some powers of two in favor of loops? Maybe start
4301 at the max of TO/LEN/word alignment, at least when optimizing for
4302 size, instead of ensuring O(log len) dynamic compares? */
4304 bool
4305 try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
4306 unsigned HOST_WIDE_INT min_len,
4307 unsigned HOST_WIDE_INT max_len,
4308 rtx val, char valc, unsigned int align)
4310 int max_bits = floor_log2 (max_len);
4311 int min_bits = floor_log2 (min_len);
4312 int sctz_len = ctz_len;
4314 gcc_checking_assert (sctz_len >= 0);
4316 if (val)
4317 valc = 1;
4319 /* Bits more significant than TST_BITS are part of the shared prefix
4320 in the binary representation of both min_len and max_len. Since
4321 they're identical, we don't need to test them in the loop. */
4322 int tst_bits = (max_bits != min_bits ? max_bits
4323 : floor_log2 (max_len ^ min_len));
4325 /* Check whether it's profitable to start by storing a fixed BLKSIZE
4326 bytes, to lower max_bits. In the unlikely case of a constant LEN
4327 (implied by identical MAX_LEN and MIN_LEN), we want to issue a
4328 single store_by_pieces, but otherwise, select the minimum multiple
4329 of the ALIGN (in bytes) and of the MCD of the possible LENs, that
4330 brings MAX_LEN below TST_BITS, if that's lower than min_len. */
4331 unsigned HOST_WIDE_INT blksize;
4332 if (max_len > min_len)
4334 unsigned HOST_WIDE_INT alrng = MAX (HOST_WIDE_INT_1U << ctz_len,
4335 align / BITS_PER_UNIT);
4336 blksize = max_len - (HOST_WIDE_INT_1U << tst_bits) + alrng;
4337 blksize &= ~(alrng - 1);
4339 else if (max_len == min_len)
4340 blksize = max_len;
4341 else
4342 /* Huh, max_len < min_len? Punt. See pr100843.c. */
4343 return false;
4344 if (min_len >= blksize)
4346 min_len -= blksize;
4347 min_bits = floor_log2 (min_len);
4348 max_len -= blksize;
4349 max_bits = floor_log2 (max_len);
4351 tst_bits = (max_bits != min_bits ? max_bits
4352 : floor_log2 (max_len ^ min_len));
4354 else
4355 blksize = 0;
4357 /* Check that we can use store by pieces for the maximum store count
4358 we may issue (initial fixed-size block, plus conditional
4359 power-of-two-sized from max_bits to ctz_len. */
4360 unsigned HOST_WIDE_INT xlenest = blksize;
4361 if (max_bits >= 0)
4362 xlenest += ((HOST_WIDE_INT_1U << max_bits) * 2
4363 - (HOST_WIDE_INT_1U << ctz_len));
4364 if (!can_store_by_pieces (xlenest, builtin_memset_read_str,
4365 &valc, align, true))
4366 return false;
4368 by_pieces_constfn constfun;
4369 void *constfundata;
4370 if (val)
4372 constfun = builtin_memset_gen_str;
4373 constfundata = val = force_reg (TYPE_MODE (unsigned_char_type_node),
4374 val);
4376 else
4378 constfun = builtin_memset_read_str;
4379 constfundata = &valc;
4382 rtx ptr = copy_addr_to_reg (XEXP (to, 0));
4383 rtx rem = copy_to_mode_reg (ptr_mode, convert_to_mode (ptr_mode, len, 0));
4384 to = replace_equiv_address (to, ptr);
4385 set_mem_align (to, align);
4387 if (blksize)
4389 to = store_by_pieces (to, blksize,
4390 constfun, constfundata,
4391 align, true,
4392 max_len != 0 ? RETURN_END : RETURN_BEGIN);
4393 if (max_len == 0)
4394 return true;
4396 /* Adjust PTR, TO and REM. Since TO's address is likely
4397 PTR+offset, we have to replace it. */
4398 emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4399 to = replace_equiv_address (to, ptr);
4400 rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4401 emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4404 /* Iterate over power-of-two block sizes from the maximum length to
4405 the least significant bit possibly set in the length. */
4406 for (int i = max_bits; i >= sctz_len; i--)
4408 rtx_code_label *label = NULL;
4409 blksize = HOST_WIDE_INT_1U << i;
4411 /* If we're past the bits shared between min_ and max_len, expand
4412 a test on the dynamic length, comparing it with the
4413 BLKSIZE. */
4414 if (i <= tst_bits)
4416 label = gen_label_rtx ();
4417 emit_cmp_and_jump_insns (rem, GEN_INT (blksize), LT, NULL,
4418 ptr_mode, 1, label,
4419 profile_probability::even ());
4421 /* If we are at a bit that is in the prefix shared by min_ and
4422 max_len, skip this BLKSIZE if the bit is clear. */
4423 else if ((max_len & blksize) == 0)
4424 continue;
4426 /* Issue a store of BLKSIZE bytes. */
4427 to = store_by_pieces (to, blksize,
4428 constfun, constfundata,
4429 align, true,
4430 i != sctz_len ? RETURN_END : RETURN_BEGIN);
4432 /* Adjust REM and PTR, unless this is the last iteration. */
4433 if (i != sctz_len)
4435 emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4436 to = replace_equiv_address (to, ptr);
4437 rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4438 emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4441 if (label)
4443 emit_label (label);
4445 /* Given conditional stores, the offset can no longer be
4446 known, so clear it. */
4447 clear_mem_offset (to);
4451 return true;
4454 /* Helper function to do the actual work for expand_builtin_memset. The
4455 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4456 so that this can also be called without constructing an actual CALL_EXPR.
4457 The other arguments and return value are the same as for
4458 expand_builtin_memset. */
4460 static rtx
4461 expand_builtin_memset_args (tree dest, tree val, tree len,
4462 rtx target, machine_mode mode, tree orig_exp)
4464 tree fndecl, fn;
4465 enum built_in_function fcode;
4466 machine_mode val_mode;
4467 char c;
4468 unsigned int dest_align;
4469 rtx dest_mem, dest_addr, len_rtx;
4470 HOST_WIDE_INT expected_size = -1;
4471 unsigned int expected_align = 0;
4472 unsigned HOST_WIDE_INT min_size;
4473 unsigned HOST_WIDE_INT max_size;
4474 unsigned HOST_WIDE_INT probable_max_size;
4476 dest_align = get_pointer_alignment (dest);
4478 /* If DEST is not a pointer type, don't do this operation in-line. */
4479 if (dest_align == 0)
4480 return NULL_RTX;
4482 if (currently_expanding_gimple_stmt)
4483 stringop_block_profile (currently_expanding_gimple_stmt,
4484 &expected_align, &expected_size);
4486 if (expected_align < dest_align)
4487 expected_align = dest_align;
4489 /* If the LEN parameter is zero, return DEST. */
4490 if (integer_zerop (len))
4492 /* Evaluate and ignore VAL in case it has side-effects. */
4493 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4494 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4497 /* Stabilize the arguments in case we fail. */
4498 dest = builtin_save_expr (dest);
4499 val = builtin_save_expr (val);
4500 len = builtin_save_expr (len);
4502 len_rtx = expand_normal (len);
4503 determine_block_size (len, len_rtx, &min_size, &max_size,
4504 &probable_max_size);
4505 dest_mem = get_memory_rtx (dest, len);
4506 val_mode = TYPE_MODE (unsigned_char_type_node);
4508 if (TREE_CODE (val) != INTEGER_CST
4509 || target_char_cast (val, &c))
4511 rtx val_rtx;
4513 val_rtx = expand_normal (val);
4514 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4516 /* Assume that we can memset by pieces if we can store
4517 * the coefficients by pieces (in the required modes).
4518 * We can't pass builtin_memset_gen_str as that emits RTL. */
4519 c = 1;
4520 if (tree_fits_uhwi_p (len)
4521 && can_store_by_pieces (tree_to_uhwi (len),
4522 builtin_memset_read_str, &c, dest_align,
4523 true))
4525 val_rtx = force_reg (val_mode, val_rtx);
4526 store_by_pieces (dest_mem, tree_to_uhwi (len),
4527 builtin_memset_gen_str, val_rtx, dest_align,
4528 true, RETURN_BEGIN);
4530 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4531 dest_align, expected_align,
4532 expected_size, min_size, max_size,
4533 probable_max_size)
4534 && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4535 tree_ctz (len),
4536 min_size, max_size,
4537 val_rtx, 0,
4538 dest_align))
4539 goto do_libcall;
4541 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4542 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4543 return dest_mem;
4546 if (c)
4548 if (tree_fits_uhwi_p (len)
4549 && can_store_by_pieces (tree_to_uhwi (len),
4550 builtin_memset_read_str, &c, dest_align,
4551 true))
4552 store_by_pieces (dest_mem, tree_to_uhwi (len),
4553 builtin_memset_read_str, &c, dest_align, true,
4554 RETURN_BEGIN);
4555 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4556 gen_int_mode (c, val_mode),
4557 dest_align, expected_align,
4558 expected_size, min_size, max_size,
4559 probable_max_size)
4560 && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4561 tree_ctz (len),
4562 min_size, max_size,
4563 NULL_RTX, c,
4564 dest_align))
4565 goto do_libcall;
4567 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4568 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4569 return dest_mem;
4572 set_mem_align (dest_mem, dest_align);
4573 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4574 CALL_EXPR_TAILCALL (orig_exp)
4575 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4576 expected_align, expected_size,
4577 min_size, max_size,
4578 probable_max_size, tree_ctz (len));
4580 if (dest_addr == 0)
4582 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4583 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4586 return dest_addr;
4588 do_libcall:
4589 fndecl = get_callee_fndecl (orig_exp);
4590 fcode = DECL_FUNCTION_CODE (fndecl);
4591 if (fcode == BUILT_IN_MEMSET)
4592 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4593 dest, val, len);
4594 else if (fcode == BUILT_IN_BZERO)
4595 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4596 dest, len);
4597 else
4598 gcc_unreachable ();
4599 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4600 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4601 return expand_call (fn, target, target == const0_rtx);
4604 /* Expand expression EXP, which is a call to the bzero builtin. Return
4605 NULL_RTX if we failed the caller should emit a normal call. */
4607 static rtx
4608 expand_builtin_bzero (tree exp)
4610 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4611 return NULL_RTX;
4613 tree dest = CALL_EXPR_ARG (exp, 0);
4614 tree size = CALL_EXPR_ARG (exp, 1);
4616 /* New argument list transforming bzero(ptr x, int y) to
4617 memset(ptr x, int 0, size_t y). This is done this way
4618 so that if it isn't expanded inline, we fallback to
4619 calling bzero instead of memset. */
4621 location_t loc = EXPR_LOCATION (exp);
4623 return expand_builtin_memset_args (dest, integer_zero_node,
4624 fold_convert_loc (loc,
4625 size_type_node, size),
4626 const0_rtx, VOIDmode, exp);
4629 /* Try to expand cmpstr operation ICODE with the given operands.
4630 Return the result rtx on success, otherwise return null. */
4632 static rtx
4633 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4634 HOST_WIDE_INT align)
4636 machine_mode insn_mode = insn_data[icode].operand[0].mode;
4638 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4639 target = NULL_RTX;
4641 class expand_operand ops[4];
4642 create_output_operand (&ops[0], target, insn_mode);
4643 create_fixed_operand (&ops[1], arg1_rtx);
4644 create_fixed_operand (&ops[2], arg2_rtx);
4645 create_integer_operand (&ops[3], align);
4646 if (maybe_expand_insn (icode, 4, ops))
4647 return ops[0].value;
4648 return NULL_RTX;
4651 /* Expand expression EXP, which is a call to the memcmp built-in function.
4652 Return NULL_RTX if we failed and the caller should emit a normal call,
4653 otherwise try to get the result in TARGET, if convenient.
4654 RESULT_EQ is true if we can relax the returned value to be either zero
4655 or nonzero, without caring about the sign. */
4657 static rtx
4658 expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4660 if (!validate_arglist (exp,
4661 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4662 return NULL_RTX;
4664 tree arg1 = CALL_EXPR_ARG (exp, 0);
4665 tree arg2 = CALL_EXPR_ARG (exp, 1);
4666 tree len = CALL_EXPR_ARG (exp, 2);
4668 /* Due to the performance benefit, always inline the calls first
4669 when result_eq is false. */
4670 rtx result = NULL_RTX;
4671 enum built_in_function fcode = DECL_FUNCTION_CODE (get_callee_fndecl (exp));
4672 if (!result_eq && fcode != BUILT_IN_BCMP)
4674 result = inline_expand_builtin_bytecmp (exp, target);
4675 if (result)
4676 return result;
4679 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4680 location_t loc = EXPR_LOCATION (exp);
4682 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4683 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4685 /* If we don't have POINTER_TYPE, call the function. */
4686 if (arg1_align == 0 || arg2_align == 0)
4687 return NULL_RTX;
4689 rtx arg1_rtx = get_memory_rtx (arg1, len);
4690 rtx arg2_rtx = get_memory_rtx (arg2, len);
4691 rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4693 /* Set MEM_SIZE as appropriate. */
4694 if (CONST_INT_P (len_rtx))
4696 set_mem_size (arg1_rtx, INTVAL (len_rtx));
4697 set_mem_size (arg2_rtx, INTVAL (len_rtx));
4700 by_pieces_constfn constfn = NULL;
4702 /* Try to get the byte representation of the constant ARG2 (or, only
4703 when the function's result is used for equality to zero, ARG1)
4704 points to, with its byte size in NBYTES. */
4705 unsigned HOST_WIDE_INT nbytes;
4706 const char *rep = getbyterep (arg2, &nbytes);
4707 if (result_eq && rep == NULL)
4709 /* For equality to zero the arguments are interchangeable. */
4710 rep = getbyterep (arg1, &nbytes);
4711 if (rep != NULL)
4712 std::swap (arg1_rtx, arg2_rtx);
4715 /* If the function's constant bound LEN_RTX is less than or equal
4716 to the byte size of the representation of the constant argument,
4717 and if block move would be done by pieces, we can avoid loading
4718 the bytes from memory and only store the computed constant result. */
4719 if (rep
4720 && CONST_INT_P (len_rtx)
4721 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes)
4722 constfn = builtin_memcpy_read_str;
4724 result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4725 TREE_TYPE (len), target,
4726 result_eq, constfn,
4727 CONST_CAST (char *, rep));
4729 if (result)
4731 /* Return the value in the proper mode for this function. */
4732 if (GET_MODE (result) == mode)
4733 return result;
4735 if (target != 0)
4737 convert_move (target, result, 0);
4738 return target;
4741 return convert_to_mode (mode, result, 0);
4744 return NULL_RTX;
4747 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4748 if we failed the caller should emit a normal call, otherwise try to get
4749 the result in TARGET, if convenient. */
4751 static rtx
4752 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4754 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4755 return NULL_RTX;
4757 tree arg1 = CALL_EXPR_ARG (exp, 0);
4758 tree arg2 = CALL_EXPR_ARG (exp, 1);
4760 /* Due to the performance benefit, always inline the calls first. */
4761 rtx result = NULL_RTX;
4762 result = inline_expand_builtin_bytecmp (exp, target);
4763 if (result)
4764 return result;
4766 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4767 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4768 if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4769 return NULL_RTX;
4771 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4772 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4774 /* If we don't have POINTER_TYPE, call the function. */
4775 if (arg1_align == 0 || arg2_align == 0)
4776 return NULL_RTX;
4778 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4779 arg1 = builtin_save_expr (arg1);
4780 arg2 = builtin_save_expr (arg2);
4782 rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4783 rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4785 /* Try to call cmpstrsi. */
4786 if (cmpstr_icode != CODE_FOR_nothing)
4787 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4788 MIN (arg1_align, arg2_align));
4790 /* Try to determine at least one length and call cmpstrnsi. */
4791 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4793 tree len;
4794 rtx arg3_rtx;
4796 tree len1 = c_strlen (arg1, 1);
4797 tree len2 = c_strlen (arg2, 1);
4799 if (len1)
4800 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4801 if (len2)
4802 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4804 /* If we don't have a constant length for the first, use the length
4805 of the second, if we know it. We don't require a constant for
4806 this case; some cost analysis could be done if both are available
4807 but neither is constant. For now, assume they're equally cheap,
4808 unless one has side effects. If both strings have constant lengths,
4809 use the smaller. */
4811 if (!len1)
4812 len = len2;
4813 else if (!len2)
4814 len = len1;
4815 else if (TREE_SIDE_EFFECTS (len1))
4816 len = len2;
4817 else if (TREE_SIDE_EFFECTS (len2))
4818 len = len1;
4819 else if (TREE_CODE (len1) != INTEGER_CST)
4820 len = len2;
4821 else if (TREE_CODE (len2) != INTEGER_CST)
4822 len = len1;
4823 else if (tree_int_cst_lt (len1, len2))
4824 len = len1;
4825 else
4826 len = len2;
4828 /* If both arguments have side effects, we cannot optimize. */
4829 if (len && !TREE_SIDE_EFFECTS (len))
4831 arg3_rtx = expand_normal (len);
4832 result = expand_cmpstrn_or_cmpmem
4833 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4834 arg3_rtx, MIN (arg1_align, arg2_align));
4838 tree fndecl = get_callee_fndecl (exp);
4839 if (result)
4841 /* Return the value in the proper mode for this function. */
4842 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4843 if (GET_MODE (result) == mode)
4844 return result;
4845 if (target == 0)
4846 return convert_to_mode (mode, result, 0);
4847 convert_move (target, result, 0);
4848 return target;
4851 /* Expand the library call ourselves using a stabilized argument
4852 list to avoid re-evaluating the function's arguments twice. */
4853 tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4854 copy_warning (fn, exp);
4855 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4856 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4857 return expand_call (fn, target, target == const0_rtx);
4860 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4861 NULL_RTX if we failed the caller should emit a normal call, otherwise
4862 try to get the result in TARGET, if convenient. */
4864 static rtx
4865 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4866 ATTRIBUTE_UNUSED machine_mode mode)
4868 if (!validate_arglist (exp,
4869 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4870 return NULL_RTX;
4872 tree arg1 = CALL_EXPR_ARG (exp, 0);
4873 tree arg2 = CALL_EXPR_ARG (exp, 1);
4874 tree arg3 = CALL_EXPR_ARG (exp, 2);
4876 location_t loc = EXPR_LOCATION (exp);
4877 tree len1 = c_strlen (arg1, 1);
4878 tree len2 = c_strlen (arg2, 1);
4880 /* Due to the performance benefit, always inline the calls first. */
4881 rtx result = NULL_RTX;
4882 result = inline_expand_builtin_bytecmp (exp, target);
4883 if (result)
4884 return result;
4886 /* If c_strlen can determine an expression for one of the string
4887 lengths, and it doesn't have side effects, then emit cmpstrnsi
4888 using length MIN(strlen(string)+1, arg3). */
4889 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4890 if (cmpstrn_icode == CODE_FOR_nothing)
4891 return NULL_RTX;
4893 tree len;
4895 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4896 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4898 if (len1)
4899 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4900 if (len2)
4901 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4903 tree len3 = fold_convert_loc (loc, sizetype, arg3);
4905 /* If we don't have a constant length for the first, use the length
4906 of the second, if we know it. If neither string is constant length,
4907 use the given length argument. We don't require a constant for
4908 this case; some cost analysis could be done if both are available
4909 but neither is constant. For now, assume they're equally cheap,
4910 unless one has side effects. If both strings have constant lengths,
4911 use the smaller. */
4913 if (!len1 && !len2)
4914 len = len3;
4915 else if (!len1)
4916 len = len2;
4917 else if (!len2)
4918 len = len1;
4919 else if (TREE_SIDE_EFFECTS (len1))
4920 len = len2;
4921 else if (TREE_SIDE_EFFECTS (len2))
4922 len = len1;
4923 else if (TREE_CODE (len1) != INTEGER_CST)
4924 len = len2;
4925 else if (TREE_CODE (len2) != INTEGER_CST)
4926 len = len1;
4927 else if (tree_int_cst_lt (len1, len2))
4928 len = len1;
4929 else
4930 len = len2;
4932 /* If we are not using the given length, we must incorporate it here.
4933 The actual new length parameter will be MIN(len,arg3) in this case. */
4934 if (len != len3)
4936 len = fold_convert_loc (loc, sizetype, len);
4937 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
4939 rtx arg1_rtx = get_memory_rtx (arg1, len);
4940 rtx arg2_rtx = get_memory_rtx (arg2, len);
4941 rtx arg3_rtx = expand_normal (len);
4942 result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4943 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4944 MIN (arg1_align, arg2_align));
4946 tree fndecl = get_callee_fndecl (exp);
4947 if (result)
4949 /* Return the value in the proper mode for this function. */
4950 mode = TYPE_MODE (TREE_TYPE (exp));
4951 if (GET_MODE (result) == mode)
4952 return result;
4953 if (target == 0)
4954 return convert_to_mode (mode, result, 0);
4955 convert_move (target, result, 0);
4956 return target;
4959 /* Expand the library call ourselves using a stabilized argument
4960 list to avoid re-evaluating the function's arguments twice. */
4961 tree call = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
4962 copy_warning (call, exp);
4963 gcc_assert (TREE_CODE (call) == CALL_EXPR);
4964 CALL_EXPR_TAILCALL (call) = CALL_EXPR_TAILCALL (exp);
4965 return expand_call (call, target, target == const0_rtx);
4968 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4969 if that's convenient. */
4972 expand_builtin_saveregs (void)
4974 rtx val;
4975 rtx_insn *seq;
4977 /* Don't do __builtin_saveregs more than once in a function.
4978 Save the result of the first call and reuse it. */
4979 if (saveregs_value != 0)
4980 return saveregs_value;
4982 /* When this function is called, it means that registers must be
4983 saved on entry to this function. So we migrate the call to the
4984 first insn of this function. */
4986 start_sequence ();
4988 /* Do whatever the machine needs done in this case. */
4989 val = targetm.calls.expand_builtin_saveregs ();
4991 seq = get_insns ();
4992 end_sequence ();
4994 saveregs_value = val;
4996 /* Put the insns after the NOTE that starts the function. If this
4997 is inside a start_sequence, make the outer-level insn chain current, so
4998 the code is placed at the start of the function. */
4999 push_topmost_sequence ();
5000 emit_insn_after (seq, entry_of_function ());
5001 pop_topmost_sequence ();
5003 return val;
5006 /* Expand a call to __builtin_next_arg. */
5008 static rtx
5009 expand_builtin_next_arg (void)
5011 /* Checking arguments is already done in fold_builtin_next_arg
5012 that must be called before this function. */
5013 return expand_binop (ptr_mode, add_optab,
5014 crtl->args.internal_arg_pointer,
5015 crtl->args.arg_offset_rtx,
5016 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5019 /* Make it easier for the backends by protecting the valist argument
5020 from multiple evaluations. */
5022 static tree
5023 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
5025 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
5027 /* The current way of determining the type of valist is completely
5028 bogus. We should have the information on the va builtin instead. */
5029 if (!vatype)
5030 vatype = targetm.fn_abi_va_list (cfun->decl);
5032 if (TREE_CODE (vatype) == ARRAY_TYPE)
5034 if (TREE_SIDE_EFFECTS (valist))
5035 valist = save_expr (valist);
5037 /* For this case, the backends will be expecting a pointer to
5038 vatype, but it's possible we've actually been given an array
5039 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
5040 So fix it. */
5041 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
5043 tree p1 = build_pointer_type (TREE_TYPE (vatype));
5044 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
5047 else
5049 tree pt = build_pointer_type (vatype);
5051 if (! needs_lvalue)
5053 if (! TREE_SIDE_EFFECTS (valist))
5054 return valist;
5056 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
5057 TREE_SIDE_EFFECTS (valist) = 1;
5060 if (TREE_SIDE_EFFECTS (valist))
5061 valist = save_expr (valist);
5062 valist = fold_build2_loc (loc, MEM_REF,
5063 vatype, valist, build_int_cst (pt, 0));
5066 return valist;
5069 /* The "standard" definition of va_list is void*. */
5071 tree
5072 std_build_builtin_va_list (void)
5074 return ptr_type_node;
5077 /* The "standard" abi va_list is va_list_type_node. */
5079 tree
5080 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
5082 return va_list_type_node;
5085 /* The "standard" type of va_list is va_list_type_node. */
5087 tree
5088 std_canonical_va_list_type (tree type)
5090 tree wtype, htype;
5092 wtype = va_list_type_node;
5093 htype = type;
5095 if (TREE_CODE (wtype) == ARRAY_TYPE)
5097 /* If va_list is an array type, the argument may have decayed
5098 to a pointer type, e.g. by being passed to another function.
5099 In that case, unwrap both types so that we can compare the
5100 underlying records. */
5101 if (TREE_CODE (htype) == ARRAY_TYPE
5102 || POINTER_TYPE_P (htype))
5104 wtype = TREE_TYPE (wtype);
5105 htype = TREE_TYPE (htype);
5108 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
5109 return va_list_type_node;
5111 return NULL_TREE;
5114 /* The "standard" implementation of va_start: just assign `nextarg' to
5115 the variable. */
5117 void
5118 std_expand_builtin_va_start (tree valist, rtx nextarg)
5120 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
5121 convert_move (va_r, nextarg, 0);
5124 /* Expand EXP, a call to __builtin_va_start. */
5126 static rtx
5127 expand_builtin_va_start (tree exp)
5129 rtx nextarg;
5130 tree valist;
5131 location_t loc = EXPR_LOCATION (exp);
5133 if (call_expr_nargs (exp) < 2)
5135 error_at (loc, "too few arguments to function %<va_start%>");
5136 return const0_rtx;
5139 if (fold_builtin_next_arg (exp, true))
5140 return const0_rtx;
5142 nextarg = expand_builtin_next_arg ();
5143 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
5145 if (targetm.expand_builtin_va_start)
5146 targetm.expand_builtin_va_start (valist, nextarg);
5147 else
5148 std_expand_builtin_va_start (valist, nextarg);
5150 return const0_rtx;
5153 /* Expand EXP, a call to __builtin_va_end. */
5155 static rtx
5156 expand_builtin_va_end (tree exp)
5158 tree valist = CALL_EXPR_ARG (exp, 0);
5160 /* Evaluate for side effects, if needed. I hate macros that don't
5161 do that. */
5162 if (TREE_SIDE_EFFECTS (valist))
5163 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
5165 return const0_rtx;
5168 /* Expand EXP, a call to __builtin_va_copy. We do this as a
5169 builtin rather than just as an assignment in stdarg.h because of the
5170 nastiness of array-type va_list types. */
5172 static rtx
5173 expand_builtin_va_copy (tree exp)
5175 tree dst, src, t;
5176 location_t loc = EXPR_LOCATION (exp);
5178 dst = CALL_EXPR_ARG (exp, 0);
5179 src = CALL_EXPR_ARG (exp, 1);
5181 dst = stabilize_va_list_loc (loc, dst, 1);
5182 src = stabilize_va_list_loc (loc, src, 0);
5184 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
5186 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
5188 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
5189 TREE_SIDE_EFFECTS (t) = 1;
5190 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5192 else
5194 rtx dstb, srcb, size;
5196 /* Evaluate to pointers. */
5197 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
5198 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
5199 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
5200 NULL_RTX, VOIDmode, EXPAND_NORMAL);
5202 dstb = convert_memory_address (Pmode, dstb);
5203 srcb = convert_memory_address (Pmode, srcb);
5205 /* "Dereference" to BLKmode memories. */
5206 dstb = gen_rtx_MEM (BLKmode, dstb);
5207 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
5208 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5209 srcb = gen_rtx_MEM (BLKmode, srcb);
5210 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
5211 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
5213 /* Copy. */
5214 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
5217 return const0_rtx;
5220 /* Expand a call to one of the builtin functions __builtin_frame_address or
5221 __builtin_return_address. */
5223 static rtx
5224 expand_builtin_frame_address (tree fndecl, tree exp)
5226 /* The argument must be a nonnegative integer constant.
5227 It counts the number of frames to scan up the stack.
5228 The value is either the frame pointer value or the return
5229 address saved in that frame. */
5230 if (call_expr_nargs (exp) == 0)
5231 /* Warning about missing arg was already issued. */
5232 return const0_rtx;
5233 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
5235 error ("invalid argument to %qD", fndecl);
5236 return const0_rtx;
5238 else
5240 /* Number of frames to scan up the stack. */
5241 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
5243 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
5245 /* Some ports cannot access arbitrary stack frames. */
5246 if (tem == NULL)
5248 warning (0, "unsupported argument to %qD", fndecl);
5249 return const0_rtx;
5252 if (count)
5254 /* Warn since no effort is made to ensure that any frame
5255 beyond the current one exists or can be safely reached. */
5256 warning (OPT_Wframe_address, "calling %qD with "
5257 "a nonzero argument is unsafe", fndecl);
5260 /* For __builtin_frame_address, return what we've got. */
5261 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5262 return tem;
5264 if (!REG_P (tem)
5265 && ! CONSTANT_P (tem))
5266 tem = copy_addr_to_reg (tem);
5267 return tem;
5271 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5272 failed and the caller should emit a normal call. */
5274 static rtx
5275 expand_builtin_alloca (tree exp)
5277 rtx op0;
5278 rtx result;
5279 unsigned int align;
5280 tree fndecl = get_callee_fndecl (exp);
5281 HOST_WIDE_INT max_size;
5282 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5283 bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5284 bool valid_arglist
5285 = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5286 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5287 VOID_TYPE)
5288 : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5289 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5290 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5292 if (!valid_arglist)
5293 return NULL_RTX;
5295 /* Compute the argument. */
5296 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5298 /* Compute the alignment. */
5299 align = (fcode == BUILT_IN_ALLOCA
5300 ? BIGGEST_ALIGNMENT
5301 : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5303 /* Compute the maximum size. */
5304 max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5305 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2))
5306 : -1);
5308 /* Allocate the desired space. If the allocation stems from the declaration
5309 of a variable-sized object, it cannot accumulate. */
5310 result
5311 = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5312 result = convert_memory_address (ptr_mode, result);
5314 /* Dynamic allocations for variables are recorded during gimplification. */
5315 if (!alloca_for_var && (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC))
5316 record_dynamic_alloc (exp);
5318 return result;
5321 /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
5322 of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
5323 STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
5324 handle_builtin_stack_restore function. */
5326 static rtx
5327 expand_asan_emit_allocas_unpoison (tree exp)
5329 tree arg0 = CALL_EXPR_ARG (exp, 0);
5330 tree arg1 = CALL_EXPR_ARG (exp, 1);
5331 rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5332 rtx bot = expand_expr (arg1, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5333 rtx off = expand_simple_binop (Pmode, MINUS, virtual_stack_dynamic_rtx,
5334 stack_pointer_rtx, NULL_RTX, 0,
5335 OPTAB_LIB_WIDEN);
5336 off = convert_modes (ptr_mode, Pmode, off, 0);
5337 bot = expand_simple_binop (ptr_mode, PLUS, bot, off, NULL_RTX, 0,
5338 OPTAB_LIB_WIDEN);
5339 rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
5340 ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
5341 top, ptr_mode, bot, ptr_mode);
5342 return ret;
5345 /* Expand a call to bswap builtin in EXP.
5346 Return NULL_RTX if a normal call should be emitted rather than expanding the
5347 function in-line. If convenient, the result should be placed in TARGET.
5348 SUBTARGET may be used as the target for computing one of EXP's operands. */
5350 static rtx
5351 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5352 rtx subtarget)
5354 tree arg;
5355 rtx op0;
5357 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5358 return NULL_RTX;
5360 arg = CALL_EXPR_ARG (exp, 0);
5361 op0 = expand_expr (arg,
5362 subtarget && GET_MODE (subtarget) == target_mode
5363 ? subtarget : NULL_RTX,
5364 target_mode, EXPAND_NORMAL);
5365 if (GET_MODE (op0) != target_mode)
5366 op0 = convert_to_mode (target_mode, op0, 1);
5368 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5370 gcc_assert (target);
5372 return convert_to_mode (target_mode, target, 1);
5375 /* Expand a call to a unary builtin in EXP.
5376 Return NULL_RTX if a normal call should be emitted rather than expanding the
5377 function in-line. If convenient, the result should be placed in TARGET.
5378 SUBTARGET may be used as the target for computing one of EXP's operands. */
5380 static rtx
5381 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5382 rtx subtarget, optab op_optab)
5384 rtx op0;
5386 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5387 return NULL_RTX;
5389 /* Compute the argument. */
5390 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5391 (subtarget
5392 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5393 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5394 VOIDmode, EXPAND_NORMAL);
5395 /* Compute op, into TARGET if possible.
5396 Set TARGET to wherever the result comes back. */
5397 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5398 op_optab, op0, target, op_optab != clrsb_optab);
5399 gcc_assert (target);
5401 return convert_to_mode (target_mode, target, 0);
5404 /* Expand a call to __builtin_expect. We just return our argument
5405 as the builtin_expect semantic should've been already executed by
5406 tree branch prediction pass. */
5408 static rtx
5409 expand_builtin_expect (tree exp, rtx target)
5411 tree arg;
5413 if (call_expr_nargs (exp) < 2)
5414 return const0_rtx;
5415 arg = CALL_EXPR_ARG (exp, 0);
5417 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5418 /* When guessing was done, the hints should be already stripped away. */
5419 gcc_assert (!flag_guess_branch_prob
5420 || optimize == 0 || seen_error ());
5421 return target;
5424 /* Expand a call to __builtin_expect_with_probability. We just return our
5425 argument as the builtin_expect semantic should've been already executed by
5426 tree branch prediction pass. */
5428 static rtx
5429 expand_builtin_expect_with_probability (tree exp, rtx target)
5431 tree arg;
5433 if (call_expr_nargs (exp) < 3)
5434 return const0_rtx;
5435 arg = CALL_EXPR_ARG (exp, 0);
5437 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5438 /* When guessing was done, the hints should be already stripped away. */
5439 gcc_assert (!flag_guess_branch_prob
5440 || optimize == 0 || seen_error ());
5441 return target;
5445 /* Expand a call to __builtin_assume_aligned. We just return our first
5446 argument as the builtin_assume_aligned semantic should've been already
5447 executed by CCP. */
5449 static rtx
5450 expand_builtin_assume_aligned (tree exp, rtx target)
5452 if (call_expr_nargs (exp) < 2)
5453 return const0_rtx;
5454 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
5455 EXPAND_NORMAL);
5456 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
5457 && (call_expr_nargs (exp) < 3
5458 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
5459 return target;
5462 void
5463 expand_builtin_trap (void)
5465 if (targetm.have_trap ())
5467 rtx_insn *insn = emit_insn (targetm.gen_trap ());
5468 /* For trap insns when not accumulating outgoing args force
5469 REG_ARGS_SIZE note to prevent crossjumping of calls with
5470 different args sizes. */
5471 if (!ACCUMULATE_OUTGOING_ARGS)
5472 add_args_size_note (insn, stack_pointer_delta);
5474 else
5476 tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
5477 tree call_expr = build_call_expr (fn, 0);
5478 expand_call (call_expr, NULL_RTX, false);
5481 emit_barrier ();
5484 /* Expand a call to __builtin_unreachable. We do nothing except emit
5485 a barrier saying that control flow will not pass here.
5487 It is the responsibility of the program being compiled to ensure
5488 that control flow does never reach __builtin_unreachable. */
5489 static void
5490 expand_builtin_unreachable (void)
5492 /* Use gimple_build_builtin_unreachable or builtin_decl_unreachable
5493 to avoid this. */
5494 gcc_checking_assert (!sanitize_flags_p (SANITIZE_UNREACHABLE));
5495 emit_barrier ();
5498 /* Expand EXP, a call to fabs, fabsf or fabsl.
5499 Return NULL_RTX if a normal call should be emitted rather than expanding
5500 the function inline. If convenient, the result should be placed
5501 in TARGET. SUBTARGET may be used as the target for computing
5502 the operand. */
5504 static rtx
5505 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5507 machine_mode mode;
5508 tree arg;
5509 rtx op0;
5511 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5512 return NULL_RTX;
5514 arg = CALL_EXPR_ARG (exp, 0);
5515 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5516 mode = TYPE_MODE (TREE_TYPE (arg));
5517 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5518 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5521 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5522 Return NULL is a normal call should be emitted rather than expanding the
5523 function inline. If convenient, the result should be placed in TARGET.
5524 SUBTARGET may be used as the target for computing the operand. */
5526 static rtx
5527 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5529 rtx op0, op1;
5530 tree arg;
5532 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5533 return NULL_RTX;
5535 arg = CALL_EXPR_ARG (exp, 0);
5536 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5538 arg = CALL_EXPR_ARG (exp, 1);
5539 op1 = expand_normal (arg);
5541 return expand_copysign (op0, op1, target);
5544 /* Emit a call to __builtin___clear_cache. */
5546 void
5547 default_emit_call_builtin___clear_cache (rtx begin, rtx end)
5549 rtx callee = gen_rtx_SYMBOL_REF (Pmode,
5550 BUILTIN_ASM_NAME_PTR
5551 (BUILT_IN_CLEAR_CACHE));
5553 emit_library_call (callee,
5554 LCT_NORMAL, VOIDmode,
5555 convert_memory_address (ptr_mode, begin), ptr_mode,
5556 convert_memory_address (ptr_mode, end), ptr_mode);
5559 /* Emit a call to __builtin___clear_cache, unless the target specifies
5560 it as do-nothing. This function can be used by trampoline
5561 finalizers to duplicate the effects of expanding a call to the
5562 clear_cache builtin. */
5564 void
5565 maybe_emit_call_builtin___clear_cache (rtx begin, rtx end)
5567 gcc_assert ((GET_MODE (begin) == ptr_mode || GET_MODE (begin) == Pmode
5568 || CONST_INT_P (begin))
5569 && (GET_MODE (end) == ptr_mode || GET_MODE (end) == Pmode
5570 || CONST_INT_P (end)));
5572 if (targetm.have_clear_cache ())
5574 /* We have a "clear_cache" insn, and it will handle everything. */
5575 class expand_operand ops[2];
5577 create_address_operand (&ops[0], begin);
5578 create_address_operand (&ops[1], end);
5580 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
5581 return;
5583 else
5585 #ifndef CLEAR_INSN_CACHE
5586 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5587 does nothing. There is no need to call it. Do nothing. */
5588 return;
5589 #endif /* CLEAR_INSN_CACHE */
5592 targetm.calls.emit_call_builtin___clear_cache (begin, end);
5595 /* Expand a call to __builtin___clear_cache. */
5597 static void
5598 expand_builtin___clear_cache (tree exp)
5600 tree begin, end;
5601 rtx begin_rtx, end_rtx;
5603 /* We must not expand to a library call. If we did, any
5604 fallback library function in libgcc that might contain a call to
5605 __builtin___clear_cache() would recurse infinitely. */
5606 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5608 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5609 return;
5612 begin = CALL_EXPR_ARG (exp, 0);
5613 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5615 end = CALL_EXPR_ARG (exp, 1);
5616 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5618 maybe_emit_call_builtin___clear_cache (begin_rtx, end_rtx);
5621 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5623 static rtx
5624 round_trampoline_addr (rtx tramp)
5626 rtx temp, addend, mask;
5628 /* If we don't need too much alignment, we'll have been guaranteed
5629 proper alignment by get_trampoline_type. */
5630 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5631 return tramp;
5633 /* Round address up to desired boundary. */
5634 temp = gen_reg_rtx (Pmode);
5635 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
5636 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
5638 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5639 temp, 0, OPTAB_LIB_WIDEN);
5640 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5641 temp, 0, OPTAB_LIB_WIDEN);
5643 return tramp;
5646 static rtx
5647 expand_builtin_init_trampoline (tree exp, bool onstack)
5649 tree t_tramp, t_func, t_chain;
5650 rtx m_tramp, r_tramp, r_chain, tmp;
5652 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5653 POINTER_TYPE, VOID_TYPE))
5654 return NULL_RTX;
5656 t_tramp = CALL_EXPR_ARG (exp, 0);
5657 t_func = CALL_EXPR_ARG (exp, 1);
5658 t_chain = CALL_EXPR_ARG (exp, 2);
5660 r_tramp = expand_normal (t_tramp);
5661 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5662 MEM_NOTRAP_P (m_tramp) = 1;
5664 /* If ONSTACK, the TRAMP argument should be the address of a field
5665 within the local function's FRAME decl. Either way, let's see if
5666 we can fill in the MEM_ATTRs for this memory. */
5667 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5668 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
5670 /* Creator of a heap trampoline is responsible for making sure the
5671 address is aligned to at least STACK_BOUNDARY. Normally malloc
5672 will ensure this anyhow. */
5673 tmp = round_trampoline_addr (r_tramp);
5674 if (tmp != r_tramp)
5676 m_tramp = change_address (m_tramp, BLKmode, tmp);
5677 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5678 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
5681 /* The FUNC argument should be the address of the nested function.
5682 Extract the actual function decl to pass to the hook. */
5683 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5684 t_func = TREE_OPERAND (t_func, 0);
5685 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5687 r_chain = expand_normal (t_chain);
5689 /* Generate insns to initialize the trampoline. */
5690 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5692 if (onstack)
5694 trampolines_created = 1;
5696 if (targetm.calls.custom_function_descriptors != 0)
5697 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5698 "trampoline generated for nested function %qD", t_func);
5701 return const0_rtx;
5704 static rtx
5705 expand_builtin_adjust_trampoline (tree exp)
5707 rtx tramp;
5709 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5710 return NULL_RTX;
5712 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5713 tramp = round_trampoline_addr (tramp);
5714 if (targetm.calls.trampoline_adjust_address)
5715 tramp = targetm.calls.trampoline_adjust_address (tramp);
5717 return tramp;
5720 /* Expand a call to the builtin descriptor initialization routine.
5721 A descriptor is made up of a couple of pointers to the static
5722 chain and the code entry in this order. */
5724 static rtx
5725 expand_builtin_init_descriptor (tree exp)
5727 tree t_descr, t_func, t_chain;
5728 rtx m_descr, r_descr, r_func, r_chain;
5730 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
5731 VOID_TYPE))
5732 return NULL_RTX;
5734 t_descr = CALL_EXPR_ARG (exp, 0);
5735 t_func = CALL_EXPR_ARG (exp, 1);
5736 t_chain = CALL_EXPR_ARG (exp, 2);
5738 r_descr = expand_normal (t_descr);
5739 m_descr = gen_rtx_MEM (BLKmode, r_descr);
5740 MEM_NOTRAP_P (m_descr) = 1;
5741 set_mem_align (m_descr, GET_MODE_ALIGNMENT (ptr_mode));
5743 r_func = expand_normal (t_func);
5744 r_chain = expand_normal (t_chain);
5746 /* Generate insns to initialize the descriptor. */
5747 emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
5748 emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
5749 POINTER_SIZE / BITS_PER_UNIT), r_func);
5751 return const0_rtx;
5754 /* Expand a call to the builtin descriptor adjustment routine. */
5756 static rtx
5757 expand_builtin_adjust_descriptor (tree exp)
5759 rtx tramp;
5761 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5762 return NULL_RTX;
5764 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5766 /* Unalign the descriptor to allow runtime identification. */
5767 tramp = plus_constant (ptr_mode, tramp,
5768 targetm.calls.custom_function_descriptors);
5770 return force_operand (tramp, NULL_RTX);
5773 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5774 function. The function first checks whether the back end provides
5775 an insn to implement signbit for the respective mode. If not, it
5776 checks whether the floating point format of the value is such that
5777 the sign bit can be extracted. If that is not the case, error out.
5778 EXP is the expression that is a call to the builtin function; if
5779 convenient, the result should be placed in TARGET. */
5780 static rtx
5781 expand_builtin_signbit (tree exp, rtx target)
5783 const struct real_format *fmt;
5784 scalar_float_mode fmode;
5785 scalar_int_mode rmode, imode;
5786 tree arg;
5787 int word, bitpos;
5788 enum insn_code icode;
5789 rtx temp;
5790 location_t loc = EXPR_LOCATION (exp);
5792 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5793 return NULL_RTX;
5795 arg = CALL_EXPR_ARG (exp, 0);
5796 fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
5797 rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5798 fmt = REAL_MODE_FORMAT (fmode);
5800 arg = builtin_save_expr (arg);
5802 /* Expand the argument yielding a RTX expression. */
5803 temp = expand_normal (arg);
5805 /* Check if the back end provides an insn that handles signbit for the
5806 argument's mode. */
5807 icode = optab_handler (signbit_optab, fmode);
5808 if (icode != CODE_FOR_nothing)
5810 rtx_insn *last = get_last_insn ();
5811 rtx this_target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5812 if (maybe_emit_unop_insn (icode, this_target, temp, UNKNOWN))
5813 return this_target;
5814 delete_insns_since (last);
5817 /* For floating point formats without a sign bit, implement signbit
5818 as "ARG < 0.0". */
5819 bitpos = fmt->signbit_ro;
5820 if (bitpos < 0)
5822 /* But we can't do this if the format supports signed zero. */
5823 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
5825 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5826 build_real (TREE_TYPE (arg), dconst0));
5827 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5830 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5832 imode = int_mode_for_mode (fmode).require ();
5833 temp = gen_lowpart (imode, temp);
5835 else
5837 imode = word_mode;
5838 /* Handle targets with different FP word orders. */
5839 if (FLOAT_WORDS_BIG_ENDIAN)
5840 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5841 else
5842 word = bitpos / BITS_PER_WORD;
5843 temp = operand_subword_force (temp, word, fmode);
5844 bitpos = bitpos % BITS_PER_WORD;
5847 /* Force the intermediate word_mode (or narrower) result into a
5848 register. This avoids attempting to create paradoxical SUBREGs
5849 of floating point modes below. */
5850 temp = force_reg (imode, temp);
5852 /* If the bitpos is within the "result mode" lowpart, the operation
5853 can be implement with a single bitwise AND. Otherwise, we need
5854 a right shift and an AND. */
5856 if (bitpos < GET_MODE_BITSIZE (rmode))
5858 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5860 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5861 temp = gen_lowpart (rmode, temp);
5862 temp = expand_binop (rmode, and_optab, temp,
5863 immed_wide_int_const (mask, rmode),
5864 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5866 else
5868 /* Perform a logical right shift to place the signbit in the least
5869 significant bit, then truncate the result to the desired mode
5870 and mask just this bit. */
5871 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5872 temp = gen_lowpart (rmode, temp);
5873 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5874 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5877 return temp;
5880 /* Expand fork or exec calls. TARGET is the desired target of the
5881 call. EXP is the call. FN is the
5882 identificator of the actual function. IGNORE is nonzero if the
5883 value is to be ignored. */
5885 static rtx
5886 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5888 tree id, decl;
5889 tree call;
5891 /* If we are not profiling, just call the function. */
5892 if (!profile_arc_flag)
5893 return NULL_RTX;
5895 /* Otherwise call the wrapper. This should be equivalent for the rest of
5896 compiler, so the code does not diverge, and the wrapper may run the
5897 code necessary for keeping the profiling sane. */
5899 switch (DECL_FUNCTION_CODE (fn))
5901 case BUILT_IN_FORK:
5902 id = get_identifier ("__gcov_fork");
5903 break;
5905 case BUILT_IN_EXECL:
5906 id = get_identifier ("__gcov_execl");
5907 break;
5909 case BUILT_IN_EXECV:
5910 id = get_identifier ("__gcov_execv");
5911 break;
5913 case BUILT_IN_EXECLP:
5914 id = get_identifier ("__gcov_execlp");
5915 break;
5917 case BUILT_IN_EXECLE:
5918 id = get_identifier ("__gcov_execle");
5919 break;
5921 case BUILT_IN_EXECVP:
5922 id = get_identifier ("__gcov_execvp");
5923 break;
5925 case BUILT_IN_EXECVE:
5926 id = get_identifier ("__gcov_execve");
5927 break;
5929 default:
5930 gcc_unreachable ();
5933 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5934 FUNCTION_DECL, id, TREE_TYPE (fn));
5935 DECL_EXTERNAL (decl) = 1;
5936 TREE_PUBLIC (decl) = 1;
5937 DECL_ARTIFICIAL (decl) = 1;
5938 TREE_NOTHROW (decl) = 1;
5939 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5940 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5941 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5942 return expand_call (call, target, ignore);
5947 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5948 the pointer in these functions is void*, the tree optimizers may remove
5949 casts. The mode computed in expand_builtin isn't reliable either, due
5950 to __sync_bool_compare_and_swap.
5952 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5953 group of builtins. This gives us log2 of the mode size. */
5955 static inline machine_mode
5956 get_builtin_sync_mode (int fcode_diff)
5958 /* The size is not negotiable, so ask not to get BLKmode in return
5959 if the target indicates that a smaller size would be better. */
5960 return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require ();
5963 /* Expand the memory expression LOC and return the appropriate memory operand
5964 for the builtin_sync operations. */
5966 static rtx
5967 get_builtin_sync_mem (tree loc, machine_mode mode)
5969 rtx addr, mem;
5970 int addr_space = TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc))
5971 ? TREE_TYPE (TREE_TYPE (loc))
5972 : TREE_TYPE (loc));
5973 scalar_int_mode addr_mode = targetm.addr_space.address_mode (addr_space);
5975 addr = expand_expr (loc, NULL_RTX, addr_mode, EXPAND_SUM);
5976 addr = convert_memory_address (addr_mode, addr);
5978 /* Note that we explicitly do not want any alias information for this
5979 memory, so that we kill all other live memories. Otherwise we don't
5980 satisfy the full barrier semantics of the intrinsic. */
5981 mem = gen_rtx_MEM (mode, addr);
5983 set_mem_addr_space (mem, addr_space);
5985 mem = validize_mem (mem);
5987 /* The alignment needs to be at least according to that of the mode. */
5988 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5989 get_pointer_alignment (loc)));
5990 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5991 MEM_VOLATILE_P (mem) = 1;
5993 return mem;
5996 /* Make sure an argument is in the right mode.
5997 EXP is the tree argument.
5998 MODE is the mode it should be in. */
6000 static rtx
6001 expand_expr_force_mode (tree exp, machine_mode mode)
6003 rtx val;
6004 machine_mode old_mode;
6006 if (TREE_CODE (exp) == SSA_NAME
6007 && TYPE_MODE (TREE_TYPE (exp)) != mode)
6009 /* Undo argument promotion if possible, as combine might not
6010 be able to do it later due to MEM_VOLATILE_P uses in the
6011 patterns. */
6012 gimple *g = get_gimple_for_ssa_name (exp);
6013 if (g && gimple_assign_cast_p (g))
6015 tree rhs = gimple_assign_rhs1 (g);
6016 tree_code code = gimple_assign_rhs_code (g);
6017 if (CONVERT_EXPR_CODE_P (code)
6018 && TYPE_MODE (TREE_TYPE (rhs)) == mode
6019 && INTEGRAL_TYPE_P (TREE_TYPE (exp))
6020 && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
6021 && (TYPE_PRECISION (TREE_TYPE (exp))
6022 > TYPE_PRECISION (TREE_TYPE (rhs))))
6023 exp = rhs;
6027 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
6028 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
6029 of CONST_INTs, where we know the old_mode only from the call argument. */
6031 old_mode = GET_MODE (val);
6032 if (old_mode == VOIDmode)
6033 old_mode = TYPE_MODE (TREE_TYPE (exp));
6034 val = convert_modes (mode, old_mode, val, 1);
6035 return val;
6039 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
6040 EXP is the CALL_EXPR. CODE is the rtx code
6041 that corresponds to the arithmetic or logical operation from the name;
6042 an exception here is that NOT actually means NAND. TARGET is an optional
6043 place for us to store the results; AFTER is true if this is the
6044 fetch_and_xxx form. */
6046 static rtx
6047 expand_builtin_sync_operation (machine_mode mode, tree exp,
6048 enum rtx_code code, bool after,
6049 rtx target)
6051 rtx val, mem;
6052 location_t loc = EXPR_LOCATION (exp);
6054 if (code == NOT && warn_sync_nand)
6056 tree fndecl = get_callee_fndecl (exp);
6057 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6059 static bool warned_f_a_n, warned_n_a_f;
6061 switch (fcode)
6063 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
6064 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
6065 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
6066 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
6067 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
6068 if (warned_f_a_n)
6069 break;
6071 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
6072 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6073 warned_f_a_n = true;
6074 break;
6076 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
6077 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
6078 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
6079 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
6080 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
6081 if (warned_n_a_f)
6082 break;
6084 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
6085 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
6086 warned_n_a_f = true;
6087 break;
6089 default:
6090 gcc_unreachable ();
6094 /* Expand the operands. */
6095 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6096 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6098 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
6099 after);
6102 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
6103 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
6104 true if this is the boolean form. TARGET is a place for us to store the
6105 results; this is NOT optional if IS_BOOL is true. */
6107 static rtx
6108 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
6109 bool is_bool, rtx target)
6111 rtx old_val, new_val, mem;
6112 rtx *pbool, *poval;
6114 /* Expand the operands. */
6115 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6116 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6117 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6119 pbool = poval = NULL;
6120 if (target != const0_rtx)
6122 if (is_bool)
6123 pbool = &target;
6124 else
6125 poval = &target;
6127 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
6128 false, MEMMODEL_SYNC_SEQ_CST,
6129 MEMMODEL_SYNC_SEQ_CST))
6130 return NULL_RTX;
6132 return target;
6135 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
6136 general form is actually an atomic exchange, and some targets only
6137 support a reduced form with the second argument being a constant 1.
6138 EXP is the CALL_EXPR; TARGET is an optional place for us to store
6139 the results. */
6141 static rtx
6142 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
6143 rtx target)
6145 rtx val, mem;
6147 /* Expand the operands. */
6148 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6149 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6151 return expand_sync_lock_test_and_set (target, mem, val);
6154 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
6156 static void
6157 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
6159 rtx mem;
6161 /* Expand the operands. */
6162 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6164 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
6167 /* Given an integer representing an ``enum memmodel'', verify its
6168 correctness and return the memory model enum. */
6170 static enum memmodel
6171 get_memmodel (tree exp)
6173 /* If the parameter is not a constant, it's a run time value so we'll just
6174 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
6175 if (TREE_CODE (exp) != INTEGER_CST)
6176 return MEMMODEL_SEQ_CST;
6178 rtx op = expand_normal (exp);
6180 unsigned HOST_WIDE_INT val = INTVAL (op);
6181 if (targetm.memmodel_check)
6182 val = targetm.memmodel_check (val);
6183 else if (val & ~MEMMODEL_MASK)
6184 return MEMMODEL_SEQ_CST;
6186 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
6187 if (memmodel_base (val) >= MEMMODEL_LAST)
6188 return MEMMODEL_SEQ_CST;
6190 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
6191 be conservative and promote consume to acquire. */
6192 if (val == MEMMODEL_CONSUME)
6193 val = MEMMODEL_ACQUIRE;
6195 return (enum memmodel) val;
6198 /* Expand the __atomic_exchange intrinsic:
6199 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
6200 EXP is the CALL_EXPR.
6201 TARGET is an optional place for us to store the results. */
6203 static rtx
6204 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
6206 rtx val, mem;
6207 enum memmodel model;
6209 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6211 if (!flag_inline_atomics)
6212 return NULL_RTX;
6214 /* Expand the operands. */
6215 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6216 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6218 return expand_atomic_exchange (target, mem, val, model);
6221 /* Expand the __atomic_compare_exchange intrinsic:
6222 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
6223 TYPE desired, BOOL weak,
6224 enum memmodel success,
6225 enum memmodel failure)
6226 EXP is the CALL_EXPR.
6227 TARGET is an optional place for us to store the results. */
6229 static rtx
6230 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
6231 rtx target)
6233 rtx expect, desired, mem, oldval;
6234 rtx_code_label *label;
6235 tree weak;
6236 bool is_weak;
6238 memmodel success = get_memmodel (CALL_EXPR_ARG (exp, 4));
6239 memmodel failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
6241 if (failure > success)
6242 success = MEMMODEL_SEQ_CST;
6244 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6246 failure = MEMMODEL_SEQ_CST;
6247 success = MEMMODEL_SEQ_CST;
6251 if (!flag_inline_atomics)
6252 return NULL_RTX;
6254 /* Expand the operands. */
6255 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6257 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
6258 expect = convert_memory_address (Pmode, expect);
6259 expect = gen_rtx_MEM (mode, expect);
6260 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
6262 weak = CALL_EXPR_ARG (exp, 3);
6263 is_weak = false;
6264 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
6265 is_weak = true;
6267 if (target == const0_rtx)
6268 target = NULL;
6270 /* Lest the rtl backend create a race condition with an imporoper store
6271 to memory, always create a new pseudo for OLDVAL. */
6272 oldval = NULL;
6274 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
6275 is_weak, success, failure))
6276 return NULL_RTX;
6278 /* Conditionally store back to EXPECT, lest we create a race condition
6279 with an improper store to memory. */
6280 /* ??? With a rearrangement of atomics at the gimple level, we can handle
6281 the normal case where EXPECT is totally private, i.e. a register. At
6282 which point the store can be unconditional. */
6283 label = gen_label_rtx ();
6284 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
6285 GET_MODE (target), 1, label);
6286 emit_move_insn (expect, oldval);
6287 emit_label (label);
6289 return target;
6292 /* Helper function for expand_ifn_atomic_compare_exchange - expand
6293 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
6294 call. The weak parameter must be dropped to match the expected parameter
6295 list and the expected argument changed from value to pointer to memory
6296 slot. */
6298 static void
6299 expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
6301 unsigned int z;
6302 vec<tree, va_gc> *vec;
6304 vec_alloc (vec, 5);
6305 vec->quick_push (gimple_call_arg (call, 0));
6306 tree expected = gimple_call_arg (call, 1);
6307 rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
6308 TREE_TYPE (expected));
6309 rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
6310 if (expd != x)
6311 emit_move_insn (x, expd);
6312 tree v = make_tree (TREE_TYPE (expected), x);
6313 vec->quick_push (build1 (ADDR_EXPR,
6314 build_pointer_type (TREE_TYPE (expected)), v));
6315 vec->quick_push (gimple_call_arg (call, 2));
6316 /* Skip the boolean weak parameter. */
6317 for (z = 4; z < 6; z++)
6318 vec->quick_push (gimple_call_arg (call, z));
6319 /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6320 unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
6321 gcc_assert (bytes_log2 < 5);
6322 built_in_function fncode
6323 = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6324 + bytes_log2);
6325 tree fndecl = builtin_decl_explicit (fncode);
6326 tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
6327 fndecl);
6328 tree exp = build_call_vec (boolean_type_node, fn, vec);
6329 tree lhs = gimple_call_lhs (call);
6330 rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
6331 if (lhs)
6333 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6334 if (GET_MODE (boolret) != mode)
6335 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6336 x = force_reg (mode, x);
6337 write_complex_part (target, boolret, true, true);
6338 write_complex_part (target, x, false, false);
6342 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6344 void
6345 expand_ifn_atomic_compare_exchange (gcall *call)
6347 int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6348 gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6349 machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6351 memmodel success = get_memmodel (gimple_call_arg (call, 4));
6352 memmodel failure = get_memmodel (gimple_call_arg (call, 5));
6354 if (failure > success)
6355 success = MEMMODEL_SEQ_CST;
6357 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6359 failure = MEMMODEL_SEQ_CST;
6360 success = MEMMODEL_SEQ_CST;
6363 if (!flag_inline_atomics)
6365 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6366 return;
6369 /* Expand the operands. */
6370 rtx mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6372 rtx expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6373 rtx desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6375 bool is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6377 rtx boolret = NULL;
6378 rtx oldval = NULL;
6380 if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
6381 is_weak, success, failure))
6383 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6384 return;
6387 tree lhs = gimple_call_lhs (call);
6388 if (lhs)
6390 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6391 if (GET_MODE (boolret) != mode)
6392 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6393 write_complex_part (target, boolret, true, true);
6394 write_complex_part (target, oldval, false, false);
6398 /* Expand the __atomic_load intrinsic:
6399 TYPE __atomic_load (TYPE *object, enum memmodel)
6400 EXP is the CALL_EXPR.
6401 TARGET is an optional place for us to store the results. */
6403 static rtx
6404 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6406 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6407 if (is_mm_release (model) || is_mm_acq_rel (model))
6408 model = MEMMODEL_SEQ_CST;
6410 if (!flag_inline_atomics)
6411 return NULL_RTX;
6413 /* Expand the operand. */
6414 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6416 return expand_atomic_load (target, mem, model);
6420 /* Expand the __atomic_store intrinsic:
6421 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6422 EXP is the CALL_EXPR.
6423 TARGET is an optional place for us to store the results. */
6425 static rtx
6426 expand_builtin_atomic_store (machine_mode mode, tree exp)
6428 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6429 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6430 || is_mm_release (model)))
6431 model = MEMMODEL_SEQ_CST;
6433 if (!flag_inline_atomics)
6434 return NULL_RTX;
6436 /* Expand the operands. */
6437 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6438 rtx val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6440 return expand_atomic_store (mem, val, model, false);
6443 /* Expand the __atomic_fetch_XXX intrinsic:
6444 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6445 EXP is the CALL_EXPR.
6446 TARGET is an optional place for us to store the results.
6447 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6448 FETCH_AFTER is true if returning the result of the operation.
6449 FETCH_AFTER is false if returning the value before the operation.
6450 IGNORE is true if the result is not used.
6451 EXT_CALL is the correct builtin for an external call if this cannot be
6452 resolved to an instruction sequence. */
6454 static rtx
6455 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
6456 enum rtx_code code, bool fetch_after,
6457 bool ignore, enum built_in_function ext_call)
6459 rtx val, mem, ret;
6460 enum memmodel model;
6461 tree fndecl;
6462 tree addr;
6464 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6466 /* Expand the operands. */
6467 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6468 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6470 /* Only try generating instructions if inlining is turned on. */
6471 if (flag_inline_atomics)
6473 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6474 if (ret)
6475 return ret;
6478 /* Return if a different routine isn't needed for the library call. */
6479 if (ext_call == BUILT_IN_NONE)
6480 return NULL_RTX;
6482 /* Change the call to the specified function. */
6483 fndecl = get_callee_fndecl (exp);
6484 addr = CALL_EXPR_FN (exp);
6485 STRIP_NOPS (addr);
6487 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
6488 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
6490 /* If we will emit code after the call, the call cannot be a tail call.
6491 If it is emitted as a tail call, a barrier is emitted after it, and
6492 then all trailing code is removed. */
6493 if (!ignore)
6494 CALL_EXPR_TAILCALL (exp) = 0;
6496 /* Expand the call here so we can emit trailing code. */
6497 ret = expand_call (exp, target, ignore);
6499 /* Replace the original function just in case it matters. */
6500 TREE_OPERAND (addr, 0) = fndecl;
6502 /* Then issue the arithmetic correction to return the right result. */
6503 if (!ignore)
6505 if (code == NOT)
6507 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
6508 OPTAB_LIB_WIDEN);
6509 ret = expand_simple_unop (mode, NOT, ret, target, true);
6511 else
6512 ret = expand_simple_binop (mode, code, ret, val, target, true,
6513 OPTAB_LIB_WIDEN);
6515 return ret;
6518 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6520 void
6521 expand_ifn_atomic_bit_test_and (gcall *call)
6523 tree ptr = gimple_call_arg (call, 0);
6524 tree bit = gimple_call_arg (call, 1);
6525 tree flag = gimple_call_arg (call, 2);
6526 tree lhs = gimple_call_lhs (call);
6527 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6528 machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
6529 enum rtx_code code;
6530 optab optab;
6531 class expand_operand ops[5];
6533 gcc_assert (flag_inline_atomics);
6535 if (gimple_call_num_args (call) == 5)
6536 model = get_memmodel (gimple_call_arg (call, 3));
6538 rtx mem = get_builtin_sync_mem (ptr, mode);
6539 rtx val = expand_expr_force_mode (bit, mode);
6541 switch (gimple_call_internal_fn (call))
6543 case IFN_ATOMIC_BIT_TEST_AND_SET:
6544 code = IOR;
6545 optab = atomic_bit_test_and_set_optab;
6546 break;
6547 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
6548 code = XOR;
6549 optab = atomic_bit_test_and_complement_optab;
6550 break;
6551 case IFN_ATOMIC_BIT_TEST_AND_RESET:
6552 code = AND;
6553 optab = atomic_bit_test_and_reset_optab;
6554 break;
6555 default:
6556 gcc_unreachable ();
6559 if (lhs == NULL_TREE)
6561 rtx val2 = expand_simple_binop (mode, ASHIFT, const1_rtx,
6562 val, NULL_RTX, true, OPTAB_DIRECT);
6563 if (code == AND)
6564 val2 = expand_simple_unop (mode, NOT, val2, NULL_RTX, true);
6565 if (expand_atomic_fetch_op (const0_rtx, mem, val2, code, model, false))
6566 return;
6569 rtx target;
6570 if (lhs)
6571 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6572 else
6573 target = gen_reg_rtx (mode);
6574 enum insn_code icode = direct_optab_handler (optab, mode);
6575 gcc_assert (icode != CODE_FOR_nothing);
6576 create_output_operand (&ops[0], target, mode);
6577 create_fixed_operand (&ops[1], mem);
6578 create_convert_operand_to (&ops[2], val, mode, true);
6579 create_integer_operand (&ops[3], model);
6580 create_integer_operand (&ops[4], integer_onep (flag));
6581 if (maybe_expand_insn (icode, 5, ops))
6582 return;
6584 rtx bitval = val;
6585 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6586 val, NULL_RTX, true, OPTAB_DIRECT);
6587 rtx maskval = val;
6588 if (code == AND)
6589 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6590 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
6591 code, model, false);
6592 if (!result)
6594 bool is_atomic = gimple_call_num_args (call) == 5;
6595 tree tcall = gimple_call_arg (call, 3 + is_atomic);
6596 tree fndecl = gimple_call_addr_fndecl (tcall);
6597 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6598 tree exp = build_call_nary (type, tcall, 2 + is_atomic, ptr,
6599 make_tree (type, val),
6600 is_atomic
6601 ? gimple_call_arg (call, 3)
6602 : integer_zero_node);
6603 result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
6604 mode, !lhs);
6606 if (!lhs)
6607 return;
6608 if (integer_onep (flag))
6610 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
6611 NULL_RTX, true, OPTAB_DIRECT);
6612 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
6613 true, OPTAB_DIRECT);
6615 else
6616 result = expand_simple_binop (mode, AND, result, maskval, target, true,
6617 OPTAB_DIRECT);
6618 if (result != target)
6619 emit_move_insn (target, result);
6622 /* Expand IFN_ATOMIC_*_FETCH_CMP_0 internal function. */
6624 void
6625 expand_ifn_atomic_op_fetch_cmp_0 (gcall *call)
6627 tree cmp = gimple_call_arg (call, 0);
6628 tree ptr = gimple_call_arg (call, 1);
6629 tree arg = gimple_call_arg (call, 2);
6630 tree lhs = gimple_call_lhs (call);
6631 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6632 machine_mode mode = TYPE_MODE (TREE_TYPE (cmp));
6633 optab optab;
6634 rtx_code code;
6635 class expand_operand ops[5];
6637 gcc_assert (flag_inline_atomics);
6639 if (gimple_call_num_args (call) == 5)
6640 model = get_memmodel (gimple_call_arg (call, 3));
6642 rtx mem = get_builtin_sync_mem (ptr, mode);
6643 rtx op = expand_expr_force_mode (arg, mode);
6645 switch (gimple_call_internal_fn (call))
6647 case IFN_ATOMIC_ADD_FETCH_CMP_0:
6648 code = PLUS;
6649 optab = atomic_add_fetch_cmp_0_optab;
6650 break;
6651 case IFN_ATOMIC_SUB_FETCH_CMP_0:
6652 code = MINUS;
6653 optab = atomic_sub_fetch_cmp_0_optab;
6654 break;
6655 case IFN_ATOMIC_AND_FETCH_CMP_0:
6656 code = AND;
6657 optab = atomic_and_fetch_cmp_0_optab;
6658 break;
6659 case IFN_ATOMIC_OR_FETCH_CMP_0:
6660 code = IOR;
6661 optab = atomic_or_fetch_cmp_0_optab;
6662 break;
6663 case IFN_ATOMIC_XOR_FETCH_CMP_0:
6664 code = XOR;
6665 optab = atomic_xor_fetch_cmp_0_optab;
6666 break;
6667 default:
6668 gcc_unreachable ();
6671 enum rtx_code comp = UNKNOWN;
6672 switch (tree_to_uhwi (cmp))
6674 case ATOMIC_OP_FETCH_CMP_0_EQ: comp = EQ; break;
6675 case ATOMIC_OP_FETCH_CMP_0_NE: comp = NE; break;
6676 case ATOMIC_OP_FETCH_CMP_0_GT: comp = GT; break;
6677 case ATOMIC_OP_FETCH_CMP_0_GE: comp = GE; break;
6678 case ATOMIC_OP_FETCH_CMP_0_LT: comp = LT; break;
6679 case ATOMIC_OP_FETCH_CMP_0_LE: comp = LE; break;
6680 default: gcc_unreachable ();
6683 rtx target;
6684 if (lhs == NULL_TREE)
6685 target = gen_reg_rtx (TYPE_MODE (boolean_type_node));
6686 else
6687 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6688 enum insn_code icode = direct_optab_handler (optab, mode);
6689 gcc_assert (icode != CODE_FOR_nothing);
6690 create_output_operand (&ops[0], target, TYPE_MODE (boolean_type_node));
6691 create_fixed_operand (&ops[1], mem);
6692 create_convert_operand_to (&ops[2], op, mode, true);
6693 create_integer_operand (&ops[3], model);
6694 create_integer_operand (&ops[4], comp);
6695 if (maybe_expand_insn (icode, 5, ops))
6696 return;
6698 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, op,
6699 code, model, true);
6700 if (!result)
6702 bool is_atomic = gimple_call_num_args (call) == 5;
6703 tree tcall = gimple_call_arg (call, 3 + is_atomic);
6704 tree fndecl = gimple_call_addr_fndecl (tcall);
6705 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6706 tree exp = build_call_nary (type, tcall,
6707 2 + is_atomic, ptr, arg,
6708 is_atomic
6709 ? gimple_call_arg (call, 3)
6710 : integer_zero_node);
6711 result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
6712 mode, !lhs);
6715 if (lhs)
6717 result = emit_store_flag_force (target, comp, result, const0_rtx, mode,
6718 0, 1);
6719 if (result != target)
6720 emit_move_insn (target, result);
6724 /* Expand an atomic clear operation.
6725 void _atomic_clear (BOOL *obj, enum memmodel)
6726 EXP is the call expression. */
6728 static rtx
6729 expand_builtin_atomic_clear (tree exp)
6731 machine_mode mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6732 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6733 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6735 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
6736 model = MEMMODEL_SEQ_CST;
6738 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6739 Failing that, a store is issued by __atomic_store. The only way this can
6740 fail is if the bool type is larger than a word size. Unlikely, but
6741 handle it anyway for completeness. Assume a single threaded model since
6742 there is no atomic support in this case, and no barriers are required. */
6743 rtx ret = expand_atomic_store (mem, const0_rtx, model, true);
6744 if (!ret)
6745 emit_move_insn (mem, const0_rtx);
6746 return const0_rtx;
6749 /* Expand an atomic test_and_set operation.
6750 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6751 EXP is the call expression. */
6753 static rtx
6754 expand_builtin_atomic_test_and_set (tree exp, rtx target)
6756 rtx mem;
6757 enum memmodel model;
6758 machine_mode mode;
6760 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6761 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6762 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6764 return expand_atomic_test_and_set (target, mem, model);
6768 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6769 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6771 static tree
6772 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
6774 int size;
6775 machine_mode mode;
6776 unsigned int mode_align, type_align;
6778 if (TREE_CODE (arg0) != INTEGER_CST)
6779 return NULL_TREE;
6781 /* We need a corresponding integer mode for the access to be lock-free. */
6782 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
6783 if (!int_mode_for_size (size, 0).exists (&mode))
6784 return boolean_false_node;
6786 mode_align = GET_MODE_ALIGNMENT (mode);
6788 if (TREE_CODE (arg1) == INTEGER_CST)
6790 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
6792 /* Either this argument is null, or it's a fake pointer encoding
6793 the alignment of the object. */
6794 val = least_bit_hwi (val);
6795 val *= BITS_PER_UNIT;
6797 if (val == 0 || mode_align < val)
6798 type_align = mode_align;
6799 else
6800 type_align = val;
6802 else
6804 tree ttype = TREE_TYPE (arg1);
6806 /* This function is usually invoked and folded immediately by the front
6807 end before anything else has a chance to look at it. The pointer
6808 parameter at this point is usually cast to a void *, so check for that
6809 and look past the cast. */
6810 if (CONVERT_EXPR_P (arg1)
6811 && POINTER_TYPE_P (ttype)
6812 && VOID_TYPE_P (TREE_TYPE (ttype))
6813 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
6814 arg1 = TREE_OPERAND (arg1, 0);
6816 ttype = TREE_TYPE (arg1);
6817 gcc_assert (POINTER_TYPE_P (ttype));
6819 /* Get the underlying type of the object. */
6820 ttype = TREE_TYPE (ttype);
6821 type_align = TYPE_ALIGN (ttype);
6824 /* If the object has smaller alignment, the lock free routines cannot
6825 be used. */
6826 if (type_align < mode_align)
6827 return boolean_false_node;
6829 /* Check if a compare_and_swap pattern exists for the mode which represents
6830 the required size. The pattern is not allowed to fail, so the existence
6831 of the pattern indicates support is present. Also require that an
6832 atomic load exists for the required size. */
6833 if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
6834 return boolean_true_node;
6835 else
6836 return boolean_false_node;
6839 /* Return true if the parameters to call EXP represent an object which will
6840 always generate lock free instructions. The first argument represents the
6841 size of the object, and the second parameter is a pointer to the object
6842 itself. If NULL is passed for the object, then the result is based on
6843 typical alignment for an object of the specified size. Otherwise return
6844 false. */
6846 static rtx
6847 expand_builtin_atomic_always_lock_free (tree exp)
6849 tree size;
6850 tree arg0 = CALL_EXPR_ARG (exp, 0);
6851 tree arg1 = CALL_EXPR_ARG (exp, 1);
6853 if (TREE_CODE (arg0) != INTEGER_CST)
6855 error ("non-constant argument 1 to %qs", "__atomic_always_lock_free");
6856 return const0_rtx;
6859 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
6860 if (size == boolean_true_node)
6861 return const1_rtx;
6862 return const0_rtx;
6865 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6866 is lock free on this architecture. */
6868 static tree
6869 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
6871 if (!flag_inline_atomics)
6872 return NULL_TREE;
6874 /* If it isn't always lock free, don't generate a result. */
6875 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
6876 return boolean_true_node;
6878 return NULL_TREE;
6881 /* Return true if the parameters to call EXP represent an object which will
6882 always generate lock free instructions. The first argument represents the
6883 size of the object, and the second parameter is a pointer to the object
6884 itself. If NULL is passed for the object, then the result is based on
6885 typical alignment for an object of the specified size. Otherwise return
6886 NULL*/
6888 static rtx
6889 expand_builtin_atomic_is_lock_free (tree exp)
6891 tree size;
6892 tree arg0 = CALL_EXPR_ARG (exp, 0);
6893 tree arg1 = CALL_EXPR_ARG (exp, 1);
6895 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6897 error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
6898 return NULL_RTX;
6901 if (!flag_inline_atomics)
6902 return NULL_RTX;
6904 /* If the value is known at compile time, return the RTX for it. */
6905 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
6906 if (size == boolean_true_node)
6907 return const1_rtx;
6909 return NULL_RTX;
6912 /* Expand the __atomic_thread_fence intrinsic:
6913 void __atomic_thread_fence (enum memmodel)
6914 EXP is the CALL_EXPR. */
6916 static void
6917 expand_builtin_atomic_thread_fence (tree exp)
6919 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6920 expand_mem_thread_fence (model);
6923 /* Expand the __atomic_signal_fence intrinsic:
6924 void __atomic_signal_fence (enum memmodel)
6925 EXP is the CALL_EXPR. */
6927 static void
6928 expand_builtin_atomic_signal_fence (tree exp)
6930 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6931 expand_mem_signal_fence (model);
6934 /* Expand the __sync_synchronize intrinsic. */
6936 static void
6937 expand_builtin_sync_synchronize (void)
6939 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
6942 static rtx
6943 expand_builtin_thread_pointer (tree exp, rtx target)
6945 enum insn_code icode;
6946 if (!validate_arglist (exp, VOID_TYPE))
6947 return const0_rtx;
6948 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
6949 if (icode != CODE_FOR_nothing)
6951 class expand_operand op;
6952 /* If the target is not sutitable then create a new target. */
6953 if (target == NULL_RTX
6954 || !REG_P (target)
6955 || GET_MODE (target) != Pmode)
6956 target = gen_reg_rtx (Pmode);
6957 create_output_operand (&op, target, Pmode);
6958 expand_insn (icode, 1, &op);
6959 return target;
6961 error ("%<__builtin_thread_pointer%> is not supported on this target");
6962 return const0_rtx;
6965 static void
6966 expand_builtin_set_thread_pointer (tree exp)
6968 enum insn_code icode;
6969 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6970 return;
6971 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
6972 if (icode != CODE_FOR_nothing)
6974 class expand_operand op;
6975 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
6976 Pmode, EXPAND_NORMAL);
6977 create_input_operand (&op, val, Pmode);
6978 expand_insn (icode, 1, &op);
6979 return;
6981 error ("%<__builtin_set_thread_pointer%> is not supported on this target");
6985 /* Emit code to restore the current value of stack. */
6987 static void
6988 expand_stack_restore (tree var)
6990 rtx_insn *prev;
6991 rtx sa = expand_normal (var);
6993 sa = convert_memory_address (Pmode, sa);
6995 prev = get_last_insn ();
6996 emit_stack_restore (SAVE_BLOCK, sa);
6998 record_new_stack_level ();
7000 fixup_args_size_notes (prev, get_last_insn (), 0);
7003 /* Emit code to save the current value of stack. */
7005 static rtx
7006 expand_stack_save (void)
7008 rtx ret = NULL_RTX;
7010 emit_stack_save (SAVE_BLOCK, &ret);
7011 return ret;
7014 /* Emit code to get the openacc gang, worker or vector id or size. */
7016 static rtx
7017 expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
7019 const char *name;
7020 rtx fallback_retval;
7021 rtx_insn *(*gen_fn) (rtx, rtx);
7022 switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
7024 case BUILT_IN_GOACC_PARLEVEL_ID:
7025 name = "__builtin_goacc_parlevel_id";
7026 fallback_retval = const0_rtx;
7027 gen_fn = targetm.gen_oacc_dim_pos;
7028 break;
7029 case BUILT_IN_GOACC_PARLEVEL_SIZE:
7030 name = "__builtin_goacc_parlevel_size";
7031 fallback_retval = const1_rtx;
7032 gen_fn = targetm.gen_oacc_dim_size;
7033 break;
7034 default:
7035 gcc_unreachable ();
7038 if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
7040 error ("%qs only supported in OpenACC code", name);
7041 return const0_rtx;
7044 tree arg = CALL_EXPR_ARG (exp, 0);
7045 if (TREE_CODE (arg) != INTEGER_CST)
7047 error ("non-constant argument 0 to %qs", name);
7048 return const0_rtx;
7051 int dim = TREE_INT_CST_LOW (arg);
7052 switch (dim)
7054 case GOMP_DIM_GANG:
7055 case GOMP_DIM_WORKER:
7056 case GOMP_DIM_VECTOR:
7057 break;
7058 default:
7059 error ("illegal argument 0 to %qs", name);
7060 return const0_rtx;
7063 if (ignore)
7064 return target;
7066 if (target == NULL_RTX)
7067 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
7069 if (!targetm.have_oacc_dim_size ())
7071 emit_move_insn (target, fallback_retval);
7072 return target;
7075 rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
7076 emit_insn (gen_fn (reg, GEN_INT (dim)));
7077 if (reg != target)
7078 emit_move_insn (target, reg);
7080 return target;
7083 /* Expand a string compare operation using a sequence of char comparison
7084 to get rid of the calling overhead, with result going to TARGET if
7085 that's convenient.
7087 VAR_STR is the variable string source;
7088 CONST_STR is the constant string source;
7089 LENGTH is the number of chars to compare;
7090 CONST_STR_N indicates which source string is the constant string;
7091 IS_MEMCMP indicates whether it's a memcmp or strcmp.
7093 to: (assume const_str_n is 2, i.e., arg2 is a constant string)
7095 target = (int) (unsigned char) var_str[0]
7096 - (int) (unsigned char) const_str[0];
7097 if (target != 0)
7098 goto ne_label;
7100 target = (int) (unsigned char) var_str[length - 2]
7101 - (int) (unsigned char) const_str[length - 2];
7102 if (target != 0)
7103 goto ne_label;
7104 target = (int) (unsigned char) var_str[length - 1]
7105 - (int) (unsigned char) const_str[length - 1];
7106 ne_label:
7109 static rtx
7110 inline_string_cmp (rtx target, tree var_str, const char *const_str,
7111 unsigned HOST_WIDE_INT length,
7112 int const_str_n, machine_mode mode)
7114 HOST_WIDE_INT offset = 0;
7115 rtx var_rtx_array
7116 = get_memory_rtx (var_str, build_int_cst (unsigned_type_node,length));
7117 rtx var_rtx = NULL_RTX;
7118 rtx const_rtx = NULL_RTX;
7119 rtx result = target ? target : gen_reg_rtx (mode);
7120 rtx_code_label *ne_label = gen_label_rtx ();
7121 tree unit_type_node = unsigned_char_type_node;
7122 scalar_int_mode unit_mode
7123 = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
7125 start_sequence ();
7127 for (unsigned HOST_WIDE_INT i = 0; i < length; i++)
7129 var_rtx
7130 = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset);
7131 const_rtx = c_readstr (const_str + offset, unit_mode);
7132 rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx;
7133 rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
7135 op0 = convert_modes (mode, unit_mode, op0, 1);
7136 op1 = convert_modes (mode, unit_mode, op1, 1);
7137 result = expand_simple_binop (mode, MINUS, op0, op1,
7138 result, 1, OPTAB_WIDEN);
7139 if (i < length - 1)
7140 emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
7141 mode, true, ne_label);
7142 offset += GET_MODE_SIZE (unit_mode);
7145 emit_label (ne_label);
7146 rtx_insn *insns = get_insns ();
7147 end_sequence ();
7148 emit_insn (insns);
7150 return result;
7153 /* Inline expansion of a call to str(n)cmp and memcmp, with result going
7154 to TARGET if that's convenient.
7155 If the call is not been inlined, return NULL_RTX. */
7157 static rtx
7158 inline_expand_builtin_bytecmp (tree exp, rtx target)
7160 tree fndecl = get_callee_fndecl (exp);
7161 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7162 bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP);
7164 /* Do NOT apply this inlining expansion when optimizing for size or
7165 optimization level below 2. */
7166 if (optimize < 2 || optimize_insn_for_size_p ())
7167 return NULL_RTX;
7169 gcc_checking_assert (fcode == BUILT_IN_STRCMP
7170 || fcode == BUILT_IN_STRNCMP
7171 || fcode == BUILT_IN_MEMCMP);
7173 /* On a target where the type of the call (int) has same or narrower presicion
7174 than unsigned char, give up the inlining expansion. */
7175 if (TYPE_PRECISION (unsigned_char_type_node)
7176 >= TYPE_PRECISION (TREE_TYPE (exp)))
7177 return NULL_RTX;
7179 tree arg1 = CALL_EXPR_ARG (exp, 0);
7180 tree arg2 = CALL_EXPR_ARG (exp, 1);
7181 tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
7183 unsigned HOST_WIDE_INT len1 = 0;
7184 unsigned HOST_WIDE_INT len2 = 0;
7185 unsigned HOST_WIDE_INT len3 = 0;
7187 /* Get the object representation of the initializers of ARG1 and ARG2
7188 as strings, provided they refer to constant objects, with their byte
7189 sizes in LEN1 and LEN2, respectively. */
7190 const char *bytes1 = getbyterep (arg1, &len1);
7191 const char *bytes2 = getbyterep (arg2, &len2);
7193 /* Fail if neither argument refers to an initialized constant. */
7194 if (!bytes1 && !bytes2)
7195 return NULL_RTX;
7197 if (is_ncmp)
7199 /* Fail if the memcmp/strncmp bound is not a constant. */
7200 if (!tree_fits_uhwi_p (len3_tree))
7201 return NULL_RTX;
7203 len3 = tree_to_uhwi (len3_tree);
7205 if (fcode == BUILT_IN_MEMCMP)
7207 /* Fail if the memcmp bound is greater than the size of either
7208 of the two constant objects. */
7209 if ((bytes1 && len1 < len3)
7210 || (bytes2 && len2 < len3))
7211 return NULL_RTX;
7215 if (fcode != BUILT_IN_MEMCMP)
7217 /* For string functions (i.e., strcmp and strncmp) reduce LEN1
7218 and LEN2 to the length of the nul-terminated string stored
7219 in each. */
7220 if (bytes1 != NULL)
7221 len1 = strnlen (bytes1, len1) + 1;
7222 if (bytes2 != NULL)
7223 len2 = strnlen (bytes2, len2) + 1;
7226 /* See inline_string_cmp. */
7227 int const_str_n;
7228 if (!len1)
7229 const_str_n = 2;
7230 else if (!len2)
7231 const_str_n = 1;
7232 else if (len2 > len1)
7233 const_str_n = 1;
7234 else
7235 const_str_n = 2;
7237 /* For strncmp only, compute the new bound as the smallest of
7238 the lengths of the two strings (plus 1) and the bound provided
7239 to the function. */
7240 unsigned HOST_WIDE_INT bound = (const_str_n == 1) ? len1 : len2;
7241 if (is_ncmp && len3 < bound)
7242 bound = len3;
7244 /* If the bound of the comparison is larger than the threshold,
7245 do nothing. */
7246 if (bound > (unsigned HOST_WIDE_INT) param_builtin_string_cmp_inline_length)
7247 return NULL_RTX;
7249 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
7251 /* Now, start inline expansion the call. */
7252 return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
7253 (const_str_n == 1) ? bytes1 : bytes2, bound,
7254 const_str_n, mode);
7257 /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
7258 represents the size of the first argument to that call, or VOIDmode
7259 if the argument is a pointer. IGNORE will be true if the result
7260 isn't used. */
7261 static rtx
7262 expand_speculation_safe_value (machine_mode mode, tree exp, rtx target,
7263 bool ignore)
7265 rtx val, failsafe;
7266 unsigned nargs = call_expr_nargs (exp);
7268 tree arg0 = CALL_EXPR_ARG (exp, 0);
7270 if (mode == VOIDmode)
7272 mode = TYPE_MODE (TREE_TYPE (arg0));
7273 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
7276 val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
7278 /* An optional second argument can be used as a failsafe value on
7279 some machines. If it isn't present, then the failsafe value is
7280 assumed to be 0. */
7281 if (nargs > 1)
7283 tree arg1 = CALL_EXPR_ARG (exp, 1);
7284 failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL);
7286 else
7287 failsafe = const0_rtx;
7289 /* If the result isn't used, the behavior is undefined. It would be
7290 nice to emit a warning here, but path splitting means this might
7291 happen with legitimate code. So simply drop the builtin
7292 expansion in that case; we've handled any side-effects above. */
7293 if (ignore)
7294 return const0_rtx;
7296 /* If we don't have a suitable target, create one to hold the result. */
7297 if (target == NULL || GET_MODE (target) != mode)
7298 target = gen_reg_rtx (mode);
7300 if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode)
7301 val = convert_modes (mode, VOIDmode, val, false);
7303 return targetm.speculation_safe_value (mode, target, val, failsafe);
7306 /* Expand an expression EXP that calls a built-in function,
7307 with result going to TARGET if that's convenient
7308 (and in mode MODE if that's convenient).
7309 SUBTARGET may be used as the target for computing one of EXP's operands.
7310 IGNORE is nonzero if the value is to be ignored. */
7313 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
7314 int ignore)
7316 tree fndecl = get_callee_fndecl (exp);
7317 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
7318 int flags;
7320 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7321 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7323 /* When ASan is enabled, we don't want to expand some memory/string
7324 builtins and rely on libsanitizer's hooks. This allows us to avoid
7325 redundant checks and be sure, that possible overflow will be detected
7326 by ASan. */
7328 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7329 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
7330 return expand_call (exp, target, ignore);
7332 /* When not optimizing, generate calls to library functions for a certain
7333 set of builtins. */
7334 if (!optimize
7335 && !called_as_built_in (fndecl)
7336 && fcode != BUILT_IN_FORK
7337 && fcode != BUILT_IN_EXECL
7338 && fcode != BUILT_IN_EXECV
7339 && fcode != BUILT_IN_EXECLP
7340 && fcode != BUILT_IN_EXECLE
7341 && fcode != BUILT_IN_EXECVP
7342 && fcode != BUILT_IN_EXECVE
7343 && fcode != BUILT_IN_CLEAR_CACHE
7344 && !ALLOCA_FUNCTION_CODE_P (fcode)
7345 && fcode != BUILT_IN_FREE)
7346 return expand_call (exp, target, ignore);
7348 /* The built-in function expanders test for target == const0_rtx
7349 to determine whether the function's result will be ignored. */
7350 if (ignore)
7351 target = const0_rtx;
7353 /* If the result of a pure or const built-in function is ignored, and
7354 none of its arguments are volatile, we can avoid expanding the
7355 built-in call and just evaluate the arguments for side-effects. */
7356 if (target == const0_rtx
7357 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
7358 && !(flags & ECF_LOOPING_CONST_OR_PURE))
7360 bool volatilep = false;
7361 tree arg;
7362 call_expr_arg_iterator iter;
7364 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7365 if (TREE_THIS_VOLATILE (arg))
7367 volatilep = true;
7368 break;
7371 if (! volatilep)
7373 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7374 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
7375 return const0_rtx;
7379 switch (fcode)
7381 CASE_FLT_FN (BUILT_IN_FABS):
7382 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
7383 case BUILT_IN_FABSD32:
7384 case BUILT_IN_FABSD64:
7385 case BUILT_IN_FABSD128:
7386 target = expand_builtin_fabs (exp, target, subtarget);
7387 if (target)
7388 return target;
7389 break;
7391 CASE_FLT_FN (BUILT_IN_COPYSIGN):
7392 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
7393 target = expand_builtin_copysign (exp, target, subtarget);
7394 if (target)
7395 return target;
7396 break;
7398 /* Just do a normal library call if we were unable to fold
7399 the values. */
7400 CASE_FLT_FN (BUILT_IN_CABS):
7401 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CABS):
7402 break;
7404 CASE_FLT_FN (BUILT_IN_FMA):
7405 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
7406 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
7407 if (target)
7408 return target;
7409 break;
7411 CASE_FLT_FN (BUILT_IN_ILOGB):
7412 if (! flag_unsafe_math_optimizations)
7413 break;
7414 gcc_fallthrough ();
7415 CASE_FLT_FN (BUILT_IN_ISINF):
7416 CASE_FLT_FN (BUILT_IN_FINITE):
7417 case BUILT_IN_ISFINITE:
7418 case BUILT_IN_ISNORMAL:
7419 target = expand_builtin_interclass_mathfn (exp, target);
7420 if (target)
7421 return target;
7422 break;
7424 case BUILT_IN_ISSIGNALING:
7425 target = expand_builtin_issignaling (exp, target);
7426 if (target)
7427 return target;
7428 break;
7430 CASE_FLT_FN (BUILT_IN_ICEIL):
7431 CASE_FLT_FN (BUILT_IN_LCEIL):
7432 CASE_FLT_FN (BUILT_IN_LLCEIL):
7433 CASE_FLT_FN (BUILT_IN_LFLOOR):
7434 CASE_FLT_FN (BUILT_IN_IFLOOR):
7435 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7436 target = expand_builtin_int_roundingfn (exp, target);
7437 if (target)
7438 return target;
7439 break;
7441 CASE_FLT_FN (BUILT_IN_IRINT):
7442 CASE_FLT_FN (BUILT_IN_LRINT):
7443 CASE_FLT_FN (BUILT_IN_LLRINT):
7444 CASE_FLT_FN (BUILT_IN_IROUND):
7445 CASE_FLT_FN (BUILT_IN_LROUND):
7446 CASE_FLT_FN (BUILT_IN_LLROUND):
7447 target = expand_builtin_int_roundingfn_2 (exp, target);
7448 if (target)
7449 return target;
7450 break;
7452 CASE_FLT_FN (BUILT_IN_POWI):
7453 target = expand_builtin_powi (exp, target);
7454 if (target)
7455 return target;
7456 break;
7458 CASE_FLT_FN (BUILT_IN_CEXPI):
7459 target = expand_builtin_cexpi (exp, target);
7460 gcc_assert (target);
7461 return target;
7463 CASE_FLT_FN (BUILT_IN_SIN):
7464 CASE_FLT_FN (BUILT_IN_COS):
7465 if (! flag_unsafe_math_optimizations)
7466 break;
7467 target = expand_builtin_mathfn_3 (exp, target, subtarget);
7468 if (target)
7469 return target;
7470 break;
7472 CASE_FLT_FN (BUILT_IN_SINCOS):
7473 if (! flag_unsafe_math_optimizations)
7474 break;
7475 target = expand_builtin_sincos (exp);
7476 if (target)
7477 return target;
7478 break;
7480 case BUILT_IN_FEGETROUND:
7481 target = expand_builtin_fegetround (exp, target, target_mode);
7482 if (target)
7483 return target;
7484 break;
7486 case BUILT_IN_FECLEAREXCEPT:
7487 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
7488 feclearexcept_optab);
7489 if (target)
7490 return target;
7491 break;
7493 case BUILT_IN_FERAISEEXCEPT:
7494 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
7495 feraiseexcept_optab);
7496 if (target)
7497 return target;
7498 break;
7500 case BUILT_IN_APPLY_ARGS:
7501 return expand_builtin_apply_args ();
7503 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
7504 FUNCTION with a copy of the parameters described by
7505 ARGUMENTS, and ARGSIZE. It returns a block of memory
7506 allocated on the stack into which is stored all the registers
7507 that might possibly be used for returning the result of a
7508 function. ARGUMENTS is the value returned by
7509 __builtin_apply_args. ARGSIZE is the number of bytes of
7510 arguments that must be copied. ??? How should this value be
7511 computed? We'll also need a safe worst case value for varargs
7512 functions. */
7513 case BUILT_IN_APPLY:
7514 if (!validate_arglist (exp, POINTER_TYPE,
7515 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
7516 && !validate_arglist (exp, REFERENCE_TYPE,
7517 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7518 return const0_rtx;
7519 else
7521 rtx ops[3];
7523 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
7524 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
7525 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
7527 return expand_builtin_apply (ops[0], ops[1], ops[2]);
7530 /* __builtin_return (RESULT) causes the function to return the
7531 value described by RESULT. RESULT is address of the block of
7532 memory returned by __builtin_apply. */
7533 case BUILT_IN_RETURN:
7534 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7535 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
7536 return const0_rtx;
7538 case BUILT_IN_SAVEREGS:
7539 return expand_builtin_saveregs ();
7541 case BUILT_IN_VA_ARG_PACK:
7542 /* All valid uses of __builtin_va_arg_pack () are removed during
7543 inlining. */
7544 error ("invalid use of %<__builtin_va_arg_pack ()%>");
7545 return const0_rtx;
7547 case BUILT_IN_VA_ARG_PACK_LEN:
7548 /* All valid uses of __builtin_va_arg_pack_len () are removed during
7549 inlining. */
7550 error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
7551 return const0_rtx;
7553 /* Return the address of the first anonymous stack arg. */
7554 case BUILT_IN_NEXT_ARG:
7555 if (fold_builtin_next_arg (exp, false))
7556 return const0_rtx;
7557 return expand_builtin_next_arg ();
7559 case BUILT_IN_CLEAR_CACHE:
7560 expand_builtin___clear_cache (exp);
7561 return const0_rtx;
7563 case BUILT_IN_CLASSIFY_TYPE:
7564 return expand_builtin_classify_type (exp);
7566 case BUILT_IN_CONSTANT_P:
7567 return const0_rtx;
7569 case BUILT_IN_FRAME_ADDRESS:
7570 case BUILT_IN_RETURN_ADDRESS:
7571 return expand_builtin_frame_address (fndecl, exp);
7573 /* Returns the address of the area where the structure is returned.
7574 0 otherwise. */
7575 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
7576 if (call_expr_nargs (exp) != 0
7577 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
7578 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
7579 return const0_rtx;
7580 else
7581 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
7583 CASE_BUILT_IN_ALLOCA:
7584 target = expand_builtin_alloca (exp);
7585 if (target)
7586 return target;
7587 break;
7589 case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
7590 return expand_asan_emit_allocas_unpoison (exp);
7592 case BUILT_IN_STACK_SAVE:
7593 return expand_stack_save ();
7595 case BUILT_IN_STACK_RESTORE:
7596 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
7597 return const0_rtx;
7599 case BUILT_IN_BSWAP16:
7600 case BUILT_IN_BSWAP32:
7601 case BUILT_IN_BSWAP64:
7602 case BUILT_IN_BSWAP128:
7603 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
7604 if (target)
7605 return target;
7606 break;
7608 CASE_INT_FN (BUILT_IN_FFS):
7609 target = expand_builtin_unop (target_mode, exp, target,
7610 subtarget, ffs_optab);
7611 if (target)
7612 return target;
7613 break;
7615 CASE_INT_FN (BUILT_IN_CLZ):
7616 target = expand_builtin_unop (target_mode, exp, target,
7617 subtarget, clz_optab);
7618 if (target)
7619 return target;
7620 break;
7622 CASE_INT_FN (BUILT_IN_CTZ):
7623 target = expand_builtin_unop (target_mode, exp, target,
7624 subtarget, ctz_optab);
7625 if (target)
7626 return target;
7627 break;
7629 CASE_INT_FN (BUILT_IN_CLRSB):
7630 target = expand_builtin_unop (target_mode, exp, target,
7631 subtarget, clrsb_optab);
7632 if (target)
7633 return target;
7634 break;
7636 CASE_INT_FN (BUILT_IN_POPCOUNT):
7637 target = expand_builtin_unop (target_mode, exp, target,
7638 subtarget, popcount_optab);
7639 if (target)
7640 return target;
7641 break;
7643 CASE_INT_FN (BUILT_IN_PARITY):
7644 target = expand_builtin_unop (target_mode, exp, target,
7645 subtarget, parity_optab);
7646 if (target)
7647 return target;
7648 break;
7650 case BUILT_IN_STRLEN:
7651 target = expand_builtin_strlen (exp, target, target_mode);
7652 if (target)
7653 return target;
7654 break;
7656 case BUILT_IN_STRNLEN:
7657 target = expand_builtin_strnlen (exp, target, target_mode);
7658 if (target)
7659 return target;
7660 break;
7662 case BUILT_IN_STRCPY:
7663 target = expand_builtin_strcpy (exp, target);
7664 if (target)
7665 return target;
7666 break;
7668 case BUILT_IN_STRNCPY:
7669 target = expand_builtin_strncpy (exp, target);
7670 if (target)
7671 return target;
7672 break;
7674 case BUILT_IN_STPCPY:
7675 target = expand_builtin_stpcpy (exp, target, mode);
7676 if (target)
7677 return target;
7678 break;
7680 case BUILT_IN_MEMCPY:
7681 target = expand_builtin_memcpy (exp, target);
7682 if (target)
7683 return target;
7684 break;
7686 case BUILT_IN_MEMMOVE:
7687 target = expand_builtin_memmove (exp, target);
7688 if (target)
7689 return target;
7690 break;
7692 case BUILT_IN_MEMPCPY:
7693 target = expand_builtin_mempcpy (exp, target);
7694 if (target)
7695 return target;
7696 break;
7698 case BUILT_IN_MEMSET:
7699 target = expand_builtin_memset (exp, target, mode);
7700 if (target)
7701 return target;
7702 break;
7704 case BUILT_IN_BZERO:
7705 target = expand_builtin_bzero (exp);
7706 if (target)
7707 return target;
7708 break;
7710 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7711 back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
7712 when changing it to a strcmp call. */
7713 case BUILT_IN_STRCMP_EQ:
7714 target = expand_builtin_memcmp (exp, target, true);
7715 if (target)
7716 return target;
7718 /* Change this call back to a BUILT_IN_STRCMP. */
7719 TREE_OPERAND (exp, 1)
7720 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
7722 /* Delete the last parameter. */
7723 unsigned int i;
7724 vec<tree, va_gc> *arg_vec;
7725 vec_alloc (arg_vec, 2);
7726 for (i = 0; i < 2; i++)
7727 arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
7728 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
7729 /* FALLTHROUGH */
7731 case BUILT_IN_STRCMP:
7732 target = expand_builtin_strcmp (exp, target);
7733 if (target)
7734 return target;
7735 break;
7737 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7738 back to a BUILT_IN_STRNCMP. */
7739 case BUILT_IN_STRNCMP_EQ:
7740 target = expand_builtin_memcmp (exp, target, true);
7741 if (target)
7742 return target;
7744 /* Change it back to a BUILT_IN_STRNCMP. */
7745 TREE_OPERAND (exp, 1)
7746 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
7747 /* FALLTHROUGH */
7749 case BUILT_IN_STRNCMP:
7750 target = expand_builtin_strncmp (exp, target, mode);
7751 if (target)
7752 return target;
7753 break;
7755 case BUILT_IN_BCMP:
7756 case BUILT_IN_MEMCMP:
7757 case BUILT_IN_MEMCMP_EQ:
7758 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
7759 if (target)
7760 return target;
7761 if (fcode == BUILT_IN_MEMCMP_EQ)
7763 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
7764 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
7766 break;
7768 case BUILT_IN_SETJMP:
7769 /* This should have been lowered to the builtins below. */
7770 gcc_unreachable ();
7772 case BUILT_IN_SETJMP_SETUP:
7773 /* __builtin_setjmp_setup is passed a pointer to an array of five words
7774 and the receiver label. */
7775 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7777 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7778 VOIDmode, EXPAND_NORMAL);
7779 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
7780 rtx_insn *label_r = label_rtx (label);
7782 expand_builtin_setjmp_setup (buf_addr, label_r);
7783 return const0_rtx;
7785 break;
7787 case BUILT_IN_SETJMP_RECEIVER:
7788 /* __builtin_setjmp_receiver is passed the receiver label. */
7789 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7791 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
7792 rtx_insn *label_r = label_rtx (label);
7794 expand_builtin_setjmp_receiver (label_r);
7795 nonlocal_goto_handler_labels
7796 = gen_rtx_INSN_LIST (VOIDmode, label_r,
7797 nonlocal_goto_handler_labels);
7798 /* ??? Do not let expand_label treat us as such since we would
7799 not want to be both on the list of non-local labels and on
7800 the list of forced labels. */
7801 FORCED_LABEL (label) = 0;
7802 return const0_rtx;
7804 break;
7806 /* __builtin_longjmp is passed a pointer to an array of five words.
7807 It's similar to the C library longjmp function but works with
7808 __builtin_setjmp above. */
7809 case BUILT_IN_LONGJMP:
7810 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7812 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7813 VOIDmode, EXPAND_NORMAL);
7814 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
7816 if (value != const1_rtx)
7818 error ("%<__builtin_longjmp%> second argument must be 1");
7819 return const0_rtx;
7822 expand_builtin_longjmp (buf_addr, value);
7823 return const0_rtx;
7825 break;
7827 case BUILT_IN_NONLOCAL_GOTO:
7828 target = expand_builtin_nonlocal_goto (exp);
7829 if (target)
7830 return target;
7831 break;
7833 /* This updates the setjmp buffer that is its argument with the value
7834 of the current stack pointer. */
7835 case BUILT_IN_UPDATE_SETJMP_BUF:
7836 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7838 rtx buf_addr
7839 = expand_normal (CALL_EXPR_ARG (exp, 0));
7841 expand_builtin_update_setjmp_buf (buf_addr);
7842 return const0_rtx;
7844 break;
7846 case BUILT_IN_TRAP:
7847 expand_builtin_trap ();
7848 return const0_rtx;
7850 case BUILT_IN_UNREACHABLE:
7851 expand_builtin_unreachable ();
7852 return const0_rtx;
7854 CASE_FLT_FN (BUILT_IN_SIGNBIT):
7855 case BUILT_IN_SIGNBITD32:
7856 case BUILT_IN_SIGNBITD64:
7857 case BUILT_IN_SIGNBITD128:
7858 target = expand_builtin_signbit (exp, target);
7859 if (target)
7860 return target;
7861 break;
7863 /* Various hooks for the DWARF 2 __throw routine. */
7864 case BUILT_IN_UNWIND_INIT:
7865 expand_builtin_unwind_init ();
7866 return const0_rtx;
7867 case BUILT_IN_DWARF_CFA:
7868 return virtual_cfa_rtx;
7869 #ifdef DWARF2_UNWIND_INFO
7870 case BUILT_IN_DWARF_SP_COLUMN:
7871 return expand_builtin_dwarf_sp_column ();
7872 case BUILT_IN_INIT_DWARF_REG_SIZES:
7873 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
7874 return const0_rtx;
7875 #endif
7876 case BUILT_IN_FROB_RETURN_ADDR:
7877 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
7878 case BUILT_IN_EXTRACT_RETURN_ADDR:
7879 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
7880 case BUILT_IN_EH_RETURN:
7881 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
7882 CALL_EXPR_ARG (exp, 1));
7883 return const0_rtx;
7884 case BUILT_IN_EH_RETURN_DATA_REGNO:
7885 return expand_builtin_eh_return_data_regno (exp);
7886 case BUILT_IN_EXTEND_POINTER:
7887 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
7888 case BUILT_IN_EH_POINTER:
7889 return expand_builtin_eh_pointer (exp);
7890 case BUILT_IN_EH_FILTER:
7891 return expand_builtin_eh_filter (exp);
7892 case BUILT_IN_EH_COPY_VALUES:
7893 return expand_builtin_eh_copy_values (exp);
7895 case BUILT_IN_VA_START:
7896 return expand_builtin_va_start (exp);
7897 case BUILT_IN_VA_END:
7898 return expand_builtin_va_end (exp);
7899 case BUILT_IN_VA_COPY:
7900 return expand_builtin_va_copy (exp);
7901 case BUILT_IN_EXPECT:
7902 return expand_builtin_expect (exp, target);
7903 case BUILT_IN_EXPECT_WITH_PROBABILITY:
7904 return expand_builtin_expect_with_probability (exp, target);
7905 case BUILT_IN_ASSUME_ALIGNED:
7906 return expand_builtin_assume_aligned (exp, target);
7907 case BUILT_IN_PREFETCH:
7908 expand_builtin_prefetch (exp);
7909 return const0_rtx;
7911 case BUILT_IN_INIT_TRAMPOLINE:
7912 return expand_builtin_init_trampoline (exp, true);
7913 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
7914 return expand_builtin_init_trampoline (exp, false);
7915 case BUILT_IN_ADJUST_TRAMPOLINE:
7916 return expand_builtin_adjust_trampoline (exp);
7918 case BUILT_IN_INIT_DESCRIPTOR:
7919 return expand_builtin_init_descriptor (exp);
7920 case BUILT_IN_ADJUST_DESCRIPTOR:
7921 return expand_builtin_adjust_descriptor (exp);
7923 case BUILT_IN_FORK:
7924 case BUILT_IN_EXECL:
7925 case BUILT_IN_EXECV:
7926 case BUILT_IN_EXECLP:
7927 case BUILT_IN_EXECLE:
7928 case BUILT_IN_EXECVP:
7929 case BUILT_IN_EXECVE:
7930 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
7931 if (target)
7932 return target;
7933 break;
7935 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
7936 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
7937 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
7938 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
7939 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
7940 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
7941 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
7942 if (target)
7943 return target;
7944 break;
7946 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
7947 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
7948 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
7949 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
7950 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
7951 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
7952 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
7953 if (target)
7954 return target;
7955 break;
7957 case BUILT_IN_SYNC_FETCH_AND_OR_1:
7958 case BUILT_IN_SYNC_FETCH_AND_OR_2:
7959 case BUILT_IN_SYNC_FETCH_AND_OR_4:
7960 case BUILT_IN_SYNC_FETCH_AND_OR_8:
7961 case BUILT_IN_SYNC_FETCH_AND_OR_16:
7962 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
7963 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
7964 if (target)
7965 return target;
7966 break;
7968 case BUILT_IN_SYNC_FETCH_AND_AND_1:
7969 case BUILT_IN_SYNC_FETCH_AND_AND_2:
7970 case BUILT_IN_SYNC_FETCH_AND_AND_4:
7971 case BUILT_IN_SYNC_FETCH_AND_AND_8:
7972 case BUILT_IN_SYNC_FETCH_AND_AND_16:
7973 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
7974 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
7975 if (target)
7976 return target;
7977 break;
7979 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
7980 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
7981 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
7982 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
7983 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
7984 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
7985 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
7986 if (target)
7987 return target;
7988 break;
7990 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
7991 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
7992 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
7993 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
7994 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
7995 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
7996 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
7997 if (target)
7998 return target;
7999 break;
8001 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
8002 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
8003 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
8004 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
8005 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
8006 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
8007 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
8008 if (target)
8009 return target;
8010 break;
8012 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
8013 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
8014 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
8015 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
8016 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
8017 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
8018 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
8019 if (target)
8020 return target;
8021 break;
8023 case BUILT_IN_SYNC_OR_AND_FETCH_1:
8024 case BUILT_IN_SYNC_OR_AND_FETCH_2:
8025 case BUILT_IN_SYNC_OR_AND_FETCH_4:
8026 case BUILT_IN_SYNC_OR_AND_FETCH_8:
8027 case BUILT_IN_SYNC_OR_AND_FETCH_16:
8028 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
8029 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
8030 if (target)
8031 return target;
8032 break;
8034 case BUILT_IN_SYNC_AND_AND_FETCH_1:
8035 case BUILT_IN_SYNC_AND_AND_FETCH_2:
8036 case BUILT_IN_SYNC_AND_AND_FETCH_4:
8037 case BUILT_IN_SYNC_AND_AND_FETCH_8:
8038 case BUILT_IN_SYNC_AND_AND_FETCH_16:
8039 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
8040 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
8041 if (target)
8042 return target;
8043 break;
8045 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
8046 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
8047 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
8048 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
8049 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
8050 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
8051 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
8052 if (target)
8053 return target;
8054 break;
8056 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
8057 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
8058 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
8059 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
8060 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
8061 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
8062 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
8063 if (target)
8064 return target;
8065 break;
8067 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
8068 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
8069 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
8070 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
8071 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
8072 if (mode == VOIDmode)
8073 mode = TYPE_MODE (boolean_type_node);
8074 if (!target || !register_operand (target, mode))
8075 target = gen_reg_rtx (mode);
8077 mode = get_builtin_sync_mode
8078 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
8079 target = expand_builtin_compare_and_swap (mode, exp, true, target);
8080 if (target)
8081 return target;
8082 break;
8084 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
8085 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
8086 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
8087 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
8088 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
8089 mode = get_builtin_sync_mode
8090 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
8091 target = expand_builtin_compare_and_swap (mode, exp, false, target);
8092 if (target)
8093 return target;
8094 break;
8096 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
8097 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
8098 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
8099 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
8100 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
8101 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
8102 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
8103 if (target)
8104 return target;
8105 break;
8107 case BUILT_IN_SYNC_LOCK_RELEASE_1:
8108 case BUILT_IN_SYNC_LOCK_RELEASE_2:
8109 case BUILT_IN_SYNC_LOCK_RELEASE_4:
8110 case BUILT_IN_SYNC_LOCK_RELEASE_8:
8111 case BUILT_IN_SYNC_LOCK_RELEASE_16:
8112 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
8113 expand_builtin_sync_lock_release (mode, exp);
8114 return const0_rtx;
8116 case BUILT_IN_SYNC_SYNCHRONIZE:
8117 expand_builtin_sync_synchronize ();
8118 return const0_rtx;
8120 case BUILT_IN_ATOMIC_EXCHANGE_1:
8121 case BUILT_IN_ATOMIC_EXCHANGE_2:
8122 case BUILT_IN_ATOMIC_EXCHANGE_4:
8123 case BUILT_IN_ATOMIC_EXCHANGE_8:
8124 case BUILT_IN_ATOMIC_EXCHANGE_16:
8125 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
8126 target = expand_builtin_atomic_exchange (mode, exp, target);
8127 if (target)
8128 return target;
8129 break;
8131 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
8132 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
8133 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
8134 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
8135 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
8137 unsigned int nargs, z;
8138 vec<tree, va_gc> *vec;
8140 mode =
8141 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
8142 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
8143 if (target)
8144 return target;
8146 /* If this is turned into an external library call, the weak parameter
8147 must be dropped to match the expected parameter list. */
8148 nargs = call_expr_nargs (exp);
8149 vec_alloc (vec, nargs - 1);
8150 for (z = 0; z < 3; z++)
8151 vec->quick_push (CALL_EXPR_ARG (exp, z));
8152 /* Skip the boolean weak parameter. */
8153 for (z = 4; z < 6; z++)
8154 vec->quick_push (CALL_EXPR_ARG (exp, z));
8155 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
8156 break;
8159 case BUILT_IN_ATOMIC_LOAD_1:
8160 case BUILT_IN_ATOMIC_LOAD_2:
8161 case BUILT_IN_ATOMIC_LOAD_4:
8162 case BUILT_IN_ATOMIC_LOAD_8:
8163 case BUILT_IN_ATOMIC_LOAD_16:
8164 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
8165 target = expand_builtin_atomic_load (mode, exp, target);
8166 if (target)
8167 return target;
8168 break;
8170 case BUILT_IN_ATOMIC_STORE_1:
8171 case BUILT_IN_ATOMIC_STORE_2:
8172 case BUILT_IN_ATOMIC_STORE_4:
8173 case BUILT_IN_ATOMIC_STORE_8:
8174 case BUILT_IN_ATOMIC_STORE_16:
8175 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
8176 target = expand_builtin_atomic_store (mode, exp);
8177 if (target)
8178 return const0_rtx;
8179 break;
8181 case BUILT_IN_ATOMIC_ADD_FETCH_1:
8182 case BUILT_IN_ATOMIC_ADD_FETCH_2:
8183 case BUILT_IN_ATOMIC_ADD_FETCH_4:
8184 case BUILT_IN_ATOMIC_ADD_FETCH_8:
8185 case BUILT_IN_ATOMIC_ADD_FETCH_16:
8187 enum built_in_function lib;
8188 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
8189 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
8190 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
8191 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
8192 ignore, lib);
8193 if (target)
8194 return target;
8195 break;
8197 case BUILT_IN_ATOMIC_SUB_FETCH_1:
8198 case BUILT_IN_ATOMIC_SUB_FETCH_2:
8199 case BUILT_IN_ATOMIC_SUB_FETCH_4:
8200 case BUILT_IN_ATOMIC_SUB_FETCH_8:
8201 case BUILT_IN_ATOMIC_SUB_FETCH_16:
8203 enum built_in_function lib;
8204 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
8205 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
8206 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
8207 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
8208 ignore, lib);
8209 if (target)
8210 return target;
8211 break;
8213 case BUILT_IN_ATOMIC_AND_FETCH_1:
8214 case BUILT_IN_ATOMIC_AND_FETCH_2:
8215 case BUILT_IN_ATOMIC_AND_FETCH_4:
8216 case BUILT_IN_ATOMIC_AND_FETCH_8:
8217 case BUILT_IN_ATOMIC_AND_FETCH_16:
8219 enum built_in_function lib;
8220 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
8221 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
8222 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
8223 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
8224 ignore, lib);
8225 if (target)
8226 return target;
8227 break;
8229 case BUILT_IN_ATOMIC_NAND_FETCH_1:
8230 case BUILT_IN_ATOMIC_NAND_FETCH_2:
8231 case BUILT_IN_ATOMIC_NAND_FETCH_4:
8232 case BUILT_IN_ATOMIC_NAND_FETCH_8:
8233 case BUILT_IN_ATOMIC_NAND_FETCH_16:
8235 enum built_in_function lib;
8236 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
8237 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
8238 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
8239 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
8240 ignore, lib);
8241 if (target)
8242 return target;
8243 break;
8245 case BUILT_IN_ATOMIC_XOR_FETCH_1:
8246 case BUILT_IN_ATOMIC_XOR_FETCH_2:
8247 case BUILT_IN_ATOMIC_XOR_FETCH_4:
8248 case BUILT_IN_ATOMIC_XOR_FETCH_8:
8249 case BUILT_IN_ATOMIC_XOR_FETCH_16:
8251 enum built_in_function lib;
8252 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
8253 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
8254 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
8255 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
8256 ignore, lib);
8257 if (target)
8258 return target;
8259 break;
8261 case BUILT_IN_ATOMIC_OR_FETCH_1:
8262 case BUILT_IN_ATOMIC_OR_FETCH_2:
8263 case BUILT_IN_ATOMIC_OR_FETCH_4:
8264 case BUILT_IN_ATOMIC_OR_FETCH_8:
8265 case BUILT_IN_ATOMIC_OR_FETCH_16:
8267 enum built_in_function lib;
8268 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
8269 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
8270 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
8271 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
8272 ignore, lib);
8273 if (target)
8274 return target;
8275 break;
8277 case BUILT_IN_ATOMIC_FETCH_ADD_1:
8278 case BUILT_IN_ATOMIC_FETCH_ADD_2:
8279 case BUILT_IN_ATOMIC_FETCH_ADD_4:
8280 case BUILT_IN_ATOMIC_FETCH_ADD_8:
8281 case BUILT_IN_ATOMIC_FETCH_ADD_16:
8282 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
8283 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
8284 ignore, BUILT_IN_NONE);
8285 if (target)
8286 return target;
8287 break;
8289 case BUILT_IN_ATOMIC_FETCH_SUB_1:
8290 case BUILT_IN_ATOMIC_FETCH_SUB_2:
8291 case BUILT_IN_ATOMIC_FETCH_SUB_4:
8292 case BUILT_IN_ATOMIC_FETCH_SUB_8:
8293 case BUILT_IN_ATOMIC_FETCH_SUB_16:
8294 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
8295 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
8296 ignore, BUILT_IN_NONE);
8297 if (target)
8298 return target;
8299 break;
8301 case BUILT_IN_ATOMIC_FETCH_AND_1:
8302 case BUILT_IN_ATOMIC_FETCH_AND_2:
8303 case BUILT_IN_ATOMIC_FETCH_AND_4:
8304 case BUILT_IN_ATOMIC_FETCH_AND_8:
8305 case BUILT_IN_ATOMIC_FETCH_AND_16:
8306 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
8307 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
8308 ignore, BUILT_IN_NONE);
8309 if (target)
8310 return target;
8311 break;
8313 case BUILT_IN_ATOMIC_FETCH_NAND_1:
8314 case BUILT_IN_ATOMIC_FETCH_NAND_2:
8315 case BUILT_IN_ATOMIC_FETCH_NAND_4:
8316 case BUILT_IN_ATOMIC_FETCH_NAND_8:
8317 case BUILT_IN_ATOMIC_FETCH_NAND_16:
8318 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
8319 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
8320 ignore, BUILT_IN_NONE);
8321 if (target)
8322 return target;
8323 break;
8325 case BUILT_IN_ATOMIC_FETCH_XOR_1:
8326 case BUILT_IN_ATOMIC_FETCH_XOR_2:
8327 case BUILT_IN_ATOMIC_FETCH_XOR_4:
8328 case BUILT_IN_ATOMIC_FETCH_XOR_8:
8329 case BUILT_IN_ATOMIC_FETCH_XOR_16:
8330 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
8331 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
8332 ignore, BUILT_IN_NONE);
8333 if (target)
8334 return target;
8335 break;
8337 case BUILT_IN_ATOMIC_FETCH_OR_1:
8338 case BUILT_IN_ATOMIC_FETCH_OR_2:
8339 case BUILT_IN_ATOMIC_FETCH_OR_4:
8340 case BUILT_IN_ATOMIC_FETCH_OR_8:
8341 case BUILT_IN_ATOMIC_FETCH_OR_16:
8342 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
8343 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
8344 ignore, BUILT_IN_NONE);
8345 if (target)
8346 return target;
8347 break;
8349 case BUILT_IN_ATOMIC_TEST_AND_SET:
8350 return expand_builtin_atomic_test_and_set (exp, target);
8352 case BUILT_IN_ATOMIC_CLEAR:
8353 return expand_builtin_atomic_clear (exp);
8355 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8356 return expand_builtin_atomic_always_lock_free (exp);
8358 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8359 target = expand_builtin_atomic_is_lock_free (exp);
8360 if (target)
8361 return target;
8362 break;
8364 case BUILT_IN_ATOMIC_THREAD_FENCE:
8365 expand_builtin_atomic_thread_fence (exp);
8366 return const0_rtx;
8368 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
8369 expand_builtin_atomic_signal_fence (exp);
8370 return const0_rtx;
8372 case BUILT_IN_OBJECT_SIZE:
8373 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
8374 return expand_builtin_object_size (exp);
8376 case BUILT_IN_MEMCPY_CHK:
8377 case BUILT_IN_MEMPCPY_CHK:
8378 case BUILT_IN_MEMMOVE_CHK:
8379 case BUILT_IN_MEMSET_CHK:
8380 target = expand_builtin_memory_chk (exp, target, mode, fcode);
8381 if (target)
8382 return target;
8383 break;
8385 case BUILT_IN_STRCPY_CHK:
8386 case BUILT_IN_STPCPY_CHK:
8387 case BUILT_IN_STRNCPY_CHK:
8388 case BUILT_IN_STPNCPY_CHK:
8389 case BUILT_IN_STRCAT_CHK:
8390 case BUILT_IN_STRNCAT_CHK:
8391 case BUILT_IN_SNPRINTF_CHK:
8392 case BUILT_IN_VSNPRINTF_CHK:
8393 maybe_emit_chk_warning (exp, fcode);
8394 break;
8396 case BUILT_IN_SPRINTF_CHK:
8397 case BUILT_IN_VSPRINTF_CHK:
8398 maybe_emit_sprintf_chk_warning (exp, fcode);
8399 break;
8401 case BUILT_IN_THREAD_POINTER:
8402 return expand_builtin_thread_pointer (exp, target);
8404 case BUILT_IN_SET_THREAD_POINTER:
8405 expand_builtin_set_thread_pointer (exp);
8406 return const0_rtx;
8408 case BUILT_IN_ACC_ON_DEVICE:
8409 /* Do library call, if we failed to expand the builtin when
8410 folding. */
8411 break;
8413 case BUILT_IN_GOACC_PARLEVEL_ID:
8414 case BUILT_IN_GOACC_PARLEVEL_SIZE:
8415 return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
8417 case BUILT_IN_SPECULATION_SAFE_VALUE_PTR:
8418 return expand_speculation_safe_value (VOIDmode, exp, target, ignore);
8420 case BUILT_IN_SPECULATION_SAFE_VALUE_1:
8421 case BUILT_IN_SPECULATION_SAFE_VALUE_2:
8422 case BUILT_IN_SPECULATION_SAFE_VALUE_4:
8423 case BUILT_IN_SPECULATION_SAFE_VALUE_8:
8424 case BUILT_IN_SPECULATION_SAFE_VALUE_16:
8425 mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1);
8426 return expand_speculation_safe_value (mode, exp, target, ignore);
8428 default: /* just do library call, if unknown builtin */
8429 break;
8432 /* The switch statement above can drop through to cause the function
8433 to be called normally. */
8434 return expand_call (exp, target, ignore);
8437 /* Determine whether a tree node represents a call to a built-in
8438 function. If the tree T is a call to a built-in function with
8439 the right number of arguments of the appropriate types, return
8440 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
8441 Otherwise the return value is END_BUILTINS. */
8443 enum built_in_function
8444 builtin_mathfn_code (const_tree t)
8446 const_tree fndecl, arg, parmlist;
8447 const_tree argtype, parmtype;
8448 const_call_expr_arg_iterator iter;
8450 if (TREE_CODE (t) != CALL_EXPR)
8451 return END_BUILTINS;
8453 fndecl = get_callee_fndecl (t);
8454 if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
8455 return END_BUILTINS;
8457 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
8458 init_const_call_expr_arg_iterator (t, &iter);
8459 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
8461 /* If a function doesn't take a variable number of arguments,
8462 the last element in the list will have type `void'. */
8463 parmtype = TREE_VALUE (parmlist);
8464 if (VOID_TYPE_P (parmtype))
8466 if (more_const_call_expr_args_p (&iter))
8467 return END_BUILTINS;
8468 return DECL_FUNCTION_CODE (fndecl);
8471 if (! more_const_call_expr_args_p (&iter))
8472 return END_BUILTINS;
8474 arg = next_const_call_expr_arg (&iter);
8475 argtype = TREE_TYPE (arg);
8477 if (SCALAR_FLOAT_TYPE_P (parmtype))
8479 if (! SCALAR_FLOAT_TYPE_P (argtype))
8480 return END_BUILTINS;
8482 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
8484 if (! COMPLEX_FLOAT_TYPE_P (argtype))
8485 return END_BUILTINS;
8487 else if (POINTER_TYPE_P (parmtype))
8489 if (! POINTER_TYPE_P (argtype))
8490 return END_BUILTINS;
8492 else if (INTEGRAL_TYPE_P (parmtype))
8494 if (! INTEGRAL_TYPE_P (argtype))
8495 return END_BUILTINS;
8497 else
8498 return END_BUILTINS;
8501 /* Variable-length argument list. */
8502 return DECL_FUNCTION_CODE (fndecl);
8505 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
8506 evaluate to a constant. */
8508 static tree
8509 fold_builtin_constant_p (tree arg)
8511 /* We return 1 for a numeric type that's known to be a constant
8512 value at compile-time or for an aggregate type that's a
8513 literal constant. */
8514 STRIP_NOPS (arg);
8516 /* If we know this is a constant, emit the constant of one. */
8517 if (CONSTANT_CLASS_P (arg)
8518 || (TREE_CODE (arg) == CONSTRUCTOR
8519 && TREE_CONSTANT (arg)))
8520 return integer_one_node;
8521 if (TREE_CODE (arg) == ADDR_EXPR)
8523 tree op = TREE_OPERAND (arg, 0);
8524 if (TREE_CODE (op) == STRING_CST
8525 || (TREE_CODE (op) == ARRAY_REF
8526 && integer_zerop (TREE_OPERAND (op, 1))
8527 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
8528 return integer_one_node;
8531 /* If this expression has side effects, show we don't know it to be a
8532 constant. Likewise if it's a pointer or aggregate type since in
8533 those case we only want literals, since those are only optimized
8534 when generating RTL, not later.
8535 And finally, if we are compiling an initializer, not code, we
8536 need to return a definite result now; there's not going to be any
8537 more optimization done. */
8538 if (TREE_SIDE_EFFECTS (arg)
8539 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
8540 || POINTER_TYPE_P (TREE_TYPE (arg))
8541 || cfun == 0
8542 || folding_initializer
8543 || force_folding_builtin_constant_p)
8544 return integer_zero_node;
8546 return NULL_TREE;
8549 /* Create builtin_expect or builtin_expect_with_probability
8550 with PRED and EXPECTED as its arguments and return it as a truthvalue.
8551 Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
8552 builtin_expect_with_probability instead uses third argument as PROBABILITY
8553 value. */
8555 static tree
8556 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
8557 tree predictor, tree probability)
8559 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
8561 fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT
8562 : BUILT_IN_EXPECT_WITH_PROBABILITY);
8563 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
8564 ret_type = TREE_TYPE (TREE_TYPE (fn));
8565 pred_type = TREE_VALUE (arg_types);
8566 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
8568 pred = fold_convert_loc (loc, pred_type, pred);
8569 expected = fold_convert_loc (loc, expected_type, expected);
8571 if (probability)
8572 call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability);
8573 else
8574 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
8575 predictor);
8577 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
8578 build_int_cst (ret_type, 0));
8581 /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
8582 NULL_TREE if no simplification is possible. */
8584 tree
8585 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2,
8586 tree arg3)
8588 tree inner, fndecl, inner_arg0;
8589 enum tree_code code;
8591 /* Distribute the expected value over short-circuiting operators.
8592 See through the cast from truthvalue_type_node to long. */
8593 inner_arg0 = arg0;
8594 while (CONVERT_EXPR_P (inner_arg0)
8595 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
8596 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
8597 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
8599 /* If this is a builtin_expect within a builtin_expect keep the
8600 inner one. See through a comparison against a constant. It
8601 might have been added to create a thruthvalue. */
8602 inner = inner_arg0;
8604 if (COMPARISON_CLASS_P (inner)
8605 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
8606 inner = TREE_OPERAND (inner, 0);
8608 if (TREE_CODE (inner) == CALL_EXPR
8609 && (fndecl = get_callee_fndecl (inner))
8610 && (fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
8611 || fndecl_built_in_p (fndecl, BUILT_IN_EXPECT_WITH_PROBABILITY)))
8612 return arg0;
8614 inner = inner_arg0;
8615 code = TREE_CODE (inner);
8616 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
8618 tree op0 = TREE_OPERAND (inner, 0);
8619 tree op1 = TREE_OPERAND (inner, 1);
8620 arg1 = save_expr (arg1);
8622 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3);
8623 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3);
8624 inner = build2 (code, TREE_TYPE (inner), op0, op1);
8626 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
8629 /* If the argument isn't invariant then there's nothing else we can do. */
8630 if (!TREE_CONSTANT (inner_arg0))
8631 return NULL_TREE;
8633 /* If we expect that a comparison against the argument will fold to
8634 a constant return the constant. In practice, this means a true
8635 constant or the address of a non-weak symbol. */
8636 inner = inner_arg0;
8637 STRIP_NOPS (inner);
8638 if (TREE_CODE (inner) == ADDR_EXPR)
8642 inner = TREE_OPERAND (inner, 0);
8644 while (TREE_CODE (inner) == COMPONENT_REF
8645 || TREE_CODE (inner) == ARRAY_REF);
8646 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
8647 return NULL_TREE;
8650 /* Otherwise, ARG0 already has the proper type for the return value. */
8651 return arg0;
8654 /* Fold a call to __builtin_classify_type with argument ARG. */
8656 static tree
8657 fold_builtin_classify_type (tree arg)
8659 if (arg == 0)
8660 return build_int_cst (integer_type_node, no_type_class);
8662 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
8665 /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
8666 ARG. */
8668 static tree
8669 fold_builtin_strlen (location_t loc, tree expr, tree type, tree arg)
8671 if (!validate_arg (arg, POINTER_TYPE))
8672 return NULL_TREE;
8673 else
8675 c_strlen_data lendata = { };
8676 tree len = c_strlen (arg, 0, &lendata);
8678 if (len)
8679 return fold_convert_loc (loc, type, len);
8681 /* TODO: Move this to gimple-ssa-warn-access once the pass runs
8682 also early enough to detect invalid reads in multimensional
8683 arrays and struct members. */
8684 if (!lendata.decl)
8685 c_strlen (arg, 1, &lendata);
8687 if (lendata.decl)
8689 if (EXPR_HAS_LOCATION (arg))
8690 loc = EXPR_LOCATION (arg);
8691 else if (loc == UNKNOWN_LOCATION)
8692 loc = input_location;
8693 warn_string_no_nul (loc, expr, "strlen", arg, lendata.decl);
8696 return NULL_TREE;
8700 /* Fold a call to __builtin_inf or __builtin_huge_val. */
8702 static tree
8703 fold_builtin_inf (location_t loc, tree type, int warn)
8705 /* __builtin_inff is intended to be usable to define INFINITY on all
8706 targets. If an infinity is not available, INFINITY expands "to a
8707 positive constant of type float that overflows at translation
8708 time", footnote "In this case, using INFINITY will violate the
8709 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
8710 Thus we pedwarn to ensure this constraint violation is
8711 diagnosed. */
8712 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
8713 pedwarn (loc, 0, "target format does not support infinity");
8715 return build_real (type, dconstinf);
8718 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8719 NULL_TREE if no simplification can be made. */
8721 static tree
8722 fold_builtin_sincos (location_t loc,
8723 tree arg0, tree arg1, tree arg2)
8725 tree type;
8726 tree fndecl, call = NULL_TREE;
8728 if (!validate_arg (arg0, REAL_TYPE)
8729 || !validate_arg (arg1, POINTER_TYPE)
8730 || !validate_arg (arg2, POINTER_TYPE))
8731 return NULL_TREE;
8733 type = TREE_TYPE (arg0);
8735 /* Calculate the result when the argument is a constant. */
8736 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
8737 if (fn == END_BUILTINS)
8738 return NULL_TREE;
8740 /* Canonicalize sincos to cexpi. */
8741 if (TREE_CODE (arg0) == REAL_CST)
8743 tree complex_type = build_complex_type (type);
8744 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
8746 if (!call)
8748 if (!targetm.libc_has_function (function_c99_math_complex, type)
8749 || !builtin_decl_implicit_p (fn))
8750 return NULL_TREE;
8751 fndecl = builtin_decl_explicit (fn);
8752 call = build_call_expr_loc (loc, fndecl, 1, arg0);
8753 call = builtin_save_expr (call);
8756 tree ptype = build_pointer_type (type);
8757 arg1 = fold_convert (ptype, arg1);
8758 arg2 = fold_convert (ptype, arg2);
8759 return build2 (COMPOUND_EXPR, void_type_node,
8760 build2 (MODIFY_EXPR, void_type_node,
8761 build_fold_indirect_ref_loc (loc, arg1),
8762 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
8763 build2 (MODIFY_EXPR, void_type_node,
8764 build_fold_indirect_ref_loc (loc, arg2),
8765 fold_build1_loc (loc, REALPART_EXPR, type, call)));
8768 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8769 Return NULL_TREE if no simplification can be made. */
8771 static tree
8772 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8774 if (!validate_arg (arg1, POINTER_TYPE)
8775 || !validate_arg (arg2, POINTER_TYPE)
8776 || !validate_arg (len, INTEGER_TYPE))
8777 return NULL_TREE;
8779 /* If the LEN parameter is zero, return zero. */
8780 if (integer_zerop (len))
8781 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8782 arg1, arg2);
8784 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8785 if (operand_equal_p (arg1, arg2, 0))
8786 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8788 /* If len parameter is one, return an expression corresponding to
8789 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8790 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8792 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8793 tree cst_uchar_ptr_node
8794 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8796 tree ind1
8797 = fold_convert_loc (loc, integer_type_node,
8798 build1 (INDIRECT_REF, cst_uchar_node,
8799 fold_convert_loc (loc,
8800 cst_uchar_ptr_node,
8801 arg1)));
8802 tree ind2
8803 = fold_convert_loc (loc, integer_type_node,
8804 build1 (INDIRECT_REF, cst_uchar_node,
8805 fold_convert_loc (loc,
8806 cst_uchar_ptr_node,
8807 arg2)));
8808 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8811 return NULL_TREE;
8814 /* Fold a call to builtin isascii with argument ARG. */
8816 static tree
8817 fold_builtin_isascii (location_t loc, tree arg)
8819 if (!validate_arg (arg, INTEGER_TYPE))
8820 return NULL_TREE;
8821 else
8823 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8824 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8825 build_int_cst (integer_type_node,
8826 ~ (unsigned HOST_WIDE_INT) 0x7f));
8827 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8828 arg, integer_zero_node);
8832 /* Fold a call to builtin toascii with argument ARG. */
8834 static tree
8835 fold_builtin_toascii (location_t loc, tree arg)
8837 if (!validate_arg (arg, INTEGER_TYPE))
8838 return NULL_TREE;
8840 /* Transform toascii(c) -> (c & 0x7f). */
8841 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8842 build_int_cst (integer_type_node, 0x7f));
8845 /* Fold a call to builtin isdigit with argument ARG. */
8847 static tree
8848 fold_builtin_isdigit (location_t loc, tree arg)
8850 if (!validate_arg (arg, INTEGER_TYPE))
8851 return NULL_TREE;
8852 else
8854 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8855 /* According to the C standard, isdigit is unaffected by locale.
8856 However, it definitely is affected by the target character set. */
8857 unsigned HOST_WIDE_INT target_digit0
8858 = lang_hooks.to_target_charset ('0');
8860 if (target_digit0 == 0)
8861 return NULL_TREE;
8863 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8864 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8865 build_int_cst (unsigned_type_node, target_digit0));
8866 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8867 build_int_cst (unsigned_type_node, 9));
8871 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8873 static tree
8874 fold_builtin_fabs (location_t loc, tree arg, tree type)
8876 if (!validate_arg (arg, REAL_TYPE))
8877 return NULL_TREE;
8879 arg = fold_convert_loc (loc, type, arg);
8880 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8883 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8885 static tree
8886 fold_builtin_abs (location_t loc, tree arg, tree type)
8888 if (!validate_arg (arg, INTEGER_TYPE))
8889 return NULL_TREE;
8891 arg = fold_convert_loc (loc, type, arg);
8892 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8895 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8897 static tree
8898 fold_builtin_carg (location_t loc, tree arg, tree type)
8900 if (validate_arg (arg, COMPLEX_TYPE)
8901 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8903 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8905 if (atan2_fn)
8907 tree new_arg = builtin_save_expr (arg);
8908 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8909 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8910 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8914 return NULL_TREE;
8917 /* Fold a call to builtin frexp, we can assume the base is 2. */
8919 static tree
8920 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
8922 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8923 return NULL_TREE;
8925 STRIP_NOPS (arg0);
8927 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8928 return NULL_TREE;
8930 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8932 /* Proceed if a valid pointer type was passed in. */
8933 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
8935 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8936 tree frac, exp, res;
8938 switch (value->cl)
8940 case rvc_zero:
8941 /* For +-0, return (*exp = 0, +-0). */
8942 exp = integer_zero_node;
8943 frac = arg0;
8944 break;
8945 case rvc_nan:
8946 case rvc_inf:
8947 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8948 return omit_one_operand_loc (loc, rettype, arg0, arg1);
8949 case rvc_normal:
8951 /* Since the frexp function always expects base 2, and in
8952 GCC normalized significands are already in the range
8953 [0.5, 1.0), we have exactly what frexp wants. */
8954 REAL_VALUE_TYPE frac_rvt = *value;
8955 SET_REAL_EXP (&frac_rvt, 0);
8956 frac = build_real (rettype, frac_rvt);
8957 exp = build_int_cst (integer_type_node, REAL_EXP (value));
8959 break;
8960 default:
8961 gcc_unreachable ();
8964 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8965 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
8966 TREE_SIDE_EFFECTS (arg1) = 1;
8967 res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
8968 suppress_warning (res, OPT_Wunused_value);
8969 return res;
8972 return NULL_TREE;
8975 /* Fold a call to builtin modf. */
8977 static tree
8978 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
8980 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8981 return NULL_TREE;
8983 STRIP_NOPS (arg0);
8985 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8986 return NULL_TREE;
8988 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8990 /* Proceed if a valid pointer type was passed in. */
8991 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
8993 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8994 REAL_VALUE_TYPE trunc, frac;
8995 tree res;
8997 switch (value->cl)
8999 case rvc_nan:
9000 case rvc_zero:
9001 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
9002 trunc = frac = *value;
9003 break;
9004 case rvc_inf:
9005 /* For +-Inf, return (*arg1 = arg0, +-0). */
9006 frac = dconst0;
9007 frac.sign = value->sign;
9008 trunc = *value;
9009 break;
9010 case rvc_normal:
9011 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
9012 real_trunc (&trunc, VOIDmode, value);
9013 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
9014 /* If the original number was negative and already
9015 integral, then the fractional part is -0.0. */
9016 if (value->sign && frac.cl == rvc_zero)
9017 frac.sign = value->sign;
9018 break;
9021 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
9022 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
9023 build_real (rettype, trunc));
9024 TREE_SIDE_EFFECTS (arg1) = 1;
9025 res = fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
9026 build_real (rettype, frac));
9027 suppress_warning (res, OPT_Wunused_value);
9028 return res;
9031 return NULL_TREE;
9034 /* Given a location LOC, an interclass builtin function decl FNDECL
9035 and its single argument ARG, return an folded expression computing
9036 the same, or NULL_TREE if we either couldn't or didn't want to fold
9037 (the latter happen if there's an RTL instruction available). */
9039 static tree
9040 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
9042 machine_mode mode;
9044 if (!validate_arg (arg, REAL_TYPE))
9045 return NULL_TREE;
9047 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
9048 return NULL_TREE;
9050 mode = TYPE_MODE (TREE_TYPE (arg));
9052 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
9054 /* If there is no optab, try generic code. */
9055 switch (DECL_FUNCTION_CODE (fndecl))
9057 tree result;
9059 CASE_FLT_FN (BUILT_IN_ISINF):
9061 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
9062 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9063 tree type = TREE_TYPE (arg);
9064 REAL_VALUE_TYPE r;
9065 char buf[128];
9067 if (is_ibm_extended)
9069 /* NaN and Inf are encoded in the high-order double value
9070 only. The low-order value is not significant. */
9071 type = double_type_node;
9072 mode = DFmode;
9073 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9075 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9076 real_from_string (&r, buf);
9077 result = build_call_expr (isgr_fn, 2,
9078 fold_build1_loc (loc, ABS_EXPR, type, arg),
9079 build_real (type, r));
9080 return result;
9082 CASE_FLT_FN (BUILT_IN_FINITE):
9083 case BUILT_IN_ISFINITE:
9085 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
9086 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9087 tree type = TREE_TYPE (arg);
9088 REAL_VALUE_TYPE r;
9089 char buf[128];
9091 if (is_ibm_extended)
9093 /* NaN and Inf are encoded in the high-order double value
9094 only. The low-order value is not significant. */
9095 type = double_type_node;
9096 mode = DFmode;
9097 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9099 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9100 real_from_string (&r, buf);
9101 result = build_call_expr (isle_fn, 2,
9102 fold_build1_loc (loc, ABS_EXPR, type, arg),
9103 build_real (type, r));
9104 /*result = fold_build2_loc (loc, UNGT_EXPR,
9105 TREE_TYPE (TREE_TYPE (fndecl)),
9106 fold_build1_loc (loc, ABS_EXPR, type, arg),
9107 build_real (type, r));
9108 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
9109 TREE_TYPE (TREE_TYPE (fndecl)),
9110 result);*/
9111 return result;
9113 case BUILT_IN_ISNORMAL:
9115 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
9116 islessequal(fabs(x),DBL_MAX). */
9117 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
9118 tree type = TREE_TYPE (arg);
9119 tree orig_arg, max_exp, min_exp;
9120 machine_mode orig_mode = mode;
9121 REAL_VALUE_TYPE rmax, rmin;
9122 char buf[128];
9124 orig_arg = arg = builtin_save_expr (arg);
9125 if (is_ibm_extended)
9127 /* Use double to test the normal range of IBM extended
9128 precision. Emin for IBM extended precision is
9129 different to emin for IEEE double, being 53 higher
9130 since the low double exponent is at least 53 lower
9131 than the high double exponent. */
9132 type = double_type_node;
9133 mode = DFmode;
9134 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
9136 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
9138 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
9139 real_from_string (&rmax, buf);
9140 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
9141 real_from_string (&rmin, buf);
9142 max_exp = build_real (type, rmax);
9143 min_exp = build_real (type, rmin);
9145 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
9146 if (is_ibm_extended)
9148 /* Testing the high end of the range is done just using
9149 the high double, using the same test as isfinite().
9150 For the subnormal end of the range we first test the
9151 high double, then if its magnitude is equal to the
9152 limit of 0x1p-969, we test whether the low double is
9153 non-zero and opposite sign to the high double. */
9154 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
9155 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
9156 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
9157 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
9158 arg, min_exp);
9159 tree as_complex = build1 (VIEW_CONVERT_EXPR,
9160 complex_double_type_node, orig_arg);
9161 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
9162 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
9163 tree zero = build_real (type, dconst0);
9164 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
9165 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
9166 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
9167 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
9168 fold_build3 (COND_EXPR,
9169 integer_type_node,
9170 hilt, logt, lolt));
9171 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
9172 eq_min, ok_lo);
9173 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
9174 gt_min, eq_min);
9176 else
9178 tree const isge_fn
9179 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
9180 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
9182 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
9183 max_exp, min_exp);
9184 return result;
9186 default:
9187 break;
9190 return NULL_TREE;
9193 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
9194 ARG is the argument for the call. */
9196 static tree
9197 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
9199 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9201 if (!validate_arg (arg, REAL_TYPE))
9202 return NULL_TREE;
9204 switch (builtin_index)
9206 case BUILT_IN_ISINF:
9207 if (tree_expr_infinite_p (arg))
9208 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9209 if (!tree_expr_maybe_infinite_p (arg))
9210 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9211 return NULL_TREE;
9213 case BUILT_IN_ISINF_SIGN:
9215 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
9216 /* In a boolean context, GCC will fold the inner COND_EXPR to
9217 1. So e.g. "if (isinf_sign(x))" would be folded to just
9218 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
9219 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
9220 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
9221 tree tmp = NULL_TREE;
9223 arg = builtin_save_expr (arg);
9225 if (signbit_fn && isinf_fn)
9227 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
9228 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
9230 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9231 signbit_call, integer_zero_node);
9232 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
9233 isinf_call, integer_zero_node);
9235 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
9236 integer_minus_one_node, integer_one_node);
9237 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9238 isinf_call, tmp,
9239 integer_zero_node);
9242 return tmp;
9245 case BUILT_IN_ISFINITE:
9246 if (tree_expr_finite_p (arg))
9247 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9248 if (tree_expr_nan_p (arg) || tree_expr_infinite_p (arg))
9249 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9250 return NULL_TREE;
9252 case BUILT_IN_ISNAN:
9253 if (tree_expr_nan_p (arg))
9254 return omit_one_operand_loc (loc, type, integer_one_node, arg);
9255 if (!tree_expr_maybe_nan_p (arg))
9256 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9259 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
9260 if (is_ibm_extended)
9262 /* NaN and Inf are encoded in the high-order double value
9263 only. The low-order value is not significant. */
9264 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
9267 arg = builtin_save_expr (arg);
9268 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
9270 case BUILT_IN_ISSIGNALING:
9271 /* Folding to true for REAL_CST is done in fold_const_call_ss.
9272 Don't use tree_expr_signaling_nan_p (arg) -> integer_one_node
9273 and !tree_expr_maybe_signaling_nan_p (arg) -> integer_zero_node
9274 here, so there is some possibility of __builtin_issignaling working
9275 without -fsignaling-nans. Especially when -fno-signaling-nans is
9276 the default. */
9277 if (!tree_expr_maybe_nan_p (arg))
9278 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
9279 return NULL_TREE;
9281 default:
9282 gcc_unreachable ();
9286 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
9287 This builtin will generate code to return the appropriate floating
9288 point classification depending on the value of the floating point
9289 number passed in. The possible return values must be supplied as
9290 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
9291 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
9292 one floating point argument which is "type generic". */
9294 static tree
9295 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
9297 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
9298 arg, type, res, tmp;
9299 machine_mode mode;
9300 REAL_VALUE_TYPE r;
9301 char buf[128];
9303 /* Verify the required arguments in the original call. */
9304 if (nargs != 6
9305 || !validate_arg (args[0], INTEGER_TYPE)
9306 || !validate_arg (args[1], INTEGER_TYPE)
9307 || !validate_arg (args[2], INTEGER_TYPE)
9308 || !validate_arg (args[3], INTEGER_TYPE)
9309 || !validate_arg (args[4], INTEGER_TYPE)
9310 || !validate_arg (args[5], REAL_TYPE))
9311 return NULL_TREE;
9313 fp_nan = args[0];
9314 fp_infinite = args[1];
9315 fp_normal = args[2];
9316 fp_subnormal = args[3];
9317 fp_zero = args[4];
9318 arg = args[5];
9319 type = TREE_TYPE (arg);
9320 mode = TYPE_MODE (type);
9321 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
9323 /* fpclassify(x) ->
9324 isnan(x) ? FP_NAN :
9325 (fabs(x) == Inf ? FP_INFINITE :
9326 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9327 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9329 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9330 build_real (type, dconst0));
9331 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9332 tmp, fp_zero, fp_subnormal);
9334 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9335 real_from_string (&r, buf);
9336 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9337 arg, build_real (type, r));
9338 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9340 if (tree_expr_maybe_infinite_p (arg))
9342 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9343 build_real (type, dconstinf));
9344 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9345 fp_infinite, res);
9348 if (tree_expr_maybe_nan_p (arg))
9350 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9351 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9354 return res;
9357 /* Fold a call to an unordered comparison function such as
9358 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9359 being called and ARG0 and ARG1 are the arguments for the call.
9360 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9361 the opposite of the desired result. UNORDERED_CODE is used
9362 for modes that can hold NaNs and ORDERED_CODE is used for
9363 the rest. */
9365 static tree
9366 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9367 enum tree_code unordered_code,
9368 enum tree_code ordered_code)
9370 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9371 enum tree_code code;
9372 tree type0, type1;
9373 enum tree_code code0, code1;
9374 tree cmp_type = NULL_TREE;
9376 type0 = TREE_TYPE (arg0);
9377 type1 = TREE_TYPE (arg1);
9379 code0 = TREE_CODE (type0);
9380 code1 = TREE_CODE (type1);
9382 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9383 /* Choose the wider of two real types. */
9384 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9385 ? type0 : type1;
9386 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9387 cmp_type = type0;
9388 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9389 cmp_type = type1;
9391 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9392 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9394 if (unordered_code == UNORDERED_EXPR)
9396 if (tree_expr_nan_p (arg0) || tree_expr_nan_p (arg1))
9397 return omit_two_operands_loc (loc, type, integer_one_node, arg0, arg1);
9398 if (!tree_expr_maybe_nan_p (arg0) && !tree_expr_maybe_nan_p (arg1))
9399 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9400 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9403 code = (tree_expr_maybe_nan_p (arg0) || tree_expr_maybe_nan_p (arg1))
9404 ? unordered_code : ordered_code;
9405 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9406 fold_build2_loc (loc, code, type, arg0, arg1));
9409 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9410 arithmetics if it can never overflow, or into internal functions that
9411 return both result of arithmetics and overflowed boolean flag in
9412 a complex integer result, or some other check for overflow.
9413 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
9414 checking part of that. */
9416 static tree
9417 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
9418 tree arg0, tree arg1, tree arg2)
9420 enum internal_fn ifn = IFN_LAST;
9421 /* The code of the expression corresponding to the built-in. */
9422 enum tree_code opcode = ERROR_MARK;
9423 bool ovf_only = false;
9425 switch (fcode)
9427 case BUILT_IN_ADD_OVERFLOW_P:
9428 ovf_only = true;
9429 /* FALLTHRU */
9430 case BUILT_IN_ADD_OVERFLOW:
9431 case BUILT_IN_SADD_OVERFLOW:
9432 case BUILT_IN_SADDL_OVERFLOW:
9433 case BUILT_IN_SADDLL_OVERFLOW:
9434 case BUILT_IN_UADD_OVERFLOW:
9435 case BUILT_IN_UADDL_OVERFLOW:
9436 case BUILT_IN_UADDLL_OVERFLOW:
9437 opcode = PLUS_EXPR;
9438 ifn = IFN_ADD_OVERFLOW;
9439 break;
9440 case BUILT_IN_SUB_OVERFLOW_P:
9441 ovf_only = true;
9442 /* FALLTHRU */
9443 case BUILT_IN_SUB_OVERFLOW:
9444 case BUILT_IN_SSUB_OVERFLOW:
9445 case BUILT_IN_SSUBL_OVERFLOW:
9446 case BUILT_IN_SSUBLL_OVERFLOW:
9447 case BUILT_IN_USUB_OVERFLOW:
9448 case BUILT_IN_USUBL_OVERFLOW:
9449 case BUILT_IN_USUBLL_OVERFLOW:
9450 opcode = MINUS_EXPR;
9451 ifn = IFN_SUB_OVERFLOW;
9452 break;
9453 case BUILT_IN_MUL_OVERFLOW_P:
9454 ovf_only = true;
9455 /* FALLTHRU */
9456 case BUILT_IN_MUL_OVERFLOW:
9457 case BUILT_IN_SMUL_OVERFLOW:
9458 case BUILT_IN_SMULL_OVERFLOW:
9459 case BUILT_IN_SMULLL_OVERFLOW:
9460 case BUILT_IN_UMUL_OVERFLOW:
9461 case BUILT_IN_UMULL_OVERFLOW:
9462 case BUILT_IN_UMULLL_OVERFLOW:
9463 opcode = MULT_EXPR;
9464 ifn = IFN_MUL_OVERFLOW;
9465 break;
9466 default:
9467 gcc_unreachable ();
9470 /* For the "generic" overloads, the first two arguments can have different
9471 types and the last argument determines the target type to use to check
9472 for overflow. The arguments of the other overloads all have the same
9473 type. */
9474 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
9476 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
9477 arguments are constant, attempt to fold the built-in call into a constant
9478 expression indicating whether or not it detected an overflow. */
9479 if (ovf_only
9480 && TREE_CODE (arg0) == INTEGER_CST
9481 && TREE_CODE (arg1) == INTEGER_CST)
9482 /* Perform the computation in the target type and check for overflow. */
9483 return omit_one_operand_loc (loc, boolean_type_node,
9484 arith_overflowed_p (opcode, type, arg0, arg1)
9485 ? boolean_true_node : boolean_false_node,
9486 arg2);
9488 tree intres, ovfres;
9489 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9491 intres = fold_binary_loc (loc, opcode, type,
9492 fold_convert_loc (loc, type, arg0),
9493 fold_convert_loc (loc, type, arg1));
9494 if (TREE_OVERFLOW (intres))
9495 intres = drop_tree_overflow (intres);
9496 ovfres = (arith_overflowed_p (opcode, type, arg0, arg1)
9497 ? boolean_true_node : boolean_false_node);
9499 else
9501 tree ctype = build_complex_type (type);
9502 tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
9503 arg0, arg1);
9504 tree tgt = save_expr (call);
9505 intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9506 ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9507 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
9510 if (ovf_only)
9511 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
9513 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
9514 tree store
9515 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
9516 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
9519 /* Fold a call to __builtin_FILE to a constant string. */
9521 static inline tree
9522 fold_builtin_FILE (location_t loc)
9524 if (const char *fname = LOCATION_FILE (loc))
9526 /* The documentation says this builtin is equivalent to the preprocessor
9527 __FILE__ macro so it appears appropriate to use the same file prefix
9528 mappings. */
9529 fname = remap_macro_filename (fname);
9530 return build_string_literal (fname);
9533 return build_string_literal ("");
9536 /* Fold a call to __builtin_FUNCTION to a constant string. */
9538 static inline tree
9539 fold_builtin_FUNCTION ()
9541 const char *name = "";
9543 if (current_function_decl)
9544 name = lang_hooks.decl_printable_name (current_function_decl, 0);
9546 return build_string_literal (name);
9549 /* Fold a call to __builtin_LINE to an integer constant. */
9551 static inline tree
9552 fold_builtin_LINE (location_t loc, tree type)
9554 return build_int_cst (type, LOCATION_LINE (loc));
9557 /* Fold a call to built-in function FNDECL with 0 arguments.
9558 This function returns NULL_TREE if no simplification was possible. */
9560 static tree
9561 fold_builtin_0 (location_t loc, tree fndecl)
9563 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9564 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9565 switch (fcode)
9567 case BUILT_IN_FILE:
9568 return fold_builtin_FILE (loc);
9570 case BUILT_IN_FUNCTION:
9571 return fold_builtin_FUNCTION ();
9573 case BUILT_IN_LINE:
9574 return fold_builtin_LINE (loc, type);
9576 CASE_FLT_FN (BUILT_IN_INF):
9577 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
9578 case BUILT_IN_INFD32:
9579 case BUILT_IN_INFD64:
9580 case BUILT_IN_INFD128:
9581 return fold_builtin_inf (loc, type, true);
9583 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9584 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
9585 return fold_builtin_inf (loc, type, false);
9587 case BUILT_IN_CLASSIFY_TYPE:
9588 return fold_builtin_classify_type (NULL_TREE);
9590 case BUILT_IN_UNREACHABLE:
9591 /* Rewrite any explicit calls to __builtin_unreachable. */
9592 if (sanitize_flags_p (SANITIZE_UNREACHABLE))
9593 return build_builtin_unreachable (loc);
9594 break;
9596 default:
9597 break;
9599 return NULL_TREE;
9602 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9603 This function returns NULL_TREE if no simplification was possible. */
9605 static tree
9606 fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
9608 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9609 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9611 if (TREE_CODE (arg0) == ERROR_MARK)
9612 return NULL_TREE;
9614 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
9615 return ret;
9617 switch (fcode)
9619 case BUILT_IN_CONSTANT_P:
9621 tree val = fold_builtin_constant_p (arg0);
9623 /* Gimplification will pull the CALL_EXPR for the builtin out of
9624 an if condition. When not optimizing, we'll not CSE it back.
9625 To avoid link error types of regressions, return false now. */
9626 if (!val && !optimize)
9627 val = integer_zero_node;
9629 return val;
9632 case BUILT_IN_CLASSIFY_TYPE:
9633 return fold_builtin_classify_type (arg0);
9635 case BUILT_IN_STRLEN:
9636 return fold_builtin_strlen (loc, expr, type, arg0);
9638 CASE_FLT_FN (BUILT_IN_FABS):
9639 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
9640 case BUILT_IN_FABSD32:
9641 case BUILT_IN_FABSD64:
9642 case BUILT_IN_FABSD128:
9643 return fold_builtin_fabs (loc, arg0, type);
9645 case BUILT_IN_ABS:
9646 case BUILT_IN_LABS:
9647 case BUILT_IN_LLABS:
9648 case BUILT_IN_IMAXABS:
9649 return fold_builtin_abs (loc, arg0, type);
9651 CASE_FLT_FN (BUILT_IN_CONJ):
9652 if (validate_arg (arg0, COMPLEX_TYPE)
9653 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9654 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9655 break;
9657 CASE_FLT_FN (BUILT_IN_CREAL):
9658 if (validate_arg (arg0, COMPLEX_TYPE)
9659 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9660 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9661 break;
9663 CASE_FLT_FN (BUILT_IN_CIMAG):
9664 if (validate_arg (arg0, COMPLEX_TYPE)
9665 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9666 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9667 break;
9669 CASE_FLT_FN (BUILT_IN_CARG):
9670 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CARG):
9671 return fold_builtin_carg (loc, arg0, type);
9673 case BUILT_IN_ISASCII:
9674 return fold_builtin_isascii (loc, arg0);
9676 case BUILT_IN_TOASCII:
9677 return fold_builtin_toascii (loc, arg0);
9679 case BUILT_IN_ISDIGIT:
9680 return fold_builtin_isdigit (loc, arg0);
9682 CASE_FLT_FN (BUILT_IN_FINITE):
9683 case BUILT_IN_FINITED32:
9684 case BUILT_IN_FINITED64:
9685 case BUILT_IN_FINITED128:
9686 case BUILT_IN_ISFINITE:
9688 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
9689 if (ret)
9690 return ret;
9691 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9694 CASE_FLT_FN (BUILT_IN_ISINF):
9695 case BUILT_IN_ISINFD32:
9696 case BUILT_IN_ISINFD64:
9697 case BUILT_IN_ISINFD128:
9699 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
9700 if (ret)
9701 return ret;
9702 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9705 case BUILT_IN_ISNORMAL:
9706 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9708 case BUILT_IN_ISINF_SIGN:
9709 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
9711 CASE_FLT_FN (BUILT_IN_ISNAN):
9712 case BUILT_IN_ISNAND32:
9713 case BUILT_IN_ISNAND64:
9714 case BUILT_IN_ISNAND128:
9715 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
9717 case BUILT_IN_ISSIGNALING:
9718 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISSIGNALING);
9720 case BUILT_IN_FREE:
9721 if (integer_zerop (arg0))
9722 return build_empty_stmt (loc);
9723 break;
9725 default:
9726 break;
9729 return NULL_TREE;
9733 /* Folds a call EXPR (which may be null) to built-in function FNDECL
9734 with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
9735 if no simplification was possible. */
9737 static tree
9738 fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
9740 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9741 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9743 if (TREE_CODE (arg0) == ERROR_MARK
9744 || TREE_CODE (arg1) == ERROR_MARK)
9745 return NULL_TREE;
9747 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
9748 return ret;
9750 switch (fcode)
9752 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
9753 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
9754 if (validate_arg (arg0, REAL_TYPE)
9755 && validate_arg (arg1, POINTER_TYPE))
9756 return do_mpfr_lgamma_r (arg0, arg1, type);
9757 break;
9759 CASE_FLT_FN (BUILT_IN_FREXP):
9760 return fold_builtin_frexp (loc, arg0, arg1, type);
9762 CASE_FLT_FN (BUILT_IN_MODF):
9763 return fold_builtin_modf (loc, arg0, arg1, type);
9765 case BUILT_IN_STRSPN:
9766 return fold_builtin_strspn (loc, expr, arg0, arg1);
9768 case BUILT_IN_STRCSPN:
9769 return fold_builtin_strcspn (loc, expr, arg0, arg1);
9771 case BUILT_IN_STRPBRK:
9772 return fold_builtin_strpbrk (loc, expr, arg0, arg1, type);
9774 case BUILT_IN_EXPECT:
9775 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE);
9777 case BUILT_IN_ISGREATER:
9778 return fold_builtin_unordered_cmp (loc, fndecl,
9779 arg0, arg1, UNLE_EXPR, LE_EXPR);
9780 case BUILT_IN_ISGREATEREQUAL:
9781 return fold_builtin_unordered_cmp (loc, fndecl,
9782 arg0, arg1, UNLT_EXPR, LT_EXPR);
9783 case BUILT_IN_ISLESS:
9784 return fold_builtin_unordered_cmp (loc, fndecl,
9785 arg0, arg1, UNGE_EXPR, GE_EXPR);
9786 case BUILT_IN_ISLESSEQUAL:
9787 return fold_builtin_unordered_cmp (loc, fndecl,
9788 arg0, arg1, UNGT_EXPR, GT_EXPR);
9789 case BUILT_IN_ISLESSGREATER:
9790 return fold_builtin_unordered_cmp (loc, fndecl,
9791 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
9792 case BUILT_IN_ISUNORDERED:
9793 return fold_builtin_unordered_cmp (loc, fndecl,
9794 arg0, arg1, UNORDERED_EXPR,
9795 NOP_EXPR);
9797 /* We do the folding for va_start in the expander. */
9798 case BUILT_IN_VA_START:
9799 break;
9801 case BUILT_IN_OBJECT_SIZE:
9802 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
9803 return fold_builtin_object_size (arg0, arg1, fcode);
9805 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
9806 return fold_builtin_atomic_always_lock_free (arg0, arg1);
9808 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
9809 return fold_builtin_atomic_is_lock_free (arg0, arg1);
9811 default:
9812 break;
9814 return NULL_TREE;
9817 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
9818 and ARG2.
9819 This function returns NULL_TREE if no simplification was possible. */
9821 static tree
9822 fold_builtin_3 (location_t loc, tree fndecl,
9823 tree arg0, tree arg1, tree arg2)
9825 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9826 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9828 if (TREE_CODE (arg0) == ERROR_MARK
9829 || TREE_CODE (arg1) == ERROR_MARK
9830 || TREE_CODE (arg2) == ERROR_MARK)
9831 return NULL_TREE;
9833 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
9834 arg0, arg1, arg2))
9835 return ret;
9837 switch (fcode)
9840 CASE_FLT_FN (BUILT_IN_SINCOS):
9841 return fold_builtin_sincos (loc, arg0, arg1, arg2);
9843 CASE_FLT_FN (BUILT_IN_REMQUO):
9844 if (validate_arg (arg0, REAL_TYPE)
9845 && validate_arg (arg1, REAL_TYPE)
9846 && validate_arg (arg2, POINTER_TYPE))
9847 return do_mpfr_remquo (arg0, arg1, arg2);
9848 break;
9850 case BUILT_IN_MEMCMP:
9851 return fold_builtin_memcmp (loc, arg0, arg1, arg2);
9853 case BUILT_IN_EXPECT:
9854 return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE);
9856 case BUILT_IN_EXPECT_WITH_PROBABILITY:
9857 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2);
9859 case BUILT_IN_ADD_OVERFLOW:
9860 case BUILT_IN_SUB_OVERFLOW:
9861 case BUILT_IN_MUL_OVERFLOW:
9862 case BUILT_IN_ADD_OVERFLOW_P:
9863 case BUILT_IN_SUB_OVERFLOW_P:
9864 case BUILT_IN_MUL_OVERFLOW_P:
9865 case BUILT_IN_SADD_OVERFLOW:
9866 case BUILT_IN_SADDL_OVERFLOW:
9867 case BUILT_IN_SADDLL_OVERFLOW:
9868 case BUILT_IN_SSUB_OVERFLOW:
9869 case BUILT_IN_SSUBL_OVERFLOW:
9870 case BUILT_IN_SSUBLL_OVERFLOW:
9871 case BUILT_IN_SMUL_OVERFLOW:
9872 case BUILT_IN_SMULL_OVERFLOW:
9873 case BUILT_IN_SMULLL_OVERFLOW:
9874 case BUILT_IN_UADD_OVERFLOW:
9875 case BUILT_IN_UADDL_OVERFLOW:
9876 case BUILT_IN_UADDLL_OVERFLOW:
9877 case BUILT_IN_USUB_OVERFLOW:
9878 case BUILT_IN_USUBL_OVERFLOW:
9879 case BUILT_IN_USUBLL_OVERFLOW:
9880 case BUILT_IN_UMUL_OVERFLOW:
9881 case BUILT_IN_UMULL_OVERFLOW:
9882 case BUILT_IN_UMULLL_OVERFLOW:
9883 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
9885 default:
9886 break;
9888 return NULL_TREE;
9891 /* Folds a call EXPR (which may be null) to built-in function FNDECL.
9892 ARGS is an array of NARGS arguments. IGNORE is true if the result
9893 of the function call is ignored. This function returns NULL_TREE
9894 if no simplification was possible. */
9896 static tree
9897 fold_builtin_n (location_t loc, tree expr, tree fndecl, tree *args,
9898 int nargs, bool)
9900 tree ret = NULL_TREE;
9902 switch (nargs)
9904 case 0:
9905 ret = fold_builtin_0 (loc, fndecl);
9906 break;
9907 case 1:
9908 ret = fold_builtin_1 (loc, expr, fndecl, args[0]);
9909 break;
9910 case 2:
9911 ret = fold_builtin_2 (loc, expr, fndecl, args[0], args[1]);
9912 break;
9913 case 3:
9914 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
9915 break;
9916 default:
9917 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
9918 break;
9920 if (ret)
9922 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
9923 SET_EXPR_LOCATION (ret, loc);
9924 return ret;
9926 return NULL_TREE;
9929 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
9930 list ARGS along with N new arguments in NEWARGS. SKIP is the number
9931 of arguments in ARGS to be omitted. OLDNARGS is the number of
9932 elements in ARGS. */
9934 static tree
9935 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
9936 int skip, tree fndecl, int n, va_list newargs)
9938 int nargs = oldnargs - skip + n;
9939 tree *buffer;
9941 if (n > 0)
9943 int i, j;
9945 buffer = XALLOCAVEC (tree, nargs);
9946 for (i = 0; i < n; i++)
9947 buffer[i] = va_arg (newargs, tree);
9948 for (j = skip; j < oldnargs; j++, i++)
9949 buffer[i] = args[j];
9951 else
9952 buffer = args + skip;
9954 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
9957 /* Return true if FNDECL shouldn't be folded right now.
9958 If a built-in function has an inline attribute always_inline
9959 wrapper, defer folding it after always_inline functions have
9960 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
9961 might not be performed. */
9963 bool
9964 avoid_folding_inline_builtin (tree fndecl)
9966 return (DECL_DECLARED_INLINE_P (fndecl)
9967 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
9968 && cfun
9969 && !cfun->always_inline_functions_inlined
9970 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
9973 /* A wrapper function for builtin folding that prevents warnings for
9974 "statement without effect" and the like, caused by removing the
9975 call node earlier than the warning is generated. */
9977 tree
9978 fold_call_expr (location_t loc, tree exp, bool ignore)
9980 tree ret = NULL_TREE;
9981 tree fndecl = get_callee_fndecl (exp);
9982 if (fndecl && fndecl_built_in_p (fndecl)
9983 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
9984 yet. Defer folding until we see all the arguments
9985 (after inlining). */
9986 && !CALL_EXPR_VA_ARG_PACK (exp))
9988 int nargs = call_expr_nargs (exp);
9990 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
9991 instead last argument is __builtin_va_arg_pack (). Defer folding
9992 even in that case, until arguments are finalized. */
9993 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
9995 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
9996 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
9997 return NULL_TREE;
10000 if (avoid_folding_inline_builtin (fndecl))
10001 return NULL_TREE;
10003 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10004 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
10005 CALL_EXPR_ARGP (exp), ignore);
10006 else
10008 tree *args = CALL_EXPR_ARGP (exp);
10009 ret = fold_builtin_n (loc, exp, fndecl, args, nargs, ignore);
10010 if (ret)
10011 return ret;
10014 return NULL_TREE;
10017 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
10018 N arguments are passed in the array ARGARRAY. Return a folded
10019 expression or NULL_TREE if no simplification was possible. */
10021 tree
10022 fold_builtin_call_array (location_t loc, tree,
10023 tree fn,
10024 int n,
10025 tree *argarray)
10027 if (TREE_CODE (fn) != ADDR_EXPR)
10028 return NULL_TREE;
10030 tree fndecl = TREE_OPERAND (fn, 0);
10031 if (TREE_CODE (fndecl) == FUNCTION_DECL
10032 && fndecl_built_in_p (fndecl))
10034 /* If last argument is __builtin_va_arg_pack (), arguments to this
10035 function are not finalized yet. Defer folding until they are. */
10036 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
10038 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
10039 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
10040 return NULL_TREE;
10042 if (avoid_folding_inline_builtin (fndecl))
10043 return NULL_TREE;
10044 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10045 return targetm.fold_builtin (fndecl, n, argarray, false);
10046 else
10047 return fold_builtin_n (loc, NULL_TREE, fndecl, argarray, n, false);
10050 return NULL_TREE;
10053 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
10054 along with N new arguments specified as the "..." parameters. SKIP
10055 is the number of arguments in EXP to be omitted. This function is used
10056 to do varargs-to-varargs transformations. */
10058 static tree
10059 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
10061 va_list ap;
10062 tree t;
10064 va_start (ap, n);
10065 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
10066 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
10067 va_end (ap);
10069 return t;
10072 /* Validate a single argument ARG against a tree code CODE representing
10073 a type. Return true when argument is valid. */
10075 static bool
10076 validate_arg (const_tree arg, enum tree_code code)
10078 if (!arg)
10079 return false;
10080 else if (code == POINTER_TYPE)
10081 return POINTER_TYPE_P (TREE_TYPE (arg));
10082 else if (code == INTEGER_TYPE)
10083 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
10084 return code == TREE_CODE (TREE_TYPE (arg));
10087 /* This function validates the types of a function call argument list
10088 against a specified list of tree_codes. If the last specifier is a 0,
10089 that represents an ellipses, otherwise the last specifier must be a
10090 VOID_TYPE.
10092 This is the GIMPLE version of validate_arglist. Eventually we want to
10093 completely convert builtins.cc to work from GIMPLEs and the tree based
10094 validate_arglist will then be removed. */
10096 bool
10097 validate_gimple_arglist (const gcall *call, ...)
10099 enum tree_code code;
10100 bool res = 0;
10101 va_list ap;
10102 const_tree arg;
10103 size_t i;
10105 va_start (ap, call);
10106 i = 0;
10110 code = (enum tree_code) va_arg (ap, int);
10111 switch (code)
10113 case 0:
10114 /* This signifies an ellipses, any further arguments are all ok. */
10115 res = true;
10116 goto end;
10117 case VOID_TYPE:
10118 /* This signifies an endlink, if no arguments remain, return
10119 true, otherwise return false. */
10120 res = (i == gimple_call_num_args (call));
10121 goto end;
10122 default:
10123 /* If no parameters remain or the parameter's code does not
10124 match the specified code, return false. Otherwise continue
10125 checking any remaining arguments. */
10126 arg = gimple_call_arg (call, i++);
10127 if (!validate_arg (arg, code))
10128 goto end;
10129 break;
10132 while (1);
10134 /* We need gotos here since we can only have one VA_CLOSE in a
10135 function. */
10136 end: ;
10137 va_end (ap);
10139 return res;
10142 /* Default target-specific builtin expander that does nothing. */
10145 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
10146 rtx target ATTRIBUTE_UNUSED,
10147 rtx subtarget ATTRIBUTE_UNUSED,
10148 machine_mode mode ATTRIBUTE_UNUSED,
10149 int ignore ATTRIBUTE_UNUSED)
10151 return NULL_RTX;
10154 /* Returns true is EXP represents data that would potentially reside
10155 in a readonly section. */
10157 bool
10158 readonly_data_expr (tree exp)
10160 STRIP_NOPS (exp);
10162 if (TREE_CODE (exp) != ADDR_EXPR)
10163 return false;
10165 exp = get_base_address (TREE_OPERAND (exp, 0));
10166 if (!exp)
10167 return false;
10169 /* Make sure we call decl_readonly_section only for trees it
10170 can handle (since it returns true for everything it doesn't
10171 understand). */
10172 if (TREE_CODE (exp) == STRING_CST
10173 || TREE_CODE (exp) == CONSTRUCTOR
10174 || (VAR_P (exp) && TREE_STATIC (exp)))
10175 return decl_readonly_section (exp, 0);
10176 else
10177 return false;
10180 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
10181 to the call, and TYPE is its return type.
10183 Return NULL_TREE if no simplification was possible, otherwise return the
10184 simplified form of the call as a tree.
10186 The simplified form may be a constant or other expression which
10187 computes the same value, but in a more efficient manner (including
10188 calls to other builtin functions).
10190 The call may contain arguments which need to be evaluated, but
10191 which are not useful to determine the result of the call. In
10192 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10193 COMPOUND_EXPR will be an argument which must be evaluated.
10194 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10195 COMPOUND_EXPR in the chain will contain the tree for the simplified
10196 form of the builtin function call. */
10198 static tree
10199 fold_builtin_strpbrk (location_t loc, tree, tree s1, tree s2, tree type)
10201 if (!validate_arg (s1, POINTER_TYPE)
10202 || !validate_arg (s2, POINTER_TYPE))
10203 return NULL_TREE;
10205 tree fn;
10206 const char *p1, *p2;
10208 p2 = c_getstr (s2);
10209 if (p2 == NULL)
10210 return NULL_TREE;
10212 p1 = c_getstr (s1);
10213 if (p1 != NULL)
10215 const char *r = strpbrk (p1, p2);
10216 tree tem;
10218 if (r == NULL)
10219 return build_int_cst (TREE_TYPE (s1), 0);
10221 /* Return an offset into the constant string argument. */
10222 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
10223 return fold_convert_loc (loc, type, tem);
10226 if (p2[0] == '\0')
10227 /* strpbrk(x, "") == NULL.
10228 Evaluate and ignore s1 in case it had side-effects. */
10229 return omit_one_operand_loc (loc, type, integer_zero_node, s1);
10231 if (p2[1] != '\0')
10232 return NULL_TREE; /* Really call strpbrk. */
10234 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
10235 if (!fn)
10236 return NULL_TREE;
10238 /* New argument list transforming strpbrk(s1, s2) to
10239 strchr(s1, s2[0]). */
10240 return build_call_expr_loc (loc, fn, 2, s1,
10241 build_int_cst (integer_type_node, p2[0]));
10244 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
10245 to the call.
10247 Return NULL_TREE if no simplification was possible, otherwise return the
10248 simplified form of the call as a tree.
10250 The simplified form may be a constant or other expression which
10251 computes the same value, but in a more efficient manner (including
10252 calls to other builtin functions).
10254 The call may contain arguments which need to be evaluated, but
10255 which are not useful to determine the result of the call. In
10256 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10257 COMPOUND_EXPR will be an argument which must be evaluated.
10258 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10259 COMPOUND_EXPR in the chain will contain the tree for the simplified
10260 form of the builtin function call. */
10262 static tree
10263 fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2)
10265 if (!validate_arg (s1, POINTER_TYPE)
10266 || !validate_arg (s2, POINTER_TYPE))
10267 return NULL_TREE;
10269 if (!check_nul_terminated_array (expr, s1)
10270 || !check_nul_terminated_array (expr, s2))
10271 return NULL_TREE;
10273 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
10275 /* If either argument is "", return NULL_TREE. */
10276 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
10277 /* Evaluate and ignore both arguments in case either one has
10278 side-effects. */
10279 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
10280 s1, s2);
10281 return NULL_TREE;
10284 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
10285 to the call.
10287 Return NULL_TREE if no simplification was possible, otherwise return the
10288 simplified form of the call as a tree.
10290 The simplified form may be a constant or other expression which
10291 computes the same value, but in a more efficient manner (including
10292 calls to other builtin functions).
10294 The call may contain arguments which need to be evaluated, but
10295 which are not useful to determine the result of the call. In
10296 this case we return a chain of COMPOUND_EXPRs. The LHS of each
10297 COMPOUND_EXPR will be an argument which must be evaluated.
10298 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
10299 COMPOUND_EXPR in the chain will contain the tree for the simplified
10300 form of the builtin function call. */
10302 static tree
10303 fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2)
10305 if (!validate_arg (s1, POINTER_TYPE)
10306 || !validate_arg (s2, POINTER_TYPE))
10307 return NULL_TREE;
10309 if (!check_nul_terminated_array (expr, s1)
10310 || !check_nul_terminated_array (expr, s2))
10311 return NULL_TREE;
10313 /* If the first argument is "", return NULL_TREE. */
10314 const char *p1 = c_getstr (s1);
10315 if (p1 && *p1 == '\0')
10317 /* Evaluate and ignore argument s2 in case it has
10318 side-effects. */
10319 return omit_one_operand_loc (loc, size_type_node,
10320 size_zero_node, s2);
10323 /* If the second argument is "", return __builtin_strlen(s1). */
10324 const char *p2 = c_getstr (s2);
10325 if (p2 && *p2 == '\0')
10327 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
10329 /* If the replacement _DECL isn't initialized, don't do the
10330 transformation. */
10331 if (!fn)
10332 return NULL_TREE;
10334 return build_call_expr_loc (loc, fn, 1, s1);
10336 return NULL_TREE;
10339 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
10340 produced. False otherwise. This is done so that we don't output the error
10341 or warning twice or three times. */
10343 bool
10344 fold_builtin_next_arg (tree exp, bool va_start_p)
10346 tree fntype = TREE_TYPE (current_function_decl);
10347 int nargs = call_expr_nargs (exp);
10348 tree arg;
10349 /* There is good chance the current input_location points inside the
10350 definition of the va_start macro (perhaps on the token for
10351 builtin) in a system header, so warnings will not be emitted.
10352 Use the location in real source code. */
10353 location_t current_location =
10354 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
10355 NULL);
10357 if (!stdarg_p (fntype))
10359 error ("%<va_start%> used in function with fixed arguments");
10360 return true;
10363 if (va_start_p)
10365 if (va_start_p && (nargs != 2))
10367 error ("wrong number of arguments to function %<va_start%>");
10368 return true;
10370 arg = CALL_EXPR_ARG (exp, 1);
10372 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10373 when we checked the arguments and if needed issued a warning. */
10374 else
10376 if (nargs == 0)
10378 /* Evidently an out of date version of <stdarg.h>; can't validate
10379 va_start's second argument, but can still work as intended. */
10380 warning_at (current_location,
10381 OPT_Wvarargs,
10382 "%<__builtin_next_arg%> called without an argument");
10383 return true;
10385 else if (nargs > 1)
10387 error ("wrong number of arguments to function %<__builtin_next_arg%>");
10388 return true;
10390 arg = CALL_EXPR_ARG (exp, 0);
10393 if (TREE_CODE (arg) == SSA_NAME
10394 && SSA_NAME_VAR (arg))
10395 arg = SSA_NAME_VAR (arg);
10397 /* We destructively modify the call to be __builtin_va_start (ap, 0)
10398 or __builtin_next_arg (0) the first time we see it, after checking
10399 the arguments and if needed issuing a warning. */
10400 if (!integer_zerop (arg))
10402 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10404 /* Strip off all nops for the sake of the comparison. This
10405 is not quite the same as STRIP_NOPS. It does more.
10406 We must also strip off INDIRECT_EXPR for C++ reference
10407 parameters. */
10408 while (CONVERT_EXPR_P (arg)
10409 || TREE_CODE (arg) == INDIRECT_REF)
10410 arg = TREE_OPERAND (arg, 0);
10411 if (arg != last_parm)
10413 /* FIXME: Sometimes with the tree optimizers we can get the
10414 not the last argument even though the user used the last
10415 argument. We just warn and set the arg to be the last
10416 argument so that we will get wrong-code because of
10417 it. */
10418 warning_at (current_location,
10419 OPT_Wvarargs,
10420 "second parameter of %<va_start%> not last named argument");
10423 /* Undefined by C99 7.15.1.4p4 (va_start):
10424 "If the parameter parmN is declared with the register storage
10425 class, with a function or array type, or with a type that is
10426 not compatible with the type that results after application of
10427 the default argument promotions, the behavior is undefined."
10429 else if (DECL_REGISTER (arg))
10431 warning_at (current_location,
10432 OPT_Wvarargs,
10433 "undefined behavior when second parameter of "
10434 "%<va_start%> is declared with %<register%> storage");
10437 /* We want to verify the second parameter just once before the tree
10438 optimizers are run and then avoid keeping it in the tree,
10439 as otherwise we could warn even for correct code like:
10440 void foo (int i, ...)
10441 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10442 if (va_start_p)
10443 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
10444 else
10445 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
10447 return false;
10451 /* Expand a call EXP to __builtin_object_size. */
10453 static rtx
10454 expand_builtin_object_size (tree exp)
10456 tree ost;
10457 int object_size_type;
10458 tree fndecl = get_callee_fndecl (exp);
10460 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10462 error ("first argument of %qD must be a pointer, second integer constant",
10463 fndecl);
10464 expand_builtin_trap ();
10465 return const0_rtx;
10468 ost = CALL_EXPR_ARG (exp, 1);
10469 STRIP_NOPS (ost);
10471 if (TREE_CODE (ost) != INTEGER_CST
10472 || tree_int_cst_sgn (ost) < 0
10473 || compare_tree_int (ost, 3) > 0)
10475 error ("last argument of %qD is not integer constant between 0 and 3",
10476 fndecl);
10477 expand_builtin_trap ();
10478 return const0_rtx;
10481 object_size_type = tree_to_shwi (ost);
10483 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10486 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10487 FCODE is the BUILT_IN_* to use.
10488 Return NULL_RTX if we failed; the caller should emit a normal call,
10489 otherwise try to get the result in TARGET, if convenient (and in
10490 mode MODE if that's convenient). */
10492 static rtx
10493 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
10494 enum built_in_function fcode)
10496 if (!validate_arglist (exp,
10497 POINTER_TYPE,
10498 fcode == BUILT_IN_MEMSET_CHK
10499 ? INTEGER_TYPE : POINTER_TYPE,
10500 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10501 return NULL_RTX;
10503 tree dest = CALL_EXPR_ARG (exp, 0);
10504 tree src = CALL_EXPR_ARG (exp, 1);
10505 tree len = CALL_EXPR_ARG (exp, 2);
10506 tree size = CALL_EXPR_ARG (exp, 3);
10508 /* FIXME: Set access mode to write only for memset et al. */
10509 bool sizes_ok = check_access (exp, len, /*maxread=*/NULL_TREE,
10510 /*srcstr=*/NULL_TREE, size, access_read_write);
10512 if (!tree_fits_uhwi_p (size))
10513 return NULL_RTX;
10515 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
10517 /* Avoid transforming the checking call to an ordinary one when
10518 an overflow has been detected or when the call couldn't be
10519 validated because the size is not constant. */
10520 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
10521 return NULL_RTX;
10523 tree fn = NULL_TREE;
10524 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10525 mem{cpy,pcpy,move,set} is available. */
10526 switch (fcode)
10528 case BUILT_IN_MEMCPY_CHK:
10529 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
10530 break;
10531 case BUILT_IN_MEMPCPY_CHK:
10532 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
10533 break;
10534 case BUILT_IN_MEMMOVE_CHK:
10535 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
10536 break;
10537 case BUILT_IN_MEMSET_CHK:
10538 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
10539 break;
10540 default:
10541 break;
10544 if (! fn)
10545 return NULL_RTX;
10547 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
10548 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10549 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10550 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10552 else if (fcode == BUILT_IN_MEMSET_CHK)
10553 return NULL_RTX;
10554 else
10556 unsigned int dest_align = get_pointer_alignment (dest);
10558 /* If DEST is not a pointer type, call the normal function. */
10559 if (dest_align == 0)
10560 return NULL_RTX;
10562 /* If SRC and DEST are the same (and not volatile), do nothing. */
10563 if (operand_equal_p (src, dest, 0))
10565 tree expr;
10567 if (fcode != BUILT_IN_MEMPCPY_CHK)
10569 /* Evaluate and ignore LEN in case it has side-effects. */
10570 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10571 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10574 expr = fold_build_pointer_plus (dest, len);
10575 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10578 /* __memmove_chk special case. */
10579 if (fcode == BUILT_IN_MEMMOVE_CHK)
10581 unsigned int src_align = get_pointer_alignment (src);
10583 if (src_align == 0)
10584 return NULL_RTX;
10586 /* If src is categorized for a readonly section we can use
10587 normal __memcpy_chk. */
10588 if (readonly_data_expr (src))
10590 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
10591 if (!fn)
10592 return NULL_RTX;
10593 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
10594 dest, src, len, size);
10595 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10596 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10597 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10600 return NULL_RTX;
10604 /* Emit warning if a buffer overflow is detected at compile time. */
10606 static void
10607 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10609 /* The source string. */
10610 tree srcstr = NULL_TREE;
10611 /* The size of the destination object returned by __builtin_object_size. */
10612 tree objsize = NULL_TREE;
10613 /* The string that is being concatenated with (as in __strcat_chk)
10614 or null if it isn't. */
10615 tree catstr = NULL_TREE;
10616 /* The maximum length of the source sequence in a bounded operation
10617 (such as __strncat_chk) or null if the operation isn't bounded
10618 (such as __strcat_chk). */
10619 tree maxread = NULL_TREE;
10620 /* The exact size of the access (such as in __strncpy_chk). */
10621 tree size = NULL_TREE;
10622 /* The access by the function that's checked. Except for snprintf
10623 both writing and reading is checked. */
10624 access_mode mode = access_read_write;
10626 switch (fcode)
10628 case BUILT_IN_STRCPY_CHK:
10629 case BUILT_IN_STPCPY_CHK:
10630 srcstr = CALL_EXPR_ARG (exp, 1);
10631 objsize = CALL_EXPR_ARG (exp, 2);
10632 break;
10634 case BUILT_IN_STRCAT_CHK:
10635 /* For __strcat_chk the warning will be emitted only if overflowing
10636 by at least strlen (dest) + 1 bytes. */
10637 catstr = CALL_EXPR_ARG (exp, 0);
10638 srcstr = CALL_EXPR_ARG (exp, 1);
10639 objsize = CALL_EXPR_ARG (exp, 2);
10640 break;
10642 case BUILT_IN_STRNCAT_CHK:
10643 catstr = CALL_EXPR_ARG (exp, 0);
10644 srcstr = CALL_EXPR_ARG (exp, 1);
10645 maxread = CALL_EXPR_ARG (exp, 2);
10646 objsize = CALL_EXPR_ARG (exp, 3);
10647 break;
10649 case BUILT_IN_STRNCPY_CHK:
10650 case BUILT_IN_STPNCPY_CHK:
10651 srcstr = CALL_EXPR_ARG (exp, 1);
10652 size = CALL_EXPR_ARG (exp, 2);
10653 objsize = CALL_EXPR_ARG (exp, 3);
10654 break;
10656 case BUILT_IN_SNPRINTF_CHK:
10657 case BUILT_IN_VSNPRINTF_CHK:
10658 maxread = CALL_EXPR_ARG (exp, 1);
10659 objsize = CALL_EXPR_ARG (exp, 3);
10660 /* The only checked access the write to the destination. */
10661 mode = access_write_only;
10662 break;
10663 default:
10664 gcc_unreachable ();
10667 if (catstr && maxread)
10669 /* Check __strncat_chk. There is no way to determine the length
10670 of the string to which the source string is being appended so
10671 just warn when the length of the source string is not known. */
10672 check_strncat_sizes (exp, objsize);
10673 return;
10676 check_access (exp, size, maxread, srcstr, objsize, mode);
10679 /* Emit warning if a buffer overflow is detected at compile time
10680 in __sprintf_chk/__vsprintf_chk calls. */
10682 static void
10683 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10685 tree size, len, fmt;
10686 const char *fmt_str;
10687 int nargs = call_expr_nargs (exp);
10689 /* Verify the required arguments in the original call. */
10691 if (nargs < 4)
10692 return;
10693 size = CALL_EXPR_ARG (exp, 2);
10694 fmt = CALL_EXPR_ARG (exp, 3);
10696 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
10697 return;
10699 /* Check whether the format is a literal string constant. */
10700 fmt_str = c_getstr (fmt);
10701 if (fmt_str == NULL)
10702 return;
10704 if (!init_target_chars ())
10705 return;
10707 /* If the format doesn't contain % args or %%, we know its size. */
10708 if (strchr (fmt_str, target_percent) == 0)
10709 len = build_int_cstu (size_type_node, strlen (fmt_str));
10710 /* If the format is "%s" and first ... argument is a string literal,
10711 we know it too. */
10712 else if (fcode == BUILT_IN_SPRINTF_CHK
10713 && strcmp (fmt_str, target_percent_s) == 0)
10715 tree arg;
10717 if (nargs < 5)
10718 return;
10719 arg = CALL_EXPR_ARG (exp, 4);
10720 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10721 return;
10723 len = c_strlen (arg, 1);
10724 if (!len || ! tree_fits_uhwi_p (len))
10725 return;
10727 else
10728 return;
10730 /* Add one for the terminating nul. */
10731 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
10733 check_access (exp, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, len, size,
10734 access_write_only);
10737 /* Fold a call to __builtin_object_size with arguments PTR and OST,
10738 if possible. */
10740 static tree
10741 fold_builtin_object_size (tree ptr, tree ost, enum built_in_function fcode)
10743 tree bytes;
10744 int object_size_type;
10746 if (!validate_arg (ptr, POINTER_TYPE)
10747 || !validate_arg (ost, INTEGER_TYPE))
10748 return NULL_TREE;
10750 STRIP_NOPS (ost);
10752 if (TREE_CODE (ost) != INTEGER_CST
10753 || tree_int_cst_sgn (ost) < 0
10754 || compare_tree_int (ost, 3) > 0)
10755 return NULL_TREE;
10757 object_size_type = tree_to_shwi (ost);
10759 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10760 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10761 and (size_t) 0 for types 2 and 3. */
10762 if (TREE_SIDE_EFFECTS (ptr))
10763 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10765 if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
10766 object_size_type |= OST_DYNAMIC;
10768 if (TREE_CODE (ptr) == ADDR_EXPR)
10770 compute_builtin_object_size (ptr, object_size_type, &bytes);
10771 if ((object_size_type & OST_DYNAMIC)
10772 || int_fits_type_p (bytes, size_type_node))
10773 return fold_convert (size_type_node, bytes);
10775 else if (TREE_CODE (ptr) == SSA_NAME)
10777 /* If object size is not known yet, delay folding until
10778 later. Maybe subsequent passes will help determining
10779 it. */
10780 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
10781 && ((object_size_type & OST_DYNAMIC)
10782 || int_fits_type_p (bytes, size_type_node)))
10783 return fold_convert (size_type_node, bytes);
10786 return NULL_TREE;
10789 /* Builtins with folding operations that operate on "..." arguments
10790 need special handling; we need to store the arguments in a convenient
10791 data structure before attempting any folding. Fortunately there are
10792 only a few builtins that fall into this category. FNDECL is the
10793 function, EXP is the CALL_EXPR for the call. */
10795 static tree
10796 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
10798 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10799 tree ret = NULL_TREE;
10801 switch (fcode)
10803 case BUILT_IN_FPCLASSIFY:
10804 ret = fold_builtin_fpclassify (loc, args, nargs);
10805 break;
10807 default:
10808 break;
10810 if (ret)
10812 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10813 SET_EXPR_LOCATION (ret, loc);
10814 suppress_warning (ret);
10815 return ret;
10817 return NULL_TREE;
10820 /* Initialize format string characters in the target charset. */
10822 bool
10823 init_target_chars (void)
10825 static bool init;
10826 if (!init)
10828 target_newline = lang_hooks.to_target_charset ('\n');
10829 target_percent = lang_hooks.to_target_charset ('%');
10830 target_c = lang_hooks.to_target_charset ('c');
10831 target_s = lang_hooks.to_target_charset ('s');
10832 if (target_newline == 0 || target_percent == 0 || target_c == 0
10833 || target_s == 0)
10834 return false;
10836 target_percent_c[0] = target_percent;
10837 target_percent_c[1] = target_c;
10838 target_percent_c[2] = '\0';
10840 target_percent_s[0] = target_percent;
10841 target_percent_s[1] = target_s;
10842 target_percent_s[2] = '\0';
10844 target_percent_s_newline[0] = target_percent;
10845 target_percent_s_newline[1] = target_s;
10846 target_percent_s_newline[2] = target_newline;
10847 target_percent_s_newline[3] = '\0';
10849 init = true;
10851 return true;
10854 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
10855 and no overflow/underflow occurred. INEXACT is true if M was not
10856 exactly calculated. TYPE is the tree type for the result. This
10857 function assumes that you cleared the MPFR flags and then
10858 calculated M to see if anything subsequently set a flag prior to
10859 entering this function. Return NULL_TREE if any checks fail. */
10861 static tree
10862 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
10864 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10865 overflow/underflow occurred. If -frounding-math, proceed iff the
10866 result of calling FUNC was exact. */
10867 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
10868 && (!flag_rounding_math || !inexact))
10870 REAL_VALUE_TYPE rr;
10872 real_from_mpfr (&rr, m, type, MPFR_RNDN);
10873 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
10874 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10875 but the mpft_t is not, then we underflowed in the
10876 conversion. */
10877 if (real_isfinite (&rr)
10878 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
10880 REAL_VALUE_TYPE rmode;
10882 real_convert (&rmode, TYPE_MODE (type), &rr);
10883 /* Proceed iff the specified mode can hold the value. */
10884 if (real_identical (&rmode, &rr))
10885 return build_real (type, rmode);
10888 return NULL_TREE;
10891 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
10892 number and no overflow/underflow occurred. INEXACT is true if M
10893 was not exactly calculated. TYPE is the tree type for the result.
10894 This function assumes that you cleared the MPFR flags and then
10895 calculated M to see if anything subsequently set a flag prior to
10896 entering this function. Return NULL_TREE if any checks fail, if
10897 FORCE_CONVERT is true, then bypass the checks. */
10899 static tree
10900 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
10902 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10903 overflow/underflow occurred. If -frounding-math, proceed iff the
10904 result of calling FUNC was exact. */
10905 if (force_convert
10906 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
10907 && !mpfr_overflow_p () && !mpfr_underflow_p ()
10908 && (!flag_rounding_math || !inexact)))
10910 REAL_VALUE_TYPE re, im;
10912 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), MPFR_RNDN);
10913 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), MPFR_RNDN);
10914 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
10915 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10916 but the mpft_t is not, then we underflowed in the
10917 conversion. */
10918 if (force_convert
10919 || (real_isfinite (&re) && real_isfinite (&im)
10920 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
10921 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
10923 REAL_VALUE_TYPE re_mode, im_mode;
10925 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
10926 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
10927 /* Proceed iff the specified mode can hold the value. */
10928 if (force_convert
10929 || (real_identical (&re_mode, &re)
10930 && real_identical (&im_mode, &im)))
10931 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
10932 build_real (TREE_TYPE (type), im_mode));
10935 return NULL_TREE;
10938 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
10939 the pointer *(ARG_QUO) and return the result. The type is taken
10940 from the type of ARG0 and is used for setting the precision of the
10941 calculation and results. */
10943 static tree
10944 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
10946 tree const type = TREE_TYPE (arg0);
10947 tree result = NULL_TREE;
10949 STRIP_NOPS (arg0);
10950 STRIP_NOPS (arg1);
10952 /* To proceed, MPFR must exactly represent the target floating point
10953 format, which only happens when the target base equals two. */
10954 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10955 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
10956 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
10958 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
10959 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
10961 if (real_isfinite (ra0) && real_isfinite (ra1))
10963 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10964 const int prec = fmt->p;
10965 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
10966 tree result_rem;
10967 long integer_quo;
10968 mpfr_t m0, m1;
10970 mpfr_inits2 (prec, m0, m1, NULL);
10971 mpfr_from_real (m0, ra0, MPFR_RNDN);
10972 mpfr_from_real (m1, ra1, MPFR_RNDN);
10973 mpfr_clear_flags ();
10974 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
10975 /* Remquo is independent of the rounding mode, so pass
10976 inexact=0 to do_mpfr_ckconv(). */
10977 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
10978 mpfr_clears (m0, m1, NULL);
10979 if (result_rem)
10981 /* MPFR calculates quo in the host's long so it may
10982 return more bits in quo than the target int can hold
10983 if sizeof(host long) > sizeof(target int). This can
10984 happen even for native compilers in LP64 mode. In
10985 these cases, modulo the quo value with the largest
10986 number that the target int can hold while leaving one
10987 bit for the sign. */
10988 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
10989 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
10991 /* Dereference the quo pointer argument. */
10992 arg_quo = build_fold_indirect_ref (arg_quo);
10993 /* Proceed iff a valid pointer type was passed in. */
10994 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
10996 /* Set the value. */
10997 tree result_quo
10998 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
10999 build_int_cst (TREE_TYPE (arg_quo),
11000 integer_quo));
11001 TREE_SIDE_EFFECTS (result_quo) = 1;
11002 /* Combine the quo assignment with the rem. */
11003 result = fold_build2 (COMPOUND_EXPR, type,
11004 result_quo, result_rem);
11005 suppress_warning (result, OPT_Wunused_value);
11006 result = non_lvalue (result);
11011 return result;
11014 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
11015 resulting value as a tree with type TYPE. The mpfr precision is
11016 set to the precision of TYPE. We assume that this mpfr function
11017 returns zero if the result could be calculated exactly within the
11018 requested precision. In addition, the integer pointer represented
11019 by ARG_SG will be dereferenced and set to the appropriate signgam
11020 (-1,1) value. */
11022 static tree
11023 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
11025 tree result = NULL_TREE;
11027 STRIP_NOPS (arg);
11029 /* To proceed, MPFR must exactly represent the target floating point
11030 format, which only happens when the target base equals two. Also
11031 verify ARG is a constant and that ARG_SG is an int pointer. */
11032 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
11033 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
11034 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
11035 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
11037 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
11039 /* In addition to NaN and Inf, the argument cannot be zero or a
11040 negative integer. */
11041 if (real_isfinite (ra)
11042 && ra->cl != rvc_zero
11043 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
11045 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
11046 const int prec = fmt->p;
11047 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
11048 int inexact, sg;
11049 mpfr_t m;
11050 tree result_lg;
11052 mpfr_init2 (m, prec);
11053 mpfr_from_real (m, ra, MPFR_RNDN);
11054 mpfr_clear_flags ();
11055 inexact = mpfr_lgamma (m, &sg, m, rnd);
11056 result_lg = do_mpfr_ckconv (m, type, inexact);
11057 mpfr_clear (m);
11058 if (result_lg)
11060 tree result_sg;
11062 /* Dereference the arg_sg pointer argument. */
11063 arg_sg = build_fold_indirect_ref (arg_sg);
11064 /* Assign the signgam value into *arg_sg. */
11065 result_sg = fold_build2 (MODIFY_EXPR,
11066 TREE_TYPE (arg_sg), arg_sg,
11067 build_int_cst (TREE_TYPE (arg_sg), sg));
11068 TREE_SIDE_EFFECTS (result_sg) = 1;
11069 /* Combine the signgam assignment with the lgamma result. */
11070 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
11071 result_sg, result_lg));
11076 return result;
11079 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
11080 mpc function FUNC on it and return the resulting value as a tree
11081 with type TYPE. The mpfr precision is set to the precision of
11082 TYPE. We assume that function FUNC returns zero if the result
11083 could be calculated exactly within the requested precision. If
11084 DO_NONFINITE is true, then fold expressions containing Inf or NaN
11085 in the arguments and/or results. */
11087 tree
11088 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
11089 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
11091 tree result = NULL_TREE;
11093 STRIP_NOPS (arg0);
11094 STRIP_NOPS (arg1);
11096 /* To proceed, MPFR must exactly represent the target floating point
11097 format, which only happens when the target base equals two. */
11098 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
11099 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
11100 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
11101 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
11102 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
11104 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
11105 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
11106 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
11107 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
11109 if (do_nonfinite
11110 || (real_isfinite (re0) && real_isfinite (im0)
11111 && real_isfinite (re1) && real_isfinite (im1)))
11113 const struct real_format *const fmt =
11114 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
11115 const int prec = fmt->p;
11116 const mpfr_rnd_t rnd = fmt->round_towards_zero
11117 ? MPFR_RNDZ : MPFR_RNDN;
11118 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
11119 int inexact;
11120 mpc_t m0, m1;
11122 mpc_init2 (m0, prec);
11123 mpc_init2 (m1, prec);
11124 mpfr_from_real (mpc_realref (m0), re0, rnd);
11125 mpfr_from_real (mpc_imagref (m0), im0, rnd);
11126 mpfr_from_real (mpc_realref (m1), re1, rnd);
11127 mpfr_from_real (mpc_imagref (m1), im1, rnd);
11128 mpfr_clear_flags ();
11129 inexact = func (m0, m0, m1, crnd);
11130 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
11131 mpc_clear (m0);
11132 mpc_clear (m1);
11136 return result;
11139 /* A wrapper function for builtin folding that prevents warnings for
11140 "statement without effect" and the like, caused by removing the
11141 call node earlier than the warning is generated. */
11143 tree
11144 fold_call_stmt (gcall *stmt, bool ignore)
11146 tree ret = NULL_TREE;
11147 tree fndecl = gimple_call_fndecl (stmt);
11148 location_t loc = gimple_location (stmt);
11149 if (fndecl && fndecl_built_in_p (fndecl)
11150 && !gimple_call_va_arg_pack_p (stmt))
11152 int nargs = gimple_call_num_args (stmt);
11153 tree *args = (nargs > 0
11154 ? gimple_call_arg_ptr (stmt, 0)
11155 : &error_mark_node);
11157 if (avoid_folding_inline_builtin (fndecl))
11158 return NULL_TREE;
11159 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
11161 return targetm.fold_builtin (fndecl, nargs, args, ignore);
11163 else
11165 ret = fold_builtin_n (loc, NULL_TREE, fndecl, args, nargs, ignore);
11166 if (ret)
11168 /* Propagate location information from original call to
11169 expansion of builtin. Otherwise things like
11170 maybe_emit_chk_warning, that operate on the expansion
11171 of a builtin, will use the wrong location information. */
11172 if (gimple_has_location (stmt))
11174 tree realret = ret;
11175 if (TREE_CODE (ret) == NOP_EXPR)
11176 realret = TREE_OPERAND (ret, 0);
11177 if (CAN_HAVE_LOCATION_P (realret)
11178 && !EXPR_HAS_LOCATION (realret))
11179 SET_EXPR_LOCATION (realret, loc);
11180 return realret;
11182 return ret;
11186 return NULL_TREE;
11189 /* Look up the function in builtin_decl that corresponds to DECL
11190 and set ASMSPEC as its user assembler name. DECL must be a
11191 function decl that declares a builtin. */
11193 void
11194 set_builtin_user_assembler_name (tree decl, const char *asmspec)
11196 gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL)
11197 && asmspec != 0);
11199 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
11200 set_user_assembler_name (builtin, asmspec);
11202 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
11203 && INT_TYPE_SIZE < BITS_PER_WORD)
11205 scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
11206 set_user_assembler_libfunc ("ffs", asmspec);
11207 set_optab_libfunc (ffs_optab, mode, "ffs");
11211 /* Return true if DECL is a builtin that expands to a constant or similarly
11212 simple code. */
11213 bool
11214 is_simple_builtin (tree decl)
11216 if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
11217 switch (DECL_FUNCTION_CODE (decl))
11219 /* Builtins that expand to constants. */
11220 case BUILT_IN_CONSTANT_P:
11221 case BUILT_IN_EXPECT:
11222 case BUILT_IN_OBJECT_SIZE:
11223 case BUILT_IN_UNREACHABLE:
11224 /* Simple register moves or loads from stack. */
11225 case BUILT_IN_ASSUME_ALIGNED:
11226 case BUILT_IN_RETURN_ADDRESS:
11227 case BUILT_IN_EXTRACT_RETURN_ADDR:
11228 case BUILT_IN_FROB_RETURN_ADDR:
11229 case BUILT_IN_RETURN:
11230 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
11231 case BUILT_IN_FRAME_ADDRESS:
11232 case BUILT_IN_VA_END:
11233 case BUILT_IN_STACK_SAVE:
11234 case BUILT_IN_STACK_RESTORE:
11235 case BUILT_IN_DWARF_CFA:
11236 /* Exception state returns or moves registers around. */
11237 case BUILT_IN_EH_FILTER:
11238 case BUILT_IN_EH_POINTER:
11239 case BUILT_IN_EH_COPY_VALUES:
11240 return true;
11242 default:
11243 return false;
11246 return false;
11249 /* Return true if DECL is a builtin that is not expensive, i.e., they are
11250 most probably expanded inline into reasonably simple code. This is a
11251 superset of is_simple_builtin. */
11252 bool
11253 is_inexpensive_builtin (tree decl)
11255 if (!decl)
11256 return false;
11257 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
11258 return true;
11259 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
11260 switch (DECL_FUNCTION_CODE (decl))
11262 case BUILT_IN_ABS:
11263 CASE_BUILT_IN_ALLOCA:
11264 case BUILT_IN_BSWAP16:
11265 case BUILT_IN_BSWAP32:
11266 case BUILT_IN_BSWAP64:
11267 case BUILT_IN_BSWAP128:
11268 case BUILT_IN_CLZ:
11269 case BUILT_IN_CLZIMAX:
11270 case BUILT_IN_CLZL:
11271 case BUILT_IN_CLZLL:
11272 case BUILT_IN_CTZ:
11273 case BUILT_IN_CTZIMAX:
11274 case BUILT_IN_CTZL:
11275 case BUILT_IN_CTZLL:
11276 case BUILT_IN_FFS:
11277 case BUILT_IN_FFSIMAX:
11278 case BUILT_IN_FFSL:
11279 case BUILT_IN_FFSLL:
11280 case BUILT_IN_IMAXABS:
11281 case BUILT_IN_FINITE:
11282 case BUILT_IN_FINITEF:
11283 case BUILT_IN_FINITEL:
11284 case BUILT_IN_FINITED32:
11285 case BUILT_IN_FINITED64:
11286 case BUILT_IN_FINITED128:
11287 case BUILT_IN_FPCLASSIFY:
11288 case BUILT_IN_ISFINITE:
11289 case BUILT_IN_ISINF_SIGN:
11290 case BUILT_IN_ISINF:
11291 case BUILT_IN_ISINFF:
11292 case BUILT_IN_ISINFL:
11293 case BUILT_IN_ISINFD32:
11294 case BUILT_IN_ISINFD64:
11295 case BUILT_IN_ISINFD128:
11296 case BUILT_IN_ISNAN:
11297 case BUILT_IN_ISNANF:
11298 case BUILT_IN_ISNANL:
11299 case BUILT_IN_ISNAND32:
11300 case BUILT_IN_ISNAND64:
11301 case BUILT_IN_ISNAND128:
11302 case BUILT_IN_ISNORMAL:
11303 case BUILT_IN_ISGREATER:
11304 case BUILT_IN_ISGREATEREQUAL:
11305 case BUILT_IN_ISLESS:
11306 case BUILT_IN_ISLESSEQUAL:
11307 case BUILT_IN_ISLESSGREATER:
11308 case BUILT_IN_ISUNORDERED:
11309 case BUILT_IN_VA_ARG_PACK:
11310 case BUILT_IN_VA_ARG_PACK_LEN:
11311 case BUILT_IN_VA_COPY:
11312 case BUILT_IN_TRAP:
11313 case BUILT_IN_SAVEREGS:
11314 case BUILT_IN_POPCOUNTL:
11315 case BUILT_IN_POPCOUNTLL:
11316 case BUILT_IN_POPCOUNTIMAX:
11317 case BUILT_IN_POPCOUNT:
11318 case BUILT_IN_PARITYL:
11319 case BUILT_IN_PARITYLL:
11320 case BUILT_IN_PARITYIMAX:
11321 case BUILT_IN_PARITY:
11322 case BUILT_IN_LABS:
11323 case BUILT_IN_LLABS:
11324 case BUILT_IN_PREFETCH:
11325 case BUILT_IN_ACC_ON_DEVICE:
11326 return true;
11328 default:
11329 return is_simple_builtin (decl);
11332 return false;
11335 /* Return true if T is a constant and the value cast to a target char
11336 can be represented by a host char.
11337 Store the casted char constant in *P if so. */
11339 bool
11340 target_char_cst_p (tree t, char *p)
11342 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
11343 return false;
11345 *p = (char)tree_to_uhwi (t);
11346 return true;
11349 /* Return true if the builtin DECL is implemented in a standard library.
11350 Otherwise return false which doesn't guarantee it is not (thus the list
11351 of handled builtins below may be incomplete). */
11353 bool
11354 builtin_with_linkage_p (tree decl)
11356 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
11357 switch (DECL_FUNCTION_CODE (decl))
11359 CASE_FLT_FN (BUILT_IN_ACOS):
11360 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
11361 CASE_FLT_FN (BUILT_IN_ACOSH):
11362 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH):
11363 CASE_FLT_FN (BUILT_IN_ASIN):
11364 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
11365 CASE_FLT_FN (BUILT_IN_ASINH):
11366 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINH):
11367 CASE_FLT_FN (BUILT_IN_ATAN):
11368 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN):
11369 CASE_FLT_FN (BUILT_IN_ATANH):
11370 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH):
11371 CASE_FLT_FN (BUILT_IN_ATAN2):
11372 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATAN2):
11373 CASE_FLT_FN (BUILT_IN_CBRT):
11374 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CBRT):
11375 CASE_FLT_FN (BUILT_IN_CEIL):
11376 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
11377 CASE_FLT_FN (BUILT_IN_COPYSIGN):
11378 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
11379 CASE_FLT_FN (BUILT_IN_COS):
11380 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COS):
11381 CASE_FLT_FN (BUILT_IN_COSH):
11382 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSH):
11383 CASE_FLT_FN (BUILT_IN_ERF):
11384 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERF):
11385 CASE_FLT_FN (BUILT_IN_ERFC):
11386 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ERFC):
11387 CASE_FLT_FN (BUILT_IN_EXP):
11388 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP):
11389 CASE_FLT_FN (BUILT_IN_EXP2):
11390 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP2):
11391 CASE_FLT_FN (BUILT_IN_EXPM1):
11392 CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXPM1):
11393 CASE_FLT_FN (BUILT_IN_FABS):
11394 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
11395 CASE_FLT_FN (BUILT_IN_FDIM):
11396 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FDIM):
11397 CASE_FLT_FN (BUILT_IN_FLOOR):
11398 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
11399 CASE_FLT_FN (BUILT_IN_FMA):
11400 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
11401 CASE_FLT_FN (BUILT_IN_FMAX):
11402 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
11403 CASE_FLT_FN (BUILT_IN_FMIN):
11404 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
11405 CASE_FLT_FN (BUILT_IN_FMOD):
11406 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMOD):
11407 CASE_FLT_FN (BUILT_IN_FREXP):
11408 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FREXP):
11409 CASE_FLT_FN (BUILT_IN_HYPOT):
11410 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HYPOT):
11411 CASE_FLT_FN (BUILT_IN_ILOGB):
11412 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ILOGB):
11413 CASE_FLT_FN (BUILT_IN_LDEXP):
11414 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LDEXP):
11415 CASE_FLT_FN (BUILT_IN_LGAMMA):
11416 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LGAMMA):
11417 CASE_FLT_FN (BUILT_IN_LLRINT):
11418 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLRINT):
11419 CASE_FLT_FN (BUILT_IN_LLROUND):
11420 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LLROUND):
11421 CASE_FLT_FN (BUILT_IN_LOG):
11422 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG):
11423 CASE_FLT_FN (BUILT_IN_LOG10):
11424 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10):
11425 CASE_FLT_FN (BUILT_IN_LOG1P):
11426 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P):
11427 CASE_FLT_FN (BUILT_IN_LOG2):
11428 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2):
11429 CASE_FLT_FN (BUILT_IN_LOGB):
11430 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOGB):
11431 CASE_FLT_FN (BUILT_IN_LRINT):
11432 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LRINT):
11433 CASE_FLT_FN (BUILT_IN_LROUND):
11434 CASE_FLT_FN_FLOATN_NX (BUILT_IN_LROUND):
11435 CASE_FLT_FN (BUILT_IN_MODF):
11436 CASE_FLT_FN_FLOATN_NX (BUILT_IN_MODF):
11437 CASE_FLT_FN (BUILT_IN_NAN):
11438 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NAN):
11439 CASE_FLT_FN (BUILT_IN_NEARBYINT):
11440 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
11441 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
11442 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEXTAFTER):
11443 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
11444 CASE_FLT_FN (BUILT_IN_POW):
11445 CASE_FLT_FN_FLOATN_NX (BUILT_IN_POW):
11446 CASE_FLT_FN (BUILT_IN_REMAINDER):
11447 CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMAINDER):
11448 CASE_FLT_FN (BUILT_IN_REMQUO):
11449 CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMQUO):
11450 CASE_FLT_FN (BUILT_IN_RINT):
11451 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
11452 CASE_FLT_FN (BUILT_IN_ROUND):
11453 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
11454 CASE_FLT_FN (BUILT_IN_SCALBLN):
11455 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBLN):
11456 CASE_FLT_FN (BUILT_IN_SCALBN):
11457 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SCALBN):
11458 CASE_FLT_FN (BUILT_IN_SIN):
11459 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SIN):
11460 CASE_FLT_FN (BUILT_IN_SINH):
11461 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINH):
11462 CASE_FLT_FN (BUILT_IN_SINCOS):
11463 CASE_FLT_FN (BUILT_IN_SQRT):
11464 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
11465 CASE_FLT_FN (BUILT_IN_TAN):
11466 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TAN):
11467 CASE_FLT_FN (BUILT_IN_TANH):
11468 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TANH):
11469 CASE_FLT_FN (BUILT_IN_TGAMMA):
11470 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TGAMMA):
11471 CASE_FLT_FN (BUILT_IN_TRUNC):
11472 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
11473 return true;
11475 case BUILT_IN_STPCPY:
11476 case BUILT_IN_STPNCPY:
11477 /* stpcpy is both referenced in libiberty's pex-win32.c and provided
11478 by libiberty's stpcpy.c for MinGW targets so we need to return true
11479 in order to be able to build libiberty in LTO mode for them. */
11480 return true;
11482 default:
11483 break;
11485 return false;
11488 /* Return true if OFFRNG is bounded to a subrange of offset values
11489 valid for the largest possible object. */
11491 bool
11492 access_ref::offset_bounded () const
11494 tree min = TYPE_MIN_VALUE (ptrdiff_type_node);
11495 tree max = TYPE_MAX_VALUE (ptrdiff_type_node);
11496 return wi::to_offset (min) <= offrng[0] && offrng[1] <= wi::to_offset (max);
11499 /* If CALLEE has known side effects, fill in INFO and return true.
11500 See tree-ssa-structalias.cc:find_func_aliases
11501 for the list of builtins we might need to handle here. */
11503 attr_fnspec
11504 builtin_fnspec (tree callee)
11506 built_in_function code = DECL_FUNCTION_CODE (callee);
11508 switch (code)
11510 /* All the following functions read memory pointed to by
11511 their second argument and write memory pointed to by first
11512 argument.
11513 strcat/strncat additionally reads memory pointed to by the first
11514 argument. */
11515 case BUILT_IN_STRCAT:
11516 case BUILT_IN_STRCAT_CHK:
11517 return "1cW 1 ";
11518 case BUILT_IN_STRNCAT:
11519 case BUILT_IN_STRNCAT_CHK:
11520 return "1cW 13";
11521 case BUILT_IN_STRCPY:
11522 case BUILT_IN_STRCPY_CHK:
11523 return "1cO 1 ";
11524 case BUILT_IN_STPCPY:
11525 case BUILT_IN_STPCPY_CHK:
11526 return ".cO 1 ";
11527 case BUILT_IN_STRNCPY:
11528 case BUILT_IN_MEMCPY:
11529 case BUILT_IN_MEMMOVE:
11530 case BUILT_IN_TM_MEMCPY:
11531 case BUILT_IN_TM_MEMMOVE:
11532 case BUILT_IN_STRNCPY_CHK:
11533 case BUILT_IN_MEMCPY_CHK:
11534 case BUILT_IN_MEMMOVE_CHK:
11535 return "1cO313";
11536 case BUILT_IN_MEMPCPY:
11537 case BUILT_IN_MEMPCPY_CHK:
11538 return ".cO313";
11539 case BUILT_IN_STPNCPY:
11540 case BUILT_IN_STPNCPY_CHK:
11541 return ".cO313";
11542 case BUILT_IN_BCOPY:
11543 return ".c23O3";
11544 case BUILT_IN_BZERO:
11545 return ".cO2";
11546 case BUILT_IN_MEMCMP:
11547 case BUILT_IN_MEMCMP_EQ:
11548 case BUILT_IN_BCMP:
11549 case BUILT_IN_STRNCMP:
11550 case BUILT_IN_STRNCMP_EQ:
11551 case BUILT_IN_STRNCASECMP:
11552 return ".cR3R3";
11554 /* The following functions read memory pointed to by their
11555 first argument. */
11556 CASE_BUILT_IN_TM_LOAD (1):
11557 CASE_BUILT_IN_TM_LOAD (2):
11558 CASE_BUILT_IN_TM_LOAD (4):
11559 CASE_BUILT_IN_TM_LOAD (8):
11560 CASE_BUILT_IN_TM_LOAD (FLOAT):
11561 CASE_BUILT_IN_TM_LOAD (DOUBLE):
11562 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
11563 CASE_BUILT_IN_TM_LOAD (M64):
11564 CASE_BUILT_IN_TM_LOAD (M128):
11565 CASE_BUILT_IN_TM_LOAD (M256):
11566 case BUILT_IN_TM_LOG:
11567 case BUILT_IN_TM_LOG_1:
11568 case BUILT_IN_TM_LOG_2:
11569 case BUILT_IN_TM_LOG_4:
11570 case BUILT_IN_TM_LOG_8:
11571 case BUILT_IN_TM_LOG_FLOAT:
11572 case BUILT_IN_TM_LOG_DOUBLE:
11573 case BUILT_IN_TM_LOG_LDOUBLE:
11574 case BUILT_IN_TM_LOG_M64:
11575 case BUILT_IN_TM_LOG_M128:
11576 case BUILT_IN_TM_LOG_M256:
11577 return ".cR ";
11579 case BUILT_IN_INDEX:
11580 case BUILT_IN_RINDEX:
11581 case BUILT_IN_STRCHR:
11582 case BUILT_IN_STRLEN:
11583 case BUILT_IN_STRRCHR:
11584 return ".cR ";
11585 case BUILT_IN_STRNLEN:
11586 return ".cR2";
11588 /* These read memory pointed to by the first argument.
11589 Allocating memory does not have any side-effects apart from
11590 being the definition point for the pointer.
11591 Unix98 specifies that errno is set on allocation failure. */
11592 case BUILT_IN_STRDUP:
11593 return "mCR ";
11594 case BUILT_IN_STRNDUP:
11595 return "mCR2";
11596 /* Allocating memory does not have any side-effects apart from
11597 being the definition point for the pointer. */
11598 case BUILT_IN_MALLOC:
11599 case BUILT_IN_ALIGNED_ALLOC:
11600 case BUILT_IN_CALLOC:
11601 case BUILT_IN_GOMP_ALLOC:
11602 return "mC";
11603 CASE_BUILT_IN_ALLOCA:
11604 return "mc";
11605 /* These read memory pointed to by the first argument with size
11606 in the third argument. */
11607 case BUILT_IN_MEMCHR:
11608 return ".cR3";
11609 /* These read memory pointed to by the first and second arguments. */
11610 case BUILT_IN_STRSTR:
11611 case BUILT_IN_STRPBRK:
11612 case BUILT_IN_STRCASECMP:
11613 case BUILT_IN_STRCSPN:
11614 case BUILT_IN_STRSPN:
11615 case BUILT_IN_STRCMP:
11616 case BUILT_IN_STRCMP_EQ:
11617 return ".cR R ";
11618 /* Freeing memory kills the pointed-to memory. More importantly
11619 the call has to serve as a barrier for moving loads and stores
11620 across it. */
11621 case BUILT_IN_STACK_RESTORE:
11622 case BUILT_IN_FREE:
11623 case BUILT_IN_GOMP_FREE:
11624 return ".co ";
11625 case BUILT_IN_VA_END:
11626 return ".cO ";
11627 /* Realloc serves both as allocation point and deallocation point. */
11628 case BUILT_IN_REALLOC:
11629 return ".Cw ";
11630 case BUILT_IN_GAMMA_R:
11631 case BUILT_IN_GAMMAF_R:
11632 case BUILT_IN_GAMMAL_R:
11633 case BUILT_IN_LGAMMA_R:
11634 case BUILT_IN_LGAMMAF_R:
11635 case BUILT_IN_LGAMMAL_R:
11636 return ".C. Ot";
11637 case BUILT_IN_FREXP:
11638 case BUILT_IN_FREXPF:
11639 case BUILT_IN_FREXPL:
11640 case BUILT_IN_MODF:
11641 case BUILT_IN_MODFF:
11642 case BUILT_IN_MODFL:
11643 return ".c. Ot";
11644 case BUILT_IN_REMQUO:
11645 case BUILT_IN_REMQUOF:
11646 case BUILT_IN_REMQUOL:
11647 return ".c. . Ot";
11648 case BUILT_IN_SINCOS:
11649 case BUILT_IN_SINCOSF:
11650 case BUILT_IN_SINCOSL:
11651 return ".c. OtOt";
11652 case BUILT_IN_MEMSET:
11653 case BUILT_IN_MEMSET_CHK:
11654 case BUILT_IN_TM_MEMSET:
11655 return "1cO3";
11656 CASE_BUILT_IN_TM_STORE (1):
11657 CASE_BUILT_IN_TM_STORE (2):
11658 CASE_BUILT_IN_TM_STORE (4):
11659 CASE_BUILT_IN_TM_STORE (8):
11660 CASE_BUILT_IN_TM_STORE (FLOAT):
11661 CASE_BUILT_IN_TM_STORE (DOUBLE):
11662 CASE_BUILT_IN_TM_STORE (LDOUBLE):
11663 CASE_BUILT_IN_TM_STORE (M64):
11664 CASE_BUILT_IN_TM_STORE (M128):
11665 CASE_BUILT_IN_TM_STORE (M256):
11666 return ".cO ";
11667 case BUILT_IN_STACK_SAVE:
11668 case BUILT_IN_RETURN:
11669 case BUILT_IN_EH_POINTER:
11670 case BUILT_IN_EH_FILTER:
11671 case BUILT_IN_UNWIND_RESUME:
11672 case BUILT_IN_CXA_END_CLEANUP:
11673 case BUILT_IN_EH_COPY_VALUES:
11674 case BUILT_IN_FRAME_ADDRESS:
11675 case BUILT_IN_APPLY_ARGS:
11676 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
11677 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
11678 case BUILT_IN_PREFETCH:
11679 case BUILT_IN_DWARF_CFA:
11680 case BUILT_IN_RETURN_ADDRESS:
11681 return ".c";
11682 case BUILT_IN_ASSUME_ALIGNED:
11683 return "1cX ";
11684 /* But posix_memalign stores a pointer into the memory pointed to
11685 by its first argument. */
11686 case BUILT_IN_POSIX_MEMALIGN:
11687 return ".cOt";
11689 default:
11690 return "";