PR middle-end/77357 - strlen of constant strings not folded
[official-gcc.git] / gcc / builtins.c
blob820d6c262b09e47092daeb4cb0389c34fd6b5b56
1 /* Expand builtin functions.
2 Copyright (C) 1988-2018 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.c 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-restrict.h"
47 #include "stor-layout.h"
48 #include "calls.h"
49 #include "varasm.h"
50 #include "tree-object-size.h"
51 #include "realmpfr.h"
52 #include "cfgrtl.h"
53 #include "except.h"
54 #include "dojump.h"
55 #include "explow.h"
56 #include "stmt.h"
57 #include "expr.h"
58 #include "libfuncs.h"
59 #include "output.h"
60 #include "typeclass.h"
61 #include "langhooks.h"
62 #include "value-prof.h"
63 #include "builtins.h"
64 #include "stringpool.h"
65 #include "attribs.h"
66 #include "asan.h"
67 #include "internal-fn.h"
68 #include "case-cfn-macros.h"
69 #include "gimple-fold.h"
70 #include "intl.h"
71 #include "file-prefix-map.h" /* remap_macro_filename() */
72 #include "gomp-constants.h"
73 #include "omp-general.h"
75 struct target_builtins default_target_builtins;
76 #if SWITCHABLE_TARGET
77 struct target_builtins *this_target_builtins = &default_target_builtins;
78 #endif
80 /* Define the names of the builtin function types and codes. */
81 const char *const built_in_class_names[BUILT_IN_LAST]
82 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
84 #define DEF_BUILTIN(X, N, C, T, LT, B, F, NA, AT, IM, COND) #X,
85 const char * built_in_names[(int) END_BUILTINS] =
87 #include "builtins.def"
90 /* Setup an array of builtin_info_type, make sure each element decl is
91 initialized to NULL_TREE. */
92 builtin_info_type builtin_info[(int)END_BUILTINS];
94 /* Non-zero if __builtin_constant_p should be folded right away. */
95 bool force_folding_builtin_constant_p;
97 static rtx c_readstr (const char *, scalar_int_mode);
98 static int target_char_cast (tree, char *);
99 static rtx get_memory_rtx (tree, tree);
100 static int apply_args_size (void);
101 static int apply_result_size (void);
102 static rtx result_vector (int, rtx);
103 static void expand_builtin_prefetch (tree);
104 static rtx expand_builtin_apply_args (void);
105 static rtx expand_builtin_apply_args_1 (void);
106 static rtx expand_builtin_apply (rtx, rtx, rtx);
107 static void expand_builtin_return (rtx);
108 static enum type_class type_to_class (tree);
109 static rtx expand_builtin_classify_type (tree);
110 static rtx expand_builtin_mathfn_3 (tree, rtx, rtx);
111 static rtx expand_builtin_mathfn_ternary (tree, rtx, rtx);
112 static rtx expand_builtin_interclass_mathfn (tree, rtx);
113 static rtx expand_builtin_sincos (tree);
114 static rtx expand_builtin_cexpi (tree, rtx);
115 static rtx expand_builtin_int_roundingfn (tree, rtx);
116 static rtx expand_builtin_int_roundingfn_2 (tree, rtx);
117 static rtx expand_builtin_next_arg (void);
118 static rtx expand_builtin_va_start (tree);
119 static rtx expand_builtin_va_end (tree);
120 static rtx expand_builtin_va_copy (tree);
121 static rtx expand_builtin_strcmp (tree, rtx);
122 static rtx expand_builtin_strncmp (tree, rtx, machine_mode);
123 static rtx builtin_memcpy_read_str (void *, HOST_WIDE_INT, scalar_int_mode);
124 static rtx expand_builtin_memchr (tree, rtx);
125 static rtx expand_builtin_memcpy (tree, rtx);
126 static rtx expand_builtin_memory_copy_args (tree dest, tree src, tree len,
127 rtx target, tree exp, int endp);
128 static rtx expand_builtin_memmove (tree, rtx);
129 static rtx expand_builtin_mempcpy (tree, rtx);
130 static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, tree, int);
131 static rtx expand_builtin_strcat (tree, rtx);
132 static rtx expand_builtin_strcpy (tree, rtx);
133 static rtx expand_builtin_strcpy_args (tree, tree, rtx);
134 static rtx expand_builtin_stpcpy (tree, rtx, machine_mode);
135 static rtx expand_builtin_stpncpy (tree, rtx);
136 static rtx expand_builtin_strncat (tree, rtx);
137 static rtx expand_builtin_strncpy (tree, rtx);
138 static rtx builtin_memset_gen_str (void *, HOST_WIDE_INT, scalar_int_mode);
139 static rtx expand_builtin_memset (tree, rtx, machine_mode);
140 static rtx expand_builtin_memset_args (tree, tree, tree, rtx, machine_mode, tree);
141 static rtx expand_builtin_bzero (tree);
142 static rtx expand_builtin_strlen (tree, rtx, machine_mode);
143 static rtx expand_builtin_strnlen (tree, rtx, machine_mode);
144 static rtx expand_builtin_alloca (tree);
145 static rtx expand_builtin_unop (machine_mode, tree, rtx, rtx, optab);
146 static rtx expand_builtin_frame_address (tree, tree);
147 static tree stabilize_va_list_loc (location_t, tree, int);
148 static rtx expand_builtin_expect (tree, rtx);
149 static tree fold_builtin_constant_p (tree);
150 static tree fold_builtin_classify_type (tree);
151 static tree fold_builtin_strlen (location_t, tree, tree);
152 static tree fold_builtin_inf (location_t, tree, int);
153 static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
154 static bool validate_arg (const_tree, enum tree_code code);
155 static rtx expand_builtin_fabs (tree, rtx, rtx);
156 static rtx expand_builtin_signbit (tree, rtx);
157 static tree fold_builtin_memcmp (location_t, tree, tree, tree);
158 static tree fold_builtin_isascii (location_t, tree);
159 static tree fold_builtin_toascii (location_t, tree);
160 static tree fold_builtin_isdigit (location_t, tree);
161 static tree fold_builtin_fabs (location_t, tree, tree);
162 static tree fold_builtin_abs (location_t, tree, tree);
163 static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code,
164 enum tree_code);
165 static tree fold_builtin_0 (location_t, tree);
166 static tree fold_builtin_1 (location_t, tree, tree);
167 static tree fold_builtin_2 (location_t, tree, tree, tree);
168 static tree fold_builtin_3 (location_t, tree, tree, tree, tree);
169 static tree fold_builtin_varargs (location_t, tree, tree*, int);
171 static tree fold_builtin_strpbrk (location_t, tree, tree, tree);
172 static tree fold_builtin_strspn (location_t, tree, tree);
173 static tree fold_builtin_strcspn (location_t, tree, tree);
175 static rtx expand_builtin_object_size (tree);
176 static rtx expand_builtin_memory_chk (tree, rtx, machine_mode,
177 enum built_in_function);
178 static void maybe_emit_chk_warning (tree, enum built_in_function);
179 static void maybe_emit_sprintf_chk_warning (tree, enum built_in_function);
180 static void maybe_emit_free_warning (tree);
181 static tree fold_builtin_object_size (tree, tree);
183 unsigned HOST_WIDE_INT target_newline;
184 unsigned HOST_WIDE_INT target_percent;
185 static unsigned HOST_WIDE_INT target_c;
186 static unsigned HOST_WIDE_INT target_s;
187 char target_percent_c[3];
188 char target_percent_s[3];
189 char target_percent_s_newline[4];
190 static tree do_mpfr_remquo (tree, tree, tree);
191 static tree do_mpfr_lgamma_r (tree, tree, tree);
192 static void expand_builtin_sync_synchronize (void);
194 /* Return true if NAME starts with __builtin_ or __sync_. */
196 static bool
197 is_builtin_name (const char *name)
199 if (strncmp (name, "__builtin_", 10) == 0)
200 return true;
201 if (strncmp (name, "__sync_", 7) == 0)
202 return true;
203 if (strncmp (name, "__atomic_", 9) == 0)
204 return true;
205 return false;
209 /* Return true if DECL is a function symbol representing a built-in. */
211 bool
212 is_builtin_fn (tree decl)
214 return TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl);
217 /* Return true if NODE should be considered for inline expansion regardless
218 of the optimization level. This means whenever a function is invoked with
219 its "internal" name, which normally contains the prefix "__builtin". */
221 bool
222 called_as_built_in (tree node)
224 /* Note that we must use DECL_NAME, not DECL_ASSEMBLER_NAME_SET_P since
225 we want the name used to call the function, not the name it
226 will have. */
227 const char *name = IDENTIFIER_POINTER (DECL_NAME (node));
228 return is_builtin_name (name);
231 /* Compute values M and N such that M divides (address of EXP - N) and such
232 that N < M. If these numbers can be determined, store M in alignp and N in
233 *BITPOSP and return true. Otherwise return false and store BITS_PER_UNIT to
234 *alignp and any bit-offset to *bitposp.
236 Note that the address (and thus the alignment) computed here is based
237 on the address to which a symbol resolves, whereas DECL_ALIGN is based
238 on the address at which an object is actually located. These two
239 addresses are not always the same. For example, on ARM targets,
240 the address &foo of a Thumb function foo() has the lowest bit set,
241 whereas foo() itself starts on an even address.
243 If ADDR_P is true we are taking the address of the memory reference EXP
244 and thus cannot rely on the access taking place. */
246 static bool
247 get_object_alignment_2 (tree exp, unsigned int *alignp,
248 unsigned HOST_WIDE_INT *bitposp, bool addr_p)
250 poly_int64 bitsize, bitpos;
251 tree offset;
252 machine_mode mode;
253 int unsignedp, reversep, volatilep;
254 unsigned int align = BITS_PER_UNIT;
255 bool known_alignment = false;
257 /* Get the innermost object and the constant (bitpos) and possibly
258 variable (offset) offset of the access. */
259 exp = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode,
260 &unsignedp, &reversep, &volatilep);
262 /* Extract alignment information from the innermost object and
263 possibly adjust bitpos and offset. */
264 if (TREE_CODE (exp) == FUNCTION_DECL)
266 /* Function addresses can encode extra information besides their
267 alignment. However, if TARGET_PTRMEMFUNC_VBIT_LOCATION
268 allows the low bit to be used as a virtual bit, we know
269 that the address itself must be at least 2-byte aligned. */
270 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn)
271 align = 2 * BITS_PER_UNIT;
273 else if (TREE_CODE (exp) == LABEL_DECL)
275 else if (TREE_CODE (exp) == CONST_DECL)
277 /* The alignment of a CONST_DECL is determined by its initializer. */
278 exp = DECL_INITIAL (exp);
279 align = TYPE_ALIGN (TREE_TYPE (exp));
280 if (CONSTANT_CLASS_P (exp))
281 align = targetm.constant_alignment (exp, align);
283 known_alignment = true;
285 else if (DECL_P (exp))
287 align = DECL_ALIGN (exp);
288 known_alignment = true;
290 else if (TREE_CODE (exp) == INDIRECT_REF
291 || TREE_CODE (exp) == MEM_REF
292 || TREE_CODE (exp) == TARGET_MEM_REF)
294 tree addr = TREE_OPERAND (exp, 0);
295 unsigned ptr_align;
296 unsigned HOST_WIDE_INT ptr_bitpos;
297 unsigned HOST_WIDE_INT ptr_bitmask = ~0;
299 /* If the address is explicitely aligned, handle that. */
300 if (TREE_CODE (addr) == BIT_AND_EXPR
301 && TREE_CODE (TREE_OPERAND (addr, 1)) == INTEGER_CST)
303 ptr_bitmask = TREE_INT_CST_LOW (TREE_OPERAND (addr, 1));
304 ptr_bitmask *= BITS_PER_UNIT;
305 align = least_bit_hwi (ptr_bitmask);
306 addr = TREE_OPERAND (addr, 0);
309 known_alignment
310 = get_pointer_alignment_1 (addr, &ptr_align, &ptr_bitpos);
311 align = MAX (ptr_align, align);
313 /* Re-apply explicit alignment to the bitpos. */
314 ptr_bitpos &= ptr_bitmask;
316 /* The alignment of the pointer operand in a TARGET_MEM_REF
317 has to take the variable offset parts into account. */
318 if (TREE_CODE (exp) == TARGET_MEM_REF)
320 if (TMR_INDEX (exp))
322 unsigned HOST_WIDE_INT step = 1;
323 if (TMR_STEP (exp))
324 step = TREE_INT_CST_LOW (TMR_STEP (exp));
325 align = MIN (align, least_bit_hwi (step) * BITS_PER_UNIT);
327 if (TMR_INDEX2 (exp))
328 align = BITS_PER_UNIT;
329 known_alignment = false;
332 /* When EXP is an actual memory reference then we can use
333 TYPE_ALIGN of a pointer indirection to derive alignment.
334 Do so only if get_pointer_alignment_1 did not reveal absolute
335 alignment knowledge and if using that alignment would
336 improve the situation. */
337 unsigned int talign;
338 if (!addr_p && !known_alignment
339 && (talign = min_align_of_type (TREE_TYPE (exp)) * BITS_PER_UNIT)
340 && talign > align)
341 align = talign;
342 else
344 /* Else adjust bitpos accordingly. */
345 bitpos += ptr_bitpos;
346 if (TREE_CODE (exp) == MEM_REF
347 || TREE_CODE (exp) == TARGET_MEM_REF)
348 bitpos += mem_ref_offset (exp).force_shwi () * BITS_PER_UNIT;
351 else if (TREE_CODE (exp) == STRING_CST)
353 /* STRING_CST are the only constant objects we allow to be not
354 wrapped inside a CONST_DECL. */
355 align = TYPE_ALIGN (TREE_TYPE (exp));
356 if (CONSTANT_CLASS_P (exp))
357 align = targetm.constant_alignment (exp, align);
359 known_alignment = true;
362 /* If there is a non-constant offset part extract the maximum
363 alignment that can prevail. */
364 if (offset)
366 unsigned int trailing_zeros = tree_ctz (offset);
367 if (trailing_zeros < HOST_BITS_PER_INT)
369 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
370 if (inner)
371 align = MIN (align, inner);
375 /* Account for the alignment of runtime coefficients, so that the constant
376 bitpos is guaranteed to be accurate. */
377 unsigned int alt_align = ::known_alignment (bitpos - bitpos.coeffs[0]);
378 if (alt_align != 0 && alt_align < align)
380 align = alt_align;
381 known_alignment = false;
384 *alignp = align;
385 *bitposp = bitpos.coeffs[0] & (align - 1);
386 return known_alignment;
389 /* For a memory reference expression EXP compute values M and N such that M
390 divides (&EXP - N) and such that N < M. If these numbers can be determined,
391 store M in alignp and N in *BITPOSP and return true. Otherwise return false
392 and store BITS_PER_UNIT to *alignp and any bit-offset to *bitposp. */
394 bool
395 get_object_alignment_1 (tree exp, unsigned int *alignp,
396 unsigned HOST_WIDE_INT *bitposp)
398 return get_object_alignment_2 (exp, alignp, bitposp, false);
401 /* Return the alignment in bits of EXP, an object. */
403 unsigned int
404 get_object_alignment (tree exp)
406 unsigned HOST_WIDE_INT bitpos = 0;
407 unsigned int align;
409 get_object_alignment_1 (exp, &align, &bitpos);
411 /* align and bitpos now specify known low bits of the pointer.
412 ptr & (align - 1) == bitpos. */
414 if (bitpos != 0)
415 align = least_bit_hwi (bitpos);
416 return align;
419 /* For a pointer valued expression EXP compute values M and N such that M
420 divides (EXP - N) and such that N < M. If these numbers can be determined,
421 store M in alignp and N in *BITPOSP and return true. Return false if
422 the results are just a conservative approximation.
424 If EXP is not a pointer, false is returned too. */
426 bool
427 get_pointer_alignment_1 (tree exp, unsigned int *alignp,
428 unsigned HOST_WIDE_INT *bitposp)
430 STRIP_NOPS (exp);
432 if (TREE_CODE (exp) == ADDR_EXPR)
433 return get_object_alignment_2 (TREE_OPERAND (exp, 0),
434 alignp, bitposp, true);
435 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
437 unsigned int align;
438 unsigned HOST_WIDE_INT bitpos;
439 bool res = get_pointer_alignment_1 (TREE_OPERAND (exp, 0),
440 &align, &bitpos);
441 if (TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
442 bitpos += TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT;
443 else
445 unsigned int trailing_zeros = tree_ctz (TREE_OPERAND (exp, 1));
446 if (trailing_zeros < HOST_BITS_PER_INT)
448 unsigned int inner = (1U << trailing_zeros) * BITS_PER_UNIT;
449 if (inner)
450 align = MIN (align, inner);
453 *alignp = align;
454 *bitposp = bitpos & (align - 1);
455 return res;
457 else if (TREE_CODE (exp) == SSA_NAME
458 && POINTER_TYPE_P (TREE_TYPE (exp)))
460 unsigned int ptr_align, ptr_misalign;
461 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (exp);
463 if (pi && get_ptr_info_alignment (pi, &ptr_align, &ptr_misalign))
465 *bitposp = ptr_misalign * BITS_PER_UNIT;
466 *alignp = ptr_align * BITS_PER_UNIT;
467 /* Make sure to return a sensible alignment when the multiplication
468 by BITS_PER_UNIT overflowed. */
469 if (*alignp == 0)
470 *alignp = 1u << (HOST_BITS_PER_INT - 1);
471 /* We cannot really tell whether this result is an approximation. */
472 return false;
474 else
476 *bitposp = 0;
477 *alignp = BITS_PER_UNIT;
478 return false;
481 else if (TREE_CODE (exp) == INTEGER_CST)
483 *alignp = BIGGEST_ALIGNMENT;
484 *bitposp = ((TREE_INT_CST_LOW (exp) * BITS_PER_UNIT)
485 & (BIGGEST_ALIGNMENT - 1));
486 return true;
489 *bitposp = 0;
490 *alignp = BITS_PER_UNIT;
491 return false;
494 /* Return the alignment in bits of EXP, a pointer valued expression.
495 The alignment returned is, by default, the alignment of the thing that
496 EXP points to. If it is not a POINTER_TYPE, 0 is returned.
498 Otherwise, look at the expression to see if we can do better, i.e., if the
499 expression is actually pointing at an object whose alignment is tighter. */
501 unsigned int
502 get_pointer_alignment (tree exp)
504 unsigned HOST_WIDE_INT bitpos = 0;
505 unsigned int align;
507 get_pointer_alignment_1 (exp, &align, &bitpos);
509 /* align and bitpos now specify known low bits of the pointer.
510 ptr & (align - 1) == bitpos. */
512 if (bitpos != 0)
513 align = least_bit_hwi (bitpos);
515 return align;
518 /* Return the number of non-zero elements in the sequence
519 [ PTR, PTR + MAXELTS ) where each element's size is ELTSIZE bytes.
520 ELTSIZE must be a power of 2 less than 8. Used by c_strlen. */
522 static unsigned
523 string_length (const void *ptr, unsigned eltsize, unsigned maxelts)
525 gcc_checking_assert (eltsize == 1 || eltsize == 2 || eltsize == 4);
527 unsigned n;
529 if (eltsize == 1)
531 /* Optimize the common case of plain char. */
532 for (n = 0; n < maxelts; n++)
534 const char *elt = (const char*) ptr + n;
535 if (!*elt)
536 break;
539 else
541 for (n = 0; n < maxelts; n++)
543 const char *elt = (const char*) ptr + n * eltsize;
544 if (!memcmp (elt, "\0\0\0\0", eltsize))
545 break;
548 return n;
551 /* Compute the length of a null-terminated character string or wide
552 character string handling character sizes of 1, 2, and 4 bytes.
553 TREE_STRING_LENGTH is not the right way because it evaluates to
554 the size of the character array in bytes (as opposed to characters)
555 and because it can contain a zero byte in the middle.
557 ONLY_VALUE should be nonzero if the result is not going to be emitted
558 into the instruction stream and zero if it is going to be expanded.
559 E.g. with i++ ? "foo" : "bar", if ONLY_VALUE is nonzero, constant 3
560 is returned, otherwise NULL, since
561 len = c_strlen (src, 1); if (len) expand_expr (len, ...); would not
562 evaluate the side-effects.
564 If ONLY_VALUE is two then we do not emit warnings about out-of-bound
565 accesses. Note that this implies the result is not going to be emitted
566 into the instruction stream.
568 The value returned is of type `ssizetype'.
570 Unfortunately, string_constant can't access the values of const char
571 arrays with initializers, so neither can we do so here. */
573 tree
574 c_strlen (tree src, int only_value)
576 STRIP_NOPS (src);
577 if (TREE_CODE (src) == COND_EXPR
578 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
580 tree len1, len2;
582 len1 = c_strlen (TREE_OPERAND (src, 1), only_value);
583 len2 = c_strlen (TREE_OPERAND (src, 2), only_value);
584 if (tree_int_cst_equal (len1, len2))
585 return len1;
588 if (TREE_CODE (src) == COMPOUND_EXPR
589 && (only_value || !TREE_SIDE_EFFECTS (TREE_OPERAND (src, 0))))
590 return c_strlen (TREE_OPERAND (src, 1), only_value);
592 location_t loc = EXPR_LOC_OR_LOC (src, input_location);
594 /* Offset from the beginning of the string in bytes. */
595 tree byteoff;
596 src = string_constant (src, &byteoff);
597 if (src == 0)
598 return NULL_TREE;
600 /* Determine the size of the string element. */
601 unsigned eltsize
602 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (src))));
604 /* Set MAXELTS to sizeof (SRC) / sizeof (*SRC) - 1, the maximum possible
605 length of SRC. Prefer TYPE_SIZE() to TREE_STRING_LENGTH() if possible
606 in case the latter is less than the size of the array. */
607 HOST_WIDE_INT maxelts = TREE_STRING_LENGTH (src);
608 tree type = TREE_TYPE (src);
609 if (tree size = TYPE_SIZE_UNIT (type))
610 if (tree_fits_shwi_p (size))
611 maxelts = tree_to_uhwi (size);
613 maxelts = maxelts / eltsize - 1;
615 /* PTR can point to the byte representation of any string type, including
616 char* and wchar_t*. */
617 const char *ptr = TREE_STRING_POINTER (src);
619 if (byteoff && TREE_CODE (byteoff) != INTEGER_CST)
621 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
622 compute the offset to the following null if we don't know where to
623 start searching for it. */
624 if (string_length (ptr, eltsize, maxelts) < maxelts)
626 /* Return when an embedded null character is found. */
627 return NULL_TREE;
630 if (!maxelts)
631 return ssize_int (0);
633 /* We don't know the starting offset, but we do know that the string
634 has no internal zero bytes. We can assume that the offset falls
635 within the bounds of the string; otherwise, the programmer deserves
636 what he gets. Subtract the offset from the length of the string,
637 and return that. This would perhaps not be valid if we were dealing
638 with named arrays in addition to literal string constants. */
639 return size_diffop_loc (loc, size_int (maxelts * eltsize), byteoff);
642 /* Offset from the beginning of the string in elements. */
643 HOST_WIDE_INT eltoff;
645 /* We have a known offset into the string. Start searching there for
646 a null character if we can represent it as a single HOST_WIDE_INT. */
647 if (byteoff == 0)
648 eltoff = 0;
649 else if (! tree_fits_shwi_p (byteoff))
650 eltoff = -1;
651 else
652 eltoff = tree_to_shwi (byteoff) / eltsize;
654 /* If the offset is known to be out of bounds, warn, and call strlen at
655 runtime. */
656 if (eltoff < 0 || eltoff > maxelts)
658 /* Suppress multiple warnings for propagated constant strings. */
659 if (only_value != 2
660 && !TREE_NO_WARNING (src))
662 warning_at (loc, OPT_Warray_bounds,
663 "offset %qwi outside bounds of constant string",
664 eltoff);
665 TREE_NO_WARNING (src) = 1;
667 return NULL_TREE;
670 /* Use strlen to search for the first zero byte. Since any strings
671 constructed with build_string will have nulls appended, we win even
672 if we get handed something like (char[4])"abcd".
674 Since ELTOFF is our starting index into the string, no further
675 calculation is needed. */
676 unsigned len = string_length (ptr + eltoff * eltsize, eltsize,
677 maxelts - eltoff);
679 return ssize_int (len);
682 /* Return a constant integer corresponding to target reading
683 GET_MODE_BITSIZE (MODE) bits from string constant STR. */
685 static rtx
686 c_readstr (const char *str, scalar_int_mode mode)
688 HOST_WIDE_INT ch;
689 unsigned int i, j;
690 HOST_WIDE_INT tmp[MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT];
692 gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
693 unsigned int len = (GET_MODE_PRECISION (mode) + HOST_BITS_PER_WIDE_INT - 1)
694 / HOST_BITS_PER_WIDE_INT;
696 gcc_assert (len <= MAX_BITSIZE_MODE_ANY_INT / HOST_BITS_PER_WIDE_INT);
697 for (i = 0; i < len; i++)
698 tmp[i] = 0;
700 ch = 1;
701 for (i = 0; i < GET_MODE_SIZE (mode); i++)
703 j = i;
704 if (WORDS_BIG_ENDIAN)
705 j = GET_MODE_SIZE (mode) - i - 1;
706 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
707 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD)
708 j = j + UNITS_PER_WORD - 2 * (j % UNITS_PER_WORD) - 1;
709 j *= BITS_PER_UNIT;
711 if (ch)
712 ch = (unsigned char) str[i];
713 tmp[j / HOST_BITS_PER_WIDE_INT] |= ch << (j % HOST_BITS_PER_WIDE_INT);
716 wide_int c = wide_int::from_array (tmp, len, GET_MODE_PRECISION (mode));
717 return immed_wide_int_const (c, mode);
720 /* Cast a target constant CST to target CHAR and if that value fits into
721 host char type, return zero and put that value into variable pointed to by
722 P. */
724 static int
725 target_char_cast (tree cst, char *p)
727 unsigned HOST_WIDE_INT val, hostval;
729 if (TREE_CODE (cst) != INTEGER_CST
730 || CHAR_TYPE_SIZE > HOST_BITS_PER_WIDE_INT)
731 return 1;
733 /* Do not care if it fits or not right here. */
734 val = TREE_INT_CST_LOW (cst);
736 if (CHAR_TYPE_SIZE < HOST_BITS_PER_WIDE_INT)
737 val &= (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
739 hostval = val;
740 if (HOST_BITS_PER_CHAR < HOST_BITS_PER_WIDE_INT)
741 hostval &= (HOST_WIDE_INT_1U << HOST_BITS_PER_CHAR) - 1;
743 if (val != hostval)
744 return 1;
746 *p = hostval;
747 return 0;
750 /* Similar to save_expr, but assumes that arbitrary code is not executed
751 in between the multiple evaluations. In particular, we assume that a
752 non-addressable local variable will not be modified. */
754 static tree
755 builtin_save_expr (tree exp)
757 if (TREE_CODE (exp) == SSA_NAME
758 || (TREE_ADDRESSABLE (exp) == 0
759 && (TREE_CODE (exp) == PARM_DECL
760 || (VAR_P (exp) && !TREE_STATIC (exp)))))
761 return exp;
763 return save_expr (exp);
766 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
767 times to get the address of either a higher stack frame, or a return
768 address located within it (depending on FNDECL_CODE). */
770 static rtx
771 expand_builtin_return_addr (enum built_in_function fndecl_code, int count)
773 int i;
774 rtx tem = INITIAL_FRAME_ADDRESS_RTX;
775 if (tem == NULL_RTX)
777 /* For a zero count with __builtin_return_address, we don't care what
778 frame address we return, because target-specific definitions will
779 override us. Therefore frame pointer elimination is OK, and using
780 the soft frame pointer is OK.
782 For a nonzero count, or a zero count with __builtin_frame_address,
783 we require a stable offset from the current frame pointer to the
784 previous one, so we must use the hard frame pointer, and
785 we must disable frame pointer elimination. */
786 if (count == 0 && fndecl_code == BUILT_IN_RETURN_ADDRESS)
787 tem = frame_pointer_rtx;
788 else
790 tem = hard_frame_pointer_rtx;
792 /* Tell reload not to eliminate the frame pointer. */
793 crtl->accesses_prior_frames = 1;
797 if (count > 0)
798 SETUP_FRAME_ADDRESSES ();
800 /* On the SPARC, the return address is not in the frame, it is in a
801 register. There is no way to access it off of the current frame
802 pointer, but it can be accessed off the previous frame pointer by
803 reading the value from the register window save area. */
804 if (RETURN_ADDR_IN_PREVIOUS_FRAME && fndecl_code == BUILT_IN_RETURN_ADDRESS)
805 count--;
807 /* Scan back COUNT frames to the specified frame. */
808 for (i = 0; i < count; i++)
810 /* Assume the dynamic chain pointer is in the word that the
811 frame address points to, unless otherwise specified. */
812 tem = DYNAMIC_CHAIN_ADDRESS (tem);
813 tem = memory_address (Pmode, tem);
814 tem = gen_frame_mem (Pmode, tem);
815 tem = copy_to_reg (tem);
818 /* For __builtin_frame_address, return what we've got. But, on
819 the SPARC for example, we may have to add a bias. */
820 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
821 return FRAME_ADDR_RTX (tem);
823 /* For __builtin_return_address, get the return address from that frame. */
824 #ifdef RETURN_ADDR_RTX
825 tem = RETURN_ADDR_RTX (count, tem);
826 #else
827 tem = memory_address (Pmode,
828 plus_constant (Pmode, tem, GET_MODE_SIZE (Pmode)));
829 tem = gen_frame_mem (Pmode, tem);
830 #endif
831 return tem;
834 /* Alias set used for setjmp buffer. */
835 static alias_set_type setjmp_alias_set = -1;
837 /* Construct the leading half of a __builtin_setjmp call. Control will
838 return to RECEIVER_LABEL. This is also called directly by the SJLJ
839 exception handling code. */
841 void
842 expand_builtin_setjmp_setup (rtx buf_addr, rtx receiver_label)
844 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
845 rtx stack_save;
846 rtx mem;
848 if (setjmp_alias_set == -1)
849 setjmp_alias_set = new_alias_set ();
851 buf_addr = convert_memory_address (Pmode, buf_addr);
853 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
855 /* We store the frame pointer and the address of receiver_label in
856 the buffer and use the rest of it for the stack save area, which
857 is machine-dependent. */
859 mem = gen_rtx_MEM (Pmode, buf_addr);
860 set_mem_alias_set (mem, setjmp_alias_set);
861 emit_move_insn (mem, targetm.builtin_setjmp_frame_value ());
863 mem = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
864 GET_MODE_SIZE (Pmode))),
865 set_mem_alias_set (mem, setjmp_alias_set);
867 emit_move_insn (validize_mem (mem),
868 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, receiver_label)));
870 stack_save = gen_rtx_MEM (sa_mode,
871 plus_constant (Pmode, buf_addr,
872 2 * GET_MODE_SIZE (Pmode)));
873 set_mem_alias_set (stack_save, setjmp_alias_set);
874 emit_stack_save (SAVE_NONLOCAL, &stack_save);
876 /* If there is further processing to do, do it. */
877 if (targetm.have_builtin_setjmp_setup ())
878 emit_insn (targetm.gen_builtin_setjmp_setup (buf_addr));
880 /* We have a nonlocal label. */
881 cfun->has_nonlocal_label = 1;
884 /* Construct the trailing part of a __builtin_setjmp call. This is
885 also called directly by the SJLJ exception handling code.
886 If RECEIVER_LABEL is NULL, instead contruct a nonlocal goto handler. */
888 void
889 expand_builtin_setjmp_receiver (rtx receiver_label)
891 rtx chain;
893 /* Mark the FP as used when we get here, so we have to make sure it's
894 marked as used by this function. */
895 emit_use (hard_frame_pointer_rtx);
897 /* Mark the static chain as clobbered here so life information
898 doesn't get messed up for it. */
899 chain = rtx_for_static_chain (current_function_decl, true);
900 if (chain && REG_P (chain))
901 emit_clobber (chain);
903 /* Now put in the code to restore the frame pointer, and argument
904 pointer, if needed. */
905 if (! targetm.have_nonlocal_goto ())
907 /* First adjust our frame pointer to its actual value. It was
908 previously set to the start of the virtual area corresponding to
909 the stacked variables when we branched here and now needs to be
910 adjusted to the actual hardware fp value.
912 Assignments to virtual registers are converted by
913 instantiate_virtual_regs into the corresponding assignment
914 to the underlying register (fp in this case) that makes
915 the original assignment true.
916 So the following insn will actually be decrementing fp by
917 TARGET_STARTING_FRAME_OFFSET. */
918 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
920 /* Restoring the frame pointer also modifies the hard frame pointer.
921 Mark it used (so that the previous assignment remains live once
922 the frame pointer is eliminated) and clobbered (to represent the
923 implicit update from the assignment). */
924 emit_use (hard_frame_pointer_rtx);
925 emit_clobber (hard_frame_pointer_rtx);
928 if (!HARD_FRAME_POINTER_IS_ARG_POINTER && fixed_regs[ARG_POINTER_REGNUM])
930 /* If the argument pointer can be eliminated in favor of the
931 frame pointer, we don't need to restore it. We assume here
932 that if such an elimination is present, it can always be used.
933 This is the case on all known machines; if we don't make this
934 assumption, we do unnecessary saving on many machines. */
935 size_t i;
936 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
938 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
939 if (elim_regs[i].from == ARG_POINTER_REGNUM
940 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
941 break;
943 if (i == ARRAY_SIZE (elim_regs))
945 /* Now restore our arg pointer from the address at which it
946 was saved in our stack frame. */
947 emit_move_insn (crtl->args.internal_arg_pointer,
948 copy_to_reg (get_arg_pointer_save_area ()));
952 if (receiver_label != NULL && targetm.have_builtin_setjmp_receiver ())
953 emit_insn (targetm.gen_builtin_setjmp_receiver (receiver_label));
954 else if (targetm.have_nonlocal_goto_receiver ())
955 emit_insn (targetm.gen_nonlocal_goto_receiver ());
956 else
957 { /* Nothing */ }
959 /* We must not allow the code we just generated to be reordered by
960 scheduling. Specifically, the update of the frame pointer must
961 happen immediately, not later. */
962 emit_insn (gen_blockage ());
965 /* __builtin_longjmp is passed a pointer to an array of five words (not
966 all will be used on all machines). It operates similarly to the C
967 library function of the same name, but is more efficient. Much of
968 the code below is copied from the handling of non-local gotos. */
970 static void
971 expand_builtin_longjmp (rtx buf_addr, rtx value)
973 rtx fp, lab, stack;
974 rtx_insn *insn, *last;
975 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
977 /* DRAP is needed for stack realign if longjmp is expanded to current
978 function */
979 if (SUPPORTS_STACK_ALIGNMENT)
980 crtl->need_drap = true;
982 if (setjmp_alias_set == -1)
983 setjmp_alias_set = new_alias_set ();
985 buf_addr = convert_memory_address (Pmode, buf_addr);
987 buf_addr = force_reg (Pmode, buf_addr);
989 /* We require that the user must pass a second argument of 1, because
990 that is what builtin_setjmp will return. */
991 gcc_assert (value == const1_rtx);
993 last = get_last_insn ();
994 if (targetm.have_builtin_longjmp ())
995 emit_insn (targetm.gen_builtin_longjmp (buf_addr));
996 else
998 fp = gen_rtx_MEM (Pmode, buf_addr);
999 lab = gen_rtx_MEM (Pmode, plus_constant (Pmode, buf_addr,
1000 GET_MODE_SIZE (Pmode)));
1002 stack = gen_rtx_MEM (sa_mode, plus_constant (Pmode, buf_addr,
1003 2 * GET_MODE_SIZE (Pmode)));
1004 set_mem_alias_set (fp, setjmp_alias_set);
1005 set_mem_alias_set (lab, setjmp_alias_set);
1006 set_mem_alias_set (stack, setjmp_alias_set);
1008 /* Pick up FP, label, and SP from the block and jump. This code is
1009 from expand_goto in stmt.c; see there for detailed comments. */
1010 if (targetm.have_nonlocal_goto ())
1011 /* We have to pass a value to the nonlocal_goto pattern that will
1012 get copied into the static_chain pointer, but it does not matter
1013 what that value is, because builtin_setjmp does not use it. */
1014 emit_insn (targetm.gen_nonlocal_goto (value, lab, stack, fp));
1015 else
1017 lab = copy_to_reg (lab);
1019 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1020 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1022 emit_move_insn (hard_frame_pointer_rtx, fp);
1023 emit_stack_restore (SAVE_NONLOCAL, stack);
1025 emit_use (hard_frame_pointer_rtx);
1026 emit_use (stack_pointer_rtx);
1027 emit_indirect_jump (lab);
1031 /* Search backwards and mark the jump insn as a non-local goto.
1032 Note that this precludes the use of __builtin_longjmp to a
1033 __builtin_setjmp target in the same function. However, we've
1034 already cautioned the user that these functions are for
1035 internal exception handling use only. */
1036 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1038 gcc_assert (insn != last);
1040 if (JUMP_P (insn))
1042 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1043 break;
1045 else if (CALL_P (insn))
1046 break;
1050 static inline bool
1051 more_const_call_expr_args_p (const const_call_expr_arg_iterator *iter)
1053 return (iter->i < iter->n);
1056 /* This function validates the types of a function call argument list
1057 against a specified list of tree_codes. If the last specifier is a 0,
1058 that represents an ellipsis, otherwise the last specifier must be a
1059 VOID_TYPE. */
1061 static bool
1062 validate_arglist (const_tree callexpr, ...)
1064 enum tree_code code;
1065 bool res = 0;
1066 va_list ap;
1067 const_call_expr_arg_iterator iter;
1068 const_tree arg;
1070 va_start (ap, callexpr);
1071 init_const_call_expr_arg_iterator (callexpr, &iter);
1073 /* Get a bitmap of pointer argument numbers declared attribute nonnull. */
1074 tree fn = CALL_EXPR_FN (callexpr);
1075 bitmap argmap = get_nonnull_args (TREE_TYPE (TREE_TYPE (fn)));
1077 for (unsigned argno = 1; ; ++argno)
1079 code = (enum tree_code) va_arg (ap, int);
1081 switch (code)
1083 case 0:
1084 /* This signifies an ellipses, any further arguments are all ok. */
1085 res = true;
1086 goto end;
1087 case VOID_TYPE:
1088 /* This signifies an endlink, if no arguments remain, return
1089 true, otherwise return false. */
1090 res = !more_const_call_expr_args_p (&iter);
1091 goto end;
1092 case POINTER_TYPE:
1093 /* The actual argument must be nonnull when either the whole
1094 called function has been declared nonnull, or when the formal
1095 argument corresponding to the actual argument has been. */
1096 if (argmap
1097 && (bitmap_empty_p (argmap) || bitmap_bit_p (argmap, argno)))
1099 arg = next_const_call_expr_arg (&iter);
1100 if (!validate_arg (arg, code) || integer_zerop (arg))
1101 goto end;
1102 break;
1104 /* FALLTHRU */
1105 default:
1106 /* If no parameters remain or the parameter's code does not
1107 match the specified code, return false. Otherwise continue
1108 checking any remaining arguments. */
1109 arg = next_const_call_expr_arg (&iter);
1110 if (!validate_arg (arg, code))
1111 goto end;
1112 break;
1116 /* We need gotos here since we can only have one VA_CLOSE in a
1117 function. */
1118 end: ;
1119 va_end (ap);
1121 BITMAP_FREE (argmap);
1123 return res;
1126 /* Expand a call to __builtin_nonlocal_goto. We're passed the target label
1127 and the address of the save area. */
1129 static rtx
1130 expand_builtin_nonlocal_goto (tree exp)
1132 tree t_label, t_save_area;
1133 rtx r_label, r_save_area, r_fp, r_sp;
1134 rtx_insn *insn;
1136 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
1137 return NULL_RTX;
1139 t_label = CALL_EXPR_ARG (exp, 0);
1140 t_save_area = CALL_EXPR_ARG (exp, 1);
1142 r_label = expand_normal (t_label);
1143 r_label = convert_memory_address (Pmode, r_label);
1144 r_save_area = expand_normal (t_save_area);
1145 r_save_area = convert_memory_address (Pmode, r_save_area);
1146 /* Copy the address of the save location to a register just in case it was
1147 based on the frame pointer. */
1148 r_save_area = copy_to_reg (r_save_area);
1149 r_fp = gen_rtx_MEM (Pmode, r_save_area);
1150 r_sp = gen_rtx_MEM (STACK_SAVEAREA_MODE (SAVE_NONLOCAL),
1151 plus_constant (Pmode, r_save_area,
1152 GET_MODE_SIZE (Pmode)));
1154 crtl->has_nonlocal_goto = 1;
1156 /* ??? We no longer need to pass the static chain value, afaik. */
1157 if (targetm.have_nonlocal_goto ())
1158 emit_insn (targetm.gen_nonlocal_goto (const0_rtx, r_label, r_sp, r_fp));
1159 else
1161 r_label = copy_to_reg (r_label);
1163 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1164 emit_clobber (gen_rtx_MEM (BLKmode, hard_frame_pointer_rtx));
1166 /* Restore frame pointer for containing function. */
1167 emit_move_insn (hard_frame_pointer_rtx, r_fp);
1168 emit_stack_restore (SAVE_NONLOCAL, r_sp);
1170 /* USE of hard_frame_pointer_rtx added for consistency;
1171 not clear if really needed. */
1172 emit_use (hard_frame_pointer_rtx);
1173 emit_use (stack_pointer_rtx);
1175 /* If the architecture is using a GP register, we must
1176 conservatively assume that the target function makes use of it.
1177 The prologue of functions with nonlocal gotos must therefore
1178 initialize the GP register to the appropriate value, and we
1179 must then make sure that this value is live at the point
1180 of the jump. (Note that this doesn't necessarily apply
1181 to targets with a nonlocal_goto pattern; they are free
1182 to implement it in their own way. Note also that this is
1183 a no-op if the GP register is a global invariant.) */
1184 unsigned regnum = PIC_OFFSET_TABLE_REGNUM;
1185 if (regnum != INVALID_REGNUM && fixed_regs[regnum])
1186 emit_use (pic_offset_table_rtx);
1188 emit_indirect_jump (r_label);
1191 /* Search backwards to the jump insn and mark it as a
1192 non-local goto. */
1193 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1195 if (JUMP_P (insn))
1197 add_reg_note (insn, REG_NON_LOCAL_GOTO, const0_rtx);
1198 break;
1200 else if (CALL_P (insn))
1201 break;
1204 return const0_rtx;
1207 /* __builtin_update_setjmp_buf is passed a pointer to an array of five words
1208 (not all will be used on all machines) that was passed to __builtin_setjmp.
1209 It updates the stack pointer in that block to the current value. This is
1210 also called directly by the SJLJ exception handling code. */
1212 void
1213 expand_builtin_update_setjmp_buf (rtx buf_addr)
1215 machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
1216 buf_addr = convert_memory_address (Pmode, buf_addr);
1217 rtx stack_save
1218 = gen_rtx_MEM (sa_mode,
1219 memory_address
1220 (sa_mode,
1221 plus_constant (Pmode, buf_addr,
1222 2 * GET_MODE_SIZE (Pmode))));
1224 emit_stack_save (SAVE_NONLOCAL, &stack_save);
1227 /* Expand a call to __builtin_prefetch. For a target that does not support
1228 data prefetch, evaluate the memory address argument in case it has side
1229 effects. */
1231 static void
1232 expand_builtin_prefetch (tree exp)
1234 tree arg0, arg1, arg2;
1235 int nargs;
1236 rtx op0, op1, op2;
1238 if (!validate_arglist (exp, POINTER_TYPE, 0))
1239 return;
1241 arg0 = CALL_EXPR_ARG (exp, 0);
1243 /* Arguments 1 and 2 are optional; argument 1 (read/write) defaults to
1244 zero (read) and argument 2 (locality) defaults to 3 (high degree of
1245 locality). */
1246 nargs = call_expr_nargs (exp);
1247 if (nargs > 1)
1248 arg1 = CALL_EXPR_ARG (exp, 1);
1249 else
1250 arg1 = integer_zero_node;
1251 if (nargs > 2)
1252 arg2 = CALL_EXPR_ARG (exp, 2);
1253 else
1254 arg2 = integer_three_node;
1256 /* Argument 0 is an address. */
1257 op0 = expand_expr (arg0, NULL_RTX, Pmode, EXPAND_NORMAL);
1259 /* Argument 1 (read/write flag) must be a compile-time constant int. */
1260 if (TREE_CODE (arg1) != INTEGER_CST)
1262 error ("second argument to %<__builtin_prefetch%> must be a constant");
1263 arg1 = integer_zero_node;
1265 op1 = expand_normal (arg1);
1266 /* Argument 1 must be either zero or one. */
1267 if (INTVAL (op1) != 0 && INTVAL (op1) != 1)
1269 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
1270 " using zero");
1271 op1 = const0_rtx;
1274 /* Argument 2 (locality) must be a compile-time constant int. */
1275 if (TREE_CODE (arg2) != INTEGER_CST)
1277 error ("third argument to %<__builtin_prefetch%> must be a constant");
1278 arg2 = integer_zero_node;
1280 op2 = expand_normal (arg2);
1281 /* Argument 2 must be 0, 1, 2, or 3. */
1282 if (INTVAL (op2) < 0 || INTVAL (op2) > 3)
1284 warning (0, "invalid third argument to %<__builtin_prefetch%>; using zero");
1285 op2 = const0_rtx;
1288 if (targetm.have_prefetch ())
1290 struct expand_operand ops[3];
1292 create_address_operand (&ops[0], op0);
1293 create_integer_operand (&ops[1], INTVAL (op1));
1294 create_integer_operand (&ops[2], INTVAL (op2));
1295 if (maybe_expand_insn (targetm.code_for_prefetch, 3, ops))
1296 return;
1299 /* Don't do anything with direct references to volatile memory, but
1300 generate code to handle other side effects. */
1301 if (!MEM_P (op0) && side_effects_p (op0))
1302 emit_insn (op0);
1305 /* Get a MEM rtx for expression EXP which is the address of an operand
1306 to be used in a string instruction (cmpstrsi, movmemsi, ..). LEN is
1307 the maximum length of the block of memory that might be accessed or
1308 NULL if unknown. */
1310 static rtx
1311 get_memory_rtx (tree exp, tree len)
1313 tree orig_exp = exp;
1314 rtx addr, mem;
1316 /* When EXP is not resolved SAVE_EXPR, MEM_ATTRS can be still derived
1317 from its expression, for expr->a.b only <variable>.a.b is recorded. */
1318 if (TREE_CODE (exp) == SAVE_EXPR && !SAVE_EXPR_RESOLVED_P (exp))
1319 exp = TREE_OPERAND (exp, 0);
1321 addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
1322 mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
1324 /* Get an expression we can use to find the attributes to assign to MEM.
1325 First remove any nops. */
1326 while (CONVERT_EXPR_P (exp)
1327 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
1328 exp = TREE_OPERAND (exp, 0);
1330 /* Build a MEM_REF representing the whole accessed area as a byte blob,
1331 (as builtin stringops may alias with anything). */
1332 exp = fold_build2 (MEM_REF,
1333 build_array_type (char_type_node,
1334 build_range_type (sizetype,
1335 size_one_node, len)),
1336 exp, build_int_cst (ptr_type_node, 0));
1338 /* If the MEM_REF has no acceptable address, try to get the base object
1339 from the original address we got, and build an all-aliasing
1340 unknown-sized access to that one. */
1341 if (is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
1342 set_mem_attributes (mem, exp, 0);
1343 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
1344 && (exp = get_base_address (TREE_OPERAND (TREE_OPERAND (exp, 0),
1345 0))))
1347 exp = build_fold_addr_expr (exp);
1348 exp = fold_build2 (MEM_REF,
1349 build_array_type (char_type_node,
1350 build_range_type (sizetype,
1351 size_zero_node,
1352 NULL)),
1353 exp, build_int_cst (ptr_type_node, 0));
1354 set_mem_attributes (mem, exp, 0);
1356 set_mem_alias_set (mem, 0);
1357 return mem;
1360 /* Built-in functions to perform an untyped call and return. */
1362 #define apply_args_mode \
1363 (this_target_builtins->x_apply_args_mode)
1364 #define apply_result_mode \
1365 (this_target_builtins->x_apply_result_mode)
1367 /* Return the size required for the block returned by __builtin_apply_args,
1368 and initialize apply_args_mode. */
1370 static int
1371 apply_args_size (void)
1373 static int size = -1;
1374 int align;
1375 unsigned int regno;
1377 /* The values computed by this function never change. */
1378 if (size < 0)
1380 /* The first value is the incoming arg-pointer. */
1381 size = GET_MODE_SIZE (Pmode);
1383 /* The second value is the structure value address unless this is
1384 passed as an "invisible" first argument. */
1385 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1386 size += GET_MODE_SIZE (Pmode);
1388 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1389 if (FUNCTION_ARG_REGNO_P (regno))
1391 fixed_size_mode mode = targetm.calls.get_raw_arg_mode (regno);
1393 gcc_assert (mode != VOIDmode);
1395 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1396 if (size % align != 0)
1397 size = CEIL (size, align) * align;
1398 size += GET_MODE_SIZE (mode);
1399 apply_args_mode[regno] = mode;
1401 else
1403 apply_args_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1406 return size;
1409 /* Return the size required for the block returned by __builtin_apply,
1410 and initialize apply_result_mode. */
1412 static int
1413 apply_result_size (void)
1415 static int size = -1;
1416 int align, regno;
1418 /* The values computed by this function never change. */
1419 if (size < 0)
1421 size = 0;
1423 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1424 if (targetm.calls.function_value_regno_p (regno))
1426 fixed_size_mode mode = targetm.calls.get_raw_result_mode (regno);
1428 gcc_assert (mode != VOIDmode);
1430 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1431 if (size % align != 0)
1432 size = CEIL (size, align) * align;
1433 size += GET_MODE_SIZE (mode);
1434 apply_result_mode[regno] = mode;
1436 else
1437 apply_result_mode[regno] = as_a <fixed_size_mode> (VOIDmode);
1439 /* Allow targets that use untyped_call and untyped_return to override
1440 the size so that machine-specific information can be stored here. */
1441 #ifdef APPLY_RESULT_SIZE
1442 size = APPLY_RESULT_SIZE;
1443 #endif
1445 return size;
1448 /* Create a vector describing the result block RESULT. If SAVEP is true,
1449 the result block is used to save the values; otherwise it is used to
1450 restore the values. */
1452 static rtx
1453 result_vector (int savep, rtx result)
1455 int regno, size, align, nelts;
1456 fixed_size_mode mode;
1457 rtx reg, mem;
1458 rtx *savevec = XALLOCAVEC (rtx, FIRST_PSEUDO_REGISTER);
1460 size = nelts = 0;
1461 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1462 if ((mode = apply_result_mode[regno]) != VOIDmode)
1464 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1465 if (size % align != 0)
1466 size = CEIL (size, align) * align;
1467 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
1468 mem = adjust_address (result, mode, size);
1469 savevec[nelts++] = (savep
1470 ? gen_rtx_SET (mem, reg)
1471 : gen_rtx_SET (reg, mem));
1472 size += GET_MODE_SIZE (mode);
1474 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
1477 /* Save the state required to perform an untyped call with the same
1478 arguments as were passed to the current function. */
1480 static rtx
1481 expand_builtin_apply_args_1 (void)
1483 rtx registers, tem;
1484 int size, align, regno;
1485 fixed_size_mode mode;
1486 rtx struct_incoming_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 1);
1488 /* Create a block where the arg-pointer, structure value address,
1489 and argument registers can be saved. */
1490 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
1492 /* Walk past the arg-pointer and structure value address. */
1493 size = GET_MODE_SIZE (Pmode);
1494 if (targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0))
1495 size += GET_MODE_SIZE (Pmode);
1497 /* Save each register used in calling a function to the block. */
1498 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1499 if ((mode = apply_args_mode[regno]) != VOIDmode)
1501 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1502 if (size % align != 0)
1503 size = CEIL (size, align) * align;
1505 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1507 emit_move_insn (adjust_address (registers, mode, size), tem);
1508 size += GET_MODE_SIZE (mode);
1511 /* Save the arg pointer to the block. */
1512 tem = copy_to_reg (crtl->args.internal_arg_pointer);
1513 /* We need the pointer as the caller actually passed them to us, not
1514 as we might have pretended they were passed. Make sure it's a valid
1515 operand, as emit_move_insn isn't expected to handle a PLUS. */
1516 if (STACK_GROWS_DOWNWARD)
1518 = force_operand (plus_constant (Pmode, tem,
1519 crtl->args.pretend_args_size),
1520 NULL_RTX);
1521 emit_move_insn (adjust_address (registers, Pmode, 0), tem);
1523 size = GET_MODE_SIZE (Pmode);
1525 /* Save the structure value address unless this is passed as an
1526 "invisible" first argument. */
1527 if (struct_incoming_value)
1529 emit_move_insn (adjust_address (registers, Pmode, size),
1530 copy_to_reg (struct_incoming_value));
1531 size += GET_MODE_SIZE (Pmode);
1534 /* Return the address of the block. */
1535 return copy_addr_to_reg (XEXP (registers, 0));
1538 /* __builtin_apply_args returns block of memory allocated on
1539 the stack into which is stored the arg pointer, structure
1540 value address, static chain, and all the registers that might
1541 possibly be used in performing a function call. The code is
1542 moved to the start of the function so the incoming values are
1543 saved. */
1545 static rtx
1546 expand_builtin_apply_args (void)
1548 /* Don't do __builtin_apply_args more than once in a function.
1549 Save the result of the first call and reuse it. */
1550 if (apply_args_value != 0)
1551 return apply_args_value;
1553 /* When this function is called, it means that registers must be
1554 saved on entry to this function. So we migrate the
1555 call to the first insn of this function. */
1556 rtx temp;
1558 start_sequence ();
1559 temp = expand_builtin_apply_args_1 ();
1560 rtx_insn *seq = get_insns ();
1561 end_sequence ();
1563 apply_args_value = temp;
1565 /* Put the insns after the NOTE that starts the function.
1566 If this is inside a start_sequence, make the outer-level insn
1567 chain current, so the code is placed at the start of the
1568 function. If internal_arg_pointer is a non-virtual pseudo,
1569 it needs to be placed after the function that initializes
1570 that pseudo. */
1571 push_topmost_sequence ();
1572 if (REG_P (crtl->args.internal_arg_pointer)
1573 && REGNO (crtl->args.internal_arg_pointer) > LAST_VIRTUAL_REGISTER)
1574 emit_insn_before (seq, parm_birth_insn);
1575 else
1576 emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
1577 pop_topmost_sequence ();
1578 return temp;
1582 /* Perform an untyped call and save the state required to perform an
1583 untyped return of whatever value was returned by the given function. */
1585 static rtx
1586 expand_builtin_apply (rtx function, rtx arguments, rtx argsize)
1588 int size, align, regno;
1589 fixed_size_mode mode;
1590 rtx incoming_args, result, reg, dest, src;
1591 rtx_call_insn *call_insn;
1592 rtx old_stack_level = 0;
1593 rtx call_fusage = 0;
1594 rtx struct_value = targetm.calls.struct_value_rtx (cfun ? TREE_TYPE (cfun->decl) : 0, 0);
1596 arguments = convert_memory_address (Pmode, arguments);
1598 /* Create a block where the return registers can be saved. */
1599 result = assign_stack_local (BLKmode, apply_result_size (), -1);
1601 /* Fetch the arg pointer from the ARGUMENTS block. */
1602 incoming_args = gen_reg_rtx (Pmode);
1603 emit_move_insn (incoming_args, gen_rtx_MEM (Pmode, arguments));
1604 if (!STACK_GROWS_DOWNWARD)
1605 incoming_args = expand_simple_binop (Pmode, MINUS, incoming_args, argsize,
1606 incoming_args, 0, OPTAB_LIB_WIDEN);
1608 /* Push a new argument block and copy the arguments. Do not allow
1609 the (potential) memcpy call below to interfere with our stack
1610 manipulations. */
1611 do_pending_stack_adjust ();
1612 NO_DEFER_POP;
1614 /* Save the stack with nonlocal if available. */
1615 if (targetm.have_save_stack_nonlocal ())
1616 emit_stack_save (SAVE_NONLOCAL, &old_stack_level);
1617 else
1618 emit_stack_save (SAVE_BLOCK, &old_stack_level);
1620 /* Allocate a block of memory onto the stack and copy the memory
1621 arguments to the outgoing arguments address. We can pass TRUE
1622 as the 4th argument because we just saved the stack pointer
1623 and will restore it right after the call. */
1624 allocate_dynamic_stack_space (argsize, 0, BIGGEST_ALIGNMENT, -1, true);
1626 /* Set DRAP flag to true, even though allocate_dynamic_stack_space
1627 may have already set current_function_calls_alloca to true.
1628 current_function_calls_alloca won't be set if argsize is zero,
1629 so we have to guarantee need_drap is true here. */
1630 if (SUPPORTS_STACK_ALIGNMENT)
1631 crtl->need_drap = true;
1633 dest = virtual_outgoing_args_rtx;
1634 if (!STACK_GROWS_DOWNWARD)
1636 if (CONST_INT_P (argsize))
1637 dest = plus_constant (Pmode, dest, -INTVAL (argsize));
1638 else
1639 dest = gen_rtx_PLUS (Pmode, dest, negate_rtx (Pmode, argsize));
1641 dest = gen_rtx_MEM (BLKmode, dest);
1642 set_mem_align (dest, PARM_BOUNDARY);
1643 src = gen_rtx_MEM (BLKmode, incoming_args);
1644 set_mem_align (src, PARM_BOUNDARY);
1645 emit_block_move (dest, src, argsize, BLOCK_OP_NORMAL);
1647 /* Refer to the argument block. */
1648 apply_args_size ();
1649 arguments = gen_rtx_MEM (BLKmode, arguments);
1650 set_mem_align (arguments, PARM_BOUNDARY);
1652 /* Walk past the arg-pointer and structure value address. */
1653 size = GET_MODE_SIZE (Pmode);
1654 if (struct_value)
1655 size += GET_MODE_SIZE (Pmode);
1657 /* Restore each of the registers previously saved. Make USE insns
1658 for each of these registers for use in making the call. */
1659 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1660 if ((mode = apply_args_mode[regno]) != VOIDmode)
1662 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1663 if (size % align != 0)
1664 size = CEIL (size, align) * align;
1665 reg = gen_rtx_REG (mode, regno);
1666 emit_move_insn (reg, adjust_address (arguments, mode, size));
1667 use_reg (&call_fusage, reg);
1668 size += GET_MODE_SIZE (mode);
1671 /* Restore the structure value address unless this is passed as an
1672 "invisible" first argument. */
1673 size = GET_MODE_SIZE (Pmode);
1674 if (struct_value)
1676 rtx value = gen_reg_rtx (Pmode);
1677 emit_move_insn (value, adjust_address (arguments, Pmode, size));
1678 emit_move_insn (struct_value, value);
1679 if (REG_P (struct_value))
1680 use_reg (&call_fusage, struct_value);
1681 size += GET_MODE_SIZE (Pmode);
1684 /* All arguments and registers used for the call are set up by now! */
1685 function = prepare_call_address (NULL, function, NULL, &call_fusage, 0, 0);
1687 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
1688 and we don't want to load it into a register as an optimization,
1689 because prepare_call_address already did it if it should be done. */
1690 if (GET_CODE (function) != SYMBOL_REF)
1691 function = memory_address (FUNCTION_MODE, function);
1693 /* Generate the actual call instruction and save the return value. */
1694 if (targetm.have_untyped_call ())
1696 rtx mem = gen_rtx_MEM (FUNCTION_MODE, function);
1697 emit_call_insn (targetm.gen_untyped_call (mem, result,
1698 result_vector (1, result)));
1700 else if (targetm.have_call_value ())
1702 rtx valreg = 0;
1704 /* Locate the unique return register. It is not possible to
1705 express a call that sets more than one return register using
1706 call_value; use untyped_call for that. In fact, untyped_call
1707 only needs to save the return registers in the given block. */
1708 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1709 if ((mode = apply_result_mode[regno]) != VOIDmode)
1711 gcc_assert (!valreg); /* have_untyped_call required. */
1713 valreg = gen_rtx_REG (mode, regno);
1716 emit_insn (targetm.gen_call_value (valreg,
1717 gen_rtx_MEM (FUNCTION_MODE, function),
1718 const0_rtx, NULL_RTX, const0_rtx));
1720 emit_move_insn (adjust_address (result, GET_MODE (valreg), 0), valreg);
1722 else
1723 gcc_unreachable ();
1725 /* Find the CALL insn we just emitted, and attach the register usage
1726 information. */
1727 call_insn = last_call_insn ();
1728 add_function_usage_to (call_insn, call_fusage);
1730 /* Restore the stack. */
1731 if (targetm.have_save_stack_nonlocal ())
1732 emit_stack_restore (SAVE_NONLOCAL, old_stack_level);
1733 else
1734 emit_stack_restore (SAVE_BLOCK, old_stack_level);
1735 fixup_args_size_notes (call_insn, get_last_insn (), 0);
1737 OK_DEFER_POP;
1739 /* Return the address of the result block. */
1740 result = copy_addr_to_reg (XEXP (result, 0));
1741 return convert_memory_address (ptr_mode, result);
1744 /* Perform an untyped return. */
1746 static void
1747 expand_builtin_return (rtx result)
1749 int size, align, regno;
1750 fixed_size_mode mode;
1751 rtx reg;
1752 rtx_insn *call_fusage = 0;
1754 result = convert_memory_address (Pmode, result);
1756 apply_result_size ();
1757 result = gen_rtx_MEM (BLKmode, result);
1759 if (targetm.have_untyped_return ())
1761 rtx vector = result_vector (0, result);
1762 emit_jump_insn (targetm.gen_untyped_return (result, vector));
1763 emit_barrier ();
1764 return;
1767 /* Restore the return value and note that each value is used. */
1768 size = 0;
1769 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1770 if ((mode = apply_result_mode[regno]) != VOIDmode)
1772 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1773 if (size % align != 0)
1774 size = CEIL (size, align) * align;
1775 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1776 emit_move_insn (reg, adjust_address (result, mode, size));
1778 push_to_sequence (call_fusage);
1779 emit_use (reg);
1780 call_fusage = get_insns ();
1781 end_sequence ();
1782 size += GET_MODE_SIZE (mode);
1785 /* Put the USE insns before the return. */
1786 emit_insn (call_fusage);
1788 /* Return whatever values was restored by jumping directly to the end
1789 of the function. */
1790 expand_naked_return ();
1793 /* Used by expand_builtin_classify_type and fold_builtin_classify_type. */
1795 static enum type_class
1796 type_to_class (tree type)
1798 switch (TREE_CODE (type))
1800 case VOID_TYPE: return void_type_class;
1801 case INTEGER_TYPE: return integer_type_class;
1802 case ENUMERAL_TYPE: return enumeral_type_class;
1803 case BOOLEAN_TYPE: return boolean_type_class;
1804 case POINTER_TYPE: return pointer_type_class;
1805 case REFERENCE_TYPE: return reference_type_class;
1806 case OFFSET_TYPE: return offset_type_class;
1807 case REAL_TYPE: return real_type_class;
1808 case COMPLEX_TYPE: return complex_type_class;
1809 case FUNCTION_TYPE: return function_type_class;
1810 case METHOD_TYPE: return method_type_class;
1811 case RECORD_TYPE: return record_type_class;
1812 case UNION_TYPE:
1813 case QUAL_UNION_TYPE: return union_type_class;
1814 case ARRAY_TYPE: return (TYPE_STRING_FLAG (type)
1815 ? string_type_class : array_type_class);
1816 case LANG_TYPE: return lang_type_class;
1817 default: return no_type_class;
1821 /* Expand a call EXP to __builtin_classify_type. */
1823 static rtx
1824 expand_builtin_classify_type (tree exp)
1826 if (call_expr_nargs (exp))
1827 return GEN_INT (type_to_class (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
1828 return GEN_INT (no_type_class);
1831 /* This helper macro, meant to be used in mathfn_built_in below, determines
1832 which among a set of builtin math functions is appropriate for a given type
1833 mode. The `F' (float) and `L' (long double) are automatically generated
1834 from the 'double' case. If a function supports the _Float<N> and _Float<N>X
1835 types, there are additional types that are considered with 'F32', 'F64',
1836 'F128', etc. suffixes. */
1837 #define CASE_MATHFN(MATHFN) \
1838 CASE_CFN_##MATHFN: \
1839 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1840 fcodel = BUILT_IN_##MATHFN##L ; break;
1841 /* Similar to the above, but also add support for the _Float<N> and _Float<N>X
1842 types. */
1843 #define CASE_MATHFN_FLOATN(MATHFN) \
1844 CASE_CFN_##MATHFN: \
1845 fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
1846 fcodel = BUILT_IN_##MATHFN##L ; fcodef16 = BUILT_IN_##MATHFN##F16 ; \
1847 fcodef32 = BUILT_IN_##MATHFN##F32; fcodef64 = BUILT_IN_##MATHFN##F64 ; \
1848 fcodef128 = BUILT_IN_##MATHFN##F128 ; fcodef32x = BUILT_IN_##MATHFN##F32X ; \
1849 fcodef64x = BUILT_IN_##MATHFN##F64X ; fcodef128x = BUILT_IN_##MATHFN##F128X ;\
1850 break;
1851 /* Similar to above, but appends _R after any F/L suffix. */
1852 #define CASE_MATHFN_REENT(MATHFN) \
1853 case CFN_BUILT_IN_##MATHFN##_R: \
1854 case CFN_BUILT_IN_##MATHFN##F_R: \
1855 case CFN_BUILT_IN_##MATHFN##L_R: \
1856 fcode = BUILT_IN_##MATHFN##_R; fcodef = BUILT_IN_##MATHFN##F_R ; \
1857 fcodel = BUILT_IN_##MATHFN##L_R ; break;
1859 /* Return a function equivalent to FN but operating on floating-point
1860 values of type TYPE, or END_BUILTINS if no such function exists.
1861 This is purely an operation on function codes; it does not guarantee
1862 that the target actually has an implementation of the function. */
1864 static built_in_function
1865 mathfn_built_in_2 (tree type, combined_fn fn)
1867 tree mtype;
1868 built_in_function fcode, fcodef, fcodel;
1869 built_in_function fcodef16 = END_BUILTINS;
1870 built_in_function fcodef32 = END_BUILTINS;
1871 built_in_function fcodef64 = END_BUILTINS;
1872 built_in_function fcodef128 = END_BUILTINS;
1873 built_in_function fcodef32x = END_BUILTINS;
1874 built_in_function fcodef64x = END_BUILTINS;
1875 built_in_function fcodef128x = END_BUILTINS;
1877 switch (fn)
1879 CASE_MATHFN (ACOS)
1880 CASE_MATHFN (ACOSH)
1881 CASE_MATHFN (ASIN)
1882 CASE_MATHFN (ASINH)
1883 CASE_MATHFN (ATAN)
1884 CASE_MATHFN (ATAN2)
1885 CASE_MATHFN (ATANH)
1886 CASE_MATHFN (CBRT)
1887 CASE_MATHFN_FLOATN (CEIL)
1888 CASE_MATHFN (CEXPI)
1889 CASE_MATHFN_FLOATN (COPYSIGN)
1890 CASE_MATHFN (COS)
1891 CASE_MATHFN (COSH)
1892 CASE_MATHFN (DREM)
1893 CASE_MATHFN (ERF)
1894 CASE_MATHFN (ERFC)
1895 CASE_MATHFN (EXP)
1896 CASE_MATHFN (EXP10)
1897 CASE_MATHFN (EXP2)
1898 CASE_MATHFN (EXPM1)
1899 CASE_MATHFN (FABS)
1900 CASE_MATHFN (FDIM)
1901 CASE_MATHFN_FLOATN (FLOOR)
1902 CASE_MATHFN_FLOATN (FMA)
1903 CASE_MATHFN_FLOATN (FMAX)
1904 CASE_MATHFN_FLOATN (FMIN)
1905 CASE_MATHFN (FMOD)
1906 CASE_MATHFN (FREXP)
1907 CASE_MATHFN (GAMMA)
1908 CASE_MATHFN_REENT (GAMMA) /* GAMMA_R */
1909 CASE_MATHFN (HUGE_VAL)
1910 CASE_MATHFN (HYPOT)
1911 CASE_MATHFN (ILOGB)
1912 CASE_MATHFN (ICEIL)
1913 CASE_MATHFN (IFLOOR)
1914 CASE_MATHFN (INF)
1915 CASE_MATHFN (IRINT)
1916 CASE_MATHFN (IROUND)
1917 CASE_MATHFN (ISINF)
1918 CASE_MATHFN (J0)
1919 CASE_MATHFN (J1)
1920 CASE_MATHFN (JN)
1921 CASE_MATHFN (LCEIL)
1922 CASE_MATHFN (LDEXP)
1923 CASE_MATHFN (LFLOOR)
1924 CASE_MATHFN (LGAMMA)
1925 CASE_MATHFN_REENT (LGAMMA) /* LGAMMA_R */
1926 CASE_MATHFN (LLCEIL)
1927 CASE_MATHFN (LLFLOOR)
1928 CASE_MATHFN (LLRINT)
1929 CASE_MATHFN (LLROUND)
1930 CASE_MATHFN (LOG)
1931 CASE_MATHFN (LOG10)
1932 CASE_MATHFN (LOG1P)
1933 CASE_MATHFN (LOG2)
1934 CASE_MATHFN (LOGB)
1935 CASE_MATHFN (LRINT)
1936 CASE_MATHFN (LROUND)
1937 CASE_MATHFN (MODF)
1938 CASE_MATHFN (NAN)
1939 CASE_MATHFN (NANS)
1940 CASE_MATHFN_FLOATN (NEARBYINT)
1941 CASE_MATHFN (NEXTAFTER)
1942 CASE_MATHFN (NEXTTOWARD)
1943 CASE_MATHFN (POW)
1944 CASE_MATHFN (POWI)
1945 CASE_MATHFN (POW10)
1946 CASE_MATHFN (REMAINDER)
1947 CASE_MATHFN (REMQUO)
1948 CASE_MATHFN_FLOATN (RINT)
1949 CASE_MATHFN_FLOATN (ROUND)
1950 CASE_MATHFN (SCALB)
1951 CASE_MATHFN (SCALBLN)
1952 CASE_MATHFN (SCALBN)
1953 CASE_MATHFN (SIGNBIT)
1954 CASE_MATHFN (SIGNIFICAND)
1955 CASE_MATHFN (SIN)
1956 CASE_MATHFN (SINCOS)
1957 CASE_MATHFN (SINH)
1958 CASE_MATHFN_FLOATN (SQRT)
1959 CASE_MATHFN (TAN)
1960 CASE_MATHFN (TANH)
1961 CASE_MATHFN (TGAMMA)
1962 CASE_MATHFN_FLOATN (TRUNC)
1963 CASE_MATHFN (Y0)
1964 CASE_MATHFN (Y1)
1965 CASE_MATHFN (YN)
1967 default:
1968 return END_BUILTINS;
1971 mtype = TYPE_MAIN_VARIANT (type);
1972 if (mtype == double_type_node)
1973 return fcode;
1974 else if (mtype == float_type_node)
1975 return fcodef;
1976 else if (mtype == long_double_type_node)
1977 return fcodel;
1978 else if (mtype == float16_type_node)
1979 return fcodef16;
1980 else if (mtype == float32_type_node)
1981 return fcodef32;
1982 else if (mtype == float64_type_node)
1983 return fcodef64;
1984 else if (mtype == float128_type_node)
1985 return fcodef128;
1986 else if (mtype == float32x_type_node)
1987 return fcodef32x;
1988 else if (mtype == float64x_type_node)
1989 return fcodef64x;
1990 else if (mtype == float128x_type_node)
1991 return fcodef128x;
1992 else
1993 return END_BUILTINS;
1996 /* Return mathematic function equivalent to FN but operating directly on TYPE,
1997 if available. If IMPLICIT_P is true use the implicit builtin declaration,
1998 otherwise use the explicit declaration. If we can't do the conversion,
1999 return null. */
2001 static tree
2002 mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
2004 built_in_function fcode2 = mathfn_built_in_2 (type, fn);
2005 if (fcode2 == END_BUILTINS)
2006 return NULL_TREE;
2008 if (implicit_p && !builtin_decl_implicit_p (fcode2))
2009 return NULL_TREE;
2011 return builtin_decl_explicit (fcode2);
2014 /* Like mathfn_built_in_1, but always use the implicit array. */
2016 tree
2017 mathfn_built_in (tree type, combined_fn fn)
2019 return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);
2022 /* Like mathfn_built_in_1, but take a built_in_function and
2023 always use the implicit array. */
2025 tree
2026 mathfn_built_in (tree type, enum built_in_function fn)
2028 return mathfn_built_in_1 (type, as_combined_fn (fn), /*implicit=*/ 1);
2031 /* If BUILT_IN_NORMAL function FNDECL has an associated internal function,
2032 return its code, otherwise return IFN_LAST. Note that this function
2033 only tests whether the function is defined in internals.def, not whether
2034 it is actually available on the target. */
2036 internal_fn
2037 associated_internal_fn (tree fndecl)
2039 gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
2040 tree return_type = TREE_TYPE (TREE_TYPE (fndecl));
2041 switch (DECL_FUNCTION_CODE (fndecl))
2043 #define DEF_INTERNAL_FLT_FN(NAME, FLAGS, OPTAB, TYPE) \
2044 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2045 #define DEF_INTERNAL_FLT_FLOATN_FN(NAME, FLAGS, OPTAB, TYPE) \
2046 CASE_FLT_FN (BUILT_IN_##NAME): return IFN_##NAME; \
2047 CASE_FLT_FN_FLOATN_NX (BUILT_IN_##NAME): return IFN_##NAME;
2048 #define DEF_INTERNAL_INT_FN(NAME, FLAGS, OPTAB, TYPE) \
2049 CASE_INT_FN (BUILT_IN_##NAME): return IFN_##NAME;
2050 #include "internal-fn.def"
2052 CASE_FLT_FN (BUILT_IN_POW10):
2053 return IFN_EXP10;
2055 CASE_FLT_FN (BUILT_IN_DREM):
2056 return IFN_REMAINDER;
2058 CASE_FLT_FN (BUILT_IN_SCALBN):
2059 CASE_FLT_FN (BUILT_IN_SCALBLN):
2060 if (REAL_MODE_FORMAT (TYPE_MODE (return_type))->b == 2)
2061 return IFN_LDEXP;
2062 return IFN_LAST;
2064 default:
2065 return IFN_LAST;
2069 /* If CALL is a call to a BUILT_IN_NORMAL function that could be replaced
2070 on the current target by a call to an internal function, return the
2071 code of that internal function, otherwise return IFN_LAST. The caller
2072 is responsible for ensuring that any side-effects of the built-in
2073 call are dealt with correctly. E.g. if CALL sets errno, the caller
2074 must decide that the errno result isn't needed or make it available
2075 in some other way. */
2077 internal_fn
2078 replacement_internal_fn (gcall *call)
2080 if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2082 internal_fn ifn = associated_internal_fn (gimple_call_fndecl (call));
2083 if (ifn != IFN_LAST)
2085 tree_pair types = direct_internal_fn_types (ifn, call);
2086 optimization_type opt_type = bb_optimization_type (gimple_bb (call));
2087 if (direct_internal_fn_supported_p (ifn, types, opt_type))
2088 return ifn;
2091 return IFN_LAST;
2094 /* Expand a call to the builtin trinary math functions (fma).
2095 Return NULL_RTX if a normal call should be emitted rather than expanding the
2096 function in-line. EXP is the expression that is a call to the builtin
2097 function; if convenient, the result should be placed in TARGET.
2098 SUBTARGET may be used as the target for computing one of EXP's
2099 operands. */
2101 static rtx
2102 expand_builtin_mathfn_ternary (tree exp, rtx target, rtx subtarget)
2104 optab builtin_optab;
2105 rtx op0, op1, op2, result;
2106 rtx_insn *insns;
2107 tree fndecl = get_callee_fndecl (exp);
2108 tree arg0, arg1, arg2;
2109 machine_mode mode;
2111 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, REAL_TYPE, VOID_TYPE))
2112 return NULL_RTX;
2114 arg0 = CALL_EXPR_ARG (exp, 0);
2115 arg1 = CALL_EXPR_ARG (exp, 1);
2116 arg2 = CALL_EXPR_ARG (exp, 2);
2118 switch (DECL_FUNCTION_CODE (fndecl))
2120 CASE_FLT_FN (BUILT_IN_FMA):
2121 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
2122 builtin_optab = fma_optab; break;
2123 default:
2124 gcc_unreachable ();
2127 /* Make a suitable register to place result in. */
2128 mode = TYPE_MODE (TREE_TYPE (exp));
2130 /* Before working hard, check whether the instruction is available. */
2131 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2132 return NULL_RTX;
2134 result = gen_reg_rtx (mode);
2136 /* Always stabilize the argument list. */
2137 CALL_EXPR_ARG (exp, 0) = arg0 = builtin_save_expr (arg0);
2138 CALL_EXPR_ARG (exp, 1) = arg1 = builtin_save_expr (arg1);
2139 CALL_EXPR_ARG (exp, 2) = arg2 = builtin_save_expr (arg2);
2141 op0 = expand_expr (arg0, subtarget, VOIDmode, EXPAND_NORMAL);
2142 op1 = expand_normal (arg1);
2143 op2 = expand_normal (arg2);
2145 start_sequence ();
2147 /* Compute into RESULT.
2148 Set RESULT to wherever the result comes back. */
2149 result = expand_ternary_op (mode, builtin_optab, op0, op1, op2,
2150 result, 0);
2152 /* If we were unable to expand via the builtin, stop the sequence
2153 (without outputting the insns) and call to the library function
2154 with the stabilized argument list. */
2155 if (result == 0)
2157 end_sequence ();
2158 return expand_call (exp, target, target == const0_rtx);
2161 /* Output the entire sequence. */
2162 insns = get_insns ();
2163 end_sequence ();
2164 emit_insn (insns);
2166 return result;
2169 /* Expand a call to the builtin sin and cos math functions.
2170 Return NULL_RTX if a normal call should be emitted rather than expanding the
2171 function in-line. EXP is the expression that is a call to the builtin
2172 function; if convenient, the result should be placed in TARGET.
2173 SUBTARGET may be used as the target for computing one of EXP's
2174 operands. */
2176 static rtx
2177 expand_builtin_mathfn_3 (tree exp, rtx target, rtx subtarget)
2179 optab builtin_optab;
2180 rtx op0;
2181 rtx_insn *insns;
2182 tree fndecl = get_callee_fndecl (exp);
2183 machine_mode mode;
2184 tree arg;
2186 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2187 return NULL_RTX;
2189 arg = CALL_EXPR_ARG (exp, 0);
2191 switch (DECL_FUNCTION_CODE (fndecl))
2193 CASE_FLT_FN (BUILT_IN_SIN):
2194 CASE_FLT_FN (BUILT_IN_COS):
2195 builtin_optab = sincos_optab; break;
2196 default:
2197 gcc_unreachable ();
2200 /* Make a suitable register to place result in. */
2201 mode = TYPE_MODE (TREE_TYPE (exp));
2203 /* Check if sincos insn is available, otherwise fallback
2204 to sin or cos insn. */
2205 if (optab_handler (builtin_optab, mode) == CODE_FOR_nothing)
2206 switch (DECL_FUNCTION_CODE (fndecl))
2208 CASE_FLT_FN (BUILT_IN_SIN):
2209 builtin_optab = sin_optab; break;
2210 CASE_FLT_FN (BUILT_IN_COS):
2211 builtin_optab = cos_optab; break;
2212 default:
2213 gcc_unreachable ();
2216 /* Before working hard, check whether the instruction is available. */
2217 if (optab_handler (builtin_optab, mode) != CODE_FOR_nothing)
2219 rtx result = gen_reg_rtx (mode);
2221 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2222 need to expand the argument again. This way, we will not perform
2223 side-effects more the once. */
2224 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2226 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
2228 start_sequence ();
2230 /* Compute into RESULT.
2231 Set RESULT to wherever the result comes back. */
2232 if (builtin_optab == sincos_optab)
2234 int ok;
2236 switch (DECL_FUNCTION_CODE (fndecl))
2238 CASE_FLT_FN (BUILT_IN_SIN):
2239 ok = expand_twoval_unop (builtin_optab, op0, 0, result, 0);
2240 break;
2241 CASE_FLT_FN (BUILT_IN_COS):
2242 ok = expand_twoval_unop (builtin_optab, op0, result, 0, 0);
2243 break;
2244 default:
2245 gcc_unreachable ();
2247 gcc_assert (ok);
2249 else
2250 result = expand_unop (mode, builtin_optab, op0, result, 0);
2252 if (result != 0)
2254 /* Output the entire sequence. */
2255 insns = get_insns ();
2256 end_sequence ();
2257 emit_insn (insns);
2258 return result;
2261 /* If we were unable to expand via the builtin, stop the sequence
2262 (without outputting the insns) and call to the library function
2263 with the stabilized argument list. */
2264 end_sequence ();
2267 return expand_call (exp, target, target == const0_rtx);
2270 /* Given an interclass math builtin decl FNDECL and it's argument ARG
2271 return an RTL instruction code that implements the functionality.
2272 If that isn't possible or available return CODE_FOR_nothing. */
2274 static enum insn_code
2275 interclass_mathfn_icode (tree arg, tree fndecl)
2277 bool errno_set = false;
2278 optab builtin_optab = unknown_optab;
2279 machine_mode mode;
2281 switch (DECL_FUNCTION_CODE (fndecl))
2283 CASE_FLT_FN (BUILT_IN_ILOGB):
2284 errno_set = true; builtin_optab = ilogb_optab; break;
2285 CASE_FLT_FN (BUILT_IN_ISINF):
2286 builtin_optab = isinf_optab; break;
2287 case BUILT_IN_ISNORMAL:
2288 case BUILT_IN_ISFINITE:
2289 CASE_FLT_FN (BUILT_IN_FINITE):
2290 case BUILT_IN_FINITED32:
2291 case BUILT_IN_FINITED64:
2292 case BUILT_IN_FINITED128:
2293 case BUILT_IN_ISINFD32:
2294 case BUILT_IN_ISINFD64:
2295 case BUILT_IN_ISINFD128:
2296 /* These builtins have no optabs (yet). */
2297 break;
2298 default:
2299 gcc_unreachable ();
2302 /* There's no easy way to detect the case we need to set EDOM. */
2303 if (flag_errno_math && errno_set)
2304 return CODE_FOR_nothing;
2306 /* Optab mode depends on the mode of the input argument. */
2307 mode = TYPE_MODE (TREE_TYPE (arg));
2309 if (builtin_optab)
2310 return optab_handler (builtin_optab, mode);
2311 return CODE_FOR_nothing;
2314 /* Expand a call to one of the builtin math functions that operate on
2315 floating point argument and output an integer result (ilogb, isinf,
2316 isnan, etc).
2317 Return 0 if a normal call should be emitted rather than expanding the
2318 function in-line. EXP is the expression that is a call to the builtin
2319 function; if convenient, the result should be placed in TARGET. */
2321 static rtx
2322 expand_builtin_interclass_mathfn (tree exp, rtx target)
2324 enum insn_code icode = CODE_FOR_nothing;
2325 rtx op0;
2326 tree fndecl = get_callee_fndecl (exp);
2327 machine_mode mode;
2328 tree arg;
2330 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2331 return NULL_RTX;
2333 arg = CALL_EXPR_ARG (exp, 0);
2334 icode = interclass_mathfn_icode (arg, fndecl);
2335 mode = TYPE_MODE (TREE_TYPE (arg));
2337 if (icode != CODE_FOR_nothing)
2339 struct expand_operand ops[1];
2340 rtx_insn *last = get_last_insn ();
2341 tree orig_arg = arg;
2343 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2344 need to expand the argument again. This way, we will not perform
2345 side-effects more the once. */
2346 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2348 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2350 if (mode != GET_MODE (op0))
2351 op0 = convert_to_mode (mode, op0, 0);
2353 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
2354 if (maybe_legitimize_operands (icode, 0, 1, ops)
2355 && maybe_emit_unop_insn (icode, ops[0].value, op0, UNKNOWN))
2356 return ops[0].value;
2358 delete_insns_since (last);
2359 CALL_EXPR_ARG (exp, 0) = orig_arg;
2362 return NULL_RTX;
2365 /* Expand a call to the builtin sincos math function.
2366 Return NULL_RTX if a normal call should be emitted rather than expanding the
2367 function in-line. EXP is the expression that is a call to the builtin
2368 function. */
2370 static rtx
2371 expand_builtin_sincos (tree exp)
2373 rtx op0, op1, op2, target1, target2;
2374 machine_mode mode;
2375 tree arg, sinp, cosp;
2376 int result;
2377 location_t loc = EXPR_LOCATION (exp);
2378 tree alias_type, alias_off;
2380 if (!validate_arglist (exp, REAL_TYPE,
2381 POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
2382 return NULL_RTX;
2384 arg = CALL_EXPR_ARG (exp, 0);
2385 sinp = CALL_EXPR_ARG (exp, 1);
2386 cosp = CALL_EXPR_ARG (exp, 2);
2388 /* Make a suitable register to place result in. */
2389 mode = TYPE_MODE (TREE_TYPE (arg));
2391 /* Check if sincos insn is available, otherwise emit the call. */
2392 if (optab_handler (sincos_optab, mode) == CODE_FOR_nothing)
2393 return NULL_RTX;
2395 target1 = gen_reg_rtx (mode);
2396 target2 = gen_reg_rtx (mode);
2398 op0 = expand_normal (arg);
2399 alias_type = build_pointer_type_for_mode (TREE_TYPE (arg), ptr_mode, true);
2400 alias_off = build_int_cst (alias_type, 0);
2401 op1 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2402 sinp, alias_off));
2403 op2 = expand_normal (fold_build2_loc (loc, MEM_REF, TREE_TYPE (arg),
2404 cosp, alias_off));
2406 /* Compute into target1 and target2.
2407 Set TARGET to wherever the result comes back. */
2408 result = expand_twoval_unop (sincos_optab, op0, target2, target1, 0);
2409 gcc_assert (result);
2411 /* Move target1 and target2 to the memory locations indicated
2412 by op1 and op2. */
2413 emit_move_insn (op1, target1);
2414 emit_move_insn (op2, target2);
2416 return const0_rtx;
2419 /* Expand a call to the internal cexpi builtin to the sincos math function.
2420 EXP is the expression that is a call to the builtin function; if convenient,
2421 the result should be placed in TARGET. */
2423 static rtx
2424 expand_builtin_cexpi (tree exp, rtx target)
2426 tree fndecl = get_callee_fndecl (exp);
2427 tree arg, type;
2428 machine_mode mode;
2429 rtx op0, op1, op2;
2430 location_t loc = EXPR_LOCATION (exp);
2432 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2433 return NULL_RTX;
2435 arg = CALL_EXPR_ARG (exp, 0);
2436 type = TREE_TYPE (arg);
2437 mode = TYPE_MODE (TREE_TYPE (arg));
2439 /* Try expanding via a sincos optab, fall back to emitting a libcall
2440 to sincos or cexp. We are sure we have sincos or cexp because cexpi
2441 is only generated from sincos, cexp or if we have either of them. */
2442 if (optab_handler (sincos_optab, mode) != CODE_FOR_nothing)
2444 op1 = gen_reg_rtx (mode);
2445 op2 = gen_reg_rtx (mode);
2447 op0 = expand_expr (arg, NULL_RTX, VOIDmode, EXPAND_NORMAL);
2449 /* Compute into op1 and op2. */
2450 expand_twoval_unop (sincos_optab, op0, op2, op1, 0);
2452 else if (targetm.libc_has_function (function_sincos))
2454 tree call, fn = NULL_TREE;
2455 tree top1, top2;
2456 rtx op1a, op2a;
2458 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2459 fn = builtin_decl_explicit (BUILT_IN_SINCOSF);
2460 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2461 fn = builtin_decl_explicit (BUILT_IN_SINCOS);
2462 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2463 fn = builtin_decl_explicit (BUILT_IN_SINCOSL);
2464 else
2465 gcc_unreachable ();
2467 op1 = assign_temp (TREE_TYPE (arg), 1, 1);
2468 op2 = assign_temp (TREE_TYPE (arg), 1, 1);
2469 op1a = copy_addr_to_reg (XEXP (op1, 0));
2470 op2a = copy_addr_to_reg (XEXP (op2, 0));
2471 top1 = make_tree (build_pointer_type (TREE_TYPE (arg)), op1a);
2472 top2 = make_tree (build_pointer_type (TREE_TYPE (arg)), op2a);
2474 /* Make sure not to fold the sincos call again. */
2475 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2476 expand_normal (build_call_nary (TREE_TYPE (TREE_TYPE (fn)),
2477 call, 3, arg, top1, top2));
2479 else
2481 tree call, fn = NULL_TREE, narg;
2482 tree ctype = build_complex_type (type);
2484 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2485 fn = builtin_decl_explicit (BUILT_IN_CEXPF);
2486 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2487 fn = builtin_decl_explicit (BUILT_IN_CEXP);
2488 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2489 fn = builtin_decl_explicit (BUILT_IN_CEXPL);
2490 else
2491 gcc_unreachable ();
2493 /* If we don't have a decl for cexp create one. This is the
2494 friendliest fallback if the user calls __builtin_cexpi
2495 without full target C99 function support. */
2496 if (fn == NULL_TREE)
2498 tree fntype;
2499 const char *name = NULL;
2501 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIF)
2502 name = "cexpf";
2503 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPI)
2504 name = "cexp";
2505 else if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CEXPIL)
2506 name = "cexpl";
2508 fntype = build_function_type_list (ctype, ctype, NULL_TREE);
2509 fn = build_fn_decl (name, fntype);
2512 narg = fold_build2_loc (loc, COMPLEX_EXPR, ctype,
2513 build_real (type, dconst0), arg);
2515 /* Make sure not to fold the cexp call again. */
2516 call = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2517 return expand_expr (build_call_nary (ctype, call, 1, narg),
2518 target, VOIDmode, EXPAND_NORMAL);
2521 /* Now build the proper return type. */
2522 return expand_expr (build2 (COMPLEX_EXPR, build_complex_type (type),
2523 make_tree (TREE_TYPE (arg), op2),
2524 make_tree (TREE_TYPE (arg), op1)),
2525 target, VOIDmode, EXPAND_NORMAL);
2528 /* Conveniently construct a function call expression. FNDECL names the
2529 function to be called, N is the number of arguments, and the "..."
2530 parameters are the argument expressions. Unlike build_call_exr
2531 this doesn't fold the call, hence it will always return a CALL_EXPR. */
2533 static tree
2534 build_call_nofold_loc (location_t loc, tree fndecl, int n, ...)
2536 va_list ap;
2537 tree fntype = TREE_TYPE (fndecl);
2538 tree fn = build1 (ADDR_EXPR, build_pointer_type (fntype), fndecl);
2540 va_start (ap, n);
2541 fn = build_call_valist (TREE_TYPE (fntype), fn, n, ap);
2542 va_end (ap);
2543 SET_EXPR_LOCATION (fn, loc);
2544 return fn;
2547 /* Expand a call to one of the builtin rounding functions gcc defines
2548 as an extension (lfloor and lceil). As these are gcc extensions we
2549 do not need to worry about setting errno to EDOM.
2550 If expanding via optab fails, lower expression to (int)(floor(x)).
2551 EXP is the expression that is a call to the builtin function;
2552 if convenient, the result should be placed in TARGET. */
2554 static rtx
2555 expand_builtin_int_roundingfn (tree exp, rtx target)
2557 convert_optab builtin_optab;
2558 rtx op0, tmp;
2559 rtx_insn *insns;
2560 tree fndecl = get_callee_fndecl (exp);
2561 enum built_in_function fallback_fn;
2562 tree fallback_fndecl;
2563 machine_mode mode;
2564 tree arg;
2566 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2567 gcc_unreachable ();
2569 arg = CALL_EXPR_ARG (exp, 0);
2571 switch (DECL_FUNCTION_CODE (fndecl))
2573 CASE_FLT_FN (BUILT_IN_ICEIL):
2574 CASE_FLT_FN (BUILT_IN_LCEIL):
2575 CASE_FLT_FN (BUILT_IN_LLCEIL):
2576 builtin_optab = lceil_optab;
2577 fallback_fn = BUILT_IN_CEIL;
2578 break;
2580 CASE_FLT_FN (BUILT_IN_IFLOOR):
2581 CASE_FLT_FN (BUILT_IN_LFLOOR):
2582 CASE_FLT_FN (BUILT_IN_LLFLOOR):
2583 builtin_optab = lfloor_optab;
2584 fallback_fn = BUILT_IN_FLOOR;
2585 break;
2587 default:
2588 gcc_unreachable ();
2591 /* Make a suitable register to place result in. */
2592 mode = TYPE_MODE (TREE_TYPE (exp));
2594 target = gen_reg_rtx (mode);
2596 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2597 need to expand the argument again. This way, we will not perform
2598 side-effects more the once. */
2599 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2601 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2603 start_sequence ();
2605 /* Compute into TARGET. */
2606 if (expand_sfix_optab (target, op0, builtin_optab))
2608 /* Output the entire sequence. */
2609 insns = get_insns ();
2610 end_sequence ();
2611 emit_insn (insns);
2612 return target;
2615 /* If we were unable to expand via the builtin, stop the sequence
2616 (without outputting the insns). */
2617 end_sequence ();
2619 /* Fall back to floating point rounding optab. */
2620 fallback_fndecl = mathfn_built_in (TREE_TYPE (arg), fallback_fn);
2622 /* For non-C99 targets we may end up without a fallback fndecl here
2623 if the user called __builtin_lfloor directly. In this case emit
2624 a call to the floor/ceil variants nevertheless. This should result
2625 in the best user experience for not full C99 targets. */
2626 if (fallback_fndecl == NULL_TREE)
2628 tree fntype;
2629 const char *name = NULL;
2631 switch (DECL_FUNCTION_CODE (fndecl))
2633 case BUILT_IN_ICEIL:
2634 case BUILT_IN_LCEIL:
2635 case BUILT_IN_LLCEIL:
2636 name = "ceil";
2637 break;
2638 case BUILT_IN_ICEILF:
2639 case BUILT_IN_LCEILF:
2640 case BUILT_IN_LLCEILF:
2641 name = "ceilf";
2642 break;
2643 case BUILT_IN_ICEILL:
2644 case BUILT_IN_LCEILL:
2645 case BUILT_IN_LLCEILL:
2646 name = "ceill";
2647 break;
2648 case BUILT_IN_IFLOOR:
2649 case BUILT_IN_LFLOOR:
2650 case BUILT_IN_LLFLOOR:
2651 name = "floor";
2652 break;
2653 case BUILT_IN_IFLOORF:
2654 case BUILT_IN_LFLOORF:
2655 case BUILT_IN_LLFLOORF:
2656 name = "floorf";
2657 break;
2658 case BUILT_IN_IFLOORL:
2659 case BUILT_IN_LFLOORL:
2660 case BUILT_IN_LLFLOORL:
2661 name = "floorl";
2662 break;
2663 default:
2664 gcc_unreachable ();
2667 fntype = build_function_type_list (TREE_TYPE (arg),
2668 TREE_TYPE (arg), NULL_TREE);
2669 fallback_fndecl = build_fn_decl (name, fntype);
2672 exp = build_call_nofold_loc (EXPR_LOCATION (exp), fallback_fndecl, 1, arg);
2674 tmp = expand_normal (exp);
2675 tmp = maybe_emit_group_store (tmp, TREE_TYPE (exp));
2677 /* Truncate the result of floating point optab to integer
2678 via expand_fix (). */
2679 target = gen_reg_rtx (mode);
2680 expand_fix (target, tmp, 0);
2682 return target;
2685 /* Expand a call to one of the builtin math functions doing integer
2686 conversion (lrint).
2687 Return 0 if a normal call should be emitted rather than expanding the
2688 function in-line. EXP is the expression that is a call to the builtin
2689 function; if convenient, the result should be placed in TARGET. */
2691 static rtx
2692 expand_builtin_int_roundingfn_2 (tree exp, rtx target)
2694 convert_optab builtin_optab;
2695 rtx op0;
2696 rtx_insn *insns;
2697 tree fndecl = get_callee_fndecl (exp);
2698 tree arg;
2699 machine_mode mode;
2700 enum built_in_function fallback_fn = BUILT_IN_NONE;
2702 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
2703 gcc_unreachable ();
2705 arg = CALL_EXPR_ARG (exp, 0);
2707 switch (DECL_FUNCTION_CODE (fndecl))
2709 CASE_FLT_FN (BUILT_IN_IRINT):
2710 fallback_fn = BUILT_IN_LRINT;
2711 gcc_fallthrough ();
2712 CASE_FLT_FN (BUILT_IN_LRINT):
2713 CASE_FLT_FN (BUILT_IN_LLRINT):
2714 builtin_optab = lrint_optab;
2715 break;
2717 CASE_FLT_FN (BUILT_IN_IROUND):
2718 fallback_fn = BUILT_IN_LROUND;
2719 gcc_fallthrough ();
2720 CASE_FLT_FN (BUILT_IN_LROUND):
2721 CASE_FLT_FN (BUILT_IN_LLROUND):
2722 builtin_optab = lround_optab;
2723 break;
2725 default:
2726 gcc_unreachable ();
2729 /* There's no easy way to detect the case we need to set EDOM. */
2730 if (flag_errno_math && fallback_fn == BUILT_IN_NONE)
2731 return NULL_RTX;
2733 /* Make a suitable register to place result in. */
2734 mode = TYPE_MODE (TREE_TYPE (exp));
2736 /* There's no easy way to detect the case we need to set EDOM. */
2737 if (!flag_errno_math)
2739 rtx result = gen_reg_rtx (mode);
2741 /* Wrap the computation of the argument in a SAVE_EXPR, as we may
2742 need to expand the argument again. This way, we will not perform
2743 side-effects more the once. */
2744 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
2746 op0 = expand_expr (arg, NULL, VOIDmode, EXPAND_NORMAL);
2748 start_sequence ();
2750 if (expand_sfix_optab (result, op0, builtin_optab))
2752 /* Output the entire sequence. */
2753 insns = get_insns ();
2754 end_sequence ();
2755 emit_insn (insns);
2756 return result;
2759 /* If we were unable to expand via the builtin, stop the sequence
2760 (without outputting the insns) and call to the library function
2761 with the stabilized argument list. */
2762 end_sequence ();
2765 if (fallback_fn != BUILT_IN_NONE)
2767 /* Fall back to rounding to long int. Use implicit_p 0 - for non-C99
2768 targets, (int) round (x) should never be transformed into
2769 BUILT_IN_IROUND and if __builtin_iround is called directly, emit
2770 a call to lround in the hope that the target provides at least some
2771 C99 functions. This should result in the best user experience for
2772 not full C99 targets. */
2773 tree fallback_fndecl = mathfn_built_in_1
2774 (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0);
2776 exp = build_call_nofold_loc (EXPR_LOCATION (exp),
2777 fallback_fndecl, 1, arg);
2779 target = expand_call (exp, NULL_RTX, target == const0_rtx);
2780 target = maybe_emit_group_store (target, TREE_TYPE (exp));
2781 return convert_to_mode (mode, target, 0);
2784 return expand_call (exp, target, target == const0_rtx);
2787 /* Expand a call to the powi built-in mathematical function. Return NULL_RTX if
2788 a normal call should be emitted rather than expanding the function
2789 in-line. EXP is the expression that is a call to the builtin
2790 function; if convenient, the result should be placed in TARGET. */
2792 static rtx
2793 expand_builtin_powi (tree exp, rtx target)
2795 tree arg0, arg1;
2796 rtx op0, op1;
2797 machine_mode mode;
2798 machine_mode mode2;
2800 if (! validate_arglist (exp, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
2801 return NULL_RTX;
2803 arg0 = CALL_EXPR_ARG (exp, 0);
2804 arg1 = CALL_EXPR_ARG (exp, 1);
2805 mode = TYPE_MODE (TREE_TYPE (exp));
2807 /* Emit a libcall to libgcc. */
2809 /* Mode of the 2nd argument must match that of an int. */
2810 mode2 = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
2812 if (target == NULL_RTX)
2813 target = gen_reg_rtx (mode);
2815 op0 = expand_expr (arg0, NULL_RTX, mode, EXPAND_NORMAL);
2816 if (GET_MODE (op0) != mode)
2817 op0 = convert_to_mode (mode, op0, 0);
2818 op1 = expand_expr (arg1, NULL_RTX, mode2, EXPAND_NORMAL);
2819 if (GET_MODE (op1) != mode2)
2820 op1 = convert_to_mode (mode2, op1, 0);
2822 target = emit_library_call_value (optab_libfunc (powi_optab, mode),
2823 target, LCT_CONST, mode,
2824 op0, mode, op1, mode2);
2826 return target;
2829 /* Expand expression EXP which is a call to the strlen builtin. Return
2830 NULL_RTX if we failed and the caller should emit a normal call, otherwise
2831 try to get the result in TARGET, if convenient. */
2833 static rtx
2834 expand_builtin_strlen (tree exp, rtx target,
2835 machine_mode target_mode)
2837 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
2838 return NULL_RTX;
2840 struct expand_operand ops[4];
2841 rtx pat;
2842 tree len;
2843 tree src = CALL_EXPR_ARG (exp, 0);
2844 rtx src_reg;
2845 rtx_insn *before_strlen;
2846 machine_mode insn_mode;
2847 enum insn_code icode = CODE_FOR_nothing;
2848 unsigned int align;
2850 /* If the length can be computed at compile-time, return it. */
2851 len = c_strlen (src, 0);
2852 if (len)
2853 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2855 /* If the length can be computed at compile-time and is constant
2856 integer, but there are side-effects in src, evaluate
2857 src for side-effects, then return len.
2858 E.g. x = strlen (i++ ? "xfoo" + 1 : "bar");
2859 can be optimized into: i++; x = 3; */
2860 len = c_strlen (src, 1);
2861 if (len && TREE_CODE (len) == INTEGER_CST)
2863 expand_expr (src, const0_rtx, VOIDmode, EXPAND_NORMAL);
2864 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2867 align = get_pointer_alignment (src) / BITS_PER_UNIT;
2869 /* If SRC is not a pointer type, don't do this operation inline. */
2870 if (align == 0)
2871 return NULL_RTX;
2873 /* Bail out if we can't compute strlen in the right mode. */
2874 FOR_EACH_MODE_FROM (insn_mode, target_mode)
2876 icode = optab_handler (strlen_optab, insn_mode);
2877 if (icode != CODE_FOR_nothing)
2878 break;
2880 if (insn_mode == VOIDmode)
2881 return NULL_RTX;
2883 /* Make a place to hold the source address. We will not expand
2884 the actual source until we are sure that the expansion will
2885 not fail -- there are trees that cannot be expanded twice. */
2886 src_reg = gen_reg_rtx (Pmode);
2888 /* Mark the beginning of the strlen sequence so we can emit the
2889 source operand later. */
2890 before_strlen = get_last_insn ();
2892 create_output_operand (&ops[0], target, insn_mode);
2893 create_fixed_operand (&ops[1], gen_rtx_MEM (BLKmode, src_reg));
2894 create_integer_operand (&ops[2], 0);
2895 create_integer_operand (&ops[3], align);
2896 if (!maybe_expand_insn (icode, 4, ops))
2897 return NULL_RTX;
2899 /* Check to see if the argument was declared attribute nonstring
2900 and if so, issue a warning since at this point it's not known
2901 to be nul-terminated. */
2902 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
2904 /* Now that we are assured of success, expand the source. */
2905 start_sequence ();
2906 pat = expand_expr (src, src_reg, Pmode, EXPAND_NORMAL);
2907 if (pat != src_reg)
2909 #ifdef POINTERS_EXTEND_UNSIGNED
2910 if (GET_MODE (pat) != Pmode)
2911 pat = convert_to_mode (Pmode, pat,
2912 POINTERS_EXTEND_UNSIGNED);
2913 #endif
2914 emit_move_insn (src_reg, pat);
2916 pat = get_insns ();
2917 end_sequence ();
2919 if (before_strlen)
2920 emit_insn_after (pat, before_strlen);
2921 else
2922 emit_insn_before (pat, get_insns ());
2924 /* Return the value in the proper mode for this function. */
2925 if (GET_MODE (ops[0].value) == target_mode)
2926 target = ops[0].value;
2927 else if (target != 0)
2928 convert_move (target, ops[0].value, 0);
2929 else
2930 target = convert_to_mode (target_mode, ops[0].value, 0);
2932 return target;
2935 /* Expand call EXP to the strnlen built-in, returning the result
2936 and setting it in TARGET. Otherwise return NULL_RTX on failure. */
2938 static rtx
2939 expand_builtin_strnlen (tree exp, rtx target, machine_mode target_mode)
2941 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
2942 return NULL_RTX;
2944 tree src = CALL_EXPR_ARG (exp, 0);
2945 tree bound = CALL_EXPR_ARG (exp, 1);
2947 if (!bound)
2948 return NULL_RTX;
2950 location_t loc = UNKNOWN_LOCATION;
2951 if (EXPR_HAS_LOCATION (exp))
2952 loc = EXPR_LOCATION (exp);
2954 tree maxobjsize = max_object_size ();
2955 tree func = get_callee_fndecl (exp);
2957 tree len = c_strlen (src, 0);
2959 if (TREE_CODE (bound) == INTEGER_CST)
2961 if (!TREE_NO_WARNING (exp)
2962 && tree_int_cst_lt (maxobjsize, bound)
2963 && warning_at (loc, OPT_Wstringop_overflow_,
2964 "%K%qD specified bound %E "
2965 "exceeds maximum object size %E",
2966 exp, func, bound, maxobjsize))
2967 TREE_NO_WARNING (exp) = true;
2969 if (!len || TREE_CODE (len) != INTEGER_CST)
2970 return NULL_RTX;
2972 len = fold_convert_loc (loc, size_type_node, len);
2973 len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
2974 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2977 if (TREE_CODE (bound) != SSA_NAME)
2978 return NULL_RTX;
2980 wide_int min, max;
2981 enum value_range_type rng = get_range_info (bound, &min, &max);
2982 if (rng != VR_RANGE)
2983 return NULL_RTX;
2985 if (!TREE_NO_WARNING (exp)
2986 && wi::ltu_p (wi::to_wide (maxobjsize), min)
2987 && warning_at (loc, OPT_Wstringop_overflow_,
2988 "%K%qD specified bound [%wu, %wu] "
2989 "exceeds maximum object size %E",
2990 exp, func, min.to_uhwi (), max.to_uhwi (), maxobjsize))
2991 TREE_NO_WARNING (exp) = true;
2993 if (!len || TREE_CODE (len) != INTEGER_CST)
2994 return NULL_RTX;
2996 if (wi::gtu_p (min, wi::to_wide (len)))
2997 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
2999 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, bound);
3000 return expand_expr (len, target, target_mode, EXPAND_NORMAL);
3003 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3004 bytes from constant string DATA + OFFSET and return it as target
3005 constant. */
3007 static rtx
3008 builtin_memcpy_read_str (void *data, HOST_WIDE_INT offset,
3009 scalar_int_mode mode)
3011 const char *str = (const char *) data;
3013 gcc_assert (offset >= 0
3014 && ((unsigned HOST_WIDE_INT) offset + GET_MODE_SIZE (mode)
3015 <= strlen (str) + 1));
3017 return c_readstr (str + offset, mode);
3020 /* LEN specify length of the block of memcpy/memset operation.
3021 Figure out its range and put it into MIN_SIZE/MAX_SIZE.
3022 In some cases we can make very likely guess on max size, then we
3023 set it into PROBABLE_MAX_SIZE. */
3025 static void
3026 determine_block_size (tree len, rtx len_rtx,
3027 unsigned HOST_WIDE_INT *min_size,
3028 unsigned HOST_WIDE_INT *max_size,
3029 unsigned HOST_WIDE_INT *probable_max_size)
3031 if (CONST_INT_P (len_rtx))
3033 *min_size = *max_size = *probable_max_size = UINTVAL (len_rtx);
3034 return;
3036 else
3038 wide_int min, max;
3039 enum value_range_type range_type = VR_UNDEFINED;
3041 /* Determine bounds from the type. */
3042 if (tree_fits_uhwi_p (TYPE_MIN_VALUE (TREE_TYPE (len))))
3043 *min_size = tree_to_uhwi (TYPE_MIN_VALUE (TREE_TYPE (len)));
3044 else
3045 *min_size = 0;
3046 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (TREE_TYPE (len))))
3047 *probable_max_size = *max_size
3048 = tree_to_uhwi (TYPE_MAX_VALUE (TREE_TYPE (len)));
3049 else
3050 *probable_max_size = *max_size = GET_MODE_MASK (GET_MODE (len_rtx));
3052 if (TREE_CODE (len) == SSA_NAME)
3053 range_type = get_range_info (len, &min, &max);
3054 if (range_type == VR_RANGE)
3056 if (wi::fits_uhwi_p (min) && *min_size < min.to_uhwi ())
3057 *min_size = min.to_uhwi ();
3058 if (wi::fits_uhwi_p (max) && *max_size > max.to_uhwi ())
3059 *probable_max_size = *max_size = max.to_uhwi ();
3061 else if (range_type == VR_ANTI_RANGE)
3063 /* Anti range 0...N lets us to determine minimal size to N+1. */
3064 if (min == 0)
3066 if (wi::fits_uhwi_p (max) && max.to_uhwi () + 1 != 0)
3067 *min_size = max.to_uhwi () + 1;
3069 /* Code like
3071 int n;
3072 if (n < 100)
3073 memcpy (a, b, n)
3075 Produce anti range allowing negative values of N. We still
3076 can use the information and make a guess that N is not negative.
3078 else if (!wi::leu_p (max, 1 << 30) && wi::fits_uhwi_p (min))
3079 *probable_max_size = min.to_uhwi () - 1;
3082 gcc_checking_assert (*max_size <=
3083 (unsigned HOST_WIDE_INT)
3084 GET_MODE_MASK (GET_MODE (len_rtx)));
3087 /* Try to verify that the sizes and lengths of the arguments to a string
3088 manipulation function given by EXP are within valid bounds and that
3089 the operation does not lead to buffer overflow or read past the end.
3090 Arguments other than EXP may be null. When non-null, the arguments
3091 have the following meaning:
3092 DST is the destination of a copy call or NULL otherwise.
3093 SRC is the source of a copy call or NULL otherwise.
3094 DSTWRITE is the number of bytes written into the destination obtained
3095 from the user-supplied size argument to the function (such as in
3096 memcpy(DST, SRCs, DSTWRITE) or strncpy(DST, DRC, DSTWRITE).
3097 MAXREAD is the user-supplied bound on the length of the source sequence
3098 (such as in strncat(d, s, N). It specifies the upper limit on the number
3099 of bytes to write. If NULL, it's taken to be the same as DSTWRITE.
3100 SRCSTR is the source string (such as in strcpy(DST, SRC)) when the
3101 expression EXP is a string function call (as opposed to a memory call
3102 like memcpy). As an exception, SRCSTR can also be an integer denoting
3103 the precomputed size of the source string or object (for functions like
3104 memcpy).
3105 DSTSIZE is the size of the destination object specified by the last
3106 argument to the _chk builtins, typically resulting from the expansion
3107 of __builtin_object_size (such as in __builtin___strcpy_chk(DST, SRC,
3108 DSTSIZE).
3110 When DSTWRITE is null LEN is checked to verify that it doesn't exceed
3111 SIZE_MAX.
3113 If the call is successfully verified as safe return true, otherwise
3114 return false. */
3116 static bool
3117 check_access (tree exp, tree, tree, tree dstwrite,
3118 tree maxread, tree srcstr, tree dstsize)
3120 int opt = OPT_Wstringop_overflow_;
3122 /* The size of the largest object is half the address space, or
3123 PTRDIFF_MAX. (This is way too permissive.) */
3124 tree maxobjsize = max_object_size ();
3126 /* Either the length of the source string for string functions or
3127 the size of the source object for raw memory functions. */
3128 tree slen = NULL_TREE;
3130 tree range[2] = { NULL_TREE, NULL_TREE };
3132 /* Set to true when the exact number of bytes written by a string
3133 function like strcpy is not known and the only thing that is
3134 known is that it must be at least one (for the terminating nul). */
3135 bool at_least_one = false;
3136 if (srcstr)
3138 /* SRCSTR is normally a pointer to string but as a special case
3139 it can be an integer denoting the length of a string. */
3140 if (POINTER_TYPE_P (TREE_TYPE (srcstr)))
3142 /* Try to determine the range of lengths the source string
3143 refers to. If it can be determined and is less than
3144 the upper bound given by MAXREAD add one to it for
3145 the terminating nul. Otherwise, set it to one for
3146 the same reason, or to MAXREAD as appropriate. */
3147 get_range_strlen (srcstr, range);
3148 if (range[0] && (!maxread || TREE_CODE (maxread) == INTEGER_CST))
3150 if (maxread && tree_int_cst_le (maxread, range[0]))
3151 range[0] = range[1] = maxread;
3152 else
3153 range[0] = fold_build2 (PLUS_EXPR, size_type_node,
3154 range[0], size_one_node);
3156 if (maxread && tree_int_cst_le (maxread, range[1]))
3157 range[1] = maxread;
3158 else if (!integer_all_onesp (range[1]))
3159 range[1] = fold_build2 (PLUS_EXPR, size_type_node,
3160 range[1], size_one_node);
3162 slen = range[0];
3164 else
3166 at_least_one = true;
3167 slen = size_one_node;
3170 else
3171 slen = srcstr;
3174 if (!dstwrite && !maxread)
3176 /* When the only available piece of data is the object size
3177 there is nothing to do. */
3178 if (!slen)
3179 return true;
3181 /* Otherwise, when the length of the source sequence is known
3182 (as with strlen), set DSTWRITE to it. */
3183 if (!range[0])
3184 dstwrite = slen;
3187 if (!dstsize)
3188 dstsize = maxobjsize;
3190 if (dstwrite)
3191 get_size_range (dstwrite, range);
3193 tree func = get_callee_fndecl (exp);
3195 /* First check the number of bytes to be written against the maximum
3196 object size. */
3197 if (range[0] && tree_int_cst_lt (maxobjsize, range[0]))
3199 if (TREE_NO_WARNING (exp))
3200 return false;
3202 location_t loc = tree_nonartificial_location (exp);
3203 loc = expansion_point_location_if_in_system_header (loc);
3205 bool warned;
3206 if (range[0] == range[1])
3207 warned = warning_at (loc, opt,
3208 "%K%qD specified size %E "
3209 "exceeds maximum object size %E",
3210 exp, func, range[0], maxobjsize);
3211 else
3212 warned = warning_at (loc, opt,
3213 "%K%qD specified size between %E and %E "
3214 "exceeds maximum object size %E",
3215 exp, func,
3216 range[0], range[1], maxobjsize);
3217 if (warned)
3218 TREE_NO_WARNING (exp) = true;
3220 return false;
3223 /* The number of bytes to write is "exact" if DSTWRITE is non-null,
3224 constant, and in range of unsigned HOST_WIDE_INT. */
3225 bool exactwrite = dstwrite && tree_fits_uhwi_p (dstwrite);
3227 /* Next check the number of bytes to be written against the destination
3228 object size. */
3229 if (range[0] || !exactwrite || integer_all_onesp (dstwrite))
3231 if (range[0]
3232 && ((tree_fits_uhwi_p (dstsize)
3233 && tree_int_cst_lt (dstsize, range[0]))
3234 || (tree_fits_uhwi_p (dstwrite)
3235 && tree_int_cst_lt (dstwrite, range[0]))))
3237 if (TREE_NO_WARNING (exp))
3238 return false;
3240 location_t loc = tree_nonartificial_location (exp);
3241 loc = expansion_point_location_if_in_system_header (loc);
3243 if (dstwrite == slen && at_least_one)
3245 /* This is a call to strcpy with a destination of 0 size
3246 and a source of unknown length. The call will write
3247 at least one byte past the end of the destination. */
3248 warning_at (loc, opt,
3249 "%K%qD writing %E or more bytes into a region "
3250 "of size %E overflows the destination",
3251 exp, func, range[0], dstsize);
3253 else if (tree_int_cst_equal (range[0], range[1]))
3254 warning_n (loc, opt, tree_to_uhwi (range[0]),
3255 "%K%qD writing %E byte into a region "
3256 "of size %E overflows the destination",
3257 "%K%qD writing %E bytes into a region "
3258 "of size %E overflows the destination",
3259 exp, func, range[0], dstsize);
3260 else if (tree_int_cst_sign_bit (range[1]))
3262 /* Avoid printing the upper bound if it's invalid. */
3263 warning_at (loc, opt,
3264 "%K%qD writing %E or more bytes into a region "
3265 "of size %E overflows the destination",
3266 exp, func, range[0], dstsize);
3268 else
3269 warning_at (loc, opt,
3270 "%K%qD writing between %E and %E bytes into "
3271 "a region of size %E overflows the destination",
3272 exp, func, range[0], range[1],
3273 dstsize);
3275 /* Return error when an overflow has been detected. */
3276 return false;
3280 /* Check the maximum length of the source sequence against the size
3281 of the destination object if known, or against the maximum size
3282 of an object. */
3283 if (maxread)
3285 get_size_range (maxread, range);
3287 /* Use the lower end for MAXREAD from now on. */
3288 if (range[0])
3289 maxread = range[0];
3291 if (range[0] && dstsize && tree_fits_uhwi_p (dstsize))
3293 location_t loc = tree_nonartificial_location (exp);
3294 loc = expansion_point_location_if_in_system_header (loc);
3296 if (tree_int_cst_lt (maxobjsize, range[0]))
3298 if (TREE_NO_WARNING (exp))
3299 return false;
3301 /* Warn about crazy big sizes first since that's more
3302 likely to be meaningful than saying that the bound
3303 is greater than the object size if both are big. */
3304 if (range[0] == range[1])
3305 warning_at (loc, opt,
3306 "%K%qD specified bound %E "
3307 "exceeds maximum object size %E",
3308 exp, func,
3309 range[0], maxobjsize);
3310 else
3311 warning_at (loc, opt,
3312 "%K%qD specified bound between %E and %E "
3313 "exceeds maximum object size %E",
3314 exp, func,
3315 range[0], range[1], maxobjsize);
3317 return false;
3320 if (dstsize != maxobjsize && tree_int_cst_lt (dstsize, range[0]))
3322 if (TREE_NO_WARNING (exp))
3323 return false;
3325 if (tree_int_cst_equal (range[0], range[1]))
3326 warning_at (loc, opt,
3327 "%K%qD specified bound %E "
3328 "exceeds destination size %E",
3329 exp, func,
3330 range[0], dstsize);
3331 else
3332 warning_at (loc, opt,
3333 "%K%qD specified bound between %E and %E "
3334 "exceeds destination size %E",
3335 exp, func,
3336 range[0], range[1], dstsize);
3337 return false;
3342 /* Check for reading past the end of SRC. */
3343 if (slen
3344 && slen == srcstr
3345 && dstwrite && range[0]
3346 && tree_int_cst_lt (slen, range[0]))
3348 if (TREE_NO_WARNING (exp))
3349 return false;
3351 location_t loc = tree_nonartificial_location (exp);
3353 if (tree_int_cst_equal (range[0], range[1]))
3354 warning_n (loc, opt, tree_to_uhwi (range[0]),
3355 "%K%qD reading %E byte from a region of size %E",
3356 "%K%qD reading %E bytes from a region of size %E",
3357 exp, func, range[0], slen);
3358 else if (tree_int_cst_sign_bit (range[1]))
3360 /* Avoid printing the upper bound if it's invalid. */
3361 warning_at (loc, opt,
3362 "%K%qD reading %E or more bytes from a region "
3363 "of size %E",
3364 exp, func, range[0], slen);
3366 else
3367 warning_at (loc, opt,
3368 "%K%qD reading between %E and %E bytes from a region "
3369 "of size %E",
3370 exp, func, range[0], range[1], slen);
3371 return false;
3374 return true;
3377 /* Helper to compute the size of the object referenced by the DEST
3378 expression which must have pointer type, using Object Size type
3379 OSTYPE (only the least significant 2 bits are used). Return
3380 an estimate of the size of the object if successful or NULL when
3381 the size cannot be determined. When the referenced object involves
3382 a non-constant offset in some range the returned value represents
3383 the largest size given the smallest non-negative offset in the
3384 range. The function is intended for diagnostics and should not
3385 be used to influence code generation or optimization. */
3387 tree
3388 compute_objsize (tree dest, int ostype)
3390 unsigned HOST_WIDE_INT size;
3392 /* Only the two least significant bits are meaningful. */
3393 ostype &= 3;
3395 if (compute_builtin_object_size (dest, ostype, &size))
3396 return build_int_cst (sizetype, size);
3398 if (TREE_CODE (dest) == SSA_NAME)
3400 gimple *stmt = SSA_NAME_DEF_STMT (dest);
3401 if (!is_gimple_assign (stmt))
3402 return NULL_TREE;
3404 dest = gimple_assign_rhs1 (stmt);
3406 tree_code code = gimple_assign_rhs_code (stmt);
3407 if (code == POINTER_PLUS_EXPR)
3409 /* compute_builtin_object_size fails for addresses with
3410 non-constant offsets. Try to determine the range of
3411 such an offset here and use it to adjust the constant
3412 size. */
3413 tree off = gimple_assign_rhs2 (stmt);
3414 if (TREE_CODE (off) == INTEGER_CST)
3416 if (tree size = compute_objsize (dest, ostype))
3418 wide_int wioff = wi::to_wide (off);
3419 wide_int wisiz = wi::to_wide (size);
3421 /* Ignore negative offsets for now. For others,
3422 use the lower bound as the most optimistic
3423 estimate of the (remaining) size. */
3424 if (wi::sign_mask (wioff))
3426 else if (wi::ltu_p (wioff, wisiz))
3427 return wide_int_to_tree (TREE_TYPE (size),
3428 wi::sub (wisiz, wioff));
3429 else
3430 return size_zero_node;
3433 else if (TREE_CODE (off) == SSA_NAME
3434 && INTEGRAL_TYPE_P (TREE_TYPE (off)))
3436 wide_int min, max;
3437 enum value_range_type rng = get_range_info (off, &min, &max);
3439 if (rng == VR_RANGE)
3441 if (tree size = compute_objsize (dest, ostype))
3443 wide_int wisiz = wi::to_wide (size);
3445 /* Ignore negative offsets for now. For others,
3446 use the lower bound as the most optimistic
3447 estimate of the (remaining)size. */
3448 if (wi::sign_mask (min))
3450 else if (wi::ltu_p (min, wisiz))
3451 return wide_int_to_tree (TREE_TYPE (size),
3452 wi::sub (wisiz, min));
3453 else
3454 return size_zero_node;
3459 else if (code != ADDR_EXPR)
3460 return NULL_TREE;
3463 /* Unless computing the largest size (for memcpy and other raw memory
3464 functions), try to determine the size of the object from its type. */
3465 if (!ostype)
3466 return NULL_TREE;
3468 if (TREE_CODE (dest) != ADDR_EXPR)
3469 return NULL_TREE;
3471 tree type = TREE_TYPE (dest);
3472 if (TREE_CODE (type) == POINTER_TYPE)
3473 type = TREE_TYPE (type);
3475 type = TYPE_MAIN_VARIANT (type);
3477 if (TREE_CODE (type) == ARRAY_TYPE
3478 && !array_at_struct_end_p (TREE_OPERAND (dest, 0)))
3480 /* Return the constant size unless it's zero (that's a zero-length
3481 array likely at the end of a struct). */
3482 tree size = TYPE_SIZE_UNIT (type);
3483 if (size && TREE_CODE (size) == INTEGER_CST
3484 && !integer_zerop (size))
3485 return size;
3488 return NULL_TREE;
3491 /* Helper to determine and check the sizes of the source and the destination
3492 of calls to __builtin_{bzero,memcpy,mempcpy,memset} calls. EXP is the
3493 call expression, DEST is the destination argument, SRC is the source
3494 argument or null, and LEN is the number of bytes. Use Object Size type-0
3495 regardless of the OPT_Wstringop_overflow_ setting. Return true on success
3496 (no overflow or invalid sizes), false otherwise. */
3498 static bool
3499 check_memop_access (tree exp, tree dest, tree src, tree size)
3501 /* For functions like memset and memcpy that operate on raw memory
3502 try to determine the size of the largest source and destination
3503 object using type-0 Object Size regardless of the object size
3504 type specified by the option. */
3505 tree srcsize = src ? compute_objsize (src, 0) : NULL_TREE;
3506 tree dstsize = compute_objsize (dest, 0);
3508 return check_access (exp, dest, src, size, /*maxread=*/NULL_TREE,
3509 srcsize, dstsize);
3512 /* Validate memchr arguments without performing any expansion.
3513 Return NULL_RTX. */
3515 static rtx
3516 expand_builtin_memchr (tree exp, rtx)
3518 if (!validate_arglist (exp,
3519 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
3520 return NULL_RTX;
3522 tree arg1 = CALL_EXPR_ARG (exp, 0);
3523 tree len = CALL_EXPR_ARG (exp, 2);
3525 /* Diagnose calls where the specified length exceeds the size
3526 of the object. */
3527 if (warn_stringop_overflow)
3529 tree size = compute_objsize (arg1, 0);
3530 check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, len,
3531 /*maxread=*/NULL_TREE, size, /*objsize=*/NULL_TREE);
3534 return NULL_RTX;
3537 /* Expand a call EXP to the memcpy builtin.
3538 Return NULL_RTX if we failed, the caller should emit a normal call,
3539 otherwise try to get the result in TARGET, if convenient (and in
3540 mode MODE if that's convenient). */
3542 static rtx
3543 expand_builtin_memcpy (tree exp, rtx target)
3545 if (!validate_arglist (exp,
3546 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3547 return NULL_RTX;
3549 tree dest = CALL_EXPR_ARG (exp, 0);
3550 tree src = CALL_EXPR_ARG (exp, 1);
3551 tree len = CALL_EXPR_ARG (exp, 2);
3553 check_memop_access (exp, dest, src, len);
3555 return expand_builtin_memory_copy_args (dest, src, len, target, exp,
3556 /*endp=*/ 0);
3559 /* Check a call EXP to the memmove built-in for validity.
3560 Return NULL_RTX on both success and failure. */
3562 static rtx
3563 expand_builtin_memmove (tree exp, rtx)
3565 if (!validate_arglist (exp,
3566 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3567 return NULL_RTX;
3569 tree dest = CALL_EXPR_ARG (exp, 0);
3570 tree src = CALL_EXPR_ARG (exp, 1);
3571 tree len = CALL_EXPR_ARG (exp, 2);
3573 check_memop_access (exp, dest, src, len);
3575 return NULL_RTX;
3578 /* Expand a call EXP to the mempcpy builtin.
3579 Return NULL_RTX if we failed; the caller should emit a normal call,
3580 otherwise try to get the result in TARGET, if convenient (and in
3581 mode MODE if that's convenient). If ENDP is 0 return the
3582 destination pointer, if ENDP is 1 return the end pointer ala
3583 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3584 stpcpy. */
3586 static rtx
3587 expand_builtin_mempcpy (tree exp, rtx target)
3589 if (!validate_arglist (exp,
3590 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
3591 return NULL_RTX;
3593 tree dest = CALL_EXPR_ARG (exp, 0);
3594 tree src = CALL_EXPR_ARG (exp, 1);
3595 tree len = CALL_EXPR_ARG (exp, 2);
3597 /* Policy does not generally allow using compute_objsize (which
3598 is used internally by check_memop_size) to change code generation
3599 or drive optimization decisions.
3601 In this instance it is safe because the code we generate has
3602 the same semantics regardless of the return value of
3603 check_memop_sizes. Exactly the same amount of data is copied
3604 and the return value is exactly the same in both cases.
3606 Furthermore, check_memop_size always uses mode 0 for the call to
3607 compute_objsize, so the imprecise nature of compute_objsize is
3608 avoided. */
3610 /* Avoid expanding mempcpy into memcpy when the call is determined
3611 to overflow the buffer. This also prevents the same overflow
3612 from being diagnosed again when expanding memcpy. */
3613 if (!check_memop_access (exp, dest, src, len))
3614 return NULL_RTX;
3616 return expand_builtin_mempcpy_args (dest, src, len,
3617 target, exp, /*endp=*/ 1);
3620 /* Helper function to do the actual work for expand of memory copy family
3621 functions (memcpy, mempcpy, stpcpy). Expansing should assign LEN bytes
3622 of memory from SRC to DEST and assign to TARGET if convenient.
3623 If ENDP is 0 return the
3624 destination pointer, if ENDP is 1 return the end pointer ala
3625 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3626 stpcpy. */
3628 static rtx
3629 expand_builtin_memory_copy_args (tree dest, tree src, tree len,
3630 rtx target, tree exp, int endp)
3632 const char *src_str;
3633 unsigned int src_align = get_pointer_alignment (src);
3634 unsigned int dest_align = get_pointer_alignment (dest);
3635 rtx dest_mem, src_mem, dest_addr, len_rtx;
3636 HOST_WIDE_INT expected_size = -1;
3637 unsigned int expected_align = 0;
3638 unsigned HOST_WIDE_INT min_size;
3639 unsigned HOST_WIDE_INT max_size;
3640 unsigned HOST_WIDE_INT probable_max_size;
3642 /* If DEST is not a pointer type, call the normal function. */
3643 if (dest_align == 0)
3644 return NULL_RTX;
3646 /* If either SRC is not a pointer type, don't do this
3647 operation in-line. */
3648 if (src_align == 0)
3649 return NULL_RTX;
3651 if (currently_expanding_gimple_stmt)
3652 stringop_block_profile (currently_expanding_gimple_stmt,
3653 &expected_align, &expected_size);
3655 if (expected_align < dest_align)
3656 expected_align = dest_align;
3657 dest_mem = get_memory_rtx (dest, len);
3658 set_mem_align (dest_mem, dest_align);
3659 len_rtx = expand_normal (len);
3660 determine_block_size (len, len_rtx, &min_size, &max_size,
3661 &probable_max_size);
3662 src_str = c_getstr (src);
3664 /* If SRC is a string constant and block move would be done
3665 by pieces, we can avoid loading the string from memory
3666 and only stored the computed constants. */
3667 if (src_str
3668 && CONST_INT_P (len_rtx)
3669 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1
3670 && can_store_by_pieces (INTVAL (len_rtx), builtin_memcpy_read_str,
3671 CONST_CAST (char *, src_str),
3672 dest_align, false))
3674 dest_mem = store_by_pieces (dest_mem, INTVAL (len_rtx),
3675 builtin_memcpy_read_str,
3676 CONST_CAST (char *, src_str),
3677 dest_align, false, endp);
3678 dest_mem = force_operand (XEXP (dest_mem, 0), target);
3679 dest_mem = convert_memory_address (ptr_mode, dest_mem);
3680 return dest_mem;
3683 src_mem = get_memory_rtx (src, len);
3684 set_mem_align (src_mem, src_align);
3686 /* Copy word part most expediently. */
3687 enum block_op_methods method = BLOCK_OP_NORMAL;
3688 if (CALL_EXPR_TAILCALL (exp) && (endp == 0 || target == const0_rtx))
3689 method = BLOCK_OP_TAILCALL;
3690 if (endp == 1 && target != const0_rtx)
3691 method = BLOCK_OP_NO_LIBCALL_RET;
3692 dest_addr = emit_block_move_hints (dest_mem, src_mem, len_rtx, method,
3693 expected_align, expected_size,
3694 min_size, max_size, probable_max_size);
3695 if (dest_addr == pc_rtx)
3696 return NULL_RTX;
3698 if (dest_addr == 0)
3700 dest_addr = force_operand (XEXP (dest_mem, 0), target);
3701 dest_addr = convert_memory_address (ptr_mode, dest_addr);
3704 if (endp && target != const0_rtx)
3706 dest_addr = gen_rtx_PLUS (ptr_mode, dest_addr, len_rtx);
3707 /* stpcpy pointer to last byte. */
3708 if (endp == 2)
3709 dest_addr = gen_rtx_MINUS (ptr_mode, dest_addr, const1_rtx);
3712 return dest_addr;
3715 static rtx
3716 expand_builtin_mempcpy_args (tree dest, tree src, tree len,
3717 rtx target, tree orig_exp, int endp)
3719 return expand_builtin_memory_copy_args (dest, src, len, target, orig_exp,
3720 endp);
3723 /* Expand into a movstr instruction, if one is available. Return NULL_RTX if
3724 we failed, the caller should emit a normal call, otherwise try to
3725 get the result in TARGET, if convenient. If ENDP is 0 return the
3726 destination pointer, if ENDP is 1 return the end pointer ala
3727 mempcpy, and if ENDP is 2 return the end pointer minus one ala
3728 stpcpy. */
3730 static rtx
3731 expand_movstr (tree dest, tree src, rtx target, int endp)
3733 struct expand_operand ops[3];
3734 rtx dest_mem;
3735 rtx src_mem;
3737 if (!targetm.have_movstr ())
3738 return NULL_RTX;
3740 dest_mem = get_memory_rtx (dest, NULL);
3741 src_mem = get_memory_rtx (src, NULL);
3742 if (!endp)
3744 target = force_reg (Pmode, XEXP (dest_mem, 0));
3745 dest_mem = replace_equiv_address (dest_mem, target);
3748 create_output_operand (&ops[0], endp ? target : NULL_RTX, Pmode);
3749 create_fixed_operand (&ops[1], dest_mem);
3750 create_fixed_operand (&ops[2], src_mem);
3751 if (!maybe_expand_insn (targetm.code_for_movstr, 3, ops))
3752 return NULL_RTX;
3754 if (endp && target != const0_rtx)
3756 target = ops[0].value;
3757 /* movstr is supposed to set end to the address of the NUL
3758 terminator. If the caller requested a mempcpy-like return value,
3759 adjust it. */
3760 if (endp == 1)
3762 rtx tem = plus_constant (GET_MODE (target),
3763 gen_lowpart (GET_MODE (target), target), 1);
3764 emit_move_insn (target, force_operand (tem, NULL_RTX));
3767 return target;
3770 /* Do some very basic size validation of a call to the strcpy builtin
3771 given by EXP. Return NULL_RTX to have the built-in expand to a call
3772 to the library function. */
3774 static rtx
3775 expand_builtin_strcat (tree exp, rtx)
3777 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE)
3778 || !warn_stringop_overflow)
3779 return NULL_RTX;
3781 tree dest = CALL_EXPR_ARG (exp, 0);
3782 tree src = CALL_EXPR_ARG (exp, 1);
3784 /* There is no way here to determine the length of the string in
3785 the destination to which the SRC string is being appended so
3786 just diagnose cases when the souce string is longer than
3787 the destination object. */
3789 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
3791 check_access (exp, dest, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE, src,
3792 destsize);
3794 return NULL_RTX;
3797 /* Expand expression EXP, which is a call to the strcpy builtin. Return
3798 NULL_RTX if we failed the caller should emit a normal call, otherwise
3799 try to get the result in TARGET, if convenient (and in mode MODE if that's
3800 convenient). */
3802 static rtx
3803 expand_builtin_strcpy (tree exp, rtx target)
3805 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3806 return NULL_RTX;
3808 tree dest = CALL_EXPR_ARG (exp, 0);
3809 tree src = CALL_EXPR_ARG (exp, 1);
3811 if (warn_stringop_overflow)
3813 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
3814 check_access (exp, dest, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE,
3815 src, destsize);
3818 if (rtx ret = expand_builtin_strcpy_args (dest, src, target))
3820 /* Check to see if the argument was declared attribute nonstring
3821 and if so, issue a warning since at this point it's not known
3822 to be nul-terminated. */
3823 tree fndecl = get_callee_fndecl (exp);
3824 maybe_warn_nonstring_arg (fndecl, exp);
3825 return ret;
3828 return NULL_RTX;
3831 /* Helper function to do the actual work for expand_builtin_strcpy. The
3832 arguments to the builtin_strcpy call DEST and SRC are broken out
3833 so that this can also be called without constructing an actual CALL_EXPR.
3834 The other arguments and return value are the same as for
3835 expand_builtin_strcpy. */
3837 static rtx
3838 expand_builtin_strcpy_args (tree dest, tree src, rtx target)
3840 return expand_movstr (dest, src, target, /*endp=*/0);
3843 /* Expand a call EXP to the stpcpy builtin.
3844 Return NULL_RTX if we failed the caller should emit a normal call,
3845 otherwise try to get the result in TARGET, if convenient (and in
3846 mode MODE if that's convenient). */
3848 static rtx
3849 expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
3851 tree dst, src;
3852 location_t loc = EXPR_LOCATION (exp);
3854 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
3855 return NULL_RTX;
3857 dst = CALL_EXPR_ARG (exp, 0);
3858 src = CALL_EXPR_ARG (exp, 1);
3860 if (warn_stringop_overflow)
3862 tree destsize = compute_objsize (dst, warn_stringop_overflow - 1);
3863 check_access (exp, dst, src, /*size=*/NULL_TREE, /*maxread=*/NULL_TREE,
3864 src, destsize);
3867 /* If return value is ignored, transform stpcpy into strcpy. */
3868 if (target == const0_rtx && builtin_decl_implicit (BUILT_IN_STRCPY))
3870 tree fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3871 tree result = build_call_nofold_loc (loc, fn, 2, dst, src);
3872 return expand_expr (result, target, mode, EXPAND_NORMAL);
3874 else
3876 tree len, lenp1;
3877 rtx ret;
3879 /* Ensure we get an actual string whose length can be evaluated at
3880 compile-time, not an expression containing a string. This is
3881 because the latter will potentially produce pessimized code
3882 when used to produce the return value. */
3883 if (! c_getstr (src) || ! (len = c_strlen (src, 0)))
3884 return expand_movstr (dst, src, target, /*endp=*/2);
3886 lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
3887 ret = expand_builtin_mempcpy_args (dst, src, lenp1,
3888 target, exp, /*endp=*/2);
3890 if (ret)
3891 return ret;
3893 if (TREE_CODE (len) == INTEGER_CST)
3895 rtx len_rtx = expand_normal (len);
3897 if (CONST_INT_P (len_rtx))
3899 ret = expand_builtin_strcpy_args (dst, src, target);
3901 if (ret)
3903 if (! target)
3905 if (mode != VOIDmode)
3906 target = gen_reg_rtx (mode);
3907 else
3908 target = gen_reg_rtx (GET_MODE (ret));
3910 if (GET_MODE (target) != GET_MODE (ret))
3911 ret = gen_lowpart (GET_MODE (target), ret);
3913 ret = plus_constant (GET_MODE (ret), ret, INTVAL (len_rtx));
3914 ret = emit_move_insn (target, force_operand (ret, NULL_RTX));
3915 gcc_assert (ret);
3917 return target;
3922 return expand_movstr (dst, src, target, /*endp=*/2);
3926 /* Expand a call EXP to the stpcpy builtin and diagnose uses of nonstring
3927 arguments while being careful to avoid duplicate warnings (which could
3928 be issued if the expander were to expand the call, resulting in it
3929 being emitted in expand_call(). */
3931 static rtx
3932 expand_builtin_stpcpy (tree exp, rtx target, machine_mode mode)
3934 if (rtx ret = expand_builtin_stpcpy_1 (exp, target, mode))
3936 /* The call has been successfully expanded. Check for nonstring
3937 arguments and issue warnings as appropriate. */
3938 maybe_warn_nonstring_arg (get_callee_fndecl (exp), exp);
3939 return ret;
3942 return NULL_RTX;
3945 /* Check a call EXP to the stpncpy built-in for validity.
3946 Return NULL_RTX on both success and failure. */
3948 static rtx
3949 expand_builtin_stpncpy (tree exp, rtx)
3951 if (!validate_arglist (exp,
3952 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
3953 || !warn_stringop_overflow)
3954 return NULL_RTX;
3956 /* The source and destination of the call. */
3957 tree dest = CALL_EXPR_ARG (exp, 0);
3958 tree src = CALL_EXPR_ARG (exp, 1);
3960 /* The exact number of bytes to write (not the maximum). */
3961 tree len = CALL_EXPR_ARG (exp, 2);
3963 /* The size of the destination object. */
3964 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
3966 check_access (exp, dest, src, len, /*maxread=*/NULL_TREE, src, destsize);
3968 return NULL_RTX;
3971 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
3972 bytes from constant string DATA + OFFSET and return it as target
3973 constant. */
3976 builtin_strncpy_read_str (void *data, HOST_WIDE_INT offset,
3977 scalar_int_mode mode)
3979 const char *str = (const char *) data;
3981 if ((unsigned HOST_WIDE_INT) offset > strlen (str))
3982 return const0_rtx;
3984 return c_readstr (str + offset, mode);
3987 /* Helper to check the sizes of sequences and the destination of calls
3988 to __builtin_strncat and __builtin___strncat_chk. Returns true on
3989 success (no overflow or invalid sizes), false otherwise. */
3991 static bool
3992 check_strncat_sizes (tree exp, tree objsize)
3994 tree dest = CALL_EXPR_ARG (exp, 0);
3995 tree src = CALL_EXPR_ARG (exp, 1);
3996 tree maxread = CALL_EXPR_ARG (exp, 2);
3998 /* Try to determine the range of lengths that the source expression
3999 refers to. */
4000 tree lenrange[2];
4001 get_range_strlen (src, lenrange);
4003 /* Try to verify that the destination is big enough for the shortest
4004 string. */
4006 if (!objsize && warn_stringop_overflow)
4008 /* If it hasn't been provided by __strncat_chk, try to determine
4009 the size of the destination object into which the source is
4010 being copied. */
4011 objsize = compute_objsize (dest, warn_stringop_overflow - 1);
4014 /* Add one for the terminating nul. */
4015 tree srclen = (lenrange[0]
4016 ? fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
4017 size_one_node)
4018 : NULL_TREE);
4020 /* The strncat function copies at most MAXREAD bytes and always appends
4021 the terminating nul so the specified upper bound should never be equal
4022 to (or greater than) the size of the destination. */
4023 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (objsize)
4024 && tree_int_cst_equal (objsize, maxread))
4026 location_t loc = tree_nonartificial_location (exp);
4027 loc = expansion_point_location_if_in_system_header (loc);
4029 warning_at (loc, OPT_Wstringop_overflow_,
4030 "%K%qD specified bound %E equals destination size",
4031 exp, get_callee_fndecl (exp), maxread);
4033 return false;
4036 if (!srclen
4037 || (maxread && tree_fits_uhwi_p (maxread)
4038 && tree_fits_uhwi_p (srclen)
4039 && tree_int_cst_lt (maxread, srclen)))
4040 srclen = maxread;
4042 /* The number of bytes to write is LEN but check_access will also
4043 check SRCLEN if LEN's value isn't known. */
4044 return check_access (exp, dest, src, /*size=*/NULL_TREE, maxread, srclen,
4045 objsize);
4048 /* Similar to expand_builtin_strcat, do some very basic size validation
4049 of a call to the strcpy builtin given by EXP. Return NULL_RTX to have
4050 the built-in expand to a call to the library function. */
4052 static rtx
4053 expand_builtin_strncat (tree exp, rtx)
4055 if (!validate_arglist (exp,
4056 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
4057 || !warn_stringop_overflow)
4058 return NULL_RTX;
4060 tree dest = CALL_EXPR_ARG (exp, 0);
4061 tree src = CALL_EXPR_ARG (exp, 1);
4062 /* The upper bound on the number of bytes to write. */
4063 tree maxread = CALL_EXPR_ARG (exp, 2);
4064 /* The length of the source sequence. */
4065 tree slen = c_strlen (src, 1);
4067 /* Try to determine the range of lengths that the source expression
4068 refers to. */
4069 tree lenrange[2];
4070 if (slen)
4071 lenrange[0] = lenrange[1] = slen;
4072 else
4073 get_range_strlen (src, lenrange);
4075 /* Try to verify that the destination is big enough for the shortest
4076 string. First try to determine the size of the destination object
4077 into which the source is being copied. */
4078 tree destsize = compute_objsize (dest, warn_stringop_overflow - 1);
4080 /* Add one for the terminating nul. */
4081 tree srclen = (lenrange[0]
4082 ? fold_build2 (PLUS_EXPR, size_type_node, lenrange[0],
4083 size_one_node)
4084 : NULL_TREE);
4086 /* The strncat function copies at most MAXREAD bytes and always appends
4087 the terminating nul so the specified upper bound should never be equal
4088 to (or greater than) the size of the destination. */
4089 if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (destsize)
4090 && tree_int_cst_equal (destsize, maxread))
4092 location_t loc = tree_nonartificial_location (exp);
4093 loc = expansion_point_location_if_in_system_header (loc);
4095 warning_at (loc, OPT_Wstringop_overflow_,
4096 "%K%qD specified bound %E equals destination size",
4097 exp, get_callee_fndecl (exp), maxread);
4099 return NULL_RTX;
4102 if (!srclen
4103 || (maxread && tree_fits_uhwi_p (maxread)
4104 && tree_fits_uhwi_p (srclen)
4105 && tree_int_cst_lt (maxread, srclen)))
4106 srclen = maxread;
4108 /* The number of bytes to write is SRCLEN. */
4109 check_access (exp, dest, src, NULL_TREE, maxread, srclen, destsize);
4111 return NULL_RTX;
4114 /* Expand expression EXP, which is a call to the strncpy builtin. Return
4115 NULL_RTX if we failed the caller should emit a normal call. */
4117 static rtx
4118 expand_builtin_strncpy (tree exp, rtx target)
4120 location_t loc = EXPR_LOCATION (exp);
4122 if (validate_arglist (exp,
4123 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4125 tree dest = CALL_EXPR_ARG (exp, 0);
4126 tree src = CALL_EXPR_ARG (exp, 1);
4127 /* The number of bytes to write (not the maximum). */
4128 tree len = CALL_EXPR_ARG (exp, 2);
4129 /* The length of the source sequence. */
4130 tree slen = c_strlen (src, 1);
4132 if (warn_stringop_overflow)
4134 tree destsize = compute_objsize (dest,
4135 warn_stringop_overflow - 1);
4137 /* The number of bytes to write is LEN but check_access will also
4138 check SLEN if LEN's value isn't known. */
4139 check_access (exp, dest, src, len, /*maxread=*/NULL_TREE, src,
4140 destsize);
4143 /* We must be passed a constant len and src parameter. */
4144 if (!tree_fits_uhwi_p (len) || !slen || !tree_fits_uhwi_p (slen))
4145 return NULL_RTX;
4147 slen = size_binop_loc (loc, PLUS_EXPR, slen, ssize_int (1));
4149 /* We're required to pad with trailing zeros if the requested
4150 len is greater than strlen(s2)+1. In that case try to
4151 use store_by_pieces, if it fails, punt. */
4152 if (tree_int_cst_lt (slen, len))
4154 unsigned int dest_align = get_pointer_alignment (dest);
4155 const char *p = c_getstr (src);
4156 rtx dest_mem;
4158 if (!p || dest_align == 0 || !tree_fits_uhwi_p (len)
4159 || !can_store_by_pieces (tree_to_uhwi (len),
4160 builtin_strncpy_read_str,
4161 CONST_CAST (char *, p),
4162 dest_align, false))
4163 return NULL_RTX;
4165 dest_mem = get_memory_rtx (dest, len);
4166 store_by_pieces (dest_mem, tree_to_uhwi (len),
4167 builtin_strncpy_read_str,
4168 CONST_CAST (char *, p), dest_align, false, 0);
4169 dest_mem = force_operand (XEXP (dest_mem, 0), target);
4170 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4171 return dest_mem;
4174 return NULL_RTX;
4177 /* Callback routine for store_by_pieces. Read GET_MODE_BITSIZE (MODE)
4178 bytes from constant string DATA + OFFSET and return it as target
4179 constant. */
4182 builtin_memset_read_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4183 scalar_int_mode mode)
4185 const char *c = (const char *) data;
4186 char *p = XALLOCAVEC (char, GET_MODE_SIZE (mode));
4188 memset (p, *c, GET_MODE_SIZE (mode));
4190 return c_readstr (p, mode);
4193 /* Callback routine for store_by_pieces. Return the RTL of a register
4194 containing GET_MODE_SIZE (MODE) consecutive copies of the unsigned
4195 char value given in the RTL register data. For example, if mode is
4196 4 bytes wide, return the RTL for 0x01010101*data. */
4198 static rtx
4199 builtin_memset_gen_str (void *data, HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
4200 scalar_int_mode mode)
4202 rtx target, coeff;
4203 size_t size;
4204 char *p;
4206 size = GET_MODE_SIZE (mode);
4207 if (size == 1)
4208 return (rtx) data;
4210 p = XALLOCAVEC (char, size);
4211 memset (p, 1, size);
4212 coeff = c_readstr (p, mode);
4214 target = convert_to_mode (mode, (rtx) data, 1);
4215 target = expand_mult (mode, target, coeff, NULL_RTX, 1);
4216 return force_reg (mode, target);
4219 /* Expand expression EXP, which is a call to the memset builtin. Return
4220 NULL_RTX if we failed the caller should emit a normal call, otherwise
4221 try to get the result in TARGET, if convenient (and in mode MODE if that's
4222 convenient). */
4224 static rtx
4225 expand_builtin_memset (tree exp, rtx target, machine_mode mode)
4227 if (!validate_arglist (exp,
4228 POINTER_TYPE, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
4229 return NULL_RTX;
4231 tree dest = CALL_EXPR_ARG (exp, 0);
4232 tree val = CALL_EXPR_ARG (exp, 1);
4233 tree len = CALL_EXPR_ARG (exp, 2);
4235 check_memop_access (exp, dest, NULL_TREE, len);
4237 return expand_builtin_memset_args (dest, val, len, target, mode, exp);
4240 /* Helper function to do the actual work for expand_builtin_memset. The
4241 arguments to the builtin_memset call DEST, VAL, and LEN are broken out
4242 so that this can also be called without constructing an actual CALL_EXPR.
4243 The other arguments and return value are the same as for
4244 expand_builtin_memset. */
4246 static rtx
4247 expand_builtin_memset_args (tree dest, tree val, tree len,
4248 rtx target, machine_mode mode, tree orig_exp)
4250 tree fndecl, fn;
4251 enum built_in_function fcode;
4252 machine_mode val_mode;
4253 char c;
4254 unsigned int dest_align;
4255 rtx dest_mem, dest_addr, len_rtx;
4256 HOST_WIDE_INT expected_size = -1;
4257 unsigned int expected_align = 0;
4258 unsigned HOST_WIDE_INT min_size;
4259 unsigned HOST_WIDE_INT max_size;
4260 unsigned HOST_WIDE_INT probable_max_size;
4262 dest_align = get_pointer_alignment (dest);
4264 /* If DEST is not a pointer type, don't do this operation in-line. */
4265 if (dest_align == 0)
4266 return NULL_RTX;
4268 if (currently_expanding_gimple_stmt)
4269 stringop_block_profile (currently_expanding_gimple_stmt,
4270 &expected_align, &expected_size);
4272 if (expected_align < dest_align)
4273 expected_align = dest_align;
4275 /* If the LEN parameter is zero, return DEST. */
4276 if (integer_zerop (len))
4278 /* Evaluate and ignore VAL in case it has side-effects. */
4279 expand_expr (val, const0_rtx, VOIDmode, EXPAND_NORMAL);
4280 return expand_expr (dest, target, mode, EXPAND_NORMAL);
4283 /* Stabilize the arguments in case we fail. */
4284 dest = builtin_save_expr (dest);
4285 val = builtin_save_expr (val);
4286 len = builtin_save_expr (len);
4288 len_rtx = expand_normal (len);
4289 determine_block_size (len, len_rtx, &min_size, &max_size,
4290 &probable_max_size);
4291 dest_mem = get_memory_rtx (dest, len);
4292 val_mode = TYPE_MODE (unsigned_char_type_node);
4294 if (TREE_CODE (val) != INTEGER_CST)
4296 rtx val_rtx;
4298 val_rtx = expand_normal (val);
4299 val_rtx = convert_to_mode (val_mode, val_rtx, 0);
4301 /* Assume that we can memset by pieces if we can store
4302 * the coefficients by pieces (in the required modes).
4303 * We can't pass builtin_memset_gen_str as that emits RTL. */
4304 c = 1;
4305 if (tree_fits_uhwi_p (len)
4306 && can_store_by_pieces (tree_to_uhwi (len),
4307 builtin_memset_read_str, &c, dest_align,
4308 true))
4310 val_rtx = force_reg (val_mode, val_rtx);
4311 store_by_pieces (dest_mem, tree_to_uhwi (len),
4312 builtin_memset_gen_str, val_rtx, dest_align,
4313 true, 0);
4315 else if (!set_storage_via_setmem (dest_mem, len_rtx, val_rtx,
4316 dest_align, expected_align,
4317 expected_size, min_size, max_size,
4318 probable_max_size))
4319 goto do_libcall;
4321 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4322 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4323 return dest_mem;
4326 if (target_char_cast (val, &c))
4327 goto do_libcall;
4329 if (c)
4331 if (tree_fits_uhwi_p (len)
4332 && can_store_by_pieces (tree_to_uhwi (len),
4333 builtin_memset_read_str, &c, dest_align,
4334 true))
4335 store_by_pieces (dest_mem, tree_to_uhwi (len),
4336 builtin_memset_read_str, &c, dest_align, true, 0);
4337 else if (!set_storage_via_setmem (dest_mem, len_rtx,
4338 gen_int_mode (c, val_mode),
4339 dest_align, expected_align,
4340 expected_size, min_size, max_size,
4341 probable_max_size))
4342 goto do_libcall;
4344 dest_mem = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4345 dest_mem = convert_memory_address (ptr_mode, dest_mem);
4346 return dest_mem;
4349 set_mem_align (dest_mem, dest_align);
4350 dest_addr = clear_storage_hints (dest_mem, len_rtx,
4351 CALL_EXPR_TAILCALL (orig_exp)
4352 ? BLOCK_OP_TAILCALL : BLOCK_OP_NORMAL,
4353 expected_align, expected_size,
4354 min_size, max_size,
4355 probable_max_size);
4357 if (dest_addr == 0)
4359 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
4360 dest_addr = convert_memory_address (ptr_mode, dest_addr);
4363 return dest_addr;
4365 do_libcall:
4366 fndecl = get_callee_fndecl (orig_exp);
4367 fcode = DECL_FUNCTION_CODE (fndecl);
4368 if (fcode == BUILT_IN_MEMSET)
4369 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 3,
4370 dest, val, len);
4371 else if (fcode == BUILT_IN_BZERO)
4372 fn = build_call_nofold_loc (EXPR_LOCATION (orig_exp), fndecl, 2,
4373 dest, len);
4374 else
4375 gcc_unreachable ();
4376 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4377 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (orig_exp);
4378 return expand_call (fn, target, target == const0_rtx);
4381 /* Expand expression EXP, which is a call to the bzero builtin. Return
4382 NULL_RTX if we failed the caller should emit a normal call. */
4384 static rtx
4385 expand_builtin_bzero (tree exp)
4387 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4388 return NULL_RTX;
4390 tree dest = CALL_EXPR_ARG (exp, 0);
4391 tree size = CALL_EXPR_ARG (exp, 1);
4393 check_memop_access (exp, dest, NULL_TREE, size);
4395 /* New argument list transforming bzero(ptr x, int y) to
4396 memset(ptr x, int 0, size_t y). This is done this way
4397 so that if it isn't expanded inline, we fallback to
4398 calling bzero instead of memset. */
4400 location_t loc = EXPR_LOCATION (exp);
4402 return expand_builtin_memset_args (dest, integer_zero_node,
4403 fold_convert_loc (loc,
4404 size_type_node, size),
4405 const0_rtx, VOIDmode, exp);
4408 /* Try to expand cmpstr operation ICODE with the given operands.
4409 Return the result rtx on success, otherwise return null. */
4411 static rtx
4412 expand_cmpstr (insn_code icode, rtx target, rtx arg1_rtx, rtx arg2_rtx,
4413 HOST_WIDE_INT align)
4415 machine_mode insn_mode = insn_data[icode].operand[0].mode;
4417 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
4418 target = NULL_RTX;
4420 struct expand_operand ops[4];
4421 create_output_operand (&ops[0], target, insn_mode);
4422 create_fixed_operand (&ops[1], arg1_rtx);
4423 create_fixed_operand (&ops[2], arg2_rtx);
4424 create_integer_operand (&ops[3], align);
4425 if (maybe_expand_insn (icode, 4, ops))
4426 return ops[0].value;
4427 return NULL_RTX;
4430 /* Expand expression EXP, which is a call to the memcmp built-in function.
4431 Return NULL_RTX if we failed and the caller should emit a normal call,
4432 otherwise try to get the result in TARGET, if convenient.
4433 RESULT_EQ is true if we can relax the returned value to be either zero
4434 or nonzero, without caring about the sign. */
4436 static rtx
4437 expand_builtin_memcmp (tree exp, rtx target, bool result_eq)
4439 if (!validate_arglist (exp,
4440 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4441 return NULL_RTX;
4443 tree arg1 = CALL_EXPR_ARG (exp, 0);
4444 tree arg2 = CALL_EXPR_ARG (exp, 1);
4445 tree len = CALL_EXPR_ARG (exp, 2);
4447 /* Diagnose calls where the specified length exceeds the size of either
4448 object. */
4449 if (warn_stringop_overflow)
4451 tree size = compute_objsize (arg1, 0);
4452 if (check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, len,
4453 /*maxread=*/NULL_TREE, size, /*objsize=*/NULL_TREE))
4455 size = compute_objsize (arg2, 0);
4456 check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, len,
4457 /*maxread=*/NULL_TREE, size, /*objsize=*/NULL_TREE);
4461 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4462 location_t loc = EXPR_LOCATION (exp);
4464 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4465 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4467 /* If we don't have POINTER_TYPE, call the function. */
4468 if (arg1_align == 0 || arg2_align == 0)
4469 return NULL_RTX;
4471 rtx arg1_rtx = get_memory_rtx (arg1, len);
4472 rtx arg2_rtx = get_memory_rtx (arg2, len);
4473 rtx len_rtx = expand_normal (fold_convert_loc (loc, sizetype, len));
4475 /* Set MEM_SIZE as appropriate. */
4476 if (CONST_INT_P (len_rtx))
4478 set_mem_size (arg1_rtx, INTVAL (len_rtx));
4479 set_mem_size (arg2_rtx, INTVAL (len_rtx));
4482 by_pieces_constfn constfn = NULL;
4484 const char *src_str = c_getstr (arg2);
4485 if (result_eq && src_str == NULL)
4487 src_str = c_getstr (arg1);
4488 if (src_str != NULL)
4489 std::swap (arg1_rtx, arg2_rtx);
4492 /* If SRC is a string constant and block move would be done
4493 by pieces, we can avoid loading the string from memory
4494 and only stored the computed constants. */
4495 if (src_str
4496 && CONST_INT_P (len_rtx)
4497 && (unsigned HOST_WIDE_INT) INTVAL (len_rtx) <= strlen (src_str) + 1)
4498 constfn = builtin_memcpy_read_str;
4500 rtx result = emit_block_cmp_hints (arg1_rtx, arg2_rtx, len_rtx,
4501 TREE_TYPE (len), target,
4502 result_eq, constfn,
4503 CONST_CAST (char *, src_str));
4505 if (result)
4507 /* Return the value in the proper mode for this function. */
4508 if (GET_MODE (result) == mode)
4509 return result;
4511 if (target != 0)
4513 convert_move (target, result, 0);
4514 return target;
4517 return convert_to_mode (mode, result, 0);
4520 return NULL_RTX;
4523 /* Expand expression EXP, which is a call to the strcmp builtin. Return NULL_RTX
4524 if we failed the caller should emit a normal call, otherwise try to get
4525 the result in TARGET, if convenient. */
4527 static rtx
4528 expand_builtin_strcmp (tree exp, ATTRIBUTE_UNUSED rtx target)
4530 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
4531 return NULL_RTX;
4533 insn_code cmpstr_icode = direct_optab_handler (cmpstr_optab, SImode);
4534 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4535 if (cmpstr_icode == CODE_FOR_nothing && cmpstrn_icode == CODE_FOR_nothing)
4536 return NULL_RTX;
4538 tree arg1 = CALL_EXPR_ARG (exp, 0);
4539 tree arg2 = CALL_EXPR_ARG (exp, 1);
4541 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4542 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4544 /* If we don't have POINTER_TYPE, call the function. */
4545 if (arg1_align == 0 || arg2_align == 0)
4546 return NULL_RTX;
4548 /* Stabilize the arguments in case gen_cmpstr(n)si fail. */
4549 arg1 = builtin_save_expr (arg1);
4550 arg2 = builtin_save_expr (arg2);
4552 rtx arg1_rtx = get_memory_rtx (arg1, NULL);
4553 rtx arg2_rtx = get_memory_rtx (arg2, NULL);
4555 rtx result = NULL_RTX;
4556 /* Try to call cmpstrsi. */
4557 if (cmpstr_icode != CODE_FOR_nothing)
4558 result = expand_cmpstr (cmpstr_icode, target, arg1_rtx, arg2_rtx,
4559 MIN (arg1_align, arg2_align));
4561 /* Try to determine at least one length and call cmpstrnsi. */
4562 if (!result && cmpstrn_icode != CODE_FOR_nothing)
4564 tree len;
4565 rtx arg3_rtx;
4567 tree len1 = c_strlen (arg1, 1);
4568 tree len2 = c_strlen (arg2, 1);
4570 if (len1)
4571 len1 = size_binop (PLUS_EXPR, ssize_int (1), len1);
4572 if (len2)
4573 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
4575 /* If we don't have a constant length for the first, use the length
4576 of the second, if we know it. We don't require a constant for
4577 this case; some cost analysis could be done if both are available
4578 but neither is constant. For now, assume they're equally cheap,
4579 unless one has side effects. If both strings have constant lengths,
4580 use the smaller. */
4582 if (!len1)
4583 len = len2;
4584 else if (!len2)
4585 len = len1;
4586 else if (TREE_SIDE_EFFECTS (len1))
4587 len = len2;
4588 else if (TREE_SIDE_EFFECTS (len2))
4589 len = len1;
4590 else if (TREE_CODE (len1) != INTEGER_CST)
4591 len = len2;
4592 else if (TREE_CODE (len2) != INTEGER_CST)
4593 len = len1;
4594 else if (tree_int_cst_lt (len1, len2))
4595 len = len1;
4596 else
4597 len = len2;
4599 /* If both arguments have side effects, we cannot optimize. */
4600 if (len && !TREE_SIDE_EFFECTS (len))
4602 arg3_rtx = expand_normal (len);
4603 result = expand_cmpstrn_or_cmpmem
4604 (cmpstrn_icode, target, arg1_rtx, arg2_rtx, TREE_TYPE (len),
4605 arg3_rtx, MIN (arg1_align, arg2_align));
4609 tree fndecl = get_callee_fndecl (exp);
4610 if (result)
4612 /* Check to see if the argument was declared attribute nonstring
4613 and if so, issue a warning since at this point it's not known
4614 to be nul-terminated. */
4615 maybe_warn_nonstring_arg (fndecl, exp);
4617 /* Return the value in the proper mode for this function. */
4618 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4619 if (GET_MODE (result) == mode)
4620 return result;
4621 if (target == 0)
4622 return convert_to_mode (mode, result, 0);
4623 convert_move (target, result, 0);
4624 return target;
4627 /* Expand the library call ourselves using a stabilized argument
4628 list to avoid re-evaluating the function's arguments twice. */
4629 tree fn = build_call_nofold_loc (EXPR_LOCATION (exp), fndecl, 2, arg1, arg2);
4630 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4631 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4632 return expand_call (fn, target, target == const0_rtx);
4635 /* Expand expression EXP, which is a call to the strncmp builtin. Return
4636 NULL_RTX if we failed the caller should emit a normal call, otherwise try to get
4637 the result in TARGET, if convenient. */
4639 static rtx
4640 expand_builtin_strncmp (tree exp, ATTRIBUTE_UNUSED rtx target,
4641 ATTRIBUTE_UNUSED machine_mode mode)
4643 if (!validate_arglist (exp,
4644 POINTER_TYPE, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
4645 return NULL_RTX;
4647 /* If c_strlen can determine an expression for one of the string
4648 lengths, and it doesn't have side effects, then emit cmpstrnsi
4649 using length MIN(strlen(string)+1, arg3). */
4650 insn_code cmpstrn_icode = direct_optab_handler (cmpstrn_optab, SImode);
4651 if (cmpstrn_icode == CODE_FOR_nothing)
4652 return NULL_RTX;
4654 tree len;
4656 tree arg1 = CALL_EXPR_ARG (exp, 0);
4657 tree arg2 = CALL_EXPR_ARG (exp, 1);
4658 tree arg3 = CALL_EXPR_ARG (exp, 2);
4660 unsigned int arg1_align = get_pointer_alignment (arg1) / BITS_PER_UNIT;
4661 unsigned int arg2_align = get_pointer_alignment (arg2) / BITS_PER_UNIT;
4663 tree len1 = c_strlen (arg1, 1);
4664 tree len2 = c_strlen (arg2, 1);
4666 location_t loc = EXPR_LOCATION (exp);
4668 if (len1)
4669 len1 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len1);
4670 if (len2)
4671 len2 = size_binop_loc (loc, PLUS_EXPR, ssize_int (1), len2);
4673 tree len3 = fold_convert_loc (loc, sizetype, arg3);
4675 /* If we don't have a constant length for the first, use the length
4676 of the second, if we know it. If neither string is constant length,
4677 use the given length argument. We don't require a constant for
4678 this case; some cost analysis could be done if both are available
4679 but neither is constant. For now, assume they're equally cheap,
4680 unless one has side effects. If both strings have constant lengths,
4681 use the smaller. */
4683 if (!len1 && !len2)
4684 len = len3;
4685 else if (!len1)
4686 len = len2;
4687 else if (!len2)
4688 len = len1;
4689 else if (TREE_SIDE_EFFECTS (len1))
4690 len = len2;
4691 else if (TREE_SIDE_EFFECTS (len2))
4692 len = len1;
4693 else if (TREE_CODE (len1) != INTEGER_CST)
4694 len = len2;
4695 else if (TREE_CODE (len2) != INTEGER_CST)
4696 len = len1;
4697 else if (tree_int_cst_lt (len1, len2))
4698 len = len1;
4699 else
4700 len = len2;
4702 /* If we are not using the given length, we must incorporate it here.
4703 The actual new length parameter will be MIN(len,arg3) in this case. */
4704 if (len != len3)
4705 len = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (len), len, len3);
4706 rtx arg1_rtx = get_memory_rtx (arg1, len);
4707 rtx arg2_rtx = get_memory_rtx (arg2, len);
4708 rtx arg3_rtx = expand_normal (len);
4709 rtx result = expand_cmpstrn_or_cmpmem (cmpstrn_icode, target, arg1_rtx,
4710 arg2_rtx, TREE_TYPE (len), arg3_rtx,
4711 MIN (arg1_align, arg2_align));
4713 tree fndecl = get_callee_fndecl (exp);
4714 if (result)
4716 /* Check to see if the argument was declared attribute nonstring
4717 and if so, issue a warning since at this point it's not known
4718 to be nul-terminated. */
4719 maybe_warn_nonstring_arg (fndecl, exp);
4721 /* Return the value in the proper mode for this function. */
4722 mode = TYPE_MODE (TREE_TYPE (exp));
4723 if (GET_MODE (result) == mode)
4724 return result;
4725 if (target == 0)
4726 return convert_to_mode (mode, result, 0);
4727 convert_move (target, result, 0);
4728 return target;
4731 /* Expand the library call ourselves using a stabilized argument
4732 list to avoid re-evaluating the function's arguments twice. */
4733 tree fn = build_call_nofold_loc (loc, fndecl, 3, arg1, arg2, len);
4734 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
4735 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
4736 return expand_call (fn, target, target == const0_rtx);
4739 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
4740 if that's convenient. */
4743 expand_builtin_saveregs (void)
4745 rtx val;
4746 rtx_insn *seq;
4748 /* Don't do __builtin_saveregs more than once in a function.
4749 Save the result of the first call and reuse it. */
4750 if (saveregs_value != 0)
4751 return saveregs_value;
4753 /* When this function is called, it means that registers must be
4754 saved on entry to this function. So we migrate the call to the
4755 first insn of this function. */
4757 start_sequence ();
4759 /* Do whatever the machine needs done in this case. */
4760 val = targetm.calls.expand_builtin_saveregs ();
4762 seq = get_insns ();
4763 end_sequence ();
4765 saveregs_value = val;
4767 /* Put the insns after the NOTE that starts the function. If this
4768 is inside a start_sequence, make the outer-level insn chain current, so
4769 the code is placed at the start of the function. */
4770 push_topmost_sequence ();
4771 emit_insn_after (seq, entry_of_function ());
4772 pop_topmost_sequence ();
4774 return val;
4777 /* Expand a call to __builtin_next_arg. */
4779 static rtx
4780 expand_builtin_next_arg (void)
4782 /* Checking arguments is already done in fold_builtin_next_arg
4783 that must be called before this function. */
4784 return expand_binop (ptr_mode, add_optab,
4785 crtl->args.internal_arg_pointer,
4786 crtl->args.arg_offset_rtx,
4787 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4790 /* Make it easier for the backends by protecting the valist argument
4791 from multiple evaluations. */
4793 static tree
4794 stabilize_va_list_loc (location_t loc, tree valist, int needs_lvalue)
4796 tree vatype = targetm.canonical_va_list_type (TREE_TYPE (valist));
4798 /* The current way of determining the type of valist is completely
4799 bogus. We should have the information on the va builtin instead. */
4800 if (!vatype)
4801 vatype = targetm.fn_abi_va_list (cfun->decl);
4803 if (TREE_CODE (vatype) == ARRAY_TYPE)
4805 if (TREE_SIDE_EFFECTS (valist))
4806 valist = save_expr (valist);
4808 /* For this case, the backends will be expecting a pointer to
4809 vatype, but it's possible we've actually been given an array
4810 (an actual TARGET_CANONICAL_VA_LIST_TYPE (valist)).
4811 So fix it. */
4812 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
4814 tree p1 = build_pointer_type (TREE_TYPE (vatype));
4815 valist = build_fold_addr_expr_with_type_loc (loc, valist, p1);
4818 else
4820 tree pt = build_pointer_type (vatype);
4822 if (! needs_lvalue)
4824 if (! TREE_SIDE_EFFECTS (valist))
4825 return valist;
4827 valist = fold_build1_loc (loc, ADDR_EXPR, pt, valist);
4828 TREE_SIDE_EFFECTS (valist) = 1;
4831 if (TREE_SIDE_EFFECTS (valist))
4832 valist = save_expr (valist);
4833 valist = fold_build2_loc (loc, MEM_REF,
4834 vatype, valist, build_int_cst (pt, 0));
4837 return valist;
4840 /* The "standard" definition of va_list is void*. */
4842 tree
4843 std_build_builtin_va_list (void)
4845 return ptr_type_node;
4848 /* The "standard" abi va_list is va_list_type_node. */
4850 tree
4851 std_fn_abi_va_list (tree fndecl ATTRIBUTE_UNUSED)
4853 return va_list_type_node;
4856 /* The "standard" type of va_list is va_list_type_node. */
4858 tree
4859 std_canonical_va_list_type (tree type)
4861 tree wtype, htype;
4863 wtype = va_list_type_node;
4864 htype = type;
4866 if (TREE_CODE (wtype) == ARRAY_TYPE)
4868 /* If va_list is an array type, the argument may have decayed
4869 to a pointer type, e.g. by being passed to another function.
4870 In that case, unwrap both types so that we can compare the
4871 underlying records. */
4872 if (TREE_CODE (htype) == ARRAY_TYPE
4873 || POINTER_TYPE_P (htype))
4875 wtype = TREE_TYPE (wtype);
4876 htype = TREE_TYPE (htype);
4879 if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
4880 return va_list_type_node;
4882 return NULL_TREE;
4885 /* The "standard" implementation of va_start: just assign `nextarg' to
4886 the variable. */
4888 void
4889 std_expand_builtin_va_start (tree valist, rtx nextarg)
4891 rtx va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
4892 convert_move (va_r, nextarg, 0);
4895 /* Expand EXP, a call to __builtin_va_start. */
4897 static rtx
4898 expand_builtin_va_start (tree exp)
4900 rtx nextarg;
4901 tree valist;
4902 location_t loc = EXPR_LOCATION (exp);
4904 if (call_expr_nargs (exp) < 2)
4906 error_at (loc, "too few arguments to function %<va_start%>");
4907 return const0_rtx;
4910 if (fold_builtin_next_arg (exp, true))
4911 return const0_rtx;
4913 nextarg = expand_builtin_next_arg ();
4914 valist = stabilize_va_list_loc (loc, CALL_EXPR_ARG (exp, 0), 1);
4916 if (targetm.expand_builtin_va_start)
4917 targetm.expand_builtin_va_start (valist, nextarg);
4918 else
4919 std_expand_builtin_va_start (valist, nextarg);
4921 return const0_rtx;
4924 /* Expand EXP, a call to __builtin_va_end. */
4926 static rtx
4927 expand_builtin_va_end (tree exp)
4929 tree valist = CALL_EXPR_ARG (exp, 0);
4931 /* Evaluate for side effects, if needed. I hate macros that don't
4932 do that. */
4933 if (TREE_SIDE_EFFECTS (valist))
4934 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
4936 return const0_rtx;
4939 /* Expand EXP, a call to __builtin_va_copy. We do this as a
4940 builtin rather than just as an assignment in stdarg.h because of the
4941 nastiness of array-type va_list types. */
4943 static rtx
4944 expand_builtin_va_copy (tree exp)
4946 tree dst, src, t;
4947 location_t loc = EXPR_LOCATION (exp);
4949 dst = CALL_EXPR_ARG (exp, 0);
4950 src = CALL_EXPR_ARG (exp, 1);
4952 dst = stabilize_va_list_loc (loc, dst, 1);
4953 src = stabilize_va_list_loc (loc, src, 0);
4955 gcc_assert (cfun != NULL && cfun->decl != NULL_TREE);
4957 if (TREE_CODE (targetm.fn_abi_va_list (cfun->decl)) != ARRAY_TYPE)
4959 t = build2 (MODIFY_EXPR, targetm.fn_abi_va_list (cfun->decl), dst, src);
4960 TREE_SIDE_EFFECTS (t) = 1;
4961 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4963 else
4965 rtx dstb, srcb, size;
4967 /* Evaluate to pointers. */
4968 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
4969 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
4970 size = expand_expr (TYPE_SIZE_UNIT (targetm.fn_abi_va_list (cfun->decl)),
4971 NULL_RTX, VOIDmode, EXPAND_NORMAL);
4973 dstb = convert_memory_address (Pmode, dstb);
4974 srcb = convert_memory_address (Pmode, srcb);
4976 /* "Dereference" to BLKmode memories. */
4977 dstb = gen_rtx_MEM (BLKmode, dstb);
4978 set_mem_alias_set (dstb, get_alias_set (TREE_TYPE (TREE_TYPE (dst))));
4979 set_mem_align (dstb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4980 srcb = gen_rtx_MEM (BLKmode, srcb);
4981 set_mem_alias_set (srcb, get_alias_set (TREE_TYPE (TREE_TYPE (src))));
4982 set_mem_align (srcb, TYPE_ALIGN (targetm.fn_abi_va_list (cfun->decl)));
4984 /* Copy. */
4985 emit_block_move (dstb, srcb, size, BLOCK_OP_NORMAL);
4988 return const0_rtx;
4991 /* Expand a call to one of the builtin functions __builtin_frame_address or
4992 __builtin_return_address. */
4994 static rtx
4995 expand_builtin_frame_address (tree fndecl, tree exp)
4997 /* The argument must be a nonnegative integer constant.
4998 It counts the number of frames to scan up the stack.
4999 The value is either the frame pointer value or the return
5000 address saved in that frame. */
5001 if (call_expr_nargs (exp) == 0)
5002 /* Warning about missing arg was already issued. */
5003 return const0_rtx;
5004 else if (! tree_fits_uhwi_p (CALL_EXPR_ARG (exp, 0)))
5006 error ("invalid argument to %qD", fndecl);
5007 return const0_rtx;
5009 else
5011 /* Number of frames to scan up the stack. */
5012 unsigned HOST_WIDE_INT count = tree_to_uhwi (CALL_EXPR_ARG (exp, 0));
5014 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl), count);
5016 /* Some ports cannot access arbitrary stack frames. */
5017 if (tem == NULL)
5019 warning (0, "unsupported argument to %qD", fndecl);
5020 return const0_rtx;
5023 if (count)
5025 /* Warn since no effort is made to ensure that any frame
5026 beyond the current one exists or can be safely reached. */
5027 warning (OPT_Wframe_address, "calling %qD with "
5028 "a nonzero argument is unsafe", fndecl);
5031 /* For __builtin_frame_address, return what we've got. */
5032 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
5033 return tem;
5035 if (!REG_P (tem)
5036 && ! CONSTANT_P (tem))
5037 tem = copy_addr_to_reg (tem);
5038 return tem;
5042 /* Expand EXP, a call to the alloca builtin. Return NULL_RTX if we
5043 failed and the caller should emit a normal call. */
5045 static rtx
5046 expand_builtin_alloca (tree exp)
5048 rtx op0;
5049 rtx result;
5050 unsigned int align;
5051 tree fndecl = get_callee_fndecl (exp);
5052 HOST_WIDE_INT max_size;
5053 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5054 bool alloca_for_var = CALL_ALLOCA_FOR_VAR_P (exp);
5055 bool valid_arglist
5056 = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5057 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, INTEGER_TYPE,
5058 VOID_TYPE)
5059 : fcode == BUILT_IN_ALLOCA_WITH_ALIGN
5060 ? validate_arglist (exp, INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE)
5061 : validate_arglist (exp, INTEGER_TYPE, VOID_TYPE));
5063 if (!valid_arglist)
5064 return NULL_RTX;
5066 if ((alloca_for_var && !warn_vla_limit)
5067 || (!alloca_for_var && !warn_alloca_limit))
5069 /* -Walloca-larger-than and -Wvla-larger-than settings override
5070 the more general -Walloc-size-larger-than so unless either of
5071 the former options is specified check the alloca arguments for
5072 overflow. */
5073 tree args[] = { CALL_EXPR_ARG (exp, 0), NULL_TREE };
5074 int idx[] = { 0, -1 };
5075 maybe_warn_alloc_args_overflow (fndecl, exp, args, idx);
5078 /* Compute the argument. */
5079 op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
5081 /* Compute the alignment. */
5082 align = (fcode == BUILT_IN_ALLOCA
5083 ? BIGGEST_ALIGNMENT
5084 : TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 1)));
5086 /* Compute the maximum size. */
5087 max_size = (fcode == BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX
5088 ? TREE_INT_CST_LOW (CALL_EXPR_ARG (exp, 2))
5089 : -1);
5091 /* Allocate the desired space. If the allocation stems from the declaration
5092 of a variable-sized object, it cannot accumulate. */
5093 result
5094 = allocate_dynamic_stack_space (op0, 0, align, max_size, alloca_for_var);
5095 result = convert_memory_address (ptr_mode, result);
5097 return result;
5100 /* Emit a call to __asan_allocas_unpoison call in EXP. Add to second argument
5101 of the call virtual_stack_dynamic_rtx - stack_pointer_rtx, which is the
5102 STACK_DYNAMIC_OFFSET value. See motivation for this in comment to
5103 handle_builtin_stack_restore function. */
5105 static rtx
5106 expand_asan_emit_allocas_unpoison (tree exp)
5108 tree arg0 = CALL_EXPR_ARG (exp, 0);
5109 tree arg1 = CALL_EXPR_ARG (exp, 1);
5110 rtx top = expand_expr (arg0, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5111 rtx bot = expand_expr (arg1, NULL_RTX, ptr_mode, EXPAND_NORMAL);
5112 rtx off = expand_simple_binop (Pmode, MINUS, virtual_stack_dynamic_rtx,
5113 stack_pointer_rtx, NULL_RTX, 0,
5114 OPTAB_LIB_WIDEN);
5115 off = convert_modes (ptr_mode, Pmode, off, 0);
5116 bot = expand_simple_binop (ptr_mode, PLUS, bot, off, NULL_RTX, 0,
5117 OPTAB_LIB_WIDEN);
5118 rtx ret = init_one_libfunc ("__asan_allocas_unpoison");
5119 ret = emit_library_call_value (ret, NULL_RTX, LCT_NORMAL, ptr_mode,
5120 top, ptr_mode, bot, ptr_mode);
5121 return ret;
5124 /* Expand a call to bswap builtin in EXP.
5125 Return NULL_RTX if a normal call should be emitted rather than expanding the
5126 function in-line. If convenient, the result should be placed in TARGET.
5127 SUBTARGET may be used as the target for computing one of EXP's operands. */
5129 static rtx
5130 expand_builtin_bswap (machine_mode target_mode, tree exp, rtx target,
5131 rtx subtarget)
5133 tree arg;
5134 rtx op0;
5136 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5137 return NULL_RTX;
5139 arg = CALL_EXPR_ARG (exp, 0);
5140 op0 = expand_expr (arg,
5141 subtarget && GET_MODE (subtarget) == target_mode
5142 ? subtarget : NULL_RTX,
5143 target_mode, EXPAND_NORMAL);
5144 if (GET_MODE (op0) != target_mode)
5145 op0 = convert_to_mode (target_mode, op0, 1);
5147 target = expand_unop (target_mode, bswap_optab, op0, target, 1);
5149 gcc_assert (target);
5151 return convert_to_mode (target_mode, target, 1);
5154 /* Expand a call to a unary builtin in EXP.
5155 Return NULL_RTX if a normal call should be emitted rather than expanding the
5156 function in-line. If convenient, the result should be placed in TARGET.
5157 SUBTARGET may be used as the target for computing one of EXP's operands. */
5159 static rtx
5160 expand_builtin_unop (machine_mode target_mode, tree exp, rtx target,
5161 rtx subtarget, optab op_optab)
5163 rtx op0;
5165 if (!validate_arglist (exp, INTEGER_TYPE, VOID_TYPE))
5166 return NULL_RTX;
5168 /* Compute the argument. */
5169 op0 = expand_expr (CALL_EXPR_ARG (exp, 0),
5170 (subtarget
5171 && (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0)))
5172 == GET_MODE (subtarget))) ? subtarget : NULL_RTX,
5173 VOIDmode, EXPAND_NORMAL);
5174 /* Compute op, into TARGET if possible.
5175 Set TARGET to wherever the result comes back. */
5176 target = expand_unop (TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))),
5177 op_optab, op0, target, op_optab != clrsb_optab);
5178 gcc_assert (target);
5180 return convert_to_mode (target_mode, target, 0);
5183 /* Expand a call to __builtin_expect. We just return our argument
5184 as the builtin_expect semantic should've been already executed by
5185 tree branch prediction pass. */
5187 static rtx
5188 expand_builtin_expect (tree exp, rtx target)
5190 tree arg;
5192 if (call_expr_nargs (exp) < 2)
5193 return const0_rtx;
5194 arg = CALL_EXPR_ARG (exp, 0);
5196 target = expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5197 /* When guessing was done, the hints should be already stripped away. */
5198 gcc_assert (!flag_guess_branch_prob
5199 || optimize == 0 || seen_error ());
5200 return target;
5203 /* Expand a call to __builtin_assume_aligned. We just return our first
5204 argument as the builtin_assume_aligned semantic should've been already
5205 executed by CCP. */
5207 static rtx
5208 expand_builtin_assume_aligned (tree exp, rtx target)
5210 if (call_expr_nargs (exp) < 2)
5211 return const0_rtx;
5212 target = expand_expr (CALL_EXPR_ARG (exp, 0), target, VOIDmode,
5213 EXPAND_NORMAL);
5214 gcc_assert (!TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 1))
5215 && (call_expr_nargs (exp) < 3
5216 || !TREE_SIDE_EFFECTS (CALL_EXPR_ARG (exp, 2))));
5217 return target;
5220 void
5221 expand_builtin_trap (void)
5223 if (targetm.have_trap ())
5225 rtx_insn *insn = emit_insn (targetm.gen_trap ());
5226 /* For trap insns when not accumulating outgoing args force
5227 REG_ARGS_SIZE note to prevent crossjumping of calls with
5228 different args sizes. */
5229 if (!ACCUMULATE_OUTGOING_ARGS)
5230 add_args_size_note (insn, stack_pointer_delta);
5232 else
5234 tree fn = builtin_decl_implicit (BUILT_IN_ABORT);
5235 tree call_expr = build_call_expr (fn, 0);
5236 expand_call (call_expr, NULL_RTX, false);
5239 emit_barrier ();
5242 /* Expand a call to __builtin_unreachable. We do nothing except emit
5243 a barrier saying that control flow will not pass here.
5245 It is the responsibility of the program being compiled to ensure
5246 that control flow does never reach __builtin_unreachable. */
5247 static void
5248 expand_builtin_unreachable (void)
5250 emit_barrier ();
5253 /* Expand EXP, a call to fabs, fabsf or fabsl.
5254 Return NULL_RTX if a normal call should be emitted rather than expanding
5255 the function inline. If convenient, the result should be placed
5256 in TARGET. SUBTARGET may be used as the target for computing
5257 the operand. */
5259 static rtx
5260 expand_builtin_fabs (tree exp, rtx target, rtx subtarget)
5262 machine_mode mode;
5263 tree arg;
5264 rtx op0;
5266 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5267 return NULL_RTX;
5269 arg = CALL_EXPR_ARG (exp, 0);
5270 CALL_EXPR_ARG (exp, 0) = arg = builtin_save_expr (arg);
5271 mode = TYPE_MODE (TREE_TYPE (arg));
5272 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5273 return expand_abs (mode, op0, target, 0, safe_from_p (target, arg, 1));
5276 /* Expand EXP, a call to copysign, copysignf, or copysignl.
5277 Return NULL is a normal call should be emitted rather than expanding the
5278 function inline. If convenient, the result should be placed in TARGET.
5279 SUBTARGET may be used as the target for computing the operand. */
5281 static rtx
5282 expand_builtin_copysign (tree exp, rtx target, rtx subtarget)
5284 rtx op0, op1;
5285 tree arg;
5287 if (!validate_arglist (exp, REAL_TYPE, REAL_TYPE, VOID_TYPE))
5288 return NULL_RTX;
5290 arg = CALL_EXPR_ARG (exp, 0);
5291 op0 = expand_expr (arg, subtarget, VOIDmode, EXPAND_NORMAL);
5293 arg = CALL_EXPR_ARG (exp, 1);
5294 op1 = expand_normal (arg);
5296 return expand_copysign (op0, op1, target);
5299 /* Expand a call to __builtin___clear_cache. */
5301 static rtx
5302 expand_builtin___clear_cache (tree exp)
5304 if (!targetm.code_for_clear_cache)
5306 #ifdef CLEAR_INSN_CACHE
5307 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5308 does something. Just do the default expansion to a call to
5309 __clear_cache(). */
5310 return NULL_RTX;
5311 #else
5312 /* There is no "clear_cache" insn, and __clear_cache() in libgcc
5313 does nothing. There is no need to call it. Do nothing. */
5314 return const0_rtx;
5315 #endif /* CLEAR_INSN_CACHE */
5318 /* We have a "clear_cache" insn, and it will handle everything. */
5319 tree begin, end;
5320 rtx begin_rtx, end_rtx;
5322 /* We must not expand to a library call. If we did, any
5323 fallback library function in libgcc that might contain a call to
5324 __builtin___clear_cache() would recurse infinitely. */
5325 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
5327 error ("both arguments to %<__builtin___clear_cache%> must be pointers");
5328 return const0_rtx;
5331 if (targetm.have_clear_cache ())
5333 struct expand_operand ops[2];
5335 begin = CALL_EXPR_ARG (exp, 0);
5336 begin_rtx = expand_expr (begin, NULL_RTX, Pmode, EXPAND_NORMAL);
5338 end = CALL_EXPR_ARG (exp, 1);
5339 end_rtx = expand_expr (end, NULL_RTX, Pmode, EXPAND_NORMAL);
5341 create_address_operand (&ops[0], begin_rtx);
5342 create_address_operand (&ops[1], end_rtx);
5343 if (maybe_expand_insn (targetm.code_for_clear_cache, 2, ops))
5344 return const0_rtx;
5346 return const0_rtx;
5349 /* Given a trampoline address, make sure it satisfies TRAMPOLINE_ALIGNMENT. */
5351 static rtx
5352 round_trampoline_addr (rtx tramp)
5354 rtx temp, addend, mask;
5356 /* If we don't need too much alignment, we'll have been guaranteed
5357 proper alignment by get_trampoline_type. */
5358 if (TRAMPOLINE_ALIGNMENT <= STACK_BOUNDARY)
5359 return tramp;
5361 /* Round address up to desired boundary. */
5362 temp = gen_reg_rtx (Pmode);
5363 addend = gen_int_mode (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1, Pmode);
5364 mask = gen_int_mode (-TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT, Pmode);
5366 temp = expand_simple_binop (Pmode, PLUS, tramp, addend,
5367 temp, 0, OPTAB_LIB_WIDEN);
5368 tramp = expand_simple_binop (Pmode, AND, temp, mask,
5369 temp, 0, OPTAB_LIB_WIDEN);
5371 return tramp;
5374 static rtx
5375 expand_builtin_init_trampoline (tree exp, bool onstack)
5377 tree t_tramp, t_func, t_chain;
5378 rtx m_tramp, r_tramp, r_chain, tmp;
5380 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE,
5381 POINTER_TYPE, VOID_TYPE))
5382 return NULL_RTX;
5384 t_tramp = CALL_EXPR_ARG (exp, 0);
5385 t_func = CALL_EXPR_ARG (exp, 1);
5386 t_chain = CALL_EXPR_ARG (exp, 2);
5388 r_tramp = expand_normal (t_tramp);
5389 m_tramp = gen_rtx_MEM (BLKmode, r_tramp);
5390 MEM_NOTRAP_P (m_tramp) = 1;
5392 /* If ONSTACK, the TRAMP argument should be the address of a field
5393 within the local function's FRAME decl. Either way, let's see if
5394 we can fill in the MEM_ATTRs for this memory. */
5395 if (TREE_CODE (t_tramp) == ADDR_EXPR)
5396 set_mem_attributes (m_tramp, TREE_OPERAND (t_tramp, 0), true);
5398 /* Creator of a heap trampoline is responsible for making sure the
5399 address is aligned to at least STACK_BOUNDARY. Normally malloc
5400 will ensure this anyhow. */
5401 tmp = round_trampoline_addr (r_tramp);
5402 if (tmp != r_tramp)
5404 m_tramp = change_address (m_tramp, BLKmode, tmp);
5405 set_mem_align (m_tramp, TRAMPOLINE_ALIGNMENT);
5406 set_mem_size (m_tramp, TRAMPOLINE_SIZE);
5409 /* The FUNC argument should be the address of the nested function.
5410 Extract the actual function decl to pass to the hook. */
5411 gcc_assert (TREE_CODE (t_func) == ADDR_EXPR);
5412 t_func = TREE_OPERAND (t_func, 0);
5413 gcc_assert (TREE_CODE (t_func) == FUNCTION_DECL);
5415 r_chain = expand_normal (t_chain);
5417 /* Generate insns to initialize the trampoline. */
5418 targetm.calls.trampoline_init (m_tramp, t_func, r_chain);
5420 if (onstack)
5422 trampolines_created = 1;
5424 if (targetm.calls.custom_function_descriptors != 0)
5425 warning_at (DECL_SOURCE_LOCATION (t_func), OPT_Wtrampolines,
5426 "trampoline generated for nested function %qD", t_func);
5429 return const0_rtx;
5432 static rtx
5433 expand_builtin_adjust_trampoline (tree exp)
5435 rtx tramp;
5437 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5438 return NULL_RTX;
5440 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5441 tramp = round_trampoline_addr (tramp);
5442 if (targetm.calls.trampoline_adjust_address)
5443 tramp = targetm.calls.trampoline_adjust_address (tramp);
5445 return tramp;
5448 /* Expand a call to the builtin descriptor initialization routine.
5449 A descriptor is made up of a couple of pointers to the static
5450 chain and the code entry in this order. */
5452 static rtx
5453 expand_builtin_init_descriptor (tree exp)
5455 tree t_descr, t_func, t_chain;
5456 rtx m_descr, r_descr, r_func, r_chain;
5458 if (!validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
5459 VOID_TYPE))
5460 return NULL_RTX;
5462 t_descr = CALL_EXPR_ARG (exp, 0);
5463 t_func = CALL_EXPR_ARG (exp, 1);
5464 t_chain = CALL_EXPR_ARG (exp, 2);
5466 r_descr = expand_normal (t_descr);
5467 m_descr = gen_rtx_MEM (BLKmode, r_descr);
5468 MEM_NOTRAP_P (m_descr) = 1;
5470 r_func = expand_normal (t_func);
5471 r_chain = expand_normal (t_chain);
5473 /* Generate insns to initialize the descriptor. */
5474 emit_move_insn (adjust_address_nv (m_descr, ptr_mode, 0), r_chain);
5475 emit_move_insn (adjust_address_nv (m_descr, ptr_mode,
5476 POINTER_SIZE / BITS_PER_UNIT), r_func);
5478 return const0_rtx;
5481 /* Expand a call to the builtin descriptor adjustment routine. */
5483 static rtx
5484 expand_builtin_adjust_descriptor (tree exp)
5486 rtx tramp;
5488 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
5489 return NULL_RTX;
5491 tramp = expand_normal (CALL_EXPR_ARG (exp, 0));
5493 /* Unalign the descriptor to allow runtime identification. */
5494 tramp = plus_constant (ptr_mode, tramp,
5495 targetm.calls.custom_function_descriptors);
5497 return force_operand (tramp, NULL_RTX);
5500 /* Expand the call EXP to the built-in signbit, signbitf or signbitl
5501 function. The function first checks whether the back end provides
5502 an insn to implement signbit for the respective mode. If not, it
5503 checks whether the floating point format of the value is such that
5504 the sign bit can be extracted. If that is not the case, error out.
5505 EXP is the expression that is a call to the builtin function; if
5506 convenient, the result should be placed in TARGET. */
5507 static rtx
5508 expand_builtin_signbit (tree exp, rtx target)
5510 const struct real_format *fmt;
5511 scalar_float_mode fmode;
5512 scalar_int_mode rmode, imode;
5513 tree arg;
5514 int word, bitpos;
5515 enum insn_code icode;
5516 rtx temp;
5517 location_t loc = EXPR_LOCATION (exp);
5519 if (!validate_arglist (exp, REAL_TYPE, VOID_TYPE))
5520 return NULL_RTX;
5522 arg = CALL_EXPR_ARG (exp, 0);
5523 fmode = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (arg));
5524 rmode = SCALAR_INT_TYPE_MODE (TREE_TYPE (exp));
5525 fmt = REAL_MODE_FORMAT (fmode);
5527 arg = builtin_save_expr (arg);
5529 /* Expand the argument yielding a RTX expression. */
5530 temp = expand_normal (arg);
5532 /* Check if the back end provides an insn that handles signbit for the
5533 argument's mode. */
5534 icode = optab_handler (signbit_optab, fmode);
5535 if (icode != CODE_FOR_nothing)
5537 rtx_insn *last = get_last_insn ();
5538 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
5539 if (maybe_emit_unop_insn (icode, target, temp, UNKNOWN))
5540 return target;
5541 delete_insns_since (last);
5544 /* For floating point formats without a sign bit, implement signbit
5545 as "ARG < 0.0". */
5546 bitpos = fmt->signbit_ro;
5547 if (bitpos < 0)
5549 /* But we can't do this if the format supports signed zero. */
5550 gcc_assert (!fmt->has_signed_zero || !HONOR_SIGNED_ZEROS (fmode));
5552 arg = fold_build2_loc (loc, LT_EXPR, TREE_TYPE (exp), arg,
5553 build_real (TREE_TYPE (arg), dconst0));
5554 return expand_expr (arg, target, VOIDmode, EXPAND_NORMAL);
5557 if (GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
5559 imode = int_mode_for_mode (fmode).require ();
5560 temp = gen_lowpart (imode, temp);
5562 else
5564 imode = word_mode;
5565 /* Handle targets with different FP word orders. */
5566 if (FLOAT_WORDS_BIG_ENDIAN)
5567 word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
5568 else
5569 word = bitpos / BITS_PER_WORD;
5570 temp = operand_subword_force (temp, word, fmode);
5571 bitpos = bitpos % BITS_PER_WORD;
5574 /* Force the intermediate word_mode (or narrower) result into a
5575 register. This avoids attempting to create paradoxical SUBREGs
5576 of floating point modes below. */
5577 temp = force_reg (imode, temp);
5579 /* If the bitpos is within the "result mode" lowpart, the operation
5580 can be implement with a single bitwise AND. Otherwise, we need
5581 a right shift and an AND. */
5583 if (bitpos < GET_MODE_BITSIZE (rmode))
5585 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (rmode));
5587 if (GET_MODE_SIZE (imode) > GET_MODE_SIZE (rmode))
5588 temp = gen_lowpart (rmode, temp);
5589 temp = expand_binop (rmode, and_optab, temp,
5590 immed_wide_int_const (mask, rmode),
5591 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5593 else
5595 /* Perform a logical right shift to place the signbit in the least
5596 significant bit, then truncate the result to the desired mode
5597 and mask just this bit. */
5598 temp = expand_shift (RSHIFT_EXPR, imode, temp, bitpos, NULL_RTX, 1);
5599 temp = gen_lowpart (rmode, temp);
5600 temp = expand_binop (rmode, and_optab, temp, const1_rtx,
5601 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5604 return temp;
5607 /* Expand fork or exec calls. TARGET is the desired target of the
5608 call. EXP is the call. FN is the
5609 identificator of the actual function. IGNORE is nonzero if the
5610 value is to be ignored. */
5612 static rtx
5613 expand_builtin_fork_or_exec (tree fn, tree exp, rtx target, int ignore)
5615 tree id, decl;
5616 tree call;
5618 /* If we are not profiling, just call the function. */
5619 if (!profile_arc_flag)
5620 return NULL_RTX;
5622 /* Otherwise call the wrapper. This should be equivalent for the rest of
5623 compiler, so the code does not diverge, and the wrapper may run the
5624 code necessary for keeping the profiling sane. */
5626 switch (DECL_FUNCTION_CODE (fn))
5628 case BUILT_IN_FORK:
5629 id = get_identifier ("__gcov_fork");
5630 break;
5632 case BUILT_IN_EXECL:
5633 id = get_identifier ("__gcov_execl");
5634 break;
5636 case BUILT_IN_EXECV:
5637 id = get_identifier ("__gcov_execv");
5638 break;
5640 case BUILT_IN_EXECLP:
5641 id = get_identifier ("__gcov_execlp");
5642 break;
5644 case BUILT_IN_EXECLE:
5645 id = get_identifier ("__gcov_execle");
5646 break;
5648 case BUILT_IN_EXECVP:
5649 id = get_identifier ("__gcov_execvp");
5650 break;
5652 case BUILT_IN_EXECVE:
5653 id = get_identifier ("__gcov_execve");
5654 break;
5656 default:
5657 gcc_unreachable ();
5660 decl = build_decl (DECL_SOURCE_LOCATION (fn),
5661 FUNCTION_DECL, id, TREE_TYPE (fn));
5662 DECL_EXTERNAL (decl) = 1;
5663 TREE_PUBLIC (decl) = 1;
5664 DECL_ARTIFICIAL (decl) = 1;
5665 TREE_NOTHROW (decl) = 1;
5666 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
5667 DECL_VISIBILITY_SPECIFIED (decl) = 1;
5668 call = rewrite_call_expr (EXPR_LOCATION (exp), exp, 0, decl, 0);
5669 return expand_call (call, target, ignore);
5674 /* Reconstitute a mode for a __sync intrinsic operation. Since the type of
5675 the pointer in these functions is void*, the tree optimizers may remove
5676 casts. The mode computed in expand_builtin isn't reliable either, due
5677 to __sync_bool_compare_and_swap.
5679 FCODE_DIFF should be fcode - base, where base is the FOO_1 code for the
5680 group of builtins. This gives us log2 of the mode size. */
5682 static inline machine_mode
5683 get_builtin_sync_mode (int fcode_diff)
5685 /* The size is not negotiable, so ask not to get BLKmode in return
5686 if the target indicates that a smaller size would be better. */
5687 return int_mode_for_size (BITS_PER_UNIT << fcode_diff, 0).require ();
5690 /* Expand the memory expression LOC and return the appropriate memory operand
5691 for the builtin_sync operations. */
5693 static rtx
5694 get_builtin_sync_mem (tree loc, machine_mode mode)
5696 rtx addr, mem;
5698 addr = expand_expr (loc, NULL_RTX, ptr_mode, EXPAND_SUM);
5699 addr = convert_memory_address (Pmode, addr);
5701 /* Note that we explicitly do not want any alias information for this
5702 memory, so that we kill all other live memories. Otherwise we don't
5703 satisfy the full barrier semantics of the intrinsic. */
5704 mem = validize_mem (gen_rtx_MEM (mode, addr));
5706 /* The alignment needs to be at least according to that of the mode. */
5707 set_mem_align (mem, MAX (GET_MODE_ALIGNMENT (mode),
5708 get_pointer_alignment (loc)));
5709 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
5710 MEM_VOLATILE_P (mem) = 1;
5712 return mem;
5715 /* Make sure an argument is in the right mode.
5716 EXP is the tree argument.
5717 MODE is the mode it should be in. */
5719 static rtx
5720 expand_expr_force_mode (tree exp, machine_mode mode)
5722 rtx val;
5723 machine_mode old_mode;
5725 val = expand_expr (exp, NULL_RTX, mode, EXPAND_NORMAL);
5726 /* If VAL is promoted to a wider mode, convert it back to MODE. Take care
5727 of CONST_INTs, where we know the old_mode only from the call argument. */
5729 old_mode = GET_MODE (val);
5730 if (old_mode == VOIDmode)
5731 old_mode = TYPE_MODE (TREE_TYPE (exp));
5732 val = convert_modes (mode, old_mode, val, 1);
5733 return val;
5737 /* Expand the __sync_xxx_and_fetch and __sync_fetch_and_xxx intrinsics.
5738 EXP is the CALL_EXPR. CODE is the rtx code
5739 that corresponds to the arithmetic or logical operation from the name;
5740 an exception here is that NOT actually means NAND. TARGET is an optional
5741 place for us to store the results; AFTER is true if this is the
5742 fetch_and_xxx form. */
5744 static rtx
5745 expand_builtin_sync_operation (machine_mode mode, tree exp,
5746 enum rtx_code code, bool after,
5747 rtx target)
5749 rtx val, mem;
5750 location_t loc = EXPR_LOCATION (exp);
5752 if (code == NOT && warn_sync_nand)
5754 tree fndecl = get_callee_fndecl (exp);
5755 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5757 static bool warned_f_a_n, warned_n_a_f;
5759 switch (fcode)
5761 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
5762 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
5763 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
5764 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
5765 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
5766 if (warned_f_a_n)
5767 break;
5769 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_FETCH_AND_NAND_N);
5770 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5771 warned_f_a_n = true;
5772 break;
5774 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
5775 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
5776 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
5777 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
5778 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
5779 if (warned_n_a_f)
5780 break;
5782 fndecl = builtin_decl_implicit (BUILT_IN_SYNC_NAND_AND_FETCH_N);
5783 inform (loc, "%qD changed semantics in GCC 4.4", fndecl);
5784 warned_n_a_f = true;
5785 break;
5787 default:
5788 gcc_unreachable ();
5792 /* Expand the operands. */
5793 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5794 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5796 return expand_atomic_fetch_op (target, mem, val, code, MEMMODEL_SYNC_SEQ_CST,
5797 after);
5800 /* Expand the __sync_val_compare_and_swap and __sync_bool_compare_and_swap
5801 intrinsics. EXP is the CALL_EXPR. IS_BOOL is
5802 true if this is the boolean form. TARGET is a place for us to store the
5803 results; this is NOT optional if IS_BOOL is true. */
5805 static rtx
5806 expand_builtin_compare_and_swap (machine_mode mode, tree exp,
5807 bool is_bool, rtx target)
5809 rtx old_val, new_val, mem;
5810 rtx *pbool, *poval;
5812 /* Expand the operands. */
5813 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5814 old_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5815 new_val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5817 pbool = poval = NULL;
5818 if (target != const0_rtx)
5820 if (is_bool)
5821 pbool = &target;
5822 else
5823 poval = &target;
5825 if (!expand_atomic_compare_and_swap (pbool, poval, mem, old_val, new_val,
5826 false, MEMMODEL_SYNC_SEQ_CST,
5827 MEMMODEL_SYNC_SEQ_CST))
5828 return NULL_RTX;
5830 return target;
5833 /* Expand the __sync_lock_test_and_set intrinsic. Note that the most
5834 general form is actually an atomic exchange, and some targets only
5835 support a reduced form with the second argument being a constant 1.
5836 EXP is the CALL_EXPR; TARGET is an optional place for us to store
5837 the results. */
5839 static rtx
5840 expand_builtin_sync_lock_test_and_set (machine_mode mode, tree exp,
5841 rtx target)
5843 rtx val, mem;
5845 /* Expand the operands. */
5846 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5847 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5849 return expand_sync_lock_test_and_set (target, mem, val);
5852 /* Expand the __sync_lock_release intrinsic. EXP is the CALL_EXPR. */
5854 static void
5855 expand_builtin_sync_lock_release (machine_mode mode, tree exp)
5857 rtx mem;
5859 /* Expand the operands. */
5860 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5862 expand_atomic_store (mem, const0_rtx, MEMMODEL_SYNC_RELEASE, true);
5865 /* Given an integer representing an ``enum memmodel'', verify its
5866 correctness and return the memory model enum. */
5868 static enum memmodel
5869 get_memmodel (tree exp)
5871 rtx op;
5872 unsigned HOST_WIDE_INT val;
5873 source_location loc
5874 = expansion_point_location_if_in_system_header (input_location);
5876 /* If the parameter is not a constant, it's a run time value so we'll just
5877 convert it to MEMMODEL_SEQ_CST to avoid annoying runtime checking. */
5878 if (TREE_CODE (exp) != INTEGER_CST)
5879 return MEMMODEL_SEQ_CST;
5881 op = expand_normal (exp);
5883 val = INTVAL (op);
5884 if (targetm.memmodel_check)
5885 val = targetm.memmodel_check (val);
5886 else if (val & ~MEMMODEL_MASK)
5888 warning_at (loc, OPT_Winvalid_memory_model,
5889 "unknown architecture specifier in memory model to builtin");
5890 return MEMMODEL_SEQ_CST;
5893 /* Should never see a user explicit SYNC memodel model, so >= LAST works. */
5894 if (memmodel_base (val) >= MEMMODEL_LAST)
5896 warning_at (loc, OPT_Winvalid_memory_model,
5897 "invalid memory model argument to builtin");
5898 return MEMMODEL_SEQ_CST;
5901 /* Workaround for Bugzilla 59448. GCC doesn't track consume properly, so
5902 be conservative and promote consume to acquire. */
5903 if (val == MEMMODEL_CONSUME)
5904 val = MEMMODEL_ACQUIRE;
5906 return (enum memmodel) val;
5909 /* Expand the __atomic_exchange intrinsic:
5910 TYPE __atomic_exchange (TYPE *object, TYPE desired, enum memmodel)
5911 EXP is the CALL_EXPR.
5912 TARGET is an optional place for us to store the results. */
5914 static rtx
5915 expand_builtin_atomic_exchange (machine_mode mode, tree exp, rtx target)
5917 rtx val, mem;
5918 enum memmodel model;
5920 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
5922 if (!flag_inline_atomics)
5923 return NULL_RTX;
5925 /* Expand the operands. */
5926 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5927 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
5929 return expand_atomic_exchange (target, mem, val, model);
5932 /* Expand the __atomic_compare_exchange intrinsic:
5933 bool __atomic_compare_exchange (TYPE *object, TYPE *expect,
5934 TYPE desired, BOOL weak,
5935 enum memmodel success,
5936 enum memmodel failure)
5937 EXP is the CALL_EXPR.
5938 TARGET is an optional place for us to store the results. */
5940 static rtx
5941 expand_builtin_atomic_compare_exchange (machine_mode mode, tree exp,
5942 rtx target)
5944 rtx expect, desired, mem, oldval;
5945 rtx_code_label *label;
5946 enum memmodel success, failure;
5947 tree weak;
5948 bool is_weak;
5949 source_location loc
5950 = expansion_point_location_if_in_system_header (input_location);
5952 success = get_memmodel (CALL_EXPR_ARG (exp, 4));
5953 failure = get_memmodel (CALL_EXPR_ARG (exp, 5));
5955 if (failure > success)
5957 warning_at (loc, OPT_Winvalid_memory_model,
5958 "failure memory model cannot be stronger than success "
5959 "memory model for %<__atomic_compare_exchange%>");
5960 success = MEMMODEL_SEQ_CST;
5963 if (is_mm_release (failure) || is_mm_acq_rel (failure))
5965 warning_at (loc, OPT_Winvalid_memory_model,
5966 "invalid failure memory model for "
5967 "%<__atomic_compare_exchange%>");
5968 failure = MEMMODEL_SEQ_CST;
5969 success = MEMMODEL_SEQ_CST;
5973 if (!flag_inline_atomics)
5974 return NULL_RTX;
5976 /* Expand the operands. */
5977 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
5979 expect = expand_normal (CALL_EXPR_ARG (exp, 1));
5980 expect = convert_memory_address (Pmode, expect);
5981 expect = gen_rtx_MEM (mode, expect);
5982 desired = expand_expr_force_mode (CALL_EXPR_ARG (exp, 2), mode);
5984 weak = CALL_EXPR_ARG (exp, 3);
5985 is_weak = false;
5986 if (tree_fits_shwi_p (weak) && tree_to_shwi (weak) != 0)
5987 is_weak = true;
5989 if (target == const0_rtx)
5990 target = NULL;
5992 /* Lest the rtl backend create a race condition with an imporoper store
5993 to memory, always create a new pseudo for OLDVAL. */
5994 oldval = NULL;
5996 if (!expand_atomic_compare_and_swap (&target, &oldval, mem, expect, desired,
5997 is_weak, success, failure))
5998 return NULL_RTX;
6000 /* Conditionally store back to EXPECT, lest we create a race condition
6001 with an improper store to memory. */
6002 /* ??? With a rearrangement of atomics at the gimple level, we can handle
6003 the normal case where EXPECT is totally private, i.e. a register. At
6004 which point the store can be unconditional. */
6005 label = gen_label_rtx ();
6006 emit_cmp_and_jump_insns (target, const0_rtx, NE, NULL,
6007 GET_MODE (target), 1, label);
6008 emit_move_insn (expect, oldval);
6009 emit_label (label);
6011 return target;
6014 /* Helper function for expand_ifn_atomic_compare_exchange - expand
6015 internal ATOMIC_COMPARE_EXCHANGE call into __atomic_compare_exchange_N
6016 call. The weak parameter must be dropped to match the expected parameter
6017 list and the expected argument changed from value to pointer to memory
6018 slot. */
6020 static void
6021 expand_ifn_atomic_compare_exchange_into_call (gcall *call, machine_mode mode)
6023 unsigned int z;
6024 vec<tree, va_gc> *vec;
6026 vec_alloc (vec, 5);
6027 vec->quick_push (gimple_call_arg (call, 0));
6028 tree expected = gimple_call_arg (call, 1);
6029 rtx x = assign_stack_temp_for_type (mode, GET_MODE_SIZE (mode),
6030 TREE_TYPE (expected));
6031 rtx expd = expand_expr (expected, x, mode, EXPAND_NORMAL);
6032 if (expd != x)
6033 emit_move_insn (x, expd);
6034 tree v = make_tree (TREE_TYPE (expected), x);
6035 vec->quick_push (build1 (ADDR_EXPR,
6036 build_pointer_type (TREE_TYPE (expected)), v));
6037 vec->quick_push (gimple_call_arg (call, 2));
6038 /* Skip the boolean weak parameter. */
6039 for (z = 4; z < 6; z++)
6040 vec->quick_push (gimple_call_arg (call, z));
6041 /* At present we only have BUILT_IN_ATOMIC_COMPARE_EXCHANGE_{1,2,4,8,16}. */
6042 unsigned int bytes_log2 = exact_log2 (GET_MODE_SIZE (mode).to_constant ());
6043 gcc_assert (bytes_log2 < 5);
6044 built_in_function fncode
6045 = (built_in_function) ((int) BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1
6046 + bytes_log2);
6047 tree fndecl = builtin_decl_explicit (fncode);
6048 tree fn = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fndecl)),
6049 fndecl);
6050 tree exp = build_call_vec (boolean_type_node, fn, vec);
6051 tree lhs = gimple_call_lhs (call);
6052 rtx boolret = expand_call (exp, NULL_RTX, lhs == NULL_TREE);
6053 if (lhs)
6055 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6056 if (GET_MODE (boolret) != mode)
6057 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6058 x = force_reg (mode, x);
6059 write_complex_part (target, boolret, true);
6060 write_complex_part (target, x, false);
6064 /* Expand IFN_ATOMIC_COMPARE_EXCHANGE internal function. */
6066 void
6067 expand_ifn_atomic_compare_exchange (gcall *call)
6069 int size = tree_to_shwi (gimple_call_arg (call, 3)) & 255;
6070 gcc_assert (size == 1 || size == 2 || size == 4 || size == 8 || size == 16);
6071 machine_mode mode = int_mode_for_size (BITS_PER_UNIT * size, 0).require ();
6072 rtx expect, desired, mem, oldval, boolret;
6073 enum memmodel success, failure;
6074 tree lhs;
6075 bool is_weak;
6076 source_location loc
6077 = expansion_point_location_if_in_system_header (gimple_location (call));
6079 success = get_memmodel (gimple_call_arg (call, 4));
6080 failure = get_memmodel (gimple_call_arg (call, 5));
6082 if (failure > success)
6084 warning_at (loc, OPT_Winvalid_memory_model,
6085 "failure memory model cannot be stronger than success "
6086 "memory model for %<__atomic_compare_exchange%>");
6087 success = MEMMODEL_SEQ_CST;
6090 if (is_mm_release (failure) || is_mm_acq_rel (failure))
6092 warning_at (loc, OPT_Winvalid_memory_model,
6093 "invalid failure memory model for "
6094 "%<__atomic_compare_exchange%>");
6095 failure = MEMMODEL_SEQ_CST;
6096 success = MEMMODEL_SEQ_CST;
6099 if (!flag_inline_atomics)
6101 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6102 return;
6105 /* Expand the operands. */
6106 mem = get_builtin_sync_mem (gimple_call_arg (call, 0), mode);
6108 expect = expand_expr_force_mode (gimple_call_arg (call, 1), mode);
6109 desired = expand_expr_force_mode (gimple_call_arg (call, 2), mode);
6111 is_weak = (tree_to_shwi (gimple_call_arg (call, 3)) & 256) != 0;
6113 boolret = NULL;
6114 oldval = NULL;
6116 if (!expand_atomic_compare_and_swap (&boolret, &oldval, mem, expect, desired,
6117 is_weak, success, failure))
6119 expand_ifn_atomic_compare_exchange_into_call (call, mode);
6120 return;
6123 lhs = gimple_call_lhs (call);
6124 if (lhs)
6126 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6127 if (GET_MODE (boolret) != mode)
6128 boolret = convert_modes (mode, GET_MODE (boolret), boolret, 1);
6129 write_complex_part (target, boolret, true);
6130 write_complex_part (target, oldval, false);
6134 /* Expand the __atomic_load intrinsic:
6135 TYPE __atomic_load (TYPE *object, enum memmodel)
6136 EXP is the CALL_EXPR.
6137 TARGET is an optional place for us to store the results. */
6139 static rtx
6140 expand_builtin_atomic_load (machine_mode mode, tree exp, rtx target)
6142 rtx mem;
6143 enum memmodel model;
6145 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6146 if (is_mm_release (model) || is_mm_acq_rel (model))
6148 source_location loc
6149 = expansion_point_location_if_in_system_header (input_location);
6150 warning_at (loc, OPT_Winvalid_memory_model,
6151 "invalid memory model for %<__atomic_load%>");
6152 model = MEMMODEL_SEQ_CST;
6155 if (!flag_inline_atomics)
6156 return NULL_RTX;
6158 /* Expand the operand. */
6159 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6161 return expand_atomic_load (target, mem, model);
6165 /* Expand the __atomic_store intrinsic:
6166 void __atomic_store (TYPE *object, TYPE desired, enum memmodel)
6167 EXP is the CALL_EXPR.
6168 TARGET is an optional place for us to store the results. */
6170 static rtx
6171 expand_builtin_atomic_store (machine_mode mode, tree exp)
6173 rtx mem, val;
6174 enum memmodel model;
6176 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6177 if (!(is_mm_relaxed (model) || is_mm_seq_cst (model)
6178 || is_mm_release (model)))
6180 source_location loc
6181 = expansion_point_location_if_in_system_header (input_location);
6182 warning_at (loc, OPT_Winvalid_memory_model,
6183 "invalid memory model for %<__atomic_store%>");
6184 model = MEMMODEL_SEQ_CST;
6187 if (!flag_inline_atomics)
6188 return NULL_RTX;
6190 /* Expand the operands. */
6191 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6192 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6194 return expand_atomic_store (mem, val, model, false);
6197 /* Expand the __atomic_fetch_XXX intrinsic:
6198 TYPE __atomic_fetch_XXX (TYPE *object, TYPE val, enum memmodel)
6199 EXP is the CALL_EXPR.
6200 TARGET is an optional place for us to store the results.
6201 CODE is the operation, PLUS, MINUS, ADD, XOR, or IOR.
6202 FETCH_AFTER is true if returning the result of the operation.
6203 FETCH_AFTER is false if returning the value before the operation.
6204 IGNORE is true if the result is not used.
6205 EXT_CALL is the correct builtin for an external call if this cannot be
6206 resolved to an instruction sequence. */
6208 static rtx
6209 expand_builtin_atomic_fetch_op (machine_mode mode, tree exp, rtx target,
6210 enum rtx_code code, bool fetch_after,
6211 bool ignore, enum built_in_function ext_call)
6213 rtx val, mem, ret;
6214 enum memmodel model;
6215 tree fndecl;
6216 tree addr;
6218 model = get_memmodel (CALL_EXPR_ARG (exp, 2));
6220 /* Expand the operands. */
6221 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6222 val = expand_expr_force_mode (CALL_EXPR_ARG (exp, 1), mode);
6224 /* Only try generating instructions if inlining is turned on. */
6225 if (flag_inline_atomics)
6227 ret = expand_atomic_fetch_op (target, mem, val, code, model, fetch_after);
6228 if (ret)
6229 return ret;
6232 /* Return if a different routine isn't needed for the library call. */
6233 if (ext_call == BUILT_IN_NONE)
6234 return NULL_RTX;
6236 /* Change the call to the specified function. */
6237 fndecl = get_callee_fndecl (exp);
6238 addr = CALL_EXPR_FN (exp);
6239 STRIP_NOPS (addr);
6241 gcc_assert (TREE_OPERAND (addr, 0) == fndecl);
6242 TREE_OPERAND (addr, 0) = builtin_decl_explicit (ext_call);
6244 /* If we will emit code after the call, the call can not be a tail call.
6245 If it is emitted as a tail call, a barrier is emitted after it, and
6246 then all trailing code is removed. */
6247 if (!ignore)
6248 CALL_EXPR_TAILCALL (exp) = 0;
6250 /* Expand the call here so we can emit trailing code. */
6251 ret = expand_call (exp, target, ignore);
6253 /* Replace the original function just in case it matters. */
6254 TREE_OPERAND (addr, 0) = fndecl;
6256 /* Then issue the arithmetic correction to return the right result. */
6257 if (!ignore)
6259 if (code == NOT)
6261 ret = expand_simple_binop (mode, AND, ret, val, NULL_RTX, true,
6262 OPTAB_LIB_WIDEN);
6263 ret = expand_simple_unop (mode, NOT, ret, target, true);
6265 else
6266 ret = expand_simple_binop (mode, code, ret, val, target, true,
6267 OPTAB_LIB_WIDEN);
6269 return ret;
6272 /* Expand IFN_ATOMIC_BIT_TEST_AND_* internal function. */
6274 void
6275 expand_ifn_atomic_bit_test_and (gcall *call)
6277 tree ptr = gimple_call_arg (call, 0);
6278 tree bit = gimple_call_arg (call, 1);
6279 tree flag = gimple_call_arg (call, 2);
6280 tree lhs = gimple_call_lhs (call);
6281 enum memmodel model = MEMMODEL_SYNC_SEQ_CST;
6282 machine_mode mode = TYPE_MODE (TREE_TYPE (flag));
6283 enum rtx_code code;
6284 optab optab;
6285 struct expand_operand ops[5];
6287 gcc_assert (flag_inline_atomics);
6289 if (gimple_call_num_args (call) == 4)
6290 model = get_memmodel (gimple_call_arg (call, 3));
6292 rtx mem = get_builtin_sync_mem (ptr, mode);
6293 rtx val = expand_expr_force_mode (bit, mode);
6295 switch (gimple_call_internal_fn (call))
6297 case IFN_ATOMIC_BIT_TEST_AND_SET:
6298 code = IOR;
6299 optab = atomic_bit_test_and_set_optab;
6300 break;
6301 case IFN_ATOMIC_BIT_TEST_AND_COMPLEMENT:
6302 code = XOR;
6303 optab = atomic_bit_test_and_complement_optab;
6304 break;
6305 case IFN_ATOMIC_BIT_TEST_AND_RESET:
6306 code = AND;
6307 optab = atomic_bit_test_and_reset_optab;
6308 break;
6309 default:
6310 gcc_unreachable ();
6313 if (lhs == NULL_TREE)
6315 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6316 val, NULL_RTX, true, OPTAB_DIRECT);
6317 if (code == AND)
6318 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6319 expand_atomic_fetch_op (const0_rtx, mem, val, code, model, false);
6320 return;
6323 rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
6324 enum insn_code icode = direct_optab_handler (optab, mode);
6325 gcc_assert (icode != CODE_FOR_nothing);
6326 create_output_operand (&ops[0], target, mode);
6327 create_fixed_operand (&ops[1], mem);
6328 create_convert_operand_to (&ops[2], val, mode, true);
6329 create_integer_operand (&ops[3], model);
6330 create_integer_operand (&ops[4], integer_onep (flag));
6331 if (maybe_expand_insn (icode, 5, ops))
6332 return;
6334 rtx bitval = val;
6335 val = expand_simple_binop (mode, ASHIFT, const1_rtx,
6336 val, NULL_RTX, true, OPTAB_DIRECT);
6337 rtx maskval = val;
6338 if (code == AND)
6339 val = expand_simple_unop (mode, NOT, val, NULL_RTX, true);
6340 rtx result = expand_atomic_fetch_op (gen_reg_rtx (mode), mem, val,
6341 code, model, false);
6342 if (integer_onep (flag))
6344 result = expand_simple_binop (mode, ASHIFTRT, result, bitval,
6345 NULL_RTX, true, OPTAB_DIRECT);
6346 result = expand_simple_binop (mode, AND, result, const1_rtx, target,
6347 true, OPTAB_DIRECT);
6349 else
6350 result = expand_simple_binop (mode, AND, result, maskval, target, true,
6351 OPTAB_DIRECT);
6352 if (result != target)
6353 emit_move_insn (target, result);
6356 /* Expand an atomic clear operation.
6357 void _atomic_clear (BOOL *obj, enum memmodel)
6358 EXP is the call expression. */
6360 static rtx
6361 expand_builtin_atomic_clear (tree exp)
6363 machine_mode mode;
6364 rtx mem, ret;
6365 enum memmodel model;
6367 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6368 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6369 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6371 if (is_mm_consume (model) || is_mm_acquire (model) || is_mm_acq_rel (model))
6373 source_location loc
6374 = expansion_point_location_if_in_system_header (input_location);
6375 warning_at (loc, OPT_Winvalid_memory_model,
6376 "invalid memory model for %<__atomic_store%>");
6377 model = MEMMODEL_SEQ_CST;
6380 /* Try issuing an __atomic_store, and allow fallback to __sync_lock_release.
6381 Failing that, a store is issued by __atomic_store. The only way this can
6382 fail is if the bool type is larger than a word size. Unlikely, but
6383 handle it anyway for completeness. Assume a single threaded model since
6384 there is no atomic support in this case, and no barriers are required. */
6385 ret = expand_atomic_store (mem, const0_rtx, model, true);
6386 if (!ret)
6387 emit_move_insn (mem, const0_rtx);
6388 return const0_rtx;
6391 /* Expand an atomic test_and_set operation.
6392 bool _atomic_test_and_set (BOOL *obj, enum memmodel)
6393 EXP is the call expression. */
6395 static rtx
6396 expand_builtin_atomic_test_and_set (tree exp, rtx target)
6398 rtx mem;
6399 enum memmodel model;
6400 machine_mode mode;
6402 mode = int_mode_for_size (BOOL_TYPE_SIZE, 0).require ();
6403 mem = get_builtin_sync_mem (CALL_EXPR_ARG (exp, 0), mode);
6404 model = get_memmodel (CALL_EXPR_ARG (exp, 1));
6406 return expand_atomic_test_and_set (target, mem, model);
6410 /* Return true if (optional) argument ARG1 of size ARG0 is always lock free on
6411 this architecture. If ARG1 is NULL, use typical alignment for size ARG0. */
6413 static tree
6414 fold_builtin_atomic_always_lock_free (tree arg0, tree arg1)
6416 int size;
6417 machine_mode mode;
6418 unsigned int mode_align, type_align;
6420 if (TREE_CODE (arg0) != INTEGER_CST)
6421 return NULL_TREE;
6423 /* We need a corresponding integer mode for the access to be lock-free. */
6424 size = INTVAL (expand_normal (arg0)) * BITS_PER_UNIT;
6425 if (!int_mode_for_size (size, 0).exists (&mode))
6426 return boolean_false_node;
6428 mode_align = GET_MODE_ALIGNMENT (mode);
6430 if (TREE_CODE (arg1) == INTEGER_CST)
6432 unsigned HOST_WIDE_INT val = UINTVAL (expand_normal (arg1));
6434 /* Either this argument is null, or it's a fake pointer encoding
6435 the alignment of the object. */
6436 val = least_bit_hwi (val);
6437 val *= BITS_PER_UNIT;
6439 if (val == 0 || mode_align < val)
6440 type_align = mode_align;
6441 else
6442 type_align = val;
6444 else
6446 tree ttype = TREE_TYPE (arg1);
6448 /* This function is usually invoked and folded immediately by the front
6449 end before anything else has a chance to look at it. The pointer
6450 parameter at this point is usually cast to a void *, so check for that
6451 and look past the cast. */
6452 if (CONVERT_EXPR_P (arg1)
6453 && POINTER_TYPE_P (ttype)
6454 && VOID_TYPE_P (TREE_TYPE (ttype))
6455 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))))
6456 arg1 = TREE_OPERAND (arg1, 0);
6458 ttype = TREE_TYPE (arg1);
6459 gcc_assert (POINTER_TYPE_P (ttype));
6461 /* Get the underlying type of the object. */
6462 ttype = TREE_TYPE (ttype);
6463 type_align = TYPE_ALIGN (ttype);
6466 /* If the object has smaller alignment, the lock free routines cannot
6467 be used. */
6468 if (type_align < mode_align)
6469 return boolean_false_node;
6471 /* Check if a compare_and_swap pattern exists for the mode which represents
6472 the required size. The pattern is not allowed to fail, so the existence
6473 of the pattern indicates support is present. Also require that an
6474 atomic load exists for the required size. */
6475 if (can_compare_and_swap_p (mode, true) && can_atomic_load_p (mode))
6476 return boolean_true_node;
6477 else
6478 return boolean_false_node;
6481 /* Return true if the parameters to call EXP represent an object which will
6482 always generate lock free instructions. The first argument represents the
6483 size of the object, and the second parameter is a pointer to the object
6484 itself. If NULL is passed for the object, then the result is based on
6485 typical alignment for an object of the specified size. Otherwise return
6486 false. */
6488 static rtx
6489 expand_builtin_atomic_always_lock_free (tree exp)
6491 tree size;
6492 tree arg0 = CALL_EXPR_ARG (exp, 0);
6493 tree arg1 = CALL_EXPR_ARG (exp, 1);
6495 if (TREE_CODE (arg0) != INTEGER_CST)
6497 error ("non-constant argument 1 to __atomic_always_lock_free");
6498 return const0_rtx;
6501 size = fold_builtin_atomic_always_lock_free (arg0, arg1);
6502 if (size == boolean_true_node)
6503 return const1_rtx;
6504 return const0_rtx;
6507 /* Return a one or zero if it can be determined that object ARG1 of size ARG
6508 is lock free on this architecture. */
6510 static tree
6511 fold_builtin_atomic_is_lock_free (tree arg0, tree arg1)
6513 if (!flag_inline_atomics)
6514 return NULL_TREE;
6516 /* If it isn't always lock free, don't generate a result. */
6517 if (fold_builtin_atomic_always_lock_free (arg0, arg1) == boolean_true_node)
6518 return boolean_true_node;
6520 return NULL_TREE;
6523 /* Return true if the parameters to call EXP represent an object which will
6524 always generate lock free instructions. The first argument represents the
6525 size of the object, and the second parameter is a pointer to the object
6526 itself. If NULL is passed for the object, then the result is based on
6527 typical alignment for an object of the specified size. Otherwise return
6528 NULL*/
6530 static rtx
6531 expand_builtin_atomic_is_lock_free (tree exp)
6533 tree size;
6534 tree arg0 = CALL_EXPR_ARG (exp, 0);
6535 tree arg1 = CALL_EXPR_ARG (exp, 1);
6537 if (!INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
6539 error ("non-integer argument 1 to __atomic_is_lock_free");
6540 return NULL_RTX;
6543 if (!flag_inline_atomics)
6544 return NULL_RTX;
6546 /* If the value is known at compile time, return the RTX for it. */
6547 size = fold_builtin_atomic_is_lock_free (arg0, arg1);
6548 if (size == boolean_true_node)
6549 return const1_rtx;
6551 return NULL_RTX;
6554 /* Expand the __atomic_thread_fence intrinsic:
6555 void __atomic_thread_fence (enum memmodel)
6556 EXP is the CALL_EXPR. */
6558 static void
6559 expand_builtin_atomic_thread_fence (tree exp)
6561 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6562 expand_mem_thread_fence (model);
6565 /* Expand the __atomic_signal_fence intrinsic:
6566 void __atomic_signal_fence (enum memmodel)
6567 EXP is the CALL_EXPR. */
6569 static void
6570 expand_builtin_atomic_signal_fence (tree exp)
6572 enum memmodel model = get_memmodel (CALL_EXPR_ARG (exp, 0));
6573 expand_mem_signal_fence (model);
6576 /* Expand the __sync_synchronize intrinsic. */
6578 static void
6579 expand_builtin_sync_synchronize (void)
6581 expand_mem_thread_fence (MEMMODEL_SYNC_SEQ_CST);
6584 static rtx
6585 expand_builtin_thread_pointer (tree exp, rtx target)
6587 enum insn_code icode;
6588 if (!validate_arglist (exp, VOID_TYPE))
6589 return const0_rtx;
6590 icode = direct_optab_handler (get_thread_pointer_optab, Pmode);
6591 if (icode != CODE_FOR_nothing)
6593 struct expand_operand op;
6594 /* If the target is not sutitable then create a new target. */
6595 if (target == NULL_RTX
6596 || !REG_P (target)
6597 || GET_MODE (target) != Pmode)
6598 target = gen_reg_rtx (Pmode);
6599 create_output_operand (&op, target, Pmode);
6600 expand_insn (icode, 1, &op);
6601 return target;
6603 error ("__builtin_thread_pointer is not supported on this target");
6604 return const0_rtx;
6607 static void
6608 expand_builtin_set_thread_pointer (tree exp)
6610 enum insn_code icode;
6611 if (!validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6612 return;
6613 icode = direct_optab_handler (set_thread_pointer_optab, Pmode);
6614 if (icode != CODE_FOR_nothing)
6616 struct expand_operand op;
6617 rtx val = expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
6618 Pmode, EXPAND_NORMAL);
6619 create_input_operand (&op, val, Pmode);
6620 expand_insn (icode, 1, &op);
6621 return;
6623 error ("__builtin_set_thread_pointer is not supported on this target");
6627 /* Emit code to restore the current value of stack. */
6629 static void
6630 expand_stack_restore (tree var)
6632 rtx_insn *prev;
6633 rtx sa = expand_normal (var);
6635 sa = convert_memory_address (Pmode, sa);
6637 prev = get_last_insn ();
6638 emit_stack_restore (SAVE_BLOCK, sa);
6640 record_new_stack_level ();
6642 fixup_args_size_notes (prev, get_last_insn (), 0);
6645 /* Emit code to save the current value of stack. */
6647 static rtx
6648 expand_stack_save (void)
6650 rtx ret = NULL_RTX;
6652 emit_stack_save (SAVE_BLOCK, &ret);
6653 return ret;
6656 /* Emit code to get the openacc gang, worker or vector id or size. */
6658 static rtx
6659 expand_builtin_goacc_parlevel_id_size (tree exp, rtx target, int ignore)
6661 const char *name;
6662 rtx fallback_retval;
6663 rtx_insn *(*gen_fn) (rtx, rtx);
6664 switch (DECL_FUNCTION_CODE (get_callee_fndecl (exp)))
6666 case BUILT_IN_GOACC_PARLEVEL_ID:
6667 name = "__builtin_goacc_parlevel_id";
6668 fallback_retval = const0_rtx;
6669 gen_fn = targetm.gen_oacc_dim_pos;
6670 break;
6671 case BUILT_IN_GOACC_PARLEVEL_SIZE:
6672 name = "__builtin_goacc_parlevel_size";
6673 fallback_retval = const1_rtx;
6674 gen_fn = targetm.gen_oacc_dim_size;
6675 break;
6676 default:
6677 gcc_unreachable ();
6680 if (oacc_get_fn_attrib (current_function_decl) == NULL_TREE)
6682 error ("%qs only supported in OpenACC code", name);
6683 return const0_rtx;
6686 tree arg = CALL_EXPR_ARG (exp, 0);
6687 if (TREE_CODE (arg) != INTEGER_CST)
6689 error ("non-constant argument 0 to %qs", name);
6690 return const0_rtx;
6693 int dim = TREE_INT_CST_LOW (arg);
6694 switch (dim)
6696 case GOMP_DIM_GANG:
6697 case GOMP_DIM_WORKER:
6698 case GOMP_DIM_VECTOR:
6699 break;
6700 default:
6701 error ("illegal argument 0 to %qs", name);
6702 return const0_rtx;
6705 if (ignore)
6706 return target;
6708 if (target == NULL_RTX)
6709 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
6711 if (!targetm.have_oacc_dim_size ())
6713 emit_move_insn (target, fallback_retval);
6714 return target;
6717 rtx reg = MEM_P (target) ? gen_reg_rtx (GET_MODE (target)) : target;
6718 emit_insn (gen_fn (reg, GEN_INT (dim)));
6719 if (reg != target)
6720 emit_move_insn (target, reg);
6722 return target;
6725 /* Expand an expression EXP that calls a built-in function,
6726 with result going to TARGET if that's convenient
6727 (and in mode MODE if that's convenient).
6728 SUBTARGET may be used as the target for computing one of EXP's operands.
6729 IGNORE is nonzero if the value is to be ignored. */
6732 expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
6733 int ignore)
6735 tree fndecl = get_callee_fndecl (exp);
6736 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
6737 machine_mode target_mode = TYPE_MODE (TREE_TYPE (exp));
6738 int flags;
6740 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6741 return targetm.expand_builtin (exp, target, subtarget, mode, ignore);
6743 /* When ASan is enabled, we don't want to expand some memory/string
6744 builtins and rely on libsanitizer's hooks. This allows us to avoid
6745 redundant checks and be sure, that possible overflow will be detected
6746 by ASan. */
6748 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_intercepted_p (fcode))
6749 return expand_call (exp, target, ignore);
6751 /* When not optimizing, generate calls to library functions for a certain
6752 set of builtins. */
6753 if (!optimize
6754 && !called_as_built_in (fndecl)
6755 && fcode != BUILT_IN_FORK
6756 && fcode != BUILT_IN_EXECL
6757 && fcode != BUILT_IN_EXECV
6758 && fcode != BUILT_IN_EXECLP
6759 && fcode != BUILT_IN_EXECLE
6760 && fcode != BUILT_IN_EXECVP
6761 && fcode != BUILT_IN_EXECVE
6762 && !ALLOCA_FUNCTION_CODE_P (fcode)
6763 && fcode != BUILT_IN_FREE)
6764 return expand_call (exp, target, ignore);
6766 /* The built-in function expanders test for target == const0_rtx
6767 to determine whether the function's result will be ignored. */
6768 if (ignore)
6769 target = const0_rtx;
6771 /* If the result of a pure or const built-in function is ignored, and
6772 none of its arguments are volatile, we can avoid expanding the
6773 built-in call and just evaluate the arguments for side-effects. */
6774 if (target == const0_rtx
6775 && ((flags = flags_from_decl_or_type (fndecl)) & (ECF_CONST | ECF_PURE))
6776 && !(flags & ECF_LOOPING_CONST_OR_PURE))
6778 bool volatilep = false;
6779 tree arg;
6780 call_expr_arg_iterator iter;
6782 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6783 if (TREE_THIS_VOLATILE (arg))
6785 volatilep = true;
6786 break;
6789 if (! volatilep)
6791 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
6792 expand_expr (arg, const0_rtx, VOIDmode, EXPAND_NORMAL);
6793 return const0_rtx;
6797 switch (fcode)
6799 CASE_FLT_FN (BUILT_IN_FABS):
6800 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
6801 case BUILT_IN_FABSD32:
6802 case BUILT_IN_FABSD64:
6803 case BUILT_IN_FABSD128:
6804 target = expand_builtin_fabs (exp, target, subtarget);
6805 if (target)
6806 return target;
6807 break;
6809 CASE_FLT_FN (BUILT_IN_COPYSIGN):
6810 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
6811 target = expand_builtin_copysign (exp, target, subtarget);
6812 if (target)
6813 return target;
6814 break;
6816 /* Just do a normal library call if we were unable to fold
6817 the values. */
6818 CASE_FLT_FN (BUILT_IN_CABS):
6819 break;
6821 CASE_FLT_FN (BUILT_IN_FMA):
6822 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
6823 target = expand_builtin_mathfn_ternary (exp, target, subtarget);
6824 if (target)
6825 return target;
6826 break;
6828 CASE_FLT_FN (BUILT_IN_ILOGB):
6829 if (! flag_unsafe_math_optimizations)
6830 break;
6831 gcc_fallthrough ();
6832 CASE_FLT_FN (BUILT_IN_ISINF):
6833 CASE_FLT_FN (BUILT_IN_FINITE):
6834 case BUILT_IN_ISFINITE:
6835 case BUILT_IN_ISNORMAL:
6836 target = expand_builtin_interclass_mathfn (exp, target);
6837 if (target)
6838 return target;
6839 break;
6841 CASE_FLT_FN (BUILT_IN_ICEIL):
6842 CASE_FLT_FN (BUILT_IN_LCEIL):
6843 CASE_FLT_FN (BUILT_IN_LLCEIL):
6844 CASE_FLT_FN (BUILT_IN_LFLOOR):
6845 CASE_FLT_FN (BUILT_IN_IFLOOR):
6846 CASE_FLT_FN (BUILT_IN_LLFLOOR):
6847 target = expand_builtin_int_roundingfn (exp, target);
6848 if (target)
6849 return target;
6850 break;
6852 CASE_FLT_FN (BUILT_IN_IRINT):
6853 CASE_FLT_FN (BUILT_IN_LRINT):
6854 CASE_FLT_FN (BUILT_IN_LLRINT):
6855 CASE_FLT_FN (BUILT_IN_IROUND):
6856 CASE_FLT_FN (BUILT_IN_LROUND):
6857 CASE_FLT_FN (BUILT_IN_LLROUND):
6858 target = expand_builtin_int_roundingfn_2 (exp, target);
6859 if (target)
6860 return target;
6861 break;
6863 CASE_FLT_FN (BUILT_IN_POWI):
6864 target = expand_builtin_powi (exp, target);
6865 if (target)
6866 return target;
6867 break;
6869 CASE_FLT_FN (BUILT_IN_CEXPI):
6870 target = expand_builtin_cexpi (exp, target);
6871 gcc_assert (target);
6872 return target;
6874 CASE_FLT_FN (BUILT_IN_SIN):
6875 CASE_FLT_FN (BUILT_IN_COS):
6876 if (! flag_unsafe_math_optimizations)
6877 break;
6878 target = expand_builtin_mathfn_3 (exp, target, subtarget);
6879 if (target)
6880 return target;
6881 break;
6883 CASE_FLT_FN (BUILT_IN_SINCOS):
6884 if (! flag_unsafe_math_optimizations)
6885 break;
6886 target = expand_builtin_sincos (exp);
6887 if (target)
6888 return target;
6889 break;
6891 case BUILT_IN_APPLY_ARGS:
6892 return expand_builtin_apply_args ();
6894 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
6895 FUNCTION with a copy of the parameters described by
6896 ARGUMENTS, and ARGSIZE. It returns a block of memory
6897 allocated on the stack into which is stored all the registers
6898 that might possibly be used for returning the result of a
6899 function. ARGUMENTS is the value returned by
6900 __builtin_apply_args. ARGSIZE is the number of bytes of
6901 arguments that must be copied. ??? How should this value be
6902 computed? We'll also need a safe worst case value for varargs
6903 functions. */
6904 case BUILT_IN_APPLY:
6905 if (!validate_arglist (exp, POINTER_TYPE,
6906 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)
6907 && !validate_arglist (exp, REFERENCE_TYPE,
6908 POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6909 return const0_rtx;
6910 else
6912 rtx ops[3];
6914 ops[0] = expand_normal (CALL_EXPR_ARG (exp, 0));
6915 ops[1] = expand_normal (CALL_EXPR_ARG (exp, 1));
6916 ops[2] = expand_normal (CALL_EXPR_ARG (exp, 2));
6918 return expand_builtin_apply (ops[0], ops[1], ops[2]);
6921 /* __builtin_return (RESULT) causes the function to return the
6922 value described by RESULT. RESULT is address of the block of
6923 memory returned by __builtin_apply. */
6924 case BUILT_IN_RETURN:
6925 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
6926 expand_builtin_return (expand_normal (CALL_EXPR_ARG (exp, 0)));
6927 return const0_rtx;
6929 case BUILT_IN_SAVEREGS:
6930 return expand_builtin_saveregs ();
6932 case BUILT_IN_VA_ARG_PACK:
6933 /* All valid uses of __builtin_va_arg_pack () are removed during
6934 inlining. */
6935 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
6936 return const0_rtx;
6938 case BUILT_IN_VA_ARG_PACK_LEN:
6939 /* All valid uses of __builtin_va_arg_pack_len () are removed during
6940 inlining. */
6941 error ("%Kinvalid use of %<__builtin_va_arg_pack_len ()%>", exp);
6942 return const0_rtx;
6944 /* Return the address of the first anonymous stack arg. */
6945 case BUILT_IN_NEXT_ARG:
6946 if (fold_builtin_next_arg (exp, false))
6947 return const0_rtx;
6948 return expand_builtin_next_arg ();
6950 case BUILT_IN_CLEAR_CACHE:
6951 target = expand_builtin___clear_cache (exp);
6952 if (target)
6953 return target;
6954 break;
6956 case BUILT_IN_CLASSIFY_TYPE:
6957 return expand_builtin_classify_type (exp);
6959 case BUILT_IN_CONSTANT_P:
6960 return const0_rtx;
6962 case BUILT_IN_FRAME_ADDRESS:
6963 case BUILT_IN_RETURN_ADDRESS:
6964 return expand_builtin_frame_address (fndecl, exp);
6966 /* Returns the address of the area where the structure is returned.
6967 0 otherwise. */
6968 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
6969 if (call_expr_nargs (exp) != 0
6970 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
6971 || !MEM_P (DECL_RTL (DECL_RESULT (current_function_decl))))
6972 return const0_rtx;
6973 else
6974 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
6976 CASE_BUILT_IN_ALLOCA:
6977 target = expand_builtin_alloca (exp);
6978 if (target)
6979 return target;
6980 break;
6982 case BUILT_IN_ASAN_ALLOCAS_UNPOISON:
6983 return expand_asan_emit_allocas_unpoison (exp);
6985 case BUILT_IN_STACK_SAVE:
6986 return expand_stack_save ();
6988 case BUILT_IN_STACK_RESTORE:
6989 expand_stack_restore (CALL_EXPR_ARG (exp, 0));
6990 return const0_rtx;
6992 case BUILT_IN_BSWAP16:
6993 case BUILT_IN_BSWAP32:
6994 case BUILT_IN_BSWAP64:
6995 target = expand_builtin_bswap (target_mode, exp, target, subtarget);
6996 if (target)
6997 return target;
6998 break;
7000 CASE_INT_FN (BUILT_IN_FFS):
7001 target = expand_builtin_unop (target_mode, exp, target,
7002 subtarget, ffs_optab);
7003 if (target)
7004 return target;
7005 break;
7007 CASE_INT_FN (BUILT_IN_CLZ):
7008 target = expand_builtin_unop (target_mode, exp, target,
7009 subtarget, clz_optab);
7010 if (target)
7011 return target;
7012 break;
7014 CASE_INT_FN (BUILT_IN_CTZ):
7015 target = expand_builtin_unop (target_mode, exp, target,
7016 subtarget, ctz_optab);
7017 if (target)
7018 return target;
7019 break;
7021 CASE_INT_FN (BUILT_IN_CLRSB):
7022 target = expand_builtin_unop (target_mode, exp, target,
7023 subtarget, clrsb_optab);
7024 if (target)
7025 return target;
7026 break;
7028 CASE_INT_FN (BUILT_IN_POPCOUNT):
7029 target = expand_builtin_unop (target_mode, exp, target,
7030 subtarget, popcount_optab);
7031 if (target)
7032 return target;
7033 break;
7035 CASE_INT_FN (BUILT_IN_PARITY):
7036 target = expand_builtin_unop (target_mode, exp, target,
7037 subtarget, parity_optab);
7038 if (target)
7039 return target;
7040 break;
7042 case BUILT_IN_STRLEN:
7043 target = expand_builtin_strlen (exp, target, target_mode);
7044 if (target)
7045 return target;
7046 break;
7048 case BUILT_IN_STRNLEN:
7049 target = expand_builtin_strnlen (exp, target, target_mode);
7050 if (target)
7051 return target;
7052 break;
7054 case BUILT_IN_STRCAT:
7055 target = expand_builtin_strcat (exp, target);
7056 if (target)
7057 return target;
7058 break;
7060 case BUILT_IN_STRCPY:
7061 target = expand_builtin_strcpy (exp, target);
7062 if (target)
7063 return target;
7064 break;
7066 case BUILT_IN_STRNCAT:
7067 target = expand_builtin_strncat (exp, target);
7068 if (target)
7069 return target;
7070 break;
7072 case BUILT_IN_STRNCPY:
7073 target = expand_builtin_strncpy (exp, target);
7074 if (target)
7075 return target;
7076 break;
7078 case BUILT_IN_STPCPY:
7079 target = expand_builtin_stpcpy (exp, target, mode);
7080 if (target)
7081 return target;
7082 break;
7084 case BUILT_IN_STPNCPY:
7085 target = expand_builtin_stpncpy (exp, target);
7086 if (target)
7087 return target;
7088 break;
7090 case BUILT_IN_MEMCHR:
7091 target = expand_builtin_memchr (exp, target);
7092 if (target)
7093 return target;
7094 break;
7096 case BUILT_IN_MEMCPY:
7097 target = expand_builtin_memcpy (exp, target);
7098 if (target)
7099 return target;
7100 break;
7102 case BUILT_IN_MEMMOVE:
7103 target = expand_builtin_memmove (exp, target);
7104 if (target)
7105 return target;
7106 break;
7108 case BUILT_IN_MEMPCPY:
7109 target = expand_builtin_mempcpy (exp, target);
7110 if (target)
7111 return target;
7112 break;
7114 case BUILT_IN_MEMSET:
7115 target = expand_builtin_memset (exp, target, mode);
7116 if (target)
7117 return target;
7118 break;
7120 case BUILT_IN_BZERO:
7121 target = expand_builtin_bzero (exp);
7122 if (target)
7123 return target;
7124 break;
7126 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7127 back to a BUILT_IN_STRCMP. Remember to delete the 3rd paramater
7128 when changing it to a strcmp call. */
7129 case BUILT_IN_STRCMP_EQ:
7130 target = expand_builtin_memcmp (exp, target, true);
7131 if (target)
7132 return target;
7134 /* Change this call back to a BUILT_IN_STRCMP. */
7135 TREE_OPERAND (exp, 1)
7136 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRCMP));
7138 /* Delete the last parameter. */
7139 unsigned int i;
7140 vec<tree, va_gc> *arg_vec;
7141 vec_alloc (arg_vec, 2);
7142 for (i = 0; i < 2; i++)
7143 arg_vec->quick_push (CALL_EXPR_ARG (exp, i));
7144 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), arg_vec);
7145 /* FALLTHROUGH */
7147 case BUILT_IN_STRCMP:
7148 target = expand_builtin_strcmp (exp, target);
7149 if (target)
7150 return target;
7151 break;
7153 /* Expand it as BUILT_IN_MEMCMP_EQ first. If not successful, change it
7154 back to a BUILT_IN_STRNCMP. */
7155 case BUILT_IN_STRNCMP_EQ:
7156 target = expand_builtin_memcmp (exp, target, true);
7157 if (target)
7158 return target;
7160 /* Change it back to a BUILT_IN_STRNCMP. */
7161 TREE_OPERAND (exp, 1)
7162 = build_fold_addr_expr (builtin_decl_explicit (BUILT_IN_STRNCMP));
7163 /* FALLTHROUGH */
7165 case BUILT_IN_STRNCMP:
7166 target = expand_builtin_strncmp (exp, target, mode);
7167 if (target)
7168 return target;
7169 break;
7171 case BUILT_IN_BCMP:
7172 case BUILT_IN_MEMCMP:
7173 case BUILT_IN_MEMCMP_EQ:
7174 target = expand_builtin_memcmp (exp, target, fcode == BUILT_IN_MEMCMP_EQ);
7175 if (target)
7176 return target;
7177 if (fcode == BUILT_IN_MEMCMP_EQ)
7179 tree newdecl = builtin_decl_explicit (BUILT_IN_MEMCMP);
7180 TREE_OPERAND (exp, 1) = build_fold_addr_expr (newdecl);
7182 break;
7184 case BUILT_IN_SETJMP:
7185 /* This should have been lowered to the builtins below. */
7186 gcc_unreachable ();
7188 case BUILT_IN_SETJMP_SETUP:
7189 /* __builtin_setjmp_setup is passed a pointer to an array of five words
7190 and the receiver label. */
7191 if (validate_arglist (exp, POINTER_TYPE, POINTER_TYPE, VOID_TYPE))
7193 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7194 VOIDmode, EXPAND_NORMAL);
7195 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 1), 0);
7196 rtx_insn *label_r = label_rtx (label);
7198 /* This is copied from the handling of non-local gotos. */
7199 expand_builtin_setjmp_setup (buf_addr, label_r);
7200 nonlocal_goto_handler_labels
7201 = gen_rtx_INSN_LIST (VOIDmode, label_r,
7202 nonlocal_goto_handler_labels);
7203 /* ??? Do not let expand_label treat us as such since we would
7204 not want to be both on the list of non-local labels and on
7205 the list of forced labels. */
7206 FORCED_LABEL (label) = 0;
7207 return const0_rtx;
7209 break;
7211 case BUILT_IN_SETJMP_RECEIVER:
7212 /* __builtin_setjmp_receiver is passed the receiver label. */
7213 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7215 tree label = TREE_OPERAND (CALL_EXPR_ARG (exp, 0), 0);
7216 rtx_insn *label_r = label_rtx (label);
7218 expand_builtin_setjmp_receiver (label_r);
7219 return const0_rtx;
7221 break;
7223 /* __builtin_longjmp is passed a pointer to an array of five words.
7224 It's similar to the C library longjmp function but works with
7225 __builtin_setjmp above. */
7226 case BUILT_IN_LONGJMP:
7227 if (validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
7229 rtx buf_addr = expand_expr (CALL_EXPR_ARG (exp, 0), subtarget,
7230 VOIDmode, EXPAND_NORMAL);
7231 rtx value = expand_normal (CALL_EXPR_ARG (exp, 1));
7233 if (value != const1_rtx)
7235 error ("%<__builtin_longjmp%> second argument must be 1");
7236 return const0_rtx;
7239 expand_builtin_longjmp (buf_addr, value);
7240 return const0_rtx;
7242 break;
7244 case BUILT_IN_NONLOCAL_GOTO:
7245 target = expand_builtin_nonlocal_goto (exp);
7246 if (target)
7247 return target;
7248 break;
7250 /* This updates the setjmp buffer that is its argument with the value
7251 of the current stack pointer. */
7252 case BUILT_IN_UPDATE_SETJMP_BUF:
7253 if (validate_arglist (exp, POINTER_TYPE, VOID_TYPE))
7255 rtx buf_addr
7256 = expand_normal (CALL_EXPR_ARG (exp, 0));
7258 expand_builtin_update_setjmp_buf (buf_addr);
7259 return const0_rtx;
7261 break;
7263 case BUILT_IN_TRAP:
7264 expand_builtin_trap ();
7265 return const0_rtx;
7267 case BUILT_IN_UNREACHABLE:
7268 expand_builtin_unreachable ();
7269 return const0_rtx;
7271 CASE_FLT_FN (BUILT_IN_SIGNBIT):
7272 case BUILT_IN_SIGNBITD32:
7273 case BUILT_IN_SIGNBITD64:
7274 case BUILT_IN_SIGNBITD128:
7275 target = expand_builtin_signbit (exp, target);
7276 if (target)
7277 return target;
7278 break;
7280 /* Various hooks for the DWARF 2 __throw routine. */
7281 case BUILT_IN_UNWIND_INIT:
7282 expand_builtin_unwind_init ();
7283 return const0_rtx;
7284 case BUILT_IN_DWARF_CFA:
7285 return virtual_cfa_rtx;
7286 #ifdef DWARF2_UNWIND_INFO
7287 case BUILT_IN_DWARF_SP_COLUMN:
7288 return expand_builtin_dwarf_sp_column ();
7289 case BUILT_IN_INIT_DWARF_REG_SIZES:
7290 expand_builtin_init_dwarf_reg_sizes (CALL_EXPR_ARG (exp, 0));
7291 return const0_rtx;
7292 #endif
7293 case BUILT_IN_FROB_RETURN_ADDR:
7294 return expand_builtin_frob_return_addr (CALL_EXPR_ARG (exp, 0));
7295 case BUILT_IN_EXTRACT_RETURN_ADDR:
7296 return expand_builtin_extract_return_addr (CALL_EXPR_ARG (exp, 0));
7297 case BUILT_IN_EH_RETURN:
7298 expand_builtin_eh_return (CALL_EXPR_ARG (exp, 0),
7299 CALL_EXPR_ARG (exp, 1));
7300 return const0_rtx;
7301 case BUILT_IN_EH_RETURN_DATA_REGNO:
7302 return expand_builtin_eh_return_data_regno (exp);
7303 case BUILT_IN_EXTEND_POINTER:
7304 return expand_builtin_extend_pointer (CALL_EXPR_ARG (exp, 0));
7305 case BUILT_IN_EH_POINTER:
7306 return expand_builtin_eh_pointer (exp);
7307 case BUILT_IN_EH_FILTER:
7308 return expand_builtin_eh_filter (exp);
7309 case BUILT_IN_EH_COPY_VALUES:
7310 return expand_builtin_eh_copy_values (exp);
7312 case BUILT_IN_VA_START:
7313 return expand_builtin_va_start (exp);
7314 case BUILT_IN_VA_END:
7315 return expand_builtin_va_end (exp);
7316 case BUILT_IN_VA_COPY:
7317 return expand_builtin_va_copy (exp);
7318 case BUILT_IN_EXPECT:
7319 return expand_builtin_expect (exp, target);
7320 case BUILT_IN_ASSUME_ALIGNED:
7321 return expand_builtin_assume_aligned (exp, target);
7322 case BUILT_IN_PREFETCH:
7323 expand_builtin_prefetch (exp);
7324 return const0_rtx;
7326 case BUILT_IN_INIT_TRAMPOLINE:
7327 return expand_builtin_init_trampoline (exp, true);
7328 case BUILT_IN_INIT_HEAP_TRAMPOLINE:
7329 return expand_builtin_init_trampoline (exp, false);
7330 case BUILT_IN_ADJUST_TRAMPOLINE:
7331 return expand_builtin_adjust_trampoline (exp);
7333 case BUILT_IN_INIT_DESCRIPTOR:
7334 return expand_builtin_init_descriptor (exp);
7335 case BUILT_IN_ADJUST_DESCRIPTOR:
7336 return expand_builtin_adjust_descriptor (exp);
7338 case BUILT_IN_FORK:
7339 case BUILT_IN_EXECL:
7340 case BUILT_IN_EXECV:
7341 case BUILT_IN_EXECLP:
7342 case BUILT_IN_EXECLE:
7343 case BUILT_IN_EXECVP:
7344 case BUILT_IN_EXECVE:
7345 target = expand_builtin_fork_or_exec (fndecl, exp, target, ignore);
7346 if (target)
7347 return target;
7348 break;
7350 case BUILT_IN_SYNC_FETCH_AND_ADD_1:
7351 case BUILT_IN_SYNC_FETCH_AND_ADD_2:
7352 case BUILT_IN_SYNC_FETCH_AND_ADD_4:
7353 case BUILT_IN_SYNC_FETCH_AND_ADD_8:
7354 case BUILT_IN_SYNC_FETCH_AND_ADD_16:
7355 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_ADD_1);
7356 target = expand_builtin_sync_operation (mode, exp, PLUS, false, target);
7357 if (target)
7358 return target;
7359 break;
7361 case BUILT_IN_SYNC_FETCH_AND_SUB_1:
7362 case BUILT_IN_SYNC_FETCH_AND_SUB_2:
7363 case BUILT_IN_SYNC_FETCH_AND_SUB_4:
7364 case BUILT_IN_SYNC_FETCH_AND_SUB_8:
7365 case BUILT_IN_SYNC_FETCH_AND_SUB_16:
7366 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_SUB_1);
7367 target = expand_builtin_sync_operation (mode, exp, MINUS, false, target);
7368 if (target)
7369 return target;
7370 break;
7372 case BUILT_IN_SYNC_FETCH_AND_OR_1:
7373 case BUILT_IN_SYNC_FETCH_AND_OR_2:
7374 case BUILT_IN_SYNC_FETCH_AND_OR_4:
7375 case BUILT_IN_SYNC_FETCH_AND_OR_8:
7376 case BUILT_IN_SYNC_FETCH_AND_OR_16:
7377 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_OR_1);
7378 target = expand_builtin_sync_operation (mode, exp, IOR, false, target);
7379 if (target)
7380 return target;
7381 break;
7383 case BUILT_IN_SYNC_FETCH_AND_AND_1:
7384 case BUILT_IN_SYNC_FETCH_AND_AND_2:
7385 case BUILT_IN_SYNC_FETCH_AND_AND_4:
7386 case BUILT_IN_SYNC_FETCH_AND_AND_8:
7387 case BUILT_IN_SYNC_FETCH_AND_AND_16:
7388 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_AND_1);
7389 target = expand_builtin_sync_operation (mode, exp, AND, false, target);
7390 if (target)
7391 return target;
7392 break;
7394 case BUILT_IN_SYNC_FETCH_AND_XOR_1:
7395 case BUILT_IN_SYNC_FETCH_AND_XOR_2:
7396 case BUILT_IN_SYNC_FETCH_AND_XOR_4:
7397 case BUILT_IN_SYNC_FETCH_AND_XOR_8:
7398 case BUILT_IN_SYNC_FETCH_AND_XOR_16:
7399 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_XOR_1);
7400 target = expand_builtin_sync_operation (mode, exp, XOR, false, target);
7401 if (target)
7402 return target;
7403 break;
7405 case BUILT_IN_SYNC_FETCH_AND_NAND_1:
7406 case BUILT_IN_SYNC_FETCH_AND_NAND_2:
7407 case BUILT_IN_SYNC_FETCH_AND_NAND_4:
7408 case BUILT_IN_SYNC_FETCH_AND_NAND_8:
7409 case BUILT_IN_SYNC_FETCH_AND_NAND_16:
7410 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_FETCH_AND_NAND_1);
7411 target = expand_builtin_sync_operation (mode, exp, NOT, false, target);
7412 if (target)
7413 return target;
7414 break;
7416 case BUILT_IN_SYNC_ADD_AND_FETCH_1:
7417 case BUILT_IN_SYNC_ADD_AND_FETCH_2:
7418 case BUILT_IN_SYNC_ADD_AND_FETCH_4:
7419 case BUILT_IN_SYNC_ADD_AND_FETCH_8:
7420 case BUILT_IN_SYNC_ADD_AND_FETCH_16:
7421 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_ADD_AND_FETCH_1);
7422 target = expand_builtin_sync_operation (mode, exp, PLUS, true, target);
7423 if (target)
7424 return target;
7425 break;
7427 case BUILT_IN_SYNC_SUB_AND_FETCH_1:
7428 case BUILT_IN_SYNC_SUB_AND_FETCH_2:
7429 case BUILT_IN_SYNC_SUB_AND_FETCH_4:
7430 case BUILT_IN_SYNC_SUB_AND_FETCH_8:
7431 case BUILT_IN_SYNC_SUB_AND_FETCH_16:
7432 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_SUB_AND_FETCH_1);
7433 target = expand_builtin_sync_operation (mode, exp, MINUS, true, target);
7434 if (target)
7435 return target;
7436 break;
7438 case BUILT_IN_SYNC_OR_AND_FETCH_1:
7439 case BUILT_IN_SYNC_OR_AND_FETCH_2:
7440 case BUILT_IN_SYNC_OR_AND_FETCH_4:
7441 case BUILT_IN_SYNC_OR_AND_FETCH_8:
7442 case BUILT_IN_SYNC_OR_AND_FETCH_16:
7443 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_OR_AND_FETCH_1);
7444 target = expand_builtin_sync_operation (mode, exp, IOR, true, target);
7445 if (target)
7446 return target;
7447 break;
7449 case BUILT_IN_SYNC_AND_AND_FETCH_1:
7450 case BUILT_IN_SYNC_AND_AND_FETCH_2:
7451 case BUILT_IN_SYNC_AND_AND_FETCH_4:
7452 case BUILT_IN_SYNC_AND_AND_FETCH_8:
7453 case BUILT_IN_SYNC_AND_AND_FETCH_16:
7454 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_AND_AND_FETCH_1);
7455 target = expand_builtin_sync_operation (mode, exp, AND, true, target);
7456 if (target)
7457 return target;
7458 break;
7460 case BUILT_IN_SYNC_XOR_AND_FETCH_1:
7461 case BUILT_IN_SYNC_XOR_AND_FETCH_2:
7462 case BUILT_IN_SYNC_XOR_AND_FETCH_4:
7463 case BUILT_IN_SYNC_XOR_AND_FETCH_8:
7464 case BUILT_IN_SYNC_XOR_AND_FETCH_16:
7465 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_XOR_AND_FETCH_1);
7466 target = expand_builtin_sync_operation (mode, exp, XOR, true, target);
7467 if (target)
7468 return target;
7469 break;
7471 case BUILT_IN_SYNC_NAND_AND_FETCH_1:
7472 case BUILT_IN_SYNC_NAND_AND_FETCH_2:
7473 case BUILT_IN_SYNC_NAND_AND_FETCH_4:
7474 case BUILT_IN_SYNC_NAND_AND_FETCH_8:
7475 case BUILT_IN_SYNC_NAND_AND_FETCH_16:
7476 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_NAND_AND_FETCH_1);
7477 target = expand_builtin_sync_operation (mode, exp, NOT, true, target);
7478 if (target)
7479 return target;
7480 break;
7482 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
7483 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
7484 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
7485 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
7486 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
7487 if (mode == VOIDmode)
7488 mode = TYPE_MODE (boolean_type_node);
7489 if (!target || !register_operand (target, mode))
7490 target = gen_reg_rtx (mode);
7492 mode = get_builtin_sync_mode
7493 (fcode - BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1);
7494 target = expand_builtin_compare_and_swap (mode, exp, true, target);
7495 if (target)
7496 return target;
7497 break;
7499 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1:
7500 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_2:
7501 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_4:
7502 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_8:
7503 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_16:
7504 mode = get_builtin_sync_mode
7505 (fcode - BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_1);
7506 target = expand_builtin_compare_and_swap (mode, exp, false, target);
7507 if (target)
7508 return target;
7509 break;
7511 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_1:
7512 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_2:
7513 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_4:
7514 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_8:
7515 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_16:
7516 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_TEST_AND_SET_1);
7517 target = expand_builtin_sync_lock_test_and_set (mode, exp, target);
7518 if (target)
7519 return target;
7520 break;
7522 case BUILT_IN_SYNC_LOCK_RELEASE_1:
7523 case BUILT_IN_SYNC_LOCK_RELEASE_2:
7524 case BUILT_IN_SYNC_LOCK_RELEASE_4:
7525 case BUILT_IN_SYNC_LOCK_RELEASE_8:
7526 case BUILT_IN_SYNC_LOCK_RELEASE_16:
7527 mode = get_builtin_sync_mode (fcode - BUILT_IN_SYNC_LOCK_RELEASE_1);
7528 expand_builtin_sync_lock_release (mode, exp);
7529 return const0_rtx;
7531 case BUILT_IN_SYNC_SYNCHRONIZE:
7532 expand_builtin_sync_synchronize ();
7533 return const0_rtx;
7535 case BUILT_IN_ATOMIC_EXCHANGE_1:
7536 case BUILT_IN_ATOMIC_EXCHANGE_2:
7537 case BUILT_IN_ATOMIC_EXCHANGE_4:
7538 case BUILT_IN_ATOMIC_EXCHANGE_8:
7539 case BUILT_IN_ATOMIC_EXCHANGE_16:
7540 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_EXCHANGE_1);
7541 target = expand_builtin_atomic_exchange (mode, exp, target);
7542 if (target)
7543 return target;
7544 break;
7546 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
7547 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
7548 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
7549 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
7550 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
7552 unsigned int nargs, z;
7553 vec<tree, va_gc> *vec;
7555 mode =
7556 get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1);
7557 target = expand_builtin_atomic_compare_exchange (mode, exp, target);
7558 if (target)
7559 return target;
7561 /* If this is turned into an external library call, the weak parameter
7562 must be dropped to match the expected parameter list. */
7563 nargs = call_expr_nargs (exp);
7564 vec_alloc (vec, nargs - 1);
7565 for (z = 0; z < 3; z++)
7566 vec->quick_push (CALL_EXPR_ARG (exp, z));
7567 /* Skip the boolean weak parameter. */
7568 for (z = 4; z < 6; z++)
7569 vec->quick_push (CALL_EXPR_ARG (exp, z));
7570 exp = build_call_vec (TREE_TYPE (exp), CALL_EXPR_FN (exp), vec);
7571 break;
7574 case BUILT_IN_ATOMIC_LOAD_1:
7575 case BUILT_IN_ATOMIC_LOAD_2:
7576 case BUILT_IN_ATOMIC_LOAD_4:
7577 case BUILT_IN_ATOMIC_LOAD_8:
7578 case BUILT_IN_ATOMIC_LOAD_16:
7579 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_LOAD_1);
7580 target = expand_builtin_atomic_load (mode, exp, target);
7581 if (target)
7582 return target;
7583 break;
7585 case BUILT_IN_ATOMIC_STORE_1:
7586 case BUILT_IN_ATOMIC_STORE_2:
7587 case BUILT_IN_ATOMIC_STORE_4:
7588 case BUILT_IN_ATOMIC_STORE_8:
7589 case BUILT_IN_ATOMIC_STORE_16:
7590 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_STORE_1);
7591 target = expand_builtin_atomic_store (mode, exp);
7592 if (target)
7593 return const0_rtx;
7594 break;
7596 case BUILT_IN_ATOMIC_ADD_FETCH_1:
7597 case BUILT_IN_ATOMIC_ADD_FETCH_2:
7598 case BUILT_IN_ATOMIC_ADD_FETCH_4:
7599 case BUILT_IN_ATOMIC_ADD_FETCH_8:
7600 case BUILT_IN_ATOMIC_ADD_FETCH_16:
7602 enum built_in_function lib;
7603 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1);
7604 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_ADD_1 +
7605 (fcode - BUILT_IN_ATOMIC_ADD_FETCH_1));
7606 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, true,
7607 ignore, lib);
7608 if (target)
7609 return target;
7610 break;
7612 case BUILT_IN_ATOMIC_SUB_FETCH_1:
7613 case BUILT_IN_ATOMIC_SUB_FETCH_2:
7614 case BUILT_IN_ATOMIC_SUB_FETCH_4:
7615 case BUILT_IN_ATOMIC_SUB_FETCH_8:
7616 case BUILT_IN_ATOMIC_SUB_FETCH_16:
7618 enum built_in_function lib;
7619 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1);
7620 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_SUB_1 +
7621 (fcode - BUILT_IN_ATOMIC_SUB_FETCH_1));
7622 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, true,
7623 ignore, lib);
7624 if (target)
7625 return target;
7626 break;
7628 case BUILT_IN_ATOMIC_AND_FETCH_1:
7629 case BUILT_IN_ATOMIC_AND_FETCH_2:
7630 case BUILT_IN_ATOMIC_AND_FETCH_4:
7631 case BUILT_IN_ATOMIC_AND_FETCH_8:
7632 case BUILT_IN_ATOMIC_AND_FETCH_16:
7634 enum built_in_function lib;
7635 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_AND_FETCH_1);
7636 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_AND_1 +
7637 (fcode - BUILT_IN_ATOMIC_AND_FETCH_1));
7638 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, true,
7639 ignore, lib);
7640 if (target)
7641 return target;
7642 break;
7644 case BUILT_IN_ATOMIC_NAND_FETCH_1:
7645 case BUILT_IN_ATOMIC_NAND_FETCH_2:
7646 case BUILT_IN_ATOMIC_NAND_FETCH_4:
7647 case BUILT_IN_ATOMIC_NAND_FETCH_8:
7648 case BUILT_IN_ATOMIC_NAND_FETCH_16:
7650 enum built_in_function lib;
7651 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1);
7652 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_NAND_1 +
7653 (fcode - BUILT_IN_ATOMIC_NAND_FETCH_1));
7654 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, true,
7655 ignore, lib);
7656 if (target)
7657 return target;
7658 break;
7660 case BUILT_IN_ATOMIC_XOR_FETCH_1:
7661 case BUILT_IN_ATOMIC_XOR_FETCH_2:
7662 case BUILT_IN_ATOMIC_XOR_FETCH_4:
7663 case BUILT_IN_ATOMIC_XOR_FETCH_8:
7664 case BUILT_IN_ATOMIC_XOR_FETCH_16:
7666 enum built_in_function lib;
7667 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1);
7668 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_XOR_1 +
7669 (fcode - BUILT_IN_ATOMIC_XOR_FETCH_1));
7670 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, true,
7671 ignore, lib);
7672 if (target)
7673 return target;
7674 break;
7676 case BUILT_IN_ATOMIC_OR_FETCH_1:
7677 case BUILT_IN_ATOMIC_OR_FETCH_2:
7678 case BUILT_IN_ATOMIC_OR_FETCH_4:
7679 case BUILT_IN_ATOMIC_OR_FETCH_8:
7680 case BUILT_IN_ATOMIC_OR_FETCH_16:
7682 enum built_in_function lib;
7683 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_OR_FETCH_1);
7684 lib = (enum built_in_function)((int)BUILT_IN_ATOMIC_FETCH_OR_1 +
7685 (fcode - BUILT_IN_ATOMIC_OR_FETCH_1));
7686 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, true,
7687 ignore, lib);
7688 if (target)
7689 return target;
7690 break;
7692 case BUILT_IN_ATOMIC_FETCH_ADD_1:
7693 case BUILT_IN_ATOMIC_FETCH_ADD_2:
7694 case BUILT_IN_ATOMIC_FETCH_ADD_4:
7695 case BUILT_IN_ATOMIC_FETCH_ADD_8:
7696 case BUILT_IN_ATOMIC_FETCH_ADD_16:
7697 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_ADD_1);
7698 target = expand_builtin_atomic_fetch_op (mode, exp, target, PLUS, false,
7699 ignore, BUILT_IN_NONE);
7700 if (target)
7701 return target;
7702 break;
7704 case BUILT_IN_ATOMIC_FETCH_SUB_1:
7705 case BUILT_IN_ATOMIC_FETCH_SUB_2:
7706 case BUILT_IN_ATOMIC_FETCH_SUB_4:
7707 case BUILT_IN_ATOMIC_FETCH_SUB_8:
7708 case BUILT_IN_ATOMIC_FETCH_SUB_16:
7709 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_SUB_1);
7710 target = expand_builtin_atomic_fetch_op (mode, exp, target, MINUS, false,
7711 ignore, BUILT_IN_NONE);
7712 if (target)
7713 return target;
7714 break;
7716 case BUILT_IN_ATOMIC_FETCH_AND_1:
7717 case BUILT_IN_ATOMIC_FETCH_AND_2:
7718 case BUILT_IN_ATOMIC_FETCH_AND_4:
7719 case BUILT_IN_ATOMIC_FETCH_AND_8:
7720 case BUILT_IN_ATOMIC_FETCH_AND_16:
7721 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_AND_1);
7722 target = expand_builtin_atomic_fetch_op (mode, exp, target, AND, false,
7723 ignore, BUILT_IN_NONE);
7724 if (target)
7725 return target;
7726 break;
7728 case BUILT_IN_ATOMIC_FETCH_NAND_1:
7729 case BUILT_IN_ATOMIC_FETCH_NAND_2:
7730 case BUILT_IN_ATOMIC_FETCH_NAND_4:
7731 case BUILT_IN_ATOMIC_FETCH_NAND_8:
7732 case BUILT_IN_ATOMIC_FETCH_NAND_16:
7733 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_NAND_1);
7734 target = expand_builtin_atomic_fetch_op (mode, exp, target, NOT, false,
7735 ignore, BUILT_IN_NONE);
7736 if (target)
7737 return target;
7738 break;
7740 case BUILT_IN_ATOMIC_FETCH_XOR_1:
7741 case BUILT_IN_ATOMIC_FETCH_XOR_2:
7742 case BUILT_IN_ATOMIC_FETCH_XOR_4:
7743 case BUILT_IN_ATOMIC_FETCH_XOR_8:
7744 case BUILT_IN_ATOMIC_FETCH_XOR_16:
7745 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_XOR_1);
7746 target = expand_builtin_atomic_fetch_op (mode, exp, target, XOR, false,
7747 ignore, BUILT_IN_NONE);
7748 if (target)
7749 return target;
7750 break;
7752 case BUILT_IN_ATOMIC_FETCH_OR_1:
7753 case BUILT_IN_ATOMIC_FETCH_OR_2:
7754 case BUILT_IN_ATOMIC_FETCH_OR_4:
7755 case BUILT_IN_ATOMIC_FETCH_OR_8:
7756 case BUILT_IN_ATOMIC_FETCH_OR_16:
7757 mode = get_builtin_sync_mode (fcode - BUILT_IN_ATOMIC_FETCH_OR_1);
7758 target = expand_builtin_atomic_fetch_op (mode, exp, target, IOR, false,
7759 ignore, BUILT_IN_NONE);
7760 if (target)
7761 return target;
7762 break;
7764 case BUILT_IN_ATOMIC_TEST_AND_SET:
7765 return expand_builtin_atomic_test_and_set (exp, target);
7767 case BUILT_IN_ATOMIC_CLEAR:
7768 return expand_builtin_atomic_clear (exp);
7770 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
7771 return expand_builtin_atomic_always_lock_free (exp);
7773 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
7774 target = expand_builtin_atomic_is_lock_free (exp);
7775 if (target)
7776 return target;
7777 break;
7779 case BUILT_IN_ATOMIC_THREAD_FENCE:
7780 expand_builtin_atomic_thread_fence (exp);
7781 return const0_rtx;
7783 case BUILT_IN_ATOMIC_SIGNAL_FENCE:
7784 expand_builtin_atomic_signal_fence (exp);
7785 return const0_rtx;
7787 case BUILT_IN_OBJECT_SIZE:
7788 return expand_builtin_object_size (exp);
7790 case BUILT_IN_MEMCPY_CHK:
7791 case BUILT_IN_MEMPCPY_CHK:
7792 case BUILT_IN_MEMMOVE_CHK:
7793 case BUILT_IN_MEMSET_CHK:
7794 target = expand_builtin_memory_chk (exp, target, mode, fcode);
7795 if (target)
7796 return target;
7797 break;
7799 case BUILT_IN_STRCPY_CHK:
7800 case BUILT_IN_STPCPY_CHK:
7801 case BUILT_IN_STRNCPY_CHK:
7802 case BUILT_IN_STPNCPY_CHK:
7803 case BUILT_IN_STRCAT_CHK:
7804 case BUILT_IN_STRNCAT_CHK:
7805 case BUILT_IN_SNPRINTF_CHK:
7806 case BUILT_IN_VSNPRINTF_CHK:
7807 maybe_emit_chk_warning (exp, fcode);
7808 break;
7810 case BUILT_IN_SPRINTF_CHK:
7811 case BUILT_IN_VSPRINTF_CHK:
7812 maybe_emit_sprintf_chk_warning (exp, fcode);
7813 break;
7815 case BUILT_IN_FREE:
7816 if (warn_free_nonheap_object)
7817 maybe_emit_free_warning (exp);
7818 break;
7820 case BUILT_IN_THREAD_POINTER:
7821 return expand_builtin_thread_pointer (exp, target);
7823 case BUILT_IN_SET_THREAD_POINTER:
7824 expand_builtin_set_thread_pointer (exp);
7825 return const0_rtx;
7827 case BUILT_IN_ACC_ON_DEVICE:
7828 /* Do library call, if we failed to expand the builtin when
7829 folding. */
7830 break;
7832 case BUILT_IN_GOACC_PARLEVEL_ID:
7833 case BUILT_IN_GOACC_PARLEVEL_SIZE:
7834 return expand_builtin_goacc_parlevel_id_size (exp, target, ignore);
7836 default: /* just do library call, if unknown builtin */
7837 break;
7840 /* The switch statement above can drop through to cause the function
7841 to be called normally. */
7842 return expand_call (exp, target, ignore);
7845 /* Determine whether a tree node represents a call to a built-in
7846 function. If the tree T is a call to a built-in function with
7847 the right number of arguments of the appropriate types, return
7848 the DECL_FUNCTION_CODE of the call, e.g. BUILT_IN_SQRT.
7849 Otherwise the return value is END_BUILTINS. */
7851 enum built_in_function
7852 builtin_mathfn_code (const_tree t)
7854 const_tree fndecl, arg, parmlist;
7855 const_tree argtype, parmtype;
7856 const_call_expr_arg_iterator iter;
7858 if (TREE_CODE (t) != CALL_EXPR)
7859 return END_BUILTINS;
7861 fndecl = get_callee_fndecl (t);
7862 if (fndecl == NULL_TREE
7863 || TREE_CODE (fndecl) != FUNCTION_DECL
7864 || ! DECL_BUILT_IN (fndecl)
7865 || DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
7866 return END_BUILTINS;
7868 parmlist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
7869 init_const_call_expr_arg_iterator (t, &iter);
7870 for (; parmlist; parmlist = TREE_CHAIN (parmlist))
7872 /* If a function doesn't take a variable number of arguments,
7873 the last element in the list will have type `void'. */
7874 parmtype = TREE_VALUE (parmlist);
7875 if (VOID_TYPE_P (parmtype))
7877 if (more_const_call_expr_args_p (&iter))
7878 return END_BUILTINS;
7879 return DECL_FUNCTION_CODE (fndecl);
7882 if (! more_const_call_expr_args_p (&iter))
7883 return END_BUILTINS;
7885 arg = next_const_call_expr_arg (&iter);
7886 argtype = TREE_TYPE (arg);
7888 if (SCALAR_FLOAT_TYPE_P (parmtype))
7890 if (! SCALAR_FLOAT_TYPE_P (argtype))
7891 return END_BUILTINS;
7893 else if (COMPLEX_FLOAT_TYPE_P (parmtype))
7895 if (! COMPLEX_FLOAT_TYPE_P (argtype))
7896 return END_BUILTINS;
7898 else if (POINTER_TYPE_P (parmtype))
7900 if (! POINTER_TYPE_P (argtype))
7901 return END_BUILTINS;
7903 else if (INTEGRAL_TYPE_P (parmtype))
7905 if (! INTEGRAL_TYPE_P (argtype))
7906 return END_BUILTINS;
7908 else
7909 return END_BUILTINS;
7912 /* Variable-length argument list. */
7913 return DECL_FUNCTION_CODE (fndecl);
7916 /* Fold a call to __builtin_constant_p, if we know its argument ARG will
7917 evaluate to a constant. */
7919 static tree
7920 fold_builtin_constant_p (tree arg)
7922 /* We return 1 for a numeric type that's known to be a constant
7923 value at compile-time or for an aggregate type that's a
7924 literal constant. */
7925 STRIP_NOPS (arg);
7927 /* If we know this is a constant, emit the constant of one. */
7928 if (CONSTANT_CLASS_P (arg)
7929 || (TREE_CODE (arg) == CONSTRUCTOR
7930 && TREE_CONSTANT (arg)))
7931 return integer_one_node;
7932 if (TREE_CODE (arg) == ADDR_EXPR)
7934 tree op = TREE_OPERAND (arg, 0);
7935 if (TREE_CODE (op) == STRING_CST
7936 || (TREE_CODE (op) == ARRAY_REF
7937 && integer_zerop (TREE_OPERAND (op, 1))
7938 && TREE_CODE (TREE_OPERAND (op, 0)) == STRING_CST))
7939 return integer_one_node;
7942 /* If this expression has side effects, show we don't know it to be a
7943 constant. Likewise if it's a pointer or aggregate type since in
7944 those case we only want literals, since those are only optimized
7945 when generating RTL, not later.
7946 And finally, if we are compiling an initializer, not code, we
7947 need to return a definite result now; there's not going to be any
7948 more optimization done. */
7949 if (TREE_SIDE_EFFECTS (arg)
7950 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
7951 || POINTER_TYPE_P (TREE_TYPE (arg))
7952 || cfun == 0
7953 || folding_initializer
7954 || force_folding_builtin_constant_p)
7955 return integer_zero_node;
7957 return NULL_TREE;
7960 /* Create builtin_expect with PRED and EXPECTED as its arguments and
7961 return it as a truthvalue. */
7963 static tree
7964 build_builtin_expect_predicate (location_t loc, tree pred, tree expected,
7965 tree predictor)
7967 tree fn, arg_types, pred_type, expected_type, call_expr, ret_type;
7969 fn = builtin_decl_explicit (BUILT_IN_EXPECT);
7970 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
7971 ret_type = TREE_TYPE (TREE_TYPE (fn));
7972 pred_type = TREE_VALUE (arg_types);
7973 expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
7975 pred = fold_convert_loc (loc, pred_type, pred);
7976 expected = fold_convert_loc (loc, expected_type, expected);
7977 call_expr = build_call_expr_loc (loc, fn, predictor ? 3 : 2, pred, expected,
7978 predictor);
7980 return build2 (NE_EXPR, TREE_TYPE (pred), call_expr,
7981 build_int_cst (ret_type, 0));
7984 /* Fold a call to builtin_expect with arguments ARG0 and ARG1. Return
7985 NULL_TREE if no simplification is possible. */
7987 tree
7988 fold_builtin_expect (location_t loc, tree arg0, tree arg1, tree arg2)
7990 tree inner, fndecl, inner_arg0;
7991 enum tree_code code;
7993 /* Distribute the expected value over short-circuiting operators.
7994 See through the cast from truthvalue_type_node to long. */
7995 inner_arg0 = arg0;
7996 while (CONVERT_EXPR_P (inner_arg0)
7997 && INTEGRAL_TYPE_P (TREE_TYPE (inner_arg0))
7998 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (inner_arg0, 0))))
7999 inner_arg0 = TREE_OPERAND (inner_arg0, 0);
8001 /* If this is a builtin_expect within a builtin_expect keep the
8002 inner one. See through a comparison against a constant. It
8003 might have been added to create a thruthvalue. */
8004 inner = inner_arg0;
8006 if (COMPARISON_CLASS_P (inner)
8007 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
8008 inner = TREE_OPERAND (inner, 0);
8010 if (TREE_CODE (inner) == CALL_EXPR
8011 && (fndecl = get_callee_fndecl (inner))
8012 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
8013 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT)
8014 return arg0;
8016 inner = inner_arg0;
8017 code = TREE_CODE (inner);
8018 if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
8020 tree op0 = TREE_OPERAND (inner, 0);
8021 tree op1 = TREE_OPERAND (inner, 1);
8022 arg1 = save_expr (arg1);
8024 op0 = build_builtin_expect_predicate (loc, op0, arg1, arg2);
8025 op1 = build_builtin_expect_predicate (loc, op1, arg1, arg2);
8026 inner = build2 (code, TREE_TYPE (inner), op0, op1);
8028 return fold_convert_loc (loc, TREE_TYPE (arg0), inner);
8031 /* If the argument isn't invariant then there's nothing else we can do. */
8032 if (!TREE_CONSTANT (inner_arg0))
8033 return NULL_TREE;
8035 /* If we expect that a comparison against the argument will fold to
8036 a constant return the constant. In practice, this means a true
8037 constant or the address of a non-weak symbol. */
8038 inner = inner_arg0;
8039 STRIP_NOPS (inner);
8040 if (TREE_CODE (inner) == ADDR_EXPR)
8044 inner = TREE_OPERAND (inner, 0);
8046 while (TREE_CODE (inner) == COMPONENT_REF
8047 || TREE_CODE (inner) == ARRAY_REF);
8048 if (VAR_OR_FUNCTION_DECL_P (inner) && DECL_WEAK (inner))
8049 return NULL_TREE;
8052 /* Otherwise, ARG0 already has the proper type for the return value. */
8053 return arg0;
8056 /* Fold a call to __builtin_classify_type with argument ARG. */
8058 static tree
8059 fold_builtin_classify_type (tree arg)
8061 if (arg == 0)
8062 return build_int_cst (integer_type_node, no_type_class);
8064 return build_int_cst (integer_type_node, type_to_class (TREE_TYPE (arg)));
8067 /* Fold a call to __builtin_strlen with argument ARG. */
8069 static tree
8070 fold_builtin_strlen (location_t loc, tree type, tree arg)
8072 if (!validate_arg (arg, POINTER_TYPE))
8073 return NULL_TREE;
8074 else
8076 tree len = c_strlen (arg, 0);
8078 if (len)
8079 return fold_convert_loc (loc, type, len);
8081 return NULL_TREE;
8085 /* Fold a call to __builtin_inf or __builtin_huge_val. */
8087 static tree
8088 fold_builtin_inf (location_t loc, tree type, int warn)
8090 REAL_VALUE_TYPE real;
8092 /* __builtin_inff is intended to be usable to define INFINITY on all
8093 targets. If an infinity is not available, INFINITY expands "to a
8094 positive constant of type float that overflows at translation
8095 time", footnote "In this case, using INFINITY will violate the
8096 constraint in 6.4.4 and thus require a diagnostic." (C99 7.12#4).
8097 Thus we pedwarn to ensure this constraint violation is
8098 diagnosed. */
8099 if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
8100 pedwarn (loc, 0, "target format does not support infinity");
8102 real_inf (&real);
8103 return build_real (type, real);
8106 /* Fold function call to builtin sincos, sincosf, or sincosl. Return
8107 NULL_TREE if no simplification can be made. */
8109 static tree
8110 fold_builtin_sincos (location_t loc,
8111 tree arg0, tree arg1, tree arg2)
8113 tree type;
8114 tree fndecl, call = NULL_TREE;
8116 if (!validate_arg (arg0, REAL_TYPE)
8117 || !validate_arg (arg1, POINTER_TYPE)
8118 || !validate_arg (arg2, POINTER_TYPE))
8119 return NULL_TREE;
8121 type = TREE_TYPE (arg0);
8123 /* Calculate the result when the argument is a constant. */
8124 built_in_function fn = mathfn_built_in_2 (type, CFN_BUILT_IN_CEXPI);
8125 if (fn == END_BUILTINS)
8126 return NULL_TREE;
8128 /* Canonicalize sincos to cexpi. */
8129 if (TREE_CODE (arg0) == REAL_CST)
8131 tree complex_type = build_complex_type (type);
8132 call = fold_const_call (as_combined_fn (fn), complex_type, arg0);
8134 if (!call)
8136 if (!targetm.libc_has_function (function_c99_math_complex)
8137 || !builtin_decl_implicit_p (fn))
8138 return NULL_TREE;
8139 fndecl = builtin_decl_explicit (fn);
8140 call = build_call_expr_loc (loc, fndecl, 1, arg0);
8141 call = builtin_save_expr (call);
8144 tree ptype = build_pointer_type (type);
8145 arg1 = fold_convert (ptype, arg1);
8146 arg2 = fold_convert (ptype, arg2);
8147 return build2 (COMPOUND_EXPR, void_type_node,
8148 build2 (MODIFY_EXPR, void_type_node,
8149 build_fold_indirect_ref_loc (loc, arg1),
8150 fold_build1_loc (loc, IMAGPART_EXPR, type, call)),
8151 build2 (MODIFY_EXPR, void_type_node,
8152 build_fold_indirect_ref_loc (loc, arg2),
8153 fold_build1_loc (loc, REALPART_EXPR, type, call)));
8156 /* Fold function call to builtin memcmp with arguments ARG1 and ARG2.
8157 Return NULL_TREE if no simplification can be made. */
8159 static tree
8160 fold_builtin_memcmp (location_t loc, tree arg1, tree arg2, tree len)
8162 if (!validate_arg (arg1, POINTER_TYPE)
8163 || !validate_arg (arg2, POINTER_TYPE)
8164 || !validate_arg (len, INTEGER_TYPE))
8165 return NULL_TREE;
8167 /* If the LEN parameter is zero, return zero. */
8168 if (integer_zerop (len))
8169 return omit_two_operands_loc (loc, integer_type_node, integer_zero_node,
8170 arg1, arg2);
8172 /* If ARG1 and ARG2 are the same (and not volatile), return zero. */
8173 if (operand_equal_p (arg1, arg2, 0))
8174 return omit_one_operand_loc (loc, integer_type_node, integer_zero_node, len);
8176 /* If len parameter is one, return an expression corresponding to
8177 (*(const unsigned char*)arg1 - (const unsigned char*)arg2). */
8178 if (tree_fits_uhwi_p (len) && tree_to_uhwi (len) == 1)
8180 tree cst_uchar_node = build_type_variant (unsigned_char_type_node, 1, 0);
8181 tree cst_uchar_ptr_node
8182 = build_pointer_type_for_mode (cst_uchar_node, ptr_mode, true);
8184 tree ind1
8185 = fold_convert_loc (loc, integer_type_node,
8186 build1 (INDIRECT_REF, cst_uchar_node,
8187 fold_convert_loc (loc,
8188 cst_uchar_ptr_node,
8189 arg1)));
8190 tree ind2
8191 = fold_convert_loc (loc, integer_type_node,
8192 build1 (INDIRECT_REF, cst_uchar_node,
8193 fold_convert_loc (loc,
8194 cst_uchar_ptr_node,
8195 arg2)));
8196 return fold_build2_loc (loc, MINUS_EXPR, integer_type_node, ind1, ind2);
8199 return NULL_TREE;
8202 /* Fold a call to builtin isascii with argument ARG. */
8204 static tree
8205 fold_builtin_isascii (location_t loc, tree arg)
8207 if (!validate_arg (arg, INTEGER_TYPE))
8208 return NULL_TREE;
8209 else
8211 /* Transform isascii(c) -> ((c & ~0x7f) == 0). */
8212 arg = fold_build2 (BIT_AND_EXPR, integer_type_node, arg,
8213 build_int_cst (integer_type_node,
8214 ~ (unsigned HOST_WIDE_INT) 0x7f));
8215 return fold_build2_loc (loc, EQ_EXPR, integer_type_node,
8216 arg, integer_zero_node);
8220 /* Fold a call to builtin toascii with argument ARG. */
8222 static tree
8223 fold_builtin_toascii (location_t loc, tree arg)
8225 if (!validate_arg (arg, INTEGER_TYPE))
8226 return NULL_TREE;
8228 /* Transform toascii(c) -> (c & 0x7f). */
8229 return fold_build2_loc (loc, BIT_AND_EXPR, integer_type_node, arg,
8230 build_int_cst (integer_type_node, 0x7f));
8233 /* Fold a call to builtin isdigit with argument ARG. */
8235 static tree
8236 fold_builtin_isdigit (location_t loc, tree arg)
8238 if (!validate_arg (arg, INTEGER_TYPE))
8239 return NULL_TREE;
8240 else
8242 /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */
8243 /* According to the C standard, isdigit is unaffected by locale.
8244 However, it definitely is affected by the target character set. */
8245 unsigned HOST_WIDE_INT target_digit0
8246 = lang_hooks.to_target_charset ('0');
8248 if (target_digit0 == 0)
8249 return NULL_TREE;
8251 arg = fold_convert_loc (loc, unsigned_type_node, arg);
8252 arg = fold_build2 (MINUS_EXPR, unsigned_type_node, arg,
8253 build_int_cst (unsigned_type_node, target_digit0));
8254 return fold_build2_loc (loc, LE_EXPR, integer_type_node, arg,
8255 build_int_cst (unsigned_type_node, 9));
8259 /* Fold a call to fabs, fabsf or fabsl with argument ARG. */
8261 static tree
8262 fold_builtin_fabs (location_t loc, tree arg, tree type)
8264 if (!validate_arg (arg, REAL_TYPE))
8265 return NULL_TREE;
8267 arg = fold_convert_loc (loc, type, arg);
8268 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8271 /* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
8273 static tree
8274 fold_builtin_abs (location_t loc, tree arg, tree type)
8276 if (!validate_arg (arg, INTEGER_TYPE))
8277 return NULL_TREE;
8279 arg = fold_convert_loc (loc, type, arg);
8280 return fold_build1_loc (loc, ABS_EXPR, type, arg);
8283 /* Fold a call to builtin carg(a+bi) -> atan2(b,a). */
8285 static tree
8286 fold_builtin_carg (location_t loc, tree arg, tree type)
8288 if (validate_arg (arg, COMPLEX_TYPE)
8289 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
8291 tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);
8293 if (atan2_fn)
8295 tree new_arg = builtin_save_expr (arg);
8296 tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
8297 tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
8298 return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
8302 return NULL_TREE;
8305 /* Fold a call to builtin frexp, we can assume the base is 2. */
8307 static tree
8308 fold_builtin_frexp (location_t loc, tree arg0, tree arg1, tree rettype)
8310 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8311 return NULL_TREE;
8313 STRIP_NOPS (arg0);
8315 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8316 return NULL_TREE;
8318 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8320 /* Proceed if a valid pointer type was passed in. */
8321 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == integer_type_node)
8323 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8324 tree frac, exp;
8326 switch (value->cl)
8328 case rvc_zero:
8329 /* For +-0, return (*exp = 0, +-0). */
8330 exp = integer_zero_node;
8331 frac = arg0;
8332 break;
8333 case rvc_nan:
8334 case rvc_inf:
8335 /* For +-NaN or +-Inf, *exp is unspecified, return arg0. */
8336 return omit_one_operand_loc (loc, rettype, arg0, arg1);
8337 case rvc_normal:
8339 /* Since the frexp function always expects base 2, and in
8340 GCC normalized significands are already in the range
8341 [0.5, 1.0), we have exactly what frexp wants. */
8342 REAL_VALUE_TYPE frac_rvt = *value;
8343 SET_REAL_EXP (&frac_rvt, 0);
8344 frac = build_real (rettype, frac_rvt);
8345 exp = build_int_cst (integer_type_node, REAL_EXP (value));
8347 break;
8348 default:
8349 gcc_unreachable ();
8352 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8353 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1, exp);
8354 TREE_SIDE_EFFECTS (arg1) = 1;
8355 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1, frac);
8358 return NULL_TREE;
8361 /* Fold a call to builtin modf. */
8363 static tree
8364 fold_builtin_modf (location_t loc, tree arg0, tree arg1, tree rettype)
8366 if (! validate_arg (arg0, REAL_TYPE) || ! validate_arg (arg1, POINTER_TYPE))
8367 return NULL_TREE;
8369 STRIP_NOPS (arg0);
8371 if (!(TREE_CODE (arg0) == REAL_CST && ! TREE_OVERFLOW (arg0)))
8372 return NULL_TREE;
8374 arg1 = build_fold_indirect_ref_loc (loc, arg1);
8376 /* Proceed if a valid pointer type was passed in. */
8377 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg1)) == TYPE_MAIN_VARIANT (rettype))
8379 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (arg0);
8380 REAL_VALUE_TYPE trunc, frac;
8382 switch (value->cl)
8384 case rvc_nan:
8385 case rvc_zero:
8386 /* For +-NaN or +-0, return (*arg1 = arg0, arg0). */
8387 trunc = frac = *value;
8388 break;
8389 case rvc_inf:
8390 /* For +-Inf, return (*arg1 = arg0, +-0). */
8391 frac = dconst0;
8392 frac.sign = value->sign;
8393 trunc = *value;
8394 break;
8395 case rvc_normal:
8396 /* Return (*arg1 = trunc(arg0), arg0-trunc(arg0)). */
8397 real_trunc (&trunc, VOIDmode, value);
8398 real_arithmetic (&frac, MINUS_EXPR, value, &trunc);
8399 /* If the original number was negative and already
8400 integral, then the fractional part is -0.0. */
8401 if (value->sign && frac.cl == rvc_zero)
8402 frac.sign = value->sign;
8403 break;
8406 /* Create the COMPOUND_EXPR (*arg1 = trunc, frac). */
8407 arg1 = fold_build2_loc (loc, MODIFY_EXPR, rettype, arg1,
8408 build_real (rettype, trunc));
8409 TREE_SIDE_EFFECTS (arg1) = 1;
8410 return fold_build2_loc (loc, COMPOUND_EXPR, rettype, arg1,
8411 build_real (rettype, frac));
8414 return NULL_TREE;
8417 /* Given a location LOC, an interclass builtin function decl FNDECL
8418 and its single argument ARG, return an folded expression computing
8419 the same, or NULL_TREE if we either couldn't or didn't want to fold
8420 (the latter happen if there's an RTL instruction available). */
8422 static tree
8423 fold_builtin_interclass_mathfn (location_t loc, tree fndecl, tree arg)
8425 machine_mode mode;
8427 if (!validate_arg (arg, REAL_TYPE))
8428 return NULL_TREE;
8430 if (interclass_mathfn_icode (arg, fndecl) != CODE_FOR_nothing)
8431 return NULL_TREE;
8433 mode = TYPE_MODE (TREE_TYPE (arg));
8435 bool is_ibm_extended = MODE_COMPOSITE_P (mode);
8437 /* If there is no optab, try generic code. */
8438 switch (DECL_FUNCTION_CODE (fndecl))
8440 tree result;
8442 CASE_FLT_FN (BUILT_IN_ISINF):
8444 /* isinf(x) -> isgreater(fabs(x),DBL_MAX). */
8445 tree const isgr_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8446 tree type = TREE_TYPE (arg);
8447 REAL_VALUE_TYPE r;
8448 char buf[128];
8450 if (is_ibm_extended)
8452 /* NaN and Inf are encoded in the high-order double value
8453 only. The low-order value is not significant. */
8454 type = double_type_node;
8455 mode = DFmode;
8456 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8458 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8459 real_from_string (&r, buf);
8460 result = build_call_expr (isgr_fn, 2,
8461 fold_build1_loc (loc, ABS_EXPR, type, arg),
8462 build_real (type, r));
8463 return result;
8465 CASE_FLT_FN (BUILT_IN_FINITE):
8466 case BUILT_IN_ISFINITE:
8468 /* isfinite(x) -> islessequal(fabs(x),DBL_MAX). */
8469 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8470 tree type = TREE_TYPE (arg);
8471 REAL_VALUE_TYPE r;
8472 char buf[128];
8474 if (is_ibm_extended)
8476 /* NaN and Inf are encoded in the high-order double value
8477 only. The low-order value is not significant. */
8478 type = double_type_node;
8479 mode = DFmode;
8480 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8482 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8483 real_from_string (&r, buf);
8484 result = build_call_expr (isle_fn, 2,
8485 fold_build1_loc (loc, ABS_EXPR, type, arg),
8486 build_real (type, r));
8487 /*result = fold_build2_loc (loc, UNGT_EXPR,
8488 TREE_TYPE (TREE_TYPE (fndecl)),
8489 fold_build1_loc (loc, ABS_EXPR, type, arg),
8490 build_real (type, r));
8491 result = fold_build1_loc (loc, TRUTH_NOT_EXPR,
8492 TREE_TYPE (TREE_TYPE (fndecl)),
8493 result);*/
8494 return result;
8496 case BUILT_IN_ISNORMAL:
8498 /* isnormal(x) -> isgreaterequal(fabs(x),DBL_MIN) &
8499 islessequal(fabs(x),DBL_MAX). */
8500 tree const isle_fn = builtin_decl_explicit (BUILT_IN_ISLESSEQUAL);
8501 tree type = TREE_TYPE (arg);
8502 tree orig_arg, max_exp, min_exp;
8503 machine_mode orig_mode = mode;
8504 REAL_VALUE_TYPE rmax, rmin;
8505 char buf[128];
8507 orig_arg = arg = builtin_save_expr (arg);
8508 if (is_ibm_extended)
8510 /* Use double to test the normal range of IBM extended
8511 precision. Emin for IBM extended precision is
8512 different to emin for IEEE double, being 53 higher
8513 since the low double exponent is at least 53 lower
8514 than the high double exponent. */
8515 type = double_type_node;
8516 mode = DFmode;
8517 arg = fold_build1_loc (loc, NOP_EXPR, type, arg);
8519 arg = fold_build1_loc (loc, ABS_EXPR, type, arg);
8521 get_max_float (REAL_MODE_FORMAT (mode), buf, sizeof (buf));
8522 real_from_string (&rmax, buf);
8523 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (orig_mode)->emin - 1);
8524 real_from_string (&rmin, buf);
8525 max_exp = build_real (type, rmax);
8526 min_exp = build_real (type, rmin);
8528 max_exp = build_call_expr (isle_fn, 2, arg, max_exp);
8529 if (is_ibm_extended)
8531 /* Testing the high end of the range is done just using
8532 the high double, using the same test as isfinite().
8533 For the subnormal end of the range we first test the
8534 high double, then if its magnitude is equal to the
8535 limit of 0x1p-969, we test whether the low double is
8536 non-zero and opposite sign to the high double. */
8537 tree const islt_fn = builtin_decl_explicit (BUILT_IN_ISLESS);
8538 tree const isgt_fn = builtin_decl_explicit (BUILT_IN_ISGREATER);
8539 tree gt_min = build_call_expr (isgt_fn, 2, arg, min_exp);
8540 tree eq_min = fold_build2 (EQ_EXPR, integer_type_node,
8541 arg, min_exp);
8542 tree as_complex = build1 (VIEW_CONVERT_EXPR,
8543 complex_double_type_node, orig_arg);
8544 tree hi_dbl = build1 (REALPART_EXPR, type, as_complex);
8545 tree lo_dbl = build1 (IMAGPART_EXPR, type, as_complex);
8546 tree zero = build_real (type, dconst0);
8547 tree hilt = build_call_expr (islt_fn, 2, hi_dbl, zero);
8548 tree lolt = build_call_expr (islt_fn, 2, lo_dbl, zero);
8549 tree logt = build_call_expr (isgt_fn, 2, lo_dbl, zero);
8550 tree ok_lo = fold_build1 (TRUTH_NOT_EXPR, integer_type_node,
8551 fold_build3 (COND_EXPR,
8552 integer_type_node,
8553 hilt, logt, lolt));
8554 eq_min = fold_build2 (TRUTH_ANDIF_EXPR, integer_type_node,
8555 eq_min, ok_lo);
8556 min_exp = fold_build2 (TRUTH_ORIF_EXPR, integer_type_node,
8557 gt_min, eq_min);
8559 else
8561 tree const isge_fn
8562 = builtin_decl_explicit (BUILT_IN_ISGREATEREQUAL);
8563 min_exp = build_call_expr (isge_fn, 2, arg, min_exp);
8565 result = fold_build2 (BIT_AND_EXPR, integer_type_node,
8566 max_exp, min_exp);
8567 return result;
8569 default:
8570 break;
8573 return NULL_TREE;
8576 /* Fold a call to __builtin_isnan(), __builtin_isinf, __builtin_finite.
8577 ARG is the argument for the call. */
8579 static tree
8580 fold_builtin_classify (location_t loc, tree fndecl, tree arg, int builtin_index)
8582 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8584 if (!validate_arg (arg, REAL_TYPE))
8585 return NULL_TREE;
8587 switch (builtin_index)
8589 case BUILT_IN_ISINF:
8590 if (!HONOR_INFINITIES (arg))
8591 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8593 return NULL_TREE;
8595 case BUILT_IN_ISINF_SIGN:
8597 /* isinf_sign(x) -> isinf(x) ? (signbit(x) ? -1 : 1) : 0 */
8598 /* In a boolean context, GCC will fold the inner COND_EXPR to
8599 1. So e.g. "if (isinf_sign(x))" would be folded to just
8600 "if (isinf(x) ? 1 : 0)" which becomes "if (isinf(x))". */
8601 tree signbit_fn = builtin_decl_explicit (BUILT_IN_SIGNBIT);
8602 tree isinf_fn = builtin_decl_explicit (BUILT_IN_ISINF);
8603 tree tmp = NULL_TREE;
8605 arg = builtin_save_expr (arg);
8607 if (signbit_fn && isinf_fn)
8609 tree signbit_call = build_call_expr_loc (loc, signbit_fn, 1, arg);
8610 tree isinf_call = build_call_expr_loc (loc, isinf_fn, 1, arg);
8612 signbit_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8613 signbit_call, integer_zero_node);
8614 isinf_call = fold_build2_loc (loc, NE_EXPR, integer_type_node,
8615 isinf_call, integer_zero_node);
8617 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node, signbit_call,
8618 integer_minus_one_node, integer_one_node);
8619 tmp = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8620 isinf_call, tmp,
8621 integer_zero_node);
8624 return tmp;
8627 case BUILT_IN_ISFINITE:
8628 if (!HONOR_NANS (arg)
8629 && !HONOR_INFINITIES (arg))
8630 return omit_one_operand_loc (loc, type, integer_one_node, arg);
8632 return NULL_TREE;
8634 case BUILT_IN_ISNAN:
8635 if (!HONOR_NANS (arg))
8636 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
8639 bool is_ibm_extended = MODE_COMPOSITE_P (TYPE_MODE (TREE_TYPE (arg)));
8640 if (is_ibm_extended)
8642 /* NaN and Inf are encoded in the high-order double value
8643 only. The low-order value is not significant. */
8644 arg = fold_build1_loc (loc, NOP_EXPR, double_type_node, arg);
8647 arg = builtin_save_expr (arg);
8648 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg, arg);
8650 default:
8651 gcc_unreachable ();
8655 /* Fold a call to __builtin_fpclassify(int, int, int, int, int, ...).
8656 This builtin will generate code to return the appropriate floating
8657 point classification depending on the value of the floating point
8658 number passed in. The possible return values must be supplied as
8659 int arguments to the call in the following order: FP_NAN, FP_INFINITE,
8660 FP_NORMAL, FP_SUBNORMAL and FP_ZERO. The ellipses is for exactly
8661 one floating point argument which is "type generic". */
8663 static tree
8664 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
8666 tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
8667 arg, type, res, tmp;
8668 machine_mode mode;
8669 REAL_VALUE_TYPE r;
8670 char buf[128];
8672 /* Verify the required arguments in the original call. */
8673 if (nargs != 6
8674 || !validate_arg (args[0], INTEGER_TYPE)
8675 || !validate_arg (args[1], INTEGER_TYPE)
8676 || !validate_arg (args[2], INTEGER_TYPE)
8677 || !validate_arg (args[3], INTEGER_TYPE)
8678 || !validate_arg (args[4], INTEGER_TYPE)
8679 || !validate_arg (args[5], REAL_TYPE))
8680 return NULL_TREE;
8682 fp_nan = args[0];
8683 fp_infinite = args[1];
8684 fp_normal = args[2];
8685 fp_subnormal = args[3];
8686 fp_zero = args[4];
8687 arg = args[5];
8688 type = TREE_TYPE (arg);
8689 mode = TYPE_MODE (type);
8690 arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
8692 /* fpclassify(x) ->
8693 isnan(x) ? FP_NAN :
8694 (fabs(x) == Inf ? FP_INFINITE :
8695 (fabs(x) >= DBL_MIN ? FP_NORMAL :
8696 (x == 0 ? FP_ZERO : FP_SUBNORMAL))). */
8698 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8699 build_real (type, dconst0));
8700 res = fold_build3_loc (loc, COND_EXPR, integer_type_node,
8701 tmp, fp_zero, fp_subnormal);
8703 sprintf (buf, "0x1p%d", REAL_MODE_FORMAT (mode)->emin - 1);
8704 real_from_string (&r, buf);
8705 tmp = fold_build2_loc (loc, GE_EXPR, integer_type_node,
8706 arg, build_real (type, r));
8707 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, fp_normal, res);
8709 if (HONOR_INFINITIES (mode))
8711 real_inf (&r);
8712 tmp = fold_build2_loc (loc, EQ_EXPR, integer_type_node, arg,
8713 build_real (type, r));
8714 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp,
8715 fp_infinite, res);
8718 if (HONOR_NANS (mode))
8720 tmp = fold_build2_loc (loc, ORDERED_EXPR, integer_type_node, arg, arg);
8721 res = fold_build3_loc (loc, COND_EXPR, integer_type_node, tmp, res, fp_nan);
8724 return res;
8727 /* Fold a call to an unordered comparison function such as
8728 __builtin_isgreater(). FNDECL is the FUNCTION_DECL for the function
8729 being called and ARG0 and ARG1 are the arguments for the call.
8730 UNORDERED_CODE and ORDERED_CODE are comparison codes that give
8731 the opposite of the desired result. UNORDERED_CODE is used
8732 for modes that can hold NaNs and ORDERED_CODE is used for
8733 the rest. */
8735 static tree
8736 fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1,
8737 enum tree_code unordered_code,
8738 enum tree_code ordered_code)
8740 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8741 enum tree_code code;
8742 tree type0, type1;
8743 enum tree_code code0, code1;
8744 tree cmp_type = NULL_TREE;
8746 type0 = TREE_TYPE (arg0);
8747 type1 = TREE_TYPE (arg1);
8749 code0 = TREE_CODE (type0);
8750 code1 = TREE_CODE (type1);
8752 if (code0 == REAL_TYPE && code1 == REAL_TYPE)
8753 /* Choose the wider of two real types. */
8754 cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1)
8755 ? type0 : type1;
8756 else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
8757 cmp_type = type0;
8758 else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE)
8759 cmp_type = type1;
8761 arg0 = fold_convert_loc (loc, cmp_type, arg0);
8762 arg1 = fold_convert_loc (loc, cmp_type, arg1);
8764 if (unordered_code == UNORDERED_EXPR)
8766 if (!HONOR_NANS (arg0))
8767 return omit_two_operands_loc (loc, type, integer_zero_node, arg0, arg1);
8768 return fold_build2_loc (loc, UNORDERED_EXPR, type, arg0, arg1);
8771 code = HONOR_NANS (arg0) ? unordered_code : ordered_code;
8772 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
8773 fold_build2_loc (loc, code, type, arg0, arg1));
8776 /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal
8777 arithmetics if it can never overflow, or into internal functions that
8778 return both result of arithmetics and overflowed boolean flag in
8779 a complex integer result, or some other check for overflow.
8780 Similarly fold __builtin_{add,sub,mul}_overflow_p to just the overflow
8781 checking part of that. */
8783 static tree
8784 fold_builtin_arith_overflow (location_t loc, enum built_in_function fcode,
8785 tree arg0, tree arg1, tree arg2)
8787 enum internal_fn ifn = IFN_LAST;
8788 /* The code of the expression corresponding to the type-generic
8789 built-in, or ERROR_MARK for the type-specific ones. */
8790 enum tree_code opcode = ERROR_MARK;
8791 bool ovf_only = false;
8793 switch (fcode)
8795 case BUILT_IN_ADD_OVERFLOW_P:
8796 ovf_only = true;
8797 /* FALLTHRU */
8798 case BUILT_IN_ADD_OVERFLOW:
8799 opcode = PLUS_EXPR;
8800 /* FALLTHRU */
8801 case BUILT_IN_SADD_OVERFLOW:
8802 case BUILT_IN_SADDL_OVERFLOW:
8803 case BUILT_IN_SADDLL_OVERFLOW:
8804 case BUILT_IN_UADD_OVERFLOW:
8805 case BUILT_IN_UADDL_OVERFLOW:
8806 case BUILT_IN_UADDLL_OVERFLOW:
8807 ifn = IFN_ADD_OVERFLOW;
8808 break;
8809 case BUILT_IN_SUB_OVERFLOW_P:
8810 ovf_only = true;
8811 /* FALLTHRU */
8812 case BUILT_IN_SUB_OVERFLOW:
8813 opcode = MINUS_EXPR;
8814 /* FALLTHRU */
8815 case BUILT_IN_SSUB_OVERFLOW:
8816 case BUILT_IN_SSUBL_OVERFLOW:
8817 case BUILT_IN_SSUBLL_OVERFLOW:
8818 case BUILT_IN_USUB_OVERFLOW:
8819 case BUILT_IN_USUBL_OVERFLOW:
8820 case BUILT_IN_USUBLL_OVERFLOW:
8821 ifn = IFN_SUB_OVERFLOW;
8822 break;
8823 case BUILT_IN_MUL_OVERFLOW_P:
8824 ovf_only = true;
8825 /* FALLTHRU */
8826 case BUILT_IN_MUL_OVERFLOW:
8827 opcode = MULT_EXPR;
8828 /* FALLTHRU */
8829 case BUILT_IN_SMUL_OVERFLOW:
8830 case BUILT_IN_SMULL_OVERFLOW:
8831 case BUILT_IN_SMULLL_OVERFLOW:
8832 case BUILT_IN_UMUL_OVERFLOW:
8833 case BUILT_IN_UMULL_OVERFLOW:
8834 case BUILT_IN_UMULLL_OVERFLOW:
8835 ifn = IFN_MUL_OVERFLOW;
8836 break;
8837 default:
8838 gcc_unreachable ();
8841 /* For the "generic" overloads, the first two arguments can have different
8842 types and the last argument determines the target type to use to check
8843 for overflow. The arguments of the other overloads all have the same
8844 type. */
8845 tree type = ovf_only ? TREE_TYPE (arg2) : TREE_TYPE (TREE_TYPE (arg2));
8847 /* For the __builtin_{add,sub,mul}_overflow_p builtins, when the first two
8848 arguments are constant, attempt to fold the built-in call into a constant
8849 expression indicating whether or not it detected an overflow. */
8850 if (ovf_only
8851 && TREE_CODE (arg0) == INTEGER_CST
8852 && TREE_CODE (arg1) == INTEGER_CST)
8853 /* Perform the computation in the target type and check for overflow. */
8854 return omit_one_operand_loc (loc, boolean_type_node,
8855 arith_overflowed_p (opcode, type, arg0, arg1)
8856 ? boolean_true_node : boolean_false_node,
8857 arg2);
8859 tree ctype = build_complex_type (type);
8860 tree call = build_call_expr_internal_loc (loc, ifn, ctype,
8861 2, arg0, arg1);
8862 tree tgt = save_expr (call);
8863 tree intres = build1_loc (loc, REALPART_EXPR, type, tgt);
8864 tree ovfres = build1_loc (loc, IMAGPART_EXPR, type, tgt);
8865 ovfres = fold_convert_loc (loc, boolean_type_node, ovfres);
8867 if (ovf_only)
8868 return omit_one_operand_loc (loc, boolean_type_node, ovfres, arg2);
8870 tree mem_arg2 = build_fold_indirect_ref_loc (loc, arg2);
8871 tree store
8872 = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, mem_arg2, intres);
8873 return build2_loc (loc, COMPOUND_EXPR, boolean_type_node, store, ovfres);
8876 /* Fold a call to __builtin_FILE to a constant string. */
8878 static inline tree
8879 fold_builtin_FILE (location_t loc)
8881 if (const char *fname = LOCATION_FILE (loc))
8883 /* The documentation says this builtin is equivalent to the preprocessor
8884 __FILE__ macro so it appears appropriate to use the same file prefix
8885 mappings. */
8886 fname = remap_macro_filename (fname);
8887 return build_string_literal (strlen (fname) + 1, fname);
8890 return build_string_literal (1, "");
8893 /* Fold a call to __builtin_FUNCTION to a constant string. */
8895 static inline tree
8896 fold_builtin_FUNCTION ()
8898 const char *name = "";
8900 if (current_function_decl)
8901 name = lang_hooks.decl_printable_name (current_function_decl, 0);
8903 return build_string_literal (strlen (name) + 1, name);
8906 /* Fold a call to __builtin_LINE to an integer constant. */
8908 static inline tree
8909 fold_builtin_LINE (location_t loc, tree type)
8911 return build_int_cst (type, LOCATION_LINE (loc));
8914 /* Fold a call to built-in function FNDECL with 0 arguments.
8915 This function returns NULL_TREE if no simplification was possible. */
8917 static tree
8918 fold_builtin_0 (location_t loc, tree fndecl)
8920 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8921 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8922 switch (fcode)
8924 case BUILT_IN_FILE:
8925 return fold_builtin_FILE (loc);
8927 case BUILT_IN_FUNCTION:
8928 return fold_builtin_FUNCTION ();
8930 case BUILT_IN_LINE:
8931 return fold_builtin_LINE (loc, type);
8933 CASE_FLT_FN (BUILT_IN_INF):
8934 CASE_FLT_FN_FLOATN_NX (BUILT_IN_INF):
8935 case BUILT_IN_INFD32:
8936 case BUILT_IN_INFD64:
8937 case BUILT_IN_INFD128:
8938 return fold_builtin_inf (loc, type, true);
8940 CASE_FLT_FN (BUILT_IN_HUGE_VAL):
8941 CASE_FLT_FN_FLOATN_NX (BUILT_IN_HUGE_VAL):
8942 return fold_builtin_inf (loc, type, false);
8944 case BUILT_IN_CLASSIFY_TYPE:
8945 return fold_builtin_classify_type (NULL_TREE);
8947 default:
8948 break;
8950 return NULL_TREE;
8953 /* Fold a call to built-in function FNDECL with 1 argument, ARG0.
8954 This function returns NULL_TREE if no simplification was possible. */
8956 static tree
8957 fold_builtin_1 (location_t loc, tree fndecl, tree arg0)
8959 tree type = TREE_TYPE (TREE_TYPE (fndecl));
8960 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
8962 if (TREE_CODE (arg0) == ERROR_MARK)
8963 return NULL_TREE;
8965 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0))
8966 return ret;
8968 switch (fcode)
8970 case BUILT_IN_CONSTANT_P:
8972 tree val = fold_builtin_constant_p (arg0);
8974 /* Gimplification will pull the CALL_EXPR for the builtin out of
8975 an if condition. When not optimizing, we'll not CSE it back.
8976 To avoid link error types of regressions, return false now. */
8977 if (!val && !optimize)
8978 val = integer_zero_node;
8980 return val;
8983 case BUILT_IN_CLASSIFY_TYPE:
8984 return fold_builtin_classify_type (arg0);
8986 case BUILT_IN_STRLEN:
8987 return fold_builtin_strlen (loc, type, arg0);
8989 CASE_FLT_FN (BUILT_IN_FABS):
8990 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
8991 case BUILT_IN_FABSD32:
8992 case BUILT_IN_FABSD64:
8993 case BUILT_IN_FABSD128:
8994 return fold_builtin_fabs (loc, arg0, type);
8996 case BUILT_IN_ABS:
8997 case BUILT_IN_LABS:
8998 case BUILT_IN_LLABS:
8999 case BUILT_IN_IMAXABS:
9000 return fold_builtin_abs (loc, arg0, type);
9002 CASE_FLT_FN (BUILT_IN_CONJ):
9003 if (validate_arg (arg0, COMPLEX_TYPE)
9004 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9005 return fold_build1_loc (loc, CONJ_EXPR, type, arg0);
9006 break;
9008 CASE_FLT_FN (BUILT_IN_CREAL):
9009 if (validate_arg (arg0, COMPLEX_TYPE)
9010 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9011 return non_lvalue_loc (loc, fold_build1_loc (loc, REALPART_EXPR, type, arg0));
9012 break;
9014 CASE_FLT_FN (BUILT_IN_CIMAG):
9015 if (validate_arg (arg0, COMPLEX_TYPE)
9016 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE)
9017 return non_lvalue_loc (loc, fold_build1_loc (loc, IMAGPART_EXPR, type, arg0));
9018 break;
9020 CASE_FLT_FN (BUILT_IN_CARG):
9021 return fold_builtin_carg (loc, arg0, type);
9023 case BUILT_IN_ISASCII:
9024 return fold_builtin_isascii (loc, arg0);
9026 case BUILT_IN_TOASCII:
9027 return fold_builtin_toascii (loc, arg0);
9029 case BUILT_IN_ISDIGIT:
9030 return fold_builtin_isdigit (loc, arg0);
9032 CASE_FLT_FN (BUILT_IN_FINITE):
9033 case BUILT_IN_FINITED32:
9034 case BUILT_IN_FINITED64:
9035 case BUILT_IN_FINITED128:
9036 case BUILT_IN_ISFINITE:
9038 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISFINITE);
9039 if (ret)
9040 return ret;
9041 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9044 CASE_FLT_FN (BUILT_IN_ISINF):
9045 case BUILT_IN_ISINFD32:
9046 case BUILT_IN_ISINFD64:
9047 case BUILT_IN_ISINFD128:
9049 tree ret = fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF);
9050 if (ret)
9051 return ret;
9052 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9055 case BUILT_IN_ISNORMAL:
9056 return fold_builtin_interclass_mathfn (loc, fndecl, arg0);
9058 case BUILT_IN_ISINF_SIGN:
9059 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISINF_SIGN);
9061 CASE_FLT_FN (BUILT_IN_ISNAN):
9062 case BUILT_IN_ISNAND32:
9063 case BUILT_IN_ISNAND64:
9064 case BUILT_IN_ISNAND128:
9065 return fold_builtin_classify (loc, fndecl, arg0, BUILT_IN_ISNAN);
9067 case BUILT_IN_FREE:
9068 if (integer_zerop (arg0))
9069 return build_empty_stmt (loc);
9070 break;
9072 default:
9073 break;
9076 return NULL_TREE;
9080 /* Fold a call to built-in function FNDECL with 2 arguments, ARG0 and ARG1.
9081 This function returns NULL_TREE if no simplification was possible. */
9083 static tree
9084 fold_builtin_2 (location_t loc, tree fndecl, tree arg0, tree arg1)
9086 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9087 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9089 if (TREE_CODE (arg0) == ERROR_MARK
9090 || TREE_CODE (arg1) == ERROR_MARK)
9091 return NULL_TREE;
9093 if (tree ret = fold_const_call (as_combined_fn (fcode), type, arg0, arg1))
9094 return ret;
9096 switch (fcode)
9098 CASE_FLT_FN_REENT (BUILT_IN_GAMMA): /* GAMMA_R */
9099 CASE_FLT_FN_REENT (BUILT_IN_LGAMMA): /* LGAMMA_R */
9100 if (validate_arg (arg0, REAL_TYPE)
9101 && validate_arg (arg1, POINTER_TYPE))
9102 return do_mpfr_lgamma_r (arg0, arg1, type);
9103 break;
9105 CASE_FLT_FN (BUILT_IN_FREXP):
9106 return fold_builtin_frexp (loc, arg0, arg1, type);
9108 CASE_FLT_FN (BUILT_IN_MODF):
9109 return fold_builtin_modf (loc, arg0, arg1, type);
9111 case BUILT_IN_STRSPN:
9112 return fold_builtin_strspn (loc, arg0, arg1);
9114 case BUILT_IN_STRCSPN:
9115 return fold_builtin_strcspn (loc, arg0, arg1);
9117 case BUILT_IN_STRPBRK:
9118 return fold_builtin_strpbrk (loc, arg0, arg1, type);
9120 case BUILT_IN_EXPECT:
9121 return fold_builtin_expect (loc, arg0, arg1, NULL_TREE);
9123 case BUILT_IN_ISGREATER:
9124 return fold_builtin_unordered_cmp (loc, fndecl,
9125 arg0, arg1, UNLE_EXPR, LE_EXPR);
9126 case BUILT_IN_ISGREATEREQUAL:
9127 return fold_builtin_unordered_cmp (loc, fndecl,
9128 arg0, arg1, UNLT_EXPR, LT_EXPR);
9129 case BUILT_IN_ISLESS:
9130 return fold_builtin_unordered_cmp (loc, fndecl,
9131 arg0, arg1, UNGE_EXPR, GE_EXPR);
9132 case BUILT_IN_ISLESSEQUAL:
9133 return fold_builtin_unordered_cmp (loc, fndecl,
9134 arg0, arg1, UNGT_EXPR, GT_EXPR);
9135 case BUILT_IN_ISLESSGREATER:
9136 return fold_builtin_unordered_cmp (loc, fndecl,
9137 arg0, arg1, UNEQ_EXPR, EQ_EXPR);
9138 case BUILT_IN_ISUNORDERED:
9139 return fold_builtin_unordered_cmp (loc, fndecl,
9140 arg0, arg1, UNORDERED_EXPR,
9141 NOP_EXPR);
9143 /* We do the folding for va_start in the expander. */
9144 case BUILT_IN_VA_START:
9145 break;
9147 case BUILT_IN_OBJECT_SIZE:
9148 return fold_builtin_object_size (arg0, arg1);
9150 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
9151 return fold_builtin_atomic_always_lock_free (arg0, arg1);
9153 case BUILT_IN_ATOMIC_IS_LOCK_FREE:
9154 return fold_builtin_atomic_is_lock_free (arg0, arg1);
9156 default:
9157 break;
9159 return NULL_TREE;
9162 /* Fold a call to built-in function FNDECL with 3 arguments, ARG0, ARG1,
9163 and ARG2.
9164 This function returns NULL_TREE if no simplification was possible. */
9166 static tree
9167 fold_builtin_3 (location_t loc, tree fndecl,
9168 tree arg0, tree arg1, tree arg2)
9170 tree type = TREE_TYPE (TREE_TYPE (fndecl));
9171 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9173 if (TREE_CODE (arg0) == ERROR_MARK
9174 || TREE_CODE (arg1) == ERROR_MARK
9175 || TREE_CODE (arg2) == ERROR_MARK)
9176 return NULL_TREE;
9178 if (tree ret = fold_const_call (as_combined_fn (fcode), type,
9179 arg0, arg1, arg2))
9180 return ret;
9182 switch (fcode)
9185 CASE_FLT_FN (BUILT_IN_SINCOS):
9186 return fold_builtin_sincos (loc, arg0, arg1, arg2);
9188 CASE_FLT_FN (BUILT_IN_REMQUO):
9189 if (validate_arg (arg0, REAL_TYPE)
9190 && validate_arg (arg1, REAL_TYPE)
9191 && validate_arg (arg2, POINTER_TYPE))
9192 return do_mpfr_remquo (arg0, arg1, arg2);
9193 break;
9195 case BUILT_IN_MEMCMP:
9196 return fold_builtin_memcmp (loc, arg0, arg1, arg2);
9198 case BUILT_IN_EXPECT:
9199 return fold_builtin_expect (loc, arg0, arg1, arg2);
9201 case BUILT_IN_ADD_OVERFLOW:
9202 case BUILT_IN_SUB_OVERFLOW:
9203 case BUILT_IN_MUL_OVERFLOW:
9204 case BUILT_IN_ADD_OVERFLOW_P:
9205 case BUILT_IN_SUB_OVERFLOW_P:
9206 case BUILT_IN_MUL_OVERFLOW_P:
9207 case BUILT_IN_SADD_OVERFLOW:
9208 case BUILT_IN_SADDL_OVERFLOW:
9209 case BUILT_IN_SADDLL_OVERFLOW:
9210 case BUILT_IN_SSUB_OVERFLOW:
9211 case BUILT_IN_SSUBL_OVERFLOW:
9212 case BUILT_IN_SSUBLL_OVERFLOW:
9213 case BUILT_IN_SMUL_OVERFLOW:
9214 case BUILT_IN_SMULL_OVERFLOW:
9215 case BUILT_IN_SMULLL_OVERFLOW:
9216 case BUILT_IN_UADD_OVERFLOW:
9217 case BUILT_IN_UADDL_OVERFLOW:
9218 case BUILT_IN_UADDLL_OVERFLOW:
9219 case BUILT_IN_USUB_OVERFLOW:
9220 case BUILT_IN_USUBL_OVERFLOW:
9221 case BUILT_IN_USUBLL_OVERFLOW:
9222 case BUILT_IN_UMUL_OVERFLOW:
9223 case BUILT_IN_UMULL_OVERFLOW:
9224 case BUILT_IN_UMULLL_OVERFLOW:
9225 return fold_builtin_arith_overflow (loc, fcode, arg0, arg1, arg2);
9227 default:
9228 break;
9230 return NULL_TREE;
9233 /* Fold a call to built-in function FNDECL. ARGS is an array of NARGS
9234 arguments. IGNORE is true if the result of the
9235 function call is ignored. This function returns NULL_TREE if no
9236 simplification was possible. */
9238 tree
9239 fold_builtin_n (location_t loc, tree fndecl, tree *args, int nargs, bool)
9241 tree ret = NULL_TREE;
9243 switch (nargs)
9245 case 0:
9246 ret = fold_builtin_0 (loc, fndecl);
9247 break;
9248 case 1:
9249 ret = fold_builtin_1 (loc, fndecl, args[0]);
9250 break;
9251 case 2:
9252 ret = fold_builtin_2 (loc, fndecl, args[0], args[1]);
9253 break;
9254 case 3:
9255 ret = fold_builtin_3 (loc, fndecl, args[0], args[1], args[2]);
9256 break;
9257 default:
9258 ret = fold_builtin_varargs (loc, fndecl, args, nargs);
9259 break;
9261 if (ret)
9263 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
9264 SET_EXPR_LOCATION (ret, loc);
9265 return ret;
9267 return NULL_TREE;
9270 /* Construct a new CALL_EXPR to FNDECL using the tail of the argument
9271 list ARGS along with N new arguments in NEWARGS. SKIP is the number
9272 of arguments in ARGS to be omitted. OLDNARGS is the number of
9273 elements in ARGS. */
9275 static tree
9276 rewrite_call_expr_valist (location_t loc, int oldnargs, tree *args,
9277 int skip, tree fndecl, int n, va_list newargs)
9279 int nargs = oldnargs - skip + n;
9280 tree *buffer;
9282 if (n > 0)
9284 int i, j;
9286 buffer = XALLOCAVEC (tree, nargs);
9287 for (i = 0; i < n; i++)
9288 buffer[i] = va_arg (newargs, tree);
9289 for (j = skip; j < oldnargs; j++, i++)
9290 buffer[i] = args[j];
9292 else
9293 buffer = args + skip;
9295 return build_call_expr_loc_array (loc, fndecl, nargs, buffer);
9298 /* Return true if FNDECL shouldn't be folded right now.
9299 If a built-in function has an inline attribute always_inline
9300 wrapper, defer folding it after always_inline functions have
9301 been inlined, otherwise e.g. -D_FORTIFY_SOURCE checking
9302 might not be performed. */
9304 bool
9305 avoid_folding_inline_builtin (tree fndecl)
9307 return (DECL_DECLARED_INLINE_P (fndecl)
9308 && DECL_DISREGARD_INLINE_LIMITS (fndecl)
9309 && cfun
9310 && !cfun->always_inline_functions_inlined
9311 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fndecl)));
9314 /* A wrapper function for builtin folding that prevents warnings for
9315 "statement without effect" and the like, caused by removing the
9316 call node earlier than the warning is generated. */
9318 tree
9319 fold_call_expr (location_t loc, tree exp, bool ignore)
9321 tree ret = NULL_TREE;
9322 tree fndecl = get_callee_fndecl (exp);
9323 if (fndecl
9324 && TREE_CODE (fndecl) == FUNCTION_DECL
9325 && DECL_BUILT_IN (fndecl)
9326 /* If CALL_EXPR_VA_ARG_PACK is set, the arguments aren't finalized
9327 yet. Defer folding until we see all the arguments
9328 (after inlining). */
9329 && !CALL_EXPR_VA_ARG_PACK (exp))
9331 int nargs = call_expr_nargs (exp);
9333 /* Before gimplification CALL_EXPR_VA_ARG_PACK is not set, but
9334 instead last argument is __builtin_va_arg_pack (). Defer folding
9335 even in that case, until arguments are finalized. */
9336 if (nargs && TREE_CODE (CALL_EXPR_ARG (exp, nargs - 1)) == CALL_EXPR)
9338 tree fndecl2 = get_callee_fndecl (CALL_EXPR_ARG (exp, nargs - 1));
9339 if (fndecl2
9340 && TREE_CODE (fndecl2) == FUNCTION_DECL
9341 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
9342 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
9343 return NULL_TREE;
9346 if (avoid_folding_inline_builtin (fndecl))
9347 return NULL_TREE;
9349 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9350 return targetm.fold_builtin (fndecl, call_expr_nargs (exp),
9351 CALL_EXPR_ARGP (exp), ignore);
9352 else
9354 tree *args = CALL_EXPR_ARGP (exp);
9355 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
9356 if (ret)
9357 return ret;
9360 return NULL_TREE;
9363 /* Fold a CALL_EXPR with type TYPE with FN as the function expression.
9364 N arguments are passed in the array ARGARRAY. Return a folded
9365 expression or NULL_TREE if no simplification was possible. */
9367 tree
9368 fold_builtin_call_array (location_t loc, tree,
9369 tree fn,
9370 int n,
9371 tree *argarray)
9373 if (TREE_CODE (fn) != ADDR_EXPR)
9374 return NULL_TREE;
9376 tree fndecl = TREE_OPERAND (fn, 0);
9377 if (TREE_CODE (fndecl) == FUNCTION_DECL
9378 && DECL_BUILT_IN (fndecl))
9380 /* If last argument is __builtin_va_arg_pack (), arguments to this
9381 function are not finalized yet. Defer folding until they are. */
9382 if (n && TREE_CODE (argarray[n - 1]) == CALL_EXPR)
9384 tree fndecl2 = get_callee_fndecl (argarray[n - 1]);
9385 if (fndecl2
9386 && TREE_CODE (fndecl2) == FUNCTION_DECL
9387 && DECL_BUILT_IN_CLASS (fndecl2) == BUILT_IN_NORMAL
9388 && DECL_FUNCTION_CODE (fndecl2) == BUILT_IN_VA_ARG_PACK)
9389 return NULL_TREE;
9391 if (avoid_folding_inline_builtin (fndecl))
9392 return NULL_TREE;
9393 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
9394 return targetm.fold_builtin (fndecl, n, argarray, false);
9395 else
9396 return fold_builtin_n (loc, fndecl, argarray, n, false);
9399 return NULL_TREE;
9402 /* Construct a new CALL_EXPR using the tail of the argument list of EXP
9403 along with N new arguments specified as the "..." parameters. SKIP
9404 is the number of arguments in EXP to be omitted. This function is used
9405 to do varargs-to-varargs transformations. */
9407 static tree
9408 rewrite_call_expr (location_t loc, tree exp, int skip, tree fndecl, int n, ...)
9410 va_list ap;
9411 tree t;
9413 va_start (ap, n);
9414 t = rewrite_call_expr_valist (loc, call_expr_nargs (exp),
9415 CALL_EXPR_ARGP (exp), skip, fndecl, n, ap);
9416 va_end (ap);
9418 return t;
9421 /* Validate a single argument ARG against a tree code CODE representing
9422 a type. Return true when argument is valid. */
9424 static bool
9425 validate_arg (const_tree arg, enum tree_code code)
9427 if (!arg)
9428 return false;
9429 else if (code == POINTER_TYPE)
9430 return POINTER_TYPE_P (TREE_TYPE (arg));
9431 else if (code == INTEGER_TYPE)
9432 return INTEGRAL_TYPE_P (TREE_TYPE (arg));
9433 return code == TREE_CODE (TREE_TYPE (arg));
9436 /* This function validates the types of a function call argument list
9437 against a specified list of tree_codes. If the last specifier is a 0,
9438 that represents an ellipses, otherwise the last specifier must be a
9439 VOID_TYPE.
9441 This is the GIMPLE version of validate_arglist. Eventually we want to
9442 completely convert builtins.c to work from GIMPLEs and the tree based
9443 validate_arglist will then be removed. */
9445 bool
9446 validate_gimple_arglist (const gcall *call, ...)
9448 enum tree_code code;
9449 bool res = 0;
9450 va_list ap;
9451 const_tree arg;
9452 size_t i;
9454 va_start (ap, call);
9455 i = 0;
9459 code = (enum tree_code) va_arg (ap, int);
9460 switch (code)
9462 case 0:
9463 /* This signifies an ellipses, any further arguments are all ok. */
9464 res = true;
9465 goto end;
9466 case VOID_TYPE:
9467 /* This signifies an endlink, if no arguments remain, return
9468 true, otherwise return false. */
9469 res = (i == gimple_call_num_args (call));
9470 goto end;
9471 default:
9472 /* If no parameters remain or the parameter's code does not
9473 match the specified code, return false. Otherwise continue
9474 checking any remaining arguments. */
9475 arg = gimple_call_arg (call, i++);
9476 if (!validate_arg (arg, code))
9477 goto end;
9478 break;
9481 while (1);
9483 /* We need gotos here since we can only have one VA_CLOSE in a
9484 function. */
9485 end: ;
9486 va_end (ap);
9488 return res;
9491 /* Default target-specific builtin expander that does nothing. */
9494 default_expand_builtin (tree exp ATTRIBUTE_UNUSED,
9495 rtx target ATTRIBUTE_UNUSED,
9496 rtx subtarget ATTRIBUTE_UNUSED,
9497 machine_mode mode ATTRIBUTE_UNUSED,
9498 int ignore ATTRIBUTE_UNUSED)
9500 return NULL_RTX;
9503 /* Returns true is EXP represents data that would potentially reside
9504 in a readonly section. */
9506 bool
9507 readonly_data_expr (tree exp)
9509 STRIP_NOPS (exp);
9511 if (TREE_CODE (exp) != ADDR_EXPR)
9512 return false;
9514 exp = get_base_address (TREE_OPERAND (exp, 0));
9515 if (!exp)
9516 return false;
9518 /* Make sure we call decl_readonly_section only for trees it
9519 can handle (since it returns true for everything it doesn't
9520 understand). */
9521 if (TREE_CODE (exp) == STRING_CST
9522 || TREE_CODE (exp) == CONSTRUCTOR
9523 || (VAR_P (exp) && TREE_STATIC (exp)))
9524 return decl_readonly_section (exp, 0);
9525 else
9526 return false;
9529 /* Simplify a call to the strpbrk builtin. S1 and S2 are the arguments
9530 to the call, and TYPE is its return type.
9532 Return NULL_TREE if no simplification was possible, otherwise return the
9533 simplified form of the call as a tree.
9535 The simplified form may be a constant or other expression which
9536 computes the same value, but in a more efficient manner (including
9537 calls to other builtin functions).
9539 The call may contain arguments which need to be evaluated, but
9540 which are not useful to determine the result of the call. In
9541 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9542 COMPOUND_EXPR will be an argument which must be evaluated.
9543 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9544 COMPOUND_EXPR in the chain will contain the tree for the simplified
9545 form of the builtin function call. */
9547 static tree
9548 fold_builtin_strpbrk (location_t loc, tree s1, tree s2, tree type)
9550 if (!validate_arg (s1, POINTER_TYPE)
9551 || !validate_arg (s2, POINTER_TYPE))
9552 return NULL_TREE;
9553 else
9555 tree fn;
9556 const char *p1, *p2;
9558 p2 = c_getstr (s2);
9559 if (p2 == NULL)
9560 return NULL_TREE;
9562 p1 = c_getstr (s1);
9563 if (p1 != NULL)
9565 const char *r = strpbrk (p1, p2);
9566 tree tem;
9568 if (r == NULL)
9569 return build_int_cst (TREE_TYPE (s1), 0);
9571 /* Return an offset into the constant string argument. */
9572 tem = fold_build_pointer_plus_hwi_loc (loc, s1, r - p1);
9573 return fold_convert_loc (loc, type, tem);
9576 if (p2[0] == '\0')
9577 /* strpbrk(x, "") == NULL.
9578 Evaluate and ignore s1 in case it had side-effects. */
9579 return omit_one_operand_loc (loc, type, integer_zero_node, s1);
9581 if (p2[1] != '\0')
9582 return NULL_TREE; /* Really call strpbrk. */
9584 fn = builtin_decl_implicit (BUILT_IN_STRCHR);
9585 if (!fn)
9586 return NULL_TREE;
9588 /* New argument list transforming strpbrk(s1, s2) to
9589 strchr(s1, s2[0]). */
9590 return build_call_expr_loc (loc, fn, 2, s1,
9591 build_int_cst (integer_type_node, p2[0]));
9595 /* Simplify a call to the strspn builtin. S1 and S2 are the arguments
9596 to the call.
9598 Return NULL_TREE if no simplification was possible, otherwise return the
9599 simplified form of the call as a tree.
9601 The simplified form may be a constant or other expression which
9602 computes the same value, but in a more efficient manner (including
9603 calls to other builtin functions).
9605 The call may contain arguments which need to be evaluated, but
9606 which are not useful to determine the result of the call. In
9607 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9608 COMPOUND_EXPR will be an argument which must be evaluated.
9609 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9610 COMPOUND_EXPR in the chain will contain the tree for the simplified
9611 form of the builtin function call. */
9613 static tree
9614 fold_builtin_strspn (location_t loc, tree s1, tree s2)
9616 if (!validate_arg (s1, POINTER_TYPE)
9617 || !validate_arg (s2, POINTER_TYPE))
9618 return NULL_TREE;
9619 else
9621 const char *p1 = c_getstr (s1), *p2 = c_getstr (s2);
9623 /* If either argument is "", return NULL_TREE. */
9624 if ((p1 && *p1 == '\0') || (p2 && *p2 == '\0'))
9625 /* Evaluate and ignore both arguments in case either one has
9626 side-effects. */
9627 return omit_two_operands_loc (loc, size_type_node, size_zero_node,
9628 s1, s2);
9629 return NULL_TREE;
9633 /* Simplify a call to the strcspn builtin. S1 and S2 are the arguments
9634 to the call.
9636 Return NULL_TREE if no simplification was possible, otherwise return the
9637 simplified form of the call as a tree.
9639 The simplified form may be a constant or other expression which
9640 computes the same value, but in a more efficient manner (including
9641 calls to other builtin functions).
9643 The call may contain arguments which need to be evaluated, but
9644 which are not useful to determine the result of the call. In
9645 this case we return a chain of COMPOUND_EXPRs. The LHS of each
9646 COMPOUND_EXPR will be an argument which must be evaluated.
9647 COMPOUND_EXPRs are chained through their RHS. The RHS of the last
9648 COMPOUND_EXPR in the chain will contain the tree for the simplified
9649 form of the builtin function call. */
9651 static tree
9652 fold_builtin_strcspn (location_t loc, tree s1, tree s2)
9654 if (!validate_arg (s1, POINTER_TYPE)
9655 || !validate_arg (s2, POINTER_TYPE))
9656 return NULL_TREE;
9657 else
9659 /* If the first argument is "", return NULL_TREE. */
9660 const char *p1 = c_getstr (s1);
9661 if (p1 && *p1 == '\0')
9663 /* Evaluate and ignore argument s2 in case it has
9664 side-effects. */
9665 return omit_one_operand_loc (loc, size_type_node,
9666 size_zero_node, s2);
9669 /* If the second argument is "", return __builtin_strlen(s1). */
9670 const char *p2 = c_getstr (s2);
9671 if (p2 && *p2 == '\0')
9673 tree fn = builtin_decl_implicit (BUILT_IN_STRLEN);
9675 /* If the replacement _DECL isn't initialized, don't do the
9676 transformation. */
9677 if (!fn)
9678 return NULL_TREE;
9680 return build_call_expr_loc (loc, fn, 1, s1);
9682 return NULL_TREE;
9686 /* Fold the next_arg or va_start call EXP. Returns true if there was an error
9687 produced. False otherwise. This is done so that we don't output the error
9688 or warning twice or three times. */
9690 bool
9691 fold_builtin_next_arg (tree exp, bool va_start_p)
9693 tree fntype = TREE_TYPE (current_function_decl);
9694 int nargs = call_expr_nargs (exp);
9695 tree arg;
9696 /* There is good chance the current input_location points inside the
9697 definition of the va_start macro (perhaps on the token for
9698 builtin) in a system header, so warnings will not be emitted.
9699 Use the location in real source code. */
9700 source_location current_location =
9701 linemap_unwind_to_first_non_reserved_loc (line_table, input_location,
9702 NULL);
9704 if (!stdarg_p (fntype))
9706 error ("%<va_start%> used in function with fixed args");
9707 return true;
9710 if (va_start_p)
9712 if (va_start_p && (nargs != 2))
9714 error ("wrong number of arguments to function %<va_start%>");
9715 return true;
9717 arg = CALL_EXPR_ARG (exp, 1);
9719 /* We use __builtin_va_start (ap, 0, 0) or __builtin_next_arg (0, 0)
9720 when we checked the arguments and if needed issued a warning. */
9721 else
9723 if (nargs == 0)
9725 /* Evidently an out of date version of <stdarg.h>; can't validate
9726 va_start's second argument, but can still work as intended. */
9727 warning_at (current_location,
9728 OPT_Wvarargs,
9729 "%<__builtin_next_arg%> called without an argument");
9730 return true;
9732 else if (nargs > 1)
9734 error ("wrong number of arguments to function %<__builtin_next_arg%>");
9735 return true;
9737 arg = CALL_EXPR_ARG (exp, 0);
9740 if (TREE_CODE (arg) == SSA_NAME)
9741 arg = SSA_NAME_VAR (arg);
9743 /* We destructively modify the call to be __builtin_va_start (ap, 0)
9744 or __builtin_next_arg (0) the first time we see it, after checking
9745 the arguments and if needed issuing a warning. */
9746 if (!integer_zerop (arg))
9748 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
9750 /* Strip off all nops for the sake of the comparison. This
9751 is not quite the same as STRIP_NOPS. It does more.
9752 We must also strip off INDIRECT_EXPR for C++ reference
9753 parameters. */
9754 while (CONVERT_EXPR_P (arg)
9755 || TREE_CODE (arg) == INDIRECT_REF)
9756 arg = TREE_OPERAND (arg, 0);
9757 if (arg != last_parm)
9759 /* FIXME: Sometimes with the tree optimizers we can get the
9760 not the last argument even though the user used the last
9761 argument. We just warn and set the arg to be the last
9762 argument so that we will get wrong-code because of
9763 it. */
9764 warning_at (current_location,
9765 OPT_Wvarargs,
9766 "second parameter of %<va_start%> not last named argument");
9769 /* Undefined by C99 7.15.1.4p4 (va_start):
9770 "If the parameter parmN is declared with the register storage
9771 class, with a function or array type, or with a type that is
9772 not compatible with the type that results after application of
9773 the default argument promotions, the behavior is undefined."
9775 else if (DECL_REGISTER (arg))
9777 warning_at (current_location,
9778 OPT_Wvarargs,
9779 "undefined behavior when second parameter of "
9780 "%<va_start%> is declared with %<register%> storage");
9783 /* We want to verify the second parameter just once before the tree
9784 optimizers are run and then avoid keeping it in the tree,
9785 as otherwise we could warn even for correct code like:
9786 void foo (int i, ...)
9787 { va_list ap; i++; va_start (ap, i); va_end (ap); } */
9788 if (va_start_p)
9789 CALL_EXPR_ARG (exp, 1) = integer_zero_node;
9790 else
9791 CALL_EXPR_ARG (exp, 0) = integer_zero_node;
9793 return false;
9797 /* Expand a call EXP to __builtin_object_size. */
9799 static rtx
9800 expand_builtin_object_size (tree exp)
9802 tree ost;
9803 int object_size_type;
9804 tree fndecl = get_callee_fndecl (exp);
9806 if (!validate_arglist (exp, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
9808 error ("%Kfirst argument of %qD must be a pointer, second integer constant",
9809 exp, fndecl);
9810 expand_builtin_trap ();
9811 return const0_rtx;
9814 ost = CALL_EXPR_ARG (exp, 1);
9815 STRIP_NOPS (ost);
9817 if (TREE_CODE (ost) != INTEGER_CST
9818 || tree_int_cst_sgn (ost) < 0
9819 || compare_tree_int (ost, 3) > 0)
9821 error ("%Klast argument of %qD is not integer constant between 0 and 3",
9822 exp, fndecl);
9823 expand_builtin_trap ();
9824 return const0_rtx;
9827 object_size_type = tree_to_shwi (ost);
9829 return object_size_type < 2 ? constm1_rtx : const0_rtx;
9832 /* Expand EXP, a call to the __mem{cpy,pcpy,move,set}_chk builtin.
9833 FCODE is the BUILT_IN_* to use.
9834 Return NULL_RTX if we failed; the caller should emit a normal call,
9835 otherwise try to get the result in TARGET, if convenient (and in
9836 mode MODE if that's convenient). */
9838 static rtx
9839 expand_builtin_memory_chk (tree exp, rtx target, machine_mode mode,
9840 enum built_in_function fcode)
9842 if (!validate_arglist (exp,
9843 POINTER_TYPE,
9844 fcode == BUILT_IN_MEMSET_CHK
9845 ? INTEGER_TYPE : POINTER_TYPE,
9846 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
9847 return NULL_RTX;
9849 tree dest = CALL_EXPR_ARG (exp, 0);
9850 tree src = CALL_EXPR_ARG (exp, 1);
9851 tree len = CALL_EXPR_ARG (exp, 2);
9852 tree size = CALL_EXPR_ARG (exp, 3);
9854 bool sizes_ok = check_access (exp, dest, src, len, /*maxread=*/NULL_TREE,
9855 /*str=*/NULL_TREE, size);
9857 if (!tree_fits_uhwi_p (size))
9858 return NULL_RTX;
9860 if (tree_fits_uhwi_p (len) || integer_all_onesp (size))
9862 /* Avoid transforming the checking call to an ordinary one when
9863 an overflow has been detected or when the call couldn't be
9864 validated because the size is not constant. */
9865 if (!sizes_ok && !integer_all_onesp (size) && tree_int_cst_lt (size, len))
9866 return NULL_RTX;
9868 tree fn = NULL_TREE;
9869 /* If __builtin_mem{cpy,pcpy,move,set}_chk is used, assume
9870 mem{cpy,pcpy,move,set} is available. */
9871 switch (fcode)
9873 case BUILT_IN_MEMCPY_CHK:
9874 fn = builtin_decl_explicit (BUILT_IN_MEMCPY);
9875 break;
9876 case BUILT_IN_MEMPCPY_CHK:
9877 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY);
9878 break;
9879 case BUILT_IN_MEMMOVE_CHK:
9880 fn = builtin_decl_explicit (BUILT_IN_MEMMOVE);
9881 break;
9882 case BUILT_IN_MEMSET_CHK:
9883 fn = builtin_decl_explicit (BUILT_IN_MEMSET);
9884 break;
9885 default:
9886 break;
9889 if (! fn)
9890 return NULL_RTX;
9892 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 3, dest, src, len);
9893 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9894 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9895 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9897 else if (fcode == BUILT_IN_MEMSET_CHK)
9898 return NULL_RTX;
9899 else
9901 unsigned int dest_align = get_pointer_alignment (dest);
9903 /* If DEST is not a pointer type, call the normal function. */
9904 if (dest_align == 0)
9905 return NULL_RTX;
9907 /* If SRC and DEST are the same (and not volatile), do nothing. */
9908 if (operand_equal_p (src, dest, 0))
9910 tree expr;
9912 if (fcode != BUILT_IN_MEMPCPY_CHK)
9914 /* Evaluate and ignore LEN in case it has side-effects. */
9915 expand_expr (len, const0_rtx, VOIDmode, EXPAND_NORMAL);
9916 return expand_expr (dest, target, mode, EXPAND_NORMAL);
9919 expr = fold_build_pointer_plus (dest, len);
9920 return expand_expr (expr, target, mode, EXPAND_NORMAL);
9923 /* __memmove_chk special case. */
9924 if (fcode == BUILT_IN_MEMMOVE_CHK)
9926 unsigned int src_align = get_pointer_alignment (src);
9928 if (src_align == 0)
9929 return NULL_RTX;
9931 /* If src is categorized for a readonly section we can use
9932 normal __memcpy_chk. */
9933 if (readonly_data_expr (src))
9935 tree fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
9936 if (!fn)
9937 return NULL_RTX;
9938 fn = build_call_nofold_loc (EXPR_LOCATION (exp), fn, 4,
9939 dest, src, len, size);
9940 gcc_assert (TREE_CODE (fn) == CALL_EXPR);
9941 CALL_EXPR_TAILCALL (fn) = CALL_EXPR_TAILCALL (exp);
9942 return expand_expr (fn, target, mode, EXPAND_NORMAL);
9945 return NULL_RTX;
9949 /* Emit warning if a buffer overflow is detected at compile time. */
9951 static void
9952 maybe_emit_chk_warning (tree exp, enum built_in_function fcode)
9954 /* The source string. */
9955 tree srcstr = NULL_TREE;
9956 /* The size of the destination object. */
9957 tree objsize = NULL_TREE;
9958 /* The string that is being concatenated with (as in __strcat_chk)
9959 or null if it isn't. */
9960 tree catstr = NULL_TREE;
9961 /* The maximum length of the source sequence in a bounded operation
9962 (such as __strncat_chk) or null if the operation isn't bounded
9963 (such as __strcat_chk). */
9964 tree maxread = NULL_TREE;
9965 /* The exact size of the access (such as in __strncpy_chk). */
9966 tree size = NULL_TREE;
9968 switch (fcode)
9970 case BUILT_IN_STRCPY_CHK:
9971 case BUILT_IN_STPCPY_CHK:
9972 srcstr = CALL_EXPR_ARG (exp, 1);
9973 objsize = CALL_EXPR_ARG (exp, 2);
9974 break;
9976 case BUILT_IN_STRCAT_CHK:
9977 /* For __strcat_chk the warning will be emitted only if overflowing
9978 by at least strlen (dest) + 1 bytes. */
9979 catstr = CALL_EXPR_ARG (exp, 0);
9980 srcstr = CALL_EXPR_ARG (exp, 1);
9981 objsize = CALL_EXPR_ARG (exp, 2);
9982 break;
9984 case BUILT_IN_STRNCAT_CHK:
9985 catstr = CALL_EXPR_ARG (exp, 0);
9986 srcstr = CALL_EXPR_ARG (exp, 1);
9987 maxread = CALL_EXPR_ARG (exp, 2);
9988 objsize = CALL_EXPR_ARG (exp, 3);
9989 break;
9991 case BUILT_IN_STRNCPY_CHK:
9992 case BUILT_IN_STPNCPY_CHK:
9993 srcstr = CALL_EXPR_ARG (exp, 1);
9994 size = CALL_EXPR_ARG (exp, 2);
9995 objsize = CALL_EXPR_ARG (exp, 3);
9996 break;
9998 case BUILT_IN_SNPRINTF_CHK:
9999 case BUILT_IN_VSNPRINTF_CHK:
10000 maxread = CALL_EXPR_ARG (exp, 1);
10001 objsize = CALL_EXPR_ARG (exp, 3);
10002 break;
10003 default:
10004 gcc_unreachable ();
10007 if (catstr && maxread)
10009 /* Check __strncat_chk. There is no way to determine the length
10010 of the string to which the source string is being appended so
10011 just warn when the length of the source string is not known. */
10012 check_strncat_sizes (exp, objsize);
10013 return;
10016 /* The destination argument is the first one for all built-ins above. */
10017 tree dst = CALL_EXPR_ARG (exp, 0);
10019 check_access (exp, dst, srcstr, size, maxread, srcstr, objsize);
10022 /* Emit warning if a buffer overflow is detected at compile time
10023 in __sprintf_chk/__vsprintf_chk calls. */
10025 static void
10026 maybe_emit_sprintf_chk_warning (tree exp, enum built_in_function fcode)
10028 tree size, len, fmt;
10029 const char *fmt_str;
10030 int nargs = call_expr_nargs (exp);
10032 /* Verify the required arguments in the original call. */
10034 if (nargs < 4)
10035 return;
10036 size = CALL_EXPR_ARG (exp, 2);
10037 fmt = CALL_EXPR_ARG (exp, 3);
10039 if (! tree_fits_uhwi_p (size) || integer_all_onesp (size))
10040 return;
10042 /* Check whether the format is a literal string constant. */
10043 fmt_str = c_getstr (fmt);
10044 if (fmt_str == NULL)
10045 return;
10047 if (!init_target_chars ())
10048 return;
10050 /* If the format doesn't contain % args or %%, we know its size. */
10051 if (strchr (fmt_str, target_percent) == 0)
10052 len = build_int_cstu (size_type_node, strlen (fmt_str));
10053 /* If the format is "%s" and first ... argument is a string literal,
10054 we know it too. */
10055 else if (fcode == BUILT_IN_SPRINTF_CHK
10056 && strcmp (fmt_str, target_percent_s) == 0)
10058 tree arg;
10060 if (nargs < 5)
10061 return;
10062 arg = CALL_EXPR_ARG (exp, 4);
10063 if (! POINTER_TYPE_P (TREE_TYPE (arg)))
10064 return;
10066 len = c_strlen (arg, 1);
10067 if (!len || ! tree_fits_uhwi_p (len))
10068 return;
10070 else
10071 return;
10073 /* Add one for the terminating nul. */
10074 len = fold_build2 (PLUS_EXPR, TREE_TYPE (len), len, size_one_node);
10076 check_access (exp, /*dst=*/NULL_TREE, /*src=*/NULL_TREE, /*size=*/NULL_TREE,
10077 /*maxread=*/NULL_TREE, len, size);
10080 /* Emit warning if a free is called with address of a variable. */
10082 static void
10083 maybe_emit_free_warning (tree exp)
10085 tree arg = CALL_EXPR_ARG (exp, 0);
10087 STRIP_NOPS (arg);
10088 if (TREE_CODE (arg) != ADDR_EXPR)
10089 return;
10091 arg = get_base_address (TREE_OPERAND (arg, 0));
10092 if (arg == NULL || INDIRECT_REF_P (arg) || TREE_CODE (arg) == MEM_REF)
10093 return;
10095 if (SSA_VAR_P (arg))
10096 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
10097 "%Kattempt to free a non-heap object %qD", exp, arg);
10098 else
10099 warning_at (tree_nonartificial_location (exp), OPT_Wfree_nonheap_object,
10100 "%Kattempt to free a non-heap object", exp);
10103 /* Fold a call to __builtin_object_size with arguments PTR and OST,
10104 if possible. */
10106 static tree
10107 fold_builtin_object_size (tree ptr, tree ost)
10109 unsigned HOST_WIDE_INT bytes;
10110 int object_size_type;
10112 if (!validate_arg (ptr, POINTER_TYPE)
10113 || !validate_arg (ost, INTEGER_TYPE))
10114 return NULL_TREE;
10116 STRIP_NOPS (ost);
10118 if (TREE_CODE (ost) != INTEGER_CST
10119 || tree_int_cst_sgn (ost) < 0
10120 || compare_tree_int (ost, 3) > 0)
10121 return NULL_TREE;
10123 object_size_type = tree_to_shwi (ost);
10125 /* __builtin_object_size doesn't evaluate side-effects in its arguments;
10126 if there are any side-effects, it returns (size_t) -1 for types 0 and 1
10127 and (size_t) 0 for types 2 and 3. */
10128 if (TREE_SIDE_EFFECTS (ptr))
10129 return build_int_cst_type (size_type_node, object_size_type < 2 ? -1 : 0);
10131 if (TREE_CODE (ptr) == ADDR_EXPR)
10133 compute_builtin_object_size (ptr, object_size_type, &bytes);
10134 if (wi::fits_to_tree_p (bytes, size_type_node))
10135 return build_int_cstu (size_type_node, bytes);
10137 else if (TREE_CODE (ptr) == SSA_NAME)
10139 /* If object size is not known yet, delay folding until
10140 later. Maybe subsequent passes will help determining
10141 it. */
10142 if (compute_builtin_object_size (ptr, object_size_type, &bytes)
10143 && wi::fits_to_tree_p (bytes, size_type_node))
10144 return build_int_cstu (size_type_node, bytes);
10147 return NULL_TREE;
10150 /* Builtins with folding operations that operate on "..." arguments
10151 need special handling; we need to store the arguments in a convenient
10152 data structure before attempting any folding. Fortunately there are
10153 only a few builtins that fall into this category. FNDECL is the
10154 function, EXP is the CALL_EXPR for the call. */
10156 static tree
10157 fold_builtin_varargs (location_t loc, tree fndecl, tree *args, int nargs)
10159 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10160 tree ret = NULL_TREE;
10162 switch (fcode)
10164 case BUILT_IN_FPCLASSIFY:
10165 ret = fold_builtin_fpclassify (loc, args, nargs);
10166 break;
10168 default:
10169 break;
10171 if (ret)
10173 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
10174 SET_EXPR_LOCATION (ret, loc);
10175 TREE_NO_WARNING (ret) = 1;
10176 return ret;
10178 return NULL_TREE;
10181 /* Initialize format string characters in the target charset. */
10183 bool
10184 init_target_chars (void)
10186 static bool init;
10187 if (!init)
10189 target_newline = lang_hooks.to_target_charset ('\n');
10190 target_percent = lang_hooks.to_target_charset ('%');
10191 target_c = lang_hooks.to_target_charset ('c');
10192 target_s = lang_hooks.to_target_charset ('s');
10193 if (target_newline == 0 || target_percent == 0 || target_c == 0
10194 || target_s == 0)
10195 return false;
10197 target_percent_c[0] = target_percent;
10198 target_percent_c[1] = target_c;
10199 target_percent_c[2] = '\0';
10201 target_percent_s[0] = target_percent;
10202 target_percent_s[1] = target_s;
10203 target_percent_s[2] = '\0';
10205 target_percent_s_newline[0] = target_percent;
10206 target_percent_s_newline[1] = target_s;
10207 target_percent_s_newline[2] = target_newline;
10208 target_percent_s_newline[3] = '\0';
10210 init = true;
10212 return true;
10215 /* Helper function for do_mpfr_arg*(). Ensure M is a normal number
10216 and no overflow/underflow occurred. INEXACT is true if M was not
10217 exactly calculated. TYPE is the tree type for the result. This
10218 function assumes that you cleared the MPFR flags and then
10219 calculated M to see if anything subsequently set a flag prior to
10220 entering this function. Return NULL_TREE if any checks fail. */
10222 static tree
10223 do_mpfr_ckconv (mpfr_srcptr m, tree type, int inexact)
10225 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10226 overflow/underflow occurred. If -frounding-math, proceed iff the
10227 result of calling FUNC was exact. */
10228 if (mpfr_number_p (m) && !mpfr_overflow_p () && !mpfr_underflow_p ()
10229 && (!flag_rounding_math || !inexact))
10231 REAL_VALUE_TYPE rr;
10233 real_from_mpfr (&rr, m, type, GMP_RNDN);
10234 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR value,
10235 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10236 but the mpft_t is not, then we underflowed in the
10237 conversion. */
10238 if (real_isfinite (&rr)
10239 && (rr.cl == rvc_zero) == (mpfr_zero_p (m) != 0))
10241 REAL_VALUE_TYPE rmode;
10243 real_convert (&rmode, TYPE_MODE (type), &rr);
10244 /* Proceed iff the specified mode can hold the value. */
10245 if (real_identical (&rmode, &rr))
10246 return build_real (type, rmode);
10249 return NULL_TREE;
10252 /* Helper function for do_mpc_arg*(). Ensure M is a normal complex
10253 number and no overflow/underflow occurred. INEXACT is true if M
10254 was not exactly calculated. TYPE is the tree type for the result.
10255 This function assumes that you cleared the MPFR flags and then
10256 calculated M to see if anything subsequently set a flag prior to
10257 entering this function. Return NULL_TREE if any checks fail, if
10258 FORCE_CONVERT is true, then bypass the checks. */
10260 static tree
10261 do_mpc_ckconv (mpc_srcptr m, tree type, int inexact, int force_convert)
10263 /* Proceed iff we get a normal number, i.e. not NaN or Inf and no
10264 overflow/underflow occurred. If -frounding-math, proceed iff the
10265 result of calling FUNC was exact. */
10266 if (force_convert
10267 || (mpfr_number_p (mpc_realref (m)) && mpfr_number_p (mpc_imagref (m))
10268 && !mpfr_overflow_p () && !mpfr_underflow_p ()
10269 && (!flag_rounding_math || !inexact)))
10271 REAL_VALUE_TYPE re, im;
10273 real_from_mpfr (&re, mpc_realref (m), TREE_TYPE (type), GMP_RNDN);
10274 real_from_mpfr (&im, mpc_imagref (m), TREE_TYPE (type), GMP_RNDN);
10275 /* Proceed iff GCC's REAL_VALUE_TYPE can hold the MPFR values,
10276 check for overflow/underflow. If the REAL_VALUE_TYPE is zero
10277 but the mpft_t is not, then we underflowed in the
10278 conversion. */
10279 if (force_convert
10280 || (real_isfinite (&re) && real_isfinite (&im)
10281 && (re.cl == rvc_zero) == (mpfr_zero_p (mpc_realref (m)) != 0)
10282 && (im.cl == rvc_zero) == (mpfr_zero_p (mpc_imagref (m)) != 0)))
10284 REAL_VALUE_TYPE re_mode, im_mode;
10286 real_convert (&re_mode, TYPE_MODE (TREE_TYPE (type)), &re);
10287 real_convert (&im_mode, TYPE_MODE (TREE_TYPE (type)), &im);
10288 /* Proceed iff the specified mode can hold the value. */
10289 if (force_convert
10290 || (real_identical (&re_mode, &re)
10291 && real_identical (&im_mode, &im)))
10292 return build_complex (type, build_real (TREE_TYPE (type), re_mode),
10293 build_real (TREE_TYPE (type), im_mode));
10296 return NULL_TREE;
10299 /* If arguments ARG0 and ARG1 are REAL_CSTs, call mpfr_remquo() to set
10300 the pointer *(ARG_QUO) and return the result. The type is taken
10301 from the type of ARG0 and is used for setting the precision of the
10302 calculation and results. */
10304 static tree
10305 do_mpfr_remquo (tree arg0, tree arg1, tree arg_quo)
10307 tree const type = TREE_TYPE (arg0);
10308 tree result = NULL_TREE;
10310 STRIP_NOPS (arg0);
10311 STRIP_NOPS (arg1);
10313 /* To proceed, MPFR must exactly represent the target floating point
10314 format, which only happens when the target base equals two. */
10315 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10316 && TREE_CODE (arg0) == REAL_CST && !TREE_OVERFLOW (arg0)
10317 && TREE_CODE (arg1) == REAL_CST && !TREE_OVERFLOW (arg1))
10319 const REAL_VALUE_TYPE *const ra0 = TREE_REAL_CST_PTR (arg0);
10320 const REAL_VALUE_TYPE *const ra1 = TREE_REAL_CST_PTR (arg1);
10322 if (real_isfinite (ra0) && real_isfinite (ra1))
10324 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10325 const int prec = fmt->p;
10326 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
10327 tree result_rem;
10328 long integer_quo;
10329 mpfr_t m0, m1;
10331 mpfr_inits2 (prec, m0, m1, NULL);
10332 mpfr_from_real (m0, ra0, GMP_RNDN);
10333 mpfr_from_real (m1, ra1, GMP_RNDN);
10334 mpfr_clear_flags ();
10335 mpfr_remquo (m0, &integer_quo, m0, m1, rnd);
10336 /* Remquo is independent of the rounding mode, so pass
10337 inexact=0 to do_mpfr_ckconv(). */
10338 result_rem = do_mpfr_ckconv (m0, type, /*inexact=*/ 0);
10339 mpfr_clears (m0, m1, NULL);
10340 if (result_rem)
10342 /* MPFR calculates quo in the host's long so it may
10343 return more bits in quo than the target int can hold
10344 if sizeof(host long) > sizeof(target int). This can
10345 happen even for native compilers in LP64 mode. In
10346 these cases, modulo the quo value with the largest
10347 number that the target int can hold while leaving one
10348 bit for the sign. */
10349 if (sizeof (integer_quo) * CHAR_BIT > INT_TYPE_SIZE)
10350 integer_quo %= (long)(1UL << (INT_TYPE_SIZE - 1));
10352 /* Dereference the quo pointer argument. */
10353 arg_quo = build_fold_indirect_ref (arg_quo);
10354 /* Proceed iff a valid pointer type was passed in. */
10355 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg_quo)) == integer_type_node)
10357 /* Set the value. */
10358 tree result_quo
10359 = fold_build2 (MODIFY_EXPR, TREE_TYPE (arg_quo), arg_quo,
10360 build_int_cst (TREE_TYPE (arg_quo),
10361 integer_quo));
10362 TREE_SIDE_EFFECTS (result_quo) = 1;
10363 /* Combine the quo assignment with the rem. */
10364 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10365 result_quo, result_rem));
10370 return result;
10373 /* If ARG is a REAL_CST, call mpfr_lgamma() on it and return the
10374 resulting value as a tree with type TYPE. The mpfr precision is
10375 set to the precision of TYPE. We assume that this mpfr function
10376 returns zero if the result could be calculated exactly within the
10377 requested precision. In addition, the integer pointer represented
10378 by ARG_SG will be dereferenced and set to the appropriate signgam
10379 (-1,1) value. */
10381 static tree
10382 do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type)
10384 tree result = NULL_TREE;
10386 STRIP_NOPS (arg);
10388 /* To proceed, MPFR must exactly represent the target floating point
10389 format, which only happens when the target base equals two. Also
10390 verify ARG is a constant and that ARG_SG is an int pointer. */
10391 if (REAL_MODE_FORMAT (TYPE_MODE (type))->b == 2
10392 && TREE_CODE (arg) == REAL_CST && !TREE_OVERFLOW (arg)
10393 && TREE_CODE (TREE_TYPE (arg_sg)) == POINTER_TYPE
10394 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (arg_sg))) == integer_type_node)
10396 const REAL_VALUE_TYPE *const ra = TREE_REAL_CST_PTR (arg);
10398 /* In addition to NaN and Inf, the argument cannot be zero or a
10399 negative integer. */
10400 if (real_isfinite (ra)
10401 && ra->cl != rvc_zero
10402 && !(real_isneg (ra) && real_isinteger (ra, TYPE_MODE (type))))
10404 const struct real_format *fmt = REAL_MODE_FORMAT (TYPE_MODE (type));
10405 const int prec = fmt->p;
10406 const mp_rnd_t rnd = fmt->round_towards_zero? GMP_RNDZ : GMP_RNDN;
10407 int inexact, sg;
10408 mpfr_t m;
10409 tree result_lg;
10411 mpfr_init2 (m, prec);
10412 mpfr_from_real (m, ra, GMP_RNDN);
10413 mpfr_clear_flags ();
10414 inexact = mpfr_lgamma (m, &sg, m, rnd);
10415 result_lg = do_mpfr_ckconv (m, type, inexact);
10416 mpfr_clear (m);
10417 if (result_lg)
10419 tree result_sg;
10421 /* Dereference the arg_sg pointer argument. */
10422 arg_sg = build_fold_indirect_ref (arg_sg);
10423 /* Assign the signgam value into *arg_sg. */
10424 result_sg = fold_build2 (MODIFY_EXPR,
10425 TREE_TYPE (arg_sg), arg_sg,
10426 build_int_cst (TREE_TYPE (arg_sg), sg));
10427 TREE_SIDE_EFFECTS (result_sg) = 1;
10428 /* Combine the signgam assignment with the lgamma result. */
10429 result = non_lvalue (fold_build2 (COMPOUND_EXPR, type,
10430 result_sg, result_lg));
10435 return result;
10438 /* If arguments ARG0 and ARG1 are a COMPLEX_CST, call the two-argument
10439 mpc function FUNC on it and return the resulting value as a tree
10440 with type TYPE. The mpfr precision is set to the precision of
10441 TYPE. We assume that function FUNC returns zero if the result
10442 could be calculated exactly within the requested precision. If
10443 DO_NONFINITE is true, then fold expressions containing Inf or NaN
10444 in the arguments and/or results. */
10446 tree
10447 do_mpc_arg2 (tree arg0, tree arg1, tree type, int do_nonfinite,
10448 int (*func)(mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t))
10450 tree result = NULL_TREE;
10452 STRIP_NOPS (arg0);
10453 STRIP_NOPS (arg1);
10455 /* To proceed, MPFR must exactly represent the target floating point
10456 format, which only happens when the target base equals two. */
10457 if (TREE_CODE (arg0) == COMPLEX_CST && !TREE_OVERFLOW (arg0)
10458 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg0))) == REAL_TYPE
10459 && TREE_CODE (arg1) == COMPLEX_CST && !TREE_OVERFLOW (arg1)
10460 && TREE_CODE (TREE_TYPE (TREE_TYPE (arg1))) == REAL_TYPE
10461 && REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0))))->b == 2)
10463 const REAL_VALUE_TYPE *const re0 = TREE_REAL_CST_PTR (TREE_REALPART (arg0));
10464 const REAL_VALUE_TYPE *const im0 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg0));
10465 const REAL_VALUE_TYPE *const re1 = TREE_REAL_CST_PTR (TREE_REALPART (arg1));
10466 const REAL_VALUE_TYPE *const im1 = TREE_REAL_CST_PTR (TREE_IMAGPART (arg1));
10468 if (do_nonfinite
10469 || (real_isfinite (re0) && real_isfinite (im0)
10470 && real_isfinite (re1) && real_isfinite (im1)))
10472 const struct real_format *const fmt =
10473 REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (type)));
10474 const int prec = fmt->p;
10475 const mp_rnd_t rnd = fmt->round_towards_zero ? GMP_RNDZ : GMP_RNDN;
10476 const mpc_rnd_t crnd = fmt->round_towards_zero ? MPC_RNDZZ : MPC_RNDNN;
10477 int inexact;
10478 mpc_t m0, m1;
10480 mpc_init2 (m0, prec);
10481 mpc_init2 (m1, prec);
10482 mpfr_from_real (mpc_realref (m0), re0, rnd);
10483 mpfr_from_real (mpc_imagref (m0), im0, rnd);
10484 mpfr_from_real (mpc_realref (m1), re1, rnd);
10485 mpfr_from_real (mpc_imagref (m1), im1, rnd);
10486 mpfr_clear_flags ();
10487 inexact = func (m0, m0, m1, crnd);
10488 result = do_mpc_ckconv (m0, type, inexact, do_nonfinite);
10489 mpc_clear (m0);
10490 mpc_clear (m1);
10494 return result;
10497 /* A wrapper function for builtin folding that prevents warnings for
10498 "statement without effect" and the like, caused by removing the
10499 call node earlier than the warning is generated. */
10501 tree
10502 fold_call_stmt (gcall *stmt, bool ignore)
10504 tree ret = NULL_TREE;
10505 tree fndecl = gimple_call_fndecl (stmt);
10506 location_t loc = gimple_location (stmt);
10507 if (fndecl
10508 && TREE_CODE (fndecl) == FUNCTION_DECL
10509 && DECL_BUILT_IN (fndecl)
10510 && !gimple_call_va_arg_pack_p (stmt))
10512 int nargs = gimple_call_num_args (stmt);
10513 tree *args = (nargs > 0
10514 ? gimple_call_arg_ptr (stmt, 0)
10515 : &error_mark_node);
10517 if (avoid_folding_inline_builtin (fndecl))
10518 return NULL_TREE;
10519 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
10521 return targetm.fold_builtin (fndecl, nargs, args, ignore);
10523 else
10525 ret = fold_builtin_n (loc, fndecl, args, nargs, ignore);
10526 if (ret)
10528 /* Propagate location information from original call to
10529 expansion of builtin. Otherwise things like
10530 maybe_emit_chk_warning, that operate on the expansion
10531 of a builtin, will use the wrong location information. */
10532 if (gimple_has_location (stmt))
10534 tree realret = ret;
10535 if (TREE_CODE (ret) == NOP_EXPR)
10536 realret = TREE_OPERAND (ret, 0);
10537 if (CAN_HAVE_LOCATION_P (realret)
10538 && !EXPR_HAS_LOCATION (realret))
10539 SET_EXPR_LOCATION (realret, loc);
10540 return realret;
10542 return ret;
10546 return NULL_TREE;
10549 /* Look up the function in builtin_decl that corresponds to DECL
10550 and set ASMSPEC as its user assembler name. DECL must be a
10551 function decl that declares a builtin. */
10553 void
10554 set_builtin_user_assembler_name (tree decl, const char *asmspec)
10556 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
10557 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
10558 && asmspec != 0);
10560 tree builtin = builtin_decl_explicit (DECL_FUNCTION_CODE (decl));
10561 set_user_assembler_name (builtin, asmspec);
10563 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_FFS
10564 && INT_TYPE_SIZE < BITS_PER_WORD)
10566 scalar_int_mode mode = int_mode_for_size (INT_TYPE_SIZE, 0).require ();
10567 set_user_assembler_libfunc ("ffs", asmspec);
10568 set_optab_libfunc (ffs_optab, mode, "ffs");
10572 /* Return true if DECL is a builtin that expands to a constant or similarly
10573 simple code. */
10574 bool
10575 is_simple_builtin (tree decl)
10577 if (decl && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10578 switch (DECL_FUNCTION_CODE (decl))
10580 /* Builtins that expand to constants. */
10581 case BUILT_IN_CONSTANT_P:
10582 case BUILT_IN_EXPECT:
10583 case BUILT_IN_OBJECT_SIZE:
10584 case BUILT_IN_UNREACHABLE:
10585 /* Simple register moves or loads from stack. */
10586 case BUILT_IN_ASSUME_ALIGNED:
10587 case BUILT_IN_RETURN_ADDRESS:
10588 case BUILT_IN_EXTRACT_RETURN_ADDR:
10589 case BUILT_IN_FROB_RETURN_ADDR:
10590 case BUILT_IN_RETURN:
10591 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
10592 case BUILT_IN_FRAME_ADDRESS:
10593 case BUILT_IN_VA_END:
10594 case BUILT_IN_STACK_SAVE:
10595 case BUILT_IN_STACK_RESTORE:
10596 /* Exception state returns or moves registers around. */
10597 case BUILT_IN_EH_FILTER:
10598 case BUILT_IN_EH_POINTER:
10599 case BUILT_IN_EH_COPY_VALUES:
10600 return true;
10602 default:
10603 return false;
10606 return false;
10609 /* Return true if DECL is a builtin that is not expensive, i.e., they are
10610 most probably expanded inline into reasonably simple code. This is a
10611 superset of is_simple_builtin. */
10612 bool
10613 is_inexpensive_builtin (tree decl)
10615 if (!decl)
10616 return false;
10617 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_MD)
10618 return true;
10619 else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
10620 switch (DECL_FUNCTION_CODE (decl))
10622 case BUILT_IN_ABS:
10623 CASE_BUILT_IN_ALLOCA:
10624 case BUILT_IN_BSWAP16:
10625 case BUILT_IN_BSWAP32:
10626 case BUILT_IN_BSWAP64:
10627 case BUILT_IN_CLZ:
10628 case BUILT_IN_CLZIMAX:
10629 case BUILT_IN_CLZL:
10630 case BUILT_IN_CLZLL:
10631 case BUILT_IN_CTZ:
10632 case BUILT_IN_CTZIMAX:
10633 case BUILT_IN_CTZL:
10634 case BUILT_IN_CTZLL:
10635 case BUILT_IN_FFS:
10636 case BUILT_IN_FFSIMAX:
10637 case BUILT_IN_FFSL:
10638 case BUILT_IN_FFSLL:
10639 case BUILT_IN_IMAXABS:
10640 case BUILT_IN_FINITE:
10641 case BUILT_IN_FINITEF:
10642 case BUILT_IN_FINITEL:
10643 case BUILT_IN_FINITED32:
10644 case BUILT_IN_FINITED64:
10645 case BUILT_IN_FINITED128:
10646 case BUILT_IN_FPCLASSIFY:
10647 case BUILT_IN_ISFINITE:
10648 case BUILT_IN_ISINF_SIGN:
10649 case BUILT_IN_ISINF:
10650 case BUILT_IN_ISINFF:
10651 case BUILT_IN_ISINFL:
10652 case BUILT_IN_ISINFD32:
10653 case BUILT_IN_ISINFD64:
10654 case BUILT_IN_ISINFD128:
10655 case BUILT_IN_ISNAN:
10656 case BUILT_IN_ISNANF:
10657 case BUILT_IN_ISNANL:
10658 case BUILT_IN_ISNAND32:
10659 case BUILT_IN_ISNAND64:
10660 case BUILT_IN_ISNAND128:
10661 case BUILT_IN_ISNORMAL:
10662 case BUILT_IN_ISGREATER:
10663 case BUILT_IN_ISGREATEREQUAL:
10664 case BUILT_IN_ISLESS:
10665 case BUILT_IN_ISLESSEQUAL:
10666 case BUILT_IN_ISLESSGREATER:
10667 case BUILT_IN_ISUNORDERED:
10668 case BUILT_IN_VA_ARG_PACK:
10669 case BUILT_IN_VA_ARG_PACK_LEN:
10670 case BUILT_IN_VA_COPY:
10671 case BUILT_IN_TRAP:
10672 case BUILT_IN_SAVEREGS:
10673 case BUILT_IN_POPCOUNTL:
10674 case BUILT_IN_POPCOUNTLL:
10675 case BUILT_IN_POPCOUNTIMAX:
10676 case BUILT_IN_POPCOUNT:
10677 case BUILT_IN_PARITYL:
10678 case BUILT_IN_PARITYLL:
10679 case BUILT_IN_PARITYIMAX:
10680 case BUILT_IN_PARITY:
10681 case BUILT_IN_LABS:
10682 case BUILT_IN_LLABS:
10683 case BUILT_IN_PREFETCH:
10684 case BUILT_IN_ACC_ON_DEVICE:
10685 return true;
10687 default:
10688 return is_simple_builtin (decl);
10691 return false;
10694 /* Return true if T is a constant and the value cast to a target char
10695 can be represented by a host char.
10696 Store the casted char constant in *P if so. */
10698 bool
10699 target_char_cst_p (tree t, char *p)
10701 if (!tree_fits_uhwi_p (t) || CHAR_TYPE_SIZE != HOST_BITS_PER_CHAR)
10702 return false;
10704 *p = (char)tree_to_uhwi (t);
10705 return true;
10708 /* Return the maximum object size. */
10710 tree
10711 max_object_size (void)
10713 /* To do: Make this a configurable parameter. */
10714 return TYPE_MAX_VALUE (ptrdiff_type_node);