compiler: don't generate stubs for ambiguous direct interface methods
[official-gcc.git] / gcc / builtins.cc
blob971b18c374592bb60dec9feee21d44f4d1a1ebc4
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-iterator.h"
71 #include "gimple-fold.h"
72 #include "intl.h"
73 #include "file-prefix-map.h" /* remap_macro_filename() */
74 #include "gomp-constants.h"
75 #include "omp-general.h"
76 #include "tree-dfa.h"
77 #include "gimple-ssa.h"
78 #include "tree-ssa-live.h"
79 #include "tree-outof-ssa.h"
80 #include "attr-fnspec.h"
81 #include "demangle.h"
82 #include "gimple-range.h"
83 #include "pointer-query.h"
85 struct target_builtins default_target_builtins;
86 #if SWITCHABLE_TARGET
87 struct target_builtins *this_target_builtins = &default_target_builtins;
88 #endif
90 /* Define the names of the builtin function types and codes. */
91 const char *const built_in_class_names[BUILT_IN_LAST]
92 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
94 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
95 const char * built_in_names[(int) END_BUILTINS] =
97 #include "builtins.def"
100 /* Setup an array of builtin_info_type, make sure each element decl is
101 initialized to NULL_TREE. */
102 builtin_info_type builtin_info[(int)END_BUILTINS];
104 /* Non-zero if __builtin_constant_p should be folded right away. */
105 bool force_folding_builtin_constant_p;
107 static int target_char_cast (tree, char *);
108 static int apply_args_size (void);
109 static int apply_result_size (void);
110 static rtx result_vector (int, rtx);
111 static void expand_builtin_prefetch (tree);
112 static rtx expand_builtin_apply_args (void);
113 static rtx expand_builtin_apply_args_1 (void);
114 static rtx expand_builtin_apply (rtx, rtx, rtx);
115 static void expand_builtin_return (rtx);
116 static enum type_class type_to_class (tree);
117 static rtx expand_builtin_classify_type (tree);
118 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
119 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
120 static rtx expand_builtin_interclass_mathfn (tree, rtx);
121 static rtx expand_builtin_sincos (tree);
122 static rtx expand_builtin_fegetround (tree, rtx, machine_mode);
123 static rtx expand_builtin_feclear_feraise_except (tree, rtx, machine_mode,
124 optab);
125 static rtx expand_builtin_cexpi (tree, rtx);
126 static rtx expand_builtin_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 ARRAY_SIZE (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) == 5)
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 rtx val2 = expand_simple_binop (mode, ASHIFT, const1_rtx,
6254 val, NULL_RTX, true, OPTAB_DIRECT);
6255 if (code == AND)
6256 val2 = expand_simple_unop (mode, NOT, val2, NULL_RTX, true);
6257 if (expand_atomic_fetch_op (const0_rtx, mem, val2, code, model, false))
6258 return;
6261 rtx target;
6262 if (lhs)
6263 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6264 else
6265 target = gen_reg_rtx (mode);
6266 enum insn_code icode = direct_optab_handler (optab, mode);
6267 gcc_assert (icode != CODE_FOR_nothing);
6268 create_output_operand (&ops[0], target, mode);
6269 create_fixed_operand (&ops[1], mem);
6270 create_convert_operand_to (&ops[2], val, mode, true);
6271 create_integer_operand (&ops[3], model);
6272 create_integer_operand (&ops[4], integer_onep (flag));
6273 if (maybe_expand_insn (icode, 5, ops))
6274 return;
6276 rtx bitval = val;
6277 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6278 val, NULL_RTX, true, OPTAB_DIRECT);
6279 rtx maskval = val;
6280 if (code == AND)
6281 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6282 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
6283 code, model, false);
6284 if (!result)
6286 bool is_atomic = gimple_call_num_args (call) == 5;
6287 tree tcall = gimple_call_arg (call, 3 + is_atomic);
6288 tree fndecl = gimple_call_addr_fndecl (tcall);
6289 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6290 tree exp = build_call_nary (type, tcall, 2 + is_atomic, ptr,
6291 make_tree (type, val),
6292 is_atomic
6293 ? gimple_call_arg (call, 3)
6294 : integer_zero_node);
6295 result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
6296 mode, !lhs);
6298 if (!lhs)
6299 return;
6300 if (integer_onep (flag))
6302 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
6303 NULL_RTX, true, OPTAB_DIRECT);
6304 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
6305 true, OPTAB_DIRECT);
6307 else
6308 result = expand_simple_binop (mode, AND, result, maskval, target, true,
6309 OPTAB_DIRECT);
6310 if (result != target)
6311 emit_move_insn (target, result);
6314 /* Expand IFN_ATOMIC_*_FETCH_CMP_0 internal function. */
6316 void
6317 expand_ifn_atomic_op_fetch_cmp_0 (gcall *call)
6319 tree cmp = gimple_call_arg (call, 0);
6320 tree ptr = gimple_call_arg (call, 1);
6321 tree arg = gimple_call_arg (call, 2);
6322 tree lhs = gimple_call_lhs (call);
6323 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6324 machine_mode mode = TYPE_MODE (TREE_TYPE (cmp));
6325 optab optab;
6326 rtx_code code;
6327 class expand_operand ops[5];
6329 gcc_assert (flag_inline_atomics);
6331 if (gimple_call_num_args (call) == 5)
6332 model = get_memmodel (gimple_call_arg (call, 3));
6334 rtx mem = get_builtin_sync_mem (ptr, mode);
6335 rtx op = expand_expr_force_mode (arg, mode);
6337 switch (gimple_call_internal_fn (call))
6339 case IFN_ATOMIC_ADD_FETCH_CMP_0:
6340 code = PLUS;
6341 optab = atomic_add_fetch_cmp_0_optab;
6342 break;
6343 case IFN_ATOMIC_SUB_FETCH_CMP_0:
6344 code = MINUS;
6345 optab = atomic_sub_fetch_cmp_0_optab;
6346 break;
6347 case IFN_ATOMIC_AND_FETCH_CMP_0:
6348 code = AND;
6349 optab = atomic_and_fetch_cmp_0_optab;
6350 break;
6351 case IFN_ATOMIC_OR_FETCH_CMP_0:
6352 code = IOR;
6353 optab = atomic_or_fetch_cmp_0_optab;
6354 break;
6355 case IFN_ATOMIC_XOR_FETCH_CMP_0:
6356 code = XOR;
6357 optab = atomic_xor_fetch_cmp_0_optab;
6358 break;
6359 default:
6360 gcc_unreachable ();
6363 enum rtx_code comp = UNKNOWN;
6364 switch (tree_to_uhwi (cmp))
6366 case ATOMIC_OP_FETCH_CMP_0_EQ: comp = EQ; break;
6367 case ATOMIC_OP_FETCH_CMP_0_NE: comp = NE; break;
6368 case ATOMIC_OP_FETCH_CMP_0_GT: comp = GT; break;
6369 case ATOMIC_OP_FETCH_CMP_0_GE: comp = GE; break;
6370 case ATOMIC_OP_FETCH_CMP_0_LT: comp = LT; break;
6371 case ATOMIC_OP_FETCH_CMP_0_LE: comp = LE; break;
6372 default: gcc_unreachable ();
6375 rtx target;
6376 if (lhs == NULL_TREE)
6377 target = gen_reg_rtx (TYPE_MODE (boolean_type_node));
6378 else
6379 target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6380 enum insn_code icode = direct_optab_handler (optab, mode);
6381 gcc_assert (icode != CODE_FOR_nothing);
6382 create_output_operand (&ops[0], target, TYPE_MODE (boolean_type_node));
6383 create_fixed_operand (&ops[1], mem);
6384 create_convert_operand_to (&ops[2], op, mode, true);
6385 create_integer_operand (&ops[3], model);
6386 create_integer_operand (&ops[4], comp);
6387 if (maybe_expand_insn (icode, 5, ops))
6388 return;
6390 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, op,
6391 code, model, true);
6392 if (!result)
6394 bool is_atomic = gimple_call_num_args (call) == 5;
6395 tree tcall = gimple_call_arg (call, 3 + is_atomic);
6396 tree fndecl = gimple_call_addr_fndecl (tcall);
6397 tree type = TREE_TYPE (TREE_TYPE (fndecl));
6398 tree exp = build_call_nary (type, tcall,
6399 2 + is_atomic, ptr, arg,
6400 is_atomic
6401 ? gimple_call_arg (call, 3)
6402 : integer_zero_node);
6403 result = expand_builtin (exp, gen_reg_rtx (mode), NULL_RTX,
6404 mode, !lhs);
6407 if (lhs)
6409 result = emit_store_flag_force (target, comp, result, const0_rtx, mode,
6410 0, 1);
6411 if (result != target)
6412 emit_move_insn (target, result);
6416 /* Expand an atomic clear operation.
6417 void _atomic_clear (BOOL *obj, enum memmodel)
6418 EXP is the call expression. */
6420 static rtx
6421 expand_builtin_atomic_clear (tree exp)
6423 machine_mode mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6424 rtx mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6425 memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6427 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
6428 model = MEMMODEL_SEQ_CST;
6430 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6431 Failing that, a store is issued by __atomic_store. The only way this can
6432 fail is if the bool type is larger than a word size. Unlikely, but
6433 handle it anyway for completeness. Assume a single threaded model since
6434 there is no atomic support in this case, and no barriers are required. */
6435 rtx ret = expand_atomic_store (mem, const0_rtx, model, true);
6436 if (!ret)
6437 emit_move_insn (mem, const0_rtx);
6438 return const0_rtx;
6441 /* Expand an atomic test_and_set operation.
6442 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6443 EXP is the call expression. */
6445 static rtx
6446 expand_builtin_atomic_test_and_set (tree exp, rtx target)
6448 rtx mem;
6449 enum memmodel model;
6450 machine_mode mode;
6452 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6453 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6454 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6456 return expand_atomic_test_and_set (target, mem, model);
6460 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6461 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6463 static tree
6464 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
6466 int size;
6467 machine_mode mode;
6468 unsigned int mode_align, type_align;
6470 if (TREE_CODE (arg0) != INTEGER_CST)
6471 return NULL_TREE;
6473 /* We need a corresponding integer mode for the access to be lock-free. */
6474 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
6475 if (!int_mode_for_size (size, 0).exists (&mode))
6476 return boolean_false_node;
6478 mode_align = GET_MODE_ALIGNMENT (mode);
6480 if (TREE_CODE (arg1) == INTEGER_CST)
6482 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
6484 /* Either this argument is null, or it's a fake pointer encoding
6485 the alignment of the object. */
6486 val = least_bit_hwi (val);
6487 val *= BITS_PER_UNIT;
6489 if (val == 0 || mode_align < val)
6490 type_align = mode_align;
6491 else
6492 type_align = val;
6494 else
6496 tree ttype = TREE_TYPE (arg1);
6498 /* This function is usually invoked and folded immediately by the front
6499 end before anything else has a chance to look at it. The pointer
6500 parameter at this point is usually cast to a void *, so check for that
6501 and look past the cast. */
6502 if (CONVERT_EXPR_P (arg1)
6503 && POINTER_TYPE_P (ttype)
6504 && VOID_TYPE_P (TREE_TYPE (ttype))
6505 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
6506 arg1 = TREE_OPERAND (arg1, 0);
6508 ttype = TREE_TYPE (arg1);
6509 gcc_assert (POINTER_TYPE_P (ttype));
6511 /* Get the underlying type of the object. */
6512 ttype = TREE_TYPE (ttype);
6513 type_align = TYPE_ALIGN (ttype);
6516 /* If the object has smaller alignment, the lock free routines cannot
6517 be used. */
6518 if (type_align < mode_align)
6519 return boolean_false_node;
6521 /* Check if a compare_and_swap pattern exists for the mode which represents
6522 the required size. The pattern is not allowed to fail, so the existence
6523 of the pattern indicates support is present. Also require that an
6524 atomic load exists for the required size. */
6525 if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
6526 return boolean_true_node;
6527 else
6528 return boolean_false_node;
6531 /* Return true if the parameters to call EXP represent an object which will
6532 always generate lock free instructions. The first argument represents the
6533 size of the object, and the second parameter is a pointer to the object
6534 itself. If NULL is passed for the object, then the result is based on
6535 typical alignment for an object of the specified size. Otherwise return
6536 false. */
6538 static rtx
6539 expand_builtin_atomic_always_lock_free (tree exp)
6541 tree size;
6542 tree arg0 = CALL_EXPR_ARG (exp, 0);
6543 tree arg1 = CALL_EXPR_ARG (exp, 1);
6545 if (TREE_CODE (arg0) != INTEGER_CST)
6547 error ("non-constant argument 1 to %qs", "__atomic_always_lock_free");
6548 return const0_rtx;
6551 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
6552 if (size == boolean_true_node)
6553 return const1_rtx;
6554 return const0_rtx;
6557 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6558 is lock free on this architecture. */
6560 static tree
6561 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
6563 if (!flag_inline_atomics)
6564 return NULL_TREE;
6566 /* If it isn't always lock free, don't generate a result. */
6567 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
6568 return boolean_true_node;
6570 return NULL_TREE;
6573 /* Return true if the parameters to call EXP represent an object which will
6574 always generate lock free instructions. The first argument represents the
6575 size of the object, and the second parameter is a pointer to the object
6576 itself. If NULL is passed for the object, then the result is based on
6577 typical alignment for an object of the specified size. Otherwise return
6578 NULL*/
6580 static rtx
6581 expand_builtin_atomic_is_lock_free (tree exp)
6583 tree size;
6584 tree arg0 = CALL_EXPR_ARG (exp, 0);
6585 tree arg1 = CALL_EXPR_ARG (exp, 1);
6587 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6589 error ("non-integer argument 1 to %qs", "__atomic_is_lock_free");
6590 return NULL_RTX;
6593 if (!flag_inline_atomics)
6594 return NULL_RTX;
6596 /* If the value is known at compile time, return the RTX for it. */
6597 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
6598 if (size == boolean_true_node)
6599 return const1_rtx;
6601 return NULL_RTX;
6604 /* Expand the __atomic_thread_fence intrinsic:
6605 void __atomic_thread_fence (enum memmodel)
6606 EXP is the CALL_EXPR. */
6608 static void
6609 expand_builtin_atomic_thread_fence (tree exp)
6611 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6612 expand_mem_thread_fence (model);
6615 /* Expand the __atomic_signal_fence intrinsic:
6616 void __atomic_signal_fence (enum memmodel)
6617 EXP is the CALL_EXPR. */
6619 static void
6620 expand_builtin_atomic_signal_fence (tree exp)
6622 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6623 expand_mem_signal_fence (model);
6626 /* Expand the __sync_synchronize intrinsic. */
6628 static void
6629 expand_builtin_sync_synchronize (void)
6631 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
6634 static rtx
6635 expand_builtin_thread_pointer (tree exp, rtx target)
6637 enum insn_code icode;
6638 if (!validate_arglist (exp, VOID_TYPE))
6639 return const0_rtx;
6640 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
6641 if (icode != CODE_FOR_nothing)
6643 class expand_operand op;
6644 /* If the target is not sutitable then create a new target. */
6645 if (target == NULL_RTX
6646 || !REG_P (target)
6647 || GET_MODE (target) != Pmode)
6648 target = gen_reg_rtx (Pmode);
6649 create_output_operand (&op, target, Pmode);
6650 expand_insn (icode, 1, &op);
6651 return target;
6653 error ("%<__builtin_thread_pointer%> is not supported on this target");
6654 return const0_rtx;
6657 static void
6658 expand_builtin_set_thread_pointer (tree exp)
6660 enum insn_code icode;
6661 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6662 return;
6663 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
6664 if (icode != CODE_FOR_nothing)
6666 class expand_operand op;
6667 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
6668 Pmode, EXPAND_NORMAL);
6669 create_input_operand (&op, val, Pmode);
6670 expand_insn (icode, 1, &op);
6671 return;
6673 error ("%<__builtin_set_thread_pointer%> is not supported on this target");
6677 /* Emit code to restore the current value of stack. */
6679 static void
6680 expand_stack_restore (tree var)
6682 rtx_insn *prev;
6683 rtx sa = expand_normal (var);
6685 sa = convert_memory_address (Pmode, sa);
6687 prev = get_last_insn ();
6688 emit_stack_restore (SAVE_BLOCK, sa);
6690 record_new_stack_level ();
6692 fixup_args_size_notes (prev, get_last_insn (), 0);
6695 /* Emit code to save the current value of stack. */
6697 static rtx
6698 expand_stack_save (void)
6700 rtx ret = NULL_RTX;
6702 emit_stack_save (SAVE_BLOCK, &ret);
6703 return ret;
6706 /* Emit code to get the openacc gang, worker or vector id or size. */
6708 static rtx
6709 expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
6711 const char *name;
6712 rtx fallback_retval;
6713 rtx_insn *(*gen_fn) (rtx, rtx);
6714 switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
6716 case BUILT_IN_GOACC_PARLEVEL_ID:
6717 name = "__builtin_goacc_parlevel_id";
6718 fallback_retval = const0_rtx;
6719 gen_fn = targetm.gen_oacc_dim_pos;
6720 break;
6721 case BUILT_IN_GOACC_PARLEVEL_SIZE:
6722 name = "__builtin_goacc_parlevel_size";
6723 fallback_retval = const1_rtx;
6724 gen_fn = targetm.gen_oacc_dim_size;
6725 break;
6726 default:
6727 gcc_unreachable ();
6730 if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
6732 error ("%qs only supported in OpenACC code", name);
6733 return const0_rtx;
6736 tree arg = CALL_EXPR_ARG (exp, 0);
6737 if (TREE_CODE (arg) != INTEGER_CST)
6739 error ("non-constant argument 0 to %qs", name);
6740 return const0_rtx;
6743 int dim = TREE_INT_CST_LOW (arg);
6744 switch (dim)
6746 case GOMP_DIM_GANG:
6747 case GOMP_DIM_WORKER:
6748 case GOMP_DIM_VECTOR:
6749 break;
6750 default:
6751 error ("illegal argument 0 to %qs", name);
6752 return const0_rtx;
6755 if (ignore)
6756 return target;
6758 if (target == NULL_RTX)
6759 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
6761 if (!targetm.have_oacc_dim_size ())
6763 emit_move_insn (target, fallback_retval);
6764 return target;
6767 rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
6768 emit_insn (gen_fn (reg, GEN_INT (dim)));
6769 if (reg != target)
6770 emit_move_insn (target, reg);
6772 return target;
6775 /* Expand a string compare operation using a sequence of char comparison
6776 to get rid of the calling overhead, with result going to TARGET if
6777 that's convenient.
6779 VAR_STR is the variable string source;
6780 CONST_STR is the constant string source;
6781 LENGTH is the number of chars to compare;
6782 CONST_STR_N indicates which source string is the constant string;
6783 IS_MEMCMP indicates whether it's a memcmp or strcmp.
6785 to: (assume const_str_n is 2, i.e., arg2 is a constant string)
6787 target = (int) (unsigned char) var_str[0]
6788 - (int) (unsigned char) const_str[0];
6789 if (target != 0)
6790 goto ne_label;
6792 target = (int) (unsigned char) var_str[length - 2]
6793 - (int) (unsigned char) const_str[length - 2];
6794 if (target != 0)
6795 goto ne_label;
6796 target = (int) (unsigned char) var_str[length - 1]
6797 - (int) (unsigned char) const_str[length - 1];
6798 ne_label:
6801 static rtx
6802 inline_string_cmp (rtx target, tree var_str, const char *const_str,
6803 unsigned HOST_WIDE_INT length,
6804 int const_str_n, machine_mode mode)
6806 HOST_WIDE_INT offset = 0;
6807 rtx var_rtx_array
6808 = get_memory_rtx (var_str, build_int_cst (unsigned_type_node,length));
6809 rtx var_rtx = NULL_RTX;
6810 rtx const_rtx = NULL_RTX;
6811 rtx result = target ? target : gen_reg_rtx (mode);
6812 rtx_code_label *ne_label = gen_label_rtx ();
6813 tree unit_type_node = unsigned_char_type_node;
6814 scalar_int_mode unit_mode
6815 = as_a <scalar_int_mode> TYPE_MODE (unit_type_node);
6817 start_sequence ();
6819 for (unsigned HOST_WIDE_INT i = 0; i < length; i++)
6821 var_rtx
6822 = adjust_address (var_rtx_array, TYPE_MODE (unit_type_node), offset);
6823 const_rtx = c_readstr (const_str + offset, unit_mode);
6824 rtx op0 = (const_str_n == 1) ? const_rtx : var_rtx;
6825 rtx op1 = (const_str_n == 1) ? var_rtx : const_rtx;
6827 op0 = convert_modes (mode, unit_mode, op0, 1);
6828 op1 = convert_modes (mode, unit_mode, op1, 1);
6829 result = expand_simple_binop (mode, MINUS, op0, op1,
6830 result, 1, OPTAB_WIDEN);
6831 if (i < length - 1)
6832 emit_cmp_and_jump_insns (result, CONST0_RTX (mode), NE, NULL_RTX,
6833 mode, true, ne_label);
6834 offset += GET_MODE_SIZE (unit_mode);
6837 emit_label (ne_label);
6838 rtx_insn *insns = get_insns ();
6839 end_sequence ();
6840 emit_insn (insns);
6842 return result;
6845 /* Inline expansion of a call to str(n)cmp and memcmp, with result going
6846 to TARGET if that's convenient.
6847 If the call is not been inlined, return NULL_RTX. */
6849 static rtx
6850 inline_expand_builtin_bytecmp (tree exp, rtx target)
6852 tree fndecl = get_callee_fndecl (exp);
6853 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6854 bool is_ncmp = (fcode == BUILT_IN_STRNCMP || fcode == BUILT_IN_MEMCMP);
6856 /* Do NOT apply this inlining expansion when optimizing for size or
6857 optimization level below 2. */
6858 if (optimize < 2 || optimize_insn_for_size_p ())
6859 return NULL_RTX;
6861 gcc_checking_assert (fcode == BUILT_IN_STRCMP
6862 || fcode == BUILT_IN_STRNCMP
6863 || fcode == BUILT_IN_MEMCMP);
6865 /* On a target where the type of the call (int) has same or narrower presicion
6866 than unsigned char, give up the inlining expansion. */
6867 if (TYPE_PRECISION (unsigned_char_type_node)
6868 >= TYPE_PRECISION (TREE_TYPE (exp)))
6869 return NULL_RTX;
6871 tree arg1 = CALL_EXPR_ARG (exp, 0);
6872 tree arg2 = CALL_EXPR_ARG (exp, 1);
6873 tree len3_tree = is_ncmp ? CALL_EXPR_ARG (exp, 2) : NULL_TREE;
6875 unsigned HOST_WIDE_INT len1 = 0;
6876 unsigned HOST_WIDE_INT len2 = 0;
6877 unsigned HOST_WIDE_INT len3 = 0;
6879 /* Get the object representation of the initializers of ARG1 and ARG2
6880 as strings, provided they refer to constant objects, with their byte
6881 sizes in LEN1 and LEN2, respectively. */
6882 const char *bytes1 = getbyterep (arg1, &len1);
6883 const char *bytes2 = getbyterep (arg2, &len2);
6885 /* Fail if neither argument refers to an initialized constant. */
6886 if (!bytes1 && !bytes2)
6887 return NULL_RTX;
6889 if (is_ncmp)
6891 /* Fail if the memcmp/strncmp bound is not a constant. */
6892 if (!tree_fits_uhwi_p (len3_tree))
6893 return NULL_RTX;
6895 len3 = tree_to_uhwi (len3_tree);
6897 if (fcode == BUILT_IN_MEMCMP)
6899 /* Fail if the memcmp bound is greater than the size of either
6900 of the two constant objects. */
6901 if ((bytes1 && len1 < len3)
6902 || (bytes2 && len2 < len3))
6903 return NULL_RTX;
6907 if (fcode != BUILT_IN_MEMCMP)
6909 /* For string functions (i.e., strcmp and strncmp) reduce LEN1
6910 and LEN2 to the length of the nul-terminated string stored
6911 in each. */
6912 if (bytes1 != NULL)
6913 len1 = strnlen (bytes1, len1) + 1;
6914 if (bytes2 != NULL)
6915 len2 = strnlen (bytes2, len2) + 1;
6918 /* See inline_string_cmp. */
6919 int const_str_n;
6920 if (!len1)
6921 const_str_n = 2;
6922 else if (!len2)
6923 const_str_n = 1;
6924 else if (len2 > len1)
6925 const_str_n = 1;
6926 else
6927 const_str_n = 2;
6929 /* For strncmp only, compute the new bound as the smallest of
6930 the lengths of the two strings (plus 1) and the bound provided
6931 to the function. */
6932 unsigned HOST_WIDE_INT bound = (const_str_n == 1) ? len1 : len2;
6933 if (is_ncmp && len3 < bound)
6934 bound = len3;
6936 /* If the bound of the comparison is larger than the threshold,
6937 do nothing. */
6938 if (bound > (unsigned HOST_WIDE_INT) param_builtin_string_cmp_inline_length)
6939 return NULL_RTX;
6941 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
6943 /* Now, start inline expansion the call. */
6944 return inline_string_cmp (target, (const_str_n == 1) ? arg2 : arg1,
6945 (const_str_n == 1) ? bytes1 : bytes2, bound,
6946 const_str_n, mode);
6949 /* Expand a call to __builtin_speculation_safe_value_<N>. MODE
6950 represents the size of the first argument to that call, or VOIDmode
6951 if the argument is a pointer. IGNORE will be true if the result
6952 isn't used. */
6953 static rtx
6954 expand_speculation_safe_value (machine_mode mode, tree exp, rtx target,
6955 bool ignore)
6957 rtx val, failsafe;
6958 unsigned nargs = call_expr_nargs (exp);
6960 tree arg0 = CALL_EXPR_ARG (exp, 0);
6962 if (mode == VOIDmode)
6964 mode = TYPE_MODE (TREE_TYPE (arg0));
6965 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
6968 val = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
6970 /* An optional second argument can be used as a failsafe value on
6971 some machines. If it isn't present, then the failsafe value is
6972 assumed to be 0. */
6973 if (nargs > 1)
6975 tree arg1 = CALL_EXPR_ARG (exp, 1);
6976 failsafe = expand_expr (arg1, NULL_RTX, mode, EXPAND_NORMAL);
6978 else
6979 failsafe = const0_rtx;
6981 /* If the result isn't used, the behavior is undefined. It would be
6982 nice to emit a warning here, but path splitting means this might
6983 happen with legitimate code. So simply drop the builtin
6984 expansion in that case; we've handled any side-effects above. */
6985 if (ignore)
6986 return const0_rtx;
6988 /* If we don't have a suitable target, create one to hold the result. */
6989 if (target == NULL || GET_MODE (target) != mode)
6990 target = gen_reg_rtx (mode);
6992 if (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode)
6993 val = convert_modes (mode, VOIDmode, val, false);
6995 return targetm.speculation_safe_value (mode, target, val, failsafe);
6998 /* Expand an expression EXP that calls a built-in function,
6999 with result going to TARGET if that's convenient
7000 (and in mode MODE if that's convenient).
7001 SUBTARGET may be used as the target for computing one of EXP's operands.
7002 IGNORE is nonzero if the value is to be ignored. */
7005 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
7006 int ignore)
7008 tree fndecl = get_callee_fndecl (exp);
7009 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
7010 int flags;
7012 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7013 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
7015 /* When ASan is enabled, we don't want to expand some memory/string
7016 builtins and rely on libsanitizer's hooks. This allows us to avoid
7017 redundant checks and be sure, that possible overflow will be detected
7018 by ASan. */
7020 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
7021 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
7022 return expand_call (exp, target, ignore);
7024 /* When not optimizing, generate calls to library functions for a certain
7025 set of builtins. */
7026 if (!optimize
7027 && !called_as_built_in (fndecl)
7028 && fcode != BUILT_IN_FORK
7029 && fcode != BUILT_IN_EXECL
7030 && fcode != BUILT_IN_EXECV
7031 && fcode != BUILT_IN_EXECLP
7032 && fcode != BUILT_IN_EXECLE
7033 && fcode != BUILT_IN_EXECVP
7034 && fcode != BUILT_IN_EXECVE
7035 && fcode != BUILT_IN_CLEAR_CACHE
7036 && !ALLOCA_FUNCTION_CODE_P (fcode)
7037 && fcode != BUILT_IN_FREE)
7038 return expand_call (exp, target, ignore);
7040 /* The built-in function expanders test for target == const0_rtx
7041 to determine whether the function's result will be ignored. */
7042 if (ignore)
7043 target = const0_rtx;
7045 /* If the result of a pure or const built-in function is ignored, and
7046 none of its arguments are volatile, we can avoid expanding the
7047 built-in call and just evaluate the arguments for side-effects. */
7048 if (target == const0_rtx
7049 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
7050 && !(flags & ECF_LOOPING_CONST_OR_PURE))
7052 bool volatilep = false;
7053 tree arg;
7054 call_expr_arg_iterator iter;
7056 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7057 if (TREE_THIS_VOLATILE (arg))
7059 volatilep = true;
7060 break;
7063 if (! volatilep)
7065 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
7066 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
7067 return const0_rtx;
7071 switch (fcode)
7073 CASE_FLT_FN (BUILT_IN_FABS):
7074 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
7075 case BUILT_IN_FABSD32:
7076 case BUILT_IN_FABSD64:
7077 case BUILT_IN_FABSD128:
7078 target = expand_builtin_fabs (exp, target, subtarget);
7079 if (target)
7080 return target;
7081 break;
7083 CASE_FLT_FN (BUILT_IN_COPYSIGN):
7084 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
7085 target = expand_builtin_copysign (exp, target, subtarget);
7086 if (target)
7087 return target;
7088 break;
7090 /* Just do a normal library call if we were unable to fold
7091 the values. */
7092 CASE_FLT_FN (BUILT_IN_CABS):
7093 break;
7095 CASE_FLT_FN (BUILT_IN_FMA):
7096 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
7097 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
7098 if (target)
7099 return target;
7100 break;
7102 CASE_FLT_FN (BUILT_IN_ILOGB):
7103 if (! flag_unsafe_math_optimizations)
7104 break;
7105 gcc_fallthrough ();
7106 CASE_FLT_FN (BUILT_IN_ISINF):
7107 CASE_FLT_FN (BUILT_IN_FINITE):
7108 case BUILT_IN_ISFINITE:
7109 case BUILT_IN_ISNORMAL:
7110 target = expand_builtin_interclass_mathfn (exp, target);
7111 if (target)
7112 return target;
7113 break;
7115 CASE_FLT_FN (BUILT_IN_ICEIL):
7116 CASE_FLT_FN (BUILT_IN_LCEIL):
7117 CASE_FLT_FN (BUILT_IN_LLCEIL):
7118 CASE_FLT_FN (BUILT_IN_LFLOOR):
7119 CASE_FLT_FN (BUILT_IN_IFLOOR):
7120 CASE_FLT_FN (BUILT_IN_LLFLOOR):
7121 target = expand_builtin_int_roundingfn (exp, target);
7122 if (target)
7123 return target;
7124 break;
7126 CASE_FLT_FN (BUILT_IN_IRINT):
7127 CASE_FLT_FN (BUILT_IN_LRINT):
7128 CASE_FLT_FN (BUILT_IN_LLRINT):
7129 CASE_FLT_FN (BUILT_IN_IROUND):
7130 CASE_FLT_FN (BUILT_IN_LROUND):
7131 CASE_FLT_FN (BUILT_IN_LLROUND):
7132 target = expand_builtin_int_roundingfn_2 (exp, target);
7133 if (target)
7134 return target;
7135 break;
7137 CASE_FLT_FN (BUILT_IN_POWI):
7138 target = expand_builtin_powi (exp, target);
7139 if (target)
7140 return target;
7141 break;
7143 CASE_FLT_FN (BUILT_IN_CEXPI):
7144 target = expand_builtin_cexpi (exp, target);
7145 gcc_assert (target);
7146 return target;
7148 CASE_FLT_FN (BUILT_IN_SIN):
7149 CASE_FLT_FN (BUILT_IN_COS):
7150 if (! flag_unsafe_math_optimizations)
7151 break;
7152 target = expand_builtin_mathfn_3 (exp, target, subtarget);
7153 if (target)
7154 return target;
7155 break;
7157 CASE_FLT_FN (BUILT_IN_SINCOS):
7158 if (! flag_unsafe_math_optimizations)
7159 break;
7160 target = expand_builtin_sincos (exp);
7161 if (target)
7162 return target;
7163 break;
7165 case BUILT_IN_FEGETROUND:
7166 target = expand_builtin_fegetround (exp, target, target_mode);
7167 if (target)
7168 return target;
7169 break;
7171 case BUILT_IN_FECLEAREXCEPT:
7172 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
7173 feclearexcept_optab);
7174 if (target)
7175 return target;
7176 break;
7178 case BUILT_IN_FERAISEEXCEPT:
7179 target = expand_builtin_feclear_feraise_except (exp, target, target_mode,
7180 feraiseexcept_optab);
7181 if (target)
7182 return target;
7183 break;
7185 case BUILT_IN_APPLY_ARGS:
7186 return expand_builtin_apply_args ();
7188 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
7189 FUNCTION with a copy of the parameters described by
7190 ARGUMENTS, and ARGSIZE. It returns a block of memory
7191 allocated on the stack into which is stored all the registers
7192 that might possibly be used for returning the result of a
7193 function. ARGUMENTS is the value returned by
7194 __builtin_apply_args. ARGSIZE is the number of bytes of
7195 arguments that must be copied. ??? How should this value be
7196 computed? We'll also need a safe worst case value for varargs
7197 functions. */
7198 case BUILT_IN_APPLY:
7199 if (!validate_arglist (exp, POINTER_TYPE,
7200 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
7201 && !validate_arglist (exp, REFERENCE_TYPE,
7202 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7203 return const0_rtx;
7204 else
7206 rtx ops[3];
7208 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
7209 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
7210 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
7212 return expand_builtin_apply (ops[0], ops[1], ops[2]);
7215 /* __builtin_return (RESULT) causes the function to return the
7216 value described by RESULT. RESULT is address of the block of
7217 memory returned by __builtin_apply. */
7218 case BUILT_IN_RETURN:
7219 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7220 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
7221 return const0_rtx;
7223 case BUILT_IN_SAVEREGS:
7224 return expand_builtin_saveregs ();
7226 case BUILT_IN_VA_ARG_PACK:
7227 /* All valid uses of __builtin_va_arg_pack () are removed during
7228 inlining. */
7229 error ("invalid use of %<__builtin_va_arg_pack ()%>");
7230 return const0_rtx;
7232 case BUILT_IN_VA_ARG_PACK_LEN:
7233 /* All valid uses of __builtin_va_arg_pack_len () are removed during
7234 inlining. */
7235 error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
7236 return const0_rtx;
7238 /* Return the address of the first anonymous stack arg. */
7239 case BUILT_IN_NEXT_ARG:
7240 if (fold_builtin_next_arg (exp, false))
7241 return const0_rtx;
7242 return expand_builtin_next_arg ();
7244 case BUILT_IN_CLEAR_CACHE:
7245 expand_builtin___clear_cache (exp);
7246 return const0_rtx;
7248 case BUILT_IN_CLASSIFY_TYPE:
7249 return expand_builtin_classify_type (exp);
7251 case BUILT_IN_CONSTANT_P:
7252 return const0_rtx;
7254 case BUILT_IN_FRAME_ADDRESS:
7255 case BUILT_IN_RETURN_ADDRESS:
7256 return expand_builtin_frame_address (fndecl, exp);
7258 /* Returns the address of the area where the structure is returned.
7259 0 otherwise. */
7260 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
7261 if (call_expr_nargs (exp) != 0
7262 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
7263 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
7264 return const0_rtx;
7265 else
7266 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
7268 CASE_BUILT_IN_ALLOCA:
7269 target = expand_builtin_alloca (exp);
7270 if (target)
7271 return target;
7272 break;
7274 case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
7275 return expand_asan_emit_allocas_unpoison (exp);
7277 case BUILT_IN_STACK_SAVE:
7278 return expand_stack_save ();
7280 case BUILT_IN_STACK_RESTORE:
7281 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
7282 return const0_rtx;
7284 case BUILT_IN_BSWAP16:
7285 case BUILT_IN_BSWAP32:
7286 case BUILT_IN_BSWAP64:
7287 case BUILT_IN_BSWAP128:
7288 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
7289 if (target)
7290 return target;
7291 break;
7293 CASE_INT_FN (BUILT_IN_FFS):
7294 target = expand_builtin_unop (target_mode, exp, target,
7295 subtarget, ffs_optab);
7296 if (target)
7297 return target;
7298 break;
7300 CASE_INT_FN (BUILT_IN_CLZ):
7301 target = expand_builtin_unop (target_mode, exp, target,
7302 subtarget, clz_optab);
7303 if (target)
7304 return target;
7305 break;
7307 CASE_INT_FN (BUILT_IN_CTZ):
7308 target = expand_builtin_unop (target_mode, exp, target,
7309 subtarget, ctz_optab);
7310 if (target)
7311 return target;
7312 break;
7314 CASE_INT_FN (BUILT_IN_CLRSB):
7315 target = expand_builtin_unop (target_mode, exp, target,
7316 subtarget, clrsb_optab);
7317 if (target)
7318 return target;
7319 break;
7321 CASE_INT_FN (BUILT_IN_POPCOUNT):
7322 target = expand_builtin_unop (target_mode, exp, target,
7323 subtarget, popcount_optab);
7324 if (target)
7325 return target;
7326 break;
7328 CASE_INT_FN (BUILT_IN_PARITY):
7329 target = expand_builtin_unop (target_mode, exp, target,
7330 subtarget, parity_optab);
7331 if (target)
7332 return target;
7333 break;
7335 case BUILT_IN_STRLEN:
7336 target = expand_builtin_strlen (exp, target, target_mode);
7337 if (target)
7338 return target;
7339 break;
7341 case BUILT_IN_STRNLEN:
7342 target = expand_builtin_strnlen (exp, target, target_mode);
7343 if (target)
7344 return target;
7345 break;
7347 case BUILT_IN_STRCPY:
7348 target = expand_builtin_strcpy (exp, target);
7349 if (target)
7350 return target;
7351 break;
7353 case BUILT_IN_STRNCPY:
7354 target = expand_builtin_strncpy (exp, target);
7355 if (target)
7356 return target;
7357 break;
7359 case BUILT_IN_STPCPY:
7360 target = expand_builtin_stpcpy (exp, target, mode);
7361 if (target)
7362 return target;
7363 break;
7365 case BUILT_IN_MEMCPY:
7366 target = expand_builtin_memcpy (exp, target);
7367 if (target)
7368 return target;
7369 break;
7371 case BUILT_IN_MEMMOVE:
7372 target = expand_builtin_memmove (exp, target);
7373 if (target)
7374 return target;
7375 break;
7377 case BUILT_IN_MEMPCPY:
7378 target = expand_builtin_mempcpy (exp, target);
7379 if (target)
7380 return target;
7381 break;
7383 case BUILT_IN_MEMSET:
7384 target = expand_builtin_memset (exp, target, mode);
7385 if (target)
7386 return target;
7387 break;
7389 case BUILT_IN_BZERO:
7390 target = expand_builtin_bzero (exp);
7391 if (target)
7392 return target;
7393 break;
7395 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7396 back to a BUILT_IN_STRCMP. Remember to delete the 3rd parameter
7397 when changing it to a strcmp call. */
7398 case BUILT_IN_STRCMP_EQ:
7399 target = expand_builtin_memcmp (exp, target, true);
7400 if (target)
7401 return target;
7403 /* Change this call back to a BUILT_IN_STRCMP. */
7404 TREE_OPERAND (exp, 1)
7405 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
7407 /* Delete the last parameter. */
7408 unsigned int i;
7409 vec<tree, va_gc> *arg_vec;
7410 vec_alloc (arg_vec, 2);
7411 for (i = 0; i < 2; i++)
7412 arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
7413 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
7414 /* FALLTHROUGH */
7416 case BUILT_IN_STRCMP:
7417 target = expand_builtin_strcmp (exp, target);
7418 if (target)
7419 return target;
7420 break;
7422 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7423 back to a BUILT_IN_STRNCMP. */
7424 case BUILT_IN_STRNCMP_EQ:
7425 target = expand_builtin_memcmp (exp, target, true);
7426 if (target)
7427 return target;
7429 /* Change it back to a BUILT_IN_STRNCMP. */
7430 TREE_OPERAND (exp, 1)
7431 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
7432 /* FALLTHROUGH */
7434 case BUILT_IN_STRNCMP:
7435 target = expand_builtin_strncmp (exp, target, mode);
7436 if (target)
7437 return target;
7438 break;
7440 case BUILT_IN_BCMP:
7441 case BUILT_IN_MEMCMP:
7442 case BUILT_IN_MEMCMP_EQ:
7443 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
7444 if (target)
7445 return target;
7446 if (fcode == BUILT_IN_MEMCMP_EQ)
7448 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
7449 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
7451 break;
7453 case BUILT_IN_SETJMP:
7454 /* This should have been lowered to the builtins below. */
7455 gcc_unreachable ();
7457 case BUILT_IN_SETJMP_SETUP:
7458 /* __builtin_setjmp_setup is passed a pointer to an array of five words
7459 and the receiver label. */
7460 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7462 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7463 VOIDmode, EXPAND_NORMAL);
7464 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
7465 rtx_insn *label_r = label_rtx (label);
7467 /* This is copied from the handling of non-local gotos. */
7468 expand_builtin_setjmp_setup (buf_addr, label_r);
7469 nonlocal_goto_handler_labels
7470 = gen_rtx_INSN_LIST (VOIDmode, label_r,
7471 nonlocal_goto_handler_labels);
7472 /* ??? Do not let expand_label treat us as such since we would
7473 not want to be both on the list of non-local labels and on
7474 the list of forced labels. */
7475 FORCED_LABEL (label) = 0;
7476 return const0_rtx;
7478 break;
7480 case BUILT_IN_SETJMP_RECEIVER:
7481 /* __builtin_setjmp_receiver is passed the receiver label. */
7482 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7484 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
7485 rtx_insn *label_r = label_rtx (label);
7487 expand_builtin_setjmp_receiver (label_r);
7488 return const0_rtx;
7490 break;
7492 /* __builtin_longjmp is passed a pointer to an array of five words.
7493 It's similar to the C library longjmp function but works with
7494 __builtin_setjmp above. */
7495 case BUILT_IN_LONGJMP:
7496 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7498 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7499 VOIDmode, EXPAND_NORMAL);
7500 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
7502 if (value != const1_rtx)
7504 error ("%<__builtin_longjmp%> second argument must be 1");
7505 return const0_rtx;
7508 expand_builtin_longjmp (buf_addr, value);
7509 return const0_rtx;
7511 break;
7513 case BUILT_IN_NONLOCAL_GOTO:
7514 target = expand_builtin_nonlocal_goto (exp);
7515 if (target)
7516 return target;
7517 break;
7519 /* This updates the setjmp buffer that is its argument with the value
7520 of the current stack pointer. */
7521 case BUILT_IN_UPDATE_SETJMP_BUF:
7522 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7524 rtx buf_addr
7525 = expand_normal (CALL_EXPR_ARG (exp, 0));
7527 expand_builtin_update_setjmp_buf (buf_addr);
7528 return const0_rtx;
7530 break;
7532 case BUILT_IN_TRAP:
7533 expand_builtin_trap ();
7534 return const0_rtx;
7536 case BUILT_IN_UNREACHABLE:
7537 expand_builtin_unreachable ();
7538 return const0_rtx;
7540 CASE_FLT_FN (BUILT_IN_SIGNBIT):
7541 case BUILT_IN_SIGNBITD32:
7542 case BUILT_IN_SIGNBITD64:
7543 case BUILT_IN_SIGNBITD128:
7544 target = expand_builtin_signbit (exp, target);
7545 if (target)
7546 return target;
7547 break;
7549 /* Various hooks for the DWARF 2 __throw routine. */
7550 case BUILT_IN_UNWIND_INIT:
7551 expand_builtin_unwind_init ();
7552 return const0_rtx;
7553 case BUILT_IN_DWARF_CFA:
7554 return virtual_cfa_rtx;
7555 #ifdef DWARF2_UNWIND_INFO
7556 case BUILT_IN_DWARF_SP_COLUMN:
7557 return expand_builtin_dwarf_sp_column ();
7558 case BUILT_IN_INIT_DWARF_REG_SIZES:
7559 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
7560 return const0_rtx;
7561 #endif
7562 case BUILT_IN_FROB_RETURN_ADDR:
7563 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
7564 case BUILT_IN_EXTRACT_RETURN_ADDR:
7565 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
7566 case BUILT_IN_EH_RETURN:
7567 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
7568 CALL_EXPR_ARG (exp, 1));
7569 return const0_rtx;
7570 case BUILT_IN_EH_RETURN_DATA_REGNO:
7571 return expand_builtin_eh_return_data_regno (exp);
7572 case BUILT_IN_EXTEND_POINTER:
7573 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
7574 case BUILT_IN_EH_POINTER:
7575 return expand_builtin_eh_pointer (exp);
7576 case BUILT_IN_EH_FILTER:
7577 return expand_builtin_eh_filter (exp);
7578 case BUILT_IN_EH_COPY_VALUES:
7579 return expand_builtin_eh_copy_values (exp);
7581 case BUILT_IN_VA_START:
7582 return expand_builtin_va_start (exp);
7583 case BUILT_IN_VA_END:
7584 return expand_builtin_va_end (exp);
7585 case BUILT_IN_VA_COPY:
7586 return expand_builtin_va_copy (exp);
7587 case BUILT_IN_EXPECT:
7588 return expand_builtin_expect (exp, target);
7589 case BUILT_IN_EXPECT_WITH_PROBABILITY:
7590 return expand_builtin_expect_with_probability (exp, target);
7591 case BUILT_IN_ASSUME_ALIGNED:
7592 return expand_builtin_assume_aligned (exp, target);
7593 case BUILT_IN_PREFETCH:
7594 expand_builtin_prefetch (exp);
7595 return const0_rtx;
7597 case BUILT_IN_INIT_TRAMPOLINE:
7598 return expand_builtin_init_trampoline (exp, true);
7599 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
7600 return expand_builtin_init_trampoline (exp, false);
7601 case BUILT_IN_ADJUST_TRAMPOLINE:
7602 return expand_builtin_adjust_trampoline (exp);
7604 case BUILT_IN_INIT_DESCRIPTOR:
7605 return expand_builtin_init_descriptor (exp);
7606 case BUILT_IN_ADJUST_DESCRIPTOR:
7607 return expand_builtin_adjust_descriptor (exp);
7609 case BUILT_IN_FORK:
7610 case BUILT_IN_EXECL:
7611 case BUILT_IN_EXECV:
7612 case BUILT_IN_EXECLP:
7613 case BUILT_IN_EXECLE:
7614 case BUILT_IN_EXECVP:
7615 case BUILT_IN_EXECVE:
7616 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
7617 if (target)
7618 return target;
7619 break;
7621 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
7622 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
7623 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
7624 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
7625 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
7626 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
7627 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
7628 if (target)
7629 return target;
7630 break;
7632 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
7633 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
7634 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
7635 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
7636 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
7637 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
7638 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
7639 if (target)
7640 return target;
7641 break;
7643 case BUILT_IN_SYNC_FETCH_AND_OR_1:
7644 case BUILT_IN_SYNC_FETCH_AND_OR_2:
7645 case BUILT_IN_SYNC_FETCH_AND_OR_4:
7646 case BUILT_IN_SYNC_FETCH_AND_OR_8:
7647 case BUILT_IN_SYNC_FETCH_AND_OR_16:
7648 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
7649 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
7650 if (target)
7651 return target;
7652 break;
7654 case BUILT_IN_SYNC_FETCH_AND_AND_1:
7655 case BUILT_IN_SYNC_FETCH_AND_AND_2:
7656 case BUILT_IN_SYNC_FETCH_AND_AND_4:
7657 case BUILT_IN_SYNC_FETCH_AND_AND_8:
7658 case BUILT_IN_SYNC_FETCH_AND_AND_16:
7659 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
7660 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
7661 if (target)
7662 return target;
7663 break;
7665 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
7666 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
7667 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
7668 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
7669 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
7670 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
7671 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
7672 if (target)
7673 return target;
7674 break;
7676 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
7677 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
7678 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
7679 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
7680 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
7681 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
7682 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
7683 if (target)
7684 return target;
7685 break;
7687 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
7688 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
7689 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
7690 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
7691 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
7692 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
7693 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
7694 if (target)
7695 return target;
7696 break;
7698 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
7699 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
7700 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
7701 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
7702 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
7703 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
7704 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
7705 if (target)
7706 return target;
7707 break;
7709 case BUILT_IN_SYNC_OR_AND_FETCH_1:
7710 case BUILT_IN_SYNC_OR_AND_FETCH_2:
7711 case BUILT_IN_SYNC_OR_AND_FETCH_4:
7712 case BUILT_IN_SYNC_OR_AND_FETCH_8:
7713 case BUILT_IN_SYNC_OR_AND_FETCH_16:
7714 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
7715 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
7716 if (target)
7717 return target;
7718 break;
7720 case BUILT_IN_SYNC_AND_AND_FETCH_1:
7721 case BUILT_IN_SYNC_AND_AND_FETCH_2:
7722 case BUILT_IN_SYNC_AND_AND_FETCH_4:
7723 case BUILT_IN_SYNC_AND_AND_FETCH_8:
7724 case BUILT_IN_SYNC_AND_AND_FETCH_16:
7725 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
7726 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
7727 if (target)
7728 return target;
7729 break;
7731 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
7732 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
7733 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
7734 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
7735 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
7736 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
7737 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
7738 if (target)
7739 return target;
7740 break;
7742 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
7743 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
7744 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
7745 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
7746 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
7747 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
7748 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
7749 if (target)
7750 return target;
7751 break;
7753 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
7754 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
7755 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
7756 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
7757 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
7758 if (mode == VOIDmode)
7759 mode = TYPE_MODE (boolean_type_node);
7760 if (!target || !register_operand (target, mode))
7761 target = gen_reg_rtx (mode);
7763 mode = get_builtin_sync_mode
7764 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
7765 target = expand_builtin_compare_and_swap (mode, exp, true, target);
7766 if (target)
7767 return target;
7768 break;
7770 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
7771 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
7772 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
7773 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
7774 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
7775 mode = get_builtin_sync_mode
7776 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
7777 target = expand_builtin_compare_and_swap (mode, exp, false, target);
7778 if (target)
7779 return target;
7780 break;
7782 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
7783 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
7784 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
7785 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
7786 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
7787 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
7788 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
7789 if (target)
7790 return target;
7791 break;
7793 case BUILT_IN_SYNC_LOCK_RELEASE_1:
7794 case BUILT_IN_SYNC_LOCK_RELEASE_2:
7795 case BUILT_IN_SYNC_LOCK_RELEASE_4:
7796 case BUILT_IN_SYNC_LOCK_RELEASE_8:
7797 case BUILT_IN_SYNC_LOCK_RELEASE_16:
7798 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
7799 expand_builtin_sync_lock_release (mode, exp);
7800 return const0_rtx;
7802 case BUILT_IN_SYNC_SYNCHRONIZE:
7803 expand_builtin_sync_synchronize ();
7804 return const0_rtx;
7806 case BUILT_IN_ATOMIC_EXCHANGE_1:
7807 case BUILT_IN_ATOMIC_EXCHANGE_2:
7808 case BUILT_IN_ATOMIC_EXCHANGE_4:
7809 case BUILT_IN_ATOMIC_EXCHANGE_8:
7810 case BUILT_IN_ATOMIC_EXCHANGE_16:
7811 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
7812 target = expand_builtin_atomic_exchange (mode, exp, target);
7813 if (target)
7814 return target;
7815 break;
7817 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
7818 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
7819 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
7820 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
7821 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
7823 unsigned int nargs, z;
7824 vec<tree, va_gc> *vec;
7826 mode =
7827 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
7828 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
7829 if (target)
7830 return target;
7832 /* If this is turned into an external library call, the weak parameter
7833 must be dropped to match the expected parameter list. */
7834 nargs = call_expr_nargs (exp);
7835 vec_alloc (vec, nargs - 1);
7836 for (z = 0; z < 3; z++)
7837 vec->quick_push (CALL_EXPR_ARG (exp, z));
7838 /* Skip the boolean weak parameter. */
7839 for (z = 4; z < 6; z++)
7840 vec->quick_push (CALL_EXPR_ARG (exp, z));
7841 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
7842 break;
7845 case BUILT_IN_ATOMIC_LOAD_1:
7846 case BUILT_IN_ATOMIC_LOAD_2:
7847 case BUILT_IN_ATOMIC_LOAD_4:
7848 case BUILT_IN_ATOMIC_LOAD_8:
7849 case BUILT_IN_ATOMIC_LOAD_16:
7850 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
7851 target = expand_builtin_atomic_load (mode, exp, target);
7852 if (target)
7853 return target;
7854 break;
7856 case BUILT_IN_ATOMIC_STORE_1:
7857 case BUILT_IN_ATOMIC_STORE_2:
7858 case BUILT_IN_ATOMIC_STORE_4:
7859 case BUILT_IN_ATOMIC_STORE_8:
7860 case BUILT_IN_ATOMIC_STORE_16:
7861 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
7862 target = expand_builtin_atomic_store (mode, exp);
7863 if (target)
7864 return const0_rtx;
7865 break;
7867 case BUILT_IN_ATOMIC_ADD_FETCH_1:
7868 case BUILT_IN_ATOMIC_ADD_FETCH_2:
7869 case BUILT_IN_ATOMIC_ADD_FETCH_4:
7870 case BUILT_IN_ATOMIC_ADD_FETCH_8:
7871 case BUILT_IN_ATOMIC_ADD_FETCH_16:
7873 enum built_in_function lib;
7874 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
7875 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
7876 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
7877 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
7878 ignore, lib);
7879 if (target)
7880 return target;
7881 break;
7883 case BUILT_IN_ATOMIC_SUB_FETCH_1:
7884 case BUILT_IN_ATOMIC_SUB_FETCH_2:
7885 case BUILT_IN_ATOMIC_SUB_FETCH_4:
7886 case BUILT_IN_ATOMIC_SUB_FETCH_8:
7887 case BUILT_IN_ATOMIC_SUB_FETCH_16:
7889 enum built_in_function lib;
7890 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
7891 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
7892 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
7893 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
7894 ignore, lib);
7895 if (target)
7896 return target;
7897 break;
7899 case BUILT_IN_ATOMIC_AND_FETCH_1:
7900 case BUILT_IN_ATOMIC_AND_FETCH_2:
7901 case BUILT_IN_ATOMIC_AND_FETCH_4:
7902 case BUILT_IN_ATOMIC_AND_FETCH_8:
7903 case BUILT_IN_ATOMIC_AND_FETCH_16:
7905 enum built_in_function lib;
7906 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
7907 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
7908 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
7909 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
7910 ignore, lib);
7911 if (target)
7912 return target;
7913 break;
7915 case BUILT_IN_ATOMIC_NAND_FETCH_1:
7916 case BUILT_IN_ATOMIC_NAND_FETCH_2:
7917 case BUILT_IN_ATOMIC_NAND_FETCH_4:
7918 case BUILT_IN_ATOMIC_NAND_FETCH_8:
7919 case BUILT_IN_ATOMIC_NAND_FETCH_16:
7921 enum built_in_function lib;
7922 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
7923 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
7924 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
7925 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
7926 ignore, lib);
7927 if (target)
7928 return target;
7929 break;
7931 case BUILT_IN_ATOMIC_XOR_FETCH_1:
7932 case BUILT_IN_ATOMIC_XOR_FETCH_2:
7933 case BUILT_IN_ATOMIC_XOR_FETCH_4:
7934 case BUILT_IN_ATOMIC_XOR_FETCH_8:
7935 case BUILT_IN_ATOMIC_XOR_FETCH_16:
7937 enum built_in_function lib;
7938 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
7939 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
7940 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
7941 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
7942 ignore, lib);
7943 if (target)
7944 return target;
7945 break;
7947 case BUILT_IN_ATOMIC_OR_FETCH_1:
7948 case BUILT_IN_ATOMIC_OR_FETCH_2:
7949 case BUILT_IN_ATOMIC_OR_FETCH_4:
7950 case BUILT_IN_ATOMIC_OR_FETCH_8:
7951 case BUILT_IN_ATOMIC_OR_FETCH_16:
7953 enum built_in_function lib;
7954 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
7955 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
7956 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
7957 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
7958 ignore, lib);
7959 if (target)
7960 return target;
7961 break;
7963 case BUILT_IN_ATOMIC_FETCH_ADD_1:
7964 case BUILT_IN_ATOMIC_FETCH_ADD_2:
7965 case BUILT_IN_ATOMIC_FETCH_ADD_4:
7966 case BUILT_IN_ATOMIC_FETCH_ADD_8:
7967 case BUILT_IN_ATOMIC_FETCH_ADD_16:
7968 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
7969 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
7970 ignore, BUILT_IN_NONE);
7971 if (target)
7972 return target;
7973 break;
7975 case BUILT_IN_ATOMIC_FETCH_SUB_1:
7976 case BUILT_IN_ATOMIC_FETCH_SUB_2:
7977 case BUILT_IN_ATOMIC_FETCH_SUB_4:
7978 case BUILT_IN_ATOMIC_FETCH_SUB_8:
7979 case BUILT_IN_ATOMIC_FETCH_SUB_16:
7980 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
7981 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
7982 ignore, BUILT_IN_NONE);
7983 if (target)
7984 return target;
7985 break;
7987 case BUILT_IN_ATOMIC_FETCH_AND_1:
7988 case BUILT_IN_ATOMIC_FETCH_AND_2:
7989 case BUILT_IN_ATOMIC_FETCH_AND_4:
7990 case BUILT_IN_ATOMIC_FETCH_AND_8:
7991 case BUILT_IN_ATOMIC_FETCH_AND_16:
7992 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
7993 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
7994 ignore, BUILT_IN_NONE);
7995 if (target)
7996 return target;
7997 break;
7999 case BUILT_IN_ATOMIC_FETCH_NAND_1:
8000 case BUILT_IN_ATOMIC_FETCH_NAND_2:
8001 case BUILT_IN_ATOMIC_FETCH_NAND_4:
8002 case BUILT_IN_ATOMIC_FETCH_NAND_8:
8003 case BUILT_IN_ATOMIC_FETCH_NAND_16:
8004 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
8005 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
8006 ignore, BUILT_IN_NONE);
8007 if (target)
8008 return target;
8009 break;
8011 case BUILT_IN_ATOMIC_FETCH_XOR_1:
8012 case BUILT_IN_ATOMIC_FETCH_XOR_2:
8013 case BUILT_IN_ATOMIC_FETCH_XOR_4:
8014 case BUILT_IN_ATOMIC_FETCH_XOR_8:
8015 case BUILT_IN_ATOMIC_FETCH_XOR_16:
8016 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
8017 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
8018 ignore, BUILT_IN_NONE);
8019 if (target)
8020 return target;
8021 break;
8023 case BUILT_IN_ATOMIC_FETCH_OR_1:
8024 case BUILT_IN_ATOMIC_FETCH_OR_2:
8025 case BUILT_IN_ATOMIC_FETCH_OR_4:
8026 case BUILT_IN_ATOMIC_FETCH_OR_8:
8027 case BUILT_IN_ATOMIC_FETCH_OR_16:
8028 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
8029 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
8030 ignore, BUILT_IN_NONE);
8031 if (target)
8032 return target;
8033 break;
8035 case BUILT_IN_ATOMIC_TEST_AND_SET:
8036 return expand_builtin_atomic_test_and_set (exp, target);
8038 case BUILT_IN_ATOMIC_CLEAR:
8039 return expand_builtin_atomic_clear (exp);
8041 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
8042 return expand_builtin_atomic_always_lock_free (exp);
8044 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
8045 target = expand_builtin_atomic_is_lock_free (exp);
8046 if (target)
8047 return target;
8048 break;
8050 case BUILT_IN_ATOMIC_THREAD_FENCE:
8051 expand_builtin_atomic_thread_fence (exp);
8052 return const0_rtx;
8054 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
8055 expand_builtin_atomic_signal_fence (exp);
8056 return const0_rtx;
8058 case BUILT_IN_OBJECT_SIZE:
8059 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
8060 return expand_builtin_object_size (exp);
8062 case BUILT_IN_MEMCPY_CHK:
8063 case BUILT_IN_MEMPCPY_CHK:
8064 case BUILT_IN_MEMMOVE_CHK:
8065 case BUILT_IN_MEMSET_CHK:
8066 target = expand_builtin_memory_chk (exp, target, mode, fcode);
8067 if (target)
8068 return target;
8069 break;
8071 case BUILT_IN_STRCPY_CHK:
8072 case BUILT_IN_STPCPY_CHK:
8073 case BUILT_IN_STRNCPY_CHK:
8074 case BUILT_IN_STPNCPY_CHK:
8075 case BUILT_IN_STRCAT_CHK:
8076 case BUILT_IN_STRNCAT_CHK:
8077 case BUILT_IN_SNPRINTF_CHK:
8078 case BUILT_IN_VSNPRINTF_CHK:
8079 maybe_emit_chk_warning (exp, fcode);
8080 break;
8082 case BUILT_IN_SPRINTF_CHK:
8083 case BUILT_IN_VSPRINTF_CHK:
8084 maybe_emit_sprintf_chk_warning (exp, fcode);
8085 break;
8087 case BUILT_IN_THREAD_POINTER:
8088 return expand_builtin_thread_pointer (exp, target);
8090 case BUILT_IN_SET_THREAD_POINTER:
8091 expand_builtin_set_thread_pointer (exp);
8092 return const0_rtx;
8094 case BUILT_IN_ACC_ON_DEVICE:
8095 /* Do library call, if we failed to expand the builtin when
8096 folding. */
8097 break;
8099 case BUILT_IN_GOACC_PARLEVEL_ID:
8100 case BUILT_IN_GOACC_PARLEVEL_SIZE:
8101 return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
8103 case BUILT_IN_SPECULATION_SAFE_VALUE_PTR:
8104 return expand_speculation_safe_value (VOIDmode, exp, target, ignore);
8106 case BUILT_IN_SPECULATION_SAFE_VALUE_1:
8107 case BUILT_IN_SPECULATION_SAFE_VALUE_2:
8108 case BUILT_IN_SPECULATION_SAFE_VALUE_4:
8109 case BUILT_IN_SPECULATION_SAFE_VALUE_8:
8110 case BUILT_IN_SPECULATION_SAFE_VALUE_16:
8111 mode = get_builtin_sync_mode (fcode - BUILT_IN_SPECULATION_SAFE_VALUE_1);
8112 return expand_speculation_safe_value (mode, exp, target, ignore);
8114 default: /* just do library call, if unknown builtin */
8115 break;
8118 /* The switch statement above can drop through to cause the function
8119 to be called normally. */
8120 return expand_call (exp, target, ignore);
8123 /* Determine whether a tree node represents a call to a built-in
8124 function. If the tree T is a call to a built-in function with
8125 the right number of arguments of the appropriate types, return
8126 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
8127 Otherwise the return value is END_BUILTINS. */
8129 enum built_in_function
8130 builtin_mathfn_code (const_tree t)
8132 const_tree fndecl, arg, parmlist;
8133 const_tree argtype, parmtype;
8134 const_call_expr_arg_iterator iter;
8136 if (TREE_CODE (t) != CALL_EXPR)
8137 return END_BUILTINS;
8139 fndecl = get_callee_fndecl (t);
8140 if (fndecl == NULL_TREE || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
8141 return END_BUILTINS;
8143 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
8144 init_const_call_expr_arg_iterator (t, &iter);
8145 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
8147 /* If a function doesn't take a variable number of arguments,
8148 the last element in the list will have type `void'. */
8149 parmtype = TREE_VALUE (parmlist);
8150 if (VOID_TYPE_P (parmtype))
8152 if (more_const_call_expr_args_p (&iter))
8153 return END_BUILTINS;
8154 return DECL_FUNCTION_CODE (fndecl);
8157 if (! more_const_call_expr_args_p (&iter))
8158 return END_BUILTINS;
8160 arg = next_const_call_expr_arg (&iter);
8161 argtype = TREE_TYPE (arg);
8163 if (SCALAR_FLOAT_TYPE_P (parmtype))
8165 if (! SCALAR_FLOAT_TYPE_P (argtype))
8166 return END_BUILTINS;
8168 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
8170 if (! COMPLEX_FLOAT_TYPE_P (argtype))
8171 return END_BUILTINS;
8173 else if (POINTER_TYPE_P (parmtype))
8175 if (! POINTER_TYPE_P (argtype))
8176 return END_BUILTINS;
8178 else if (INTEGRAL_TYPE_P (parmtype))
8180 if (! INTEGRAL_TYPE_P (argtype))
8181 return END_BUILTINS;
8183 else
8184 return END_BUILTINS;
8187 /* Variable-length argument list. */
8188 return DECL_FUNCTION_CODE (fndecl);
8191 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
8192 evaluate to a constant. */
8194 static tree
8195 fold_builtin_constant_p (tree arg)
8197 /* We return 1 for a numeric type that's known to be a constant
8198 value at compile-time or for an aggregate type that's a
8199 literal constant. */
8200 STRIP_NOPS (arg);
8202 /* If we know this is a constant, emit the constant of one. */
8203 if (CONSTANT_CLASS_P (arg)
8204 || (TREE_CODE (arg) == CONSTRUCTOR
8205 && TREE_CONSTANT (arg)))
8206 return integer_one_node;
8207 if (TREE_CODE (arg) == ADDR_EXPR)
8209 tree op = TREE_OPERAND (arg, 0);
8210 if (TREE_CODE (op) == STRING_CST
8211 || (TREE_CODE (op) == ARRAY_REF
8212 && integer_zerop (TREE_OPERAND (op, 1))
8213 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
8214 return integer_one_node;
8217 /* If this expression has side effects, show we don't know it to be a
8218 constant. Likewise if it's a pointer or aggregate type since in
8219 those case we only want literals, since those are only optimized
8220 when generating RTL, not later.
8221 And finally, if we are compiling an initializer, not code, we
8222 need to return a definite result now; there's not going to be any
8223 more optimization done. */
8224 if (TREE_SIDE_EFFECTS (arg)
8225 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
8226 || POINTER_TYPE_P (TREE_TYPE (arg))
8227 || cfun == 0
8228 || folding_initializer
8229 || force_folding_builtin_constant_p)
8230 return integer_zero_node;
8232 return NULL_TREE;
8235 /* Create builtin_expect or builtin_expect_with_probability
8236 with PRED and EXPECTED as its arguments and return it as a truthvalue.
8237 Fortran FE can also produce builtin_expect with PREDICTOR as third argument.
8238 builtin_expect_with_probability instead uses third argument as PROBABILITY
8239 value. */
8241 static tree
8242 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
8243 tree predictor, tree probability)
8245 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
8247 fn = builtin_decl_explicit (probability == NULL_TREE ? BUILT_IN_EXPECT
8248 : BUILT_IN_EXPECT_WITH_PROBABILITY);
8249 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
8250 ret_type = TREE_TYPE (TREE_TYPE (fn));
8251 pred_type = TREE_VALUE (arg_types);
8252 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
8254 pred = fold_convert_loc (loc, pred_type, pred);
8255 expected = fold_convert_loc (loc, expected_type, expected);
8257 if (probability)
8258 call_expr = build_call_expr_loc (loc, fn, 3, pred, expected, probability);
8259 else
8260 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
8261 predictor);
8263 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
8264 build_int_cst (ret_type, 0));
8267 /* Fold a call to builtin_expect with arguments ARG0, ARG1, ARG2, ARG3. Return
8268 NULL_TREE if no simplification is possible. */
8270 tree
8271 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2,
8272 tree arg3)
8274 tree inner, fndecl, inner_arg0;
8275 enum tree_code code;
8277 /* Distribute the expected value over short-circuiting operators.
8278 See through the cast from truthvalue_type_node to long. */
8279 inner_arg0 = arg0;
8280 while (CONVERT_EXPR_P (inner_arg0)
8281 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
8282 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
8283 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
8285 /* If this is a builtin_expect within a builtin_expect keep the
8286 inner one. See through a comparison against a constant. It
8287 might have been added to create a thruthvalue. */
8288 inner = inner_arg0;
8290 if (COMPARISON_CLASS_P (inner)
8291 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
8292 inner = TREE_OPERAND (inner, 0);
8294 if (TREE_CODE (inner) == CALL_EXPR
8295 && (fndecl = get_callee_fndecl (inner))
8296 && (fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
8297 || fndecl_built_in_p (fndecl, BUILT_IN_EXPECT_WITH_PROBABILITY)))
8298 return arg0;
8300 inner = inner_arg0;
8301 code = TREE_CODE (inner);
8302 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
8304 tree op0 = TREE_OPERAND (inner, 0);
8305 tree op1 = TREE_OPERAND (inner, 1);
8306 arg1 = save_expr (arg1);
8308 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2, arg3);
8309 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2, arg3);
8310 inner = build2 (code, TREE_TYPE (inner), op0, op1);
8312 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
8315 /* If the argument isn't invariant then there's nothing else we can do. */
8316 if (!TREE_CONSTANT (inner_arg0))
8317 return NULL_TREE;
8319 /* If we expect that a comparison against the argument will fold to
8320 a constant return the constant. In practice, this means a true
8321 constant or the address of a non-weak symbol. */
8322 inner = inner_arg0;
8323 STRIP_NOPS (inner);
8324 if (TREE_CODE (inner) == ADDR_EXPR)
8328 inner = TREE_OPERAND (inner, 0);
8330 while (TREE_CODE (inner) == COMPONENT_REF
8331 || TREE_CODE (inner) == ARRAY_REF);
8332 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
8333 return NULL_TREE;
8336 /* Otherwise, ARG0 already has the proper type for the return value. */
8337 return arg0;
8340 /* Fold a call to __builtin_classify_type with argument ARG. */
8342 static tree
8343 fold_builtin_classify_type (tree arg)
8345 if (arg == 0)
8346 return build_int_cst (integer_type_node, no_type_class);
8348 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
8351 /* Fold a call EXPR (which may be null) to __builtin_strlen with argument
8352 ARG. */
8354 static tree
8355 fold_builtin_strlen (location_t loc, tree expr, tree type, tree arg)
8357 if (!validate_arg (arg, POINTER_TYPE))
8358 return NULL_TREE;
8359 else
8361 c_strlen_data lendata = { };
8362 tree len = c_strlen (arg, 0, &lendata);
8364 if (len)
8365 return fold_convert_loc (loc, type, len);
8367 /* TODO: Move this to gimple-ssa-warn-access once the pass runs
8368 also early enough to detect invalid reads in multimensional
8369 arrays and struct members. */
8370 if (!lendata.decl)
8371 c_strlen (arg, 1, &lendata);
8373 if (lendata.decl)
8375 if (EXPR_HAS_LOCATION (arg))
8376 loc = EXPR_LOCATION (arg);
8377 else if (loc == UNKNOWN_LOCATION)
8378 loc = input_location;
8379 warn_string_no_nul (loc, expr, "strlen", arg, lendata.decl);
8382 return NULL_TREE;
8386 /* Fold a call to __builtin_inf or __builtin_huge_val. */
8388 static tree
8389 fold_builtin_inf (location_t loc, tree type, int warn)
8391 REAL_VALUE_TYPE real;
8393 /* __builtin_inff is intended to be usable to define INFINITY on all
8394 targets. If an infinity is not available, INFINITY expands "to a
8395 positive constant of type float that overflows at translation
8396 time", footnote "In this case, using INFINITY will violate the
8397 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
8398 Thus we pedwarn to ensure this constraint violation is
8399 diagnosed. */
8400 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
8401 pedwarn (loc, 0, "target format does not support infinity");
8403 real_inf (&real);
8404 return build_real (type, real);
8407 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8408 NULL_TREE if no simplification can be made. */
8410 static tree
8411 fold_builtin_sincos (location_t loc,
8412 tree arg0, tree arg1, tree arg2)
8414 tree type;
8415 tree fndecl, call = NULL_TREE;
8417 if (!validate_arg (arg0, REAL_TYPE)
8418 || !validate_arg (arg1, POINTER_TYPE)
8419 || !validate_arg (arg2, POINTER_TYPE))
8420 return NULL_TREE;
8422 type = TREE_TYPE (arg0);
8424 /* Calculate the result when the argument is a constant. */
8425 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
8426 if (fn == END_BUILTINS)
8427 return NULL_TREE;
8429 /* Canonicalize sincos to cexpi. */
8430 if (TREE_CODE (arg0) == REAL_CST)
8432 tree complex_type = build_complex_type (type);
8433 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
8435 if (!call)
8437 if (!targetm.libc_has_function (function_c99_math_complex, type)
8438 || !builtin_decl_implicit_p (fn))
8439 return NULL_TREE;
8440 fndecl = builtin_decl_explicit (fn);
8441 call = build_call_expr_loc (loc, fndecl, 1, arg0);
8442 call = builtin_save_expr (call);
8445 tree ptype = build_pointer_type (type);
8446 arg1 = fold_convert (ptype, arg1);
8447 arg2 = fold_convert (ptype, arg2);
8448 return build2 (COMPOUND_EXPR, void_type_node,
8449 build2 (MODIFY_EXPR, void_type_node,
8450 build_fold_indirect_ref_loc (loc, arg1),
8451 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
8452 build2 (MODIFY_EXPR, void_type_node,
8453 build_fold_indirect_ref_loc (loc, arg2),
8454 fold_build1_loc (loc, REALPART_EXPR, type, call)));
8457 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8458 Return NULL_TREE if no simplification can be made. */
8460 static tree
8461 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8463 if (!validate_arg (arg1, POINTER_TYPE)
8464 || !validate_arg (arg2, POINTER_TYPE)
8465 || !validate_arg (len, INTEGER_TYPE))
8466 return NULL_TREE;
8468 /* If the LEN parameter is zero, return zero. */
8469 if (integer_zerop (len))
8470 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8471 arg1, arg2);
8473 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8474 if (operand_equal_p (arg1, arg2, 0))
8475 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8477 /* If len parameter is one, return an expression corresponding to
8478 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8479 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8481 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8482 tree cst_uchar_ptr_node
8483 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8485 tree ind1
8486 = fold_convert_loc (loc, integer_type_node,
8487 build1 (INDIRECT_REF, cst_uchar_node,
8488 fold_convert_loc (loc,
8489 cst_uchar_ptr_node,
8490 arg1)));
8491 tree ind2
8492 = fold_convert_loc (loc, integer_type_node,
8493 build1 (INDIRECT_REF, cst_uchar_node,
8494 fold_convert_loc (loc,
8495 cst_uchar_ptr_node,
8496 arg2)));
8497 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8500 return NULL_TREE;
8503 /* Fold a call to builtin isascii with argument ARG. */
8505 static tree
8506 fold_builtin_isascii (location_t loc, tree arg)
8508 if (!validate_arg (arg, INTEGER_TYPE))
8509 return NULL_TREE;
8510 else
8512 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8513 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8514 build_int_cst (integer_type_node,
8515 ~ (unsigned HOST_WIDE_INT) 0x7f));
8516 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8517 arg, integer_zero_node);
8521 /* Fold a call to builtin toascii with argument ARG. */
8523 static tree
8524 fold_builtin_toascii (location_t loc, tree arg)
8526 if (!validate_arg (arg, INTEGER_TYPE))
8527 return NULL_TREE;
8529 /* Transform toascii(c) -> (c & 0x7f). */
8530 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8531 build_int_cst (integer_type_node, 0x7f));
8534 /* Fold a call to builtin isdigit with argument ARG. */
8536 static tree
8537 fold_builtin_isdigit (location_t loc, tree arg)
8539 if (!validate_arg (arg, INTEGER_TYPE))
8540 return NULL_TREE;
8541 else
8543 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8544 /* According to the C standard, isdigit is unaffected by locale.
8545 However, it definitely is affected by the target character set. */
8546 unsigned HOST_WIDE_INT target_digit0
8547 = lang_hooks.to_target_charset ('0');
8549 if (target_digit0 == 0)
8550 return NULL_TREE;
8552 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8553 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8554 build_int_cst (unsigned_type_node, target_digit0));
8555 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8556 build_int_cst (unsigned_type_node, 9));
8560 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8562 static tree
8563 fold_builtin_fabs (location_t loc, tree arg, tree type)
8565 if (!validate_arg (arg, REAL_TYPE))
8566 return NULL_TREE;
8568 arg = fold_convert_loc (loc, type, arg);
8569 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8572 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8574 static tree
8575 fold_builtin_abs (location_t loc, tree arg, tree type)
8577 if (!validate_arg (arg, INTEGER_TYPE))
8578 return NULL_TREE;
8580 arg = fold_convert_loc (loc, type, arg);
8581 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8584 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8586 static tree
8587 fold_builtin_carg (location_t loc, tree arg, tree type)
8589 if (validate_arg (arg, COMPLEX_TYPE)
8590 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8592 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8594 if (atan2_fn)
8596 tree new_arg = builtin_save_expr (arg);
8597 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8598 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8599 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8603 return NULL_TREE;
8606 /* Fold a call to builtin frexp, we can assume the base is 2. */
8608 static tree
8609 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
8611 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8612 return NULL_TREE;
8614 STRIP_NOPS (arg0);
8616 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8617 return NULL_TREE;
8619 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8621 /* Proceed if a valid pointer type was passed in. */
8622 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
8624 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8625 tree frac, exp;
8627 switch (value->cl)
8629 case rvc_zero:
8630 /* For +-0, return (*exp = 0, +-0). */
8631 exp = integer_zero_node;
8632 frac = arg0;
8633 break;
8634 case rvc_nan:
8635 case rvc_inf:
8636 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8637 return omit_one_operand_loc (loc, rettype, arg0, arg1);
8638 case rvc_normal:
8640 /* Since the frexp function always expects base 2, and in
8641 GCC normalized significands are already in the range
8642 [0.5, 1.0), we have exactly what frexp wants. */
8643 REAL_VALUE_TYPE frac_rvt = *value;
8644 SET_REAL_EXP (&frac_rvt, 0);
8645 frac = build_real (rettype, frac_rvt);
8646 exp = build_int_cst (integer_type_node, REAL_EXP (value));
8648 break;
8649 default:
8650 gcc_unreachable ();
8653 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8654 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
8655 TREE_SIDE_EFFECTS (arg1) = 1;
8656 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
8659 return NULL_TREE;
8662 /* Fold a call to builtin modf. */
8664 static tree
8665 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
8667 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8668 return NULL_TREE;
8670 STRIP_NOPS (arg0);
8672 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8673 return NULL_TREE;
8675 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8677 /* Proceed if a valid pointer type was passed in. */
8678 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
8680 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8681 REAL_VALUE_TYPE trunc, frac;
8683 switch (value->cl)
8685 case rvc_nan:
8686 case rvc_zero:
8687 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
8688 trunc = frac = *value;
8689 break;
8690 case rvc_inf:
8691 /* For +-Inf, return (*arg1 = arg0, +-0). */
8692 frac = dconst0;
8693 frac.sign = value->sign;
8694 trunc = *value;
8695 break;
8696 case rvc_normal:
8697 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
8698 real_trunc (&trunc, VOIDmode, value);
8699 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
8700 /* If the original number was negative and already
8701 integral, then the fractional part is -0.0. */
8702 if (value->sign && frac.cl == rvc_zero)
8703 frac.sign = value->sign;
8704 break;
8707 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8708 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
8709 build_real (rettype, trunc));
8710 TREE_SIDE_EFFECTS (arg1) = 1;
8711 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
8712 build_real (rettype, frac));
8715 return NULL_TREE;
8718 /* Given a location LOC, an interclass builtin function decl FNDECL
8719 and its single argument ARG, return an folded expression computing
8720 the same, or NULL_TREE if we either couldn't or didn't want to fold
8721 (the latter happen if there's an RTL instruction available). */
8723 static tree
8724 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
8726 machine_mode mode;
8728 if (!validate_arg (arg, REAL_TYPE))
8729 return NULL_TREE;
8731 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
8732 return NULL_TREE;
8734 mode = TYPE_MODE (TREE_TYPE (arg));
8736 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
8738 /* If there is no optab, try generic code. */
8739 switch (DECL_FUNCTION_CODE (fndecl))
8741 tree result;
8743 CASE_FLT_FN (BUILT_IN_ISINF):
8745 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
8746 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8747 tree type = TREE_TYPE (arg);
8748 REAL_VALUE_TYPE r;
8749 char buf[128];
8751 if (is_ibm_extended)
8753 /* NaN and Inf are encoded in the high-order double value
8754 only. The low-order value is not significant. */
8755 type = double_type_node;
8756 mode = DFmode;
8757 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8759 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
8760 real_from_string (&r, buf);
8761 result = build_call_expr (isgr_fn, 2,
8762 fold_build1_loc (loc, ABS_EXPR, type, arg),
8763 build_real (type, r));
8764 return result;
8766 CASE_FLT_FN (BUILT_IN_FINITE):
8767 case BUILT_IN_ISFINITE:
8769 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
8770 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8771 tree type = TREE_TYPE (arg);
8772 REAL_VALUE_TYPE r;
8773 char buf[128];
8775 if (is_ibm_extended)
8777 /* NaN and Inf are encoded in the high-order double value
8778 only. The low-order value is not significant. */
8779 type = double_type_node;
8780 mode = DFmode;
8781 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8783 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
8784 real_from_string (&r, buf);
8785 result = build_call_expr (isle_fn, 2,
8786 fold_build1_loc (loc, ABS_EXPR, type, arg),
8787 build_real (type, r));
8788 /*result = fold_build2_loc (loc, UNGT_EXPR,
8789 TREE_TYPE (TREE_TYPE (fndecl)),
8790 fold_build1_loc (loc, ABS_EXPR, type, arg),
8791 build_real (type, r));
8792 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
8793 TREE_TYPE (TREE_TYPE (fndecl)),
8794 result);*/
8795 return result;
8797 case BUILT_IN_ISNORMAL:
8799 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
8800 islessequal(fabs(x),DBL_MAX). */
8801 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8802 tree type = TREE_TYPE (arg);
8803 tree orig_arg, max_exp, min_exp;
8804 machine_mode orig_mode = mode;
8805 REAL_VALUE_TYPE rmax, rmin;
8806 char buf[128];
8808 orig_arg = arg = builtin_save_expr (arg);
8809 if (is_ibm_extended)
8811 /* Use double to test the normal range of IBM extended
8812 precision. Emin for IBM extended precision is
8813 different to emin for IEEE double, being 53 higher
8814 since the low double exponent is at least 53 lower
8815 than the high double exponent. */
8816 type = double_type_node;
8817 mode = DFmode;
8818 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8820 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
8822 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf), false);
8823 real_from_string (&rmax, buf);
8824 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
8825 real_from_string (&rmin, buf);
8826 max_exp = build_real (type, rmax);
8827 min_exp = build_real (type, rmin);
8829 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
8830 if (is_ibm_extended)
8832 /* Testing the high end of the range is done just using
8833 the high double, using the same test as isfinite().
8834 For the subnormal end of the range we first test the
8835 high double, then if its magnitude is equal to the
8836 limit of 0x1p-969, we test whether the low double is
8837 non-zero and opposite sign to the high double. */
8838 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
8839 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8840 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
8841 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
8842 arg, min_exp);
8843 tree as_complex = build1 (VIEW_CONVERT_EXPR,
8844 complex_double_type_node, orig_arg);
8845 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
8846 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
8847 tree zero = build_real (type, dconst0);
8848 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
8849 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
8850 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
8851 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
8852 fold_build3 (COND_EXPR,
8853 integer_type_node,
8854 hilt, logt, lolt));
8855 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
8856 eq_min, ok_lo);
8857 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
8858 gt_min, eq_min);
8860 else
8862 tree const isge_fn
8863 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
8864 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
8866 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
8867 max_exp, min_exp);
8868 return result;
8870 default:
8871 break;
8874 return NULL_TREE;
8877 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8878 ARG is the argument for the call. */
8880 static tree
8881 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
8883 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8885 if (!validate_arg (arg, REAL_TYPE))
8886 return NULL_TREE;
8888 switch (builtin_index)
8890 case BUILT_IN_ISINF:
8891 if (tree_expr_infinite_p (arg))
8892 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8893 if (!tree_expr_maybe_infinite_p (arg))
8894 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8895 return NULL_TREE;
8897 case BUILT_IN_ISINF_SIGN:
8899 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
8900 /* In a boolean context, GCC will fold the inner COND_EXPR to
8901 1. So e.g. "if (isinf_sign(x))" would be folded to just
8902 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
8903 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
8904 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
8905 tree tmp = NULL_TREE;
8907 arg = builtin_save_expr (arg);
8909 if (signbit_fn && isinf_fn)
8911 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
8912 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
8914 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8915 signbit_call, integer_zero_node);
8916 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8917 isinf_call, integer_zero_node);
8919 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
8920 integer_minus_one_node, integer_one_node);
8921 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8922 isinf_call, tmp,
8923 integer_zero_node);
8926 return tmp;
8929 case BUILT_IN_ISFINITE:
8930 if (tree_expr_finite_p (arg))
8931 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8932 if (tree_expr_nan_p (arg) || tree_expr_infinite_p (arg))
8933 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8934 return NULL_TREE;
8936 case BUILT_IN_ISNAN:
8937 if (tree_expr_nan_p (arg))
8938 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8939 if (!tree_expr_maybe_nan_p (arg))
8940 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8943 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
8944 if (is_ibm_extended)
8946 /* NaN and Inf are encoded in the high-order double value
8947 only. The low-order value is not significant. */
8948 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
8951 arg = builtin_save_expr (arg);
8952 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
8954 default:
8955 gcc_unreachable ();
8959 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
8960 This builtin will generate code to return the appropriate floating
8961 point classification depending on the value of the floating point
8962 number passed in. The possible return values must be supplied as
8963 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
8964 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
8965 one floating point argument which is "type generic". */
8967 static tree
8968 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
8970 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
8971 arg, type, res, tmp;
8972 machine_mode mode;
8973 REAL_VALUE_TYPE r;
8974 char buf[128];
8976 /* Verify the required arguments in the original call. */
8977 if (nargs != 6
8978 || !validate_arg (args[0], INTEGER_TYPE)
8979 || !validate_arg (args[1], INTEGER_TYPE)
8980 || !validate_arg (args[2], INTEGER_TYPE)
8981 || !validate_arg (args[3], INTEGER_TYPE)
8982 || !validate_arg (args[4], INTEGER_TYPE)
8983 || !validate_arg (args[5], REAL_TYPE))
8984 return NULL_TREE;
8986 fp_nan = args[0];
8987 fp_infinite = args[1];
8988 fp_normal = args[2];
8989 fp_subnormal = args[3];
8990 fp_zero = args[4];
8991 arg = args[5];
8992 type = TREE_TYPE (arg);
8993 mode = TYPE_MODE (type);
8994 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
8996 /* fpclassify(x) ->
8997 isnan(x) ? FP_NAN :
8998 (fabs(x) == Inf ? FP_INFINITE :
8999 (fabs(x) >= DBL_MIN ? FP_NORMAL :
9000 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
9002 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9003 build_real (type, dconst0));
9004 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
9005 tmp, fp_zero, fp_subnormal);
9007 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
9008 real_from_string (&r, buf);
9009 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
9010 arg, build_real (type, r));
9011 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
9013 if (tree_expr_maybe_infinite_p (arg))
9015 real_inf (&r);
9016 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
9017 build_real (type, r));
9018 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
9019 fp_infinite, res);
9022 if (tree_expr_maybe_nan_p (arg))
9024 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
9025 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
9028 return res;
9031 /* Fold a call to an unordered comparison function such as
9032 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
9033 being called and ARG0 and ARG1 are the arguments for the call.
9034 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
9035 the opposite of the desired result. UNORDERED_CODE is used
9036 for modes that can hold NaNs and ORDERED_CODE is used for
9037 the rest. */
9039 static tree
9040 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
9041 enum tree_code unordered_code,
9042 enum tree_code ordered_code)
9044 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9045 enum tree_code code;
9046 tree type0, type1;
9047 enum tree_code code0, code1;
9048 tree cmp_type = NULL_TREE;
9050 type0 = TREE_TYPE (arg0);
9051 type1 = TREE_TYPE (arg1);
9053 code0 = TREE_CODE (type0);
9054 code1 = TREE_CODE (type1);
9056 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
9057 /* Choose the wider of two real types. */
9058 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
9059 ? type0 : type1;
9060 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9061 cmp_type = type0;
9062 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
9063 cmp_type = type1;
9065 arg0 = fold_convert_loc (loc, cmp_type, arg0);
9066 arg1 = fold_convert_loc (loc, cmp_type, arg1);
9068 if (unordered_code == UNORDERED_EXPR)
9070 if (tree_expr_nan_p (arg0) || tree_expr_nan_p (arg1))
9071 return omit_two_operands_loc (loc, type, integer_one_node, arg0, arg1);
9072 if (!tree_expr_maybe_nan_p (arg0) && !tree_expr_maybe_nan_p (arg1))
9073 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
9074 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
9077 code = (tree_expr_maybe_nan_p (arg0) || tree_expr_maybe_nan_p (arg1))
9078 ? unordered_code : ordered_code;
9079 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
9080 fold_build2_loc (loc, code, type, arg0, arg1));
9083 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
9084 arithmetics if it can never overflow, or into internal functions that
9085 return both result of arithmetics and overflowed boolean flag in
9086 a complex integer result, or some other check for overflow.
9087 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
9088 checking part of that. */
9090 static tree
9091 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
9092 tree arg0, tree arg1, tree arg2)
9094 enum internal_fn ifn = IFN_LAST;
9095 /* The code of the expression corresponding to the built-in. */
9096 enum tree_code opcode = ERROR_MARK;
9097 bool ovf_only = false;
9099 switch (fcode)
9101 case BUILT_IN_ADD_OVERFLOW_P:
9102 ovf_only = true;
9103 /* FALLTHRU */
9104 case BUILT_IN_ADD_OVERFLOW:
9105 case BUILT_IN_SADD_OVERFLOW:
9106 case BUILT_IN_SADDL_OVERFLOW:
9107 case BUILT_IN_SADDLL_OVERFLOW:
9108 case BUILT_IN_UADD_OVERFLOW:
9109 case BUILT_IN_UADDL_OVERFLOW:
9110 case BUILT_IN_UADDLL_OVERFLOW:
9111 opcode = PLUS_EXPR;
9112 ifn = IFN_ADD_OVERFLOW;
9113 break;
9114 case BUILT_IN_SUB_OVERFLOW_P:
9115 ovf_only = true;
9116 /* FALLTHRU */
9117 case BUILT_IN_SUB_OVERFLOW:
9118 case BUILT_IN_SSUB_OVERFLOW:
9119 case BUILT_IN_SSUBL_OVERFLOW:
9120 case BUILT_IN_SSUBLL_OVERFLOW:
9121 case BUILT_IN_USUB_OVERFLOW:
9122 case BUILT_IN_USUBL_OVERFLOW:
9123 case BUILT_IN_USUBLL_OVERFLOW:
9124 opcode = MINUS_EXPR;
9125 ifn = IFN_SUB_OVERFLOW;
9126 break;
9127 case BUILT_IN_MUL_OVERFLOW_P:
9128 ovf_only = true;
9129 /* FALLTHRU */
9130 case BUILT_IN_MUL_OVERFLOW:
9131 case BUILT_IN_SMUL_OVERFLOW:
9132 case BUILT_IN_SMULL_OVERFLOW:
9133 case BUILT_IN_SMULLL_OVERFLOW:
9134 case BUILT_IN_UMUL_OVERFLOW:
9135 case BUILT_IN_UMULL_OVERFLOW:
9136 case BUILT_IN_UMULLL_OVERFLOW:
9137 opcode = MULT_EXPR;
9138 ifn = IFN_MUL_OVERFLOW;
9139 break;
9140 default:
9141 gcc_unreachable ();
9144 /* For the "generic" overloads, the first two arguments can have different
9145 types and the last argument determines the target type to use to check
9146 for overflow. The arguments of the other overloads all have the same
9147 type. */
9148 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
9150 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
9151 arguments are constant, attempt to fold the built-in call into a constant
9152 expression indicating whether or not it detected an overflow. */
9153 if (ovf_only
9154 && TREE_CODE (arg0) == INTEGER_CST
9155 && TREE_CODE (arg1) == INTEGER_CST)
9156 /* Perform the computation in the target type and check for overflow. */
9157 return omit_one_operand_loc (loc, boolean_type_node,
9158 arith_overflowed_p (opcode, type, arg0, arg1)
9159 ? boolean_true_node : boolean_false_node,
9160 arg2);
9162 tree intres, ovfres;
9163 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9165 intres = fold_binary_loc (loc, opcode, type,
9166 fold_convert_loc (loc, type, arg0),
9167 fold_convert_loc (loc, type, arg1));
9168 if (TREE_OVERFLOW (intres))
9169 intres = drop_tree_overflow (intres);
9170 ovfres = (arith_overflowed_p (opcode, type, arg0, arg1)
9171 ? boolean_true_node : boolean_false_node);
9173 else
9175 tree ctype = build_complex_type (type);
9176 tree call = build_call_expr_internal_loc (loc, ifn, ctype, 2,
9177 arg0, arg1);
9178 tree tgt = save_expr (call);
9179 intres = build1_loc (loc, REALPART_EXPR, type, tgt);
9180 ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
9181 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
9184 if (ovf_only)
9185 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
9187 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
9188 tree store
9189 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
9190 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
9193 /* Fold a call to __builtin_FILE to a constant string. */
9195 static inline tree
9196 fold_builtin_FILE (location_t loc)
9198 if (const char *fname = LOCATION_FILE (loc))
9200 /* The documentation says this builtin is equivalent to the preprocessor
9201 __FILE__ macro so it appears appropriate to use the same file prefix
9202 mappings. */
9203 fname = remap_macro_filename (fname);
9204 return build_string_literal (strlen (fname) + 1, fname);
9207 return build_string_literal (1, "");
9210 /* Fold a call to __builtin_FUNCTION to a constant string. */
9212 static inline tree
9213 fold_builtin_FUNCTION ()
9215 const char *name = "";
9217 if (current_function_decl)
9218 name = lang_hooks.decl_printable_name (current_function_decl, 0);
9220 return build_string_literal (strlen (name) + 1, name);
9223 /* Fold a call to __builtin_LINE to an integer constant. */
9225 static inline tree
9226 fold_builtin_LINE (location_t loc, tree type)
9228 return build_int_cst (type, LOCATION_LINE (loc));
9231 /* Fold a call to built-in function FNDECL with 0 arguments.
9232 This function returns NULL_TREE if no simplification was possible. */
9234 static tree
9235 fold_builtin_0 (location_t loc, tree fndecl)
9237 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9238 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9239 switch (fcode)
9241 case BUILT_IN_FILE:
9242 return fold_builtin_FILE (loc);
9244 case BUILT_IN_FUNCTION:
9245 return fold_builtin_FUNCTION ();
9247 case BUILT_IN_LINE:
9248 return fold_builtin_LINE (loc, type);
9250 CASE_FLT_FN (BUILT_IN_INF):
9251 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
9252 case BUILT_IN_INFD32:
9253 case BUILT_IN_INFD64:
9254 case BUILT_IN_INFD128:
9255 return fold_builtin_inf (loc, type, true);
9257 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
9258 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
9259 return fold_builtin_inf (loc, type, false);
9261 case BUILT_IN_CLASSIFY_TYPE:
9262 return fold_builtin_classify_type (NULL_TREE);
9264 default:
9265 break;
9267 return NULL_TREE;
9270 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
9271 This function returns NULL_TREE if no simplification was possible. */
9273 static tree
9274 fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
9276 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9277 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9279 if (TREE_CODE (arg0) == ERROR_MARK)
9280 return NULL_TREE;
9282 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
9283 return ret;
9285 switch (fcode)
9287 case BUILT_IN_CONSTANT_P:
9289 tree val = fold_builtin_constant_p (arg0);
9291 /* Gimplification will pull the CALL_EXPR for the builtin out of
9292 an if condition. When not optimizing, we'll not CSE it back.
9293 To avoid link error types of regressions, return false now. */
9294 if (!val && !optimize)
9295 val = integer_zero_node;
9297 return val;
9300 case BUILT_IN_CLASSIFY_TYPE:
9301 return fold_builtin_classify_type (arg0);
9303 case BUILT_IN_STRLEN:
9304 return fold_builtin_strlen (loc, expr, type, arg0);
9306 CASE_FLT_FN (BUILT_IN_FABS):
9307 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
9308 case BUILT_IN_FABSD32:
9309 case BUILT_IN_FABSD64:
9310 case BUILT_IN_FABSD128:
9311 return fold_builtin_fabs (loc, arg0, type);
9313 case BUILT_IN_ABS:
9314 case BUILT_IN_LABS:
9315 case BUILT_IN_LLABS:
9316 case BUILT_IN_IMAXABS:
9317 return fold_builtin_abs (loc, arg0, type);
9319 CASE_FLT_FN (BUILT_IN_CONJ):
9320 if (validate_arg (arg0, COMPLEX_TYPE)
9321 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9322 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9323 break;
9325 CASE_FLT_FN (BUILT_IN_CREAL):
9326 if (validate_arg (arg0, COMPLEX_TYPE)
9327 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9328 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9329 break;
9331 CASE_FLT_FN (BUILT_IN_CIMAG):
9332 if (validate_arg (arg0, COMPLEX_TYPE)
9333 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9334 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9335 break;
9337 CASE_FLT_FN (BUILT_IN_CARG):
9338 return fold_builtin_carg (loc, arg0, type);
9340 case BUILT_IN_ISASCII:
9341 return fold_builtin_isascii (loc, arg0);
9343 case BUILT_IN_TOASCII:
9344 return fold_builtin_toascii (loc, arg0);
9346 case BUILT_IN_ISDIGIT:
9347 return fold_builtin_isdigit (loc, arg0);
9349 CASE_FLT_FN (BUILT_IN_FINITE):
9350 case BUILT_IN_FINITED32:
9351 case BUILT_IN_FINITED64:
9352 case BUILT_IN_FINITED128:
9353 case BUILT_IN_ISFINITE:
9355 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
9356 if (ret)
9357 return ret;
9358 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9361 CASE_FLT_FN (BUILT_IN_ISINF):
9362 case BUILT_IN_ISINFD32:
9363 case BUILT_IN_ISINFD64:
9364 case BUILT_IN_ISINFD128:
9366 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
9367 if (ret)
9368 return ret;
9369 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9372 case BUILT_IN_ISNORMAL:
9373 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9375 case BUILT_IN_ISINF_SIGN:
9376 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
9378 CASE_FLT_FN (BUILT_IN_ISNAN):
9379 case BUILT_IN_ISNAND32:
9380 case BUILT_IN_ISNAND64:
9381 case BUILT_IN_ISNAND128:
9382 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
9384 case BUILT_IN_FREE:
9385 if (integer_zerop (arg0))
9386 return build_empty_stmt (loc);
9387 break;
9389 default:
9390 break;
9393 return NULL_TREE;
9397 /* Folds a call EXPR (which may be null) to built-in function FNDECL
9398 with 2 arguments, ARG0 and ARG1. This function returns NULL_TREE
9399 if no simplification was possible. */
9401 static tree
9402 fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1)
9404 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9405 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9407 if (TREE_CODE (arg0) == ERROR_MARK
9408 || TREE_CODE (arg1) == ERROR_MARK)
9409 return NULL_TREE;
9411 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
9412 return ret;
9414 switch (fcode)
9416 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
9417 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
9418 if (validate_arg (arg0, REAL_TYPE)
9419 && validate_arg (arg1, POINTER_TYPE))
9420 return do_mpfr_lgamma_r (arg0, arg1, type);
9421 break;
9423 CASE_FLT_FN (BUILT_IN_FREXP):
9424 return fold_builtin_frexp (loc, arg0, arg1, type);
9426 CASE_FLT_FN (BUILT_IN_MODF):
9427 return fold_builtin_modf (loc, arg0, arg1, type);
9429 case BUILT_IN_STRSPN:
9430 return fold_builtin_strspn (loc, expr, arg0, arg1);
9432 case BUILT_IN_STRCSPN:
9433 return fold_builtin_strcspn (loc, expr, arg0, arg1);
9435 case BUILT_IN_STRPBRK:
9436 return fold_builtin_strpbrk (loc, expr, arg0, arg1, type);
9438 case BUILT_IN_EXPECT:
9439 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, NULL_TREE);
9441 case BUILT_IN_ISGREATER:
9442 return fold_builtin_unordered_cmp (loc, fndecl,
9443 arg0, arg1, UNLE_EXPR, LE_EXPR);
9444 case BUILT_IN_ISGREATEREQUAL:
9445 return fold_builtin_unordered_cmp (loc, fndecl,
9446 arg0, arg1, UNLT_EXPR, LT_EXPR);
9447 case BUILT_IN_ISLESS:
9448 return fold_builtin_unordered_cmp (loc, fndecl,
9449 arg0, arg1, UNGE_EXPR, GE_EXPR);
9450 case BUILT_IN_ISLESSEQUAL:
9451 return fold_builtin_unordered_cmp (loc, fndecl,
9452 arg0, arg1, UNGT_EXPR, GT_EXPR);
9453 case BUILT_IN_ISLESSGREATER:
9454 return fold_builtin_unordered_cmp (loc, fndecl,
9455 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
9456 case BUILT_IN_ISUNORDERED:
9457 return fold_builtin_unordered_cmp (loc, fndecl,
9458 arg0, arg1, UNORDERED_EXPR,
9459 NOP_EXPR);
9461 /* We do the folding for va_start in the expander. */
9462 case BUILT_IN_VA_START:
9463 break;
9465 case BUILT_IN_OBJECT_SIZE:
9466 case BUILT_IN_DYNAMIC_OBJECT_SIZE:
9467 return fold_builtin_object_size (arg0, arg1, fcode);
9469 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
9470 return fold_builtin_atomic_always_lock_free (arg0, arg1);
9472 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
9473 return fold_builtin_atomic_is_lock_free (arg0, arg1);
9475 default:
9476 break;
9478 return NULL_TREE;
9481 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
9482 and ARG2.
9483 This function returns NULL_TREE if no simplification was possible. */
9485 static tree
9486 fold_builtin_3 (location_t loc, tree fndecl,
9487 tree arg0, tree arg1, tree arg2)
9489 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9490 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9492 if (TREE_CODE (arg0) == ERROR_MARK
9493 || TREE_CODE (arg1) == ERROR_MARK
9494 || TREE_CODE (arg2) == ERROR_MARK)
9495 return NULL_TREE;
9497 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
9498 arg0, arg1, arg2))
9499 return ret;
9501 switch (fcode)
9504 CASE_FLT_FN (BUILT_IN_SINCOS):
9505 return fold_builtin_sincos (loc, arg0, arg1, arg2);
9507 CASE_FLT_FN (BUILT_IN_REMQUO):
9508 if (validate_arg (arg0, REAL_TYPE)
9509 && validate_arg (arg1, REAL_TYPE)
9510 && validate_arg (arg2, POINTER_TYPE))
9511 return do_mpfr_remquo (arg0, arg1, arg2);
9512 break;
9514 case BUILT_IN_MEMCMP:
9515 return fold_builtin_memcmp (loc, arg0, arg1, arg2);
9517 case BUILT_IN_EXPECT:
9518 return fold_builtin_expect (loc, arg0, arg1, arg2, NULL_TREE);
9520 case BUILT_IN_EXPECT_WITH_PROBABILITY:
9521 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE, arg2);
9523 case BUILT_IN_ADD_OVERFLOW:
9524 case BUILT_IN_SUB_OVERFLOW:
9525 case BUILT_IN_MUL_OVERFLOW:
9526 case BUILT_IN_ADD_OVERFLOW_P:
9527 case BUILT_IN_SUB_OVERFLOW_P:
9528 case BUILT_IN_MUL_OVERFLOW_P:
9529 case BUILT_IN_SADD_OVERFLOW:
9530 case BUILT_IN_SADDL_OVERFLOW:
9531 case BUILT_IN_SADDLL_OVERFLOW:
9532 case BUILT_IN_SSUB_OVERFLOW:
9533 case BUILT_IN_SSUBL_OVERFLOW:
9534 case BUILT_IN_SSUBLL_OVERFLOW:
9535 case BUILT_IN_SMUL_OVERFLOW:
9536 case BUILT_IN_SMULL_OVERFLOW:
9537 case BUILT_IN_SMULLL_OVERFLOW:
9538 case BUILT_IN_UADD_OVERFLOW:
9539 case BUILT_IN_UADDL_OVERFLOW:
9540 case BUILT_IN_UADDLL_OVERFLOW:
9541 case BUILT_IN_USUB_OVERFLOW:
9542 case BUILT_IN_USUBL_OVERFLOW:
9543 case BUILT_IN_USUBLL_OVERFLOW:
9544 case BUILT_IN_UMUL_OVERFLOW:
9545 case BUILT_IN_UMULL_OVERFLOW:
9546 case BUILT_IN_UMULLL_OVERFLOW:
9547 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
9549 default:
9550 break;
9552 return NULL_TREE;
9555 /* Folds a call EXPR (which may be null) to built-in function FNDECL.
9556 ARGS is an array of NARGS arguments. IGNORE is true if the result
9557 of the function call is ignored. This function returns NULL_TREE
9558 if no simplification was possible. */
9560 static tree
9561 fold_builtin_n (location_t loc, tree expr, tree fndecl, tree *args,
9562 int nargs, bool)
9564 tree ret = NULL_TREE;
9566 switch (nargs)
9568 case 0:
9569 ret = fold_builtin_0 (loc, fndecl);
9570 break;
9571 case 1:
9572 ret = fold_builtin_1 (loc, expr, fndecl, args[0]);
9573 break;
9574 case 2:
9575 ret = fold_builtin_2 (loc, expr, fndecl, args[0], args[1]);
9576 break;
9577 case 3:
9578 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
9579 break;
9580 default:
9581 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
9582 break;
9584 if (ret)
9586 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
9587 SET_EXPR_LOCATION (ret, loc);
9588 return ret;
9590 return NULL_TREE;
9593 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
9594 list ARGS along with N new arguments in NEWARGS. SKIP is the number
9595 of arguments in ARGS to be omitted. OLDNARGS is the number of
9596 elements in ARGS. */
9598 static tree
9599 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
9600 int skip, tree fndecl, int n, va_list newargs)
9602 int nargs = oldnargs - skip + n;
9603 tree *buffer;
9605 if (n > 0)
9607 int i, j;
9609 buffer = XALLOCAVEC (tree, nargs);
9610 for (i = 0; i < n; i++)
9611 buffer[i] = va_arg (newargs, tree);
9612 for (j = skip; j < oldnargs; j++, i++)
9613 buffer[i] = args[j];
9615 else
9616 buffer = args + skip;
9618 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
9621 /* Return true if FNDECL shouldn't be folded right now.
9622 If a built-in function has an inline attribute always_inline
9623 wrapper, defer folding it after always_inline functions have
9624 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
9625 might not be performed. */
9627 bool
9628 avoid_folding_inline_builtin (tree fndecl)
9630 return (DECL_DECLARED_INLINE_P (fndecl)
9631 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
9632 && cfun
9633 && !cfun->always_inline_functions_inlined
9634 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
9637 /* A wrapper function for builtin folding that prevents warnings for
9638 "statement without effect" and the like, caused by removing the
9639 call node earlier than the warning is generated. */
9641 tree
9642 fold_call_expr (location_t loc, tree exp, bool ignore)
9644 tree ret = NULL_TREE;
9645 tree fndecl = get_callee_fndecl (exp);
9646 if (fndecl && fndecl_built_in_p (fndecl)
9647 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
9648 yet. Defer folding until we see all the arguments
9649 (after inlining). */
9650 && !CALL_EXPR_VA_ARG_PACK (exp))
9652 int nargs = call_expr_nargs (exp);
9654 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
9655 instead last argument is __builtin_va_arg_pack (). Defer folding
9656 even in that case, until arguments are finalized. */
9657 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
9659 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
9660 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
9661 return NULL_TREE;
9664 if (avoid_folding_inline_builtin (fndecl))
9665 return NULL_TREE;
9667 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9668 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
9669 CALL_EXPR_ARGP (exp), ignore);
9670 else
9672 tree *args = CALL_EXPR_ARGP (exp);
9673 ret = fold_builtin_n (loc, exp, fndecl, args, nargs, ignore);
9674 if (ret)
9675 return ret;
9678 return NULL_TREE;
9681 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
9682 N arguments are passed in the array ARGARRAY. Return a folded
9683 expression or NULL_TREE if no simplification was possible. */
9685 tree
9686 fold_builtin_call_array (location_t loc, tree,
9687 tree fn,
9688 int n,
9689 tree *argarray)
9691 if (TREE_CODE (fn) != ADDR_EXPR)
9692 return NULL_TREE;
9694 tree fndecl = TREE_OPERAND (fn, 0);
9695 if (TREE_CODE (fndecl) == FUNCTION_DECL
9696 && fndecl_built_in_p (fndecl))
9698 /* If last argument is __builtin_va_arg_pack (), arguments to this
9699 function are not finalized yet. Defer folding until they are. */
9700 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
9702 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
9703 if (fndecl2 && fndecl_built_in_p (fndecl2, BUILT_IN_VA_ARG_PACK))
9704 return NULL_TREE;
9706 if (avoid_folding_inline_builtin (fndecl))
9707 return NULL_TREE;
9708 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9709 return targetm.fold_builtin (fndecl, n, argarray, false);
9710 else
9711 return fold_builtin_n (loc, NULL_TREE, fndecl, argarray, n, false);
9714 return NULL_TREE;
9717 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
9718 along with N new arguments specified as the "..." parameters. SKIP
9719 is the number of arguments in EXP to be omitted. This function is used
9720 to do varargs-to-varargs transformations. */
9722 static tree
9723 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
9725 va_list ap;
9726 tree t;
9728 va_start (ap, n);
9729 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
9730 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
9731 va_end (ap);
9733 return t;
9736 /* Validate a single argument ARG against a tree code CODE representing
9737 a type. Return true when argument is valid. */
9739 static bool
9740 validate_arg (const_tree arg, enum tree_code code)
9742 if (!arg)
9743 return false;
9744 else if (code == POINTER_TYPE)
9745 return POINTER_TYPE_P (TREE_TYPE (arg));
9746 else if (code == INTEGER_TYPE)
9747 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
9748 return code == TREE_CODE (TREE_TYPE (arg));
9751 /* This function validates the types of a function call argument list
9752 against a specified list of tree_codes. If the last specifier is a 0,
9753 that represents an ellipses, otherwise the last specifier must be a
9754 VOID_TYPE.
9756 This is the GIMPLE version of validate_arglist. Eventually we want to
9757 completely convert builtins.cc to work from GIMPLEs and the tree based
9758 validate_arglist will then be removed. */
9760 bool
9761 validate_gimple_arglist (const gcall *call, ...)
9763 enum tree_code code;
9764 bool res = 0;
9765 va_list ap;
9766 const_tree arg;
9767 size_t i;
9769 va_start (ap, call);
9770 i = 0;
9774 code = (enum tree_code) va_arg (ap, int);
9775 switch (code)
9777 case 0:
9778 /* This signifies an ellipses, any further arguments are all ok. */
9779 res = true;
9780 goto end;
9781 case VOID_TYPE:
9782 /* This signifies an endlink, if no arguments remain, return
9783 true, otherwise return false. */
9784 res = (i == gimple_call_num_args (call));
9785 goto end;
9786 default:
9787 /* If no parameters remain or the parameter's code does not
9788 match the specified code, return false. Otherwise continue
9789 checking any remaining arguments. */
9790 arg = gimple_call_arg (call, i++);
9791 if (!validate_arg (arg, code))
9792 goto end;
9793 break;
9796 while (1);
9798 /* We need gotos here since we can only have one VA_CLOSE in a
9799 function. */
9800 end: ;
9801 va_end (ap);
9803 return res;
9806 /* Default target-specific builtin expander that does nothing. */
9809 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9810 rtx target ATTRIBUTE_UNUSED,
9811 rtx subtarget ATTRIBUTE_UNUSED,
9812 machine_mode mode ATTRIBUTE_UNUSED,
9813 int ignore ATTRIBUTE_UNUSED)
9815 return NULL_RTX;
9818 /* Returns true is EXP represents data that would potentially reside
9819 in a readonly section. */
9821 bool
9822 readonly_data_expr (tree exp)
9824 STRIP_NOPS (exp);
9826 if (TREE_CODE (exp) != ADDR_EXPR)
9827 return false;
9829 exp = get_base_address (TREE_OPERAND (exp, 0));
9830 if (!exp)
9831 return false;
9833 /* Make sure we call decl_readonly_section only for trees it
9834 can handle (since it returns true for everything it doesn't
9835 understand). */
9836 if (TREE_CODE (exp) == STRING_CST
9837 || TREE_CODE (exp) == CONSTRUCTOR
9838 || (VAR_P (exp) && TREE_STATIC (exp)))
9839 return decl_readonly_section (exp, 0);
9840 else
9841 return false;
9844 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
9845 to the call, and TYPE is its return type.
9847 Return NULL_TREE if no simplification was possible, otherwise return the
9848 simplified form of the call as a tree.
9850 The simplified form may be a constant or other expression which
9851 computes the same value, but in a more efficient manner (including
9852 calls to other builtin functions).
9854 The call may contain arguments which need to be evaluated, but
9855 which are not useful to determine the result of the call. In
9856 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9857 COMPOUND_EXPR will be an argument which must be evaluated.
9858 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9859 COMPOUND_EXPR in the chain will contain the tree for the simplified
9860 form of the builtin function call. */
9862 static tree
9863 fold_builtin_strpbrk (location_t loc, tree, tree s1, tree s2, tree type)
9865 if (!validate_arg (s1, POINTER_TYPE)
9866 || !validate_arg (s2, POINTER_TYPE))
9867 return NULL_TREE;
9869 tree fn;
9870 const char *p1, *p2;
9872 p2 = c_getstr (s2);
9873 if (p2 == NULL)
9874 return NULL_TREE;
9876 p1 = c_getstr (s1);
9877 if (p1 != NULL)
9879 const char *r = strpbrk (p1, p2);
9880 tree tem;
9882 if (r == NULL)
9883 return build_int_cst (TREE_TYPE (s1), 0);
9885 /* Return an offset into the constant string argument. */
9886 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
9887 return fold_convert_loc (loc, type, tem);
9890 if (p2[0] == '\0')
9891 /* strpbrk(x, "") == NULL.
9892 Evaluate and ignore s1 in case it had side-effects. */
9893 return omit_one_operand_loc (loc, type, integer_zero_node, s1);
9895 if (p2[1] != '\0')
9896 return NULL_TREE; /* Really call strpbrk. */
9898 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
9899 if (!fn)
9900 return NULL_TREE;
9902 /* New argument list transforming strpbrk(s1, s2) to
9903 strchr(s1, s2[0]). */
9904 return build_call_expr_loc (loc, fn, 2, s1,
9905 build_int_cst (integer_type_node, p2[0]));
9908 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
9909 to the call.
9911 Return NULL_TREE if no simplification was possible, otherwise return the
9912 simplified form of the call as a tree.
9914 The simplified form may be a constant or other expression which
9915 computes the same value, but in a more efficient manner (including
9916 calls to other builtin functions).
9918 The call may contain arguments which need to be evaluated, but
9919 which are not useful to determine the result of the call. In
9920 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9921 COMPOUND_EXPR will be an argument which must be evaluated.
9922 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9923 COMPOUND_EXPR in the chain will contain the tree for the simplified
9924 form of the builtin function call. */
9926 static tree
9927 fold_builtin_strspn (location_t loc, tree expr, tree s1, tree s2)
9929 if (!validate_arg (s1, POINTER_TYPE)
9930 || !validate_arg (s2, POINTER_TYPE))
9931 return NULL_TREE;
9933 if (!check_nul_terminated_array (expr, s1)
9934 || !check_nul_terminated_array (expr, s2))
9935 return NULL_TREE;
9937 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9939 /* If either argument is "", return NULL_TREE. */
9940 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9941 /* Evaluate and ignore both arguments in case either one has
9942 side-effects. */
9943 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
9944 s1, s2);
9945 return NULL_TREE;
9948 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
9949 to the call.
9951 Return NULL_TREE if no simplification was possible, otherwise return the
9952 simplified form of the call as a tree.
9954 The simplified form may be a constant or other expression which
9955 computes the same value, but in a more efficient manner (including
9956 calls to other builtin functions).
9958 The call may contain arguments which need to be evaluated, but
9959 which are not useful to determine the result of the call. In
9960 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9961 COMPOUND_EXPR will be an argument which must be evaluated.
9962 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9963 COMPOUND_EXPR in the chain will contain the tree for the simplified
9964 form of the builtin function call. */
9966 static tree
9967 fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2)
9969 if (!validate_arg (s1, POINTER_TYPE)
9970 || !validate_arg (s2, POINTER_TYPE))
9971 return NULL_TREE;
9973 if (!check_nul_terminated_array (expr, s1)
9974 || !check_nul_terminated_array (expr, s2))
9975 return NULL_TREE;
9977 /* If the first argument is "", return NULL_TREE. */
9978 const char *p1 = c_getstr (s1);
9979 if (p1 && *p1 == '\0')
9981 /* Evaluate and ignore argument s2 in case it has
9982 side-effects. */
9983 return omit_one_operand_loc (loc, size_type_node,
9984 size_zero_node, s2);
9987 /* If the second argument is "", return __builtin_strlen(s1). */
9988 const char *p2 = c_getstr (s2);
9989 if (p2 && *p2 == '\0')
9991 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
9993 /* If the replacement _DECL isn't initialized, don't do the
9994 transformation. */
9995 if (!fn)
9996 return NULL_TREE;
9998 return build_call_expr_loc (loc, fn, 1, s1);
10000 return NULL_TREE;
10003 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
10004 produced. False otherwise. This is done so that we don't output the error
10005 or warning twice or three times. */
10007 bool
10008 fold_builtin_next_arg (tree exp, bool va_start_p)
10010 tree fntype = TREE_TYPE (current_function_decl);
10011 int nargs = call_expr_nargs (exp);
10012 tree arg;
10013 /* There is good chance the current input_location points inside the
10014 definition of the va_start macro (perhaps on the token for
10015 builtin) in a system header, so warnings will not be emitted.
10016 Use the location in real source code. */
10017 location_t current_location =
10018 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
10019 NULL);
10021 if (!stdarg_p (fntype))
10023 error ("%<va_start%> used in function with fixed arguments");
10024 return true;
10027 if (va_start_p)
10029 if (va_start_p && (nargs != 2))
10031 error ("wrong number of arguments to function %<va_start%>");
10032 return true;
10034 arg = CALL_EXPR_ARG (exp, 1);
10036 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
10037 when we checked the arguments and if needed issued a warning. */
10038 else
10040 if (nargs == 0)
10042 /* Evidently an out of date version of <stdarg.h>; can't validate
10043 va_start's second argument, but can still work as intended. */
10044 warning_at (current_location,
10045 OPT_Wvarargs,
10046 "%<__builtin_next_arg%> called without an argument");
10047 return true;
10049 else if (nargs > 1)
10051 error ("wrong number of arguments to function %<__builtin_next_arg%>");
10052 return true;
10054 arg = CALL_EXPR_ARG (exp, 0);
10057 if (TREE_CODE (arg) == SSA_NAME
10058 && SSA_NAME_VAR (arg))
10059 arg = SSA_NAME_VAR (arg);
10061 /* We destructively modify the call to be __builtin_va_start (ap, 0)
10062 or __builtin_next_arg (0) the first time we see it, after checking
10063 the arguments and if needed issuing a warning. */
10064 if (!integer_zerop (arg))
10066 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
10068 /* Strip off all nops for the sake of the comparison. This
10069 is not quite the same as STRIP_NOPS. It does more.
10070 We must also strip off INDIRECT_EXPR for C++ reference
10071 parameters. */
10072 while (CONVERT_EXPR_P (arg)
10073 || TREE_CODE (arg) == INDIRECT_REF)
10074 arg = TREE_OPERAND (arg, 0);
10075 if (arg != last_parm)
10077 /* FIXME: Sometimes with the tree optimizers we can get the
10078 not the last argument even though the user used the last
10079 argument. We just warn and set the arg to be the last
10080 argument so that we will get wrong-code because of
10081 it. */
10082 warning_at (current_location,
10083 OPT_Wvarargs,
10084 "second parameter of %<va_start%> not last named argument");
10087 /* Undefined by C99 7.15.1.4p4 (va_start):
10088 "If the parameter parmN is declared with the register storage
10089 class, with a function or array type, or with a type that is
10090 not compatible with the type that results after application of
10091 the default argument promotions, the behavior is undefined."
10093 else if (DECL_REGISTER (arg))
10095 warning_at (current_location,
10096 OPT_Wvarargs,
10097 "undefined behavior when second parameter of "
10098 "%<va_start%> is declared with %<register%> storage");
10101 /* We want to verify the second parameter just once before the tree
10102 optimizers are run and then avoid keeping it in the tree,
10103 as otherwise we could warn even for correct code like:
10104 void foo (int i, ...)
10105 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
10106 if (va_start_p)
10107 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
10108 else
10109 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
10111 return false;
10115 /* Expand a call EXP to __builtin_object_size. */
10117 static rtx
10118 expand_builtin_object_size (tree exp)
10120 tree ost;
10121 int object_size_type;
10122 tree fndecl = get_callee_fndecl (exp);
10124 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
10126 error ("first argument of %qD must be a pointer, second integer constant",
10127 fndecl);
10128 expand_builtin_trap ();
10129 return const0_rtx;
10132 ost = CALL_EXPR_ARG (exp, 1);
10133 STRIP_NOPS (ost);
10135 if (TREE_CODE (ost) != INTEGER_CST
10136 || tree_int_cst_sgn (ost) < 0
10137 || compare_tree_int (ost, 3) > 0)
10139 error ("last argument of %qD is not integer constant between 0 and 3",
10140 fndecl);
10141 expand_builtin_trap ();
10142 return const0_rtx;
10145 object_size_type = tree_to_shwi (ost);
10147 return object_size_type < 2 ? constm1_rtx : const0_rtx;
10150 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
10151 FCODE is the BUILT_IN_* to use.
10152 Return NULL_RTX if we failed; the caller should emit a normal call,
10153 otherwise try to get the result in TARGET, if convenient (and in
10154 mode MODE if that's convenient). */
10156 static rtx
10157 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
10158 enum built_in_function fcode)
10160 if (!validate_arglist (exp,
10161 POINTER_TYPE,
10162 fcode == BUILT_IN_MEMSET_CHK
10163 ? INTEGER_TYPE : POINTER_TYPE,
10164 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
10165 return NULL_RTX;
10167 tree dest = CALL_EXPR_ARG (exp, 0);
10168 tree src = CALL_EXPR_ARG (exp, 1);
10169 tree len = CALL_EXPR_ARG (exp, 2);
10170 tree size = CALL_EXPR_ARG (exp, 3);
10172 /* FIXME: Set access mode to write only for memset et al. */
10173 bool sizes_ok = check_access (exp, len, /*maxread=*/NULL_TREE,
10174 /*srcstr=*/NULL_TREE, size, access_read_write);
10176 if (!tree_fits_uhwi_p (size))
10177 return NULL_RTX;
10179 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
10181 /* Avoid transforming the checking call to an ordinary one when
10182 an overflow has been detected or when the call couldn't be
10183 validated because the size is not constant. */
10184 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
10185 return NULL_RTX;
10187 tree fn = NULL_TREE;
10188 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
10189 mem{cpy,pcpy,move,set} is available. */
10190 switch (fcode)
10192 case BUILT_IN_MEMCPY_CHK:
10193 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
10194 break;
10195 case BUILT_IN_MEMPCPY_CHK:
10196 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
10197 break;
10198 case BUILT_IN_MEMMOVE_CHK:
10199 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
10200 break;
10201 case BUILT_IN_MEMSET_CHK:
10202 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
10203 break;
10204 default:
10205 break;
10208 if (! fn)
10209 return NULL_RTX;
10211 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
10212 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10213 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10214 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10216 else if (fcode == BUILT_IN_MEMSET_CHK)
10217 return NULL_RTX;
10218 else
10220 unsigned int dest_align = get_pointer_alignment (dest);
10222 /* If DEST is not a pointer type, call the normal function. */
10223 if (dest_align == 0)
10224 return NULL_RTX;
10226 /* If SRC and DEST are the same (and not volatile), do nothing. */
10227 if (operand_equal_p (src, dest, 0))
10229 tree expr;
10231 if (fcode != BUILT_IN_MEMPCPY_CHK)
10233 /* Evaluate and ignore LEN in case it has side-effects. */
10234 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
10235 return expand_expr (dest, target, mode, EXPAND_NORMAL);
10238 expr = fold_build_pointer_plus (dest, len);
10239 return expand_expr (expr, target, mode, EXPAND_NORMAL);
10242 /* __memmove_chk special case. */
10243 if (fcode == BUILT_IN_MEMMOVE_CHK)
10245 unsigned int src_align = get_pointer_alignment (src);
10247 if (src_align == 0)
10248 return NULL_RTX;
10250 /* If src is categorized for a readonly section we can use
10251 normal __memcpy_chk. */
10252 if (readonly_data_expr (src))
10254 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
10255 if (!fn)
10256 return NULL_RTX;
10257 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
10258 dest, src, len, size);
10259 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
10260 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
10261 return expand_expr (fn, target, mode, EXPAND_NORMAL);
10264 return NULL_RTX;
10268 /* Emit warning if a buffer overflow is detected at compile time. */
10270 static void
10271 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
10273 /* The source string. */
10274 tree srcstr = NULL_TREE;
10275 /* The size of the destination object returned by __builtin_object_size. */
10276 tree objsize = NULL_TREE;
10277 /* The string that is being concatenated with (as in __strcat_chk)
10278 or null if it isn't. */
10279 tree catstr = NULL_TREE;
10280 /* The maximum length of the source sequence in a bounded operation
10281 (such as __strncat_chk) or null if the operation isn't bounded
10282 (such as __strcat_chk). */
10283 tree maxread = NULL_TREE;
10284 /* The exact size of the access (such as in __strncpy_chk). */
10285 tree size = NULL_TREE;
10286 /* The access by the function that's checked. Except for snprintf
10287 both writing and reading is checked. */
10288 access_mode mode = access_read_write;
10290 switch (fcode)
10292 case BUILT_IN_STRCPY_CHK:
10293 case BUILT_IN_STPCPY_CHK:
10294 srcstr = CALL_EXPR_ARG (exp, 1);
10295 objsize = CALL_EXPR_ARG (exp, 2);
10296 break;
10298 case BUILT_IN_STRCAT_CHK:
10299 /* For __strcat_chk the warning will be emitted only if overflowing
10300 by at least strlen (dest) + 1 bytes. */
10301 catstr = CALL_EXPR_ARG (exp, 0);
10302 srcstr = CALL_EXPR_ARG (exp, 1);
10303 objsize = CALL_EXPR_ARG (exp, 2);
10304 break;
10306 case BUILT_IN_STRNCAT_CHK:
10307 catstr = CALL_EXPR_ARG (exp, 0);
10308 srcstr = CALL_EXPR_ARG (exp, 1);
10309 maxread = CALL_EXPR_ARG (exp, 2);
10310 objsize = CALL_EXPR_ARG (exp, 3);
10311 break;
10313 case BUILT_IN_STRNCPY_CHK:
10314 case BUILT_IN_STPNCPY_CHK:
10315 srcstr = CALL_EXPR_ARG (exp, 1);
10316 size = CALL_EXPR_ARG (exp, 2);
10317 objsize = CALL_EXPR_ARG (exp, 3);
10318 break;
10320 case BUILT_IN_SNPRINTF_CHK:
10321 case BUILT_IN_VSNPRINTF_CHK:
10322 maxread = CALL_EXPR_ARG (exp, 1);
10323 objsize = CALL_EXPR_ARG (exp, 3);
10324 /* The only checked access the write to the destination. */
10325 mode = access_write_only;
10326 break;
10327 default:
10328 gcc_unreachable ();
10331 if (catstr && maxread)
10333 /* Check __strncat_chk. There is no way to determine the length
10334 of the string to which the source string is being appended so
10335 just warn when the length of the source string is not known. */
10336 check_strncat_sizes (exp, objsize);
10337 return;
10340 check_access (exp, size, maxread, srcstr, objsize, mode);
10343 /* Emit warning if a buffer overflow is detected at compile time
10344 in __sprintf_chk/__vsprintf_chk calls. */
10346 static void
10347 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10349 tree size, len, fmt;
10350 const char *fmt_str;
10351 int nargs = call_expr_nargs (exp);
10353 /* Verify the required arguments in the original call. */
10355 if (nargs < 4)
10356 return;
10357 size = CALL_EXPR_ARG (exp, 2);
10358 fmt = CALL_EXPR_ARG (exp, 3);
10360 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
10361 return;
10363 /* Check whether the format is a literal string constant. */
10364 fmt_str = c_getstr (fmt);
10365 if (fmt_str == NULL)
10366 return;
10368 if (!init_target_chars ())
10369 return;
10371 /* If the format doesn't contain % args or %%, we know its size. */
10372 if (strchr (fmt_str, target_percent) == 0)
10373 len = build_int_cstu (size_type_node, strlen (fmt_str));
10374 /* If the format is "%s" and first ... argument is a string literal,
10375 we know it too. */
10376 else if (fcode == BUILT_IN_SPRINTF_CHK
10377 && strcmp (fmt_str, target_percent_s) == 0)
10379 tree arg;
10381 if (nargs < 5)
10382 return;
10383 arg = CALL_EXPR_ARG (exp, 4);
10384 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10385 return;
10387 len = c_strlen (arg, 1);
10388 if (!len || ! tree_fits_uhwi_p (len))
10389 return;
10391 else
10392 return;
10394 /* Add one for the terminating nul. */
10395 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
10397 check_access (exp, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, len, size,
10398 access_write_only);
10401 /* Fold a call to __builtin_object_size with arguments PTR and OST,
10402 if possible. */
10404 static tree
10405 fold_builtin_object_size (tree ptr, tree ost, enum built_in_function fcode)
10407 tree bytes;
10408 int object_size_type;
10410 if (!validate_arg (ptr, POINTER_TYPE)
10411 || !validate_arg (ost, INTEGER_TYPE))
10412 return NULL_TREE;
10414 STRIP_NOPS (ost);
10416 if (TREE_CODE (ost) != INTEGER_CST
10417 || tree_int_cst_sgn (ost) < 0
10418 || compare_tree_int (ost, 3) > 0)
10419 return NULL_TREE;
10421 object_size_type = tree_to_shwi (ost);
10423 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10424 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10425 and (size_t) 0 for types 2 and 3. */
10426 if (TREE_SIDE_EFFECTS (ptr))
10427 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10429 if (fcode == BUILT_IN_DYNAMIC_OBJECT_SIZE)
10430 object_size_type |= OST_DYNAMIC;
10432 if (TREE_CODE (ptr) == ADDR_EXPR)
10434 compute_builtin_object_size (ptr, object_size_type, &bytes);
10435 if ((object_size_type & OST_DYNAMIC)
10436 || int_fits_type_p (bytes, size_type_node))
10437 return fold_convert (size_type_node, bytes);
10439 else if (TREE_CODE (ptr) == SSA_NAME)
10441 /* If object size is not known yet, delay folding until
10442 later. Maybe subsequent passes will help determining
10443 it. */
10444 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
10445 && ((object_size_type & OST_DYNAMIC)
10446 || int_fits_type_p (bytes, size_type_node)))
10447 return fold_convert (size_type_node, bytes);
10450 return NULL_TREE;
10453 /* Builtins with folding operations that operate on "..." arguments
10454 need special handling; we need to store the arguments in a convenient
10455 data structure before attempting any folding. Fortunately there are
10456 only a few builtins that fall into this category. FNDECL is the
10457 function, EXP is the CALL_EXPR for the call. */
10459 static tree
10460 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
10462 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10463 tree ret = NULL_TREE;
10465 switch (fcode)
10467 case BUILT_IN_FPCLASSIFY:
10468 ret = fold_builtin_fpclassify (loc, args, nargs);
10469 break;
10471 default:
10472 break;
10474 if (ret)
10476 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10477 SET_EXPR_LOCATION (ret, loc);
10478 suppress_warning (ret);
10479 return ret;
10481 return NULL_TREE;
10484 /* Initialize format string characters in the target charset. */
10486 bool
10487 init_target_chars (void)
10489 static bool init;
10490 if (!init)
10492 target_newline = lang_hooks.to_target_charset ('\n');
10493 target_percent = lang_hooks.to_target_charset ('%');
10494 target_c = lang_hooks.to_target_charset ('c');
10495 target_s = lang_hooks.to_target_charset ('s');
10496 if (target_newline == 0 || target_percent == 0 || target_c == 0
10497 || target_s == 0)
10498 return false;
10500 target_percent_c[0] = target_percent;
10501 target_percent_c[1] = target_c;
10502 target_percent_c[2] = '\0';
10504 target_percent_s[0] = target_percent;
10505 target_percent_s[1] = target_s;
10506 target_percent_s[2] = '\0';
10508 target_percent_s_newline[0] = target_percent;
10509 target_percent_s_newline[1] = target_s;
10510 target_percent_s_newline[2] = target_newline;
10511 target_percent_s_newline[3] = '\0';
10513 init = true;
10515 return true;
10518 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
10519 and no overflow/underflow occurred. INEXACT is true if M was not
10520 exactly calculated. TYPE is the tree type for the result. This
10521 function assumes that you cleared the MPFR flags and then
10522 calculated M to see if anything subsequently set a flag prior to
10523 entering this function. Return NULL_TREE if any checks fail. */
10525 static tree
10526 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
10528 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10529 overflow/underflow occurred. If -frounding-math, proceed iff the
10530 result of calling FUNC was exact. */
10531 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
10532 && (!flag_rounding_math || !inexact))
10534 REAL_VALUE_TYPE rr;
10536 real_from_mpfr (&rr, m, type, MPFR_RNDN);
10537 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
10538 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10539 but the mpft_t is not, then we underflowed in the
10540 conversion. */
10541 if (real_isfinite (&rr)
10542 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
10544 REAL_VALUE_TYPE rmode;
10546 real_convert (&rmode, TYPE_MODE (type), &rr);
10547 /* Proceed iff the specified mode can hold the value. */
10548 if (real_identical (&rmode, &rr))
10549 return build_real (type, rmode);
10552 return NULL_TREE;
10555 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
10556 number and no overflow/underflow occurred. INEXACT is true if M
10557 was not exactly calculated. TYPE is the tree type for the result.
10558 This function assumes that you cleared the MPFR flags and then
10559 calculated M to see if anything subsequently set a flag prior to
10560 entering this function. Return NULL_TREE if any checks fail, if
10561 FORCE_CONVERT is true, then bypass the checks. */
10563 static tree
10564 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
10566 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10567 overflow/underflow occurred. If -frounding-math, proceed iff the
10568 result of calling FUNC was exact. */
10569 if (force_convert
10570 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
10571 && !mpfr_overflow_p () && !mpfr_underflow_p ()
10572 && (!flag_rounding_math || !inexact)))
10574 REAL_VALUE_TYPE re, im;
10576 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), MPFR_RNDN);
10577 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), MPFR_RNDN);
10578 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
10579 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10580 but the mpft_t is not, then we underflowed in the
10581 conversion. */
10582 if (force_convert
10583 || (real_isfinite (&re) && real_isfinite (&im)
10584 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
10585 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
10587 REAL_VALUE_TYPE re_mode, im_mode;
10589 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
10590 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
10591 /* Proceed iff the specified mode can hold the value. */
10592 if (force_convert
10593 || (real_identical (&re_mode, &re)
10594 && real_identical (&im_mode, &im)))
10595 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
10596 build_real (TREE_TYPE (type), im_mode));
10599 return NULL_TREE;
10602 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
10603 the pointer *(ARG_QUO) and return the result. The type is taken
10604 from the type of ARG0 and is used for setting the precision of the
10605 calculation and results. */
10607 static tree
10608 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
10610 tree const type = TREE_TYPE (arg0);
10611 tree result = NULL_TREE;
10613 STRIP_NOPS (arg0);
10614 STRIP_NOPS (arg1);
10616 /* To proceed, MPFR must exactly represent the target floating point
10617 format, which only happens when the target base equals two. */
10618 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10619 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
10620 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
10622 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
10623 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
10625 if (real_isfinite (ra0) && real_isfinite (ra1))
10627 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10628 const int prec = fmt->p;
10629 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
10630 tree result_rem;
10631 long integer_quo;
10632 mpfr_t m0, m1;
10634 mpfr_inits2 (prec, m0, m1, NULL);
10635 mpfr_from_real (m0, ra0, MPFR_RNDN);
10636 mpfr_from_real (m1, ra1, MPFR_RNDN);
10637 mpfr_clear_flags ();
10638 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
10639 /* Remquo is independent of the rounding mode, so pass
10640 inexact=0 to do_mpfr_ckconv(). */
10641 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
10642 mpfr_clears (m0, m1, NULL);
10643 if (result_rem)
10645 /* MPFR calculates quo in the host's long so it may
10646 return more bits in quo than the target int can hold
10647 if sizeof(host long) > sizeof(target int). This can
10648 happen even for native compilers in LP64 mode. In
10649 these cases, modulo the quo value with the largest
10650 number that the target int can hold while leaving one
10651 bit for the sign. */
10652 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
10653 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
10655 /* Dereference the quo pointer argument. */
10656 arg_quo = build_fold_indirect_ref (arg_quo);
10657 /* Proceed iff a valid pointer type was passed in. */
10658 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
10660 /* Set the value. */
10661 tree result_quo
10662 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
10663 build_int_cst (TREE_TYPE (arg_quo),
10664 integer_quo));
10665 TREE_SIDE_EFFECTS (result_quo) = 1;
10666 /* Combine the quo assignment with the rem. */
10667 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10668 result_quo, result_rem));
10673 return result;
10676 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
10677 resulting value as a tree with type TYPE. The mpfr precision is
10678 set to the precision of TYPE. We assume that this mpfr function
10679 returns zero if the result could be calculated exactly within the
10680 requested precision. In addition, the integer pointer represented
10681 by ARG_SG will be dereferenced and set to the appropriate signgam
10682 (-1,1) value. */
10684 static tree
10685 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
10687 tree result = NULL_TREE;
10689 STRIP_NOPS (arg);
10691 /* To proceed, MPFR must exactly represent the target floating point
10692 format, which only happens when the target base equals two. Also
10693 verify ARG is a constant and that ARG_SG is an int pointer. */
10694 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10695 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
10696 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
10697 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
10699 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
10701 /* In addition to NaN and Inf, the argument cannot be zero or a
10702 negative integer. */
10703 if (real_isfinite (ra)
10704 && ra->cl != rvc_zero
10705 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
10707 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10708 const int prec = fmt->p;
10709 const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN;
10710 int inexact, sg;
10711 mpfr_t m;
10712 tree result_lg;
10714 mpfr_init2 (m, prec);
10715 mpfr_from_real (m, ra, MPFR_RNDN);
10716 mpfr_clear_flags ();
10717 inexact = mpfr_lgamma (m, &sg, m, rnd);
10718 result_lg = do_mpfr_ckconv (m, type, inexact);
10719 mpfr_clear (m);
10720 if (result_lg)
10722 tree result_sg;
10724 /* Dereference the arg_sg pointer argument. */
10725 arg_sg = build_fold_indirect_ref (arg_sg);
10726 /* Assign the signgam value into *arg_sg. */
10727 result_sg = fold_build2 (MODIFY_EXPR,
10728 TREE_TYPE (arg_sg), arg_sg,
10729 build_int_cst (TREE_TYPE (arg_sg), sg));
10730 TREE_SIDE_EFFECTS (result_sg) = 1;
10731 /* Combine the signgam assignment with the lgamma result. */
10732 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10733 result_sg, result_lg));
10738 return result;
10741 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
10742 mpc function FUNC on it and return the resulting value as a tree
10743 with type TYPE. The mpfr precision is set to the precision of
10744 TYPE. We assume that function FUNC returns zero if the result
10745 could be calculated exactly within the requested precision. If
10746 DO_NONFINITE is true, then fold expressions containing Inf or NaN
10747 in the arguments and/or results. */
10749 tree
10750 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
10751 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
10753 tree result = NULL_TREE;
10755 STRIP_NOPS (arg0);
10756 STRIP_NOPS (arg1);
10758 /* To proceed, MPFR must exactly represent the target floating point
10759 format, which only happens when the target base equals two. */
10760 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
10761 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10762 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
10763 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
10764 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
10766 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
10767 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
10768 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
10769 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
10771 if (do_nonfinite
10772 || (real_isfinite (re0) && real_isfinite (im0)
10773 && real_isfinite (re1) && real_isfinite (im1)))
10775 const struct real_format *const fmt =
10776 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
10777 const int prec = fmt->p;
10778 const mpfr_rnd_t rnd = fmt->round_towards_zero
10779 ? MPFR_RNDZ : MPFR_RNDN;
10780 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
10781 int inexact;
10782 mpc_t m0, m1;
10784 mpc_init2 (m0, prec);
10785 mpc_init2 (m1, prec);
10786 mpfr_from_real (mpc_realref (m0), re0, rnd);
10787 mpfr_from_real (mpc_imagref (m0), im0, rnd);
10788 mpfr_from_real (mpc_realref (m1), re1, rnd);
10789 mpfr_from_real (mpc_imagref (m1), im1, rnd);
10790 mpfr_clear_flags ();
10791 inexact = func (m0, m0, m1, crnd);
10792 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
10793 mpc_clear (m0);
10794 mpc_clear (m1);
10798 return result;
10801 /* A wrapper function for builtin folding that prevents warnings for
10802 "statement without effect" and the like, caused by removing the
10803 call node earlier than the warning is generated. */
10805 tree
10806 fold_call_stmt (gcall *stmt, bool ignore)
10808 tree ret = NULL_TREE;
10809 tree fndecl = gimple_call_fndecl (stmt);
10810 location_t loc = gimple_location (stmt);
10811 if (fndecl && fndecl_built_in_p (fndecl)
10812 && !gimple_call_va_arg_pack_p (stmt))
10814 int nargs = gimple_call_num_args (stmt);
10815 tree *args = (nargs > 0
10816 ? gimple_call_arg_ptr (stmt, 0)
10817 : &error_mark_node);
10819 if (avoid_folding_inline_builtin (fndecl))
10820 return NULL_TREE;
10821 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10823 return targetm.fold_builtin (fndecl, nargs, args, ignore);
10825 else
10827 ret = fold_builtin_n (loc, NULL_TREE, fndecl, args, nargs, ignore);
10828 if (ret)
10830 /* Propagate location information from original call to
10831 expansion of builtin. Otherwise things like
10832 maybe_emit_chk_warning, that operate on the expansion
10833 of a builtin, will use the wrong location information. */
10834 if (gimple_has_location (stmt))
10836 tree realret = ret;
10837 if (TREE_CODE (ret) == NOP_EXPR)
10838 realret = TREE_OPERAND (ret, 0);
10839 if (CAN_HAVE_LOCATION_P (realret)
10840 && !EXPR_HAS_LOCATION (realret))
10841 SET_EXPR_LOCATION (realret, loc);
10842 return realret;
10844 return ret;
10848 return NULL_TREE;
10851 /* Look up the function in builtin_decl that corresponds to DECL
10852 and set ASMSPEC as its user assembler name. DECL must be a
10853 function decl that declares a builtin. */
10855 void
10856 set_builtin_user_assembler_name (tree decl, const char *asmspec)
10858 gcc_assert (fndecl_built_in_p (decl, BUILT_IN_NORMAL)
10859 && asmspec != 0);
10861 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
10862 set_user_assembler_name (builtin, asmspec);
10864 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
10865 && INT_TYPE_SIZE < BITS_PER_WORD)
10867 scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
10868 set_user_assembler_libfunc ("ffs", asmspec);
10869 set_optab_libfunc (ffs_optab, mode, "ffs");
10873 /* Return true if DECL is a builtin that expands to a constant or similarly
10874 simple code. */
10875 bool
10876 is_simple_builtin (tree decl)
10878 if (decl && fndecl_built_in_p (decl, BUILT_IN_NORMAL))
10879 switch (DECL_FUNCTION_CODE (decl))
10881 /* Builtins that expand to constants. */
10882 case BUILT_IN_CONSTANT_P:
10883 case BUILT_IN_EXPECT:
10884 case BUILT_IN_OBJECT_SIZE:
10885 case BUILT_IN_UNREACHABLE:
10886 /* Simple register moves or loads from stack. */
10887 case BUILT_IN_ASSUME_ALIGNED:
10888 case BUILT_IN_RETURN_ADDRESS:
10889 case BUILT_IN_EXTRACT_RETURN_ADDR:
10890 case BUILT_IN_FROB_RETURN_ADDR:
10891 case BUILT_IN_RETURN:
10892 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
10893 case BUILT_IN_FRAME_ADDRESS:
10894 case BUILT_IN_VA_END:
10895 case BUILT_IN_STACK_SAVE:
10896 case BUILT_IN_STACK_RESTORE:
10897 case BUILT_IN_DWARF_CFA:
10898 /* Exception state returns or moves registers around. */
10899 case BUILT_IN_EH_FILTER:
10900 case BUILT_IN_EH_POINTER:
10901 case BUILT_IN_EH_COPY_VALUES:
10902 return true;
10904 default:
10905 return false;
10908 return false;
10911 /* Return true if DECL is a builtin that is not expensive, i.e., they are
10912 most probably expanded inline into reasonably simple code. This is a
10913 superset of is_simple_builtin. */
10914 bool
10915 is_inexpensive_builtin (tree decl)
10917 if (!decl)
10918 return false;
10919 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
10920 return true;
10921 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10922 switch (DECL_FUNCTION_CODE (decl))
10924 case BUILT_IN_ABS:
10925 CASE_BUILT_IN_ALLOCA:
10926 case BUILT_IN_BSWAP16:
10927 case BUILT_IN_BSWAP32:
10928 case BUILT_IN_BSWAP64:
10929 case BUILT_IN_BSWAP128:
10930 case BUILT_IN_CLZ:
10931 case BUILT_IN_CLZIMAX:
10932 case BUILT_IN_CLZL:
10933 case BUILT_IN_CLZLL:
10934 case BUILT_IN_CTZ:
10935 case BUILT_IN_CTZIMAX:
10936 case BUILT_IN_CTZL:
10937 case BUILT_IN_CTZLL:
10938 case BUILT_IN_FFS:
10939 case BUILT_IN_FFSIMAX:
10940 case BUILT_IN_FFSL:
10941 case BUILT_IN_FFSLL:
10942 case BUILT_IN_IMAXABS:
10943 case BUILT_IN_FINITE:
10944 case BUILT_IN_FINITEF:
10945 case BUILT_IN_FINITEL:
10946 case BUILT_IN_FINITED32:
10947 case BUILT_IN_FINITED64:
10948 case BUILT_IN_FINITED128:
10949 case BUILT_IN_FPCLASSIFY:
10950 case BUILT_IN_ISFINITE:
10951 case BUILT_IN_ISINF_SIGN:
10952 case BUILT_IN_ISINF:
10953 case BUILT_IN_ISINFF:
10954 case BUILT_IN_ISINFL:
10955 case BUILT_IN_ISINFD32:
10956 case BUILT_IN_ISINFD64:
10957 case BUILT_IN_ISINFD128:
10958 case BUILT_IN_ISNAN:
10959 case BUILT_IN_ISNANF:
10960 case BUILT_IN_ISNANL:
10961 case BUILT_IN_ISNAND32:
10962 case BUILT_IN_ISNAND64:
10963 case BUILT_IN_ISNAND128:
10964 case BUILT_IN_ISNORMAL:
10965 case BUILT_IN_ISGREATER:
10966 case BUILT_IN_ISGREATEREQUAL:
10967 case BUILT_IN_ISLESS:
10968 case BUILT_IN_ISLESSEQUAL:
10969 case BUILT_IN_ISLESSGREATER:
10970 case BUILT_IN_ISUNORDERED:
10971 case BUILT_IN_VA_ARG_PACK:
10972 case BUILT_IN_VA_ARG_PACK_LEN:
10973 case BUILT_IN_VA_COPY:
10974 case BUILT_IN_TRAP:
10975 case BUILT_IN_SAVEREGS:
10976 case BUILT_IN_POPCOUNTL:
10977 case BUILT_IN_POPCOUNTLL:
10978 case BUILT_IN_POPCOUNTIMAX:
10979 case BUILT_IN_POPCOUNT:
10980 case BUILT_IN_PARITYL:
10981 case BUILT_IN_PARITYLL:
10982 case BUILT_IN_PARITYIMAX:
10983 case BUILT_IN_PARITY:
10984 case BUILT_IN_LABS:
10985 case BUILT_IN_LLABS:
10986 case BUILT_IN_PREFETCH:
10987 case BUILT_IN_ACC_ON_DEVICE:
10988 return true;
10990 default:
10991 return is_simple_builtin (decl);
10994 return false;
10997 /* Return true if T is a constant and the value cast to a target char
10998 can be represented by a host char.
10999 Store the casted char constant in *P if so. */
11001 bool
11002 target_char_cst_p (tree t, char *p)
11004 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
11005 return false;
11007 *p = (char)tree_to_uhwi (t);
11008 return true;
11011 /* Return true if the builtin DECL is implemented in a standard library.
11012 Otherwise return false which doesn't guarantee it is not (thus the list
11013 of handled builtins below may be incomplete). */
11015 bool
11016 builtin_with_linkage_p (tree decl)
11018 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
11019 switch (DECL_FUNCTION_CODE (decl))
11021 CASE_FLT_FN (BUILT_IN_ACOS):
11022 CASE_FLT_FN (BUILT_IN_ACOSH):
11023 CASE_FLT_FN (BUILT_IN_ASIN):
11024 CASE_FLT_FN (BUILT_IN_ASINH):
11025 CASE_FLT_FN (BUILT_IN_ATAN):
11026 CASE_FLT_FN (BUILT_IN_ATANH):
11027 CASE_FLT_FN (BUILT_IN_ATAN2):
11028 CASE_FLT_FN (BUILT_IN_CBRT):
11029 CASE_FLT_FN (BUILT_IN_CEIL):
11030 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
11031 CASE_FLT_FN (BUILT_IN_COPYSIGN):
11032 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
11033 CASE_FLT_FN (BUILT_IN_COS):
11034 CASE_FLT_FN (BUILT_IN_COSH):
11035 CASE_FLT_FN (BUILT_IN_ERF):
11036 CASE_FLT_FN (BUILT_IN_ERFC):
11037 CASE_FLT_FN (BUILT_IN_EXP):
11038 CASE_FLT_FN (BUILT_IN_EXP2):
11039 CASE_FLT_FN (BUILT_IN_EXPM1):
11040 CASE_FLT_FN (BUILT_IN_FABS):
11041 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
11042 CASE_FLT_FN (BUILT_IN_FDIM):
11043 CASE_FLT_FN (BUILT_IN_FLOOR):
11044 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
11045 CASE_FLT_FN (BUILT_IN_FMA):
11046 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
11047 CASE_FLT_FN (BUILT_IN_FMAX):
11048 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
11049 CASE_FLT_FN (BUILT_IN_FMIN):
11050 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
11051 CASE_FLT_FN (BUILT_IN_FMOD):
11052 CASE_FLT_FN (BUILT_IN_FREXP):
11053 CASE_FLT_FN (BUILT_IN_HYPOT):
11054 CASE_FLT_FN (BUILT_IN_ILOGB):
11055 CASE_FLT_FN (BUILT_IN_LDEXP):
11056 CASE_FLT_FN (BUILT_IN_LGAMMA):
11057 CASE_FLT_FN (BUILT_IN_LLRINT):
11058 CASE_FLT_FN (BUILT_IN_LLROUND):
11059 CASE_FLT_FN (BUILT_IN_LOG):
11060 CASE_FLT_FN (BUILT_IN_LOG10):
11061 CASE_FLT_FN (BUILT_IN_LOG1P):
11062 CASE_FLT_FN (BUILT_IN_LOG2):
11063 CASE_FLT_FN (BUILT_IN_LOGB):
11064 CASE_FLT_FN (BUILT_IN_LRINT):
11065 CASE_FLT_FN (BUILT_IN_LROUND):
11066 CASE_FLT_FN (BUILT_IN_MODF):
11067 CASE_FLT_FN (BUILT_IN_NAN):
11068 CASE_FLT_FN (BUILT_IN_NEARBYINT):
11069 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
11070 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
11071 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
11072 CASE_FLT_FN (BUILT_IN_POW):
11073 CASE_FLT_FN (BUILT_IN_REMAINDER):
11074 CASE_FLT_FN (BUILT_IN_REMQUO):
11075 CASE_FLT_FN (BUILT_IN_RINT):
11076 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
11077 CASE_FLT_FN (BUILT_IN_ROUND):
11078 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
11079 CASE_FLT_FN (BUILT_IN_SCALBLN):
11080 CASE_FLT_FN (BUILT_IN_SCALBN):
11081 CASE_FLT_FN (BUILT_IN_SIN):
11082 CASE_FLT_FN (BUILT_IN_SINH):
11083 CASE_FLT_FN (BUILT_IN_SINCOS):
11084 CASE_FLT_FN (BUILT_IN_SQRT):
11085 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
11086 CASE_FLT_FN (BUILT_IN_TAN):
11087 CASE_FLT_FN (BUILT_IN_TANH):
11088 CASE_FLT_FN (BUILT_IN_TGAMMA):
11089 CASE_FLT_FN (BUILT_IN_TRUNC):
11090 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
11091 return true;
11093 case BUILT_IN_STPCPY:
11094 case BUILT_IN_STPNCPY:
11095 /* stpcpy is both referenced in libiberty's pex-win32.c and provided
11096 by libiberty's stpcpy.c for MinGW targets so we need to return true
11097 in order to be able to build libiberty in LTO mode for them. */
11098 return true;
11100 default:
11101 break;
11103 return false;
11106 /* Return true if OFFRNG is bounded to a subrange of offset values
11107 valid for the largest possible object. */
11109 bool
11110 access_ref::offset_bounded () const
11112 tree min = TYPE_MIN_VALUE (ptrdiff_type_node);
11113 tree max = TYPE_MAX_VALUE (ptrdiff_type_node);
11114 return wi::to_offset (min) <= offrng[0] && offrng[1] <= wi::to_offset (max);
11117 /* If CALLEE has known side effects, fill in INFO and return true.
11118 See tree-ssa-structalias.cc:find_func_aliases
11119 for the list of builtins we might need to handle here. */
11121 attr_fnspec
11122 builtin_fnspec (tree callee)
11124 built_in_function code = DECL_FUNCTION_CODE (callee);
11126 switch (code)
11128 /* All the following functions read memory pointed to by
11129 their second argument and write memory pointed to by first
11130 argument.
11131 strcat/strncat additionally reads memory pointed to by the first
11132 argument. */
11133 case BUILT_IN_STRCAT:
11134 case BUILT_IN_STRCAT_CHK:
11135 return "1cW 1 ";
11136 case BUILT_IN_STRNCAT:
11137 case BUILT_IN_STRNCAT_CHK:
11138 return "1cW 13";
11139 case BUILT_IN_STRCPY:
11140 case BUILT_IN_STRCPY_CHK:
11141 return "1cO 1 ";
11142 case BUILT_IN_STPCPY:
11143 case BUILT_IN_STPCPY_CHK:
11144 return ".cO 1 ";
11145 case BUILT_IN_STRNCPY:
11146 case BUILT_IN_MEMCPY:
11147 case BUILT_IN_MEMMOVE:
11148 case BUILT_IN_TM_MEMCPY:
11149 case BUILT_IN_TM_MEMMOVE:
11150 case BUILT_IN_STRNCPY_CHK:
11151 case BUILT_IN_MEMCPY_CHK:
11152 case BUILT_IN_MEMMOVE_CHK:
11153 return "1cO313";
11154 case BUILT_IN_MEMPCPY:
11155 case BUILT_IN_MEMPCPY_CHK:
11156 return ".cO313";
11157 case BUILT_IN_STPNCPY:
11158 case BUILT_IN_STPNCPY_CHK:
11159 return ".cO313";
11160 case BUILT_IN_BCOPY:
11161 return ".c23O3";
11162 case BUILT_IN_BZERO:
11163 return ".cO2";
11164 case BUILT_IN_MEMCMP:
11165 case BUILT_IN_MEMCMP_EQ:
11166 case BUILT_IN_BCMP:
11167 case BUILT_IN_STRNCMP:
11168 case BUILT_IN_STRNCMP_EQ:
11169 case BUILT_IN_STRNCASECMP:
11170 return ".cR3R3";
11172 /* The following functions read memory pointed to by their
11173 first argument. */
11174 CASE_BUILT_IN_TM_LOAD (1):
11175 CASE_BUILT_IN_TM_LOAD (2):
11176 CASE_BUILT_IN_TM_LOAD (4):
11177 CASE_BUILT_IN_TM_LOAD (8):
11178 CASE_BUILT_IN_TM_LOAD (FLOAT):
11179 CASE_BUILT_IN_TM_LOAD (DOUBLE):
11180 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
11181 CASE_BUILT_IN_TM_LOAD (M64):
11182 CASE_BUILT_IN_TM_LOAD (M128):
11183 CASE_BUILT_IN_TM_LOAD (M256):
11184 case BUILT_IN_TM_LOG:
11185 case BUILT_IN_TM_LOG_1:
11186 case BUILT_IN_TM_LOG_2:
11187 case BUILT_IN_TM_LOG_4:
11188 case BUILT_IN_TM_LOG_8:
11189 case BUILT_IN_TM_LOG_FLOAT:
11190 case BUILT_IN_TM_LOG_DOUBLE:
11191 case BUILT_IN_TM_LOG_LDOUBLE:
11192 case BUILT_IN_TM_LOG_M64:
11193 case BUILT_IN_TM_LOG_M128:
11194 case BUILT_IN_TM_LOG_M256:
11195 return ".cR ";
11197 case BUILT_IN_INDEX:
11198 case BUILT_IN_RINDEX:
11199 case BUILT_IN_STRCHR:
11200 case BUILT_IN_STRLEN:
11201 case BUILT_IN_STRRCHR:
11202 return ".cR ";
11203 case BUILT_IN_STRNLEN:
11204 return ".cR2";
11206 /* These read memory pointed to by the first argument.
11207 Allocating memory does not have any side-effects apart from
11208 being the definition point for the pointer.
11209 Unix98 specifies that errno is set on allocation failure. */
11210 case BUILT_IN_STRDUP:
11211 return "mCR ";
11212 case BUILT_IN_STRNDUP:
11213 return "mCR2";
11214 /* Allocating memory does not have any side-effects apart from
11215 being the definition point for the pointer. */
11216 case BUILT_IN_MALLOC:
11217 case BUILT_IN_ALIGNED_ALLOC:
11218 case BUILT_IN_CALLOC:
11219 case BUILT_IN_GOMP_ALLOC:
11220 return "mC";
11221 CASE_BUILT_IN_ALLOCA:
11222 return "mc";
11223 /* These read memory pointed to by the first argument with size
11224 in the third argument. */
11225 case BUILT_IN_MEMCHR:
11226 return ".cR3";
11227 /* These read memory pointed to by the first and second arguments. */
11228 case BUILT_IN_STRSTR:
11229 case BUILT_IN_STRPBRK:
11230 case BUILT_IN_STRCASECMP:
11231 case BUILT_IN_STRCSPN:
11232 case BUILT_IN_STRSPN:
11233 case BUILT_IN_STRCMP:
11234 case BUILT_IN_STRCMP_EQ:
11235 return ".cR R ";
11236 /* Freeing memory kills the pointed-to memory. More importantly
11237 the call has to serve as a barrier for moving loads and stores
11238 across it. */
11239 case BUILT_IN_STACK_RESTORE:
11240 case BUILT_IN_FREE:
11241 case BUILT_IN_GOMP_FREE:
11242 return ".co ";
11243 case BUILT_IN_VA_END:
11244 return ".cO ";
11245 /* Realloc serves both as allocation point and deallocation point. */
11246 case BUILT_IN_REALLOC:
11247 return ".Cw ";
11248 case BUILT_IN_GAMMA_R:
11249 case BUILT_IN_GAMMAF_R:
11250 case BUILT_IN_GAMMAL_R:
11251 case BUILT_IN_LGAMMA_R:
11252 case BUILT_IN_LGAMMAF_R:
11253 case BUILT_IN_LGAMMAL_R:
11254 return ".C. Ot";
11255 case BUILT_IN_FREXP:
11256 case BUILT_IN_FREXPF:
11257 case BUILT_IN_FREXPL:
11258 case BUILT_IN_MODF:
11259 case BUILT_IN_MODFF:
11260 case BUILT_IN_MODFL:
11261 return ".c. Ot";
11262 case BUILT_IN_REMQUO:
11263 case BUILT_IN_REMQUOF:
11264 case BUILT_IN_REMQUOL:
11265 return ".c. . Ot";
11266 case BUILT_IN_SINCOS:
11267 case BUILT_IN_SINCOSF:
11268 case BUILT_IN_SINCOSL:
11269 return ".c. OtOt";
11270 case BUILT_IN_MEMSET:
11271 case BUILT_IN_MEMSET_CHK:
11272 case BUILT_IN_TM_MEMSET:
11273 return "1cO3";
11274 CASE_BUILT_IN_TM_STORE (1):
11275 CASE_BUILT_IN_TM_STORE (2):
11276 CASE_BUILT_IN_TM_STORE (4):
11277 CASE_BUILT_IN_TM_STORE (8):
11278 CASE_BUILT_IN_TM_STORE (FLOAT):
11279 CASE_BUILT_IN_TM_STORE (DOUBLE):
11280 CASE_BUILT_IN_TM_STORE (LDOUBLE):
11281 CASE_BUILT_IN_TM_STORE (M64):
11282 CASE_BUILT_IN_TM_STORE (M128):
11283 CASE_BUILT_IN_TM_STORE (M256):
11284 return ".cO ";
11285 case BUILT_IN_STACK_SAVE:
11286 case BUILT_IN_RETURN:
11287 case BUILT_IN_EH_POINTER:
11288 case BUILT_IN_EH_FILTER:
11289 case BUILT_IN_UNWIND_RESUME:
11290 case BUILT_IN_CXA_END_CLEANUP:
11291 case BUILT_IN_EH_COPY_VALUES:
11292 case BUILT_IN_FRAME_ADDRESS:
11293 case BUILT_IN_APPLY_ARGS:
11294 case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT:
11295 case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT:
11296 case BUILT_IN_PREFETCH:
11297 case BUILT_IN_DWARF_CFA:
11298 case BUILT_IN_RETURN_ADDRESS:
11299 return ".c";
11300 case BUILT_IN_ASSUME_ALIGNED:
11301 return "1cX ";
11302 /* But posix_memalign stores a pointer into the memory pointed to
11303 by its first argument. */
11304 case BUILT_IN_POSIX_MEMALIGN:
11305 return ".cOt";
11307 default:
11308 return "";