c, c++: attribute format on a ctor with a vbase [PR101833, PR47634]
[official-gcc.git] / gcc / builtins.cc
blob5b085e3a14f5e48d99a0b09716bca22047e43e38
1 /* Expand builtin functions.
2 Copyright (C) 1988-2022 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-fold.h"
71 #include "intl.h"
72 #include "file-prefix-map.h" /* remap_macro_filename() */
73 #include "gomp-constants.h"
74 #include "omp-general.h"
75 #include "tree-dfa.h"
76 #include "gimple-iterator.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_int_roundingfn (tree, rtx);
127 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
128 static rtx expand_builtin_next_arg (void);
129 static rtx expand_builtin_va_start (tree);
130 static rtx expand_builtin_va_end (tree);
131 static rtx expand_builtin_va_copy (tree);
132 static rtx inline_expand_builtin_bytecmp (tree, rtx);
133 static rtx expand_builtin_strcmp (tree, rtx);
134 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
135 static rtx expand_builtin_memcpy (tree, rtx);
136 static rtx expand_builtin_memory_copy_args (tree dest, tree src, tree len,
137 rtx target, tree exp,
138 memop_ret retmode,
139 bool might_overlap);
140 static rtx expand_builtin_memmove (tree, rtx);
141 static rtx expand_builtin_mempcpy (tree, rtx);
142 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, tree, memop_ret);
143 static rtx expand_builtin_strcpy (tree, rtx);
144 static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx);
145 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
146 static rtx expand_builtin_strncpy (tree, rtx);
147 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
148 static rtx expand_builtin_bzero (tree);
149 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
150 static rtx expand_builtin_strnlen (tree, rtx, machine_mode);
151 static rtx expand_builtin_alloca (tree);
152 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
153 static rtx expand_builtin_frame_address (tree, tree);
154 static tree stabilize_va_list_loc (location_t, tree, int);
155 static rtx expand_builtin_expect (tree, rtx);
156 static rtx expand_builtin_expect_with_probability (tree, rtx);
157 static tree fold_builtin_constant_p (tree);
158 static tree fold_builtin_classify_type (tree);
159 static tree fold_builtin_strlen (location_t, tree, tree, tree);
160 static tree fold_builtin_inf (location_t, tree, int);
161 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
162 static bool validate_arg (const_tree, enum tree_code code);
163 static rtx expand_builtin_fabs (tree, rtx, rtx);
164 static rtx expand_builtin_signbit (tree, rtx);
165 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
166 static tree fold_builtin_isascii (location_t, tree);
167 static tree fold_builtin_toascii (location_t, tree);
168 static tree fold_builtin_isdigit (location_t, tree);
169 static tree fold_builtin_fabs (location_t, tree, tree);
170 static tree fold_builtin_abs (location_t, tree, tree);
171 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
172 enum tree_code);
173 static tree fold_builtin_varargs (location_t, tree, tree*, int);
175 static tree fold_builtin_strpbrk (location_t, tree, tree, tree, tree);
176 static tree fold_builtin_strspn (location_t, tree, tree, tree);
177 static tree fold_builtin_strcspn (location_t, tree, tree, tree);
179 static rtx expand_builtin_object_size (tree);
180 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
181 enum built_in_function);
182 static void maybe_emit_chk_warning (tree, enum built_in_function);
183 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
184 static tree fold_builtin_object_size (tree, tree, enum built_in_function);
186 unsigned HOST_WIDE_INT target_newline;
187 unsigned HOST_WIDE_INT target_percent;
188 static unsigned HOST_WIDE_INT target_c;
189 static unsigned HOST_WIDE_INT target_s;
190 char target_percent_c[3];
191 char target_percent_s[3];
192 char target_percent_s_newline[4];
193 static tree do_mpfr_remquo (tree, tree, tree);
194 static tree do_mpfr_lgamma_r (tree, tree, tree);
195 static void expand_builtin_sync_synchronize (void);
197 /* Return true if NAME starts with __builtin_ or __sync_. */
199 static bool
200 is_builtin_name (const char *name)
202 return (startswith (name, "__builtin_")
203 || startswith (name, "__sync_")
204 || startswith (name, "__atomic_"));
207 /* Return true if NODE should be considered for inline expansion regardless
208 of the optimization level. This means whenever a function is invoked with
209 its "internal" name, which normally contains the prefix "__builtin". */
211 bool
212 called_as_built_in (tree node)
214 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
215 we want the name used to call the function, not the name it
216 will have. */
217 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
218 return is_builtin_name (name);
221 /* Compute values M and N such that M divides (address of EXP - N) and such
222 that N < M. If these numbers can be determined, store M in alignp and N in
223 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
224 *alignp and any bit-offset to *bitposp.
226 Note that the address (and thus the alignment) computed here is based
227 on the address to which a symbol resolves, whereas DECL_ALIGN is based
228 on the address at which an object is actually located. These two
229 addresses are not always the same. For example, on ARM targets,
230 the address &foo of a Thumb function foo() has the lowest bit set,
231 whereas foo() itself starts on an even address.
233 If ADDR_P is true we are taking the address of the memory reference EXP
234 and thus cannot rely on the access taking place. */
236 bool
237 get_object_alignment_2 (tree exp, unsigned int *alignp,
238 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
240 poly_int64 bitsize, bitpos;
241 tree offset;
242 machine_mode mode;
243 int unsignedp, reversep, volatilep;
244 unsigned int align = BITS_PER_UNIT;
245 bool known_alignment = false;
247 /* Get the innermost object and the constant (bitpos) and possibly
248 variable (offset) offset of the access. */
249 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
250 &unsignedp, &reversep, &volatilep);
252 /* Extract alignment information from the innermost object and
253 possibly adjust bitpos and offset. */
254 if (TREE_CODE (exp) == FUNCTION_DECL)
256 /* Function addresses can encode extra information besides their
257 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
258 allows the low bit to be used as a virtual bit, we know
259 that the address itself must be at least 2-byte aligned. */
260 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
261 align = 2 * BITS_PER_UNIT;
263 else if (TREE_CODE (exp) == LABEL_DECL)
265 else if (TREE_CODE (exp) == CONST_DECL)
267 /* The alignment of a CONST_DECL is determined by its initializer. */
268 exp = DECL_INITIAL (exp);
269 align = TYPE_ALIGN (TREE_TYPE (exp));
270 if (CONSTANT_CLASS_P (exp))
271 align = targetm.constant_alignment (exp, align);
273 known_alignment = true;
275 else if (DECL_P (exp))
277 align = DECL_ALIGN (exp);
278 known_alignment = true;
280 else if (TREE_CODE (exp) == INDIRECT_REF
281 || TREE_CODE (exp) == MEM_REF
282 || TREE_CODE (exp) == TARGET_MEM_REF)
284 tree addr = TREE_OPERAND (exp, 0);
285 unsigned ptr_align;
286 unsigned HOST_WIDE_INT ptr_bitpos;
287 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
289 /* If the address is explicitely aligned, handle that. */
290 if (TREE_CODE (addr) == BIT_AND_EXPR
291 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
293 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
294 ptr_bitmask *= BITS_PER_UNIT;
295 align = least_bit_hwi (ptr_bitmask);
296 addr = TREE_OPERAND (addr, 0);
299 known_alignment
300 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
301 align = MAX (ptr_align, align);
303 /* Re-apply explicit alignment to the bitpos. */
304 ptr_bitpos &= ptr_bitmask;
306 /* The alignment of the pointer operand in a TARGET_MEM_REF
307 has to take the variable offset parts into account. */
308 if (TREE_CODE (exp) == TARGET_MEM_REF)
310 if (TMR_INDEX (exp))
312 unsigned HOST_WIDE_INT step = 1;
313 if (TMR_STEP (exp))
314 step = TREE_INT_CST_LOW (TMR_STEP (exp));
315 align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
317 if (TMR_INDEX2 (exp))
318 align = BITS_PER_UNIT;
319 known_alignment = false;
322 /* When EXP is an actual memory reference then we can use
323 TYPE_ALIGN of a pointer indirection to derive alignment.
324 Do so only if get_pointer_alignment_1 did not reveal absolute
325 alignment knowledge and if using that alignment would
326 improve the situation. */
327 unsigned int talign;
328 if (!addr_p && !known_alignment
329 && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT)
330 && talign > align)
331 align = talign;
332 else
334 /* Else adjust bitpos accordingly. */
335 bitpos += ptr_bitpos;
336 if (TREE_CODE (exp) == MEM_REF
337 || TREE_CODE (exp) == TARGET_MEM_REF)
338 bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT;
341 else if (TREE_CODE (exp) == STRING_CST)
343 /* STRING_CST are the only constant objects we allow to be not
344 wrapped inside a CONST_DECL. */
345 align = TYPE_ALIGN (TREE_TYPE (exp));
346 if (CONSTANT_CLASS_P (exp))
347 align = targetm.constant_alignment (exp, align);
349 known_alignment = true;
352 /* If there is a non-constant offset part extract the maximum
353 alignment that can prevail. */
354 if (offset)
356 unsigned int trailing_zeros = tree_ctz (offset);
357 if (trailing_zeros < HOST_BITS_PER_INT)
359 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
360 if (inner)
361 align = MIN (align, inner);
365 /* Account for the alignment of runtime coefficients, so that the constant
366 bitpos is guaranteed to be accurate. */
367 unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]);
368 if (alt_align != 0 && alt_align < align)
370 align = alt_align;
371 known_alignment = false;
374 *alignp = align;
375 *bitposp = bitpos.coeffs[0] & (align - 1);
376 return known_alignment;
379 /* For a memory reference expression EXP compute values M and N such that M
380 divides (&EXP - N) and such that N < M. If these numbers can be determined,
381 store M in alignp and N in *BITPOSP and return true. Otherwise return false
382 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
384 bool
385 get_object_alignment_1 (tree exp, unsigned int *alignp,
386 unsigned HOST_WIDE_INT *bitposp)
388 /* Strip a WITH_SIZE_EXPR, get_inner_reference doesn't know how to deal
389 with it. */
390 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
391 exp = TREE_OPERAND (exp, 0);
392 return get_object_alignment_2 (exp, alignp, bitposp, false);
395 /* Return the alignment in bits of EXP, an object. */
397 unsigned int
398 get_object_alignment (tree exp)
400 unsigned HOST_WIDE_INT bitpos = 0;
401 unsigned int align;
403 get_object_alignment_1 (exp, &align, &bitpos);
405 /* align and bitpos now specify known low bits of the pointer.
406 ptr & (align - 1) == bitpos. */
408 if (bitpos != 0)
409 align = least_bit_hwi (bitpos);
410 return align;
413 /* For a pointer valued expression EXP compute values M and N such that M
414 divides (EXP - N) and such that N < M. If these numbers can be determined,
415 store M in alignp and N in *BITPOSP and return true. Return false if
416 the results are just a conservative approximation.
418 If EXP is not a pointer, false is returned too. */
420 bool
421 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
422 unsigned HOST_WIDE_INT *bitposp)
424 STRIP_NOPS (exp);
426 if (TREE_CODE (exp) == ADDR_EXPR)
427 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
428 alignp, bitposp, true);
429 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
431 unsigned int align;
432 unsigned HOST_WIDE_INT bitpos;
433 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
434 &align, &bitpos);
435 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
436 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
437 else
439 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
440 if (trailing_zeros < HOST_BITS_PER_INT)
442 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
443 if (inner)
444 align = MIN (align, inner);
447 *alignp = align;
448 *bitposp = bitpos & (align - 1);
449 return res;
451 else if (TREE_CODE (exp) == SSA_NAME
452 && POINTER_TYPE_P (TREE_TYPE (exp)))
454 unsigned int ptr_align, ptr_misalign;
455 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
457 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
459 *bitposp = ptr_misalign * BITS_PER_UNIT;
460 *alignp = ptr_align * BITS_PER_UNIT;
461 /* Make sure to return a sensible alignment when the multiplication
462 by BITS_PER_UNIT overflowed. */
463 if (*alignp == 0)
464 *alignp = 1u << (HOST_BITS_PER_INT - 1);
465 /* We cannot really tell whether this result is an approximation. */
466 return false;
468 else
470 *bitposp = 0;
471 *alignp = BITS_PER_UNIT;
472 return false;
475 else if (TREE_CODE (exp) == INTEGER_CST)
477 *alignp = BIGGEST_ALIGNMENT;
478 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
479 & (BIGGEST_ALIGNMENT - 1));
480 return true;
483 *bitposp = 0;
484 *alignp = BITS_PER_UNIT;
485 return false;
488 /* Return the alignment in bits of EXP, a pointer valued expression.
489 The alignment returned is, by default, the alignment of the thing that
490 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
492 Otherwise, look at the expression to see if we can do better, i.e., if the
493 expression is actually pointing at an object whose alignment is tighter. */
495 unsigned int
496 get_pointer_alignment (tree exp)
498 unsigned HOST_WIDE_INT bitpos = 0;
499 unsigned int align;
501 get_pointer_alignment_1 (exp, &align, &bitpos);
503 /* align and bitpos now specify known low bits of the pointer.
504 ptr & (align - 1) == bitpos. */
506 if (bitpos != 0)
507 align = least_bit_hwi (bitpos);
509 return align;
512 /* Return the number of leading non-zero elements in the sequence
513 [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
514 ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
516 unsigned
517 string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
519 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
521 unsigned n;
523 if (eltsize == 1)
525 /* Optimize the common case of plain char. */
526 for (n = 0; n < maxelts; n++)
528 const char *elt = (const char*) ptr + n;
529 if (!*elt)
530 break;
533 else
535 for (n = 0; n < maxelts; n++)
537 const char *elt = (const char*) ptr + n * eltsize;
538 if (!memcmp (elt, "\0\0\0\0", eltsize))
539 break;
542 return n;
545 /* Compute the length of a null-terminated character string or wide
546 character string handling character sizes of 1, 2, and 4 bytes.
547 TREE_STRING_LENGTH is not the right way because it evaluates to
548 the size of the character array in bytes (as opposed to characters)
549 and because it can contain a zero byte in the middle.
551 ONLY_VALUE should be nonzero if the result is not going to be emitted
552 into the instruction stream and zero if it is going to be expanded.
553 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
554 is returned, otherwise NULL, since
555 len = c_strlen (ARG, 1); if (len) expand_expr (len, ...); would not
556 evaluate the side-effects.
558 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
559 accesses. Note that this implies the result is not going to be emitted
560 into the instruction stream.
562 Additional information about the string accessed may be recorded
563 in DATA. For example, if ARG references an unterminated string,
564 then the declaration will be stored in the DECL field. If the
565 length of the unterminated string can be determined, it'll be
566 stored in the LEN field. Note this length could well be different
567 than what a C strlen call would return.
569 ELTSIZE is 1 for normal single byte character strings, and 2 or
570 4 for wide characer strings. ELTSIZE is by default 1.
572 The value returned is of type `ssizetype'. */
574 tree
575 c_strlen (tree arg, int only_value, c_strlen_data *data, unsigned eltsize)
577 /* If we were not passed a DATA pointer, then get one to a local
578 structure. That avoids having to check DATA for NULL before
579 each time we want to use it. */
580 c_strlen_data local_strlen_data = { };
581 if (!data)
582 data = &local_strlen_data;
584 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
586 tree src = STRIP_NOPS (arg);
587 if (TREE_CODE (src) == COND_EXPR
588 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
590 tree len1, len2;
592 len1 = c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
593 len2 = c_strlen (TREE_OPERAND (src, 2), only_value, data, eltsize);
594 if (tree_int_cst_equal (len1, len2))
595 return len1;
598 if (TREE_CODE (src) == COMPOUND_EXPR
599 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
600 return c_strlen (TREE_OPERAND (src, 1), only_value, data, eltsize);
602 location_t loc = EXPR_LOC_OR_LOC (src, input_location);
604 /* Offset from the beginning of the string in bytes. */
605 tree byteoff;
606 tree memsize;
607 tree decl;
608 src = string_constant (src, &byteoff, &memsize, &decl);
609 if (src == 0)
610 return NULL_TREE;
612 /* Determine the size of the string element. */
613 if (eltsize != tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src)))))
614 return NULL_TREE;
616 /* Set MAXELTS to sizeof (SRC) / sizeof (*SRC) - 1, the maximum possible
617 length of SRC. Prefer TYPE_SIZE() to TREE_STRING_LENGTH() if possible
618 in case the latter is less than the size of the array, such as when
619 SRC refers to a short string literal used to initialize a large array.
620 In that case, the elements of the array after the terminating NUL are
621 all NUL. */
622 HOST_WIDE_INT strelts = TREE_STRING_LENGTH (src);
623 strelts = strelts / eltsize;
625 if (!tree_fits_uhwi_p (memsize))
626 return NULL_TREE;
628 HOST_WIDE_INT maxelts = tree_to_uhwi (memsize) / eltsize;
630 /* PTR can point to the byte representation of any string type, including
631 char* and wchar_t*. */
632 const char *ptr = TREE_STRING_POINTER (src);
634 if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
636 /* The code below works only for single byte character types. */
637 if (eltsize != 1)
638 return NULL_TREE;
640 /* If the string has an internal NUL character followed by any
641 non-NUL characters (e.g., "foo\0bar"), we can't compute
642 the offset to the following NUL if we don't know where to
643 start searching for it. */
644 unsigned len = string_length (ptr, eltsize, strelts);
646 /* Return when an embedded null character is found or none at all.
647 In the latter case, set the DECL/LEN field in the DATA structure
648 so that callers may examine them. */
649 if (len + 1 < strelts)
650 return NULL_TREE;
651 else if (len >= maxelts)
653 data->decl = decl;
654 data->off = byteoff;
655 data->minlen = ssize_int (len);
656 return NULL_TREE;
659 /* For empty strings the result should be zero. */
660 if (len == 0)
661 return ssize_int (0);
663 /* We don't know the starting offset, but we do know that the string
664 has no internal zero bytes. If the offset falls within the bounds
665 of the string subtract the offset from the length of the string,
666 and return that. Otherwise the length is zero. Take care to
667 use SAVE_EXPR in case the OFFSET has side-effects. */
668 tree offsave = TREE_SIDE_EFFECTS (byteoff) ? save_expr (byteoff)
669 : byteoff;
670 offsave = fold_convert_loc (loc, sizetype, offsave);
671 tree condexp = fold_build2_loc (loc, LE_EXPR, boolean_type_node, offsave,
672 size_int (len));
673 tree lenexp = fold_build2_loc (loc, MINUS_EXPR, sizetype, size_int (len),
674 offsave);
675 lenexp = fold_convert_loc (loc, ssizetype, lenexp);
676 return fold_build3_loc (loc, COND_EXPR, ssizetype, condexp, lenexp,
677 build_zero_cst (ssizetype));
680 /* Offset from the beginning of the string in elements. */
681 HOST_WIDE_INT eltoff;
683 /* We have a known offset into the string. Start searching there for
684 a null character if we can represent it as a single HOST_WIDE_INT. */
685 if (byteoff == 0)
686 eltoff = 0;
687 else if (! tree_fits_uhwi_p (byteoff) || tree_to_uhwi (byteoff) % eltsize)
688 eltoff = -1;
689 else
690 eltoff = tree_to_uhwi (byteoff) / eltsize;
692 /* If the offset is known to be out of bounds, warn, and call strlen at
693 runtime. */
694 if (eltoff < 0 || eltoff >= maxelts)
696 /* Suppress multiple warnings for propagated constant strings. */
697 if (only_value != 2
698 && !warning_suppressed_p (arg, OPT_Warray_bounds)
699 && warning_at (loc, OPT_Warray_bounds,
700 "offset %qwi outside bounds of constant string",
701 eltoff))
703 if (decl)
704 inform (DECL_SOURCE_LOCATION (decl), "%qE declared here", decl);
705 suppress_warning (arg, OPT_Warray_bounds);
707 return NULL_TREE;
710 /* If eltoff is larger than strelts but less than maxelts the
711 string length is zero, since the excess memory will be zero. */
712 if (eltoff > strelts)
713 return ssize_int (0);
715 /* Use strlen to search for the first zero byte. Since any strings
716 constructed with build_string will have nulls appended, we win even
717 if we get handed something like (char[4])"abcd".
719 Since ELTOFF is our starting index into the string, no further
720 calculation is needed. */
721 unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
722 strelts - eltoff);
724 /* Don't know what to return if there was no zero termination.
725 Ideally this would turn into a gcc_checking_assert over time.
726 Set DECL/LEN so callers can examine them. */
727 if (len >= maxelts - eltoff)
729 data->decl = decl;
730 data->off = byteoff;
731 data->minlen = ssize_int (len);
732 return NULL_TREE;
735 return ssize_int (len);
738 /* Return a constant integer corresponding to target reading
739 GET_MODE_BITSIZE (MODE) bits from string constant STR. If
740 NULL_TERMINATED_P, reading stops after '\0' character, all further ones
741 are assumed to be zero, otherwise it reads as many characters
742 as needed. */
745 c_readstr (const char *str, scalar_int_mode mode,
746 bool null_terminated_p/*=true*/)
748 HOST_WIDE_INT ch;
749 unsigned int i, j;
750 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
752 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
753 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
754 / HOST_BITS_PER_WIDE_INT;
756 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
757 for (i = 0; i < len; i++)
758 tmp[i] = 0;
760 ch = 1;
761 for (i = 0; i < GET_MODE_SIZE (mode); i++)
763 j = i;
764 if (WORDS_BIG_ENDIAN)
765 j = GET_MODE_SIZE (mode) - i - 1;
766 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
767 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
768 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
769 j *= BITS_PER_UNIT;
771 if (ch || !null_terminated_p)
772 ch = (unsigned char) str[i];
773 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
776 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
777 return immed_wide_int_const (c, mode);
780 /* Cast a target constant CST to target CHAR and if that value fits into
781 host char type, return zero and put that value into variable pointed to by
782 P. */
784 static int
785 target_char_cast (tree cst, char *p)
787 unsigned HOST_WIDE_INT val, hostval;
789 if (TREE_CODE (cst) != INTEGER_CST
790 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
791 return 1;
793 /* Do not care if it fits or not right here. */
794 val = TREE_INT_CST_LOW (cst);
796 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
797 val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
799 hostval = val;
800 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
801 hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
803 if (val != hostval)
804 return 1;
806 *p = hostval;
807 return 0;
810 /* Similar to save_expr, but assumes that arbitrary code is not executed
811 in between the multiple evaluations. In particular, we assume that a
812 non-addressable local variable will not be modified. */
814 static tree
815 builtin_save_expr (tree exp)
817 if (TREE_CODE (exp) == SSA_NAME
818 || (TREE_ADDRESSABLE (exp) == 0
819 && (TREE_CODE (exp) == PARM_DECL
820 || (VAR_P (exp) && !TREE_STATIC (exp)))))
821 return exp;
823 return save_expr (exp);
826 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
827 times to get the address of either a higher stack frame, or a return
828 address located within it (depending on FNDECL_CODE). */
830 static rtx
831 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
833 int i;
834 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
835 if (tem == NULL_RTX)
837 /* For a zero count with __builtin_return_address, we don't care what
838 frame address we return, because target-specific definitions will
839 override us. Therefore frame pointer elimination is OK, and using
840 the soft frame pointer is OK.
842 For a nonzero count, or a zero count with __builtin_frame_address,
843 we require a stable offset from the current frame pointer to the
844 previous one, so we must use the hard frame pointer, and
845 we must disable frame pointer elimination. */
846 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
847 tem = frame_pointer_rtx;
848 else
850 tem = hard_frame_pointer_rtx;
852 /* Tell reload not to eliminate the frame pointer. */
853 crtl->accesses_prior_frames = 1;
857 if (count > 0)
858 SETUP_FRAME_ADDRESSES ();
860 /* On the SPARC, the return address is not in the frame, it is in a
861 register. There is no way to access it off of the current frame
862 pointer, but it can be accessed off the previous frame pointer by
863 reading the value from the register window save area. */
864 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
865 count--;
867 /* Scan back COUNT frames to the specified frame. */
868 for (i = 0; i < count; i++)
870 /* Assume the dynamic chain pointer is in the word that the
871 frame address points to, unless otherwise specified. */
872 tem = DYNAMIC_CHAIN_ADDRESS (tem);
873 tem = memory_address (Pmode, tem);
874 tem = gen_frame_mem (Pmode, tem);
875 tem = copy_to_reg (tem);
878 /* For __builtin_frame_address, return what we've got. But, on
879 the SPARC for example, we may have to add a bias. */
880 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
881 return FRAME_ADDR_RTX (tem);
883 /* For __builtin_return_address, get the return address from that frame. */
884 #ifdef RETURN_ADDR_RTX
885 tem = RETURN_ADDR_RTX (count, tem);
886 #else
887 tem = memory_address (Pmode,
888 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
889 tem = gen_frame_mem (Pmode, tem);
890 #endif
891 return tem;
894 /* Alias set used for setjmp buffer. */
895 static alias_set_type setjmp_alias_set = -1;
897 /* Construct the leading half of a __builtin_setjmp call. Control will
898 return to RECEIVER_LABEL. This is also called directly by the SJLJ
899 exception handling code. */
901 void
902 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
904 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
905 rtx stack_save;
906 rtx mem;
908 if (setjmp_alias_set == -1)
909 setjmp_alias_set = new_alias_set ();
911 buf_addr = convert_memory_address (Pmode, buf_addr);
913 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
915 /* We store the frame pointer and the address of receiver_label in
916 the buffer and use the rest of it for the stack save area, which
917 is machine-dependent. */
919 mem = gen_rtx_MEM (Pmode, buf_addr);
920 set_mem_alias_set (mem, setjmp_alias_set);
921 emit_move_insn (mem, hard_frame_pointer_rtx);
923 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
924 GET_MODE_SIZE (Pmode))),
925 set_mem_alias_set (mem, setjmp_alias_set);
927 emit_move_insn (validize_mem (mem),
928 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
930 stack_save = gen_rtx_MEM (sa_mode,
931 plus_constant (Pmode, buf_addr,
932 2 * GET_MODE_SIZE (Pmode)));
933 set_mem_alias_set (stack_save, setjmp_alias_set);
934 emit_stack_save (SAVE_NONLOCAL, &stack_save);
936 /* If there is further processing to do, do it. */
937 if (targetm.have_builtin_setjmp_setup ())
938 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
940 /* We have a nonlocal label. */
941 cfun->has_nonlocal_label = 1;
944 /* Construct the trailing part of a __builtin_setjmp call. This is
945 also called directly by the SJLJ exception handling code.
946 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
948 void
949 expand_builtin_setjmp_receiver (rtx receiver_label)
951 rtx chain;
953 /* Mark the FP as used when we get here, so we have to make sure it's
954 marked as used by this function. */
955 emit_use (hard_frame_pointer_rtx);
957 /* Mark the static chain as clobbered here so life information
958 doesn't get messed up for it. */
959 chain = rtx_for_static_chain (current_function_decl, true);
960 if (chain && REG_P (chain))
961 emit_clobber (chain);
963 if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
965 /* If the argument pointer can be eliminated in favor of the
966 frame pointer, we don't need to restore it. We assume here
967 that if such an elimination is present, it can always be used.
968 This is the case on all known machines; if we don't make this
969 assumption, we do unnecessary saving on many machines. */
970 size_t i;
971 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
973 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
974 if (elim_regs[i].from == ARG_POINTER_REGNUM
975 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
976 break;
978 if (i == ARRAY_SIZE (elim_regs))
980 /* Now restore our arg pointer from the address at which it
981 was saved in our stack frame. */
982 emit_move_insn (crtl->args.internal_arg_pointer,
983 copy_to_reg (get_arg_pointer_save_area ()));
987 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
988 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
989 else if (targetm.have_nonlocal_goto_receiver ())
990 emit_insn (targetm.gen_nonlocal_goto_receiver ());
991 else
992 { /* Nothing */ }
994 /* We must not allow the code we just generated to be reordered by
995 scheduling. Specifically, the update of the frame pointer must
996 happen immediately, not later. */
997 emit_insn (gen_blockage ());
1000 /* __builtin_longjmp is passed a pointer to an array of five words (not
1001 all will be used on all machines). It operates similarly to the C
1002 library function of the same name, but is more efficient. Much of
1003 the code below is copied from the handling of non-local gotos. */
1005 static void
1006 expand_builtin_longjmp (rtx buf_addr, rtx value)
1008 rtx fp, lab, stack;
1009 rtx_insn *insn, *last;
1010 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1012 /* DRAP is needed for stack realign if longjmp is expanded to current
1013 function */
1014 if (SUPPORTS_STACK_ALIGNMENT)
1015 crtl->need_drap = true;
1017 if (setjmp_alias_set == -1)
1018 setjmp_alias_set = new_alias_set ();
1020 buf_addr = convert_memory_address (Pmode, buf_addr);
1022 buf_addr = force_reg (Pmode, buf_addr);
1024 /* We require that the user must pass a second argument of 1, because
1025 that is what builtin_setjmp will return. */
1026 gcc_assert (value == const1_rtx);
1028 last = get_last_insn ();
1029 if (targetm.have_builtin_longjmp ())
1030 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
1031 else
1033 fp = gen_rtx_MEM (Pmode, buf_addr);
1034 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1035 GET_MODE_SIZE (Pmode)));
1037 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1038 2 * GET_MODE_SIZE (Pmode)));
1039 set_mem_alias_set (fp, setjmp_alias_set);
1040 set_mem_alias_set (lab, setjmp_alias_set);
1041 set_mem_alias_set (stack, setjmp_alias_set);
1043 /* Pick up FP, label, and SP from the block and jump. This code is
1044 from expand_goto in stmt.cc; see there for detailed comments. */
1045 if (targetm.have_nonlocal_goto ())
1046 /* We have to pass a value to the nonlocal_goto pattern that will
1047 get copied into the static_chain pointer, but it does not matter
1048 what that value is, because builtin_setjmp does not use it. */
1049 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1050 else
1052 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1053 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1055 lab = copy_to_reg (lab);
1057 /* Restore the frame pointer and stack pointer. We must use a
1058 temporary since the setjmp buffer may be a local. */
1059 fp = copy_to_reg (fp);
1060 emit_stack_restore (SAVE_NONLOCAL, stack);
1062 /* Ensure the frame pointer move is not optimized. */
1063 emit_insn (gen_blockage ());
1064 emit_clobber (hard_frame_pointer_rtx);
1065 emit_clobber (frame_pointer_rtx);
1066 emit_move_insn (hard_frame_pointer_rtx, fp);
1068 emit_use (hard_frame_pointer_rtx);
1069 emit_use (stack_pointer_rtx);
1070 emit_indirect_jump (lab);
1074 /* Search backwards and mark the jump insn as a non-local goto.
1075 Note that this precludes the use of __builtin_longjmp to a
1076 __builtin_setjmp target in the same function. However, we've
1077 already cautioned the user that these functions are for
1078 internal exception handling use only. */
1079 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1081 gcc_assert (insn != last);
1083 if (JUMP_P (insn))
1085 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1086 break;
1088 else if (CALL_P (insn))
1089 break;
1093 static inline bool
1094 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1096 return (iter->i < iter->n);
1099 /* This function validates the types of a function call argument list
1100 against a specified list of tree_codes. If the last specifier is a 0,
1101 that represents an ellipsis, otherwise the last specifier must be a
1102 VOID_TYPE. */
1104 static bool
1105 validate_arglist (const_tree callexpr, ...)
1107 enum tree_code code;
1108 bool res = 0;
1109 va_list ap;
1110 const_call_expr_arg_iterator iter;
1111 const_tree arg;
1113 va_start (ap, callexpr);
1114 init_const_call_expr_arg_iterator (callexpr, &iter);
1116 /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1117 tree fn = CALL_EXPR_FN (callexpr);
1118 bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1120 for (unsigned argno = 1; ; ++argno)
1122 code = (enum tree_code) va_arg (ap, int);
1124 switch (code)
1126 case 0:
1127 /* This signifies an ellipses, any further arguments are all ok. */
1128 res = true;
1129 goto end;
1130 case VOID_TYPE:
1131 /* This signifies an endlink, if no arguments remain, return
1132 true, otherwise return false. */
1133 res = !more_const_call_expr_args_p (&iter);
1134 goto end;
1135 case POINTER_TYPE:
1136 /* The actual argument must be nonnull when either the whole
1137 called function has been declared nonnull, or when the formal
1138 argument corresponding to the actual argument has been. */
1139 if (argmap
1140 && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1142 arg = next_const_call_expr_arg (&iter);
1143 if (!validate_arg (arg, code) || integer_zerop (arg))
1144 goto end;
1145 break;
1147 /* FALLTHRU */
1148 default:
1149 /* If no parameters remain or the parameter's code does not
1150 match the specified code, return false. Otherwise continue
1151 checking any remaining arguments. */
1152 arg = next_const_call_expr_arg (&iter);
1153 if (!validate_arg (arg, code))
1154 goto end;
1155 break;
1159 /* We need gotos here since we can only have one VA_CLOSE in a
1160 function. */
1161 end: ;
1162 va_end (ap);
1164 BITMAP_FREE (argmap);
1166 return res;
1169 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1170 and the address of the save area. */
1172 static rtx
1173 expand_builtin_nonlocal_goto (tree exp)
1175 tree t_label, t_save_area;
1176 rtx r_label, r_save_area, r_fp, r_sp;
1177 rtx_insn *insn;
1179 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1180 return NULL_RTX;
1182 t_label = CALL_EXPR_ARG (exp, 0);
1183 t_save_area = CALL_EXPR_ARG (exp, 1);
1185 r_label = expand_normal (t_label);
1186 r_label = convert_memory_address (Pmode, r_label);
1187 r_save_area = expand_normal (t_save_area);
1188 r_save_area = convert_memory_address (Pmode, r_save_area);
1189 /* Copy the address of the save location to a register just in case it was
1190 based on the frame pointer. */
1191 r_save_area = copy_to_reg (r_save_area);
1192 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1193 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1194 plus_constant (Pmode, r_save_area,
1195 GET_MODE_SIZE (Pmode)));
1197 crtl->has_nonlocal_goto = 1;
1199 /* ??? We no longer need to pass the static chain value, afaik. */
1200 if (targetm.have_nonlocal_goto ())
1201 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1202 else
1204 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1205 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1207 r_label = copy_to_reg (r_label);
1209 /* Restore the frame pointer and stack pointer. We must use a
1210 temporary since the setjmp buffer may be a local. */
1211 r_fp = copy_to_reg (r_fp);
1212 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1214 /* Ensure the frame pointer move is not optimized. */
1215 emit_insn (gen_blockage ());
1216 emit_clobber (hard_frame_pointer_rtx);
1217 emit_clobber (frame_pointer_rtx);
1218 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1220 /* USE of hard_frame_pointer_rtx added for consistency;
1221 not clear if really needed. */
1222 emit_use (hard_frame_pointer_rtx);
1223 emit_use (stack_pointer_rtx);
1225 /* If the architecture is using a GP register, we must
1226 conservatively assume that the target function makes use of it.
1227 The prologue of functions with nonlocal gotos must therefore
1228 initialize the GP register to the appropriate value, and we
1229 must then make sure that this value is live at the point
1230 of the jump. (Note that this doesn't necessarily apply
1231 to targets with a nonlocal_goto pattern; they are free
1232 to implement it in their own way. Note also that this is
1233 a no-op if the GP register is a global invariant.) */
1234 unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1235 if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1236 emit_use (pic_offset_table_rtx);
1238 emit_indirect_jump (r_label);
1241 /* Search backwards to the jump insn and mark it as a
1242 non-local goto. */
1243 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1245 if (JUMP_P (insn))
1247 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1248 break;
1250 else if (CALL_P (insn))
1251 break;
1254 return const0_rtx;
1257 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1258 (not all will be used on all machines) that was passed to __builtin_setjmp.
1259 It updates the stack pointer in that block to the current value. This is
1260 also called directly by the SJLJ exception handling code. */
1262 void
1263 expand_builtin_update_setjmp_buf (rtx buf_addr)
1265 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1266 buf_addr = convert_memory_address (Pmode, buf_addr);
1267 rtx stack_save
1268 = gen_rtx_MEM (sa_mode,
1269 memory_address
1270 (sa_mode,
1271 plus_constant (Pmode, buf_addr,
1272 2 * GET_MODE_SIZE (Pmode))));
1274 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1277 /* Expand a call to __builtin_prefetch. For a target that does not support
1278 data prefetch, evaluate the memory address argument in case it has side
1279 effects. */
1281 static void
1282 expand_builtin_prefetch (tree exp)
1284 tree arg0, arg1, arg2;
1285 int nargs;
1286 rtx op0, op1, op2;
1288 if (!validate_arglist (exp, POINTER_TYPE, 0))
1289 return;
1291 arg0 = CALL_EXPR_ARG (exp, 0);
1293 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1294 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1295 locality). */
1296 nargs = call_expr_nargs (exp);
1297 if (nargs > 1)
1298 arg1 = CALL_EXPR_ARG (exp, 1);
1299 else
1300 arg1 = integer_zero_node;
1301 if (nargs > 2)
1302 arg2 = CALL_EXPR_ARG (exp, 2);
1303 else
1304 arg2 = integer_three_node;
1306 /* Argument 0 is an address. */
1307 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1309 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1310 if (TREE_CODE (arg1) != INTEGER_CST)
1312 error ("second argument to %<__builtin_prefetch%> must be a constant");
1313 arg1 = integer_zero_node;
1315 op1 = expand_normal (arg1);
1316 /* Argument 1 must be either zero or one. */
1317 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1319 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1320 " using zero");
1321 op1 = const0_rtx;
1324 /* Argument 2 (locality) must be a compile-time constant int. */
1325 if (TREE_CODE (arg2) != INTEGER_CST)
1327 error ("third argument to %<__builtin_prefetch%> must be a constant");
1328 arg2 = integer_zero_node;
1330 op2 = expand_normal (arg2);
1331 /* Argument 2 must be 0, 1, 2, or 3. */
1332 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1334 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1335 op2 = const0_rtx;
1338 if (targetm.have_prefetch ())
1340 class expand_operand ops[3];
1342 create_address_operand (&ops[0], op0);
1343 create_integer_operand (&ops[1], INTVAL (op1));
1344 create_integer_operand (&ops[2], INTVAL (op2));
1345 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1346 return;
1349 /* Don't do anything with direct references to volatile memory, but
1350 generate code to handle other side effects. */
1351 if (!MEM_P (op0) && side_effects_p (op0))
1352 emit_insn (op0);
1355 /* Get a MEM rtx for expression EXP which is the address of an operand
1356 to be used in a string instruction (cmpstrsi, cpymemsi, ..). LEN is
1357 the maximum length of the block of memory that might be accessed or
1358 NULL if unknown. */
1361 get_memory_rtx (tree exp, tree len)
1363 tree orig_exp = exp;
1364 rtx addr, mem;
1366 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1367 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1368 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1369 exp = TREE_OPERAND (exp, 0);
1371 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1372 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1374 /* Get an expression we can use to find the attributes to assign to MEM.
1375 First remove any nops. */
1376 while (CONVERT_EXPR_P (exp)
1377 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1378 exp = TREE_OPERAND (exp, 0);
1380 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1381 (as builtin stringops may alias with anything). */
1382 exp = fold_build2 (MEM_REF,
1383 build_array_type (char_type_node,
1384 build_range_type (sizetype,
1385 size_one_node, len)),
1386 exp, build_int_cst (ptr_type_node, 0));
1388 /* If the MEM_REF has no acceptable address, try to get the base object
1389 from the original address we got, and build an all-aliasing
1390 unknown-sized access to that one. */
1391 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1392 set_mem_attributes (mem, exp, 0);
1393 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1394 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1395 0))))
1397 exp = build_fold_addr_expr (exp);
1398 exp = fold_build2 (MEM_REF,
1399 build_array_type (char_type_node,
1400 build_range_type (sizetype,
1401 size_zero_node,
1402 NULL)),
1403 exp, build_int_cst (ptr_type_node, 0));
1404 set_mem_attributes (mem, exp, 0);
1406 set_mem_alias_set (mem, 0);
1407 return mem;
1410 /* Built-in functions to perform an untyped call and return. */
1412 #define apply_args_mode \
1413 (this_target_builtins->x_apply_args_mode)
1414 #define apply_result_mode \
1415 (this_target_builtins->x_apply_result_mode)
1417 /* Return the size required for the block returned by __builtin_apply_args,
1418 and initialize apply_args_mode. */
1420 static int
1421 apply_args_size (void)
1423 static int size = -1;
1424 int align;
1425 unsigned int regno;
1427 /* The values computed by this function never change. */
1428 if (size < 0)
1430 /* The first value is the incoming arg-pointer. */
1431 size = GET_MODE_SIZE (Pmode);
1433 /* The second value is the structure value address unless this is
1434 passed as an "invisible" first argument. */
1435 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1436 size += GET_MODE_SIZE (Pmode);
1438 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1439 if (FUNCTION_ARG_REGNO_P (regno))
1441 fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno);
1443 gcc_assert (mode != VOIDmode);
1445 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1446 if (size % align != 0)
1447 size = CEIL (size, align) * align;
1448 size += GET_MODE_SIZE (mode);
1449 apply_args_mode[regno] = mode;
1451 else
1453 apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1456 return size;
1459 /* Return the size required for the block returned by __builtin_apply,
1460 and initialize apply_result_mode. */
1462 static int
1463 apply_result_size (void)
1465 static int size = -1;
1466 int align, regno;
1468 /* The values computed by this function never change. */
1469 if (size < 0)
1471 size = 0;
1473 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1474 if (targetm.calls.function_value_regno_p (regno))
1476 fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1478 gcc_assert (mode != VOIDmode);
1480 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1481 if (size % align != 0)
1482 size = CEIL (size, align) * align;
1483 size += GET_MODE_SIZE (mode);
1484 apply_result_mode[regno] = mode;
1486 else
1487 apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1489 /* Allow targets that use untyped_call and untyped_return to override
1490 the size so that machine-specific information can be stored here. */
1491 #ifdef APPLY_RESULT_SIZE
1492 size = APPLY_RESULT_SIZE;
1493 #endif
1495 return size;
1498 /* Create a vector describing the result block RESULT. If SAVEP is true,
1499 the result block is used to save the values; otherwise it is used to
1500 restore the values. */
1502 static rtx
1503 result_vector (int savep, rtx result)
1505 int regno, size, align, nelts;
1506 fixed_size_mode mode;
1507 rtx reg, mem;
1508 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1510 size = nelts = 0;
1511 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1512 if ((mode = apply_result_mode[regno]) != VOIDmode)
1514 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1515 if (size % align != 0)
1516 size = CEIL (size, align) * align;
1517 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1518 mem = adjust_address (result, mode, size);
1519 savevec[nelts++] = (savep
1520 ? gen_rtx_SET (mem, reg)
1521 : gen_rtx_SET (reg, mem));
1522 size += GET_MODE_SIZE (mode);
1524 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1527 /* Save the state required to perform an untyped call with the same
1528 arguments as were passed to the current function. */
1530 static rtx
1531 expand_builtin_apply_args_1 (void)
1533 rtx registers, tem;
1534 int size, align, regno;
1535 fixed_size_mode mode;
1536 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1538 /* Create a block where the arg-pointer, structure value address,
1539 and argument registers can be saved. */
1540 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1542 /* Walk past the arg-pointer and structure value address. */
1543 size = GET_MODE_SIZE (Pmode);
1544 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1545 size += GET_MODE_SIZE (Pmode);
1547 /* Save each register used in calling a function to the block. */
1548 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1549 if ((mode = apply_args_mode[regno]) != VOIDmode)
1551 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1552 if (size % align != 0)
1553 size = CEIL (size, align) * align;
1555 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1557 emit_move_insn (adjust_address (registers, mode, size), tem);
1558 size += GET_MODE_SIZE (mode);
1561 /* Save the arg pointer to the block. */
1562 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1563 /* We need the pointer as the caller actually passed them to us, not
1564 as we might have pretended they were passed. Make sure it's a valid
1565 operand, as emit_move_insn isn't expected to handle a PLUS. */
1566 if (STACK_GROWS_DOWNWARD)
1568 = force_operand (plus_constant (Pmode, tem,
1569 crtl->args.pretend_args_size),
1570 NULL_RTX);
1571 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1573 size = GET_MODE_SIZE (Pmode);
1575 /* Save the structure value address unless this is passed as an
1576 "invisible" first argument. */
1577 if (struct_incoming_value)
1578 emit_move_insn (adjust_address (registers, Pmode, size),
1579 copy_to_reg (struct_incoming_value));
1581 /* Return the address of the block. */
1582 return copy_addr_to_reg (XEXP (registers, 0));
1585 /* __builtin_apply_args returns block of memory allocated on
1586 the stack into which is stored the arg pointer, structure
1587 value address, static chain, and all the registers that might
1588 possibly be used in performing a function call. The code is
1589 moved to the start of the function so the incoming values are
1590 saved. */
1592 static rtx
1593 expand_builtin_apply_args (void)
1595 /* Don't do __builtin_apply_args more than once in a function.
1596 Save the result of the first call and reuse it. */
1597 if (apply_args_value != 0)
1598 return apply_args_value;
1600 /* When this function is called, it means that registers must be
1601 saved on entry to this function. So we migrate the
1602 call to the first insn of this function. */
1603 rtx temp;
1605 start_sequence ();
1606 temp = expand_builtin_apply_args_1 ();
1607 rtx_insn *seq = get_insns ();
1608 end_sequence ();
1610 apply_args_value = temp;
1612 /* Put the insns after the NOTE that starts the function.
1613 If this is inside a start_sequence, make the outer-level insn
1614 chain current, so the code is placed at the start of the
1615 function. If internal_arg_pointer is a non-virtual pseudo,
1616 it needs to be placed after the function that initializes
1617 that pseudo. */
1618 push_topmost_sequence ();
1619 if (REG_P (crtl->args.internal_arg_pointer)
1620 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1621 emit_insn_before (seq, parm_birth_insn);
1622 else
1623 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1624 pop_topmost_sequence ();
1625 return temp;
1629 /* Perform an untyped call and save the state required to perform an
1630 untyped return of whatever value was returned by the given function. */
1632 static rtx
1633 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1635 int size, align, regno;
1636 fixed_size_mode mode;
1637 rtx incoming_args, result, reg, dest, src;
1638 rtx_call_insn *call_insn;
1639 rtx old_stack_level = 0;
1640 rtx call_fusage = 0;
1641 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1643 arguments = convert_memory_address (Pmode, arguments);
1645 /* Create a block where the return registers can be saved. */
1646 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1648 /* Fetch the arg pointer from the ARGUMENTS block. */
1649 incoming_args = gen_reg_rtx (Pmode);
1650 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1651 if (!STACK_GROWS_DOWNWARD)
1652 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1653 incoming_args, 0, OPTAB_LIB_WIDEN);
1655 /* Push a new argument block and copy the arguments. Do not allow
1656 the (potential) memcpy call below to interfere with our stack
1657 manipulations. */
1658 do_pending_stack_adjust ();
1659 NO_DEFER_POP;
1661 /* Save the stack with nonlocal if available. */
1662 if (targetm.have_save_stack_nonlocal ())
1663 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1664 else
1665 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1667 /* Allocate a block of memory onto the stack and copy the memory
1668 arguments to the outgoing arguments address. We can pass TRUE
1669 as the 4th argument because we just saved the stack pointer
1670 and will restore it right after the call. */
1671 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1673 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1674 may have already set current_function_calls_alloca to true.
1675 current_function_calls_alloca won't be set if argsize is zero,
1676 so we have to guarantee need_drap is true here. */
1677 if (SUPPORTS_STACK_ALIGNMENT)
1678 crtl->need_drap = true;
1680 dest = virtual_outgoing_args_rtx;
1681 if (!STACK_GROWS_DOWNWARD)
1683 if (CONST_INT_P (argsize))
1684 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1685 else
1686 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1688 dest = gen_rtx_MEM (BLKmode, dest);
1689 set_mem_align (dest, PARM_BOUNDARY);
1690 src = gen_rtx_MEM (BLKmode, incoming_args);
1691 set_mem_align (src, PARM_BOUNDARY);
1692 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1694 /* Refer to the argument block. */
1695 apply_args_size ();
1696 arguments = gen_rtx_MEM (BLKmode, arguments);
1697 set_mem_align (arguments, PARM_BOUNDARY);
1699 /* Walk past the arg-pointer and structure value address. */
1700 size = GET_MODE_SIZE (Pmode);
1701 if (struct_value)
1702 size += GET_MODE_SIZE (Pmode);
1704 /* Restore each of the registers previously saved. Make USE insns
1705 for each of these registers for use in making the call. */
1706 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1707 if ((mode = apply_args_mode[regno]) != VOIDmode)
1709 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1710 if (size % align != 0)
1711 size = CEIL (size, align) * align;
1712 reg = gen_rtx_REG (mode, regno);
1713 emit_move_insn (reg, adjust_address (arguments, mode, size));
1714 use_reg (&call_fusage, reg);
1715 size += GET_MODE_SIZE (mode);
1718 /* Restore the structure value address unless this is passed as an
1719 "invisible" first argument. */
1720 size = GET_MODE_SIZE (Pmode);
1721 if (struct_value)
1723 rtx value = gen_reg_rtx (Pmode);
1724 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1725 emit_move_insn (struct_value, value);
1726 if (REG_P (struct_value))
1727 use_reg (&call_fusage, struct_value);
1730 /* All arguments and registers used for the call are set up by now! */
1731 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1733 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1734 and we don't want to load it into a register as an optimization,
1735 because prepare_call_address already did it if it should be done. */
1736 if (GET_CODE (function) != SYMBOL_REF)
1737 function = memory_address (FUNCTION_MODE, function);
1739 /* Generate the actual call instruction and save the return value. */
1740 if (targetm.have_untyped_call ())
1742 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1743 rtx_insn *seq = targetm.gen_untyped_call (mem, result,
1744 result_vector (1, result));
1745 for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
1746 if (CALL_P (insn))
1747 add_reg_note (insn, REG_UNTYPED_CALL, NULL_RTX);
1748 emit_insn (seq);
1750 else if (targetm.have_call_value ())
1752 rtx valreg = 0;
1754 /* Locate the unique return register. It is not possible to
1755 express a call that sets more than one return register using
1756 call_value; use untyped_call for that. In fact, untyped_call
1757 only needs to save the return registers in the given block. */
1758 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1759 if ((mode = apply_result_mode[regno]) != VOIDmode)
1761 gcc_assert (!valreg); /* have_untyped_call required. */
1763 valreg = gen_rtx_REG (mode, regno);
1766 emit_insn (targetm.gen_call_value (valreg,
1767 gen_rtx_MEM (FUNCTION_MODE, function),
1768 const0_rtx, NULL_RTX, const0_rtx));
1770 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1772 else
1773 gcc_unreachable ();
1775 /* Find the CALL insn we just emitted, and attach the register usage
1776 information. */
1777 call_insn = last_call_insn ();
1778 add_function_usage_to (call_insn, call_fusage);
1780 /* Restore the stack. */
1781 if (targetm.have_save_stack_nonlocal ())
1782 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1783 else
1784 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1785 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1787 OK_DEFER_POP;
1789 /* Return the address of the result block. */
1790 result = copy_addr_to_reg (XEXP (result, 0));
1791 return convert_memory_address (ptr_mode, result);
1794 /* Perform an untyped return. */
1796 static void
1797 expand_builtin_return (rtx result)
1799 int size, align, regno;
1800 fixed_size_mode mode;
1801 rtx reg;
1802 rtx_insn *call_fusage = 0;
1804 result = convert_memory_address (Pmode, result);
1806 apply_result_size ();
1807 result = gen_rtx_MEM (BLKmode, result);
1809 if (targetm.have_untyped_return ())
1811 rtx vector = result_vector (0, result);
1812 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1813 emit_barrier ();
1814 return;
1817 /* Restore the return value and note that each value is used. */
1818 size = 0;
1819 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1820 if ((mode = apply_result_mode[regno]) != VOIDmode)
1822 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1823 if (size % align != 0)
1824 size = CEIL (size, align) * align;
1825 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1826 emit_move_insn (reg, adjust_address (result, mode, size));
1828 push_to_sequence (call_fusage);
1829 emit_use (reg);
1830 call_fusage = get_insns ();
1831 end_sequence ();
1832 size += GET_MODE_SIZE (mode);
1835 /* Put the USE insns before the return. */
1836 emit_insn (call_fusage);
1838 /* Return whatever values was restored by jumping directly to the end
1839 of the function. */
1840 expand_naked_return ();
1843 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1845 static enum type_class
1846 type_to_class (tree type)
1848 switch (TREE_CODE (type))
1850 case VOID_TYPE: return void_type_class;
1851 case INTEGER_TYPE: return integer_type_class;
1852 case ENUMERAL_TYPE: return enumeral_type_class;
1853 case BOOLEAN_TYPE: return boolean_type_class;
1854 case POINTER_TYPE: return pointer_type_class;
1855 case REFERENCE_TYPE: return reference_type_class;
1856 case OFFSET_TYPE: return offset_type_class;
1857 case REAL_TYPE: return real_type_class;
1858 case COMPLEX_TYPE: return complex_type_class;
1859 case FUNCTION_TYPE: return function_type_class;
1860 case METHOD_TYPE: return method_type_class;
1861 case RECORD_TYPE: return record_type_class;
1862 case UNION_TYPE:
1863 case QUAL_UNION_TYPE: return union_type_class;
1864 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1865 ? string_type_class : array_type_class);
1866 case LANG_TYPE: return lang_type_class;
1867 case OPAQUE_TYPE: return opaque_type_class;
1868 default: return no_type_class;
1872 /* Expand a call EXP to __builtin_classify_type. */
1874 static rtx
1875 expand_builtin_classify_type (tree exp)
1877 if (call_expr_nargs (exp))
1878 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1879 return GEN_INT (no_type_class);
1882 /* This helper macro, meant to be used in mathfn_built_in below, determines
1883 which among a set of builtin math functions is appropriate for a given type
1884 mode. The `F' (float) and `L' (long double) are automatically generated
1885 from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1886 types, there are additional types that are considered with 'F32', 'F64',
1887 'F128', etc. suffixes. */
1888 #define CASE_MATHFN(MATHFN) \
1889 CASE_CFN_##MATHFN: \
1890 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1891 fcodel = BUILT_IN_##MATHFN##L ; break;
1892 /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1893 types. */
1894 #define CASE_MATHFN_FLOATN(MATHFN) \
1895 CASE_CFN_##MATHFN: \
1896 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1897 fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1898 fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1899 fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1900 fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1901 break;
1902 /* Similar to above, but appends _R after any F/L suffix. */
1903 #define CASE_MATHFN_REENT(MATHFN) \
1904 case CFN_BUILT_IN_##MATHFN##_R: \
1905 case CFN_BUILT_IN_##MATHFN##F_R: \
1906 case CFN_BUILT_IN_##MATHFN##L_R: \
1907 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1908 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1910 /* Return a function equivalent to FN but operating on floating-point
1911 values of type TYPE, or END_BUILTINS if no such function exists.
1912 This is purely an operation on function codes; it does not guarantee
1913 that the target actually has an implementation of the function. */
1915 static built_in_function
1916 mathfn_built_in_2 (tree type, combined_fn fn)
1918 tree mtype;
1919 built_in_function fcode, fcodef, fcodel;
1920 built_in_function fcodef16 = END_BUILTINS;
1921 built_in_function fcodef32 = END_BUILTINS;
1922 built_in_function fcodef64 = END_BUILTINS;
1923 built_in_function fcodef128 = END_BUILTINS;
1924 built_in_function fcodef32x = END_BUILTINS;
1925 built_in_function fcodef64x = END_BUILTINS;
1926 built_in_function fcodef128x = END_BUILTINS;
1928 switch (fn)
1930 #define SEQ_OF_CASE_MATHFN \
1931 CASE_MATHFN (ACOS) \
1932 CASE_MATHFN (ACOSH) \
1933 CASE_MATHFN (ASIN) \
1934 CASE_MATHFN (ASINH) \
1935 CASE_MATHFN (ATAN) \
1936 CASE_MATHFN (ATAN2) \
1937 CASE_MATHFN (ATANH) \
1938 CASE_MATHFN (CBRT) \
1939 CASE_MATHFN_FLOATN (CEIL) \
1940 CASE_MATHFN (CEXPI) \
1941 CASE_MATHFN_FLOATN (COPYSIGN) \
1942 CASE_MATHFN (COS) \
1943 CASE_MATHFN (COSH) \
1944 CASE_MATHFN (DREM) \
1945 CASE_MATHFN (ERF) \
1946 CASE_MATHFN (ERFC) \
1947 CASE_MATHFN (EXP) \
1948 CASE_MATHFN (EXP10) \
1949 CASE_MATHFN (EXP2) \
1950 CASE_MATHFN (EXPM1) \
1951 CASE_MATHFN (FABS) \
1952 CASE_MATHFN (FDIM) \
1953 CASE_MATHFN_FLOATN (FLOOR) \
1954 CASE_MATHFN_FLOATN (FMA) \
1955 CASE_MATHFN_FLOATN (FMAX) \
1956 CASE_MATHFN_FLOATN (FMIN) \
1957 CASE_MATHFN (FMOD) \
1958 CASE_MATHFN (FREXP) \
1959 CASE_MATHFN (GAMMA) \
1960 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */ \
1961 CASE_MATHFN (HUGE_VAL) \
1962 CASE_MATHFN (HYPOT) \
1963 CASE_MATHFN (ILOGB) \
1964 CASE_MATHFN (ICEIL) \
1965 CASE_MATHFN (IFLOOR) \
1966 CASE_MATHFN (INF) \
1967 CASE_MATHFN (IRINT) \
1968 CASE_MATHFN (IROUND) \
1969 CASE_MATHFN (ISINF) \
1970 CASE_MATHFN (J0) \
1971 CASE_MATHFN (J1) \
1972 CASE_MATHFN (JN) \
1973 CASE_MATHFN (LCEIL) \
1974 CASE_MATHFN (LDEXP) \
1975 CASE_MATHFN (LFLOOR) \
1976 CASE_MATHFN (LGAMMA) \
1977 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */ \
1978 CASE_MATHFN (LLCEIL) \
1979 CASE_MATHFN (LLFLOOR) \
1980 CASE_MATHFN (LLRINT) \
1981 CASE_MATHFN (LLROUND) \
1982 CASE_MATHFN (LOG) \
1983 CASE_MATHFN (LOG10) \
1984 CASE_MATHFN (LOG1P) \
1985 CASE_MATHFN (LOG2) \
1986 CASE_MATHFN (LOGB) \
1987 CASE_MATHFN (LRINT) \
1988 CASE_MATHFN (LROUND) \
1989 CASE_MATHFN (MODF) \
1990 CASE_MATHFN (NAN) \
1991 CASE_MATHFN (NANS) \
1992 CASE_MATHFN_FLOATN (NEARBYINT) \
1993 CASE_MATHFN (NEXTAFTER) \
1994 CASE_MATHFN (NEXTTOWARD) \
1995 CASE_MATHFN (POW) \
1996 CASE_MATHFN (POWI) \
1997 CASE_MATHFN (POW10) \
1998 CASE_MATHFN (REMAINDER) \
1999 CASE_MATHFN (REMQUO) \
2000 CASE_MATHFN_FLOATN (RINT) \
2001 CASE_MATHFN_FLOATN (ROUND) \
2002 CASE_MATHFN_FLOATN (ROUNDEVEN) \
2003 CASE_MATHFN (SCALB) \
2004 CASE_MATHFN (SCALBLN) \
2005 CASE_MATHFN (SCALBN) \
2006 CASE_MATHFN (SIGNBIT) \
2007 CASE_MATHFN (SIGNIFICAND) \
2008 CASE_MATHFN (SIN) \
2009 CASE_MATHFN (SINCOS) \
2010 CASE_MATHFN (SINH) \
2011 CASE_MATHFN_FLOATN (SQRT) \
2012 CASE_MATHFN (TAN) \
2013 CASE_MATHFN (TANH) \
2014 CASE_MATHFN (TGAMMA) \
2015 CASE_MATHFN_FLOATN (TRUNC) \
2016 CASE_MATHFN (Y0) \
2017 CASE_MATHFN (Y1) \
2018 CASE_MATHFN (YN)
2020 SEQ_OF_CASE_MATHFN
2022 default:
2023 return END_BUILTINS;
2026 mtype = TYPE_MAIN_VARIANT (type);
2027 if (mtype == double_type_node)
2028 return fcode;
2029 else if (mtype == float_type_node)
2030 return fcodef;
2031 else if (mtype == long_double_type_node)
2032 return fcodel;
2033 else if (mtype == float16_type_node)
2034 return fcodef16;
2035 else if (mtype == float32_type_node)
2036 return fcodef32;
2037 else if (mtype == float64_type_node)
2038 return fcodef64;
2039 else if (mtype == float128_type_node)
2040 return fcodef128;
2041 else if (mtype == float32x_type_node)
2042 return fcodef32x;
2043 else if (mtype == float64x_type_node)
2044 return fcodef64x;
2045 else if (mtype == float128x_type_node)
2046 return fcodef128x;
2047 else
2048 return END_BUILTINS;
2051 #undef CASE_MATHFN
2052 #undef CASE_MATHFN_FLOATN
2053 #undef CASE_MATHFN_REENT
2055 /* Return mathematic function equivalent to FN but operating directly on TYPE,
2056 if available. If IMPLICIT_P is true use the implicit builtin declaration,
2057 otherwise use the explicit declaration. If we can't do the conversion,
2058 return null. */
2060 static tree
2061 mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2063 built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2064 if (fcode2 == END_BUILTINS)
2065 return NULL_TREE;
2067 if (implicit_p && !builtin_decl_implicit_p (fcode2))
2068 return NULL_TREE;
2070 return builtin_decl_explicit (fcode2);
2073 /* Like mathfn_built_in_1, but always use the implicit array. */
2075 tree
2076 mathfn_built_in (tree type, combined_fn fn)
2078 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2081 /* Like mathfn_built_in_1, but take a built_in_function and
2082 always use the implicit array. */
2084 tree
2085 mathfn_built_in (tree type, enum built_in_function fn)
2087 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2090 /* Return the type associated with a built in function, i.e., the one
2091 to be passed to mathfn_built_in to get the type-specific
2092 function. */
2094 tree
2095 mathfn_built_in_type (combined_fn fn)
2097 #define CASE_MATHFN(MATHFN) \
2098 case CFN_BUILT_IN_##MATHFN: \
2099 return double_type_node; \
2100 case CFN_BUILT_IN_##MATHFN##F: \
2101 return float_type_node; \
2102 case CFN_BUILT_IN_##MATHFN##L: \
2103 return long_double_type_node;
2105 #define CASE_MATHFN_FLOATN(MATHFN) \
2106 CASE_MATHFN(MATHFN) \
2107 case CFN_BUILT_IN_##MATHFN##F16: \
2108 return float16_type_node; \
2109 case CFN_BUILT_IN_##MATHFN##F32: \
2110 return float32_type_node; \
2111 case CFN_BUILT_IN_##MATHFN##F64: \
2112 return float64_type_node; \
2113 case CFN_BUILT_IN_##MATHFN##F128: \
2114 return float128_type_node; \
2115 case CFN_BUILT_IN_##MATHFN##F32X: \
2116 return float32x_type_node; \
2117 case CFN_BUILT_IN_##MATHFN##F64X: \
2118 return float64x_type_node; \
2119 case CFN_BUILT_IN_##MATHFN##F128X: \
2120 return float128x_type_node;
2122 /* Similar to above, but appends _R after any F/L suffix. */
2123 #define CASE_MATHFN_REENT(MATHFN) \
2124 case CFN_BUILT_IN_##MATHFN##_R: \
2125 return double_type_node; \
2126 case CFN_BUILT_IN_##MATHFN##F_R: \
2127 return float_type_node; \
2128 case CFN_BUILT_IN_##MATHFN##L_R: \
2129 return long_double_type_node;
2131 switch (fn)
2133 SEQ_OF_CASE_MATHFN
2135 default:
2136 return NULL_TREE;
2139 #undef CASE_MATHFN
2140 #undef CASE_MATHFN_FLOATN
2141 #undef CASE_MATHFN_REENT
2142 #undef SEQ_OF_CASE_MATHFN
2145 /* Check whether there is an internal function associated with function FN
2146 and return type RETURN_TYPE. Return the function if so, otherwise return
2147 IFN_LAST.
2149 Note that this function only tests whether the function is defined in
2150 internals.def, not whether it is actually available on the target. */
2152 static internal_fn
2153 associated_internal_fn (built_in_function fn, tree return_type)
2155 switch (fn)
2157 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2158 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2159 #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2160 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2161 CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2162 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2163 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2164 #include "internal-fn.def"
2166 CASE_FLT_FN (BUILT_IN_POW10):
2167 return IFN_EXP10;
2169 CASE_FLT_FN (BUILT_IN_DREM):
2170 return IFN_REMAINDER;
2172 CASE_FLT_FN (BUILT_IN_SCALBN):
2173 CASE_FLT_FN (BUILT_IN_SCALBLN):
2174 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2175 return IFN_LDEXP;
2176 return IFN_LAST;
2178 default:
2179 return IFN_LAST;
2183 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2184 return its code, otherwise return IFN_LAST. Note that this function
2185 only tests whether the function is defined in internals.def, not whether
2186 it is actually available on the target. */
2188 internal_fn
2189 associated_internal_fn (tree fndecl)
2191 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2192 return associated_internal_fn (DECL_FUNCTION_CODE (fndecl),
2193 TREE_TYPE (TREE_TYPE (fndecl)));
2196 /* Check whether there is an internal function associated with function CFN
2197 and return type RETURN_TYPE. Return the function if so, otherwise return
2198 IFN_LAST.
2200 Note that this function only tests whether the function is defined in
2201 internals.def, not whether it is actually available on the target. */
2203 internal_fn
2204 associated_internal_fn (combined_fn cfn, tree return_type)
2206 if (internal_fn_p (cfn))
2207 return as_internal_fn (cfn);
2208 return associated_internal_fn (as_builtin_fn (cfn), return_type);
2211 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2212 on the current target by a call to an internal function, return the
2213 code of that internal function, otherwise return IFN_LAST. The caller
2214 is responsible for ensuring that any side-effects of the built-in
2215 call are dealt with correctly. E.g. if CALL sets errno, the caller
2216 must decide that the errno result isn't needed or make it available
2217 in some other way. */
2219 internal_fn
2220 replacement_internal_fn (gcall *call)
2222 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2224 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2225 if (ifn != IFN_LAST)
2227 tree_pair types = direct_internal_fn_types (ifn, call);
2228 optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2229 if (direct_internal_fn_supported_p (ifn, types, opt_type))
2230 return ifn;
2233 return IFN_LAST;
2236 /* Expand a call to the builtin trinary math functions (fma).
2237 Return NULL_RTX if a normal call should be emitted rather than expanding the
2238 function in-line. EXP is the expression that is a call to the builtin
2239 function; if convenient, the result should be placed in TARGET.
2240 SUBTARGET may be used as the target for computing one of EXP's
2241 operands. */
2243 static rtx
2244 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2246 optab builtin_optab;
2247 rtx op0, op1, op2, result;
2248 rtx_insn *insns;
2249 tree fndecl = get_callee_fndecl (exp);
2250 tree arg0, arg1, arg2;
2251 machine_mode mode;
2253 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2254 return NULL_RTX;
2256 arg0 = CALL_EXPR_ARG (exp, 0);
2257 arg1 = CALL_EXPR_ARG (exp, 1);
2258 arg2 = CALL_EXPR_ARG (exp, 2);
2260 switch (DECL_FUNCTION_CODE (fndecl))
2262 CASE_FLT_FN (BUILT_IN_FMA):
2263 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2264 builtin_optab = fma_optab; break;
2265 default:
2266 gcc_unreachable ();
2269 /* Make a suitable register to place result in. */
2270 mode = TYPE_MODE (TREE_TYPE (exp));
2272 /* Before working hard, check whether the instruction is available. */
2273 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2274 return NULL_RTX;
2276 result = gen_reg_rtx (mode);
2278 /* Always stabilize the argument list. */
2279 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2280 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2281 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2283 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2284 op1 = expand_normal (arg1);
2285 op2 = expand_normal (arg2);
2287 start_sequence ();
2289 /* Compute into RESULT.
2290 Set RESULT to wherever the result comes back. */
2291 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2292 result, 0);
2294 /* If we were unable to expand via the builtin, stop the sequence
2295 (without outputting the insns) and call to the library function
2296 with the stabilized argument list. */
2297 if (result == 0)
2299 end_sequence ();
2300 return expand_call (exp, target, target == const0_rtx);
2303 /* Output the entire sequence. */
2304 insns = get_insns ();
2305 end_sequence ();
2306 emit_insn (insns);
2308 return result;
2311 /* Expand a call to the builtin sin and cos math functions.
2312 Return NULL_RTX if a normal call should be emitted rather than expanding the
2313 function in-line. EXP is the expression that is a call to the builtin
2314 function; if convenient, the result should be placed in TARGET.
2315 SUBTARGET may be used as the target for computing one of EXP's
2316 operands. */
2318 static rtx
2319 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2321 optab builtin_optab;
2322 rtx op0;
2323 rtx_insn *insns;
2324 tree fndecl = get_callee_fndecl (exp);
2325 machine_mode mode;
2326 tree arg;
2328 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2329 return NULL_RTX;
2331 arg = CALL_EXPR_ARG (exp, 0);
2333 switch (DECL_FUNCTION_CODE (fndecl))
2335 CASE_FLT_FN (BUILT_IN_SIN):
2336 CASE_FLT_FN (BUILT_IN_COS):
2337 builtin_optab = sincos_optab; break;
2338 default:
2339 gcc_unreachable ();
2342 /* Make a suitable register to place result in. */
2343 mode = TYPE_MODE (TREE_TYPE (exp));
2345 /* Check if sincos insn is available, otherwise fallback
2346 to sin or cos insn. */
2347 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2348 switch (DECL_FUNCTION_CODE (fndecl))
2350 CASE_FLT_FN (BUILT_IN_SIN):
2351 builtin_optab = sin_optab; break;
2352 CASE_FLT_FN (BUILT_IN_COS):
2353 builtin_optab = cos_optab; break;
2354 default:
2355 gcc_unreachable ();
2358 /* Before working hard, check whether the instruction is available. */
2359 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2361 rtx result = gen_reg_rtx (mode);
2363 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2364 need to expand the argument again. This way, we will not perform
2365 side-effects more the once. */
2366 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2368 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2370 start_sequence ();
2372 /* Compute into RESULT.
2373 Set RESULT to wherever the result comes back. */
2374 if (builtin_optab == sincos_optab)
2376 int ok;
2378 switch (DECL_FUNCTION_CODE (fndecl))
2380 CASE_FLT_FN (BUILT_IN_SIN):
2381 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2382 break;
2383 CASE_FLT_FN (BUILT_IN_COS):
2384 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2385 break;
2386 default:
2387 gcc_unreachable ();
2389 gcc_assert (ok);
2391 else
2392 result = expand_unop (mode, builtin_optab, op0, result, 0);
2394 if (result != 0)
2396 /* Output the entire sequence. */
2397 insns = get_insns ();
2398 end_sequence ();
2399 emit_insn (insns);
2400 return result;
2403 /* If we were unable to expand via the builtin, stop the sequence
2404 (without outputting the insns) and call to the library function
2405 with the stabilized argument list. */
2406 end_sequence ();
2409 return expand_call (exp, target, target == const0_rtx);
2412 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2413 return an RTL instruction code that implements the functionality.
2414 If that isn't possible or available return CODE_FOR_nothing. */
2416 static enum insn_code
2417 interclass_mathfn_icode (tree arg, tree fndecl)
2419 bool errno_set = false;
2420 optab builtin_optab = unknown_optab;
2421 machine_mode mode;
2423 switch (DECL_FUNCTION_CODE (fndecl))
2425 CASE_FLT_FN (BUILT_IN_ILOGB):
2426 errno_set = true; builtin_optab = ilogb_optab; break;
2427 CASE_FLT_FN (BUILT_IN_ISINF):
2428 builtin_optab = isinf_optab; break;
2429 case BUILT_IN_ISNORMAL:
2430 case BUILT_IN_ISFINITE:
2431 CASE_FLT_FN (BUILT_IN_FINITE):
2432 case BUILT_IN_FINITED32:
2433 case BUILT_IN_FINITED64:
2434 case BUILT_IN_FINITED128:
2435 case BUILT_IN_ISINFD32:
2436 case BUILT_IN_ISINFD64:
2437 case BUILT_IN_ISINFD128:
2438 /* These builtins have no optabs (yet). */
2439 break;
2440 default:
2441 gcc_unreachable ();
2444 /* There's no easy way to detect the case we need to set EDOM. */
2445 if (flag_errno_math && errno_set)
2446 return CODE_FOR_nothing;
2448 /* Optab mode depends on the mode of the input argument. */
2449 mode = TYPE_MODE (TREE_TYPE (arg));
2451 if (builtin_optab)
2452 return optab_handler (builtin_optab, mode);
2453 return CODE_FOR_nothing;
2456 /* Expand a call to one of the builtin math functions that operate on
2457 floating point argument and output an integer result (ilogb, isinf,
2458 isnan, etc).
2459 Return 0 if a normal call should be emitted rather than expanding the
2460 function in-line. EXP is the expression that is a call to the builtin
2461 function; if convenient, the result should be placed in TARGET. */
2463 static rtx
2464 expand_builtin_interclass_mathfn (tree exp, rtx target)
2466 enum insn_code icode = CODE_FOR_nothing;
2467 rtx op0;
2468 tree fndecl = get_callee_fndecl (exp);
2469 machine_mode mode;
2470 tree arg;
2472 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2473 return NULL_RTX;
2475 arg = CALL_EXPR_ARG (exp, 0);
2476 icode = interclass_mathfn_icode (arg, fndecl);
2477 mode = TYPE_MODE (TREE_TYPE (arg));
2479 if (icode != CODE_FOR_nothing)
2481 class expand_operand ops[1];
2482 rtx_insn *last = get_last_insn ();
2483 tree orig_arg = arg;
2485 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2486 need to expand the argument again. This way, we will not perform
2487 side-effects more the once. */
2488 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2490 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2492 if (mode != GET_MODE (op0))
2493 op0 = convert_to_mode (mode, op0, 0);
2495 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2496 if (maybe_legitimize_operands (icode, 0, 1, ops)
2497 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2498 return ops[0].value;
2500 delete_insns_since (last);
2501 CALL_EXPR_ARG (exp, 0) = orig_arg;
2504 return NULL_RTX;
2507 /* Expand a call to the builtin sincos math function.
2508 Return NULL_RTX if a normal call should be emitted rather than expanding the
2509 function in-line. EXP is the expression that is a call to the builtin
2510 function. */
2512 static rtx
2513 expand_builtin_sincos (tree exp)
2515 rtx op0, op1, op2, target1, target2;
2516 machine_mode mode;
2517 tree arg, sinp, cosp;
2518 int result;
2519 location_t loc = EXPR_LOCATION (exp);
2520 tree alias_type, alias_off;
2522 if (!validate_arglist (exp, REAL_TYPE,
2523 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2524 return NULL_RTX;
2526 arg = CALL_EXPR_ARG (exp, 0);
2527 sinp = CALL_EXPR_ARG (exp, 1);
2528 cosp = CALL_EXPR_ARG (exp, 2);
2530 /* Make a suitable register to place result in. */
2531 mode = TYPE_MODE (TREE_TYPE (arg));
2533 /* Check if sincos insn is available, otherwise emit the call. */
2534 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2535 return NULL_RTX;
2537 target1 = gen_reg_rtx (mode);
2538 target2 = gen_reg_rtx (mode);
2540 op0 = expand_normal (arg);
2541 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2542 alias_off = build_int_cst (alias_type, 0);
2543 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2544 sinp, alias_off));
2545 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2546 cosp, alias_off));
2548 /* Compute into target1 and target2.
2549 Set TARGET to wherever the result comes back. */
2550 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2551 gcc_assert (result);
2553 /* Move target1 and target2 to the memory locations indicated
2554 by op1 and op2. */
2555 emit_move_insn (op1, target1);
2556 emit_move_insn (op2, target2);
2558 return const0_rtx;
2561 /* Expand call EXP to the fegetround builtin (from C99 fenv.h), returning the
2562 result and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2563 static rtx
2564 expand_builtin_fegetround (tree exp, rtx target, machine_mode target_mode)
2566 if (!validate_arglist (exp, VOID_TYPE))
2567 return NULL_RTX;
2569 insn_code icode = direct_optab_handler (fegetround_optab, SImode);
2570 if (icode == CODE_FOR_nothing)
2571 return NULL_RTX;
2573 if (target == 0
2574 || GET_MODE (target) != target_mode
2575 || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2576 target = gen_reg_rtx (target_mode);
2578 rtx pat = GEN_FCN (icode) (target);
2579 if (!pat)
2580 return NULL_RTX;
2581 emit_insn (pat);
2583 return target;
2586 /* Expand call EXP to either feclearexcept or feraiseexcept builtins (from C99
2587 fenv.h), returning the result and setting it in TARGET. Otherwise return
2588 NULL_RTX on failure. */
2589 static rtx
2590 expand_builtin_feclear_feraise_except (tree exp, rtx target,
2591 machine_mode target_mode, optab op_optab)
2593 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
2594 return NULL_RTX;
2595 rtx op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
2597 insn_code icode = direct_optab_handler (op_optab, SImode);
2598 if (icode == CODE_FOR_nothing)
2599 return NULL_RTX;
2601 if (!(*insn_data[icode].operand[1].predicate) (op0, GET_MODE (op0)))
2602 return NULL_RTX;
2604 if (target == 0
2605 || GET_MODE (target) != target_mode
2606 || !(*insn_data[icode].operand[0].predicate) (target, target_mode))
2607 target = gen_reg_rtx (target_mode);
2609 rtx pat = GEN_FCN (icode) (target, op0);
2610 if (!pat)
2611 return NULL_RTX;
2612 emit_insn (pat);
2614 return target;
2617 /* Expand a call to the internal cexpi builtin to the sincos math function.
2618 EXP is the expression that is a call to the builtin function; if convenient,
2619 the result should be placed in TARGET. */
2621 static rtx
2622 expand_builtin_cexpi (tree exp, rtx target)
2624 tree fndecl = get_callee_fndecl (exp);
2625 tree arg, type;
2626 machine_mode mode;
2627 rtx op0, op1, op2;
2628 location_t loc = EXPR_LOCATION (exp);
2630 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2631 return NULL_RTX;
2633 arg = CALL_EXPR_ARG (exp, 0);
2634 type = TREE_TYPE (arg);
2635 mode = TYPE_MODE (TREE_TYPE (arg));
2637 /* Try expanding via a sincos optab, fall back to emitting a libcall
2638 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2639 is only generated from sincos, cexp or if we have either of them. */
2640 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2642 op1 = gen_reg_rtx (mode);
2643 op2 = gen_reg_rtx (mode);
2645 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2647 /* Compute into op1 and op2. */
2648 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2650 else if (targetm.libc_has_function (function_sincos, type))
2652 tree call, fn = NULL_TREE;
2653 tree top1, top2;
2654 rtx op1a, op2a;
2656 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2657 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2658 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2659 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2660 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2661 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2662 else
2663 gcc_unreachable ();
2665 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2666 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2667 op1a = copy_addr_to_reg (XEXP (op1, 0));
2668 op2a = copy_addr_to_reg (XEXP (op2, 0));
2669 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2670 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2672 /* Make sure not to fold the sincos call again. */
2673 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2674 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2675 call, 3, arg, top1, top2));
2677 else
2679 tree call, fn = NULL_TREE, narg;
2680 tree ctype = build_complex_type (type);
2682 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2683 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2684 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2685 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2686 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2687 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2688 else
2689 gcc_unreachable ();
2691 /* If we don't have a decl for cexp create one. This is the
2692 friendliest fallback if the user calls __builtin_cexpi
2693 without full target C99 function support. */
2694 if (fn == NULL_TREE)
2696 tree fntype;
2697 const char *name = NULL;
2699 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2700 name = "cexpf";
2701 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2702 name = "cexp";
2703 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2704 name = "cexpl";
2706 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2707 fn = build_fn_decl (name, fntype);
2710 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2711 build_real (type, dconst0), arg);
2713 /* Make sure not to fold the cexp call again. */
2714 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2715 return expand_expr (build_call_nary (ctype, call, 1, narg),
2716 target, VOIDmode, EXPAND_NORMAL);
2719 /* Now build the proper return type. */
2720 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2721 make_tree (TREE_TYPE (arg), op2),
2722 make_tree (TREE_TYPE (arg), op1)),
2723 target, VOIDmode, EXPAND_NORMAL);
2726 /* Conveniently construct a function call expression. FNDECL names the
2727 function to be called, N is the number of arguments, and the "..."
2728 parameters are the argument expressions. Unlike build_call_exr
2729 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2731 static tree
2732 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2734 va_list ap;
2735 tree fntype = TREE_TYPE (fndecl);
2736 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2738 va_start (ap, n);
2739 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2740 va_end (ap);
2741 SET_EXPR_LOCATION (fn, loc);
2742 return fn;
2745 /* Expand a call to one of the builtin rounding functions gcc defines
2746 as an extension (lfloor and lceil). As these are gcc extensions we
2747 do not need to worry about setting errno to EDOM.
2748 If expanding via optab fails, lower expression to (int)(floor(x)).
2749 EXP is the expression that is a call to the builtin function;
2750 if convenient, the result should be placed in TARGET. */
2752 static rtx
2753 expand_builtin_int_roundingfn (tree exp, rtx target)
2755 convert_optab builtin_optab;
2756 rtx op0, tmp;
2757 rtx_insn *insns;
2758 tree fndecl = get_callee_fndecl (exp);
2759 enum built_in_function fallback_fn;
2760 tree fallback_fndecl;
2761 machine_mode mode;
2762 tree arg;
2764 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2765 return NULL_RTX;
2767 arg = CALL_EXPR_ARG (exp, 0);
2769 switch (DECL_FUNCTION_CODE (fndecl))
2771 CASE_FLT_FN (BUILT_IN_ICEIL):
2772 CASE_FLT_FN (BUILT_IN_LCEIL):
2773 CASE_FLT_FN (BUILT_IN_LLCEIL):
2774 builtin_optab = lceil_optab;
2775 fallback_fn = BUILT_IN_CEIL;
2776 break;
2778 CASE_FLT_FN (BUILT_IN_IFLOOR):
2779 CASE_FLT_FN (BUILT_IN_LFLOOR):
2780 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2781 builtin_optab = lfloor_optab;
2782 fallback_fn = BUILT_IN_FLOOR;
2783 break;
2785 default:
2786 gcc_unreachable ();
2789 /* Make a suitable register to place result in. */
2790 mode = TYPE_MODE (TREE_TYPE (exp));
2792 target = gen_reg_rtx (mode);
2794 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2795 need to expand the argument again. This way, we will not perform
2796 side-effects more the once. */
2797 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2799 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2801 start_sequence ();
2803 /* Compute into TARGET. */
2804 if (expand_sfix_optab (target, op0, builtin_optab))
2806 /* Output the entire sequence. */
2807 insns = get_insns ();
2808 end_sequence ();
2809 emit_insn (insns);
2810 return target;
2813 /* If we were unable to expand via the builtin, stop the sequence
2814 (without outputting the insns). */
2815 end_sequence ();
2817 /* Fall back to floating point rounding optab. */
2818 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2820 /* For non-C99 targets we may end up without a fallback fndecl here
2821 if the user called __builtin_lfloor directly. In this case emit
2822 a call to the floor/ceil variants nevertheless. This should result
2823 in the best user experience for not full C99 targets. */
2824 if (fallback_fndecl == NULL_TREE)
2826 tree fntype;
2827 const char *name = NULL;
2829 switch (DECL_FUNCTION_CODE (fndecl))
2831 case BUILT_IN_ICEIL:
2832 case BUILT_IN_LCEIL:
2833 case BUILT_IN_LLCEIL:
2834 name = "ceil";
2835 break;
2836 case BUILT_IN_ICEILF:
2837 case BUILT_IN_LCEILF:
2838 case BUILT_IN_LLCEILF:
2839 name = "ceilf";
2840 break;
2841 case BUILT_IN_ICEILL:
2842 case BUILT_IN_LCEILL:
2843 case BUILT_IN_LLCEILL:
2844 name = "ceill";
2845 break;
2846 case BUILT_IN_IFLOOR:
2847 case BUILT_IN_LFLOOR:
2848 case BUILT_IN_LLFLOOR:
2849 name = "floor";
2850 break;
2851 case BUILT_IN_IFLOORF:
2852 case BUILT_IN_LFLOORF:
2853 case BUILT_IN_LLFLOORF:
2854 name = "floorf";
2855 break;
2856 case BUILT_IN_IFLOORL:
2857 case BUILT_IN_LFLOORL:
2858 case BUILT_IN_LLFLOORL:
2859 name = "floorl";
2860 break;
2861 default:
2862 gcc_unreachable ();
2865 fntype = build_function_type_list (TREE_TYPE (arg),
2866 TREE_TYPE (arg), NULL_TREE);
2867 fallback_fndecl = build_fn_decl (name, fntype);
2870 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2872 tmp = expand_normal (exp);
2873 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2875 /* Truncate the result of floating point optab to integer
2876 via expand_fix (). */
2877 target = gen_reg_rtx (mode);
2878 expand_fix (target, tmp, 0);
2880 return target;
2883 /* Expand a call to one of the builtin math functions doing integer
2884 conversion (lrint).
2885 Return 0 if a normal call should be emitted rather than expanding the
2886 function in-line. EXP is the expression that is a call to the builtin
2887 function; if convenient, the result should be placed in TARGET. */
2889 static rtx
2890 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2892 convert_optab builtin_optab;
2893 rtx op0;
2894 rtx_insn *insns;
2895 tree fndecl = get_callee_fndecl (exp);
2896 tree arg;
2897 machine_mode mode;
2898 enum built_in_function fallback_fn = BUILT_IN_NONE;
2900 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2901 return NULL_RTX;
2903 arg = CALL_EXPR_ARG (exp, 0);
2905 switch (DECL_FUNCTION_CODE (fndecl))
2907 CASE_FLT_FN (BUILT_IN_IRINT):
2908 fallback_fn = BUILT_IN_LRINT;
2909 gcc_fallthrough ();
2910 CASE_FLT_FN (BUILT_IN_LRINT):
2911 CASE_FLT_FN (BUILT_IN_LLRINT):
2912 builtin_optab = lrint_optab;
2913 break;
2915 CASE_FLT_FN (BUILT_IN_IROUND):
2916 fallback_fn = BUILT_IN_LROUND;
2917 gcc_fallthrough ();
2918 CASE_FLT_FN (BUILT_IN_LROUND):
2919 CASE_FLT_FN (BUILT_IN_LLROUND):
2920 builtin_optab = lround_optab;
2921 break;
2923 default:
2924 gcc_unreachable ();
2927 /* There's no easy way to detect the case we need to set EDOM. */
2928 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2929 return NULL_RTX;
2931 /* Make a suitable register to place result in. */
2932 mode = TYPE_MODE (TREE_TYPE (exp));
2934 /* There's no easy way to detect the case we need to set EDOM. */
2935 if (!flag_errno_math)
2937 rtx result = gen_reg_rtx (mode);
2939 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2940 need to expand the argument again. This way, we will not perform
2941 side-effects more the once. */
2942 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2944 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2946 start_sequence ();
2948 if (expand_sfix_optab (result, op0, builtin_optab))
2950 /* Output the entire sequence. */
2951 insns = get_insns ();
2952 end_sequence ();
2953 emit_insn (insns);
2954 return result;
2957 /* If we were unable to expand via the builtin, stop the sequence
2958 (without outputting the insns) and call to the library function
2959 with the stabilized argument list. */
2960 end_sequence ();
2963 if (fallback_fn != BUILT_IN_NONE)
2965 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2966 targets, (int) round (x) should never be transformed into
2967 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2968 a call to lround in the hope that the target provides at least some
2969 C99 functions. This should result in the best user experience for
2970 not full C99 targets.
2971 As scalar float conversions with same mode are useless in GIMPLE,
2972 we can end up e.g. with _Float32 argument passed to float builtin,
2973 try to get the type from the builtin prototype first. */
2974 tree fallback_fndecl = NULL_TREE;
2975 if (tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
2976 fallback_fndecl
2977 = mathfn_built_in_1 (TREE_VALUE (argtypes),
2978 as_combined_fn (fallback_fn), 0);
2979 if (fallback_fndecl == NULL_TREE)
2980 fallback_fndecl
2981 = mathfn_built_in_1 (TREE_TYPE (arg),
2982 as_combined_fn (fallback_fn), 0);
2983 if (fallback_fndecl)
2985 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2986 fallback_fndecl, 1, arg);
2988 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2989 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2990 return convert_to_mode (mode, target, 0);
2994 return expand_call (exp, target, target == const0_rtx);
2997 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2998 a normal call should be emitted rather than expanding the function
2999 in-line. EXP is the expression that is a call to the builtin
3000 function; if convenient, the result should be placed in TARGET. */
3002 static rtx
3003 expand_builtin_powi (tree exp, rtx target)
3005 tree arg0, arg1;
3006 rtx op0, op1;
3007 machine_mode mode;
3008 machine_mode mode2;
3010 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
3011 return NULL_RTX;
3013 arg0 = CALL_EXPR_ARG (exp, 0);
3014 arg1 = CALL_EXPR_ARG (exp, 1);
3015 mode = TYPE_MODE (TREE_TYPE (exp));
3017 /* Emit a libcall to libgcc. */
3019 /* Mode of the 2nd argument must match that of an int. */
3020 mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
3022 if (target == NULL_RTX)
3023 target = gen_reg_rtx (mode);
3025 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
3026 if (GET_MODE (op0) != mode)
3027 op0 = convert_to_mode (mode, op0, 0);
3028 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
3029 if (GET_MODE (op1) != mode2)
3030 op1 = convert_to_mode (mode2, op1, 0);
3032 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
3033 target, LCT_CONST, mode,
3034 op0, mode, op1, mode2);
3036 return target;
3039 /* Expand expression EXP which is a call to the strlen builtin. Return
3040 NULL_RTX if we failed and the caller should emit a normal call, otherwise
3041 try to get the result in TARGET, if convenient. */
3043 static rtx
3044 expand_builtin_strlen (tree exp, rtx target,
3045 machine_mode target_mode)
3047 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
3048 return NULL_RTX;
3050 tree src = CALL_EXPR_ARG (exp, 0);
3052 /* If the length can be computed at compile-time, return it. */
3053 if (tree len = c_strlen (src, 0))
3054 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3056 /* If the length can be computed at compile-time and is constant
3057 integer, but there are side-effects in src, evaluate
3058 src for side-effects, then return len.
3059 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
3060 can be optimized into: i++; x = 3; */
3061 tree len = c_strlen (src, 1);
3062 if (len && TREE_CODE (len) == INTEGER_CST)
3064 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
3065 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3068 unsigned int align = get_pointer_alignment (src) / BITS_PER_UNIT;
3070 /* If SRC is not a pointer type, don't do this operation inline. */
3071 if (align == 0)
3072 return NULL_RTX;
3074 /* Bail out if we can't compute strlen in the right mode. */
3075 machine_mode insn_mode;
3076 enum insn_code icode = CODE_FOR_nothing;
3077 FOR_EACH_MODE_FROM (insn_mode, target_mode)
3079 icode = optab_handler (strlen_optab, insn_mode);
3080 if (icode != CODE_FOR_nothing)
3081 break;
3083 if (insn_mode == VOIDmode)
3084 return NULL_RTX;
3086 /* Make a place to hold the source address. We will not expand
3087 the actual source until we are sure that the expansion will
3088 not fail -- there are trees that cannot be expanded twice. */
3089 rtx src_reg = gen_reg_rtx (Pmode);
3091 /* Mark the beginning of the strlen sequence so we can emit the
3092 source operand later. */
3093 rtx_insn *before_strlen = get_last_insn ();
3095 class expand_operand ops[4];
3096 create_output_operand (&ops[0], target, insn_mode);
3097 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
3098 create_integer_operand (&ops[2], 0);
3099 create_integer_operand (&ops[3], align);
3100 if (!maybe_expand_insn (icode, 4, ops))
3101 return NULL_RTX;
3103 /* Check to see if the argument was declared attribute nonstring
3104 and if so, issue a warning since at this point it's not known
3105 to be nul-terminated. */
3106 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3108 /* Now that we are assured of success, expand the source. */
3109 start_sequence ();
3110 rtx pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
3111 if (pat != src_reg)
3113 #ifdef POINTERS_EXTEND_UNSIGNED
3114 if (GET_MODE (pat) != Pmode)
3115 pat = convert_to_mode (Pmode, pat,
3116 POINTERS_EXTEND_UNSIGNED);
3117 #endif
3118 emit_move_insn (src_reg, pat);
3120 pat = get_insns ();
3121 end_sequence ();
3123 if (before_strlen)
3124 emit_insn_after (pat, before_strlen);
3125 else
3126 emit_insn_before (pat, get_insns ());
3128 /* Return the value in the proper mode for this function. */
3129 if (GET_MODE (ops[0].value) == target_mode)
3130 target = ops[0].value;
3131 else if (target != 0)
3132 convert_move (target, ops[0].value, 0);
3133 else
3134 target = convert_to_mode (target_mode, ops[0].value, 0);
3136 return target;
3139 /* Expand call EXP to the strnlen built-in, returning the result
3140 and setting it in TARGET. Otherwise return NULL_RTX on failure. */
3142 static rtx
3143 expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
3145 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3146 return NULL_RTX;
3148 tree src = CALL_EXPR_ARG (exp, 0);
3149 tree bound = CALL_EXPR_ARG (exp, 1);
3151 if (!bound)
3152 return NULL_RTX;
3154 location_t loc = UNKNOWN_LOCATION;
3155 if (EXPR_HAS_LOCATION (exp))
3156 loc = EXPR_LOCATION (exp);
3158 /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
3159 so these conversions aren't necessary. */
3160 c_strlen_data lendata = { };
3161 tree len = c_strlen (src, 0, &lendata, 1);
3162 if (len)
3163 len = fold_convert_loc (loc, TREE_TYPE (bound), len);
3165 if (TREE_CODE (bound) == INTEGER_CST)
3167 if (!len)
3168 return NULL_RTX;
3170 len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
3171 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3174 if (TREE_CODE (bound) != SSA_NAME)
3175 return NULL_RTX;
3177 wide_int min, max;
3178 value_range r;
3179 get_global_range_query ()->range_of_expr (r, bound);
3180 if (r.kind () != VR_RANGE)
3181 return NULL_RTX;
3182 min = r.lower_bound ();
3183 max = r.upper_bound ();
3185 if (!len || TREE_CODE (len) != INTEGER_CST)
3187 bool exact;
3188 lendata.decl = unterminated_array (src, &len, &exact);
3189 if (!lendata.decl)
3190 return NULL_RTX;
3193 if (lendata.decl)
3194 return NULL_RTX;
3196 if (wi::gtu_p (min, wi::to_wide (len)))
3197 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3199 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound);
3200 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3203 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3204 bytes from bytes at DATA + OFFSET and return it reinterpreted as
3205 a target constant. */
3207 static rtx
3208 builtin_memcpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3209 fixed_size_mode mode)
3211 /* The REPresentation pointed to by DATA need not be a nul-terminated
3212 string but the caller guarantees it's large enough for MODE. */
3213 const char *rep = (const char *) data;
3215 /* The by-pieces infrastructure does not try to pick a vector mode
3216 for memcpy expansion. */
3217 return c_readstr (rep + offset, as_a <scalar_int_mode> (mode),
3218 /*nul_terminated=*/false);
3221 /* LEN specify length of the block of memcpy/memset operation.
3222 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3223 In some cases we can make very likely guess on max size, then we
3224 set it into PROBABLE_MAX_SIZE. */
3226 static void
3227 determine_block_size (tree len, rtx len_rtx,
3228 unsigned HOST_WIDE_INT *min_size,
3229 unsigned HOST_WIDE_INT *max_size,
3230 unsigned HOST_WIDE_INT *probable_max_size)
3232 if (CONST_INT_P (len_rtx))
3234 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3235 return;
3237 else
3239 wide_int min, max;
3240 enum value_range_kind range_type = VR_UNDEFINED;
3242 /* Determine bounds from the type. */
3243 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3244 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3245 else
3246 *min_size = 0;
3247 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3248 *probable_max_size = *max_size
3249 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3250 else
3251 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3253 if (TREE_CODE (len) == SSA_NAME)
3255 value_range r;
3256 get_global_range_query ()->range_of_expr (r, len);
3257 range_type = r.kind ();
3258 if (range_type != VR_UNDEFINED)
3260 min = wi::to_wide (r.min ());
3261 max = wi::to_wide (r.max ());
3264 if (range_type == VR_RANGE)
3266 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3267 *min_size = min.to_uhwi ();
3268 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3269 *probable_max_size = *max_size = max.to_uhwi ();
3271 else if (range_type == VR_ANTI_RANGE)
3273 /* Code like
3275 int n;
3276 if (n < 100)
3277 memcpy (a, b, n)
3279 Produce anti range allowing negative values of N. We still
3280 can use the information and make a guess that N is not negative.
3282 if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3283 *probable_max_size = min.to_uhwi () - 1;
3286 gcc_checking_assert (*max_size <=
3287 (unsigned HOST_WIDE_INT)
3288 GET_MODE_MASK (GET_MODE (len_rtx)));
3291 /* Expand a call EXP to the memcpy builtin.
3292 Return NULL_RTX if we failed, the caller should emit a normal call,
3293 otherwise try to get the result in TARGET, if convenient (and in
3294 mode MODE if that's convenient). */
3296 static rtx
3297 expand_builtin_memcpy (tree exp, rtx target)
3299 if (!validate_arglist (exp,
3300 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3301 return NULL_RTX;
3303 tree dest = CALL_EXPR_ARG (exp, 0);
3304 tree src = CALL_EXPR_ARG (exp, 1);
3305 tree len = CALL_EXPR_ARG (exp, 2);
3307 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3308 /*retmode=*/ RETURN_BEGIN, false);
3311 /* Check a call EXP to the memmove built-in for validity.
3312 Return NULL_RTX on both success and failure. */
3314 static rtx
3315 expand_builtin_memmove (tree exp, rtx target)
3317 if (!validate_arglist (exp,
3318 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3319 return NULL_RTX;
3321 tree dest = CALL_EXPR_ARG (exp, 0);
3322 tree src = CALL_EXPR_ARG (exp, 1);
3323 tree len = CALL_EXPR_ARG (exp, 2);
3325 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3326 /*retmode=*/ RETURN_BEGIN, true);
3329 /* Expand a call EXP to the mempcpy builtin.
3330 Return NULL_RTX if we failed; the caller should emit a normal call,
3331 otherwise try to get the result in TARGET, if convenient (and in
3332 mode MODE if that's convenient). */
3334 static rtx
3335 expand_builtin_mempcpy (tree exp, rtx target)
3337 if (!validate_arglist (exp,
3338 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3339 return NULL_RTX;
3341 tree dest = CALL_EXPR_ARG (exp, 0);
3342 tree src = CALL_EXPR_ARG (exp, 1);
3343 tree len = CALL_EXPR_ARG (exp, 2);
3345 /* Policy does not generally allow using compute_objsize (which
3346 is used internally by check_memop_size) to change code generation
3347 or drive optimization decisions.
3349 In this instance it is safe because the code we generate has
3350 the same semantics regardless of the return value of
3351 check_memop_sizes. Exactly the same amount of data is copied
3352 and the return value is exactly the same in both cases.
3354 Furthermore, check_memop_size always uses mode 0 for the call to
3355 compute_objsize, so the imprecise nature of compute_objsize is
3356 avoided. */
3358 /* Avoid expanding mempcpy into memcpy when the call is determined
3359 to overflow the buffer. This also prevents the same overflow
3360 from being diagnosed again when expanding memcpy. */
3362 return expand_builtin_mempcpy_args (dest, src, len,
3363 target, exp, /*retmode=*/ RETURN_END);
3366 /* Helper function to do the actual work for expand of memory copy family
3367 functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3368 of memory from SRC to DEST and assign to TARGET if convenient. Return
3369 value is based on RETMODE argument. */
3371 static rtx
3372 expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3373 rtx target, tree exp, memop_ret retmode,
3374 bool might_overlap)
3376 unsigned int src_align = get_pointer_alignment (src);
3377 unsigned int dest_align = get_pointer_alignment (dest);
3378 rtx dest_mem, src_mem, dest_addr, len_rtx;
3379 HOST_WIDE_INT expected_size = -1;
3380 unsigned int expected_align = 0;
3381 unsigned HOST_WIDE_INT min_size;
3382 unsigned HOST_WIDE_INT max_size;
3383 unsigned HOST_WIDE_INT probable_max_size;
3385 bool is_move_done;
3387 /* If DEST is not a pointer type, call the normal function. */
3388 if (dest_align == 0)
3389 return NULL_RTX;
3391 /* If either SRC is not a pointer type, don't do this
3392 operation in-line. */
3393 if (src_align == 0)
3394 return NULL_RTX;
3396 if (currently_expanding_gimple_stmt)
3397 stringop_block_profile (currently_expanding_gimple_stmt,
3398 &expected_align, &expected_size);
3400 if (expected_align < dest_align)
3401 expected_align = dest_align;
3402 dest_mem = get_memory_rtx (dest, len);
3403 set_mem_align (dest_mem, dest_align);
3404 len_rtx = expand_normal (len);
3405 determine_block_size (len, len_rtx, &min_size, &max_size,
3406 &probable_max_size);
3408 /* Try to get the byte representation of the constant SRC points to,
3409 with its byte size in NBYTES. */
3410 unsigned HOST_WIDE_INT nbytes;
3411 const char *rep = getbyterep (src, &nbytes);
3413 /* If the function's constant bound LEN_RTX is less than or equal
3414 to the byte size of the representation of the constant argument,
3415 and if block move would be done by pieces, we can avoid loading
3416 the bytes from memory and only store the computed constant.
3417 This works in the overlap (memmove) case as well because
3418 store_by_pieces just generates a series of stores of constants
3419 from the representation returned by getbyterep(). */
3420 if (rep
3421 && CONST_INT_P (len_rtx)
3422 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes
3423 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3424 CONST_CAST (char *, rep),
3425 dest_align, false))
3427 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3428 builtin_memcpy_read_str,
3429 CONST_CAST (char *, rep),
3430 dest_align, false, retmode);
3431 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3432 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3433 return dest_mem;
3436 src_mem = get_memory_rtx (src, len);
3437 set_mem_align (src_mem, src_align);
3439 /* Copy word part most expediently. */
3440 enum block_op_methods method = BLOCK_OP_NORMAL;
3441 if (CALL_EXPR_TAILCALL (exp)
3442 && (retmode == RETURN_BEGIN || target == const0_rtx))
3443 method = BLOCK_OP_TAILCALL;
3444 bool use_mempcpy_call = (targetm.libc_has_fast_function (BUILT_IN_MEMPCPY)
3445 && retmode == RETURN_END
3446 && !might_overlap
3447 && target != const0_rtx);
3448 if (use_mempcpy_call)
3449 method = BLOCK_OP_NO_LIBCALL_RET;
3450 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3451 expected_align, expected_size,
3452 min_size, max_size, probable_max_size,
3453 use_mempcpy_call, &is_move_done,
3454 might_overlap);
3456 /* Bail out when a mempcpy call would be expanded as libcall and when
3457 we have a target that provides a fast implementation
3458 of mempcpy routine. */
3459 if (!is_move_done)
3460 return NULL_RTX;
3462 if (dest_addr == pc_rtx)
3463 return NULL_RTX;
3465 if (dest_addr == 0)
3467 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3468 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3471 if (retmode != RETURN_BEGIN && target != const0_rtx)
3473 dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3474 /* stpcpy pointer to last byte. */
3475 if (retmode == RETURN_END_MINUS_ONE)
3476 dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3479 return dest_addr;
3482 static rtx
3483 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3484 rtx target, tree orig_exp, memop_ret retmode)
3486 return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3487 retmode, false);
3490 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3491 we failed, the caller should emit a normal call, otherwise try to
3492 get the result in TARGET, if convenient.
3493 Return value is based on RETMODE argument. */
3495 static rtx
3496 expand_movstr (tree dest, tree src, rtx target, memop_ret retmode)
3498 class expand_operand ops[3];
3499 rtx dest_mem;
3500 rtx src_mem;
3502 if (!targetm.have_movstr ())
3503 return NULL_RTX;
3505 dest_mem = get_memory_rtx (dest, NULL);
3506 src_mem = get_memory_rtx (src, NULL);
3507 if (retmode == RETURN_BEGIN)
3509 target = force_reg (Pmode, XEXP (dest_mem, 0));
3510 dest_mem = replace_equiv_address (dest_mem, target);
3513 create_output_operand (&ops[0],
3514 retmode != RETURN_BEGIN ? target : NULL_RTX, Pmode);
3515 create_fixed_operand (&ops[1], dest_mem);
3516 create_fixed_operand (&ops[2], src_mem);
3517 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3518 return NULL_RTX;
3520 if (retmode != RETURN_BEGIN && target != const0_rtx)
3522 target = ops[0].value;
3523 /* movstr is supposed to set end to the address of the NUL
3524 terminator. If the caller requested a mempcpy-like return value,
3525 adjust it. */
3526 if (retmode == RETURN_END)
3528 rtx tem = plus_constant (GET_MODE (target),
3529 gen_lowpart (GET_MODE (target), target), 1);
3530 emit_move_insn (target, force_operand (tem, NULL_RTX));
3533 return target;
3536 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3537 NULL_RTX if we failed the caller should emit a normal call, otherwise
3538 try to get the result in TARGET, if convenient (and in mode MODE if that's
3539 convenient). */
3541 static rtx
3542 expand_builtin_strcpy (tree exp, rtx target)
3544 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3545 return NULL_RTX;
3547 tree dest = CALL_EXPR_ARG (exp, 0);
3548 tree src = CALL_EXPR_ARG (exp, 1);
3550 return expand_builtin_strcpy_args (exp, dest, src, target);
3553 /* Helper function to do the actual work for expand_builtin_strcpy. The
3554 arguments to the builtin_strcpy call DEST and SRC are broken out
3555 so that this can also be called without constructing an actual CALL_EXPR.
3556 The other arguments and return value are the same as for
3557 expand_builtin_strcpy. */
3559 static rtx
3560 expand_builtin_strcpy_args (tree, tree dest, tree src, rtx target)
3562 return expand_movstr (dest, src, target, /*retmode=*/ RETURN_BEGIN);
3565 /* Expand a call EXP to the stpcpy builtin.
3566 Return NULL_RTX if we failed the caller should emit a normal call,
3567 otherwise try to get the result in TARGET, if convenient (and in
3568 mode MODE if that's convenient). */
3570 static rtx
3571 expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
3573 tree dst, src;
3574 location_t loc = EXPR_LOCATION (exp);
3576 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3577 return NULL_RTX;
3579 dst = CALL_EXPR_ARG (exp, 0);
3580 src = CALL_EXPR_ARG (exp, 1);
3582 /* If return value is ignored, transform stpcpy into strcpy. */
3583 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3585 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3586 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3587 return expand_expr (result, target, mode, EXPAND_NORMAL);
3589 else
3591 tree len, lenp1;
3592 rtx ret;
3594 /* Ensure we get an actual string whose length can be evaluated at
3595 compile-time, not an expression containing a string. This is
3596 because the latter will potentially produce pessimized code
3597 when used to produce the return value. */
3598 c_strlen_data lendata = { };
3599 if (!c_getstr (src)
3600 || !(len = c_strlen (src, 0, &lendata, 1)))
3601 return expand_movstr (dst, src, target,
3602 /*retmode=*/ RETURN_END_MINUS_ONE);
3604 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3605 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3606 target, exp,
3607 /*retmode=*/ RETURN_END_MINUS_ONE);
3609 if (ret)
3610 return ret;
3612 if (TREE_CODE (len) == INTEGER_CST)
3614 rtx len_rtx = expand_normal (len);
3616 if (CONST_INT_P (len_rtx))
3618 ret = expand_builtin_strcpy_args (exp, dst, src, target);
3620 if (ret)
3622 if (! target)
3624 if (mode != VOIDmode)
3625 target = gen_reg_rtx (mode);
3626 else
3627 target = gen_reg_rtx (GET_MODE (ret));
3629 if (GET_MODE (target) != GET_MODE (ret))
3630 ret = gen_lowpart (GET_MODE (target), ret);
3632 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3633 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3634 gcc_assert (ret);
3636 return target;
3641 return expand_movstr (dst, src, target,
3642 /*retmode=*/ RETURN_END_MINUS_ONE);
3646 /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
3647 arguments while being careful to avoid duplicate warnings (which could
3648 be issued if the expander were to expand the call, resulting in it
3649 being emitted in expand_call(). */
3651 static rtx
3652 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3654 if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode))
3656 /* The call has been successfully expanded. Check for nonstring
3657 arguments and issue warnings as appropriate. */
3658 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3659 return ret;
3662 return NULL_RTX;
3665 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3666 bytes from constant string DATA + OFFSET and return it as target
3667 constant. */
3670 builtin_strncpy_read_str (void *data, void *, HOST_WIDE_INT offset,
3671 fixed_size_mode mode)
3673 const char *str = (const char *) data;
3675 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3676 return const0_rtx;
3678 /* The by-pieces infrastructure does not try to pick a vector mode
3679 for strncpy expansion. */
3680 return c_readstr (str + offset, as_a <scalar_int_mode> (mode));
3683 /* Helper to check the sizes of sequences and the destination of calls
3684 to __builtin_strncat and __builtin___strncat_chk. Returns true on
3685 success (no overflow or invalid sizes), false otherwise. */
3687 static bool
3688 check_strncat_sizes (tree exp, tree objsize)
3690 tree dest = CALL_EXPR_ARG (exp, 0);
3691 tree src = CALL_EXPR_ARG (exp, 1);
3692 tree maxread = CALL_EXPR_ARG (exp, 2);
3694 /* Try to determine the range of lengths that the source expression
3695 refers to. */
3696 c_strlen_data lendata = { };
3697 get_range_strlen (src, &lendata, /* eltsize = */ 1);
3699 /* Try to verify that the destination is big enough for the shortest
3700 string. */
3702 access_data data (nullptr, exp, access_read_write, maxread, true);
3703 if (!objsize && warn_stringop_overflow)
3705 /* If it hasn't been provided by __strncat_chk, try to determine
3706 the size of the destination object into which the source is
3707 being copied. */
3708 objsize = compute_objsize (dest, warn_stringop_overflow - 1, &data.dst);
3711 /* Add one for the terminating nul. */
3712 tree srclen = (lendata.minlen
3713 ? fold_build2 (PLUS_EXPR, size_type_node, lendata.minlen,
3714 size_one_node)
3715 : NULL_TREE);
3717 /* The strncat function copies at most MAXREAD bytes and always appends
3718 the terminating nul so the specified upper bound should never be equal
3719 to (or greater than) the size of the destination. */
3720 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
3721 && tree_int_cst_equal (objsize, maxread))
3723 location_t loc = EXPR_LOCATION (exp);
3724 warning_at (loc, OPT_Wstringop_overflow_,
3725 "%qD specified bound %E equals destination size",
3726 get_callee_fndecl (exp), maxread);
3728 return false;
3731 if (!srclen
3732 || (maxread && tree_fits_uhwi_p (maxread)
3733 && tree_fits_uhwi_p (srclen)
3734 && tree_int_cst_lt (maxread, srclen)))
3735 srclen = maxread;
3737 /* The number of bytes to write is LEN but check_access will alsoa
3738 check SRCLEN if LEN's value isn't known. */
3739 return check_access (exp, /*dstwrite=*/NULL_TREE, maxread, srclen,
3740 objsize, data.mode, &data);
3743 /* Expand expression EXP, which is a call to the strncpy builtin. Return
3744 NULL_RTX if we failed the caller should emit a normal call. */
3746 static rtx
3747 expand_builtin_strncpy (tree exp, rtx target)
3749 location_t loc = EXPR_LOCATION (exp);
3751 if (!validate_arglist (exp,
3752 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3753 return NULL_RTX;
3754 tree dest = CALL_EXPR_ARG (exp, 0);
3755 tree src = CALL_EXPR_ARG (exp, 1);
3756 /* The number of bytes to write (not the maximum). */
3757 tree len = CALL_EXPR_ARG (exp, 2);
3759 /* The length of the source sequence. */
3760 tree slen = c_strlen (src, 1);
3762 /* We must be passed a constant len and src parameter. */
3763 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
3764 return NULL_RTX;
3766 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
3768 /* We're required to pad with trailing zeros if the requested
3769 len is greater than strlen(s2)+1. In that case try to
3770 use store_by_pieces, if it fails, punt. */
3771 if (tree_int_cst_lt (slen, len))
3773 unsigned int dest_align = get_pointer_alignment (dest);
3774 const char *p = c_getstr (src);
3775 rtx dest_mem;
3777 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
3778 || !can_store_by_pieces (tree_to_uhwi (len),
3779 builtin_strncpy_read_str,
3780 CONST_CAST (char *, p),
3781 dest_align, false))
3782 return NULL_RTX;
3784 dest_mem = get_memory_rtx (dest, len);
3785 store_by_pieces (dest_mem, tree_to_uhwi (len),
3786 builtin_strncpy_read_str,
3787 CONST_CAST (char *, p), dest_align, false,
3788 RETURN_BEGIN);
3789 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3790 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3791 return dest_mem;
3794 return NULL_RTX;
3797 /* Return the RTL of a register in MODE generated from PREV in the
3798 previous iteration. */
3800 static rtx
3801 gen_memset_value_from_prev (by_pieces_prev *prev, fixed_size_mode mode)
3803 rtx target = nullptr;
3804 if (prev != nullptr && prev->data != nullptr)
3806 /* Use the previous data in the same mode. */
3807 if (prev->mode == mode)
3808 return prev->data;
3810 fixed_size_mode prev_mode = prev->mode;
3812 /* Don't use the previous data to write QImode if it is in a
3813 vector mode. */
3814 if (VECTOR_MODE_P (prev_mode) && mode == QImode)
3815 return target;
3817 rtx prev_rtx = prev->data;
3819 if (REG_P (prev_rtx)
3820 && HARD_REGISTER_P (prev_rtx)
3821 && lowpart_subreg_regno (REGNO (prev_rtx), prev_mode, mode) < 0)
3823 /* This case occurs when PREV_MODE is a vector and when
3824 MODE is too small to store using vector operations.
3825 After register allocation, the code will need to move the
3826 lowpart of the vector register into a non-vector register.
3828 Also, the target has chosen to use a hard register
3829 instead of going with the default choice of using a
3830 pseudo register. We should respect that choice and try to
3831 avoid creating a pseudo register with the same mode as the
3832 current hard register.
3834 In principle, we could just use a lowpart MODE subreg of
3835 the vector register. However, the vector register mode might
3836 be too wide for non-vector registers, and we already know
3837 that the non-vector mode is too small for vector registers.
3838 It's therefore likely that we'd need to spill to memory in
3839 the vector mode and reload the non-vector value from there.
3841 Try to avoid that by reducing the vector register to the
3842 smallest size that it can hold. This should increase the
3843 chances that non-vector registers can hold both the inner
3844 and outer modes of the subreg that we generate later. */
3845 machine_mode m;
3846 fixed_size_mode candidate;
3847 FOR_EACH_MODE_IN_CLASS (m, GET_MODE_CLASS (mode))
3848 if (is_a<fixed_size_mode> (m, &candidate))
3850 if (GET_MODE_SIZE (candidate)
3851 >= GET_MODE_SIZE (prev_mode))
3852 break;
3853 if (GET_MODE_SIZE (candidate) >= GET_MODE_SIZE (mode)
3854 && lowpart_subreg_regno (REGNO (prev_rtx),
3855 prev_mode, candidate) >= 0)
3857 target = lowpart_subreg (candidate, prev_rtx,
3858 prev_mode);
3859 prev_rtx = target;
3860 prev_mode = candidate;
3861 break;
3864 if (target == nullptr)
3865 prev_rtx = copy_to_reg (prev_rtx);
3868 target = lowpart_subreg (mode, prev_rtx, prev_mode);
3870 return target;
3873 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3874 bytes from constant string DATA + OFFSET and return it as target
3875 constant. If PREV isn't nullptr, it has the RTL info from the
3876 previous iteration. */
3879 builtin_memset_read_str (void *data, void *prev,
3880 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3881 fixed_size_mode mode)
3883 const char *c = (const char *) data;
3884 unsigned int size = GET_MODE_SIZE (mode);
3886 rtx target = gen_memset_value_from_prev ((by_pieces_prev *) prev,
3887 mode);
3888 if (target != nullptr)
3889 return target;
3890 rtx src = gen_int_mode (*c, QImode);
3892 if (VECTOR_MODE_P (mode))
3894 gcc_assert (GET_MODE_INNER (mode) == QImode);
3896 rtx const_vec = gen_const_vec_duplicate (mode, src);
3897 if (prev == NULL)
3898 /* Return CONST_VECTOR when called by a query function. */
3899 return const_vec;
3901 /* Use the move expander with CONST_VECTOR. */
3902 target = targetm.gen_memset_scratch_rtx (mode);
3903 emit_move_insn (target, const_vec);
3904 return target;
3907 char *p = XALLOCAVEC (char, size);
3909 memset (p, *c, size);
3911 /* Vector modes should be handled above. */
3912 return c_readstr (p, as_a <scalar_int_mode> (mode));
3915 /* Callback routine for store_by_pieces. Return the RTL of a register
3916 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
3917 char value given in the RTL register data. For example, if mode is
3918 4 bytes wide, return the RTL for 0x01010101*data. If PREV isn't
3919 nullptr, it has the RTL info from the previous iteration. */
3921 static rtx
3922 builtin_memset_gen_str (void *data, void *prev,
3923 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
3924 fixed_size_mode mode)
3926 rtx target, coeff;
3927 size_t size;
3928 char *p;
3930 size = GET_MODE_SIZE (mode);
3931 if (size == 1)
3932 return (rtx) data;
3934 target = gen_memset_value_from_prev ((by_pieces_prev *) prev, mode);
3935 if (target != nullptr)
3936 return target;
3938 if (VECTOR_MODE_P (mode))
3940 gcc_assert (GET_MODE_INNER (mode) == QImode);
3942 /* vec_duplicate_optab is a precondition to pick a vector mode for
3943 the memset expander. */
3944 insn_code icode = optab_handler (vec_duplicate_optab, mode);
3946 target = targetm.gen_memset_scratch_rtx (mode);
3947 class expand_operand ops[2];
3948 create_output_operand (&ops[0], target, mode);
3949 create_input_operand (&ops[1], (rtx) data, QImode);
3950 expand_insn (icode, 2, ops);
3951 if (!rtx_equal_p (target, ops[0].value))
3952 emit_move_insn (target, ops[0].value);
3954 return target;
3957 p = XALLOCAVEC (char, size);
3958 memset (p, 1, size);
3959 /* Vector modes should be handled above. */
3960 coeff = c_readstr (p, as_a <scalar_int_mode> (mode));
3962 target = convert_to_mode (mode, (rtx) data, 1);
3963 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
3964 return force_reg (mode, target);
3967 /* Expand expression EXP, which is a call to the memset builtin. Return
3968 NULL_RTX if we failed the caller should emit a normal call, otherwise
3969 try to get the result in TARGET, if convenient (and in mode MODE if that's
3970 convenient). */
3973 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
3975 if (!validate_arglist (exp,
3976 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3977 return NULL_RTX;
3979 tree dest = CALL_EXPR_ARG (exp, 0);
3980 tree val = CALL_EXPR_ARG (exp, 1);
3981 tree len = CALL_EXPR_ARG (exp, 2);
3983 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
3986 /* Try to store VAL (or, if NULL_RTX, VALC) in LEN bytes starting at TO.
3987 Return TRUE if successful, FALSE otherwise. TO is assumed to be
3988 aligned at an ALIGN-bits boundary. LEN must be a multiple of
3989 1<<CTZ_LEN between MIN_LEN and MAX_LEN.
3991 The strategy is to issue one store_by_pieces for each power of two,
3992 from most to least significant, guarded by a test on whether there
3993 are at least that many bytes left to copy in LEN.
3995 ??? Should we skip some powers of two in favor of loops? Maybe start
3996 at the max of TO/LEN/word alignment, at least when optimizing for
3997 size, instead of ensuring O(log len) dynamic compares? */
3999 bool
4000 try_store_by_multiple_pieces (rtx to, rtx len, unsigned int ctz_len,
4001 unsigned HOST_WIDE_INT min_len,
4002 unsigned HOST_WIDE_INT max_len,
4003 rtx val, char valc, unsigned int align)
4005 int max_bits = floor_log2 (max_len);
4006 int min_bits = floor_log2 (min_len);
4007 int sctz_len = ctz_len;
4009 gcc_checking_assert (sctz_len >= 0);
4011 if (val)
4012 valc = 1;
4014 /* Bits more significant than TST_BITS are part of the shared prefix
4015 in the binary representation of both min_len and max_len. Since
4016 they're identical, we don't need to test them in the loop. */
4017 int tst_bits = (max_bits != min_bits ? max_bits
4018 : floor_log2 (max_len ^ min_len));
4020 /* Check whether it's profitable to start by storing a fixed BLKSIZE
4021 bytes, to lower max_bits. In the unlikely case of a constant LEN
4022 (implied by identical MAX_LEN and MIN_LEN), we want to issue a
4023 single store_by_pieces, but otherwise, select the minimum multiple
4024 of the ALIGN (in bytes) and of the MCD of the possible LENs, that
4025 brings MAX_LEN below TST_BITS, if that's lower than min_len. */
4026 unsigned HOST_WIDE_INT blksize;
4027 if (max_len > min_len)
4029 unsigned HOST_WIDE_INT alrng = MAX (HOST_WIDE_INT_1U << ctz_len,
4030 align / BITS_PER_UNIT);
4031 blksize = max_len - (HOST_WIDE_INT_1U << tst_bits) + alrng;
4032 blksize &= ~(alrng - 1);
4034 else if (max_len == min_len)
4035 blksize = max_len;
4036 else
4037 /* Huh, max_len < min_len? Punt. See pr100843.c. */
4038 return false;
4039 if (min_len >= blksize)
4041 min_len -= blksize;
4042 min_bits = floor_log2 (min_len);
4043 max_len -= blksize;
4044 max_bits = floor_log2 (max_len);
4046 tst_bits = (max_bits != min_bits ? max_bits
4047 : floor_log2 (max_len ^ min_len));
4049 else
4050 blksize = 0;
4052 /* Check that we can use store by pieces for the maximum store count
4053 we may issue (initial fixed-size block, plus conditional
4054 power-of-two-sized from max_bits to ctz_len. */
4055 unsigned HOST_WIDE_INT xlenest = blksize;
4056 if (max_bits >= 0)
4057 xlenest += ((HOST_WIDE_INT_1U << max_bits) * 2
4058 - (HOST_WIDE_INT_1U << ctz_len));
4059 if (!can_store_by_pieces (xlenest, builtin_memset_read_str,
4060 &valc, align, true))
4061 return false;
4063 by_pieces_constfn constfun;
4064 void *constfundata;
4065 if (val)
4067 constfun = builtin_memset_gen_str;
4068 constfundata = val = force_reg (TYPE_MODE (unsigned_char_type_node),
4069 val);
4071 else
4073 constfun = builtin_memset_read_str;
4074 constfundata = &valc;
4077 rtx ptr = copy_addr_to_reg (XEXP (to, 0));
4078 rtx rem = copy_to_mode_reg (ptr_mode, convert_to_mode (ptr_mode, len, 0));
4079 to = replace_equiv_address (to, ptr);
4080 set_mem_align (to, align);
4082 if (blksize)
4084 to = store_by_pieces (to, blksize,
4085 constfun, constfundata,
4086 align, true,
4087 max_len != 0 ? RETURN_END : RETURN_BEGIN);
4088 if (max_len == 0)
4089 return true;
4091 /* Adjust PTR, TO and REM. Since TO's address is likely
4092 PTR+offset, we have to replace it. */
4093 emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4094 to = replace_equiv_address (to, ptr);
4095 rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4096 emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4099 /* Iterate over power-of-two block sizes from the maximum length to
4100 the least significant bit possibly set in the length. */
4101 for (int i = max_bits; i >= sctz_len; i--)
4103 rtx_code_label *label = NULL;
4104 blksize = HOST_WIDE_INT_1U << i;
4106 /* If we're past the bits shared between min_ and max_len, expand
4107 a test on the dynamic length, comparing it with the
4108 BLKSIZE. */
4109 if (i <= tst_bits)
4111 label = gen_label_rtx ();
4112 emit_cmp_and_jump_insns (rem, GEN_INT (blksize), LT, NULL,
4113 ptr_mode, 1, label,
4114 profile_probability::even ());
4116 /* If we are at a bit that is in the prefix shared by min_ and
4117 max_len, skip this BLKSIZE if the bit is clear. */
4118 else if ((max_len & blksize) == 0)
4119 continue;
4121 /* Issue a store of BLKSIZE bytes. */
4122 to = store_by_pieces (to, blksize,
4123 constfun, constfundata,
4124 align, true,
4125 i != sctz_len ? RETURN_END : RETURN_BEGIN);
4127 /* Adjust REM and PTR, unless this is the last iteration. */
4128 if (i != sctz_len)
4130 emit_move_insn (ptr, force_operand (XEXP (to, 0), NULL_RTX));
4131 to = replace_equiv_address (to, ptr);
4132 rtx rem_minus_blksize = plus_constant (ptr_mode, rem, -blksize);
4133 emit_move_insn (rem, force_operand (rem_minus_blksize, NULL_RTX));
4136 if (label)
4138 emit_label (label);
4140 /* Given conditional stores, the offset can no longer be
4141 known, so clear it. */
4142 clear_mem_offset (to);
4146 return true;
4149 /* Helper function to do the actual work for expand_builtin_memset. The
4150 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4151 so that this can also be called without constructing an actual CALL_EXPR.
4152 The other arguments and return value are the same as for
4153 expand_builtin_memset. */
4155 static rtx
4156 expand_builtin_memset_args (tree dest, tree val, tree len,
4157 rtx target, machine_mode mode, tree orig_exp)
4159 tree fndecl, fn;
4160 enum built_in_function fcode;
4161 machine_mode val_mode;
4162 char c;
4163 unsigned int dest_align;
4164 rtx dest_mem, dest_addr, len_rtx;
4165 HOST_WIDE_INT expected_size = -1;
4166 unsigned int expected_align = 0;
4167 unsigned HOST_WIDE_INT min_size;
4168 unsigned HOST_WIDE_INT max_size;
4169 unsigned HOST_WIDE_INT probable_max_size;
4171 dest_align = get_pointer_alignment (dest);
4173 /* If DEST is not a pointer type, don't do this operation in-line. */
4174 if (dest_align == 0)
4175 return NULL_RTX;
4177 if (currently_expanding_gimple_stmt)
4178 stringop_block_profile (currently_expanding_gimple_stmt,
4179 &expected_align, &expected_size);
4181 if (expected_align < dest_align)
4182 expected_align = dest_align;
4184 /* If the LEN parameter is zero, return DEST. */
4185 if (integer_zerop (len))
4187 /* Evaluate and ignore VAL in case it has side-effects. */
4188 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4189 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4192 /* Stabilize the arguments in case we fail. */
4193 dest = builtin_save_expr (dest);
4194 val = builtin_save_expr (val);
4195 len = builtin_save_expr (len);
4197 len_rtx = expand_normal (len);
4198 determine_block_size (len, len_rtx, &min_size, &max_size,
4199 &probable_max_size);
4200 dest_mem = get_memory_rtx (dest, len);
4201 val_mode = TYPE_MODE (unsigned_char_type_node);
4203 if (TREE_CODE (val) != INTEGER_CST
4204 || target_char_cast (val, &c))
4206 rtx val_rtx;
4208 val_rtx = expand_normal (val);
4209 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4211 /* Assume that we can memset by pieces if we can store
4212 * the coefficients by pieces (in the required modes).
4213 * We can't pass builtin_memset_gen_str as that emits RTL. */
4214 c = 1;
4215 if (tree_fits_uhwi_p (len)
4216 && can_store_by_pieces (tree_to_uhwi (len),
4217 builtin_memset_read_str, &c, dest_align,
4218 true))
4220 val_rtx = force_reg (val_mode, val_rtx);
4221 store_by_pieces (dest_mem, tree_to_uhwi (len),
4222 builtin_memset_gen_str, val_rtx, dest_align,
4223 true, RETURN_BEGIN);
4225 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4226 dest_align, expected_align,
4227 expected_size, min_size, max_size,
4228 probable_max_size)
4229 && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4230 tree_ctz (len),
4231 min_size, max_size,
4232 val_rtx, 0,
4233 dest_align))
4234 goto do_libcall;
4236 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4237 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4238 return dest_mem;
4241 if (c)
4243 if (tree_fits_uhwi_p (len)
4244 && can_store_by_pieces (tree_to_uhwi (len),
4245 builtin_memset_read_str, &c, dest_align,
4246 true))
4247 store_by_pieces (dest_mem, tree_to_uhwi (len),
4248 builtin_memset_read_str, &c, dest_align, true,
4249 RETURN_BEGIN);
4250 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4251 gen_int_mode (c, val_mode),
4252 dest_align, expected_align,
4253 expected_size, min_size, max_size,
4254 probable_max_size)
4255 && !try_store_by_multiple_pieces (dest_mem, len_rtx,
4256 tree_ctz (len),
4257 min_size, max_size,
4258 NULL_RTX, c,
4259 dest_align))
4260 goto do_libcall;
4262 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4263 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4264 return dest_mem;
4267 set_mem_align (dest_mem, dest_align);
4268 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4269 CALL_EXPR_TAILCALL (orig_exp)
4270 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4271 expected_align, expected_size,
4272 min_size, max_size,
4273 probable_max_size, tree_ctz (len));
4275 if (dest_addr == 0)
4277 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4278 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4281 return dest_addr;
4283 do_libcall:
4284 fndecl = get_callee_fndecl (orig_exp);
4285 fcode = DECL_FUNCTION_CODE (fndecl);
4286 if (fcode == BUILT_IN_MEMSET)
4287 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4288 dest, val, len);
4289 else if (fcode == BUILT_IN_BZERO)
4290 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4291 dest, len);
4292 else
4293 gcc_unreachable ();
4294 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4295 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4296 return expand_call (fn, target, target == const0_rtx);
4299 /* Expand expression EXP, which is a call to the bzero builtin. Return
4300 NULL_RTX if we failed the caller should emit a normal call. */
4302 static rtx
4303 expand_builtin_bzero (tree exp)
4305 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4306 return NULL_RTX;
4308 tree dest = CALL_EXPR_ARG (exp, 0);
4309 tree size = CALL_EXPR_ARG (exp, 1);
4311 /* New argument list transforming bzero(ptr x, int y) to
4312 memset(ptr x, int 0, size_t y). This is done this way
4313 so that if it isn't expanded inline, we fallback to
4314 calling bzero instead of memset. */
4316 location_t loc = EXPR_LOCATION (exp);
4318 return expand_builtin_memset_args (dest, integer_zero_node,
4319 fold_convert_loc (loc,
4320 size_type_node, size),
4321 const0_rtx, VOIDmode, exp);
4324 /* Try to expand cmpstr operation ICODE with the given operands.
4325 Return the result rtx on success, otherwise return null. */
4327 static rtx
4328 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4329 HOST_WIDE_INT align)
4331 machine_mode insn_mode = insn_data[icode].operand[0].mode;
4333 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4334 target = NULL_RTX;
4336 class expand_operand ops[4];
4337 create_output_operand (&ops[0], target, insn_mode);
4338 create_fixed_operand (&ops[1], arg1_rtx);
4339 create_fixed_operand (&ops[2], arg2_rtx);
4340 create_integer_operand (&ops[3], align);
4341 if (maybe_expand_insn (icode, 4, ops))
4342 return ops[0].value;
4343 return NULL_RTX;
4346 /* Expand expression EXP, which is a call to the memcmp built-in function.
4347 Return NULL_RTX if we failed and the caller should emit a normal call,
4348 otherwise try to get the result in TARGET, if convenient.
4349 RESULT_EQ is true if we can relax the returned value to be either zero
4350 or nonzero, without caring about the sign. */
4352 static rtx
4353 expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4355 if (!validate_arglist (exp,
4356 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4357 return NULL_RTX;
4359 tree arg1 = CALL_EXPR_ARG (exp, 0);
4360 tree arg2 = CALL_EXPR_ARG (exp, 1);
4361 tree len = CALL_EXPR_ARG (exp, 2);
4363 /* Due to the performance benefit, always inline the calls first
4364 when result_eq is false. */
4365 rtx result = NULL_RTX;
4366 enum built_in_function fcode = DECL_FUNCTION_CODE (get_callee_fndecl (exp));
4367 if (!result_eq && fcode != BUILT_IN_BCMP)
4369 result = inline_expand_builtin_bytecmp (exp, target);
4370 if (result)
4371 return result;
4374 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4375 location_t loc = EXPR_LOCATION (exp);
4377 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4378 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4380 /* If we don't have POINTER_TYPE, call the function. */
4381 if (arg1_align == 0 || arg2_align == 0)
4382 return NULL_RTX;
4384 rtx arg1_rtx = get_memory_rtx (arg1, len);
4385 rtx arg2_rtx = get_memory_rtx (arg2, len);
4386 rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4388 /* Set MEM_SIZE as appropriate. */
4389 if (CONST_INT_P (len_rtx))
4391 set_mem_size (arg1_rtx, INTVAL (len_rtx));
4392 set_mem_size (arg2_rtx, INTVAL (len_rtx));
4395 by_pieces_constfn constfn = NULL;
4397 /* Try to get the byte representation of the constant ARG2 (or, only
4398 when the function's result is used for equality to zero, ARG1)
4399 points to, with its byte size in NBYTES. */
4400 unsigned HOST_WIDE_INT nbytes;
4401 const char *rep = getbyterep (arg2, &nbytes);
4402 if (result_eq && rep == NULL)
4404 /* For equality to zero the arguments are interchangeable. */
4405 rep = getbyterep (arg1, &nbytes);
4406 if (rep != NULL)
4407 std::swap (arg1_rtx, arg2_rtx);
4410 /* If the function's constant bound LEN_RTX is less than or equal
4411 to the byte size of the representation of the constant argument,
4412 and if block move would be done by pieces, we can avoid loading
4413 the bytes from memory and only store the computed constant result. */
4414 if (rep
4415 && CONST_INT_P (len_rtx)
4416 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= nbytes)
4417 constfn = builtin_memcpy_read_str;
4419 result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4420 TREE_TYPE (len), target,
4421 result_eq, constfn,
4422 CONST_CAST (char *, rep));
4424 if (result)
4426 /* Return the value in the proper mode for this function. */
4427 if (GET_MODE (result) == mode)
4428 return result;
4430 if (target != 0)
4432 convert_move (target, result, 0);
4433 return target;
4436 return convert_to_mode (mode, result, 0);
4439 return NULL_RTX;
4442 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4443 if we failed the caller should emit a normal call, otherwise try to get
4444 the result in TARGET, if convenient. */
4446 static rtx
4447 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4449 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4450 return NULL_RTX;
4452 tree arg1 = CALL_EXPR_ARG (exp, 0);
4453 tree arg2 = CALL_EXPR_ARG (exp, 1);
4455 /* Due to the performance benefit, always inline the calls first. */
4456 rtx result = NULL_RTX;
4457 result = inline_expand_builtin_bytecmp (exp, target);
4458 if (result)
4459 return result;
4461 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4462 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4463 if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4464 return NULL_RTX;
4466 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4467 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4469 /* If we don't have POINTER_TYPE, call the function. */
4470 if (arg1_align == 0 || arg2_align == 0)
4471 return NULL_RTX;
4473 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4474 arg1 = builtin_save_expr (arg1);
4475 arg2 = builtin_save_expr (arg2);
4477 rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4478 rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4480 /* Try to call cmpstrsi. */
4481 if (cmpstr_icode != CODE_FOR_nothing)
4482 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4483 MIN (arg1_align, arg2_align));
4485 /* Try to determine at least one length and call cmpstrnsi. */
4486 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4488 tree len;
4489 rtx arg3_rtx;
4491 tree len1 = c_strlen (arg1, 1);
4492 tree len2 = c_strlen (arg2, 1);
4494 if (len1)
4495 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4496 if (len2)
4497 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4499 /* If we don't have a constant length for the first, use the length
4500 of the second, if we know it. We don't require a constant for
4501 this case; some cost analysis could be done if both are available
4502 but neither is constant. For now, assume they're equally cheap,
4503 unless one has side effects. If both strings have constant lengths,
4504 use the smaller. */
4506 if (!len1)
4507 len = len2;
4508 else if (!len2)
4509 len = len1;
4510 else if (TREE_SIDE_EFFECTS (len1))
4511 len = len2;
4512 else if (TREE_SIDE_EFFECTS (len2))
4513 len = len1;
4514 else if (TREE_CODE (len1) != INTEGER_CST)
4515 len = len2;
4516 else if (TREE_CODE (len2) != INTEGER_CST)
4517 len = len1;
4518 else if (tree_int_cst_lt (len1, len2))
4519 len = len1;
4520 else
4521 len = len2;
4523 /* If both arguments have side effects, we cannot optimize. */
4524 if (len && !TREE_SIDE_EFFECTS (len))
4526 arg3_rtx = expand_normal (len);
4527 result = expand_cmpstrn_or_cmpmem
4528 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4529 arg3_rtx, MIN (arg1_align, arg2_align));
4533 tree fndecl = get_callee_fndecl (exp);
4534 if (result)
4536 /* Return the value in the proper mode for this function. */
4537 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4538 if (GET_MODE (result) == mode)
4539 return result;
4540 if (target == 0)
4541 return convert_to_mode (mode, result, 0);
4542 convert_move (target, result, 0);
4543 return target;
4546 /* Expand the library call ourselves using a stabilized argument
4547 list to avoid re-evaluating the function's arguments twice. */
4548 tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4549 copy_warning (fn, exp);
4550 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4551 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4552 return expand_call (fn, target, target == const0_rtx);
4555 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4556 NULL_RTX if we failed the caller should emit a normal call, otherwise
4557 try to get the result in TARGET, if convenient. */
4559 static rtx
4560 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4561 ATTRIBUTE_UNUSED machine_mode mode)
4563 if (!validate_arglist (exp,
4564 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4565 return NULL_RTX;
4567 tree arg1 = CALL_EXPR_ARG (exp, 0);
4568 tree arg2 = CALL_EXPR_ARG (exp, 1);
4569 tree arg3 = CALL_EXPR_ARG (exp, 2);
4571 location_t loc = EXPR_LOCATION (exp);
4572 tree len1 = c_strlen (arg1, 1);
4573 tree len2 = c_strlen (arg2, 1);
4575 /* Due to the performance benefit, always inline the calls first. */
4576 rtx result = NULL_RTX;
4577 result = inline_expand_builtin_bytecmp (exp, target);
4578 if (result)
4579 return result;
4581 /* If c_strlen can determine an expression for one of the string
4582 lengths, and it doesn't have side effects, then emit cmpstrnsi
4583 using length MIN(strlen(string)+1, arg3). */
4584 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4585 if (cmpstrn_icode == CODE_FOR_nothing)
4586 return NULL_RTX;
4588 tree len;
4590 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4591 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4593 if (len1)
4594 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4595 if (len2)
4596 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4598 tree len3 = fold_convert_loc (loc, sizetype, arg3);
4600 /* If we don't have a constant length for the first, use the length
4601 of the second, if we know it. If neither string is constant length,
4602 use the given length argument. We don't require a constant for
4603 this case; some cost analysis could be done if both are available
4604 but neither is constant. For now, assume they're equally cheap,
4605 unless one has side effects. If both strings have constant lengths,
4606 use the smaller. */
4608 if (!len1 && !len2)
4609 len = len3;
4610 else if (!len1)
4611 len = len2;
4612 else if (!len2)
4613 len = len1;
4614 else if (TREE_SIDE_EFFECTS (len1))
4615 len = len2;
4616 else if (TREE_SIDE_EFFECTS (len2))
4617 len = len1;
4618 else if (TREE_CODE (len1) != INTEGER_CST)
4619 len = len2;
4620 else if (TREE_CODE (len2) != INTEGER_CST)
4621 len = len1;
4622 else if (tree_int_cst_lt (len1, len2))
4623 len = len1;
4624 else
4625 len = len2;
4627 /* If we are not using the given length, we must incorporate it here.
4628 The actual new length parameter will be MIN(len,arg3) in this case. */
4629 if (len != len3)
4631 len = fold_convert_loc (loc, sizetype, len);
4632 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
4634 rtx arg1_rtx = get_memory_rtx (arg1, len);
4635 rtx arg2_rtx = get_memory_rtx (arg2, len);
4636 rtx arg3_rtx = expand_normal (len);
4637 result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4638 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4639 MIN (arg1_align, arg2_align));
4641 tree fndecl = get_callee_fndecl (exp);
4642 if (result)
4644 /* Return the value in the proper mode for this function. */
4645 mode = TYPE_MODE (TREE_TYPE (exp));
4646 if (GET_MODE (result) == mode)
4647 return result;
4648 if (target == 0)
4649 return convert_to_mode (mode, result, 0);
4650 convert_move (target, result, 0);
4651 return target;
4654 /* Expand the library call ourselves using a stabilized argument
4655 list to avoid re-evaluating the function's arguments twice. */
4656 tree call = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
4657 copy_warning (call, exp);
4658 gcc_assert (TREE_CODE (call) == CALL_EXPR);
4659 CALL_EXPR_TAILCALL (call) = CALL_EXPR_TAILCALL (exp);
4660 return expand_call (call, target, target == const0_rtx);
4663 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4664 if that's convenient. */
4667 expand_builtin_saveregs (void)
4669 rtx val;
4670 rtx_insn *seq;
4672 /* Don't do __builtin_saveregs more than once in a function.
4673 Save the result of the first call and reuse it. */
4674 if (saveregs_value != 0)
4675 return saveregs_value;
4677 /* When this function is called, it means that registers must be
4678 saved on entry to this function. So we migrate the call to the
4679 first insn of this function. */
4681 start_sequence ();
4683 /* Do whatever the machine needs done in this case. */
4684 val = targetm.calls.expand_builtin_saveregs ();
4686 seq = get_insns ();
4687 end_sequence ();
4689 saveregs_value = val;
4691 /* Put the insns after the NOTE that starts the function. If this
4692 is inside a start_sequence, make the outer-level insn chain current, so
4693 the code is placed at the start of the function. */
4694 push_topmost_sequence ();
4695 emit_insn_after (seq, entry_of_function ());
4696 pop_topmost_sequence ();
4698 return val;
4701 /* Expand a call to __builtin_next_arg. */
4703 static rtx
4704 expand_builtin_next_arg (void)
4706 /* Checking arguments is already done in fold_builtin_next_arg
4707 that must be called before this function. */
4708 return expand_binop (ptr_mode, add_optab,
4709 crtl->args.internal_arg_pointer,
4710 crtl->args.arg_offset_rtx,
4711 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4714 /* Make it easier for the backends by protecting the valist argument
4715 from multiple evaluations. */
4717 static tree
4718 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4720 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4722 /* The current way of determining the type of valist is completely
4723 bogus. We should have the information on the va builtin instead. */
4724 if (!vatype)
4725 vatype = targetm.fn_abi_va_list (cfun->decl);
4727 if (TREE_CODE (vatype) == ARRAY_TYPE)
4729 if (TREE_SIDE_EFFECTS (valist))
4730 valist = save_expr (valist);
4732 /* For this case, the backends will be expecting a pointer to
4733 vatype, but it's possible we've actually been given an array
4734 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4735 So fix it. */
4736 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4738 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4739 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4742 else
4744 tree pt = build_pointer_type (vatype);
4746 if (! needs_lvalue)
4748 if (! TREE_SIDE_EFFECTS (valist))
4749 return valist;
4751 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4752 TREE_SIDE_EFFECTS (valist) = 1;
4755 if (TREE_SIDE_EFFECTS (valist))
4756 valist = save_expr (valist);
4757 valist = fold_build2_loc (loc, MEM_REF,
4758 vatype, valist, build_int_cst (pt, 0));
4761 return valist;
4764 /* The "standard" definition of va_list is void*. */
4766 tree
4767 std_build_builtin_va_list (void)
4769 return ptr_type_node;
4772 /* The "standard" abi va_list is va_list_type_node. */
4774 tree
4775 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4777 return va_list_type_node;
4780 /* The "standard" type of va_list is va_list_type_node. */
4782 tree
4783 std_canonical_va_list_type (tree type)
4785 tree wtype, htype;
4787 wtype = va_list_type_node;
4788 htype = type;
4790 if (TREE_CODE (wtype) == ARRAY_TYPE)
4792 /* If va_list is an array type, the argument may have decayed
4793 to a pointer type, e.g. by being passed to another function.
4794 In that case, unwrap both types so that we can compare the
4795 underlying records. */
4796 if (TREE_CODE (htype) == ARRAY_TYPE
4797 || POINTER_TYPE_P (htype))
4799 wtype = TREE_TYPE (wtype);
4800 htype = TREE_TYPE (htype);
4803 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4804 return va_list_type_node;
4806 return NULL_TREE;
4809 /* The "standard" implementation of va_start: just assign `nextarg' to
4810 the variable. */
4812 void
4813 std_expand_builtin_va_start (tree valist, rtx nextarg)
4815 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4816 convert_move (va_r, nextarg, 0);
4819 /* Expand EXP, a call to __builtin_va_start. */
4821 static rtx
4822 expand_builtin_va_start (tree exp)
4824 rtx nextarg;
4825 tree valist;
4826 location_t loc = EXPR_LOCATION (exp);
4828 if (call_expr_nargs (exp) < 2)
4830 error_at (loc, "too few arguments to function %<va_start%>");
4831 return const0_rtx;
4834 if (fold_builtin_next_arg (exp, true))
4835 return const0_rtx;
4837 nextarg = expand_builtin_next_arg ();
4838 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4840 if (targetm.expand_builtin_va_start)
4841 targetm.expand_builtin_va_start (valist, nextarg);
4842 else
4843 std_expand_builtin_va_start (valist, nextarg);
4845 return const0_rtx;
4848 /* Expand EXP, a call to __builtin_va_end. */
4850 static rtx
4851 expand_builtin_va_end (tree exp)
4853 tree valist = CALL_EXPR_ARG (exp, 0);
4855 /* Evaluate for side effects, if needed. I hate macros that don't
4856 do that. */
4857 if (TREE_SIDE_EFFECTS (valist))
4858 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4860 return const0_rtx;
4863 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4864 builtin rather than just as an assignment in stdarg.h because of the
4865 nastiness of array-type va_list types. */
4867 static rtx
4868 expand_builtin_va_copy (tree exp)
4870 tree dst, src, t;
4871 location_t loc = EXPR_LOCATION (exp);
4873 dst = CALL_EXPR_ARG (exp, 0);
4874 src = CALL_EXPR_ARG (exp, 1);
4876 dst = stabilize_va_list_loc (loc, dst, 1);
4877 src = stabilize_va_list_loc (loc, src, 0);
4879 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4881 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4883 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4884 TREE_SIDE_EFFECTS (t) = 1;
4885 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4887 else
4889 rtx dstb, srcb, size;
4891 /* Evaluate to pointers. */
4892 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4893 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4894 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4895 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4897 dstb = convert_memory_address (Pmode, dstb);
4898 srcb = convert_memory_address (Pmode, srcb);
4900 /* "Dereference" to BLKmode memories. */
4901 dstb = gen_rtx_MEM (BLKmode, dstb);
4902 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4903 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4904 srcb = gen_rtx_MEM (BLKmode, srcb);
4905 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4906 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4908 /* Copy. */
4909 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4912 return const0_rtx;
4915 /* Expand a call to one of the builtin functions __builtin_frame_address or
4916 __builtin_return_address. */
4918 static rtx
4919 expand_builtin_frame_address (tree fndecl, tree exp)
4921 /* The argument must be a nonnegative integer constant.
4922 It counts the number of frames to scan up the stack.
4923 The value is either the frame pointer value or the return
4924 address saved in that frame. */
4925 if (call_expr_nargs (exp) == 0)
4926 /* Warning about missing arg was already issued. */
4927 return const0_rtx;
4928 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
4930 error ("invalid argument to %qD", fndecl);
4931 return const0_rtx;
4933 else
4935 /* Number of frames to scan up the stack. */
4936 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
4938 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
4940 /* Some ports cannot access arbitrary stack frames. */
4941 if (tem == NULL)
4943 warning (0, "unsupported argument to %qD", fndecl);
4944 return const0_rtx;
4947 if (count)
4949 /* Warn since no effort is made to ensure that any frame
4950 beyond the current one exists or can be safely reached. */
4951 warning (OPT_Wframe_address, "calling %qD with "
4952 "a nonzero argument is unsafe", fndecl);
4955 /* For __builtin_frame_address, return what we've got. */
4956 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
4957 return tem;
4959 if (!REG_P (tem)
4960 && ! CONSTANT_P (tem))
4961 tem = copy_addr_to_reg (tem);
4962 return tem;
4966 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
4967 failed and the caller should emit a normal call. */
4969 static rtx
4970 expand_builtin_alloca (tree exp)
4972 rtx op0;
4973 rtx result;
4974 unsigned int align;
4975 tree fndecl = get_callee_fndecl (exp);
4976 HOST_WIDE_INT max_size;
4977 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4978 bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
4979 bool valid_arglist
4980 = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
4981 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
4982 VOID_TYPE)
4983 : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
4984 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
4985 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
4987 if (!valid_arglist)
4988 return NULL_RTX;
4990 /* Compute the argument. */
4991 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
4993 /* Compute the alignment. */
4994 align = (fcode == BUILT_IN_ALLOCA
4995 ? BIGGEST_ALIGNMENT
4996 : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
4998 /* Compute the maximum size. */
4999 max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5000 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2))
5001 : -1);
5003 /* Allocate the desired space. If the allocation stems from the declaration
5004 of a variable-sized object, it cannot accumulate. */
5005 result
5006 = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5007 result = convert_memory_address (ptr_mode, result);
5009 /* Dynamic allocations for variables are recorded during gimplification. */
5010 if (!alloca_for_var && (flag_callgraph_info & CALLGRAPH_INFO_DYNAMIC_ALLOC))
5011 record_dynamic_alloc (exp);
5013 return result;
5016 /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
5017 of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
5018 STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
5019 handle_builtin_stack_restore function. */
5021 static rtx
5022 expand_asan_emit_allocas_unpoison (tree exp)
5024 tree arg0 = CALL_EXPR_ARG (exp, 0);
5025 tree arg1 = CALL_EXPR_ARG (exp, 1);
5026 rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5027 rtx bot = expand_expr (arg1, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5028 rtx off = expand_simple_binop (Pmode, MINUS, virtual_stack_dynamic_rtx,
5029 stack_pointer_rtx, NULL_RTX, 0,
5030 OPTAB_LIB_WIDEN);
5031 off = convert_modes (ptr_mode, Pmode, off, 0);
5032 bot = expand_simple_binop (ptr_mode, PLUS, bot, off, NULL_RTX, 0,
5033 OPTAB_LIB_WIDEN);
5034 rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
5035 ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
5036 top, ptr_mode, bot, ptr_mode);
5037 return ret;
5040 /* Expand a call to bswap builtin in EXP.
5041 Return NULL_RTX if a normal call should be emitted rather than expanding the
5042 function in-line. If convenient, the result should be placed in TARGET.
5043 SUBTARGET may be used as the target for computing one of EXP's operands. */
5045 static rtx
5046 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5047 rtx subtarget)
5049 tree arg;
5050 rtx op0;
5052 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5053 return NULL_RTX;
5055 arg = CALL_EXPR_ARG (exp, 0);
5056 op0 = expand_expr (arg,
5057 subtarget && GET_MODE (subtarget) == target_mode
5058 ? subtarget : NULL_RTX,
5059 target_mode, EXPAND_NORMAL);
5060 if (GET_MODE (op0) != target_mode)
5061 op0 = convert_to_mode (target_mode, op0, 1);
5063 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5065 gcc_assert (target);
5067 return convert_to_mode (target_mode, target, 1);
5070 /* Expand a call to a unary builtin in EXP.
5071 Return NULL_RTX if a normal call should be emitted rather than expanding the
5072 function in-line. If convenient, the result should be placed in TARGET.
5073 SUBTARGET may be used as the target for computing one of EXP's operands. */
5075 static rtx
5076 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5077 rtx subtarget, optab op_optab)
5079 rtx op0;
5081 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5082 return NULL_RTX;
5084 /* Compute the argument. */
5085 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5086 (subtarget
5087 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5088 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5089 VOIDmode, EXPAND_NORMAL);
5090 /* Compute op, into TARGET if possible.
5091 Set TARGET to wherever the result comes back. */
5092 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5093 op_optab, op0, target, op_optab != clrsb_optab);
5094 gcc_assert (target);
5096 return convert_to_mode (target_mode, target, 0);
5099 /* Expand a call to __builtin_expect. We just return our argument
5100 as the builtin_expect semantic should've been already executed by
5101 tree branch prediction pass. */
5103 static rtx
5104 expand_builtin_expect (tree exp, rtx target)
5106 tree arg;
5108 if (call_expr_nargs (exp) < 2)
5109 return const0_rtx;
5110 arg = CALL_EXPR_ARG (exp, 0);
5112 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5113 /* When guessing was done, the hints should be already stripped away. */
5114 gcc_assert (!flag_guess_branch_prob
5115 || optimize == 0 || seen_error ());
5116 return target;
5119 /* Expand a call to __builtin_expect_with_probability. We just return our
5120 argument as the builtin_expect semantic should've been already executed by
5121 tree branch prediction pass. */
5123 static rtx
5124 expand_builtin_expect_with_probability (tree exp, rtx target)
5126 tree arg;
5128 if (call_expr_nargs (exp) < 3)
5129 return const0_rtx;
5130 arg = CALL_EXPR_ARG (exp, 0);
5132 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5133 /* When guessing was done, the hints should be already stripped away. */
5134 gcc_assert (!flag_guess_branch_prob
5135 || optimize == 0 || seen_error ());
5136 return target;
5140 /* Expand a call to __builtin_assume_aligned. We just return our first
5141 argument as the builtin_assume_aligned semantic should've been already
5142 executed by CCP. */
5144 static rtx
5145 expand_builtin_assume_aligned (tree exp, rtx target)
5147 if (call_expr_nargs (exp) < 2)
5148 return const0_rtx;
5149 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
5150 EXPAND_NORMAL);
5151 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
5152 && (call_expr_nargs (exp) < 3
5153 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
5154 return target;
5157 void
5158 expand_builtin_trap (void)
5160 if (targetm.have_trap ())
5162 rtx_insn *insn = emit_insn (targetm.gen_trap ());
5163 /* For trap insns when not accumulating outgoing args force
5164 REG_ARGS_SIZE note to prevent crossjumping of calls with
5165 different args sizes. */
5166 if (!ACCUMULATE_OUTGOING_ARGS)
5167 add_args_size_note (insn, stack_pointer_delta);
5169 else
5171 tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
5172 tree call_expr = build_call_expr (fn, 0);
5173 expand_call (call_expr, NULL_RTX, false);
5176 emit_barrier ();
5179 /* Expand a call to __builtin_unreachable. We do nothing except emit
5180 a barrier saying that control flow will not pass here.
5182 It is the responsibility of the program being compiled to ensure
5183 that control flow does never reach __builtin_unreachable. */
5184 static void
5185 expand_builtin_unreachable (void)
5187 emit_barrier ();
5190 /* Expand EXP, a call to fabs, fabsf or fabsl.
5191 Return NULL_RTX if a normal call should be emitted rather than expanding
5192 the function inline. If convenient, the result should be placed
5193 in TARGET. SUBTARGET may be used as the target for computing
5194 the operand. */
5196 static rtx
5197 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5199 machine_mode mode;
5200 tree arg;
5201 rtx op0;
5203 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5204 return NULL_RTX;
5206 arg = CALL_EXPR_ARG (exp, 0);
5207 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5208 mode = TYPE_MODE (TREE_TYPE (arg));
5209 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5210 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5213 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5214 Return NULL is a normal call should be emitted rather than expanding the
5215 function inline. If convenient, the result should be placed in TARGET.
5216 SUBTARGET may be used as the target for computing the operand. */
5218 static rtx
5219 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5221 rtx op0, op1;
5222 tree arg;
5224 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5225 return NULL_RTX;
5227 arg = CALL_EXPR_ARG (exp, 0);
5228 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5230 arg = CALL_EXPR_ARG (exp, 1);
5231 op1 = expand_normal (arg);
5233 return expand_copysign (op0, op1, target);
5236 /* Emit a call to __builtin___clear_cache. */
5238 void
5239 default_emit_call_builtin___clear_cache (rtx begin, rtx end)
5241 rtx callee = gen_rtx_SYMBOL_REF (Pmode,
5242 BUILTIN_ASM_NAME_PTR
5243 (BUILT_IN_CLEAR_CACHE));
5245 emit_library_call (callee,
5246 LCT_NORMAL, VOIDmode,
5247 convert_memory_address (ptr_mode, begin), ptr_mode,
5248 convert_memory_address (ptr_mode, end), ptr_mode);
5251 /* Emit a call to __builtin___clear_cache, unless the target specifies
5252 it as do-nothing. This function can be used by trampoline
5253 finalizers to duplicate the effects of expanding a call to the
5254 clear_cache builtin. */
5256 void
5257 maybe_emit_call_builtin___clear_cache (rtx begin, rtx end)
5259 gcc_assert ((GET_MODE (begin) == ptr_mode || GET_MODE (begin) == Pmode
5260 || CONST_INT_P (begin))
5261 && (GET_MODE (end) == ptr_mode || GET_MODE (end) == Pmode
5262 || CONST_INT_P (end)));
5264 if (targetm.have_clear_cache ())
5266 /* We have a "clear_cache" insn, and it will handle everything. */
5267 class expand_operand ops[2];
5269 create_address_operand (&ops[0], begin);
5270 create_address_operand (&ops[1], end);
5272 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
5273 return;
5275 else
5277 #ifndef CLEAR_INSN_CACHE
5278 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5279 does nothing. There is no need to call it. Do nothing. */
5280 return;
5281 #endif /* CLEAR_INSN_CACHE */
5284 targetm.calls.emit_call_builtin___clear_cache (begin, end);
5287 /* Expand a call to __builtin___clear_cache. */
5289 static void
5290 expand_builtin___clear_cache (tree exp)
5292 tree begin, end;
5293 rtx begin_rtx, end_rtx;
5295 /* We must not expand to a library call. If we did, any
5296 fallback library function in libgcc that might contain a call to
5297 __builtin___clear_cache() would recurse infinitely. */
5298 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5300 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5301 return;
5304 begin = CALL_EXPR_ARG (exp, 0);
5305 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5307 end = CALL_EXPR_ARG (exp, 1);
5308 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5310 maybe_emit_call_builtin___clear_cache (begin_rtx, end_rtx);
5313 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5315 static rtx
5316 round_trampoline_addr (rtx tramp)
5318 rtx temp, addend, mask;
5320 /* If we don't need too much alignment, we'll have been guaranteed
5321 proper alignment by get_trampoline_type. */
5322 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5323 return tramp;
5325 /* Round address up to desired boundary. */
5326 temp = gen_reg_rtx (Pmode);
5327 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
5328 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
5330 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5331 temp, 0, OPTAB_LIB_WIDEN);
5332 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5333 temp, 0, OPTAB_LIB_WIDEN);
5335 return tramp;
5338 static rtx
5339 expand_builtin_init_trampoline (tree exp, bool onstack)
5341 tree t_tramp, t_func, t_chain;
5342 rtx m_tramp, r_tramp, r_chain, tmp;
5344 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5345 POINTER_TYPE, VOID_TYPE))
5346 return NULL_RTX;
5348 t_tramp = CALL_EXPR_ARG (exp, 0);
5349 t_func = CALL_EXPR_ARG (exp, 1);
5350 t_chain = CALL_EXPR_ARG (exp, 2);
5352 r_tramp = expand_normal (t_tramp);
5353 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5354 MEM_NOTRAP_P (m_tramp) = 1;
5356 /* If ONSTACK, the TRAMP argument should be the address of a field
5357 within the local function's FRAME decl. Either way, let's see if
5358 we can fill in the MEM_ATTRs for this memory. */
5359 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5360 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
5362 /* Creator of a heap trampoline is responsible for making sure the
5363 address is aligned to at least STACK_BOUNDARY. Normally malloc
5364 will ensure this anyhow. */
5365 tmp = round_trampoline_addr (r_tramp);
5366 if (tmp != r_tramp)
5368 m_tramp = change_address (m_tramp, BLKmode, tmp);
5369 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5370 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
5373 /* The FUNC argument should be the address of the nested function.
5374 Extract the actual function decl to pass to the hook. */
5375 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5376 t_func = TREE_OPERAND (t_func, 0);
5377 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5379 r_chain = expand_normal (t_chain);
5381 /* Generate insns to initialize the trampoline. */
5382 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5384 if (onstack)
5386 trampolines_created = 1;
5388 if (targetm.calls.custom_function_descriptors != 0)
5389 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5390 "trampoline generated for nested function %qD", t_func);
5393 return const0_rtx;
5396 static rtx
5397 expand_builtin_adjust_trampoline (tree exp)
5399 rtx tramp;
5401 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5402 return NULL_RTX;
5404 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5405 tramp = round_trampoline_addr (tramp);
5406 if (targetm.calls.trampoline_adjust_address)
5407 tramp = targetm.calls.trampoline_adjust_address (tramp);
5409 return tramp;
5412 /* Expand a call to the builtin descriptor initialization routine.
5413 A descriptor is made up of a couple of pointers to the static
5414 chain and the code entry in this order. */
5416 static rtx
5417 expand_builtin_init_descriptor (tree exp)
5419 tree t_descr, t_func, t_chain;
5420 rtx m_descr, r_descr, r_func, r_chain;
5422 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
5423 VOID_TYPE))
5424 return NULL_RTX;
5426 t_descr = CALL_EXPR_ARG (exp, 0);
5427 t_func = CALL_EXPR_ARG (exp, 1);
5428 t_chain = CALL_EXPR_ARG (exp, 2);
5430 r_descr = expand_normal (t_descr);
5431 m_descr = gen_rtx_MEM (BLKmode, r_descr);
5432 MEM_NOTRAP_P (m_descr) = 1;
5433 set_mem_align (m_descr, GET_MODE_ALIGNMENT (ptr_mode));
5435 r_func = expand_normal (t_func);
5436 r_chain = expand_normal (t_chain);
5438 /* Generate insns to initialize the descriptor. */
5439 emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
5440 emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
5441 POINTER_SIZE / BITS_PER_UNIT), r_func);
5443 return const0_rtx;
5446 /* Expand a call to the builtin descriptor adjustment routine. */
5448 static rtx
5449 expand_builtin_adjust_descriptor (tree exp)
5451 rtx tramp;
5453 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5454 return NULL_RTX;
5456 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5458 /* Unalign the descriptor to allow runtime identification. */
5459 tramp = plus_constant (ptr_mode, tramp,
5460 targetm.calls.custom_function_descriptors);
5462 return force_operand (tramp, NULL_RTX);
5465 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5466 function. The function first checks whether the back end provides
5467 an insn to implement signbit for the respective mode. If not, it
5468 checks whether the floating point format of the value is such that
5469 the sign bit can be extracted. If that is not the case, error out.
5470 EXP is the expression that is a call to the builtin function; if
5471 convenient, the result should be placed in TARGET. */
5472 static rtx
5473 expand_builtin_signbit (tree exp, rtx target)
5475 const struct real_format *fmt;
5476 scalar_float_mode fmode;
5477 scalar_int_mode rmode, imode;
5478 tree arg;
5479 int word, bitpos;
5480 enum insn_code icode;
5481 rtx temp;
5482 location_t loc = EXPR_LOCATION (exp);
5484 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5485 return NULL_RTX;
5487 arg = CALL_EXPR_ARG (exp, 0);
5488 fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
5489 rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5490 fmt = REAL_MODE_FORMAT (fmode);
5492 arg = builtin_save_expr (arg);
5494 /* Expand the argument yielding a RTX expression. */
5495 temp = expand_normal (arg);
5497 /* Check if the back end provides an insn that handles signbit for the
5498 argument's mode. */
5499 icode = optab_handler (signbit_optab, fmode);
5500 if (icode != CODE_FOR_nothing)
5502 rtx_insn *last = get_last_insn ();
5503 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5504 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5505 return target;
5506 delete_insns_since (last);
5509 /* For floating point formats without a sign bit, implement signbit
5510 as "ARG < 0.0". */
5511 bitpos = fmt->signbit_ro;
5512 if (bitpos < 0)
5514 /* But we can't do this if the format supports signed zero. */
5515 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
5517 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5518 build_real (TREE_TYPE (arg), dconst0));
5519 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5522 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5524 imode = int_mode_for_mode (fmode).require ();
5525 temp = gen_lowpart (imode, temp);
5527 else
5529 imode = word_mode;
5530 /* Handle targets with different FP word orders. */
5531 if (FLOAT_WORDS_BIG_ENDIAN)
5532 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5533 else
5534 word = bitpos / BITS_PER_WORD;
5535 temp = operand_subword_force (temp, word, fmode);
5536 bitpos = bitpos % BITS_PER_WORD;
5539 /* Force the intermediate word_mode (or narrower) result into a
5540 register. This avoids attempting to create paradoxical SUBREGs
5541 of floating point modes below. */
5542 temp = force_reg (imode, temp);
5544 /* If the bitpos is within the "result mode" lowpart, the operation
5545 can be implement with a single bitwise AND. Otherwise, we need
5546 a right shift and an AND. */
5548 if (bitpos < GET_MODE_BITSIZE (rmode))
5550 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5552 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5553 temp = gen_lowpart (rmode, temp);
5554 temp = expand_binop (rmode, and_optab, temp,
5555 immed_wide_int_const (mask, rmode),
5556 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5558 else
5560 /* Perform a logical right shift to place the signbit in the least
5561 significant bit, then truncate the result to the desired mode
5562 and mask just this bit. */
5563 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5564 temp = gen_lowpart (rmode, temp);
5565 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5566 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5569 return temp;
5572 /* Expand fork or exec calls. TARGET is the desired target of the
5573 call. EXP is the call. FN is the
5574 identificator of the actual function. IGNORE is nonzero if the
5575 value is to be ignored. */
5577 static rtx
5578 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5580 tree id, decl;
5581 tree call;
5583 /* If we are not profiling, just call the function. */
5584 if (!profile_arc_flag)
5585 return NULL_RTX;
5587 /* Otherwise call the wrapper. This should be equivalent for the rest of
5588 compiler, so the code does not diverge, and the wrapper may run the
5589 code necessary for keeping the profiling sane. */
5591 switch (DECL_FUNCTION_CODE (fn))
5593 case BUILT_IN_FORK:
5594 id = get_identifier ("__gcov_fork");
5595 break;
5597 case BUILT_IN_EXECL:
5598 id = get_identifier ("__gcov_execl");
5599 break;
5601 case BUILT_IN_EXECV:
5602 id = get_identifier ("__gcov_execv");
5603 break;
5605 case BUILT_IN_EXECLP:
5606 id = get_identifier ("__gcov_execlp");
5607 break;
5609 case BUILT_IN_EXECLE:
5610 id = get_identifier ("__gcov_execle");
5611 break;
5613 case BUILT_IN_EXECVP:
5614 id = get_identifier ("__gcov_execvp");
5615 break;
5617 case BUILT_IN_EXECVE:
5618 id = get_identifier ("__gcov_execve");
5619 break;
5621 default:
5622 gcc_unreachable ();
5625 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5626 FUNCTION_DECL, id, TREE_TYPE (fn));
5627 DECL_EXTERNAL (decl) = 1;
5628 TREE_PUBLIC (decl) = 1;
5629 DECL_ARTIFICIAL (decl) = 1;
5630 TREE_NOTHROW (decl) = 1;
5631 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5632 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5633 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5634 return expand_call (call, target, ignore);
5639 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5640 the pointer in these functions is void*, the tree optimizers may remove
5641 casts. The mode computed in expand_builtin isn't reliable either, due
5642 to __sync_bool_compare_and_swap.
5644 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5645 group of builtins. This gives us log2 of the mode size. */
5647 static inline machine_mode
5648 get_builtin_sync_mode (int fcode_diff)
5650 /* The size is not negotiable, so ask not to get BLKmode in return
5651 if the target indicates that a smaller size would be better. */
5652 return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require ();
5655 /* Expand the memory expression LOC and return the appropriate memory operand
5656 for the builtin_sync operations. */
5658 static rtx
5659 get_builtin_sync_mem (tree loc, machine_mode mode)
5661 rtx addr, mem;
5662 int addr_space = TYPE_ADDR_SPACE (POINTER_TYPE_P (TREE_TYPE (loc))
5663 ? TREE_TYPE (TREE_TYPE (loc))
5664 : TREE_TYPE (loc));
5665 scalar_int_mode addr_mode = targetm.addr_space.address_mode (addr_space);
5667 addr = expand_expr (loc, NULL_RTX, addr_mode, EXPAND_SUM);
5668 addr = convert_memory_address (addr_mode, addr);
5670 /* Note that we explicitly do not want any alias information for this
5671 memory, so that we kill all other live memories. Otherwise we don't
5672 satisfy the full barrier semantics of the intrinsic. */
5673 mem = gen_rtx_MEM (mode, addr);
5675 set_mem_addr_space (mem, addr_space);
5677 mem = validize_mem (mem);
5679 /* The alignment needs to be at least according to that of the mode. */
5680 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5681 get_pointer_alignment (loc)));
5682 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5683 MEM_VOLATILE_P (mem) = 1;
5685 return mem;
5688 /* Make sure an argument is in the right mode.
5689 EXP is the tree argument.
5690 MODE is the mode it should be in. */
5692 static rtx
5693 expand_expr_force_mode (tree exp, machine_mode mode)
5695 rtx val;
5696 machine_mode old_mode;
5698 if (TREE_CODE (exp) == SSA_NAME
5699 && TYPE_MODE (TREE_TYPE (exp)) != mode)
5701 /* Undo argument promotion if possible, as combine might not
5702 be able to do it later due to MEM_VOLATILE_P uses in the
5703 patterns. */
5704 gimple *g = get_gimple_for_ssa_name (exp);
5705 if (g && gimple_assign_cast_p (g))
5707 tree rhs = gimple_assign_rhs1 (g);
5708 tree_code code = gimple_assign_rhs_code (g);
5709 if (CONVERT_EXPR_CODE_P (code)
5710 && TYPE_MODE (TREE_TYPE (rhs)) == mode
5711 && INTEGRAL_TYPE_P (TREE_TYPE (exp))
5712 && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5713 && (TYPE_PRECISION (TREE_TYPE (exp))
5714 > TYPE_PRECISION (TREE_TYPE (rhs))))
5715 exp = rhs;
5719 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5720 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5721 of CONST_INTs, where we know the old_mode only from the call argument. */
5723 old_mode = GET_MODE (val);
5724 if (old_mode == VOIDmode)
5725 old_mode = TYPE_MODE (TREE_TYPE (exp));
5726 val = convert_modes (mode, old_mode, val, 1);
5727 return val;
5731 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5732 EXP is the CALL_EXPR. CODE is the rtx code
5733 that corresponds to the arithmetic or logical operation from the name;
5734 an exception here is that NOT actually means NAND. TARGET is an optional
5735 place for us to store the results; AFTER is true if this is the
5736 fetch_and_xxx form. */
5738 static rtx
5739 expand_builtin_sync_operation (machine_mode mode, tree exp,
5740 enum rtx_code code, bool after,
5741 rtx target)
5743 rtx val, mem;
5744 location_t loc = EXPR_LOCATION (exp);
5746 if (code == NOT && warn_sync_nand)
5748 tree fndecl = get_callee_fndecl (exp);
5749 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5751 static bool warned_f_a_n, warned_n_a_f;
5753 switch (fcode)
5755 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5756 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5757 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5758 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5759 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5760 if (warned_f_a_n)
5761 break;
5763 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5764 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5765 warned_f_a_n = true;
5766 break;
5768 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5769 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5770 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5771 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5772 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5773 if (warned_n_a_f)
5774 break;
5776 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5777 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5778 warned_n_a_f = true;
5779 break;
5781 default:
5782 gcc_unreachable ();
5786 /* Expand the operands. */
5787 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5788 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5790 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
5791 after);
5794 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5795 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5796 true if this is the boolean form. TARGET is a place for us to store the
5797 results; this is NOT optional if IS_BOOL is true. */
5799 static rtx
5800 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5801 bool is_bool, rtx target)
5803 rtx old_val, new_val, mem;
5804 rtx *pbool, *poval;
5806 /* Expand the operands. */
5807 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5808 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5809 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5811 pbool = poval = NULL;
5812 if (target != const0_rtx)
5814 if (is_bool)
5815 pbool = &target;
5816 else
5817 poval = &target;
5819 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5820 false, MEMMODEL_SYNC_SEQ_CST,
5821 MEMMODEL_SYNC_SEQ_CST))
5822 return NULL_RTX;
5824 return target;
5827 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5828 general form is actually an atomic exchange, and some targets only
5829 support a reduced form with the second argument being a constant 1.
5830 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5831 the results. */
5833 static rtx
5834 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5835 rtx target)
5837 rtx val, mem;
5839 /* Expand the operands. */
5840 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5841 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5843 return expand_sync_lock_test_and_set (target, mem, val);
5846 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5848 static void
5849 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5851 rtx mem;
5853 /* Expand the operands. */
5854 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5856 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
5859 /* Given an integer representing an ``enum memmodel'', verify its
5860 correctness and return the memory model enum. */
5862 static enum memmodel
5863 get_memmodel (tree exp)
5865 /* If the parameter is not a constant, it's a run time value so we'll just
5866 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5867 if (TREE_CODE (exp) != INTEGER_CST)
5868 return MEMMODEL_SEQ_CST;
5870 rtx op = expand_normal (exp);
5872 unsigned HOST_WIDE_INT val = INTVAL (op);
5873 if (targetm.memmodel_check)
5874 val = targetm.memmodel_check (val);
5875 else if (val & ~MEMMODEL_MASK)
5876 return MEMMODEL_SEQ_CST;
5878 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5879 if (memmodel_base (val) >= MEMMODEL_LAST)
5880 return MEMMODEL_SEQ_CST;
5882 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5883 be conservative and promote consume to acquire. */
5884 if (val == MEMMODEL_CONSUME)
5885 val = MEMMODEL_ACQUIRE;
5887 return (enum memmodel) val;
5890 /* Expand the __atomic_exchange intrinsic:
5891 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5892 EXP is the CALL_EXPR.
5893 TARGET is an optional place for us to store the results. */
5895 static rtx
5896 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5898 rtx val, mem;
5899 enum memmodel model;
5901 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5903 if (!flag_inline_atomics)
5904 return NULL_RTX;
5906 /* Expand the operands. */
5907 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5908 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5910 return expand_atomic_exchange (target, mem, val, model);
5913 /* Expand the __atomic_compare_exchange intrinsic:
5914 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5915 TYPE desired, BOOL weak,
5916 enum memmodel success,
5917 enum memmodel failure)
5918 EXP is the CALL_EXPR.
5919 TARGET is an optional place for us to store the results. */
5921 static rtx
5922 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5923 rtx target)
5925 rtx expect, desired, mem, oldval;
5926 rtx_code_label *label;
5927 tree weak;
5928 bool is_weak;
5930 memmodel success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5931 memmodel failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5933 if (failure > success)
5934 success = MEMMODEL_SEQ_CST;
5936 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5938 failure = MEMMODEL_SEQ_CST;
5939 success = MEMMODEL_SEQ_CST;
5943 if (!flag_inline_atomics)
5944 return NULL_RTX;
5946 /* Expand the operands. */
5947 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5949 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5950 expect = convert_memory_address (Pmode, expect);
5951 expect = gen_rtx_MEM (mode, expect);
5952 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5954 weak = CALL_EXPR_ARG (exp, 3);
5955 is_weak = false;
5956 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5957 is_weak = true;
5959 if (target == const0_rtx)
5960 target = NULL;
5962 /* Lest the rtl backend create a race condition with an imporoper store
5963 to memory, always create a new pseudo for OLDVAL. */
5964 oldval = NULL;
5966 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5967 is_weak, success, failure))
5968 return NULL_RTX;
5970 /* Conditionally store back to EXPECT, lest we create a race condition
5971 with an improper store to memory. */
5972 /* ??? With a rearrangement of atomics at the gimple level, we can handle
5973 the normal case where EXPECT is totally private, i.e. a register. At
5974 which point the store can be unconditional. */
5975 label = gen_label_rtx ();
5976 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
5977 GET_MODE (target), 1, label);
5978 emit_move_insn (expect, oldval);
5979 emit_label (label);
5981 return target;
5984 /* Helper function for expand_ifn_atomic_compare_exchange - expand
5985 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
5986 call. The weak parameter must be dropped to match the expected parameter
5987 list and the expected argument changed from value to pointer to memory
5988 slot. */
5990 static void
5991 expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
5993 unsigned int z;
5994 vec<tree, va_gc> *vec;
5996 vec_alloc (vec, 5);
5997 vec->quick_push (gimple_call_arg (call, 0));
5998 tree expected = gimple_call_arg (call, 1);
5999 rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
6000 TREE_TYPE (expected));
6001 rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
6002 if (expd != x)
6003 emit_move_insn (x, expd);
6004 tree v = make_tree (TREE_TYPE (expected), x);
6005 vec->quick_push (build1 (ADDR_EXPR,
6006 build_pointer_type (TREE_TYPE (expected)), v));
6007 vec->quick_push (gimple_call_arg (call, 2));
6008 /* Skip the boolean weak parameter. */
6009 for (z = 4; z < 6; z++)
6010 vec->quick_push (gimple_call_arg (call, z));
6011 /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6012 unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
6013 gcc_assert (bytes_log2 < 5);
6014 built_in_function fncode
6015 = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6016 + bytes_log2);
6017 tree fndecl = builtin_decl_explicit (fncode);
6018 tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
6019 fndecl);
6020 tree exp = build_call_vec (boolean_type_node, fn, vec);
6021 tree lhs = gimple_call_lhs (call);
6022 rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
6023 if (lhs)
6025 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6026 if (GET_MODE (boolret) != mode)
6027 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6028 x = force_reg (mode, x);
6029 write_complex_part (target, boolret, true);
6030 write_complex_part (target, x, false);
6034 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6036 void
6037 expand_ifn_atomic_compare_exchange (gcall *call)
6039 int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6040 gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6041 machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6043 memmodel success = get_memmodel (gimple_call_arg (call, 4));
6044 memmodel failure = get_memmodel (gimple_call_arg (call, 5));
6046 if (failure > success)
6047 success = MEMMODEL_SEQ_CST;
6049 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6051 failure = MEMMODEL_SEQ_CST;
6052 success = MEMMODEL_SEQ_CST;
6055 if (!flag_inline_atomics)
6057 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6058 return;
6061 /* Expand the operands. */
6062 rtx mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6064 rtx expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6065 rtx desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6067 bool is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6069 rtx boolret = NULL;
6070 rtx oldval = NULL;
6072 if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
6073 is_weak, success, failure))
6075 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6076 return;
6079 tree lhs = gimple_call_lhs (call);
6080 if (lhs)
6082 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6083 if (GET_MODE (boolret) != mode)
6084 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6085 write_complex_part (target, boolret, true);
6086 write_complex_part (target, oldval, false);
6090 /* Expand the __atomic_load intrinsic:
6091 TYPE __atomic_load (TYPE *object, enum memmodel)
6092 EXP is the CALL_EXPR.
6093 TARGET is an optional place for us to store the results. */
6095 static rtx
6096 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6098 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6099 if (is_mm_release (model) || is_mm_acq_rel (model))
6100 model = MEMMODEL_SEQ_CST;
6102 if (!flag_inline_atomics)
6103 return NULL_RTX;
6105 /* Expand the operand. */
6106 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6108 return expand_atomic_load (target, mem, model);
6112 /* Expand the __atomic_store intrinsic:
6113 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6114 EXP is the CALL_EXPR.
6115 TARGET is an optional place for us to store the results. */
6117 static rtx
6118 expand_builtin_atomic_store (machine_mode mode, tree exp)
6120 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6121 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6122 || is_mm_release (model)))
6123 model = MEMMODEL_SEQ_CST;
6125 if (!flag_inline_atomics)
6126 return NULL_RTX;
6128 /* Expand the operands. */
6129 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6130 rtx val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6132 return expand_atomic_store (mem, val, model, false);
6135 /* Expand the __atomic_fetch_XXX intrinsic:
6136 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6137 EXP is the CALL_EXPR.
6138 TARGET is an optional place for us to store the results.
6139 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6140 FETCH_AFTER is true if returning the result of the operation.
6141 FETCH_AFTER is false if returning the value before the operation.
6142 IGNORE is true if the result is not used.
6143 EXT_CALL is the correct builtin for an external call if this cannot be
6144 resolved to an instruction sequence. */
6146 static rtx
6147 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
6148 enum rtx_code code, bool fetch_after,
6149 bool ignore, enum built_in_function ext_call)
6151 rtx val, mem, ret;
6152 enum memmodel model;
6153 tree fndecl;
6154 tree addr;
6156 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6158 /* Expand the operands. */
6159 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6160 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6162 /* Only try generating instructions if inlining is turned on. */
6163 if (flag_inline_atomics)
6165 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6166 if (ret)
6167 return ret;
6170 /* Return if a different routine isn't needed for the library call. */
6171 if (ext_call == BUILT_IN_NONE)
6172 return NULL_RTX;
6174 /* Change the call to the specified function. */
6175 fndecl = get_callee_fndecl (exp);
6176 addr = CALL_EXPR_FN (exp);
6177 STRIP_NOPS (addr);
6179 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
6180 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
6182 /* If we will emit code after the call, the call cannot be a tail call.
6183 If it is emitted as a tail call, a barrier is emitted after it, and
6184 then all trailing code is removed. */
6185 if (!ignore)
6186 CALL_EXPR_TAILCALL (exp) = 0;
6188 /* Expand the call here so we can emit trailing code. */
6189 ret = expand_call (exp, target, ignore);
6191 /* Replace the original function just in case it matters. */
6192 TREE_OPERAND (addr, 0) = fndecl;
6194 /* Then issue the arithmetic correction to return the right result. */
6195 if (!ignore)
6197 if (code == NOT)
6199 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
6200 OPTAB_LIB_WIDEN);
6201 ret = expand_simple_unop (mode, NOT, ret, target, true);
6203 else
6204 ret = expand_simple_binop (mode, code, ret, val, target, true,
6205 OPTAB_LIB_WIDEN);
6207 return ret;
6210 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6212 void
6213 expand_ifn_atomic_bit_test_and (gcall *call)
6215 tree ptr = gimple_call_arg (call, 0);
6216 tree bit = gimple_call_arg (call, 1);
6217 tree flag = gimple_call_arg (call, 2);
6218 tree lhs = gimple_call_lhs (call);
6219 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6220 machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
6221 enum rtx_code code;
6222 optab optab;
6223 class expand_operand ops[5];
6225 gcc_assert (flag_inline_atomics);
6227 if (gimple_call_num_args (call) == 4)
6228 model = get_memmodel (gimple_call_arg (call, 3));
6230 rtx mem = get_builtin_sync_mem (ptr, mode);
6231 rtx val = expand_expr_force_mode (bit, mode);
6233 switch (gimple_call_internal_fn (call))
6235 case IFN_ATOMIC_BIT_TEST_AND_SET:
6236 code = IOR;
6237 optab = atomic_bit_test_and_set_optab;
6238 break;
6239 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
6240 code = XOR;
6241 optab = atomic_bit_test_and_complement_optab;
6242 break;
6243 case IFN_ATOMIC_BIT_TEST_AND_RESET:
6244 code = AND;
6245 optab = atomic_bit_test_and_reset_optab;
6246 break;
6247 default:
6248 gcc_unreachable ();
6251 if (lhs == NULL_TREE)
6253 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6254 val, NULL_RTX, true, OPTAB_DIRECT);
6255 if (code == AND)
6256 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6257 expand_atomic_fetch_op (const0_rtx, mem, val, code, model, false);
6258 return;
6261 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6262 enum insn_code icode = direct_optab_handler (optab, mode);
6263 gcc_assert (icode != CODE_FOR_nothing);
6264 create_output_operand (&ops[0], target, mode);
6265 create_fixed_operand (&ops[1], mem);
6266 create_convert_operand_to (&ops[2], val, mode, true);
6267 create_integer_operand (&ops[3], model);
6268 create_integer_operand (&ops[4], integer_onep (flag));
6269 if (maybe_expand_insn (icode, 5, ops))
6270 return;
6272 rtx bitval = val;
6273 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6274 val, NULL_RTX, true, OPTAB_DIRECT);
6275 rtx maskval = val;
6276 if (code == AND)
6277 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6278 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
6279 code, model, false);
6280 if (integer_onep (flag))
6282 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
6283 NULL_RTX, true, OPTAB_DIRECT);
6284 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
6285 true, OPTAB_DIRECT);
6287 else
6288 result = expand_simple_binop (mode, AND, result, maskval, target, true,
6289 OPTAB_DIRECT);
6290 if (result != target)
6291 emit_move_insn (target, result);
6294 /* Expand IFN_ATOMIC_*_FETCH_CMP_0 internal function. */
6296 void
6297 expand_ifn_atomic_op_fetch_cmp_0 (gcall *call)
6299 tree cmp = gimple_call_arg (call, 0);
6300 tree ptr = gimple_call_arg (call, 1);
6301 tree arg = gimple_call_arg (call, 2);
6302 tree lhs = gimple_call_lhs (call);
6303 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6304 machine_mode mode = TYPE_MODE (TREE_TYPE (cmp));
6305 optab optab;
6306 rtx_code code;
6307 class expand_operand ops[5];
6309 gcc_assert (flag_inline_atomics);
6311 if (gimple_call_num_args (call) == 4)
6312 model = get_memmodel (gimple_call_arg (call, 3));
6314 rtx mem = get_builtin_sync_mem (ptr, mode);
6315 rtx op = expand_expr_force_mode (arg, mode);
6317 switch (gimple_call_internal_fn (call))
6319 case IFN_ATOMIC_ADD_FETCH_CMP_0:
6320 code = PLUS;
6321 optab = atomic_add_fetch_cmp_0_optab;
6322 break;
6323 case IFN_ATOMIC_SUB_FETCH_CMP_0:
6324 code = MINUS;
6325 optab = atomic_sub_fetch_cmp_0_optab;
6326 break;
6327 case IFN_ATOMIC_AND_FETCH_CMP_0:
6328 code = AND;
6329 optab = atomic_and_fetch_cmp_0_optab;
6330 break;
6331 case IFN_ATOMIC_OR_FETCH_CMP_0:
6332 code = IOR;
6333 optab = atomic_or_fetch_cmp_0_optab;
6334 break;
6335 case IFN_ATOMIC_XOR_FETCH_CMP_0:
6336 code = XOR;
6337 optab = atomic_xor_fetch_cmp_0_optab;
6338 break;
6339 default:
6340 gcc_unreachable ();
6343 enum rtx_code comp = UNKNOWN;
6344 switch (tree_to_uhwi (cmp))
6346 case ATOMIC_OP_FETCH_CMP_0_EQ: comp = EQ; break;
6347 case ATOMIC_OP_FETCH_CMP_0_NE: comp = NE; break;
6348 case ATOMIC_OP_FETCH_CMP_0_GT: comp = GT; break;
6349 case ATOMIC_OP_FETCH_CMP_0_GE: comp = GE; break;
6350 case ATOMIC_OP_FETCH_CMP_0_LT: comp = LT; break;
6351 case ATOMIC_OP_FETCH_CMP_0_LE: comp = LE; break;
6352 default: gcc_unreachable ();
6355 rtx target;
6356 if (lhs == NULL_TREE)
6357 target = gen_reg_rtx (TYPE_MODE (boolean_type_node));
6358 else
6359 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6360 enum insn_code icode = direct_optab_handler (optab, mode);
6361 gcc_assert (icode != CODE_FOR_nothing);
6362 create_output_operand (&ops[0], target, TYPE_MODE (boolean_type_node));
6363 create_fixed_operand (&ops[1], mem);
6364 create_convert_operand_to (&ops[2], op, mode, true);
6365 create_integer_operand (&ops[3], model);
6366 create_integer_operand (&ops[4], comp);
6367 if (maybe_expand_insn (icode, 5, ops))
6368 return;
6370 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, op,
6371 code, model, true);
6372 if (lhs)
6374 result = emit_store_flag_force (target, comp, result, const0_rtx, mode,
6375 0, 1);
6376 if (result != target)
6377 emit_move_insn (target, result);
6381 /* Expand an atomic clear operation.
6382 void _atomic_clear (BOOL *obj, enum memmodel)
6383 EXP is the call expression. */
6385 static rtx
6386 expand_builtin_atomic_clear (tree exp)
6388 machine_mode mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6389 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6390 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6392 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
6393 model = MEMMODEL_SEQ_CST;
6395 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6396 Failing that, a store is issued by __atomic_store. The only way this can
6397 fail is if the bool type is larger than a word size. Unlikely, but
6398 handle it anyway for completeness. Assume a single threaded model since
6399 there is no atomic support in this case, and no barriers are required. */
6400 rtx ret = expand_atomic_store (mem, const0_rtx, model, true);
6401 if (!ret)
6402 emit_move_insn (mem, const0_rtx);
6403 return const0_rtx;
6406 /* Expand an atomic test_and_set operation.
6407 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6408 EXP is the call expression. */
6410 static rtx
6411 expand_builtin_atomic_test_and_set (tree exp, rtx target)
6413 rtx mem;
6414 enum memmodel model;
6415 machine_mode mode;
6417 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6418 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6419 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6421 return expand_atomic_test_and_set (target, mem, model);
6425 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6426 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6428 static tree
6429 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
6431 int size;
6432 machine_mode mode;
6433 unsigned int mode_align, type_align;
6435 if (TREE_CODE (arg0) != INTEGER_CST)
6436 return NULL_TREE;
6438 /* We need a corresponding integer mode for the access to be lock-free. */
6439 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
6440 if (!int_mode_for_size (size, 0).exists (&mode))
6441 return boolean_false_node;
6443 mode_align = GET_MODE_ALIGNMENT (mode);
6445 if (TREE_CODE (arg1) == INTEGER_CST)
6447 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
6449 /* Either this argument is null, or it's a fake pointer encoding
6450 the alignment of the object. */
6451 val = least_bit_hwi (val);
6452 val *= BITS_PER_UNIT;
6454 if (val == 0 || mode_align < val)
6455 type_align = mode_align;
6456 else
6457 type_align = val;
6459 else
6461 tree ttype = TREE_TYPE (arg1);
6463 /* This function is usually invoked and folded immediately by the front
6464 end before anything else has a chance to look at it. The pointer
6465 parameter at this point is usually cast to a void *, so check for that
6466 and look past the cast. */
6467 if (CONVERT_EXPR_P (arg1)
6468 && POINTER_TYPE_P (ttype)
6469 && VOID_TYPE_P (TREE_TYPE (ttype))
6470 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
6471 arg1 = TREE_OPERAND (arg1, 0);
6473 ttype = TREE_TYPE (arg1);
6474 gcc_assert (POINTER_TYPE_P (ttype));
6476 /* Get the underlying type of the object. */
6477 ttype = TREE_TYPE (ttype);
6478 type_align = TYPE_ALIGN (ttype);
6481 /* If the object has smaller alignment, the lock free routines cannot
6482 be used. */
6483 if (type_align < mode_align)
6484 return boolean_false_node;
6486 /* Check if a compare_and_swap pattern exists for the mode which represents
6487 the required size. The pattern is not allowed to fail, so the existence
6488 of the pattern indicates support is present. Also require that an
6489 atomic load exists for the required size. */
6490 if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
6491 return boolean_true_node;
6492 else
6493 return boolean_false_node;
6496 /* Return true if the parameters to call EXP represent an object which will
6497 always generate lock free instructions. The first argument represents the
6498 size of the object, and the second parameter is a pointer to the object
6499 itself. If NULL is passed for the object, then the result is based on
6500 typical alignment for an object of the specified size. Otherwise return
6501 false. */
6503 static rtx
6504 expand_builtin_atomic_always_lock_free (tree exp)
6506 tree size;
6507 tree arg0 = CALL_EXPR_ARG (exp, 0);
6508 tree arg1 = CALL_EXPR_ARG (exp, 1);
6510 if (TREE_CODE (arg0) != INTEGER_CST)
6512 error ("non-constant argument 1 to %qs", "__atomic_always_lock_free");
6513 return const0_rtx;
6516 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
6517 if (size == boolean_true_node)
6518 return const1_rtx;
6519 return const0_rtx;
6522 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6523 is lock free on this architecture. */
6525 static tree
6526 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
6528 if (!flag_inline_atomics)
6529 return NULL_TREE;
6531 /* If it isn't always lock free, don't generate a result. */
6532 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
6533 return boolean_true_node;
6535 return NULL_TREE;
6538 /* Return true if the parameters to call EXP represent an object which will
6539 always generate lock free instructions. The first argument represents the
6540 size of the object, and the second parameter is a pointer to the object
6541 itself. If NULL is passed for the object, then the result is based on
6542 typical alignment for an object of the specified size. Otherwise return
6543 NULL*/
6545 static rtx
6546 expand_builtin_atomic_is_lock_free (tree exp)
6548 tree size;
6549 tree arg0 = CALL_EXPR_ARG (exp, 0);
6550 tree arg1 = CALL_EXPR_ARG (exp, 1);
6552 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6554 error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
6555 return NULL_RTX;
6558 if (!flag_inline_atomics)
6559 return NULL_RTX;
6561 /* If the value is known at compile time, return the RTX for it. */
6562 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
6563 if (size == boolean_true_node)
6564 return const1_rtx;
6566 return NULL_RTX;
6569 /* Expand the __atomic_thread_fence intrinsic:
6570 void __atomic_thread_fence (enum memmodel)
6571 EXP is the CALL_EXPR. */
6573 static void
6574 expand_builtin_atomic_thread_fence (tree exp)
6576 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6577 expand_mem_thread_fence (model);
6580 /* Expand the __atomic_signal_fence intrinsic:
6581 void __atomic_signal_fence (enum memmodel)
6582 EXP is the CALL_EXPR. */
6584 static void
6585 expand_builtin_atomic_signal_fence (tree exp)
6587 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6588 expand_mem_signal_fence (model);
6591 /* Expand the __sync_synchronize intrinsic. */
6593 static void
6594 expand_builtin_sync_synchronize (void)
6596 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
6599 static rtx
6600 expand_builtin_thread_pointer (tree exp, rtx target)
6602 enum insn_code icode;
6603 if (!validate_arglist (exp, VOID_TYPE))
6604 return const0_rtx;
6605 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
6606 if (icode != CODE_FOR_nothing)
6608 class expand_operand op;
6609 /* If the target is not sutitable then create a new target. */
6610 if (target == NULL_RTX
6611 || !REG_P (target)
6612 || GET_MODE (target) != Pmode)
6613 target = gen_reg_rtx (Pmode);
6614 create_output_operand (&op, target, Pmode);
6615 expand_insn (icode, 1, &op);
6616 return target;
6618 error ("%<__builtin_thread_pointer%> is not supported on this target");
6619 return const0_rtx;
6622 static void
6623 expand_builtin_set_thread_pointer (tree exp)
6625 enum insn_code icode;
6626 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6627 return;
6628 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
6629 if (icode != CODE_FOR_nothing)
6631 class expand_operand op;
6632 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
6633 Pmode, EXPAND_NORMAL);
6634 create_input_operand (&op, val, Pmode);
6635 expand_insn (icode, 1, &op);
6636 return;
6638 error ("%<__builtin_set_thread_pointer%> is not supported on this target");
6642 /* Emit code to restore the current value of stack. */
6644 static void
6645 expand_stack_restore (tree var)
6647 rtx_insn *prev;
6648 rtx sa = expand_normal (var);
6650 sa = convert_memory_address (Pmode, sa);
6652 prev = get_last_insn ();
6653 emit_stack_restore (SAVE_BLOCK, sa);
6655 record_new_stack_level ();
6657 fixup_args_size_notes (prev, get_last_insn (), 0);
6660 /* Emit code to save the current value of stack. */
6662 static rtx
6663 expand_stack_save (void)
6665 rtx ret = NULL_RTX;
6667 emit_stack_save (SAVE_BLOCK, &ret);
6668 return ret;
6671 /* Emit code to get the openacc gang, worker or vector id or size. */
6673 static rtx
6674 expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
6676 const char *name;
6677 rtx fallback_retval;
6678 rtx_insn *(*gen_fn) (rtx, rtx);
6679 switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
6681 case BUILT_IN_GOACC_PARLEVEL_ID:
6682 name = "__builtin_goacc_parlevel_id";
6683 fallback_retval = const0_rtx;
6684 gen_fn = targetm.gen_oacc_dim_pos;
6685 break;
6686 case BUILT_IN_GOACC_PARLEVEL_SIZE:
6687 name = "__builtin_goacc_parlevel_size";
6688 fallback_retval = const1_rtx;
6689 gen_fn = targetm.gen_oacc_dim_size;
6690 break;
6691 default:
6692 gcc_unreachable ();
6695 if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
6697 error ("%qs only supported in OpenACC code", name);
6698 return const0_rtx;
6701 tree arg = CALL_EXPR_ARG (exp, 0);
6702 if (TREE_CODE (arg) != INTEGER_CST)
6704 error ("non-constant argument 0 to %qs", name);
6705 return const0_rtx;
6708 int dim = TREE_INT_CST_LOW (arg);
6709 switch (dim)
6711 case GOMP_DIM_GANG:
6712 case GOMP_DIM_WORKER:
6713 case GOMP_DIM_VECTOR:
6714 break;
6715 default:
6716 error ("illegal argument 0 to %qs", name);
6717 return const0_rtx;
6720 if (ignore)
6721 return target;
6723 if (target == NULL_RTX)
6724 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
6726 if (!targetm.have_oacc_dim_size ())
6728 emit_move_insn (target, fallback_retval);
6729 return target;
6732 rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
6733 emit_insn (gen_fn (reg, GEN_INT (dim)));
6734 if (reg != target)
6735 emit_move_insn (target, reg);
6737 return target;
6740 /* Expand a string compare operation using a sequence of char comparison
6741 to get rid of the calling overhead, with result going to TARGET if
6742 that's convenient.
6744 VAR_STR is the variable string source;
6745 CONST_STR is the constant string source;
6746 LENGTH is the number of chars to compare;
6747 CONST_STR_N indicates which source string is the constant string;
6748 IS_MEMCMP indicates whether it's a memcmp or strcmp.
6750 to: (assume const_str_n is 2, i.e., arg2 is a constant string)
6752 target = (int) (unsigned char) var_str[0]
6753 - (int) (unsigned char) const_str[0];
6754 if (target != 0)
6755 goto ne_label;
6757 target = (int) (unsigned char) var_str[length - 2]
6758 - (int) (unsigned char) const_str[length - 2];
6759 if (target != 0)
6760 goto ne_label;
6761 target = (int) (unsigned char) var_str[length - 1]
6762 - (int) (unsigned char) const_str[length - 1];
6763 ne_label:
6766 static rtx
6767 inline_string_cmp (rtx target, tree var_str, const char *const_str,
6768 unsigned HOST_WIDE_INT length,
6769 int const_str_n, machine_mode mode)
6771 HOST_WIDE_INT offset = 0;
6772 rtx var_rtx_array
6773 = get_memory_rtx (var_str, build_int_cst (unsigned_type_node,length));
6774 rtx var_rtx = NULL_RTX;
6775 rtx const_rtx = NULL_RTX;
6776 rtx result = target ? target : gen_reg_rtx (mode);
6777 rtx_code_label *ne_label = gen_label_rtx ();
6778 tree unit_type_node = unsigned_char_type_node;
6779 scalar_int_mode unit_mode
6780 = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
6782 start_sequence ();
6784 for (unsigned HOST_WIDE_INT i = 0; i < length; i++)
6786 var_rtx
6787 = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset);
6788 const_rtx = c_readstr (const_str + offset, unit_mode);
6789 rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx;
6790 rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
6792 op0 = convert_modes (mode, unit_mode, op0, 1);
6793 op1 = convert_modes (mode, unit_mode, op1, 1);
6794 result = expand_simple_binop (mode, MINUS, op0, op1,
6795 result, 1, OPTAB_WIDEN);
6796 if (i < length - 1)
6797 emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
6798 mode, true, ne_label);
6799 offset += GET_MODE_SIZE (unit_mode);
6802 emit_label (ne_label);
6803 rtx_insn *insns = get_insns ();
6804 end_sequence ();
6805 emit_insn (insns);
6807 return result;
6810 /* Inline expansion of a call to str(n)cmp and memcmp, with result going
6811 to TARGET if that's convenient.
6812 If the call is not been inlined, return NULL_RTX. */
6814 static rtx
6815 inline_expand_builtin_bytecmp (tree exp, rtx target)
6817 tree fndecl = get_callee_fndecl (exp);
6818 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6819 bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP);
6821 /* Do NOT apply this inlining expansion when optimizing for size or
6822 optimization level below 2. */
6823 if (optimize < 2 || optimize_insn_for_size_p ())
6824 return NULL_RTX;
6826 gcc_checking_assert (fcode == BUILT_IN_STRCMP
6827 || fcode == BUILT_IN_STRNCMP
6828 || fcode == BUILT_IN_MEMCMP);
6830 /* On a target where the type of the call (int) has same or narrower presicion
6831 than unsigned char, give up the inlining expansion. */
6832 if (TYPE_PRECISION (unsigned_char_type_node)
6833 >= TYPE_PRECISION (TREE_TYPE (exp)))
6834 return NULL_RTX;
6836 tree arg1 = CALL_EXPR_ARG (exp, 0);
6837 tree arg2 = CALL_EXPR_ARG (exp, 1);
6838 tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
6840 unsigned HOST_WIDE_INT len1 = 0;
6841 unsigned HOST_WIDE_INT len2 = 0;
6842 unsigned HOST_WIDE_INT len3 = 0;
6844 /* Get the object representation of the initializers of ARG1 and ARG2
6845 as strings, provided they refer to constant objects, with their byte
6846 sizes in LEN1 and LEN2, respectively. */
6847 const char *bytes1 = getbyterep (arg1, &len1);
6848 const char *bytes2 = getbyterep (arg2, &len2);
6850 /* Fail if neither argument refers to an initialized constant. */
6851 if (!bytes1 && !bytes2)
6852 return NULL_RTX;
6854 if (is_ncmp)
6856 /* Fail if the memcmp/strncmp bound is not a constant. */
6857 if (!tree_fits_uhwi_p (len3_tree))
6858 return NULL_RTX;
6860 len3 = tree_to_uhwi (len3_tree);
6862 if (fcode == BUILT_IN_MEMCMP)
6864 /* Fail if the memcmp bound is greater than the size of either
6865 of the two constant objects. */
6866 if ((bytes1 && len1 < len3)
6867 || (bytes2 && len2 < len3))
6868 return NULL_RTX;
6872 if (fcode != BUILT_IN_MEMCMP)
6874 /* For string functions (i.e., strcmp and strncmp) reduce LEN1
6875 and LEN2 to the length of the nul-terminated string stored
6876 in each. */
6877 if (bytes1 != NULL)
6878 len1 = strnlen (bytes1, len1) + 1;
6879 if (bytes2 != NULL)
6880 len2 = strnlen (bytes2, len2) + 1;
6883 /* See inline_string_cmp. */
6884 int const_str_n;
6885 if (!len1)
6886 const_str_n = 2;
6887 else if (!len2)
6888 const_str_n = 1;
6889 else if (len2 > len1)
6890 const_str_n = 1;
6891 else
6892 const_str_n = 2;
6894 /* For strncmp only, compute the new bound as the smallest of
6895 the lengths of the two strings (plus 1) and the bound provided
6896 to the function. */
6897 unsigned HOST_WIDE_INT bound = (const_str_n == 1) ? len1 : len2;
6898 if (is_ncmp && len3 < bound)
6899 bound = len3;
6901 /* If the bound of the comparison is larger than the threshold,
6902 do nothing. */
6903 if (bound > (unsigned HOST_WIDE_INT) param_builtin_string_cmp_inline_length)
6904 return NULL_RTX;
6906 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
6908 /* Now, start inline expansion the call. */
6909 return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
6910 (const_str_n == 1) ? bytes1 : bytes2, bound,
6911 const_str_n, mode);
6914 /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
6915 represents the size of the first argument to that call, or VOIDmode
6916 if the argument is a pointer. IGNORE will be true if the result
6917 isn't used. */
6918 static rtx
6919 expand_speculation_safe_value (machine_mode mode, tree exp, rtx target,
6920 bool ignore)
6922 rtx val, failsafe;
6923 unsigned nargs = call_expr_nargs (exp);
6925 tree arg0 = CALL_EXPR_ARG (exp, 0);
6927 if (mode == VOIDmode)
6929 mode = TYPE_MODE (TREE_TYPE (arg0));
6930 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
6933 val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
6935 /* An optional second argument can be used as a failsafe value on
6936 some machines. If it isn't present, then the failsafe value is
6937 assumed to be 0. */
6938 if (nargs > 1)
6940 tree arg1 = CALL_EXPR_ARG (exp, 1);
6941 failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL);
6943 else
6944 failsafe = const0_rtx;
6946 /* If the result isn't used, the behavior is undefined. It would be
6947 nice to emit a warning here, but path splitting means this might
6948 happen with legitimate code. So simply drop the builtin
6949 expansion in that case; we've handled any side-effects above. */
6950 if (ignore)
6951 return const0_rtx;
6953 /* If we don't have a suitable target, create one to hold the result. */
6954 if (target == NULL || GET_MODE (target) != mode)
6955 target = gen_reg_rtx (mode);
6957 if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode)
6958 val = convert_modes (mode, VOIDmode, val, false);
6960 return targetm.speculation_safe_value (mode, target, val, failsafe);
6963 /* Expand an expression EXP that calls a built-in function,
6964 with result going to TARGET if that's convenient
6965 (and in mode MODE if that's convenient).
6966 SUBTARGET may be used as the target for computing one of EXP's operands.
6967 IGNORE is nonzero if the value is to be ignored. */
6970 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
6971 int ignore)
6973 tree fndecl = get_callee_fndecl (exp);
6974 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
6975 int flags;
6977 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6978 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6980 /* When ASan is enabled, we don't want to expand some memory/string
6981 builtins and rely on libsanitizer's hooks. This allows us to avoid
6982 redundant checks and be sure, that possible overflow will be detected
6983 by ASan. */
6985 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6986 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
6987 return expand_call (exp, target, ignore);
6989 /* When not optimizing, generate calls to library functions for a certain
6990 set of builtins. */
6991 if (!optimize
6992 && !called_as_built_in (fndecl)
6993 && fcode != BUILT_IN_FORK
6994 && fcode != BUILT_IN_EXECL
6995 && fcode != BUILT_IN_EXECV
6996 && fcode != BUILT_IN_EXECLP
6997 && fcode != BUILT_IN_EXECLE
6998 && fcode != BUILT_IN_EXECVP
6999 && fcode != BUILT_IN_EXECVE
7000 && fcode != BUILT_IN_CLEAR_CACHE
7001 && !ALLOCA_FUNCTION_CODE_P (fcode)
7002 && fcode != BUILT_IN_FREE)
7003 return expand_call (exp, target, ignore);
7005 /* The built-in function expanders test for target == const0_rtx
7006 to determine whether the function's result will be ignored. */
7007 if (ignore)
7008 target = const0_rtx;
7010 /* If the result of a pure or const built-in function is ignored, and
7011 none of its arguments are volatile, we can avoid expanding the
7012 built-in call and just evaluate the arguments for side-effects. */
7013 if (target == const0_rtx
7014 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
7015 && !(flags & ECF_LOOPING_CONST_OR_PURE))
7017 bool volatilep = false;
7018 tree arg;
7019 call_expr_arg_iterator iter;
7021 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7022 if (TREE_THIS_VOLATILE (arg))
7024 volatilep = true;
7025 break;
7028 if (! volatilep)
7030 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7031 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
7032 return const0_rtx;
7036 switch (fcode)
7038 CASE_FLT_FN (BUILT_IN_FABS):
7039 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
7040 case BUILT_IN_FABSD32:
7041 case BUILT_IN_FABSD64:
7042 case BUILT_IN_FABSD128:
7043 target = expand_builtin_fabs (exp, target, subtarget);
7044 if (target)
7045 return target;
7046 break;
7048 CASE_FLT_FN (BUILT_IN_COPYSIGN):
7049 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
7050 target = expand_builtin_copysign (exp, target, subtarget);
7051 if (target)
7052 return target;
7053 break;
7055 /* Just do a normal library call if we were unable to fold
7056 the values. */
7057 CASE_FLT_FN (BUILT_IN_CABS):
7058 break;
7060 CASE_FLT_FN (BUILT_IN_FMA):
7061 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
7062 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
7063 if (target)
7064 return target;
7065 break;
7067 CASE_FLT_FN (BUILT_IN_ILOGB):
7068 if (! flag_unsafe_math_optimizations)
7069 break;
7070 gcc_fallthrough ();
7071 CASE_FLT_FN (BUILT_IN_ISINF):
7072 CASE_FLT_FN (BUILT_IN_FINITE):
7073 case BUILT_IN_ISFINITE:
7074 case BUILT_IN_ISNORMAL:
7075 target = expand_builtin_interclass_mathfn (exp, target);
7076 if (target)
7077 return target;
7078 break;
7080 CASE_FLT_FN (BUILT_IN_ICEIL):
7081 CASE_FLT_FN (BUILT_IN_LCEIL):
7082 CASE_FLT_FN (BUILT_IN_LLCEIL):
7083 CASE_FLT_FN (BUILT_IN_LFLOOR):
7084 CASE_FLT_FN (BUILT_IN_IFLOOR):
7085 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7086 target = expand_builtin_int_roundingfn (exp, target);
7087 if (target)
7088 return target;
7089 break;
7091 CASE_FLT_FN (BUILT_IN_IRINT):
7092 CASE_FLT_FN (BUILT_IN_LRINT):
7093 CASE_FLT_FN (BUILT_IN_LLRINT):
7094 CASE_FLT_FN (BUILT_IN_IROUND):
7095 CASE_FLT_FN (BUILT_IN_LROUND):
7096 CASE_FLT_FN (BUILT_IN_LLROUND):
7097 target = expand_builtin_int_roundingfn_2 (exp, target);
7098 if (target)
7099 return target;
7100 break;
7102 CASE_FLT_FN (BUILT_IN_POWI):
7103 target = expand_builtin_powi (exp, target);
7104 if (target)
7105 return target;
7106 break;
7108 CASE_FLT_FN (BUILT_IN_CEXPI):
7109 target = expand_builtin_cexpi (exp, target);
7110 gcc_assert (target);
7111 return target;
7113 CASE_FLT_FN (BUILT_IN_SIN):
7114 CASE_FLT_FN (BUILT_IN_COS):
7115 if (! flag_unsafe_math_optimizations)
7116 break;
7117 target = expand_builtin_mathfn_3 (exp, target, subtarget);
7118 if (target)
7119 return target;
7120 break;
7122 CASE_FLT_FN (BUILT_IN_SINCOS):
7123 if (! flag_unsafe_math_optimizations)
7124 break;
7125 target = expand_builtin_sincos (exp);
7126 if (target)
7127 return target;
7128 break;
7130 case BUILT_IN_FEGETROUND:
7131 target = expand_builtin_fegetround (exp, target, target_mode);
7132 if (target)
7133 return target;
7134 break;
7136 case BUILT_IN_FECLEAREXCEPT:
7137 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
7138 feclearexcept_optab);
7139 if (target)
7140 return target;
7141 break;
7143 case BUILT_IN_FERAISEEXCEPT:
7144 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
7145 feraiseexcept_optab);
7146 if (target)
7147 return target;
7148 break;
7150 case BUILT_IN_APPLY_ARGS:
7151 return expand_builtin_apply_args ();
7153 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
7154 FUNCTION with a copy of the parameters described by
7155 ARGUMENTS, and ARGSIZE. It returns a block of memory
7156 allocated on the stack into which is stored all the registers
7157 that might possibly be used for returning the result of a
7158 function. ARGUMENTS is the value returned by
7159 __builtin_apply_args. ARGSIZE is the number of bytes of
7160 arguments that must be copied. ??? How should this value be
7161 computed? We'll also need a safe worst case value for varargs
7162 functions. */
7163 case BUILT_IN_APPLY:
7164 if (!validate_arglist (exp, POINTER_TYPE,
7165 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
7166 && !validate_arglist (exp, REFERENCE_TYPE,
7167 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7168 return const0_rtx;
7169 else
7171 rtx ops[3];
7173 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
7174 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
7175 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
7177 return expand_builtin_apply (ops[0], ops[1], ops[2]);
7180 /* __builtin_return (RESULT) causes the function to return the
7181 value described by RESULT. RESULT is address of the block of
7182 memory returned by __builtin_apply. */
7183 case BUILT_IN_RETURN:
7184 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7185 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
7186 return const0_rtx;
7188 case BUILT_IN_SAVEREGS:
7189 return expand_builtin_saveregs ();
7191 case BUILT_IN_VA_ARG_PACK:
7192 /* All valid uses of __builtin_va_arg_pack () are removed during
7193 inlining. */
7194 error ("invalid use of %<__builtin_va_arg_pack ()%>");
7195 return const0_rtx;
7197 case BUILT_IN_VA_ARG_PACK_LEN:
7198 /* All valid uses of __builtin_va_arg_pack_len () are removed during
7199 inlining. */
7200 error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
7201 return const0_rtx;
7203 /* Return the address of the first anonymous stack arg. */
7204 case BUILT_IN_NEXT_ARG:
7205 if (fold_builtin_next_arg (exp, false))
7206 return const0_rtx;
7207 return expand_builtin_next_arg ();
7209 case BUILT_IN_CLEAR_CACHE:
7210 expand_builtin___clear_cache (exp);
7211 return const0_rtx;
7213 case BUILT_IN_CLASSIFY_TYPE:
7214 return expand_builtin_classify_type (exp);
7216 case BUILT_IN_CONSTANT_P:
7217 return const0_rtx;
7219 case BUILT_IN_FRAME_ADDRESS:
7220 case BUILT_IN_RETURN_ADDRESS:
7221 return expand_builtin_frame_address (fndecl, exp);
7223 /* Returns the address of the area where the structure is returned.
7224 0 otherwise. */
7225 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
7226 if (call_expr_nargs (exp) != 0
7227 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
7228 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
7229 return const0_rtx;
7230 else
7231 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
7233 CASE_BUILT_IN_ALLOCA:
7234 target = expand_builtin_alloca (exp);
7235 if (target)
7236 return target;
7237 break;
7239 case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
7240 return expand_asan_emit_allocas_unpoison (exp);
7242 case BUILT_IN_STACK_SAVE:
7243 return expand_stack_save ();
7245 case BUILT_IN_STACK_RESTORE:
7246 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
7247 return const0_rtx;
7249 case BUILT_IN_BSWAP16:
7250 case BUILT_IN_BSWAP32:
7251 case BUILT_IN_BSWAP64:
7252 case BUILT_IN_BSWAP128:
7253 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
7254 if (target)
7255 return target;
7256 break;
7258 CASE_INT_FN (BUILT_IN_FFS):
7259 target = expand_builtin_unop (target_mode, exp, target,
7260 subtarget, ffs_optab);
7261 if (target)
7262 return target;
7263 break;
7265 CASE_INT_FN (BUILT_IN_CLZ):
7266 target = expand_builtin_unop (target_mode, exp, target,
7267 subtarget, clz_optab);
7268 if (target)
7269 return target;
7270 break;
7272 CASE_INT_FN (BUILT_IN_CTZ):
7273 target = expand_builtin_unop (target_mode, exp, target,
7274 subtarget, ctz_optab);
7275 if (target)
7276 return target;
7277 break;
7279 CASE_INT_FN (BUILT_IN_CLRSB):
7280 target = expand_builtin_unop (target_mode, exp, target,
7281 subtarget, clrsb_optab);
7282 if (target)
7283 return target;
7284 break;
7286 CASE_INT_FN (BUILT_IN_POPCOUNT):
7287 target = expand_builtin_unop (target_mode, exp, target,
7288 subtarget, popcount_optab);
7289 if (target)
7290 return target;
7291 break;
7293 CASE_INT_FN (BUILT_IN_PARITY):
7294 target = expand_builtin_unop (target_mode, exp, target,
7295 subtarget, parity_optab);
7296 if (target)
7297 return target;
7298 break;
7300 case BUILT_IN_STRLEN:
7301 target = expand_builtin_strlen (exp, target, target_mode);
7302 if (target)
7303 return target;
7304 break;
7306 case BUILT_IN_STRNLEN:
7307 target = expand_builtin_strnlen (exp, target, target_mode);
7308 if (target)
7309 return target;
7310 break;
7312 case BUILT_IN_STRCPY:
7313 target = expand_builtin_strcpy (exp, target);
7314 if (target)
7315 return target;
7316 break;
7318 case BUILT_IN_STRNCPY:
7319 target = expand_builtin_strncpy (exp, target);
7320 if (target)
7321 return target;
7322 break;
7324 case BUILT_IN_STPCPY:
7325 target = expand_builtin_stpcpy (exp, target, mode);
7326 if (target)
7327 return target;
7328 break;
7330 case BUILT_IN_MEMCPY:
7331 target = expand_builtin_memcpy (exp, target);
7332 if (target)
7333 return target;
7334 break;
7336 case BUILT_IN_MEMMOVE:
7337 target = expand_builtin_memmove (exp, target);
7338 if (target)
7339 return target;
7340 break;
7342 case BUILT_IN_MEMPCPY:
7343 target = expand_builtin_mempcpy (exp, target);
7344 if (target)
7345 return target;
7346 break;
7348 case BUILT_IN_MEMSET:
7349 target = expand_builtin_memset (exp, target, mode);
7350 if (target)
7351 return target;
7352 break;
7354 case BUILT_IN_BZERO:
7355 target = expand_builtin_bzero (exp);
7356 if (target)
7357 return target;
7358 break;
7360 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7361 back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
7362 when changing it to a strcmp call. */
7363 case BUILT_IN_STRCMP_EQ:
7364 target = expand_builtin_memcmp (exp, target, true);
7365 if (target)
7366 return target;
7368 /* Change this call back to a BUILT_IN_STRCMP. */
7369 TREE_OPERAND (exp, 1)
7370 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
7372 /* Delete the last parameter. */
7373 unsigned int i;
7374 vec<tree, va_gc> *arg_vec;
7375 vec_alloc (arg_vec, 2);
7376 for (i = 0; i < 2; i++)
7377 arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
7378 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
7379 /* FALLTHROUGH */
7381 case BUILT_IN_STRCMP:
7382 target = expand_builtin_strcmp (exp, target);
7383 if (target)
7384 return target;
7385 break;
7387 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7388 back to a BUILT_IN_STRNCMP. */
7389 case BUILT_IN_STRNCMP_EQ:
7390 target = expand_builtin_memcmp (exp, target, true);
7391 if (target)
7392 return target;
7394 /* Change it back to a BUILT_IN_STRNCMP. */
7395 TREE_OPERAND (exp, 1)
7396 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
7397 /* FALLTHROUGH */
7399 case BUILT_IN_STRNCMP:
7400 target = expand_builtin_strncmp (exp, target, mode);
7401 if (target)
7402 return target;
7403 break;
7405 case BUILT_IN_BCMP:
7406 case BUILT_IN_MEMCMP:
7407 case BUILT_IN_MEMCMP_EQ:
7408 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
7409 if (target)
7410 return target;
7411 if (fcode == BUILT_IN_MEMCMP_EQ)
7413 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
7414 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
7416 break;
7418 case BUILT_IN_SETJMP:
7419 /* This should have been lowered to the builtins below. */
7420 gcc_unreachable ();
7422 case BUILT_IN_SETJMP_SETUP:
7423 /* __builtin_setjmp_setup is passed a pointer to an array of five words
7424 and the receiver label. */
7425 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7427 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7428 VOIDmode, EXPAND_NORMAL);
7429 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
7430 rtx_insn *label_r = label_rtx (label);
7432 /* This is copied from the handling of non-local gotos. */
7433 expand_builtin_setjmp_setup (buf_addr, label_r);
7434 nonlocal_goto_handler_labels
7435 = gen_rtx_INSN_LIST (VOIDmode, label_r,
7436 nonlocal_goto_handler_labels);
7437 /* ??? Do not let expand_label treat us as such since we would
7438 not want to be both on the list of non-local labels and on
7439 the list of forced labels. */
7440 FORCED_LABEL (label) = 0;
7441 return const0_rtx;
7443 break;
7445 case BUILT_IN_SETJMP_RECEIVER:
7446 /* __builtin_setjmp_receiver is passed the receiver label. */
7447 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7449 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
7450 rtx_insn *label_r = label_rtx (label);
7452 expand_builtin_setjmp_receiver (label_r);
7453 return const0_rtx;
7455 break;
7457 /* __builtin_longjmp is passed a pointer to an array of five words.
7458 It's similar to the C library longjmp function but works with
7459 __builtin_setjmp above. */
7460 case BUILT_IN_LONGJMP:
7461 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7463 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7464 VOIDmode, EXPAND_NORMAL);
7465 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
7467 if (value != const1_rtx)
7469 error ("%<__builtin_longjmp%> second argument must be 1");
7470 return const0_rtx;
7473 expand_builtin_longjmp (buf_addr, value);
7474 return const0_rtx;
7476 break;
7478 case BUILT_IN_NONLOCAL_GOTO:
7479 target = expand_builtin_nonlocal_goto (exp);
7480 if (target)
7481 return target;
7482 break;
7484 /* This updates the setjmp buffer that is its argument with the value
7485 of the current stack pointer. */
7486 case BUILT_IN_UPDATE_SETJMP_BUF:
7487 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7489 rtx buf_addr
7490 = expand_normal (CALL_EXPR_ARG (exp, 0));
7492 expand_builtin_update_setjmp_buf (buf_addr);
7493 return const0_rtx;
7495 break;
7497 case BUILT_IN_TRAP:
7498 expand_builtin_trap ();
7499 return const0_rtx;
7501 case BUILT_IN_UNREACHABLE:
7502 expand_builtin_unreachable ();
7503 return const0_rtx;
7505 CASE_FLT_FN (BUILT_IN_SIGNBIT):
7506 case BUILT_IN_SIGNBITD32:
7507 case BUILT_IN_SIGNBITD64:
7508 case BUILT_IN_SIGNBITD128:
7509 target = expand_builtin_signbit (exp, target);
7510 if (target)
7511 return target;
7512 break;
7514 /* Various hooks for the DWARF 2 __throw routine. */
7515 case BUILT_IN_UNWIND_INIT:
7516 expand_builtin_unwind_init ();
7517 return const0_rtx;
7518 case BUILT_IN_DWARF_CFA:
7519 return virtual_cfa_rtx;
7520 #ifdef DWARF2_UNWIND_INFO
7521 case BUILT_IN_DWARF_SP_COLUMN:
7522 return expand_builtin_dwarf_sp_column ();
7523 case BUILT_IN_INIT_DWARF_REG_SIZES:
7524 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
7525 return const0_rtx;
7526 #endif
7527 case BUILT_IN_FROB_RETURN_ADDR:
7528 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
7529 case BUILT_IN_EXTRACT_RETURN_ADDR:
7530 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
7531 case BUILT_IN_EH_RETURN:
7532 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
7533 CALL_EXPR_ARG (exp, 1));
7534 return const0_rtx;
7535 case BUILT_IN_EH_RETURN_DATA_REGNO:
7536 return expand_builtin_eh_return_data_regno (exp);
7537 case BUILT_IN_EXTEND_POINTER:
7538 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
7539 case BUILT_IN_EH_POINTER:
7540 return expand_builtin_eh_pointer (exp);
7541 case BUILT_IN_EH_FILTER:
7542 return expand_builtin_eh_filter (exp);
7543 case BUILT_IN_EH_COPY_VALUES:
7544 return expand_builtin_eh_copy_values (exp);
7546 case BUILT_IN_VA_START:
7547 return expand_builtin_va_start (exp);
7548 case BUILT_IN_VA_END:
7549 return expand_builtin_va_end (exp);
7550 case BUILT_IN_VA_COPY:
7551 return expand_builtin_va_copy (exp);
7552 case BUILT_IN_EXPECT:
7553 return expand_builtin_expect (exp, target);
7554 case BUILT_IN_EXPECT_WITH_PROBABILITY:
7555 return expand_builtin_expect_with_probability (exp, target);
7556 case BUILT_IN_ASSUME_ALIGNED:
7557 return expand_builtin_assume_aligned (exp, target);
7558 case BUILT_IN_PREFETCH:
7559 expand_builtin_prefetch (exp);
7560 return const0_rtx;
7562 case BUILT_IN_INIT_TRAMPOLINE:
7563 return expand_builtin_init_trampoline (exp, true);
7564 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
7565 return expand_builtin_init_trampoline (exp, false);
7566 case BUILT_IN_ADJUST_TRAMPOLINE:
7567 return expand_builtin_adjust_trampoline (exp);
7569 case BUILT_IN_INIT_DESCRIPTOR:
7570 return expand_builtin_init_descriptor (exp);
7571 case BUILT_IN_ADJUST_DESCRIPTOR:
7572 return expand_builtin_adjust_descriptor (exp);
7574 case BUILT_IN_FORK:
7575 case BUILT_IN_EXECL:
7576 case BUILT_IN_EXECV:
7577 case BUILT_IN_EXECLP:
7578 case BUILT_IN_EXECLE:
7579 case BUILT_IN_EXECVP:
7580 case BUILT_IN_EXECVE:
7581 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
7582 if (target)
7583 return target;
7584 break;
7586 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
7587 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
7588 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
7589 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
7590 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
7591 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
7592 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
7593 if (target)
7594 return target;
7595 break;
7597 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
7598 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
7599 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
7600 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
7601 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
7602 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
7603 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
7604 if (target)
7605 return target;
7606 break;
7608 case BUILT_IN_SYNC_FETCH_AND_OR_1:
7609 case BUILT_IN_SYNC_FETCH_AND_OR_2:
7610 case BUILT_IN_SYNC_FETCH_AND_OR_4:
7611 case BUILT_IN_SYNC_FETCH_AND_OR_8:
7612 case BUILT_IN_SYNC_FETCH_AND_OR_16:
7613 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
7614 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
7615 if (target)
7616 return target;
7617 break;
7619 case BUILT_IN_SYNC_FETCH_AND_AND_1:
7620 case BUILT_IN_SYNC_FETCH_AND_AND_2:
7621 case BUILT_IN_SYNC_FETCH_AND_AND_4:
7622 case BUILT_IN_SYNC_FETCH_AND_AND_8:
7623 case BUILT_IN_SYNC_FETCH_AND_AND_16:
7624 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
7625 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
7626 if (target)
7627 return target;
7628 break;
7630 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
7631 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
7632 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
7633 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
7634 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
7635 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
7636 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
7637 if (target)
7638 return target;
7639 break;
7641 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
7642 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
7643 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
7644 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
7645 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
7646 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
7647 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
7648 if (target)
7649 return target;
7650 break;
7652 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
7653 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
7654 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
7655 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
7656 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
7657 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
7658 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
7659 if (target)
7660 return target;
7661 break;
7663 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
7664 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
7665 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
7666 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
7667 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
7668 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
7669 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
7670 if (target)
7671 return target;
7672 break;
7674 case BUILT_IN_SYNC_OR_AND_FETCH_1:
7675 case BUILT_IN_SYNC_OR_AND_FETCH_2:
7676 case BUILT_IN_SYNC_OR_AND_FETCH_4:
7677 case BUILT_IN_SYNC_OR_AND_FETCH_8:
7678 case BUILT_IN_SYNC_OR_AND_FETCH_16:
7679 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
7680 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
7681 if (target)
7682 return target;
7683 break;
7685 case BUILT_IN_SYNC_AND_AND_FETCH_1:
7686 case BUILT_IN_SYNC_AND_AND_FETCH_2:
7687 case BUILT_IN_SYNC_AND_AND_FETCH_4:
7688 case BUILT_IN_SYNC_AND_AND_FETCH_8:
7689 case BUILT_IN_SYNC_AND_AND_FETCH_16:
7690 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
7691 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
7692 if (target)
7693 return target;
7694 break;
7696 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
7697 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
7698 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
7699 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
7700 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
7701 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
7702 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
7703 if (target)
7704 return target;
7705 break;
7707 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
7708 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
7709 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
7710 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
7711 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
7712 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
7713 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
7714 if (target)
7715 return target;
7716 break;
7718 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
7719 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
7720 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
7721 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
7722 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
7723 if (mode == VOIDmode)
7724 mode = TYPE_MODE (boolean_type_node);
7725 if (!target || !register_operand (target, mode))
7726 target = gen_reg_rtx (mode);
7728 mode = get_builtin_sync_mode
7729 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
7730 target = expand_builtin_compare_and_swap (mode, exp, true, target);
7731 if (target)
7732 return target;
7733 break;
7735 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
7736 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
7737 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
7738 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
7739 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
7740 mode = get_builtin_sync_mode
7741 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
7742 target = expand_builtin_compare_and_swap (mode, exp, false, target);
7743 if (target)
7744 return target;
7745 break;
7747 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
7748 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
7749 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
7750 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
7751 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
7752 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
7753 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
7754 if (target)
7755 return target;
7756 break;
7758 case BUILT_IN_SYNC_LOCK_RELEASE_1:
7759 case BUILT_IN_SYNC_LOCK_RELEASE_2:
7760 case BUILT_IN_SYNC_LOCK_RELEASE_4:
7761 case BUILT_IN_SYNC_LOCK_RELEASE_8:
7762 case BUILT_IN_SYNC_LOCK_RELEASE_16:
7763 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
7764 expand_builtin_sync_lock_release (mode, exp);
7765 return const0_rtx;
7767 case BUILT_IN_SYNC_SYNCHRONIZE:
7768 expand_builtin_sync_synchronize ();
7769 return const0_rtx;
7771 case BUILT_IN_ATOMIC_EXCHANGE_1:
7772 case BUILT_IN_ATOMIC_EXCHANGE_2:
7773 case BUILT_IN_ATOMIC_EXCHANGE_4:
7774 case BUILT_IN_ATOMIC_EXCHANGE_8:
7775 case BUILT_IN_ATOMIC_EXCHANGE_16:
7776 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
7777 target = expand_builtin_atomic_exchange (mode, exp, target);
7778 if (target)
7779 return target;
7780 break;
7782 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
7783 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
7784 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
7785 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
7786 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
7788 unsigned int nargs, z;
7789 vec<tree, va_gc> *vec;
7791 mode =
7792 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
7793 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
7794 if (target)
7795 return target;
7797 /* If this is turned into an external library call, the weak parameter
7798 must be dropped to match the expected parameter list. */
7799 nargs = call_expr_nargs (exp);
7800 vec_alloc (vec, nargs - 1);
7801 for (z = 0; z < 3; z++)
7802 vec->quick_push (CALL_EXPR_ARG (exp, z));
7803 /* Skip the boolean weak parameter. */
7804 for (z = 4; z < 6; z++)
7805 vec->quick_push (CALL_EXPR_ARG (exp, z));
7806 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
7807 break;
7810 case BUILT_IN_ATOMIC_LOAD_1:
7811 case BUILT_IN_ATOMIC_LOAD_2:
7812 case BUILT_IN_ATOMIC_LOAD_4:
7813 case BUILT_IN_ATOMIC_LOAD_8:
7814 case BUILT_IN_ATOMIC_LOAD_16:
7815 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
7816 target = expand_builtin_atomic_load (mode, exp, target);
7817 if (target)
7818 return target;
7819 break;
7821 case BUILT_IN_ATOMIC_STORE_1:
7822 case BUILT_IN_ATOMIC_STORE_2:
7823 case BUILT_IN_ATOMIC_STORE_4:
7824 case BUILT_IN_ATOMIC_STORE_8:
7825 case BUILT_IN_ATOMIC_STORE_16:
7826 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
7827 target = expand_builtin_atomic_store (mode, exp);
7828 if (target)
7829 return const0_rtx;
7830 break;
7832 case BUILT_IN_ATOMIC_ADD_FETCH_1:
7833 case BUILT_IN_ATOMIC_ADD_FETCH_2:
7834 case BUILT_IN_ATOMIC_ADD_FETCH_4:
7835 case BUILT_IN_ATOMIC_ADD_FETCH_8:
7836 case BUILT_IN_ATOMIC_ADD_FETCH_16:
7838 enum built_in_function lib;
7839 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
7840 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
7841 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
7842 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
7843 ignore, lib);
7844 if (target)
7845 return target;
7846 break;
7848 case BUILT_IN_ATOMIC_SUB_FETCH_1:
7849 case BUILT_IN_ATOMIC_SUB_FETCH_2:
7850 case BUILT_IN_ATOMIC_SUB_FETCH_4:
7851 case BUILT_IN_ATOMIC_SUB_FETCH_8:
7852 case BUILT_IN_ATOMIC_SUB_FETCH_16:
7854 enum built_in_function lib;
7855 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
7856 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
7857 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
7858 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
7859 ignore, lib);
7860 if (target)
7861 return target;
7862 break;
7864 case BUILT_IN_ATOMIC_AND_FETCH_1:
7865 case BUILT_IN_ATOMIC_AND_FETCH_2:
7866 case BUILT_IN_ATOMIC_AND_FETCH_4:
7867 case BUILT_IN_ATOMIC_AND_FETCH_8:
7868 case BUILT_IN_ATOMIC_AND_FETCH_16:
7870 enum built_in_function lib;
7871 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
7872 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
7873 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
7874 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
7875 ignore, lib);
7876 if (target)
7877 return target;
7878 break;
7880 case BUILT_IN_ATOMIC_NAND_FETCH_1:
7881 case BUILT_IN_ATOMIC_NAND_FETCH_2:
7882 case BUILT_IN_ATOMIC_NAND_FETCH_4:
7883 case BUILT_IN_ATOMIC_NAND_FETCH_8:
7884 case BUILT_IN_ATOMIC_NAND_FETCH_16:
7886 enum built_in_function lib;
7887 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
7888 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
7889 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
7890 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
7891 ignore, lib);
7892 if (target)
7893 return target;
7894 break;
7896 case BUILT_IN_ATOMIC_XOR_FETCH_1:
7897 case BUILT_IN_ATOMIC_XOR_FETCH_2:
7898 case BUILT_IN_ATOMIC_XOR_FETCH_4:
7899 case BUILT_IN_ATOMIC_XOR_FETCH_8:
7900 case BUILT_IN_ATOMIC_XOR_FETCH_16:
7902 enum built_in_function lib;
7903 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
7904 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
7905 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
7906 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
7907 ignore, lib);
7908 if (target)
7909 return target;
7910 break;
7912 case BUILT_IN_ATOMIC_OR_FETCH_1:
7913 case BUILT_IN_ATOMIC_OR_FETCH_2:
7914 case BUILT_IN_ATOMIC_OR_FETCH_4:
7915 case BUILT_IN_ATOMIC_OR_FETCH_8:
7916 case BUILT_IN_ATOMIC_OR_FETCH_16:
7918 enum built_in_function lib;
7919 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
7920 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
7921 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
7922 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
7923 ignore, lib);
7924 if (target)
7925 return target;
7926 break;
7928 case BUILT_IN_ATOMIC_FETCH_ADD_1:
7929 case BUILT_IN_ATOMIC_FETCH_ADD_2:
7930 case BUILT_IN_ATOMIC_FETCH_ADD_4:
7931 case BUILT_IN_ATOMIC_FETCH_ADD_8:
7932 case BUILT_IN_ATOMIC_FETCH_ADD_16:
7933 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
7934 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
7935 ignore, BUILT_IN_NONE);
7936 if (target)
7937 return target;
7938 break;
7940 case BUILT_IN_ATOMIC_FETCH_SUB_1:
7941 case BUILT_IN_ATOMIC_FETCH_SUB_2:
7942 case BUILT_IN_ATOMIC_FETCH_SUB_4:
7943 case BUILT_IN_ATOMIC_FETCH_SUB_8:
7944 case BUILT_IN_ATOMIC_FETCH_SUB_16:
7945 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
7946 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
7947 ignore, BUILT_IN_NONE);
7948 if (target)
7949 return target;
7950 break;
7952 case BUILT_IN_ATOMIC_FETCH_AND_1:
7953 case BUILT_IN_ATOMIC_FETCH_AND_2:
7954 case BUILT_IN_ATOMIC_FETCH_AND_4:
7955 case BUILT_IN_ATOMIC_FETCH_AND_8:
7956 case BUILT_IN_ATOMIC_FETCH_AND_16:
7957 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
7958 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
7959 ignore, BUILT_IN_NONE);
7960 if (target)
7961 return target;
7962 break;
7964 case BUILT_IN_ATOMIC_FETCH_NAND_1:
7965 case BUILT_IN_ATOMIC_FETCH_NAND_2:
7966 case BUILT_IN_ATOMIC_FETCH_NAND_4:
7967 case BUILT_IN_ATOMIC_FETCH_NAND_8:
7968 case BUILT_IN_ATOMIC_FETCH_NAND_16:
7969 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
7970 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
7971 ignore, BUILT_IN_NONE);
7972 if (target)
7973 return target;
7974 break;
7976 case BUILT_IN_ATOMIC_FETCH_XOR_1:
7977 case BUILT_IN_ATOMIC_FETCH_XOR_2:
7978 case BUILT_IN_ATOMIC_FETCH_XOR_4:
7979 case BUILT_IN_ATOMIC_FETCH_XOR_8:
7980 case BUILT_IN_ATOMIC_FETCH_XOR_16:
7981 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
7982 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
7983 ignore, BUILT_IN_NONE);
7984 if (target)
7985 return target;
7986 break;
7988 case BUILT_IN_ATOMIC_FETCH_OR_1:
7989 case BUILT_IN_ATOMIC_FETCH_OR_2:
7990 case BUILT_IN_ATOMIC_FETCH_OR_4:
7991 case BUILT_IN_ATOMIC_FETCH_OR_8:
7992 case BUILT_IN_ATOMIC_FETCH_OR_16:
7993 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
7994 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
7995 ignore, BUILT_IN_NONE);
7996 if (target)
7997 return target;
7998 break;
8000 case BUILT_IN_ATOMIC_TEST_AND_SET:
8001 return expand_builtin_atomic_test_and_set (exp, target);
8003 case BUILT_IN_ATOMIC_CLEAR:
8004 return expand_builtin_atomic_clear (exp);
8006 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8007 return expand_builtin_atomic_always_lock_free (exp);
8009 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8010 target = expand_builtin_atomic_is_lock_free (exp);
8011 if (target)
8012 return target;
8013 break;
8015 case BUILT_IN_ATOMIC_THREAD_FENCE:
8016 expand_builtin_atomic_thread_fence (exp);
8017 return const0_rtx;
8019 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
8020 expand_builtin_atomic_signal_fence (exp);
8021 return const0_rtx;
8023 case BUILT_IN_OBJECT_SIZE:
8024 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
8025 return expand_builtin_object_size (exp);
8027 case BUILT_IN_MEMCPY_CHK:
8028 case BUILT_IN_MEMPCPY_CHK:
8029 case BUILT_IN_MEMMOVE_CHK:
8030 case BUILT_IN_MEMSET_CHK:
8031 target = expand_builtin_memory_chk (exp, target, mode, fcode);
8032 if (target)
8033 return target;
8034 break;
8036 case BUILT_IN_STRCPY_CHK:
8037 case BUILT_IN_STPCPY_CHK:
8038 case BUILT_IN_STRNCPY_CHK:
8039 case BUILT_IN_STPNCPY_CHK:
8040 case BUILT_IN_STRCAT_CHK:
8041 case BUILT_IN_STRNCAT_CHK:
8042 case BUILT_IN_SNPRINTF_CHK:
8043 case BUILT_IN_VSNPRINTF_CHK:
8044 maybe_emit_chk_warning (exp, fcode);
8045 break;
8047 case BUILT_IN_SPRINTF_CHK:
8048 case BUILT_IN_VSPRINTF_CHK:
8049 maybe_emit_sprintf_chk_warning (exp, fcode);
8050 break;
8052 case BUILT_IN_THREAD_POINTER:
8053 return expand_builtin_thread_pointer (exp, target);
8055 case BUILT_IN_SET_THREAD_POINTER:
8056 expand_builtin_set_thread_pointer (exp);
8057 return const0_rtx;
8059 case BUILT_IN_ACC_ON_DEVICE:
8060 /* Do library call, if we failed to expand the builtin when
8061 folding. */
8062 break;
8064 case BUILT_IN_GOACC_PARLEVEL_ID:
8065 case BUILT_IN_GOACC_PARLEVEL_SIZE:
8066 return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
8068 case BUILT_IN_SPECULATION_SAFE_VALUE_PTR:
8069 return expand_speculation_safe_value (VOIDmode, exp, target, ignore);
8071 case BUILT_IN_SPECULATION_SAFE_VALUE_1:
8072 case BUILT_IN_SPECULATION_SAFE_VALUE_2:
8073 case BUILT_IN_SPECULATION_SAFE_VALUE_4:
8074 case BUILT_IN_SPECULATION_SAFE_VALUE_8:
8075 case BUILT_IN_SPECULATION_SAFE_VALUE_16:
8076 mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1);
8077 return expand_speculation_safe_value (mode, exp, target, ignore);
8079 default: /* just do library call, if unknown builtin */
8080 break;
8083 /* The switch statement above can drop through to cause the function
8084 to be called normally. */
8085 return expand_call (exp, target, ignore);
8088 /* Determine whether a tree node represents a call to a built-in
8089 function. If the tree T is a call to a built-in function with
8090 the right number of arguments of the appropriate types, return
8091 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
8092 Otherwise the return value is END_BUILTINS. */
8094 enum built_in_function
8095 builtin_mathfn_code (const_tree t)
8097 const_tree fndecl, arg, parmlist;
8098 const_tree argtype, parmtype;
8099 const_call_expr_arg_iterator iter;
8101 if (TREE_CODE (t) != CALL_EXPR)
8102 return END_BUILTINS;
8104 fndecl = get_callee_fndecl (t);
8105 if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
8106 return END_BUILTINS;
8108 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
8109 init_const_call_expr_arg_iterator (t, &iter);
8110 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
8112 /* If a function doesn't take a variable number of arguments,
8113 the last element in the list will have type `void'. */
8114 parmtype = TREE_VALUE (parmlist);
8115 if (VOID_TYPE_P (parmtype))
8117 if (more_const_call_expr_args_p (&iter))
8118 return END_BUILTINS;
8119 return DECL_FUNCTION_CODE (fndecl);
8122 if (! more_const_call_expr_args_p (&iter))
8123 return END_BUILTINS;
8125 arg = next_const_call_expr_arg (&iter);
8126 argtype = TREE_TYPE (arg);
8128 if (SCALAR_FLOAT_TYPE_P (parmtype))
8130 if (! SCALAR_FLOAT_TYPE_P (argtype))
8131 return END_BUILTINS;
8133 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
8135 if (! COMPLEX_FLOAT_TYPE_P (argtype))
8136 return END_BUILTINS;
8138 else if (POINTER_TYPE_P (parmtype))
8140 if (! POINTER_TYPE_P (argtype))
8141 return END_BUILTINS;
8143 else if (INTEGRAL_TYPE_P (parmtype))
8145 if (! INTEGRAL_TYPE_P (argtype))
8146 return END_BUILTINS;
8148 else
8149 return END_BUILTINS;
8152 /* Variable-length argument list. */
8153 return DECL_FUNCTION_CODE (fndecl);
8156 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
8157 evaluate to a constant. */
8159 static tree
8160 fold_builtin_constant_p (tree arg)
8162 /* We return 1 for a numeric type that's known to be a constant
8163 value at compile-time or for an aggregate type that's a
8164 literal constant. */
8165 STRIP_NOPS (arg);
8167 /* If we know this is a constant, emit the constant of one. */
8168 if (CONSTANT_CLASS_P (arg)
8169 || (TREE_CODE (arg) == CONSTRUCTOR
8170 && TREE_CONSTANT (arg)))
8171 return integer_one_node;
8172 if (TREE_CODE (arg) == ADDR_EXPR)
8174 tree op = TREE_OPERAND (arg, 0);
8175 if (TREE_CODE (op) == STRING_CST
8176 || (TREE_CODE (op) == ARRAY_REF
8177 && integer_zerop (TREE_OPERAND (op, 1))
8178 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
8179 return integer_one_node;
8182 /* If this expression has side effects, show we don't know it to be a
8183 constant. Likewise if it's a pointer or aggregate type since in
8184 those case we only want literals, since those are only optimized
8185 when generating RTL, not later.
8186 And finally, if we are compiling an initializer, not code, we
8187 need to return a definite result now; there's not going to be any
8188 more optimization done. */
8189 if (TREE_SIDE_EFFECTS (arg)
8190 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
8191 || POINTER_TYPE_P (TREE_TYPE (arg))
8192 || cfun == 0
8193 || folding_initializer
8194 || force_folding_builtin_constant_p)
8195 return integer_zero_node;
8197 return NULL_TREE;
8200 /* Create builtin_expect or builtin_expect_with_probability
8201 with PRED and EXPECTED as its arguments and return it as a truthvalue.
8202 Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
8203 builtin_expect_with_probability instead uses third argument as PROBABILITY
8204 value. */
8206 static tree
8207 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
8208 tree predictor, tree probability)
8210 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
8212 fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT
8213 : BUILT_IN_EXPECT_WITH_PROBABILITY);
8214 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
8215 ret_type = TREE_TYPE (TREE_TYPE (fn));
8216 pred_type = TREE_VALUE (arg_types);
8217 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
8219 pred = fold_convert_loc (loc, pred_type, pred);
8220 expected = fold_convert_loc (loc, expected_type, expected);
8222 if (probability)
8223 call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability);
8224 else
8225 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
8226 predictor);
8228 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
8229 build_int_cst (ret_type, 0));
8232 /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
8233 NULL_TREE if no simplification is possible. */
8235 tree
8236 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2,
8237 tree arg3)
8239 tree inner, fndecl, inner_arg0;
8240 enum tree_code code;
8242 /* Distribute the expected value over short-circuiting operators.
8243 See through the cast from truthvalue_type_node to long. */
8244 inner_arg0 = arg0;
8245 while (CONVERT_EXPR_P (inner_arg0)
8246 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
8247 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
8248 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
8250 /* If this is a builtin_expect within a builtin_expect keep the
8251 inner one. See through a comparison against a constant. It
8252 might have been added to create a thruthvalue. */
8253 inner = inner_arg0;
8255 if (COMPARISON_CLASS_P (inner)
8256 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
8257 inner = TREE_OPERAND (inner, 0);
8259 if (TREE_CODE (inner) == CALL_EXPR
8260 && (fndecl = get_callee_fndecl (inner))
8261 && (fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
8262 || fndecl_built_in_p (fndecl, BUILT_IN_EXPECT_WITH_PROBABILITY)))
8263 return arg0;
8265 inner = inner_arg0;
8266 code = TREE_CODE (inner);
8267 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
8269 tree op0 = TREE_OPERAND (inner, 0);
8270 tree op1 = TREE_OPERAND (inner, 1);
8271 arg1 = save_expr (arg1);
8273 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3);
8274 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3);
8275 inner = build2 (code, TREE_TYPE (inner), op0, op1);
8277 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
8280 /* If the argument isn't invariant then there's nothing else we can do. */
8281 if (!TREE_CONSTANT (inner_arg0))
8282 return NULL_TREE;
8284 /* If we expect that a comparison against the argument will fold to
8285 a constant return the constant. In practice, this means a true
8286 constant or the address of a non-weak symbol. */
8287 inner = inner_arg0;
8288 STRIP_NOPS (inner);
8289 if (TREE_CODE (inner) == ADDR_EXPR)
8293 inner = TREE_OPERAND (inner, 0);
8295 while (TREE_CODE (inner) == COMPONENT_REF
8296 || TREE_CODE (inner) == ARRAY_REF);
8297 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
8298 return NULL_TREE;
8301 /* Otherwise, ARG0 already has the proper type for the return value. */
8302 return arg0;
8305 /* Fold a call to __builtin_classify_type with argument ARG. */
8307 static tree
8308 fold_builtin_classify_type (tree arg)
8310 if (arg == 0)
8311 return build_int_cst (integer_type_node, no_type_class);
8313 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
8316 /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
8317 ARG. */
8319 static tree
8320 fold_builtin_strlen (location_t loc, tree expr, tree type, tree arg)
8322 if (!validate_arg (arg, POINTER_TYPE))
8323 return NULL_TREE;
8324 else
8326 c_strlen_data lendata = { };
8327 tree len = c_strlen (arg, 0, &lendata);
8329 if (len)
8330 return fold_convert_loc (loc, type, len);
8332 /* TODO: Move this to gimple-ssa-warn-access once the pass runs
8333 also early enough to detect invalid reads in multimensional
8334 arrays and struct members. */
8335 if (!lendata.decl)
8336 c_strlen (arg, 1, &lendata);
8338 if (lendata.decl)
8340 if (EXPR_HAS_LOCATION (arg))
8341 loc = EXPR_LOCATION (arg);
8342 else if (loc == UNKNOWN_LOCATION)
8343 loc = input_location;
8344 warn_string_no_nul (loc, expr, "strlen", arg, lendata.decl);
8347 return NULL_TREE;
8351 /* Fold a call to __builtin_inf or __builtin_huge_val. */
8353 static tree
8354 fold_builtin_inf (location_t loc, tree type, int warn)
8356 REAL_VALUE_TYPE real;
8358 /* __builtin_inff is intended to be usable to define INFINITY on all
8359 targets. If an infinity is not available, INFINITY expands "to a
8360 positive constant of type float that overflows at translation
8361 time", footnote "In this case, using INFINITY will violate the
8362 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
8363 Thus we pedwarn to ensure this constraint violation is
8364 diagnosed. */
8365 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
8366 pedwarn (loc, 0, "target format does not support infinity");
8368 real_inf (&real);
8369 return build_real (type, real);
8372 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8373 NULL_TREE if no simplification can be made. */
8375 static tree
8376 fold_builtin_sincos (location_t loc,
8377 tree arg0, tree arg1, tree arg2)
8379 tree type;
8380 tree fndecl, call = NULL_TREE;
8382 if (!validate_arg (arg0, REAL_TYPE)
8383 || !validate_arg (arg1, POINTER_TYPE)
8384 || !validate_arg (arg2, POINTER_TYPE))
8385 return NULL_TREE;
8387 type = TREE_TYPE (arg0);
8389 /* Calculate the result when the argument is a constant. */
8390 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
8391 if (fn == END_BUILTINS)
8392 return NULL_TREE;
8394 /* Canonicalize sincos to cexpi. */
8395 if (TREE_CODE (arg0) == REAL_CST)
8397 tree complex_type = build_complex_type (type);
8398 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
8400 if (!call)
8402 if (!targetm.libc_has_function (function_c99_math_complex, type)
8403 || !builtin_decl_implicit_p (fn))
8404 return NULL_TREE;
8405 fndecl = builtin_decl_explicit (fn);
8406 call = build_call_expr_loc (loc, fndecl, 1, arg0);
8407 call = builtin_save_expr (call);
8410 tree ptype = build_pointer_type (type);
8411 arg1 = fold_convert (ptype, arg1);
8412 arg2 = fold_convert (ptype, arg2);
8413 return build2 (COMPOUND_EXPR, void_type_node,
8414 build2 (MODIFY_EXPR, void_type_node,
8415 build_fold_indirect_ref_loc (loc, arg1),
8416 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
8417 build2 (MODIFY_EXPR, void_type_node,
8418 build_fold_indirect_ref_loc (loc, arg2),
8419 fold_build1_loc (loc, REALPART_EXPR, type, call)));
8422 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8423 Return NULL_TREE if no simplification can be made. */
8425 static tree
8426 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8428 if (!validate_arg (arg1, POINTER_TYPE)
8429 || !validate_arg (arg2, POINTER_TYPE)
8430 || !validate_arg (len, INTEGER_TYPE))
8431 return NULL_TREE;
8433 /* If the LEN parameter is zero, return zero. */
8434 if (integer_zerop (len))
8435 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8436 arg1, arg2);
8438 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8439 if (operand_equal_p (arg1, arg2, 0))
8440 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8442 /* If len parameter is one, return an expression corresponding to
8443 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8444 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8446 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8447 tree cst_uchar_ptr_node
8448 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8450 tree ind1
8451 = fold_convert_loc (loc, integer_type_node,
8452 build1 (INDIRECT_REF, cst_uchar_node,
8453 fold_convert_loc (loc,
8454 cst_uchar_ptr_node,
8455 arg1)));
8456 tree ind2
8457 = fold_convert_loc (loc, integer_type_node,
8458 build1 (INDIRECT_REF, cst_uchar_node,
8459 fold_convert_loc (loc,
8460 cst_uchar_ptr_node,
8461 arg2)));
8462 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8465 return NULL_TREE;
8468 /* Fold a call to builtin isascii with argument ARG. */
8470 static tree
8471 fold_builtin_isascii (location_t loc, tree arg)
8473 if (!validate_arg (arg, INTEGER_TYPE))
8474 return NULL_TREE;
8475 else
8477 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8478 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8479 build_int_cst (integer_type_node,
8480 ~ (unsigned HOST_WIDE_INT) 0x7f));
8481 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8482 arg, integer_zero_node);
8486 /* Fold a call to builtin toascii with argument ARG. */
8488 static tree
8489 fold_builtin_toascii (location_t loc, tree arg)
8491 if (!validate_arg (arg, INTEGER_TYPE))
8492 return NULL_TREE;
8494 /* Transform toascii(c) -> (c & 0x7f). */
8495 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8496 build_int_cst (integer_type_node, 0x7f));
8499 /* Fold a call to builtin isdigit with argument ARG. */
8501 static tree
8502 fold_builtin_isdigit (location_t loc, tree arg)
8504 if (!validate_arg (arg, INTEGER_TYPE))
8505 return NULL_TREE;
8506 else
8508 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8509 /* According to the C standard, isdigit is unaffected by locale.
8510 However, it definitely is affected by the target character set. */
8511 unsigned HOST_WIDE_INT target_digit0
8512 = lang_hooks.to_target_charset ('0');
8514 if (target_digit0 == 0)
8515 return NULL_TREE;
8517 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8518 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8519 build_int_cst (unsigned_type_node, target_digit0));
8520 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8521 build_int_cst (unsigned_type_node, 9));
8525 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8527 static tree
8528 fold_builtin_fabs (location_t loc, tree arg, tree type)
8530 if (!validate_arg (arg, REAL_TYPE))
8531 return NULL_TREE;
8533 arg = fold_convert_loc (loc, type, arg);
8534 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8537 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8539 static tree
8540 fold_builtin_abs (location_t loc, tree arg, tree type)
8542 if (!validate_arg (arg, INTEGER_TYPE))
8543 return NULL_TREE;
8545 arg = fold_convert_loc (loc, type, arg);
8546 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8549 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8551 static tree
8552 fold_builtin_carg (location_t loc, tree arg, tree type)
8554 if (validate_arg (arg, COMPLEX_TYPE)
8555 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8557 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8559 if (atan2_fn)
8561 tree new_arg = builtin_save_expr (arg);
8562 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8563 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8564 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8568 return NULL_TREE;
8571 /* Fold a call to builtin frexp, we can assume the base is 2. */
8573 static tree
8574 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
8576 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8577 return NULL_TREE;
8579 STRIP_NOPS (arg0);
8581 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8582 return NULL_TREE;
8584 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8586 /* Proceed if a valid pointer type was passed in. */
8587 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
8589 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8590 tree frac, exp;
8592 switch (value->cl)
8594 case rvc_zero:
8595 /* For +-0, return (*exp = 0, +-0). */
8596 exp = integer_zero_node;
8597 frac = arg0;
8598 break;
8599 case rvc_nan:
8600 case rvc_inf:
8601 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8602 return omit_one_operand_loc (loc, rettype, arg0, arg1);
8603 case rvc_normal:
8605 /* Since the frexp function always expects base 2, and in
8606 GCC normalized significands are already in the range
8607 [0.5, 1.0), we have exactly what frexp wants. */
8608 REAL_VALUE_TYPE frac_rvt = *value;
8609 SET_REAL_EXP (&frac_rvt, 0);
8610 frac = build_real (rettype, frac_rvt);
8611 exp = build_int_cst (integer_type_node, REAL_EXP (value));
8613 break;
8614 default:
8615 gcc_unreachable ();
8618 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8619 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
8620 TREE_SIDE_EFFECTS (arg1) = 1;
8621 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
8624 return NULL_TREE;
8627 /* Fold a call to builtin modf. */
8629 static tree
8630 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
8632 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8633 return NULL_TREE;
8635 STRIP_NOPS (arg0);
8637 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8638 return NULL_TREE;
8640 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8642 /* Proceed if a valid pointer type was passed in. */
8643 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
8645 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8646 REAL_VALUE_TYPE trunc, frac;
8648 switch (value->cl)
8650 case rvc_nan:
8651 case rvc_zero:
8652 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
8653 trunc = frac = *value;
8654 break;
8655 case rvc_inf:
8656 /* For +-Inf, return (*arg1 = arg0, +-0). */
8657 frac = dconst0;
8658 frac.sign = value->sign;
8659 trunc = *value;
8660 break;
8661 case rvc_normal:
8662 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
8663 real_trunc (&trunc, VOIDmode, value);
8664 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
8665 /* If the original number was negative and already
8666 integral, then the fractional part is -0.0. */
8667 if (value->sign && frac.cl == rvc_zero)
8668 frac.sign = value->sign;
8669 break;
8672 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8673 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
8674 build_real (rettype, trunc));
8675 TREE_SIDE_EFFECTS (arg1) = 1;
8676 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
8677 build_real (rettype, frac));
8680 return NULL_TREE;
8683 /* Given a location LOC, an interclass builtin function decl FNDECL
8684 and its single argument ARG, return an folded expression computing
8685 the same, or NULL_TREE if we either couldn't or didn't want to fold
8686 (the latter happen if there's an RTL instruction available). */
8688 static tree
8689 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
8691 machine_mode mode;
8693 if (!validate_arg (arg, REAL_TYPE))
8694 return NULL_TREE;
8696 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
8697 return NULL_TREE;
8699 mode = TYPE_MODE (TREE_TYPE (arg));
8701 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
8703 /* If there is no optab, try generic code. */
8704 switch (DECL_FUNCTION_CODE (fndecl))
8706 tree result;
8708 CASE_FLT_FN (BUILT_IN_ISINF):
8710 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
8711 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8712 tree type = TREE_TYPE (arg);
8713 REAL_VALUE_TYPE r;
8714 char buf[128];
8716 if (is_ibm_extended)
8718 /* NaN and Inf are encoded in the high-order double value
8719 only. The low-order value is not significant. */
8720 type = double_type_node;
8721 mode = DFmode;
8722 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8724 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
8725 real_from_string (&r, buf);
8726 result = build_call_expr (isgr_fn, 2,
8727 fold_build1_loc (loc, ABS_EXPR, type, arg),
8728 build_real (type, r));
8729 return result;
8731 CASE_FLT_FN (BUILT_IN_FINITE):
8732 case BUILT_IN_ISFINITE:
8734 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
8735 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8736 tree type = TREE_TYPE (arg);
8737 REAL_VALUE_TYPE r;
8738 char buf[128];
8740 if (is_ibm_extended)
8742 /* NaN and Inf are encoded in the high-order double value
8743 only. The low-order value is not significant. */
8744 type = double_type_node;
8745 mode = DFmode;
8746 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8748 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
8749 real_from_string (&r, buf);
8750 result = build_call_expr (isle_fn, 2,
8751 fold_build1_loc (loc, ABS_EXPR, type, arg),
8752 build_real (type, r));
8753 /*result = fold_build2_loc (loc, UNGT_EXPR,
8754 TREE_TYPE (TREE_TYPE (fndecl)),
8755 fold_build1_loc (loc, ABS_EXPR, type, arg),
8756 build_real (type, r));
8757 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
8758 TREE_TYPE (TREE_TYPE (fndecl)),
8759 result);*/
8760 return result;
8762 case BUILT_IN_ISNORMAL:
8764 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
8765 islessequal(fabs(x),DBL_MAX). */
8766 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8767 tree type = TREE_TYPE (arg);
8768 tree orig_arg, max_exp, min_exp;
8769 machine_mode orig_mode = mode;
8770 REAL_VALUE_TYPE rmax, rmin;
8771 char buf[128];
8773 orig_arg = arg = builtin_save_expr (arg);
8774 if (is_ibm_extended)
8776 /* Use double to test the normal range of IBM extended
8777 precision. Emin for IBM extended precision is
8778 different to emin for IEEE double, being 53 higher
8779 since the low double exponent is at least 53 lower
8780 than the high double exponent. */
8781 type = double_type_node;
8782 mode = DFmode;
8783 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8785 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
8787 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
8788 real_from_string (&rmax, buf);
8789 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
8790 real_from_string (&rmin, buf);
8791 max_exp = build_real (type, rmax);
8792 min_exp = build_real (type, rmin);
8794 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
8795 if (is_ibm_extended)
8797 /* Testing the high end of the range is done just using
8798 the high double, using the same test as isfinite().
8799 For the subnormal end of the range we first test the
8800 high double, then if its magnitude is equal to the
8801 limit of 0x1p-969, we test whether the low double is
8802 non-zero and opposite sign to the high double. */
8803 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
8804 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8805 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
8806 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
8807 arg, min_exp);
8808 tree as_complex = build1 (VIEW_CONVERT_EXPR,
8809 complex_double_type_node, orig_arg);
8810 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
8811 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
8812 tree zero = build_real (type, dconst0);
8813 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
8814 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
8815 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
8816 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
8817 fold_build3 (COND_EXPR,
8818 integer_type_node,
8819 hilt, logt, lolt));
8820 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
8821 eq_min, ok_lo);
8822 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
8823 gt_min, eq_min);
8825 else
8827 tree const isge_fn
8828 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
8829 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
8831 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
8832 max_exp, min_exp);
8833 return result;
8835 default:
8836 break;
8839 return NULL_TREE;
8842 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8843 ARG is the argument for the call. */
8845 static tree
8846 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
8848 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8850 if (!validate_arg (arg, REAL_TYPE))
8851 return NULL_TREE;
8853 switch (builtin_index)
8855 case BUILT_IN_ISINF:
8856 if (tree_expr_infinite_p (arg))
8857 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8858 if (!tree_expr_maybe_infinite_p (arg))
8859 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8860 return NULL_TREE;
8862 case BUILT_IN_ISINF_SIGN:
8864 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
8865 /* In a boolean context, GCC will fold the inner COND_EXPR to
8866 1. So e.g. "if (isinf_sign(x))" would be folded to just
8867 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
8868 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
8869 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
8870 tree tmp = NULL_TREE;
8872 arg = builtin_save_expr (arg);
8874 if (signbit_fn && isinf_fn)
8876 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
8877 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
8879 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8880 signbit_call, integer_zero_node);
8881 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8882 isinf_call, integer_zero_node);
8884 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
8885 integer_minus_one_node, integer_one_node);
8886 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8887 isinf_call, tmp,
8888 integer_zero_node);
8891 return tmp;
8894 case BUILT_IN_ISFINITE:
8895 if (tree_expr_finite_p (arg))
8896 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8897 if (tree_expr_nan_p (arg) || tree_expr_infinite_p (arg))
8898 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8899 return NULL_TREE;
8901 case BUILT_IN_ISNAN:
8902 if (tree_expr_nan_p (arg))
8903 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8904 if (!tree_expr_maybe_nan_p (arg))
8905 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8908 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
8909 if (is_ibm_extended)
8911 /* NaN and Inf are encoded in the high-order double value
8912 only. The low-order value is not significant. */
8913 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
8916 arg = builtin_save_expr (arg);
8917 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
8919 default:
8920 gcc_unreachable ();
8924 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
8925 This builtin will generate code to return the appropriate floating
8926 point classification depending on the value of the floating point
8927 number passed in. The possible return values must be supplied as
8928 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
8929 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
8930 one floating point argument which is "type generic". */
8932 static tree
8933 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
8935 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
8936 arg, type, res, tmp;
8937 machine_mode mode;
8938 REAL_VALUE_TYPE r;
8939 char buf[128];
8941 /* Verify the required arguments in the original call. */
8942 if (nargs != 6
8943 || !validate_arg (args[0], INTEGER_TYPE)
8944 || !validate_arg (args[1], INTEGER_TYPE)
8945 || !validate_arg (args[2], INTEGER_TYPE)
8946 || !validate_arg (args[3], INTEGER_TYPE)
8947 || !validate_arg (args[4], INTEGER_TYPE)
8948 || !validate_arg (args[5], REAL_TYPE))
8949 return NULL_TREE;
8951 fp_nan = args[0];
8952 fp_infinite = args[1];
8953 fp_normal = args[2];
8954 fp_subnormal = args[3];
8955 fp_zero = args[4];
8956 arg = args[5];
8957 type = TREE_TYPE (arg);
8958 mode = TYPE_MODE (type);
8959 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
8961 /* fpclassify(x) ->
8962 isnan(x) ? FP_NAN :
8963 (fabs(x) == Inf ? FP_INFINITE :
8964 (fabs(x) >= DBL_MIN ? FP_NORMAL :
8965 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
8967 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8968 build_real (type, dconst0));
8969 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8970 tmp, fp_zero, fp_subnormal);
8972 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
8973 real_from_string (&r, buf);
8974 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
8975 arg, build_real (type, r));
8976 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
8978 if (tree_expr_maybe_infinite_p (arg))
8980 real_inf (&r);
8981 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8982 build_real (type, r));
8983 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
8984 fp_infinite, res);
8987 if (tree_expr_maybe_nan_p (arg))
8989 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
8990 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
8993 return res;
8996 /* Fold a call to an unordered comparison function such as
8997 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8998 being called and ARG0 and ARG1 are the arguments for the call.
8999 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9000 the opposite of the desired result. UNORDERED_CODE is used
9001 for modes that can hold NaNs and ORDERED_CODE is used for
9002 the rest. */
9004 static tree
9005 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9006 enum tree_code unordered_code,
9007 enum tree_code ordered_code)
9009 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9010 enum tree_code code;
9011 tree type0, type1;
9012 enum tree_code code0, code1;
9013 tree cmp_type = NULL_TREE;
9015 type0 = TREE_TYPE (arg0);
9016 type1 = TREE_TYPE (arg1);
9018 code0 = TREE_CODE (type0);
9019 code1 = TREE_CODE (type1);
9021 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9022 /* Choose the wider of two real types. */
9023 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9024 ? type0 : type1;
9025 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9026 cmp_type = type0;
9027 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9028 cmp_type = type1;
9030 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9031 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9033 if (unordered_code == UNORDERED_EXPR)
9035 if (tree_expr_nan_p (arg0) || tree_expr_nan_p (arg1))
9036 return omit_two_operands_loc (loc, type, integer_one_node, arg0, arg1);
9037 if (!tree_expr_maybe_nan_p (arg0) && !tree_expr_maybe_nan_p (arg1))
9038 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9039 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9042 code = (tree_expr_maybe_nan_p (arg0) || tree_expr_maybe_nan_p (arg1))
9043 ? unordered_code : ordered_code;
9044 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9045 fold_build2_loc (loc, code, type, arg0, arg1));
9048 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9049 arithmetics if it can never overflow, or into internal functions that
9050 return both result of arithmetics and overflowed boolean flag in
9051 a complex integer result, or some other check for overflow.
9052 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
9053 checking part of that. */
9055 static tree
9056 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
9057 tree arg0, tree arg1, tree arg2)
9059 enum internal_fn ifn = IFN_LAST;
9060 /* The code of the expression corresponding to the built-in. */
9061 enum tree_code opcode = ERROR_MARK;
9062 bool ovf_only = false;
9064 switch (fcode)
9066 case BUILT_IN_ADD_OVERFLOW_P:
9067 ovf_only = true;
9068 /* FALLTHRU */
9069 case BUILT_IN_ADD_OVERFLOW:
9070 case BUILT_IN_SADD_OVERFLOW:
9071 case BUILT_IN_SADDL_OVERFLOW:
9072 case BUILT_IN_SADDLL_OVERFLOW:
9073 case BUILT_IN_UADD_OVERFLOW:
9074 case BUILT_IN_UADDL_OVERFLOW:
9075 case BUILT_IN_UADDLL_OVERFLOW:
9076 opcode = PLUS_EXPR;
9077 ifn = IFN_ADD_OVERFLOW;
9078 break;
9079 case BUILT_IN_SUB_OVERFLOW_P:
9080 ovf_only = true;
9081 /* FALLTHRU */
9082 case BUILT_IN_SUB_OVERFLOW:
9083 case BUILT_IN_SSUB_OVERFLOW:
9084 case BUILT_IN_SSUBL_OVERFLOW:
9085 case BUILT_IN_SSUBLL_OVERFLOW:
9086 case BUILT_IN_USUB_OVERFLOW:
9087 case BUILT_IN_USUBL_OVERFLOW:
9088 case BUILT_IN_USUBLL_OVERFLOW:
9089 opcode = MINUS_EXPR;
9090 ifn = IFN_SUB_OVERFLOW;
9091 break;
9092 case BUILT_IN_MUL_OVERFLOW_P:
9093 ovf_only = true;
9094 /* FALLTHRU */
9095 case BUILT_IN_MUL_OVERFLOW:
9096 case BUILT_IN_SMUL_OVERFLOW:
9097 case BUILT_IN_SMULL_OVERFLOW:
9098 case BUILT_IN_SMULLL_OVERFLOW:
9099 case BUILT_IN_UMUL_OVERFLOW:
9100 case BUILT_IN_UMULL_OVERFLOW:
9101 case BUILT_IN_UMULLL_OVERFLOW:
9102 opcode = MULT_EXPR;
9103 ifn = IFN_MUL_OVERFLOW;
9104 break;
9105 default:
9106 gcc_unreachable ();
9109 /* For the "generic" overloads, the first two arguments can have different
9110 types and the last argument determines the target type to use to check
9111 for overflow. The arguments of the other overloads all have the same
9112 type. */
9113 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
9115 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
9116 arguments are constant, attempt to fold the built-in call into a constant
9117 expression indicating whether or not it detected an overflow. */
9118 if (ovf_only
9119 && TREE_CODE (arg0) == INTEGER_CST
9120 && TREE_CODE (arg1) == INTEGER_CST)
9121 /* Perform the computation in the target type and check for overflow. */
9122 return omit_one_operand_loc (loc, boolean_type_node,
9123 arith_overflowed_p (opcode, type, arg0, arg1)
9124 ? boolean_true_node : boolean_false_node,
9125 arg2);
9127 tree intres, ovfres;
9128 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9130 intres = fold_binary_loc (loc, opcode, type,
9131 fold_convert_loc (loc, type, arg0),
9132 fold_convert_loc (loc, type, arg1));
9133 if (TREE_OVERFLOW (intres))
9134 intres = drop_tree_overflow (intres);
9135 ovfres = (arith_overflowed_p (opcode, type, arg0, arg1)
9136 ? boolean_true_node : boolean_false_node);
9138 else
9140 tree ctype = build_complex_type (type);
9141 tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
9142 arg0, arg1);
9143 tree tgt = save_expr (call);
9144 intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9145 ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9146 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
9149 if (ovf_only)
9150 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
9152 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
9153 tree store
9154 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
9155 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
9158 /* Fold a call to __builtin_FILE to a constant string. */
9160 static inline tree
9161 fold_builtin_FILE (location_t loc)
9163 if (const char *fname = LOCATION_FILE (loc))
9165 /* The documentation says this builtin is equivalent to the preprocessor
9166 __FILE__ macro so it appears appropriate to use the same file prefix
9167 mappings. */
9168 fname = remap_macro_filename (fname);
9169 return build_string_literal (strlen (fname) + 1, fname);
9172 return build_string_literal (1, "");
9175 /* Fold a call to __builtin_FUNCTION to a constant string. */
9177 static inline tree
9178 fold_builtin_FUNCTION ()
9180 const char *name = "";
9182 if (current_function_decl)
9183 name = lang_hooks.decl_printable_name (current_function_decl, 0);
9185 return build_string_literal (strlen (name) + 1, name);
9188 /* Fold a call to __builtin_LINE to an integer constant. */
9190 static inline tree
9191 fold_builtin_LINE (location_t loc, tree type)
9193 return build_int_cst (type, LOCATION_LINE (loc));
9196 /* Fold a call to built-in function FNDECL with 0 arguments.
9197 This function returns NULL_TREE if no simplification was possible. */
9199 static tree
9200 fold_builtin_0 (location_t loc, tree fndecl)
9202 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9203 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9204 switch (fcode)
9206 case BUILT_IN_FILE:
9207 return fold_builtin_FILE (loc);
9209 case BUILT_IN_FUNCTION:
9210 return fold_builtin_FUNCTION ();
9212 case BUILT_IN_LINE:
9213 return fold_builtin_LINE (loc, type);
9215 CASE_FLT_FN (BUILT_IN_INF):
9216 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
9217 case BUILT_IN_INFD32:
9218 case BUILT_IN_INFD64:
9219 case BUILT_IN_INFD128:
9220 return fold_builtin_inf (loc, type, true);
9222 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9223 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
9224 return fold_builtin_inf (loc, type, false);
9226 case BUILT_IN_CLASSIFY_TYPE:
9227 return fold_builtin_classify_type (NULL_TREE);
9229 default:
9230 break;
9232 return NULL_TREE;
9235 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9236 This function returns NULL_TREE if no simplification was possible. */
9238 static tree
9239 fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
9241 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9242 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9244 if (TREE_CODE (arg0) == ERROR_MARK)
9245 return NULL_TREE;
9247 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
9248 return ret;
9250 switch (fcode)
9252 case BUILT_IN_CONSTANT_P:
9254 tree val = fold_builtin_constant_p (arg0);
9256 /* Gimplification will pull the CALL_EXPR for the builtin out of
9257 an if condition. When not optimizing, we'll not CSE it back.
9258 To avoid link error types of regressions, return false now. */
9259 if (!val && !optimize)
9260 val = integer_zero_node;
9262 return val;
9265 case BUILT_IN_CLASSIFY_TYPE:
9266 return fold_builtin_classify_type (arg0);
9268 case BUILT_IN_STRLEN:
9269 return fold_builtin_strlen (loc, expr, type, arg0);
9271 CASE_FLT_FN (BUILT_IN_FABS):
9272 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
9273 case BUILT_IN_FABSD32:
9274 case BUILT_IN_FABSD64:
9275 case BUILT_IN_FABSD128:
9276 return fold_builtin_fabs (loc, arg0, type);
9278 case BUILT_IN_ABS:
9279 case BUILT_IN_LABS:
9280 case BUILT_IN_LLABS:
9281 case BUILT_IN_IMAXABS:
9282 return fold_builtin_abs (loc, arg0, type);
9284 CASE_FLT_FN (BUILT_IN_CONJ):
9285 if (validate_arg (arg0, COMPLEX_TYPE)
9286 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9287 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9288 break;
9290 CASE_FLT_FN (BUILT_IN_CREAL):
9291 if (validate_arg (arg0, COMPLEX_TYPE)
9292 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9293 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9294 break;
9296 CASE_FLT_FN (BUILT_IN_CIMAG):
9297 if (validate_arg (arg0, COMPLEX_TYPE)
9298 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9299 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9300 break;
9302 CASE_FLT_FN (BUILT_IN_CARG):
9303 return fold_builtin_carg (loc, arg0, type);
9305 case BUILT_IN_ISASCII:
9306 return fold_builtin_isascii (loc, arg0);
9308 case BUILT_IN_TOASCII:
9309 return fold_builtin_toascii (loc, arg0);
9311 case BUILT_IN_ISDIGIT:
9312 return fold_builtin_isdigit (loc, arg0);
9314 CASE_FLT_FN (BUILT_IN_FINITE):
9315 case BUILT_IN_FINITED32:
9316 case BUILT_IN_FINITED64:
9317 case BUILT_IN_FINITED128:
9318 case BUILT_IN_ISFINITE:
9320 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
9321 if (ret)
9322 return ret;
9323 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9326 CASE_FLT_FN (BUILT_IN_ISINF):
9327 case BUILT_IN_ISINFD32:
9328 case BUILT_IN_ISINFD64:
9329 case BUILT_IN_ISINFD128:
9331 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
9332 if (ret)
9333 return ret;
9334 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9337 case BUILT_IN_ISNORMAL:
9338 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9340 case BUILT_IN_ISINF_SIGN:
9341 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
9343 CASE_FLT_FN (BUILT_IN_ISNAN):
9344 case BUILT_IN_ISNAND32:
9345 case BUILT_IN_ISNAND64:
9346 case BUILT_IN_ISNAND128:
9347 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
9349 case BUILT_IN_FREE:
9350 if (integer_zerop (arg0))
9351 return build_empty_stmt (loc);
9352 break;
9354 default:
9355 break;
9358 return NULL_TREE;
9362 /* Folds a call EXPR (which may be null) to built-in function FNDECL
9363 with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
9364 if no simplification was possible. */
9366 static tree
9367 fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
9369 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9370 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9372 if (TREE_CODE (arg0) == ERROR_MARK
9373 || TREE_CODE (arg1) == ERROR_MARK)
9374 return NULL_TREE;
9376 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
9377 return ret;
9379 switch (fcode)
9381 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
9382 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
9383 if (validate_arg (arg0, REAL_TYPE)
9384 && validate_arg (arg1, POINTER_TYPE))
9385 return do_mpfr_lgamma_r (arg0, arg1, type);
9386 break;
9388 CASE_FLT_FN (BUILT_IN_FREXP):
9389 return fold_builtin_frexp (loc, arg0, arg1, type);
9391 CASE_FLT_FN (BUILT_IN_MODF):
9392 return fold_builtin_modf (loc, arg0, arg1, type);
9394 case BUILT_IN_STRSPN:
9395 return fold_builtin_strspn (loc, expr, arg0, arg1);
9397 case BUILT_IN_STRCSPN:
9398 return fold_builtin_strcspn (loc, expr, arg0, arg1);
9400 case BUILT_IN_STRPBRK:
9401 return fold_builtin_strpbrk (loc, expr, arg0, arg1, type);
9403 case BUILT_IN_EXPECT:
9404 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE);
9406 case BUILT_IN_ISGREATER:
9407 return fold_builtin_unordered_cmp (loc, fndecl,
9408 arg0, arg1, UNLE_EXPR, LE_EXPR);
9409 case BUILT_IN_ISGREATEREQUAL:
9410 return fold_builtin_unordered_cmp (loc, fndecl,
9411 arg0, arg1, UNLT_EXPR, LT_EXPR);
9412 case BUILT_IN_ISLESS:
9413 return fold_builtin_unordered_cmp (loc, fndecl,
9414 arg0, arg1, UNGE_EXPR, GE_EXPR);
9415 case BUILT_IN_ISLESSEQUAL:
9416 return fold_builtin_unordered_cmp (loc, fndecl,
9417 arg0, arg1, UNGT_EXPR, GT_EXPR);
9418 case BUILT_IN_ISLESSGREATER:
9419 return fold_builtin_unordered_cmp (loc, fndecl,
9420 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
9421 case BUILT_IN_ISUNORDERED:
9422 return fold_builtin_unordered_cmp (loc, fndecl,
9423 arg0, arg1, UNORDERED_EXPR,
9424 NOP_EXPR);
9426 /* We do the folding for va_start in the expander. */
9427 case BUILT_IN_VA_START:
9428 break;
9430 case BUILT_IN_OBJECT_SIZE:
9431 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
9432 return fold_builtin_object_size (arg0, arg1, fcode);
9434 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
9435 return fold_builtin_atomic_always_lock_free (arg0, arg1);
9437 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
9438 return fold_builtin_atomic_is_lock_free (arg0, arg1);
9440 default:
9441 break;
9443 return NULL_TREE;
9446 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
9447 and ARG2.
9448 This function returns NULL_TREE if no simplification was possible. */
9450 static tree
9451 fold_builtin_3 (location_t loc, tree fndecl,
9452 tree arg0, tree arg1, tree arg2)
9454 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9455 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9457 if (TREE_CODE (arg0) == ERROR_MARK
9458 || TREE_CODE (arg1) == ERROR_MARK
9459 || TREE_CODE (arg2) == ERROR_MARK)
9460 return NULL_TREE;
9462 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
9463 arg0, arg1, arg2))
9464 return ret;
9466 switch (fcode)
9469 CASE_FLT_FN (BUILT_IN_SINCOS):
9470 return fold_builtin_sincos (loc, arg0, arg1, arg2);
9472 CASE_FLT_FN (BUILT_IN_REMQUO):
9473 if (validate_arg (arg0, REAL_TYPE)
9474 && validate_arg (arg1, REAL_TYPE)
9475 && validate_arg (arg2, POINTER_TYPE))
9476 return do_mpfr_remquo (arg0, arg1, arg2);
9477 break;
9479 case BUILT_IN_MEMCMP:
9480 return fold_builtin_memcmp (loc, arg0, arg1, arg2);
9482 case BUILT_IN_EXPECT:
9483 return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE);
9485 case BUILT_IN_EXPECT_WITH_PROBABILITY:
9486 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2);
9488 case BUILT_IN_ADD_OVERFLOW:
9489 case BUILT_IN_SUB_OVERFLOW:
9490 case BUILT_IN_MUL_OVERFLOW:
9491 case BUILT_IN_ADD_OVERFLOW_P:
9492 case BUILT_IN_SUB_OVERFLOW_P:
9493 case BUILT_IN_MUL_OVERFLOW_P:
9494 case BUILT_IN_SADD_OVERFLOW:
9495 case BUILT_IN_SADDL_OVERFLOW:
9496 case BUILT_IN_SADDLL_OVERFLOW:
9497 case BUILT_IN_SSUB_OVERFLOW:
9498 case BUILT_IN_SSUBL_OVERFLOW:
9499 case BUILT_IN_SSUBLL_OVERFLOW:
9500 case BUILT_IN_SMUL_OVERFLOW:
9501 case BUILT_IN_SMULL_OVERFLOW:
9502 case BUILT_IN_SMULLL_OVERFLOW:
9503 case BUILT_IN_UADD_OVERFLOW:
9504 case BUILT_IN_UADDL_OVERFLOW:
9505 case BUILT_IN_UADDLL_OVERFLOW:
9506 case BUILT_IN_USUB_OVERFLOW:
9507 case BUILT_IN_USUBL_OVERFLOW:
9508 case BUILT_IN_USUBLL_OVERFLOW:
9509 case BUILT_IN_UMUL_OVERFLOW:
9510 case BUILT_IN_UMULL_OVERFLOW:
9511 case BUILT_IN_UMULLL_OVERFLOW:
9512 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
9514 default:
9515 break;
9517 return NULL_TREE;
9520 /* Folds a call EXPR (which may be null) to built-in function FNDECL.
9521 ARGS is an array of NARGS arguments. IGNORE is true if the result
9522 of the function call is ignored. This function returns NULL_TREE
9523 if no simplification was possible. */
9525 static tree
9526 fold_builtin_n (location_t loc, tree expr, tree fndecl, tree *args,
9527 int nargs, bool)
9529 tree ret = NULL_TREE;
9531 switch (nargs)
9533 case 0:
9534 ret = fold_builtin_0 (loc, fndecl);
9535 break;
9536 case 1:
9537 ret = fold_builtin_1 (loc, expr, fndecl, args[0]);
9538 break;
9539 case 2:
9540 ret = fold_builtin_2 (loc, expr, fndecl, args[0], args[1]);
9541 break;
9542 case 3:
9543 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
9544 break;
9545 default:
9546 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
9547 break;
9549 if (ret)
9551 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
9552 SET_EXPR_LOCATION (ret, loc);
9553 return ret;
9555 return NULL_TREE;
9558 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
9559 list ARGS along with N new arguments in NEWARGS. SKIP is the number
9560 of arguments in ARGS to be omitted. OLDNARGS is the number of
9561 elements in ARGS. */
9563 static tree
9564 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
9565 int skip, tree fndecl, int n, va_list newargs)
9567 int nargs = oldnargs - skip + n;
9568 tree *buffer;
9570 if (n > 0)
9572 int i, j;
9574 buffer = XALLOCAVEC (tree, nargs);
9575 for (i = 0; i < n; i++)
9576 buffer[i] = va_arg (newargs, tree);
9577 for (j = skip; j < oldnargs; j++, i++)
9578 buffer[i] = args[j];
9580 else
9581 buffer = args + skip;
9583 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
9586 /* Return true if FNDECL shouldn't be folded right now.
9587 If a built-in function has an inline attribute always_inline
9588 wrapper, defer folding it after always_inline functions have
9589 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
9590 might not be performed. */
9592 bool
9593 avoid_folding_inline_builtin (tree fndecl)
9595 return (DECL_DECLARED_INLINE_P (fndecl)
9596 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
9597 && cfun
9598 && !cfun->always_inline_functions_inlined
9599 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
9602 /* A wrapper function for builtin folding that prevents warnings for
9603 "statement without effect" and the like, caused by removing the
9604 call node earlier than the warning is generated. */
9606 tree
9607 fold_call_expr (location_t loc, tree exp, bool ignore)
9609 tree ret = NULL_TREE;
9610 tree fndecl = get_callee_fndecl (exp);
9611 if (fndecl && fndecl_built_in_p (fndecl)
9612 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
9613 yet. Defer folding until we see all the arguments
9614 (after inlining). */
9615 && !CALL_EXPR_VA_ARG_PACK (exp))
9617 int nargs = call_expr_nargs (exp);
9619 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
9620 instead last argument is __builtin_va_arg_pack (). Defer folding
9621 even in that case, until arguments are finalized. */
9622 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
9624 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
9625 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
9626 return NULL_TREE;
9629 if (avoid_folding_inline_builtin (fndecl))
9630 return NULL_TREE;
9632 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9633 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
9634 CALL_EXPR_ARGP (exp), ignore);
9635 else
9637 tree *args = CALL_EXPR_ARGP (exp);
9638 ret = fold_builtin_n (loc, exp, fndecl, args, nargs, ignore);
9639 if (ret)
9640 return ret;
9643 return NULL_TREE;
9646 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
9647 N arguments are passed in the array ARGARRAY. Return a folded
9648 expression or NULL_TREE if no simplification was possible. */
9650 tree
9651 fold_builtin_call_array (location_t loc, tree,
9652 tree fn,
9653 int n,
9654 tree *argarray)
9656 if (TREE_CODE (fn) != ADDR_EXPR)
9657 return NULL_TREE;
9659 tree fndecl = TREE_OPERAND (fn, 0);
9660 if (TREE_CODE (fndecl) == FUNCTION_DECL
9661 && fndecl_built_in_p (fndecl))
9663 /* If last argument is __builtin_va_arg_pack (), arguments to this
9664 function are not finalized yet. Defer folding until they are. */
9665 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
9667 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
9668 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
9669 return NULL_TREE;
9671 if (avoid_folding_inline_builtin (fndecl))
9672 return NULL_TREE;
9673 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9674 return targetm.fold_builtin (fndecl, n, argarray, false);
9675 else
9676 return fold_builtin_n (loc, NULL_TREE, fndecl, argarray, n, false);
9679 return NULL_TREE;
9682 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
9683 along with N new arguments specified as the "..." parameters. SKIP
9684 is the number of arguments in EXP to be omitted. This function is used
9685 to do varargs-to-varargs transformations. */
9687 static tree
9688 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
9690 va_list ap;
9691 tree t;
9693 va_start (ap, n);
9694 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
9695 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
9696 va_end (ap);
9698 return t;
9701 /* Validate a single argument ARG against a tree code CODE representing
9702 a type. Return true when argument is valid. */
9704 static bool
9705 validate_arg (const_tree arg, enum tree_code code)
9707 if (!arg)
9708 return false;
9709 else if (code == POINTER_TYPE)
9710 return POINTER_TYPE_P (TREE_TYPE (arg));
9711 else if (code == INTEGER_TYPE)
9712 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
9713 return code == TREE_CODE (TREE_TYPE (arg));
9716 /* This function validates the types of a function call argument list
9717 against a specified list of tree_codes. If the last specifier is a 0,
9718 that represents an ellipses, otherwise the last specifier must be a
9719 VOID_TYPE.
9721 This is the GIMPLE version of validate_arglist. Eventually we want to
9722 completely convert builtins.cc to work from GIMPLEs and the tree based
9723 validate_arglist will then be removed. */
9725 bool
9726 validate_gimple_arglist (const gcall *call, ...)
9728 enum tree_code code;
9729 bool res = 0;
9730 va_list ap;
9731 const_tree arg;
9732 size_t i;
9734 va_start (ap, call);
9735 i = 0;
9739 code = (enum tree_code) va_arg (ap, int);
9740 switch (code)
9742 case 0:
9743 /* This signifies an ellipses, any further arguments are all ok. */
9744 res = true;
9745 goto end;
9746 case VOID_TYPE:
9747 /* This signifies an endlink, if no arguments remain, return
9748 true, otherwise return false. */
9749 res = (i == gimple_call_num_args (call));
9750 goto end;
9751 default:
9752 /* If no parameters remain or the parameter's code does not
9753 match the specified code, return false. Otherwise continue
9754 checking any remaining arguments. */
9755 arg = gimple_call_arg (call, i++);
9756 if (!validate_arg (arg, code))
9757 goto end;
9758 break;
9761 while (1);
9763 /* We need gotos here since we can only have one VA_CLOSE in a
9764 function. */
9765 end: ;
9766 va_end (ap);
9768 return res;
9771 /* Default target-specific builtin expander that does nothing. */
9774 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9775 rtx target ATTRIBUTE_UNUSED,
9776 rtx subtarget ATTRIBUTE_UNUSED,
9777 machine_mode mode ATTRIBUTE_UNUSED,
9778 int ignore ATTRIBUTE_UNUSED)
9780 return NULL_RTX;
9783 /* Returns true is EXP represents data that would potentially reside
9784 in a readonly section. */
9786 bool
9787 readonly_data_expr (tree exp)
9789 STRIP_NOPS (exp);
9791 if (TREE_CODE (exp) != ADDR_EXPR)
9792 return false;
9794 exp = get_base_address (TREE_OPERAND (exp, 0));
9795 if (!exp)
9796 return false;
9798 /* Make sure we call decl_readonly_section only for trees it
9799 can handle (since it returns true for everything it doesn't
9800 understand). */
9801 if (TREE_CODE (exp) == STRING_CST
9802 || TREE_CODE (exp) == CONSTRUCTOR
9803 || (VAR_P (exp) && TREE_STATIC (exp)))
9804 return decl_readonly_section (exp, 0);
9805 else
9806 return false;
9809 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
9810 to the call, and TYPE is its return type.
9812 Return NULL_TREE if no simplification was possible, otherwise return the
9813 simplified form of the call as a tree.
9815 The simplified form may be a constant or other expression which
9816 computes the same value, but in a more efficient manner (including
9817 calls to other builtin functions).
9819 The call may contain arguments which need to be evaluated, but
9820 which are not useful to determine the result of the call. In
9821 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9822 COMPOUND_EXPR will be an argument which must be evaluated.
9823 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9824 COMPOUND_EXPR in the chain will contain the tree for the simplified
9825 form of the builtin function call. */
9827 static tree
9828 fold_builtin_strpbrk (location_t loc, tree, tree s1, tree s2, tree type)
9830 if (!validate_arg (s1, POINTER_TYPE)
9831 || !validate_arg (s2, POINTER_TYPE))
9832 return NULL_TREE;
9834 tree fn;
9835 const char *p1, *p2;
9837 p2 = c_getstr (s2);
9838 if (p2 == NULL)
9839 return NULL_TREE;
9841 p1 = c_getstr (s1);
9842 if (p1 != NULL)
9844 const char *r = strpbrk (p1, p2);
9845 tree tem;
9847 if (r == NULL)
9848 return build_int_cst (TREE_TYPE (s1), 0);
9850 /* Return an offset into the constant string argument. */
9851 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
9852 return fold_convert_loc (loc, type, tem);
9855 if (p2[0] == '\0')
9856 /* strpbrk(x, "") == NULL.
9857 Evaluate and ignore s1 in case it had side-effects. */
9858 return omit_one_operand_loc (loc, type, integer_zero_node, s1);
9860 if (p2[1] != '\0')
9861 return NULL_TREE; /* Really call strpbrk. */
9863 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
9864 if (!fn)
9865 return NULL_TREE;
9867 /* New argument list transforming strpbrk(s1, s2) to
9868 strchr(s1, s2[0]). */
9869 return build_call_expr_loc (loc, fn, 2, s1,
9870 build_int_cst (integer_type_node, p2[0]));
9873 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
9874 to the call.
9876 Return NULL_TREE if no simplification was possible, otherwise return the
9877 simplified form of the call as a tree.
9879 The simplified form may be a constant or other expression which
9880 computes the same value, but in a more efficient manner (including
9881 calls to other builtin functions).
9883 The call may contain arguments which need to be evaluated, but
9884 which are not useful to determine the result of the call. In
9885 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9886 COMPOUND_EXPR will be an argument which must be evaluated.
9887 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9888 COMPOUND_EXPR in the chain will contain the tree for the simplified
9889 form of the builtin function call. */
9891 static tree
9892 fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2)
9894 if (!validate_arg (s1, POINTER_TYPE)
9895 || !validate_arg (s2, POINTER_TYPE))
9896 return NULL_TREE;
9898 if (!check_nul_terminated_array (expr, s1)
9899 || !check_nul_terminated_array (expr, s2))
9900 return NULL_TREE;
9902 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9904 /* If either argument is "", return NULL_TREE. */
9905 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9906 /* Evaluate and ignore both arguments in case either one has
9907 side-effects. */
9908 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
9909 s1, s2);
9910 return NULL_TREE;
9913 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
9914 to the call.
9916 Return NULL_TREE if no simplification was possible, otherwise return the
9917 simplified form of the call as a tree.
9919 The simplified form may be a constant or other expression which
9920 computes the same value, but in a more efficient manner (including
9921 calls to other builtin functions).
9923 The call may contain arguments which need to be evaluated, but
9924 which are not useful to determine the result of the call. In
9925 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9926 COMPOUND_EXPR will be an argument which must be evaluated.
9927 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9928 COMPOUND_EXPR in the chain will contain the tree for the simplified
9929 form of the builtin function call. */
9931 static tree
9932 fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2)
9934 if (!validate_arg (s1, POINTER_TYPE)
9935 || !validate_arg (s2, POINTER_TYPE))
9936 return NULL_TREE;
9938 if (!check_nul_terminated_array (expr, s1)
9939 || !check_nul_terminated_array (expr, s2))
9940 return NULL_TREE;
9942 /* If the first argument is "", return NULL_TREE. */
9943 const char *p1 = c_getstr (s1);
9944 if (p1 && *p1 == '\0')
9946 /* Evaluate and ignore argument s2 in case it has
9947 side-effects. */
9948 return omit_one_operand_loc (loc, size_type_node,
9949 size_zero_node, s2);
9952 /* If the second argument is "", return __builtin_strlen(s1). */
9953 const char *p2 = c_getstr (s2);
9954 if (p2 && *p2 == '\0')
9956 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
9958 /* If the replacement _DECL isn't initialized, don't do the
9959 transformation. */
9960 if (!fn)
9961 return NULL_TREE;
9963 return build_call_expr_loc (loc, fn, 1, s1);
9965 return NULL_TREE;
9968 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
9969 produced. False otherwise. This is done so that we don't output the error
9970 or warning twice or three times. */
9972 bool
9973 fold_builtin_next_arg (tree exp, bool va_start_p)
9975 tree fntype = TREE_TYPE (current_function_decl);
9976 int nargs = call_expr_nargs (exp);
9977 tree arg;
9978 /* There is good chance the current input_location points inside the
9979 definition of the va_start macro (perhaps on the token for
9980 builtin) in a system header, so warnings will not be emitted.
9981 Use the location in real source code. */
9982 location_t current_location =
9983 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
9984 NULL);
9986 if (!stdarg_p (fntype))
9988 error ("%<va_start%> used in function with fixed arguments");
9989 return true;
9992 if (va_start_p)
9994 if (va_start_p && (nargs != 2))
9996 error ("wrong number of arguments to function %<va_start%>");
9997 return true;
9999 arg = CALL_EXPR_ARG (exp, 1);
10001 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10002 when we checked the arguments and if needed issued a warning. */
10003 else
10005 if (nargs == 0)
10007 /* Evidently an out of date version of <stdarg.h>; can't validate
10008 va_start's second argument, but can still work as intended. */
10009 warning_at (current_location,
10010 OPT_Wvarargs,
10011 "%<__builtin_next_arg%> called without an argument");
10012 return true;
10014 else if (nargs > 1)
10016 error ("wrong number of arguments to function %<__builtin_next_arg%>");
10017 return true;
10019 arg = CALL_EXPR_ARG (exp, 0);
10022 if (TREE_CODE (arg) == SSA_NAME
10023 && SSA_NAME_VAR (arg))
10024 arg = SSA_NAME_VAR (arg);
10026 /* We destructively modify the call to be __builtin_va_start (ap, 0)
10027 or __builtin_next_arg (0) the first time we see it, after checking
10028 the arguments and if needed issuing a warning. */
10029 if (!integer_zerop (arg))
10031 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10033 /* Strip off all nops for the sake of the comparison. This
10034 is not quite the same as STRIP_NOPS. It does more.
10035 We must also strip off INDIRECT_EXPR for C++ reference
10036 parameters. */
10037 while (CONVERT_EXPR_P (arg)
10038 || TREE_CODE (arg) == INDIRECT_REF)
10039 arg = TREE_OPERAND (arg, 0);
10040 if (arg != last_parm)
10042 /* FIXME: Sometimes with the tree optimizers we can get the
10043 not the last argument even though the user used the last
10044 argument. We just warn and set the arg to be the last
10045 argument so that we will get wrong-code because of
10046 it. */
10047 warning_at (current_location,
10048 OPT_Wvarargs,
10049 "second parameter of %<va_start%> not last named argument");
10052 /* Undefined by C99 7.15.1.4p4 (va_start):
10053 "If the parameter parmN is declared with the register storage
10054 class, with a function or array type, or with a type that is
10055 not compatible with the type that results after application of
10056 the default argument promotions, the behavior is undefined."
10058 else if (DECL_REGISTER (arg))
10060 warning_at (current_location,
10061 OPT_Wvarargs,
10062 "undefined behavior when second parameter of "
10063 "%<va_start%> is declared with %<register%> storage");
10066 /* We want to verify the second parameter just once before the tree
10067 optimizers are run and then avoid keeping it in the tree,
10068 as otherwise we could warn even for correct code like:
10069 void foo (int i, ...)
10070 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10071 if (va_start_p)
10072 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
10073 else
10074 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
10076 return false;
10080 /* Expand a call EXP to __builtin_object_size. */
10082 static rtx
10083 expand_builtin_object_size (tree exp)
10085 tree ost;
10086 int object_size_type;
10087 tree fndecl = get_callee_fndecl (exp);
10089 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10091 error ("first argument of %qD must be a pointer, second integer constant",
10092 fndecl);
10093 expand_builtin_trap ();
10094 return const0_rtx;
10097 ost = CALL_EXPR_ARG (exp, 1);
10098 STRIP_NOPS (ost);
10100 if (TREE_CODE (ost) != INTEGER_CST
10101 || tree_int_cst_sgn (ost) < 0
10102 || compare_tree_int (ost, 3) > 0)
10104 error ("last argument of %qD is not integer constant between 0 and 3",
10105 fndecl);
10106 expand_builtin_trap ();
10107 return const0_rtx;
10110 object_size_type = tree_to_shwi (ost);
10112 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10115 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10116 FCODE is the BUILT_IN_* to use.
10117 Return NULL_RTX if we failed; the caller should emit a normal call,
10118 otherwise try to get the result in TARGET, if convenient (and in
10119 mode MODE if that's convenient). */
10121 static rtx
10122 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
10123 enum built_in_function fcode)
10125 if (!validate_arglist (exp,
10126 POINTER_TYPE,
10127 fcode == BUILT_IN_MEMSET_CHK
10128 ? INTEGER_TYPE : POINTER_TYPE,
10129 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10130 return NULL_RTX;
10132 tree dest = CALL_EXPR_ARG (exp, 0);
10133 tree src = CALL_EXPR_ARG (exp, 1);
10134 tree len = CALL_EXPR_ARG (exp, 2);
10135 tree size = CALL_EXPR_ARG (exp, 3);
10137 /* FIXME: Set access mode to write only for memset et al. */
10138 bool sizes_ok = check_access (exp, len, /*maxread=*/NULL_TREE,
10139 /*srcstr=*/NULL_TREE, size, access_read_write);
10141 if (!tree_fits_uhwi_p (size))
10142 return NULL_RTX;
10144 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
10146 /* Avoid transforming the checking call to an ordinary one when
10147 an overflow has been detected or when the call couldn't be
10148 validated because the size is not constant. */
10149 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
10150 return NULL_RTX;
10152 tree fn = NULL_TREE;
10153 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10154 mem{cpy,pcpy,move,set} is available. */
10155 switch (fcode)
10157 case BUILT_IN_MEMCPY_CHK:
10158 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
10159 break;
10160 case BUILT_IN_MEMPCPY_CHK:
10161 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
10162 break;
10163 case BUILT_IN_MEMMOVE_CHK:
10164 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
10165 break;
10166 case BUILT_IN_MEMSET_CHK:
10167 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
10168 break;
10169 default:
10170 break;
10173 if (! fn)
10174 return NULL_RTX;
10176 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
10177 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10178 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10179 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10181 else if (fcode == BUILT_IN_MEMSET_CHK)
10182 return NULL_RTX;
10183 else
10185 unsigned int dest_align = get_pointer_alignment (dest);
10187 /* If DEST is not a pointer type, call the normal function. */
10188 if (dest_align == 0)
10189 return NULL_RTX;
10191 /* If SRC and DEST are the same (and not volatile), do nothing. */
10192 if (operand_equal_p (src, dest, 0))
10194 tree expr;
10196 if (fcode != BUILT_IN_MEMPCPY_CHK)
10198 /* Evaluate and ignore LEN in case it has side-effects. */
10199 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10200 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10203 expr = fold_build_pointer_plus (dest, len);
10204 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10207 /* __memmove_chk special case. */
10208 if (fcode == BUILT_IN_MEMMOVE_CHK)
10210 unsigned int src_align = get_pointer_alignment (src);
10212 if (src_align == 0)
10213 return NULL_RTX;
10215 /* If src is categorized for a readonly section we can use
10216 normal __memcpy_chk. */
10217 if (readonly_data_expr (src))
10219 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
10220 if (!fn)
10221 return NULL_RTX;
10222 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
10223 dest, src, len, size);
10224 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10225 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10226 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10229 return NULL_RTX;
10233 /* Emit warning if a buffer overflow is detected at compile time. */
10235 static void
10236 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10238 /* The source string. */
10239 tree srcstr = NULL_TREE;
10240 /* The size of the destination object returned by __builtin_object_size. */
10241 tree objsize = NULL_TREE;
10242 /* The string that is being concatenated with (as in __strcat_chk)
10243 or null if it isn't. */
10244 tree catstr = NULL_TREE;
10245 /* The maximum length of the source sequence in a bounded operation
10246 (such as __strncat_chk) or null if the operation isn't bounded
10247 (such as __strcat_chk). */
10248 tree maxread = NULL_TREE;
10249 /* The exact size of the access (such as in __strncpy_chk). */
10250 tree size = NULL_TREE;
10251 /* The access by the function that's checked. Except for snprintf
10252 both writing and reading is checked. */
10253 access_mode mode = access_read_write;
10255 switch (fcode)
10257 case BUILT_IN_STRCPY_CHK:
10258 case BUILT_IN_STPCPY_CHK:
10259 srcstr = CALL_EXPR_ARG (exp, 1);
10260 objsize = CALL_EXPR_ARG (exp, 2);
10261 break;
10263 case BUILT_IN_STRCAT_CHK:
10264 /* For __strcat_chk the warning will be emitted only if overflowing
10265 by at least strlen (dest) + 1 bytes. */
10266 catstr = CALL_EXPR_ARG (exp, 0);
10267 srcstr = CALL_EXPR_ARG (exp, 1);
10268 objsize = CALL_EXPR_ARG (exp, 2);
10269 break;
10271 case BUILT_IN_STRNCAT_CHK:
10272 catstr = CALL_EXPR_ARG (exp, 0);
10273 srcstr = CALL_EXPR_ARG (exp, 1);
10274 maxread = CALL_EXPR_ARG (exp, 2);
10275 objsize = CALL_EXPR_ARG (exp, 3);
10276 break;
10278 case BUILT_IN_STRNCPY_CHK:
10279 case BUILT_IN_STPNCPY_CHK:
10280 srcstr = CALL_EXPR_ARG (exp, 1);
10281 size = CALL_EXPR_ARG (exp, 2);
10282 objsize = CALL_EXPR_ARG (exp, 3);
10283 break;
10285 case BUILT_IN_SNPRINTF_CHK:
10286 case BUILT_IN_VSNPRINTF_CHK:
10287 maxread = CALL_EXPR_ARG (exp, 1);
10288 objsize = CALL_EXPR_ARG (exp, 3);
10289 /* The only checked access the write to the destination. */
10290 mode = access_write_only;
10291 break;
10292 default:
10293 gcc_unreachable ();
10296 if (catstr && maxread)
10298 /* Check __strncat_chk. There is no way to determine the length
10299 of the string to which the source string is being appended so
10300 just warn when the length of the source string is not known. */
10301 check_strncat_sizes (exp, objsize);
10302 return;
10305 check_access (exp, size, maxread, srcstr, objsize, mode);
10308 /* Emit warning if a buffer overflow is detected at compile time
10309 in __sprintf_chk/__vsprintf_chk calls. */
10311 static void
10312 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10314 tree size, len, fmt;
10315 const char *fmt_str;
10316 int nargs = call_expr_nargs (exp);
10318 /* Verify the required arguments in the original call. */
10320 if (nargs < 4)
10321 return;
10322 size = CALL_EXPR_ARG (exp, 2);
10323 fmt = CALL_EXPR_ARG (exp, 3);
10325 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
10326 return;
10328 /* Check whether the format is a literal string constant. */
10329 fmt_str = c_getstr (fmt);
10330 if (fmt_str == NULL)
10331 return;
10333 if (!init_target_chars ())
10334 return;
10336 /* If the format doesn't contain % args or %%, we know its size. */
10337 if (strchr (fmt_str, target_percent) == 0)
10338 len = build_int_cstu (size_type_node, strlen (fmt_str));
10339 /* If the format is "%s" and first ... argument is a string literal,
10340 we know it too. */
10341 else if (fcode == BUILT_IN_SPRINTF_CHK
10342 && strcmp (fmt_str, target_percent_s) == 0)
10344 tree arg;
10346 if (nargs < 5)
10347 return;
10348 arg = CALL_EXPR_ARG (exp, 4);
10349 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10350 return;
10352 len = c_strlen (arg, 1);
10353 if (!len || ! tree_fits_uhwi_p (len))
10354 return;
10356 else
10357 return;
10359 /* Add one for the terminating nul. */
10360 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
10362 check_access (exp, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, len, size,
10363 access_write_only);
10366 /* Fold a call to __builtin_object_size with arguments PTR and OST,
10367 if possible. */
10369 static tree
10370 fold_builtin_object_size (tree ptr, tree ost, enum built_in_function fcode)
10372 tree bytes;
10373 int object_size_type;
10375 if (!validate_arg (ptr, POINTER_TYPE)
10376 || !validate_arg (ost, INTEGER_TYPE))
10377 return NULL_TREE;
10379 STRIP_NOPS (ost);
10381 if (TREE_CODE (ost) != INTEGER_CST
10382 || tree_int_cst_sgn (ost) < 0
10383 || compare_tree_int (ost, 3) > 0)
10384 return NULL_TREE;
10386 object_size_type = tree_to_shwi (ost);
10388 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10389 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10390 and (size_t) 0 for types 2 and 3. */
10391 if (TREE_SIDE_EFFECTS (ptr))
10392 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10394 if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
10395 object_size_type |= OST_DYNAMIC;
10397 if (TREE_CODE (ptr) == ADDR_EXPR)
10399 compute_builtin_object_size (ptr, object_size_type, &bytes);
10400 if ((object_size_type & OST_DYNAMIC)
10401 || int_fits_type_p (bytes, size_type_node))
10402 return fold_convert (size_type_node, bytes);
10404 else if (TREE_CODE (ptr) == SSA_NAME)
10406 /* If object size is not known yet, delay folding until
10407 later. Maybe subsequent passes will help determining
10408 it. */
10409 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
10410 && ((object_size_type & OST_DYNAMIC)
10411 || int_fits_type_p (bytes, size_type_node)))
10412 return fold_convert (size_type_node, bytes);
10415 return NULL_TREE;
10418 /* Builtins with folding operations that operate on "..." arguments
10419 need special handling; we need to store the arguments in a convenient
10420 data structure before attempting any folding. Fortunately there are
10421 only a few builtins that fall into this category. FNDECL is the
10422 function, EXP is the CALL_EXPR for the call. */
10424 static tree
10425 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
10427 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10428 tree ret = NULL_TREE;
10430 switch (fcode)
10432 case BUILT_IN_FPCLASSIFY:
10433 ret = fold_builtin_fpclassify (loc, args, nargs);
10434 break;
10436 default:
10437 break;
10439 if (ret)
10441 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10442 SET_EXPR_LOCATION (ret, loc);
10443 suppress_warning (ret);
10444 return ret;
10446 return NULL_TREE;
10449 /* Initialize format string characters in the target charset. */
10451 bool
10452 init_target_chars (void)
10454 static bool init;
10455 if (!init)
10457 target_newline = lang_hooks.to_target_charset ('\n');
10458 target_percent = lang_hooks.to_target_charset ('%');
10459 target_c = lang_hooks.to_target_charset ('c');
10460 target_s = lang_hooks.to_target_charset ('s');
10461 if (target_newline == 0 || target_percent == 0 || target_c == 0
10462 || target_s == 0)
10463 return false;
10465 target_percent_c[0] = target_percent;
10466 target_percent_c[1] = target_c;
10467 target_percent_c[2] = '\0';
10469 target_percent_s[0] = target_percent;
10470 target_percent_s[1] = target_s;
10471 target_percent_s[2] = '\0';
10473 target_percent_s_newline[0] = target_percent;
10474 target_percent_s_newline[1] = target_s;
10475 target_percent_s_newline[2] = target_newline;
10476 target_percent_s_newline[3] = '\0';
10478 init = true;
10480 return true;
10483 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
10484 and no overflow/underflow occurred. INEXACT is true if M was not
10485 exactly calculated. TYPE is the tree type for the result. This
10486 function assumes that you cleared the MPFR flags and then
10487 calculated M to see if anything subsequently set a flag prior to
10488 entering this function. Return NULL_TREE if any checks fail. */
10490 static tree
10491 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
10493 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10494 overflow/underflow occurred. If -frounding-math, proceed iff the
10495 result of calling FUNC was exact. */
10496 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
10497 && (!flag_rounding_math || !inexact))
10499 REAL_VALUE_TYPE rr;
10501 real_from_mpfr (&rr, m, type, MPFR_RNDN);
10502 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
10503 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10504 but the mpft_t is not, then we underflowed in the
10505 conversion. */
10506 if (real_isfinite (&rr)
10507 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
10509 REAL_VALUE_TYPE rmode;
10511 real_convert (&rmode, TYPE_MODE (type), &rr);
10512 /* Proceed iff the specified mode can hold the value. */
10513 if (real_identical (&rmode, &rr))
10514 return build_real (type, rmode);
10517 return NULL_TREE;
10520 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
10521 number and no overflow/underflow occurred. INEXACT is true if M
10522 was not exactly calculated. TYPE is the tree type for the result.
10523 This function assumes that you cleared the MPFR flags and then
10524 calculated M to see if anything subsequently set a flag prior to
10525 entering this function. Return NULL_TREE if any checks fail, if
10526 FORCE_CONVERT is true, then bypass the checks. */
10528 static tree
10529 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
10531 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10532 overflow/underflow occurred. If -frounding-math, proceed iff the
10533 result of calling FUNC was exact. */
10534 if (force_convert
10535 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
10536 && !mpfr_overflow_p () && !mpfr_underflow_p ()
10537 && (!flag_rounding_math || !inexact)))
10539 REAL_VALUE_TYPE re, im;
10541 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), MPFR_RNDN);
10542 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), MPFR_RNDN);
10543 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
10544 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10545 but the mpft_t is not, then we underflowed in the
10546 conversion. */
10547 if (force_convert
10548 || (real_isfinite (&re) && real_isfinite (&im)
10549 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
10550 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
10552 REAL_VALUE_TYPE re_mode, im_mode;
10554 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
10555 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
10556 /* Proceed iff the specified mode can hold the value. */
10557 if (force_convert
10558 || (real_identical (&re_mode, &re)
10559 && real_identical (&im_mode, &im)))
10560 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
10561 build_real (TREE_TYPE (type), im_mode));
10564 return NULL_TREE;
10567 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
10568 the pointer *(ARG_QUO) and return the result. The type is taken
10569 from the type of ARG0 and is used for setting the precision of the
10570 calculation and results. */
10572 static tree
10573 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
10575 tree const type = TREE_TYPE (arg0);
10576 tree result = NULL_TREE;
10578 STRIP_NOPS (arg0);
10579 STRIP_NOPS (arg1);
10581 /* To proceed, MPFR must exactly represent the target floating point
10582 format, which only happens when the target base equals two. */
10583 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10584 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
10585 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
10587 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
10588 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
10590 if (real_isfinite (ra0) && real_isfinite (ra1))
10592 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10593 const int prec = fmt->p;
10594 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
10595 tree result_rem;
10596 long integer_quo;
10597 mpfr_t m0, m1;
10599 mpfr_inits2 (prec, m0, m1, NULL);
10600 mpfr_from_real (m0, ra0, MPFR_RNDN);
10601 mpfr_from_real (m1, ra1, MPFR_RNDN);
10602 mpfr_clear_flags ();
10603 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
10604 /* Remquo is independent of the rounding mode, so pass
10605 inexact=0 to do_mpfr_ckconv(). */
10606 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
10607 mpfr_clears (m0, m1, NULL);
10608 if (result_rem)
10610 /* MPFR calculates quo in the host's long so it may
10611 return more bits in quo than the target int can hold
10612 if sizeof(host long) > sizeof(target int). This can
10613 happen even for native compilers in LP64 mode. In
10614 these cases, modulo the quo value with the largest
10615 number that the target int can hold while leaving one
10616 bit for the sign. */
10617 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
10618 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
10620 /* Dereference the quo pointer argument. */
10621 arg_quo = build_fold_indirect_ref (arg_quo);
10622 /* Proceed iff a valid pointer type was passed in. */
10623 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
10625 /* Set the value. */
10626 tree result_quo
10627 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
10628 build_int_cst (TREE_TYPE (arg_quo),
10629 integer_quo));
10630 TREE_SIDE_EFFECTS (result_quo) = 1;
10631 /* Combine the quo assignment with the rem. */
10632 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10633 result_quo, result_rem));
10638 return result;
10641 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
10642 resulting value as a tree with type TYPE. The mpfr precision is
10643 set to the precision of TYPE. We assume that this mpfr function
10644 returns zero if the result could be calculated exactly within the
10645 requested precision. In addition, the integer pointer represented
10646 by ARG_SG will be dereferenced and set to the appropriate signgam
10647 (-1,1) value. */
10649 static tree
10650 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
10652 tree result = NULL_TREE;
10654 STRIP_NOPS (arg);
10656 /* To proceed, MPFR must exactly represent the target floating point
10657 format, which only happens when the target base equals two. Also
10658 verify ARG is a constant and that ARG_SG is an int pointer. */
10659 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10660 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
10661 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
10662 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
10664 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
10666 /* In addition to NaN and Inf, the argument cannot be zero or a
10667 negative integer. */
10668 if (real_isfinite (ra)
10669 && ra->cl != rvc_zero
10670 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
10672 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10673 const int prec = fmt->p;
10674 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
10675 int inexact, sg;
10676 mpfr_t m;
10677 tree result_lg;
10679 mpfr_init2 (m, prec);
10680 mpfr_from_real (m, ra, MPFR_RNDN);
10681 mpfr_clear_flags ();
10682 inexact = mpfr_lgamma (m, &sg, m, rnd);
10683 result_lg = do_mpfr_ckconv (m, type, inexact);
10684 mpfr_clear (m);
10685 if (result_lg)
10687 tree result_sg;
10689 /* Dereference the arg_sg pointer argument. */
10690 arg_sg = build_fold_indirect_ref (arg_sg);
10691 /* Assign the signgam value into *arg_sg. */
10692 result_sg = fold_build2 (MODIFY_EXPR,
10693 TREE_TYPE (arg_sg), arg_sg,
10694 build_int_cst (TREE_TYPE (arg_sg), sg));
10695 TREE_SIDE_EFFECTS (result_sg) = 1;
10696 /* Combine the signgam assignment with the lgamma result. */
10697 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10698 result_sg, result_lg));
10703 return result;
10706 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
10707 mpc function FUNC on it and return the resulting value as a tree
10708 with type TYPE. The mpfr precision is set to the precision of
10709 TYPE. We assume that function FUNC returns zero if the result
10710 could be calculated exactly within the requested precision. If
10711 DO_NONFINITE is true, then fold expressions containing Inf or NaN
10712 in the arguments and/or results. */
10714 tree
10715 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
10716 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
10718 tree result = NULL_TREE;
10720 STRIP_NOPS (arg0);
10721 STRIP_NOPS (arg1);
10723 /* To proceed, MPFR must exactly represent the target floating point
10724 format, which only happens when the target base equals two. */
10725 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
10726 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10727 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
10728 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
10729 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
10731 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
10732 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
10733 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
10734 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
10736 if (do_nonfinite
10737 || (real_isfinite (re0) && real_isfinite (im0)
10738 && real_isfinite (re1) && real_isfinite (im1)))
10740 const struct real_format *const fmt =
10741 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
10742 const int prec = fmt->p;
10743 const mpfr_rnd_t rnd = fmt->round_towards_zero
10744 ? MPFR_RNDZ : MPFR_RNDN;
10745 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
10746 int inexact;
10747 mpc_t m0, m1;
10749 mpc_init2 (m0, prec);
10750 mpc_init2 (m1, prec);
10751 mpfr_from_real (mpc_realref (m0), re0, rnd);
10752 mpfr_from_real (mpc_imagref (m0), im0, rnd);
10753 mpfr_from_real (mpc_realref (m1), re1, rnd);
10754 mpfr_from_real (mpc_imagref (m1), im1, rnd);
10755 mpfr_clear_flags ();
10756 inexact = func (m0, m0, m1, crnd);
10757 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
10758 mpc_clear (m0);
10759 mpc_clear (m1);
10763 return result;
10766 /* A wrapper function for builtin folding that prevents warnings for
10767 "statement without effect" and the like, caused by removing the
10768 call node earlier than the warning is generated. */
10770 tree
10771 fold_call_stmt (gcall *stmt, bool ignore)
10773 tree ret = NULL_TREE;
10774 tree fndecl = gimple_call_fndecl (stmt);
10775 location_t loc = gimple_location (stmt);
10776 if (fndecl && fndecl_built_in_p (fndecl)
10777 && !gimple_call_va_arg_pack_p (stmt))
10779 int nargs = gimple_call_num_args (stmt);
10780 tree *args = (nargs > 0
10781 ? gimple_call_arg_ptr (stmt, 0)
10782 : &error_mark_node);
10784 if (avoid_folding_inline_builtin (fndecl))
10785 return NULL_TREE;
10786 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10788 return targetm.fold_builtin (fndecl, nargs, args, ignore);
10790 else
10792 ret = fold_builtin_n (loc, NULL_TREE, fndecl, args, nargs, ignore);
10793 if (ret)
10795 /* Propagate location information from original call to
10796 expansion of builtin. Otherwise things like
10797 maybe_emit_chk_warning, that operate on the expansion
10798 of a builtin, will use the wrong location information. */
10799 if (gimple_has_location (stmt))
10801 tree realret = ret;
10802 if (TREE_CODE (ret) == NOP_EXPR)
10803 realret = TREE_OPERAND (ret, 0);
10804 if (CAN_HAVE_LOCATION_P (realret)
10805 && !EXPR_HAS_LOCATION (realret))
10806 SET_EXPR_LOCATION (realret, loc);
10807 return realret;
10809 return ret;
10813 return NULL_TREE;
10816 /* Look up the function in builtin_decl that corresponds to DECL
10817 and set ASMSPEC as its user assembler name. DECL must be a
10818 function decl that declares a builtin. */
10820 void
10821 set_builtin_user_assembler_name (tree decl, const char *asmspec)
10823 gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL)
10824 && asmspec != 0);
10826 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
10827 set_user_assembler_name (builtin, asmspec);
10829 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
10830 && INT_TYPE_SIZE < BITS_PER_WORD)
10832 scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
10833 set_user_assembler_libfunc ("ffs", asmspec);
10834 set_optab_libfunc (ffs_optab, mode, "ffs");
10838 /* Return true if DECL is a builtin that expands to a constant or similarly
10839 simple code. */
10840 bool
10841 is_simple_builtin (tree decl)
10843 if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
10844 switch (DECL_FUNCTION_CODE (decl))
10846 /* Builtins that expand to constants. */
10847 case BUILT_IN_CONSTANT_P:
10848 case BUILT_IN_EXPECT:
10849 case BUILT_IN_OBJECT_SIZE:
10850 case BUILT_IN_UNREACHABLE:
10851 /* Simple register moves or loads from stack. */
10852 case BUILT_IN_ASSUME_ALIGNED:
10853 case BUILT_IN_RETURN_ADDRESS:
10854 case BUILT_IN_EXTRACT_RETURN_ADDR:
10855 case BUILT_IN_FROB_RETURN_ADDR:
10856 case BUILT_IN_RETURN:
10857 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
10858 case BUILT_IN_FRAME_ADDRESS:
10859 case BUILT_IN_VA_END:
10860 case BUILT_IN_STACK_SAVE:
10861 case BUILT_IN_STACK_RESTORE:
10862 case BUILT_IN_DWARF_CFA:
10863 /* Exception state returns or moves registers around. */
10864 case BUILT_IN_EH_FILTER:
10865 case BUILT_IN_EH_POINTER:
10866 case BUILT_IN_EH_COPY_VALUES:
10867 return true;
10869 default:
10870 return false;
10873 return false;
10876 /* Return true if DECL is a builtin that is not expensive, i.e., they are
10877 most probably expanded inline into reasonably simple code. This is a
10878 superset of is_simple_builtin. */
10879 bool
10880 is_inexpensive_builtin (tree decl)
10882 if (!decl)
10883 return false;
10884 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
10885 return true;
10886 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10887 switch (DECL_FUNCTION_CODE (decl))
10889 case BUILT_IN_ABS:
10890 CASE_BUILT_IN_ALLOCA:
10891 case BUILT_IN_BSWAP16:
10892 case BUILT_IN_BSWAP32:
10893 case BUILT_IN_BSWAP64:
10894 case BUILT_IN_BSWAP128:
10895 case BUILT_IN_CLZ:
10896 case BUILT_IN_CLZIMAX:
10897 case BUILT_IN_CLZL:
10898 case BUILT_IN_CLZLL:
10899 case BUILT_IN_CTZ:
10900 case BUILT_IN_CTZIMAX:
10901 case BUILT_IN_CTZL:
10902 case BUILT_IN_CTZLL:
10903 case BUILT_IN_FFS:
10904 case BUILT_IN_FFSIMAX:
10905 case BUILT_IN_FFSL:
10906 case BUILT_IN_FFSLL:
10907 case BUILT_IN_IMAXABS:
10908 case BUILT_IN_FINITE:
10909 case BUILT_IN_FINITEF:
10910 case BUILT_IN_FINITEL:
10911 case BUILT_IN_FINITED32:
10912 case BUILT_IN_FINITED64:
10913 case BUILT_IN_FINITED128:
10914 case BUILT_IN_FPCLASSIFY:
10915 case BUILT_IN_ISFINITE:
10916 case BUILT_IN_ISINF_SIGN:
10917 case BUILT_IN_ISINF:
10918 case BUILT_IN_ISINFF:
10919 case BUILT_IN_ISINFL:
10920 case BUILT_IN_ISINFD32:
10921 case BUILT_IN_ISINFD64:
10922 case BUILT_IN_ISINFD128:
10923 case BUILT_IN_ISNAN:
10924 case BUILT_IN_ISNANF:
10925 case BUILT_IN_ISNANL:
10926 case BUILT_IN_ISNAND32:
10927 case BUILT_IN_ISNAND64:
10928 case BUILT_IN_ISNAND128:
10929 case BUILT_IN_ISNORMAL:
10930 case BUILT_IN_ISGREATER:
10931 case BUILT_IN_ISGREATEREQUAL:
10932 case BUILT_IN_ISLESS:
10933 case BUILT_IN_ISLESSEQUAL:
10934 case BUILT_IN_ISLESSGREATER:
10935 case BUILT_IN_ISUNORDERED:
10936 case BUILT_IN_VA_ARG_PACK:
10937 case BUILT_IN_VA_ARG_PACK_LEN:
10938 case BUILT_IN_VA_COPY:
10939 case BUILT_IN_TRAP:
10940 case BUILT_IN_SAVEREGS:
10941 case BUILT_IN_POPCOUNTL:
10942 case BUILT_IN_POPCOUNTLL:
10943 case BUILT_IN_POPCOUNTIMAX:
10944 case BUILT_IN_POPCOUNT:
10945 case BUILT_IN_PARITYL:
10946 case BUILT_IN_PARITYLL:
10947 case BUILT_IN_PARITYIMAX:
10948 case BUILT_IN_PARITY:
10949 case BUILT_IN_LABS:
10950 case BUILT_IN_LLABS:
10951 case BUILT_IN_PREFETCH:
10952 case BUILT_IN_ACC_ON_DEVICE:
10953 return true;
10955 default:
10956 return is_simple_builtin (decl);
10959 return false;
10962 /* Return true if T is a constant and the value cast to a target char
10963 can be represented by a host char.
10964 Store the casted char constant in *P if so. */
10966 bool
10967 target_char_cst_p (tree t, char *p)
10969 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
10970 return false;
10972 *p = (char)tree_to_uhwi (t);
10973 return true;
10976 /* Return true if the builtin DECL is implemented in a standard library.
10977 Otherwise return false which doesn't guarantee it is not (thus the list
10978 of handled builtins below may be incomplete). */
10980 bool
10981 builtin_with_linkage_p (tree decl)
10983 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10984 switch (DECL_FUNCTION_CODE (decl))
10986 CASE_FLT_FN (BUILT_IN_ACOS):
10987 CASE_FLT_FN (BUILT_IN_ACOSH):
10988 CASE_FLT_FN (BUILT_IN_ASIN):
10989 CASE_FLT_FN (BUILT_IN_ASINH):
10990 CASE_FLT_FN (BUILT_IN_ATAN):
10991 CASE_FLT_FN (BUILT_IN_ATANH):
10992 CASE_FLT_FN (BUILT_IN_ATAN2):
10993 CASE_FLT_FN (BUILT_IN_CBRT):
10994 CASE_FLT_FN (BUILT_IN_CEIL):
10995 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
10996 CASE_FLT_FN (BUILT_IN_COPYSIGN):
10997 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
10998 CASE_FLT_FN (BUILT_IN_COS):
10999 CASE_FLT_FN (BUILT_IN_COSH):
11000 CASE_FLT_FN (BUILT_IN_ERF):
11001 CASE_FLT_FN (BUILT_IN_ERFC):
11002 CASE_FLT_FN (BUILT_IN_EXP):
11003 CASE_FLT_FN (BUILT_IN_EXP2):
11004 CASE_FLT_FN (BUILT_IN_EXPM1):
11005 CASE_FLT_FN (BUILT_IN_FABS):
11006 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
11007 CASE_FLT_FN (BUILT_IN_FDIM):
11008 CASE_FLT_FN (BUILT_IN_FLOOR):
11009 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
11010 CASE_FLT_FN (BUILT_IN_FMA):
11011 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
11012 CASE_FLT_FN (BUILT_IN_FMAX):
11013 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
11014 CASE_FLT_FN (BUILT_IN_FMIN):
11015 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
11016 CASE_FLT_FN (BUILT_IN_FMOD):
11017 CASE_FLT_FN (BUILT_IN_FREXP):
11018 CASE_FLT_FN (BUILT_IN_HYPOT):
11019 CASE_FLT_FN (BUILT_IN_ILOGB):
11020 CASE_FLT_FN (BUILT_IN_LDEXP):
11021 CASE_FLT_FN (BUILT_IN_LGAMMA):
11022 CASE_FLT_FN (BUILT_IN_LLRINT):
11023 CASE_FLT_FN (BUILT_IN_LLROUND):
11024 CASE_FLT_FN (BUILT_IN_LOG):
11025 CASE_FLT_FN (BUILT_IN_LOG10):
11026 CASE_FLT_FN (BUILT_IN_LOG1P):
11027 CASE_FLT_FN (BUILT_IN_LOG2):
11028 CASE_FLT_FN (BUILT_IN_LOGB):
11029 CASE_FLT_FN (BUILT_IN_LRINT):
11030 CASE_FLT_FN (BUILT_IN_LROUND):
11031 CASE_FLT_FN (BUILT_IN_MODF):
11032 CASE_FLT_FN (BUILT_IN_NAN):
11033 CASE_FLT_FN (BUILT_IN_NEARBYINT):
11034 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
11035 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
11036 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
11037 CASE_FLT_FN (BUILT_IN_POW):
11038 CASE_FLT_FN (BUILT_IN_REMAINDER):
11039 CASE_FLT_FN (BUILT_IN_REMQUO):
11040 CASE_FLT_FN (BUILT_IN_RINT):
11041 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
11042 CASE_FLT_FN (BUILT_IN_ROUND):
11043 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
11044 CASE_FLT_FN (BUILT_IN_SCALBLN):
11045 CASE_FLT_FN (BUILT_IN_SCALBN):
11046 CASE_FLT_FN (BUILT_IN_SIN):
11047 CASE_FLT_FN (BUILT_IN_SINH):
11048 CASE_FLT_FN (BUILT_IN_SINCOS):
11049 CASE_FLT_FN (BUILT_IN_SQRT):
11050 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
11051 CASE_FLT_FN (BUILT_IN_TAN):
11052 CASE_FLT_FN (BUILT_IN_TANH):
11053 CASE_FLT_FN (BUILT_IN_TGAMMA):
11054 CASE_FLT_FN (BUILT_IN_TRUNC):
11055 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
11056 return true;
11058 case BUILT_IN_STPCPY:
11059 case BUILT_IN_STPNCPY:
11060 /* stpcpy is both referenced in libiberty's pex-win32.c and provided
11061 by libiberty's stpcpy.c for MinGW targets so we need to return true
11062 in order to be able to build libiberty in LTO mode for them. */
11063 return true;
11065 default:
11066 break;
11068 return false;
11071 /* Return true if OFFRNG is bounded to a subrange of offset values
11072 valid for the largest possible object. */
11074 bool
11075 access_ref::offset_bounded () const
11077 tree min = TYPE_MIN_VALUE (ptrdiff_type_node);
11078 tree max = TYPE_MAX_VALUE (ptrdiff_type_node);
11079 return wi::to_offset (min) <= offrng[0] && offrng[1] <= wi::to_offset (max);
11082 /* If CALLEE has known side effects, fill in INFO and return true.
11083 See tree-ssa-structalias.cc:find_func_aliases
11084 for the list of builtins we might need to handle here. */
11086 attr_fnspec
11087 builtin_fnspec (tree callee)
11089 built_in_function code = DECL_FUNCTION_CODE (callee);
11091 switch (code)
11093 /* All the following functions read memory pointed to by
11094 their second argument and write memory pointed to by first
11095 argument.
11096 strcat/strncat additionally reads memory pointed to by the first
11097 argument. */
11098 case BUILT_IN_STRCAT:
11099 case BUILT_IN_STRCAT_CHK:
11100 return "1cW 1 ";
11101 case BUILT_IN_STRNCAT:
11102 case BUILT_IN_STRNCAT_CHK:
11103 return "1cW 13";
11104 case BUILT_IN_STRCPY:
11105 case BUILT_IN_STRCPY_CHK:
11106 return "1cO 1 ";
11107 case BUILT_IN_STPCPY:
11108 case BUILT_IN_STPCPY_CHK:
11109 return ".cO 1 ";
11110 case BUILT_IN_STRNCPY:
11111 case BUILT_IN_MEMCPY:
11112 case BUILT_IN_MEMMOVE:
11113 case BUILT_IN_TM_MEMCPY:
11114 case BUILT_IN_TM_MEMMOVE:
11115 case BUILT_IN_STRNCPY_CHK:
11116 case BUILT_IN_MEMCPY_CHK:
11117 case BUILT_IN_MEMMOVE_CHK:
11118 return "1cO313";
11119 case BUILT_IN_MEMPCPY:
11120 case BUILT_IN_MEMPCPY_CHK:
11121 return ".cO313";
11122 case BUILT_IN_STPNCPY:
11123 case BUILT_IN_STPNCPY_CHK:
11124 return ".cO313";
11125 case BUILT_IN_BCOPY:
11126 return ".c23O3";
11127 case BUILT_IN_BZERO:
11128 return ".cO2";
11129 case BUILT_IN_MEMCMP:
11130 case BUILT_IN_MEMCMP_EQ:
11131 case BUILT_IN_BCMP:
11132 case BUILT_IN_STRNCMP:
11133 case BUILT_IN_STRNCMP_EQ:
11134 case BUILT_IN_STRNCASECMP:
11135 return ".cR3R3";
11137 /* The following functions read memory pointed to by their
11138 first argument. */
11139 CASE_BUILT_IN_TM_LOAD (1):
11140 CASE_BUILT_IN_TM_LOAD (2):
11141 CASE_BUILT_IN_TM_LOAD (4):
11142 CASE_BUILT_IN_TM_LOAD (8):
11143 CASE_BUILT_IN_TM_LOAD (FLOAT):
11144 CASE_BUILT_IN_TM_LOAD (DOUBLE):
11145 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
11146 CASE_BUILT_IN_TM_LOAD (M64):
11147 CASE_BUILT_IN_TM_LOAD (M128):
11148 CASE_BUILT_IN_TM_LOAD (M256):
11149 case BUILT_IN_TM_LOG:
11150 case BUILT_IN_TM_LOG_1:
11151 case BUILT_IN_TM_LOG_2:
11152 case BUILT_IN_TM_LOG_4:
11153 case BUILT_IN_TM_LOG_8:
11154 case BUILT_IN_TM_LOG_FLOAT:
11155 case BUILT_IN_TM_LOG_DOUBLE:
11156 case BUILT_IN_TM_LOG_LDOUBLE:
11157 case BUILT_IN_TM_LOG_M64:
11158 case BUILT_IN_TM_LOG_M128:
11159 case BUILT_IN_TM_LOG_M256:
11160 return ".cR ";
11162 case BUILT_IN_INDEX:
11163 case BUILT_IN_RINDEX:
11164 case BUILT_IN_STRCHR:
11165 case BUILT_IN_STRLEN:
11166 case BUILT_IN_STRRCHR:
11167 return ".cR ";
11168 case BUILT_IN_STRNLEN:
11169 return ".cR2";
11171 /* These read memory pointed to by the first argument.
11172 Allocating memory does not have any side-effects apart from
11173 being the definition point for the pointer.
11174 Unix98 specifies that errno is set on allocation failure. */
11175 case BUILT_IN_STRDUP:
11176 return "mCR ";
11177 case BUILT_IN_STRNDUP:
11178 return "mCR2";
11179 /* Allocating memory does not have any side-effects apart from
11180 being the definition point for the pointer. */
11181 case BUILT_IN_MALLOC:
11182 case BUILT_IN_ALIGNED_ALLOC:
11183 case BUILT_IN_CALLOC:
11184 case BUILT_IN_GOMP_ALLOC:
11185 return "mC";
11186 CASE_BUILT_IN_ALLOCA:
11187 return "mc";
11188 /* These read memory pointed to by the first argument with size
11189 in the third argument. */
11190 case BUILT_IN_MEMCHR:
11191 return ".cR3";
11192 /* These read memory pointed to by the first and second arguments. */
11193 case BUILT_IN_STRSTR:
11194 case BUILT_IN_STRPBRK:
11195 case BUILT_IN_STRCASECMP:
11196 case BUILT_IN_STRCSPN:
11197 case BUILT_IN_STRSPN:
11198 case BUILT_IN_STRCMP:
11199 case BUILT_IN_STRCMP_EQ:
11200 return ".cR R ";
11201 /* Freeing memory kills the pointed-to memory. More importantly
11202 the call has to serve as a barrier for moving loads and stores
11203 across it. */
11204 case BUILT_IN_STACK_RESTORE:
11205 case BUILT_IN_FREE:
11206 case BUILT_IN_GOMP_FREE:
11207 return ".co ";
11208 case BUILT_IN_VA_END:
11209 return ".cO ";
11210 /* Realloc serves both as allocation point and deallocation point. */
11211 case BUILT_IN_REALLOC:
11212 return ".Cw ";
11213 case BUILT_IN_GAMMA_R:
11214 case BUILT_IN_GAMMAF_R:
11215 case BUILT_IN_GAMMAL_R:
11216 case BUILT_IN_LGAMMA_R:
11217 case BUILT_IN_LGAMMAF_R:
11218 case BUILT_IN_LGAMMAL_R:
11219 return ".C. Ot";
11220 case BUILT_IN_FREXP:
11221 case BUILT_IN_FREXPF:
11222 case BUILT_IN_FREXPL:
11223 case BUILT_IN_MODF:
11224 case BUILT_IN_MODFF:
11225 case BUILT_IN_MODFL:
11226 return ".c. Ot";
11227 case BUILT_IN_REMQUO:
11228 case BUILT_IN_REMQUOF:
11229 case BUILT_IN_REMQUOL:
11230 return ".c. . Ot";
11231 case BUILT_IN_SINCOS:
11232 case BUILT_IN_SINCOSF:
11233 case BUILT_IN_SINCOSL:
11234 return ".c. OtOt";
11235 case BUILT_IN_MEMSET:
11236 case BUILT_IN_MEMSET_CHK:
11237 case BUILT_IN_TM_MEMSET:
11238 return "1cO3";
11239 CASE_BUILT_IN_TM_STORE (1):
11240 CASE_BUILT_IN_TM_STORE (2):
11241 CASE_BUILT_IN_TM_STORE (4):
11242 CASE_BUILT_IN_TM_STORE (8):
11243 CASE_BUILT_IN_TM_STORE (FLOAT):
11244 CASE_BUILT_IN_TM_STORE (DOUBLE):
11245 CASE_BUILT_IN_TM_STORE (LDOUBLE):
11246 CASE_BUILT_IN_TM_STORE (M64):
11247 CASE_BUILT_IN_TM_STORE (M128):
11248 CASE_BUILT_IN_TM_STORE (M256):
11249 return ".cO ";
11250 case BUILT_IN_STACK_SAVE:
11251 case BUILT_IN_RETURN:
11252 case BUILT_IN_EH_POINTER:
11253 case BUILT_IN_EH_FILTER:
11254 case BUILT_IN_UNWIND_RESUME:
11255 case BUILT_IN_CXA_END_CLEANUP:
11256 case BUILT_IN_EH_COPY_VALUES:
11257 case BUILT_IN_FRAME_ADDRESS:
11258 case BUILT_IN_APPLY_ARGS:
11259 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
11260 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
11261 case BUILT_IN_PREFETCH:
11262 case BUILT_IN_DWARF_CFA:
11263 case BUILT_IN_RETURN_ADDRESS:
11264 return ".c";
11265 case BUILT_IN_ASSUME_ALIGNED:
11266 return "1cX ";
11267 /* But posix_memalign stores a pointer into the memory pointed to
11268 by its first argument. */
11269 case BUILT_IN_POSIX_MEMALIGN:
11270 return ".cOt";
11272 default:
11273 return "";